aboutsummaryrefslogtreecommitdiff
path: root/src/namestore/plugin_namestore_flat.c
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2018-04-10 13:02:28 +0200
committerChristian Grothoff <christian@grothoff.org>2018-04-10 13:02:51 +0200
commit0cd0e3a5287af73bb81219ebc9548395a8945ce8 (patch)
treeb5bb24d78a4eed519b37e6fb8f9291a6282d06d4 /src/namestore/plugin_namestore_flat.c
parent3eae3e43d6f94449cb89998a676e6696438cc604 (diff)
downloadgnunet-0cd0e3a5287af73bb81219ebc9548395a8945ce8.tar.gz
gnunet-0cd0e3a5287af73bb81219ebc9548395a8945ce8.zip
more work on gnunet-zoneimport, some bugfix in flat namestore, misc. style fixes
Diffstat (limited to 'src/namestore/plugin_namestore_flat.c')
-rw-r--r--src/namestore/plugin_namestore_flat.c142
1 files changed, 93 insertions, 49 deletions
diff --git a/src/namestore/plugin_namestore_flat.c b/src/namestore/plugin_namestore_flat.c
index 923d2696f..024fc34f2 100644
--- a/src/namestore/plugin_namestore_flat.c
+++ b/src/namestore/plugin_namestore_flat.c
@@ -1,6 +1,6 @@
1 /* 1 /*
2 * This file is part of GNUnet 2 * This file is part of GNUnet
3 * Copyright (C) 2009-2015 GNUnet e.V. 3 * Copyright (C) 2009-2015, 2018 GNUnet e.V.
4 * 4 *
5 * GNUnet is free software; you can redistribute it and/or modify 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 6 * it under the terms of the GNU General Public License as published
@@ -22,6 +22,7 @@
22 * @file namestore/plugin_namestore_flat.c 22 * @file namestore/plugin_namestore_flat.c
23 * @brief file-based namestore backend 23 * @brief file-based namestore backend
24 * @author Martin Schanzenbach 24 * @author Martin Schanzenbach
25 * @author Christian Grothoff
25 */ 26 */
26 27
27#include "platform.h" 28#include "platform.h"
@@ -76,7 +77,7 @@ struct Plugin
76 /** 77 /**
77 * PKEY to look for in zone to name 78 * PKEY to look for in zone to name
78 */ 79 */
79 struct GNUNET_CRYPTO_EcdsaPublicKey *iter_pkey; 80 const struct GNUNET_CRYPTO_EcdsaPublicKey *iter_pkey;
80 81
81 /** 82 /**
82 * Iteration result found 83 * Iteration result found
@@ -147,15 +148,19 @@ database_setup (struct Plugin *plugin)
147 if (GNUNET_OK != 148 if (GNUNET_OK !=
148 GNUNET_CONFIGURATION_get_value_filename (plugin->cfg, 149 GNUNET_CONFIGURATION_get_value_filename (plugin->cfg,
149 "namestore-flat", 150 "namestore-flat",
150 "FILENAME", &afsdir)) 151 "FILENAME",
152 &afsdir))
151 { 153 {
152 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, 154 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
153 "namestore-flat", "FILENAME"); 155 "namestore-flat",
156 "FILENAME");
154 return GNUNET_SYSERR; 157 return GNUNET_SYSERR;
155 } 158 }
156 if (GNUNET_OK != GNUNET_DISK_file_test (afsdir)) 159 if (GNUNET_OK !=
160 GNUNET_DISK_file_test (afsdir))
157 { 161 {
158 if (GNUNET_OK != GNUNET_DISK_directory_create_for_file (afsdir)) 162 if (GNUNET_OK !=
163 GNUNET_DISK_directory_create_for_file (afsdir))
159 { 164 {
160 GNUNET_break (0); 165 GNUNET_break (0);
161 GNUNET_free (afsdir); 166 GNUNET_free (afsdir);
@@ -229,15 +234,19 @@ database_setup (struct Plugin *plugin)
229 if (NULL == label) 234 if (NULL == label)
230 break; 235 break;
231 line = strtok (NULL, "\n"); 236 line = strtok (NULL, "\n");
232 entry = GNUNET_malloc (sizeof (struct FlatFileEntry)); 237 entry = GNUNET_new (struct FlatFileEntry);
233 if (1 != sscanf (rvalue, "%lu", &entry->rvalue)) 238 if (1 != sscanf (rvalue,
239 "%lu",
240 &entry->rvalue))
234 { 241 {
235 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 242 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
236 "Error parsing entry\n"); 243 "Error parsing entry\n");
237 GNUNET_free (entry); 244 GNUNET_free (entry);
238 break; 245 break;
239 } 246 }
240 if (1 != sscanf (record_count, "%u", &entry->record_count)) 247 if (1 != sscanf (record_count,
248 "%u",
249 &entry->record_count))
241 { 250 {
242 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 251 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
243 "Error parsing entry\n"); 252 "Error parsing entry\n");
@@ -245,18 +254,22 @@ database_setup (struct Plugin *plugin)
245 break; 254 break;
246 } 255 }
247 entry->label = GNUNET_strdup (label); 256 entry->label = GNUNET_strdup (label);
248 record_data_size = GNUNET_STRINGS_base64_decode (record_data_b64, 257 record_data_size
249 strlen (record_data_b64), 258 = GNUNET_STRINGS_base64_decode (record_data_b64,
250 &record_data); 259 strlen (record_data_b64),
260 &record_data);
251 entry->record_data = 261 entry->record_data =
252 GNUNET_malloc (sizeof (struct GNUNET_GNSRECORD_Data) * entry->record_count); 262 GNUNET_new_array (entry->record_count,
253 if (GNUNET_OK != GNUNET_GNSRECORD_records_deserialize (record_data_size, 263 struct GNUNET_GNSRECORD_Data);
254 record_data, 264 if (GNUNET_OK !=
255 entry->record_count, 265 GNUNET_GNSRECORD_records_deserialize (record_data_size,
256 entry->record_data)) 266 record_data,
267 entry->record_count,
268 entry->record_data))
257 { 269 {
258 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 270 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
259 "Unable to deserialize record %s\n", label); 271 "Unable to deserialize record %s\n",
272 label);
260 GNUNET_free (entry->label); 273 GNUNET_free (entry->label);
261 GNUNET_free (entry); 274 GNUNET_free (entry);
262 GNUNET_free (record_data); 275 GNUNET_free (record_data);
@@ -268,10 +281,12 @@ database_setup (struct Plugin *plugin)
268 (char**)&entry->private_key); 281 (char**)&entry->private_key);
269 key_len = strlen (label) + sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey); 282 key_len = strlen (label) + sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey);
270 key = GNUNET_malloc (strlen (label) + sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey)); 283 key = GNUNET_malloc (strlen (label) + sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey));
271 GNUNET_memcpy (key, label, strlen (label)); 284 GNUNET_memcpy (key,
285 label,
286 strlen (label));
272 GNUNET_memcpy (key+strlen(label), 287 GNUNET_memcpy (key+strlen(label),
273 entry->private_key, 288 entry->private_key,
274 sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey)); 289 sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey));
275 GNUNET_CRYPTO_hash (key, 290 GNUNET_CRYPTO_hash (key,
276 key_len, 291 key_len,
277 &hkey); 292 &hkey);
@@ -296,6 +311,8 @@ database_setup (struct Plugin *plugin)
296 * Store values in hashmap in file and free data 311 * Store values in hashmap in file and free data
297 * 312 *
298 * @param plugin the plugin context 313 * @param plugin the plugin context
314 * @param key key in the map
315 * @param value a `struct FlatFileEntry`
299 */ 316 */
300static int 317static int
301store_and_free_entries (void *cls, 318store_and_free_entries (void *cls,
@@ -309,6 +326,7 @@ store_and_free_entries (void *cls,
309 char *record_data_b64; 326 char *record_data_b64;
310 size_t data_size; 327 size_t data_size;
311 328
329 (void) key;
312 GNUNET_STRINGS_base64_encode ((char*)entry->private_key, 330 GNUNET_STRINGS_base64_encode ((char*)entry->private_key,
313 sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey), 331 sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey),
314 &zone_private_key); 332 &zone_private_key);
@@ -316,12 +334,14 @@ store_and_free_entries (void *cls,
316 entry->record_data); 334 entry->record_data);
317 { 335 {
318 char data[data_size]; 336 char data[data_size];
319 337 ssize_t ret;
320 if (data_size != 338
321 GNUNET_GNSRECORD_records_serialize (entry->record_count, 339 ret = GNUNET_GNSRECORD_records_serialize (entry->record_count,
322 entry->record_data, 340 entry->record_data,
323 data_size, 341 data_size,
324 data)) 342 data);
343 if ( (ret < 0) ||
344 (data_size != (size_t) ret) )
325 { 345 {
326 GNUNET_break (0); 346 GNUNET_break (0);
327 GNUNET_free (zone_private_key); 347 GNUNET_free (zone_private_key);
@@ -363,6 +383,7 @@ static void
363database_shutdown (struct Plugin *plugin) 383database_shutdown (struct Plugin *plugin)
364{ 384{
365 struct GNUNET_DISK_FileHandle *fh; 385 struct GNUNET_DISK_FileHandle *fh;
386
366 fh = GNUNET_DISK_file_open (plugin->fn, 387 fh = GNUNET_DISK_file_open (plugin->fn,
367 GNUNET_DISK_OPEN_CREATE | 388 GNUNET_DISK_OPEN_CREATE |
368 GNUNET_DISK_OPEN_TRUNCATE | 389 GNUNET_DISK_OPEN_TRUNCATE |
@@ -409,9 +430,9 @@ namestore_store_records (void *cls,
409 char *key; 430 char *key;
410 struct GNUNET_HashCode hkey; 431 struct GNUNET_HashCode hkey;
411 struct FlatFileEntry *entry; 432 struct FlatFileEntry *entry;
412 int i;
413 433
414 rvalue = GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK, UINT64_MAX); 434 rvalue = GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
435 UINT64_MAX);
415 key_len = strlen (label) + sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey); 436 key_len = strlen (label) + sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey);
416 key = GNUNET_malloc (key_len); 437 key = GNUNET_malloc (key_len);
417 GNUNET_memcpy (key, label, strlen (label)); 438 GNUNET_memcpy (key, label, strlen (label));
@@ -426,18 +447,19 @@ namestore_store_records (void *cls,
426 447
427 if (0 < rd_count) 448 if (0 < rd_count)
428 { 449 {
429 entry = GNUNET_malloc (sizeof (struct FlatFileEntry)); 450 entry = GNUNET_new (struct FlatFileEntry);
430 entry->private_key = GNUNET_malloc (sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey)); 451 entry->private_key = GNUNET_new (struct GNUNET_CRYPTO_EcdsaPrivateKey);
431 GNUNET_asprintf (&entry->label, 452 GNUNET_asprintf (&entry->label,
432 label, 453 label,
433 strlen (label)); 454 strlen (label));
434 GNUNET_memcpy (entry->private_key, 455 GNUNET_memcpy (entry->private_key,
435 zone_key, 456 zone_key,
436 sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey)); 457 sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey));
437 entry->rvalue = rvalue; 458 entry->rvalue = rvalue;
438 entry->record_count = rd_count; 459 entry->record_count = rd_count;
439 entry->record_data = GNUNET_malloc (sizeof (struct GNUNET_GNSRECORD_Data) * rd_count); 460 entry->record_data = GNUNET_new_array (rd_count,
440 for (i = 0; i < rd_count; i++) 461 struct GNUNET_GNSRECORD_Data);
462 for (unsigned int i = 0; i < rd_count; i++)
441 { 463 {
442 entry->record_data[i].expiration_time = rd[i].expiration_time; 464 entry->record_data[i].expiration_time = rd[i].expiration_time;
443 entry->record_data[i].record_type = rd[i].record_type; 465 entry->record_data[i].record_type = rd[i].record_type;
@@ -484,21 +506,28 @@ namestore_lookup_records (void *cls,
484 } 506 }
485 key_len = strlen (label) + sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey); 507 key_len = strlen (label) + sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey);
486 key = GNUNET_malloc (key_len); 508 key = GNUNET_malloc (key_len);
487 GNUNET_memcpy (key, label, strlen (label)); 509 GNUNET_memcpy (key,
510 label,
511 strlen (label));
488 GNUNET_memcpy (key+strlen(label), 512 GNUNET_memcpy (key+strlen(label),
489 zone, 513 zone,
490 sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey)); 514 sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey));
491 GNUNET_CRYPTO_hash (key, 515 GNUNET_CRYPTO_hash (key,
492 key_len, 516 key_len,
493 &hkey); 517 &hkey);
494 GNUNET_free (key); 518 GNUNET_free (key);
495 519
496 entry = GNUNET_CONTAINER_multihashmap_get (plugin->hm, &hkey); 520 entry = GNUNET_CONTAINER_multihashmap_get (plugin->hm,
521 &hkey);
497 522
498 if (NULL == entry) 523 if (NULL == entry)
499 return GNUNET_NO; 524 return GNUNET_NO;
500 if (NULL != iter) 525 if (NULL != iter)
501 iter (iter_cls, entry->private_key, entry->label, entry->record_count, entry->record_data); 526 iter (iter_cls,
527 entry->private_key,
528 entry->label,
529 entry->record_count,
530 entry->record_data);
502 return GNUNET_YES; 531 return GNUNET_YES;
503} 532}
504 533
@@ -511,6 +540,7 @@ iterate_zones (void *cls,
511 struct Plugin *plugin = cls; 540 struct Plugin *plugin = cls;
512 struct FlatFileEntry *entry = value; 541 struct FlatFileEntry *entry = value;
513 542
543 (void) key;
514 if ((plugin->target_offset > plugin->offset) || 544 if ((plugin->target_offset > plugin->offset) ||
515 ( (NULL != plugin->iter_zone) && 545 ( (NULL != plugin->iter_zone) &&
516 (0 != memcmp (entry->private_key, 546 (0 != memcmp (entry->private_key,
@@ -528,6 +558,7 @@ iterate_zones (void *cls,
528 return GNUNET_NO; 558 return GNUNET_NO;
529} 559}
530 560
561
531/** 562/**
532 * Iterate over the results for a particular key and zone in the 563 * Iterate over the results for a particular key and zone in the
533 * datastore. Will return at most one result to the iterator. 564 * datastore. Will return at most one result to the iterator.
@@ -543,9 +574,13 @@ static int
543namestore_iterate_records (void *cls, 574namestore_iterate_records (void *cls,
544 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone, 575 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
545 uint64_t offset, 576 uint64_t offset,
546 GNUNET_NAMESTORE_RecordIterator iter, void *iter_cls) 577 GNUNET_NAMESTORE_RecordIterator iter,
578 void *iter_cls)
547{ 579{
548 struct Plugin *plugin = cls; 580 struct Plugin *plugin = cls;
581
582 /* FIXME: maybe use separate closure to better handle
583 recursive calls? */
549 plugin->target_offset = offset; 584 plugin->target_offset = offset;
550 plugin->offset = 0; 585 plugin->offset = 0;
551 plugin->iter = iter; 586 plugin->iter = iter;
@@ -558,6 +593,7 @@ namestore_iterate_records (void *cls,
558 return plugin->iter_result_found; 593 return plugin->iter_result_found;
559} 594}
560 595
596
561static int 597static int
562zone_to_name (void *cls, 598zone_to_name (void *cls,
563 const struct GNUNET_HashCode *key, 599 const struct GNUNET_HashCode *key,
@@ -565,14 +601,15 @@ zone_to_name (void *cls,
565{ 601{
566 struct Plugin *plugin = cls; 602 struct Plugin *plugin = cls;
567 struct FlatFileEntry *entry = value; 603 struct FlatFileEntry *entry = value;
568 int i;
569 604
605 (void) key;
570 if (0 != memcmp (entry->private_key, 606 if (0 != memcmp (entry->private_key,
571 plugin->iter_zone, 607 plugin->iter_zone,
572 sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey))) 608 sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey)))
573 return GNUNET_YES; 609 return GNUNET_YES;
574 610
575 for (i = 0; i < entry->record_count; i++) { 611 for (unsigned int i = 0; i < entry->record_count; i++)
612 {
576 if (GNUNET_GNSRECORD_TYPE_PKEY != entry->record_data[i].record_type) 613 if (GNUNET_GNSRECORD_TYPE_PKEY != entry->record_data[i].record_type)
577 continue; 614 continue;
578 if (0 == memcmp (plugin->iter_pkey, 615 if (0 == memcmp (plugin->iter_pkey,
@@ -588,10 +625,10 @@ zone_to_name (void *cls,
588 625
589 } 626 }
590 } 627 }
591
592 return GNUNET_YES; 628 return GNUNET_YES;
593} 629}
594 630
631
595/** 632/**
596 * Look for an existing PKEY delegation record for a given public key. 633 * Look for an existing PKEY delegation record for a given public key.
597 * Returns at most one result to the iterator. 634 * Returns at most one result to the iterator.
@@ -607,19 +644,24 @@ static int
607namestore_zone_to_name (void *cls, 644namestore_zone_to_name (void *cls,
608 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone, 645 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
609 const struct GNUNET_CRYPTO_EcdsaPublicKey *value_zone, 646 const struct GNUNET_CRYPTO_EcdsaPublicKey *value_zone,
610 GNUNET_NAMESTORE_RecordIterator iter, void *iter_cls) 647 GNUNET_NAMESTORE_RecordIterator iter,
648 void *iter_cls)
611{ 649{
612 struct Plugin *plugin = cls; 650 struct Plugin *plugin = cls;
613 651
652 /* FIXME: maybe use separate closure to better handle
653 recursive calls? */
654 plugin->iter = iter;
655 plugin->iter_cls = iter_cls;
656 plugin->iter_zone = zone;
657 plugin->iter_pkey = value_zone;
658 plugin->iter_result_found = GNUNET_NO;
614 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 659 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
615 "Performing reverse lookup for `%s'\n", 660 "Performing reverse lookup for `%s'\n",
616 GNUNET_GNSRECORD_z2s (value_zone)); 661 GNUNET_GNSRECORD_z2s (value_zone));
617
618 GNUNET_CONTAINER_multihashmap_iterate (plugin->hm, 662 GNUNET_CONTAINER_multihashmap_iterate (plugin->hm,
619 &zone_to_name, 663 &zone_to_name,
620 plugin); 664 plugin);
621
622
623 return plugin->iter_result_found; 665 return plugin->iter_result_found;
624} 666}
625 667
@@ -639,7 +681,9 @@ libgnunet_plugin_namestore_flat_init (void *cls)
639 681
640 if (NULL != plugin.cfg) 682 if (NULL != plugin.cfg)
641 return NULL; /* can only initialize once! */ 683 return NULL; /* can only initialize once! */
642 memset (&plugin, 0, sizeof (struct Plugin)); 684 memset (&plugin,
685 0,
686 sizeof (struct Plugin));
643 plugin.cfg = cfg; 687 plugin.cfg = cfg;
644 if (GNUNET_OK != database_setup (&plugin)) 688 if (GNUNET_OK != database_setup (&plugin))
645 { 689 {