STUN (Session Traversal Utilitiies for NAT) it's an IETF protocol used for real-time audio video in IP networks. STUN is a server-client protocol, a STUN server usually operates on both UPD and TCP and listens on port 3478.
The main purpose of the STUN protocol is to enable a device running behind a NAT discover its public IP and what type of NAT is.
STUN provides a mechanism to communicate between peers behind a NAT. The peers send a request to a STUN server to know which is the public IP address and Port. The binding requests sent from client to server are used to determine the IP and ports bindings allocated by NAT's. The STUN client sends a Binding request to the STUN server, the server examines the source IP and Port used by client, and returns this information to the client.
STUN Server
TsgcSTUNServer is the server that implements the STUN protocol and allows to process binding requests from STUN clients.
The STUN server can be configured with or without Authentication, can verify Fingerprint Attribute, send an alternate server and more.
Basic Usage
Usually stun servers runs on UDP port 3478 and don't require authentication, so in order to configure a STUN server, set the listening port (by default 3478) and start the server.
Configure the server
- Port: the listening Server port, example: 3478
Set the property Active = True to start the STUN server.
Check the following Delphi Code which shows how start a new Delphi STUN Server.
Long-Term Credentials
Usually STUN Servers are configured without Authentication, so any STUN client can send a binding request and expect a response from server without Authentication.
sgcWebSockets STUN Server supports Long-Term Credentials, so you can configure TsgcSTUNServer to only allow binding requests with Long-Term credentials info.
To configure it, access to STUNOptions.Authorization property and enable it.
Then access to LongTermCredentials property and enabled it. By default, this type of authorization is already configured with a Realm string and with a default StaleNonce value of 10 minutes (= 600 seconds).
Check the following Delphi Code which shows how create a new Delphi STUN Server with Long-Credentials enabled.
STUN Client
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.
Basic usage
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
- Host: the IP or DNS name of the server, example: stun.sgcwebsockets.com
- Port: the listening Server port, example: 3478
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.
- If the server returns an error, the event OnSTUNResponseError will be called and you can access the Error Code and Reason reading the aError object.
This browser does not support the video element.