RouterModule.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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 RouterModule_H
  16. #define RouterModule_H
  17. #include "crypto/random/Random.h"
  18. #include "dht/Address.h"
  19. #include "dht/DHTModuleRegistry.h"
  20. #include "dht/dhtcore/Node.h"
  21. #include "dht/dhtcore/NodeStore.h"
  22. #include "benc/Object.h"
  23. #include "util/log/Log.h"
  24. #include "util/events/EventBase.h"
  25. #include <stdint.h>
  26. #include <stdbool.h>
  27. /**
  28. * The router module is the functional part of the DHT engine.
  29. * It's job is to maintain a routing table which is updated by all incoming packets.
  30. * When it gets an incoming find_node or get_* requrest, its job is to add nodes to the reply
  31. * so that the asking node can find other nodes which are closer to its target than us.
  32. */
  33. struct RouterModule;
  34. struct RouterModule_Promise;
  35. struct RouterModule_Promise
  36. {
  37. void (* callback)(struct RouterModule_Promise* promise,
  38. uint32_t lag,
  39. struct Node* fromNode,
  40. Dict* result);
  41. void* userData;
  42. struct Allocator* alloc;
  43. };
  44. /** The number of nodes to return in a search query. */
  45. #define RouterModule_K 8
  46. /** Set this in the compile to make the node a dumb client which will not advertize routes. */
  47. #ifndef RouterModule_DUMB_CLIENT
  48. #define RouterModule_DUMB_CLIENT 0
  49. #endif
  50. /**
  51. * Register a new RouterModule.
  52. *
  53. * @param registry the DHT module registry for signal handling.
  54. * @param allocator a means to allocate memory.
  55. * @param myAddress the public key for this node.
  56. * @param eventBase the libevent base.
  57. * @param logger the means of writing logs.
  58. * @param rand a source of random numbers
  59. * @param nodeStore
  60. */
  61. struct RouterModule* RouterModule_register(struct DHTModuleRegistry* registry,
  62. struct Allocator* allocator,
  63. const uint8_t myAddress[Address_KEY_SIZE],
  64. struct EventBase* eventBase,
  65. struct Log* logger,
  66. struct Random* rand,
  67. struct NodeStore* nodeStore);
  68. /**
  69. * The amount of time to wait before skipping over the first node and trying another in a search.
  70. * Any node which can't beat this time will have its reach set to 0.
  71. *
  72. * @param module this module.
  73. * @return the timeout time.
  74. */
  75. uint64_t RouterModule_searchTimeoutMilliseconds(struct RouterModule* module);
  76. /**
  77. * Manually add a node to the routing table.
  78. * This injects a node directly into the routing table, it's much safer to ping the node and let the
  79. * routing engine pick up the ping response and insert the node then.
  80. *
  81. * @param module the router module to add the node to.
  82. * @param address the address of the node.
  83. * @param version the protocol version of the node which we are adding.
  84. */
  85. void RouterModule_addNode(struct RouterModule* module, struct Address* address, uint32_t version);
  86. /**
  87. * Send a ping to a node, when it responds it will be added to the routing table.
  88. * This is the best way to introduce nodes manually.
  89. *
  90. * @param node the node to ping.
  91. * @param timeoutMilliseconds the number of milliseconds to wait beforwe calling a ping timed out
  92. * if zero, it will be calculated based on the mean response time.
  93. * @param module the router module.
  94. * @param alloc to cancel the ping, free this allocator
  95. * @return 0 if the ping was sent, -1 if there was no more space to store state.
  96. */
  97. struct RouterModule_Promise* RouterModule_pingNode(struct Node* node,
  98. uint32_t timeoutMilliseconds,
  99. struct RouterModule* module,
  100. struct Allocator* alloc);
  101. struct RouterModule_Promise* RouterModule_newMessage(struct Node* node,
  102. uint32_t timeoutMilliseconds,
  103. struct RouterModule* module,
  104. struct Allocator* alloc);
  105. void RouterModule_sendMessage(struct RouterModule_Promise* promise, Dict* request);
  106. int RouterModule_brokenPath(const uint64_t path, struct RouterModule* module);
  107. /**
  108. * Get a node from the NodeStore, see: NodeStore_getNodeByNetworkAddr() of which this is a clone.
  109. */
  110. struct Node* RouterModule_getNode(uint64_t path, struct RouterModule* module);
  111. struct Node* RouterModule_lookup(uint8_t targetAddr[Address_SEARCH_TARGET_SIZE],
  112. struct RouterModule* module);
  113. void RouterModule_updateReach(struct Node* node, struct RouterModule* module);
  114. uint32_t RouterModule_globalMeanResponseTime(struct RouterModule* module);
  115. #endif