NodeStore.h 9.0 KB

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