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 gns/plugin_rest_gns.c
23 * @brief GNUnet GNS REST plugin
28 #include "gnunet_rest_plugin.h"
29 #include <gnunet_dnsparser_lib.h>
30 #include <gnunet_identity_service.h>
31 #include <gnunet_gnsrecord_lib.h>
32 #include <gnunet_namestore_service.h>
33 #include <gnunet_gns_service.h>
34 #include <gnunet_rest_lib.h>
35 #include <gnunet_jsonapi_lib.h>
36 #include <gnunet_jsonapi_util.h>
39 #define GNUNET_REST_API_NS_GNS "/gns"
41 #define GNUNET_REST_JSONAPI_GNS_RECORD_TYPE "record_type"
43 #define GNUNET_REST_JSONAPI_GNS_TYPEINFO "gns_name"
45 #define GNUNET_REST_JSONAPI_GNS_RECORD "records"
47 #define GNUNET_REST_JSONAPI_GNS_EGO "ego"
49 #define GNUNET_REST_JSONAPI_GNS_PKEY "pkey"
51 #define GNUNET_REST_JSONAPI_GNS_OPTIONS "options"
54 * @brief struct returned by the initialization function of the plugin
58 const struct GNUNET_CONFIGURATION_Handle *cfg;
61 const struct GNUNET_CONFIGURATION_Handle *cfg;
66 * Handle to GNS service.
68 struct GNUNET_GNS_Handle *gns;
71 * Desired timeout for the lookup (default is no timeout).
73 struct GNUNET_TIME_Relative timeout;
76 * Handle to lookup request
78 struct GNUNET_GNS_LookupRequest *lookup_request;
81 * Handle to rest request
83 struct GNUNET_REST_RequestHandle *rest_handle;
86 * Lookup an ego with the identity service.
88 struct GNUNET_IDENTITY_EgoLookup *el;
91 * Handle for identity service.
93 struct GNUNET_IDENTITY_Handle *identity;
96 * Active operation on identity service.
98 struct GNUNET_IDENTITY_Operation *id_op;
101 * ID of a task associated with the resolution process.
103 struct GNUNET_SCHEDULER_Task * timeout_task;
106 * The root of the received JSON or NULL
111 * The plugin result processor
113 GNUNET_REST_ResultProcessor proc;
116 * The closure of the result processor
121 * The name to look up
127 * In string representation from JSON
133 * In string representation from JSON
135 const char *pkey_str;
143 * The public key of to use for lookup
145 struct GNUNET_CRYPTO_EcdsaPublicKey pkey;
148 * The public key to use for lookup
150 struct GNUNET_CRYPTO_EcdsaPublicKey pkeym;
153 * The resolver options
155 enum GNUNET_GNS_LocalOptions options;
160 struct GNUNET_CRYPTO_EcdsaPrivateKey shorten_key;
171 * Cleanup lookup handle.
173 * @param handle Handle to clean up
176 cleanup_handle (struct LookupHandle *handle)
178 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
180 if (NULL != handle->json_root)
181 json_decref (handle->json_root);
183 if (NULL != handle->name)
184 GNUNET_free (handle->name);
185 if (NULL != handle->el)
187 GNUNET_IDENTITY_ego_lookup_cancel (handle->el);
190 if (NULL != handle->id_op)
192 GNUNET_IDENTITY_cancel (handle->id_op);
193 handle->id_op = NULL;
195 if (NULL != handle->lookup_request)
197 GNUNET_GNS_lookup_cancel (handle->lookup_request);
198 handle->lookup_request = NULL;
200 if (NULL != handle->identity)
202 GNUNET_IDENTITY_disconnect (handle->identity);
203 handle->identity = NULL;
205 if (NULL != handle->gns)
207 GNUNET_GNS_disconnect (handle->gns);
211 if (NULL != handle->timeout_task)
213 GNUNET_SCHEDULER_cancel (handle->timeout_task);
215 GNUNET_free (handle);
220 * Task run on shutdown. Cleans up everything.
223 * @param tc scheduler context
228 struct LookupHandle *handle = cls;
229 struct MHD_Response *resp;
231 resp = GNUNET_REST_create_response (NULL);
232 handle->proc (handle->proc_cls, resp, handle->response_code);
233 cleanup_handle (handle);
238 * Create json representation of a GNSRECORD
240 * @param rd the GNSRECORD_Data
243 gnsrecord_to_json (const struct GNUNET_GNSRECORD_Data *rd)
245 const char *typename;
250 typename = GNUNET_GNSRECORD_number_to_typename (rd->record_type);
251 string_val = GNUNET_GNSRECORD_value_to_string (rd->record_type,
255 if (NULL == string_val)
257 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
258 "Record of type %d malformed, skipping\n",
259 (int) rd->record_type);
262 record_obj = json_object ();
263 json_object_set_new (record_obj, "type", json_string (typename));
264 json_object_set_new (record_obj, "value", json_string (string_val));
265 GNUNET_free (string_val);
267 if (GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION & rd->flags)
269 struct GNUNET_TIME_Relative time_rel;
270 time_rel.rel_value_us = rd->expiration_time;
271 exp_str = GNUNET_STRINGS_relative_time_to_string (time_rel, 1);
275 struct GNUNET_TIME_Absolute time_abs;
276 time_abs.abs_value_us = rd->expiration_time;
277 exp_str = GNUNET_STRINGS_absolute_time_to_string (time_abs);
279 json_object_set_new (record_obj, "expiration_time", json_string (exp_str));
281 json_object_set_new (record_obj, "expired",
282 json_boolean (GNUNET_YES == GNUNET_GNSRECORD_is_expired (rd)));
287 * Function called with the result of a GNS lookup.
289 * @param cls the 'const char *' name that was resolved
290 * @param rd_count number of records returned
291 * @param rd array of @a rd_count records with the results
294 process_lookup_result (void *cls, uint32_t rd_count,
295 const struct GNUNET_GNSRECORD_Data *rd)
297 struct LookupHandle *handle = cls;
298 struct MHD_Response *resp;
299 struct GNUNET_JSONAPI_Document *json_document;
300 struct GNUNET_JSONAPI_Resource *json_resource;
303 json_t *result_array;
306 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;
310 for (i=0; i<rd_count; i++)
312 if ( (rd[i].record_type != handle->type) &&
313 (GNUNET_GNSRECORD_TYPE_ANY != handle->type) )
315 record_obj = gnsrecord_to_json (&(rd[i]));
316 json_array_append (result_array, record_obj);
317 json_decref (record_obj);
319 GNUNET_JSONAPI_resource_add_attr (json_resource,
320 GNUNET_REST_JSONAPI_GNS_RECORD,
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);
325 json_decref (result_array);
326 GNUNET_JSONAPI_document_delete (json_document);
327 resp = GNUNET_REST_create_response (result);
328 handle->proc (handle->proc_cls, resp, MHD_HTTP_OK);
329 GNUNET_free (result);
330 cleanup_handle (handle);
335 * Perform the actual resolution, starting with the zone
336 * identified by the given public key and the shorten zone.
338 * @param pkey public key to use for the zone, can be NULL
339 * @param shorten_key private key used for shortening, can be NULL
342 lookup_with_keys (struct LookupHandle *handle, const struct GNUNET_CRYPTO_EcdsaPrivateKey *shorten_key)
344 if (UINT32_MAX == handle->type)
346 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
347 _("Invalid typename specified, assuming `ANY'\n"));
348 handle->type = GNUNET_GNSRECORD_TYPE_ANY;
350 if (NULL != handle->name)
352 handle->lookup_request = GNUNET_GNS_lookup (handle->gns,
358 &process_lookup_result,
363 GNUNET_SCHEDULER_add_now (&do_error, handle);
369 * Method called to with the ego we are to use for shortening
372 * @param cls closure contains the public key to use
373 * @param ego ego handle, NULL if not found
374 * @param ctx context for application to store data for this ego
375 * (during the lifetime of this process, initially NULL)
376 * @param name name assigned by the user for this ego,
377 * NULL if the user just deleted the ego and it
378 * must thus no longer be used
381 identity_shorten_cb (void *cls,
382 struct GNUNET_IDENTITY_Ego *ego,
386 struct LookupHandle *handle = cls;
388 handle->id_op = NULL;
390 lookup_with_keys (handle, NULL);
392 lookup_with_keys (handle,
393 GNUNET_IDENTITY_ego_get_private_key (ego));
397 * Perform the actual resolution, starting with the zone
398 * identified by the given public key.
400 * @param pkey public key to use for the zone
403 lookup_with_public_key (struct LookupHandle *handle)
405 handle->pkeym = handle->pkey;
406 GNUNET_break (NULL == handle->id_op);
407 handle->id_op = GNUNET_IDENTITY_get (handle->identity,
409 &identity_shorten_cb,
411 if (NULL == handle->id_op)
414 lookup_with_keys (handle, NULL);
419 * Method called to with the ego we are to use for the lookup,
420 * when the ego is determined by a name.
422 * @param cls closure (NULL, unused)
423 * @param ego ego handle, NULL if not found
426 identity_zone_cb (void *cls,
427 const struct GNUNET_IDENTITY_Ego *ego)
429 struct LookupHandle *handle = cls;
434 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
435 _("Ego for not found, cannot perform lookup.\n"));
436 GNUNET_SCHEDULER_add_now (&do_error, handle);
441 GNUNET_IDENTITY_ego_get_public_key (ego, &handle->pkey);
442 lookup_with_public_key (handle);
444 json_decref(handle->json_root);
448 * Method called to with the ego we are to use for the lookup,
449 * when the ego is the one for the default master zone.
451 * @param cls closure (NULL, unused)
452 * @param ego ego handle, NULL if not found
453 * @param ctx context for application to store data for this ego
454 * (during the lifetime of this process, initially NULL)
455 * @param name name assigned by the user for this ego,
456 * NULL if the user just deleted the ego and it
457 * must thus no longer be used
460 identity_master_cb (void *cls,
461 struct GNUNET_IDENTITY_Ego *ego,
466 struct LookupHandle *handle = cls;
468 handle->id_op = NULL;
471 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
472 _("Ego for `gns-master' not found, cannot perform lookup. Did you run gnunet-gns-import.sh?\n"));
473 GNUNET_SCHEDULER_add_now (&do_error, handle);
476 GNUNET_IDENTITY_ego_get_public_key (ego, &handle->pkey);
477 /* main name is our own master zone, do no look for that in the DHT */
478 handle->options = GNUNET_GNS_LO_LOCAL_MASTER;
479 /* if the name is of the form 'label.gnu', never go to the DHT */
481 if (NULL != handle->name)
482 dot = strchr (handle->name, '.');
483 if ( (NULL != dot) &&
484 (0 == strcasecmp (dot, ".gnu")) )
485 handle->options = GNUNET_GNS_LO_NO_DHT;
486 lookup_with_public_key (handle);
490 * Parse REST uri for name and record type
492 * @param url Url to parse
493 * @param handle lookup handle to populate
494 * @return GNUNET_SYSERR on error
497 parse_url (const char *url, struct LookupHandle *handle)
500 char tmp_url[strlen(url)+1];
503 strcpy (tmp_url, url);
504 tok = strtok ((char*)tmp_url, "/");
506 return GNUNET_SYSERR;
507 name = strtok (NULL, "/");
509 return GNUNET_SYSERR;
510 GNUNET_asprintf (&handle->name,
513 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
514 "Got name: %s\n", handle->name);
519 get_gns_cont (struct GNUNET_REST_RequestHandle *conndata_handle,
523 struct LookupHandle *handle = cls;
524 struct GNUNET_HashCode key;
526 //parse name and type from url
527 if (GNUNET_OK != parse_url (url, handle))
529 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Error parsing url...\n");
530 GNUNET_SCHEDULER_add_now (&do_error, handle);
533 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
535 handle->gns = GNUNET_GNS_connect (cfg);
536 handle->identity = GNUNET_IDENTITY_connect (cfg, NULL, NULL);
537 handle->timeout_task = GNUNET_SCHEDULER_add_delayed (handle->timeout,
539 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
541 if (NULL == handle->gns)
543 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
544 "Connecting to GNS failed\n");
545 GNUNET_SCHEDULER_add_now (&do_error, handle);
548 GNUNET_CRYPTO_hash (GNUNET_REST_JSONAPI_GNS_OPTIONS,
549 strlen (GNUNET_REST_JSONAPI_GNS_OPTIONS),
551 handle->options = GNUNET_GNS_LO_DEFAULT;
553 GNUNET_CONTAINER_multihashmap_contains (conndata_handle->url_param_map,
556 handle->options = GNUNET_GNS_LO_DEFAULT;//TODO(char*) GNUNET_CONTAINER_multihashmap_get (conndata_handle->url_param_map,
559 GNUNET_CRYPTO_hash (GNUNET_REST_JSONAPI_GNS_RECORD_TYPE,
560 strlen (GNUNET_REST_JSONAPI_GNS_RECORD_TYPE),
563 GNUNET_CONTAINER_multihashmap_contains (conndata_handle->url_param_map,
566 handle->type = GNUNET_GNSRECORD_typename_to_number
567 (GNUNET_CONTAINER_multihashmap_get (conndata_handle->url_param_map,
571 handle->type = GNUNET_GNSRECORD_TYPE_ANY;
573 GNUNET_CRYPTO_hash (GNUNET_REST_JSONAPI_GNS_PKEY,
574 strlen (GNUNET_REST_JSONAPI_GNS_PKEY),
577 GNUNET_CONTAINER_multihashmap_contains (conndata_handle->url_param_map,
580 handle->pkey_str = GNUNET_CONTAINER_multihashmap_get (conndata_handle->url_param_map,
583 GNUNET_CRYPTO_ecdsa_public_key_from_string (handle->pkey_str,
584 strlen(handle->pkey_str),
587 GNUNET_SCHEDULER_add_now (&do_error, handle);
590 lookup_with_public_key (handle);
593 GNUNET_CRYPTO_hash (GNUNET_REST_JSONAPI_GNS_EGO,
594 strlen (GNUNET_REST_JSONAPI_GNS_EGO),
597 GNUNET_CONTAINER_multihashmap_contains (conndata_handle->url_param_map,
600 handle->ego_str = GNUNET_CONTAINER_multihashmap_get (conndata_handle->url_param_map,
602 handle->el = GNUNET_IDENTITY_ego_lookup (cfg,
608 if ( (NULL != handle->name) &&
609 (strlen (handle->name) > 4) &&
610 (0 == strcmp (".zkey",
611 &handle->name[strlen (handle->name) - 4])) )
613 GNUNET_CRYPTO_ecdsa_key_get_public
614 (GNUNET_CRYPTO_ecdsa_key_get_anonymous (),
616 lookup_with_public_key (handle);
620 GNUNET_break (NULL == handle->id_op);
621 handle->id_op = GNUNET_IDENTITY_get (handle->identity,
625 GNUNET_assert (NULL != handle->id_op);
630 * Handle rest request
632 * @param handle the lookup handle
635 options_cont (struct GNUNET_REST_RequestHandle *con_handle,
639 struct MHD_Response *resp;
640 struct LookupHandle *handle = cls;
642 //For GNS, independent of path return all options
643 resp = GNUNET_REST_create_response (NULL);
644 MHD_add_response_header (resp,
645 "Access-Control-Allow-Methods",
646 MHD_HTTP_METHOD_GET);
647 handle->proc (handle->proc_cls,
650 cleanup_handle (handle);
655 * Function processing the REST call
657 * @param method HTTP method
658 * @param url URL of the HTTP request
659 * @param data body of the HTTP request (optional)
660 * @param data_size length of the body
661 * @param proc callback function for the result
662 * @param proc_cls closure for callback function
663 * @return GNUNET_OK if request accepted
666 rest_gns_process_request(struct GNUNET_REST_RequestHandle *conndata_handle,
667 GNUNET_REST_ResultProcessor proc,
670 struct LookupHandle *handle = GNUNET_new (struct LookupHandle);
671 struct GNUNET_REST_RequestHandlerError err;
673 handle->timeout = GNUNET_TIME_UNIT_FOREVER_REL;
674 handle->proc_cls = proc_cls;
676 handle->rest_handle = conndata_handle;
678 static const struct GNUNET_REST_RequestHandler handlers[] = {
679 {MHD_HTTP_METHOD_GET, GNUNET_REST_API_NS_GNS, &get_gns_cont},
680 {MHD_HTTP_METHOD_OPTIONS, GNUNET_REST_API_NS_GNS, &options_cont},
681 GNUNET_REST_HANDLER_END
684 if (GNUNET_NO == GNUNET_JSONAPI_handle_request (conndata_handle,
689 handle->response_code = err.error_code;
690 GNUNET_SCHEDULER_add_now (&do_error, handle);
696 * Entry point for the plugin.
698 * @param cls the "struct GNUNET_NAMESTORE_PluginEnvironment*"
699 * @return NULL on error, otherwise the plugin context
702 libgnunet_plugin_rest_gns_init (void *cls)
704 static struct Plugin plugin;
706 struct GNUNET_REST_Plugin *api;
708 if (NULL != plugin.cfg)
709 return NULL; /* can only initialize once! */
710 memset (&plugin, 0, sizeof (struct Plugin));
712 api = GNUNET_new (struct GNUNET_REST_Plugin);
714 api->name = GNUNET_REST_API_NS_GNS;
715 api->process_request = &rest_gns_process_request;
716 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
717 _("GNS REST API initialized\n"));
723 * Exit point from the plugin.
725 * @param cls the plugin context (as returned by "init")
726 * @return always NULL
729 libgnunet_plugin_rest_gns_done (void *cls)
731 struct GNUNET_REST_Plugin *api = cls;
732 struct Plugin *plugin = api->cls;
736 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
737 "GNS REST plugin is finished\n");
741 /* end of plugin_rest_gns.c */