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 * 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;
55 static struct GNUNET_SCHEDULER_Handle *sched;
58 * For keeping statistics.
60 static struct GNUNET_STATISTICS_Handle *stats;
63 * Handle to the core service (NULL until we've connected to it).
65 static struct GNUNET_CORE_Handle *core;
68 * Handle to the peerinfo notify service (NULL until we've connected to it).
70 static struct GNUNET_PEERINFO_NotifyContext *notify;
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 * Our canonical response.
85 static struct MHD_Response *response;
88 * NULL if we are not currenlty iterating over peer information.
90 static struct GNUNET_PEERINFO_IteratorContext *pitr;
93 * Handle for accessing peerinfo service.
95 static struct GNUNET_PEERINFO_Handle *peerinfo;
98 * Context for host processor.
108 * Set if we are allowed to advertise our hostlist to others.
110 static int advertising;
113 * Buffer for the hostlist address
115 static char * hostlist_uri;
119 * Function that assembles our response.
122 finish_response (struct HostSet *results)
124 if (response != NULL)
125 MHD_destroy_response (response);
126 #if DEBUG_HOSTLIST_SERVER
127 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
128 "Creating hostlist response with %u bytes\n",
129 (unsigned int) results->size);
131 response = MHD_create_response_from_data (results->size,
132 results->data, MHD_YES, MHD_NO);
133 if ( (daemon_handle_v4 == NULL) &&
134 (daemon_handle_v6 == NULL) )
136 MHD_destroy_response (response);
139 GNUNET_STATISTICS_set (stats,
140 gettext_noop("bytes in hostlist"),
143 GNUNET_free (results);
148 * Set 'cls' to GNUNET_YES (we have an address!).
150 * @param cls closure, an 'int*'
151 * @param tname name of the transport (ignored)
152 * @param expiration expiration time (call is ignored if this is in the past)
153 * @param addr the address (ignored)
154 * @param addrlen length of the address (ignored)
155 * @return GNUNET_SYSERR to stop iterating (unless expiration has occured)
158 check_has_addr (void *cls,
160 struct GNUNET_TIME_Absolute expiration,
166 if (GNUNET_TIME_absolute_get_remaining (expiration).value == 0)
168 GNUNET_STATISTICS_update (stats,
169 gettext_noop("expired addresses encountered"),
172 return GNUNET_YES; /* ignore this address */
175 return GNUNET_SYSERR;
180 * Callback that processes each of the known HELLOs for the
181 * hostlist response construction.
184 host_processor (void *cls,
185 const struct GNUNET_PeerIdentity * peer,
186 const struct GNUNET_HELLO_Message *hello,
189 struct HostSet *results = cls;
197 finish_response (results);
202 has_addr = GNUNET_NO;
203 GNUNET_HELLO_iterate_addresses (hello,
207 if (GNUNET_NO == has_addr)
209 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
210 "HELLO for peer `%4s' has no address, not suitable for hostlist!\n",
212 GNUNET_STATISTICS_update (stats,
213 gettext_noop("HELLOs without addresses encountered (ignored)"),
219 s = GNUNET_HELLO_size(hello);
220 #if DEBUG_HOSTLIST_SERVER
221 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
222 "Received %u bytes of `%s' from peer `%s' for hostlist.\n",
227 if ( (old + s >= GNUNET_MAX_MALLOC_CHECKED) || (old + s >= MAX_BYTES_PER_HOSTLISTS) )
229 GNUNET_STATISTICS_update (stats,
230 gettext_noop("bytes not included in hostlist (size limit)"),
233 return; /* too large, skip! */
235 #if DEBUG_HOSTLIST_SERVER
236 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
237 "Adding peer `%s' to hostlist (%u bytes)\n",
241 GNUNET_array_grow (results->data,
244 memcpy (&results->data[old], hello, s);
250 * Hostlist access policy (very permissive, allows everything).
253 accept_policy_callback (void *cls,
254 const struct sockaddr *addr, socklen_t addrlen)
256 if (NULL == response)
258 #if DEBUG_HOSTLIST_SERVER
259 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
260 "Received request for hostlist, but I am not yet ready; rejecting!\n");
264 return MHD_YES; /* accept all */
269 * Main request handler.
272 access_handler_callback (void *cls,
273 struct MHD_Connection *connection,
277 const char *upload_data,
278 size_t*upload_data_size, void **con_cls)
282 if (0 != strcmp (method, MHD_HTTP_METHOD_GET))
284 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
285 _("Refusing `%s' request to hostlist server\n"),
287 GNUNET_STATISTICS_update (stats,
288 gettext_noop("hostlist requests refused (not HTTP GET)"),
293 if (NULL == *con_cls)
296 #if DEBUG_HOSTLIST_SERVER
297 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
298 _("Sending 100 CONTINUE reply\n"));
300 return MHD_YES; /* send 100 continue */
302 if (*upload_data_size != 0)
304 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
305 _("Refusing `%s' request with %llu bytes of upload data\n"),
307 (unsigned long long) *upload_data_size);
308 GNUNET_STATISTICS_update (stats,
309 gettext_noop("hostlist requests refused (upload data)"),
312 return MHD_NO; /* do not support upload data */
314 if (response == NULL)
316 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
317 _("Could not handle hostlist request since I do not have a response yet\n"));
318 GNUNET_STATISTICS_update (stats,
319 gettext_noop("hostlist requests refused (not ready)"),
322 return MHD_NO; /* internal error, no response yet */
324 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
325 _("Received request for our hostlist\n"));
326 GNUNET_STATISTICS_update (stats,
327 gettext_noop("hostlist requests processed"),
330 return MHD_queue_response (connection, MHD_HTTP_OK, response);
335 * Handler called by core when core is ready to transmit message
337 * @param size size of buffer to copy message to
338 * @param buf buffer to copy message to
341 adv_transmit_ready ( void *cls, size_t size, void *buf)
343 static uint64_t hostlist_adv_count;
345 size_t transmission_size;
346 size_t uri_size; /* Including \0 termination! */
347 struct GNUNET_MessageHeader header;
352 GNUNET_log ( GNUNET_ERROR_TYPE_DEBUG,
353 "Transmission failed, buffer invalid!\n" );
356 uri_size = strlen ( hostlist_uri ) + 1;
357 transmission_size = sizeof (struct GNUNET_MessageHeader) + uri_size;
358 header.type = htons (GNUNET_MESSAGE_TYPE_HOSTLIST_ADVERTISEMENT);
359 header.size = htons (transmission_size);
360 GNUNET_assert (size >= transmission_size);
361 memcpy (buf, &header, sizeof (struct GNUNET_MessageHeader));
363 memcpy (&cbuf[sizeof (struct GNUNET_MessageHeader)],
364 hostlist_uri, uri_size);
365 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
366 "Sent advertisement message: Copied %u bytes into buffer!\n",
367 (unsigned int) transmission_size);
368 hostlist_adv_count++;
369 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
370 " # Sent advertisement message: %u\n",
372 GNUNET_STATISTICS_update (stats,
373 gettext_noop("# hostlist advertisements send"),
376 return transmission_size;
381 * Method called whenever a given peer connects.
384 * @param peer peer identity this notification is about
385 * @param latency reported latency of the connection with 'other'
386 * @param distance reported distance (DV) to 'other'
389 connect_handler (void *cls,
391 GNUNET_PeerIdentity * peer,
392 struct GNUNET_TIME_Relative latency,
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 trust how much we trust the peer (not used)
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");
463 results = GNUNET_malloc(sizeof(struct HostSet));
464 GNUNET_assert (peerinfo != NULL);
465 pitr = GNUNET_PEERINFO_iterate (peerinfo,
468 GNUNET_TIME_UNIT_MINUTES,
474 * Function that queries MHD's select sets and
475 * starts the task waiting for them.
477 static GNUNET_SCHEDULER_TaskIdentifier
478 prepare_daemon (struct MHD_Daemon *daemon_handle);
482 * Call MHD to process pending requests and then go back
483 * and schedule the next run.
486 run_daemon (void *cls,
487 const struct GNUNET_SCHEDULER_TaskContext *tc)
489 struct MHD_Daemon *daemon_handle = cls;
491 if (daemon_handle == daemon_handle_v4)
492 hostlist_task_v4 = GNUNET_SCHEDULER_NO_TASK;
494 hostlist_task_v6 = GNUNET_SCHEDULER_NO_TASK;
496 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
498 GNUNET_assert (MHD_YES == MHD_run (daemon_handle));
499 if (daemon_handle == daemon_handle_v4)
500 hostlist_task_v4 = prepare_daemon (daemon_handle);
502 hostlist_task_v6 = prepare_daemon (daemon_handle);
507 * Function that queries MHD's select sets and
508 * starts the task waiting for them.
510 static GNUNET_SCHEDULER_TaskIdentifier
511 prepare_daemon (struct MHD_Daemon *daemon_handle)
513 GNUNET_SCHEDULER_TaskIdentifier ret;
517 struct GNUNET_NETWORK_FDSet *wrs;
518 struct GNUNET_NETWORK_FDSet *wws;
519 struct GNUNET_NETWORK_FDSet *wes;
521 unsigned long long timeout;
523 struct GNUNET_TIME_Relative tv;
528 wrs = GNUNET_NETWORK_fdset_create ();
529 wes = GNUNET_NETWORK_fdset_create ();
530 wws = GNUNET_NETWORK_fdset_create ();
532 GNUNET_assert (MHD_YES ==
533 MHD_get_fdset (daemon_handle,
538 haveto = MHD_get_timeout (daemon_handle, &timeout);
539 if (haveto == MHD_YES)
540 tv.value = (uint64_t) timeout;
542 tv = GNUNET_TIME_UNIT_FOREVER_REL;
543 GNUNET_NETWORK_fdset_copy_native (wrs, &rs, max);
544 GNUNET_NETWORK_fdset_copy_native (wws, &ws, max);
545 GNUNET_NETWORK_fdset_copy_native (wes, &es, max);
546 ret = GNUNET_SCHEDULER_add_select (sched,
547 GNUNET_SCHEDULER_PRIORITY_HIGH,
548 GNUNET_SCHEDULER_NO_TASK,
554 GNUNET_NETWORK_fdset_destroy (wrs);
555 GNUNET_NETWORK_fdset_destroy (wws);
556 GNUNET_NETWORK_fdset_destroy (wes);
563 * Start server offering our hostlist.
565 * @return GNUNET_OK on success
568 GNUNET_HOSTLIST_server_start (const struct GNUNET_CONFIGURATION_Handle *c,
569 struct GNUNET_SCHEDULER_Handle *s,
570 struct GNUNET_STATISTICS_Handle *st,
571 struct GNUNET_CORE_Handle *co,
572 GNUNET_CORE_ConnectEventHandler *server_ch,
573 GNUNET_CORE_DisconnectEventHandler *server_dh,
576 unsigned long long port;
580 advertising = advertise;
582 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
583 "Advertising not enabled on this hostlist server\n");
585 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
586 "Advertising enabled on this hostlist server\n");
590 peerinfo = GNUNET_PEERINFO_connect (sched, cfg);
591 if (peerinfo == NULL)
593 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
594 _("Could not access PEERINFO service. Exiting.\n"));
595 return GNUNET_SYSERR;
597 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (cfg,
601 return GNUNET_SYSERR;
603 (port > UINT16_MAX) )
605 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
606 _("Invalid port number %llu. Exiting.\n"),
608 return GNUNET_SYSERR;
611 if ( GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string (cfg,
615 hostname = GNUNET_RESOLVER_local_fqdn_get ();
617 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
618 _("Hostlist service starts on %s:%llu\n"),
620 if (NULL != hostname)
622 size = strlen (hostname);
623 if (size + 15 > MAX_URL_LEN)
629 GNUNET_asprintf (&hostlist_uri,
632 (unsigned int) port);
633 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
634 _("Address to obtain hostlist: `%s'\n"),
637 GNUNET_free ( hostname );
639 daemon_handle_v6 = MHD_start_daemon (MHD_USE_IPv6
640 #if DEBUG_HOSTLIST_SERVER
644 (unsigned short) port,
645 &accept_policy_callback,
647 &access_handler_callback,
649 MHD_OPTION_CONNECTION_LIMIT, (unsigned int) 16,
650 MHD_OPTION_PER_IP_CONNECTION_LIMIT, (unsigned int) 1,
651 MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) 16,
652 MHD_OPTION_CONNECTION_MEMORY_LIMIT, (size_t) (16 * 1024),
654 daemon_handle_v4 = MHD_start_daemon (MHD_NO_FLAG
655 #if DEBUG_HOSTLIST_SERVER
659 (unsigned short) port,
660 &accept_policy_callback,
662 &access_handler_callback,
664 MHD_OPTION_CONNECTION_LIMIT, (unsigned int) 16,
665 MHD_OPTION_PER_IP_CONNECTION_LIMIT, (unsigned int) 1,
666 MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) 16,
667 MHD_OPTION_CONNECTION_MEMORY_LIMIT, (size_t) (16 * 1024),
670 if ( (daemon_handle_v6 == NULL) &&
671 (daemon_handle_v4 == NULL) )
673 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
674 _("Could not start hostlist HTTP server on port %u\n"),
675 (unsigned short) port);
676 return GNUNET_SYSERR;
681 *server_ch = &connect_handler;
682 *server_dh = &disconnect_handler;
684 if (daemon_handle_v4 != NULL)
685 hostlist_task_v4 = prepare_daemon (daemon_handle_v4);
686 if (daemon_handle_v6 != NULL)
687 hostlist_task_v6 = prepare_daemon (daemon_handle_v6);
689 notify = GNUNET_PEERINFO_notify ( cfg, sched, process_notify, NULL);
695 * Stop server offering our hostlist.
698 GNUNET_HOSTLIST_server_stop ()
700 #if DEBUG_HOSTLIST_SERVER
701 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
702 "Hostlist server shutdown\n");
706 GNUNET_PEERINFO_notify_cancel (notify);
709 if (GNUNET_SCHEDULER_NO_TASK != hostlist_task_v6)
711 GNUNET_SCHEDULER_cancel (sched, hostlist_task_v6);
712 hostlist_task_v6 = GNUNET_SCHEDULER_NO_TASK;
714 if (GNUNET_SCHEDULER_NO_TASK != hostlist_task_v4)
716 GNUNET_SCHEDULER_cancel (sched, hostlist_task_v4);
717 hostlist_task_v4 = GNUNET_SCHEDULER_NO_TASK;
721 GNUNET_PEERINFO_iterate_cancel (pitr);
724 if (NULL != daemon_handle_v4)
726 MHD_stop_daemon (daemon_handle_v4);
727 daemon_handle_v4 = NULL;
729 if (NULL != daemon_handle_v6)
731 MHD_stop_daemon (daemon_handle_v6);
732 daemon_handle_v6 = NULL;
734 if (response != NULL)
736 MHD_destroy_response (response);
739 if (peerinfo != NULL)
741 GNUNET_PEERINFO_disconnect (peerinfo);
750 /* end of hostlist-server.c */