You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

680 lines
18 KiB

9 years ago
9 years ago
7 years ago
9 years ago
9 years ago
8 years ago
9 years ago
7 years ago
9 years ago
7 years ago
7 years ago
9 years ago
7 years ago
9 years ago
7 years ago
9 years ago
7 years ago
9 years ago
7 years ago
9 years ago
7 years ago
7 years ago
7 years ago
9 years ago
9 years ago
9 years ago
7 years ago
9 years ago
8 years ago
9 years ago
7 years ago
9 years ago
7 years ago
9 years ago
9 years ago
9 years ago
7 years ago
9 years ago
9 years ago
9 years ago
7 years ago
9 years ago
7 years ago
8 years ago
9 years ago
8 years ago
9 years ago
7 years ago
9 years ago
9 years ago
7 years ago
9 years ago
7 years ago
9 years ago
7 years ago
9 years ago
7 years ago
9 years ago
9 years ago
9 years ago
7 years ago
9 years ago
9 years ago
7 years ago
9 years ago
9 years ago
7 years ago
7 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
7 years ago
9 years ago
7 years ago
9 years ago
7 years ago
8 years ago
7 years ago
9 years ago
7 years ago
9 years ago
9 years ago
9 years ago
9 years ago
8 years ago
9 years ago
7 years ago
9 years ago
9 years ago
9 years ago
9 years ago
7 years ago
9 years ago
8 years ago
9 years ago
9 years ago
9 years ago
9 years ago
7 years ago
9 years ago
8 years ago
9 years ago
8 years ago
9 years ago
9 years ago
9 years ago
7 years ago
9 years ago
8 years ago
9 years ago
9 years ago
7 years ago
9 years ago
9 years ago
7 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
8 years ago
9 years ago
7 years ago
9 years ago
8 years ago
9 years ago
7 years ago
9 years ago
8 years ago
9 years ago
8 years ago
9 years ago
8 years ago
9 years ago
8 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
7 years ago
9 years ago
9 years ago
9 years ago
7 years ago
  1. // Modified for Tendermint
  2. // Originally Copyright (c) 2013-2014 Conformal Systems LLC.
  3. // https://github.com/conformal/btcd/blob/master/LICENSE
  4. package pex
  5. import (
  6. "crypto/sha256"
  7. "encoding/binary"
  8. "fmt"
  9. "math"
  10. "math/rand"
  11. "net"
  12. "sync"
  13. "time"
  14. crypto "github.com/tendermint/go-crypto"
  15. "github.com/tendermint/tendermint/p2p"
  16. cmn "github.com/tendermint/tmlibs/common"
  17. )
  18. const (
  19. bucketTypeNew = 0x01
  20. bucketTypeOld = 0x02
  21. )
  22. // AddrBook is an address book used for tracking peers
  23. // so we can gossip about them to others and select
  24. // peers to dial.
  25. // TODO: break this up?
  26. type AddrBook interface {
  27. cmn.Service
  28. // Add our own addresses so we don't later add ourselves
  29. AddOurAddress(*p2p.NetAddress)
  30. // Add and remove an address
  31. AddAddress(addr *p2p.NetAddress, src *p2p.NetAddress) error
  32. RemoveAddress(addr *p2p.NetAddress)
  33. // Do we need more peers?
  34. NeedMoreAddrs() bool
  35. // Pick an address to dial
  36. PickAddress(newBias int) *p2p.NetAddress
  37. // Mark address
  38. MarkGood(*p2p.NetAddress)
  39. MarkAttempt(*p2p.NetAddress)
  40. MarkBad(*p2p.NetAddress)
  41. // Send a selection of addresses to peers
  42. GetSelection() []*p2p.NetAddress
  43. // TODO: remove
  44. ListOfKnownAddresses() []*knownAddress
  45. // Persist to disk
  46. Save()
  47. }
  48. var _ AddrBook = (*addrBook)(nil)
  49. // addrBook - concurrency safe peer address manager.
  50. // Implements AddrBook.
  51. type addrBook struct {
  52. cmn.BaseService
  53. // immutable after creation
  54. filePath string
  55. routabilityStrict bool
  56. key string // random prefix for bucket placement
  57. // accessed concurrently
  58. mtx sync.Mutex
  59. rand *rand.Rand
  60. ourAddrs map[string]*p2p.NetAddress
  61. addrLookup map[p2p.ID]*knownAddress // new & old
  62. bucketsOld []map[string]*knownAddress
  63. bucketsNew []map[string]*knownAddress
  64. nOld int
  65. nNew int
  66. wg sync.WaitGroup
  67. }
  68. // NewAddrBook creates a new address book.
  69. // Use Start to begin processing asynchronous address updates.
  70. func NewAddrBook(filePath string, routabilityStrict bool) *addrBook {
  71. am := &addrBook{
  72. rand: rand.New(rand.NewSource(time.Now().UnixNano())), // TODO: seed from outside
  73. ourAddrs: make(map[string]*p2p.NetAddress),
  74. addrLookup: make(map[p2p.ID]*knownAddress),
  75. filePath: filePath,
  76. routabilityStrict: routabilityStrict,
  77. }
  78. am.init()
  79. am.BaseService = *cmn.NewBaseService(nil, "AddrBook", am)
  80. return am
  81. }
  82. // Initialize the buckets.
  83. // When modifying this, don't forget to update loadFromFile()
  84. func (a *addrBook) init() {
  85. a.key = crypto.CRandHex(24) // 24/2 * 8 = 96 bits
  86. // New addr buckets
  87. a.bucketsNew = make([]map[string]*knownAddress, newBucketCount)
  88. for i := range a.bucketsNew {
  89. a.bucketsNew[i] = make(map[string]*knownAddress)
  90. }
  91. // Old addr buckets
  92. a.bucketsOld = make([]map[string]*knownAddress, oldBucketCount)
  93. for i := range a.bucketsOld {
  94. a.bucketsOld[i] = make(map[string]*knownAddress)
  95. }
  96. }
  97. // OnStart implements Service.
  98. func (a *addrBook) OnStart() error {
  99. if err := a.BaseService.OnStart(); err != nil {
  100. return err
  101. }
  102. a.loadFromFile(a.filePath)
  103. // wg.Add to ensure that any invocation of .Wait()
  104. // later on will wait for saveRoutine to terminate.
  105. a.wg.Add(1)
  106. go a.saveRoutine()
  107. return nil
  108. }
  109. // OnStop implements Service.
  110. func (a *addrBook) OnStop() {
  111. a.BaseService.OnStop()
  112. }
  113. func (a *addrBook) Wait() {
  114. a.wg.Wait()
  115. }
  116. //-------------------------------------------------------
  117. // AddOurAddress one of our addresses.
  118. func (a *addrBook) AddOurAddress(addr *p2p.NetAddress) {
  119. a.mtx.Lock()
  120. defer a.mtx.Unlock()
  121. a.Logger.Info("Add our address to book", "addr", addr)
  122. a.ourAddrs[addr.String()] = addr
  123. }
  124. // AddAddress implements AddrBook - adds the given address as received from the given source.
  125. // NOTE: addr must not be nil
  126. func (a *addrBook) AddAddress(addr *p2p.NetAddress, src *p2p.NetAddress) error {
  127. a.mtx.Lock()
  128. defer a.mtx.Unlock()
  129. return a.addAddress(addr, src)
  130. }
  131. // RemoveAddress implements AddrBook - removes the address from the book.
  132. func (a *addrBook) RemoveAddress(addr *p2p.NetAddress) {
  133. a.mtx.Lock()
  134. defer a.mtx.Unlock()
  135. ka := a.addrLookup[addr.ID]
  136. if ka == nil {
  137. return
  138. }
  139. a.Logger.Info("Remove address from book", "addr", ka.Addr, "ID", ka.ID)
  140. a.removeFromAllBuckets(ka)
  141. }
  142. // NeedMoreAddrs implements AddrBook - returns true if there are not have enough addresses in the book.
  143. func (a *addrBook) NeedMoreAddrs() bool {
  144. return a.Size() < needAddressThreshold
  145. }
  146. // PickAddress implements AddrBook. It picks an address to connect to.
  147. // The address is picked randomly from an old or new bucket according
  148. // to the newBias argument, which must be between [0, 100] (or else is truncated to that range)
  149. // and determines how biased we are to pick an address from a new bucket.
  150. // PickAddress returns nil if the AddrBook is empty or if we try to pick
  151. // from an empty bucket.
  152. func (a *addrBook) PickAddress(newBias int) *p2p.NetAddress {
  153. a.mtx.Lock()
  154. defer a.mtx.Unlock()
  155. if a.size() == 0 {
  156. return nil
  157. }
  158. if newBias > 100 {
  159. newBias = 100
  160. }
  161. if newBias < 0 {
  162. newBias = 0
  163. }
  164. // Bias between new and old addresses.
  165. oldCorrelation := math.Sqrt(float64(a.nOld)) * (100.0 - float64(newBias))
  166. newCorrelation := math.Sqrt(float64(a.nNew)) * float64(newBias)
  167. // pick a random peer from a random bucket
  168. var bucket map[string]*knownAddress
  169. pickFromOldBucket := (newCorrelation+oldCorrelation)*a.rand.Float64() < oldCorrelation
  170. if (pickFromOldBucket && a.nOld == 0) ||
  171. (!pickFromOldBucket && a.nNew == 0) {
  172. return nil
  173. }
  174. // loop until we pick a random non-empty bucket
  175. for len(bucket) == 0 {
  176. if pickFromOldBucket {
  177. bucket = a.bucketsOld[a.rand.Intn(len(a.bucketsOld))]
  178. } else {
  179. bucket = a.bucketsNew[a.rand.Intn(len(a.bucketsNew))]
  180. }
  181. }
  182. // pick a random index and loop over the map to return that index
  183. randIndex := a.rand.Intn(len(bucket))
  184. for _, ka := range bucket {
  185. if randIndex == 0 {
  186. return ka.Addr
  187. }
  188. randIndex--
  189. }
  190. return nil
  191. }
  192. // MarkGood implements AddrBook - it marks the peer as good and
  193. // moves it into an "old" bucket.
  194. func (a *addrBook) MarkGood(addr *p2p.NetAddress) {
  195. a.mtx.Lock()
  196. defer a.mtx.Unlock()
  197. ka := a.addrLookup[addr.ID]
  198. if ka == nil {
  199. return
  200. }
  201. ka.markGood()
  202. if ka.isNew() {
  203. a.moveToOld(ka)
  204. }
  205. }
  206. // MarkAttempt implements AddrBook - it marks that an attempt was made to connect to the address.
  207. func (a *addrBook) MarkAttempt(addr *p2p.NetAddress) {
  208. a.mtx.Lock()
  209. defer a.mtx.Unlock()
  210. ka := a.addrLookup[addr.ID]
  211. if ka == nil {
  212. return
  213. }
  214. ka.markAttempt()
  215. }
  216. // MarkBad implements AddrBook. Currently it just ejects the address.
  217. // TODO: black list for some amount of time
  218. func (a *addrBook) MarkBad(addr *p2p.NetAddress) {
  219. a.RemoveAddress(addr)
  220. }
  221. // GetSelection implements AddrBook.
  222. // It randomly selects some addresses (old & new). Suitable for peer-exchange protocols.
  223. func (a *addrBook) GetSelection() []*p2p.NetAddress {
  224. a.mtx.Lock()
  225. defer a.mtx.Unlock()
  226. if a.size() == 0 {
  227. return nil
  228. }
  229. allAddr := make([]*p2p.NetAddress, a.size())
  230. i := 0
  231. for _, ka := range a.addrLookup {
  232. allAddr[i] = ka.Addr
  233. i++
  234. }
  235. numAddresses := cmn.MaxInt(
  236. cmn.MinInt(minGetSelection, len(allAddr)),
  237. len(allAddr)*getSelectionPercent/100)
  238. numAddresses = cmn.MinInt(maxGetSelection, numAddresses)
  239. // Fisher-Yates shuffle the array. We only need to do the first
  240. // `numAddresses' since we are throwing the rest.
  241. // XXX: What's the point of this if we already loop randomly through addrLookup ?
  242. for i := 0; i < numAddresses; i++ {
  243. // pick a number between current index and the end
  244. j := rand.Intn(len(allAddr)-i) + i
  245. allAddr[i], allAddr[j] = allAddr[j], allAddr[i]
  246. }
  247. // slice off the limit we are willing to share.
  248. return allAddr[:numAddresses]
  249. }
  250. // ListOfKnownAddresses returns the new and old addresses.
  251. func (a *addrBook) ListOfKnownAddresses() []*knownAddress {
  252. a.mtx.Lock()
  253. defer a.mtx.Unlock()
  254. addrs := []*knownAddress{}
  255. for _, addr := range a.addrLookup {
  256. addrs = append(addrs, addr.copy())
  257. }
  258. return addrs
  259. }
  260. //------------------------------------------------
  261. // Size returns the number of addresses in the book.
  262. func (a *addrBook) Size() int {
  263. a.mtx.Lock()
  264. defer a.mtx.Unlock()
  265. return a.size()
  266. }
  267. func (a *addrBook) size() int {
  268. return a.nNew + a.nOld
  269. }
  270. //----------------------------------------------------------
  271. // Save persists the address book to disk.
  272. func (a *addrBook) Save() {
  273. a.saveToFile(a.filePath) // thread safe
  274. }
  275. func (a *addrBook) saveRoutine() {
  276. defer a.wg.Done()
  277. saveFileTicker := time.NewTicker(dumpAddressInterval)
  278. out:
  279. for {
  280. select {
  281. case <-saveFileTicker.C:
  282. a.saveToFile(a.filePath)
  283. case <-a.Quit():
  284. break out
  285. }
  286. }
  287. saveFileTicker.Stop()
  288. a.saveToFile(a.filePath)
  289. a.Logger.Info("Address handler done")
  290. }
  291. //----------------------------------------------------------
  292. func (a *addrBook) getBucket(bucketType byte, bucketIdx int) map[string]*knownAddress {
  293. switch bucketType {
  294. case bucketTypeNew:
  295. return a.bucketsNew[bucketIdx]
  296. case bucketTypeOld:
  297. return a.bucketsOld[bucketIdx]
  298. default:
  299. cmn.PanicSanity("Should not happen")
  300. return nil
  301. }
  302. }
  303. // Adds ka to new bucket. Returns false if it couldn't do it cuz buckets full.
  304. // NOTE: currently it always returns true.
  305. func (a *addrBook) addToNewBucket(ka *knownAddress, bucketIdx int) bool {
  306. // Sanity check
  307. if ka.isOld() {
  308. a.Logger.Error(cmn.Fmt("Cannot add address already in old bucket to a new bucket: %v", ka))
  309. return false
  310. }
  311. addrStr := ka.Addr.String()
  312. bucket := a.getBucket(bucketTypeNew, bucketIdx)
  313. // Already exists?
  314. if _, ok := bucket[addrStr]; ok {
  315. return true
  316. }
  317. // Enforce max addresses.
  318. if len(bucket) > newBucketSize {
  319. a.Logger.Info("new bucket is full, expiring new")
  320. a.expireNew(bucketIdx)
  321. }
  322. // Add to bucket.
  323. bucket[addrStr] = ka
  324. // increment nNew if the peer doesnt already exist in a bucket
  325. if ka.addBucketRef(bucketIdx) == 1 {
  326. a.nNew++
  327. }
  328. // Add it to addrLookup
  329. a.addrLookup[ka.ID()] = ka
  330. return true
  331. }
  332. // Adds ka to old bucket. Returns false if it couldn't do it cuz buckets full.
  333. func (a *addrBook) addToOldBucket(ka *knownAddress, bucketIdx int) bool {
  334. // Sanity check
  335. if ka.isNew() {
  336. a.Logger.Error(cmn.Fmt("Cannot add new address to old bucket: %v", ka))
  337. return false
  338. }
  339. if len(ka.Buckets) != 0 {
  340. a.Logger.Error(cmn.Fmt("Cannot add already old address to another old bucket: %v", ka))
  341. return false
  342. }
  343. addrStr := ka.Addr.String()
  344. bucket := a.getBucket(bucketTypeOld, bucketIdx)
  345. // Already exists?
  346. if _, ok := bucket[addrStr]; ok {
  347. return true
  348. }
  349. // Enforce max addresses.
  350. if len(bucket) > oldBucketSize {
  351. return false
  352. }
  353. // Add to bucket.
  354. bucket[addrStr] = ka
  355. if ka.addBucketRef(bucketIdx) == 1 {
  356. a.nOld++
  357. }
  358. // Ensure in addrLookup
  359. a.addrLookup[ka.ID()] = ka
  360. return true
  361. }
  362. func (a *addrBook) removeFromBucket(ka *knownAddress, bucketType byte, bucketIdx int) {
  363. if ka.BucketType != bucketType {
  364. a.Logger.Error(cmn.Fmt("Bucket type mismatch: %v", ka))
  365. return
  366. }
  367. bucket := a.getBucket(bucketType, bucketIdx)
  368. delete(bucket, ka.Addr.String())
  369. if ka.removeBucketRef(bucketIdx) == 0 {
  370. if bucketType == bucketTypeNew {
  371. a.nNew--
  372. } else {
  373. a.nOld--
  374. }
  375. delete(a.addrLookup, ka.ID())
  376. }
  377. }
  378. func (a *addrBook) removeFromAllBuckets(ka *knownAddress) {
  379. for _, bucketIdx := range ka.Buckets {
  380. bucket := a.getBucket(ka.BucketType, bucketIdx)
  381. delete(bucket, ka.Addr.String())
  382. }
  383. ka.Buckets = nil
  384. if ka.BucketType == bucketTypeNew {
  385. a.nNew--
  386. } else {
  387. a.nOld--
  388. }
  389. delete(a.addrLookup, ka.ID())
  390. }
  391. //----------------------------------------------------------
  392. func (a *addrBook) pickOldest(bucketType byte, bucketIdx int) *knownAddress {
  393. bucket := a.getBucket(bucketType, bucketIdx)
  394. var oldest *knownAddress
  395. for _, ka := range bucket {
  396. if oldest == nil || ka.LastAttempt.Before(oldest.LastAttempt) {
  397. oldest = ka
  398. }
  399. }
  400. return oldest
  401. }
  402. // adds the address to a "new" bucket. if its already in one,
  403. // it only adds it probabilistically
  404. func (a *addrBook) addAddress(addr, src *p2p.NetAddress) error {
  405. if a.routabilityStrict && !addr.Routable() {
  406. return fmt.Errorf("Cannot add non-routable address %v", addr)
  407. }
  408. if _, ok := a.ourAddrs[addr.String()]; ok {
  409. // Ignore our own listener address.
  410. return fmt.Errorf("Cannot add ourselves with address %v", addr)
  411. }
  412. ka := a.addrLookup[addr.ID]
  413. if ka != nil {
  414. // Already old.
  415. if ka.isOld() {
  416. return nil
  417. }
  418. // Already in max new buckets.
  419. if len(ka.Buckets) == maxNewBucketsPerAddress {
  420. return nil
  421. }
  422. // The more entries we have, the less likely we are to add more.
  423. factor := int32(2 * len(ka.Buckets))
  424. if a.rand.Int31n(factor) != 0 {
  425. return nil
  426. }
  427. } else {
  428. ka = newKnownAddress(addr, src)
  429. }
  430. bucket := a.calcNewBucket(addr, src)
  431. added := a.addToNewBucket(ka, bucket)
  432. if !added {
  433. a.Logger.Info("Can't add new address, addr book is full", "address", addr, "total", a.size())
  434. }
  435. a.Logger.Info("Added new address", "address", addr, "total", a.size())
  436. return nil
  437. }
  438. // Make space in the new buckets by expiring the really bad entries.
  439. // If no bad entries are available we remove the oldest.
  440. func (a *addrBook) expireNew(bucketIdx int) {
  441. for addrStr, ka := range a.bucketsNew[bucketIdx] {
  442. // If an entry is bad, throw it away
  443. if ka.isBad() {
  444. a.Logger.Info(cmn.Fmt("expiring bad address %v", addrStr))
  445. a.removeFromBucket(ka, bucketTypeNew, bucketIdx)
  446. return
  447. }
  448. }
  449. // If we haven't thrown out a bad entry, throw out the oldest entry
  450. oldest := a.pickOldest(bucketTypeNew, bucketIdx)
  451. a.removeFromBucket(oldest, bucketTypeNew, bucketIdx)
  452. }
  453. // Promotes an address from new to old. If the destination bucket is full,
  454. // demote the oldest one to a "new" bucket.
  455. // TODO: Demote more probabilistically?
  456. func (a *addrBook) moveToOld(ka *knownAddress) {
  457. // Sanity check
  458. if ka.isOld() {
  459. a.Logger.Error(cmn.Fmt("Cannot promote address that is already old %v", ka))
  460. return
  461. }
  462. if len(ka.Buckets) == 0 {
  463. a.Logger.Error(cmn.Fmt("Cannot promote address that isn't in any new buckets %v", ka))
  464. return
  465. }
  466. // Remember one of the buckets in which ka is in.
  467. freedBucket := ka.Buckets[0]
  468. // Remove from all (new) buckets.
  469. a.removeFromAllBuckets(ka)
  470. // It's officially old now.
  471. ka.BucketType = bucketTypeOld
  472. // Try to add it to its oldBucket destination.
  473. oldBucketIdx := a.calcOldBucket(ka.Addr)
  474. added := a.addToOldBucket(ka, oldBucketIdx)
  475. if !added {
  476. // No room, must evict something
  477. oldest := a.pickOldest(bucketTypeOld, oldBucketIdx)
  478. a.removeFromBucket(oldest, bucketTypeOld, oldBucketIdx)
  479. // Find new bucket to put oldest in
  480. newBucketIdx := a.calcNewBucket(oldest.Addr, oldest.Src)
  481. added := a.addToNewBucket(oldest, newBucketIdx)
  482. // No space in newBucket either, just put it in freedBucket from above.
  483. if !added {
  484. added := a.addToNewBucket(oldest, freedBucket)
  485. if !added {
  486. a.Logger.Error(cmn.Fmt("Could not migrate oldest %v to freedBucket %v", oldest, freedBucket))
  487. }
  488. }
  489. // Finally, add to bucket again.
  490. added = a.addToOldBucket(ka, oldBucketIdx)
  491. if !added {
  492. a.Logger.Error(cmn.Fmt("Could not re-add ka %v to oldBucketIdx %v", ka, oldBucketIdx))
  493. }
  494. }
  495. }
  496. //---------------------------------------------------------------------
  497. // calculate bucket placements
  498. // doublesha256( key + sourcegroup +
  499. // int64(doublesha256(key + group + sourcegroup))%bucket_per_group ) % num_new_buckets
  500. func (a *addrBook) calcNewBucket(addr, src *p2p.NetAddress) int {
  501. data1 := []byte{}
  502. data1 = append(data1, []byte(a.key)...)
  503. data1 = append(data1, []byte(a.groupKey(addr))...)
  504. data1 = append(data1, []byte(a.groupKey(src))...)
  505. hash1 := doubleSha256(data1)
  506. hash64 := binary.BigEndian.Uint64(hash1)
  507. hash64 %= newBucketsPerGroup
  508. var hashbuf [8]byte
  509. binary.BigEndian.PutUint64(hashbuf[:], hash64)
  510. data2 := []byte{}
  511. data2 = append(data2, []byte(a.key)...)
  512. data2 = append(data2, a.groupKey(src)...)
  513. data2 = append(data2, hashbuf[:]...)
  514. hash2 := doubleSha256(data2)
  515. return int(binary.BigEndian.Uint64(hash2) % newBucketCount)
  516. }
  517. // doublesha256( key + group +
  518. // int64(doublesha256(key + addr))%buckets_per_group ) % num_old_buckets
  519. func (a *addrBook) calcOldBucket(addr *p2p.NetAddress) int {
  520. data1 := []byte{}
  521. data1 = append(data1, []byte(a.key)...)
  522. data1 = append(data1, []byte(addr.String())...)
  523. hash1 := doubleSha256(data1)
  524. hash64 := binary.BigEndian.Uint64(hash1)
  525. hash64 %= oldBucketsPerGroup
  526. var hashbuf [8]byte
  527. binary.BigEndian.PutUint64(hashbuf[:], hash64)
  528. data2 := []byte{}
  529. data2 = append(data2, []byte(a.key)...)
  530. data2 = append(data2, a.groupKey(addr)...)
  531. data2 = append(data2, hashbuf[:]...)
  532. hash2 := doubleSha256(data2)
  533. return int(binary.BigEndian.Uint64(hash2) % oldBucketCount)
  534. }
  535. // Return a string representing the network group of this address.
  536. // This is the /16 for IPv4, the /32 (/36 for he.net) for IPv6, the string
  537. // "local" for a local address and the string "unroutable" for an unroutable
  538. // address.
  539. func (a *addrBook) groupKey(na *p2p.NetAddress) string {
  540. if a.routabilityStrict && na.Local() {
  541. return "local"
  542. }
  543. if a.routabilityStrict && !na.Routable() {
  544. return "unroutable"
  545. }
  546. if ipv4 := na.IP.To4(); ipv4 != nil {
  547. return (&net.IPNet{IP: na.IP, Mask: net.CIDRMask(16, 32)}).String()
  548. }
  549. if na.RFC6145() || na.RFC6052() {
  550. // last four bytes are the ip address
  551. ip := net.IP(na.IP[12:16])
  552. return (&net.IPNet{IP: ip, Mask: net.CIDRMask(16, 32)}).String()
  553. }
  554. if na.RFC3964() {
  555. ip := net.IP(na.IP[2:7])
  556. return (&net.IPNet{IP: ip, Mask: net.CIDRMask(16, 32)}).String()
  557. }
  558. if na.RFC4380() {
  559. // teredo tunnels have the last 4 bytes as the v4 address XOR
  560. // 0xff.
  561. ip := net.IP(make([]byte, 4))
  562. for i, byte := range na.IP[12:16] {
  563. ip[i] = byte ^ 0xff
  564. }
  565. return (&net.IPNet{IP: ip, Mask: net.CIDRMask(16, 32)}).String()
  566. }
  567. // OK, so now we know ourselves to be a IPv6 address.
  568. // bitcoind uses /32 for everything, except for Hurricane Electric's
  569. // (he.net) IP range, which it uses /36 for.
  570. bits := 32
  571. heNet := &net.IPNet{IP: net.ParseIP("2001:470::"),
  572. Mask: net.CIDRMask(32, 128)}
  573. if heNet.Contains(na.IP) {
  574. bits = 36
  575. }
  576. return (&net.IPNet{IP: na.IP, Mask: net.CIDRMask(bits, 128)}).String()
  577. }
  578. // doubleSha256 calculates sha256(sha256(b)) and returns the resulting bytes.
  579. func doubleSha256(b []byte) []byte {
  580. hasher := sha256.New()
  581. hasher.Write(b) // nolint: errcheck, gas
  582. sum := hasher.Sum(nil)
  583. hasher.Reset()
  584. hasher.Write(sum) // nolint: errcheck, gas
  585. return hasher.Sum(nil)
  586. }