Ejemplo n.º 1
0
// Builds and returns the Multiaddrs for the swarm to listen on
func BuildSwarmAddrs(port int) [][]byte {
	// Get all interface addresses
	all, _ := manet.InterfaceMultiaddrs()

	// Filter out loopback and link-local addresses
	var filtered []ma.Multiaddr
	for _, m := range all {
		if manet.IsIPLoopback(m) {
			continue
		}
		if manet.IsIP6LinkLocal(m) {
			continue
		}
		filtered = append(filtered, m)
	}

	// Add tcp/<port> to each address and convert to byte representation
	prt, err := ma.NewMultiaddr(fmt.Sprintf("/tcp/%d", port))
	if err != nil {
		panic(err)
	}

	var listenAddrs [][]byte
	for _, a := range filtered {
		listenAddrs = append(listenAddrs, a.Encapsulate(prt).Bytes())
	}
	return listenAddrs
}
Ejemplo n.º 2
0
func localAddresses() []ma.Multiaddr {
	maddrs, err := manet.InterfaceMultiaddrs()
	if err != nil {
		die(err)
	}

	if !hideLoopback {
		return maddrs
	}

	var maddrs2 []ma.Multiaddr
	for _, a := range maddrs {
		if !manet.IsIPLoopback(a) {
			maddrs2 = append(maddrs2, a)
		}
	}

	return maddrs2
}
Ejemplo n.º 3
0
// 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 := manet.InterfaceMultiaddrs()
	if err != nil {
		return nil, err
	}
	log.Debug("InterfaceAddresses: from manet:", maddrs)

	var out []ma.Multiaddr
	for _, a := range maddrs {
		if !AddrUsable(a, true) { // partial
			// log.Debug("InterfaceAddresses: skipping unusable:", a)
			continue
		}

		out = append(out, a)
	}

	log.Debug("InterfaceAddresses: usable:", out)
	return out, nil
}