ETHInterface_admin.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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 "interface/ETHInterface_admin.h"
  16. #include "interface/ETHInterface.h"
  17. #include "benc/Int.h"
  18. #include "admin/Admin.h"
  19. #include "crypto/Key.h"
  20. #include "exception/Jmp.h"
  21. #include "memory/Allocator.h"
  22. #include "net/InterfaceController.h"
  23. #include "util/AddrTools.h"
  24. #include "util/Identity.h"
  25. struct Context
  26. {
  27. struct EventBase* eventBase;
  28. struct Allocator* alloc;
  29. struct Log* logger;
  30. struct Admin* admin;
  31. struct InterfaceController* ic;
  32. Identity
  33. };
  34. static void newInterface(Dict* args, void* vcontext, String* txid, struct Allocator* requestAlloc)
  35. {
  36. struct Context* const ctx = Identity_check((struct Context*) vcontext);
  37. String* const bindDevice = Dict_getString(args, String_CONST("bindDevice"));
  38. struct Allocator* const alloc = Allocator_child(ctx->alloc);
  39. struct ETHInterface* ethIf = NULL;
  40. struct Jmp jmp;
  41. Jmp_try(jmp) {
  42. ethIf = ETHInterface_new(
  43. ctx->eventBase, bindDevice->bytes, alloc, &jmp.handler, ctx->logger);
  44. } Jmp_catch {
  45. Dict* out = Dict_new(requestAlloc);
  46. Dict_putString(out, String_CONST("error"), String_CONST(jmp.message), requestAlloc);
  47. Admin_sendMessage(out, txid, ctx->admin);
  48. Allocator_free(alloc);
  49. return;
  50. }
  51. String* ifName = String_printf(requestAlloc, "ETH/%s", bindDevice->bytes);
  52. Dict* out = Dict_new(requestAlloc);
  53. struct InterfaceController_Iface* ici;
  54. char* err = NULL;
  55. int ret = InterfaceController_newIface(ctx->ic, ifName, true, alloc, &ici);
  56. if (!ret) {
  57. Iface_plumb(&ici->addrIf, &ethIf->generic.iface);
  58. Dict_putString(out, String_CONST("ifName"), ifName, requestAlloc);
  59. err = "none";
  60. } else if (ret == InterfaceController_newIface_NAME_EXISTS) {
  61. err = "InterfaceController_newIface() -> name_exists";
  62. } else {
  63. err = "unknown";
  64. }
  65. Dict_putString(out, String_CONST("error"), String_CONST(err), requestAlloc);
  66. Admin_sendMessage(out, txid, ctx->admin);
  67. }
  68. static void listDevices(Dict* args, void* vcontext, String* txid, struct Allocator* requestAlloc)
  69. {
  70. struct Context* ctx = Identity_check((struct Context*) vcontext);
  71. List* devices = NULL;
  72. struct Jmp jmp;
  73. Jmp_try(jmp) {
  74. devices = ETHInterface_listDevices(requestAlloc, &jmp.handler);
  75. } Jmp_catch {
  76. Dict* out = Dict_new(requestAlloc);
  77. Dict_putString(out, String_CONST("error"), String_CONST(jmp.message), requestAlloc);
  78. Admin_sendMessage(out, txid, ctx->admin);
  79. return;
  80. }
  81. Dict* out = Dict_new(requestAlloc);
  82. Dict_putString(out, String_CONST("error"), String_CONST("none"), requestAlloc);
  83. Dict_putList(out, String_CONST("devices"), devices, requestAlloc);
  84. Admin_sendMessage(out, txid, ctx->admin);
  85. }
  86. void ETHInterface_admin_register(struct EventBase* base,
  87. struct Allocator* alloc,
  88. struct Log* logger,
  89. struct Admin* admin,
  90. struct InterfaceController* ic)
  91. {
  92. struct Context* ctx = Allocator_clone(alloc, (&(struct Context) {
  93. .eventBase = base,
  94. .alloc = alloc,
  95. .logger = logger,
  96. .admin = admin,
  97. .ic = ic
  98. }));
  99. Identity_set(ctx);
  100. Admin_registerFunction("ETHInterface_new", newInterface, ctx, true,
  101. ((struct Admin_FunctionArg[]) {
  102. { .name = "bindDevice", .required = 1, .type = "String" }
  103. }), admin);
  104. Admin_registerFunction("ETHInterface_listDevices", listDevices, ctx, true, NULL, admin);
  105. }