swarm_addr.go 1.24 KB
Newer Older
1
2
3
package swarm

import (
4
5
	conn "github.com/libp2p/go-libp2p/p2p/net/conn"
	addrutil "github.com/libp2p/go-libp2p/p2p/net/swarm/addr"
6

Jeromy's avatar
Jeromy committed
7
	ma "github.com/jbenet/go-multiaddr"
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
)

// 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 {
		if l2, ok := l.NetListener().(conn.Listener); ok {
			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.
25
func (s *Swarm) InterfaceListenAddresses() ([]ma.Multiaddr, error) {
26
27
28
29
30
31
	return addrutil.ResolveUnspecifiedAddresses(s.ListenAddresses(), nil)
}

// checkNATWarning checks if our observed addresses differ. if so,
// informs the user that certain things might not work yet
func checkNATWarning(s *Swarm, observed ma.Multiaddr, expected ma.Multiaddr) {
32
	listen, err := s.InterfaceListenAddresses()
33
	if err != nil {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
34
		log.Debugf("Error retrieving swarm.InterfaceListenAddresses: %s", err)
35
36
37
38
39
		return
	}

	addrutil.CheckNATWarning(observed, expected, listen)
}