STUN Server
TsgcSTUNServer — RFC 8489 STUN server for sgcWebSockets; lets behind-NAT peers discover their public reflexive address.
TsgcSTUNServer — RFC 8489 STUN server for sgcWebSockets; lets behind-NAT peers discover their public reflexive address.
The STUN server can be configured with or without Authentication, can verify Fingerprint Attribute, send an alternate server and more.
TsgcSTUNServer| Standards & specs | STUN — RFC 8489 |
| Component class | TsgcSTUNServer (unit sgcSTUN_Server) |
| Frameworks | VCL, FireMonkey, Lazarus / FPC |
| Platforms | Windows, macOS, Linux, iOS, Android |
The principal published / public properties used to configure and drive the component. Consult the online help for the full list.
Active | Starts or stops the STUN server; set to True to begin listening for Binding Requests. |
Host | Local IP address or host name the STUN server binds to when Active is set to True. |
Port | UDP port the STUN server listens on; defaults to 3478 as reserved by RFC 5389. |
STUNOptions | Server-side STUN options: FINGERPRINT, SOFTWARE, Authentication and the attributes added to Binding Responses. |
LogFile | Appends every STUN message received or sent by the server to a file for debugging. |
NotifyEvents | Selects how threaded server events are synchronized with the main VCL/FMX thread. |
IPVersion | Address family used by the listening socket; defaults to IPv4. |
Version | Read-only build version of the sgcWebSockets component library. |
The principal public methods exposed by the component.
AddBinding() | Adds an extra listening endpoint (IP/port) to the STUN server without restarting the currently active bindings. |
RemoveBinding() | Removes a previously added listening endpoint and closes its socket without stopping the STUN server. |
The component exposes the following published events; consult the online help for full event-handler signatures.
OnSTUNException | Raised when an unhandled exception is caught while parsing or responding to a STUN message. |
OnSTUNRequestAuthorization | Raised when a Binding Request requires authentication; supply the password associated with the incoming Username/Realm. |
OnSTUNRequestError | Raised before the server sends a STUN error response; allows the handler to inspect or suppress the reply. |
OnSTUNRequestSuccess | Raised before the server sends a successful Binding Response so the handler can inspect or veto the reply. |
Drop the component on a form, configure the properties below and activate it. The snippet that follows shows the typical TsgcSTUNServer — Basic usage configuration sourced from the online help.
oSTUN := TsgcSTUNServer.Create(nil); oSTUN.Port := 3478; oSTUN.Active := True;
TsgcSTUNServer oSTUN = new TsgcSTUNServer(); oSTUN->Port = 3478; oSTUN->Active = true;
TsgcSTUNServer oSTUN = new TsgcSTUNServer(); oSTUN.Port = 3478; oSTUN.Active = true;
The following scenarios are lifted verbatim from the online help. Each shows the configuration and method calls needed to drive the component through a specific real-world flow.
The alternate server represents an alternate transport address identifying a different STUN server that the STUN client should try.
oSTUN := TsgcSTUNServer.Create(nil); oSTUN.Port := 3478; oSTUN.STUNOptions.BindingAttributes.AlternateServer.Enabled := True; oSTUN.STUNOptions.BindingAttributes.AlternateServer.IPAddress := '80.54.54.1'; oSTUN.STUNOptions.BindingAttributes.AlternateServer.Port := 3478; oSTUN.Active := True;
TsgcSTUNServer oSTUN = new TsgcSTUNServer(); oSTUN->Port = 3478; oSTUN->STUNOptions->BindingAttributes->AlternateServer->Enabled = true; oSTUN->STUNOptions->BindingAttributes->AlternateServer->IPAddress = "80.54.54.1"; oSTUN->STUNOptions->BindingAttributes->AlternateServer->Port = 3478; oSTUN->Active = true;
TsgcSTUNServer oSTUN = new TsgcSTUNServer(); oSTUN.Port = 3478; oSTUN.STUNOptions.BindingAttributes.AlternateServer.Enabled = true; oSTUN.STUNOptions.BindingAttributes.AlternateServer.IPAddress = "80.54.54.1"; oSTUN.STUNOptions.BindingAttributes.AlternateServer.Port = 3478; oSTUN.Active = true;
Usually STUN Servers are configured without Authentication, so any STUN client can send a binding request and expect a response from server without Authentication.
oSTUN := TsgcSTUNServer.Create(nil); oSTUN.Port := 3478; oSTUN.STUNOptions.Authentication.Enabled := True; oSTUN.STUNOptions.Authentication.LongTermCredentials.Enabled := True; oSTUN.STUNOptions.Authentication.LongTermCredentials.Realm := 'sgcWebSockets'; oSTUN.STUNOptions.Authentication.LongTermCredentials.StaleNonce := 600; oSTUN.Active := True; procedure OnSTUNRequestAuthorization(Sender: TObject; const aRequest: TsgcSTUN_Message; const aUsername, aRealm: string; var Password: string); begin if aUsername = 'my-user' then Password := 'my-password'; end;
TsgcSTUNServer oSTUN = new TsgcSTUNServer(); oSTUN->STUNOptions->Authentication->Enabled = true; oSTUN->STUNOptions->Authentication->LongTermCredentials->Enabled = true; oSTUN->STUNOptions->Authentication->LongTermCredentials->Realm = "sgcWebSockets"; oSTUN->STUNOptions->Authentication->LongTermCredentials->StaleNonce = 600; oSTUN->Port = 3478; oSTUN->Active = true; private void OnSTUNRequestAuthorization(TObject *Sender, const TsgcSTUN_Message *aRequest, const string aUsername, const string aRealm, ref string Password) { if (aUsername == "my-user") { Password = "my-password"; } }
TsgcSTUNServer oSTUN = new TsgcSTUNServer(); oSTUN.Port = 3478; oSTUN.STUNOptions.Authentication.Enabled = true; oSTUN.STUNOptions.Authentication.LongTermCredentials.Enabled = true; oSTUN.STUNOptions.Authentication.LongTermCredentials.Realm = "sgcWebSockets"; oSTUN.STUNOptions.Authentication.LongTermCredentials.StaleNonce = 600; oSTUN.Active = true; private void OnSTUNRequestAuthorization(Component Sender, TsgcSTUN_Message aRequest, string aUsername, string aRealm, ref string Password) { if ((aUsername == "my-user") && (aRealm == "sgcWebSockets")) { Password = "my-password"; } }
Every external claim links back to a primary source. The online-help references decode the canonical deep-link the company maintains for this component.
Demos\35.P2P\02.STUN