STUN Client

Discover your reflexive endpoint behind NAT with a typed STUN (RFC 8489) client. Used as the bootstrap step for ICE candidate gathering and WebRTC.

TsgcSTUNClient

STUN client — Binding requests, XOR-MAPPED-ADDRESS extraction, MESSAGE-INTEGRITY / FINGERPRINT support, retransmission timer per RFC 8489.

Component class

TsgcSTUNClient

Protocol

STUN (RFC 8489)

Platforms

Windows, macOS, Linux, iOS, Android

Edition

Standard / Professional / Enterprise

Drop the component, ask for the reflexive address

Set the STUN server host/port, call SendBindingRequest, then read MappedAddress / MappedPort from OnSTUNMappedAddress.

uses
  sgcP2P;

var
  STUN: TsgcSTUNClient;
begin
  STUN := TsgcSTUNClient.Create(nil);
  STUN.Host := 'stun.l.google.com';
  STUN.Port := 19302;

  STUN.OnSTUNMappedAddress := procedure(Sender: TObject;
    const aMappedAddress, aMappedPort: string)
  begin
    Memo1.Lines.Add('public: ' + aMappedAddress + ':' + aMappedPort);
  end;

  STUN.SendBindingRequest;
end;
// uses: sgcP2P
TsgcSTUNClient *STUN = new TsgcSTUNClient(this);
STUN->Host = "stun.l.google.com";
STUN->Port = 19302;
STUN->SendBindingRequest();

What's inside

A typed RFC 8489 STUN client with the message-integrity and fingerprint extensions used by ICE.

Binding request / response

SendBindingRequest sends the STUN 0x0001 message; the response surfaces in OnSTUNMappedAddress with the parsed XOR-MAPPED-ADDRESS attribute.

Auth (long-term / short-term)

Set Authentication.UserName / Password and the component computes MESSAGE-INTEGRITY (HMAC-SHA1 / HMAC-SHA256). The realm and nonce are extracted from the 401 challenge response.

Retransmission

STUN over UDP requires application-layer retransmission. The component implements the standard back-off (RTO / Rc / Rm) per RFC 8489 §6.2.1.

IPv4 / IPv6

XOR-MAPPED-ADDRESS comes back in either family — the component decodes both and stores the canonical text form in MappedAddress.

Use as ICE host gatherer

Drive multiple TsgcSTUNClient instances against several STUN servers to gather server-reflexive ICE candidates — one per local interface.

Underlying socket

Uses TsgcUDPClient internally; the local-port binding is configurable so STUN, ICE and the data path can share an endpoint when required.

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 — TsgcSTUNClient 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.

Ready to Discover NAT Mappings?

Download the free trial and add STUN-based NAT discovery to your Delphi applications.