Skip to content

Commit

Permalink
Remove IPv6 ZoneID from ICE candidates
Browse files Browse the repository at this point in the history
Link-local IPv6 addresses may have ZoneID attached at the end. It has
local meaning only and should not be send to other parties. This change
removes ZoneID from generated candidate string, and ignores ZoneID
when received candidate is parsed.
  • Loading branch information
daniel@poradnik-webmastera.com authored and daniel@poradnik-webmastera.com committed Jun 30, 2024
1 parent 2a9fdb5 commit 6b4bf29
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 3 deletions.
11 changes: 9 additions & 2 deletions candidate_base.go
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,13 @@ func (c *candidateBase) copy() (Candidate, error) {
return UnmarshalCandidate(c.Marshal())
}

func removeZoneIdFromAddress(addr string) string {

Check warning on line 463 in candidate_base.go

View workflow job for this annotation

GitHub Actions / lint / Go

var-naming: func removeZoneIdFromAddress should be removeZoneIDFromAddress (revive)
if i := strings.Index(addr, "%"); i != -1 {
return addr[:i]
}
return addr
}

// Marshal returns the string representation of the ICECandidate
func (c *candidateBase) Marshal() string {
val := c.Foundation()
Expand All @@ -472,7 +479,7 @@ func (c *candidateBase) Marshal() string {
c.Component(),
c.NetworkType().NetworkShort(),
c.Priority(),
c.Address(),
removeZoneIdFromAddress(c.Address()),
c.Port(),
c.Type())

Expand Down Expand Up @@ -522,7 +529,7 @@ func UnmarshalCandidate(raw string) (Candidate, error) {
priority := uint32(priorityRaw)

// Address
address := split[4]
address := removeZoneIdFromAddress(split[4])

// Port
rawPort, err := strconv.ParseUint(split[5], 10, 16)
Expand Down
32 changes: 31 additions & 1 deletion candidate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,7 @@ func TestCandidateMarshal(t *testing.T) {

require.NoError(t, err)

require.True(t, test.candidate.Equal(actualCandidate))
require.Truef(t, test.candidate.Equal(actualCandidate), "%s != %s", test.candidate.String(), actualCandidate.String())
require.Equal(t, test.marshaled, actualCandidate.Marshal())
})
}
Expand Down Expand Up @@ -437,3 +437,33 @@ func TestCandidateWriteTo(t *testing.T) {
_, err = c1.writeTo([]byte("test"), c2)
require.Error(t, err, "writing to closed conn")
}

func TestMarshalUnmarshalCandidateWithZoneID(t *testing.T) {
candidateWithZoneID := mustCandidateHost(&CandidateHostConfig{
Network: NetworkTypeUDP6.String(),
Address: "fcd9:e3b8:12ce:9fc5:74a5:c6bb:d8b:e08a%Local Connection",
Port: 53987,
Priority: 500,
Foundation: "750",
})
candidateStr := "750 0 udp 500 fcd9:e3b8:12ce:9fc5:74a5:c6bb:d8b:e08a 53987 typ host"
require.Equal(t, candidateStr, candidateWithZoneID.Marshal())

candidate := mustCandidateHost(&CandidateHostConfig{
Network: NetworkTypeUDP6.String(),
Address: "fcd9:e3b8:12ce:9fc5:74a5:c6bb:d8b:e08a",
Port: 53987,
Priority: 500,
Foundation: "750",
})
candidateWithZoneIDStr := "750 0 udp 500 fcd9:e3b8:12ce:9fc5:74a5:c6bb:d8b:e08a%eth0 53987 typ host"
candidate2, err := UnmarshalCandidate(candidateWithZoneIDStr)
require.NoError(t, err)
require.Truef(t, candidate.Equal(candidate2), "%s != %s", candidate.String(), candidate2.String())

candidateWithZoneIDStr2 := "750 0 udp 500 fcd9:e3b8:12ce:9fc5:74a5:c6bb:d8b:e08a%eth0%eth1 53987 typ host"
candidate2, err = UnmarshalCandidate(candidateWithZoneIDStr2)
require.NoError(t, err)
require.NoError(t, err)
require.Truef(t, candidate.Equal(candidate2), "%s != %s", candidate.String(), candidate2.String())
}

0 comments on commit 6b4bf29

Please sign in to comment.