From 64f825db92ce31102b29ac96fd382ac3643fb6ae Mon Sep 17 00:00:00 2001 From: Tushar Pankaj Date: Mon, 12 Nov 2018 17:06:07 -0600 Subject: Write client connect_to_server Signed-off-by: Tushar Pankaj --- client.c | 38 +++++++++++++++++++++++++++++++++++--- client.h | 6 +++++- protocol.h | 12 ++++++++++++ 3 files changed, 52 insertions(+), 4 deletions(-) create mode 100644 protocol.h diff --git a/client.c b/client.c index 2c2f091..1a839b9 100644 --- a/client.c +++ b/client.c @@ -3,8 +3,40 @@ * Copyright (C) 2018 Wireguard LLC */ -int connect_to_server(const char interface[]) +#include +#include +#include +#include +#include +#include +#include +#include "protocol.h" +#include "client.h" + +bool is_server_in_allowed_ips(const char interface[]) +{ + /* TODO: check if IP is in wg allowed ips, etc */ + return true; +} + +int connect_to_server() +{ + int sock = -1; + struct sockaddr_in6 addr; + + sock = socket(AF_INET6, SOCK_STREAM, 0); + addr.sin6_family = AF_INET6; + addr.sin6_port = htons(WG_DYNAMIC_SERVER_PORT); + inet_pton(AF_INET6, WG_DYNAMIC_SERVER_IP, &addr.sin6_addr); + connect(sock, (struct sockaddr *)&addr, sizeof(addr)); + + return sock; +} + +void close_connection(int sock) { - /* TODO */ - return -1; + if (close(sock) < 0) { + perror("error closing socket to server"); + exit(EXIT_FAILURE); + } } diff --git a/client.h b/client.h index 996cb58..8dd35db 100644 --- a/client.h +++ b/client.h @@ -6,6 +6,10 @@ #ifndef CLIENT_H #define CLIENT_H -int connect_to_server(const char interface[]); +#include + +bool is_server_in_allowed_ips(const char interface[]); +int connect_to_server(); +void close_connection(int sock); #endif diff --git a/protocol.h b/protocol.h new file mode 100644 index 0000000..ff6c04d --- /dev/null +++ b/protocol.h @@ -0,0 +1,12 @@ +/* SPDX-License-Identifier: MIT */ +/* + * Copyright (C) 2018 Wireguard LLC + */ + +#ifndef PROTOCOL_H +#define PROTOCOL_H + +#define WG_DYNAMIC_SERVER_IP "::1" +#define WG_DYNAMIC_SERVER_PORT 51820 + +#endif -- cgit v1.2.3-59-g8ed1b