Exemplo n.º 1
0
        /// <summary>
        /// Get list of forwarded port on UPnP gateway
        /// </summary>
        /// <returns></returns>
        public List <PortForwading> ListForwardedPort()
        {
            if (string.IsNullOrEmpty(_serviceUrl))
            {
                throw new Exception("No UPnP service available or Discover() has not been called");
            }

            List <PortForwading> forwadedPort = new List <PortForwading>();

            for (int index = 0; ; ++index)
            {
                try {
                    XmlDocument xdoc = _GetSOAPRequest(
                        _serviceUrl,
                        "<m:GetGenericPortMappingEntry xmlns:m=\"urn:schemas-upnp-org:service:WANIPConnection:1\">" +
                        "<NewPortMappingIndex>" + index + "</NewPortMappingIndex>" +
                        "</m:GetGenericPortMappingEntry>",
                        "GetGenericPortMappingEntry");
                    XmlNamespaceManager nsMgr = new XmlNamespaceManager(xdoc.NameTable);
                    nsMgr.AddNamespace("tns", "urn:schemas-upnp-org:device-1-0");
                    PortForwading port = new PortForwading();
                    XmlNode       node = xdoc.SelectSingleNode("//NewInternalClient/text()", nsMgr);
                    if (node != null)
                    {
                        port.internalIP = IPAddress.Parse(node.Value);
                    }
                    node = xdoc.SelectSingleNode("//NewInternalPort/text()", nsMgr);
                    if (node != null)
                    {
                        port.internalPort = int.Parse(node.Value);
                    }
                    node = xdoc.SelectSingleNode("//NewExternalPort/text()", nsMgr);
                    if (node != null)
                    {
                        port.externalPort = int.Parse(node.Value);
                    }
                    node = xdoc.SelectSingleNode("//NewProtocol/text()", nsMgr);
                    if (node != null)
                    {
                        port.protocol = (ProtocolType)Enum.Parse(typeof(ProtocolType), node.Value, true);
                    }
                    node = xdoc.SelectSingleNode("//NewPortMappingDescription/text()", nsMgr);
                    if (node != null)
                    {
                        port.description = node.Value;
                    }
                    node = xdoc.SelectSingleNode("//NewEnabled/text()", nsMgr);
                    if (node != null)
                    {
                        port.enabled = node.Value != "0";
                    }
                    forwadedPort.Add(port);
                }
                catch (WebException) {
                    break;
                }
            }
            return(forwadedPort);
        }
Exemplo n.º 2
0
	/// <summary>
	/// Get list of forwarded port on UPnP gateway
	/// </summary>
	/// <returns></returns>
	public List<PortForwading> ListForwardedPort()
	{
		if (string.IsNullOrEmpty(_serviceUrl))
			throw new Exception("No UPnP service available or Discover() has not been called");

		List<PortForwading> forwadedPort = new List<PortForwading>();
		for (int index = 0; ; ++index)
		{
			try
			{
				XmlDocument xdoc = _GetSOAPRequest(
					_serviceUrl,
					"<m:GetGenericPortMappingEntry xmlns:m=\"urn:schemas-upnp-org:service:WANIPConnection:1\">" +
					"<NewPortMappingIndex>" + index + "</NewPortMappingIndex>" +
					"</m:GetGenericPortMappingEntry>",
					"GetGenericPortMappingEntry");
				XmlNamespaceManager nsMgr = new XmlNamespaceManager(xdoc.NameTable);
				nsMgr.AddNamespace("tns", "urn:schemas-upnp-org:device-1-0");
				PortForwading port = new PortForwading();
				XmlNode node = xdoc.SelectSingleNode("//NewInternalClient/text()", nsMgr);
				if (node != null)
					port.internalIP = IPAddress.Parse(node.Value);
				node = xdoc.SelectSingleNode("//NewInternalPort/text()", nsMgr);
				if (node != null)
					port.internalPort = int.Parse(node.Value);
				node = xdoc.SelectSingleNode("//NewExternalPort/text()", nsMgr);
				if (node != null)
					port.externalPort = int.Parse(node.Value);
				node = xdoc.SelectSingleNode("//NewProtocol/text()", nsMgr);
				if (node != null)
					port.protocol = (ProtocolType)Enum.Parse(typeof(ProtocolType), node.Value, true);
				node = xdoc.SelectSingleNode("//NewPortMappingDescription/text()", nsMgr);
				if (node != null)
					port.description = node.Value;
				node = xdoc.SelectSingleNode("//NewEnabled/text()", nsMgr);
				if (node != null)
					port.enabled = node.Value != "0";
				forwadedPort.Add(port);
			}
			catch (WebException)
			{
				break;
			}
		}
		return forwadedPort;
	}