2 This file is part of GNUnet.
3 Copyright (C) 2009-2018 GNUnet e.V.
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your 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 Affero General Public License for more details.
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
20 * @file rest-plugins/json_reclaim.c
21 * @brief JSON handling of reclaim data
22 * @author Martin Schanzenbach
25 #include "gnunet_util_lib.h"
26 #include "gnunet_json_lib.h"
27 #include "gnunet_reclaim_service.h"
28 #include "gnunet_reclaim_attribute_lib.h"
31 * Parse given JSON object to a claim
33 * @param cls closure, NULL
34 * @param root the json object representing data
35 * @param spec where to write the data
36 * @return #GNUNET_OK upon successful parsing; #GNUNET_SYSERR upon error
39 parse_attr (void *cls,
41 struct GNUNET_JSON_Specification *spec)
43 struct GNUNET_RECLAIM_ATTRIBUTE_Claim *attr;
53 GNUNET_assert(NULL != root);
55 if(!json_is_object(root))
57 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
58 "Error json is not array nor object!\n");
61 //interpret single attribute
62 unpack_state = json_unpack(root,
63 "{s:s, s:s, s:s, s:s!}",
68 if (0 != unpack_state)
70 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
71 "Error json object has a wrong format!\n");
74 type = GNUNET_RECLAIM_ATTRIBUTE_typename_to_number (type_str);
75 if (GNUNET_SYSERR == (GNUNET_RECLAIM_ATTRIBUTE_string_to_value (type,
80 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
81 "Attribute value invalid!\n");
84 attr = GNUNET_RECLAIM_ATTRIBUTE_claim_new (name_str,
88 *(struct GNUNET_RECLAIM_ATTRIBUTE_Claim **) spec->ptr = attr;
93 * Cleanup data left from parsing RSA public key.
95 * @param cls closure, NULL
96 * @param[out] spec where to free the data
99 clean_attr (void *cls, struct GNUNET_JSON_Specification *spec)
101 struct GNUNET_RECLAIM_ATTRIBUTE_Claim **attr;
102 attr = (struct GNUNET_RECLAIM_ATTRIBUTE_Claim **) spec->ptr;
111 * JSON Specification for Reclaim claims.
113 * @param ticket struct of GNUNET_RECLAIM_ATTRIBUTE_Claim to fill
114 * @return JSON Specification
116 struct GNUNET_JSON_Specification
117 GNUNET_RECLAIM_JSON_spec_claim (struct GNUNET_RECLAIM_ATTRIBUTE_Claim **attr)
119 struct GNUNET_JSON_Specification ret = {
120 .parser = &parse_attr,
121 .cleaner = &clean_attr,
132 * Parse given JSON object to a ticket
134 * @param cls closure, NULL
135 * @param root the json object representing data
136 * @param spec where to write the data
137 * @return #GNUNET_OK upon successful parsing; #GNUNET_SYSERR upon error
140 parse_ticket (void *cls,
142 struct GNUNET_JSON_Specification *spec)
144 struct GNUNET_RECLAIM_Ticket *ticket;
150 GNUNET_assert(NULL != root);
152 if(!json_is_object(root))
154 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
155 "Error json is not array nor object!\n");
156 return GNUNET_SYSERR;
158 //interpret single ticket
159 unpack_state = json_unpack(root,
162 "audience", &aud_str,
163 "identity", &id_str);
164 if (0 != unpack_state)
166 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
167 "Error json object has a wrong format!\n");
168 return GNUNET_SYSERR;
170 ticket = GNUNET_new (struct GNUNET_RECLAIM_Ticket);
171 if (GNUNET_OK != GNUNET_STRINGS_string_to_data (rnd_str,
176 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,"Rnd invalid\n");
178 return GNUNET_SYSERR;
180 GNUNET_STRINGS_string_to_data (id_str,
183 sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey));
185 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,"Identity invalid\n");
187 return GNUNET_SYSERR;
190 GNUNET_STRINGS_string_to_data (aud_str,
193 sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey));
195 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,"Audience invalid\n");
197 return GNUNET_SYSERR;
200 *(struct GNUNET_RECLAIM_Ticket **) spec->ptr = ticket;
205 * Cleanup data left from parsing RSA public key.
207 * @param cls closure, NULL
208 * @param[out] spec where to free the data
211 clean_ticket (void *cls, struct GNUNET_JSON_Specification *spec)
213 struct GNUNET_RECLAIM_Ticket **ticket;
214 ticket = (struct GNUNET_RECLAIM_Ticket **) spec->ptr;
217 GNUNET_free(*ticket);
223 * JSON Specification for Reclaim tickets.
225 * @param ticket struct of GNUNET_RECLAIM_Ticket to fill
226 * @return JSON Specification
228 struct GNUNET_JSON_Specification
229 GNUNET_RECLAIM_JSON_spec_ticket (struct GNUNET_RECLAIM_Ticket **ticket)
231 struct GNUNET_JSON_Specification ret = {
232 .parser = &parse_ticket,
233 .cleaner = &clean_ticket,