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 3, 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 * Handle to the HTTP server as provided by libmicrohttpd for IPv6.
40 static struct MHD_Daemon *daemon_handle_v6;
43 * Handle to the HTTP server as provided by libmicrohttpd for IPv4.
45 static struct MHD_Daemon *daemon_handle_v4;
50 static const struct GNUNET_CONFIGURATION_Handle *cfg;
53 * For keeping statistics.
55 static struct GNUNET_STATISTICS_Handle *stats;
58 * Handle to the core service (NULL until we've connected to it).
60 static struct GNUNET_CORE_Handle *core;
63 * Handle to the peerinfo notify service (NULL until we've connected to it).
65 static struct GNUNET_PEERINFO_NotifyContext *notify;
68 * Our primary task for IPv4.
70 static GNUNET_SCHEDULER_TaskIdentifier hostlist_task_v4;
73 * Our primary task for IPv6.
75 static GNUNET_SCHEDULER_TaskIdentifier hostlist_task_v6;
78 * Our canonical response.
80 static struct MHD_Response *response;
83 * NULL if we are not currenlty iterating over peer information.
85 static struct GNUNET_PEERINFO_IteratorContext *pitr;
88 * Handle for accessing peerinfo service.
90 static struct GNUNET_PEERINFO_Handle *peerinfo;
93 * Context for host processor.
103 * Set if we are allowed to advertise our hostlist to others.
105 static int advertising;
108 * Buffer for the hostlist address
110 static char * hostlist_uri;
114 * Function that assembles our response.
117 finish_response (struct HostSet *results)
119 if (response != NULL)
120 MHD_destroy_response (response);
121 #if DEBUG_HOSTLIST_SERVER
122 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
123 "Creating hostlist response with %u bytes\n",
124 (unsigned int) results->size);
126 response = MHD_create_response_from_data (results->size,
127 results->data, MHD_YES, MHD_NO);
128 if ( (daemon_handle_v4 == NULL) &&
129 (daemon_handle_v6 == NULL) )
131 MHD_destroy_response (response);
134 GNUNET_STATISTICS_set (stats,
135 gettext_noop("bytes in hostlist"),
138 GNUNET_free (results);
143 * Set 'cls' to GNUNET_YES (we have an address!).
145 * @param cls closure, an 'int*'
146 * @param tname name of the transport (ignored)
147 * @param expiration expiration time (call is ignored if this is in the past)
148 * @param addr the address (ignored)
149 * @param addrlen length of the address (ignored)
150 * @return GNUNET_SYSERR to stop iterating (unless expiration has occured)
153 check_has_addr (void *cls,
155 struct GNUNET_TIME_Absolute expiration,
161 if (GNUNET_TIME_absolute_get_remaining (expiration).rel_value == 0)
163 GNUNET_STATISTICS_update (stats,
164 gettext_noop("expired addresses encountered"),
167 return GNUNET_YES; /* ignore this address */
170 return GNUNET_SYSERR;
175 * Callback that processes each of the known HELLOs for the
176 * hostlist response construction.
179 host_processor (void *cls,
180 const struct GNUNET_PeerIdentity * peer,
181 const struct GNUNET_HELLO_Message *hello,
184 struct HostSet *results = cls;
191 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
192 _("Error in communication with PEERINFO service:\n `%s'"), err_msg);
199 finish_response (results);
204 has_addr = GNUNET_NO;
205 GNUNET_HELLO_iterate_addresses (hello,
209 if (GNUNET_NO == has_addr)
211 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
212 "HELLO for peer `%4s' has no address, not suitable for hostlist!\n",
214 GNUNET_STATISTICS_update (stats,
215 gettext_noop("HELLOs without addresses encountered (ignored)"),
221 s = GNUNET_HELLO_size(hello);
222 #if DEBUG_HOSTLIST_SERVER
223 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
224 "Received %u bytes of `%s' from peer `%s' for hostlist.\n",
229 if ( (old + s >= GNUNET_MAX_MALLOC_CHECKED) || (old + s >= MAX_BYTES_PER_HOSTLISTS) )
231 GNUNET_STATISTICS_update (stats,
232 gettext_noop("bytes not included in hostlist (size limit)"),
235 return; /* too large, skip! */
237 #if DEBUG_HOSTLIST_SERVER
238 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
239 "Adding peer `%s' to hostlist (%u bytes)\n",
243 GNUNET_array_grow (results->data,
246 memcpy (&results->data[old], hello, s);
252 * Hostlist access policy (very permissive, allows everything).
255 accept_policy_callback (void *cls,
256 const struct sockaddr *addr, socklen_t addrlen)
258 if (NULL == response)
260 #if DEBUG_HOSTLIST_SERVER
261 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
262 "Received request for hostlist, but I am not yet ready; rejecting!\n");
266 return MHD_YES; /* accept all */
271 * Main request handler.
274 access_handler_callback (void *cls,
275 struct MHD_Connection *connection,
279 const char *upload_data,
280 size_t*upload_data_size, void **con_cls)
284 if (0 != strcmp (method, MHD_HTTP_METHOD_GET))
286 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
287 _("Refusing `%s' request to hostlist server\n"),
289 GNUNET_STATISTICS_update (stats,
290 gettext_noop("hostlist requests refused (not HTTP GET)"),
295 if (NULL == *con_cls)
298 #if DEBUG_HOSTLIST_SERVER
299 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
300 _("Sending 100 CONTINUE reply\n"));
302 return MHD_YES; /* send 100 continue */
304 if (*upload_data_size != 0)
306 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
307 _("Refusing `%s' request with %llu bytes of upload data\n"),
309 (unsigned long long) *upload_data_size);
310 GNUNET_STATISTICS_update (stats,
311 gettext_noop("hostlist requests refused (upload data)"),
314 return MHD_NO; /* do not support upload data */
316 if (response == NULL)
318 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
319 _("Could not handle hostlist request since I do not have a response yet\n"));
320 GNUNET_STATISTICS_update (stats,
321 gettext_noop("hostlist requests refused (not ready)"),
324 return MHD_NO; /* internal error, no response yet */
326 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
327 _("Received request for our hostlist\n"));
328 GNUNET_STATISTICS_update (stats,
329 gettext_noop("hostlist requests processed"),
332 return MHD_queue_response (connection, MHD_HTTP_OK, response);
337 * Handler called by core when core is ready to transmit message
339 * @param size size of buffer to copy message to
340 * @param buf buffer to copy message to
343 adv_transmit_ready ( void *cls, size_t size, void *buf)
345 static uint64_t hostlist_adv_count;
347 size_t transmission_size;
348 size_t uri_size; /* Including \0 termination! */
349 struct GNUNET_MessageHeader header;
354 GNUNET_log ( GNUNET_ERROR_TYPE_DEBUG,
355 "Transmission failed, buffer invalid!\n" );
358 uri_size = strlen ( hostlist_uri ) + 1;
359 transmission_size = sizeof (struct GNUNET_MessageHeader) + uri_size;
360 header.type = htons (GNUNET_MESSAGE_TYPE_HOSTLIST_ADVERTISEMENT);
361 header.size = htons (transmission_size);
362 GNUNET_assert (size >= transmission_size);
363 memcpy (buf, &header, sizeof (struct GNUNET_MessageHeader));
365 memcpy (&cbuf[sizeof (struct GNUNET_MessageHeader)],
366 hostlist_uri, uri_size);
367 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
368 "Sent advertisement message: Copied %u bytes into buffer!\n",
369 (unsigned int) transmission_size);
370 hostlist_adv_count++;
371 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
372 " # Sent advertisement message: %u\n",
374 GNUNET_STATISTICS_update (stats,
375 gettext_noop("# hostlist advertisements send"),
378 return transmission_size;
383 * Method called whenever a given peer connects.
386 * @param peer peer identity this notification is about
387 * @param atsi performance data
390 connect_handler (void *cls,
392 GNUNET_PeerIdentity * peer,
393 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
399 if (hostlist_uri == NULL)
401 size = strlen (hostlist_uri) + 1;
402 if (size + sizeof (struct GNUNET_MessageHeader) >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
407 size += sizeof (struct GNUNET_MessageHeader);
413 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
414 "Asked core to transmit advertisement message with a size of %u bytes to peer `%s'\n",
415 size,GNUNET_i2s(peer));
416 if (NULL == GNUNET_CORE_notify_transmit_ready (core,
421 &adv_transmit_ready, NULL))
423 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
424 _("Advertisement message could not be queued by core\n"));
430 * Method called whenever a given peer disconnects.
433 * @param peer peer identity this notification is about
436 disconnect_handler (void *cls,
438 GNUNET_PeerIdentity * peer)
444 * PEERINFO calls this function to let us know about a possible peer
445 * that we might want to connect to.
447 * @param cls closure (not used)
448 * @param peer potential peer to connect to
449 * @param hello HELLO for this peer (or NULL)
450 * @param err_msg NULL if successful, otherwise contains error message
453 process_notify (void *cls,
454 const struct GNUNET_PeerIdentity *peer,
455 const struct GNUNET_HELLO_Message *hello,
458 struct HostSet *results;
459 #if DEBUG_HOSTLIST_SERVER
460 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
461 "Peerinfo is notifying us to rebuild our hostlist\n");
465 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
466 _("Error in communication with PEERINFO service\n"));
469 results = GNUNET_malloc(sizeof(struct HostSet));
470 GNUNET_assert (peerinfo != NULL);
471 pitr = GNUNET_PEERINFO_iterate (peerinfo,
473 GNUNET_TIME_UNIT_MINUTES,
479 * Function that queries MHD's select sets and
480 * starts the task waiting for them.
482 static GNUNET_SCHEDULER_TaskIdentifier
483 prepare_daemon (struct MHD_Daemon *daemon_handle);
487 * Call MHD to process pending requests and then go back
488 * and schedule the next run.
491 run_daemon (void *cls,
492 const struct GNUNET_SCHEDULER_TaskContext *tc)
494 struct MHD_Daemon *daemon_handle = cls;
496 if (daemon_handle == daemon_handle_v4)
497 hostlist_task_v4 = GNUNET_SCHEDULER_NO_TASK;
499 hostlist_task_v6 = GNUNET_SCHEDULER_NO_TASK;
501 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
503 GNUNET_assert (MHD_YES == MHD_run (daemon_handle));
504 if (daemon_handle == daemon_handle_v4)
505 hostlist_task_v4 = prepare_daemon (daemon_handle);
507 hostlist_task_v6 = prepare_daemon (daemon_handle);
512 * Function that queries MHD's select sets and
513 * starts the task waiting for them.
515 static GNUNET_SCHEDULER_TaskIdentifier
516 prepare_daemon (struct MHD_Daemon *daemon_handle)
518 GNUNET_SCHEDULER_TaskIdentifier ret;
522 struct GNUNET_NETWORK_FDSet *wrs;
523 struct GNUNET_NETWORK_FDSet *wws;
524 struct GNUNET_NETWORK_FDSet *wes;
526 unsigned long long timeout;
528 struct GNUNET_TIME_Relative tv;
533 wrs = GNUNET_NETWORK_fdset_create ();
534 wes = GNUNET_NETWORK_fdset_create ();
535 wws = GNUNET_NETWORK_fdset_create ();
537 GNUNET_assert (MHD_YES ==
538 MHD_get_fdset (daemon_handle,
543 haveto = MHD_get_timeout (daemon_handle, &timeout);
544 if (haveto == MHD_YES)
545 tv.rel_value = (uint64_t) timeout;
547 tv = GNUNET_TIME_UNIT_FOREVER_REL;
548 GNUNET_NETWORK_fdset_copy_native (wrs, &rs, max + 1);
549 GNUNET_NETWORK_fdset_copy_native (wws, &ws, max + 1);
550 GNUNET_NETWORK_fdset_copy_native (wes, &es, max + 1);
551 ret = GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_HIGH,
552 GNUNET_SCHEDULER_NO_TASK,
558 GNUNET_NETWORK_fdset_destroy (wrs);
559 GNUNET_NETWORK_fdset_destroy (wws);
560 GNUNET_NETWORK_fdset_destroy (wes);
567 * Start server offering our hostlist.
569 * @return GNUNET_OK on success
572 GNUNET_HOSTLIST_server_start (const struct GNUNET_CONFIGURATION_Handle *c,
573 struct GNUNET_STATISTICS_Handle *st,
574 struct GNUNET_CORE_Handle *co,
575 GNUNET_CORE_ConnectEventHandler *server_ch,
576 GNUNET_CORE_DisconnectEventHandler *server_dh,
579 unsigned long long port;
583 advertising = advertise;
585 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
586 "Advertising not enabled on this hostlist server\n");
588 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
589 "Advertising enabled on this hostlist server\n");
592 peerinfo = GNUNET_PEERINFO_connect (cfg);
593 if (peerinfo == NULL)
595 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
596 _("Could not access PEERINFO service. Exiting.\n"));
597 return GNUNET_SYSERR;
599 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (cfg,
603 return GNUNET_SYSERR;
605 (port > UINT16_MAX) )
607 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
608 _("Invalid port number %llu. Exiting.\n"),
610 return GNUNET_SYSERR;
613 if ( GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string (cfg,
617 hostname = GNUNET_RESOLVER_local_fqdn_get ();
619 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
620 _("Hostlist service starts on %s:%llu\n"),
622 if (NULL != hostname)
624 size = strlen (hostname);
625 if (size + 15 > MAX_URL_LEN)
631 GNUNET_asprintf (&hostlist_uri,
634 (unsigned int) port);
635 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
636 _("Address to obtain hostlist: `%s'\n"),
639 GNUNET_free ( hostname );
641 daemon_handle_v6 = MHD_start_daemon (MHD_USE_IPv6
642 #if DEBUG_HOSTLIST_SERVER
646 (unsigned short) port,
647 &accept_policy_callback,
649 &access_handler_callback,
651 MHD_OPTION_CONNECTION_LIMIT, (unsigned int) 16,
652 MHD_OPTION_PER_IP_CONNECTION_LIMIT, (unsigned int) 1,
653 MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) 16,
654 MHD_OPTION_CONNECTION_MEMORY_LIMIT, (size_t) (16 * 1024),
656 daemon_handle_v4 = MHD_start_daemon (MHD_NO_FLAG
657 #if DEBUG_HOSTLIST_SERVER
661 (unsigned short) port,
662 &accept_policy_callback,
664 &access_handler_callback,
666 MHD_OPTION_CONNECTION_LIMIT, (unsigned int) 16,
667 MHD_OPTION_PER_IP_CONNECTION_LIMIT, (unsigned int) 1,
668 MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) 16,
669 MHD_OPTION_CONNECTION_MEMORY_LIMIT, (size_t) (16 * 1024),
672 if ( (daemon_handle_v6 == NULL) &&
673 (daemon_handle_v4 == NULL) )
675 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
676 _("Could not start hostlist HTTP server on port %u\n"),
677 (unsigned short) port);
678 return GNUNET_SYSERR;
683 *server_ch = &connect_handler;
684 *server_dh = &disconnect_handler;
686 if (daemon_handle_v4 != NULL)
687 hostlist_task_v4 = prepare_daemon (daemon_handle_v4);
688 if (daemon_handle_v6 != NULL)
689 hostlist_task_v6 = prepare_daemon (daemon_handle_v6);
691 notify = GNUNET_PEERINFO_notify ( cfg, process_notify, NULL);
697 * Stop server offering our hostlist.
700 GNUNET_HOSTLIST_server_stop ()
702 #if DEBUG_HOSTLIST_SERVER
703 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
704 "Hostlist server shutdown\n");
708 GNUNET_PEERINFO_notify_cancel (notify);
711 if (GNUNET_SCHEDULER_NO_TASK != hostlist_task_v6)
713 GNUNET_SCHEDULER_cancel (hostlist_task_v6);
714 hostlist_task_v6 = GNUNET_SCHEDULER_NO_TASK;
716 if (GNUNET_SCHEDULER_NO_TASK != hostlist_task_v4)
718 GNUNET_SCHEDULER_cancel (hostlist_task_v4);
719 hostlist_task_v4 = GNUNET_SCHEDULER_NO_TASK;
723 GNUNET_PEERINFO_iterate_cancel (pitr);
726 if (NULL != daemon_handle_v4)
728 MHD_stop_daemon (daemon_handle_v4);
729 daemon_handle_v4 = NULL;
731 if (NULL != daemon_handle_v6)
733 MHD_stop_daemon (daemon_handle_v6);
734 daemon_handle_v6 = NULL;
736 if (response != NULL)
738 MHD_destroy_response (response);
741 if (peerinfo != NULL)
743 GNUNET_PEERINFO_disconnect (peerinfo);
751 /* end of hostlist-server.c */