2 * This file is part of GNUnet
3 * Copyright (C) 2009-2015 Christian Grothoff (and other contributing authors)
5 * GNUnet is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published
7 * by the Free Software Foundation; either version 3, or (at your
8 * 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 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with GNUnet; see the file COPYING. If not, write to the
17 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
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.
39 const struct GNUNET_CONFIGURATION_Handle *cfg;
49 struct GNUNET_CONTAINER_MultiHashMap *hm;
58 struct GNUNET_GNSRECORD_Block *block;
63 struct GNUNET_HashCode query;
68 * Initialize the database connections and associated
69 * data structures (create tables and indices
72 * @param plugin the plugin context (state for this module)
73 * @return #GNUNET_OK on success
76 database_setup (struct Plugin *plugin)
85 struct FlatFileEntry *entry;
86 struct GNUNET_DISK_FileHandle *fh;
89 GNUNET_CONFIGURATION_get_value_filename (plugin->cfg, "namecache-flat",
92 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
93 "namecache-flat", "FILENAME");
96 if (GNUNET_OK != GNUNET_DISK_file_test (afsdir))
98 if (GNUNET_OK != GNUNET_DISK_directory_create_for_file (afsdir))
101 GNUNET_free (afsdir);
102 return GNUNET_SYSERR;
105 /* afsdir should be UTF-8-encoded. If it isn't, it's a bug */
108 /* Load data from file into hashmap */
109 plugin->hm = GNUNET_CONTAINER_multihashmap_create (10,
111 fh = GNUNET_DISK_file_open (afsdir,
112 GNUNET_DISK_OPEN_CREATE,
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 return GNUNET_SYSERR;
134 buffer = GNUNET_malloc (size);
136 if (GNUNET_SYSERR == GNUNET_DISK_file_read (fh,
140 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
141 _("Unable to read file: %s.\n"),
143 return GNUNET_SYSERR;
146 GNUNET_DISK_file_close (fh);
148 line = strtok ("\n", buffer);
149 while (line != NULL) {
150 query = strtok (",", line);
151 block = strtok (NULL, line);
152 line = strtok ("\n", buffer);
153 entry = GNUNET_malloc (sizeof (struct FlatFileEntry));
154 GNUNET_CRYPTO_hash_from_string (query,
156 GNUNET_STRINGS_base64_decode (block,
159 entry->block = (struct GNUNET_GNSRECORD_Block *) block_buffer;
161 GNUNET_CONTAINER_multihashmap_put (plugin->hm,
164 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
170 GNUNET_free (buffer);
175 * Store values in hashmap in file and free data
177 * @param plugin the plugin context
180 store_and_free_entries (void *cls,
181 const struct GNUNET_HashCode *key,
184 struct GNUNET_DISK_FileHandle *fh = cls;
185 struct FlatFileEntry *entry = value;
189 struct GNUNET_CRYPTO_HashAsciiEncoded query;
192 block_size = ntohl (entry->block->purpose.size) +
193 sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey) +
194 sizeof (struct GNUNET_CRYPTO_EcdsaSignature);
196 GNUNET_STRINGS_base64_encode ((char*)entry->block,
199 GNUNET_CRYPTO_hash_to_enc (&entry->query,
201 GNUNET_asprintf (&line,
206 GNUNET_free (block_b64);
208 GNUNET_DISK_file_write (fh,
212 GNUNET_free (entry->block);
218 * Shutdown database connection and associate data
220 * @param plugin the plugin context (state for this module)
223 database_shutdown (struct Plugin *plugin)
225 struct GNUNET_DISK_FileHandle *fh;
226 fh = GNUNET_DISK_file_open (plugin->fn,
227 GNUNET_DISK_OPEN_CREATE |
228 GNUNET_DISK_OPEN_TRUNCATE,
229 GNUNET_DISK_PERM_USER_WRITE |
230 GNUNET_DISK_PERM_USER_READ);
233 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
234 _("Unable to initialize file: %s.\n"),
239 GNUNET_CONTAINER_multihashmap_iterate (plugin->hm,
240 &store_and_free_entries,
242 GNUNET_CONTAINER_multihashmap_destroy (plugin->hm);
243 GNUNET_DISK_file_close (fh);
248 expire_blocks (void *cls,
249 const struct GNUNET_HashCode *key,
252 struct Plugin *plugin = cls;
253 struct FlatFileEntry *entry = value;
254 struct GNUNET_TIME_Absolute now;
255 struct GNUNET_TIME_Absolute expiration;
257 now = GNUNET_TIME_absolute_get ();
258 expiration = GNUNET_TIME_absolute_ntoh (entry->block->expiration_time);
260 if (0 == GNUNET_TIME_absolute_get_difference (now,
261 expiration).rel_value_us)
263 GNUNET_CONTAINER_multihashmap_remove_all (plugin->hm, key);
271 * Removes any expired block.
273 * @param plugin the plugin
276 namecache_expire_blocks (struct Plugin *plugin)
278 GNUNET_CONTAINER_multihashmap_iterate (plugin->hm,
285 * Cache a block in the datastore.
287 * @param cls closure (internal context for the plugin)
288 * @param block block to cache
289 * @return #GNUNET_OK on success, else #GNUNET_SYSERR
292 namecache_cache_block (void *cls,
293 const struct GNUNET_GNSRECORD_Block *block)
295 struct Plugin *plugin = cls;
296 struct GNUNET_HashCode query;
297 struct FlatFileEntry *entry;
300 namecache_expire_blocks (plugin);
301 GNUNET_CRYPTO_hash (&block->derived_key,
302 sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey),
304 block_size = ntohl (block->purpose.size) +
305 sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey) +
306 sizeof (struct GNUNET_CRYPTO_EcdsaSignature);
307 if (block_size > 64 * 65536)
310 return GNUNET_SYSERR;
312 entry = GNUNET_malloc (sizeof (struct FlatFileEntry));
313 entry->block = GNUNET_malloc (block_size);
314 memcpy (entry->block, block, block_size);
315 GNUNET_CONTAINER_multihashmap_remove_all (plugin->hm, &query);
317 GNUNET_CONTAINER_multihashmap_put (plugin->hm,
320 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
324 return GNUNET_SYSERR;
326 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
327 "Caching block under derived key `%s'\n",
328 GNUNET_h2s_full (&query));
334 * Get the block for a particular zone and label in the
335 * datastore. Will return at most one result to the iterator.
337 * @param cls closure (internal context for the plugin)
338 * @param query hash of public key derived from the zone and the label
339 * @param iter function to call with the result
340 * @param iter_cls closure for @a iter
341 * @return #GNUNET_OK on success, #GNUNET_NO if there were no results, #GNUNET_SYSERR on error
344 namecache_lookup_block (void *cls,
345 const struct GNUNET_HashCode *query,
346 GNUNET_NAMECACHE_BlockCallback iter, void *iter_cls)
348 struct Plugin *plugin = cls;
349 const struct GNUNET_GNSRECORD_Block *block;
351 block = GNUNET_CONTAINER_multihashmap_get (plugin->hm, query);
354 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
355 "Found block under derived key `%s'\n",
356 GNUNET_h2s_full (query));
357 iter (iter_cls, block);
363 * Entry point for the plugin.
365 * @param cls the "struct GNUNET_NAMECACHE_PluginEnvironment*"
366 * @return NULL on error, otherwise the plugin context
369 libgnunet_plugin_namecache_flat_init (void *cls)
371 static struct Plugin plugin;
372 const struct GNUNET_CONFIGURATION_Handle *cfg = cls;
373 struct GNUNET_NAMECACHE_PluginFunctions *api;
375 if (NULL != plugin.cfg)
376 return NULL; /* can only initialize once! */
377 memset (&plugin, 0, sizeof (struct Plugin));
379 if (GNUNET_OK != database_setup (&plugin))
381 database_shutdown (&plugin);
384 api = GNUNET_new (struct GNUNET_NAMECACHE_PluginFunctions);
386 api->cache_block = &namecache_cache_block;
387 api->lookup_block = &namecache_lookup_block;
388 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
389 _("flat plugin running\n"));
395 * Exit point from the plugin.
397 * @param cls the plugin context (as returned by "init")
398 * @return always NULL
401 libgnunet_plugin_namecache_flat_done (void *cls)
403 struct GNUNET_NAMECACHE_PluginFunctions *api = cls;
404 struct Plugin *plugin = api->cls;
406 database_shutdown (plugin);
409 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
410 "flat plugin is finished\n");
414 /* end of plugin_namecache_sqlite.c */