Esempio n. 1
0
        public async Task <IEnumerable <NatDevice> > OpenPort(int port)
        {
            var gatewayInterfaces = GetGatewayForDestination(new IPAddress(new byte[] { 8, 8, 8, 8 }));
            var cts     = new CancellationTokenSource(2000);
            var devices = await nat.DiscoverDevicesAsync(PortMapper.Upnp, cts);

            if (devices.Any(dev => LocalAddress(dev).AddressFamily == AddressFamily.InterNetworkV6))
            {
            }
            Debug.WriteLine($"Found UPnP devices: {string.Join(", ", devices.Select(dev => LocalAddress(dev)))}");
            foreach (var device in devices.Where(dev => gatewayInterfaces.Any(addr => LocalAddress(dev).Equals(addr))))
            {
                Debug.WriteLine($"Setting UPnP redirects for host {LocalAddress(device)}");
                try
                {
                    await device.CreatePortMapAsync(new Mapping(Protocol.Udp, port, port, 86400, "MonoTorrent"));
                }
                catch (Exception ex)
                {
                }
                try
                {
                    await device.CreatePortMapAsync(new Mapping(Protocol.Tcp, port, port, 86400, "MonoTorrent"));
                }
                catch (Exception ex)
                {
                }
            }
            return(devices);
        }
Esempio n. 2
0
        public static async Task TryMapPort(int port, int timeOut)
        {
            var natDiscoverer           = new NatDiscoverer();
            var cancellationTokenSource = new CancellationTokenSource(timeOut);
            var devices = (await natDiscoverer.DiscoverDevicesAsync(PortMapper.Upnp, cancellationTokenSource))
                          .Union(await natDiscoverer.DiscoverDevicesAsync(PortMapper.Pmp, cancellationTokenSource));

            foreach (var device in devices)
            {
                try
                {
                    await device.CreatePortMapAsync(new Mapping(Protocol.Udp, port, port, "Dnmp auto port map"));
                }
                catch (MappingException mappingException)
                {
                    if (mappingException.ErrorCode == portAlreadyInUseErrorCode)
                    {
                        continue;
                    }
                    logger.Warn(mappingException, "Exception in TryMapPort");
                }
            }
        }
Esempio n. 3
0
        public async Task <int> ListDevicesAsync(bool includePMP)
        {
            var nat = new NatDiscoverer();
            var cts = new CancellationTokenSource(TIMEOUT);

            PortMapper param = PortMapper.Upnp;

            if (includePMP)
            {
                param |= PortMapper.Pmp;
            }

            logMessageContext.Create("Getting devices");

            return(await nat.DiscoverDevicesAsync(param, cts).ContinueWith(t =>
            {
                int result = 0;

                if (t.Status != TaskStatus.Faulted)
                {
                    using (IEnumerator <NatDevice> enumerator = t.Result.GetEnumerator())
                    {
                        while (enumerator.MoveNext())
                        {
                            logMessageContext.Create("Found device: " + enumerator.Current.ToString());
                            result++;
                        }
                    }
                }
                else
                {
                    if (!(t.Exception.InnerException is NatDeviceNotFoundException))
                    {
                        result = -1;
                        logMessageContext.Create("error listing devices");
                    }
                }

                return result;
            }));
        }