From 16d314cf300ca99f71b196fb8321992838f92884 Mon Sep 17 00:00:00 2001 From: Cyril Fougeray Date: Thu, 20 Aug 2020 18:36:07 +0200 Subject: [PATCH] Deduplicating code when parsing ipv4/ipv6 address; from the comment on the PR --- p2p/discovery/mdns.go | 29 ++++++++++++----------------- 1 file changed, 12 insertions(+), 17 deletions(-) diff --git a/p2p/discovery/mdns.go b/p2p/discovery/mdns.go index 94fed3d31..bccc9e6d2 100644 --- a/p2p/discovery/mdns.go +++ b/p2p/discovery/mdns.go @@ -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},