[kaffe] IP address autodetection

Matthew Toseland toad@amphibian.dyndns.org
Sun Feb 23 10:47:01 2003


--i7F3eY7HS/tUJxUd
Content-Type: multipart/mixed; boundary="qcHopEYAB45HaUaB"
Content-Disposition: inline


--qcHopEYAB45HaUaB
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
Content-Transfer-Encoding: quoted-printable

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(). This returns 0.0.0.0 on Kaffe CVS; any=20
chance of fixing? Anything I can do to help?
--=20
Matthew Toseland
toad@amphibian.dyndns.org/amphibian@users.sourceforge.net
Full time freenet hacker.
http://freenetproject.org/
Freenet Distribution Node (temporary) at http://0.0.0.0:8889/HPhwJMG1iEE/
ICTHUS.

--qcHopEYAB45HaUaB
Content-Type: text/x-java; charset=us-ascii
Content-Disposition: attachment; filename="IPAddressDetector.java"
Content-Transfer-Encoding: quoted-printable

package freenet.node;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.net.DatagramSocket;
import java.net.SocketException;
import freenet.support.Checkpointed;
import freenet.Core;
import freenet.Address;
import freenet.Transport;
import freenet.BadAddressException;

/**
 * A class to autodetect our IP address(es)
 */

class IPAddressDetector implements Checkpointed {

    public IPAddressDetector() {
    }
   =20

    /**
     * @return our name
     */
    public String getCheckpointName() {
	return "Autodetection of IP addresses";
    }

    /**=20
     * @return next scheduling point
     */
    public long nextCheckpoint() {
	return System.currentTimeMillis() + 10000; // We are pretty cheap
    }

    InetAddress lastAddress =3D null;
    long lastDetectedTime =3D -1;

    public InetAddress getAddress() {
	return getAddress(0);
    }
   =20
    /**
     * Get the IP address
     */
    public InetAddress getAddress(long recheckTime) {
	if(lastAddress =3D=3D null ||=20
	   System.currentTimeMillis() >=20
	   (lastDetectedTime + recheckTime))
	    checkpoint();
	return lastAddress;
    }
   =20
    /**
     * Execute a checkpoint - detect our internet IP address and log it
     */
    public void checkpoint() {
	DatagramSocket ds;
	try {
	    ds =3D new DatagramSocket();
	} catch (SocketException e) {
	    Core.logger.log(this, "SocketException", e,
			    Core.logger.ERROR);
	    return;
	}
	// This does not transfer any data
	// The ip is a.root-servers.net, 42 is DNS
	try {
	    ds.connect(InetAddress.getByName("198.41.0.4"), 42);
	} catch (UnknownHostException e) {
	    Core.logger.log(this, "UnknownHostException", e,
			    Core.logger.ERROR);
	    return;
	}
	InetAddress myAddress =3D ds.getLocalAddress();
	if(lastAddress !=3D null && (!myAddress.equals(lastAddress))) {
	    Core.logger.log(this, "Address changed!", Core.logger.MINOR);
	    lastAddress =3D myAddress;
	    lastDetectedTime =3D System.currentTimeMillis();
	    ds.close();
	    Main.newInetAddress(myAddress);
	} else {
	    ds.close();
	    lastAddress =3D myAddress;
	    lastDetectedTime =3D System.currentTimeMillis();
	}
    }
}



--qcHopEYAB45HaUaB--

--i7F3eY7HS/tUJxUd
Content-Type: application/pgp-signature
Content-Disposition: inline

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.1 (GNU/Linux)

iD8DBQE+WRdlmlVGrEH/+qIRAqjfAJ49nKS0tmCfCO/hqu/AwEisyyYyzgCcCvuA
iYt7zJekn1S6sic+nQr2JRA=
=eeCr
-----END PGP SIGNATURE-----

--i7F3eY7HS/tUJxUd--