I have an Android device connected to Wifi network with an IP address. I did NS lookup on my Linux computer of this IP address and verified that there is a corresponding hostname for that IP address.
I have a piece of Java code, which when run on my Windows PC does reverse DNS lookup fine (returns a hostname):
String dnSuffix;
String ipAddress = "10.228.59.217";
InetAddress inetAddr;
try {
//inetAddr = InetAddress.getLocalHost();
inetAddr = InetAddress.getByName(ipAddress);
//System.out.println("inetAddr = " + inetAddr);
Log.v(LOG_TAG, "inetAddr = " + inetAddr);
if (inetAddr != null) {
dnSuffix = inetAddr.getHostName();
//System.out.println("dnSuffix is " + dnSuffix);
Log.v(LOG_TAG,"dnSuffix is " + dnSuffix);
}
} catch (UnknownHostException e) {
//System.out.println("Error getting DN suffix: " + e.getMessage());
Log.v(LOG_TAG,"Error getting DN suffix: " + e.getMessage());
}
dnSuffix is the hostname as expected on Windows.
But on Android, it returns an IP address instead of a hostname, which indicates that it failed.
I have all the permissions that I would think I need for this in my app:
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" ></uses-permission>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
I found this documentation:
getCanonicalHostName
public String getCanonicalHostName()
Gets the fully qualified domain name for this IP address. Best effort
method, meaning we may not be able to return the FQDN depending on the
underlying system configuration. If there is a security manager, this
method first calls its checkConnect method with the hostname and -1 as
its arguments to see if the calling code is allowed to know the
hostname for this IP address, i.e., to connect to the host. If the
operation is not allowed, it will return the textual representation of
the IP address.
Returns:
the fully qualified domain name for this IP address, or if the
operation is not allowed by the security check, the textual
representation of the IP address.
Since:
1.4
See Also:
SecurityManager.checkConnect(java.lang.String, int)
I also found someone having similar problem:
getCanonicalHostName returns an IP address
But no resolution.
Can anyone provide any help at all?
I'm really not sure it can works at all, following 2.3 sources (shortened)
inetAddr = InetAddress.getByName(ipAddress);
getByName(numeric) -> getAllByName/Impl(numeric) -> lookupHostByName(numeric)
return bytesToInetAddresses(getaddrinfo(host), host)[0]
host is the numeric string, then calling getHostName() on the resulting object will return host (the ip)
Related
I need to return the IP address for the device running my React Native app (an Android smart tv app). I am making use of react-native-device-info which has allowed me to get the model, manufacturer and operating system. However I am unable to get the ip address.
This is my code
deviceInfo = DeviceInfo.getIPAddress().then(ip => {
return ip;
});
However on the front end it appears as [object Object]. I can see in the console it is returning an object like this:
wifi:
_40: 0
_55: null
_65: 0
_72: null
I would have hoped to just return a string of the correct IP address.
I have also added the right permissions in my AndroidManifest.xml.
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
Also worth noting I am passing the information back by value: ${JSON.stringify(deviceInfo)}
Any one experienced this issue before?
I have used below library:
https://www.npmjs.com/package/react-native-network-info
And it is working fine, Below is the code:
// Get IPv4 IP (priority: WiFi first, cellular second)
NetworkInfo.getIPV4Address().then(ipv4Address => {
console.log(ipv4Address); //result e.g 192.168.1.100
});
Since its returning a promise try putting inside a async function and try to get the result.
Ex:
const getIpAddress = async()=>{
const ip = await DeviceInfo.getIPAddress();
console.log(ip);
return ip;
}
Hope it helps. Thank you.
I am trying to display the ip address of connected networks in android. I am using the following code. But it returns 2 IPv6 ip address. How to find the correct ip address from that?
I am using the following code:
List<LinkAddress> linkAddresses = connectivityManager.getLinkProperties(connectivityManager.getActiveNetwork()).getLinkAddresses();
for (LinkAddress linkAddress : linkAddresses) {
Log.i("","LinkAddress getAddress "+linkAddress.getAddress() + "");
Log.i("","Is IPV6 " + (linkAddress.getAddress() instanceof Inet6Address) +"");
Log.i("","Is IPV4 " + (linkAddress.getAddress() instanceof Inet4Address) +"");
Log.i("","Is isLinkLocalAddress " + (linkAddress.getAddress().isLinkLocalAddress()) +"");
Log.i("","Is not isLoopbackAddress " + (!linkAddress.getAddress().isLoopbackAddress()) +"");
}
Now I am getting 4 ip addresses.
LinkAddress getAddress /fe80::2d0:caff:fe00:5ad6
LinkAddress getAddress /2401:4900:2305:14e:2d0:caff:fe00:5ad6
LinkAddress getAddress /2401:4900:2305:14e:28e2:5192:e38f:3e9
LinkAddress getAddress /192.168.43.176
I can identify fe80 is Link Local Ip address and 192. is IPv4 address. But I am confused to identify IPV6 address from this. Please help me to find out the IPv6 ip address.
Both IPv6 addresses are valid. It's normal to have multiple addresses per interface. Both are in the same subnet (2401:4900:2305:14e::/64). When you look at the interface ID (the second half of the address) you'll see that one has ..ff:fe.. in the middle. That's a sign that that address is probably derived from the MAC address of the interface. The other address is a temporary address that will change over time to protect the privacy of the user.
But in short: both addresses are completely valid and usable.
I created a database on cpanel and i choosed a name and pass for it.
So i declared "database","password","user name","localhost" in services.php.
Services.php:
<?php
if (isset($_REQUEST['action'])){
$foo = $_REQUEST['action'];
}else{
echo "Invalid Data";
exit;
}
/*************************************************/
function connectToDatabase()
{
$servername ="localhost";
$username = "us_utab";
$password = "sk3%CkvH";
$dbname = "us_utab";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
$conn->set_charset("utf8");
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
return $conn;
}
/*************************************************/
if ($foo == 'search')
{ search(); }
if ($foo == 'read')
{ read();}
if ($foo == 'insert')
{ insert();}
.
.
.
I changed my android code.
HttpPost method = new HttpPost("http://192.168.1.2/test-website/services.php?action=read");
To :
HttpPost method = new HttpPost("https://utab.com:2083/services.php?
action=read");
Is this correct?
It is not need to use database name , password , user name in android place?
It does not connect to server . so I deleted port 2083 and I am encountering error in below :
javax.net.ssl.SSLPeerUnverifiedException: No peer certificate
Well your code seems to be correct and not the problem in this case.
It is not need to use database name , password , user name in android place?
Nope you don't have to if the PHP file already contains these values.
The actual error we are seeing is a SSLPeerUnverifiedException which means that the Certificate of https://utab.com has some issues. So you need to check on that. Also check the date of your device, is it correct?
javax.net.ssl.SSLPeerUnverifiedException: No peer certificate
That means you are trying to do a secure https connection to a http server that doesn't expect for secure connections.
$servername ="localhost";
$username = "us_utab";
$password = "sk3%CkvH";
$dbname = "us_utab";
Is that the only contents of services.php ? If yes then it lacks all the code to connect to the database, get arguments from the http request, make probably some database query and return some response. It seems that if the only one argument is action=read it is not clear what it must respond.
UPDATE
After reading your comment, if the server is accessible from the same computer but not from another computer then you have an IP routing problem, not a programming problem. May the computer on the server resides has a firewall that blocks incoming connections, or its network interface is misconfigured.
Take also in account that inside a LAN the public IP with which the router connects to the WAN is not accessible from the LAN computers. For testing inside the LAN you have to use the private LAN IPs, for example 192.168.1.2, not "utab.com"
I can able to fetch all device ip addresses in Local Area Network with inetaddress class. What I need to do is reverse lookup ip-address and find as device name in network like : "Jimmie's Macbook"
My Code block which able to fetch all IP address over Local Network Range:
private ArrayList<String> scanSubNet(String subnet) {
ArrayList<String> hosts = new ArrayList<>();
InetAddress inetAddress;
for (int i = 1; i <= 255; i++) {
try {
inetAddress = InetAddress.getByName(subnet + String.valueOf(i));
if (inetAddress.isReachable(1000)) {
hosts.add(inetAddress.getHostName());
Log.d(TAG, InetAddress.getByAddress(inetAddress.getAddress()).getHostAddress());
Log.d(TAG, InetAddress.getByAddress(inetAddress.getAddress()).getCanonicalHostName());
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
return hosts;
}
And i am calling my method as;
ArrayList<String> subnetList = scanSubNet("192.168.1.");ArrayList<String> subnetList = scanSubNet("192.168.1.");
in Log.d(TAG, i am trying to get device name with reverse dns. But both of line gives me output as ip-address ( Not Device-Name as string)
Is there any chance to succeed it ?
Regards,
Onder.
I just do it with fetching MACID and match first 3digits which belongs manufacturers.
https://macvendors.com/ this website also provide api (Post/GET) to resolve MAC Address.
Instead of resolve fullname of MAC, you need to do Handshake peer to peer.
This is probably happening due to router misconfiguration.
Within a LAN, there are no crucial functions that depend on successful reverse DNS lookups, so a misconfiguration of that kind can easily go undetected for a long time.
It is kind of hard to tell what is wrong in your particular case without a lot more information about your LAN, but the first thing that comes to mind is configuring a proper "DNS Suffix" on the router. This is usually found under DHCP settings.
i am using PJSIP for voice calling. When i used our server, everything fine i.e. call connected, communicate. But when i am using SIP2SIP.INFO server. Registration is OK But Call is not connected. i saw log in SIP2SIP.info there wasn't log of outgoing or incoming call.
so call is not initiate.
char cfg_reg_uri[] = "sip:sip2sip.info";
char cfg_cred_realm[] = "sip2sip.info";
char cfg_cred_scheme[]="digest";
pjsua_acc_config cfg;
pjsua_acc_config_default(&cfg);
cfg.id = pj_str(cfg_id);
cfg.reg_uri = pj_str(cfg_reg_uri);
cfg.cred_count = 1;
cfg.cred_info[0].realm = pj_str(cfg_cred_realm);
cfg.cred_info[0].scheme = pj_str(cfg_cred_scheme);
cfg.cred_info[0].username = pj_str(cfg_cred_username);
cfg.cred_info[0].data_type = PJSIP_CRED_DATA_PLAIN_PASSWD;
cfg.cred_info[0].data = pj_str(cfg_cred_password);
status = pjsua_acc_add(&cfg, PJ_TRUE, &_acc_id);
I noted that we need to use outbound proxy in sip2sip called "proxy.sipthor.net".
but confused how can i used in pjsip code.
please help expert.
If you read the Sip2Sip device configuration page it states that:
" the SIP device must always perform DNS lookups as defined in SIP standard RFC3263 (NAPTR + SRV + A DNS lookups)"
PJSIP supports DNS SRV lookups.
In PJSUA it will only do DNS SRV lookup if you don't provide the port number in the SIP URL.
"sip:xxx#sip2sip.info" will try to do a DNS SRV record lookup first then fail over to DNS A/C name lookup.
and
"sip:xxx#sip2sip.info:5060" will only do DNS A/C name lookup.
What PJSUA will not support automatically is failover support, they say:
"What we've been suggesting is to implement the failover mechanism in the application layer."
If you want a "quick and easy" setup, what you want to do is set the outbound_proxy to "proxy.sipthor.net". e.g.
cfg.outbound_proxy_cnt = 1;
cfg.outbound_proxy[0] = pj_str("sip:proxy.sipthor.net:5060");
If you want a more robust solution, you need to use pjsip's SRV resolution functions to resolve sip2sip.info srv record e.g: "_sip._udp.sip2sip.info" and then set the outbound_proxy records with the result.
The code is a little bit involved.
pjsip_resolver_t* resolver_;
...
status = pjsip_resolver_create( pool, &resolver_ );
...
pjsip_host_info host;
host.flag = PJSIP_TRANSPORT_DATAGRAM; // is using UDP, see pjsip_transport_flags_e
host.type = PJSIP_TRANSPORT_UDP; // if using UDP, see pjsip_transport_type_e
host.addr.host = pj_str("sip2sip.info");
host.addr.port = 5060;
pjsip_resolve(resolver_, pool, &host, token, resolver_cb_func);
...
static void resolver_cb_func( pj_status_t status, void *token, const struct pjsip_server_addresses *addr)
{
...
// use results to fill in the outbound_proxy
}
You could also take it further to support failover, but it looks like sip2sip doesn't have multiple sip servers in there DNS SRV record so it will not be used currently. If they ever add more then it would become more useful.
_sip._udp.sip2sip.info
Server: fritz.box Address: fd00::2665:11ff:fef9:ec51
Non-authoritative answer:
_sip._udp.sip2sip.info SRV service location:
priority = 100
weight = 100
port = 5060
svr hostname = proxy.sipthor.net
sip2sip.info nameserver = ns2.dns-hosting.info
sip2sip.info nameserver = ns1.dns-hosting.info
sip2sip.info nameserver = ns7.dns-hosting.info
Sip2Sip also support STUN setup, so I would also setup the STUN settings on the account as well:
cfg.stun_srv_cnt = 1;
cfg.stun_srv[0] = pj_str("sip2sip.info");
Since your example seems to not provide the port information it should work. To diagnose this further would require see the pjsip log output.