Skip to content

Commit

Permalink
feat: support connect with ipv6 link-local address
Browse files Browse the repository at this point in the history
  • Loading branch information
jclab-joseph committed Dec 7, 2023
1 parent 97b4ca0 commit f1a42fe
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 4 deletions.
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
15 changes: 13 additions & 2 deletions p2p/net/swarm/swarm_dial.go
Original file line number Diff line number Diff line change
Expand Up @@ -489,8 +489,19 @@ 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 manet.IsIP6LinkLocal(addr) {
var hasZone bool
ma.ForEach(addr, func(c ma.Component) bool {
if c.Protocol().Code == ma.P_IP6ZONE {
hasZone = true
}
return false
})
return hasZone
}
return true
},
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
64 changes: 64 additions & 0 deletions p2p/transport/tcp/tcp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package tcp
import (
"context"
"errors"
"net"
"testing"

"github.com/libp2p/go-libp2p/core/crypto"
Expand Down Expand Up @@ -46,6 +47,69 @@ func TestTcpTransport(t *testing.T) {
envReuseportVal = true
}

func TestTcpTransportCanDialToLinkLocalAddress(t *testing.T) {
addr := ma.StringCast("/ip6zone/eth0/ip6/fe80::fc54:ff:fe43:e553/tcp/1234")

var u transport.Upgrader
tpt, err := NewTCPTransport(u, nil)
require.NoError(t, err)

if !tpt.CanDial(addr) {
t.Fatal("should be able to dial ip6zone")
}
}

func TestTcpTransportWithLinkLocalAddress(t *testing.T) {
ifaces, err := net.Interfaces()
if err != nil {
t.Error(err)
return
}

var targetAddr ma.Multiaddr

findInterface:
for _, iface := range ifaces {
addrs, err := iface.Addrs()
if err == nil {
for _, a := range addrs {
addr, err := manet.FromNetAddr(a)
if err == nil {
if manet.IsIP6LinkLocal(addr) {
targetAddr = ma.StringCast("/ip6zone/" + iface.Name + addr.String())
break findInterface
}
}
}
}
}

if targetAddr == nil {
t.Fail()
return
}

for i := 0; i < 2; i++ {
peerA, ia := makeInsecureMuxer(t)
_, ib := makeInsecureMuxer(t)

ua, err := tptu.New(ia, muxers, nil, nil, nil)
require.NoError(t, err)
ta, err := NewTCPTransport(ua, nil)
require.NoError(t, err)
ub, err := tptu.New(ib, muxers, nil, nil, nil)
require.NoError(t, err)
tb, err := NewTCPTransport(ub, nil)
require.NoError(t, err)

zero := targetAddr.String() + "/tcp/0"
ttransport.SubtestTransport(t, ta, tb, zero, peerA)

envReuseportVal = false
}
envReuseportVal = true
}

func TestTcpTransportWithMetrics(t *testing.T) {
peerA, ia := makeInsecureMuxer(t)
_, ib := makeInsecureMuxer(t)
Expand Down

0 comments on commit f1a42fe

Please sign in to comment.