Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-5087907

InetAddress.getAllByName does not obey setting of java.net.preferIPv6Addresses

    XMLWordPrintable

Details

    • Bug
    • Resolution: Fixed
    • P3
    • 5.0u1
    • 5.0, 5.0u1
    • core-libs
    • 01
    • generic, x86, sparc
    • generic, linux, solaris_2.6, solaris_9, windows_xp

    Backports

      Description

        InetAddress.getAllByName is supposed to examine the java.net.preferIPv6Addresses system property and sort the results according. The default value for the property is false so on a dual stack system this should mean that the results are ordered so that the IPv4 addresses are first in the list.

        The interpretation of this property seems to be broken. Consider the following test case on SuSE 9.1 or Mandrake 10 systems - both distribution ships with IPv6 enabled and /etc/hosts configured as follows :-

        127.0.0.1 localhost
         
        # special IPv6 addresses
        ::1 localhost ipv6-localhost ipv6-loopback


        In these environments InetAddress.getAllByName("localhost") correctly resolves to ::1 and 127.0.0.1 but the addresses are returned in the wrong order. The implication is that InetAddress.getByName("localhost") resolves to ::1 even when the preferIPv6Addresses property is explicitly set to false.

        The bug appears to be in the sorting implementation - if this test is run with preferIPv6Addresses set to true then the IPv4 address is returned first.

        Note that the bug breaks Oracle tools on SuSE 9.1 and is likely to break other applications that attempt to connect to "localhost". The bug is not a regression in 1.5.0 and also duplicates with 1.4.2.


        import java.net.*;
         
        public class Test {
         
            public static void main(String args[]) throws UnknownHostException {
                InetAddress lh = InetAddress.getByName("localhost");
                InetAddress addrs[] = InetAddress.getAllByName("localhost");
         
                boolean hasIPv4Address = false;
                boolean hasIPv6Address = false;
                for (InetAddress addr: addrs) {
                    if (addr instanceof Inet4Address) {
                        hasIPv4Address = true;
                    }
                    if (addr instanceof Inet6Address) {
                        hasIPv6Address = true;
                    }
                    if (hasIPv4Address && hasIPv6Address) {
                        break;
                    }
                }
         
                String prop = System.getProperty("java.net.preferIPv6Addresses");
                boolean preferIPv6Addresses = (prop == null) ? false : prop.equals("true");
         
                System.out.println("java.net.preferIPv6Addresses: " + preferIPv6Addresses);
                System.out.println("localhost resolves to:");
                for (InetAddress addr: addrs) {
                    System.out.println(" " + addr);
                }
                System.out.println("InetAddres.getByName returned: " + lh);
         
                boolean failed = false;
                if (preferIPv6Addresses && hasIPv6Address) {
                    if (!(lh instanceof Inet6Address)) failed = true;
                }
                if (!preferIPv6Addresses && hasIPv4Address) {
                    if (!(lh instanceof Inet4Address)) failed = true;
                }
                if (failed) {
                    throw new RuntimeException("Test failed!");
                }
            }
         
        }
        ###@###.### 10/12/04 20:24 GMT

        Attachments

          Issue Links

            Activity

              People

                michaelm Michael McMahon
                alanb Alan Bateman
                Votes:
                0 Vote for this issue
                Watchers:
                2 Start watching this issue

                Dates

                  Created:
                  Updated:
                  Resolved:
                  Imported:
                  Indexed: