Ejemplo n.º 1
0
        public static MessageBase Decode(UpnpNatDevice device, string message)
        {
            XmlNode     node;
            XmlDocument doc = new XmlDocument();

            doc.LoadXml(message);

            XmlNamespaceManager nsm = new XmlNamespaceManager(doc.NameTable);

            // Error messages should be found under this namespace
            nsm.AddNamespace("errorNs", "urn:schemas-upnp-org:control-1-0");
            nsm.AddNamespace("responseNs", device.ServiceType);

            // Check to see if we have a fault code message.
            if ((node = doc.SelectSingleNode("//errorNs:UPnPError", nsm)) != null)
            {
                string errorCode        = node["errorCode"] != null ? node["errorCode"].InnerText : "";
                string errorDescription = node["errorDescription"] != null ? node["errorDescription"].InnerText : "";

                return(new ErrorMessage(Convert.ToInt32(errorCode, CultureInfo.InvariantCulture), errorDescription));
            }

            if ((doc.SelectSingleNode("//responseNs:AddPortMappingResponse", nsm)) != null)
            {
                return(new CreatePortMappingResponseMessage());
            }

            if ((doc.SelectSingleNode("//responseNs:DeletePortMappingResponse", nsm)) != null)
            {
                return(new DeletePortMapResponseMessage());
            }

            if ((node = doc.SelectSingleNode("//responseNs:GetExternalIPAddressResponse", nsm)) != null)
            {
                string newExternalIPAddress = node["NewExternalIPAddress"] != null ? node["NewExternalIPAddress"].InnerText : "";
                return(new GetExternalIPAddressResponseMessage(newExternalIPAddress));
            }

            if ((node = doc.SelectSingleNode("//responseNs:GetGenericPortMappingEntryResponse", nsm)) != null)
            {
                return(new GetGenericPortMappingEntryResponseMessage(node, true));
            }

            if ((node = doc.SelectSingleNode("//responseNs:GetSpecificPortMappingEntryResponse", nsm)) != null)
            {
                return(new GetGenericPortMappingEntryResponseMessage(node, false));
            }

            NatUtility.Log("Unknown message returned. Please send me back the following XML:");
            NatUtility.Log(message);
            return(null);
        }
        public static ResponseMessage Decode(UpnpNatDevice device, string message)
        {
            XmlNode     node;
            XmlDocument doc = new XmlDocument();

            doc.LoadXml(message);

            XmlNamespaceManager nsm = new XmlNamespaceManager(doc.NameTable);

            // Error messages should be found under this namespace
            nsm.AddNamespace("errorNs", "urn:schemas-upnp-org:control-1-0");
            nsm.AddNamespace("responseNs", device.ServiceType);

            // Check to see if we have a fault code message.
            if ((node = doc.SelectSingleNode("//errorNs:UPnPError", nsm)) != null)
            {
                string errorCode        = node["errorCode"] != null ? node["errorCode"].InnerText : "";
                string errorDescription = node["errorDescription"] != null ? node["errorDescription"].InnerText : "";

                throw new MappingException((ErrorCode)int.Parse(errorCode), errorDescription);
            }

            if ((doc.SelectSingleNode("//responseNs:AddPortMappingResponse", nsm)) != null)
            {
                return(new CreatePortMappingResponseMessage());
            }

            if ((doc.SelectSingleNode("//responseNs:DeletePortMappingResponse", nsm)) != null)
            {
                return(new DeletePortMapResponseMessage());
            }

            if ((node = doc.SelectSingleNode("//responseNs:GetExternalIPAddressResponse", nsm)) != null)
            {
                return(new GetExternalIPAddressResponseMessage(node));
            }

            if ((node = doc.SelectSingleNode("//responseNs:GetGenericPortMappingEntryResponse", nsm)) != null)
            {
                return(new GetGenericPortMappingEntryResponseMessage(node));
            }

            if ((node = doc.SelectSingleNode("//responseNs:GetSpecificPortMappingEntryResponse", nsm)) != null)
            {
                return(new GetSpecificPortMappingEntryResponseMessage(node));
            }

            Log.ErrorFormatted("Unknown message returned. Please send me back the following XML: {0}", message);
            return(null);
        }
Ejemplo n.º 3
0
        public virtual Task <UpnpNatDevice> Handle(IPAddress localAddress, byte[] response, IPEndPoint endpoint)
        {
            // Convert it to a string for easy parsing
            string dataString = null;


            string urn;

            dataString = Encoding.UTF8.GetString(response);

            if (NatUtility.Verbose)
            {
                NatUtility.Log("UPnP Response: {0}", dataString);
            }

            /* For UPnP Port Mapping we need ot find either WANPPPConnection or WANIPConnection.
             *  Any other device type is no good to us for this purpose. See the IGP overview paper
             *  page 5 for an overview of device types and their hierarchy.
             *  http://upnp.org/specs/gw/UPnP-gw-InternetGatewayDevice-v1-Device.pdf */

            /* TODO: Currently we are assuming version 1 of the protocol. We should figure out which
             *  version it is and apply the correct URN. */

            /* Some routers don't correctly implement the version ID on the URN, so we only search for the type
             *  prefix. */

            string           log = "UPnP Response: Router advertised a '{0}' service";
            StringComparison c   = StringComparison.OrdinalIgnoreCase;

            if (dataString.IndexOf("urn:schemas-upnp-org:service:WANIPConnection:", c) != -1)
            {
                urn = "urn:schemas-upnp-org:service:WANIPConnection:1";
                NatUtility.Log(log, "urn:schemas-upnp-org:service:WANIPConnection:1");
            }
            else if (dataString.IndexOf("urn:schemas-upnp-org:service:WANPPPConnection:", c) != -1)
            {
                urn = "urn:schemas-upnp-org:service:WANPPPConnection:1";
                NatUtility.Log(log, "urn:schemas-upnp-org:service:WANPPPConnection:");
            }
            else
            {
                throw new NotSupportedException("Received non-supported device type");
            }

            // We have an internet gateway device now
            var device = new UpnpNatDevice(localAddress, dataString, urn, Logger, HttpClient);

            return(Task.FromResult(device));
        }
Ejemplo n.º 4
0
 public GetSpecificPortMappingEntryMessage(Protocol protocol, int externalPort, UpnpNatDevice device)
     : base(device, "GetSpecificPortMappingEntry")
 {
     Protocol     = protocol;
     ExternalPort = externalPort;
 }
Ejemplo n.º 5
0
 protected RequestMessage(UpnpNatDevice device, string requestType)
 {
     Device      = device;
     RequestType = requestType;
 }
Ejemplo n.º 6
0
 public GetExternalIPAddressMessage(UpnpNatDevice device)
     : base(device)
 {
 }
 public CreatePortMappingMessage(Mapping mapping, IPAddress localIpAddress, UpnpNatDevice device)
     : base(device, "AddPortMapping")
 {
     Mapping        = mapping;
     LocalIpAddress = localIpAddress;
 }
Ejemplo n.º 8
0
 protected MessageBase(UpnpNatDevice device)
 {
     this.device = device;
 }
Ejemplo n.º 9
0
 public DeletePortMappingMessage(Mapping mapping, UpnpNatDevice device)
     : base(device, "DeletePortMapping")
 {
     Mapping = mapping;
 }
Ejemplo n.º 10
0
 public CreatePortMappingMessage(Mapping mapping, IPAddress localIpAddress, UpnpNatDevice device)
     : base(device)
 {
     this.mapping        = mapping;
     this.localIpAddress = localIpAddress;
 }
Ejemplo n.º 11
0
 public DeletePortMappingMessage(Mapping mapping, UpnpNatDevice device)
     : base(device)
 {
     this.mapping = mapping;
 }
 public GetGenericPortMappingEntry(int index, UpnpNatDevice device)
     : base(device, "GetGenericPortMappingEntry")
 {
     Index = index;
 }
Ejemplo n.º 13
0
 public GetGenericPortMappingEntry(int index, UpnpNatDevice device)
     : base(device)
 {
     this.index = index;
 }