UDPInterface_admin.c 6.5 KB

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