GetMyAddress() public static method

Gets my local IP address (not necessarily external) and subnet mask
public static GetMyAddress ( IPAddress &mask ) : IPAddress
mask System.Net.IPAddress
return System.Net.IPAddress
Exemplo n.º 1
0
        /// <summary>
        /// Add a forwarding rule to the router using UPnP
        /// </summary>
        public bool ForwardPort(int port, string description)
        {
            if (m_serviceUrl == null)
            {
                return(false);
            }

            IPAddress mask;
            var       client = NetUtility.GetMyAddress(out mask);

            try
            {
                XmlDocument xdoc = SOAPRequest(m_serviceUrl,
                                               "<u:AddPortMapping xmlns:u=\"urn:schemas-upnp-org:service:WANIPConnection:1\">" +
                                               "<NewRemoteHost></NewRemoteHost><NewExternalPort>" + port.ToString() + "</NewExternalPort>" +
                                               "<NewProtocol>" + ProtocolType.Udp.ToString().ToUpper() + "</NewProtocol>" +
                                               "<NewInternalPort>" + port.ToString() + "</NewInternalPort>" +
                                               "<NewInternalClient>" + client.ToString() + "</NewInternalClient>" +
                                               "<NewEnabled>1</NewEnabled>" +
                                               "<NewPortMappingDescription>" + description + "</NewPortMappingDescription>" +
                                               "<NewLeaseDuration>0</NewLeaseDuration>" +
                                               "</u:AddPortMapping>",
                                               "AddPortMapping");

                m_peer.LogDebug("Sent UPnP port forward request");
                System.Threading.Thread.Sleep(50);
            }
            catch (Exception ex)
            {
                m_peer.LogWarning("UPnP port forward failed: " + ex.Message);
                return(false);
            }
            return(true);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Add a forwarding rule to the router using UPnP
        /// </summary>
        /// <param name="externalPort">The external, WAN facing, port</param>
        /// <param name="description">A description for the port forwarding rule</param>
        /// <param name="internalPort">The port on the client machine to send traffic to</param>
        public bool ForwardPort(int externalPort, string description, int internalPort = 0)
        {
            if (!CheckAvailability())
            {
                return(false);
            }

            IPAddress mask;
            var       client = NetUtility.GetMyAddress(out mask);

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

            if (internalPort == 0)
            {
                internalPort = externalPort;
            }

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

                m_peer.LogDebug("Sent UPnP port forward request");
                NetUtility.Sleep(50);
            }
            catch (Exception ex)
            {
                m_peer.LogWarning("UPnP port forward failed: " + ex.Message);
                return(false);
            }
            return(true);
        }