From e2150df5da6b9dd0abec7bc8a8bff01d74607f37 Mon Sep 17 00:00:00 2001 From: angt Date: Mon, 9 Nov 2015 06:47:33 +0100 Subject: [PATCH] Add option.[ch] --- Makefile.am | 2 +- glorytun.c | 143 +++++----------------------------------------------- option.c | 117 ++++++++++++++++++++++++++++++++++++++++++ option.h | 14 +++++ 4 files changed, 145 insertions(+), 131 deletions(-) create mode 100644 option.c create mode 100644 option.h diff --git a/Makefile.am b/Makefile.am index e1a9b48..5e26a37 100644 --- a/Makefile.am +++ b/Makefile.am @@ -1,3 +1,3 @@ bin_PROGRAMS = glorytun -glorytun_SOURCES = glorytun.c common.h common-static.h +glorytun_SOURCES = glorytun.c option.c common.h common-static.h glorytun_LDADD = $(libsodium_LIBS) diff --git a/glorytun.c b/glorytun.c index 91f1180..c3706bc 100644 --- a/glorytun.c +++ b/glorytun.c @@ -1,4 +1,5 @@ #include "common-static.h" +#include "option.h" #include #include @@ -27,12 +28,6 @@ #define GT_BUFFER_SIZE (4*1024*1024) -struct option { - char *name; - void *data; - int (*call) (void *, int, char **); -}; - struct netio { int fd; struct { @@ -217,18 +212,18 @@ static socklen_t sk_get_info (int fd, struct tcp_info *ti) static void print_tcp_info (struct tcp_info *ti) { fprintf(stderr, "tcpinfo" - " rto:%" PRIu32 " ato:%" PRIu32 " snd_mss:%" PRIu32 - " rcv_mss:%" PRIu32 " unacked:%" PRIu32 " sacked:%" PRIu32 - " lost:%" PRIu32 " retrans:%" PRIu32 " fackets:%" PRIu32 - " pmtu:%" PRIu32 " rcv_ssthresh:%" PRIu32 " rtt:%" PRIu32 - " rttvar:%" PRIu32 " snd_ssthresh:%" PRIu32 " snd_cwnd:%" PRIu32 - " advmss:%" PRIu32 " reordering:%" PRIu32 "\n", - ti->tcpi_rto, ti->tcpi_ato, ti->tcpi_snd_mss, - ti->tcpi_rcv_mss, ti->tcpi_unacked, ti->tcpi_sacked, - ti->tcpi_lost, ti->tcpi_retrans, ti->tcpi_fackets, - ti->tcpi_pmtu, ti->tcpi_rcv_ssthresh, ti->tcpi_rtt, - ti->tcpi_rttvar, ti->tcpi_snd_ssthresh, ti->tcpi_snd_cwnd, - ti->tcpi_advmss, ti->tcpi_reordering); + " rto:%" PRIu32 " ato:%" PRIu32 " snd_mss:%" PRIu32 + " rcv_mss:%" PRIu32 " unacked:%" PRIu32 " sacked:%" PRIu32 + " lost:%" PRIu32 " retrans:%" PRIu32 " fackets:%" PRIu32 + " pmtu:%" PRIu32 " rcv_ssthresh:%" PRIu32 " rtt:%" PRIu32 + " rttvar:%" PRIu32 " snd_ssthresh:%" PRIu32 " snd_cwnd:%" PRIu32 + " advmss:%" PRIu32 " reordering:%" PRIu32 "\n", + ti->tcpi_rto, ti->tcpi_ato, ti->tcpi_snd_mss, + ti->tcpi_rcv_mss, ti->tcpi_unacked, ti->tcpi_sacked, + ti->tcpi_lost, ti->tcpi_retrans, ti->tcpi_fackets, + ti->tcpi_pmtu, ti->tcpi_rcv_ssthresh, ti->tcpi_rtt, + ti->tcpi_rttvar, ti->tcpi_snd_ssthresh, ti->tcpi_snd_cwnd, + ti->tcpi_advmss, ti->tcpi_reordering); } #endif @@ -481,118 +476,6 @@ static int decrypt_packet (struct crypto_ctx *ctx, uint8_t *packet, size_t size, return 0; } -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; -} - -_unused_ -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 valid 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 *opts = (struct option *)data; - - for (int i=1; i40) - slen = 12; - - for (int k=0; opts[k].name; k++) { - int isflag = opts[k].call==option_flag; - size_t inc = str_len(opts[k].name)+(isflag?0:4)+4; - - if (len+inc>60) { - printf("\n%*s", (int) slen, ""); - len = 0; - } - printf(" [%s%s]", opts[k].name, isflag?"":" ARG"); - len += inc; - } - - printf("\n"); -} - -static int option (struct option *opts, int argc, char **argv) -{ - int ret = option_option(opts, argc, argv); - - if (ret==argc) - return 0; - - if (ret<0 || ret+1>=argc) - return 1; - - printf("option `%s' is unknown\n", argv[ret+1]); - option_usage(opts, argv[0]); - - return 1; -} - static void set_ip_size (uint8_t *data, size_t size) { data[2] = 0xFF&(size>>8); diff --git a/option.c b/option.c new file mode 100644 index 0000000..ee2df6a --- /dev/null +++ b/option.c @@ -0,0 +1,117 @@ +#include "common-static.h" + +#include +#include + +#include "option.h" + +int option_flag (void *data, _unused_ int argc, _unused_ char **argv) +{ + const int one = 1; + byte_cpy(data, &one, sizeof(one)); + + return 0; +} + +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; +} + +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 valid integer\n", argv[1]); + return -1; + } + + byte_cpy(data, &val, sizeof(val)); + + return 1; +} + +int option_option (void *data, int argc, char **argv) +{ + struct option *opts = (struct option *)data; + + for (int i=1; i40) + slen = 12; + + for (int k=0; opts[k].name; k++) { + int isflag = opts[k].call==option_flag; + size_t inc = str_len(opts[k].name)+(isflag?0:4)+4; + + if (len+inc>60) { + printf("\n%*s", (int)slen, ""); + len = 0; + } + printf(" [%s%s]", opts[k].name, isflag?"":" ARG"); + len += inc; + } + + printf("\n"); +} + +int option (struct option *opts, int argc, char **argv) +{ + int ret = option_option(opts, argc, argv); + + if (ret==argc) + return 0; + + if (ret<0 || ret+1>=argc) + return 1; + + printf("option `%s' is unknown\n", argv[ret+1]); + option_usage(opts, argv[0]); + + return 1; +} diff --git a/option.h b/option.h new file mode 100644 index 0000000..07241d6 --- /dev/null +++ b/option.h @@ -0,0 +1,14 @@ +#pragma once + +struct option { + char *name; + void *data; + int (*call) (void *, int, char **); +}; + +int option_flag (void *, int, char **); +int option_str (void *, int, char **); +int option_long (void *, int, char **); +int option_option (void *, int, char **); + +int option (struct option *, int, char **);