Ejemplo n.º 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;
                    }
                }
            }));
        }
Ejemplo n.º 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;
            }));
        }