Delphi STUN Server

Run your own STUN (RFC 8489) server in Delphi. Answer binding requests with the canonical XOR-MAPPED-ADDRESS reply — useful in private networks, on-premise deployments and ICE bootstrapping.

TsgcSTUNServer

A typed RFC 8489 STUN server — processes Binding / Indication messages, replies with XOR-MAPPED-ADDRESS, optionally enforces MESSAGE-INTEGRITY for long-term auth.

Component class

TsgcSTUNServer

Protocol

STUN (RFC 8489)

Platforms

Windows, macOS, Linux, iOS, Android

Edition

Professional / Enterprise

Drop, set port, you have a STUN endpoint

Set Port (default 3478), Active := True — your server now answers Binding requests with the canonical RFC 8489 XOR-MAPPED-ADDRESS reply.

uses
  sgcP2P, sgcP2P_STUN_Classes;

// TFormSTUNServer holds FServer: TsgcSTUNServer and the handler

procedure TFormSTUNServer.StartServer;
begin
  FServer := TsgcSTUNServer.Create(nil);
  FServer.Port := 3478;

  // optional: long term credentials
  FServer.STUNOptions.Authentication.Enabled := True;
  FServer.STUNOptions.Authentication.LongTermCredentials.Enabled := True;
  FServer.STUNOptions.Authentication.LongTermCredentials.Realm :=
    'example.com';

  FServer.OnSTUNRequestAuthorization := OnSTUNRequestAuthorization;

  FServer.Active := True;
end;

procedure TFormSTUNServer.OnSTUNRequestAuthorization(Sender: TObject;
  const aRequest: TsgcSTUN_Message;
  const aUsername, aRealm: string;
  var Password: string);
begin
  // return the password of that user, empty rejects the request
  Password := LookupPassword(aUsername);
end;
// includes: sgcP2P.hpp, sgcP2P_STUN_Classes.hpp
TsgcSTUNServer *Server = new TsgcSTUNServer(this);
Server->Port = 3478;
Server->STUNOptions->Authentication->Enabled = true;
Server->STUNOptions->Authentication->LongTermCredentials->Enabled = true;
Server->STUNOptions->Authentication->LongTermCredentials->Realm = "example.com";
Server->OnSTUNRequestAuthorization = OnSTUNRequestAuthorization;
Server->Active = true;

What's inside

A self-hosted STUN server — ideal for on-premise WebRTC deployments and private-cloud testing.

Binding request handler

Inbound Binding requests are answered with XOR-MAPPED-ADDRESS, MAPPED-ADDRESS (legacy) and FINGERPRINT — matching the standard RFC 8489 reply shape.

Long-term / short-term auth

Toggle STUNOptions.Authentication.Enabled to require MESSAGE-INTEGRITY and set the realm on LongTermCredentials.Realm. OnSTUNRequestAuthorization hands you the username and the realm, and you return the matching password.

Reply attributes you control

STUNOptions.BindingAttributes switches the legacy RFC 3489 MAPPED-ADDRESS, OTHER-ADDRESS, RESPONSE-ORIGIN and SOURCE-ADDRESS on or off, and AlternateServer answers 300 Try Alternate with the server you name.

IPv4 / IPv6

Bind to either family or dual-stack via IPVersion. The XOR-MAPPED-ADDRESS reply uses the family of the inbound transport.

Stale nonce rotation

LongTermCredentials.StaleNonce sets how long a nonce stays valid. Once it expires the server answers 438 Stale Nonce with a fresh one, which is what RFC 8489 §9.2 asks for.

Diagnostics

OnSTUNRequestSuccess and OnSTUNRequestError hand you the request and the response the server is about to send, with a var Accept you can clear. LogFile writes the raw exchange to disk.

Specifications & references

Authoritative sources for the protocol this component implements.

Documentation & Demos

Deep-link to the component reference, grab the ready-to-run demo project, and download the trial.

Online Help — TsgcSTUNServer Full property, method and event reference for this component.
Demo Project — Demos\35.P2P\02.STUN Ready-to-run example project. Ships inside the sgcWebSockets package — download the trial below.
Technical Document (PDF) Features, quick start, code samples for Delphi & C++ Builder and primary-source references — this component only.
User Manual (PDF) Comprehensive manual covering every component in the library.

STUN Server Frequently Asked Questions

Common questions about running a self-hosted STUN server in Delphi and C++ Builder.

Drop a TsgcSTUNServer component, set Port (default 3478) and set Active := True. The server then answers inbound Binding requests with the canonical XOR-MAPPED-ADDRESS reply. Optionally enable Authentication and handle OnSTUNRequestAuthorization to require MESSAGE-INTEGRITY with long-term credentials.
It implements STUN per RFC 8489, processing Binding and Indication messages and replying with XOR-MAPPED-ADDRESS, the legacy MAPPED-ADDRESS and FINGERPRINT. It supports IPv4 and IPv6, optional STUN-over-TLS, and references RFC 5780 for NAT behaviour discovery.
No. TsgcSTUNServer is a self-hosted STUN server you embed directly in your own Delphi or C++ Builder application, so you do not need to deploy and operate a separate coturn instance. It is ideal for private networks, on-premise WebRTC deployments and ICE bootstrapping under your own control.
The STUN server is part of the sgcWebSockets Enterprise edition and supports Delphi 7 through the latest Delphi release and the matching C++ Builder versions, on Windows, macOS, Linux, iOS and Android. Download the free trial to stand up a STUN endpoint in your own project.
Best value: All-AccessEvery eSeGeCe product, Premium Support included, from €1,059/year.
See All-Access pricing

Ready to Run Your Own STUN Server?

Download the free trial and stand up a STUN endpoint in Delphi.