aboutsummaryrefslogtreecommitdiff
path: root/src/namestore
diff options
context:
space:
mode:
authorPhil <phil.buschmann@tum.de>2018-07-25 21:57:49 +0200
committerPhil <phil.buschmann@tum.de>2018-07-25 21:57:49 +0200
commitcc577a227d6a5ae8ef75e0fa91ef98ced2d2b743 (patch)
tree07084e1aa8a37dc0dc4d15fe0724801d7867157a /src/namestore
parent4992eacc10bedaa0edfa03b401253408c6267798 (diff)
downloadgnunet-cc577a227d6a5ae8ef75e0fa91ef98ced2d2b743.tar.gz
gnunet-cc577a227d6a5ae8ef75e0fa91ef98ced2d2b743.zip
-wip namestore api, changed adding gnsrecord
Diffstat (limited to 'src/namestore')
-rw-r--r--src/namestore/plugin_rest_namestore.c124
1 files changed, 53 insertions, 71 deletions
diff --git a/src/namestore/plugin_rest_namestore.c b/src/namestore/plugin_rest_namestore.c
index ab490c04f..afe010b79 100644
--- a/src/namestore/plugin_rest_namestore.c
+++ b/src/namestore/plugin_rest_namestore.c
@@ -2,20 +2,18 @@
2 This file is part of GNUnet. 2 This file is part of GNUnet.
3 Copyright (C) 2012-2015 GNUnet e.V. 3 Copyright (C) 2012-2015 GNUnet e.V.
4 4
5 GNUnet is free software; you can redistribute it and/or modify 5 GNUnet is free software: you can redistribute it and/or modify it
6 it under the terms of the GNU General Public License as published 6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation; either version 3, or (at your 7 by the Free Software Foundation, either version 3 of the License,
8 option) any later version. 8 or (at your option) any later version.
9 9
10 GNUnet is distributed in the hope that it will be useful, but 10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of 11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details. 13 Affero General Public License for more details.
14 14
15 You should have received a copy of the GNU General Public License 15 You should have received a copy of the GNU Affero General Public License
16 along with GNUnet; see the file COPYING. If not, write to the 16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
19 */ 17 */
20/** 18/**
21 * @author Martin Schanzenbach 19 * @author Martin Schanzenbach
@@ -42,6 +40,7 @@
42#define GNUNET_REST_JSON_NAMESTORE_VALUE "value" 40#define GNUNET_REST_JSON_NAMESTORE_VALUE "value"
43#define GNUNET_REST_JSON_NAMESTORE_EXPIRATION "expiration" 41#define GNUNET_REST_JSON_NAMESTORE_EXPIRATION "expiration"
44#define GNUNET_REST_JSON_NAMESTORE_EXPIRED "expired" 42#define GNUNET_REST_JSON_NAMESTORE_EXPIRED "expired"
43#define GNUNET_REST_ERROR_UNKNOWN "Unknown Error"
45 44
46#define GNUNET_REST_NAMESTORE_RD_COUNT 1 45#define GNUNET_REST_NAMESTORE_RD_COUNT 1
47 46
@@ -96,17 +95,17 @@ struct RequestHandle
96 /** 95 /**
97 * Records to store 96 * Records to store
98 */ 97 */
99 struct GNUNET_GNSRECORD_Data *rd; 98 char *label_name;
100 99
101 /** 100 /**
102 * NAMESTORE Operation 101 * Records to store
103 */ 102 */
104 struct GNUNET_NAMESTORE_QueueEntry *add_qe; 103 struct GNUNET_GNSRECORD_Data *rd;
105 104
106 /** 105 /**
107 * JSON data parser 106 * NAMESTORE Operation
108 */ 107 */
109 struct GNUNET_REST_JSON_Data *json_data; 108 struct GNUNET_NAMESTORE_QueueEntry *add_qe;
110 109
111 /** 110 /**
112 * Response object 111 * Response object
@@ -192,8 +191,9 @@ struct RequestHandle
192 * @param handle Handle to clean up 191 * @param handle Handle to clean up
193 */ 192 */
194static void 193static void
195cleanup_handle (struct RequestHandle *handle) 194cleanup_handle (void *cls)
196{ 195{
196 struct RequestHandle *handle = cls;
197 size_t index; 197 size_t index;
198 json_t *json_ego; 198 json_t *json_ego;
199 199
@@ -204,6 +204,8 @@ cleanup_handle (struct RequestHandle *handle)
204 GNUNET_SCHEDULER_cancel (handle->timeout_task); 204 GNUNET_SCHEDULER_cancel (handle->timeout_task);
205 handle->timeout_task = NULL; 205 handle->timeout_task = NULL;
206 } 206 }
207 if (NULL != handle->label_name)
208 GNUNET_free(handle->label_name);
207 if (NULL != handle->url) 209 if (NULL != handle->url)
208 GNUNET_free(handle->url); 210 GNUNET_free(handle->url);
209 if (NULL != handle->emsg) 211 if (NULL != handle->emsg)
@@ -243,9 +245,6 @@ cleanup_handle (struct RequestHandle *handle)
243 } 245 }
244 json_decref(handle->resp_object); 246 json_decref(handle->resp_object);
245 } 247 }
246
247 if (NULL != handle->json_data)
248 GNUNET_REST_JSON_free(handle->json_data);
249 248
250 GNUNET_free (handle); 249 GNUNET_free (handle);
251} 250}
@@ -261,20 +260,22 @@ do_error (void *cls)
261{ 260{
262 struct RequestHandle *handle = cls; 261 struct RequestHandle *handle = cls;
263 struct MHD_Response *resp; 262 struct MHD_Response *resp;
264 char *json_error; 263 json_t *json_error = json_object();
264 char *response;
265 265
266 if (NULL == handle->emsg) 266 if (NULL == handle->emsg)
267 handle->emsg = GNUNET_strdup("Unknown Error"); 267 handle->emsg = GNUNET_strdup(GNUNET_REST_ERROR_UNKNOWN);
268
269 json_object_set_new(json_error,"error", json_string(handle->emsg));
268 270
269 GNUNET_asprintf (&json_error, "{\"error\": \"%s\"}", handle->emsg);
270
271 if (0 == handle->response_code) 271 if (0 == handle->response_code)
272 handle->response_code = MHD_HTTP_OK; 272 handle->response_code = MHD_HTTP_OK;
273 273 response = json_dumps (json_error, 0);
274 resp = GNUNET_REST_create_response (json_error); 274 resp = GNUNET_REST_create_response (response);
275 handle->proc (handle->proc_cls, resp, handle->response_code); 275 handle->proc (handle->proc_cls, resp, handle->response_code);
276 cleanup_handle (handle); 276 json_decref(json_error);
277 GNUNET_free(json_error); 277 GNUNET_free(response);
278 GNUNET_SCHEDULER_add_now (&cleanup_handle, handle);
278} 279}
279 280
280/** 281/**
@@ -287,7 +288,7 @@ namestore_iteration_error (void *cls)
287 struct MHD_Response *resp = GNUNET_REST_create_response (NULL); 288 struct MHD_Response *resp = GNUNET_REST_create_response (NULL);
288 handle->response_code = MHD_HTTP_INTERNAL_SERVER_ERROR; 289 handle->response_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
289 handle->proc (handle->proc_cls, resp, handle->response_code); 290 handle->proc (handle->proc_cls, resp, handle->response_code);
290 cleanup_handle (handle); 291 GNUNET_SCHEDULER_add_now (&cleanup_handle, handle);
291} 292}
292 293
293/** 294/**
@@ -360,7 +361,7 @@ create_finished (void *cls, int32_t success, const char *emsg)
360 } 361 }
361 resp = GNUNET_REST_create_response (NULL); 362 resp = GNUNET_REST_create_response (NULL);
362 handle->proc (handle->proc_cls, resp, MHD_HTTP_NO_CONTENT); 363 handle->proc (handle->proc_cls, resp, MHD_HTTP_NO_CONTENT);
363 cleanup_handle(handle); 364 GNUNET_SCHEDULER_add_now (&cleanup_handle, handle);
364} 365}
365 366
366/** 367/**
@@ -391,7 +392,7 @@ namestore_list_finished (void *cls)
391 resp = GNUNET_REST_create_response (result_str); 392 resp = GNUNET_REST_create_response (result_str);
392 handle->proc (handle->proc_cls, resp, MHD_HTTP_OK); 393 handle->proc (handle->proc_cls, resp, MHD_HTTP_OK);
393 GNUNET_free_non_null (result_str); 394 GNUNET_free_non_null (result_str);
394 cleanup_handle(handle); 395 GNUNET_SCHEDULER_add_now (&cleanup_handle, handle);
395} 396}
396 397
397 398
@@ -476,34 +477,7 @@ namestore_get (struct GNUNET_REST_RequestHandle *con_handle,
476 &namestore_list_finished, 477 &namestore_list_finished,
477 handle); 478 handle);
478} 479}
479 480/*
480int
481check_needed_data(struct RequestHandle *handle){
482 if(NULL == handle->json_data->name)
483 {
484 handle->emsg = GNUNET_strdup("Missing JSON parameter: name");
485 return GNUNET_SYSERR;
486 }
487
488 if(NULL == handle->json_data->type)
489 {
490 handle->emsg = GNUNET_strdup("Missing JSON parameter: type");
491 return GNUNET_SYSERR;
492 }
493
494 if(NULL == handle->json_data->value)
495 {
496 handle->emsg = GNUNET_strdup("Missing JSON parameter: value");
497 return GNUNET_SYSERR;
498 }
499
500 if(NULL == handle->json_data->expiration_time)
501 {
502 handle->emsg = GNUNET_strdup("Missing JSON parameter: expiration time");
503 return GNUNET_SYSERR;
504 }
505 return GNUNET_OK;
506}
507 481
508//TODO filter input 482//TODO filter input
509static int 483static int
@@ -544,7 +518,7 @@ json_to_gnsrecord (struct RequestHandle *handle)
544 rde->flags |= GNUNET_GNSRECORD_RF_SHADOW_RECORD; 518 rde->flags |= GNUNET_GNSRECORD_RF_SHADOW_RECORD;
545 if (1 != handle->is_public) 519 if (1 != handle->is_public)
546 rde->flags |= GNUNET_GNSRECORD_RF_PRIVATE; 520 rde->flags |= GNUNET_GNSRECORD_RF_PRIVATE;
547 */ 521 *
548 if (0 == strcmp (handle->json_data->expiration_time, "never")) 522 if (0 == strcmp (handle->json_data->expiration_time, "never"))
549 { 523 {
550 (*handle->rd).expiration_time = GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us; 524 (*handle->rd).expiration_time = GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us;
@@ -569,7 +543,7 @@ json_to_gnsrecord (struct RequestHandle *handle)
569 } 543 }
570 return GNUNET_OK; 544 return GNUNET_OK;
571} 545}
572 546*/
573 547
574/** 548/**
575 * We're storing a new record; this requires 549 * We're storing a new record; this requires
@@ -590,8 +564,9 @@ create_new_record_cont (void *cls,
590{ 564{
591 struct RequestHandle *handle = cls; 565 struct RequestHandle *handle = cls;
592 566
567
593 handle->add_qe = NULL; 568 handle->add_qe = NULL;
594 if (0 != strcmp (rec_name, handle->json_data->name)) 569 if (0 != strcmp (rec_name, handle->label_name))
595 { 570 {
596 GNUNET_break (0); 571 GNUNET_break (0);
597 do_error (handle); 572 do_error (handle);
@@ -603,19 +578,18 @@ create_new_record_cont (void *cls,
603 handle->proc (handle->proc_cls, 578 handle->proc (handle->proc_cls,
604 GNUNET_REST_create_response (NULL), 579 GNUNET_REST_create_response (NULL),
605 MHD_HTTP_CONFLICT); 580 MHD_HTTP_CONFLICT);
606 cleanup_handle(handle); 581 GNUNET_SCHEDULER_add_now (&cleanup_handle, handle);
607 return; 582 return;
608 } 583 }
609 handle->add_qe = GNUNET_NAMESTORE_records_store (handle->ns_handle, 584 handle->add_qe = GNUNET_NAMESTORE_records_store (handle->ns_handle,
610 &handle->zone_pkey, 585 &handle->zone_pkey,
611 handle->json_data->name, 586 handle->label_name,
612 GNUNET_REST_NAMESTORE_RD_COUNT, 587 GNUNET_REST_NAMESTORE_RD_COUNT,
613 handle->rd, 588 handle->rd,
614 &create_finished, 589 &create_finished,
615 handle); 590 handle);
616} 591}
617 592
618
619/** 593/**
620 * Handle namestore POST request 594 * Handle namestore POST request
621 * 595 *
@@ -629,9 +603,15 @@ namestore_add (struct GNUNET_REST_RequestHandle *con_handle,
629 void *cls) 603 void *cls)
630{ 604{
631 struct RequestHandle *handle = cls; 605 struct RequestHandle *handle = cls;
606 struct GNUNET_GNSRECORD_Data *gns_record;
632 json_t *data_js; 607 json_t *data_js;
608 json_t *name_json;
633 json_error_t err; 609 json_error_t err;
634 char term_data[handle->rest_handle->data_size + 1]; 610 char term_data[handle->rest_handle->data_size + 1];
611 struct GNUNET_JSON_Specification gnsspec[] = {
612 GNUNET_JSON_spec_gnsrecord_data(&gns_record),
613 GNUNET_JSON_spec_end ()
614 };
635 615
636 if (strlen (GNUNET_REST_API_NS_NAMESTORE) != strlen (handle->url)) 616 if (strlen (GNUNET_REST_API_NS_NAMESTORE) != strlen (handle->url))
637 { 617 {
@@ -649,24 +629,26 @@ namestore_add (struct GNUNET_REST_RequestHandle *con_handle,
649 GNUNET_memcpy(term_data, handle->rest_handle->data, 629 GNUNET_memcpy(term_data, handle->rest_handle->data,
650 handle->rest_handle->data_size); 630 handle->rest_handle->data_size);
651 data_js = json_loads (term_data, JSON_DECODE_ANY, &err); 631 data_js = json_loads (term_data, JSON_DECODE_ANY, &err);
652 GNUNET_REST_JSON_parse(&handle->json_data, data_js); 632 GNUNET_assert (GNUNET_OK == GNUNET_JSON_parse (data_js, gnsspec, NULL, NULL));
653 if(NULL == handle->json_data) 633 name_json = json_object_get(data_js, "label");
634 if (!json_is_string(name_json))
654 { 635 {
655 handle->emsg = GNUNET_strdup("Wrong data"); 636 handle->emsg = GNUNET_strdup("Missing name");
656 GNUNET_SCHEDULER_add_now (&do_error, handle); 637 GNUNET_SCHEDULER_add_now (&do_error, handle);
657 return; 638 return;
658 } 639 }
659 if(GNUNET_SYSERR == check_needed_data(handle)) 640 handle->label_name = GNUNET_strdup(json_string_value(name_json));
641 if(NULL == handle->label_name)
660 { 642 {
661 json_decref (data_js); 643 handle->emsg = GNUNET_strdup("Missing name");
662 GNUNET_SCHEDULER_add_now (&do_error, handle); 644 GNUNET_SCHEDULER_add_now (&do_error, handle);
663 return; 645 return;
664 } 646 }
665 json_decref (data_js); 647 json_decref (data_js);
666 json_to_gnsrecord (handle); 648 handle->rd = gns_record;
667 handle->add_qe = GNUNET_NAMESTORE_records_lookup (handle->ns_handle, 649 handle->add_qe = GNUNET_NAMESTORE_records_lookup (handle->ns_handle,
668 &handle->zone_pkey, 650 &handle->zone_pkey,
669 handle->json_data->name, 651 handle->label_name,
670 &do_error, 652 &do_error,
671 handle, 653 handle,
672 &create_new_record_cont, 654 &create_new_record_cont,
@@ -718,7 +700,7 @@ options_cont (struct GNUNET_REST_RequestHandle *con_handle,
718 "Access-Control-Allow-Methods", 700 "Access-Control-Allow-Methods",
719 allow_methods); 701 allow_methods);
720 handle->proc (handle->proc_cls, resp, MHD_HTTP_OK); 702 handle->proc (handle->proc_cls, resp, MHD_HTTP_OK);
721 cleanup_handle (handle); 703 GNUNET_SCHEDULER_add_now (&cleanup_handle, handle);
722 return; 704 return;
723} 705}
724 706