Node.h 4.2 KB

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