Delphi 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 and port, call SendRequest, then read aBinding.RemoteIP and aBinding.RemotePort in OnSTUNResponseSuccess.

uses
  sgcP2P, sgcP2P_STUN_Classes, sgcSocket_Classes;

// TFormSTUN holds FClient: TsgcSTUNClient and FLog: TStringList

procedure TFormSTUN.Discover;
begin
  FClient := TsgcSTUNClient.Create(nil);
  FClient.Host := 'stun.l.google.com';
  FClient.Port := 19302;

  FClient.OnSTUNResponseSuccess := OnSTUNResponseSuccess;

  FClient.SendRequest;
end;

procedure TFormSTUN.OnSTUNResponseSuccess(Sender: TObject;
  const aSocket: TsgcSocketConnection;
  const aMessage: TsgcSTUN_Message;
  const aBinding: TsgcSTUN_ResponseBinding);
begin
  // RemoteIP and RemotePort carry the XOR-MAPPED-ADDRESS the server saw
  FLog.Add('public: ' + aBinding.RemoteIP + ':' +
    IntToStr(aBinding.RemotePort));
end;
// includes: sgcP2P.hpp, sgcP2P_STUN_Classes.hpp
TsgcSTUNClient *Client = new TsgcSTUNClient(this);
Client->Host = "stun.l.google.com";
Client->Port = 19302;
Client->OnSTUNResponseSuccess = OnSTUNResponseSuccess;
Client->SendRequest();

What's inside

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

Binding request / response

SendRequest sends the STUN 0x0001 Binding message to Host / Port, and SendBindingRequest(host, port) queries another server through the same socket. The reply arrives on OnSTUNResponseSuccess with XOR-MAPPED-ADDRESS already parsed.

Auth (long-term / short-term)

Set STUNOptions.Authentication.Credentials to stauShortTermCredential or stauLongTermCredential, fill Username and Password, and the component computes MESSAGE-INTEGRITY. The realm and nonce come from the 401 challenge.

Retransmission

STUN over UDP needs application-layer retransmission. RetransmissionOptions.RTO and MaxRetries drive the RFC 8489 §6.2.1 back-off, and Enabled turns it off when you drive the timing yourself.

IPv4 / IPv6

XOR-MAPPED-ADDRESS comes back in either family. The component decodes both and hands you the text form in aBinding.RemoteIP, with the family in aBinding.IPVersion.

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, and Transport := stunTCP switches to the TCP path. GetLocalSocketBinding returns the transport address of that socket, which is the RFC 8445 base every candidate gathered through it shares.

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.
Best value: All-AccessEvery eSeGeCe product, Premium Support included, from €1,059/year.
See All-Access pricing

Ready to Discover NAT Mappings?

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