RouterModule_admin.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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/ReplySerializer.h"
  22. #include "dht/Address.h"
  23. #include "dht/CJDHTConstants.h"
  24. #include "memory/Allocator.h"
  25. #include "util/AddrTools.h"
  26. struct Context {
  27. struct Admin* admin;
  28. struct Allocator* allocator;
  29. struct RouterModule* router;
  30. Identity
  31. };
  32. static void lookup(Dict* args, void* vcontext, String* txid, struct Allocator* requestAlloc)
  33. {
  34. struct Context* ctx = vcontext;
  35. String* addrStr = Dict_getString(args, String_CONST("address"));
  36. char* err = NULL;
  37. uint8_t addr[16];
  38. uint8_t resultBuff[60];
  39. char* result = (char*) resultBuff;
  40. if (addrStr->len != 39) {
  41. err = "address wrong length";
  42. } else if (AddrTools_parseIp(addr, (uint8_t*) addrStr->bytes)) {
  43. err = "failed to parse address";
  44. } else {
  45. struct Node_Two* n = RouterModule_lookup(addr, ctx->router);
  46. if (!n) {
  47. result = "not found";
  48. } else if (Bits_memcmp(addr, n->address.ip6.bytes, 16)) {
  49. Address_print(resultBuff, &n->address);
  50. } else {
  51. AddrTools_printPath(resultBuff, n->address.path);
  52. }
  53. }
  54. Dict response = Dict_CONST(
  55. String_CONST("error"), String_OBJ(String_CONST((err) ? err : "none")), Dict_CONST(
  56. String_CONST("result"), String_OBJ(String_CONST(result)), NULL
  57. ));
  58. Admin_sendMessage(&response, txid, ctx->admin);
  59. }
  60. struct Ping
  61. {
  62. String* txid;
  63. struct RouterModule_Promise* rp;
  64. struct Context* ctx;
  65. Identity
  66. };
  67. static void pingResponse(struct RouterModule_Promise* promise,
  68. uint32_t lag,
  69. struct Address* from,
  70. Dict* responseDict)
  71. {
  72. struct Ping* ping = Identity_check((struct Ping*)promise->userData);
  73. struct Allocator* tempAlloc = promise->alloc;
  74. Dict* resp = Dict_new(tempAlloc);
  75. String* versionBin = Dict_getString(responseDict, CJDHTConstants_VERSION);
  76. if (versionBin && versionBin->len == 20) {
  77. String* versionStr = String_newBinary(NULL, 40, tempAlloc);
  78. Hex_encode(versionStr->bytes, 40, versionBin->bytes, 20);
  79. Dict_putString(resp, String_CONST("version"), versionStr, tempAlloc);
  80. } else {
  81. Dict_putString(resp, String_CONST("version"), String_CONST("unknown"), tempAlloc);
  82. }
  83. String* result = (responseDict) ? String_CONST("pong") : String_CONST("timeout");
  84. Dict_putString(resp, String_CONST("result"), result, tempAlloc);
  85. int64_t* protocolVersion = Dict_getInt(responseDict, CJDHTConstants_PROTOCOL);
  86. if (protocolVersion) {
  87. Dict_putInt(resp, String_CONST("protocol"), *protocolVersion, tempAlloc);
  88. }
  89. Dict_putInt(resp, String_CONST("ms"), lag, tempAlloc);
  90. if (from) {
  91. uint8_t fromStr[60] = "";
  92. Address_print(fromStr, from);
  93. Dict_putString(resp, String_CONST("from"), String_new(fromStr, tempAlloc), tempAlloc);
  94. }
  95. Admin_sendMessage(resp, ping->txid, ping->ctx->admin);
  96. }
  97. static void getPeersResponse(struct RouterModule_Promise* promise,
  98. uint32_t lag,
  99. struct Address* from,
  100. Dict* responseDict)
  101. {
  102. struct Ping* ping = Identity_check((struct Ping*)promise->userData);
  103. Dict* out = Dict_new(promise->alloc);
  104. String* result = (responseDict) ? String_CONST("peers") : String_CONST("timeout");
  105. Dict_putString(out, String_CONST("result"), result, promise->alloc);
  106. if (responseDict) {
  107. struct Address_List* addrs =
  108. ReplySerializer_parse(from, responseDict, NULL, promise->alloc);
  109. List* nodes = NULL;
  110. for (int i = 0; i < addrs->length; i++) {
  111. String* addr = Address_toString(&addrs->elems[i], promise->alloc);
  112. nodes = List_addString(nodes, addr, promise->alloc);
  113. }
  114. Dict_putList(out, String_CONST("peers"), nodes, promise->alloc);
  115. }
  116. Dict_putInt(out, String_CONST("ms"), lag, promise->alloc);
  117. Dict_putString(out, String_CONST("error"), String_CONST("none"), promise->alloc);
  118. Admin_sendMessage(out, ping->txid, ping->ctx->admin);
  119. }
  120. static struct Address* getNode(String* pathStr,
  121. struct Context* ctx,
  122. char** errOut,
  123. struct Allocator* alloc)
  124. {
  125. struct Address addr = {.path=0};
  126. if (pathStr->len == 19 && !AddrTools_parsePath(&addr.path, pathStr->bytes)) {
  127. struct Node_Two* n = RouterModule_nodeForPath(addr.path, ctx->router);
  128. if (!n) {
  129. *errOut = "not_found";
  130. return NULL;
  131. } else {
  132. Bits_memcpyConst(&addr, &n->address, sizeof(struct Address));
  133. }
  134. } else if (pathStr->len == 39 && !AddrTools_parseIp(addr.ip6.bytes, pathStr->bytes)) {
  135. struct Node_Two* n = RouterModule_lookup(addr.ip6.bytes, ctx->router);
  136. if (!n || Bits_memcmp(addr.ip6.bytes, n->address.ip6.bytes, 16)) {
  137. *errOut = "not_found";
  138. return NULL;
  139. } else {
  140. Bits_memcpyConst(&addr, &n->address, sizeof(struct Address));
  141. }
  142. } else {
  143. struct Address* a = Address_fromString(pathStr, alloc);
  144. if (a) { return a; }
  145. *errOut = "parse_path";
  146. return NULL;
  147. }
  148. return Allocator_clone(alloc, &addr);
  149. }
  150. static void pingNode(Dict* args, void* vctx, String* txid, struct Allocator* requestAlloc)
  151. {
  152. struct Context* ctx = Identity_check((struct Context*) vctx);
  153. String* pathStr = Dict_getString(args, String_CONST("path"));
  154. int64_t* timeoutPtr = Dict_getInt(args, String_CONST("timeout"));
  155. uint32_t timeout = (timeoutPtr && *timeoutPtr > 0) ? *timeoutPtr : 0;
  156. char* err = NULL;
  157. struct Address* addr = getNode(pathStr, ctx, &err, requestAlloc);
  158. if (err) {
  159. Dict errDict = Dict_CONST(String_CONST("error"), String_OBJ(String_CONST(err)), NULL);
  160. Admin_sendMessage(&errDict, txid, ctx->admin);
  161. return;
  162. }
  163. struct RouterModule_Promise* rp =
  164. RouterModule_pingNode(addr, timeout, ctx->router, ctx->allocator);
  165. struct Ping* ping = Allocator_calloc(rp->alloc, sizeof(struct Ping), 1);
  166. Identity_set(ping);
  167. ping->txid = String_clone(txid, rp->alloc);
  168. ping->rp = rp;
  169. ping->ctx = ctx;
  170. rp->userData = ping;
  171. rp->callback = pingResponse;
  172. }
  173. static void getPeers(Dict* args, void* vctx, String* txid, struct Allocator* requestAlloc)
  174. {
  175. struct Context* ctx = Identity_check((struct Context*) vctx);
  176. String* nearbyLabelStr = Dict_getString(args, String_CONST("nearbyPath"));
  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. uint64_t nearbyLabel = 0;
  183. if (!err && nearbyLabelStr) {
  184. if (nearbyLabelStr->len != 19 || AddrTools_parsePath(&nearbyLabel, nearbyLabelStr->bytes)) {
  185. err = "parse_nearbyLabel";
  186. }
  187. }
  188. if (err) {
  189. Dict errDict = Dict_CONST(String_CONST("error"), String_OBJ(String_CONST(err)), NULL);
  190. Admin_sendMessage(&errDict, txid, ctx->admin);
  191. return;
  192. }
  193. struct RouterModule_Promise* rp =
  194. RouterModule_getPeers(addr, nearbyLabel, timeout, ctx->router, ctx->allocator);
  195. struct Ping* ping = Allocator_calloc(rp->alloc, sizeof(struct Ping), 1);
  196. Identity_set(ping);
  197. ping->txid = String_clone(txid, rp->alloc);
  198. ping->rp = rp;
  199. ping->ctx = ctx;
  200. rp->userData = ping;
  201. rp->callback = getPeersResponse;
  202. }
  203. void RouterModule_admin_register(struct RouterModule* module,
  204. struct Admin* admin,
  205. struct Allocator* alloc)
  206. {
  207. // for improved reporting
  208. alloc = Allocator_child(alloc);
  209. struct Context* ctx = Allocator_clone(alloc, (&(struct Context) {
  210. .admin = admin,
  211. .allocator = alloc,
  212. .router = module
  213. }));
  214. Identity_set(ctx);
  215. Admin_registerFunction("RouterModule_lookup", lookup, ctx, true,
  216. ((struct Admin_FunctionArg[]) {
  217. { .name = "address", .required = 1, .type = "String" }
  218. }), admin);
  219. Admin_registerFunction("RouterModule_pingNode", pingNode, ctx, true,
  220. ((struct Admin_FunctionArg[]) {
  221. { .name = "path", .required = 1, .type = "String" },
  222. { .name = "timeout", .required = 0, .type = "Int" },
  223. }), admin);
  224. Admin_registerFunction("RouterModule_getPeers", getPeers, ctx, true,
  225. ((struct Admin_FunctionArg[]) {
  226. { .name = "path", .required = 1, .type = "String" },
  227. { .name = "timeout", .required = 0, .type = "Int" },
  228. { .name = "nearbyPath", .required = 0, .type = "String" }
  229. }), admin);
  230. }