Re: [kaffe] IP address autodetection
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. Hm... 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 0.0.0.0 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 (0.0.0.0). 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. 231.42.21.152 (Host A: the remote host) | /---+\ |Internet| \--+--+--/ | | eth0 eth1 (Host B: the local host w/2 ethernet adapters) 34.21.12.2 34.21.22.3 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 ___ kaffe mailing list [EMAIL PROTECTED] http://kaffe.org/cgi-bin/mailman/listinfo/kaffe
Re: [kaffe] IP address autodetection
--- Timothy Stack [EMAIL PROTECTED] 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 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? I think they do that because Sun's JDK has a bug and caches InetAddresses forever, leading to problems when the IP address of the localhost changes (expired DHCP lease ?). Did I guess right? useNetworkInterface.getNetworkInterfaces() and handle the=20 NoClassDefFoundError with this method. Hm... this is supported by Kaffe? We'd have to have a fallback for pre-1.4 JVMs, yeah... I think its in CVS now. Yes. Merged in from JanosVM 1.0. cheers, dalibor topic __ Do you Yahoo!? Yahoo! Tax Center - forms, calculators, tips, more http://taxes.yahoo.com/ ___ kaffe mailing list [EMAIL PROTECTED] http://kaffe.org/cgi-bin/mailman/listinfo/kaffe
Re: [kaffe] IP address autodetection
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 ___ kaffe mailing list [EMAIL PROTECTED] http://kaffe.org/cgi-bin/mailman/listinfo/kaffe
Re: [kaffe] IP address autodetection
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, useNetworkInterface.getNetworkInterfaces() and handle the NoClassDefFoundError with this method. This returns 0.0.0.0 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() says it returns the local address to which the socket is bound, or an InetAddress representing any local address. Since the attached example didn't do a bind its technically correct to return the any address (0.0.0.0). Also, connecting a datagram socket discriminates on the remote address, not the network interface. So, again, its very dodgy behavior to be relying on. -- Matthew Toseland tim ___ kaffe mailing list [EMAIL PROTECTED] http://kaffe.org/cgi-bin/mailman/listinfo/kaffe