summaryrefslogtreecommitdiff
path: root/src/namecache/plugin_namecache_sqlite.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/namecache/plugin_namecache_sqlite.c')
-rw-r--r--src/namecache/plugin_namecache_sqlite.c656
1 files changed, 329 insertions, 327 deletions
diff --git a/src/namecache/plugin_namecache_sqlite.c b/src/namecache/plugin_namecache_sqlite.c
index f493e015f..e52438b4e 100644
--- a/src/namecache/plugin_namecache_sqlite.c
+++ b/src/namecache/plugin_namecache_sqlite.c
@@ -1,22 +1,22 @@
1 /* 1/*
2 * This file is part of GNUnet 2 * This file is part of GNUnet
3 * Copyright (C) 2009-2013 GNUnet e.V. 3 * Copyright (C) 2009-2013 GNUnet e.V.
4 * 4 *
5 * GNUnet is free software: you can redistribute it and/or modify it 5 * GNUnet is free software: you can redistribute it and/or modify it
6 * under the terms of the GNU Affero General Public License as published 6 * under the terms of the GNU Affero General Public License as published
7 * by the Free Software Foundation, either version 3 of the License, 7 * by the Free Software Foundation, either version 3 of the License,
8 * or (at your option) any later version. 8 * or (at your option) any later version.
9 * 9 *
10 * GNUnet is distributed in the hope that it will be useful, but 10 * GNUnet is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of 11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Affero General Public License for more details. 13 * Affero General Public License for more details.
14 * 14 *
15 * You should have received a copy of the GNU Affero General Public License 15 * You should have received a copy of the GNU Affero General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>. 16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 17
18 SPDX-License-Identifier: AGPL3.0-or-later 18 SPDX-License-Identifier: AGPL3.0-or-later
19 */ 19 */
20 20
21/** 21/**
22 * @file namecache/plugin_namecache_sqlite.c 22 * @file namecache/plugin_namecache_sqlite.c
@@ -49,17 +49,15 @@
49 * a failure of the command 'cmd' on file 'filename' 49 * a failure of the command 'cmd' on file 'filename'
50 * with the message given by strerror(errno). 50 * with the message given by strerror(errno).
51 */ 51 */
52#define LOG_SQLITE(db, level, cmd) do { GNUNET_log_from (level, "namecache-sqlite", _("`%s' failed at %s:%d with error: %s\n"), cmd, __FILE__, __LINE__, sqlite3_errmsg(db->dbh)); } while(0) 52#define LOG_SQLITE(db, level, cmd) do { GNUNET_log_from(level, "namecache-sqlite", _("`%s' failed at %s:%d with error: %s\n"), cmd, __FILE__, __LINE__, sqlite3_errmsg(db->dbh)); } while (0)
53 53
54#define LOG(kind,...) GNUNET_log_from (kind, "namecache-sqlite", __VA_ARGS__) 54#define LOG(kind, ...) GNUNET_log_from(kind, "namecache-sqlite", __VA_ARGS__)
55 55
56 56
57/** 57/**
58 * Context for all functions in this plugin. 58 * Context for all functions in this plugin.
59 */ 59 */
60struct Plugin 60struct Plugin {
61{
62
63 const struct GNUNET_CONFIGURATION_Handle *cfg; 61 const struct GNUNET_CONFIGURATION_Handle *cfg;
64 62
65 /** 63 /**
@@ -91,7 +89,6 @@ struct Plugin
91 * Precompiled SQL for removing expired blocks 89 * Precompiled SQL for removing expired blocks
92 */ 90 */
93 sqlite3_stmt *expire_blocks; 91 sqlite3_stmt *expire_blocks;
94
95}; 92};
96 93
97 94
@@ -104,100 +101,100 @@ struct Plugin
104 * @return #GNUNET_OK on success 101 * @return #GNUNET_OK on success
105 */ 102 */
106static int 103static int
107database_setup (struct Plugin *plugin) 104database_setup(struct Plugin *plugin)
108{ 105{
109 struct GNUNET_SQ_ExecuteStatement es[] = { 106 struct GNUNET_SQ_ExecuteStatement es[] = {
110 GNUNET_SQ_make_try_execute ("PRAGMA temp_store=MEMORY"), 107 GNUNET_SQ_make_try_execute("PRAGMA temp_store=MEMORY"),
111 GNUNET_SQ_make_try_execute ("PRAGMA synchronous=NORMAL"), 108 GNUNET_SQ_make_try_execute("PRAGMA synchronous=NORMAL"),
112 GNUNET_SQ_make_try_execute ("PRAGMA legacy_file_format=OFF"), 109 GNUNET_SQ_make_try_execute("PRAGMA legacy_file_format=OFF"),
113 GNUNET_SQ_make_try_execute ("PRAGMA auto_vacuum=INCREMENTAL"), 110 GNUNET_SQ_make_try_execute("PRAGMA auto_vacuum=INCREMENTAL"),
114 GNUNET_SQ_make_try_execute ("PRAGMA encoding=\"UTF-8\""), 111 GNUNET_SQ_make_try_execute("PRAGMA encoding=\"UTF-8\""),
115 GNUNET_SQ_make_try_execute ("PRAGMA locking_mode=EXCLUSIVE"), 112 GNUNET_SQ_make_try_execute("PRAGMA locking_mode=EXCLUSIVE"),
116 GNUNET_SQ_make_try_execute ("PRAGMA page_size=4092"), 113 GNUNET_SQ_make_try_execute("PRAGMA page_size=4092"),
117 GNUNET_SQ_make_try_execute ("PRAGMA journal_mode=WAL"), 114 GNUNET_SQ_make_try_execute("PRAGMA journal_mode=WAL"),
118 GNUNET_SQ_make_execute ("CREATE TABLE IF NOT EXISTS ns096blocks (" 115 GNUNET_SQ_make_execute("CREATE TABLE IF NOT EXISTS ns096blocks ("
119 " query BLOB NOT NULL," 116 " query BLOB NOT NULL,"
120 " block BLOB NOT NULL," 117 " block BLOB NOT NULL,"
121 " expiration_time INT8 NOT NULL" 118 " expiration_time INT8 NOT NULL"
122 ")"), 119 ")"),
123 GNUNET_SQ_make_execute ("CREATE INDEX IF NOT EXISTS ir_query_hash " 120 GNUNET_SQ_make_execute("CREATE INDEX IF NOT EXISTS ir_query_hash "
124 "ON ns096blocks (query,expiration_time)"), 121 "ON ns096blocks (query,expiration_time)"),
125 GNUNET_SQ_make_execute ("CREATE INDEX IF NOT EXISTS ir_block_expiration " 122 GNUNET_SQ_make_execute("CREATE INDEX IF NOT EXISTS ir_block_expiration "
126 "ON ns096blocks (expiration_time)"), 123 "ON ns096blocks (expiration_time)"),
127 GNUNET_SQ_EXECUTE_STATEMENT_END 124 GNUNET_SQ_EXECUTE_STATEMENT_END
128 }; 125 };
129 struct GNUNET_SQ_PrepareStatement ps[] = { 126 struct GNUNET_SQ_PrepareStatement ps[] = {
130 GNUNET_SQ_make_prepare ("INSERT INTO ns096blocks (query,block,expiration_time) VALUES (?, ?, ?)", 127 GNUNET_SQ_make_prepare("INSERT INTO ns096blocks (query,block,expiration_time) VALUES (?, ?, ?)",
131 &plugin->cache_block), 128 &plugin->cache_block),
132 GNUNET_SQ_make_prepare ("DELETE FROM ns096blocks WHERE expiration_time<?", 129 GNUNET_SQ_make_prepare("DELETE FROM ns096blocks WHERE expiration_time<?",
133 &plugin->expire_blocks), 130 &plugin->expire_blocks),
134 GNUNET_SQ_make_prepare ("DELETE FROM ns096blocks WHERE query=? AND expiration_time<=?", 131 GNUNET_SQ_make_prepare("DELETE FROM ns096blocks WHERE query=? AND expiration_time<=?",
135 &plugin->delete_block), 132 &plugin->delete_block),
136 GNUNET_SQ_make_prepare ("SELECT block FROM ns096blocks WHERE query=? " 133 GNUNET_SQ_make_prepare("SELECT block FROM ns096blocks WHERE query=? "
137 "ORDER BY expiration_time DESC LIMIT 1", 134 "ORDER BY expiration_time DESC LIMIT 1",
138 &plugin->lookup_block), 135 &plugin->lookup_block),
139 GNUNET_SQ_PREPARE_END 136 GNUNET_SQ_PREPARE_END
140 }; 137 };
141 char *afsdir; 138 char *afsdir;
142 139
143 if (GNUNET_OK != 140 if (GNUNET_OK !=
144 GNUNET_CONFIGURATION_get_value_filename (plugin->cfg, 141 GNUNET_CONFIGURATION_get_value_filename(plugin->cfg,
145 "namecache-sqlite", 142 "namecache-sqlite",
146 "FILENAME", 143 "FILENAME",
147 &afsdir)) 144 &afsdir))
148 {
149 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
150 "namecache-sqlite",
151 "FILENAME");
152 return GNUNET_SYSERR;
153 }
154 if (GNUNET_OK !=
155 GNUNET_DISK_file_test (afsdir))
156 {
157 if (GNUNET_OK !=
158 GNUNET_DISK_directory_create_for_file (afsdir))
159 { 145 {
160 GNUNET_break (0); 146 GNUNET_log_config_missing(GNUNET_ERROR_TYPE_ERROR,
161 GNUNET_free (afsdir); 147 "namecache-sqlite",
148 "FILENAME");
162 return GNUNET_SYSERR; 149 return GNUNET_SYSERR;
163 } 150 }
164 } 151 if (GNUNET_OK !=
152 GNUNET_DISK_file_test(afsdir))
153 {
154 if (GNUNET_OK !=
155 GNUNET_DISK_directory_create_for_file(afsdir))
156 {
157 GNUNET_break(0);
158 GNUNET_free(afsdir);
159 return GNUNET_SYSERR;
160 }
161 }
165 /* afsdir should be UTF-8-encoded. If it isn't, it's a bug */ 162 /* afsdir should be UTF-8-encoded. If it isn't, it's a bug */
166 plugin->fn = afsdir; 163 plugin->fn = afsdir;
167 164
168 /* Open database and precompile statements */ 165 /* Open database and precompile statements */
169 if (SQLITE_OK != 166 if (SQLITE_OK !=
170 sqlite3_open (plugin->fn, &plugin->dbh)) 167 sqlite3_open(plugin->fn, &plugin->dbh))
171 { 168 {
172 LOG (GNUNET_ERROR_TYPE_ERROR, 169 LOG(GNUNET_ERROR_TYPE_ERROR,
173 _("Unable to initialize SQLite: %s.\n"), 170 _("Unable to initialize SQLite: %s.\n"),
174 sqlite3_errmsg (plugin->dbh)); 171 sqlite3_errmsg(plugin->dbh));
175 return GNUNET_SYSERR; 172 return GNUNET_SYSERR;
176 } 173 }
177 if (GNUNET_OK != 174 if (GNUNET_OK !=
178 GNUNET_SQ_exec_statements (plugin->dbh, 175 GNUNET_SQ_exec_statements(plugin->dbh,
179 es)) 176 es))
180 { 177 {
181 GNUNET_break (0); 178 GNUNET_break(0);
182 LOG (GNUNET_ERROR_TYPE_ERROR, 179 LOG(GNUNET_ERROR_TYPE_ERROR,
183 _("Failed to setup database at `%s'\n"), 180 _("Failed to setup database at `%s'\n"),
184 plugin->fn); 181 plugin->fn);
185 return GNUNET_SYSERR; 182 return GNUNET_SYSERR;
186 } 183 }
187 GNUNET_break (SQLITE_OK == 184 GNUNET_break(SQLITE_OK ==
188 sqlite3_busy_timeout (plugin->dbh, 185 sqlite3_busy_timeout(plugin->dbh,
189 BUSY_TIMEOUT_MS)); 186 BUSY_TIMEOUT_MS));
190 187
191 if (GNUNET_OK != 188 if (GNUNET_OK !=
192 GNUNET_SQ_prepare (plugin->dbh, 189 GNUNET_SQ_prepare(plugin->dbh,
193 ps)) 190 ps))
194 { 191 {
195 GNUNET_break (0); 192 GNUNET_break(0);
196 LOG (GNUNET_ERROR_TYPE_ERROR, 193 LOG(GNUNET_ERROR_TYPE_ERROR,
197 _("Failed to setup database at `%s'\n"), 194 _("Failed to setup database at `%s'\n"),
198 plugin->fn); 195 plugin->fn);
199 return GNUNET_SYSERR; 196 return GNUNET_SYSERR;
200 } 197 }
201 198
202 return GNUNET_OK; 199 return GNUNET_OK;
203} 200}
@@ -209,50 +206,50 @@ database_setup (struct Plugin *plugin)
209 * @param plugin the plugin context (state for this module) 206 * @param plugin the plugin context (state for this module)
210 */ 207 */
211static void 208static void
212database_shutdown (struct Plugin *plugin) 209database_shutdown(struct Plugin *plugin)
213{ 210{
214 int result; 211 int result;
215 sqlite3_stmt *stmt; 212 sqlite3_stmt *stmt;
216 213
217 if (NULL != plugin->cache_block) 214 if (NULL != plugin->cache_block)
218 sqlite3_finalize (plugin->cache_block); 215 sqlite3_finalize(plugin->cache_block);
219 if (NULL != plugin->lookup_block) 216 if (NULL != plugin->lookup_block)
220 sqlite3_finalize (plugin->lookup_block); 217 sqlite3_finalize(plugin->lookup_block);
221 if (NULL != plugin->expire_blocks) 218 if (NULL != plugin->expire_blocks)
222 sqlite3_finalize (plugin->expire_blocks); 219 sqlite3_finalize(plugin->expire_blocks);
223 if (NULL != plugin->delete_block) 220 if (NULL != plugin->delete_block)
224 sqlite3_finalize (plugin->delete_block); 221 sqlite3_finalize(plugin->delete_block);
225 result = sqlite3_close (plugin->dbh); 222 result = sqlite3_close(plugin->dbh);
226 if (result == SQLITE_BUSY) 223 if (result == SQLITE_BUSY)
227 {
228 LOG (GNUNET_ERROR_TYPE_WARNING,
229 _("Tried to close sqlite without finalizing all prepared statements.\n"));
230 stmt = sqlite3_next_stmt (plugin->dbh,
231 NULL);
232 while (stmt != NULL)
233 { 224 {
234 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, 225 LOG(GNUNET_ERROR_TYPE_WARNING,
235 "sqlite", 226 _("Tried to close sqlite without finalizing all prepared statements.\n"));
236 "Closing statement %p\n", 227 stmt = sqlite3_next_stmt(plugin->dbh,
237 stmt); 228 NULL);
238 result = sqlite3_finalize (stmt); 229 while (stmt != NULL)
239 if (result != SQLITE_OK) 230 {
240 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, 231 GNUNET_log_from(GNUNET_ERROR_TYPE_DEBUG,
241 "sqlite", 232 "sqlite",
242 "Failed to close statement %p: %d\n", 233 "Closing statement %p\n",
243 stmt, 234 stmt);
244 result); 235 result = sqlite3_finalize(stmt);
245 stmt = sqlite3_next_stmt (plugin->dbh, 236 if (result != SQLITE_OK)
246 NULL); 237 GNUNET_log_from(GNUNET_ERROR_TYPE_WARNING,
238 "sqlite",
239 "Failed to close statement %p: %d\n",
240 stmt,
241 result);
242 stmt = sqlite3_next_stmt(plugin->dbh,
243 NULL);
244 }
245 result = sqlite3_close(plugin->dbh);
247 } 246 }
248 result = sqlite3_close (plugin->dbh);
249 }
250 if (SQLITE_OK != result) 247 if (SQLITE_OK != result)
251 LOG_SQLITE (plugin, 248 LOG_SQLITE(plugin,
252 GNUNET_ERROR_TYPE_ERROR, 249 GNUNET_ERROR_TYPE_ERROR,
253 "sqlite3_close"); 250 "sqlite3_close");
254 251
255 GNUNET_free_non_null (plugin->fn); 252 GNUNET_free_non_null(plugin->fn);
256} 253}
257 254
258 255
@@ -262,48 +259,50 @@ database_shutdown (struct Plugin *plugin)
262 * @param plugin the plugin 259 * @param plugin the plugin
263 */ 260 */
264static void 261static void
265namecache_sqlite_expire_blocks (struct Plugin *plugin) 262namecache_sqlite_expire_blocks(struct Plugin *plugin)
266{ 263{
267 struct GNUNET_TIME_Absolute now; 264 struct GNUNET_TIME_Absolute now;
268 struct GNUNET_SQ_QueryParam params[] = { 265 struct GNUNET_SQ_QueryParam params[] = {
269 GNUNET_SQ_query_param_absolute_time (&now), 266 GNUNET_SQ_query_param_absolute_time(&now),
270 GNUNET_SQ_query_param_end 267 GNUNET_SQ_query_param_end
271 }; 268 };
272 int n; 269 int n;
273 270
274 now = GNUNET_TIME_absolute_get (); 271 now = GNUNET_TIME_absolute_get();
275 if (GNUNET_OK != 272 if (GNUNET_OK !=
276 GNUNET_SQ_bind (plugin->expire_blocks, 273 GNUNET_SQ_bind(plugin->expire_blocks,
277 params)) 274 params))
278 { 275 {
279 LOG_SQLITE (plugin, 276 LOG_SQLITE(plugin,
280 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, 277 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
281 "sqlite3_bind_XXXX"); 278 "sqlite3_bind_XXXX");
282 GNUNET_SQ_reset (plugin->dbh, 279 GNUNET_SQ_reset(plugin->dbh,
283 plugin->expire_blocks); 280 plugin->expire_blocks);
284 return; 281 return;
285 } 282 }
286 n = sqlite3_step (plugin->expire_blocks); 283 n = sqlite3_step(plugin->expire_blocks);
287 GNUNET_SQ_reset (plugin->dbh, 284 GNUNET_SQ_reset(plugin->dbh,
288 plugin->expire_blocks); 285 plugin->expire_blocks);
289 switch (n) 286 switch (n)
290 { 287 {
291 case SQLITE_DONE: 288 case SQLITE_DONE:
292 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, 289 GNUNET_log_from(GNUNET_ERROR_TYPE_DEBUG,
293 "sqlite", 290 "sqlite",
294 "Records expired\n"); 291 "Records expired\n");
295 return; 292 return;
296 case SQLITE_BUSY: 293
297 LOG_SQLITE (plugin, 294 case SQLITE_BUSY:
298 GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK, 295 LOG_SQLITE(plugin,
299 "sqlite3_step"); 296 GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK,
300 return; 297 "sqlite3_step");
301 default: 298 return;
302 LOG_SQLITE (plugin, 299
303 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, 300 default:
304 "sqlite3_step"); 301 LOG_SQLITE(plugin,
305 return; 302 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
306 } 303 "sqlite3_step");
304 return;
305 }
307} 306}
308 307
309 308
@@ -315,120 +314,123 @@ namecache_sqlite_expire_blocks (struct Plugin *plugin)
315 * @return #GNUNET_OK on success, else #GNUNET_SYSERR 314 * @return #GNUNET_OK on success, else #GNUNET_SYSERR
316 */ 315 */
317static int 316static int
318namecache_sqlite_cache_block (void *cls, 317namecache_sqlite_cache_block(void *cls,
319 const struct GNUNET_GNSRECORD_Block *block) 318 const struct GNUNET_GNSRECORD_Block *block)
320{ 319{
321 static struct GNUNET_TIME_Absolute last_expire; 320 static struct GNUNET_TIME_Absolute last_expire;
322 struct Plugin *plugin = cls; 321 struct Plugin *plugin = cls;
323 struct GNUNET_HashCode query; 322 struct GNUNET_HashCode query;
324 struct GNUNET_TIME_Absolute expiration; 323 struct GNUNET_TIME_Absolute expiration;
325 size_t block_size = ntohl (block->purpose.size) + 324 size_t block_size = ntohl(block->purpose.size) +
326 sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey) + 325 sizeof(struct GNUNET_CRYPTO_EcdsaPublicKey) +
327 sizeof (struct GNUNET_CRYPTO_EcdsaSignature); 326 sizeof(struct GNUNET_CRYPTO_EcdsaSignature);
328 struct GNUNET_SQ_QueryParam del_params[] = { 327 struct GNUNET_SQ_QueryParam del_params[] = {
329 GNUNET_SQ_query_param_auto_from_type (&query), 328 GNUNET_SQ_query_param_auto_from_type(&query),
330 GNUNET_SQ_query_param_absolute_time (&expiration), 329 GNUNET_SQ_query_param_absolute_time(&expiration),
331 GNUNET_SQ_query_param_end 330 GNUNET_SQ_query_param_end
332 }; 331 };
333 struct GNUNET_SQ_QueryParam ins_params[] = { 332 struct GNUNET_SQ_QueryParam ins_params[] = {
334 GNUNET_SQ_query_param_auto_from_type (&query), 333 GNUNET_SQ_query_param_auto_from_type(&query),
335 GNUNET_SQ_query_param_fixed_size (block, 334 GNUNET_SQ_query_param_fixed_size(block,
336 block_size), 335 block_size),
337 GNUNET_SQ_query_param_absolute_time (&expiration), 336 GNUNET_SQ_query_param_absolute_time(&expiration),
338 GNUNET_SQ_query_param_end 337 GNUNET_SQ_query_param_end
339 }; 338 };
340 int n; 339 int n;
341 340
342 /* run expiration of old cache entries once per hour */ 341 /* run expiration of old cache entries once per hour */
343 if (GNUNET_TIME_absolute_get_duration (last_expire).rel_value_us > 342 if (GNUNET_TIME_absolute_get_duration(last_expire).rel_value_us >
344 GNUNET_TIME_UNIT_HOURS.rel_value_us) 343 GNUNET_TIME_UNIT_HOURS.rel_value_us)
345 { 344 {
346 last_expire = GNUNET_TIME_absolute_get (); 345 last_expire = GNUNET_TIME_absolute_get();
347 namecache_sqlite_expire_blocks (plugin); 346 namecache_sqlite_expire_blocks(plugin);
348 } 347 }
349 GNUNET_CRYPTO_hash (&block->derived_key, 348 GNUNET_CRYPTO_hash(&block->derived_key,
350 sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey), 349 sizeof(struct GNUNET_CRYPTO_EcdsaPublicKey),
351 &query); 350 &query);
352 expiration = GNUNET_TIME_absolute_ntoh (block->expiration_time); 351 expiration = GNUNET_TIME_absolute_ntoh(block->expiration_time);
353 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 352 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
354 "Caching new version of block %s (expires %s)\n", 353 "Caching new version of block %s (expires %s)\n",
355 GNUNET_h2s (&query), 354 GNUNET_h2s(&query),
356 GNUNET_STRINGS_absolute_time_to_string (expiration)); 355 GNUNET_STRINGS_absolute_time_to_string(expiration));
357 if (block_size > 64 * 65536) 356 if (block_size > 64 * 65536)
358 { 357 {
359 GNUNET_break (0); 358 GNUNET_break(0);
360 return GNUNET_SYSERR; 359 return GNUNET_SYSERR;
361 } 360 }
362 361
363 /* delete old version of the block */ 362 /* delete old version of the block */
364 if (GNUNET_OK != 363 if (GNUNET_OK !=
365 GNUNET_SQ_bind (plugin->delete_block, 364 GNUNET_SQ_bind(plugin->delete_block,
366 del_params)) 365 del_params))
367 { 366 {
368 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, 367 LOG_SQLITE(plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
369 "sqlite3_bind_XXXX"); 368 "sqlite3_bind_XXXX");
370 GNUNET_SQ_reset (plugin->dbh, 369 GNUNET_SQ_reset(plugin->dbh,
371 plugin->delete_block); 370 plugin->delete_block);
372 return GNUNET_SYSERR; 371 return GNUNET_SYSERR;
373 } 372 }
374 n = sqlite3_step (plugin->delete_block); 373 n = sqlite3_step(plugin->delete_block);
375 switch (n) 374 switch (n)
376 { 375 {
377 case SQLITE_DONE: 376 case SQLITE_DONE:
378 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, 377 GNUNET_log_from(GNUNET_ERROR_TYPE_DEBUG,
379 "sqlite", 378 "sqlite",
380 "Old block deleted\n"); 379 "Old block deleted\n");
381 break; 380 break;
382 case SQLITE_BUSY: 381
383 LOG_SQLITE (plugin, 382 case SQLITE_BUSY:
384 GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK, 383 LOG_SQLITE(plugin,
385 "sqlite3_step"); 384 GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK,
386 break; 385 "sqlite3_step");
387 default: 386 break;
388 LOG_SQLITE (plugin, 387
389 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, 388 default:
390 "sqlite3_step"); 389 LOG_SQLITE(plugin,
391 break; 390 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
392 } 391 "sqlite3_step");
393 GNUNET_SQ_reset (plugin->dbh, 392 break;
394 plugin->delete_block); 393 }
394 GNUNET_SQ_reset(plugin->dbh,
395 plugin->delete_block);
395 396
396 /* insert new version of the block */ 397 /* insert new version of the block */
397 if (GNUNET_OK != 398 if (GNUNET_OK !=
398 GNUNET_SQ_bind (plugin->cache_block, 399 GNUNET_SQ_bind(plugin->cache_block,
399 ins_params)) 400 ins_params))
400 { 401 {
401 LOG_SQLITE (plugin, 402 LOG_SQLITE(plugin,
402 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, 403 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
403 "sqlite3_bind_XXXX"); 404 "sqlite3_bind_XXXX");
404 GNUNET_SQ_reset (plugin->dbh, 405 GNUNET_SQ_reset(plugin->dbh,
405 plugin->cache_block); 406 plugin->cache_block);
406 return GNUNET_SYSERR; 407 return GNUNET_SYSERR;
407 408 }
408 } 409 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
409 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 410 "Caching block under derived key `%s'\n",
410 "Caching block under derived key `%s'\n", 411 GNUNET_h2s_full(&query));
411 GNUNET_h2s_full (&query)); 412 n = sqlite3_step(plugin->cache_block);
412 n = sqlite3_step (plugin->cache_block); 413 GNUNET_SQ_reset(plugin->dbh,
413 GNUNET_SQ_reset (plugin->dbh, 414 plugin->cache_block);
414 plugin->cache_block);
415 switch (n) 415 switch (n)
416 { 416 {
417 case SQLITE_DONE: 417 case SQLITE_DONE:
418 LOG (GNUNET_ERROR_TYPE_DEBUG, 418 LOG(GNUNET_ERROR_TYPE_DEBUG,
419 "Record stored\n"); 419 "Record stored\n");
420 return GNUNET_OK; 420 return GNUNET_OK;
421 case SQLITE_BUSY: 421
422 LOG_SQLITE (plugin, 422 case SQLITE_BUSY:
423 GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK, 423 LOG_SQLITE(plugin,
424 "sqlite3_step"); 424 GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK,
425 return GNUNET_NO; 425 "sqlite3_step");
426 default: 426 return GNUNET_NO;
427 LOG_SQLITE (plugin, 427
428 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, 428 default:
429 "sqlite3_step"); 429 LOG_SQLITE(plugin,
430 return GNUNET_SYSERR; 430 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
431 } 431 "sqlite3_step");
432 return GNUNET_SYSERR;
433 }
432} 434}
433 435
434 436
@@ -443,10 +445,10 @@ namecache_sqlite_cache_block (void *cls,
443 * @return #GNUNET_OK on success, #GNUNET_NO if there were no results, #GNUNET_SYSERR on error 445 * @return #GNUNET_OK on success, #GNUNET_NO if there were no results, #GNUNET_SYSERR on error
444 */ 446 */
445static int 447static int
446namecache_sqlite_lookup_block (void *cls, 448namecache_sqlite_lookup_block(void *cls,
447 const struct GNUNET_HashCode *query, 449 const struct GNUNET_HashCode *query,
448 GNUNET_NAMECACHE_BlockCallback iter, 450 GNUNET_NAMECACHE_BlockCallback iter,
449 void *iter_cls) 451 void *iter_cls)
450{ 452{
451 struct Plugin *plugin = cls; 453 struct Plugin *plugin = cls;
452 int ret; 454 int ret;
@@ -454,75 +456,75 @@ namecache_sqlite_lookup_block (void *cls,
454 size_t block_size; 456 size_t block_size;
455 const struct GNUNET_GNSRECORD_Block *block; 457 const struct GNUNET_GNSRECORD_Block *block;
456 struct GNUNET_SQ_QueryParam params[] = { 458 struct GNUNET_SQ_QueryParam params[] = {
457 GNUNET_SQ_query_param_auto_from_type (query), 459 GNUNET_SQ_query_param_auto_from_type(query),
458 GNUNET_SQ_query_param_end 460 GNUNET_SQ_query_param_end
459 }; 461 };
460 struct GNUNET_SQ_ResultSpec rs[] = { 462 struct GNUNET_SQ_ResultSpec rs[] = {
461 GNUNET_SQ_result_spec_variable_size ((void **) &block, 463 GNUNET_SQ_result_spec_variable_size((void **)&block,
462 &block_size), 464 &block_size),
463 GNUNET_SQ_result_spec_end 465 GNUNET_SQ_result_spec_end
464 }; 466 };
465 467
466 if (GNUNET_OK != 468 if (GNUNET_OK !=
467 GNUNET_SQ_bind (plugin->lookup_block, 469 GNUNET_SQ_bind(plugin->lookup_block,
468 params)) 470 params))
469 {
470 LOG_SQLITE (plugin,
471 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
472 "sqlite3_bind_XXXX");
473 GNUNET_SQ_reset (plugin->dbh,
474 plugin->lookup_block);
475 return GNUNET_SYSERR;
476 }
477 ret = GNUNET_NO;
478 if (SQLITE_ROW ==
479 (sret = sqlite3_step (plugin->lookup_block)))
480 {
481 if (GNUNET_OK !=
482 GNUNET_SQ_extract_result (plugin->lookup_block,
483 rs))
484 { 471 {
485 GNUNET_break (0); 472 LOG_SQLITE(plugin,
486 ret = GNUNET_SYSERR; 473 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
487 } 474 "sqlite3_bind_XXXX");
488 else if ( (block_size < sizeof (struct GNUNET_GNSRECORD_Block)) || 475 GNUNET_SQ_reset(plugin->dbh,
489 (ntohl (block->purpose.size) + 476 plugin->lookup_block);
490 sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey) + 477 return GNUNET_SYSERR;
491 sizeof (struct GNUNET_CRYPTO_EcdsaSignature) != block_size) )
492 {
493 GNUNET_break (0);
494 GNUNET_SQ_cleanup_result (rs);
495 ret = GNUNET_SYSERR;
496 } 478 }
497 else 479 ret = GNUNET_NO;
480 if (SQLITE_ROW ==
481 (sret = sqlite3_step(plugin->lookup_block)))
498 { 482 {
499 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 483 if (GNUNET_OK !=
500 "Found block under derived key `%s'\n", 484 GNUNET_SQ_extract_result(plugin->lookup_block,
501 GNUNET_h2s_full (query)); 485 rs))
502 iter (iter_cls, 486 {
503 block); 487 GNUNET_break(0);
504 GNUNET_SQ_cleanup_result (rs); 488 ret = GNUNET_SYSERR;
505 ret = GNUNET_YES; 489 }
490 else if ((block_size < sizeof(struct GNUNET_GNSRECORD_Block)) ||
491 (ntohl(block->purpose.size) +
492 sizeof(struct GNUNET_CRYPTO_EcdsaPublicKey) +
493 sizeof(struct GNUNET_CRYPTO_EcdsaSignature) != block_size))
494 {
495 GNUNET_break(0);
496 GNUNET_SQ_cleanup_result(rs);
497 ret = GNUNET_SYSERR;
498 }
499 else
500 {
501 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
502 "Found block under derived key `%s'\n",
503 GNUNET_h2s_full(query));
504 iter(iter_cls,
505 block);
506 GNUNET_SQ_cleanup_result(rs);
507 ret = GNUNET_YES;
508 }
506 } 509 }
507 }
508 else 510 else
509 {
510 if (SQLITE_DONE != sret)
511 {
512 LOG_SQLITE (plugin,
513 GNUNET_ERROR_TYPE_ERROR,
514 "sqlite_step");
515 ret = GNUNET_SYSERR;
516 }
517 else
518 { 511 {
519 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 512 if (SQLITE_DONE != sret)
520 "No block found under derived key `%s'\n", 513 {
521 GNUNET_h2s_full (query)); 514 LOG_SQLITE(plugin,
515 GNUNET_ERROR_TYPE_ERROR,
516 "sqlite_step");
517 ret = GNUNET_SYSERR;
518 }
519 else
520 {
521 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
522 "No block found under derived key `%s'\n",
523 GNUNET_h2s_full(query));
524 }
522 } 525 }
523 } 526 GNUNET_SQ_reset(plugin->dbh,
524 GNUNET_SQ_reset (plugin->dbh, 527 plugin->lookup_block);
525 plugin->lookup_block);
526 return ret; 528 return ret;
527} 529}
528 530
@@ -534,7 +536,7 @@ namecache_sqlite_lookup_block (void *cls,
534 * @return NULL on error, otherwise the plugin context 536 * @return NULL on error, otherwise the plugin context
535 */ 537 */
536void * 538void *
537libgnunet_plugin_namecache_sqlite_init (void *cls) 539libgnunet_plugin_namecache_sqlite_init(void *cls)
538{ 540{
539 static struct Plugin plugin; 541 static struct Plugin plugin;
540 const struct GNUNET_CONFIGURATION_Handle *cfg = cls; 542 const struct GNUNET_CONFIGURATION_Handle *cfg = cls;
@@ -542,19 +544,19 @@ libgnunet_plugin_namecache_sqlite_init (void *cls)
542 544
543 if (NULL != plugin.cfg) 545 if (NULL != plugin.cfg)
544 return NULL; /* can only initialize once! */ 546 return NULL; /* can only initialize once! */
545 memset (&plugin, 0, sizeof (struct Plugin)); 547 memset(&plugin, 0, sizeof(struct Plugin));
546 plugin.cfg = cfg; 548 plugin.cfg = cfg;
547 if (GNUNET_OK != database_setup (&plugin)) 549 if (GNUNET_OK != database_setup(&plugin))
548 { 550 {
549 database_shutdown (&plugin); 551 database_shutdown(&plugin);
550 return NULL; 552 return NULL;
551 } 553 }
552 api = GNUNET_new (struct GNUNET_NAMECACHE_PluginFunctions); 554 api = GNUNET_new(struct GNUNET_NAMECACHE_PluginFunctions);
553 api->cls = &plugin; 555 api->cls = &plugin;
554 api->cache_block = &namecache_sqlite_cache_block; 556 api->cache_block = &namecache_sqlite_cache_block;
555 api->lookup_block = &namecache_sqlite_lookup_block; 557 api->lookup_block = &namecache_sqlite_lookup_block;
556 LOG (GNUNET_ERROR_TYPE_INFO, 558 LOG(GNUNET_ERROR_TYPE_INFO,
557 _("Sqlite database running\n")); 559 _("Sqlite database running\n"));
558 return api; 560 return api;
559} 561}
560 562
@@ -566,16 +568,16 @@ libgnunet_plugin_namecache_sqlite_init (void *cls)
566 * @return always NULL 568 * @return always NULL
567 */ 569 */
568void * 570void *
569libgnunet_plugin_namecache_sqlite_done (void *cls) 571libgnunet_plugin_namecache_sqlite_done(void *cls)
570{ 572{
571 struct GNUNET_NAMECACHE_PluginFunctions *api = cls; 573 struct GNUNET_NAMECACHE_PluginFunctions *api = cls;
572 struct Plugin *plugin = api->cls; 574 struct Plugin *plugin = api->cls;
573 575
574 database_shutdown (plugin); 576 database_shutdown(plugin);
575 plugin->cfg = NULL; 577 plugin->cfg = NULL;
576 GNUNET_free (api); 578 GNUNET_free(api);
577 LOG (GNUNET_ERROR_TYPE_DEBUG, 579 LOG(GNUNET_ERROR_TYPE_DEBUG,
578 "sqlite plugin is finished\n"); 580 "sqlite plugin is finished\n");
579 return NULL; 581 return NULL;
580} 582}
581 583