1
0

SessionManager_admin.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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->sendSwitchLabel;
  78. addr.protocolVersion = session->version;
  79. Dict_putStringC(r, "addr", Address_toString(&addr, alloc), alloc);
  80. Dict_putIntC(r, "handle", session->receiveHandle, alloc);
  81. Dict_putIntC(r, "sendHandle", session->sendHandle, alloc);
  82. Dict_putIntC(r, "timeOfLastIn", session->timeOfLastIn, alloc);
  83. Dict_putIntC(r, "timeOfLastOut", session->timeOfLastOut, alloc);
  84. Dict_putIntC(r, "metric", session->metric, alloc);
  85. Admin_sendMessage(r, txid, context->admin);
  86. return;
  87. }
  88. static void sessionStats(Dict* args,
  89. void* vcontext,
  90. String* txid,
  91. struct Allocator* alloc)
  92. {
  93. struct Context* context = Identity_check((struct Context*) vcontext);
  94. int64_t* handleP = Dict_getIntC(args, "handle");
  95. uint32_t handle = *handleP;
  96. struct SessionManager_Session* session = SessionManager_sessionForHandle(handle, context->sm);
  97. outputSession(context, session, txid, alloc);
  98. }
  99. static struct SessionManager_Session* sessionForIP(Dict* args,
  100. struct Context* context,
  101. String* txid,
  102. struct Allocator* alloc)
  103. {
  104. String* ip6Str = Dict_getStringC(args, "ip6");
  105. uint8_t ip6Binary[16] = {0};
  106. Dict* r = Dict_new(alloc);
  107. if (AddrTools_parseIp(ip6Binary, ip6Str->bytes)) {
  108. Dict_putStringCC(r, "error", "malformed_ip", alloc);
  109. Admin_sendMessage(r, txid, context->admin);
  110. return NULL;
  111. }
  112. struct SessionManager_Session* session = SessionManager_sessionForIp6(ip6Binary, context->sm);
  113. if (!session) {
  114. Dict_putStringCC(r, "error", "no such session", alloc);
  115. Admin_sendMessage(r, txid, context->admin);
  116. return NULL;
  117. }
  118. return session;
  119. }
  120. static void sessionStatsByIP(Dict* args,
  121. void* vcontext,
  122. String* txid,
  123. struct Allocator* alloc)
  124. {
  125. struct Context* context = Identity_check((struct Context*) vcontext);
  126. struct SessionManager_Session* session = sessionForIP(args, context, txid, alloc);
  127. if (!session) { return; }
  128. outputSession(context, session, txid, alloc);
  129. }
  130. static void resetCA(Dict* args,
  131. void* vcontext,
  132. String* txid,
  133. struct Allocator* alloc)
  134. {
  135. struct Context* context = Identity_check((struct Context*) vcontext);
  136. struct SessionManager_Session* session = sessionForIP(args, context, txid, alloc);
  137. if (!session) { return; }
  138. CryptoAuth_reset(session->caSession);
  139. Dict* r = Dict_new(alloc);
  140. Dict_putStringCC(r, "error", "none", alloc);
  141. Admin_sendMessage(r, txid, context->admin);
  142. }
  143. void SessionManager_admin_register(struct SessionManager* sm,
  144. struct Admin* admin,
  145. struct Allocator* alloc)
  146. {
  147. struct Context* ctx = Allocator_clone(alloc, (&(struct Context) {
  148. .alloc = alloc,
  149. .sm = sm,
  150. .admin = admin
  151. }));
  152. Identity_set(ctx);
  153. Admin_registerFunction("SessionManager_getHandles", getHandles, ctx, true,
  154. ((struct Admin_FunctionArg[]) {
  155. { .name = "page", .required = 0, .type = "Int" }
  156. }), admin);
  157. Admin_registerFunction("SessionManager_sessionStats", sessionStats, ctx, true,
  158. ((struct Admin_FunctionArg[]) {
  159. { .name = "handle", .required = 1, .type = "Int" }
  160. }), admin);
  161. Admin_registerFunction("SessionManager_sessionStatsByIP", sessionStatsByIP, ctx, true,
  162. ((struct Admin_FunctionArg[]) {
  163. { .name = "ip6", .required = 1, .type = "String" }
  164. }), admin);
  165. Admin_registerFunction("SessionManager_resetCA", resetCA, ctx, true,
  166. ((struct Admin_FunctionArg[]) {
  167. { .name = "ip6", .required = 1, .type = "String" }
  168. }), admin);
  169. }