Exemplo n.º 1
0
        /// <summary>
        /// Add a forwarding rule to the router using UPnP
        /// </summary>
        public bool ForwardPort(int port, string description)
        {
            if (!CheckAvailability())
            {
                return(false);
            }

            var client = NetUtility.GetSelfAddresses();

            if (client == null)
            {
                return(false);
            }

            bool success = false;

            lock (m_discoveryResults) {
                for (int i = 0; i < m_discoveryResults.Count; i++)
                {
                    ref DiscoveryResult result = ref m_discoveryResults.Data[i];

                    try {
                        SOAPRequest(result.ServiceUrl,
                                    "<u:AddPortMapping xmlns:u=\"urn:schemas-upnp-org:service:" + result.ServiceName + ":1\">" +
                                    "<NewRemoteHost></NewRemoteHost>" +
                                    "<NewExternalPort>" + port.ToString() + "</NewExternalPort>" +
                                    "<NewProtocol>" + ProtocolType.Udp.ToString().ToUpper(System.Globalization.CultureInfo.InvariantCulture) + "</NewProtocol>" +
                                    "<NewInternalPort>" + port.ToString() + "</NewInternalPort>" +
                                    "<NewInternalClient>" + client.ToString() + "</NewInternalClient>" +
                                    "<NewEnabled>1</NewEnabled>" +
                                    "<NewPortMappingDescription>" + description + "</NewPortMappingDescription>" +
                                    "<NewLeaseDuration>0</NewLeaseDuration>" +
                                    "</u:AddPortMapping>",
                                    "AddPortMapping", result.ServiceName);

                        success = true;
                        m_peer.LogDebug("Sent UPnP port forward request");
                    } catch (Exception ex) {
                        m_peer.LogVerbose("UPnP port forward failed: " + ex.Message);
                    }
                }
            }