From sgcWebSockets 4.5.2, you can build your own STUN/TURN Server and Clients using sgcWebSockets library.
STUN
Session Traversal Utilities for NAT (STUN) (acronym within an acronym) is a protocol to discover your public address and determine any restrictions in your router that would prevent a direct connection with a peer.
The client will send a request to a STUN server on the Internet who will reply with the client's public address and whether or not the client is accessible behind the router's NAT.
TURN
Some routers using NAT employ a restriction called 'Symmetric NAT'. This means the router will only accept connections from peers you've previously connected to.
Traversal Using Relays around NAT (TURN) is meant to bypass the Symmetric NAT restriction by opening a connection with a TURN server and relaying all information through that server. You would create a connection with a TURN server and tell all peers to send packets to the server which will then be forwarded to you. This obviously comes with some overhead so it is only used if there are no other alternatives.
The process to create a STUN/TURN Delphi Server is very simple, just create a TsgcTURNServer and configure the following properties:
Handle the event OnSTUNRequestAuthorization to set the password when a TURN client sends a Request.
Finally set Active property to True to start the server.
Find below a sample configuration where STUN requests doesn't require authorization and TURN requests require Long-Term credentials.
oTURN := TsgcTURNServer.Create(nil); oTURN.Port := 3478; oTURN.STUNOptions.Authentication.Enabled := False; oTURN.TURNOptions.Authentication.Enabled := True; oTURN.TURNOptions.Authentication.LongTermCredentials.Enabled := True; oTURN.TURNOptions.Authentication.LongTermCredentials.Realm := 'sgcWebSockets'; oTURN.TURNOptions.Authentication.LongTermCredentials.StaleNonce := 600; oTURN.Active := True; procedure OnSTUNRequestAuthorization(Sender: TObject; const aRequest: TsgcSTUN_Message; const aUsername, aRealm: string; var Password: string); begin if (aUsername = 'my-user') and (aRealm = 'sgcWebSockets') then Password := 'my-password'; end;
Create a new Delphi STUN/TURN Client is as simple as a Server. Just create a TsgcTURNClient and configure the following properties:
Allocate IP Address
TURN Protocol allows to use a Relayed IP Address to exchange data between peers that are behind NATs.
To create a new Relayed IP Address on a TURN server, the client must first call the method Allocate, this method sends a Request to the TURN server to create a new Relayed IP Address, if the TURN server can create a new Relayed IP Address, the client will receive a successful response. The client will be able to communicate with other peers during the time defined in the Allocation's lifetime.
oTURN := TsgcTURNClient.Create(nil); oTURN.Host := 'turn.sgcwebsockets.com'; oTURN.Port := 3478; oTURN.Allocate(); procedure OnTURNAllocate(Sender: TObject; const aSocket: TsgcSocketConnection; const aMessage: TsgcSTUN_Message; const aAllocation: TsgcTURN_ResponseAllocation); begin DoLog('Relayed IP: ' + aAllocation.RelayedIP + '. Relayed Port: ' + IntToStr(aAllocation.RelayedPort)); end; procedure OnSTUNResponseError(Sender: TObject; const aMessage: TsgcSTUN_Message; const aError: TsgcSTUN_ResponseError); begin DoLog('Error: ' + IntToStr(aError.Code) + ' ' + aError.Reason); end;
The lifetime can be updated to avoid expiration using the method Refresh. The Lifetime is the number of seconds to expire. If the value is zero the Allocation will be deleted.
oTURN.Refresh(600);
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.