Author: toad
Date: 2006-05-12 16:34:26 +0000 (Fri, 12 May 2006)
New Revision: 8678

Added:
   trunk/freenet/src/freenet/io/comm/FreenetInetAddress.java
Modified:
   trunk/freenet/src/freenet/clients/http/ConfigToadlet.java
   trunk/freenet/src/freenet/clients/http/DarknetConnectionsToadlet.java
   trunk/freenet/src/freenet/io/comm/DMT.java
   trunk/freenet/src/freenet/io/comm/Peer.java
   trunk/freenet/src/freenet/node/Node.java
   trunk/freenet/src/freenet/node/PeerManager.java
   trunk/freenet/src/freenet/node/PeerNode.java
   trunk/freenet/src/freenet/node/TextModeClientInterface.java
   trunk/freenet/src/freenet/node/Version.java
Log:
698:
Split Peer into Peer and FreenetInetAddress.
FreenetInetAddress is an InetAddress where the name is primary rather than the 
IP.
FIAs can therefore be safely used long-term for dyndns addresses.
InetAddress's can't, because the IP is always primary; therefore InetAddress's 
aren't really suitable for dyndns use. Not sure if this had any practical 
effects...
Fixed a bug involving excessive inserting of ARKs.
Also improved comparison functions for FIAs (originally Peer's).
Allow creation of FIAs and Peers for nonexistant domains in physical.udp from 
other nodes (but not in ipAddressOverride).
Ignore metadata in non-local references.
Delete some old Dijjer messages.

Modified: trunk/freenet/src/freenet/clients/http/ConfigToadlet.java
===================================================================
--- trunk/freenet/src/freenet/clients/http/ConfigToadlet.java   2006-05-12 
16:13:20 UTC (rev 8677)
+++ trunk/freenet/src/freenet/clients/http/ConfigToadlet.java   2006-05-12 
16:34:26 UTC (rev 8678)
@@ -51,7 +51,7 @@
                                Logger.minor(this, "Setting 
"+prefix+"."+configName);

                                // we ignore unreconized parameters 
-                               if(request.getParam(prefix+"."+configName) != 
""){
+                               
if(request.isParameterSet(prefix+"."+configName)) {
                                        
if(!(o[j].getValueString().equals(request.getParam(prefix+"."+configName)))){
                                                Logger.minor(this, "Setting 
"+prefix+"."+configName+" to "+request.getParam(prefix+"."+configName));
                                                try{

Modified: trunk/freenet/src/freenet/clients/http/DarknetConnectionsToadlet.java
===================================================================
--- trunk/freenet/src/freenet/clients/http/DarknetConnectionsToadlet.java       
2006-05-12 16:13:20 UTC (rev 8677)
+++ trunk/freenet/src/freenet/clients/http/DarknetConnectionsToadlet.java       
2006-05-12 16:34:26 UTC (rev 8678)
@@ -280,7 +280,7 @@
                        }
                        PeerNode pn;
                        try {
-                               pn = new PeerNode(fs, this.node);
+                               pn = new PeerNode(fs, this.node, false);
                        } catch (FSParseException e1) {
                                this.sendErrorPage(ctx, 200, "Failed to add 
node", "Unable to parse the given text: <pre>"+ref+"</pre> as a node reference: 
"+e1+". Please <a href=\".\">Try again</a>.");
                                return;

Modified: trunk/freenet/src/freenet/io/comm/DMT.java
===================================================================
--- trunk/freenet/src/freenet/io/comm/DMT.java  2006-05-12 16:13:20 UTC (rev 
8677)
+++ trunk/freenet/src/freenet/io/comm/DMT.java  2006-05-12 16:34:26 UTC (rev 
8678)
@@ -119,27 +119,6 @@
                return msg;
        }

-       public static final MessageType whoAreYou = new 
MessageType("whoAreYou") {{
-       }};
-
-       public static final Message createWhoAreYou() {
-               return new Message(whoAreYou);
-       }
-
-       public static final MessageType introduce = new 
MessageType("introduce") {{
-               addField(EXTERNAL_ADDRESS, Peer.class);
-               addField(BUILD, Integer.class);
-               addField(FIRST_GOOD_BUILD, Integer.class);
-       }};
-       
-       public static final Message createIntroduce(Peer externalAddress) {
-               Message msg = new Message(introduce);
-               msg.set(EXTERNAL_ADDRESS, externalAddress);
-//             msg.set(BUILD, Dijjer.BUILD);
-//             msg.set(FIRST_GOOD_BUILD, Dijjer.FIRST_GOOD_BUILD);
-               return msg;
-       }
-
        public static final MessageType rejectDueToLoop = new 
MessageType("rejectDueToLoop") {{ 
                addField(UID, Long.class);
        }};
@@ -151,144 +130,6 @@
        }

        // Assimilation
-       public static final MessageType joinRequest = new 
MessageType("joinRequest") {{
-               addField(UID, Long.class);
-               addField(JOINER, Peer.class);
-               addField(TTL, Integer.class);
-       }};
-       
-       public static final Message createJoinRequest(long uid, Peer joiner, 
int ttl) {
-               Message msg = new Message(joinRequest);
-               msg.set(UID, uid);
-               msg.set(JOINER, joiner);
-               msg.set(TTL, ttl);
-               return msg;
-       }
-       
-       public static final MessageType joinRequestAck = new 
MessageType("joinRequestAck") {{
-               addField(UID, Long.class);
-       }};
-       
-       public static final Message createJoinRequestAck(long uid) {
-               Message msg = new Message(joinRequestAck);
-               msg.set(UID, uid);
-               return msg;
-       }
-       
-       public static final MessageType joinResponse = new 
MessageType("joinResponse") {{
-               addField(UID, Long.class);
-               addLinkedListField(PEERS, Peer.class);
-       }};
-       
-       public static final Message createJoinResponse(long uid, List peers) {
-               Message msg = new Message(joinResponse);
-               msg.set(UID, uid);
-               msg.set(PEERS, peers);
-               return msg;
-       }       
-       
-       public static final MessageType disconnectNotification = new 
MessageType("disconnectNotification") {{ 
-               addField(REASON, String.class);
-       }};
-       
-       public static final Message createDisconnectNotification(String reason) 
{
-               Message msg = new Message(disconnectNotification);
-               msg.set(REASON, reason);
-               return msg;
-       }
-       
-       // Data search
-       public static final MessageType requestData = new 
MessageType("requestData") {{
-               addField(UID, Long.class);
-               addField(URL, String.class);
-               addLinkedListField(FORWARDERS, Peer.class);
-               addField(FILE_LENGTH, Long.class);
-               addField(LAST_MODIFIED, String.class);
-               addField(CHUNK_NO, Integer.class);
-               addField(TTL, Integer.class);
-       }};
-       
-       public static final Message createRequestData(long uid, String url, 
List forwarders, long fileLength, String lastModified, int chunkNo, int ttl) {
-               Message msg = new Message(requestData);
-               msg.set(UID, uid);
-               msg.set(URL, url);
-               msg.set(FORWARDERS, forwarders);
-               msg.set(FILE_LENGTH, fileLength);
-               msg.set(LAST_MODIFIED, lastModified);
-               msg.set(CHUNK_NO, chunkNo);
-               msg.set(TTL, ttl);
-               return msg;
-       }
-
-       public static final MessageType acknowledgeRequest = new 
MessageType("acknowledgeRequest") {{
-               addField(UID, Long.class);
-       }};
-       
-       public static final Message createAcknowledgeRequest(long uid) {
-               Message msg = new Message(acknowledgeRequest);
-               msg.set(UID, uid);
-               return msg;
-       }
-       
-       public static final MessageType requestSuccessful = new 
MessageType("requestSuccessful") {{
-               addField(UID, Long.class);
-               addField(DATA_SOURCE, Peer.class);
-               addField(CACHED, Boolean.class);
-       }};
-
-       public static final Message createRequestSuccessful(long uid, Peer 
dataSource, boolean cached) {
-               Message msg = new Message(requestSuccessful);
-               msg.set(UID, uid);
-               msg.set(DATA_SOURCE, dataSource);
-               msg.set(CACHED, cached);
-               return msg;
-       }
-
-       public static final MessageType requestFailed = new 
MessageType("requestFailed") {{
-               addField(UID, Long.class);
-               addField(REASON, Integer.class);
-               addField(DESCRIPTION, String.class);
-       }};
-       
-       public static final Message createRequestFailed(long uid, int reason, 
String description) {
-               Message msg = new Message(requestFailed);
-               msg.set(UID, uid);
-               msg.set(REASON, reason);
-               msg.set(DESCRIPTION, description);
-               return msg;
-       }
-       
-       // Hash search
-       public static final MessageType requestHash = new 
MessageType("requestHash") {{
-               addField(UID, Long.class);
-               addField(URL, String.class);
-               addField(FILE_LENGTH, Long.class);
-               addField(LAST_MODIFIED, String.class);
-               addField(CHUNK_NO, Integer.class);
-               addField(TTL, Integer.class);
-       }};
-       
-       public static final Message createRequestHash(long uid, String url, 
long fileLength, String lastModified, int chunkNo, int ttl) {
-               Message msg = new Message(requestHash);
-               msg.set(UID, uid);
-               msg.set(URL, url);
-               msg.set(FILE_LENGTH, fileLength);
-               msg.set(LAST_MODIFIED, lastModified);
-               msg.set(CHUNK_NO, chunkNo);
-               msg.set(TTL, ttl);
-               return msg;
-       }
-       
-       public static final MessageType requestHashAck = new 
MessageType("requestHashAck") {{
-               addField(UID, Long.class);
-       }};
-       
-       public static final Message createRequestHashAck(long uid) {
-               Message msg = new Message(requestHashAck);
-               msg.set(UID, uid);
-               return msg;
-       }
-       
        // Corruption notification
        public static final MessageType corruptionNotification = new 
MessageType("corruptionNotification") {{
                addField(UID, Long.class);

Added: trunk/freenet/src/freenet/io/comm/FreenetInetAddress.java
===================================================================
--- trunk/freenet/src/freenet/io/comm/FreenetInetAddress.java   2006-05-12 
16:13:20 UTC (rev 8677)
+++ trunk/freenet/src/freenet/io/comm/FreenetInetAddress.java   2006-05-12 
16:34:26 UTC (rev 8678)
@@ -0,0 +1,243 @@
+package freenet.io.comm;
+
+import java.io.DataInputStream;
+import java.io.DataOutputStream;
+import java.io.EOFException;
+import java.io.IOException;
+import java.net.InetAddress;
+import java.net.UnknownHostException;
+
+import freenet.io.AddressIdentifier;
+import freenet.support.Logger;
+
+/**
+ * Long-term InetAddress. If created with an IP address, then the IP address 
is primary.
+ * If created with a name, then the name is primary, and the IP address can 
change.
+ * Most code ripped from Peer.
+ * @author root
+ *
+ */
+public class FreenetInetAddress {
+
+       // hostname - only set if we were created with a hostname
+       // and not an address
+       private final String hostname;
+       private InetAddress _address;
+
+       /**
+        * Create from serialized form on a DataInputStream.
+        */
+       public FreenetInetAddress(DataInputStream dis) throws IOException {
+               byte[] ba = new byte[4];
+               dis.readFully(ba);
+               _address = InetAddress.getByAddress(ba);
+               String name = null;
+               // FIXME once everyone has upgraded, remove the try { } catch 
() { }.
+               try {
+                       String s = dis.readUTF();
+                       if(s.length() > 0)
+                               name = s;
+               } catch (EOFException e) {
+                       // Ignore
+                       name = null;
+               }
+               hostname = name;
+       }
+
+       /**
+        * Create from an InetAddress. The IP address is primary i.e. fixed.
+        * The hostname either doesn't exist, or is looked up.
+        */
+       public FreenetInetAddress(InetAddress address) {
+               _address = address;
+               hostname = null;
+       }
+
+       public FreenetInetAddress(String host, boolean allowUnknown) throws 
UnknownHostException {
+        InetAddress addr = null;
+        // if we were created with an explicit IP address, use it as such
+        // debugging log messages because AddressIdentifier doesn't appear to 
handle all IPv6 literals correctly, such as "fe80::204:1234:dead:beef"
+        AddressIdentifier.AddressType addressType = 
AddressIdentifier.getAddressType(host);
+        Logger.debug(this, "Address type of '"+host+"' appears to be 
'"+addressType+"'");
+        if(!addressType.toString().equals("Other")) {
+            try {
+                addr = InetAddress.getByName(host);
+            } catch (UnknownHostException e) {
+               if(!allowUnknown) throw e;
+                addr = null;
+            }
+            Logger.debug(this, "host is '"+host+"' and addr.getHostAddress() 
is '"+addr.getHostAddress()+"'");
+            if(addr.getHostAddress().equals(host)) {
+                Logger.debug(this, "'"+host+"' looks like an IP address");
+                host = null;
+            } else {
+                addr = null;
+            }
+        }
+        if( addr == null ) {
+            Logger.debug(this, "'"+host+"' does not look like an IP address");
+        }
+        this._address = addr;
+        this.hostname = host;
+        // we're created with a hostname so delay the lookup of the address
+        // until it's needed to work better with dynamic DNS hostnames
+       }
+       
+       public boolean equals(Object o) {
+               if(!(o instanceof FreenetInetAddress)) {
+                       return false;
+               }
+               FreenetInetAddress addr = (FreenetInetAddress)o;
+               if(hostname != null) {
+                       if(addr.hostname == null)
+                               return false;
+                       if (!hostname.equalsIgnoreCase(addr.hostname)) {
+                               return false;
+                       }
+                       // Now that we know we have the same hostname, we can 
propagate the IP.
+                       if(_address != null && addr._address == null)
+                               addr._address = _address;
+                       if(addr._address != null && _address == null)
+                               _address = addr._address;
+                       // Except if we actually do have two different 
looked-up IPs!
+                       if(addr._address != null && _address != null && 
!addr._address.equals(_address))
+                               return false;
+                       // Equal.
+                       return true;
+               }
+
+               // No hostname, go by address.
+               if(!_address.equals(addr._address)) {
+                       return false;
+               }
+               
+               return true;
+       }
+
+       public boolean strictEquals(FreenetInetAddress addr) {
+               if(hostname != null) {
+                       if(addr.hostname == null)
+                               return false;
+                       if (!hostname.equalsIgnoreCase(addr.hostname)) {
+                               return false;
+                       }
+                       // Now that we know we have the same hostname, we can 
propagate the IP.
+                       if(_address != null && addr._address == null)
+                               addr._address = _address;
+                       if(addr._address != null && _address == null)
+                               _address = addr._address;
+                       // Except if we actually do have two different 
looked-up IPs!
+                       if(addr._address != null && _address != null && 
!addr._address.equals(_address))
+                               return false;
+                       // Equal.
+                       return true;
+               }
+
+               // No hostname, go by address.
+               
if(!getHostName(_address).equalsIgnoreCase(getHostName(addr._address))) {
+                       // FIXME remove excessive logging
+                       Logger.minor(this, "Addresses do not match: 
mine="+getHostName(_address)+" his="+getHostName(addr._address));
+                       return false;
+               }
+               
+               return true;
+       }
+
+       /**
+        * Get the IP address. Look it up if necessary, but return the last 
value if it
+        * has ever been looked up before; will not trigger a new lookup if it 
has been
+        * looked up before.
+        */
+       public InetAddress getAddress() {
+               if (_address != null) {
+                       return _address;
+               } else {
+                       InetAddress addr = getHandshakeAddress();
+                       if( addr != null ) {
+                               this._address = addr;
+                       }
+                       return addr;
+               }
+       }
+
+       /**
+        * Get the IP address, looking up the hostname if the hostname is 
primary, even if
+        * it has been looked up before. Typically called on a reconnect 
attempt, when the
+        * dyndns address may have changed.
+        */
+       public InetAddress getHandshakeAddress() {
+           // Since we're handshaking, hostname-to-IP may have changed
+           if (_address != null && hostname == null) {
+               return _address;
+           } else {
+                Logger.minor(this, "Looking up '"+hostname+"' in DNS");
+               /* 
+                * Peers are constructed from an address once a
+                * handshake has been completed, so this lookup
+                * will only be performed during a handshake
+                * (this method should normally only be called
+                * from PeerNode.getHandshakeIPs() and once
+                * each connection from this.getAddress()
+                * otherwise) - it doesn't mean we perform a
+                * DNS lookup with every packet we send.
+                */
+               try {
+                    InetAddress addr = InetAddress.getByName(hostname);
+                    //Logger.normal(this, "Look up got '"+addr+"'");
+                    if( addr != null ) {
+                        /*
+                         * cache the answer since getHandshakeAddress()
+                         * doesn't use the cached value, thus
+                         * getHandshakeIPs() should always get the
+                         * latest value from DNS (minus Java's caching)
+                         */
+                        this._address = addr;
+                    }
+                    return addr;
+               } catch (UnknownHostException e) {
+                   return null;
+               }
+           }
+       }
+
+       public int hashCode() {
+               if (_address != null) {
+                       return _address.hashCode();
+               } else {
+                       return hostname.hashCode();
+               }
+       }
+       
+       public String toString() {
+               if (_address != null) {
+                       return getHostName(_address);
+               } else {
+                       return hostname;
+               }
+       }
+
+       public void writeToDataOutputStream(DataOutputStream dos) throws 
IOException {
+               InetAddress addr = this.getAddress();
+               if (addr == null) throw new UnknownHostException();
+               dos.write(addr.getAddress());
+               if(hostname != null)
+                       dos.writeUTF(hostname);
+               else
+                       dos.writeUTF("");
+       }
+
+       /**
+        * Return the hostname or the IP address of the given InetAddress.
+        * Does not attempt to do a reverse lookup; if the hostname is
+        * known, return it, otherwise return the textual IP address.
+        */
+       public static String getHostName(InetAddress primaryIPAddress) {
+               if(primaryIPAddress == null) return null;
+               String s = primaryIPAddress.toString();
+               String addr = s.substring(0, s.indexOf('/')).trim();
+               if(addr.length() == 0)
+                       return primaryIPAddress.getHostAddress();
+               else
+                       return addr;
+       }
+}

Modified: trunk/freenet/src/freenet/io/comm/Peer.java
===================================================================
--- trunk/freenet/src/freenet/io/comm/Peer.java 2006-05-12 16:13:20 UTC (rev 
8677)
+++ trunk/freenet/src/freenet/io/comm/Peer.java 2006-05-12 16:34:26 UTC (rev 
8678)
@@ -35,11 +35,8 @@
 public class Peer implements WritableToDataOutputStream {

     public static final String VERSION = "$Id: Peer.java,v 1.4 2005/08/25 
17:28:19 amphibian Exp $";
-       
-       // hostname - only set if we were created with a hostname
-       // and not an address
-       private final String hostname;
-       private InetAddress _address;
+
+    private final FreenetInetAddress addr;
        private final int _port;

        // Create a null peer
@@ -48,53 +45,39 @@
        }

        public Peer(DataInputStream dis) throws IOException {
-               byte[] ba = new byte[4];
-               dis.readFully(ba);
-               _address = InetAddress.getByAddress(ba);
+               addr = new FreenetInetAddress(dis);
                _port = dis.readInt();
-               this.hostname = null;
        }

+       /**
+        * Create a Peer from an InetAddress and a port. The IP address is 
primary; that is
+        * to say, it will remain the same regardless of DNS changes. Don't do 
this if you
+        * are dealing with domain names whose IP may change.
+        */
        public Peer(InetAddress address, int port) {
-               _address = address;
+               addr = new FreenetInetAddress(address);
                _port = port;
-               this.hostname = null;
        }

        /**
-     * @param physical
-     */
-    public Peer(String physical) throws PeerParseException {
+        * Create a Peer from a string. This may be an IP address or a domain 
name. If it
+        * is the latter, the name is primary rather than the IP address; 
+        * getHandshakeAddress() will do a new lookup on the name, and change 
the IP address
+        * if the domain name has changed.
+        * @param physical The string to be parsed, in the format [ ip or 
domain name ]:[ port number].
+        * @param allowUnknown If true, allow construction of the Peer even if 
the domain name
+        * lookup fails.
+        * @throws PeerParseException If the string is not valid e.g. if it 
doesn't contain a 
+        * port.
+        * @throws UnknownHostException If allowUnknown is not set, and a 
domain name which does
+        * not exist was passed in.
+        */
+    public Peer(String physical, boolean allowUnknown) throws 
PeerParseException, UnknownHostException {
         int offset = physical.lastIndexOf(':'); // ipv6
         if(offset < 0) throw new PeerParseException();
-        InetAddress addr = null;
         String host = physical.substring(0, offset);
-        // if we were created with an explicit IP address, use it as such
-        // debugging log messages because AddressIdentifier doesn't appear to 
handle all IPv6 literals correctly, such as "fe80::204:1234:dead:beef"
-        AddressIdentifier.AddressType addressType = 
AddressIdentifier.getAddressType(host);
-        Logger.debug(this, "Address type of '"+host+"' appears to be 
'"+addressType+"'");
-        if(!addressType.toString().equals("Other")) {
-            try {
-                addr = InetAddress.getByName(host);
-            } catch (UnknownHostException e) {
-                addr = null;
-            }
-            Logger.debug(this, "host is '"+host+"' and addr.getHostAddress() 
is '"+addr.getHostAddress()+"'");
-            if(addr.getHostAddress().equals(host)) {
-                Logger.debug(this, "'"+host+"' looks like an IP address");
-                host = null;
-            } else {
-                addr = null;
-            }
-        }
-        if( addr == null ) {
-            Logger.debug(this, "'"+host+"' does not look like an IP address");
-        }
-        this._address = addr;
-        this.hostname = host;
+        addr = new FreenetInetAddress(host, allowUnknown);
         String strport = physical.substring(offset+1);
-        // we're created with a hostname so delay the lookup of the address
-        // until it's needed to work better with dynamic DNS hostnames
         try {
             _port = Integer.parseInt(strport);
         } catch (NumberFormatException e) {
@@ -102,7 +85,12 @@
         }
     }

-    public boolean isNull() {
+    public Peer(FreenetInetAddress addr, int port) {
+       this.addr = addr;
+       this._port = port;
+       }
+
+       public boolean isNull() {
                return _port == 0;
        }

@@ -119,15 +107,8 @@
                if (_port != peer._port) {
                        return false;
                }
-               if (this._address != null) {
-                       if (!_address.equals(peer._address)) {
-                               return false;
-                       }
-               } else {
-                       if (!hostname.equals(peer.hostname)) {
-                               return false;
-                       }
-               }
+               if(!addr.equals(peer.addr))
+                       return false;
                return true;
        }

@@ -145,71 +126,31 @@
                if (_port != peer._port) {
                        return false;
                }
-               if (this._address != null) {
-                       
if(!getHostName(_address).equals(getHostName(peer._address))) {
-                               return false;
-                       }
-               } else {
-                       if (!hostname.equals(peer.hostname)) {
-                               return false;
-                       }
-               }
+               if(!addr.strictEquals(peer.addr))
+                       return false;
                return true;
        }
-       
+
+       /**
+        * Get the IP address. Look it up if necessary, but return the last 
value if it
+        * has ever been looked up before; will not trigger a new lookup if it 
has been
+        * looked up before.
+        */
        public InetAddress getAddress() {
-               if (_address != null) {
-                       return _address;
-               } else {
-                       InetAddress addr = getHandshakeAddress();
-                       if( addr != null ) {
-                               this._address = addr;
-                       }
-                       return addr;
-               }
+               return addr.getAddress();
        }
-
+       
+       /**
+        * Get the IP address, looking up the hostname if the hostname is 
primary, even if
+        * it has been looked up before. Typically called on a reconnect 
attempt, when the
+        * dyndns address may have changed.
+        */
        public InetAddress getHandshakeAddress() {
-           // Since we're handshaking, hostname-to-IP may have changed
-           if (_address != null && hostname == null) {
-               return _address;
-           } else {
-                Logger.minor(this, "Looking up '"+hostname+"' in DNS");
-               /* 
-                * Peers are constructed from an address once a
-                * handshake has been completed, so this lookup
-                * will only be performed during a handshake
-                * (this method should normally only be called
-                * from PeerNode.getHandshakeIPs() and once
-                * each connection from this.getAddress()
-                * otherwise) - it doesn't mean we perform a
-                * DNS lookup with every packet we send.
-                */
-               try {
-                    InetAddress addr = InetAddress.getByName(hostname);
-                    //Logger.normal(this, "Look up got '"+addr+"'");
-                    if( addr != null ) {
-                        /*
-                         * cache the answer since getHandshakeAddress()
-                         * doesn't use the cached value, thus
-                         * getHandshakeIPs() should always get the
-                         * latest value from DNS (minus Java's caching)
-                         */
-                        this._address = addr;
-                    }
-                    return addr;
-               } catch (UnknownHostException e) {
-                   return null;
-               }
-           }
+               return addr.getHandshakeAddress();
        }
-
+       
        public int hashCode() {
-               if (_address != null) {
-                       return _address.hashCode() + _port;
-               } else {
-                       return hostname.hashCode() + _port;
-               }
+               return addr.hashCode() + _port;
        }

        public int getPort() {
@@ -217,32 +158,11 @@
        }

        public String toString() {
-               if (_address != null) {
-                       return getHostName(_address) + ":" + _port;
-               } else {
-                       return hostname + ":" + _port;
-               }
+               return addr.toString() + ":" + _port;
        }

        public void writeToDataOutputStream(DataOutputStream dos) throws 
IOException {
-               InetAddress addr = this.getAddress();
-               if (addr == null) throw new UnknownHostException();
-               dos.write(addr.getAddress());
+               addr.writeToDataOutputStream(dos);
                dos.writeInt(_port);
        }
-
-       /**
-        * Return the hostname or the IP address of the given InetAddress.
-        * Does not attempt to do a reverse lookup; if the hostname is
-        * known, return it, otherwise return the textual IP address.
-        */
-       public static String getHostName(InetAddress primaryIPAddress) {
-               if(primaryIPAddress == null) return null;
-               String s = primaryIPAddress.toString();
-               String addr = s.substring(0, s.indexOf('/')).trim();
-               if(addr.length() == 0)
-                       return primaryIPAddress.getHostAddress();
-               else
-                       return addr;
-       }
 }
\ No newline at end of file

Modified: trunk/freenet/src/freenet/node/Node.java
===================================================================
--- trunk/freenet/src/freenet/node/Node.java    2006-05-12 16:13:20 UTC (rev 
8677)
+++ trunk/freenet/src/freenet/node/Node.java    2006-05-12 16:34:26 UTC (rev 
8678)
@@ -59,6 +59,7 @@
 import freenet.crypt.Yarrow;
 import freenet.io.comm.DMT;
 import freenet.io.comm.DisconnectedException;
+import freenet.io.comm.FreenetInetAddress;
 import freenet.io.comm.Message;
 import freenet.io.comm.MessageFilter;
 import freenet.io.comm.Peer;
@@ -183,9 +184,11 @@
                                        lastInsertedAddress = null;
                                else {
                                        try {
-                                               lastInsertedAddress = new 
Peer(fs.get("physical.udp"));
+                                               lastInsertedAddress = new 
Peer(fs.get("physical.udp"), false);
                                        } catch (PeerParseException e1) {
                                                Logger.error(this, "Error 
parsing own ref: "+e1+" : "+fs.get("physical.udp"), e1);
+                                       } catch (UnknownHostException e1) {
+                                               Logger.error(this, "Error 
parsing own ref: "+e1+" : "+fs.get("physical.udp"), e1);
                                        }
                                }
                        } catch (InserterException e) {
@@ -536,7 +539,7 @@
         if(physical != null) {
                Peer myOldPeer;
                try {
-                       myOldPeer = new Peer(physical);
+                       myOldPeer = new Peer(physical, false);
                } catch (PeerParseException e) {
                        IOException e1 = new IOException();
                        e1.initCause(e);
@@ -801,7 +804,7 @@

                        public String get() {
                                if(overrideIPAddress == null) return "";
-                               else return Peer.getHostName(overrideIPAddress);
+                               else return overrideIPAddress.toString();
                        }

                        public void set(String val) throws 
InvalidConfigValueException {
@@ -814,9 +817,9 @@
                                        shouldInsertARK();
                                        return;
                                }
-                               InetAddress addr;
+                               FreenetInetAddress addr;
                                try {
-                                       addr = InetAddress.getByName(val);
+                                       addr = new FreenetInetAddress(val, 
false);
                                } catch (UnknownHostException e) {
                                        throw new 
InvalidConfigValueException("Unknown host: "+e.getMessage());
                                }
@@ -833,7 +836,7 @@
                overrideIPAddress = null;
        else {
                        try {
-                               overrideIPAddress = 
InetAddress.getByName(ipOverrideString);
+                               overrideIPAddress = new 
FreenetInetAddress(ipOverrideString, false);
                        } catch (UnknownHostException e) {
                                String msg = "Unknown host: 
"+ipOverrideString+" in config: "+e.getMessage();
                                Logger.error(this, msg);
@@ -1755,10 +1758,10 @@
      */
     public SimpleFieldSet exportPublicFieldSet() {
         SimpleFieldSet fs = new SimpleFieldSet(true);
-        InetAddress ip = getPrimaryIPAddress();
+        FreenetInetAddress ip = getPrimaryIPAddress();
         fs.put("base64", "true");
         if(ip != null)
-               fs.put("physical.udp", Peer.getHostName(ip)+":"+portNumber);
+               fs.put("physical.udp", ip.toString()+":"+portNumber);
         fs.put("identity", Base64.encode(myIdentity));
         fs.put("location", Double.toString(lm.getLocation().getValue()));
         fs.put("version", Version.getVersionString());
@@ -1775,11 +1778,11 @@
         return fs;
     }

-    InetAddress overrideIPAddress;
+    FreenetInetAddress overrideIPAddress;
     /** IP address from last time */
     InetAddress oldIPAddress;
     /** Last detected IP address */
-    InetAddress lastIPAddress;
+    FreenetInetAddress lastIPAddress;

     /**
      * @return Our current main IP address.
@@ -1787,7 +1790,7 @@
      * detection properly with NetworkInterface, and we should use
      * third parties if available and UP&P if available.
      */
-    InetAddress detectPrimaryIPAddress() {
+    FreenetInetAddress detectPrimaryIPAddress() {
         if(overrideIPAddress != null) {
             Logger.minor(this, "Returning overridden address: 
"+overrideIPAddress);
             lastIPAddress = overrideIPAddress;
@@ -1796,8 +1799,9 @@
         Logger.minor(this, "IP address not overridden");
                InetAddress addr = ipDetector.getAddress();
                if(addr != null) {
-                       lastIPAddress = addr;
-                       return addr;
+                       FreenetInetAddress a = new FreenetInetAddress(addr);
+                       lastIPAddress = a;
+                       return a;
                }
                // Try to pick it up from our connections
                if(peers != null) {
@@ -1820,7 +1824,9 @@
                        if(countsByPeer.size() == 0) return null;
                        Iterator it = countsByPeer.keySet().iterator();
                        if(countsByPeer.size() == 1) {
-                               return (InetAddress) it.next();
+                       FreenetInetAddress a = new 
FreenetInetAddress((InetAddress)it.next());
+                       lastIPAddress = a;
+                       return a;
                        }
                        // Pick most popular address
                        // FIXME use multi-homing here
@@ -1834,10 +1840,10 @@
                                        best = cur;
                                }
                        }
-                       lastIPAddress = best;
+                       lastIPAddress = new FreenetInetAddress(best);
                }
                else {
-                       lastIPAddress = oldIPAddress;
+                       lastIPAddress = new FreenetInetAddress(oldIPAddress);
                }
                if (lastIPAddress == null) {
                        this.alerts.register(primaryIPUndetectedAlert);
@@ -1847,7 +1853,7 @@
                return lastIPAddress;
     }

-    InetAddress getPrimaryIPAddress() {
+    FreenetInetAddress getPrimaryIPAddress() {
        if(lastIPAddress == null) return detectPrimaryIPAddress();
        return lastIPAddress;
     }
@@ -2335,12 +2341,12 @@
                return sskInsertThrottle;
        }

-       InetAddress lastIP;
+       FreenetInetAddress lastIP;

        public void redetectAddress() {
-               InetAddress newIP = detectPrimaryIPAddress();
+               FreenetInetAddress newIP = detectPrimaryIPAddress();
                shouldInsertARK();
-               if(newIP.equals(lastIP)) return;
+               if(newIP == null || newIP.equals(lastIP)) return;
                writeNodeFile();
        }


Modified: trunk/freenet/src/freenet/node/PeerManager.java
===================================================================
--- trunk/freenet/src/freenet/node/PeerManager.java     2006-05-12 16:13:20 UTC 
(rev 8677)
+++ trunk/freenet/src/freenet/node/PeerManager.java     2006-05-12 16:34:26 UTC 
(rev 8678)
@@ -69,7 +69,7 @@
                     fs = new SimpleFieldSet(br, true);
                     PeerNode pn;
                     try {
-                        pn = new PeerNode(fs, node);
+                        pn = new PeerNode(fs, node, true);
                     } catch (FSParseException e2) {
                         Logger.error(this, "Could not parse peer: 
"+e2+"\n"+fs.toString(),e2);
                         continue;
@@ -238,7 +238,7 @@
      * Connect to a node provided the fieldset representing it.
      */
     public void connect(SimpleFieldSet noderef) throws FSParseException, 
PeerParseException {
-        PeerNode pn = new PeerNode(noderef, node);
+        PeerNode pn = new PeerNode(noderef, node, false);
         for(int i=0;i<myPeers.length;i++) {
             if(Arrays.equals(myPeers[i].identity, pn.identity)) return;
         }

Modified: trunk/freenet/src/freenet/node/PeerNode.java
===================================================================
--- trunk/freenet/src/freenet/node/PeerNode.java        2006-05-12 16:13:20 UTC 
(rev 8677)
+++ trunk/freenet/src/freenet/node/PeerNode.java        2006-05-12 16:34:26 UTC 
(rev 8678)
@@ -7,6 +7,7 @@
 import java.io.UnsupportedEncodingException;
 import java.io.Writer;
 import java.net.InetAddress;
+import java.net.UnknownHostException;
 import java.security.MessageDigest;
 import java.security.NoSuchAlgorithmException;
 import java.util.Arrays;
@@ -192,7 +193,7 @@
      * @param fs The SimpleFieldSet to parse
      * @param node2 The running Node we are part of.
      */
-    public PeerNode(SimpleFieldSet fs, Node node2) throws FSParseException, 
PeerParseException {
+    public PeerNode(SimpleFieldSet fs, Node node2, boolean fromLocal) throws 
FSParseException, PeerParseException {
         this.node = node2;
         boolean base64 = Fields.stringToBool(fs.get("base64"), false);
         String identityString = fs.get("identity");
@@ -233,12 +234,13 @@
         try{
                String physical[]=fs.getAll("physical.udp");
                if(physical==null){
-                       Peer p = new Peer(fs.get("physical.udp"));
+                       // Be tolerant of nonexistent domains.
+                       Peer p = new Peer(fs.get("physical.udp"), true);
                        if(p != null)
                                nominalPeer.addElement(p);
                }else{
                        for(int i=0;i<physical.length;i++){             
-                                       Peer p = new Peer(physical[i]);
+                                       Peer p = new Peer(physical[i], true);
                                    if(!nominalPeer.contains(p)) 
                                        nominalPeer.addElement(p);
                        }
@@ -322,12 +324,25 @@
         // Now for the metadata.
         // The metadata sub-fieldset contains data about the node which is not 
part of the node reference.
         // It belongs to this node, not to the node being described.
+        // Therefore, if we are parsing a remotely supplied ref, ignore it.

+        if(!fromLocal) return;
+        
         SimpleFieldSet metadata = fs.subset("metadata");

         if(metadata != null) {

-               Peer p = new Peer(metadata.get("detected.udp"));
+               // Don't be tolerant of nonexistant domains; this should be an 
IP address.
+               Peer p;
+                       try {
+                               p = new Peer(metadata.get("detected.udp"), 
false);
+                       } catch (UnknownHostException e) {
+                               p = null;
+                               Logger.error(this, "detected.udp = 
"+metadata.get("detected.udp")+" - "+e, e);
+                       } catch (PeerParseException e) {
+                               p = null;
+                               Logger.error(this, "detected.udp = 
"+metadata.get("detected.udp")+" - "+e, e);
+                       }
                if(p != null)
                        detectedPeer = p;

@@ -373,10 +388,10 @@
        }
        // Hack for two nodes on the same IP that can't talk over inet for 
routing reasons
        InetAddress localhost = node.localhostAddress;
-       InetAddress nodeIP = node.getPrimaryIPAddress();
+       InetAddress nodeIP = node.getPrimaryIPAddress().getAddress();
        if(nodeIP != null && nodeIP.equals(localhost)) return p;
        if(peerIP != null && peerIP.equals(localhost)) return p;
-       if(nodeIP != null && nodeIP.equals(peerIP)) {
+       if(nodeIP != null && nodeIP.equals(peerIP)) {
                Peer[] newPeers = new Peer[p.length+1];
                System.arraycopy(p, 0, newPeers, 0, p.length);
                newPeers[newPeers.length-1] = new Peer(node.localhostAddress, 
detectedPeer.getPort());
@@ -955,11 +970,11 @@
         try{
                String physical[]=fs.getAll("physical.udp");
                if(physical==null){
-                       Peer p = new Peer(fs.get("physical.udp"));
+                       Peer p = new Peer(fs.get("physical.udp"), true);
                        nominalPeer.addElement(p);
                }else{
                        for(int i=0;i<physical.length;i++){             
-                                       Peer p = new Peer(physical[i]);
+                                       Peer p = new Peer(physical[i], true);
                                    if(!nominalPeer.contains(p)) 
                                        nominalPeer.addElement(p);
                        }

Modified: trunk/freenet/src/freenet/node/TextModeClientInterface.java
===================================================================
--- trunk/freenet/src/freenet/node/TextModeClientInterface.java 2006-05-12 
16:13:20 UTC (rev 8677)
+++ trunk/freenet/src/freenet/node/TextModeClientInterface.java 2006-05-12 
16:34:26 UTC (rev 8678)
@@ -741,7 +741,7 @@
         }
         PeerNode pn;
         try {
-            pn = new PeerNode(fs, n);
+            pn = new PeerNode(fs, n, false);
         } catch (FSParseException e1) {
             System.err.println("Did not parse: "+e1);
             Logger.error(this, "Did not parse: "+e1, e1);

Modified: trunk/freenet/src/freenet/node/Version.java
===================================================================
--- trunk/freenet/src/freenet/node/Version.java 2006-05-12 16:13:20 UTC (rev 
8677)
+++ trunk/freenet/src/freenet/node/Version.java 2006-05-12 16:34:26 UTC (rev 
8678)
@@ -18,7 +18,7 @@
        public static final String protocolVersion = "1.0";

        /** The build number of the current revision */
-       private static final int buildNumber = 697;
+       private static final int buildNumber = 698;

        /** Oldest build of Fred we will talk to */
        private static final int lastGoodBuild = 591;


Reply via email to