2
0

UpnpMessage.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. //
  2. // Authors:
  3. // Alan McGovern alan.mcgovern@gmail.com
  4. //
  5. // Copyright (C) 2006 Alan McGovern
  6. //
  7. // Permission is hereby granted, free of charge, to any person obtaining
  8. // a copy of this software and associated documentation files (the
  9. // "Software"), to deal in the Software without restriction, including
  10. // without limitation the rights to use, copy, modify, merge, publish,
  11. // distribute, sublicense, and/or sell copies of the Software, and to
  12. // permit persons to whom the Software is furnished to do so, subject to
  13. // the following conditions:
  14. //
  15. // The above copyright notice and this permission notice shall be
  16. // included in all copies or substantial portions of the Software.
  17. //
  18. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  19. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  20. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  21. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  22. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  23. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  24. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  25. //
  26. using System;
  27. using System.Diagnostics;
  28. using System.Xml;
  29. using System.Net;
  30. using System.IO;
  31. using System.Text;
  32. using System.Globalization;
  33. using MediaBrowser.Common.Net;
  34. namespace Mono.Nat.Upnp
  35. {
  36. internal abstract class MessageBase
  37. {
  38. internal static readonly CultureInfo Culture = CultureInfo.InvariantCulture;
  39. protected UpnpNatDevice device;
  40. protected MessageBase(UpnpNatDevice device)
  41. {
  42. this.device = device;
  43. }
  44. protected WebRequest CreateRequest(string upnpMethod, string methodParameters, out byte[] body)
  45. {
  46. string ss = "http://" + this.device.HostEndPoint.ToString() + this.device.ControlUrl;
  47. NatUtility.Log("Initiating request to: {0}", ss);
  48. Uri location = new Uri(ss);
  49. HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(location);
  50. req.KeepAlive = false;
  51. req.Method = "POST";
  52. req.ContentType = "text/xml; charset=\"utf-8\"";
  53. req.Headers.Add("SOAPACTION", "\"" + device.ServiceType + "#" + upnpMethod + "\"");
  54. string bodyString = "<s:Envelope "
  55. + "xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\" "
  56. + "s:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">"
  57. + "<s:Body>"
  58. + "<u:" + upnpMethod + " "
  59. + "xmlns:u=\"" + device.ServiceType + "\">"
  60. + methodParameters
  61. + "</u:" + upnpMethod + ">"
  62. + "</s:Body>"
  63. + "</s:Envelope>\r\n\r\n";
  64. body = System.Text.Encoding.UTF8.GetBytes(bodyString);
  65. return req;
  66. }
  67. protected HttpRequestOptions CreateRequest(string upnpMethod, string methodParameters)
  68. {
  69. string ss = "http://" + this.device.HostEndPoint.ToString() + this.device.ControlUrl;
  70. NatUtility.Log("Initiating request to: {0}", ss);
  71. var req = new HttpRequestOptions();
  72. req.Url = ss;
  73. req.EnableKeepAlive = false;
  74. req.RequestContentType = "text/xml; charset=\"utf-8\"";
  75. req.RequestHeaders.Add("SOAPACTION", "\"" + device.ServiceType + "#" + upnpMethod + "\"");
  76. string bodyString = "<s:Envelope "
  77. + "xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\" "
  78. + "s:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">"
  79. + "<s:Body>"
  80. + "<u:" + upnpMethod + " "
  81. + "xmlns:u=\"" + device.ServiceType + "\">"
  82. + methodParameters
  83. + "</u:" + upnpMethod + ">"
  84. + "</s:Body>"
  85. + "</s:Envelope>\r\n\r\n";
  86. req.RequestContentBytes = System.Text.Encoding.UTF8.GetBytes(bodyString);
  87. return req;
  88. }
  89. public static MessageBase Decode(UpnpNatDevice device, string message)
  90. {
  91. XmlNode node;
  92. XmlDocument doc = new XmlDocument();
  93. doc.LoadXml(message);
  94. XmlNamespaceManager nsm = new XmlNamespaceManager(doc.NameTable);
  95. // Error messages should be found under this namespace
  96. nsm.AddNamespace("errorNs", "urn:schemas-upnp-org:control-1-0");
  97. nsm.AddNamespace("responseNs", device.ServiceType);
  98. // Check to see if we have a fault code message.
  99. if ((node = doc.SelectSingleNode("//errorNs:UPnPError", nsm)) != null) {
  100. string errorCode = node["errorCode"] != null ? node["errorCode"].InnerText : "";
  101. string errorDescription = node["errorDescription"] != null ? node["errorDescription"].InnerText : "";
  102. return new ErrorMessage(Convert.ToInt32(errorCode, CultureInfo.InvariantCulture), errorDescription);
  103. }
  104. if ((doc.SelectSingleNode("//responseNs:AddPortMappingResponse", nsm)) != null)
  105. return new CreatePortMappingResponseMessage();
  106. if ((doc.SelectSingleNode("//responseNs:DeletePortMappingResponse", nsm)) != null)
  107. return new DeletePortMapResponseMessage();
  108. if ((node = doc.SelectSingleNode("//responseNs:GetExternalIPAddressResponse", nsm)) != null) {
  109. string newExternalIPAddress = node["NewExternalIPAddress"] != null ? node["NewExternalIPAddress"].InnerText : "";
  110. return new GetExternalIPAddressResponseMessage(newExternalIPAddress);
  111. }
  112. if ((node = doc.SelectSingleNode("//responseNs:GetGenericPortMappingEntryResponse", nsm)) != null)
  113. return new GetGenericPortMappingEntryResponseMessage(node, true);
  114. if ((node = doc.SelectSingleNode("//responseNs:GetSpecificPortMappingEntryResponse", nsm)) != null)
  115. return new GetGenericPortMappingEntryResponseMessage(node, false);
  116. NatUtility.Log("Unknown message returned. Please send me back the following XML:");
  117. NatUtility.Log(message);
  118. return null;
  119. }
  120. public abstract HttpRequestOptions Encode();
  121. public abstract WebRequest Encode(out byte[] body);
  122. public virtual string Method
  123. {
  124. get { return "POST"; }
  125. }
  126. internal static void WriteFullElement(XmlWriter writer, string element, string value)
  127. {
  128. writer.WriteStartElement(element);
  129. writer.WriteString(value);
  130. writer.WriteEndElement();
  131. }
  132. internal static XmlWriter CreateWriter(StringBuilder sb)
  133. {
  134. XmlWriterSettings settings = new XmlWriterSettings();
  135. settings.ConformanceLevel = ConformanceLevel.Fragment;
  136. return XmlWriter.Create(sb, settings);
  137. }
  138. }
  139. }