Skip to content

Commit f27a2c6

Browse files
committed
IPv6 is disabled only if not supported by the network
1 parent 8fde879 commit f27a2c6

File tree

1 file changed

+31
-14
lines changed

1 file changed

+31
-14
lines changed

main.go

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -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
6268
type MDNSDiscovery struct {
6369
cancelFunc func()
@@ -127,27 +133,38 @@ 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
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()
155+
}
156+
// If we managed to open a connection close it, mdns.Query opens
157+
// another one on the same IP address we use and it would fail
158+
// if we leave this open.
159+
mconn4.Close()
160+
161+
params := &mdns.QueryParam{
162+
Service: mdnsServiceName,
163+
Domain: "local",
164+
Timeout: discoveryInterval,
165+
Entries: queriesChan,
166+
WantUnicastResponse: false,
167+
DisableIPv6: disableIPv6,
151168
}
152169
for {
153170
if err := mdns.Query(params); err != nil {

0 commit comments

Comments
 (0)