Skip to content

Commit

Permalink
move ResolveUnspecifiedAddress(es) here from libp2p/go-addr-util
Browse files Browse the repository at this point in the history
  • Loading branch information
marten-seemann committed Dec 14, 2021
1 parent fc72303 commit 458c2bc
Show file tree
Hide file tree
Showing 2 changed files with 160 additions and 0 deletions.
95 changes: 95 additions & 0 deletions net/resolve.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package manet

import (
"fmt"

ma "github.com/multiformats/go-multiaddr"
)

// ResolveUnspecifiedAddress expands an unspecified ip addresses (/ip4/0.0.0.0, /ip6/::) to
// use the known local interfaces. If ifaceAddr is nil, we request interface addresses
// from the network stack. (this is so you can provide a cached value if resolving many addrs)
func ResolveUnspecifiedAddress(resolve ma.Multiaddr, ifaceAddrs []ma.Multiaddr) ([]ma.Multiaddr, error) {
// split address into its components
split := ma.Split(resolve)

// if first component (ip) is not unspecified, use it as is.
if !IsIPUnspecified(split[0]) {
return []ma.Multiaddr{resolve}, nil
}

out := make([]ma.Multiaddr, 0, len(ifaceAddrs))
for _, ia := range ifaceAddrs {
// must match the first protocol to be resolve.
if ia.Protocols()[0].Code != resolve.Protocols()[0].Code {
continue
}

split[0] = ia
joined := ma.Join(split...)
out = append(out, joined)
}
if len(out) < 1 {
return nil, fmt.Errorf("failed to resolve: %s", resolve)
}
return out, nil
}

// ResolveUnspecifiedAddresses expands unspecified ip addresses (/ip4/0.0.0.0, /ip6/::) to
// use the known local interfaces.
func ResolveUnspecifiedAddresses(unspecAddrs, ifaceAddrs []ma.Multiaddr) ([]ma.Multiaddr, error) {
// todo optimize: only fetch these if we have a "any" addr.
if len(ifaceAddrs) < 1 {
var err error
ifaceAddrs, err = InterfaceAddresses()
if err != nil {
return nil, err
}
}

var outputAddrs []ma.Multiaddr
for _, a := range unspecAddrs {
// unspecified?
resolved, err := ResolveUnspecifiedAddress(a, ifaceAddrs)
if err != nil {
continue // optimistic. if we can't resolve anything, we'll know at the bottom.
}
outputAddrs = append(outputAddrs, resolved...)
}

if len(outputAddrs) < 1 {
return nil, fmt.Errorf("failed to specify addrs: %s", unspecAddrs)
}
return outputAddrs, nil
}

// InterfaceAddresses returns a list of addresses associated with local machine
// Note: we do not return link local addresses. IP loopback is ok, because we
// may be connecting to other nodes in the same machine.
func InterfaceAddresses() ([]ma.Multiaddr, error) {
maddrs, err := InterfaceMultiaddrs()
if err != nil {
return nil, err
}

var out []ma.Multiaddr
for _, a := range maddrs {
if !AddrOverNonLocalIP(a) {
continue
}
out = append(out, a)
}
return out, nil
}

// AddrOverNonLocalIP returns whether the addr uses a non-local ip link
func AddrOverNonLocalIP(a ma.Multiaddr) bool {
split := ma.Split(a)
if len(split) < 1 {
return false
}
if IsIP6LinkLocal(split[0]) {
return false
}
return true
}
65 changes: 65 additions & 0 deletions net/resolve_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package manet

import (
"testing"

ma "github.com/multiformats/go-multiaddr"
)

func TestResolvingAddrs(t *testing.T) {

unspec := []ma.Multiaddr{
newMultiaddr(t, "/ip4/0.0.0.0/tcp/1234"),
newMultiaddr(t, "/ip4/1.2.3.4/tcp/1234"),
newMultiaddr(t, "/ip6/::/tcp/1234"),
newMultiaddr(t, "/ip6/::100/tcp/1234"),
}

iface := []ma.Multiaddr{
newMultiaddr(t, "/ip4/127.0.0.1"),
newMultiaddr(t, "/ip4/10.20.30.40"),
newMultiaddr(t, "/ip6/::1"),
newMultiaddr(t, "/ip6/::f"),
}

spec := []ma.Multiaddr{
newMultiaddr(t, "/ip4/127.0.0.1/tcp/1234"),
newMultiaddr(t, "/ip4/10.20.30.40/tcp/1234"),
newMultiaddr(t, "/ip4/1.2.3.4/tcp/1234"),
newMultiaddr(t, "/ip6/::1/tcp/1234"),
newMultiaddr(t, "/ip6/::f/tcp/1234"),
newMultiaddr(t, "/ip6/::100/tcp/1234"),
}

actual, err := ResolveUnspecifiedAddresses(unspec, iface)
if err != nil {
t.Fatal(err)
}

for i, a := range actual {
if !a.Equal(spec[i]) {
t.Error(a, " != ", spec[i])
}
}

ip4u := []ma.Multiaddr{newMultiaddr(t, "/ip4/0.0.0.0")}
ip4i := []ma.Multiaddr{newMultiaddr(t, "/ip4/1.2.3.4")}

ip6u := []ma.Multiaddr{newMultiaddr(t, "/ip6/::")}
ip6i := []ma.Multiaddr{newMultiaddr(t, "/ip6/::1")}

if _, err := ResolveUnspecifiedAddress(ip4u[0], ip6i); err == nil {
t.Fatal("should have failed")
}
if _, err := ResolveUnspecifiedAddress(ip6u[0], ip4i); err == nil {
t.Fatal("should have failed")
}

if _, err := ResolveUnspecifiedAddresses(ip6u, ip4i); err == nil {
t.Fatal("should have failed")
}
if _, err := ResolveUnspecifiedAddresses(ip4u, ip6i); err == nil {
t.Fatal("should have failed")
}

}

0 comments on commit 458c2bc

Please sign in to comment.