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 Affero 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.
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
19 * @author Martin Schanzenbach
20 * @file gns/plugin_rest_gns.c
21 * @brief GNUnet GNS REST plugin
26 #include "gnunet_rest_plugin.h"
27 #include <gnunet_dnsparser_lib.h>
28 #include <gnunet_identity_service.h>
29 #include <gnunet_gnsrecord_lib.h>
30 #include <gnunet_namestore_service.h>
31 #include <gnunet_gns_service.h>
32 #include <gnunet_rest_lib.h>
33 #include <gnunet_jsonapi_lib.h>
34 #include <gnunet_jsonapi_util.h>
37 #define GNUNET_REST_API_NS_GNS "/gns"
39 #define GNUNET_REST_JSONAPI_GNS_RECORD_TYPE "record_type"
41 #define GNUNET_REST_JSONAPI_GNS_TYPEINFO "gns_name"
43 #define GNUNET_REST_JSONAPI_GNS_RECORD "records"
45 #define GNUNET_REST_JSONAPI_GNS_EGO "ego"
47 #define GNUNET_REST_JSONAPI_GNS_PKEY "pkey"
49 #define GNUNET_REST_JSONAPI_GNS_OPTIONS "options"
52 * @brief struct returned by the initialization function of the plugin
56 const struct GNUNET_CONFIGURATION_Handle *cfg;
59 const struct GNUNET_CONFIGURATION_Handle *cfg;
64 * Handle to GNS service.
66 struct GNUNET_GNS_Handle *gns;
69 * Desired timeout for the lookup (default is no timeout).
71 struct GNUNET_TIME_Relative timeout;
74 * Handle to lookup request
76 struct GNUNET_GNS_LookupRequest *lookup_request;
79 * Handle to rest request
81 struct GNUNET_REST_RequestHandle *rest_handle;
84 * Lookup an ego with the identity service.
86 struct GNUNET_IDENTITY_EgoLookup *el;
89 * Handle for identity service.
91 struct GNUNET_IDENTITY_Handle *identity;
94 * Active operation on identity service.
96 struct GNUNET_IDENTITY_Operation *id_op;
99 * ID of a task associated with the resolution process.
101 struct GNUNET_SCHEDULER_Task * timeout_task;
104 * The root of the received JSON or NULL
109 * The plugin result processor
111 GNUNET_REST_ResultProcessor proc;
114 * The closure of the result processor
119 * The name to look up
125 * In string representation from JSON
131 * In string representation from JSON
133 const char *pkey_str;
141 * The public key of to use for lookup
143 struct GNUNET_CRYPTO_EcdsaPublicKey pkey;
146 * The public key to use for lookup
148 struct GNUNET_CRYPTO_EcdsaPublicKey pkeym;
151 * The resolver options
153 enum GNUNET_GNS_LocalOptions options;
158 struct GNUNET_CRYPTO_EcdsaPrivateKey shorten_key;
169 * Cleanup lookup handle.
171 * @param handle Handle to clean up
174 cleanup_handle (struct LookupHandle *handle)
176 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
178 if (NULL != handle->json_root)
179 json_decref (handle->json_root);
181 if (NULL != handle->name)
182 GNUNET_free (handle->name);
183 if (NULL != handle->el)
185 GNUNET_IDENTITY_ego_lookup_cancel (handle->el);
188 if (NULL != handle->id_op)
190 GNUNET_IDENTITY_cancel (handle->id_op);
191 handle->id_op = NULL;
193 if (NULL != handle->lookup_request)
195 GNUNET_GNS_lookup_cancel (handle->lookup_request);
196 handle->lookup_request = NULL;
198 if (NULL != handle->identity)
200 GNUNET_IDENTITY_disconnect (handle->identity);
201 handle->identity = NULL;
203 if (NULL != handle->gns)
205 GNUNET_GNS_disconnect (handle->gns);
209 if (NULL != handle->timeout_task)
211 GNUNET_SCHEDULER_cancel (handle->timeout_task);
213 GNUNET_free (handle);
218 * Task run on shutdown. Cleans up everything.
221 * @param tc scheduler context
226 struct LookupHandle *handle = cls;
227 struct MHD_Response *resp;
229 resp = GNUNET_REST_create_response (NULL);
230 handle->proc (handle->proc_cls, resp, handle->response_code);
231 cleanup_handle (handle);
236 * Create json representation of a GNSRECORD
238 * @param rd the GNSRECORD_Data
241 gnsrecord_to_json (const struct GNUNET_GNSRECORD_Data *rd)
243 const char *typename;
248 typename = GNUNET_GNSRECORD_number_to_typename (rd->record_type);
249 string_val = GNUNET_GNSRECORD_value_to_string (rd->record_type,
253 if (NULL == string_val)
255 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
256 "Record of type %d malformed, skipping\n",
257 (int) rd->record_type);
260 record_obj = json_object ();
261 json_object_set_new (record_obj, "type", json_string (typename));
262 json_object_set_new (record_obj, "value", json_string (string_val));
263 GNUNET_free (string_val);
265 if (GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION & rd->flags)
267 struct GNUNET_TIME_Relative time_rel;
268 time_rel.rel_value_us = rd->expiration_time;
269 exp_str = GNUNET_STRINGS_relative_time_to_string (time_rel, 1);
273 struct GNUNET_TIME_Absolute time_abs;
274 time_abs.abs_value_us = rd->expiration_time;
275 exp_str = GNUNET_STRINGS_absolute_time_to_string (time_abs);
277 json_object_set_new (record_obj, "expiration_time", json_string (exp_str));
279 json_object_set_new (record_obj, "expired",
280 json_boolean (GNUNET_YES == GNUNET_GNSRECORD_is_expired (rd)));
286 do_cleanup (void *cls)
288 struct LookupHandle *handle = cls;
289 cleanup_handle (handle);
294 * Function called with the result of a GNS lookup.
296 * @param cls the 'const char *' name that was resolved
297 * @param rd_count number of records returned
298 * @param rd array of @a rd_count records with the results
301 process_lookup_result (void *cls, uint32_t rd_count,
302 const struct GNUNET_GNSRECORD_Data *rd)
304 struct LookupHandle *handle = cls;
305 struct MHD_Response *resp;
306 struct GNUNET_JSONAPI_Document *json_document;
307 struct GNUNET_JSONAPI_Resource *json_resource;
310 json_t *result_array;
313 result_array = json_array();
314 json_document = GNUNET_JSONAPI_document_new ();
315 json_resource = GNUNET_JSONAPI_resource_new (GNUNET_REST_JSONAPI_GNS_TYPEINFO, handle->name);
316 handle->lookup_request = NULL;
317 for (i=0; i<rd_count; i++)
319 if ( (rd[i].record_type != handle->type) &&
320 (GNUNET_GNSRECORD_TYPE_ANY != handle->type) )
322 record_obj = gnsrecord_to_json (&(rd[i]));
323 json_array_append (result_array, record_obj);
324 json_decref (record_obj);
326 GNUNET_JSONAPI_resource_add_attr (json_resource,
327 GNUNET_REST_JSONAPI_GNS_RECORD,
329 GNUNET_JSONAPI_document_resource_add (json_document, json_resource);
330 GNUNET_JSONAPI_document_serialize (json_document, &result);
331 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Result %s\n", result);
332 json_decref (result_array);
333 GNUNET_JSONAPI_document_delete (json_document);
334 resp = GNUNET_REST_create_response (result);
335 handle->proc (handle->proc_cls, resp, MHD_HTTP_OK);
336 GNUNET_free (result);
337 GNUNET_SCHEDULER_add_now (&do_cleanup, handle);
342 * Perform the actual resolution, starting with the zone
343 * identified by the given public key and the shorten zone.
345 * @param pkey public key to use for the zone, can be NULL
348 lookup_with_public_key (struct LookupHandle *handle)
350 if (UINT32_MAX == handle->type)
352 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
353 _("Invalid typename specified, assuming `ANY'\n"));
354 handle->type = GNUNET_GNSRECORD_TYPE_ANY;
356 if (NULL != handle->name)
358 handle->lookup_request = GNUNET_GNS_lookup (handle->gns,
363 &process_lookup_result,
368 GNUNET_SCHEDULER_add_now (&do_error, handle);
375 * Method called to with the ego we are to use for the lookup,
376 * when the ego is determined by a name.
378 * @param cls closure (NULL, unused)
379 * @param ego ego handle, NULL if not found
382 identity_zone_cb (void *cls,
383 const struct GNUNET_IDENTITY_Ego *ego)
385 struct LookupHandle *handle = cls;
390 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
391 _("Ego for not found, cannot perform lookup.\n"));
392 GNUNET_SCHEDULER_add_now (&do_error, handle);
397 GNUNET_IDENTITY_ego_get_public_key (ego, &handle->pkey);
398 lookup_with_public_key (handle);
400 json_decref(handle->json_root);
405 * Method called to with the ego we are to use for the lookup,
406 * when the ego is the one for the default master zone.
408 * @param cls closure (NULL, unused)
409 * @param ego ego handle, NULL if not found
410 * @param ctx context for application to store data for this ego
411 * (during the lifetime of this process, initially NULL)
412 * @param name name assigned by the user for this ego,
413 * NULL if the user just deleted the ego and it
414 * must thus no longer be used
417 identity_master_cb (void *cls,
418 struct GNUNET_IDENTITY_Ego *ego,
423 struct LookupHandle *handle = cls;
425 handle->id_op = NULL;
428 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
429 _("Ego for `gns-master' not found, cannot perform lookup. Did you run gnunet-gns-import.sh?\n"));
430 GNUNET_SCHEDULER_add_now (&do_error, handle);
433 GNUNET_IDENTITY_ego_get_public_key (ego,
435 /* main name is our own master zone, do no look for that in the DHT */
436 handle->options = GNUNET_GNS_LO_LOCAL_MASTER;
437 /* if the name is of the form 'label.gnu', never go to the DHT */
439 if (NULL != handle->name)
440 dot = strchr (handle->name, '.');
441 if ( (NULL != dot) &&
442 (0 == strcasecmp (dot, ".gnu")) )
443 handle->options = GNUNET_GNS_LO_NO_DHT;
444 lookup_with_public_key (handle);
448 * Parse REST uri for name and record type
450 * @param url Url to parse
451 * @param handle lookup handle to populate
452 * @return GNUNET_SYSERR on error
455 parse_url (const char *url, struct LookupHandle *handle)
458 char tmp_url[strlen(url)+1];
461 strcpy (tmp_url, url);
462 tok = strtok ((char*)tmp_url, "/");
464 return GNUNET_SYSERR;
465 name = strtok (NULL, "/");
467 return GNUNET_SYSERR;
468 GNUNET_asprintf (&handle->name,
471 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
472 "Got name: %s\n", handle->name);
478 get_gns_cont (struct GNUNET_REST_RequestHandle *conndata_handle,
482 struct LookupHandle *handle = cls;
483 struct GNUNET_HashCode key;
485 //parse name and type from url
486 if (GNUNET_OK != parse_url (url, handle))
488 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Error parsing url...\n");
489 GNUNET_SCHEDULER_add_now (&do_error, handle);
492 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
494 handle->gns = GNUNET_GNS_connect (cfg);
495 handle->identity = GNUNET_IDENTITY_connect (cfg, NULL, NULL);
496 handle->timeout_task = GNUNET_SCHEDULER_add_delayed (handle->timeout,
498 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
500 if (NULL == handle->gns)
502 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
503 "Connecting to GNS failed\n");
504 GNUNET_SCHEDULER_add_now (&do_error, handle);
507 GNUNET_CRYPTO_hash (GNUNET_REST_JSONAPI_GNS_OPTIONS,
508 strlen (GNUNET_REST_JSONAPI_GNS_OPTIONS),
510 handle->options = GNUNET_GNS_LO_DEFAULT;
512 GNUNET_CONTAINER_multihashmap_contains (conndata_handle->url_param_map,
515 handle->options = GNUNET_GNS_LO_DEFAULT;//TODO(char*) GNUNET_CONTAINER_multihashmap_get (conndata_handle->url_param_map,
518 GNUNET_CRYPTO_hash (GNUNET_REST_JSONAPI_GNS_RECORD_TYPE,
519 strlen (GNUNET_REST_JSONAPI_GNS_RECORD_TYPE),
522 GNUNET_CONTAINER_multihashmap_contains (conndata_handle->url_param_map,
525 handle->type = GNUNET_GNSRECORD_typename_to_number
526 (GNUNET_CONTAINER_multihashmap_get (conndata_handle->url_param_map,
530 handle->type = GNUNET_GNSRECORD_TYPE_ANY;
532 GNUNET_CRYPTO_hash (GNUNET_REST_JSONAPI_GNS_PKEY,
533 strlen (GNUNET_REST_JSONAPI_GNS_PKEY),
536 GNUNET_CONTAINER_multihashmap_contains (conndata_handle->url_param_map,
539 handle->pkey_str = GNUNET_CONTAINER_multihashmap_get (conndata_handle->url_param_map,
541 GNUNET_assert (NULL != handle->pkey_str);
543 GNUNET_CRYPTO_ecdsa_public_key_from_string (handle->pkey_str,
544 strlen(handle->pkey_str),
547 GNUNET_SCHEDULER_add_now (&do_error, handle);
550 lookup_with_public_key (handle);
553 GNUNET_CRYPTO_hash (GNUNET_REST_JSONAPI_GNS_EGO,
554 strlen (GNUNET_REST_JSONAPI_GNS_EGO),
557 GNUNET_CONTAINER_multihashmap_contains (conndata_handle->url_param_map,
560 handle->ego_str = GNUNET_CONTAINER_multihashmap_get (conndata_handle->url_param_map,
562 handle->el = GNUNET_IDENTITY_ego_lookup (cfg,
568 if ( (NULL != handle->name) &&
569 (strlen (handle->name) > 4) &&
570 (0 == strcmp (".zkey",
571 &handle->name[strlen (handle->name) - 4])) )
573 GNUNET_CRYPTO_ecdsa_key_get_public
574 (GNUNET_CRYPTO_ecdsa_key_get_anonymous (),
576 lookup_with_public_key (handle);
580 GNUNET_break (NULL == handle->id_op);
581 handle->id_op = GNUNET_IDENTITY_get (handle->identity,
585 GNUNET_assert (NULL != handle->id_op);
590 * Handle rest request
592 * @param handle the lookup handle
595 options_cont (struct GNUNET_REST_RequestHandle *con_handle,
599 struct MHD_Response *resp;
600 struct LookupHandle *handle = cls;
602 //For GNS, independent of path return all options
603 resp = GNUNET_REST_create_response (NULL);
604 MHD_add_response_header (resp,
605 "Access-Control-Allow-Methods",
606 MHD_HTTP_METHOD_GET);
607 handle->proc (handle->proc_cls,
610 cleanup_handle (handle);
615 * Function processing the REST call
617 * @param method HTTP method
618 * @param url URL of the HTTP request
619 * @param data body of the HTTP request (optional)
620 * @param data_size length of the body
621 * @param proc callback function for the result
622 * @param proc_cls closure for @a proc
623 * @return #GNUNET_OK if request accepted
626 rest_gns_process_request (struct GNUNET_REST_RequestHandle *conndata_handle,
627 GNUNET_REST_ResultProcessor proc,
630 static const struct GNUNET_REST_RequestHandler handlers[] = {
631 {MHD_HTTP_METHOD_GET, GNUNET_REST_API_NS_GNS, &get_gns_cont},
632 {MHD_HTTP_METHOD_OPTIONS, GNUNET_REST_API_NS_GNS, &options_cont},
633 GNUNET_REST_HANDLER_END
635 struct LookupHandle *handle = GNUNET_new (struct LookupHandle);
636 struct GNUNET_REST_RequestHandlerError err;
638 handle->timeout = GNUNET_TIME_UNIT_FOREVER_REL;
639 handle->proc_cls = proc_cls;
641 handle->rest_handle = conndata_handle;
643 if (GNUNET_NO == GNUNET_JSONAPI_handle_request (conndata_handle,
648 handle->response_code = err.error_code;
649 GNUNET_SCHEDULER_add_now (&do_error, handle);
655 * Entry point for the plugin.
657 * @param cls the "struct GNUNET_NAMESTORE_PluginEnvironment*"
658 * @return NULL on error, otherwise the plugin context
661 libgnunet_plugin_rest_gns_init (void *cls)
663 static struct Plugin plugin;
665 struct GNUNET_REST_Plugin *api;
667 if (NULL != plugin.cfg)
668 return NULL; /* can only initialize once! */
669 memset (&plugin, 0, sizeof (struct Plugin));
671 api = GNUNET_new (struct GNUNET_REST_Plugin);
673 api->name = GNUNET_REST_API_NS_GNS;
674 api->process_request = &rest_gns_process_request;
675 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
676 _("GNS REST API initialized\n"));
682 * Exit point from the plugin.
684 * @param cls the plugin context (as returned by "init")
685 * @return always NULL
688 libgnunet_plugin_rest_gns_done (void *cls)
690 struct GNUNET_REST_Plugin *api = cls;
691 struct Plugin *plugin = api->cls;
695 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
696 "GNS REST plugin is finished\n");
700 /* end of plugin_rest_gns.c */