2 This file is part of GNUnet.
3 Copyright (C) 2008, 2009, 2010, 2014, 2016 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.
22 * @file hostlist/gnunet-daemon-hostlist_server.c
23 * @author Christian Grothoff
24 * @author Matthias Wachs
25 * @author David Barksdale
26 * @brief application to provide an integrated hostlist HTTP server
29 #include <microhttpd.h>
30 #include "gnunet-daemon-hostlist_server.h"
31 #include "gnunet_hello_lib.h"
32 #include "gnunet_peerinfo_service.h"
33 #include "gnunet-daemon-hostlist.h"
34 #include "gnunet_resolver_service.h"
38 * How long until our hostlist advertisment transmission via CORE should
41 #define GNUNET_ADV_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 5)
45 * Handle to the HTTP server as provided by libmicrohttpd for IPv6.
47 static struct MHD_Daemon *daemon_handle_v6;
50 * Handle to the HTTP server as provided by libmicrohttpd for IPv4.
52 static struct MHD_Daemon *daemon_handle_v4;
57 static const struct GNUNET_CONFIGURATION_Handle *cfg;
60 * For keeping statistics.
62 static struct GNUNET_STATISTICS_Handle *stats;
65 * Handle to the core service (NULL until we've connected to it).
67 static struct GNUNET_CORE_Handle *core;
70 * Handle to the peerinfo notify service (NULL until we've connected to it).
72 static struct GNUNET_PEERINFO_NotifyContext *notify;
75 * Our primary task for IPv4.
77 static struct GNUNET_SCHEDULER_Task *hostlist_task_v4;
80 * Our primary task for IPv6.
82 static struct GNUNET_SCHEDULER_Task *hostlist_task_v6;
85 * Our canonical response.
87 static struct MHD_Response *response;
90 * Handle for accessing peerinfo service.
92 static struct GNUNET_PEERINFO_Handle *peerinfo;
95 * Set if we are allowed to advertise our hostlist to others.
97 static int advertising;
100 * Buffer for the hostlist address
102 static char *hostlist_uri;
106 * Context for #host_processor().
111 * Iterator used to build @e data (NULL when done).
113 struct GNUNET_PEERINFO_IteratorContext *pitr;
116 * Place where we accumulate all of the HELLO messages.
121 * Number of bytes in @e data.
129 * NULL if we are not currenlty iterating over peer information.
131 static struct HostSet *builder;
135 * Add headers to a request indicating that we allow Cross-Origin Resource
138 * @param response response to add headers to
141 add_cors_headers (struct MHD_Response *response)
143 MHD_add_response_header (response,
144 "Access-Control-Allow-Origin",
146 MHD_add_response_header (response,
147 "Access-Control-Allow-Methods",
149 MHD_add_response_header (response,
150 "Access-Control-Max-Age",
156 * Function that assembles our response.
161 if (NULL != response)
162 MHD_destroy_response (response);
163 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
164 "Creating hostlist response with %u bytes\n",
165 (unsigned int) builder->size);
167 MHD_create_response_from_buffer (builder->size,
169 MHD_RESPMEM_MUST_FREE);
170 add_cors_headers (response);
171 if ((NULL == daemon_handle_v4) && (NULL == daemon_handle_v6))
173 MHD_destroy_response (response);
176 GNUNET_STATISTICS_set (stats, gettext_noop ("bytes in hostlist"),
177 builder->size, GNUNET_YES);
178 GNUNET_free (builder);
184 * Set @a cls to #GNUNET_YES (we have an address!).
186 * @param cls closure, an `int *`
187 * @param address the address (ignored)
188 * @param expiration expiration time (call is ignored if this is in the past)
189 * @return #GNUNET_SYSERR to stop iterating (unless expiration has occured)
192 check_has_addr (void *cls,
193 const struct GNUNET_HELLO_Address *address,
194 struct GNUNET_TIME_Absolute expiration)
198 if (0 == GNUNET_TIME_absolute_get_remaining (expiration).rel_value_us)
200 GNUNET_STATISTICS_update (stats,
201 gettext_noop ("expired addresses encountered"), 1,
203 return GNUNET_YES; /* ignore this address */
206 return GNUNET_SYSERR;
211 * Callback that processes each of the known HELLOs for the
212 * hostlist response construction.
214 * @param cls closure, NULL
215 * @param peer id of the peer, NULL for last call
216 * @param hello hello message for the peer (can be NULL)
217 * @param err_msg message
220 host_processor (void *cls,
221 const struct GNUNET_PeerIdentity *peer,
222 const struct GNUNET_HELLO_Message *hello,
231 GNUNET_assert (NULL == peer);
232 builder->pitr = NULL;
233 GNUNET_free_non_null (builder->data);
234 GNUNET_free (builder);
236 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
237 _("Error in communication with PEERINFO service: %s\n"),
243 builder->pitr = NULL;
249 has_addr = GNUNET_NO;
250 GNUNET_HELLO_iterate_addresses (hello,
254 if (GNUNET_NO == has_addr)
256 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
257 "HELLO for peer `%4s' has no address, not suitable for hostlist!\n",
259 GNUNET_STATISTICS_update (stats,
261 ("HELLOs without addresses encountered (ignored)"),
266 s = GNUNET_HELLO_size (hello);
267 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
268 "Received %u bytes of `%s' from peer `%s' for hostlist.\n",
272 if ( (old + s >= GNUNET_MAX_MALLOC_CHECKED) ||
273 (old + s >= MAX_BYTES_PER_HOSTLISTS) )
275 /* too large, skip! */
276 GNUNET_STATISTICS_update (stats,
278 ("bytes not included in hostlist (size limit)"),
282 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
283 "Adding peer `%s' to hostlist (%u bytes)\n",
286 GNUNET_array_grow (builder->data,
289 GNUNET_memcpy (&builder->data[old],
296 * Hostlist access policy (very permissive, allows everything).
297 * Returns #MHD_NO only if we are not yet ready to serve.
300 * @param addr address information from the client
301 * @param addrlen length of @a addr
302 * @return #MHD_YES if connection is allowed, #MHD_NO if not (we are not ready)
305 accept_policy_callback (void *cls,
306 const struct sockaddr *addr,
309 if (NULL == response)
311 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
312 "Received request for hostlist, but I am not yet ready; rejecting!\n");
315 return MHD_YES; /* accept all */
320 * Main request handler.
322 * @param cls argument given together with the function
323 * pointer when the handler was registered with MHD
325 * @param url the requested url
326 * @param method the HTTP method used (#MHD_HTTP_METHOD_GET,
327 * #MHD_HTTP_METHOD_PUT, etc.)
328 * @param version the HTTP version string (i.e.
329 * #MHD_HTTP_VERSION_1_1)
330 * @param upload_data the data being uploaded (excluding HEADERS,
331 * for a POST that fits into memory and that is encoded
332 * with a supported encoding, the POST data will NOT be
333 * given in upload_data and is instead available as
334 * part of #MHD_get_connection_values; very large POST
335 * data *will* be made available incrementally in
337 * @param upload_data_size set initially to the size of the
338 * @a upload_data provided; the method must update this
339 * value to the number of bytes NOT processed;
340 * @param con_cls pointer that the callback can set to some
341 * address and that will be preserved by MHD for future
342 * calls for this request; since the access handler may
343 * be called many times (i.e., for a PUT/POST operation
344 * with plenty of upload data) this allows the application
345 * to easily associate some request-specific state.
346 * If necessary, this state can be cleaned up in the
347 * global #MHD_RequestCompletedCallback (which
348 * can be set with the #MHD_OPTION_NOTIFY_COMPLETED).
349 * Initially, `*con_cls` will be NULL.
350 * @return #MHD_YES if the connection was handled successfully,
351 * #MHD_NO if the socket must be closed due to a serios
352 * error while handling the request
355 access_handler_callback (void *cls,
356 struct MHD_Connection *connection,
360 const char *upload_data,
361 size_t *upload_data_size,
366 /* CORS pre-flight request */
367 if (0 == strcmp (MHD_HTTP_METHOD_OPTIONS, method))
369 struct MHD_Response *options_response;
372 options_response = MHD_create_response_from_buffer (0, NULL,
373 MHD_RESPMEM_PERSISTENT);
374 add_cors_headers(options_response);
375 rc = MHD_queue_response (connection, MHD_HTTP_OK, options_response);
376 MHD_destroy_response (options_response);
379 if (0 != strcmp (method, MHD_HTTP_METHOD_GET))
381 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
382 _("Refusing `%s' request to hostlist server\n"), method);
383 GNUNET_STATISTICS_update (stats,
385 ("hostlist requests refused (not HTTP GET)"), 1,
389 if (NULL == *con_cls)
394 if (0 != *upload_data_size)
396 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
397 _("Refusing `%s' request with %llu bytes of upload data\n"),
398 method, (unsigned long long) *upload_data_size);
399 GNUNET_STATISTICS_update (stats,
401 ("hostlist requests refused (upload data)"), 1,
403 return MHD_NO; /* do not support upload data */
405 if (NULL == response)
407 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
408 _("Could not handle hostlist request since I do not have a response yet\n"));
409 GNUNET_STATISTICS_update (stats,
411 ("hostlist requests refused (not ready)"), 1,
413 return MHD_NO; /* internal error, no response yet */
415 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
416 _("Received request for our hostlist\n"));
417 GNUNET_STATISTICS_update (stats,
418 gettext_noop ("hostlist requests processed"),
420 return MHD_queue_response (connection, MHD_HTTP_OK, response);
425 * Handler called by CORE when CORE is ready to transmit message
427 * @param cls closure with the `const struct GNUNET_PeerIdentity *` of
428 * the peer we are sending to
429 * @param size size of buffer to copy message to
430 * @param buf buffer to copy message to
431 * @return number of bytes copied to @a buf
434 adv_transmit (struct GNUNET_MQ_Handle *mq)
436 static uint64_t hostlist_adv_count;
439 size_t uri_size; /* Including \0 termination! */
440 struct GNUNET_MessageHeader *header;
441 struct GNUNET_MQ_Envelope *env;
443 extra = GNUNET_CORE_get_mq_options (GNUNET_YES,
444 GNUNET_CORE_PRIO_BEST_EFFORT,
446 uri_size = strlen (hostlist_uri) + 1;
447 env = GNUNET_MQ_msg_extra (header,
449 GNUNET_MESSAGE_TYPE_HOSTLIST_ADVERTISEMENT);
450 GNUNET_memcpy (&header[1],
453 GNUNET_MQ_env_set_options (env,
458 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
459 "Sent advertisement message: Copied %u bytes into buffer!\n",
460 (unsigned int) uri_size);
461 hostlist_adv_count++;
462 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
463 " # Sent advertisement message: %llu\n",
464 (unsigned long long) hostlist_adv_count);
465 GNUNET_STATISTICS_update (stats,
466 gettext_noop ("# hostlist advertisements send"), 1,
472 * Method called whenever a given peer connects.
475 * @param peer peer identity this notification is about
476 * @param mq queue for transmission to @a peer
477 * @return NULL (must!)
480 connect_handler (void *cls,
481 const struct GNUNET_PeerIdentity *peer,
482 struct GNUNET_MQ_Handle *mq)
488 if (NULL == hostlist_uri)
490 size = strlen (hostlist_uri) + 1;
491 if (size + sizeof (struct GNUNET_MessageHeader) >=
492 GNUNET_SERVER_MAX_MESSAGE_SIZE)
497 size += sizeof (struct GNUNET_MessageHeader);
503 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
504 "Asked CORE to transmit advertisement message with a size of %u bytes to peer `%s'\n",
513 * PEERINFO calls this function to let us know about a possible peer
514 * that we might want to connect to.
516 * @param cls closure (not used)
517 * @param peer potential peer to connect to
518 * @param hello HELLO for this peer (or NULL)
519 * @param err_msg NULL if successful, otherwise contains error message
522 process_notify (void *cls,
523 const struct GNUNET_PeerIdentity *peer,
524 const struct GNUNET_HELLO_Message *hello,
527 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
528 "Peerinfo is notifying us to rebuild our hostlist\n");
530 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
531 _("Error in communication with PEERINFO service: %s\n"),
535 /* restart re-build already in progress ... */
536 if (NULL != builder->pitr)
538 GNUNET_PEERINFO_iterate_cancel (builder->pitr);
539 builder->pitr = NULL;
541 GNUNET_free_non_null (builder->data);
543 builder->data = NULL;
547 builder = GNUNET_new (struct HostSet);
549 GNUNET_assert (NULL != peerinfo);
551 = GNUNET_PEERINFO_iterate (peerinfo,
553 &host_processor, NULL);
558 * Function that queries MHD's select sets and
559 * starts the task waiting for them.
561 static struct GNUNET_SCHEDULER_Task *
562 prepare_daemon (struct MHD_Daemon *daemon_handle);
566 * Call MHD to process pending requests and then go back
567 * and schedule the next run.
569 * @param cls the `struct MHD_Daemon` of the HTTP server to run
572 run_daemon (void *cls)
574 struct MHD_Daemon *daemon_handle = cls;
576 if (daemon_handle == daemon_handle_v4)
577 hostlist_task_v4 = NULL;
579 hostlist_task_v6 = NULL;
580 GNUNET_assert (MHD_YES == MHD_run (daemon_handle));
581 if (daemon_handle == daemon_handle_v4)
582 hostlist_task_v4 = prepare_daemon (daemon_handle);
584 hostlist_task_v6 = prepare_daemon (daemon_handle);
589 * Function that queries MHD's select sets and
590 * starts the task waiting for them.
592 * @param daemon_handle HTTP server to prepare to run
594 static struct GNUNET_SCHEDULER_Task *
595 prepare_daemon (struct MHD_Daemon *daemon_handle)
597 struct GNUNET_SCHEDULER_Task * ret;
601 struct GNUNET_NETWORK_FDSet *wrs;
602 struct GNUNET_NETWORK_FDSet *wws;
604 MHD_UNSIGNED_LONG_LONG timeout;
606 struct GNUNET_TIME_Relative tv;
611 wrs = GNUNET_NETWORK_fdset_create ();
612 wws = GNUNET_NETWORK_fdset_create ();
614 GNUNET_assert (MHD_YES ==
615 MHD_get_fdset (daemon_handle,
616 &rs, &ws, &es, &max));
617 haveto = MHD_get_timeout (daemon_handle, &timeout);
618 if (haveto == MHD_YES)
619 tv.rel_value_us = (uint64_t) timeout * 1000LL;
621 tv = GNUNET_TIME_UNIT_FOREVER_REL;
622 GNUNET_NETWORK_fdset_copy_native (wrs, &rs, max + 1);
623 GNUNET_NETWORK_fdset_copy_native (wws, &ws, max + 1);
624 ret = GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_HIGH,
626 &run_daemon, daemon_handle);
627 GNUNET_NETWORK_fdset_destroy (wrs);
628 GNUNET_NETWORK_fdset_destroy (wws);
634 * Start server offering our hostlist.
636 * @param c configuration to use
637 * @param st statistics handle to use
638 * @param co core handle to use
639 * @param[out] server_ch set to handler for CORE connect events
640 * @param advertise #GNUNET_YES if we should advertise our hostlist
641 * @return #GNUNET_OK on success
644 GNUNET_HOSTLIST_server_start (const struct GNUNET_CONFIGURATION_Handle *c,
645 struct GNUNET_STATISTICS_Handle *st,
646 struct GNUNET_CORE_Handle *co,
647 GNUNET_CORE_ConnecTEventHandler *server_ch,
650 unsigned long long port;
657 struct sockaddr_in v4;
658 struct sockaddr_in6 v6;
659 const struct sockaddr *sa4;
660 const struct sockaddr *sa6;
662 advertising = advertise;
665 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
666 "Advertising not enabled on this hostlist server\n");
670 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
671 "Advertising enabled on this hostlist server\n");
675 peerinfo = GNUNET_PEERINFO_connect (cfg);
676 if (NULL == peerinfo)
678 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
679 _("Could not access PEERINFO service. Exiting.\n"));
680 return GNUNET_SYSERR;
683 GNUNET_CONFIGURATION_get_value_number (cfg,
687 return GNUNET_SYSERR;
688 if ((0 == port) || (port > UINT16_MAX))
690 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
691 _("Invalid port number %llu. Exiting.\n"),
693 return GNUNET_SYSERR;
697 GNUNET_CONFIGURATION_get_value_string (cfg,
701 hostname = GNUNET_RESOLVER_local_fqdn_get ();
702 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
703 _("Hostlist service starts on %s:%llu\n"),
705 if (NULL != hostname)
707 size = strlen (hostname);
708 if (size + 15 > MAX_URL_LEN)
714 GNUNET_asprintf (&hostlist_uri,
715 "http://%s:%u/", hostname,
716 (unsigned int) port);
717 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
718 _("Address to obtain hostlist: `%s'\n"),
721 GNUNET_free (hostname);
724 if (GNUNET_CONFIGURATION_have_value (cfg, "HOSTLIST", "BINDTOIPV4"))
727 GNUNET_CONFIGURATION_get_value_string (cfg, "HOSTLIST",
730 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
731 _("BINDTOIP does not a valid IPv4 address! Ignoring BINDTOIPV4.\n"));
737 if (GNUNET_CONFIGURATION_have_value (cfg,
742 GNUNET_CONFIGURATION_get_value_string (cfg,
747 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
748 _("BINDTOIP does not a valid IPv4 address! Ignoring BINDTOIPV6.\n"));
756 if (1 == inet_pton (AF_INET, ipv4, &i4))
758 memset (&v4, 0, sizeof (v4));
759 v4.sin_family = AF_INET;
761 v4.sin_port = htons (port);
762 #if HAVE_SOCKADDR_IN_SIN_LEN
763 v4.sin_len = sizeof (v4);
765 sa4 = (const struct sockaddr *) &v4;
768 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
769 _("`%s' is not a valid IPv4 address! Ignoring BINDTOIPV4.\n"),
776 if (1 == inet_pton (AF_INET6, ipv6, &i6))
778 memset (&v6, 0, sizeof (v6));
779 v6.sin6_family = AF_INET6;
781 v6.sin6_port = htons (port);
782 #if HAVE_SOCKADDR_IN_SIN_LEN
783 v6.sin6_len = sizeof (v6);
785 sa6 = (const struct sockaddr *) &v6;
788 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
789 _("`%s' is not a valid IPv6 address! Ignoring BINDTOIPV6.\n"),
794 daemon_handle_v6 = MHD_start_daemon (MHD_USE_IPv6 | MHD_USE_DEBUG,
796 &accept_policy_callback, NULL,
797 &access_handler_callback, NULL,
798 MHD_OPTION_CONNECTION_LIMIT,
800 MHD_OPTION_PER_IP_CONNECTION_LIMIT,
802 MHD_OPTION_CONNECTION_TIMEOUT,
804 MHD_OPTION_CONNECTION_MEMORY_LIMIT,
805 (size_t) (16 * 1024),
806 MHD_OPTION_SOCK_ADDR,
809 daemon_handle_v4 = MHD_start_daemon (MHD_NO_FLAG | MHD_USE_DEBUG,
811 &accept_policy_callback, NULL,
812 &access_handler_callback, NULL,
813 MHD_OPTION_CONNECTION_LIMIT,
815 MHD_OPTION_PER_IP_CONNECTION_LIMIT,
817 MHD_OPTION_CONNECTION_TIMEOUT,
819 MHD_OPTION_CONNECTION_MEMORY_LIMIT,
820 (size_t) (16 * 1024),
821 MHD_OPTION_SOCK_ADDR,
825 if ( (NULL == daemon_handle_v6) &&
826 (NULL == daemon_handle_v4) )
828 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
829 _("Could not start hostlist HTTP server on port %u\n"),
830 (unsigned short) port);
831 return GNUNET_SYSERR;
835 *server_ch = &connect_handler;
836 if (NULL != daemon_handle_v4)
837 hostlist_task_v4 = prepare_daemon (daemon_handle_v4);
838 if (NULL != daemon_handle_v6)
839 hostlist_task_v6 = prepare_daemon (daemon_handle_v6);
840 notify = GNUNET_PEERINFO_notify (cfg,
842 &process_notify, NULL);
848 * Stop server offering our hostlist.
851 GNUNET_HOSTLIST_server_stop ()
853 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
854 "Hostlist server shutdown\n");
855 if (NULL != hostlist_task_v6)
857 GNUNET_SCHEDULER_cancel (hostlist_task_v6);
858 hostlist_task_v6 = NULL;
860 if (NULL != hostlist_task_v4)
862 GNUNET_SCHEDULER_cancel (hostlist_task_v4);
863 hostlist_task_v4 = NULL;
865 if (NULL != daemon_handle_v4)
867 MHD_stop_daemon (daemon_handle_v4);
868 daemon_handle_v4 = NULL;
870 if (NULL != daemon_handle_v6)
872 MHD_stop_daemon (daemon_handle_v6);
873 daemon_handle_v6 = NULL;
875 if (NULL != response)
877 MHD_destroy_response (response);
882 GNUNET_PEERINFO_notify_cancel (notify);
887 if (NULL != builder->pitr)
889 GNUNET_PEERINFO_iterate_cancel (builder->pitr);
890 builder->pitr = NULL;
892 GNUNET_free_non_null (builder->data);
893 GNUNET_free (builder);
896 if (NULL != peerinfo)
898 GNUNET_PEERINFO_disconnect (peerinfo);
906 /* end of gnunet-daemon-hostlist_server.c */