InterfaceController_admin.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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 "net/InterfaceController.h"
  21. #include "net/InterfaceController_admin.h"
  22. struct Context
  23. {
  24. struct Allocator* alloc;
  25. struct InterfaceController* ic;
  26. struct Admin* admin;
  27. Identity
  28. };
  29. // typical peer record is around 140 benc chars, so can't have very many in 1023
  30. #define ENTRIES_PER_PAGE 6
  31. static void adminInterfaces(Dict* args,
  32. void* vcontext,
  33. String* txid,
  34. struct Allocator* alloc)
  35. {
  36. struct Context* context = Identity_check((struct Context*)vcontext);
  37. int64_t* page = Dict_getIntC(args, "page");
  38. int i = (page) ? *page * ENTRIES_PER_PAGE : 0;
  39. int count = InterfaceController_ifaceCount(context->ic);
  40. //int count = InterfaceController_getIface(context->ic, alloc, &stats);
  41. List* list = List_new(alloc);
  42. for (int counter = 0; i < count && counter++ < ENTRIES_PER_PAGE; i++) {
  43. struct InterfaceController_Iface* iface = InterfaceController_getIface(context->ic, i);
  44. Dict* d = Dict_new(alloc);
  45. Dict_putIntC(d, "ifNum", iface->ifNum, alloc);
  46. Dict_putStringC(d, "name", iface->name, alloc);
  47. char* bs = InterfaceController_beaconStateString(iface->beaconState);
  48. Dict_putStringCC(d, "beaconState", bs, alloc);
  49. List_addDict(list, d, alloc);
  50. }
  51. Dict* resp = Dict_new(alloc);
  52. Dict_putListC(resp, "ifaces", list, alloc);
  53. Dict_putIntC(resp, "total", count, alloc);
  54. if (i < count) { Dict_putIntC(resp, "more", 1, alloc); }
  55. Admin_sendMessage(resp, txid, context->admin);
  56. }
  57. static void adminPeerStats(Dict* args, void* vcontext, String* txid, struct Allocator* alloc)
  58. {
  59. struct Context* context = Identity_check((struct Context*)vcontext);
  60. struct InterfaceController_PeerStats* stats = NULL;
  61. int64_t* page = Dict_getIntC(args, "page");
  62. int i = (page) ? *page * ENTRIES_PER_PAGE : 0;
  63. int count = InterfaceController_getPeerStats(context->ic, alloc, &stats);
  64. List* list = List_new(alloc);
  65. for (int counter=0; i < count && counter++ < ENTRIES_PER_PAGE; i++) {
  66. Dict* d = Dict_new(alloc);
  67. Dict_putIntC(d, "bytesIn", stats[i].bytesIn, alloc);
  68. Dict_putIntC(d, "bytesOut", stats[i].bytesOut, alloc);
  69. Dict_putIntC(d, "recvKbps", stats[i].recvKbps, alloc);
  70. Dict_putIntC(d, "sendKbps", stats[i].sendKbps, alloc);
  71. Dict_putStringC(d, "addr", Address_toStringKey(&stats[i].addr, alloc), alloc);
  72. String* lladdrString = String_new(Sockaddr_print(stats[i].lladdr, alloc), alloc);
  73. Dict_putStringC(d, "lladdr", lladdrString, alloc);
  74. String* stateString = String_new(InterfaceController_stateString(stats[i].state), alloc);
  75. Dict_putStringC(d, "state", stateString, alloc);
  76. Dict_putIntC(d, "last", stats[i].timeOfLastMessage, alloc);
  77. Dict_putIntC(d, "isIncoming", stats[i].isIncomingConnection, alloc);
  78. Dict_putIntC(d, "duplicates", stats[i].duplicates, alloc);
  79. Dict_putIntC(d, "lostPackets", stats[i].lostPackets, alloc);
  80. Dict_putIntC(d, "receivedOutOfRange", stats[i].receivedOutOfRange, alloc);
  81. Dict_putIntC(d, "noiseProto", stats[i].noiseProto, alloc);
  82. Dict_putIntC(d, "ifNum", stats[i].ifNum, alloc);
  83. if (stats[i].user) {
  84. Dict_putStringC(d, "user", stats[i].user, alloc);
  85. }
  86. Dict_putIntC(d, "receivedPackets", stats[i].receivedPackets, alloc);
  87. List_addDict(list, d, alloc);
  88. }
  89. Dict* resp = Dict_new(alloc);
  90. Dict_putListC(resp, "peers", list, alloc);
  91. Dict_putIntC(resp, "total", count, alloc);
  92. if (i < count) {
  93. Dict_putIntC(resp, "more", 1, alloc);
  94. }
  95. Admin_sendMessage(resp, txid, context->admin);
  96. }
  97. static void adminDisconnectPeer(Dict* args,
  98. void* vcontext,
  99. String* txid,
  100. struct Allocator* requestAlloc)
  101. {
  102. struct Context* context = Identity_check((struct Context*)vcontext);
  103. String* pubkeyString = Dict_getStringC(args, "pubkey");
  104. // parse the key
  105. uint8_t pubkey[32];
  106. uint8_t addr[16];
  107. int count = 0;
  108. char* errorMsg = NULL;
  109. if (Key_parse(pubkeyString, pubkey, addr)) {
  110. errorMsg = "bad key";
  111. } else {
  112. // try to remove the peer if the key is valid
  113. count = InterfaceController_disconnectPeer(context->ic,pubkey);
  114. if (count == 0) {
  115. errorMsg = "no peer found for that key";
  116. }
  117. }
  118. Dict* response = Dict_new(requestAlloc);
  119. Dict_putIntC(response, "success", errorMsg ? 0 : 1, requestAlloc);
  120. if (errorMsg) {
  121. Dict_putStringCC(response, "error", errorMsg, requestAlloc);
  122. }
  123. Dict_putIntC(response, "sessionsDisconnected", count, requestAlloc);
  124. Admin_sendMessage(response, txid, context->admin);
  125. }
  126. static void adminResetPeering(Dict* args,
  127. void* vcontext,
  128. String* txid,
  129. struct Allocator* requestAlloc)
  130. {
  131. struct Context* context = Identity_check((struct Context*)vcontext);
  132. String* pubkeyString = Dict_getStringC(args, "pubkey");
  133. int error = 0;
  134. char* errorMsg = NULL;
  135. if (pubkeyString) {
  136. // parse the key
  137. uint8_t pubkey[32];
  138. uint8_t addr[16];
  139. error = Key_parse(pubkeyString, pubkey, addr);
  140. if (error) {
  141. errorMsg = "bad key";
  142. } else {
  143. InterfaceController_resetPeering(context->ic, pubkey);
  144. }
  145. } else {
  146. // reset all
  147. InterfaceController_resetPeering(context->ic, NULL);
  148. }
  149. Dict* response = Dict_new(requestAlloc);
  150. Dict_putIntC(response, "success", error ? 0 : 1, requestAlloc);
  151. if (error) {
  152. Dict_putStringCC(response, "error", errorMsg, requestAlloc);
  153. }
  154. Admin_sendMessage(response, txid, context->admin);
  155. }
  156. /*
  157. static resetSession(Dict* args, void* vcontext, String* txid, struct Allocator* requestAlloc)
  158. {
  159. struct Context* context = Identity_check((struct Context*)vcontext);
  160. String* pubkeyString = Dict_getStringC(args, "pubkey");
  161. // parse the key
  162. uint8_t pubkey[32];
  163. uint8_t addr[16];
  164. int error = Key_parse(pubkeyString, pubkey, addr);
  165. char* errorMsg = NULL;
  166. if (error) {
  167. errorMsg = "bad key";
  168. } else {
  169. // try to remove the peer if the key is valid
  170. error = InterfaceController_disconnectPeer(context->ic,pubkey);
  171. if (error) {
  172. errorMsg = "no peer found for that key";
  173. }
  174. }
  175. Dict* response = Dict_new(requestAlloc);
  176. Dict_putIntC(response, "success", error ? 0 : 1, requestAlloc);
  177. if (error) {
  178. Dict_putStringCC(response, "error", errorMsg, requestAlloc);
  179. }
  180. Admin_sendMessage(response, txid, context->admin);
  181. }*/
  182. void InterfaceController_admin_register(struct InterfaceController* ic,
  183. struct Admin* admin,
  184. struct Allocator* allocator)
  185. {
  186. struct Allocator* alloc = Allocator_child(allocator);
  187. struct Context* ctx = Allocator_clone(alloc, (&(struct Context) {
  188. .alloc = alloc,
  189. .ic = ic,
  190. .admin = admin
  191. }));
  192. Identity_set(ctx);
  193. Admin_registerFunction("InterfaceController_interfaces", adminInterfaces, ctx, true,
  194. ((struct Admin_FunctionArg[]) {
  195. { .name = "page", .required = 0, .type = "Int" }
  196. }), admin);
  197. Admin_registerFunction("InterfaceController_peerStats", adminPeerStats, ctx, false,
  198. ((struct Admin_FunctionArg[]) {
  199. { .name = "page", .required = 0, .type = "Int" }
  200. }), admin);
  201. Admin_registerFunction("InterfaceController_resetPeering", adminResetPeering, ctx, true,
  202. ((struct Admin_FunctionArg[]) {
  203. { .name = "pubkey", .required = 0, .type = "String" }
  204. }), admin);
  205. Admin_registerFunction("InterfaceController_disconnectPeer", adminDisconnectPeer, ctx, true,
  206. ((struct Admin_FunctionArg[]) {
  207. { .name = "pubkey", .required = 1, .type = "String" }
  208. }), admin);
  209. }