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)));
285 * Function called with the result of a GNS lookup.
287 * @param cls the 'const char *' name that was resolved
288 * @param rd_count number of records returned
289 * @param rd array of @a rd_count records with the results
292 process_lookup_result (void *cls, uint32_t rd_count,
293 const struct GNUNET_GNSRECORD_Data *rd)
295 struct LookupHandle *handle = cls;
296 struct MHD_Response *resp;
297 struct GNUNET_JSONAPI_Document *json_document;
298 struct GNUNET_JSONAPI_Resource *json_resource;
301 json_t *result_array;
304 result_array = json_array();
305 json_document = GNUNET_JSONAPI_document_new ();
306 json_resource = GNUNET_JSONAPI_resource_new (GNUNET_REST_JSONAPI_GNS_TYPEINFO, handle->name);
307 handle->lookup_request = NULL;
308 for (i=0; i<rd_count; i++)
310 if ( (rd[i].record_type != handle->type) &&
311 (GNUNET_GNSRECORD_TYPE_ANY != handle->type) )
313 record_obj = gnsrecord_to_json (&(rd[i]));
314 json_array_append (result_array, record_obj);
315 json_decref (record_obj);
317 GNUNET_JSONAPI_resource_add_attr (json_resource,
318 GNUNET_REST_JSONAPI_GNS_RECORD,
320 GNUNET_JSONAPI_document_resource_add (json_document, json_resource);
321 GNUNET_JSONAPI_document_serialize (json_document, &result);
322 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Result %s\n", result);
323 json_decref (result_array);
324 GNUNET_JSONAPI_document_delete (json_document);
325 resp = GNUNET_REST_create_response (result);
326 handle->proc (handle->proc_cls, resp, MHD_HTTP_OK);
327 GNUNET_free (result);
328 cleanup_handle (handle);
333 * Perform the actual resolution, starting with the zone
334 * identified by the given public key and the shorten zone.
336 * @param pkey public key to use for the zone, can be NULL
339 lookup_with_public_key (struct LookupHandle *handle)
341 if (UINT32_MAX == handle->type)
343 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
344 _("Invalid typename specified, assuming `ANY'\n"));
345 handle->type = GNUNET_GNSRECORD_TYPE_ANY;
347 if (NULL != handle->name)
349 handle->lookup_request = GNUNET_GNS_lookup (handle->gns,
354 &process_lookup_result,
359 GNUNET_SCHEDULER_add_now (&do_error, handle);
366 * Method called to with the ego we are to use for the lookup,
367 * when the ego is determined by a name.
369 * @param cls closure (NULL, unused)
370 * @param ego ego handle, NULL if not found
373 identity_zone_cb (void *cls,
374 const struct GNUNET_IDENTITY_Ego *ego)
376 struct LookupHandle *handle = cls;
381 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
382 _("Ego for not found, cannot perform lookup.\n"));
383 GNUNET_SCHEDULER_add_now (&do_error, handle);
388 GNUNET_IDENTITY_ego_get_public_key (ego, &handle->pkey);
389 lookup_with_public_key (handle);
391 json_decref(handle->json_root);
396 * Method called to with the ego we are to use for the lookup,
397 * when the ego is the one for the default master zone.
399 * @param cls closure (NULL, unused)
400 * @param ego ego handle, NULL if not found
401 * @param ctx context for application to store data for this ego
402 * (during the lifetime of this process, initially NULL)
403 * @param name name assigned by the user for this ego,
404 * NULL if the user just deleted the ego and it
405 * must thus no longer be used
408 identity_master_cb (void *cls,
409 struct GNUNET_IDENTITY_Ego *ego,
414 struct LookupHandle *handle = cls;
416 handle->id_op = NULL;
419 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
420 _("Ego for `gns-master' not found, cannot perform lookup. Did you run gnunet-gns-import.sh?\n"));
421 GNUNET_SCHEDULER_add_now (&do_error, handle);
424 GNUNET_IDENTITY_ego_get_public_key (ego,
426 /* main name is our own master zone, do no look for that in the DHT */
427 handle->options = GNUNET_GNS_LO_LOCAL_MASTER;
428 /* if the name is of the form 'label.gnu', never go to the DHT */
430 if (NULL != handle->name)
431 dot = strchr (handle->name, '.');
432 if ( (NULL != dot) &&
433 (0 == strcasecmp (dot, ".gnu")) )
434 handle->options = GNUNET_GNS_LO_NO_DHT;
435 lookup_with_public_key (handle);
439 * Parse REST uri for name and record type
441 * @param url Url to parse
442 * @param handle lookup handle to populate
443 * @return GNUNET_SYSERR on error
446 parse_url (const char *url, struct LookupHandle *handle)
449 char tmp_url[strlen(url)+1];
452 strcpy (tmp_url, url);
453 tok = strtok ((char*)tmp_url, "/");
455 return GNUNET_SYSERR;
456 name = strtok (NULL, "/");
458 return GNUNET_SYSERR;
459 GNUNET_asprintf (&handle->name,
462 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
463 "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,
532 GNUNET_assert (NULL != handle->pkey_str);
534 GNUNET_CRYPTO_ecdsa_public_key_from_string (handle->pkey_str,
535 strlen(handle->pkey_str),
538 GNUNET_SCHEDULER_add_now (&do_error, handle);
541 lookup_with_public_key (handle);
544 GNUNET_CRYPTO_hash (GNUNET_REST_JSONAPI_GNS_EGO,
545 strlen (GNUNET_REST_JSONAPI_GNS_EGO),
548 GNUNET_CONTAINER_multihashmap_contains (conndata_handle->url_param_map,
551 handle->ego_str = GNUNET_CONTAINER_multihashmap_get (conndata_handle->url_param_map,
553 handle->el = GNUNET_IDENTITY_ego_lookup (cfg,
559 if ( (NULL != handle->name) &&
560 (strlen (handle->name) > 4) &&
561 (0 == strcmp (".zkey",
562 &handle->name[strlen (handle->name) - 4])) )
564 GNUNET_CRYPTO_ecdsa_key_get_public
565 (GNUNET_CRYPTO_ecdsa_key_get_anonymous (),
567 lookup_with_public_key (handle);
571 GNUNET_break (NULL == handle->id_op);
572 handle->id_op = GNUNET_IDENTITY_get (handle->identity,
576 GNUNET_assert (NULL != handle->id_op);
581 * Handle rest request
583 * @param handle the lookup handle
586 options_cont (struct GNUNET_REST_RequestHandle *con_handle,
590 struct MHD_Response *resp;
591 struct LookupHandle *handle = cls;
593 //For GNS, independent of path return all options
594 resp = GNUNET_REST_create_response (NULL);
595 MHD_add_response_header (resp,
596 "Access-Control-Allow-Methods",
597 MHD_HTTP_METHOD_GET);
598 handle->proc (handle->proc_cls,
601 cleanup_handle (handle);
606 * Function processing the REST call
608 * @param method HTTP method
609 * @param url URL of the HTTP request
610 * @param data body of the HTTP request (optional)
611 * @param data_size length of the body
612 * @param proc callback function for the result
613 * @param proc_cls closure for @a proc
614 * @return #GNUNET_OK if request accepted
617 rest_gns_process_request (struct GNUNET_REST_RequestHandle *conndata_handle,
618 GNUNET_REST_ResultProcessor proc,
621 static const struct GNUNET_REST_RequestHandler handlers[] = {
622 {MHD_HTTP_METHOD_GET, GNUNET_REST_API_NS_GNS, &get_gns_cont},
623 {MHD_HTTP_METHOD_OPTIONS, GNUNET_REST_API_NS_GNS, &options_cont},
624 GNUNET_REST_HANDLER_END
626 struct LookupHandle *handle = GNUNET_new (struct LookupHandle);
627 struct GNUNET_REST_RequestHandlerError err;
629 handle->timeout = GNUNET_TIME_UNIT_FOREVER_REL;
630 handle->proc_cls = proc_cls;
632 handle->rest_handle = conndata_handle;
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 */