aboutsummaryrefslogtreecommitdiff
path: root/src/namestore/plugin_rest_namestore.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/namestore/plugin_rest_namestore.c')
-rw-r--r--src/namestore/plugin_rest_namestore.c632
1 files changed, 317 insertions, 315 deletions
diff --git a/src/namestore/plugin_rest_namestore.c b/src/namestore/plugin_rest_namestore.c
index 1d8c180fb..42fbf8347 100644
--- a/src/namestore/plugin_rest_namestore.c
+++ b/src/namestore/plugin_rest_namestore.c
@@ -16,7 +16,7 @@
16 along with this program. If not, see <http://www.gnu.org/licenses/>. 16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 17
18 SPDX-License-Identifier: AGPL3.0-or-later 18 SPDX-License-Identifier: AGPL3.0-or-later
19 */ 19 */
20/** 20/**
21 * @author Martin Schanzenbach 21 * @author Martin Schanzenbach
22 * @author Philippe Buschmann 22 * @author Philippe Buschmann
@@ -87,16 +87,14 @@ static char *allow_methods;
87/** 87/**
88 * @brief struct returned by the initialization function of the plugin 88 * @brief struct returned by the initialization function of the plugin
89 */ 89 */
90struct Plugin 90struct Plugin {
91{
92 const struct GNUNET_CONFIGURATION_Handle *cfg; 91 const struct GNUNET_CONFIGURATION_Handle *cfg;
93}; 92};
94 93
95/** 94/**
96 * The default namestore ego 95 * The default namestore ego
97 */ 96 */
98struct EgoEntry 97struct EgoEntry {
99{
100 /** 98 /**
101 * DLL 99 * DLL
102 */ 100 */
@@ -126,8 +124,7 @@ struct EgoEntry
126/** 124/**
127 * The request handle 125 * The request handle
128 */ 126 */
129struct RequestHandle 127struct RequestHandle {
130{
131 /** 128 /**
132 * Records to store 129 * Records to store
133 */ 130 */
@@ -244,61 +241,61 @@ struct RequestHandle
244 * @param handle Handle to clean up 241 * @param handle Handle to clean up
245 */ 242 */
246static void 243static void
247cleanup_handle (void *cls) 244cleanup_handle(void *cls)
248{ 245{
249 struct RequestHandle *handle = cls; 246 struct RequestHandle *handle = cls;
250 struct EgoEntry *ego_entry; 247 struct EgoEntry *ego_entry;
251 struct EgoEntry *ego_tmp; 248 struct EgoEntry *ego_tmp;
252 249
253 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Cleaning up\n"); 250 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Cleaning up\n");
254 if (NULL != handle->timeout_task) 251 if (NULL != handle->timeout_task)
255 { 252 {
256 GNUNET_SCHEDULER_cancel (handle->timeout_task); 253 GNUNET_SCHEDULER_cancel(handle->timeout_task);
257 handle->timeout_task = NULL; 254 handle->timeout_task = NULL;
258 } 255 }
259 if (NULL != handle->record_name) 256 if (NULL != handle->record_name)
260 GNUNET_free (handle->record_name); 257 GNUNET_free(handle->record_name);
261 if (NULL != handle->url) 258 if (NULL != handle->url)
262 GNUNET_free (handle->url); 259 GNUNET_free(handle->url);
263 if (NULL != handle->emsg) 260 if (NULL != handle->emsg)
264 GNUNET_free (handle->emsg); 261 GNUNET_free(handle->emsg);
265 if (NULL != handle->rd) 262 if (NULL != handle->rd)
266 {
267 for (int i = 0; i < handle->rd_count; i++)
268 { 263 {
269 if (NULL != handle->rd[i].data) 264 for (int i = 0; i < handle->rd_count; i++)
270 GNUNET_free ((void *) handle->rd[i].data); 265 {
266 if (NULL != handle->rd[i].data)
267 GNUNET_free((void *)handle->rd[i].data);
268 }
269 GNUNET_free(handle->rd);
271 } 270 }
272 GNUNET_free (handle->rd);
273 }
274 if (NULL != handle->timeout_task) 271 if (NULL != handle->timeout_task)
275 GNUNET_SCHEDULER_cancel (handle->timeout_task); 272 GNUNET_SCHEDULER_cancel(handle->timeout_task);
276 if (NULL != handle->list_it) 273 if (NULL != handle->list_it)
277 GNUNET_NAMESTORE_zone_iteration_stop (handle->list_it); 274 GNUNET_NAMESTORE_zone_iteration_stop(handle->list_it);
278 if (NULL != handle->add_qe) 275 if (NULL != handle->add_qe)
279 GNUNET_NAMESTORE_cancel (handle->add_qe); 276 GNUNET_NAMESTORE_cancel(handle->add_qe);
280 if (NULL != handle->identity_handle) 277 if (NULL != handle->identity_handle)
281 GNUNET_IDENTITY_disconnect (handle->identity_handle); 278 GNUNET_IDENTITY_disconnect(handle->identity_handle);
282 if (NULL != handle->ns_handle) 279 if (NULL != handle->ns_handle)
283 { 280 {
284 GNUNET_NAMESTORE_disconnect (handle->ns_handle); 281 GNUNET_NAMESTORE_disconnect(handle->ns_handle);
285 } 282 }
286 283
287 for (ego_entry = handle->ego_head; NULL != ego_entry;) 284 for (ego_entry = handle->ego_head; NULL != ego_entry;)
288 { 285 {
289 ego_tmp = ego_entry; 286 ego_tmp = ego_entry;
290 ego_entry = ego_entry->next; 287 ego_entry = ego_entry->next;
291 GNUNET_free (ego_tmp->identifier); 288 GNUNET_free(ego_tmp->identifier);
292 GNUNET_free (ego_tmp->keystring); 289 GNUNET_free(ego_tmp->keystring);
293 GNUNET_free (ego_tmp); 290 GNUNET_free(ego_tmp);
294 } 291 }
295 292
296 if (NULL != handle->resp_object) 293 if (NULL != handle->resp_object)
297 { 294 {
298 json_decref (handle->resp_object); 295 json_decref(handle->resp_object);
299 } 296 }
300 297
301 GNUNET_free (handle); 298 GNUNET_free(handle);
302} 299}
303 300
304 301
@@ -308,26 +305,26 @@ cleanup_handle (void *cls)
308 * @param cls the `struct RequestHandle` 305 * @param cls the `struct RequestHandle`
309 */ 306 */
310static void 307static void
311do_error (void *cls) 308do_error(void *cls)
312{ 309{
313 struct RequestHandle *handle = cls; 310 struct RequestHandle *handle = cls;
314 struct MHD_Response *resp; 311 struct MHD_Response *resp;
315 json_t *json_error = json_object (); 312 json_t *json_error = json_object();
316 char *response; 313 char *response;
317 314
318 if (NULL == handle->emsg) 315 if (NULL == handle->emsg)
319 handle->emsg = GNUNET_strdup (GNUNET_REST_NAMESTORE_ERROR_UNKNOWN); 316 handle->emsg = GNUNET_strdup(GNUNET_REST_NAMESTORE_ERROR_UNKNOWN);
320 317
321 json_object_set_new (json_error, "error", json_string (handle->emsg)); 318 json_object_set_new(json_error, "error", json_string(handle->emsg));
322 319
323 if (0 == handle->response_code) 320 if (0 == handle->response_code)
324 handle->response_code = MHD_HTTP_INTERNAL_SERVER_ERROR; 321 handle->response_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
325 response = json_dumps (json_error, 0); 322 response = json_dumps(json_error, 0);
326 resp = GNUNET_REST_create_response (response); 323 resp = GNUNET_REST_create_response(response);
327 handle->proc (handle->proc_cls, resp, handle->response_code); 324 handle->proc(handle->proc_cls, resp, handle->response_code);
328 json_decref (json_error); 325 json_decref(json_error);
329 GNUNET_free (response); 326 GNUNET_free(response);
330 GNUNET_SCHEDULER_add_now (&cleanup_handle, handle); 327 GNUNET_SCHEDULER_add_now(&cleanup_handle, handle);
331} 328}
332 329
333 330
@@ -341,19 +338,20 @@ do_error (void *cls)
341 * @return EgoEntry or NULL if not found 338 * @return EgoEntry or NULL if not found
342 */ 339 */
343struct EgoEntry * 340struct EgoEntry *
344get_egoentry_namestore (struct RequestHandle *handle, char *name) 341get_egoentry_namestore(struct RequestHandle *handle, char *name)
345{ 342{
346 struct EgoEntry *ego_entry; 343 struct EgoEntry *ego_entry;
344
347 if (NULL != name) 345 if (NULL != name)
348 {
349 for (ego_entry = handle->ego_head; NULL != ego_entry;
350 ego_entry = ego_entry->next)
351 { 346 {
352 if (0 != strcasecmp (name, ego_entry->identifier)) 347 for (ego_entry = handle->ego_head; NULL != ego_entry;
353 continue; 348 ego_entry = ego_entry->next)
354 return ego_entry; 349 {
350 if (0 != strcasecmp(name, ego_entry->identifier))
351 continue;
352 return ego_entry;
353 }
355 } 354 }
356 }
357 return NULL; 355 return NULL;
358} 356}
359 357
@@ -364,11 +362,12 @@ get_egoentry_namestore (struct RequestHandle *handle, char *name)
364 * @param cls the `struct RequestHandle` 362 * @param cls the `struct RequestHandle`
365 */ 363 */
366static void 364static void
367namestore_iteration_error (void *cls) 365namestore_iteration_error(void *cls)
368{ 366{
369 struct RequestHandle *handle = cls; 367 struct RequestHandle *handle = cls;
370 handle->emsg = GNUNET_strdup (GNUNET_REST_NAMESTORE_FAILED); 368
371 GNUNET_SCHEDULER_add_now (&do_error, handle); 369 handle->emsg = GNUNET_strdup(GNUNET_REST_NAMESTORE_FAILED);
370 GNUNET_SCHEDULER_add_now(&do_error, handle);
372 return; 371 return;
373} 372}
374 373
@@ -381,27 +380,27 @@ namestore_iteration_error (void *cls)
381 * @param emsg the error message (can be NULL) 380 * @param emsg the error message (can be NULL)
382 */ 381 */
383static void 382static void
384create_finished (void *cls, int32_t success, const char *emsg) 383create_finished(void *cls, int32_t success, const char *emsg)
385{ 384{
386 struct RequestHandle *handle = cls; 385 struct RequestHandle *handle = cls;
387 struct MHD_Response *resp; 386 struct MHD_Response *resp;
388 387
389 handle->add_qe = NULL; 388 handle->add_qe = NULL;
390 if (GNUNET_YES != success) 389 if (GNUNET_YES != success)
391 {
392 if (NULL != emsg)
393 { 390 {
394 handle->emsg = GNUNET_strdup (emsg); 391 if (NULL != emsg)
395 GNUNET_SCHEDULER_add_now (&do_error, handle); 392 {
393 handle->emsg = GNUNET_strdup(emsg);
394 GNUNET_SCHEDULER_add_now(&do_error, handle);
395 return;
396 }
397 handle->emsg = GNUNET_strdup("Error storing records");
398 GNUNET_SCHEDULER_add_now(&do_error, handle);
396 return; 399 return;
397 } 400 }
398 handle->emsg = GNUNET_strdup ("Error storing records"); 401 resp = GNUNET_REST_create_response(NULL);
399 GNUNET_SCHEDULER_add_now (&do_error, handle); 402 handle->proc(handle->proc_cls, resp, MHD_HTTP_NO_CONTENT);
400 return; 403 GNUNET_SCHEDULER_add_now(&cleanup_handle, handle);
401 }
402 resp = GNUNET_REST_create_response (NULL);
403 handle->proc (handle->proc_cls, resp, MHD_HTTP_NO_CONTENT);
404 GNUNET_SCHEDULER_add_now (&cleanup_handle, handle);
405} 404}
406 405
407 406
@@ -413,34 +412,34 @@ create_finished (void *cls, int32_t success, const char *emsg)
413 * @param emsg the error message (can be NULL) 412 * @param emsg the error message (can be NULL)
414 */ 413 */
415static void 414static void
416del_finished (void *cls, int32_t success, const char *emsg) 415del_finished(void *cls, int32_t success, const char *emsg)
417{ 416{
418 struct RequestHandle *handle = cls; 417 struct RequestHandle *handle = cls;
419 418
420 handle->add_qe = NULL; 419 handle->add_qe = NULL;
421 if (GNUNET_NO == success) 420 if (GNUNET_NO == success)
422 { 421 {
423 handle->response_code = MHD_HTTP_NOT_FOUND; 422 handle->response_code = MHD_HTTP_NOT_FOUND;
424 handle->emsg = GNUNET_strdup ("No record found"); 423 handle->emsg = GNUNET_strdup("No record found");
425 GNUNET_SCHEDULER_add_now (&do_error, handle); 424 GNUNET_SCHEDULER_add_now(&do_error, handle);
426 return; 425 return;
427 } 426 }
428 if (GNUNET_SYSERR == success) 427 if (GNUNET_SYSERR == success)
429 {
430 if (NULL != emsg)
431 { 428 {
432 handle->emsg = GNUNET_strdup (emsg); 429 if (NULL != emsg)
433 GNUNET_SCHEDULER_add_now (&do_error, handle); 430 {
431 handle->emsg = GNUNET_strdup(emsg);
432 GNUNET_SCHEDULER_add_now(&do_error, handle);
433 return;
434 }
435 handle->emsg = GNUNET_strdup("Deleting record failed");
436 GNUNET_SCHEDULER_add_now(&do_error, handle);
434 return; 437 return;
435 } 438 }
436 handle->emsg = GNUNET_strdup ("Deleting record failed"); 439 handle->proc(handle->proc_cls,
437 GNUNET_SCHEDULER_add_now (&do_error, handle); 440 GNUNET_REST_create_response(NULL),
438 return; 441 MHD_HTTP_NO_CONTENT);
439 } 442 GNUNET_SCHEDULER_add_now(&cleanup_handle, handle);
440 handle->proc (handle->proc_cls,
441 GNUNET_REST_create_response (NULL),
442 MHD_HTTP_NO_CONTENT);
443 GNUNET_SCHEDULER_add_now (&cleanup_handle, handle);
444} 443}
445 444
446 445
@@ -451,7 +450,7 @@ del_finished (void *cls, int32_t success, const char *emsg)
451 * @param cls the `struct RequestHandle` 450 * @param cls the `struct RequestHandle`
452 */ 451 */
453static void 452static void
454namestore_list_finished (void *cls) 453namestore_list_finished(void *cls)
455{ 454{
456 struct RequestHandle *handle = cls; 455 struct RequestHandle *handle = cls;
457 char *result_str; 456 char *result_str;
@@ -460,14 +459,14 @@ namestore_list_finished (void *cls)
460 handle->list_it = NULL; 459 handle->list_it = NULL;
461 460
462 if (NULL == handle->resp_object) 461 if (NULL == handle->resp_object)
463 handle->resp_object = json_array (); 462 handle->resp_object = json_array();
464 463
465 result_str = json_dumps (handle->resp_object, 0); 464 result_str = json_dumps(handle->resp_object, 0);
466 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Result %s\n", result_str); 465 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Result %s\n", result_str);
467 resp = GNUNET_REST_create_response (result_str); 466 resp = GNUNET_REST_create_response(result_str);
468 handle->proc (handle->proc_cls, resp, MHD_HTTP_OK); 467 handle->proc(handle->proc_cls, resp, MHD_HTTP_OK);
469 GNUNET_free_non_null (result_str); 468 GNUNET_free_non_null(result_str);
470 GNUNET_SCHEDULER_add_now (&cleanup_handle, handle); 469 GNUNET_SCHEDULER_add_now(&cleanup_handle, handle);
471} 470}
472 471
473 472
@@ -477,22 +476,22 @@ namestore_list_finished (void *cls)
477 * @param handle the RequestHandle 476 * @param handle the RequestHandle
478 */ 477 */
479static void 478static void
480namestore_list_iteration (void *cls, 479namestore_list_iteration(void *cls,
481 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone_key, 480 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone_key,
482 const char *rname, 481 const char *rname,
483 unsigned int rd_len, 482 unsigned int rd_len,
484 const struct GNUNET_GNSRECORD_Data *rd) 483 const struct GNUNET_GNSRECORD_Data *rd)
485{ 484{
486 struct RequestHandle *handle = cls; 485 struct RequestHandle *handle = cls;
487 json_t *record_obj; 486 json_t *record_obj;
488 487
489 if (NULL == handle->resp_object) 488 if (NULL == handle->resp_object)
490 handle->resp_object = json_array (); 489 handle->resp_object = json_array();
491 record_obj = GNUNET_JSON_from_gnsrecord (rname, 490 record_obj = GNUNET_JSON_from_gnsrecord(rname,
492 rd, 491 rd,
493 rd_len); 492 rd_len);
494 json_array_append_new (handle->resp_object, record_obj); 493 json_array_append_new(handle->resp_object, record_obj);
495 GNUNET_NAMESTORE_zone_iterator_next (handle->list_it, 1); 494 GNUNET_NAMESTORE_zone_iterator_next(handle->list_it, 1);
496} 495}
497 496
498 497
@@ -504,9 +503,9 @@ namestore_list_iteration (void *cls,
504 * @param cls the RequestHandle 503 * @param cls the RequestHandle
505 */ 504 */
506void 505void
507namestore_get (struct GNUNET_REST_RequestHandle *con_handle, 506namestore_get(struct GNUNET_REST_RequestHandle *con_handle,
508 const char *url, 507 const char *url,
509 void *cls) 508 void *cls)
510{ 509{
511 struct RequestHandle *handle = cls; 510 struct RequestHandle *handle = cls;
512 struct EgoEntry *ego_entry; 511 struct EgoEntry *ego_entry;
@@ -516,75 +515,77 @@ namestore_get (struct GNUNET_REST_RequestHandle *con_handle,
516 ego_entry = NULL; 515 ego_entry = NULL;
517 516
518 // set zone to name if given 517 // set zone to name if given
519 if (strlen (GNUNET_REST_API_NS_NAMESTORE) < strlen (handle->url)) 518 if (strlen(GNUNET_REST_API_NS_NAMESTORE) < strlen(handle->url))
520 {
521 egoname = &handle->url[strlen (GNUNET_REST_API_NS_NAMESTORE) + 1];
522 ego_entry = get_egoentry_namestore (handle, egoname);
523
524 if (NULL == ego_entry)
525 { 519 {
526 handle->response_code = MHD_HTTP_NOT_FOUND; 520 egoname = &handle->url[strlen(GNUNET_REST_API_NS_NAMESTORE) + 1];
527 handle->emsg = GNUNET_strdup (GNUNET_REST_IDENTITY_NOT_FOUND); 521 ego_entry = get_egoentry_namestore(handle, egoname);
528 GNUNET_SCHEDULER_add_now (&do_error, handle); 522
529 return; 523 if (NULL == ego_entry)
524 {
525 handle->response_code = MHD_HTTP_NOT_FOUND;
526 handle->emsg = GNUNET_strdup(GNUNET_REST_IDENTITY_NOT_FOUND);
527 GNUNET_SCHEDULER_add_now(&do_error, handle);
528 return;
529 }
530 } 530 }
531 }
532 if (NULL != ego_entry) 531 if (NULL != ego_entry)
533 handle->zone_pkey = GNUNET_IDENTITY_ego_get_private_key (ego_entry->ego); 532 handle->zone_pkey = GNUNET_IDENTITY_ego_get_private_key(ego_entry->ego);
534 533
535 handle->list_it = 534 handle->list_it =
536 GNUNET_NAMESTORE_zone_iteration_start (handle->ns_handle, 535 GNUNET_NAMESTORE_zone_iteration_start(handle->ns_handle,
537 handle->zone_pkey, 536 handle->zone_pkey,
538 &namestore_iteration_error, 537 &namestore_iteration_error,
539 handle, 538 handle,
540 &namestore_list_iteration, 539 &namestore_list_iteration,
541 handle, 540 handle,
542 &namestore_list_finished, 541 &namestore_list_finished,
543 handle); 542 handle);
544 if (NULL == handle->list_it) 543 if (NULL == handle->list_it)
545 { 544 {
546 handle->emsg = GNUNET_strdup (GNUNET_REST_NAMESTORE_FAILED); 545 handle->emsg = GNUNET_strdup(GNUNET_REST_NAMESTORE_FAILED);
547 GNUNET_SCHEDULER_add_now (&do_error, handle); 546 GNUNET_SCHEDULER_add_now(&do_error, handle);
548 return; 547 return;
549 } 548 }
550} 549}
551 550
552 551
553static void 552static void
554ns_lookup_error_cb (void *cls) 553ns_lookup_error_cb(void *cls)
555{ 554{
556 struct RequestHandle *handle = cls; 555 struct RequestHandle *handle = cls;
557 handle->emsg = GNUNET_strdup (GNUNET_REST_NAMESTORE_FAILED); 556
558 GNUNET_SCHEDULER_add_now (&do_error, handle); 557 handle->emsg = GNUNET_strdup(GNUNET_REST_NAMESTORE_FAILED);
558 GNUNET_SCHEDULER_add_now(&do_error, handle);
559} 559}
560 560
561 561
562static void 562static void
563ns_lookup_cb (void *cls, 563ns_lookup_cb(void *cls,
564 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone, 564 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
565 const char *label, 565 const char *label,
566 unsigned int rd_count, 566 unsigned int rd_count,
567 const struct GNUNET_GNSRECORD_Data *rd) 567 const struct GNUNET_GNSRECORD_Data *rd)
568{ 568{
569 struct RequestHandle *handle = cls; 569 struct RequestHandle *handle = cls;
570 struct GNUNET_GNSRECORD_Data rd_new[rd_count + handle->rd_count]; 570 struct GNUNET_GNSRECORD_Data rd_new[rd_count + handle->rd_count];
571
571 for (int i = 0; i < rd_count; i++) 572 for (int i = 0; i < rd_count; i++)
572 rd_new[i] = rd[i]; 573 rd_new[i] = rd[i];
573 for (int j = 0; j < handle->rd_count; j++) 574 for (int j = 0; j < handle->rd_count; j++)
574 rd_new[rd_count + j] = handle->rd[j]; 575 rd_new[rd_count + j] = handle->rd[j];
575 handle->add_qe = GNUNET_NAMESTORE_records_store (handle->ns_handle, 576 handle->add_qe = GNUNET_NAMESTORE_records_store(handle->ns_handle,
576 handle->zone_pkey, 577 handle->zone_pkey,
577 handle->record_name, 578 handle->record_name,
578 rd_count + handle->rd_count, 579 rd_count + handle->rd_count,
579 rd_new, 580 rd_new,
580 &create_finished, 581 &create_finished,
581 handle); 582 handle);
582 if (NULL == handle->add_qe) 583 if (NULL == handle->add_qe)
583 { 584 {
584 handle->emsg = GNUNET_strdup (GNUNET_REST_NAMESTORE_FAILED); 585 handle->emsg = GNUNET_strdup(GNUNET_REST_NAMESTORE_FAILED);
585 GNUNET_SCHEDULER_add_now (&do_error, handle); 586 GNUNET_SCHEDULER_add_now(&do_error, handle);
586 return; 587 return;
587 } 588 }
588} 589}
589 590
590 591
@@ -596,9 +597,9 @@ ns_lookup_cb (void *cls,
596 * @param cls the RequestHandle 597 * @param cls the RequestHandle
597 */ 598 */
598void 599void
599namestore_add (struct GNUNET_REST_RequestHandle *con_handle, 600namestore_add(struct GNUNET_REST_RequestHandle *con_handle,
600 const char *url, 601 const char *url,
601 void *cls) 602 void *cls)
602{ 603{
603 struct RequestHandle *handle = cls; 604 struct RequestHandle *handle = cls;
604 struct EgoEntry *ego_entry; 605 struct EgoEntry *ego_entry;
@@ -609,67 +610,67 @@ namestore_add (struct GNUNET_REST_RequestHandle *con_handle,
609 char term_data[handle->rest_handle->data_size + 1]; 610 char term_data[handle->rest_handle->data_size + 1];
610 611
611 if (0 >= handle->rest_handle->data_size) 612 if (0 >= handle->rest_handle->data_size)
612 { 613 {
613 handle->emsg = GNUNET_strdup (GNUNET_REST_NAMESTORE_NO_DATA); 614 handle->emsg = GNUNET_strdup(GNUNET_REST_NAMESTORE_NO_DATA);
614 GNUNET_SCHEDULER_add_now (&do_error, handle); 615 GNUNET_SCHEDULER_add_now(&do_error, handle);
615 return; 616 return;
616 } 617 }
617 term_data[handle->rest_handle->data_size] = '\0'; 618 term_data[handle->rest_handle->data_size] = '\0';
618 GNUNET_memcpy (term_data, 619 GNUNET_memcpy(term_data,
619 handle->rest_handle->data, 620 handle->rest_handle->data,
620 handle->rest_handle->data_size); 621 handle->rest_handle->data_size);
621 data_js = json_loads (term_data, JSON_DECODE_ANY, &err); 622 data_js = json_loads(term_data, JSON_DECODE_ANY, &err);
622 struct GNUNET_JSON_Specification gnsspec[] = 623 struct GNUNET_JSON_Specification gnsspec[] =
623 {GNUNET_JSON_spec_gnsrecord (&handle->rd, &handle->rd_count, &handle->record_name), GNUNET_JSON_spec_end ()}; 624 { GNUNET_JSON_spec_gnsrecord(&handle->rd, &handle->rd_count, &handle->record_name), GNUNET_JSON_spec_end() };
624 if (GNUNET_OK != GNUNET_JSON_parse (data_js, gnsspec, NULL, NULL)) 625 if (GNUNET_OK != GNUNET_JSON_parse(data_js, gnsspec, NULL, NULL))
625 { 626 {
626 handle->emsg = GNUNET_strdup (GNUNET_REST_NAMESTORE_INVALID_DATA); 627 handle->emsg = GNUNET_strdup(GNUNET_REST_NAMESTORE_INVALID_DATA);
627 GNUNET_SCHEDULER_add_now (&do_error, handle); 628 GNUNET_SCHEDULER_add_now(&do_error, handle);
628 json_decref (data_js); 629 json_decref(data_js);
629 return; 630 return;
630 } 631 }
631 GNUNET_JSON_parse_free (gnsspec); 632 GNUNET_JSON_parse_free(gnsspec);
632 if (0 >= strlen (handle->record_name)) 633 if (0 >= strlen(handle->record_name))
633 { 634 {
634 handle->emsg = GNUNET_strdup (GNUNET_REST_NAMESTORE_INVALID_DATA); 635 handle->emsg = GNUNET_strdup(GNUNET_REST_NAMESTORE_INVALID_DATA);
635 GNUNET_SCHEDULER_add_now (&do_error, handle); 636 GNUNET_SCHEDULER_add_now(&do_error, handle);
636 json_decref (data_js); 637 json_decref(data_js);
637 return; 638 return;
638 } 639 }
639 json_decref (data_js); 640 json_decref(data_js);
640 641
641 egoname = NULL; 642 egoname = NULL;
642 ego_entry = NULL; 643 ego_entry = NULL;
643 644
644 // set zone to name if given 645 // set zone to name if given
645 if (strlen (GNUNET_REST_API_NS_NAMESTORE) < strlen (handle->url)) 646 if (strlen(GNUNET_REST_API_NS_NAMESTORE) < strlen(handle->url))
646 {
647 egoname = &handle->url[strlen (GNUNET_REST_API_NS_NAMESTORE) + 1];
648 ego_entry = get_egoentry_namestore (handle, egoname);
649
650 if (NULL == ego_entry)
651 { 647 {
652 handle->response_code = MHD_HTTP_NOT_FOUND; 648 egoname = &handle->url[strlen(GNUNET_REST_API_NS_NAMESTORE) + 1];
653 handle->emsg = GNUNET_strdup (GNUNET_REST_IDENTITY_NOT_FOUND); 649 ego_entry = get_egoentry_namestore(handle, egoname);
654 GNUNET_SCHEDULER_add_now (&do_error, handle); 650
655 return; 651 if (NULL == ego_entry)
652 {
653 handle->response_code = MHD_HTTP_NOT_FOUND;
654 handle->emsg = GNUNET_strdup(GNUNET_REST_IDENTITY_NOT_FOUND);
655 GNUNET_SCHEDULER_add_now(&do_error, handle);
656 return;
657 }
656 } 658 }
657 }
658 if (NULL != ego_entry) 659 if (NULL != ego_entry)
659 handle->zone_pkey = GNUNET_IDENTITY_ego_get_private_key (ego_entry->ego); 660 handle->zone_pkey = GNUNET_IDENTITY_ego_get_private_key(ego_entry->ego);
660 handle->add_qe = GNUNET_NAMESTORE_records_lookup (handle->ns_handle, 661 handle->add_qe = GNUNET_NAMESTORE_records_lookup(handle->ns_handle,
661 handle->zone_pkey, 662 handle->zone_pkey,
662 handle->record_name, 663 handle->record_name,
663 &ns_lookup_error_cb, 664 &ns_lookup_error_cb,
664 handle, 665 handle,
665 &ns_lookup_cb, 666 &ns_lookup_cb,
666 handle); 667 handle);
667 if (NULL == handle->add_qe) 668 if (NULL == handle->add_qe)
668 { 669 {
669 handle->emsg = GNUNET_strdup (GNUNET_REST_NAMESTORE_FAILED); 670 handle->emsg = GNUNET_strdup(GNUNET_REST_NAMESTORE_FAILED);
670 GNUNET_SCHEDULER_add_now (&do_error, handle); 671 GNUNET_SCHEDULER_add_now(&do_error, handle);
671 return; 672 return;
672 } 673 }
673} 674}
674 675
675 676
@@ -681,9 +682,9 @@ namestore_add (struct GNUNET_REST_RequestHandle *con_handle,
681 * @param cls the RequestHandle 682 * @param cls the RequestHandle
682 */ 683 */
683void 684void
684namestore_delete (struct GNUNET_REST_RequestHandle *con_handle, 685namestore_delete(struct GNUNET_REST_RequestHandle *con_handle,
685 const char *url, 686 const char *url,
686 void *cls) 687 void *cls)
687{ 688{
688 struct RequestHandle *handle = cls; 689 struct RequestHandle *handle = cls;
689 struct GNUNET_HashCode key; 690 struct GNUNET_HashCode key;
@@ -694,46 +695,46 @@ namestore_delete (struct GNUNET_REST_RequestHandle *con_handle,
694 ego_entry = NULL; 695 ego_entry = NULL;
695 696
696 // set zone to name if given 697 // set zone to name if given
697 if (strlen (GNUNET_REST_API_NS_NAMESTORE) < strlen (handle->url)) 698 if (strlen(GNUNET_REST_API_NS_NAMESTORE) < strlen(handle->url))
698 {
699 egoname = &handle->url[strlen (GNUNET_REST_API_NS_NAMESTORE) + 1];
700 ego_entry = get_egoentry_namestore (handle, egoname);
701
702 if (NULL == ego_entry)
703 { 699 {
704 handle->response_code = MHD_HTTP_NOT_FOUND; 700 egoname = &handle->url[strlen(GNUNET_REST_API_NS_NAMESTORE) + 1];
705 handle->emsg = GNUNET_strdup (GNUNET_REST_IDENTITY_NOT_FOUND); 701 ego_entry = get_egoentry_namestore(handle, egoname);
706 GNUNET_SCHEDULER_add_now (&do_error, handle); 702
707 return; 703 if (NULL == ego_entry)
704 {
705 handle->response_code = MHD_HTTP_NOT_FOUND;
706 handle->emsg = GNUNET_strdup(GNUNET_REST_IDENTITY_NOT_FOUND);
707 GNUNET_SCHEDULER_add_now(&do_error, handle);
708 return;
709 }
708 } 710 }
709 }
710 if (NULL != ego_entry) 711 if (NULL != ego_entry)
711 handle->zone_pkey = GNUNET_IDENTITY_ego_get_private_key (ego_entry->ego); 712 handle->zone_pkey = GNUNET_IDENTITY_ego_get_private_key(ego_entry->ego);
712 713
713 GNUNET_CRYPTO_hash ("record_name", strlen ("record_name"), &key); 714 GNUNET_CRYPTO_hash("record_name", strlen("record_name"), &key);
714 if (GNUNET_NO == 715 if (GNUNET_NO ==
715 GNUNET_CONTAINER_multihashmap_contains (con_handle->url_param_map, &key)) 716 GNUNET_CONTAINER_multihashmap_contains(con_handle->url_param_map, &key))
716 { 717 {
717 handle->emsg = GNUNET_strdup (GNUNET_REST_NAMESTORE_INVALID_DATA); 718 handle->emsg = GNUNET_strdup(GNUNET_REST_NAMESTORE_INVALID_DATA);
718 GNUNET_SCHEDULER_add_now (&do_error, handle); 719 GNUNET_SCHEDULER_add_now(&do_error, handle);
719 return; 720 return;
720 } 721 }
721 handle->record_name = GNUNET_strdup ( 722 handle->record_name = GNUNET_strdup(
722 GNUNET_CONTAINER_multihashmap_get (con_handle->url_param_map, &key)); 723 GNUNET_CONTAINER_multihashmap_get(con_handle->url_param_map, &key));
723 724
724 handle->add_qe = GNUNET_NAMESTORE_records_store (handle->ns_handle, 725 handle->add_qe = GNUNET_NAMESTORE_records_store(handle->ns_handle,
725 handle->zone_pkey, 726 handle->zone_pkey,
726 handle->record_name, 727 handle->record_name,
727 0, 728 0,
728 NULL, 729 NULL,
729 &del_finished, 730 &del_finished,
730 handle); 731 handle);
731 if (NULL == handle->add_qe) 732 if (NULL == handle->add_qe)
732 { 733 {
733 handle->emsg = GNUNET_strdup (GNUNET_REST_NAMESTORE_FAILED); 734 handle->emsg = GNUNET_strdup(GNUNET_REST_NAMESTORE_FAILED);
734 GNUNET_SCHEDULER_add_now (&do_error, handle); 735 GNUNET_SCHEDULER_add_now(&do_error, handle);
735 return; 736 return;
736 } 737 }
737} 738}
738 739
739 740
@@ -745,18 +746,18 @@ namestore_delete (struct GNUNET_REST_RequestHandle *con_handle,
745 * @param cls the RequestHandle 746 * @param cls the RequestHandle
746 */ 747 */
747static void 748static void
748options_cont (struct GNUNET_REST_RequestHandle *con_handle, 749options_cont(struct GNUNET_REST_RequestHandle *con_handle,
749 const char *url, 750 const char *url,
750 void *cls) 751 void *cls)
751{ 752{
752 struct MHD_Response *resp; 753 struct MHD_Response *resp;
753 struct RequestHandle *handle = cls; 754 struct RequestHandle *handle = cls;
754 755
755 // independent of path return all options 756 // independent of path return all options
756 resp = GNUNET_REST_create_response (NULL); 757 resp = GNUNET_REST_create_response(NULL);
757 MHD_add_response_header (resp, "Access-Control-Allow-Methods", allow_methods); 758 MHD_add_response_header(resp, "Access-Control-Allow-Methods", allow_methods);
758 handle->proc (handle->proc_cls, resp, MHD_HTTP_OK); 759 handle->proc(handle->proc_cls, resp, MHD_HTTP_OK);
759 GNUNET_SCHEDULER_add_now (&cleanup_handle, handle); 760 GNUNET_SCHEDULER_add_now(&cleanup_handle, handle);
760 return; 761 return;
761} 762}
762 763
@@ -767,22 +768,22 @@ options_cont (struct GNUNET_REST_RequestHandle *con_handle,
767 * @param handle the request handle 768 * @param handle the request handle
768 */ 769 */
769static void 770static void
770init_cont (struct RequestHandle *handle) 771init_cont(struct RequestHandle *handle)
771{ 772{
772 struct GNUNET_REST_RequestHandlerError err; 773 struct GNUNET_REST_RequestHandlerError err;
773 static const struct GNUNET_REST_RequestHandler handlers[] = 774 static const struct GNUNET_REST_RequestHandler handlers[] =
774 {{MHD_HTTP_METHOD_GET, GNUNET_REST_API_NS_NAMESTORE, &namestore_get}, 775 { { MHD_HTTP_METHOD_GET, GNUNET_REST_API_NS_NAMESTORE, &namestore_get },
775 {MHD_HTTP_METHOD_POST, GNUNET_REST_API_NS_NAMESTORE, &namestore_add}, 776 { MHD_HTTP_METHOD_POST, GNUNET_REST_API_NS_NAMESTORE, &namestore_add },
776 {MHD_HTTP_METHOD_DELETE, GNUNET_REST_API_NS_NAMESTORE, &namestore_delete}, 777 { MHD_HTTP_METHOD_DELETE, GNUNET_REST_API_NS_NAMESTORE, &namestore_delete },
777 {MHD_HTTP_METHOD_OPTIONS, GNUNET_REST_API_NS_NAMESTORE, &options_cont}, 778 { MHD_HTTP_METHOD_OPTIONS, GNUNET_REST_API_NS_NAMESTORE, &options_cont },
778 GNUNET_REST_HANDLER_END}; 779 GNUNET_REST_HANDLER_END };
779 780
780 if (GNUNET_NO == 781 if (GNUNET_NO ==
781 GNUNET_REST_handle_request (handle->rest_handle, handlers, &err, handle)) 782 GNUNET_REST_handle_request(handle->rest_handle, handlers, &err, handle))
782 { 783 {
783 handle->response_code = err.error_code; 784 handle->response_code = err.error_code;
784 GNUNET_SCHEDULER_add_now (&do_error, handle); 785 GNUNET_SCHEDULER_add_now(&do_error, handle);
785 } 786 }
786} 787}
787 788
788 789
@@ -818,32 +819,32 @@ init_cont (struct RequestHandle *handle)
818 * must thus no longer be used 819 * must thus no longer be used
819 */ 820 */
820static void 821static void
821id_connect_cb (void *cls, 822id_connect_cb(void *cls,
822 struct GNUNET_IDENTITY_Ego *ego, 823 struct GNUNET_IDENTITY_Ego *ego,
823 void **ctx, 824 void **ctx,
824 const char *name) 825 const char *name)
825{ 826{
826 struct RequestHandle *handle = cls; 827 struct RequestHandle *handle = cls;
827 struct EgoEntry *ego_entry; 828 struct EgoEntry *ego_entry;
828 struct GNUNET_CRYPTO_EcdsaPublicKey pk; 829 struct GNUNET_CRYPTO_EcdsaPublicKey pk;
829 830
830 if ((NULL == ego) && (ID_REST_STATE_INIT == handle->state)) 831 if ((NULL == ego) && (ID_REST_STATE_INIT == handle->state))
831 { 832 {
832 handle->state = ID_REST_STATE_POST_INIT; 833 handle->state = ID_REST_STATE_POST_INIT;
833 init_cont (handle); 834 init_cont(handle);
834 return; 835 return;
835 } 836 }
836 if (ID_REST_STATE_INIT == handle->state) 837 if (ID_REST_STATE_INIT == handle->state)
837 { 838 {
838 ego_entry = GNUNET_new (struct EgoEntry); 839 ego_entry = GNUNET_new(struct EgoEntry);
839 GNUNET_IDENTITY_ego_get_public_key (ego, &pk); 840 GNUNET_IDENTITY_ego_get_public_key(ego, &pk);
840 ego_entry->keystring = GNUNET_CRYPTO_ecdsa_public_key_to_string (&pk); 841 ego_entry->keystring = GNUNET_CRYPTO_ecdsa_public_key_to_string(&pk);
841 ego_entry->ego = ego; 842 ego_entry->ego = ego;
842 GNUNET_asprintf (&ego_entry->identifier, "%s", name); 843 GNUNET_asprintf(&ego_entry->identifier, "%s", name);
843 GNUNET_CONTAINER_DLL_insert_tail (handle->ego_head, 844 GNUNET_CONTAINER_DLL_insert_tail(handle->ego_head,
844 handle->ego_tail, 845 handle->ego_tail,
845 ego_entry); 846 ego_entry);
846 } 847 }
847} 848}
848 849
849 850
@@ -859,11 +860,11 @@ id_connect_cb (void *cls,
859 * @return GNUNET_OK if request accepted 860 * @return GNUNET_OK if request accepted
860 */ 861 */
861static void 862static void
862rest_process_request (struct GNUNET_REST_RequestHandle *rest_handle, 863rest_process_request(struct GNUNET_REST_RequestHandle *rest_handle,
863 GNUNET_REST_ResultProcessor proc, 864 GNUNET_REST_ResultProcessor proc,
864 void *proc_cls) 865 void *proc_cls)
865{ 866{
866 struct RequestHandle *handle = GNUNET_new (struct RequestHandle); 867 struct RequestHandle *handle = GNUNET_new(struct RequestHandle);
867 868
868 handle->response_code = 0; 869 handle->response_code = 0;
869 handle->timeout = GNUNET_TIME_UNIT_FOREVER_REL; 870 handle->timeout = GNUNET_TIME_UNIT_FOREVER_REL;
@@ -872,18 +873,18 @@ rest_process_request (struct GNUNET_REST_RequestHandle *rest_handle,
872 handle->rest_handle = rest_handle; 873 handle->rest_handle = rest_handle;
873 handle->zone_pkey = NULL; 874 handle->zone_pkey = NULL;
874 875
875 handle->url = GNUNET_strdup (rest_handle->url); 876 handle->url = GNUNET_strdup(rest_handle->url);
876 if (handle->url[strlen (handle->url) - 1] == '/') 877 if (handle->url[strlen(handle->url) - 1] == '/')
877 handle->url[strlen (handle->url) - 1] = '\0'; 878 handle->url[strlen(handle->url) - 1] = '\0';
878 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connecting...\n"); 879 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Connecting...\n");
879 880
880 handle->ns_handle = GNUNET_NAMESTORE_connect (cfg); 881 handle->ns_handle = GNUNET_NAMESTORE_connect(cfg);
881 handle->identity_handle = 882 handle->identity_handle =
882 GNUNET_IDENTITY_connect (cfg, &id_connect_cb, handle); 883 GNUNET_IDENTITY_connect(cfg, &id_connect_cb, handle);
883 handle->timeout_task = 884 handle->timeout_task =
884 GNUNET_SCHEDULER_add_delayed (handle->timeout, &do_error, handle); 885 GNUNET_SCHEDULER_add_delayed(handle->timeout, &do_error, handle);
885 886
886 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connected\n"); 887 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Connected\n");
887} 888}
888 889
889 890
@@ -894,7 +895,7 @@ rest_process_request (struct GNUNET_REST_RequestHandle *rest_handle,
894 * @return NULL on error, otherwise the plugin context 895 * @return NULL on error, otherwise the plugin context
895 */ 896 */
896void * 897void *
897libgnunet_plugin_rest_namestore_init (void *cls) 898libgnunet_plugin_rest_namestore_init(void *cls)
898{ 899{
899 static struct Plugin plugin; 900 static struct Plugin plugin;
900 struct GNUNET_REST_Plugin *api; 901 struct GNUNET_REST_Plugin *api;
@@ -902,21 +903,21 @@ libgnunet_plugin_rest_namestore_init (void *cls)
902 cfg = cls; 903 cfg = cls;
903 if (NULL != plugin.cfg) 904 if (NULL != plugin.cfg)
904 return NULL; /* can only initialize once! */ 905 return NULL; /* can only initialize once! */
905 memset (&plugin, 0, sizeof (struct Plugin)); 906 memset(&plugin, 0, sizeof(struct Plugin));
906 plugin.cfg = cfg; 907 plugin.cfg = cfg;
907 api = GNUNET_new (struct GNUNET_REST_Plugin); 908 api = GNUNET_new(struct GNUNET_REST_Plugin);
908 api->cls = &plugin; 909 api->cls = &plugin;
909 api->name = GNUNET_REST_API_NS_NAMESTORE; 910 api->name = GNUNET_REST_API_NS_NAMESTORE;
910 api->process_request = &rest_process_request; 911 api->process_request = &rest_process_request;
911 GNUNET_asprintf (&allow_methods, 912 GNUNET_asprintf(&allow_methods,
912 "%s, %s, %s, %s, %s", 913 "%s, %s, %s, %s, %s",
913 MHD_HTTP_METHOD_GET, 914 MHD_HTTP_METHOD_GET,
914 MHD_HTTP_METHOD_POST, 915 MHD_HTTP_METHOD_POST,
915 MHD_HTTP_METHOD_PUT, 916 MHD_HTTP_METHOD_PUT,
916 MHD_HTTP_METHOD_DELETE, 917 MHD_HTTP_METHOD_DELETE,
917 MHD_HTTP_METHOD_OPTIONS); 918 MHD_HTTP_METHOD_OPTIONS);
918 919
919 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _ ("Namestore REST API initialized\n")); 920 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, _("Namestore REST API initialized\n"));
920 return api; 921 return api;
921} 922}
922 923
@@ -928,15 +929,16 @@ libgnunet_plugin_rest_namestore_init (void *cls)
928 * @return always NULL 929 * @return always NULL
929 */ 930 */
930void * 931void *
931libgnunet_plugin_rest_namestore_done (void *cls) 932libgnunet_plugin_rest_namestore_done(void *cls)
932{ 933{
933 struct GNUNET_REST_Plugin *api = cls; 934 struct GNUNET_REST_Plugin *api = cls;
934 struct Plugin *plugin = api->cls; 935 struct Plugin *plugin = api->cls;
936
935 plugin->cfg = NULL; 937 plugin->cfg = NULL;
936 938
937 GNUNET_free_non_null (allow_methods); 939 GNUNET_free_non_null(allow_methods);
938 GNUNET_free (api); 940 GNUNET_free(api);
939 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Namestore REST plugin is finished\n"); 941 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Namestore REST plugin is finished\n");
940 return NULL; 942 return NULL;
941} 943}
942 944