Deduplicating code when parsing ipv4/ipv6 address; from the comment on the PR

This commit is contained in:
Cyril Fougeray 2020-08-20 18:36:07 +02:00
parent 3fa37a3452
commit 16d314cf30
1 changed files with 12 additions and 17 deletions

View File

@ -169,30 +169,25 @@ func (m *mdnsService) handleEntry(e *mdns.ServiceEntry) {
return
}
var maddr ma.Multiaddr
var addr net.IP
if e.AddrV4 != nil {
maddr, err = manet.FromNetAddr(&net.TCPAddr{
IP: e.AddrV4,
Port: e.Port,
})
if err != nil {
log.Warning("Error parsing multiaddr from mdns entry: ", err)
return
}
addr = e.AddrV4
} else if e.AddrV6 != nil {
maddr, err = manet.FromNetAddr(&net.TCPAddr{
IP: e.AddrV6,
Port: e.Port,
})
if err != nil {
log.Warning("Error parsing multiaddr from mdns entry: ", err)
return
}
addr = e.AddrV6
} else {
log.Warning("Error parsing multiaddr from mdns entry: no IP address found")
return
}
maddr, err := manet.FromNetAddr(&net.TCPAddr{
IP: addr,
Port: e.Port,
})
if err != nil {
log.Warning("Error parsing multiaddr from mdns entry: ", err)
return
}
pi := peer.AddrInfo{
ID: mpeer,
Addrs: []ma.Multiaddr{maddr},