2 * This file is part of GNUnet
3 * (C) 2009-2013 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_postgres.c
23 * @brief postgres-based namestore backend
24 * @author Christian Grothoff
27 #include "gnunet_namestore_plugin.h"
28 #include "gnunet_namestore_service.h"
29 #include "gnunet_gnsrecord_lib.h"
30 #include "gnunet_postgres_lib.h"
31 #include "namestore.h"
35 * After how many ms "busy" should a DB operation fail for good?
36 * A low value makes sure that we are more responsive to requests
37 * (especially PUTs). A high value guarantees a higher success
38 * rate (SELECTs in iterate can take several seconds despite LIMIT=1).
40 * The default value of 1s should ensure that users do not experience
41 * huge latencies while at the same time allowing operations to succeed
42 * with reasonable probability.
44 #define BUSY_TIMEOUT_MS 1000
48 * Log an error message at log-level 'level' that indicates
49 * a failure of the command 'cmd' on file 'filename'
50 * with the message given by strerror(errno).
52 #define LOG_POSTGRES(db, level, cmd) do { GNUNET_log_from (level, "namestore-postgres", _("`%s' failed at %s:%d with error: %s\n"), cmd, __FILE__, __LINE__, sqlite3_errmsg(db->dbh)); } while(0)
54 #define LOG(kind,...) GNUNET_log_from (kind, "namestore-postgres", __VA_ARGS__)
58 * Context for all functions in this plugin.
63 const struct GNUNET_CONFIGURATION_Handle *cfg;
66 * Native Postgres database handle.
74 * Create our database indices.
76 * @param dbh handle to the database
79 create_indices (PGconn * dbh)
83 GNUNET_POSTGRES_exec (dbh,
84 "CREATE INDEX ir_query_hash ON ns096blocks (query,expiration_time)")) ||
86 GNUNET_POSTGRES_exec (dbh,
87 "CREATE INDEX ir_block_expiration ON ns096blocks (expiration_time)")) ||
89 GNUNET_POSTGRES_exec (dbh,
90 "CREATE INDEX ir_pkey_reverse ON ns097records (zone_private_key,pkey)")) ||
92 GNUNET_POSTGRES_exec (dbh,
93 "CREATE INDEX ir_pkey_iter ON ns097records (zone_private_key,rvalue)")) ||
95 GNUNET_POSTGRES_exec (dbh,
96 "CREATE INDEX it_iter ON ns097records (rvalue)")) )
97 LOG (GNUNET_ERROR_TYPE_ERROR,
98 _("Failed to create indices\n"));
103 * Initialize the database connections and associated
104 * data structures (create tables and indices
105 * as needed as well).
107 * @param plugin the plugin context (state for this module)
108 * @return GNUNET_OK on success
111 database_setup (struct Plugin *plugin)
115 plugin->dbh = GNUNET_POSTGRES_connect (plugin->cfg,
116 "namestore-postgres");
117 if (NULL == plugin->dbh)
118 return GNUNET_SYSERR;
120 GNUNET_CONFIGURATION_get_value_yesno (plugin->cfg,
121 "namestore-postgres",
126 "CREATE TEMPORARY TABLE ns097records ("
127 " zone_private_key BYTEA NOT NULL DEFAULT '',"
128 " pkey BYTEA DEFAULT '',"
129 " rvalue BYTEA NOT NULL DEFAULT '',"
130 " record_count INTEGER NOT NULL DEFAULT 0,"
131 " record_data BYTEA NOT NULL DEFAULT '',"
132 " label TEXT NOT NULL DEFAULT ''"
139 "CREATE TABLE ns097records ("
140 " zone_private_key BYTEA NOT NULL DEFAULT '',"
141 " pkey BYTEA DEFAULT '',"
142 " rvalue BYTEA NOT NULL DEFAULT '',"
143 " record_count INTEGER NOT NULL DEFAULT 0,"
144 " record_data BYTEA NOT NULL DEFAULT '',"
145 " label TEXT NOT NULL DEFAULT ''"
148 if ( (NULL == res) ||
149 ((PQresultStatus (res) != PGRES_COMMAND_OK) &&
150 (0 != strcmp ("42P07", /* duplicate table */
153 PG_DIAG_SQLSTATE)))))
155 (void) GNUNET_POSTGRES_check_result (plugin->dbh, res,
156 PGRES_COMMAND_OK, "CREATE TABLE",
158 PQfinish (plugin->dbh);
160 return GNUNET_SYSERR;
165 GNUNET_CONFIGURATION_get_value_yesno (plugin->cfg,
166 "namestore-postgres",
171 "CREATE TEMPORARY TABLE ns096blocks ("
172 " query BYTEA NOT NULL DEFAULT '',"
173 " block BYTEA NOT NULL DEFAULT '',"
174 " expiration_time BIGINT NOT NULL DEFAULT 0"
181 "CREATE TABLE ns096blocks ("
182 " query BYTEA NOT NULL DEFAULT '',"
183 " block BYTEA NOT NULL DEFAULT '',"
184 " expiration_time BIGINT NOT NULL DEFAULT 0"
187 if ( (NULL == res) ||
188 ((PQresultStatus (res) != PGRES_COMMAND_OK) &&
189 (0 != strcmp ("42P07", /* duplicate table */
192 PG_DIAG_SQLSTATE)))))
194 (void) GNUNET_POSTGRES_check_result (plugin->dbh, res,
195 PGRES_COMMAND_OK, "CREATE TABLE",
197 PQfinish (plugin->dbh);
199 return GNUNET_SYSERR;
201 if (PQresultStatus (res) == PGRES_COMMAND_OK)
202 create_indices (plugin->dbh);
206 GNUNET_POSTGRES_prepare (plugin->dbh,
208 "INSERT INTO ns096blocks (query, block, expiration_time) VALUES "
209 "($1, $2, $3)", 3)) ||
211 GNUNET_POSTGRES_prepare (plugin->dbh,
213 "DELETE FROM ns096blocks WHERE expiration_time<$1", 1)) ||
215 GNUNET_POSTGRES_prepare (plugin->dbh,
217 "DELETE FROM ns096blocks WHERE query=$1 AND expiration_time<=$2", 2)) ||
219 GNUNET_POSTGRES_prepare (plugin->dbh,
221 "SELECT block FROM ns096blocks WHERE query=$1"
222 " ORDER BY expiration_time DESC LIMIT 1", 1)) ||
224 GNUNET_POSTGRES_prepare (plugin->dbh,
226 "INSERT INTO ns097records (zone_private_key, pkey, rvalue, record_count, record_data, label) VALUES "
227 "($1, $2, $3, $4, $5, $6)", 6)) ||
229 GNUNET_POSTGRES_prepare (plugin->dbh,
231 "DELETE FROM ns097records WHERE zone_private_key=$1 AND label=$2", 2)) ||
233 GNUNET_POSTGRES_prepare (plugin->dbh,
235 "SELECT record_count,record_data,label FROM ns097records"
236 " WHERE zone_private_key=$1 AND pkey=$2", 2)) ||
238 GNUNET_POSTGRES_prepare (plugin->dbh,
240 "SELECT record_count, record_data, label FROM ns097records"
241 " WHERE zone_private_key=$1 ORDER BY rvalue LIMIT 1 OFFSET $2", 2)) ||
243 GNUNET_POSTGRES_prepare (plugin->dbh,
245 "SELECT record_count,record_data,label,zone_private_key"
246 " FROM ns097records ORDER BY rvalue LIMIT 1 OFFSET $1", 1)))
248 PQfinish (plugin->dbh);
250 return GNUNET_SYSERR;
257 * Removes any expired block.
259 * @param plugin the plugin
262 namestore_postgres_expire_blocks (struct Plugin *plugin)
264 struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get ();
265 struct GNUNET_TIME_AbsoluteNBO now_be = GNUNET_TIME_absolute_hton (now);
266 const char *paramValues[] = {
267 (const char *) &now_be
269 int paramLengths[] = {
272 const int paramFormats[] = { 1 };
276 PQexecPrepared (plugin->dbh, "expire_blocks", 1,
277 paramValues, paramLengths, paramFormats, 1);
279 GNUNET_POSTGRES_check_result (plugin->dbh,
290 * Delete older block in the datastore.
293 * @param query query for the block
294 * @param expiration time how old does the block have to be for deletion
295 * @return #GNUNET_OK on success, else #GNUNET_SYSERR
298 delete_old_block (struct Plugin *plugin,
299 const struct GNUNET_HashCode *query,
300 struct GNUNET_TIME_AbsoluteNBO expiration_time)
302 const char *paramValues[] = {
303 (const char *) query,
304 (const char *) &expiration_time
306 int paramLengths[] = {
308 sizeof (expiration_time)
310 const int paramFormats[] = { 1, 1 };
314 PQexecPrepared (plugin->dbh, "delete_block", 2,
315 paramValues, paramLengths, paramFormats, 1);
317 GNUNET_POSTGRES_check_result (plugin->dbh,
328 * Cache a block in the datastore.
330 * @param cls closure (internal context for the plugin)
331 * @param block block to cache
332 * @return #GNUNET_OK on success, else #GNUNET_SYSERR
335 namestore_postgres_cache_block (void *cls,
336 const struct GNUNET_NAMESTORE_Block *block)
338 struct Plugin *plugin = cls;
339 struct GNUNET_HashCode query;
340 size_t block_size = ntohl (block->purpose.size) +
341 sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey) +
342 sizeof (struct GNUNET_CRYPTO_EcdsaSignature);
343 const char *paramValues[] = {
344 (const char *) &query,
345 (const char *) block,
346 (const char *) &block->expiration_time
348 int paramLengths[] = {
351 sizeof (block->expiration_time)
353 const int paramFormats[] = { 1, 1, 1 };
356 namestore_postgres_expire_blocks (plugin);
357 GNUNET_CRYPTO_hash (&block->derived_key,
358 sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey),
360 if (block_size > 64 * 65536)
363 return GNUNET_SYSERR;
365 delete_old_block (plugin, &query, block->expiration_time);
368 PQexecPrepared (plugin->dbh, "cache_block", 3,
369 paramValues, paramLengths, paramFormats, 1);
371 GNUNET_POSTGRES_check_result (plugin->dbh,
376 return GNUNET_SYSERR;
383 * Get the block for a particular zone and label in the
384 * datastore. Will return at most one result to the iterator.
386 * @param cls closure (internal context for the plugin)
387 * @param query hash of public key derived from the zone and the label
388 * @param iter function to call with the result
389 * @param iter_cls closure for @a iter
390 * @return #GNUNET_OK on success, #GNUNET_NO if there were no results, #GNUNET_SYSERR on error
393 namestore_postgres_lookup_block (void *cls,
394 const struct GNUNET_HashCode *query,
395 GNUNET_NAMESTORE_BlockCallback iter, void *iter_cls)
397 struct Plugin *plugin = cls;
398 const char *paramValues[] = {
401 int paramLengths[] = {
404 const int paramFormats[] = { 1 };
408 const struct GNUNET_NAMESTORE_Block *block;
410 res = PQexecPrepared (plugin->dbh,
412 paramValues, paramLengths, paramFormats,
415 GNUNET_POSTGRES_check_result (plugin->dbh, res, PGRES_TUPLES_OK,
419 LOG (GNUNET_ERROR_TYPE_DEBUG,
420 "Failing lookup (postgres error)\n");
421 return GNUNET_SYSERR;
423 if (0 == (cnt = PQntuples (res)))
426 LOG (GNUNET_ERROR_TYPE_DEBUG,
427 "Ending iteration (no more results)\n");
431 GNUNET_assert (1 == cnt);
432 GNUNET_assert (1 != PQnfields (res));
433 bsize = PQgetlength (res, 0, 0);
434 block = (const struct GNUNET_NAMESTORE_Block *) PQgetvalue (res, 0, 0);
435 if ( (bsize < sizeof (*block)) ||
436 (bsize != ntohl (block->purpose.size) +
437 sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey) +
438 sizeof (struct GNUNET_CRYPTO_EcdsaSignature)) )
441 LOG (GNUNET_ERROR_TYPE_DEBUG,
442 "Failing lookup (corrupt block)\n");
444 return GNUNET_SYSERR;
446 iter (iter_cls, block);
453 * Store a record in the datastore. Removes any existing record in the
454 * same zone with the same name.
456 * @param cls closure (internal context for the plugin)
457 * @param zone_key private key of the zone
458 * @param label name that is being mapped (at most 255 characters long)
459 * @param rd_count number of entries in @a rd array
460 * @param rd array of records with data to store
461 * @return #GNUNET_OK on success, else #GNUNET_SYSERR
464 namestore_postgres_store_records (void *cls,
465 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone_key,
467 unsigned int rd_count,
468 const struct GNUNET_NAMESTORE_RecordData *rd)
470 struct Plugin *plugin = cls;
471 struct GNUNET_CRYPTO_EcdsaPublicKey pkey;
473 uint32_t rd_count_nbo = htonl ((uint32_t) rd_count);
477 memset (&pkey, 0, sizeof (pkey));
478 for (i=0;i<rd_count;i++)
479 if (GNUNET_NAMESTORE_TYPE_PKEY == rd[i].record_type)
481 GNUNET_break (sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey) == rd[i].data_size);
487 rvalue = GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK, UINT64_MAX);
488 data_size = GNUNET_NAMESTORE_records_get_size (rd_count, rd);
489 if (data_size > 64 * 65536)
492 return GNUNET_SYSERR;
495 char data[data_size];
496 const char *paramValues[] = {
497 (const char *) zone_key,
498 (const char *) &pkey,
499 (const char *) &rvalue,
500 (const char *) &rd_count_nbo,
504 int paramLengths[] = {
508 sizeof (rd_count_nbo),
512 const int paramFormats[] = { 1, 1, 1, 1, 1, 1 };
515 if (data_size != GNUNET_NAMESTORE_records_serialize (rd_count, rd,
519 return GNUNET_SYSERR;
523 PQexecPrepared (plugin->dbh, "store_records", 6,
524 paramValues, paramLengths, paramFormats, 1);
526 GNUNET_POSTGRES_check_result (plugin->dbh,
531 return GNUNET_SYSERR;
539 * A statement has been run. We should evaluate the result, and if possible
540 * call the given @a iter with the result.
542 * @param plugin plugin context
543 * @param res result from the statement that was run (to be cleaned up)
544 * @param zone_key private key of the zone, could be NULL, in which case we should
545 * get the zone from @a res
546 * @param iter iterator to call with the result
547 * @param iter_cls closure for @a iter
548 * @return #GNUNET_OK on success, #GNUNET_NO if there were no results, #GNUNET_SYSERR on error
551 get_record_and_call_iterator (struct Plugin *plugin,
553 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone_key,
554 GNUNET_NAMESTORE_RecordIterator iter, void *iter_cls)
558 uint32_t record_count;
564 GNUNET_POSTGRES_check_result (plugin->dbh, res, PGRES_TUPLES_OK,
568 LOG (GNUNET_ERROR_TYPE_DEBUG,
569 "Failing lookup (postgres error)\n");
570 return GNUNET_SYSERR;
572 if (0 == (cnt = PQntuples (res)))
575 LOG (GNUNET_ERROR_TYPE_DEBUG,
576 "Ending iteration (no more results)\n");
580 GNUNET_assert (1 == cnt);
581 GNUNET_assert (3 + ((NULL == zone_key) ? 1 : 0) == PQnfields (res));
582 if (NULL == zone_key)
584 if (sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey) != PQgetlength (res, 0, 3))
588 return GNUNET_SYSERR;
590 zone_key = (const struct GNUNET_CRYPTO_EcdsaPrivateKey *) PQgetvalue (res, 0, 3);
592 if (sizeof (uint32_t) != PQfsize (res, 0))
596 return GNUNET_SYSERR;
599 record_count = ntohl (*(uint32_t *) PQgetvalue (res, 0, 0));
600 data = PQgetvalue (res, 0, 1);
601 data_size = PQgetlength (res, 0, 1);
602 label = PQgetvalue (res, 0, 2);
603 label_len = PQgetlength (res, 0, 1);
604 if (record_count > 64 * 1024)
606 /* sanity check, don't stack allocate far too much just
607 because database might contain a large value here */
610 return GNUNET_SYSERR;
613 struct GNUNET_NAMESTORE_RecordData rd[record_count];
614 char buf[label_len + 1];
616 memcpy (buf, label, label_len);
617 buf[label_len] = '\0';
619 GNUNET_NAMESTORE_records_deserialize (data_size, data,
624 return GNUNET_SYSERR;
626 iter (iter_cls, zone_key, buf, record_count, rd);
634 * Iterate over the results for a particular key and zone in the
635 * datastore. Will return at most one result to the iterator.
637 * @param cls closure (internal context for the plugin)
638 * @param zone hash of public key of the zone, NULL to iterate over all zones
639 * @param offset offset in the list of all matching records
640 * @param iter function to call with the result
641 * @param iter_cls closure for @a iter
642 * @return #GNUNET_OK on success, #GNUNET_NO if there were no results, #GNUNET_SYSERR on error
645 namestore_postgres_iterate_records (void *cls,
646 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
648 GNUNET_NAMESTORE_RecordIterator iter, void *iter_cls)
650 struct Plugin *plugin = cls;
651 uint64_t offset_be = GNUNET_htonll (offset);
655 const char *paramValues[] = {
656 (const char *) &offset_be
658 int paramLengths[] = {
661 const int paramFormats[] = { 1 };
664 res = PQexecPrepared (plugin->dbh,
665 "iterate_all_zones", 1,
666 paramValues, paramLengths, paramFormats,
668 return get_record_and_call_iterator (plugin,
675 const char *paramValues[] = {
677 (const char *) &offset_be
679 int paramLengths[] = {
683 const int paramFormats[] = { 1, 1 };
686 res = PQexecPrepared (plugin->dbh,
688 paramValues, paramLengths, paramFormats,
690 return get_record_and_call_iterator (plugin,
699 * Look for an existing PKEY delegation record for a given public key.
700 * Returns at most one result to the iterator.
702 * @param cls closure (internal context for the plugin)
703 * @param zone private key of the zone to look up in, never NULL
704 * @param value_zone public key of the target zone (value), never NULL
705 * @param iter function to call with the result
706 * @param iter_cls closure for @a iter
707 * @return #GNUNET_OK on success, #GNUNET_NO if there were no results, #GNUNET_SYSERR on error
710 namestore_postgres_zone_to_name (void *cls,
711 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
712 const struct GNUNET_CRYPTO_EcdsaPublicKey *value_zone,
713 GNUNET_NAMESTORE_RecordIterator iter, void *iter_cls)
715 struct Plugin *plugin = cls;
716 const char *paramValues[] = {
718 (const char *) value_zone
720 int paramLengths[] = {
724 const int paramFormats[] = { 1, 1 };
727 res = PQexecPrepared (plugin->dbh,
729 paramValues, paramLengths, paramFormats,
731 return get_record_and_call_iterator (plugin,
739 * Shutdown database connection and associate data
742 * @param plugin the plugin context (state for this module)
745 database_shutdown (struct Plugin *plugin)
747 PQfinish (plugin->dbh);
753 * Entry point for the plugin.
755 * @param cls the "struct GNUNET_NAMESTORE_PluginEnvironment*"
756 * @return NULL on error, othrewise the plugin context
759 libgnunet_plugin_namestore_postgres_init (void *cls)
761 static struct Plugin plugin;
762 const struct GNUNET_CONFIGURATION_Handle *cfg = cls;
763 struct GNUNET_NAMESTORE_PluginFunctions *api;
765 if (NULL != plugin.cfg)
766 return NULL; /* can only initialize once! */
767 memset (&plugin, 0, sizeof (struct Plugin));
769 if (GNUNET_OK != database_setup (&plugin))
771 database_shutdown (&plugin);
774 api = GNUNET_new (struct GNUNET_NAMESTORE_PluginFunctions);
776 api->cache_block = &namestore_postgres_cache_block;
777 api->lookup_block = &namestore_postgres_lookup_block;
778 api->store_records = &namestore_postgres_store_records;
779 api->iterate_records = &namestore_postgres_iterate_records;
780 api->zone_to_name = &namestore_postgres_zone_to_name;
781 LOG (GNUNET_ERROR_TYPE_INFO,
782 _("Postgres database running\n"));
788 * Exit point from the plugin.
790 * @param cls the plugin context (as returned by "init")
791 * @return always NULL
794 libgnunet_plugin_namestore_postgres_done (void *cls)
796 struct GNUNET_NAMESTORE_PluginFunctions *api = cls;
797 struct Plugin *plugin = api->cls;
799 database_shutdown (plugin);
802 LOG (GNUNET_ERROR_TYPE_DEBUG,
803 "postgres plugin is finished\n");
807 /* end of plugin_namestore_postgres.c */