InterfaceController_admin.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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 "benc/Int.h"
  20. #include "crypto/Key.h"
  21. #include "interface/InterfaceController.h"
  22. #include "interface/InterfaceController_admin.h"
  23. #include "util/AddrTools.h"
  24. struct Context
  25. {
  26. struct Allocator* alloc;
  27. struct InterfaceController* ic;
  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 6
  32. static void adminPeerStats(Dict* args, void* vcontext, String* txid)
  33. {
  34. struct Context* context = vcontext;
  35. struct Allocator* alloc = Allocator_child(context->alloc);
  36. struct InterfaceController_peerStats* stats = NULL;
  37. int64_t* page = Dict_getInt(args, String_CONST("page"));
  38. int i = (page) ? *page * ENTRIES_PER_PAGE : 0;
  39. int count = context->ic->getPeerStats(context->ic, alloc, &stats);
  40. String* bytesIn = String_CONST("bytesIn");
  41. String* bytesOut = String_CONST("bytesOut");
  42. String* pubKey = String_CONST("publicKey");
  43. String* state = String_CONST("state");
  44. String* last = String_CONST("last");
  45. String* switchLabel = String_CONST("switchLabel");
  46. String* isIncoming = String_CONST("isIncoming");
  47. String* user = String_CONST("user");
  48. String* duplicates = String_CONST("duplicates");
  49. String* lostPackets = String_CONST("lostPackets");
  50. String* receivedOutOfRange = String_CONST("receivedOutOfRange");
  51. List* list = NULL;
  52. for (int counter=0; i < count && counter++ < ENTRIES_PER_PAGE; i++) {
  53. Dict* d = Dict_new(alloc);
  54. Dict_putInt(d, bytesIn, stats[i].bytesIn, alloc);
  55. Dict_putInt(d, bytesOut, stats[i].bytesOut, alloc);
  56. Dict_putString(d, pubKey, Key_stringify(stats[i].pubKey, alloc), alloc);
  57. String* stateString = String_new(InterfaceController_stateString(stats[i].state), alloc);
  58. Dict_putString(d, state, stateString, alloc);
  59. Dict_putInt(d, last, stats[i].timeOfLastMessage, alloc);
  60. uint8_t labelStack[20];
  61. AddrTools_printPath(labelStack, stats[i].switchLabel);
  62. Dict_putString(d, switchLabel, String_new((char*)labelStack, alloc), alloc);
  63. Dict_putInt(d, isIncoming, stats[i].isIncomingConnection, alloc);
  64. Dict_putInt(d, duplicates, stats[i].duplicates, alloc);
  65. Dict_putInt(d, lostPackets, stats[i].lostPackets, alloc);
  66. Dict_putInt(d, receivedOutOfRange, stats[i].receivedOutOfRange, alloc);
  67. if (stats[i].isIncomingConnection) {
  68. Dict_putString(d, user, stats[i].user, alloc);
  69. }
  70. list = List_addDict(list, d, alloc);
  71. }
  72. Dict response = Dict_CONST(
  73. String_CONST("peers"), List_OBJ(list), Dict_CONST(
  74. String_CONST("total"), Int_OBJ(count), NULL
  75. ));
  76. if (i < count) {
  77. response = Dict_CONST(
  78. String_CONST("more"), Int_OBJ(1), response
  79. );
  80. }
  81. Admin_sendMessage(&response, txid, context->admin);
  82. Allocator_free(alloc);
  83. }
  84. static void adminDisconnectPeer(Dict* args, void* vcontext, String* txid)
  85. {
  86. struct Context* context = vcontext;
  87. struct Allocator* alloc = Allocator_child(context->alloc);
  88. String* pubkeyString = Dict_getString(args, String_CONST("pubkey"));
  89. // parse the key
  90. uint8_t pubkey[32];
  91. uint8_t addr[16];
  92. int error = Key_parse(pubkeyString, pubkey, addr);
  93. char* errorMsg = NULL;
  94. if (error) {
  95. errorMsg = "bad key";
  96. }
  97. else {
  98. // try to remove the peer if the key is valid
  99. error = context->ic->disconnectPeer(context->ic,pubkey);
  100. if (error) {
  101. errorMsg = "no peer found for that key";
  102. }
  103. }
  104. Dict* response = Dict_new(alloc);
  105. Dict_putInt(response, String_CONST("sucess"), error ? 0 : 1, alloc);
  106. if (error) {
  107. Dict_putString(response, String_CONST("error"), String_CONST(errorMsg), alloc);
  108. }
  109. Admin_sendMessage(response, txid, context->admin);
  110. Allocator_free(alloc);
  111. }
  112. void InterfaceController_admin_register(struct InterfaceController* ic,
  113. struct Admin* admin,
  114. struct Allocator* alloc)
  115. {
  116. struct Context* ctx = Allocator_clone(alloc, (&(struct Context) {
  117. .alloc = alloc,
  118. .ic = ic,
  119. .admin = admin
  120. }));
  121. Admin_registerFunction("InterfaceController_peerStats", adminPeerStats, ctx, true,
  122. ((struct Admin_FunctionArg[]) {
  123. { .name = "page", .required = 0, .type = "Int" }
  124. }), admin);
  125. Admin_registerFunction("InterfaceController_disconnectPeer", adminDisconnectPeer, ctx, true,
  126. ((struct Admin_FunctionArg[]) {
  127. { .name = "pubkey", .required = 1, .type = "String" }
  128. }), admin);
  129. }