Node.h 4.6 KB

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