aboutsummaryrefslogtreecommitdiffstats
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
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>
-rw-r--r--protocol.h1
-rw-r--r--server.c54
-rw-r--r--server.h2
-rw-r--r--wg_dynamic_client.c4
-rw-r--r--wg_dynamic_server.c2
5 files changed, 57 insertions, 6 deletions
diff --git a/protocol.h b/protocol.h
index ff6c04d..fd482a2 100644
--- a/protocol.h
+++ b/protocol.h
@@ -7,6 +7,7 @@
#define PROTOCOL_H
#define WG_DYNAMIC_SERVER_IP "::1"
+/*#define WG_DYNAMIC_SERVER_IP "fe80::"*/
#define WG_DYNAMIC_SERVER_PORT 51820
#endif
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) {
diff --git a/server.h b/server.h
index 43f8af2..ec8942c 100644
--- a/server.h
+++ b/server.h
@@ -8,6 +8,8 @@
#include <stdbool.h>
+#define BASE_DYN_IP 0xc0a80100
+
bool is_wg_up_on_iface(const char iface[]);
int setup_server();
int handle_connections(int sock);
diff --git a/wg_dynamic_client.c b/wg_dynamic_client.c
index 7862e9e..e18e429 100644
--- a/wg_dynamic_client.c
+++ b/wg_dynamic_client.c
@@ -30,11 +30,11 @@ int main(int argc, char *argv[])
iface = argv[1];
- if (!is_server_in_allowed_ips(iface)) {
+ /*if (!is_server_in_allowed_ips(iface)) {
fprintf(stderr, "server is not in allowed IPs for tunnel %s\n",
iface);
return EXIT_FAILURE;
- }
+ }*/
if ((sock = connect_to_server(argv[1])) < 0) {
fprintf(stderr, "error connecting to server: %s\n",
diff --git a/wg_dynamic_server.c b/wg_dynamic_server.c
index fb4fc44..80531ff 100644
--- a/wg_dynamic_server.c
+++ b/wg_dynamic_server.c
@@ -36,7 +36,7 @@ int main(int argc, char *argv[])
return EXIT_FAILURE;
}
- if ((sock = setup_server(argv[1])) < 0) {
+ if ((sock = setup_server()) < 0) {
fprintf(stderr, "error setting up server: %s\n",
strerror(-sock));
return EXIT_FAILURE;