Duplicate :
|
|
Duplicate :
|
|
Relates :
|
Synopsis : Hostname returned by getHostName() appears not to be cached Problem Description : ====================== We had a recent customer case saying that their product was causing excessive DNS lookups. The JRE is 1.5 update 10 on Solaris 8 and Red Hat Enterprise Linux 4 update 5. They have tested on later JREs and see similar behavior. After investigation, they found that InetAddress.getHostName() appears to not cache the name when the InetAddress instance is obtained by calling InetAddress.getByName( "nnn.nnn.nnn.nnn" ) or InetAddress.getByAddress( bytes ). The system call that results in a DNS lookup is always performed if nscd is not running on the host. They had expected that InetAddress.getHostName() would cache the result. Any reason as to Why name is not cached in this case ? Customer re-ran with the same result on Solaris 10 and RHEL 4 with 1.5.0_14 and 1.6.0_04. Customer is not sure, Is this a "design intent" or generally broken and not tested with nscd disabled. Test procedure to reproduce on Solaris 8 or RHEL 4: ---------------------------------------------------- 1. Stop nscd on the test box - as root, run /etc/init.d/nscd stop so that the box itself doesn't cache name lookups. Verify that default JRE security parameters are used for caching i.e. use a newly installed/unmodified JRE to test with. 2. Make sure that the address strings specified in the test with aren't in /etc/hosts and that /etc/nsswitch.conf is configured as "files dns" for hosts. 3. Run tcpdump or some other sniffer program to look for DNS packets to and from this host e.g. as root, run tcpdump -p -n -s 200 port domain. 4. Run the following test program. Verify in tcpdump output that when you enter the same IP address multiple times for which a name can be resolved, that a DNS lookup request is issued each time. No caching occurs. So customer question is: why isn't the hostname returned by getHostName() cached in this case as described under "InetAddress Caching" in the InetAddress javadoc, where it says "The InetAddress class has a cache to store successful as well as unsuccessful host name resolutions" ? Test Code : ------------ import java.io.*; import java.net.*; public class namelookup { static public void main( String[] args ) { BufferedReader in = new BufferedReader( new InputStreamReader( System.in ) ); while( true ) { try { System.out.print( "Enter address: " ); System.out.flush(); String addrStr = in.readLine(); InetAddress addr = InetAddress.getByName( addrStr ); String addrName = addr.getHostName(); System.out.println( "name: " + addrName ); } catch( Exception ex ) { System.out.println( "error: " + ex ); } } } }
|