This is observed in Java 5 and Java 6.
When called with -Djava.net.preferIPv6Addresses=true the call to DatagramSocket getLocalSocketAddress returns an all-zero address, instead of ::1.
InetAddress iNet = InetAddress.getByName("localhost");
DatagramSocket soc = new DatagramSocket( 12345, iNet);
SocketAddress sa = soc. getLocalSocketAddress();
System.out.println("iNet: " + iNet);
System.out.println("Expected: " + iNet + ":12345");
System.out.println("Returned: " + ((InetSocketAddress)sa).getAddress());
workaround: Here's a fix:
In net_util_md.h, the macro IN6_IS_ADDR_ANY is defined as:
#define IN6_IS_ADDR_ANY(a) \
(((a)->s6_words[0] == 0) && ((a)->s6_words[1] == 0) && \
((a)->s6_words[2] == 0) && ((a)->s6_words[3] == 0) && \
((a)->s6_words[4] == 0) && ((a)->s6_words[5] == 0))
It should be:
#define IN6_IS_ADDR_ANY(a) \
(((a)->s6_words[0] == 0) && ((a)->s6_words[1] == 0) && \
((a)->s6_words[2] == 0) && ((a)->s6_words[3] == 0) && \
((a)->s6_words[4] == 0) && ((a)->s6_words[5] == 0) && \
((a)->s6_words[6] == 0) && ((a)->s6_words[7] == 0))
IPV6 addresses are 16 bytes long, the macros only tests 12 bytes.