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