Xirsys.Sockets.Listener.UDP (xturn_sockets v1.2.0)

View Source

Enhanced 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 received
  • client_ip - Client IP address tuple
  • client_port - Client port number
  • server_ip - Server IP address tuple
  • server_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

child_spec(init_arg)

Returns a specification to start this module under a supervisor.

See Supervisor.

handle_cast(msg, state)

Enhanced asynchronous socket response handler with rate limiting

handle_info(msg, state)

Enhanced message handler for incoming UDP packets with security checks

init(list)

Initialises connection with IPv6 address and enhanced options

start_link(cb, ip, port)

Standard OTP module startup with enhanced configuration

start_link(cb, ip, port, ssl)