NodeStore.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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 "util/log/Log.h"
  20. #include "memory/Allocator.h"
  21. #include "switch/EncodingScheme.h"
  22. #include "util/Linker.h"
  23. Linker_require("dht/dhtcore/NodeStore.c")
  24. #include <stdint.h>
  25. #include <stdbool.h>
  26. struct NodeStore
  27. {
  28. struct Address* selfAddress;
  29. struct Node_Two* selfNode;
  30. };
  31. /**
  32. * Create a new NodeStore.
  33. *
  34. * @param myAddress the address for this DHT node.
  35. * @param capacity the number of nodes which this store can hold.
  36. * @param allocator the allocator to allocate storage space for this NodeStore.
  37. * @param logger the means for this node store to log.
  38. */
  39. struct NodeStore* NodeStore_new(struct Address* myAddress,
  40. const uint32_t capacity,
  41. struct Allocator* allocator,
  42. struct Log* logger);
  43. /**
  44. * Discover a new node (or rediscover an existing one).
  45. *
  46. * @param nodeStore the store
  47. * @param addr the address of the new node
  48. * @param reachDiff the amount to credit this node
  49. * @param scheme the encoding scheme used by this node.
  50. * @param encodingFormNumber the number of the smallest possible encoding form for to encoding
  51. * the interface number through which this message came.
  52. * @param reach the quality of the path to the new node
  53. */
  54. struct Node_Link* NodeStore_discoverNode(struct NodeStore* nodeStore,
  55. struct Address* addr,
  56. struct EncodingScheme* scheme,
  57. int inverseLinkEncodingFormNumber,
  58. uint32_t reach);
  59. struct Node_Two* NodeStore_nodeForAddr(struct NodeStore* nodeStore, uint8_t addr[16]);
  60. struct Node_Two* NodeStore_closestNode(struct NodeStore* nodeStore, uint64_t path);
  61. struct Node_Link* NodeStore_linkForPath(struct NodeStore* nodeStore, uint64_t path);
  62. struct Node_Link* NodeStore_getLink(struct Node_Two* parent, uint32_t linkNum);
  63. struct Node_Link* NodeStore_getLinkOnPath(struct NodeStore* nodeStore,
  64. uint64_t routeLabel,
  65. uint32_t hopNum);
  66. uint32_t NodeStore_linkCount(struct Node_Two* node);
  67. #define NodeStore_optimizePath_INVALID (~((uint64_t)0))
  68. uint64_t NodeStore_optimizePath(struct NodeStore* nodeStore, uint64_t path);
  69. /**
  70. * Get a route label for a given path through the network.
  71. *
  72. * @param nodeStore the store
  73. * @param pathToParent a label for getting to a node.
  74. * @param pathParentToChild the cannonicalized label for getting from the parent node to the child.
  75. * @return a path if all goes well, otherwise:
  76. * NodeStore_getRouteLabel_PARENT_NOT_FOUND if the path to the parent node does not
  77. * lead to a known node, or:
  78. * NodeStore_getRouteLabel_CHILD_NOT_FOUND if no peer could be found which links from that
  79. * path from the parent.
  80. */
  81. #define NodeStore_getRouteLabel_PARENT_NOT_FOUND ((~((uint64_t)0))-1)
  82. #define NodeStore_getRouteLabel_CHILD_NOT_FOUND ((~((uint64_t)0))-2)
  83. uint64_t NodeStore_getRouteLabel(struct NodeStore* nodeStore,
  84. uint64_t pathToParent,
  85. uint64_t pathParentToChild);
  86. /**
  87. * @return a human readable version of the error response from getRouteLabel or return NULL if
  88. * getRouteLabel succeeded.
  89. */
  90. char* NodeStore_getRouteLabel_strerror(uint64_t returnVal);
  91. /**
  92. * Find the one best node using LinkStateNodeCollector. LinkStateNodeCollector prefers a
  93. * keyspace match (same address). It breaks ties by choosing the highest version node
  94. * (versions above it's own are considered the same as it's version). It breaks ties of the
  95. * above two by which node has non-zero reach and finally shortest label fragment wins.
  96. *
  97. * @param targetAddress the address used for comparing distance
  98. * @param store the NodeStore
  99. * @return the node w/ address closest to targetAddress or NULL if myAddress is closest
  100. */
  101. struct Node_Two* NodeStore_getBest(struct Address* targetAddress, struct NodeStore* store);
  102. /**
  103. * Get direct peers of this node.
  104. * Will get peers with switch labels XOR close to the provided label up to max number.
  105. *
  106. * @param label will get peers whose labels are XOR close to this label.
  107. * @param max will not return more than this number of peers.
  108. * @param allocator for getting memory for the list.
  109. * @param store the nodestore.
  110. */
  111. struct NodeList* NodeStore_getPeers(uint64_t label,
  112. const uint32_t max,
  113. struct Allocator* allocator,
  114. struct NodeStore* store);
  115. /**
  116. * Get the best nodes for servicing a lookup.
  117. * These are returned in reverse order, from farthest to closest.
  118. *
  119. * @param store the store to get the nodes from.
  120. * @param targetAddress the address to get closest nodes for.
  121. * @param count the number of nodes to return.
  122. * @param versionOfRequestingNode the version of the node who asked for the list, no nodes will
  123. * be returned which are known to be incompatible with this version.
  124. * @param allocator the memory allocator to use for getting the memory to store the output.
  125. */
  126. struct NodeList* NodeStore_getClosestNodes(struct NodeStore* store,
  127. struct Address* targetAddress,
  128. const uint32_t count,
  129. uint32_t versionOfRequestingNode,
  130. struct Allocator* allocator);
  131. void NodeStore_updateReach(struct NodeStore* nodeStore, struct Node_Two* node, uint32_t newReach);
  132. int NodeStore_nonZeroNodes(struct NodeStore* nodeStore);
  133. int NodeStore_size(struct NodeStore* nodeStore);
  134. /**
  135. * Remove all nodes who are reachable by this path.
  136. *
  137. * @param path the label part in host order.
  138. * @param store the node store.
  139. */
  140. void NodeStore_brokenPath(uint64_t path, struct NodeStore* store);
  141. /**
  142. * Dump the table, one node at a time.
  143. */
  144. struct Node_Two* NodeStore_dumpTable(struct NodeStore* store, uint32_t index);
  145. #endif