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;
118 * Buffer for the hostlist address
120 static char * hostlist_uri;
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);
378 * Handler called by core when core is ready to transmit message
380 * @param size size of buffer to copy message to
381 * @param buf buffer to copy message to
384 adv_transmit_ready ( void *cls, size_t size, void *buf)
386 size_t transmission_size;
387 size_t uri_size; /* Including \0 termination! */
388 struct GNUNET_MessageHeader header;
393 GNUNET_log ( GNUNET_ERROR_TYPE_DEBUG,
394 "Transmission failed, buffer invalid!\n" );
397 uri_size = strlen ( hostlist_uri ) + 1;
398 transmission_size = sizeof (struct GNUNET_MessageHeader) + uri_size;
399 header.type = htons (GNUNET_MESSAGE_TYPE_HOSTLIST_ADVERTISEMENT);
400 header.size = htons (transmission_size);
401 GNUNET_assert (size >= transmission_size);
402 memcpy (buf, &header, sizeof (struct GNUNET_MessageHeader));
404 memcpy (&cbuf[sizeof (struct GNUNET_MessageHeader)],
405 hostlist_uri, uri_size);
406 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
407 "Sent advertisement message: Copied %u bytes into buffer!\n",
408 (unsigned int) transmission_size);
409 hostlist_adv_count++;
410 GNUNET_STATISTICS_set (stats,
411 gettext_noop("# hostlist advertisements send"),
414 return transmission_size;
419 * Method called whenever a given peer connects.
422 * @param peer peer identity this notification is about
423 * @param latency reported latency of the connection with 'other'
424 * @param distance reported distance (DV) to 'other'
427 connect_handler (void *cls,
429 GNUNET_PeerIdentity * peer,
430 struct GNUNET_TIME_Relative latency,
437 if (hostlist_uri == NULL)
439 size = strlen (hostlist_uri) + 1;
440 if (size + sizeof (struct GNUNET_MessageHeader) > GNUNET_SERVER_MAX_MESSAGE_SIZE)
445 size += sizeof (struct GNUNET_MessageHeader);
451 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
452 "Asked core to transmit advertisement message with a size of %u bytes\n",
454 if (NULL == GNUNET_CORE_notify_transmit_ready (core,
459 &adv_transmit_ready, NULL))
461 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
462 _("Advertisement message could not be queued by core\n"));
468 * Method called whenever a given peer disconnects.
471 * @param peer peer identity this notification is about
474 disconnect_handler (void *cls,
476 GNUNET_PeerIdentity * peer)
483 * Function that queries MHD's select sets and
484 * starts the task waiting for them.
486 static GNUNET_SCHEDULER_TaskIdentifier
487 prepare_daemon (struct MHD_Daemon *daemon_handle);
491 * Call MHD to process pending requests and then go back
492 * and schedule the next run.
495 run_daemon (void *cls,
496 const struct GNUNET_SCHEDULER_TaskContext *tc)
498 struct MHD_Daemon *daemon_handle = cls;
500 if (daemon_handle == daemon_handle_v4)
501 hostlist_task_v4 = GNUNET_SCHEDULER_NO_TASK;
503 hostlist_task_v6 = GNUNET_SCHEDULER_NO_TASK;
505 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
507 GNUNET_assert (MHD_YES == MHD_run (daemon_handle));
508 if (daemon_handle == daemon_handle_v4)
509 hostlist_task_v4 = prepare_daemon (daemon_handle);
511 hostlist_task_v6 = prepare_daemon (daemon_handle);
516 * Function that queries MHD's select sets and
517 * starts the task waiting for them.
519 static GNUNET_SCHEDULER_TaskIdentifier
520 prepare_daemon (struct MHD_Daemon *daemon_handle)
522 GNUNET_SCHEDULER_TaskIdentifier ret;
526 struct GNUNET_NETWORK_FDSet *wrs;
527 struct GNUNET_NETWORK_FDSet *wws;
528 struct GNUNET_NETWORK_FDSet *wes;
530 unsigned long long timeout;
532 struct GNUNET_TIME_Relative tv;
537 wrs = GNUNET_NETWORK_fdset_create ();
538 wes = GNUNET_NETWORK_fdset_create ();
539 wws = GNUNET_NETWORK_fdset_create ();
541 GNUNET_assert (MHD_YES ==
542 MHD_get_fdset (daemon_handle,
547 haveto = MHD_get_timeout (daemon_handle, &timeout);
548 if (haveto == MHD_YES)
549 tv.value = (uint64_t) timeout;
551 tv = GNUNET_TIME_UNIT_FOREVER_REL;
552 GNUNET_NETWORK_fdset_copy_native (wrs, &rs, max);
553 GNUNET_NETWORK_fdset_copy_native (wws, &ws, max);
554 GNUNET_NETWORK_fdset_copy_native (wes, &es, max);
555 ret = GNUNET_SCHEDULER_add_select (sched,
556 GNUNET_SCHEDULER_PRIORITY_HIGH,
557 GNUNET_SCHEDULER_NO_TASK,
563 GNUNET_NETWORK_fdset_destroy (wrs);
564 GNUNET_NETWORK_fdset_destroy (wws);
565 GNUNET_NETWORK_fdset_destroy (wes);
572 * Start server offering our hostlist.
574 * @return GNUNET_OK on success
577 GNUNET_HOSTLIST_server_start (const struct GNUNET_CONFIGURATION_Handle *c,
578 struct GNUNET_SCHEDULER_Handle *s,
579 struct GNUNET_STATISTICS_Handle *st,
580 struct GNUNET_CORE_Handle *co,
581 GNUNET_CORE_ConnectEventHandler *server_ch,
582 GNUNET_CORE_DisconnectEventHandler *server_dh,
585 unsigned long long port;
589 advertising = advertise;
591 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
592 "Advertising not enabled on this hostlist server\n");
594 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
595 "Advertising enabled on this hostlist server\n");
599 if (-1 == GNUNET_CONFIGURATION_get_value_number (cfg,
603 return GNUNET_SYSERR;
604 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
605 _("Hostlist service starts on port %llu\n"),
607 hostname = GNUNET_RESOLVER_local_hostname_get ();
608 if (NULL != hostname)
610 size = strlen (hostname);
611 if (size + 15 > MAX_URL_LEN)
617 GNUNET_asprintf (&hostlist_uri,
620 (unsigned int) port);
621 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
622 _("Address to obtain hostlist: `%s'\n"),
626 GNUNET_free ( hostname );
627 daemon_handle_v6 = MHD_start_daemon (MHD_USE_IPv6
628 #if DEBUG_HOSTLIST_SERVER
632 (unsigned short) port,
633 &accept_policy_callback,
635 &access_handler_callback,
637 MHD_OPTION_CONNECTION_LIMIT, (unsigned int) 16,
638 MHD_OPTION_PER_IP_CONNECTION_LIMIT, (unsigned int) 1,
639 MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) 16,
640 MHD_OPTION_CONNECTION_MEMORY_LIMIT, (size_t) (16 * 1024),
642 daemon_handle_v4 = MHD_start_daemon (MHD_NO_FLAG
643 #if DEBUG_HOSTLIST_SERVER
647 (unsigned short) port,
648 &accept_policy_callback,
650 &access_handler_callback,
652 MHD_OPTION_CONNECTION_LIMIT, (unsigned int) 16,
653 MHD_OPTION_PER_IP_CONNECTION_LIMIT, (unsigned int) 1,
654 MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) 16,
655 MHD_OPTION_CONNECTION_MEMORY_LIMIT, (size_t) (16 * 1024),
658 if ( (daemon_handle_v6 == NULL) &&
659 (daemon_handle_v4 == NULL) )
661 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
662 _("Could not start hostlist HTTP server on port %u\n"),
663 (unsigned short) port);
664 return GNUNET_SYSERR;
669 *server_ch = &connect_handler;
670 *server_dh = &disconnect_handler;
672 if (daemon_handle_v4 != NULL)
673 hostlist_task_v4 = prepare_daemon (daemon_handle_v4);
674 if (daemon_handle_v6 != NULL)
675 hostlist_task_v6 = prepare_daemon (daemon_handle_v6);
676 response_task = GNUNET_SCHEDULER_add_now (sched,
683 * Stop server offering our hostlist.
686 GNUNET_HOSTLIST_server_stop ()
688 #if DEBUG_HOSTLIST_SERVER
689 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
690 "Hostlist server shutdown\n");
692 if (GNUNET_SCHEDULER_NO_TASK != hostlist_task_v6)
694 GNUNET_SCHEDULER_cancel (sched, hostlist_task_v6);
695 hostlist_task_v6 = GNUNET_SCHEDULER_NO_TASK;
697 if (GNUNET_SCHEDULER_NO_TASK != hostlist_task_v4)
699 GNUNET_SCHEDULER_cancel (sched, hostlist_task_v4);
700 hostlist_task_v4 = GNUNET_SCHEDULER_NO_TASK;
704 GNUNET_PEERINFO_iterate_cancel (pitr);
707 if (GNUNET_SCHEDULER_NO_TASK != response_task)
709 GNUNET_SCHEDULER_cancel (sched, response_task);
710 response_task = GNUNET_SCHEDULER_NO_TASK;
712 if (NULL != daemon_handle_v4)
714 MHD_stop_daemon (daemon_handle_v4);
715 daemon_handle_v4 = NULL;
717 if (NULL != daemon_handle_v6)
719 MHD_stop_daemon (daemon_handle_v6);
720 daemon_handle_v6 = NULL;
722 if (response != NULL)
724 MHD_destroy_response (response);
729 /* end of hostlist-server.c */