Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remove wrong (and redundant) IsIpv6LinkLocal #170

Merged
merged 1 commit into from
Dec 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that IsIpv6Local was doing the opposite of what the name suggested.

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)
}
}