UDPInterface_admin.c 6.4 KB

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