This repository has been archived by the owner on Jan 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathentry.go
210 lines (180 loc) · 4.22 KB
/
entry.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
package mdns
import (
"fmt"
"log"
"net"
"strings"
"sync"
)
type srv struct {
fqn string
target string
port uint16
txt map[string]string
s *serviceDef
}
// Entry contains a full record of a mDNS entry
//
// Simplified mdns entry to show the record by IPv4
type Entry struct {
IPv4 net.IP
IPv6 net.IP
Name string
Model string
services map[string]*srv
}
// String return a simple Entry string
func (e *Entry) String() string {
return fmt.Sprintf("%16s %s %s - %v services", e.IPv4, e.Name, e.Model, len(e.services))
}
func (e *Entry) addSRV(fqn string, target string, port uint16) {
if r, ok := e.services[fqn]; ok {
r.target = target
r.port = port
return
}
e.services[fqn] = &srv{fqn: fqn, target: target, port: port, txt: make(map[string]string)}
}
func (e *Entry) addTXT(fqn string, txt []string) {
// copy to a map
m := make(map[string]string)
for i := range txt {
a := strings.Split(txt[i], "=")
if len(a) > 1 {
m[a[0]] = a[1]
}
}
// Merge txt attributes if service exist
if r, ok := e.services[fqn]; ok {
for k, v := range m {
r.txt[k] = v
}
return
}
e.services[fqn] = &srv{fqn: fqn, txt: m}
}
type mdnsTable struct {
table map[string]*Entry
sync.RWMutex // anynymous
}
func (c *mdnsTable) findByNameNoLock(name string) *Entry {
for _, v := range c.table {
if v.Name == name {
return v
}
}
return nil
}
func (c *mdnsTable) processEntry(entry *Entry) (*Entry, bool) {
model := ""
defaultModel := ""
name := ""
for _, value := range entry.services {
// Get name
if value.target != "" {
switch name {
case "":
name = value.target
default:
if name != value.target {
log.Printf("mdns ignoring dual name=%s target=%s", name, value.target)
}
}
}
// find model from TXT entry if available
if index := findServiceIndex(value.fqn); index != -1 {
s := serviceTable[index]
m := ""
if s.keyName != "" {
m = value.txt[s.keyName]
if m != "" {
m = strings.Split(m, ",")[0] // remove trailing ,xxxx
}
}
if s.authoritative && m != "" { //Sonos entry does not have model txt; m will be ""
model = m
}
if m != "" && !strings.Contains(model, m) { // append if string is new only
if model != "" {
model = model + ","
}
model = model + m
}
if !strings.Contains(defaultModel, s.defaultModel) {
if defaultModel != "" {
defaultModel = defaultModel + ","
}
defaultModel = defaultModel + s.defaultModel
}
} else {
log.Printf("mdns service not registered %+v", value)
}
}
entry.Name = name
if model != "" {
entry.Model = model
} else {
entry.Model = defaultModel
}
if entry.IPv4 == nil || entry.IPv4.IsUnspecified() || entry.Name == "" || entry.Model == "" {
if Debug {
log.Printf("mdns invalid entry %+v", *entry)
}
return nil, false
}
// spaces and special character are escaped with \\
// iphone send "Bob's\ iphone"
entry.Name = strings.Replace(entry.Name, "\\", "", -1)
modified := false
c.Lock()
current := c.table[string(entry.IPv4)]
if current == nil {
// delete stale name if IP changed
if e := c.findByNameNoLock(entry.Name); e != nil {
if Debug {
log.Printf("mdns changed IP name=%s ip=%s new_ip=%s", entry.Name, e.IPv4, entry.IPv4)
}
delete(c.table, string(e.IPv4))
}
current = &Entry{}
*current = *entry
modified = true
c.table[string(current.IPv4)] = current
if Debug {
log.Printf("mdns new entry %+v", current)
}
} else {
if current.Model != entry.Model && len(current.Model) < len(entry.Model) {
current.Model = entry.Model
current.Name = entry.Name
modified = true
if Debug {
log.Printf("mdns updated model %v", current)
}
}
// update services
for k, v := range entry.services {
current.services[k] = v
}
}
c.Unlock()
return current, modified
}
func (c *mdnsTable) getEntryByIP(ip net.IP) *Entry {
c.RLock()
defer c.RUnlock()
return c.table[string(ip)]
}
// PrintTable will print entries to stdout
func (c *mdnsTable) printTable() {
c.RLock()
defer c.RUnlock()
for _, v := range c.table {
fmt.Printf("MDNS %16s %v %v\n", v.IPv4, v.Name, v.Model)
}
}
func (c *mdnsTable) findByName(hostname string) *Entry {
c.RLock()
defer c.RUnlock()
return c.table[hostname]
}