Angel.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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 "admin/angel/Angel.h"
  17. #include "benc/Int.h"
  18. #include "benc/String.h"
  19. #include "exception/Jmp.h"
  20. #include "memory/Allocator.h"
  21. #include "util/platform/netdev/NetDev.h"
  22. #include "interface/addressable/AddrIfaceAdapter.h"
  23. #ifdef HAS_ETH_INTERFACE
  24. #include "interface/ETHInterface.h"
  25. #endif
  26. #include "util/events/EventBase.h"
  27. #include "util/log/Log.h"
  28. #include "util/Identity.h"
  29. #include <stdlib.h>
  30. struct AngelContext
  31. {
  32. struct Iface* coreIface;
  33. struct EventBase* eventBase;
  34. struct Allocator* alloc;
  35. struct Log* logger;
  36. struct Admin* admin;
  37. Identity
  38. };
  39. static void adminExit(Dict* args, void* vcontext, String* txid, struct Allocator* requestAlloc)
  40. {
  41. struct AngelContext* ctx = Identity_check((struct AngelContext*) vcontext);
  42. Log_info(ctx->logger, "Got request to exit");
  43. Dict d = Dict_CONST(String_CONST("error"), String_OBJ(String_CONST("none")), NULL);
  44. Admin_sendMessage(&d, txid, ctx->admin);
  45. exit(0);
  46. }
  47. static void adminAddIp2(char* interfaceName,
  48. struct Sockaddr* addr,
  49. int prefixLen,
  50. String* txid,
  51. struct Allocator* tempAlloc,
  52. struct AngelContext* ctx)
  53. {
  54. struct Jmp j;
  55. Jmp_try(j) {
  56. NetDev_addAddress(interfaceName, addr, prefixLen, NULL, &j.handler);
  57. } Jmp_catch {
  58. Dict d = Dict_CONST(
  59. String_CONST("error"), String_OBJ(String_CONST(j.message)), NULL
  60. );
  61. Admin_sendMessage(&d, txid, ctx->admin);
  62. return;
  63. }
  64. Dict d = Dict_CONST(
  65. String_CONST("error"), String_OBJ(String_CONST("none")), NULL
  66. );
  67. Admin_sendMessage(&d, txid, ctx->admin);
  68. }
  69. static void adminAddIp(Dict* args, void* vcontext, String* txid, struct Allocator* requestAlloc)
  70. {
  71. struct AngelContext* ctx = Identity_check((struct AngelContext*) vcontext);
  72. String* interfaceName = Dict_getString(args, String_CONST("interfaceName"));
  73. String* address = Dict_getString(args, String_CONST("address"));
  74. int64_t* prefixLenP = Dict_getInt(args, String_CONST("prefixLen"));
  75. int prefixLen = *prefixLenP;
  76. Log_info(ctx->logger, "Got request to set IP");
  77. struct Sockaddr_storage ss;
  78. if (Sockaddr_parse(address->bytes, &ss)) {
  79. Dict d = Dict_CONST(
  80. String_CONST("error"), String_OBJ(String_CONST("Failed to parse addesss")), NULL
  81. );
  82. Admin_sendMessage(&d, txid, ctx->admin);
  83. } else {
  84. struct Allocator* tempAlloc = Allocator_child(ctx->alloc);
  85. adminAddIp2(interfaceName->bytes, &ss.addr, prefixLen, txid, tempAlloc, ctx);
  86. Allocator_free(tempAlloc);
  87. }
  88. }
  89. #ifdef HAS_ETH_INTERFACE
  90. static void ethListDevices(Dict* args, void* vcontext, String* txid, struct Allocator* requestAlloc)
  91. {
  92. struct AngelContext* ctx = Identity_check((struct AngelContext*) vcontext);
  93. List* devices = NULL;
  94. struct Jmp jmp;
  95. Jmp_try(jmp) {
  96. devices = ETHInterface_listDevices(requestAlloc, &jmp.handler);
  97. } Jmp_catch {
  98. Dict* out = Dict_new(requestAlloc);
  99. Dict_putString(out, String_CONST("error"), String_CONST(jmp.message), requestAlloc);
  100. Admin_sendMessage(out, txid, ctx->admin);
  101. return;
  102. }
  103. Dict* out = Dict_new(requestAlloc);
  104. Dict_putString(out, String_CONST("error"), String_CONST("none"), requestAlloc);
  105. Dict_putList(out, String_CONST("devices"), devices, requestAlloc);
  106. Admin_sendMessage(out, txid, ctx->admin);
  107. }
  108. #endif
  109. void Angel_start(struct Iface* coreIface,
  110. struct EventBase* eventBase,
  111. struct Log* logger,
  112. struct Allocator* alloc)
  113. {
  114. struct AngelContext ctx = {
  115. .eventBase = eventBase,
  116. .logger = logger,
  117. .coreIface = coreIface,
  118. .alloc = alloc
  119. };
  120. Identity_set(&ctx);
  121. struct AddrIfaceAdapter* addrIfAdapt = AddrIfaceAdapter_new(alloc);
  122. Iface_plumb(&addrIfAdapt->inputIf, coreIface);
  123. // this is inside of a pipe so the password is unimportant.
  124. String* passwd = String_new("null", alloc);
  125. ctx.admin = Admin_new(&addrIfAdapt->generic, NULL, eventBase, passwd);
  126. Admin_registerFunction("Angel_exit", adminExit, &ctx, false, NULL, ctx.admin);
  127. #ifdef HAS_ETH_INTERFACE
  128. Admin_registerFunction(
  129. "ETHInterface_listDevices", ethListDevices, &ctx, false, NULL, ctx.admin);
  130. #endif
  131. Admin_registerFunction("Angel_addIp", adminAddIp, &ctx, false, ((struct Admin_FunctionArg[]) {
  132. { .name = "interfaceName", .required = 1, .type = "String" },
  133. { .name = "address", .required = 1, .type = "String" },
  134. { .name = "prefixLen", .required = 1, .type = "Int" }
  135. }), ctx.admin);
  136. EventBase_beginLoop(eventBase);
  137. }