From fbdc2fbb450684e4c98cd67574182481dcbd286b Mon Sep 17 00:00:00 2001 From: "Schanzenbach, Martin" Date: Sat, 9 May 2020 19:12:51 +0200 Subject: [PATCH] change label processing in namestore REST api for gns records properly fix bugs in json gnsrecord parser --- src/json/json_generator.c | 27 +++++++++++++++++------- src/json/json_gnsrecord.c | 44 +++++++++++++++++++++++++++++---------- 2 files changed, 53 insertions(+), 18 deletions(-) diff --git a/src/json/json_generator.c b/src/json/json_generator.c index dd875871e..594fcaf27 100644 --- a/src/json/json_generator.c +++ b/src/json/json_generator.c @@ -213,7 +213,8 @@ GNUNET_JSON_from_gnsrecord (const char*rname, const struct GNUNET_GNSRECORD_Data *rd, unsigned int rd_count) { - struct GNUNET_TIME_Absolute expiration_time; + struct GNUNET_TIME_Absolute abs_exp; + struct GNUNET_TIME_Relative rel_exp; const char *expiration_time_str; const char *record_type_str; char *value_str; @@ -248,22 +249,34 @@ GNUNET_JSON_from_gnsrecord (const char*rname, value_str = GNUNET_GNSRECORD_value_to_string (rd[i].record_type, rd[i].data, rd[i].data_size); - expiration_time = GNUNET_GNSRECORD_record_get_expiration_time (1, &rd[i]); - expiration_time_str = GNUNET_STRINGS_absolute_time_to_string ( - expiration_time); + if (GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION & rd[i].flags) + { + rel_exp.rel_value_us = rd[i].expiration_time; + expiration_time_str = GNUNET_STRINGS_relative_time_to_string (rel_exp, + GNUNET_NO); + } else { + abs_exp.abs_value_us = rd[i].expiration_time; + expiration_time_str = GNUNET_STRINGS_absolute_time_to_string (abs_exp); + } record_type_str = GNUNET_GNSRECORD_number_to_typename (rd[i].record_type); GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Packing %s %s %s %d\n", value_str, record_type_str, expiration_time_str, rd[i].flags); - record = json_pack ("{s:s,s:s,s:s,s:i}", + record = json_pack ("{s:s,s:s,s:s,s:b,s:b,s:b,s:b}", "value", value_str, "record_type", record_type_str, "expiration_time", expiration_time_str, - "flag", - rd[i].flags); + "private", + rd[i].flags & GNUNET_GNSRECORD_RF_PRIVATE, + "relative_expiration", + rd[i].flags & GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION, + "supplemental", + rd[i].flags & GNUNET_GNSRECORD_RF_SUPPLEMENTAL, + "shadow", + rd[i].flags & GNUNET_GNSRECORD_RF_SHADOW_RECORD); GNUNET_free (value_str); if (NULL == record) { diff --git a/src/json/json_gnsrecord.c b/src/json/json_gnsrecord.c index 37f0c03da..fe5858f06 100644 --- a/src/json/json_gnsrecord.c +++ b/src/json/json_gnsrecord.c @@ -31,7 +31,10 @@ #define GNUNET_JSON_GNSRECORD_RECORD_DATA "data" #define GNUNET_JSON_GNSRECORD_TYPE "record_type" #define GNUNET_JSON_GNSRECORD_EXPIRATION_TIME "expiration_time" -#define GNUNET_JSON_GNSRECORD_FLAG "flag" +#define GNUNET_JSON_GNSRECORD_FLAG_PRIVATE "private" +#define GNUNET_JSON_GNSRECORD_FLAG_SUPPLEMENTAL "supplemental" +#define GNUNET_JSON_GNSRECORD_FLAG_RELATIVE "relative_expiration" +#define GNUNET_JSON_GNSRECORD_FLAG_SHADOW "shadow" #define GNUNET_JSON_GNSRECORD_RECORD_NAME "record_name" #define GNUNET_JSON_GNSRECORD_NEVER "never" @@ -48,12 +51,15 @@ struct GnsRecordInfo static void cleanup_recordinfo (struct GnsRecordInfo *gnsrecord_info) { + char *tmp; + if (NULL != *(gnsrecord_info->rd)) { - for (unsigned int i = 0; i < *(gnsrecord_info->rd_count); i++) + for (int i = 0; i < *(gnsrecord_info->rd_count); i++) { - if (NULL != (*(gnsrecord_info->rd))[i].data) - GNUNET_free_nz ((char *) (*(gnsrecord_info->rd))[i].data); + tmp = (char*) (*(gnsrecord_info->rd))[i].data; + if (NULL != tmp) + GNUNET_free (tmp); } GNUNET_free (*(gnsrecord_info->rd)); *(gnsrecord_info->rd) = NULL; @@ -80,20 +86,29 @@ parse_record (json_t *data, struct GNUNET_GNSRECORD_Data *rd) const char *value; const char *record_type; const char *expiration_time; - int flag; + int private; + int supplemental; + int rel_exp; + int shadow; int unpack_state = 0; // interpret single gns record unpack_state = json_unpack (data, - "{s:s, s:s, s:s, s?:i!}", + "{s:s, s:s, s:s, s:b, s:b, s:b, s:b}", GNUNET_JSON_GNSRECORD_VALUE, &value, GNUNET_JSON_GNSRECORD_TYPE, &record_type, GNUNET_JSON_GNSRECORD_EXPIRATION_TIME, &expiration_time, - GNUNET_JSON_GNSRECORD_FLAG, - &flag); + GNUNET_JSON_GNSRECORD_FLAG_PRIVATE, + &private, + GNUNET_JSON_GNSRECORD_FLAG_SUPPLEMENTAL, + &supplemental, + GNUNET_JSON_GNSRECORD_FLAG_RELATIVE, + &rel_exp, + GNUNET_JSON_GNSRECORD_FLAG_SHADOW, + &shadow); if (0 != unpack_state) { GNUNET_log (GNUNET_ERROR_TYPE_ERROR, @@ -119,9 +134,10 @@ parse_record (json_t *data, struct GNUNET_GNSRECORD_Data *rd) { rd->expiration_time = GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us; } - else if (GNUNET_OK == + else if ((1 != rel_exp) && + (GNUNET_OK == GNUNET_STRINGS_fancy_time_to_absolute (expiration_time, - &abs_expiration_time)) + &abs_expiration_time))) { rd->expiration_time = abs_expiration_time.abs_value_us; } @@ -129,6 +145,7 @@ parse_record (json_t *data, struct GNUNET_GNSRECORD_Data *rd) GNUNET_STRINGS_fancy_time_to_relative (expiration_time, &rel_expiration_time)) { + rd->flags |= GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION; rd->expiration_time = rel_expiration_time.rel_value_us; } else @@ -136,7 +153,12 @@ parse_record (json_t *data, struct GNUNET_GNSRECORD_Data *rd) GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Expiration time invalid\n"); return GNUNET_SYSERR; } - rd->flags = (enum GNUNET_GNSRECORD_Flags) flag; + if (1 == private) + rd->flags |= GNUNET_GNSRECORD_RF_PRIVATE; + if (1 == supplemental) + rd->flags |= GNUNET_GNSRECORD_RF_SUPPLEMENTAL; + if (1 == shadow) + rd->flags |= GNUNET_GNSRECORD_RF_SHADOW_RECORD; return GNUNET_OK; } -- 2.25.1