UpnpNatDevice.cs 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. //
  2. // Authors:
  3. // Alan McGovern alan.mcgovern@gmail.com
  4. // Ben Motmans <ben.motmans@gmail.com>
  5. //
  6. // Copyright (C) 2006 Alan McGovern
  7. // Copyright (C) 2007 Ben Motmans
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining
  10. // a copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. using System;
  29. using System.IO;
  30. using System.Net;
  31. using System.Xml;
  32. using System.Text;
  33. using System.Diagnostics;
  34. using System.Threading.Tasks;
  35. using MediaBrowser.Common.Net;
  36. using MediaBrowser.Model.Logging;
  37. using MediaBrowser.Model.Dlna;
  38. namespace Mono.Nat.Upnp
  39. {
  40. public sealed class UpnpNatDevice : AbstractNatDevice, IEquatable<UpnpNatDevice>
  41. {
  42. private EndPoint hostEndPoint;
  43. private IPAddress localAddress;
  44. private string serviceDescriptionUrl;
  45. private string controlUrl;
  46. private string serviceType;
  47. private readonly ILogger _logger;
  48. private readonly IHttpClient _httpClient;
  49. public override IPAddress LocalAddress
  50. {
  51. get { return localAddress; }
  52. }
  53. internal UpnpNatDevice(IPAddress localAddress, UpnpDeviceInfo deviceInfo, IPEndPoint hostEndPoint, string serviceType, ILogger logger, IHttpClient httpClient)
  54. {
  55. this.LastSeen = DateTime.Now;
  56. this.localAddress = localAddress;
  57. // Split the string at the "location" section so i can extract the ipaddress and service description url
  58. string locationDetails = deviceInfo.Location.ToString();
  59. this.serviceType = serviceType;
  60. _logger = logger;
  61. _httpClient = httpClient;
  62. // Make sure we have no excess whitespace
  63. locationDetails = locationDetails.Trim();
  64. // FIXME: Is this reliable enough. What if we get a hostname as opposed to a proper http address
  65. // Are we going to get addresses with the "http://" attached?
  66. if (locationDetails.StartsWith("http://", StringComparison.OrdinalIgnoreCase))
  67. {
  68. NatUtility.Log("Found device at: {0}", locationDetails);
  69. // This bit strings out the "http://" from the string
  70. locationDetails = locationDetails.Substring(7);
  71. this.hostEndPoint = hostEndPoint;
  72. NatUtility.Log("Parsed device as: {0}", this.hostEndPoint.ToString());
  73. // The service description URL is the remainder of the "locationDetails" string. The bit that was originally after the ip
  74. // and port information
  75. this.serviceDescriptionUrl = locationDetails.Substring(locationDetails.IndexOf('/'));
  76. }
  77. else
  78. {
  79. NatUtility.Log("Couldn't decode address. Please send following string to the developer: ");
  80. }
  81. }
  82. internal UpnpNatDevice(IPAddress localAddress, string deviceDetails, string serviceType, ILogger logger, IHttpClient httpClient)
  83. {
  84. _logger = logger;
  85. _httpClient = httpClient;
  86. this.LastSeen = DateTime.Now;
  87. this.localAddress = localAddress;
  88. // Split the string at the "location" section so i can extract the ipaddress and service description url
  89. string locationDetails = deviceDetails.Substring(deviceDetails.IndexOf("Location", StringComparison.OrdinalIgnoreCase) + 9).Split('\r')[0];
  90. this.serviceType = serviceType;
  91. // Make sure we have no excess whitespace
  92. locationDetails = locationDetails.Trim();
  93. // FIXME: Is this reliable enough. What if we get a hostname as opposed to a proper http address
  94. // Are we going to get addresses with the "http://" attached?
  95. if (locationDetails.StartsWith("http://", StringComparison.OrdinalIgnoreCase))
  96. {
  97. NatUtility.Log("Found device at: {0}", locationDetails);
  98. // This bit strings out the "http://" from the string
  99. locationDetails = locationDetails.Substring(7);
  100. // We then split off the end of the string to get something like: 192.168.0.3:241 in our string
  101. string hostAddressAndPort = locationDetails.Remove(locationDetails.IndexOf('/'));
  102. // From this we parse out the IP address and Port
  103. if (hostAddressAndPort.IndexOf(':') > 0)
  104. {
  105. this.hostEndPoint = new IPEndPoint(IPAddress.Parse(hostAddressAndPort.Remove(hostAddressAndPort.IndexOf(':'))),
  106. Convert.ToUInt16(hostAddressAndPort.Substring(hostAddressAndPort.IndexOf(':') + 1), System.Globalization.CultureInfo.InvariantCulture));
  107. }
  108. else
  109. {
  110. // there is no port specified, use default port (80)
  111. this.hostEndPoint = new IPEndPoint(IPAddress.Parse(hostAddressAndPort), 80);
  112. }
  113. NatUtility.Log("Parsed device as: {0}", this.hostEndPoint.ToString());
  114. // The service description URL is the remainder of the "locationDetails" string. The bit that was originally after the ip
  115. // and port information
  116. this.serviceDescriptionUrl = locationDetails.Substring(locationDetails.IndexOf('/'));
  117. }
  118. else
  119. {
  120. logger.Warn("Couldn't decode address: " + deviceDetails);
  121. }
  122. }
  123. /// <summary>
  124. /// The EndPoint that the device is at
  125. /// </summary>
  126. internal EndPoint HostEndPoint
  127. {
  128. get { return this.hostEndPoint; }
  129. }
  130. /// <summary>
  131. /// The relative url of the xml file that describes the list of services is at
  132. /// </summary>
  133. internal string ServiceDescriptionUrl
  134. {
  135. get { return this.serviceDescriptionUrl; }
  136. }
  137. /// <summary>
  138. /// The relative url that we can use to control the port forwarding
  139. /// </summary>
  140. internal string ControlUrl
  141. {
  142. get { return this.controlUrl; }
  143. }
  144. /// <summary>
  145. /// The service type we're using on the device
  146. /// </summary>
  147. public string ServiceType
  148. {
  149. get { return serviceType; }
  150. }
  151. public override Task CreatePortMap(Mapping mapping)
  152. {
  153. CreatePortMappingMessage message = new CreatePortMappingMessage(mapping, localAddress, this);
  154. return _httpClient.SendAsync(message.Encode(), message.Method);
  155. }
  156. public override bool Equals(object obj)
  157. {
  158. UpnpNatDevice device = obj as UpnpNatDevice;
  159. return (device == null) ? false : this.Equals((device));
  160. }
  161. public bool Equals(UpnpNatDevice other)
  162. {
  163. return (other == null) ? false : (this.hostEndPoint.Equals(other.hostEndPoint)
  164. //&& this.controlUrl == other.controlUrl
  165. && this.serviceDescriptionUrl == other.serviceDescriptionUrl);
  166. }
  167. public override int GetHashCode()
  168. {
  169. return (this.hostEndPoint.GetHashCode() ^ this.controlUrl.GetHashCode() ^ this.serviceDescriptionUrl.GetHashCode());
  170. }
  171. /// <summary>
  172. /// Overridden.
  173. /// </summary>
  174. /// <returns></returns>
  175. public override string ToString()
  176. {
  177. //GetExternalIP is blocking and can throw exceptions, can't use it here.
  178. return String.Format(
  179. "UpnpNatDevice - EndPoint: {0}, External IP: {1}, Control Url: {2}, Service Description Url: {3}, Service Type: {4}, Last Seen: {5}",
  180. this.hostEndPoint, "Manually Check" /*this.GetExternalIP()*/, this.controlUrl, this.serviceDescriptionUrl, this.serviceType, this.LastSeen);
  181. }
  182. }
  183. }