2 * This file is part of GNUnet
3 * Copyright (C) 2009-2017 GNUnet e.V.
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., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
22 * @file namestore/plugin_namestore_sqlite.c
23 * @brief sqlite-based namestore backend
24 * @author Christian Grothoff
28 #include "gnunet_namestore_plugin.h"
29 #include "gnunet_namestore_service.h"
30 #include "gnunet_gnsrecord_lib.h"
31 #include "gnunet_sq_lib.h"
32 #include "namestore.h"
36 * After how many ms "busy" should a DB operation fail for good? A
37 * low value makes sure that we are more responsive to requests
38 * (especially PUTs). A high value guarantees a higher success rate
39 * (SELECTs in iterate can take several seconds despite LIMIT=1).
41 * The default value of 1s should ensure that users do not experience
42 * huge latencies while at the same time allowing operations to
43 * succeed with reasonable probability.
45 #define BUSY_TIMEOUT_MS 1000
49 * Log an error message at log-level 'level' that indicates
50 * a failure of the command 'cmd' on file 'filename'
51 * with the message given by strerror(errno).
53 #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)
55 #define LOG(kind,...) GNUNET_log_from (kind, "namestore-sqlite", __VA_ARGS__)
59 * Context for all functions in this plugin.
64 const struct GNUNET_CONFIGURATION_Handle *cfg;
72 * Native SQLite database handle.
77 * Precompiled SQL to store records.
79 sqlite3_stmt *store_records;
82 * Precompiled SQL to deltete existing records.
84 sqlite3_stmt *delete_records;
87 * Precompiled SQL for iterate records within a zone.
89 sqlite3_stmt *iterate_zone;
92 * Precompiled SQL for iterate all records within all zones.
94 sqlite3_stmt *iterate_all_zones;
97 * Precompiled SQL to for reverse lookup based on PKEY.
99 sqlite3_stmt *zone_to_name;
102 * Precompiled SQL to lookup records based on label.
104 sqlite3_stmt *lookup_label;
109 * Initialize the database connections and associated
110 * data structures (create tables and indices
111 * as needed as well).
113 * @param plugin the plugin context (state for this module)
114 * @return #GNUNET_OK on success
117 database_setup (struct Plugin *plugin)
119 char *sqlite_filename;
120 struct GNUNET_SQ_ExecuteStatement es[] = {
121 GNUNET_SQ_make_try_execute ("PRAGMA temp_store=MEMORY"),
122 GNUNET_SQ_make_try_execute ("PRAGMA synchronous=NORMAL"),
123 GNUNET_SQ_make_try_execute ("PRAGMA legacy_file_format=OFF"),
124 GNUNET_SQ_make_try_execute ("PRAGMA auto_vacuum=INCREMENTAL"),
125 GNUNET_SQ_make_try_execute ("PRAGMA encoding=\"UTF-8\""),
126 GNUNET_SQ_make_try_execute ("PRAGMA locking_mode=EXCLUSIVE"),
127 GNUNET_SQ_make_try_execute ("PRAGMA page_size=4092"),
128 GNUNET_SQ_make_execute ("CREATE TABLE IF NOT EXISTS ns098records ("
129 " uid INTEGER PRIMARY KEY,"
130 " zone_private_key BLOB NOT NULL,"
132 " rvalue INT8 NOT NULL,"
133 " record_count INT NOT NULL,"
134 " record_data BLOB NOT NULL,"
135 " label TEXT NOT NULL"
137 GNUNET_SQ_make_try_execute ("CREATE INDEX IF NOT EXISTS ir_pkey_reverse "
138 "ON ns098records (zone_private_key,pkey)"),
139 GNUNET_SQ_make_try_execute ("CREATE INDEX IF NOT EXISTS ir_pkey_iter "
140 "ON ns098records (zone_private_key,uid)"),
141 GNUNET_SQ_EXECUTE_STATEMENT_END
143 struct GNUNET_SQ_PrepareStatement ps[] = {
144 GNUNET_SQ_make_prepare ("INSERT INTO ns098records "
145 "(zone_private_key,pkey,rvalue,record_count,record_data,label)"
146 " VALUES (?, ?, ?, ?, ?, ?)",
147 &plugin->store_records),
148 GNUNET_SQ_make_prepare ("DELETE FROM ns098records "
149 "WHERE zone_private_key=? AND label=?",
150 &plugin->delete_records),
151 GNUNET_SQ_make_prepare ("SELECT uid,record_count,record_data,label"
153 " WHERE zone_private_key=? AND pkey=?",
154 &plugin->zone_to_name),
155 GNUNET_SQ_make_prepare ("SELECT uid,record_count,record_data,label"
157 " WHERE zone_private_key=? AND _rowid_ >= ?"
158 " ORDER BY _rowid_ ASC"
160 &plugin->iterate_zone),
161 GNUNET_SQ_make_prepare ("SELECT uid,record_count,record_data,label,zone_private_key"
163 " WHERE _rowid_ >= ?"
164 " ORDER BY _rowid_ ASC"
166 &plugin->iterate_all_zones),
167 GNUNET_SQ_make_prepare ("SELECT uid,record_count,record_data,label,zone_private_key"
169 " WHERE zone_private_key=? AND label=?",
170 &plugin->lookup_label),
171 GNUNET_SQ_PREPARE_END
175 GNUNET_CONFIGURATION_get_value_filename (plugin->cfg,
180 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
183 return GNUNET_SYSERR;
186 GNUNET_DISK_file_test (sqlite_filename))
189 GNUNET_DISK_directory_create_for_file (sqlite_filename))
192 GNUNET_free (sqlite_filename);
193 return GNUNET_SYSERR;
196 /* sqlite_filename should be UTF-8-encoded. If it isn't, it's a bug */
197 plugin->fn = sqlite_filename;
199 /* Open database and precompile statements */
201 sqlite3_open (plugin->fn,
204 LOG (GNUNET_ERROR_TYPE_ERROR,
205 _("Unable to initialize SQLite: %s.\n"),
206 sqlite3_errmsg (plugin->dbh));
207 return GNUNET_SYSERR;
209 GNUNET_break (SQLITE_OK ==
210 sqlite3_busy_timeout (plugin->dbh,
213 GNUNET_SQ_exec_statements (plugin->dbh,
217 LOG (GNUNET_ERROR_TYPE_ERROR,
218 _("Failed to setup database at `%s'\n"),
220 return GNUNET_SYSERR;
224 GNUNET_SQ_prepare (plugin->dbh,
228 LOG (GNUNET_ERROR_TYPE_ERROR,
229 _("Failed to setup database at `%s'\n"),
231 return GNUNET_SYSERR;
238 * Shutdown database connection and associate data
240 * @param plugin the plugin context (state for this module)
243 database_shutdown (struct Plugin *plugin)
248 if (NULL != plugin->store_records)
249 sqlite3_finalize (plugin->store_records);
250 if (NULL != plugin->delete_records)
251 sqlite3_finalize (plugin->delete_records);
252 if (NULL != plugin->iterate_zone)
253 sqlite3_finalize (plugin->iterate_zone);
254 if (NULL != plugin->iterate_all_zones)
255 sqlite3_finalize (plugin->iterate_all_zones);
256 if (NULL != plugin->zone_to_name)
257 sqlite3_finalize (plugin->zone_to_name);
258 if (NULL != plugin->lookup_label)
259 sqlite3_finalize (plugin->lookup_label);
260 result = sqlite3_close (plugin->dbh);
261 if (result == SQLITE_BUSY)
263 LOG (GNUNET_ERROR_TYPE_WARNING,
264 _("Tried to close sqlite without finalizing all prepared statements.\n"));
265 stmt = sqlite3_next_stmt (plugin->dbh,
269 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
271 "Closing statement %p\n",
273 result = sqlite3_finalize (stmt);
274 if (result != SQLITE_OK)
275 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING,
277 "Failed to close statement %p: %d\n",
280 stmt = sqlite3_next_stmt (plugin->dbh,
283 result = sqlite3_close (plugin->dbh);
285 if (SQLITE_OK != result)
287 GNUNET_ERROR_TYPE_ERROR,
290 GNUNET_free_non_null (plugin->fn);
295 * Store a record in the datastore. Removes any existing record in the
296 * same zone with the same name.
298 * @param cls closure (internal context for the plugin)
299 * @param zone_key private key of the zone
300 * @param label name that is being mapped (at most 255 characters long)
301 * @param rd_count number of entries in @a rd array
302 * @param rd array of records with data to store
303 * @return #GNUNET_OK on success, else #GNUNET_SYSERR
306 namestore_sqlite_store_records (void *cls,
307 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone_key,
309 unsigned int rd_count,
310 const struct GNUNET_GNSRECORD_Data *rd)
312 struct Plugin *plugin = cls;
314 struct GNUNET_CRYPTO_EcdsaPublicKey pkey;
321 for (unsigned int i=0;i<rd_count;i++)
322 if (GNUNET_GNSRECORD_TYPE_PKEY == rd[i].record_type)
324 GNUNET_break (sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey) ==
326 GNUNET_memcpy (&pkey,
331 rvalue = GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
333 data_size = GNUNET_GNSRECORD_records_get_size (rd_count,
338 return GNUNET_SYSERR;
340 if (data_size > 64 * 65536)
343 return GNUNET_SYSERR;
346 /* First delete 'old' records */
347 char data[data_size];
348 struct GNUNET_SQ_QueryParam dparams[] = {
349 GNUNET_SQ_query_param_auto_from_type (zone_key),
350 GNUNET_SQ_query_param_string (label),
351 GNUNET_SQ_query_param_end
355 ret = GNUNET_GNSRECORD_records_serialize (rd_count,
363 return GNUNET_SYSERR;
366 GNUNET_SQ_bind (plugin->delete_records,
370 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
371 "sqlite3_bind_XXXX");
372 GNUNET_SQ_reset (plugin->dbh,
373 plugin->delete_records);
374 return GNUNET_SYSERR;
377 n = sqlite3_step (plugin->delete_records);
378 GNUNET_SQ_reset (plugin->dbh,
379 plugin->delete_records);
383 uint32_t rd_count32 = (uint32_t) rd_count;
384 struct GNUNET_SQ_QueryParam sparams[] = {
385 GNUNET_SQ_query_param_auto_from_type (zone_key),
386 GNUNET_SQ_query_param_auto_from_type (&pkey),
387 GNUNET_SQ_query_param_uint64 (&rvalue),
388 GNUNET_SQ_query_param_uint32 (&rd_count32),
389 GNUNET_SQ_query_param_fixed_size (data, data_size),
390 GNUNET_SQ_query_param_string (label),
391 GNUNET_SQ_query_param_end
395 GNUNET_SQ_bind (plugin->store_records,
399 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
400 "sqlite3_bind_XXXX");
401 GNUNET_SQ_reset (plugin->dbh,
402 plugin->store_records);
403 return GNUNET_SYSERR;
405 n = sqlite3_step (plugin->store_records);
406 GNUNET_SQ_reset (plugin->dbh,
407 plugin->store_records);
414 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
418 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
424 GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK,
429 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
431 return GNUNET_SYSERR;
437 * The given 'sqlite' statement has been prepared to be run.
438 * It will return a record which should be given to the iterator.
439 * Runs the statement and parses the returned record.
441 * @param plugin plugin context
442 * @param stmt to run (and then clean up)
443 * @param zone_key private key of the zone
444 * @param limit maximum number of results to fetch
445 * @param iter iterator to call with the result
446 * @param iter_cls closure for @a iter
447 * @return #GNUNET_OK on success, #GNUNET_NO if there were no results, #GNUNET_SYSERR on error
450 get_records_and_call_iterator (struct Plugin *plugin,
452 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone_key,
454 GNUNET_NAMESTORE_RecordIterator iter,
461 for (uint64_t i = 0;i<limit;i++)
463 sret = sqlite3_step (stmt);
465 if (SQLITE_DONE == sret)
467 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
468 "Iteration done (no results)\n");
472 if (SQLITE_ROW != sret)
475 GNUNET_ERROR_TYPE_ERROR,
483 uint32_t record_count;
487 struct GNUNET_CRYPTO_EcdsaPrivateKey zk;
488 struct GNUNET_SQ_ResultSpec rs[] = {
489 GNUNET_SQ_result_spec_uint64 (&seq),
490 GNUNET_SQ_result_spec_uint32 (&record_count),
491 GNUNET_SQ_result_spec_variable_size (&data,
493 GNUNET_SQ_result_spec_string (&label),
494 GNUNET_SQ_result_spec_end
496 struct GNUNET_SQ_ResultSpec rsx[] = {
497 GNUNET_SQ_result_spec_uint64 (&seq),
498 GNUNET_SQ_result_spec_uint32 (&record_count),
499 GNUNET_SQ_result_spec_variable_size (&data,
501 GNUNET_SQ_result_spec_string (&label),
502 GNUNET_SQ_result_spec_auto_from_type (&zk),
503 GNUNET_SQ_result_spec_end
506 if (NULL == zone_key)
509 ret = GNUNET_SQ_extract_result (stmt,
514 ret = GNUNET_SQ_extract_result (stmt,
517 if ( (GNUNET_OK != ret) ||
518 (record_count > 64 * 1024) )
520 /* sanity check, don't stack allocate far too much just
521 because database might contain a large value here */
528 struct GNUNET_GNSRECORD_Data rd[record_count];
531 GNUNET_GNSRECORD_records_deserialize (data_size,
551 GNUNET_SQ_cleanup_result (rs);
554 GNUNET_SQ_reset (plugin->dbh,
561 * Lookup records in the datastore for which we are the authority.
563 * @param cls closure (internal context for the plugin)
564 * @param zone private key of the zone
565 * @param label name of the record in the zone
566 * @param iter function to call with the result
567 * @param iter_cls closure for @a iter
568 * @return #GNUNET_OK on success, #GNUNET_NO for no results, else #GNUNET_SYSERR
571 namestore_sqlite_lookup_records (void *cls,
572 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
574 GNUNET_NAMESTORE_RecordIterator iter,
577 struct Plugin *plugin = cls;
578 struct GNUNET_SQ_QueryParam params[] = {
579 GNUNET_SQ_query_param_auto_from_type (zone),
580 GNUNET_SQ_query_param_string (label),
581 GNUNET_SQ_query_param_end
587 return GNUNET_SYSERR;
590 GNUNET_SQ_bind (plugin->lookup_label,
593 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
594 "sqlite3_bind_XXXX");
595 GNUNET_SQ_reset (plugin->dbh,
596 plugin->lookup_label);
597 return GNUNET_SYSERR;
599 return get_records_and_call_iterator (plugin,
600 plugin->lookup_label,
609 * Iterate over the results for a particular key and zone in the
610 * datastore. Will return at most one result to the iterator.
612 * @param cls closure (internal context for the plugin)
613 * @param zone hash of public key of the zone, NULL to iterate over all zones
614 * @param serial serial number to exclude in the list of all matching records
615 * @param limit maximum number of results to return
616 * @param iter function to call with the result
617 * @param iter_cls closure for @a iter
618 * @return #GNUNET_OK on success, #GNUNET_NO if there were no more results, #GNUNET_SYSERR on error
621 namestore_sqlite_iterate_records (void *cls,
622 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
625 GNUNET_NAMESTORE_RecordIterator iter,
628 struct Plugin *plugin = cls;
634 struct GNUNET_SQ_QueryParam params[] = {
635 GNUNET_SQ_query_param_uint64 (&serial),
636 GNUNET_SQ_query_param_uint64 (&limit),
637 GNUNET_SQ_query_param_end
640 stmt = plugin->iterate_all_zones;
641 err = GNUNET_SQ_bind (stmt,
646 struct GNUNET_SQ_QueryParam params[] = {
647 GNUNET_SQ_query_param_auto_from_type (zone),
648 GNUNET_SQ_query_param_uint64 (&serial),
649 GNUNET_SQ_query_param_uint64 (&limit),
650 GNUNET_SQ_query_param_end
653 stmt = plugin->iterate_zone;
654 err = GNUNET_SQ_bind (stmt,
657 if (GNUNET_OK != err)
660 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
661 "sqlite3_bind_XXXX");
662 GNUNET_SQ_reset (plugin->dbh,
664 return GNUNET_SYSERR;
666 return get_records_and_call_iterator (plugin,
676 * Look for an existing PKEY delegation record for a given public key.
677 * Returns at most one result to the iterator.
679 * @param cls closure (internal context for the plugin)
680 * @param zone private key of the zone to look up in, never NULL
681 * @param value_zone public key of the target zone (value), never NULL
682 * @param iter function to call with the result
683 * @param iter_cls closure for @a iter
684 * @return #GNUNET_OK on success, #GNUNET_NO if there were no results, #GNUNET_SYSERR on error
687 namestore_sqlite_zone_to_name (void *cls,
688 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
689 const struct GNUNET_CRYPTO_EcdsaPublicKey *value_zone,
690 GNUNET_NAMESTORE_RecordIterator iter,
693 struct Plugin *plugin = cls;
694 struct GNUNET_SQ_QueryParam params[] = {
695 GNUNET_SQ_query_param_auto_from_type (zone),
696 GNUNET_SQ_query_param_auto_from_type (value_zone),
697 GNUNET_SQ_query_param_end
701 GNUNET_SQ_bind (plugin->zone_to_name,
705 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
706 "sqlite3_bind_XXXX");
707 GNUNET_SQ_reset (plugin->dbh,
708 plugin->zone_to_name);
709 return GNUNET_SYSERR;
711 LOG (GNUNET_ERROR_TYPE_DEBUG,
712 "Performing reverse lookup for `%s'\n",
713 GNUNET_GNSRECORD_z2s (value_zone));
714 return get_records_and_call_iterator (plugin,
715 plugin->zone_to_name,
724 * Entry point for the plugin.
726 * @param cls the "struct GNUNET_NAMESTORE_PluginEnvironment*"
727 * @return NULL on error, otherwise the plugin context
730 libgnunet_plugin_namestore_sqlite_init (void *cls)
732 static struct Plugin plugin;
733 const struct GNUNET_CONFIGURATION_Handle *cfg = cls;
734 struct GNUNET_NAMESTORE_PluginFunctions *api;
736 if (NULL != plugin.cfg)
737 return NULL; /* can only initialize once! */
740 sizeof (struct Plugin));
742 if (GNUNET_OK != database_setup (&plugin))
744 database_shutdown (&plugin);
747 api = GNUNET_new (struct GNUNET_NAMESTORE_PluginFunctions);
749 api->store_records = &namestore_sqlite_store_records;
750 api->iterate_records = &namestore_sqlite_iterate_records;
751 api->zone_to_name = &namestore_sqlite_zone_to_name;
752 api->lookup_records = &namestore_sqlite_lookup_records;
753 LOG (GNUNET_ERROR_TYPE_INFO,
754 _("Sqlite database running\n"));
760 * Exit point from the plugin.
762 * @param cls the plugin context (as returned by "init")
763 * @return always NULL
766 libgnunet_plugin_namestore_sqlite_done (void *cls)
768 struct GNUNET_NAMESTORE_PluginFunctions *api = cls;
769 struct Plugin *plugin = api->cls;
771 database_shutdown (plugin);
774 LOG (GNUNET_ERROR_TYPE_DEBUG,
775 "sqlite plugin is finished\n");
779 /* end of plugin_namestore_sqlite.c */