Find IPv6 global scope address on Android device

2019-07-14 12:01发布

问题:

I am looking for a way to determine the global IPv6 address on my android device for testing purpose. Now, what I have obtained a list of interfaces from Android API as the following code:

NetworkInterface netInt: NetworkInterface.getNetworkInterfaces()

netInt actually contains a list of all the interfaces on the devices. for example, as you can see from the following list..(just a part of the list)

[dummy0][2][/fe80::d878:d9ff:fe94:eeae%dummy0%2]
[rmnet_data0][7][/fe80::e03:fac6:8232:95d6%rmnet_data0%7]
                [/2607:fb90:543:e9dd:e03:fac6:8232:95d6%7%7]

I use some public IPv6 site and realized that my public IPv6 address is 2607:fb90:543:e9dd:e03:fac6:8232:95d6. I would like to see if there is a reliable way to to extract 2607:fb90:543:e9dd:e03:fac6:8232:95d6%7%7 from the array since the index of the array changing all the time and from devices to devices.

I appreciate your help.

回答1:

What you need to check for is that the first address word is 4 characters and the first character is a 2 or a 3. If the array element meets those two conditions, it is a global address. You can then clean up the surrounding garbage to get a valid address.

Note: IPv6 allows multiple global addresses to be assigned to an interface. In fact, some OSes do that on purpose for security.

Edit: The definition for an IPv6 Global Unicast address is 2000::/3. This yields 2000:: to 3ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff. See: IPv6 Global Unicast Address Assignments

Note
The assignable Global Unicast Address space is defined in [RFC4291] as
being the address block defined by the prefix 2000::/3. All address
space in this block not listed in the table below is reserved by IANA
for future allocation.

IPv6 allows any number of IPv6 addresses on an interface, and you can use them as either incoming or outgoing.



标签: android ipv6