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 (
22
22
"fmt"
23
23
"io/ioutil"
24
24
"log"
25
+ "net"
25
26
"os"
26
27
"strconv"
27
28
"strings"
@@ -51,6 +52,12 @@ const portsTTL = time.Second * 60
51
52
// This is interval at which mDNS queries are made.
52
53
const discoveryInterval = time .Second * 15
53
54
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
+
54
61
// MDNSDiscovery is the implementation of the network pluggable-discovery
55
62
type MDNSDiscovery struct {
56
63
cancelFunc func ()
@@ -132,6 +139,16 @@ func (d *MDNSDiscovery) StartSync(eventCB discovery.EventCallback, errorCB disco
132
139
ctx , cancel := context .WithCancel (context .Background ())
133
140
go func () {
134
141
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
+ }
135
152
for {
136
153
if err := mdns .Query (params ); err != nil {
137
154
errorCB ("mdns lookup error: " + err .Error ())
You can’t perform that action at this time.
0 commit comments