From 5b44bcb44f0dcfa9f687c623fae36f30c7459106 Mon Sep 17 00:00:00 2001
From: zzz <zzz@mail.i2p>
Date: Thu, 30 Apr 2009 16:50:47 +0000
Subject: [PATCH] - NTCP Port must now be either auto or configured;   now
 defaults to auto; and configured now trumps auto.   Port configuration now
 does not affect whether inbound   NTCP is enabled - the host configuration
 alone can do that.

---
 .../net/i2p/router/web/ConfigNetHandler.java  | 13 ++----
 .../net/i2p/router/web/ConfigNetHelper.java   |  6 +--
 apps/routerconsole/jsp/config.jsp             |  2 -
 .../transport/CommSystemFacadeImpl.java       | 41 ++++++++++++-------
 4 files changed, 32 insertions(+), 30 deletions(-)

diff --git a/apps/routerconsole/java/src/net/i2p/router/web/ConfigNetHandler.java b/apps/routerconsole/java/src/net/i2p/router/web/ConfigNetHandler.java
index 26e03feff8..d3c52a53ff 100644
--- a/apps/routerconsole/java/src/net/i2p/router/web/ConfigNetHandler.java
+++ b/apps/routerconsole/java/src/net/i2p/router/web/ConfigNetHandler.java
@@ -66,8 +66,6 @@ public class ConfigNetHandler extends FormHandler {
     }
     public void setNtcpAutoPort(String mode) {
         _ntcpAutoPort = mode.equals("2");
-        if (mode.equals("0"))
-            _ntcpInboundDisabled = true;
     }
     public void setUpnp(String moo) { _upnp = true; }
     
@@ -155,19 +153,14 @@ public class ConfigNetHandler extends FormHandler {
                 restartRequired = true;
             }
             if (oldAutoPort != _ntcpAutoPort || ! oldNPort.equals(_ntcpPort)) {
-                if ( _ntcpAutoPort ) {
-                    _context.router().setConfigSetting(ConfigNetHelper.PROP_I2NP_NTCP_AUTO_PORT, "true");
-                    _context.router().removeConfigSetting(ConfigNetHelper.PROP_I2NP_NTCP_PORT);
-                    addFormNotice("Updating inbound TCP port to auto");
-                } else if (_ntcpPort.length() > 0) {
+                if (_ntcpPort.length() > 0 && !_ntcpAutoPort) {
                     _context.router().setConfigSetting(ConfigNetHelper.PROP_I2NP_NTCP_PORT, _ntcpPort);
-                    _context.router().removeConfigSetting(ConfigNetHelper.PROP_I2NP_NTCP_AUTO_PORT);
                     addFormNotice("Updating inbound TCP port to " + _ntcpPort);
                 } else {
                     _context.router().removeConfigSetting(ConfigNetHelper.PROP_I2NP_NTCP_PORT);
-                    _context.router().removeConfigSetting(ConfigNetHelper.PROP_I2NP_NTCP_AUTO_PORT);
-                    addFormNotice("Disabling inbound TCP");
+                    addFormNotice("Updating inbound TCP port to auto");
                 }
+                _context.router().setConfigSetting(ConfigNetHelper.PROP_I2NP_NTCP_AUTO_PORT, "" + _ntcpAutoPort);
                 restartRequired = true;
             }
 
diff --git a/apps/routerconsole/java/src/net/i2p/router/web/ConfigNetHelper.java b/apps/routerconsole/java/src/net/i2p/router/web/ConfigNetHelper.java
index 2c90b805cf..95a370efc7 100644
--- a/apps/routerconsole/java/src/net/i2p/router/web/ConfigNetHelper.java
+++ b/apps/routerconsole/java/src/net/i2p/router/web/ConfigNetHelper.java
@@ -103,10 +103,8 @@ public class ConfigNetHelper extends HelperBase {
             return DISABLED;
         String port = _context.getProperty(PROP_I2NP_NTCP_PORT); 
         boolean specified = port != null && port.length() > 0;
-        boolean auto = Boolean.valueOf(_context.getProperty(PROP_I2NP_NTCP_AUTO_PORT)).booleanValue();
-        if ((mode == 0 && (!specified) && !auto) ||
-            (mode == 1 && specified && !auto) ||
-            (mode == 2 && auto))
+        if ((mode == 1 && specified) ||
+            (mode == 2 && !specified))
             return CHECKED;
         return "";
     }
diff --git a/apps/routerconsole/jsp/config.jsp b/apps/routerconsole/jsp/config.jsp
index 5eb809ed5f..298414e222 100644
--- a/apps/routerconsole/jsp/config.jsp
+++ b/apps/routerconsole/jsp/config.jsp
@@ -125,8 +125,6 @@
     <i>(dyndns and the like are fine)</i><br />
  <p>
  Externally reachable TCP port:<br />
-    <input type="radio" name="ntcpAutoPort" value="0" <%=nethelper.getTcpAutoPortChecked(0) %> />
-    Disable<br />
     <input type="radio" name="ntcpAutoPort" value="2" <%=nethelper.getTcpAutoPortChecked(2) %> />
     Use the same port configured for SSU
     <i>(currently <jsp:getProperty name="nethelper" property="udpPort" />)</i><br />
diff --git a/router/java/src/net/i2p/router/transport/CommSystemFacadeImpl.java b/router/java/src/net/i2p/router/transport/CommSystemFacadeImpl.java
index 9060ede84c..fa62fdc317 100644
--- a/router/java/src/net/i2p/router/transport/CommSystemFacadeImpl.java
+++ b/router/java/src/net/i2p/router/transport/CommSystemFacadeImpl.java
@@ -193,6 +193,9 @@ public class CommSystemFacadeImpl extends CommSystemFacade {
     public final static String PROP_I2NP_NTCP_AUTO_PORT = "i2np.ntcp.autoport";
     public final static String PROP_I2NP_NTCP_AUTO_IP = "i2np.ntcp.autoip";
     
+    /**
+     * This should really be moved to ntcp/NTCPTransport.java, why is it here?
+     */
     public static RouterAddress createNTCPAddress(RouterContext ctx) {
         if (!TransportManager.enableNTCP(ctx)) return null;
         RouterAddress addr = new RouterAddress();
@@ -236,6 +239,7 @@ public class CommSystemFacadeImpl extends CommSystemFacade {
 
     /**
      * UDP changed addresses, tell NTCP and restart
+     * This should really be moved to ntcp/NTCPTransport.java, why is it here?
      */
     @Override
     public void notifyReplaceAddress(RouterAddress UDPAddr) {
@@ -249,7 +253,8 @@ public class CommSystemFacadeImpl extends CommSystemFacade {
             return;
         Properties newProps;
         RouterAddress oldAddr = t.getCurrentAddress();
-        //_log.warn("Changing NTCP Address? was " + oldAddr);
+        if (_log.shouldLog(Log.INFO))
+            _log.info("Changing NTCP Address? was " + oldAddr);
         RouterAddress newAddr = oldAddr;
         if (newAddr == null) {
             newAddr = new RouterAddress();
@@ -264,23 +269,27 @@ public class CommSystemFacadeImpl extends CommSystemFacade {
         }
 
         boolean changed = false;
+        // old behavior (<= 0.7.3): auto-port defaults to false, and true trumps explicit setting
+        // new behavior (>= 0.7.4): auto-port defaults to true, but explicit setting trumps auto
         String oport = newProps.getProperty(NTCPAddress.PROP_PORT);
-        String enabled = _context.getProperty(PROP_I2NP_NTCP_AUTO_PORT, "false");
-        if ( (enabled != null) && ("true".equalsIgnoreCase(enabled)) ) {
-            String nport = UDPProps.getProperty(UDPAddress.PROP_PORT);
-            if (nport == null || nport.length() <= 0)
-                return;
-            if (oport == null || ! oport.equals(nport)) {
-                newProps.setProperty(NTCPAddress.PROP_PORT, nport);
-                changed = true;
-            }
-        } else if (oport == null || oport.length() <= 0) {
+        String nport = null;
+        String cport = _context.getProperty(PROP_I2NP_NTCP_PORT);
+        if (cport != null && cport.length() > 0) {
+            nport = cport;
+        } else if (Boolean.valueOf(_context.getProperty(PROP_I2NP_NTCP_AUTO_PORT, "true")).booleanValue()) {
+            nport = UDPProps.getProperty(UDPAddress.PROP_PORT);
+        }
+        if (_log.shouldLog(Log.INFO))
+            _log.info("old: " + oport + " config: " + cport + " new: " + nport);
+        if (nport == null || nport.length() <= 0)
             return;
+        if (oport == null || ! oport.equals(nport)) {
+            newProps.setProperty(NTCPAddress.PROP_PORT, nport);
+            changed = true;
         }
 
         String ohost = newProps.getProperty(NTCPAddress.PROP_HOST);
-        enabled = _context.getProperty(PROP_I2NP_NTCP_AUTO_IP, "false");
-        if ( (enabled != null) && ("true".equalsIgnoreCase(enabled)) ) {
+        if (Boolean.valueOf(_context.getProperty(PROP_I2NP_NTCP_AUTO_IP)).booleanValue()) {
             String nhost = UDPProps.getProperty(UDPAddress.PROP_HOST);
             if (nhost == null || nhost.length() <= 0)
                 return;
@@ -293,12 +302,16 @@ public class CommSystemFacadeImpl extends CommSystemFacade {
         }
 
         if (!changed) {
-            //_log.warn("No change to NTCP Address");
+            _log.warn("No change to NTCP Address");
             return;
         }
 
         // stopListening stops the pumper, readers, and writers, so required even if
         // oldAddr == null since startListening starts them all again
+        //
+        // really need to fix this so that we can change or create an inbound address
+        // without tearing down everything
+        //
         _log.warn("Halting NTCP to change address");
         t.stopListening();
         newAddr.setOptions(newProps);
-- 
GitLab