diff options
Diffstat (limited to 'src/json')
-rw-r--r-- | src/json/Makefile.am | 7 | ||||
-rw-r--r-- | src/json/json_generator.c | 108 | ||||
-rw-r--r-- | src/json/json_gnsrecord.c | 279 |
3 files changed, 2 insertions, 392 deletions
diff --git a/src/json/Makefile.am b/src/json/Makefile.am index 2ed05c35a..d4ea38adf 100644 --- a/src/json/Makefile.am +++ b/src/json/Makefile.am | |||
@@ -18,18 +18,15 @@ libgnunetjson_la_SOURCES = \ | |||
18 | json.c \ | 18 | json.c \ |
19 | json_mhd.c \ | 19 | json_mhd.c \ |
20 | json_generator.c \ | 20 | json_generator.c \ |
21 | json_helper.c \ | 21 | json_helper.c |
22 | json_gnsrecord.c | ||
23 | libgnunetjson_la_LIBADD = \ | 22 | libgnunetjson_la_LIBADD = \ |
24 | $(top_builddir)/src/util/libgnunetutil.la \ | 23 | $(top_builddir)/src/util/libgnunetutil.la \ |
25 | $(top_builddir)/src/gnsrecord/libgnunetgnsrecord.la \ | ||
26 | -ljansson \ | 24 | -ljansson \ |
27 | $(MHD_LIBS) \ | 25 | $(MHD_LIBS) \ |
28 | $(XLIB) \ | 26 | $(XLIB) \ |
29 | $(Z_LIBS) | 27 | $(Z_LIBS) |
30 | libgnunetjson_la_DEPENDENCIES = \ | 28 | libgnunetjson_la_DEPENDENCIES = \ |
31 | $(top_builddir)/src/util/libgnunetutil.la \ | 29 | $(top_builddir)/src/util/libgnunetutil.la |
32 | $(top_builddir)/src/gnsrecord/libgnunetgnsrecord.la | ||
33 | 30 | ||
34 | 31 | ||
35 | check_PROGRAMS = \ | 32 | check_PROGRAMS = \ |
diff --git a/src/json/json_generator.c b/src/json/json_generator.c index 3f82a5f17..5806eb174 100644 --- a/src/json/json_generator.c +++ b/src/json/json_generator.c | |||
@@ -201,113 +201,5 @@ GNUNET_JSON_from_rsa_signature (const struct GNUNET_CRYPTO_RsaSignature *sig) | |||
201 | } | 201 | } |
202 | 202 | ||
203 | 203 | ||
204 | /** | ||
205 | * Convert GNS record to JSON. | ||
206 | * | ||
207 | * @param rname name of record | ||
208 | * @param rd record data | ||
209 | * @return corresponding JSON encoding | ||
210 | */ | ||
211 | json_t * | ||
212 | GNUNET_JSON_from_gnsrecord (const char*rname, | ||
213 | const struct GNUNET_GNSRECORD_Data *rd, | ||
214 | unsigned int rd_count) | ||
215 | { | ||
216 | struct GNUNET_TIME_Absolute abs_exp; | ||
217 | struct GNUNET_TIME_Relative rel_exp; | ||
218 | const char *expiration_time_str; | ||
219 | const char *record_type_str; | ||
220 | char *value_str; | ||
221 | json_t *data; | ||
222 | json_t *record; | ||
223 | json_t *records; | ||
224 | |||
225 | data = json_object (); | ||
226 | if (NULL == data) | ||
227 | { | ||
228 | GNUNET_break (0); | ||
229 | return NULL; | ||
230 | } | ||
231 | if (0 != | ||
232 | json_object_set_new (data, | ||
233 | "record_name", | ||
234 | json_string (rname))) | ||
235 | { | ||
236 | GNUNET_break (0); | ||
237 | json_decref (data); | ||
238 | return NULL; | ||
239 | } | ||
240 | records = json_array (); | ||
241 | if (NULL == records) | ||
242 | { | ||
243 | GNUNET_break (0); | ||
244 | json_decref (data); | ||
245 | return NULL; | ||
246 | } | ||
247 | for (int i = 0; i < rd_count; i++) | ||
248 | { | ||
249 | value_str = GNUNET_GNSRECORD_value_to_string (rd[i].record_type, | ||
250 | rd[i].data, | ||
251 | rd[i].data_size); | ||
252 | if (GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION & rd[i].flags) | ||
253 | { | ||
254 | rel_exp.rel_value_us = rd[i].expiration_time; | ||
255 | expiration_time_str = GNUNET_STRINGS_relative_time_to_string (rel_exp, | ||
256 | GNUNET_NO); | ||
257 | } | ||
258 | else | ||
259 | { | ||
260 | abs_exp.abs_value_us = rd[i].expiration_time; | ||
261 | expiration_time_str = GNUNET_STRINGS_absolute_time_to_string (abs_exp); | ||
262 | } | ||
263 | record_type_str = GNUNET_GNSRECORD_number_to_typename (rd[i].record_type); | ||
264 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
265 | "Packing %s %s %s %d\n", | ||
266 | value_str, record_type_str, expiration_time_str, rd[i].flags); | ||
267 | record = json_pack ("{s:s,s:s,s:s,s:b,s:b,s:b,s:b}", | ||
268 | "value", | ||
269 | value_str, | ||
270 | "record_type", | ||
271 | record_type_str, | ||
272 | "expiration_time", | ||
273 | expiration_time_str, | ||
274 | "private", | ||
275 | rd[i].flags & GNUNET_GNSRECORD_RF_PRIVATE, | ||
276 | "relative_expiration", | ||
277 | rd[i].flags & GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION, | ||
278 | "supplemental", | ||
279 | rd[i].flags & GNUNET_GNSRECORD_RF_SUPPLEMENTAL, | ||
280 | "shadow", | ||
281 | rd[i].flags & GNUNET_GNSRECORD_RF_SHADOW_RECORD); | ||
282 | GNUNET_free (value_str); | ||
283 | if (NULL == record) | ||
284 | { | ||
285 | GNUNET_break (0); | ||
286 | json_decref (records); | ||
287 | json_decref (data); | ||
288 | return NULL; | ||
289 | } | ||
290 | if (0 != | ||
291 | json_array_append_new (records, | ||
292 | record)) | ||
293 | { | ||
294 | GNUNET_break (0); | ||
295 | json_decref (records); | ||
296 | json_decref (data); | ||
297 | return NULL; | ||
298 | } | ||
299 | } | ||
300 | if (0 != | ||
301 | json_object_set_new (data, | ||
302 | "data", | ||
303 | records)) | ||
304 | { | ||
305 | GNUNET_break (0); | ||
306 | json_decref (data); | ||
307 | return NULL; | ||
308 | } | ||
309 | return data; | ||
310 | } | ||
311 | |||
312 | 204 | ||
313 | /* End of json/json_generator.c */ | 205 | /* End of json/json_generator.c */ |
diff --git a/src/json/json_gnsrecord.c b/src/json/json_gnsrecord.c deleted file mode 100644 index 7e11aba94..000000000 --- a/src/json/json_gnsrecord.c +++ /dev/null | |||
@@ -1,279 +0,0 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet. | ||
3 | Copyright (C) 2009-2013 GNUnet e.V. | ||
4 | |||
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. | ||
9 | |||
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. | ||
14 | |||
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/>. | ||
17 | |||
18 | SPDX-License-Identifier: AGPL3.0-or-later | ||
19 | */ | ||
20 | |||
21 | /** | ||
22 | * @file json/json_gnsrecord.c | ||
23 | * @brief JSON handling of GNS record data | ||
24 | * @author Philippe Buschmann | ||
25 | */ | ||
26 | #include "platform.h" | ||
27 | #include "gnunet_util_lib.h" | ||
28 | #include "gnunet_json_lib.h" | ||
29 | |||
30 | #define GNUNET_JSON_GNSRECORD_VALUE "value" | ||
31 | #define GNUNET_JSON_GNSRECORD_RECORD_DATA "data" | ||
32 | #define GNUNET_JSON_GNSRECORD_TYPE "record_type" | ||
33 | #define GNUNET_JSON_GNSRECORD_EXPIRATION_TIME "expiration_time" | ||
34 | #define GNUNET_JSON_GNSRECORD_FLAG_PRIVATE "private" | ||
35 | #define GNUNET_JSON_GNSRECORD_FLAG_SUPPLEMENTAL "supplemental" | ||
36 | #define GNUNET_JSON_GNSRECORD_FLAG_RELATIVE "relative_expiration" | ||
37 | #define GNUNET_JSON_GNSRECORD_FLAG_SHADOW "shadow" | ||
38 | #define GNUNET_JSON_GNSRECORD_RECORD_NAME "record_name" | ||
39 | #define GNUNET_JSON_GNSRECORD_NEVER "never" | ||
40 | |||
41 | struct GnsRecordInfo | ||
42 | { | ||
43 | char **name; | ||
44 | |||
45 | unsigned int *rd_count; | ||
46 | |||
47 | struct GNUNET_GNSRECORD_Data **rd; | ||
48 | }; | ||
49 | |||
50 | |||
51 | static void | ||
52 | cleanup_recordinfo (struct GnsRecordInfo *gnsrecord_info) | ||
53 | { | ||
54 | char *tmp; | ||
55 | |||
56 | if (NULL != *(gnsrecord_info->rd)) | ||
57 | { | ||
58 | for (int i = 0; i < *(gnsrecord_info->rd_count); i++) | ||
59 | { | ||
60 | tmp = (char*) (*(gnsrecord_info->rd))[i].data; | ||
61 | if (NULL != tmp) | ||
62 | GNUNET_free (tmp); | ||
63 | } | ||
64 | GNUNET_free (*(gnsrecord_info->rd)); | ||
65 | *(gnsrecord_info->rd) = NULL; | ||
66 | } | ||
67 | if (NULL != *(gnsrecord_info->name)) | ||
68 | GNUNET_free (*(gnsrecord_info->name)); | ||
69 | *(gnsrecord_info->name) = NULL; | ||
70 | } | ||
71 | |||
72 | |||
73 | /** | ||
74 | * Parse given JSON object to gns record | ||
75 | * | ||
76 | * @param cls closure, NULL | ||
77 | * @param root the json object representing data | ||
78 | * @param spec where to write the data | ||
79 | * @return #GNUNET_OK upon successful parsing; #GNUNET_SYSERR upon error | ||
80 | */ | ||
81 | static int | ||
82 | parse_record (json_t *data, struct GNUNET_GNSRECORD_Data *rd) | ||
83 | { | ||
84 | struct GNUNET_TIME_Absolute abs_expiration_time; | ||
85 | struct GNUNET_TIME_Relative rel_expiration_time; | ||
86 | const char *value; | ||
87 | const char *record_type; | ||
88 | const char *expiration_time; | ||
89 | int private; | ||
90 | int supplemental; | ||
91 | int rel_exp; | ||
92 | int shadow; | ||
93 | int unpack_state = 0; | ||
94 | |||
95 | // interpret single gns record | ||
96 | unpack_state = json_unpack (data, | ||
97 | "{s:s, s:s, s:s, s:b, s:b, s:b, s:b}", | ||
98 | GNUNET_JSON_GNSRECORD_VALUE, | ||
99 | &value, | ||
100 | GNUNET_JSON_GNSRECORD_TYPE, | ||
101 | &record_type, | ||
102 | GNUNET_JSON_GNSRECORD_EXPIRATION_TIME, | ||
103 | &expiration_time, | ||
104 | GNUNET_JSON_GNSRECORD_FLAG_PRIVATE, | ||
105 | &private, | ||
106 | GNUNET_JSON_GNSRECORD_FLAG_SUPPLEMENTAL, | ||
107 | &supplemental, | ||
108 | GNUNET_JSON_GNSRECORD_FLAG_RELATIVE, | ||
109 | &rel_exp, | ||
110 | GNUNET_JSON_GNSRECORD_FLAG_SHADOW, | ||
111 | &shadow); | ||
112 | if (0 != unpack_state) | ||
113 | { | ||
114 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
115 | "Error gnsdata object has a wrong format!\n"); | ||
116 | return GNUNET_SYSERR; | ||
117 | } | ||
118 | rd->record_type = GNUNET_GNSRECORD_typename_to_number (record_type); | ||
119 | if (UINT32_MAX == rd->record_type) | ||
120 | { | ||
121 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Unsupported type\n"); | ||
122 | return GNUNET_SYSERR; | ||
123 | } | ||
124 | if (GNUNET_OK != GNUNET_GNSRECORD_string_to_value (rd->record_type, | ||
125 | value, | ||
126 | (void **) &rd->data, | ||
127 | &rd->data_size)) | ||
128 | { | ||
129 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Value invalid for record type\n"); | ||
130 | return GNUNET_SYSERR; | ||
131 | } | ||
132 | |||
133 | if (0 == strcmp (expiration_time, GNUNET_JSON_GNSRECORD_NEVER)) | ||
134 | { | ||
135 | rd->expiration_time = GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us; | ||
136 | } | ||
137 | else if ((1 != rel_exp) && | ||
138 | (GNUNET_OK == | ||
139 | GNUNET_STRINGS_fancy_time_to_absolute (expiration_time, | ||
140 | &abs_expiration_time))) | ||
141 | { | ||
142 | rd->expiration_time = abs_expiration_time.abs_value_us; | ||
143 | } | ||
144 | else if (GNUNET_OK == | ||
145 | GNUNET_STRINGS_fancy_time_to_relative (expiration_time, | ||
146 | &rel_expiration_time)) | ||
147 | { | ||
148 | rd->flags |= GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION; | ||
149 | rd->expiration_time = rel_expiration_time.rel_value_us; | ||
150 | } | ||
151 | else | ||
152 | { | ||
153 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Expiration time invalid\n"); | ||
154 | return GNUNET_SYSERR; | ||
155 | } | ||
156 | if (1 == private) | ||
157 | rd->flags |= GNUNET_GNSRECORD_RF_PRIVATE; | ||
158 | if (1 == supplemental) | ||
159 | rd->flags |= GNUNET_GNSRECORD_RF_SUPPLEMENTAL; | ||
160 | if (1 == shadow) | ||
161 | rd->flags |= GNUNET_GNSRECORD_RF_SHADOW_RECORD; | ||
162 | return GNUNET_OK; | ||
163 | } | ||
164 | |||
165 | |||
166 | /** | ||
167 | * Parse given JSON object to gns record | ||
168 | * | ||
169 | * @param cls closure, NULL | ||
170 | * @param root the json object representing data | ||
171 | * @param spec where to write the data | ||
172 | * @return #GNUNET_OK upon successful parsing; #GNUNET_SYSERR upon error | ||
173 | */ | ||
174 | static int | ||
175 | parse_record_data (struct GnsRecordInfo *gnsrecord_info, json_t *data) | ||
176 | { | ||
177 | GNUNET_assert (NULL != data); | ||
178 | if (! json_is_array (data)) | ||
179 | { | ||
180 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
181 | "Error gns record data JSON is not an array!\n"); | ||
182 | return GNUNET_SYSERR; | ||
183 | } | ||
184 | *(gnsrecord_info->rd_count) = json_array_size (data); | ||
185 | *(gnsrecord_info->rd) = GNUNET_malloc (sizeof(struct GNUNET_GNSRECORD_Data) | ||
186 | * json_array_size (data)); | ||
187 | size_t index; | ||
188 | json_t *value; | ||
189 | json_array_foreach (data, index, value) | ||
190 | { | ||
191 | if (GNUNET_OK != parse_record (value, &(*(gnsrecord_info->rd))[index])) | ||
192 | return GNUNET_SYSERR; | ||
193 | } | ||
194 | return GNUNET_OK; | ||
195 | } | ||
196 | |||
197 | |||
198 | static int | ||
199 | parse_gnsrecordobject (void *cls, | ||
200 | json_t *root, | ||
201 | struct GNUNET_JSON_Specification *spec) | ||
202 | { | ||
203 | struct GnsRecordInfo *gnsrecord_info; | ||
204 | int unpack_state = 0; | ||
205 | const char *name; | ||
206 | json_t *data; | ||
207 | |||
208 | GNUNET_assert (NULL != root); | ||
209 | if (! json_is_object (root)) | ||
210 | { | ||
211 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
212 | "Error record JSON is not an object!\n"); | ||
213 | return GNUNET_SYSERR; | ||
214 | } | ||
215 | // interpret single gns record | ||
216 | unpack_state = json_unpack (root, | ||
217 | "{s:s, s:o!}", | ||
218 | GNUNET_JSON_GNSRECORD_RECORD_NAME, | ||
219 | &name, | ||
220 | GNUNET_JSON_GNSRECORD_RECORD_DATA, | ||
221 | &data); | ||
222 | if (0 != unpack_state) | ||
223 | { | ||
224 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
225 | "Error namestore records object has a wrong format!\n"); | ||
226 | return GNUNET_SYSERR; | ||
227 | } | ||
228 | gnsrecord_info = (struct GnsRecordInfo *) spec->ptr; | ||
229 | *(gnsrecord_info->name) = GNUNET_strdup (name); | ||
230 | if (GNUNET_OK != parse_record_data (gnsrecord_info, data)) | ||
231 | { | ||
232 | cleanup_recordinfo (gnsrecord_info); | ||
233 | return GNUNET_SYSERR; | ||
234 | } | ||
235 | return GNUNET_OK; | ||
236 | } | ||
237 | |||
238 | |||
239 | /** | ||
240 | * Cleanup data left from parsing the record. | ||
241 | * | ||
242 | * @param cls closure, NULL | ||
243 | * @param[out] spec where to free the data | ||
244 | */ | ||
245 | static void | ||
246 | clean_gnsrecordobject (void *cls, struct GNUNET_JSON_Specification *spec) | ||
247 | { | ||
248 | struct GnsRecordInfo *gnsrecord_info = (struct GnsRecordInfo *) spec->ptr; | ||
249 | |||
250 | GNUNET_free (gnsrecord_info); | ||
251 | } | ||
252 | |||
253 | |||
254 | /** | ||
255 | * JSON Specification for GNS Records. | ||
256 | * | ||
257 | * @param gnsrecord_object struct of GNUNET_GNSRECORD_Data to fill | ||
258 | * @return JSON Specification | ||
259 | */ | ||
260 | struct GNUNET_JSON_Specification | ||
261 | GNUNET_JSON_spec_gnsrecord (struct GNUNET_GNSRECORD_Data **rd, | ||
262 | unsigned int *rd_count, | ||
263 | char **name) | ||
264 | { | ||
265 | struct GnsRecordInfo *gnsrecord_info = GNUNET_new (struct GnsRecordInfo); | ||
266 | |||
267 | gnsrecord_info->rd = rd; | ||
268 | gnsrecord_info->name = name; | ||
269 | gnsrecord_info->rd_count = rd_count; | ||
270 | struct GNUNET_JSON_Specification ret = { .parser = &parse_gnsrecordobject, | ||
271 | .cleaner = &clean_gnsrecordobject, | ||
272 | .cls = NULL, | ||
273 | .field = NULL, | ||
274 | .ptr = (struct GnsRecordInfo *) | ||
275 | gnsrecord_info, | ||
276 | .ptr_size = 0, | ||
277 | .size_ptr = NULL }; | ||
278 | return ret; | ||
279 | } | ||