From d37bec73c909e7cb2930877f877e63dde9998d25 Mon Sep 17 00:00:00 2001 From: Asim Date: Sun, 1 May 2016 19:45:12 +0100 Subject: [PATCH] Fix the udp6 binding error --- client.go | 27 +++++++++++++++++++++++++-- server.go | 2 +- 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/client.go b/client.go index b6d2ff1..24c1450 100644 --- a/client.go +++ b/client.go @@ -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, diff --git a/server.go b/server.go index 4cde1a5..77ab248 100644 --- a/server.go +++ b/server.go @@ -23,7 +23,7 @@ var ( Port: 5353, } mdnsWildcardAddrIPv6 = &net.UDPAddr{ - IP: net.ParseIP("[ff02::]"), + IP: net.ParseIP("ff02::"), Port: 5353, }