UpnpSearcher.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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.Common.Net;
  39. using Microsoft.Extensions.Logging;
  40. using MediaBrowser.Model.Dlna;
  41. using System.Threading.Tasks;
  42. namespace Mono.Nat
  43. {
  44. internal class UpnpSearcher : ISearcher
  45. {
  46. public event EventHandler<DeviceEventArgs> DeviceFound;
  47. private DateTime nextSearch;
  48. private readonly ILogger _logger;
  49. private readonly IHttpClient _httpClient;
  50. public UpnpSearcher(ILogger logger, IHttpClient httpClient)
  51. {
  52. _logger = logger;
  53. _httpClient = httpClient;
  54. }
  55. public void Search()
  56. {
  57. }
  58. public async Task Handle(IPAddress localAddress, UpnpDeviceInfo deviceInfo, IPEndPoint endpoint)
  59. {
  60. if (localAddress == null)
  61. {
  62. throw new ArgumentNullException(nameof(localAddress));
  63. }
  64. try
  65. {
  66. /* For UPnP Port Mapping we need ot find either WANPPPConnection or WANIPConnection.
  67. * Any other device type is no good to us for this purpose. See the IGP overview paper
  68. * page 5 for an overview of device types and their hierarchy.
  69. * http://upnp.org/specs/gw/UPnP-gw-InternetGatewayDevice-v1-Device.pdf */
  70. /* TODO: Currently we are assuming version 1 of the protocol. We should figure out which
  71. * version it is and apply the correct URN. */
  72. /* Some routers don't correctly implement the version ID on the URN, so we only search for the type
  73. * prefix. */
  74. // We have an internet gateway device now
  75. var d = new UpnpNatDevice(localAddress, deviceInfo, endpoint, string.Empty, _logger, _httpClient);
  76. await d.GetServicesList().ConfigureAwait(false);
  77. OnDeviceFound(new DeviceEventArgs(d));
  78. }
  79. catch (Exception ex)
  80. {
  81. _logger.LogError(ex, "Error decoding device response");
  82. }
  83. }
  84. public void Handle(IPAddress localAddress, byte[] response, IPEndPoint endpoint)
  85. {
  86. }
  87. public DateTime NextSearch
  88. {
  89. get { return nextSearch; }
  90. }
  91. private void OnDeviceFound(DeviceEventArgs args)
  92. {
  93. if (DeviceFound != null)
  94. DeviceFound(this, args);
  95. }
  96. public NatProtocol Protocol
  97. {
  98. get { return NatProtocol.Upnp; }
  99. }
  100. }
  101. }