From 1722b5e05f7df409bd931332fe91536ae0a31719 Mon Sep 17 00:00:00 2001 From: angt Date: Fri, 30 Oct 2015 16:34:41 +0100 Subject: [PATCH] Add a more generic option system --- common.h | 2 + glorytun.c | 129 +++++++++++++++++++++++++++++++++++------------------ 2 files changed, 87 insertions(+), 44 deletions(-) diff --git a/common.h b/common.h index 9ca996d..4a0477c 100644 --- a/common.h +++ b/common.h @@ -14,6 +14,8 @@ #define PALIGN(x) ((void *)ALIGN((size_t)(x))) #define PALIGN_DOWN(x) ((void *)ALIGN_DOWN((size_t)(x))) +#define _unused_ __attribute__((unused)) + typedef struct buffer buffer_t; struct buffer { diff --git a/glorytun.c b/glorytun.c index d0f9bbe..1f4691c 100644 --- a/glorytun.c +++ b/glorytun.c @@ -18,6 +18,18 @@ #define GT_BUFFER_SIZE (32*1024) +struct option { + char *name; + void *data; + int (*call) (void *, int, char **); +}; + +struct netio { + int fd; + buffer_t recv; + buffer_t send; // TODO +}; + volatile sig_atomic_t running; static void fd_set_nonblock (int fd) @@ -259,52 +271,86 @@ static ssize_t fd_writev (int fd, const struct iovec *iov, int count) } */ -enum option_type { - option_flag, - option_string, -}; - -struct option { - char *name; - void *data; - enum option_type type; -}; - -static int option (int argc, char **argv, int n, struct option *opt) +static int option_flag (void *data, _unused_ int argc, _unused_ char **argv) { + const int one = 1; + byte_cpy(data, &one, sizeof(one)); + + return 0; +} + +static int option_str (void *data, int argc, char **argv) +{ + if (argc<2 || !argv[1]) { + printf("option `%s' need a string argument\n", argv[0]); + return -1; + } + + byte_cpy(data, &argv[1], sizeof(argv[1])); + + return 1; +} + +static int option_long (void *data, int argc, char **argv) +{ + if (argc<2 || !argv[1]) { + printf("option `%s' need an integer argument\n", argv[0]); + return -1; + } + + errno = 0; + char *end; + long val = strtol(argv[1], &end, 0); + + if (errno || argv[1]==end) { + printf("argument `%s' is not a valide integer\n", argv[1]); + return -1; + } + + byte_cpy(data, &val, sizeof(val)); + + return 1; +} + +static int option_option (void *data, int argc, char **argv) +{ + struct option *opt = (struct option *)data; + for (int i=1; i=0) + printf("option `%s' is unknown\n", argv[ret+1]); + + return 1; } static void set_ip_size (uint8_t *data, size_t size) @@ -324,12 +370,6 @@ static ssize_t get_ip_size (const uint8_t *data, size_t size) return 0; } -struct netio { - int fd; - buffer_t recv; - buffer_t send; // TODO -}; - int main (int argc, char **argv) { gt_set_signal(); @@ -341,14 +381,15 @@ int main (int argc, char **argv) char *congestion = NULL; struct option opts[] = { - { "dev", &dev, option_string }, - { "host", &host, option_string }, - { "port", &port, option_string }, - { "listener", &listener, option_flag }, - { "congestion", &congestion, option_string }, + { "dev", &dev, option_str }, + { "host", &host, option_str }, + { "port", &port, option_str }, + { "listener", &listener, option_flag }, + { "congestion", &congestion, option_str }, + { NULL }, }; - if (option(argc, argv, COUNT(opts), opts)) + if (option(opts, argc, argv)) return 1; struct addrinfo hints = {