PmpSearcher.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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.Pmp;
  35. using System.Net.NetworkInformation;
  36. using System.Net.Sockets;
  37. using System.Linq;
  38. namespace Mono.Nat
  39. {
  40. internal class PmpSearcher : Pmp.Pmp, ISearcher
  41. {
  42. static PmpSearcher instance = new PmpSearcher();
  43. public static PmpSearcher Instance
  44. {
  45. get { return instance; }
  46. }
  47. private int timeout;
  48. private DateTime nextSearch;
  49. public event EventHandler<DeviceEventArgs> DeviceFound;
  50. public event EventHandler<DeviceEventArgs> DeviceLost;
  51. static PmpSearcher()
  52. {
  53. CreateSocketsAndAddGateways();
  54. }
  55. PmpSearcher()
  56. {
  57. timeout = 250;
  58. }
  59. public void Search()
  60. {
  61. foreach (UdpClient s in sockets)
  62. {
  63. try
  64. {
  65. Search(s);
  66. }
  67. catch
  68. {
  69. // Ignore any search errors
  70. }
  71. }
  72. }
  73. void Search (UdpClient client)
  74. {
  75. // Sort out the time for the next search first. The spec says the
  76. // timeout should double after each attempt. Once it reaches 64 seconds
  77. // (and that attempt fails), assume no devices available
  78. nextSearch = DateTime.Now.AddMilliseconds(timeout);
  79. timeout *= 2;
  80. // We've tried 9 times as per spec, try searching again in 5 minutes
  81. if (timeout == 128 * 1000)
  82. {
  83. timeout = 250;
  84. nextSearch = DateTime.Now.AddMinutes(10);
  85. return;
  86. }
  87. // The nat-pmp search message. Must be sent to GatewayIP:53531
  88. byte[] buffer = new byte[] { PmpConstants.Version, PmpConstants.OperationCode };
  89. foreach (IPEndPoint gatewayEndpoint in gatewayLists[client])
  90. client.Send(buffer, buffer.Length, gatewayEndpoint);
  91. }
  92. bool IsSearchAddress(IPAddress address)
  93. {
  94. foreach (List<IPEndPoint> gatewayList in gatewayLists.Values)
  95. foreach (IPEndPoint gatewayEndpoint in gatewayList)
  96. if (gatewayEndpoint.Address.Equals(address))
  97. return true;
  98. return false;
  99. }
  100. public void Handle(IPAddress localAddress, byte[] response, IPEndPoint endpoint)
  101. {
  102. if (!IsSearchAddress(endpoint.Address))
  103. return;
  104. if (response.Length != 12)
  105. return;
  106. if (response[0] != PmpConstants.Version)
  107. return;
  108. if (response[1] != PmpConstants.ServerNoop)
  109. return;
  110. int errorcode = IPAddress.NetworkToHostOrder(BitConverter.ToInt16(response, 2));
  111. if (errorcode != 0)
  112. NatUtility.Log("Non zero error: {0}", errorcode);
  113. IPAddress publicIp = new IPAddress(new byte[] { response[8], response[9], response[10], response[11] });
  114. nextSearch = DateTime.Now.AddMinutes(5);
  115. timeout = 250;
  116. OnDeviceFound(new DeviceEventArgs(new PmpNatDevice(endpoint.Address, publicIp)));
  117. }
  118. public DateTime NextSearch
  119. {
  120. get { return nextSearch; }
  121. }
  122. private void OnDeviceFound(DeviceEventArgs args)
  123. {
  124. if (DeviceFound != null)
  125. DeviceFound(this, args);
  126. }
  127. public NatProtocol Protocol
  128. {
  129. get { return NatProtocol.Pmp; }
  130. }
  131. }
  132. }