Author: zothar
Date: 2007-09-23 16:02:34 +0000 (Sun, 23 Sep 2007)
New Revision: 15285

Modified:
   trunk/freenet/src/freenet/io/comm/FreenetInetAddress.java
   trunk/freenet/src/freenet/io/comm/Peer.java
   trunk/freenet/src/freenet/node/NodeIPDetector.java
Log:
Move the hostname/IP address syntax check into FreenetInetAddress to simplify 
the code (thanks toad for the suggestion)

Modified: trunk/freenet/src/freenet/io/comm/FreenetInetAddress.java
===================================================================
--- trunk/freenet/src/freenet/io/comm/FreenetInetAddress.java   2007-09-23 
10:12:43 UTC (rev 15284)
+++ trunk/freenet/src/freenet/io/comm/FreenetInetAddress.java   2007-09-23 
16:02:34 UTC (rev 15285)
@@ -11,6 +11,8 @@

 import freenet.io.AddressIdentifier;
 import freenet.support.Logger;
+import freenet.support.transport.ip.HostnameSyntaxException;
+import freenet.support.transport.ip.HostnameUtil;
 import freenet.support.transport.ip.IPUtil;

 /**
@@ -58,6 +60,39 @@
        }

        /**
+        * Create from serialized form on a DataInputStream.
+        */
+       public FreenetInetAddress(DataInputStream dis, boolean 
checkHostnameOrIPSyntax) throws HostnameSyntaxException, IOException {
+               int firstByte = dis.readUnsignedByte();
+               byte[] ba;
+               if(firstByte == 255) {
+                       if(Logger.shouldLog(Logger.MINOR, this)) 
Logger.minor(this, "New format IPv6 address");
+                       // New format IPv6 address
+                       ba = new byte[16];
+                       dis.readFully(ba);
+               } else if(firstByte == 0) {
+                       if(Logger.shouldLog(Logger.MINOR, this)) 
Logger.minor(this, "New format IPv4 address");
+                       // New format IPv4 address
+                       ba = new byte[4];
+                       dis.readFully(ba);
+               } else {
+                       // Old format IPv4 address
+                       ba = new byte[4];
+                       ba[0] = (byte)firstByte;
+                       dis.readFully(ba, 1, 3);
+               }
+               _address = InetAddress.getByAddress(ba);
+               String name = null;
+               String s = dis.readUTF();
+               if(s.length() > 0)
+                       name = s;
+               hostname = name;
+        if(checkHostnameOrIPSyntax && null != hostname) {
+               if(!HostnameUtil.isValidHostname(hostname, true)) throw new 
HostnameSyntaxException();
+               }
+       }
+
+       /**
         * Create from an InetAddress. The IP address is primary i.e. fixed.
         * The hostname either doesn't exist, or is looked up.
         */
@@ -100,6 +135,44 @@
         // 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 FreenetInetAddress(String host, boolean allowUnknown, boolean 
checkHostnameOrIPSyntax) throws HostnameSyntaxException, UnknownHostException {
+        InetAddress addr = null;
+        if(host != null){
+               if(host.startsWith("/")) host = host.substring(1);
+               host = host.trim();
+        }
+        // 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);
+        boolean logDEBUG = Logger.shouldLog(Logger.DEBUG, this);
+        if(logDEBUG) 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;
+            }
+            if(logDEBUG) Logger.debug(this, "host is '"+host+"' and 
addr.getHostAddress() is '"+addr.getHostAddress()+ '\'');
+            if(addr != null && addr.getHostAddress().equals(host)) {
+               if(logDEBUG) Logger.debug(this, '\'' +host+"' looks like an IP 
address");
+                host = null;
+            } else {
+                addr = null;
+            }
+        }
+        if( addr == null ) {
+               if(logDEBUG) Logger.debug(this, '\'' +host+"' does not look 
like an IP address");
+        }
+        this._address = addr;
+        this.hostname = host;
+        if(checkHostnameOrIPSyntax && null != this.hostname) {
+               if(!HostnameUtil.isValidHostname(this.hostname, true)) throw 
new HostnameSyntaxException();
+               }
+        // 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)) {

Modified: trunk/freenet/src/freenet/io/comm/Peer.java
===================================================================
--- trunk/freenet/src/freenet/io/comm/Peer.java 2007-09-23 10:12:43 UTC (rev 
15284)
+++ trunk/freenet/src/freenet/io/comm/Peer.java 2007-09-23 16:02:34 UTC (rev 
15285)
@@ -26,7 +26,6 @@
 import java.net.UnknownHostException;

 import freenet.support.transport.ip.HostnameSyntaxException;
-import freenet.support.transport.ip.HostnameUtil;
 import freenet.support.transport.ip.IPUtil;

 /**
@@ -56,6 +55,11 @@
                _port = dis.readInt();
        }

+       public Peer(DataInputStream dis, boolean checkHostnameOrIPSyntax) 
throws HostnameSyntaxException, IOException {
+               addr = new FreenetInetAddress(dis, checkHostnameOrIPSyntax);
+               _port = dis.readInt();
+       }
+
        /**
         * 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
@@ -74,7 +78,7 @@
         * @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.
-        * @param checkHostname If true, validate the syntax of the given DNS 
hostname or IPv4
+        * @param checkHostnameOrIPSyntax If true, validate the syntax of the 
given DNS hostname or IPv4
         * IP address
         * @throws HostSyntaxException If the string is not formatted as a 
proper DNS hostname
         * or IPv4 IP address
@@ -83,14 +87,11 @@
         * @throws UnknownHostException If allowUnknown is not set, and a 
domain name which does
         * not exist was passed in.
         */
-    public Peer(String physical, boolean allowUnknown, boolean checkHostname) 
throws HostnameSyntaxException, PeerParseException, UnknownHostException {
+    public Peer(String physical, boolean allowUnknown, boolean 
checkHostnameOrIPSyntax) throws HostnameSyntaxException, PeerParseException, 
UnknownHostException {
         int offset = physical.lastIndexOf(':'); // ipv6
         if(offset < 0) throw new PeerParseException();
         String host = physical.substring(0, offset);
-        if(checkHostname) {
-               if(!HostnameUtil.isValidHostname(host, true)) throw new 
HostnameSyntaxException();
-               }
-        addr = new FreenetInetAddress(host, allowUnknown);
+        addr = new FreenetInetAddress(host, allowUnknown, 
checkHostnameOrIPSyntax);
         String strport = physical.substring(offset+1);
         try {
             _port = Integer.parseInt(strport);

Modified: trunk/freenet/src/freenet/node/NodeIPDetector.java
===================================================================
--- trunk/freenet/src/freenet/node/NodeIPDetector.java  2007-09-23 10:12:43 UTC 
(rev 15284)
+++ trunk/freenet/src/freenet/node/NodeIPDetector.java  2007-09-23 16:02:34 UTC 
(rev 15285)
@@ -306,23 +306,14 @@
                                        redetectAddress();
                                        return;
                                }
-                               // Try making a dummy Peer, which will allow us 
to do a syntax check on the given hostname/IP address
+                               FreenetInetAddress addr;
                                try {
-                                       String hostAndDummyPort = val + 
":8888";  // add a dummy port so our string can be parsed by Peer's constructor
-                                       new Peer(hostAndDummyPort, false, true);
+                                       addr = new FreenetInetAddress(val, 
false, true);
                                } catch (HostnameSyntaxException e) {
                                        throw new 
InvalidConfigValueException(l10n("unknownHostErrorInIPOverride", "error", 
"hostname or IP address syntax error"));
-                               } catch (PeerParseException e) {
-                                       throw new 
InvalidConfigValueException(l10n("unknownHostErrorInIPOverride", "error", 
"parse error"));
                                } catch (UnknownHostException e) {
                                        throw new 
InvalidConfigValueException(l10n("unknownHostErrorInIPOverride", "error", 
e.getMessage()));
                                }
-                               FreenetInetAddress addr;
-                               try {
-                                       addr = new FreenetInetAddress(val, 
false);
-                               } catch (UnknownHostException e) {
-                                       throw new 
InvalidConfigValueException(l10n("unknownHostErrorInIPOverride", "error", 
e.getMessage()));
-                               }
                                overrideIPAddress = addr;
                                lastIPAddress = null;
                                redetectAddress();


Reply via email to