Skip to content

Commit

Permalink
Respect IncludeLoopback in UDPMuxDefault with unspecified IP
Browse files Browse the repository at this point in the history
  • Loading branch information
dinvlad committed Oct 10, 2023
1 parent e6ed8ff commit 1cb32ef
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 1 deletion.
5 changes: 5 additions & 0 deletions gather.go
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,11 @@ func (a *Agent) gatherCandidatesLocalUDPMux(ctx context.Context) error { //nolin
return errInvalidAddress
}
candidateIP := udpAddr.IP
if _, ok := a.udpMux.(*UDPMuxDefault); ok && !a.includeLoopback && candidateIP.IsLoopback() {
// Unlike MultiUDPMux Default, UDPMuxDefault doesn't have
// a separate param to include loopback, so we respect agent config
continue

Check warning on line 272 in gather.go

View check run for this annotation

Codecov / codecov/patch

gather.go#L269-L272

Added lines #L269 - L272 were not covered by tests
}
if a.extIPMapper != nil && a.extIPMapper.candidateType == CandidateTypeHost {
mappedIP, err := a.extIPMapper.findExternalIP(candidateIP.String())
if err != nil {
Expand Down
28 changes: 28 additions & 0 deletions gather_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,16 @@ func TestLoopbackCandidate(t *testing.T) {
assert.NoError(t, err)
muxWithLo, errlo := NewMultiUDPMuxFromPort(12501, UDPMuxFromPortWithLoopback())
assert.NoError(t, errlo)

unspecConn, errconn := net.ListenPacket("udp", ":0")
assert.NoError(t, errconn)
defer func() {
_ = unspecConn.Close()
}()
muxUnspecDefault := NewUDPMuxDefault(UDPMuxParams{
UDPConn: unspecConn,
})

testCases := []testCase{
{
name: "mux should not have loopback candidate",
Expand All @@ -150,6 +160,23 @@ func TestLoopbackCandidate(t *testing.T) {
},
loExpected: true,
},
{
name: "UDPMuxDefault with unspecified IP should not have loopback candidate",
agentConfig: &AgentConfig{
NetworkTypes: []NetworkType{NetworkTypeUDP4, NetworkTypeUDP6},
UDPMux: muxUnspecDefault,
},
loExpected: false,
},
{
name: "UDPMuxDefault with unspecified IP should respect agent includeloopback",
agentConfig: &AgentConfig{
NetworkTypes: []NetworkType{NetworkTypeUDP4, NetworkTypeUDP6},
UDPMux: muxUnspecDefault,
IncludeLoopback: true,
},
loExpected: true,
},
{
name: "includeloopback enabled",
agentConfig: &AgentConfig{
Expand Down Expand Up @@ -198,6 +225,7 @@ func TestLoopbackCandidate(t *testing.T) {

assert.NoError(t, mux.Close())
assert.NoError(t, muxWithLo.Close())
assert.NoError(t, muxUnspecDefault.Close())
}

// Assert that STUN gathering is done concurrently
Expand Down
2 changes: 1 addition & 1 deletion udp_mux.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func NewUDPMuxDefault(params UDPMuxParams) *UDPMuxDefault {
}
}

ips, err := localInterfaces(params.Net, nil, nil, networks, false)
ips, err := localInterfaces(params.Net, nil, nil, networks, true)
if err == nil {
for _, ip := range ips {
localAddrsForUnspecified = append(localAddrsForUnspecified, &net.UDPAddr{IP: ip, Port: addr.Port})
Expand Down

0 comments on commit 1cb32ef

Please sign in to comment.