UDPInterface_admin.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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 "memory/Allocator.h"
  19. #include "interface/InterfaceController.h"
  20. #include "interface/addressable/UDPAddrInterface.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* alloc;
  28. struct Log* logger;
  29. struct Admin* admin;
  30. struct AddrInterface* udpIf;
  31. struct InterfaceController* ic;
  32. };
  33. static void beginConnection(Dict* args,
  34. void* vcontext,
  35. String* txid,
  36. struct Allocator* requestAlloc)
  37. {
  38. struct Context* ctx = vcontext;
  39. String* password = Dict_getString(args, String_CONST("password"));
  40. String* publicKey = Dict_getString(args, String_CONST("publicKey"));
  41. String* address = Dict_getString(args, String_CONST("address"));
  42. int64_t* interfaceNumber = Dict_getInt(args, String_CONST("interfaceNumber"));
  43. uint32_t ifNum = (interfaceNumber) ? ((uint32_t) *interfaceNumber) : 0;
  44. String* error = NULL;
  45. Log_debug(ctx->logger, "Peering with [%s]", publicKey->bytes);
  46. struct Sockaddr_storage ss;
  47. uint8_t pkBytes[32];
  48. int ret;
  49. if (interfaceNumber && *interfaceNumber < 0) {
  50. error = String_CONST("negative interfaceNumber");
  51. } else if ((ret = Key_parse(publicKey, pkBytes, NULL))) {
  52. error = String_CONST(Key_parse_strerror(ret));
  53. } else if (Sockaddr_parse(address->bytes, &ss)) {
  54. error = String_CONST("unable to parse ip address and port.");
  55. } else if (Sockaddr_getFamily(&ss.addr) != Sockaddr_getFamily(ctx->udpIf->addr)) {
  56. error = String_CONST("different address type than this socket is bound to.");
  57. } else {
  58. struct Sockaddr* addr = &ss.addr;
  59. char* addrPtr = NULL;
  60. int addrLen = Sockaddr_getAddress(&ss.addr, &addrPtr);
  61. Assert_true(addrLen > 0);
  62. struct Allocator* tempAlloc = Allocator_child(ctx->alloc);
  63. if (Bits_isZero(addrPtr, addrLen)) {
  64. // unspec'd address, convert to loopback
  65. if (Sockaddr_getFamily(addr) == Sockaddr_AF_INET) {
  66. addr = Sockaddr_clone(Sockaddr_LOOPBACK, tempAlloc);
  67. } else if (Sockaddr_getFamily(addr) == Sockaddr_AF_INET6) {
  68. addr = Sockaddr_clone(Sockaddr_LOOPBACK6, tempAlloc);
  69. } else {
  70. Assert_failure("Sockaddr which is not AF_INET nor AF_INET6");
  71. }
  72. Sockaddr_setPort(addr, Sockaddr_getPort(&ss.addr));
  73. }
  74. int ret =
  75. InterfaceController_bootstrapPeer(ctx->ic, ifNum, pkBytes, addr, password, ctx->alloc);
  76. Allocator_free(tempAlloc);
  77. if (ret) {
  78. switch(ret) {
  79. case InterfaceController_bootstrapPeer_BAD_IFNUM:
  80. error = String_CONST("no such interface for interfaceNumber");
  81. break;
  82. case InterfaceController_bootstrapPeer_BAD_KEY:
  83. error = String_CONST("invalid cjdns public key.");
  84. break;
  85. case InterfaceController_bootstrapPeer_OUT_OF_SPACE:
  86. error = String_CONST("no more space to register with the switch.");
  87. break;
  88. default:
  89. error = String_CONST("unknown error");
  90. break;
  91. }
  92. } else {
  93. error = String_CONST("none");
  94. }
  95. }
  96. Dict out = Dict_CONST(String_CONST("error"), String_OBJ(error), NULL);
  97. Admin_sendMessage(&out, txid, ctx->admin);
  98. }
  99. static void newInterface2(struct Context* ctx,
  100. struct Sockaddr* addr,
  101. String* txid,
  102. struct Allocator* requestAlloc)
  103. {
  104. struct Allocator* const alloc = Allocator_child(ctx->alloc);
  105. struct AddrInterface* udpIf = NULL;
  106. struct Jmp jmp;
  107. Jmp_try(jmp) {
  108. udpIf = UDPAddrInterface_new(ctx->eventBase, addr, alloc, &jmp.handler, ctx->logger);
  109. } Jmp_catch {
  110. String* errStr = String_CONST(jmp.message);
  111. Dict out = Dict_CONST(String_CONST("error"), String_OBJ(errStr), NULL);
  112. Admin_sendMessage(&out, txid, ctx->admin);
  113. Allocator_free(alloc);
  114. return;
  115. }
  116. ctx->udpIf = udpIf;
  117. int ifNum = InterfaceController_regIface(ctx->ic, &udpIf->generic, String_CONST("UDP"), alloc);
  118. Dict* out = Dict_new(requestAlloc);
  119. Dict_putString(out, String_CONST("error"), String_CONST("none"), requestAlloc);
  120. Dict_putInt(out, String_CONST("interfaceNumber"), ifNum, requestAlloc);
  121. char* printedAddr = Sockaddr_print(udpIf->addr, requestAlloc);
  122. Dict_putString(out,
  123. String_CONST("bindAddress"),
  124. String_CONST(printedAddr),
  125. requestAlloc);
  126. Admin_sendMessage(out, txid, ctx->admin);
  127. }
  128. static void newInterface(Dict* args, void* vcontext, String* txid, struct Allocator* requestAlloc)
  129. {
  130. struct Context* ctx = vcontext;
  131. String* bindAddress = Dict_getString(args, String_CONST("bindAddress"));
  132. struct Sockaddr_storage addr;
  133. if (Sockaddr_parse((bindAddress) ? bindAddress->bytes : "0.0.0.0", &addr)) {
  134. Dict out = Dict_CONST(
  135. String_CONST("error"), String_OBJ(String_CONST("Failed to parse address")), NULL
  136. );
  137. Admin_sendMessage(&out, txid, ctx->admin);
  138. return;
  139. }
  140. newInterface2(ctx, &addr.addr, txid, requestAlloc);
  141. }
  142. void UDPInterface_admin_register(struct EventBase* base,
  143. struct Allocator* alloc,
  144. struct Log* logger,
  145. struct Admin* admin,
  146. struct InterfaceController* ic)
  147. {
  148. struct Context* ctx = Allocator_clone(alloc, (&(struct Context) {
  149. .eventBase = base,
  150. .alloc = alloc,
  151. .logger = logger,
  152. .admin = admin,
  153. .ic = ic
  154. }));
  155. struct Admin_FunctionArg adma[1] = {
  156. { .name = "bindAddress", .required = 0, .type = "String" }
  157. };
  158. Admin_registerFunction("UDPInterface_new", newInterface, ctx, true, adma, admin);
  159. struct Admin_FunctionArg adma2[4] = {
  160. { .name = "interfaceNumber", .required = 0, .type = "Int" },
  161. { .name = "password", .required = 0, .type = "String" },
  162. { .name = "publicKey", .required = 1, .type = "String" },
  163. { .name = "address", .required = 1, .type = "String" }
  164. };
  165. Admin_registerFunction("UDPInterface_beginConnection",
  166. beginConnection, ctx, true, adma2, admin);
  167. }