Skip to content

Conversation

pappz
Copy link
Contributor

@pappz pappz commented Sep 1, 2025

Describe your changes

Deduplicate STUN package sending.
Originally, because every peer shared the same UDP address, the library could not distinguish which STUN message was associated with which candidate. As a result, the Pion library responded from all candidates for every STUN message.

  • During candidate exchange, send the Candidate ID in the extension part.
  • In STUN messages, include both the source and destination Candidate IDs (this is required for peer-reflexive candidates).
  • Move the extra server reflexive candidate handling from the sender to the receiver side.

Issue ticket number and link

Stack

Checklist

  • Is it a bug fix
  • Is a typo/documentation fix
  • Is a feature enhancement
  • It is a refactor
  • Created tests that fail without the change (if possible)

By submitting this pull request, you confirm that you have read and agree to the terms of the Contributor License Agreement.

Documentation

Select exactly one:

  • I added/updated documentation for this change
  • Documentation is not needed for this change (explain why)

Docs PR URL (required if "docs added" is checked)

Paste the PR link from https://github.com/netbirdio/docs here:

https://github.com/netbirdio/docs/pull/__

@Copilot Copilot AI review requested due to automatic review settings September 1, 2025 20:01
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

Updates STUN routing logic to deduplicate package sending and improve candidate handling by using candidate IDs for message routing. The PR also moves UDP multiplexer code from the bind package to a dedicated udpmux package.

  • Implements candidate ID-based STUN message routing to avoid duplication
  • Refactors UDP multiplexer code into a dedicated udpmux package
  • Updates ICE library dependency to support enhanced STUN routing

Reviewed Changes

Copilot reviewed 22 out of 23 changed files in this pull request and generated 3 comments.

File Description
go.mod Updates ICE library dependency version
client/internal/peer/worker_ice.go Removes duplicate STUN candidate logic and adds candidate ID routing
client/iface/udpmux/*.go New package containing refactored UDP multiplexer implementation
Multiple device files Updates imports from bind to udpmux package

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

Comment on lines +454 to +456
// overwrite the original candidate ID with the new one to avoid candidate duplication
if e.Key == ice.ExtensionKeyCandidateID {
e.Value = candidate.ID()
Copy link
Preview

Copilot AI Sep 1, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code is modifying the original candidate's extension value instead of creating a new extension for the cloned candidate. This could affect the original candidate's ID. Consider creating a new extension with the new candidate ID rather than modifying the existing one.

Suggested change
// overwrite the original candidate ID with the new one to avoid candidate duplication
if e.Key == ice.ExtensionKeyCandidateID {
e.Value = candidate.ID()
newExt := ice.Extension{
Key: ice.ExtensionKeyCandidateID,
Value: candidate.ID(),
}
if err := ec.AddExtension(newExt); err != nil {
return nil, err
}
continue

Copilot uses AI. Check for mistakes.

Copy link

sonarqubecloud bot commented Sep 2, 2025

go func() {
<-c.CloseChannel()
m.RemoveConnByUfrag(ufrag)
}()

m.candidateConnMap[candidateID] = c
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would having a dedicated mutex to m.candidateConnMapmake any difference in a good way?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should protect this variable in the same way we protect connsIPv4 and connsIPv6. If I add a new lock, I risk causing a deadlock. This code is simpler.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants