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.
17 * @file namecache/plugin_namecache_flat.c
18 * @brief flat file-based namecache backend
19 * @author Martin Schanzenbach
23 #include "gnunet_namecache_plugin.h"
24 #include "gnunet_namecache_service.h"
25 #include "gnunet_gnsrecord_lib.h"
26 #include "namecache.h"
29 * Context for all functions in this plugin.
34 const struct GNUNET_CONFIGURATION_Handle *cfg;
44 struct GNUNET_CONTAINER_MultiHashMap *hm;
53 struct GNUNET_GNSRECORD_Block *block;
58 struct GNUNET_HashCode query;
63 * Initialize the database connections and associated
64 * data structures (create tables and indices
67 * @param plugin the plugin context (state for this module)
68 * @return #GNUNET_OK on success
71 database_setup (struct Plugin *plugin)
80 struct FlatFileEntry *entry;
81 struct GNUNET_DISK_FileHandle *fh;
84 GNUNET_CONFIGURATION_get_value_filename (plugin->cfg,
89 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
90 "namecache-flat", "FILENAME");
93 if (GNUNET_OK != GNUNET_DISK_file_test (afsdir))
95 if (GNUNET_OK != GNUNET_DISK_directory_create_for_file (afsdir))
102 /* afsdir should be UTF-8-encoded. If it isn't, it's a bug */
105 /* Load data from file into hashmap */
106 plugin->hm = GNUNET_CONTAINER_multihashmap_create (10,
108 fh = GNUNET_DISK_file_open (afsdir,
109 GNUNET_DISK_OPEN_CREATE |
110 GNUNET_DISK_OPEN_READWRITE,
111 GNUNET_DISK_PERM_USER_WRITE |
112 GNUNET_DISK_PERM_USER_READ);
115 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
116 _("Unable to initialize file: %s.\n"),
118 return GNUNET_SYSERR;
121 if (GNUNET_SYSERR == GNUNET_DISK_file_size (afsdir,
126 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
127 _("Unable to get filesize: %s.\n"),
129 GNUNET_DISK_file_close (fh);
130 return GNUNET_SYSERR;
135 GNUNET_DISK_file_close (fh);
139 buffer = GNUNET_malloc (size + 1);
141 if (GNUNET_SYSERR == GNUNET_DISK_file_read (fh,
145 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
146 _("Unable to read file: %s.\n"),
148 GNUNET_free (buffer);
149 GNUNET_DISK_file_close (fh);
150 return GNUNET_SYSERR;
154 GNUNET_DISK_file_close (fh);
157 line = strtok (buffer, "\n");
158 while (line != NULL) {
159 query = strtok (line, ",");
162 block = strtok (NULL, ",");
165 line = strtok (NULL, "\n");
166 entry = GNUNET_malloc (sizeof (struct FlatFileEntry));
167 GNUNET_assert (GNUNET_OK == GNUNET_CRYPTO_hash_from_string (query,
169 GNUNET_STRINGS_base64_decode (block,
172 entry->block = (struct GNUNET_GNSRECORD_Block *) block_buffer;
174 GNUNET_CONTAINER_multihashmap_put (plugin->hm,
177 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
184 GNUNET_free (buffer);
189 * Store values in hashmap in file and free data
191 * @param plugin the plugin context
194 store_and_free_entries (void *cls,
195 const struct GNUNET_HashCode *key,
198 struct GNUNET_DISK_FileHandle *fh = cls;
199 struct FlatFileEntry *entry = value;
203 struct GNUNET_CRYPTO_HashAsciiEncoded query;
206 block_size = ntohl (entry->block->purpose.size) +
207 sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey) +
208 sizeof (struct GNUNET_CRYPTO_EcdsaSignature);
210 GNUNET_STRINGS_base64_encode ((char*)entry->block,
213 GNUNET_CRYPTO_hash_to_enc (&entry->query,
215 GNUNET_asprintf (&line,
220 GNUNET_free (block_b64);
222 GNUNET_DISK_file_write (fh,
226 GNUNET_free (entry->block);
233 * Shutdown database connection and associate data
235 * @param plugin the plugin context (state for this module)
238 database_shutdown (struct Plugin *plugin)
240 struct GNUNET_DISK_FileHandle *fh;
241 fh = GNUNET_DISK_file_open (plugin->fn,
242 GNUNET_DISK_OPEN_CREATE |
243 GNUNET_DISK_OPEN_TRUNCATE |
244 GNUNET_DISK_OPEN_READWRITE,
245 GNUNET_DISK_PERM_USER_WRITE |
246 GNUNET_DISK_PERM_USER_READ);
249 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
250 _("Unable to initialize file: %s.\n"),
255 GNUNET_CONTAINER_multihashmap_iterate (plugin->hm,
256 &store_and_free_entries,
258 GNUNET_CONTAINER_multihashmap_destroy (plugin->hm);
259 GNUNET_DISK_file_close (fh);
264 expire_blocks (void *cls,
265 const struct GNUNET_HashCode *key,
268 struct Plugin *plugin = cls;
269 struct FlatFileEntry *entry = value;
270 struct GNUNET_TIME_Absolute now;
271 struct GNUNET_TIME_Absolute expiration;
273 now = GNUNET_TIME_absolute_get ();
274 expiration = GNUNET_TIME_absolute_ntoh (entry->block->expiration_time);
276 if (0 == GNUNET_TIME_absolute_get_difference (now,
277 expiration).rel_value_us)
279 GNUNET_CONTAINER_multihashmap_remove_all (plugin->hm, key);
287 * Removes any expired block.
289 * @param plugin the plugin
292 namecache_expire_blocks (struct Plugin *plugin)
294 GNUNET_CONTAINER_multihashmap_iterate (plugin->hm,
301 * Cache a block in the datastore.
303 * @param cls closure (internal context for the plugin)
304 * @param block block to cache
305 * @return #GNUNET_OK on success, else #GNUNET_SYSERR
308 namecache_cache_block (void *cls,
309 const struct GNUNET_GNSRECORD_Block *block)
311 struct Plugin *plugin = cls;
312 struct GNUNET_HashCode query;
313 struct FlatFileEntry *entry;
316 namecache_expire_blocks (plugin);
317 GNUNET_CRYPTO_hash (&block->derived_key,
318 sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey),
320 block_size = ntohl (block->purpose.size) +
321 sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey) +
322 sizeof (struct GNUNET_CRYPTO_EcdsaSignature);
323 if (block_size > 64 * 65536)
326 return GNUNET_SYSERR;
328 entry = GNUNET_malloc (sizeof (struct FlatFileEntry));
329 entry->block = GNUNET_malloc (block_size);
330 GNUNET_memcpy (entry->block, block, block_size);
331 GNUNET_CONTAINER_multihashmap_remove_all (plugin->hm, &query);
333 GNUNET_CONTAINER_multihashmap_put (plugin->hm,
336 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
340 return GNUNET_SYSERR;
342 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
343 "Caching block under derived key `%s'\n",
344 GNUNET_h2s_full (&query));
350 * Get the block for a particular zone and label in the
351 * datastore. Will return at most one result to the iterator.
353 * @param cls closure (internal context for the plugin)
354 * @param query hash of public key derived from the zone and the label
355 * @param iter function to call with the result
356 * @param iter_cls closure for @a iter
357 * @return #GNUNET_OK on success, #GNUNET_NO if there were no results, #GNUNET_SYSERR on error
360 namecache_lookup_block (void *cls,
361 const struct GNUNET_HashCode *query,
362 GNUNET_NAMECACHE_BlockCallback iter, void *iter_cls)
364 struct Plugin *plugin = cls;
365 const struct GNUNET_GNSRECORD_Block *block;
367 block = GNUNET_CONTAINER_multihashmap_get (plugin->hm, query);
370 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
371 "Found block under derived key `%s'\n",
372 GNUNET_h2s_full (query));
373 iter (iter_cls, block);
379 * Entry point for the plugin.
381 * @param cls the "struct GNUNET_NAMECACHE_PluginEnvironment*"
382 * @return NULL on error, otherwise the plugin context
385 libgnunet_plugin_namecache_flat_init (void *cls)
387 static struct Plugin plugin;
388 const struct GNUNET_CONFIGURATION_Handle *cfg = cls;
389 struct GNUNET_NAMECACHE_PluginFunctions *api;
391 if (NULL != plugin.cfg)
392 return NULL; /* can only initialize once! */
393 memset (&plugin, 0, sizeof (struct Plugin));
395 if (GNUNET_OK != database_setup (&plugin))
397 database_shutdown (&plugin);
400 api = GNUNET_new (struct GNUNET_NAMECACHE_PluginFunctions);
402 api->cache_block = &namecache_cache_block;
403 api->lookup_block = &namecache_lookup_block;
404 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
405 _("flat plugin running\n"));
411 * Exit point from the plugin.
413 * @param cls the plugin context (as returned by "init")
414 * @return always NULL
417 libgnunet_plugin_namecache_flat_done (void *cls)
419 struct GNUNET_NAMECACHE_PluginFunctions *api = cls;
420 struct Plugin *plugin = api->cls;
422 database_shutdown (plugin);
425 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
426 "flat plugin is finished\n");
430 /* end of plugin_namecache_sqlite.c */