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, Matthias Wachs
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 * How many times was the hostlist advertised?
115 static uint64_t hostlist_adv_count = 0;
118 * Buffer for the hostlist address
120 char hostlist_uri[255];
124 * Task that will produce a new response object.
127 update_response (void *cls,
128 const struct GNUNET_SCHEDULER_TaskContext *tc);
132 * Function that assembles our response.
135 finish_response (struct HostSet *results)
137 struct GNUNET_TIME_Relative freq;
139 if (response != NULL)
140 MHD_destroy_response (response);
141 #if DEBUG_HOSTLIST_SERVER
142 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
143 "Creating hostlist response with %u bytes\n",
144 (unsigned int) results->size);
146 response = MHD_create_response_from_data (results->size,
147 results->data, MHD_YES, MHD_NO);
148 if ( (daemon_handle_v4 != NULL) ||
149 (daemon_handle_v6 != NULL) )
151 freq = RESPONSE_UPDATE_FREQUENCY;
152 if (results->size == 0)
153 freq = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 250);
154 /* schedule next update of the response */
155 response_task = GNUNET_SCHEDULER_add_delayed (sched,
162 /* already past shutdown */
163 MHD_destroy_response (response);
166 GNUNET_STATISTICS_set (stats,
167 gettext_noop("bytes in hostlist"),
170 GNUNET_free (results);
175 * Set 'cls' to GNUNET_YES (we have an address!).
177 * @param cls closure, an 'int*'
178 * @param tname name of the transport (ignored)
179 * @param expiration expiration time (call is ignored if this is in the past)
180 * @param addr the address (ignored)
181 * @param addrlen length of the address (ignored)
182 * @return GNUNET_SYSERR to stop iterating (unless expiration has occured)
185 check_has_addr (void *cls,
187 struct GNUNET_TIME_Absolute expiration,
188 const void *addr, size_t addrlen)
192 if (GNUNET_TIME_absolute_get_remaining (expiration).value == 0)
194 GNUNET_STATISTICS_update (stats,
195 gettext_noop("expired addresses encountered"),
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.
210 host_processor (void *cls,
211 const struct GNUNET_PeerIdentity * peer,
212 const struct GNUNET_HELLO_Message *hello,
215 struct HostSet *results = cls;
223 finish_response (results);
228 has_addr = GNUNET_NO;
229 GNUNET_HELLO_iterate_addresses (hello,
233 if (GNUNET_NO == has_addr)
235 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
236 "HELLO for peer `%4s' has no address, not suitable for hostlist!\n",
238 GNUNET_STATISTICS_update (stats,
239 gettext_noop("HELLOs without addresses encountered (ignored)"),
245 s = GNUNET_HELLO_size(hello);
246 #if DEBUG_HOSTLIST_SERVER
247 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
248 "Received %u bytes of `%s' from peer `%s' for hostlist.\n",
253 if (old + s >= GNUNET_MAX_MALLOC_CHECKED)
255 GNUNET_STATISTICS_update (stats,
256 gettext_noop("bytes not included in hostlist (size limit)"),
259 return; /* too large, skip! */
261 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
262 "Adding peer `%s' to hostlist (%u bytes)\n",
265 GNUNET_array_grow (results->data,
268 memcpy (&results->data[old], hello, s);
273 * Task that will produce a new response object.
276 update_response (void *cls,
277 const struct GNUNET_SCHEDULER_TaskContext *tc)
279 struct HostSet *results;
281 response_task = GNUNET_SCHEDULER_NO_TASK;
282 results = GNUNET_malloc(sizeof(struct HostSet));
283 pitr = GNUNET_PEERINFO_iterate (cfg, sched,
286 GNUNET_TIME_UNIT_MINUTES,
293 * Hostlist access policy (very permissive, allows everything).
296 accept_policy_callback (void *cls,
297 const struct sockaddr *addr, socklen_t addrlen)
299 if (NULL == response)
301 #if DEBUG_HOSTLIST_SERVER
302 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
303 "Received request for hostlist, but I am not yet ready; rejecting!\n");
307 return MHD_YES; /* accept all */
312 * Main request handler.
315 access_handler_callback (void *cls,
316 struct MHD_Connection *connection,
320 const char *upload_data,
321 size_t*upload_data_size, void **con_cls)
325 if (0 != strcmp (method, MHD_HTTP_METHOD_GET))
327 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
328 _("Refusing `%s' request to hostlist server\n"),
330 GNUNET_STATISTICS_update (stats,
331 gettext_noop("hostlist requests refused (not HTTP GET)"),
336 if (NULL == *con_cls)
339 #if DEBUG_HOSTLIST_SERVER
340 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
341 _("Sending 100 CONTINUE reply\n"));
343 return MHD_YES; /* send 100 continue */
345 if (*upload_data_size != 0)
347 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
348 _("Refusing `%s' request with %llu bytes of upload data\n"),
350 (unsigned long long) *upload_data_size);
351 GNUNET_STATISTICS_update (stats,
352 gettext_noop("hostlist requests refused (upload data)"),
355 return MHD_NO; /* do not support upload data */
357 if (response == NULL)
359 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
360 _("Could not handle hostlist request since I do not have a response yet\n"));
361 GNUNET_STATISTICS_update (stats,
362 gettext_noop("hostlist requests refused (not ready)"),
365 return MHD_NO; /* internal error, no response yet */
367 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
368 _("Received request for our hostlist\n"));
369 GNUNET_STATISTICS_update (stats,
370 gettext_noop("hostlist requests processed"),
373 return MHD_queue_response (connection, MHD_HTTP_OK, response);
377 * Handler called by core when core is ready to transmit message
379 * @param size size of buffer to copy message to
380 * @param buf buffer to copy message to
383 adv_transmit_ready ( void *cls, size_t size, void *buf)
385 size_t transmission_size;
386 size_t uri_size; /* Including \0 termination! */
387 uri_size = strlen ( hostlist_uri ) + 1;
389 struct GNUNET_HOSTLIST_ADV_Message * adv_message;
390 adv_message = GNUNET_malloc ( sizeof(struct GNUNET_HOSTLIST_ADV_Message) + uri_size);
391 if ( NULL == adv_message)
393 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR,
394 "Could not allocate memory for the message");
397 transmission_size = sizeof (struct GNUNET_HOSTLIST_ADV_Message) + uri_size;
399 adv_message->header.type = htons (GNUNET_MESSAGE_TYPE_HOSTLIST_ADVERTISEMENT);
400 adv_message->header.size = htons (transmission_size);
401 memcpy(&adv_message[1],hostlist_uri,uri_size);
405 GNUNET_log ( GNUNET_ERROR_TYPE_DEBUG, "Transmission failed, buffer invalid!\n" );
409 if ( size >= transmission_size )
411 memcpy ( buf, adv_message, transmission_size );
412 GNUNET_log ( GNUNET_ERROR_TYPE_DEBUG, "Sent advertisement message: Copied %d bytes into buffer!\n", transmission_size);
413 GNUNET_free ( adv_message );
414 return transmission_size;
417 hostlist_adv_count++;
418 GNUNET_STATISTICS_set (stats,
419 gettext_noop("# hostlist advertisements send"),
423 GNUNET_free (adv_message );
428 * Method that asks core service to transmit the message to the peer
429 * @param peer peer to transmit message to
430 * @param size size of the message
433 adv_transmit_message ( const struct GNUNET_PeerIdentity * peer, size_t size )
435 /* transmit message to peer */
438 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
439 _("Not connected to core, unable to send advertisement message\n"));
443 struct GNUNET_TIME_Relative timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, GNUNET_ADV_TIMEOUT);
444 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
445 _("Asked core to transmit advertisement message with a size of %u bytes\n"), size);
446 struct GNUNET_CORE_TransmitHandle * th;
447 th = GNUNET_CORE_notify_transmit_ready (core,
452 &adv_transmit_ready, NULL);
454 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
455 _("Advertisement message could not be queued by core\n"));
462 * Method that assembles our hostlist advertisement message
463 * @param peer peer to send the hostlist advertisement
466 adv_create_message ( const struct GNUNET_PeerIdentity * peer )
471 unsigned long long port;
474 char hostname[GNUNET_OS_get_hostname_max_length() + 1];
475 char *protocol = "http://";
476 char *port_s = GNUNET_malloc(6 * sizeof(char));
478 if (0 != gethostname (hostname, sizeof (hostname) - 1))
480 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR,
481 "Could not get system's hostname, unable to create advertisement message");
484 if (-1 == GNUNET_CONFIGURATION_get_value_number (cfg,
488 return GNUNET_SYSERR;
490 sprintf(port_s, "%llu", port);
491 length = strlen(hostname)+strlen(protocol)+strlen(port_s)+2;
492 size = (length+1) * sizeof (char);
493 uri = GNUNET_malloc(size);
494 uri = strcpy(uri, protocol);
495 uri = strcat(uri, hostname);
496 uri = strcat(uri, ":");
497 uri = strcat(uri, port_s);
498 uri = strcat(uri, "/");
500 strcpy(hostlist_uri,uri);
502 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Address to obtain hostlist: %s\n", hostlist_uri);
504 if ( ( size + sizeof( struct GNUNET_HOSTLIST_ADV_Message )) > GNUNET_SERVER_MAX_MESSAGE_SIZE)
506 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR,
507 "Advertisement message is bigger than GNUNET allows");
511 /* Request core to transmit message to peer */
512 size = size + sizeof ( struct GNUNET_HOSTLIST_ADV_Message );
513 adv_transmit_message(peer, size);
515 GNUNET_free ( port_s );
522 * Method called whenever a given peer connects.
525 * @param peer peer identity this notification is about
526 * @param latency reported latency of the connection with 'other'
527 * @param distance reported distance (DV) to 'other'
530 connect_handler (void *cls,
532 GNUNET_PeerIdentity * peer,
533 struct GNUNET_TIME_Relative latency,
539 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
540 "A new peer connected to the server, preparing to send hostlist advertisement\n");
541 /* create a new advertisement message */
542 if ( (GNUNET_OK != adv_create_message(peer)))
544 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
545 _(" GNUNET_OK Could not create a hostlist advertisement message, impossible to advertise hostlist\n"));
552 * Method called whenever a given peer disconnects.
555 * @param peer peer identity this notification is about
558 disconnect_handler (void *cls,
560 GNUNET_PeerIdentity * peer)
567 * Function that queries MHD's select sets and
568 * starts the task waiting for them.
570 static GNUNET_SCHEDULER_TaskIdentifier
571 prepare_daemon (struct MHD_Daemon *daemon_handle);
574 * Call MHD to process pending requests and then go back
575 * and schedule the next run.
578 run_daemon (void *cls,
579 const struct GNUNET_SCHEDULER_TaskContext *tc)
581 struct MHD_Daemon *daemon_handle = cls;
583 if (daemon_handle == daemon_handle_v4)
584 hostlist_task_v4 = GNUNET_SCHEDULER_NO_TASK;
586 hostlist_task_v6 = GNUNET_SCHEDULER_NO_TASK;
588 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
590 GNUNET_assert (MHD_YES == MHD_run (daemon_handle));
591 if (daemon_handle == daemon_handle_v4)
592 hostlist_task_v4 = prepare_daemon (daemon_handle);
594 hostlist_task_v6 = prepare_daemon (daemon_handle);
599 * Function that queries MHD's select sets and
600 * starts the task waiting for them.
602 static GNUNET_SCHEDULER_TaskIdentifier
603 prepare_daemon (struct MHD_Daemon *daemon_handle)
605 GNUNET_SCHEDULER_TaskIdentifier ret;
609 struct GNUNET_NETWORK_FDSet *wrs;
610 struct GNUNET_NETWORK_FDSet *wws;
611 struct GNUNET_NETWORK_FDSet *wes;
613 unsigned long long timeout;
615 struct GNUNET_TIME_Relative tv;
620 wrs = GNUNET_NETWORK_fdset_create ();
621 wes = GNUNET_NETWORK_fdset_create ();
622 wws = GNUNET_NETWORK_fdset_create ();
624 GNUNET_assert (MHD_YES ==
625 MHD_get_fdset (daemon_handle,
630 haveto = MHD_get_timeout (daemon_handle, &timeout);
631 if (haveto == MHD_YES)
632 tv.value = (uint64_t) timeout;
634 tv = GNUNET_TIME_UNIT_FOREVER_REL;
635 GNUNET_NETWORK_fdset_copy_native (wrs, &rs, max);
636 GNUNET_NETWORK_fdset_copy_native (wws, &ws, max);
637 GNUNET_NETWORK_fdset_copy_native (wes, &es, max);
638 ret = GNUNET_SCHEDULER_add_select (sched,
639 GNUNET_SCHEDULER_PRIORITY_HIGH,
640 GNUNET_SCHEDULER_NO_TASK,
646 GNUNET_NETWORK_fdset_destroy (wrs);
647 GNUNET_NETWORK_fdset_destroy (wws);
648 GNUNET_NETWORK_fdset_destroy (wes);
655 * Start server offering our hostlist.
657 * @return GNUNET_OK on success
660 GNUNET_HOSTLIST_server_start (const struct GNUNET_CONFIGURATION_Handle *c,
661 struct GNUNET_SCHEDULER_Handle *s,
662 struct GNUNET_STATISTICS_Handle *st,
663 struct GNUNET_CORE_Handle *co,
664 GNUNET_CORE_ConnectEventHandler *server_ch,
665 GNUNET_CORE_DisconnectEventHandler *server_dh,
668 unsigned long long port;
670 advertising = advertise;
672 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
673 "Advertising not enabled on this hostlist server\n");
675 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
676 "Advertising enabled on this hostlist server\n");
680 if (-1 == GNUNET_CONFIGURATION_get_value_number (cfg,
684 return GNUNET_SYSERR;
685 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
686 _("Hostlist service starts on port %llu\n"),
688 daemon_handle_v6 = MHD_start_daemon (MHD_USE_IPv6
689 #if DEBUG_HOSTLIST_SERVER
693 (unsigned short) port,
694 &accept_policy_callback,
696 &access_handler_callback,
698 MHD_OPTION_CONNECTION_LIMIT, (unsigned int) 16,
699 MHD_OPTION_PER_IP_CONNECTION_LIMIT, (unsigned int) 1,
700 MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) 16,
701 MHD_OPTION_CONNECTION_MEMORY_LIMIT, (size_t) (16 * 1024),
703 daemon_handle_v4 = MHD_start_daemon (MHD_NO_FLAG
704 #if DEBUG_HOSTLIST_SERVER
708 (unsigned short) port,
709 &accept_policy_callback,
711 &access_handler_callback,
713 MHD_OPTION_CONNECTION_LIMIT, (unsigned int) 16,
714 MHD_OPTION_PER_IP_CONNECTION_LIMIT, (unsigned int) 1,
715 MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) 16,
716 MHD_OPTION_CONNECTION_MEMORY_LIMIT, (size_t) (16 * 1024),
719 if ( (daemon_handle_v6 == NULL) &&
720 (daemon_handle_v4 == NULL) )
722 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
723 _("Could not start hostlist HTTP server on port %u\n"),
724 (unsigned short) port);
725 return GNUNET_SYSERR;
730 *server_ch = &connect_handler;
731 *server_dh = &disconnect_handler;
733 if (daemon_handle_v4 != NULL)
734 hostlist_task_v4 = prepare_daemon (daemon_handle_v4);
735 if (daemon_handle_v6 != NULL)
736 hostlist_task_v6 = prepare_daemon (daemon_handle_v6);
737 response_task = GNUNET_SCHEDULER_add_now (sched,
744 * Stop server offering our hostlist.
747 GNUNET_HOSTLIST_server_stop ()
749 #if DEBUG_HOSTLIST_SERVER
750 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
751 "Hostlist server shutdown\n");
753 if (GNUNET_SCHEDULER_NO_TASK != hostlist_task_v6)
755 GNUNET_SCHEDULER_cancel (sched, hostlist_task_v6);
756 hostlist_task_v6 = GNUNET_SCHEDULER_NO_TASK;
758 if (GNUNET_SCHEDULER_NO_TASK != hostlist_task_v4)
760 GNUNET_SCHEDULER_cancel (sched, hostlist_task_v4);
761 hostlist_task_v4 = GNUNET_SCHEDULER_NO_TASK;
765 GNUNET_PEERINFO_iterate_cancel (pitr);
768 if (GNUNET_SCHEDULER_NO_TASK != response_task)
770 GNUNET_SCHEDULER_cancel (sched, response_task);
771 response_task = GNUNET_SCHEDULER_NO_TASK;
773 if (NULL != daemon_handle_v4)
775 MHD_stop_daemon (daemon_handle_v4);
776 daemon_handle_v4 = NULL;
778 if (NULL != daemon_handle_v6)
780 MHD_stop_daemon (daemon_handle_v6);
781 daemon_handle_v6 = NULL;
783 if (response != NULL)
785 MHD_destroy_response (response);
790 /* end of hostlist-server.c */