NodeStore_admin.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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 "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_getInt(args, String_CONST("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_putString(nodeDict, String_CONST("ip"), ip, requestAlloc);
  50. String* addr = Address_toString(&nn->address, requestAlloc);
  51. Dict_putString(nodeDict, String_CONST("addr"), addr, requestAlloc);
  52. String* path = String_newBinary(NULL, 19, requestAlloc);
  53. AddrTools_printPath(path->bytes, nn->address.path);
  54. Dict_putString(nodeDict, String_CONST("path"), path, requestAlloc);
  55. Dict_putInt(nodeDict, String_CONST("link"), Node_getReach(nn), requestAlloc);
  56. Dict_putInt(nodeDict, String_CONST("version"), nn->address.protocolVersion, requestAlloc);
  57. Dict_putInt(nodeDict,
  58. String_CONST("time"),
  59. NodeStore_timeSinceLastPing(ctx->store, nn),
  60. requestAlloc);
  61. List_addDict(table, nodeDict, requestAlloc);
  62. }
  63. Dict_putList(out, String_CONST("routingTable"), table, requestAlloc);
  64. if (nn) {
  65. Dict_putInt(out, String_CONST("more"), 1, requestAlloc);
  66. }
  67. Dict_putInt(out, String_CONST("count"), ctx->store->nodeCount, requestAlloc);
  68. Dict_putInt(out, String_CONST("peers"), ctx->store->peerCount, requestAlloc);
  69. Dict_putString(out, String_CONST("deprecation"),
  70. String_CONST("ip,path,version will soon be removed"), requestAlloc);
  71. Admin_sendMessage(out, txid, ctx->admin);
  72. }
  73. static int linkCount(struct Node_Two* parent)
  74. {
  75. struct Node_Link* link = NULL;
  76. int i = 0;
  77. do {
  78. link = NodeStore_nextLink(parent, link);
  79. i++;
  80. } while (link);
  81. return i;
  82. }
  83. static struct Node_Link* getLinkByNum(struct Node_Two* parent, int linkNum)
  84. {
  85. struct Node_Link* link = NULL;
  86. for (int i = 0; i <= linkNum; i++) {
  87. link = NodeStore_nextLink(parent, link);
  88. if (!link) { break; }
  89. }
  90. return link;
  91. }
  92. static void getLink(Dict* args, void* vcontext, String* txid, struct Allocator* alloc)
  93. {
  94. struct Context* ctx = Identity_check((struct Context*) vcontext);
  95. Dict* ret = Dict_new(alloc);
  96. Dict* result = Dict_new(alloc);
  97. Dict_putDict(ret, String_new("result", alloc), result, alloc);
  98. Dict_putString(ret, String_new("error", alloc), String_new("none", alloc), alloc);
  99. struct Node_Link* link = NULL;
  100. struct Node_Two* node = NULL;
  101. String* ipStr = Dict_getString(args, String_new("parent", alloc));
  102. int64_t* linkNum = Dict_getInt(args, String_new("linkNum", alloc));
  103. uint8_t ip[16];
  104. if (ipStr->len != 39 || AddrTools_parseIp(ip, ipStr->bytes)) {
  105. Dict_remove(ret, String_CONST("result"));
  106. Dict_putString(ret,
  107. String_new("error", alloc),
  108. String_new("parse_parent", alloc),
  109. alloc);
  110. } else if (!(node = NodeStore_nodeForAddr(ctx->store, ip))) {
  111. Dict_putString(ret,
  112. String_new("error", alloc),
  113. String_new("not_found", alloc),
  114. alloc);
  115. } else if ((link = getLinkByNum(node, *linkNum))) {
  116. Dict_putInt(result,
  117. String_new("inverseLinkEncodingFormNumber", alloc),
  118. link->inverseLinkEncodingFormNumber,
  119. alloc);
  120. Dict_putInt(result, String_new("linkState", alloc), link->linkState, alloc);
  121. Dict_putInt(result, String_new("isOneHop", alloc), Node_isOneHopLink(link), alloc);
  122. String* cannonicalLabel = String_newBinary(NULL, 19, alloc);
  123. AddrTools_printPath(cannonicalLabel->bytes, link->cannonicalLabel);
  124. Dict_putString(result, String_new("cannonicalLabel", alloc), cannonicalLabel, alloc);
  125. String* parent = String_newBinary(NULL, 39, alloc);
  126. AddrTools_printIp(parent->bytes, link->parent->address.ip6.bytes);
  127. Dict_putString(result, String_new("parent", alloc), parent, alloc);
  128. String* child = String_newBinary(NULL, 39, alloc);
  129. AddrTools_printIp(child->bytes, link->child->address.ip6.bytes);
  130. Dict_putString(result, String_new("child", alloc), child, alloc);
  131. }
  132. Admin_sendMessage(ret, txid, ctx->admin);
  133. }
  134. static void nodeForAddr(Dict* args, void* vcontext, String* txid, struct Allocator* alloc)
  135. {
  136. struct Context* ctx = Identity_check((struct Context*) vcontext);
  137. Dict* ret = Dict_new(alloc);
  138. Dict* result = Dict_new(alloc);
  139. Dict_putDict(ret, String_new("result", alloc), result, alloc);
  140. Dict_putString(ret, String_new("error", alloc), String_new("none", alloc), alloc);
  141. // no ipStr specified --> return self-node
  142. struct Node_Two* node = ctx->store->selfNode;
  143. String* ipStr = Dict_getString(args, String_new("ip", alloc));
  144. uint8_t ip[16];
  145. while (ipStr) {
  146. if (ipStr->len != 39 || AddrTools_parseIp(ip, ipStr->bytes)) {
  147. Dict_remove(ret, String_CONST("result"));
  148. Dict_putString(ret,
  149. String_new("error", alloc),
  150. String_new("parse_ip", alloc),
  151. alloc);
  152. } else if (!(node = NodeStore_nodeForAddr(ctx->store, ip))) {
  153. // not found
  154. } else {
  155. break;
  156. }
  157. Admin_sendMessage(ret, txid, ctx->admin);
  158. return;
  159. }
  160. Dict_putInt(result, String_new("protocolVersion", alloc), node->address.protocolVersion, alloc);
  161. String* key = Key_stringify(node->address.key, alloc);
  162. Dict_putString(result, String_new("key", alloc), key, alloc);
  163. uint32_t count = linkCount(node);
  164. Dict_putInt(result, String_new("linkCount", alloc), count, alloc);
  165. Dict_putInt(result, String_new("reach", alloc), Node_getReach(node), alloc);
  166. List* encScheme = EncodingScheme_asList(node->encodingScheme, alloc);
  167. Dict_putList(result, String_new("encodingScheme", alloc), encScheme, alloc);
  168. Dict* bestParent = Dict_new(alloc);
  169. String* parentIp = String_newBinary(NULL, 39, alloc);
  170. AddrTools_printIp(parentIp->bytes, Node_getBestParent(node)->parent->address.ip6.bytes);
  171. Dict_putString(bestParent, String_CONST("ip"), parentIp, alloc);
  172. String* parentChildLabel = String_newBinary(NULL, 19, alloc);
  173. AddrTools_printPath(parentChildLabel->bytes, Node_getBestParent(node)->cannonicalLabel);
  174. Dict_putString(bestParent, String_CONST("parentChildLabel"), parentChildLabel, alloc);
  175. Dict_putDict(result, String_CONST("bestParent"), bestParent, alloc);
  176. String* bestLabel = String_newBinary(NULL, 19, alloc);
  177. AddrTools_printPath(bestLabel->bytes, node->address.path);
  178. Dict_putString(result, String_CONST("routeLabel"), bestLabel, alloc);
  179. Admin_sendMessage(ret, txid, ctx->admin);
  180. }
  181. static void getRouteLabel(Dict* args, void* vcontext, String* txid, struct Allocator* requestAlloc)
  182. {
  183. struct Context* ctx = Identity_check((struct Context*) vcontext);
  184. char* err = NULL;
  185. String* pathToParentS = Dict_getString(args, String_CONST("pathToParent"));
  186. uint64_t pathToParent = 0;
  187. if (pathToParentS->len != 19 || AddrTools_parsePath(&pathToParent, pathToParentS->bytes)) {
  188. err = "parse_pathToParent";
  189. }
  190. String* pathParentToChildS = Dict_getString(args, String_CONST("pathParentToChild"));
  191. uint64_t pathParentToChild = 0;
  192. if (pathParentToChildS->len != 19
  193. || AddrTools_parsePath(&pathParentToChild, pathParentToChildS->bytes))
  194. {
  195. err = "parse_pathParentToChild";
  196. }
  197. uint64_t label = UINT64_MAX;
  198. if (!err) {
  199. label = NodeStore_getRouteLabel(ctx->store, pathToParent, pathParentToChild);
  200. err = NodeStore_getRouteLabel_strerror(label);
  201. }
  202. Dict* response = Dict_new(requestAlloc);
  203. if (!err) {
  204. String* printedPath = String_newBinary(NULL, 19, requestAlloc);
  205. AddrTools_printPath(printedPath->bytes, label);
  206. Dict_putString(response, String_new("result", requestAlloc), printedPath, requestAlloc);
  207. Dict_putString(response,
  208. String_new("error", requestAlloc),
  209. String_new("none", requestAlloc),
  210. requestAlloc);
  211. Admin_sendMessage(response, txid, ctx->admin);
  212. } else {
  213. Dict_putString(response,
  214. String_new("error", requestAlloc),
  215. String_new(err, requestAlloc),
  216. requestAlloc);
  217. Admin_sendMessage(response, txid, ctx->admin);
  218. }
  219. }
  220. void NodeStore_admin_register(struct NodeStore* nodeStore,
  221. struct Admin* admin,
  222. struct Allocator* alloc)
  223. {
  224. struct Context* ctx = Allocator_clone(alloc, (&(struct Context) {
  225. .admin = admin,
  226. .alloc = alloc,
  227. .store = nodeStore
  228. }));
  229. Identity_set(ctx);
  230. Admin_registerFunction("NodeStore_dumpTable", dumpTable, ctx, false,
  231. ((struct Admin_FunctionArg[]) {
  232. { .name = "page", .required = 1, .type = "Int" },
  233. }), admin);
  234. Admin_registerFunction("NodeStore_getLink", getLink, ctx, false,
  235. ((struct Admin_FunctionArg[]) {
  236. { .name = "parent", .required = 1, .type = "String" },
  237. { .name = "linkNum", .required = 1, .type = "Int" },
  238. }), admin);
  239. Admin_registerFunction("NodeStore_nodeForAddr", nodeForAddr, ctx, false,
  240. ((struct Admin_FunctionArg[]) {
  241. { .name = "ip", .required = 0, .type = "String" },
  242. }), admin);
  243. Admin_registerFunction("NodeStore_getRouteLabel", getRouteLabel, ctx, true,
  244. ((struct Admin_FunctionArg[]) {
  245. { .name = "pathToParent", .required = 1, .type = "String" },
  246. { .name = "pathParentToChild", .required = 1, .type = "String" }
  247. }), admin);
  248. }