UDPInterface_admin.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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 "net/InterfaceController.h"
  20. #include "util/events/UDPAddrIface.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 AddrIface* 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 UDPAddrIface* udpIf = NULL;
  106. struct Jmp jmp;
  107. Jmp_try(jmp) {
  108. udpIf = UDPAddrIface_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. struct AddrIface* ai = ctx->udpIf = &udpIf->generic;
  117. struct InterfaceController_Iface* ici =
  118. InterfaceController_newIface(ctx->ic, String_CONST("UDP"), alloc);
  119. Iface_plumb(&ici->addrIf, &ai->iface);
  120. Dict* out = Dict_new(requestAlloc);
  121. Dict_putString(out, String_CONST("error"), String_CONST("none"), requestAlloc);
  122. Dict_putInt(out, String_CONST("interfaceNumber"), ici->ifNum, requestAlloc);
  123. char* printedAddr = Sockaddr_print(ai->addr, requestAlloc);
  124. Dict_putString(out,
  125. String_CONST("bindAddress"),
  126. String_CONST(printedAddr),
  127. requestAlloc);
  128. Admin_sendMessage(out, txid, ctx->admin);
  129. }
  130. static void newInterface(Dict* args, void* vcontext, String* txid, struct Allocator* requestAlloc)
  131. {
  132. struct Context* ctx = vcontext;
  133. String* bindAddress = Dict_getString(args, String_CONST("bindAddress"));
  134. struct Sockaddr_storage addr;
  135. if (Sockaddr_parse((bindAddress) ? bindAddress->bytes : "0.0.0.0", &addr)) {
  136. Dict out = Dict_CONST(
  137. String_CONST("error"), String_OBJ(String_CONST("Failed to parse address")), NULL
  138. );
  139. Admin_sendMessage(&out, txid, ctx->admin);
  140. return;
  141. }
  142. newInterface2(ctx, &addr.addr, txid, requestAlloc);
  143. }
  144. void UDPInterface_admin_register(struct EventBase* base,
  145. struct Allocator* alloc,
  146. struct Log* logger,
  147. struct Admin* admin,
  148. struct InterfaceController* ic)
  149. {
  150. struct Context* ctx = Allocator_clone(alloc, (&(struct Context) {
  151. .eventBase = base,
  152. .alloc = alloc,
  153. .logger = logger,
  154. .admin = admin,
  155. .ic = ic
  156. }));
  157. struct Admin_FunctionArg adma[1] = {
  158. { .name = "bindAddress", .required = 0, .type = "String" }
  159. };
  160. Admin_registerFunction("UDPInterface_new", newInterface, ctx, true, adma, admin);
  161. struct Admin_FunctionArg adma2[4] = {
  162. { .name = "interfaceNumber", .required = 0, .type = "Int" },
  163. { .name = "password", .required = 0, .type = "String" },
  164. { .name = "publicKey", .required = 1, .type = "String" },
  165. { .name = "address", .required = 1, .type = "String" }
  166. };
  167. Admin_registerFunction("UDPInterface_beginConnection",
  168. beginConnection, ctx, true, adma2, admin);
  169. }