From cf780e296e9ebff92d27a6e4cc836e8615faf38f Mon Sep 17 00:00:00 2001
From: jrandom <jrandom>
Date: Mon, 27 Sep 2004 18:05:28 +0000
Subject: [PATCH] bugfixes for autodetection/update of IP address

---
 .../transport/CommSystemFacadeImpl.java       |  5 ++++-
 .../router/transport/TransportManager.java    | 22 +++++++++++++------
 .../transport/tcp/ConnectionBuilder.java      |  2 +-
 .../transport/tcp/ConnectionHandler.java      |  3 +++
 .../i2p/router/transport/tcp/TCPAddress.java  |  6 ++++-
 .../i2p/router/transport/tcp/TCPListener.java | 16 ++++++++++++--
 .../router/transport/tcp/TCPTransport.java    | 16 +++++++++-----
 7 files changed, 53 insertions(+), 17 deletions(-)

diff --git a/router/java/src/net/i2p/router/transport/CommSystemFacadeImpl.java b/router/java/src/net/i2p/router/transport/CommSystemFacadeImpl.java
index 7b73be4e3b..51a5bb989e 100644
--- a/router/java/src/net/i2p/router/transport/CommSystemFacadeImpl.java
+++ b/router/java/src/net/i2p/router/transport/CommSystemFacadeImpl.java
@@ -75,6 +75,9 @@ public class CommSystemFacadeImpl extends CommSystemFacade {
         RouterAddress addr = createTCPAddress();
         if (addr != null)
             addresses.add(addr);
+        
+        addresses.addAll(_manager.getAddresses());
+        
         if (_log.shouldLog(Log.INFO))
             _log.info("Creating addresses: " + addresses);
         return addresses;
@@ -91,7 +94,7 @@ public class CommSystemFacadeImpl extends CommSystemFacade {
         String name = _context.router().getConfigSetting(PROP_I2NP_TCP_HOSTNAME);
         String port = _context.router().getConfigSetting(PROP_I2NP_TCP_PORT);
         if ( (name == null) || (port == null) ) {
-            _log.info("TCP Host/Port not specified in config file - skipping TCP transport");
+            //_log.info("TCP Host/Port not specified in config file - skipping TCP transport");
             return null;
         } else {
             _log.info("Creating TCP address on " + name + ":" + port);
diff --git a/router/java/src/net/i2p/router/transport/TransportManager.java b/router/java/src/net/i2p/router/transport/TransportManager.java
index e81a1f6f01..35beef902a 100644
--- a/router/java/src/net/i2p/router/transport/TransportManager.java
+++ b/router/java/src/net/i2p/router/transport/TransportManager.java
@@ -36,7 +36,6 @@ import net.i2p.util.Log;
 public class TransportManager implements TransportEventListener {
     private Log _log;
     private List _transports;
-    private List _addresses;
     private RouterContext _context;
 
     private final static String PROP_DISABLE_TCP = "i2np.tcp.disable";
@@ -45,7 +44,6 @@ public class TransportManager implements TransportEventListener {
         _context = context;
         _log = _context.logManager().getLog(TransportManager.class);
         _transports = new ArrayList();
-        _addresses = new ArrayList();
     }
     
     public void addTransport(Transport transport) {
@@ -77,7 +75,6 @@ public class TransportManager implements TransportEventListener {
         for (int i = 0; i < _transports.size(); i++) {
             Transport t = (Transport)_transports.get(i);
             RouterAddress addr = t.startListening();
-            if (addr != null) _addresses.add(addr);
             _log.debug("Transport " + i + " (" + t.getStyle() + ") started");
         }
         _log.debug("Done start listening on transports");
@@ -94,7 +91,6 @@ public class TransportManager implements TransportEventListener {
             ((Transport)_transports.get(i)).stopListening();
         }
         _transports.clear();
-        _addresses.clear();
     }
     
     private boolean isSupported(Set addresses, Transport t) {
@@ -114,6 +110,15 @@ public class TransportManager implements TransportEventListener {
         return peers;
     }
     
+    List getAddresses() {
+        List rv = new ArrayList(_transports.size());
+        for (int i = 0; i < _transports.size(); i++) {
+            Transport t = (Transport)_transports.get(i);
+            rv.addAll(t.getCurrentAddresses());
+        }
+        return rv;
+    }
+    
     public List getBids(OutNetMessage msg) {
         if (msg == null)
             throw new IllegalArgumentException("Null message?  no bidding on a null outNetMessage!");
@@ -264,9 +269,12 @@ public class TransportManager implements TransportEventListener {
         StringBuffer buf = new StringBuffer(8*1024);
         buf.append("<h2>Transport Manager</h2>\n");
         buf.append("Listening on: <br /><pre>\n");
-        for (Iterator iter = _addresses.iterator(); iter.hasNext(); ) {
-            RouterAddress addr = (RouterAddress)iter.next();
-            buf.append(addr.toString()).append("\n\n");
+        for (int i = 0; i < _transports.size(); i++) {
+            Transport t = (Transport)_transports.get(i);
+            for (Iterator iter = t.getCurrentAddresses().iterator(); iter.hasNext(); ) {
+                RouterAddress addr = (RouterAddress)iter.next();
+                buf.append(addr.toString()).append("\n\n");
+            }   
         }
         buf.append("</pre>\n");
         for (Iterator iter = _transports.iterator(); iter.hasNext(); ) {
diff --git a/router/java/src/net/i2p/router/transport/tcp/ConnectionBuilder.java b/router/java/src/net/i2p/router/transport/tcp/ConnectionBuilder.java
index 9aecad5441..9a2bfa89d6 100644
--- a/router/java/src/net/i2p/router/transport/tcp/ConnectionBuilder.java
+++ b/router/java/src/net/i2p/router/transport/tcp/ConnectionBuilder.java
@@ -610,7 +610,7 @@ public class ConnectionBuilder {
             case 1: // not reachable
                 fail("According to " 
                      + _target.getIdentity().calculateHash().toBase64().substring(0,6)
-                     + ", we are not reachable on " + _localIP);
+                     + ", we are not reachable on " + _localIP + ":" + _transport.getPort());
                 return false;
             case 2: // clock skew
                 fail("According to " 
diff --git a/router/java/src/net/i2p/router/transport/tcp/ConnectionHandler.java b/router/java/src/net/i2p/router/transport/tcp/ConnectionHandler.java
index 1485317d31..1593b6dda0 100644
--- a/router/java/src/net/i2p/router/transport/tcp/ConnectionHandler.java
+++ b/router/java/src/net/i2p/router/transport/tcp/ConnectionHandler.java
@@ -639,6 +639,9 @@ public class ConnectionHandler {
     private boolean verifyReachability() { 
         if (_actualPeer == null) return false;
         _remoteAddress = new TCPAddress(_actualPeer.getTargetAddress(TCPTransport.STYLE));
+        if (!_transport.allowAddress(_remoteAddress))
+            return false;
+        
         //if (true) return true;
         Socket s = null;
         try {
diff --git a/router/java/src/net/i2p/router/transport/tcp/TCPAddress.java b/router/java/src/net/i2p/router/transport/tcp/TCPAddress.java
index ee9d4a17e3..a71c420c02 100644
--- a/router/java/src/net/i2p/router/transport/tcp/TCPAddress.java
+++ b/router/java/src/net/i2p/router/transport/tcp/TCPAddress.java
@@ -92,6 +92,9 @@ public class TCPAddress {
         
         RouterAddress addr = new RouterAddress();
         
+        addr.setCost(10);
+        addr.setExpiration(null);
+        
         Properties props = new Properties();
         props.setProperty(PROP_HOST, _host);
         props.setProperty(PROP_PORT, ""+_port);
@@ -141,7 +144,8 @@ public class TCPAddress {
     public boolean equals(Object val) {
         if ( (val != null) && (val instanceof TCPAddress) ) {
             TCPAddress addr = (TCPAddress)val;
-            if ( (_addr != null) && (_addr.getHostAddress() != null) ) {
+            if ( (_addr != null) && (_addr.getHostAddress() != null) 
+                 && (addr.getAddress() != null) && (addr.getAddress().getHostAddress() != null) ) {
                 return DataHelper.eq(getAddress().getHostAddress(), addr.getAddress().getHostAddress())
                        && (getPort() == addr.getPort());
             } else {
diff --git a/router/java/src/net/i2p/router/transport/tcp/TCPListener.java b/router/java/src/net/i2p/router/transport/tcp/TCPListener.java
index 61ac9117d5..9487f1eb6e 100644
--- a/router/java/src/net/i2p/router/transport/tcp/TCPListener.java
+++ b/router/java/src/net/i2p/router/transport/tcp/TCPListener.java
@@ -63,7 +63,14 @@ class TCPListener {
         
     public void startListening() {
         TCPAddress addr = _transport.getMyAddress();
-        if (addr != null) {
+        if ( (addr != null) && (addr.getHost() != null) && (addr.getPort() > 0) ) {
+            if (_listener != null) {
+                if (_log.shouldLog(Log.WARN))
+                    _log.warn("Not starting another listener on " + addr 
+                              + " while already listening on " + _listener.getMyAddress());
+                return;
+            }
+            
             _listener = new ListenerRunner(addr);
             Thread t = new I2PThread(_listener, "Listener [" + addr.getPort()+"]");
             t.setDaemon(true);
@@ -80,7 +87,9 @@ class TCPListener {
     }
     
     public void stopListening() {
-        _listener.stopListening();
+        if (_listener != null)
+            _listener.stopListening();
+        
         for (int i = 0; i < _handlers.size(); i++) {
             SocketHandler h = (SocketHandler)_handlers.get(i);
             h.stopHandling();
@@ -93,6 +102,7 @@ class TCPListener {
                 _socket = null;
             } catch (IOException ioe) {}
         }
+        _listener = null;
     }
     
     private InetAddress getInetAddress(String host) {
@@ -119,6 +129,8 @@ class TCPListener {
         }
         public void stopListening() { _isRunning = false; }
         
+        public TCPAddress getMyAddress() { return _myAddress; }
+        
         public void run() {
             if (_log.shouldLog(Log.INFO))
                 _log.info("Beginning TCP listener");
diff --git a/router/java/src/net/i2p/router/transport/tcp/TCPTransport.java b/router/java/src/net/i2p/router/transport/tcp/TCPTransport.java
index ae1467f5b3..9aff2a2a7a 100644
--- a/router/java/src/net/i2p/router/transport/tcp/TCPTransport.java
+++ b/router/java/src/net/i2p/router/transport/tcp/TCPTransport.java
@@ -270,7 +270,7 @@ public class TCPTransport extends TransportImpl {
      *
      * @param address address that the remote host said was ours
      */
-    void ourAddressReceived(String address) {
+    synchronized void ourAddressReceived(String address) {
         if (allowAddressUpdate()) {
             int port = getPort();
             TCPAddress addr = new TCPAddress(address, port);
@@ -282,6 +282,8 @@ public class TCPTransport extends TransportImpl {
                             return;
                         }
                     }
+                    if (_log.shouldLog(Log.INFO))
+                        _log.info("Update our local address to " + address);
                     updateAddress(addr);
                 }
             } else {
@@ -361,10 +363,10 @@ public class TCPTransport extends TransportImpl {
     }
     
     /**
-     * Is the given address a valid one that we could listen to? 
+     * Is the given address a valid one that we could listen to or contact? 
      *
      */
-    private boolean allowAddress(TCPAddress address) {
+    boolean allowAddress(TCPAddress address) {
         if (address == null) return false;
         if ( (address.getPort() <= 0) || (address.getPort() > 65535) )
             return false;
@@ -391,7 +393,6 @@ public class TCPTransport extends TransportImpl {
         RouterAddress routerAddr = addr.toRouterAddress();
         _myAddress = addr;
         _listener.stopListening();
-        _listener.startListening();
         
         Set addresses = getCurrentAddresses();
         List toRemove = null;
@@ -411,6 +412,11 @@ public class TCPTransport extends TransportImpl {
         addresses.add(routerAddr);
         
         _context.router().rebuildRouterInfo();
+      
+        if (_log.shouldLog(Log.INFO))
+            _log.info("Updating our local address to include " + addr.toString() 
+                      + " and modified our routerInfo to have: " 
+                      + _context.router().getRouterInfo().getAddresses());
         
         _listener.startListening();
     }
@@ -435,7 +441,7 @@ public class TCPTransport extends TransportImpl {
      *
      * @return the port number, or -1 if there is no valid port
      */
-    private int getPort() {
+    int getPort() {
         if ( (_myAddress != null) && (_myAddress.getPort() > 0) )
             return _myAddress.getPort();
         
-- 
GitLab