Delphi TURN Server Component (3/4)

From sgcWebSockets 4.5.2, TURN protocol is supported on Server components.

TsgcTURNServer is the server that implements the TURN Protocol and allows to process requests from TURN clients. The component inherits from TsgcSTUNServer, so all methods and properties are available on TsgcTURNServer.

TURN Server supports Long-Term Authentication, Allocation, Permissions, Channel Data and more.

Basic Usage 

Usually TURN servers runs on UDP port 3478 and require Long-Term credentials, so in order to configure a TURN server, set the listening port (by default 3478) and start the server.

Configure the server

  • Port: the listening Server port, example: 3478
  • Define the Long-Term Credentials properties in TURNOptions.Authentication.LongTermCredentials
  • Handle the OnSTUNRequestAuthorization to set the password when a TURN client sends a request to TURN Server.

Set the property Active = True to start the STUN server.

oTURN := TsgcTURNServer.Create(nil);
oTURN.Port := 3478;
oTURN.TURNOptions.Authentication.Enabled := True;
oTURN.TURNOptions.Authentication.LongTermCredentials.Enabled := True;
oTURN.TURNOptions.Authentication.LongTermCredentials.Realm := 'esegece.com';
oTURN.Active := True;


procedure OnSTUNRequestAuthorization(Sender: TObject; const aRequest: TsgcSTUN_Message; const aUsername, aRealm: string; var Password: string);
begin
  if (aUsername = 'user') and (aRealm = 'esegece.com') then
    Password := 'password';
end; 

Allocations 

All TURN operations revolve around allocations and all TURN messages are associated with an Allocation. An allocation consists of:

  • The relayed transport address
  • The 5-Tuple: client's IP Address, client's IP port, server IP address, server port and transport protocol.
  • The authentication information.
  • The time-to-expiry for each relayed transport address.
  • A list of permissions for each relayed transport address.
  • A list of channels bindings for each relayed transport address.

When a TURN client sends an Allocate request, this TURN message is processed by server and tries to create a new Relayed Transport Address. By default, if there is any available UDP port, it will create a new Relayed Address, but you can use OnTURNBeforeAllocate event to reject a new Allocation request.

procedure OnTURNBeforeAllocate(Sender: TObject; const aSocket: TsgcSocketConnection; const aIP: string; aPort: Word; var Reject: Boolean);
begin
  if not (your own rules) then
	Reject := false;
end; 

If the process continues, the server creates a new allocation and the event OnTURNCreateAllocation is called. This event provides information about the Allocation through the class TsgcTURNAllocationItem.

procedure OnTURNCreateAllocation(Sender: TObject; const aSocket: TsgcSocketConnection; const Allocation: TsgcTURNAllocationItem);
begin
  DoLog('New Allocation: ' + Allocation.RelayIP + ':' + IntToStr(Allocation.RelayPort));
end; 

When the Allocation expires or is deleted receiving a Refresh Request from client with a lifetime of zero, the event OnTURNDeleteAllocation event is fired.

procedure OnTURNDeleteAllocation(Sender: TObject; const aSocket: TsgcSocketConnection; const Allocation: TsgcTURNAllocationItem);
begin
  DoLog('Allocation Deleted: ' + Allocation.RelayIP + ':' + IntToStr(Allocation.RelayPort));
end; 
×
Stay Informed

When you subscribe to the blog, we will send you an e-mail when there are new updates on the site so you wouldn't miss them.

Delphi STUN/TURN Server and Client (4/4)
Delphi TURN Client Component (2/4)

Related Posts