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 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;
125 * Initialize the database connections and associated
126 * data structures (create tables and indices
127 * as needed as well).
129 * @param plugin the plugin context (state for this module)
130 * @return #GNUNET_OK on success
133 database_setup (struct Plugin *plugin)
138 char *zone_private_key;
139 char *record_data_b64;
145 size_t record_data_size;
147 struct GNUNET_HashCode hkey;
148 struct GNUNET_DISK_FileHandle *fh;
149 struct FlatFileEntry *entry;
152 GNUNET_CONFIGURATION_get_value_filename (plugin->cfg,
154 "FILENAME", &afsdir))
156 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
157 "namestore-flat", "FILENAME");
158 return GNUNET_SYSERR;
160 if (GNUNET_OK != GNUNET_DISK_file_test (afsdir))
162 if (GNUNET_OK != GNUNET_DISK_directory_create_for_file (afsdir))
165 GNUNET_free (afsdir);
166 return GNUNET_SYSERR;
169 /* afsdir should be UTF-8-encoded. If it isn't, it's a bug */
172 /* Load data from file into hashmap */
173 plugin->hm = GNUNET_CONTAINER_multihashmap_create (10,
175 fh = GNUNET_DISK_file_open (afsdir,
176 GNUNET_DISK_OPEN_CREATE |
177 GNUNET_DISK_OPEN_READWRITE,
178 GNUNET_DISK_PERM_USER_WRITE |
179 GNUNET_DISK_PERM_USER_READ);
182 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
183 _("Unable to initialize file: %s.\n"),
185 return GNUNET_SYSERR;
188 if (GNUNET_SYSERR == GNUNET_DISK_file_size (afsdir,
193 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
194 _("Unable to get filesize: %s.\n"),
196 return GNUNET_SYSERR;
199 buffer = GNUNET_malloc (size);
201 if (GNUNET_SYSERR == GNUNET_DISK_file_read (fh,
205 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
206 _("Unable to read file: %s.\n"),
208 return GNUNET_SYSERR;
211 GNUNET_DISK_file_close (fh);
213 line = strtok (buffer, "\n");
214 while (line != NULL) {
215 zone_private_key = strtok (line, ",");
216 if (NULL == zone_private_key)
218 rvalue = strtok (NULL, ",");
221 record_count = strtok (NULL, ",");
222 if (NULL == record_count)
224 record_data_b64 = strtok (NULL, ",");
225 if (NULL == record_data_b64)
227 label = strtok (NULL, ",");
230 line = strtok (NULL, "\n");
231 entry = GNUNET_malloc (sizeof (struct FlatFileEntry));
232 sscanf (rvalue, "%lu", &entry->rvalue);
233 sscanf (record_count, "%u", &entry->record_count);
234 entry->label = GNUNET_strdup (label);
235 record_data_size = GNUNET_STRINGS_base64_decode (record_data_b64,
236 strlen (record_data_b64),
239 GNUNET_malloc (sizeof (struct GNUNET_GNSRECORD_Data) * entry->record_count);
240 GNUNET_GNSRECORD_records_deserialize (record_data_size,
244 GNUNET_free (record_data);
245 GNUNET_STRINGS_base64_decode (zone_private_key,
246 strlen (zone_private_key),
247 (char**)&entry->private_key);
248 key_str = GNUNET_malloc (strlen (label) + sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey));
249 memcpy (key_str, label, strlen (label));
250 memcpy (key_str+strlen(label),
252 sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey));
253 GNUNET_CRYPTO_hash (key_str,
256 GNUNET_free (key_str);
258 GNUNET_CONTAINER_multihashmap_put (plugin->hm,
261 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
268 GNUNET_free (buffer);
274 * Store values in hashmap in file and free data
276 * @param plugin the plugin context
279 store_and_free_entries (void *cls,
280 const struct GNUNET_HashCode *key,
283 struct GNUNET_DISK_FileHandle *fh = cls;
284 struct FlatFileEntry *entry = value;
286 char *zone_private_key;
289 char *record_data_b64;
291 GNUNET_STRINGS_base64_encode ((char*)entry->private_key,
292 sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey),
294 GNUNET_asprintf (&rvalue, "%lu", entry->rvalue);
295 GNUNET_asprintf (&record_count, "%u", entry->record_count);
297 GNUNET_STRINGS_base64_encode ((char*)entry->record_data,
301 GNUNET_asprintf (&line,
309 GNUNET_free (rvalue);
310 GNUNET_free (record_count);
311 GNUNET_free (record_data_b64);
313 GNUNET_DISK_file_write (fh,
317 GNUNET_free (entry->private_key);
318 GNUNET_free (entry->label);
319 GNUNET_free (entry->record_data);
325 * Shutdown database connection and associate data
327 * @param plugin the plugin context (state for this module)
330 database_shutdown (struct Plugin *plugin)
332 struct GNUNET_DISK_FileHandle *fh;
333 fh = GNUNET_DISK_file_open (plugin->fn,
334 GNUNET_DISK_OPEN_CREATE |
335 GNUNET_DISK_OPEN_TRUNCATE |
336 GNUNET_DISK_OPEN_READWRITE,
337 GNUNET_DISK_PERM_USER_WRITE |
338 GNUNET_DISK_PERM_USER_READ);
341 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
342 _("Unable to initialize file: %s.\n"),
347 GNUNET_CONTAINER_multihashmap_iterate (plugin->hm,
348 &store_and_free_entries,
350 GNUNET_CONTAINER_multihashmap_destroy (plugin->hm);
351 GNUNET_DISK_file_close (fh);
356 * Store a record in the datastore. Removes any existing record in the
357 * same zone with the same name.
359 * @param cls closure (internal context for the plugin)
360 * @param zone_key private key of the zone
361 * @param label name that is being mapped (at most 255 characters long)
362 * @param rd_count number of entries in @a rd array
363 * @param rd array of records with data to store
364 * @return #GNUNET_OK on success, else #GNUNET_SYSERR
367 namestore_store_records (void *cls,
368 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone_key,
370 unsigned int rd_count,
371 const struct GNUNET_GNSRECORD_Data *rd)
373 struct Plugin *plugin = cls;
377 struct GNUNET_HashCode hkey;
378 struct FlatFileEntry *entry;
380 rvalue = GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK, UINT64_MAX);
381 data_size = GNUNET_GNSRECORD_records_get_size (rd_count, rd);
382 if (data_size > 64 * 65536)
385 return GNUNET_SYSERR;
387 char data[data_size];
389 if (data_size != GNUNET_GNSRECORD_records_serialize (rd_count, rd,
393 return GNUNET_SYSERR;
396 key_str = GNUNET_malloc (strlen (label) + sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey));
397 memcpy (key_str, label, strlen (label));
398 memcpy (key_str+strlen(label),
400 sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey));
401 GNUNET_CRYPTO_hash (key_str,
405 GNUNET_CONTAINER_multihashmap_remove_all (plugin->hm, &hkey);
409 entry = GNUNET_malloc (sizeof (struct FlatFileEntry));
410 entry->private_key = GNUNET_malloc (sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey));
411 GNUNET_asprintf (&entry->label,
414 memcpy (entry->private_key,
416 sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey));
417 entry->rvalue = rvalue;
418 entry->record_count = rd_count;
419 entry->data_size = data_size;
420 entry->record_data = GNUNET_malloc (data_size);
421 memcpy (entry->record_data, data, data_size);
422 return GNUNET_CONTAINER_multihashmap_put (plugin->hm,
425 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
432 * Lookup records in the datastore for which we are the authority.
434 * @param cls closure (internal context for the plugin)
435 * @param zone private key of the zone
436 * @param label name of the record in the zone
437 * @param iter function to call with the result
438 * @param iter_cls closure for @a iter
439 * @return #GNUNET_OK on success, else #GNUNET_SYSERR
442 namestore_lookup_records (void *cls,
443 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
445 GNUNET_NAMESTORE_RecordIterator iter,
448 struct Plugin *plugin = cls;
449 struct FlatFileEntry *entry;
450 struct GNUNET_HashCode hkey;
455 return GNUNET_SYSERR;
457 key_str = GNUNET_malloc (strlen (label) + sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey));
458 memcpy (key_str, label, strlen (label));
459 memcpy (key_str+strlen(label),
461 sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey));
462 GNUNET_CRYPTO_hash (key_str,
465 GNUNET_free (key_str);
467 entry = GNUNET_CONTAINER_multihashmap_get (plugin->hm, &hkey);
472 iter (iter_cls, entry->private_key, entry->label, entry->record_count, entry->record_data);
478 iterate_zones (void *cls,
479 const struct GNUNET_HashCode *key,
482 struct Plugin *plugin = cls;
483 struct FlatFileEntry *entry = value;
486 if ((plugin->target_offset > plugin->offset) ||
487 (0 != memcmp (entry->private_key,
489 sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey)))) {
494 plugin->iter (plugin->iter_cls,
499 plugin->iter_result_found = GNUNET_YES;
504 * Iterate over the results for a particular key and zone in the
505 * datastore. Will return at most one result to the iterator.
507 * @param cls closure (internal context for the plugin)
508 * @param zone hash of public key of the zone, NULL to iterate over all zones
509 * @param offset offset in the list of all matching records
510 * @param iter function to call with the result
511 * @param iter_cls closure for @a iter
512 * @return #GNUNET_OK on success, #GNUNET_NO if there were no results, #GNUNET_SYSERR on error
515 namestore_iterate_records (void *cls,
516 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
518 GNUNET_NAMESTORE_RecordIterator iter, void *iter_cls)
520 struct Plugin *plugin = cls;
521 plugin->target_offset = offset;
524 plugin->iter_cls = cls;
525 plugin->iter_zone = zone;
526 plugin->iter_result_found = GNUNET_NO;
527 GNUNET_CONTAINER_multihashmap_iterate (plugin->hm,
530 return plugin->iter_result_found;
534 zone_to_name (void *cls,
535 const struct GNUNET_HashCode *key,
538 struct Plugin *plugin = cls;
539 struct FlatFileEntry *entry = value;
542 if (0 != memcmp (entry->private_key,
544 sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey)))
547 for (i = 0; i < entry->record_count; i++) {
548 if (GNUNET_GNSRECORD_TYPE_PKEY != entry->record_data[i].record_type)
550 if (0 == memcmp (plugin->iter_pkey,
551 entry->record_data[i].data,
552 sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey)))
554 plugin->iter (plugin->iter_cls,
559 plugin->iter_result_found = GNUNET_YES;
568 * Look for an existing PKEY delegation record for a given public key.
569 * Returns at most one result to the iterator.
571 * @param cls closure (internal context for the plugin)
572 * @param zone private key of the zone to look up in, never NULL
573 * @param value_zone public key of the target zone (value), never NULL
574 * @param iter function to call with the result
575 * @param iter_cls closure for @a iter
576 * @return #GNUNET_OK on success, #GNUNET_NO if there were no results, #GNUNET_SYSERR on error
579 namestore_zone_to_name (void *cls,
580 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
581 const struct GNUNET_CRYPTO_EcdsaPublicKey *value_zone,
582 GNUNET_NAMESTORE_RecordIterator iter, void *iter_cls)
584 struct Plugin *plugin = cls;
586 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
587 "Performing reverse lookup for `%s'\n",
588 GNUNET_GNSRECORD_z2s (value_zone));
590 GNUNET_CONTAINER_multihashmap_iterate (plugin->hm,
595 return plugin->iter_result_found;
600 * Entry point for the plugin.
602 * @param cls the "struct GNUNET_NAMESTORE_PluginEnvironment*"
603 * @return NULL on error, otherwise the plugin context
606 libgnunet_plugin_namestore_flat_init (void *cls)
608 static struct Plugin plugin;
609 const struct GNUNET_CONFIGURATION_Handle *cfg = cls;
610 struct GNUNET_NAMESTORE_PluginFunctions *api;
612 if (NULL != plugin.cfg)
613 return NULL; /* can only initialize once! */
614 memset (&plugin, 0, sizeof (struct Plugin));
616 if (GNUNET_OK != database_setup (&plugin))
618 database_shutdown (&plugin);
621 api = GNUNET_new (struct GNUNET_NAMESTORE_PluginFunctions);
623 api->store_records = &namestore_store_records;
624 api->iterate_records = &namestore_iterate_records;
625 api->zone_to_name = &namestore_zone_to_name;
626 api->lookup_records = &namestore_lookup_records;
627 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
628 _("flat file database running\n"));
634 * Exit point from the plugin.
636 * @param cls the plugin context (as returned by "init")
637 * @return always NULL
640 libgnunet_plugin_namestore_flat_done (void *cls)
642 struct GNUNET_NAMESTORE_PluginFunctions *api = cls;
643 struct Plugin *plugin = api->cls;
645 database_shutdown (plugin);
648 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
649 "flat file plugin is finished\n");
653 /* end of plugin_namestore_flat.c */