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/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
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 \
42 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 5)
46 * Handle to the HTTP server as provided by libmicrohttpd for IPv6.
48 static struct MHD_Daemon *daemon_handle_v6;
51 * Handle to the HTTP server as provided by libmicrohttpd for IPv4.
53 static struct MHD_Daemon *daemon_handle_v4;
58 static const struct GNUNET_CONFIGURATION_Handle *cfg;
61 * For keeping statistics.
63 static struct GNUNET_STATISTICS_Handle *stats;
66 * Handle to the core service (NULL until we've connected to it).
68 static struct GNUNET_CORE_Handle *core;
71 * Handle to the peerinfo notify service (NULL until we've connected to it).
73 static struct GNUNET_PEERINFO_NotifyContext *notify;
76 * Our primary task for IPv4.
78 static struct GNUNET_SCHEDULER_Task *hostlist_task_v4;
81 * Our primary task for IPv6.
83 static struct GNUNET_SCHEDULER_Task *hostlist_task_v6;
86 * Our canonical response.
88 static struct MHD_Response *response;
91 * Handle for accessing peerinfo service.
93 static struct GNUNET_PEERINFO_Handle *peerinfo;
96 * Set if we are allowed to advertise our hostlist to others.
98 static int advertising;
101 * Buffer for the hostlist address
103 static char *hostlist_uri;
107 * Context for #host_processor().
112 * Iterator used to build @e data (NULL when done).
114 struct GNUNET_PEERINFO_IteratorContext *pitr;
117 * Place where we accumulate all of the HELLO messages.
122 * 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, "Access-Control-Allow-Origin", "*");
144 MHD_add_response_header (response,
145 "Access-Control-Allow-Methods",
147 MHD_add_response_header (response, "Access-Control-Max-Age", "86400");
152 * Function that assembles our response.
157 if (NULL != response)
158 MHD_destroy_response (response);
159 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
160 "Creating hostlist response with %u bytes\n",
161 (unsigned int) builder->size);
162 response = 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,
172 gettext_noop ("bytes in hostlist"),
175 GNUNET_free (builder);
181 * Set @a cls to #GNUNET_YES (we have an address!).
183 * @param cls closure, an `int *`
184 * @param address the address (ignored)
185 * @param expiration expiration time (call is ignored if this is in the past)
186 * @return #GNUNET_SYSERR to stop iterating (unless expiration has occured)
189 check_has_addr (void *cls,
190 const struct GNUNET_HELLO_Address *address,
191 struct GNUNET_TIME_Absolute expiration)
195 if (0 == GNUNET_TIME_absolute_get_remaining (expiration).rel_value_us)
197 GNUNET_STATISTICS_update (stats,
198 gettext_noop ("expired addresses encountered"),
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, GNUNET_NO, &check_has_addr, &has_addr);
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)"),
262 s = GNUNET_HELLO_size (hello);
263 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
264 "Received %u bytes of `%s' from peer `%s' for hostlist.\n",
268 if ((old + s >= GNUNET_MAX_MALLOC_CHECKED) ||
269 (old + s >= MAX_BYTES_PER_HOSTLISTS))
271 /* too large, skip! */
272 GNUNET_STATISTICS_update (stats,
274 "bytes not included in hostlist (size limit)"),
279 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
280 "Adding peer `%s' to hostlist (%u bytes)\n",
283 GNUNET_array_grow (builder->data, builder->size, old + s);
284 GNUNET_memcpy (&builder->data[old], hello, s);
289 * Hostlist access policy (very permissive, allows everything).
290 * Returns #MHD_NO only if we are not yet ready to serve.
293 * @param addr address information from the client
294 * @param addrlen length of @a addr
295 * @return #MHD_YES if connection is allowed, #MHD_NO if not (we are not ready)
298 accept_policy_callback (void *cls,
299 const struct sockaddr *addr,
302 if (NULL == response)
305 GNUNET_ERROR_TYPE_DEBUG,
306 "Received request for hostlist, but I am not yet ready; rejecting!\n");
309 return MHD_YES; /* accept all */
314 * Main request handler.
316 * @param cls argument given together with the function
317 * pointer when the handler was registered with MHD
319 * @param url the requested url
320 * @param method the HTTP method used (#MHD_HTTP_METHOD_GET,
321 * #MHD_HTTP_METHOD_PUT, etc.)
322 * @param version the HTTP version string (i.e.
323 * #MHD_HTTP_VERSION_1_1)
324 * @param upload_data the data being uploaded (excluding HEADERS,
325 * for a POST that fits into memory and that is encoded
326 * with a supported encoding, the POST data will NOT be
327 * given in upload_data and is instead available as
328 * part of #MHD_get_connection_values; very large POST
329 * data *will* be made available incrementally in
331 * @param upload_data_size set initially to the size of the
332 * @a upload_data provided; the method must update this
333 * value to the number of bytes NOT processed;
334 * @param con_cls pointer that the callback can set to some
335 * address and that will be preserved by MHD for future
336 * calls for this request; since the access handler may
337 * be called many times (i.e., for a PUT/POST operation
338 * with plenty of upload data) this allows the application
339 * to easily associate some request-specific state.
340 * If necessary, this state can be cleaned up in the
341 * global #MHD_RequestCompletedCallback (which
342 * can be set with the #MHD_OPTION_NOTIFY_COMPLETED).
343 * Initially, `*con_cls` will be NULL.
344 * @return #MHD_YES if the connection was handled successfully,
345 * #MHD_NO if the socket must be closed due to a serios
346 * error while handling the request
349 access_handler_callback (void *cls,
350 struct MHD_Connection *connection,
354 const char *upload_data,
355 size_t *upload_data_size,
360 /* CORS pre-flight request */
361 if (0 == strcmp (MHD_HTTP_METHOD_OPTIONS, method))
363 struct MHD_Response *options_response;
367 MHD_create_response_from_buffer (0, NULL, MHD_RESPMEM_PERSISTENT);
368 add_cors_headers (options_response);
369 rc = MHD_queue_response (connection, MHD_HTTP_OK, options_response);
370 MHD_destroy_response (options_response);
373 if (0 != strcmp (method, MHD_HTTP_METHOD_GET))
375 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
376 _ ("Refusing `%s' request to hostlist server\n"),
378 GNUNET_STATISTICS_update (stats,
380 "hostlist requests refused (not HTTP GET)"),
385 if (NULL == *con_cls)
390 if (0 != *upload_data_size)
392 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
393 _ ("Refusing `%s' request with %llu bytes of upload data\n"),
395 (unsigned long long) *upload_data_size);
396 GNUNET_STATISTICS_update (stats,
398 "hostlist requests refused (upload data)"),
401 return MHD_NO; /* do not support upload data */
403 if (NULL == response)
406 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)"),
414 return MHD_NO; /* internal error, no response yet */
416 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
417 _ ("Received request for our hostlist\n"));
418 GNUNET_STATISTICS_update (stats,
419 gettext_noop ("hostlist requests processed"),
422 return MHD_queue_response (connection, MHD_HTTP_OK, response);
427 * Handler called by CORE when CORE is ready to transmit message
429 * @param cls closure with the `const struct GNUNET_PeerIdentity *` of
430 * the peer we are sending to
431 * @param size size of buffer to copy message to
432 * @param buf buffer to copy message to
433 * @return number of bytes copied to @a buf
436 adv_transmit (struct GNUNET_MQ_Handle *mq)
438 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 uri_size = strlen (hostlist_uri) + 1;
444 env = GNUNET_MQ_msg_extra (header,
446 GNUNET_MESSAGE_TYPE_HOSTLIST_ADVERTISEMENT);
447 GNUNET_memcpy (&header[1], hostlist_uri, uri_size);
448 GNUNET_MQ_env_set_options (env,
449 GNUNET_MQ_PREF_CORK_ALLOWED
450 | GNUNET_MQ_PREF_UNRELIABLE);
451 GNUNET_MQ_send (mq, env);
452 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
453 "Sent advertisement message: Copied %u bytes into buffer!\n",
454 (unsigned int) uri_size);
455 hostlist_adv_count++;
456 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
457 " # Sent advertisement message: %llu\n",
458 (unsigned long long) hostlist_adv_count);
459 GNUNET_STATISTICS_update (stats,
460 gettext_noop ("# hostlist advertisements send"),
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) >= GNUNET_MAX_MESSAGE_SIZE)
491 size += sizeof(struct GNUNET_MessageHeader);
498 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, GNUNET_NO, NULL, &host_processor, NULL);
551 * Function that queries MHD's select sets and
552 * starts the task waiting for them.
554 static struct GNUNET_SCHEDULER_Task *
555 prepare_daemon (struct MHD_Daemon *daemon_handle);
559 * Call MHD to process pending requests and then go back
560 * and schedule the next run.
562 * @param cls the `struct MHD_Daemon` of the HTTP server to run
565 run_daemon (void *cls)
567 struct MHD_Daemon *daemon_handle = cls;
569 if (daemon_handle == daemon_handle_v4)
570 hostlist_task_v4 = NULL;
572 hostlist_task_v6 = NULL;
573 GNUNET_assert (MHD_YES == MHD_run (daemon_handle));
574 if (daemon_handle == daemon_handle_v4)
575 hostlist_task_v4 = prepare_daemon (daemon_handle);
577 hostlist_task_v6 = prepare_daemon (daemon_handle);
582 * Function that queries MHD's select sets and
583 * starts the task waiting for them.
585 * @param daemon_handle HTTP server to prepare to run
587 static struct GNUNET_SCHEDULER_Task *
588 prepare_daemon (struct MHD_Daemon *daemon_handle)
590 struct GNUNET_SCHEDULER_Task *ret;
594 struct GNUNET_NETWORK_FDSet *wrs;
595 struct GNUNET_NETWORK_FDSet *wws;
597 MHD_UNSIGNED_LONG_LONG timeout;
599 struct GNUNET_TIME_Relative tv;
604 wrs = GNUNET_NETWORK_fdset_create ();
605 wws = GNUNET_NETWORK_fdset_create ();
607 GNUNET_assert (MHD_YES == MHD_get_fdset (daemon_handle, &rs, &ws, &es, &max));
608 haveto = MHD_get_timeout (daemon_handle, &timeout);
609 if (haveto == MHD_YES)
610 tv.rel_value_us = (uint64_t) timeout * 1000LL;
612 tv = GNUNET_TIME_UNIT_FOREVER_REL;
613 GNUNET_NETWORK_fdset_copy_native (wrs, &rs, max + 1);
614 GNUNET_NETWORK_fdset_copy_native (wws, &ws, max + 1);
615 ret = GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_HIGH,
621 GNUNET_NETWORK_fdset_destroy (wrs);
622 GNUNET_NETWORK_fdset_destroy (wws);
628 * Start server offering our hostlist.
630 * @param c configuration to use
631 * @param st statistics handle to use
632 * @param co core handle to use
633 * @param[out] server_ch set to handler for CORE connect events
634 * @param advertise #GNUNET_YES if we should advertise our hostlist
635 * @return #GNUNET_OK on success
638 GNUNET_HOSTLIST_server_start (const struct GNUNET_CONFIGURATION_Handle *c,
639 struct GNUNET_STATISTICS_Handle *st,
640 struct GNUNET_CORE_Handle *co,
641 GNUNET_CORE_ConnectEventHandler *server_ch,
644 unsigned long long port;
651 struct sockaddr_in v4;
652 struct sockaddr_in6 v6;
653 const struct sockaddr *sa4;
654 const struct sockaddr *sa6;
656 advertising = advertise;
659 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
660 "Advertising not enabled on this hostlist server\n");
664 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
665 "Advertising enabled on this hostlist server\n");
669 peerinfo = GNUNET_PEERINFO_connect (cfg);
670 if (NULL == peerinfo)
672 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
673 _ ("Could not access PEERINFO service. Exiting.\n"));
674 return GNUNET_SYSERR;
676 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (cfg,
680 return GNUNET_SYSERR;
681 if ((0 == port) || (port > UINT16_MAX))
683 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
684 _ ("Invalid port number %llu. Exiting.\n"),
686 return GNUNET_SYSERR;
690 GNUNET_CONFIGURATION_get_value_string (cfg,
694 hostname = GNUNET_RESOLVER_local_fqdn_get ();
695 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
696 _ ("Hostlist service starts on %s:%llu\n"),
699 if (NULL != hostname)
701 size = strlen (hostname);
702 if (size + 15 > MAX_URL_LEN)
708 GNUNET_asprintf (&hostlist_uri,
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"))
721 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string (cfg,
727 GNUNET_ERROR_TYPE_WARNING,
728 _ ("BINDTOIP does not a valid IPv4 address! Ignoring BINDTOIPV4.\n"));
733 if (GNUNET_CONFIGURATION_have_value (cfg, "HOSTLIST", "BINDTOIPV6"))
735 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string (cfg,
741 GNUNET_ERROR_TYPE_WARNING,
742 _ ("BINDTOIP does not a valid IPv4 address! Ignoring BINDTOIPV6.\n"));
750 if (1 == inet_pton (AF_INET, ipv4, &i4))
752 memset (&v4, 0, sizeof(v4));
753 v4.sin_family = AF_INET;
755 v4.sin_port = htons (port);
756 #if HAVE_SOCKADDR_IN_SIN_LEN
757 v4.sin_len = sizeof(v4);
759 sa4 = (const struct sockaddr *) &v4;
762 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,
785 "`%s' is not a valid IPv6 address! Ignoring BINDTOIPV6.\n"),
790 daemon_handle_v6 = MHD_start_daemon (MHD_USE_IPv6 | MHD_USE_DEBUG,
792 &accept_policy_callback,
794 &access_handler_callback,
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,
811 &access_handler_callback,
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) && (NULL == daemon_handle_v4))
827 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
828 _ ("Could not start hostlist HTTP server on port %u\n"),
829 (unsigned short) port);
830 return GNUNET_SYSERR;
834 *server_ch = &connect_handler;
835 if (NULL != daemon_handle_v4)
836 hostlist_task_v4 = prepare_daemon (daemon_handle_v4);
837 if (NULL != daemon_handle_v6)
838 hostlist_task_v6 = prepare_daemon (daemon_handle_v6);
839 notify = GNUNET_PEERINFO_notify (cfg, GNUNET_NO, &process_notify, NULL);
845 * Stop server offering our hostlist.
848 GNUNET_HOSTLIST_server_stop ()
850 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Hostlist server shutdown\n");
851 if (NULL != hostlist_task_v6)
853 GNUNET_SCHEDULER_cancel (hostlist_task_v6);
854 hostlist_task_v6 = NULL;
856 if (NULL != hostlist_task_v4)
858 GNUNET_SCHEDULER_cancel (hostlist_task_v4);
859 hostlist_task_v4 = NULL;
861 if (NULL != daemon_handle_v4)
863 MHD_stop_daemon (daemon_handle_v4);
864 daemon_handle_v4 = NULL;
866 if (NULL != daemon_handle_v6)
868 MHD_stop_daemon (daemon_handle_v6);
869 daemon_handle_v6 = NULL;
871 if (NULL != response)
873 MHD_destroy_response (response);
878 GNUNET_PEERINFO_notify_cancel (notify);
883 if (NULL != builder->pitr)
885 GNUNET_PEERINFO_iterate_cancel (builder->pitr);
886 builder->pitr = NULL;
888 GNUNET_free_non_null (builder->data);
889 GNUNET_free (builder);
892 if (NULL != peerinfo)
894 GNUNET_PEERINFO_disconnect (peerinfo);
903 /* end of gnunet-daemon-hostlist_server.c */