-
Notifications
You must be signed in to change notification settings - Fork 0
/
candidate.go
276 lines (241 loc) · 6.66 KB
/
candidate.go
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
package ice
import (
"fmt"
"net"
"sync"
"time"
"github.com/pion/stun"
)
const (
receiveMTU = 8192
defaultLocalPreference = 65535
// ComponentRTP indicates that the candidate is used for RTP
ComponentRTP uint16 = 1
// ComponentRTCP indicates that the candidate is used for RTCP
ComponentRTCP
)
// Candidate represents an ICE candidate
type Candidate struct {
NetworkType
Type CandidateType
LocalPreference uint16
Component uint16
IP net.IP
Port int
RelatedAddress *CandidateRelatedAddress
lock sync.RWMutex
lastSent time.Time
lastReceived time.Time
agent *Agent
conn net.PacketConn
closeCh chan struct{}
closedCh chan struct{}
}
// NewCandidateHost creates a new host candidate
func NewCandidateHost(network string, ip net.IP, port int, component uint16) (*Candidate, error) {
networkType, err := determineNetworkType(network, ip)
if err != nil {
return nil, err
}
return &Candidate{
Type: CandidateTypeHost,
NetworkType: networkType,
IP: ip,
Port: port,
LocalPreference: defaultLocalPreference,
Component: component,
}, nil
}
// NewCandidateServerReflexive creates a new server reflective candidate
func NewCandidateServerReflexive(network string, ip net.IP, port int, component uint16, relAddr string, relPort int) (*Candidate, error) {
networkType, err := determineNetworkType(network, ip)
if err != nil {
return nil, err
}
return &Candidate{
Type: CandidateTypeServerReflexive,
NetworkType: networkType,
IP: ip,
Port: port,
LocalPreference: defaultLocalPreference,
Component: component,
RelatedAddress: &CandidateRelatedAddress{
Address: relAddr,
Port: relPort,
},
}, nil
}
// NewCandidatePeerReflexive creates a new peer reflective candidate
func NewCandidatePeerReflexive(network string, ip net.IP, port int, component uint16, relAddr string, relPort int) (*Candidate, error) {
networkType, err := determineNetworkType(network, ip)
if err != nil {
return nil, err
}
return &Candidate{
Type: CandidateTypePeerReflexive,
NetworkType: networkType,
IP: ip,
Port: port,
LocalPreference: defaultLocalPreference,
Component: component,
RelatedAddress: &CandidateRelatedAddress{
Address: relAddr,
Port: relPort,
},
}, nil
}
// NewCandidateRelay creates a new relay candidate
func NewCandidateRelay(network string, ip net.IP, port int, component uint16, relAddr string, relPort int) (*Candidate, error) {
networkType, err := determineNetworkType(network, ip)
if err != nil {
return nil, err
}
return &Candidate{
Type: CandidateTypeRelay,
NetworkType: networkType,
IP: ip,
Port: port,
LocalPreference: defaultLocalPreference,
Component: component,
RelatedAddress: &CandidateRelatedAddress{
Address: relAddr,
Port: relPort,
},
}, nil
}
// start runs the candidate using the provided connection
func (c *Candidate) start(a *Agent, conn net.PacketConn) {
c.agent = a
c.conn = conn
c.closeCh = make(chan struct{})
c.closedCh = make(chan struct{})
go c.recvLoop()
}
func (c *Candidate) recvLoop() {
defer func() {
close(c.closedCh)
}()
log := c.agent.log
buffer := make([]byte, receiveMTU)
for {
n, srcAddr, err := c.conn.ReadFrom(buffer)
if err != nil {
return
}
if stun.IsSTUN(buffer[:n]) {
var m *stun.Message
m, err = stun.NewMessage(buffer[:n])
if err != nil {
log.Warnf("Failed to handle decode ICE from %s to %s: %v", c.addr(), srcAddr, err)
continue
}
err = c.agent.run(func(agent *Agent) {
agent.handleInbound(m, c, srcAddr)
})
if err != nil {
log.Warnf("Failed to handle message: %v", err)
}
continue
}
isValidRemoteCandidate := make(chan bool, 1)
err = c.agent.run(func(agent *Agent) {
isValidRemoteCandidate <- agent.noSTUNSeen(c, srcAddr)
})
if err != nil {
log.Warnf("Failed to handle message: %v", err)
} else if !<-isValidRemoteCandidate {
log.Warnf("Discarded message from %s, not a valid remote candidate", c.addr())
}
// NOTE This will return packetio.ErrFull if the buffer ever manages to fill up.
_, err = c.agent.buffer.Write(buffer[:n])
if err != nil {
log.Warnf("failed to write packet")
}
}
}
// close stops the recvLoop
func (c *Candidate) close() error {
c.lock.Lock()
defer c.lock.Unlock()
if c.conn != nil {
// Unblock recvLoop
close(c.closeCh)
// Close the conn
err := c.conn.Close()
if err != nil {
return err
}
// Wait until the recvLoop is closed
<-c.closedCh
}
return nil
}
func (c *Candidate) writeTo(raw []byte, dst *Candidate) (int, error) {
n, err := c.conn.WriteTo(raw, dst.addr())
if err != nil {
return n, fmt.Errorf("failed to send packet: %v", err)
}
c.seen(true)
return n, nil
}
// Priority computes the priority for this ICE Candidate
func (c *Candidate) Priority() uint32 {
// The local preference MUST be an integer from 0 (lowest preference) to
// 65535 (highest preference) inclusive. When there is only a single IP
// address, this value SHOULD be set to 65535. If there are multiple
// candidates for a particular component for a particular data stream
// that have the same type, the local preference MUST be unique for each
// one.
return (1<<24)*uint32(c.Type.Preference()) +
(1<<8)*uint32(c.LocalPreference) +
uint32(256-c.Component)
}
// Equal is used to compare two CandidateBases
func (c *Candidate) Equal(other *Candidate) bool {
return c.NetworkType == other.NetworkType &&
c.Type == other.Type &&
c.IP.Equal(other.IP) &&
c.Port == other.Port &&
c.RelatedAddress.Equal(other.RelatedAddress)
}
// String makes the CandidateHost printable
func (c *Candidate) String() string {
return fmt.Sprintf("%s %s:%d%s", c.Type, c.IP, c.Port, c.RelatedAddress)
}
// LastReceived returns a time.Time indicating the last time
// this candidate was received
func (c *Candidate) LastReceived() time.Time {
c.lock.RLock()
defer c.lock.RUnlock()
return c.lastReceived
}
func (c *Candidate) setLastReceived(t time.Time) {
c.lock.Lock()
defer c.lock.Unlock()
c.lastReceived = t
}
// LastSent returns a time.Time indicating the last time
// this candidate was sent
func (c *Candidate) LastSent() time.Time {
c.lock.RLock()
defer c.lock.RUnlock()
return c.lastSent
}
func (c *Candidate) setLastSent(t time.Time) {
c.lock.Lock()
defer c.lock.Unlock()
c.lastSent = t
}
func (c *Candidate) seen(outbound bool) {
if outbound {
c.setLastSent(time.Now())
} else {
c.setLastReceived(time.Now())
}
}
func (c *Candidate) addr() net.Addr {
return &net.UDPAddr{
IP: c.IP,
Port: c.Port,
}
}