Changeset 3a49d6d


Ignore:
Timestamp:
May 9, 2013 4:18:58 PM (8 years ago)
Author:
zzz <zzz@…>
Branches:
master
Children:
c76c800
Parents:
94e34ff
Message:
  • NTCP: Move SSU address notification handling from CSFI to NTCPTransport
Location:
router/java/src/net/i2p/router/transport
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • router/java/src/net/i2p/router/transport/CommSystemFacadeImpl.java

    r94e34ff r3a49d6d  
    195195        //    return Collections.EMPTY_SET;
    196196        List<RouterAddress> addresses = new ArrayList(_manager.getAddresses());
    197 
    198         Transport ntcp = _manager.getTransport(NTCPTransport.STYLE);
    199         boolean hasNTCP = ntcp != null && ntcp.hasCurrentAddress();
    200 
    201         boolean newCreated = false;
    202         if (!hasNTCP) {
    203             RouterAddress addr = createNTCPAddress(_context);
    204             if (_log.shouldLog(Log.INFO))
    205                 _log.info("NTCP address: " + addr);
    206             if (addr != null) {
    207                 addresses.add(addr);
    208                 newCreated = true;
    209             }
    210         }
    211        
    212197        if (_log.shouldLog(Log.INFO))
    213             _log.info("Creating addresses: " + addresses + " isNew? " + newCreated, new Exception("creator"));
     198            _log.info("Creating addresses: " + addresses, new Exception("creator"));
    214199        return addresses;
    215200    }
    216201   
    217     public final static String PROP_I2NP_NTCP_HOSTNAME = "i2np.ntcp.hostname";
    218     public final static String PROP_I2NP_NTCP_PORT = "i2np.ntcp.port";
    219     public final static String PROP_I2NP_NTCP_AUTO_PORT = "i2np.ntcp.autoport";
    220     public final static String PROP_I2NP_NTCP_AUTO_IP = "i2np.ntcp.autoip";
    221    
    222     /**
    223      * This only creates an address if the hostname AND port are set in router.config,
    224      * which should be rare.
    225      * Otherwise, notifyReplaceAddress() below takes care of it.
    226      * Note this is called both from above and from NTCPTransport.startListening()
     202    /**
     203     * UDP changed addresses, tell NTCP and restart
    227204     *
    228      * This should really be moved to ntcp/NTCPTransport.java, why is it here?
    229      */
    230     public static RouterAddress createNTCPAddress(RouterContext ctx) {
    231         if (!TransportManager.isNTCPEnabled(ctx)) return null;
    232         String name = ctx.router().getConfigSetting(PROP_I2NP_NTCP_HOSTNAME);
    233         String port = ctx.router().getConfigSetting(PROP_I2NP_NTCP_PORT);
    234         /*
    235         boolean isNew = false;
    236         if (name == null) {
    237             name = "localhost";
    238             isNew = true;
    239         }
    240         if (port == null) {
    241             port = String.valueOf(ctx.random().nextInt(10240)+1024);
    242             isNew = true;
    243         }
    244          */
    245         if ( (name == null) || (port == null) || (name.trim().length() <= 0) || ("null".equals(name)) )
    246             return null;
    247         try {
    248             int p = Integer.parseInt(port);
    249             if ( (p <= 0) || (p > 64*1024) )
    250                 return null;
    251         } catch (NumberFormatException nfe) {
    252             return null;
    253         }
    254         Properties props = new Properties();
    255         props.setProperty(NTCPAddress.PROP_HOST, name);
    256         props.setProperty(NTCPAddress.PROP_PORT, port);
    257         RouterAddress addr = new RouterAddress();
    258         addr.setCost(NTCPAddress.DEFAULT_COST);
    259         //addr.setExpiration(null);
    260         addr.setOptions(props);
    261         addr.setTransportStyle(NTCPTransport.STYLE);
    262         //if (isNew) {
    263             // why save the same thing?
    264             Map<String, String> changes = new HashMap();
    265             changes.put(PROP_I2NP_NTCP_HOSTNAME, name);
    266             changes.put(PROP_I2NP_NTCP_PORT, port);
    267             ctx.router().saveConfig(changes, null);
    268         //}
    269         return addr;
    270     }
    271 
    272     /**
    273      * UDP changed addresses, tell NTCP and restart
    274      * This should really be moved to ntcp/NTCPTransport.java, why is it here?
    275      */
    276     @Override
    277     public synchronized void notifyReplaceAddress(RouterAddress udpAddr) {
    278         if (udpAddr == null)
    279             return;
    280         NTCPTransport t = (NTCPTransport) _manager.getTransport(NTCPTransport.STYLE);
    281         if (t == null)
    282             return;
    283 
    284         //////// FIXME just take first IPv4 address for now
    285         List<RouterAddress> oldAddrs = t.getCurrentAddresses();
    286         RouterAddress oldAddr = null;
    287         for (RouterAddress ra : oldAddrs) {
    288             byte[] ipx = ra.getIP();
    289             if (ipx != null && ipx.length == 4) {
    290                 oldAddr = ra;
    291                 break;
    292             }
    293         }
    294         if (_log.shouldLog(Log.INFO))
    295             _log.info("Changing NTCP Address? was " + oldAddr);
    296 
    297         RouterAddress newAddr = new RouterAddress();
    298         newAddr.setTransportStyle(NTCPTransport.STYLE);
    299         Properties newProps = new Properties();
    300         if (oldAddr == null) {
    301             newAddr.setCost(NTCPAddress.DEFAULT_COST);
    302         } else {
    303             newAddr.setCost(oldAddr.getCost());
    304             newProps.putAll(oldAddr.getOptionsMap());
    305         }
    306 
    307         boolean changed = false;
    308 
    309         // Auto Port Setting
    310         // old behavior (<= 0.7.3): auto-port defaults to false, and true trumps explicit setting
    311         // new behavior (>= 0.7.4): auto-port defaults to true, but explicit setting trumps auto
    312         // TODO rewrite this to operate on ints instead of strings
    313         String oport = newProps.getProperty(NTCPAddress.PROP_PORT);
    314         String nport = null;
    315         String cport = _context.getProperty(PROP_I2NP_NTCP_PORT);
    316         if (cport != null && cport.length() > 0) {
    317             nport = cport;
    318         } else if (_context.getBooleanPropertyDefaultTrue(PROP_I2NP_NTCP_AUTO_PORT)) {
    319             // 0.9.6 change
    320             // This wasn't quite right, as udpAddr is the EXTERNAL port and we really
    321             // want NTCP to bind to the INTERNAL port the first time,
    322             // because if they are different, the NAT is changing them, and
    323             // it probably isn't mapping UDP and TCP the same.
     205     * All the work moved to NTCPTransport.externalAddressReceived()
     206     */
     207    @Override
     208    public void notifyReplaceAddress(RouterAddress udpAddr) {
     209        byte[] ip = udpAddr != null ? udpAddr.getIP() : null;
     210        int port = udpAddr != null ? udpAddr.getPort() : 0;
     211        if (port < 0) {
    324212            Transport udp = _manager.getTransport(UDPTransport.STYLE);
    325             if (udp != null) {
    326                 int udpIntPort = udp.getRequestedPort();
    327                 if (udpIntPort > 0)
    328                     // should always be true
    329                     nport = Integer.toString(udpIntPort);
    330             }
    331             if (nport == null)
    332                 // fallback
    333                 nport = udpAddr.getOption(UDPAddress.PROP_PORT);
    334         }
    335         if (_log.shouldLog(Log.INFO))
    336             _log.info("old: " + oport + " config: " + cport + " new: " + nport);
    337         if (nport == null || nport.length() <= 0)
    338             return;
    339         // 0.9.6 change
    340         // Don't have NTCP "chase" SSU's external port,
    341         // as it may change, possibly frequently.
    342         //if (oport == null || ! oport.equals(nport)) {
    343         if (oport == null) {
    344             newProps.setProperty(NTCPAddress.PROP_PORT, nport);
    345             changed = true;
    346         }
    347 
    348         // Auto IP Setting
    349         // old behavior (<= 0.7.3): auto-ip defaults to false, and trumps configured hostname,
    350         //                          and ignores reachability status - leading to
    351         //                          "firewalled with inbound TCP enabled" warnings.
    352         // new behavior (>= 0.7.4): auto-ip defaults to true, and explicit setting trumps auto,
    353         //                          and only takes effect if reachability is OK.
    354         //                          And new "always" setting ignores reachability status, like
    355         //                          "true" was in 0.7.3
    356         String ohost = newProps.getProperty(NTCPAddress.PROP_HOST);
    357         String enabled = _context.getProperty(PROP_I2NP_NTCP_AUTO_IP, "true").toLowerCase(Locale.US);
    358         String name = _context.getProperty(PROP_I2NP_NTCP_HOSTNAME);
    359         // hostname config trumps auto config
    360         if (name != null && name.length() > 0)
    361             enabled = "false";
    362         Transport udp = _manager.getTransport(UDPTransport.STYLE);
    363         short status = STATUS_UNKNOWN;
    364         if (udp != null)
    365             status = udp.getReachabilityStatus();
    366         if (_log.shouldLog(Log.INFO))
    367             _log.info("old: " + ohost + " config: " + name + " auto: " + enabled + " status: " + status);
    368         if (enabled.equals("always") ||
    369             (Boolean.parseBoolean(enabled) && status == STATUS_OK)) {
    370             String nhost = udpAddr.getOption(UDPAddress.PROP_HOST);
    371             if (_log.shouldLog(Log.INFO))
    372                 _log.info("old: " + ohost + " config: " + name + " new: " + nhost);
    373             if (nhost == null || nhost.length() <= 0)
    374                 return;
    375             if (ohost == null || ! ohost.equalsIgnoreCase(nhost)) {
    376                 newProps.setProperty(NTCPAddress.PROP_HOST, nhost);
    377                 changed = true;
    378             }
    379         } else if (enabled.equals("false") &&
    380                    name != null && name.length() > 0 &&
    381                    !name.equals(ohost) &&
    382                    nport != null) {
    383             // Host name is configured, and we have a port (either auto or configured)
    384             // but we probably only get here if the port is auto,
    385             // otherwise createNTCPAddress() would have done it already
    386             if (_log.shouldLog(Log.INFO))
    387                 _log.info("old: " + ohost + " config: " + name + " new: " + name);
    388             newProps.setProperty(NTCPAddress.PROP_HOST, name);
    389             changed = true;
    390         } else if (ohost == null || ohost.length() <= 0) {
    391             return;
    392         } else if (Boolean.parseBoolean(enabled) && status != STATUS_OK) {
    393             // UDP transitioned to not-OK, turn off NTCP address
    394             // This will commonly happen at startup if we were initially OK
    395             // because UPnP was successful, but a subsequent SSU Peer Test determines
    396             // we are still firewalled (SW firewall, bad UPnP indication, etc.)
    397             if (_log.shouldLog(Log.INFO))
    398                 _log.info("old: " + ohost + " config: " + name + " new: null");
    399             newAddr = null;
    400             changed = true;
    401         }
    402 
    403         if (!changed) {
    404             if (oldAddr != null) {
    405                 int oldCost = oldAddr.getCost();
    406                 int newCost = NTCPAddress.DEFAULT_COST;
    407                 if (TransportImpl.ADJUST_COST && !t.haveCapacity())
    408                     newCost++;
    409                 if (newCost != oldCost) {
    410                     oldAddr.setCost(newCost);
    411                     if (_log.shouldLog(Log.WARN))
    412                         _log.warn("Changing NTCP cost from " + oldCost + " to " + newCost);
    413                 } else {
    414                     _log.info("No change to NTCP Address");
    415                 }
    416             } else {
    417                 _log.info("No change to NTCP Address");
    418             }
    419             return;
    420         }
    421 
    422         // stopListening stops the pumper, readers, and writers, so required even if
    423         // oldAddr == null since startListening starts them all again
    424         //
    425         // really need to fix this so that we can change or create an inbound address
    426         // without tearing down everything
    427         // Especially on disabling the address, we shouldn't tear everything down.
    428         //
    429         _log.warn("Halting NTCP to change address");
    430         t.stopListening();
    431         if (newAddr != null)
    432             newAddr.setOptions(newProps);
    433         // Wait for NTCP Pumper to stop so we don't end up with two...
    434         while (t.isAlive()) {
    435             try { Thread.sleep(5*1000); } catch (InterruptedException ie) {}
    436         }
    437         t.restartListening(newAddr);
    438         _log.warn("Changed NTCP Address and started up, address is now " + newAddr);
    439         return;         
     213            if (udp != null)
     214                port = udp.getRequestedPort();
     215        }
     216        _manager.externalAddressReceived(Transport.AddressSource.SOURCE_SSU, ip, port);
    440217    }
    441218   
  • router/java/src/net/i2p/router/transport/Transport.java

    r94e34ff r3a49d6d  
    6464        /** unused */
    6565        SOURCE_CONFIG("config"),
    66         /** unused */
    67         SOURCE_SSU_PEER("ssu");
     66        SOURCE_SSU("ssu");
    6867
    6968        private final String cfgstr;
  • router/java/src/net/i2p/router/transport/TransportImpl.java

    r94e34ff r3a49d6d  
    553553     *
    554554     *  @param source defined in Transport.java
    555      *  @param ip typ. IPv4 or IPv6 non-local
     555     *  @param ip typ. IPv4 or IPv6 non-local; may be null to indicate IPv4 failure or port info only
    556556     *  @param port 0 for unknown or unchanged
    557557     */
  • router/java/src/net/i2p/router/transport/TransportManager.java

    r94e34ff r3a49d6d  
    3131import net.i2p.router.OutNetMessage;
    3232import net.i2p.router.RouterContext;
    33 import static net.i2p.router.transport.Transport.AddressSource.SOURCE_INTERFACE;
     33import static net.i2p.router.transport.Transport.AddressSource.*;
    3434import net.i2p.router.transport.crypto.DHSessionKeyBuilder;
    3535import net.i2p.router.transport.ntcp.NTCPTransport;
     
    121121
    122122    /**
    123      * callback from UPnP
     123     * callback from UPnP or SSU
    124124     * Only tell SSU, it will tell NTCP
    125125     *
    126126     */
    127127    public void externalAddressReceived(Transport.AddressSource source, byte[] ip, int port) {
    128         Transport t = getTransport(UDPTransport.STYLE);
    129         if (t != null)
    130             t.externalAddressReceived(source, ip, port);
     128        for (Transport t : _transports.values()) {
     129            // don't loop
     130            if (!(source == SOURCE_SSU && t.getStyle().equals(UDPTransport.STYLE)))
     131                t.externalAddressReceived(source, ip, port);
     132        }
    131133    }
    132134
     
    386388            // Use UDP port for NTCP too - see comment in NTCPTransport.getRequestedPort() for why this is here
    387389            if (t.getStyle().equals(NTCPTransport.STYLE) && port <= 0 &&
    388                 _context.getBooleanProperty(CommSystemFacadeImpl.PROP_I2NP_NTCP_AUTO_PORT)) {
     390                _context.getBooleanProperty(NTCPTransport.PROP_I2NP_NTCP_AUTO_PORT)) {
    389391                Transport udp = getTransport(UDPTransport.STYLE);
    390392                if (udp != null)
  • router/java/src/net/i2p/router/transport/ntcp/NTCPTransport.java

    r94e34ff r3a49d6d  
    3131import net.i2p.router.transport.CommSystemFacadeImpl;
    3232import net.i2p.router.transport.Transport;
     33import static net.i2p.router.transport.Transport.AddressSource.*;
    3334import net.i2p.router.transport.TransportBid;
    3435import net.i2p.router.transport.TransportImpl;
    3536import net.i2p.router.transport.crypto.DHSessionKeyBuilder;
     37import net.i2p.router.transport.udp.UDPTransport;
    3638import net.i2p.util.Addresses;
    3739import net.i2p.util.ConcurrentHashSet;
    3840import net.i2p.util.Log;
     41import net.i2p.util.OrderedProperties;
    3942import net.i2p.util.Translate;
    4043
     
    6366    private final Set<NTCPConnection> _establishing;
    6467
     68    public final static String PROP_I2NP_NTCP_HOSTNAME = "i2np.ntcp.hostname";
     69    public final static String PROP_I2NP_NTCP_PORT = "i2np.ntcp.port";
     70    public final static String PROP_I2NP_NTCP_AUTO_PORT = "i2np.ntcp.autoport";
     71    public final static String PROP_I2NP_NTCP_AUTO_IP = "i2np.ntcp.autoip";
     72   
    6573    /** this is rarely if ever used, default is to bind to wildcard address */
    6674    public static final String PROP_BIND_INTERFACE = "i2np.ntcp.bindInterface";
     
    472480
    473481    /**
    474      *  Only called by CSFI.
     482     *  Only called by externalAddressReceived().
    475483     *  Caller should stop the transport first, then
    476484     *  verify stopped with isAlive()
    477485     */
    478     public synchronized void restartListening(RouterAddress addr) {
     486    private synchronized void restartListening(RouterAddress addr) {
    479487        // try once again to prevent two pumpers which is fatal
    480488        // we could just return null since the return value is ignored
     
    529537                // AND it's one of our local interfaces,
    530538                // bind only to that.
    531                 boolean isFixed = _context.getProperty(CommSystemFacadeImpl.PROP_I2NP_NTCP_AUTO_IP, "true")
     539                boolean isFixed = _context.getProperty(PROP_I2NP_NTCP_AUTO_IP, "true")
    532540                                  .toLowerCase(Locale.US).equals("false");
    533                 String fixedHost = _context.getProperty(CommSystemFacadeImpl.PROP_I2NP_NTCP_HOSTNAME);
     541                String fixedHost = _context.getProperty(PROP_I2NP_NTCP_HOSTNAME);
    534542                if (isFixed && fixedHost != null) {
    535543                    try {
     
    639647    /** caller must synch on this */
    640648    private void configureLocalAddress() {
    641         RouterContext ctx = getContext();
    642         if (ctx == null) {
    643             System.err.println("NIO transport has no context?");
    644         } else {
    645649            // this generally returns null -- see javadoc
    646             RouterAddress ra = CommSystemFacadeImpl.createNTCPAddress(ctx);
     650            RouterAddress ra = createNTCPAddress();
    647651            if (ra != null) {
    648652                NTCPAddress addr = new NTCPAddress(ra);
     
    661665                    _log.info("NTCP address is outbound only");
    662666            }
    663         }
    664     }
     667    }
     668
     669    /**
     670     * This only creates an address if the hostname AND port are set in router.config,
     671     * which should be rare.
     672     * Otherwise, notifyReplaceAddress() below takes care of it.
     673     * Note this is called both from above and from NTCPTransport.startListening()
     674     *
     675     * @since IPv6 moved from CSFI
     676     */
     677    private RouterAddress createNTCPAddress() {
     678        String name = _context.getProperty(PROP_I2NP_NTCP_HOSTNAME);
     679        if ( (name == null) || (name.trim().length() <= 0) || ("null".equals(name)) )
     680            return null;
     681        int p = _context.getProperty(PROP_I2NP_NTCP_PORT, -1);
     682        if (p <= 0 || p >= 64*1024)
     683            return null;
     684        OrderedProperties props = new OrderedProperties();
     685        props.setProperty(NTCPAddress.PROP_HOST, name);
     686        props.setProperty(NTCPAddress.PROP_PORT, Integer.toString(p));
     687        RouterAddress addr = new RouterAddress(STYLE, props, NTCPAddress.DEFAULT_COST);
     688        return addr;
     689    }
     690   
     691    /**
     692     *  UDP changed addresses, tell NTCP and restart
     693     *
     694     *  @since IPv6 moved from CSFI.notifyReplaceAddress()
     695     */
     696    @Override
     697    public void externalAddressReceived(AddressSource source, byte[] ip, int port) {
     698        if (_log.shouldLog(Log.WARN))
     699            _log.warn("Received address: " + Addresses.toString(ip, port) + " from: " + source);
     700        // ignore UPnP for now, get everything from SSU
     701        if (source != SOURCE_SSU)
     702            return;
     703        externalAddressReceived(ip, port);
     704    }
     705   
     706    /**
     707     *  UDP changed addresses, tell NTCP and restart
     708     *  Port may be set to indicate requested port even if ip is null;
     709     *  see CSFI.notifyReplaceAddress()
     710     *
     711     *  @since IPv6 moved from CSFI.notifyReplaceAddress()
     712     */
     713    private synchronized void externalAddressReceived(byte[] ip, int port) {
     714        // FIXME just take first IPv4 address for now
     715        // FIXME if SSU set to hostname, NTCP will be set to IP
     716        RouterAddress oldAddr = getCurrentAddress(false);
     717        if (_log.shouldLog(Log.INFO))
     718            _log.info("Changing NTCP Address? was " + oldAddr);
     719
     720        OrderedProperties newProps = new OrderedProperties();
     721        int cost;
     722        if (oldAddr == null) {
     723            cost = NTCPAddress.DEFAULT_COST;
     724        } else {
     725            cost = oldAddr.getCost();
     726            newProps.putAll(oldAddr.getOptionsMap());
     727        }
     728        RouterAddress newAddr = new RouterAddress(STYLE, newProps, cost);
     729
     730        boolean changed = false;
     731
     732        // Auto Port Setting
     733        // old behavior (<= 0.7.3): auto-port defaults to false, and true trumps explicit setting
     734        // new behavior (>= 0.7.4): auto-port defaults to true, but explicit setting trumps auto
     735        // TODO rewrite this to operate on ints instead of strings
     736        String oport = newProps.getProperty(NTCPAddress.PROP_PORT);
     737        String nport = null;
     738        String cport = _context.getProperty(PROP_I2NP_NTCP_PORT);
     739        if (cport != null && cport.length() > 0) {
     740            nport = cport;
     741        } else if (_context.getBooleanPropertyDefaultTrue(PROP_I2NP_NTCP_AUTO_PORT)) {
     742            // 0.9.6 change
     743            // This wasn't quite right, as udpAddr is the EXTERNAL port and we really
     744            // want NTCP to bind to the INTERNAL port the first time,
     745            // because if they are different, the NAT is changing them, and
     746            // it probably isn't mapping UDP and TCP the same.
     747            if (port > 0)
     748                // should always be true
     749                nport = Integer.toString(port);
     750        }
     751        if (_log.shouldLog(Log.INFO))
     752            _log.info("old: " + oport + " config: " + cport + " new: " + nport);
     753        if (nport == null || nport.length() <= 0)
     754            return;
     755        // 0.9.6 change
     756        // Don't have NTCP "chase" SSU's external port,
     757        // as it may change, possibly frequently.
     758        //if (oport == null || ! oport.equals(nport)) {
     759        if (oport == null) {
     760            newProps.setProperty(NTCPAddress.PROP_PORT, nport);
     761            changed = true;
     762        }
     763
     764        // Auto IP Setting
     765        // old behavior (<= 0.7.3): auto-ip defaults to false, and trumps configured hostname,
     766        //                          and ignores reachability status - leading to
     767        //                          "firewalled with inbound TCP enabled" warnings.
     768        // new behavior (>= 0.7.4): auto-ip defaults to true, and explicit setting trumps auto,
     769        //                          and only takes effect if reachability is OK.
     770        //                          And new "always" setting ignores reachability status, like
     771        //                          "true" was in 0.7.3
     772        String ohost = newProps.getProperty(NTCPAddress.PROP_HOST);
     773        String enabled = _context.getProperty(PROP_I2NP_NTCP_AUTO_IP, "true").toLowerCase(Locale.US);
     774        String name = _context.getProperty(PROP_I2NP_NTCP_HOSTNAME);
     775        // hostname config trumps auto config
     776        if (name != null && name.length() > 0)
     777            enabled = "false";
     778
     779        // assume SSU is happy if the address is non-null
     780        // TODO is this sufficient?
     781        boolean ssuOK = ip != null;
     782        if (_log.shouldLog(Log.INFO))
     783            _log.info("old: " + ohost + " config: " + name + " auto: " + enabled + " ssuOK? " + ssuOK);
     784        if (enabled.equals("always") ||
     785            (Boolean.parseBoolean(enabled) && ssuOK)) {
     786            // ip non-null
     787            String nhost = Addresses.toString(ip);
     788            if (_log.shouldLog(Log.INFO))
     789                _log.info("old: " + ohost + " config: " + name + " new: " + nhost);
     790            if (nhost == null || nhost.length() <= 0)
     791                return;
     792            if (ohost == null || ! ohost.equalsIgnoreCase(nhost)) {
     793                newProps.setProperty(NTCPAddress.PROP_HOST, nhost);
     794                changed = true;
     795            }
     796        } else if (enabled.equals("false") &&
     797                   name != null && name.length() > 0 &&
     798                   !name.equals(ohost) &&
     799                   nport != null) {
     800            // Host name is configured, and we have a port (either auto or configured)
     801            // but we probably only get here if the port is auto,
     802            // otherwise createNTCPAddress() would have done it already
     803            if (_log.shouldLog(Log.INFO))
     804                _log.info("old: " + ohost + " config: " + name + " new: " + name);
     805            newProps.setProperty(NTCPAddress.PROP_HOST, name);
     806            changed = true;
     807        } else if (ohost == null || ohost.length() <= 0) {
     808            return;
     809        } else if (Boolean.parseBoolean(enabled) && !ssuOK) {
     810            // UDP transitioned to not-OK, turn off NTCP address
     811            // This will commonly happen at startup if we were initially OK
     812            // because UPnP was successful, but a subsequent SSU Peer Test determines
     813            // we are still firewalled (SW firewall, bad UPnP indication, etc.)
     814            if (_log.shouldLog(Log.INFO))
     815                _log.info("old: " + ohost + " config: " + name + " new: null");
     816            newAddr = null;
     817            changed = true;
     818        }
     819
     820        if (!changed) {
     821            if (oldAddr != null) {
     822                int oldCost = oldAddr.getCost();
     823                int newCost = NTCPAddress.DEFAULT_COST;
     824                if (TransportImpl.ADJUST_COST && !haveCapacity())
     825                    newCost++;
     826                if (newCost != oldCost) {
     827                    oldAddr.setCost(newCost);
     828                    if (_log.shouldLog(Log.WARN))
     829                        _log.warn("Changing NTCP cost from " + oldCost + " to " + newCost);
     830                } else {
     831                    _log.info("No change to NTCP Address");
     832                }
     833            } else {
     834                _log.info("No change to NTCP Address");
     835            }
     836            return;
     837        }
     838
     839        // stopListening stops the pumper, readers, and writers, so required even if
     840        // oldAddr == null since startListening starts them all again
     841        //
     842        // really need to fix this so that we can change or create an inbound address
     843        // without tearing down everything
     844        // Especially on disabling the address, we shouldn't tear everything down.
     845        //
     846        _log.warn("Halting NTCP to change address");
     847        stopListening();
     848        if (newAddr != null)
     849            newAddr.setOptions(newProps);
     850        // Wait for NTCP Pumper to stop so we don't end up with two...
     851        while (isAlive()) {
     852            try { Thread.sleep(5*1000); } catch (InterruptedException ie) {}
     853        }
     854        restartListening(newAddr);
     855        _log.warn("Changed NTCP Address and started up, address is now " + newAddr);
     856        return;         
     857    }
     858   
     859
    665860
    666861    /**
     
    699894        // if (Boolean.valueOf(_context.getProperty(CommSystemFacadeImpl.PROP_I2NP_NTCP_AUTO_PORT)).booleanValue())
    700895        //    return foo;
    701         return _context.getProperty(CommSystemFacadeImpl.PROP_I2NP_NTCP_PORT, -1);
     896        return _context.getProperty(PROP_I2NP_NTCP_PORT, -1);
    702897    }
    703898
  • router/java/src/net/i2p/router/transport/udp/UDPTransport.java

    r94e34ff r3a49d6d  
    156156    public static final String DEFAULT_SOURCES = SOURCE_INTERFACE.toConfigString() + ',' +
    157157                                                 SOURCE_UPNP.toConfigString() + ',' +
    158                                                  SOURCE_SSU_PEER.toConfigString();
     158                                                 SOURCE_SSU.toConfigString();
    159159    /** remember IP changes */
    160160    public static final String PROP_IP= "i2np.lastIP";
     
    564564     *
    565565     * @param source as defined in Transport.SOURCE_xxx
    566      * @param ip publicly routable IPv4 or IPv6
     566     * @param ip publicly routable IPv4 or IPv6, null ok
    567567     * @param port 0 if unknown
    568568     */
     
    571571        if (_log.shouldLog(Log.WARN))
    572572            _log.warn("Received address: " + Addresses.toString(ip, port) + " from: " + source);
     573        if (ip == null)
     574            return;
    573575        if (source == SOURCE_INTERFACE && ip.length == 16) {
    574576            // must be set before isValid() call
Note: See TracChangeset for help on using the changeset viewer.