NodeStore_admin.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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. #define string_strcpy
  16. #define string_strlen
  17. #include "admin/Admin.h"
  18. #include "benc/Dict.h"
  19. #include "benc/String.h"
  20. #include "benc/Int.h"
  21. #include "crypto/Key.h"
  22. #include "dht/dhtcore/Node.h"
  23. #include "dht/dhtcore/NodeStore.h"
  24. #include "dht/dhtcore/NodeStore_admin.h"
  25. #include "memory/Allocator.h"
  26. #include "switch/EncodingScheme.h"
  27. #include "util/AddrTools.h"
  28. #include "util/version/Version.h"
  29. #include "util/platform/libc/string.h"
  30. struct Context {
  31. struct Admin* admin;
  32. struct Allocator* alloc;
  33. struct NodeStore* store;
  34. Identity
  35. };
  36. static void dumpTable_send(struct Context* ctx,
  37. struct List_Item* last,
  38. bool isMore,
  39. String* txid)
  40. {
  41. Dict table = Dict_CONST(String_CONST("routingTable"), List_OBJ(&last), NULL);
  42. if (isMore) {
  43. table = Dict_CONST(String_CONST("more"), Int_OBJ(1), table);
  44. } else {
  45. int count = ctx->store->nodeCount;
  46. table = Dict_CONST(String_CONST("count"), Int_OBJ(count), table);
  47. }
  48. Admin_sendMessage(&table, txid, ctx->admin);
  49. }
  50. #define ENTRIES_PER_PAGE 8
  51. static void dumpTable_addEntries(struct Context* ctx,
  52. int i,
  53. int j,
  54. struct List_Item* last,
  55. String* txid)
  56. {
  57. uint8_t path[20];
  58. uint8_t ip[40];
  59. String* pathStr = &(String) { .len = 19, .bytes = (char*)path };
  60. String* ipStr = &(String) { .len = 39, .bytes = (char*)ip };
  61. Object* link = Int_OBJ(0xFFFFFFFF);
  62. Object* version = Int_OBJ(Version_DEFAULT_ASSUMPTION);
  63. Dict entry = Dict_CONST(
  64. String_CONST("ip"), String_OBJ(ipStr), Dict_CONST(
  65. String_CONST("link"), link, Dict_CONST(
  66. String_CONST("path"), String_OBJ(pathStr), Dict_CONST(
  67. String_CONST("version"), version, NULL
  68. ))));
  69. struct List_Item next = { .next = last, .elem = Dict_OBJ(&entry) };
  70. if (i >= ctx->store->nodeCount || j >= ENTRIES_PER_PAGE) {
  71. dumpTable_send(ctx, last, (j >= ENTRIES_PER_PAGE), txid);
  72. return;
  73. }
  74. struct Node_Two* n = NodeStore_dumpTable(ctx->store, i);
  75. link->as.number = Node_getReach(n);
  76. version->as.number = n->address.protocolVersion;
  77. Address_printIp(ip, &n->address);
  78. AddrTools_printPath(path, n->address.path);
  79. dumpTable_addEntries(ctx, i + 1, j + 1, &next, txid);
  80. }
  81. static void dumpTable(Dict* args, void* vcontext, String* txid, struct Allocator* requestAlloc)
  82. {
  83. struct Context* ctx = Identity_check((struct Context*) vcontext);
  84. int64_t* page = Dict_getInt(args, String_CONST("page"));
  85. int i = (page) ? *page * ENTRIES_PER_PAGE : 0;
  86. dumpTable_addEntries(ctx, i, 0, NULL, txid);
  87. }
  88. static void getLink(Dict* args, void* vcontext, String* txid, struct Allocator* alloc)
  89. {
  90. struct Context* ctx = Identity_check((struct Context*) vcontext);
  91. Dict* ret = Dict_new(alloc);
  92. Dict* result = Dict_new(alloc);
  93. Dict_putDict(ret, String_new("result", alloc), result, alloc);
  94. Dict_putString(ret, String_new("error", alloc), String_new("none", alloc), alloc);
  95. struct Node_Link* link = NULL;
  96. struct Node_Two* node = NULL;
  97. String* ipStr = Dict_getString(args, String_new("parent", alloc));
  98. int64_t* linkNum = Dict_getInt(args, String_new("linkNum", alloc));
  99. uint8_t ip[16];
  100. if (ipStr->len != 39 || AddrTools_parseIp(ip, ipStr->bytes)) {
  101. Dict_remove(ret, String_CONST("result"));
  102. Dict_putString(ret,
  103. String_new("error", alloc),
  104. String_new("parse_parent", alloc),
  105. alloc);
  106. } else if (!(node = NodeStore_nodeForAddr(ctx->store, ip))) {
  107. Dict_putString(ret,
  108. String_new("error", alloc),
  109. String_new("not_found", alloc),
  110. alloc);
  111. } else if ((link = NodeStore_getLink(node, *linkNum))) {
  112. Dict_putInt(result,
  113. String_new("inverseLinkEncodingFormNumber", alloc),
  114. link->inverseLinkEncodingFormNumber,
  115. alloc);
  116. Dict_putInt(result, String_new("linkState", alloc), link->linkState, alloc);
  117. Dict_putInt(result, String_new("isOneHop", alloc), Node_isOneHopLink(link), alloc);
  118. String* cannonicalLabel = String_newBinary(NULL, 19, alloc);
  119. AddrTools_printPath(cannonicalLabel->bytes, link->cannonicalLabel);
  120. Dict_putString(result, String_new("cannonicalLabel", alloc), cannonicalLabel, alloc);
  121. String* parent = String_newBinary(NULL, 39, alloc);
  122. AddrTools_printIp(parent->bytes, link->parent->address.ip6.bytes);
  123. Dict_putString(result, String_new("parent", alloc), parent, alloc);
  124. String* child = String_newBinary(NULL, 39, alloc);
  125. AddrTools_printIp(child->bytes, link->child->address.ip6.bytes);
  126. Dict_putString(result, String_new("child", alloc), child, alloc);
  127. }
  128. Admin_sendMessage(ret, txid, ctx->admin);
  129. }
  130. static void nodeForAddr(Dict* args, void* vcontext, String* txid, struct Allocator* alloc)
  131. {
  132. struct Context* ctx = Identity_check((struct Context*) vcontext);
  133. Dict* ret = Dict_new(alloc);
  134. Dict* result = Dict_new(alloc);
  135. Dict_putDict(ret, String_new("result", alloc), result, alloc);
  136. Dict_putString(ret, String_new("error", alloc), String_new("none", alloc), alloc);
  137. // no ipStr specified --> return self-node
  138. struct Node_Two* node = ctx->store->selfNode;
  139. String* ipStr = Dict_getString(args, String_new("ip", alloc));
  140. uint8_t ip[16];
  141. while (ipStr) {
  142. if (ipStr->len != 39 || AddrTools_parseIp(ip, ipStr->bytes)) {
  143. Dict_remove(ret, String_CONST("result"));
  144. Dict_putString(ret,
  145. String_new("error", alloc),
  146. String_new("parse_ip", alloc),
  147. alloc);
  148. } else if (!(node = NodeStore_nodeForAddr(ctx->store, ip))) {
  149. // not found
  150. } else {
  151. break;
  152. }
  153. Admin_sendMessage(ret, txid, ctx->admin);
  154. return;
  155. }
  156. Dict_putInt(result, String_new("protocolVersion", alloc), node->address.protocolVersion, alloc);
  157. String* key = Key_stringify(node->address.key, alloc);
  158. Dict_putString(result, String_new("key", alloc), key, alloc);
  159. uint32_t linkCount = NodeStore_linkCount(node);
  160. Dict_putInt(result, String_new("linkCount", alloc), linkCount, alloc);
  161. Dict_putInt(result, String_new("reach", alloc), Node_getReach(node), alloc);
  162. List* encScheme = EncodingScheme_asList(node->encodingScheme, alloc);
  163. Dict_putList(result, String_new("encodingScheme", alloc), encScheme, alloc);
  164. Dict* bestParent = Dict_new(alloc);
  165. String* parentIp = String_newBinary(NULL, 39, alloc);
  166. AddrTools_printIp(parentIp->bytes, node->bestParent->parent->address.ip6.bytes);
  167. Dict_putString(bestParent, String_CONST("ip"), parentIp, alloc);
  168. String* parentChildLabel = String_newBinary(NULL, 19, alloc);
  169. AddrTools_printPath(parentChildLabel->bytes, node->bestParent->cannonicalLabel);
  170. Dict_putString(bestParent, String_CONST("parentChildLabel"), parentChildLabel, alloc);
  171. Dict_putDict(result, String_CONST("bestParent"), bestParent, alloc);
  172. String* bestLabel = String_newBinary(NULL, 19, alloc);
  173. AddrTools_printPath(bestLabel->bytes, node->address.path);
  174. Dict_putString(result, String_CONST("routeLabel"), bestLabel, alloc);
  175. Admin_sendMessage(ret, txid, ctx->admin);
  176. }
  177. static void getRouteLabel(Dict* args, void* vcontext, String* txid, struct Allocator* requestAlloc)
  178. {
  179. struct Context* ctx = Identity_check((struct Context*) vcontext);
  180. char* err = NULL;
  181. String* pathToParentS = Dict_getString(args, String_CONST("pathToParent"));
  182. uint64_t pathToParent;
  183. if (pathToParentS->len != 19 || AddrTools_parsePath(&pathToParent, pathToParentS->bytes)) {
  184. err = "parse_pathToParent";
  185. }
  186. String* pathParentToChildS = Dict_getString(args, String_CONST("pathParentToChild"));
  187. uint64_t pathParentToChild;
  188. if (pathParentToChildS->len != 19
  189. || AddrTools_parsePath(&pathParentToChild, pathParentToChildS->bytes))
  190. {
  191. err = "parse_pathParentToChild";
  192. }
  193. uint64_t label = UINT64_MAX;
  194. if (!err) {
  195. label = NodeStore_getRouteLabel(ctx->store, pathToParent, pathParentToChild);
  196. err = NodeStore_getRouteLabel_strerror(label);
  197. }
  198. Dict* response = Dict_new(requestAlloc);
  199. if (!err) {
  200. String* printedPath = String_newBinary(NULL, 19, requestAlloc);
  201. AddrTools_printPath(printedPath->bytes, label);
  202. Dict_putString(response, String_new("result", requestAlloc), printedPath, requestAlloc);
  203. Dict_putString(response,
  204. String_new("error", requestAlloc),
  205. String_new("none", requestAlloc),
  206. requestAlloc);
  207. Admin_sendMessage(response, txid, ctx->admin);
  208. } else {
  209. Dict_putString(response,
  210. String_new("error", requestAlloc),
  211. String_new(err, requestAlloc),
  212. requestAlloc);
  213. Admin_sendMessage(response, txid, ctx->admin);
  214. }
  215. }
  216. void NodeStore_admin_register(struct NodeStore* nodeStore,
  217. struct Admin* admin,
  218. struct Allocator* alloc)
  219. {
  220. struct Context* ctx = Allocator_clone(alloc, (&(struct Context) {
  221. .admin = admin,
  222. .alloc = alloc,
  223. .store = nodeStore
  224. }));
  225. Identity_set(ctx);
  226. Admin_registerFunction("NodeStore_dumpTable", dumpTable, ctx, false,
  227. ((struct Admin_FunctionArg[]) {
  228. { .name = "page", .required = 1, .type = "Int" },
  229. }), admin);
  230. Admin_registerFunction("NodeStore_getLink", getLink, ctx, true,
  231. ((struct Admin_FunctionArg[]) {
  232. { .name = "parent", .required = 1, .type = "String" },
  233. { .name = "linkNum", .required = 1, .type = "Int" },
  234. }), admin);
  235. Admin_registerFunction("NodeStore_nodeForAddr", nodeForAddr, ctx, true,
  236. ((struct Admin_FunctionArg[]) {
  237. { .name = "ip", .required = 0, .type = "String" },
  238. }), admin);
  239. Admin_registerFunction("NodeStore_getRouteLabel", getRouteLabel, ctx, true,
  240. ((struct Admin_FunctionArg[]) {
  241. { .name = "pathToParent", .required = 1, .type = "String" },
  242. { .name = "pathParentToChild", .required = 1, .type = "String" }
  243. }), admin);
  244. }