RouterModule_admin.c 14 KB

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