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
28 #include "gnunet_namestore_plugin.h"
29 #include "gnunet_namestore_service.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, "CREATE INDEX ir_zone_name_rv ON ns091records (zone_hash,record_name_hash,rvalue)")) ||
85 GNUNET_POSTGRES_exec (dbh, "CREATE INDEX ir_zone_delegation ON ns091records (zone_hash,zone_delegation)")) ||
87 GNUNET_POSTGRES_exec (dbh, "CREATE INDEX ir_zone_rv ON ns091records (zone_hash,rvalue)")) ||
89 GNUNET_POSTGRES_exec (dbh, "CREATE INDEX ir_zone ON ns091records (zone_hash)")) ||
91 GNUNET_POSTGRES_exec (dbh, "CREATE INDEX ir_name_rv ON ns091records (record_name_hash,rvalue)")) ||
93 GNUNET_POSTGRES_exec (dbh, "CREATE INDEX ir_rv ON ns091records (rvalue)")) )
94 LOG (GNUNET_ERROR_TYPE_ERROR,
95 _("Failed to create indices\n"));
100 * Initialize the database connections and associated
101 * data structures (create tables and indices
102 * as needed as well).
104 * @param plugin the plugin context (state for this module)
105 * @return GNUNET_OK on success
108 database_setup (struct Plugin *plugin)
112 plugin->dbh = GNUNET_POSTGRES_connect (plugin->cfg,
113 "namestore-postgres");
114 if (NULL == plugin->dbh)
115 return GNUNET_SYSERR;
117 GNUNET_CONFIGURATION_get_value_yesno (plugin->cfg,
118 "namestore-postgres",
123 "CREATE TEMPORARY TABLE ns091records ("
124 " zone_key BYTEA NOT NULL DEFAULT '',"
125 " zone_delegation BYTEA NOT NULL DEFAULT '',"
126 " zone_hash BYTEA NOT NULL DEFAULT '',"
127 " record_count INTEGER NOT NULL DEFAULT 0,"
128 " record_data BYTEA NOT NULL DEFAULT '',"
129 " block_expiration_time BIGINT NOT NULL DEFAULT 0,"
130 " signature BYTEA NOT NULL DEFAULT '',"
131 " record_name TEXT NOT NULL DEFAULT '',"
132 " record_name_hash BYTEA NOT NULL DEFAULT '',"
133 " rvalue BIGINT NOT NULL DEFAULT 0"
139 "CREATE TABLE ns091records ("
140 " zone_key BYTEA NOT NULL DEFAULT '',"
141 " zone_delegation BYTEA NOT NULL DEFAULT '',"
142 " zone_hash BYTEA NOT NULL DEFAULT '',"
143 " record_count INTEGER NOT NULL DEFAULT 0,"
144 " record_data BYTEA NOT NULL DEFAULT '',"
145 " block_expiration_time BIGINT NOT NULL DEFAULT 0,"
146 " signature BYTEA NOT NULL DEFAULT '',"
147 " record_name TEXT NOT NULL DEFAULT '',"
148 " record_name_hash BYTEA NOT NULL DEFAULT '',"
149 " rvalue BIGINT NOT NULL DEFAULT 0"
152 if ((NULL == res) || ((PQresultStatus (res) != PGRES_COMMAND_OK) && (0 != strcmp ("42P07", /* duplicate table */
155 PG_DIAG_SQLSTATE)))))
157 (void) GNUNET_POSTGRES_check_result (plugin->dbh, res, PGRES_COMMAND_OK, "CREATE TABLE",
159 PQfinish (plugin->dbh);
161 return GNUNET_SYSERR;
163 if (PQresultStatus (res) == PGRES_COMMAND_OK)
164 create_indices (plugin->dbh);
168 GNUNET_POSTGRES_prepare (plugin->dbh,
170 "INSERT INTO ns091records (zone_key, record_name, record_count, record_data, block_expiration_time, signature"
171 ", zone_delegation, zone_hash, record_name_hash, rvalue) VALUES "
172 "($1, $2, $3, $4, $5, $6, $7, $8, $9, $10)", 10)) ||
174 GNUNET_POSTGRES_prepare (plugin->dbh,
176 "DELETE FROM ns091records WHERE zone_hash=$1 AND record_name_hash=$2", 2)) ||
178 GNUNET_POSTGRES_prepare (plugin->dbh,
180 "SELECT zone_key, record_name, record_count, record_data, block_expiration_time, signature"
181 " FROM ns091records WHERE zone_hash=$1 AND record_name_hash=$2 ORDER BY rvalue LIMIT 1 OFFSET $3", 3)) ||
183 GNUNET_POSTGRES_prepare (plugin->dbh,
185 "SELECT zone_key, record_name, record_count, record_data, block_expiration_time, signature"
186 " FROM ns091records WHERE zone_hash=$1 ORDER BY rvalue LIMIT 1 OFFSET $2", 2)) ||
188 GNUNET_POSTGRES_prepare (plugin->dbh,
190 "SELECT zone_key, record_name, record_count, record_data, block_expiration_time, signature"
191 " FROM ns091records WHERE record_name_hash=$1 ORDER BY rvalue LIMIT 1 OFFSET $2", 2)) ||
193 GNUNET_POSTGRES_prepare (plugin->dbh,
195 "SELECT zone_key, record_name, record_count, record_data, block_expiration_time, signature"
196 " FROM ns091records ORDER BY rvalue LIMIT 1 OFFSET $1", 1)) ||
198 GNUNET_POSTGRES_prepare (plugin->dbh,
200 "SELECT zone_key, record_name, record_count, record_data, block_expiration_time, signature"
201 " FROM ns091records WHERE zone_hash=$1 AND zone_delegation=$2", 2)) ||
203 GNUNET_POSTGRES_prepare (plugin->dbh,
205 "DELETE FROM ns091records WHERE zone_hash=$1", 1)))
207 PQfinish (plugin->dbh);
209 return GNUNET_SYSERR;
216 * Removes any existing record in the given zone with the same name.
218 * @param cls closure (internal context for the plugin)
219 * @param zone hash of the public key of the zone
220 * @param name name to remove (at most 255 characters long)
221 * @return GNUNET_OK on success
224 namestore_postgres_remove_records (void *cls,
225 const struct GNUNET_CRYPTO_ShortHashCode *zone,
228 struct Plugin *plugin = cls;
230 struct GNUNET_CRYPTO_ShortHashCode nh;
231 const char *paramValues[] = {
235 int paramLengths[] = {
236 sizeof (struct GNUNET_CRYPTO_ShortHashCode),
237 sizeof (struct GNUNET_CRYPTO_ShortHashCode)
239 const int paramFormats[] = { 1, 1 };
242 name_len = strlen (name);
243 GNUNET_CRYPTO_short_hash (name, name_len, &nh);
245 PQexecPrepared (plugin->dbh, "remove_records", 2, paramValues, paramLengths,
248 GNUNET_POSTGRES_check_result (plugin->dbh, ret, PGRES_COMMAND_OK, "PQexecPrepared", "remove_records"))
249 return GNUNET_SYSERR;
256 * Store a record in the datastore. Removes any existing record in the
257 * same zone with the same name.
259 * @param cls closure (internal context for the plugin)
260 * @param zone_key public key of the zone
261 * @param expire when does the corresponding block in the DHT expire (until
262 * when should we never do a DHT lookup for the same name again)?
263 * @param name name that is being mapped (at most 255 characters long)
264 * @param rd_count number of entries in 'rd' array
265 * @param rd array of records with data to store
266 * @param signature signature of the record block, NULL if signature is unavailable (i.e.
267 * because the user queried for a particular record type only)
268 * @return GNUNET_OK on success, else GNUNET_SYSERR
271 namestore_postgres_put_records (void *cls,
272 const struct GNUNET_CRYPTO_EccPublicKey *zone_key,
273 struct GNUNET_TIME_Absolute expire,
275 unsigned int rd_count,
276 const struct GNUNET_NAMESTORE_RecordData *rd,
277 const struct GNUNET_CRYPTO_EccSignature *signature)
279 struct Plugin *plugin = cls;
281 struct GNUNET_CRYPTO_ShortHashCode zone;
282 struct GNUNET_CRYPTO_ShortHashCode zone_delegation;
283 struct GNUNET_CRYPTO_ShortHashCode nh;
289 GNUNET_CRYPTO_short_hash (zone_key,
290 sizeof (struct GNUNET_CRYPTO_EccPublicKey),
292 (void) namestore_postgres_remove_records (plugin, &zone, name);
293 name_len = strlen (name);
294 GNUNET_CRYPTO_short_hash (name, name_len, &nh);
295 memset (&zone_delegation, 0, sizeof (zone_delegation));
296 for (i=0;i<rd_count;i++)
297 if (rd[i].record_type == GNUNET_NAMESTORE_TYPE_PKEY)
299 GNUNET_assert (sizeof (struct GNUNET_CRYPTO_ShortHashCode) == rd[i].data_size);
300 memcpy (&zone_delegation,
302 sizeof (struct GNUNET_CRYPTO_ShortHashCode));
305 rvalue = GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK, UINT64_MAX);
306 data_size = GNUNET_NAMESTORE_records_get_size (rd_count, rd);
307 if (data_size > 64 * 65536)
310 return GNUNET_SYSERR;
313 char data[data_size];
314 uint64_t expire_be = GNUNET_htonll (expire.abs_value_us);
315 uint64_t rvalue_be = GNUNET_htonll (rvalue);
316 uint32_t rd_count_be = htonl ((uint32_t) rd_count);
317 const char *paramValues[] = {
318 (const char *) zone_key,
320 (const char *) &rd_count_be,
322 (const char *) &expire_be,
323 (const char *) signature,
324 (const char *) &zone_delegation,
325 (const char *) &zone,
327 (const char *) &rvalue_be
329 int paramLengths[] = {
330 sizeof (struct GNUNET_CRYPTO_EccPublicKey),
335 sizeof (struct GNUNET_CRYPTO_EccSignature),
336 sizeof (struct GNUNET_CRYPTO_ShortHashCode),
337 sizeof (struct GNUNET_CRYPTO_ShortHashCode),
338 sizeof (struct GNUNET_CRYPTO_ShortHashCode),
341 const int paramFormats[] = { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 };
343 if (data_size != GNUNET_NAMESTORE_records_serialize (rd_count, rd,
347 return GNUNET_SYSERR;
350 PQexecPrepared (plugin->dbh, "put_records", 10, paramValues, paramLengths,
353 GNUNET_POSTGRES_check_result (plugin->dbh, ret, PGRES_COMMAND_OK, "PQexecPrepared", "put_records"))
354 return GNUNET_SYSERR;
362 * The given 'postgres' result was obtained from the database.
363 * Parse the record and give it to the iterator.
365 * @param plugin plugin context
366 * @param stmt_name name of the prepared statement that was executed
367 * @param res result from postgres to interpret (and then clean up)
368 * @param iter iterator to call with the result
369 * @param iter_cls closure for 'iter'
370 * @return GNUNET_OK on success, GNUNET_NO if there were no results, GNUNET_SYSERR on error
371 * 'iter' will have been called unless the return value is 'GNUNET_SYSERR'
374 get_record_and_call_iterator (struct Plugin *plugin,
375 const char *stmt_name,
377 GNUNET_NAMESTORE_RecordIterator iter, void *iter_cls)
379 unsigned int record_count;
381 const struct GNUNET_CRYPTO_EccPublicKey *zone_key;
382 const struct GNUNET_CRYPTO_EccSignature *sig;
383 struct GNUNET_TIME_Absolute expiration;
390 GNUNET_POSTGRES_check_result (plugin->dbh, res, PGRES_TUPLES_OK, "PQexecPrepared",
393 LOG (GNUNET_ERROR_TYPE_DEBUG,
394 "Ending iteration (postgres error)\n");
395 return GNUNET_SYSERR;
398 if (0 == (cnt = PQntuples (res)))
401 LOG (GNUNET_ERROR_TYPE_DEBUG,
402 "Ending iteration (no more results)\n");
404 iter (iter_cls, NULL, GNUNET_TIME_UNIT_ZERO_ABS, NULL, 0, NULL, NULL);
407 GNUNET_assert (1 == cnt);
408 if ((6 != PQnfields (res)) ||
409 (sizeof (struct GNUNET_CRYPTO_EccPublicKey) != PQgetlength (res, 0, 0)) ||
410 (sizeof (uint32_t) != PQfsize (res, 2)) ||
411 (sizeof (uint64_t) != PQfsize (res, 4)) ||
412 (sizeof (struct GNUNET_CRYPTO_EccSignature) != PQgetlength (res, 0, 5)))
416 return GNUNET_SYSERR;
418 zone_key = (const struct GNUNET_CRYPTO_EccPublicKey *) PQgetvalue (res, 0, 0);
419 name = PQgetvalue (res, 0, 1);
420 name_len = PQgetlength (res, 0, 1);
421 record_count = ntohl (*(uint32_t *) PQgetvalue (res, 0, 2));
422 data_size = PQgetlength (res, 0, 3);
423 data = PQgetvalue (res, 0, 3);
424 expiration.abs_value_us =
425 GNUNET_ntohll (*(uint64_t *) PQgetvalue (res, 0, 4));
426 sig = (const struct GNUNET_CRYPTO_EccSignature*) PQgetvalue (res, 0, 5);
427 if (record_count > 64 * 1024)
429 /* sanity check, don't stack allocate far too much just
430 because database might contain a large value here */
433 return GNUNET_SYSERR;
436 struct GNUNET_NAMESTORE_RecordData rd[record_count];
437 char buf[name_len + 1];
439 memcpy (buf, name, name_len);
440 buf[name_len] = '\0';
442 GNUNET_NAMESTORE_records_deserialize (data_size, data,
447 return GNUNET_SYSERR;
449 iter (iter_cls, zone_key, expiration, buf,
450 record_count, rd, sig);
458 * Iterate over the results for a particular key and zone in the
459 * datastore. Will return at most one result to the iterator.
461 * @param cls closure (internal context for the plugin)
462 * @param zone hash of public key of the zone, NULL to iterate over all zones
463 * @param name name as string, NULL to iterate over all records of the zone
464 * @param offset offset in the list of all matching records
465 * @param iter function to call with the result
466 * @param iter_cls closure for iter
467 * @return GNUNET_OK on success, GNUNET_NO if there were no results, GNUNET_SYSERR on error
468 * 'iter' will have been called unless the return value is 'GNUNET_SYSERR'
471 namestore_postgres_iterate_records (void *cls,
472 const struct GNUNET_CRYPTO_ShortHashCode *zone,
475 GNUNET_NAMESTORE_RecordIterator iter, void *iter_cls)
477 struct Plugin *plugin = cls;
478 const char *stmt_name;
479 struct GNUNET_CRYPTO_ShortHashCode name_hase;
480 uint64_t offset_be = GNUNET_htonll (offset);
481 const char *paramValues[] = {
483 (const char *) &name_hase,
484 (const char *) &offset_be,
486 (const char *) &offset_be,
488 int paramLengths[] = {
489 sizeof (struct GNUNET_CRYPTO_ShortHashCode),
490 sizeof (struct GNUNET_CRYPTO_ShortHashCode),
492 sizeof (struct GNUNET_CRYPTO_ShortHashCode),
495 const int paramFormats[] = { 1, 1, 1, 1, 1 };
496 unsigned int num_params;
497 unsigned int first_param;
503 stmt_name = "iterate_all";
509 GNUNET_CRYPTO_short_hash (name, strlen(name), &name_hase);
510 stmt_name = "iterate_by_name";
517 stmt_name = "iterate_by_zone";
523 GNUNET_CRYPTO_short_hash (name, strlen(name), &name_hase);
524 stmt_name = "iterate_records";
529 PQexecPrepared (plugin->dbh, stmt_name, num_params,
530 ¶mValues[first_param],
531 ¶mLengths[first_param],
532 ¶mFormats[first_param], 1);
533 return get_record_and_call_iterator (plugin, stmt_name, res, iter, iter_cls);
538 * Look for an existing PKEY delegation record for a given public key.
539 * Returns at most one result to the iterator.
541 * @param cls closure (internal context for the plugin)
542 * @param zone hash of public key of the zone to look up in, never NULL
543 * @param value_zone hash of the public key of the target zone (value), never NULL
544 * @param iter function to call with the result
545 * @param iter_cls closure for iter
546 * @return GNUNET_OK on success, GNUNET_NO if there were no results, GNUNET_SYSERR on error
547 * 'iter' will have been called unless the return value is 'GNUNET_SYSERR'
550 namestore_postgres_zone_to_name (void *cls,
551 const struct GNUNET_CRYPTO_ShortHashCode *zone,
552 const struct GNUNET_CRYPTO_ShortHashCode *value_zone,
553 GNUNET_NAMESTORE_RecordIterator iter, void *iter_cls)
555 struct Plugin *plugin = cls;
556 const char *paramValues[] = {
558 (const char *) value_zone
560 int paramLengths[] = {
561 sizeof (struct GNUNET_CRYPTO_ShortHashCode),
562 sizeof (struct GNUNET_CRYPTO_ShortHashCode)
564 const int paramFormats[] = { 1, 1 };
568 PQexecPrepared (plugin->dbh, "zone_to_name", 2,
569 paramValues, paramLengths, paramFormats, 1);
570 return get_record_and_call_iterator (plugin, "zone_to_name", res, iter, iter_cls);
575 * Delete an entire zone (all records). Not used in normal operation.
577 * @param cls closure (internal context for the plugin)
578 * @param zone zone to delete
581 namestore_postgres_delete_zone (void *cls,
582 const struct GNUNET_CRYPTO_ShortHashCode *zone)
584 struct Plugin *plugin = cls;
586 const char *paramValues[] = {
589 int paramLengths[] = {
590 sizeof (struct GNUNET_CRYPTO_ShortHashCode)
592 const int paramFormats[] = { 1 };
595 PQexecPrepared (plugin->dbh, "delete_zone", 1, paramValues, paramLengths,
598 GNUNET_POSTGRES_check_result (plugin->dbh, ret, PGRES_COMMAND_OK, "PQexecPrepared", "delete_zone"))
600 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
601 "Deleting zone failed!\n");
609 * Shutdown database connection and associate data
612 * @param plugin the plugin context (state for this module)
615 database_shutdown (struct Plugin *plugin)
617 PQfinish (plugin->dbh);
623 * Entry point for the plugin.
625 * @param cls the "struct GNUNET_NAMESTORE_PluginEnvironment*"
626 * @return NULL on error, othrewise the plugin context
629 libgnunet_plugin_namestore_postgres_init (void *cls)
631 static struct Plugin plugin;
632 const struct GNUNET_CONFIGURATION_Handle *cfg = cls;
633 struct GNUNET_NAMESTORE_PluginFunctions *api;
635 if (NULL != plugin.cfg)
636 return NULL; /* can only initialize once! */
637 memset (&plugin, 0, sizeof (struct Plugin));
639 if (GNUNET_OK != database_setup (&plugin))
641 database_shutdown (&plugin);
644 api = GNUNET_malloc (sizeof (struct GNUNET_NAMESTORE_PluginFunctions));
646 api->put_records = &namestore_postgres_put_records;
647 api->remove_records = &namestore_postgres_remove_records;
648 api->iterate_records = &namestore_postgres_iterate_records;
649 api->zone_to_name = &namestore_postgres_zone_to_name;
650 api->delete_zone = &namestore_postgres_delete_zone;
651 LOG (GNUNET_ERROR_TYPE_INFO,
652 _("Postgres database running\n"));
658 * Exit point from the plugin.
660 * @param cls the plugin context (as returned by "init")
661 * @return always NULL
664 libgnunet_plugin_namestore_postgres_done (void *cls)
666 struct GNUNET_NAMESTORE_PluginFunctions *api = cls;
667 struct Plugin *plugin = api->cls;
669 database_shutdown (plugin);
672 LOG (GNUNET_ERROR_TYPE_DEBUG,
673 "postgres plugin is finished\n");
677 /* end of plugin_namestore_postgres.c */