aboutsummaryrefslogtreecommitdiff
path: root/src/namestore/plugin_namestore_flat.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/namestore/plugin_namestore_flat.c')
-rw-r--r--src/namestore/plugin_namestore_flat.c191
1 files changed, 94 insertions, 97 deletions
diff --git a/src/namestore/plugin_namestore_flat.c b/src/namestore/plugin_namestore_flat.c
index bbb9e3c62..e16fe91b7 100644
--- a/src/namestore/plugin_namestore_flat.c
+++ b/src/namestore/plugin_namestore_flat.c
@@ -2,20 +2,18 @@
2 * This file is part of GNUnet 2 * This file is part of GNUnet
3 * Copyright (C) 2009-2015, 2018 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 it
6 * it under the terms of the GNU General Public License as published 6 * under the terms of the GNU Affero General Public License as published
7 * by the Free Software Foundation; either version 3, or (at your 7 * by the Free Software Foundation, either version 3 of the License,
8 * option) any later version. 8 * or (at your option) any later version.
9 * 9 *
10 * GNUnet is distributed in the hope that it will be useful, but 10 * GNUnet is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of 11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details. 13 * Affero General Public License for more details.
14 * 14 *
15 * You should have received a copy of the GNU General Public License 15 * You should have received a copy of the GNU Affero General Public License
16 * along with GNUnet; see the file COPYING. If not, write to the 16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
19 */ 17 */
20 18
21/** 19/**
@@ -49,41 +47,6 @@ struct Plugin
49 */ 47 */
50 struct GNUNET_CONTAINER_MultiHashMap *hm; 48 struct GNUNET_CONTAINER_MultiHashMap *hm;
51 49
52 /**
53 * Offset
54 */
55 uint32_t offset;
56
57 /**
58 * Target Offset
59 */
60 uint32_t target_offset;
61
62 /**
63 * Iterator closure
64 */
65 void *iter_cls;
66
67 /**
68 * Iterator
69 */
70 GNUNET_NAMESTORE_RecordIterator iter;
71
72 /**
73 * Zone to iterate
74 */
75 const struct GNUNET_CRYPTO_EcdsaPrivateKey *iter_zone;
76
77 /**
78 * PKEY to look for in zone to name
79 */
80 const struct GNUNET_CRYPTO_EcdsaPublicKey *iter_pkey;
81
82 /**
83 * Iteration result found
84 */
85 int iter_result_found;
86
87}; 50};
88 51
89 52
@@ -92,7 +55,7 @@ struct FlatFileEntry
92 /** 55 /**
93 * Entry zone 56 * Entry zone
94 */ 57 */
95 struct GNUNET_CRYPTO_EcdsaPrivateKey *private_key; 58 struct GNUNET_CRYPTO_EcdsaPrivateKey private_key;
96 59
97 /** 60 /**
98 * Record cound 61 * Record cound
@@ -130,7 +93,6 @@ static int
130database_setup (struct Plugin *plugin) 93database_setup (struct Plugin *plugin)
131{ 94{
132 char *afsdir; 95 char *afsdir;
133 char *key;
134 char *record_data; 96 char *record_data;
135 char *zone_private_key; 97 char *zone_private_key;
136 char *record_data_b64; 98 char *record_data_b64;
@@ -141,7 +103,6 @@ database_setup (struct Plugin *plugin)
141 char *record_count; 103 char *record_count;
142 size_t record_data_size; 104 size_t record_data_size;
143 uint64_t size; 105 uint64_t size;
144 size_t key_len;
145 struct GNUNET_HashCode hkey; 106 struct GNUNET_HashCode hkey;
146 struct GNUNET_DISK_FileHandle *fh; 107 struct GNUNET_DISK_FileHandle *fh;
147 struct FlatFileEntry *entry; 108 struct FlatFileEntry *entry;
@@ -269,7 +230,7 @@ database_setup (struct Plugin *plugin)
269 record_data_size 230 record_data_size
270 = GNUNET_STRINGS_base64_decode (record_data_b64, 231 = GNUNET_STRINGS_base64_decode (record_data_b64,
271 strlen (record_data_b64), 232 strlen (record_data_b64),
272 &record_data); 233 (void **) &record_data);
273 entry->record_data = 234 entry->record_data =
274 GNUNET_new_array (entry->record_count, 235 GNUNET_new_array (entry->record_count,
275 struct GNUNET_GNSRECORD_Data); 236 struct GNUNET_GNSRECORD_Data);
@@ -288,21 +249,34 @@ database_setup (struct Plugin *plugin)
288 break; 249 break;
289 } 250 }
290 GNUNET_free (record_data); 251 GNUNET_free (record_data);
291 GNUNET_STRINGS_base64_decode (zone_private_key, 252
292 strlen (zone_private_key), 253 {
293 (char**)&entry->private_key); 254 struct GNUNET_CRYPTO_EcdsaPrivateKey *private_key;
294 key_len = strlen (label) + sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey); 255
295 key = GNUNET_malloc (strlen (label) + sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey)); 256 GNUNET_STRINGS_base64_decode (zone_private_key,
296 GNUNET_memcpy (key, 257 strlen (zone_private_key),
297 label, 258 (void**)&private_key);
298 strlen (label)); 259 entry->private_key = *private_key;
299 GNUNET_memcpy (key+strlen(label), 260 GNUNET_free (private_key);
300 entry->private_key, 261 }
301 sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey)); 262
302 GNUNET_CRYPTO_hash (key, 263 {
303 key_len, 264 char *key;
304 &hkey); 265 size_t key_len;
305 GNUNET_free (key); 266
267 key_len = strlen (label) + sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey);
268 key = GNUNET_malloc (strlen (label) + sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey));
269 GNUNET_memcpy (key,
270 label,
271 strlen (label));
272 GNUNET_memcpy (key+strlen(label),
273 &entry->private_key,
274 sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey));
275 GNUNET_CRYPTO_hash (key,
276 key_len,
277 &hkey);
278 GNUNET_free (key);
279 }
306 if (GNUNET_OK != 280 if (GNUNET_OK !=
307 GNUNET_CONTAINER_multihashmap_put (plugin->hm, 281 GNUNET_CONTAINER_multihashmap_put (plugin->hm,
308 &hkey, 282 &hkey,
@@ -336,14 +310,26 @@ store_and_free_entries (void *cls,
336 char *line; 310 char *line;
337 char *zone_private_key; 311 char *zone_private_key;
338 char *record_data_b64; 312 char *record_data_b64;
339 size_t data_size; 313 ssize_t data_size;
340 314
341 (void) key; 315 (void) key;
342 GNUNET_STRINGS_base64_encode ((char*)entry->private_key, 316 GNUNET_STRINGS_base64_encode (&entry->private_key,
343 sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey), 317 sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey),
344 &zone_private_key); 318 &zone_private_key);
345 data_size = GNUNET_GNSRECORD_records_get_size (entry->record_count, 319 data_size = GNUNET_GNSRECORD_records_get_size (entry->record_count,
346 entry->record_data); 320 entry->record_data);
321 if (data_size < 0)
322 {
323 GNUNET_break (0);
324 GNUNET_free (zone_private_key);
325 return GNUNET_SYSERR;
326 }
327 if (data_size >= UINT16_MAX)
328 {
329 GNUNET_break (0);
330 GNUNET_free (zone_private_key);
331 return GNUNET_SYSERR;
332 }
347 { 333 {
348 char data[data_size]; 334 char data[data_size];
349 ssize_t ret; 335 ssize_t ret;
@@ -353,7 +339,7 @@ store_and_free_entries (void *cls,
353 data_size, 339 data_size,
354 data); 340 data);
355 if ( (ret < 0) || 341 if ( (ret < 0) ||
356 (data_size != (size_t) ret) ) 342 (data_size != ret) )
357 { 343 {
358 GNUNET_break (0); 344 GNUNET_break (0);
359 GNUNET_free (zone_private_key); 345 GNUNET_free (zone_private_key);
@@ -364,10 +350,10 @@ store_and_free_entries (void *cls,
364 &record_data_b64); 350 &record_data_b64);
365 } 351 }
366 GNUNET_asprintf (&line, 352 GNUNET_asprintf (&line,
367 "%s,%lu,%u,%s,%s\n", 353 "%s,%llu,%u,%s,%s\n",
368 zone_private_key, 354 zone_private_key,
369 entry->rvalue, 355 (unsigned long long) entry->rvalue,
370 entry->record_count, 356 (unsigned int) entry->record_count,
371 record_data_b64, 357 record_data_b64,
372 entry->label); 358 entry->label);
373 GNUNET_free (record_data_b64); 359 GNUNET_free (record_data_b64);
@@ -378,7 +364,6 @@ store_and_free_entries (void *cls,
378 strlen (line)); 364 strlen (line));
379 365
380 GNUNET_free (line); 366 GNUNET_free (line);
381 GNUNET_free (entry->private_key);
382 GNUNET_free (entry->label); 367 GNUNET_free (entry->label);
383 GNUNET_free (entry->record_data); 368 GNUNET_free (entry->record_data);
384 GNUNET_free (entry); 369 GNUNET_free (entry);
@@ -466,11 +451,10 @@ namestore_flat_store_records (void *cls,
466 return GNUNET_OK; 451 return GNUNET_OK;
467 } 452 }
468 entry = GNUNET_new (struct FlatFileEntry); 453 entry = GNUNET_new (struct FlatFileEntry);
469 entry->private_key = GNUNET_new (struct GNUNET_CRYPTO_EcdsaPrivateKey);
470 GNUNET_asprintf (&entry->label, 454 GNUNET_asprintf (&entry->label,
471 label, 455 label,
472 strlen (label)); 456 strlen (label));
473 GNUNET_memcpy (entry->private_key, 457 GNUNET_memcpy (&entry->private_key,
474 zone_key, 458 zone_key,
475 sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey)); 459 sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey));
476 entry->rvalue = rvalue; 460 entry->rvalue = rvalue;
@@ -544,7 +528,7 @@ namestore_flat_lookup_records (void *cls,
544 if (NULL != iter) 528 if (NULL != iter)
545 iter (iter_cls, 529 iter (iter_cls,
546 0, 530 0,
547 entry->private_key, 531 &entry->private_key,
548 entry->label, 532 entry->label,
549 entry->record_count, 533 entry->record_count,
550 entry->record_data); 534 entry->record_data);
@@ -608,14 +592,14 @@ iterate_zones (void *cls,
608 struct FlatFileEntry *entry = value; 592 struct FlatFileEntry *entry = value;
609 593
610 (void) key; 594 (void) key;
611 ic->pos++;
612 if (0 == ic->limit) 595 if (0 == ic->limit)
613 return GNUNET_NO; 596 return GNUNET_NO;
614 if ( (NULL != ic->zone) && 597 if ( (NULL != ic->zone) &&
615 (0 != memcmp (entry->private_key, 598 (0 != memcmp (&entry->private_key,
616 ic->zone, 599 ic->zone,
617 sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey))) ) 600 sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey))) )
618 return GNUNET_YES; 601 return GNUNET_YES;
602 ic->pos++;
619 if (ic->offset > 0) 603 if (ic->offset > 0)
620 { 604 {
621 ic->offset--; 605 ic->offset--;
@@ -623,7 +607,7 @@ iterate_zones (void *cls,
623 } 607 }
624 ic->iter (ic->iter_cls, 608 ic->iter (ic->iter_cls,
625 ic->pos, 609 ic->pos,
626 entry->private_key, 610 &entry->private_key,
627 entry->label, 611 entry->label,
628 entry->record_count, 612 entry->record_count,
629 entry->record_data); 613 entry->record_data);
@@ -670,17 +654,31 @@ namestore_flat_iterate_records (void *cls,
670} 654}
671 655
672 656
657/**
658 * Closure for #zone_to_name.
659 */
660struct ZoneToNameContext
661{
662 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone;
663 const struct GNUNET_CRYPTO_EcdsaPublicKey *value_zone;
664 GNUNET_NAMESTORE_RecordIterator iter;
665 void *iter_cls;
666
667 int result_found;
668};
669
670
673static int 671static int
674zone_to_name (void *cls, 672zone_to_name (void *cls,
675 const struct GNUNET_HashCode *key, 673 const struct GNUNET_HashCode *key,
676 void *value) 674 void *value)
677{ 675{
678 struct Plugin *plugin = cls; 676 struct ZoneToNameContext *ztn = cls;
679 struct FlatFileEntry *entry = value; 677 struct FlatFileEntry *entry = value;
680 678
681 (void) key; 679 (void) key;
682 if (0 != memcmp (entry->private_key, 680 if (0 != memcmp (&entry->private_key,
683 plugin->iter_zone, 681 ztn->zone,
684 sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey))) 682 sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey)))
685 return GNUNET_YES; 683 return GNUNET_YES;
686 684
@@ -688,18 +686,17 @@ zone_to_name (void *cls,
688 { 686 {
689 if (GNUNET_GNSRECORD_TYPE_PKEY != entry->record_data[i].record_type) 687 if (GNUNET_GNSRECORD_TYPE_PKEY != entry->record_data[i].record_type)
690 continue; 688 continue;
691 if (0 == memcmp (plugin->iter_pkey, 689 if (0 == memcmp (ztn->value_zone,
692 entry->record_data[i].data, 690 entry->record_data[i].data,
693 sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey))) 691 sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey)))
694 { 692 {
695 plugin->iter (plugin->iter_cls, 693 ztn->iter (ztn->iter_cls,
696 0, 694 0,
697 entry->private_key, 695 &entry->private_key,
698 entry->label, 696 entry->label,
699 entry->record_count, 697 entry->record_count,
700 entry->record_data); 698 entry->record_data);
701 plugin->iter_result_found = GNUNET_YES; 699 ztn->result_found = GNUNET_YES;
702
703 } 700 }
704 } 701 }
705 return GNUNET_YES; 702 return GNUNET_YES;
@@ -725,21 +722,21 @@ namestore_flat_zone_to_name (void *cls,
725 void *iter_cls) 722 void *iter_cls)
726{ 723{
727 struct Plugin *plugin = cls; 724 struct Plugin *plugin = cls;
725 struct ZoneToNameContext ztn = {
726 .iter = iter,
727 .iter_cls = iter_cls,
728 .zone = zone,
729 .value_zone = value_zone,
730 .result_found = GNUNET_NO
731 };
728 732
729 /* FIXME: maybe use separate closure to better handle
730 recursive calls? */
731 plugin->iter = iter;
732 plugin->iter_cls = iter_cls;
733 plugin->iter_zone = zone;
734 plugin->iter_pkey = value_zone;
735 plugin->iter_result_found = GNUNET_NO;
736 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 733 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
737 "Performing reverse lookup for `%s'\n", 734 "Performing reverse lookup for `%s'\n",
738 GNUNET_GNSRECORD_z2s (value_zone)); 735 GNUNET_GNSRECORD_z2s (value_zone));
739 GNUNET_CONTAINER_multihashmap_iterate (plugin->hm, 736 GNUNET_CONTAINER_multihashmap_iterate (plugin->hm,
740 &zone_to_name, 737 &zone_to_name,
741 plugin); 738 &ztn);
742 return plugin->iter_result_found; 739 return ztn.result_found;
743} 740}
744 741
745 742