alanmcgovern / Mono.Nat

UPNP and NAT-PMP port forwarding for .NET
https://github.com/mono/Mono.Nat
MIT License
160 stars 156 forks source link

Unable to create any port mapping. #15

Closed sved-dry closed 4 years ago

sved-dry commented 4 years ago

Hi,

I would like to use Mono Nat to open a UDP port for an app I am developing. When trying to create a port mapping, I get the following exception "Mono.Nat.MappingException: Error ConflictInMappingEntry". I get this exception for any port number I try, regardless of udp/tcp. I see the following output for my log statement regarding Device details:

Device details: Upnp 4/4/2020 5:09:20 PM 192.168.1.1:49808

My router is Asus CM_AC2600 and upnp is enabled. Any ideas on where to go next? Router is reachable and has the services needed as far as i can tell. Code is attached below:

urn:schemas-upnp-org:device:WANDevice:1 WANDevice MiniUPnP
private Mapping _currentMapping;
public int MIN_PORT => 42500;
public int MAX_PORT => 42900;
private void DeviceFound(object sender, DeviceEventArgs args)
        {
            Debug.Log("Device Found");
            INatDevice device = args.Device;

            Debug.Log($"Device details: {device.NatProtocol} {device.LastSeen} {device.DeviceEndpoint}");
            try
            {
                for (int i = MIN_PORT; i < MAX_PORT; i++)
                {
                    try
                    {
                        _currentMapping = device.CreatePortMap(new Mapping(Mono.Nat.Protocol.Udp, i, i));
                        break;
                    }
                    catch (Exception e)
                    {
                        Debug.Log($"port {i} in use, still scanning for port to open..., {e}");
                    }
                }

                Debug.Log($"We got map of public, {_currentMapping.PublicPort} private {_currentMapping.PrivatePort}");

            }
            catch (Exception e)
            {
                Debug.Log($"exception device: {e}");
            }
        }

Mono.Nat.MappingException: Error ConflictInMappingEntry: ConflictInMappingEntry at Mono.Nat.Upnp.ResponseMessage.Decode (Mono.Nat.Upnp.UpnpNatDevice device, System.String message) [0x0009e] in :0 at Mono.Nat.Upnp.UpnpNatDevice+d21.MoveNext () [0x001b6] in :0 --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000c] in <599589bf4ce248909b8a14cbe4a2034e>:0 at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (System.Threading.Tasks.Task task) [0x0003e] in <599589bf4ce248909b8a14cbe4a2034e>:0 at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Threading.Tasks.Task task) [0x00028] in <599589bf4ce248909b8a14cbe4a2034e>:0 at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd (System.Threading.Tasks.Task task) [0x00008] in <599589bf4ce248909b8a14cbe4a2034e>:0 at System.Runtime.CompilerServices.TaskAwaiter`1[TResult].GetResult () [0x00000] in <599589bf4ce248909b8a14cbe4a2034e>:0 at Mono.Nat.Upnp.UpnpNatDevice+d17.MoveNext () [0x00340] in :0 --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000c] in <599589bf4ce248909b8a14cbe4a2034e>:0 at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (System.Threading.Tasks.Task task) [0x0003e] in <599589bf4ce248909b8a14cbe4a2034e>:0 at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Threading.Tasks.Task task) [0x00028] in <599589bf4ce248909b8a14cbe4a2034e>:0 at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd (System.Threading.Tasks.Task task) [0x00008] in <599589bf4ce248909b8a14cbe4a2034e>:0 at System.Runtime.CompilerServices.ConfiguredTaskAwaitable1+ConfiguredTaskAwaiter[TResult].GetResult () [0x00000] in <599589bf4ce248909b8a14cbe4a2034e>:0 at Mono.Nat.Upnp.UpnpNatDevice+<CreatePortMapAsync>d__12.MoveNext () [0x00083] in <bb7a49cf350d4cdf92cc79833d23f455>:0 --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000c] in <599589bf4ce248909b8a14cbe4a2034e>:0 at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (System.Threading.Tasks.Task task) [0x0003e] in <599589bf4ce248909b8a14cbe4a2034e>:0 at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Threading.Tasks.Task task) [0x00028] in <599589bf4ce248909b8a14cbe4a2034e>:0 at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd (System.Threading.Tasks.Task task) [0x00008] in <599589bf4ce248909b8a14cbe4a2034e>:0 at System.Runtime.CompilerServices.TaskAwaiter1[TResult].GetResult () [0x00000] in <599589bf4ce248909b8a14cbe4a2034e>:0 at Mono.Nat.NatDeviceExtensions.CreatePortMap (Mono.Nat.INatDevice device, Mono.Nat.Mapping mapping) [0x0000d] in :0

alanmcgovern commented 4 years ago

Hi!

There are 3 things to try!

  1. Can you attach a logger to NatUtility.Logger and attach the output to this report? You can review it to remove any personal information first. I wouldn't expect there to be anything private in the logging, but I always recommend people look first :)

  2. This is a long shot... but can you modify your code so you always create a unique Description for each mapping? Alternatively, if you clone this repository and run the sample application 'Mono.Nat.Test' I have already pushed a change which makes the Description uniquehttps://github.com/alanmcgovern/Mono.Nat/commit/7ba5924e3641ffd27f140802eae13cea5bcec0fd . If the sample client works, then the odds are your application will also work once you make the messages unique.

  3. Can you invoke GetAllMappings, or GetAllMappingsAsync to get all port mappings on the device, assuming your device does support that. If you log all the information from the existing mappings (ports, description, enabled state etc) it may help me figure out what needs to change to make your router accept a new port mapping.

If the solution in 2 works, I'd still love a copy of the full request/response for a failing attempt and a successful attempt. While I don't think it will help, there's a chance there'll be something in the failing response I can use to generate a better error. I'll also release an update with this change if it helps your situation.

NOTE: If you use Console.Out for logging then everything will be threadsafe. Otherwise please wrap it in a TextWriter.Synchronized so things don't get corrupted. For example:

NatUtility.Logger = Console.Out; NatUtility.Logger = TextWriter.Synchronized(new StreamWriter("logging.txt") { AutoFlush = true });

sved-dry commented 4 years ago

Thanks for the response! I'll get the logging output and I'll clone the repo. I will give these a shot and get back to you.

sved-dry commented 4 years ago

logging.txt entire output:

UPnP Response: HTTP/1.1 200 OK
CACHE-CONTROL: max-age=120
ST: uuid:ac3606da-0db6-451c-bf0c-1b51ae92b3f9
USN: uuid:ac3606da-0db6-451c-bf0c-1b51ae92b3f9
EXT:
SERVER: Puma6 UPnP/1.1 MiniUPnPd/1.9
LOCATION: http://192.168.1.1:42667/rootDesc.xml
OPT: "http://schemas.upnp.org/upnp/1/0/"; ns=01
01-NLS: 1586102321
BOOTID.UPNP.ORG: 1586102321
CONFIGID.UPNP.ORG: 1337

UPnP Response: HTTP/1.1 200 OK
CACHE-CONTROL: max-age=120
ST: uuid:ac3606da-0db6-451c-bf0c-1b51ae92b3f8
USN: uuid:ac3606da-0db6-451c-bf0c-1b51ae92b3f8
EXT:
SERVER: Puma6 UPnP/1.1 MiniUPnPd/1.9
LOCATION: http://192.168.1.1:42667/rootDesc.xml
OPT: "http://schemas.upnp.org/upnp/1/0/"; ns=01
01-NLS: 1586102321
BOOTID.UPNP.ORG: 1586102321
CONFIGID.UPNP.ORG: 1337

UPnP Response: HTTP/1.1 200 OK
CACHE-CONTROL: max-age=120
ST: uuid:ac3606da-0db6-451c-bf0c-1b51ae92b3f7
USN: uuid:ac3606da-0db6-451c-bf0c-1b51ae92b3f7
EXT:
SERVER: Puma6 UPnP/1.1 MiniUPnPd/1.9
LOCATION: http://192.168.1.1:42667/rootDesc.xml
OPT: "http://schemas.upnp.org/upnp/1/0/"; ns=01
01-NLS: 1586102321
BOOTID.UPNP.ORG: 1586102321
CONFIGID.UPNP.ORG: 1337

UPnP Response: HTTP/1.1 200 OK
CACHE-CONTROL: max-age=120
ST: urn:schemas-upnp-org:service:Layer3Forwarding:1
USN: uuid:ac3606da-0db6-451c-bf0c-1b51ae92b3f7::urn:schemas-upnp-org:service:Layer3Forwarding:1
EXT:
SERVER: Puma6 UPnP/1.1 MiniUPnPd/1.9
LOCATION: http://192.168.1.1:42667/rootDesc.xml
OPT: "http://schemas.upnp.org/upnp/1/0/"; ns=01
01-NLS: 1586102321
BOOTID.UPNP.ORG: 1586102321
CONFIGID.UPNP.ORG: 1337

UPnP Response: HTTP/1.1 200 OK
CACHE-CONTROL: max-age=120
ST: urn:schemas-upnp-org:service:WANPPPConnection:1
USN: uuid:ac3606da-0db6-451c-bf0c-1b51ae92b3f9::urn:schemas-upnp-org:service:WANPPPConnection:1
EXT:
SERVER: Puma6 UPnP/1.1 MiniUPnPd/1.9
LOCATION: http://192.168.1.1:42667/rootDesc.xml
OPT: "http://schemas.upnp.org/upnp/1/0/"; ns=01
01-NLS: 1586102321
BOOTID.UPNP.ORG: 1586102321
CONFIGID.UPNP.ORG: 1337

UPnP Response: Router advertised a 'urn:schemas-upnp-org:service:WANPPPConnection:' service
Fetching service list: http://192.168.1.1:42667/rootDesc.xml
http://192.168.1.1:42667/rootDesc.xml: Parsed services list
http://192.168.1.1:42667/rootDesc.xml: Found service: urn:schemas-upnp-org:service:Layer3Forwarding:1
http://192.168.1.1:42667/rootDesc.xml: Found service: urn:schemas-upnp-org:service:WANCommonInterfaceConfig:1
http://192.168.1.1:42667/rootDesc.xml: Found service: urn:schemas-upnp-org:service:WANIPConnection:1
http://192.168.1.1:42667/rootDesc.xml: Found upnp service at: /ctl/IPConn
192.168.1.1:42667: Handshake Complete

Output of a GET reqeust to http://192.168.1.1:42667/rootDesc.xml

This XML file does not appear to have any style information associated with it. The document tree is shown below.
<root xmlns="urn:schemas-upnp-org:device-1-0">
<specVersion>
<major>1</major>
<minor>0</minor>
</specVersion>
<device>
<deviceType>
urn:schemas-upnp-org:device:InternetGatewayDevice:1
</deviceType>
<friendlyName>CM-32_AC2600 router</friendlyName>
<manufacturer>CM-32_AC2600</manufacturer>
<manufacturerURL>http://www.kernel.org/</manufacturerURL>
<modelDescription>CM-32_AC2600 router</modelDescription>
<modelName>CM-32_AC2600 router</modelName>
<modelNumber>1</modelNumber>
<modelURL>http://www.kernel.org/</modelURL>
<serialNumber>H3IABT001</serialNumber>
<UDN>uuid:ac3606da-0db6-451c-bf0c-1b51ae92b3f7</UDN>
<serviceList>
<service>
<serviceType>urn:schemas-upnp-org:service:Layer3Forwarding:1</serviceType>
<serviceId>urn:upnp-org:serviceId:Layer3Forwarding1</serviceId>
<controlURL>/ctl/L3F</controlURL>
<eventSubURL>/evt/L3F</eventSubURL>
<SCPDURL>/L3F.xml</SCPDURL>
</service>
</serviceList>
<deviceList>
<device>
<deviceType>urn:schemas-upnp-org:device:WANDevice:1</deviceType>
<friendlyName>WANDevice</friendlyName>
<manufacturer>MiniUPnP</manufacturer>
<manufacturerURL>http://miniupnp.free.fr/</manufacturerURL>
<modelDescription>WAN Device</modelDescription>
<modelName>WAN Device</modelName>
<modelNumber>20161110</modelNumber>
<modelURL>http://miniupnp.free.fr/</modelURL>
<serialNumber>H3IABT001</serialNumber>
<UDN>uuid:ac3606da-0db6-451c-bf0c-1b51ae92b3f8</UDN>
<UPC>000000000000</UPC>
<serviceList>
<service>
<serviceType>
urn:schemas-upnp-org:service:WANCommonInterfaceConfig:1
</serviceType>
<serviceId>urn:upnp-org:serviceId:WANCommonIFC1</serviceId>
<controlURL>/ctl/CmnIfCfg</controlURL>
<eventSubURL>/evt/CmnIfCfg</eventSubURL>
<SCPDURL>/WANCfg.xml</SCPDURL>
</service>
</serviceList>
<deviceList>
<device>
<deviceType>urn:schemas-upnp-org:device:WANConnectionDevice:1</deviceType>
<friendlyName>WANConnectionDevice</friendlyName>
<manufacturer>MiniUPnP</manufacturer>
<manufacturerURL>http://miniupnp.free.fr/</manufacturerURL>
<modelDescription>MiniUPnP daemon</modelDescription>
<modelName>MiniUPnPd</modelName>
<modelNumber>20161110</modelNumber>
<modelURL>http://miniupnp.free.fr/</modelURL>
<serialNumber>H3IABT001</serialNumber>
<UDN>uuid:ac3606da-0db6-451c-bf0c-1b51ae92b3f9</UDN>
<UPC>000000000000</UPC>
<serviceList>
<service>
<serviceType>urn:schemas-upnp-org:service:WANIPConnection:1</serviceType>
<serviceId>urn:upnp-org:serviceId:WANIPConn1</serviceId>
<controlURL>/ctl/IPConn</controlURL>
<eventSubURL>/evt/IPConn</eventSubURL>
<SCPDURL>/WANIPCn.xml</SCPDURL>
</service>
</serviceList>
</device>
</deviceList>
</device>
</deviceList>
<presentationURL>http://192.168.1.1</presentationURL>
</device>
</root>
sved-dry commented 4 years ago

Output of your test with the repo clone using branch unique-descriptions:

G:/GitHub/Mono.Nat/Mono.Nat.Test/bin/Debug/net472/Mono.Nat.Test.exe
                           Device found: Upnp
Type: UpnpNatDevice
IP: ******.61.81
---
UPnP Response: HTTP/1.1 200 OK
CACHE-CONTROL: max-age=120
ST: upnp:rootdevice
USN: uuid:ac3606da-0db6-451c-bf0c-1b51ae92b3f7::upnp:rootdevice
EXT:
SERVER: Puma6 UPnP/1.1 MiniUPnPd/1.9
LOCATION: http://192.168.1.1:42667/rootDesc.xml
OPT: "http://schemas.upnp.org/upnp/1/0/"; ns=01
01-NLS: 1586102321
BOOTID.UPNP.ORG: 1586102321
CONFIGID.UPNP.ORG: 1337

Unhandled Exception: Mono.Nat.MappingException: Error ConflictInMappingEntry: ConflictInMappingEntry
   at Mono.Nat.Upnp.ResponseMessage.Decode(UpnpNatDevice device, String message) in G:\GitHub\Mono.Nat\Mono.Nat\Upnp\Messages\Responses\ResponseMessage.cs:line 28
   at Mono.Nat.Upnp.UpnpNatDevice.<DecodeMessageFromResponse>d__21.MoveNext() in G:\GitHub\Mono.Nat\Mono.Nat\Upnp\UpnpNatDevice.cs:line 180
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
   at Mono.Nat.Upnp.UpnpNatDevice.<SendMessageAsync>d__17.MoveNext() in G:\GitHub\Mono.Nat\Mono.Nat\Upnp\UpnpNatDevice.cs:line 146
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1.ConfiguredTaskAwaiter.GetResult()
   at Mono.Nat.Upnp.UpnpNatDevice.<CreatePortMapAsync>d__12.MoveNext() in G:\GitHub\Mono.Nat\Mono.Nat\Upnp\UpnpNatDevice.cs:line 68
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
   at Mono.Nat.Test.NatTest.<DeviceFound>d__3.MoveNext() in G:\GitHub\Mono.Nat\Mono.Nat.Test\Main.cs:line 82
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.QueueUserWorkItemCallback.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem()
   at System.Threading.ThreadPoolWorkQueue.Dispatch()
sved-dry commented 4 years ago

GetAllMappings returns an empty array.

alanmcgovern commented 4 years ago

GetAllMappings returns an empty array.

That's not too surprising considering it's failing with 'normal' requests.

From the logging you provided I realised the library is not logging the full request/response data. I just added two more logging statements which should give the full information. Can you re-run the test application from tip of master so you see logging like this:

uPnP Request:
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"><s:Body><u:AddPortMapping xmlns:u="urn:schemas-upnp-org:service:WANIPConnection:1"><NewRemoteHost></NewRemoteHost><NewExternalPort>6001</NewExternalPort><NewProtocol>TCP</NewProtocol><NewInternalPort>6001</NewInternalPort><NewInternalClient>192.168.9.5</NewInternalClient><NewEnabled>1</NewEnabled><NewPortMappingDescription>Mono.Nat.Tes Tcp 6001 6001</NewPortMappingDescription><NewLeaseDuration>0</NewLeaseDuration></u:AddPortMapping></s:Body></s:Envelope>

uPnP Response:
<?xml version="1.0"?>
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"><s:Body><u:AddPortMappingResponse xmlns:u="urn:schemas-upnp-org:service:WANIPConnection:1"/></s:Body></s:Envelope>

Create Mapping: protocol=Tcp, public=6001, private=6001
uPnP Request:
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"><s:Body><u:GetSpecificPortMappingEntry xmlns:u="urn:schemas-upnp-org:service:WANIPConnection:1"><NewRemoteHost></NewRemoteHost><NewExternalPort>6001</NewExternalPort><NewProtocol>TCP</NewProtocol></u:GetSpecificPortMappingEntry></s:Body></s:Envelope>

uPnP Response:
<?xml version="1.0"?>
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"><s:Body><u:GetSpecificPortMappingEntryResponse xmlns:u="urn:schemas-upnp-org:service:WANIPConnection:1"><NewInternalPort>6001</NewInternalPort><NewInternalClient>192.168.9.5</NewInternalClient><NewEnabled>1</NewEnabled><NewPortMappingDescription>Mono.Nat.Tes Tcp 6001 6001</NewPortMappingDescription><NewLeaseDuration>0</NewLeaseDuration></u:GetSpecificPortMappingEntryResponse></s:Body></s:Envelope>

This may help diagnose the issue :)

alanmcgovern commented 4 years ago

Also if you want to test this further - can you see if setting Mapping.Lifetime to 7200 helps? Some routers fail when this is zero, some fail when it's non-zero :p

This is something the library could attempt to work around by automatically retrying a mapping with a different value for this property if the initial attempt fails.

sved-dry commented 4 years ago

Here is the output of the test with master. I also threw in a GetAllMappings into the test, you can find it by searching for Num mappings 0 below.

192.168.1.1:42667: Handshake Complete
Device found: Upnp
Type: UpnpNatDevice
uPnP Request:
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"><s:Body><u:GetExternalIPAddress xmlns:u="urn:schemas-upnp-org:service:WANIPConnection:1"></u:GetExternalIPAddress></s:Body></s:Envelope>

uPnP Search Response: HTTP/1.1 200 OK
CACHE-CONTROL: max-age=120
ST: urn:schemas-upnp-org:service:WANIPConnection:1
USN: uuid:ac3606da-0db6-451c-bf0c-1b51ae92b3f9::urn:schemas-upnp-org:service:WANIPConnection:1
EXT:
SERVER: Puma6 UPnP/1.1 MiniUPnPd/1.9
LOCATION: http://192.168.1.1:42667/rootDesc.xml
OPT: "http://schemas.upnp.org/upnp/1/0/"; ns=01
01-NLS: 1586102321
BOOTID.UPNP.ORG: 1586102321
CONFIGID.UPNP.ORG: 1337

uPnP Search Response: Router advertised a 'urn:schemas-upnp-org:service:WANIPConnection:1' service
uPnP Search Response: HTTP/1.1 200 OK
CACHE-CONTROL: max-age=120
ST: urn:schemas-upnp-org:service:WANCommonInterfaceConfig:1
USN: uuid:ac3606da-0db6-451c-bf0c-1b51ae92b3f8::urn:schemas-upnp-org:service:WANCommonInterfaceConfig:1
EXT:
SERVER: Puma6 UPnP/1.1 MiniUPnPd/1.9
LOCATION: http://192.168.1.1:42667/rootDesc.xml
OPT: "http://schemas.upnp.org/upnp/1/0/"; ns=01
01-NLS: 1586102321
BOOTID.UPNP.ORG: 1586102321
CONFIGID.UPNP.ORG: 1337

uPnP Search Response: HTTP/1.1 200 OK
CACHE-CONTROL: max-age=120
ST: urn:schemas-upnp-org:device:WANDevice:1
USN: uuid:ac3606da-0db6-451c-bf0c-1b51ae92b3f8::urn:schemas-upnp-org:device:WANDevice:1
EXT:
SERVER: Puma6 UPnP/1.1 MiniUPnPd/1.9
LOCATION: http://192.168.1.1:42667/rootDesc.xml
OPT: "http://schemas.upnp.org/upnp/1/0/"; ns=01
01-NLS: 1586102321
BOOTID.UPNP.ORG: 1586102321
CONFIGID.UPNP.ORG: 1337

uPnP Search Response: HTTP/1.1 200 OK
CACHE-CONTROL: max-age=120
ST: urn:schemas-upnp-org:device:WANConnectionDevice:1
USN: uuid:ac3606da-0db6-451c-bf0c-1b51ae92b3f9::urn:schemas-upnp-org:device:WANConnectionDevice:1
EXT:
SERVER: Puma6 UPnP/1.1 MiniUPnPd/1.9
LOCATION: http://192.168.1.1:42667/rootDesc.xml
OPT: "http://schemas.upnp.org/upnp/1/0/"; ns=01
01-NLS: 1586102321
BOOTID.UPNP.ORG: 1586102321
CONFIGID.UPNP.ORG: 1337

uPnP Search Response: HTTP/1.1 200 OK
CACHE-CONTROL: max-age=120
ST: urn:schemas-upnp-org:device:InternetGatewayDevice:1
USN: uuid:ac3606da-0db6-451c-bf0c-1b51ae92b3f7::urn:schemas-upnp-org:device:InternetGatewayDevice:1
EXT:
SERVER: Puma6 UPnP/1.1 MiniUPnPd/1.9
LOCATION: http://192.168.1.1:42667/rootDesc.xml
OPT: "http://schemas.upnp.org/upnp/1/0/"; ns=01
01-NLS: 1586102321
BOOTID.UPNP.ORG: 1586102321
CONFIGID.UPNP.ORG: 1337

uPnP Search Response: HTTP/1.1 200 OK
CACHE-CONTROL: max-age=120
ST: upnp:rootdevice
USN: uuid:ac3606da-0db6-451c-bf0c-1b51ae92b3f7::upnp:rootdevice
EXT:
SERVER: Puma6 UPnP/1.1 MiniUPnPd/1.9
LOCATION: http://192.168.1.1:42667/rootDesc.xml
OPT: "http://schemas.upnp.org/upnp/1/0/"; ns=01
01-NLS: 1586102321
BOOTID.UPNP.ORG: 1586102321
CONFIGID.UPNP.ORG: 1337

uPnP Response:
<?xml version="1.0"?>
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"><s:Body><u:GetExternalIPAddressResponse xmlns:u="urn:schemas-upnp-org:service:WANIPConnection:1"><NewExternalIPAddress>*****.61.81</NewExternalIPAddress></u:GetExternalIPAddressResponse></s:Body></s:Envelope>

IP: *****.61.81
uPnP Request:
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"><s:Body><u:GetGenericPortMappingEntry xmlns:u="urn:schemas-upnp-org:service:WANIPConnection:1"><NewPortMappingIndex>0</NewPortMappingIndex></u:GetGenericPortMappingEntry></s:Body></s:Envelope>

uPnP Response:
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"><s:Body><s:Fault><faultcode>s:Client</faultcode><faultstring>UPnPError</faultstring><detail><UPnPError xmlns="urn:schemas-upnp-org:control-1-0"><errorCode>713</errorCode><errorDescription>SpecifiedArrayIndexInvalid</errorDescription></
UPnPError></detail></s:Fault></s:Body></s:Envelope>
Num mappings 0
---
uPnP Request:
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"><s:Body><u:AddPortMapping xmlns:u="urn:schemas-upnp-org:service:WANIPConnection:1"><NewRemoteHost></NewRemoteHost><NewExternalPort>6001</NewExternalPort><NewProtocol>TCP</NewProtocol><NewInternalPort>6001</NewInternalPort><NewInternalC
lient>192.168.1.6</NewInternalClient><NewEnabled>1</NewEnabled><NewPortMappingDescription>Mono.Nat.Tes Tcp 6001 6001</NewPortMappingDescription><NewLeaseDuration>0</NewLeaseDuration></u:AddPortMapping></s:Body></s:Envelope>

uPnP Response:
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"><s:Body><s:Fault><faultcode>s:Client</faultcode><faultstring>UPnPError</faultstring><detail><UPnPError xmlns="urn:schemas-upnp-org:control-1-0"><errorCode>718</errorCode><errorDescription>ConflictInMappingEntry</errorDescription></UPnP
Error></detail></s:Fault></s:Body></s:Envelope>

Unhandled Exception: Mono.Nat.MappingException: Error ConflictInMappingEntry: ConflictInMappingEntry
   at Mono.Nat.Upnp.ResponseMessage.Decode(UpnpNatDevice device, String message) in G:\GitHub\Mono.Nat\Mono.Nat\Upnp\Messages\Responses\ResponseMessage.cs:line 28
   at Mono.Nat.Upnp.UpnpNatDevice.<DecodeMessageFromResponse>d__21.MoveNext() in G:\GitHub\Mono.Nat\Mono.Nat\Upnp\UpnpNatDevice.cs:line 186
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
   at Mono.Nat.Upnp.UpnpNatDevice.<SendMessageAsync>d__17.MoveNext() in G:\GitHub\Mono.Nat\Mono.Nat\Upnp\UpnpNatDevice.cs:line 149
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1.ConfiguredTaskAwaiter.GetResult()
   at Mono.Nat.Upnp.UpnpNatDevice.<CreatePortMapAsync>d__12.MoveNext() in G:\GitHub\Mono.Nat\Mono.Nat\Upnp\UpnpNatDevice.cs:line 68
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
   at Mono.Nat.Test.NatTest.<DeviceFound>d__3.MoveNext() in G:\GitHub\Mono.Nat\Mono.Nat.Test\Main.cs:line 86
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.QueueUserWorkItemCallback.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem()
   at System.Threading.ThreadPoolWorkQueue.Dispatch()
sved-dry commented 4 years ago

Here is the output of the test again, but with

var mapping = new Mapping(Protocol.Tcp, 6001, 6001, 7200, "descriptionTester");

192.168.1.1:42667: Handshake Complete
Device found: Upnp
Type: UpnpNatDevice
uPnP Request:
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"><s:Body><u:GetExternalIPAddress xmlns:u="urn:schemas-upnp-org:service:WANIPConnection:1"></u:GetExternalIPAddress></s:Body></s:Envelope>

uPnP Search Response: HTTP/1.1 200 OK
CACHE-CONTROL: max-age=120
ST: urn:schemas-upnp-org:service:WANIPConnection:1
USN: uuid:ac3606da-0db6-451c-bf0c-1b51ae92b3f9::urn:schemas-upnp-org:service:WANIPConnection:1
EXT:
SERVER: Puma6 UPnP/1.1 MiniUPnPd/1.9
LOCATION: http://192.168.1.1:42667/rootDesc.xml
OPT: "http://schemas.upnp.org/upnp/1/0/"; ns=01
01-NLS: 1586102321
BOOTID.UPNP.ORG: 1586102321
CONFIGID.UPNP.ORG: 1337

uPnP Search Response: Router advertised a 'urn:schemas-upnp-org:service:WANIPConnection:1' service
uPnP Search Response: HTTP/1.1 200 OK
CACHE-CONTROL: max-age=120
ST: urn:schemas-upnp-org:service:WANCommonInterfaceConfig:1
USN: uuid:ac3606da-0db6-451c-bf0c-1b51ae92b3f8::urn:schemas-upnp-org:service:WANCommonInterfaceConfig:1
EXT:
SERVER: Puma6 UPnP/1.1 MiniUPnPd/1.9
LOCATION: http://192.168.1.1:42667/rootDesc.xml
OPT: "http://schemas.upnp.org/upnp/1/0/"; ns=01
01-NLS: 1586102321
BOOTID.UPNP.ORG: 1586102321
CONFIGID.UPNP.ORG: 1337

uPnP Search Response: HTTP/1.1 200 OK
CACHE-CONTROL: max-age=120
ST: urn:schemas-upnp-org:device:WANDevice:1
USN: uuid:ac3606da-0db6-451c-bf0c-1b51ae92b3f8::urn:schemas-upnp-org:device:WANDevice:1
EXT:
SERVER: Puma6 UPnP/1.1 MiniUPnPd/1.9
LOCATION: http://192.168.1.1:42667/rootDesc.xml
OPT: "http://schemas.upnp.org/upnp/1/0/"; ns=01
01-NLS: 1586102321
BOOTID.UPNP.ORG: 1586102321
CONFIGID.UPNP.ORG: 1337

uPnP Search Response: HTTP/1.1 200 OK
CACHE-CONTROL: max-age=120
ST: urn:schemas-upnp-org:device:WANConnectionDevice:1
USN: uuid:ac3606da-0db6-451c-bf0c-1b51ae92b3f9::urn:schemas-upnp-org:device:WANConnectionDevice:1
EXT:
SERVER: Puma6 UPnP/1.1 MiniUPnPd/1.9
LOCATION: http://192.168.1.1:42667/rootDesc.xml
OPT: "http://schemas.upnp.org/upnp/1/0/"; ns=01
01-NLS: 1586102321
BOOTID.UPNP.ORG: 1586102321
CONFIGID.UPNP.ORG: 1337

uPnP Search Response: HTTP/1.1 200 OK
CACHE-CONTROL: max-age=120
ST: urn:schemas-upnp-org:device:InternetGatewayDevice:1
USN: uuid:ac3606da-0db6-451c-bf0c-1b51ae92b3f7::urn:schemas-upnp-org:device:InternetGatewayDevice:1
EXT:
SERVER: Puma6 UPnP/1.1 MiniUPnPd/1.9
LOCATION: http://192.168.1.1:42667/rootDesc.xml
OPT: "http://schemas.upnp.org/upnp/1/0/"; ns=01
01-NLS: 1586102321
BOOTID.UPNP.ORG: 1586102321
CONFIGID.UPNP.ORG: 1337

uPnP Search Response: HTTP/1.1 200 OK
CACHE-CONTROL: max-age=120
ST: upnp:rootdevice
USN: uuid:ac3606da-0db6-451c-bf0c-1b51ae92b3f7::upnp:rootdevice
EXT:
SERVER: Puma6 UPnP/1.1 MiniUPnPd/1.9
LOCATION: http://192.168.1.1:42667/rootDesc.xml
OPT: "http://schemas.upnp.org/upnp/1/0/"; ns=01
01-NLS: 1586102321
BOOTID.UPNP.ORG: 1586102321
CONFIGID.UPNP.ORG: 1337

uPnP Response:
<?xml version="1.0"?>
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"><s:Body><u:GetExternalIPAddressResponse xmlns:u="urn:schemas-upnp-org:service:WANIPConnection:1"><NewExternalIPAddress>*****.61.81</NewExternalIPAddress></u:GetExternalIPAddressResponse></s:Body></s:Envelope>

IP: ******.61.81
uPnP Request:
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"><s:Body><u:GetGenericPortMappingEntry xmlns:u="urn:schemas-upnp-org:service:WANIPConnection:1"><NewPortMappingIndex>0</NewPortMappingIndex></u:GetGenericPortMappingEntry></s:Body></s:Envelope>

uPnP Response:
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"><s:Body><s:Fault><faultcode>s:Client</faultcode><faultstring>UPnPError</faultstring><detail><UPnPError xmlns="urn:schemas-upnp-org:control-1-0"><errorCode>713</errorCode><errorDescription>SpecifiedArrayIndexInvalid</errorDescription></
UPnPError></detail></s:Fault></s:Body></s:Envelope>
Num mappings 0
---
uPnP Request:
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"><s:Body><u:AddPortMapping xmlns:u="urn:schemas-upnp-org:service:WANIPConnection:1"><NewRemoteHost></NewRemoteHost><NewExternalPort>6001</NewExternalPort><NewProtocol>TCP</NewProtocol><NewInternalPort>6001</NewInternalPort><NewInternalC
lient>192.168.1.6</NewInternalClient><NewEnabled>1</NewEnabled><NewPortMappingDescription>descriptionTester</NewPortMappingDescription><NewLeaseDuration>7200</NewLeaseDuration></u:AddPortMapping></s:Body></s:Envelope>

uPnP Response:
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"><s:Body><s:Fault><faultcode>s:Client</faultcode><faultstring>UPnPError</faultstring><detail><UPnPError xmlns="urn:schemas-upnp-org:control-1-0"><errorCode>718</errorCode><errorDescription>ConflictInMappingEntry</errorDescription></UPnP
Error></detail></s:Fault></s:Body></s:Envelope>

Unhandled Exception: Mono.Nat.MappingException: Error ConflictInMappingEntry: ConflictInMappingEntry
   at Mono.Nat.Upnp.ResponseMessage.Decode(UpnpNatDevice device, String message) in G:\GitHub\Mono.Nat\Mono.Nat\Upnp\Messages\Responses\ResponseMessage.cs:line 28
   at Mono.Nat.Upnp.UpnpNatDevice.<DecodeMessageFromResponse>d__21.MoveNext() in G:\GitHub\Mono.Nat\Mono.Nat\Upnp\UpnpNatDevice.cs:line 186
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
   at Mono.Nat.Upnp.UpnpNatDevice.<SendMessageAsync>d__17.MoveNext() in G:\GitHub\Mono.Nat\Mono.Nat\Upnp\UpnpNatDevice.cs:line 149
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1.ConfiguredTaskAwaiter.GetResult()
   at Mono.Nat.Upnp.UpnpNatDevice.<CreatePortMapAsync>d__12.MoveNext() in G:\GitHub\Mono.Nat\Mono.Nat\Upnp\UpnpNatDevice.cs:line 68
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
   at Mono.Nat.Test.NatTest.<DeviceFound>d__3.MoveNext() in G:\GitHub\Mono.Nat\Mono.Nat.Test\Main.cs:line 87
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.QueueUserWorkItemCallback.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem()
   at System.Threading.ThreadPoolWorkQueue.Dispatch()
alanmcgovern commented 4 years ago

Hrm, this is perplexing.

Can you view the list of existing forwarded ports if you log into your router's management page? If so, does it look like there's a collision?

Can any other upnp library, or application with built-in upnp support, correctly forward ports? If they can, could you use wireshark to capture their request/response messages so we can figure out what the difference is? Two options would be to use uTorrent or http://miniupnp.free.fr/files . If either can forward ports, then if you could capture the request/response using WireShark it would help me figure out what the difference is!

alanmcgovern commented 4 years ago

Oh, i have a question. Does your machine have more than 1 network adapter? For example is it connected to both WIFI and ethernet? Could it be that the code is sending a port forwarding request saying "Forward port 6001 to my IPAddress 192.168.1.6", but it's sending it using a different network adapter with a different IP address.

It is normal for upnp requests to be rejected if you try to forward a port to an IP address other than the one associated with the HTTP request.

sved-dry commented 4 years ago

I have attached a wireshark capture. Let me know if it contains enough data. I ran your test during the capture. myCaptureOnEthernet2.zip

I can download utorrent again and try a capture with that if it would help.

sved-dry commented 4 years ago

networkImage

I believe I am using Ethernet2 when your test executes. I see some traffic on a couple of these in wireshark, though. Almost all of my traffic occurs on Ethernet2.

sved-dry commented 4 years ago

This is what I see in my router's management page:

routerOutput

alanmcgovern commented 4 years ago

I think we're getting close!

The trace you attached only shows the upnp request to get the external IP address. That request looks like it was successful.

Do you think you could get a trace containing port mapping requests? Both Successful and failing if possible :)

sved-dry commented 4 years ago

I am not too fluent in wireshark. I can only capture a failure at this time. I will install torrent and get a success example from there. I will attach the failure case here in this reply. secondTry.zip

sved-dry commented 4 years ago

I this the output of uTorrent when I open it.

utorrentUPNP.zip

sved-dry commented 4 years ago

Is it possible my vEthernet adapter is causing any trouble? I think it is enabled because I have docker desktop:

vEthernet

Edit: I disabled vEthernet and it still fails with the same responses.

alanmcgovern commented 4 years ago

Hi! The uTorrent trace is perfect.

If you open it up in wireshark and look at item 10, its a HTTP POST request to your router. If you expand out the message in the bottom pane you can see the request is as expected: utorrent_request

If you look at item 11 it is the response, and it shows the same error Mono.Nat is receiving: utorrent_response

The trace also confirms that the issue isn't caused by having multiple active network devices. We can tell because the source IP is the same as the IP address in the port map request.

At this stage the issue is likely to be a bug in your router. If you haven't tried rebooting it, try that. It might help to toggle uPnP off, then reboot, then turn it on and reboot again. That should forcibly clear out bad settings. You could also try a factory reset (usually press and hold the power switch for 10-20 seconds).

If all of that fails, there's one other possibility. It looks like your router may support NAT-PMP and uPnP. If that is true, you should get two INatDevices. If you ignore/discard the uPnP one and try to forward ports using NatPmP then that might work. You can use this property to select the INatDevice which uses the NatPMP protocol https://github.com/alanmcgovern/Mono.Nat/blob/master/Mono.Nat/INatDevice.cs#L50

alanmcgovern commented 4 years ago

Based off the utorrent data you provided, there is no reason this will help your situation. However I have made the change to ensure Mono.Nat binds to the correct local IP address when issuing port map requests. If you do have multiple active network adapters, things should be reliably correctly instead of accidentally correct :)

https://github.com/alanmcgovern/Mono.Nat/commit/ce0028efeee98dffe06af1614a2b6c2a26a1097b

sved-dry commented 4 years ago

Same failure, but that is expected based upon what you mentioned. For some reason, the only DeviceFound event I receive is for Upnp. I never get a DeviceFound event for NAT-PMP. I don't mind helping you out more, but is up to you on how far you'd like to take this. I am not blocked or anything. FYI, I rebooted my router a few times, no change.

alanmcgovern commented 4 years ago

Your router is running MiniUPnPd 1.9 which is new enough to support NAT-PMP and also PCP. Mono.Nat itself doesn't support PCP yet, but it is worth adding support for that. I wouldn't expect that to help you as your router isn't responding to uPnP or NATPMP correctly and should support both of those.

The only suggestion I have left is to go back through every setting in your router and see if there's a second option which allows people to modify uPnP mappings. Some routers have one option to enable uPnP and then a second option which allows port mappings to be created. My guess is that the split was created as you can use uPnP to query your public IP address (which has no security concerns), but allowing port mapping requests has security concerns and so some manufacturers put that behind a second setting.

This happened to another user here https://github.com/alanmcgovern/Mono.Nat/issues/14 .

If there is no setting, and if your router does not have a firmware upgrade, the best option would be to try a few port mapping libraries. If something works then capturing a trace of the request/response would help me diagnose the difference.

I would not expect this to help as miniupnpd is generally speaking the gold standard. If two applications (Mono.Nat and uTorrent) cannot map ports using it then it really looks like a router specific configuration issue or bug.

sved-dry commented 4 years ago

I believe you are right about my router possibly having a configuration issue, since uTorrent fails to use it as well. I opened up issue #14 and I found that those settings are not present in my router. I'll show an image below of what my router exposes. FYI, the upnp faq link leads to a 404 page... lol

image

image

alanmcgovern commented 4 years ago

In your first screenshot there's a virtual server / port forwarding tab. According to the manual there's an additional setting in there called Enable Port Forwarding. Can you double check that setting is enabled? https://www.manualowl.com/m/Asus/CM-32-AC2600/Manual/504768?page=62

If that doesn't work then I'm all out of ideas :(

sved-dry commented 4 years ago

I have it enabled. And it gives us the same error. No worries, I think the next step for me is to either contact Asus or to try NAT hole punching instead.

alanmcgovern commented 4 years ago

I'll go ahead and close this as it looks like your router is the thing with the bug. If you do get it working with any library, or you find the right setting/update for your router, do let me know!

Sorry we couldn't get your router functional after all the diagnostics. At least the library did get one or two small improvements as a result of the issue you filed so it wasn't wasted effort.

Thanks

sved-dry commented 4 years ago

Thank you for your help on this. Happy to help.