Node.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. #ifndef Node_H
  16. #define Node_H
  17. #ifdef SUBNODE
  18. #error "this file should not be included in subnode"
  19. #endif
  20. #include "dht/Address.h"
  21. #include "switch/EncodingScheme.h"
  22. #include "memory/Allocator.h"
  23. #include "util/Identity.h"
  24. #include "util/Linker.h"
  25. Linker_require("dht/dhtcore/Node.c")
  26. struct Node_Link;
  27. struct Node_Two
  28. {
  29. /**
  30. * The cost of the node (how small/slow/far it is).
  31. * DO NOT ALTER THIS OUTSIDE OF NODESTORE
  32. */
  33. uint64_t cost_pvt;
  34. /** This is used to mark/sweep nodes in getWorstNode(), it's meaningless otherwise. */
  35. uint32_t marked;
  36. // true if this node is flagged do-not-disturb
  37. int dnd;
  38. /** Time the node was last pinged, *not* reset on path changes. */
  39. uint64_t timeLastPinged;
  40. /** The address of the node. */
  41. struct Address address;
  42. /** The encoding method used by this node. */
  43. struct EncodingScheme* encodingScheme;
  44. /**
  45. * Peers of this node for which we know the forward direction.
  46. * Use RB_NFIND(PeerRBTree, node->peerTree, struct type* elm)
  47. */
  48. struct PeerRBTree {
  49. struct Node_Link* rbh_root;
  50. } peerTree;
  51. /** Used for freeing the links associated with this node. */
  52. struct Node_Link* reversePeers;
  53. /** The best link for getting to this node. */
  54. struct Node_Link* bestParent_pvt;
  55. /** Used by nodeStore's RBTree of nodes by address. */
  56. struct {
  57. struct Node_Two* rbe_left;
  58. struct Node_Two* rbe_right;
  59. struct Node_Two* rbe_parent;
  60. int rbe_color;
  61. } nodeTree;
  62. struct Allocator* alloc;
  63. Identity
  64. };
  65. /**
  66. * A link represents a link between two nodes.
  67. * Links are unidirectional because deriving the inverse of a route is non-trivial.
  68. * (it cannot be calculated)
  69. */
  70. struct Node_Link
  71. {
  72. /** Used by the parent's RBTree of links. */
  73. struct {
  74. struct Node_Link* rbe_left;
  75. struct Node_Link* rbe_right;
  76. struct Node_Link* rbe_parent;
  77. int rbe_color;
  78. } peerTree;
  79. /**
  80. * The Encoding Form number which is used to represent the first director in the path from
  81. * child to parent.
  82. */
  83. int inverseLinkEncodingFormNumber;
  84. /**
  85. * The quality of the link between parent and child,
  86. * between 0 (perfect) and 0xFFFFFFFF (intolerable).
  87. */
  88. uint32_t linkCost;
  89. /** The time this link was last seen carrying traffic. (Currently limited to ping traffic.) */
  90. uint64_t timeLastSeen;
  91. /** The parent of this peer, this is where the root of the RBTree is. */
  92. struct Node_Two* parent;
  93. /** The child of this link. */
  94. struct Node_Two* child;
  95. /**
  96. * The next link which points to the same child.
  97. * For each child there are many links pointing to it,
  98. * they are represented here as a linked list.
  99. */
  100. struct Node_Link* nextPeer;
  101. /** Used internally by NodeStore for creating a list used for splitting links. */
  102. struct Node_Link* nextInSplitList;
  103. /**
  104. * The label which would be used to reach the child from the parent.
  105. * This label is in a cannonical state and must be altered so that the first Director uses
  106. * at least as many bits as are required to reach the grandparent from the parent
  107. * in the reverse direction.
  108. */
  109. uint64_t cannonicalLabel;
  110. /** The path which the incoming packet followed when this node was discovered. */
  111. uint64_t discoveredPath;
  112. Identity
  113. };
  114. static inline uint64_t Node_getCost(struct Node_Two* node)
  115. {
  116. return node->cost_pvt;
  117. }
  118. static inline struct Node_Link* Node_getBestParent(struct Node_Two* node)
  119. {
  120. return node->bestParent_pvt;
  121. }
  122. bool Node_isAncestorOf(struct Node_Two* ancestor, struct Node_Two* child);
  123. void Node_setParentCostAndPath(struct Node_Two* node,
  124. struct Node_Link* bestParent,
  125. uint64_t cost,
  126. uint64_t path);
  127. bool Node_isOneHopLink(struct Node_Link* link);
  128. #endif