StunClient5389UDP.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. using STUN.Enums;
  2. using STUN.Interfaces;
  3. using STUN.Message;
  4. using STUN.StunResult;
  5. using STUN.Utils;
  6. using System;
  7. using System.Diagnostics;
  8. using System.Linq;
  9. using System.Net;
  10. using System.Net.Sockets;
  11. using System.Threading.Tasks;
  12. namespace STUN.Client
  13. {
  14. /// <summary>
  15. /// https://tools.ietf.org/html/rfc5389#section-7.2.1
  16. /// https://tools.ietf.org/html/rfc5780#section-4.2
  17. /// </summary>
  18. public class StunClient5389UDP : StunClient3489
  19. {
  20. public StunClient5389UDP(string server, ushort port = 3478, IPEndPoint local = null, IDnsQuery dnsQuery = null)
  21. : base(server, port, local, dnsQuery)
  22. {
  23. Timeout = TimeSpan.FromSeconds(3);
  24. }
  25. public override IStunResult Query()
  26. {
  27. throw new NotImplementedException();
  28. }
  29. public override async Task<IStunResult> QueryAsync()
  30. {
  31. var (result, _) = await BindingTestAsync(RemoteEndPoint);
  32. return result;
  33. }
  34. public async Task<(StunResult5389, IPEndPoint)> BindingTestAsync(IPEndPoint remote)
  35. {
  36. BindingTestResult res;
  37. var test = new StunMessage5389 { StunMessageType = StunMessageType.BindingRequest };
  38. var (response1, _, local1) = await TestAsync(test, remote, remote);
  39. var mappedAddress1 = AttributeExtensions.GetXorMappedAddressAttribute(response1);
  40. var otherAddress = AttributeExtensions.GetOtherAddressAttribute(response1);
  41. if (response1 == null)
  42. {
  43. res = BindingTestResult.Fail;
  44. }
  45. else if (mappedAddress1 == null)
  46. {
  47. res = BindingTestResult.UnsupportedServer;
  48. }
  49. else
  50. {
  51. res = BindingTestResult.Success;
  52. }
  53. return (new StunResult5389
  54. {
  55. BindingTestResult = res,
  56. LocalEndPoint = local1 == null ? null : new IPEndPoint(local1, LocalEndPoint.Port),
  57. PublicEndPoint = mappedAddress1
  58. }, otherAddress);
  59. }
  60. public async Task<StunResult5389> MappingBehaviorTestAsync()
  61. {
  62. // test I
  63. var (result1, otherAddress) = await BindingTestAsync(RemoteEndPoint);
  64. if (result1.BindingTestResult != BindingTestResult.Success)
  65. {
  66. return result1;
  67. }
  68. if (otherAddress == null
  69. || Equals(otherAddress.Address, RemoteEndPoint.Address)
  70. || otherAddress.Port == RemoteEndPoint.Port)
  71. {
  72. result1.MappingBehavior = MappingBehavior.UnsupportedServer;
  73. return result1;
  74. }
  75. if (Equals(result1.PublicEndPoint, result1.LocalEndPoint))
  76. {
  77. result1.MappingBehavior = MappingBehavior.Direct;
  78. return result1;
  79. }
  80. // test II
  81. var (result2, _) = await BindingTestAsync(new IPEndPoint(otherAddress.Address, RemoteEndPoint.Port));
  82. if (result2.BindingTestResult != BindingTestResult.Success)
  83. {
  84. result1.MappingBehavior = MappingBehavior.Fail;
  85. return result1;
  86. }
  87. if (Equals(result2.PublicEndPoint, result1.PublicEndPoint))
  88. {
  89. result1.MappingBehavior = MappingBehavior.EndpointIndependent;
  90. return result1;
  91. }
  92. // test III
  93. var (result3, _) = await BindingTestAsync(otherAddress);
  94. if (result3.BindingTestResult != BindingTestResult.Success)
  95. {
  96. result1.MappingBehavior = MappingBehavior.Fail;
  97. return result1;
  98. }
  99. result1.MappingBehavior = Equals(result3.PublicEndPoint, result2.PublicEndPoint) ? MappingBehavior.AddressDependent : MappingBehavior.AddressAndPortDependent;
  100. return result1;
  101. }
  102. public async Task<StunResult5389> FilteringBehaviorTestAsync()
  103. {
  104. // test I
  105. var (result1, otherAddress) = await BindingTestAsync(RemoteEndPoint);
  106. if (result1.BindingTestResult != BindingTestResult.Success)
  107. {
  108. return result1;
  109. }
  110. if (otherAddress == null
  111. || Equals(otherAddress.Address, RemoteEndPoint.Address)
  112. || otherAddress.Port == RemoteEndPoint.Port)
  113. {
  114. result1.FilteringBehavior = FilteringBehavior.UnsupportedServer;
  115. return result1;
  116. }
  117. // test II
  118. var test2 = new StunMessage5389
  119. {
  120. StunMessageType = StunMessageType.BindingRequest,
  121. Attributes = new[] { AttributeExtensions.BuildChangeRequest(true, true) }
  122. };
  123. var (response2, _, _) = await TestAsync(test2, RemoteEndPoint, otherAddress);
  124. if (response2 != null)
  125. {
  126. result1.FilteringBehavior = FilteringBehavior.EndpointIndependent;
  127. return result1;
  128. }
  129. // test III
  130. var test3 = new StunMessage5389
  131. {
  132. StunMessageType = StunMessageType.BindingRequest,
  133. Attributes = new[] { AttributeExtensions.BuildChangeRequest(false, true) }
  134. };
  135. var (response3, remote3, _) = await TestAsync(test3, RemoteEndPoint, RemoteEndPoint);
  136. if (response3 == null)
  137. {
  138. result1.FilteringBehavior = FilteringBehavior.AddressAndPortDependent;
  139. return result1;
  140. }
  141. if (Equals(remote3.Address, RemoteEndPoint.Address) && remote3.Port != RemoteEndPoint.Port)
  142. {
  143. result1.FilteringBehavior = FilteringBehavior.AddressAndPortDependent;
  144. }
  145. else
  146. {
  147. result1.FilteringBehavior = FilteringBehavior.UnsupportedServer;
  148. }
  149. return result1;
  150. }
  151. private async Task<(StunMessage5389, IPEndPoint, IPAddress)> TestAsync(StunMessage5389 sendMessage, IPEndPoint remote, IPEndPoint receive)
  152. {
  153. try
  154. {
  155. var b1 = sendMessage.Bytes.ToArray();
  156. //var t = DateTime.Now;
  157. // Simple retransmissions
  158. //https://tools.ietf.org/html/rfc5389#section-7.2.1
  159. //while (t + TimeSpan.FromSeconds(6) > DateTime.Now)
  160. {
  161. try
  162. {
  163. var (receive1, ipe, local) = await UdpClient.UdpReceiveAsync(b1, remote, receive);
  164. var message = new StunMessage5389();
  165. if (message.TryParse(receive1) &&
  166. message.ClassicTransactionId.IsEqual(sendMessage.ClassicTransactionId))
  167. {
  168. return (message, ipe, local);
  169. }
  170. }
  171. catch (Exception ex)
  172. {
  173. Debug.WriteLine(ex);
  174. }
  175. }
  176. }
  177. catch (Exception ex)
  178. {
  179. Debug.WriteLine(ex);
  180. }
  181. return (null, null, null);
  182. }
  183. }
  184. }