diff options
Diffstat (limited to 'src/gnsrecord')
-rw-r--r-- | src/gnsrecord/Makefile.am | 1 | ||||
-rw-r--r-- | src/gnsrecord/gnsrecord.c | 127 |
2 files changed, 128 insertions, 0 deletions
diff --git a/src/gnsrecord/Makefile.am b/src/gnsrecord/Makefile.am index f840a31a4..47d83169f 100644 --- a/src/gnsrecord/Makefile.am +++ b/src/gnsrecord/Makefile.am | |||
@@ -39,6 +39,7 @@ libgnunetgnsrecord_la_SOURCES = \ | |||
39 | gnsrecord_misc.c | 39 | gnsrecord_misc.c |
40 | libgnunetgnsrecord_la_LIBADD = \ | 40 | libgnunetgnsrecord_la_LIBADD = \ |
41 | $(top_builddir)/src/util/libgnunetutil.la \ | 41 | $(top_builddir)/src/util/libgnunetutil.la \ |
42 | -ljansson \ | ||
42 | $(GN_LIBINTL) | 43 | $(GN_LIBINTL) |
43 | libgnunetgnsrecord_la_LDFLAGS = \ | 44 | libgnunetgnsrecord_la_LDFLAGS = \ |
44 | $(GN_LIB_LDFLAGS) $(WINFLAGS) \ | 45 | $(GN_LIB_LDFLAGS) $(WINFLAGS) \ |
diff --git a/src/gnsrecord/gnsrecord.c b/src/gnsrecord/gnsrecord.c index da34846e7..30a0ffa83 100644 --- a/src/gnsrecord/gnsrecord.c +++ b/src/gnsrecord/gnsrecord.c | |||
@@ -28,7 +28,9 @@ | |||
28 | #include "gnunet_constants.h" | 28 | #include "gnunet_constants.h" |
29 | #include "gnunet_gnsrecord_lib.h" | 29 | #include "gnunet_gnsrecord_lib.h" |
30 | #include "gnunet_gnsrecord_plugin.h" | 30 | #include "gnunet_gnsrecord_plugin.h" |
31 | #include "gnunet_json_lib.h" | ||
31 | #include "gnunet_tun_lib.h" | 32 | #include "gnunet_tun_lib.h" |
33 | #include <jansson.h> | ||
32 | 34 | ||
33 | 35 | ||
34 | #define LOG(kind,...) GNUNET_log_from (kind, "gnsrecord",__VA_ARGS__) | 36 | #define LOG(kind,...) GNUNET_log_from (kind, "gnsrecord",__VA_ARGS__) |
@@ -245,5 +247,130 @@ GNUNET_GNSRECORD_number_to_typename (uint32_t type) | |||
245 | return NULL; | 247 | return NULL; |
246 | } | 248 | } |
247 | 249 | ||
250 | /** | ||
251 | * Parse given JSON object to gns record | ||
252 | * | ||
253 | * @param cls closure, NULL | ||
254 | * @param root the json object representing data | ||
255 | * @param spec where to write the data | ||
256 | * @return #GNUNET_OK upon successful parsing; #GNUNET_SYSERR upon error | ||
257 | */ | ||
258 | static int | ||
259 | parse_gnsrecordobject (void *cls, | ||
260 | json_t *root, | ||
261 | struct GNUNET_JSON_Specification *spec) | ||
262 | { | ||
263 | struct GNUNET_GNSRECORD_Data *gnsrecord_object; | ||
264 | struct GNUNET_TIME_Absolute abs_expiration_time; | ||
265 | int unpack_state=0; | ||
266 | const char *data; | ||
267 | const char *expiration_date; | ||
268 | const char *record_type; | ||
269 | const char *dummy_value; | ||
270 | int flag; | ||
271 | void *rdata; | ||
272 | size_t rdata_size; | ||
273 | |||
274 | if(!json_is_object(root)) | ||
275 | { | ||
276 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
277 | "Error json is not array nor object!\n"); | ||
278 | return GNUNET_SYSERR; | ||
279 | } | ||
280 | //interpret single gns record | ||
281 | unpack_state = json_unpack(root, | ||
282 | "{s:s, s:s, s:s, s?:i, s:s!}", | ||
283 | "value", &data, | ||
284 | "type", &record_type, | ||
285 | "expiration_time", &expiration_date, | ||
286 | "flag", &flag, | ||
287 | "label", &dummy_value); | ||
288 | GNUNET_log(GNUNET_ERROR_TYPE_ERROR, | ||
289 | "{value:%s, type:%s, expire:%s, flag:%i}", | ||
290 | data, | ||
291 | record_type, | ||
292 | expiration_date, | ||
293 | flag); | ||
294 | if (GNUNET_SYSERR == unpack_state) | ||
295 | { | ||
296 | GNUNET_log(GNUNET_ERROR_TYPE_ERROR, | ||
297 | "Error json object has a wrong format!\n"); | ||
298 | return GNUNET_SYSERR; | ||
299 | } | ||
300 | //TODO test | ||
301 | gnsrecord_object = GNUNET_new (struct GNUNET_GNSRECORD_Data); | ||
302 | gnsrecord_object->record_type = GNUNET_GNSRECORD_typename_to_number(record_type); | ||
303 | if (GNUNET_OK | ||
304 | != GNUNET_GNSRECORD_string_to_value (gnsrecord_object->record_type, | ||
305 | data, &rdata, | ||
306 | &rdata_size)) | ||
307 | { | ||
308 | GNUNET_log(GNUNET_ERROR_TYPE_ERROR,"Value invalid for record type"); | ||
309 | return GNUNET_SYSERR; | ||
310 | } | ||
311 | gnsrecord_object->data = rdata; | ||
312 | gnsrecord_object->data_size = rdata_size; | ||
313 | |||
314 | if (0 == strcmp (expiration_date, "never")) | ||
315 | { | ||
316 | gnsrecord_object->expiration_time = GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us; | ||
317 | } | ||
318 | else if (GNUNET_OK | ||
319 | == GNUNET_STRINGS_fancy_time_to_absolute (expiration_date, | ||
320 | &abs_expiration_time)) | ||
321 | { | ||
322 | gnsrecord_object->expiration_time = abs_expiration_time.abs_value_us; | ||
323 | } | ||
324 | else | ||
325 | { | ||
326 | GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Value invalid for record type"); | ||
327 | return GNUNET_SYSERR; | ||
328 | } | ||
329 | gnsrecord_object->flags = (enum GNUNET_GNSRECORD_Flags)flag; | ||
330 | *(struct GNUNET_GNSRECORD_Data **) spec->ptr = gnsrecord_object; | ||
331 | return GNUNET_OK; | ||
332 | } | ||
333 | |||
334 | /** | ||
335 | * Cleanup data left from parsing RSA public key. | ||
336 | * | ||
337 | * @param cls closure, NULL | ||
338 | * @param[out] spec where to free the data | ||
339 | */ | ||
340 | static void | ||
341 | clean_gnsrecordobject (void *cls, struct GNUNET_JSON_Specification *spec) | ||
342 | { | ||
343 | struct GNUNET_GNSRECORD_Data **gnsrecord_object; | ||
344 | gnsrecord_object = (struct GNUNET_GNSRECORD_Data **) spec->ptr; | ||
345 | if (NULL != *gnsrecord_object) | ||
346 | { | ||
347 | if (NULL != (*gnsrecord_object)->data) | ||
348 | GNUNET_free((char*)(*gnsrecord_object)->data); | ||
349 | GNUNET_free(*gnsrecord_object); | ||
350 | *gnsrecord_object = NULL; | ||
351 | } | ||
352 | } | ||
353 | |||
354 | /** | ||
355 | * JSON Specification for GNS Records. | ||
356 | * | ||
357 | * @param gnsrecord_object struct of GNUNET_GNSRECORD_Data to fill | ||
358 | * @return JSON Specification | ||
359 | */ | ||
360 | struct GNUNET_JSON_Specification | ||
361 | GNUNET_JSON_spec_gnsrecord_data (struct GNUNET_GNSRECORD_Data **gnsrecord_object) | ||
362 | { | ||
363 | struct GNUNET_JSON_Specification ret = { | ||
364 | .parser = &parse_gnsrecordobject, | ||
365 | .cleaner = &clean_gnsrecordobject, | ||
366 | .cls = NULL, | ||
367 | .field = NULL, | ||
368 | .ptr = gnsrecord_object, | ||
369 | .ptr_size = 0, | ||
370 | .size_ptr = NULL | ||
371 | }; | ||
372 | *gnsrecord_object = NULL; | ||
373 | return ret; | ||
374 | } | ||
248 | 375 | ||
249 | /* end of gnsrecord.c */ | 376 | /* end of gnsrecord.c */ |