Skip to content

Commit 0da6a04

Browse files
bartwelontivero
authored andcommitted
Fix CreatePortMapAsync await recursion issue, Add workaround for GetSpecificMappingAsync protocol problem. (lontivero#46)
* Do call the rety of CreatePortMapAsync inside CreatePortMapAsync with await. (cherry picked from commit 7ee8dc8) * The fritz!box 7490 has a bug where it will respond to a GetSpecificPortMappingEntry with the incorrect protocol, patch up the response with the protocol as given by the request. (cherry picked from commit 66fa32d)
1 parent 5771516 commit 0da6a04

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

Open.Nat/Upnp/UpnpNatDevice.cs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ public override async Task CreatePortMapAsync(Mapping mapping)
145145
if(mapping.PrivateIP.Equals(IPAddress.None)) mapping.PrivateIP = DeviceInfo.LocalAddress;
146146

147147
NatDiscoverer.TraceSource.LogInfo("CreatePortMapAsync - Creating port mapping {0}", mapping);
148+
bool retry = false;
148149
try
149150
{
150151
var message = new CreatePortMappingRequestMessage(mapping);
@@ -162,17 +163,17 @@ await _soapClient
162163
mapping.Lifetime = 0;
163164
// We create the mapping anyway. It must be released on shutdown.
164165
mapping.LifetimeType = MappingLifetime.ForcedSession;
165-
CreatePortMapAsync(mapping);
166+
retry = true;
166167
break;
167168
case UpnpConstants.SamePortValuesRequired:
168169
NatDiscoverer.TraceSource.LogWarn("Same Port Values Required - Using internal port {0}", mapping.PrivatePort);
169170
mapping.PublicPort = mapping.PrivatePort;
170-
CreatePortMapAsync(mapping);
171+
retry = true;
171172
break;
172173
case UpnpConstants.RemoteHostOnlySupportsWildcard:
173174
NatDiscoverer.TraceSource.LogWarn("Remote Host Only Supports Wildcard");
174175
mapping.PublicIP = IPAddress.None;
175-
CreatePortMapAsync(mapping);
176+
retry = true;
176177
break;
177178
case UpnpConstants.ExternalPortOnlySupportsWildcard:
178179
NatDiscoverer.TraceSource.LogWarn("External Port Only Supports Wildcard");
@@ -185,6 +186,8 @@ await _soapClient
185186
throw;
186187
}
187188
}
189+
if (retry)
190+
await CreatePortMapAsync(mapping);
188191
}
189192
#endif
190193

@@ -427,7 +430,10 @@ public override async Task<Mapping> GetSpecificMappingAsync (Protocol protocol,
427430

428431
var messageResponse = new GetPortMappingEntryResponseMessage(responseData, DeviceInfo.ServiceType, false);
429432

430-
return new Mapping(messageResponse.Protocol
433+
if (messageResponse.Protocol != protocol)
434+
NatDiscoverer.TraceSource.LogWarn("Router responded to a protocol {0} query with a protocol {1} answer, work around applied.", protocol, messageResponse.Protocol);
435+
436+
return new Mapping(protocol
431437
, IPAddress.Parse(messageResponse.InternalClient)
432438
, messageResponse.InternalPort
433439
, publicPort // messageResponse.ExternalPort is short.MaxValue

0 commit comments

Comments
 (0)