1
0

IpTunnel_admin.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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 "admin/Admin.h"
  16. #include "benc/String.h"
  17. #include "benc/Int.h"
  18. #include "benc/Dict.h"
  19. #include "benc/List.h"
  20. #include "crypto/Key.h"
  21. #include "memory/Allocator.h"
  22. #include "tunnel/IpTunnel.h"
  23. #include "tunnel/IpTunnel_admin.h"
  24. #include "util/platform/Sockaddr.h"
  25. #include <stddef.h>
  26. struct Context
  27. {
  28. struct IpTunnel* ipTun;
  29. struct Admin* admin;
  30. };
  31. static void sendResponse(int conn, String* txid, struct Admin* admin)
  32. {
  33. Dict resp = Dict_CONST(
  34. String_CONST("connection"), Int_OBJ(conn), Dict_CONST(
  35. String_CONST("error"), String_OBJ(String_CONST("none")), NULL
  36. ));
  37. Admin_sendMessage(&resp, txid, admin);
  38. }
  39. static void sendError(char* error, String* txid, struct Admin* admin)
  40. {
  41. Dict resp = Dict_CONST(
  42. String_CONST("error"), String_OBJ(String_CONST(error)), NULL
  43. );
  44. Admin_sendMessage(&resp, txid, admin);
  45. }
  46. static void allowConnection(Dict* args,
  47. void* vcontext,
  48. String* txid,
  49. struct Allocator* requestAlloc)
  50. {
  51. struct Context* context = (struct Context*) vcontext;
  52. String* publicKeyOfAuthorizedNode =
  53. Dict_getString(args, String_CONST("publicKeyOfAuthorizedNode"));
  54. String* ip6Address = Dict_getString(args, String_CONST("ip6Address"));
  55. String* ip4Address = Dict_getString(args, String_CONST("ip4Address"));
  56. uint8_t pubKey[32];
  57. uint8_t ip6Addr[16];
  58. struct Sockaddr_storage ip6ToGive;
  59. struct Sockaddr_storage ip4ToGive;
  60. char* error;
  61. int ret;
  62. if (!ip6Address && !ip4Address) {
  63. error = "Must specify ip6Address or ip4Address";
  64. } else if ((ret = Key_parse(publicKeyOfAuthorizedNode, pubKey, ip6Addr)) != 0) {
  65. error = Key_parse_strerror(ret);
  66. } else if (ip6Address
  67. && (Sockaddr_parse(ip6Address->bytes, &ip6ToGive)
  68. || Sockaddr_getFamily(&ip6ToGive.addr) != Sockaddr_AF_INET6))
  69. {
  70. error = "malformed ip6Address";
  71. } else if (ip4Address
  72. && (Sockaddr_parse(ip4Address->bytes, &ip4ToGive)
  73. || Sockaddr_getFamily(&ip4ToGive.addr) != Sockaddr_AF_INET))
  74. {
  75. error = "malformed ip4Address";
  76. } else {
  77. int conn = IpTunnel_allowConnection(pubKey,
  78. (ip6Address) ? &ip6ToGive.addr : NULL,
  79. (ip4Address) ? &ip4ToGive.addr : NULL,
  80. context->ipTun);
  81. sendResponse(conn, txid, context->admin);
  82. return;
  83. }
  84. sendError(error, txid, context->admin);
  85. }
  86. static void connectTo(Dict* args, void* vcontext, String* txid, struct Allocator* requestAlloc)
  87. {
  88. struct Context* context = vcontext;
  89. String* publicKeyOfNodeToConnectTo =
  90. Dict_getString(args, String_CONST("publicKeyOfNodeToConnectTo"));
  91. uint8_t pubKey[32];
  92. uint8_t ip6[16];
  93. int ret;
  94. if ((ret = Key_parse(publicKeyOfNodeToConnectTo, pubKey, ip6)) != 0) {
  95. sendError(Key_parse_strerror(ret), txid, context->admin);
  96. return;
  97. }
  98. int conn = IpTunnel_connectTo(pubKey, context->ipTun);
  99. sendResponse(conn, txid, context->admin);
  100. }
  101. static void removeConnection(Dict* args,
  102. void* vcontext,
  103. String* txid,
  104. struct Allocator* requestAlloc)
  105. {
  106. struct Context* context = vcontext;
  107. /*
  108. int conn = (int) *(Dict_getInt(args, String_CONST("connection")));
  109. char* error = "none";
  110. if (IpTunnel_removeConnection_NOT_FOUND == IpTunnel_removeConnection(conn, context->ipTun)) {
  111. error = "not found";
  112. }
  113. */
  114. sendError("not implemented", txid, context->admin);
  115. }
  116. static void listConnections(Dict* args,
  117. void* vcontext,
  118. String* txid,
  119. struct Allocator* alloc)
  120. {
  121. struct Context* context = vcontext;
  122. List* l = NULL;
  123. for (int i = 0; i < (int)context->ipTun->connectionList.count; i++) {
  124. l = List_addInt(l, context->ipTun->connectionList.connections[i].number, alloc);
  125. }
  126. Dict resp = Dict_CONST(
  127. String_CONST("connections"), List_OBJ(l), Dict_CONST(
  128. String_CONST("error"), String_OBJ(String_CONST("none")), NULL
  129. ));
  130. Admin_sendMessage(&resp, txid, context->admin);
  131. }
  132. static void showConn(struct IpTunnel_Connection* conn,
  133. String* txid,
  134. struct Admin* admin,
  135. struct Allocator* alloc)
  136. {
  137. Dict* d = Dict_new(alloc);
  138. if (!Bits_isZero(conn->connectionIp6, 16)) {
  139. struct Sockaddr* addr = Sockaddr_clone(Sockaddr_LOOPBACK6, alloc);
  140. uint8_t* address;
  141. Assert_always(16 == Sockaddr_getAddress(addr, &address));
  142. Bits_memcpyConst(address, conn->connectionIp6, 16);
  143. char* printedAddr = Sockaddr_print(addr, alloc);
  144. Dict_putString(d, String_CONST("ip6Address"), String_CONST(printedAddr), alloc);
  145. }
  146. if (!Bits_isZero(conn->connectionIp4, 4)) {
  147. struct Sockaddr* addr = Sockaddr_clone(Sockaddr_LOOPBACK, alloc);
  148. uint8_t* address;
  149. Assert_always(4 == Sockaddr_getAddress(addr, &address));
  150. Bits_memcpyConst(address, conn->connectionIp4, 4);
  151. char* printedAddr = Sockaddr_print(addr, alloc);
  152. Dict_putString(d, String_CONST("ip4Address"), String_CONST(printedAddr), alloc);
  153. }
  154. Dict_putString(d, String_CONST("key"), Key_stringify(conn->header.nodeKey, alloc), alloc);
  155. Dict_putInt(d, String_CONST("outgoing"), (conn->isOutgoing) ? 1 : 0, alloc);
  156. Dict_putString(d, String_CONST("error"), String_CONST("none"), alloc);
  157. Admin_sendMessage(d, txid, admin);
  158. }
  159. static void showConnection(Dict* args, void* vcontext, String* txid, struct Allocator* alloc)
  160. {
  161. struct Context* context = vcontext;
  162. int connNum = (int) *(Dict_getInt(args, String_CONST("connection")));
  163. for (int i = 0; i < (int)context->ipTun->connectionList.count; i++) {
  164. if (connNum == context->ipTun->connectionList.connections[i].number) {
  165. showConn(&context->ipTun->connectionList.connections[i], txid, context->admin, alloc);
  166. return;
  167. }
  168. }
  169. sendError("connection not found", txid, context->admin);
  170. }
  171. void IpTunnel_admin_register(struct IpTunnel* ipTun, struct Admin* admin, struct Allocator* alloc)
  172. {
  173. struct Context* context = Allocator_clone(alloc, (&(struct Context) {
  174. .admin = admin,
  175. .ipTun = ipTun
  176. }));
  177. Admin_registerFunction("IpTunnel_allowConnection", allowConnection, context, true,
  178. ((struct Admin_FunctionArg[]) {
  179. { .name = "publicKeyOfAuthorizedNode", .required = 1, .type = "String" },
  180. { .name = "ip6Address", .required = 0, .type = "String" },
  181. { .name = "ip4Address", .required = 0, .type = "String" },
  182. }), admin);
  183. Admin_registerFunction("IpTunnel_connectTo", connectTo, context, true,
  184. ((struct Admin_FunctionArg[]) {
  185. { .name = "publicKeyOfNodeToConnectTo", .required = 1, .type = "String" }
  186. }), admin);
  187. Admin_registerFunction("IpTunnel_removeConnection", removeConnection, context, true,
  188. ((struct Admin_FunctionArg[]) {
  189. { .name = "connection", .required = 1, .type = "Int" }
  190. }), admin);
  191. Admin_registerFunction("IpTunnel_showConnection", showConnection, context, true,
  192. ((struct Admin_FunctionArg[]) {
  193. { .name = "connection", .required = 1, .type = "Int" }
  194. }), admin);
  195. Admin_registerFunction("IpTunnel_listConnections", listConnections, context, true, NULL, admin);
  196. }