2 * This file is part of GNUnet
3 * (C) 2009, 2011, 2012 Christian Grothoff (and other contributing authors)
5 * GNUnet is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published
7 * by the Free Software Foundation; either version 3, or (at your
8 * option) any later version.
10 * GNUnet is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with GNUnet; see the file COPYING. If not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
22 * @file namestore/plugin_namestore_sqlite.c
23 * @brief sqlite-based namestore backend
24 * @author Christian Grothoff
28 #include "gnunet_namestore_plugin.h"
32 * After how many ms "busy" should a DB operation fail for good?
33 * A low value makes sure that we are more responsive to requests
34 * (especially PUTs). A high value guarantees a higher success
35 * rate (SELECTs in iterate can take several seconds despite LIMIT=1).
37 * The default value of 1s should ensure that users do not experience
38 * huge latencies while at the same time allowing operations to succeed
39 * with reasonable probability.
41 #define BUSY_TIMEOUT_MS 1000
45 * Log an error message at log-level 'level' that indicates
46 * a failure of the command 'cmd' on file 'filename'
47 * with the message given by strerror(errno).
49 #define LOG_SQLITE(db, level, cmd) do { GNUNET_log_from (level, "namestore-sqlite", _("`%s' failed at %s:%d with error: %s\n"), cmd, __FILE__, __LINE__, sqlite3_errmsg(db->dbh)); } while(0)
51 #define LOG(kind,...) GNUNET_log_from (kind, "namestore-sqlite", __VA_ARGS__)
55 * Context for all functions in this plugin.
60 const struct GNUNET_CONFIGURATION_Handle *cfg;
68 * Native SQLite database handle.
73 * Precompiled SQL for put record
75 sqlite3_stmt *put_record;
78 * Precompiled SQL for put node
80 sqlite3_stmt *put_node;
83 * Precompiled SQL for put signature
85 sqlite3_stmt *put_signature;
88 * Precompiled SQL for iterate records
90 sqlite3_stmt *iterate_records;
93 * Precompiled SQL for get node
95 sqlite3_stmt *get_node;
98 * Precompiled SQL for get signature
100 sqlite3_stmt *get_signature;
103 * Precompiled SQL for delete zone
105 sqlite3_stmt *delete_zone_records;
108 * Precompiled SQL for delete zone
110 sqlite3_stmt *delete_zone_nodes;
113 * Precompiled SQL for delete zone
115 sqlite3_stmt *delete_zone_signatures;
121 * @brief Prepare a SQL statement
123 * @param dbh handle to the database
124 * @param zSql SQL statement, UTF-8 encoded
125 * @param ppStmt set to the prepared statement
126 * @return 0 on success
129 sq_prepare (sqlite3 * dbh, const char *zSql, sqlite3_stmt ** ppStmt)
135 sqlite3_prepare_v2 (dbh, zSql, strlen (zSql), ppStmt,
136 (const char **) &dummy);
137 LOG (GNUNET_ERROR_TYPE_DEBUG,
138 "Prepared `%s' / %p: %d\n", zSql, *ppStmt, result);
144 * Create our database indices.
146 * @param dbh handle to the database
149 create_indices (sqlite3 * dbh)
153 sqlite3_exec (dbh, "CREATE INDEX IF NOT EXISTS ir_zone_name_hash ON ns090records (zone_hash,record_name_hash)",
155 LOG (GNUNET_ERROR_TYPE_ERROR,
156 "Failed to create indices: %s\n", sqlite3_errmsg (dbh));
160 sqlite3_exec (dbh, "CREATE INDEX IF NOT EXISTS in_zone_location ON ns090nodes (zone_hash,zone_revision,node_location_depth,node_location_offset DESC)",
162 LOG (GNUNET_ERROR_TYPE_ERROR,
163 "Failed to create indices: %s\n", sqlite3_errmsg (dbh));
167 sqlite3_exec (dbh, "CREATE INDEX IF NOT EXISTS is_zone ON ns090signatures (zone_hash)",
169 LOG (GNUNET_ERROR_TYPE_ERROR,
170 "Failed to create indices: %s\n", sqlite3_errmsg (dbh));
175 #define CHECK(a) GNUNET_break(a)
179 #define ENULL_DEFINED 1
180 #define CHECK(a) if (! a) { GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "%s\n", e); sqlite3_free(e); }
185 * Initialize the database connections and associated
186 * data structures (create tables and indices
187 * as needed as well).
189 * @param plugin the plugin context (state for this module)
190 * @return GNUNET_OK on success
193 database_setup (struct Plugin *plugin)
202 GNUNET_CONFIGURATION_get_value_filename (plugin->cfg, "namestore-sqlite",
203 "FILENAME", &afsdir))
205 LOG (GNUNET_ERROR_TYPE_ERROR,
206 _ ("Option `%s' in section `%s' missing in configuration!\n"),
207 "FILENAME", "namestore-sqlite");
208 return GNUNET_SYSERR;
210 if (GNUNET_OK != GNUNET_DISK_file_test (afsdir))
212 if (GNUNET_OK != GNUNET_DISK_directory_create_for_file (afsdir))
215 GNUNET_free (afsdir);
216 return GNUNET_SYSERR;
221 GNUNET_STRINGS_to_utf8 (afsdir, strlen (afsdir), nl_langinfo (CODESET));
223 plugin->fn = GNUNET_STRINGS_to_utf8 (afsdir, strlen (afsdir), "UTF-8"); /* good luck */
225 GNUNET_free (afsdir);
227 /* Open database and precompile statements */
228 if (sqlite3_open (plugin->fn, &plugin->dbh) != SQLITE_OK)
230 LOG (GNUNET_ERROR_TYPE_ERROR,
231 _("Unable to initialize SQLite: %s.\n"),
232 sqlite3_errmsg (plugin->dbh));
233 return GNUNET_SYSERR;
236 sqlite3_exec (plugin->dbh, "PRAGMA temp_store=MEMORY", NULL, NULL,
239 sqlite3_exec (plugin->dbh, "PRAGMA synchronous=NORMAL", NULL, NULL,
242 sqlite3_exec (plugin->dbh, "PRAGMA legacy_file_format=OFF", NULL, NULL,
245 sqlite3_exec (plugin->dbh, "PRAGMA auto_vacuum=INCREMENTAL", NULL,
248 sqlite3_exec (plugin->dbh, "PRAGMA encoding=\"UTF-8\"", NULL,
251 sqlite3_exec (plugin->dbh, "PRAGMA locking_mode=EXCLUSIVE", NULL, NULL,
254 sqlite3_exec (plugin->dbh, "PRAGMA count_changes=OFF", NULL, NULL,
257 sqlite3_exec (plugin->dbh, "PRAGMA page_size=4092", NULL, NULL,
260 CHECK (SQLITE_OK == sqlite3_busy_timeout (plugin->dbh, BUSY_TIMEOUT_MS));
265 sq_prepare (plugin->dbh,
266 "SELECT 1 FROM sqlite_master WHERE tbl_name = 'ns090records'",
268 if ((sqlite3_step (stmt) == SQLITE_DONE) &&
271 "CREATE TABLE ns090records ("
272 " zone_hash BLOB NOT NULL DEFAULT '',"
273 " zone_revision INT4 NOT NULL DEFAULT 0,"
274 " record_name_hash BLOB NOT NULL DEFAULT '',"
275 " record_name TEXT NOT NULL DEFAULT '',"
276 " record_type INT4 NOT NULL DEFAULT 0,"
277 " node_location_depth INT4 NOT NULL DEFAULT 0,"
278 " node_location_offset INT8 NOT NULL DEFAULT 0,"
279 " record_expiration_time INT8 NOT NULL DEFAULT 0,"
280 " record_flags INT4 NOT NULL DEFAULT 0,"
281 " record_value BLOB NOT NULL DEFAULT ''"
283 NULL, NULL, NULL) != SQLITE_OK))
285 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR, "sqlite3_exec");
286 sqlite3_finalize (stmt);
287 return GNUNET_SYSERR;
289 sqlite3_finalize (stmt);
292 sq_prepare (plugin->dbh,
293 "SELECT 1 FROM sqlite_master WHERE tbl_name = 'ns090nodes'",
295 if ((sqlite3_step (stmt) == SQLITE_DONE) &&
298 "CREATE TABLE ns090nodes ("
299 " zone_hash BLOB NOT NULL DEFAULT '',"
300 " zone_revision INT4 NOT NULL DEFAULT 0,"
301 " node_location_depth INT4 NOT NULL DEFAULT 0,"
302 " node_location_offset INT8 NOT NULL DEFAULT 0,"
303 " node_parent_offset INT8 NOT NULL DEFAULT 0,"
304 " node_hashcodes BLOB NOT NULL DEFAULT ''"
306 NULL, NULL, NULL) != SQLITE_OK))
308 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR, "sqlite3_exec");
309 sqlite3_finalize (stmt);
310 return GNUNET_SYSERR;
312 sqlite3_finalize (stmt);
316 sq_prepare (plugin->dbh,
317 "SELECT 1 FROM sqlite_master WHERE tbl_name = 'ns090signatures'",
319 if ((sqlite3_step (stmt) == SQLITE_DONE) &&
322 "CREATE TABLE ns090signatures ("
323 " zone_hash BLOB NOT NULL DEFAULT '',"
324 " zone_revision INT4 NOT NULL DEFAULT 0,"
325 " zone_time INT8 NOT NULL DEFAULT 0,"
326 " zone_root_hash BLOB NOT NULL DEFAULT 0,"
327 " zone_root_depth INT4 NOT NULL DEFAULT 0,"
328 " zone_public_key BLOB NOT NULL DEFAULT 0,"
329 " zone_signature BLOB NOT NULL DEFAULT 0"
331 NULL, NULL, NULL) != SQLITE_OK))
333 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR, "sqlite3_exec");
334 sqlite3_finalize (stmt);
335 return GNUNET_SYSERR;
337 sqlite3_finalize (stmt);
340 create_indices (plugin->dbh);
344 "INSERT INTO ns090records (zone_hash, zone_revision, record_name_hash, record_name, "
345 "record_type, node_location_depth, node_location_offset, "
346 "record_expiration_time, record_flags, record_value) VALUES "
347 "(?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
348 &plugin->put_record) != SQLITE_OK) ||
351 "INSERT INTO ns090nodes (zone_hash, zone_revision, "
352 "node_location_depth, node_location_offset, node_parent_offset, node_hashcodes) "
353 "VALUES (?, ?, ?, ?, ?, ?)",
354 &plugin->put_node) != SQLITE_OK) ||
357 "INSERT INTO ns090signatures (zone_hash, zone_revision, zone_time, zone_root_hash, "
358 "zone_root_depth, zone_public_key, zone_signature) "
359 "VALUES (?, ?, ?, ?, ?, ?, ?)",
360 &plugin->put_signature) != SQLITE_OK) ||
363 "SELECT zone_revision,record_name,record_type,node_location_depth,node_location_offset,record_expiration_time,record_flags,record_value "
364 "FROM ns090records WHERE zone_hash=? AND record_name_hash=?",
365 &plugin->iterate_records) != SQLITE_OK) ||
368 "SELECT node_parent_offset,node_location_offset,node_hashcodes FROM ns090nodes "
369 "WHERE zone_hash=? AND zone_revision=? AND node_location_depth=? AND node_location_offset<=? ORDER BY node_location_offset DESC LIMIT 1",
370 &plugin->get_node) != SQLITE_OK) ||
373 "SELECT zone_revision,zone_time,zone_root_hash,zone_root_depth,zone_public_key,zone_signature "
374 "FROM ns090signatures WHERE zone_hash=?",
375 &plugin->get_signature) != SQLITE_OK) ||
378 "DELETE FROM ns090records WHERE zone_hash=?",
379 &plugin->delete_zone_records) != SQLITE_OK) ||
382 "DELETE FROM ns090nodes WHERE zone_hash=?",
383 &plugin->delete_zone_nodes) != SQLITE_OK) ||
386 "DELETE FROM ns090signatures WHERE zone_hash=?",
387 &plugin->delete_zone_signatures) != SQLITE_OK) )
389 LOG_SQLITE (plugin,GNUNET_ERROR_TYPE_ERROR, "precompiling");
390 return GNUNET_SYSERR;
397 * Shutdown database connection and associate data
399 * @param plugin the plugin context (state for this module)
402 database_shutdown (struct Plugin *plugin)
407 if (NULL != plugin->put_record)
408 sqlite3_finalize (plugin->put_record);
409 if (NULL != plugin->put_node)
410 sqlite3_finalize (plugin->put_node);
411 if (NULL != plugin->put_signature)
412 sqlite3_finalize (plugin->put_signature);
413 if (NULL != plugin->iterate_records)
414 sqlite3_finalize (plugin->iterate_records);
415 if (NULL != plugin->get_node)
416 sqlite3_finalize (plugin->get_node);
417 if (NULL != plugin->get_signature)
418 sqlite3_finalize (plugin->get_signature);
419 if (NULL != plugin->delete_zone_records)
420 sqlite3_finalize (plugin->delete_zone_records);
421 if (NULL != plugin->delete_zone_nodes)
422 sqlite3_finalize (plugin->delete_zone_nodes);
423 if (NULL != plugin->delete_zone_signatures)
424 sqlite3_finalize (plugin->delete_zone_signatures);
425 result = sqlite3_close (plugin->dbh);
426 if (result == SQLITE_BUSY)
428 LOG (GNUNET_ERROR_TYPE_WARNING,
429 _("Tried to close sqlite without finalizing all prepared statements.\n"));
430 stmt = sqlite3_next_stmt (plugin->dbh, NULL);
433 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "sqlite",
434 "Closing statement %p\n", stmt);
435 result = sqlite3_finalize (stmt);
436 if (result != SQLITE_OK)
437 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, "sqlite",
438 "Failed to close statement %p: %d\n", stmt, result);
439 stmt = sqlite3_next_stmt (plugin->dbh, NULL);
441 result = sqlite3_close (plugin->dbh);
443 if (SQLITE_OK != result)
444 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR, "sqlite3_close");
446 GNUNET_free_non_null (plugin->fn);
451 * Store a record in the datastore.
453 * @param cls closure (internal context for the plugin)
454 * @param zone hash of the public key of the zone
455 * @param name name that is being mapped (at most 255 characters long)
456 * @param record_type type of the record (A, AAAA, PKEY, etc.)
457 * @param loc location of the signature for the record
458 * @param expiration expiration time for the content
459 * @param flags flags for the content
460 * @param data_size number of bytes in data
461 * @param data value, semantics depend on 'record_type' (see RFCs for DNS and
462 * GNS specification for GNS extensions)
463 * @return GNUNET_OK on success
466 namestore_sqlite_put_record (void *cls,
467 const GNUNET_HashCode *zone,
469 uint32_t record_type,
470 const struct GNUNET_NAMESTORE_SignatureLocation *loc,
471 struct GNUNET_TIME_Absolute expiration,
472 enum GNUNET_NAMESTORE_RecordFlags flags,
476 struct Plugin *plugin = cls;
481 name_len = strlen (name);
482 GNUNET_CRYPTO_hash (name, name_len, &nh);
483 if ((SQLITE_OK != sqlite3_bind_blob (plugin->put_record, 1, zone, sizeof (GNUNET_HashCode), SQLITE_STATIC)) ||
484 (SQLITE_OK != sqlite3_bind_int64 (plugin->put_record, 2, loc->revision)) ||
485 (SQLITE_OK != sqlite3_bind_blob (plugin->put_record, 3, &nh, sizeof (GNUNET_HashCode), SQLITE_STATIC)) ||
486 (SQLITE_OK != sqlite3_bind_text (plugin->put_record, 4, name, -1, SQLITE_STATIC)) ||
487 (SQLITE_OK != sqlite3_bind_int (plugin->put_record, 5, record_type)) ||
488 (SQLITE_OK != sqlite3_bind_int (plugin->put_record, 6, loc->depth)) ||
489 (SQLITE_OK != sqlite3_bind_int64 (plugin->put_record, 7, loc->offset)) ||
490 (SQLITE_OK != sqlite3_bind_int64 (plugin->put_record, 8, expiration.abs_value)) ||
491 (SQLITE_OK != sqlite3_bind_int (plugin->put_record, 9, flags)) ||
492 (SQLITE_OK != sqlite3_bind_blob (plugin->put_record, 10, data, data_size, SQLITE_STATIC)) )
495 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
496 "sqlite3_bind_XXXX");
497 if (SQLITE_OK != sqlite3_reset (plugin->put_record))
499 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
501 return GNUNET_SYSERR;
504 n = sqlite3_step (plugin->put_record);
505 if (SQLITE_OK != sqlite3_reset (plugin->put_record))
506 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
511 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "sqlite", "Record stored\n");
514 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK,
518 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
520 return GNUNET_SYSERR;
526 * Store a Merkle tree node in the datastore.
528 * @param cls closure (internal context for the plugin)
529 * @param zone hash of public key of the zone
530 * @param loc location in the B-tree
531 * @param ploc parent's location in the B-tree (must have depth = loc.depth + 1), NULL for root
532 * @param num_entries number of entries at this node in the B-tree
533 * @param entries the 'num_entries' entries to store (hashes over the
535 * @return GNUNET_OK on success
538 namestore_sqlite_put_node (void *cls,
539 const GNUNET_HashCode *zone,
540 const struct GNUNET_NAMESTORE_SignatureLocation *loc,
541 const struct GNUNET_NAMESTORE_SignatureLocation *ploc,
542 unsigned int num_entries,
543 const GNUNET_HashCode *entries)
545 struct Plugin *plugin = cls;
548 if ( (loc->revision != ploc->revision) ||
549 (loc->depth + 1 != ploc->depth) ||
553 return GNUNET_SYSERR;
555 if ((SQLITE_OK != sqlite3_bind_blob (plugin->put_node, 1, zone, sizeof (GNUNET_HashCode), SQLITE_STATIC)) ||
556 (SQLITE_OK != sqlite3_bind_int (plugin->put_node, 2, loc->revision)) ||
557 (SQLITE_OK != sqlite3_bind_int (plugin->put_node, 3, loc->depth)) ||
558 (SQLITE_OK != sqlite3_bind_int64 (plugin->put_node, 4, loc->offset)) ||
559 (SQLITE_OK != sqlite3_bind_int64 (plugin->put_node, 5, ploc->offset)) ||
560 (SQLITE_OK != sqlite3_bind_blob (plugin->put_node, 6, entries, num_entries * sizeof (GNUNET_HashCode), SQLITE_STATIC)) )
562 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
563 "sqlite3_bind_XXXX");
564 if (SQLITE_OK != sqlite3_reset (plugin->put_node))
566 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
568 return GNUNET_SYSERR;
571 n = sqlite3_step (plugin->put_node);
572 if (SQLITE_OK != sqlite3_reset (plugin->put_node))
573 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
578 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "sqlite", "Node stored\n");
581 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK,
585 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
587 return GNUNET_SYSERR;
593 * Iterate over the results for a particular key and zone in the
594 * datastore. Will only query the latest revision known for the
595 * zone (as adding a new zone revision will cause the plugin to
596 * delete all records from previous revisions).
598 * @param cls closure (internal context for the plugin)
599 * @param zone hash of public key of the zone
600 * @param name_hash hash of name, NULL to iterate over all records of the zone
601 * @param iter maybe NULL (to just count)
602 * @param iter_cls closure for iter
603 * @return the number of results found
606 namestore_sqlite_iterate_records (void *cls,
607 const GNUNET_HashCode *zone,
608 const GNUNET_HashCode *name_hash,
609 GNUNET_NAMESTORE_RecordIterator iter, void *iter_cls)
611 struct Plugin *plugin = cls;
614 struct GNUNET_TIME_Absolute expiration;
615 uint32_t record_type;
616 enum GNUNET_NAMESTORE_RecordFlags flags;
619 struct GNUNET_NAMESTORE_SignatureLocation loc;
622 if ((SQLITE_OK != sqlite3_bind_blob (plugin->iterate_records, 1,
623 zone, sizeof (GNUNET_HashCode),
625 (SQLITE_OK != sqlite3_bind_blob (plugin->iterate_records, 2,
626 name_hash, sizeof (GNUNET_HashCode),
629 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
630 "sqlite3_bind_XXXX");
631 if (SQLITE_OK != sqlite3_reset (plugin->iterate_records))
633 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
635 return GNUNET_SYSERR;
638 while (SQLITE_ROW == (sret = sqlite3_step (plugin->iterate_records)))
642 continue; /* FIXME: just counting can be done more cheaply... */
643 loc.revision = sqlite3_column_int (plugin->iterate_records, 0);
644 name = (const char*) sqlite3_column_text (plugin->iterate_records, 1);
645 record_type = sqlite3_column_int (plugin->iterate_records, 2);
646 loc.depth = sqlite3_column_int (plugin->iterate_records, 3);
647 loc.offset = sqlite3_column_int64 (plugin->iterate_records, 4);
648 expiration.abs_value = (uint64_t) sqlite3_column_int64 (plugin->iterate_records, 5);
649 flags = (enum GNUNET_NAMESTORE_RecordFlags) sqlite3_column_int (plugin->iterate_records, 6);
650 data = sqlite3_column_blob (plugin->iterate_records, 7);
651 data_size = sqlite3_column_bytes (plugin->iterate_records, 7);
652 iter (iter_cls, zone,
653 &loc, name, record_type,
654 expiration, flags, data_size, data);
656 if (SQLITE_DONE != sret)
657 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR, "sqlite_step");
658 sqlite3_finalize (plugin->iterate_records);
664 * Get a particular node from the signature tree.
666 * @param cls closure (internal context for the plugin)
667 * @param zone hash of public key of the zone
668 * @param loc location of the node in the signature tree
669 * @param cb function to call with the result
670 * @param cb_cls closure for cont
671 * @return GNUNET_OK on success, GNUNET_NO if no node was found
674 namestore_sqlite_get_node (void *cls,
675 const GNUNET_HashCode *zone,
676 const struct GNUNET_NAMESTORE_SignatureLocation *loc,
677 GNUNET_NAMESTORE_NodeCallback cb, void *cb_cls)
679 struct Plugin *plugin = cls;
681 size_t hashcodes_size;
682 const GNUNET_HashCode *hashcodes;
683 struct GNUNET_NAMESTORE_SignatureLocation ploc;
684 struct GNUNET_NAMESTORE_SignatureLocation rloc;
686 GNUNET_assert (NULL != cb);
687 if ((SQLITE_OK != sqlite3_bind_blob (plugin->get_node, 1, zone, sizeof (GNUNET_HashCode), SQLITE_STATIC)) ||
688 (SQLITE_OK != sqlite3_bind_int (plugin->get_node, 2, loc->revision)) ||
689 (SQLITE_OK != sqlite3_bind_int (plugin->get_node, 3, loc->depth)) ||
690 (SQLITE_OK != sqlite3_bind_int64 (plugin->get_node, 4, loc->offset)) )
692 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
693 "sqlite3_bind_XXXX");
694 if (SQLITE_OK != sqlite3_reset (plugin->get_node))
696 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
698 return GNUNET_SYSERR;
701 rloc.revision = loc->revision;
702 rloc.depth = loc->depth;
703 ploc.revision = loc->revision;
704 ploc.depth = loc->depth + 1;
706 if (SQLITE_ROW == (ret = sqlite3_step (plugin->get_node)))
708 ploc.offset = sqlite3_column_int64 (plugin->get_node, 0);
709 rloc.offset = sqlite3_column_int64 (plugin->get_node, 1);
710 hashcodes = sqlite3_column_blob (plugin->get_node, 2);
711 hashcodes_size = sqlite3_column_bytes (plugin->get_node, 2);
712 if (0 != (hashcodes_size % sizeof (GNUNET_HashCode)))
715 /* FIXME: delete bogus record? */
724 hashcodes_size / sizeof (GNUNET_HashCode),
728 else if (SQLITE_DONE != ret)
729 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR, "sqlite_step");
730 sqlite3_finalize (plugin->get_node);
736 * Get the current signature for a zone.
738 * @param cls closure (internal context for the plugin)
739 * @param zone hash of public key of the zone
740 * @param cb function to call with the result
741 * @param cb_cls closure for cont
742 * @return GNUNET_OK on success, GNUNET_NO if no node was found
745 namestore_sqlite_get_signature (void *cls,
746 const GNUNET_HashCode *zone,
747 GNUNET_NAMESTORE_SignatureCallback cb, void *cb_cls)
749 struct Plugin *plugin = cls;
751 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key;
752 struct GNUNET_NAMESTORE_SignatureLocation top_loc;
753 const struct GNUNET_CRYPTO_RsaSignature *zone_sig;
754 struct GNUNET_TIME_Absolute zone_time;
755 const GNUNET_HashCode *top_hash;
757 GNUNET_assert (NULL != cb);
758 if (SQLITE_OK != sqlite3_bind_blob (plugin->get_signature, 1, zone, sizeof (GNUNET_HashCode), SQLITE_STATIC))
760 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
761 "sqlite3_bind_XXXX");
762 if (SQLITE_OK != sqlite3_reset (plugin->get_signature))
764 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
766 return GNUNET_SYSERR;
769 if (SQLITE_ROW == (ret = sqlite3_step (plugin->get_signature)))
772 top_loc.revision = sqlite3_column_int (plugin->get_signature, 0);
773 zone_time.abs_value = sqlite3_column_int64 (plugin->get_signature, 1);
774 top_hash = sqlite3_column_blob (plugin->get_signature, 2);
775 top_loc.depth = sqlite3_column_int (plugin->get_signature, 3);
776 zone_key = sqlite3_column_blob (plugin->get_signature, 4);
777 zone_sig = sqlite3_column_blob (plugin->get_signature, 5);
779 if ((sizeof (GNUNET_HashCode) != sqlite3_column_bytes (plugin->get_signature, 2)) ||
780 (sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded) != sqlite3_column_bytes (plugin->get_signature, 4)) ||
781 (sizeof (struct GNUNET_CRYPTO_RsaSignature) != sqlite3_column_bytes (plugin->get_signature, 5)))
784 /* FIXME: delete bogus record & full zone (!?) */
797 if (SQLITE_DONE != ret)
798 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR, "sqlite_step");
799 sqlite3_finalize (plugin->get_signature);
805 * Run a SQL statement that takes only a 'zone' as the argument
806 * and returns nothing (deletes records).
808 * @param plugin our plugin
809 * @param zone zone argument to pass
810 * @param stmt prepared statement to run
813 run_delete_statement (struct Plugin *plugin,
814 const GNUNET_HashCode *zone,
819 if (SQLITE_OK != sqlite3_bind_blob (plugin->delete_zone_records, 1, zone, sizeof (GNUNET_HashCode), SQLITE_STATIC))
821 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
822 "sqlite3_bind_XXXX");
823 if (SQLITE_OK != sqlite3_reset (plugin->delete_zone_records))
825 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
829 n = sqlite3_step (plugin->put_signature);
830 if (SQLITE_OK != sqlite3_reset (plugin->delete_zone_records))
831 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
836 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "sqlite", "Zone records deleted\n");
839 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK,
843 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
851 * Delete an entire zone (all revisions, all records, all nodes,
852 * all signatures). Not used in normal operation.
854 * @param cls closure (internal context for the plugin)
855 * @param zone zone to delete
858 namestore_sqlite_delete_zone (void *cls,
859 const GNUNET_HashCode *zone)
861 struct Plugin *plugin = cls;
863 run_delete_statement (plugin, zone, plugin->delete_zone_records);
864 run_delete_statement (plugin, zone, plugin->delete_zone_nodes);
865 run_delete_statement (plugin, zone, plugin->delete_zone_signatures);
870 * Context for 'delete_old_zone_information'.
877 struct Plugin *plugin;
880 * Hash of the public key of the zone (to avoid having to
883 const GNUNET_HashCode *zone;
886 * Revision to compare against.
894 * Function called on the current signature in the database for
895 * a zone. If the revision given in the closure is more recent,
896 * delete all information about the zone. Otherwise, only delete
899 * @param cls a 'struct DeleteContext' with a revision to compare against
900 * @param zone_key public key of the zone
901 * @param loc location of the root in the B-tree (depth, revision)
902 * @param top_sig signature signing the zone
903 * @param zone_time time the signature was created
904 * @param root_hash top level hash that is being signed
907 delete_old_zone_information (void *cls,
908 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
909 const struct GNUNET_NAMESTORE_SignatureLocation *loc,
910 const struct GNUNET_CRYPTO_RsaSignature *top_sig,
911 struct GNUNET_TIME_Absolute zone_time,
912 const GNUNET_HashCode *root_hash)
914 struct DeleteContext *dc = cls;
916 run_delete_statement (dc->plugin, dc->zone, dc->plugin->delete_zone_signatures);
917 if (loc->revision == dc->revision)
919 run_delete_statement (dc->plugin, dc->zone, dc->plugin->delete_zone_records);
920 run_delete_statement (dc->plugin, dc->zone, dc->plugin->delete_zone_nodes);
925 * Store a zone signature in the datastore. If a signature for the zone with a
926 * lower depth exists, the old signature is removed. If a signature for an
927 * older revision of the zone exists, this will delete all records, nodes
928 * and signatures for the older revision of the zone.
930 * @param cls closure (internal context for the plugin)
931 * @param zone_key public key of the zone
932 * @param loc location in the B-tree (top of the tree, offset 0, depth at 'maximum')
933 * @param top_sig signature at the top, NULL if 'loc.depth > 0'
934 * @param root_hash top level hash that is signed
935 * @param zone_time time the zone was signed
936 * @return GNUNET_OK on success
939 namestore_sqlite_put_signature (void *cls,
940 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
941 const struct GNUNET_NAMESTORE_SignatureLocation *loc,
942 const struct GNUNET_CRYPTO_RsaSignature *top_sig,
943 const GNUNET_HashCode *root_hash,
944 struct GNUNET_TIME_Absolute zone_time)
946 struct Plugin *plugin = cls;
948 GNUNET_HashCode zone;
949 struct DeleteContext dc;
951 GNUNET_CRYPTO_hash (zone_key,
952 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
954 /* get "old" signature, if older revision, delete all existing
955 records/nodes for the zone, if same revision, delete only the signature */
958 dc.revision = loc->revision;
959 (void) namestore_sqlite_get_signature (plugin,
961 &delete_old_zone_information,
963 if ((SQLITE_OK != sqlite3_bind_blob (plugin->put_signature, 1, &zone, sizeof (GNUNET_HashCode), SQLITE_STATIC)) ||
964 (SQLITE_OK != sqlite3_bind_int (plugin->put_signature, 2, loc->revision)) ||
965 (SQLITE_OK != sqlite3_bind_int64 (plugin->put_signature, 3, zone_time.abs_value)) ||
966 (SQLITE_OK != sqlite3_bind_blob (plugin->put_signature, 4, root_hash, sizeof (GNUNET_HashCode), SQLITE_STATIC)) ||
967 (SQLITE_OK != sqlite3_bind_int (plugin->put_signature, 5, loc->depth)) ||
968 (SQLITE_OK != sqlite3_bind_blob (plugin->put_signature, 6, zone_key, sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded), SQLITE_STATIC))||
969 (SQLITE_OK != sqlite3_bind_blob (plugin->put_signature, 7, top_sig, sizeof (struct GNUNET_CRYPTO_RsaSignature), SQLITE_STATIC)) )
971 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
972 "sqlite3_bind_XXXX");
973 if (SQLITE_OK != sqlite3_reset (plugin->put_signature))
975 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
977 return GNUNET_SYSERR;
980 n = sqlite3_step (plugin->put_signature);
981 if (SQLITE_OK != sqlite3_reset (plugin->put_signature))
982 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
987 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "sqlite", "Signature stored\n");
990 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK,
994 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
996 return GNUNET_SYSERR;
1002 * Entry point for the plugin.
1004 * @param cls the "struct GNUNET_NAMESTORE_PluginEnvironment*"
1005 * @return NULL on error, othrewise the plugin context
1008 libgnunet_plugin_namestore_sqlite_init (void *cls)
1010 static struct Plugin plugin;
1011 const struct GNUNET_CONFIGURATION_Handle *cfg = cls;
1012 struct GNUNET_NAMESTORE_PluginFunctions *api;
1014 if (NULL != plugin.cfg)
1015 return NULL; /* can only initialize once! */
1016 memset (&plugin, 0, sizeof (struct Plugin));
1018 if (GNUNET_OK != database_setup (&plugin))
1020 database_shutdown (&plugin);
1023 api = GNUNET_malloc (sizeof (struct GNUNET_NAMESTORE_PluginFunctions));
1025 api->put_record = &namestore_sqlite_put_record;
1026 api->put_node = &namestore_sqlite_put_node;
1027 api->put_signature = &namestore_sqlite_put_signature;
1028 api->iterate_records = &namestore_sqlite_iterate_records;
1029 api->get_node = &namestore_sqlite_get_node;
1030 api->get_signature = &namestore_sqlite_get_signature;
1031 api->delete_zone = &namestore_sqlite_delete_zone;
1032 LOG (GNUNET_ERROR_TYPE_INFO,
1033 _("Sqlite database running\n"));
1039 * Exit point from the plugin.
1041 * @param cls the plugin context (as returned by "init")
1042 * @return always NULL
1045 libgnunet_plugin_namestore_sqlite_done (void *cls)
1047 struct GNUNET_NAMESTORE_PluginFunctions *api = cls;
1048 struct Plugin *plugin = api->cls;
1050 LOG (GNUNET_ERROR_TYPE_DEBUG,
1051 "sqlite plugin is done\n");
1052 database_shutdown (plugin);
1055 LOG (GNUNET_ERROR_TYPE_DEBUG,
1056 "sqlite plugin is finished\n");
1060 /* end of plugin_namestore_sqlite.c */