1
0

Node.h 4.5 KB

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