From 4382ba70751a387cfe69fb75c82d5e429147d6d4 Mon Sep 17 00:00:00 2001 From: Linus Nordberg Date: Mon, 30 Sep 2019 08:44:34 +0200 Subject: server: add --leasetime as a commandline option --- common.h | 2 -- tests/netsh.sh | 2 +- wg-dynamic-server.c | 41 ++++++++++++++++++++++++++++++++++++----- 3 files changed, 37 insertions(+), 8 deletions(-) diff --git a/common.h b/common.h index 426ad20..c44e6c7 100644 --- a/common.h +++ b/common.h @@ -25,8 +25,6 @@ static const char WG_DYNAMIC_ADDR[] = "fe80::"; static const uint16_t WG_DYNAMIC_PORT = 970; /* ASCII sum of "wireguard" */ -#define WG_DYNAMIC_LEASETIME 10 /* NOTE: 10s is good for testing purposes */ - #define ITEMS \ E(WGKEY_UNKNOWN, "") /* must be the first entry */ \ /* CMD START */ \ diff --git a/tests/netsh.sh b/tests/netsh.sh index 0376c14..fff06d8 100755 --- a/tests/netsh.sh +++ b/tests/netsh.sh @@ -85,4 +85,4 @@ n2 wg set wg0 peer "$server_public" endpoint [::1]:1 n2 ping6 -c 10 -f -W 1 fe80::%wg0 n1 ping6 -c 10 -f -W 1 fe80::badc:0ffe:e0dd:f00d%wg0 -n1 ./wg-dynamic-server wg0 +n1 ./wg-dynamic-server --leasetime 10 wg0 diff --git a/wg-dynamic-server.c b/wg-dynamic-server.c index 3c1fb53..b53f857 100644 --- a/wg-dynamic-server.c +++ b/wg-dynamic-server.c @@ -6,6 +6,7 @@ #define _GNU_SOURCE #define _POSIX_C_SOURCE 200112L +#include #include #include #include @@ -28,11 +29,12 @@ #include "netlink.h" static const char *progname; -static const char *wg_interface; +static const char *wg_interface = NULL; static struct in6_addr well_known; static wg_device *device = NULL; static struct wg_dynamic_request requests[MAX_CONNECTIONS] = { 0 }; +static uint32_t leasetime = 3600; static int sockfd = -1; static int epollfd = -1; @@ -48,7 +50,9 @@ struct mnl_cb_data { static void usage() { - die("usage: %s \n", progname); + fprintf(stderr, "usage: %s [--leasetime ] \n", + progname); + exit(EXIT_FAILURE); } static int data_cb(const struct nlmsghdr *nlh, void *data) @@ -269,7 +273,6 @@ static int response_request_ip(struct wg_dynamic_attr *cur, wg_key pubkey, { struct in_addr *ipv4 = NULL; struct in6_addr *ipv6 = NULL; - uint32_t leasetime = WG_DYNAMIC_LEASETIME; while (cur) { switch (cur->key) { @@ -529,10 +532,38 @@ static void poll_loop() int main(int argc, char *argv[]) { progname = argv[0]; - if (argc != 2) + + while (1) { + int ret, index; + char *endptr = NULL; + const struct option options[] = { + { "leasetime", required_argument, NULL, 0 }, + { 0, 0, 0, 0 } + }; + + ret = getopt_long(argc, argv, "", options, &index); + if (ret == -1) + break; + + switch (ret) { + case 0: + if (index != 0) + usage(); + leasetime = (uint32_t)strtoul(optarg, &endptr, 10); + if (*endptr) + usage(); + break; + default: + usage(); + } + } + + if (optind < argc) + wg_interface = argv[optind++]; + + if (!wg_interface || optind < argc) usage(); - wg_interface = argv[1]; setup(); poll_loop(); -- cgit v1.2.3-59-g8ed1b