JDK-6656822 : InetAddress does not cache reverse DNS lookups
  • Type: Enhancement
  • Component: core-libs
  • Sub-Component: java.net
  • Affected Version: 5.0u14
  • Priority: P3
  • Status: Open
  • Resolution: Unresolved
  • OS: solaris_10
  • CPU: sparc
  • Submitted: 2008-01-30
  • Updated: 2022-12-07
Related Reports
Duplicate :  
Duplicate :  
Relates :  
Description
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 );
            }
        }
    }
}

Comments
The net-properties.html needs to be corrected to state that only results of forward lookups are cached: JDK-8293656
07-12-2022

Implementing a reverse lookup cache on InetAddress level looks like a complicated task. Apps running on Java 18+ that need to have a reverse lookup caching, or/and configure custom lookup timeouts can provide their own implementation of InetAddressResolverProvider (see JEP-418 - JDK-8263693) which will wrap the built-in resolver with additional functionality dictated by an application logic added. I believe this issue can be closed as "Won't Fix".
07-12-2022

EVALUATION The InetAddress instance described in the description section is created either from an IP literal or from a byte array containing the IP address in bytes. Clearly this InetAddress instance will contian the actual IP address but not the hostname. When getHostName is invoked on this instance it requires a reverse DNS lookup to retrieve the hostname. Currently, and in previous releases, reverse DNS lookups are not cached. Only forward lookups are cached. This bug is complaining about the fact that reverse lookups are not cached. I will update the synopsis field to clearly reflect this and keep the bug open for now to evaluation the possibility of InetAddress caching reverse lookups.
06-02-2008