Xirsys.Sockets.Listener.UDP (xturn_sockets v1.2.0)
View SourceEnhanced UDP protocol socket handler optimized for TURN server usage.
Includes rate limiting, connection tracking, and robust error handling. Designed specifically for STUN/TURN protocols (RFC 5389, RFC 5766) which require 5-tuple information for proper operation.
## Callback Interface
Your callback module must implement:
def process_buffer(data, client_ip, client_port, server_ip, server_port)
Where:
data
- Binary packet data receivedclient_ip
- Client IP address tupleclient_port
- Client port numberserver_ip
- Server IP address tupleserver_port
- Server port number
And:
def dispatch(conn)
Where:
conn
-%Xirsys.Sockets.Conn{}
struct with parsed message and connection info
The 5-tuple information during process_buffer
is essential for:
- STUN XOR-MAPPED-ADDRESS responses (RFC 5389)
- TURN allocation and permission management (RFC 5766)
- Proper WebRTC compatibility
Example
defmodule MySTUNHandler do
def process_buffer(data, client_ip, client_port, server_ip, server_port) do
# Parse STUN/TURN packet with full connection context
case parse_stun_packet(data, client_ip, client_port, server_ip, server_port) do
{:ok, packet} -> {packet, <<>>}
{:error, _} -> {nil, <<>>}
end
end
def dispatch(conn) do
# Handle processed STUN/TURN message
send_stun_response(conn)
end
end
Summary
Functions
Returns a specification to start this module under a supervisor.
Enhanced asynchronous socket response handler with rate limiting
Enhanced message handler for incoming UDP packets with security checks
Initialises connection with IPv6 address and enhanced options
Standard OTP module startup with enhanced configuration
Functions
Returns a specification to start this module under a supervisor.
See Supervisor
.
Enhanced asynchronous socket response handler with rate limiting
Enhanced message handler for incoming UDP packets with security checks
Initialises connection with IPv6 address and enhanced options
Standard OTP module startup with enhanced configuration