SsdpCommunicationsServer.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Net;
  5. using System.Net.Http;
  6. using System.Text;
  7. using System.Threading;
  8. using System.Threading.Tasks;
  9. using MediaBrowser.Common.Net;
  10. using Microsoft.Extensions.Logging;
  11. using MediaBrowser.Model.Net;
  12. namespace Rssdp.Infrastructure
  13. {
  14. /// <summary>
  15. /// Provides the platform independent logic for publishing device existence and responding to search requests.
  16. /// </summary>
  17. public sealed class SsdpCommunicationsServer : DisposableManagedObjectBase, ISsdpCommunicationsServer
  18. {
  19. #region Fields
  20. /* We could technically use one socket listening on port 1900 for everything.
  21. * This should get both multicast (notifications) and unicast (search response) messages, however
  22. * this often doesn't work under Windows because the MS SSDP service is running. If that service
  23. * is running then it will steal the unicast messages and we will never see search responses.
  24. * Since stopping the service would be a bad idea (might not be allowed security wise and might
  25. * break other apps running on the system) the only other work around is to use two sockets.
  26. *
  27. * We use one socket to listen for/receive notifications and search requests (_BroadcastListenSocket).
  28. * We use a second socket, bound to a different local port, to send search requests and listen for
  29. * responses (_SendSocket). The responses are sent to the local port this socket is bound to,
  30. * which isn't port 1900 so the MS service doesn't steal them. While the caller can specify a local
  31. * port to use, we will default to 0 which allows the underlying system to auto-assign a free port.
  32. */
  33. private object _BroadcastListenSocketSynchroniser = new object();
  34. private ISocket _BroadcastListenSocket;
  35. private object _SendSocketSynchroniser = new object();
  36. private List<ISocket> _sendSockets;
  37. private HttpRequestParser _RequestParser;
  38. private HttpResponseParser _ResponseParser;
  39. private readonly ILogger _logger;
  40. private ISocketFactory _SocketFactory;
  41. private readonly INetworkManager _networkManager;
  42. private int _LocalPort;
  43. private int _MulticastTtl;
  44. private bool _IsShared;
  45. private readonly bool _enableMultiSocketBinding;
  46. #endregion
  47. #region Events
  48. /// <summary>
  49. /// Raised when a HTTPU request message is received by a socket (unicast or multicast).
  50. /// </summary>
  51. public event EventHandler<RequestReceivedEventArgs> RequestReceived;
  52. /// <summary>
  53. /// Raised when an HTTPU response message is received by a socket (unicast or multicast).
  54. /// </summary>
  55. public event EventHandler<ResponseReceivedEventArgs> ResponseReceived;
  56. #endregion
  57. #region Constructors
  58. /// <summary>
  59. /// Minimum constructor.
  60. /// </summary>
  61. /// <exception cref="ArgumentNullException">The <paramref name="socketFactory"/> argument is null.</exception>
  62. public SsdpCommunicationsServer(ISocketFactory socketFactory, INetworkManager networkManager, ILogger logger, bool enableMultiSocketBinding)
  63. : this(socketFactory, 0, SsdpConstants.SsdpDefaultMulticastTimeToLive, networkManager, logger, enableMultiSocketBinding)
  64. {
  65. }
  66. /// <summary>
  67. /// Full constructor.
  68. /// </summary>
  69. /// <exception cref="ArgumentNullException">The <paramref name="socketFactory"/> argument is null.</exception>
  70. /// <exception cref="ArgumentOutOfRangeException">The <paramref name="multicastTimeToLive"/> argument is less than or equal to zero.</exception>
  71. public SsdpCommunicationsServer(ISocketFactory socketFactory, int localPort, int multicastTimeToLive, INetworkManager networkManager, ILogger logger, bool enableMultiSocketBinding)
  72. {
  73. if (socketFactory == null) throw new ArgumentNullException(nameof(socketFactory));
  74. if (multicastTimeToLive <= 0) throw new ArgumentOutOfRangeException(nameof(multicastTimeToLive), "multicastTimeToLive must be greater than zero.");
  75. _BroadcastListenSocketSynchroniser = new object();
  76. _SendSocketSynchroniser = new object();
  77. _LocalPort = localPort;
  78. _SocketFactory = socketFactory;
  79. _RequestParser = new HttpRequestParser();
  80. _ResponseParser = new HttpResponseParser();
  81. _MulticastTtl = multicastTimeToLive;
  82. _networkManager = networkManager;
  83. _logger = logger;
  84. _enableMultiSocketBinding = enableMultiSocketBinding;
  85. }
  86. #endregion
  87. #region Public Methods
  88. /// <summary>
  89. /// Causes the server to begin listening for multicast messages, being SSDP search requests and notifications.
  90. /// </summary>
  91. /// <exception cref="ObjectDisposedException">Thrown if the <see cref="DisposableManagedObjectBase.IsDisposed"/> property is true (because <seealso cref="DisposableManagedObjectBase.Dispose()" /> has been called previously).</exception>
  92. public void BeginListeningForBroadcasts()
  93. {
  94. ThrowIfDisposed();
  95. if (_BroadcastListenSocket == null)
  96. {
  97. lock (_BroadcastListenSocketSynchroniser)
  98. {
  99. if (_BroadcastListenSocket == null)
  100. {
  101. try
  102. {
  103. _BroadcastListenSocket = ListenForBroadcastsAsync();
  104. }
  105. catch (Exception ex)
  106. {
  107. _logger.LogError(ex, "Error in BeginListeningForBroadcasts");
  108. }
  109. }
  110. }
  111. }
  112. }
  113. /// <summary>
  114. /// Causes the server to stop listening for multicast messages, being SSDP search requests and notifications.
  115. /// </summary>
  116. /// <exception cref="ObjectDisposedException">Thrown if the <see cref="DisposableManagedObjectBase.IsDisposed"/> property is true (because <seealso cref="DisposableManagedObjectBase.Dispose()" /> has been called previously).</exception>
  117. public void StopListeningForBroadcasts()
  118. {
  119. lock (_BroadcastListenSocketSynchroniser)
  120. {
  121. if (_BroadcastListenSocket != null)
  122. {
  123. _logger.LogInformation("{0} disposing _BroadcastListenSocket.", GetType().Name);
  124. _BroadcastListenSocket.Dispose();
  125. _BroadcastListenSocket = null;
  126. }
  127. }
  128. }
  129. /// <summary>
  130. /// Sends a message to a particular address (uni or multicast) and port.
  131. /// </summary>
  132. public async Task SendMessage(byte[] messageData, IpEndPointInfo destination, IpAddressInfo fromLocalIpAddress, CancellationToken cancellationToken)
  133. {
  134. if (messageData == null) throw new ArgumentNullException(nameof(messageData));
  135. ThrowIfDisposed();
  136. var sockets = GetSendSockets(fromLocalIpAddress, destination);
  137. if (sockets.Count == 0)
  138. {
  139. return;
  140. }
  141. // SSDP spec recommends sending messages multiple times (not more than 3) to account for possible packet loss over UDP.
  142. for (var i = 0; i < SsdpConstants.UdpResendCount; i++)
  143. {
  144. var tasks = sockets.Select(s => SendFromSocket(s, messageData, destination, cancellationToken)).ToArray();
  145. await Task.WhenAll(tasks).ConfigureAwait(false);
  146. await Task.Delay(100, cancellationToken).ConfigureAwait(false);
  147. }
  148. }
  149. private async Task SendFromSocket(ISocket socket, byte[] messageData, IpEndPointInfo destination, CancellationToken cancellationToken)
  150. {
  151. try
  152. {
  153. await socket.SendToAsync(messageData, 0, messageData.Length, destination, cancellationToken).ConfigureAwait(false);
  154. }
  155. catch (ObjectDisposedException)
  156. {
  157. }
  158. catch (OperationCanceledException)
  159. {
  160. }
  161. catch (Exception ex)
  162. {
  163. _logger.LogError(ex, "Error sending socket message from {0} to {1}", socket.LocalIPAddress.ToString(), destination.ToString());
  164. }
  165. }
  166. private List<ISocket> GetSendSockets(IpAddressInfo fromLocalIpAddress, IpEndPointInfo destination)
  167. {
  168. EnsureSendSocketCreated();
  169. lock (_SendSocketSynchroniser)
  170. {
  171. var sockets = _sendSockets.Where(i => i.LocalIPAddress.AddressFamily == fromLocalIpAddress.AddressFamily);
  172. // Send from the Any socket and the socket with the matching address
  173. if (fromLocalIpAddress.AddressFamily == IpAddressFamily.InterNetwork)
  174. {
  175. sockets = sockets.Where(i => i.LocalIPAddress.Equals(IpAddressInfo.Any) || fromLocalIpAddress.Equals(i.LocalIPAddress));
  176. // If sending to the loopback address, filter the socket list as well
  177. if (destination.IpAddress.Equals(IpAddressInfo.Loopback))
  178. {
  179. sockets = sockets.Where(i => i.LocalIPAddress.Equals(IpAddressInfo.Any) || i.LocalIPAddress.Equals(IpAddressInfo.Loopback));
  180. }
  181. }
  182. else if (fromLocalIpAddress.AddressFamily == IpAddressFamily.InterNetworkV6)
  183. {
  184. sockets = sockets.Where(i => i.LocalIPAddress.Equals(IpAddressInfo.IPv6Any) || fromLocalIpAddress.Equals(i.LocalIPAddress));
  185. // If sending to the loopback address, filter the socket list as well
  186. if (destination.IpAddress.Equals(IpAddressInfo.IPv6Loopback))
  187. {
  188. sockets = sockets.Where(i => i.LocalIPAddress.Equals(IpAddressInfo.IPv6Any) || i.LocalIPAddress.Equals(IpAddressInfo.IPv6Loopback));
  189. }
  190. }
  191. return sockets.ToList();
  192. }
  193. }
  194. public Task SendMulticastMessage(string message, CancellationToken cancellationToken)
  195. {
  196. return SendMulticastMessage(message, SsdpConstants.UdpResendCount, cancellationToken);
  197. }
  198. /// <summary>
  199. /// Sends a message to the SSDP multicast address and port.
  200. /// </summary>
  201. public async Task SendMulticastMessage(string message, int sendCount, CancellationToken cancellationToken)
  202. {
  203. if (message == null) throw new ArgumentNullException(nameof(message));
  204. byte[] messageData = Encoding.UTF8.GetBytes(message);
  205. ThrowIfDisposed();
  206. cancellationToken.ThrowIfCancellationRequested();
  207. EnsureSendSocketCreated();
  208. // SSDP spec recommends sending messages multiple times (not more than 3) to account for possible packet loss over UDP.
  209. for (var i = 0; i < sendCount; i++)
  210. {
  211. await SendMessageIfSocketNotDisposed(messageData, new IpEndPointInfo
  212. {
  213. IpAddress = new IpAddressInfo(SsdpConstants.MulticastLocalAdminAddress, IpAddressFamily.InterNetwork),
  214. Port = SsdpConstants.MulticastPort
  215. }, cancellationToken).ConfigureAwait(false);
  216. await Task.Delay(100, cancellationToken).ConfigureAwait(false);
  217. }
  218. }
  219. /// <summary>
  220. /// Stops listening for search responses on the local, unicast socket.
  221. /// </summary>
  222. /// <exception cref="ObjectDisposedException">Thrown if the <see cref="DisposableManagedObjectBase.IsDisposed"/> property is true (because <seealso cref="DisposableManagedObjectBase.Dispose()" /> has been called previously).</exception>
  223. public void StopListeningForResponses()
  224. {
  225. lock (_SendSocketSynchroniser)
  226. {
  227. if (_sendSockets != null)
  228. {
  229. var sockets = _sendSockets.ToList();
  230. _sendSockets = null;
  231. _logger.LogInformation("{0} Disposing {1} sendSockets", GetType().Name, sockets.Count);
  232. foreach (var socket in sockets)
  233. {
  234. _logger.LogInformation("{0} disposing sendSocket from {1}", GetType().Name, socket.LocalIPAddress);
  235. socket.Dispose();
  236. }
  237. }
  238. }
  239. }
  240. #endregion
  241. #region Public Properties
  242. /// <summary>
  243. /// Gets or sets a boolean value indicating whether or not this instance is shared amongst multiple <see cref="SsdpDeviceLocatorBase"/> and/or <see cref="ISsdpDevicePublisher"/> instances.
  244. /// </summary>
  245. /// <remarks>
  246. /// <para>If true, disposing an instance of a <see cref="SsdpDeviceLocatorBase"/>or a <see cref="ISsdpDevicePublisher"/> will not dispose this comms server instance. The calling code is responsible for managing the lifetime of the server.</para>
  247. /// </remarks>
  248. public bool IsShared
  249. {
  250. get { return _IsShared; }
  251. set { _IsShared = value; }
  252. }
  253. #endregion
  254. #region Overrides
  255. /// <summary>
  256. /// Stops listening for requests, disposes this instance and all internal resources.
  257. /// </summary>
  258. /// <param name="disposing"></param>
  259. protected override void Dispose(bool disposing)
  260. {
  261. if (disposing)
  262. {
  263. StopListeningForBroadcasts();
  264. StopListeningForResponses();
  265. }
  266. }
  267. #endregion
  268. #region Private Methods
  269. private Task SendMessageIfSocketNotDisposed(byte[] messageData, IpEndPointInfo destination, CancellationToken cancellationToken)
  270. {
  271. var sockets = _sendSockets;
  272. if (sockets != null)
  273. {
  274. sockets = sockets.ToList();
  275. var tasks = sockets.Select(s => SendFromSocket(s, messageData, destination, cancellationToken));
  276. return Task.WhenAll(tasks);
  277. }
  278. return Task.CompletedTask;
  279. }
  280. private ISocket ListenForBroadcastsAsync()
  281. {
  282. var socket = _SocketFactory.CreateUdpMulticastSocket(SsdpConstants.MulticastLocalAdminAddress, _MulticastTtl, SsdpConstants.MulticastPort);
  283. ListenToSocket(socket);
  284. return socket;
  285. }
  286. private List<ISocket> CreateSocketAndListenForResponsesAsync()
  287. {
  288. var sockets = new List<ISocket>();
  289. sockets.Add(_SocketFactory.CreateSsdpUdpSocket(IpAddressInfo.Any, _LocalPort));
  290. if (_enableMultiSocketBinding)
  291. {
  292. foreach (var address in _networkManager.GetLocalIpAddresses())
  293. {
  294. if (address.AddressFamily == IpAddressFamily.InterNetworkV6)
  295. {
  296. // Not supported ?
  297. continue;
  298. }
  299. try
  300. {
  301. sockets.Add(_SocketFactory.CreateSsdpUdpSocket(address, _LocalPort));
  302. }
  303. catch (Exception ex)
  304. {
  305. _logger.LogError(ex, "Error in CreateSsdpUdpSocket. IPAddress: {0}", address);
  306. }
  307. }
  308. }
  309. foreach (var socket in sockets)
  310. {
  311. ListenToSocket(socket);
  312. }
  313. return sockets;
  314. }
  315. [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1804:RemoveUnusedLocals", MessageId = "t", Justification = "Capturing task to local variable removes compiler warning, task is not otherwise required.")]
  316. private void ListenToSocket(ISocket socket)
  317. {
  318. // Tasks are captured to local variables even if we don't use them just to avoid compiler warnings.
  319. var t = Task.Run(() => ListenToSocketInternal(socket));
  320. }
  321. private async Task ListenToSocketInternal(ISocket socket)
  322. {
  323. var cancelled = false;
  324. var receiveBuffer = new byte[8192];
  325. while (!cancelled && !IsDisposed)
  326. {
  327. try
  328. {
  329. var result = await socket.ReceiveAsync(receiveBuffer, 0, receiveBuffer.Length, CancellationToken.None).ConfigureAwait(false);
  330. if (result.ReceivedBytes > 0)
  331. {
  332. // Strange cannot convert compiler error here if I don't explicitly
  333. // assign or cast to Action first. Assignment is easier to read,
  334. // so went with that.
  335. ProcessMessage(System.Text.UTF8Encoding.UTF8.GetString(result.Buffer, 0, result.ReceivedBytes), result.RemoteEndPoint, result.LocalIPAddress);
  336. }
  337. }
  338. catch (ObjectDisposedException)
  339. {
  340. cancelled = true;
  341. }
  342. catch (TaskCanceledException)
  343. {
  344. cancelled = true;
  345. }
  346. }
  347. }
  348. private void EnsureSendSocketCreated()
  349. {
  350. if (_sendSockets == null)
  351. {
  352. lock (_SendSocketSynchroniser)
  353. {
  354. if (_sendSockets == null)
  355. {
  356. _sendSockets = CreateSocketAndListenForResponsesAsync();
  357. }
  358. }
  359. }
  360. }
  361. private void ProcessMessage(string data, IpEndPointInfo endPoint, IpAddressInfo receivedOnLocalIpAddress)
  362. {
  363. //Responses start with the HTTP version, prefixed with HTTP/ while
  364. //requests start with a method which can vary and might be one we haven't
  365. //seen/don't know. We'll check if this message is a request or a response
  366. //by checking for the HTTP/ prefix on the start of the message.
  367. if (data.StartsWith("HTTP/", StringComparison.OrdinalIgnoreCase))
  368. {
  369. HttpResponseMessage responseMessage = null;
  370. try
  371. {
  372. responseMessage = _ResponseParser.Parse(data);
  373. }
  374. catch (ArgumentException)
  375. {
  376. // Ignore invalid packets.
  377. }
  378. if (responseMessage != null)
  379. OnResponseReceived(responseMessage, endPoint, receivedOnLocalIpAddress);
  380. }
  381. else
  382. {
  383. HttpRequestMessage requestMessage = null;
  384. try
  385. {
  386. requestMessage = _RequestParser.Parse(data);
  387. }
  388. catch (ArgumentException)
  389. {
  390. // Ignore invalid packets.
  391. }
  392. if (requestMessage != null)
  393. {
  394. OnRequestReceived(requestMessage, endPoint, receivedOnLocalIpAddress);
  395. }
  396. }
  397. }
  398. private void OnRequestReceived(HttpRequestMessage data, IpEndPointInfo remoteEndPoint, IpAddressInfo receivedOnLocalIpAddress)
  399. {
  400. //SSDP specification says only * is currently used but other uri's might
  401. //be implemented in the future and should be ignored unless understood.
  402. //Section 4.2 - http://tools.ietf.org/html/draft-cai-ssdp-v1-03#page-11
  403. if (data.RequestUri.ToString() != "*")
  404. {
  405. return;
  406. }
  407. var handlers = this.RequestReceived;
  408. if (handlers != null)
  409. handlers(this, new RequestReceivedEventArgs(data, remoteEndPoint, receivedOnLocalIpAddress));
  410. }
  411. private void OnResponseReceived(HttpResponseMessage data, IpEndPointInfo endPoint, IpAddressInfo localIpAddress)
  412. {
  413. var handlers = this.ResponseReceived;
  414. if (handlers != null)
  415. handlers(this, new ResponseReceivedEventArgs(data, endPoint)
  416. {
  417. LocalIpAddress = localIpAddress
  418. });
  419. }
  420. #endregion
  421. }
  422. }