GenServer owning one TURN allocation (relay, permissions, channels, TCP relay).
What problem this solves
After a successful Allocate, the client needs a relay address on the server,
permission to send to peer IPs, optional channel bindings for efficient
media, and periodic Refresh to keep the allocation alive. One GenServer per
allocation holds relay sockets, timed permissions/channels, byte counters, and
the client %ClientSocket{} so control and data paths share consistent state.
Internal note
Started by Allocate.Supervisor; indexed in Allocate.Store for fast lookup.
Operators interact via STUN/TURN and REST, not this module's functions.
RFCs
Summary
Functions
Returns address family integers (e.g. 4, 8) with active relays.
Grants permission for peer_ip (creates/refreshes timed permission entry).
Registers socket as the relay socket for address family family.
Assigns TCP listen socket for inbound peer connections on family.
Binds channel_number to peer_address (RFC 5766 ChannelBind).
Caches success response attributes for idempotent retransmits.
Returns cached attrs for {method, transactionid}, or nil.
Returns a specification to start this module under a supervisor.
Clears IPv4 header checksum field on the relay socket (platform-specific).
Completes RFC 6062 ConnectionBind: hands peer_socket to owner_pid.
Returns {:ok, count} of running allocation workers from the supervisor.
Starts an allocation under Allocate.Supervisor with default lifetime (600s).
Starts an allocation under Allocate.Supervisor with explicit lifetime (seconds).
Stops the allocation process gracefully (:normal reason).
Sets IP_DF (don't fragment) on the primary relay socket; may return :not_supported.
Enables don't-fragment behaviour for cross-family UDP relay.
Returns {username, passhash} credentials stored on the allocation.
Returns the allocation id string/key.
Returns current lifetime in seconds.
Returns {:ok, permissions_map} of active permission entries.
Returns list of relay transport addresses for all active families.
Opens an even UDP port (RFC 5766 EVEN-PORT / RESERVATION-TOKEN).
Opens a relay UDP port, preferring port on bind_ip.
Opens a relay UDP port on bind_ip using an ephemeral (random) port.
Opens a relay UDP port in [min, max] on bind_ip.
Refreshes allocation lifetime; lifetime 0 triggers deallocation.
Refreshes channel lifetime for id (channel number).
Relays data to {pip, pport} via socket and updates byte counters.
Removes channel binding channel_number.
Returns :udp or :tcp as requested at allocation time.
Sends data on channel to the bound peer.
Sends msg to the allocation's client using state-derived client address.
Sends msg to a client or peer via %ClientSocket{}, a UDP port, or relay %State{}.
Sends channel payload to a peer via the relay socket (fast path, no GenServer cast).
Sends a Send indication payload to peer_address.
Stores TURN long-term credentials on the allocation.
Associates billing/metadata namespace ns and peer_id with the allocation.
Records the primary relayed transport address (cast, no reply).
Publishes per-family relay {socket, address} map to Store.
Starts an allocation GenServer linked to the caller.
Splices a TCP client socket to a peer socket (RFC 6062 data transfer).
Opens an outbound TCP connection to peer (RFC 6062 Connect).
Refreshes permission (and optional channel) from live relay traffic.
Functions
Returns address family integers (e.g. 4, 8) with active relays.
Grants permission for peer_ip (creates/refreshes timed permission entry).
Registers socket as the relay socket for address family family.
Assigns TCP listen socket for inbound peer connections on family.
Binds channel_number to peer_address (RFC 5766 ChannelBind).
Returns :ok or {:error, :conflict}.
Caches success response attributes for idempotent retransmits.
Returns cached attrs for {method, transactionid}, or nil.
Returns a specification to start this module under a supervisor.
See Supervisor.
Clears IPv4 header checksum field on the relay socket (platform-specific).
Completes RFC 6062 ConnectionBind: hands peer_socket to owner_pid.
connection_id must match a pending inbound/outbound TCP connection.
Returns {:ok, peer_socket} or {:error, :not_found}.
Returns {:ok, count} of running allocation workers from the supervisor.
Starts an allocation under Allocate.Supervisor with default lifetime (600s).
Starts an allocation under Allocate.Supervisor with explicit lifetime (seconds).
Stops the allocation process gracefully (:normal reason).
Sets IP_DF (don't fragment) on the primary relay socket; may return :not_supported.
Enables don't-fragment behaviour for cross-family UDP relay.
Returns {username, passhash} credentials stored on the allocation.
Returns the allocation id string/key.
Returns current lifetime in seconds.
Returns {:ok, permissions_map} of active permission entries.
Returns list of relay transport addresses for all active families.
Opens an even UDP port (RFC 5766 EVEN-PORT / RESERVATION-TOKEN).
When reserve_next? is true, reserves the following odd port as well.
Returns {:ok, socket, port, token} or {:error, reason}.
Opens a relay UDP port, preferring port on bind_ip.
Opens a relay UDP port on bind_ip using an ephemeral (random) port.
Opens a relay UDP port in [min, max] on bind_ip.
Refreshes allocation lifetime; lifetime 0 triggers deallocation.
Refreshes channel lifetime for id (channel number).
Relays data to {pip, pport} via socket and updates byte counters.
Used by the media fast path when socket and peer are already resolved.
Removes channel binding channel_number.
Returns :udp or :tcp as requested at allocation time.
Sends data on channel to the bound peer.
When socket and peer_address are provided, sends synchronously on the
fast path without a GenServer cast. Otherwise queues {:send_channel, ...}.
Sends msg to the allocation's client using state-derived client address.
Parameters
msg- encoded STUN or ChannelData binarystate- allocation%State{}
Sends msg to a client or peer via %ClientSocket{}, a UDP port, or relay %State{}.
Sends channel payload to a peer via the relay socket (fast path, no GenServer cast).
Parameters
data- channel payloadpeer_address-{ip, port}peer tuplesocket- relay UDP portstate- optional%State{}for DF/recv_meta; defaults to bare relay send
Sends a Send indication payload to peer_address.
With socket and perms set, checks permissions and sends on the fast path.
Otherwise casts to the GenServer for permission-gated UDP send.
Stores TURN long-term credentials on the allocation.
Associates billing/metadata namespace ns and peer_id with the allocation.
Records the primary relayed transport address (cast, no reply).
Publishes per-family relay {socket, address} map to Store.
relays is {family => {socket, port, address}}.
Starts an allocation GenServer linked to the caller.
id is the allocation key in Store. tuple5 identifies the client
five-tuple; lifetime is the initial refresh interval in seconds.
Splices a TCP client socket to a peer socket (RFC 6062 data transfer).
Opens an outbound TCP connection to peer (RFC 6062 Connect).
Returns {:ok, connection_id} or {:error, :exists} / {:error, :timeout}.
Refreshes permission (and optional channel) from live relay traffic.
Casts so the media path never blocks.