UpnpSearcher.cs 3.8 KB

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