diff options
Diffstat (limited to 'src/reclaim/json_reclaim.c')
-rw-r--r-- | src/reclaim/json_reclaim.c | 166 |
1 files changed, 72 insertions, 94 deletions
diff --git a/src/reclaim/json_reclaim.c b/src/reclaim/json_reclaim.c index 0fe9150d9..79110490a 100644 --- a/src/reclaim/json_reclaim.c +++ b/src/reclaim/json_reclaim.c | |||
@@ -24,10 +24,13 @@ | |||
24 | * @author Martin Schanzenbach | 24 | * @author Martin Schanzenbach |
25 | */ | 25 | */ |
26 | #include "platform.h" | 26 | #include "platform.h" |
27 | |||
27 | #include "gnunet_util_lib.h" | 28 | #include "gnunet_util_lib.h" |
29 | |||
28 | #include "gnunet_json_lib.h" | 30 | #include "gnunet_json_lib.h" |
29 | #include "gnunet_reclaim_service.h" | ||
30 | #include "gnunet_reclaim_attribute_lib.h" | 31 | #include "gnunet_reclaim_attribute_lib.h" |
32 | #include "gnunet_reclaim_service.h" | ||
33 | |||
31 | 34 | ||
32 | /** | 35 | /** |
33 | * Parse given JSON object to a claim | 36 | * Parse given JSON object to a claim |
@@ -38,54 +41,48 @@ | |||
38 | * @return #GNUNET_OK upon successful parsing; #GNUNET_SYSERR upon error | 41 | * @return #GNUNET_OK upon successful parsing; #GNUNET_SYSERR upon error |
39 | */ | 42 | */ |
40 | static int | 43 | static int |
41 | parse_attr (void *cls, | 44 | parse_attr (void *cls, json_t *root, struct GNUNET_JSON_Specification *spec) |
42 | json_t *root, | ||
43 | struct GNUNET_JSON_Specification *spec) | ||
44 | { | 45 | { |
45 | struct GNUNET_RECLAIM_ATTRIBUTE_Claim *attr; | 46 | struct GNUNET_RECLAIM_ATTRIBUTE_Claim *attr; |
46 | const char* name_str; | 47 | const char *name_str; |
47 | const char* val_str; | 48 | const char *val_str; |
48 | const char* type_str; | 49 | const char *type_str; |
50 | const char *id_str; | ||
49 | char *data; | 51 | char *data; |
50 | int unpack_state; | 52 | int unpack_state; |
51 | uint32_t type; | 53 | uint32_t type; |
52 | size_t data_size; | 54 | size_t data_size; |
53 | 55 | ||
54 | GNUNET_assert(NULL != root); | 56 | GNUNET_assert (NULL != root); |
55 | 57 | ||
56 | if(!json_is_object(root)) | 58 | if (!json_is_object (root)) { |
57 | { | ||
58 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | 59 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, |
59 | "Error json is not array nor object!\n"); | 60 | "Error json is not array nor object!\n"); |
60 | return GNUNET_SYSERR; | 61 | return GNUNET_SYSERR; |
61 | } | 62 | } |
62 | //interpret single attribute | 63 | // interpret single attribute |
63 | unpack_state = json_unpack(root, | 64 | unpack_state = |
64 | "{s:s, s:s, s:s!}", | 65 | json_unpack (root, "{s:s, s:s, s:s, s:s!}", "name", &name_str, "id", |
65 | "name", &name_str, | 66 | &id_str, "type", &type_str, "value", &val_str); |
66 | "type", &type_str, | 67 | if (0 != unpack_state) { |
67 | "value", &val_str); | 68 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, |
68 | if (0 != unpack_state) | 69 | "Error json object has a wrong format!\n"); |
69 | { | ||
70 | GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, | ||
71 | "Error json object has a wrong format!\n"); | ||
72 | return GNUNET_SYSERR; | 70 | return GNUNET_SYSERR; |
73 | } | 71 | } |
74 | type = GNUNET_RECLAIM_ATTRIBUTE_typename_to_number (type_str); | 72 | type = GNUNET_RECLAIM_ATTRIBUTE_typename_to_number (type_str); |
75 | if (GNUNET_SYSERR == (GNUNET_RECLAIM_ATTRIBUTE_string_to_value (type, | 73 | if (GNUNET_SYSERR == (GNUNET_RECLAIM_ATTRIBUTE_string_to_value ( |
76 | val_str, | 74 | type, val_str, (void **)&data, &data_size))) { |
77 | (void**)&data, | 75 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Attribute value invalid!\n"); |
78 | &data_size))) | ||
79 | { | ||
80 | GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, | ||
81 | "Attribute value invalid!\n"); | ||
82 | return GNUNET_SYSERR; | 76 | return GNUNET_SYSERR; |
83 | } | 77 | } |
84 | attr = GNUNET_RECLAIM_ATTRIBUTE_claim_new (name_str, | 78 | attr = GNUNET_RECLAIM_ATTRIBUTE_claim_new (name_str, type, data, data_size); |
85 | type, | 79 | if (0 == strlen (id_str)) |
86 | data, | 80 | attr->id = 0; |
87 | data_size); | 81 | else |
88 | *(struct GNUNET_RECLAIM_ATTRIBUTE_Claim **) spec->ptr = attr; | 82 | GNUNET_STRINGS_string_to_data (id_str, strlen (id_str), &attr->id, |
83 | sizeof (uint64_t)); | ||
84 | |||
85 | *(struct GNUNET_RECLAIM_ATTRIBUTE_Claim **)spec->ptr = attr; | ||
89 | return GNUNET_OK; | 86 | return GNUNET_OK; |
90 | } | 87 | } |
91 | 88 | ||
@@ -99,10 +96,9 @@ static void | |||
99 | clean_attr (void *cls, struct GNUNET_JSON_Specification *spec) | 96 | clean_attr (void *cls, struct GNUNET_JSON_Specification *spec) |
100 | { | 97 | { |
101 | struct GNUNET_RECLAIM_ATTRIBUTE_Claim **attr; | 98 | struct GNUNET_RECLAIM_ATTRIBUTE_Claim **attr; |
102 | attr = (struct GNUNET_RECLAIM_ATTRIBUTE_Claim **) spec->ptr; | 99 | attr = (struct GNUNET_RECLAIM_ATTRIBUTE_Claim **)spec->ptr; |
103 | if (NULL != *attr) | 100 | if (NULL != *attr) { |
104 | { | 101 | GNUNET_free (*attr); |
105 | GNUNET_free(*attr); | ||
106 | *attr = NULL; | 102 | *attr = NULL; |
107 | } | 103 | } |
108 | } | 104 | } |
@@ -116,15 +112,13 @@ clean_attr (void *cls, struct GNUNET_JSON_Specification *spec) | |||
116 | struct GNUNET_JSON_Specification | 112 | struct GNUNET_JSON_Specification |
117 | GNUNET_RECLAIM_JSON_spec_claim (struct GNUNET_RECLAIM_ATTRIBUTE_Claim **attr) | 113 | GNUNET_RECLAIM_JSON_spec_claim (struct GNUNET_RECLAIM_ATTRIBUTE_Claim **attr) |
118 | { | 114 | { |
119 | struct GNUNET_JSON_Specification ret = { | 115 | struct GNUNET_JSON_Specification ret = {.parser = &parse_attr, |
120 | .parser = &parse_attr, | 116 | .cleaner = &clean_attr, |
121 | .cleaner = &clean_attr, | 117 | .cls = NULL, |
122 | .cls = NULL, | 118 | .field = NULL, |
123 | .field = NULL, | 119 | .ptr = attr, |
124 | .ptr = attr, | 120 | .ptr_size = 0, |
125 | .ptr_size = 0, | 121 | .size_ptr = NULL}; |
126 | .size_ptr = NULL | ||
127 | }; | ||
128 | *attr = NULL; | 122 | *attr = NULL; |
129 | return ret; | 123 | return ret; |
130 | } | 124 | } |
@@ -137,67 +131,54 @@ GNUNET_RECLAIM_JSON_spec_claim (struct GNUNET_RECLAIM_ATTRIBUTE_Claim **attr) | |||
137 | * @return #GNUNET_OK upon successful parsing; #GNUNET_SYSERR upon error | 131 | * @return #GNUNET_OK upon successful parsing; #GNUNET_SYSERR upon error |
138 | */ | 132 | */ |
139 | static int | 133 | static int |
140 | parse_ticket (void *cls, | 134 | parse_ticket (void *cls, json_t *root, struct GNUNET_JSON_Specification *spec) |
141 | json_t *root, | ||
142 | struct GNUNET_JSON_Specification *spec) | ||
143 | { | 135 | { |
144 | struct GNUNET_RECLAIM_Ticket *ticket; | 136 | struct GNUNET_RECLAIM_Ticket *ticket; |
145 | const char* rnd_str; | 137 | const char *rnd_str; |
146 | const char* aud_str; | 138 | const char *aud_str; |
147 | const char* id_str; | 139 | const char *id_str; |
148 | int unpack_state; | 140 | int unpack_state; |
149 | 141 | ||
150 | GNUNET_assert(NULL != root); | 142 | GNUNET_assert (NULL != root); |
151 | 143 | ||
152 | if(!json_is_object(root)) | 144 | if (!json_is_object (root)) { |
153 | { | ||
154 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | 145 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, |
155 | "Error json is not array nor object!\n"); | 146 | "Error json is not array nor object!\n"); |
156 | return GNUNET_SYSERR; | 147 | return GNUNET_SYSERR; |
157 | } | 148 | } |
158 | //interpret single ticket | 149 | // interpret single ticket |
159 | unpack_state = json_unpack(root, | 150 | unpack_state = json_unpack (root, "{s:s, s:s, s:s!}", "rnd", &rnd_str, |
160 | "{s:s, s:s, s:s!}", | 151 | "audience", &aud_str, "identity", &id_str); |
161 | "rnd", &rnd_str, | 152 | if (0 != unpack_state) { |
162 | "audience", &aud_str, | 153 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, |
163 | "identity", &id_str); | 154 | "Error json object has a wrong format!\n"); |
164 | if (0 != unpack_state) | ||
165 | { | ||
166 | GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, | ||
167 | "Error json object has a wrong format!\n"); | ||
168 | return GNUNET_SYSERR; | 155 | return GNUNET_SYSERR; |
169 | } | 156 | } |
170 | ticket = GNUNET_new (struct GNUNET_RECLAIM_Ticket); | 157 | ticket = GNUNET_new (struct GNUNET_RECLAIM_Ticket); |
171 | if (GNUNET_OK != GNUNET_STRINGS_string_to_data (rnd_str, | 158 | if (GNUNET_OK != GNUNET_STRINGS_string_to_data (rnd_str, strlen (rnd_str), |
172 | strlen (rnd_str), | ||
173 | &ticket->rnd, | 159 | &ticket->rnd, |
174 | sizeof (uint64_t))) | 160 | sizeof (uint64_t))) { |
175 | { | 161 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Rnd invalid\n"); |
176 | GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,"Rnd invalid\n"); | 162 | GNUNET_free (ticket); |
177 | GNUNET_free(ticket); | ||
178 | return GNUNET_SYSERR; | 163 | return GNUNET_SYSERR; |
179 | } | 164 | } |
180 | GNUNET_STRINGS_string_to_data (id_str, | 165 | GNUNET_STRINGS_string_to_data (id_str, strlen (id_str), &ticket->identity, |
181 | strlen (id_str), | ||
182 | &ticket->identity, | ||
183 | sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey)); | 166 | sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey)); |
184 | { | 167 | { |
185 | GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,"Identity invalid\n"); | 168 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Identity invalid\n"); |
186 | GNUNET_free(ticket); | 169 | GNUNET_free (ticket); |
187 | return GNUNET_SYSERR; | 170 | return GNUNET_SYSERR; |
188 | } | 171 | } |
189 | 172 | ||
190 | GNUNET_STRINGS_string_to_data (aud_str, | 173 | GNUNET_STRINGS_string_to_data (aud_str, strlen (aud_str), &ticket->audience, |
191 | strlen (aud_str), | ||
192 | &ticket->audience, | ||
193 | sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey)); | 174 | sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey)); |
194 | { | 175 | { |
195 | GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,"Audience invalid\n"); | 176 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Audience invalid\n"); |
196 | GNUNET_free(ticket); | 177 | GNUNET_free (ticket); |
197 | return GNUNET_SYSERR; | 178 | return GNUNET_SYSERR; |
198 | } | 179 | } |
199 | 180 | ||
200 | *(struct GNUNET_RECLAIM_Ticket **) spec->ptr = ticket; | 181 | *(struct GNUNET_RECLAIM_Ticket **)spec->ptr = ticket; |
201 | return GNUNET_OK; | 182 | return GNUNET_OK; |
202 | } | 183 | } |
203 | 184 | ||
@@ -211,10 +192,9 @@ static void | |||
211 | clean_ticket (void *cls, struct GNUNET_JSON_Specification *spec) | 192 | clean_ticket (void *cls, struct GNUNET_JSON_Specification *spec) |
212 | { | 193 | { |
213 | struct GNUNET_RECLAIM_Ticket **ticket; | 194 | struct GNUNET_RECLAIM_Ticket **ticket; |
214 | ticket = (struct GNUNET_RECLAIM_Ticket **) spec->ptr; | 195 | ticket = (struct GNUNET_RECLAIM_Ticket **)spec->ptr; |
215 | if (NULL != *ticket) | 196 | if (NULL != *ticket) { |
216 | { | 197 | GNUNET_free (*ticket); |
217 | GNUNET_free(*ticket); | ||
218 | *ticket = NULL; | 198 | *ticket = NULL; |
219 | } | 199 | } |
220 | } | 200 | } |
@@ -228,15 +208,13 @@ clean_ticket (void *cls, struct GNUNET_JSON_Specification *spec) | |||
228 | struct GNUNET_JSON_Specification | 208 | struct GNUNET_JSON_Specification |
229 | GNUNET_RECLAIM_JSON_spec_ticket (struct GNUNET_RECLAIM_Ticket **ticket) | 209 | GNUNET_RECLAIM_JSON_spec_ticket (struct GNUNET_RECLAIM_Ticket **ticket) |
230 | { | 210 | { |
231 | struct GNUNET_JSON_Specification ret = { | 211 | struct GNUNET_JSON_Specification ret = {.parser = &parse_ticket, |
232 | .parser = &parse_ticket, | 212 | .cleaner = &clean_ticket, |
233 | .cleaner = &clean_ticket, | 213 | .cls = NULL, |
234 | .cls = NULL, | 214 | .field = NULL, |
235 | .field = NULL, | 215 | .ptr = ticket, |
236 | .ptr = ticket, | 216 | .ptr_size = 0, |
237 | .ptr_size = 0, | 217 | .size_ptr = NULL}; |
238 | .size_ptr = NULL | ||
239 | }; | ||
240 | *ticket = NULL; | 218 | *ticket = NULL; |
241 | return ret; | 219 | return ret; |
242 | } | 220 | } |