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
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 namestore/plugin_namestore_flat.c
23 * @brief file-based namestore backend
24 * @author Martin Schanzenbach
28 #include "gnunet_namestore_plugin.h"
29 #include "gnunet_namestore_service.h"
30 #include "gnunet_gnsrecord_lib.h"
31 #include "namestore.h"
34 * Context for all functions in this plugin.
39 const struct GNUNET_CONFIGURATION_Handle *cfg;
49 struct GNUNET_CONTAINER_MultiHashMap *hm;
59 uint32_t target_offset;
69 GNUNET_NAMESTORE_RecordIterator iter;
74 const struct GNUNET_CRYPTO_EcdsaPrivateKey *iter_zone;
77 * PKEY to look for in zone to name
79 struct GNUNET_CRYPTO_EcdsaPublicKey *iter_pkey;
82 * Iteration result found
84 int iter_result_found;
93 struct GNUNET_CRYPTO_EcdsaPrivateKey *private_key;
98 uint32_t record_count;
108 struct GNUNET_GNSRECORD_Data *record_data;
120 * Initialize the database connections and associated
121 * data structures (create tables and indices
122 * as needed as well).
124 * @param plugin the plugin context (state for this module)
125 * @return #GNUNET_OK on success
128 database_setup (struct Plugin *plugin)
133 char *zone_private_key;
134 char *record_data_b64;
140 size_t record_data_size;
143 struct GNUNET_HashCode hkey;
144 struct GNUNET_DISK_FileHandle *fh;
145 struct FlatFileEntry *entry;
148 GNUNET_CONFIGURATION_get_value_filename (plugin->cfg,
150 "FILENAME", &afsdir))
152 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
153 "namestore-flat", "FILENAME");
154 return GNUNET_SYSERR;
156 if (GNUNET_OK != GNUNET_DISK_file_test (afsdir))
158 if (GNUNET_OK != GNUNET_DISK_directory_create_for_file (afsdir))
161 GNUNET_free (afsdir);
162 return GNUNET_SYSERR;
165 /* afsdir should be UTF-8-encoded. If it isn't, it's a bug */
168 /* Load data from file into hashmap */
169 plugin->hm = GNUNET_CONTAINER_multihashmap_create (10,
171 fh = GNUNET_DISK_file_open (afsdir,
172 GNUNET_DISK_OPEN_CREATE |
173 GNUNET_DISK_OPEN_READWRITE,
174 GNUNET_DISK_PERM_USER_WRITE |
175 GNUNET_DISK_PERM_USER_READ);
178 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
179 _("Unable to initialize file: %s.\n"),
181 return GNUNET_SYSERR;
184 GNUNET_DISK_file_size (afsdir,
189 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
190 _("Unable to get filesize: %s.\n"),
192 GNUNET_DISK_file_close (fh);
193 return GNUNET_SYSERR;
196 buffer = GNUNET_malloc (size + 1);
198 GNUNET_DISK_file_read (fh,
202 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
203 _("Unable to read file: %s.\n"),
205 GNUNET_free (buffer);
206 GNUNET_DISK_file_close (fh);
207 return GNUNET_SYSERR;
210 GNUNET_DISK_file_close (fh);
214 line = strtok (buffer, "\n");
215 while (line != NULL) {
216 zone_private_key = strtok (line, ",");
217 if (NULL == zone_private_key)
219 rvalue = strtok (NULL, ",");
222 record_count = strtok (NULL, ",");
223 if (NULL == record_count)
225 record_data_b64 = strtok (NULL, ",");
226 if (NULL == record_data_b64)
228 label = strtok (NULL, ",");
231 line = strtok (NULL, "\n");
232 entry = GNUNET_malloc (sizeof (struct FlatFileEntry));
233 if (1 != sscanf (rvalue, "%lu", &entry->rvalue))
235 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
236 "Error parsing entry\n");
240 if (1 != sscanf (record_count, "%u", &entry->record_count))
242 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
243 "Error parsing entry\n");
247 entry->label = GNUNET_strdup (label);
248 record_data_size = GNUNET_STRINGS_base64_decode (record_data_b64,
249 strlen (record_data_b64),
252 GNUNET_malloc (sizeof (struct GNUNET_GNSRECORD_Data) * entry->record_count);
253 if (GNUNET_OK != GNUNET_GNSRECORD_records_deserialize (record_data_size,
258 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
259 "Unable to deserialize record %s\n", label);
260 GNUNET_free (entry->label);
262 GNUNET_free (record_data);
265 GNUNET_free (record_data);
266 GNUNET_STRINGS_base64_decode (zone_private_key,
267 strlen (zone_private_key),
268 (char**)&entry->private_key);
269 key_len = strlen (label) + sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey);
270 key = GNUNET_malloc (strlen (label) + sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey));
271 GNUNET_memcpy (key, label, strlen (label));
272 GNUNET_memcpy (key+strlen(label),
274 sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey));
275 GNUNET_CRYPTO_hash (key,
280 GNUNET_CONTAINER_multihashmap_put (plugin->hm,
283 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
290 GNUNET_free (buffer);
296 * Store values in hashmap in file and free data
298 * @param plugin the plugin context
301 store_and_free_entries (void *cls,
302 const struct GNUNET_HashCode *key,
305 struct GNUNET_DISK_FileHandle *fh = cls;
306 struct FlatFileEntry *entry = value;
308 char *zone_private_key;
309 char *record_data_b64;
312 GNUNET_STRINGS_base64_encode ((char*)entry->private_key,
313 sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey),
315 data_size = GNUNET_GNSRECORD_records_get_size (entry->record_count,
318 char data[data_size];
321 GNUNET_GNSRECORD_records_serialize (entry->record_count,
327 GNUNET_free (zone_private_key);
328 return GNUNET_SYSERR;
330 GNUNET_STRINGS_base64_encode (data,
334 GNUNET_asprintf (&line,
341 GNUNET_free (record_data_b64);
342 GNUNET_free (zone_private_key);
344 GNUNET_DISK_file_write (fh,
349 GNUNET_free (entry->private_key);
350 GNUNET_free (entry->label);
351 GNUNET_free (entry->record_data);
358 * Shutdown database connection and associate data
360 * @param plugin the plugin context (state for this module)
363 database_shutdown (struct Plugin *plugin)
365 struct GNUNET_DISK_FileHandle *fh;
366 fh = GNUNET_DISK_file_open (plugin->fn,
367 GNUNET_DISK_OPEN_CREATE |
368 GNUNET_DISK_OPEN_TRUNCATE |
369 GNUNET_DISK_OPEN_READWRITE,
370 GNUNET_DISK_PERM_USER_WRITE |
371 GNUNET_DISK_PERM_USER_READ);
374 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
375 _("Unable to initialize file: %s.\n"),
380 GNUNET_CONTAINER_multihashmap_iterate (plugin->hm,
381 &store_and_free_entries,
383 GNUNET_CONTAINER_multihashmap_destroy (plugin->hm);
384 GNUNET_DISK_file_close (fh);
389 * Store a record in the datastore. Removes any existing record in the
390 * same zone with the same name.
392 * @param cls closure (internal context for the plugin)
393 * @param zone_key private key of the zone
394 * @param label name that is being mapped (at most 255 characters long)
395 * @param rd_count number of entries in @a rd array
396 * @param rd array of records with data to store
397 * @return #GNUNET_OK on success, else #GNUNET_SYSERR
400 namestore_store_records (void *cls,
401 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone_key,
403 unsigned int rd_count,
404 const struct GNUNET_GNSRECORD_Data *rd)
406 struct Plugin *plugin = cls;
410 struct GNUNET_HashCode hkey;
411 struct FlatFileEntry *entry;
414 rvalue = GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK, UINT64_MAX);
415 key_len = strlen (label) + sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey);
416 key = GNUNET_malloc (key_len);
417 GNUNET_memcpy (key, label, strlen (label));
418 GNUNET_memcpy (key+strlen(label),
420 sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey));
421 GNUNET_CRYPTO_hash (key,
425 GNUNET_CONTAINER_multihashmap_remove_all (plugin->hm, &hkey);
429 entry = GNUNET_malloc (sizeof (struct FlatFileEntry));
430 entry->private_key = GNUNET_malloc (sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey));
431 GNUNET_asprintf (&entry->label,
434 GNUNET_memcpy (entry->private_key,
436 sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey));
437 entry->rvalue = rvalue;
438 entry->record_count = rd_count;
439 entry->record_data = GNUNET_malloc (sizeof (struct GNUNET_GNSRECORD_Data) * rd_count);
440 for (i = 0; i < rd_count; i++)
442 entry->record_data[i].expiration_time = rd[i].expiration_time;
443 entry->record_data[i].record_type = rd[i].record_type;
444 entry->record_data[i].flags = rd[i].flags;
445 entry->record_data[i].data_size = rd[i].data_size;
446 entry->record_data[i].data = GNUNET_malloc (rd[i].data_size);
447 GNUNET_memcpy ((char*)entry->record_data[i].data, rd[i].data, rd[i].data_size);
449 return GNUNET_CONTAINER_multihashmap_put (plugin->hm,
452 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
459 * Lookup records in the datastore for which we are the authority.
461 * @param cls closure (internal context for the plugin)
462 * @param zone private key of the zone
463 * @param label name of the record in the zone
464 * @param iter function to call with the result
465 * @param iter_cls closure for @a iter
466 * @return #GNUNET_OK on success, else #GNUNET_SYSERR
469 namestore_lookup_records (void *cls,
470 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
472 GNUNET_NAMESTORE_RecordIterator iter,
475 struct Plugin *plugin = cls;
476 struct FlatFileEntry *entry;
477 struct GNUNET_HashCode hkey;
483 return GNUNET_SYSERR;
485 key_len = strlen (label) + sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey);
486 key = GNUNET_malloc (key_len);
487 GNUNET_memcpy (key, label, strlen (label));
488 GNUNET_memcpy (key+strlen(label),
490 sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey));
491 GNUNET_CRYPTO_hash (key,
496 entry = GNUNET_CONTAINER_multihashmap_get (plugin->hm, &hkey);
501 iter (iter_cls, entry->private_key, entry->label, entry->record_count, entry->record_data);
507 iterate_zones (void *cls,
508 const struct GNUNET_HashCode *key,
511 struct Plugin *plugin = cls;
512 struct FlatFileEntry *entry = value;
514 if ((plugin->target_offset > plugin->offset) ||
515 ( (NULL != plugin->iter_zone) &&
516 (0 != memcmp (entry->private_key,
518 sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey))))) {
522 plugin->iter (plugin->iter_cls,
527 plugin->iter_result_found = GNUNET_YES;
532 * Iterate over the results for a particular key and zone in the
533 * datastore. Will return at most one result to the iterator.
535 * @param cls closure (internal context for the plugin)
536 * @param zone hash of public key of the zone, NULL to iterate over all zones
537 * @param offset offset in the list of all matching records
538 * @param iter function to call with the result
539 * @param iter_cls closure for @a iter
540 * @return #GNUNET_OK on success, #GNUNET_NO if there were no results, #GNUNET_SYSERR on error
543 namestore_iterate_records (void *cls,
544 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
546 GNUNET_NAMESTORE_RecordIterator iter, void *iter_cls)
548 struct Plugin *plugin = cls;
549 plugin->target_offset = offset;
552 plugin->iter_cls = iter_cls;
553 plugin->iter_zone = zone;
554 plugin->iter_result_found = GNUNET_NO;
555 GNUNET_CONTAINER_multihashmap_iterate (plugin->hm,
558 return plugin->iter_result_found;
562 zone_to_name (void *cls,
563 const struct GNUNET_HashCode *key,
566 struct Plugin *plugin = cls;
567 struct FlatFileEntry *entry = value;
570 if (0 != memcmp (entry->private_key,
572 sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey)))
575 for (i = 0; i < entry->record_count; i++) {
576 if (GNUNET_GNSRECORD_TYPE_PKEY != entry->record_data[i].record_type)
578 if (0 == memcmp (plugin->iter_pkey,
579 entry->record_data[i].data,
580 sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey)))
582 plugin->iter (plugin->iter_cls,
587 plugin->iter_result_found = GNUNET_YES;
596 * Look for an existing PKEY delegation record for a given public key.
597 * Returns at most one result to the iterator.
599 * @param cls closure (internal context for the plugin)
600 * @param zone private key of the zone to look up in, never NULL
601 * @param value_zone public key of the target zone (value), never NULL
602 * @param iter function to call with the result
603 * @param iter_cls closure for @a iter
604 * @return #GNUNET_OK on success, #GNUNET_NO if there were no results, #GNUNET_SYSERR on error
607 namestore_zone_to_name (void *cls,
608 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
609 const struct GNUNET_CRYPTO_EcdsaPublicKey *value_zone,
610 GNUNET_NAMESTORE_RecordIterator iter, void *iter_cls)
612 struct Plugin *plugin = cls;
614 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
615 "Performing reverse lookup for `%s'\n",
616 GNUNET_GNSRECORD_z2s (value_zone));
618 GNUNET_CONTAINER_multihashmap_iterate (plugin->hm,
623 return plugin->iter_result_found;
628 * Entry point for the plugin.
630 * @param cls the "struct GNUNET_NAMESTORE_PluginEnvironment*"
631 * @return NULL on error, otherwise the plugin context
634 libgnunet_plugin_namestore_flat_init (void *cls)
636 static struct Plugin plugin;
637 const struct GNUNET_CONFIGURATION_Handle *cfg = cls;
638 struct GNUNET_NAMESTORE_PluginFunctions *api;
640 if (NULL != plugin.cfg)
641 return NULL; /* can only initialize once! */
642 memset (&plugin, 0, sizeof (struct Plugin));
644 if (GNUNET_OK != database_setup (&plugin))
646 database_shutdown (&plugin);
649 api = GNUNET_new (struct GNUNET_NAMESTORE_PluginFunctions);
651 api->store_records = &namestore_store_records;
652 api->iterate_records = &namestore_iterate_records;
653 api->zone_to_name = &namestore_zone_to_name;
654 api->lookup_records = &namestore_lookup_records;
655 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
656 _("flat file database running\n"));
662 * Exit point from the plugin.
664 * @param cls the plugin context (as returned by "init")
665 * @return always NULL
668 libgnunet_plugin_namestore_flat_done (void *cls)
670 struct GNUNET_NAMESTORE_PluginFunctions *api = cls;
671 struct Plugin *plugin = api->cls;
673 database_shutdown (plugin);
676 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
677 "flat file plugin is finished\n");
681 /* end of plugin_namestore_flat.c */