|
@@ -36,26 +36,23 @@ namespace NatTypeTester_Console.Net.STUN.Client
|
|
|
/// <param name="host">STUN server name or IP.</param>
|
|
|
/// <param name="port">STUN server port. Default port is 3478.</param>
|
|
|
/// <param name="localEP">Local IP end point.</param>
|
|
|
- /// <returns>Returns UDP netwrok info.</returns>
|
|
|
+ /// <returns>Returns UDP network info.</returns>
|
|
|
/// <exception cref="Exception">Is raised when <b>host</b> or <b>localEP</b> is null reference.</exception>
|
|
|
/// <exception cref="ArgumentNullException">Throws exception if unexpected error happens.</exception>
|
|
|
public static StunResult Query(string host, int port, IPEndPoint localEP)
|
|
|
{
|
|
|
if (host == null)
|
|
|
{
|
|
|
- throw new ArgumentNullException("host");
|
|
|
+ throw new ArgumentNullException(nameof(host));
|
|
|
}
|
|
|
if (localEP == null)
|
|
|
{
|
|
|
- throw new ArgumentNullException("localEP");
|
|
|
+ throw new ArgumentNullException(nameof(localEP));
|
|
|
}
|
|
|
|
|
|
- using (var s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp))
|
|
|
- {
|
|
|
- s.Bind(localEP);
|
|
|
-
|
|
|
- return Query(host, port, s);
|
|
|
- }
|
|
|
+ using var s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
|
|
|
+ s.Bind(localEP);
|
|
|
+ return Query(host, port, s);
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
@@ -64,25 +61,25 @@ namespace NatTypeTester_Console.Net.STUN.Client
|
|
|
/// <param name="host">STUN server name or IP.</param>
|
|
|
/// <param name="port">STUN server port. Default port is 3478.</param>
|
|
|
/// <param name="socket">UDP socket to use.</param>
|
|
|
- /// <returns>Returns UDP netwrok info.</returns>
|
|
|
+ /// <returns>Returns UDP network info.</returns>
|
|
|
/// <exception cref="Exception">Throws exception if unexpected error happens.</exception>
|
|
|
public static StunResult Query(string host, int port, Socket socket)
|
|
|
{
|
|
|
if (host == null)
|
|
|
{
|
|
|
- throw new ArgumentNullException("host");
|
|
|
+ throw new ArgumentNullException(nameof(host));
|
|
|
}
|
|
|
if (socket == null)
|
|
|
{
|
|
|
- throw new ArgumentNullException("socket");
|
|
|
+ throw new ArgumentNullException(nameof(socket));
|
|
|
}
|
|
|
if (port < 1)
|
|
|
{
|
|
|
- throw new ArgumentException("Port value must be >= 1 !");
|
|
|
+ throw new ArgumentException(@"Port value must be >= 1 !");
|
|
|
}
|
|
|
if (socket.ProtocolType != ProtocolType.Udp)
|
|
|
{
|
|
|
- throw new ArgumentException("Socket must be UDP socket !");
|
|
|
+ throw new ArgumentException(@"Socket must be UDP socket !");
|
|
|
}
|
|
|
|
|
|
var remoteEndPoint = new IPEndPoint(Dns.GetHostAddresses(host)[0], port);
|
|
@@ -145,35 +142,36 @@ namespace NatTypeTester_Console.Net.STUN.Client
|
|
|
try
|
|
|
{
|
|
|
// Test I
|
|
|
- var test1 = new StunMessage();
|
|
|
- test1.Type = StunMessageType.BindingRequest;
|
|
|
- var test1response = DoTransaction(test1, socket, remoteEndPoint, 1600);
|
|
|
+ var test1 = new StunMessage { Type = StunMessageType.BindingRequest };
|
|
|
+ var test1Response = DoTransaction(test1, socket, remoteEndPoint, 1600);
|
|
|
|
|
|
// UDP blocked.
|
|
|
- if (test1response == null)
|
|
|
+ if (test1Response == null)
|
|
|
{
|
|
|
return new StunResult(NatType.UdpBlocked, null);
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
// Test II
|
|
|
- var test2 = new StunMessage();
|
|
|
- test2.Type = StunMessageType.BindingRequest;
|
|
|
- test2.ChangeRequest = new StunChangeRequest(true, true);
|
|
|
+ var test2 = new StunMessage
|
|
|
+ {
|
|
|
+ Type = StunMessageType.BindingRequest,
|
|
|
+ ChangeRequest = new StunChangeRequest(true, true)
|
|
|
+ };
|
|
|
|
|
|
// No NAT.
|
|
|
- if (socket.LocalEndPoint.Equals(test1response.MappedAddress))
|
|
|
+ if (socket.LocalEndPoint.Equals(test1Response.MappedAddress))
|
|
|
{
|
|
|
var test2Response = DoTransaction(test2, socket, remoteEndPoint, 1600);
|
|
|
// Open Internet.
|
|
|
if (test2Response != null)
|
|
|
{
|
|
|
- return new StunResult(NatType.OpenInternet, test1response.MappedAddress);
|
|
|
+ return new StunResult(NatType.OpenInternet, test1Response.MappedAddress);
|
|
|
}
|
|
|
// Symmetric UDP firewall.
|
|
|
else
|
|
|
{
|
|
|
- return new StunResult(NatType.SymmetricUdpFirewall, test1response.MappedAddress);
|
|
|
+ return new StunResult(NatType.SymmetricUdpFirewall, test1Response.MappedAddress);
|
|
|
}
|
|
|
}
|
|
|
// NAT
|
|
@@ -184,7 +182,7 @@ namespace NatTypeTester_Console.Net.STUN.Client
|
|
|
// Full cone NAT.
|
|
|
if (test2Response != null)
|
|
|
{
|
|
|
- return new StunResult(NatType.FullCone, test1response.MappedAddress);
|
|
|
+ return new StunResult(NatType.FullCone, test1Response.MappedAddress);
|
|
|
}
|
|
|
else
|
|
|
{
|
|
@@ -194,38 +192,39 @@ namespace NatTypeTester_Console.Net.STUN.Client
|
|
|
*/
|
|
|
|
|
|
// Test I(II)
|
|
|
- var test12 = new StunMessage();
|
|
|
- test12.Type = StunMessageType.BindingRequest;
|
|
|
+ var test12 = new StunMessage { Type = StunMessageType.BindingRequest };
|
|
|
|
|
|
- var test12Response = DoTransaction(test12, socket, test1response.ChangedAddress, 1600);
|
|
|
+ var test12Response = DoTransaction(test12, socket, test1Response.ChangedAddress, 1600);
|
|
|
if (test12Response == null)
|
|
|
{
|
|
|
- throw new Exception("STUN Test I(II) dind't get resonse !");
|
|
|
+ throw new Exception(@"STUN Test I(II) didn't get response !");
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
// Symmetric NAT
|
|
|
- if (!test12Response.MappedAddress.Equals(test1response.MappedAddress))
|
|
|
+ if (!test12Response.MappedAddress.Equals(test1Response.MappedAddress))
|
|
|
{
|
|
|
- return new StunResult(NatType.Symmetric, test1response.MappedAddress);
|
|
|
+ return new StunResult(NatType.Symmetric, test1Response.MappedAddress);
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
// Test III
|
|
|
- var test3 = new StunMessage();
|
|
|
- test3.Type = StunMessageType.BindingRequest;
|
|
|
- test3.ChangeRequest = new StunChangeRequest(false, true);
|
|
|
+ var test3 = new StunMessage
|
|
|
+ {
|
|
|
+ Type = StunMessageType.BindingRequest,
|
|
|
+ ChangeRequest = new StunChangeRequest(false, true)
|
|
|
+ };
|
|
|
|
|
|
- var test3Response = DoTransaction(test3, socket, test1response.ChangedAddress, 1600);
|
|
|
+ var test3Response = DoTransaction(test3, socket, test1Response.ChangedAddress, 1600);
|
|
|
// Restricted
|
|
|
if (test3Response != null)
|
|
|
{
|
|
|
- return new StunResult(NatType.RestrictedCone, test1response.MappedAddress);
|
|
|
+ return new StunResult(NatType.RestrictedCone, test1Response.MappedAddress);
|
|
|
}
|
|
|
// Port restricted
|
|
|
else
|
|
|
{
|
|
|
- return new StunResult(NatType.PortRestrictedCone, test1response.MappedAddress);
|
|
|
+ return new StunResult(NatType.PortRestrictedCone, test1Response.MappedAddress);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
@@ -267,19 +266,19 @@ namespace NatTypeTester_Console.Net.STUN.Client
|
|
|
{
|
|
|
if (stunServer == null)
|
|
|
{
|
|
|
- throw new ArgumentNullException("stunServer");
|
|
|
+ throw new ArgumentNullException(nameof(stunServer));
|
|
|
}
|
|
|
- if (stunServer == "")
|
|
|
+ if (stunServer == string.Empty)
|
|
|
{
|
|
|
- throw new ArgumentException("Argument 'stunServer' value must be specified.");
|
|
|
+ throw new ArgumentException(@"Argument 'stunServer' value must be specified.");
|
|
|
}
|
|
|
if (port < 1)
|
|
|
{
|
|
|
- throw new ArgumentException("Invalid argument 'port' value.");
|
|
|
+ throw new ArgumentException(@"Invalid argument 'port' value.");
|
|
|
}
|
|
|
if (localIP == null)
|
|
|
{
|
|
|
- throw new ArgumentNullException("localIP");
|
|
|
+ throw new ArgumentNullException(nameof(localIP));
|
|
|
}
|
|
|
|
|
|
if (!NetUtils.IsPrivateIP(localIP))
|
|
@@ -293,7 +292,7 @@ namespace NatTypeTester_Console.Net.STUN.Client
|
|
|
return result.PublicEndPoint.Address;
|
|
|
}
|
|
|
|
|
|
- throw new IOException("Failed to STUN public IP address. STUN server name is invalid or firewall blocks STUN.");
|
|
|
+ throw new IOException(@"Failed to STUN public IP address. STUN server name is invalid or firewall blocks STUN.");
|
|
|
}
|
|
|
|
|
|
#endregion
|
|
@@ -314,23 +313,23 @@ namespace NatTypeTester_Console.Net.STUN.Client
|
|
|
{
|
|
|
if (stunServer == null)
|
|
|
{
|
|
|
- throw new ArgumentNullException("stunServer");
|
|
|
+ throw new ArgumentNullException(nameof(stunServer));
|
|
|
}
|
|
|
- if (stunServer == "")
|
|
|
+ if (stunServer == string.Empty)
|
|
|
{
|
|
|
- throw new ArgumentException("Argument 'stunServer' value must be specified.");
|
|
|
+ throw new ArgumentException(@"Argument 'stunServer' value must be specified.");
|
|
|
}
|
|
|
if (port < 1)
|
|
|
{
|
|
|
- throw new ArgumentException("Invalid argument 'port' value.");
|
|
|
+ throw new ArgumentException(@"Invalid argument 'port' value.");
|
|
|
}
|
|
|
if (socket == null)
|
|
|
{
|
|
|
- throw new ArgumentNullException("socket");
|
|
|
+ throw new ArgumentNullException(nameof(socket));
|
|
|
}
|
|
|
if (socket.ProtocolType != ProtocolType.Udp)
|
|
|
{
|
|
|
- throw new ArgumentException("Socket must be UDP socket !");
|
|
|
+ throw new ArgumentException(@"Socket must be UDP socket !");
|
|
|
}
|
|
|
|
|
|
var remoteEndPoint = new IPEndPoint(Dns.GetHostAddresses(stunServer)[0], port);
|
|
@@ -338,21 +337,20 @@ namespace NatTypeTester_Console.Net.STUN.Client
|
|
|
try
|
|
|
{
|
|
|
// Test I
|
|
|
- var test1 = new StunMessage();
|
|
|
- test1.Type = StunMessageType.BindingRequest;
|
|
|
+ var test1 = new StunMessage { Type = StunMessageType.BindingRequest };
|
|
|
var test1response = DoTransaction(test1, socket, remoteEndPoint, 1000);
|
|
|
|
|
|
// UDP blocked.
|
|
|
if (test1response == null)
|
|
|
{
|
|
|
- throw new IOException("Failed to STUN public IP address. STUN server name is invalid or firewall blocks STUN.");
|
|
|
+ throw new IOException(@"Failed to STUN public IP address. STUN server name is invalid or firewall blocks STUN.");
|
|
|
}
|
|
|
|
|
|
return test1response.SourceAddress;
|
|
|
}
|
|
|
catch
|
|
|
{
|
|
|
- throw new IOException("Failed to STUN public IP address. STUN server name is invalid or firewall blocks STUN.");
|
|
|
+ throw new IOException(@"Failed to STUN public IP address. STUN server name is invalid or firewall blocks STUN.");
|
|
|
}
|
|
|
finally
|
|
|
{
|
|
@@ -380,7 +378,7 @@ namespace NatTypeTester_Console.Net.STUN.Client
|
|
|
/// <param name="request">STUN message.</param>
|
|
|
/// <param name="socket">Socket to use for send/receive.</param>
|
|
|
/// <param name="remoteEndPoint">Remote end point.</param>
|
|
|
- /// <param name="timeout">Timeout in milli seconds.</param>
|
|
|
+ /// <param name="timeout">Timeout in milliseconds.</param>
|
|
|
/// <returns>Returns transaction response or null if transaction failed.</returns>
|
|
|
private static StunMessage DoTransaction(StunMessage request, Socket socket, IPEndPoint remoteEndPoint, int timeout)
|
|
|
{
|