@@ -58,6 +58,12 @@ var ipv4Addr = &net.UDPAddr{
5858 Port : 5353 ,
5959}
6060
61+ // IP address used to check if IPv6 is supported by the local network
62+ var ipv6Addr = & net.UDPAddr {
63+ IP : net .ParseIP ("ff02::fb" ),
64+ Port : 5353 ,
65+ }
66+
6167// MDNSDiscovery is the implementation of the network pluggable-discovery
6268type MDNSDiscovery struct {
6369 cancelFunc func ()
@@ -127,28 +133,40 @@ func (d *MDNSDiscovery) StartSync(eventCB discovery.EventCallback, errorCB disco
127133 // neither we have to any to do it, we can only wait for it
128134 // to return.
129135 queriesChan := make (chan * mdns.ServiceEntry )
130- params := & mdns.QueryParam {
131- Service : mdnsServiceName ,
132- Domain : "local" ,
133- Timeout : discoveryInterval ,
134- Entries : queriesChan ,
135- WantUnicastResponse : false ,
136- DisableIPv6 : true ,
137- }
138136
139137 ctx , cancel := context .WithCancel (context .Background ())
140138 go func () {
141139 defer close (queriesChan )
140+
141+ disableIPv6 := false
142+ // Check if the current network supports IPv6
143+ mconn6 , err := net .ListenMulticastUDP ("udp6" , nil , ipv6Addr )
144+ if err != nil {
145+ disableIPv6 = true
146+ } else {
147+ mconn6 .Close ()
148+ }
149+
142150 // We must check if we're connected to a local network, if we don't
143151 // the subsequent mDNS query would fail and return an error.
144- if mconn4 , err := net .ListenMulticastUDP ("udp4" , nil , ipv4Addr ); err != nil {
152+ mconn4 , err := net .ListenMulticastUDP ("udp4" , nil , ipv4Addr )
153+ if err != nil {
145154 return
146155 } else {
147156 // If we managed to open a connection close it, mdns.Query opens
148157 // another one on the same IP address we use and it would fail
149158 // if we leave this open.
150159 mconn4 .Close ()
151160 }
161+
162+ params := & mdns.QueryParam {
163+ Service : mdnsServiceName ,
164+ Domain : "local" ,
165+ Timeout : discoveryInterval ,
166+ Entries : queriesChan ,
167+ WantUnicastResponse : false ,
168+ DisableIPv6 : disableIPv6 ,
169+ }
152170 for {
153171 if err := mdns .Query (params ); err != nil {
154172 errorCB ("mdns lookup error: " + err .Error ())
0 commit comments