GNUnet  0.11.x
Data Structures | Macros | Functions
plugin_namestore_postgres.c File Reference

postgres-based namestore backend More...

#include "platform.h"
#include "gnunet_namestore_plugin.h"
#include "gnunet_namestore_service.h"
#include "gnunet_gnsrecord_lib.h"
#include "gnunet_pq_lib.h"
#include "namestore.h"
Include dependency graph for plugin_namestore_postgres.c:

Go to the source code of this file.

Data Structures

struct  Plugin
 Handle for a plugin. More...
 
struct  ParserContext
 Closure for parse_result_call_iterator. More...
 

Macros

#define LOG(kind, ...)   GNUNET_log_from (kind, "namestore-postgres", __VA_ARGS__)
 

Functions

static int database_setup (struct Plugin *plugin)
 Initialize the database connections and associated data structures (create tables and indices as needed as well). More...
 
static int namestore_postgres_store_records (void *cls, const struct GNUNET_IDENTITY_PrivateKey *zone_key, const char *label, unsigned int rd_count, const struct GNUNET_GNSRECORD_Data *rd)
 Store a record in the datastore. More...
 
static void parse_result_call_iterator (void *cls, PGresult *res, unsigned int num_results)
 A statement has been run. More...
 
static int namestore_postgres_lookup_records (void *cls, const struct GNUNET_IDENTITY_PrivateKey *zone, const char *label, GNUNET_NAMESTORE_RecordIterator iter, void *iter_cls)
 Lookup records in the datastore for which we are the authority. More...
 
static int namestore_postgres_iterate_records (void *cls, const struct GNUNET_IDENTITY_PrivateKey *zone, uint64_t serial, uint64_t limit, GNUNET_NAMESTORE_RecordIterator iter, void *iter_cls)
 Iterate over the results for a particular key and zone in the datastore. More...
 
static int namestore_postgres_zone_to_name (void *cls, const struct GNUNET_IDENTITY_PrivateKey *zone, const struct GNUNET_IDENTITY_PublicKey *value_zone, GNUNET_NAMESTORE_RecordIterator iter, void *iter_cls)
 Look for an existing PKEY delegation record for a given public key. More...
 
static void database_shutdown (struct Plugin *plugin)
 Shutdown database connection and associate data structures. More...
 
void * libgnunet_plugin_namestore_postgres_init (void *cls)
 Entry point for the plugin. More...
 
void * libgnunet_plugin_namestore_postgres_done (void *cls)
 Exit point from the plugin. More...
 

Detailed Description

postgres-based namestore backend

Author
Christian Grothoff

Definition in file plugin_namestore_postgres.c.

Macro Definition Documentation

◆ LOG

#define LOG (   kind,
  ... 
)    GNUNET_log_from (kind, "namestore-postgres", __VA_ARGS__)

Definition at line 34 of file plugin_namestore_postgres.c.

Function Documentation

◆ database_setup()

static int database_setup ( struct Plugin plugin)
static

Initialize the database connections and associated data structures (create tables and indices as needed as well).

Parameters
pluginthe plugin context (state for this module)
Returns
GNUNET_OK on success

Definition at line 63 of file plugin_namestore_postgres.c.

64 {
65  struct GNUNET_PQ_ExecuteStatement es_temporary =
67  "CREATE TEMPORARY TABLE IF NOT EXISTS ns098records ("
68  " seq BIGSERIAL PRIMARY KEY,"
69  " zone_private_key BYTEA NOT NULL DEFAULT '',"
70  " pkey BYTEA DEFAULT '',"
71  " rvalue BYTEA NOT NULL DEFAULT '',"
72  " record_count INTEGER NOT NULL DEFAULT 0,"
73  " record_data BYTEA NOT NULL DEFAULT '',"
74  " label TEXT NOT NULL DEFAULT '',"
75  " CONSTRAINT zl UNIQUE (zone_private_key,label)"
76  ")");
77  struct GNUNET_PQ_ExecuteStatement es_default =
78  GNUNET_PQ_make_execute ("CREATE TABLE IF NOT EXISTS ns098records ("
79  " seq BIGSERIAL PRIMARY KEY,"
80  " zone_private_key BYTEA NOT NULL DEFAULT '',"
81  " pkey BYTEA DEFAULT '',"
82  " rvalue BYTEA NOT NULL DEFAULT '',"
83  " record_count INTEGER NOT NULL DEFAULT 0,"
84  " record_data BYTEA NOT NULL DEFAULT '',"
85  " label TEXT NOT NULL DEFAULT '',"
86  " CONSTRAINT zl UNIQUE (zone_private_key,label)"
87  ")");
88  const struct GNUNET_PQ_ExecuteStatement *cr;
90 
91  if (GNUNET_YES ==
93  "namestore-postgres",
94  "TEMPORARY_TABLE"))
95  {
96  cr = &es_temporary;
97  }
98  else
99  {
100  cr = &es_default;
101  }
102 
103  if (GNUNET_YES ==
105  "namestore-postgres",
106  "ASYNC_COMMIT"))
107  sc = GNUNET_PQ_make_try_execute ("SET synchronous_commit TO off");
108 
109  {
110  struct GNUNET_PQ_ExecuteStatement es[] = {
111  *cr,
112  GNUNET_PQ_make_try_execute ("CREATE INDEX IF NOT EXISTS ir_pkey_reverse "
113  "ON ns098records (zone_private_key,pkey)"),
114  GNUNET_PQ_make_try_execute ("CREATE INDEX IF NOT EXISTS ir_pkey_iter "
115  "ON ns098records (zone_private_key,seq)"),
116  GNUNET_PQ_make_try_execute ("CREATE INDEX IF NOT EXISTS ir_label "
117  "ON ns098records (label)"),
118  GNUNET_PQ_make_try_execute ("CREATE INDEX IF NOT EXISTS zone_label "
119  "ON ns098records (zone_private_key,label)"),
120  sc,
122  };
123  struct GNUNET_PQ_PreparedStatement ps[] = {
124  GNUNET_PQ_make_prepare ("store_records",
125  "INSERT INTO ns098records"
126  " (zone_private_key, pkey, rvalue, record_count, record_data, label)"
127  " VALUES ($1, $2, $3, $4, $5, $6)"
128  " ON CONFLICT ON CONSTRAINT zl"
129  " DO UPDATE"
130  " SET pkey=$2,rvalue=$3,record_count=$4,record_data=$5"
131  " WHERE ns098records.zone_private_key = $1"
132  " AND ns098records.label = $6",
133  6),
134  GNUNET_PQ_make_prepare ("delete_records",
135  "DELETE FROM ns098records "
136  "WHERE zone_private_key=$1 AND label=$2",
137  2),
138  GNUNET_PQ_make_prepare ("zone_to_name",
139  "SELECT seq,record_count,record_data,label FROM ns098records"
140  " WHERE zone_private_key=$1 AND pkey=$2",
141  2),
142  GNUNET_PQ_make_prepare ("iterate_zone",
143  "SELECT seq,record_count,record_data,label FROM ns098records "
144  "WHERE zone_private_key=$1 AND seq > $2 ORDER BY seq ASC LIMIT $3",
145  3),
146  GNUNET_PQ_make_prepare ("iterate_all_zones",
147  "SELECT seq,record_count,record_data,label,zone_private_key"
148  " FROM ns098records WHERE seq > $1 ORDER BY seq ASC LIMIT $2",
149  2),
150  GNUNET_PQ_make_prepare ("lookup_label",
151  "SELECT seq,record_count,record_data,label "
152  "FROM ns098records WHERE zone_private_key=$1 AND label=$2",
153  2),
155  };
156 
158  "namestore-postgres",
159  NULL,
160  es,
161  ps);
162  }
163  if (NULL == plugin->dbh)
164  return GNUNET_SYSERR;
165  return GNUNET_OK;
166 }
struct Plugin * plugin
The process handle to the testbed service.
static struct GNUNET_FS_SearchContext * sc
Definition: gnunet-search.c:37
@ GNUNET_OK
Definition: gnunet_common.h:95
@ GNUNET_YES
Definition: gnunet_common.h:97
@ GNUNET_SYSERR
Definition: gnunet_common.h:93
struct GNUNET_PQ_PreparedStatement GNUNET_PQ_make_prepare(const char *name, const char *sql, unsigned int num_args)
Create a struct GNUNET_PQ_PreparedStatement.
Definition: pq_prepare.c:38
struct GNUNET_PQ_ExecuteStatement GNUNET_PQ_make_execute(const char *sql)
Create a struct GNUNET_PQ_ExecuteStatement where errors are fatal.
Definition: pq_exec.c:36
struct GNUNET_PQ_Context * GNUNET_PQ_connect_with_cfg(const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section, const char *load_path_suffix, const struct GNUNET_PQ_ExecuteStatement *es, const struct GNUNET_PQ_PreparedStatement *ps)
Connect to a postgres database using the configuration option "CONFIG" in section.
Definition: pq_connect.c:449
struct GNUNET_PQ_ExecuteStatement GNUNET_PQ_make_try_execute(const char *sql)
Create a struct GNUNET_PQ_ExecuteStatement where errors should be tolerated.
Definition: pq_exec.c:55
#define GNUNET_PQ_PREPARED_STATEMENT_END
Terminator for prepared statement list.
#define GNUNET_PQ_EXECUTE_STATEMENT_END
Terminator for executable statement list.
enum GNUNET_GenericReturnValue GNUNET_CONFIGURATION_get_value_yesno(const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section, const char *option)
Get a configuration value that should be in a set of "YES" or "NO".
Information needed to run a list of SQL statements using GNUNET_PQ_exec_statements().
Information needed to prepare a list of SQL statements using GNUNET_PQ_prepare_statements().
struct GNUNET_PQ_Context * dbh
Native Postgres database handle.
const struct GNUNET_CONFIGURATION_Handle * cfg
Our configuration.

References Plugin::cfg, Plugin::dbh, GNUNET_CONFIGURATION_get_value_yesno(), GNUNET_OK, GNUNET_PQ_connect_with_cfg(), GNUNET_PQ_EXECUTE_STATEMENT_END, GNUNET_PQ_make_execute(), GNUNET_PQ_make_prepare(), GNUNET_PQ_make_try_execute(), GNUNET_PQ_PREPARED_STATEMENT_END, GNUNET_SYSERR, GNUNET_YES, plugin, and sc.

Referenced by libgnunet_plugin_namestore_postgres_init().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ namestore_postgres_store_records()

static int namestore_postgres_store_records ( void *  cls,
const struct GNUNET_IDENTITY_PrivateKey zone_key,
const char *  label,
unsigned int  rd_count,
const struct GNUNET_GNSRECORD_Data rd 
)
static

Store a record in the datastore.

Removes any existing record in the same zone with the same name.

Parameters
clsclosure (internal context for the plugin)
zone_keyprivate key of the zone
labelname that is being mapped (at most 255 characters long)
rd_countnumber of entries in rd array
rdarray of records with data to store
Returns
GNUNET_OK on success, else GNUNET_SYSERR

Definition at line 181 of file plugin_namestore_postgres.c.

187 {
188  struct Plugin *plugin = cls;
190  uint64_t rvalue;
191  uint32_t rd_count32 = (uint32_t) rd_count;
192  ssize_t data_size;
193 
194  memset (&pkey,
195  0,
196  sizeof(pkey));
197  for (unsigned int i = 0; i < rd_count; i++)
198  if (GNUNET_YES ==
199  GNUNET_GNSRECORD_is_zonekey_type (rd[i].record_type))
200  {
203  rd[i].data_size,
204  rd[i].record_type,
205  &pkey));
206  break;
207  }
209  UINT64_MAX);
211  rd);
212  if (data_size < 0)
213  {
214  GNUNET_break (0);
215  return GNUNET_SYSERR;
216  }
217  if (data_size >= UINT16_MAX)
218  {
219  GNUNET_break (0);
220  return GNUNET_SYSERR;
221  }
222  /* if record set is empty, delete existing records */
223  if (0 == rd_count)
224  {
225  struct GNUNET_PQ_QueryParam params[] = {
229  };
231 
233  "delete_records",
234  params);
237  {
238  GNUNET_break (0);
239  return GNUNET_SYSERR;
240  }
242  "postgres",
243  "Record deleted\n");
244  return GNUNET_OK;
245  }
246  /* otherwise, UPSERT (i.e. UPDATE if exists, otherwise INSERT) */
247  {
248  char data[data_size];
249  struct GNUNET_PQ_QueryParam params[] = {
253  GNUNET_PQ_query_param_uint32 (&rd_count32),
257  };
259  ssize_t ret;
260 
262  rd,
263  data_size,
264  data);
265  if ((ret < 0) ||
266  (data_size != ret))
267  {
268  GNUNET_break (0);
269  return GNUNET_SYSERR;
270  }
271 
273  "store_records",
274  params);
276  return GNUNET_SYSERR;
277  }
278  return GNUNET_OK;
279 }
static int ret
Return value of the commandline.
Definition: gnunet-abd.c:81
static size_t data_size
Number of bytes in data.
Definition: gnunet-abd.c:187
static int res
uint32_t data
The data value.
static char * pkey
Public key of the zone to look in, in ASCII.
static const struct GNUNET_IDENTITY_PrivateKey * zone_key
Private key of the zone.
#define GNUNET_log_from(kind, comp,...)
GNUNET_DB_QueryStatus
Status code returned from functions running database commands.
Definition: gnunet_db_lib.h:36
@ GNUNET_DB_STATUS_SUCCESS_ONE_RESULT
The transaction succeeded, and yielded one result.
Definition: gnunet_db_lib.h:59
@ GNUNET_DB_STATUS_SUCCESS_NO_RESULTS
The transaction succeeded, but yielded zero results.
Definition: gnunet_db_lib.h:54
struct GNUNET_PQ_QueryParam GNUNET_PQ_query_param_fixed_size(const void *ptr, size_t ptr_size)
Generate query parameter for a buffer ptr of ptr_size bytes.
struct GNUNET_PQ_QueryParam GNUNET_PQ_query_param_uint64(const uint64_t *x)
Generate query parameter for an uint16_t in host byte order.
#define GNUNET_PQ_query_param_auto_from_type(x)
Generate fixed-size query parameter with size determined by variable type.
#define GNUNET_PQ_query_param_end
End of query parameter specification.
Definition: gnunet_pq_lib.h:97
struct GNUNET_PQ_QueryParam GNUNET_PQ_query_param_uint32(const uint32_t *x)
Generate query parameter for an uint32_t in host byte order.
enum GNUNET_DB_QueryStatus GNUNET_PQ_eval_prepared_non_select(struct GNUNET_PQ_Context *db, const char *statement_name, const struct GNUNET_PQ_QueryParam *params)
Execute a named prepared statement that is NOT a SELECT statement in connection using the given param...
Definition: pq_eval.c:164
struct GNUNET_PQ_QueryParam GNUNET_PQ_query_param_string(const char *ptr)
Generate query parameter for a string.
uint64_t GNUNET_CRYPTO_random_u64(enum GNUNET_CRYPTO_Quality mode, uint64_t max)
Random on unsigned 64-bit values.
@ GNUNET_CRYPTO_QUALITY_WEAK
No good quality of the operation is needed (i.e., random numbers can be pseudo-random).
enum GNUNET_GenericReturnValue GNUNET_GNSRECORD_identity_from_data(const char *data, size_t data_size, uint32_t type, struct GNUNET_IDENTITY_PublicKey *key)
Build a #GNUNET_GNSRECORD_PublicKey from zone delegation resource record data.
ssize_t GNUNET_GNSRECORD_records_serialize(unsigned int rd_count, const struct GNUNET_GNSRECORD_Data *rd, size_t dest_size, char *dest)
Serialize the given records to the given destination buffer.
GNUNET_NETWORK_STRUCT_END ssize_t GNUNET_GNSRECORD_records_get_size(unsigned int rd_count, const struct GNUNET_GNSRECORD_Data *rd)
Calculate how many bytes we will need to serialize the given records.
enum GNUNET_GenericReturnValue GNUNET_GNSRECORD_is_zonekey_type(uint32_t type)
Check if this type is one of the supported GNS zone types.
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur.
@ GNUNET_ERROR_TYPE_DEBUG
An identity key as per LSD0001.
Description of a DB query parameter.
Definition: gnunet_pq_lib.h:65
Handle for a plugin.
Definition: block.c:38

References data, data_size, Plugin::dbh, GNUNET_break, GNUNET_CRYPTO_QUALITY_WEAK, GNUNET_CRYPTO_random_u64(), GNUNET_DB_STATUS_SUCCESS_NO_RESULTS, GNUNET_DB_STATUS_SUCCESS_ONE_RESULT, GNUNET_ERROR_TYPE_DEBUG, GNUNET_GNSRECORD_identity_from_data(), GNUNET_GNSRECORD_is_zonekey_type(), GNUNET_GNSRECORD_records_get_size(), GNUNET_GNSRECORD_records_serialize(), GNUNET_log_from, GNUNET_OK, GNUNET_PQ_eval_prepared_non_select(), GNUNET_PQ_query_param_auto_from_type, GNUNET_PQ_query_param_end, GNUNET_PQ_query_param_fixed_size(), GNUNET_PQ_query_param_string(), GNUNET_PQ_query_param_uint32(), GNUNET_PQ_query_param_uint64(), GNUNET_SYSERR, GNUNET_YES, pkey, plugin, res, ret, and zone_key.

Referenced by libgnunet_plugin_namestore_postgres_init().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ parse_result_call_iterator()

static void parse_result_call_iterator ( void *  cls,
PGresult *  res,
unsigned int  num_results 
)
static

A statement has been run.

We should evaluate the result, and if possible call the iter in cls with the result.

Parameters
clsclosure of type struct ParserContext *
resultthe postgres result
num_resultthe number of results in result

Definition at line 319 of file plugin_namestore_postgres.c.

322 {
323  struct ParserContext *pc = cls;
324 
325  if (NULL == pc->iter)
326  return; /* no need to do more work */
327  for (unsigned int i = 0; i < num_results; i++)
328  {
329  uint64_t serial;
330  void *data;
331  size_t data_size;
332  uint32_t record_count;
333  char *label;
334  struct GNUNET_IDENTITY_PrivateKey zk;
335  struct GNUNET_PQ_ResultSpec rs_with_zone[] = {
336  GNUNET_PQ_result_spec_uint64 ("seq", &serial),
337  GNUNET_PQ_result_spec_uint32 ("record_count", &record_count),
339  GNUNET_PQ_result_spec_string ("label", &label),
340  GNUNET_PQ_result_spec_auto_from_type ("zone_private_key", &zk),
342  };
343  struct GNUNET_PQ_ResultSpec rs_without_zone[] = {
344  GNUNET_PQ_result_spec_uint64 ("seq", &serial),
345  GNUNET_PQ_result_spec_uint32 ("record_count", &record_count),
347  GNUNET_PQ_result_spec_string ("label", &label),
349  };
350  struct GNUNET_PQ_ResultSpec *rs;
351 
352  rs = (NULL == pc->zone_key) ? rs_with_zone : rs_without_zone;
353  if (GNUNET_YES !=
355  rs,
356  i))
357  {
358  GNUNET_break (0);
359  return;
360  }
361 
362  if (record_count > 64 * 1024)
363  {
364  /* sanity check, don't stack allocate far too much just
365  because database might contain a large value here */
366  GNUNET_break (0);
368  return;
369  }
370 
371  {
373 
374  GNUNET_assert (0 != serial);
375  if (GNUNET_OK !=
377  data,
378  record_count,
379  rd))
380  {
381  GNUNET_break (0);
383  return;
384  }
385  pc->iter (pc->iter_cls,
386  serial,
387  (NULL == pc->zone_key) ? &zk : pc->zone_key,
388  label,
389  record_count,
390  rd);
391  }
393  }
394  pc->limit -= num_results;
395 }
static uint64_t record_count
Record count.
static struct GNUNET_FS_PublishContext * pc
Handle to FS-publishing operation.
#define GNUNET_NZL(l)
Macro used to avoid using 0 for the length of a variable-size array (Non-Zero-Length).
struct GNUNET_PQ_ResultSpec GNUNET_PQ_result_spec_uint32(const char *name, uint32_t *u32)
uint32_t expected.
#define GNUNET_PQ_result_spec_auto_from_type(name, dst)
We expect a fixed-size result, with size determined by the type of * dst
struct GNUNET_PQ_ResultSpec GNUNET_PQ_result_spec_uint64(const char *name, uint64_t *u64)
uint64_t expected.
struct GNUNET_PQ_ResultSpec GNUNET_PQ_result_spec_string(const char *name, char **dst)
0-terminated string expected.
struct GNUNET_PQ_ResultSpec GNUNET_PQ_result_spec_variable_size(const char *name, void **dst, size_t *sptr)
Variable-size result expected.
void GNUNET_PQ_cleanup_result(struct GNUNET_PQ_ResultSpec *rs)
Free all memory that was allocated in rs during GNUNET_PQ_extract_result().
Definition: pq.c:117
enum GNUNET_GenericReturnValue GNUNET_PQ_extract_result(PGresult *result, struct GNUNET_PQ_ResultSpec *rs, int row)
Extract results from a query result according to the given specification.
Definition: pq.c:127
#define GNUNET_PQ_result_spec_end
End of result parameter specification.
int GNUNET_GNSRECORD_records_deserialize(size_t len, const char *src, unsigned int rd_count, struct GNUNET_GNSRECORD_Data *dest)
Deserialize the given records to the given destination.
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
A private key for an identity as per LSD0001.
Description of a DB result cell.
Closure for parse_result_call_iterator.

References data, data_size, GNUNET_assert, GNUNET_break, GNUNET_GNSRECORD_records_deserialize(), GNUNET_NZL, GNUNET_OK, GNUNET_PQ_cleanup_result(), GNUNET_PQ_extract_result(), GNUNET_PQ_result_spec_auto_from_type, GNUNET_PQ_result_spec_end, GNUNET_PQ_result_spec_string(), GNUNET_PQ_result_spec_uint32(), GNUNET_PQ_result_spec_uint64(), GNUNET_PQ_result_spec_variable_size(), GNUNET_YES, pc, record_count, and res.

Referenced by namestore_postgres_iterate_records(), namestore_postgres_lookup_records(), and namestore_postgres_zone_to_name().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ namestore_postgres_lookup_records()

static int namestore_postgres_lookup_records ( void *  cls,
const struct GNUNET_IDENTITY_PrivateKey zone,
const char *  label,
GNUNET_NAMESTORE_RecordIterator  iter,
void *  iter_cls 
)
static

Lookup records in the datastore for which we are the authority.

Parameters
clsclosure (internal context for the plugin)
zoneprivate key of the zone
labelname of the record in the zone
iterfunction to call with the result
iter_clsclosure for iter
Returns
GNUNET_OK on success, GNUNET_NO for no results, else GNUNET_SYSERR

Definition at line 409 of file plugin_namestore_postgres.c.

415 {
416  struct Plugin *plugin = cls;
417  struct GNUNET_PQ_QueryParam params[] = {
421  };
422  struct ParserContext pc;
424 
425  if (NULL == zone)
426  {
427  GNUNET_break (0);
428  return GNUNET_SYSERR;
429  }
430  pc.iter = iter;
431  pc.iter_cls = iter_cls;
432  pc.zone_key = zone;
434  "lookup_label",
435  params,
437  &pc);
438  if (res < 0)
439  return GNUNET_SYSERR;
441  return GNUNET_NO;
442  return GNUNET_OK;
443 }
static char * zone
Name of the zone being managed.
@ GNUNET_NO
Definition: gnunet_common.h:94
enum GNUNET_DB_QueryStatus GNUNET_PQ_eval_prepared_multi_select(struct GNUNET_PQ_Context *db, const char *statement_name, const struct GNUNET_PQ_QueryParam *params, GNUNET_PQ_PostgresResultHandler rh, void *rh_cls)
Execute a named prepared statement that is a SELECT statement which may return multiple results in co...
Definition: pq_eval.c:209
static void parse_result_call_iterator(void *cls, PGresult *res, unsigned int num_results)
A statement has been run.
void * iter_cls
Closure for iter.
GNUNET_NAMESTORE_RecordIterator iter
Function to call for each result.

References Plugin::dbh, GNUNET_break, GNUNET_DB_STATUS_SUCCESS_NO_RESULTS, GNUNET_NO, GNUNET_OK, GNUNET_PQ_eval_prepared_multi_select(), GNUNET_PQ_query_param_auto_from_type, GNUNET_PQ_query_param_end, GNUNET_PQ_query_param_string(), GNUNET_SYSERR, ParserContext::iter, ParserContext::iter_cls, parse_result_call_iterator(), pc, plugin, res, and zone.

Referenced by libgnunet_plugin_namestore_postgres_init().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ namestore_postgres_iterate_records()

static int namestore_postgres_iterate_records ( void *  cls,
const struct GNUNET_IDENTITY_PrivateKey zone,
uint64_t  serial,
uint64_t  limit,
GNUNET_NAMESTORE_RecordIterator  iter,
void *  iter_cls 
)
static

Iterate over the results for a particular key and zone in the datastore.

Will return at most one result to the iterator.

Parameters
clsclosure (internal context for the plugin)
zonehash of public key of the zone, NULL to iterate over all zones
serialserial number to exclude in the list of all matching records
limitmaximum number of results to fetch
iterfunction to call with the result
iter_clsclosure for iter
Returns
GNUNET_OK on success, GNUNET_NO if there were no more results, GNUNET_SYSERR on error

Definition at line 459 of file plugin_namestore_postgres.c.

466 {
467  struct Plugin *plugin = cls;
469  struct ParserContext pc;
470 
471  pc.iter = iter;
472  pc.iter_cls = iter_cls;
473  pc.zone_key = zone;
474  pc.limit = limit;
475  if (NULL == zone)
476  {
477  struct GNUNET_PQ_QueryParam params_without_zone[] = {
481  };
482 
484  "iterate_all_zones",
485  params_without_zone,
487  &pc);
488  }
489  else
490  {
491  struct GNUNET_PQ_QueryParam params_with_zone[] = {
496  };
497 
499  "iterate_zone",
500  params_with_zone,
502  &pc);
503  }
504  if (res < 0)
505  return GNUNET_SYSERR;
506 
508  (pc.limit > 0))
509  return GNUNET_NO;
510  return GNUNET_OK;
511 }
uint64_t limit
Number of results still to return (counted down by number of results given to iterator).

References Plugin::dbh, GNUNET_DB_STATUS_SUCCESS_NO_RESULTS, GNUNET_NO, GNUNET_OK, GNUNET_PQ_eval_prepared_multi_select(), GNUNET_PQ_query_param_auto_from_type, GNUNET_PQ_query_param_end, GNUNET_PQ_query_param_uint64(), GNUNET_SYSERR, ParserContext::iter, ParserContext::iter_cls, ParserContext::limit, parse_result_call_iterator(), pc, plugin, res, and zone.

Referenced by libgnunet_plugin_namestore_postgres_init().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ namestore_postgres_zone_to_name()

static int namestore_postgres_zone_to_name ( void *  cls,
const struct GNUNET_IDENTITY_PrivateKey zone,
const struct GNUNET_IDENTITY_PublicKey value_zone,
GNUNET_NAMESTORE_RecordIterator  iter,
void *  iter_cls 
)
static

Look for an existing PKEY delegation record for a given public key.

Returns at most one result to the iterator.

Parameters
clsclosure (internal context for the plugin)
zoneprivate key of the zone to look up in, never NULL
value_zonepublic key of the target zone (value), never NULL
iterfunction to call with the result
iter_clsclosure for iter
Returns
GNUNET_OK on success, GNUNET_NO if there were no results, GNUNET_SYSERR on error

Definition at line 526 of file plugin_namestore_postgres.c.

533 {
534  struct Plugin *plugin = cls;
535  struct GNUNET_PQ_QueryParam params[] = {
539  };
541  struct ParserContext pc;
542 
543  pc.iter = iter;
544  pc.iter_cls = iter_cls;
545  pc.zone_key = zone;
547  "zone_to_name",
548  params,
550  &pc);
551  if (res < 0)
552  return GNUNET_SYSERR;
553  return GNUNET_OK;
554 }

References Plugin::dbh, GNUNET_OK, GNUNET_PQ_eval_prepared_multi_select(), GNUNET_PQ_query_param_auto_from_type, GNUNET_PQ_query_param_end, GNUNET_SYSERR, ParserContext::iter, ParserContext::iter_cls, parse_result_call_iterator(), pc, plugin, res, and zone.

Referenced by libgnunet_plugin_namestore_postgres_init().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ database_shutdown()

static void database_shutdown ( struct Plugin plugin)
static

Shutdown database connection and associate data structures.

Parameters
pluginthe plugin context (state for this module)

Definition at line 564 of file plugin_namestore_postgres.c.

565 {
567  plugin->dbh = NULL;
568 }
void GNUNET_PQ_disconnect(struct GNUNET_PQ_Context *db)
Disconnect from the database, destroying the prepared statements and releasing other associated resou...
Definition: pq_connect.c:508

References Plugin::dbh, GNUNET_PQ_disconnect(), and plugin.

Referenced by libgnunet_plugin_namestore_postgres_done(), and libgnunet_plugin_namestore_postgres_init().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ libgnunet_plugin_namestore_postgres_init()

void* libgnunet_plugin_namestore_postgres_init ( void *  cls)

Entry point for the plugin.

Parameters
clsthe struct GNUNET_NAMESTORE_PluginEnvironment*
Returns
NULL on error, othrewise the plugin context

Definition at line 578 of file plugin_namestore_postgres.c.

579 {
580  static struct Plugin plugin;
581  const struct GNUNET_CONFIGURATION_Handle *cfg = cls;
583 
584  if (NULL != plugin.cfg)
585  return NULL; /* can only initialize once! */
586  memset (&plugin, 0, sizeof(struct Plugin));
587  plugin.cfg = cfg;
588  if (GNUNET_OK != database_setup (&plugin))
589  {
591  return NULL;
592  }
594  api->cls = &plugin;
600  "Postgres namestore plugin running\n");
601  return api;
602 }
static const struct GNUNET_CONFIGURATION_Handle * cfg
Configuration we are using.
Definition: gnunet-abd.c:36
@ GNUNET_ERROR_TYPE_INFO
#define GNUNET_new(type)
Allocate a struct or union of the given type.
static void database_shutdown(struct Plugin *plugin)
Shutdown database connection and associate data structures.
static int namestore_postgres_zone_to_name(void *cls, const struct GNUNET_IDENTITY_PrivateKey *zone, const struct GNUNET_IDENTITY_PublicKey *value_zone, GNUNET_NAMESTORE_RecordIterator iter, void *iter_cls)
Look for an existing PKEY delegation record for a given public key.
static int database_setup(struct Plugin *plugin)
Initialize the database connections and associated data structures (create tables and indices as need...
static int namestore_postgres_iterate_records(void *cls, const struct GNUNET_IDENTITY_PrivateKey *zone, uint64_t serial, uint64_t limit, GNUNET_NAMESTORE_RecordIterator iter, void *iter_cls)
Iterate over the results for a particular key and zone in the datastore.
#define LOG(kind,...)
static int namestore_postgres_store_records(void *cls, const struct GNUNET_IDENTITY_PrivateKey *zone_key, const char *label, unsigned int rd_count, const struct GNUNET_GNSRECORD_Data *rd)
Store a record in the datastore.
static int namestore_postgres_lookup_records(void *cls, const struct GNUNET_IDENTITY_PrivateKey *zone, const char *label, GNUNET_NAMESTORE_RecordIterator iter, void *iter_cls)
Lookup records in the datastore for which we are the authority.
struct returned by the initialization function of the plugin
int(* lookup_records)(void *cls, const struct GNUNET_IDENTITY_PrivateKey *zone, const char *label, GNUNET_NAMESTORE_RecordIterator iter, void *iter_cls)
Lookup records in the datastore for which we are the authority.
int(* zone_to_name)(void *cls, const struct GNUNET_IDENTITY_PrivateKey *zone, const struct GNUNET_IDENTITY_PublicKey *value_zone, GNUNET_NAMESTORE_RecordIterator iter, void *iter_cls)
Look for an existing PKEY delegation record for a given public key.
int(* iterate_records)(void *cls, const struct GNUNET_IDENTITY_PrivateKey *zone, uint64_t serial, uint64_t limit, GNUNET_NAMESTORE_RecordIterator iter, void *iter_cls)
Iterate over the results for a particular zone in the datastore.
int(* store_records)(void *cls, const struct GNUNET_IDENTITY_PrivateKey *zone, const char *label, unsigned int rd_count, const struct GNUNET_GNSRECORD_Data *rd)
Store a record in the datastore for which we are the authority.
void * cls
Closure to pass to all plugin functions.

References cfg, Plugin::cfg, GNUNET_NAMESTORE_PluginFunctions::cls, database_setup(), database_shutdown(), GNUNET_ERROR_TYPE_INFO, GNUNET_new, GNUNET_OK, GNUNET_NAMESTORE_PluginFunctions::iterate_records, LOG, GNUNET_NAMESTORE_PluginFunctions::lookup_records, namestore_postgres_iterate_records(), namestore_postgres_lookup_records(), namestore_postgres_store_records(), namestore_postgres_zone_to_name(), plugin, GNUNET_NAMESTORE_PluginFunctions::store_records, and GNUNET_NAMESTORE_PluginFunctions::zone_to_name.

Here is the call graph for this function:

◆ libgnunet_plugin_namestore_postgres_done()

void* libgnunet_plugin_namestore_postgres_done ( void *  cls)

Exit point from the plugin.

Parameters
clsthe plugin context (as returned by "init")
Returns
always NULL

Definition at line 612 of file plugin_namestore_postgres.c.

613 {
615  struct Plugin *plugin = api->cls;
616 
618  plugin->cfg = NULL;
619  GNUNET_free (api);
621  "Postgres namestore plugin is finished\n");
622  return NULL;
623 }
#define GNUNET_free(ptr)
Wrapper around free.
void * cls
Closure for all of the callbacks.
struct GNUNET_BLOCK_PluginFunctions * api
Plugin API.
Definition: block.c:47

References Plugin::api, Plugin::cfg, GNUNET_BLOCK_PluginFunctions::cls, GNUNET_NAMESTORE_PluginFunctions::cls, database_shutdown(), GNUNET_ERROR_TYPE_DEBUG, GNUNET_free, LOG, and plugin.

Here is the call graph for this function: