What does InetAddress.isSiteLocalAddress() actually mean?
Asked Answered
T

5

16

Here is some code to determine the local host name that is supposed to work on a multi-homed box:

 /**
 * Work out the first local host name by iterating the network interfaces
 * 
 * @return
 * @throws SocketException
 */
private String findFirstLocalHostName() throws SocketException {

    Enumeration<NetworkInterface> ifaces = NetworkInterface.getNetworkInterfaces();
    while (ifaces.hasMoreElements()) {
        NetworkInterface iface = ifaces.nextElement();
        Enumeration<InetAddress> addresses = iface.getInetAddresses();
        while (addresses.hasMoreElements()) {
            InetAddress add = addresses.nextElement();
            if (!add.isLoopbackAddress() && add.isSiteLocalAddress()) {
                return add.getHostName();
            }
        }
    }
    throw new RuntimeException("Failed to determine local hostname");
}

Does the call to isSiteLocalAddress introduce a bug? I can't find any useful information about this method, but I have a feeling that it relates to IP v 6 only and is deprecated.

Trophoblast answered 11/4, 2011 at 9:32 Comment(1)
For clarity, I didn't mean that the method was deprecated... just the notion of "site local" address in IPv6 as per ietf.org/rfc/rfc3879.txtTrophoblast
T
29

The method is definitely not deprecated and it's definitely not just used in IPv6.

In IPv4 there are 3 network address ranges that are defined for site-local addresses: 10/8, 172.16/12 and 192.168/16.

Reading Inet4Address.isSiteLocalAddress() shows that addresses from exactly those 3 networks will return true on those methods.

IPv6 has a similar concept, here these addresses are called unique local addresses.

Effectively this tells you if the address you have is definitely not a public one (note that even if this method returns false, the address might still not be public).

Thibault answered 11/4, 2011 at 9:38 Comment(2)
see here. it returns true if the address is an IPv6 site-local address.Rescue
@John: yes, I know that there is a similar meaning in IPv6 as well, but I don't know the specifics of it. But since the question implied that it's IPv6-only, I wanted to clarify that aspect. (By the way, I can't read the page you linked to).Thibault
R
1

Looking at the implementation...

For an Inet4Address, it checks to see if it's one of the RFC1918 "unrouteable" addresses: 10.0.0.0/8, 172.16.0.0/12, 192.168.0.0/16.

For an Inet6Address, it checks the first two octets to see if it's a real "site local" address.

Rooster answered 11/4, 2011 at 9:39 Comment(0)
B
0

'Site local' is a deprecated name for private IP space. (Some nuances, but basically right.) See RFC 1918.

Bustamante answered 15/4, 2011 at 16:13 Comment(0)
M
0

I just came across what I believe is a similar problem: trying to determine what IPv6 I should use for LAN comuncation:

  • IMHO, Inet6Address.isSiteLocalAddress() is useless. Given that the 0xFEC0 prefix has been depricated by RFC 3879 as @tigz mentioned. I have yet to see any device (android, win, osx) actually have a 0xFEC0 (with limited testing)

    //from java.net.Inet6Address (1.8.0_45) boolean isSiteLocalAddress() { return ((ipaddress[0] & 0xff) == 0xfe && (ipaddress[1] & 0xc0) == 0xc0); }

  • 0xFE80 address although not supposed be used for traffic (from my understanding and reading (www.cisco.com)) did work for LAN communication with my single router (ping6, curl, http).

  • My Global Unicast (which is just another name for public IP) 2601::/20 from Comcast worked for my LAN communication. So I would say that this is the correct address to use.

Prefix table: www.iana.org

Mount answered 24/6, 2015 at 20:32 Comment(0)
W
-1

As far as I know the isSiteLocalAddress method is not deprecated.

isSiteLocalAddress - Explanation

indicating if the InetAddress is a site local address; or false if address is not a site local unicast address.

The InetAddress even have two direct subclasses;

Inet4Address and Inet6Address

The best bet is to read the JavaDocs.

Which version of the JDK are you using?

Winterwinterbottom answered 11/4, 2011 at 9:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.