1
0

RouterModule_admin.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  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. #include "admin/Admin.h"
  16. #include "benc/Dict.h"
  17. #include "benc/String.h"
  18. #include "benc/Int.h"
  19. #include "dht/dhtcore/Node.h"
  20. #include "dht/dhtcore/RouterModule.h"
  21. #include "dht/dhtcore/RouterModule_admin.h"
  22. #include "dht/dhtcore/Router.h"
  23. #include "dht/dhtcore/ReplySerializer.h"
  24. #include "dht/Address.h"
  25. #include "dht/CJDHTConstants.h"
  26. #include "memory/Allocator.h"
  27. #include "util/AddrTools.h"
  28. #include "util/Hex.h"
  29. struct Context {
  30. struct Admin* admin;
  31. struct Allocator* allocator;
  32. struct RouterModule* module;
  33. struct Router* router;
  34. Identity
  35. };
  36. static void lookup(Dict* args, void* vcontext, String* txid, struct Allocator* requestAlloc)
  37. {
  38. struct Context* ctx = vcontext;
  39. String* addrStr = Dict_getStringC(args, "address");
  40. char* err = NULL;
  41. uint8_t addr[16];
  42. uint8_t resultBuff[60];
  43. char* result = (char*) resultBuff;
  44. if (addrStr->len != 39) {
  45. err = "address wrong length";
  46. } else if (AddrTools_parseIp(addr, (uint8_t*) addrStr->bytes)) {
  47. err = "failed to parse address";
  48. } else {
  49. struct Node_Two* n = Router_lookup(ctx->router, addr);
  50. if (!n) {
  51. result = "not found";
  52. } else if (Bits_memcmp(addr, n->address.ip6.bytes, 16)) {
  53. Address_print(resultBuff, &n->address);
  54. } else {
  55. AddrTools_printPath(resultBuff, n->address.path);
  56. }
  57. }
  58. Dict response = Dict_CONST(
  59. String_CONST("error"), String_OBJ(String_CONST((err) ? err : "none")), Dict_CONST(
  60. String_CONST("result"), String_OBJ(String_CONST(result)), NULL
  61. ));
  62. Admin_sendMessage(&response, txid, ctx->admin);
  63. }
  64. struct Ping
  65. {
  66. String* txid;
  67. struct RouterModule_Promise* rp;
  68. struct Context* ctx;
  69. Identity
  70. };
  71. static void pingResponse(struct RouterModule_Promise* promise,
  72. uint32_t lag,
  73. struct Address* from,
  74. Dict* responseDict)
  75. {
  76. struct Ping* ping = Identity_check((struct Ping*)promise->userData);
  77. struct Allocator* tempAlloc = promise->alloc;
  78. Dict* resp = Dict_new(tempAlloc);
  79. String* versionBin = Dict_getString(responseDict, CJDHTConstants_VERSION);
  80. if (versionBin && versionBin->len == 20) {
  81. String* versionStr = String_newBinary(NULL, 40, tempAlloc);
  82. Hex_encode(versionStr->bytes, 40, versionBin->bytes, 20);
  83. Dict_putStringC(resp, "version", versionStr, tempAlloc);
  84. } else {
  85. Dict_putStringCC(resp, "version", "unknown", tempAlloc);
  86. }
  87. String* result = (responseDict) ? String_CONST("pong") : String_CONST("timeout");
  88. Dict_putStringC(resp, "result", result, tempAlloc);
  89. int64_t* protocolVersion = Dict_getInt(responseDict, CJDHTConstants_PROTOCOL);
  90. if (protocolVersion) {
  91. Dict_putIntC(resp, "protocol", *protocolVersion, tempAlloc);
  92. }
  93. Dict_putIntC(resp, "ms", lag, tempAlloc);
  94. if (from) {
  95. uint8_t fromStr[60] = "";
  96. Address_print(fromStr, from);
  97. Dict_putStringC(resp, "from", String_new(fromStr, tempAlloc), tempAlloc);
  98. String* addr = Address_toString(from, tempAlloc);
  99. Dict_putStringC(resp, "addr", addr, tempAlloc);
  100. }
  101. Dict_putStringCC(resp, "deprecation",
  102. "from,protocol,version will soon be removed", tempAlloc);
  103. Admin_sendMessage(resp, ping->txid, ping->ctx->admin);
  104. }
  105. static void genericResponse(struct RouterModule_Promise* promise,
  106. uint32_t lag,
  107. struct Address* from,
  108. Dict* responseDict,
  109. String* name,
  110. bool splicePath)
  111. {
  112. struct Ping* ping = Identity_check((struct Ping*)promise->userData);
  113. Dict* out = Dict_new(promise->alloc);
  114. String* result = (responseDict) ? name : String_CONST("timeout");
  115. Dict_putStringC(out, "result", result, promise->alloc);
  116. if (responseDict) {
  117. struct Address_List* addrs =
  118. ReplySerializer_parse(from, responseDict, NULL, splicePath, promise->alloc);
  119. List* nodes = List_new(promise->alloc);
  120. for (int i = 0; addrs && i < addrs->length; i++) {
  121. String* addr = Address_toString(&addrs->elems[i], promise->alloc);
  122. List_addString(nodes, addr, promise->alloc);
  123. }
  124. Dict_putList(out, name, nodes, promise->alloc);
  125. String* schemeDefinition = Dict_getString(responseDict, CJDHTConstants_ENC_SCHEME);
  126. if (schemeDefinition) {
  127. struct EncodingScheme* scheme =
  128. EncodingScheme_deserialize(schemeDefinition, promise->alloc);
  129. if (scheme) {
  130. List* encScheme = EncodingScheme_asList(scheme, promise->alloc);
  131. Dict_putList(
  132. out, String_new("encodingScheme", promise->alloc), encScheme, promise->alloc);
  133. }
  134. }
  135. }
  136. Dict_putIntC(out, "ms", lag, promise->alloc);
  137. Dict_putStringCC(out, "error", "none", promise->alloc);
  138. Admin_sendMessage(out, ping->txid, ping->ctx->admin);
  139. }
  140. static void getPeersResponse(struct RouterModule_Promise* promise,
  141. uint32_t lag,
  142. struct Address* from,
  143. Dict* responseDict)
  144. {
  145. genericResponse(promise, lag, from, responseDict, String_CONST("peers"), false);
  146. }
  147. static void findNodeResponse(struct RouterModule_Promise* promise,
  148. uint32_t lag,
  149. struct Address* from,
  150. Dict* responseDict)
  151. {
  152. genericResponse(promise, lag, from, responseDict, String_CONST("nodes"), true);
  153. }
  154. static struct Address* getNode(String* pathStr,
  155. struct Context* ctx,
  156. char** errOut,
  157. struct Allocator* alloc)
  158. {
  159. struct Address addr = {.path=0};
  160. if (pathStr->len == 19 && !AddrTools_parsePath(&addr.path, pathStr->bytes)) {
  161. struct Node_Link* nl = Router_linkForPath(ctx->router, addr.path);
  162. if (!nl) {
  163. *errOut = "not_found";
  164. return NULL;
  165. } else {
  166. Bits_memcpy(&addr, &nl->child->address, sizeof(struct Address));
  167. }
  168. } else if (!AddrTools_parseIp(addr.ip6.bytes, pathStr->bytes)) {
  169. struct Node_Two* n = Router_lookup(ctx->router, addr.ip6.bytes);
  170. if (!n || Bits_memcmp(addr.ip6.bytes, n->address.ip6.bytes, 16)) {
  171. *errOut = "not_found";
  172. return NULL;
  173. } else {
  174. Bits_memcpy(&addr, &n->address, sizeof(struct Address));
  175. }
  176. } else {
  177. struct Address* a = Address_fromString(pathStr, alloc);
  178. if (a) { return a; }
  179. *errOut = "parse_path";
  180. return NULL;
  181. }
  182. return Allocator_clone(alloc, &addr);
  183. }
  184. static void pingNode(Dict* args, void* vctx, String* txid, struct Allocator* requestAlloc)
  185. {
  186. struct Context* ctx = Identity_check((struct Context*) vctx);
  187. String* pathStr = Dict_getStringC(args, "path");
  188. int64_t* timeoutPtr = Dict_getIntC(args, "timeout");
  189. uint32_t timeout = (timeoutPtr && *timeoutPtr > 0) ? *timeoutPtr : 0;
  190. char* err = NULL;
  191. struct Address* addr = getNode(pathStr, ctx, &err, requestAlloc);
  192. if (err) {
  193. Dict errDict = Dict_CONST(String_CONST("error"), String_OBJ(String_CONST(err)), NULL);
  194. Admin_sendMessage(&errDict, txid, ctx->admin);
  195. return;
  196. }
  197. struct RouterModule_Promise* rp =
  198. RouterModule_pingNode(addr, timeout, ctx->module, ctx->allocator);
  199. struct Ping* ping = Allocator_calloc(rp->alloc, sizeof(struct Ping), 1);
  200. Identity_set(ping);
  201. ping->txid = String_clone(txid, rp->alloc);
  202. ping->rp = rp;
  203. ping->ctx = ctx;
  204. rp->userData = ping;
  205. rp->callback = pingResponse;
  206. }
  207. static void getPeers(Dict* args, void* vctx, String* txid, struct Allocator* requestAlloc)
  208. {
  209. struct Context* ctx = Identity_check((struct Context*) vctx);
  210. String* nearbyLabelStr = Dict_getStringC(args, "nearbyPath");
  211. String* pathStr = Dict_getStringC(args, "path");
  212. int64_t* timeoutPtr = Dict_getIntC(args, "timeout");
  213. uint32_t timeout = (timeoutPtr && *timeoutPtr > 0) ? *timeoutPtr : 0;
  214. char* err = NULL;
  215. struct Address* addr = getNode(pathStr, ctx, &err, requestAlloc);
  216. uint64_t nearbyLabel = 0;
  217. if (!err && nearbyLabelStr) {
  218. if (nearbyLabelStr->len != 19 || AddrTools_parsePath(&nearbyLabel, nearbyLabelStr->bytes)) {
  219. err = "parse_nearbyLabel";
  220. }
  221. }
  222. if (err) {
  223. Dict errDict = Dict_CONST(String_CONST("error"), String_OBJ(String_CONST(err)), NULL);
  224. Admin_sendMessage(&errDict, txid, ctx->admin);
  225. return;
  226. }
  227. struct RouterModule_Promise* rp =
  228. RouterModule_getPeers(addr, nearbyLabel, timeout, ctx->module, ctx->allocator);
  229. struct Ping* ping = Allocator_calloc(rp->alloc, sizeof(struct Ping), 1);
  230. Identity_set(ping);
  231. ping->txid = String_clone(txid, rp->alloc);
  232. ping->rp = rp;
  233. ping->ctx = ctx;
  234. rp->userData = ping;
  235. rp->callback = getPeersResponse;
  236. }
  237. static void findNode(Dict* args, void* vctx, String* txid, struct Allocator* requestAlloc)
  238. {
  239. struct Context* ctx = Identity_check((struct Context*) vctx);
  240. String* nodeToQueryStr = Dict_getStringC(args, "nodeToQuery");
  241. String* targetStr = Dict_getStringC(args, "target");
  242. int64_t* timeoutPtr = Dict_getIntC(args, "timeout");
  243. uint32_t timeout = (timeoutPtr && *timeoutPtr > 0) ? *timeoutPtr : 0;
  244. char* err = NULL;
  245. struct Address* nodeToQuery = getNode(nodeToQueryStr, ctx, &err, requestAlloc);
  246. uint8_t target[16];
  247. if (!err) {
  248. if (targetStr->len != 39 || AddrTools_parseIp(target, targetStr->bytes)) {
  249. err = "parse_target";
  250. }
  251. }
  252. if (err) {
  253. Dict errDict = Dict_CONST(String_CONST("error"), String_OBJ(String_CONST(err)), NULL);
  254. Admin_sendMessage(&errDict, txid, ctx->admin);
  255. return;
  256. }
  257. struct RouterModule_Promise* rp =
  258. RouterModule_findNode(nodeToQuery, target, timeout, ctx->module, ctx->allocator);
  259. struct Ping* ping = Allocator_calloc(rp->alloc, sizeof(struct Ping), 1);
  260. Identity_set(ping);
  261. ping->txid = String_clone(txid, rp->alloc);
  262. ping->rp = rp;
  263. ping->ctx = ctx;
  264. rp->userData = ping;
  265. rp->callback = findNodeResponse;
  266. }
  267. static void nextHop(Dict* args, void* vctx, String* txid, struct Allocator* requestAlloc)
  268. {
  269. struct Context* ctx = Identity_check((struct Context*) vctx);
  270. String* nodeToQueryStr = Dict_getStringC(args, "nodeToQuery");
  271. String* targetStr = Dict_getStringC(args, "target");
  272. int64_t* timeoutPtr = Dict_getIntC(args, "timeout");
  273. uint32_t timeout = (timeoutPtr && *timeoutPtr > 0) ? *timeoutPtr : 0;
  274. char* err = NULL;
  275. struct Address* nodeToQuery = getNode(nodeToQueryStr, ctx, &err, requestAlloc);
  276. uint8_t target[16];
  277. if (!err && AddrTools_parseIp(target, targetStr->bytes)) { err = "parse_target"; }
  278. if (err) {
  279. Dict errDict = Dict_CONST(String_CONST("error"), String_OBJ(String_CONST(err)), NULL);
  280. Admin_sendMessage(&errDict, txid, ctx->admin);
  281. return;
  282. }
  283. struct RouterModule_Promise* rp =
  284. RouterModule_nextHop(nodeToQuery, target, timeout, ctx->module, ctx->allocator);
  285. struct Ping* ping = Allocator_calloc(rp->alloc, sizeof(struct Ping), 1);
  286. Identity_set(ping);
  287. ping->txid = String_clone(txid, rp->alloc);
  288. ping->rp = rp;
  289. ping->ctx = ctx;
  290. rp->userData = ping;
  291. rp->callback = findNodeResponse;
  292. }
  293. void RouterModule_admin_register(struct RouterModule* module,
  294. struct Router* router,
  295. struct Admin* admin,
  296. struct Allocator* alloc)
  297. {
  298. // for improved reporting
  299. alloc = Allocator_child(alloc);
  300. struct Context* ctx = Allocator_clone(alloc, (&(struct Context) {
  301. .admin = admin,
  302. .allocator = alloc,
  303. .module = module,
  304. .router = router
  305. }));
  306. Identity_set(ctx);
  307. Admin_registerFunction("RouterModule_nextHop", nextHop, ctx, true,
  308. ((struct Admin_FunctionArg[]) {
  309. { .name = "nodeToQuery", .required = 1, .type = "String" },
  310. { .name = "target", .required = 1, .type = "String" },
  311. { .name = "timeout", .required = 0, .type = "Int" }
  312. }), admin);
  313. Admin_registerFunction("RouterModule_lookup", lookup, ctx, true,
  314. ((struct Admin_FunctionArg[]) {
  315. { .name = "address", .required = 1, .type = "String" }
  316. }), admin);
  317. Admin_registerFunction("RouterModule_pingNode", pingNode, ctx, true,
  318. ((struct Admin_FunctionArg[]) {
  319. { .name = "path", .required = 1, .type = "String" },
  320. { .name = "timeout", .required = 0, .type = "Int" },
  321. }), admin);
  322. Admin_registerFunction("RouterModule_getPeers", getPeers, ctx, true,
  323. ((struct Admin_FunctionArg[]) {
  324. { .name = "path", .required = 1, .type = "String" },
  325. { .name = "timeout", .required = 0, .type = "Int" },
  326. { .name = "nearbyPath", .required = 0, .type = "String" }
  327. }), admin);
  328. Admin_registerFunction("RouterModule_findNode", findNode, ctx, true,
  329. ((struct Admin_FunctionArg[]) {
  330. { .name = "nodeToQuery", .required = 1, .type = "String" },
  331. { .name = "target", .required = 1, .type = "String" },
  332. { .name = "timeout", .required = 0, .type = "Int" }
  333. }), admin);
  334. }