Fix the udp6 binding error

This commit is contained in:
Asim 2016-05-01 19:45:12 +01:00
parent f4bad1caf6
commit d37bec73c9
2 changed files with 26 additions and 3 deletions

View File

@ -182,11 +182,11 @@ func newClient() (*client, error) {
return nil, fmt.Errorf("failed to bind to any unicast udp port")
}
mconn4, err := net.ListenMulticastUDP("udp4", nil, ipv4Addr)
mconn4, err := net.ListenUDP("udp4", mdnsWildcardAddrIPv4)
if err != nil {
log.Printf("[ERR] mdns: Failed to bind to udp4 port: %v", err)
}
mconn6, err := net.ListenMulticastUDP("udp6", nil, ipv6Addr)
mconn6, err := net.ListenUDP("udp6", mdnsWildcardAddrIPv6)
if err != nil {
log.Printf("[ERR] mdns: Failed to bind to udp6 port: %v", err)
}
@ -195,6 +195,29 @@ func newClient() (*client, error) {
return nil, fmt.Errorf("failed to bind to any multicast udp port")
}
p1 := ipv4.NewPacketConn(mconn4)
p2 := ipv6.NewPacketConn(mconn6)
ifaces, err := net.Interfaces()
if err != nil {
return nil, err
}
var errCount1, errCount2 int
for _, iface := range ifaces {
if err := p1.JoinGroup(&iface, &net.UDPAddr{IP: mdnsGroupIPv4}); err != nil {
errCount1++
}
if err := p2.JoinGroup(&iface, &net.UDPAddr{IP: mdnsGroupIPv6}); err != nil {
errCount2++
}
}
if len(ifaces) == errCount1 && len(ifaces) == errCount2 {
return nil, fmt.Errorf("Failed to join multicast group on all interfaces!")
}
c := &client{
ipv4MulticastConn: mconn4,
ipv6MulticastConn: mconn6,

View File

@ -23,7 +23,7 @@ var (
Port: 5353,
}
mdnsWildcardAddrIPv6 = &net.UDPAddr{
IP: net.ParseIP("[ff02::]"),
IP: net.ParseIP("ff02::"),
Port: 5353,
}