diff options
author | Schanzenbach, Martin <mschanzenbach@posteo.de> | 2019-04-30 15:56:16 +0200 |
---|---|---|
committer | Schanzenbach, Martin <mschanzenbach@posteo.de> | 2019-04-30 15:56:16 +0200 |
commit | 342619bf17d389e4305c9ee72b609059706a8023 (patch) | |
tree | 391ff802b3d7389a08947dfb2e2fc816f4013752 /src/namestore | |
parent | 57636ddf7839aaeedd14c90afcd28b375ec516a6 (diff) | |
download | gnunet-342619bf17d389e4305c9ee72b609059706a8023.tar.gz gnunet-342619bf17d389e4305c9ee72b609059706a8023.zip |
REST/NAMESTORE: rework API
Diffstat (limited to 'src/namestore')
-rw-r--r-- | src/namestore/plugin_rest_namestore.c | 228 |
1 files changed, 21 insertions, 207 deletions
diff --git a/src/namestore/plugin_rest_namestore.c b/src/namestore/plugin_rest_namestore.c index 46e5a590f..39b21c233 100644 --- a/src/namestore/plugin_rest_namestore.c +++ b/src/namestore/plugin_rest_namestore.c | |||
@@ -49,10 +49,6 @@ | |||
49 | */ | 49 | */ |
50 | #define GNUNET_REST_IDENTITY_NOT_FOUND "No identity found" | 50 | #define GNUNET_REST_IDENTITY_NOT_FOUND "No identity found" |
51 | 51 | ||
52 | /** | ||
53 | * Error message No default zone specified | ||
54 | */ | ||
55 | #define GNUNET_REST_NAMESTORE_NO_DEFAULT_ZONE "No default zone specified" | ||
56 | 52 | ||
57 | /** | 53 | /** |
58 | * Error message Failed request | 54 | * Error message Failed request |
@@ -143,6 +139,11 @@ struct RequestHandle | |||
143 | struct GNUNET_GNSRECORD_Data *rd; | 139 | struct GNUNET_GNSRECORD_Data *rd; |
144 | 140 | ||
145 | /** | 141 | /** |
142 | * Number of records in rd | ||
143 | */ | ||
144 | unsigned int rd_count; | ||
145 | |||
146 | /** | ||
146 | * NAMESTORE Operation | 147 | * NAMESTORE Operation |
147 | */ | 148 | */ |
148 | struct GNUNET_NAMESTORE_QueueEntry *add_qe; | 149 | struct GNUNET_NAMESTORE_QueueEntry *add_qe; |
@@ -263,8 +264,11 @@ cleanup_handle (void *cls) | |||
263 | GNUNET_free (handle->emsg); | 264 | GNUNET_free (handle->emsg); |
264 | if (NULL != handle->rd) | 265 | if (NULL != handle->rd) |
265 | { | 266 | { |
266 | if (NULL != handle->rd->data) | 267 | for (int i = 0; i < handle->rd_count; i++) |
267 | GNUNET_free ((void *) handle->rd->data); | 268 | { |
269 | if (NULL != handle->rd[i].data) | ||
270 | GNUNET_free ((void *) handle->rd[i].data); | ||
271 | } | ||
268 | GNUNET_free (handle->rd); | 272 | GNUNET_free (handle->rd); |
269 | } | 273 | } |
270 | if (NULL != handle->timeout_task) | 274 | if (NULL != handle->timeout_task) |
@@ -484,68 +488,13 @@ namestore_list_iteration (void *cls, | |||
484 | 488 | ||
485 | if (NULL == handle->resp_object) | 489 | if (NULL == handle->resp_object) |
486 | handle->resp_object = json_array (); | 490 | handle->resp_object = json_array (); |
487 | 491 | record_obj = GNUNET_JSON_from_gnsrecord (rname, | |
488 | for (unsigned int i = 0; i < rd_len; i++) | 492 | rd, |
489 | { | 493 | rd_len); |
490 | if ((GNUNET_GNSRECORD_TYPE_NICK == rd[i].record_type) && | 494 | json_array_append_new (handle->resp_object, record_obj); |
491 | (0 != strcmp (rname, GNUNET_GNS_EMPTY_LABEL_AT))) | ||
492 | continue; | ||
493 | |||
494 | record_obj = GNUNET_JSON_from_gns_record (rname, &rd[i]); | ||
495 | |||
496 | if (NULL == record_obj) | ||
497 | continue; | ||
498 | |||
499 | json_array_append (handle->resp_object, record_obj); | ||
500 | json_decref (record_obj); | ||
501 | } | ||
502 | |||
503 | GNUNET_NAMESTORE_zone_iterator_next (handle->list_it, 1); | 495 | GNUNET_NAMESTORE_zone_iterator_next (handle->list_it, 1); |
504 | } | 496 | } |
505 | 497 | ||
506 | /** | ||
507 | * @param cls closure | ||
508 | * @param ego ego handle | ||
509 | * @param ctx context for application to store data for this ego | ||
510 | * (during the lifetime of this process, initially NULL) | ||
511 | * @param identifier identifier assigned by the user for this ego, | ||
512 | * NULL if the user just deleted the ego and it | ||
513 | * must thus no longer be used | ||
514 | */ | ||
515 | static void | ||
516 | default_ego_get (void *cls, | ||
517 | struct GNUNET_IDENTITY_Ego *ego, | ||
518 | void **ctx, | ||
519 | const char *identifier) | ||
520 | { | ||
521 | struct RequestHandle *handle = cls; | ||
522 | handle->op = NULL; | ||
523 | |||
524 | if (ego == NULL) | ||
525 | { | ||
526 | handle->emsg = GNUNET_strdup (GNUNET_REST_NAMESTORE_NO_DEFAULT_ZONE); | ||
527 | GNUNET_SCHEDULER_add_now (&do_error, handle); | ||
528 | return; | ||
529 | } | ||
530 | handle->zone_pkey = GNUNET_IDENTITY_ego_get_private_key (ego); | ||
531 | |||
532 | handle->list_it = | ||
533 | GNUNET_NAMESTORE_zone_iteration_start (handle->ns_handle, | ||
534 | handle->zone_pkey, | ||
535 | &namestore_iteration_error, | ||
536 | handle, | ||
537 | &namestore_list_iteration, | ||
538 | handle, | ||
539 | &namestore_list_finished, | ||
540 | handle); | ||
541 | if (NULL == handle->list_it) | ||
542 | { | ||
543 | handle->emsg = GNUNET_strdup (GNUNET_REST_NAMESTORE_FAILED); | ||
544 | GNUNET_SCHEDULER_add_now (&do_error, handle); | ||
545 | return; | ||
546 | } | ||
547 | } | ||
548 | |||
549 | 498 | ||
550 | /** | 499 | /** |
551 | * Handle namestore GET request | 500 | * Handle namestore GET request |
@@ -581,18 +530,8 @@ namestore_get (struct GNUNET_REST_RequestHandle *con_handle, | |||
581 | } | 530 | } |
582 | } | 531 | } |
583 | if (NULL != ego_entry) | 532 | if (NULL != ego_entry) |
584 | { | ||
585 | handle->zone_pkey = GNUNET_IDENTITY_ego_get_private_key (ego_entry->ego); | 533 | handle->zone_pkey = GNUNET_IDENTITY_ego_get_private_key (ego_entry->ego); |
586 | } | ||
587 | 534 | ||
588 | if (NULL == handle->zone_pkey) | ||
589 | { | ||
590 | handle->op = GNUNET_IDENTITY_get (handle->identity_handle, | ||
591 | "namestore", | ||
592 | &default_ego_get, | ||
593 | handle); | ||
594 | return; | ||
595 | } | ||
596 | handle->list_it = | 535 | handle->list_it = |
597 | GNUNET_NAMESTORE_zone_iteration_start (handle->ns_handle, | 536 | GNUNET_NAMESTORE_zone_iteration_start (handle->ns_handle, |
598 | handle->zone_pkey, | 537 | handle->zone_pkey, |
@@ -611,48 +550,6 @@ namestore_get (struct GNUNET_REST_RequestHandle *con_handle, | |||
611 | } | 550 | } |
612 | 551 | ||
613 | 552 | ||
614 | /** | ||
615 | * @param cls closure | ||
616 | * @param ego ego handle | ||
617 | * @param ctx context for application to store data for this ego | ||
618 | * (during the lifetime of this process, initially NULL) | ||
619 | * @param identifier identifier assigned by the user for this ego, | ||
620 | * NULL if the user just deleted the ego and it | ||
621 | * must thus no longer be used | ||
622 | */ | ||
623 | static void | ||
624 | default_ego_post (void *cls, | ||
625 | struct GNUNET_IDENTITY_Ego *ego, | ||
626 | void **ctx, | ||
627 | const char *identifier) | ||
628 | { | ||
629 | struct RequestHandle *handle = cls; | ||
630 | handle->op = NULL; | ||
631 | |||
632 | if (ego == NULL) | ||
633 | { | ||
634 | handle->emsg = GNUNET_strdup (GNUNET_REST_NAMESTORE_NO_DEFAULT_ZONE); | ||
635 | GNUNET_SCHEDULER_add_now (&do_error, handle); | ||
636 | return; | ||
637 | } | ||
638 | handle->zone_pkey = GNUNET_IDENTITY_ego_get_private_key (ego); | ||
639 | |||
640 | handle->add_qe = GNUNET_NAMESTORE_records_store (handle->ns_handle, | ||
641 | handle->zone_pkey, | ||
642 | handle->record_name, | ||
643 | 1, | ||
644 | handle->rd, | ||
645 | &create_finished, | ||
646 | handle); | ||
647 | if (NULL == handle->add_qe) | ||
648 | { | ||
649 | handle->emsg = GNUNET_strdup (GNUNET_REST_NAMESTORE_FAILED); | ||
650 | GNUNET_SCHEDULER_add_now (&do_error, handle); | ||
651 | return; | ||
652 | } | ||
653 | } | ||
654 | |||
655 | |||
656 | static void | 553 | static void |
657 | ns_lookup_error_cb (void *cls) | 554 | ns_lookup_error_cb (void *cls) |
658 | { | 555 | { |
@@ -670,14 +567,15 @@ ns_lookup_cb (void *cls, | |||
670 | const struct GNUNET_GNSRECORD_Data *rd) | 567 | const struct GNUNET_GNSRECORD_Data *rd) |
671 | { | 568 | { |
672 | struct RequestHandle *handle = cls; | 569 | struct RequestHandle *handle = cls; |
673 | struct GNUNET_GNSRECORD_Data rd_new[rd_count + 1]; | 570 | struct GNUNET_GNSRECORD_Data rd_new[rd_count + handle->rd_count]; |
674 | for (int i = 0; i < rd_count; i++) | 571 | for (int i = 0; i < rd_count; i++) |
675 | rd_new[i] = rd[i]; | 572 | rd_new[i] = rd[i]; |
676 | rd_new[rd_count] = *handle->rd; | 573 | for (int j = 0; j < handle->rd_count; j++) |
574 | rd_new[rd_count + j] = handle->rd[j]; | ||
677 | handle->add_qe = GNUNET_NAMESTORE_records_store (handle->ns_handle, | 575 | handle->add_qe = GNUNET_NAMESTORE_records_store (handle->ns_handle, |
678 | handle->zone_pkey, | 576 | handle->zone_pkey, |
679 | handle->record_name, | 577 | handle->record_name, |
680 | rd_count + 1, | 578 | rd_count + handle->rd_count, |
681 | rd_new, | 579 | rd_new, |
682 | &create_finished, | 580 | &create_finished, |
683 | handle); | 581 | handle); |
@@ -703,16 +601,12 @@ namestore_add (struct GNUNET_REST_RequestHandle *con_handle, | |||
703 | void *cls) | 601 | void *cls) |
704 | { | 602 | { |
705 | struct RequestHandle *handle = cls; | 603 | struct RequestHandle *handle = cls; |
706 | struct GNUNET_GNSRECORD_Data *gns_record; | ||
707 | struct EgoEntry *ego_entry; | 604 | struct EgoEntry *ego_entry; |
708 | char *egoname; | 605 | char *egoname; |
709 | json_t *data_js; | 606 | json_t *data_js; |
710 | json_t *name_json; | ||
711 | json_error_t err; | 607 | json_error_t err; |
712 | char term_data[handle->rest_handle->data_size + 1]; | ||
713 | 608 | ||
714 | struct GNUNET_JSON_Specification gnsspec[] = | 609 | char term_data[handle->rest_handle->data_size + 1]; |
715 | {GNUNET_JSON_spec_gnsrecord_data (&gns_record), GNUNET_JSON_spec_end ()}; | ||
716 | 610 | ||
717 | if (0 >= handle->rest_handle->data_size) | 611 | if (0 >= handle->rest_handle->data_size) |
718 | { | 612 | { |
@@ -725,29 +619,12 @@ namestore_add (struct GNUNET_REST_RequestHandle *con_handle, | |||
725 | handle->rest_handle->data, | 619 | handle->rest_handle->data, |
726 | handle->rest_handle->data_size); | 620 | handle->rest_handle->data_size); |
727 | data_js = json_loads (term_data, JSON_DECODE_ANY, &err); | 621 | data_js = json_loads (term_data, JSON_DECODE_ANY, &err); |
622 | struct GNUNET_JSON_Specification gnsspec[] = | ||
623 | {GNUNET_JSON_spec_gnsrecord (&handle->rd, &handle->rd_count, &handle->record_name), GNUNET_JSON_spec_end ()}; | ||
728 | if (GNUNET_OK != GNUNET_JSON_parse (data_js, gnsspec, NULL, NULL)) | 624 | if (GNUNET_OK != GNUNET_JSON_parse (data_js, gnsspec, NULL, NULL)) |
729 | { | 625 | { |
730 | handle->emsg = GNUNET_strdup (GNUNET_REST_NAMESTORE_INVALID_DATA); | 626 | handle->emsg = GNUNET_strdup (GNUNET_REST_NAMESTORE_INVALID_DATA); |
731 | GNUNET_SCHEDULER_add_now (&do_error, handle); | 627 | GNUNET_SCHEDULER_add_now (&do_error, handle); |
732 | GNUNET_JSON_parse_free (gnsspec); | ||
733 | json_decref (data_js); | ||
734 | return; | ||
735 | } | ||
736 | handle->rd = gns_record; | ||
737 | |||
738 | name_json = json_object_get (data_js, "record_name"); | ||
739 | if (! json_is_string (name_json)) | ||
740 | { | ||
741 | handle->emsg = GNUNET_strdup (GNUNET_REST_NAMESTORE_INVALID_DATA); | ||
742 | GNUNET_SCHEDULER_add_now (&do_error, handle); | ||
743 | json_decref (data_js); | ||
744 | return; | ||
745 | } | ||
746 | handle->record_name = GNUNET_strdup (json_string_value (name_json)); | ||
747 | if (NULL == handle->record_name) | ||
748 | { | ||
749 | handle->emsg = GNUNET_strdup (GNUNET_REST_NAMESTORE_INVALID_DATA); | ||
750 | GNUNET_SCHEDULER_add_now (&do_error, handle); | ||
751 | json_decref (data_js); | 628 | json_decref (data_js); |
752 | return; | 629 | return; |
753 | } | 630 | } |
@@ -778,17 +655,7 @@ namestore_add (struct GNUNET_REST_RequestHandle *con_handle, | |||
778 | } | 655 | } |
779 | } | 656 | } |
780 | if (NULL != ego_entry) | 657 | if (NULL != ego_entry) |
781 | { | ||
782 | handle->zone_pkey = GNUNET_IDENTITY_ego_get_private_key (ego_entry->ego); | 658 | handle->zone_pkey = GNUNET_IDENTITY_ego_get_private_key (ego_entry->ego); |
783 | } | ||
784 | if (NULL == handle->zone_pkey) | ||
785 | { | ||
786 | handle->op = GNUNET_IDENTITY_get (handle->identity_handle, | ||
787 | "namestore", | ||
788 | &default_ego_post, | ||
789 | handle); | ||
790 | return; | ||
791 | } | ||
792 | handle->add_qe = GNUNET_NAMESTORE_records_lookup (handle->ns_handle, | 659 | handle->add_qe = GNUNET_NAMESTORE_records_lookup (handle->ns_handle, |
793 | handle->zone_pkey, | 660 | handle->zone_pkey, |
794 | handle->record_name, | 661 | handle->record_name, |
@@ -806,48 +673,6 @@ namestore_add (struct GNUNET_REST_RequestHandle *con_handle, | |||
806 | 673 | ||
807 | 674 | ||
808 | /** | 675 | /** |
809 | * @param cls closure | ||
810 | * @param ego ego handle | ||
811 | * @param ctx context for application to store data for this ego | ||
812 | * (during the lifetime of this process, initially NULL) | ||
813 | * @param identifier identifier assigned by the user for this ego, | ||
814 | * NULL if the user just deleted the ego and it | ||
815 | * must thus no longer be used | ||
816 | */ | ||
817 | static void | ||
818 | default_ego_delete (void *cls, | ||
819 | struct GNUNET_IDENTITY_Ego *ego, | ||
820 | void **ctx, | ||
821 | const char *identifier) | ||
822 | { | ||
823 | struct RequestHandle *handle = cls; | ||
824 | handle->op = NULL; | ||
825 | |||
826 | if (ego == NULL) | ||
827 | { | ||
828 | handle->emsg = GNUNET_strdup (GNUNET_REST_NAMESTORE_NO_DEFAULT_ZONE); | ||
829 | GNUNET_SCHEDULER_add_now (&do_error, handle); | ||
830 | return; | ||
831 | } | ||
832 | handle->zone_pkey = GNUNET_IDENTITY_ego_get_private_key (ego); | ||
833 | |||
834 | handle->add_qe = GNUNET_NAMESTORE_records_store (handle->ns_handle, | ||
835 | handle->zone_pkey, | ||
836 | handle->record_name, | ||
837 | 0, | ||
838 | NULL, | ||
839 | &del_finished, | ||
840 | handle); | ||
841 | if (NULL == handle->add_qe) | ||
842 | { | ||
843 | handle->emsg = GNUNET_strdup (GNUNET_REST_NAMESTORE_FAILED); | ||
844 | GNUNET_SCHEDULER_add_now (&do_error, handle); | ||
845 | return; | ||
846 | } | ||
847 | } | ||
848 | |||
849 | |||
850 | /** | ||
851 | * Handle namestore DELETE request | 676 | * Handle namestore DELETE request |
852 | * | 677 | * |
853 | * @param con_handle the connection handle | 678 | * @param con_handle the connection handle |
@@ -882,9 +707,7 @@ namestore_delete (struct GNUNET_REST_RequestHandle *con_handle, | |||
882 | } | 707 | } |
883 | } | 708 | } |
884 | if (NULL != ego_entry) | 709 | if (NULL != ego_entry) |
885 | { | ||
886 | handle->zone_pkey = GNUNET_IDENTITY_ego_get_private_key (ego_entry->ego); | 710 | handle->zone_pkey = GNUNET_IDENTITY_ego_get_private_key (ego_entry->ego); |
887 | } | ||
888 | 711 | ||
889 | GNUNET_CRYPTO_hash ("record_name", strlen ("record_name"), &key); | 712 | GNUNET_CRYPTO_hash ("record_name", strlen ("record_name"), &key); |
890 | if (GNUNET_NO == | 713 | if (GNUNET_NO == |
@@ -897,15 +720,6 @@ namestore_delete (struct GNUNET_REST_RequestHandle *con_handle, | |||
897 | handle->record_name = GNUNET_strdup ( | 720 | handle->record_name = GNUNET_strdup ( |
898 | GNUNET_CONTAINER_multihashmap_get (con_handle->url_param_map, &key)); | 721 | GNUNET_CONTAINER_multihashmap_get (con_handle->url_param_map, &key)); |
899 | 722 | ||
900 | if (NULL == handle->zone_pkey) | ||
901 | { | ||
902 | handle->op = GNUNET_IDENTITY_get (handle->identity_handle, | ||
903 | "namestore", | ||
904 | &default_ego_delete, | ||
905 | handle); | ||
906 | return; | ||
907 | } | ||
908 | |||
909 | handle->add_qe = GNUNET_NAMESTORE_records_store (handle->ns_handle, | 723 | handle->add_qe = GNUNET_NAMESTORE_records_store (handle->ns_handle, |
910 | handle->zone_pkey, | 724 | handle->zone_pkey, |
911 | handle->record_name, | 725 | handle->record_name, |