diff --git a/net/resolve.go b/net/resolve.go index a2478e7..532fa87 100644 --- a/net/resolve.go +++ b/net/resolve.go @@ -7,26 +7,26 @@ import ( ) // 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) +// use the known local interfaces 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]) { + first, rest := ma.SplitFirst(resolve) + if first == nil || !IsIPUnspecified(first) { 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 { + // must match the first protocol to resolve. + match := false + ma.ForEach(ia, func(c ma.Component) bool { + match = c.Protocol().Code == first.Protocol().Code + return false + }) + if !match { continue } - - split[0] = ia - joined := ma.Join(split...) + joined := ma.Join(ia, rest) out = append(out, joined) } if len(out) < 1 { diff --git a/net/resolve_test.go b/net/resolve_test.go index 387e70a..b1e2479 100644 --- a/net/resolve_test.go +++ b/net/resolve_test.go @@ -1,6 +1,7 @@ package manet import ( + "math/rand" "testing" ma "github.com/multiformats/go-multiaddr" @@ -55,3 +56,17 @@ func TestResolvingAddrs(t *testing.T) { t.Fatal("should have failed") } } + +func BenchmarkResolveAddr(b *testing.B) { + b.ReportAllocs() + unspec := []ma.Multiaddr{ + ma.StringCast("/ip4/0.0.0.0/tcp/1234"), + } + + iface := []ma.Multiaddr{ + ma.StringCast("/ip4/127.0.0.1"), + } + for i := 0; i < b.N; i++ { + ResolveUnspecifiedAddress(unspec[rand.Intn(len(unspec))], iface) + } +} diff --git a/util.go b/util.go index cf4469a..71bddc7 100644 --- a/util.go +++ b/util.go @@ -27,16 +27,14 @@ func Join(ms ...Multiaddr) Multiaddr { } length := 0 - bs := make([][]byte, len(ms)) - for i, m := range ms { - bs[i] = m.Bytes() - length += len(bs[i]) + for _, m := range ms { + length += len(m.Bytes()) } bidx := 0 b := make([]byte, length) - for _, mb := range bs { - bidx += copy(b[bidx:], mb) + for _, m := range ms { + bidx += copy(b[bidx:], m.Bytes()) } return &multiaddr{bytes: b} }