Example #1
0
        public Task <PortResult> OpenPort(UPnPPort port)
        {
            return(Task.Run(async() =>
            {
                if (State == UPnPSupportState.NoPrepared)
                {
                    return PortResult.EngineNotPrepared;
                }
                else if (State == UPnPSupportState.NotSupported)
                {
                    return PortResult.EngineNotSupported;
                }
                else
                {
                    try
                    {
                        PortResult opened = await CheckPort(port);
                        switch (opened)
                        {
                        case PortResult.Opened:
                            return PortResult.AlreadyOpened;

                        case PortResult.Closed:
                            switch (port.Type)
                            {
                            case PortType.TCP:

                                await device.CreatePortMapAsync(new Mapping(Protocol.Tcp, System.Net.IPAddress.Parse(Utils.GetLocalIP()), 100, 100, 0, port.Description));
                                await device.CreatePortMapAsync(new Mapping(Protocol.Tcp, port.InternalPort, port.ExternalPort, port.Description));
                                return PortResult.Opened;

                            case PortType.UDP:
                                await device.CreatePortMapAsync(new Mapping(Protocol.Udp, port.InternalPort, port.ExternalPort, port.Description));
                                return PortResult.Opened;

                            case PortType.BOTH:
                                await device.CreatePortMapAsync(new Mapping(Protocol.Tcp, port.InternalPort, port.ExternalPort, port.Description));
                                await device.CreatePortMapAsync(new Mapping(Protocol.Udp, port.InternalPort, port.ExternalPort, port.Description));
                                return PortResult.Opened;

                            default:
                                return PortResult.FailedUnknown;
                            }

                        default:
                            return PortResult.FailedUnknown;
                        }
                    }
                    catch (Exception)
                    {
                        return PortResult.FailedUnknown;
                    }
                }
            }));
        }
Example #2
0
        public Task <UPnPSupportState> Prepare()
        {
            return(Task <UPnPSupportState> .Run(async() =>
            {
                try
                {
                    manager = new UPnPNAT();
                    UPnPPort testPort = new UPnPPort(1, PortType.TCP, Utils.GetLocalIP());

                    PortResult result = await CheckPort(testPort);
                    switch (result)
                    {
                    case PortResult.Opened:
                        //No more checks needed
                        State = UPnPSupportState.Supported;
                        break;

                    case PortResult.Closed:
                        //Let us check if we are able to open and close the test port.
                        PortResult r = await OpenPort(testPort);
                        if (r == PortResult.Opened)
                        {
                            r = await ClosePort(testPort);
                            if (r == PortResult.Closed)
                            {
                                State = UPnPSupportState.Supported;
                            }
                            else
                            {
                                State = UPnPSupportState.NotSupported;
                            }
                        }
                        else
                        {
                            State = UPnPSupportState.NotSupported;
                        }

                        break;

                    default:
                        //Something went wrong
                        State = UPnPSupportState.NoPrepared;
                        break;
                    }
                }
                catch (Exception)
                {
                    State = UPnPSupportState.NotSupported;
                }

                return State;
            }));
        }
Example #3
0
 public Task <PortResult> CheckPort(UPnPPort port)
 {
     return(Task.Run(async() =>
     {
         if (State == UPnPSupportState.NoPrepared)
         {
             return PortResult.EngineNotPrepared;
         }
         else if (State == UPnPSupportState.NotSupported)
         {
             return PortResult.EngineNotSupported;
         }
         else
         {
             try
             {
                 IEnumerable <Mapping> mappings = await device.GetAllMappingsAsync();
                 foreach (Mapping map in mappings)
                 {
                     if (map.PublicPort == port.ExternalPort)
                     {
                         if (port.Type == PortType.BOTH)
                         {
                             return PortResult.Opened;
                         }
                         else if (port.Type == PortType.TCP && map.Protocol == Protocol.Tcp)
                         {
                             return PortResult.Opened;
                         }
                         else if (port.Type == PortType.UDP && map.Protocol == Protocol.Udp)
                         {
                             return PortResult.Opened;
                         }
                         else
                         {
                             return PortResult.Closed;
                         }
                     }
                 }
                 return PortResult.Closed;
             }
             catch (Exception)
             {
                 return PortResult.FailedUnknown;
             }
         }
     }));
 }
Example #4
0
        public Task <PortResult> ClosePort(UPnPPort port)
        {
            return(Task <PortResult> .Run(async() =>
            {
                switch (State)
                {
                case UPnPSupportState.Supported:
                    try
                    {
                        List <UPnPPort> ports = await getOpenedPorts();

                        if (!ports.Contains(port))
                        {
                            return PortResult.AlreadyClosed;
                        }

                        IStaticPortMappingCollection maps = manager.StaticPortMappingCollection;
                        foreach (IStaticPortMapping map in maps)
                        {
                            UPnPPort port2 = convert(map);
                            if (port == port2)
                            {
                                maps.Remove(map.ExternalPort, map.Protocol);
                                return PortResult.Closed;
                            }
                        }
                        return PortResult.FailedUnknown;
                    }
                    catch (Exception)
                    {
                        return PortResult.FailedUnknown;
                    }

                case UPnPSupportState.NotSupported:
                    return PortResult.EngineNotSupported;

                case UPnPSupportState.NoPrepared:
                    return PortResult.EngineNotPrepared;

                default:
                    break;
                }

                return PortResult.FailedUnknown;
            }));
        }
Example #5
0
        private UPnPPort convert(IStaticPortMapping mapping)
        {
            UPnPPort port = new UPnPPort((UInt16)mapping.InternalPort, PortType.TCP, "");

            port.Description  = mapping.Description;
            port.ExternalPort = (UInt16)mapping.ExternalPort;
            port.InternalPort = (UInt16)mapping.InternalPort;
            switch (mapping.Protocol.ToUpper())
            {
            case "TCP":
                port.Type = PortType.TCP;
                break;

            case "UDP":
                port.Type = PortType.UDP;
                break;

            default:
                port.Type = PortType.BOTH;
                break;
            }
            return(port);
        }
Example #6
0
        public Task <PortResult> CheckPort(UPnPPort port)
        {
            return(Task <PortResult> .Run(async() =>
            {
                try
                {
                    List <UPnPPort> ports = await getOpenedPorts();

                    foreach (UPnPPort p in ports)
                    {
                        if (p == port)
                        {
                            return PortResult.Opened;
                        }
                    }

                    return PortResult.Closed;
                }
                catch (Exception)
                {
                    return PortResult.FailedUnknown;
                }
            }));
        }
Example #7
0
        public Task <PortResult> OpenPort(UPnPPort port)
        {
            return(Task <PortResult> .Run(async() =>
            {
                try
                {
                    switch (State)
                    {
                    case UPnPSupportState.Supported:

                        try
                        {
                            List <UPnPPort> ports = await getOpenedPorts();

                            if (ports.Contains(port))
                            {
                                return PortResult.AlreadyOpened;
                            }

                            IStaticPortMappingCollection maps = manager.StaticPortMappingCollection;
                            switch (port.Type)
                            {
                            case PortType.TCP:
                                maps.Add(port.ExternalPort, "TCP", port.InternalPort, port.InternalAddress, true, port.Description);
                                break;

                            case PortType.UDP:
                                maps.Add(port.ExternalPort, "UDP", port.InternalPort, port.InternalAddress, true, port.Description);
                                break;

                            case PortType.BOTH:
                                maps.Add(port.ExternalPort, "TCP", port.InternalPort, port.InternalAddress, true, port.Description);
                                maps.Add(port.ExternalPort, "UDP", port.InternalPort, port.InternalAddress, true, port.Description);
                                break;

                            default:
                                break;
                            }
                            return PortResult.Opened;
                        }
                        catch (Exception)
                        {
                            return PortResult.FailedUnknown;
                        }

                    case UPnPSupportState.NotSupported:
                        return PortResult.EngineNotSupported;

                    case UPnPSupportState.NoPrepared:
                        return PortResult.EngineNotPrepared;

                    default:
                        return PortResult.FailedUnknown;
                    }
                }
                catch (Exception)
                {
                    return PortResult.FailedUnknown;
                }
            }));
        }