NodeStore_admin.c 11 KB

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