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;
117 /* FIXME: define once... */
118 #define MAX_URL_LEN 1000
121 * Buffer for the hostlist address
123 static char * hostlist_uri;
127 * Task that will produce a new response object.
130 update_response (void *cls,
131 const struct GNUNET_SCHEDULER_TaskContext *tc);
135 * Function that assembles our response.
138 finish_response (struct HostSet *results)
140 struct GNUNET_TIME_Relative freq;
142 if (response != NULL)
143 MHD_destroy_response (response);
144 #if DEBUG_HOSTLIST_SERVER
145 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
146 "Creating hostlist response with %u bytes\n",
147 (unsigned int) results->size);
149 response = MHD_create_response_from_data (results->size,
150 results->data, MHD_YES, MHD_NO);
151 if ( (daemon_handle_v4 != NULL) ||
152 (daemon_handle_v6 != NULL) )
154 freq = RESPONSE_UPDATE_FREQUENCY;
155 if (results->size == 0)
156 freq = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 250);
157 /* schedule next update of the response */
158 response_task = GNUNET_SCHEDULER_add_delayed (sched,
165 /* already past shutdown */
166 MHD_destroy_response (response);
169 GNUNET_STATISTICS_set (stats,
170 gettext_noop("bytes in hostlist"),
173 GNUNET_free (results);
178 * Set 'cls' to GNUNET_YES (we have an address!).
180 * @param cls closure, an 'int*'
181 * @param tname name of the transport (ignored)
182 * @param expiration expiration time (call is ignored if this is in the past)
183 * @param addr the address (ignored)
184 * @param addrlen length of the address (ignored)
185 * @return GNUNET_SYSERR to stop iterating (unless expiration has occured)
188 check_has_addr (void *cls,
190 struct GNUNET_TIME_Absolute expiration,
191 const void *addr, size_t addrlen)
195 if (GNUNET_TIME_absolute_get_remaining (expiration).value == 0)
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.
213 host_processor (void *cls,
214 const struct GNUNET_PeerIdentity * peer,
215 const struct GNUNET_HELLO_Message *hello,
218 struct HostSet *results = cls;
226 finish_response (results);
231 has_addr = GNUNET_NO;
232 GNUNET_HELLO_iterate_addresses (hello,
236 if (GNUNET_NO == has_addr)
238 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
239 "HELLO for peer `%4s' has no address, not suitable for hostlist!\n",
241 GNUNET_STATISTICS_update (stats,
242 gettext_noop("HELLOs without addresses encountered (ignored)"),
248 s = GNUNET_HELLO_size(hello);
249 #if DEBUG_HOSTLIST_SERVER
250 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
251 "Received %u bytes of `%s' from peer `%s' for hostlist.\n",
256 if (old + s >= GNUNET_MAX_MALLOC_CHECKED)
258 GNUNET_STATISTICS_update (stats,
259 gettext_noop("bytes not included in hostlist (size limit)"),
262 return; /* too large, skip! */
264 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
265 "Adding peer `%s' to hostlist (%u bytes)\n",
268 GNUNET_array_grow (results->data,
271 memcpy (&results->data[old], hello, s);
276 * Task that will produce a new response object.
279 update_response (void *cls,
280 const struct GNUNET_SCHEDULER_TaskContext *tc)
282 struct HostSet *results;
284 response_task = GNUNET_SCHEDULER_NO_TASK;
285 results = GNUNET_malloc(sizeof(struct HostSet));
286 pitr = GNUNET_PEERINFO_iterate (cfg, sched,
289 GNUNET_TIME_UNIT_MINUTES,
296 * Hostlist access policy (very permissive, allows everything).
299 accept_policy_callback (void *cls,
300 const struct sockaddr *addr, socklen_t addrlen)
302 if (NULL == response)
304 #if DEBUG_HOSTLIST_SERVER
305 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
306 "Received request for hostlist, but I am not yet ready; rejecting!\n");
310 return MHD_YES; /* accept all */
315 * Main request handler.
318 access_handler_callback (void *cls,
319 struct MHD_Connection *connection,
323 const char *upload_data,
324 size_t*upload_data_size, void **con_cls)
328 if (0 != strcmp (method, MHD_HTTP_METHOD_GET))
330 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
331 _("Refusing `%s' request to hostlist server\n"),
333 GNUNET_STATISTICS_update (stats,
334 gettext_noop("hostlist requests refused (not HTTP GET)"),
339 if (NULL == *con_cls)
342 #if DEBUG_HOSTLIST_SERVER
343 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
344 _("Sending 100 CONTINUE reply\n"));
346 return MHD_YES; /* send 100 continue */
348 if (*upload_data_size != 0)
350 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
351 _("Refusing `%s' request with %llu bytes of upload data\n"),
353 (unsigned long long) *upload_data_size);
354 GNUNET_STATISTICS_update (stats,
355 gettext_noop("hostlist requests refused (upload data)"),
358 return MHD_NO; /* do not support upload data */
360 if (response == NULL)
362 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
363 _("Could not handle hostlist request since I do not have a response yet\n"));
364 GNUNET_STATISTICS_update (stats,
365 gettext_noop("hostlist requests refused (not ready)"),
368 return MHD_NO; /* internal error, no response yet */
370 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
371 _("Received request for our hostlist\n"));
372 GNUNET_STATISTICS_update (stats,
373 gettext_noop("hostlist requests processed"),
376 return MHD_queue_response (connection, MHD_HTTP_OK, response);
381 * Handler called by core when core is ready to transmit message
383 * @param size size of buffer to copy message to
384 * @param buf buffer to copy message to
387 adv_transmit_ready ( void *cls, size_t size, void *buf)
389 size_t transmission_size;
390 size_t uri_size; /* Including \0 termination! */
391 struct GNUNET_MessageHeader header;
396 GNUNET_log ( GNUNET_ERROR_TYPE_DEBUG,
397 "Transmission failed, buffer invalid!\n" );
400 uri_size = strlen ( hostlist_uri ) + 1;
401 transmission_size = sizeof (struct GNUNET_MessageHeader) + uri_size;
402 header.type = htons (GNUNET_MESSAGE_TYPE_HOSTLIST_ADVERTISEMENT);
403 header.size = htons (transmission_size);
404 GNUNET_assert (size >= transmission_size);
405 memcpy (buf, &header, sizeof (struct GNUNET_MessageHeader));
407 memcpy (&cbuf[sizeof (struct GNUNET_MessageHeader)],
408 hostlist_uri, uri_size);
409 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
410 "Sent advertisement message: Copied %u bytes into buffer!\n",
411 (unsigned int) transmission_size);
412 hostlist_adv_count++;
413 GNUNET_STATISTICS_set (stats,
414 gettext_noop("# hostlist advertisements send"),
417 return transmission_size;
422 * Method called whenever a given peer connects.
425 * @param peer peer identity this notification is about
426 * @param latency reported latency of the connection with 'other'
427 * @param distance reported distance (DV) to 'other'
430 connect_handler (void *cls,
432 GNUNET_PeerIdentity * peer,
433 struct GNUNET_TIME_Relative latency,
440 if (hostlist_uri == NULL)
442 size = strlen (hostlist_uri) + 1;
443 if (size + sizeof (struct GNUNET_MessageHeader) > GNUNET_SERVER_MAX_MESSAGE_SIZE)
448 size += sizeof (struct GNUNET_MessageHeader);
454 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
455 "Asked core to transmit advertisement message with a size of %u bytes\n",
457 if (NULL == GNUNET_CORE_notify_transmit_ready (core,
462 &adv_transmit_ready, NULL))
464 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
465 _("Advertisement message could not be queued by core\n"));
471 * Method called whenever a given peer disconnects.
474 * @param peer peer identity this notification is about
477 disconnect_handler (void *cls,
479 GNUNET_PeerIdentity * peer)
486 * Function that queries MHD's select sets and
487 * starts the task waiting for them.
489 static GNUNET_SCHEDULER_TaskIdentifier
490 prepare_daemon (struct MHD_Daemon *daemon_handle);
494 * Call MHD to process pending requests and then go back
495 * and schedule the next run.
498 run_daemon (void *cls,
499 const struct GNUNET_SCHEDULER_TaskContext *tc)
501 struct MHD_Daemon *daemon_handle = cls;
503 if (daemon_handle == daemon_handle_v4)
504 hostlist_task_v4 = GNUNET_SCHEDULER_NO_TASK;
506 hostlist_task_v6 = GNUNET_SCHEDULER_NO_TASK;
508 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
510 GNUNET_assert (MHD_YES == MHD_run (daemon_handle));
511 if (daemon_handle == daemon_handle_v4)
512 hostlist_task_v4 = prepare_daemon (daemon_handle);
514 hostlist_task_v6 = prepare_daemon (daemon_handle);
519 * Function that queries MHD's select sets and
520 * starts the task waiting for them.
522 static GNUNET_SCHEDULER_TaskIdentifier
523 prepare_daemon (struct MHD_Daemon *daemon_handle)
525 GNUNET_SCHEDULER_TaskIdentifier ret;
529 struct GNUNET_NETWORK_FDSet *wrs;
530 struct GNUNET_NETWORK_FDSet *wws;
531 struct GNUNET_NETWORK_FDSet *wes;
533 unsigned long long timeout;
535 struct GNUNET_TIME_Relative tv;
540 wrs = GNUNET_NETWORK_fdset_create ();
541 wes = GNUNET_NETWORK_fdset_create ();
542 wws = GNUNET_NETWORK_fdset_create ();
544 GNUNET_assert (MHD_YES ==
545 MHD_get_fdset (daemon_handle,
550 haveto = MHD_get_timeout (daemon_handle, &timeout);
551 if (haveto == MHD_YES)
552 tv.value = (uint64_t) timeout;
554 tv = GNUNET_TIME_UNIT_FOREVER_REL;
555 GNUNET_NETWORK_fdset_copy_native (wrs, &rs, max);
556 GNUNET_NETWORK_fdset_copy_native (wws, &ws, max);
557 GNUNET_NETWORK_fdset_copy_native (wes, &es, max);
558 ret = GNUNET_SCHEDULER_add_select (sched,
559 GNUNET_SCHEDULER_PRIORITY_HIGH,
560 GNUNET_SCHEDULER_NO_TASK,
566 GNUNET_NETWORK_fdset_destroy (wrs);
567 GNUNET_NETWORK_fdset_destroy (wws);
568 GNUNET_NETWORK_fdset_destroy (wes);
575 * Start server offering our hostlist.
577 * @return GNUNET_OK on success
580 GNUNET_HOSTLIST_server_start (const struct GNUNET_CONFIGURATION_Handle *c,
581 struct GNUNET_SCHEDULER_Handle *s,
582 struct GNUNET_STATISTICS_Handle *st,
583 struct GNUNET_CORE_Handle *co,
584 GNUNET_CORE_ConnectEventHandler *server_ch,
585 GNUNET_CORE_DisconnectEventHandler *server_dh,
588 unsigned long long port;
592 advertising = advertise;
594 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
595 "Advertising not enabled on this hostlist server\n");
597 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
598 "Advertising enabled on this hostlist server\n");
602 if (-1 == GNUNET_CONFIGURATION_get_value_number (cfg,
606 return GNUNET_SYSERR;
607 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
608 _("Hostlist service starts on port %llu\n"),
610 hostname = GNUNET_RESOLVER_local_hostname_get ();
611 if (NULL != hostname)
613 size = strlen (hostname);
614 if (size + 15 > MAX_URL_LEN)
620 GNUNET_asprintf (&hostlist_uri,
623 (unsigned int) port);
624 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
625 _("Address to obtain hostlist: `%s'\n"),
629 GNUNET_free ( hostname );
630 daemon_handle_v6 = MHD_start_daemon (MHD_USE_IPv6
631 #if DEBUG_HOSTLIST_SERVER
635 (unsigned short) port,
636 &accept_policy_callback,
638 &access_handler_callback,
640 MHD_OPTION_CONNECTION_LIMIT, (unsigned int) 16,
641 MHD_OPTION_PER_IP_CONNECTION_LIMIT, (unsigned int) 1,
642 MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) 16,
643 MHD_OPTION_CONNECTION_MEMORY_LIMIT, (size_t) (16 * 1024),
645 daemon_handle_v4 = MHD_start_daemon (MHD_NO_FLAG
646 #if DEBUG_HOSTLIST_SERVER
650 (unsigned short) port,
651 &accept_policy_callback,
653 &access_handler_callback,
655 MHD_OPTION_CONNECTION_LIMIT, (unsigned int) 16,
656 MHD_OPTION_PER_IP_CONNECTION_LIMIT, (unsigned int) 1,
657 MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) 16,
658 MHD_OPTION_CONNECTION_MEMORY_LIMIT, (size_t) (16 * 1024),
661 if ( (daemon_handle_v6 == NULL) &&
662 (daemon_handle_v4 == NULL) )
664 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
665 _("Could not start hostlist HTTP server on port %u\n"),
666 (unsigned short) port);
667 return GNUNET_SYSERR;
672 *server_ch = &connect_handler;
673 *server_dh = &disconnect_handler;
675 if (daemon_handle_v4 != NULL)
676 hostlist_task_v4 = prepare_daemon (daemon_handle_v4);
677 if (daemon_handle_v6 != NULL)
678 hostlist_task_v6 = prepare_daemon (daemon_handle_v6);
679 response_task = GNUNET_SCHEDULER_add_now (sched,
686 * Stop server offering our hostlist.
689 GNUNET_HOSTLIST_server_stop ()
691 #if DEBUG_HOSTLIST_SERVER
692 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
693 "Hostlist server shutdown\n");
695 if (GNUNET_SCHEDULER_NO_TASK != hostlist_task_v6)
697 GNUNET_SCHEDULER_cancel (sched, hostlist_task_v6);
698 hostlist_task_v6 = GNUNET_SCHEDULER_NO_TASK;
700 if (GNUNET_SCHEDULER_NO_TASK != hostlist_task_v4)
702 GNUNET_SCHEDULER_cancel (sched, hostlist_task_v4);
703 hostlist_task_v4 = GNUNET_SCHEDULER_NO_TASK;
707 GNUNET_PEERINFO_iterate_cancel (pitr);
710 if (GNUNET_SCHEDULER_NO_TASK != response_task)
712 GNUNET_SCHEDULER_cancel (sched, response_task);
713 response_task = GNUNET_SCHEDULER_NO_TASK;
715 if (NULL != daemon_handle_v4)
717 MHD_stop_daemon (daemon_handle_v4);
718 daemon_handle_v4 = NULL;
720 if (NULL != daemon_handle_v6)
722 MHD_stop_daemon (daemon_handle_v6);
723 daemon_handle_v6 = NULL;
725 if (response != NULL)
727 MHD_destroy_response (response);
732 /* end of hostlist-server.c */