java.lang.Object
net.i2p.router.transport.UPnPManager

class UPnPManagerextends Object
Universal Plug and Play (UPnP) manager for NAT traversal and port forwarding.

This class manages UPnP operations to automatically configure port forwarding on UPnP-enabled routers, allowing I2P to work through NAT and firewalls. It bridges between I2P's RouterAddress structures and the external FreeNet UPnP library.

Core Features:

  • Automatic UPnP device discovery
  • Port forwarding request and management
  • External IP address detection
  • Gateway device mapping and configuration
  • Lease renewal and monitoring
  • Service advertisement for I2P

UPnP Protocol Support:

  • SSDP (Simple Service Discovery Protocol)
  • HTTP interface for device configuration
  • SOAP-based control messages
  • Event-driven architecture with callbacks

Integration:

  • Bridges I2P RouterAddress to FreeNet structures
  • Works with TransportManager for address updates
  • Provides callbacks for transport events
  • Handles multiple router discovery protocols

Configuration:

  • Configurable HTTP and SSDP ports
  • Automatic device selection and fallback
  • Error handling and retry logic
  • Logging and monitoring capabilities
  • Field Details

    • _log

      private final Log _log
      Logger for the UPnP forwarding.
    • _context

      private final RouterContext _context
      The router context.
    • _upnp

      private final UPnP _upnp
      The UPnP protocol implementation.
    • _upnpCallback

      private final UPnPManager.UPnPCallback _upnpCallback
      Callback for the UPnP results.
    • _scannerCallback

      private final UPnPScannerCallback _scannerCallback
      Callback for the gateway scans.
    • _delayedCallback

      private final UPnPManager.DelayedCallback _delayedCallback
      Callback for the delayed retries.
    • _isRunning

      private volatile boolean _isRunning
      True while UPnP is running.
    • _shouldBeRunning

      private volatile boolean _shouldBeRunning
      True if the config wants UPnP on.
    • _lastRescan

      private volatile long _lastRescan
      When the gateway was last rescanned.
    • _errorLogged

      private boolean _errorLogged
      True once the current error was logged.
    • _disconLogged

      private boolean _disconLogged
      True once the gateway loss was logged.
    • _detectedAddress

      private InetAddress _detectedAddress
      The gateway address detected.
    • _manager

      private final TransportManager _manager
      The transport manager we forward for.
    • _rescanner

      private final SimpleTimer2.TimedEvent _rescanner
      Timer rescanning the gateway.
    • PROP_HTTP_PORT

      private static final String PROP_HTTP_PORT
      This is the TCP HTTP Event listener We move these so we don't conflict with other users of the same upnp library UPnP also binds to port 1900 UDP for multicast reception - this cannot be changed.
      See Also:
    • DEFAULT_HTTP_PORT

      private static final int DEFAULT_HTTP_PORT
      Default HTTP port for the callbacks.
      See Also:
    • PROP_SSDP_PORT

      private static final String PROP_SSDP_PORT
      This is the UDP SSDP search reply listener.
      See Also:
    • DEFAULT_SSDP_PORT

      private static final int DEFAULT_SSDP_PORT
      Default SSDP port for discovery.
      See Also:
    • RESCAN_MIN_DELAY

      private static final long RESCAN_MIN_DELAY
      Shortest delay between rescans.
      See Also:
    • RESCAN_SHORT_DELAY

      private static final long RESCAN_SHORT_DELAY
      Rescan delay after a failure.
      See Also:
    • RESCAN_LONG_DELAY

      private static final long RESCAN_LONG_DELAY
      Rescan delay while everything works.
      See Also:
    • TCP_PORT_NAME

      private static final String TCP_PORT_NAME
      Port mapping name for TCP.
      See Also:
    • UDP_PORT_NAME

      private static final String UDP_PORT_NAME
      Port mapping name for UDP.
      See Also:
    • BUNDLE_NAME

      private static final String BUNDLE_NAME
      Bundle holding the translated console strings.
      See Also:
  • Constructor Details

    • UPnPManager

      public UPnPManager(RouterContext context, TransportManager manager)
      Set up UPnP discovery and hold the transport manager to notify.
      Parameters:
      context - router context supplying properties, logging, and the SSDP/HTTP ports
      manager - transport manager notified of the port mappings UPnP discovers
  • Method Details

    • start

      public void start()
      Blocking, may take a while. May be called even if already running.
    • stop

      public void stop()
      Blocking, may take a while, up to 20 seconds.
    • rescan

      public boolean rescan()
      Call when IP or network connectivity might have changed. Starts UPnP if previous start failed, else starts a search. Must have called start() first, and not called stop().

      Should be fast. This only starts the search, the responses will come in over the MX time (3 seconds).

      Returns:
      true if a rescan was actually fired off
    • update

      public void update(Set<TransportManager.Port> ports)
      Call when the ports might have changed The transports can call this pretty quickly at startup, which can have multiple UPnP threads running at once, but that should be ok.
      Parameters:
      ports - transport ports to expose, one entry per style such as SSU or NTCP
    • renderStatusHTML

      public String renderStatusHTML()
      Warning - blocking, very slow, queries the active router, will take many seconds if it has vanished.
      Returns:
      an HTML fragment describing the mappings UPnP has discovered
    • _t

      private final String _t(String s)
      Look up a translation key in the router web message bundle.
      Parameters:
      s - the translation key
      Returns:
      the translated string