UpnpMapper.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. //
  2. // Authors:
  3. // Nicholas Terry <nick.i.terry@gmail.com>
  4. //
  5. // Copyright (C) 2014 Nicholas Terry
  6. //
  7. // Permission is hereby granted, free of charge, to any person obtaining
  8. // a copy of this software and associated documentation files (the
  9. // "Software"), to deal in the Software without restriction, including
  10. // without limitation the rights to use, copy, modify, merge, publish,
  11. // distribute, sublicense, and/or sell copies of the Software, and to
  12. // permit persons to whom the Software is furnished to do so, subject to
  13. // the following conditions:
  14. //
  15. // The above copyright notice and this permission notice shall be
  16. // included in all copies or substantial portions of the Software.
  17. //
  18. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  19. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  20. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  21. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  22. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  23. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  24. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  25. //
  26. using System;
  27. using System.Collections.Generic;
  28. using System.Diagnostics;
  29. using System.Linq;
  30. using System.Net;
  31. using System.Net.Sockets;
  32. using System.Text;
  33. using System.Threading;
  34. using MediaBrowser.Model.Logging;
  35. namespace Mono.Nat.Upnp.Mappers
  36. {
  37. internal class UpnpMapper : Upnp, IMapper
  38. {
  39. public event EventHandler<DeviceEventArgs> DeviceFound;
  40. public UdpClient Client { get; set; }
  41. public UpnpMapper(ILogger logger)
  42. : base(logger)
  43. {
  44. //Bind to local port 1900 for ssdp responses
  45. Client = new UdpClient(1900);
  46. }
  47. public void Map(IPAddress gatewayAddress)
  48. {
  49. //Get the httpu request payload
  50. byte[] data = DiscoverDeviceMessage.EncodeUnicast(gatewayAddress);
  51. Client.Send(data, data.Length, new IPEndPoint(gatewayAddress, 1900));
  52. new Thread(Receive).Start();
  53. }
  54. public void Receive()
  55. {
  56. while (true)
  57. {
  58. IPEndPoint received = new IPEndPoint(IPAddress.Parse("192.168.0.1"), 5351);
  59. if (Client.Available > 0)
  60. {
  61. IPAddress localAddress = ((IPEndPoint)Client.Client.LocalEndPoint).Address;
  62. byte[] data = Client.Receive(ref received);
  63. Handle(localAddress, data, received);
  64. }
  65. }
  66. }
  67. public void Handle(IPAddress localAddres, byte[] response)
  68. {
  69. Handle(localAddres, response, null);
  70. }
  71. public void Handle(IPAddress localAddress, byte[] response, IPEndPoint endpoint)
  72. {
  73. // No matter what, this method should never throw an exception. If something goes wrong
  74. // we should still be in a position to handle the next reply correctly.
  75. try
  76. {
  77. UpnpNatDevice d = base.Handle(localAddress, response, endpoint);
  78. d.GetServicesList(DeviceSetupComplete);
  79. }
  80. catch (Exception ex)
  81. {
  82. Logger.ErrorException("Error mapping port. Data string: {0}", ex, Encoding.UTF8.GetString(response));
  83. }
  84. }
  85. private void DeviceSetupComplete(INatDevice device)
  86. {
  87. OnDeviceFound(new DeviceEventArgs(device));
  88. }
  89. private void OnDeviceFound(DeviceEventArgs args)
  90. {
  91. if (DeviceFound != null)
  92. DeviceFound(this, args);
  93. }
  94. }
  95. }