Ejemplo n.º 1
0
// 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 !manet.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)
		log.Debug("adding resolved addr:", resolve, joined, out)
	}
	if len(out) < 1 {
		return nil, fmt.Errorf("failed to resolve: %s", resolve)
	}
	return out, nil
}
Ejemplo n.º 2
0
func toPeerInfo(bp config.BootstrapPeer) peer.PeerInfo {
	// for now, we drop the "ipfs addr" part of the multiaddr. the rest
	// of the codebase currently uses addresses without the peerid part.
	m := bp.Multiaddr()
	s := ma.Split(m)
	m = ma.Join(s[:len(s)-1]...)

	return peer.PeerInfo{
		ID:    bp.ID(),
		Addrs: []ma.Multiaddr{m},
	}
}
Ejemplo n.º 3
0
func TestTransport(t *testing.T) {
	for _, g := range good {
		a, err := ParseString(g)
		if err != nil {
			t.Error("failed to parse", g, err)
			continue
		}

		m := newMultiaddr(t, g)
		split := ma.Split(m)
		m = ma.Join(split[:len(split)-1]...)
		if a.Multiaddr().Equal(m) {
			t.Error("should not be equal", a.Multiaddr(), m)
		}
		if !Transport(a).Equal(m) {
			t.Error("should be equal", Transport(a), m)
		}
	}
}
Ejemplo n.º 4
0
func Transport(iaddr IPFSAddr) (maddr ma.Multiaddr) {
	maddr = iaddr.Multiaddr()
	split := ma.Split(maddr)
	maddr = ma.Join(split[:len(split)-1]...)
	return
}