示例#1
0
 /// <summary>
 /// Get specific port mapping entry.
 /// </summary>
 /// <param name="service">Router WANIPConnection/WANPPPConnection service.</param>
 /// <param name="man">Append MAN HTTP Header If throw 405 WebException. MAN: "http://schemas.xmlsoap.org/soap/envelope/"; ns=01</param>
 /// <param name="externalPort">The external port to query.</param>
 /// <param name="protocol">The protocol to query. This property accepts the following protocol: TCP, UDP.</param>
 /// <returns></returns>
 /// <exception cref="Exception"/>
 public static PortMappingEntry GetSpecificPortMappingEntry(UPnPService service, bool man, int externalPort, string protocol)
 {
     KeyValuePair <string, string>[] arguments = new KeyValuePair <string, string>[] {
         new KeyValuePair <string, string>("NewRemoteHost", string.Empty),
         new KeyValuePair <string, string>("NewExternalPort", externalPort.ToString(CultureInfo.InvariantCulture)),
         new KeyValuePair <string, string>("NewProtocol", protocol)
     };
     try
     {
         string down = PostAction(service, man, "GetSpecificPortMappingEntry", arguments);
         return(new PortMappingEntry(true,
                                     IPAddress.Parse(GetResponseValue(down, "NewInternalClient")),
                                     int.Parse(GetResponseValue(down, "NewInternalPort"), CultureInfo.InvariantCulture),
                                     Convert.ToBoolean(int.Parse(GetResponseValue(down, "NewEnabled"), CultureInfo.InvariantCulture)),
                                     GetResponseValue(down, "NewPortMappingDescription")));
     }
     catch (WebException ex)
     {
         if (((HttpWebResponse)ex.Response).StatusCode == HttpStatusCode.InternalServerError)
         {
             return(new PortMappingEntry(false, null, 0, false, string.Empty));
         }
         else
         {
             throw;
         }
     }
 }
示例#2
0
        /// <summary>
        /// Get NAT RSIP status.
        /// </summary>
        /// <param name="service">Router WANIPConnection/WANPPPConnection service.</param>
        /// <param name="man">Append MAN HTTP Header If throw 405 WebException. MAN: "http://schemas.xmlsoap.org/soap/envelope/"; ns=01</param>
        /// <returns></returns>
        /// <exception cref="Exception"/>
        public static NatRsipStatus GetNATRSIPStatus(UPnPService service, bool man)
        {
            string down = PostAction(service, man, "GetNATRSIPStatus", null);

            return(new NatRsipStatus(Convert.ToBoolean(int.Parse(GetResponseValue(down, "NewRSIPAvailable"), CultureInfo.InvariantCulture)),
                                     Convert.ToBoolean(int.Parse(GetResponseValue(down, "NewNATEnabled"), CultureInfo.InvariantCulture))));
        }
示例#3
0
        /// <summary>
        /// Get external(internet) IPAddress.
        /// </summary>
        /// <param name="service">Router WANIPConnection/WANPPPConnection service.</param>
        /// <param name="man">Append MAN HTTP Header If throw 405 WebException. MAN: "http://schemas.xmlsoap.org/soap/envelope/"; ns=01</param>
        /// <returns></returns>
        /// <exception cref="Exception"/>
        public static IPAddress GetExternalIPAddress(UPnPService service, bool man)
        {
            string down = PostAction(service, man, "GetExternalIPAddress", null);
            string ip   = GetResponseValue(down, "NewExternalIPAddress");

            return(IPAddress.Parse(ip));
        }
示例#4
0
 /// <summary>
 /// Add port mapping.
 /// </summary>
 /// <param name="service">Router WANIPConnection/WANPPPConnection service.</param>
 /// <param name="man">Append MAN HTTP Header If throw 405 WebException. MAN: "http://schemas.xmlsoap.org/soap/envelope/"; ns=01</param>
 /// <param name="externalPort">The external port to mapping.</param>
 /// <param name="protocol">The protocol to mapping. This property accepts the following protocol: TCP, UDP.</param>
 /// <param name="internalPort">The public port to mapping.</param>
 /// <param name="internalIPAddress">The public IPAddress to mapping.</param>
 /// <param name="enabled">Enable.</param>
 /// <param name="leaseDuration">Lease duration. This property accepts the following 0 - 604800. Unit is seconds. Set 0 to permanents.</param>
 /// <param name="description">Mapping description.</param>
 /// <returns></returns>
 /// <exception cref="Exception"/>
 public static void AddPortMapping(UPnPService service,
                                   bool man,
                                   int externalPort,
                                   string protocol,
                                   int internalPort,
                                   IPAddress internalIPAddress,
                                   bool enabled,
                                   int leaseDuration,
                                   string description)
 {
     if (internalIPAddress is null)
     {
         throw new ArgumentNullException(nameof(internalIPAddress));
     }
     KeyValuePair <string, string>[] arguments = new KeyValuePair <string, string>[] {
         new KeyValuePair <string, string>("NewRemoteHost", string.Empty),
         new KeyValuePair <string, string>("NewExternalPort", externalPort.ToString(CultureInfo.InvariantCulture)),
         new KeyValuePair <string, string>("NewProtocol", protocol),
         new KeyValuePair <string, string>("NewInternalPort", internalPort.ToString(CultureInfo.InvariantCulture)),
         new KeyValuePair <string, string>("NewInternalClient", internalIPAddress.ToString()),
         new KeyValuePair <string, string>("NewEnabled", (enabled ? "1" : "0")),
         new KeyValuePair <string, string>("NewLeaseDuration", leaseDuration.ToString(CultureInfo.InvariantCulture)),
         new KeyValuePair <string, string>("NewPortMappingDescription", description)
     };
     PostAction(service, man, "AddPortMapping", arguments);
 }
示例#5
0
 /// <summary>
 /// Delete port mapping.
 /// </summary>
 /// <param name="service">Router WANIPConnection/WANPPPConnection service.</param>
 /// <param name="man">Append MAN HTTP Header If throw 405 WebException. MAN: "http://schemas.xmlsoap.org/soap/envelope/"; ns=01</param>
 /// <param name="externalPort">The external port to delete mapping.</param>
 /// <param name="protocol">The protocol to delete mapping. This property accepts the following protocol: TCP, UDP.</param>
 /// <returns></returns>
 /// <exception cref="Exception"/>
 public static void DeletePortMapping(UPnPService service, bool man, int externalPort, string protocol)
 {
     KeyValuePair <string, string>[] arguments = new KeyValuePair <string, string>[] {
         new KeyValuePair <string, string>("NewRemoteHost", string.Empty),
         new KeyValuePair <string, string>("NewExternalPort", externalPort.ToString(CultureInfo.InvariantCulture)),
         new KeyValuePair <string, string>("NewProtocol", protocol)
     };
     PostAction(service, man, "DeletePortMapping", arguments);
 }
示例#6
0
 public static string PostAction(UPnPService service, bool man, string action, params KeyValuePair <string, string>[] arguments)
 {
     if (service is null)
     {
         throw new ArgumentNullException(nameof(service));
     }
     using (WebClient wc = new WebClient())
     {
         StringBuilder sb = new StringBuilder();
         sb.Append("<?xml version=\"1.0\"?>\r\n");
         sb.Append("<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\" s:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">\r\n");
         sb.Append("  <s:Body>\r\n");
         sb.Append("    <u:" + action + " xmlns:u=\"" + service.ServiceType + "\">\r\n");
         if (arguments != null && arguments.Length > 0)
         {
             foreach (KeyValuePair <string, string> argument in arguments)
             {
                 sb.Append("      <" + argument.Key + ">" + argument.Value + "</" + argument.Key + ">\r\n");
             }
         }
         sb.Append("    </u:" + action + ">\r\n");
         sb.Append("  </s:Body>\r\n");
         sb.Append("</s:Envelope>\r\n");
         byte[] data = Encoding.UTF8.GetBytes(sb.ToString());
         wc.Encoding = Encoding.UTF8;
         wc.Headers.Add(HttpRequestHeader.UserAgent, "Mozilla/5.0(WindowsNT6.1;rv:2.0.1)Gecko/20100101Firefox/4.0.1");
         wc.Headers.Add(HttpRequestHeader.CacheControl, "no-store");
         wc.Headers.Add(HttpRequestHeader.Pragma, "no-cache");
         if (man)
         {
             wc.Headers.Add("MAN", "\"http://schemas.xmlsoap.org/soap/envelope/\"; ns=01");
         }
         wc.Headers.Add("SOAPAction", "\"" + service.ServiceType + "#" + action + "\"");
         byte[] down = wc.UploadData(service.ControlUrl, "POST", data);
         return(Encoding.UTF8.GetString(down));
     }
 }