RumorMill.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /* vim: set expandtab ts=4 sw=4: */
  2. /*
  3. * You may redistribute this program and/or modify it under the terms of
  4. * the GNU General Public License as published by the Free Software Foundation,
  5. * either version 3 of the License, or (at your option) any later version.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. #include "dht/dhtcore/RumorMill.h"
  16. #include "dht/Address.h"
  17. #include "memory/Allocator.h"
  18. #include "util/Identity.h"
  19. #include "util/Assert.h"
  20. #include "util/Bits.h"
  21. /**
  22. * The rumor mill is for new nodes which have been discovered by search and getPeers requests
  23. * but we have never actually communicated with them so we are not sure if they exist.
  24. *
  25. * More importantly, we *cannot* link them into the nodeStore tree because we are not sure of
  26. * their encoding scheme.
  27. */
  28. struct RumorMill_pvt
  29. {
  30. struct RumorMill pub;
  31. struct Address* selfAddr;
  32. struct Address* addresses;
  33. int count;
  34. int capacity;
  35. Identity
  36. };
  37. static inline bool hasNode(struct RumorMill* mill, struct Address* addr)
  38. {
  39. struct RumorMill_pvt* rm = Identity_check((struct RumorMill_pvt*) mill);
  40. for (int i = 0; i < rm->pub.count; i++) {
  41. if (rm->addresses[i].path == addr->path ||
  42. !Bits_memcmp(&rm->addresses[i], addr->ip6.bytes, Address_SEARCH_TARGET_SIZE)) {
  43. return true;
  44. }
  45. }
  46. return false;
  47. }
  48. static int getBadness(struct Address* badAddr, struct Address* selfAddr)
  49. {
  50. uint64_t xor = Endian_bigEndianToHost64(badAddr->ip6.longs.one_be ^ selfAddr->ip6.longs.one_be);
  51. return Bits_log2x64(xor) + Bits_log2x64(badAddr->path);
  52. }
  53. static struct Address* getWorst(struct RumorMill_pvt* rm)
  54. {
  55. struct Address* worst = NULL;
  56. int howBadIsTheWorst = 0;
  57. for (int i = 0; i < rm->pub.count; i++) {
  58. int howBad = getBadness(&rm->addresses[i], rm->selfAddr);
  59. if (howBad > howBadIsTheWorst) {
  60. howBadIsTheWorst = howBad;
  61. worst = &rm->addresses[i];
  62. }
  63. }
  64. Assert_ifParanoid(worst);
  65. return worst;
  66. }
  67. static struct Address* getBest(struct RumorMill_pvt* rm)
  68. {
  69. if (rm->pub.count == 0) { return NULL; }
  70. struct Address* best = NULL;
  71. int howBadIsTheBest = -1;
  72. for (int i = 0; i < rm->pub.count; i++) {
  73. int howBad = getBadness(&rm->addresses[i], rm->selfAddr);
  74. if (howBad < howBadIsTheBest || !best) {
  75. howBadIsTheBest = howBad;
  76. best = &rm->addresses[i];
  77. }
  78. }
  79. Assert_true(best);
  80. return best;
  81. }
  82. void RumorMill_addNode(struct RumorMill* mill, struct Address* addr)
  83. {
  84. if (hasNode(mill, addr)) { return; } // Avoid duplicates
  85. struct RumorMill_pvt* rm = Identity_check((struct RumorMill_pvt*) mill);
  86. if (!Bits_memcmp(addr->key, rm->selfAddr->key, 32)) { return; }
  87. Address_getPrefix(addr);
  88. struct Address* replace;
  89. if (rm->pub.count < rm->capacity) {
  90. replace = &rm->addresses[rm->pub.count++];
  91. } else {
  92. replace = getWorst(rm);
  93. }
  94. Bits_memcpyConst(replace, addr, sizeof(struct Address));
  95. }
  96. bool RumorMill_getNode(struct RumorMill* mill, struct Address* output)
  97. {
  98. struct RumorMill_pvt* rm = Identity_check((struct RumorMill_pvt*) mill);
  99. if (!rm->pub.count) { return false; }
  100. struct Address temp;
  101. struct Address* best = getBest(rm);
  102. Bits_memcpyConst(&temp, &rm->addresses[--rm->pub.count], sizeof(struct Address));
  103. Bits_memcpyConst(output, best, sizeof(struct Address));
  104. Bits_memcpyConst(best, &temp, sizeof(struct Address));
  105. return true;
  106. }
  107. struct RumorMill* RumorMill_new(struct Allocator* allocator, struct Address* selfAddr, int capacity)
  108. {
  109. struct Allocator* alloc = Allocator_child(allocator);
  110. Address_getPrefix(selfAddr);
  111. struct RumorMill_pvt* rm = Allocator_calloc(alloc, sizeof(struct RumorMill_pvt), 1);
  112. rm->addresses = Allocator_calloc(alloc, sizeof(struct Address), capacity);
  113. rm->capacity = capacity;
  114. rm->selfAddr = Allocator_clone(alloc, selfAddr);
  115. Identity_set(rm);
  116. return &rm->pub;
  117. }