2 * This file is part of GNUnet
3 * Copyright (C) 2009-2013, 2016, 2017 GNUnet e.V.
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.
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.
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/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
22 * @file namecache/plugin_namecache_postgres.c
23 * @brief postgres-based namecache backend
24 * @author Christian Grothoff
27 #include "gnunet_namecache_plugin.h"
28 #include "gnunet_namecache_service.h"
29 #include "gnunet_gnsrecord_lib.h"
30 #include "gnunet_pq_lib.h"
31 #include "namecache.h"
34 #define LOG(kind, ...) GNUNET_log_from (kind, "namecache-postgres", __VA_ARGS__)
38 * Context for all functions in this plugin.
42 const struct GNUNET_CONFIGURATION_Handle *cfg;
45 * Postgres database handle.
47 struct GNUNET_PQ_Context *dbh;
52 * Initialize the database connections and associated
53 * data structures (create tables and indices
56 * @param plugin the plugin context (state for this module)
57 * @return #GNUNET_OK on success
60 database_setup (struct Plugin *plugin)
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"
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"
76 const struct GNUNET_PQ_ExecuteStatement *cr;
79 GNUNET_CONFIGURATION_get_value_yesno (plugin->cfg,
90 struct GNUNET_PQ_ExecuteStatement es[] = {
92 GNUNET_PQ_make_try_execute (
93 "CREATE INDEX ir_query_hash ON ns096blocks (query,expiration_time)"),
94 GNUNET_PQ_make_try_execute (
95 "CREATE INDEX ir_block_expiration ON ns096blocks (expiration_time)"),
96 GNUNET_PQ_EXECUTE_STATEMENT_END
98 struct GNUNET_PQ_PreparedStatement ps[] = {
99 GNUNET_PQ_make_prepare ("cache_block",
100 "INSERT INTO ns096blocks (query, block, expiration_time) VALUES "
102 GNUNET_PQ_make_prepare ("expire_blocks",
103 "DELETE FROM ns096blocks WHERE expiration_time<$1",
105 GNUNET_PQ_make_prepare ("delete_block",
106 "DELETE FROM ns096blocks WHERE query=$1 AND expiration_time<=$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),
111 GNUNET_PQ_PREPARED_STATEMENT_END
114 plugin->dbh = GNUNET_PQ_connect_with_cfg (plugin->cfg,
115 "namecache-postgres",
120 if (NULL == plugin->dbh)
121 return GNUNET_SYSERR;
127 * Removes any expired block.
129 * @param plugin the plugin
132 namecache_postgres_expire_blocks (struct Plugin *plugin)
134 struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get ();
135 struct GNUNET_PQ_QueryParam params[] = {
136 GNUNET_PQ_query_param_absolute_time (&now),
137 GNUNET_PQ_query_param_end
139 enum GNUNET_DB_QueryStatus res;
141 res = GNUNET_PQ_eval_prepared_non_select (plugin->dbh,
144 GNUNET_break (GNUNET_DB_STATUS_HARD_ERROR != res);
149 * Delete older block in the datastore.
151 * @param plugin the plugin
152 * @param query query for the block
153 * @param expiration_time how old does the block have to be for deletion
156 delete_old_block (struct Plugin *plugin,
157 const struct GNUNET_HashCode *query,
158 struct GNUNET_TIME_AbsoluteNBO expiration_time)
160 struct GNUNET_PQ_QueryParam params[] = {
161 GNUNET_PQ_query_param_auto_from_type (query),
162 GNUNET_PQ_query_param_absolute_time_nbo (&expiration_time),
163 GNUNET_PQ_query_param_end
165 enum GNUNET_DB_QueryStatus res;
167 res = GNUNET_PQ_eval_prepared_non_select (plugin->dbh,
170 GNUNET_break (GNUNET_DB_STATUS_HARD_ERROR != res);
175 * Cache a block in the datastore.
177 * @param cls closure (internal context for the plugin)
178 * @param block block to cache
179 * @return #GNUNET_OK on success, else #GNUNET_SYSERR
182 namecache_postgres_cache_block (void *cls,
183 const struct GNUNET_GNSRECORD_Block *block)
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[] = {
191 GNUNET_PQ_query_param_auto_from_type (&query),
192 GNUNET_PQ_query_param_fixed_size (block, block_size),
193 GNUNET_PQ_query_param_absolute_time_nbo (&block->expiration_time),
194 GNUNET_PQ_query_param_end
196 enum GNUNET_DB_QueryStatus res;
198 namecache_postgres_expire_blocks (plugin);
199 GNUNET_CRYPTO_hash (&block->derived_key,
200 sizeof(struct GNUNET_CRYPTO_EcdsaPublicKey),
202 if (block_size > 64 * 65536)
205 return GNUNET_SYSERR;
207 delete_old_block (plugin,
209 block->expiration_time);
211 res = GNUNET_PQ_eval_prepared_non_select (plugin->dbh,
215 return GNUNET_SYSERR;
221 * Get the block for a particular zone and label in the
222 * datastore. Will return at most one result to the iterator.
224 * @param cls closure (internal context for the plugin)
225 * @param query hash of public key derived from the zone and the label
226 * @param iter function to call with the result
227 * @param iter_cls closure for @a iter
228 * @return #GNUNET_OK on success, #GNUNET_NO if there were no results, #GNUNET_SYSERR on error
231 namecache_postgres_lookup_block (void *cls,
232 const struct GNUNET_HashCode *query,
233 GNUNET_NAMECACHE_BlockCallback iter,
236 struct Plugin *plugin = cls;
238 struct GNUNET_GNSRECORD_Block *block;
239 struct GNUNET_PQ_QueryParam params[] = {
240 GNUNET_PQ_query_param_auto_from_type (query),
241 GNUNET_PQ_query_param_end
243 struct GNUNET_PQ_ResultSpec rs[] = {
244 GNUNET_PQ_result_spec_variable_size ("block",
247 GNUNET_PQ_result_spec_end
249 enum GNUNET_DB_QueryStatus res;
251 res = GNUNET_PQ_eval_prepared_singleton_select (plugin->dbh,
257 LOG (GNUNET_ERROR_TYPE_WARNING,
258 "Failing lookup block in namecache (postgres error)\n");
259 return GNUNET_SYSERR;
261 if (GNUNET_DB_STATUS_SUCCESS_NO_RESULTS == res)
264 LOG (GNUNET_ERROR_TYPE_DEBUG,
265 "Ending iteration (no more results)\n");
268 if ((bsize < sizeof(*block)) ||
269 (bsize != ntohl (block->purpose.size)
270 + sizeof(struct GNUNET_CRYPTO_EcdsaPublicKey)
271 + sizeof(struct GNUNET_CRYPTO_EcdsaSignature)))
274 LOG (GNUNET_ERROR_TYPE_DEBUG,
275 "Failing lookup (corrupt block)\n");
276 GNUNET_PQ_cleanup_result (rs);
277 return GNUNET_SYSERR;
281 GNUNET_PQ_cleanup_result (rs);
287 * Shutdown database connection and associate data
290 * @param plugin the plugin context (state for this module)
293 database_shutdown (struct Plugin *plugin)
295 GNUNET_PQ_disconnect (plugin->dbh);
301 * Entry point for the plugin.
303 * @param cls the `struct GNUNET_NAMECACHE_PluginEnvironment *`
304 * @return NULL on error, otherwise the plugin context
307 libgnunet_plugin_namecache_postgres_init (void *cls)
309 static struct Plugin plugin;
310 const struct GNUNET_CONFIGURATION_Handle *cfg = cls;
311 struct GNUNET_NAMECACHE_PluginFunctions *api;
313 if (NULL != plugin.cfg)
314 return NULL; /* can only initialize once! */
315 memset (&plugin, 0, sizeof(struct Plugin));
317 if (GNUNET_OK != database_setup (&plugin))
319 database_shutdown (&plugin);
322 api = GNUNET_new (struct GNUNET_NAMECACHE_PluginFunctions);
324 api->cache_block = &namecache_postgres_cache_block;
325 api->lookup_block = &namecache_postgres_lookup_block;
326 LOG (GNUNET_ERROR_TYPE_INFO,
327 "Postgres namecache plugin running\n");
333 * Exit point from the plugin.
335 * @param cls the plugin context (as returned by "init")
336 * @return always NULL
339 libgnunet_plugin_namecache_postgres_done (void *cls)
341 struct GNUNET_NAMECACHE_PluginFunctions *api = cls;
342 struct Plugin *plugin = api->cls;
344 database_shutdown (plugin);
347 LOG (GNUNET_ERROR_TYPE_DEBUG,
348 "Postgres namecache plugin is finished\n");
353 /* end of plugin_namecache_postgres.c */