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 * Handle for accessing peerinfo service.
100 static struct GNUNET_PEERINFO_Handle *peerinfo;
103 * Context for host processor.
113 * Set if we are allowed to advertise our hostlist to others.
115 static int advertising;
118 * How many times was the hostlist advertised?
120 static uint64_t hostlist_adv_count;
123 * Buffer for the hostlist address
125 static char * hostlist_uri;
129 * Task that will produce a new response object.
132 update_response (void *cls,
133 const struct GNUNET_SCHEDULER_TaskContext *tc);
137 * Function that assembles our response.
140 finish_response (struct HostSet *results)
142 struct GNUNET_TIME_Relative freq;
144 if (response != NULL)
145 MHD_destroy_response (response);
146 #if DEBUG_HOSTLIST_SERVER
147 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
148 "Creating hostlist response with %u bytes\n",
149 (unsigned int) results->size);
151 response = MHD_create_response_from_data (results->size,
152 results->data, MHD_YES, MHD_NO);
153 if ( (daemon_handle_v4 != NULL) ||
154 (daemon_handle_v6 != NULL) )
156 freq = RESPONSE_UPDATE_FREQUENCY;
157 if (results->size == 0)
158 freq = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 250);
159 /* schedule next update of the response */
160 response_task = GNUNET_SCHEDULER_add_delayed (sched,
167 /* already past shutdown */
168 MHD_destroy_response (response);
171 GNUNET_STATISTICS_set (stats,
172 gettext_noop("bytes in hostlist"),
175 GNUNET_free (results);
180 * Set 'cls' to GNUNET_YES (we have an address!).
182 * @param cls closure, an 'int*'
183 * @param tname name of the transport (ignored)
184 * @param expiration expiration time (call is ignored if this is in the past)
185 * @param addr the address (ignored)
186 * @param addrlen length of the address (ignored)
187 * @return GNUNET_SYSERR to stop iterating (unless expiration has occured)
190 check_has_addr (void *cls,
192 struct GNUNET_TIME_Absolute expiration,
193 const void *addr, size_t addrlen)
197 if (GNUNET_TIME_absolute_get_remaining (expiration).value == 0)
199 GNUNET_STATISTICS_update (stats,
200 gettext_noop("expired addresses encountered"),
203 return GNUNET_YES; /* ignore this address */
206 return GNUNET_SYSERR;
211 * Callback that processes each of the known HELLOs for the
212 * hostlist response construction.
215 host_processor (void *cls,
216 const struct GNUNET_PeerIdentity * peer,
217 const struct GNUNET_HELLO_Message *hello,
220 struct HostSet *results = cls;
228 finish_response (results);
233 has_addr = GNUNET_NO;
234 GNUNET_HELLO_iterate_addresses (hello,
238 if (GNUNET_NO == has_addr)
240 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
241 "HELLO for peer `%4s' has no address, not suitable for hostlist!\n",
243 GNUNET_STATISTICS_update (stats,
244 gettext_noop("HELLOs without addresses encountered (ignored)"),
250 s = GNUNET_HELLO_size(hello);
251 #if DEBUG_HOSTLIST_SERVER
252 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
253 "Received %u bytes of `%s' from peer `%s' for hostlist.\n",
258 if (old + s >= GNUNET_MAX_MALLOC_CHECKED)
260 GNUNET_STATISTICS_update (stats,
261 gettext_noop("bytes not included in hostlist (size limit)"),
264 return; /* too large, skip! */
266 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
267 "Adding peer `%s' to hostlist (%u bytes)\n",
270 GNUNET_array_grow (results->data,
273 memcpy (&results->data[old], hello, s);
278 * Task that will produce a new response object.
281 update_response (void *cls,
282 const struct GNUNET_SCHEDULER_TaskContext *tc)
284 struct HostSet *results;
286 response_task = GNUNET_SCHEDULER_NO_TASK;
287 results = GNUNET_malloc(sizeof(struct HostSet));
288 GNUNET_assert (peerinfo != NULL);
289 pitr = GNUNET_PEERINFO_iterate (peerinfo,
292 GNUNET_TIME_UNIT_MINUTES,
299 * Hostlist access policy (very permissive, allows everything).
302 accept_policy_callback (void *cls,
303 const struct sockaddr *addr, socklen_t addrlen)
305 if (NULL == response)
307 #if DEBUG_HOSTLIST_SERVER
308 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
309 "Received request for hostlist, but I am not yet ready; rejecting!\n");
313 return MHD_YES; /* accept all */
318 * Main request handler.
321 access_handler_callback (void *cls,
322 struct MHD_Connection *connection,
326 const char *upload_data,
327 size_t*upload_data_size, void **con_cls)
331 if (0 != strcmp (method, MHD_HTTP_METHOD_GET))
333 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
334 _("Refusing `%s' request to hostlist server\n"),
336 GNUNET_STATISTICS_update (stats,
337 gettext_noop("hostlist requests refused (not HTTP GET)"),
342 if (NULL == *con_cls)
345 #if DEBUG_HOSTLIST_SERVER
346 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
347 _("Sending 100 CONTINUE reply\n"));
349 return MHD_YES; /* send 100 continue */
351 if (*upload_data_size != 0)
353 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
354 _("Refusing `%s' request with %llu bytes of upload data\n"),
356 (unsigned long long) *upload_data_size);
357 GNUNET_STATISTICS_update (stats,
358 gettext_noop("hostlist requests refused (upload data)"),
361 return MHD_NO; /* do not support upload data */
363 if (response == NULL)
365 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
366 _("Could not handle hostlist request since I do not have a response yet\n"));
367 GNUNET_STATISTICS_update (stats,
368 gettext_noop("hostlist requests refused (not ready)"),
371 return MHD_NO; /* internal error, no response yet */
373 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
374 _("Received request for our hostlist\n"));
375 GNUNET_STATISTICS_update (stats,
376 gettext_noop("hostlist requests processed"),
379 return MHD_queue_response (connection, MHD_HTTP_OK, response);
384 * Handler called by core when core is ready to transmit message
386 * @param size size of buffer to copy message to
387 * @param buf buffer to copy message to
390 adv_transmit_ready ( void *cls, size_t size, void *buf)
392 size_t transmission_size;
393 size_t uri_size; /* Including \0 termination! */
394 struct GNUNET_MessageHeader header;
399 GNUNET_log ( GNUNET_ERROR_TYPE_DEBUG,
400 "Transmission failed, buffer invalid!\n" );
403 uri_size = strlen ( hostlist_uri ) + 1;
404 transmission_size = sizeof (struct GNUNET_MessageHeader) + uri_size;
405 header.type = htons (GNUNET_MESSAGE_TYPE_HOSTLIST_ADVERTISEMENT);
406 header.size = htons (transmission_size);
407 GNUNET_assert (size >= transmission_size);
408 memcpy (buf, &header, sizeof (struct GNUNET_MessageHeader));
410 memcpy (&cbuf[sizeof (struct GNUNET_MessageHeader)],
411 hostlist_uri, uri_size);
412 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
413 "Sent advertisement message: Copied %u bytes into buffer!\n",
414 (unsigned int) transmission_size);
415 hostlist_adv_count++;
416 GNUNET_STATISTICS_set (stats,
417 gettext_noop("# hostlist advertisements send"),
420 return transmission_size;
425 * Method called whenever a given peer connects.
428 * @param peer peer identity this notification is about
429 * @param latency reported latency of the connection with 'other'
430 * @param distance reported distance (DV) to 'other'
433 connect_handler (void *cls,
435 GNUNET_PeerIdentity * peer,
436 struct GNUNET_TIME_Relative latency,
443 if (hostlist_uri == NULL)
445 size = strlen (hostlist_uri) + 1;
446 if (size + sizeof (struct GNUNET_MessageHeader) > GNUNET_SERVER_MAX_MESSAGE_SIZE)
451 size += sizeof (struct GNUNET_MessageHeader);
457 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
458 "Asked core to transmit advertisement message with a size of %u bytes\n",
460 if (NULL == GNUNET_CORE_notify_transmit_ready (core,
465 &adv_transmit_ready, NULL))
467 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
468 _("Advertisement message could not be queued by core\n"));
474 * Method called whenever a given peer disconnects.
477 * @param peer peer identity this notification is about
480 disconnect_handler (void *cls,
482 GNUNET_PeerIdentity * peer)
489 * Function that queries MHD's select sets and
490 * starts the task waiting for them.
492 static GNUNET_SCHEDULER_TaskIdentifier
493 prepare_daemon (struct MHD_Daemon *daemon_handle);
497 * Call MHD to process pending requests and then go back
498 * and schedule the next run.
501 run_daemon (void *cls,
502 const struct GNUNET_SCHEDULER_TaskContext *tc)
504 struct MHD_Daemon *daemon_handle = cls;
506 if (daemon_handle == daemon_handle_v4)
507 hostlist_task_v4 = GNUNET_SCHEDULER_NO_TASK;
509 hostlist_task_v6 = GNUNET_SCHEDULER_NO_TASK;
511 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
513 GNUNET_assert (MHD_YES == MHD_run (daemon_handle));
514 if (daemon_handle == daemon_handle_v4)
515 hostlist_task_v4 = prepare_daemon (daemon_handle);
517 hostlist_task_v6 = prepare_daemon (daemon_handle);
522 * Function that queries MHD's select sets and
523 * starts the task waiting for them.
525 static GNUNET_SCHEDULER_TaskIdentifier
526 prepare_daemon (struct MHD_Daemon *daemon_handle)
528 GNUNET_SCHEDULER_TaskIdentifier ret;
532 struct GNUNET_NETWORK_FDSet *wrs;
533 struct GNUNET_NETWORK_FDSet *wws;
534 struct GNUNET_NETWORK_FDSet *wes;
536 unsigned long long timeout;
538 struct GNUNET_TIME_Relative tv;
543 wrs = GNUNET_NETWORK_fdset_create ();
544 wes = GNUNET_NETWORK_fdset_create ();
545 wws = GNUNET_NETWORK_fdset_create ();
547 GNUNET_assert (MHD_YES ==
548 MHD_get_fdset (daemon_handle,
553 haveto = MHD_get_timeout (daemon_handle, &timeout);
554 if (haveto == MHD_YES)
555 tv.value = (uint64_t) timeout;
557 tv = GNUNET_TIME_UNIT_FOREVER_REL;
558 GNUNET_NETWORK_fdset_copy_native (wrs, &rs, max);
559 GNUNET_NETWORK_fdset_copy_native (wws, &ws, max);
560 GNUNET_NETWORK_fdset_copy_native (wes, &es, max);
561 ret = GNUNET_SCHEDULER_add_select (sched,
562 GNUNET_SCHEDULER_PRIORITY_HIGH,
563 GNUNET_SCHEDULER_NO_TASK,
569 GNUNET_NETWORK_fdset_destroy (wrs);
570 GNUNET_NETWORK_fdset_destroy (wws);
571 GNUNET_NETWORK_fdset_destroy (wes);
578 * Start server offering our hostlist.
580 * @return GNUNET_OK on success
583 GNUNET_HOSTLIST_server_start (const struct GNUNET_CONFIGURATION_Handle *c,
584 struct GNUNET_SCHEDULER_Handle *s,
585 struct GNUNET_STATISTICS_Handle *st,
586 struct GNUNET_CORE_Handle *co,
587 GNUNET_CORE_ConnectEventHandler *server_ch,
588 GNUNET_CORE_DisconnectEventHandler *server_dh,
591 unsigned long long port;
595 advertising = advertise;
597 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
598 "Advertising not enabled on this hostlist server\n");
600 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
601 "Advertising enabled on this hostlist server\n");
605 peerinfo = GNUNET_PEERINFO_connect (sched, cfg);
606 if (peerinfo == NULL)
608 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
609 _("Could not access PEERINFO service. Exiting.\n"));
610 return GNUNET_SYSERR;
612 if (-1 == GNUNET_CONFIGURATION_get_value_number (cfg,
616 return GNUNET_SYSERR;
617 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
618 _("Hostlist service starts on port %llu\n"),
620 hostname = GNUNET_RESOLVER_local_hostname_get ();
621 if (NULL != hostname)
623 size = strlen (hostname);
624 if (size + 15 > MAX_URL_LEN)
630 GNUNET_asprintf (&hostlist_uri,
633 (unsigned int) port);
634 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
635 _("Address to obtain hostlist: `%s'\n"),
639 GNUNET_free ( hostname );
640 daemon_handle_v6 = MHD_start_daemon (MHD_USE_IPv6
641 #if DEBUG_HOSTLIST_SERVER
645 (unsigned short) port,
646 &accept_policy_callback,
648 &access_handler_callback,
650 MHD_OPTION_CONNECTION_LIMIT, (unsigned int) 16,
651 MHD_OPTION_PER_IP_CONNECTION_LIMIT, (unsigned int) 1,
652 MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) 16,
653 MHD_OPTION_CONNECTION_MEMORY_LIMIT, (size_t) (16 * 1024),
655 daemon_handle_v4 = MHD_start_daemon (MHD_NO_FLAG
656 #if DEBUG_HOSTLIST_SERVER
660 (unsigned short) port,
661 &accept_policy_callback,
663 &access_handler_callback,
665 MHD_OPTION_CONNECTION_LIMIT, (unsigned int) 16,
666 MHD_OPTION_PER_IP_CONNECTION_LIMIT, (unsigned int) 1,
667 MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) 16,
668 MHD_OPTION_CONNECTION_MEMORY_LIMIT, (size_t) (16 * 1024),
671 if ( (daemon_handle_v6 == NULL) &&
672 (daemon_handle_v4 == NULL) )
674 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
675 _("Could not start hostlist HTTP server on port %u\n"),
676 (unsigned short) port);
677 return GNUNET_SYSERR;
682 *server_ch = &connect_handler;
683 *server_dh = &disconnect_handler;
685 if (daemon_handle_v4 != NULL)
686 hostlist_task_v4 = prepare_daemon (daemon_handle_v4);
687 if (daemon_handle_v6 != NULL)
688 hostlist_task_v6 = prepare_daemon (daemon_handle_v6);
689 response_task = GNUNET_SCHEDULER_add_now (sched,
696 * Stop server offering our hostlist.
699 GNUNET_HOSTLIST_server_stop ()
701 #if DEBUG_HOSTLIST_SERVER
702 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
703 "Hostlist server shutdown\n");
705 if (GNUNET_SCHEDULER_NO_TASK != hostlist_task_v6)
707 GNUNET_SCHEDULER_cancel (sched, hostlist_task_v6);
708 hostlist_task_v6 = GNUNET_SCHEDULER_NO_TASK;
710 if (GNUNET_SCHEDULER_NO_TASK != hostlist_task_v4)
712 GNUNET_SCHEDULER_cancel (sched, hostlist_task_v4);
713 hostlist_task_v4 = GNUNET_SCHEDULER_NO_TASK;
717 GNUNET_PEERINFO_iterate_cancel (pitr);
720 if (GNUNET_SCHEDULER_NO_TASK != response_task)
722 GNUNET_SCHEDULER_cancel (sched, response_task);
723 response_task = GNUNET_SCHEDULER_NO_TASK;
725 if (NULL != daemon_handle_v4)
727 MHD_stop_daemon (daemon_handle_v4);
728 daemon_handle_v4 = NULL;
730 if (NULL != daemon_handle_v6)
732 MHD_stop_daemon (daemon_handle_v6);
733 daemon_handle_v6 = NULL;
735 if (response != NULL)
737 MHD_destroy_response (response);
740 if (peerinfo != NULL)
742 GNUNET_PEERINFO_disconnect (peerinfo);
747 /* end of hostlist-server.c */