From 56317faeadcfca8d65c15a8d2df793741a615266 Mon Sep 17 00:00:00 2001 From: Richard Maw Date: Fri, 27 Dec 2024 19:05:57 +0000 Subject: [PATCH] Support routers that only provide WANIPService:2 InternetGatewayDevice:2 adds WANConnectionDevice:2 as a sub-device that implements WANIPConnection:2 instead of WANIPConnection:1. This provides the same methods as WANIPConnection:1 but has some additional constraints such as the gateway must only serve it on the LAN interface and should reject mappings to an address that wasn't the origin of the request. --- .../util/upnp/PortMappingListener.java | 33 +++++++++++++------ 1 file changed, 23 insertions(+), 10 deletions(-) diff --git a/src/main/java/net/rptools/maptool/util/upnp/PortMappingListener.java b/src/main/java/net/rptools/maptool/util/upnp/PortMappingListener.java index 5dc4a5111b..f6d413d016 100644 --- a/src/main/java/net/rptools/maptool/util/upnp/PortMappingListener.java +++ b/src/main/java/net/rptools/maptool/util/upnp/PortMappingListener.java @@ -78,10 +78,13 @@ public class PortMappingListener extends DefaultRegistryListener { private final Logger logger = LoggerFactory.getLogger(PortMappingListener.class); - public static final DeviceType IGD_DEVICE_TYPE = new UDADeviceType("InternetGatewayDevice", 1); - public static final DeviceType CONNECTION_DEVICE_TYPE = new UDADeviceType("WANConnectionDevice", 1); + public static final DeviceType IGD_DEVICE_TYPE_V1 = new UDADeviceType("InternetGatewayDevice", 1); + public static final DeviceType IGD_DEVICE_TYPE_V2 = new UDADeviceType("InternetGatewayDevice", 2); + public static final DeviceType CONNECTION_DEVICE_TYPE_V1 = new UDADeviceType("WANConnectionDevice", 1); + public static final DeviceType CONNECTION_DEVICE_TYPE_V2 = new UDADeviceType("WANConnectionDevice", 2); - public static final ServiceType IP_SERVICE_TYPE = new UDAServiceType("WANIPConnection", 1); + public static final ServiceType IP_SERVICE_TYPE_V1 = new UDAServiceType("WANIPConnection", 1); + public static final ServiceType IP_SERVICE_TYPE_V2 = new UDAServiceType("WANIPConnection", 2); public static final ServiceType PPP_SERVICE_TYPE = new UDAServiceType("WANPPPConnection", 1); protected PortMapping[] portMappings; @@ -199,27 +202,37 @@ public void failure(ActionInvocation invocation, UpnpResponse operation, String } protected Service discoverConnectionService(Device device) { - if (!device.getType().equals(IGD_DEVICE_TYPE)) { + DeviceType deviceType = device.getType(); + if (!deviceType.equals(IGD_DEVICE_TYPE_V1) && !deviceType.equals(IGD_DEVICE_TYPE_V2)) { return null; } - Device[] connectionDevices = device.findDevices(CONNECTION_DEVICE_TYPE); + Device[] connectionDevices = device.findDevices(CONNECTION_DEVICE_TYPE_V2); if (connectionDevices.length == 0) { - logger.debug("IGD doesn't support '{}': {}", CONNECTION_DEVICE_TYPE, device); + logger.debug("IGD doesn't support '{}': {}", CONNECTION_DEVICE_TYPE_V2, device); + connectionDevices = device.findDevices(CONNECTION_DEVICE_TYPE_V1); + } + if (connectionDevices.length == 0) { + logger.debug("IGD doesn't support '{}': {}", CONNECTION_DEVICE_TYPE_V1, device); return null; } Device connectionDevice = connectionDevices[0]; logger.debug("Using first discovered WAN connection device: {}", connectionDevice); - Service ipConnectionService = connectionDevice.findService(IP_SERVICE_TYPE); - Service pppConnectionService = connectionDevice.findService(PPP_SERVICE_TYPE); + Service connectionService = connectionDevice.findService(IP_SERVICE_TYPE_V2); + if (connectionService == null) { + connectionService = connectionDevice.findService(IP_SERVICE_TYPE_V1); + } + if (connectionService == null) { + connectionService = connectionDevice.findService(PPP_SERVICE_TYPE); + } - if (ipConnectionService == null && pppConnectionService == null) { + if (connectionService == null) { logger.debug("IGD doesn't support IP or PPP WAN connection service: {}", device); } - return ipConnectionService != null ? ipConnectionService : pppConnectionService; + return connectionService; } protected void handleFailureMessage(String s) {