RouteTracer.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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. #include "dht/Address.h"
  16. #include "dht/dhtcore/RouteTracer.h"
  17. #include "dht/dhtcore/Node.h"
  18. #include "dht/dhtcore/NodeList.h"
  19. #include "dht/dhtcore/VersionList.h"
  20. #include "dht/CJDHTConstants.h"
  21. #include "switch/LabelSplicer.h"
  22. #include "util/events/EventBase.h"
  23. #include "util/events/Timeout.h"
  24. #include "util/version/Version.h"
  25. #include "util/Bits.h"
  26. #include "util/log/Log.h"
  27. #include "util/Endian.h"
  28. struct RouteTracer_pvt
  29. {
  30. struct RouteTracer pub;
  31. struct EventBase* eventBase;
  32. struct NodeStore* nodeStore;
  33. struct RouterModule* router;
  34. struct Log* logger;
  35. uint8_t myAddress[16];
  36. Identity
  37. };
  38. struct RouteTracer_Trace
  39. {
  40. struct RouterModule_Promise pub;
  41. struct RouteTracer_pvt* tracer;
  42. uint64_t target;
  43. uint64_t lastNodeAsked;
  44. Identity
  45. };
  46. static void noPeers(void* vtrace)
  47. {
  48. struct RouteTracer_Trace* trace = Identity_cast((struct RouteTracer_Trace*)vtrace);
  49. // trace has stalled.
  50. if (trace->pub.callback) {
  51. trace->pub.callback(&trace->pub, 0, NULL, NULL);
  52. }
  53. Allocator_free(trace->pub.alloc);
  54. }
  55. #define log(logger, trace, message) \
  56. do { \
  57. uint8_t target[20]; \
  58. uint8_t lastNodeAsked[20]; \
  59. AddrTools_printPath(target, (trace)->target); \
  60. AddrTools_printPath(lastNodeAsked, (trace)->lastNodeAsked); \
  61. Log_debug((logger), "tracing [%s] last request to [%s] %s", \
  62. target, lastNodeAsked, (message)); \
  63. } while (0)
  64. // CHECKFILES_IGNORE expecting a ;
  65. static void traceStep(struct RouteTracer_Trace* trace, struct Node* next);
  66. static void responseCallback(struct RouterModule_Promise* promise,
  67. uint32_t lagMilliseconds,
  68. struct Node* fromNode,
  69. Dict* result)
  70. {
  71. struct RouteTracer_Trace* trace = Identity_cast((struct RouteTracer_Trace*)promise->userData);
  72. struct RouteTracer_pvt* ctx = Identity_cast((struct RouteTracer_pvt*)trace->tracer);
  73. if (!fromNode) {
  74. // trace has stalled.
  75. log(ctx->logger, trace, "STALLED request timed out");
  76. noPeers(trace);
  77. return;
  78. }
  79. if (trace->pub.callback) {
  80. trace->pub.callback(&trace->pub, lagMilliseconds, fromNode, result);
  81. }
  82. String* nodes = Dict_getString(result, CJDHTConstants_NODES);
  83. if (nodes && (nodes->len == 0 || nodes->len % Address_SERIALIZED_SIZE != 0)) {
  84. log(ctx->logger, trace, "STALLED dropping unrecognized reply");
  85. noPeers(trace);
  86. return;
  87. }
  88. struct VersionList* versions = NULL;
  89. String* versionsStr = Dict_getString(result, CJDHTConstants_NODE_PROTOCOLS);
  90. if (versionsStr) {
  91. versions = VersionList_parse(versionsStr, promise->alloc);
  92. #ifdef Version_1_COMPAT
  93. // Version 1 lies about the versions of other nodes, assume they're all v1.
  94. if (fromNode->version < 2) {
  95. for (int i = 0; i < (int)versions->length; i++) {
  96. versions->versions[i] = 1;
  97. }
  98. }
  99. #endif
  100. }
  101. struct Node* next = NULL;
  102. for (uint32_t i = 0; nodes && i < nodes->len; i += Address_SERIALIZED_SIZE) {
  103. struct Address addr;
  104. Address_parse(&addr, (uint8_t*) &nodes->bytes[i]);
  105. // calculate the ipv6
  106. Address_getPrefix(&addr);
  107. if (!Bits_memcmp(ctx->myAddress, addr.ip6.bytes, 16)) {
  108. // Any path which loops back through us is necessarily a dead route.
  109. NodeStore_brokenPath(addr.path, ctx->nodeStore);
  110. continue;
  111. }
  112. // We need to splice the given address on to the end of the
  113. // address of the node which gave it to us.
  114. addr.path = LabelSplicer_splice(addr.path, fromNode->address.path);
  115. #ifdef Log_DEBUG
  116. uint8_t printedAddr[60];
  117. Address_print(printedAddr, &addr);
  118. Log_debug(ctx->logger, "discovered node [%s]", printedAddr);
  119. #endif
  120. if (addr.path == UINT64_MAX) {
  121. log(ctx->logger, trace, "dropping node because route could not be spliced");
  122. continue;
  123. }
  124. if (!AddressCalc_validAddress(addr.ip6.bytes)) {
  125. log(ctx->logger, trace, "was told garbage");
  126. // This should never happen, badnode.
  127. break;
  128. }
  129. // Nodes we are told about are inserted with 0 reach and assumed version 1.
  130. uint32_t version = (versions) ? versions->versions[i / Address_SERIALIZED_SIZE] : 1;
  131. struct Node* n = NodeStore_addNode(ctx->nodeStore, &addr, 0, version);
  132. if (!n) {
  133. // incompatible version, introduced to ourselves...
  134. } else if (!LabelSplicer_routesThrough(trace->target, n->address.path)) {
  135. // not on the way
  136. } else if (n->address.path <= fromNode->address.path) {
  137. // losing ground
  138. } else if (next && n->address.path >= next->address.path) {
  139. // not better than the one we have
  140. } else {
  141. next = n;
  142. }
  143. }
  144. if (fromNode->address.path == trace->target) {
  145. log(ctx->logger, trace, "Trace completed successfully");
  146. noPeers(trace);
  147. return;
  148. }
  149. if (!nodes) {
  150. log(ctx->logger, trace, "No nodes in trace response");
  151. }
  152. if (!next) {
  153. log(ctx->logger, trace, "STALLED no suitable peers in reply");
  154. noPeers(trace);
  155. return;
  156. }
  157. if (!LabelSplicer_routesThrough(trace->target, next->address.path)) {
  158. log(ctx->logger, trace, "STALLED Nodestore broke the path of the best option");
  159. noPeers(trace);
  160. return;
  161. }
  162. traceStep(trace, next);
  163. }
  164. static void traceStep(struct RouteTracer_Trace* trace, struct Node* next)
  165. {
  166. struct RouteTracer_pvt* ctx = Identity_cast((struct RouteTracer_pvt*)trace->tracer);
  167. if (!next) {
  168. // can't find a next node, stalled.
  169. Timeout_setTimeout(noPeers, trace, 0, trace->tracer->eventBase, trace->pub.alloc);
  170. return;
  171. }
  172. Assert_true(LabelSplicer_routesThrough(trace->target, next->address.path));
  173. trace->lastNodeAsked = next->address.path;
  174. struct RouterModule_Promise* rp =
  175. RouterModule_newMessage(next, 0, ctx->router, trace->pub.alloc);
  176. Dict* message = Dict_new(rp->alloc);
  177. #ifdef Version_4_COMPAT
  178. if (next->version < 5) {
  179. // The node doesn't support the new API so try running a search for
  180. // the bitwise complement of their address to get some peers.
  181. Dict_putString(message, CJDHTConstants_QUERY, CJDHTConstants_QUERY_FN, rp->alloc);
  182. String* notAddr = String_newBinary((char*)next->address.ip6.bytes, 16, rp->alloc);
  183. for (int i = 0; i < 16; i++) {
  184. notAddr->bytes[i] ^= 0xff;
  185. }
  186. Dict_putString(message, CJDHTConstants_TARGET, notAddr, rp->alloc);
  187. log(ctx->logger, trace, "Sending legacy search method because getpeers is unavailable");
  188. } else {
  189. #endif
  190. Dict_putString(message, CJDHTConstants_QUERY, CJDHTConstants_QUERY_GP, rp->alloc);
  191. uint64_t labelForThem = LabelSplicer_unsplice(trace->target, next->address.path);
  192. labelForThem = Endian_hostToBigEndian64(labelForThem);
  193. String* target = String_newBinary((char*)&labelForThem, 8, rp->alloc);
  194. Dict_putString(message, CJDHTConstants_TARGET, target, rp->alloc);
  195. log(ctx->logger, trace, "Sending getpeers request");
  196. #ifdef Version_4_COMPAT
  197. }
  198. #endif
  199. rp->userData = trace;
  200. rp->callback = responseCallback;
  201. RouterModule_sendMessage(rp, message);
  202. }
  203. struct RouterModule_Promise* RouteTracer_trace(uint64_t route,
  204. struct RouteTracer* routeTracer,
  205. struct Allocator* allocator)
  206. {
  207. struct RouteTracer_pvt* tracer = Identity_cast((struct RouteTracer_pvt*)routeTracer);
  208. struct Allocator* alloc = Allocator_child(allocator);
  209. struct RouteTracer_Trace* trace = Allocator_clone(alloc, (&(struct RouteTracer_Trace) {
  210. .target = route,
  211. .tracer = tracer,
  212. .pub = {
  213. .alloc = alloc
  214. }
  215. }));
  216. Identity_set(trace);
  217. struct NodeList* firstHop = NodeStore_getPeers(route, 1, alloc, tracer->nodeStore);
  218. struct Node* n = (firstHop->size > 0) ? firstHop->nodes[0] : NULL;
  219. if (n && !LabelSplicer_routesThrough(trace->target, n->address.path)) {
  220. n = NULL;
  221. }
  222. traceStep(trace, n);
  223. return &trace->pub;
  224. }
  225. struct RouteTracer* RouteTracer_new(struct NodeStore* store,
  226. struct RouterModule* router,
  227. const uint8_t myAddress[16],
  228. struct EventBase* base,
  229. struct Log* logger,
  230. struct Allocator* alloc)
  231. {
  232. struct RouteTracer_pvt* out = Allocator_clone(alloc, (&(struct RouteTracer_pvt) {
  233. .nodeStore = store,
  234. .router = router,
  235. .logger = logger,
  236. .eventBase = base
  237. }));
  238. Identity_set(out);
  239. Bits_memcpyConst(out->myAddress, myAddress, 16);
  240. return &out->pub;
  241. }