NodeStore.h 9.7 KB

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