reintegrated bus-refactoring branch
[strongswan.git] / src / charon / 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 * $Id: sql_logger.c 3589 2008-03-13 14:14:44Z martin $
16 */
17
18 #include <string.h>
19
20 #include "sql_logger.h"
21
22 #include <daemon.h>
23
24 typedef struct private_sql_logger_t private_sql_logger_t;
25
26 /**
27 * Private data of an sql_logger_t object
28 */
29 struct private_sql_logger_t {
30
31 /**
32 * Public part
33 */
34 sql_logger_t public;
35
36 /**
37 * database connection
38 */
39 database_t *db;
40
41 /**
42 * logging level
43 */
44 int level;
45
46 /**
47 * avoid recursive logging
48 */
49 bool recursive;
50 };
51
52 /**
53 * Implementation of bus_listener_t.log.
54 */
55 static bool log_(private_sql_logger_t *this, debug_t group, level_t level,
56 int thread, ike_sa_t* ike_sa, char *format, va_list args)
57 {
58 if (this->recursive)
59 {
60 return TRUE;
61 }
62 this->recursive = TRUE;
63
64 if (ike_sa && level <= this->level)
65 {
66 char buffer[8192];
67 chunk_t local_spi, remote_spi;
68 host_t *local_host, *remote_host;
69 identification_t *local_id, *remote_id;
70 u_int64_t ispi, rspi;
71 ike_sa_id_t *id;
72
73 id = ike_sa->get_id(ike_sa);
74 ispi = id->get_initiator_spi(id);
75 rspi = id->get_responder_spi(id);
76 if (id->is_initiator(id))
77 {
78 local_spi.ptr = (char*)&ispi;
79 remote_spi.ptr = (char*)&rspi;
80 }
81 else
82 {
83 local_spi.ptr = (char*)&rspi;
84 remote_spi.ptr = (char*)&ispi;
85 }
86 local_spi.len = remote_spi.len = sizeof(ispi);
87 local_id = ike_sa->get_my_id(ike_sa);
88 remote_id = ike_sa->get_other_id(ike_sa);
89 local_host = ike_sa->get_my_host(ike_sa);
90 remote_host = ike_sa->get_other_host(ike_sa);
91
92 vsnprintf(buffer, sizeof(buffer), format, args);
93
94 this->db->execute(this->db, NULL, "REPLACE INTO ike_sas ("
95 "local_spi, remote_spi, id, initiator, "
96 "local_id_type, local_id_data, "
97 "remote_id_type, remote_id_data, "
98 "host_family, local_host_data, remote_host_data) "
99 "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
100 DB_BLOB, local_spi, DB_BLOB, remote_spi,
101 DB_INT, ike_sa->get_unique_id(ike_sa),
102 DB_INT, id->is_initiator(id),
103 DB_INT, local_id->get_type(local_id),
104 DB_BLOB, local_id->get_encoding(local_id),
105 DB_INT, remote_id->get_type(remote_id),
106 DB_BLOB, remote_id->get_encoding(remote_id),
107 DB_INT, local_host->get_family(local_host),
108 DB_BLOB, local_host->get_address(local_host),
109 DB_BLOB, remote_host->get_address(remote_host));
110 this->db->execute(this->db, NULL, "INSERT INTO logs ("
111 "local_spi, signal, level, msg) VALUES (?, ?, ?, ?)",
112 DB_BLOB, local_spi, DB_INT, group, DB_INT, level,
113 DB_TEXT, buffer);
114 }
115 this->recursive = FALSE;
116 /* always stay registered */
117 return TRUE;
118 }
119
120 /**
121 * Implementation of sql_logger_t.destroy.
122 */
123 static void destroy(private_sql_logger_t *this)
124 {
125 free(this);
126 }
127
128 /**
129 * Described in header.
130 */
131 sql_logger_t *sql_logger_create(database_t *db)
132 {
133 private_sql_logger_t *this = malloc_thing(private_sql_logger_t);
134
135 memset(&this->public.listener, 0, sizeof(listener_t));
136 this->public.listener.log = (bool(*)(listener_t*,debug_t,level_t,int,ike_sa_t*,char*,va_list))log_;
137 this->public.destroy = (void(*)(sql_logger_t*))destroy;
138
139 this->db = db;
140 this->recursive = FALSE;
141
142 this->level = lib->settings->get_int(lib->settings,
143 "charon.plugins.sql.loglevel", -1);
144
145 return &this->public;
146 }
147