GNUnet 0.28.1-dev.6-3-gbc381df64
 
Loading...
Searching...
No Matches
plugin_namecache_postgres.c File Reference

postgres-based namecache backend More...

#include "platform.h"
#include "gnunet_namecache_plugin.h"
#include "gnunet_gnsrecord_lib.h"
#include "gnunet_pq_lib.h"
Include dependency graph for plugin_namecache_postgres.c:

Go to the source code of this file.

Data Structures

struct  Plugin
 Handle for a plugin. More...
 

Macros

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

Functions

static void reconnect_setup (void *cls, struct GNUNET_PQ_Context *pq)
 Function called whenever we reconnect to the DB.
 
static enum GNUNET_GenericReturnValue database_setup (struct Plugin *plugin)
 Initialize the database connections and associated data structures (create tables and indices as needed as well).
 
static void namecache_postgres_expire_blocks (struct Plugin *plugin)
 Removes any expired block.
 
static void delete_old_block (struct Plugin *plugin, const struct GNUNET_HashCode *query, struct GNUNET_TIME_Absolute expiration_time)
 Delete older block in the datastore.
 
static enum GNUNET_GenericReturnValue namecache_postgres_cache_block (void *cls, const struct GNUNET_GNSRECORD_Block *block)
 Cache a block in the datastore.
 
static enum GNUNET_GenericReturnValue namecache_postgres_del_block (void *cls, const struct GNUNET_HashCode *query)
 Remove the block for a particular zone and label from the datastore.
 
static enum GNUNET_GenericReturnValue 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.
 
void * libgnunet_plugin_namecache_postgres_init (void *cls)
 Entry point for the plugin.
 
void * libgnunet_plugin_namecache_postgres_done (void *cls)
 Exit point from the plugin.
 

Detailed Description

postgres-based namecache backend

Author
Christian Grothoff

Definition in file plugin_namecache_postgres.c.

Macro Definition Documentation

◆ LOG

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

Definition at line 32 of file plugin_namecache_postgres.c.

Function Documentation

◆ reconnect_setup()

static void reconnect_setup ( void *  cls,
struct GNUNET_PQ_Context pq 
)
static

Function called whenever we reconnect to the DB.

Sets up the options, possibly the tables and the prepared statements.

Parameters
clsthe struct Plugin
pqdatabase handle

Definition at line 62 of file plugin_namecache_postgres.c.

64{
65 struct Plugin *plugin = cls;
67 GNUNET_PQ_make_prepare ("cache_block",
68 "INSERT INTO namecache.ns096blocks"
69 " (query, block, expiration_time)"
70 " VALUES"
71 " ($1, $2, $3)"),
72 GNUNET_PQ_make_prepare ("expire_blocks",
73 "DELETE FROM namecache.ns096blocks"
74 " WHERE expiration_time<$1"),
75 GNUNET_PQ_make_prepare ("delete_block",
76 "DELETE FROM namecache.ns096blocks"
77 " WHERE query=$1 AND expiration_time<=$2"),
78 GNUNET_PQ_make_prepare ("purge_block",
79 "DELETE FROM namecache.ns096blocks"
80 " WHERE query=$1"),
81 GNUNET_PQ_make_prepare ("lookup_block",
82 "SELECT block"
83 " FROM namecache.ns096blocks"
84 " WHERE query=$1"
85 " ORDER BY expiration_time DESC LIMIT 1"),
87 };
88
89 if (GNUNET_SYSERR ==
91 {
92 plugin->ready = false;
93 return;
94 }
95 if (GNUNET_OK !=
97 "namecache-"))
98 {
99 plugin->ready = false;
100 return;
101 }
102 if (GNUNET_OK !=
104 ps))
105 {
106 plugin->ready = false;
107 return;
108 }
109 plugin->ready = true;
110}
static struct GNUNET_TESTING_PluginFunctions * plugin
Plugin to dynamically load a test case.
static struct GNUNET_PEERSTORE_Handle * ps
Handle to the PEERSTORE service.
enum GNUNET_GenericReturnValue GNUNET_PQ_prepare_statements(struct GNUNET_PQ_Context *db, const struct GNUNET_PQ_PreparedStatement *ps)
Request creation of prepared statements ps from Postgres.
Definition pq_prepare.c:77
struct GNUNET_PQ_PreparedStatement GNUNET_PQ_make_prepare(const char *name, const char *sql)
Create a struct GNUNET_PQ_PreparedStatement.
Definition pq_prepare.c:30
#define GNUNET_PQ_PREPARED_STATEMENT_END
Terminator for prepared statement list.
enum GNUNET_GenericReturnValue GNUNET_PQ_load_versioning(struct GNUNET_PQ_Context *db)
Setup database versioning.
Definition pq_connect.c:712
enum GNUNET_GenericReturnValue GNUNET_PQ_run_sql(struct GNUNET_PQ_Context *db, const char *load_suffix)
Within the db context, run all the SQL files in the load path where the name starts with the load_suf...
Definition pq_connect.c:486
@ GNUNET_OK
@ GNUNET_SYSERR
Information needed to prepare a list of SQL statements using GNUNET_PQ_prepare_statements().
Handle for a plugin.
Definition block.c:38

References GNUNET_OK, GNUNET_PQ_load_versioning(), GNUNET_PQ_make_prepare(), GNUNET_PQ_prepare_statements(), GNUNET_PQ_PREPARED_STATEMENT_END, GNUNET_PQ_run_sql(), GNUNET_SYSERR, plugin, and ps.

Referenced by database_setup().

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

◆ database_setup()

static enum GNUNET_GenericReturnValue 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 122 of file plugin_namecache_postgres.c.

123{
124 plugin->dbh = GNUNET_PQ_init (plugin->cfg,
125 "namecache-postgres",
127 plugin);
128 if (NULL == plugin->dbh)
129 return GNUNET_SYSERR;
130 if (! plugin->ready)
131 return GNUNET_NO;
132 return GNUNET_OK;
133}
struct GNUNET_PQ_Context * GNUNET_PQ_init(const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section, GNUNET_PQ_ReconnectCallback rc, void *rc_cls)
Connect to a postgres database using the configuration option "CONFIG" in section.
@ GNUNET_NO
static void reconnect_setup(void *cls, struct GNUNET_PQ_Context *pq)
Function called whenever we reconnect to the DB.

References GNUNET_NO, GNUNET_OK, GNUNET_PQ_init(), GNUNET_SYSERR, plugin, and reconnect_setup().

Referenced by libgnunet_plugin_namecache_postgres_init().

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

◆ namecache_postgres_expire_blocks()

static void namecache_postgres_expire_blocks ( struct Plugin plugin)
static

Removes any expired block.

Parameters
pluginthe plugin

Definition at line 142 of file plugin_namecache_postgres.c.

143{
145 struct GNUNET_PQ_QueryParam params[] = {
148 };
150
152 "expire_blocks",
153 params);
155}
static char * res
Currently read line or NULL on EOF.
GNUNET_DB_QueryStatus
Status code returned from functions running database commands.
@ GNUNET_DB_STATUS_HARD_ERROR
A hard error occurred, retrying will not help.
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_PQ_query_param_end
End of query parameter specification.
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:135
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur.
struct GNUNET_TIME_Absolute GNUNET_TIME_absolute_get(void)
Get the current time.
Definition time.c:111
Description of a DB query parameter.
Time for absolute times used by GNUnet, in microseconds.

References GNUNET_break, GNUNET_DB_STATUS_HARD_ERROR, GNUNET_PQ_eval_prepared_non_select(), GNUNET_PQ_query_param_absolute_time(), GNUNET_PQ_query_param_end, GNUNET_TIME_absolute_get(), plugin, and res.

Referenced by namecache_postgres_cache_block().

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

◆ delete_old_block()

static void delete_old_block ( struct Plugin plugin,
const struct GNUNET_HashCode query,
struct GNUNET_TIME_Absolute  expiration_time 
)
static

Delete older block in the datastore.

Parameters
pluginthe plugin
queryquery for the block
expiration_timehow old does the block have to be for deletion

Definition at line 166 of file plugin_namecache_postgres.c.

169{
170 struct GNUNET_PQ_QueryParam params[] = {
172 GNUNET_PQ_query_param_absolute_time (&expiration_time),
174 };
176
178 "delete_block",
179 params);
181}
#define GNUNET_PQ_query_param_auto_from_type(x)
Generate fixed-size query parameter with size determined by variable type.

References GNUNET_break, GNUNET_DB_STATUS_HARD_ERROR, GNUNET_PQ_eval_prepared_non_select(), GNUNET_PQ_query_param_absolute_time(), GNUNET_PQ_query_param_auto_from_type, GNUNET_PQ_query_param_end, plugin, and res.

Referenced by namecache_postgres_cache_block().

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

◆ namecache_postgres_cache_block()

static enum GNUNET_GenericReturnValue namecache_postgres_cache_block ( void *  cls,
const struct GNUNET_GNSRECORD_Block block 
)
static

Cache a block in the datastore.

Parameters
clsclosure (internal context for the plugin)
blockblock to cache
Returns
GNUNET_OK on success, else GNUNET_SYSERR

Definition at line 192 of file plugin_namecache_postgres.c.

194{
195 struct Plugin *plugin = cls;
196 struct GNUNET_HashCode query;
197 size_t block_size = GNUNET_GNSRECORD_block_get_size (block);
199 );
201
204 &query);
205 if (block_size > 64 * 65536)
206 {
207 GNUNET_break (0);
208 return GNUNET_SYSERR;
209 }
210 {
211 struct GNUNET_PQ_QueryParam params[] = {
213 GNUNET_PQ_query_param_fixed_size (block, block_size),
216 };
218 &query,
219 exp);
220
222 "cache_block",
223 params);
224 }
225 if (0 > res)
226 return GNUNET_SYSERR;
227 return GNUNET_OK;
228}
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.
size_t GNUNET_GNSRECORD_block_get_size(const struct GNUNET_GNSRECORD_Block *block)
Returns the length of this block in bytes.
enum GNUNET_GenericReturnValue GNUNET_GNSRECORD_query_from_block(const struct GNUNET_GNSRECORD_Block *block, struct GNUNET_HashCode *query)
Builds the query hash from a block.
struct GNUNET_TIME_Absolute GNUNET_GNSRECORD_block_get_expiration(const struct GNUNET_GNSRECORD_Block *block)
Returns the expiration of a block.
static void delete_old_block(struct Plugin *plugin, const struct GNUNET_HashCode *query, struct GNUNET_TIME_Absolute expiration_time)
Delete older block in the datastore.
static void namecache_postgres_expire_blocks(struct Plugin *plugin)
Removes any expired block.
A 512-bit hashcode.

References delete_old_block(), GNUNET_break, GNUNET_GNSRECORD_block_get_expiration(), GNUNET_GNSRECORD_block_get_size(), GNUNET_GNSRECORD_query_from_block(), GNUNET_OK, GNUNET_PQ_eval_prepared_non_select(), GNUNET_PQ_query_param_absolute_time(), GNUNET_PQ_query_param_auto_from_type, GNUNET_PQ_query_param_end, GNUNET_PQ_query_param_fixed_size(), GNUNET_SYSERR, namecache_postgres_expire_blocks(), plugin, and res.

Referenced by libgnunet_plugin_namecache_postgres_init().

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

◆ namecache_postgres_del_block()

static enum GNUNET_GenericReturnValue namecache_postgres_del_block ( void *  cls,
const struct GNUNET_HashCode query 
)
static

Remove the block for a particular zone and label from the datastore.

Parameters
clsclosure (internal context for the plugin)
queryhash of public key derived from the zone and the label
Returns
GNUNET_OK on success, GNUNET_NO if there was no such block, GNUNET_SYSERR on error

Definition at line 241 of file plugin_namecache_postgres.c.

243{
244 struct Plugin *plugin = cls;
245 struct GNUNET_PQ_QueryParam params[] = {
248 };
250
252 "purge_block",
253 params);
255 return GNUNET_NO;
256 if (0 > res)
257 return GNUNET_SYSERR;
258 return GNUNET_OK;
259}
@ GNUNET_DB_STATUS_SUCCESS_NO_RESULTS
The transaction succeeded, but yielded zero results.

References GNUNET_DB_STATUS_SUCCESS_NO_RESULTS, GNUNET_NO, GNUNET_OK, GNUNET_PQ_eval_prepared_non_select(), GNUNET_PQ_query_param_auto_from_type, GNUNET_PQ_query_param_end, GNUNET_SYSERR, plugin, and res.

Referenced by libgnunet_plugin_namecache_postgres_init().

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

◆ namecache_postgres_lookup_block()

static enum GNUNET_GenericReturnValue namecache_postgres_lookup_block ( void *  cls,
const struct GNUNET_HashCode query,
GNUNET_NAMECACHE_BlockCallback  iter,
void *  iter_cls 
)
static

Get the block for a particular zone and label in the datastore.

Will return at most one result to the iterator.

Parameters
clsclosure (internal context for the plugin)
queryhash of public key derived from the zone and the label
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 273 of file plugin_namecache_postgres.c.

277{
278 struct Plugin *plugin = cls;
279 size_t bsize;
280 struct GNUNET_GNSRECORD_Block *block;
281 struct GNUNET_PQ_QueryParam params[] = {
284 };
285 struct GNUNET_PQ_ResultSpec rs[] = {
287 (void **) &block,
288 &bsize),
290 };
292
294 "lookup_block",
295 params,
296 rs);
297 if (0 > res)
298 {
300 "Failing lookup block in namecache (postgres error)\n");
301 return GNUNET_SYSERR;
302 }
304 {
305 /* no result */
307 "Ending iteration (no more results)\n");
308 return GNUNET_NO;
309 }
310 if ((bsize < sizeof(*block)))
311 {
312 GNUNET_break (0);
314 "Failing lookup (corrupt block)\n");
316 return GNUNET_SYSERR;
317 }
318 iter (iter_cls,
319 block);
321 return GNUNET_OK;
322}
static unsigned int bsize
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:199
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:143
#define GNUNET_PQ_result_spec_end
End of result parameter specification.
@ GNUNET_ERROR_TYPE_WARNING
@ GNUNET_ERROR_TYPE_DEBUG
#define LOG(kind,...)
Description of a DB result cell.

References bsize, GNUNET_break, GNUNET_DB_STATUS_SUCCESS_NO_RESULTS, GNUNET_ERROR_TYPE_DEBUG, GNUNET_ERROR_TYPE_WARNING, GNUNET_NO, GNUNET_OK, GNUNET_PQ_cleanup_result(), GNUNET_PQ_eval_prepared_singleton_select(), GNUNET_PQ_query_param_auto_from_type, GNUNET_PQ_query_param_end, GNUNET_PQ_result_spec_end, GNUNET_PQ_result_spec_variable_size(), GNUNET_SYSERR, LOG, plugin, and res.

Referenced by libgnunet_plugin_namecache_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 332 of file plugin_namecache_postgres.c.

333{
335 plugin->dbh = NULL;
336}
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:788

References GNUNET_PQ_disconnect(), and plugin.

Referenced by libgnunet_plugin_namecache_postgres_done(), and libgnunet_plugin_namecache_postgres_init().

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

◆ libgnunet_plugin_namecache_postgres_init()

void * libgnunet_plugin_namecache_postgres_init ( void *  cls)

Entry point for the plugin.

Parameters
clsthe struct GNUNET_NAMECACHE_PluginEnvironment *
Returns
NULL on error, otherwise the plugin context

Definition at line 349 of file plugin_namecache_postgres.c.

350{
351 static struct Plugin plugin;
352 const struct GNUNET_CONFIGURATION_Handle *cfg = cls;
354
355 if (NULL != plugin.cfg)
356 return NULL; /* can only initialize once! */
357 memset (&plugin,
358 0,
359 sizeof(struct Plugin));
360 plugin.cfg = cfg;
361 if (GNUNET_OK !=
363 {
365 return NULL;
366 }
368 api->cls = &plugin;
373 "Postgres namecache plugin running\n");
374 return api;
375}
static struct GNUNET_CONFIGURATION_Handle * cfg
Our configuration.
Definition gnunet-arm.c:108
@ GNUNET_ERROR_TYPE_INFO
#define GNUNET_new(type)
Allocate a struct or union of the given type.
static enum GNUNET_GenericReturnValue database_setup(struct Plugin *plugin)
Initialize the database connections and associated data structures (create tables and indices as need...
static enum GNUNET_GenericReturnValue namecache_postgres_cache_block(void *cls, const struct GNUNET_GNSRECORD_Block *block)
Cache a block in the datastore.
static enum GNUNET_GenericReturnValue namecache_postgres_del_block(void *cls, const struct GNUNET_HashCode *query)
Remove the block for a particular zone and label from the datastore.
static void database_shutdown(struct Plugin *plugin)
Shutdown database connection and associate data structures.
static enum GNUNET_GenericReturnValue 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.
struct returned by the initialization function of the plugin
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.
int(* del_block)(void *cls, const struct GNUNET_HashCode *query)
Remove the block for a particular zone and label from the datastore.
int(* cache_block)(void *cls, const struct GNUNET_GNSRECORD_Block *block)
Cache a block in the datastore.
void * cls
Closure to pass to all plugin functions.

References GNUNET_NAMECACHE_PluginFunctions::cache_block, cfg, GNUNET_NAMECACHE_PluginFunctions::cls, database_setup(), database_shutdown(), GNUNET_NAMECACHE_PluginFunctions::del_block, GNUNET_ERROR_TYPE_INFO, GNUNET_new, GNUNET_OK, LOG, GNUNET_NAMECACHE_PluginFunctions::lookup_block, namecache_postgres_cache_block(), namecache_postgres_del_block(), namecache_postgres_lookup_block(), and plugin.

Here is the call graph for this function:

◆ libgnunet_plugin_namecache_postgres_done()

void * libgnunet_plugin_namecache_postgres_done ( void *  cls)

Exit point from the plugin.

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

Definition at line 388 of file plugin_namecache_postgres.c.

389{
391 struct Plugin *plugin = api->cls;
392
394 plugin->cfg = NULL;
397 "Postgres namecache plugin is finished\n");
398 return NULL;
399}
#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, GNUNET_BLOCK_PluginFunctions::cls, GNUNET_NAMECACHE_PluginFunctions::cls, database_shutdown(), GNUNET_ERROR_TYPE_DEBUG, GNUNET_free, LOG, and plugin.

Here is the call graph for this function: