diff --git a/workspace/popjava/src/ch/icosys/popjava/core/util/upnp/UPNPManager.java b/workspace/popjava/src/ch/icosys/popjava/core/util/upnp/UPNPManager.java index 1b47e6ab..8205fc76 100644 --- a/workspace/popjava/src/ch/icosys/popjava/core/util/upnp/UPNPManager.java +++ b/workspace/popjava/src/ch/icosys/popjava/core/util/upnp/UPNPManager.java @@ -113,8 +113,13 @@ public Tuple call() throws Exception { LogWriter.writeDebugInfo("Port " + port + " is already forwarded to ourself"); }else { LogWriter.writeDebugInfo("Port " + port + " is already forwarded to "+portMapping.getInternalClient()); - newPort = getFreeNATPort(port); - LogWriter.writeDebugInfo("Remap " + port + " to "+newPort); + newPort = getFreeNATPort(localAddress, port); + if(newPort < 0) { + LogWriter.writeDebugInfo("Remap of " + port + " to "+newPort+" is already in place"); + directMapping = true; + }else { + LogWriter.writeDebugInfo("Remap " + port + " to "+newPort); + } } } @@ -183,7 +188,7 @@ public void run() { } } - private static int getFreeNATPort(int port) throws IOException, SAXException { + private static int getFreeNATPort(InetAddress localAddress, int port) throws IOException, SAXException { int counter = 0; do { @@ -193,9 +198,13 @@ private static int getFreeNATPort(int port) throws IOException, SAXException { return port; } + if(portMapping.getInternalClient().equals(localAddress.getHostAddress())) { + return -port; + } + //Abort after 1000 ports if(counter++ > 1000) { - return -1; + return 0; } }while(true);