aboutsummaryrefslogtreecommitdiff
path: root/src/namestore/plugin_namestore_sqlite.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/namestore/plugin_namestore_sqlite.c')
-rw-r--r--src/namestore/plugin_namestore_sqlite.c983
1 files changed, 0 insertions, 983 deletions
diff --git a/src/namestore/plugin_namestore_sqlite.c b/src/namestore/plugin_namestore_sqlite.c
deleted file mode 100644
index 15a6586b5..000000000
--- a/src/namestore/plugin_namestore_sqlite.c
+++ /dev/null
@@ -1,983 +0,0 @@
1/*
2 * This file is part of GNUnet
3 * Copyright (C) 2009-2017 GNUnet e.V.
4 *
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
7 * by the Free Software Foundation, either version 3 of the License,
8 * or (at your option) any later version.
9 *
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 * Affero General Public License for more details.
14 *
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/>.
17
18 SPDX-License-Identifier: AGPL3.0-or-later
19 */
20
21/**
22 * @file namestore/plugin_namestore_sqlite.c
23 * @brief sqlite-based namestore backend
24 * @author Christian Grothoff
25 */
26
27#include "platform.h"
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"
33#include <sqlite3.h>
34
35/**
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).
40 *
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.
44 */
45#define BUSY_TIMEOUT_MS 1000
46
47
48/**
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).
52 */
53#define LOG_SQLITE(db, level, cmd) do { \
54 GNUNET_log_from (level, \
55 "namestore-sqlite", _ ( \
56 "`%s' failed at %s:%d with error: %s\n"), \
57 cmd, \
58 __FILE__, __LINE__, \
59 sqlite3_errmsg ( \
60 db->dbh)); \
61} while (0)
62
63#define LOG(kind, ...) GNUNET_log_from (kind, "namestore-sqlite", __VA_ARGS__)
64
65
66/**
67 * Context for all functions in this plugin.
68 */
69struct Plugin
70{
71 const struct GNUNET_CONFIGURATION_Handle *cfg;
72
73 /**
74 * Database filename.
75 */
76 char *fn;
77
78 /**
79 * Statements prepared, we are ready to go if true.
80 */
81 bool ready;
82
83 /**
84 * Native SQLite database handle.
85 */
86 sqlite3 *dbh;
87
88 /**
89 * Precompiled SQL to store records.
90 */
91 sqlite3_stmt *store_records;
92
93 /**
94 * Precompiled SQL to deltete existing records.
95 */
96 sqlite3_stmt *delete_records;
97
98 /**
99 * Precompiled SQL for iterate records within a zone.
100 */
101 sqlite3_stmt *iterate_zone;
102
103 /**
104 * Precompiled SQL for iterate all records within all zones.
105 */
106 sqlite3_stmt *iterate_all_zones;
107
108 /**
109 * Precompiled SQL to for reverse lookup based on PKEY.
110 */
111 sqlite3_stmt *zone_to_name;
112
113 /**
114 * Precompiled SQL to lookup records based on label.
115 */
116 sqlite3_stmt *lookup_label;
117};
118
119
120/**
121 * Initialize the database connections and associated
122 * data structures (create tables and indices
123 * as needed as well).
124 *
125 * @param plugin the plugin context (state for this module)
126 * @return #GNUNET_OK on success
127 */
128static enum GNUNET_GenericReturnValue
129database_prepare (struct Plugin *plugin)
130{
131 if (plugin->ready)
132 return GNUNET_OK;
133 struct GNUNET_SQ_ExecuteStatement es[] = {
134 GNUNET_SQ_make_try_execute ("PRAGMA temp_store=MEMORY"),
135 GNUNET_SQ_make_try_execute ("PRAGMA synchronous=NORMAL"),
136 GNUNET_SQ_make_try_execute ("PRAGMA legacy_file_format=OFF"),
137 GNUNET_SQ_make_try_execute ("PRAGMA auto_vacuum=INCREMENTAL"),
138 GNUNET_SQ_make_try_execute ("PRAGMA encoding=\"UTF-8\""),
139 GNUNET_SQ_make_try_execute ("PRAGMA locking_mode=NORMAL"),
140 GNUNET_SQ_make_try_execute ("PRAGMA journal_mode=WAL"),
141 GNUNET_SQ_make_try_execute ("PRAGMA page_size=4092"),
142 GNUNET_SQ_EXECUTE_STATEMENT_END
143 };
144 struct GNUNET_SQ_PrepareStatement ps[] = {
145 GNUNET_SQ_make_prepare ("INSERT INTO ns098records "
146 "(zone_private_key,pkey,rvalue,record_count,record_data,label)"
147 " VALUES (?, ?, ?, ?, ?, ?)",
148 &plugin->store_records),
149 GNUNET_SQ_make_prepare ("DELETE FROM ns098records "
150 "WHERE zone_private_key=? AND label=?",
151 &plugin->delete_records),
152 GNUNET_SQ_make_prepare ("SELECT uid,record_count,record_data,label"
153 " FROM ns098records"
154 " WHERE zone_private_key=? AND pkey=?",
155 &plugin->zone_to_name),
156 GNUNET_SQ_make_prepare ("SELECT uid,record_count,record_data,label"
157 " FROM ns098records"
158 " WHERE zone_private_key=? AND uid > ?"
159 " ORDER BY uid ASC"
160 " LIMIT ?",
161 &plugin->iterate_zone),
162 GNUNET_SQ_make_prepare (
163 "SELECT uid,record_count,record_data,label,zone_private_key"
164 " FROM ns098records"
165 " WHERE uid > ?"
166 " ORDER BY uid ASC"
167 " LIMIT ?",
168 &plugin->iterate_all_zones),
169 GNUNET_SQ_make_prepare ("SELECT uid,record_count,record_data,label"
170 " FROM ns098records"
171 " WHERE zone_private_key=? AND label=?",
172 &plugin->lookup_label),
173 GNUNET_SQ_PREPARE_END
174 };
175
176 if (GNUNET_OK !=
177 GNUNET_SQ_exec_statements (plugin->dbh,
178 es))
179 {
180 LOG (GNUNET_ERROR_TYPE_ERROR,
181 _ ("Failed to setup database with: `%s'\n"),
182 sqlite3_errmsg (plugin->dbh));
183 return GNUNET_SYSERR;
184 }
185 if (GNUNET_OK !=
186 GNUNET_SQ_prepare (plugin->dbh,
187 ps))
188 {
189 GNUNET_break (0);
190 LOG (GNUNET_ERROR_TYPE_ERROR,
191 _ ("Failed to setup database with: `%s'\n"),
192 sqlite3_errmsg (plugin->dbh));
193 return GNUNET_SYSERR;
194 }
195 plugin->ready = GNUNET_YES;
196 return GNUNET_OK;
197}
198
199
200/**
201 * Shutdown database connection and associate data
202 * structures.
203 * @param plugin the plugin context (state for this module)
204 */
205static void
206database_shutdown (struct Plugin *plugin)
207{
208 int result;
209 sqlite3_stmt *stmt;
210
211 if (NULL != plugin->store_records)
212 sqlite3_finalize (plugin->store_records);
213 if (NULL != plugin->delete_records)
214 sqlite3_finalize (plugin->delete_records);
215 if (NULL != plugin->iterate_zone)
216 sqlite3_finalize (plugin->iterate_zone);
217 if (NULL != plugin->iterate_all_zones)
218 sqlite3_finalize (plugin->iterate_all_zones);
219 if (NULL != plugin->zone_to_name)
220 sqlite3_finalize (plugin->zone_to_name);
221 if (NULL != plugin->lookup_label)
222 sqlite3_finalize (plugin->lookup_label);
223 result = sqlite3_close (plugin->dbh);
224 if (result == SQLITE_BUSY)
225 {
226 LOG (GNUNET_ERROR_TYPE_WARNING,
227 _ (
228 "Tried to close sqlite without finalizing all prepared statements.\n"));
229 stmt = sqlite3_next_stmt (plugin->dbh,
230 NULL);
231 while (NULL != stmt)
232 {
233 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
234 "sqlite",
235 "Closing statement %p\n",
236 stmt);
237 result = sqlite3_finalize (stmt);
238 if (result != SQLITE_OK)
239 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING,
240 "sqlite",
241 "Failed to close statement %p: %d\n",
242 stmt,
243 result);
244 stmt = sqlite3_next_stmt (plugin->dbh,
245 NULL);
246 }
247 result = sqlite3_close (plugin->dbh);
248 }
249 if (SQLITE_OK != result)
250 LOG_SQLITE (plugin,
251 GNUNET_ERROR_TYPE_ERROR,
252 "sqlite3_close");
253
254}
255
256
257/**
258 * Store a record in the datastore. Removes any existing record in the
259 * same zone with the same name.
260 *
261 * @param cls closure (internal context for the plugin)
262 * @param zone_key private key of the zone
263 * @param label name that is being mapped (at most 255 characters long)
264 * @param rd_count number of entries in @a rd array
265 * @param rd array of records with data to store
266 * @return #GNUNET_OK on success, else #GNUNET_SYSERR
267 */
268static enum GNUNET_GenericReturnValue
269namestore_sqlite_store_records (void *cls,
270 const struct
271 GNUNET_IDENTITY_PrivateKey *zone_key,
272 const char *label,
273 unsigned int rd_count,
274 const struct GNUNET_GNSRECORD_Data *rd)
275{
276 struct Plugin *plugin = cls;
277 int n;
278 struct GNUNET_IDENTITY_PublicKey pkey;
279 uint64_t rvalue;
280 ssize_t data_size;
281
282 GNUNET_assert (GNUNET_OK == database_prepare (plugin));
283 memset (&pkey,
284 0,
285 sizeof(pkey));
286 for (unsigned int i = 0; i < rd_count; i++)
287 {
288 LOG (GNUNET_ERROR_TYPE_DEBUG,
289 "Checking if `%d' is zonekey type\n",
290 rd[i].record_type);
291
292 if (GNUNET_YES == GNUNET_GNSRECORD_is_zonekey_type (rd[i].record_type))
293 {
294 GNUNET_break (GNUNET_YES ==
295 GNUNET_GNSRECORD_identity_from_data (rd[i].data,
296 rd[i].data_size,
297 rd[i].record_type,
298 &pkey));
299 LOG (GNUNET_ERROR_TYPE_DEBUG,
300 "Storing delegation zone record value `%s'\n",
301 GNUNET_GNSRECORD_z2s (&pkey));
302
303 break;
304 }
305 }
306 rvalue = GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
307 UINT64_MAX);
308 data_size = GNUNET_GNSRECORD_records_get_size (rd_count,
309 rd);
310 if (data_size < 0)
311 {
312 GNUNET_break (0);
313 return GNUNET_SYSERR;
314 }
315 if (data_size > 64 * 65536)
316 {
317 GNUNET_break (0);
318 return GNUNET_SYSERR;
319 }
320 {
321 /* First delete 'old' records */
322 char data[data_size];
323 struct GNUNET_SQ_QueryParam dparams[] = {
324 GNUNET_SQ_query_param_auto_from_type (zone_key),
325 GNUNET_SQ_query_param_string (label),
326 GNUNET_SQ_query_param_end
327 };
328 ssize_t ret;
329
330 ret = GNUNET_GNSRECORD_records_serialize (rd_count,
331 rd,
332 data_size,
333 data);
334 if ((ret < 0) ||
335 (data_size != ret))
336 {
337 GNUNET_break (0);
338 return GNUNET_SYSERR;
339 }
340 if (GNUNET_OK !=
341 GNUNET_SQ_bind (plugin->delete_records,
342 dparams))
343 {
344 LOG_SQLITE (plugin,
345 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
346 "sqlite3_bind_XXXX");
347 GNUNET_SQ_reset (plugin->dbh,
348 plugin->delete_records);
349 return GNUNET_SYSERR;
350 }
351 n = sqlite3_step (plugin->delete_records);
352 GNUNET_SQ_reset (plugin->dbh,
353 plugin->delete_records);
354
355 if (0 != rd_count)
356 {
357 uint32_t rd_count32 = (uint32_t) rd_count;
358 struct GNUNET_SQ_QueryParam sparams[] = {
359 GNUNET_SQ_query_param_auto_from_type (zone_key),
360 GNUNET_SQ_query_param_auto_from_type (&pkey),
361 GNUNET_SQ_query_param_uint64 (&rvalue),
362 GNUNET_SQ_query_param_uint32 (&rd_count32),
363 GNUNET_SQ_query_param_fixed_size (data, data_size),
364 GNUNET_SQ_query_param_string (label),
365 GNUNET_SQ_query_param_end
366 };
367
368 if (GNUNET_OK !=
369 GNUNET_SQ_bind (plugin->store_records,
370 sparams))
371 {
372 LOG_SQLITE (plugin,
373 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
374 "sqlite3_bind_XXXX");
375 GNUNET_SQ_reset (plugin->dbh,
376 plugin->store_records);
377 return GNUNET_SYSERR;
378 }
379 n = sqlite3_step (plugin->store_records);
380 GNUNET_SQ_reset (plugin->dbh,
381 plugin->store_records);
382 }
383 }
384 switch (n)
385 {
386 case SQLITE_DONE:
387 if (0 != rd_count)
388 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
389 "sqlite",
390 "Record stored\n");
391 else
392 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
393 "sqlite",
394 "Record deleted\n");
395 return GNUNET_OK;
396
397 case SQLITE_BUSY:
398 LOG_SQLITE (plugin,
399 GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK,
400 "sqlite3_step");
401 return GNUNET_NO;
402
403 default:
404 LOG_SQLITE (plugin,
405 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
406 "sqlite3_step");
407 return GNUNET_SYSERR;
408 }
409}
410
411
412/**
413 * The given 'sqlite' statement has been prepared to be run.
414 * It will return a record which should be given to the iterator.
415 * Runs the statement and parses the returned record.
416 *
417 * @param plugin plugin context
418 * @param stmt to run (and then clean up)
419 * @param zone_key private key of the zone
420 * @param limit maximum number of results to fetch
421 * @param iter iterator to call with the result
422 * @param iter_cls closure for @a iter
423 * @return #GNUNET_OK on success, #GNUNET_NO if there were no results, #GNUNET_SYSERR on error
424 */
425static enum GNUNET_GenericReturnValue
426get_records_and_call_iterator (struct Plugin *plugin,
427 sqlite3_stmt *stmt,
428 const struct
429 GNUNET_IDENTITY_PrivateKey *zone_key,
430 uint64_t limit,
431 GNUNET_NAMESTORE_RecordIterator iter,
432 void *iter_cls)
433{
434 int ret;
435 int sret;
436
437 ret = GNUNET_OK;
438 for (uint64_t i = 0; i < limit; i++)
439 {
440 sret = sqlite3_step (stmt);
441
442 if (SQLITE_DONE == sret)
443 {
444 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
445 "Iteration done (no results)\n");
446 ret = GNUNET_NO;
447 break;
448 }
449 if (SQLITE_ROW != sret)
450 {
451 LOG_SQLITE (plugin,
452 GNUNET_ERROR_TYPE_ERROR,
453 "sqlite_step");
454 ret = GNUNET_SYSERR;
455 break;
456 }
457
458 {
459 uint64_t seq;
460 uint32_t record_count;
461 size_t data_size;
462 void *data;
463 char *label;
464 struct GNUNET_IDENTITY_PrivateKey zk;
465 struct GNUNET_SQ_ResultSpec rs[] = {
466 GNUNET_SQ_result_spec_uint64 (&seq),
467 GNUNET_SQ_result_spec_uint32 (&record_count),
468 GNUNET_SQ_result_spec_variable_size (&data,
469 &data_size),
470 GNUNET_SQ_result_spec_string (&label),
471 GNUNET_SQ_result_spec_end
472 };
473 struct GNUNET_SQ_ResultSpec rsx[] = {
474 GNUNET_SQ_result_spec_uint64 (&seq),
475 GNUNET_SQ_result_spec_uint32 (&record_count),
476 GNUNET_SQ_result_spec_variable_size (&data,
477 &data_size),
478 GNUNET_SQ_result_spec_string (&label),
479 GNUNET_SQ_result_spec_auto_from_type (&zk),
480 GNUNET_SQ_result_spec_end
481 };
482
483 ret = GNUNET_SQ_extract_result (stmt,
484 (NULL == zone_key)
485 ? rsx
486 : rs);
487 if ((GNUNET_OK != ret) ||
488 (record_count > 64 * 1024))
489 {
490 /* sanity check, don't stack allocate far too much just
491 because database might contain a large value here */
492 GNUNET_break (0);
493 ret = GNUNET_SYSERR;
494 break;
495 }
496 else
497 {
498 struct GNUNET_GNSRECORD_Data rd[record_count];
499
500 GNUNET_assert (0 != seq);
501 if (GNUNET_OK !=
502 GNUNET_GNSRECORD_records_deserialize (data_size,
503 data,
504 record_count,
505 rd))
506 {
507 GNUNET_break (0);
508 ret = GNUNET_SYSERR;
509 break;
510 }
511 else
512 {
513 if (NULL != zone_key)
514 zk = *zone_key;
515 if (NULL != iter)
516 iter (iter_cls,
517 seq,
518 &zk,
519 label,
520 record_count,
521 rd);
522 }
523 }
524 GNUNET_SQ_cleanup_result (rs);
525 }
526 }
527 GNUNET_SQ_reset (plugin->dbh,
528 stmt);
529 return ret;
530}
531
532
533/**
534 * Lookup records in the datastore for which we are the authority.
535 *
536 * @param cls closure (internal context for the plugin)
537 * @param zone private key of the zone
538 * @param label name of the record in the zone
539 * @param iter function to call with the result
540 * @param iter_cls closure for @a iter
541 * @return #GNUNET_OK on success, #GNUNET_NO for no results, else #GNUNET_SYSERR
542 */
543static enum GNUNET_GenericReturnValue
544namestore_sqlite_lookup_records (void *cls,
545 const struct
546 GNUNET_IDENTITY_PrivateKey *zone,
547 const char *label,
548 GNUNET_NAMESTORE_RecordIterator iter,
549 void *iter_cls)
550{
551 struct Plugin *plugin = cls;
552 GNUNET_assert (GNUNET_OK == database_prepare (plugin));
553 struct GNUNET_SQ_QueryParam params[] = {
554 GNUNET_SQ_query_param_auto_from_type (zone),
555 GNUNET_SQ_query_param_string (label),
556 GNUNET_SQ_query_param_end
557 };
558
559 if (NULL == zone)
560 {
561 GNUNET_break (0);
562 return GNUNET_SYSERR;
563 }
564 if (GNUNET_OK !=
565 GNUNET_SQ_bind (plugin->lookup_label,
566 params))
567 {
568 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
569 "sqlite3_bind_XXXX");
570 GNUNET_SQ_reset (plugin->dbh,
571 plugin->lookup_label);
572 return GNUNET_SYSERR;
573 }
574 return get_records_and_call_iterator (plugin,
575 plugin->lookup_label,
576 zone,
577 1,
578 iter,
579 iter_cls);
580}
581
582
583/**
584 * Iterate over the results for a particular key and zone in the
585 * datastore. Will return at most one result to the iterator.
586 *
587 * @param cls closure (internal context for the plugin)
588 * @param zone hash of public key of the zone, NULL to iterate over all zones
589 * @param serial serial number to exclude in the list of all matching records
590 * @param limit maximum number of results to return
591 * @param iter function to call with the result
592 * @param iter_cls closure for @a iter
593 * @return #GNUNET_OK on success, #GNUNET_NO if there were no more results, #GNUNET_SYSERR on error
594 */
595static enum GNUNET_GenericReturnValue
596namestore_sqlite_iterate_records (void *cls,
597 const struct
598 GNUNET_IDENTITY_PrivateKey *zone,
599 uint64_t serial,
600 uint64_t limit,
601 GNUNET_NAMESTORE_RecordIterator iter,
602 void *iter_cls)
603{
604 struct Plugin *plugin = cls;
605 sqlite3_stmt *stmt;
606 int err;
607
608 GNUNET_assert (GNUNET_OK == database_prepare (plugin));
609 if (NULL == zone)
610 {
611 struct GNUNET_SQ_QueryParam params[] = {
612 GNUNET_SQ_query_param_uint64 (&serial),
613 GNUNET_SQ_query_param_uint64 (&limit),
614 GNUNET_SQ_query_param_end
615 };
616
617 stmt = plugin->iterate_all_zones;
618 err = GNUNET_SQ_bind (stmt,
619 params);
620 }
621 else
622 {
623 struct GNUNET_SQ_QueryParam params[] = {
624 GNUNET_SQ_query_param_auto_from_type (zone),
625 GNUNET_SQ_query_param_uint64 (&serial),
626 GNUNET_SQ_query_param_uint64 (&limit),
627 GNUNET_SQ_query_param_end
628 };
629
630 stmt = plugin->iterate_zone;
631 err = GNUNET_SQ_bind (stmt,
632 params);
633 }
634 if (GNUNET_OK != err)
635 {
636 LOG_SQLITE (plugin,
637 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
638 "sqlite3_bind_XXXX");
639 GNUNET_SQ_reset (plugin->dbh,
640 stmt);
641 return GNUNET_SYSERR;
642 }
643 return get_records_and_call_iterator (plugin,
644 stmt,
645 zone,
646 limit,
647 iter,
648 iter_cls);
649}
650
651
652/**
653 * Look for an existing PKEY delegation record for a given public key.
654 * Returns at most one result to the iterator.
655 *
656 * @param cls closure (internal context for the plugin)
657 * @param zone private key of the zone to look up in, never NULL
658 * @param value_zone public key of the target zone (value), never NULL
659 * @param iter function to call with the result
660 * @param iter_cls closure for @a iter
661 * @return #GNUNET_OK on success, #GNUNET_NO if there were no results, #GNUNET_SYSERR on error
662 */
663static enum GNUNET_GenericReturnValue
664namestore_sqlite_zone_to_name (void *cls,
665 const struct GNUNET_IDENTITY_PrivateKey *zone,
666 const struct
667 GNUNET_IDENTITY_PublicKey *value_zone,
668 GNUNET_NAMESTORE_RecordIterator iter,
669 void *iter_cls)
670{
671 struct Plugin *plugin = cls;
672 GNUNET_assert (GNUNET_OK == database_prepare (plugin));
673 struct GNUNET_SQ_QueryParam params[] = {
674 GNUNET_SQ_query_param_auto_from_type (zone),
675 GNUNET_SQ_query_param_auto_from_type (value_zone),
676 GNUNET_SQ_query_param_end
677 };
678
679 if (GNUNET_OK !=
680 GNUNET_SQ_bind (plugin->zone_to_name,
681 params))
682 {
683 LOG_SQLITE (plugin,
684 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
685 "sqlite3_bind_XXXX");
686 GNUNET_SQ_reset (plugin->dbh,
687 plugin->zone_to_name);
688 return GNUNET_SYSERR;
689 }
690 LOG (GNUNET_ERROR_TYPE_DEBUG,
691 "Performing reverse lookup for `%s'\n",
692 GNUNET_GNSRECORD_z2s (value_zone));
693 return get_records_and_call_iterator (plugin,
694 plugin->zone_to_name,
695 zone,
696 1,
697 iter,
698 iter_cls);
699}
700
701
702/**
703 * Begin a transaction for a client.
704 * This locks the database. SQLite is unable to discern between different
705 * rows with a specific zone key but the API looks like this anyway.
706 * https://www.sqlite.org/lang_transaction.html
707 *
708 * @param cls closure (internal context for the plugin)
709 * @param emsg error message set of return code is #GNUNET_SYSERR
710 * @return #GNUNET_YES on success, #GNUNET_SYSERR if transaction cannot be started.
711 */
712static enum GNUNET_GenericReturnValue
713namestore_sqlite_transaction_begin (void *cls,
714 char **emsg)
715{
716 struct Plugin *plugin = cls;
717 int rc;
718 char *sqlEmsg;
719
720 GNUNET_assert (GNUNET_OK == database_prepare (plugin));
721 rc = sqlite3_exec (plugin->dbh, "BEGIN IMMEDIATE TRANSACTION;",
722 NULL, NULL, &sqlEmsg);
723 if (SQLITE_OK != rc)
724 {
725 *emsg = GNUNET_strdup (sqlEmsg);
726 sqlite3_free (sqlEmsg);
727 }
728 return (SQLITE_OK != rc) ? GNUNET_SYSERR : GNUNET_YES;
729}
730
731
732/**
733 * Commit a transaction for a client.
734 * This releases the lock on the database.
735 *
736 * @param cls closure (internal context for the plugin)
737 * @param emsg error message set of return code is #GNUNET_SYSERR
738 * @return #GNUNET_YES on success, #GNUNET_SYSERR if transaction cannot be started.
739 */
740static enum GNUNET_GenericReturnValue
741namestore_sqlite_transaction_rollback (void *cls,
742 char **emsg)
743{
744 struct Plugin *plugin = cls;
745 int rc;
746 char *sqlEmsg;
747
748 GNUNET_assert (GNUNET_OK == database_prepare (plugin));
749 rc = sqlite3_exec (plugin->dbh, "ROLLBACK;",
750 NULL, NULL, &sqlEmsg);
751 if (SQLITE_OK != rc)
752 {
753 *emsg = GNUNET_strdup (sqlEmsg);
754 sqlite3_free (sqlEmsg);
755 }
756 return (SQLITE_OK != rc) ? GNUNET_SYSERR : GNUNET_YES;
757}
758
759
760/**
761 * Roll back a transaction for a client.
762 * This releases the lock on the database.
763 *
764 * @param cls closure (internal context for the plugin)
765 * @param emsg error message set of return code is #GNUNET_SYSERR
766 * @return #GNUNET_YES on success, #GNUNET_SYSERR if transaction cannot be started.
767 */
768static enum GNUNET_GenericReturnValue
769namestore_sqlite_transaction_commit (void *cls,
770 char **emsg)
771{
772 struct Plugin *plugin = cls;
773 int rc;
774 char *sqlEmsg;
775
776 GNUNET_assert (GNUNET_OK == database_prepare (plugin));
777 rc = sqlite3_exec (plugin->dbh, "END TRANSACTION;",
778 NULL, NULL, &sqlEmsg);
779 if (SQLITE_OK != rc)
780 {
781 *emsg = GNUNET_strdup (sqlEmsg);
782 sqlite3_free (sqlEmsg);
783 }
784 return (SQLITE_OK != rc) ? GNUNET_SYSERR : GNUNET_YES;
785}
786
787
788static enum GNUNET_GenericReturnValue
789namestore_sqlite_create_tables (void *cls)
790{
791 struct Plugin *plugin = cls;
792 struct GNUNET_SQ_ExecuteStatement es[] = {
793 GNUNET_SQ_make_try_execute ("PRAGMA temp_store=MEMORY"),
794 GNUNET_SQ_make_try_execute ("PRAGMA synchronous=NORMAL"),
795 GNUNET_SQ_make_try_execute ("PRAGMA legacy_file_format=OFF"),
796 GNUNET_SQ_make_try_execute ("PRAGMA auto_vacuum=INCREMENTAL"),
797 GNUNET_SQ_make_try_execute ("PRAGMA encoding=\"UTF-8\""),
798 GNUNET_SQ_make_try_execute ("PRAGMA locking_mode=NORMAL"),
799 GNUNET_SQ_make_try_execute ("PRAGMA journal_mode=WAL"),
800 GNUNET_SQ_make_try_execute ("PRAGMA page_size=4092"),
801 GNUNET_SQ_make_execute ("CREATE TABLE IF NOT EXISTS ns098records ("
802 " uid INTEGER PRIMARY KEY,"
803 " zone_private_key BLOB NOT NULL,"
804 " pkey BLOB,"
805 " rvalue INT8 NOT NULL,"
806 " record_count INT NOT NULL,"
807 " record_data BLOB NOT NULL,"
808 " label TEXT NOT NULL"
809 ")"),
810 GNUNET_SQ_make_try_execute ("CREATE INDEX ir_pkey_reverse "
811 "ON ns098records (zone_private_key,pkey)"),
812 GNUNET_SQ_make_try_execute ("CREATE INDEX ir_pkey_iter "
813 "ON ns098records (zone_private_key,uid)"),
814 GNUNET_SQ_EXECUTE_STATEMENT_END
815 };
816
817 if (GNUNET_OK !=
818 GNUNET_SQ_exec_statements (plugin->dbh,
819 es))
820 {
821 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
822 "Failed to setup database with: `%s'\n",
823 sqlite3_errmsg (plugin->dbh));
824 return GNUNET_SYSERR;
825 }
826 return GNUNET_OK;
827}
828
829
830static enum GNUNET_GenericReturnValue
831namestore_sqlite_drop_tables (void *cls)
832{
833 struct Plugin *plugin = cls;
834 struct GNUNET_SQ_ExecuteStatement es_drop[] = {
835 GNUNET_SQ_make_execute ("DROP TABLE IF EXISTS ns098records"),
836 GNUNET_SQ_EXECUTE_STATEMENT_END
837 };
838
839 if (GNUNET_OK !=
840 GNUNET_SQ_exec_statements (plugin->dbh,
841 es_drop))
842 {
843 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
844 "Failed to drop database with: `%s'\n",
845 sqlite3_errmsg (plugin->dbh));
846 return GNUNET_SYSERR;
847 }
848 return GNUNET_OK;
849}
850
851
852/**
853 * Initialize the database connections and associated
854 * data structures (create tables and indices
855 * as needed as well).
856 *
857 * @param plugin the plugin context (state for this module)
858 * @return #GNUNET_OK on success
859 */
860static enum GNUNET_GenericReturnValue
861database_connect (struct Plugin *plugin)
862{
863 char *sqlite_filename;
864
865 if (GNUNET_OK !=
866 GNUNET_CONFIGURATION_get_value_filename (plugin->cfg,
867 "namestore-sqlite",
868 "FILENAME",
869 &sqlite_filename))
870 {
871 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
872 "namestore-sqlite",
873 "FILENAME");
874 return GNUNET_SYSERR;
875 }
876 if (GNUNET_OK !=
877 GNUNET_DISK_file_test (sqlite_filename))
878 {
879 if (GNUNET_OK !=
880 GNUNET_DISK_directory_create_for_file (sqlite_filename))
881 {
882 GNUNET_break (0);
883 GNUNET_free (sqlite_filename);
884 return GNUNET_SYSERR;
885 }
886 }
887
888 /* Open database and precompile statements */
889 if ((NULL == plugin->dbh) &&
890 (SQLITE_OK != sqlite3_open (sqlite_filename,
891 &plugin->dbh)))
892 {
893 LOG (GNUNET_ERROR_TYPE_ERROR,
894 _ ("Unable to initialize SQLite: %s.\n"),
895 sqlite3_errmsg (plugin->dbh));
896 GNUNET_free (sqlite_filename);
897 return GNUNET_SYSERR;
898 }
899 GNUNET_free (sqlite_filename);
900 GNUNET_break (SQLITE_OK ==
901 sqlite3_busy_timeout (plugin->dbh,
902 BUSY_TIMEOUT_MS));
903 if (GNUNET_YES ==
904 GNUNET_CONFIGURATION_get_value_yesno (plugin->cfg,
905 "namestore-sqlite",
906 "INIT_ON_CONNECT"))
907 {
908 if (GNUNET_OK !=
909 namestore_sqlite_create_tables (plugin))
910 return GNUNET_SYSERR;
911 }
912 return GNUNET_OK;
913}
914
915
916/**
917 * Entry point for the plugin.
918 *
919 * @param cls the "struct GNUNET_NAMESTORE_PluginEnvironment*"
920 * @return NULL on error, otherwise the plugin context
921 */
922void *
923libgnunet_plugin_namestore_sqlite_init (void *cls)
924{
925 struct Plugin *plugin;
926 const struct GNUNET_CONFIGURATION_Handle *cfg = cls;
927 struct GNUNET_NAMESTORE_PluginFunctions *api;
928
929 plugin = GNUNET_new (struct Plugin);
930 plugin->cfg = cfg;
931 if (GNUNET_OK != database_connect (plugin))
932 {
933 LOG (GNUNET_ERROR_TYPE_ERROR,
934 "Database could not be connected to.\n");
935 GNUNET_free (plugin);
936 return NULL;
937 }
938 api = GNUNET_new (struct GNUNET_NAMESTORE_PluginFunctions);
939 api->cls = plugin;
940 api->store_records = &namestore_sqlite_store_records;
941 api->iterate_records = &namestore_sqlite_iterate_records;
942 api->zone_to_name = &namestore_sqlite_zone_to_name;
943 api->lookup_records = &namestore_sqlite_lookup_records;
944 api->transaction_begin = &namestore_sqlite_transaction_begin;
945 api->transaction_commit = &namestore_sqlite_transaction_commit;
946 api->transaction_rollback = &namestore_sqlite_transaction_rollback;
947 api->create_tables = &namestore_sqlite_create_tables;
948 api->drop_tables = &namestore_sqlite_drop_tables;
949 /**
950 * NOTE: Since SQlite does not support SELECT ... FOR UPDATE this is
951 * just an alias to lookup_records. The BEGIN IMMEDIATE mechanic currently
952 * implicitly ensures this API behaves as it should
953 */
954 api->edit_records = &namestore_sqlite_lookup_records;
955 LOG (GNUNET_ERROR_TYPE_DEBUG,
956 _ ("SQlite database running\n"));
957 return api;
958}
959
960
961/**
962 * Exit point from the plugin.
963 *
964 * @param cls the plugin context (as returned by "init")
965 * @return always NULL
966 */
967void *
968libgnunet_plugin_namestore_sqlite_done (void *cls)
969{
970 struct GNUNET_NAMESTORE_PluginFunctions *api = cls;
971 struct Plugin *plugin = api->cls;
972
973 database_shutdown (plugin);
974 plugin->cfg = NULL;
975 GNUNET_free (plugin);
976 GNUNET_free (api);
977 LOG (GNUNET_ERROR_TYPE_DEBUG,
978 "SQlite plugin is finished\n");
979 return NULL;
980}
981
982
983/* end of plugin_namestore_sqlite.c */