aboutsummaryrefslogtreecommitdiffstats
path: root/server.c
diff options
context:
space:
mode:
authorTushar Pankaj <tushar.s.pankaj@gmail.com>2018-11-12 18:34:29 -0600
committerTushar Pankaj <tushar.s.pankaj@gmail.com>2018-11-12 18:34:29 -0600
commit5f4e092e04755bcb89d2252d828de2de8d3d7aca (patch)
treef785dd089389c10483fdb74d4001d8fdef4c338e /server.c
parentWrite basic mains so project compiles (diff)
downloadwg-dynamic-5f4e092e04755bcb89d2252d828de2de8d3d7aca.tar.xz
wg-dynamic-5f4e092e04755bcb89d2252d828de2de8d3d7aca.zip
Implement basic server/client + protocol
Includes WireGuard submodule to use the embeddable library Squashed commit of the following: commit 3e5252d93c87c4cddf596d4030d736e2cea81557 Author: Tushar Pankaj <tushar.s.pankaj@gmail.com> Date: Mon Nov 12 18:33:33 2018 -0600 Implement server check iface Signed-off-by: Tushar Pankaj <tushar.s.pankaj@gmail.com> commit a30c60e51ecae887fd89654bdedddcf0decb766c Author: Tushar Pankaj <tushar.s.pankaj@gmail.com> Date: Mon Nov 12 18:26:57 2018 -0600 Add WireGuard embeddable library Signed-off-by: Tushar Pankaj <tushar.s.pankaj@gmail.com> commit ea6ab8ce09d4e84440185536f7b3e92346789233 Author: Tushar Pankaj <tushar.s.pankaj@gmail.com> Date: Mon Nov 12 17:59:39 2018 -0600 Working client and server comms Signed-off-by: Tushar Pankaj <tushar.s.pankaj@gmail.com> commit 5d717e37baaa9882ea2356fa24a2b85beaf51558 Author: Tushar Pankaj <tushar.s.pankaj@gmail.com> Date: Mon Nov 12 17:13:32 2018 -0600 Add IP addr structs to protocol Signed-off-by: Tushar Pankaj <tushar.s.pankaj@gmail.com> commit 8c31eec08257f77a9c95c7437b21bf7eb8106916 Author: Tushar Pankaj <tushar.s.pankaj@gmail.com> Date: Mon Nov 12 17:06:18 2018 -0600 Style fixes Signed-off-by: Tushar Pankaj <tushar.s.pankaj@gmail.com> commit 64f825db92ce31102b29ac96fd382ac3643fb6ae Author: Tushar Pankaj <tushar.s.pankaj@gmail.com> Date: Mon Nov 12 17:06:07 2018 -0600 Write client connect_to_server Signed-off-by: Tushar Pankaj <tushar.s.pankaj@gmail.com> commit f33225d130263eea481e28269b6b01a7cf75b0c8 Author: Tushar Pankaj <tushar.s.pankaj@gmail.com> Date: Fri Nov 9 20:00:37 2018 -0600 Add pthread library Signed-off-by: Tushar Pankaj <tushar.s.pankaj@gmail.com> commit 1f73168641d92917dc942c3d6cc200fb7f557674 Merge: 98fe966 3048896 Author: Tushar Pankaj <tushar.s.pankaj@gmail.com> Date: Wed Nov 7 10:59:13 2018 -0600 Merge branch 'master' into tp/protocol_draft commit 98fe966940eaf324b5d8d1edc64f6d0ff1334441 Author: Tushar Pankaj <tushar.s.pankaj@gmail.com> Date: Wed Nov 7 10:55:53 2018 -0600 Add Makefile library includes for capnp Signed-off-by: Tushar Pankaj <tushar.s.pankaj@gmail.com> commit 3bd7dffda7db8e9bf4cd0dad2455328508efb9b2 Author: Tushar Pankaj <tushar.s.pankaj@gmail.com> Date: Wed Nov 7 10:49:50 2018 -0600 Make protocol.capnp.o compile Signed-off-by: Tushar Pankaj <tushar.s.pankaj@gmail.com> commit 14e708783433816fd6cead46e2aa823ab8e41294 Author: Tushar Pankaj <tushar.s.pankaj@gmail.com> Date: Wed Nov 7 10:38:29 2018 -0600 Untested first draft of protocol.capnp Signed-off-by: Tushar Pankaj <tushar.s.pankaj@gmail.com> commit 5558c37a1a2626beaee5bb69c83d4013f2bcaf44 Author: Tushar Pankaj <tushar.s.pankaj@gmail.com> Date: Tue Nov 6 20:51:49 2018 -0600 Exclude capnproto from clang-format Signed-off-by: Tushar Pankaj <tushar.s.pankaj@gmail.com> commit 50042af6aec61c495c08d1b83f4c63288a30cc90 Author: Tushar Pankaj <tushar.s.pankaj@gmail.com> Date: Wed Oct 31 20:40:23 2018 -0500 Make better var names Signed-off-by: Tushar Pankaj <tushar.s.pankaj@gmail.com> commit 2352cbc91e6be9bcd73ca2794372b1ed8361448e Author: Tushar Pankaj <tushar.s.pankaj@gmail.com> Date: Wed Oct 31 20:08:05 2018 -0500 First draft of protocol Signed-off-by: Tushar Pankaj <tushar.s.pankaj@gmail.com>
Diffstat (limited to 'server.c')
-rw-r--r--server.c79
1 files changed, 77 insertions, 2 deletions
diff --git a/server.c b/server.c
index 0315ccb..318dc60 100644
--- a/server.c
+++ b/server.c
@@ -3,8 +3,83 @@
* Copyright (C) 2018 Wireguard LLC
*/
-int setup_server(const char interface[])
+#include <stdlib.h>
+#include <stdio.h>
+#include <unistd.h>
+#include <errno.h>
+#include <sys/types.h>
+#include <sys/wait.h>
+#include <sys/socket.h>
+#include <arpa/inet.h>
+#include "wireguard.h"
+#include "protocol.h"
+#include "server.h"
+
+bool is_wg_up_on_iface(const char iface[])
+{
+ wg_device *device;
+ if (wg_get_device(&device, iface) < 0) {
+ return false;
+ } else {
+ return true;
+ }
+}
+
+int setup_server()
+{
+ int sock = -1;
+ int reuseaddr = 1;
+ int ret;
+ struct sockaddr_in6 addr;
+
+ sock = socket(AF_INET6, SOCK_STREAM, 0);
+ if (sock < 0) {
+ return -errno;
+ }
+ setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &reuseaddr,
+ sizeof(reuseaddr));
+ addr.sin6_family = AF_INET6;
+ addr.sin6_port = htons(WG_DYNAMIC_SERVER_PORT);
+ inet_pton(AF_INET6, WG_DYNAMIC_SERVER_IP, &addr.sin6_addr);
+ ret = bind(sock, (struct sockaddr *)&addr, sizeof(addr));
+ if (ret < 0) {
+ return -errno;
+ }
+ ret = listen(sock, 5);
+ if (ret < 0) {
+ return -errno;
+ }
+ return sock;
+}
+
+static void handle_connection(int conn, struct sockaddr_in6 addr)
{
/* TODO */
- return -1;
+}
+
+int handle_connections(int sock)
+{
+ int conn = -1;
+ pid_t pid = -1;
+ struct sockaddr_in6 addr;
+ socklen_t addr_size = sizeof(addr);
+ ;
+ while (1) {
+ conn = accept(sock, (struct sockaddr *)&addr, &addr_size);
+ if (conn < 0) {
+ return -errno;
+ }
+ pid = fork();
+ if (pid < 0) {
+ return -errno;
+ } else if (pid == 0) {
+ close(sock);
+ handle_connection(conn, addr);
+ close(conn);
+ exit(EXIT_SUCCESS);
+ } else {
+ close(conn);
+ }
+ }
+ return 0;
}