aboutsummaryrefslogtreecommitdiffstats
path: root/server.c
diff options
context:
space:
mode:
authorTushar Pankaj <tushar.s.pankaj@gmail.com>2018-11-20 20:24:47 -0600
committerTushar Pankaj <tushar.s.pankaj@gmail.com>2018-11-20 20:24:47 -0600
commit88634c81e5a1b243caf9ecff0402a79dc93c8b59 (patch)
tree176b25a48245d85a14929770f48e44075686bf73 /server.c
parentFix bugs in server address check (diff)
downloadwg-dynamic-88634c81e5a1b243caf9ecff0402a79dc93c8b59.tar.xz
wg-dynamic-88634c81e5a1b243caf9ecff0402a79dc93c8b59.zip
Get capnproto decode of WgClientMsg workingtp/protocol_draft
Signed-off-by: Tushar Pankaj <tushar.s.pankaj@gmail.com>
Diffstat (limited to 'server.c')
-rw-r--r--server.c54
1 files changed, 51 insertions, 3 deletions
diff --git a/server.c b/server.c
index 7f2dce9..d819032 100644
--- a/server.c
+++ b/server.c
@@ -7,12 +7,14 @@
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
+#include <signal.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include "wireguard.h"
#include "protocol.h"
+#include "protocol.capnp.h"
#include "server.h"
bool is_wg_up_on_iface(const char iface[])
@@ -27,7 +29,7 @@ bool is_wg_up_on_iface(const char iface[])
}
}
-int setup_server()
+int setup_server(void)
{
int sock = -1;
int reuseaddr = 1;
@@ -54,9 +56,51 @@ int setup_server()
return sock;
}
+static void handle_simpleipv4_request(int conn, struct sockaddr_in6 addr)
+{
+ printf("Entering simple ipv4 request handler!\n");
+}
+
static void handle_connection(int conn, struct sockaddr_in6 addr)
{
- /* TODO */
+ /* get client message */
+ unsigned char client_buf[WgClientMsg_struct_bytes_count];
+ if (recv(conn, client_buf, WgClientMsg_struct_bytes_count, 0) < 0) {
+ perror("recv failed");
+ return;
+ }
+
+ /* init capnproto */
+ struct capn rc;
+ int init_mem_ret = capn_init_mem(&rc, client_buf,
+ WgClientMsg_struct_bytes_count, 0);
+ if (init_mem_ret != 0) {
+ fprintf(stderr, "error initializing capnproto memory\n");
+ return;
+ }
+
+ /* deserialize client message */
+ WgClientMsg_ptr client_root;
+ struct WgClientMsg client_msg;
+ client_root.p = capn_getp(capn_root(&rc), 0, 1);
+ read_WgClientMsg(&client_msg, client_root);
+
+ /* free capnproto */
+ capn_free(&rc);
+
+ /* handle client request */
+ switch (client_msg.request) {
+ case WgClientMsg_WgClientRequestType_simpleIpv4:
+ handle_simpleipv4_request(conn, addr);
+ break;
+ }
+}
+
+static void catch_sigchld(int signo)
+{
+ if (signo == SIGCHLD) {
+ wait(NULL);
+ }
}
int handle_connections(int sock)
@@ -65,7 +109,11 @@ int handle_connections(int sock)
pid_t pid = -1;
struct sockaddr_in6 addr;
socklen_t addr_size = sizeof(addr);
- ;
+
+ if (signal(SIGCHLD, catch_sigchld) == SIG_ERR) {
+ return -errno;
+ }
+
while (1) {
conn = accept(sock, (struct sockaddr *)&addr, &addr_size);
if (conn < 0) {