-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdiscovery.go
61 lines (51 loc) · 1.6 KB
/
discovery.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
package main
import (
"fmt"
"log"
"net"
"strings"
"github.com/grandcat/zeroconf"
"github.com/tinyiot/thing-directory/wot"
)
// escape special characters as recommended by https://tools.ietf.org/html/rfc6763#section-4.3
func escapeDNSSDServiceInstance(instance string) (escaped string) {
// replace \ by \\
escaped = strings.ReplaceAll(instance, "\\", "\\\\")
// replace . by \.
escaped = strings.ReplaceAll(escaped, ".", "\\.")
return escaped
}
// register as a DNS-SD Service
func registerDNSSDService(conf *Config) (func(), error) {
instance := escapeDNSSDServiceInstance(conf.DNSSD.Publish.Instance)
log.Printf("DNS-SD: registering as \"%s.%s.%s\", subtype: %s",
instance, wot.DNSSDServiceType, conf.DNSSD.Publish.Domain, wot.DNSSDServiceSubtypeDirectory)
var ifs []net.Interface
for _, name := range conf.DNSSD.Publish.Interfaces {
iface, err := net.InterfaceByName(name)
if err != nil {
return nil, fmt.Errorf("error finding interface %s: %s", name, err)
}
if (iface.Flags & net.FlagMulticast) > 0 {
ifs = append(ifs, *iface)
} else {
return nil, fmt.Errorf("interface %s does not support multicast", name)
}
log.Printf("DNS-SD: will register to interface: %s", name)
}
if len(ifs) == 0 {
log.Println("DNS-SD: publish interfaces not set. Will register to all interfaces with multicast support.")
}
sd, err := zeroconf.Register(
instance,
wot.DNSSDServiceType+","+wot.DNSSDServiceSubtypeDirectory,
conf.DNSSD.Publish.Domain,
conf.HTTP.BindPort,
[]string{"td=/td", "version=" + Version},
ifs,
)
if err != nil {
return sd.Shutdown, err
}
return sd.Shutdown, nil
}