32dd77e315c338d81f21e7b44fb2f4d28f46a208
[strongswan.git] / src / swanctl / commands / version.c
1 /*
2 * Copyright (C) 2014 Martin Willi
3 * Copyright (C) 2014 revosec AG
4 *
5 * Copyright (C) 2015 Andreas Steffen
6 * HSR Hochschule fuer Technik Rapperswil
7 *
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; either version 2 of the License, or (at your
11 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
12 *
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 * for more details.
17 */
18
19 #include "command.h"
20
21 #include <vici_version.h>
22
23 #include <errno.h>
24
25 static int version(vici_conn_t *conn)
26 {
27 vici_req_t *req;
28 vici_res_t *res;
29 char *arg;
30 bool daemon = FALSE;
31 command_format_options_t format = COMMAND_FORMAT_NONE;
32 int ret;
33
34 while (TRUE)
35 {
36 switch (command_getopt(&arg))
37 {
38 case 'h':
39 return command_usage(NULL);
40 case 'P':
41 format |= COMMAND_FORMAT_PRETTY;
42 /* fall through to raw */
43 case 'r':
44 format |= COMMAND_FORMAT_RAW;
45 continue;
46 case 'd':
47 daemon = TRUE;
48 continue;
49 case EOF:
50 break;
51 default:
52 return command_usage("invalid --terminate option");
53 }
54 break;
55 }
56
57 if (!daemon)
58 {
59 printf("strongSwan swanctl %s vici %N\n", VERSION,
60 vici_version_names, VICI_VERSION);
61 return 0;
62 }
63
64 req = vici_begin("version");
65 res = vici_submit(req, conn);
66 if (!res)
67 {
68 ret = errno;
69 fprintf(stderr, "version request failed: %s\n", strerror(errno));
70 return ret;
71 }
72 if (format & COMMAND_FORMAT_RAW)
73 {
74 vici_dump(res, "version reply", format & COMMAND_FORMAT_PRETTY, stdout);
75 }
76 else
77 {
78 printf("strongSwan %s vici %s %s (%s, %s, %s)\n",
79 vici_find_str(res, "" , "version"),
80 vici_find_str(res, "1.0", "vici"),
81 vici_find_str(res, "" , "daemon"),
82 vici_find_str(res, "" , "sysname"),
83 vici_find_str(res, "" , "release"),
84 vici_find_str(res, "" , "machine"));
85 }
86 vici_free_res(res);
87 return 0;
88 }
89
90 /**
91 * Register the command.
92 */
93 static void __attribute__ ((constructor))reg()
94 {
95 command_register((command_t) {
96 version, 'v', "version", "show version information",
97 {"[--raw|--pretty]"},
98 {
99 {"help", 'h', 0, "show usage information"},
100 {"daemon", 'd', 0, "query daemon version"},
101 {"raw", 'r', 0, "dump raw response message"},
102 {"pretty", 'P', 0, "dump raw response message in pretty print"},
103 }
104 });
105 }