GNUnet  0.10.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  const struct GNUNET_CONFIGURATION_Handle *cfg;
42 
46  PGconn *dbh;
47 };
48 
49 
58 static int
60 {
61  struct GNUNET_PQ_ExecuteStatement es_temporary =
62  GNUNET_PQ_make_execute("CREATE TEMPORARY TABLE IF NOT EXISTS ns096blocks ("
63  " query BYTEA NOT NULL DEFAULT '',"
64  " block BYTEA NOT NULL DEFAULT '',"
65  " expiration_time BIGINT NOT NULL DEFAULT 0"
66  ")"
67  "WITH OIDS");
68  struct GNUNET_PQ_ExecuteStatement es_default =
69  GNUNET_PQ_make_execute("CREATE TABLE IF NOT EXISTS ns096blocks ("
70  " query BYTEA NOT NULL DEFAULT '',"
71  " block BYTEA NOT NULL DEFAULT '',"
72  " expiration_time BIGINT NOT NULL DEFAULT 0"
73  ")"
74  "WITH OIDS");
75  const struct GNUNET_PQ_ExecuteStatement *cr;
76 
77  plugin->dbh = GNUNET_PQ_connect_with_cfg(plugin->cfg,
78  "namecache-postgres");
79  if (NULL == plugin->dbh)
80  return GNUNET_SYSERR;
81  if (GNUNET_YES ==
83  "namecache-postgres",
84  "TEMPORARY_TABLE"))
85  {
86  cr = &es_temporary;
87  }
88  else
89  {
90  cr = &es_default;
91  }
92 
93  {
94  struct GNUNET_PQ_ExecuteStatement es[] = {
95  *cr,
96  GNUNET_PQ_make_try_execute("CREATE INDEX ir_query_hash ON ns096blocks (query,expiration_time)"),
97  GNUNET_PQ_make_try_execute("CREATE INDEX ir_block_expiration ON ns096blocks (expiration_time)"),
99  };
100 
101  if (GNUNET_OK !=
103  es))
104  {
105  PQfinish(plugin->dbh);
106  plugin->dbh = NULL;
107  return GNUNET_SYSERR;
108  }
109  }
110 
111  {
112  struct GNUNET_PQ_PreparedStatement ps[] = {
113  GNUNET_PQ_make_prepare("cache_block",
114  "INSERT INTO ns096blocks (query, block, expiration_time) VALUES "
115  "($1, $2, $3)", 3),
116  GNUNET_PQ_make_prepare("expire_blocks",
117  "DELETE FROM ns096blocks WHERE expiration_time<$1", 1),
118  GNUNET_PQ_make_prepare("delete_block",
119  "DELETE FROM ns096blocks WHERE query=$1 AND expiration_time<=$2", 2),
120  GNUNET_PQ_make_prepare("lookup_block",
121  "SELECT block FROM ns096blocks WHERE query=$1"
122  " ORDER BY expiration_time DESC LIMIT 1", 1),
124  };
125 
126  if (GNUNET_OK !=
128  ps))
129  {
130  PQfinish(plugin->dbh);
131  plugin->dbh = NULL;
132  return GNUNET_SYSERR;
133  }
134  }
135 
136  return GNUNET_OK;
137 }
138 
139 
145 static void
147 {
149  struct GNUNET_PQ_QueryParam params[] = {
152  };
154 
156  "expire_blocks",
157  params);
159 }
160 
161 
169 static void
171  const struct GNUNET_HashCode *query,
172  struct GNUNET_TIME_AbsoluteNBO expiration_time)
173 {
174  struct GNUNET_PQ_QueryParam params[] = {
176  GNUNET_PQ_query_param_absolute_time_nbo(&expiration_time),
178  };
180 
182  "delete_block",
183  params);
185 }
186 
187 
195 static int
197  const struct GNUNET_GNSRECORD_Block *block)
198 {
199  struct Plugin *plugin = cls;
200  struct GNUNET_HashCode query;
201  size_t block_size = ntohl(block->purpose.size) +
202  sizeof(struct GNUNET_CRYPTO_EcdsaPublicKey) +
203  sizeof(struct GNUNET_CRYPTO_EcdsaSignature);
204  struct GNUNET_PQ_QueryParam params[] = {
206  GNUNET_PQ_query_param_fixed_size(block, block_size),
209  };
211 
214  sizeof(struct GNUNET_CRYPTO_EcdsaPublicKey),
215  &query);
216  if (block_size > 64 * 65536)
217  {
218  GNUNET_break(0);
219  return GNUNET_SYSERR;
220  }
221  delete_old_block(plugin,
222  &query,
223  block->expiration_time);
224 
226  "cache_block",
227  params);
228  if (0 > res)
229  return GNUNET_SYSERR;
230  return GNUNET_OK;
231 }
232 
233 
244 static int
246  const struct GNUNET_HashCode *query,
248  void *iter_cls)
249 {
250  struct Plugin *plugin = cls;
251  size_t bsize;
252  struct GNUNET_GNSRECORD_Block *block;
253  struct GNUNET_PQ_QueryParam params[] = {
256  };
257  struct GNUNET_PQ_ResultSpec rs[] = {
259  (void **)&block,
260  &bsize),
262  };
264 
266  "lookup_block",
267  params,
268  rs);
269  if (0 > res)
270  {
272  "Failing lookup block in namecache (postgres error)\n");
273  return GNUNET_SYSERR;
274  }
276  {
277  /* no result */
279  "Ending iteration (no more results)\n");
280  return GNUNET_NO;
281  }
282  if ((bsize < sizeof(*block)) ||
283  (bsize != ntohl(block->purpose.size) +
284  sizeof(struct GNUNET_CRYPTO_EcdsaPublicKey) +
285  sizeof(struct GNUNET_CRYPTO_EcdsaSignature)))
286  {
287  GNUNET_break(0);
289  "Failing lookup (corrupt block)\n");
291  return GNUNET_SYSERR;
292  }
293  iter(iter_cls,
294  block);
296  return GNUNET_OK;
297 }
298 
299 
306 static void
308 {
309  PQfinish(plugin->dbh);
310  plugin->dbh = NULL;
311 }
312 
313 
320 void *
322 {
323  static struct Plugin plugin;
324  const struct GNUNET_CONFIGURATION_Handle *cfg = cls;
326 
327  if (NULL != plugin.cfg)
328  return NULL; /* can only initialize once! */
329  memset(&plugin, 0, sizeof(struct Plugin));
330  plugin.cfg = cfg;
331  if (GNUNET_OK != database_setup(&plugin))
332  {
333  database_shutdown(&plugin);
334  return NULL;
335  }
337  api->cls = &plugin;
341  "Postgres namecache plugin running\n");
342  return api;
343 }
344 
345 
352 void *
354 {
356  struct Plugin *plugin = api->cls;
357 
358  database_shutdown(plugin);
359  plugin->cfg = NULL;
360  GNUNET_free(api);
362  "Postgres namecache plugin is finished\n");
363  return NULL;
364 }
365 
366 /* 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 * 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:37
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.
#define GNUNET_NO
Definition: gnunet_common.h:78
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_OK
Named constants for return values.
Definition: gnunet_common.h:75
enum GNUNET_DB_QueryStatus GNUNET_PQ_eval_prepared_singleton_select(PGconn *connection, 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:241
#define GNUNET_new(type)
Allocate a struct or union of the given type.
enum GNUNET_DB_QueryStatus GNUNET_PQ_eval_prepared_non_select(PGconn *connection, 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:151
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:38
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:46
int(* cache_block)(void *cls, const struct GNUNET_GNSRECORD_Block *block)
Cache a block in the datastore.
static struct GNUNET_ATS_SolverFunctions * plugin
Our solver.
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:94
int GNUNET_PQ_prepare_statements(PGconn *connection, const struct GNUNET_PQ_PreparedStatement *ps)
Request creation of prepared statements ps from Postgres.
Definition: pq_prepare.c:63
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.
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:44
#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
struct GNUNET_TIME_Absolute GNUNET_TIME_absolute_get(void)
Get the current time.
Definition: time.c:118
PGconn * GNUNET_PQ_connect_with_cfg(const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section)
Connect to a postgres database using the configuration option "CONFIG" in section.
Definition: pq_connect.c:112
#define GNUNET_PQ_PREPARED_STATEMENT_END
Terminator for prepared statement list.
#define GNUNET_SYSERR
Definition: gnunet_common.h:76
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...
int GNUNET_PQ_exec_statements(PGconn *connection, const struct GNUNET_PQ_ExecuteStatement *es)
Request execution of an array of statements es from Postgres.
Definition: pq_exec.c:77
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.
static void namecache_postgres_expire_blocks(struct Plugin *plugin)
Removes any expired block.
configuration data
Definition: configuration.c:83
Handle for a plugin.
Definition: block.c:37
PGconn * 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:63
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.
#define GNUNET_YES
Definition: gnunet_common.h:77
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:56
The transaction succeeded, but yielded zero results.
Definition: gnunet_db_lib.h:52
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
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:39
#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:121