SessionManager_admin.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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/String.h"
  17. #include "benc/Dict.h"
  18. #include "benc/List.h"
  19. #include "crypto/Key.h"
  20. #include "crypto/ReplayProtector.h"
  21. #include "dht/Address.h"
  22. #include "net/SessionManager.h"
  23. #include "net/SessionManager_admin.h"
  24. #include "util/AddrTools.h"
  25. #include "util/Identity.h"
  26. struct Context
  27. {
  28. struct Allocator* alloc;
  29. struct SessionManager* sm;
  30. struct Admin* admin;
  31. Identity
  32. };
  33. #define ENTRIES_PER_PAGE 64
  34. static void getHandles(Dict* args, void* vcontext, String* txid, struct Allocator* requestAlloc)
  35. {
  36. struct Context* context = Identity_check((struct Context*) vcontext);
  37. struct Allocator* alloc = Allocator_child(context->alloc);
  38. int64_t* page = Dict_getIntC(args, "page");
  39. int i = (page) ? *page * ENTRIES_PER_PAGE : 0;
  40. struct SessionManager_HandleList* hList = SessionManager_getHandleList(context->sm, alloc);
  41. List* list = List_new(alloc);
  42. for (int counter = 0; i < hList->length && counter++ < ENTRIES_PER_PAGE; i++) {
  43. List_addInt(list, hList->handles[i], alloc);
  44. }
  45. Dict* r = Dict_new(alloc);
  46. Dict_putListC(r, "handles", list, alloc);
  47. Dict_putIntC(r, "total", hList->length, alloc);
  48. if (i < hList->length) {
  49. Dict_putIntC(r, "more", 1, alloc);
  50. }
  51. Admin_sendMessage(r, txid, context->admin);
  52. Allocator_free(alloc);
  53. }
  54. static void outputSession(struct Context* context,
  55. struct SessionManager_Session* session,
  56. String* txid,
  57. struct Allocator* alloc)
  58. {
  59. Dict* r = Dict_new(alloc);
  60. if (!session) {
  61. Dict_putStringCC(r, "error", "no such session", alloc);
  62. Admin_sendMessage(r, txid, context->admin);
  63. return;
  64. }
  65. uint8_t printedAddr[40];
  66. AddrTools_printIp(printedAddr, session->caSession->herIp6);
  67. Dict_putStringC(r, "ip6", String_new(printedAddr, alloc), alloc);
  68. String* state =
  69. String_new(CryptoAuth_stateString(CryptoAuth_getState(session->caSession)), alloc);
  70. Dict_putStringC(r, "state", state, alloc);
  71. struct ReplayProtector* rp = &session->caSession->replayProtector;
  72. Dict_putIntC(r, "duplicates", rp->duplicates, alloc);
  73. Dict_putIntC(r, "lostPackets", rp->lostPackets, alloc);
  74. Dict_putIntC(r, "receivedOutOfRange", rp->receivedOutOfRange, alloc);
  75. struct Address addr;
  76. Bits_memcpy(addr.key, session->caSession->herPublicKey, 32);
  77. addr.path = session->paths[0].label;
  78. addr.protocolVersion = session->version;
  79. Dict_putStringC(r, "addr", Address_toStringKey(&addr, alloc), alloc);
  80. Dict_putIntC(r, "handle", session->receiveHandle, alloc);
  81. Dict_putIntC(r, "sendHandle", session->sendHandle, alloc);
  82. Dict_putIntC(r, "metric", SessionManager_effectiveMetric(session), alloc);
  83. Dict_putIntC(r, "timeOfLastUsage", session->timeOfLastUsage, alloc);
  84. Admin_sendMessage(r, txid, context->admin);
  85. return;
  86. }
  87. static void sessionStats(Dict* args,
  88. void* vcontext,
  89. String* txid,
  90. struct Allocator* alloc)
  91. {
  92. struct Context* context = Identity_check((struct Context*) vcontext);
  93. int64_t* handleP = Dict_getIntC(args, "handle");
  94. uint32_t handle = *handleP;
  95. struct SessionManager_Session* session = SessionManager_sessionForHandle(handle, context->sm);
  96. outputSession(context, session, txid, alloc);
  97. }
  98. static struct SessionManager_Session* sessionForIP(Dict* args,
  99. struct Context* context,
  100. String* txid,
  101. struct Allocator* alloc)
  102. {
  103. String* ip6Str = Dict_getStringC(args, "ip6");
  104. uint8_t ip6Binary[16] = {0};
  105. Dict* r = Dict_new(alloc);
  106. if (AddrTools_parseIp(ip6Binary, ip6Str->bytes)) {
  107. Dict_putStringCC(r, "error", "malformed_ip", alloc);
  108. Admin_sendMessage(r, txid, context->admin);
  109. return NULL;
  110. }
  111. struct SessionManager_Session* session = SessionManager_sessionForIp6(ip6Binary, context->sm);
  112. if (!session) {
  113. Dict_putStringCC(r, "error", "no such session", alloc);
  114. Admin_sendMessage(r, txid, context->admin);
  115. return NULL;
  116. }
  117. return session;
  118. }
  119. static void sessionStatsByIP(Dict* args,
  120. void* vcontext,
  121. String* txid,
  122. struct Allocator* alloc)
  123. {
  124. struct Context* context = Identity_check((struct Context*) vcontext);
  125. struct SessionManager_Session* session = sessionForIP(args, context, txid, alloc);
  126. if (!session) { return; }
  127. outputSession(context, session, txid, alloc);
  128. }
  129. static void resetCA(Dict* args,
  130. void* vcontext,
  131. String* txid,
  132. struct Allocator* alloc)
  133. {
  134. struct Context* context = Identity_check((struct Context*) vcontext);
  135. struct SessionManager_Session* session = sessionForIP(args, context, txid, alloc);
  136. if (!session) { return; }
  137. CryptoAuth_reset(session->caSession);
  138. Dict* r = Dict_new(alloc);
  139. Dict_putStringCC(r, "error", "none", alloc);
  140. Admin_sendMessage(r, txid, context->admin);
  141. }
  142. void SessionManager_admin_register(struct SessionManager* sm,
  143. struct Admin* admin,
  144. struct Allocator* alloc)
  145. {
  146. struct Context* ctx = Allocator_clone(alloc, (&(struct Context) {
  147. .alloc = alloc,
  148. .sm = sm,
  149. .admin = admin
  150. }));
  151. Identity_set(ctx);
  152. Admin_registerFunction("SessionManager_getHandles", getHandles, ctx, true,
  153. ((struct Admin_FunctionArg[]) {
  154. { .name = "page", .required = 0, .type = "Int" }
  155. }), admin);
  156. Admin_registerFunction("SessionManager_sessionStats", sessionStats, ctx, true,
  157. ((struct Admin_FunctionArg[]) {
  158. { .name = "handle", .required = 1, .type = "Int" }
  159. }), admin);
  160. Admin_registerFunction("SessionManager_sessionStatsByIP", sessionStatsByIP, ctx, true,
  161. ((struct Admin_FunctionArg[]) {
  162. { .name = "ip6", .required = 1, .type = "String" }
  163. }), admin);
  164. Admin_registerFunction("SessionManager_resetCA", resetCA, ctx, true,
  165. ((struct Admin_FunctionArg[]) {
  166. { .name = "ip6", .required = 1, .type = "String" }
  167. }), admin);
  168. }