aboutsummaryrefslogtreecommitdiff
path: root/src/gns/plugin_rest_gns.c
diff options
context:
space:
mode:
authorPhil <phil.buschmann@tum.de>2018-06-10 19:02:07 +0200
committerPhil <phil.buschmann@tum.de>2018-06-10 19:02:07 +0200
commitc1062075eddca49cfeaac8f7fd8c84e73198c2aa (patch)
tree8df0d081b7c25b43551cff19de191e697898a692 /src/gns/plugin_rest_gns.c
parentf5aae6fb1455b197c54073d0be807b460268da6d (diff)
downloadgnunet-c1062075eddca49cfeaac8f7fd8c84e73198c2aa.tar.gz
gnunet-c1062075eddca49cfeaac8f7fd8c84e73198c2aa.zip
-wip error gns
Diffstat (limited to 'src/gns/plugin_rest_gns.c')
-rw-r--r--src/gns/plugin_rest_gns.c188
1 files changed, 107 insertions, 81 deletions
diff --git a/src/gns/plugin_rest_gns.c b/src/gns/plugin_rest_gns.c
index 1d215b6a4..eeb74887e 100644
--- a/src/gns/plugin_rest_gns.c
+++ b/src/gns/plugin_rest_gns.c
@@ -19,6 +19,7 @@
19 */ 19 */
20/** 20/**
21 * @author Martin Schanzenbach 21 * @author Martin Schanzenbach
22 * @author Philippe Buschmann
22 * @file gns/plugin_rest_gns.c 23 * @file gns/plugin_rest_gns.c
23 * @brief GNUnet GNS REST plugin 24 * @brief GNUnet GNS REST plugin
24 * 25 *
@@ -40,6 +41,8 @@
40 41
41#define GNUNET_REST_JSONAPI_GNS_RECORD_TYPE "record_type" 42#define GNUNET_REST_JSONAPI_GNS_RECORD_TYPE "record_type"
42 43
44#define GNUNET_REST_PARAMETER_GNS_NAME "name"
45
43#define GNUNET_REST_JSONAPI_GNS_TYPEINFO "gns_name" 46#define GNUNET_REST_JSONAPI_GNS_TYPEINFO "gns_name"
44 47
45#define GNUNET_REST_JSONAPI_GNS_RECORD "records" 48#define GNUNET_REST_JSONAPI_GNS_RECORD "records"
@@ -164,6 +167,11 @@ struct LookupHandle
164 */ 167 */
165 int response_code; 168 int response_code;
166 169
170 /**
171 * HTTP response code
172 */
173 char* emsg;
174
167}; 175};
168 176
169 177
@@ -182,6 +190,8 @@ cleanup_handle (struct LookupHandle *handle)
182 190
183 if (NULL != handle->name) 191 if (NULL != handle->name)
184 GNUNET_free (handle->name); 192 GNUNET_free (handle->name);
193 if (NULL != handle->emsg)
194 GNUNET_free (handle->emsg);
185 if (NULL != handle->el) 195 if (NULL != handle->el)
186 { 196 {
187 GNUNET_IDENTITY_ego_lookup_cancel (handle->el); 197 GNUNET_IDENTITY_ego_lookup_cancel (handle->el);
@@ -227,10 +237,18 @@ do_error (void *cls)
227{ 237{
228 struct LookupHandle *handle = cls; 238 struct LookupHandle *handle = cls;
229 struct MHD_Response *resp; 239 struct MHD_Response *resp;
240 char *json_error;
230 241
231 resp = GNUNET_REST_create_response (NULL); 242 if (NULL == handle->emsg)
243 handle->emsg = GNUNET_strdup("Unknown Error");
244
245 GNUNET_asprintf (&json_error, "{\"error\": \"%s\"}", handle->emsg);
246 handle->response_code = MHD_HTTP_OK;
247
248 resp = GNUNET_REST_create_response (json_error);
232 handle->proc (handle->proc_cls, resp, handle->response_code); 249 handle->proc (handle->proc_cls, resp, handle->response_code);
233 cleanup_handle (handle); 250 cleanup_handle (handle);
251 GNUNET_free(json_error);
234} 252}
235 253
236 254
@@ -296,16 +314,12 @@ process_lookup_result (void *cls, uint32_t rd_count,
296{ 314{
297 struct LookupHandle *handle = cls; 315 struct LookupHandle *handle = cls;
298 struct MHD_Response *resp; 316 struct MHD_Response *resp;
299 struct GNUNET_JSONAPI_Document *json_document;
300 struct GNUNET_JSONAPI_Resource *json_resource;
301 uint32_t i; 317 uint32_t i;
302 char *result; 318 char *result;
303 json_t *result_array; 319 json_t *result_array;
304 json_t *record_obj; 320 json_t *record_obj;
305 321
306 result_array = json_array(); 322 result_array = json_array();
307 json_document = GNUNET_JSONAPI_document_new ();
308 json_resource = GNUNET_JSONAPI_resource_new (GNUNET_REST_JSONAPI_GNS_TYPEINFO, handle->name);
309 handle->lookup_request = NULL; 323 handle->lookup_request = NULL;
310 for (i=0; i<rd_count; i++) 324 for (i=0; i<rd_count; i++)
311 { 325 {
@@ -316,17 +330,12 @@ process_lookup_result (void *cls, uint32_t rd_count,
316 json_array_append (result_array, record_obj); 330 json_array_append (result_array, record_obj);
317 json_decref (record_obj); 331 json_decref (record_obj);
318 } 332 }
319 GNUNET_JSONAPI_resource_add_attr (json_resource, 333 result = json_dumps(result_array, 0);
320 GNUNET_REST_JSONAPI_GNS_RECORD,
321 result_array);
322 GNUNET_JSONAPI_document_resource_add (json_document, json_resource);
323 GNUNET_JSONAPI_document_serialize (json_document, &result);
324 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Result %s\n", result); 334 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Result %s\n", result);
325 json_decref (result_array);
326 GNUNET_JSONAPI_document_delete (json_document);
327 resp = GNUNET_REST_create_response (result); 335 resp = GNUNET_REST_create_response (result);
328 handle->proc (handle->proc_cls, resp, MHD_HTTP_OK); 336 handle->proc (handle->proc_cls, resp, MHD_HTTP_OK);
329 GNUNET_free (result); 337 GNUNET_free (result);
338 json_decref (result_array);
330 cleanup_handle (handle); 339 cleanup_handle (handle);
331} 340}
332 341
@@ -358,6 +367,7 @@ lookup_with_public_key (struct LookupHandle *handle)
358 } 367 }
359 else 368 else
360 { 369 {
370 handle->emsg = GNUNET_strdup("Parameter name is missing");
361 GNUNET_SCHEDULER_add_now (&do_error, handle); 371 GNUNET_SCHEDULER_add_now (&do_error, handle);
362 return; 372 return;
363 } 373 }
@@ -382,6 +392,7 @@ identity_zone_cb (void *cls,
382 { 392 {
383 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 393 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
384 _("Ego for not found, cannot perform lookup.\n")); 394 _("Ego for not found, cannot perform lookup.\n"));
395 handle->emsg = GNUNET_strdup ("Ego for not found, cannot perform lookup.");
385 GNUNET_SCHEDULER_add_now (&do_error, handle); 396 GNUNET_SCHEDULER_add_now (&do_error, handle);
386 return; 397 return;
387 } 398 }
@@ -420,6 +431,7 @@ identity_master_cb (void *cls,
420 { 431 {
421 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 432 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
422 _("Ego for `gns-master' not found, cannot perform lookup. Did you run gnunet-gns-import.sh?\n")); 433 _("Ego for `gns-master' not found, cannot perform lookup. Did you run gnunet-gns-import.sh?\n"));
434 handle->emsg = GNUNET_strdup("Ego for `gns-master' not found, cannot perform lookup. Did you run gnunet-gns-import.sh?");
423 GNUNET_SCHEDULER_add_now (&do_error, handle); 435 GNUNET_SCHEDULER_add_now (&do_error, handle);
424 return; 436 return;
425 } 437 }
@@ -438,35 +450,10 @@ identity_master_cb (void *cls,
438} 450}
439 451
440/** 452/**
441 * Parse REST uri for name and record type 453 * Handle get request
442 * 454 *
443 * @param url Url to parse 455 * @param handle the lookup handle
444 * @param handle lookup handle to populate
445 * @return GNUNET_SYSERR on error
446 */ 456 */
447static int
448parse_url (const char *url, struct LookupHandle *handle)
449{
450 char *name;
451 char tmp_url[strlen(url)+1];
452 char *tok;
453
454 strcpy (tmp_url, url);
455 tok = strtok ((char*)tmp_url, "/");
456 if (NULL == tok)
457 return GNUNET_SYSERR;
458 name = strtok (NULL, "/");
459 if (NULL == name)
460 return GNUNET_SYSERR;
461 GNUNET_asprintf (&handle->name,
462 "%s",
463 name);
464 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
465 "Got name: %s\n", handle->name);
466 return GNUNET_OK;
467}
468
469
470static void 457static void
471get_gns_cont (struct GNUNET_REST_RequestHandle *conndata_handle, 458get_gns_cont (struct GNUNET_REST_RequestHandle *conndata_handle,
472 const char* url, 459 const char* url,
@@ -474,40 +461,48 @@ get_gns_cont (struct GNUNET_REST_RequestHandle *conndata_handle,
474{ 461{
475 struct LookupHandle *handle = cls; 462 struct LookupHandle *handle = cls;
476 struct GNUNET_HashCode key; 463 struct GNUNET_HashCode key;
464 long int enum_test;
465 char *temp_val;
477 466
478 //parse name and type from url 467 //check for /gns otherwise 404
479 if (GNUNET_OK != parse_url (url, handle)) 468 if (strlen (GNUNET_REST_API_NS_GNS) > strlen (url))
480 { 469 {
481 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Error parsing url...\n"); 470 handle->emsg = GNUNET_strdup("Wrong URL");
482 GNUNET_SCHEDULER_add_now (&do_error, handle); 471 GNUNET_SCHEDULER_add_now (&do_error, handle);
483 return; 472 return;
484 } 473 }
485 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 474
486 "Connecting...\n"); 475 //connect to gns
476 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Connecting...\n");
487 handle->gns = GNUNET_GNS_connect (cfg); 477 handle->gns = GNUNET_GNS_connect (cfg);
488 handle->identity = GNUNET_IDENTITY_connect (cfg, NULL, NULL); 478 handle->identity = GNUNET_IDENTITY_connect (cfg, NULL, NULL);
489 handle->timeout_task = GNUNET_SCHEDULER_add_delayed (handle->timeout, 479 handle->timeout_task = GNUNET_SCHEDULER_add_delayed (handle->timeout,
490 &do_error, handle); 480 &do_error, handle);
491 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 481 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Connected\n");
492 "Connected\n");
493 if (NULL == handle->gns) 482 if (NULL == handle->gns)
494 { 483 {
495 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 484 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Connecting to GNS failed\n");
496 "Connecting to GNS failed\n"); 485 handle->emsg = GNUNET_strdup("Connecting to GNS failed");
497 GNUNET_SCHEDULER_add_now (&do_error, handle); 486 GNUNET_SCHEDULER_add_now (&do_error, handle);
498 return; 487 return;
499 } 488 }
500 GNUNET_CRYPTO_hash (GNUNET_REST_JSONAPI_GNS_OPTIONS, 489
501 strlen (GNUNET_REST_JSONAPI_GNS_OPTIONS), 490 //check parameter name -> BAD_REQUEST
502 &key); 491 GNUNET_CRYPTO_hash (GNUNET_REST_PARAMETER_GNS_NAME,
503 handle->options = GNUNET_GNS_LO_DEFAULT; 492 strlen (GNUNET_REST_PARAMETER_GNS_NAME),
504 if ( GNUNET_YES == 493 &key);
505 GNUNET_CONTAINER_multihashmap_contains (conndata_handle->url_param_map, 494 if ( GNUNET_NO
506 &key) ) 495 == GNUNET_CONTAINER_multihashmap_contains (conndata_handle->url_param_map,
496 &key))
507 { 497 {
508 handle->options = GNUNET_GNS_LO_DEFAULT;//TODO(char*) GNUNET_CONTAINER_multihashmap_get (conndata_handle->url_param_map, 498 handle->emsg = GNUNET_strdup("Parameter name is missing");
509 //&key); 499 GNUNET_SCHEDULER_add_now (&do_error, handle);
500 return;
510 } 501 }
502 handle->name = GNUNET_strdup(GNUNET_CONTAINER_multihashmap_get (conndata_handle->url_param_map,
503 &key));
504
505 //check parameter record_type, optional
511 GNUNET_CRYPTO_hash (GNUNET_REST_JSONAPI_GNS_RECORD_TYPE, 506 GNUNET_CRYPTO_hash (GNUNET_REST_JSONAPI_GNS_RECORD_TYPE,
512 strlen (GNUNET_REST_JSONAPI_GNS_RECORD_TYPE), 507 strlen (GNUNET_REST_JSONAPI_GNS_RECORD_TYPE),
513 &key); 508 &key);
@@ -515,34 +510,66 @@ get_gns_cont (struct GNUNET_REST_RequestHandle *conndata_handle,
515 GNUNET_CONTAINER_multihashmap_contains (conndata_handle->url_param_map, 510 GNUNET_CONTAINER_multihashmap_contains (conndata_handle->url_param_map,
516 &key) ) 511 &key) )
517 { 512 {
518 handle->type = GNUNET_GNSRECORD_typename_to_number 513 handle->type = GNUNET_GNSRECORD_typename_to_number(
519 (GNUNET_CONTAINER_multihashmap_get (conndata_handle->url_param_map, 514 GNUNET_CONTAINER_multihashmap_get (conndata_handle->url_param_map,
520 &key)); 515 &key));
521 } 516 }
522 else 517 else
518 {
523 handle->type = GNUNET_GNSRECORD_TYPE_ANY; 519 handle->type = GNUNET_GNSRECORD_TYPE_ANY;
520 }
524 521
522 //check parameter options, optional
523 GNUNET_CRYPTO_hash (GNUNET_REST_JSONAPI_GNS_OPTIONS,
524 strlen (GNUNET_REST_JSONAPI_GNS_OPTIONS),
525 &key);
526 handle->options = GNUNET_GNS_LO_DEFAULT;
527 if ( GNUNET_YES
528 == GNUNET_CONTAINER_multihashmap_contains (conndata_handle->url_param_map,
529 &key))
530 {
531 temp_val = GNUNET_CONTAINER_multihashmap_get (conndata_handle->url_param_map, &key);
532 if (1 < strlen(temp_val))
533 {
534 handle->options = GNUNET_GNS_LO_DEFAULT;
535 }
536 else
537 {
538 //atoi because no valid conversion is default local option
539 enum_test = atoi(temp_val);
540 if (2 < enum_test)
541 handle->options = GNUNET_GNS_LO_DEFAULT;
542 else
543 handle->options = enum_test;
544 }
545 }
546 else
547 handle->options = GNUNET_GNS_LO_DEFAULT;
548
549 //check parameter pkey, shortcut to lookup
525 GNUNET_CRYPTO_hash (GNUNET_REST_JSONAPI_GNS_PKEY, 550 GNUNET_CRYPTO_hash (GNUNET_REST_JSONAPI_GNS_PKEY,
526 strlen (GNUNET_REST_JSONAPI_GNS_PKEY), 551 strlen (GNUNET_REST_JSONAPI_GNS_PKEY),
527 &key); 552 &key);
528 if ( GNUNET_YES == 553 if ( GNUNET_YES
529 GNUNET_CONTAINER_multihashmap_contains (conndata_handle->url_param_map, 554 == GNUNET_CONTAINER_multihashmap_contains (conndata_handle->url_param_map,
530 &key) ) 555 &key))
531 { 556 {
532 handle->pkey_str = GNUNET_CONTAINER_multihashmap_get (conndata_handle->url_param_map, 557 handle->pkey_str = GNUNET_CONTAINER_multihashmap_get (conndata_handle->url_param_map,
533 &key); 558 &key);
534 GNUNET_assert (NULL != handle->pkey_str); 559 GNUNET_assert(NULL != handle->pkey_str);
535 if (GNUNET_OK != 560 if (GNUNET_OK
536 GNUNET_CRYPTO_ecdsa_public_key_from_string (handle->pkey_str, 561 != GNUNET_CRYPTO_ecdsa_public_key_from_string (
537 strlen(handle->pkey_str), 562 handle->pkey_str, strlen (handle->pkey_str), &(handle->pkey)))
538 &(handle->pkey)))
539 { 563 {
564 handle->emsg = GNUNET_strdup("Parameter pkey has a wrong format");
540 GNUNET_SCHEDULER_add_now (&do_error, handle); 565 GNUNET_SCHEDULER_add_now (&do_error, handle);
541 return; 566 return;
542 } 567 }
543 lookup_with_public_key (handle); 568 lookup_with_public_key (handle);
544 return; 569 return;
545 } 570 }
571
572 //check parameter ego, lookup public key of ego
546 GNUNET_CRYPTO_hash (GNUNET_REST_JSONAPI_GNS_EGO, 573 GNUNET_CRYPTO_hash (GNUNET_REST_JSONAPI_GNS_EGO,
547 strlen (GNUNET_REST_JSONAPI_GNS_EGO), 574 strlen (GNUNET_REST_JSONAPI_GNS_EGO),
548 &key); 575 &key);
@@ -558,24 +585,23 @@ get_gns_cont (struct GNUNET_REST_RequestHandle *conndata_handle,
558 handle); 585 handle);
559 return; 586 return;
560 } 587 }
588
589 //if name ends with .zkey then get public key
561 if ( (NULL != handle->name) && 590 if ( (NULL != handle->name) &&
562 (strlen (handle->name) > 4) && 591 (strlen (handle->name) > 4) &&
563 (0 == strcmp (".zkey", 592 (0 == strcmp (".zkey",
564 &handle->name[strlen (handle->name) - 4])) ) 593 &handle->name[strlen (handle->name) - 4])) )
565 { 594 {
566 GNUNET_CRYPTO_ecdsa_key_get_public 595 GNUNET_CRYPTO_ecdsa_key_get_public( GNUNET_CRYPTO_ecdsa_key_get_anonymous (),
567 (GNUNET_CRYPTO_ecdsa_key_get_anonymous (), 596 &(handle->pkey));
568 &(handle->pkey));
569 lookup_with_public_key (handle); 597 lookup_with_public_key (handle);
570 } 598 }
571 else 599 else //else use gns-master identity
572 { 600 {
573 GNUNET_break (NULL == handle->id_op);
574 handle->id_op = GNUNET_IDENTITY_get (handle->identity, 601 handle->id_op = GNUNET_IDENTITY_get (handle->identity,
575 "gns-master", 602 "gns-master",
576 &identity_master_cb, 603 &identity_master_cb,
577 handle); 604 handle);
578 GNUNET_assert (NULL != handle->id_op);
579 } 605 }
580} 606}
581 607
@@ -633,10 +659,10 @@ rest_gns_process_request (struct GNUNET_REST_RequestHandle *conndata_handle,
633 handle->proc = proc; 659 handle->proc = proc;
634 handle->rest_handle = conndata_handle; 660 handle->rest_handle = conndata_handle;
635 661
636 if (GNUNET_NO == GNUNET_JSONAPI_handle_request (conndata_handle, 662 if (GNUNET_NO == GNUNET_REST_handle_request (conndata_handle,
637 handlers, 663 handlers,
638 &err, 664 &err,
639 handle)) 665 handle))
640 { 666 {
641 handle->response_code = err.error_code; 667 handle->response_code = err.error_code;
642 GNUNET_SCHEDULER_add_now (&do_error, handle); 668 GNUNET_SCHEDULER_add_now (&do_error, handle);