Ejemplo n.º 1
0
        public override Task CreatePortMapAsync(Mapping mapping)
        {
            Guard.IsNotNull(mapping, "mapping");
            if (mapping.PrivateIP.Equals(IPAddress.None))
            {
                mapping.PrivateIP = DeviceInfo.LocalAddress;
            }

            NatDiscoverer.TraceSource.LogInfo("CreatePortMapAsync - Creating port mapping {0}", mapping);

            var message = new CreatePortMappingRequestMessage(mapping);

            return(_soapClient
                   .InvokeAsync("AddPortMapping", message.ToXml())
                   .TimeoutAfter(TimeSpan.FromSeconds(4))
                   .ContinueWith(task =>
            {
                if (!task.IsFaulted)
                {
                    RegisterMapping(mapping);
                }
                else
                {
                    MappingException me = task.Exception.InnerException as MappingException;

                    if (me == null)
                    {
                        throw task.Exception.InnerException;
                    }

                    switch (me.ErrorCode)
                    {
                    case UpnpConstants.OnlyPermanentLeasesSupported:
                        NatDiscoverer.TraceSource.LogWarn(
                            "Only Permanent Leases Supported - There is no warranty it will be closed");
                        mapping.Lifetime = 0;
                        // We create the mapping anyway. It must be released on shutdown.
                        mapping.LifetimeType = MappingLifetime.ForcedSession;
                        CreatePortMapAsync(mapping);
                        break;

                    case UpnpConstants.SamePortValuesRequired:
                        NatDiscoverer.TraceSource.LogWarn(
                            "Same Port Values Required - Using internal port {0}", mapping.PrivatePort);
                        mapping.PublicPort = mapping.PrivatePort;
                        CreatePortMapAsync(mapping);
                        break;

                    case UpnpConstants.RemoteHostOnlySupportsWildcard:
                        NatDiscoverer.TraceSource.LogWarn("Remote Host Only Supports Wildcard");
                        mapping.PublicIP = IPAddress.None;
                        CreatePortMapAsync(mapping);
                        break;

                    case UpnpConstants.ExternalPortOnlySupportsWildcard:
                        NatDiscoverer.TraceSource.LogWarn("External Port Only Supports Wildcard");
                        throw me;

                    case UpnpConstants.ConflictInMappingEntry:
                        NatDiscoverer.TraceSource.LogWarn("Conflict with an already existing mapping");
                        throw me;

                    default:
                        throw me;
                    }
                }
            }));
        }
Ejemplo n.º 2
0
 public CreatePortMappingRequestMessage(Mapping mapping)
 {
     _mapping = mapping;
 }
Ejemplo n.º 3
0
        public void GetGenericMappingAsync(int index, List <Mapping> mappings,
                                           TaskCompletionSource <IEnumerable <Mapping> > taskCompletionSource)
        {
            var message = new GetGenericPortMappingEntry(index);

            _soapClient
            .InvokeAsync("GetGenericPortMappingEntry", message.ToXml())
            .TimeoutAfter(TimeSpan.FromSeconds(4))
            .ContinueWith(task =>
            {
                if (!task.IsFaulted)
                {
                    var responseData    = task.Result;
                    var responseMessage = new GetPortMappingEntryResponseMessage(responseData,
                                                                                 DeviceInfo.ServiceType, true);

                    IPAddress internalClientIp;
                    if (!IPAddress.TryParse(responseMessage.InternalClient, out internalClientIp))
                    {
                        NatDiscoverer.TraceSource.LogWarn("InternalClient is not an IP address. Mapping ignored!");
                    }
                    else
                    {
                        var mapping = new Mapping(responseMessage.Protocol
                                                  , internalClientIp
                                                  , responseMessage.InternalPort
                                                  , responseMessage.ExternalPort
                                                  , responseMessage.LeaseDuration
                                                  , responseMessage.PortMappingDescription);
                        mappings.Add(mapping);
                    }

                    GetGenericMappingAsync(index + 1, mappings, taskCompletionSource);
                }
                else
                {
                    MappingException e = task.Exception.InnerException as MappingException;

                    if (e == null)
                    {
                        throw task.Exception.InnerException;
                    }

                    if (e.ErrorCode == UpnpConstants.SpecifiedArrayIndexInvalid ||
                        e.ErrorCode == UpnpConstants.NoSuchEntryInArray)
                    {
                        // there are no more mappings
                        taskCompletionSource.SetResult(mappings);
                        return;
                    }

                    // DD-WRT Linux base router (and others probably) fails with 402-InvalidArgument when index is out of range
                    if (e.ErrorCode == UpnpConstants.InvalidArguments)
                    {
                        NatDiscoverer.TraceSource.LogWarn("Router failed with 402-InvalidArgument. No more mappings is assumed.");
                        taskCompletionSource.SetResult(mappings);
                        return;
                    }

                    throw task.Exception.InnerException;
                }
            });
        }