Skip to content

Commit

Permalink
Include ufrag in generated ICE candidates
Browse files Browse the repository at this point in the history
Include ufrag extension in the ICE candidates generated by the ICE agent
  • Loading branch information
JoeTurki committed Jan 30, 2025
1 parent 60cb55e commit c333fb0
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
12 changes: 12 additions & 0 deletions agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -783,6 +783,8 @@ func (a *Agent) addRemoteCandidate(cand Candidate) { //nolint:cyclop
}

func (a *Agent) addCandidate(ctx context.Context, cand Candidate, candidateConn net.PacketConn) error {
a.setCandidateExtensions(cand)

return a.loop.Run(ctx, func(context.Context) {
set := a.localCandidates[cand.NetworkType()]
for _, candidate := range set {
Expand Down Expand Up @@ -818,6 +820,16 @@ func (a *Agent) addCandidate(ctx context.Context, cand Candidate, candidateConn
})
}

func (a *Agent) setCandidateExtensions(cand Candidate) {
err := cand.AddExtension(CandidateExtension{
Key: "ufrag",
Value: a.localUfrag,
})
if err != nil {
a.log.Errorf("Failed to add ufrag extension to candidate: %v", err)
}
}

// GetRemoteCandidates returns the remote candidates.
func (a *Agent) GetRemoteCandidates() ([]Candidate, error) {
var res []Candidate
Expand Down
39 changes: 39 additions & 0 deletions agent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2071,3 +2071,42 @@ func TestAgentGracefulCloseDeadlock(t *testing.T) {
closeNow.Done()
closed.Wait()
}

func TestSetCandidatesUfrag(t *testing.T) {
var config AgentConfig

agent, err := NewAgent(&config)
if err != nil {
t.Fatalf("Error constructing ice.Agent: %v", err)
}
defer func() {
require.NoError(t, agent.Close())
}()

dummyConn := &net.UDPConn{}

for i := 0; i < 5; i++ {
cfg := CandidateHostConfig{
Network: "udp",
Address: "192.168.0.2",
Port: 1000 + i,
Component: 1,
}

cand, errCand := NewCandidateHost(&cfg)
require.NoError(t, errCand)

err = agent.addCandidate(context.Background(), cand, dummyConn)
require.NoError(t, err)
}

actualCandidates, err := agent.GetLocalCandidates()
require.NoError(t, err)

for _, candidate := range actualCandidates {
ext, ok := candidate.GetExtension("ufrag")

require.True(t, ok)
require.Equal(t, agent.localUfrag, ext.Value)
}
}

0 comments on commit c333fb0

Please sign in to comment.