forked from brutella/dnssd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cache.go
193 lines (165 loc) · 3.94 KB
/
cache.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
package dnssd
import (
"net"
"sort"
"strings"
"time"
"github.com/miekg/dns"
)
type Cache struct {
services map[string]*Service
}
func NewCache() *Cache {
return &Cache{
services: make(map[string]*Service),
}
}
func (c *Cache) Services() []*Service {
tmp := []*Service{}
for _, s := range c.services {
tmp = append(tmp, s)
}
return tmp
}
// UpdateFrom updates the cache from resource records in msg.
// TODO consider the cache-flush bit to make records as to be deleted in one second
func (c *Cache) UpdateFrom(msg *dns.Msg, iface *net.Interface) (adds []*Service, rmvs []*Service) {
answers := filterRecords(msg, iface, nil)
sort.Sort(byType(answers))
for _, answer := range answers {
switch rr := answer.(type) {
case *dns.PTR:
ttl := time.Duration(rr.Hdr.Ttl) * time.Second
var entry *Service
if e, ok := c.services[rr.Ptr]; !ok {
if ttl == 0 {
// Ignore new records with no ttl
break
}
entry = newService(rr.Ptr)
adds = append(adds, entry)
c.services[entry.ServiceInstanceName()] = entry
} else {
entry = e
}
entry.TTL = ttl
entry.expiration = time.Now().Add(ttl)
case *dns.SRV:
ttl := time.Duration(rr.Hdr.Ttl) * time.Second
var entry *Service
if e, ok := c.services[rr.Hdr.Name]; !ok {
if ttl == 0 {
// Ignore new records with no ttl
break
}
entry = newService(rr.Hdr.Name)
adds = append(adds, entry)
c.services[entry.ServiceInstanceName()] = entry
} else {
entry = e
}
entry.SetHostname(rr.Target)
entry.TTL = ttl
entry.expiration = time.Now().Add(ttl)
entry.Port = int(rr.Port)
case *dns.A:
for _, entry := range c.services {
if entry.Hostname() == rr.Hdr.Name {
entry.addIP(rr.A, iface)
}
}
case *dns.AAAA:
for _, entry := range c.services {
if entry.Hostname() == rr.Hdr.Name {
entry.addIP(rr.AAAA, iface)
}
}
case *dns.TXT:
if entry, ok := c.services[rr.Hdr.Name]; ok {
text := make(map[string]string)
for _, txt := range rr.Txt {
elems := strings.SplitN(txt, "=", 2)
if len(elems) == 2 {
key := elems[0]
value := elems[1]
// Don't override existing keys
// TODO make txt records case insensitive
if _, ok := text[key]; !ok {
text[key] = value
}
text[key] = value
}
}
entry.Text = text
entry.TTL = time.Duration(rr.Hdr.Ttl) * time.Second
entry.expiration = time.Now().Add(entry.TTL)
}
default:
// ignore
break
}
}
// TODO remove outdated services regularly
rmvs = c.removeExpired()
return
}
func (c *Cache) removeExpired() []*Service {
var outdated []*Service
var services = c.services
for key, srv := range services {
if time.Now().After(srv.expiration) {
outdated = append(outdated, srv)
delete(c.services, key)
}
}
return outdated
}
type byType []dns.RR
func (a byType) Len() int { return len(a) }
func (a byType) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a byType) Less(i, j int) bool {
// Sort in the following order
// 1. SRV or PTR
// 2. Anything else
switch a[i].(type) {
case *dns.SRV:
return true
case *dns.PTR:
return true
}
return false
}
func filterRecords(m *dns.Msg, iface *net.Interface, service *Service) []dns.RR {
if iface != nil && service != nil && len(service.Ifaces) > 0 {
if !service.IsVisibleAtInterface(iface.Name) {
// Ignnore message if coming from a ignored interface.
return []dns.RR{}
}
}
var all []dns.RR
all = append(all, m.Answer...)
all = append(all, m.Ns...)
all = append(all, m.Extra...)
if service == nil {
return all
}
var answers []dns.RR
for _, answer := range all {
switch rr := answer.(type) {
case *dns.SRV:
if rr.Hdr.Name != service.ServiceInstanceName() {
continue
}
case *dns.A:
if service.Hostname() != rr.Hdr.Name {
continue
}
case *dns.AAAA:
if service.Hostname() != rr.Hdr.Name {
continue
}
}
answers = append(answers, answer)
}
return answers
}