1
0

SessionManager_admin.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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/String.h"
  17. #include "benc/Dict.h"
  18. #include "benc/List.h"
  19. #include "crypto/Key.h"
  20. #include "crypto/ReplayProtector.h"
  21. #include "interface/SessionManager.h"
  22. #include "interface/SessionManager_admin.h"
  23. #include "util/AddrTools.h"
  24. struct Context
  25. {
  26. struct Allocator* alloc;
  27. struct SessionManager* sm;
  28. struct Admin* admin;
  29. };
  30. // typical peer record is around 140 benc chars, so can't have very many in 1023
  31. #define ENTRIES_PER_PAGE 64
  32. static void getHandles(Dict* args, void* vcontext, String* txid, struct Allocator* requestAlloc)
  33. {
  34. struct Context* context = vcontext;
  35. struct Allocator* alloc = Allocator_child(context->alloc);
  36. int64_t* page = Dict_getInt(args, String_CONST("page"));
  37. uint32_t i = (page) ? *page * ENTRIES_PER_PAGE : 0;
  38. struct SessionManager_HandleList* hList = SessionManager_getHandleList(context->sm, alloc);
  39. List* list = NULL;
  40. for (int counter=0; i < hList->count && counter++ < ENTRIES_PER_PAGE; i++) {
  41. list = List_addInt(list, hList->handles[i], alloc);
  42. }
  43. Dict* r = Dict_new(alloc);
  44. Dict_putList(r, String_CONST("handles"), list, alloc);
  45. Dict_putInt(r, String_CONST("total"), hList->count, alloc);
  46. String* more = String_CONST("more");
  47. if (i < hList->count) {
  48. Dict_putInt(r, more, 1, alloc);
  49. }
  50. Admin_sendMessage(r, txid, context->admin);
  51. Allocator_free(alloc);
  52. }
  53. static void sessionStats(Dict* args,
  54. void* vcontext,
  55. String* txid,
  56. struct Allocator* alloc)
  57. {
  58. struct Context* context = vcontext;
  59. int64_t* handleP = Dict_getInt(args, String_CONST("handle"));
  60. uint32_t handle = *handleP;
  61. struct SessionManager_Session* session = SessionManager_sessionForHandle(handle, context->sm);
  62. uint8_t* ip6 = SessionManager_getIp6(handle, context->sm);
  63. Dict* r = Dict_new(alloc);
  64. if (!session) {
  65. Dict_putString(r, String_CONST("error"), String_CONST("no such session"), alloc);
  66. Admin_sendMessage(r, txid, context->admin);
  67. return;
  68. }
  69. // both or neither
  70. Assert_always(ip6);
  71. uint8_t printedAddr[40];
  72. AddrTools_printIp(printedAddr, ip6);
  73. Dict_putString(r, String_CONST("ip6"), String_new(printedAddr, alloc), alloc);
  74. int state = CryptoAuth_getState(&session->iface);
  75. Dict_putString(r,
  76. String_CONST("state"),
  77. String_new(CryptoAuth_stateString(state), alloc),
  78. alloc);
  79. struct ReplayProtector* rp = CryptoAuth_getReplayProtector(&session->iface);
  80. Dict_putInt(r, String_CONST("duplicates"), rp->duplicates, alloc);
  81. Dict_putInt(r, String_CONST("lostPackets"), rp->lostPackets, alloc);
  82. Dict_putInt(r, String_CONST("receivedOutOfRange"), rp->receivedOutOfRange, alloc);
  83. uint8_t* key = CryptoAuth_getHerPublicKey(&session->iface);
  84. Dict_putString(r, String_CONST("publicKey"), Key_stringify(key, alloc), alloc);
  85. Dict_putInt(r, String_CONST("last"), session->lastMessageTime, alloc);
  86. Dict_putInt(r, String_CONST("version"), session->version, alloc);
  87. Dict_putInt(r, String_CONST("handle"),
  88. Endian_bigEndianToHost32(session->receiveHandle_be), alloc);
  89. Dict_putInt(r, String_CONST("sendHandle"),
  90. Endian_bigEndianToHost32(session->sendHandle_be), alloc);
  91. Admin_sendMessage(r, txid, context->admin);
  92. return;
  93. }
  94. void SessionManager_admin_register(struct SessionManager* sm,
  95. struct Admin* admin,
  96. struct Allocator* alloc)
  97. {
  98. struct Context* ctx = Allocator_clone(alloc, (&(struct Context) {
  99. .alloc = alloc,
  100. .sm = sm,
  101. .admin = admin
  102. }));
  103. Admin_registerFunction("SessionManager_getHandles", getHandles, ctx, true,
  104. ((struct Admin_FunctionArg[]) {
  105. { .name = "page", .required = 0, .type = "Int" }
  106. }), admin);
  107. Admin_registerFunction("SessionManager_sessionStats", sessionStats, ctx, true,
  108. ((struct Admin_FunctionArg[]) {
  109. { .name = "handle", .required = 1, .type = "Int" }
  110. }), admin);
  111. }