155771657d2fec72e8a8dd3ffbd0a8a839f0d5a6
[strongswan.git] / src / swanctl / commands / list_pools.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 #define _GNU_SOURCE
17 #include <stdio.h>
18 #include <errno.h>
19
20 #include "command.h"
21
22 CALLBACK(list_pool, int,
23 linked_list_t *list, vici_res_t *res, char *name)
24 {
25 char pool[64], leases[32];
26
27 snprintf(pool, sizeof(pool), "%s:", name);
28 snprintf(leases, sizeof(leases), "%s / %s / %s",
29 vici_find_str(res, "", "%s.online", name),
30 vici_find_str(res, "", "%s.offline", name),
31 vici_find_str(res, "", "%s.size", name));
32
33 printf("%-20s %-30s %16s\n",
34 name, vici_find_str(res, "", "%s.base", name), leases);
35
36 return 0;
37 }
38
39 static int list_pools(vici_conn_t *conn)
40 {
41 vici_req_t *req;
42 vici_res_t *res;
43 command_format_options_t format = COMMAND_FORMAT_NONE;
44 char *arg;
45 int ret = 0;
46
47 while (TRUE)
48 {
49 switch (command_getopt(&arg))
50 {
51 case 'h':
52 return command_usage(NULL);
53 case 'P':
54 format |= COMMAND_FORMAT_PRETTY;
55 /* fall through to raw */
56 case 'r':
57 format |= COMMAND_FORMAT_RAW;
58 continue;
59 case EOF:
60 break;
61 default:
62 return command_usage("invalid --list-pools option");
63 }
64 break;
65 }
66
67 req = vici_begin("get-pools");
68 res = vici_submit(req, conn);
69 if (!res)
70 {
71 ret = errno;
72 fprintf(stderr, "get-pools request failed: %s\n", strerror(errno));
73 return ret;
74 }
75 if (format & COMMAND_FORMAT_RAW)
76 {
77 vici_dump(res, "get-pools reply", format & COMMAND_FORMAT_PRETTY,
78 stdout);
79 }
80 else
81 {
82 ret = vici_parse_cb(res, list_pool, NULL, NULL, NULL);
83 }
84 vici_free_res(res);
85 return ret;
86 }
87
88 /**
89 * Register the command.
90 */
91 static void __attribute__ ((constructor))reg()
92 {
93 command_register((command_t) {
94 list_pools, 'A', "list-pools", "list loaded pool configurations",
95 {"[--raw|--pretty]"},
96 {
97 {"help", 'h', 0, "show usage information"},
98 {"raw", 'r', 0, "dump raw response message"},
99 {"pretty", 'P', 0, "dump raw response message in pretty print"},
100 }
101 });
102 }