From a56f86575daa180d5f947884a287e4ab46ed3f58 Mon Sep 17 00:00:00 2001 From: Nikos Kitmeridis Date: Wed, 22 May 2019 22:53:26 +0300 Subject: [PATCH] Fixes new client or server creation failure when one of ipv4 or ipv6 is unavailable --- client.go | 14 ++++++++++++++ server.go | 16 +++++++++------- 2 files changed, 23 insertions(+), 7 deletions(-) diff --git a/client.go b/client.go index 3d762fd..d2c278c 100644 --- a/client.go +++ b/client.go @@ -191,6 +191,13 @@ func newClient() (*client, error) { return nil, fmt.Errorf("failed to bind to any unicast udp port") } + if uconn4 == nil { + uconn4 = &net.UDPConn{} + } + if uconn6 == nil { + uconn6 = &net.UDPConn{} + } + mconn4, err := net.ListenUDP("udp4", mdnsWildcardAddrIPv4) if err != nil { log.Printf("[ERR] mdns: Failed to bind to udp4 port: %v", err) @@ -204,6 +211,13 @@ func newClient() (*client, error) { return nil, fmt.Errorf("failed to bind to any multicast udp port") } + if mconn4 == nil { + mconn4 = &net.UDPConn{} + } + if mconn6 == nil { + mconn6 = &net.UDPConn{} + } + p1 := ipv4.NewPacketConn(mconn4) p2 := ipv6.NewPacketConn(mconn6) diff --git a/server.go b/server.go index 3b7eb44..3c91586 100644 --- a/server.go +++ b/server.go @@ -73,6 +73,13 @@ func NewServer(config *Config) (*Server, error) { return nil, fmt.Errorf("[ERR] mdns: Failed to bind to any udp port!") } + if ipv4List == nil { + ipv4List = &net.UDPConn{} + } + if ipv6List == nil { + ipv6List = &net.UDPConn{} + } + // Join multicast groups to receive announcements p1 := ipv4.NewPacketConn(ipv4List) p2 := ipv6.NewPacketConn(ipv6List) @@ -112,13 +119,8 @@ func NewServer(config *Config) (*Server, error) { shutdownCh: make(chan struct{}), } - if ipv4List != nil { - go s.recv(s.ipv4List) - } - - if ipv6List != nil { - go s.recv(s.ipv6List) - } + go s.recv(s.ipv4List) + go s.recv(s.ipv6List) s.wg.Add(1) go s.probe()