2 * This file is part of GNUnet
3 * Copyright (C) 2009-2015 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_flat.c
23 * @brief flat file-based namecache backend
24 * @author Martin Schanzenbach
28 #include "gnunet_namecache_plugin.h"
29 #include "gnunet_namecache_service.h"
30 #include "gnunet_gnsrecord_lib.h"
31 #include "namecache.h"
34 * Context for all functions in this plugin.
38 const struct GNUNET_CONFIGURATION_Handle *cfg;
48 struct GNUNET_CONTAINER_MultiHashMap *hm;
56 struct GNUNET_GNSRECORD_Block *block;
61 struct GNUNET_HashCode query;
65 * Initialize the database connections and associated
66 * data structures (create tables and indices
69 * @param plugin the plugin context (state for this module)
70 * @return #GNUNET_OK on success
73 database_setup (struct Plugin *plugin)
82 struct FlatFileEntry *entry;
83 struct GNUNET_DISK_FileHandle *fh;
86 GNUNET_CONFIGURATION_get_value_filename (plugin->cfg,
91 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
92 "namecache-flat", "FILENAME");
95 if (GNUNET_OK != GNUNET_DISK_file_test (afsdir))
97 if (GNUNET_OK != GNUNET_DISK_directory_create_for_file (afsdir))
100 GNUNET_free (afsdir);
101 return GNUNET_SYSERR;
104 /* afsdir should be UTF-8-encoded. If it isn't, it's a bug */
107 /* Load data from file into hashmap */
108 plugin->hm = GNUNET_CONTAINER_multihashmap_create (10,
110 fh = GNUNET_DISK_file_open (afsdir,
111 GNUNET_DISK_OPEN_CREATE
112 | GNUNET_DISK_OPEN_READWRITE,
113 GNUNET_DISK_PERM_USER_WRITE
114 | GNUNET_DISK_PERM_USER_READ);
117 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
118 _ ("Unable to initialize file: %s.\n"),
120 return GNUNET_SYSERR;
123 if (GNUNET_SYSERR == GNUNET_DISK_file_size (afsdir,
128 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
129 _ ("Unable to get filesize: %s.\n"),
131 GNUNET_DISK_file_close (fh);
132 return GNUNET_SYSERR;
137 GNUNET_DISK_file_close (fh);
141 buffer = GNUNET_malloc (size + 1);
143 if (GNUNET_SYSERR == GNUNET_DISK_file_read (fh,
147 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
148 _ ("Unable to read file: %s.\n"),
150 GNUNET_free (buffer);
151 GNUNET_DISK_file_close (fh);
152 return GNUNET_SYSERR;
156 GNUNET_DISK_file_close (fh);
159 line = strtok (buffer, "\n");
162 query = strtok (line, ",");
165 block = strtok (NULL, ",");
168 line = strtok (NULL, "\n");
169 entry = GNUNET_malloc (sizeof(struct FlatFileEntry));
170 GNUNET_assert (GNUNET_OK == GNUNET_CRYPTO_hash_from_string (query,
172 GNUNET_STRINGS_base64_decode (block,
174 (void**) &block_buffer);
175 entry->block = (struct GNUNET_GNSRECORD_Block *) block_buffer;
177 GNUNET_CONTAINER_multihashmap_put (plugin->hm,
180 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
187 GNUNET_free (buffer);
193 * Store values in hashmap in file and free data
195 * @param plugin the plugin context
198 store_and_free_entries (void *cls,
199 const struct GNUNET_HashCode *key,
202 struct GNUNET_DISK_FileHandle *fh = cls;
203 struct FlatFileEntry *entry = value;
207 struct GNUNET_CRYPTO_HashAsciiEncoded query;
210 block_size = ntohl (entry->block->purpose.size)
211 + sizeof(struct GNUNET_CRYPTO_EcdsaPublicKey)
212 + sizeof(struct GNUNET_CRYPTO_EcdsaSignature);
214 GNUNET_STRINGS_base64_encode ((char*) entry->block,
217 GNUNET_CRYPTO_hash_to_enc (&entry->query,
219 GNUNET_asprintf (&line,
224 GNUNET_free (block_b64);
226 GNUNET_DISK_file_write (fh,
230 GNUNET_free (entry->block);
238 * Shutdown database connection and associate data
240 * @param plugin the plugin context (state for this module)
243 database_shutdown (struct Plugin *plugin)
245 struct GNUNET_DISK_FileHandle *fh;
247 fh = GNUNET_DISK_file_open (plugin->fn,
248 GNUNET_DISK_OPEN_CREATE
249 | GNUNET_DISK_OPEN_TRUNCATE
250 | GNUNET_DISK_OPEN_READWRITE,
251 GNUNET_DISK_PERM_USER_WRITE
252 | GNUNET_DISK_PERM_USER_READ);
255 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
256 _ ("Unable to initialize file: %s.\n"),
261 GNUNET_CONTAINER_multihashmap_iterate (plugin->hm,
262 &store_and_free_entries,
264 GNUNET_CONTAINER_multihashmap_destroy (plugin->hm);
265 GNUNET_DISK_file_close (fh);
270 expire_blocks (void *cls,
271 const struct GNUNET_HashCode *key,
274 struct Plugin *plugin = cls;
275 struct FlatFileEntry *entry = value;
276 struct GNUNET_TIME_Absolute now;
277 struct GNUNET_TIME_Absolute expiration;
279 now = GNUNET_TIME_absolute_get ();
280 expiration = GNUNET_TIME_absolute_ntoh (entry->block->expiration_time);
282 if (0 == GNUNET_TIME_absolute_get_difference (now,
283 expiration).rel_value_us)
285 GNUNET_CONTAINER_multihashmap_remove_all (plugin->hm, key);
292 * Removes any expired block.
294 * @param plugin the plugin
297 namecache_expire_blocks (struct Plugin *plugin)
299 GNUNET_CONTAINER_multihashmap_iterate (plugin->hm,
306 * Cache a block in the datastore.
308 * @param cls closure (internal context for the plugin)
309 * @param block block to cache
310 * @return #GNUNET_OK on success, else #GNUNET_SYSERR
313 namecache_cache_block (void *cls,
314 const struct GNUNET_GNSRECORD_Block *block)
316 struct Plugin *plugin = cls;
317 struct GNUNET_HashCode query;
318 struct FlatFileEntry *entry;
321 namecache_expire_blocks (plugin);
322 GNUNET_CRYPTO_hash (&block->derived_key,
323 sizeof(struct GNUNET_CRYPTO_EcdsaPublicKey),
325 block_size = ntohl (block->purpose.size)
326 + sizeof(struct GNUNET_CRYPTO_EcdsaPublicKey)
327 + sizeof(struct GNUNET_CRYPTO_EcdsaSignature);
328 if (block_size > 64 * 65536)
331 return GNUNET_SYSERR;
333 entry = GNUNET_malloc (sizeof(struct FlatFileEntry));
334 entry->block = GNUNET_malloc (block_size);
335 GNUNET_memcpy (entry->block, block, block_size);
336 GNUNET_CONTAINER_multihashmap_remove_all (plugin->hm, &query);
338 GNUNET_CONTAINER_multihashmap_put (plugin->hm,
341 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
345 return GNUNET_SYSERR;
347 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
348 "Caching block under derived key `%s'\n",
349 GNUNET_h2s_full (&query));
355 * Get the block for a particular zone and label in the
356 * datastore. Will return at most one result to the iterator.
358 * @param cls closure (internal context for the plugin)
359 * @param query hash of public key derived from the zone and the label
360 * @param iter function to call with the result
361 * @param iter_cls closure for @a iter
362 * @return #GNUNET_OK on success, #GNUNET_NO if there were no results, #GNUNET_SYSERR on error
365 namecache_lookup_block (void *cls,
366 const struct GNUNET_HashCode *query,
367 GNUNET_NAMECACHE_BlockCallback iter, void *iter_cls)
369 struct Plugin *plugin = cls;
370 const struct GNUNET_GNSRECORD_Block *block;
372 block = GNUNET_CONTAINER_multihashmap_get (plugin->hm, query);
375 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
376 "Found block under derived key `%s'\n",
377 GNUNET_h2s_full (query));
378 iter (iter_cls, block);
384 * Entry point for the plugin.
386 * @param cls the "struct GNUNET_NAMECACHE_PluginEnvironment*"
387 * @return NULL on error, otherwise the plugin context
390 libgnunet_plugin_namecache_flat_init (void *cls)
392 static struct Plugin plugin;
393 const struct GNUNET_CONFIGURATION_Handle *cfg = cls;
394 struct GNUNET_NAMECACHE_PluginFunctions *api;
396 if (NULL != plugin.cfg)
397 return NULL; /* can only initialize once! */
398 memset (&plugin, 0, sizeof(struct Plugin));
400 if (GNUNET_OK != database_setup (&plugin))
402 database_shutdown (&plugin);
405 api = GNUNET_new (struct GNUNET_NAMECACHE_PluginFunctions);
407 api->cache_block = &namecache_cache_block;
408 api->lookup_block = &namecache_lookup_block;
409 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
410 _ ("flat plugin running\n"));
416 * Exit point from the plugin.
418 * @param cls the plugin context (as returned by "init")
419 * @return always NULL
422 libgnunet_plugin_namecache_flat_done (void *cls)
424 struct GNUNET_NAMECACHE_PluginFunctions *api = cls;
425 struct Plugin *plugin = api->cls;
427 database_shutdown (plugin);
430 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
431 "flat plugin is finished\n");
436 /* end of plugin_namecache_sqlite.c */