RouterModule.h 5.9 KB

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