Fixes new client or server creation failure when one of ipv4 or ipv6 is unavailable

This commit is contained in:
Nikos Kitmeridis 2019-05-22 22:53:26 +03:00
parent 3a29b6675e
commit a56f86575d
2 changed files with 23 additions and 7 deletions

View File

@ -191,6 +191,13 @@ func newClient() (*client, error) {
return nil, fmt.Errorf("failed to bind to any unicast udp port") 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) mconn4, err := net.ListenUDP("udp4", mdnsWildcardAddrIPv4)
if err != nil { if err != nil {
log.Printf("[ERR] mdns: Failed to bind to udp4 port: %v", err) 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") 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) p1 := ipv4.NewPacketConn(mconn4)
p2 := ipv6.NewPacketConn(mconn6) p2 := ipv6.NewPacketConn(mconn6)

View File

@ -73,6 +73,13 @@ func NewServer(config *Config) (*Server, error) {
return nil, fmt.Errorf("[ERR] mdns: Failed to bind to any udp port!") 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 // Join multicast groups to receive announcements
p1 := ipv4.NewPacketConn(ipv4List) p1 := ipv4.NewPacketConn(ipv4List)
p2 := ipv6.NewPacketConn(ipv6List) p2 := ipv6.NewPacketConn(ipv6List)
@ -112,13 +119,8 @@ func NewServer(config *Config) (*Server, error) {
shutdownCh: make(chan struct{}), shutdownCh: make(chan struct{}),
} }
if ipv4List != nil {
go s.recv(s.ipv4List) go s.recv(s.ipv4List)
}
if ipv6List != nil {
go s.recv(s.ipv6List) go s.recv(s.ipv6List)
}
s.wg.Add(1) s.wg.Add(1)
go s.probe() go s.probe()