UpnpSearcher.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. //
  2. // Authors:
  3. // Alan McGovern alan.mcgovern@gmail.com
  4. // Ben Motmans <ben.motmans@gmail.com>
  5. // Nicholas Terry <nick.i.terry@gmail.com>
  6. //
  7. // Copyright (C) 2006 Alan McGovern
  8. // Copyright (C) 2007 Ben Motmans
  9. // Copyright (C) 2014 Nicholas Terry
  10. //
  11. // Permission is hereby granted, free of charge, to any person obtaining
  12. // a copy of this software and associated documentation files (the
  13. // "Software"), to deal in the Software without restriction, including
  14. // without limitation the rights to use, copy, modify, merge, publish,
  15. // distribute, sublicense, and/or sell copies of the Software, and to
  16. // permit persons to whom the Software is furnished to do so, subject to
  17. // the following conditions:
  18. //
  19. // The above copyright notice and this permission notice shall be
  20. // included in all copies or substantial portions of the Software.
  21. //
  22. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  23. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  24. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  25. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  26. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  27. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  28. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  29. //
  30. using System;
  31. using System.Collections.Generic;
  32. using System.Text;
  33. using System.Net;
  34. using Mono.Nat.Upnp;
  35. using System.Diagnostics;
  36. using System.Net.Sockets;
  37. using System.Net.NetworkInformation;
  38. using MediaBrowser.Controller.Dlna;
  39. namespace Mono.Nat
  40. {
  41. internal class UpnpSearcher : ISearcher
  42. {
  43. private const int SearchPeriod = 5 * 60; // The time in seconds between each search
  44. static UpnpSearcher instance = new UpnpSearcher();
  45. public static List<UdpClient> sockets = CreateSockets();
  46. public static UpnpSearcher Instance
  47. {
  48. get { return instance; }
  49. }
  50. public event EventHandler<DeviceEventArgs> DeviceFound;
  51. public event EventHandler<DeviceEventArgs> DeviceLost;
  52. private List<INatDevice> devices;
  53. private Dictionary<IPAddress, DateTime> lastFetched;
  54. private DateTime nextSearch;
  55. private IPEndPoint searchEndpoint;
  56. UpnpSearcher()
  57. {
  58. devices = new List<INatDevice>();
  59. lastFetched = new Dictionary<IPAddress, DateTime>();
  60. //searchEndpoint = new IPEndPoint(IPAddress.Parse("239.255.255.250"), 1900);
  61. searchEndpoint = new IPEndPoint(IPAddress.Parse("239.255.255.250"), 1900);
  62. }
  63. static List<UdpClient> CreateSockets()
  64. {
  65. List<UdpClient> clients = new List<UdpClient>();
  66. try
  67. {
  68. foreach (NetworkInterface n in NetworkInterface.GetAllNetworkInterfaces())
  69. {
  70. foreach (UnicastIPAddressInformation address in n.GetIPProperties().UnicastAddresses)
  71. {
  72. if (address.Address.AddressFamily == AddressFamily.InterNetwork)
  73. {
  74. try
  75. {
  76. clients.Add(new UdpClient(new IPEndPoint(address.Address, 0)));
  77. }
  78. catch
  79. {
  80. continue; // Move on to the next address.
  81. }
  82. }
  83. }
  84. }
  85. }
  86. catch (Exception)
  87. {
  88. clients.Add(new UdpClient(0));
  89. }
  90. return clients;
  91. }
  92. public void Search()
  93. {
  94. foreach (UdpClient s in sockets)
  95. {
  96. try
  97. {
  98. Search(s);
  99. }
  100. catch
  101. {
  102. // Ignore any search errors
  103. }
  104. }
  105. }
  106. void Search(UdpClient client)
  107. {
  108. nextSearch = DateTime.Now.AddSeconds(SearchPeriod);
  109. byte[] data = DiscoverDeviceMessage.EncodeSSDP();
  110. // UDP is unreliable, so send 3 requests at a time (per Upnp spec, sec 1.1.2)
  111. for (int i = 0; i < 3; i++)
  112. client.Send(data, data.Length, searchEndpoint);
  113. }
  114. public IPEndPoint SearchEndpoint
  115. {
  116. get { return searchEndpoint; }
  117. }
  118. public void Handle(IPAddress localAddress, UpnpDeviceInfo deviceInfo, IPEndPoint endpoint)
  119. {
  120. // No matter what, this method should never throw an exception. If something goes wrong
  121. // we should still be in a position to handle the next reply correctly.
  122. try
  123. {
  124. /* For UPnP Port Mapping we need ot find either WANPPPConnection or WANIPConnection.
  125. Any other device type is no good to us for this purpose. See the IGP overview paper
  126. page 5 for an overview of device types and their hierarchy.
  127. http://upnp.org/specs/gw/UPnP-gw-InternetGatewayDevice-v1-Device.pdf */
  128. /* TODO: Currently we are assuming version 1 of the protocol. We should figure out which
  129. version it is and apply the correct URN. */
  130. /* Some routers don't correctly implement the version ID on the URN, so we only search for the type
  131. prefix. */
  132. // We have an internet gateway device now
  133. UpnpNatDevice d = new UpnpNatDevice(localAddress, deviceInfo, endpoint, string.Empty);
  134. if (devices.Contains(d))
  135. {
  136. // We already have found this device, so we just refresh it to let people know it's
  137. // Still alive. If a device doesn't respond to a search, we dump it.
  138. devices[devices.IndexOf(d)].LastSeen = DateTime.Now;
  139. }
  140. else
  141. {
  142. // If we send 3 requests at a time, ensure we only fetch the services list once
  143. // even if three responses are received
  144. if (lastFetched.ContainsKey(endpoint.Address))
  145. {
  146. DateTime last = lastFetched[endpoint.Address];
  147. if ((DateTime.Now - last) < TimeSpan.FromSeconds(20))
  148. return;
  149. }
  150. lastFetched[endpoint.Address] = DateTime.Now;
  151. // Once we've parsed the information we need, we tell the device to retrieve it's service list
  152. // Once we successfully receive the service list, the callback provided will be invoked.
  153. NatUtility.Log("Fetching service list: {0}", d.HostEndPoint);
  154. d.GetServicesList(DeviceSetupComplete);
  155. }
  156. }
  157. catch (Exception ex)
  158. {
  159. NatUtility.Log("Unhandled exception when trying to decode a device's response Send me the following data: ");
  160. NatUtility.Log("ErrorMessage:");
  161. NatUtility.Log(ex.Message);
  162. }
  163. }
  164. public void Handle(IPAddress localAddress, byte[] response, IPEndPoint endpoint)
  165. {
  166. // Convert it to a string for easy parsing
  167. string dataString = null;
  168. // No matter what, this method should never throw an exception. If something goes wrong
  169. // we should still be in a position to handle the next reply correctly.
  170. try {
  171. string urn;
  172. dataString = Encoding.UTF8.GetString(response);
  173. if (NatUtility.Verbose)
  174. NatUtility.Log("UPnP Response: {0}", dataString);
  175. /* For UPnP Port Mapping we need ot find either WANPPPConnection or WANIPConnection.
  176. Any other device type is no good to us for this purpose. See the IGP overview paper
  177. page 5 for an overview of device types and their hierarchy.
  178. http://upnp.org/specs/gw/UPnP-gw-InternetGatewayDevice-v1-Device.pdf */
  179. /* TODO: Currently we are assuming version 1 of the protocol. We should figure out which
  180. version it is and apply the correct URN. */
  181. /* Some routers don't correctly implement the version ID on the URN, so we only search for the type
  182. prefix. */
  183. string log = "UPnP Response: Router advertised a '{0}' service";
  184. StringComparison c = StringComparison.OrdinalIgnoreCase;
  185. if (dataString.IndexOf("urn:schemas-upnp-org:service:WANIPConnection:", c) != -1) {
  186. urn = "urn:schemas-upnp-org:service:WANIPConnection:1";
  187. NatUtility.Log(log, "urn:schemas-upnp-org:service:WANIPConnection:1");
  188. } else if (dataString.IndexOf("urn:schemas-upnp-org:service:WANPPPConnection:", c) != -1) {
  189. urn = "urn:schemas-upnp-org:service:WANPPPConnection:1";
  190. NatUtility.Log(log, "urn:schemas-upnp-org:service:WANPPPConnection:");
  191. } else
  192. return;
  193. // We have an internet gateway device now
  194. UpnpNatDevice d = new UpnpNatDevice(localAddress, dataString, urn);
  195. if (devices.Contains(d))
  196. {
  197. // We already have found this device, so we just refresh it to let people know it's
  198. // Still alive. If a device doesn't respond to a search, we dump it.
  199. devices[devices.IndexOf(d)].LastSeen = DateTime.Now;
  200. }
  201. else
  202. {
  203. // If we send 3 requests at a time, ensure we only fetch the services list once
  204. // even if three responses are received
  205. if (lastFetched.ContainsKey(endpoint.Address))
  206. {
  207. DateTime last = lastFetched[endpoint.Address];
  208. if ((DateTime.Now - last) < TimeSpan.FromSeconds(20))
  209. return;
  210. }
  211. lastFetched[endpoint.Address] = DateTime.Now;
  212. // Once we've parsed the information we need, we tell the device to retrieve it's service list
  213. // Once we successfully receive the service list, the callback provided will be invoked.
  214. NatUtility.Log("Fetching service list: {0}", d.HostEndPoint);
  215. d.GetServicesList(DeviceSetupComplete);
  216. }
  217. }
  218. catch (Exception ex)
  219. {
  220. Trace.WriteLine("Unhandled exception when trying to decode a device's response Send me the following data: ");
  221. Trace.WriteLine("ErrorMessage:");
  222. Trace.WriteLine(ex.Message);
  223. Trace.WriteLine("Data string:");
  224. Trace.WriteLine(dataString);
  225. }
  226. }
  227. public DateTime NextSearch
  228. {
  229. get { return nextSearch; }
  230. }
  231. private void DeviceSetupComplete(INatDevice device)
  232. {
  233. lock (this.devices)
  234. {
  235. // We don't want the same device in there twice
  236. if (devices.Contains(device))
  237. return;
  238. devices.Add(device);
  239. }
  240. OnDeviceFound(new DeviceEventArgs(device));
  241. }
  242. private void OnDeviceFound(DeviceEventArgs args)
  243. {
  244. if (DeviceFound != null)
  245. DeviceFound(this, args);
  246. }
  247. public NatProtocol Protocol
  248. {
  249. get { return NatProtocol.Upnp; }
  250. }
  251. }
  252. }