UpnpSearcher.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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 MediaBrowser.Model.Logging;
  40. using MediaBrowser.Model.Dlna;
  41. namespace Mono.Nat
  42. {
  43. internal class UpnpSearcher : ISearcher
  44. {
  45. public event EventHandler<DeviceEventArgs> DeviceFound;
  46. public event EventHandler<DeviceEventArgs> DeviceLost;
  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 void Handle(IPAddress localAddress, UpnpDeviceInfo deviceInfo, IPEndPoint endpoint)
  59. {
  60. // No matter what, this method should never throw an exception. If something goes wrong
  61. // we should still be in a position to handle the next reply correctly.
  62. try
  63. {
  64. /* For UPnP Port Mapping we need ot find either WANPPPConnection or WANIPConnection.
  65. Any other device type is no good to us for this purpose. See the IGP overview paper
  66. page 5 for an overview of device types and their hierarchy.
  67. http://upnp.org/specs/gw/UPnP-gw-InternetGatewayDevice-v1-Device.pdf */
  68. /* TODO: Currently we are assuming version 1 of the protocol. We should figure out which
  69. version it is and apply the correct URN. */
  70. /* Some routers don't correctly implement the version ID on the URN, so we only search for the type
  71. prefix. */
  72. // We have an internet gateway device now
  73. UpnpNatDevice d = new UpnpNatDevice(localAddress, deviceInfo, endpoint, string.Empty, _logger, _httpClient);
  74. NatUtility.Log("Fetching service list: {0}", d.HostEndPoint);
  75. OnDeviceFound(new DeviceEventArgs(d));
  76. }
  77. catch (Exception ex)
  78. {
  79. _logger.ErrorException("Error decoding device response", ex);
  80. }
  81. }
  82. public void Handle(IPAddress localAddress, byte[] response, IPEndPoint endpoint)
  83. {
  84. }
  85. public DateTime NextSearch
  86. {
  87. get { return nextSearch; }
  88. }
  89. private void OnDeviceFound(DeviceEventArgs args)
  90. {
  91. if (DeviceFound != null)
  92. DeviceFound(this, args);
  93. }
  94. public NatProtocol Protocol
  95. {
  96. get { return NatProtocol.Upnp; }
  97. }
  98. }
  99. }