[kaffe] IP address autodetection

Timothy Stack stack at cs.utah.edu
Mon Feb 24 10:52:01 PST 2003


> On Mon, Feb 24, 2003 at 09:56:42AM -0700, Timothy Stack wrote:
> > > On Sun, Feb 23, 2003 at 02:46:19PM -0700, Timothy Stack wrote:
> > > > On Sunday, February 23, 2003, at 11:48  AM, Matthew Toseland wrote:
> > > > >While trying to get freenet working on Kaffe, I discovered a side
> > > > >issue... we use the attached code to detect the local internet IP
> > > > >address. Basically, we open a datagram socket to the A-root nameserv=
> er
> > > > >and call getLocalAddress().
> > > > why not just use InetAddress.getLocalHost()?  Or,=3D20
> > > Because it doesn't do what we want, at least in the Sun JVM?
> >=20
> > Care to elaborate?
> 
> Hmmm, it returned 0.0.0.0

Kaffe?  Or some broken JVM?

> or the ethernet address.

Are you sure it wasn't an IPv6 address enclosed in an Inet6Address?  The
spec isn't clear on which address (ipv4/ipv6 and/or which NIC) the VM is
supposed to return, so its a hard interface to use consistently.  annoying 
isn't it ;)

> > > > useNetworkInterface.getNetworkInterfaces() and handle the=3D20
> > > > NoClassDefFoundError with this method.
> 
> Does that mean I'd have to dynamically load the class? Or can I just
> import it and enclose the section where I actually use it in a
> try/catch?

yes, something like this should even work on kaffe:

InetAddress getLocalIPv4FromNetworkInterface()
	throws SocketException,
	       NoClassDefFoundError
{
	Enumeration enum;
	
	enum = NetworkInterface.getNetworkInterfaces();
	while( enum.hasMoreElements() )
	{
		NetworkInterface ni;
		Enumeration addrs;

		ni = (NetworkInterface)enum.nextElement();
		addrs = ni.getInetAddresses();
		while( addrs.hasMoreElements() )
		{
			InetAddress ia = (InetAddress)addrs.nextElement();
			byte bits[];

			bits = ia.getAddress();
			if( bits.length == 4 && !(bits[0] == 0x7F)
			    /* or whatever test u need */ )
			{
				return ia;
			}
		}
	}
	throw new SocketException("No valid InetAddresses found");
}

public InetAddress getLocalIPv4()
{
	InetAddress retval;

	try
	{
		retval = this.getLocalIPv4FromNetworkInterface();
	}
	catch(NoClassDefFoundError e)
	{
		retval = this.getLocalIPv4FromAltMethod();
	}
	catch(SocketException e)
	{
		retval = this.getLocalIPv4FromAltMethod();
	}
	return retval;
}

You'll also need to require developers to have a JVM/jar with
NetworkInterface/etc... or provide stub versions to compile against.  I do
the latter with the Java NodeOS:

	http://www.cs.utah.edu/flux/janos/jnodeos.html

Notice that we keep all references to NetworkInterface in its own separate 
method.  This works around a bug in kaffe's lazy class loading.  
Basically, kaffe throws the NoClassDefFoundError at the method boundary 
instead of the actual access point in the method.

Also, the current implementation of NetworkInterface in kaffe does the
lookup once, at initialization, and always returns the same result.  I can
fix this if you need to be able to detect changes in the configuration
over time (this was on the to-do list anyways).

> Matthew Toseland

tim stack




More information about the kaffe mailing list