Annotation of embedaddon/strongswan/src/libcharon/plugins/stroke/stroke_counter.c, revision 1.1

1.1     ! misho       1: /*
        !             2:  * Copyright (C) 2017 Tobias Brunner
        !             3:  * HSR Hochschule fuer Technik Rapperswil
        !             4:  *
        !             5:  * Copyright (C) 2012 Martin Willi
        !             6:  * Copyright (C) 2012 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: #include <inttypes.h>
        !            20: 
        !            21: #include "stroke_counter.h"
        !            22: 
        !            23: #include <counters_query.h>
        !            24: 
        !            25: ENUM(stroke_counter_type_names,
        !            26:        COUNTER_INIT_IKE_SA_REKEY, COUNTER_OUT_INFORMATIONAL_RSP,
        !            27:        "ikeInitRekey",
        !            28:        "ikeRspRekey",
        !            29:        "ikeChildSaRekey",
        !            30:        "ikeInInvalid",
        !            31:        "ikeInInvalidSpi",
        !            32:        "ikeInInitReq",
        !            33:        "ikeInInitRsp",
        !            34:        "ikeOutInitReq",
        !            35:        "ikeOutInitRsp",
        !            36:        "ikeInAuthReq",
        !            37:        "ikeInAuthRsp",
        !            38:        "ikeOutAuthReq",
        !            39:        "ikeOutAuthRsp",
        !            40:        "ikeInCrChildReq",
        !            41:        "ikeInCrChildRsp",
        !            42:        "ikeOutCrChildReq",
        !            43:        "ikeOutCrChildRsp",
        !            44:        "ikeInInfoReq",
        !            45:        "ikeInInfoRsp",
        !            46:        "ikeOutInfoReq",
        !            47:        "ikeOutInfoRsp",
        !            48: );
        !            49: 
        !            50: typedef struct private_stroke_counter_t private_stroke_counter_t;
        !            51: 
        !            52: /**
        !            53:  * Private data of an stroke_counter_t object.
        !            54:  */
        !            55: struct private_stroke_counter_t {
        !            56: 
        !            57:        /**
        !            58:         * Public stroke_counter_t interface.
        !            59:         */
        !            60:        stroke_counter_t public;
        !            61: 
        !            62:        /**
        !            63:         * Reference to query interface
        !            64:         */
        !            65:        counters_query_t *query;
        !            66: };
        !            67: 
        !            68: /**
        !            69:  * Make sure we have the query interface
        !            70:  */
        !            71: static inline bool ensure_query(private_stroke_counter_t *this)
        !            72: {
        !            73:        if (this->query)
        !            74:        {
        !            75:                return TRUE;
        !            76:        }
        !            77:        return (this->query = lib->get(lib, "counters")) != NULL;
        !            78: }
        !            79: 
        !            80: /**
        !            81:  * Print global or connection-specific IKE counters
        !            82:  */
        !            83: static void print_one(private_stroke_counter_t *this, FILE *out, char *name)
        !            84: {
        !            85:        uint64_t *counters;
        !            86:        counter_type_t i;
        !            87: 
        !            88:        counters = this->query->get_all(this->query, name);
        !            89:        if (!counters)
        !            90:        {
        !            91:                fprintf(out, "No IKE counters found for '%s'\n", name);
        !            92:                return;
        !            93:        }
        !            94:        if (name)
        !            95:        {
        !            96:                fprintf(out, "\nList of IKE counters for '%s':\n\n", name);
        !            97:        }
        !            98:        else
        !            99:        {
        !           100:                fprintf(out, "\nList of IKE counters:\n\n");
        !           101:        }
        !           102:        for (i = 0; i < COUNTER_MAX; i++)
        !           103:        {
        !           104:                fprintf(out, "%-18N %12"PRIu64"\n", stroke_counter_type_names, i,
        !           105:                                counters[i]);
        !           106:        }
        !           107:        free(counters);
        !           108: }
        !           109: 
        !           110: /**
        !           111:  * Print counters for all connections
        !           112:  */
        !           113: static void print_all(private_stroke_counter_t *this, FILE *out)
        !           114: {
        !           115:        enumerator_t *enumerator;
        !           116:        char *name;
        !           117: 
        !           118:        enumerator = this->query->get_names(this->query);
        !           119:        while (enumerator->enumerate(enumerator, &name))
        !           120:        {
        !           121:                print_one(this, out, name);
        !           122:        }
        !           123:        enumerator->destroy(enumerator);
        !           124: }
        !           125: 
        !           126: METHOD(stroke_counter_t, print, void,
        !           127:        private_stroke_counter_t *this, FILE *out, char *name)
        !           128: {
        !           129:        if (!ensure_query(this))
        !           130:        {
        !           131:                fprintf(out, "\nNo counters available (plugin missing?)\n\n");
        !           132:                return;
        !           133:        }
        !           134:        if (name && streq(name, "all"))
        !           135:        {
        !           136:                return print_all(this, out);
        !           137:        }
        !           138:        return print_one(this, out, name);
        !           139: }
        !           140: 
        !           141: METHOD(stroke_counter_t, reset, void,
        !           142:        private_stroke_counter_t *this, char *name)
        !           143: {
        !           144:        if (!ensure_query(this))
        !           145:        {
        !           146:                return;
        !           147:        }
        !           148:        this->query->reset(this->query, name);
        !           149: }
        !           150: 
        !           151: METHOD(stroke_counter_t, destroy, void,
        !           152:        private_stroke_counter_t *this)
        !           153: {
        !           154:        free(this);
        !           155: }
        !           156: 
        !           157: /**
        !           158:  * See header
        !           159:  */
        !           160: stroke_counter_t *stroke_counter_create()
        !           161: {
        !           162:        private_stroke_counter_t *this;
        !           163: 
        !           164:        INIT(this,
        !           165:                .public = {
        !           166:                        .print = _print,
        !           167:                        .reset = _reset,
        !           168:                        .destroy = _destroy,
        !           169:                },
        !           170:        );
        !           171: 
        !           172:        return &this->public;
        !           173: }

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