Skip to content

Commit

Permalink
IPv6 handling updates
Browse files Browse the repository at this point in the history
- Try connecting via IPv4 addresses first
- Ignore IPv6 link local addresses
  • Loading branch information
DerAndereAndi committed Sep 4, 2024
1 parent 262116b commit 04d944c
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 1 deletion.
13 changes: 13 additions & 0 deletions hub/hub_connections.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import (
"errors"
"fmt"
"math/rand"
"net"
"net/http"
"slices"
"strconv"
"time"

Expand Down Expand Up @@ -353,6 +355,17 @@ func (h *Hub) initateConnection(remoteService *api.ServiceDetails, entry *api.Md
}
}

// try IPv4 addresses before IPv6 addresses
slices.SortFunc(entry.Addresses, func(a, b net.IP) int {
if a.To4() != nil && b.To4() == nil {
return -1
}
if a.To4() == nil && b.To4() != nil {
return 1
}
return 0
})

// try connecting via the provided IP addresses
for _, address := range entry.Addresses {
logging.Log().Debug("trying to connect to", remoteService.SKI(), "at", address)
Expand Down
10 changes: 10 additions & 0 deletions mdns/mdns.go
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,16 @@ func (m *MdnsManager) processMdnsEntry(elements map[string]string, name, host st
return
}

// remove IPv6 local link addresses
var newAddresses []net.IP
for _, address := range addresses {
if address.To4() == nil && address.IsLinkLocalUnicast() {
continue
}
newAddresses = append(newAddresses, address)
}
addresses = newAddresses

var deviceType, model, brand string

if _, ok := elements["brand"]; ok {
Expand Down
2 changes: 1 addition & 1 deletion mdns/mdns_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ func (s *MdnsSuite) Test_ProcessMdnsEntry() {
s.sut.processMdnsEntry(elements, name, host, ips, port, false)
assert.Equal(s.T(), 1, len(s.sut.mdnsEntries()))

ips = []net.IP{[]byte("127.0.0.1")}
ips = []net.IP{[]byte("127.0.0.1"), []byte{0xfe, 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}}
s.sut.processMdnsEntry(elements, name, host, ips, port, false)
assert.Equal(s.T(), 1, len(s.sut.mdnsEntries()))

Expand Down

0 comments on commit 04d944c

Please sign in to comment.