RumorMill.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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 (!Bits_memcmp(&rm->addresses[i], addr->ip6.bytes, Address_SEARCH_TARGET_SIZE)) {
  42. return true;
  43. }
  44. }
  45. return false;
  46. }
  47. static int getBadness(struct Address* badAddr, struct Address* selfAddr)
  48. {
  49. uint64_t xor = Endian_bigEndianToHost64(badAddr->ip6.longs.one_be ^ selfAddr->ip6.longs.one_be);
  50. return Bits_log2x64(xor) + Bits_log2x64(badAddr->path);
  51. }
  52. static struct Address* getWorst(struct RumorMill_pvt* rm)
  53. {
  54. struct Address* worst = NULL;
  55. int howBadIsTheWorst = 0;
  56. for (int i = 0; i < rm->pub.count; i++) {
  57. int howBad = getBadness(&rm->addresses[i], rm->selfAddr);
  58. if (howBad > howBadIsTheWorst) {
  59. howBadIsTheWorst = howBad;
  60. worst = &rm->addresses[i];
  61. }
  62. }
  63. Assert_true(worst);
  64. return worst;
  65. }
  66. void RumorMill_addNode(struct RumorMill* mill, struct Address* addr)
  67. {
  68. if (hasNode(mill, addr)) { return; } // Avoid duplicates
  69. struct RumorMill_pvt* rm = Identity_check((struct RumorMill_pvt*) mill);
  70. if (!Bits_memcmp(addr->key, rm->selfAddr->key, 32)) { return; }
  71. Address_getPrefix(addr);
  72. struct Address* replace;
  73. if (rm->pub.count < rm->capacity) {
  74. replace = &rm->addresses[rm->pub.count++];
  75. } else {
  76. replace = getWorst(rm);
  77. }
  78. Bits_memcpyConst(replace, addr, sizeof(struct Address));
  79. }
  80. bool RumorMill_getNode(struct RumorMill* mill, struct Address* output)
  81. {
  82. struct RumorMill_pvt* rm = Identity_check((struct RumorMill_pvt*) mill);
  83. if (!rm->pub.count) { return false; }
  84. Bits_memcpyConst(output, &rm->addresses[--rm->pub.count], sizeof(struct Address));
  85. return true;
  86. }
  87. struct RumorMill* RumorMill_new(struct Allocator* allocator, struct Address* selfAddr, int capacity)
  88. {
  89. struct Allocator* alloc = Allocator_child(allocator);
  90. Address_getPrefix(selfAddr);
  91. struct RumorMill_pvt* rm = Allocator_calloc(alloc, sizeof(struct RumorMill_pvt), 1);
  92. rm->addresses = Allocator_calloc(alloc, sizeof(struct Address), capacity);
  93. rm->capacity = capacity;
  94. rm->selfAddr = Allocator_clone(alloc, selfAddr);
  95. Identity_set(rm);
  96. return &rm->pub;
  97. }