1
0

Angel.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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/Interface.h"
  23. #include "interface/addressable/AddrInterfaceAdapter.h"
  24. #include "util/events/EventBase.h"
  25. #include "util/log/Log.h"
  26. #include "util/Identity.h"
  27. #include <stdlib.h>
  28. struct AngelContext
  29. {
  30. struct Interface* coreIface;
  31. struct EventBase* eventBase;
  32. struct Allocator* alloc;
  33. struct Log* logger;
  34. struct Admin* admin;
  35. Identity
  36. };
  37. static void adminExit(Dict* args, void* vcontext, String* txid, struct Allocator* requestAlloc)
  38. {
  39. struct AngelContext* ctx = Identity_check((struct AngelContext*) vcontext);
  40. Log_info(ctx->logger, "Got request to exit");
  41. Dict d = Dict_CONST(String_CONST("error"), String_OBJ(String_CONST("none")), NULL);
  42. Admin_sendMessage(&d, txid, ctx->admin);
  43. exit(0);
  44. }
  45. static void adminAddIp2(char* interfaceName,
  46. struct Sockaddr* addr,
  47. int prefixLen,
  48. String* txid,
  49. struct Allocator* tempAlloc,
  50. struct AngelContext* ctx)
  51. {
  52. struct Jmp j;
  53. Jmp_try(j) {
  54. NetDev_addAddress(interfaceName, addr, prefixLen, NULL, &j.handler);
  55. } Jmp_catch {
  56. Dict d = Dict_CONST(
  57. String_CONST("error"), String_OBJ(String_CONST(j.message)), NULL
  58. );
  59. Admin_sendMessage(&d, txid, ctx->admin);
  60. return;
  61. }
  62. Dict d = Dict_CONST(
  63. String_CONST("error"), String_OBJ(String_CONST("none")), NULL
  64. );
  65. Admin_sendMessage(&d, txid, ctx->admin);
  66. }
  67. static void adminAddIp(Dict* args, void* vcontext, String* txid, struct Allocator* requestAlloc)
  68. {
  69. struct AngelContext* ctx = Identity_check((struct AngelContext*) vcontext);
  70. String* interfaceName = Dict_getString(args, String_CONST("interfaceName"));
  71. String* address = Dict_getString(args, String_CONST("address"));
  72. int64_t* prefixLenP = Dict_getInt(args, String_CONST("prefixLen"));
  73. int prefixLen = *prefixLenP;
  74. Log_info(ctx->logger, "Got request to set IP");
  75. struct Sockaddr_storage ss;
  76. if (Sockaddr_parse(address->bytes, &ss)) {
  77. Dict d = Dict_CONST(
  78. String_CONST("error"), String_OBJ(String_CONST("Failed to parse addesss")), NULL
  79. );
  80. Admin_sendMessage(&d, txid, ctx->admin);
  81. } else {
  82. struct Allocator* tempAlloc = Allocator_child(ctx->alloc);
  83. adminAddIp2(interfaceName->bytes, &ss.addr, prefixLen, txid, tempAlloc, ctx);
  84. Allocator_free(tempAlloc);
  85. }
  86. }
  87. void Angel_start(struct Interface* coreIface,
  88. struct EventBase* eventBase,
  89. struct Log* logger,
  90. struct Allocator* alloc)
  91. {
  92. struct AngelContext ctx = {
  93. .eventBase = eventBase,
  94. .logger = logger,
  95. .coreIface = coreIface,
  96. .alloc = alloc
  97. };
  98. Identity_set(&ctx);
  99. struct AddrInterface* addrIf = AddrInterfaceAdapter_new(coreIface, alloc);
  100. // this is inside of a pipe so the password is unimportant.
  101. String* passwd = String_new("null", alloc);
  102. ctx.admin = Admin_new(addrIf, alloc, NULL, eventBase, passwd);
  103. Admin_registerFunction("Angel_exit", adminExit, &ctx, false, NULL, ctx.admin);
  104. Admin_registerFunction("Angel_addIp", adminAddIp, &ctx, false, ((struct Admin_FunctionArg[]) {
  105. { .name = "interfaceName", .required = 1, .type = "String" },
  106. { .name = "address", .required = 1, .type = "String" },
  107. { .name = "prefixLen", .required = 1, .type = "Int" }
  108. }), ctx.admin);
  109. EventBase_beginLoop(eventBase);
  110. }