aboutsummaryrefslogtreecommitdiff
path: root/src/gns/plugin_rest_gns.c
diff options
context:
space:
mode:
authorMartin Schanzenbach <mschanzenbach@posteo.de>2015-03-11 18:45:23 +0000
committerMartin Schanzenbach <mschanzenbach@posteo.de>2015-03-11 18:45:23 +0000
commit02a113da7aed6b497c13e955403b83c801ee95b4 (patch)
tree2dc6b9af83e2a5480a841908d288f568a45683a2 /src/gns/plugin_rest_gns.c
parent2e229c4fc56bcd2adf119f8e2dcb9db90d555c73 (diff)
downloadgnunet-02a113da7aed6b497c13e955403b83c801ee95b4.tar.gz
gnunet-02a113da7aed6b497c13e955403b83c801ee95b4.zip
-fix
Diffstat (limited to 'src/gns/plugin_rest_gns.c')
-rw-r--r--src/gns/plugin_rest_gns.c96
1 files changed, 54 insertions, 42 deletions
diff --git a/src/gns/plugin_rest_gns.c b/src/gns/plugin_rest_gns.c
index 2dc140b37..c403b4acf 100644
--- a/src/gns/plugin_rest_gns.c
+++ b/src/gns/plugin_rest_gns.c
@@ -205,6 +205,55 @@ do_error (void *cls,
205} 205}
206 206
207/** 207/**
208 * Create json representation of a GNSRECORD
209 *
210 * @param rd the GNSRECORD_Data
211 */
212json_t *
213gnsrecord_to_json (const struct GNUNET_GNSRECORD_Data *rd)
214{
215 const char *typename;
216 char *string_val;
217 const char *exp_str;
218 json_t *record_obj;
219
220 typename = GNUNET_GNSRECORD_number_to_typename (rd->record_type);
221 string_val = GNUNET_GNSRECORD_value_to_string (rd->record_type,
222 rd->data,
223 rd->data_size);
224
225 if (NULL == string_val)
226 {
227 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
228 "Record of type %d malformed, skipping\n",
229 (int) rd->record_type);
230 return NULL;
231 }
232 record_obj = json_object();
233 json_object_set_new (record_obj, "type", json_string (typename));
234 json_object_set_new (record_obj, "value", json_string (string_val));
235 GNUNET_free (string_val);
236
237 if (GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION & rd->flags)
238 {
239 struct GNUNET_TIME_Relative time_rel;
240 time_rel.rel_value_us = rd->expiration_time;
241 exp_str = GNUNET_STRINGS_relative_time_to_string (time_rel, 1);
242 }
243 else
244 {
245 struct GNUNET_TIME_Absolute time_abs;
246 time_abs.abs_value_us = rd->expiration_time;
247 exp_str = GNUNET_STRINGS_absolute_time_to_string (time_abs);
248 }
249 json_object_set_new (record_obj, "expiration_time", json_string (exp_str));
250
251 json_object_set_new (record_obj, "expired",
252 json_boolean (GNUNET_YES == GNUNET_GNSRECORD_is_expired (rd)));
253 return record_obj;
254}
255
256/**
208 * Function called with the result of a GNS lookup. 257 * Function called with the result of a GNS lookup.
209 * 258 *
210 * @param cls the 'const char *' name that was resolved 259 * @param cls the 'const char *' name that was resolved
@@ -217,10 +266,7 @@ process_lookup_result (void *cls, uint32_t rd_count,
217{ 266{
218 struct LookupHandle *handle = cls; 267 struct LookupHandle *handle = cls;
219 uint32_t i; 268 uint32_t i;
220 const char *typename;
221 char *string_val;
222 char *result; 269 char *result;
223 const char *exp_str;
224 json_t *result_root; 270 json_t *result_root;
225 json_t *result_name; 271 json_t *result_name;
226 json_t *result_array; 272 json_t *result_array;
@@ -238,45 +284,10 @@ process_lookup_result (void *cls, uint32_t rd_count,
238 if ( (rd[i].record_type != handle->type) && 284 if ( (rd[i].record_type != handle->type) &&
239 (GNUNET_GNSRECORD_TYPE_ANY != handle->type) ) 285 (GNUNET_GNSRECORD_TYPE_ANY != handle->type) )
240 continue; 286 continue;
241 typename = GNUNET_GNSRECORD_number_to_typename (rd[i].record_type); 287
242 string_val = GNUNET_GNSRECORD_value_to_string (rd[i].record_type, 288 record_obj = gnsrecord_to_json (&(rd[i]));
243 rd[i].data, 289 json_array_append (result_array, record_obj);
244 rd[i].data_size); 290 json_decref (record_obj);
245 if (NULL == string_val)
246 {
247 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
248 "Record %u of type %d malformed, skipping\n",
249 (unsigned int) i,
250 (int) rd[i].record_type);
251 continue;
252 }
253 else
254 {
255 record_obj = json_object();
256 json_object_set_new (record_obj, "type", json_string (typename));
257 json_object_set_new (record_obj, "value", json_string (string_val));
258
259 if (GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION & rd[i].flags)
260 {
261 struct GNUNET_TIME_Relative time_rel;
262 time_rel.rel_value_us = rd[i].expiration_time;
263 exp_str = GNUNET_STRINGS_relative_time_to_string (time_rel, 1);
264 }
265 else
266 {
267 struct GNUNET_TIME_Absolute time_abs;
268 time_abs.abs_value_us = rd[i].expiration_time;
269 exp_str = GNUNET_STRINGS_absolute_time_to_string (time_abs);
270 }
271 json_object_set_new (record_obj, "expiration_time", json_string (exp_str));
272
273 json_object_set_new (record_obj, "expired",
274 json_boolean (GNUNET_YES == GNUNET_GNSRECORD_is_expired (&(rd[i]))));
275
276 json_array_append (result_array, record_obj);
277 json_decref (record_obj);
278 }
279 GNUNET_free (string_val);
280 } 291 }
281 json_object_set (result_root, "query_result", result_array); 292 json_object_set (result_root, "query_result", result_array);
282 json_decref (result_array); 293 json_decref (result_array);
@@ -288,6 +299,7 @@ process_lookup_result (void *cls, uint32_t rd_count,
288 cleanup_handle (handle); 299 cleanup_handle (handle);
289} 300}
290 301
302
291/** 303/**
292 * Perform the actual resolution, starting with the zone 304 * Perform the actual resolution, starting with the zone
293 * identified by the given public key and the shorten zone. 305 * identified by the given public key and the shorten zone.