Author: toad
Date: 2006-07-05 23:56:06 +0000 (Wed, 05 Jul 2006)
New Revision: 9473

Modified:
   trunk/freenet/src/freenet/io/comm/FreenetInetAddress.java
   trunk/freenet/src/freenet/io/comm/Peer.java
   trunk/freenet/src/freenet/io/comm/UdpSocketManager.java
   trunk/freenet/src/freenet/node/FNPPacketMangler.java
   trunk/freenet/src/freenet/node/Node.java
   trunk/freenet/src/freenet/node/PeerNode.java
   trunk/freenet/src/freenet/transport/IPAddressDetector.java
Log:
New config option: includeLocalAddressesInNoderefs
If set, the node *may* include its local (LAN) IP address in the noderef.
New undocumented per-peer option: 
metadata.allowLocalAddresses=true - allows node to connect to these local IPs.
Set that in your peers file for the node in question (no UI yet), if you're on 
a LAN.
Security: Don't connect to these normally.

Modified: trunk/freenet/src/freenet/io/comm/FreenetInetAddress.java
===================================================================
--- trunk/freenet/src/freenet/io/comm/FreenetInetAddress.java   2006-07-05 
21:47:17 UTC (rev 9472)
+++ trunk/freenet/src/freenet/io/comm/FreenetInetAddress.java   2006-07-05 
23:56:06 UTC (rev 9473)
@@ -9,6 +9,7 @@

 import freenet.io.AddressIdentifier;
 import freenet.support.Logger;
+import freenet.transport.IPUtil;

 /**
  * Long-term InetAddress. If created with an IP address, then the IP address 
is primary.
@@ -254,4 +255,17 @@
                else
                        return addr;
        }
+
+       public boolean isRealInternetAddress(boolean lookup, boolean 
defaultVal) {
+               if(_address != null) {
+                       return IPUtil.checkAddress(_address);
+               } else {
+                       if(lookup) {
+                               InetAddress a = getAddress();
+                               if(a != null)
+                                       return IPUtil.checkAddress(a);
+                       }
+                       return defaultVal;      
+               }
+       }
 }

Modified: trunk/freenet/src/freenet/io/comm/Peer.java
===================================================================
--- trunk/freenet/src/freenet/io/comm/Peer.java 2006-07-05 21:47:17 UTC (rev 
9472)
+++ trunk/freenet/src/freenet/io/comm/Peer.java 2006-07-05 23:56:06 UTC (rev 
9473)
@@ -23,6 +23,7 @@
 import java.net.*;

 import freenet.io.WritableToDataOutputStream;
+import freenet.transport.IPUtil;

 /**
  * @author ian
@@ -32,8 +33,12 @@
  */
 public class Peer implements WritableToDataOutputStream {

-    public static final String VERSION = "$Id: Peer.java,v 1.4 2005/08/25 
17:28:19 amphibian Exp $";
+    public class LocalAddressException extends Exception {

+       }
+
+       public static final String VERSION = "$Id: Peer.java,v 1.4 2005/08/25 
17:28:19 amphibian Exp $";
+
     private final FreenetInetAddress addr;
        private final int _port;

@@ -147,6 +152,13 @@
                return addr.getAddress(doDNSRequest);
        }

+       public InetAddress getAddress(boolean doDNSRequest, boolean allowLocal) 
throws LocalAddressException {
+               InetAddress a = addr.getAddress(doDNSRequest);
+               if(a == null) return null;
+               if(allowLocal || IPUtil.checkAddress(a)) return a;
+               throw new LocalAddressException();
+       }
+       
        /**
         * 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
@@ -176,4 +188,8 @@
        public FreenetInetAddress getFreenetAddress() {
                return addr;
        }
+
+       public boolean isRealInternetAddress(boolean lookup, boolean 
defaultVal) {
+               return addr.isRealInternetAddress(lookup, defaultVal);
+       }
 }
\ No newline at end of file

Modified: trunk/freenet/src/freenet/io/comm/UdpSocketManager.java
===================================================================
--- trunk/freenet/src/freenet/io/comm/UdpSocketManager.java     2006-07-05 
21:47:17 UTC (rev 9472)
+++ trunk/freenet/src/freenet/io/comm/UdpSocketManager.java     2006-07-05 
23:56:06 UTC (rev 9473)
@@ -24,6 +24,7 @@

 import org.tanukisoftware.wrapper.WrapperManager;

+import freenet.io.comm.Peer.LocalAddressException;
 import freenet.node.Node;
 import freenet.node.PeerNode;
 import freenet.support.Logger;
@@ -511,11 +512,11 @@
      * @param blockToSend The data block to send.
      * @param destination The peer to send it to.
      */
-    public void sendPacket(byte[] blockToSend, Peer destination) {
+    public void sendPacket(byte[] blockToSend, Peer destination, boolean 
allowLocalAddresses) throws LocalAddressException {
                // there should be no DNS needed here, but go ahead if we can, 
but complain doing it
-               if( destination.getAddress(false) == null ) {
+               if( destination.getAddress(false, allowLocalAddresses) == null 
) {
                        Logger.error(this, "Tried sending to destination 
without pre-looked up IP address(needs a real Peer.getHostname()): null:" + 
destination.getPort(), new Exception("error"));
-                       if( destination.getAddress(true) == null ) {
+                       if( destination.getAddress(true, allowLocalAddresses) 
== null ) {
                                Logger.error(this, "Tried sending to bad 
destination address: null:" + destination.getPort(), new Exception("error"));
                                return;
                        }
@@ -527,7 +528,7 @@
                        }
                }
                DatagramPacket packet = new DatagramPacket(blockToSend, 
blockToSend.length);
-               packet.setAddress(destination.getAddress(false));
+               packet.setAddress(destination.getAddress(false, 
allowLocalAddresses));
                packet.setPort(destination.getPort());

                // TODO: keep?

Modified: trunk/freenet/src/freenet/node/FNPPacketMangler.java
===================================================================
--- trunk/freenet/src/freenet/node/FNPPacketMangler.java        2006-07-05 
21:47:17 UTC (rev 9472)
+++ trunk/freenet/src/freenet/node/FNPPacketMangler.java        2006-07-05 
23:56:06 UTC (rev 9473)
@@ -12,6 +12,7 @@
 import freenet.crypt.EntropySource;
 import freenet.crypt.PCFBMode;
 import freenet.io.comm.*;
+import freenet.io.comm.Peer.LocalAddressException;
 import freenet.support.Fields;
 import freenet.support.HexUtil;
 import freenet.support.Logger;
@@ -385,7 +386,11 @@
         data[hash.length+iv.length+1] = (byte) pcfb.encipher((byte)length);
         pcfb.blockEncipher(output, 0, output.length);
         System.arraycopy(output, 0, data, hash.length+iv.length+2, 
output.length);
-        usm.sendPacket(data, replyTo);
+        try {
+                       usm.sendPacket(data, replyTo, pn.allowLocalAddresses());
+               } catch (LocalAddressException e) {
+                       Logger.error(this, "Tried to send auth packet to local 
address: "+replyTo+" for "+pn);
+               }
         Logger.minor(this, "Sending auth packet (long) to "+replyTo+" - size 
"+data.length+" data length: "+output.length);
      }

@@ -1292,7 +1297,11 @@
         Logger.minor(this,"Sending packet of length "+output.length+" (" + 
Fields.hashCode(output) + " to "+kt.pn);

         // pn.getPeer() cannot be null
-        usm.sendPacket(output, kt.pn.getPeer());
+        try {
+                       usm.sendPacket(output, kt.pn.getPeer(), 
kt.pn.allowLocalAddresses());
+               } catch (LocalAddressException e) {
+                       Logger.error(this, "Tried to send data packet to local 
address: "+kt.pn.getPeer()+" for "+kt.pn.allowLocalAddresses());
+               }
         kt.pn.sentPacket();
     }

@@ -1332,6 +1341,7 @@
         for(int i=0;i<handshakeIPs.length;i++){
                long innerLoopTime1 = System.currentTimeMillis();
                if(handshakeIPs[i].getAddress(false) == null) continue;
+               if(!handshakeIPs[i].isRealInternetAddress(false, false)) 
continue;
                long innerLoopTime2 = System.currentTimeMillis();
                if((innerLoopTime2 - innerLoopTime1) > 500)
                        Logger.normal(this, "innerLoopTime2 is more than half a 
second after innerLoopTime1 ("+(innerLoopTime2 - innerLoopTime1)+") working on 
"+handshakeIPs[i]+" of "+pn.getName());

Modified: trunk/freenet/src/freenet/node/Node.java
===================================================================
--- trunk/freenet/src/freenet/node/Node.java    2006-07-05 21:47:17 UTC (rev 
9472)
+++ trunk/freenet/src/freenet/node/Node.java    2006-07-05 23:56:06 UTC (rev 
9473)
@@ -49,6 +49,7 @@
 import freenet.clients.http.BookmarkManager;
 import freenet.clients.http.FProxyToadlet;
 import freenet.clients.http.SimpleToadletServer;
+import freenet.config.BooleanCallback;
 import freenet.config.Config;
 import freenet.config.FilePersistentConfig;
 import freenet.config.IntCallback;
@@ -530,6 +531,8 @@
        public boolean bwlimitDelayAlertRelevant = false;
        /** nodeAveragePing PeerManagerUserAlert should happen if true */
        public boolean nodeAveragePingAlertRelevant = false;
+       /** If true, include local addresses on noderefs */
+       public boolean includeLocalAddressesInNoderefs = false;

        private final HashSet runningUIDs;

@@ -1106,8 +1109,22 @@
                        }
                }

+               // Include local IPs in noderef file

+               nodeConfig.register("includeLocalAddressesInNoderefs", false, 
sortOrder++, true, "Include local addresses in noderef", "Whether to include 
local addresses (LAN and localhost) in node references. This will not be useful 
unless the other side sets metadata.allowLocalAddresses=true for this 
reference.", new BooleanCallback() {
+
+                       public boolean get() {
+                               return includeLocalAddressesInNoderefs;
+                       }
+
+                       public void set(boolean val) throws 
InvalidConfigValueException {
+                               includeLocalAddressesInNoderefs = val;
+                       }
+                       
+               });

+               includeLocalAddressesInNoderefs = 
nodeConfig.getBoolean("includeLocalAddressesInNoderefs");
+               
                // Determine where to bind to



Modified: trunk/freenet/src/freenet/node/PeerNode.java
===================================================================
--- trunk/freenet/src/freenet/node/PeerNode.java        2006-07-05 21:47:17 UTC 
(rev 9472)
+++ trunk/freenet/src/freenet/node/PeerNode.java        2006-07-05 23:56:06 UTC 
(rev 9473)
@@ -236,6 +236,9 @@

     /** True if we don't send handshake requests to this peer, but will 
connect if we receive one */
     private boolean isListenOnly = false;
+    
+    /** True if we want to allow LAN/localhost addresses. */
+    private boolean allowLocalAddresses = false;

     /**
      * Create a PeerNode from a SimpleFieldSet containing a
@@ -437,6 +440,7 @@
                }
                isDisabled = Fields.stringToBool(metadata.get("isDisabled"), 
false);
                isListenOnly = 
Fields.stringToBool(metadata.get("isListenOnly"), false);
+               allowLocalAddresses = 
Fields.stringToBool(metadata.get("allowLocalAddresses"), false);
                }
         } else {
             neverConnected = true;
@@ -1499,6 +1503,8 @@
                fs.put("isDisabled", "true");
        if(isListenOnly)
                fs.put("isListenOnly", "true");
+       if(allowLocalAddresses)
+               fs.put("allowLocalAddresses", "true");
        return fs;
        }

@@ -2045,4 +2051,8 @@
                verifiedIncompatibleOlderVersion = invalidVersion();
                return verifiedIncompatibleOlderVersion;
        }
+
+       public boolean allowLocalAddresses() {
+               return allowLocalAddresses;
+       }
 }

Modified: trunk/freenet/src/freenet/transport/IPAddressDetector.java
===================================================================
--- trunk/freenet/src/freenet/transport/IPAddressDetector.java  2006-07-05 
21:47:17 UTC (rev 9472)
+++ trunk/freenet/src/freenet/transport/IPAddressDetector.java  2006-07-05 
23:56:06 UTC (rev 9473)
@@ -256,7 +256,7 @@
        }

        protected boolean isInternetAddress(InetAddress addr) {
-               return IPUtil.checkAddress(addr);
+               return node.includeLocalAddressesInNoderefs || 
IPUtil.checkAddress(addr);
        }

        public void run() {


Reply via email to