RouterModule.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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 "util/Linker.h"
  26. Linker_require("dht/dhtcore/RouterModule.c")
  27. #include <stdint.h>
  28. #include <stdbool.h>
  29. /**
  30. * The router module is the functional part of the DHT engine.
  31. * It's job is to maintain a routing table which is updated by all incoming packets.
  32. * When it gets an incoming find_node or get_* requrest, its job is to add nodes to the reply
  33. * so that the asking node can find other nodes which are closer to its target than us.
  34. */
  35. struct RouterModule;
  36. struct RouterModule_Promise;
  37. struct RouterModule_Promise
  38. {
  39. void (* callback)(struct RouterModule_Promise* promise,
  40. uint32_t lag,
  41. struct Address* from,
  42. Dict* result);
  43. void* userData;
  44. struct Allocator* alloc;
  45. };
  46. /** The number of nodes to return in a search query. */
  47. #define RouterModule_K 8
  48. /**
  49. * Register a new RouterModule.
  50. *
  51. * @param registry the DHT module registry for signal handling.
  52. * @param allocator a means to allocate memory.
  53. * @param myAddress the public key for this node.
  54. * @param eventBase the libevent base.
  55. * @param logger the means of writing logs.
  56. * @param rand a source of random numbers
  57. * @param nodeStore
  58. */
  59. struct RouterModule* RouterModule_register(struct DHTModuleRegistry* registry,
  60. struct Allocator* allocator,
  61. const uint8_t myAddress[Address_KEY_SIZE],
  62. struct EventBase* eventBase,
  63. struct Log* logger,
  64. struct Random* rand,
  65. struct NodeStore* nodeStore);
  66. /**
  67. * The amount of time to wait before skipping over the first node and trying another in a search.
  68. * Any node which can't beat this time will have its reach set to 0.
  69. *
  70. * @param module this module.
  71. * @return the timeout time.
  72. */
  73. uint64_t RouterModule_searchTimeoutMilliseconds(struct RouterModule* module);
  74. /**
  75. * Send a ping to a node, when it responds it will be added to the routing table.
  76. * This is the best way to introduce nodes manually.
  77. *
  78. * @param addr the address of the node to ping.
  79. * @param timeoutMilliseconds the number of milliseconds to wait beforwe calling a ping timed out
  80. * if zero, it will be calculated based on the mean response time.
  81. * @param module the router module.
  82. * @param alloc to cancel the ping, free this allocator
  83. * @return 0 if the ping was sent, -1 if there was no more space to store state.
  84. */
  85. struct RouterModule_Promise* RouterModule_pingNode(struct Address* addr,
  86. uint32_t timeoutMilliseconds,
  87. struct RouterModule* module,
  88. struct Allocator* alloc);
  89. struct RouterModule_Promise* RouterModule_newMessage(struct Address* addr,
  90. uint32_t timeoutMilliseconds,
  91. struct RouterModule* module,
  92. struct Allocator* alloc);
  93. void RouterModule_sendMessage(struct RouterModule_Promise* promise, Dict* request);
  94. void RouterModule_brokenPath(const uint64_t path, struct RouterModule* module);
  95. struct Node_Two* RouterModule_nodeForPath(uint64_t path, struct RouterModule* module);
  96. struct Node_Two* RouterModule_lookup(uint8_t targetAddr[Address_SEARCH_TARGET_SIZE],
  97. struct RouterModule* module);
  98. uint32_t RouterModule_globalMeanResponseTime(struct RouterModule* module);
  99. struct RouterModule_Promise* RouterModule_getPeers(struct Address* addr,
  100. uint64_t nearbyLabel,
  101. uint32_t timeoutMilliseconds,
  102. struct RouterModule* module,
  103. struct Allocator* alloc);
  104. #endif