Skip to content

Commit

Permalink
Remove IPv6 ZoneID from ICE candidates (#704)
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
sirzooro committed Jul 6, 2024
1 parent 822108b commit 4d781c8
Show file tree
Hide file tree
Showing 2 changed files with 47 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 @@ -447,6 +447,13 @@ func (c *candidateBase) copy() (Candidate, error) {
return UnmarshalCandidate(c.Marshal())
}

func removeZoneIDFromAddress(addr string) string {
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 @@ -459,7 +466,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 @@ -509,7 +516,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
39 changes: 38 additions & 1 deletion candidate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,7 @@ func TestCandidateMarshal(t *testing.T) {

assert.NoError(t, err)

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

func TestMarshalUnmarshalCandidateWithZoneID(t *testing.T) {
candidateWithZoneID := &CandidateHost{
candidateBase{
networkType: NetworkTypeUDP6,
candidateType: CandidateTypeHost,
address: "fcd9:e3b8:12ce:9fc5:74a5:c6bb:d8b:e08a%Local Connection",
port: 53987,
priorityOverride: 500,
foundationOverride: "750",
},
"",
}
candidateStr := "750 0 udp 500 fcd9:e3b8:12ce:9fc5:74a5:c6bb:d8b:e08a 53987 typ host"
require.Equal(t, candidateStr, candidateWithZoneID.Marshal())

candidate := &CandidateHost{
candidateBase{
networkType: NetworkTypeUDP6,
candidateType: CandidateTypeHost,
address: "fcd9:e3b8:12ce:9fc5:74a5:c6bb:d8b:e08a",
port: 53987,
priorityOverride: 500,
foundationOverride: "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.Truef(t, candidate.Equal(candidate2), "%s != %s", candidate.String(), candidate2.String())
}

0 comments on commit 4d781c8

Please sign in to comment.