forked from nictuku/dht
/
dht.go
704 lines (641 loc) · 20.9 KB
/
dht.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
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
// DHT node for Taipei Torrent, for tracker-less peer information exchange.
// Status:
// Supports all DHT operations from the specification, except:
// - TODO: handle announce_peer.
package dht
// Summary from the bittorrent DHT protocol specification:
//
// Message types:
// - query
// - response
// - error
//
// RPCs:
// ping:
// see if node is reachable and save it on routing table.
// find_node:
// run when DHT node count drops, or every X minutes. Just to ensure
// our DHT routing table is still useful.
// get_peers:
// the real deal. Iteratively queries DHT nodes and find new sources
// for a particular infohash.
// announce_peer:
// announce that the peer associated with this node is downloading a
// torrent.
//
// Reference:
// http://www.bittorrent.org/beps/bep_0005.html
//
import (
"crypto/rand"
"expvar"
"flag"
"fmt"
"net"
"strings"
"time"
l4g "code.google.com/p/log4go"
"github.com/nictuku/nettools"
)
var (
dhtRouter string
maxNodes int
cleanupPeriod time.Duration
savePeriod time.Duration
rateLimit int64
rateLimitEnabled = true
)
func init() {
// TODO: Run my own router.
flag.StringVar(&dhtRouter, "dhtRouter", "router.utorrent.com:6881",
"IP:Port address of the DHT router used to bootstrap the DHT network.")
flag.IntVar(&maxNodes, "maxNodes", 1000,
"Maximum number of nodes to store in the routing table, in memory.")
flag.DurationVar(&cleanupPeriod, "cleanupPeriod", 15*time.Minute,
"How often to ping nodes in the network to see if they are reachable.")
flag.DurationVar(&savePeriod, "savePeriod", 5*time.Minute,
"How often to save the routing table to disk.")
flag.Int64Var(&rateLimit, "rateLimit", 100,
"Maximum packets per second to be processed. Beyond this limit they are silently dropped. Set to -1 to disable rate limiting.")
// TODO: Control the verbosity via flag.
// Setting during init has two purposes:
// - Gives the calling program the ability to override this filter inside their main().
// - Provides a sane default that isn't excessively verbose.
l4g.AddFilter("stdout", l4g.WARNING, l4g.NewConsoleLogWriter())
}
// DHT should be created by NewDHTNode(). It provides DHT features to a
// torrent client, such as finding new peers for torrent downloads without
// requiring a tracker.
type DHT struct {
nodeId string
port int
routingTable *routingTable
peerStore *peerStore
numTargetPeers int
conn *net.UDPConn
Logger Logger
exploredNeighborhood bool
remoteNodeAcquaintance chan string
peersRequest chan peerReq
pingRequest chan *remoteNode
clientThrottle *nettools.ClientThrottle
store *DHTStore
// Public channels:
PeersRequestResults chan map[InfoHash][]string // key = infohash, v = slice of peers.
}
func NewDHTNode(port, numTargetPeers int, storeEnabled bool) (node *DHT, err error) {
node = &DHT{
port: port,
routingTable: newRoutingTable(),
peerStore: newPeerStore(),
PeersRequestResults: make(chan map[InfoHash][]string, 1),
exploredNeighborhood: false,
// Buffer to avoid blocking on sends.
remoteNodeAcquaintance: make(chan string, 100),
// Buffer to avoid deadlocks and blocking on sends.
peersRequest: make(chan peerReq, 100),
pingRequest: make(chan *remoteNode),
numTargetPeers: numTargetPeers,
clientThrottle: nettools.NewThrottler(),
}
c := openStore(port, storeEnabled)
node.store = c
if len(c.Id) != 20 {
c.Id = randNodeId()
l4g.Info("newId: %x %d", c.Id, len(c.Id))
saveStore(*c)
}
// The types don't match because JSON marshalling needs []byte.
node.nodeId = string(c.Id)
expNodeIds.Add(fmt.Sprintf("%x", node.nodeId), 0)
// XXX refactor.
node.routingTable.nodeId = node.nodeId
// This is called before the engine is up and ready to read from the
// underlying channel.
go func() {
for addr, _ := range c.Remotes {
node.AddNode(addr)
}
}()
return
}
// Logger allows the DHT client to attach hooks for certain RPCs so it can log
// interesting events any way it wants.
type Logger interface {
GetPeers(*net.UDPAddr, string, InfoHash)
}
type peerReq struct {
ih InfoHash
announce bool
}
// PeersRequest asks the DHT to search for more peers for the infoHash
// provided. announce should be true if the connected peer is actively
// downloading this infohash, which is normally the case - unless this DHT node
// is just a router that doesn't downloads torrents.
func (d *DHT) PeersRequest(ih string, announce bool) {
d.peersRequest <- peerReq{InfoHash(ih), announce}
}
// AddNode informs the DHT of a new node it should add to its routing table.
// addr is a string containing the target node's "host:port" UDP address.
func (d *DHT) AddNode(addr string) {
d.remoteNodeAcquaintance <- addr
}
// Asks for more peers for a torrent.
func (d *DHT) getPeers(infoHash InfoHash) {
closest := d.routingTable.lookupFiltered(infoHash)
for _, r := range closest {
// *remoteNode is nil if it got filtered.
if r != nil {
d.getPeersFrom(r, infoHash)
}
}
}
// Find a DHT node.
func (d *DHT) findNode(id string) {
ih := InfoHash(id)
closest := d.routingTable.lookupFiltered(ih)
for _, r := range closest {
if r != nil {
d.findNodeFrom(r, id)
}
}
}
// DoDHT is the DHT node main loop and should be run as a goroutine by the torrent client.
func (d *DHT) DoDHT() {
socketChan := make(chan packetType)
socket, err := listen(d.port)
if err != nil {
return
}
d.conn = socket
bytesArena := newArena(maxUDPPacketSize, 500)
go readFromSocket(socket, socketChan, bytesArena)
// Bootstrap the network.
d.ping(dhtRouter)
cleanupTicker := time.Tick(cleanupPeriod)
saveTicker := make(<-chan time.Time)
if d.store != nil {
saveTicker = time.Tick(savePeriod)
}
var fillTokenBucket <-chan time.Time
tokenBucket := rateLimit
if rateLimit < 0 {
rateLimitEnabled = false
l4g.Warn("rate limiting disabled")
} else {
// Token bucket for limiting the number of packets per second.
fillTokenBucket = time.Tick(time.Second / 10)
if rateLimit < 10 {
// Less than 10 leads to rounding problems.
rateLimit = 10
}
}
l4g.Info("DHT: Starting DHT node %x.", d.nodeId)
for {
select {
case addr := <-d.remoteNodeAcquaintance:
d.helloFromPeer(addr)
case peersRequest := <-d.peersRequest:
// torrent server is asking for more peers for a particular infoHash. Ask the closest nodes for
// directions. The goroutine will write into the PeersNeededResults channel.
if peersRequest.announce {
d.peerStore.addLocalDownload(peersRequest.ih)
}
if d.peerStore.count(peersRequest.ih) < d.numTargetPeers {
l4g.Info("DHT: torrent client asking more peers for %x. Calling getPeers().", peersRequest.ih)
d.getPeers(peersRequest.ih)
}
case p := <-socketChan:
totalRecv.Add(1)
if rateLimitEnabled {
if tokenBucket > 0 {
d.processPacket(p)
tokenBucket -= 1
} else {
// In the future it might be better to avoid dropping things like ping replies.
totalDroppedPackets.Add(1)
}
} else {
d.processPacket(p)
}
bytesArena.Push(p.b)
case <-fillTokenBucket:
if tokenBucket < rateLimit {
tokenBucket += rateLimit / 10
}
case <-cleanupTicker:
needPing := d.routingTable.cleanup()
go pingSlowly(d.pingRequest, needPing, cleanupPeriod)
case node := <-d.pingRequest:
d.pingNode(node)
case <-saveTicker:
tbl := d.routingTable.reachableNodes()
if len(tbl) > 5 {
d.store.Remotes = tbl
saveStore(*d.store)
}
}
}
}
func (d *DHT) helloFromPeer(addr string) {
// We've got a new node id. We need to:
// - see if we know it already, skip accordingly.
// - ping it and see if it's reachable.
// - if it responds, save it in the routing table.
_, addrResolved, existed, err := d.routingTable.hostPortToNode(addr)
if err != nil {
l4g.Warn("helloFromPeer error: %v", err)
return
}
if existed {
// Node host+port already known.
return
}
if d.routingTable.length() < maxNodes {
d.ping(addrResolved)
return
}
}
func (d *DHT) processPacket(p packetType) {
if !d.clientThrottle.CheckBlock(p.raddr.IP.String()) {
totalPacketsFromBlockedHosts.Add(1)
return
}
if p.b[0] != 'd' {
// Malformed DHT packet. There are protocol extensions out
// there that we don't support or understand.
return
}
r, err := readResponse(p)
if err != nil {
l4g.Warn("DHT: readResponse Error: %v, %q", err, string(p.b))
return
}
switch {
// Response.
case r.Y == "r":
node, addr, existed, err := d.routingTable.hostPortToNode(p.raddr.String())
if err != nil {
l4g.Info("DHT readResponse error processing response: %v", err)
return
}
if !existed {
l4g.Info("DHT: Received reply from a host we don't know: %v", p.raddr)
if d.routingTable.length() < maxNodes {
d.ping(addr)
}
return
}
// Fix the node ID.
if node.id == "" {
node.id = r.R.Id
d.routingTable.update(node)
}
if node.id != r.R.Id {
l4g.Debug("DHT: Node changed IDs %x => %x", node.id, r.R.Id)
}
if query, ok := node.pendingQueries[r.T]; ok {
if !node.reachable {
node.reachable = true
totalReachableNodes.Add(1)
}
node.lastTime = time.Now()
d.routingTable.neighborhoodUpkeep(node)
// If this is the first host added to the routing table, attempt a
// recursive lookup of our own address, to build our neighborhood.
if !d.exploredNeighborhood {
d.findNode(d.nodeId)
}
d.exploredNeighborhood = true
switch query.Type {
case "ping":
// Served its purpose, nothing else to be done.
l4g.Trace("DHT: Received ping reply")
totalRecvPingReply.Add(1)
case "get_peers":
d.processGetPeerResults(node, r)
case "find_node":
d.processFindNodeResults(node, r)
case "announce_peer":
// Nothing to do. In the future, update counters.
default:
l4g.Info("DHT: Unknown query type: %v from %v", query.Type, addr)
}
node.pastQueries[r.T] = query
delete(node.pendingQueries, r.T)
} else {
l4g.Info("DHT: Unknown query id: %v", r.T)
}
case r.Y == "q":
_, addr, existed, err := d.routingTable.hostPortToNode(p.raddr.String())
if err != nil {
l4g.Warn("Error readResponse error processing query: %v", err)
return
}
if !existed {
// Another candidate for the routing table. See if it's reachable.
if d.routingTable.length() < maxNodes {
d.ping(addr)
}
}
switch r.Q {
case "ping":
d.replyPing(p.raddr, r)
case "get_peers":
d.replyGetPeers(p.raddr, r)
case "find_node":
d.replyFindNode(p.raddr, r)
case "announce_peer":
d.replyAnnouncePeer(p.raddr, r)
default:
l4g.Trace("DHT: non-implemented handler for type %v", r.Q)
}
default:
l4g.Info("DHT: Bogus DHT query from %v.", p.raddr)
}
}
func (d *DHT) ping(address string) {
r, err := d.routingTable.getOrCreateNode("", address)
if err != nil {
l4g.Info("ping error for address %v: %v", address, err)
return
}
d.pingNode(r)
}
func (d *DHT) pingNode(r *remoteNode) {
l4g.Debug("DHT: ping => %+v\n", r.address)
t := r.newQuery("ping")
queryArguments := map[string]interface{}{"id": d.nodeId}
query := queryMessage{t, "q", "ping", queryArguments}
sendMsg(d.conn, r.address, query)
totalSentPing.Add(1)
}
func (d *DHT) getPeersFrom(r *remoteNode, ih InfoHash) {
totalSentGetPeers.Add(1)
ty := "get_peers"
transId := r.newQuery(ty)
r.pendingQueries[transId].ih = ih
queryArguments := map[string]interface{}{
"id": d.nodeId,
"info_hash": ih,
}
query := queryMessage{transId, "q", ty, queryArguments}
l4g.Trace(func() string {
x := hashDistance(InfoHash(r.id), ih)
return fmt.Sprintf("DHT sending get_peers. nodeID: %x , InfoHash: %x , distance: %x", r.id, ih, x)
})
sendMsg(d.conn, r.address, query)
}
func (d *DHT) findNodeFrom(r *remoteNode, id string) {
totalSentFindNode.Add(1)
ty := "find_node"
transId := r.newQuery(ty)
r.pendingQueries[transId].ih = InfoHash(id)
queryArguments := map[string]interface{}{
"id": d.nodeId,
"target": id,
}
query := queryMessage{transId, "q", ty, queryArguments}
l4g.Trace(func() string {
x := hashDistance(InfoHash(r.id), InfoHash(id))
return fmt.Sprintf("DHT sending find_node. nodeID: %x , target ID: %x , distance: %x", r.id, id, x)
})
sendMsg(d.conn, r.address, query)
}
// announcePeer sends a message to the destination address to advertise that
// our node is a peer for this infohash, using the provided token to
// 'authenticate'.
func (d *DHT) announcePeer(address *net.UDPAddr, ih InfoHash, token string) {
r, err := d.routingTable.getOrCreateNode("", address.String())
if err != nil {
l4g.Trace("announcePeer:", err)
return
}
ty := "announce_peer"
l4g.Trace("DHT: announce_peer => %v %x %x\n", address, ih, token)
transId := r.newQuery(ty)
queryArguments := map[string]interface{}{
"id": d.nodeId,
"info_hash": ih,
"port": d.port,
"token": token,
}
query := queryMessage{transId, "q", ty, queryArguments}
sendMsg(d.conn, address, query)
}
func (d *DHT) replyAnnouncePeer(addr *net.UDPAddr, r responseType) {
l4g.Trace(func() string {
ih := InfoHash(r.A.InfoHash)
return fmt.Sprintf("DHT: announce_peer. Host %v, nodeID: %x, infoHash: %x, peerPort %d, distance to me %x",
addr, r.A.Id, ih, r.A.Port, hashDistance(ih, InfoHash(d.nodeId)),
)
})
peerAddr := net.TCPAddr{IP: addr.IP, Port: r.A.Port}
d.peerStore.addContact(InfoHash(r.A.InfoHash), nettools.DottedPortToBinary(peerAddr.String()))
// Always reply positively. jech says this is to avoid "back-tracking", not sure what that means.
reply := replyMessage{
T: r.T,
Y: "r",
R: map[string]interface{}{"id": d.nodeId},
}
sendMsg(d.conn, addr, reply)
}
func (d *DHT) replyGetPeers(addr *net.UDPAddr, r responseType) {
totalRecvGetPeers.Add(1)
l4g.Info(func() string {
return fmt.Sprintf("DHT get_peers. Host: %v , nodeID: %x , InfoHash: %x , distance to me: %x", addr, r.A.Id, InfoHash(r.A.InfoHash), hashDistance(r.A.InfoHash, InfoHash(d.nodeId)))
})
if d.Logger != nil {
d.Logger.GetPeers(addr, r.A.Id, r.A.InfoHash)
}
ih := r.A.InfoHash
// TODO(nictuku): Implement token rotation.
// See https://github.com/jech/dht/blob/master/dht.c
// rotate_secrets(), make_token()
r0 := map[string]interface{}{"id": d.nodeId, "token": "blabla"}
reply := replyMessage{
T: r.T,
Y: "r",
R: r0,
}
if peerContacts := d.peersForInfoHash(ih); len(peerContacts) > 0 {
reply.R["values"] = peerContacts
} else {
reply.R["nodes"] = d.nodesForInfoHash(ih)
}
sendMsg(d.conn, addr, reply)
}
func (d *DHT) nodesForInfoHash(ih InfoHash) string {
n := make([]string, 0, kNodes)
for _, r := range d.routingTable.lookup(ih) {
// r is nil when the node was filtered.
if r != nil {
n = append(n, r.id+nettools.DottedPortToBinary(r.address.String()))
}
}
l4g.Trace("replyGetPeers: Nodes only. Giving %d", len(n))
return strings.Join(n, "")
}
func (d *DHT) peersForInfoHash(ih InfoHash) []string {
peerContacts := d.peerStore.peerContacts(ih)
if len(peerContacts) > 0 {
l4g.Trace("replyGetPeers: Giving peers! %x was requested, and we knew %d peers!", ih, len(peerContacts))
}
return peerContacts
}
func (d *DHT) replyFindNode(addr *net.UDPAddr, r responseType) {
totalRecvFindNode.Add(1)
l4g.Trace(func() string {
x := hashDistance(InfoHash(r.A.Target), InfoHash(d.nodeId))
return fmt.Sprintf("DHT find_node. Host: %v , nodeId: %x , target ID: %x , distance to me: %x", addr, r.A.Id, r.A.Target, x)
})
node := InfoHash(r.A.Target)
r0 := map[string]interface{}{"id": d.nodeId}
reply := replyMessage{
T: r.T,
Y: "r",
R: r0,
}
// XXX we currently can't give out the peer contact. Probably requires
// processing announce_peer. XXX If there was a total match, that guy
// is the last.
neighbors := d.routingTable.lookup(node)
n := make([]string, 0, kNodes)
for _, r := range neighbors {
n = append(n, r.id+r.addressBinaryFormat)
}
l4g.Trace("replyFindNode: Nodes only. Giving %d", len(n))
reply.R["nodes"] = strings.Join(n, "")
sendMsg(d.conn, addr, reply)
}
func (d *DHT) replyPing(addr *net.UDPAddr, response responseType) {
l4g.Trace("DHT: reply ping => %v\n", addr)
reply := replyMessage{
T: response.T,
Y: "r",
R: map[string]interface{}{"id": d.nodeId},
}
sendMsg(d.conn, addr, reply)
}
// Process another node's response to a get_peers query. If the response
// contains peers, send them to the Torrent engine, our client, using the
// DHT.PeersRequestResults channel. If it contains closest nodes, query
// them if we still need it. Also announce ourselves as a peer for that node,
// unless we are in supernode mode.
func (d *DHT) processGetPeerResults(node *remoteNode, resp responseType) {
totalRecvGetPeersReply.Add(1)
query, _ := node.pendingQueries[resp.T]
if d.peerStore.hasLocalDownload(query.ih) {
d.announcePeer(node.address, query.ih, resp.R.Token)
}
if resp.R.Values != nil {
peers := make([]string, 0)
for _, peerContact := range resp.R.Values {
if ok := d.peerStore.addContact(query.ih, peerContact); ok {
peers = append(peers, peerContact)
}
}
if len(peers) > 0 {
// Finally, new peers.
result := map[InfoHash][]string{query.ih: peers}
totalPeers.Add(int64(len(peers)))
l4g.Info("DHT: processGetPeerResults, totalPeers: %v", totalPeers.String())
d.PeersRequestResults <- result
}
}
if resp.R.Nodes != "" {
for id, address := range parseNodesString(resp.R.Nodes) {
if d.peerStore.count(query.ih) >= d.numTargetPeers {
return
}
// If it's in our routing table already, ignore it.
_, addr, existed, err := d.routingTable.hostPortToNode(address)
if err != nil {
l4g.Trace("DHT error parsing get peers node: %v", err)
continue
}
if addr == node.address.String() {
// This smartass is probably trying to
// sniff the network, or attract a lot
// of traffic to itself. Ignore all
// their results.
totalSelfPromotions.Add(1)
continue
}
if existed {
l4g.Trace(func() string {
x := hashDistance(query.ih, InfoHash(node.id))
return fmt.Sprintf("DHT: DUPE node reference: %x@%v from %x@%v. Distance: %x.", id, address, node.id, node.address.String(), x)
})
totalDupes.Add(1)
} else {
// And it is actually new. Interesting.
l4g.Trace(func() string {
x := hashDistance(query.ih, InfoHash(node.id))
return fmt.Sprintf("DHT: Got new node reference: %x@%v from %x@%v. Distance: %x.", id, address, node.id, node.address.String(), x)
})
if _, err := d.routingTable.getOrCreateNode(id, addr); err == nil && d.peerStore.count(query.ih) < d.numTargetPeers {
d.getPeers(query.ih)
}
}
}
}
}
// Process another node's response to a find_node query.
func (d *DHT) processFindNodeResults(node *remoteNode, resp responseType) {
totalRecvFindNodeReply.Add(1)
query, _ := node.pendingQueries[resp.T]
if resp.R.Nodes != "" {
for id, address := range parseNodesString(resp.R.Nodes) {
_, addr, existed, err := d.routingTable.hostPortToNode(address)
if err != nil {
l4g.Trace("DHT error parsing node from find_find response: %v", err)
continue
}
// SelfPromotions are more common for find_node. They are
// happening even for router.bittorrent.com
if existed {
l4g.Trace(func() string {
x := hashDistance(query.ih, InfoHash(node.id))
return fmt.Sprintf("DHT: DUPE node reference, query %x: %x@%v from %x@%v. Distance: %x.", query.ih, id, address, node.id, addr, x)
})
totalDupes.Add(1)
} else {
l4g.Trace(func() string {
x := hashDistance(query.ih, InfoHash(node.id))
return fmt.Sprintf("DHT: Got new node reference, query %x: %x@%v from %x@%v. Distance: %x.", query.ih, id, address, node.id, addr, x)
})
if _, err := d.routingTable.getOrCreateNode(id, addr); err == nil {
// Using d.findNode() instead of d.findNodeFrom() ensures
// that only the closest neighbors are looked at.
d.findNode(string(query.ih))
}
}
}
}
}
func randNodeId() []byte {
b := make([]byte, 20)
if _, err := rand.Read(b); err != nil {
l4g.Exit("nodeId rand:", err)
}
return b
}
var (
expNodeIds = expvar.NewMap("nodeIds")
totalReachableNodes = expvar.NewInt("totalReachableNodes")
totalDupes = expvar.NewInt("totalDupes")
totalSelfPromotions = expvar.NewInt("totalSelfPromotions")
totalPeers = expvar.NewInt("totalPeers")
totalSentPing = expvar.NewInt("totalSentPing")
totalSentGetPeers = expvar.NewInt("totalSentGetPeers")
totalSentFindNode = expvar.NewInt("totalSentFindNode")
totalRecvGetPeers = expvar.NewInt("totalRecvGetPeers")
totalRecvGetPeersReply = expvar.NewInt("totalRecvGetPeersReply")
totalRecvPingReply = expvar.NewInt("totalRecvPingReply")
totalRecvFindNode = expvar.NewInt("totalRecvFindNode")
totalRecvFindNodeReply = expvar.NewInt("totalRecvFindNodeReply")
totalPacketsFromBlockedHosts = expvar.NewInt("totalPacketsFromBlockedHosts")
totalDroppedPackets = expvar.NewInt("totalDroppedPackets")
totalRecv = expvar.NewInt("totalRecv")
)