tags:

views:

144

answers:

3

I have the following code:

import java.net.InetAddress;

public class lookup {

  public static void main(String[] args) throws Exception {
    for(String host : args){
      for(InetAddress addr : InetAddress.getAllByName(host)){
      System.out.println(addr.getHostAddress());
    }
  }
}
}

We recently changed the CNAME for a host we'll call foo.example.com from pointing at bar.example.com to point at baz.example.com. If I run:

java -Djava.net.preferIPv4Stack=true lookup foo.example.com

I get baz.example.com's ip address, as expected. However if I run:

java lookup foo.example.com

I still get bar.example.com's ip address.

I've confirmed that neither bar.example.com nor baz.example.com have AAAA records. dig and ping both resolve baz.example.com as expected. How do I get java's ipv6 stack to properly resolve this?

A: 

Hmm, there's something a bit odd going on here. I suspect it might have something to do with DNS caching the domain name lookups performed. Modify the following properties in:

java.home/lib/security/java.security

and see whether this resolves the issue:

networkaddress.cache.ttl: 10
networkaddress.cache.negative.ttl: 10

A value of -1 (the default for networkaddress.cache) indicates "cache forever" which might be causing what you see above.

Jon
I've set both those values to 10 and the problem still occurs. I would think the cache would be cleared when I start a new jvm anyway.
Stuart
A: 

Could it be that your operating system is aware of two different DNS servers, one reachable by IPv4, the other reachable by and preferred for IPv6 and that the IPv6 preferred DNS server is still caching the old configuration?

jarnbjo
I'm not sure how to test that. `dig -6 foo.example.com` says `dig: add_nameserver failed`
Stuart
+1  A: 

The problem was nscd was running and had cached the records. Why it didn't respect the TTLs and why ping doesn't use the cache are still a mystery.

Stuart