SessionManager_admin.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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)
  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 sessionStats2(Dict* args,
  54. struct Context* context,
  55. struct Allocator* alloc,
  56. String* txid)
  57. {
  58. int64_t* handleP = Dict_getInt(args, String_CONST("handle"));
  59. uint32_t handle = *handleP;
  60. struct SessionManager_Session* session = SessionManager_sessionForHandle(handle, context->sm);
  61. Dict* r = Dict_new(alloc);
  62. if (!session) {
  63. Dict_putString(r, String_CONST("error"), String_CONST("no such session"), alloc);
  64. Admin_sendMessage(r, txid, context->admin);
  65. return;
  66. }
  67. struct ReplayProtector* rp = CryptoAuth_getReplayProtector(&session->iface);
  68. Dict_putInt(r, String_CONST("duplicates"), rp->duplicates, alloc);
  69. Dict_putInt(r, String_CONST("lostPackets"), rp->lostPackets, alloc);
  70. Dict_putInt(r, String_CONST("receivedOutOfRange"), rp->receivedOutOfRange, alloc);
  71. uint8_t* key = CryptoAuth_getHerPublicKey(&session->iface);
  72. Dict_putString(r, String_CONST("publicKey"), Key_stringify(key, alloc), alloc);
  73. Dict_putInt(r, String_CONST("last"), session->lastMessageTime, alloc);
  74. Dict_putInt(r, String_CONST("version"), session->version, alloc);
  75. Dict_putInt(r, String_CONST("handle"),
  76. Endian_bigEndianToHost32(session->receiveHandle_be), alloc);
  77. Dict_putInt(r, String_CONST("sendHandle"),
  78. Endian_bigEndianToHost32(session->sendHandle_be), alloc);
  79. Admin_sendMessage(r, txid, context->admin);
  80. return;
  81. }
  82. static void sessionStats(Dict* args, void* vcontext, String* txid)
  83. {
  84. struct Context* context = vcontext;
  85. struct Allocator* alloc = Allocator_child(context->alloc);
  86. sessionStats2(args, context, alloc, txid);
  87. Allocator_free(alloc);
  88. }
  89. void SessionManager_admin_register(struct SessionManager* sm,
  90. struct Admin* admin,
  91. struct Allocator* alloc)
  92. {
  93. struct Context* ctx = Allocator_clone(alloc, (&(struct Context) {
  94. .alloc = alloc,
  95. .sm = sm,
  96. .admin = admin
  97. }));
  98. Admin_registerFunction("SessionManager_getHandles", getHandles, ctx, true,
  99. ((struct Admin_FunctionArg[]) {
  100. { .name = "page", .required = 0, .type = "Int" }
  101. }), admin);
  102. Admin_registerFunction("SessionManager_sessionStats", sessionStats, ctx, true,
  103. ((struct Admin_FunctionArg[]) {
  104. { .name = "handle", .required = 1, .type = "Int" }
  105. }), admin);
  106. }