NodeStore_admin.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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 "crypto/Key.h"
  20. #include "dht/dhtcore/Node.h"
  21. #include "dht/dhtcore/NodeStore.h"
  22. #include "dht/dhtcore/NodeStore_admin.h"
  23. #include "memory/Allocator.h"
  24. #include "switch/EncodingScheme.h"
  25. #include "util/AddrTools.h"
  26. #include "util/version/Version.h"
  27. struct Context {
  28. struct Admin* admin;
  29. struct Allocator* alloc;
  30. struct NodeStore* store;
  31. Identity
  32. };
  33. #define ENTRIES_PER_PAGE 4
  34. static void dumpTable(Dict* args, void* vcontext, String* txid, struct Allocator* requestAlloc)
  35. {
  36. struct Context* ctx = Identity_check((struct Context*) vcontext);
  37. int64_t* page = Dict_getIntC(args, "page");
  38. int ctr = (page) ? *page * ENTRIES_PER_PAGE : 0;
  39. Dict* out = Dict_new(requestAlloc);
  40. List* table = List_new(requestAlloc);
  41. struct Node_Two* nn = NULL;
  42. for (int i = 0; i < ctr+ENTRIES_PER_PAGE; i++) {
  43. nn = NodeStore_getNextNode(ctx->store, nn);
  44. if (!nn) { break; }
  45. if (i < ctr) { continue; }
  46. Dict* nodeDict = Dict_new(requestAlloc);
  47. String* ip = String_newBinary(NULL, 39, requestAlloc);
  48. Address_printIp(ip->bytes, &nn->address);
  49. Dict_putStringC(nodeDict, "ip", ip, requestAlloc);
  50. String* addr = Address_toStringKey(&nn->address, requestAlloc);
  51. Dict_putStringC(nodeDict, "addr", addr, requestAlloc);
  52. String* path = String_newBinary(NULL, 19, requestAlloc);
  53. AddrTools_printPath(path->bytes, nn->address.path);
  54. Dict_putStringC(nodeDict, "path", path, requestAlloc);
  55. Dict_putIntC(nodeDict, "link", Node_getCost(nn), requestAlloc);
  56. Dict_putIntC(nodeDict, "version", nn->address.protocolVersion, requestAlloc);
  57. Dict_putIntC(nodeDict,
  58. "time",
  59. NodeStore_timeSinceLastPing(ctx->store, nn),
  60. requestAlloc);
  61. Dict_putIntC(nodeDict,
  62. "bucket",
  63. NodeStore_bucketForAddr(ctx->store->selfAddress, &nn->address),
  64. requestAlloc);
  65. List_addDict(table, nodeDict, requestAlloc);
  66. }
  67. Dict_putListC(out, "routingTable", table, requestAlloc);
  68. if (nn) {
  69. Dict_putIntC(out, "more", 1, requestAlloc);
  70. }
  71. Dict_putIntC(out, "count", ctx->store->nodeCount, requestAlloc);
  72. Dict_putIntC(out, "peers", ctx->store->peerCount, requestAlloc);
  73. Dict_putStringC(out, "deprecation",
  74. String_CONST("ip,path,version will soon be removed"), requestAlloc);
  75. Admin_sendMessage(out, txid, ctx->admin);
  76. }
  77. static int linkCount(struct Node_Two* parent)
  78. {
  79. struct Node_Link* link = NULL;
  80. int i = 0;
  81. do {
  82. link = NodeStore_nextLink(parent, link);
  83. i++;
  84. } while (link);
  85. return i;
  86. }
  87. static struct Node_Link* getLinkByNum(struct Node_Two* parent, int linkNum)
  88. {
  89. struct Node_Link* link = NULL;
  90. for (int i = 0; i <= linkNum; i++) {
  91. link = NodeStore_nextLink(parent, link);
  92. if (!link) { break; }
  93. }
  94. return link;
  95. }
  96. static void getLink(Dict* args, void* vcontext, String* txid, struct Allocator* alloc)
  97. {
  98. struct Context* ctx = Identity_check((struct Context*) vcontext);
  99. Dict* ret = Dict_new(alloc);
  100. Dict* result = Dict_new(alloc);
  101. Dict_putDict(ret, String_new("result", alloc), result, alloc);
  102. Dict_putString(ret, String_new("error", alloc), String_new("none", alloc), alloc);
  103. struct Node_Link* link = NULL;
  104. struct Node_Two* node = NULL;
  105. String* ipStr = Dict_getString(args, String_new("parent", alloc));
  106. int64_t* linkNum = Dict_getInt(args, String_new("linkNum", alloc));
  107. if (ipStr && ipStr->len) {
  108. uint8_t ip[16];
  109. if (AddrTools_parseIp(ip, ipStr->bytes)) {
  110. Dict_remove(ret, String_CONST("result"));
  111. Dict_putString(ret,
  112. String_new("error", alloc),
  113. String_new("parse_parent", alloc),
  114. alloc);
  115. Admin_sendMessage(ret, txid, ctx->admin);
  116. return;
  117. } else if (!(node = NodeStore_nodeForAddr(ctx->store, ip))) {
  118. Dict_putString(ret,
  119. String_new("error", alloc),
  120. String_new("not_found", alloc),
  121. alloc);
  122. Admin_sendMessage(ret, txid, ctx->admin);
  123. return;
  124. } else if (!(link = getLinkByNum(node, *linkNum))) {
  125. Dict_putString(ret,
  126. String_new("error", alloc),
  127. String_new("unknown", alloc),
  128. alloc);
  129. Admin_sendMessage(ret, txid, ctx->admin);
  130. return;
  131. }
  132. } else {
  133. for (int i = 0; i <= *linkNum; i++) {
  134. link = NodeStore_getNextLink(ctx->store, link);
  135. if (!link) { break; }
  136. }
  137. if (!link) {
  138. Dict_putString(ret,
  139. String_new("error", alloc),
  140. String_new("not_found", alloc),
  141. alloc);
  142. Admin_sendMessage(ret, txid, ctx->admin);
  143. return;
  144. }
  145. }
  146. Dict_putInt(result,
  147. String_new("inverseLinkEncodingFormNumber", alloc),
  148. link->inverseLinkEncodingFormNumber,
  149. alloc);
  150. Dict_putInt(result, String_new("linkCost", alloc), link->linkCost, alloc);
  151. Dict_putInt(result, String_new("isOneHop", alloc), Node_isOneHopLink(link), alloc);
  152. int bestParent = (Node_getBestParent(link->child) == link);
  153. Dict_putInt(result, String_new("bestParent", alloc), bestParent, alloc);
  154. String* cannonicalLabel = String_newBinary(NULL, 19, alloc);
  155. AddrTools_printPath(cannonicalLabel->bytes, link->cannonicalLabel);
  156. Dict_putString(result, String_new("cannonicalLabel", alloc), cannonicalLabel, alloc);
  157. String* parent = Address_toStringKey(&link->parent->address, alloc);
  158. Dict_putString(result, String_new("parent", alloc), parent, alloc);
  159. String* child = Address_toStringKey(&link->child->address, alloc);
  160. Dict_putString(result, String_new("child", alloc), child, alloc);
  161. Admin_sendMessage(ret, txid, ctx->admin);
  162. }
  163. static void nodeForAddr(Dict* args, void* vcontext, String* txid, struct Allocator* alloc)
  164. {
  165. struct Context* ctx = Identity_check((struct Context*) vcontext);
  166. Dict* ret = Dict_new(alloc);
  167. Dict* result = Dict_new(alloc);
  168. Dict_putDict(ret, String_new("result", alloc), result, alloc);
  169. Dict_putString(ret, String_new("error", alloc), String_new("none", alloc), alloc);
  170. // no ipStr specified --> return self-node
  171. struct Node_Two* node = ctx->store->selfNode;
  172. String* ipStr = Dict_getString(args, String_new("ip", alloc));
  173. uint8_t ip[16];
  174. while (ipStr) {
  175. if (AddrTools_parseIp(ip, ipStr->bytes)) {
  176. Dict_remove(ret, String_CONST("result"));
  177. Dict_putString(ret,
  178. String_new("error", alloc),
  179. String_new("parse_ip", alloc),
  180. alloc);
  181. } else if (!(node = NodeStore_nodeForAddr(ctx->store, ip))) {
  182. // not found
  183. } else {
  184. break;
  185. }
  186. Admin_sendMessage(ret, txid, ctx->admin);
  187. return;
  188. }
  189. Dict_putInt(result, String_new("protocolVersion", alloc), node->address.protocolVersion, alloc);
  190. String* key = Key_stringify(node->address.key, alloc);
  191. Dict_putString(result, String_new("key", alloc), key, alloc);
  192. uint32_t count = linkCount(node);
  193. Dict_putInt(result, String_new("linkCount", alloc), count, alloc);
  194. Dict_putInt(result, String_new("cost", alloc), Node_getCost(node), alloc);
  195. List* encScheme = EncodingScheme_asList(node->encodingScheme, alloc);
  196. Dict_putList(result, String_new("encodingScheme", alloc), encScheme, alloc);
  197. Dict* bestParent = Dict_new(alloc);
  198. String* parentIp = String_newBinary(NULL, 39, alloc);
  199. AddrTools_printIp(parentIp->bytes, Node_getBestParent(node)->parent->address.ip6.bytes);
  200. Dict_putStringC(bestParent, "ip", parentIp, alloc);
  201. String* parentChildLabel = String_newBinary(NULL, 19, alloc);
  202. AddrTools_printPath(parentChildLabel->bytes, Node_getBestParent(node)->cannonicalLabel);
  203. Dict_putStringC(bestParent, "parentChildLabel", parentChildLabel, alloc);
  204. int isOneHop = Node_isOneHopLink(Node_getBestParent(node));
  205. Dict_putIntC(bestParent, "isOneHop", isOneHop, alloc);
  206. Dict_putDictC(result, "bestParent", bestParent, alloc);
  207. String* bestLabel = String_newBinary(NULL, 19, alloc);
  208. AddrTools_printPath(bestLabel->bytes, node->address.path);
  209. Dict_putStringC(result, "routeLabel", bestLabel, alloc);
  210. Admin_sendMessage(ret, txid, ctx->admin);
  211. }
  212. static void getRouteLabel(Dict* args, void* vcontext, String* txid, struct Allocator* requestAlloc)
  213. {
  214. struct Context* ctx = Identity_check((struct Context*) vcontext);
  215. char* err = NULL;
  216. String* pathToParentS = Dict_getStringC(args, "pathToParent");
  217. uint64_t pathToParent = 0;
  218. if (pathToParentS->len != 19 || AddrTools_parsePath(&pathToParent, pathToParentS->bytes)) {
  219. err = "parse_pathToParent";
  220. }
  221. String* pathParentToChildS = Dict_getStringC(args, "pathParentToChild");
  222. uint64_t pathParentToChild = 0;
  223. if (pathParentToChildS->len != 19
  224. || AddrTools_parsePath(&pathParentToChild, pathParentToChildS->bytes))
  225. {
  226. err = "parse_pathParentToChild";
  227. }
  228. uint64_t label = UINT64_MAX;
  229. if (!err) {
  230. label = NodeStore_getRouteLabel(ctx->store, pathToParent, pathParentToChild);
  231. err = NodeStore_getRouteLabel_strerror(label);
  232. }
  233. Dict* response = Dict_new(requestAlloc);
  234. if (!err) {
  235. String* printedPath = String_newBinary(NULL, 19, requestAlloc);
  236. AddrTools_printPath(printedPath->bytes, label);
  237. Dict_putString(response, String_new("result", requestAlloc), printedPath, requestAlloc);
  238. Dict_putString(response,
  239. String_new("error", requestAlloc),
  240. String_new("none", requestAlloc),
  241. requestAlloc);
  242. Admin_sendMessage(response, txid, ctx->admin);
  243. } else {
  244. Dict_putString(response,
  245. String_new("error", requestAlloc),
  246. String_new(err, requestAlloc),
  247. requestAlloc);
  248. Admin_sendMessage(response, txid, ctx->admin);
  249. }
  250. }
  251. static void getFullVerify(Dict* args, void* vcontext, String* txid, struct Allocator* requestAlloc)
  252. {
  253. struct Context* ctx = Identity_check((struct Context*) vcontext);
  254. Dict* response = Dict_new(requestAlloc);
  255. Dict_putStringCC(response, "error", "none", requestAlloc);
  256. Dict_putIntC(response, "fullVerify", NodeStore_getFullVerify(ctx->store), requestAlloc);
  257. Admin_sendMessage(response, txid, ctx->admin);
  258. }
  259. static void setFullVerify(Dict* args, void* vcontext, String* txid, struct Allocator* requestAlloc)
  260. {
  261. struct Context* ctx = Identity_check((struct Context*) vcontext);
  262. int64_t* fv = Dict_getIntC(args, "fullVerify");
  263. NodeStore_setFullVerify(ctx->store, (int) *fv);
  264. Dict* response = Dict_new(requestAlloc);
  265. Dict_putStringCC(response, "error", "none", requestAlloc);
  266. Admin_sendMessage(response, txid, ctx->admin);
  267. }
  268. void NodeStore_admin_register(struct NodeStore* nodeStore,
  269. struct Admin* admin,
  270. struct Allocator* alloc)
  271. {
  272. struct Context* ctx = Allocator_clone(alloc, (&(struct Context) {
  273. .admin = admin,
  274. .alloc = alloc,
  275. .store = nodeStore
  276. }));
  277. Identity_set(ctx);
  278. Admin_registerFunction("NodeStore_dumpTable", dumpTable, ctx, false,
  279. ((struct Admin_FunctionArg[]) {
  280. { .name = "page", .required = true, .type = "Int" },
  281. }), admin);
  282. Admin_registerFunction("NodeStore_getLink", getLink, ctx, false,
  283. ((struct Admin_FunctionArg[]) {
  284. { .name = "parent", .required = false, .type = "String" },
  285. { .name = "linkNum", .required = true, .type = "Int" },
  286. }), admin);
  287. Admin_registerFunction("NodeStore_nodeForAddr", nodeForAddr, ctx, false,
  288. ((struct Admin_FunctionArg[]) {
  289. { .name = "ip", .required = false, .type = "String" },
  290. }), admin);
  291. Admin_registerFunction("NodeStore_getRouteLabel", getRouteLabel, ctx, true,
  292. ((struct Admin_FunctionArg[]) {
  293. { .name = "pathToParent", .required = true, .type = "String" },
  294. { .name = "pathParentToChild", .required = true, .type = "String" }
  295. }), admin);
  296. Admin_registerFunction("NodeStore_setFullVerify", setFullVerify, ctx, true,
  297. ((struct Admin_FunctionArg[]) {
  298. { .name = "fullVerify", .required = true, .type = "Int" },
  299. }), admin);
  300. Admin_registerFunction("NodeStore_getFullVerify", getFullVerify, ctx, false, NULL, admin);
  301. }