diff options
author | Yves-Alexis Perez <corsac@debian.org> | 2015-06-01 14:46:30 +0200 |
---|---|---|
committer | Yves-Alexis Perez <corsac@debian.org> | 2015-06-01 14:46:30 +0200 |
commit | fc556ec2bc92a9d476c11406fad2c33db8bf7cb0 (patch) | |
tree | 7360889e50de867d72741213d534a756c73902c8 /src/swanctl | |
parent | 83b8aebb19fe6e49e13a05d4e8f5ab9a06177642 (diff) | |
download | vyos-strongswan-fc556ec2bc92a9d476c11406fad2c33db8bf7cb0.tar.gz vyos-strongswan-fc556ec2bc92a9d476c11406fad2c33db8bf7cb0.zip |
Imported Upstream version 5.3.1
Diffstat (limited to 'src/swanctl')
-rw-r--r-- | src/swanctl/command.c | 45 | ||||
-rw-r--r-- | src/swanctl/command.h | 2 | ||||
-rw-r--r-- | src/swanctl/commands/list_sas.c | 86 |
3 files changed, 121 insertions, 12 deletions
diff --git a/src/swanctl/command.c b/src/swanctl/command.c index 1c079ec3a..03cd8b959 100644 --- a/src/swanctl/command.c +++ b/src/swanctl/command.c @@ -124,17 +124,8 @@ int command_getopt(char **arg) switch (op) { case '+': - if (!options->from(options, optarg, &argc, &argv, optind)) - { - /* a error value */ - return 255; - } - continue; case 'v': - dbg_default_set_level(atoi(optarg)); - continue; case 'u': - uri = optarg; continue; default: *arg = optarg; @@ -257,6 +248,37 @@ static void cleanup() } /** + * Process options common for all commands + */ +static bool process_common_opts() +{ + while (TRUE) + { + switch (getopt_long(argc, argv, command_optstring, command_opts, NULL)) + { + case '+': + if (!options->from(options, optarg, &argc, &argv, optind)) + { + return FALSE; + } + continue; + case 'v': + dbg_default_set_level(atoi(optarg)); + continue; + case 'u': + uri = optarg; + continue; + default: + continue; + case '?': + return FALSE; + case EOF: + return TRUE; + } + } +} + +/** * Open vici connection, call a command */ static int call_command(command_t *cmd) @@ -303,6 +325,11 @@ int command_dispatch(int c, char *v[]) { return command_usage(NULL); } + if (!process_common_opts()) + { + return command_usage("invalid options"); + } + optind = 2; return call_command(&cmds[i]); } } diff --git a/src/swanctl/command.h b/src/swanctl/command.h index 2d78a24da..ffc319085 100644 --- a/src/swanctl/command.h +++ b/src/swanctl/command.h @@ -27,7 +27,7 @@ /** * Maximum number of commands (+1). */ -#define MAX_COMMANDS 18 +#define MAX_COMMANDS 19 /** * Maximum number of options in a command (+3) diff --git a/src/swanctl/commands/list_sas.c b/src/swanctl/commands/list_sas.c index 81e1b7cca..1aca6d212 100644 --- a/src/swanctl/commands/list_sas.c +++ b/src/swanctl/commands/list_sas.c @@ -13,6 +13,28 @@ * for more details. */ +/* + * Copyright (C) 2014 Timo Teräs <timo.teras@iki.fi> + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + #define _GNU_SOURCE #include <stdio.h> #include <errno.h> @@ -126,7 +148,7 @@ CALLBACK(child_sas, int, } printf("\n"); - printf(" installed %s ago", child->get(child, "install-time")); + printf(" installed %ss ago", child->get(child, "install-time")); if (child->get(child, "rekey-time")) { printf(", rekeying in %ss", child->get(child, "rekey-time")); @@ -262,9 +284,12 @@ CALLBACK(ike_sas, int, CALLBACK(list_cb, void, command_format_options_t *format, char *name, vici_res_t *res) { + char buf[256]; + if (*format & COMMAND_FORMAT_RAW) { - vici_dump(res, "list-sa event", *format & COMMAND_FORMAT_PRETTY, + snprintf(buf, sizeof(buf), "%s event", name); + vici_dump(res, buf, *format & COMMAND_FORMAT_PRETTY, stdout); } else @@ -348,6 +373,50 @@ static int list_sas(vici_conn_t *conn) return 0; } +static int monitor_sas(vici_conn_t *conn) +{ + command_format_options_t format = COMMAND_FORMAT_NONE; + char *arg; + + while (TRUE) + { + switch (command_getopt(&arg)) + { + case 'h': + return command_usage(NULL); + case 'P': + format |= COMMAND_FORMAT_PRETTY; + /* fall through to raw */ + case 'r': + format |= COMMAND_FORMAT_RAW; + continue; + case EOF: + break; + default: + return command_usage("invalid --monitor-sa option"); + } + break; + } + if (vici_register(conn, "ike-updown", list_cb, &format) != 0) + { + fprintf(stderr, "registering for IKE_SAs failed: %s\n", + strerror(errno)); + return errno; + } + if (vici_register(conn, "child-updown", list_cb, &format) != 0) + { + fprintf(stderr, "registering for CHILD_SAs failed: %s\n", + strerror(errno)); + return errno; + } + + wait_sigint(); + + fprintf(stderr, "disconnecting...\n"); + + return 0; +} + /** * Register the command. */ @@ -366,3 +435,16 @@ static void __attribute__ ((constructor))reg() } }); } + +static void __attribute__ ((constructor))reg_monitor_sa() +{ + command_register((command_t) { + monitor_sas, 'm', "monitor-sa", "monitor for IKE_SA and CHILD_SA changes", + {"[--raw|--pretty]"}, + { + {"help", 'h', 0, "show usage information"}, + {"raw", 'r', 0, "dump raw response message"}, + {"pretty", 'P', 0, "dump raw response message in pretty print"}, + } + }); +} |