Merge pull request #1165 from ipfs/fix/mdns-addr

advertise multiple addresses over mdns
This commit is contained in:
Juan Batiz-Benet 2015-05-01 01:20:43 -07:00
commit 9058b77174
1 changed files with 12 additions and 6 deletions

View File

@ -42,7 +42,8 @@ type mdnsService struct {
interval time.Duration interval time.Duration
} }
func getDialableListenAddr(ph host.Host) (*net.TCPAddr, error) { func getDialableListenAddrs(ph host.Host) ([]*net.TCPAddr, error) {
var out []*net.TCPAddr
for _, addr := range ph.Addrs() { for _, addr := range ph.Addrs() {
na, err := manet.ToNetAddr(addr) na, err := manet.ToNetAddr(addr)
if err != nil { if err != nil {
@ -50,10 +51,13 @@ func getDialableListenAddr(ph host.Host) (*net.TCPAddr, error) {
} }
tcp, ok := na.(*net.TCPAddr) tcp, ok := na.(*net.TCPAddr)
if ok { if ok {
return tcp, nil out = append(out, tcp)
} }
} }
return nil, errors.New("failed to find good external addr from peerhost") if len(out) == 0 {
return nil, errors.New("failed to find good external addr from peerhost")
}
return out, nil
} }
func NewMdnsService(peerhost host.Host, interval time.Duration) (Service, error) { func NewMdnsService(peerhost host.Host, interval time.Duration) (Service, error) {
@ -64,12 +68,14 @@ func NewMdnsService(peerhost host.Host, interval time.Duration) (Service, error)
var ipaddrs []net.IP var ipaddrs []net.IP
port := 4001 port := 4001
addr, err := getDialableListenAddr(peerhost) addrs, err := getDialableListenAddrs(peerhost)
if err != nil { if err != nil {
log.Warning(err) log.Warning(err)
} else { } else {
ipaddrs = []net.IP{addr.IP} port = addrs[0].Port
port = addr.Port for _, a := range addrs {
ipaddrs = append(ipaddrs, a.IP)
}
} }
myid := peerhost.ID().Pretty() myid := peerhost.ID().Pretty()