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;
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)
188 struct HostSet *results = cls;
196 finish_response (results);
201 has_addr = GNUNET_NO;
202 GNUNET_HELLO_iterate_addresses (hello,
206 if (GNUNET_NO == has_addr)
208 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
209 "HELLO for peer `%4s' has no address, not suitable for hostlist!\n",
211 GNUNET_STATISTICS_update (stats,
212 gettext_noop("HELLOs without addresses encountered (ignored)"),
218 s = GNUNET_HELLO_size(hello);
219 #if DEBUG_HOSTLIST_SERVER
220 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
221 "Received %u bytes of `%s' from peer `%s' for hostlist.\n",
226 if ( (old + s >= GNUNET_MAX_MALLOC_CHECKED) || (old + s >= MAX_BYTES_PER_HOSTLISTS) )
228 GNUNET_STATISTICS_update (stats,
229 gettext_noop("bytes not included in hostlist (size limit)"),
232 return; /* too large, skip! */
234 #if DEBUG_HOSTLIST_SERVER
235 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
236 "Adding peer `%s' to hostlist (%u bytes)\n",
240 GNUNET_array_grow (results->data,
243 memcpy (&results->data[old], hello, s);
249 * Hostlist access policy (very permissive, allows everything).
252 accept_policy_callback (void *cls,
253 const struct sockaddr *addr, socklen_t addrlen)
255 if (NULL == response)
257 #if DEBUG_HOSTLIST_SERVER
258 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
259 "Received request for hostlist, but I am not yet ready; rejecting!\n");
263 return MHD_YES; /* accept all */
268 * Main request handler.
271 access_handler_callback (void *cls,
272 struct MHD_Connection *connection,
276 const char *upload_data,
277 size_t*upload_data_size, void **con_cls)
281 if (0 != strcmp (method, MHD_HTTP_METHOD_GET))
283 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
284 _("Refusing `%s' request to hostlist server\n"),
286 GNUNET_STATISTICS_update (stats,
287 gettext_noop("hostlist requests refused (not HTTP GET)"),
292 if (NULL == *con_cls)
295 #if DEBUG_HOSTLIST_SERVER
296 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
297 _("Sending 100 CONTINUE reply\n"));
299 return MHD_YES; /* send 100 continue */
301 if (*upload_data_size != 0)
303 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
304 _("Refusing `%s' request with %llu bytes of upload data\n"),
306 (unsigned long long) *upload_data_size);
307 GNUNET_STATISTICS_update (stats,
308 gettext_noop("hostlist requests refused (upload data)"),
311 return MHD_NO; /* do not support upload data */
313 if (response == NULL)
315 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
316 _("Could not handle hostlist request since I do not have a response yet\n"));
317 GNUNET_STATISTICS_update (stats,
318 gettext_noop("hostlist requests refused (not ready)"),
321 return MHD_NO; /* internal error, no response yet */
323 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
324 _("Received request for our hostlist\n"));
325 GNUNET_STATISTICS_update (stats,
326 gettext_noop("hostlist requests processed"),
329 return MHD_queue_response (connection, MHD_HTTP_OK, response);
334 * Handler called by core when core is ready to transmit message
336 * @param size size of buffer to copy message to
337 * @param buf buffer to copy message to
340 adv_transmit_ready ( void *cls, size_t size, void *buf)
342 static uint64_t hostlist_adv_count;
344 size_t transmission_size;
345 size_t uri_size; /* Including \0 termination! */
346 struct GNUNET_MessageHeader header;
351 GNUNET_log ( GNUNET_ERROR_TYPE_DEBUG,
352 "Transmission failed, buffer invalid!\n" );
355 uri_size = strlen ( hostlist_uri ) + 1;
356 transmission_size = sizeof (struct GNUNET_MessageHeader) + uri_size;
357 header.type = htons (GNUNET_MESSAGE_TYPE_HOSTLIST_ADVERTISEMENT);
358 header.size = htons (transmission_size);
359 GNUNET_assert (size >= transmission_size);
360 memcpy (buf, &header, sizeof (struct GNUNET_MessageHeader));
362 memcpy (&cbuf[sizeof (struct GNUNET_MessageHeader)],
363 hostlist_uri, uri_size);
364 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
365 "Sent advertisement message: Copied %u bytes into buffer!\n",
366 (unsigned int) transmission_size);
367 hostlist_adv_count++;
368 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
369 " # Sent advertisement message: %u\n",
371 GNUNET_STATISTICS_update (stats,
372 gettext_noop("# hostlist advertisements send"),
375 return transmission_size;
380 * Method called whenever a given peer connects.
383 * @param peer peer identity this notification is about
384 * @param latency reported latency of the connection with 'other'
385 * @param distance reported distance (DV) to 'other'
388 connect_handler (void *cls,
390 GNUNET_PeerIdentity * peer,
391 struct GNUNET_TIME_Relative latency,
398 if (hostlist_uri == NULL)
400 size = strlen (hostlist_uri) + 1;
401 if (size + sizeof (struct GNUNET_MessageHeader) >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
406 size += sizeof (struct GNUNET_MessageHeader);
412 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
413 "Asked core to transmit advertisement message with a size of %u bytes to peer `%s'\n",
414 size,GNUNET_i2s(peer));
415 if (NULL == GNUNET_CORE_notify_transmit_ready (core,
420 &adv_transmit_ready, NULL))
422 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
423 _("Advertisement message could not be queued by core\n"));
429 * Method called whenever a given peer disconnects.
432 * @param peer peer identity this notification is about
435 disconnect_handler (void *cls,
437 GNUNET_PeerIdentity * peer)
443 * PEERINFO calls this function to let us know about a possible peer
444 * that we might want to connect to.
446 * @param cls closure (not used)
447 * @param peer potential peer to connect to
448 * @param hello HELLO for this peer (or NULL)
451 process_notify (void *cls,
452 const struct GNUNET_PeerIdentity *peer,
453 const struct GNUNET_HELLO_Message *hello)
455 struct HostSet *results;
456 #if DEBUG_HOSTLIST_SERVER
457 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
458 "Peerinfo is notifying us to rebuild our hostlist\n");
460 results = GNUNET_malloc(sizeof(struct HostSet));
461 GNUNET_assert (peerinfo != NULL);
462 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 (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (cfg,
597 return GNUNET_SYSERR;
599 (port > UINT16_MAX) )
601 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
602 _("Invalid port number %llu. Exiting.\n"),
604 return GNUNET_SYSERR;
607 if ( GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string (cfg,
611 hostname = GNUNET_RESOLVER_local_fqdn_get ();
613 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
614 _("Hostlist service starts on %s:%llu\n"),
616 if (NULL != hostname)
618 size = strlen (hostname);
619 if (size + 15 > MAX_URL_LEN)
625 GNUNET_asprintf (&hostlist_uri,
628 (unsigned int) port);
629 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
630 _("Address to obtain hostlist: `%s'\n"),
633 GNUNET_free ( hostname );
635 daemon_handle_v6 = MHD_start_daemon (MHD_USE_IPv6
636 #if DEBUG_HOSTLIST_SERVER
640 (unsigned short) port,
641 &accept_policy_callback,
643 &access_handler_callback,
645 MHD_OPTION_CONNECTION_LIMIT, (unsigned int) 16,
646 MHD_OPTION_PER_IP_CONNECTION_LIMIT, (unsigned int) 1,
647 MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) 16,
648 MHD_OPTION_CONNECTION_MEMORY_LIMIT, (size_t) (16 * 1024),
650 daemon_handle_v4 = MHD_start_daemon (MHD_NO_FLAG
651 #if DEBUG_HOSTLIST_SERVER
655 (unsigned short) port,
656 &accept_policy_callback,
658 &access_handler_callback,
660 MHD_OPTION_CONNECTION_LIMIT, (unsigned int) 16,
661 MHD_OPTION_PER_IP_CONNECTION_LIMIT, (unsigned int) 1,
662 MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) 16,
663 MHD_OPTION_CONNECTION_MEMORY_LIMIT, (size_t) (16 * 1024),
666 if ( (daemon_handle_v6 == NULL) &&
667 (daemon_handle_v4 == NULL) )
669 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
670 _("Could not start hostlist HTTP server on port %u\n"),
671 (unsigned short) port);
672 return GNUNET_SYSERR;
677 *server_ch = &connect_handler;
678 *server_dh = &disconnect_handler;
680 if (daemon_handle_v4 != NULL)
681 hostlist_task_v4 = prepare_daemon (daemon_handle_v4);
682 if (daemon_handle_v6 != NULL)
683 hostlist_task_v6 = prepare_daemon (daemon_handle_v6);
685 notify = GNUNET_PEERINFO_notify ( cfg, sched, process_notify, NULL);
691 * Stop server offering our hostlist.
694 GNUNET_HOSTLIST_server_stop ()
696 #if DEBUG_HOSTLIST_SERVER
697 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
698 "Hostlist server shutdown\n");
702 GNUNET_PEERINFO_notify_cancel (notify);
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 (NULL != daemon_handle_v4)
722 MHD_stop_daemon (daemon_handle_v4);
723 daemon_handle_v4 = NULL;
725 if (NULL != daemon_handle_v6)
727 MHD_stop_daemon (daemon_handle_v6);
728 daemon_handle_v6 = NULL;
730 if (response != NULL)
732 MHD_destroy_response (response);
735 if (peerinfo != NULL)
737 GNUNET_PEERINFO_disconnect (peerinfo);
746 /* end of hostlist-server.c */