StunClient5389UDP.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. private async Task<(StunMessage5389, IPEndPoint, IPAddress)> TestAsync(StunMessage5389 sendMessage, IPEndPoint remote, IPEndPoint receive)
  103. {
  104. try
  105. {
  106. var b1 = sendMessage.Bytes.ToArray();
  107. //var t = DateTime.Now;
  108. // Simple retransmissions
  109. //https://tools.ietf.org/html/rfc5389#section-7.2.1
  110. //while (t + TimeSpan.FromSeconds(6) > DateTime.Now)
  111. {
  112. try
  113. {
  114. var (receive1, ipe, local) = await UdpClient.UdpReceiveAsync(b1, remote, receive);
  115. var message = new StunMessage5389();
  116. if (message.TryParse(receive1) &&
  117. message.ClassicTransactionId.IsEqual(sendMessage.ClassicTransactionId))
  118. {
  119. return (message, ipe, local);
  120. }
  121. }
  122. catch (Exception ex)
  123. {
  124. Debug.WriteLine(ex);
  125. }
  126. }
  127. }
  128. catch (Exception ex)
  129. {
  130. Debug.WriteLine(ex);
  131. }
  132. return (null, null, null);
  133. }
  134. }
  135. }