Skip to content

Commit

Permalink
Merge pull request #170 from multiformats/remove-is-ipv6-link-local
Browse files Browse the repository at this point in the history
remove wrong (and redundant) IsIpv6LinkLocal
  • Loading branch information
marten-seemann authored Dec 22, 2021
2 parents 114704e + dff7fed commit 719ddab
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 32 deletions.
12 changes: 0 additions & 12 deletions net/ip.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
19 changes: 18 additions & 1 deletion net/net_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand All @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion net/resolve.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
18 changes: 0 additions & 18 deletions net/resolve_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

0 comments on commit 719ddab

Please sign in to comment.