-
Notifications
You must be signed in to change notification settings - Fork 182
/
hosts.go
319 lines (253 loc) · 6.36 KB
/
hosts.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
// Copyright 2016-2017 VMware, Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package etcconf
import (
"bytes"
"fmt"
"net"
"os"
"sort"
"strings"
"sync"
log "github.com/Sirupsen/logrus"
)
type Hosts interface {
Conf
SetHost(hostname string, ip net.IP)
RemoveHost(hostname string)
RemoveAll()
HostIP(hostname string) []net.IP
}
type hostEntry struct {
IP net.IP
Hostnames []string
newAddr bool
}
func (e *hostEntry) String() string {
return fmt.Sprintf("%s %s", e.IP, strings.Join(e.Hostnames, " "))
}
func (e *hostEntry) addNames(names ...string) string {
e.Hostnames = append(e.Hostnames, names...)
sort.Strings(e.Hostnames)
return e.IP.String() + " " + strings.Join(e.Hostnames, " ")
}
func (e *hostEntry) setAddress(ip net.IP) {
if e.IP != nil {
hostnames := strings.Join(e.Hostnames, " ")
log.Infof("Changing IP address: %s -> %s", e.IP.String(), ip.String())
log.Infof("IP change impacts the following hostnames: %s", hostnames)
if e.newAddr {
log.Warn("Address has changed more than once since last load, implying a configuration race")
}
e.newAddr = true
}
e.IP = ip
}
type hosts struct {
sync.Mutex
EntryConsumer
hostsIPv4 map[string]*hostEntry
hostsIPv6 map[string]*hostEntry
entries map[string]*hostEntry
dirty bool
path string
}
type hostsWalker struct {
entries []*hostEntry
i int
}
func (w *hostsWalker) HasNext() bool {
return w.i < len(w.entries)
}
func (w *hostsWalker) Next() string {
s := w.entries[w.i].String()
w.i++
return s
}
func NewHosts(path string) Hosts {
return newHosts(path)
}
func newHosts(path string) *hosts {
if path == "" {
path = HostsPath
}
return &hosts{
path: path,
hostsIPv4: make(map[string]*hostEntry),
hostsIPv6: make(map[string]*hostEntry),
entries: make(map[string]*hostEntry),
}
}
func (h *hosts) ConsumeEntry(t string) error {
h.Lock()
defer h.Unlock()
fs := strings.Fields(t)
if len(fs) < 2 {
log.Warnf("ignoring incomplete line %q", t)
return nil
}
ip := net.ParseIP(fs[0])
if ip == nil {
log.Warnf("ignoring line %q due to invalid ip address", t)
return nil
}
for _, hs := range fs[1:] {
h.setHost(hs, ip)
}
return nil
}
// Needs to ensure that host entries don't occur twice, and that we have stable reconsiliation if they do
func (h *hosts) Load() error {
h.Lock()
defer h.Unlock()
newHosts := newHosts(h.path)
if err := load(h.path, newHosts); err != nil {
return err
}
h.hostsIPv4 = newHosts.hostsIPv4
h.hostsIPv6 = newHosts.hostsIPv6
h.entries = newHosts.entries
h.dirty = false
return nil
}
// Seed pulls replaces the current state with that from the provided hosts
// It _does not_ perform a deep copy so performs a Save immediately.
func (h *hosts) Copy(conf Conf) error {
// straight up panic if not the appropriate type
existing := conf.(*hosts)
existing.Lock()
defer existing.Unlock()
h.hostsIPv4 = existing.hostsIPv4
h.hostsIPv6 = existing.hostsIPv6
h.entries = existing.entries
h.dirty = true
return h.Save()
}
// ensure hostname is associated with localhost
func (h *hosts) Save() error {
h.Lock()
defer h.Unlock()
if !h.dirty {
log.Debugf("skipping writing file since there are no new entries")
return nil
}
var entries []*hostEntry
for _, v := range h.entries {
entries = append(entries, v)
}
if err := save(h.path, &hostsWalker{entries: entries}); err != nil {
return err
}
// make sure the file is readable
// #nosec: Expect file permissions to be 0600 or less
if err := os.Chmod(h.path, 0644); err != nil {
return err
}
h.dirty = false
return nil
}
func (h *hosts) SetHost(hostname string, ip net.IP) {
h.Lock()
defer h.Unlock()
h.setHost(hostname, ip)
}
func (h *hosts) setHost(hostname string, ip net.IP) {
h.dirty = true
if ip == nil {
return
}
// what type of address is it?
hostmap := h.hostsIPv6
ipv4 := ip.To4()
if ipv4 != nil {
// this drops any leading garbage in the array so that byte comparisons work as
// expected
ip = ipv4
hostmap = h.hostsIPv4
}
hentry := hostmap[hostname]
ientry := h.entries[ip.String()]
if hentry != nil {
// existing entry with no changes
if bytes.Equal(hentry.IP, ip) {
h.dirty = false
return
}
// existing hostname with a new address - change address for all assocated hostnames
hentry.setAddress(ip)
return
}
// completely new entry for this ip address type
if ientry == nil {
entry := &hostEntry{
IP: ip,
Hostnames: []string{hostname},
}
h.entries[ip.String()] = entry
hostmap[hostname] = entry
return
}
// add the hostname indexed IP record
ientry.Hostnames = append(ientry.Hostnames, hostname)
hostmap[hostname] = ientry
return
}
func (h *hosts) RemoveHost(hostname string) {
h.Lock()
defer h.Unlock()
for _, hostmap := range []map[string]*hostEntry{h.hostsIPv4, h.hostsIPv6} {
entry := hostmap[hostname]
if entry == nil {
continue
}
h.dirty = true
delete(hostmap, hostname)
if len(entry.Hostnames) < 2 {
log.Infof("Removing hostname and address: %s (%s)", hostname, entry.IP.String())
delete(h.entries, entry.IP.String())
continue
}
var remaining []string
for i := range entry.Hostnames {
if entry.Hostnames[i] == hostname {
remaining = entry.Hostnames[:i]
remaining = append(remaining, entry.Hostnames[i+1:]...)
}
}
entry.Hostnames = remaining
}
}
func (h *hosts) RemoveAll() {
h.Lock()
defer h.Unlock()
h.dirty = len(h.hostsIPv4) > 0 || len(h.hostsIPv6) > 0
h.hostsIPv4 = make(map[string]*hostEntry)
h.hostsIPv6 = make(map[string]*hostEntry)
h.entries = make(map[string]*hostEntry)
}
func (h *hosts) HostIP(hostname string) []net.IP {
h.Lock()
defer h.Unlock()
var ips []net.IP
if ipv4, ok := h.hostsIPv4[hostname]; ok {
ips = append(ips, ipv4.IP)
}
if ipv6, ok := h.hostsIPv6[hostname]; ok {
ips = append(ips, ipv6.IP)
}
return ips
}
func (h *hosts) Path() string {
return h.path
}