TsgcSTUNClient is the client that implements the STUN protocol and allows to send binding requests to STUN servers.
The components allows to use UDP and TCP as transport, and when used UDP as transport implements a Retransmission mechanism to re-send requests if the response not arrived after a small time.
Usually stun servers runs on UDP port 3478 and don't require authentication, so in order to send a STUN request binding, fill the server properties to allow the client know where connect and Handle the events where the component will receive the response from server.
Configure the server
Call the method SendRequest, to send a request binding to STUN server.
Handle the events
If the server returns a successful response, the event OnSTUNResponseSuccess will be called and you can access to the Binding information reading the aBinding object.
TsgcSTUNClient oSTUN = new TsgcSTUNClient();
oSTUN.Host = "stun.sgcwebsockets.com";
oSTUN.Port = 3478;
oSTUN.SendRequest();
private void OnSTUNResponseSuccess(Component Sender, TsgcSocketConnection aSocket,
TsgcSTUN_Message aMessage, TsgcSTUN_ResponseBinding aBinding)
{
DoLog("Remote IP: " + aBinding.RemoteIP + ". Remote Port: " + IntToStr(aBinding.RemotePort));
}
private void OnSTUNResponseError(Component,Sender, TsgcSocketConnection aSocket,
TsgcSTUN_Message aMessage, TsgcSTUN_ResponseError aError)
{
DoLog("Error: " + Int32.Parse(aError.Code) + " " + aError.Reason);
}
There is a single method called SendRequest, which sends a request to STUN Server, requesting binding information.
Host: it's the IP Address or DNS name of STUN server where the client will send a binding request.
Port: it's the listening port of STUN server, by default 3478.
IPVersion: it's the Family Address, by default IPv4.
Transport: it's the transport used to connect to STUN server, by default UDP.
STUNOptions: here are defined the specific STUN options of client component
Fingerprint: if enabled, the message includes a fingerprint that aids to identify STUN messages from packets of other protocols when the two are multiplexed on the same transport address.
Software: if enabled, sends an attribute with the name of the software being used by the client.
Authentication: some STUN servers requires that requests are authenticated.
Credentials: there are 2 types of Authentication: LongTermCredentials and ShortTermCredentials. By default the requests are not authenticated
Username: the string that identifies the user.
Password: the secret string.
RetransmissionOptions: when messages are sent using UDP as transport, UDP doesn't includes a mechanism to know if a message has arrived or not to other peer. This property allows to configure a mechanism to re-send UDP messages if not arrived after a small time.
Enabled: if enabled, the message will be re-send until receives a confirmation or the maximum number of retries has been reached.
RTO: retransmission time in milliseconds, by default 500ms. For example, assuming an RTO of 500 ms, requests would be sent at times 0 ms, 500 ms, 1500 ms, 3500 ms, 7500 ms, 15500 ms, and 31500 ms.
MaxRetries: Max number of retries, by default 7.
LogFile: if enabled save stun messages to a specified log file, useful for debugging. The access to log file is not thread safe if it's accessed from several threads.
Enabled: if enabled every time a message is received and sent by client it will be saved on a file.
FileName: full path to the filename.
NotifyEvents: defines which mode to notify WebSocket events.
neAsynchronous: this is the default mode, notify threaded events on asynchronous mode, adds events to a queue that are synchronized with the main thread asynchronously.
neSynchronous: if this mode is selected, notify threaded events on synchronous mode, needs to synchronize with the main thread to notify these events.
neNoSync: there is no synchronization with the main thread, if you need to access to controls that are not thread-safe, you need to implement your own synchronization methods.
OnSTUNBeforeSend
This event is called before the stun client sends a message to the server. You can access to the message properties through the aMessage parameter and modify if required.
OnSTUNResponseSuccess
When the server processes successfully a request binding, it sends a message with the binding properties (IP Address, Port and family) and other attributes, this event is called when the client receives this successful response.
OnSTUNResponseError
When there is any error in the response sent by server, this event is called with the error details.
OnSTUNException
This event is called when there is any exception processing the STUN protocol messages.