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.

Koekiebox
  • 5,793
  • 14
  • 53
  • 88
TiGz
  • 894
  • 2
  • 13
  • 21
  • For clarity, I didn't mean that the method was deprecated... just the notion of "site local" address in IPv6 as per http://www.ietf.org/rfc/rfc3879.txt – TiGz Apr 11 '11 at 10:23

5 Answers5

28

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).

Joachim Sauer
  • 302,674
  • 57
  • 556
  • 614
  • 2
    see [here](http://books.google.co.in/books?id=NyxObrhTv5oC&lpg=PT187&dq=InetAddress.isSiteLocalAddress()&pg=PT186#v=onepage&q&f=false). it returns true if the address is an IPv6 site-local address. – Prince John Wesley Apr 11 '11 at 09:43
  • @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). – Joachim Sauer Apr 11 '11 at 09:46
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.

dty
  • 18,795
  • 6
  • 56
  • 82
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

mateuscb
  • 10,150
  • 3
  • 52
  • 76
0

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

jmac
  • 1
-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?

Joachim Sauer
  • 302,674
  • 57
  • 556
  • 614
Koekiebox
  • 5,793
  • 14
  • 53
  • 88