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_assert (NULL == peer);
193 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
194 _("Error in communication with PEERINFO service: %s\n"),
201 finish_response (results);
206 has_addr = GNUNET_NO;
207 GNUNET_HELLO_iterate_addresses (hello,
211 if (GNUNET_NO == has_addr)
213 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
214 "HELLO for peer `%4s' has no address, not suitable for hostlist!\n",
216 GNUNET_STATISTICS_update (stats,
217 gettext_noop("HELLOs without addresses encountered (ignored)"),
223 s = GNUNET_HELLO_size(hello);
224 #if DEBUG_HOSTLIST_SERVER
225 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
226 "Received %u bytes of `%s' from peer `%s' for hostlist.\n",
231 if ( (old + s >= GNUNET_MAX_MALLOC_CHECKED) || (old + s >= MAX_BYTES_PER_HOSTLISTS) )
233 GNUNET_STATISTICS_update (stats,
234 gettext_noop("bytes not included in hostlist (size limit)"),
237 return; /* too large, skip! */
239 #if DEBUG_HOSTLIST_SERVER
240 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
241 "Adding peer `%s' to hostlist (%u bytes)\n",
245 GNUNET_array_grow (results->data,
248 memcpy (&results->data[old], hello, s);
254 * Hostlist access policy (very permissive, allows everything).
257 accept_policy_callback (void *cls,
258 const struct sockaddr *addr, socklen_t addrlen)
260 if (NULL == response)
262 #if DEBUG_HOSTLIST_SERVER
263 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
264 "Received request for hostlist, but I am not yet ready; rejecting!\n");
268 return MHD_YES; /* accept all */
273 * Main request handler.
276 access_handler_callback (void *cls,
277 struct MHD_Connection *connection,
281 const char *upload_data,
282 size_t*upload_data_size, void **con_cls)
286 if (0 != strcmp (method, MHD_HTTP_METHOD_GET))
288 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
289 _("Refusing `%s' request to hostlist server\n"),
291 GNUNET_STATISTICS_update (stats,
292 gettext_noop("hostlist requests refused (not HTTP GET)"),
297 if (NULL == *con_cls)
300 #if DEBUG_HOSTLIST_SERVER
301 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
302 _("Sending 100 CONTINUE reply\n"));
304 return MHD_YES; /* send 100 continue */
306 if (*upload_data_size != 0)
308 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
309 _("Refusing `%s' request with %llu bytes of upload data\n"),
311 (unsigned long long) *upload_data_size);
312 GNUNET_STATISTICS_update (stats,
313 gettext_noop("hostlist requests refused (upload data)"),
316 return MHD_NO; /* do not support upload data */
318 if (response == NULL)
320 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
321 _("Could not handle hostlist request since I do not have a response yet\n"));
322 GNUNET_STATISTICS_update (stats,
323 gettext_noop("hostlist requests refused (not ready)"),
326 return MHD_NO; /* internal error, no response yet */
328 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
329 _("Received request for our hostlist\n"));
330 GNUNET_STATISTICS_update (stats,
331 gettext_noop("hostlist requests processed"),
334 return MHD_queue_response (connection, MHD_HTTP_OK, response);
339 * Handler called by core when core is ready to transmit message
341 * @param size size of buffer to copy message to
342 * @param buf buffer to copy message to
345 adv_transmit_ready ( void *cls, size_t size, void *buf)
347 static uint64_t hostlist_adv_count;
349 size_t transmission_size;
350 size_t uri_size; /* Including \0 termination! */
351 struct GNUNET_MessageHeader header;
356 GNUNET_log ( GNUNET_ERROR_TYPE_DEBUG,
357 "Transmission failed, buffer invalid!\n" );
360 uri_size = strlen ( hostlist_uri ) + 1;
361 transmission_size = sizeof (struct GNUNET_MessageHeader) + uri_size;
362 header.type = htons (GNUNET_MESSAGE_TYPE_HOSTLIST_ADVERTISEMENT);
363 header.size = htons (transmission_size);
364 GNUNET_assert (size >= transmission_size);
365 memcpy (buf, &header, sizeof (struct GNUNET_MessageHeader));
367 memcpy (&cbuf[sizeof (struct GNUNET_MessageHeader)],
368 hostlist_uri, uri_size);
369 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
370 "Sent advertisement message: Copied %u bytes into buffer!\n",
371 (unsigned int) transmission_size);
372 hostlist_adv_count++;
373 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
374 " # Sent advertisement message: %u\n",
376 GNUNET_STATISTICS_update (stats,
377 gettext_noop("# hostlist advertisements send"),
380 return transmission_size;
385 * Method called whenever a given peer connects.
388 * @param peer peer identity this notification is about
389 * @param atsi performance data
392 connect_handler (void *cls,
394 GNUNET_PeerIdentity * peer,
395 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
401 if (hostlist_uri == NULL)
403 size = strlen (hostlist_uri) + 1;
404 if (size + sizeof (struct GNUNET_MessageHeader) >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
409 size += sizeof (struct GNUNET_MessageHeader);
415 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
416 "Asked core to transmit advertisement message with a size of %u bytes to peer `%s'\n",
417 size,GNUNET_i2s(peer));
418 if (NULL == GNUNET_CORE_notify_transmit_ready (core,
424 &adv_transmit_ready, NULL))
426 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
427 _("Advertisement message could not be queued by core\n"));
433 * Method called whenever a given peer disconnects.
436 * @param peer peer identity this notification is about
439 disconnect_handler (void *cls,
441 GNUNET_PeerIdentity * peer)
447 * PEERINFO calls this function to let us know about a possible peer
448 * that we might want to connect to.
450 * @param cls closure (not used)
451 * @param peer potential peer to connect to
452 * @param hello HELLO for this peer (or NULL)
453 * @param err_msg NULL if successful, otherwise contains error message
456 process_notify (void *cls,
457 const struct GNUNET_PeerIdentity *peer,
458 const struct GNUNET_HELLO_Message *hello,
461 struct HostSet *results;
462 #if DEBUG_HOSTLIST_SERVER
463 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
464 "Peerinfo is notifying us to rebuild our hostlist\n");
468 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
469 _("Error in communication with PEERINFO service\n"));
472 results = GNUNET_malloc(sizeof(struct HostSet));
473 GNUNET_assert (peerinfo != NULL);
474 pitr = GNUNET_PEERINFO_iterate (peerinfo,
476 GNUNET_TIME_UNIT_MINUTES,
482 * Function that queries MHD's select sets and
483 * starts the task waiting for them.
485 static GNUNET_SCHEDULER_TaskIdentifier
486 prepare_daemon (struct MHD_Daemon *daemon_handle);
490 * Call MHD to process pending requests and then go back
491 * and schedule the next run.
494 run_daemon (void *cls,
495 const struct GNUNET_SCHEDULER_TaskContext *tc)
497 struct MHD_Daemon *daemon_handle = cls;
499 if (daemon_handle == daemon_handle_v4)
500 hostlist_task_v4 = GNUNET_SCHEDULER_NO_TASK;
502 hostlist_task_v6 = GNUNET_SCHEDULER_NO_TASK;
504 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
506 GNUNET_assert (MHD_YES == MHD_run (daemon_handle));
507 if (daemon_handle == daemon_handle_v4)
508 hostlist_task_v4 = prepare_daemon (daemon_handle);
510 hostlist_task_v6 = prepare_daemon (daemon_handle);
515 * Function that queries MHD's select sets and
516 * starts the task waiting for them.
518 static GNUNET_SCHEDULER_TaskIdentifier
519 prepare_daemon (struct MHD_Daemon *daemon_handle)
521 GNUNET_SCHEDULER_TaskIdentifier ret;
525 struct GNUNET_NETWORK_FDSet *wrs;
526 struct GNUNET_NETWORK_FDSet *wws;
527 struct GNUNET_NETWORK_FDSet *wes;
529 unsigned long long timeout;
531 struct GNUNET_TIME_Relative tv;
536 wrs = GNUNET_NETWORK_fdset_create ();
537 wes = GNUNET_NETWORK_fdset_create ();
538 wws = GNUNET_NETWORK_fdset_create ();
540 GNUNET_assert (MHD_YES ==
541 MHD_get_fdset (daemon_handle,
546 haveto = MHD_get_timeout (daemon_handle, &timeout);
547 if (haveto == MHD_YES)
548 tv.rel_value = (uint64_t) timeout;
550 tv = GNUNET_TIME_UNIT_FOREVER_REL;
551 GNUNET_NETWORK_fdset_copy_native (wrs, &rs, max + 1);
552 GNUNET_NETWORK_fdset_copy_native (wws, &ws, max + 1);
553 GNUNET_NETWORK_fdset_copy_native (wes, &es, max + 1);
554 ret = GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_HIGH,
555 GNUNET_SCHEDULER_NO_TASK,
561 GNUNET_NETWORK_fdset_destroy (wrs);
562 GNUNET_NETWORK_fdset_destroy (wws);
563 GNUNET_NETWORK_fdset_destroy (wes);
570 * Start server offering our hostlist.
572 * @return GNUNET_OK on success
575 GNUNET_HOSTLIST_server_start (const struct GNUNET_CONFIGURATION_Handle *c,
576 struct GNUNET_STATISTICS_Handle *st,
577 struct GNUNET_CORE_Handle *co,
578 GNUNET_CORE_ConnectEventHandler *server_ch,
579 GNUNET_CORE_DisconnectEventHandler *server_dh,
582 unsigned long long port;
586 advertising = advertise;
588 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
589 "Advertising not enabled on this hostlist server\n");
591 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
592 "Advertising enabled on this hostlist server\n");
595 peerinfo = GNUNET_PEERINFO_connect (cfg);
596 if (peerinfo == NULL)
598 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
599 _("Could not access PEERINFO service. Exiting.\n"));
600 return GNUNET_SYSERR;
602 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (cfg,
606 return GNUNET_SYSERR;
608 (port > UINT16_MAX) )
610 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
611 _("Invalid port number %llu. Exiting.\n"),
613 return GNUNET_SYSERR;
616 if ( GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string (cfg,
620 hostname = GNUNET_RESOLVER_local_fqdn_get ();
622 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
623 _("Hostlist service starts on %s:%llu\n"),
625 if (NULL != hostname)
627 size = strlen (hostname);
628 if (size + 15 > MAX_URL_LEN)
634 GNUNET_asprintf (&hostlist_uri,
637 (unsigned int) port);
638 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
639 _("Address to obtain hostlist: `%s'\n"),
642 GNUNET_free ( hostname );
644 daemon_handle_v6 = MHD_start_daemon (MHD_USE_IPv6
645 #if DEBUG_HOSTLIST_SERVER
649 (unsigned short) port,
650 &accept_policy_callback,
652 &access_handler_callback,
654 MHD_OPTION_CONNECTION_LIMIT, (unsigned int) 16,
655 MHD_OPTION_PER_IP_CONNECTION_LIMIT, (unsigned int) 1,
656 MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) 16,
657 MHD_OPTION_CONNECTION_MEMORY_LIMIT, (size_t) (16 * 1024),
659 daemon_handle_v4 = MHD_start_daemon (MHD_NO_FLAG
660 #if DEBUG_HOSTLIST_SERVER
664 (unsigned short) port,
665 &accept_policy_callback,
667 &access_handler_callback,
669 MHD_OPTION_CONNECTION_LIMIT, (unsigned int) 16,
670 MHD_OPTION_PER_IP_CONNECTION_LIMIT, (unsigned int) 1,
671 MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) 16,
672 MHD_OPTION_CONNECTION_MEMORY_LIMIT, (size_t) (16 * 1024),
675 if ( (daemon_handle_v6 == NULL) &&
676 (daemon_handle_v4 == NULL) )
678 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
679 _("Could not start hostlist HTTP server on port %u\n"),
680 (unsigned short) port);
681 return GNUNET_SYSERR;
686 *server_ch = &connect_handler;
687 *server_dh = &disconnect_handler;
689 if (daemon_handle_v4 != NULL)
690 hostlist_task_v4 = prepare_daemon (daemon_handle_v4);
691 if (daemon_handle_v6 != NULL)
692 hostlist_task_v6 = prepare_daemon (daemon_handle_v6);
694 notify = GNUNET_PEERINFO_notify ( cfg, process_notify, NULL);
700 * Stop server offering our hostlist.
703 GNUNET_HOSTLIST_server_stop ()
705 #if DEBUG_HOSTLIST_SERVER
706 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
707 "Hostlist server shutdown\n");
711 GNUNET_PEERINFO_notify_cancel (notify);
714 if (GNUNET_SCHEDULER_NO_TASK != hostlist_task_v6)
716 GNUNET_SCHEDULER_cancel (hostlist_task_v6);
717 hostlist_task_v6 = GNUNET_SCHEDULER_NO_TASK;
719 if (GNUNET_SCHEDULER_NO_TASK != hostlist_task_v4)
721 GNUNET_SCHEDULER_cancel (hostlist_task_v4);
722 hostlist_task_v4 = GNUNET_SCHEDULER_NO_TASK;
726 GNUNET_PEERINFO_iterate_cancel (pitr);
729 if (NULL != daemon_handle_v4)
731 MHD_stop_daemon (daemon_handle_v4);
732 daemon_handle_v4 = NULL;
734 if (NULL != daemon_handle_v6)
736 MHD_stop_daemon (daemon_handle_v6);
737 daemon_handle_v6 = NULL;
739 if (response != NULL)
741 MHD_destroy_response (response);
744 if (peerinfo != NULL)
746 GNUNET_PEERINFO_disconnect (peerinfo);
754 /* end of hostlist-server.c */