void _ssdp_MessageReceived(object sender, SsdpMessageEventArgs e)
        {
            var endpoint = e.EndPoint as IPEndPoint;

            if (endpoint != null && e.LocalIp != null)
            {
                NatUtility.Handle(e.LocalIp, e.Message, endpoint, NatProtocol.Upnp);
            }
        }
Пример #2
0
        void _ssdp_MessageReceived(object sender, SsdpMessageEventArgs e)
        {
            var endpoint = e.EndPoint as IPEndPoint;

            if (endpoint == null || e.LocalEndPoint == null)
            {
                return;
            }

            string usn;

            if (!e.Headers.TryGetValue("USN", out usn))
            {
                usn = string.Empty;
            }

            string nt;

            if (!e.Headers.TryGetValue("NT", out nt))
            {
                nt = string.Empty;
            }

            // Filter device type
            if (usn.IndexOf("WANIPConnection:", StringComparison.OrdinalIgnoreCase) == -1 &&
                nt.IndexOf("WANIPConnection:", StringComparison.OrdinalIgnoreCase) == -1 &&
                usn.IndexOf("WANPPPConnection:", StringComparison.OrdinalIgnoreCase) == -1 &&
                nt.IndexOf("WANPPPConnection:", StringComparison.OrdinalIgnoreCase) == -1)
            {
                return;
            }

            var identifier = string.IsNullOrWhiteSpace(usn) ? nt : usn;

            if (!_usnsHandled.Contains(identifier))
            {
                _usnsHandled.Add(identifier);

                _logger.Debug("Calling Nat.Handle on " + identifier);
                NatUtility.Handle(e.LocalEndPoint.Address, e.Message, endpoint, NatProtocol.Upnp);
            }
        }
        private async void _deviceDiscovery_DeviceDiscovered(object sender, GenericEventArgs <UpnpDeviceInfo> e)
        {
            if (_disposed)
            {
                return;
            }

            var info = e.Argument;

            string usn;

            if (!info.Headers.TryGetValue("USN", out usn))
            {
                usn = string.Empty;
            }

            string nt;

            if (!info.Headers.TryGetValue("NT", out nt))
            {
                nt = string.Empty;
            }

            // Filter device type
            if (usn.IndexOf("WANIPConnection:", StringComparison.OrdinalIgnoreCase) == -1 &&
                nt.IndexOf("WANIPConnection:", StringComparison.OrdinalIgnoreCase) == -1 &&
                usn.IndexOf("WANPPPConnection:", StringComparison.OrdinalIgnoreCase) == -1 &&
                nt.IndexOf("WANPPPConnection:", StringComparison.OrdinalIgnoreCase) == -1)
            {
                return;
            }

            var identifier = string.IsNullOrWhiteSpace(usn) ? nt : usn;

            if (info.Location == null)
            {
                return;
            }

            lock (_usnsHandled)
            {
                if (_usnsHandled.Contains(identifier))
                {
                    return;
                }
                _usnsHandled.Add(identifier);
            }

            _logger.Debug("Found NAT device: " + identifier);

            IPAddress address;

            if (IPAddress.TryParse(info.Location.Host, out address))
            {
                // The Handle method doesn't need the port
                var endpoint = new IPEndPoint(address, info.Location.Port);

                IPAddress localAddress = null;

                try
                {
                    var localAddressString = await _appHost.GetLocalApiUrl().ConfigureAwait(false);

                    Uri uri;
                    if (Uri.TryCreate(localAddressString, UriKind.Absolute, out uri))
                    {
                        localAddressString = uri.Host;

                        if (!IPAddress.TryParse(localAddressString, out localAddress))
                        {
                            return;
                        }
                    }
                }
                catch (Exception ex)
                {
                    return;
                }

                if (_disposed)
                {
                    return;
                }

                _logger.Debug("Calling Nat.Handle on " + identifier);
                NatUtility.Handle(localAddress, info, endpoint, NatProtocol.Upnp);
            }
        }