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 it
6 under the terms of the GNU 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.
17 * @file hostlist/gnunet-daemon-hostlist_server.c
18 * @author Christian Grothoff
19 * @author Matthias Wachs
20 * @author David Barksdale
21 * @brief application to provide an integrated hostlist HTTP server
24 #include <microhttpd.h>
25 #include "gnunet-daemon-hostlist_server.h"
26 #include "gnunet_hello_lib.h"
27 #include "gnunet_peerinfo_service.h"
28 #include "gnunet-daemon-hostlist.h"
29 #include "gnunet_resolver_service.h"
33 * How long until our hostlist advertisment transmission via CORE should
36 #define GNUNET_ADV_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 5)
40 * Handle to the HTTP server as provided by libmicrohttpd for IPv6.
42 static struct MHD_Daemon *daemon_handle_v6;
45 * Handle to the HTTP server as provided by libmicrohttpd for IPv4.
47 static struct MHD_Daemon *daemon_handle_v4;
52 static const struct GNUNET_CONFIGURATION_Handle *cfg;
55 * For keeping statistics.
57 static struct GNUNET_STATISTICS_Handle *stats;
60 * Handle to the core service (NULL until we've connected to it).
62 static struct GNUNET_CORE_Handle *core;
65 * Handle to the peerinfo notify service (NULL until we've connected to it).
67 static struct GNUNET_PEERINFO_NotifyContext *notify;
70 * Our primary task for IPv4.
72 static struct GNUNET_SCHEDULER_Task *hostlist_task_v4;
75 * Our primary task for IPv6.
77 static struct GNUNET_SCHEDULER_Task *hostlist_task_v6;
80 * Our canonical response.
82 static struct MHD_Response *response;
85 * Handle for accessing peerinfo service.
87 static struct GNUNET_PEERINFO_Handle *peerinfo;
90 * Set if we are allowed to advertise our hostlist to others.
92 static int advertising;
95 * Buffer for the hostlist address
97 static char *hostlist_uri;
101 * Context for #host_processor().
106 * Iterator used to build @e data (NULL when done).
108 struct GNUNET_PEERINFO_IteratorContext *pitr;
111 * Place where we accumulate all of the HELLO messages.
116 * Number of bytes in @e data.
124 * NULL if we are not currenlty iterating over peer information.
126 static struct HostSet *builder;
130 * Add headers to a request indicating that we allow Cross-Origin Resource
133 * @param response response to add headers to
136 add_cors_headers (struct MHD_Response *response)
138 MHD_add_response_header (response,
139 "Access-Control-Allow-Origin",
141 MHD_add_response_header (response,
142 "Access-Control-Allow-Methods",
144 MHD_add_response_header (response,
145 "Access-Control-Max-Age",
151 * Function that assembles our response.
156 if (NULL != response)
157 MHD_destroy_response (response);
158 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
159 "Creating hostlist response with %u bytes\n",
160 (unsigned int) builder->size);
162 MHD_create_response_from_buffer (builder->size,
164 MHD_RESPMEM_MUST_FREE);
165 add_cors_headers (response);
166 if ((NULL == daemon_handle_v4) && (NULL == daemon_handle_v6))
168 MHD_destroy_response (response);
171 GNUNET_STATISTICS_set (stats, gettext_noop ("bytes in hostlist"),
172 builder->size, GNUNET_YES);
173 GNUNET_free (builder);
179 * Set @a cls to #GNUNET_YES (we have an address!).
181 * @param cls closure, an `int *`
182 * @param address the address (ignored)
183 * @param expiration expiration time (call is ignored if this is in the past)
184 * @return #GNUNET_SYSERR to stop iterating (unless expiration has occured)
187 check_has_addr (void *cls,
188 const struct GNUNET_HELLO_Address *address,
189 struct GNUNET_TIME_Absolute expiration)
193 if (0 == GNUNET_TIME_absolute_get_remaining (expiration).rel_value_us)
195 GNUNET_STATISTICS_update (stats,
196 gettext_noop ("expired addresses encountered"), 1,
198 return GNUNET_YES; /* ignore this address */
201 return GNUNET_SYSERR;
206 * Callback that processes each of the known HELLOs for the
207 * hostlist response construction.
209 * @param cls closure, NULL
210 * @param peer id of the peer, NULL for last call
211 * @param hello hello message for the peer (can be NULL)
212 * @param err_msg message
215 host_processor (void *cls,
216 const struct GNUNET_PeerIdentity *peer,
217 const struct GNUNET_HELLO_Message *hello,
226 GNUNET_assert (NULL == peer);
227 builder->pitr = NULL;
228 GNUNET_free_non_null (builder->data);
229 GNUNET_free (builder);
231 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
232 _("Error in communication with PEERINFO service: %s\n"),
238 builder->pitr = NULL;
244 has_addr = GNUNET_NO;
245 GNUNET_HELLO_iterate_addresses (hello,
249 if (GNUNET_NO == has_addr)
251 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
252 "HELLO for peer `%4s' has no address, not suitable for hostlist!\n",
254 GNUNET_STATISTICS_update (stats,
256 ("HELLOs without addresses encountered (ignored)"),
261 s = GNUNET_HELLO_size (hello);
262 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
263 "Received %u bytes of `%s' from peer `%s' for hostlist.\n",
267 if ( (old + s >= GNUNET_MAX_MALLOC_CHECKED) ||
268 (old + s >= MAX_BYTES_PER_HOSTLISTS) )
270 /* too large, skip! */
271 GNUNET_STATISTICS_update (stats,
273 ("bytes not included in hostlist (size limit)"),
277 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
278 "Adding peer `%s' to hostlist (%u bytes)\n",
281 GNUNET_array_grow (builder->data,
284 GNUNET_memcpy (&builder->data[old],
291 * Hostlist access policy (very permissive, allows everything).
292 * Returns #MHD_NO only if we are not yet ready to serve.
295 * @param addr address information from the client
296 * @param addrlen length of @a addr
297 * @return #MHD_YES if connection is allowed, #MHD_NO if not (we are not ready)
300 accept_policy_callback (void *cls,
301 const struct sockaddr *addr,
304 if (NULL == response)
306 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
307 "Received request for hostlist, but I am not yet ready; rejecting!\n");
310 return MHD_YES; /* accept all */
315 * Main request handler.
317 * @param cls argument given together with the function
318 * pointer when the handler was registered with MHD
320 * @param url the requested url
321 * @param method the HTTP method used (#MHD_HTTP_METHOD_GET,
322 * #MHD_HTTP_METHOD_PUT, etc.)
323 * @param version the HTTP version string (i.e.
324 * #MHD_HTTP_VERSION_1_1)
325 * @param upload_data the data being uploaded (excluding HEADERS,
326 * for a POST that fits into memory and that is encoded
327 * with a supported encoding, the POST data will NOT be
328 * given in upload_data and is instead available as
329 * part of #MHD_get_connection_values; very large POST
330 * data *will* be made available incrementally in
332 * @param upload_data_size set initially to the size of the
333 * @a upload_data provided; the method must update this
334 * value to the number of bytes NOT processed;
335 * @param con_cls pointer that the callback can set to some
336 * address and that will be preserved by MHD for future
337 * calls for this request; since the access handler may
338 * be called many times (i.e., for a PUT/POST operation
339 * with plenty of upload data) this allows the application
340 * to easily associate some request-specific state.
341 * If necessary, this state can be cleaned up in the
342 * global #MHD_RequestCompletedCallback (which
343 * can be set with the #MHD_OPTION_NOTIFY_COMPLETED).
344 * Initially, `*con_cls` will be NULL.
345 * @return #MHD_YES if the connection was handled successfully,
346 * #MHD_NO if the socket must be closed due to a serios
347 * error while handling the request
350 access_handler_callback (void *cls,
351 struct MHD_Connection *connection,
355 const char *upload_data,
356 size_t *upload_data_size,
361 /* CORS pre-flight request */
362 if (0 == strcmp (MHD_HTTP_METHOD_OPTIONS, method))
364 struct MHD_Response *options_response;
367 options_response = MHD_create_response_from_buffer (0, NULL,
368 MHD_RESPMEM_PERSISTENT);
369 add_cors_headers(options_response);
370 rc = MHD_queue_response (connection, MHD_HTTP_OK, options_response);
371 MHD_destroy_response (options_response);
374 if (0 != strcmp (method, MHD_HTTP_METHOD_GET))
376 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
377 _("Refusing `%s' request to hostlist server\n"), method);
378 GNUNET_STATISTICS_update (stats,
380 ("hostlist requests refused (not HTTP GET)"), 1,
384 if (NULL == *con_cls)
389 if (0 != *upload_data_size)
391 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
392 _("Refusing `%s' request with %llu bytes of upload data\n"),
393 method, (unsigned long long) *upload_data_size);
394 GNUNET_STATISTICS_update (stats,
396 ("hostlist requests refused (upload data)"), 1,
398 return MHD_NO; /* do not support upload data */
400 if (NULL == response)
402 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
403 _("Could not handle hostlist request since I do not have a response yet\n"));
404 GNUNET_STATISTICS_update (stats,
406 ("hostlist requests refused (not ready)"), 1,
408 return MHD_NO; /* internal error, no response yet */
410 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
411 _("Received request for our hostlist\n"));
412 GNUNET_STATISTICS_update (stats,
413 gettext_noop ("hostlist requests processed"),
415 return MHD_queue_response (connection, MHD_HTTP_OK, response);
420 * Handler called by CORE when CORE is ready to transmit message
422 * @param cls closure with the `const struct GNUNET_PeerIdentity *` of
423 * the peer we are sending to
424 * @param size size of buffer to copy message to
425 * @param buf buffer to copy message to
426 * @return number of bytes copied to @a buf
429 adv_transmit (struct GNUNET_MQ_Handle *mq)
431 static uint64_t hostlist_adv_count;
434 size_t uri_size; /* Including \0 termination! */
435 struct GNUNET_MessageHeader *header;
436 struct GNUNET_MQ_Envelope *env;
438 extra = GNUNET_CORE_get_mq_options (GNUNET_YES,
439 GNUNET_CORE_PRIO_BEST_EFFORT,
441 uri_size = strlen (hostlist_uri) + 1;
442 env = GNUNET_MQ_msg_extra (header,
444 GNUNET_MESSAGE_TYPE_HOSTLIST_ADVERTISEMENT);
445 GNUNET_memcpy (&header[1],
448 GNUNET_MQ_env_set_options (env,
453 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
454 "Sent advertisement message: Copied %u bytes into buffer!\n",
455 (unsigned int) uri_size);
456 hostlist_adv_count++;
457 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
458 " # Sent advertisement message: %llu\n",
459 (unsigned long long) hostlist_adv_count);
460 GNUNET_STATISTICS_update (stats,
461 gettext_noop ("# hostlist advertisements send"), 1,
467 * Method called whenever a given peer connects.
470 * @param peer peer identity this notification is about
471 * @param mq queue for transmission to @a peer
472 * @return NULL (must!)
475 connect_handler (void *cls,
476 const struct GNUNET_PeerIdentity *peer,
477 struct GNUNET_MQ_Handle *mq)
483 if (NULL == hostlist_uri)
485 size = strlen (hostlist_uri) + 1;
486 if (size + sizeof (struct GNUNET_MessageHeader) >=
487 GNUNET_MAX_MESSAGE_SIZE)
492 size += sizeof (struct GNUNET_MessageHeader);
498 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
499 "Asked CORE to transmit advertisement message with a size of %u bytes to peer `%s'\n",
508 * PEERINFO calls this function to let us know about a possible peer
509 * that we might want to connect to.
511 * @param cls closure (not used)
512 * @param peer potential peer to connect to
513 * @param hello HELLO for this peer (or NULL)
514 * @param err_msg NULL if successful, otherwise contains error message
517 process_notify (void *cls,
518 const struct GNUNET_PeerIdentity *peer,
519 const struct GNUNET_HELLO_Message *hello,
522 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
523 "Peerinfo is notifying us to rebuild our hostlist\n");
525 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
526 _("Error in communication with PEERINFO service: %s\n"),
530 /* restart re-build already in progress ... */
531 if (NULL != builder->pitr)
533 GNUNET_PEERINFO_iterate_cancel (builder->pitr);
534 builder->pitr = NULL;
536 GNUNET_free_non_null (builder->data);
538 builder->data = NULL;
542 builder = GNUNET_new (struct HostSet);
544 GNUNET_assert (NULL != peerinfo);
546 = GNUNET_PEERINFO_iterate (peerinfo,
548 &host_processor, NULL);
553 * Function that queries MHD's select sets and
554 * starts the task waiting for them.
556 static struct GNUNET_SCHEDULER_Task *
557 prepare_daemon (struct MHD_Daemon *daemon_handle);
561 * Call MHD to process pending requests and then go back
562 * and schedule the next run.
564 * @param cls the `struct MHD_Daemon` of the HTTP server to run
567 run_daemon (void *cls)
569 struct MHD_Daemon *daemon_handle = cls;
571 if (daemon_handle == daemon_handle_v4)
572 hostlist_task_v4 = NULL;
574 hostlist_task_v6 = NULL;
575 GNUNET_assert (MHD_YES == MHD_run (daemon_handle));
576 if (daemon_handle == daemon_handle_v4)
577 hostlist_task_v4 = prepare_daemon (daemon_handle);
579 hostlist_task_v6 = prepare_daemon (daemon_handle);
584 * Function that queries MHD's select sets and
585 * starts the task waiting for them.
587 * @param daemon_handle HTTP server to prepare to run
589 static struct GNUNET_SCHEDULER_Task *
590 prepare_daemon (struct MHD_Daemon *daemon_handle)
592 struct GNUNET_SCHEDULER_Task * ret;
596 struct GNUNET_NETWORK_FDSet *wrs;
597 struct GNUNET_NETWORK_FDSet *wws;
599 MHD_UNSIGNED_LONG_LONG timeout;
601 struct GNUNET_TIME_Relative tv;
606 wrs = GNUNET_NETWORK_fdset_create ();
607 wws = GNUNET_NETWORK_fdset_create ();
609 GNUNET_assert (MHD_YES ==
610 MHD_get_fdset (daemon_handle,
611 &rs, &ws, &es, &max));
612 haveto = MHD_get_timeout (daemon_handle, &timeout);
613 if (haveto == MHD_YES)
614 tv.rel_value_us = (uint64_t) timeout * 1000LL;
616 tv = GNUNET_TIME_UNIT_FOREVER_REL;
617 GNUNET_NETWORK_fdset_copy_native (wrs, &rs, max + 1);
618 GNUNET_NETWORK_fdset_copy_native (wws, &ws, max + 1);
619 ret = GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_HIGH,
621 &run_daemon, daemon_handle);
622 GNUNET_NETWORK_fdset_destroy (wrs);
623 GNUNET_NETWORK_fdset_destroy (wws);
629 * Start server offering our hostlist.
631 * @param c configuration to use
632 * @param st statistics handle to use
633 * @param co core handle to use
634 * @param[out] server_ch set to handler for CORE connect events
635 * @param advertise #GNUNET_YES if we should advertise our hostlist
636 * @return #GNUNET_OK on success
639 GNUNET_HOSTLIST_server_start (const struct GNUNET_CONFIGURATION_Handle *c,
640 struct GNUNET_STATISTICS_Handle *st,
641 struct GNUNET_CORE_Handle *co,
642 GNUNET_CORE_ConnectEventHandler *server_ch,
645 unsigned long long port;
652 struct sockaddr_in v4;
653 struct sockaddr_in6 v6;
654 const struct sockaddr *sa4;
655 const struct sockaddr *sa6;
657 advertising = advertise;
660 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
661 "Advertising not enabled on this hostlist server\n");
665 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
666 "Advertising enabled on this hostlist server\n");
670 peerinfo = GNUNET_PEERINFO_connect (cfg);
671 if (NULL == peerinfo)
673 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
674 _("Could not access PEERINFO service. Exiting.\n"));
675 return GNUNET_SYSERR;
678 GNUNET_CONFIGURATION_get_value_number (cfg,
682 return GNUNET_SYSERR;
683 if ((0 == port) || (port > UINT16_MAX))
685 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
686 _("Invalid port number %llu. Exiting.\n"),
688 return GNUNET_SYSERR;
692 GNUNET_CONFIGURATION_get_value_string (cfg,
696 hostname = GNUNET_RESOLVER_local_fqdn_get ();
697 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
698 _("Hostlist service starts on %s:%llu\n"),
700 if (NULL != hostname)
702 size = strlen (hostname);
703 if (size + 15 > MAX_URL_LEN)
709 GNUNET_asprintf (&hostlist_uri,
710 "http://%s:%u/", hostname,
711 (unsigned int) port);
712 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
713 _("Address to obtain hostlist: `%s'\n"),
716 GNUNET_free (hostname);
719 if (GNUNET_CONFIGURATION_have_value (cfg, "HOSTLIST", "BINDTOIPV4"))
722 GNUNET_CONFIGURATION_get_value_string (cfg, "HOSTLIST",
725 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
726 _("BINDTOIP does not a valid IPv4 address! Ignoring BINDTOIPV4.\n"));
732 if (GNUNET_CONFIGURATION_have_value (cfg,
737 GNUNET_CONFIGURATION_get_value_string (cfg,
742 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
743 _("BINDTOIP does not a valid IPv4 address! Ignoring BINDTOIPV6.\n"));
751 if (1 == inet_pton (AF_INET, ipv4, &i4))
753 memset (&v4, 0, sizeof (v4));
754 v4.sin_family = AF_INET;
756 v4.sin_port = htons (port);
757 #if HAVE_SOCKADDR_IN_SIN_LEN
758 v4.sin_len = sizeof (v4);
760 sa4 = (const struct sockaddr *) &v4;
763 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
764 _("`%s' is not a valid IPv4 address! Ignoring BINDTOIPV4.\n"),
771 if (1 == inet_pton (AF_INET6, ipv6, &i6))
773 memset (&v6, 0, sizeof (v6));
774 v6.sin6_family = AF_INET6;
776 v6.sin6_port = htons (port);
777 #if HAVE_SOCKADDR_IN_SIN_LEN
778 v6.sin6_len = sizeof (v6);
780 sa6 = (const struct sockaddr *) &v6;
783 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
784 _("`%s' is not a valid IPv6 address! Ignoring BINDTOIPV6.\n"),
789 daemon_handle_v6 = MHD_start_daemon (MHD_USE_IPv6 | MHD_USE_DEBUG,
791 &accept_policy_callback, NULL,
792 &access_handler_callback, NULL,
793 MHD_OPTION_CONNECTION_LIMIT,
795 MHD_OPTION_PER_IP_CONNECTION_LIMIT,
797 MHD_OPTION_CONNECTION_TIMEOUT,
799 MHD_OPTION_CONNECTION_MEMORY_LIMIT,
800 (size_t) (16 * 1024),
801 MHD_OPTION_SOCK_ADDR,
804 daemon_handle_v4 = MHD_start_daemon (MHD_NO_FLAG | MHD_USE_DEBUG,
806 &accept_policy_callback, NULL,
807 &access_handler_callback, NULL,
808 MHD_OPTION_CONNECTION_LIMIT,
810 MHD_OPTION_PER_IP_CONNECTION_LIMIT,
812 MHD_OPTION_CONNECTION_TIMEOUT,
814 MHD_OPTION_CONNECTION_MEMORY_LIMIT,
815 (size_t) (16 * 1024),
816 MHD_OPTION_SOCK_ADDR,
820 if ( (NULL == daemon_handle_v6) &&
821 (NULL == daemon_handle_v4) )
823 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
824 _("Could not start hostlist HTTP server on port %u\n"),
825 (unsigned short) port);
826 return GNUNET_SYSERR;
830 *server_ch = &connect_handler;
831 if (NULL != daemon_handle_v4)
832 hostlist_task_v4 = prepare_daemon (daemon_handle_v4);
833 if (NULL != daemon_handle_v6)
834 hostlist_task_v6 = prepare_daemon (daemon_handle_v6);
835 notify = GNUNET_PEERINFO_notify (cfg,
837 &process_notify, NULL);
843 * Stop server offering our hostlist.
846 GNUNET_HOSTLIST_server_stop ()
848 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
849 "Hostlist server shutdown\n");
850 if (NULL != hostlist_task_v6)
852 GNUNET_SCHEDULER_cancel (hostlist_task_v6);
853 hostlist_task_v6 = NULL;
855 if (NULL != hostlist_task_v4)
857 GNUNET_SCHEDULER_cancel (hostlist_task_v4);
858 hostlist_task_v4 = NULL;
860 if (NULL != daemon_handle_v4)
862 MHD_stop_daemon (daemon_handle_v4);
863 daemon_handle_v4 = NULL;
865 if (NULL != daemon_handle_v6)
867 MHD_stop_daemon (daemon_handle_v6);
868 daemon_handle_v6 = NULL;
870 if (NULL != response)
872 MHD_destroy_response (response);
877 GNUNET_PEERINFO_notify_cancel (notify);
882 if (NULL != builder->pitr)
884 GNUNET_PEERINFO_iterate_cancel (builder->pitr);
885 builder->pitr = NULL;
887 GNUNET_free_non_null (builder->data);
888 GNUNET_free (builder);
891 if (NULL != peerinfo)
893 GNUNET_PEERINFO_disconnect (peerinfo);
901 /* end of gnunet-daemon-hostlist_server.c */