Core_admin.c 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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/angel/Core.h"
  16. #include "memory/BufferAllocator.h"
  17. #include "exception/Jmp.h"
  18. #include "util/platform/Sockaddr.h"
  19. #include "admin/angel/Core_admin.h"
  20. struct Context
  21. {
  22. struct Sockaddr* ipAddr;
  23. struct Ducttape* ducttape;
  24. struct Log* logger;
  25. struct Allocator* alloc;
  26. struct Admin* admin;
  27. struct EventBase* eventBase;
  28. struct IpTunnel* ipTunnel;
  29. };
  30. static void sendResponse(String* error,
  31. struct Admin* admin,
  32. String* txid,
  33. struct Allocator* tempAlloc)
  34. {
  35. Dict* output = Dict_new(tempAlloc);
  36. Dict_putString(output, String_CONST("error"), error, tempAlloc);
  37. Admin_sendMessage(output, txid, admin);
  38. }
  39. static void initTunnel(Dict* args, void* vcontext, String* txid)
  40. {
  41. struct Context* const ctx = (struct Context*) vcontext;
  42. #define BUFFERSZ 1024
  43. uint8_t buffer[BUFFERSZ];
  44. struct Allocator* const alloc = BufferAllocator_new(buffer, BUFFERSZ);
  45. struct Jmp jmp;
  46. Jmp_try(jmp) {
  47. Core_initTunnel(Dict_getString(args, String_CONST("desiredTunName")),
  48. ctx->ipAddr,
  49. 8,
  50. ctx->ducttape,
  51. ctx->logger,
  52. ctx->ipTunnel,
  53. ctx->eventBase,
  54. ctx->alloc,
  55. &jmp.handler);
  56. } Jmp_catch {
  57. String* error = String_printf(alloc, "Failed to configure tunnel [%s]", jmp.message);
  58. sendResponse(error, ctx->admin, txid, alloc);
  59. return;
  60. }
  61. sendResponse(String_CONST("none"), ctx->admin, txid, alloc);
  62. }
  63. void Core_admin_register(struct Sockaddr* ipAddr,
  64. struct Ducttape* dt,
  65. struct Log* logger,
  66. struct IpTunnel* ipTunnel,
  67. struct Allocator* alloc,
  68. struct Admin* admin,
  69. struct EventBase* eventBase)
  70. {
  71. struct Context* ctx = Allocator_malloc(alloc, sizeof(struct Context));
  72. ctx->ipAddr = ipAddr;
  73. ctx->ducttape = dt;
  74. ctx->logger = logger;
  75. ctx->alloc = alloc;
  76. ctx->admin = admin;
  77. ctx->eventBase = eventBase;
  78. ctx->ipTunnel = ipTunnel;
  79. struct Admin_FunctionArg args[] = {
  80. { .name = "desiredTunName", .required = 0, .type = "String" }
  81. };
  82. Admin_registerFunction("Core_initTunnel", initTunnel, ctx, true, args, admin);
  83. }