-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathselection.go
More file actions
488 lines (401 loc) · 15.6 KB
/
selection.go
File metadata and controls
488 lines (401 loc) · 15.6 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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
// SPDX-FileCopyrightText: 2026 The Pion community <https://pion.ly>
// SPDX-License-Identifier: MIT
package ice
import (
"net"
"time"
"github.com/pion/logging"
"github.com/pion/stun/v3"
)
type pairCandidateSelector interface {
Start()
ContactCandidates()
PingCandidate(local, remote Candidate)
HandleSuccessResponse(m *stun.Message, local, remote Candidate, remoteAddr net.Addr)
HandleBindingRequest(m *stun.Message, local, remote Candidate)
}
type controllingSelector struct {
startTime time.Time
agent *Agent
nominatedPair *CandidatePair
log logging.LeveledLogger
}
func (s *controllingSelector) Start() {
s.startTime = time.Now()
s.nominatedPair = nil
}
func (s *controllingSelector) isNominatable(c Candidate) bool {
switch {
case c.Type() == CandidateTypeHost:
return time.Since(s.startTime).Nanoseconds() > s.agent.hostAcceptanceMinWait.Nanoseconds()
case c.Type() == CandidateTypeServerReflexive:
return time.Since(s.startTime).Nanoseconds() > s.agent.srflxAcceptanceMinWait.Nanoseconds()
case c.Type() == CandidateTypePeerReflexive:
return time.Since(s.startTime).Nanoseconds() > s.agent.prflxAcceptanceMinWait.Nanoseconds()
case c.Type() == CandidateTypeRelay:
return time.Since(s.startTime).Nanoseconds() > s.agent.relayAcceptanceMinWait.Nanoseconds()
}
s.log.Errorf("Invalid candidate type: %s", c.Type())
return false
}
func (s *controllingSelector) ContactCandidates() {
switch {
case s.agent.getSelectedPair() != nil:
if s.agent.validateSelectedPair() {
s.log.Trace("Checking keepalive")
s.agent.checkKeepalive()
// If automatic renomination is enabled, continuously ping all candidate pairs
// to keep them tested with fresh RTT measurements for switching decisions
if s.agent.automaticRenomination && s.agent.enableRenomination {
s.agent.keepAliveCandidatesForRenomination()
}
s.checkForAutomaticRenomination()
}
case s.nominatedPair != nil:
s.nominatePair(s.nominatedPair)
default:
p := s.agent.getBestValidCandidatePair()
if p != nil && s.isNominatable(p.Local) && s.isNominatable(p.Remote) {
s.log.Tracef("Nominatable pair found, nominating (%s, %s)", p.Local, p.Remote)
p.nominated = true
s.nominatedPair = p
s.nominatePair(p)
return
}
s.agent.pingAllCandidates()
}
}
func (s *controllingSelector) nominatePair(pair *CandidatePair) {
// The controlling agent MUST include the USE-CANDIDATE attribute in
// order to nominate a candidate pair (Section 8.1.1). The controlled
// agent MUST NOT include the USE-CANDIDATE attribute in a Binding
// request.
msg, err := stun.Build(stun.BindingRequest, stun.TransactionID,
stun.NewUsername(s.agent.remoteUfrag+":"+s.agent.localUfrag),
UseCandidate(),
AttrControlling(s.agent.tieBreaker),
PriorityAttr(pair.Local.Priority()),
stun.NewShortTermIntegrity(s.agent.remotePwd),
stun.Fingerprint,
)
if err != nil {
s.log.Error(err.Error())
return
}
s.log.Tracef("Ping STUN (nominate candidate pair) from %s to %s", pair.Local, pair.Remote)
s.agent.sendBindingRequest(msg, pair.Local, pair.Remote)
}
func (s *controllingSelector) HandleBindingRequest(message *stun.Message, local, remote Candidate) { //nolint:cyclop
s.agent.sendBindingSuccess(message, local, remote)
pair := s.agent.findPair(local, remote)
if pair == nil {
pair = s.agent.addPair(local, remote)
pair.UpdateRequestReceived()
return
}
pair.UpdateRequestReceived()
if pair.state == CandidatePairStateSucceeded && s.nominatedPair == nil && s.agent.getSelectedPair() == nil {
bestPair := s.agent.getBestAvailableCandidatePair()
if bestPair == nil {
s.log.Tracef("No best pair available")
} else if bestPair.equal(pair) && s.isNominatable(pair.Local) && s.isNominatable(pair.Remote) {
s.log.Tracef(
"The candidate (%s, %s) is the best candidate available, marking it as nominated",
pair.Local,
pair.Remote,
)
s.nominatedPair = pair
s.nominatePair(pair)
}
}
if s.agent.userBindingRequestHandler != nil {
if shouldSwitch := s.agent.userBindingRequestHandler(message, local, remote, pair); shouldSwitch {
s.agent.setSelectedPair(pair)
}
}
}
func (s *controllingSelector) HandleSuccessResponse(m *stun.Message, local, remote Candidate, remoteAddr net.Addr) {
ok, pendingRequest, rtt := s.agent.handleInboundBindingSuccess(m.TransactionID)
if !ok {
s.log.Warnf("Discard success response from (%s), unknown TransactionID 0x%x", remote, m.TransactionID)
return
}
transactionAddr := pendingRequest.destination
// Assert that NAT is not symmetric
// https://tools.ietf.org/html/rfc8445#section-7.2.5.2.1
if !addrEqual(transactionAddr, remoteAddr) {
s.log.Debugf(
"Discard message: transaction source and destination does not match expected(%s), actual(%s)",
transactionAddr,
remote,
)
return
}
s.log.Tracef("Inbound STUN (SuccessResponse) from %s to %s", remote, local)
pair := s.agent.findPair(local, remote)
if pair == nil {
// This shouldn't happen
s.log.Error("Success response from invalid candidate pair")
return
}
pair.state = CandidatePairStateSucceeded
s.log.Tracef("Found valid candidate pair: %s", pair)
// Handle nomination/renomination
if pendingRequest.isUseCandidate {
selectedPair := s.agent.getSelectedPair()
// If this is a renomination request (has nomination value), always update the selected pair
// If it's a standard nomination (no value), only set if no pair is selected yet
if pendingRequest.nominationValue != nil {
s.log.Infof("Renomination success response received for pair %s (nomination value: %d), switching to this pair",
pair, *pendingRequest.nominationValue)
s.agent.setSelectedPair(pair)
} else if selectedPair == nil {
s.agent.setSelectedPair(pair)
}
}
pair.UpdateRoundTripTime(rtt)
}
func (s *controllingSelector) PingCandidate(local, remote Candidate) {
msg, err := stun.Build(stun.BindingRequest, stun.TransactionID,
stun.NewUsername(s.agent.remoteUfrag+":"+s.agent.localUfrag),
AttrControlling(s.agent.tieBreaker),
PriorityAttr(local.Priority()),
stun.NewShortTermIntegrity(s.agent.remotePwd),
stun.Fingerprint,
)
if err != nil {
s.log.Error(err.Error())
return
}
s.agent.sendBindingRequest(msg, local, remote)
}
// checkForAutomaticRenomination evaluates if automatic renomination should occur.
// This is called periodically when the agent is in connected state and automatic
// renomination is enabled.
func (s *controllingSelector) checkForAutomaticRenomination() {
if !s.agent.automaticRenomination || !s.agent.enableRenomination {
s.log.Tracef("Automatic renomination check skipped: automaticRenomination=%v, enableRenomination=%v",
s.agent.automaticRenomination, s.agent.enableRenomination)
return
}
timeSinceStart := time.Since(s.startTime)
if timeSinceStart < s.agent.renominationInterval {
s.log.Tracef("Automatic renomination check skipped: not enough time since start (%v < %v)",
timeSinceStart, s.agent.renominationInterval)
return
}
if !s.agent.lastRenominationTime.IsZero() {
timeSinceLastRenomination := time.Since(s.agent.lastRenominationTime)
if timeSinceLastRenomination < s.agent.renominationInterval {
s.log.Tracef("Automatic renomination check skipped: too soon since last renomination (%v < %v)",
timeSinceLastRenomination, s.agent.renominationInterval)
return
}
}
currentPair := s.agent.getSelectedPair()
if currentPair == nil {
s.log.Tracef("Automatic renomination check skipped: no current selected pair")
return
}
bestPair := s.agent.findBestCandidatePair()
if bestPair == nil {
s.log.Tracef("Automatic renomination check skipped: no best pair found")
return
}
s.log.Debugf("Evaluating automatic renomination: current=%s (RTT=%.2fms), best=%s (RTT=%.2fms)",
currentPair, currentPair.CurrentRoundTripTime()*1000,
bestPair, bestPair.CurrentRoundTripTime()*1000)
if s.agent.shouldRenominate(currentPair, bestPair) {
s.log.Infof("Automatic renomination triggered: switching from %s to %s",
currentPair, bestPair)
// Update last renomination time to prevent rapid renominations
s.agent.lastRenominationTime = time.Now()
if err := s.agent.RenominateCandidate(bestPair.Local, bestPair.Remote); err != nil {
s.log.Errorf("Failed to trigger automatic renomination: %v", err)
}
} else {
s.log.Debugf("Automatic renomination not warranted")
}
}
type controlledSelector struct {
agent *Agent
log logging.LeveledLogger
lastNomination *uint32 // For renomination: tracks highest nomination value seen
}
func (s *controlledSelector) Start() {
s.lastNomination = nil
}
// shouldAcceptNomination checks if a nomination should be accepted based on renomination rules.
func (s *controlledSelector) shouldAcceptNomination(nominationValue *uint32) bool {
// If no nomination value, accept normally (standard ICE nomination)
if nominationValue == nil {
return true
}
// If nomination value is present, controlling side is using renomination
// Apply "last nomination wins" rule
if s.lastNomination == nil || *nominationValue > *s.lastNomination {
s.lastNomination = nominationValue
s.log.Tracef("Accepting nomination with value %d", *nominationValue)
return true
}
s.log.Tracef("Rejecting nomination value %d (current is %d)", *nominationValue, *s.lastNomination)
return false
}
// shouldSwitchSelectedPair determines if we should switch to a new nominated pair.
// Returns true if the switch should occur, false otherwise.
func (s *controlledSelector) shouldSwitchSelectedPair(pair, selectedPair *CandidatePair, nominationValue *uint32) bool {
switch {
case selectedPair == nil:
// No current selection, accept the nomination
return true
case selectedPair == pair:
// Same pair, no change needed
return false
case nominationValue != nil:
// Renomination is in use (nomination value present)
// Accept the switch based on nomination value alone, not priority
// The shouldAcceptNomination check already validated this is a valid renomination
s.log.Debugf("Accepting renomination to pair %s (nomination value: %d)", pair, *nominationValue)
return true
}
// Standard ICE nomination without renomination - apply priority rules
// Only switch if we don't check priority, OR new pair has strictly higher priority
return !s.agent.needsToCheckPriorityOnNominated() ||
selectedPair.priority() < pair.priority()
}
func (s *controlledSelector) ContactCandidates() {
if s.agent.getSelectedPair() != nil {
if s.agent.validateSelectedPair() {
s.log.Trace("Checking keepalive")
s.agent.checkKeepalive()
}
} else {
s.agent.pingAllCandidates()
}
}
func (s *controlledSelector) PingCandidate(local, remote Candidate) {
msg, err := stun.Build(stun.BindingRequest, stun.TransactionID,
stun.NewUsername(s.agent.remoteUfrag+":"+s.agent.localUfrag),
AttrControlled(s.agent.tieBreaker),
PriorityAttr(local.Priority()),
stun.NewShortTermIntegrity(s.agent.remotePwd),
stun.Fingerprint,
)
if err != nil {
s.log.Error(err.Error())
return
}
s.agent.sendBindingRequest(msg, local, remote)
}
func (s *controlledSelector) HandleSuccessResponse(m *stun.Message, local, remote Candidate, remoteAddr net.Addr) {
//nolint:godox
// TODO according to the standard we should specifically answer a failed nomination:
// https://tools.ietf.org/html/rfc8445#section-7.3.1.5
// If the controlled agent does not accept the request from the
// controlling agent, the controlled agent MUST reject the nomination
// request with an appropriate error code response (e.g., 400)
// [RFC5389].
ok, pendingRequest, rtt := s.agent.handleInboundBindingSuccess(m.TransactionID)
if !ok {
s.log.Warnf("Discard message from (%s), unknown TransactionID 0x%x", remote, m.TransactionID)
return
}
transactionAddr := pendingRequest.destination
// Assert that NAT is not symmetric
// https://tools.ietf.org/html/rfc8445#section-7.2.5.2.1
if !addrEqual(transactionAddr, remoteAddr) {
s.log.Debugf(
"Discard message: transaction source and destination does not match expected(%s), actual(%s)",
transactionAddr,
remote,
)
return
}
s.log.Tracef("Inbound STUN (SuccessResponse) from %s to %s", remote, local)
pair := s.agent.findPair(local, remote)
if pair == nil {
// This shouldn't happen
s.log.Error("Success response from invalid candidate pair")
return
}
pair.state = CandidatePairStateSucceeded
s.log.Tracef("Found valid candidate pair: %s", pair)
if pair.nominateOnBindingSuccess {
if selectedPair := s.agent.getSelectedPair(); selectedPair == nil ||
(selectedPair != pair &&
(!s.agent.needsToCheckPriorityOnNominated() || selectedPair.priority() <= pair.priority())) {
s.agent.setSelectedPair(pair)
} else if selectedPair != pair {
s.log.Tracef("Ignore nominate new pair %s, already nominated pair %s", pair, selectedPair)
}
}
pair.UpdateRoundTripTime(rtt)
}
func (s *controlledSelector) HandleBindingRequest(message *stun.Message, local, remote Candidate) { //nolint:cyclop
pair := s.agent.findPair(local, remote)
if pair == nil {
pair = s.agent.addPair(local, remote)
}
pair.UpdateRequestReceived()
if message.Contains(stun.AttrUseCandidate) || message.Contains(s.agent.nominationAttribute) { //nolint:nestif
// https://tools.ietf.org/html/rfc8445#section-7.3.1.5
// Check for renomination attribute
var nominationValue *uint32
var nomination NominationAttribute
if err := nomination.GetFromWithType(message, s.agent.nominationAttribute); err == nil {
nominationValue = &nomination.Value
s.log.Tracef("Received nomination with value %d", nomination.Value)
}
// Check if we should accept this nomination based on renomination rules
if !s.shouldAcceptNomination(nominationValue) {
s.log.Tracef("Rejecting nomination request due to renomination rules")
s.agent.sendBindingSuccess(message, local, remote)
return
}
if pair.state == CandidatePairStateSucceeded {
// If the state of this pair is Succeeded, it means that the check
// previously sent by this pair produced a successful response and
// generated a valid pair (Section 7.2.5.3.2). The agent sets the
// nominated flag value of the valid pair to true.
selectedPair := s.agent.getSelectedPair()
if s.shouldSwitchSelectedPair(pair, selectedPair, nominationValue) {
s.log.Tracef("Accepting nomination for pair %s", pair)
s.agent.setSelectedPair(pair)
} else {
s.log.Tracef("Ignore nominate new pair %s, already nominated pair %s", pair, selectedPair)
}
} else {
// If the received Binding request triggered a new check to be
// enqueued in the triggered-check queue (Section 7.3.1.4), once the
// check is sent and if it generates a successful response, and
// generates a valid pair, the agent sets the nominated flag of the
// pair to true. If the request fails (Section 7.2.5.2), the agent
// MUST remove the candidate pair from the valid list, set the
// candidate pair state to Failed, and set the checklist state to
// Failed.
pair.nominateOnBindingSuccess = true
}
}
s.agent.sendBindingSuccess(message, local, remote)
s.PingCandidate(local, remote)
if s.agent.userBindingRequestHandler != nil {
if shouldSwitch := s.agent.userBindingRequestHandler(message, local, remote, pair); shouldSwitch {
s.agent.setSelectedPair(pair)
}
}
}
type liteSelector struct {
pairCandidateSelector
}
// A lite selector should not contact candidates.
func (s *liteSelector) ContactCandidates() {
if _, ok := s.pairCandidateSelector.(*controllingSelector); ok {
//nolint:godox
// https://github.com/pion/ice/issues/96
// TODO: implement lite controlling agent. For now falling back to full agent.
// This only happens if both peers are lite. See RFC 8445 S6.1.1 and S6.2
s.pairCandidateSelector.ContactCandidates()
} else if v, ok := s.pairCandidateSelector.(*controlledSelector); ok {
v.agent.validateSelectedPair()
}
}