From dff7fed63c6344ecec0f8b7bbffa84b9db628bbe Mon Sep 17 00:00:00 2001 From: Marten Seemann Date: Tue, 21 Dec 2021 12:29:45 +0400 Subject: [PATCH] remove wrong (and redundant) IsIpv6LinkLocal --- net/ip.go | 12 ------------ net/net_test.go | 19 ++++++++++++++++++- net/resolve.go | 2 +- net/resolve_test.go | 18 ------------------ 4 files changed, 19 insertions(+), 32 deletions(-) diff --git a/net/ip.go b/net/ip.go index fab5ee5..1cf9a77 100644 --- a/net/ip.go +++ b/net/ip.go @@ -95,18 +95,6 @@ func IsIPUnspecified(m ma.Multiaddr) bool { return net.IP(c.RawValue()).IsUnspecified() } -// IsIpv6LinkLocal returns whether the addr uses a non-local ip link -func IsIpv6LinkLocal(a ma.Multiaddr) bool { - split := ma.Split(a) - if len(split) < 1 { - return false - } - if IsIP6LinkLocal(split[0]) { - return false - } - return true -} - // If m matches [zone,ip6,...], return [ip6,...] // else if m matches [], [zone], or [zone,...], return nil // else return m diff --git a/net/net_test.go b/net/net_test.go index 6483a9b..a2c13e2 100644 --- a/net/net_test.go +++ b/net/net_test.go @@ -12,6 +12,7 @@ import ( "time" ma "github.com/multiformats/go-multiaddr" + "github.com/stretchr/testify/require" ) func newMultiaddr(t *testing.T, m string) ma.Multiaddr { @@ -449,7 +450,7 @@ func TestIPUnspecified(t *testing.T) { func TestIP6LinkLocal(t *testing.T) { for a := 0; a < 65536; a++ { - isLinkLocal := (a&0xffc0 == 0xfe80 || a&0xff0f == 0xff02) + isLinkLocal := a&0xffc0 == 0xfe80 || a&0xff0f == 0xff02 m := newMultiaddr(t, fmt.Sprintf("/ip6/%x::1", a)) if IsIP6LinkLocal(m) != isLinkLocal { t.Errorf("IsIP6LinkLocal failed (%s != %v)", m, isLinkLocal) @@ -459,6 +460,22 @@ func TestIP6LinkLocal(t *testing.T) { if !IsIP6LinkLocal(newMultiaddr(t, "/ip6zone/hello/ip6/fe80::9999")) { t.Error("IsIP6LinkLocal failed (/ip6/fe80::9999)") } + + bad := []ma.Multiaddr{ + newMultiaddr(t, "/ip6/fe80::1/tcp/1234"), // link local + newMultiaddr(t, "/ip6/fe80::100/tcp/1234"), // link local + } + good := []ma.Multiaddr{ + newMultiaddr(t, "/ip4/127.0.0.1/tcp/1234"), + newMultiaddr(t, "/ip6/::1/tcp/1234"), + newMultiaddr(t, "/ip4/1.2.3.4/udp/1234/utp"), + } + for _, addr := range bad { + require.True(t, IsIP6LinkLocal(addr), "%s is a link local addr", addr) + } + for _, addr := range good { + require.False(t, IsIP6LinkLocal(addr), "%s is not a link local addr", addr) + } } func TestConvertNetAddr(t *testing.T) { diff --git a/net/resolve.go b/net/resolve.go index d9d0a4d..a2478e7 100644 --- a/net/resolve.go +++ b/net/resolve.go @@ -74,7 +74,7 @@ func interfaceAddresses() ([]ma.Multiaddr, error) { var out []ma.Multiaddr for _, a := range maddrs { - if !IsIpv6LinkLocal(a) { + if IsIP6LinkLocal(a) { continue } out = append(out, a) diff --git a/net/resolve_test.go b/net/resolve_test.go index f2472f8..387e70a 100644 --- a/net/resolve_test.go +++ b/net/resolve_test.go @@ -55,21 +55,3 @@ func TestResolvingAddrs(t *testing.T) { t.Fatal("should have failed") } } - -func TestAddrOverNonLocalIP(t *testing.T) { - bad := []ma.Multiaddr{ - newMultiaddr(t, "/ip6/fe80::1/tcp/1234"), // link local - newMultiaddr(t, "/ip6/fe80::100/tcp/1234"), // link local - } - good := []ma.Multiaddr{ - newMultiaddr(t, "/ip4/127.0.0.1/tcp/1234"), - newMultiaddr(t, "/ip6/::1/tcp/1234"), - newMultiaddr(t, "/ip4/1.2.3.4/udp/1234/utp"), - } - for _, addr := range bad { - require.Falsef(t, IsIpv6LinkLocal(addr), "%s is a link local addr", addr) - } - for _, addr := range good { - require.Truef(t, IsIpv6LinkLocal(addr), "%s is not a link local addr", addr) - } -}