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