10ceacb0017ba706619d87e023744144b48d726b
[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
22 typedef struct private_sql_logger_t private_sql_logger_t;
23
24 /**
25 * Private data of an sql_logger_t object
26 */
27 struct private_sql_logger_t {
28
29 /**
30 * Public part
31 */
32 sql_logger_t public;
33
34 /**
35 * database connection
36 */
37 database_t *db;
38
39 /**
40 * logging level
41 */
42 int level;
43
44 /**
45 * avoid recursive logging
46 */
47 bool recursive;
48 };
49
50 METHOD(listener_t, log_, bool,
51 private_sql_logger_t *this, debug_t group, level_t level, int thread,
52 ike_sa_t* ike_sa, char *format, va_list args)
53 {
54 if (this->recursive)
55 {
56 return TRUE;
57 }
58 this->recursive = TRUE;
59
60 if (ike_sa && level <= this->level)
61 {
62 char buffer[8192];
63 chunk_t local_spi, remote_spi;
64 host_t *local_host, *remote_host;
65 identification_t *local_id, *remote_id;
66 u_int64_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 vsnprintf(buffer, sizeof(buffer), format, args);
89
90 this->db->execute(this->db, NULL, "REPLACE INTO ike_sas ("
91 "local_spi, remote_spi, id, initiator, "
92 "local_id_type, local_id_data, "
93 "remote_id_type, remote_id_data, "
94 "host_family, local_host_data, remote_host_data) "
95 "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
96 DB_BLOB, local_spi, DB_BLOB, remote_spi,
97 DB_INT, ike_sa->get_unique_id(ike_sa),
98 DB_INT, id->is_initiator(id),
99 DB_INT, local_id->get_type(local_id),
100 DB_BLOB, local_id->get_encoding(local_id),
101 DB_INT, remote_id->get_type(remote_id),
102 DB_BLOB, remote_id->get_encoding(remote_id),
103 DB_INT, local_host->get_family(local_host),
104 DB_BLOB, local_host->get_address(local_host),
105 DB_BLOB, remote_host->get_address(remote_host));
106 this->db->execute(this->db, NULL, "INSERT INTO logs ("
107 "local_spi, signal, level, msg) VALUES (?, ?, ?, ?)",
108 DB_BLOB, local_spi, DB_INT, group, DB_INT, level,
109 DB_TEXT, buffer);
110 }
111 this->recursive = FALSE;
112 /* always stay registered */
113 return TRUE;
114 }
115
116 METHOD(sql_logger_t, destroy, void,
117 private_sql_logger_t *this)
118 {
119 free(this);
120 }
121
122 /**
123 * Described in header.
124 */
125 sql_logger_t *sql_logger_create(database_t *db)
126 {
127 private_sql_logger_t *this;
128
129 INIT(this,
130 .public = {
131 .listener = {
132 .log = _log_,
133 },
134 .destroy = _destroy,
135 },
136 .db = db,
137 .level = lib->settings->get_int(lib->settings,
138 "charon.plugins.sql.loglevel", -1),
139 );
140
141 return &this->public;
142 }
143