1
0

UDPInterface_admin.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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 <https://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/events/FakeNetwork.h"
  23. #include "util/platform/Sockaddr.h"
  24. #include "crypto/Key.h"
  25. struct Context
  26. {
  27. struct EventBase* eventBase;
  28. struct Allocator* alloc;
  29. struct Log* logger;
  30. struct Admin* admin;
  31. struct AddrIface* udpIf;
  32. struct InterfaceController* ic;
  33. struct FakeNetwork* fakeNet;
  34. };
  35. static void beginConnection(Dict* args,
  36. void* vcontext,
  37. String* txid,
  38. struct Allocator* requestAlloc)
  39. {
  40. struct Context* ctx = vcontext;
  41. String* password = Dict_getStringC(args, "password");
  42. String* login = Dict_getStringC(args, "login");
  43. String* publicKey = Dict_getStringC(args, "publicKey");
  44. String* address = Dict_getStringC(args, "address");
  45. int64_t* interfaceNumber = Dict_getIntC(args, "interfaceNumber");
  46. uint32_t ifNum = (interfaceNumber) ? ((uint32_t) *interfaceNumber) : 0;
  47. String* peerName = Dict_getStringC(args, "peerName");
  48. String* error = NULL;
  49. Log_debug(ctx->logger, "Peering with [%s]", publicKey->bytes);
  50. struct Sockaddr_storage ss;
  51. uint8_t pkBytes[32];
  52. int ret;
  53. if (interfaceNumber && *interfaceNumber < 0) {
  54. error = String_CONST("negative interfaceNumber");
  55. } else if ((ret = Key_parse(publicKey, pkBytes, NULL))) {
  56. error = String_CONST(Key_parse_strerror(ret));
  57. } else if (Sockaddr_parse(address->bytes, &ss)) {
  58. error = String_CONST("unable to parse ip address and port.");
  59. } else if (Sockaddr_getFamily(&ss.addr) != Sockaddr_getFamily(ctx->udpIf->addr)) {
  60. error = String_CONST("different address type than this socket is bound to.");
  61. } else {
  62. struct Sockaddr* addr = &ss.addr;
  63. char* addrPtr = NULL;
  64. int addrLen = Sockaddr_getAddress(&ss.addr, &addrPtr);
  65. Assert_true(addrLen > 0);
  66. struct Allocator* tempAlloc = Allocator_child(ctx->alloc);
  67. if (Bits_isZero(addrPtr, addrLen)) {
  68. // unspec'd address, convert to loopback
  69. if (Sockaddr_getFamily(addr) == Sockaddr_AF_INET) {
  70. addr = Sockaddr_clone(Sockaddr_LOOPBACK, tempAlloc);
  71. } else if (Sockaddr_getFamily(addr) == Sockaddr_AF_INET6) {
  72. addr = Sockaddr_clone(Sockaddr_LOOPBACK6, tempAlloc);
  73. } else {
  74. Assert_failure("Sockaddr which is not AF_INET nor AF_INET6");
  75. }
  76. Sockaddr_setPort(addr, Sockaddr_getPort(&ss.addr));
  77. }
  78. int ret = InterfaceController_bootstrapPeer(
  79. ctx->ic, ifNum, pkBytes, addr, password, login, peerName, ctx->alloc);
  80. Allocator_free(tempAlloc);
  81. if (ret) {
  82. switch(ret) {
  83. case InterfaceController_bootstrapPeer_BAD_IFNUM:
  84. error = String_CONST("no such interface for interfaceNumber");
  85. break;
  86. case InterfaceController_bootstrapPeer_BAD_KEY:
  87. error = String_CONST("invalid cjdns public key.");
  88. break;
  89. case InterfaceController_bootstrapPeer_OUT_OF_SPACE:
  90. error = String_CONST("no more space to register with the switch.");
  91. break;
  92. default:
  93. error = String_CONST("unknown error");
  94. break;
  95. }
  96. } else {
  97. error = String_CONST("none");
  98. }
  99. }
  100. Dict out = Dict_CONST(String_CONST("error"), String_OBJ(error), NULL);
  101. Admin_sendMessage(&out, txid, ctx->admin);
  102. }
  103. static struct AddrIface* setupLibuvUDP(struct Context* ctx,
  104. struct Sockaddr* addr,
  105. uint8_t dscp,
  106. String* txid,
  107. struct Allocator* alloc)
  108. {
  109. struct UDPAddrIface* udpIf = NULL;
  110. struct Jmp jmp;
  111. Jmp_try(jmp) {
  112. udpIf = UDPAddrIface_new(ctx->eventBase, addr, alloc, &jmp.handler, ctx->logger);
  113. if (dscp) {
  114. if (UDPAddrIface_setDSCP(udpIf, dscp)) {
  115. Log_warn(ctx->logger, "Set DSCP failed");
  116. }
  117. }
  118. } Jmp_catch {
  119. String* errStr = String_CONST(jmp.message);
  120. Dict out = Dict_CONST(String_CONST("error"), String_OBJ(errStr), NULL);
  121. Admin_sendMessage(&out, txid, ctx->admin);
  122. Allocator_free(alloc);
  123. return NULL;
  124. }
  125. return &udpIf->generic;
  126. }
  127. static struct AddrIface* setupFakeUDP(struct FakeNetwork* fakeNet,
  128. struct Sockaddr* addr,
  129. struct Allocator* alloc)
  130. {
  131. struct FakeNetwork_UDPIface* fni = FakeNetwork_iface(fakeNet, addr, alloc);
  132. return &fni->generic;
  133. }
  134. static void newInterface2(struct Context* ctx,
  135. struct Sockaddr* addr,
  136. uint8_t dscp,
  137. String* txid,
  138. struct Allocator* requestAlloc)
  139. {
  140. struct Allocator* const alloc = Allocator_child(ctx->alloc);
  141. struct AddrIface* ai;
  142. if (ctx->fakeNet) {
  143. ai = setupFakeUDP(ctx->fakeNet, addr, alloc);
  144. } else {
  145. ai = setupLibuvUDP(ctx, addr, dscp, txid, alloc);
  146. }
  147. if (!ai) { return; }
  148. ctx->udpIf = ai;
  149. struct InterfaceController_Iface* ici =
  150. InterfaceController_newIface(ctx->ic, String_CONST("UDP"), alloc);
  151. Iface_plumb(&ici->addrIf, &ai->iface);
  152. Dict* out = Dict_new(requestAlloc);
  153. Dict_putStringCC(out, "error", "none", requestAlloc);
  154. Dict_putIntC(out, "interfaceNumber", ici->ifNum, requestAlloc);
  155. char* printedAddr = Sockaddr_print(ai->addr, requestAlloc);
  156. Dict_putStringCC(out,
  157. "bindAddress",
  158. printedAddr,
  159. requestAlloc);
  160. Admin_sendMessage(out, txid, ctx->admin);
  161. }
  162. static void newInterface(Dict* args, void* vcontext, String* txid, struct Allocator* requestAlloc)
  163. {
  164. struct Context* ctx = vcontext;
  165. String* bindAddress = Dict_getStringC(args, "bindAddress");
  166. int64_t* dscpValue = Dict_getIntC(args, "dscp");
  167. uint8_t dscp = dscpValue ? ((uint8_t) *dscpValue) : 0;
  168. struct Sockaddr_storage addr;
  169. if (Sockaddr_parse((bindAddress) ? bindAddress->bytes : "0.0.0.0", &addr)) {
  170. Dict out = Dict_CONST(
  171. String_CONST("error"), String_OBJ(String_CONST("Failed to parse address")), NULL
  172. );
  173. Admin_sendMessage(&out, txid, ctx->admin);
  174. return;
  175. }
  176. newInterface2(ctx, &addr.addr, dscp, txid, requestAlloc);
  177. }
  178. void UDPInterface_admin_register(struct EventBase* base,
  179. struct Allocator* alloc,
  180. struct Log* logger,
  181. struct Admin* admin,
  182. struct InterfaceController* ic,
  183. struct FakeNetwork* fakeNet)
  184. {
  185. struct Context* ctx = Allocator_clone(alloc, (&(struct Context) {
  186. .eventBase = base,
  187. .alloc = alloc,
  188. .logger = logger,
  189. .admin = admin,
  190. .ic = ic,
  191. .fakeNet = fakeNet
  192. }));
  193. Admin_registerFunction("UDPInterface_new", newInterface, ctx, true,
  194. ((struct Admin_FunctionArg[]) {
  195. { .name = "bindAddress", .required = 0, .type = "String" },
  196. { .name = "dscp", .required = 0, .type = "Int" }
  197. }), admin);
  198. Admin_registerFunction("UDPInterface_beginConnection", beginConnection, ctx, true,
  199. ((struct Admin_FunctionArg[]) {
  200. { .name = "interfaceNumber", .required = 0, .type = "Int" },
  201. { .name = "password", .required = 0, .type = "String" },
  202. { .name = "publicKey", .required = 1, .type = "String" },
  203. { .name = "address", .required = 1, .type = "String" },
  204. { .name = "login", .required = 0, .type = "String" }
  205. }), admin);
  206. }