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
341 lookup_with_public_key (struct LookupHandle *handle)
343 if (UINT32_MAX == handle->type)
345 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
346 _("Invalid typename specified, assuming `ANY'\n"));
347 handle->type = GNUNET_GNSRECORD_TYPE_ANY;
349 if (NULL != handle->name)
351 handle->lookup_request = GNUNET_GNS_lookup (handle->gns,
356 &process_lookup_result,
361 GNUNET_SCHEDULER_add_now (&do_error, handle);
368 * Method called to with the ego we are to use for the lookup,
369 * when the ego is determined by a name.
371 * @param cls closure (NULL, unused)
372 * @param ego ego handle, NULL if not found
375 identity_zone_cb (void *cls,
376 const struct GNUNET_IDENTITY_Ego *ego)
378 struct LookupHandle *handle = cls;
383 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
384 _("Ego for not found, cannot perform lookup.\n"));
385 GNUNET_SCHEDULER_add_now (&do_error, handle);
390 GNUNET_IDENTITY_ego_get_public_key (ego, &handle->pkey);
391 lookup_with_public_key (handle);
393 json_decref(handle->json_root);
398 * Method called to with the ego we are to use for the lookup,
399 * when the ego is the one for the default master zone.
401 * @param cls closure (NULL, unused)
402 * @param ego ego handle, NULL if not found
403 * @param ctx context for application to store data for this ego
404 * (during the lifetime of this process, initially NULL)
405 * @param name name assigned by the user for this ego,
406 * NULL if the user just deleted the ego and it
407 * must thus no longer be used
410 identity_master_cb (void *cls,
411 struct GNUNET_IDENTITY_Ego *ego,
416 struct LookupHandle *handle = cls;
418 handle->id_op = NULL;
421 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
422 _("Ego for `gns-master' not found, cannot perform lookup. Did you run gnunet-gns-import.sh?\n"));
423 GNUNET_SCHEDULER_add_now (&do_error, handle);
426 GNUNET_IDENTITY_ego_get_public_key (ego, &handle->pkey);
427 /* main name is our own master zone, do no look for that in the DHT */
428 handle->options = GNUNET_GNS_LO_LOCAL_MASTER;
429 /* if the name is of the form 'label.gnu', never go to the DHT */
431 if (NULL != handle->name)
432 dot = strchr (handle->name, '.');
433 if ( (NULL != dot) &&
434 (0 == strcasecmp (dot, ".gnu")) )
435 handle->options = GNUNET_GNS_LO_NO_DHT;
436 lookup_with_public_key (handle);
440 * Parse REST uri for name and record type
442 * @param url Url to parse
443 * @param handle lookup handle to populate
444 * @return GNUNET_SYSERR on error
447 parse_url (const char *url, struct LookupHandle *handle)
450 char tmp_url[strlen(url)+1];
453 strcpy (tmp_url, url);
454 tok = strtok ((char*)tmp_url, "/");
456 return GNUNET_SYSERR;
457 name = strtok (NULL, "/");
459 return GNUNET_SYSERR;
460 GNUNET_asprintf (&handle->name,
463 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
464 "Got name: %s\n", handle->name);
469 get_gns_cont (struct GNUNET_REST_RequestHandle *conndata_handle,
473 struct LookupHandle *handle = cls;
474 struct GNUNET_HashCode key;
476 //parse name and type from url
477 if (GNUNET_OK != parse_url (url, handle))
479 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Error parsing url...\n");
480 GNUNET_SCHEDULER_add_now (&do_error, handle);
483 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
485 handle->gns = GNUNET_GNS_connect (cfg);
486 handle->identity = GNUNET_IDENTITY_connect (cfg, NULL, NULL);
487 handle->timeout_task = GNUNET_SCHEDULER_add_delayed (handle->timeout,
489 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
491 if (NULL == handle->gns)
493 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
494 "Connecting to GNS failed\n");
495 GNUNET_SCHEDULER_add_now (&do_error, handle);
498 GNUNET_CRYPTO_hash (GNUNET_REST_JSONAPI_GNS_OPTIONS,
499 strlen (GNUNET_REST_JSONAPI_GNS_OPTIONS),
501 handle->options = GNUNET_GNS_LO_DEFAULT;
503 GNUNET_CONTAINER_multihashmap_contains (conndata_handle->url_param_map,
506 handle->options = GNUNET_GNS_LO_DEFAULT;//TODO(char*) GNUNET_CONTAINER_multihashmap_get (conndata_handle->url_param_map,
509 GNUNET_CRYPTO_hash (GNUNET_REST_JSONAPI_GNS_RECORD_TYPE,
510 strlen (GNUNET_REST_JSONAPI_GNS_RECORD_TYPE),
513 GNUNET_CONTAINER_multihashmap_contains (conndata_handle->url_param_map,
516 handle->type = GNUNET_GNSRECORD_typename_to_number
517 (GNUNET_CONTAINER_multihashmap_get (conndata_handle->url_param_map,
521 handle->type = GNUNET_GNSRECORD_TYPE_ANY;
523 GNUNET_CRYPTO_hash (GNUNET_REST_JSONAPI_GNS_PKEY,
524 strlen (GNUNET_REST_JSONAPI_GNS_PKEY),
527 GNUNET_CONTAINER_multihashmap_contains (conndata_handle->url_param_map,
530 handle->pkey_str = GNUNET_CONTAINER_multihashmap_get (conndata_handle->url_param_map,
533 GNUNET_CRYPTO_ecdsa_public_key_from_string (handle->pkey_str,
534 strlen(handle->pkey_str),
537 GNUNET_SCHEDULER_add_now (&do_error, handle);
540 lookup_with_public_key (handle);
543 GNUNET_CRYPTO_hash (GNUNET_REST_JSONAPI_GNS_EGO,
544 strlen (GNUNET_REST_JSONAPI_GNS_EGO),
547 GNUNET_CONTAINER_multihashmap_contains (conndata_handle->url_param_map,
550 handle->ego_str = GNUNET_CONTAINER_multihashmap_get (conndata_handle->url_param_map,
552 handle->el = GNUNET_IDENTITY_ego_lookup (cfg,
558 if ( (NULL != handle->name) &&
559 (strlen (handle->name) > 4) &&
560 (0 == strcmp (".zkey",
561 &handle->name[strlen (handle->name) - 4])) )
563 GNUNET_CRYPTO_ecdsa_key_get_public
564 (GNUNET_CRYPTO_ecdsa_key_get_anonymous (),
566 lookup_with_public_key (handle);
570 GNUNET_break (NULL == handle->id_op);
571 handle->id_op = GNUNET_IDENTITY_get (handle->identity,
575 GNUNET_assert (NULL != handle->id_op);
580 * Handle rest request
582 * @param handle the lookup handle
585 options_cont (struct GNUNET_REST_RequestHandle *con_handle,
589 struct MHD_Response *resp;
590 struct LookupHandle *handle = cls;
592 //For GNS, independent of path return all options
593 resp = GNUNET_REST_create_response (NULL);
594 MHD_add_response_header (resp,
595 "Access-Control-Allow-Methods",
596 MHD_HTTP_METHOD_GET);
597 handle->proc (handle->proc_cls,
600 cleanup_handle (handle);
605 * Function processing the REST call
607 * @param method HTTP method
608 * @param url URL of the HTTP request
609 * @param data body of the HTTP request (optional)
610 * @param data_size length of the body
611 * @param proc callback function for the result
612 * @param proc_cls closure for callback function
613 * @return GNUNET_OK if request accepted
616 rest_gns_process_request(struct GNUNET_REST_RequestHandle *conndata_handle,
617 GNUNET_REST_ResultProcessor proc,
620 struct LookupHandle *handle = GNUNET_new (struct LookupHandle);
621 struct GNUNET_REST_RequestHandlerError err;
623 handle->timeout = GNUNET_TIME_UNIT_FOREVER_REL;
624 handle->proc_cls = proc_cls;
626 handle->rest_handle = conndata_handle;
628 static const struct GNUNET_REST_RequestHandler handlers[] = {
629 {MHD_HTTP_METHOD_GET, GNUNET_REST_API_NS_GNS, &get_gns_cont},
630 {MHD_HTTP_METHOD_OPTIONS, GNUNET_REST_API_NS_GNS, &options_cont},
631 GNUNET_REST_HANDLER_END
634 if (GNUNET_NO == GNUNET_JSONAPI_handle_request (conndata_handle,
639 handle->response_code = err.error_code;
640 GNUNET_SCHEDULER_add_now (&do_error, handle);
646 * Entry point for the plugin.
648 * @param cls the "struct GNUNET_NAMESTORE_PluginEnvironment*"
649 * @return NULL on error, otherwise the plugin context
652 libgnunet_plugin_rest_gns_init (void *cls)
654 static struct Plugin plugin;
656 struct GNUNET_REST_Plugin *api;
658 if (NULL != plugin.cfg)
659 return NULL; /* can only initialize once! */
660 memset (&plugin, 0, sizeof (struct Plugin));
662 api = GNUNET_new (struct GNUNET_REST_Plugin);
664 api->name = GNUNET_REST_API_NS_GNS;
665 api->process_request = &rest_gns_process_request;
666 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
667 _("GNS REST API initialized\n"));
673 * Exit point from the plugin.
675 * @param cls the plugin context (as returned by "init")
676 * @return always NULL
679 libgnunet_plugin_rest_gns_done (void *cls)
681 struct GNUNET_REST_Plugin *api = cls;
682 struct Plugin *plugin = api->cls;
686 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
687 "GNS REST plugin is finished\n");
691 /* end of plugin_rest_gns.c */