| 424 | } |
| 425 | |
| 426 | func (s *controlledSelector) HandleBindingRequest(message *stun.Message, local, remote Candidate) { //nolint:cyclop |
| 427 | pair := s.agent.findPair(local, remote) |
| 428 | if pair == nil { |
| 429 | pair = s.agent.addPair(local, remote) |
| 430 | } |
| 431 | pair.UpdateRequestReceived() |
| 432 | |
| 433 | if message.Contains(stun.AttrUseCandidate) || message.Contains(s.agent.nominationAttribute) { //nolint:nestif |
| 434 | // https://tools.ietf.org/html/rfc8445#section-7.3.1.5 |
| 435 | |
| 436 | // Check for renomination attribute |
| 437 | var nominationValue *uint32 |
| 438 | var nomination NominationAttribute |
| 439 | if err := nomination.GetFromWithType(message, s.agent.nominationAttribute); err == nil { |
| 440 | nominationValue = &nomination.Value |
| 441 | s.log.Tracef("Received nomination with value %d", nomination.Value) |
| 442 | } |
| 443 | |
| 444 | // Check if we should accept this nomination based on renomination rules |
| 445 | if !s.shouldAcceptNomination(nominationValue) { |
| 446 | s.log.Tracef("Rejecting nomination request due to renomination rules") |
| 447 | s.agent.sendBindingSuccess(message, local, remote) |
| 448 | |
| 449 | return |
| 450 | } |
| 451 | |
| 452 | if pair.state == CandidatePairStateSucceeded { |
| 453 | // If the state of this pair is Succeeded, it means that the check |
| 454 | // previously sent by this pair produced a successful response and |
| 455 | // generated a valid pair (Section 7.2.5.3.2). The agent sets the |
| 456 | // nominated flag value of the valid pair to true. |
| 457 | selectedPair := s.agent.getSelectedPair() |
| 458 | if s.shouldSwitchSelectedPair(pair, selectedPair, nominationValue) { |
| 459 | s.log.Tracef("Accepting nomination for pair %s", pair) |
| 460 | s.agent.setSelectedPair(pair) |
| 461 | } else { |
| 462 | s.log.Tracef("Ignore nominate new pair %s, already nominated pair %s", pair, selectedPair) |
| 463 | } |
| 464 | } else { |
| 465 | // If the received Binding request triggered a new check to be |
| 466 | // enqueued in the triggered-check queue (Section 7.3.1.4), once the |
| 467 | // check is sent and if it generates a successful response, and |
| 468 | // generates a valid pair, the agent sets the nominated flag of the |
| 469 | // pair to true. If the request fails (Section 7.2.5.2), the agent |
| 470 | // MUST remove the candidate pair from the valid list, set the |
| 471 | // candidate pair state to Failed, and set the checklist state to |
| 472 | // Failed. |
| 473 | pair.nominateOnBindingSuccess = true |
| 474 | } |
| 475 | } |
| 476 | |
| 477 | s.agent.sendBindingSuccess(message, local, remote) |
| 478 | |
| 479 | // Only send a triggered check during ICE checking phase (RFC 8445 §7.3.1.4). |
| 480 | // Once the pair is established (succeeded + selected), sending a triggered check |
| 481 | // on every inbound request creates a ping-pong busy loop: the remote side responds |
| 482 | // and sends its own request, which triggers another check here, repeating at 1/RTT. |
| 483 | // After connection, consent freshness is maintained by checkKeepalive() on a timer. |