-static int
-json_to_gnsrecord (const json_t *records_json,
- struct GNUNET_GNSRECORD_Data **rd,
- unsigned int *rd_count)
-{
- struct GNUNET_TIME_Relative etime_rel;
- struct GNUNET_TIME_Absolute etime_abs;
- char *value;
- void *rdata;
- size_t rdata_size;
- const char *typestring;
- const char *expirationstring;
- int i;
- json_t *type_json;
- json_t *value_json;
- json_t *record_json;
- json_t *exp_json;
-
- *rd_count = json_array_size (records_json);
- *rd = GNUNET_malloc (sizeof (struct GNUNET_GNSRECORD_Data) * *rd_count);
- for (i = 0; i < *rd_count; i++)
- {
- memset (&((*rd)[i]), 0, sizeof (struct GNUNET_GNSRECORD_Data));
- record_json = json_array_get (records_json, i);
- type_json = json_object_get (record_json,
- GNUNET_REST_JSONAPI_NAMESTORE_RECORD_TYPE);
- if (!json_is_string (type_json))
- {
- GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
- "Type property is no string\n");
- return GNUNET_SYSERR;
- }
- typestring = json_string_value (type_json);
- (*rd)[i].record_type = GNUNET_GNSRECORD_typename_to_number (typestring);
- if (UINT32_MAX == (*rd)[i].record_type)
- {
- GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Unsupported type `%s'\n"),
- json_string_value (type_json));
- return GNUNET_SYSERR;
- }
- value_json = json_object_get (record_json,
- GNUNET_REST_JSONAPI_NAMESTORE_VALUE);
- if (!json_is_string (value_json))
- {
- GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
- "Value property is no string\n");
- return GNUNET_SYSERR;
- }
- value = GNUNET_strdup (json_string_value (value_json));
- if (GNUNET_OK != GNUNET_GNSRECORD_string_to_value ((*rd)[i].record_type,
- value,
- &rdata,
- &rdata_size))
- {
- GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Value `%s' invalid for record type `%s'\n"),
- value, typestring);
- return GNUNET_SYSERR;
- }
- (*rd)[i].data = rdata;
- (*rd)[i].data_size = rdata_size;
- /**TODO
- * if (1 == handle->is_shadow)
- rde->flags |= GNUNET_GNSRECORD_RF_SHADOW_RECORD;
- if (1 != handle->is_public)
- rde->flags |= GNUNET_GNSRECORD_RF_PRIVATE;
- */
- exp_json = json_object_get (record_json,
- GNUNET_REST_JSONAPI_NAMESTORE_EXPIRATION);
- if (!json_is_string (exp_json))
- {
- GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
- "Expiration property is no string\n");
- return GNUNET_SYSERR;
- }
- expirationstring = json_string_value (exp_json);
- if (0 == strcmp (expirationstring, "never"))
- {
- (*rd)[i].expiration_time = GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us;
- }
- else if (GNUNET_OK ==
- GNUNET_STRINGS_fancy_time_to_relative (expirationstring,
- &etime_rel))
- {
- (*rd)[i].expiration_time = etime_rel.rel_value_us;
- (*rd)[i].flags |= GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION;
- }
- else if (GNUNET_OK ==
- GNUNET_STRINGS_fancy_time_to_absolute (expirationstring,
- &etime_abs))
- {
- (*rd)[i].expiration_time = etime_abs.abs_value_us;
- }
- else
- {
- GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Value `%s' invalid for record type `%s'\n"),
- value, typestring);
- return GNUNET_SYSERR;
- }
- }
- return GNUNET_OK;
-}