2 * This file is part of GNUnet
3 * Copyright (C) 2009-2013, 2016 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_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 "gnunet_pq_lib.h"
32 #include "namestore.h"
36 * After how many ms "busy" should a DB operation fail for good?
37 * A low value makes sure that we are more responsive to requests
38 * (especially PUTs). A high value guarantees a higher success
39 * rate (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 succeed
43 * 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_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)
55 #define LOG(kind,...) GNUNET_log_from (kind, "namestore-postgres", __VA_ARGS__)
59 * Context for all functions in this plugin.
67 const struct GNUNET_CONFIGURATION_Handle *cfg;
70 * Native Postgres database handle.
78 * Create our database indices.
80 * @param dbh handle to the database
83 create_indices (PGconn * dbh)
87 GNUNET_POSTGRES_exec (dbh,
88 "CREATE INDEX ir_pkey_reverse ON ns097records (zone_private_key,pkey)")) ||
90 GNUNET_POSTGRES_exec (dbh,
91 "CREATE INDEX ir_pkey_iter ON ns097records (zone_private_key,rvalue)")) ||
93 GNUNET_POSTGRES_exec (dbh, "CREATE INDEX it_iter ON ns097records (rvalue)")) ||
95 GNUNET_POSTGRES_exec (dbh, "CREATE INDEX ir_label ON ns097records (label)")) )
96 LOG (GNUNET_ERROR_TYPE_ERROR,
97 _("Failed to create indices\n"));
102 * Initialize the database connections and associated
103 * data structures (create tables and indices
104 * as needed as well).
106 * @param plugin the plugin context (state for this module)
107 * @return #GNUNET_OK on success
110 database_setup (struct Plugin *plugin)
114 plugin->dbh = GNUNET_POSTGRES_connect (plugin->cfg,
115 "namestore-postgres");
116 if (NULL == plugin->dbh)
117 return GNUNET_SYSERR;
119 GNUNET_CONFIGURATION_get_value_yesno (plugin->cfg,
120 "namestore-postgres",
125 "CREATE TEMPORARY TABLE ns097records ("
126 " zone_private_key BYTEA NOT NULL DEFAULT '',"
127 " pkey BYTEA DEFAULT '',"
128 " rvalue BYTEA NOT NULL DEFAULT '',"
129 " record_count INTEGER NOT NULL DEFAULT 0,"
130 " record_data BYTEA NOT NULL DEFAULT '',"
131 " label TEXT NOT NULL DEFAULT ''"
138 "CREATE TABLE ns097records ("
139 " zone_private_key BYTEA NOT NULL DEFAULT '',"
140 " pkey BYTEA DEFAULT '',"
141 " rvalue BYTEA NOT NULL DEFAULT '',"
142 " record_count INTEGER NOT NULL DEFAULT 0,"
143 " record_data BYTEA NOT NULL DEFAULT '',"
144 " label TEXT NOT NULL DEFAULT ''"
147 if ( (NULL == res) ||
148 ((PQresultStatus (res) != PGRES_COMMAND_OK) &&
149 (0 != strcmp ("42P07", /* duplicate table */
152 PG_DIAG_SQLSTATE)))))
154 (void) GNUNET_POSTGRES_check_result (plugin->dbh, res,
155 PGRES_COMMAND_OK, "CREATE TABLE",
157 PQfinish (plugin->dbh);
159 return GNUNET_SYSERR;
161 create_indices (plugin->dbh);
164 GNUNET_POSTGRES_prepare (plugin->dbh,
166 "INSERT INTO ns097records (zone_private_key, pkey, rvalue, record_count, record_data, label) VALUES "
167 "($1, $2, $3, $4, $5, $6)", 6)) ||
169 GNUNET_POSTGRES_prepare (plugin->dbh,
171 "DELETE FROM ns097records WHERE zone_private_key=$1 AND label=$2", 2)) ||
173 GNUNET_POSTGRES_prepare (plugin->dbh,
175 "SELECT record_count,record_data,label FROM ns097records"
176 " WHERE zone_private_key=$1 AND pkey=$2", 2)) ||
178 GNUNET_POSTGRES_prepare (plugin->dbh,
180 "SELECT record_count,record_data,label FROM ns097records"
181 " WHERE zone_private_key=$1 ORDER BY rvalue LIMIT 1 OFFSET $2", 2)) ||
183 GNUNET_POSTGRES_prepare (plugin->dbh,
185 "SELECT record_count,record_data,label,zone_private_key"
186 " FROM ns097records ORDER BY rvalue LIMIT 1 OFFSET $1", 1)) ||
188 GNUNET_POSTGRES_prepare (plugin->dbh,
190 "SELECT record_count,record_data,label"
191 " FROM ns097records WHERE zone_private_key=$1 AND label=$2", 2)))
193 PQfinish (plugin->dbh);
195 return GNUNET_SYSERR;
202 * Store a record in the datastore. Removes any existing record in the
203 * same zone with the same name.
205 * @param cls closure (internal context for the plugin)
206 * @param zone_key private key of the zone
207 * @param label name that is being mapped (at most 255 characters long)
208 * @param rd_count number of entries in @a rd array
209 * @param rd array of records with data to store
210 * @return #GNUNET_OK on success, else #GNUNET_SYSERR
213 namestore_postgres_store_records (void *cls,
214 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone_key,
216 unsigned int rd_count,
217 const struct GNUNET_GNSRECORD_Data *rd)
219 struct Plugin *plugin = cls;
220 struct GNUNET_CRYPTO_EcdsaPublicKey pkey;
222 uint32_t rd_count_nbo = htonl ((uint32_t) rd_count);
226 memset (&pkey, 0, sizeof (pkey));
227 for (i=0;i<rd_count;i++)
228 if (GNUNET_GNSRECORD_TYPE_PKEY == rd[i].record_type)
230 GNUNET_break (sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey) == rd[i].data_size);
231 GNUNET_memcpy (&pkey,
236 rvalue = GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK, UINT64_MAX);
237 data_size = GNUNET_GNSRECORD_records_get_size (rd_count, rd);
238 if (data_size > 64 * 65536)
241 return GNUNET_SYSERR;
244 char data[data_size];
245 // FIXME: use libgnunetpq!
246 const char *paramValues[] = {
247 (const char *) zone_key,
248 (const char *) &pkey,
249 (const char *) &rvalue,
250 (const char *) &rd_count_nbo,
254 int paramLengths[] = {
258 sizeof (rd_count_nbo),
262 const int paramFormats[] = { 1, 1, 1, 1, 1, 1 };
265 if (data_size != GNUNET_GNSRECORD_records_serialize (rd_count, rd,
269 return GNUNET_SYSERR;
273 PQexecPrepared (plugin->dbh, "store_records", 6,
274 paramValues, paramLengths, paramFormats, 1);
276 GNUNET_POSTGRES_check_result (plugin->dbh,
281 return GNUNET_SYSERR;
289 * A statement has been run. We should evaluate the result, and if possible
290 * call the given @a iter with the result.
292 * @param plugin plugin context
293 * @param res result from the statement that was run (to be cleaned up)
294 * @param zone_key private key of the zone, could be NULL, in which case we should
295 * get the zone from @a res
296 * @param iter iterator to call with the result
297 * @param iter_cls closure for @a iter
298 * @return #GNUNET_OK on success, #GNUNET_NO if there were no results, #GNUNET_SYSERR on error
301 get_record_and_call_iterator (struct Plugin *plugin,
303 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone_key,
304 GNUNET_NAMESTORE_RecordIterator iter, void *iter_cls)
308 uint32_t record_count;
314 GNUNET_POSTGRES_check_result (plugin->dbh, res, PGRES_TUPLES_OK,
318 LOG (GNUNET_ERROR_TYPE_DEBUG,
319 "Failing lookup (postgres error)\n");
320 return GNUNET_SYSERR;
322 if (0 == (cnt = PQntuples (res)))
325 LOG (GNUNET_ERROR_TYPE_DEBUG,
326 "Ending iteration (no more results)\n");
330 GNUNET_assert (1 == cnt);
331 GNUNET_assert (3 + ((NULL == zone_key) ? 1 : 0) == PQnfields (res));
332 if (NULL == zone_key)
334 if (sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey) != PQgetlength (res, 0, 3))
338 return GNUNET_SYSERR;
340 zone_key = (const struct GNUNET_CRYPTO_EcdsaPrivateKey *) PQgetvalue (res, 0, 3);
342 if (sizeof (uint32_t) != PQfsize (res, 0))
346 return GNUNET_SYSERR;
349 record_count = ntohl (*(uint32_t *) PQgetvalue (res, 0, 0));
350 data = PQgetvalue (res, 0, 1);
351 data_size = PQgetlength (res, 0, 1);
352 label = PQgetvalue (res, 0, 2);
353 label_len = PQgetlength (res, 0, 1);
354 if (record_count > 64 * 1024)
356 /* sanity check, don't stack allocate far too much just
357 because database might contain a large value here */
360 return GNUNET_SYSERR;
363 struct GNUNET_GNSRECORD_Data rd[record_count];
364 char buf[label_len + 1];
366 GNUNET_memcpy (buf, label, label_len);
367 buf[label_len] = '\0';
369 GNUNET_GNSRECORD_records_deserialize (data_size, data,
374 return GNUNET_SYSERR;
376 iter (iter_cls, zone_key, buf, record_count, rd);
384 * Lookup records in the datastore for which we are the authority.
386 * @param cls closure (internal context for the plugin)
387 * @param zone private key of the zone
388 * @param label name of the record in the zone
389 * @param iter function to call with the result
390 * @param iter_cls closure for @a iter
391 * @return #GNUNET_OK on success, else #GNUNET_SYSERR
394 namestore_postgres_lookup_records (void *cls,
395 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
397 GNUNET_NAMESTORE_RecordIterator iter,
400 struct Plugin *plugin = cls;
401 const char *paramValues[] = {
405 int paramLengths[] = {
409 const int paramFormats[] = { 1, 1 };
412 res = PQexecPrepared (plugin->dbh,
414 paramValues, paramLengths, paramFormats,
416 return get_record_and_call_iterator (plugin,
424 * Iterate over the results for a particular key and zone in the
425 * datastore. Will return at most one result to the iterator.
427 * @param cls closure (internal context for the plugin)
428 * @param zone hash of public key of the zone, NULL to iterate over all zones
429 * @param offset offset in the list of all matching records
430 * @param iter function to call with the result
431 * @param iter_cls closure for @a iter
432 * @return #GNUNET_OK on success, #GNUNET_NO if there were no results, #GNUNET_SYSERR on error
435 namestore_postgres_iterate_records (void *cls,
436 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
438 GNUNET_NAMESTORE_RecordIterator iter, void *iter_cls)
440 struct Plugin *plugin = cls;
441 uint64_t offset_be = GNUNET_htonll (offset);
445 const char *paramValues[] = {
446 (const char *) &offset_be
448 int paramLengths[] = {
451 const int paramFormats[] = { 1 };
454 res = PQexecPrepared (plugin->dbh,
455 "iterate_all_zones", 1,
456 paramValues, paramLengths, paramFormats,
458 return get_record_and_call_iterator (plugin,
465 const char *paramValues[] = {
467 (const char *) &offset_be
469 int paramLengths[] = {
473 const int paramFormats[] = { 1, 1 };
476 res = PQexecPrepared (plugin->dbh,
478 paramValues, paramLengths, paramFormats,
480 return get_record_and_call_iterator (plugin,
489 * Look for an existing PKEY delegation record for a given public key.
490 * Returns at most one result to the iterator.
492 * @param cls closure (internal context for the plugin)
493 * @param zone private key of the zone to look up in, never NULL
494 * @param value_zone public key of the target zone (value), never NULL
495 * @param iter function to call with the result
496 * @param iter_cls closure for @a iter
497 * @return #GNUNET_OK on success, #GNUNET_NO if there were no results, #GNUNET_SYSERR on error
500 namestore_postgres_zone_to_name (void *cls,
501 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
502 const struct GNUNET_CRYPTO_EcdsaPublicKey *value_zone,
503 GNUNET_NAMESTORE_RecordIterator iter, void *iter_cls)
505 struct Plugin *plugin = cls;
506 const char *paramValues[] = {
508 (const char *) value_zone
510 int paramLengths[] = {
514 const int paramFormats[] = { 1, 1 };
517 res = PQexecPrepared (plugin->dbh,
519 paramValues, paramLengths, paramFormats,
521 return get_record_and_call_iterator (plugin,
529 * Shutdown database connection and associate data
532 * @param plugin the plugin context (state for this module)
535 database_shutdown (struct Plugin *plugin)
537 PQfinish (plugin->dbh);
543 * Entry point for the plugin.
545 * @param cls the `struct GNUNET_NAMESTORE_PluginEnvironment*`
546 * @return NULL on error, othrewise the plugin context
549 libgnunet_plugin_namestore_postgres_init (void *cls)
551 static struct Plugin plugin;
552 const struct GNUNET_CONFIGURATION_Handle *cfg = cls;
553 struct GNUNET_NAMESTORE_PluginFunctions *api;
555 if (NULL != plugin.cfg)
556 return NULL; /* can only initialize once! */
557 memset (&plugin, 0, sizeof (struct Plugin));
559 if (GNUNET_OK != database_setup (&plugin))
561 database_shutdown (&plugin);
564 api = GNUNET_new (struct GNUNET_NAMESTORE_PluginFunctions);
566 api->store_records = &namestore_postgres_store_records;
567 api->iterate_records = &namestore_postgres_iterate_records;
568 api->zone_to_name = &namestore_postgres_zone_to_name;
569 api->lookup_records = &namestore_postgres_lookup_records;
570 LOG (GNUNET_ERROR_TYPE_INFO,
571 _("Postgres database running\n"));
577 * Exit point from the plugin.
579 * @param cls the plugin context (as returned by "init")
580 * @return always NULL
583 libgnunet_plugin_namestore_postgres_done (void *cls)
585 struct GNUNET_NAMESTORE_PluginFunctions *api = cls;
586 struct Plugin *plugin = api->cls;
588 database_shutdown (plugin);
591 LOG (GNUNET_ERROR_TYPE_DEBUG,
592 "postgres plugin is finished\n");
596 /* end of plugin_namestore_postgres.c */