UpnpNatDevice.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. /// <summary>
  83. /// The EndPoint that the device is at
  84. /// </summary>
  85. internal EndPoint HostEndPoint
  86. {
  87. get { return this.hostEndPoint; }
  88. }
  89. /// <summary>
  90. /// The relative url of the xml file that describes the list of services is at
  91. /// </summary>
  92. internal string ServiceDescriptionUrl
  93. {
  94. get { return this.serviceDescriptionUrl; }
  95. }
  96. /// <summary>
  97. /// The relative url that we can use to control the port forwarding
  98. /// </summary>
  99. internal string ControlUrl
  100. {
  101. get { return this.controlUrl; }
  102. }
  103. /// <summary>
  104. /// The service type we're using on the device
  105. /// </summary>
  106. public string ServiceType
  107. {
  108. get { return serviceType; }
  109. }
  110. public override Task CreatePortMap(Mapping mapping)
  111. {
  112. CreatePortMappingMessage message = new CreatePortMappingMessage(mapping, localAddress, this);
  113. return _httpClient.SendAsync(message.Encode(), message.Method);
  114. }
  115. public override bool Equals(object obj)
  116. {
  117. UpnpNatDevice device = obj as UpnpNatDevice;
  118. return (device == null) ? false : this.Equals((device));
  119. }
  120. public bool Equals(UpnpNatDevice other)
  121. {
  122. return (other == null) ? false : (this.hostEndPoint.Equals(other.hostEndPoint)
  123. //&& this.controlUrl == other.controlUrl
  124. && this.serviceDescriptionUrl == other.serviceDescriptionUrl);
  125. }
  126. public override int GetHashCode()
  127. {
  128. return (this.hostEndPoint.GetHashCode() ^ this.controlUrl.GetHashCode() ^ this.serviceDescriptionUrl.GetHashCode());
  129. }
  130. /// <summary>
  131. /// Overridden.
  132. /// </summary>
  133. /// <returns></returns>
  134. public override string ToString()
  135. {
  136. //GetExternalIP is blocking and can throw exceptions, can't use it here.
  137. return String.Format(
  138. "UpnpNatDevice - EndPoint: {0}, External IP: {1}, Control Url: {2}, Service Description Url: {3}, Service Type: {4}, Last Seen: {5}",
  139. this.hostEndPoint, "Manually Check" /*this.GetExternalIP()*/, this.controlUrl, this.serviceDescriptionUrl, this.serviceType, this.LastSeen);
  140. }
  141. }
  142. }