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 * @author Philippe Buschmann
21 * @file peerinfo/plugin_rest_peerinfo.c
22 * @brief GNUnet Peerinfo REST plugin
26 #include "gnunet_rest_plugin.h"
27 #include "gnunet_peerinfo_service.h"
28 #include "gnunet_transport_service.h"
29 #include "gnunet_rest_lib.h"
30 #include "gnunet_json_lib.h"
31 #include "microhttpd.h"
37 #define GNUNET_REST_API_NS_PEERINFO "/peerinfo"
40 * Peerinfo parameter peer
42 #define GNUNET_REST_PEERINFO_PEER "peer"
45 * Peerinfo parameter friend
47 #define GNUNET_REST_PEERINFO_FRIEND "friend"
50 * Peerinfo parameter array
52 #define GNUNET_REST_PEERINFO_ARRAY "array"
55 * Error message Unknown Error
57 #define GNUNET_REST_PEERINFO_ERROR_UNKNOWN "Unknown Error"
60 * How long until we time out during address lookup?
62 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
64 * The configuration handle
66 const struct GNUNET_CONFIGURATION_Handle *cfg;
69 * HTTP methods allows for this plugin
71 static char* allow_methods;
74 * @brief struct returned by the initialization function of the plugin
78 const struct GNUNET_CONFIGURATION_Handle *cfg;
83 * Record we keep for each printable address.
88 * Current address-to-string context (if active, otherwise NULL).
90 struct GNUNET_TRANSPORT_AddressToStringContext *atsc;
93 * Address expiration time
95 struct GNUNET_TIME_Absolute expiration;
103 * Print context this address record belongs to.
105 struct PrintContext *pc;
110 * Structure we use to collect printable address information.
117 struct PrintContext *next;
122 struct PrintContext *prev;
125 * Identity of the peer.
127 struct GNUNET_PeerIdentity peer;
130 * List of printable addresses.
132 struct AddressRecord *address_list;
135 * Number of completed addresses in @e address_list.
137 unsigned int num_addresses;
140 * Number of addresses allocated in @e address_list.
142 unsigned int address_list_size;
145 * Current offset in @e address_list (counted down).
150 * Hello was friend only, #GNUNET_YES or #GNUNET_NO
157 struct RequestHandle *handle;
162 * Head of list of print contexts.
164 static struct PrintContext *pc_head;
167 * Tail of list of print contexts.
169 static struct PrintContext *pc_tail;
177 * JSON temporary array
182 * Expiration time string
184 char *expiration_str;
192 * Iteration peer public key
202 * Handle to PEERINFO it
204 struct GNUNET_PEERINFO_IteratorContext *list_it;
209 struct GNUNET_PEERINFO_Handle *peerinfo_handle;
214 struct GNUNET_REST_RequestHandle *rest_handle;
217 * Desired timeout for the lookup (default is no timeout).
219 struct GNUNET_TIME_Relative timeout;
222 * ID of a task associated with the resolution process.
224 struct GNUNET_SCHEDULER_Task *timeout_task;
227 * The plugin result processor
229 GNUNET_REST_ResultProcessor proc;
232 * The closure of the result processor
242 * Error response message
255 * Cleanup lookup handle
256 * @param handle Handle to clean up
259 cleanup_handle (void *cls)
261 struct RequestHandle *handle = cls;
263 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
265 if (NULL != handle->timeout_task)
267 GNUNET_SCHEDULER_cancel (handle->timeout_task);
268 handle->timeout_task = NULL;
270 if (NULL != handle->url)
271 GNUNET_free (handle->url);
272 if (NULL != handle->emsg)
273 GNUNET_free (handle->emsg);
274 if (NULL != handle->address)
275 GNUNET_free ((char*)handle->address);
276 if (NULL != handle->expiration_str)
277 GNUNET_free (handle->expiration_str);
278 if (NULL != handle->pubkey)
279 GNUNET_free (handle->pubkey);
281 if (NULL != handle->temp_array)
283 json_decref(handle->temp_array);
284 handle->temp_array = NULL;
286 if (NULL != handle->response)
288 json_decref(handle->response);
289 handle->response = NULL;
292 if (NULL != handle->list_it)
294 GNUNET_PEERINFO_iterate_cancel(handle->list_it);
295 handle->list_it = NULL;
297 if (NULL != handle->peerinfo_handle)
299 GNUNET_PEERINFO_disconnect(handle->peerinfo_handle);
300 handle->peerinfo_handle = NULL;
303 GNUNET_free (handle);
308 * Task run on errors. Reports an error and cleans up everything.
310 * @param cls the `struct RequestHandle`
315 struct RequestHandle *handle = cls;
316 struct MHD_Response *resp;
317 json_t *json_error = json_object();
320 if (NULL == handle->emsg)
321 handle->emsg = GNUNET_strdup(GNUNET_REST_PEERINFO_ERROR_UNKNOWN);
323 json_object_set_new(json_error,"error", json_string(handle->emsg));
325 if (0 == handle->response_code)
326 handle->response_code = MHD_HTTP_OK;
327 response = json_dumps (json_error, 0);
328 resp = GNUNET_REST_create_response (response);
329 handle->proc (handle->proc_cls, resp, handle->response_code);
330 json_decref(json_error);
331 GNUNET_free(response);
332 GNUNET_SCHEDULER_add_now (&cleanup_handle, handle);
337 * Function that assembles the response.
339 * @param cls the `struct RequestHandle`
342 peerinfo_list_finished (void *cls)
344 struct RequestHandle *handle = cls;
346 struct MHD_Response *resp;
348 if (NULL == handle->response)
350 handle->response_code = MHD_HTTP_NOT_FOUND;
351 handle->emsg = GNUNET_strdup ("No peers found");
352 GNUNET_SCHEDULER_add_now (&do_error, handle);
356 result_str = json_dumps (handle->response, 0);
357 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Result %s\n", result_str);
358 resp = GNUNET_REST_create_response (result_str);
359 handle->proc (handle->proc_cls, resp, MHD_HTTP_OK);
360 GNUNET_free_non_null (result_str);
361 GNUNET_SCHEDULER_add_now (&cleanup_handle, handle);
366 * Iterator callback to go over all addresses and count them.
368 * @param cls `struct PrintContext *` with `off` to increment
369 * @param address the address
370 * @param expiration expiration time
371 * @return #GNUNET_OK to keep the address and continue
374 count_address (void *cls,
375 const struct GNUNET_HELLO_Address *address,
376 struct GNUNET_TIME_Absolute expiration)
378 struct PrintContext *pc = cls;
380 if (0 == GNUNET_TIME_absolute_get_remaining (expiration).rel_value_us)
382 return GNUNET_OK; /* ignore expired address */
391 * Print the collected address information to the console and free @a pc.
393 * @param pc printing context
396 dump_pc (struct PrintContext *pc)
398 struct RequestHandle *handle;
400 json_t *response_entry;
405 json_t *friend_and_peer_json;
406 char *friend_and_peer;
408 temp_array = json_array();
409 response_entry = json_object();
411 for (i = 0; i < pc->num_addresses; i++)
413 if (NULL != pc->address_list[i].result)
415 object = json_object ();
416 address = json_string(pc->address_list[i].result);
417 expires = json_string(
418 GNUNET_STRINGS_absolute_time_to_string (pc->address_list[i].expiration));
419 json_object_set (object, "address", address);
420 json_object_set (object, "expires", expires);
422 json_decref(address);
423 json_decref(expires);
425 json_array_append(temp_array, object);
427 GNUNET_free (pc->address_list[i].result);
431 if (0 < json_array_size(temp_array))
433 GNUNET_asprintf(&friend_and_peer,
435 (GNUNET_YES == pc->friend_only) ? "F2F:" : "",
436 GNUNET_i2s_full (&pc->peer));
437 friend_and_peer_json = json_string(friend_and_peer);
438 json_object_set(response_entry,
439 GNUNET_REST_PEERINFO_PEER,
440 friend_and_peer_json);
441 json_object_set(response_entry,
442 GNUNET_REST_PEERINFO_ARRAY,
444 json_array_append(pc->handle->response, response_entry);
445 json_decref(friend_and_peer_json);
446 GNUNET_free(friend_and_peer);
449 json_decref (temp_array);
450 json_decref(response_entry);
452 GNUNET_free_non_null (pc->address_list);
453 GNUNET_CONTAINER_DLL_remove (pc_head,
459 if ( (NULL == pc_head) &&
460 (NULL == handle->list_it) )
462 GNUNET_SCHEDULER_add_now (&peerinfo_list_finished, handle);
469 * Function to call with a human-readable format of an address
472 * @param address NULL on error, otherwise 0-terminated printable UTF-8 string
473 * @param res result of the address to string conversion:
474 * if #GNUNET_OK: address was valid (conversion to
475 * string might still have failed)
476 * if #GNUNET_SYSERR: address is invalid
479 process_resolved_address (void *cls,
483 struct AddressRecord *ar = cls;
484 struct PrintContext *pc = ar->pc;
488 if (0 != strlen (address))
490 if (NULL != ar->result)
491 GNUNET_free (ar->result);
492 ar->result = GNUNET_strdup (address);
497 if (GNUNET_SYSERR == res)
498 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
499 _("Failure: Cannot convert address to string for peer `%s'\n"),
500 GNUNET_i2s (&ar->pc->peer));
502 if (pc->num_addresses == pc->address_list_size)
508 * Iterator callback to go over all addresses.
511 * @param address the address
512 * @param expiration expiration time
513 * @return #GNUNET_OK to keep the address and continue
516 print_address (void *cls,
517 const struct GNUNET_HELLO_Address *address,
518 struct GNUNET_TIME_Absolute expiration)
520 struct PrintContext *pc = cls;
521 struct AddressRecord *ar;
523 if (0 == GNUNET_TIME_absolute_get_remaining (expiration).rel_value_us)
525 return GNUNET_OK; /* ignore expired address */
528 GNUNET_assert (0 < pc->off);
529 ar = &pc->address_list[--pc->off];
531 ar->expiration = expiration;
532 GNUNET_asprintf (&ar->result,
534 address->transport_name,
535 address->address_length,
536 address->local_info);
537 ar->atsc = GNUNET_TRANSPORT_address_to_string (cfg,
541 &process_resolved_address,
548 * Callback that processes each of the known HELLOs for the
549 * iteration response construction.
551 * @param cls closure, NULL
552 * @param peer id of the peer, NULL for last call
553 * @param hello hello message for the peer (can be NULL)
554 * @param err_msg message
557 peerinfo_list_iteration(void *cls,
558 const struct GNUNET_PeerIdentity *peer,
559 const struct GNUNET_HELLO_Message *hello,
562 struct RequestHandle *handle = cls;
563 struct PrintContext *pc;
566 if (NULL == handle->response)
568 handle->response = json_array();
573 handle->list_it = NULL;
574 handle->emsg = GNUNET_strdup ("Error in communication with peerinfo");
577 GNUNET_free(handle->emsg);
578 handle->emsg = GNUNET_strdup (err_msg);
579 handle->response_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
582 GNUNET_SCHEDULER_add_now (&do_error, handle);
588 friend_only = GNUNET_NO;
590 friend_only = GNUNET_HELLO_is_friend_only (hello);
592 pc = GNUNET_new(struct PrintContext);
593 GNUNET_CONTAINER_DLL_insert (pc_head,
597 pc->friend_only = friend_only;
599 GNUNET_HELLO_iterate_addresses (hello,
608 pc->address_list_size = pc->off;
609 pc->address_list = GNUNET_malloc(
610 sizeof(struct AddressRecord) * pc->off);
611 GNUNET_HELLO_iterate_addresses (hello,
618 * Handle peerinfo GET request
620 * @param con_handle the connection handle
622 * @param cls the RequestHandle
625 peerinfo_get (struct GNUNET_REST_RequestHandle *con_handle,
629 struct RequestHandle *handle = cls;
630 struct GNUNET_HashCode key;
631 const struct GNUNET_PeerIdentity *specific_peer;
632 //GNUNET_PEER_Id peer_id;
633 int include_friend_only;
634 char* include_friend_only_str;
636 include_friend_only = GNUNET_NO;
637 GNUNET_CRYPTO_hash (GNUNET_REST_PEERINFO_FRIEND,
638 strlen (GNUNET_REST_PEERINFO_FRIEND),
641 == GNUNET_CONTAINER_multihashmap_contains (con_handle->url_param_map,
644 include_friend_only_str = GNUNET_CONTAINER_multihashmap_get (
645 con_handle->url_param_map, &key);
646 if (0 == strcmp(include_friend_only_str, "yes"))
648 include_friend_only = GNUNET_YES;
652 specific_peer = NULL;
653 GNUNET_CRYPTO_hash (GNUNET_REST_PEERINFO_PEER,
654 strlen (GNUNET_REST_PEERINFO_PEER),
657 == GNUNET_CONTAINER_multihashmap_contains (con_handle->url_param_map,
660 //peer_id = *(unsigned int*)GNUNET_CONTAINER_multihashmap_get (con_handle->url_param_map, &key);
661 //specific_peer = GNUNET_PEER_resolve2(peer_id);
664 handle->list_it = GNUNET_PEERINFO_iterate(handle->peerinfo_handle,
667 &peerinfo_list_iteration,
674 * Respond to OPTIONS request
676 * @param con_handle the connection handle
678 * @param cls the RequestHandle
681 options_cont (struct GNUNET_REST_RequestHandle *con_handle,
685 struct MHD_Response *resp;
686 struct RequestHandle *handle = cls;
688 //independent of path return all options
689 resp = GNUNET_REST_create_response (NULL);
690 MHD_add_response_header (resp,
691 "Access-Control-Allow-Methods",
693 handle->proc (handle->proc_cls, resp, MHD_HTTP_OK);
694 GNUNET_SCHEDULER_add_now (&cleanup_handle, handle);
700 * Handle rest request
702 * @param handle the request handle
705 init_cont (struct RequestHandle *handle)
707 struct GNUNET_REST_RequestHandlerError err;
708 static const struct GNUNET_REST_RequestHandler handlers[] = {
709 {MHD_HTTP_METHOD_GET, GNUNET_REST_API_NS_PEERINFO, &peerinfo_get},
710 {MHD_HTTP_METHOD_OPTIONS, GNUNET_REST_API_NS_PEERINFO, &options_cont},
711 GNUNET_REST_HANDLER_END
714 if (GNUNET_NO == GNUNET_REST_handle_request (handle->rest_handle,
719 handle->response_code = err.error_code;
720 GNUNET_SCHEDULER_add_now (&do_error, handle);
726 * Function processing the REST call
728 * @param method HTTP method
729 * @param url URL of the HTTP request
730 * @param data body of the HTTP request (optional)
731 * @param data_size length of the body
732 * @param proc callback function for the result
733 * @param proc_cls closure for callback function
734 * @return GNUNET_OK if request accepted
737 rest_process_request(struct GNUNET_REST_RequestHandle *rest_handle,
738 GNUNET_REST_ResultProcessor proc,
741 struct RequestHandle *handle = GNUNET_new (struct RequestHandle);
743 handle->response_code = 0;
744 handle->timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 60);
745 handle->proc_cls = proc_cls;
747 handle->rest_handle = rest_handle;
749 handle->url = GNUNET_strdup (rest_handle->url);
750 if (handle->url[strlen (handle->url)-1] == '/')
751 handle->url[strlen (handle->url)-1] = '\0';
752 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connecting...\n");
753 handle->peerinfo_handle = GNUNET_PEERINFO_connect(cfg);
755 handle->timeout_task =
756 GNUNET_SCHEDULER_add_delayed (handle->timeout,
760 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connected\n");
765 * Entry point for the plugin.
767 * @param cls Config info
768 * @return NULL on error, otherwise the plugin context
771 libgnunet_plugin_rest_peerinfo_init (void *cls)
773 static struct Plugin plugin;
774 struct GNUNET_REST_Plugin *api;
777 if (NULL != plugin.cfg)
778 return NULL; /* can only initialize once! */
779 memset (&plugin, 0, sizeof (struct Plugin));
781 api = GNUNET_new (struct GNUNET_REST_Plugin);
783 api->name = GNUNET_REST_API_NS_PEERINFO;
784 api->process_request = &rest_process_request;
785 GNUNET_asprintf (&allow_methods,
786 "%s, %s, %s, %s, %s",
788 MHD_HTTP_METHOD_POST,
790 MHD_HTTP_METHOD_DELETE,
791 MHD_HTTP_METHOD_OPTIONS);
793 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
794 _("Peerinfo REST API initialized\n"));
800 * Exit point from the plugin.
802 * @param cls the plugin context (as returned by "init")
803 * @return always NULL
806 libgnunet_plugin_rest_peerinfo_done (void *cls)
808 struct GNUNET_REST_Plugin *api = cls;
809 struct Plugin *plugin = api->cls;
812 GNUNET_free_non_null (allow_methods);
814 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
815 "Peerinfo REST plugin is finished\n");
819 /* end of plugin_rest_peerinfo.c */