UpnpMessage.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. namespace Mono.Nat.Upnp
  34. {
  35. internal abstract class MessageBase
  36. {
  37. internal static readonly CultureInfo Culture = CultureInfo.InvariantCulture;
  38. protected UpnpNatDevice device;
  39. protected MessageBase(UpnpNatDevice device)
  40. {
  41. this.device = device;
  42. }
  43. protected WebRequest CreateRequest(string upnpMethod, string methodParameters, out byte[] body)
  44. {
  45. string ss = "http://" + this.device.HostEndPoint.ToString() + this.device.ControlUrl;
  46. NatUtility.Log("Initiating request to: {0}", ss);
  47. Uri location = new Uri(ss);
  48. HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(location);
  49. req.KeepAlive = false;
  50. req.Method = "POST";
  51. req.ContentType = "text/xml; charset=\"utf-8\"";
  52. req.Headers.Add("SOAPACTION", "\"" + device.ServiceType + "#" + upnpMethod + "\"");
  53. string bodyString = "<s:Envelope "
  54. + "xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\" "
  55. + "s:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">"
  56. + "<s:Body>"
  57. + "<u:" + upnpMethod + " "
  58. + "xmlns:u=\"" + device.ServiceType + "\">"
  59. + methodParameters
  60. + "</u:" + upnpMethod + ">"
  61. + "</s:Body>"
  62. + "</s:Envelope>\r\n\r\n";
  63. body = System.Text.Encoding.UTF8.GetBytes(bodyString);
  64. return req;
  65. }
  66. public static MessageBase Decode(UpnpNatDevice device, string message)
  67. {
  68. XmlNode node;
  69. XmlDocument doc = new XmlDocument();
  70. doc.LoadXml(message);
  71. XmlNamespaceManager nsm = new XmlNamespaceManager(doc.NameTable);
  72. // Error messages should be found under this namespace
  73. nsm.AddNamespace("errorNs", "urn:schemas-upnp-org:control-1-0");
  74. nsm.AddNamespace("responseNs", device.ServiceType);
  75. // Check to see if we have a fault code message.
  76. if ((node = doc.SelectSingleNode("//errorNs:UPnPError", nsm)) != null) {
  77. string errorCode = node["errorCode"] != null ? node["errorCode"].InnerText : "";
  78. string errorDescription = node["errorDescription"] != null ? node["errorDescription"].InnerText : "";
  79. return new ErrorMessage(Convert.ToInt32(errorCode, CultureInfo.InvariantCulture), errorDescription);
  80. }
  81. if ((doc.SelectSingleNode("//responseNs:AddPortMappingResponse", nsm)) != null)
  82. return new CreatePortMappingResponseMessage();
  83. if ((doc.SelectSingleNode("//responseNs:DeletePortMappingResponse", nsm)) != null)
  84. return new DeletePortMapResponseMessage();
  85. if ((node = doc.SelectSingleNode("//responseNs:GetExternalIPAddressResponse", nsm)) != null) {
  86. string newExternalIPAddress = node["NewExternalIPAddress"] != null ? node["NewExternalIPAddress"].InnerText : "";
  87. return new GetExternalIPAddressResponseMessage(newExternalIPAddress);
  88. }
  89. if ((node = doc.SelectSingleNode("//responseNs:GetGenericPortMappingEntryResponse", nsm)) != null)
  90. return new GetGenericPortMappingEntryResponseMessage(node, true);
  91. if ((node = doc.SelectSingleNode("//responseNs:GetSpecificPortMappingEntryResponse", nsm)) != null)
  92. return new GetGenericPortMappingEntryResponseMessage(node, false);
  93. NatUtility.Log("Unknown message returned. Please send me back the following XML:");
  94. NatUtility.Log(message);
  95. return null;
  96. }
  97. public abstract WebRequest Encode(out byte[] body);
  98. internal static void WriteFullElement(XmlWriter writer, string element, string value)
  99. {
  100. writer.WriteStartElement(element);
  101. writer.WriteString(value);
  102. writer.WriteEndElement();
  103. }
  104. internal static XmlWriter CreateWriter(StringBuilder sb)
  105. {
  106. XmlWriterSettings settings = new XmlWriterSettings();
  107. settings.ConformanceLevel = ConformanceLevel.Fragment;
  108. return XmlWriter.Create(sb, settings);
  109. }
  110. }
  111. }