swanctl: Add option to query leases with --get-pools
[strongswan.git] / src / swanctl / commands / list_pools.c
1 /*
2 * Copyright (C) 2015 Tobias Brunner
3 * Hochschule fuer Technik Rapperswil
4 *
5 * Copyright (C) 2014 Martin Willi
6 * Copyright (C) 2014 revosec AG
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 #define _GNU_SOURCE
20 #include <stdio.h>
21 #include <errno.h>
22
23 #include "command.h"
24
25 CALLBACK(list_leases, int,
26 char *pool, vici_res_t *res, char *name)
27 {
28 if (streq(name, "leases"))
29 {
30 return vici_parse_cb(res, list_leases, NULL, NULL, pool);
31 }
32 printf(" %-30s %-8s '%s'\n",
33 vici_find_str(res, "", "%s.leases.%s.address", pool, name),
34 vici_find_str(res, "", "%s.leases.%s.status", pool, name),
35 vici_find_str(res, "", "%s.leases.%s.identity", pool, name));
36 return 0;
37 }
38
39 CALLBACK(list_pool, int,
40 void *not_used, vici_res_t *res, char *name)
41 {
42 char pool[64], leases[32];
43
44 snprintf(pool, sizeof(pool), "%s:", name);
45 snprintf(leases, sizeof(leases), "%s / %s / %s",
46 vici_find_str(res, "", "%s.online", name),
47 vici_find_str(res, "", "%s.offline", name),
48 vici_find_str(res, "", "%s.size", name));
49
50 printf("%-20s %-30s %16s\n",
51 name, vici_find_str(res, "", "%s.base", name), leases);
52
53 return vici_parse_cb(res, list_leases, NULL, NULL, name);
54 }
55
56 static int list_pools(vici_conn_t *conn)
57 {
58 vici_req_t *req;
59 vici_res_t *res;
60 command_format_options_t format = COMMAND_FORMAT_NONE;
61 char *arg;
62 int ret = 0;
63 bool leases = FALSE;
64
65 while (TRUE)
66 {
67 switch (command_getopt(&arg))
68 {
69 case 'h':
70 return command_usage(NULL);
71 case 'P':
72 format |= COMMAND_FORMAT_PRETTY;
73 /* fall through to raw */
74 case 'r':
75 format |= COMMAND_FORMAT_RAW;
76 continue;
77 case 'l':
78 leases = TRUE;
79 continue;
80 case EOF:
81 break;
82 default:
83 return command_usage("invalid --list-pools option");
84 }
85 break;
86 }
87
88 req = vici_begin("get-pools");
89 if (leases)
90 {
91 vici_add_key_valuef(req, "leases", "yes");
92 }
93 res = vici_submit(req, conn);
94 if (!res)
95 {
96 ret = errno;
97 fprintf(stderr, "get-pools request failed: %s\n", strerror(errno));
98 return ret;
99 }
100 if (format & COMMAND_FORMAT_RAW)
101 {
102 vici_dump(res, "get-pools reply", format & COMMAND_FORMAT_PRETTY,
103 stdout);
104 }
105 else
106 {
107 ret = vici_parse_cb(res, list_pool, NULL, NULL, NULL);
108 }
109 vici_free_res(res);
110 return ret;
111 }
112
113 /**
114 * Register the command.
115 */
116 static void __attribute__ ((constructor))reg()
117 {
118 command_register((command_t) {
119 list_pools, 'A', "list-pools", "list loaded pool configurations",
120 {"[--leases] [--raw|--pretty]"},
121 {
122 {"help", 'h', 0, "show usage information"},
123 {"raw", 'r', 0, "dump raw response message"},
124 {"pretty", 'P', 0, "dump raw response message in pretty print"},
125 {"leases", 'l', 0, "list leases of each pool"},
126 }
127 });
128 }