diff options
author | Schanzenbach, Martin <mschanzenbach@posteo.de> | 2019-04-14 00:09:18 +0200 |
---|---|---|
committer | Schanzenbach, Martin <mschanzenbach@posteo.de> | 2019-04-14 10:01:24 +0200 |
commit | eb2d072aa1096c9d77cbfde239a1004cab642e64 (patch) | |
tree | b44e82f972c503a64a6f2679edd340f7a1ecdda6 /src/reclaim/gnunet-service-reclaim_tickets.c | |
parent | af90492d9f86b3bcce93a8459383744939318bc3 (diff) | |
download | gnunet-eb2d072aa1096c9d77cbfde239a1004cab642e64.tar.gz gnunet-eb2d072aa1096c9d77cbfde239a1004cab642e64.zip |
RECLAIM: Simplify logic
Diffstat (limited to 'src/reclaim/gnunet-service-reclaim_tickets.c')
-rw-r--r-- | src/reclaim/gnunet-service-reclaim_tickets.c | 237 |
1 files changed, 36 insertions, 201 deletions
diff --git a/src/reclaim/gnunet-service-reclaim_tickets.c b/src/reclaim/gnunet-service-reclaim_tickets.c index 5a3190c7f..4a307e635 100644 --- a/src/reclaim/gnunet-service-reclaim_tickets.c +++ b/src/reclaim/gnunet-service-reclaim_tickets.c | |||
@@ -164,21 +164,6 @@ struct TicketIssueHandle { | |||
164 | struct GNUNET_NAMESTORE_QueueEntry *ns_qe; | 164 | struct GNUNET_NAMESTORE_QueueEntry *ns_qe; |
165 | 165 | ||
166 | /** | 166 | /** |
167 | * Ticket reference list | ||
168 | */ | ||
169 | struct TicketReference *ticket_refs_head; | ||
170 | |||
171 | /** | ||
172 | * Ticket reference list | ||
173 | */ | ||
174 | struct TicketReference *ticket_refs_tail; | ||
175 | |||
176 | /** | ||
177 | * Number of references | ||
178 | */ | ||
179 | uint32_t ticket_ref_num; | ||
180 | |||
181 | /** | ||
182 | * Callback | 167 | * Callback |
183 | */ | 168 | */ |
184 | RECLAIM_TICKETS_TicketResult cb; | 169 | RECLAIM_TICKETS_TicketResult cb; |
@@ -194,19 +179,9 @@ struct TicketIssueHandle { | |||
194 | */ | 179 | */ |
195 | struct RECLAIM_TICKETS_Iterator { | 180 | struct RECLAIM_TICKETS_Iterator { |
196 | /** | 181 | /** |
197 | * Issuer Key | ||
198 | */ | ||
199 | struct GNUNET_CRYPTO_EcdsaPrivateKey identity; | ||
200 | |||
201 | /** | ||
202 | * Issuer pubkey | ||
203 | */ | ||
204 | struct GNUNET_CRYPTO_EcdsaPublicKey identity_pub; | ||
205 | |||
206 | /** | ||
207 | * Namestore queue entry | 182 | * Namestore queue entry |
208 | */ | 183 | */ |
209 | struct GNUNET_NAMESTORE_QueueEntry *ns_qe; | 184 | struct GNUNET_NAMESTORE_ZoneIterator *ns_it; |
210 | 185 | ||
211 | /** | 186 | /** |
212 | * Iter callback | 187 | * Iter callback |
@@ -217,16 +192,6 @@ struct RECLAIM_TICKETS_Iterator { | |||
217 | * Iter cls | 192 | * Iter cls |
218 | */ | 193 | */ |
219 | void *cb_cls; | 194 | void *cb_cls; |
220 | |||
221 | /** | ||
222 | * Ticket reference list | ||
223 | */ | ||
224 | struct TicketReference *tickets_head; | ||
225 | |||
226 | /** | ||
227 | * Ticket reference list | ||
228 | */ | ||
229 | struct TicketReference *tickets_tail; | ||
230 | }; | 195 | }; |
231 | 196 | ||
232 | 197 | ||
@@ -870,113 +835,13 @@ RECLAIM_TICKETS_consume_cancel (struct RECLAIM_TICKETS_ConsumeHandle *cth) | |||
870 | static void | 835 | static void |
871 | cleanup_issue_handle (struct TicketIssueHandle *handle) | 836 | cleanup_issue_handle (struct TicketIssueHandle *handle) |
872 | { | 837 | { |
873 | struct TicketReference *tr; | ||
874 | struct TicketReference *tr_tmp; | ||
875 | if (NULL != handle->attrs) | ||
876 | GNUNET_RECLAIM_ATTRIBUTE_list_destroy (handle->attrs); | ||
877 | if (NULL != handle->ns_qe) | 838 | if (NULL != handle->ns_qe) |
878 | GNUNET_NAMESTORE_cancel (handle->ns_qe); | 839 | GNUNET_NAMESTORE_cancel (handle->ns_qe); |
879 | for (tr = handle->ticket_refs_head; NULL != tr;) { | ||
880 | if (NULL != tr->attrs) | ||
881 | GNUNET_RECLAIM_ATTRIBUTE_list_destroy (tr->attrs); | ||
882 | tr_tmp = tr; | ||
883 | tr = tr->next; | ||
884 | GNUNET_free (tr_tmp); | ||
885 | } | ||
886 | GNUNET_free (handle); | 840 | GNUNET_free (handle); |
887 | } | 841 | } |
888 | 842 | ||
889 | 843 | ||
890 | static void | 844 | static void |
891 | store_ticket_refs_cont (void *cls, int32_t success, const char *emsg) | ||
892 | { | ||
893 | struct TicketIssueHandle *handle = cls; | ||
894 | handle->ns_qe = NULL; | ||
895 | if (GNUNET_OK != success) { | ||
896 | handle->cb (handle->cb_cls, NULL, GNUNET_SYSERR, | ||
897 | "Error storing updated ticket refs in GNS"); | ||
898 | cleanup_issue_handle (handle); | ||
899 | return; | ||
900 | } | ||
901 | handle->cb (handle->cb_cls, &handle->ticket, GNUNET_OK, NULL); | ||
902 | cleanup_issue_handle (handle); | ||
903 | } | ||
904 | |||
905 | |||
906 | static void | ||
907 | update_ticket_refs (void *cls) | ||
908 | { | ||
909 | struct TicketIssueHandle *handle = cls; | ||
910 | struct GNUNET_GNSRECORD_Data refs_rd[handle->ticket_ref_num]; | ||
911 | struct TicketReference *tr; | ||
912 | |||
913 | tr = handle->ticket_refs_head; | ||
914 | for (int i = 0; i < handle->ticket_ref_num; i++) { | ||
915 | refs_rd[i].data = &tr->ticket; | ||
916 | refs_rd[i].data_size = sizeof (struct GNUNET_RECLAIM_Ticket); | ||
917 | refs_rd[i].expiration_time = GNUNET_TIME_UNIT_DAYS.rel_value_us; | ||
918 | refs_rd[i].record_type = GNUNET_GNSRECORD_TYPE_RECLAIM_TICKETREF; | ||
919 | refs_rd[i].flags = | ||
920 | GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION | GNUNET_GNSRECORD_RF_PRIVATE; | ||
921 | tr = tr->next; | ||
922 | } | ||
923 | |||
924 | handle->ns_qe = GNUNET_NAMESTORE_records_store ( | ||
925 | nsh, &handle->identity, GNUNET_GNS_EMPTY_LABEL_AT, handle->ticket_ref_num, | ||
926 | refs_rd, &store_ticket_refs_cont, handle); | ||
927 | } | ||
928 | |||
929 | |||
930 | static void | ||
931 | ticket_lookup_cb (void *cls, const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone, | ||
932 | const char *label, unsigned int rd_count, | ||
933 | const struct GNUNET_GNSRECORD_Data *rd) | ||
934 | { | ||
935 | struct TicketIssueHandle *handle = cls; | ||
936 | struct TicketReference *tr; | ||
937 | |||
938 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
939 | "Received tickets from local namestore.\n"); | ||
940 | handle->ns_qe = NULL; | ||
941 | for (int i = 0; i < rd_count; i++) { | ||
942 | if (GNUNET_GNSRECORD_TYPE_RECLAIM_TICKETREF != rd[i].record_type) | ||
943 | continue; | ||
944 | tr = GNUNET_new (struct TicketReference); | ||
945 | memcpy (&tr->ticket, rd[i].data, sizeof (struct GNUNET_RECLAIM_Ticket)); | ||
946 | if (0 != memcmp (&tr->ticket.identity, &handle->ticket.identity, | ||
947 | sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey))) { | ||
948 | // Not our ticket | ||
949 | GNUNET_free (tr); | ||
950 | continue; | ||
951 | } | ||
952 | GNUNET_CONTAINER_DLL_insert (handle->ticket_refs_head, | ||
953 | handle->ticket_refs_tail, tr); | ||
954 | handle->ticket_ref_num++; | ||
955 | } | ||
956 | tr = GNUNET_new (struct TicketReference); | ||
957 | tr->ticket = handle->ticket; | ||
958 | tr->attrs = GNUNET_RECLAIM_ATTRIBUTE_list_dup (handle->attrs); | ||
959 | GNUNET_CONTAINER_DLL_insert (handle->ticket_refs_head, | ||
960 | handle->ticket_refs_tail, tr); | ||
961 | handle->ticket_ref_num++; | ||
962 | GNUNET_SCHEDULER_add_now (&update_ticket_refs, handle); | ||
963 | } | ||
964 | |||
965 | |||
966 | /** | ||
967 | * TODO maybe we should cleanup the ATTRREFS here? | ||
968 | */ | ||
969 | static void | ||
970 | ticket_lookup_error_cb (void *cls) | ||
971 | { | ||
972 | struct TicketIssueHandle *handle = cls; | ||
973 | handle->ns_qe = NULL; | ||
974 | handle->cb (handle->cb_cls, &handle->ticket, GNUNET_SYSERR, | ||
975 | "Error checking for ticketsin GNS\n"); | ||
976 | cleanup_issue_handle (handle); | ||
977 | } | ||
978 | |||
979 | static void | ||
980 | store_ticket_issue_cont (void *cls, int32_t success, const char *emsg) | 845 | store_ticket_issue_cont (void *cls, int32_t success, const char *emsg) |
981 | { | 846 | { |
982 | struct TicketIssueHandle *handle = cls; | 847 | struct TicketIssueHandle *handle = cls; |
@@ -987,10 +852,8 @@ store_ticket_issue_cont (void *cls, int32_t success, const char *emsg) | |||
987 | "Error storing AuthZ ticket in GNS"); | 852 | "Error storing AuthZ ticket in GNS"); |
988 | return; | 853 | return; |
989 | } | 854 | } |
990 | /* First, local references to tickets */ | 855 | handle->cb (handle->cb_cls, &handle->ticket, GNUNET_OK, NULL); |
991 | handle->ns_qe = GNUNET_NAMESTORE_records_lookup ( | 856 | cleanup_issue_handle (handle); |
992 | nsh, &handle->identity, GNUNET_GNS_EMPTY_LABEL_AT, | ||
993 | &ticket_lookup_error_cb, handle, &ticket_lookup_cb, handle); | ||
994 | } | 857 | } |
995 | 858 | ||
996 | 859 | ||
@@ -1000,7 +863,7 @@ issue_ticket (struct TicketIssueHandle *ih) | |||
1000 | struct GNUNET_RECLAIM_ATTRIBUTE_ClaimListEntry *le; | 863 | struct GNUNET_RECLAIM_ATTRIBUTE_ClaimListEntry *le; |
1001 | struct GNUNET_GNSRECORD_Data *attrs_record; | 864 | struct GNUNET_GNSRECORD_Data *attrs_record; |
1002 | char *label; | 865 | char *label; |
1003 | size_t list_len = 0; | 866 | size_t list_len = 1; |
1004 | int i; | 867 | int i; |
1005 | 868 | ||
1006 | for (le = ih->attrs->list_head; NULL != le; le = le->next) | 869 | for (le = ih->attrs->list_head; NULL != le; le = le->next) |
@@ -1017,6 +880,12 @@ issue_ticket (struct TicketIssueHandle *ih) | |||
1017 | attrs_record[i].flags = GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION; | 880 | attrs_record[i].flags = GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION; |
1018 | i++; | 881 | i++; |
1019 | } | 882 | } |
883 | attrs_record[i].data = &ih->ticket; | ||
884 | attrs_record[i].data_size = sizeof (struct GNUNET_RECLAIM_Ticket); | ||
885 | attrs_record[i].expiration_time = GNUNET_TIME_UNIT_DAYS.rel_value_us; | ||
886 | attrs_record[i].record_type = GNUNET_GNSRECORD_TYPE_RECLAIM_TICKET; | ||
887 | attrs_record[i].flags = | ||
888 | GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION | GNUNET_GNSRECORD_RF_PRIVATE; | ||
1020 | 889 | ||
1021 | label = | 890 | label = |
1022 | GNUNET_STRINGS_data_to_string_alloc (&ih->ticket.rnd, sizeof (uint64_t)); | 891 | GNUNET_STRINGS_data_to_string_alloc (&ih->ticket.rnd, sizeof (uint64_t)); |
@@ -1055,49 +924,11 @@ RECLAIM_TICKETS_issue (const struct GNUNET_CRYPTO_EcdsaPrivateKey *identity, | |||
1055 | static void | 924 | static void |
1056 | cleanup_iter (struct RECLAIM_TICKETS_Iterator *iter) | 925 | cleanup_iter (struct RECLAIM_TICKETS_Iterator *iter) |
1057 | { | 926 | { |
1058 | struct TicketReference *tr; | 927 | if (NULL != iter->ns_it) |
1059 | struct TicketReference *tr_tmp; | 928 | GNUNET_NAMESTORE_zone_iteration_stop (iter->ns_it); |
1060 | if (NULL != iter->ns_qe) | ||
1061 | GNUNET_NAMESTORE_cancel (iter->ns_qe); | ||
1062 | for (tr = iter->tickets_head; NULL != tr;) { | ||
1063 | if (NULL != tr->attrs) | ||
1064 | GNUNET_RECLAIM_ATTRIBUTE_list_destroy (tr->attrs); | ||
1065 | tr_tmp = tr; | ||
1066 | tr = tr->next; | ||
1067 | GNUNET_free (tr_tmp); | ||
1068 | } | ||
1069 | GNUNET_free (iter); | 929 | GNUNET_free (iter); |
1070 | } | 930 | } |
1071 | 931 | ||
1072 | static void | ||
1073 | do_cleanup_iter (void *cls) | ||
1074 | { | ||
1075 | struct RECLAIM_TICKETS_Iterator *iter = cls; | ||
1076 | cleanup_iter (iter); | ||
1077 | } | ||
1078 | |||
1079 | /** | ||
1080 | * Perform ticket iteration step | ||
1081 | * | ||
1082 | * @param ti ticket iterator to process | ||
1083 | */ | ||
1084 | static void | ||
1085 | run_ticket_iteration_round (struct RECLAIM_TICKETS_Iterator *iter) | ||
1086 | { | ||
1087 | struct TicketReference *tr; | ||
1088 | if (NULL == iter->tickets_head) { | ||
1089 | // No more tickets | ||
1090 | iter->cb (iter->cb_cls, NULL); | ||
1091 | GNUNET_SCHEDULER_add_now (&do_cleanup_iter, iter); | ||
1092 | return; | ||
1093 | } | ||
1094 | tr = iter->tickets_head; | ||
1095 | GNUNET_CONTAINER_DLL_remove (iter->tickets_head, iter->tickets_tail, tr); | ||
1096 | iter->cb (iter->cb_cls, &tr->ticket); | ||
1097 | if (NULL != tr->attrs) | ||
1098 | GNUNET_RECLAIM_ATTRIBUTE_list_destroy (tr->attrs); | ||
1099 | GNUNET_free (tr); | ||
1100 | } | ||
1101 | 932 | ||
1102 | static void | 933 | static void |
1103 | collect_tickets_cb (void *cls, const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone, | 934 | collect_tickets_cb (void *cls, const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone, |
@@ -1105,46 +936,52 @@ collect_tickets_cb (void *cls, const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone, | |||
1105 | const struct GNUNET_GNSRECORD_Data *rd) | 936 | const struct GNUNET_GNSRECORD_Data *rd) |
1106 | { | 937 | { |
1107 | struct RECLAIM_TICKETS_Iterator *iter = cls; | 938 | struct RECLAIM_TICKETS_Iterator *iter = cls; |
1108 | struct TicketReference *tr; | ||
1109 | iter->ns_qe = NULL; | ||
1110 | 939 | ||
1111 | for (int i = 0; i < rd_count; i++) { | 940 | for (int i = 0; i < rd_count; i++) { |
1112 | if (GNUNET_GNSRECORD_TYPE_RECLAIM_TICKETREF != rd[i].record_type) | 941 | if (GNUNET_GNSRECORD_TYPE_RECLAIM_TICKET != rd[i].record_type) |
1113 | continue; | 942 | continue; |
1114 | tr = GNUNET_new (struct TicketReference); | 943 | iter->cb (iter->cb_cls, (struct GNUNET_RECLAIM_Ticket *)rd[i].data); |
1115 | memcpy (&tr->ticket, rd[i].data, sizeof (struct GNUNET_RECLAIM_Ticket)); | 944 | return; |
1116 | if (0 != memcmp (&tr->ticket.identity, &iter->identity_pub, | ||
1117 | sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey))) { | ||
1118 | // Not our ticket | ||
1119 | GNUNET_free (tr); | ||
1120 | continue; | ||
1121 | } | ||
1122 | GNUNET_CONTAINER_DLL_insert (iter->tickets_head, iter->tickets_tail, tr); | ||
1123 | } | 945 | } |
1124 | run_ticket_iteration_round (iter); | 946 | GNUNET_NAMESTORE_zone_iterator_next (iter->ns_it, 1); |
1125 | } | 947 | } |
1126 | 948 | ||
949 | |||
950 | static void | ||
951 | collect_tickets_finished_cb (void *cls) | ||
952 | { | ||
953 | struct RECLAIM_TICKETS_Iterator *iter = cls; | ||
954 | iter->ns_it = NULL; | ||
955 | iter->cb (iter->cb_cls, NULL); | ||
956 | cleanup_iter (iter); | ||
957 | } | ||
958 | |||
959 | |||
1127 | static void | 960 | static void |
1128 | collect_tickets_error_cb (void *cls) | 961 | collect_tickets_error_cb (void *cls) |
1129 | { | 962 | { |
1130 | struct RECLAIM_TICKETS_Iterator *iter = cls; | 963 | struct RECLAIM_TICKETS_Iterator *iter = cls; |
1131 | iter->ns_qe = NULL; | 964 | iter->ns_it = NULL; |
1132 | iter->cb (iter->cb_cls, NULL); | 965 | iter->cb (iter->cb_cls, NULL); |
1133 | cleanup_iter (iter); | 966 | cleanup_iter (iter); |
1134 | } | 967 | } |
1135 | 968 | ||
969 | |||
1136 | void | 970 | void |
1137 | RECLAIM_TICKETS_iteration_next (struct RECLAIM_TICKETS_Iterator *iter) | 971 | RECLAIM_TICKETS_iteration_next (struct RECLAIM_TICKETS_Iterator *iter) |
1138 | { | 972 | { |
1139 | run_ticket_iteration_round (iter); | 973 | GNUNET_NAMESTORE_zone_iterator_next (iter->ns_it, 1); |
1140 | } | 974 | } |
1141 | 975 | ||
976 | |||
1142 | void | 977 | void |
1143 | RECLAIM_TICKETS_iteration_stop (struct RECLAIM_TICKETS_Iterator *iter) | 978 | RECLAIM_TICKETS_iteration_stop (struct RECLAIM_TICKETS_Iterator *iter) |
1144 | { | 979 | { |
980 | GNUNET_NAMESTORE_zone_iteration_stop (iter->ns_it); | ||
1145 | cleanup_iter (iter); | 981 | cleanup_iter (iter); |
1146 | } | 982 | } |
1147 | 983 | ||
984 | |||
1148 | struct RECLAIM_TICKETS_Iterator * | 985 | struct RECLAIM_TICKETS_Iterator * |
1149 | RECLAIM_TICKETS_iteration_start ( | 986 | RECLAIM_TICKETS_iteration_start ( |
1150 | const struct GNUNET_CRYPTO_EcdsaPrivateKey *identity, | 987 | const struct GNUNET_CRYPTO_EcdsaPrivateKey *identity, |
@@ -1153,13 +990,11 @@ RECLAIM_TICKETS_iteration_start ( | |||
1153 | struct RECLAIM_TICKETS_Iterator *iter; | 990 | struct RECLAIM_TICKETS_Iterator *iter; |
1154 | 991 | ||
1155 | iter = GNUNET_new (struct RECLAIM_TICKETS_Iterator); | 992 | iter = GNUNET_new (struct RECLAIM_TICKETS_Iterator); |
1156 | iter->identity = *identity; | ||
1157 | GNUNET_CRYPTO_ecdsa_key_get_public (identity, &iter->identity_pub); | ||
1158 | iter->cb = cb; | 993 | iter->cb = cb; |
1159 | iter->cb_cls = cb_cls; | 994 | iter->cb_cls = cb_cls; |
1160 | iter->ns_qe = GNUNET_NAMESTORE_records_lookup ( | 995 | iter->ns_it = GNUNET_NAMESTORE_zone_iteration_start ( |
1161 | nsh, identity, GNUNET_GNS_EMPTY_LABEL_AT, &collect_tickets_error_cb, iter, | 996 | nsh, identity, &collect_tickets_error_cb, iter, &collect_tickets_cb, iter, |
1162 | &collect_tickets_cb, iter); | 997 | &collect_tickets_finished_cb, iter); |
1163 | return iter; | 998 | return iter; |
1164 | } | 999 | } |
1165 | 1000 | ||