UpnpMapper.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. namespace Mono.Nat.Upnp.Mappers
  35. {
  36. internal class UpnpMapper : Upnp, IMapper
  37. {
  38. public event EventHandler<DeviceEventArgs> DeviceFound;
  39. public UdpClient Client { get; set; }
  40. public UpnpMapper()
  41. {
  42. //Bind to local port 1900 for ssdp responses
  43. Client = new UdpClient(1900);
  44. }
  45. public void Map(IPAddress gatewayAddress)
  46. {
  47. //Get the httpu request payload
  48. byte[] data = DiscoverDeviceMessage.EncodeUnicast(gatewayAddress);
  49. Client.Send(data, data.Length, new IPEndPoint(gatewayAddress, 1900));
  50. new Thread(Receive).Start();
  51. }
  52. public void Receive()
  53. {
  54. while (true)
  55. {
  56. IPEndPoint received = new IPEndPoint(IPAddress.Parse("192.168.0.1"), 5351);
  57. if (Client.Available > 0)
  58. {
  59. IPAddress localAddress = ((IPEndPoint)Client.Client.LocalEndPoint).Address;
  60. byte[] data = Client.Receive(ref received);
  61. Handle(localAddress, data, received);
  62. }
  63. }
  64. }
  65. public void Handle(IPAddress localAddres, byte[] response)
  66. {
  67. Handle(localAddres, response, null);
  68. }
  69. public void Handle(IPAddress localAddress, byte[] response, IPEndPoint endpoint)
  70. {
  71. // No matter what, this method should never throw an exception. If something goes wrong
  72. // we should still be in a position to handle the next reply correctly.
  73. try
  74. {
  75. UpnpNatDevice d = base.Handle(localAddress, response, endpoint);
  76. d.GetServicesList(DeviceSetupComplete);
  77. }
  78. catch (Exception ex)
  79. {
  80. Trace.WriteLine("Unhandled exception when trying to decode a device's response Send me the following data: ");
  81. Trace.WriteLine("ErrorMessage:");
  82. Trace.WriteLine(ex.Message);
  83. Trace.WriteLine("Data string:");
  84. Trace.WriteLine(Encoding.UTF8.GetString(response));
  85. }
  86. }
  87. private void DeviceSetupComplete(INatDevice device)
  88. {
  89. OnDeviceFound(new DeviceEventArgs(device));
  90. }
  91. private void OnDeviceFound(DeviceEventArgs args)
  92. {
  93. if (DeviceFound != null)
  94. DeviceFound(this, args);
  95. }
  96. }
  97. }