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 it
6 under the terms of the GNU General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your 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 Affero General Public License for more details.
16 * @author Martin Schanzenbach
17 * @file gns/plugin_rest_gns.c
18 * @brief GNUnet GNS REST plugin
23 #include "gnunet_rest_plugin.h"
24 #include <gnunet_dnsparser_lib.h>
25 #include <gnunet_identity_service.h>
26 #include <gnunet_gnsrecord_lib.h>
27 #include <gnunet_namestore_service.h>
28 #include <gnunet_gns_service.h>
29 #include <gnunet_rest_lib.h>
30 #include <gnunet_jsonapi_lib.h>
31 #include <gnunet_jsonapi_util.h>
34 #define GNUNET_REST_API_NS_GNS "/gns"
36 #define GNUNET_REST_JSONAPI_GNS_RECORD_TYPE "record_type"
38 #define GNUNET_REST_JSONAPI_GNS_TYPEINFO "gns_name"
40 #define GNUNET_REST_JSONAPI_GNS_RECORD "records"
42 #define GNUNET_REST_JSONAPI_GNS_EGO "ego"
44 #define GNUNET_REST_JSONAPI_GNS_PKEY "pkey"
46 #define GNUNET_REST_JSONAPI_GNS_OPTIONS "options"
49 * @brief struct returned by the initialization function of the plugin
53 const struct GNUNET_CONFIGURATION_Handle *cfg;
56 const struct GNUNET_CONFIGURATION_Handle *cfg;
61 * Handle to GNS service.
63 struct GNUNET_GNS_Handle *gns;
66 * Desired timeout for the lookup (default is no timeout).
68 struct GNUNET_TIME_Relative timeout;
71 * Handle to lookup request
73 struct GNUNET_GNS_LookupRequest *lookup_request;
76 * Handle to rest request
78 struct GNUNET_REST_RequestHandle *rest_handle;
81 * Lookup an ego with the identity service.
83 struct GNUNET_IDENTITY_EgoLookup *el;
86 * Handle for identity service.
88 struct GNUNET_IDENTITY_Handle *identity;
91 * Active operation on identity service.
93 struct GNUNET_IDENTITY_Operation *id_op;
96 * ID of a task associated with the resolution process.
98 struct GNUNET_SCHEDULER_Task * timeout_task;
101 * The root of the received JSON or NULL
106 * The plugin result processor
108 GNUNET_REST_ResultProcessor proc;
111 * The closure of the result processor
116 * The name to look up
122 * In string representation from JSON
128 * In string representation from JSON
130 const char *pkey_str;
138 * The public key of to use for lookup
140 struct GNUNET_CRYPTO_EcdsaPublicKey pkey;
143 * The public key to use for lookup
145 struct GNUNET_CRYPTO_EcdsaPublicKey pkeym;
148 * The resolver options
150 enum GNUNET_GNS_LocalOptions options;
155 struct GNUNET_CRYPTO_EcdsaPrivateKey shorten_key;
166 * Cleanup lookup handle.
168 * @param handle Handle to clean up
171 cleanup_handle (struct LookupHandle *handle)
173 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
175 if (NULL != handle->json_root)
176 json_decref (handle->json_root);
178 if (NULL != handle->name)
179 GNUNET_free (handle->name);
180 if (NULL != handle->el)
182 GNUNET_IDENTITY_ego_lookup_cancel (handle->el);
185 if (NULL != handle->id_op)
187 GNUNET_IDENTITY_cancel (handle->id_op);
188 handle->id_op = NULL;
190 if (NULL != handle->lookup_request)
192 GNUNET_GNS_lookup_cancel (handle->lookup_request);
193 handle->lookup_request = NULL;
195 if (NULL != handle->identity)
197 GNUNET_IDENTITY_disconnect (handle->identity);
198 handle->identity = NULL;
200 if (NULL != handle->gns)
202 GNUNET_GNS_disconnect (handle->gns);
206 if (NULL != handle->timeout_task)
208 GNUNET_SCHEDULER_cancel (handle->timeout_task);
210 GNUNET_free (handle);
215 * Task run on shutdown. Cleans up everything.
218 * @param tc scheduler context
223 struct LookupHandle *handle = cls;
224 struct MHD_Response *resp;
226 resp = GNUNET_REST_create_response (NULL);
227 handle->proc (handle->proc_cls, resp, handle->response_code);
228 cleanup_handle (handle);
233 * Create json representation of a GNSRECORD
235 * @param rd the GNSRECORD_Data
238 gnsrecord_to_json (const struct GNUNET_GNSRECORD_Data *rd)
240 const char *typename;
245 typename = GNUNET_GNSRECORD_number_to_typename (rd->record_type);
246 string_val = GNUNET_GNSRECORD_value_to_string (rd->record_type,
250 if (NULL == string_val)
252 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
253 "Record of type %d malformed, skipping\n",
254 (int) rd->record_type);
257 record_obj = json_object ();
258 json_object_set_new (record_obj, "type", json_string (typename));
259 json_object_set_new (record_obj, "value", json_string (string_val));
260 GNUNET_free (string_val);
262 if (GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION & rd->flags)
264 struct GNUNET_TIME_Relative time_rel;
265 time_rel.rel_value_us = rd->expiration_time;
266 exp_str = GNUNET_STRINGS_relative_time_to_string (time_rel, 1);
270 struct GNUNET_TIME_Absolute time_abs;
271 time_abs.abs_value_us = rd->expiration_time;
272 exp_str = GNUNET_STRINGS_absolute_time_to_string (time_abs);
274 json_object_set_new (record_obj, "expiration_time", json_string (exp_str));
276 json_object_set_new (record_obj, "expired",
277 json_boolean (GNUNET_YES == GNUNET_GNSRECORD_is_expired (rd)));
282 * Function called with the result of a GNS lookup.
284 * @param cls the 'const char *' name that was resolved
285 * @param rd_count number of records returned
286 * @param rd array of @a rd_count records with the results
289 process_lookup_result (void *cls, uint32_t rd_count,
290 const struct GNUNET_GNSRECORD_Data *rd)
292 struct LookupHandle *handle = cls;
293 struct MHD_Response *resp;
294 struct GNUNET_JSONAPI_Document *json_document;
295 struct GNUNET_JSONAPI_Resource *json_resource;
298 json_t *result_array;
301 result_array = json_array();
302 json_document = GNUNET_JSONAPI_document_new ();
303 json_resource = GNUNET_JSONAPI_resource_new (GNUNET_REST_JSONAPI_GNS_TYPEINFO, handle->name);
304 handle->lookup_request = NULL;
305 for (i=0; i<rd_count; i++)
307 if ( (rd[i].record_type != handle->type) &&
308 (GNUNET_GNSRECORD_TYPE_ANY != handle->type) )
310 record_obj = gnsrecord_to_json (&(rd[i]));
311 json_array_append (result_array, record_obj);
312 json_decref (record_obj);
314 GNUNET_JSONAPI_resource_add_attr (json_resource,
315 GNUNET_REST_JSONAPI_GNS_RECORD,
317 GNUNET_JSONAPI_document_resource_add (json_document, json_resource);
318 GNUNET_JSONAPI_document_serialize (json_document, &result);
319 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Result %s\n", result);
320 json_decref (result_array);
321 GNUNET_JSONAPI_document_delete (json_document);
322 resp = GNUNET_REST_create_response (result);
323 handle->proc (handle->proc_cls, resp, MHD_HTTP_OK);
324 GNUNET_free (result);
325 cleanup_handle (handle);
330 * Perform the actual resolution, starting with the zone
331 * identified by the given public key and the shorten zone.
333 * @param pkey public key to use for the zone, can be NULL
336 lookup_with_public_key (struct LookupHandle *handle)
338 if (UINT32_MAX == handle->type)
340 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
341 _("Invalid typename specified, assuming `ANY'\n"));
342 handle->type = GNUNET_GNSRECORD_TYPE_ANY;
344 if (NULL != handle->name)
346 handle->lookup_request = GNUNET_GNS_lookup (handle->gns,
351 &process_lookup_result,
356 GNUNET_SCHEDULER_add_now (&do_error, handle);
363 * Method called to with the ego we are to use for the lookup,
364 * when the ego is determined by a name.
366 * @param cls closure (NULL, unused)
367 * @param ego ego handle, NULL if not found
370 identity_zone_cb (void *cls,
371 const struct GNUNET_IDENTITY_Ego *ego)
373 struct LookupHandle *handle = cls;
378 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
379 _("Ego for not found, cannot perform lookup.\n"));
380 GNUNET_SCHEDULER_add_now (&do_error, handle);
385 GNUNET_IDENTITY_ego_get_public_key (ego, &handle->pkey);
386 lookup_with_public_key (handle);
388 json_decref(handle->json_root);
393 * Method called to with the ego we are to use for the lookup,
394 * when the ego is the one for the default master zone.
396 * @param cls closure (NULL, unused)
397 * @param ego ego handle, NULL if not found
398 * @param ctx context for application to store data for this ego
399 * (during the lifetime of this process, initially NULL)
400 * @param name name assigned by the user for this ego,
401 * NULL if the user just deleted the ego and it
402 * must thus no longer be used
405 identity_master_cb (void *cls,
406 struct GNUNET_IDENTITY_Ego *ego,
411 struct LookupHandle *handle = cls;
413 handle->id_op = NULL;
416 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
417 _("Ego for `gns-master' not found, cannot perform lookup. Did you run gnunet-gns-import.sh?\n"));
418 GNUNET_SCHEDULER_add_now (&do_error, handle);
421 GNUNET_IDENTITY_ego_get_public_key (ego,
423 /* main name is our own master zone, do no look for that in the DHT */
424 handle->options = GNUNET_GNS_LO_LOCAL_MASTER;
425 /* if the name is of the form 'label.gnu', never go to the DHT */
427 if (NULL != handle->name)
428 dot = strchr (handle->name, '.');
429 if ( (NULL != dot) &&
430 (0 == strcasecmp (dot, ".gnu")) )
431 handle->options = GNUNET_GNS_LO_NO_DHT;
432 lookup_with_public_key (handle);
436 * Parse REST uri for name and record type
438 * @param url Url to parse
439 * @param handle lookup handle to populate
440 * @return GNUNET_SYSERR on error
443 parse_url (const char *url, struct LookupHandle *handle)
446 char tmp_url[strlen(url)+1];
449 strcpy (tmp_url, url);
450 tok = strtok ((char*)tmp_url, "/");
452 return GNUNET_SYSERR;
453 name = strtok (NULL, "/");
455 return GNUNET_SYSERR;
456 GNUNET_asprintf (&handle->name,
459 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
460 "Got name: %s\n", handle->name);
466 get_gns_cont (struct GNUNET_REST_RequestHandle *conndata_handle,
470 struct LookupHandle *handle = cls;
471 struct GNUNET_HashCode key;
473 //parse name and type from url
474 if (GNUNET_OK != parse_url (url, handle))
476 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Error parsing url...\n");
477 GNUNET_SCHEDULER_add_now (&do_error, handle);
480 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
482 handle->gns = GNUNET_GNS_connect (cfg);
483 handle->identity = GNUNET_IDENTITY_connect (cfg, NULL, NULL);
484 handle->timeout_task = GNUNET_SCHEDULER_add_delayed (handle->timeout,
486 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
488 if (NULL == handle->gns)
490 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
491 "Connecting to GNS failed\n");
492 GNUNET_SCHEDULER_add_now (&do_error, handle);
495 GNUNET_CRYPTO_hash (GNUNET_REST_JSONAPI_GNS_OPTIONS,
496 strlen (GNUNET_REST_JSONAPI_GNS_OPTIONS),
498 handle->options = GNUNET_GNS_LO_DEFAULT;
500 GNUNET_CONTAINER_multihashmap_contains (conndata_handle->url_param_map,
503 handle->options = GNUNET_GNS_LO_DEFAULT;//TODO(char*) GNUNET_CONTAINER_multihashmap_get (conndata_handle->url_param_map,
506 GNUNET_CRYPTO_hash (GNUNET_REST_JSONAPI_GNS_RECORD_TYPE,
507 strlen (GNUNET_REST_JSONAPI_GNS_RECORD_TYPE),
510 GNUNET_CONTAINER_multihashmap_contains (conndata_handle->url_param_map,
513 handle->type = GNUNET_GNSRECORD_typename_to_number
514 (GNUNET_CONTAINER_multihashmap_get (conndata_handle->url_param_map,
518 handle->type = GNUNET_GNSRECORD_TYPE_ANY;
520 GNUNET_CRYPTO_hash (GNUNET_REST_JSONAPI_GNS_PKEY,
521 strlen (GNUNET_REST_JSONAPI_GNS_PKEY),
524 GNUNET_CONTAINER_multihashmap_contains (conndata_handle->url_param_map,
527 handle->pkey_str = GNUNET_CONTAINER_multihashmap_get (conndata_handle->url_param_map,
529 GNUNET_assert (NULL != handle->pkey_str);
531 GNUNET_CRYPTO_ecdsa_public_key_from_string (handle->pkey_str,
532 strlen(handle->pkey_str),
535 GNUNET_SCHEDULER_add_now (&do_error, handle);
538 lookup_with_public_key (handle);
541 GNUNET_CRYPTO_hash (GNUNET_REST_JSONAPI_GNS_EGO,
542 strlen (GNUNET_REST_JSONAPI_GNS_EGO),
545 GNUNET_CONTAINER_multihashmap_contains (conndata_handle->url_param_map,
548 handle->ego_str = GNUNET_CONTAINER_multihashmap_get (conndata_handle->url_param_map,
550 handle->el = GNUNET_IDENTITY_ego_lookup (cfg,
556 if ( (NULL != handle->name) &&
557 (strlen (handle->name) > 4) &&
558 (0 == strcmp (".zkey",
559 &handle->name[strlen (handle->name) - 4])) )
561 GNUNET_CRYPTO_ecdsa_key_get_public
562 (GNUNET_CRYPTO_ecdsa_key_get_anonymous (),
564 lookup_with_public_key (handle);
568 GNUNET_break (NULL == handle->id_op);
569 handle->id_op = GNUNET_IDENTITY_get (handle->identity,
573 GNUNET_assert (NULL != handle->id_op);
578 * Handle rest request
580 * @param handle the lookup handle
583 options_cont (struct GNUNET_REST_RequestHandle *con_handle,
587 struct MHD_Response *resp;
588 struct LookupHandle *handle = cls;
590 //For GNS, independent of path return all options
591 resp = GNUNET_REST_create_response (NULL);
592 MHD_add_response_header (resp,
593 "Access-Control-Allow-Methods",
594 MHD_HTTP_METHOD_GET);
595 handle->proc (handle->proc_cls,
598 cleanup_handle (handle);
603 * Function processing the REST call
605 * @param method HTTP method
606 * @param url URL of the HTTP request
607 * @param data body of the HTTP request (optional)
608 * @param data_size length of the body
609 * @param proc callback function for the result
610 * @param proc_cls closure for @a proc
611 * @return #GNUNET_OK if request accepted
614 rest_gns_process_request (struct GNUNET_REST_RequestHandle *conndata_handle,
615 GNUNET_REST_ResultProcessor proc,
618 static const struct GNUNET_REST_RequestHandler handlers[] = {
619 {MHD_HTTP_METHOD_GET, GNUNET_REST_API_NS_GNS, &get_gns_cont},
620 {MHD_HTTP_METHOD_OPTIONS, GNUNET_REST_API_NS_GNS, &options_cont},
621 GNUNET_REST_HANDLER_END
623 struct LookupHandle *handle = GNUNET_new (struct LookupHandle);
624 struct GNUNET_REST_RequestHandlerError err;
626 handle->timeout = GNUNET_TIME_UNIT_FOREVER_REL;
627 handle->proc_cls = proc_cls;
629 handle->rest_handle = conndata_handle;
631 if (GNUNET_NO == GNUNET_JSONAPI_handle_request (conndata_handle,
636 handle->response_code = err.error_code;
637 GNUNET_SCHEDULER_add_now (&do_error, handle);
643 * Entry point for the plugin.
645 * @param cls the "struct GNUNET_NAMESTORE_PluginEnvironment*"
646 * @return NULL on error, otherwise the plugin context
649 libgnunet_plugin_rest_gns_init (void *cls)
651 static struct Plugin plugin;
653 struct GNUNET_REST_Plugin *api;
655 if (NULL != plugin.cfg)
656 return NULL; /* can only initialize once! */
657 memset (&plugin, 0, sizeof (struct Plugin));
659 api = GNUNET_new (struct GNUNET_REST_Plugin);
661 api->name = GNUNET_REST_API_NS_GNS;
662 api->process_request = &rest_gns_process_request;
663 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
664 _("GNS REST API initialized\n"));
670 * Exit point from the plugin.
672 * @param cls the plugin context (as returned by "init")
673 * @return always NULL
676 libgnunet_plugin_rest_gns_done (void *cls)
678 struct GNUNET_REST_Plugin *api = cls;
679 struct Plugin *plugin = api->cls;
683 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
684 "GNS REST plugin is finished\n");
688 /* end of plugin_rest_gns.c */