When I call method NetworkInterface.getInetAddresses()
, i get the following string:
"fe80:0:0:0:f06c:31b8:cd17:5a44%5"
I wonder what %5
means here? Is it possible to het IPv6 address without this %5
?
When I call method NetworkInterface.getInetAddresses()
, i get the following string:
"fe80:0:0:0:f06c:31b8:cd17:5a44%5"
I wonder what %5
means here? Is it possible to het IPv6 address without this %5
?
It's explained here:
The general format for specifying the scope_id is the following:
*IPv6-address%scope_id*
The IPv6-address is a literal IPv6 address as described above. The scope_id refers to an interface on the local system, and it can be specified in two ways.
As a numeric identifier. This must be a positive integer that identifies the particular interface and scope as understood by the system. Usually, the numeric values can be determined through administration tools on the system. Each interface may have multiple values, one for each scope. If the scope is unspecified, then the default value used is zero.
As a string. This must be the exact string that is returned by NetworkInterface.getName() for the particular interface in question. When an Inet6Address is created in this way, the numeric scope-id is determined at the time the object is created by querying the relevant NetworkInterface.
Note also, that the numeric scope_id can be retrieved from Inet6Address instances returned from the NetworkInterface class. This can be used to find out the current scope ids configured on the system.
The 5 is the zone index. The % is used to separate it.
A Windows machine would have one such as %1.
A Unix machine would have one such as %eth0. Using the interface name as the zone index.
The % sign denotes the Ipv6 zone or scope id.
The open-source IPAddress Java library will validate all standard representations of IPv6. You can extract the zone as shown. Disclaimer: I am the project manager of that library.
Code example:
try {
IPAddressString str = new IPAddressString("fe80:0:0:0:f06c:31b8:cd17:5a44%5");
IPAddress addr = str.toAddress();
if(addr.isIPv6() || addr.isIPv6Convertible()) {
IPv6Address ipv6Addr = addr.toIPv6();
String zone = ipv6Addr.getZone();
IPv6Address noZoneAddr = new IPv6Address(ipv6Addr.getSection());
System.out.println("zone is " + zone + " for address " + noZoneAddr);
}
//use address
} catch(AddressStringException e) {
//e.getMessage has validation error
}
Output:
zone is 5 for address fe80:0:0:0:f06c:31b8:cd17:5a44