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 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/>.
20 * @file hostlist/gnunet-daemon-hostlist_server.c
21 * @author Christian Grothoff
22 * @author Matthias Wachs
23 * @author David Barksdale
24 * @brief application to provide an integrated hostlist HTTP server
27 #include <microhttpd.h>
28 #include "gnunet-daemon-hostlist_server.h"
29 #include "gnunet_hello_lib.h"
30 #include "gnunet_peerinfo_service.h"
31 #include "gnunet-daemon-hostlist.h"
32 #include "gnunet_resolver_service.h"
36 * How long until our hostlist advertisment transmission via CORE should
39 #define GNUNET_ADV_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 5)
43 * Handle to the HTTP server as provided by libmicrohttpd for IPv6.
45 static struct MHD_Daemon *daemon_handle_v6;
48 * Handle to the HTTP server as provided by libmicrohttpd for IPv4.
50 static struct MHD_Daemon *daemon_handle_v4;
55 static const struct GNUNET_CONFIGURATION_Handle *cfg;
58 * For keeping statistics.
60 static struct GNUNET_STATISTICS_Handle *stats;
63 * Handle to the core service (NULL until we've connected to it).
65 static struct GNUNET_CORE_Handle *core;
68 * Handle to the peerinfo notify service (NULL until we've connected to it).
70 static struct GNUNET_PEERINFO_NotifyContext *notify;
73 * Our primary task for IPv4.
75 static struct GNUNET_SCHEDULER_Task *hostlist_task_v4;
78 * Our primary task for IPv6.
80 static struct GNUNET_SCHEDULER_Task *hostlist_task_v6;
83 * Our canonical response.
85 static struct MHD_Response *response;
88 * Handle for accessing peerinfo service.
90 static struct GNUNET_PEERINFO_Handle *peerinfo;
93 * Set if we are allowed to advertise our hostlist to others.
95 static int advertising;
98 * Buffer for the hostlist address
100 static char *hostlist_uri;
104 * Context for #host_processor().
109 * Iterator used to build @e data (NULL when done).
111 struct GNUNET_PEERINFO_IteratorContext *pitr;
114 * Place where we accumulate all of the HELLO messages.
119 * Number of bytes in @e data.
127 * NULL if we are not currenlty iterating over peer information.
129 static struct HostSet *builder;
133 * Add headers to a request indicating that we allow Cross-Origin Resource
136 * @param response response to add headers to
139 add_cors_headers (struct MHD_Response *response)
141 MHD_add_response_header (response,
142 "Access-Control-Allow-Origin",
144 MHD_add_response_header (response,
145 "Access-Control-Allow-Methods",
147 MHD_add_response_header (response,
148 "Access-Control-Max-Age",
154 * Function that assembles our response.
159 if (NULL != response)
160 MHD_destroy_response (response);
161 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
162 "Creating hostlist response with %u bytes\n",
163 (unsigned int) builder->size);
165 MHD_create_response_from_buffer (builder->size,
167 MHD_RESPMEM_MUST_FREE);
168 add_cors_headers (response);
169 if ((NULL == daemon_handle_v4) && (NULL == daemon_handle_v6))
171 MHD_destroy_response (response);
174 GNUNET_STATISTICS_set (stats, gettext_noop ("bytes in hostlist"),
175 builder->size, GNUNET_YES);
176 GNUNET_free (builder);
182 * Set @a cls to #GNUNET_YES (we have an address!).
184 * @param cls closure, an `int *`
185 * @param address the address (ignored)
186 * @param expiration expiration time (call is ignored if this is in the past)
187 * @return #GNUNET_SYSERR to stop iterating (unless expiration has occured)
190 check_has_addr (void *cls,
191 const struct GNUNET_HELLO_Address *address,
192 struct GNUNET_TIME_Absolute expiration)
196 if (0 == GNUNET_TIME_absolute_get_remaining (expiration).rel_value_us)
198 GNUNET_STATISTICS_update (stats,
199 gettext_noop ("expired addresses encountered"), 1,
201 return GNUNET_YES; /* ignore this address */
204 return GNUNET_SYSERR;
209 * Callback that processes each of the known HELLOs for the
210 * hostlist response construction.
212 * @param cls closure, NULL
213 * @param peer id of the peer, NULL for last call
214 * @param hello hello message for the peer (can be NULL)
215 * @param err_msg message
218 host_processor (void *cls,
219 const struct GNUNET_PeerIdentity *peer,
220 const struct GNUNET_HELLO_Message *hello,
229 GNUNET_assert (NULL == peer);
230 builder->pitr = NULL;
231 GNUNET_free_non_null (builder->data);
232 GNUNET_free (builder);
234 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
235 _("Error in communication with PEERINFO service: %s\n"),
241 builder->pitr = NULL;
247 has_addr = GNUNET_NO;
248 GNUNET_HELLO_iterate_addresses (hello,
252 if (GNUNET_NO == has_addr)
254 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
255 "HELLO for peer `%4s' has no address, not suitable for hostlist!\n",
257 GNUNET_STATISTICS_update (stats,
259 ("HELLOs without addresses encountered (ignored)"),
264 s = GNUNET_HELLO_size (hello);
265 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
266 "Received %u bytes of `%s' from peer `%s' for hostlist.\n",
270 if ( (old + s >= GNUNET_MAX_MALLOC_CHECKED) ||
271 (old + s >= MAX_BYTES_PER_HOSTLISTS) )
273 /* too large, skip! */
274 GNUNET_STATISTICS_update (stats,
276 ("bytes not included in hostlist (size limit)"),
280 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
281 "Adding peer `%s' to hostlist (%u bytes)\n",
284 GNUNET_array_grow (builder->data,
287 GNUNET_memcpy (&builder->data[old],
294 * Hostlist access policy (very permissive, allows everything).
295 * Returns #MHD_NO only if we are not yet ready to serve.
298 * @param addr address information from the client
299 * @param addrlen length of @a addr
300 * @return #MHD_YES if connection is allowed, #MHD_NO if not (we are not ready)
303 accept_policy_callback (void *cls,
304 const struct sockaddr *addr,
307 if (NULL == response)
309 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
310 "Received request for hostlist, but I am not yet ready; rejecting!\n");
313 return MHD_YES; /* accept all */
318 * Main request handler.
320 * @param cls argument given together with the function
321 * pointer when the handler was registered with MHD
323 * @param url the requested url
324 * @param method the HTTP method used (#MHD_HTTP_METHOD_GET,
325 * #MHD_HTTP_METHOD_PUT, etc.)
326 * @param version the HTTP version string (i.e.
327 * #MHD_HTTP_VERSION_1_1)
328 * @param upload_data the data being uploaded (excluding HEADERS,
329 * for a POST that fits into memory and that is encoded
330 * with a supported encoding, the POST data will NOT be
331 * given in upload_data and is instead available as
332 * part of #MHD_get_connection_values; very large POST
333 * data *will* be made available incrementally in
335 * @param upload_data_size set initially to the size of the
336 * @a upload_data provided; the method must update this
337 * value to the number of bytes NOT processed;
338 * @param con_cls pointer that the callback can set to some
339 * address and that will be preserved by MHD for future
340 * calls for this request; since the access handler may
341 * be called many times (i.e., for a PUT/POST operation
342 * with plenty of upload data) this allows the application
343 * to easily associate some request-specific state.
344 * If necessary, this state can be cleaned up in the
345 * global #MHD_RequestCompletedCallback (which
346 * can be set with the #MHD_OPTION_NOTIFY_COMPLETED).
347 * Initially, `*con_cls` will be NULL.
348 * @return #MHD_YES if the connection was handled successfully,
349 * #MHD_NO if the socket must be closed due to a serios
350 * error while handling the request
353 access_handler_callback (void *cls,
354 struct MHD_Connection *connection,
358 const char *upload_data,
359 size_t *upload_data_size,
364 /* CORS pre-flight request */
365 if (0 == strcmp (MHD_HTTP_METHOD_OPTIONS, method))
367 struct MHD_Response *options_response;
370 options_response = MHD_create_response_from_buffer (0, NULL,
371 MHD_RESPMEM_PERSISTENT);
372 add_cors_headers(options_response);
373 rc = MHD_queue_response (connection, MHD_HTTP_OK, options_response);
374 MHD_destroy_response (options_response);
377 if (0 != strcmp (method, MHD_HTTP_METHOD_GET))
379 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
380 _("Refusing `%s' request to hostlist server\n"), method);
381 GNUNET_STATISTICS_update (stats,
383 ("hostlist requests refused (not HTTP GET)"), 1,
387 if (NULL == *con_cls)
392 if (0 != *upload_data_size)
394 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
395 _("Refusing `%s' request with %llu bytes of upload data\n"),
396 method, (unsigned long long) *upload_data_size);
397 GNUNET_STATISTICS_update (stats,
399 ("hostlist requests refused (upload data)"), 1,
401 return MHD_NO; /* do not support upload data */
403 if (NULL == response)
405 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
406 _("Could not handle hostlist request since I do not have a response yet\n"));
407 GNUNET_STATISTICS_update (stats,
409 ("hostlist requests refused (not ready)"), 1,
411 return MHD_NO; /* internal error, no response yet */
413 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
414 _("Received request for our hostlist\n"));
415 GNUNET_STATISTICS_update (stats,
416 gettext_noop ("hostlist requests processed"),
418 return MHD_queue_response (connection, MHD_HTTP_OK, response);
423 * Handler called by CORE when CORE is ready to transmit message
425 * @param cls closure with the `const struct GNUNET_PeerIdentity *` of
426 * the peer we are sending to
427 * @param size size of buffer to copy message to
428 * @param buf buffer to copy message to
429 * @return number of bytes copied to @a buf
432 adv_transmit (struct GNUNET_MQ_Handle *mq)
434 static uint64_t hostlist_adv_count;
437 size_t uri_size; /* Including \0 termination! */
438 struct GNUNET_MessageHeader *header;
439 struct GNUNET_MQ_Envelope *env;
441 extra = GNUNET_CORE_get_mq_options (GNUNET_YES,
442 GNUNET_CORE_PRIO_BEST_EFFORT,
444 uri_size = strlen (hostlist_uri) + 1;
445 env = GNUNET_MQ_msg_extra (header,
447 GNUNET_MESSAGE_TYPE_HOSTLIST_ADVERTISEMENT);
448 GNUNET_memcpy (&header[1],
451 GNUNET_MQ_env_set_options (env,
456 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
457 "Sent advertisement message: Copied %u bytes into buffer!\n",
458 (unsigned int) uri_size);
459 hostlist_adv_count++;
460 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
461 " # Sent advertisement message: %llu\n",
462 (unsigned long long) hostlist_adv_count);
463 GNUNET_STATISTICS_update (stats,
464 gettext_noop ("# hostlist advertisements send"), 1,
470 * Method called whenever a given peer connects.
473 * @param peer peer identity this notification is about
474 * @param mq queue for transmission to @a peer
475 * @return NULL (must!)
478 connect_handler (void *cls,
479 const struct GNUNET_PeerIdentity *peer,
480 struct GNUNET_MQ_Handle *mq)
486 if (NULL == hostlist_uri)
488 size = strlen (hostlist_uri) + 1;
489 if (size + sizeof (struct GNUNET_MessageHeader) >=
490 GNUNET_MAX_MESSAGE_SIZE)
495 size += sizeof (struct GNUNET_MessageHeader);
501 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
502 "Asked CORE to transmit advertisement message with a size of %u bytes to peer `%s'\n",
511 * PEERINFO calls this function to let us know about a possible peer
512 * that we might want to connect to.
514 * @param cls closure (not used)
515 * @param peer potential peer to connect to
516 * @param hello HELLO for this peer (or NULL)
517 * @param err_msg NULL if successful, otherwise contains error message
520 process_notify (void *cls,
521 const struct GNUNET_PeerIdentity *peer,
522 const struct GNUNET_HELLO_Message *hello,
525 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
526 "Peerinfo is notifying us to rebuild our hostlist\n");
528 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
529 _("Error in communication with PEERINFO service: %s\n"),
533 /* restart re-build already in progress ... */
534 if (NULL != builder->pitr)
536 GNUNET_PEERINFO_iterate_cancel (builder->pitr);
537 builder->pitr = NULL;
539 GNUNET_free_non_null (builder->data);
541 builder->data = NULL;
545 builder = GNUNET_new (struct HostSet);
547 GNUNET_assert (NULL != peerinfo);
549 = GNUNET_PEERINFO_iterate (peerinfo,
551 &host_processor, NULL);
556 * Function that queries MHD's select sets and
557 * starts the task waiting for them.
559 static struct GNUNET_SCHEDULER_Task *
560 prepare_daemon (struct MHD_Daemon *daemon_handle);
564 * Call MHD to process pending requests and then go back
565 * and schedule the next run.
567 * @param cls the `struct MHD_Daemon` of the HTTP server to run
570 run_daemon (void *cls)
572 struct MHD_Daemon *daemon_handle = cls;
574 if (daemon_handle == daemon_handle_v4)
575 hostlist_task_v4 = NULL;
577 hostlist_task_v6 = NULL;
578 GNUNET_assert (MHD_YES == MHD_run (daemon_handle));
579 if (daemon_handle == daemon_handle_v4)
580 hostlist_task_v4 = prepare_daemon (daemon_handle);
582 hostlist_task_v6 = prepare_daemon (daemon_handle);
587 * Function that queries MHD's select sets and
588 * starts the task waiting for them.
590 * @param daemon_handle HTTP server to prepare to run
592 static struct GNUNET_SCHEDULER_Task *
593 prepare_daemon (struct MHD_Daemon *daemon_handle)
595 struct GNUNET_SCHEDULER_Task * ret;
599 struct GNUNET_NETWORK_FDSet *wrs;
600 struct GNUNET_NETWORK_FDSet *wws;
602 MHD_UNSIGNED_LONG_LONG timeout;
604 struct GNUNET_TIME_Relative tv;
609 wrs = GNUNET_NETWORK_fdset_create ();
610 wws = GNUNET_NETWORK_fdset_create ();
612 GNUNET_assert (MHD_YES ==
613 MHD_get_fdset (daemon_handle,
614 &rs, &ws, &es, &max));
615 haveto = MHD_get_timeout (daemon_handle, &timeout);
616 if (haveto == MHD_YES)
617 tv.rel_value_us = (uint64_t) timeout * 1000LL;
619 tv = GNUNET_TIME_UNIT_FOREVER_REL;
620 GNUNET_NETWORK_fdset_copy_native (wrs, &rs, max + 1);
621 GNUNET_NETWORK_fdset_copy_native (wws, &ws, max + 1);
622 ret = GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_HIGH,
624 &run_daemon, daemon_handle);
625 GNUNET_NETWORK_fdset_destroy (wrs);
626 GNUNET_NETWORK_fdset_destroy (wws);
632 * Start server offering our hostlist.
634 * @param c configuration to use
635 * @param st statistics handle to use
636 * @param co core handle to use
637 * @param[out] server_ch set to handler for CORE connect events
638 * @param advertise #GNUNET_YES if we should advertise our hostlist
639 * @return #GNUNET_OK on success
642 GNUNET_HOSTLIST_server_start (const struct GNUNET_CONFIGURATION_Handle *c,
643 struct GNUNET_STATISTICS_Handle *st,
644 struct GNUNET_CORE_Handle *co,
645 GNUNET_CORE_ConnectEventHandler *server_ch,
648 unsigned long long port;
655 struct sockaddr_in v4;
656 struct sockaddr_in6 v6;
657 const struct sockaddr *sa4;
658 const struct sockaddr *sa6;
660 advertising = advertise;
663 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
664 "Advertising not enabled on this hostlist server\n");
668 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
669 "Advertising enabled on this hostlist server\n");
673 peerinfo = GNUNET_PEERINFO_connect (cfg);
674 if (NULL == peerinfo)
676 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
677 _("Could not access PEERINFO service. Exiting.\n"));
678 return GNUNET_SYSERR;
681 GNUNET_CONFIGURATION_get_value_number (cfg,
685 return GNUNET_SYSERR;
686 if ((0 == port) || (port > UINT16_MAX))
688 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
689 _("Invalid port number %llu. Exiting.\n"),
691 return GNUNET_SYSERR;
695 GNUNET_CONFIGURATION_get_value_string (cfg,
699 hostname = GNUNET_RESOLVER_local_fqdn_get ();
700 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
701 _("Hostlist service starts on %s:%llu\n"),
703 if (NULL != hostname)
705 size = strlen (hostname);
706 if (size + 15 > MAX_URL_LEN)
712 GNUNET_asprintf (&hostlist_uri,
713 "http://%s:%u/", hostname,
714 (unsigned int) port);
715 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
716 _("Address to obtain hostlist: `%s'\n"),
719 GNUNET_free (hostname);
722 if (GNUNET_CONFIGURATION_have_value (cfg, "HOSTLIST", "BINDTOIPV4"))
725 GNUNET_CONFIGURATION_get_value_string (cfg, "HOSTLIST",
728 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
729 _("BINDTOIP does not a valid IPv4 address! Ignoring BINDTOIPV4.\n"));
735 if (GNUNET_CONFIGURATION_have_value (cfg,
740 GNUNET_CONFIGURATION_get_value_string (cfg,
745 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
746 _("BINDTOIP does not a valid IPv4 address! Ignoring BINDTOIPV6.\n"));
754 if (1 == inet_pton (AF_INET, ipv4, &i4))
756 memset (&v4, 0, sizeof (v4));
757 v4.sin_family = AF_INET;
759 v4.sin_port = htons (port);
760 #if HAVE_SOCKADDR_IN_SIN_LEN
761 v4.sin_len = sizeof (v4);
763 sa4 = (const struct sockaddr *) &v4;
766 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
767 _("`%s' is not a valid IPv4 address! Ignoring BINDTOIPV4.\n"),
774 if (1 == inet_pton (AF_INET6, ipv6, &i6))
776 memset (&v6, 0, sizeof (v6));
777 v6.sin6_family = AF_INET6;
779 v6.sin6_port = htons (port);
780 #if HAVE_SOCKADDR_IN_SIN_LEN
781 v6.sin6_len = sizeof (v6);
783 sa6 = (const struct sockaddr *) &v6;
786 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
787 _("`%s' is not a valid IPv6 address! Ignoring BINDTOIPV6.\n"),
792 daemon_handle_v6 = MHD_start_daemon (MHD_USE_IPv6 | MHD_USE_DEBUG,
794 &accept_policy_callback, NULL,
795 &access_handler_callback, NULL,
796 MHD_OPTION_CONNECTION_LIMIT,
798 MHD_OPTION_PER_IP_CONNECTION_LIMIT,
800 MHD_OPTION_CONNECTION_TIMEOUT,
802 MHD_OPTION_CONNECTION_MEMORY_LIMIT,
803 (size_t) (16 * 1024),
804 MHD_OPTION_SOCK_ADDR,
807 daemon_handle_v4 = MHD_start_daemon (MHD_NO_FLAG | MHD_USE_DEBUG,
809 &accept_policy_callback, NULL,
810 &access_handler_callback, NULL,
811 MHD_OPTION_CONNECTION_LIMIT,
813 MHD_OPTION_PER_IP_CONNECTION_LIMIT,
815 MHD_OPTION_CONNECTION_TIMEOUT,
817 MHD_OPTION_CONNECTION_MEMORY_LIMIT,
818 (size_t) (16 * 1024),
819 MHD_OPTION_SOCK_ADDR,
823 if ( (NULL == daemon_handle_v6) &&
824 (NULL == daemon_handle_v4) )
826 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
827 _("Could not start hostlist HTTP server on port %u\n"),
828 (unsigned short) port);
829 return GNUNET_SYSERR;
833 *server_ch = &connect_handler;
834 if (NULL != daemon_handle_v4)
835 hostlist_task_v4 = prepare_daemon (daemon_handle_v4);
836 if (NULL != daemon_handle_v6)
837 hostlist_task_v6 = prepare_daemon (daemon_handle_v6);
838 notify = GNUNET_PEERINFO_notify (cfg,
840 &process_notify, NULL);
846 * Stop server offering our hostlist.
849 GNUNET_HOSTLIST_server_stop ()
851 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
852 "Hostlist server shutdown\n");
853 if (NULL != hostlist_task_v6)
855 GNUNET_SCHEDULER_cancel (hostlist_task_v6);
856 hostlist_task_v6 = NULL;
858 if (NULL != hostlist_task_v4)
860 GNUNET_SCHEDULER_cancel (hostlist_task_v4);
861 hostlist_task_v4 = NULL;
863 if (NULL != daemon_handle_v4)
865 MHD_stop_daemon (daemon_handle_v4);
866 daemon_handle_v4 = NULL;
868 if (NULL != daemon_handle_v6)
870 MHD_stop_daemon (daemon_handle_v6);
871 daemon_handle_v6 = NULL;
873 if (NULL != response)
875 MHD_destroy_response (response);
880 GNUNET_PEERINFO_notify_cancel (notify);
885 if (NULL != builder->pitr)
887 GNUNET_PEERINFO_iterate_cancel (builder->pitr);
888 builder->pitr = NULL;
890 GNUNET_free_non_null (builder->data);
891 GNUNET_free (builder);
894 if (NULL != peerinfo)
896 GNUNET_PEERINFO_disconnect (peerinfo);
904 /* end of gnunet-daemon-hostlist_server.c */