I'm looking for a java api that will correctly identify if a given ip address is private or local. This code seems to work for most ipv4/ipv6 addresses:
boolean isLocalIp = InetAddress.getByName(ipAddr).isSiteLocalAddress() ||
InetAddress.getByName(ipAddr).isLinkLocalAddress() ||
InetAddress.getByName(ipAddr).isLoopbackAddress() ;
Particularly, it identifies "fec0::" as a local/private type address but it does not identify, for example, "fc00::" or "fd00::" as local/private type addresses.
Looking at this wikipedia link https://en.wikipedia.org/wiki/Private_network#IPv6:
The address block fc00::/7 is reserved by IANA for Unique Local Addresses (ULA).[2] They are unicast addresses, but contain a 40-bit random number in the routing prefix to prevent collisions when two private networks are interconnected. Despite being inherently local in usage, the IPv6 address scope of unique local addresses is global.
The first block defined is fd00::/8, designed for /48 routing blocks, in which users can create multiple subnets, as needed.
Can anyone help with explaining if there is some false assumption here (e.g. that "fc00::" or "fd00::" should be identified as a local/private ipv6 address) or if there is some other java api that will correctly identify all local/private ip addresses?
Thanks for any feedback/answers!
Can anyone help with explaining if there is some false assumption here (e.g. that "fc00::" or "fd00::" should be identified as a local/private ipv6 address)
Addresses in those blocks are not meant to be routed outside a single administrative domain, but the term "site-local address" means something much more specific. That now-deprecated term refers specifically to the block fec0::/10.
Addresses in the block fc00::/7 are "unique local addresses" that are not guaranteed to be globally unique, but they are not link-local because they are permitted to be routed between different broadcast domains.
or if there is some other java api that will correctly identify all local/private ip addresses?
I looked briefly at InetAddress.isAnyLocalAddress()
, but its docs do not seem to match what you're trying to do. It doesn't look like the standard library has such a method available.