Changeset 74dc427 for router


Ignore:
Timestamp:
Feb 28, 2018 8:26:31 PM (2 years ago)
Author:
zzz <zzz@…>
Branches:
master
Children:
f0b9894e
Parents:
d167b5e
Message:

Transport: Rescan addresses and UPnP after network reconnection
Log network disconnect/reconnect
Don't even try to start UPnP if network disconnected
Fix UPnP repeatedly decrementing listen ports on failure
Make more classes and methods package private
Log and javadoc tweaks

Location:
router/java/src/net/i2p/router/transport
Files:
4 edited

Legend:

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

    rd167b5e r74dc427  
    619619             boolean good = Addresses.isConnected();
    620620             if (_netMonitorStatus != good) {
     621                 if (good)
     622                     _log.logAlways(Log.INFO, "Network reconnected");
     623                 else
     624                     _log.error("Network disconnected");
    621625                 _context.router().eventLog().addEvent(EventLog.NETWORK, good ? "connected" : "disconnected");
    622626                 _netMonitorStatus = good;
     627                 if (good) {
     628                     // Check local addresses
     629                     _manager.initializeAddress();
     630                     // fire UPnP
     631                     _manager.transportAddressChanged();
     632                 }
    623633             }
    624634             reschedule(good ? LONG_DELAY : SHORT_DELAY);
  • router/java/src/net/i2p/router/transport/TransportManager.java

    rd167b5e r74dc427  
    1515import java.net.UnknownHostException;
    1616import java.util.ArrayList;
     17import java.util.Collection;
     18import java.util.Collections;
    1719import java.util.HashMap;
    1820import java.util.HashSet;
     
    4446import net.i2p.util.VersionComparator;
    4547
     48/**
     49 *  Keeps track of the enabled transports.
     50 *  Starts UPnP.
     51 *  Pluggable transport support incomplete.
     52 *
     53 *  Public only for a couple things in the console and Android.
     54 *  To be made package private.
     55 *  Everything external should go through CommSystemFacadeImpl.
     56 *  Not a public API, not for external use.
     57 *
     58 */
    4659public class TransportManager implements TransportEventListener {
    4760    private final Log _log;
     
    145158    private void removeTransport(Transport transport) {
    146159        if (transport == null) return;
     160        transport.setListener(null);
    147161        Transport old = _transports.remove(transport.getStyle());
    148162        if (old != null && _log.shouldLog(Log.WARN))
    149163            _log.warn("Removing transport " + transport.getStyle());
    150         transport.setListener(null);
    151164    }
    152165
     
    184197     */
    185198    private void initializeAddress(Transport t) {
     199        initializeAddress(Collections.singleton(t));
     200    }
     201   
     202    /**
     203     *  Notify all transports of ALL routable interface addresses, including IPv6.
     204     *  It's the transport's job to ignore what it can't handle.
     205     *  @since 0.9.34
     206     */
     207    void initializeAddress() {
     208        initializeAddress(_transports.values());
     209    }
     210   
     211    /**
     212     *  Notify transports of ALL routable interface addresses, including IPv6.
     213     *  It's the transport's job to ignore what it can't handle.
     214     *  @since 0.9.34
     215     */
     216    private void initializeAddress(Collection<Transport> ts) {
     217        if (ts.isEmpty())
     218            return;
    186219        Set<String> ipset = Addresses.getAddresses(false, true);  // non-local, include IPv6
    187220        //
     
    227260            }
    228261        }
    229         for (InetAddress ia : addresses) {
    230             byte[] ip = ia.getAddress();
    231             t.externalAddressReceived(SOURCE_INTERFACE, ip, 0);
     262        for (Transport t : ts) {
     263            for (InetAddress ia : addresses) {
     264                byte[] ip = ia.getAddress();
     265                t.externalAddressReceived(SOURCE_INTERFACE, ip, 0);
     266            }
    232267        }
    233268    }
     
    239274     *
    240275     */
    241     public void externalAddressReceived(Transport.AddressSource source, byte[] ip, int port) {
     276    void externalAddressReceived(Transport.AddressSource source, byte[] ip, int port) {
    242277        for (Transport t : _transports.values()) {
    243278            // don't loop
     
    254289     *  @since 0.9.20
    255290     */
    256     public void externalAddressRemoved(Transport.AddressSource source, boolean ipv6) {
     291    void externalAddressRemoved(Transport.AddressSource source, boolean ipv6) {
    257292        for (Transport t : _transports.values()) {
    258293            // don't loop
     
    266301     *
    267302     */
    268     public void forwardPortStatus(String style, byte[] ip, int port, int externalPort, boolean success, String reason) {
     303    void forwardPortStatus(String style, byte[] ip, int port, int externalPort, boolean success, String reason) {
    269304        Transport t = getTransport(style);
    270305        if (t != null)
     
    272307    }
    273308
    274     public synchronized void startListening() {
     309    synchronized void startListening() {
    275310        if (_dhThread.getState() == Thread.State.NEW)
    276311            _dhThread.start();
     
    308343    }
    309344   
    310     public synchronized void restart() {
     345    synchronized void restart() {
    311346        stopListening();
    312347        try { Thread.sleep(5*1000); } catch (InterruptedException ie) {}
     
    317352     *  Can be restarted.
    318353     */
    319     public synchronized void stopListening() {
     354    synchronized void stopListening() {
    320355        if (_upnpManager != null)
    321356            _upnpManager.stop();
     
    331366     *  @since 0.9
    332367     */
    333     public synchronized void shutdown() {
     368    synchronized void shutdown() {
    334369        stopListening();
    335370        _dhThread.shutdown();
     
    338373    }
    339374   
    340     public Transport getTransport(String style) {
     375    Transport getTransport(String style) {
    341376        return _transports.get(style);
    342377    }
     
    348383     *  @since 0.9.31
    349384     */
    350     public SortedMap<String, Transport> getTransports() {
     385    SortedMap<String, Transport> getTransports() {
    351386        TreeMap<String, Transport> rv = new TreeMap<String, Transport>();
    352387        rv.putAll(_transports);
     
    360395     *  sent a message to or received a message from in the last five minutes.
    361396     */
    362     public int countActivePeers() {
     397    int countActivePeers() {
    363398        int peers = 0;
    364399        for (Transport t : _transports.values()) {
     
    373408     *  Unused for anything, to be removed.
    374409     */
    375     public int countActiveSendPeers() {
     410    int countActiveSendPeers() {
    376411        int peers = 0;
    377412        for (Transport t : _transports.values()) {
     
    387422      * @param pct percent of limit 0-100
    388423      */
    389     public boolean haveOutboundCapacity(int pct) {
     424    boolean haveOutboundCapacity(int pct) {
    390425        for (Transport t : _transports.values()) {
    391426            if (t.haveCapacity(pct))
     
    400435      * Use for throttling in the router.
    401436      */
    402     public boolean haveHighOutboundCapacity() {
     437    boolean haveHighOutboundCapacity() {
    403438        if (_transports.isEmpty())
    404439            return false;
     
    416451      * @param pct percent of limit 0-100
    417452      */
    418     public boolean haveInboundCapacity(int pct) {
     453    boolean haveInboundCapacity(int pct) {
    419454        for (Transport t : _transports.values()) {
    420455            if (t.hasCurrentAddress() && t.haveCapacity(pct))
     
    430465     * Note: this method returns them in whimsical order.
    431466     */
    432     public Vector<Long> getClockSkews() {
     467    Vector<Long> getClockSkews() {
    433468        Vector<Long> skews = new Vector<Long>();
    434469        for (Transport t : _transports.values()) {
     
    446481     *  @return the best status of any transport
    447482     */
    448     public Status getReachabilityStatus() {
     483    Status getReachabilityStatus() {
    449484        Status rv = Status.UNKNOWN;
    450485        for (Transport t : _transports.values()) {
     
    460495     */
    461496    @Deprecated
    462     public void recheckReachability() {
     497    void recheckReachability() {
    463498        for (Transport t : _transports.values())
    464499            t.recheckReachability();
    465500    }
    466501
    467     public boolean isBacklogged(Hash peer) {
     502    boolean isBacklogged(Hash peer) {
    468503        for (Transport t : _transports.values()) {
    469504            if (t.isBacklogged(peer))
     
    473508    }   
    474509   
    475     public boolean isEstablished(Hash peer) {
     510    boolean isEstablished(Hash peer) {
    476511        for (Transport t : _transports.values()) {
    477512            if (t.isEstablished(peer))
     
    487522     * @since 0.9.24
    488523     */
    489     public void mayDisconnect(Hash peer) {
     524    void mayDisconnect(Hash peer) {
    490525        for (Transport t : _transports.values()) {
    491526             t.mayDisconnect(peer);
     
    498533     * This is NOT reset if the peer contacts us.
    499534     */
    500     public boolean wasUnreachable(Hash peer) {
     535    boolean wasUnreachable(Hash peer) {
    501536        for (Transport t : _transports.values()) {
    502537            if (!t.wasUnreachable(peer))
     
    517552     * @return IPv4 or IPv6 or null
    518553     */
    519     public byte[] getIP(Hash peer) {
     554    byte[] getIP(Hash peer) {
    520555        return TransportImpl.getIP(peer);
    521556    }   
     
    524559     *  This forces a rebuild
    525560     */
    526     public List<RouterAddress> getAddresses() {
     561    List<RouterAddress> getAddresses() {
    527562        List<RouterAddress> rv = new ArrayList<RouterAddress>(4);
    528563        // do this first since SSU may force a NTCP change
     
    584619    }
    585620   
    586     public TransportBid getBid(OutNetMessage msg) {
     621    TransportBid getBid(OutNetMessage msg) {
    587622        List<TransportBid> bids = getBids(msg);
    588623        if ( (bids == null) || (bids.isEmpty()) )
     
    591626            return bids.get(0);
    592627    }
    593     public List<TransportBid> getBids(OutNetMessage msg) {
     628
     629    List<TransportBid> getBids(OutNetMessage msg) {
    594630        if (msg == null)
    595631            throw new IllegalArgumentException("Null message?  no bidding on a null outNetMessage!");
     
    621657    }
    622658   
    623     public TransportBid getNextBid(OutNetMessage msg) {
     659    TransportBid getNextBid(OutNetMessage msg) {
    624660        int unreachableTransports = 0;
    625661        Hash peer = msg.getTarget().getIdentity().calculateHash();
     
    712748        }
    713749    }
    714    
     750
     751    /**
     752     *  TransportEventListener
     753     *  calls UPnPManager rescan() and update()
     754     */
    715755    public void transportAddressChanged() {
    716756        if (_upnpManager != null) {
     
    721761    }
    722762
    723     public List<String> getMostRecentErrorMessages() {
     763    List<String> getMostRecentErrorMessages() {
    724764        List<String> rv = new ArrayList<String>(16);
    725765        for (Transport t : _transports.values()) {
  • router/java/src/net/i2p/router/transport/UPnP.java

    rd167b5e r74dc427  
    7373 * TODO: Implement EventListener and react on ip-change
    7474 */
    75 public class UPnP extends ControlPoint implements DeviceChangeListener, EventListener {
     75class UPnP extends ControlPoint implements DeviceChangeListener, EventListener {
    7676        private final Log _log;
    7777        private final I2PAppContext _context;
     
    162162        }
    163163       
     164        /**
     165         *  As we only support a single active IGD, and we don't currently have any way
     166         *  to get any IPv6 addresses, this will return at most one IPv4 address.
     167         *
     168         *  Blocking!!!
     169         *
     170         *  @return array of length 1 containing an IPv4 address, or null
     171         */
    164172        public DetectedIP[] getAddress() {
    165173                _log.info("UP&P.getAddress() is called \\o/");
     
    476484
    477485        /**
    478          * @return the external address the NAT thinks we have.  Blocking.
     486         * @return the external IPv4 address the NAT thinks we have.  Blocking.
    479487         * null if we can't find it.
    480488         */
     
    12421250                                System.out.println("<h3>Device " + (++i) +
    12431251                                                   ": " + DataHelper.escapeHTML(device.getFriendlyName()) + "</h3>");
     1252                                System.out.println("<p>UDN: " + DataHelper.escapeHTML(device.getUDN()));
     1253                                System.out.println("<br>IP: " + getIP(device));
    12441254                                System.out.println(sb.toString());
    12451255                                sb.setLength(0);
  • router/java/src/net/i2p/router/transport/UPnPManager.java

    rd167b5e r74dc427  
    7070        Debug.initialize(context);
    7171        _upnp = new UPnP(context);
    72         _upnp.setHTTPPort(_context.getProperty(PROP_HTTP_PORT, DEFAULT_HTTP_PORT));
    73         _upnp.setSSDPPort(_context.getProperty(PROP_SSDP_PORT, DEFAULT_SSDP_PORT));
    7472        _upnpCallback = new UPnPCallback();
    7573        _rescanner = new Rescanner();
     
    7775   
    7876    /**
    79      *  Blocking, may take a while
     77     *  Blocking, may take a while.
     78     *  May be called even if already running.
    8079     */
    8180    public synchronized void start() {
    82         if (_log.shouldLog(Log.DEBUG))
    83             _log.debug("UPnP Start");
    8481        _shouldBeRunning = true;
    85         if (!_isRunning) {
     82        if (!_isRunning && Addresses.isConnected()) {
     83            if (_log.shouldLog(Log.DEBUG))
     84                _log.debug("UPnP Start");
    8685            long b = _context.clock().now();
    8786            try {
     87                // We set these here every time, because ControlPoint auto-decrements on failure,
     88                // and will eventually hit 1024 and then negative
     89                _upnp.setHTTPPort(_context.getProperty(PROP_HTTP_PORT, DEFAULT_HTTP_PORT));
     90                _upnp.setSSDPPort(_context.getProperty(PROP_SSDP_PORT, DEFAULT_SSDP_PORT));
    8891                _isRunning = _upnp.runPlugin();
    89                 if (_log.shouldLog(Log.INFO))
     92                if (_log.shouldDebug())
    9093                    _log.info("UPnP runPlugin took " + (_context.clock().now() - b));
    9194            } catch (RuntimeException e) {
     
    99102        if (_isRunning) {
    100103            _rescanner.schedule(RESCAN_LONG_DELAY);
    101             if (_log.shouldLog(Log.DEBUG))
    102                 _log.debug("UPnP Start Done");
    103104        } else {
    104105            _rescanner.schedule(RESCAN_SHORT_DELAY);
     
    151152            return;
    152153        _lastRescan = now;
    153         if (_log.shouldLog(Log.DEBUG))
    154             _log.debug("UPnP Rescan Start");
    155154        if (_isRunning) {
     155            if (_log.shouldLog(Log.DEBUG))
     156                _log.debug("UPnP Rescan");
    156157            // TODO default search MX (jitter) is 3 seconds... reduce?
    157158            // See also:
     
    250251                for (DetectedIP ip : ips) {
    251252                    // store the first public one and tell the transport manager if it changed
     253                    // Note that getAddress() will actually return a max of one address.
    252254                    if (TransportUtil.isPubliclyRoutable(ip.publicAddress.getAddress(), false)) {
    253255                        if (_log.shouldLog(Log.DEBUG))
Note: See TracChangeset for help on using the changeset viewer.