diff options
author | Jason A. Donenfeld <Jason@zx2c4.com> | 2015-06-05 15:58:00 +0200 |
---|---|---|
committer | Jason A. Donenfeld <Jason@zx2c4.com> | 2016-06-25 16:48:39 +0200 |
commit | 99d303ac2739e65a02fbbc325b74ad6fcac63cc2 (patch) | |
tree | 6f4095f42d3d298cdd5ab8bc6f8ed89d9673b38b /src/tools/wg.c | |
download | wireguard-monolithic-historical-99d303ac2739e65a02fbbc325b74ad6fcac63cc2.tar.xz wireguard-monolithic-historical-99d303ac2739e65a02fbbc325b74ad6fcac63cc2.zip |
Initial commit
Diffstat (limited to 'src/tools/wg.c')
-rw-r--r-- | src/tools/wg.c | 66 |
1 files changed, 66 insertions, 0 deletions
diff --git a/src/tools/wg.c b/src/tools/wg.c new file mode 100644 index 0000000..d4d2965 --- /dev/null +++ b/src/tools/wg.c @@ -0,0 +1,66 @@ +/* Copyright 2015-2016 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved. */ + +#include <stddef.h> +#include <stdio.h> +#include <string.h> + +#include "subcommands.h" + +const char *PROG_NAME; + +static const struct { + const char *subcommand; + int (*function)(int, char**); + const char *description; +} subcommands[] = { + { "show", show_main, "Shows the current configuration and device information" }, + { "showconf", showconf_main, "Shows the current configuration of a given WireGuard interface, for use with `setconf`" }, + { "set", set_main, "Change the current configuration, add peers, remove peers, or change peers" }, + { "setconf", setconf_main, "Applies a configuration file to a WireGuard interface" }, + { "addconf", setconf_main, "Appends a configuration file to a WireGuard interface" }, + { "genkey", genkey_main, "Generates a new private key and writes it to stdout" }, + { "genpsk", genkey_main, "Generates a new pre-shared key and writes it to stdout" }, + { "pubkey", pubkey_main, "Reads a private key from stdin and writes a public key to stdout" } +}; + +static void show_usage(void) +{ + fprintf(stderr, "Usage: %s <cmd> [<args>]\n\n", PROG_NAME); + fprintf(stderr, "Available subcommands:\n"); + for (size_t i = 0; i < sizeof(subcommands) / sizeof(subcommands[0]); ++i) + fprintf(stderr, " %s: %s\n", subcommands[i].subcommand, subcommands[i].description); +} + +int main(int argc, char *argv[]) +{ + char *tmp = NULL; + PROG_NAME = argv[0]; + + if (argc == 2 && (!strcmp(argv[1], "-h") || !strcmp(argv[1], "--help") || !strcmp(argv[1], "help"))) { + show_usage(); + return 1; + } + + if (argc == 1) { + char *new_argv[] = { "show", NULL }; + return show_main(1, new_argv); + } + +findsubcommand: + for (size_t i = 0; i < sizeof(subcommands) / sizeof(subcommands[0]); ++i) { + if (!strcmp(argv[1], subcommands[i].subcommand)) + return subcommands[i].function(argc - 1, argv + 1); + } + + /* Crude way of supporting "wg wg0 show..." */ + if (!tmp && argc >= 3) { + tmp = argv[1]; + argv[1] = argv[2]; + argv[2] = tmp; + goto findsubcommand; + } + + fprintf(stderr, "Invalid subcommand: `%s`\n", argv[1]); + show_usage(); + return 1; +} |