Annotation of embedaddon/strongswan/src/swanctl/commands/list_pools.c, revision 1.1

1.1     ! misho       1: /*
        !             2:  * Copyright (C) 2015-2016 Tobias Brunner
        !             3:  * HSR 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, *name = NULL;
        !            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 'n':
        !            81:                                name = arg;
        !            82:                                continue;
        !            83:                        case EOF:
        !            84:                                break;
        !            85:                        default:
        !            86:                                return command_usage("invalid --list-pools option");
        !            87:                }
        !            88:                break;
        !            89:        }
        !            90: 
        !            91:        req = vici_begin("get-pools");
        !            92:        if (leases)
        !            93:        {
        !            94:                vici_add_key_valuef(req, "leases", "yes");
        !            95:        }
        !            96:        if (name)
        !            97:        {
        !            98:                vici_add_key_valuef(req, "name", "%s", name);
        !            99:        }
        !           100:        res = vici_submit(req, conn);
        !           101:        if (!res)
        !           102:        {
        !           103:                ret = errno;
        !           104:                fprintf(stderr, "get-pools request failed: %s\n", strerror(errno));
        !           105:                return ret;
        !           106:        }
        !           107:        if (format & COMMAND_FORMAT_RAW)
        !           108:        {
        !           109:                vici_dump(res, "get-pools reply", format & COMMAND_FORMAT_PRETTY,
        !           110:                                  stdout);
        !           111:        }
        !           112:        else
        !           113:        {
        !           114:                ret = vici_parse_cb(res, list_pool, NULL, NULL, NULL);
        !           115:        }
        !           116:        vici_free_res(res);
        !           117:        return ret;
        !           118: }
        !           119: 
        !           120: /**
        !           121:  * Register the command.
        !           122:  */
        !           123: static void __attribute__ ((constructor))reg()
        !           124: {
        !           125:        command_register((command_t) {
        !           126:                list_pools, 'A', "list-pools", "list loaded pool configurations",
        !           127:                {"[--leases] [--raw|--pretty]"},
        !           128:                {
        !           129:                        {"help",                'h', 0, "show usage information"},
        !           130:                        {"raw",                 'r', 0, "dump raw response message"},
        !           131:                        {"pretty",              'P', 0, "dump raw response message in pretty print"},
        !           132:                        {"leases",              'l', 0, "list leases of each pool"},
        !           133:                        {"name",                'n', 1, "filter pools by name"},
        !           134:                }
        !           135:        });
        !           136: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>