swarm_addr.go 855 Bytes
Newer Older
1
2
3
package swarm

import (
Jeromy's avatar
Jeromy committed
4
	addrutil "github.com/libp2p/go-addr-util"
5
	iconn "github.com/libp2p/go-libp2p-interface-conn"
Jeromy's avatar
Jeromy committed
6
	ma "github.com/multiformats/go-multiaddr"
7
8
9
10
11
12
13
)

// ListenAddresses returns a list of addresses at which this swarm listens.
func (s *Swarm) ListenAddresses() []ma.Multiaddr {
	listeners := s.swarm.Listeners()
	addrs := make([]ma.Multiaddr, 0, len(listeners))
	for _, l := range listeners {
14
		if l2, ok := l.NetListener().(iconn.Listener); ok {
15
16
17
18
19
20
21
22
23
			addrs = append(addrs, l2.Multiaddr())
		}
	}
	return addrs
}

// InterfaceListenAddresses returns a list of addresses at which this swarm
// listens. It expands "any interface" addresses (/ip4/0.0.0.0, /ip6/::) to
// use the known local interfaces.
24
func (s *Swarm) InterfaceListenAddresses() ([]ma.Multiaddr, error) {
25
26
	return addrutil.ResolveUnspecifiedAddresses(s.ListenAddresses(), nil)
}