How pion/turn works
Purpose and public surface
Pion TURN is an embeddable Go library for implementing STUN/TURN clients and servers, including UDP, TCP, TLS, IPv6, and TCP relay allocations. It is not a standalone configured daemon; consuming programs instantiate its APIs. Main exported entry points are server construction/configuration in server.go, server_config.go, and client access in client.go.
Server lifecycle and transport boundary
The server accepts application-supplied listener/packet-connection resources, dispatches incoming STUN/TURN traffic, and owns protocol session handling. server.go exposes the public server lifecycle; internal/server/server.go contains the core runtime; internal/server/stun.go and internal/server/turn.go split STUN binding behavior from TURN request handling. Network transport is Go’s socket/TLS runtime, with TLS setup delegated by embedding applications.
TURN allocation state machine
TURN allocations are the central server-side state boundary: a client/server/transport five-tuple identifies an allocation, which owns a relayed socket, lifetime, permissions, and channel bindings. internal/allocation/allocation_manager.go indexes and manages allocation lifecycle; internal/allocation/allocation.go represents an allocation; internal/allocation/five_tuple.go defines its identity. Server TURN handlers create, refresh, delete, and route through this state.
Relay forwarding, permissions, and channels
After allocation, peer traffic is constrained through permission and channel-binding state before being forwarded between client and relayed peer connections. internal/allocation/permission.go governs authorized peer addresses; internal/allocation/channel_bind.go maps compact TURN channel numbers to peers; internal/allocation/event_handler.go connects allocation events back to server behavior. Relay address selection is customizable through relay_address_generator_none.go, relay_address_generator_range.go, and relay_address_generator_static.go.
Protocol encoding and authentication
Wire parsing/serialization extends the STUN ecosystem with TURN attributes and ChannelData framing. internal/proto/proto.go is the protocol layer, while internal/proto/stun_conn.go adapts connections for STUN framing and internal/proto/chandata.go handles TURN channel data. Authentication policy enters through public long-term credential support in lt_cred.go and internal validation in internal/auth/auth.go; server nonce generation is implemented in internal/server/nonce.go.
Client transaction and allocation flow
Clients send STUN/TURN transactions, correlate responses, retry/manage timers, then expose operations for binding, allocation, permissions, and channels. client.go is the public client API; internal/client/client.go coordinates connections and transactions; internal/client/transaction.go tracks request/response state. Allocation and peer-control operations reside in internal/client/allocation.go and internal/client/permission.go, with TCP allocation support in internal/client/tcp_alloc.go.
Deployment and integration model
Applications compile this module directly with Go and provide their own authentication, observability, listeners, and relay-address policy. Runnable integration patterns are in examples/turn-server/simple/main.go, examples/turn-client/udp/main.go, and examples/mutual-tls-auth/turn-server/main.go. End-to-end validation uses containerized Coturn interoperability via e2e/Dockerfile and e2e/e2e.go.
Main entry points: server.go Server runtime · turn.go TURN handler · allocation.go Allocation state · client.go Client runtime
Components 22
Public API
Embeddable entry points supplied with application resources.
- Server API
server.go - Server configuration
server_config.go - Client API
client.go
Server Runtime
Dispatches STUN/TURN and owns server allocation state.
- Server runtime
internal/server/server.go - STUN handler
internal/server/stun.go - TURN handler
internal/server/turn.go - Nonce generation
internal/server/nonce.go
Allocation & Relay
Tracks relay state and peer forwarding controls.
- Allocation manager · Indexes allocations by client/server/transport five-tuple.
internal/allocation/allocation_manager.go - Allocation state · Owns relay socket, lifetime, permissions, and channel bindings.
internal/allocation/allocation.go - Five-tuple identity
internal/allocation/five_tuple.go - Permissions & channels · Authorizes peers and maps channel numbers to peer addresses.
internal/allocation/permission.go - Relay address policy · Applications select none, range, or static relay address generation.
relay_address_generator_range.go
Client Runtime
Manages client transactions and TURN operations.
- Client runtime
internal/client/client.go - Transactions · Correlates responses and manages retries and timers.
internal/client/transaction.go - Client allocation ops · Binding, allocation, permission, channel, and TCP allocation operations.
internal/client/allocation.go
Protocol & Auth
Wire framing and credential validation.
- TURN/STUN protocol · TURN attributes and ChannelData framing.
internal/proto/proto.go - STUN connection framing
internal/proto/stun_conn.go - Long-term authentication
internal/auth/auth.go
Other components
- Embedding application · Supplies listeners, TLS, auth, and relay policy.
- Go socket/TLS runtime · UDP, TCP, TLS, IPv6, and relay connections.
- Integration examples · Server, UDP client, and mutual-TLS embedding patterns.
examples/turn-server/simple/main.go - Coturn interoperability
e2e/e2e.go
Connections 26
- Embedding application → Server configuration: configures
- Embedding application → Server API: constructs
- Embedding application → Client API: uses
- Server API → Server runtime: starts
- Server configuration → Server runtime: supplies resources
- Server runtime → STUN handler: dispatches binding
- Server runtime → TURN handler: dispatches TURN
- Server runtime → Go socket/TLS runtime: accepts traffic
- TURN handler → Allocation manager: creates and refreshes
- TURN handler → Long-term authentication: validates requests
- TURN handler → Nonce generation: uses
- Allocation manager → Five-tuple identity: indexes by
- Allocation manager → Allocation state: owns lifecycle
- Allocation state → Permissions & channels: enforces
- Allocation state → Relay address policy: selects relay address
- Allocation state → Go socket/TLS runtime: forwards relay traffic
- Client API → Client runtime: drives
- Client runtime → Transactions: manages
- Client runtime → Client allocation ops: exposes operations
- Client runtime → STUN connection framing: uses
- Transactions → TURN/STUN protocol: encodes requests
- TURN/STUN protocol → STUN connection framing: frames
- STUN connection framing → Go socket/TLS runtime: reads and writes
- Integration examples → Server API: demonstrates
- Integration examples → Client API: demonstrates
- Coturn interoperability → Server runtime: validates