NodeStore.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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 NodeStore_H
  16. #define NodeStore_H
  17. #include "dht/Address.h"
  18. #include "dht/dhtcore/Node.h"
  19. #include "dht/dhtcore/RumorMill.h"
  20. #include "util/log/Log.h"
  21. #include "memory/Allocator.h"
  22. #include "switch/EncodingScheme.h"
  23. #include "util/Linker.h"
  24. Linker_require("dht/dhtcore/NodeStore.c")
  25. #include <stdint.h>
  26. #include <stdbool.h>
  27. struct NodeStore
  28. {
  29. struct Address* selfAddress;
  30. struct Node_Two* selfNode;
  31. int nodeCount;
  32. int nodeCapacity;
  33. int linkCount;
  34. int linkCapacity;
  35. };
  36. #define NodeStore_DEFAULT_NODE_CAPACITY 128
  37. #define NodeStore_DEFAULT_LINK_CAPACITY 1024
  38. /**
  39. * Create a new NodeStore.
  40. *
  41. * @param myAddress the address for this DHT node.
  42. * @param allocator the allocator to allocate storage space for this NodeStore.
  43. * @param logger the means for this node store to log.
  44. */
  45. struct NodeStore* NodeStore_new(struct Address* myAddress,
  46. struct Allocator* allocator,
  47. struct Log* logger,
  48. struct RumorMill* renumberMill);
  49. /**
  50. * Discover a new node (or rediscover an existing one).
  51. *
  52. * @param nodeStore the store
  53. * @param addr the address of the new node
  54. * @param reachDiff the amount to credit this node
  55. * @param scheme the encoding scheme used by this node.
  56. * @param encodingFormNumber the number of the smallest possible encoding form for to encoding
  57. * the interface number through which this message came.
  58. * @param reach the quality of the path to the new node
  59. */
  60. struct Node_Link* NodeStore_discoverNode(struct NodeStore* nodeStore,
  61. struct Address* addr,
  62. struct EncodingScheme* scheme,
  63. int inverseLinkEncodingFormNumber,
  64. uint64_t milliseconds);
  65. struct Node_Two* NodeStore_nodeForAddr(struct NodeStore* nodeStore, uint8_t addr[16]);
  66. struct Node_Two* NodeStore_closestNode(struct NodeStore* nodeStore, uint64_t path);
  67. struct Node_Link* NodeStore_linkForPath(struct NodeStore* nodeStore, uint64_t path);
  68. void NodeStore_unlinkNodes(struct NodeStore* nodeStore, struct Node_Link* link);
  69. /**
  70. * Get an outgoing link for a node.
  71. *
  72. * @param parent the node from which the link begins.
  73. * @param startLink the link to get the next link after, if NULL the first link from the parent
  74. * will be returned.
  75. * @return the next link from the parent of NULL if there are no more links.
  76. */
  77. struct Node_Link* NodeStore_nextLink(struct Node_Two* parent, struct Node_Link* startLink);
  78. /**
  79. * Get the first peer along a path.
  80. *
  81. * @param nodeStore
  82. * @param path the path to get the first peer along.
  83. * @param correctedPath if non-null, this will be set to the path from the resulting link to the
  84. * destination given by path. Calling this function iteratively, passing
  85. * the result of this back to path and passing the return value as
  86. * startingPoint will walk the path.
  87. * @param startingPoint if non-null, the starting point from which path begins, otherwise it will
  88. * be assumed to begin from the self-node.
  89. * @return the first link along the path or NULL if no such link is known.
  90. */
  91. struct Node_Link* NodeStore_firstHopInPath(struct NodeStore* nodeStore,
  92. uint64_t path,
  93. uint64_t* correctedPath,
  94. struct Node_Link* startingPoint);
  95. #define NodeStore_optimizePath_INVALID (~((uint64_t)0))
  96. uint64_t NodeStore_optimizePath(struct NodeStore* nodeStore, uint64_t path);
  97. /**
  98. * Get a route label for a given path through the network.
  99. *
  100. * @param nodeStore the store
  101. * @param pathToParent a label for getting to a node.
  102. * @param pathParentToChild the cannonicalized label for getting from the parent node to the child.
  103. * @return a path if all goes well, otherwise:
  104. * NodeStore_getRouteLabel_PARENT_NOT_FOUND if the path to the parent node does not
  105. * lead to a known node, or:
  106. * NodeStore_getRouteLabel_CHILD_NOT_FOUND if no peer could be found which links from that
  107. * path from the parent.
  108. */
  109. #define NodeStore_getRouteLabel_PARENT_NOT_FOUND ((~((uint64_t)0))-1)
  110. #define NodeStore_getRouteLabel_CHILD_NOT_FOUND ((~((uint64_t)0))-2)
  111. uint64_t NodeStore_getRouteLabel(struct NodeStore* nodeStore,
  112. uint64_t pathToParent,
  113. uint64_t pathParentToChild);
  114. /**
  115. * @return a human readable version of the error response from getRouteLabel or return NULL if
  116. * getRouteLabel succeeded.
  117. */
  118. char* NodeStore_getRouteLabel_strerror(uint64_t returnVal);
  119. /**
  120. * Find the one best node using LinkStateNodeCollector. LinkStateNodeCollector prefers a
  121. * keyspace match (same address). It breaks ties by choosing the highest version node
  122. * (versions above it's own are considered the same as it's version). It breaks ties of the
  123. * above two by which node has non-zero reach and finally shortest label fragment wins.
  124. *
  125. * @param targetAddress the address used for comparing distance
  126. * @param store the NodeStore
  127. * @return the node w/ address closest to targetAddress or NULL if myAddress is closest
  128. */
  129. struct Node_Two* NodeStore_getBest(struct Address* targetAddress, struct NodeStore* store);
  130. /**
  131. * Get direct peers of this node.
  132. * Will get peers with switch labels XOR close to the provided label up to max number.
  133. *
  134. * @param label will get peers whose labels are XOR close to this label.
  135. * @param max will not return more than this number of peers.
  136. * @param allocator for getting memory for the list.
  137. * @param store the nodestore.
  138. */
  139. struct NodeList* NodeStore_getPeers(uint64_t label,
  140. const uint32_t max,
  141. struct Allocator* allocator,
  142. struct NodeStore* store);
  143. /**
  144. * Get the best nodes for servicing a lookup.
  145. * These are returned in reverse order, from farthest to closest.
  146. *
  147. * @param store the store to get the nodes from.
  148. * @param targetAddress the address to get closest nodes for.
  149. * @param count the number of nodes to return.
  150. * @param versionOfRequestingNode the version of the node who asked for the list, no nodes will
  151. * be returned which are known to be incompatible with this version.
  152. * @param allocator the memory allocator to use for getting the memory to store the output.
  153. */
  154. struct NodeList* NodeStore_getClosestNodes(struct NodeStore* store,
  155. struct Address* targetAddress,
  156. const uint32_t count,
  157. uint32_t versionOfRequestingNode,
  158. struct Allocator* allocator);
  159. // Used to update reach when a ping/search response comes in
  160. void NodeStore_pathResponse(struct NodeStore* nodeStore, uint64_t path, uint64_t milliseconds);
  161. void NodeStore_pathTimeout(struct NodeStore* nodeStore, uint64_t path);
  162. /**
  163. * Remove all nodes who are reachable by this path.
  164. *
  165. * @param path the label part in host order.
  166. * @param store the node store.
  167. */
  168. void NodeStore_brokenPath(uint64_t path, struct NodeStore* store);
  169. /**
  170. * Dump the table, one node at a time.
  171. */
  172. struct Node_Two* NodeStore_dumpTable(struct NodeStore* store, uint32_t index);
  173. #endif