LinkStateNodeCollector.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. #ifndef LinkStateNodeCollector_H
  16. #define LinkStateNodeCollector_H
  17. #include "dht/Address.h"
  18. #include "dht/dhtcore/Node.h"
  19. #include "dht/dhtcore/NodeHeader.h"
  20. #include "dht/dhtcore/NodeCollector.h"
  21. #include "util/log/Log.h"
  22. #include "util/version/Version.h"
  23. #include "memory/Allocator.h"
  24. #include <stdbool.h>
  25. /**
  26. * NOTE: It's critical that LinkStateNodeCollector is ALWAYS used with allowNodesFartherThanUs true.
  27. * Filter a node through the collector.
  28. * If this node is better than any one of the ones known to the collector, it will be collected.
  29. *
  30. * @param header the header of the node to add.
  31. * @param body the node which is used in case the prefix is an exact match and it needs to
  32. * look at more bits.
  33. * @param collector the collector to filter the node through.
  34. */
  35. static inline void LinkStateNodeCollector_addNode(struct NodeHeader* header,
  36. struct Node* body,
  37. struct NodeCollector* collector)
  38. {
  39. uint32_t nodeDistance = header->addressPrefix ^ collector->targetPrefix;
  40. // This is a hack because we don't really care about
  41. // beyond the first 4 bytes unless it's a match.
  42. if (nodeDistance == 0
  43. && Bits_memcmp(body->address.ip6.bytes,
  44. collector->targetAddress,
  45. Address_SEARCH_TARGET_SIZE) != 0)
  46. {
  47. Log_debug(collector->logger, "Increasing distance because addr is not exact match.\n");
  48. nodeDistance++;
  49. }
  50. struct NodeCollector_Element* nodes = collector->nodes;
  51. // Check that it's not farther from the target than we are...
  52. if (nodeDistance < collector->thisNodeDistance) {
  53. uint64_t value = 0;
  54. #define LinkStateNodeCollector_getValue(value, header, body, nodeDistance) \
  55. if (value == 0 && header->reach > 0) { \
  56. uint8_t pathLength = Bits_log2x64(body->address.path); \
  57. value = (header->reach / pathLength) + (64 - pathLength); \
  58. }
  59. // 0 distance (match) always wins,
  60. // If both have 0 distance or neither have 0 distance, highest version wins.
  61. // If both have same version, highest reach wins.
  62. uint32_t i;
  63. uint32_t match = 0;
  64. for (i = 0; i < collector->capacity; i++) {
  65. if (!nodes[i].body) {
  66. // no node here so accept this one into this position.
  67. continue;
  68. }
  69. if ((nodes[i].distance == 0) != (nodeDistance == 0)) {
  70. if (nodeDistance != 0) {
  71. // This node is not a match and the one in the collector is so reject.
  72. break;
  73. } else {
  74. // This node is a match and the one in the collector isn't so replace.
  75. continue;
  76. }
  77. }
  78. // Favor nodes which are of newer version but only if they are older than us.
  79. // This is to improve connectivity by forwarding through good nodes while
  80. // avoiding placing undue load on nodes which have updated to a brand new version.
  81. if (nodes[i].body->version < Version_CURRENT_PROTOCOL) {
  82. if (nodes[i].body->version > body->version) {
  83. // This node is older than the stored node so reject
  84. break;
  85. } else if (nodes[i].body->version < body->version) {
  86. // This node is newer than the stored node so accept
  87. continue;
  88. }
  89. // Same version so fall through
  90. }
  91. // Get the "value" of the node.
  92. LinkStateNodeCollector_getValue(value, header, body, nodeDistance);
  93. // If it's less than the value of the stored node then reject
  94. if (value < nodes[i].value) {
  95. break;
  96. }
  97. // If this is another route to the same node, replace it rather than inserting
  98. // separatey.
  99. if (i > 0 && !Bits_memcmp(&body->address.ip6, &nodes[i].body->address.ip6, 16)) {
  100. match = i + 1;
  101. }
  102. }
  103. if (i > 0) {
  104. if (match > 0) {
  105. i = match;
  106. } else if (i > 1) {
  107. Bits_memmove(nodes, &nodes[1], (i - 1) * sizeof(struct NodeCollector_Element));
  108. }
  109. nodes[i - 1].node = header;
  110. nodes[i - 1].body = body;
  111. LinkStateNodeCollector_getValue(value, header, body, nodeDistance);
  112. nodes[i - 1].value = value;
  113. nodes[i - 1].distance = nodeDistance;
  114. }
  115. #undef LinkStateNodeCollector_getValue
  116. }
  117. }
  118. #endif