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 struct GNUNET_CORE_Handle *core;
68 * Handle to the peerinfo notify service (NULL until we've connected to it).
70 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 * 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 * Function that assembles our response.
127 finish_response (struct HostSet *results)
129 if (response != NULL)
130 MHD_destroy_response (response);
131 #if DEBUG_HOSTLIST_SERVER
132 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
133 "Creating hostlist response with %u bytes\n",
134 (unsigned int) results->size);
136 response = MHD_create_response_from_data (results->size,
137 results->data, MHD_YES, MHD_NO);
138 if ( (daemon_handle_v4 == NULL) &&
139 (daemon_handle_v6 == NULL) )
141 MHD_destroy_response (response);
144 GNUNET_STATISTICS_set (stats,
145 gettext_noop("bytes in hostlist"),
148 GNUNET_free (results);
153 * Set 'cls' to GNUNET_YES (we have an address!).
155 * @param cls closure, an 'int*'
156 * @param tname name of the transport (ignored)
157 * @param expiration expiration time (call is ignored if this is in the past)
158 * @param addr the address (ignored)
159 * @param addrlen length of the address (ignored)
160 * @return GNUNET_SYSERR to stop iterating (unless expiration has occured)
163 check_has_addr (void *cls,
165 struct GNUNET_TIME_Absolute expiration,
166 const void *addr, size_t addrlen)
170 if (GNUNET_TIME_absolute_get_remaining (expiration).value == 0)
172 GNUNET_STATISTICS_update (stats,
173 gettext_noop("expired addresses encountered"),
176 return GNUNET_YES; /* ignore this address */
179 return GNUNET_SYSERR;
184 * Callback that processes each of the known HELLOs for the
185 * hostlist response construction.
188 host_processor (void *cls,
189 const struct GNUNET_PeerIdentity * peer,
190 const struct GNUNET_HELLO_Message *hello,
193 struct HostSet *results = cls;
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)
233 GNUNET_STATISTICS_update (stats,
234 gettext_noop("bytes not included in hostlist (size limit)"),
237 return; /* too large, skip! */
239 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
240 "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 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_STATISTICS_set (stats,
370 gettext_noop("# hostlist advertisements send"),
373 return transmission_size;
378 * Method called whenever a given peer connects.
381 * @param peer peer identity this notification is about
382 * @param latency reported latency of the connection with 'other'
383 * @param distance reported distance (DV) to 'other'
386 connect_handler (void *cls,
388 GNUNET_PeerIdentity * peer,
389 struct GNUNET_TIME_Relative latency,
396 if (hostlist_uri == NULL)
398 size = strlen (hostlist_uri) + 1;
399 if (size + sizeof (struct GNUNET_MessageHeader) > GNUNET_SERVER_MAX_MESSAGE_SIZE)
404 size += sizeof (struct GNUNET_MessageHeader);
410 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
411 "Asked core to transmit advertisement message with a size of %u bytes to peer `%s'\n",
412 size,GNUNET_i2s(peer));
413 if (NULL == GNUNET_CORE_notify_transmit_ready (core,
418 &adv_transmit_ready, NULL))
420 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
421 _("Advertisement message could not be queued by core\n"));
427 * Method called whenever a given peer disconnects.
430 * @param peer peer identity this notification is about
433 disconnect_handler (void *cls,
435 GNUNET_PeerIdentity * peer)
441 * PEERINFO calls this function to let us know about a possible peer
442 * that we might want to connect to.
444 * @param cls closure (not used)
445 * @param peer potential peer to connect to
446 * @param hello HELLO for this peer (or NULL)
447 * @param trust how much we trust the peer (not used)
450 process_notify (void *cls,
451 const struct GNUNET_PeerIdentity *peer,
452 const struct GNUNET_HELLO_Message *hello,
455 struct HostSet *results;
457 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
458 "Peerinfo is notifying us to rebuild our hostlist\n");
459 results = GNUNET_malloc(sizeof(struct HostSet));
460 GNUNET_assert (peerinfo != NULL);
461 pitr = GNUNET_PEERINFO_iterate (peerinfo,
464 GNUNET_TIME_UNIT_MINUTES,
470 * Function that queries MHD's select sets and
471 * starts the task waiting for them.
473 static GNUNET_SCHEDULER_TaskIdentifier
474 prepare_daemon (struct MHD_Daemon *daemon_handle);
478 * Call MHD to process pending requests and then go back
479 * and schedule the next run.
482 run_daemon (void *cls,
483 const struct GNUNET_SCHEDULER_TaskContext *tc)
485 struct MHD_Daemon *daemon_handle = cls;
487 if (daemon_handle == daemon_handle_v4)
488 hostlist_task_v4 = GNUNET_SCHEDULER_NO_TASK;
490 hostlist_task_v6 = GNUNET_SCHEDULER_NO_TASK;
492 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
494 GNUNET_assert (MHD_YES == MHD_run (daemon_handle));
495 if (daemon_handle == daemon_handle_v4)
496 hostlist_task_v4 = prepare_daemon (daemon_handle);
498 hostlist_task_v6 = prepare_daemon (daemon_handle);
503 * Function that queries MHD's select sets and
504 * starts the task waiting for them.
506 static GNUNET_SCHEDULER_TaskIdentifier
507 prepare_daemon (struct MHD_Daemon *daemon_handle)
509 GNUNET_SCHEDULER_TaskIdentifier ret;
513 struct GNUNET_NETWORK_FDSet *wrs;
514 struct GNUNET_NETWORK_FDSet *wws;
515 struct GNUNET_NETWORK_FDSet *wes;
517 unsigned long long timeout;
519 struct GNUNET_TIME_Relative tv;
524 wrs = GNUNET_NETWORK_fdset_create ();
525 wes = GNUNET_NETWORK_fdset_create ();
526 wws = GNUNET_NETWORK_fdset_create ();
528 GNUNET_assert (MHD_YES ==
529 MHD_get_fdset (daemon_handle,
534 haveto = MHD_get_timeout (daemon_handle, &timeout);
535 if (haveto == MHD_YES)
536 tv.value = (uint64_t) timeout;
538 tv = GNUNET_TIME_UNIT_FOREVER_REL;
539 GNUNET_NETWORK_fdset_copy_native (wrs, &rs, max);
540 GNUNET_NETWORK_fdset_copy_native (wws, &ws, max);
541 GNUNET_NETWORK_fdset_copy_native (wes, &es, max);
542 ret = GNUNET_SCHEDULER_add_select (sched,
543 GNUNET_SCHEDULER_PRIORITY_HIGH,
544 GNUNET_SCHEDULER_NO_TASK,
550 GNUNET_NETWORK_fdset_destroy (wrs);
551 GNUNET_NETWORK_fdset_destroy (wws);
552 GNUNET_NETWORK_fdset_destroy (wes);
559 * Start server offering our hostlist.
561 * @return GNUNET_OK on success
564 GNUNET_HOSTLIST_server_start (const struct GNUNET_CONFIGURATION_Handle *c,
565 struct GNUNET_SCHEDULER_Handle *s,
566 struct GNUNET_STATISTICS_Handle *st,
567 struct GNUNET_CORE_Handle *co,
568 GNUNET_CORE_ConnectEventHandler *server_ch,
569 GNUNET_CORE_DisconnectEventHandler *server_dh,
572 unsigned long long port;
576 advertising = advertise;
578 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
579 "Advertising not enabled on this hostlist server\n");
581 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
582 "Advertising enabled on this hostlist server\n");
586 peerinfo = GNUNET_PEERINFO_connect (sched, cfg);
587 if (peerinfo == NULL)
589 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
590 _("Could not access PEERINFO service. Exiting.\n"));
591 return GNUNET_SYSERR;
593 if (-1 == GNUNET_CONFIGURATION_get_value_number (cfg,
597 return GNUNET_SYSERR;
598 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
599 _("Hostlist service starts on port %llu\n"),
601 hostname = GNUNET_RESOLVER_local_fqdn_get ();
602 if (NULL != hostname)
604 size = strlen (hostname);
605 if (size + 15 > MAX_URL_LEN)
611 GNUNET_asprintf (&hostlist_uri,
614 (unsigned int) port);
615 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
616 _("Address to obtain hostlist: `%s'\n"),
619 GNUNET_free ( hostname );
621 daemon_handle_v6 = MHD_start_daemon (MHD_USE_IPv6
622 #if DEBUG_HOSTLIST_SERVER
626 (unsigned short) port,
627 &accept_policy_callback,
629 &access_handler_callback,
631 MHD_OPTION_CONNECTION_LIMIT, (unsigned int) 16,
632 MHD_OPTION_PER_IP_CONNECTION_LIMIT, (unsigned int) 1,
633 MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) 16,
634 MHD_OPTION_CONNECTION_MEMORY_LIMIT, (size_t) (16 * 1024),
636 daemon_handle_v4 = MHD_start_daemon (MHD_NO_FLAG
637 #if DEBUG_HOSTLIST_SERVER
641 (unsigned short) port,
642 &accept_policy_callback,
644 &access_handler_callback,
646 MHD_OPTION_CONNECTION_LIMIT, (unsigned int) 16,
647 MHD_OPTION_PER_IP_CONNECTION_LIMIT, (unsigned int) 1,
648 MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) 16,
649 MHD_OPTION_CONNECTION_MEMORY_LIMIT, (size_t) (16 * 1024),
652 if ( (daemon_handle_v6 == NULL) &&
653 (daemon_handle_v4 == NULL) )
655 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
656 _("Could not start hostlist HTTP server on port %u\n"),
657 (unsigned short) port);
658 return GNUNET_SYSERR;
663 *server_ch = &connect_handler;
664 *server_dh = &disconnect_handler;
666 if (daemon_handle_v4 != NULL)
667 hostlist_task_v4 = prepare_daemon (daemon_handle_v4);
668 if (daemon_handle_v6 != NULL)
669 hostlist_task_v6 = prepare_daemon (daemon_handle_v6);
671 notify = GNUNET_PEERINFO_notify ( cfg, sched, process_notify, NULL);
677 * Stop server offering our hostlist.
680 GNUNET_HOSTLIST_server_stop ()
682 #if DEBUG_HOSTLIST_SERVER
683 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
684 "Hostlist server shutdown\n");
688 GNUNET_PEERINFO_notify_cancel (notify);
691 if (GNUNET_SCHEDULER_NO_TASK != hostlist_task_v6)
693 GNUNET_SCHEDULER_cancel (sched, hostlist_task_v6);
694 hostlist_task_v6 = GNUNET_SCHEDULER_NO_TASK;
696 if (GNUNET_SCHEDULER_NO_TASK != hostlist_task_v4)
698 GNUNET_SCHEDULER_cancel (sched, hostlist_task_v4);
699 hostlist_task_v4 = GNUNET_SCHEDULER_NO_TASK;
703 GNUNET_PEERINFO_iterate_cancel (pitr);
706 if (NULL != daemon_handle_v4)
708 MHD_stop_daemon (daemon_handle_v4);
709 daemon_handle_v4 = NULL;
711 if (NULL != daemon_handle_v6)
713 MHD_stop_daemon (daemon_handle_v6);
714 daemon_handle_v6 = NULL;
716 if (response != NULL)
718 MHD_destroy_response (response);
721 if (peerinfo != NULL)
723 GNUNET_PEERINFO_disconnect (peerinfo);
728 /* end of hostlist-server.c */