Java Solaris Communities Sun Store Join SDN My Profile Why Join?
 
Bug Database
Bug Detail
Quick Lists
Top 25 Bugs
Top 25 RFE's
Recently Closed Bugs
Printable Page Printable Page


Bug Database
Bug ID: 4211819
Votes 10
Synopsis InetAddress.getLocalHost() returns incorrect IP address from dial-up
Category java:classes_net
Reported Against 1.2
Release Fixed 1.4.1(hopper)
State 10-Fix Delivered, bug
Priority: 4-Low
Related Bugs
Submit Date 16-FEB-1999
Description




I am writing a java app that communicates with other apps on other computers via sockets.  These apps are often run on PCs that are connected to the internet via a dial-up connection.  Disconnections can be expected and a giving computer will often reconnect with a new IP address.  I would like my app to handle these problems gracefully.

The problem is that I use InetAddress.getLocalHost() to determine the IP address of the local machine and this appears to be cached for the entire session of the app.  i.e. when I run my app and read from InetAddress.getLocalHost() I always get the same value even if the user is diconnecting and reconnecting their dial-up connection and thus changing their local host address.

I've found several bugs which seem to deal with some aspects of this issue (4173986, 406228, 4026938) and I've gathered that I can disable InetAddress cacheing by changing a system property as follows:
System.setProperty("sun.net.inetaddr.ttl", "0");
Unfortunately this does not appear to fix the problem.

What am I doing wrong?  Does the property fix not work on Win32?

Here is a simple program that displays the output from InetAddress.getLocalHost()  Run the program from a machine that has a dial-up connection, check the ip value, then disconnect, reconnect, refresh and check the value.  It should be incorrectly set at the same value it was at originally.  You can run winipcfg to verify that the ip address has changed.


import javax.swing.*;
import javax.swing.text.*;
import java.awt.*;
import java.awt.event.*;
import java.net.*;

class test1 {
	
	JLabel ip;
	
	private test1() {
		
		// this is supposed to disable InetAddress ip caching
		System.setProperty("sun.net.inetaddr.ttl", "0");
		
		JFrame frame = new JFrame();
		frame.setSize(200, 200);
		Container contentPane = frame.getContentPane();
		
		ip = new JLabel();
		contentPane.add(ip, BorderLayout.CENTER);
		updateIP();
		
		JButton refresh = new JButton("Refresh");
		refresh.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				updateIP(); }
		});
		contentPane.add(refresh, BorderLayout.SOUTH);
		
		// allow the user to close the window
		frame.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				System.exit(0); }
		});
		
		frame.setVisible(true);
	}
	
	private void updateIP() {
		String localHost;
		try {
			localHost = InetAddress.getLocalHost().toString();
		} catch (UnknownHostException e) {
			localHost = "unknown host";
		}
		ip.setText("IP: "+localHost);
	}
	
	public static void main(String[] args) {
		new test1();
	}
}
(Review ID: 54214)
======================================================================
Work Around




The user of the app must exit and restart the application whenever they reconnect their dial-up connection.
======================================================================
Evaluation
InetAddress.getLocalHost() doesn't obey the networkaddress.cache.ttl
property so it will continue to return the same adddress even through we
have re-dialed and obtained a new IP address. As a workaround there is
a new class java.net.NetworkInterface in 1.4 to enumerate network
interfaces and the IP addresses bound to each interface. This class
works with dial-up connections.

  xxxxx@xxxxx   2001-12-03


In hopper (1.4.1) InetAddress.getLocalHost() has been corrected so
that doesn't caching the host address. This means that getLocalHost
will return the current address of the host. However we are dependent
on the name service returning the right address and in some
environments this may not be configured correctly (for Linux 
issues - see 4665037).
  xxxxx@xxxxx   2002-05-24
Comments
  
  Include a link with my name & email   

Submitted On 17-SEP-1999
wlwinhh
Have you tried to set the property on the commandline
by calling java with -Dsun.net.inetaddr.ttl=0 ?
This works with Apache & JServ.


Submitted On 30-SEP-1999
KleemannR
The bug still reproduces even with "-Dsun.net.inetaddr.ttl=0" from
the command line.  I'm running Win95.


Submitted On 27-OCT-1999
ggyepesi
If you reload InetAddress with a private classloader then it will tell the
right IP address.


Submitted On 13-NOV-2000
KleemannR
FYI. I'm now contacting a remote host in order to determine
my local ip address.  This gets around other problems such
as NAT.  I'm no longer blocking on this bug.


Submitted On 09-MAY-2001
cypherpunk9
ignore that...i think it was a different cause.


Submitted On 09-MAY-2001
cypherpunk9
im getting crashes with the same thing on linux.
#
# HotSpot Virtual Machine Error, Internal Error
<SNIP>
#
# Error ID: 53484152454432554E54494D450E4350500183
#
# Problematic Thread: prio=5 tid=0x804df08 nid=0x2a6e
runnable 
#
./c: line 4: 10862 Aborted                 ~/java/bin/java
-server -Xms64M -Xmx128M testinetaddr


Submitted On 13-NOV-2001
mthornton
In JDK 1.4 a new security property has been added 
(networkaddress.cache.ttl) which is set in the 
java.security file in the JRE/lib/security directory. This 
controls the caching policy. With this set to a positive 
value, the result of InetAddress.getAllByName will track 
changes. However, InetAddress.getLocalHost is still cached 
indefinitely (i.e. never changes). So by finding our local 
host name we can then use getAllByName to track changes in 
our own IP address(es).


Submitted On 13-NOV-2001
mthornton
Another approach with 1.4, is to use the 
java.net.NetworkInterface class. The list of network 
interfaces (and their associated addresses) changes 
dynamically even when the cache property is set to cache 
indefinitely.
However, the cacheing on getLocalHost should still be 
stopped --- I can't see any security issue in being allowed 
to track changes in your own address (especially as there 
are ways to bypass the cache).


Submitted On 02-SEP-2002
schlm3
The latest change in hopper (1.4.1) introduced a huge 
overhead in getting the localHostAdress. My Project used this 
function to produce reasonably unique ID's for Objects in a 
Network environment, thus creation 400 Objects (including 
parsing of XML-file and others) took about 1.5 seconds in 
1.4.0_01 . Running the same code in 1.4.1-rc took about 9.5 
seconds ! After looking about the bug with OptimizeIt, I found 
the InetAdress.getLocalHost() to be the reason for this.
While this is not a problem, because I can cache the value by 
myself, it should be considered in the releasenotes. 


Submitted On 03-SEP-2002
BatemanA
The change is documented in the release notes - see :-
http://java.sun.com/j2se/1.4.1/networking-relnotes.html


Submitted On 24-SEP-2002
ferval
I have the same problem, i tested with 1.1.8, 1.2.2, 1.3.0
all have the problem, wouldn't be better to fix this issue?.
I don't see the need to cache this information since there
are arp cache, dns cache server, etc. and probably if i'm
calling this method several times is because i'm looking for
changes in the ip address (ppp connections, dhcp address
changes).



PLEASE NOTE: JDK6 is formerly known as Project Mustang