-
Notifications
You must be signed in to change notification settings - Fork 2
/
peer.go
488 lines (421 loc) · 12.1 KB
/
peer.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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
package vmesh
import (
"bytes"
"crypto/rand"
"crypto/x509"
"errors"
"log"
"math"
"net"
"sync"
"time"
"github.com/losfair/vmesh/protocol"
"google.golang.org/protobuf/proto"
)
type MessageTag uint32
const (
MessageTag_Invalid MessageTag = iota
MessageTag_IP
MessageTag_Announce
MessageTag_Ping
MessageTag_Pong
MessageTag_UpdateDistributedConfig
MessageTag_ChannelRequest
MessageTag_ChannelResponse
)
type PeerID [32]byte
const LatencySampleSize = 10
type Peer struct {
Node *Node
LocalCert *x509.Certificate
LocalID PeerID
RemoteCert *x509.Certificate
RemoteID PeerID
RemoteName string
Out chan<- *protocol.Message
stop chan struct{}
latencyMeasurement LatencyMeasurementState
latencyResult LatencyMeasurementResult
channelKey [32]byte
udp UDPChannel
}
type LatencyMeasurementResult struct {
mu sync.RWMutex
latencyLog [LatencySampleSize]uint32
latencyFillCount int
latencyLogIndex int
}
type UDPChannel struct {
mu sync.Mutex
peerToken [32]byte
peerAddr *net.UDPAddr
peerUpdateTime time.Time
}
type LatencyMeasurementState struct {
sync.Mutex
inProgress bool
measureStart time.Time
}
func (p *Peer) GetLatencyMs() uint32 {
p.latencyResult.mu.RLock()
defer p.latencyResult.mu.RUnlock()
// We haven't got any latency information yet
if p.latencyResult.latencyFillCount == 0 {
return math.MaxUint32
}
var total uint64
for i := 0; i < p.latencyResult.latencyFillCount; i++ {
total += uint64(p.latencyResult.latencyLog[i])
}
return uint32(total / uint64(p.latencyResult.latencyFillCount))
}
func (p *Peer) PushLatencyLog(ms uint32) {
p.latencyResult.mu.Lock()
if p.latencyResult.latencyFillCount < LatencySampleSize {
p.latencyResult.latencyFillCount += 1
}
p.latencyResult.latencyLog[p.latencyResult.latencyLogIndex] = ms
p.latencyResult.latencyLogIndex = (p.latencyResult.latencyLogIndex + 1) % LatencySampleSize
p.latencyResult.mu.Unlock()
}
func (p *Peer) HandleMessage(msg *protocol.Message) error {
tag := MessageTag(msg.Tag)
switch tag {
case MessageTag_IP:
if err := p.Node.DispatchIPPacket(msg.Payload); err != nil {
if EnableDebug {
log.Println("DispatchIPPacket error:", err)
}
}
return nil
case MessageTag_Announce:
var payload protocol.Announcement
if err := proto.Unmarshal(msg.Payload, &payload); err != nil {
return errors.New("cannot unmarshal payload for Announce")
}
if len(payload.Routes) > 65536 {
return errors.New("too many routes from a single peer")
}
for _, rt := range payload.Routes {
var totalLatency uint64
if len(rt.Prefix) != 16 || rt.PrefixLength > 128 {
return errors.New("invalid prefix")
}
var prefix [16]byte
copy(prefix[:], rt.Prefix)
if !p.Node.DCState.PrefixIsWhitelisted(p.RemoteName, prefix, uint8(rt.PrefixLength)) {
// Explicitly not whitelisted
continue
}
if len(rt.Path) == 0 || !bytes.Equal(rt.Path[0].Id, p.RemoteID[:]) {
return errors.New("invalid path")
}
var circularRoute bool
for _, hop := range rt.Path {
if bytes.Equal(hop.Id, p.LocalID[:]) {
circularRoute = true
break
}
totalLatency += uint64(hop.Latency)
}
if circularRoute {
continue
}
info := RouteInfo{
Route: rt,
NextPeerID: p.RemoteID,
TotalLatency: totalLatency,
UpdateTime: time.Now(),
}
addRoute := true
displayRouteUpdateMessage := true
if err := p.Node.RoutingTable.Lookup(prefix, uint8(rt.PrefixLength), func(gotPrefix [16]byte, gotPrefixLen uint8, _oldRoute interface{}) bool {
if uint32(gotPrefixLen) != rt.PrefixLength {
return false
}
oldRoute := _oldRoute.(RouteInfo)
// Rules:
// - If this route points to the local vif, do not add route.
// - If the old route is too old, add route.
// - (majority case) If the old peer is alive, the updated route comes from the same peer, and that peer does not have major change in its route, add route without displaying message.
// - If the old peer is alive, the updated route comes from the same peer, and that peer has major change in its route, add route.
// - If the old peer is alive, the updated route comes from a different peer and does not have a latency of at least 10ms lower than our current one, do not add route.
// - Otherwise, add route.
if len(oldRoute.Route.Path) == 0 {
addRoute = false
} else if info.UpdateTime.After(oldRoute.UpdateTime) && info.UpdateTime.Sub(oldRoute.UpdateTime) > RouteTimeout {
// add route
} else {
if _, ok := p.Node.Peers.Load(oldRoute.NextPeerID); ok {
if oldRoute.NextPeerID == info.NextPeerID {
// Most time this branch should be hit.
if hopPathSimilar(oldRoute.Route, info.Route) {
displayRouteUpdateMessage = false
}
} else {
if oldRoute.TotalLatency <= info.TotalLatency || oldRoute.TotalLatency-info.TotalLatency < 10 {
addRoute = false
}
}
}
}
return false
}); err != nil {
return err
}
if addRoute {
if displayRouteUpdateMessage {
log.Printf("Updating route. Prefix = %+v, PrefixLength = %d, NextHop = %x, Latency = %d\n", net.IP(prefix[:]), rt.PrefixLength, info.NextPeerID, info.TotalLatency)
}
if err := p.Node.RoutingTable.Insert(prefix, uint8(rt.PrefixLength), info); err != nil {
log.Println("Unable to insert route into routing table:", err)
}
}
}
return nil
case MessageTag_Ping:
select {
case p.Out <- &protocol.Message{Tag: uint32(MessageTag_Pong)}:
default:
}
return nil
case MessageTag_Pong:
p.latencyMeasurement.Lock()
defer p.latencyMeasurement.Unlock()
if !p.latencyMeasurement.inProgress {
return errors.New("pong received without a previous Ping")
}
p.latencyMeasurement.inProgress = false
now := time.Now()
if now.Before(p.latencyMeasurement.measureStart) {
log.Println("Ignoring Pong as now.Before(p.latency.measureStart) == true")
return nil
}
latencyMs := uint32(now.Sub(p.latencyMeasurement.measureStart).Nanoseconds() / int64(time.Millisecond))
p.PushLatencyLog(latencyMs)
return nil
case MessageTag_UpdateDistributedConfig:
var dconf protocol.DistributedConfig
if err := proto.Unmarshal(msg.Payload, &dconf); err != nil {
if EnableDebug {
log.Println("Unable to unmarshal received distributed config")
}
return nil
}
if err := p.Node.UpdateDistributedConfig(&dconf); err != nil {
if EnableDebug {
log.Println("Error updating distributed config:", err)
}
return nil
} else {
log.Println("Applied distributed config.")
}
return nil
case MessageTag_ChannelRequest:
var req protocol.ChannelRequest
if err := proto.Unmarshal(msg.Payload, &req); err == nil {
if req.Type == protocol.ChannelType_UDP {
p.udp.mu.Lock()
copy(p.udp.peerToken[:], req.Token)
p.udp.peerUpdateTime = time.Now()
p.udp.mu.Unlock()
payload := &protocol.ChannelResponse{
Type: protocol.ChannelType_UDP,
Token: p.channelKey[:],
}
marshaled, err := proto.Marshal(payload)
if err == nil {
select {
case p.Out <- &protocol.Message{Tag: uint32(MessageTag_ChannelResponse), Payload: marshaled}:
default:
}
}
}
}
return nil
case MessageTag_ChannelResponse:
var resp protocol.ChannelResponse
if err := proto.Unmarshal(msg.Payload, &resp); err == nil {
if resp.Type == protocol.ChannelType_UDP {
p.udp.mu.Lock()
copy(p.udp.peerToken[:], resp.Token)
p.udp.peerUpdateTime = time.Now()
p.udp.mu.Unlock()
}
}
return nil
default:
return nil
}
}
func (p *Peer) Start() error {
p.stop = make(chan struct{})
if _, err := rand.Read(p.channelKey[:]); err != nil {
return err
}
go func() {
ticker := time.NewTicker(1 * time.Second)
defer ticker.Stop()
secs := uint64(0)
for ; ; secs++ {
select {
case <-p.stop:
return
case <-ticker.C:
// UDP keepalive.
if secs%1 == 0 {
p.udp.mu.Lock()
if p.udp.peerAddr != nil && p.udp.peerToken != [32]byte{} {
if encoded, ok := p.encodeLocked(nil); ok {
_, _ = p.Node.UDPChannelListener.WriteTo(encoded, p.udp.peerAddr)
}
}
p.udp.mu.Unlock()
}
// Test latency.
if (secs+1)%10 == 0 {
p.latencyMeasurement.Lock()
if !p.latencyMeasurement.inProgress {
select {
case p.Out <- &protocol.Message{Tag: uint32(MessageTag_Ping)}:
p.latencyMeasurement.inProgress = true
p.latencyMeasurement.measureStart = time.Now()
default:
}
}
p.latencyMeasurement.Unlock()
}
// Send distributed config.
if (secs+2)%30 == 0 {
p.Node.DCState.Lock()
dconf := p.Node.DCState.RawConfig
p.Node.DCState.Unlock()
if dconf != nil {
serialized, err := proto.Marshal(dconf)
if err != nil {
log.Println("Unable to marshal distributed config:", err)
} else {
select {
case p.Out <- &protocol.Message{Tag: uint32(MessageTag_UpdateDistributedConfig), Payload: serialized}:
default:
}
}
}
}
// Announce routes.
if (secs+3)%30 == 0 {
routes := make([]*protocol.Route, 0)
p.Node.RoutingTable.Range(func(prefix [16]byte, prefixLen uint8, _info interface{}) bool {
info := _info.(RouteInfo)
if !routeIsValid(info) {
p.Node.RoutingTable.Delete(prefix, prefixLen)
return true
}
route := info.Route
route.Path = append([]*protocol.Hop{{
Id: p.LocalID[:],
Latency: p.GetLatencyMs(),
}}, route.Path...)
routes = append(routes, route)
return true
})
ann := &protocol.Announcement{Routes: routes}
serialized, err := proto.Marshal(ann)
if err != nil {
log.Println("Unable to marshal announcement:", err)
} else {
select {
case p.Out <- &protocol.Message{Tag: uint32(MessageTag_Announce), Payload: serialized}:
default:
}
}
}
}
}
}()
return nil
}
func (p *Peer) Stop() {
close(p.stop)
}
func (p *Peer) encodeLocked(payload []byte) ([]byte, bool) {
outLen := 32 + 32 + len(payload)
if outLen > 1500 {
return nil, false
}
ret := make([]byte, outLen)
copy(ret[0:32], p.LocalID[:])
copy(ret[32:64], p.udp.peerToken[:])
copy(ret[64:], payload)
return ret, true
}
func (p *Peer) udpCanSendLocked() bool {
t := time.Now()
return p.udp.peerAddr != nil && t.After(p.udp.peerUpdateTime) && t.Sub(p.udp.peerUpdateTime) < 5*time.Second
}
func (p *Peer) HandleUDPRecv(raddr *net.UDPAddr, payload []byte) {
if len(payload) < 64 || !bytes.Equal(payload[0:32], p.RemoteID[:]) || !bytes.Equal(payload[32:64], p.channelKey[:]) {
return
}
p.udp.mu.Lock()
p.udp.peerUpdateTime = time.Now()
if p.udp.peerAddr == nil || !p.udp.peerAddr.IP.Equal(raddr.IP) || p.udp.peerAddr.Port != raddr.Port {
log.Printf("Received new UDP address for peer %x: %+v\n", p.RemoteID, raddr)
}
p.udp.peerAddr = raddr
p.udp.mu.Unlock()
body := payload[64:]
if len(body) > 0 {
if err := p.HandleMessage(&protocol.Message{
Tag: uint32(MessageTag_IP),
Payload: body,
}); err != nil {
if EnableDebug {
log.Println(err)
}
}
}
}
func (p *Peer) SendUDP(payload []byte) bool {
p.udp.mu.Lock()
if p.udpCanSendLocked() {
if encoded, ok := p.encodeLocked(payload); ok {
peerAddr := p.udp.peerAddr
p.udp.mu.Unlock()
if _, err := p.Node.UDPChannelListener.WriteTo(encoded, peerAddr); err == nil {
return true
} else {
return false
}
}
}
p.udp.mu.Unlock()
return false
}
func hopPathSimilar(left, right *protocol.Route) bool {
if len(left.Path) != len(right.Path) {
return false
}
var leftTotalLatency uint64
var rightTotalLatency uint64
for i, leftHop := range left.Path {
rightHop := right.Path[i]
if !bytes.Equal(leftHop.Id, rightHop.Id) {
return false
}
leftLatency, rightLatency := uint64(leftHop.Latency), uint64(rightHop.Latency)
if AbsDiffUint64(leftLatency, rightLatency) > 5 {
return false
}
leftTotalLatency += leftLatency
rightTotalLatency += rightLatency
}
return AbsDiffUint64(leftTotalLatency, rightTotalLatency) < 10
}
func AbsDiffUint64(left, right uint64) uint64 {
if left > right {
return left - right
} else {
return right - left
}
}