Esempio n. 1
0
        /// <summary>
        /// Creates a new port map.
        /// </summary>
        /// <param name="port">The port to map.</param>
        /// <param name="externalPort">The port which has been mapped, -1 if it failed.</param>
        /// <returns>True if successfull, else False.</returns>
        public static bool CreatePortMap(int port, out int externalPort)
        {
            if (!_discoveryComplete)
            {
                externalPort = -1;
                return(false);
            }

            try
            {
                Mapping mapping = new Mapping(Protocol.Tcp, port, port);

                for (int i = 0; i < 3; i++)
                {
                    _device.BeginCreatePortMap(mapping, EndCreateAsync, null);
                }

                if (_mappings.ContainsKey(mapping.PrivatePort))
                {
                    _mappings[mapping.PrivatePort] = mapping;
                }
                else
                {
                    _mappings.Add(mapping.PrivatePort, mapping);
                }

                externalPort = mapping.PublicPort;
                return(true);
            }
            catch (MappingException)
            {
                externalPort = -1;
                return(false);
            }
        }
        public void MapPort()
        {
            if (device == null)
            {
                return;
            }

            logger.Info("UPnP attempting to map port(" + map.Protocol + "/" + map.Port + ")");
            device.BeginCreatePortMap(map, "MonoTorrent", EndMapPort, map);
        }
Esempio n. 3
0
 private void EndMapTcpPort(IAsyncResult result)
 {
     try
     {
         INatDevice device = (INatDevice)result.AsyncState;
         device.EndCreatePortMap(result);
         logger.Info("UPnP port mapping successful {0}", tcpMapping);
         device.BeginCreatePortMap(udpMapping, EndMapUdpPort, device);
     }
     catch (MappingException e)
     {
         logger.Error("UPnP failed to map port {0}. Error {1}", tcpMapping, e);
     }
 }
Esempio n. 4
0
        /// <summary>
        /// Creates a new port mapping on the UPnP device.
        /// </summary>
        /// <param name="port">The port to map.</param>
        public void CreatePortMap(int port)
        {
            if (!DiscoveryCompleted)
            {
                return;
            }

            try
            {
                Mapping mapping = new Mapping(Protocol.Tcp, port, port);

                _device.BeginCreatePortMap(mapping, EndCreateAsync, mapping);
            }
            catch (MappingException)
            {
            }
        }
Esempio n. 5
0
    void MapPort()
    {
        try
        {
            Debug.Log("Mapping port...");

            bool udpDone = false; //, tcpDone = false;

            udpMapping = new Mapping(Protocol.Udp, Port, Port)
            {
                Description = "Pico Battle (UDP)"
            };
            natDevice.BeginCreatePortMap(udpMapping, state =>
            {
                if (state.IsCompleted)
                {
                    Debug.Log("UDP Mapping complete!");
//                    Debug.Log("UDP Mapping complete! Testing...");
//                    try
//                    {
//                        var m = natDevice.GetSpecificMapping(Protocol.Udp, Port);
//                        if (m == null)
//                            throw new InvalidOperationException("Mapping not found");
//                        if (m.PrivatePort != Port || m.PublicPort != Port)
//                            throw new InvalidOperationException("Mapping invalid");
//
//                        Debug.Log("Success!");
//                    }
//                    catch (Exception ex)
//                    {
//                        Debug.Log("Failed to validate UDP mapping :\n" + ex.ToString());
//                    }

                    udpDone = true;
//                    if (tcpDone)
                }
            }, null);

//            tcpMapping = new Mapping(Protocol.Tcp, Port, Port) { Description = "Pico Battle (TCP)" };
//            natDevice.BeginCreatePortMap(tcpMapping, state =>
//            {
//                if (state.IsCompleted)
//                {
//                    Debug.Log("TCP Mapping complete!");
//                    Debug.Log("TCP Mapping complete! Testing...");
//                    try
//                    {
//                        var m = natDevice.GetSpecificMapping(Protocol.Tcp, Port);
//                        if (m == null)
//                            throw new InvalidOperationException("Mapping not found");
//                        if (m.PrivatePort != Port || m.PublicPort != Port)
//                            throw new InvalidOperationException("Mapping invalid");
//
//                        Debug.Log("Success!");
//                    }
//                    catch (Exception ex)
//                    {
//                        Debug.Log("Failed to validate TCP mapping :\n" + ex.ToString());
//                    }
//
//                    tcpDone = true;
//                    if (udpDone)
//                        shouldTestConnection = true;
//                }
//            }, null);
        }
        catch (Exception ex)
        {
            Debug.Log("Failed to map port :\n" + ex.ToString());
        }
    }
Esempio n. 6
0
    void MapPort()
    {
        try
        {
            Debug.Log("Mapping port...");

            udpMapping = new Mapping(Protocol.Udp, Port, Port)
            {
                Description = "Diluvium (UDP)"
            };
            natDevice.BeginCreatePortMap(udpMapping, state =>
            {
                if (state.IsCompleted)
                {
                    Debug.Log("UDP Mapping complete! Testing...");
                    try
                    {
                        var m = natDevice.GetSpecificMapping(Protocol.Udp, Port);
                        if (m == null)
                        {
                            throw new InvalidOperationException("Mapping not found");
                        }
                        if (m.PrivatePort != Port || m.PublicPort != Port)
                        {
                            throw new InvalidOperationException("Mapping invalid");
                        }

                        Debug.Log("Success!");
                    }
                    catch (Exception ex)
                    {
                        Debug.Log("Failed to validate UDP mapping :\n" + ex.ToString());
                    }
                }
            }, null);

            tcpMapping = new Mapping(Protocol.Tcp, Port, Port)
            {
                Description = "Diluvium (TCP)"
            };
            natDevice.BeginCreatePortMap(tcpMapping, state =>
            {
                if (state.IsCompleted)
                {
                    Debug.Log("TCP Mapping complete! Testing...");
                    try
                    {
                        var m = natDevice.GetSpecificMapping(Protocol.Tcp, Port);
                        if (m == null)
                        {
                            throw new InvalidOperationException("Mapping not found");
                        }
                        if (m.PrivatePort != Port || m.PublicPort != Port)
                        {
                            throw new InvalidOperationException("Mapping invalid");
                        }

                        Debug.Log("Success!");
                    }
                    catch (Exception ex)
                    {
                        Debug.Log("Failed to validate TCP mapping :\n" + ex.ToString());
                    }
                }
            }, null);
        }
        catch (Exception ex)
        {
            Debug.Log("Failed to map port :\n" + ex.ToString());
        }
    }