[kaffe] IP address autodetection

Timothy Stack stack at cs.utah.edu
Mon Feb 24 08:55:01 PST 2003

> 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 nameserver
> > >and call getLocalAddress().
> > why not just use InetAddress.getLocalHost()?  Or,=20
> Because it doesn't do what we want, at least in the Sun JVM?

Care to elaborate?

> > useNetworkInterface.getNetworkInterfaces() and handle the=20
> > NoClassDefFoundError with this method.
> Hmmmmm... this is supported by Kaffe? We'd have to have a fallback for
> pre-1.4 JVMs, yeah...

I think its in CVS now.

> > > This returns on Kaffe CVS;
> > Its probably wrong, but its a very gray area and making dependencies on=
> > it doesn't seem like a good idea.  The javadoc for getLocalAddress()=20
> > says it returns "the local address to which the socket is bound, or an=20
> > InetAddress representing any local address."  Since the attached=20
> > example didn't do a bind its technically correct to return the any=20
> > address (  Also, connecting a datagram socket discriminates on=
> > the remote address, not the network interface.  So, again, its very=20
> Sorry, I don't understand "connecting... interface".

Suppose the local host is multihomed and it connects a datagram socket to
another host. (Host A: the remote host)
		     |  |
		  eth0  eth1 (Host B: the local host w/2 ethernet adapters)

Now, when host B connect()'s a datagram socket to host A, some[1]
implementations will trigger a side effect that sets the socket's local IP
address to that of the outgoing NIC.  Basically, its a hacky way of
querying the routing table.

[1] Read Stevens' Unix Network Programming Vol. 1 Second Edition, section
8.14.  Here's an excerpt from page 233:

	Unfortunately, this technique does not work on all
	implementations, notably SVR4-derived kernels.  For example, this
	does not work on HP-UX, Solaris 2.5, and UnixWare, but it works on
	AIX, Digital Unix, Linux, and Solaris 2.6.

> > >Matthew Toseland

tim stack

More information about the kaffe mailing list