NodeStore.h 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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. EventBase_t* 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_Two* NodeStore_dumpTable(struct NodeStore* nodeStore, uint32_t index);
  78. struct Node_Link* NodeStore_linkForPath(struct NodeStore* nodeStore, uint64_t path);
  79. void NodeStore_unlinkNodes(struct NodeStore* nodeStore, struct Node_Link* link);
  80. /**
  81. * Get an outgoing link for a node.
  82. *
  83. * @param parent the node from which the link begins.
  84. * @param startLink the link to get the next link after, if NULL the first link from the parent
  85. * will be returned.
  86. * @return the next link from the parent of NULL if there are no more links.
  87. */
  88. struct Node_Link* NodeStore_nextLink(struct Node_Two* parent, struct Node_Link* startLink);
  89. /**
  90. * Get the first peer along a path.
  91. *
  92. * @param nodeStore
  93. * @param path the path to get the first peer along.
  94. * @param correctedPath if non-null, this will be set to the path from the resulting link to the
  95. * destination given by path. Calling this function iteratively, passing
  96. * the result of this back to path and passing the return value as
  97. * startingPoint will walk the path.
  98. * @param startingPoint if non-null, the starting point from which path begins, otherwise it will
  99. * be assumed to begin from the self-node.
  100. * @return the first link along the path or NULL if no such link is known.
  101. */
  102. struct Node_Link* NodeStore_firstHopInPath(struct NodeStore* nodeStore,
  103. uint64_t path,
  104. uint64_t* correctedPath,
  105. struct Node_Link* startingPoint);
  106. #define NodeStore_optimizePath_INVALID (~((uint64_t)0))
  107. uint64_t NodeStore_optimizePath(struct NodeStore* nodeStore, uint64_t path);
  108. /**
  109. * Get a route label for a given path through the network.
  110. *
  111. * @param nodeStore the store
  112. * @param pathToParent a label for getting to a node.
  113. * @param pathParentToChild the cannonicalized label for getting from the parent node to the child.
  114. * @return a path if all goes well, otherwise:
  115. * NodeStore_getRouteLabel_PARENT_NOT_FOUND if the path to the parent node does not
  116. * lead to a known node, or:
  117. * NodeStore_getRouteLabel_CHILD_NOT_FOUND if no peer could be found which links from that
  118. * path from the parent.
  119. */
  120. #define NodeStore_getRouteLabel_PARENT_NOT_FOUND ((~((uint64_t)0))-1)
  121. #define NodeStore_getRouteLabel_CHILD_NOT_FOUND ((~((uint64_t)0))-2)
  122. #define NodeStore_getRouteLabel__ERR_MIN ((~((uint64_t)0))-3)
  123. static inline bool NodeStore_getRouteLabel_ERR(uint64_t x)
  124. {
  125. return NodeStore_getRouteLabel__ERR_MIN <= x;
  126. }
  127. uint64_t NodeStore_getRouteLabel(struct NodeStore* nodeStore,
  128. uint64_t pathToParent,
  129. uint64_t pathParentToChild);
  130. /**
  131. * @return a human readable version of the error response from getRouteLabel or return NULL if
  132. * getRouteLabel succeeded.
  133. */
  134. char* NodeStore_getRouteLabel_strerror(uint64_t returnVal);
  135. /**
  136. * FIXME(arceliar): Documentation is out of date
  137. * Find the one best node using LinkStateNodeCollector. LinkStateNodeCollector prefers a
  138. * keyspace match (same address). It breaks ties by choosing the highest version node
  139. * (versions above it's own are considered the same as it's version). It breaks ties of the
  140. * above two by which node has non-zero reach and finally shortest label fragment wins.
  141. *
  142. * @param store the NodeStore
  143. * @param targetAddress the address used for comparing distance
  144. * @return the node w/ address closest to targetAddress or NULL if myAddress is closest
  145. */
  146. struct Node_Two* NodeStore_getBest(struct NodeStore* nodeStore, uint8_t targetAddress[16]);
  147. /**
  148. * Get direct peers of this node.
  149. * Will get peers with switch labels XOR close to the provided label up to max number.
  150. *
  151. * @param label will get peers whose labels are XOR close to this label.
  152. * @param max will not return more than this number of peers.
  153. * @param allocator for getting memory for the list.
  154. * @param store the nodestore.
  155. */
  156. struct NodeList* NodeStore_getPeers(uint64_t label,
  157. const uint32_t max,
  158. struct Allocator* allocator,
  159. struct NodeStore* store);
  160. /**
  161. * Get the best nodes for servicing a lookup.
  162. * These are returned in reverse order, from farthest to closest.
  163. *
  164. * @param store the store to get the nodes from.
  165. * @param targetAddress the address to get closest nodes for.
  166. * @param count the number of nodes to return.
  167. * @param versionOfRequestingNode the version of the node who asked for the list, no nodes will
  168. * be returned which are known to be incompatible with this version.
  169. * @param allocator the memory allocator to use for getting the memory to store the output.
  170. */
  171. struct NodeList* NodeStore_getClosestNodes(struct NodeStore* store,
  172. struct Address* targetAddress,
  173. const uint32_t count,
  174. uint32_t versionOfRequestingNode,
  175. struct Allocator* allocator);
  176. // Used to update cost when a ping/search response comes in
  177. void NodeStore_pathResponse(struct NodeStore* nodeStore, uint64_t path, uint64_t milliseconds);
  178. void NodeStore_pathTimeout(struct NodeStore* nodeStore, uint64_t path);
  179. /**
  180. * Remove all nodes who are reachable by this path.
  181. *
  182. * @param path the label part in host order.
  183. * @param store the node store.
  184. */
  185. //void NodeStore_brokenPath(uint64_t path, struct NodeStore* store);
  186. void NodeStore_brokenLink(struct NodeStore* nodeStore, uint64_t path, uint64_t pathAtErrorPoint);
  187. void NodeStore_disconnectedPeer(struct NodeStore* nodeStore, uint64_t path);
  188. struct Node_Two* NodeStore_getNextNode(struct NodeStore* nodeStore, struct Node_Two* lastNode);
  189. struct Node_Link* NodeStore_getNextLink(struct NodeStore* nodeStore, struct Node_Link* last);
  190. uint64_t NodeStore_timeSinceLastPing(struct NodeStore* nodeStore, struct Node_Two* node);
  191. // Used for DHT maintenance.
  192. #define NodeStore_bucketSize 4
  193. #define NodeStore_bucketNumber 128
  194. struct Address NodeStore_addrForBucket(struct Address* source, uint16_t bucket);
  195. uint16_t NodeStore_bucketForAddr(struct Address* source, struct Address* dest);
  196. struct NodeList* NodeStore_getNodesForBucket(struct NodeStore* nodeStore,
  197. struct Allocator* allocator,
  198. uint16_t bucket,
  199. const uint32_t count);
  200. bool NodeStore_getFullVerify(struct NodeStore* nodeStore);
  201. void NodeStore_setFullVerify(struct NodeStore* nodeStore, bool fullVerify);
  202. #endif