TURN module (rfc5766)

Server

TURN server RFC 5766 / RFC 6062 implementation.

#include <icy/turn/server/server.h>

class Server

Defined in src/turn/include/icy/turn/server/server.h:126

TURN server RFC 5766 / RFC 6062 implementation. Listens on UDP and/or TCP, authenticates requests via ServerObserver, and manages ServerAllocation objects for each 5-tuple.

Public Methods

ReturnNameDescription
Server
~Server virtual
voidstart virtualBinds and listens on the configured address, then starts the maintenance timer.
voidstop virtualStops the timer, destroys all allocations, and closes server sockets.
voidhandleRequestRoutes an authenticated request to the appropriate handler based on state. Pending (Authenticating) requests are held until the observer calls back.
voidhandleAuthorizedRequestDispatches an already-authorized request to the specific method handler.
voidhandleBindingRequestHandles a Binding request; responds with XOR-MAPPED-ADDRESS.
voidhandleAllocateRequestHandles an Allocate request; creates a UDP or TCP ServerAllocation and sends a success response with XOR-RELAYED-ADDRESS and LIFETIME.
voidhandleConnectionBindRequestHandles a ConnectionBind request by locating the TCPAllocation that owns the given CONNECTION-ID and delegating to it.
voidrespondSends a STUN response, signing it with MessageIntegrity if the request had a hash. Routes via UDP or TCP depending on request.transport.
voidrespondErrorConstructs and sends an error response with SOFTWARE, REALM, NONCE, and ERROR-CODE.
std::map< FiveTuple, ServerAllocation * >allocations constReturns a snapshot copy of the allocation map for safe iteration. Returned raw pointers are valid only while the server holds the allocations.
voidaddAllocationTransfers ownership of alloc to the server and notifies the observer.
voidremoveAllocationRemoves alloc from the map and notifies the observer. Called automatically from the ServerAllocation destructor.
ServerAllocation *getAllocationLooks up an allocation by its 5-tuple.
TCPAllocation *getTCPAllocationFinds the TCPAllocation that owns a TCPConnectionPair with the given connection ID.
net::TCPSocket::PtrgetTCPSocketReturns the accepted TCP socket whose peer address matches remoteAddr.
voidreleaseTCPSocketRemoves a TCP control socket from the server's socket list and unregisters callbacks. Called when the socket is handed off to a TCPAllocation (ConnectionBind).
ServerObserver &observer
const ServerOptions &options const
net::UDPSocket &udpSocket
net::TCPSocket &tcpSocket
Timer &timer
voidonTCPAcceptConnectionAccept callback for the TCP listening socket; registers new connections for STUN message processing.
boolonTCPSocketClosedClose callback for accepted TCP sockets; removes the socket from the list.
boolonSocketRecvReceive callback for both UDP and TCP sockets; parses STUN messages and calls handleRequest() for each one.
voidonTimerPeriodic maintenance callback; expires and removes stale allocations.
voidscheduleDeferredTCPSocketReleaseDefers accepted TCP socket removal until after the active callback stack unwinds.
voiddrainReleasedTCPSockets

Server

Server(ServerObserver & observer, const ServerOptions & options = ServerOptions())

Defined in src/turn/include/icy/turn/server/server.h:131

Parameters

ParameterTypeDescription
observerServerObserver &Observer used for authentication and allocation lifecycle events.
optionsconst ServerOptions &Server configuration; defaults to 0.0.0.0:3478 with TCP and UDP enabled.

~Server

virtual

virtual ~Server()

Defined in src/turn/include/icy/turn/server/server.h:132


start

virtual

virtual void start()

Defined in src/turn/include/icy/turn/server/server.h:135

Binds and listens on the configured address, then starts the maintenance timer.


stop

virtual

virtual void stop()

Defined in src/turn/include/icy/turn/server/server.h:138

Stops the timer, destroys all allocations, and closes server sockets.


handleRequest

void handleRequest(Request & request, AuthenticationState state)

Defined in src/turn/include/icy/turn/server/server.h:144

Routes an authenticated request to the appropriate handler based on state. Pending (Authenticating) requests are held until the observer calls back.

Parameters

ParameterTypeDescription
requestRequest &Incoming STUN request.
stateAuthenticationStateResult of the observer's authenticateRequest() call.

handleAuthorizedRequest

void handleAuthorizedRequest(Request & request)

Defined in src/turn/include/icy/turn/server/server.h:148

Dispatches an already-authorized request to the specific method handler.

Parameters

ParameterTypeDescription
requestRequest &Authorized STUN request.

handleBindingRequest

void handleBindingRequest(Request & request)

Defined in src/turn/include/icy/turn/server/server.h:152

Handles a Binding request; responds with XOR-MAPPED-ADDRESS.

Parameters

ParameterTypeDescription
requestRequest &Incoming Binding request.

handleAllocateRequest

void handleAllocateRequest(Request & request)

Defined in src/turn/include/icy/turn/server/server.h:157

Handles an Allocate request; creates a UDP or TCP ServerAllocation and sends a success response with XOR-RELAYED-ADDRESS and LIFETIME.

Parameters

ParameterTypeDescription
requestRequest &Incoming Allocate request.

handleConnectionBindRequest

void handleConnectionBindRequest(Request & request)

Defined in src/turn/include/icy/turn/server/server.h:162

Handles a ConnectionBind request by locating the TCPAllocation that owns the given CONNECTION-ID and delegating to it.

Parameters

ParameterTypeDescription
requestRequest &Incoming ConnectionBind request.

respond

void respond(Request & request, stun::Message & response)

Defined in src/turn/include/icy/turn/server/server.h:168

Sends a STUN response, signing it with MessageIntegrity if the request had a hash. Routes via UDP or TCP depending on request.transport.

Parameters

ParameterTypeDescription
requestRequest &The original request (provides transport and remote address).
responsestun::Message &The response message to send.

respondError

void respondError(Request & request, int errorCode, const char * errorDesc)

Defined in src/turn/include/icy/turn/server/server.h:174

Constructs and sends an error response with SOFTWARE, REALM, NONCE, and ERROR-CODE.

Parameters

ParameterTypeDescription
requestRequest &The original request.
errorCodeintSTUN error code (e.g. 400, 401, 437).
errorDescconst char *Human-readable error description string.

allocations

const

std::map< FiveTuple, ServerAllocation * > allocations() const

Defined in src/turn/include/icy/turn/server/server.h:179

Returns a snapshot copy of the allocation map for safe iteration. Returned raw pointers are valid only while the server holds the allocations.

Returns

Map from FiveTuple to raw ServerAllocation pointers.


addAllocation

void addAllocation(std::unique_ptr< ServerAllocation > alloc)

Defined in src/turn/include/icy/turn/server/server.h:183

Transfers ownership of alloc to the server and notifies the observer.

Parameters

ParameterTypeDescription
allocstd::unique_ptr< ServerAllocation >Newly constructed allocation to register.

removeAllocation

void removeAllocation(ServerAllocation * alloc)

Defined in src/turn/include/icy/turn/server/server.h:188

Removes alloc from the map and notifies the observer. Called automatically from the ServerAllocation destructor.

Parameters

ParameterTypeDescription
allocServerAllocation *Allocation being destroyed.

getAllocation

ServerAllocation * getAllocation(const FiveTuple & tuple)

Defined in src/turn/include/icy/turn/server/server.h:193

Looks up an allocation by its 5-tuple.

Returns

Pointer to the matching allocation, or nullptr if not found.

Parameters

ParameterTypeDescription
tupleconst FiveTuple &The 5-tuple to search for.

getTCPAllocation

TCPAllocation * getTCPAllocation(const uint32_t & connectionID)

Defined in src/turn/include/icy/turn/server/server.h:198

Finds the TCPAllocation that owns a TCPConnectionPair with the given connection ID.

Returns

Pointer to the owning TCPAllocation, or nullptr if not found.

Parameters

ParameterTypeDescription
connectionIDconst uint32_t &TURN CONNECTION-ID to search for.

getTCPSocket

net::TCPSocket::Ptr getTCPSocket(const net::Address & remoteAddr)

Defined in src/turn/include/icy/turn/server/server.h:203

Returns the accepted TCP socket whose peer address matches remoteAddr.

Returns

Shared pointer to the socket, or empty if not found.

Parameters

ParameterTypeDescription
remoteAddrconst net::Address &Peer address to search for.

releaseTCPSocket

void releaseTCPSocket(const net::Socket & socket)

Defined in src/turn/include/icy/turn/server/server.h:208

Removes a TCP control socket from the server's socket list and unregisters callbacks. Called when the socket is handed off to a TCPAllocation (ConnectionBind).

Parameters

ParameterTypeDescription
socketconst net::Socket &The socket to release.

observer

ServerObserver & observer()

Defined in src/turn/include/icy/turn/server/server.h:211

Returns

Reference to the observer provided at construction.


options

const

const ServerOptions & options() const

Defined in src/turn/include/icy/turn/server/server.h:214

Returns

Reference to the immutable options struct.


udpSocket

net::UDPSocket & udpSocket()

Defined in src/turn/include/icy/turn/server/server.h:217

Returns

Reference to the UDP server socket.


tcpSocket

net::TCPSocket & tcpSocket()

Defined in src/turn/include/icy/turn/server/server.h:220

Returns

Reference to the TCP server listening socket.


timer

Timer & timer()

Defined in src/turn/include/icy/turn/server/server.h:223

Returns

Reference to the maintenance timer.


onTCPAcceptConnection

void onTCPAcceptConnection(const net::TCPSocket::Ptr & sock)

Defined in src/turn/include/icy/turn/server/server.h:228

Accept callback for the TCP listening socket; registers new connections for STUN message processing.

Parameters

ParameterTypeDescription
sockconst net::TCPSocket::Ptr &Newly accepted TCP socket.

onTCPSocketClosed

bool onTCPSocketClosed(net::Socket & socket)

Defined in src/turn/include/icy/turn/server/server.h:232

Close callback for accepted TCP sockets; removes the socket from the list.

Parameters

ParameterTypeDescription
socketnet::Socket &The closed socket.

onSocketRecv

bool onSocketRecv(net::Socket & socket, const MutableBuffer & buffer, const net::Address & peerAddress)

Defined in src/turn/include/icy/turn/server/server.h:239

Receive callback for both UDP and TCP sockets; parses STUN messages and calls handleRequest() for each one.

Parameters

ParameterTypeDescription
socketnet::Socket &The receiving socket.
bufferconst MutableBuffer &Received data buffer.
peerAddressconst net::Address &Source address of the data.

onTimer

void onTimer()

Defined in src/turn/include/icy/turn/server/server.h:243

Periodic maintenance callback; expires and removes stale allocations.


scheduleDeferredTCPSocketRelease

void scheduleDeferredTCPSocketRelease()

Defined in src/turn/include/icy/turn/server/server.h:246

Defers accepted TCP socket removal until after the active callback stack unwinds.


drainReleasedTCPSockets

void drainReleasedTCPSockets()

Defined in src/turn/include/icy/turn/server/server.h:247

Private Attributes


_mutex

std::mutex _mutex

Defined in src/turn/include/icy/turn/server/server.h:250


_observer

ServerObserver & _observer

Type: ServerObserver &

Defined in src/turn/include/icy/turn/server/server.h:251


_options

ServerOptions _options

Type: ServerOptions

Defined in src/turn/include/icy/turn/server/server.h:252


_udpSocket

net::SocketEmitter _udpSocket

Type: net::SocketEmitter

Defined in src/turn/include/icy/turn/server/server.h:253


_tcpSocket

net::SocketEmitter _tcpSocket

Type: net::SocketEmitter

Defined in src/turn/include/icy/turn/server/server.h:254


_tcpSockets

std::vector< net::SocketEmitter > _tcpSockets

Type: std::vector< net::SocketEmitter >

Defined in src/turn/include/icy/turn/server/server.h:255


_pendingReleasedTCPSockets

std::unordered_set< const net::Socket * > _pendingReleasedTCPSockets

Type: std::unordered_set< const net::Socket * >

Defined in src/turn/include/icy/turn/server/server.h:256


_tcpSocketReleaseScheduled

bool _tcpSocketReleaseScheduled {false}

Defined in src/turn/include/icy/turn/server/server.h:257


_allocations

ServerAllocationMap _allocations

Type: ServerAllocationMap

Defined in src/turn/include/icy/turn/server/server.h:258


_timer

Timer _timer

Type: Timer

Defined in src/turn/include/icy/turn/server/server.h:259