GNUnet  0.11.x
plugin_namecache_postgres.c
Go to the documentation of this file.
1 /*
2  * This file is part of GNUnet
3  * Copyright (C) 2009-2013, 2016, 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 
26 #include "platform.h"
29 #include "gnunet_gnsrecord_lib.h"
30 #include "gnunet_pq_lib.h"
31 #include "namecache.h"
32 
33 
34 #define LOG(kind, ...) GNUNET_log_from (kind, "namecache-postgres", __VA_ARGS__)
35 
36 
40 struct Plugin
41 {
42  const struct GNUNET_CONFIGURATION_Handle *cfg;
43 
47  struct GNUNET_PQ_Context *dbh;
48 };
49 
50 
59 static int
61 {
62  struct GNUNET_PQ_ExecuteStatement es_temporary =
63  GNUNET_PQ_make_execute ("CREATE TEMPORARY TABLE IF NOT EXISTS ns096blocks ("
64  " query BYTEA NOT NULL DEFAULT '',"
65  " block BYTEA NOT NULL DEFAULT '',"
66  " expiration_time BIGINT NOT NULL DEFAULT 0"
67  ")"
68  "WITH OIDS");
69  struct GNUNET_PQ_ExecuteStatement es_default =
70  GNUNET_PQ_make_execute ("CREATE TABLE IF NOT EXISTS ns096blocks ("
71  " query BYTEA NOT NULL DEFAULT '',"
72  " block BYTEA NOT NULL DEFAULT '',"
73  " expiration_time BIGINT NOT NULL DEFAULT 0"
74  ")"
75  "WITH OIDS");
76  const struct GNUNET_PQ_ExecuteStatement *cr;
77 
78  if (GNUNET_YES ==
80  "namecache-postgres",
81  "TEMPORARY_TABLE"))
82  {
83  cr = &es_temporary;
84  }
85  else
86  {
87  cr = &es_default;
88  }
89  {
90  struct GNUNET_PQ_ExecuteStatement es[] = {
91  *cr,
93  "CREATE INDEX ir_query_hash ON ns096blocks (query,expiration_time)"),
95  "CREATE INDEX ir_block_expiration ON ns096blocks (expiration_time)"),
97  };
98  struct GNUNET_PQ_PreparedStatement ps[] = {
99  GNUNET_PQ_make_prepare ("cache_block",
100  "INSERT INTO ns096blocks (query, block, expiration_time) VALUES "
101  "($1, $2, $3)", 3),
102  GNUNET_PQ_make_prepare ("expire_blocks",
103  "DELETE FROM ns096blocks WHERE expiration_time<$1",
104  1),
105  GNUNET_PQ_make_prepare ("delete_block",
106  "DELETE FROM ns096blocks WHERE query=$1 AND expiration_time<=$2",
107  2),
108  GNUNET_PQ_make_prepare ("lookup_block",
109  "SELECT block FROM ns096blocks WHERE query=$1"
110  " ORDER BY expiration_time DESC LIMIT 1", 1),
112  };
113 
114  plugin->dbh = GNUNET_PQ_connect_with_cfg (plugin->cfg,
115  "namecache-postgres",
116  NULL,
117  es,
118  ps);
119  }
120  if (NULL == plugin->dbh)
121  return GNUNET_SYSERR;
122  return GNUNET_OK;
123 }
124 
125 
131 static void
133 {
135  struct GNUNET_PQ_QueryParam params[] = {
138  };
140 
142  "expire_blocks",
143  params);
145 }
146 
147 
155 static void
157  const struct GNUNET_HashCode *query,
158  struct GNUNET_TIME_AbsoluteNBO expiration_time)
159 {
160  struct GNUNET_PQ_QueryParam params[] = {
162  GNUNET_PQ_query_param_absolute_time_nbo (&expiration_time),
164  };
166 
168  "delete_block",
169  params);
171 }
172 
173 
181 static int
183  const struct GNUNET_GNSRECORD_Block *block)
184 {
185  struct Plugin *plugin = cls;
186  struct GNUNET_HashCode query;
187  size_t block_size = ntohl (block->purpose.size)
188  + sizeof(struct GNUNET_CRYPTO_EcdsaPublicKey)
189  + sizeof(struct GNUNET_CRYPTO_EcdsaSignature);
190  struct GNUNET_PQ_QueryParam params[] = {
192  GNUNET_PQ_query_param_fixed_size (block, block_size),
195  };
197 
200  sizeof(struct GNUNET_CRYPTO_EcdsaPublicKey),
201  &query);
202  if (block_size > 64 * 65536)
203  {
204  GNUNET_break (0);
205  return GNUNET_SYSERR;
206  }
207  delete_old_block (plugin,
208  &query,
209  block->expiration_time);
210 
212  "cache_block",
213  params);
214  if (0 > res)
215  return GNUNET_SYSERR;
216  return GNUNET_OK;
217 }
218 
219 
230 static int
232  const struct GNUNET_HashCode *query,
234  void *iter_cls)
235 {
236  struct Plugin *plugin = cls;
237  size_t bsize;
238  struct GNUNET_GNSRECORD_Block *block;
239  struct GNUNET_PQ_QueryParam params[] = {
242  };
243  struct GNUNET_PQ_ResultSpec rs[] = {
245  (void **) &block,
246  &bsize),
248  };
250 
252  "lookup_block",
253  params,
254  rs);
255  if (0 > res)
256  {
258  "Failing lookup block in namecache (postgres error)\n");
259  return GNUNET_SYSERR;
260  }
262  {
263  /* no result */
265  "Ending iteration (no more results)\n");
266  return GNUNET_NO;
267  }
268  if ((bsize < sizeof(*block)) ||
269  (bsize != ntohl (block->purpose.size)
270  + sizeof(struct GNUNET_CRYPTO_EcdsaPublicKey)
271  + sizeof(struct GNUNET_CRYPTO_EcdsaSignature)))
272  {
273  GNUNET_break (0);
275  "Failing lookup (corrupt block)\n");
277  return GNUNET_SYSERR;
278  }
279  iter (iter_cls,
280  block);
282  return GNUNET_OK;
283 }
284 
285 
292 static void
294 {
295  GNUNET_PQ_disconnect (plugin->dbh);
296  plugin->dbh = NULL;
297 }
298 
299 
306 void *
308 {
309  static struct Plugin plugin;
310  const struct GNUNET_CONFIGURATION_Handle *cfg = cls;
312 
313  if (NULL != plugin.cfg)
314  return NULL; /* can only initialize once! */
315  memset (&plugin, 0, sizeof(struct Plugin));
316  plugin.cfg = cfg;
317  if (GNUNET_OK != database_setup (&plugin))
318  {
319  database_shutdown (&plugin);
320  return NULL;
321  }
323  api->cls = &plugin;
327  "Postgres namecache plugin running\n");
328  return api;
329 }
330 
331 
338 void *
340 {
342  struct Plugin *plugin = api->cls;
343 
344  database_shutdown (plugin);
345  plugin->cfg = NULL;
346  GNUNET_free (api);
348  "Postgres namecache plugin is finished\n");
349  return NULL;
350 }
351 
352 
353 /* end of plugin_namecache_postgres.c */
void(* GNUNET_NAMECACHE_BlockCallback)(void *cls, const struct GNUNET_GNSRECORD_Block *block)
Function called for matching blocks.
struct GNUNET_TIME_AbsoluteNBO expiration_time
Expiration time of the block.
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:512
void * cls
Closure to pass to all plugin functions.
struct GNUNET_PQ_QueryParam GNUNET_PQ_query_param_absolute_time_nbo(const struct GNUNET_TIME_AbsoluteNBO *x)
Generate query parameter for an absolute time value.
static int namecache_postgres_lookup_block(void *cls, const struct GNUNET_HashCode *query, GNUNET_NAMECACHE_BlockCallback iter, void *iter_cls)
Get the block for a particular zone and label in the datastore.
static void database_shutdown(struct Plugin *plugin)
Shutdown database connection and associate data structures.
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
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().
const struct GNUNET_CONFIGURATION_Handle * cfg
Our configuration.
Description of a DB result cell.
struct GNUNET_PQ_QueryParam GNUNET_PQ_query_param_absolute_time(const struct GNUNET_TIME_Absolute *x)
Generate query parameter for an absolute time value.
#define GNUNET_new(type)
Allocate a struct or union of the given type.
Information we have in an encrypted block with record data (i.e.
A hard error occurred, retrying will not help.
Definition: gnunet_db_lib.h:39
Time for absolute time used by GNUnet, in microseconds and in network byte order. ...
static int namecache_postgres_cache_block(void *cls, const struct GNUNET_GNSRECORD_Block *block)
Cache a block in the datastore.
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur...
struct GNUNET_BLOCK_PluginFunctions * api
Plugin API.
Definition: block.c:47
int(* cache_block)(void *cls, const struct GNUNET_GNSRECORD_Block *block)
Cache a block in the datastore.
void * cls
Closure for conv and cleaner.
#define GNUNET_PQ_result_spec_end
End of result parameter specification.
#define GNUNET_PQ_query_param_end
End of query parameter specification.
Definition: gnunet_pq_lib.h:96
void * libgnunet_plugin_namecache_postgres_done(void *cls)
Exit point from the plugin.
struct GNUNET_PQ_ResultSpec GNUNET_PQ_result_spec_variable_size(const char *name, void **dst, size_t *sptr)
Variable-size result expected.
enum GNUNET_DB_QueryStatus GNUNET_PQ_eval_prepared_singleton_select(struct GNUNET_PQ_Context *db, const char *statement_name, const struct GNUNET_PQ_QueryParam *params, struct GNUNET_PQ_ResultSpec *rs)
Execute a named prepared statement that is a SELECT statement which must return a single result in co...
Definition: pq_eval.c:258
int(* lookup_block)(void *cls, const struct GNUNET_HashCode *query, GNUNET_NAMECACHE_BlockCallback iter, void *iter_cls)
Get the block for a particular zone and label in the datastore.
void GNUNET_CRYPTO_hash(const void *block, size_t size, struct GNUNET_HashCode *ret)
Compute hash of a given block.
Definition: crypto_hash.c:48
#define LOG(kind,...)
#define GNUNET_PQ_query_param_auto_from_type(x)
Generate fixed-size query parameter with size determined by variable type.
an ECC signature using ECDSA
void * libgnunet_plugin_namecache_postgres_init(void *cls)
Entry point for the plugin.
uint32_t size
How many bytes does this signature sign? (including this purpose header); in network byte order (!)...
static unsigned int bsize
A 512-bit hashcode.
static int res
static char * plugin
Solver plugin name as string.
struct GNUNET_TIME_Absolute GNUNET_TIME_absolute_get(void)
Get the current time.
Definition: time.c:118
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:465
#define GNUNET_PQ_PREPARED_STATEMENT_END
Terminator for prepared statement list.
helper functions for Postgres DB interactions
void * iter_cls
Iterator cls.
static int database_setup(struct Plugin *plugin)
Initialize the database connections and associated data structures (create tables and indices as need...
struct returned by the initialization function of the plugin
static void delete_old_block(struct Plugin *plugin, const struct GNUNET_HashCode *query, struct GNUNET_TIME_AbsoluteNBO expiration_time)
Delete older block in the datastore.
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 connnection using the given para...
Definition: pq_eval.c:164
static void namecache_postgres_expire_blocks(struct Plugin *plugin)
Removes any expired block.
configuration data
Definition: configuration.c:84
Handle for a plugin.
Definition: block.c:37
struct GNUNET_PQ_Context * dbh
Native Postgres database handle.
Public ECC key (always for Curve25519) encoded in a format suitable for network transmission and ECDS...
Description of a DB query parameter.
Definition: gnunet_pq_lib.h:64
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.
#define GNUNET_PQ_EXECUTE_STATEMENT_END
Terminator for executable statement list.
Time for absolute times used by GNUnet, in microseconds.
GNUNET_DB_QueryStatus
Status code returned from functions running database commands.
Definition: gnunet_db_lib.h:34
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
The transaction succeeded, but yielded zero results.
Definition: gnunet_db_lib.h:53
int 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".
struct GNUNET_CRYPTO_EccSignaturePurpose purpose
Number of bytes signed; also specifies the number of bytes of encrypted data that follow...
GNUNET_PEERSTORE_Processor iter
Iterator.
common internal definitions for namecache service
Handle to Postgres database.
Definition: pq.h:34
struct GNUNET_CRYPTO_EcdsaPublicKey derived_key
Derived key used for signing; hash of this is the query.
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
#define GNUNET_free(ptr)
Wrapper around free.
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:130