File tree Expand file tree Collapse file tree 1 file changed +17
-0
lines changed Expand file tree Collapse file tree 1 file changed +17
-0
lines changed Original file line number Diff line number Diff line change @@ -22,6 +22,7 @@ import (
2222 "fmt"
2323 "io/ioutil"
2424 "log"
25+ "net"
2526 "os"
2627 "strconv"
2728 "strings"
@@ -51,6 +52,12 @@ const portsTTL = time.Second * 60
5152// This is interval at which mDNS queries are made.
5253const discoveryInterval = time .Second * 15
5354
55+ // IP address used to check if we're connected to a local network
56+ var ipv4Addr = & net.UDPAddr {
57+ IP : net .ParseIP ("224.0.0.251" ),
58+ Port : 5353 ,
59+ }
60+
5461// MDNSDiscovery is the implementation of the network pluggable-discovery
5562type MDNSDiscovery struct {
5663 cancelFunc func ()
@@ -132,6 +139,16 @@ func (d *MDNSDiscovery) StartSync(eventCB discovery.EventCallback, errorCB disco
132139 ctx , cancel := context .WithCancel (context .Background ())
133140 go func () {
134141 defer close (queriesChan )
142+ // We must check if we're connected to a local network, if we don't
143+ // the subsequent mDNS query would fail and return an error.
144+ if mconn4 , err := net .ListenMulticastUDP ("udp4" , nil , ipv4Addr ); err != nil {
145+ return
146+ } else {
147+ // If we managed to open a connection close it, mdns.Query opens
148+ // another one on the same IP address we use and it would fail
149+ // if we leave this open.
150+ mconn4 .Close ()
151+ }
135152 for {
136153 if err := mdns .Query (params ); err != nil {
137154 errorCB ("mdns lookup error: " + err .Error ())
You can’t perform that action at this time.
0 commit comments