Caching behaviour of InetAddress

Andreas Plesner Jacobsen apj.lists at code3.dk
Mon Feb 18 01:28:50 PST 2008


I've recently been introduced to the caching behaviour of InetAddress, 
and I think it may be improved.

The javadoc reads:
The InetAddress class has a cache to store successful as well as 
unsuccessful host name resolutions. The positive caching is there to 
guard against DNS spoofing attacks; while the negative caching is used 
to improve performance.

And that is all fine and well, but for multihomed hosts, I believe the 
current behaviour is

1) Not documented properly
2) Not correct

Coming from a unix-world, I'm used to the resolver handing out 
RR-replies in random order, and thus I would expect InetAddress to do 
the same, but a short test (courtesy of Arne Vajhøj) shows that 
InetAddress.getByName() will return the same IP for a lookup when the 
lookup is performed within the 10-second range of the cache:

public class DNSLookup { 
 

      private static final String MS = "www.microsoft.com"; 
 

      public static void main(String[] args) throws Exception { 
 

          Map<String, Integer> m = new HashMap<String, Integer>(); 
 

          InetAddress[] all = InetAddress.getAllByName(MS); 
 

          for(InetAddress one : all) { 
 

              m.put(one.getHostAddress(), 0); 
 

          } 
 

          for(int i = 0; i < 10000; i++) { 
 

              String ms = InetAddress.getByName(MS).getHostAddress(); 
 

              m.put(ms, m.get(ms) + 1); 
 

          } 
 

          for(Entry<String, Integer> e : m.entrySet()) { 
 

              System.out.println(e.getKey() + ": " + e.getValue()); 
 

          } 
 

      } 
 

} 
 


Outputs:
207.46.19.190: 0
207.46.193.254: 10000
207.46.192.254: 0
207.46.19.254: 0

While I would expect the replies to distribute to about 2500 on each 
(which it will if the cache isn't used).

-- 
Andreas



More information about the net-dev mailing list