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_OPEN_READWRITE,
114 GNUNET_DISK_PERM_USER_WRITE |
115 GNUNET_DISK_PERM_USER_READ);
118 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
119 _("Unable to initialize file: %s.\n"),
121 return GNUNET_SYSERR;
124 if (GNUNET_SYSERR == GNUNET_DISK_file_size (afsdir,
129 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
130 _("Unable to get filesize: %s.\n"),
132 return GNUNET_SYSERR;
135 buffer = GNUNET_malloc (size);
137 if (GNUNET_SYSERR == GNUNET_DISK_file_read (fh,
141 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
142 _("Unable to read file: %s.\n"),
144 return GNUNET_SYSERR;
147 GNUNET_DISK_file_close (fh);
149 line = strtok ("\n", buffer);
150 while (line != NULL) {
151 query = strtok (",", line);
152 block = strtok (NULL, line);
153 line = strtok ("\n", buffer);
154 entry = GNUNET_malloc (sizeof (struct FlatFileEntry));
155 GNUNET_CRYPTO_hash_from_string (query,
157 GNUNET_STRINGS_base64_decode (block,
160 entry->block = (struct GNUNET_GNSRECORD_Block *) block_buffer;
162 GNUNET_CONTAINER_multihashmap_put (plugin->hm,
165 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
171 GNUNET_free (buffer);
176 * Store values in hashmap in file and free data
178 * @param plugin the plugin context
181 store_and_free_entries (void *cls,
182 const struct GNUNET_HashCode *key,
185 struct GNUNET_DISK_FileHandle *fh = cls;
186 struct FlatFileEntry *entry = value;
190 struct GNUNET_CRYPTO_HashAsciiEncoded query;
193 block_size = ntohl (entry->block->purpose.size) +
194 sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey) +
195 sizeof (struct GNUNET_CRYPTO_EcdsaSignature);
197 GNUNET_STRINGS_base64_encode ((char*)entry->block,
200 GNUNET_CRYPTO_hash_to_enc (&entry->query,
202 GNUNET_asprintf (&line,
207 GNUNET_free (block_b64);
209 GNUNET_DISK_file_write (fh,
213 GNUNET_free (entry->block);
219 * Shutdown database connection and associate data
221 * @param plugin the plugin context (state for this module)
224 database_shutdown (struct Plugin *plugin)
226 struct GNUNET_DISK_FileHandle *fh;
227 fh = GNUNET_DISK_file_open (plugin->fn,
228 GNUNET_DISK_OPEN_CREATE |
229 GNUNET_DISK_OPEN_TRUNCATE,
230 GNUNET_DISK_PERM_USER_WRITE |
231 GNUNET_DISK_PERM_USER_READ);
234 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
235 _("Unable to initialize file: %s.\n"),
240 GNUNET_CONTAINER_multihashmap_iterate (plugin->hm,
241 &store_and_free_entries,
243 GNUNET_CONTAINER_multihashmap_destroy (plugin->hm);
244 GNUNET_DISK_file_close (fh);
249 expire_blocks (void *cls,
250 const struct GNUNET_HashCode *key,
253 struct Plugin *plugin = cls;
254 struct FlatFileEntry *entry = value;
255 struct GNUNET_TIME_Absolute now;
256 struct GNUNET_TIME_Absolute expiration;
258 now = GNUNET_TIME_absolute_get ();
259 expiration = GNUNET_TIME_absolute_ntoh (entry->block->expiration_time);
261 if (0 == GNUNET_TIME_absolute_get_difference (now,
262 expiration).rel_value_us)
264 GNUNET_CONTAINER_multihashmap_remove_all (plugin->hm, key);
272 * Removes any expired block.
274 * @param plugin the plugin
277 namecache_expire_blocks (struct Plugin *plugin)
279 GNUNET_CONTAINER_multihashmap_iterate (plugin->hm,
286 * Cache a block in the datastore.
288 * @param cls closure (internal context for the plugin)
289 * @param block block to cache
290 * @return #GNUNET_OK on success, else #GNUNET_SYSERR
293 namecache_cache_block (void *cls,
294 const struct GNUNET_GNSRECORD_Block *block)
296 struct Plugin *plugin = cls;
297 struct GNUNET_HashCode query;
298 struct FlatFileEntry *entry;
301 namecache_expire_blocks (plugin);
302 GNUNET_CRYPTO_hash (&block->derived_key,
303 sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey),
305 block_size = ntohl (block->purpose.size) +
306 sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey) +
307 sizeof (struct GNUNET_CRYPTO_EcdsaSignature);
308 if (block_size > 64 * 65536)
311 return GNUNET_SYSERR;
313 entry = GNUNET_malloc (sizeof (struct FlatFileEntry));
314 entry->block = GNUNET_malloc (block_size);
315 memcpy (entry->block, block, block_size);
316 GNUNET_CONTAINER_multihashmap_remove_all (plugin->hm, &query);
318 GNUNET_CONTAINER_multihashmap_put (plugin->hm,
321 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
325 return GNUNET_SYSERR;
327 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
328 "Caching block under derived key `%s'\n",
329 GNUNET_h2s_full (&query));
335 * Get the block for a particular zone and label in the
336 * datastore. Will return at most one result to the iterator.
338 * @param cls closure (internal context for the plugin)
339 * @param query hash of public key derived from the zone and the label
340 * @param iter function to call with the result
341 * @param iter_cls closure for @a iter
342 * @return #GNUNET_OK on success, #GNUNET_NO if there were no results, #GNUNET_SYSERR on error
345 namecache_lookup_block (void *cls,
346 const struct GNUNET_HashCode *query,
347 GNUNET_NAMECACHE_BlockCallback iter, void *iter_cls)
349 struct Plugin *plugin = cls;
350 const struct GNUNET_GNSRECORD_Block *block;
352 block = GNUNET_CONTAINER_multihashmap_get (plugin->hm, query);
355 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
356 "Found block under derived key `%s'\n",
357 GNUNET_h2s_full (query));
358 iter (iter_cls, block);
364 * Entry point for the plugin.
366 * @param cls the "struct GNUNET_NAMECACHE_PluginEnvironment*"
367 * @return NULL on error, otherwise the plugin context
370 libgnunet_plugin_namecache_flat_init (void *cls)
372 static struct Plugin plugin;
373 const struct GNUNET_CONFIGURATION_Handle *cfg = cls;
374 struct GNUNET_NAMECACHE_PluginFunctions *api;
376 if (NULL != plugin.cfg)
377 return NULL; /* can only initialize once! */
378 memset (&plugin, 0, sizeof (struct Plugin));
380 if (GNUNET_OK != database_setup (&plugin))
382 database_shutdown (&plugin);
385 api = GNUNET_new (struct GNUNET_NAMECACHE_PluginFunctions);
387 api->cache_block = &namecache_cache_block;
388 api->lookup_block = &namecache_lookup_block;
389 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
390 _("flat plugin running\n"));
396 * Exit point from the plugin.
398 * @param cls the plugin context (as returned by "init")
399 * @return always NULL
402 libgnunet_plugin_namecache_flat_done (void *cls)
404 struct GNUNET_NAMECACHE_PluginFunctions *api = cls;
405 struct Plugin *plugin = api->cls;
407 database_shutdown (plugin);
410 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
411 "flat plugin is finished\n");
415 /* end of plugin_namecache_sqlite.c */