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