InterfaceController_admin.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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/AddressCalc.h"
  21. #include "crypto/Key.h"
  22. #include "dht/dhtcore/NodeStore.h"
  23. #include "interface/InterfaceController.h"
  24. #include "interface/InterfaceController_admin.h"
  25. #include "util/AddrTools.h"
  26. struct Context
  27. {
  28. struct Allocator* alloc;
  29. struct InterfaceController* ic;
  30. struct NodeStore* store;
  31. struct Admin* admin;
  32. Identity
  33. };
  34. // typical peer record is around 140 benc chars, so can't have very many in 1023
  35. #define ENTRIES_PER_PAGE 6
  36. static void adminPeerStats(Dict* args, void* vcontext, String* txid, struct Allocator* alloc)
  37. {
  38. struct Context* context = Identity_check((struct Context*)vcontext);
  39. struct InterfaceController_PeerStats* stats = NULL;
  40. int64_t* page = Dict_getInt(args, String_CONST("page"));
  41. int i = (page) ? *page * ENTRIES_PER_PAGE : 0;
  42. int count = InterfaceController_getPeerStats(context->ic, alloc, &stats);
  43. String* bytesIn = String_CONST("bytesIn");
  44. String* bytesOut = String_CONST("bytesOut");
  45. String* pubKey = String_CONST("publicKey");
  46. String* addr = String_CONST("addr");
  47. String* state = String_CONST("state");
  48. String* last = String_CONST("last");
  49. String* switchLabel = String_CONST("switchLabel");
  50. String* isIncoming = String_CONST("isIncoming");
  51. String* user = String_CONST("user");
  52. String* version = String_CONST("version");
  53. String* duplicates = String_CONST("duplicates");
  54. String* lostPackets = String_CONST("lostPackets");
  55. String* receivedOutOfRange = String_CONST("receivedOutOfRange");
  56. List* list = List_new(alloc);
  57. for (int counter=0; i < count && counter++ < ENTRIES_PER_PAGE; i++) {
  58. Dict* d = Dict_new(alloc);
  59. Dict_putInt(d, bytesIn, stats[i].bytesIn, alloc);
  60. Dict_putInt(d, bytesOut, stats[i].bytesOut, alloc);
  61. Dict_putString(d, addr, Address_toString(&stats[i].addr, alloc), alloc);
  62. Dict_putString(d, pubKey, Key_stringify(stats[i].addr.key, alloc), alloc);
  63. String* stateString = String_new(InterfaceController_stateString(stats[i].state), alloc);
  64. Dict_putString(d, state, stateString, alloc);
  65. Dict_putInt(d, last, stats[i].timeOfLastMessage, alloc);
  66. uint8_t labelStack[20];
  67. AddrTools_printPath(labelStack, stats[i].addr.path);
  68. Dict_putString(d, switchLabel, String_new((char*)labelStack, alloc), alloc);
  69. Dict_putInt(d, isIncoming, stats[i].isIncomingConnection, alloc);
  70. Dict_putInt(d, duplicates, stats[i].duplicates, alloc);
  71. Dict_putInt(d, lostPackets, stats[i].lostPackets, alloc);
  72. Dict_putInt(d, receivedOutOfRange, stats[i].receivedOutOfRange, alloc);
  73. if (stats[i].user) {
  74. Dict_putString(d, user, stats[i].user, alloc);
  75. }
  76. uint8_t address[16];
  77. AddressCalc_addressForPublicKey(address, stats[i].addr.key);
  78. Dict_putInt(d, version, stats[i].addr.protocolVersion, alloc);
  79. List_addDict(list, d, alloc);
  80. }
  81. Dict* resp = Dict_new(alloc);
  82. Dict_putList(resp, String_CONST("peers"), list, alloc);
  83. Dict_putInt(resp, String_CONST("total"), count, alloc);
  84. if (i < count) {
  85. Dict_putInt(resp, String_CONST("more"), 1, alloc);
  86. }
  87. Dict_putString(resp, String_CONST("deprecation"),
  88. String_CONST("publicKey,switchLabel,version will soon be removed"), alloc);
  89. Admin_sendMessage(resp, txid, context->admin);
  90. }
  91. static void adminDisconnectPeer(Dict* args,
  92. void* vcontext,
  93. String* txid,
  94. struct Allocator* requestAlloc)
  95. {
  96. struct Context* context = Identity_check((struct Context*)vcontext);
  97. String* pubkeyString = Dict_getString(args, String_CONST("pubkey"));
  98. // parse the key
  99. uint8_t pubkey[32];
  100. uint8_t addr[16];
  101. int error = Key_parse(pubkeyString, pubkey, addr);
  102. char* errorMsg = NULL;
  103. if (error) {
  104. errorMsg = "bad key";
  105. } else {
  106. // try to remove the peer if the key is valid
  107. error = InterfaceController_disconnectPeer(context->ic,pubkey);
  108. if (error) {
  109. errorMsg = "no peer found for that key";
  110. }
  111. }
  112. Dict* response = Dict_new(requestAlloc);
  113. Dict_putInt(response, String_CONST("success"), error ? 0 : 1, requestAlloc);
  114. if (error) {
  115. Dict_putString(response, String_CONST("error"), String_CONST(errorMsg), requestAlloc);
  116. }
  117. Admin_sendMessage(response, txid, context->admin);
  118. }
  119. /*
  120. static resetSession(Dict* args, void* vcontext, String* txid, struct Allocator* requestAlloc)
  121. {
  122. struct Context* context = Identity_check((struct Context*)vcontext);
  123. String* pubkeyString = Dict_getString(args, String_CONST("pubkey"));
  124. // parse the key
  125. uint8_t pubkey[32];
  126. uint8_t addr[16];
  127. int error = Key_parse(pubkeyString, pubkey, addr);
  128. char* errorMsg = NULL;
  129. if (error) {
  130. errorMsg = "bad key";
  131. } else {
  132. // try to remove the peer if the key is valid
  133. error = InterfaceController_disconnectPeer(context->ic,pubkey);
  134. if (error) {
  135. errorMsg = "no peer found for that key";
  136. }
  137. }
  138. Dict* response = Dict_new(requestAlloc);
  139. Dict_putInt(response, String_CONST("success"), error ? 0 : 1, requestAlloc);
  140. if (error) {
  141. Dict_putString(response, String_CONST("error"), String_CONST(errorMsg), requestAlloc);
  142. }
  143. Admin_sendMessage(response, txid, context->admin);
  144. }*/
  145. void InterfaceController_admin_register(struct InterfaceController* ic,
  146. struct NodeStore* nodeStore,
  147. struct Admin* admin,
  148. struct Allocator* alloc)
  149. {
  150. struct Context* ctx = Allocator_clone(alloc, (&(struct Context) {
  151. .alloc = alloc,
  152. .ic = ic,
  153. .store = nodeStore,
  154. .admin = admin
  155. }));
  156. Identity_set(ctx);
  157. Admin_registerFunction("InterfaceController_peerStats", adminPeerStats, ctx, false,
  158. ((struct Admin_FunctionArg[]) {
  159. { .name = "page", .required = 0, .type = "Int" }
  160. }), admin);
  161. Admin_registerFunction("InterfaceController_disconnectPeer", adminDisconnectPeer, ctx, true,
  162. ((struct Admin_FunctionArg[]) {
  163. { .name = "pubkey", .required = 1, .type = "String" }
  164. }), admin);
  165. }