Node.h 4.4 KB

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