-
Notifications
You must be signed in to change notification settings - Fork 1
/
listener.go
401 lines (320 loc) · 10.1 KB
/
listener.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
package dht
import (
"encoding/hex"
"errors"
"log"
"net"
"sync"
"time"
flatbuffers "github.com/google/flatbuffers/go"
"github.com/purehyperbole/dht/protocol"
"golang.org/x/net/ipv4"
)
// a udp socket listener that processes incoming and outgoing packets
type listener struct {
// udp listener
conn *ipv4.PacketConn
// routing table
routing *routingTable
// request cache
cache *cache
// storage for all values
storage Storage
// packet manager for large packets
packet *packetManager
// flatbuffers buffer
buffer *flatbuffers.Builder
// local node id
localID []byte
// the amount of time before a request expires and times out
timeout time.Duration
// the size in bytes of the sockets send and receive buffer
bufferSize int
// collection of messages that will be read to in batch from the underlying socket
readBatch []ipv4.Message
// collection of messages that will be written in batch to the underlying socket
writeBatch []ipv4.Message
// size of the current write batch
writeBatchSize int
// mutex to protect writes to the write batch
mu sync.Mutex
// timer to schedule flushes to the underlying socket
ftimer *time.Ticker
// enables basic logging
logging bool
}
func (l *listener) process() {
for {
bs, err := l.conn.ReadBatch(l.readBatch, 0)
if err != nil {
if errors.Is(err, net.ErrClosed) {
// network connection closed, so
// we can shutdown
return
}
panic(err)
}
for i := 0; i < bs; i++ {
// if we have a fragmented packet, continue reading data
p := l.packet.assemble(l.readBatch[i].Buffers[0][:l.readBatch[i].N])
if p == nil {
continue
}
addr := l.readBatch[i].Addr.(*net.UDPAddr)
var transferKeys bool
// log.Println("received event from:", addr, "size:", rb)
e := protocol.GetRootAsEvent(p.data(), 0)
// attempt to update the node first, but if it doesn't exist, insert it
if !l.routing.seen(e.SenderBytes()) {
if l.logging {
log.Printf("discovered new node id: %s address: %s", hex.EncodeToString(e.SenderBytes()), addr.String())
}
// insert/update the node in the routing table
nid := make([]byte, e.SenderLength())
copy(nid, e.SenderBytes())
l.routing.insert(nid, addr)
// this node is new to us, so we should send it any
// keys that are closer to it than to us
transferKeys = true
}
// if this is a response to a query, send the response event to
// the registered callback
if e.Response() {
// update the senders last seen time in the routing table
l.cache.callback(e.IdBytes(), e, nil)
l.packet.done(p)
continue
}
// handle request
switch e.Event() {
case protocol.EventTypePING:
err = l.pong(e, addr)
case protocol.EventTypeSTORE:
err = l.store(e, addr)
case protocol.EventTypeFIND_NODE:
err = l.findNode(e, addr)
case protocol.EventTypeFIND_VALUE:
err = l.findValue(e, addr)
}
if err != nil {
log.Println("failed to handle request: ", err.Error())
l.packet.done(p)
continue
}
// TODO : this is going to end up with the receiver being ddos'ed
// with keys if storage is holding a large amount of values
// also, it's going to receive duplicate keys from other nodes?
// this will also lock our storage map and make us unresponsive to
// requests, potentially taking us out of other nodes routing tables.
// that may have a cascading effect...
if transferKeys {
l.transferKeys(addr, e.SenderBytes())
}
l.packet.done(p)
}
}
}
// send a pong response to the sender
func (l *listener) pong(event *protocol.Event, addr *net.UDPAddr) error {
resp := eventPong(l.buffer, event.IdBytes(), l.localID)
return l.write(addr, event.IdBytes(), resp)
}
// store a value from the sender and send a response to confirm
func (l *listener) store(event *protocol.Event, addr *net.UDPAddr) error {
payloadTable := new(flatbuffers.Table)
if !event.Payload(payloadTable) {
return errors.New("invalid store request payload")
}
s := new(protocol.Store)
s.Init(payloadTable.Bytes, payloadTable.Pos)
for i := 0; i < s.ValuesLength(); i++ {
v := new(protocol.Value)
if s.Values(v, i) {
l.storage.Set(v.KeyBytes(), v.ValueBytes(), time.Unix(0, v.Created()), time.Duration(v.Ttl()))
}
}
resp := eventStoreResponse(l.buffer, event.IdBytes(), l.localID)
return l.write(addr, event.IdBytes(), resp)
}
// find all given nodes
func (l *listener) findNode(event *protocol.Event, addr *net.UDPAddr) error {
payloadTable := new(flatbuffers.Table)
if !event.Payload(payloadTable) {
return errors.New("invalid find node request payload")
}
f := new(protocol.FindNode)
f.Init(payloadTable.Bytes, payloadTable.Pos)
// find the K closest neighbours to the given target
nodes := l.routing.closestN(f.KeyBytes(), K)
resp := eventFindNodeResponse(l.buffer, event.IdBytes(), l.localID, nodes)
return l.write(addr, event.IdBytes(), resp)
}
func (l *listener) findValue(event *protocol.Event, addr *net.UDPAddr) error {
payloadTable := new(flatbuffers.Table)
if !event.Payload(payloadTable) {
return errors.New("invalid find node request payload")
}
f := new(protocol.FindValue)
f.Init(payloadTable.Bytes, payloadTable.Pos)
vs, ok := l.storage.Get(f.KeyBytes(), time.Unix(0, f.From()))
if ok {
// we found the key in our storage, so we return it to the requester
// construct the find node table
vcap := 1100
if len(vs) < vcap {
vcap = len(vs)
}
// we can fix a maximum of ~1055 values into a single udp packet, assuming empty values.
// calculated as: 65535 - 112 (event overhead) / 62 (value table with value length of 0)
// TODO we don't need this here, just slice the results from get appropriately
values := make([]*Value, 0, vcap)
var size int // total size of the current values
for i := range vs {
if size >= MaxEventSize {
resp := eventFindValueFoundResponse(l.buffer, event.IdBytes(), l.localID, values, len(vs))
err := l.write(addr, event.IdBytes(), resp)
if err != nil {
return err
}
// reset the values array and size
values = values[:0]
size = 0
}
// add the remaining value to the array
// for the next packet. 50 is the overhead
// of the data in the value table
values = append(values, vs[i])
size = size + len(vs[i].Key) + len(vs[i].Value) + 50
}
// send any unfinished values
if len(values) > 0 {
resp := eventFindValueFoundResponse(l.buffer, event.IdBytes(), l.localID, values, len(vs))
return l.write(addr, event.IdBytes(), resp)
}
return nil
}
// we didn't find the key, so we find the K closest neighbours to the given target
nodes := l.routing.closestN(f.KeyBytes(), K)
resp := eventFindValueNotFoundResponse(l.buffer, event.IdBytes(), l.localID, nodes)
return l.write(addr, event.IdBytes(), resp)
}
func (l *listener) transferKeys(to *net.UDPAddr, id []byte) {
l.buffer.Reset()
// we can fix a maximum of ~1055 values into a single udp packet, assuming empty values.
// calculated as: 65535 - 112 (event overhead) / 62 (value table with value length of 0)
values := make([]*Value, 0, 1100)
var size int // total size of the current values
// determine whether we should transfer all nodes if the number of nodes in the network is
// below the replication factor
transferAll := l.routing.neighbours() < K
l.storage.Iterate(func(value *Value) bool {
d1 := distance(l.localID, value.Key)
d2 := distance(id, value.Key)
if transferAll || d2 > d1 {
// if we cant fit any more values in this event, send it
if size >= MaxEventSize {
rid := pseudorandomID()
req := eventStoreRequest(l.buffer, rid, l.localID, values)
err := l.request(to, rid, req, func(ev *protocol.Event, err error) bool {
if err != nil {
// just log this error for now, but it might be best to attempt to resend?
log.Println(err)
}
return true
})
if err != nil {
// log error and stop sending
log.Println(err)
return false
}
// reset the values array and size
values = values[:0]
size = 0
}
// add the remaining value to the array
// for the next packet. 50 is the overhead
// of the data in the value table
values = append(values, value)
size = size + len(value.Key) + len(value.Value) + 50
return true
}
return true
})
// send any unfinished values
if len(values) > 0 {
rid := pseudorandomID()
req := eventStoreRequest(l.buffer, rid, l.localID, values)
err := l.request(to, rid, req, func(ev *protocol.Event, err error) bool {
if err != nil {
// just log this error for now, but it might be best to attempt to resend?
log.Println(err)
}
return true
})
if err != nil {
// log error and stop sending
log.Println(err)
}
}
}
func (l *listener) request(to *net.UDPAddr, id []byte, data []byte, cb func(event *protocol.Event, err error) bool) error {
// register the callback for this request
l.cache.set(id, time.Now().Add(l.timeout), cb)
return l.write(to, id, data)
}
func (l *listener) write(to *net.UDPAddr, id, data []byte) error {
p := l.packet.fragment(id, data)
defer l.packet.done(p)
f := p.next()
l.mu.Lock()
defer l.mu.Unlock()
for f != nil {
l.writeBatch[l.writeBatchSize].Addr = to
// set the len of the buffer without allocating a new buffer
l.writeBatch[l.writeBatchSize].Buffers[0] = l.writeBatch[l.writeBatchSize].Buffers[0][:len(f)]
// copy the data from the fragment buffer into the message buffer
copy(l.writeBatch[l.writeBatchSize].Buffers[0], f)
l.writeBatchSize++
if l.writeBatchSize >= len(l.writeBatch) {
err := l.flush(false)
if err != nil {
return err
}
}
f = p.next()
}
return nil
}
func (l *listener) flusher() {
for {
_, ok := <-l.ftimer.C
if !ok {
return
}
err := l.flush(true)
if err != nil {
if errors.Is(err, net.ErrClosed) {
// network connection closed, so
// we can shutdown
return
}
panic(err)
}
}
}
func (l *listener) flush(lock bool) error {
if lock {
l.mu.Lock()
defer l.mu.Unlock()
}
if l.writeBatchSize < 1 {
return nil
}
_, err := l.conn.WriteBatch(l.writeBatch[:l.writeBatchSize], 0)
if err != nil {
return err
}
// reset the batch
l.writeBatchSize = 0
return nil
}