2 This file is part of GNUnet.
3 Copyright (C) 2012-2015 GNUnet e.V.
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
21 * @author Martin Schanzenbach
22 * @file namestore/plugin_rest_namestore.c
23 * @brief GNUnet Namestore REST plugin
28 #include "gnunet_rest_plugin.h"
29 #include "gnunet_namestore_service.h"
30 #include "gnunet_identity_service.h"
31 #include "gnunet_rest_lib.h"
32 #include "gnunet_jsonapi_lib.h"
33 #include "microhttpd.h"
36 #define GNUNET_REST_API_NS_NAMESTORE "/names"
38 #define GNUNET_REST_API_NS_NAMESTORE_ZKEY "/names/zkey"
40 #define GNUNET_REST_JSONAPI_NAMESTORE_TYPEINFO "record"
42 #define GNUNET_REST_JSONAPI_NAMESTORE_NAME "name"
44 #define GNUNET_REST_JSONAPI_NAMESTORE_REVINFO "revinfo"
46 #define GNUNET_REST_JSONAPI_NAMESTORE_RECORD GNUNET_REST_JSONAPI_NAMESTORE_TYPEINFO
48 #define GNUNET_REST_JSONAPI_NAMESTORE_RECORD_TYPE "record_type"
50 #define GNUNET_REST_JSONAPI_NAMESTORE_VALUE "value"
52 #define GNUNET_REST_JSONAPI_NAMESTORE_PUBLIC "public"
54 #define GNUNET_REST_JSONAPI_NAMESTORE_SHADOW "shadow"
56 #define GNUNET_REST_JSONAPI_NAMESTORE_PKEY "pkey"
58 #define GNUNET_REST_JSONAPI_NAMESTORE_ZKEY "zkey"
60 #define GNUNET_REST_JSONAPI_NAMESTORE_EXPIRATION "expiration"
62 #define GNUNET_REST_JSONAPI_NAMESTORE_EGO "ego"
65 * @brief struct returned by the initialization function of the plugin
69 const struct GNUNET_CONFIGURATION_Handle *cfg;
74 * HTTP methods allows for this plugin
76 static char* allow_methods;
78 const struct GNUNET_CONFIGURATION_Handle *cfg;
85 struct RecordEntry *next;
90 struct RecordEntry *prev;
99 struct RecordEntry *record_head;
104 struct record_entry *record_tail;
107 * JSON response object
109 struct GNUNET_JSONAPI_Document *resp_object;
114 struct GNUNET_REST_RequestHandle *rest_handle;
117 * Handle to GNS service.
119 struct GNUNET_IDENTITY_Handle *identity_handle;
122 * Handle to NAMESTORE
124 struct GNUNET_NAMESTORE_Handle *ns_handle;
127 * Handle to NAMESTORE it
129 struct GNUNET_NAMESTORE_ZoneIterator *list_it;
132 * Private key for the zone
134 struct GNUNET_CRYPTO_EcdsaPrivateKey zone_pkey;
137 * Handle to identity lookup
139 struct GNUNET_IDENTITY_EgoLookup *ego_lookup;
142 * Default Ego operation
144 struct GNUNET_IDENTITY_Operation *get_default;
162 * Name of the record to modify
167 * Value of the record
174 const char* zkey_str;
184 struct GNUNET_GNSRECORD_Data *rd;
189 unsigned int rd_count;
192 * NAMESTORE Operation
194 struct GNUNET_NAMESTORE_QueueEntry *add_qe;
197 * NAMESTORE Operation
199 struct GNUNET_NAMESTORE_QueueEntry *reverse_qe;
202 * Desired timeout for the lookup (default is no timeout).
204 struct GNUNET_TIME_Relative timeout;
207 * ID of a task associated with the resolution process.
209 struct GNUNET_SCHEDULER_Task * timeout_task;
212 * The plugin result processor
214 GNUNET_REST_ResultProcessor proc;
217 * The closure of the result processor
229 const struct GNUNET_CONFIGURATION_Handle *cfg;
240 * Cleanup lookup handle
242 * @param handle Handle to clean up
245 cleanup_handle (struct RequestHandle *handle)
247 struct RecordEntry *record_entry;
248 struct RecordEntry *record_tmp;
251 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
253 if (NULL != handle->resp_object)
254 GNUNET_JSONAPI_document_delete (handle->resp_object);
255 if (NULL != handle->name)
256 GNUNET_free (handle->name);
257 if (NULL != handle->timeout_task)
258 GNUNET_SCHEDULER_cancel (handle->timeout_task);
259 if (NULL != handle->ego_lookup)
260 GNUNET_IDENTITY_ego_lookup_cancel (handle->ego_lookup);
261 if (NULL != handle->get_default)
262 GNUNET_IDENTITY_cancel (handle->get_default);
263 if (NULL != handle->list_it)
264 GNUNET_NAMESTORE_zone_iteration_stop (handle->list_it);
265 if (NULL != handle->add_qe)
266 GNUNET_NAMESTORE_cancel (handle->add_qe);
267 if (NULL != handle->identity_handle)
268 GNUNET_IDENTITY_disconnect (handle->identity_handle);
269 if (NULL != handle->ns_handle)
270 GNUNET_NAMESTORE_disconnect (handle->ns_handle);
271 if (NULL != handle->url)
272 GNUNET_free (handle->url);
273 if (NULL != handle->value)
274 GNUNET_free (handle->value);
275 if (NULL != handle->rd)
277 for (i = 0; i < handle->rd_count; i++)
279 if (NULL != handle->rd[i].data)
280 GNUNET_free ((void*)handle->rd[i].data);
282 GNUNET_free (handle->rd);
284 if (NULL != handle->ego_name)
285 GNUNET_free (handle->ego_name);
286 for (record_entry = handle->record_head;
287 NULL != record_entry;)
289 record_tmp = record_entry;
290 record_entry = record_entry->next;
291 GNUNET_free (record_tmp);
293 GNUNET_free (handle);
298 * Create json representation of a GNSRECORD
300 * @param rd the GNSRECORD_Data
303 gnsrecord_to_json (const struct GNUNET_GNSRECORD_Data *rd)
305 const char *typename;
310 typename = GNUNET_GNSRECORD_number_to_typename (rd->record_type);
311 string_val = GNUNET_GNSRECORD_value_to_string (rd->record_type,
315 if (NULL == string_val)
317 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
318 "Record of type %d malformed, skipping\n",
319 (int) rd->record_type);
322 record_obj = json_object();
323 json_object_set_new (record_obj,
324 GNUNET_REST_JSONAPI_NAMESTORE_RECORD_TYPE,
325 json_string (typename));
326 json_object_set_new (record_obj,
327 GNUNET_REST_JSONAPI_NAMESTORE_VALUE,
328 json_string (string_val));
329 GNUNET_free (string_val);
331 if (GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION & rd->flags)
333 struct GNUNET_TIME_Relative time_rel;
334 time_rel.rel_value_us = rd->expiration_time;
335 exp_str = GNUNET_STRINGS_relative_time_to_string (time_rel, 1);
339 struct GNUNET_TIME_Absolute time_abs;
340 time_abs.abs_value_us = rd->expiration_time;
341 exp_str = GNUNET_STRINGS_absolute_time_to_string (time_abs);
343 json_object_set_new (record_obj, GNUNET_REST_JSONAPI_NAMESTORE_EXPIRATION, json_string (exp_str));
345 json_object_set_new (record_obj, "expired",
346 json_boolean (GNUNET_YES == GNUNET_GNSRECORD_is_expired (rd)));
352 * Task run on error. Generates error response and cleans up.
354 * @param cls the request to generate an error response for
359 struct RequestHandle *handle = cls;
360 struct MHD_Response *resp = GNUNET_REST_create_json_response (NULL);
362 handle->proc (handle->proc_cls, resp, handle->response_code);
363 cleanup_handle (handle);
368 * Task run on timeout.
370 * @param cls the request to time out
373 do_timeout (void *cls)
375 struct RequestHandle *handle = cls;
377 handle->timeout_task = NULL;
383 cleanup_handle_delayed (void *cls)
385 cleanup_handle (cls);
390 * Create a response with requested records
392 * @param handle the RequestHandle
395 namestore_list_response (void *cls,
396 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone_key,
399 const struct GNUNET_GNSRECORD_Data *rd)
401 struct RequestHandle *handle = cls;
402 struct GNUNET_JSONAPI_Resource *json_resource;
403 struct MHD_Response *resp;
404 json_t *result_array;
409 if (NULL == handle->resp_object)
410 handle->resp_object = GNUNET_JSONAPI_document_new ();
414 handle->list_it = NULL;
416 if (GNUNET_SYSERR == GNUNET_JSONAPI_document_serialize (handle->resp_object, &result))
418 GNUNET_SCHEDULER_add_now (&do_error, handle);
421 resp = GNUNET_REST_create_json_response (result);
422 handle->proc (handle->proc_cls, resp, MHD_HTTP_OK);
423 GNUNET_free (result);
424 GNUNET_SCHEDULER_add_now (&cleanup_handle_delayed, handle);
428 if ( (NULL != handle->name) &&
429 (0 != strcmp (handle->name, rname)) )
431 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
432 "%s does not match %s\n", rname, handle->name);
433 GNUNET_NAMESTORE_zone_iterator_next (handle->list_it);
437 result_array = json_array ();
438 for (i=0; i<rd_len; i++)
440 if ( (GNUNET_GNSRECORD_TYPE_NICK == rd[i].record_type) &&
441 (0 != strcmp (rname, "+")) )
444 if ( (rd[i].record_type != handle->type) &&
445 (GNUNET_GNSRECORD_TYPE_ANY != handle->type) )
447 record_obj = gnsrecord_to_json (&(rd[i]));
448 json_array_append (result_array, record_obj);
449 json_decref (record_obj);
452 if (0 < json_array_size(result_array))
454 json_resource = GNUNET_JSONAPI_resource_new (GNUNET_REST_JSONAPI_NAMESTORE_TYPEINFO,
456 GNUNET_JSONAPI_resource_add_attr (json_resource,
457 GNUNET_REST_JSONAPI_NAMESTORE_RECORD,
459 GNUNET_JSONAPI_document_resource_add (handle->resp_object, json_resource);
462 json_decref (result_array);
463 GNUNET_NAMESTORE_zone_iterator_next (handle->list_it);
467 create_finished (void *cls, int32_t success, const char *emsg)
469 struct RequestHandle *handle = cls;
470 struct MHD_Response *resp;
472 handle->add_qe = NULL;
473 if (GNUNET_YES != success)
475 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
476 "Error storing records%s%s\n",
477 (NULL == emsg) ? "" : ": ",
478 (NULL == emsg) ? "" : emsg);
479 GNUNET_SCHEDULER_add_now (&cleanup_handle_delayed, handle);
482 resp = GNUNET_REST_create_json_response (NULL);
483 handle->proc (handle->proc_cls, resp, MHD_HTTP_NO_CONTENT);
484 GNUNET_SCHEDULER_add_now (&cleanup_handle_delayed, handle);
489 * We're storing a new record; this requires
490 * that no record already exists
492 * @param cls closure, unused
493 * @param zone_key private key of the zone
494 * @param rec_name name that is being mapped (at most 255 characters long)
495 * @param rd_count number of entries in @a rd array
496 * @param rd array of records with data to store
499 create_new_record_cont (void *cls,
500 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone_key,
501 const char *rec_name,
502 unsigned int rd_count,
503 const struct GNUNET_GNSRECORD_Data *rd)
505 struct RequestHandle *handle = cls;
507 handle->add_qe = NULL;
508 if ( (NULL != zone_key) &&
509 (0 != strcmp (rec_name, handle->name)) )
512 GNUNET_SCHEDULER_add_now (&do_error, handle);
518 handle->proc (handle->proc_cls,
519 GNUNET_REST_create_json_response (NULL),
521 GNUNET_SCHEDULER_add_now (&cleanup_handle_delayed, handle);
525 GNUNET_assert (NULL != handle->name);
526 handle->add_qe = GNUNET_NAMESTORE_records_store (handle->ns_handle,
536 del_finished (void *cls,
540 struct RequestHandle *handle = cls;
542 handle->add_qe = NULL;
543 if (GNUNET_NO == success)
545 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
546 _("Deleting record failed, record does not exist%s%s\n"),
547 (NULL != emsg) ? ": " : "",
548 (NULL != emsg) ? emsg : "");
549 GNUNET_SCHEDULER_add_now (&do_error, handle); //do_not_found TODO
552 if (GNUNET_SYSERR == success)
554 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
555 _("Deleting record failed%s%s\n"),
556 (NULL != emsg) ? ": " : "",
557 (NULL != emsg) ? emsg : "");
558 GNUNET_SCHEDULER_add_now (&do_error, handle);
561 handle->proc (handle->proc_cls,
562 GNUNET_REST_create_json_response (NULL),
563 MHD_HTTP_NO_CONTENT);
564 GNUNET_SCHEDULER_add_now (&cleanup_handle_delayed, handle);
569 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
571 unsigned int rd_count,
572 const struct GNUNET_GNSRECORD_Data *rd)
574 struct RequestHandle *handle = cls;
575 handle->add_qe = NULL;
578 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
579 _("There are no records under label `%s' that could be deleted.\n"),
581 GNUNET_SCHEDULER_add_now (&do_error, handle);
585 handle->add_qe = GNUNET_NAMESTORE_records_store (handle->ns_handle,
594 namestore_delete_cont (struct GNUNET_REST_RequestHandle *con,
598 struct RequestHandle *handle = cls;
600 if (NULL == handle->name)
602 GNUNET_SCHEDULER_add_now (&do_error, handle);
606 handle->add_qe = GNUNET_NAMESTORE_records_lookup (handle->ns_handle,
614 json_to_gnsrecord (const json_t *records_json,
615 struct GNUNET_GNSRECORD_Data **rd,
616 unsigned int *rd_count)
618 struct GNUNET_TIME_Relative etime_rel;
619 struct GNUNET_TIME_Absolute etime_abs;
623 const char *typestring;
624 const char *expirationstring;
631 *rd_count = json_array_size (records_json);
632 *rd = GNUNET_malloc (sizeof (struct GNUNET_GNSRECORD_Data) * *rd_count);
633 for (i = 0; i < *rd_count; i++)
635 memset (&((*rd)[i]), 0, sizeof (struct GNUNET_GNSRECORD_Data));
636 record_json = json_array_get (records_json, i);
637 type_json = json_object_get (record_json,
638 GNUNET_REST_JSONAPI_NAMESTORE_RECORD_TYPE);
639 if (!json_is_string (type_json))
641 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
642 "Type property is no string\n");
643 return GNUNET_SYSERR;
645 typestring = json_string_value (type_json);
646 (*rd)[i].record_type = GNUNET_GNSRECORD_typename_to_number (typestring);
647 if (UINT32_MAX == (*rd)[i].record_type)
649 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Unsupported type `%s'\n"),
650 json_string_value (type_json));
651 return GNUNET_SYSERR;
653 value_json = json_object_get (record_json,
654 GNUNET_REST_JSONAPI_NAMESTORE_VALUE);
655 if (!json_is_string (value_json))
657 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
658 "Value property is no string\n");
659 return GNUNET_SYSERR;
661 value = GNUNET_strdup (json_string_value (value_json));
662 if (GNUNET_OK != GNUNET_GNSRECORD_string_to_value ((*rd)[i].record_type,
667 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Value `%s' invalid for record type `%s'\n"),
669 return GNUNET_SYSERR;
671 (*rd)[i].data = rdata;
672 (*rd)[i].data_size = rdata_size;
674 * if (1 == handle->is_shadow)
675 rde->flags |= GNUNET_GNSRECORD_RF_SHADOW_RECORD;
676 if (1 != handle->is_public)
677 rde->flags |= GNUNET_GNSRECORD_RF_PRIVATE;
679 exp_json = json_object_get (record_json,
680 GNUNET_REST_JSONAPI_NAMESTORE_EXPIRATION);
681 if (!json_is_string (exp_json))
683 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
684 "Expiration property is no string\n");
685 return GNUNET_SYSERR;
687 expirationstring = json_string_value (exp_json);
688 if (0 == strcmp (expirationstring, "never"))
690 (*rd)[i].expiration_time = GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us;
692 else if (GNUNET_OK ==
693 GNUNET_STRINGS_fancy_time_to_relative (expirationstring,
696 (*rd)[i].expiration_time = etime_rel.rel_value_us;
697 (*rd)[i].flags |= GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION;
699 else if (GNUNET_OK ==
700 GNUNET_STRINGS_fancy_time_to_absolute (expirationstring,
703 (*rd)[i].expiration_time = etime_abs.abs_value_us;
707 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Value `%s' invalid for record type `%s'\n"),
709 return GNUNET_SYSERR;
716 namestore_create_cont (struct GNUNET_REST_RequestHandle *con,
720 struct RequestHandle *handle = cls;
721 struct MHD_Response *resp;
722 struct GNUNET_JSONAPI_Document *json_obj;
723 struct GNUNET_JSONAPI_Resource *json_res;
724 json_t *records_json;
725 char term_data[handle->rest_handle->data_size+1];
727 if (strlen (GNUNET_REST_API_NS_NAMESTORE) != strlen (handle->url))
729 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
730 "Cannot create under %s\n", handle->url);
731 GNUNET_SCHEDULER_add_now (&do_error, handle);
734 if (0 >= handle->rest_handle->data_size)
736 GNUNET_SCHEDULER_add_now (&do_error, handle);
739 term_data[handle->rest_handle->data_size] = '\0';
741 handle->rest_handle->data,
742 handle->rest_handle->data_size);
743 GNUNET_assert (GNUNET_OK == GNUNET_JSONAPI_document_parse (term_data,
745 if (NULL == json_obj)
747 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
748 "Unable to parse JSONAPI Object from %s\n",
750 GNUNET_SCHEDULER_add_now (&do_error, handle);
753 if (1 != GNUNET_JSONAPI_document_resource_count (json_obj))
755 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
756 "Cannot create more than 1 resource! (Got %d)\n",
757 GNUNET_JSONAPI_document_resource_count (json_obj));
758 GNUNET_JSONAPI_document_delete (json_obj);
759 GNUNET_SCHEDULER_add_now (&do_error, handle);
762 json_res = GNUNET_JSONAPI_document_get_resource (json_obj, 0);
763 if (GNUNET_NO == GNUNET_JSONAPI_resource_check_type (json_res,
764 GNUNET_REST_JSONAPI_NAMESTORE_RECORD))
766 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
767 "Unsupported JSON data type\n");
768 GNUNET_JSONAPI_document_delete (json_obj);
769 resp = GNUNET_REST_create_json_response (NULL);
770 handle->proc (handle->proc_cls, resp, MHD_HTTP_CONFLICT);
771 cleanup_handle (handle);
774 handle->name = GNUNET_strdup (GNUNET_JSONAPI_resource_get_id (json_res));
775 records_json = GNUNET_JSONAPI_resource_read_attr (json_res,
776 GNUNET_REST_JSONAPI_NAMESTORE_RECORD);
777 if (NULL == records_json)
779 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
780 "No records given\n");
781 GNUNET_JSONAPI_document_delete (json_obj);
782 GNUNET_SCHEDULER_add_now (&do_error, handle);
785 if (GNUNET_SYSERR == json_to_gnsrecord (records_json, &handle->rd, &handle->rd_count))
787 GNUNET_JSONAPI_document_delete (json_obj);
788 GNUNET_SCHEDULER_add_now (&do_error, handle);
791 GNUNET_JSONAPI_document_delete (json_obj);
793 handle->add_qe = GNUNET_NAMESTORE_records_lookup (handle->ns_handle,
796 &create_new_record_cont, handle );
800 namestore_zkey_response (void *cls,
801 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
803 unsigned int rd_count,
804 const struct GNUNET_GNSRECORD_Data *rd)
806 struct RequestHandle *handle = cls;
807 struct MHD_Response *resp;
808 struct GNUNET_JSONAPI_Document *json_obj;
809 struct GNUNET_JSONAPI_Resource *json_res;
813 handle->reverse_qe = NULL;
814 json_obj = GNUNET_JSONAPI_document_new ();
817 name_json = json_string (label);
818 json_res = GNUNET_JSONAPI_resource_new (GNUNET_REST_JSONAPI_NAMESTORE_REVINFO,
820 GNUNET_JSONAPI_resource_add_attr (json_res,
821 GNUNET_REST_JSONAPI_NAMESTORE_NAME,
823 GNUNET_JSONAPI_document_resource_add (json_obj, json_res);
824 json_decref (name_json);
827 if (GNUNET_SYSERR == GNUNET_JSONAPI_document_serialize (json_obj, &result))
829 GNUNET_JSONAPI_document_delete (json_obj);
830 GNUNET_SCHEDULER_add_now (&do_error, handle);
833 resp = GNUNET_REST_create_json_response (result);
834 handle->proc (handle->proc_cls, resp, MHD_HTTP_OK);
835 GNUNET_JSONAPI_document_delete (json_obj);
836 GNUNET_free (result);
837 GNUNET_SCHEDULER_add_now (&cleanup_handle_delayed, handle);
843 namestore_zkey_cont (struct GNUNET_REST_RequestHandle *con,
847 struct RequestHandle *handle = cls;
848 struct GNUNET_HashCode key;
849 struct GNUNET_CRYPTO_EcdsaPublicKey pubkey;
851 GNUNET_CRYPTO_hash (GNUNET_REST_JSONAPI_NAMESTORE_ZKEY,
852 strlen (GNUNET_REST_JSONAPI_NAMESTORE_ZKEY),
855 GNUNET_CONTAINER_multihashmap_contains (handle->rest_handle->url_param_map,
858 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
859 "No zkey given %s\n", handle->url);
860 GNUNET_SCHEDULER_add_now (&do_error, handle);
863 handle->zkey_str = GNUNET_CONTAINER_multihashmap_get (handle->rest_handle->url_param_map,
866 GNUNET_CRYPTO_ecdsa_public_key_from_string (handle->zkey_str,
867 strlen (handle->zkey_str),
870 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
871 "Zkey invalid %s\n", handle->zkey_str);
872 GNUNET_SCHEDULER_add_now (&do_error, handle);
875 handle->reverse_qe = GNUNET_NAMESTORE_zone_to_name (handle->ns_handle,
878 &namestore_zkey_response,
883 namestore_info_cont (struct GNUNET_REST_RequestHandle *con,
887 struct RequestHandle *handle = cls;
888 handle->list_it = GNUNET_NAMESTORE_zone_iteration_start (handle->ns_handle,
890 &namestore_list_response,
895 get_name_from_url (const char* url)
897 if (strlen (url) <= strlen (GNUNET_REST_API_NS_NAMESTORE))
899 return (char*)url + strlen (GNUNET_REST_API_NS_NAMESTORE) + 1;
903 * Respond to OPTIONS request
905 * @param con_handle the connection handle
907 * @param cls the RequestHandle
910 options_cont (struct GNUNET_REST_RequestHandle *con_handle,
914 struct MHD_Response *resp;
915 struct RequestHandle *handle = cls;
917 //For now, independent of path return all options
918 resp = GNUNET_REST_create_json_response (NULL);
919 MHD_add_response_header (resp,
920 "Access-Control-Allow-Methods",
922 handle->proc (handle->proc_cls, resp, MHD_HTTP_OK);
923 cleanup_handle (handle);
928 * Function called with the result from the check if the namestore
929 * service is actually running. If it is, we start the actual
932 * @param cls closure with our configuration
933 * @param result #GNUNET_YES if the namestore service is running
936 testservice_task (void *cls,
939 struct RequestHandle *handle = cls;
940 struct GNUNET_REST_RequestHandlerError err;
941 static const struct GNUNET_REST_RequestHandler handlers[] = {
942 {MHD_HTTP_METHOD_GET, GNUNET_REST_API_NS_NAMESTORE_ZKEY, &namestore_zkey_cont}, //reverse
943 {MHD_HTTP_METHOD_GET, GNUNET_REST_API_NS_NAMESTORE, &namestore_info_cont}, //list
944 {MHD_HTTP_METHOD_POST, GNUNET_REST_API_NS_NAMESTORE, &namestore_create_cont}, //create
945 // {MHD_HTTP_METHOD_PUT, GNUNET_REST_API_NS_NAMESTORE, &namestore_edit_cont}, //update. TODO this shoul be PATCH
946 {MHD_HTTP_METHOD_DELETE, GNUNET_REST_API_NS_NAMESTORE, &namestore_delete_cont}, //delete
947 {MHD_HTTP_METHOD_OPTIONS, GNUNET_REST_API_NS_NAMESTORE, &options_cont},
948 GNUNET_REST_HANDLER_END
951 if (GNUNET_YES != result)
953 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Service `%s' is not running\n"),
955 GNUNET_SCHEDULER_add_now (&do_error, handle);
958 handle->ns_handle = GNUNET_NAMESTORE_connect (cfg);
959 if (NULL == handle->ns_handle)
961 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
962 _("Failed to connect to namestore\n"));
963 GNUNET_SCHEDULER_add_now (&do_error, handle);
967 if (GNUNET_OK != GNUNET_JSONAPI_handle_request (handle->rest_handle,
972 handle->response_code = err.error_code;
973 GNUNET_SCHEDULER_add_now (&do_error, (void*) handle);
979 * Callback invoked from identity service with ego information.
980 * An @a ego of NULL means the ego was not found.
982 * @param cls closure with the configuration
983 * @param ego an ego known to identity service, or NULL
986 identity_cb (void *cls,
987 const struct GNUNET_IDENTITY_Ego *ego)
989 struct RequestHandle *handle = cls;
990 struct MHD_Response *resp;
992 handle->ego_lookup = NULL;
995 if (NULL != handle->ego_name)
997 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
998 _("Ego `%s' not known to identity service\n"),
1001 resp = GNUNET_REST_create_json_response (NULL);
1002 handle->proc (handle->proc_cls, resp, MHD_HTTP_NOT_FOUND);
1003 cleanup_handle (handle);
1006 handle->zone_pkey = *GNUNET_IDENTITY_ego_get_private_key (ego);
1007 GNUNET_CLIENT_service_test ("namestore", handle->cfg,
1008 GNUNET_TIME_UNIT_SECONDS,
1014 default_ego_cb (void *cls,
1015 struct GNUNET_IDENTITY_Ego *ego,
1019 struct RequestHandle *handle = cls;
1020 struct MHD_Response *resp;
1021 handle->get_default = NULL;
1024 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1025 _("No default ego configured in identity service\n"));
1026 resp = GNUNET_REST_create_json_response (NULL);
1027 handle->proc (handle->proc_cls, resp, MHD_HTTP_NOT_FOUND);
1028 cleanup_handle (handle);
1033 identity_cb (cls, ego);
1038 id_connect_cb (void *cls,
1039 struct GNUNET_IDENTITY_Ego *ego,
1043 struct RequestHandle *handle = cls;
1046 handle->get_default = GNUNET_IDENTITY_get (handle->identity_handle,
1048 &default_ego_cb, handle);
1053 testservice_id_task (void *cls, int result)
1055 struct RequestHandle *handle = cls;
1056 struct MHD_Response *resp;
1057 struct GNUNET_HashCode key;
1062 if (result != GNUNET_YES)
1064 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1065 _("Identity service is not running\n"));
1066 resp = GNUNET_REST_create_json_response (NULL);
1067 handle->proc (handle->proc_cls, resp, MHD_HTTP_NOT_FOUND);
1068 cleanup_handle (handle);
1072 GNUNET_CRYPTO_hash (GNUNET_REST_JSONAPI_NAMESTORE_EGO,
1073 strlen (GNUNET_REST_JSONAPI_NAMESTORE_EGO),
1076 GNUNET_CONTAINER_multihashmap_contains (handle->rest_handle->url_param_map,
1079 ego = GNUNET_CONTAINER_multihashmap_get (handle->rest_handle->url_param_map,
1083 handle->type = GNUNET_GNSRECORD_TYPE_ANY;
1084 GNUNET_CRYPTO_hash (GNUNET_REST_JSONAPI_NAMESTORE_RECORD_TYPE,
1085 strlen (GNUNET_REST_JSONAPI_NAMESTORE_RECORD_TYPE),
1088 GNUNET_CONTAINER_multihashmap_contains (handle->rest_handle->url_param_map,
1091 type = GNUNET_CONTAINER_multihashmap_get (handle->rest_handle->url_param_map,
1094 handle->type = GNUNET_GNSRECORD_typename_to_number (type);
1096 name = get_name_from_url (handle->url);
1098 handle->ego_name = GNUNET_strdup (ego);
1100 handle->name = GNUNET_strdup (name);
1101 if (NULL == handle->ego_name)
1103 handle->identity_handle = GNUNET_IDENTITY_connect (handle->cfg, &id_connect_cb, handle);
1104 if (NULL == handle->identity_handle)
1106 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Cannot connect to identity service\n"));
1107 resp = GNUNET_REST_create_json_response (NULL);
1108 handle->proc (handle->proc_cls, resp, MHD_HTTP_NOT_FOUND);
1109 cleanup_handle (handle);
1113 handle->ego_lookup = GNUNET_IDENTITY_ego_lookup (cfg,
1120 * Function processing the REST call
1122 * @param method HTTP method
1123 * @param url URL of the HTTP request
1124 * @param data body of the HTTP request (optional)
1125 * @param data_size length of the body
1126 * @param proc callback function for the result
1127 * @param proc_cls closure for callback function
1128 * @return GNUNET_OK if request accepted
1131 rest_identity_process_request(struct GNUNET_REST_RequestHandle *rest_handle,
1132 GNUNET_REST_ResultProcessor proc,
1135 struct RequestHandle *handle = GNUNET_new (struct RequestHandle);
1137 handle->timeout = GNUNET_TIME_UNIT_FOREVER_REL;
1138 handle->proc_cls = proc_cls;
1139 handle->proc = proc;
1140 handle->rest_handle = rest_handle;
1141 handle->url = GNUNET_strdup (rest_handle->url);
1142 if (handle->url[strlen (handle->url)-1] == '/')
1143 handle->url[strlen (handle->url)-1] = '\0';
1144 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1147 GNUNET_CLIENT_service_test ("identity",
1149 GNUNET_TIME_UNIT_SECONDS,
1150 &testservice_id_task,
1152 handle->timeout_task = GNUNET_SCHEDULER_add_delayed (handle->timeout,
1158 * Entry point for the plugin.
1160 * @param cls Config info
1161 * @return NULL on error, otherwise the plugin context
1164 libgnunet_plugin_rest_namestore_init (void *cls)
1166 static struct Plugin plugin;
1168 struct GNUNET_REST_Plugin *api;
1170 if (NULL != plugin.cfg)
1171 return NULL; /* can only initialize once! */
1172 memset (&plugin, 0, sizeof (struct Plugin));
1174 api = GNUNET_new (struct GNUNET_REST_Plugin);
1176 api->name = GNUNET_REST_API_NS_NAMESTORE;
1177 api->process_request = &rest_identity_process_request;
1178 GNUNET_asprintf (&allow_methods,
1179 "%s, %s, %s, %s, %s",
1180 MHD_HTTP_METHOD_GET,
1181 MHD_HTTP_METHOD_POST,
1182 MHD_HTTP_METHOD_PUT,
1183 MHD_HTTP_METHOD_DELETE,
1184 MHD_HTTP_METHOD_OPTIONS);
1185 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1186 _("Namestore REST API initialized\n"));
1192 * Exit point from the plugin.
1194 * @param cls the plugin context (as returned by "init")
1195 * @return always NULL
1198 libgnunet_plugin_rest_namestore_done (void *cls)
1200 struct GNUNET_REST_Plugin *api = cls;
1201 struct Plugin *plugin = api->cls;
1205 GNUNET_free_non_null (allow_methods);
1206 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1207 "Namestore REST plugin is finished\n");
1211 /* end of plugin_rest_namestore.c */