Skip to content

Commit

Permalink
feat: support ipv6 link-local address
Browse files Browse the repository at this point in the history
  • Loading branch information
jclab-joseph committed Dec 6, 2023
1 parent 97b4ca0 commit 3a8848f
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 9 deletions.
8 changes: 7 additions & 1 deletion p2p/discovery/mdns/mdns.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,16 @@ func (s *mdnsService) Close() error {
func (s *mdnsService) getIPs(addrs []ma.Multiaddr) ([]string, error) {
var ip4, ip6 string
for _, addr := range addrs {
first, _ := ma.SplitFirst(addr)
first, remaining := ma.SplitFirst(addr)
if first == nil {
continue
}
if first.Protocol().Code == ma.P_IP6ZONE {
first, _ = ma.SplitFirst(remaining)
if first == nil {
continue
}
}
if ip4 == "" && first.Protocol().Code == ma.P_IP4 {
ip4 = first.Value()
} else if ip6 == "" && first.Protocol().Code == ma.P_IP6 {
Expand Down
13 changes: 9 additions & 4 deletions p2p/host/basic/basic_host.go
Original file line number Diff line number Diff line change
Expand Up @@ -782,22 +782,27 @@ func (h *BasicHost) Addrs() []ma.Multiaddr {

// Copy addrs slice since we'll be modifying it.
addrsOld := addrs
addrs = make([]ma.Multiaddr, len(addrsOld))
copy(addrs, addrsOld)
addrs = make([]ma.Multiaddr, 0, len(addrsOld))

for i, addr := range addrs {
for _, addr := range addrsOld {
if ok, n := libp2pwebtransport.IsWebtransportMultiaddr(addr); ok && n == 0 {
t := s.TransportForListening(addr)
tpt, ok := t.(addCertHasher)
if !ok {
addrs = append(addrs, addr)
continue
}
addrWithCerthash, added := tpt.AddCertHashes(addr)
if !added {
addrs = append(addrs, addr)
log.Debug("Couldn't add certhashes to webtransport multiaddr because we aren't listening on webtransport")
continue
}
addrs[i] = addrWithCerthash
addrs = append(addrs, addrWithCerthash)
} else if manet.IsIP6LinkLocal(addr) {
continue
} else {
addrs = append(addrs, addr)
}
}
return addrs
Expand Down
2 changes: 1 addition & 1 deletion p2p/net/reuseport/dialer.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func (d *dialer) DialContext(ctx context.Context, network, addr string) (net.Con
return nil, err
}
ip := tcpAddr.IP
if !ip.IsLoopback() && !ip.IsGlobalUnicast() {
if !ip.IsLoopback() && !ip.IsGlobalUnicast() && !ip.IsLinkLocalUnicast() {
return nil, fmt.Errorf("undialable IP: %s", ip)
}

Expand Down
2 changes: 0 additions & 2 deletions p2p/net/swarm/swarm_dial.go
Original file line number Diff line number Diff line change
Expand Up @@ -489,8 +489,6 @@ func (s *Swarm) filterKnownUndialables(p peer.ID, addrs []ma.Multiaddr) (goodAdd
}
return true
},
// TODO: Consider allowing link-local addresses
func(addr ma.Multiaddr) bool { return !manet.IsIP6LinkLocal(addr) },
func(addr ma.Multiaddr) bool {
if s.gater != nil && !s.gater.InterceptAddrDial(p, addr) {
addrErrs = append(addrErrs, TransportError{Address: addr, Cause: ErrGaterDisallowedConnection})
Expand Down
5 changes: 4 additions & 1 deletion p2p/transport/tcp/tcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,10 @@ func NewTCPTransport(upgrader transport.Upgrader, rcmgr network.ResourceManager,
return tr, nil
}

var dialMatcher = mafmt.And(mafmt.IP, mafmt.Base(ma.P_TCP))
var dialMatcher = mafmt.Or(
mafmt.And(mafmt.IP, mafmt.Base(ma.P_TCP)),
mafmt.And(mafmt.Base(ma.P_IP6ZONE), mafmt.IP, mafmt.Base(ma.P_TCP)),
)

// CanDial returns true if this transport believes it can dial the given
// multiaddr.
Expand Down

0 comments on commit 3a8848f

Please sign in to comment.