using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.Collections;
using STUN.Attributes;
using System.IO;
namespace STUN
{
///
/// Implements a RFC3489 STUN client.
///
public static class STUNClient
{
///
/// Period of time in miliseconds to wait for server response.
///
public static int ReceiveTimeout = 5000;
/// Server address
/// Query type
///
/// Set to true if created socket should closed after the query
/// else will leave open and can be used.
///
public static Task QueryAsync(IPEndPoint server, STUNQueryType queryType, bool closeSocket)
{
return Task.Run(() => Query(server, queryType, closeSocket));
}
/// A UDP that will use for query. You can also use
/// Server address
/// Query type
public static Task QueryAsync(Socket socket, IPEndPoint server, STUNQueryType queryType,
NATTypeDetectionRFC natTypeDetectionRfc)
{
return Task.Run(() => Query(socket, server, queryType, natTypeDetectionRfc));
}
/// Server address
/// Query type
///
/// Set to true if created socket should closed after the query
/// else will leave open and can be used.
///
public static STUNQueryResult Query(IPEndPoint server, STUNQueryType queryType, bool closeSocket,
NATTypeDetectionRFC natTypeDetectionRfc = NATTypeDetectionRFC.Rfc3489)
{
Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
IPEndPoint bindEndPoint = new IPEndPoint(IPAddress.Any, 0);
socket.Bind(bindEndPoint);
var result = Query(socket, server, queryType, natTypeDetectionRfc);
if (closeSocket)
{
socket.Dispose();
result.Socket = null;
}
return result;
}
/// A UDP that will use for query. You can also use
/// Server address
/// Query type
/// Rfc algorithm type
public static STUNQueryResult Query(Socket socket, IPEndPoint server, STUNQueryType queryType,
NATTypeDetectionRFC natTypeDetectionRfc)
{
if (natTypeDetectionRfc == NATTypeDetectionRFC.Rfc3489)
{
return STUNRfc3489.Query(socket, server, queryType, ReceiveTimeout);
}
if (natTypeDetectionRfc == NATTypeDetectionRFC.Rfc5780)
{
return STUNRfc5780.Query(socket, server, queryType, ReceiveTimeout);
}
return new STUNQueryResult();
}
}
}