Example #1
0
File: addr.go Project: rht/ipget
// 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
}
Example #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},
	}
}
Example #3
0
func Transport(iaddr IPFSAddr) (maddr ma.Multiaddr) {
	maddr = iaddr.Multiaddr()
	split := ma.Split(maddr)
	maddr = ma.Join(split[:len(split)-1]...)
	return
}