diff options
author | Phil <phil.buschmann@tum.de> | 2018-07-26 02:31:30 +0200 |
---|---|---|
committer | Phil <phil.buschmann@tum.de> | 2018-07-26 02:31:30 +0200 |
commit | f7ca27a73e69a8c224d65768be3416ff1388c1d7 (patch) | |
tree | 7530a6def74a55885780285162b18b4c2b353380 /src/namestore/plugin_rest_namestore.c | |
parent | cc577a227d6a5ae8ef75e0fa91ef98ced2d2b743 (diff) | |
download | gnunet-f7ca27a73e69a8c224d65768be3416ff1388c1d7.tar.gz gnunet-f7ca27a73e69a8c224d65768be3416ff1388c1d7.zip |
change namestore, json handling; fix identity, gns
Diffstat (limited to 'src/namestore/plugin_rest_namestore.c')
-rw-r--r-- | src/namestore/plugin_rest_namestore.c | 225 |
1 files changed, 75 insertions, 150 deletions
diff --git a/src/namestore/plugin_rest_namestore.c b/src/namestore/plugin_rest_namestore.c index afe010b79..6924c53a4 100644 --- a/src/namestore/plugin_rest_namestore.c +++ b/src/namestore/plugin_rest_namestore.c | |||
@@ -36,11 +36,7 @@ | |||
36 | 36 | ||
37 | #define GNUNET_REST_SUBSYSTEM_NAMESTORE "namestore" | 37 | #define GNUNET_REST_SUBSYSTEM_NAMESTORE "namestore" |
38 | 38 | ||
39 | #define GNUNET_REST_JSON_NAMESTORE_RECORD_TYPE "record_type" | 39 | #define GNUNET_REST_NAMESTORE_ERROR_UNKNOWN "Unknown Error" |
40 | #define GNUNET_REST_JSON_NAMESTORE_VALUE "value" | ||
41 | #define GNUNET_REST_JSON_NAMESTORE_EXPIRATION "expiration" | ||
42 | #define GNUNET_REST_JSON_NAMESTORE_EXPIRED "expired" | ||
43 | #define GNUNET_REST_ERROR_UNKNOWN "Unknown Error" | ||
44 | 40 | ||
45 | #define GNUNET_REST_NAMESTORE_RD_COUNT 1 | 41 | #define GNUNET_REST_NAMESTORE_RD_COUNT 1 |
46 | 42 | ||
@@ -194,8 +190,6 @@ static void | |||
194 | cleanup_handle (void *cls) | 190 | cleanup_handle (void *cls) |
195 | { | 191 | { |
196 | struct RequestHandle *handle = cls; | 192 | struct RequestHandle *handle = cls; |
197 | size_t index; | ||
198 | json_t *json_ego; | ||
199 | 193 | ||
200 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | 194 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, |
201 | "Cleaning up\n"); | 195 | "Cleaning up\n"); |
@@ -239,10 +233,6 @@ cleanup_handle (void *cls) | |||
239 | 233 | ||
240 | if(NULL != handle->resp_object) | 234 | if(NULL != handle->resp_object) |
241 | { | 235 | { |
242 | json_array_foreach(handle->resp_object, index, json_ego ) | ||
243 | { | ||
244 | json_decref (json_ego); | ||
245 | } | ||
246 | json_decref(handle->resp_object); | 236 | json_decref(handle->resp_object); |
247 | } | 237 | } |
248 | 238 | ||
@@ -264,7 +254,7 @@ do_error (void *cls) | |||
264 | char *response; | 254 | char *response; |
265 | 255 | ||
266 | if (NULL == handle->emsg) | 256 | if (NULL == handle->emsg) |
267 | handle->emsg = GNUNET_strdup(GNUNET_REST_ERROR_UNKNOWN); | 257 | handle->emsg = GNUNET_strdup(GNUNET_REST_NAMESTORE_ERROR_UNKNOWN); |
268 | 258 | ||
269 | json_object_set_new(json_error,"error", json_string(handle->emsg)); | 259 | json_object_set_new(json_error,"error", json_string(handle->emsg)); |
270 | 260 | ||
@@ -286,66 +276,10 @@ namestore_iteration_error (void *cls) | |||
286 | { | 276 | { |
287 | struct RequestHandle *handle = cls; | 277 | struct RequestHandle *handle = cls; |
288 | struct MHD_Response *resp = GNUNET_REST_create_response (NULL); | 278 | struct MHD_Response *resp = GNUNET_REST_create_response (NULL); |
289 | handle->response_code = MHD_HTTP_INTERNAL_SERVER_ERROR; | 279 | handle->proc (handle->proc_cls, resp, MHD_HTTP_INTERNAL_SERVER_ERROR); |
290 | handle->proc (handle->proc_cls, resp, handle->response_code); | ||
291 | GNUNET_SCHEDULER_add_now (&cleanup_handle, handle); | 280 | GNUNET_SCHEDULER_add_now (&cleanup_handle, handle); |
292 | } | 281 | } |
293 | 282 | ||
294 | /** | ||
295 | * Create json representation of a GNSRECORD | ||
296 | * | ||
297 | * @param rd the GNSRECORD_Data | ||
298 | */ | ||
299 | static json_t * | ||
300 | gnsrecord_to_json (const struct GNUNET_GNSRECORD_Data *rd) | ||
301 | { | ||
302 | const char *typename; | ||
303 | char *string_val; | ||
304 | const char *exp_str; | ||
305 | json_t *record_obj; | ||
306 | |||
307 | typename = GNUNET_GNSRECORD_number_to_typename (rd->record_type); | ||
308 | string_val = GNUNET_GNSRECORD_value_to_string (rd->record_type, | ||
309 | rd->data, | ||
310 | rd->data_size); | ||
311 | |||
312 | if (NULL == string_val) | ||
313 | { | ||
314 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
315 | "Record of type %d malformed, skipping\n", | ||
316 | (int) rd->record_type); | ||
317 | return NULL; | ||
318 | } | ||
319 | record_obj = json_object(); | ||
320 | json_object_set_new (record_obj, | ||
321 | GNUNET_REST_JSON_NAMESTORE_RECORD_TYPE, | ||
322 | json_string (typename)); | ||
323 | json_object_set_new (record_obj, | ||
324 | GNUNET_REST_JSON_NAMESTORE_VALUE, | ||
325 | json_string (string_val)); | ||
326 | //GNUNET_free (string_val); | ||
327 | |||
328 | if (GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION & rd->flags) | ||
329 | { | ||
330 | struct GNUNET_TIME_Relative time_rel; | ||
331 | time_rel.rel_value_us = rd->expiration_time; | ||
332 | exp_str = GNUNET_STRINGS_relative_time_to_string (time_rel, 1); | ||
333 | } | ||
334 | else | ||
335 | { | ||
336 | struct GNUNET_TIME_Absolute time_abs; | ||
337 | time_abs.abs_value_us = rd->expiration_time; | ||
338 | exp_str = GNUNET_STRINGS_absolute_time_to_string (time_abs); | ||
339 | } | ||
340 | json_object_set_new (record_obj, | ||
341 | GNUNET_REST_JSON_NAMESTORE_EXPIRATION, | ||
342 | json_string (exp_str)); | ||
343 | json_object_set_new (record_obj, "expired", | ||
344 | json_boolean (GNUNET_YES == GNUNET_GNSRECORD_is_expired (rd))); | ||
345 | return record_obj; | ||
346 | } | ||
347 | |||
348 | |||
349 | static void | 283 | static void |
350 | create_finished (void *cls, int32_t success, const char *emsg) | 284 | create_finished (void *cls, int32_t success, const char *emsg) |
351 | { | 285 | { |
@@ -364,6 +298,29 @@ create_finished (void *cls, int32_t success, const char *emsg) | |||
364 | GNUNET_SCHEDULER_add_now (&cleanup_handle, handle); | 298 | GNUNET_SCHEDULER_add_now (&cleanup_handle, handle); |
365 | } | 299 | } |
366 | 300 | ||
301 | static void | ||
302 | del_finished (void *cls, int32_t success, const char *emsg) | ||
303 | { | ||
304 | struct RequestHandle *handle = cls; | ||
305 | |||
306 | handle->add_qe = NULL; | ||
307 | if (GNUNET_NO == success) | ||
308 | { | ||
309 | handle->emsg = GNUNET_strdup("Deleting record failed. Record does not exist"); | ||
310 | GNUNET_SCHEDULER_add_now (&do_error, handle); | ||
311 | return; | ||
312 | } | ||
313 | if (GNUNET_SYSERR == success) | ||
314 | { | ||
315 | handle->emsg = GNUNET_strdup("Deleting record failed"); | ||
316 | GNUNET_SCHEDULER_add_now (&do_error, handle); | ||
317 | return; | ||
318 | } | ||
319 | handle->proc (handle->proc_cls, | ||
320 | GNUNET_REST_create_response (NULL), | ||
321 | MHD_HTTP_NO_CONTENT); | ||
322 | GNUNET_SCHEDULER_add_now (&cleanup_handle, handle); | ||
323 | } | ||
367 | /** | 324 | /** |
368 | * Iteration over all results finished, build final | 325 | * Iteration over all results finished, build final |
369 | * response. | 326 | * response. |
@@ -379,10 +336,8 @@ namestore_list_finished (void *cls) | |||
379 | 336 | ||
380 | handle->list_it = NULL; | 337 | handle->list_it = NULL; |
381 | 338 | ||
382 | GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "HEY\n"); | ||
383 | if (NULL == handle->resp_object) | 339 | if (NULL == handle->resp_object) |
384 | { | 340 | { |
385 | GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "OH\n"); | ||
386 | GNUNET_SCHEDULER_add_now (&do_error, handle); | 341 | GNUNET_SCHEDULER_add_now (&do_error, handle); |
387 | return; | 342 | return; |
388 | } | 343 | } |
@@ -415,9 +370,6 @@ namestore_list_iteration (void *cls, | |||
415 | if (NULL == handle->resp_object) | 370 | if (NULL == handle->resp_object) |
416 | handle->resp_object = json_array(); | 371 | handle->resp_object = json_array(); |
417 | 372 | ||
418 | char *result_str = json_dumps (handle->resp_object, 0); | ||
419 | GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "%s\n", result_str); | ||
420 | GNUNET_free(result_str); | ||
421 | /*if ( (NULL != handle->ego_entry->identifier) && | 373 | /*if ( (NULL != handle->ego_entry->identifier) && |
422 | (0 != strcmp (handle->ego_entry->identifier, | 374 | (0 != strcmp (handle->ego_entry->identifier, |
423 | rname)) ) | 375 | rname)) ) |
@@ -435,13 +387,12 @@ namestore_list_iteration (void *cls, | |||
435 | (0 != strcmp (rname, GNUNET_GNS_EMPTY_LABEL_AT)) ) | 387 | (0 != strcmp (rname, GNUNET_GNS_EMPTY_LABEL_AT)) ) |
436 | continue; | 388 | continue; |
437 | 389 | ||
438 | record_obj = gnsrecord_to_json (&rd[i]); | 390 | record_obj = GNUNET_JSON_from_gns_record(rname,rd); |
439 | 391 | ||
440 | if(NULL == record_obj) | 392 | if(NULL == record_obj) |
441 | continue; | 393 | continue; |
442 | 394 | ||
443 | json_array_append (handle->resp_object, | 395 | json_array_append (handle->resp_object, record_obj); |
444 | record_obj); | ||
445 | json_decref (record_obj); | 396 | json_decref (record_obj); |
446 | } | 397 | } |
447 | 398 | ||
@@ -477,73 +428,7 @@ namestore_get (struct GNUNET_REST_RequestHandle *con_handle, | |||
477 | &namestore_list_finished, | 428 | &namestore_list_finished, |
478 | handle); | 429 | handle); |
479 | } | 430 | } |
480 | /* | ||
481 | 431 | ||
482 | //TODO filter input | ||
483 | static int | ||
484 | json_to_gnsrecord (struct RequestHandle *handle) | ||
485 | { | ||
486 | struct GNUNET_TIME_Relative etime_rel; | ||
487 | struct GNUNET_TIME_Absolute etime_abs; | ||
488 | void *rdata; | ||
489 | size_t rdata_size; | ||
490 | |||
491 | handle->rd = GNUNET_new_array(GNUNET_REST_NAMESTORE_RD_COUNT, | ||
492 | struct GNUNET_GNSRECORD_Data); | ||
493 | memset (handle->rd, 0, sizeof(struct GNUNET_GNSRECORD_Data)); | ||
494 | handle->rd->record_type = GNUNET_GNSRECORD_typename_to_number ( | ||
495 | handle->json_data->type); | ||
496 | if (UINT32_MAX == (*handle->rd).record_type) | ||
497 | { | ||
498 | handle->emsg = GNUNET_strdup("Unsupported type"); | ||
499 | return GNUNET_SYSERR; | ||
500 | } | ||
501 | if (GNUNET_OK | ||
502 | != GNUNET_GNSRECORD_string_to_value ((*handle->rd).record_type, | ||
503 | handle->json_data->value, &rdata, | ||
504 | &rdata_size)) | ||
505 | { | ||
506 | handle->emsg = GNUNET_strdup("Value invalid for record type"); | ||
507 | return GNUNET_SYSERR; | ||
508 | } | ||
509 | (*handle->rd).data = rdata; | ||
510 | (*handle->rd).data_size = rdata_size; | ||
511 | //TODO other flags | ||
512 | if (0 == handle->json_data->is_public) | ||
513 | { | ||
514 | handle->rd->flags |= GNUNET_GNSRECORD_RF_PRIVATE; | ||
515 | } | ||
516 | /**TODO | ||
517 | * if (1 == handle->is_shadow) | ||
518 | rde->flags |= GNUNET_GNSRECORD_RF_SHADOW_RECORD; | ||
519 | if (1 != handle->is_public) | ||
520 | rde->flags |= GNUNET_GNSRECORD_RF_PRIVATE; | ||
521 | * | ||
522 | if (0 == strcmp (handle->json_data->expiration_time, "never")) | ||
523 | { | ||
524 | (*handle->rd).expiration_time = GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us; | ||
525 | } | ||
526 | else if (GNUNET_OK | ||
527 | == GNUNET_STRINGS_fancy_time_to_relative ( | ||
528 | handle->json_data->expiration_time, &etime_rel)) | ||
529 | { | ||
530 | (*handle->rd).expiration_time = etime_rel.rel_value_us; | ||
531 | (*handle->rd).flags |= GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION; | ||
532 | } | ||
533 | else if (GNUNET_OK | ||
534 | == GNUNET_STRINGS_fancy_time_to_absolute ( | ||
535 | handle->json_data->expiration_time, &etime_abs)) | ||
536 | { | ||
537 | (*handle->rd).expiration_time = etime_abs.abs_value_us; | ||
538 | } | ||
539 | else | ||
540 | { | ||
541 | handle->emsg = GNUNET_strdup("Value invalid for record type"); | ||
542 | return GNUNET_SYSERR; | ||
543 | } | ||
544 | return GNUNET_OK; | ||
545 | } | ||
546 | */ | ||
547 | 432 | ||
548 | /** | 433 | /** |
549 | * We're storing a new record; this requires | 434 | * We're storing a new record; this requires |
@@ -564,12 +449,11 @@ create_new_record_cont (void *cls, | |||
564 | { | 449 | { |
565 | struct RequestHandle *handle = cls; | 450 | struct RequestHandle *handle = cls; |
566 | 451 | ||
567 | |||
568 | handle->add_qe = NULL; | 452 | handle->add_qe = NULL; |
569 | if (0 != strcmp (rec_name, handle->label_name)) | 453 | if (0 != strcmp (rec_name, handle->label_name)) |
570 | { | 454 | { |
571 | GNUNET_break (0); | 455 | GNUNET_break (0); |
572 | do_error (handle); | 456 | GNUNET_SCHEDULER_add_now (&do_error, handle); |
573 | return; | 457 | return; |
574 | } | 458 | } |
575 | 459 | ||
@@ -656,6 +540,31 @@ namestore_add (struct GNUNET_REST_RequestHandle *con_handle, | |||
656 | } | 540 | } |
657 | 541 | ||
658 | 542 | ||
543 | static void | ||
544 | del_cont (void *cls, | ||
545 | const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone, | ||
546 | const char *label, | ||
547 | unsigned int rd_count, | ||
548 | const struct GNUNET_GNSRECORD_Data *rd) | ||
549 | { | ||
550 | struct RequestHandle *handle = cls; | ||
551 | |||
552 | handle->add_qe = NULL; | ||
553 | if (0 == rd_count) | ||
554 | { | ||
555 | handle->emsg = GNUNET_strdup("Record not found"); | ||
556 | GNUNET_SCHEDULER_add_now (&do_error, handle); | ||
557 | return; | ||
558 | } | ||
559 | |||
560 | handle->add_qe = GNUNET_NAMESTORE_records_store (handle->ns_handle, | ||
561 | &handle->zone_pkey, | ||
562 | handle->label_name, | ||
563 | 0, NULL, | ||
564 | &del_finished, | ||
565 | handle); | ||
566 | } | ||
567 | |||
659 | /** | 568 | /** |
660 | * Handle namestore DELETE request | 569 | * Handle namestore DELETE request |
661 | * | 570 | * |
@@ -669,12 +578,28 @@ namestore_delete (struct GNUNET_REST_RequestHandle *con_handle, | |||
669 | void *cls) | 578 | void *cls) |
670 | { | 579 | { |
671 | struct RequestHandle *handle = cls; | 580 | struct RequestHandle *handle = cls; |
581 | struct GNUNET_HashCode key; | ||
582 | |||
583 | GNUNET_CRYPTO_hash ("label", strlen ("label"), &key); | ||
584 | if ( GNUNET_NO | ||
585 | == GNUNET_CONTAINER_multihashmap_contains (con_handle->url_param_map, | ||
586 | &key)) | ||
587 | { | ||
588 | handle->emsg = GNUNET_strdup("Missing name"); | ||
589 | GNUNET_SCHEDULER_add_now (&do_error, handle); | ||
590 | return; | ||
591 | } | ||
592 | handle->label_name = GNUNET_CONTAINER_multihashmap_get (con_handle->url_param_map, | ||
593 | &key); | ||
672 | 594 | ||
673 | //TODO add behaviour and response | 595 | handle->add_qe = GNUNET_NAMESTORE_records_lookup (handle->ns_handle, |
674 | 596 | &handle->zone_pkey, | |
675 | handle->emsg = GNUNET_strdup ("Not implemented yet"); | 597 | handle->label_name, |
676 | GNUNET_SCHEDULER_add_now (&do_error, handle); | 598 | &do_error, |
677 | return; | 599 | handle, |
600 | &del_cont, | ||
601 | handle); | ||
602 | |||
678 | } | 603 | } |
679 | 604 | ||
680 | 605 | ||