How to get correct IPv6 Ip address? - android

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.

Related

Trying to get network device names with reverse dns in Android

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.

How to get another location IP Address in Android?

City1 : PG-W1 (IP Pool 1.1.0.0/16)
City2 : PG-W2 (IP Pool 2.2.0.0/16)
If I use Android(PDN Address : 1.1.1.1) in City1,
How do I get the IP(2.2.2.2) of the City2? (Without moving)
I tried to find the associated document but failed.. :_(

get IP address but in xxx.xxx.xxx.xxx format in Android

Hi guys I've gone through a lot of coding on line to get my android mobiles IP address
Most of them are ending with
if (!inetAddress.isLoopbackAddress())
{ return inetAddress.getHostAddress().toString(); }
How ever i get something that looks like this:- "fe80::a00:27ff:fe37:28b5%eth1"
Weird cause I was expecting something like xxx.xxx.xxx.xxx
Can some one help me understand whats this?
That is an IPv6 address. Additionally, since it starts with fe80:: you know it's also a link-local IPv6 address, so cannot be used for communication beyond the local network. (in this case, eth1, since that is the scope specified at the end after the % - but note that using a % to identify the scope isn't always valid when using an IPv6 address.)
Try java.net.Inet4Address instead.
Its returning IPV6 address.
Check if its IPV4 address before returning result.
if (!inetAddress.isLoopbackAddress() && inetAddress instanceof Inet4Address) {
return inetAddress.getHostAddress().toString();
}
Hope this helps.

Can't do reverse DNS lookup on Android

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)

Get my phone's Internal ip

My phone has an IP.
I would like to know how could I retrieve it?
After searching in the web, I found that i can get only the current outside ip.(I want the local-perm ip of my phone)
Thanks,
ray.
A quick search on google sent me here:
http://www.droidnova.com/get-the-ip-address-of-your-device,304.html
Read the comments about how to use the first block of code to get the wifi ip address (on local network, not public ip)
WifiManager wifiManager = (WifiManager) getSystemService(WIFI_SERVICE);
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
int ipAddress = wifiInfo.getIpAddress();
Edit:
The emulator seems to return 0 on wifiInfo.getIpAddress(), but it works fine on a phone.
The following code converts the integer to an ip address:
String ipBinary = Integer.toBinaryString(ipAddress);
//Leading zeroes are removed by toBinaryString, this will add them back.
while(ipBinary.length() < 32) {
ipBinary = "0" + ipBinary;
}
//get the four different parts
String a=ipBinary.substring(0,8);
String b=ipBinary.substring(8,16);
String c=ipBinary.substring(16,24);
String d=ipBinary.substring(24,32);
//Convert to numbers
String actualIpAddress =Integer.parseInt(d,2)+"."+Integer.parseInt(c,2)+"."+Integer.parseInt(b,2)+"."+Integer.parseInt(a,2);
your ip will change with every networkthat you connect to - your phone has a mac address - is that what you are looking to find?
The IP which I may would call "local-perm" is the localhost IP which is always 127.0.0.1
The IP of any other network adapter (Wifi in this case) changes depending on the network

Categories

Resources