forked from brutella/dnssd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
serviceHandle.go
70 lines (54 loc) · 1.23 KB
/
serviceHandle.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
package dnssd
import (
"net"
"time"
"github.com/hkontrol/dnssd/log"
"github.com/miekg/dns"
)
// ServiceHandle serves a middleman between a service and a responder.
type ServiceHandle interface {
UpdateText(text map[string]string, r Responder)
Service() Service
}
type serviceHandle struct {
service *Service
}
func (h *serviceHandle) UpdateText(text map[string]string, r Responder) {
h.service.Text = text
msg := new(dns.Msg)
msg.Answer = []dns.RR{TXT(*h.service)}
msg.Response = true
msg.Authoritative = true
setAnswerCacheFlushBit(msg)
log.Debug.Println("Reannounce TXT", text)
rr := r.(*responder)
for _, iface := range h.service.Interfaces() {
resp := &Response{msg: msg, iface: iface}
go func() {
rr.conn.SendResponse(resp)
time.Sleep(1 * time.Second)
rr.conn.SendResponse(resp)
}()
}
}
func (h *serviceHandle) Service() Service {
return *h.service
}
func (h *serviceHandle) IPv4s() []net.IP {
var result []net.IP
for _, ip := range h.service.IPs {
if ip.To4() != nil {
result = append(result, ip)
}
}
return result
}
func (h *serviceHandle) IPv6s() []net.IP {
var result []net.IP
for _, ip := range h.service.IPs {
if ip.To16() != nil {
result = append(result, ip)
}
}
return result
}