2 This file is part of GNUnet.
3 (C) 2008, 2009, 2010 Christian Grothoff (and other contributing authors)
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 2, 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., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file hostlist/hostlist-server.c
23 * @author Christian Grothoff
24 * @brief application to provide an integrated hostlist HTTP server
28 #include <microhttpd.h>
29 #include "hostlist-server.h"
30 #include "gnunet_hello_lib.h"
31 #include "gnunet_peerinfo_service.h"
32 #include "gnunet-daemon-hostlist.h"
33 #include "gnunet_resolver_service.h"
35 #define DEBUG_HOSTLIST_SERVER GNUNET_NO
38 * How often should we recalculate our response to hostlist requests?
40 #define RESPONSE_UPDATE_FREQUENCY 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;
60 static struct GNUNET_SCHEDULER_Handle *sched;
63 * For keeping statistics.
65 static struct GNUNET_STATISTICS_Handle *stats;
68 * Handle to the core service (NULL until we've connected to it).
70 struct GNUNET_CORE_Handle *core;
73 * Our primary task for IPv4.
75 static GNUNET_SCHEDULER_TaskIdentifier hostlist_task_v4;
78 * Our primary task for IPv6.
80 static GNUNET_SCHEDULER_TaskIdentifier hostlist_task_v6;
83 * Task that updates our HTTP response.
85 static GNUNET_SCHEDULER_TaskIdentifier response_task;
88 * Our canonical response.
90 static struct MHD_Response *response;
93 * NULL if we are not currenlty iterating over peer information.
95 static struct GNUNET_PEERINFO_IteratorContext *pitr;
98 * Context for host processor.
108 * Set if we are allowed to advertise our hostlist to others.
110 static int advertising;
113 * Task that will produce a new response object.
116 update_response (void *cls,
117 const struct GNUNET_SCHEDULER_TaskContext *tc);
120 * Function that assembles our response.
123 finish_response (struct HostSet *results)
125 struct GNUNET_TIME_Relative freq;
127 if (response != NULL)
128 MHD_destroy_response (response);
129 #if DEBUG_HOSTLIST_SERVER
130 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
131 "Creating hostlist response with %u bytes\n",
132 (unsigned int) results->size);
134 response = MHD_create_response_from_data (results->size,
135 results->data, MHD_YES, MHD_NO);
136 if ( (daemon_handle_v4 != NULL) ||
137 (daemon_handle_v6 != NULL) )
139 freq = RESPONSE_UPDATE_FREQUENCY;
140 if (results->size == 0)
141 freq = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 250);
142 /* schedule next update of the response */
143 response_task = GNUNET_SCHEDULER_add_delayed (sched,
150 /* already past shutdown */
151 MHD_destroy_response (response);
154 GNUNET_STATISTICS_set (stats,
155 gettext_noop("bytes in hostlist"),
158 GNUNET_free (results);
163 * Set 'cls' to GNUNET_YES (we have an address!).
165 * @param cls closure, an 'int*'
166 * @param tname name of the transport (ignored)
167 * @param expiration expiration time (call is ignored if this is in the past)
168 * @param addr the address (ignored)
169 * @param addrlen length of the address (ignored)
170 * @return GNUNET_SYSERR to stop iterating (unless expiration has occured)
173 check_has_addr (void *cls,
175 struct GNUNET_TIME_Absolute expiration,
176 const void *addr, size_t addrlen)
180 if (GNUNET_TIME_absolute_get_remaining (expiration).value == 0)
182 GNUNET_STATISTICS_update (stats,
183 gettext_noop("expired addresses encountered"),
186 return GNUNET_YES; /* ignore this address */
189 return GNUNET_SYSERR;
194 * Callback that processes each of the known HELLOs for the
195 * hostlist response construction.
198 host_processor (void *cls,
199 const struct GNUNET_PeerIdentity * peer,
200 const struct GNUNET_HELLO_Message *hello,
203 struct HostSet *results = cls;
211 finish_response (results);
216 has_addr = GNUNET_NO;
217 GNUNET_HELLO_iterate_addresses (hello,
221 if (GNUNET_NO == has_addr)
223 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
224 "HELLO for peer `%4s' has no address, not suitable for hostlist!\n",
226 GNUNET_STATISTICS_update (stats,
227 gettext_noop("HELLOs without addresses encountered (ignored)"),
233 s = GNUNET_HELLO_size(hello);
234 #if DEBUG_HOSTLIST_SERVER
235 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
236 "Received %u bytes of `%s' from peer `%s' for hostlist.\n",
241 if (old + s >= GNUNET_MAX_MALLOC_CHECKED)
243 GNUNET_STATISTICS_update (stats,
244 gettext_noop("bytes not included in hostlist (size limit)"),
247 return; /* too large, skip! */
249 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
250 "Adding peer `%s' to hostlist (%u bytes)\n",
253 GNUNET_array_grow (results->data,
256 memcpy (&results->data[old], hello, s);
261 * Task that will produce a new response object.
264 update_response (void *cls,
265 const struct GNUNET_SCHEDULER_TaskContext *tc)
267 struct HostSet *results;
269 response_task = GNUNET_SCHEDULER_NO_TASK;
270 results = GNUNET_malloc(sizeof(struct HostSet));
271 pitr = GNUNET_PEERINFO_iterate (cfg, sched,
274 GNUNET_TIME_UNIT_MINUTES,
281 * Hostlist access policy (very permissive, allows everything).
284 accept_policy_callback (void *cls,
285 const struct sockaddr *addr, socklen_t addrlen)
287 if (NULL == response)
289 #if DEBUG_HOSTLIST_SERVER
290 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
291 "Received request for hostlist, but I am not yet ready; rejecting!\n");
295 return MHD_YES; /* accept all */
300 * Main request handler.
303 access_handler_callback (void *cls,
304 struct MHD_Connection *connection,
308 const char *upload_data,
309 size_t*upload_data_size, void **con_cls)
313 if (0 != strcmp (method, MHD_HTTP_METHOD_GET))
315 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
316 _("Refusing `%s' request to hostlist server\n"),
318 GNUNET_STATISTICS_update (stats,
319 gettext_noop("hostlist requests refused (not HTTP GET)"),
324 if (NULL == *con_cls)
327 #if DEBUG_HOSTLIST_SERVER
328 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
329 _("Sending 100 CONTINUE reply\n"));
331 return MHD_YES; /* send 100 continue */
333 if (*upload_data_size != 0)
335 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
336 _("Refusing `%s' request with %llu bytes of upload data\n"),
338 (unsigned long long) *upload_data_size);
339 GNUNET_STATISTICS_update (stats,
340 gettext_noop("hostlist requests refused (upload data)"),
343 return MHD_NO; /* do not support upload data */
345 if (response == NULL)
347 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
348 _("Could not handle hostlist request since I do not have a response yet\n"));
349 GNUNET_STATISTICS_update (stats,
350 gettext_noop("hostlist requests refused (not ready)"),
353 return MHD_NO; /* internal error, no response yet */
355 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
356 _("Received request for our hostlist\n"));
357 GNUNET_STATISTICS_update (stats,
358 gettext_noop("hostlist requests processed"),
361 return MHD_queue_response (connection, MHD_HTTP_OK, response);
365 * Buffer for the hostlist address
367 char hostlist_uri[255];
370 * Handler called by core when core is ready to transmit message
372 * @param size size of buffer to copy message to
373 * @param buf buffer to copy message to
376 adv_transmit_ready ( void *cls, size_t size, void *buf)
378 size_t transmission_size;
379 size_t uri_size; /* Including \0 termination! */
380 uri_size = strlen ( hostlist_uri ) + 1;
382 struct GNUNET_HOSTLIST_ADV_Message * adv_message;
383 adv_message = GNUNET_malloc ( sizeof(struct GNUNET_HOSTLIST_ADV_Message) + uri_size);
384 if ( NULL == adv_message)
386 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR,
387 "Could not allocate memory for the message");
390 transmission_size = sizeof (struct GNUNET_HOSTLIST_ADV_Message) + uri_size;
392 adv_message->header.type = htons (GNUNET_MESSAGE_TYPE_HOSTLIST_ADVERTISEMENT);
393 adv_message->header.size = htons (transmission_size);
394 memcpy(&adv_message[1],hostlist_uri,uri_size);
398 GNUNET_log ( GNUNET_ERROR_TYPE_DEBUG, "Transmission failed, buffer invalid!\n" );
402 if ( size >= transmission_size )
404 memcpy ( buf, adv_message, transmission_size );
405 GNUNET_log ( GNUNET_ERROR_TYPE_DEBUG, "Sent advertisement message: Copied %d bytes into buffer!\n", transmission_size);
406 GNUNET_free ( adv_message );
407 return transmission_size;
410 GNUNET_free (adv_message );
415 * Method that asks core service to transmit the message to the peer
416 * @param peer peer to transmit message to
417 * @param size size of the message
420 adv_transmit_message ( const struct GNUNET_PeerIdentity * peer, size_t size )
422 /* transmit message to peer */
425 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
426 _("Not connected to core, unable to send advertisement message\n"));
430 struct GNUNET_TIME_Relative timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, GNUNET_ADV_TIMEOUT);
431 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
432 _("Asked core to transmit advertisement message with a size of %u bytes\n"), size);
433 struct GNUNET_CORE_TransmitHandle * th;
434 th = GNUNET_CORE_notify_transmit_ready (core,
439 &adv_transmit_ready, NULL);
441 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
442 _("Advertisement message could not be queued by core\n"));
449 * Method that assembles our hostlist advertisement message
450 * @param peer peer to send the hostlist advertisement
453 adv_create_message ( const struct GNUNET_PeerIdentity * peer )
458 unsigned long long port;
461 char hostname[GNUNET_OS_get_hostname_max_length() + 1];
462 char *protocol = "http://";
463 char *port_s = GNUNET_malloc(6 * sizeof(char));
465 if (0 != gethostname (hostname, sizeof (hostname) - 1))
467 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR,
468 "Could not get system's hostname, unable to create advertisement message");
471 if (-1 == GNUNET_CONFIGURATION_get_value_number (cfg,
475 return GNUNET_SYSERR;
477 sprintf(port_s, "%llu", port);
478 length = strlen(hostname)+strlen(protocol)+strlen(port_s)+2;
479 size = (length+1) * sizeof (char);
480 uri = GNUNET_malloc(size);
481 uri = strcpy(uri, protocol);
482 uri = strcat(uri, hostname);
483 uri = strcat(uri, ":");
484 uri = strcat(uri, port_s);
485 uri = strcat(uri, "/");
487 strcpy(hostlist_uri,uri);
489 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Address to obtain hostlist: %s\n", hostlist_uri);
491 if ( ( size + sizeof( struct GNUNET_HOSTLIST_ADV_Message )) > GNUNET_SERVER_MAX_MESSAGE_SIZE)
493 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR,
494 "Advertisement message is bigger than GNUNET allows");
498 /* Request core to transmit message to peer */
499 size = size + sizeof ( struct GNUNET_HOSTLIST_ADV_Message );
500 adv_transmit_message(peer, size);
502 GNUNET_free ( port_s );
509 * Method called whenever a given peer connects.
512 * @param peer peer identity this notification is about
513 * @param latency reported latency of the connection with 'other'
514 * @param distance reported distance (DV) to 'other'
517 connect_handler (void *cls,
519 GNUNET_PeerIdentity * peer,
520 struct GNUNET_TIME_Relative latency,
526 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
527 "A new peer connected to the server, preparing to send hostlist advertisement\n");
528 /* create a new advertisement message */
529 if ( (GNUNET_OK != adv_create_message(peer)))
531 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
532 _(" GNUNET_OK Could not create a hostlist advertisement message, impossible to advertise hostlist\n"));
539 * Method called whenever a given peer disconnects.
542 * @param peer peer identity this notification is about
545 disconnect_handler (void *cls,
547 GNUNET_PeerIdentity * peer)
554 * Function that queries MHD's select sets and
555 * starts the task waiting for them.
557 static GNUNET_SCHEDULER_TaskIdentifier
558 prepare_daemon (struct MHD_Daemon *daemon_handle);
561 * Call MHD to process pending requests and then go back
562 * and schedule the next run.
565 run_daemon (void *cls,
566 const struct GNUNET_SCHEDULER_TaskContext *tc)
568 struct MHD_Daemon *daemon_handle = cls;
570 if (daemon_handle == daemon_handle_v4)
571 hostlist_task_v4 = GNUNET_SCHEDULER_NO_TASK;
573 hostlist_task_v6 = GNUNET_SCHEDULER_NO_TASK;
575 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
577 GNUNET_assert (MHD_YES == MHD_run (daemon_handle));
578 if (daemon_handle == daemon_handle_v4)
579 hostlist_task_v4 = prepare_daemon (daemon_handle);
581 hostlist_task_v6 = prepare_daemon (daemon_handle);
586 * Function that queries MHD's select sets and
587 * starts the task waiting for them.
589 static GNUNET_SCHEDULER_TaskIdentifier
590 prepare_daemon (struct MHD_Daemon *daemon_handle)
592 GNUNET_SCHEDULER_TaskIdentifier ret;
596 struct GNUNET_NETWORK_FDSet *wrs;
597 struct GNUNET_NETWORK_FDSet *wws;
598 struct GNUNET_NETWORK_FDSet *wes;
600 unsigned long long timeout;
602 struct GNUNET_TIME_Relative tv;
607 wrs = GNUNET_NETWORK_fdset_create ();
608 wes = GNUNET_NETWORK_fdset_create ();
609 wws = GNUNET_NETWORK_fdset_create ();
611 GNUNET_assert (MHD_YES ==
612 MHD_get_fdset (daemon_handle,
617 haveto = MHD_get_timeout (daemon_handle, &timeout);
618 if (haveto == MHD_YES)
619 tv.value = (uint64_t) timeout;
621 tv = GNUNET_TIME_UNIT_FOREVER_REL;
622 GNUNET_NETWORK_fdset_copy_native (wrs, &rs, max);
623 GNUNET_NETWORK_fdset_copy_native (wws, &ws, max);
624 GNUNET_NETWORK_fdset_copy_native (wes, &es, max);
625 ret = GNUNET_SCHEDULER_add_select (sched,
626 GNUNET_SCHEDULER_PRIORITY_HIGH,
627 GNUNET_SCHEDULER_NO_TASK,
633 GNUNET_NETWORK_fdset_destroy (wrs);
634 GNUNET_NETWORK_fdset_destroy (wws);
635 GNUNET_NETWORK_fdset_destroy (wes);
642 * Start server offering our hostlist.
644 * @return GNUNET_OK on success
647 GNUNET_HOSTLIST_server_start (const struct GNUNET_CONFIGURATION_Handle *c,
648 struct GNUNET_SCHEDULER_Handle *s,
649 struct GNUNET_STATISTICS_Handle *st,
650 struct GNUNET_CORE_Handle *co,
651 GNUNET_CORE_ConnectEventHandler *server_ch,
652 GNUNET_CORE_DisconnectEventHandler *server_dh,
655 unsigned long long port;
657 advertising = advertise;
659 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
660 "Advertising not enabled on this hostlist server\n");
662 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
663 "Advertising enabled on this hostlist server\n");
667 if (-1 == GNUNET_CONFIGURATION_get_value_number (cfg,
671 return GNUNET_SYSERR;
672 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
673 _("Hostlist service starts on port %llu\n"),
675 daemon_handle_v6 = MHD_start_daemon (MHD_USE_IPv6
676 #if DEBUG_HOSTLIST_SERVER
680 (unsigned short) port,
681 &accept_policy_callback,
683 &access_handler_callback,
685 MHD_OPTION_CONNECTION_LIMIT, (unsigned int) 16,
686 MHD_OPTION_PER_IP_CONNECTION_LIMIT, (unsigned int) 1,
687 MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) 16,
688 MHD_OPTION_CONNECTION_MEMORY_LIMIT, (size_t) (16 * 1024),
690 daemon_handle_v4 = MHD_start_daemon (MHD_NO_FLAG
691 #if DEBUG_HOSTLIST_SERVER
695 (unsigned short) port,
696 &accept_policy_callback,
698 &access_handler_callback,
700 MHD_OPTION_CONNECTION_LIMIT, (unsigned int) 16,
701 MHD_OPTION_PER_IP_CONNECTION_LIMIT, (unsigned int) 1,
702 MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) 16,
703 MHD_OPTION_CONNECTION_MEMORY_LIMIT, (size_t) (16 * 1024),
706 if ( (daemon_handle_v6 == NULL) &&
707 (daemon_handle_v4 == NULL) )
709 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
710 _("Could not start hostlist HTTP server on port %u\n"),
711 (unsigned short) port);
712 return GNUNET_SYSERR;
717 *server_ch = &connect_handler;
718 *server_dh = &disconnect_handler;
720 if (daemon_handle_v4 != NULL)
721 hostlist_task_v4 = prepare_daemon (daemon_handle_v4);
722 if (daemon_handle_v6 != NULL)
723 hostlist_task_v6 = prepare_daemon (daemon_handle_v6);
724 response_task = GNUNET_SCHEDULER_add_now (sched,
731 * Stop server offering our hostlist.
734 GNUNET_HOSTLIST_server_stop ()
736 #if DEBUG_HOSTLIST_SERVER
737 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
738 "Hostlist server shutdown\n");
740 if (GNUNET_SCHEDULER_NO_TASK != hostlist_task_v6)
742 GNUNET_SCHEDULER_cancel (sched, hostlist_task_v6);
743 hostlist_task_v6 = GNUNET_SCHEDULER_NO_TASK;
745 if (GNUNET_SCHEDULER_NO_TASK != hostlist_task_v4)
747 GNUNET_SCHEDULER_cancel (sched, hostlist_task_v4);
748 hostlist_task_v4 = GNUNET_SCHEDULER_NO_TASK;
752 GNUNET_PEERINFO_iterate_cancel (pitr);
755 if (GNUNET_SCHEDULER_NO_TASK != response_task)
757 GNUNET_SCHEDULER_cancel (sched, response_task);
758 response_task = GNUNET_SCHEDULER_NO_TASK;
760 if (NULL != daemon_handle_v4)
762 MHD_stop_daemon (daemon_handle_v4);
763 daemon_handle_v4 = NULL;
765 if (NULL != daemon_handle_v6)
767 MHD_stop_daemon (daemon_handle_v6);
768 daemon_handle_v6 = NULL;
770 if (response != NULL)
772 MHD_destroy_response (response);
777 /* end of hostlist-server.c */