UDPInterface_admin.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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 "benc/Int.h"
  16. #include "admin/Admin.h"
  17. #include "exception/Jmp.h"
  18. #include "interface/UDPInterface.h"
  19. #include "memory/Allocator.h"
  20. #include "interface/InterfaceController.h"
  21. #include "util/events/EventBase.h"
  22. #include "crypto/Key.h"
  23. struct Context
  24. {
  25. struct EventBase* eventBase;
  26. struct Allocator* allocator;
  27. struct Log* logger;
  28. struct Admin* admin;
  29. struct InterfaceController* ic;
  30. uint32_t ifCount;
  31. struct UDPInterface** ifaces;
  32. };
  33. static void beginConnection(Dict* args, void* vcontext, String* txid)
  34. {
  35. struct Context* ctx = vcontext;
  36. String* password = Dict_getString(args, String_CONST("password"));
  37. String* publicKey = Dict_getString(args, String_CONST("publicKey"));
  38. String* address = Dict_getString(args, String_CONST("address"));
  39. int64_t* interfaceNumber = Dict_getInt(args, String_CONST("interfaceNumber"));
  40. uint32_t ifNum = (interfaceNumber) ? ((uint32_t) *interfaceNumber) : 0;
  41. String* error = NULL;
  42. uint8_t pkBytes[32];
  43. int ret;
  44. if (ctx->ifCount == 0) {
  45. error = String_CONST("no interfaces are setup, call UDPInterface_new() first");
  46. } else if (interfaceNumber && (*interfaceNumber >= ctx->ifCount || *interfaceNumber < 0)) {
  47. error = String_CONST("invalid interfaceNumber");
  48. } else if ((ret = Key_parse(publicKey, pkBytes, NULL))) {
  49. error = String_CONST(Key_parse_strerror(ret));
  50. } else {
  51. struct UDPInterface* udpif = ctx->ifaces[ifNum];
  52. switch (UDPInterface_beginConnection(address->bytes, pkBytes, password, udpif)) {
  53. case UDPInterface_beginConnection_OUT_OF_SPACE:
  54. error = String_CONST("no more space to register with the switch.");
  55. break;
  56. case UDPInterface_beginConnection_BAD_KEY:
  57. error = String_CONST("invalid cjdns public key.");
  58. break;
  59. case UDPInterface_beginConnection_BAD_ADDRESS:
  60. error = String_CONST("unable to parse ip address and port.");
  61. break;
  62. case UDPInterface_beginConnection_ADDRESS_MISMATCH:
  63. error = String_CONST("different address type than this socket is bound to.");
  64. break;
  65. case 0:
  66. error = String_CONST("none");
  67. break;
  68. default:
  69. error = String_CONST("unknown error");
  70. }
  71. }
  72. Dict out = Dict_CONST(String_CONST("error"), String_OBJ(error), NULL);
  73. Admin_sendMessage(&out, txid, ctx->admin);
  74. }
  75. static void newInterface2(struct Context* ctx,
  76. struct Sockaddr* addr,
  77. String* txid)
  78. {
  79. struct Allocator* const alloc = Allocator_child(ctx->allocator);
  80. struct UDPInterface* udpIf = NULL;
  81. struct Jmp jmp;
  82. Jmp_try(jmp) {
  83. udpIf = UDPInterface_new(ctx->eventBase, addr, alloc, &jmp.handler, ctx->logger, ctx->ic);
  84. } Jmp_catch {
  85. String* errStr = String_CONST(jmp.message);
  86. Dict out = Dict_CONST(String_CONST("error"), String_OBJ(errStr), NULL);
  87. Admin_sendMessage(&out, txid, ctx->admin);
  88. Allocator_free(alloc);
  89. }
  90. // sizeof(struct UDPInterface*) the size of a pointer.
  91. ctx->ifaces = Allocator_realloc(ctx->allocator,
  92. ctx->ifaces,
  93. sizeof(struct UDPInterface*) * (ctx->ifCount + 1));
  94. ctx->ifaces[ctx->ifCount] = udpIf;
  95. Dict out = Dict_CONST(
  96. String_CONST("error"), String_OBJ(String_CONST("none")), Dict_CONST(
  97. String_CONST("interfaceNumber"), Int_OBJ(ctx->ifCount), NULL
  98. ));
  99. Admin_sendMessage(&out, txid, ctx->admin);
  100. ctx->ifCount++;
  101. }
  102. static void newInterface(Dict* args, void* vcontext, String* txid)
  103. {
  104. struct Context* ctx = vcontext;
  105. String* bindAddress = Dict_getString(args, String_CONST("bindAddress"));
  106. struct Sockaddr_storage addr;
  107. if (Sockaddr_parse((bindAddress) ? bindAddress->bytes : "0.0.0.0", &addr)) {
  108. Dict out = Dict_CONST(
  109. String_CONST("error"), String_OBJ(String_CONST("Failed to parse address")), NULL
  110. );
  111. Admin_sendMessage(&out, txid, ctx->admin);
  112. return;
  113. }
  114. newInterface2(ctx, &addr.addr, txid);
  115. }
  116. void UDPInterface_admin_register(struct EventBase* base,
  117. struct Allocator* allocator,
  118. struct Log* logger,
  119. struct Admin* admin,
  120. struct InterfaceController* ic)
  121. {
  122. struct Context* ctx = Allocator_clone(allocator, (&(struct Context) {
  123. .eventBase = base,
  124. .allocator = allocator,
  125. .logger = logger,
  126. .admin = admin,
  127. .ic = ic
  128. }));
  129. struct Admin_FunctionArg adma[1] = {
  130. { .name = "bindAddress", .required = 0, .type = "String" }
  131. };
  132. Admin_registerFunction("UDPInterface_new", newInterface, ctx, true, adma, admin);
  133. struct Admin_FunctionArg adma2[4] = {
  134. { .name = "interfaceNumber", .required = 0, .type = "Int" },
  135. { .name = "password", .required = 0, .type = "String" },
  136. { .name = "publicKey", .required = 1, .type = "String" },
  137. { .name = "address", .required = 1, .type = "String" }
  138. };
  139. Admin_registerFunction("UDPInterface_beginConnection",
  140. beginConnection, ctx, true, adma2, admin);
  141. }