#include <icy/turn/server/server.h>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.
| Return | Name | Description |
|---|---|---|
Server | #### Parameters | |
void | start virtual | Binds and listens on the configured address, then starts the maintenance timer. |
void | stop virtual | Stops the timer, destroys all allocations, and closes server sockets. |
void | handleRequest | Routes an authenticated request to the appropriate handler based on state. Pending (Authenticating) requests are held until the observer calls back. |
void | handleAuthorizedRequest | Dispatches an already-authorized request to the specific method handler. |
void | handleBindingRequest | Handles a Binding request; responds with XOR-MAPPED-ADDRESS. |
void | handleAllocateRequest | Handles an Allocate request; creates a UDP or TCP ServerAllocation and sends a success response with XOR-RELAYED-ADDRESS and LIFETIME. |
void | handleConnectionBindRequest | Handles a ConnectionBind request by locating the TCPAllocation that owns the given CONNECTION-ID and delegating to it. |
void | respond | Sends a STUN response, signing it with MessageIntegrity if the request had a hash. Routes via UDP or TCP depending on request.transport. |
void | respondError | Constructs and sends an error response with SOFTWARE, REALM, NONCE, and ERROR-CODE. |
std::map< FiveTuple, ServerAllocation * > | allocations const | Returns a snapshot copy of the allocation map for safe iteration. Returned raw pointers are valid only while the server holds the allocations. |
void | addAllocation | Transfers ownership of alloc to the server and notifies the observer. |
void | removeAllocation | Removes alloc from the map and notifies the observer. Called automatically from the ServerAllocation destructor. |
ServerAllocation * | getAllocation | Looks up an allocation by its 5-tuple. |
TCPAllocation * | getTCPAllocation | Finds the TCPAllocation that owns a TCPConnectionPair with the given connection ID. |
net::TCPSocket::Ptr | getTCPSocket | Returns the accepted TCP socket whose peer address matches remoteAddr. |
void | releaseTCPSocket | 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). |
ServerObserver & | observer | #### Returns |
const ServerOptions & | options const | #### Returns |
net::UDPSocket & | udpSocket | #### Returns |
net::TCPSocket & | tcpSocket | #### Returns |
Timer & | timer | #### Returns |
void | onTCPAcceptConnection | Accept callback for the TCP listening socket; registers new connections for STUN message processing. |
bool | onTCPSocketClosed | Close callback for accepted TCP sockets; removes the socket from the list. |
bool | onSocketRecv | Receive callback for both UDP and TCP sockets; parses STUN messages and calls handleRequest() for each one. |
void | onTimer | Periodic maintenance callback; expires and removes stale allocations. |
void | scheduleDeferredTCPSocketRelease | Defers accepted TCP socket removal until after the active callback stack unwinds. |
void | drainReleasedTCPSockets |
Server(ServerObserver & observer, const ServerOptions & options)observer Observer used for authentication and allocation lifecycle events.
options Server configuration; defaults to 0.0.0.0:3478 with TCP and UDP enabled.
virtual
virtual void start()Binds and listens on the configured address, then starts the maintenance timer.
virtual
virtual void stop()Stops the timer, destroys all allocations, and closes server sockets.
void handleRequest(Request & request, AuthenticationState state)Routes an authenticated request to the appropriate handler based on state. Pending (Authenticating) requests are held until the observer calls back.
request Incoming STUN request.
state Result of the observer's authenticateRequest() call.
void handleAuthorizedRequest(Request & request)Dispatches an already-authorized request to the specific method handler.
request Authorized STUN request.void handleBindingRequest(Request & request)Handles a Binding request; responds with XOR-MAPPED-ADDRESS.
request Incoming Binding request.void handleAllocateRequest(Request & request)Handles an Allocate request; creates a UDP or TCP ServerAllocation and sends a success response with XOR-RELAYED-ADDRESS and LIFETIME.
request Incoming Allocate request.void handleConnectionBindRequest(Request & request)Handles a ConnectionBind request by locating the TCPAllocation that owns the given CONNECTION-ID and delegating to it.
request Incoming ConnectionBind request.void respond(Request & request, stun::Message & response)Sends a STUN response, signing it with MessageIntegrity if the request had a hash. Routes via UDP or TCP depending on request.transport.
request The original request (provides transport and remote address).
response The response message to send.
void respondError(Request & request, int errorCode, const char * errorDesc)Constructs and sends an error response with SOFTWARE, REALM, NONCE, and ERROR-CODE.
request The original request.
errorCode STUN error code (e.g. 400, 401, 437).
errorDesc Human-readable error description string.
const
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.
Map from FiveTuple to raw ServerAllocation pointers.
void addAllocation(std::unique_ptr< ServerAllocation > alloc)Transfers ownership of alloc to the server and notifies the observer.
alloc Newly constructed allocation to register.void removeAllocation(ServerAllocation * alloc)Removes alloc from the map and notifies the observer. Called automatically from the ServerAllocation destructor.
alloc Allocation being destroyed.ServerAllocation * getAllocation(const FiveTuple & tuple)Looks up an allocation by its 5-tuple.
tuple The 5-tuple to search for.Pointer to the matching allocation, or nullptr if not found.
TCPAllocation * getTCPAllocation(const uint32_t & connectionID)Finds the TCPAllocation that owns a TCPConnectionPair with the given connection ID.
connectionID TURN CONNECTION-ID to search for.Pointer to the owning TCPAllocation, or nullptr if not found.
net::TCPSocket::Ptr getTCPSocket(const net::Address & remoteAddr)Returns the accepted TCP socket whose peer address matches remoteAddr.
remoteAddr Peer address to search for.Shared pointer to the socket, or empty if not found.
void releaseTCPSocket(const net::Socket & socket)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).
socket The socket to release.ServerObserver & observer()Reference to the observer provided at construction.
const
const ServerOptions & options() constReference to the immutable options struct.
net::UDPSocket & udpSocket()Reference to the UDP server socket.
net::TCPSocket & tcpSocket()Reference to the TCP server listening socket.
Timer & timer()Reference to the maintenance timer.
void onTCPAcceptConnection(const net::TCPSocket::Ptr & sock)Accept callback for the TCP listening socket; registers new connections for STUN message processing.
sock Newly accepted TCP socket.bool onTCPSocketClosed(net::Socket & socket)Close callback for accepted TCP sockets; removes the socket from the list.
socket The closed socket.bool onSocketRecv(net::Socket & socket, const MutableBuffer & buffer, const net::Address & peerAddress)Receive callback for both UDP and TCP sockets; parses STUN messages and calls handleRequest() for each one.
socket The receiving socket.
buffer Received data buffer.
peerAddress Source address of the data.
void onTimer()Periodic maintenance callback; expires and removes stale allocations.
void scheduleDeferredTCPSocketRelease()Defers accepted TCP socket removal until after the active callback stack unwinds.
void drainReleasedTCPSockets()| Return | Name | Description |
|---|---|---|
std::mutex | _mutex | |
ServerObserver & | _observer | |
ServerOptions | _options | |
net::SocketEmitter | _udpSocket | |
net::SocketEmitter | _tcpSocket | |
std::vector< net::SocketEmitter > | _tcpSockets | |
std::unordered_set< const net::Socket * > | _pendingReleasedTCPSockets | |
bool | _tcpSocketReleaseScheduled | |
ServerAllocationMap | _allocations | |
Timer | _timer |
std::mutex _mutexServerObserver & _observerServerOptions _optionsnet::SocketEmitter _udpSocketnet::SocketEmitter _tcpSocketstd::vector< net::SocketEmitter > _tcpSocketsstd::unordered_set< const net::Socket * > _pendingReleasedTCPSocketsbool _tcpSocketReleaseScheduled {false}ServerAllocationMap _allocationsTimer _timer