Use a separate interface for loggers.
[strongswan.git] / src / libcharon / plugins / sql / sql_logger.c
1 /*
2 * Copyright (C) 2008 Martin Willi
3 * 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, char *format, va_list args)
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 && level <= this->level)
62 {
63 char buffer[8192];
64 chunk_t local_spi, remote_spi;
65 host_t *local_host, *remote_host;
66 identification_t *local_id, *remote_id;
67 u_int64_t ispi, rspi;
68 ike_sa_id_t *id;
69
70 id = ike_sa->get_id(ike_sa);
71 ispi = id->get_initiator_spi(id);
72 rspi = id->get_responder_spi(id);
73 if (id->is_initiator(id))
74 {
75 local_spi.ptr = (char*)&ispi;
76 remote_spi.ptr = (char*)&rspi;
77 }
78 else
79 {
80 local_spi.ptr = (char*)&rspi;
81 remote_spi.ptr = (char*)&ispi;
82 }
83 local_spi.len = remote_spi.len = sizeof(ispi);
84 local_id = ike_sa->get_my_id(ike_sa);
85 remote_id = ike_sa->get_other_id(ike_sa);
86 local_host = ike_sa->get_my_host(ike_sa);
87 remote_host = ike_sa->get_other_host(ike_sa);
88
89 vsnprintf(buffer, sizeof(buffer), format, args);
90
91 this->db->execute(this->db, NULL, "REPLACE INTO ike_sas ("
92 "local_spi, remote_spi, id, initiator, "
93 "local_id_type, local_id_data, "
94 "remote_id_type, remote_id_data, "
95 "host_family, local_host_data, remote_host_data) "
96 "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
97 DB_BLOB, local_spi, DB_BLOB, remote_spi,
98 DB_INT, ike_sa->get_unique_id(ike_sa),
99 DB_INT, id->is_initiator(id),
100 DB_INT, local_id->get_type(local_id),
101 DB_BLOB, local_id->get_encoding(local_id),
102 DB_INT, remote_id->get_type(remote_id),
103 DB_BLOB, remote_id->get_encoding(remote_id),
104 DB_INT, local_host->get_family(local_host),
105 DB_BLOB, local_host->get_address(local_host),
106 DB_BLOB, remote_host->get_address(remote_host));
107 this->db->execute(this->db, NULL, "INSERT INTO logs ("
108 "local_spi, signal, level, msg) VALUES (?, ?, ?, ?)",
109 DB_BLOB, local_spi, DB_INT, group, DB_INT, level,
110 DB_TEXT, buffer);
111 }
112 this->recursive->set(this->recursive, NULL);
113 }
114
115 METHOD(sql_logger_t, destroy, void,
116 private_sql_logger_t *this)
117 {
118 free(this);
119 }
120
121 /**
122 * Described in header.
123 */
124 sql_logger_t *sql_logger_create(database_t *db)
125 {
126 private_sql_logger_t *this;
127
128 INIT(this,
129 .public = {
130 .logger = {
131 .log = _log_,
132 },
133 .destroy = _destroy,
134 },
135 .db = db,
136 .recursive = thread_value_create(NULL),
137 .level = lib->settings->get_int(lib->settings,
138 "charon.plugins.sql.loglevel", -1),
139 );
140
141 return &this->public;
142 }
143