RumorMill.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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 <https://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. #include "util/Endian.h"
  22. #include "util/Defined.h"
  23. /**
  24. * The rumor mill is for new nodes which have been discovered by search and getPeers requests
  25. * but we have never actually communicated with them so we are not sure if they exist.
  26. *
  27. * More importantly, we *cannot* link them into the nodeStore tree because we are not sure of
  28. * their encoding scheme.
  29. */
  30. struct RumorMill_pvt
  31. {
  32. struct RumorMill pub;
  33. struct Address* selfAddr;
  34. int capacity;
  35. struct Log* log;
  36. Identity
  37. };
  38. static int getBadness(struct Address* badAddr, struct Address* selfAddr)
  39. {
  40. uint64_t xor = Endian_bigEndianToHost64(badAddr->ip6.longs.one_be ^ selfAddr->ip6.longs.one_be);
  41. return Bits_log2x64(xor) + Bits_log2x64(badAddr->path);
  42. }
  43. static struct Address* getWorst(struct RumorMill_pvt* rm)
  44. {
  45. struct Address* worst = NULL;
  46. int howBadIsTheWorst = 0;
  47. for (int i = 0; i < rm->pub.count; i++) {
  48. int howBad = getBadness(&rm->pub.addresses[i], rm->selfAddr);
  49. if (howBad > howBadIsTheWorst) {
  50. howBadIsTheWorst = howBad;
  51. worst = &rm->pub.addresses[i];
  52. }
  53. }
  54. Assert_true(worst);
  55. return worst;
  56. }
  57. static struct Address* getBest(struct RumorMill_pvt* rm)
  58. {
  59. if (rm->pub.count == 0) { return NULL; }
  60. struct Address* best = NULL;
  61. int howBadIsTheBest = -1;
  62. for (int i = 0; i < rm->pub.count; i++) {
  63. int howBad = getBadness(&rm->pub.addresses[i], rm->selfAddr);
  64. if (howBad < howBadIsTheBest || !best) {
  65. howBadIsTheBest = howBad;
  66. best = &rm->pub.addresses[i];
  67. }
  68. }
  69. Assert_true(best);
  70. return best;
  71. }
  72. void RumorMill__addNode(struct RumorMill* mill, struct Address* addr, const char* file, int line)
  73. {
  74. struct RumorMill_pvt* rm = Identity_check((struct RumorMill_pvt*) mill);
  75. Address_getPrefix(addr);
  76. Assert_true(addr->protocolVersion);
  77. for (int i = 0; i < rm->pub.count; i++) {
  78. if (rm->pub.addresses[i].path == addr->path &&
  79. !Bits_memcmp(rm->pub.addresses[i].key, addr->key, 32))
  80. {
  81. return;
  82. }
  83. }
  84. if (!Bits_memcmp(addr->key, rm->selfAddr->key, 32)) { return; }
  85. struct Address* replace;
  86. if (rm->pub.count < rm->capacity) {
  87. replace = &rm->pub.addresses[rm->pub.count++];
  88. } else {
  89. replace = getWorst(rm);
  90. }
  91. Bits_memcpy(replace, addr, sizeof(struct Address));
  92. if (Defined(Log_DEBUG)) {
  93. uint8_t addrStr[60];
  94. Address_print(addrStr, addr);
  95. Log_debug(rm->log, "[%s] addNode(%s) count[%d] from [%s:%d]",
  96. rm->pub.name, addrStr, rm->pub.count, file, line);
  97. }
  98. }
  99. bool RumorMill_getNode(struct RumorMill* mill, struct Address* output)
  100. {
  101. struct RumorMill_pvt* rm = Identity_check((struct RumorMill_pvt*) mill);
  102. if (!rm->pub.count) { return false; }
  103. struct Address* best = getBest(rm);
  104. if (output) {
  105. Bits_memcpy(output, best, sizeof(struct Address));
  106. }
  107. rm->pub.count--;
  108. if (&rm->pub.addresses[rm->pub.count] != best) {
  109. Bits_memcpy(best, &rm->pub.addresses[rm->pub.count], sizeof(struct Address));
  110. }
  111. return true;
  112. }
  113. struct RumorMill* RumorMill_new(struct Allocator* allocator,
  114. struct Address* selfAddr,
  115. int capacity,
  116. struct Log* log,
  117. const char* name)
  118. {
  119. struct Allocator* alloc = Allocator_child(allocator);
  120. Address_getPrefix(selfAddr);
  121. struct RumorMill_pvt* rm = Allocator_calloc(alloc, sizeof(struct RumorMill_pvt), 1);
  122. rm->pub.addresses = Allocator_calloc(alloc, sizeof(struct Address), capacity);
  123. rm->capacity = capacity;
  124. rm->selfAddr = Allocator_clone(alloc, selfAddr);
  125. rm->log = log;
  126. rm->pub.name = name;
  127. Identity_set(rm);
  128. return &rm->pub;
  129. }