java.lang.Object
net.i2p.router.transport.UPnPManager
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
TransportManagerfor 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
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate classDelayed Callback.private classInitiate a UPnP search.private classThis is the callback from UPnP.Field Summary
FieldsModifier and TypeFieldDescriptionprivate final RouterContextThe router context.private final UPnPManager.DelayedCallbackCallback for the delayed retries.private InetAddressThe gateway address detected.private booleanTrue once the gateway loss was logged.private booleanTrue once the current error was logged.private booleanTrue while UPnP is running.private longWhen the gateway was last rescanned.private final LogLogger for the UPnP forwarding.private final TransportManagerThe transport manager we forward for.private final SimpleTimer2.TimedEventTimer rescanning the gateway.private final UPnPScannerCallbackCallback for the gateway scans.private booleanTrue if the config wants UPnP on.private final UPnPThe UPnP protocol implementation.private final UPnPManager.UPnPCallbackCallback for the UPnP results.private static final StringBundle holding the translated console strings.private static final intDefault HTTP port for the callbacks.private static final intDefault SSDP port for discovery.private static final StringThis 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.private static final StringThis is the UDP SSDP search reply listener.private static final longRescan delay while everything works.private static final longShortest delay between rescans.private static final longRescan delay after a failure.private static final StringPort mapping name for TCP.private static final StringPort mapping name for UDP.Constructor Summary
ConstructorsConstructorDescriptionUPnPManager(RouterContext context, TransportManager manager) Set up UPnP discovery and hold the transport manager to notify.Method Summary
Modifier and TypeMethodDescriptionprivate final StringLook up a translation key in the router web message bundle.Warning - blocking, very slow, queries the active router, will take many seconds if it has vanished.booleanrescan()Call when IP or network connectivity might have changed.voidstart()Blocking, may take a while.voidstop()Blocking, may take a while, up to 20 seconds.voidupdate(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.
Field Details
_log
Logger for the UPnP forwarding._context
The router context._upnp
The UPnP protocol implementation._upnpCallback
Callback for the UPnP results._scannerCallback
Callback for the gateway scans._delayedCallback
Callback for the delayed retries._isRunning
private volatile boolean _isRunningTrue while UPnP is running._shouldBeRunning
private volatile boolean _shouldBeRunningTrue if the config wants UPnP on._lastRescan
private volatile long _lastRescanWhen the gateway was last rescanned._errorLogged
private boolean _errorLoggedTrue once the current error was logged._disconLogged
private boolean _disconLoggedTrue once the gateway loss was logged._detectedAddress
The gateway address detected._manager
The transport manager we forward for._rescanner
Timer rescanning the gateway.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_PORTDefault HTTP port for the callbacks.- See Also:
PROP_SSDP_PORT
This is the UDP SSDP search reply listener.- See Also:
DEFAULT_SSDP_PORT
private static final int DEFAULT_SSDP_PORTDefault SSDP port for discovery.- See Also:
RESCAN_MIN_DELAY
private static final long RESCAN_MIN_DELAYShortest delay between rescans.- See Also:
RESCAN_SHORT_DELAY
private static final long RESCAN_SHORT_DELAYRescan delay after a failure.- See Also:
RESCAN_LONG_DELAY
private static final long RESCAN_LONG_DELAYRescan delay while everything works.- See Also:
TCP_PORT_NAME
UDP_PORT_NAME
BUNDLE_NAME
Bundle holding the translated console strings.- See Also:
Constructor Details
UPnPManager
Set up UPnP discovery and hold the transport manager to notify.- Parameters:
context- router context supplying properties, logging, and the SSDP/HTTP portsmanager- 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 calledstart()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
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
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