-
Notifications
You must be signed in to change notification settings - Fork 359
Expand file tree
/
Copy pathallocation.go
More file actions
385 lines (324 loc) · 10.2 KB
/
allocation.go
File metadata and controls
385 lines (324 loc) · 10.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
// SPDX-FileCopyrightText: 2023 The Pion community <https://pion.ly>
// SPDX-License-Identifier: MIT
// Package turn contains the public API for pion/turn, a toolkit for building TURN clients and servers.
package turn
import (
"context"
"errors"
"fmt"
"net"
"sync"
"time"
"github.com/pion/transport/v4"
"github.com/pion/transport/v4/deadline"
"github.com/pion/transport/v4/packetio"
"github.com/pion/turn/v5/internal/client"
)
const defaultAcceptBacklog = 128
var (
_ transport.Dialer = &turnAllocation{}
_ net.Listener = &turnAllocation{}
_ Allocation = &turnAllocation{}
)
// Typed errors.
var (
ErrAllocationClosed = errors.New("allocation: closed")
errFailedToAlloc = errors.New("allocation: failed to create allocation")
errFailedToDial = errors.New("allocation: failed to dial")
errInvalidNetwork = errors.New("allocation: invalid network")
)
// Allocation represents a TURN allocation that provides bidirectional communication with peers
// through a TURN relay server. It combines dialer and listener semantics: use Dial to establish
// outgoing connections to peers, and Accept to receive incoming connections from peers. The
// allocation is bound to a specific transport protocol (UDP or TCP) specified when created via
// NewAllocation. All connections through this allocation use that protocol.
type Allocation interface {
// Dial establishes a connection to the specified peer address through the TURN relay.
// The network parameter must match the allocation's protocol ("udp" or "tcp").
// Dial automatically creates a TURN permission for the peer, allowing the peer to
// send data back to this allocation's relay address.
Dial(network, address string) (net.Conn, error)
// Accept waits for the next incoming connection from a peer. The peer must have a valid
// permission (created via Dial or CreatePermission) to send data to this allocation's
// relay address.
Accept() (net.Conn, error)
// Addr returns the relay transport address of the allocation.
Addr() net.Addr
// Close releases the TURN allocation and closes all associated connections.
// Any blocked Accept or Dial calls will return ErrAllocationClosed.
Close() error
// CreatePermission installs a TURN permission for the specified peer address,
// allowing that peer to send data to this allocation's relay address.
CreatePermission(addr net.Addr) error
}
type turnAllocation struct {
*Client // TURN client
network string
tcpAlloc *client.TCPAllocation // for TCP allocations
dispatcher *packetDispatcher // for UDP allocations
closeCh chan struct{}
closeOnce sync.Once
mu sync.RWMutex
closed bool
}
// NewAllocation creates a TURN allocation that can dial peers and accept incoming connections.
func NewAllocation(network string, conf *ClientConfig) (Allocation, error) {
turnClient, err := NewClient(conf)
if err != nil {
return nil, fmt.Errorf("%w: %w", errFailedToAlloc, err)
}
if err = turnClient.Listen(); err != nil {
turnClient.Close()
return nil, fmt.Errorf("%w: %w", errFailedToAlloc, err)
}
turnAlloc := &turnAllocation{
Client: turnClient,
network: network,
closeCh: make(chan struct{}),
}
switch network {
case "udp", "udp4", "udp6": //nolint:goconst
pconn, err := turnClient.Allocate()
if err != nil {
return nil, fmt.Errorf("%w: %w", errFailedToAlloc, err)
}
turnAlloc.dispatcher = newPacketDispatcher(pconn, turnAlloc.closeCh)
case "tcp", "tcp4", "tcp6": //nolint:goconst
tcpAlloc, err := turnClient.AllocateTCP()
if err != nil {
return nil, fmt.Errorf("%w: %w", errFailedToAlloc, err)
}
turnAlloc.tcpAlloc = tcpAlloc
default:
return nil, errInvalidNetwork
}
return turnAlloc, nil
}
// Dial establishes a connection to the address through TURN. Network must be "udp", "udp4",
// "udp6" or "tcp", "tcp4", "tcp6".
func (a *turnAllocation) Dial(network, address string) (net.Conn, error) {
a.mu.RLock()
if a.closed {
a.mu.RUnlock()
return nil, ErrAllocationClosed
}
a.mu.RUnlock()
switch a.network {
case "udp", "udp4", "udp6": //nolint:goconst
return a.dialUDP(network, address)
case "tcp", "tcp4", "tcp6": //nolint:goconst
return a.dialTCP(network, address)
default:
return nil, errInvalidNetwork
}
}
func (a *turnAllocation) dialUDP(network, address string) (net.Conn, error) {
if network != a.network {
return nil, errInvalidNetwork
}
peerAddr, err := net.ResolveUDPAddr(network, address)
if err != nil {
return nil, fmt.Errorf("%w: %w", errFailedToDial, err)
}
if err = a.CreatePermission(peerAddr); err != nil {
return nil, fmt.Errorf("%w: %w", errFailedToDial, err)
}
return a.dispatcher.register(peerAddr), nil
}
func (a *turnAllocation) dialTCP(network, address string) (net.Conn, error) {
if network != a.network {
return nil, errInvalidNetwork
}
peerAddr, err := net.ResolveTCPAddr(network, address)
if err != nil {
return nil, fmt.Errorf("%w: %w", errFailedToDial, err)
}
if err = a.CreatePermission(peerAddr); err != nil {
return nil, fmt.Errorf("%w: %w", errFailedToDial, err)
}
return a.tcpAlloc.DialTCP(network, nil, peerAddr)
}
// Accept returns the next incoming connection from a permitted peer.
func (a *turnAllocation) Accept() (net.Conn, error) {
switch a.network {
case "udp", "udp4", "udp6": //nolint:goconst
select {
case <-a.closeCh:
return nil, ErrAllocationClosed
case conn := <-a.dispatcher.acceptCh:
return conn, nil
}
case "tcp", "tcp4", "tcp6": //nolint:goconst
return a.tcpAlloc.Accept()
default:
return nil, errInvalidNetwork
}
}
// Addr returns the transport relay address of the allocation.
func (a *turnAllocation) Addr() net.Addr {
switch a.network {
case "udp", "udp4", "udp6": //nolint:goconst
return a.dispatcher.pconn.LocalAddr()
case "tcp", "tcp4", "tcp6": //nolint:goconst
return a.tcpAlloc.RelayAddr()
default:
return nil
}
}
// Close closes the allocation and all associated connections.
func (a *turnAllocation) Close() error {
var err error
a.closeOnce.Do(func() {
switch a.network {
case "udp", "udp4", "udp6": //nolint:goconst
err = a.dispatcher.pconn.Close()
case "tcp", "tcp4", "tcp6": //nolint:goconst
err = a.tcpAlloc.Close()
default:
err = errInvalidNetwork
}
a.Client.Close()
a.mu.Lock()
a.closed = true
a.mu.Unlock()
close(a.closeCh)
})
return err
}
// CreatePermission admits a peer on the allocation. Note that Dial automatically creates a
// permission for the peer.
func (a *turnAllocation) CreatePermission(addr net.Addr) error {
return a.Client.CreatePermission(addr)
}
const receiveMTU = 8192
// packetDispatcher demultiplexes packets from a single PacketConn to multiple Conns.
type packetDispatcher struct {
pconn net.PacketConn
mu sync.Mutex
conns map[string]*dispatchedConn
acceptCh chan *dispatchedConn
closeCh chan struct{}
}
// newPacketDispatcher creates a dispatcher and starts its read loop.
func newPacketDispatcher(pconn net.PacketConn, closeCh chan struct{}) *packetDispatcher {
d := &packetDispatcher{
pconn: pconn,
conns: make(map[string]*dispatchedConn),
acceptCh: make(chan *dispatchedConn, defaultAcceptBacklog),
closeCh: closeCh,
}
go d.readLoop()
return d
}
func (d *packetDispatcher) readLoop() {
buf := make([]byte, receiveMTU)
for {
n, raddr, err := d.pconn.ReadFrom(buf)
if err != nil {
// Close all conn buffers on read error.
d.mu.Lock()
for _, c := range d.conns {
_ = c.buffer.Close()
}
d.mu.Unlock()
return
}
d.dispatch(raddr, buf[:n])
}
}
func (d *packetDispatcher) dispatch(raddr net.Addr, buf []byte) {
d.mu.Lock()
conn, ok := d.conns[raddr.String()]
if !ok {
// New peer - create conn and send to accept channel.
conn = d.newConn(raddr)
d.conns[raddr.String()] = conn
select {
case d.acceptCh <- conn:
// Sent to accept queue.
default:
// Accept queue full: unreg and drop connection
delete(d.conns, raddr.String())
}
}
d.mu.Unlock()
_, _ = conn.buffer.Write(buf)
}
// register adds a conn for the given peer address (used by Dial).
func (d *packetDispatcher) register(raddr net.Addr) *dispatchedConn {
d.mu.Lock()
defer d.mu.Unlock()
if conn, ok := d.conns[raddr.String()]; ok {
return conn
}
conn := d.newConn(raddr)
d.conns[raddr.String()] = conn
return conn
}
// newConn creates a new dispatchedConn (must be called with lock held).
func (d *packetDispatcher) newConn(raddr net.Addr) *dispatchedConn {
return &dispatchedConn{
dispatcher: d,
rAddr: raddr,
buffer: packetio.NewBuffer(),
writeDeadline: deadline.New(),
}
}
// unregister removes a conn from the dispatcher.
func (d *packetDispatcher) unregister(raddr net.Addr) {
d.mu.Lock()
delete(d.conns, raddr.String())
d.mu.Unlock()
}
// dispatchedConn is a net.Conn bound to a specific peer, receiving via the dispatcher.
type dispatchedConn struct {
dispatcher *packetDispatcher
rAddr net.Addr
buffer *packetio.Buffer
writeDeadline *deadline.Deadline
closeOnce sync.Once
}
// Read reads data from the buffer (dispatched by the read loop).
func (c *dispatchedConn) Read(b []byte) (int, error) {
return c.buffer.Read(b)
}
// Write sends data to the bound peer.
func (c *dispatchedConn) Write(b []byte) (int, error) {
select {
case <-c.writeDeadline.Done():
return 0, context.DeadlineExceeded
default:
}
return c.dispatcher.pconn.WriteTo(b, c.rAddr)
}
// Close unregisters from dispatcher and closes the buffer.
func (c *dispatchedConn) Close() error {
var err error
c.closeOnce.Do(func() {
c.dispatcher.unregister(c.rAddr)
err = c.buffer.Close()
})
return err
}
// LocalAddr returns the local address (relay address).
func (c *dispatchedConn) LocalAddr() net.Addr {
return c.dispatcher.pconn.LocalAddr()
}
// RemoteAddr returns the bound peer address.
func (c *dispatchedConn) RemoteAddr() net.Addr {
return c.rAddr
}
// SetDeadline sets both read and write deadlines.
func (c *dispatchedConn) SetDeadline(t time.Time) error {
c.writeDeadline.Set(t)
return c.buffer.SetReadDeadline(t)
}
// SetReadDeadline sets the read deadline.
func (c *dispatchedConn) SetReadDeadline(t time.Time) error {
return c.buffer.SetReadDeadline(t)
}
// SetWriteDeadline sets the write deadline.
func (c *dispatchedConn) SetWriteDeadline(t time.Time) error {
c.writeDeadline.Set(t)
return nil
}