RouterModule_admin.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  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 "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_getString(args, String_CONST("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_putString(resp, String_CONST("version"), versionStr, tempAlloc);
  84. } else {
  85. Dict_putString(resp, String_CONST("version"), String_CONST("unknown"), tempAlloc);
  86. }
  87. String* result = (responseDict) ? String_CONST("pong") : String_CONST("timeout");
  88. Dict_putString(resp, String_CONST("result"), result, tempAlloc);
  89. int64_t* protocolVersion = Dict_getInt(responseDict, CJDHTConstants_PROTOCOL);
  90. if (protocolVersion) {
  91. Dict_putInt(resp, String_CONST("protocol"), *protocolVersion, tempAlloc);
  92. }
  93. Dict_putInt(resp, String_CONST("ms"), lag, tempAlloc);
  94. if (from) {
  95. uint8_t fromStr[60] = "";
  96. Address_print(fromStr, from);
  97. Dict_putString(resp, String_CONST("from"), String_new(fromStr, tempAlloc), tempAlloc);
  98. String* addr = Address_toString(from, tempAlloc);
  99. Dict_putString(resp, String_CONST("addr"), addr, tempAlloc);
  100. }
  101. Dict_putString(resp, String_CONST("deprecation"),
  102. String_CONST("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_putString(out, String_CONST("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. }
  126. Dict_putInt(out, String_CONST("ms"), lag, promise->alloc);
  127. Dict_putString(out, String_CONST("error"), String_CONST("none"), promise->alloc);
  128. Admin_sendMessage(out, ping->txid, ping->ctx->admin);
  129. }
  130. static void getPeersResponse(struct RouterModule_Promise* promise,
  131. uint32_t lag,
  132. struct Address* from,
  133. Dict* responseDict)
  134. {
  135. genericResponse(promise, lag, from, responseDict, String_CONST("peers"), false);
  136. }
  137. static void findNodeResponse(struct RouterModule_Promise* promise,
  138. uint32_t lag,
  139. struct Address* from,
  140. Dict* responseDict)
  141. {
  142. genericResponse(promise, lag, from, responseDict, String_CONST("nodes"), true);
  143. }
  144. static struct Address* getNode(String* pathStr,
  145. struct Context* ctx,
  146. char** errOut,
  147. struct Allocator* alloc)
  148. {
  149. struct Address addr = {.path=0};
  150. if (pathStr->len == 19 && !AddrTools_parsePath(&addr.path, pathStr->bytes)) {
  151. struct Node_Link* nl = Router_linkForPath(ctx->router, addr.path);
  152. if (!nl) {
  153. *errOut = "not_found";
  154. return NULL;
  155. } else {
  156. Bits_memcpyConst(&addr, &nl->child->address, sizeof(struct Address));
  157. }
  158. } else if (!AddrTools_parseIp(addr.ip6.bytes, pathStr->bytes)) {
  159. struct Node_Two* n = Router_lookup(ctx->router, addr.ip6.bytes);
  160. if (!n || Bits_memcmp(addr.ip6.bytes, n->address.ip6.bytes, 16)) {
  161. *errOut = "not_found";
  162. return NULL;
  163. } else {
  164. Bits_memcpyConst(&addr, &n->address, sizeof(struct Address));
  165. }
  166. } else {
  167. struct Address* a = Address_fromString(pathStr, alloc);
  168. if (a) { return a; }
  169. *errOut = "parse_path";
  170. return NULL;
  171. }
  172. return Allocator_clone(alloc, &addr);
  173. }
  174. static void pingNode(Dict* args, void* vctx, String* txid, struct Allocator* requestAlloc)
  175. {
  176. struct Context* ctx = Identity_check((struct Context*) vctx);
  177. String* pathStr = Dict_getString(args, String_CONST("path"));
  178. int64_t* timeoutPtr = Dict_getInt(args, String_CONST("timeout"));
  179. uint32_t timeout = (timeoutPtr && *timeoutPtr > 0) ? *timeoutPtr : 0;
  180. char* err = NULL;
  181. struct Address* addr = getNode(pathStr, ctx, &err, requestAlloc);
  182. if (err) {
  183. Dict errDict = Dict_CONST(String_CONST("error"), String_OBJ(String_CONST(err)), NULL);
  184. Admin_sendMessage(&errDict, txid, ctx->admin);
  185. return;
  186. }
  187. struct RouterModule_Promise* rp =
  188. RouterModule_pingNode(addr, timeout, ctx->module, ctx->allocator);
  189. struct Ping* ping = Allocator_calloc(rp->alloc, sizeof(struct Ping), 1);
  190. Identity_set(ping);
  191. ping->txid = String_clone(txid, rp->alloc);
  192. ping->rp = rp;
  193. ping->ctx = ctx;
  194. rp->userData = ping;
  195. rp->callback = pingResponse;
  196. }
  197. static void getPeers(Dict* args, void* vctx, String* txid, struct Allocator* requestAlloc)
  198. {
  199. struct Context* ctx = Identity_check((struct Context*) vctx);
  200. String* nearbyLabelStr = Dict_getString(args, String_CONST("nearbyPath"));
  201. String* pathStr = Dict_getString(args, String_CONST("path"));
  202. int64_t* timeoutPtr = Dict_getInt(args, String_CONST("timeout"));
  203. uint32_t timeout = (timeoutPtr && *timeoutPtr > 0) ? *timeoutPtr : 0;
  204. char* err = NULL;
  205. struct Address* addr = getNode(pathStr, ctx, &err, requestAlloc);
  206. uint64_t nearbyLabel = 0;
  207. if (!err && nearbyLabelStr) {
  208. if (nearbyLabelStr->len != 19 || AddrTools_parsePath(&nearbyLabel, nearbyLabelStr->bytes)) {
  209. err = "parse_nearbyLabel";
  210. }
  211. }
  212. if (err) {
  213. Dict errDict = Dict_CONST(String_CONST("error"), String_OBJ(String_CONST(err)), NULL);
  214. Admin_sendMessage(&errDict, txid, ctx->admin);
  215. return;
  216. }
  217. struct RouterModule_Promise* rp =
  218. RouterModule_getPeers(addr, nearbyLabel, timeout, ctx->module, ctx->allocator);
  219. struct Ping* ping = Allocator_calloc(rp->alloc, sizeof(struct Ping), 1);
  220. Identity_set(ping);
  221. ping->txid = String_clone(txid, rp->alloc);
  222. ping->rp = rp;
  223. ping->ctx = ctx;
  224. rp->userData = ping;
  225. rp->callback = getPeersResponse;
  226. }
  227. static void findNode(Dict* args, void* vctx, String* txid, struct Allocator* requestAlloc)
  228. {
  229. struct Context* ctx = Identity_check((struct Context*) vctx);
  230. String* nodeToQueryStr = Dict_getString(args, String_CONST("nodeToQuery"));
  231. String* targetStr = Dict_getString(args, String_CONST("target"));
  232. int64_t* timeoutPtr = Dict_getInt(args, String_CONST("timeout"));
  233. uint32_t timeout = (timeoutPtr && *timeoutPtr > 0) ? *timeoutPtr : 0;
  234. char* err = NULL;
  235. struct Address* nodeToQuery = getNode(nodeToQueryStr, ctx, &err, requestAlloc);
  236. uint8_t target[16];
  237. if (!err) {
  238. if (targetStr->len != 39 || AddrTools_parseIp(target, targetStr->bytes)) {
  239. err = "parse_target";
  240. }
  241. }
  242. if (err) {
  243. Dict errDict = Dict_CONST(String_CONST("error"), String_OBJ(String_CONST(err)), NULL);
  244. Admin_sendMessage(&errDict, txid, ctx->admin);
  245. return;
  246. }
  247. struct RouterModule_Promise* rp =
  248. RouterModule_findNode(nodeToQuery, target, timeout, ctx->module, ctx->allocator);
  249. struct Ping* ping = Allocator_calloc(rp->alloc, sizeof(struct Ping), 1);
  250. Identity_set(ping);
  251. ping->txid = String_clone(txid, rp->alloc);
  252. ping->rp = rp;
  253. ping->ctx = ctx;
  254. rp->userData = ping;
  255. rp->callback = findNodeResponse;
  256. }
  257. static void nextHop(Dict* args, void* vctx, String* txid, struct Allocator* requestAlloc)
  258. {
  259. struct Context* ctx = Identity_check((struct Context*) vctx);
  260. String* nodeToQueryStr = Dict_getString(args, String_CONST("nodeToQuery"));
  261. String* targetStr = Dict_getString(args, String_CONST("target"));
  262. int64_t* timeoutPtr = Dict_getInt(args, String_CONST("timeout"));
  263. uint32_t timeout = (timeoutPtr && *timeoutPtr > 0) ? *timeoutPtr : 0;
  264. char* err = NULL;
  265. struct Address* nodeToQuery = getNode(nodeToQueryStr, ctx, &err, requestAlloc);
  266. uint8_t target[16];
  267. if (!err && AddrTools_parseIp(target, targetStr->bytes)) { err = "parse_target"; }
  268. if (err) {
  269. Dict errDict = Dict_CONST(String_CONST("error"), String_OBJ(String_CONST(err)), NULL);
  270. Admin_sendMessage(&errDict, txid, ctx->admin);
  271. return;
  272. }
  273. struct RouterModule_Promise* rp =
  274. RouterModule_nextHop(nodeToQuery, target, timeout, ctx->module, ctx->allocator);
  275. struct Ping* ping = Allocator_calloc(rp->alloc, sizeof(struct Ping), 1);
  276. Identity_set(ping);
  277. ping->txid = String_clone(txid, rp->alloc);
  278. ping->rp = rp;
  279. ping->ctx = ctx;
  280. rp->userData = ping;
  281. rp->callback = findNodeResponse;
  282. }
  283. void RouterModule_admin_register(struct RouterModule* module,
  284. struct Router* router,
  285. struct Admin* admin,
  286. struct Allocator* alloc)
  287. {
  288. // for improved reporting
  289. alloc = Allocator_child(alloc);
  290. struct Context* ctx = Allocator_clone(alloc, (&(struct Context) {
  291. .admin = admin,
  292. .allocator = alloc,
  293. .module = module,
  294. .router = router
  295. }));
  296. Identity_set(ctx);
  297. Admin_registerFunction("RouterModule_nextHop", nextHop, ctx, true,
  298. ((struct Admin_FunctionArg[]) {
  299. { .name = "nodeToQuery", .required = 1, .type = "String" },
  300. { .name = "target", .required = 1, .type = "String" },
  301. { .name = "timeout", .required = 0, .type = "Int" }
  302. }), admin);
  303. Admin_registerFunction("RouterModule_lookup", lookup, ctx, true,
  304. ((struct Admin_FunctionArg[]) {
  305. { .name = "address", .required = 1, .type = "String" }
  306. }), admin);
  307. Admin_registerFunction("RouterModule_pingNode", pingNode, ctx, true,
  308. ((struct Admin_FunctionArg[]) {
  309. { .name = "path", .required = 1, .type = "String" },
  310. { .name = "timeout", .required = 0, .type = "Int" },
  311. }), admin);
  312. Admin_registerFunction("RouterModule_getPeers", getPeers, ctx, true,
  313. ((struct Admin_FunctionArg[]) {
  314. { .name = "path", .required = 1, .type = "String" },
  315. { .name = "timeout", .required = 0, .type = "Int" },
  316. { .name = "nearbyPath", .required = 0, .type = "String" }
  317. }), admin);
  318. Admin_registerFunction("RouterModule_findNode", findNode, ctx, true,
  319. ((struct Admin_FunctionArg[]) {
  320. { .name = "nodeToQuery", .required = 1, .type = "String" },
  321. { .name = "target", .required = 1, .type = "String" },
  322. { .name = "timeout", .required = 0, .type = "Int" }
  323. }), admin);
  324. }