Skip to content

Commit 114704e

Browse files
Merge pull request #168 from multiformats/resolve
move ResolveUnspecifiedAddress(es) and FilterAddrs here from libp2p/go-addr-util
2 parents d783511 + 6a0950f commit 114704e

File tree

7 files changed

+222
-1
lines changed

7 files changed

+222
-1
lines changed

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@ require (
66
github.com/ipfs/go-cid v0.0.7
77
github.com/multiformats/go-multihash v0.0.14
88
github.com/multiformats/go-varint v0.0.6
9+
github.com/stretchr/testify v1.7.0
910
)

go.sum

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
2+
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
13
github.com/ipfs/go-cid v0.0.7 h1:ysQJVJA3fNDF1qigJbsSQOdjhVLsOEoPdh0+R97k3jY=
24
github.com/ipfs/go-cid v0.0.7/go.mod h1:6Ux9z5e+HpkQdckYoX1PG/6xqKspzlEIR5SDmgqgC/I=
35
github.com/minio/blake2b-simd v0.0.0-20160723061019-3f5f724cb5b1 h1:lYpkrQH5ajf0OXOcUbGjvZxxijuBwbbmlSxLiuofa+g=
@@ -19,8 +21,13 @@ github.com/multiformats/go-multihash v0.0.14/go.mod h1:VdAWLKTwram9oKAatUcLxBNUj
1921
github.com/multiformats/go-varint v0.0.5/go.mod h1:3Ls8CIEsrijN6+B7PbrXRPxHRPuXSrVKRY101jdMZYE=
2022
github.com/multiformats/go-varint v0.0.6 h1:gk85QWKxh3TazbLxED/NlDVv8+q+ReFJk7Y2W/KhfNY=
2123
github.com/multiformats/go-varint v0.0.6/go.mod h1:3Ls8CIEsrijN6+B7PbrXRPxHRPuXSrVKRY101jdMZYE=
24+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
25+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
2226
github.com/spaolacci/murmur3 v1.1.0 h1:7c1g84S4BPRrfL5Xrdp6fOJ206sU9y293DDHaoy0bLI=
2327
github.com/spaolacci/murmur3 v1.1.0/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA=
28+
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
29+
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
30+
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
2431
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
2532
golang.org/x/crypto v0.0.0-20190611184440-5c40567a22f8 h1:1wopBVtVdWnn03fZelqdXTqk7U7zPQCb+T4rbU9ZEoU=
2633
golang.org/x/crypto v0.0.0-20190611184440-5c40567a22f8/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
@@ -29,3 +36,7 @@ golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5h
2936
golang.org/x/sys v0.0.0-20190412213103-97732733099d h1:+R4KGOnez64A81RvjARKc4UT5/tI9ujCIVX+P5KiHuI=
3037
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
3138
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
39+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
40+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
41+
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
42+
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

multiaddr.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,3 +184,19 @@ func (m *multiaddr) ValueForProtocol(code int) (value string, err error) {
184184
})
185185
return
186186
}
187+
188+
// FilterAddrs is a filter that removes certain addresses, according to the given filters.
189+
// If all filters return true, the address is kept.
190+
func FilterAddrs(a []Multiaddr, filters ...func(Multiaddr) bool) []Multiaddr {
191+
b := make([]Multiaddr, 0, len(a))
192+
addrloop:
193+
for _, addr := range a {
194+
for _, filter := range filters {
195+
if !filter(addr) {
196+
continue addrloop
197+
}
198+
}
199+
b = append(b, addr)
200+
}
201+
return b
202+
}

multiaddr_test.go

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import (
88
"testing"
99
"time"
1010

11+
"github.com/stretchr/testify/require"
12+
1113
"github.com/ipfs/go-cid"
1214
mh "github.com/multiformats/go-multihash"
1315
)
@@ -226,7 +228,7 @@ func TestStringToBytes(t *testing.T) {
226228
t.Error("failed to decode hex", h)
227229
}
228230

229-
//t.Log("196", h, []byte(b1))
231+
// t.Log("196", h, []byte(b1))
230232

231233
b2, err := stringToBytes(s)
232234
if err != nil {
@@ -739,3 +741,24 @@ func TestComponentJSONMarshaler(t *testing.T) {
739741
t.Error("expected equal components in circular marshaling test")
740742
}
741743
}
744+
745+
func TestFilterAddrs(t *testing.T) {
746+
bad := []Multiaddr{
747+
newMultiaddr(t, "/ip6/fe80::1/tcp/1234"),
748+
newMultiaddr(t, "/ip6/fe80::100/tcp/1234"),
749+
}
750+
good := []Multiaddr{
751+
newMultiaddr(t, "/ip4/127.0.0.1/tcp/1234"),
752+
newMultiaddr(t, "/ip4/1.1.1.1/tcp/999"),
753+
newMultiaddr(t, "/ip4/1.2.3.4/udp/1234/utp"),
754+
}
755+
goodAndBad := append(good, bad...)
756+
757+
filter := func(addr Multiaddr) bool {
758+
return addr.Protocols()[0].Code == P_IP4
759+
}
760+
761+
require.Empty(t, FilterAddrs(bad, filter))
762+
require.ElementsMatch(t, FilterAddrs(good, filter), good)
763+
require.ElementsMatch(t, FilterAddrs(goodAndBad, filter), good)
764+
}

net/ip.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,18 @@ func IsIPUnspecified(m ma.Multiaddr) bool {
9595
return net.IP(c.RawValue()).IsUnspecified()
9696
}
9797

98+
// IsIpv6LinkLocal returns whether the addr uses a non-local ip link
99+
func IsIpv6LinkLocal(a ma.Multiaddr) bool {
100+
split := ma.Split(a)
101+
if len(split) < 1 {
102+
return false
103+
}
104+
if IsIP6LinkLocal(split[0]) {
105+
return false
106+
}
107+
return true
108+
}
109+
98110
// If m matches [zone,ip6,...], return [ip6,...]
99111
// else if m matches [], [zone], or [zone,...], return nil
100112
// else return m

net/resolve.go

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
package manet
2+
3+
import (
4+
"fmt"
5+
6+
ma "github.com/multiformats/go-multiaddr"
7+
)
8+
9+
// ResolveUnspecifiedAddress expands an unspecified ip addresses (/ip4/0.0.0.0, /ip6/::) to
10+
// use the known local interfaces. If ifaceAddr is nil, we request interface addresses
11+
// from the network stack. (this is so you can provide a cached value if resolving many addrs)
12+
func ResolveUnspecifiedAddress(resolve ma.Multiaddr, ifaceAddrs []ma.Multiaddr) ([]ma.Multiaddr, error) {
13+
// split address into its components
14+
split := ma.Split(resolve)
15+
16+
// if first component (ip) is not unspecified, use it as is.
17+
if !IsIPUnspecified(split[0]) {
18+
return []ma.Multiaddr{resolve}, nil
19+
}
20+
21+
out := make([]ma.Multiaddr, 0, len(ifaceAddrs))
22+
for _, ia := range ifaceAddrs {
23+
// must match the first protocol to be resolve.
24+
if ia.Protocols()[0].Code != resolve.Protocols()[0].Code {
25+
continue
26+
}
27+
28+
split[0] = ia
29+
joined := ma.Join(split...)
30+
out = append(out, joined)
31+
}
32+
if len(out) < 1 {
33+
return nil, fmt.Errorf("failed to resolve: %s", resolve)
34+
}
35+
return out, nil
36+
}
37+
38+
// ResolveUnspecifiedAddresses expands unspecified ip addresses (/ip4/0.0.0.0, /ip6/::) to
39+
// use the known local interfaces.
40+
func ResolveUnspecifiedAddresses(unspecAddrs, ifaceAddrs []ma.Multiaddr) ([]ma.Multiaddr, error) {
41+
// todo optimize: only fetch these if we have a "any" addr.
42+
if len(ifaceAddrs) < 1 {
43+
var err error
44+
ifaceAddrs, err = interfaceAddresses()
45+
if err != nil {
46+
return nil, err
47+
}
48+
}
49+
50+
var outputAddrs []ma.Multiaddr
51+
for _, a := range unspecAddrs {
52+
// unspecified?
53+
resolved, err := ResolveUnspecifiedAddress(a, ifaceAddrs)
54+
if err != nil {
55+
continue // optimistic. if we can't resolve anything, we'll know at the bottom.
56+
}
57+
outputAddrs = append(outputAddrs, resolved...)
58+
}
59+
60+
if len(outputAddrs) < 1 {
61+
return nil, fmt.Errorf("failed to specify addrs: %s", unspecAddrs)
62+
}
63+
return outputAddrs, nil
64+
}
65+
66+
// interfaceAddresses returns a list of addresses associated with local machine
67+
// Note: we do not return link local addresses. IP loopback is ok, because we
68+
// may be connecting to other nodes in the same machine.
69+
func interfaceAddresses() ([]ma.Multiaddr, error) {
70+
maddrs, err := InterfaceMultiaddrs()
71+
if err != nil {
72+
return nil, err
73+
}
74+
75+
var out []ma.Multiaddr
76+
for _, a := range maddrs {
77+
if !IsIpv6LinkLocal(a) {
78+
continue
79+
}
80+
out = append(out, a)
81+
}
82+
return out, nil
83+
}

net/resolve_test.go

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package manet
2+
3+
import (
4+
"testing"
5+
6+
ma "github.com/multiformats/go-multiaddr"
7+
"github.com/stretchr/testify/require"
8+
)
9+
10+
func TestResolvingAddrs(t *testing.T) {
11+
unspec := []ma.Multiaddr{
12+
newMultiaddr(t, "/ip4/0.0.0.0/tcp/1234"),
13+
newMultiaddr(t, "/ip4/1.2.3.4/tcp/1234"),
14+
newMultiaddr(t, "/ip6/::/tcp/1234"),
15+
newMultiaddr(t, "/ip6/::100/tcp/1234"),
16+
}
17+
18+
iface := []ma.Multiaddr{
19+
newMultiaddr(t, "/ip4/127.0.0.1"),
20+
newMultiaddr(t, "/ip4/10.20.30.40"),
21+
newMultiaddr(t, "/ip6/::1"),
22+
newMultiaddr(t, "/ip6/::f"),
23+
}
24+
25+
spec := []ma.Multiaddr{
26+
newMultiaddr(t, "/ip4/127.0.0.1/tcp/1234"),
27+
newMultiaddr(t, "/ip4/10.20.30.40/tcp/1234"),
28+
newMultiaddr(t, "/ip4/1.2.3.4/tcp/1234"),
29+
newMultiaddr(t, "/ip6/::1/tcp/1234"),
30+
newMultiaddr(t, "/ip6/::f/tcp/1234"),
31+
newMultiaddr(t, "/ip6/::100/tcp/1234"),
32+
}
33+
34+
actual, err := ResolveUnspecifiedAddresses(unspec, iface)
35+
require.NoError(t, err)
36+
require.Equal(t, actual, spec)
37+
38+
ip4u := []ma.Multiaddr{newMultiaddr(t, "/ip4/0.0.0.0")}
39+
ip4i := []ma.Multiaddr{newMultiaddr(t, "/ip4/1.2.3.4")}
40+
41+
ip6u := []ma.Multiaddr{newMultiaddr(t, "/ip6/::")}
42+
ip6i := []ma.Multiaddr{newMultiaddr(t, "/ip6/::1")}
43+
44+
if _, err := ResolveUnspecifiedAddress(ip4u[0], ip6i); err == nil {
45+
t.Fatal("should have failed")
46+
}
47+
if _, err := ResolveUnspecifiedAddress(ip6u[0], ip4i); err == nil {
48+
t.Fatal("should have failed")
49+
}
50+
51+
if _, err := ResolveUnspecifiedAddresses(ip6u, ip4i); err == nil {
52+
t.Fatal("should have failed")
53+
}
54+
if _, err := ResolveUnspecifiedAddresses(ip4u, ip6i); err == nil {
55+
t.Fatal("should have failed")
56+
}
57+
}
58+
59+
func TestAddrOverNonLocalIP(t *testing.T) {
60+
bad := []ma.Multiaddr{
61+
newMultiaddr(t, "/ip6/fe80::1/tcp/1234"), // link local
62+
newMultiaddr(t, "/ip6/fe80::100/tcp/1234"), // link local
63+
}
64+
good := []ma.Multiaddr{
65+
newMultiaddr(t, "/ip4/127.0.0.1/tcp/1234"),
66+
newMultiaddr(t, "/ip6/::1/tcp/1234"),
67+
newMultiaddr(t, "/ip4/1.2.3.4/udp/1234/utp"),
68+
}
69+
for _, addr := range bad {
70+
require.Falsef(t, IsIpv6LinkLocal(addr), "%s is a link local addr", addr)
71+
}
72+
for _, addr := range good {
73+
require.Truef(t, IsIpv6LinkLocal(addr), "%s is not a link local addr", addr)
74+
}
75+
}

0 commit comments

Comments
 (0)