Пример #1
0
        public override async Task <Mapping> GetSpecificMappingAsync(Protocol protocol, int port)
        {
            Guard.IsTrue(protocol == Protocol.Tcp || protocol == Protocol.Udp, "protocol");
            Guard.IsInRange(port, 0, ushort.MaxValue, "port");

            try
            {
                var message      = new GetSpecificPortMappingEntryRequestMessage(protocol, port);
                var responseData = await _soapClient
                                   .InvokeAsync("GetSpecificPortMappingEntry", message.ToXml())
                                   .TimeoutAfter(TimeSpan.FromSeconds(4));

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

                return(new Mapping(messageResponse.Protocol
                                   , IPAddress.Parse(messageResponse.InternalClient)
                                   , messageResponse.InternalPort
                                   , messageResponse.ExternalPort
                                   , messageResponse.LeaseDuration
                                   , messageResponse.PortMappingDescription));
            }
            catch (MappingException e)
            {
                if (e.ErrorCode != UpnpConstants.NoSuchEntryInArray)
                {
                    throw;
                }
                return(null);
            }
        }
Пример #2
0
        public override async Task <IEnumerable <Mapping> > GetAllMappingsAsync()
        {
            var index    = 0;
            var mappings = new List <Mapping>();

            NatDiscoverer.TraceSource.LogInfo("GetAllMappingsAsync - Getting all mappings");
            while (true)
            {
                try
                {
                    var message = new GetGenericPortMappingEntry(index++);

                    var responseData = await _soapClient
                                       .InvokeAsync("GetGenericPortMappingEntry", message.ToXml())
                                       .TimeoutAfter(TimeSpan.FromSeconds(4));

                    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!");
                        continue;
                    }

                    var mapping = new Mapping(responseMessage.Protocol
                                              , internalClientIp
                                              , responseMessage.InternalPort
                                              , responseMessage.ExternalPort
                                              , responseMessage.LeaseDuration
                                              , responseMessage.PortMappingDescription);
                    mappings.Add(mapping);
                }
                catch (MappingException e)
                {
                    // there are no more mappings
                    if (e.ErrorCode == UpnpConstants.SpecifiedArrayIndexInvalid ||
                        e.ErrorCode == UpnpConstants.NoSuchEntryInArray
                        // DD-WRT Linux base router (and others probably) fails with 402-InvalidArgument when index is out of range
                        || e.ErrorCode == UpnpConstants.InvalidArguments
                        // LINKSYS WRT1900AC AC1900 it returns errocode 501-PAL_UPNP_SOAP_E_ACTION_FAILED
                        || e.ErrorCode == UpnpConstants.ActionFailed
                        // LINKSYS EA8500 returns error code 601
                        || e.ErrorCode == UpnpConstants.ArgumentValueOutOfRange)
                    {
                        NatDiscoverer.TraceSource.LogWarn("Router failed with {0}-{1}. No more mappings is assumed.", e.ErrorCode, e.ErrorText);
                        break;
                    }
                    throw;
                }
            }

            return(mappings.ToArray());
        }
Пример #3
0
        public override Task <Mapping> GetSpecificMappingAsync(Protocol protocol, int publicPort)
        {
            Guard.IsTrue(protocol == Protocol.Tcp || protocol == Protocol.Udp, "protocol");
            Guard.IsInRange(publicPort, 0, ushort.MaxValue, "port");

            NatDiscoverer.TraceSource.LogInfo("GetSpecificMappingAsync - Getting mapping for protocol: {0} port: {1}", Enum.GetName(typeof(Protocol), protocol), publicPort);

            var message = new GetSpecificPortMappingEntryRequestMessage(protocol, publicPort);

            return(_soapClient
                   .InvokeAsync("GetSpecificPortMappingEntry", message.ToXml())
                   .TimeoutAfter(TimeSpan.FromSeconds(4))
                   .ContinueWith(task =>
            {
                if (!task.IsFaulted)
                {
                    var responseData = task.Result;

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

                    return new Mapping(messageResponse.Protocol
                                       , IPAddress.Parse(messageResponse.InternalClient)
                                       , messageResponse.InternalPort
                                       , publicPort                  // messageResponse.ExternalPort is short.MaxValue
                                       , messageResponse.LeaseDuration
                                       , messageResponse.PortMappingDescription);
                }
                else
                {
                    MappingException e = task.Exception.InnerException as MappingException;
                    if (e != null && e.ErrorCode == UpnpConstants.NoSuchEntryInArray)
                    {
                        return null;
                    }

                    // DD-WRT Linux base router (and others probably) fails with 402-InvalidArgument
                    // when no mapping is found in the mappings table
                    if (e != null && e.ErrorCode == UpnpConstants.InvalidArguments)
                    {
                        NatDiscoverer.TraceSource.LogWarn("Router failed with 402-InvalidArgument. Mapping not found is assumed.");
                        return null;
                    }
                    throw task.Exception.InnerException;
                }
            }));
        }
Пример #4
0
        public override async Task <Mapping> GetSpecificMappingAsync(Protocol protocol, int publicPort)
        {
            Guard.IsTrue(protocol == Protocol.Tcp || protocol == Protocol.Udp, "protocol");
            Guard.IsInRange(publicPort, 0, ushort.MaxValue, "port");

            NatDiscoverer.TraceSource.LogInfo("GetSpecificMappingAsync - Getting mapping for protocol: {0} port: {1}", Enum.GetName(typeof(Protocol), protocol), publicPort);

            try
            {
                var message      = new GetSpecificPortMappingEntryRequestMessage(protocol, publicPort);
                var responseData = await _soapClient
                                   .InvokeAsync("GetSpecificPortMappingEntry", message.ToXml())
                                   .TimeoutAfter(TimeSpan.FromSeconds(4));

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

                if (messageResponse.Protocol != protocol)
                {
                    NatDiscoverer.TraceSource.LogWarn("Router responded to a protocol {0} query with a protocol {1} answer, work around applied.", protocol, messageResponse.Protocol);
                }

                return(new Mapping(protocol
                                   , IPAddress.Parse(messageResponse.InternalClient)
                                   , messageResponse.InternalPort
                                   , publicPort      // messageResponse.ExternalPort is short.MaxValue
                                   , messageResponse.LeaseDuration
                                   , messageResponse.PortMappingDescription));
            }
            catch (MappingException e)
            {
                // there are no more mappings
                if (e.ErrorCode == UpnpConstants.SpecifiedArrayIndexInvalid ||
                    e.ErrorCode == UpnpConstants.NoSuchEntryInArray
                    // DD-WRT Linux base router (and others probably) fails with 402-InvalidArgument when index is out of range
                    || e.ErrorCode == UpnpConstants.InvalidArguments
                    // LINKSYS WRT1900AC AC1900 it returns errocode 501-PAL_UPNP_SOAP_E_ACTION_FAILED
                    || e.ErrorCode == UpnpConstants.ActionFailed)
                {
                    NatDiscoverer.TraceSource.LogWarn("Router failed with {0}-{1}. No more mappings is assumed.", e.ErrorCode, e.ErrorText);
                    return(null);
                }
                throw;
            }
        }
Пример #5
0
        public override async Task <IEnumerable <Mapping> > GetAllMappingsAsync()
        {
            var index    = 0;
            var mappings = new List <Mapping>();

            while (true)
            {
                try
                {
                    var message = new GetGenericPortMappingEntry(index);

                    var responseData = await _soapClient
                                       .InvokeAsync("GetGenericPortMappingEntry", message.ToXml())
                                       .TimeoutAfter(TimeSpan.FromSeconds(4));

                    var responseMessage = new GetPortMappingEntryResponseMessage(responseData, DeviceInfo.ServiceType, true);

                    var mapping = new Mapping(responseMessage.Protocol
                                              , IPAddress.Parse(responseMessage.InternalClient)
                                              , responseMessage.InternalPort
                                              , responseMessage.ExternalPort
                                              , responseMessage.LeaseDuration
                                              , responseMessage.PortMappingDescription);
                    mappings.Add(mapping);
                    index++;
                }
                catch (MappingException e)
                {
                    if (e.ErrorCode == UpnpConstants.SpecifiedArrayIndexInvalid)
                    {
                        break;                                                          // there are no more mappings
                    }
                    throw;
                }
            }

            return(mappings.ToArray());
        }
Пример #6
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;
                }
            });
        }
Пример #7
0
		public override async Task<Mapping> GetSpecificMappingAsync (Protocol protocol, int port)
		{
            Guard.IsTrue(protocol == Protocol.Tcp || protocol == Protocol.Udp, "protocol");
            Guard.IsInRange(port, 0, ushort.MaxValue, "port");

            NatDiscoverer.TraceSource.LogInfo("GetSpecificMappingAsync - Getting mapping for protocol: {0} port: {1}", Enum.GetName(typeof(Protocol), protocol), port);

            try
            {
                var message = new GetSpecificPortMappingEntryRequestMessage(protocol, port);
                var responseData = await _soapClient
                    .InvokeAsync("GetSpecificPortMappingEntry", message.ToXml())
                    .TimeoutAfter(TimeSpan.FromSeconds(4));

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

                return new Mapping(messageResponse.Protocol
                    , IPAddress.Parse(messageResponse.InternalClient)
                    , messageResponse.InternalPort
                    , messageResponse.ExternalPort
                    , messageResponse.LeaseDuration
                    , messageResponse.PortMappingDescription);
            }
            catch (MappingException e)
            {
                // there are no more mappings
                if (e.ErrorCode == UpnpConstants.SpecifiedArrayIndexInvalid
                 || e.ErrorCode == UpnpConstants.NoSuchEntryInArray
                    // DD-WRT Linux base router (and others probably) fails with 402-InvalidArgument when index is out of range
                 || e.ErrorCode == UpnpConstants.InvalidArguments
                    // LINKSYS WRT1900AC AC1900 it returns errocode 501-PAL_UPNP_SOAP_E_ACTION_FAILED
                 || e.ErrorCode == UpnpConstants.ActionFailed)
                {
                    NatDiscoverer.TraceSource.LogWarn("Router failed with {0}-{1}. No more mappings is assumed.", e.ErrorCode, e.ErrorText);
                    return null;
                }
                throw;
            }
        }
Пример #8
0
		public override async Task<IEnumerable<Mapping>> GetAllMappingsAsync()
		{
            var index = 0;
		    var mappings = new List<Mapping>();

            NatDiscoverer.TraceSource.LogInfo("GetAllMappingsAsync - Getting all mappings");
            while (true)
            {
                try
                {
                    var message = new GetGenericPortMappingEntry(index++);

                    var responseData = await _soapClient
                        .InvokeAsync("GetGenericPortMappingEntry", message.ToXml())
                        .TimeoutAfter(TimeSpan.FromSeconds(4));

                    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!");
                        continue;
                    }

                    var mapping = new Mapping(responseMessage.Protocol
                        , internalClientIp
                        , responseMessage.InternalPort
                        , responseMessage.ExternalPort
                        , responseMessage.LeaseDuration
                        , responseMessage.PortMappingDescription);
                    mappings.Add(mapping);
                }
                catch (MappingException e)
                {
                    // there are no more mappings
                    if (e.ErrorCode == UpnpConstants.SpecifiedArrayIndexInvalid
                     || e.ErrorCode == UpnpConstants.NoSuchEntryInArray
                     // DD-WRT Linux base router (and others probably) fails with 402-InvalidArgument when index is out of range
                     || e.ErrorCode == UpnpConstants.InvalidArguments
                     // LINKSYS WRT1900AC AC1900 it returns errocode 501-PAL_UPNP_SOAP_E_ACTION_FAILED
                     || e.ErrorCode == UpnpConstants.ActionFailed)
                    {
                        NatDiscoverer.TraceSource.LogWarn("Router failed with {0}-{1}. No more mappings is assumed.", e.ErrorCode, e.ErrorText);
                        break; 
                    }
                    throw;
                }
            }

            return mappings.ToArray();
        }
Пример #9
0
		public override async Task<Mapping> GetSpecificMappingAsync (Protocol protocol, int port)
		{
            Guard.IsTrue(protocol == Protocol.Tcp || protocol == Protocol.Udp, "protocol");
            Guard.IsInRange(port, 0, ushort.MaxValue, "port");

            try
            {
                var message = new GetSpecificPortMappingEntryRequestMessage(protocol, port);
                var responseData = await _soapClient
                    .InvokeAsync("GetSpecificPortMappingEntry", message.ToXml())
                    .TimeoutAfter(TimeSpan.FromSeconds(4));

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

                return new Mapping(messageResponse.Protocol
                    , IPAddress.Parse(messageResponse.InternalClient)
                    , messageResponse.InternalPort
                    , messageResponse.ExternalPort
                    , messageResponse.LeaseDuration
                    , messageResponse.PortMappingDescription);
            }
            catch (MappingException e)
            {
                if (e.ErrorCode !=  UpnpConstants.NoSuchEntryInArray) throw;
                return null;
            }
        }
Пример #10
0
		public override async Task<IEnumerable<Mapping>> GetAllMappingsAsync()
		{
            var index = 0;
		    var mappings = new List<Mapping>();

            while (true)
            {
                try
                {
                    var message = new GetGenericPortMappingEntry(index);

                    var responseData = await _soapClient
                        .InvokeAsync("GetGenericPortMappingEntry", message.ToXml())
                        .TimeoutAfter(TimeSpan.FromSeconds(4));

                    var responseMessage = new GetPortMappingEntryResponseMessage(responseData, DeviceInfo.ServiceType, true);

                    var mapping = new Mapping(responseMessage.Protocol
                        , IPAddress.Parse(responseMessage.InternalClient)
                        , responseMessage.InternalPort
                        , responseMessage.ExternalPort
                        , responseMessage.LeaseDuration
                        , responseMessage.PortMappingDescription);
                    mappings.Add(mapping);
                    index++;
                }
                catch (MappingException e)
                {
                    if (e.ErrorCode == UpnpConstants.SpecifiedArrayIndexInvalid) break; // there are no more mappings
                    throw;
                }
            }

            return mappings.ToArray();
        }
Пример #11
0
        public override async Task<Mapping> GetSpecificMappingAsync (Protocol protocol, int port)
        {
            Guard.IsTrue(protocol == Protocol.Tcp || protocol == Protocol.Udp, "protocol");
            Guard.IsInRange(port, 0, ushort.MaxValue, "port");

            NatDiscoverer.TraceSource.LogInfo("GetSpecificMappingAsync - Getting mapping for protocol: {0} port: {1}", Enum.GetName(typeof(Protocol), protocol), port);

            try
            {
                var message = new GetSpecificPortMappingEntryRequestMessage(protocol, port);
                var responseData = await _soapClient
                    .InvokeAsync("GetSpecificPortMappingEntry", message.ToXml())
                    .TimeoutAfter(TimeSpan.FromSeconds(4));

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

                return new Mapping(messageResponse.Protocol
                    , IPAddress.Parse(messageResponse.InternalClient)
                    , messageResponse.InternalPort
                    , messageResponse.ExternalPort
                    , messageResponse.LeaseDuration
                    , messageResponse.PortMappingDescription);
            }
            catch (MappingException e)
            {
                if (e.ErrorCode == UpnpConstants.NoSuchEntryInArray ) return null;

                // DD-WRT Linux base router (and others probably) fails with 402-InvalidArgument 
                // when no mapping is found in the mappings table
                if (e.ErrorCode == UpnpConstants.InvalidArguments)
                {
                    NatDiscoverer.TraceSource.LogWarn("Router failed with 402-InvalidArgument. Mapping not found is assumed.");
                    return null;
                }
                throw;
            }
        }
Пример #12
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;
                    }
                });
        }