NodeStore.h 9.4 KB

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