-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
main.go
328 lines (281 loc) · 7.69 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
//
// This file is part of mdns-discovery.
//
// Copyright 2018-2021 ARDUINO SA (http://www.arduino.cc/)
//
// This software is released under the GNU General Public License version 3,
// which covers the main part of arduino-cli.
// The terms of this license can be found at:
// https://www.gnu.org/licenses/gpl-3.0.en.html
//
// You can be released from the requirements of the above licenses by purchasing
// a commercial license. Buying such a license is mandatory if you want to modify or
// otherwise use the software for commercial activities involving the Arduino
// software without disclosing the source code of your own applications. To purchase
// a commercial license, send an email to license@arduino.cc.
//
package main
import (
"context"
"fmt"
"io/ioutil"
"log"
"net"
"os"
"strconv"
"strings"
"sync"
"time"
properties "github.com/arduino/go-properties-orderedmap"
discovery "github.com/arduino/pluggable-discovery-protocol-handler/v2"
"github.com/hashicorp/mdns"
)
func main() {
parseArgs()
mdnsDiscovery := &MDNSDiscovery{}
disc := discovery.NewServer(mdnsDiscovery)
if err := disc.Run(os.Stdin, os.Stdout); err != nil {
fmt.Fprintf(os.Stderr, "Error: %s\n", err.Error())
os.Exit(1)
}
}
const mdnsServiceName = "_arduino._tcp"
// Discovered ports stay alive for this amount of time
// since the last time they've been found by an mDNS query.
const portsTTL = time.Second * 60
// Interval at which we check available network interfaces and call mdns.Query()
const queryInterval = time.Second * 30
// mdns.Query() will either exit early or timeout after this amount of time
const queryTimeout = time.Second * 15
// IP address used to check if we're connected to a local network
var ipv4Addr = &net.UDPAddr{
IP: net.ParseIP("224.0.0.251"),
Port: 5353,
}
// IP address used to check if IPv6 is supported by the local network
var ipv6Addr = &net.UDPAddr{
IP: net.ParseIP("ff02::fb"),
Port: 5353,
}
// QueryParam{} has to select which IP version(s) to use
type connectivity struct {
IPv4 bool
IPv6 bool
}
// MDNSDiscovery is the implementation of the network pluggable-discovery
type MDNSDiscovery struct {
cancelFunc func()
entriesChan chan *mdns.ServiceEntry
portsCache *portsCache
}
// Hello handles the pluggable-discovery HELLO command
func (d *MDNSDiscovery) Hello(userAgent string, protocolVersion int) error {
// The mdns library used has some logs statement that we must disable
log.SetOutput(ioutil.Discard)
return nil
}
// Stop handles the pluggable-discovery STOP command
func (d *MDNSDiscovery) Stop() error {
if d.cancelFunc != nil {
d.cancelFunc()
d.cancelFunc = nil
}
if d.entriesChan != nil {
close(d.entriesChan)
d.entriesChan = nil
}
if d.portsCache != nil {
d.portsCache.clear()
}
return nil
}
// Quit handles the pluggable-discovery QUIT command
func (d *MDNSDiscovery) Quit() {
d.Stop()
}
// StartSync handles the pluggable-discovery START_SYNC command
func (d *MDNSDiscovery) StartSync(eventCB discovery.EventCallback, errorCB discovery.ErrorCallback) error {
if d.entriesChan != nil {
return fmt.Errorf("already syncing")
}
if d.portsCache == nil {
// Initialize the cache if not already done
d.portsCache = newCache(portsTTL, func(port *discovery.Port) {
eventCB("remove", port)
})
}
d.entriesChan = make(chan *mdns.ServiceEntry)
go func() {
for entry := range d.entriesChan {
port := toDiscoveryPort(entry)
if port == nil {
continue
}
if updated := d.portsCache.storeOrUpdate(port); !updated {
// Port is not cached so let the user know a new one has been found
eventCB("add", port)
}
}
}()
// We use a separate channel to consume the events received
// from Query and send them over to d.entriesChan only if
// it's open.
// If we'd have used d.entriesChan to get the events from
// Query we risk panics cause of sends to a closed channel.
// Query doesn't stop right away when we call d.Stop()
// neither we have to any to do it, we can only wait for it
// to return.
queriesChan := make(chan *mdns.ServiceEntry)
ctx, cancel := context.WithCancel(context.Background())
go func() {
defer close(queriesChan)
queryLoop(ctx, queriesChan)
}()
go func() {
for entry := range queriesChan {
if d.entriesChan != nil {
d.entriesChan <- entry
}
}
}()
d.cancelFunc = cancel
return nil
}
func queryLoop(ctx context.Context, queriesChan chan<- *mdns.ServiceEntry) {
for {
var interfaces []net.Interface
var conn connectivity
var wg sync.WaitGroup
interfaces, err := availableInterfaces()
if err != nil {
goto NEXT
}
conn = checkConnectivity()
if !conn.available() {
goto NEXT
}
wg.Add(len(interfaces))
for n := range interfaces {
params := makeQueryParams(&interfaces[n], conn, queriesChan)
go func() {
defer wg.Done()
mdns.Query(params)
}()
}
wg.Wait()
NEXT:
select {
case <-time.After(queryInterval):
case <-ctx.Done():
return
}
}
}
func (conn *connectivity) available() bool {
return conn.IPv4 || conn.IPv6
}
func checkConnectivity() connectivity {
// We must check if we're connected to a local network, if we don't
// the subsequent mDNS query would fail and return an error.
// If we managed to open a connection close it, mdns.Query opens
// another one on the same IP address we use and it would fail
// if we leave this open.
out := connectivity{
IPv4: true,
IPv6: true,
}
// Check if the current network supports IPv6
mconn6, err := net.ListenMulticastUDP("udp6", nil, ipv6Addr)
if err != nil {
out.IPv6 = false
} else {
mconn6.Close()
}
// And the same for IPv4
mconn4, err := net.ListenMulticastUDP("udp4", nil, ipv4Addr)
if err != nil {
out.IPv4 = false
} else {
mconn4.Close()
}
return out
}
func availableInterfaces() ([]net.Interface, error) {
interfaces, err := net.Interfaces()
if err != nil {
return nil, err
}
var out []net.Interface
for _, netif := range interfaces {
if netif.Flags&net.FlagUp == 0 {
continue
}
if netif.Flags&net.FlagMulticast == 0 {
continue
}
if netif.HardwareAddr == nil {
continue
}
out = append(out, netif)
}
if len(out) == 0 {
return nil, fmt.Errorf("no valid network interfaces")
}
return out, nil
}
func makeQueryParams(netif *net.Interface, conn connectivity, queriesChan chan<- *mdns.ServiceEntry) (params *mdns.QueryParam) {
return &mdns.QueryParam{
Service: mdnsServiceName,
Domain: "local",
Timeout: queryTimeout,
Interface: netif,
Entries: queriesChan,
WantUnicastResponse: false,
DisableIPv4: !conn.IPv4,
DisableIPv6: !conn.IPv6,
}
}
func toDiscoveryPort(entry *mdns.ServiceEntry) *discovery.Port {
// Only entries that match the Arduino OTA service name must
// be returned
if !strings.HasSuffix(entry.Name, mdnsServiceName+".local.") {
return nil
}
ip := ""
if len(entry.AddrV4) > 0 {
ip = entry.AddrV4.String()
} else if len(entry.AddrV6) > 0 {
ip = entry.AddrV6.String()
}
props := properties.NewMap()
props.Set("hostname", entry.Host)
props.Set("port", strconv.Itoa(entry.Port))
for _, field := range entry.InfoFields {
split := strings.Split(field, "=")
if len(split) != 2 {
continue
}
key, value := split[0], split[1]
props.Set(key, value)
if key == "board" {
// duplicate for backwards compatibility
props.Set(".", value)
}
}
var name string
if split := strings.Split(entry.Host, "."); len(split) > 0 {
// Use the first part of the entry host name to display
// the address label
name = split[0]
} else {
// Fallback
name = entry.Name
}
return &discovery.Port{
Address: ip,
AddressLabel: fmt.Sprintf("%s at %s", name, ip),
Protocol: "network",
ProtocolLabel: "Network Port",
Properties: props,
}
}