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>
38 #define GNUNET_REST_API_NS_GNS "/gns"
40 #define GNUNET_REST_JSONAPI_GNS_RECORD_TYPE "record_type"
42 #define GNUNET_REST_JSONAPI_GNS_TYPEINFO "gns_name"
44 #define GNUNET_REST_JSONAPI_GNS_RECORD "records"
46 #define GNUNET_REST_JSONAPI_GNS_EGO "ego"
48 #define GNUNET_REST_JSONAPI_GNS_PKEY "pkey"
50 #define GNUNET_REST_JSONAPI_GNS_OPTIONS "options"
53 * @brief struct returned by the initialization function of the plugin
57 const struct GNUNET_CONFIGURATION_Handle *cfg;
60 const struct GNUNET_CONFIGURATION_Handle *cfg;
65 * Handle to GNS service.
67 struct GNUNET_GNS_Handle *gns;
70 * Desired timeout for the lookup (default is no timeout).
72 struct GNUNET_TIME_Relative timeout;
75 * Handle to lookup request
77 struct GNUNET_GNS_LookupRequest *lookup_request;
80 * Lookup an ego with the identity service.
82 struct GNUNET_IDENTITY_EgoLookup *el;
85 * Handle for identity service.
87 struct GNUNET_IDENTITY_Handle *identity;
90 * Active operation on identity service.
92 struct GNUNET_IDENTITY_Operation *id_op;
95 * ID of a task associated with the resolution process.
97 struct GNUNET_SCHEDULER_Task * timeout_task;
100 * The root of the received JSON or NULL
105 * The plugin result processor
107 GNUNET_REST_ResultProcessor proc;
110 * The closure of the result processor
115 * The name to look up
121 * In string representation from JSON
127 * In string representation from JSON
129 const char *pkey_str;
137 * The public key of to use for lookup
139 struct GNUNET_CRYPTO_EcdsaPublicKey pkey;
142 * The public key to use for lookup
144 struct GNUNET_CRYPTO_EcdsaPublicKey pkeym;
147 * The resolver options
149 enum GNUNET_GNS_LocalOptions options;
154 struct GNUNET_CRYPTO_EcdsaPrivateKey shorten_key;
160 * Cleanup lookup handle.
162 * @param handle Handle to clean up
165 cleanup_handle (struct LookupHandle *handle)
167 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
169 if (NULL != handle->json_root)
170 json_decref (handle->json_root);
172 if (NULL != handle->name)
173 GNUNET_free (handle->name);
174 if (NULL != handle->el)
176 GNUNET_IDENTITY_ego_lookup_cancel (handle->el);
179 if (NULL != handle->id_op)
181 GNUNET_IDENTITY_cancel (handle->id_op);
182 handle->id_op = NULL;
184 if (NULL != handle->lookup_request)
186 GNUNET_GNS_lookup_cancel (handle->lookup_request);
187 handle->lookup_request = NULL;
189 if (NULL != handle->identity)
191 GNUNET_IDENTITY_disconnect (handle->identity);
192 handle->identity = NULL;
194 if (NULL != handle->gns)
196 GNUNET_GNS_disconnect (handle->gns);
200 if (NULL != handle->timeout_task)
202 GNUNET_SCHEDULER_cancel (handle->timeout_task);
204 GNUNET_free (handle);
209 * Task run on shutdown. Cleans up everything.
212 * @param tc scheduler context
217 struct LookupHandle *handle = cls;
218 struct MHD_Response *resp;
220 resp = GNUNET_REST_create_json_response (NULL);
221 handle->proc (handle->proc_cls, resp, MHD_HTTP_BAD_REQUEST);
222 cleanup_handle (handle);
227 * Create json representation of a GNSRECORD
229 * @param rd the GNSRECORD_Data
232 gnsrecord_to_json (const struct GNUNET_GNSRECORD_Data *rd)
234 const char *typename;
239 typename = GNUNET_GNSRECORD_number_to_typename (rd->record_type);
240 string_val = GNUNET_GNSRECORD_value_to_string (rd->record_type,
244 if (NULL == string_val)
246 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
247 "Record of type %d malformed, skipping\n",
248 (int) rd->record_type);
251 record_obj = json_object();
252 json_object_set_new (record_obj, "type", json_string (typename));
253 json_object_set_new (record_obj, "value", json_string (string_val));
254 GNUNET_free (string_val);
256 if (GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION & rd->flags)
258 struct GNUNET_TIME_Relative time_rel;
259 time_rel.rel_value_us = rd->expiration_time;
260 exp_str = GNUNET_STRINGS_relative_time_to_string (time_rel, 1);
264 struct GNUNET_TIME_Absolute time_abs;
265 time_abs.abs_value_us = rd->expiration_time;
266 exp_str = GNUNET_STRINGS_absolute_time_to_string (time_abs);
268 json_object_set_new (record_obj, "expiration_time", json_string (exp_str));
270 json_object_set_new (record_obj, "expired",
271 json_boolean (GNUNET_YES == GNUNET_GNSRECORD_is_expired (rd)));
276 * Function called with the result of a GNS lookup.
278 * @param cls the 'const char *' name that was resolved
279 * @param rd_count number of records returned
280 * @param rd array of @a rd_count records with the results
283 process_lookup_result (void *cls, uint32_t rd_count,
284 const struct GNUNET_GNSRECORD_Data *rd)
286 struct LookupHandle *handle = cls;
287 struct MHD_Response *resp;
288 struct GNUNET_JSONAPI_Object *json_object;
289 struct GNUNET_JSONAPI_Resource *json_resource;
292 json_t *result_array;
295 result_array = json_array();
296 json_object = GNUNET_JSONAPI_object_new ();
297 json_resource = GNUNET_JSONAPI_resource_new (GNUNET_REST_JSONAPI_GNS_TYPEINFO, handle->name);
298 handle->lookup_request = NULL;
299 for (i=0; i<rd_count; i++)
301 if ( (rd[i].record_type != handle->type) &&
302 (GNUNET_GNSRECORD_TYPE_ANY != handle->type) )
304 record_obj = gnsrecord_to_json (&(rd[i]));
305 json_array_append (result_array, record_obj);
306 json_decref (record_obj);
308 GNUNET_JSONAPI_resource_add_attr (json_resource,
309 GNUNET_REST_JSONAPI_GNS_RECORD,
311 GNUNET_JSONAPI_object_resource_add (json_object, json_resource);
312 GNUNET_JSONAPI_data_serialize (json_object, &result);
313 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Result %s\n", result);
314 json_decref (result_array);
315 GNUNET_JSONAPI_object_delete (json_object);
316 resp = GNUNET_REST_create_json_response (result);
317 handle->proc (handle->proc_cls, resp, MHD_HTTP_OK);
318 GNUNET_free (result);
319 cleanup_handle (handle);
324 * Perform the actual resolution, starting with the zone
325 * identified by the given public key and the shorten zone.
327 * @param pkey public key to use for the zone, can be NULL
328 * @param shorten_key private key used for shortening, can be NULL
331 lookup_with_keys (struct LookupHandle *handle, const struct GNUNET_CRYPTO_EcdsaPrivateKey *shorten_key)
333 if (UINT32_MAX == handle->type)
335 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
336 _("Invalid typename specified, assuming `ANY'\n"));
337 handle->type = GNUNET_GNSRECORD_TYPE_ANY;
339 if (NULL != handle->name)
341 handle->lookup_request = GNUNET_GNS_lookup (handle->gns,
347 &process_lookup_result,
352 GNUNET_SCHEDULER_add_now (&do_error, handle);
358 * Method called to with the ego we are to use for shortening
361 * @param cls closure contains the public key to use
362 * @param ego ego handle, NULL if not found
363 * @param ctx context for application to store data for this ego
364 * (during the lifetime of this process, initially NULL)
365 * @param name name assigned by the user for this ego,
366 * NULL if the user just deleted the ego and it
367 * must thus no longer be used
370 identity_shorten_cb (void *cls,
371 struct GNUNET_IDENTITY_Ego *ego,
375 struct LookupHandle *handle = cls;
377 handle->id_op = NULL;
379 lookup_with_keys (handle, NULL);
381 lookup_with_keys (handle,
382 GNUNET_IDENTITY_ego_get_private_key (ego));
386 * Perform the actual resolution, starting with the zone
387 * identified by the given public key.
389 * @param pkey public key to use for the zone
392 lookup_with_public_key (struct LookupHandle *handle)
394 handle->pkeym = handle->pkey;
395 GNUNET_break (NULL == handle->id_op);
396 handle->id_op = GNUNET_IDENTITY_get (handle->identity,
398 &identity_shorten_cb,
400 if (NULL == handle->id_op)
403 lookup_with_keys (handle, NULL);
408 * Method called to with the ego we are to use for the lookup,
409 * when the ego is determined by a name.
411 * @param cls closure (NULL, unused)
412 * @param ego ego handle, NULL if not found
415 identity_zone_cb (void *cls,
416 const struct GNUNET_IDENTITY_Ego *ego)
418 struct LookupHandle *handle = cls;
423 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
424 _("Ego for not found, cannot perform lookup.\n"));
425 GNUNET_SCHEDULER_add_now (&do_error, handle);
430 GNUNET_IDENTITY_ego_get_public_key (ego, &handle->pkey);
431 lookup_with_public_key (handle);
433 json_decref(handle->json_root);
437 * Method called to with the ego we are to use for the lookup,
438 * when the ego is the one for the default master zone.
440 * @param cls closure (NULL, unused)
441 * @param ego ego handle, NULL if not found
442 * @param ctx context for application to store data for this ego
443 * (during the lifetime of this process, initially NULL)
444 * @param name name assigned by the user for this ego,
445 * NULL if the user just deleted the ego and it
446 * must thus no longer be used
449 identity_master_cb (void *cls,
450 struct GNUNET_IDENTITY_Ego *ego,
455 struct LookupHandle *handle = cls;
457 handle->id_op = NULL;
460 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
461 _("Ego for `gns-master' not found, cannot perform lookup. Did you run gnunet-gns-import.sh?\n"));
462 GNUNET_SCHEDULER_add_now (&do_error, handle);
465 GNUNET_IDENTITY_ego_get_public_key (ego, &handle->pkey);
466 /* main name is our own master zone, do no look for that in the DHT */
467 handle->options = GNUNET_GNS_LO_LOCAL_MASTER;
468 /* if the name is of the form 'label.gnu', never go to the DHT */
470 if (NULL != handle->name)
471 dot = strchr (handle->name, '.');
472 if ( (NULL != dot) &&
473 (0 == strcasecmp (dot, ".gnu")) )
474 handle->options = GNUNET_GNS_LO_NO_DHT;
475 lookup_with_public_key (handle);
479 * Parse REST uri for name and record type
481 * @param url Url to parse
482 * @param handle lookup handle to populate
483 * @return GNUNET_SYSERR on error
486 parse_url (const char *url, struct LookupHandle *handle)
489 char tmp_url[strlen(url)+1];
492 strcpy (tmp_url, url);
493 tok = strtok ((char*)tmp_url, "/");
495 return GNUNET_SYSERR;
496 name = strtok (NULL, "/");
498 return GNUNET_SYSERR;
499 GNUNET_asprintf (&handle->name,
502 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
503 "Got name: %s\n", handle->name);
508 get_gns_cont (struct RestConnectionDataHandle *conndata_handle,
512 struct LookupHandle *handle = cls;
513 struct GNUNET_HashCode key;
515 //parse name and type from url
516 if (GNUNET_OK != parse_url (url, handle))
518 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Error parsing url...\n");
519 GNUNET_SCHEDULER_add_now (&do_error, handle);
522 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
524 handle->gns = GNUNET_GNS_connect (cfg);
525 handle->identity = GNUNET_IDENTITY_connect (cfg, NULL, NULL);
526 handle->timeout_task = GNUNET_SCHEDULER_add_delayed (handle->timeout,
528 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
530 if (NULL == handle->gns)
532 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
533 "Connecting to GNS failed\n");
534 GNUNET_SCHEDULER_add_now (&do_error, handle);
537 GNUNET_CRYPTO_hash (GNUNET_REST_JSONAPI_GNS_OPTIONS,
538 strlen (GNUNET_REST_JSONAPI_GNS_OPTIONS),
540 handle->options = GNUNET_GNS_LO_DEFAULT;
542 GNUNET_CONTAINER_multihashmap_contains (conndata_handle->url_param_map,
545 handle->options = GNUNET_GNS_LO_DEFAULT;//TODO(char*) GNUNET_CONTAINER_multihashmap_get (conndata_handle->url_param_map,
548 GNUNET_CRYPTO_hash (GNUNET_REST_JSONAPI_GNS_RECORD_TYPE,
549 strlen (GNUNET_REST_JSONAPI_GNS_RECORD_TYPE),
552 GNUNET_CONTAINER_multihashmap_contains (conndata_handle->url_param_map,
555 handle->type = GNUNET_GNSRECORD_typename_to_number
556 (GNUNET_CONTAINER_multihashmap_get (conndata_handle->url_param_map,
560 handle->type = GNUNET_GNSRECORD_TYPE_ANY;
562 GNUNET_CRYPTO_hash (GNUNET_REST_JSONAPI_GNS_PKEY,
563 strlen (GNUNET_REST_JSONAPI_GNS_PKEY),
566 GNUNET_CONTAINER_multihashmap_contains (conndata_handle->url_param_map,
569 handle->pkey_str = GNUNET_CONTAINER_multihashmap_get (conndata_handle->url_param_map,
572 GNUNET_CRYPTO_ecdsa_public_key_from_string (handle->pkey_str,
573 strlen(handle->pkey_str),
576 GNUNET_SCHEDULER_add_now (&do_error, handle);
579 lookup_with_public_key (handle);
582 GNUNET_CRYPTO_hash (GNUNET_REST_JSONAPI_GNS_EGO,
583 strlen (GNUNET_REST_JSONAPI_GNS_EGO),
586 GNUNET_CONTAINER_multihashmap_contains (conndata_handle->url_param_map,
589 handle->ego_str = GNUNET_CONTAINER_multihashmap_get (conndata_handle->url_param_map,
591 handle->el = GNUNET_IDENTITY_ego_lookup (cfg,
597 if ( (NULL != handle->name) &&
598 (strlen (handle->name) > 4) &&
599 (0 == strcmp (".zkey",
600 &handle->name[strlen (handle->name) - 4])) )
602 GNUNET_CRYPTO_ecdsa_key_get_public
603 (GNUNET_CRYPTO_ecdsa_key_get_anonymous (),
605 lookup_with_public_key (handle);
609 GNUNET_break (NULL == handle->id_op);
610 handle->id_op = GNUNET_IDENTITY_get (handle->identity,
614 GNUNET_assert (NULL != handle->id_op);
619 * Handle rest request
621 * @param handle the lookup handle
624 options_cont (struct RestConnectionDataHandle *con_handle,
628 struct MHD_Response *resp;
629 struct LookupHandle *handle = cls;
631 //For GNS, independent of path return all options
632 resp = GNUNET_REST_create_json_response (NULL);
633 MHD_add_response_header (resp,
634 "Access-Control-Allow-Methods",
635 MHD_HTTP_METHOD_GET);
636 handle->proc (handle->proc_cls,
639 cleanup_handle (handle);
644 * Function processing the REST call
646 * @param method HTTP method
647 * @param url URL of the HTTP request
648 * @param data body of the HTTP request (optional)
649 * @param data_size length of the body
650 * @param proc callback function for the result
651 * @param proc_cls closure for callback function
652 * @return GNUNET_OK if request accepted
655 rest_gns_process_request(struct RestConnectionDataHandle *conndata_handle,
656 GNUNET_REST_ResultProcessor proc,
659 struct LookupHandle *handle = GNUNET_new (struct LookupHandle);
661 handle->timeout = GNUNET_TIME_UNIT_FOREVER_REL;
662 handle->proc_cls = proc_cls;
665 static const struct GNUNET_REST_RestConnectionHandler handlers[] = {
666 {MHD_HTTP_METHOD_GET, GNUNET_REST_API_NS_GNS, &get_gns_cont},
667 {MHD_HTTP_METHOD_OPTIONS, GNUNET_REST_API_NS_GNS, &options_cont},
668 GNUNET_REST_HANDLER_END
671 if (GNUNET_NO == GNUNET_REST_handle_request (conndata_handle, handlers, handle))
672 GNUNET_SCHEDULER_add_now (&do_error, handle);
677 * Entry point for the plugin.
679 * @param cls the "struct GNUNET_NAMESTORE_PluginEnvironment*"
680 * @return NULL on error, otherwise the plugin context
683 libgnunet_plugin_rest_gns_init (void *cls)
685 static struct Plugin plugin;
687 struct GNUNET_REST_Plugin *api;
689 if (NULL != plugin.cfg)
690 return NULL; /* can only initialize once! */
691 memset (&plugin, 0, sizeof (struct Plugin));
693 api = GNUNET_new (struct GNUNET_REST_Plugin);
695 api->name = GNUNET_REST_API_NS_GNS;
696 api->process_request = &rest_gns_process_request;
697 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
698 _("GNS REST API initialized\n"));
704 * Exit point from the plugin.
706 * @param cls the plugin context (as returned by "init")
707 * @return always NULL
710 libgnunet_plugin_rest_gns_done (void *cls)
712 struct GNUNET_REST_Plugin *api = cls;
713 struct Plugin *plugin = api->cls;
717 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
718 "GNS REST plugin is finished\n");
722 /* end of plugin_rest_gns.c */