Annotation of embedaddon/strongswan/src/libcharon/plugins/sql/sql_logger.c, revision 1.1.1.1

1.1       misho       1: /*
                      2:  * Copyright (C) 2008 Martin Willi
                      3:  * HSR Hochschule fuer Technik Rapperswil
                      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 <string.h>
                     17: 
                     18: #include "sql_logger.h"
                     19: 
                     20: #include <daemon.h>
                     21: #include <threading/thread_value.h>
                     22: 
                     23: typedef struct private_sql_logger_t private_sql_logger_t;
                     24: 
                     25: /**
                     26:  * Private data of an sql_logger_t object
                     27:  */
                     28: struct private_sql_logger_t {
                     29: 
                     30:        /**
                     31:         * Public part
                     32:         */
                     33:        sql_logger_t public;
                     34: 
                     35:        /**
                     36:         * database connection
                     37:         */
                     38:        database_t *db;
                     39: 
                     40:        /**
                     41:         * logging level
                     42:         */
                     43:        int level;
                     44: 
                     45:        /**
                     46:         * avoid recursive calls by the same thread
                     47:         */
                     48:        thread_value_t *recursive;
                     49: };
                     50: 
                     51: METHOD(logger_t, log_, void,
                     52:        private_sql_logger_t *this, debug_t group, level_t level, int thread,
                     53:        ike_sa_t* ike_sa, const char *message)
                     54: {
                     55:        if (this->recursive->get(this->recursive))
                     56:        {
                     57:                return;
                     58:        }
                     59:        this->recursive->set(this->recursive, this->recursive);
                     60: 
                     61:        if (ike_sa)
                     62:        {
                     63:                chunk_t local_spi, remote_spi;
                     64:                host_t *local_host, *remote_host;
                     65:                identification_t *local_id, *remote_id;
                     66:                uint64_t ispi, rspi;
                     67:                ike_sa_id_t *id;
                     68: 
                     69:                id = ike_sa->get_id(ike_sa);
                     70:                ispi = id->get_initiator_spi(id);
                     71:                rspi = id->get_responder_spi(id);
                     72:                if (id->is_initiator(id))
                     73:                {
                     74:                        local_spi.ptr = (char*)&ispi;
                     75:                        remote_spi.ptr = (char*)&rspi;
                     76:                }
                     77:                else
                     78:                {
                     79:                        local_spi.ptr = (char*)&rspi;
                     80:                        remote_spi.ptr = (char*)&ispi;
                     81:                }
                     82:                local_spi.len = remote_spi.len = sizeof(ispi);
                     83:                local_id = ike_sa->get_my_id(ike_sa);
                     84:                remote_id = ike_sa->get_other_id(ike_sa);
                     85:                local_host = ike_sa->get_my_host(ike_sa);
                     86:                remote_host = ike_sa->get_other_host(ike_sa);
                     87: 
                     88:                this->db->execute(this->db, NULL, "REPLACE INTO ike_sas ("
                     89:                                                  "local_spi, remote_spi, id, initiator, "
                     90:                                                  "local_id_type, local_id_data, "
                     91:                                                  "remote_id_type, remote_id_data, "
                     92:                                                  "host_family, local_host_data, remote_host_data) "
                     93:                                                  "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
                     94:                                                  DB_BLOB, local_spi, DB_BLOB, remote_spi,
                     95:                                                  DB_INT, ike_sa->get_unique_id(ike_sa),
                     96:                                                  DB_INT, id->is_initiator(id),
                     97:                                                  DB_INT, local_id->get_type(local_id),
                     98:                                                  DB_BLOB, local_id->get_encoding(local_id),
                     99:                                                  DB_INT, remote_id->get_type(remote_id),
                    100:                                                  DB_BLOB, remote_id->get_encoding(remote_id),
                    101:                                                  DB_INT, local_host->get_family(local_host),
                    102:                                                  DB_BLOB, local_host->get_address(local_host),
                    103:                                                  DB_BLOB, remote_host->get_address(remote_host));
                    104:                this->db->execute(this->db, NULL, "INSERT INTO logs ("
                    105:                                                  "local_spi, `signal`, level, msg) "
                    106:                                                  "VALUES (?, ?, ?, ?)",
                    107:                                                  DB_BLOB, local_spi, DB_INT, group, DB_INT, level,
                    108:                                                  DB_TEXT, message);
                    109:        }
                    110: 
                    111:        this->recursive->set(this->recursive, NULL);
                    112: }
                    113: 
                    114: METHOD(logger_t, get_level, level_t,
                    115:        private_sql_logger_t *this, debug_t group)
                    116: {
                    117:        return this->level;
                    118: }
                    119: 
                    120: METHOD(sql_logger_t, destroy, void,
                    121:        private_sql_logger_t *this)
                    122: {
                    123:        this->recursive->destroy(this->recursive);
                    124:        free(this);
                    125: }
                    126: 
                    127: /**
                    128:  * Described in header.
                    129:  */
                    130: sql_logger_t *sql_logger_create(database_t *db)
                    131: {
                    132:        private_sql_logger_t *this;
                    133: 
                    134:        INIT(this,
                    135:                .public = {
                    136:                        .logger = {
                    137:                                .log = _log_,
                    138:                                .get_level = _get_level,
                    139:                        },
                    140:                        .destroy = _destroy,
                    141:                },
                    142:                .db = db,
                    143:                .recursive = thread_value_create(NULL),
                    144:                .level = lib->settings->get_int(lib->settings,
                    145:                                                                                "%s.plugins.sql.loglevel", -1, lib->ns),
                    146:        );
                    147: 
                    148:        return &this->public;
                    149: }
                    150: 

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