better normalized tables for SQL plugin (IDs)
[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 /**
54 * Implementation of bus_listener_t.signal.
55 */
56 static bool signal_(private_sql_logger_t *this, signal_t signal, level_t level,
57 int thread, ike_sa_t* ike_sa, char *format, va_list args)
58 {
59 if (this->recursive)
60 {
61 return TRUE;
62 }
63 this->recursive = TRUE;
64
65 if (ike_sa && level <= this->level)
66 {
67 char buffer[8192];
68 chunk_t local_spi, remote_spi;
69 host_t *local_host, *remote_host;
70 identification_t *local_id, *remote_id;
71 u_int64_t ispi, rspi;
72 ike_sa_id_t *id;
73
74 id = ike_sa->get_id(ike_sa);
75 ispi = id->get_initiator_spi(id);
76 rspi = id->get_responder_spi(id);
77 if (id->is_initiator(id))
78 {
79 local_spi.ptr = (char*)&ispi;
80 remote_spi.ptr = (char*)&rspi;
81 }
82 else
83 {
84 local_spi.ptr = (char*)&rspi;
85 remote_spi.ptr = (char*)&ispi;
86 }
87 local_spi.len = remote_spi.len = sizeof(ispi);
88 local_id = ike_sa->get_my_id(ike_sa);
89 remote_id = ike_sa->get_other_id(ike_sa);
90 local_host = ike_sa->get_my_host(ike_sa);
91 remote_host = ike_sa->get_other_host(ike_sa);
92
93 vsnprintf(buffer, sizeof(buffer), format, args);
94
95 this->db->execute(this->db, NULL, "REPLACE INTO ike_sas ("
96 "local_spi, remote_spi, id, initiator, "
97 "local_id_type, local_id_data, "
98 "remote_id_type, remote_id_data, "
99 "host_family, local_host_data, remote_host_data) "
100 "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
101 DB_BLOB, local_spi, DB_BLOB, remote_spi,
102 DB_INT, ike_sa->get_unique_id(ike_sa),
103 DB_INT, id->is_initiator(id),
104 DB_INT, local_id->get_type(local_id),
105 DB_BLOB, local_id->get_encoding(local_id),
106 DB_INT, remote_id->get_type(remote_id),
107 DB_BLOB, remote_id->get_encoding(remote_id),
108 DB_INT, local_host->get_family(local_host),
109 DB_BLOB, local_host->get_address(local_host),
110 DB_BLOB, remote_host->get_address(remote_host));
111 this->db->execute(this->db, NULL, "INSERT INTO logs ("
112 "local_spi, signal, level, msg) VALUES (?, ?, ?, ?)",
113 DB_BLOB, local_spi, DB_INT, signal, DB_INT, level,
114 DB_TEXT, buffer);
115 }
116 this->recursive = FALSE;
117 /* always stay registered */
118 return TRUE;
119 }
120
121 /**
122 * Implementation of sql_logger_t.destroy.
123 */
124 static void destroy(private_sql_logger_t *this)
125 {
126 free(this);
127 }
128
129 /**
130 * Described in header.
131 */
132 sql_logger_t *sql_logger_create(database_t *db)
133 {
134 private_sql_logger_t *this = malloc_thing(private_sql_logger_t);
135
136 this->public.listener.signal = (bool(*)(bus_listener_t*,signal_t,level_t,int,ike_sa_t*,char*,va_list))signal_;
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