# pion/turn architecture

> Architecture diagram of the GitHub repository pion/turn, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-08-19). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/pion/turn
- Repository: https://github.com/pion/turn

## Overview

#### 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: [`internal/server/server.go`](https://github.com/pion/turn/blob/main/internal/server/server.go) (Server runtime), [`internal/server/turn.go`](https://github.com/pion/turn/blob/main/internal/server/turn.go) (TURN handler), [`internal/allocation/allocation.go`](https://github.com/pion/turn/blob/main/internal/allocation/allocation.go) (Allocation state), [`internal/client/client.go`](https://github.com/pion/turn/blob/main/internal/client/client.go) (Client runtime)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_public["Public API"]
  node_server_api["Server API<br/>Go API<br/>[server.go]"]
  node_server_config["Server configuration<br/>Go API<br/>[server_config.go]"]
  node_client_api["Client API<br/>Go API<br/>[client.go]"]
end

subgraph group_server["Server Runtime"]
  node_server_runtime["Server runtime<br/>[server.go]"]
  node_stun_handler["STUN handler<br/>[stun.go]"]
  node_turn_handler["TURN handler<br/>[turn.go]"]
  node_nonce["Nonce generation<br/>auth state<br/>[nonce.go]"]
end

subgraph group_allocation["Allocation &amp; Relay"]
  node_allocation_manager[("Allocation manager<br/>state manager")]
  node_allocation_state["Allocation state<br/>state machine<br/>[allocation.go]"]
  node_five_tuple["Five-tuple identity<br/>key<br/>[five_tuple.go]"]
  node_peer_controls["Permissions &amp; channels<br/>forwarding policy<br/>[permission.go]"]
  node_relay_policy["Relay address policy<br/>extension point"]
end

subgraph group_client["Client Runtime"]
  node_client_runtime["Client runtime<br/>[client.go]"]
  node_transactions["Transactions<br/>request state<br/>[transaction.go]"]
  node_client_allocation["Client allocation ops<br/>operations<br/>[allocation.go]"]
end

subgraph group_protocol["Protocol &amp; Auth"]
  node_wire_protocol["TURN/STUN protocol<br/>codec<br/>[proto.go]"]
  node_stun_framing["STUN connection framing<br/>transport adapter<br/>[stun_conn.go]"]
  node_auth["Long-term authentication<br/>credential validation<br/>[auth.go]"]
end

node_app["Embedding application<br/>consumer"]
node_sockets{{"Go socket/TLS runtime<br/>network transport"}}
node_examples["Integration examples<br/>[main.go]"]
node_e2e["Coturn interoperability<br/>end-to-end validation<br/>[e2e.go]"]

node_app -->|"configures"| node_server_config
node_app -->|"constructs"| node_server_api
node_app -->|"uses"| node_client_api
node_server_api -->|"starts"| node_server_runtime
node_server_config -->|"supplies resources"| node_server_runtime
node_server_runtime -->|"dispatches binding"| node_stun_handler
node_server_runtime -->|"dispatches TURN"| node_turn_handler
node_server_runtime -->|"accepts traffic"| node_sockets
node_turn_handler -->|"creates and refreshes"| node_allocation_manager
node_turn_handler -->|"validates requests"| node_auth
node_turn_handler -->|"uses"| node_nonce
node_allocation_manager -->|"indexes by"| node_five_tuple
node_allocation_manager -->|"owns lifecycle"| node_allocation_state
node_allocation_state -->|"enforces"| node_peer_controls
node_allocation_state -->|"selects relay address"| node_relay_policy
node_allocation_state -->|"forwards relay traffic"| node_sockets
node_client_api -->|"drives"| node_client_runtime
node_client_runtime -->|"manages"| node_transactions
node_client_runtime -->|"exposes operations"| node_client_allocation
node_client_runtime -->|"uses"| node_stun_framing
node_transactions -->|"encodes requests"| node_wire_protocol
node_wire_protocol -->|"frames"| node_stun_framing
node_stun_framing -->|"reads and writes"| node_sockets
node_examples -.->|"demonstrates"| node_server_api
node_examples -.->|"demonstrates"| node_client_api
node_e2e -.->|"validates"| node_server_runtime

click node_server_api "https://github.com/pion/turn/blob/main/server.go"
click node_server_config "https://github.com/pion/turn/blob/main/server_config.go"
click node_client_api "https://github.com/pion/turn/blob/main/client.go"
click node_server_runtime "https://github.com/pion/turn/blob/main/internal/server/server.go"
click node_stun_handler "https://github.com/pion/turn/blob/main/internal/server/stun.go"
click node_turn_handler "https://github.com/pion/turn/blob/main/internal/server/turn.go"
click node_nonce "https://github.com/pion/turn/blob/main/internal/server/nonce.go"
click node_allocation_manager "https://github.com/pion/turn/blob/main/internal/allocation/allocation_manager.go"
click node_allocation_state "https://github.com/pion/turn/blob/main/internal/allocation/allocation.go"
click node_five_tuple "https://github.com/pion/turn/blob/main/internal/allocation/five_tuple.go"
click node_peer_controls "https://github.com/pion/turn/blob/main/internal/allocation/permission.go"
click node_relay_policy "https://github.com/pion/turn/blob/main/relay_address_generator_range.go"
click node_client_runtime "https://github.com/pion/turn/blob/main/internal/client/client.go"
click node_transactions "https://github.com/pion/turn/blob/main/internal/client/transaction.go"
click node_client_allocation "https://github.com/pion/turn/blob/main/internal/client/allocation.go"
click node_wire_protocol "https://github.com/pion/turn/blob/main/internal/proto/proto.go"
click node_stun_framing "https://github.com/pion/turn/blob/main/internal/proto/stun_conn.go"
click node_auth "https://github.com/pion/turn/blob/main/internal/auth/auth.go"
click node_examples "https://github.com/pion/turn/blob/main/examples/turn-server/simple/main.go"
click node_e2e "https://github.com/pion/turn/blob/main/e2e/e2e.go"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_server_api,node_server_config,node_client_api toneBlue
class node_server_runtime,node_stun_handler,node_turn_handler,node_nonce toneAmber
class node_allocation_manager,node_allocation_state,node_five_tuple,node_peer_controls,node_relay_policy toneMint
class node_client_runtime,node_transactions,node_client_allocation toneRose
class node_wire_protocol,node_stun_framing,node_auth toneIndigo
class node_app,node_sockets,node_examples,node_e2e toneNeutral
```

## Components

### Public API

Embeddable entry points supplied with application resources.

- [Server API](https://github.com/pion/turn/blob/main/server.go): Go API · `server.go`
- [Server configuration](https://github.com/pion/turn/blob/main/server_config.go): Go API · `server_config.go`
- [Client API](https://github.com/pion/turn/blob/main/client.go): Go API · `client.go`

### Server Runtime

Dispatches STUN/TURN and owns server allocation state.

- [Server runtime](https://github.com/pion/turn/blob/main/internal/server/server.go): runtime · `internal/server/server.go`
- [STUN handler](https://github.com/pion/turn/blob/main/internal/server/stun.go): handler · `internal/server/stun.go`
- [TURN handler](https://github.com/pion/turn/blob/main/internal/server/turn.go): handler · `internal/server/turn.go`
- [Nonce generation](https://github.com/pion/turn/blob/main/internal/server/nonce.go): auth state · `internal/server/nonce.go`

### Allocation & Relay

Tracks relay state and peer forwarding controls.

- [Allocation manager](https://github.com/pion/turn/blob/main/internal/allocation/allocation_manager.go): state manager · Indexes allocations by client/server/transport five-tuple. · `internal/allocation/allocation_manager.go`
- [Allocation state](https://github.com/pion/turn/blob/main/internal/allocation/allocation.go): state machine · Owns relay socket, lifetime, permissions, and channel bindings. · `internal/allocation/allocation.go`
- [Five-tuple identity](https://github.com/pion/turn/blob/main/internal/allocation/five_tuple.go): key · `internal/allocation/five_tuple.go`
- [Permissions & channels](https://github.com/pion/turn/blob/main/internal/allocation/permission.go): forwarding policy · Authorizes peers and maps channel numbers to peer addresses. · `internal/allocation/permission.go`
- [Relay address policy](https://github.com/pion/turn/blob/main/relay_address_generator_range.go): extension point · Applications select none, range, or static relay address generation. · `relay_address_generator_range.go`

### Client Runtime

Manages client transactions and TURN operations.

- [Client runtime](https://github.com/pion/turn/blob/main/internal/client/client.go): runtime · `internal/client/client.go`
- [Transactions](https://github.com/pion/turn/blob/main/internal/client/transaction.go): request state · Correlates responses and manages retries and timers. · `internal/client/transaction.go`
- [Client allocation ops](https://github.com/pion/turn/blob/main/internal/client/allocation.go): operations · Binding, allocation, permission, channel, and TCP allocation operations. · `internal/client/allocation.go`

### Protocol & Auth

Wire framing and credential validation.

- [TURN/STUN protocol](https://github.com/pion/turn/blob/main/internal/proto/proto.go): codec · TURN attributes and ChannelData framing. · `internal/proto/proto.go`
- [STUN connection framing](https://github.com/pion/turn/blob/main/internal/proto/stun_conn.go): transport adapter · `internal/proto/stun_conn.go`
- [Long-term authentication](https://github.com/pion/turn/blob/main/internal/auth/auth.go): credential validation · `internal/auth/auth.go`

### Other components

- **Embedding application**: consumer · Supplies listeners, TLS, auth, and relay policy.
- **Go socket/TLS runtime**: network transport · UDP, TCP, TLS, IPv6, and relay connections.
- [Integration examples](https://github.com/pion/turn/blob/main/examples/turn-server/simple/main.go): examples · Server, UDP client, and mutual-TLS embedding patterns. · `examples/turn-server/simple/main.go`
- [Coturn interoperability](https://github.com/pion/turn/blob/main/e2e/e2e.go): end-to-end validation · `e2e/e2e.go`

## Connections

- 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
