/
neighborhood.go
312 lines (275 loc) · 9.45 KB
/
neighborhood.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
// Copyright 2015 The Vanadium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package mounttablelib
import (
"net"
"strconv"
"strings"
"time"
"v.io/v23/context"
"v.io/v23/glob"
"v.io/v23/naming"
"v.io/v23/rpc"
"v.io/v23/security"
"v.io/v23/security/access"
"v.io/v23/services/mounttable"
vdltime "v.io/v23/vdlroot/time"
"v.io/v23/verror"
"v.io/x/lib/netconfig"
"v.io/x/ref/internal/logger"
mdns "github.com/presotto/go-mdns-sd"
)
var (
errNoUsefulAddresses = verror.Register(pkgPath+".errNoUsefulAddresses", verror.NoRetry, "{1:}{2:} neighborhood passed no useful addresses{:_}")
errCantFindPort = verror.Register(pkgPath+".errCantFindPort", verror.NoRetry, "{1:}{2:} neighborhood couldn't determine a port to use{:_}")
errDoesntImplementMount = verror.Register(pkgPath+".errDoesntImplementMount", verror.NoRetry, "{1:}{2:} this server does not implement Mount{:_}")
errDoesntImplementUnmount = verror.Register(pkgPath+".errDoesntImplementUnmount", verror.NoRetry, "{1:}{2:} this server does not implement Unmount{:_}")
errDoesntImplementDelete = verror.Register(pkgPath+".errDoesntImplementDelete", verror.NoRetry, "{1:}{2:} this server does not implement Delete{:_}")
errDoesntImplementSetPermissions = verror.Register(pkgPath+".errDoesntImplementSetPermissions", verror.NoRetry, "{1:}{2:} this server does not implement SetPermissions{:_}")
errSlashInHostName = verror.Register(pkgPath+".errSlashInHostName", verror.NoRetry, "{1:}{2:} hostname may not contain '/'{:_}")
)
const addressPrefix = "address:"
// neighborhood defines a set of machines on the same multicast media.
type neighborhood struct {
mdns *mdns.MDNS
nelems int
stopWatch chan struct{}
lastSubscription time.Time
}
var _ rpc.Dispatcher = (*neighborhood)(nil)
type neighborhoodService struct {
name string
elems []string
nh *neighborhood
}
func getPort(address string) uint16 {
epAddr, _ := naming.SplitAddressName(address)
ep, err := naming.ParseEndpoint(epAddr)
if err != nil {
return 0
}
addr := ep.Addr()
if addr == nil {
return 0
}
switch addr.Network() {
case "tcp", "tcp4", "tcp6", "ws", "ws4", "ws6", "wsh", "wsh4", "wsh6":
default:
return 0
}
_, pstr, err := net.SplitHostPort(addr.String())
if err != nil {
return 0
}
port, err := strconv.ParseUint(pstr, 10, 16)
if err != nil || port == 0 {
return 0
}
return uint16(port)
}
func newNeighborhood(host string, addresses []string, loopback bool) (*neighborhood, error) {
if strings.Contains(host, "/") {
return nil, verror.New(errSlashInHostName, nil)
}
// Create the TXT contents with addresses to announce. Also pick up a port number.
var txt []string
var port uint16
for _, addr := range addresses {
txt = append(txt, addressPrefix+addr)
if port == 0 {
port = getPort(addr)
}
}
if txt == nil {
return nil, verror.New(errNoUsefulAddresses, nil)
}
if port == 0 {
return nil, verror.New(errCantFindPort, nil)
}
// Start up MDNS, subscribe to the vanadium service, and add us as a vanadium service provider.
m, err := mdns.NewMDNS(host, "", "", loopback, 0)
if err != nil {
// The name may not have been unique. Try one more time with a unique
// name. NewMDNS will replace the "()" with "(hardware mac address)".
if len(host) > 0 {
m, err = mdns.NewMDNS(host+"()", "", "", loopback, 0)
}
if err != nil {
logger.Global().Errorf("mdns startup failed: %s", err)
return nil, err
}
}
logger.Global().VI(2).Infof("listening for service vanadium on port %d", port)
m.SubscribeToService("vanadium")
if len(host) > 0 {
m.AddService("vanadium", "", port, txt...)
}
// A small sleep to allow the world to learn about us and vice versa. Not
// necessary but helpful.
time.Sleep(50 * time.Millisecond)
nh := &neighborhood{
mdns: m,
}
// Watch the network configuration so that we can make MDNS reattach to
// interfaces when the network changes.
nh.stopWatch = make(chan struct{}, 1)
go func() {
ch, err := netconfig.NotifyChange()
if err != nil {
logger.Global().Errorf("neighborhood can't watch network: %v", err)
return
}
select {
case <-nh.stopWatch:
return
case <-ch:
if _, err := nh.mdns.ScanInterfaces(); err != nil {
logger.Global().Errorf("nighborhood can't scan interfaces: %s", err)
}
}
}()
return nh, nil
}
// NewLoopbackNeighborhoodDispatcher creates a new instance of a dispatcher for
// a neighborhood service provider on loopback interfaces (meant for testing).
func NewLoopbackNeighborhoodDispatcher(host string, addresses ...string) (rpc.Dispatcher, error) {
return newNeighborhood(host, addresses, true)
}
// NewNeighborhoodDispatcher creates a new instance of a dispatcher for a
// neighborhood service provider.
func NewNeighborhoodDispatcher(host string, addresses ...string) (rpc.Dispatcher, error) {
return newNeighborhood(host, addresses, false)
}
// Lookup implements rpc.Dispatcher.Lookup.
func (nh *neighborhood) Lookup(ctx *context.T, name string) (interface{}, security.Authorizer, error) {
logger.Global().VI(1).Infof("*********************LookupServer '%s'\n", name)
elems := strings.Split(name, "/")[nh.nelems:]
if name == "" {
elems = nil
}
ns := &neighborhoodService{
name: name,
elems: elems,
nh: nh,
}
return mounttable.MountTableServer(ns), nh, nil
}
func (nh *neighborhood) Authorize(*context.T, security.Call) error {
// TODO(rthellend): Figure out whether it's OK to accept all requests
// unconditionally.
return nil
}
// Stop performs cleanup.
func (nh *neighborhood) Stop() {
close(nh.stopWatch)
nh.mdns.Stop()
}
// neighbor returns the MountedServers for a particular neighbor.
func (nh *neighborhood) neighbor(instance string) []naming.MountedServer {
now := time.Now()
var reply []naming.MountedServer
si := nh.mdns.ResolveInstance(instance, "vanadium")
// Use a map to dedup any addresses seen
addrMap := make(map[string]vdltime.Deadline)
// Look for any TXT records with addresses.
for _, rr := range si.TxtRRs {
for _, s := range rr.Txt {
if !strings.HasPrefix(s, addressPrefix) {
continue
}
addr := s[len(addressPrefix):]
ttl := time.Second * time.Duration(rr.Header().Ttl)
addrMap[addr] = vdltime.Deadline{Time: now.Add(ttl)}
}
}
for addr, deadline := range addrMap {
reply = append(reply, naming.MountedServer{
Server: addr,
Deadline: deadline,
})
}
return reply
}
// neighbors returns all neighbors and their MountedServer structs.
func (nh *neighborhood) neighbors() map[string][]naming.MountedServer {
// If we haven't refreshed in a while, do it now.
if time.Now().Sub(nh.lastSubscription) > time.Duration(30)*time.Second {
nh.mdns.SubscribeToService("vanadium")
time.Sleep(50 * time.Millisecond)
nh.lastSubscription = time.Now()
}
neighbors := make(map[string][]naming.MountedServer, 0)
members := nh.mdns.ServiceDiscovery("vanadium")
for _, m := range members {
if neighbor := nh.neighbor(m.Name); neighbor != nil {
neighbors[m.Name] = neighbor
}
}
logger.Global().VI(2).Infof("members %v neighbors %v", members, neighbors)
return neighbors
}
// ResolveStep implements ResolveStep
func (ns *neighborhoodService) ResolveStep(ctx *context.T, _ rpc.ServerCall) (entry naming.MountEntry, err error) {
nh := ns.nh
ctx.VI(2).Infof("ResolveStep %v\n", ns.elems)
if len(ns.elems) == 0 {
//nothing can be mounted at the root
err = verror.New(naming.ErrNoSuchNameRoot, ctx, ns.elems)
return
}
// We can only resolve the first element and it always refers to a mount table (for now).
neighbor := nh.neighbor(ns.elems[0])
if neighbor == nil {
err = verror.New(naming.ErrNoSuchName, ctx, ns.elems)
entry.Name = ns.name
return
}
entry.ServesMountTable = true
entry.Name = naming.Join(ns.elems[1:]...)
entry.Servers = neighbor
return
}
// Mount not implemented.
func (ns *neighborhoodService) Mount(ctx *context.T, _ rpc.ServerCall, _ string, _ uint32, _ naming.MountFlag) error {
return verror.New(errDoesntImplementMount, ctx)
}
// Unmount not implemented.
func (*neighborhoodService) Unmount(ctx *context.T, _ rpc.ServerCall, _ string) error {
return verror.New(errDoesntImplementUnmount, ctx)
}
// Delete not implemented.
func (*neighborhoodService) Delete(ctx *context.T, _ rpc.ServerCall, _ bool) error {
return verror.New(errDoesntImplementDelete, ctx)
}
// Glob__ implements rpc.AllGlobber
func (ns *neighborhoodService) Glob__(ctx *context.T, call rpc.GlobServerCall, g *glob.Glob) error {
// return all neighbors that match the first element of the pattern.
nh := ns.nh
sender := call.SendStream()
switch len(ns.elems) {
case 0:
matcher := g.Head()
for k, n := range nh.neighbors() {
if matcher.Match(k) {
sender.Send(naming.GlobReplyEntry{Value: naming.MountEntry{Name: k, Servers: n, ServesMountTable: true}})
}
}
return nil
case 1:
neighbor := nh.neighbor(ns.elems[0])
if neighbor == nil {
return verror.New(naming.ErrNoSuchName, ctx, ns.elems[0])
}
sender.Send(naming.GlobReplyEntry{Value: naming.MountEntry{Name: "", Servers: neighbor, ServesMountTable: true}})
return nil
default:
return verror.New(naming.ErrNoSuchName, ctx, ns.elems)
}
}
func (*neighborhoodService) SetPermissions(ctx *context.T, _ rpc.ServerCall, _ access.Permissions, _ string) error {
return verror.New(errDoesntImplementSetPermissions, ctx)
}
func (*neighborhoodService) GetPermissions(*context.T, rpc.ServerCall) (access.Permissions, string, error) {
return nil, "", nil
}