aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLinus Nordberg <linus@nordberg.se>2019-09-30 08:44:34 +0200
committerThomas Gschwantner <tharre3@gmail.com>2019-12-11 06:21:37 +0100
commit4382ba70751a387cfe69fb75c82d5e429147d6d4 (patch)
treedff5c3c639f3a504534de2791bfedd7644129987
parentlease: handle allowedips updates and improve API (diff)
downloadwg-dynamic-4382ba70751a387cfe69fb75c82d5e429147d6d4.tar.xz
wg-dynamic-4382ba70751a387cfe69fb75c82d5e429147d6d4.zip
server: add --leasetime as a commandline option
-rw-r--r--common.h2
-rwxr-xr-xtests/netsh.sh2
-rw-r--r--wg-dynamic-server.c41
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 <getopt.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
@@ -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 <wg-interface>\n", progname);
+ fprintf(stderr, "usage: %s [--leasetime <leasetime>] <wg-interface>\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();