Xirsys.XTurn.Accumulators.StunTurn (xturn v2.0.0)

Copy Markdown View Source

STUN/TURN framing accumulator for one client connection.

What problem this solves

TURN multiplexes STUN messages (first two bits 00) and ChannelData frames (first two bits 01) on the same socket. This accumulator buffers incoming bytes, splits them into complete messages, and strips transport-specific ChannelData padding before handing frames to the handler.

:framing selects padding rules:

  • :stream - padding required (TCP/TLS); consumed after each ChannelData
  • :datagram - padding optional (UDP/DTLS); unpadded frames are accepted

Emitted messages never include trailing alignment bytes.

Internal

Used as the :accumulator tier in SocketPipeline; not called by application code directly.

RFCs

  • RFC 5389 - STUN message length and attribute padding (pt.15)
  • RFC 5766 - ChannelData format and padding (pt.11, pt.11.5)

Summary

Functions

Accumulator state: :max_size (default 1 MiB) and :framing (:stream or :datagram).

Extracts the next complete STUN or ChannelData message from the buffer.

Appends chunk to the internal buffer.

Functions

init(opts)

Accumulator state: :max_size (default 1 MiB) and :framing (:stream or :datagram).

See module doc for ChannelData padding rules per transport.

pop(acc)

Extracts the next complete STUN or ChannelData message from the buffer.

Returns {:ok, message, meta, acc}, {:more, acc} when incomplete, or {:error, reason, acc} on overflow or unknown framing.

push(acc, chunk, meta)

Appends chunk to the internal buffer.

When the buffer exceeds :max_size, oldest bytes are dropped and :overflow is set; the next pop/1 returns {:error, :buffer_overflow, acc}.