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_EXTRA_LOGGING
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);
127 MHD_create_response_from_data (results->size, results->data, MHD_YES,
129 if ((daemon_handle_v4 == NULL) && (daemon_handle_v6 == NULL))
131 MHD_destroy_response (response);
134 GNUNET_STATISTICS_set (stats, gettext_noop ("bytes in hostlist"),
135 results->size, GNUNET_YES);
136 GNUNET_free (results);
141 * Set 'cls' to GNUNET_YES (we have an address!).
143 * @param cls closure, an 'int*'
144 * @param tname name of the transport (ignored)
145 * @param expiration expiration time (call is ignored if this is in the past)
146 * @param addr the address (ignored)
147 * @param addrlen length of the address (ignored)
148 * @return GNUNET_SYSERR to stop iterating (unless expiration has occured)
151 check_has_addr (void *cls, const char *tname,
152 struct GNUNET_TIME_Absolute expiration, const void *addr,
157 if (GNUNET_TIME_absolute_get_remaining (expiration).rel_value == 0)
159 GNUNET_STATISTICS_update (stats,
160 gettext_noop ("expired addresses encountered"), 1,
162 return GNUNET_YES; /* ignore this address */
165 return GNUNET_SYSERR;
170 * Callback that processes each of the known HELLOs for the
171 * hostlist response construction.
174 host_processor (void *cls, const struct GNUNET_PeerIdentity *peer,
175 const struct GNUNET_HELLO_Message *hello, const char *err_msg)
177 struct HostSet *results = cls;
184 GNUNET_assert (NULL == peer);
186 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
187 _("Error in communication with PEERINFO service: %s\n"),
194 finish_response (results);
199 has_addr = GNUNET_NO;
200 GNUNET_HELLO_iterate_addresses (hello, GNUNET_NO, &check_has_addr, &has_addr);
201 if (GNUNET_NO == has_addr)
203 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
204 "HELLO for peer `%4s' has no address, not suitable for hostlist!\n",
206 GNUNET_STATISTICS_update (stats,
208 ("HELLOs without addresses encountered (ignored)"),
213 s = GNUNET_HELLO_size (hello);
214 #if DEBUG_HOSTLIST_SERVER
215 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
216 "Received %u bytes of `%s' from peer `%s' for hostlist.\n",
217 (unsigned int) s, "HELLO", GNUNET_i2s (peer));
219 if ((old + s >= GNUNET_MAX_MALLOC_CHECKED) ||
220 (old + s >= MAX_BYTES_PER_HOSTLISTS))
222 GNUNET_STATISTICS_update (stats,
224 ("bytes not included in hostlist (size limit)"),
226 return; /* too large, skip! */
228 #if DEBUG_HOSTLIST_SERVER
229 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
230 "Adding peer `%s' to hostlist (%u bytes)\n", GNUNET_i2s (peer),
233 GNUNET_array_grow (results->data, results->size, old + s);
234 memcpy (&results->data[old], hello, s);
240 * Hostlist access policy (very permissive, allows everything).
243 accept_policy_callback (void *cls, const struct sockaddr *addr,
246 if (NULL == response)
248 #if DEBUG_HOSTLIST_SERVER
249 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
250 "Received request for hostlist, but I am not yet ready; rejecting!\n");
254 return MHD_YES; /* accept all */
259 * Main request handler.
262 access_handler_callback (void *cls, struct MHD_Connection *connection,
263 const char *url, const char *method,
264 const char *version, const char *upload_data,
265 size_t * upload_data_size, void **con_cls)
269 if (0 != strcmp (method, MHD_HTTP_METHOD_GET))
271 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
272 _("Refusing `%s' request to hostlist server\n"), method);
273 GNUNET_STATISTICS_update (stats,
275 ("hostlist requests refused (not HTTP GET)"), 1,
279 if (NULL == *con_cls)
282 #if DEBUG_HOSTLIST_SERVER
283 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Sending 100 CONTINUE reply\n"));
285 return MHD_YES; /* send 100 continue */
287 if (*upload_data_size != 0)
289 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
290 _("Refusing `%s' request with %llu bytes of upload data\n"),
291 method, (unsigned long long) *upload_data_size);
292 GNUNET_STATISTICS_update (stats,
294 ("hostlist requests refused (upload data)"), 1,
296 return MHD_NO; /* do not support upload data */
298 if (response == NULL)
300 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
302 ("Could not handle hostlist request since I do not have a response yet\n"));
303 GNUNET_STATISTICS_update (stats,
305 ("hostlist requests refused (not ready)"), 1,
307 return MHD_NO; /* internal error, no response yet */
309 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Received request for our hostlist\n"));
310 GNUNET_STATISTICS_update (stats, gettext_noop ("hostlist requests processed"),
312 return MHD_queue_response (connection, MHD_HTTP_OK, response);
317 * Handler called by core when core is ready to transmit message
319 * @param size size of buffer to copy message to
320 * @param buf buffer to copy message to
323 adv_transmit_ready (void *cls, size_t size, void *buf)
325 static uint64_t hostlist_adv_count;
327 size_t transmission_size;
328 size_t uri_size; /* Including \0 termination! */
329 struct GNUNET_MessageHeader header;
334 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
335 "Transmission failed, buffer invalid!\n");
338 uri_size = strlen (hostlist_uri) + 1;
339 transmission_size = sizeof (struct GNUNET_MessageHeader) + uri_size;
340 header.type = htons (GNUNET_MESSAGE_TYPE_HOSTLIST_ADVERTISEMENT);
341 header.size = htons (transmission_size);
342 GNUNET_assert (size >= transmission_size);
343 memcpy (buf, &header, sizeof (struct GNUNET_MessageHeader));
345 memcpy (&cbuf[sizeof (struct GNUNET_MessageHeader)], hostlist_uri, uri_size);
346 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
347 "Sent advertisement message: Copied %u bytes into buffer!\n",
348 (unsigned int) transmission_size);
349 hostlist_adv_count++;
350 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " # Sent advertisement message: %u\n",
352 GNUNET_STATISTICS_update (stats,
353 gettext_noop ("# hostlist advertisements send"), 1,
355 return transmission_size;
360 * Method called whenever a given peer connects.
363 * @param peer peer identity this notification is about
364 * @param atsi performance data
367 connect_handler (void *cls, const struct GNUNET_PeerIdentity *peer,
368 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
374 if (hostlist_uri == NULL)
376 size = strlen (hostlist_uri) + 1;
377 if (size + sizeof (struct GNUNET_MessageHeader) >=
378 GNUNET_SERVER_MAX_MESSAGE_SIZE)
383 size += sizeof (struct GNUNET_MessageHeader);
389 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
390 "Asked core to transmit advertisement message with a size of %u bytes to peer `%s'\n",
391 size, GNUNET_i2s (peer));
393 GNUNET_CORE_notify_transmit_ready (core, GNUNET_YES, 0,
394 GNUNET_ADV_TIMEOUT, peer, size,
395 &adv_transmit_ready, NULL))
397 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
398 _("Advertisement message could not be queued by core\n"));
404 * Method called whenever a given peer disconnects.
407 * @param peer peer identity this notification is about
410 disconnect_handler (void *cls, const struct GNUNET_PeerIdentity *peer)
416 * PEERINFO calls this function to let us know about a possible peer
417 * that we might want to connect to.
419 * @param cls closure (not used)
420 * @param peer potential peer to connect to
421 * @param hello HELLO for this peer (or NULL)
422 * @param err_msg NULL if successful, otherwise contains error message
425 process_notify (void *cls, const struct GNUNET_PeerIdentity *peer,
426 const struct GNUNET_HELLO_Message *hello, const char *err_msg)
428 struct HostSet *results;
430 #if DEBUG_HOSTLIST_SERVER
431 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
432 "Peerinfo is notifying us to rebuild our hostlist\n");
436 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
437 _("Error in communication with PEERINFO service\n"));
440 results = GNUNET_malloc (sizeof (struct HostSet));
441 GNUNET_assert (peerinfo != NULL);
443 GNUNET_PEERINFO_iterate (peerinfo, NULL, GNUNET_TIME_UNIT_MINUTES,
444 &host_processor, results);
448 * Function that queries MHD's select sets and
449 * starts the task waiting for them.
451 static GNUNET_SCHEDULER_TaskIdentifier
452 prepare_daemon (struct MHD_Daemon *daemon_handle);
456 * Call MHD to process pending requests and then go back
457 * and schedule the next run.
460 run_daemon (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
462 struct MHD_Daemon *daemon_handle = cls;
464 if (daemon_handle == daemon_handle_v4)
465 hostlist_task_v4 = GNUNET_SCHEDULER_NO_TASK;
467 hostlist_task_v6 = GNUNET_SCHEDULER_NO_TASK;
469 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
471 GNUNET_assert (MHD_YES == MHD_run (daemon_handle));
472 if (daemon_handle == daemon_handle_v4)
473 hostlist_task_v4 = prepare_daemon (daemon_handle);
475 hostlist_task_v6 = prepare_daemon (daemon_handle);
480 * Function that queries MHD's select sets and
481 * starts the task waiting for them.
483 static GNUNET_SCHEDULER_TaskIdentifier
484 prepare_daemon (struct MHD_Daemon *daemon_handle)
486 GNUNET_SCHEDULER_TaskIdentifier ret;
490 struct GNUNET_NETWORK_FDSet *wrs;
491 struct GNUNET_NETWORK_FDSet *wws;
492 struct GNUNET_NETWORK_FDSet *wes;
494 unsigned long long timeout;
496 struct GNUNET_TIME_Relative tv;
501 wrs = GNUNET_NETWORK_fdset_create ();
502 wes = GNUNET_NETWORK_fdset_create ();
503 wws = GNUNET_NETWORK_fdset_create ();
505 GNUNET_assert (MHD_YES == MHD_get_fdset (daemon_handle, &rs, &ws, &es, &max));
506 haveto = MHD_get_timeout (daemon_handle, &timeout);
507 if (haveto == MHD_YES)
508 tv.rel_value = (uint64_t) timeout;
510 tv = GNUNET_TIME_UNIT_FOREVER_REL;
511 GNUNET_NETWORK_fdset_copy_native (wrs, &rs, max + 1);
512 GNUNET_NETWORK_fdset_copy_native (wws, &ws, max + 1);
513 GNUNET_NETWORK_fdset_copy_native (wes, &es, max + 1);
515 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_HIGH,
516 GNUNET_SCHEDULER_NO_TASK, tv, wrs, wws,
517 &run_daemon, daemon_handle);
518 GNUNET_NETWORK_fdset_destroy (wrs);
519 GNUNET_NETWORK_fdset_destroy (wws);
520 GNUNET_NETWORK_fdset_destroy (wes);
527 * Start server offering our hostlist.
529 * @return GNUNET_OK on success
532 GNUNET_HOSTLIST_server_start (const struct GNUNET_CONFIGURATION_Handle *c,
533 struct GNUNET_STATISTICS_Handle *st,
534 struct GNUNET_CORE_Handle *co,
535 GNUNET_CORE_ConnectEventHandler *server_ch,
536 GNUNET_CORE_DisconnectEventHandler *server_dh,
539 unsigned long long port;
543 advertising = advertise;
545 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
546 "Advertising not enabled on this hostlist server\n");
548 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
549 "Advertising enabled on this hostlist server\n");
552 peerinfo = GNUNET_PEERINFO_connect (cfg);
553 if (peerinfo == NULL)
555 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
556 _("Could not access PEERINFO service. Exiting.\n"));
557 return GNUNET_SYSERR;
560 GNUNET_CONFIGURATION_get_value_number (cfg, "HOSTLIST", "HTTPPORT",
562 return GNUNET_SYSERR;
563 if ((port == 0) || (port > UINT16_MAX))
565 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
566 _("Invalid port number %llu. Exiting.\n"), port);
567 return GNUNET_SYSERR;
571 GNUNET_CONFIGURATION_get_value_string (cfg, "HOSTLIST",
572 "EXTERNAL_DNS_NAME", &hostname))
573 hostname = GNUNET_RESOLVER_local_fqdn_get ();
575 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Hostlist service starts on %s:%llu\n"),
577 if (NULL != hostname)
579 size = strlen (hostname);
580 if (size + 15 > MAX_URL_LEN)
586 GNUNET_asprintf (&hostlist_uri, "http://%s:%u/", hostname,
587 (unsigned int) port);
588 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
589 _("Address to obtain hostlist: `%s'\n"), hostlist_uri);
591 GNUNET_free (hostname);
593 daemon_handle_v6 = MHD_start_daemon (MHD_USE_IPv6
594 #if DEBUG_HOSTLIST_SERVER
597 , (unsigned short) port,
598 &accept_policy_callback, NULL,
599 &access_handler_callback, NULL,
600 MHD_OPTION_CONNECTION_LIMIT,
602 MHD_OPTION_PER_IP_CONNECTION_LIMIT,
604 MHD_OPTION_CONNECTION_TIMEOUT,
606 MHD_OPTION_CONNECTION_MEMORY_LIMIT,
607 (size_t) (16 * 1024), MHD_OPTION_END);
608 daemon_handle_v4 = MHD_start_daemon (MHD_NO_FLAG
609 #if DEBUG_HOSTLIST_SERVER
612 , (unsigned short) port,
613 &accept_policy_callback, NULL,
614 &access_handler_callback, NULL,
615 MHD_OPTION_CONNECTION_LIMIT,
617 MHD_OPTION_PER_IP_CONNECTION_LIMIT,
619 MHD_OPTION_CONNECTION_TIMEOUT,
621 MHD_OPTION_CONNECTION_MEMORY_LIMIT,
622 (size_t) (16 * 1024), MHD_OPTION_END);
624 if ((daemon_handle_v6 == NULL) && (daemon_handle_v4 == NULL))
626 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
627 _("Could not start hostlist HTTP server on port %u\n"),
628 (unsigned short) port);
629 return GNUNET_SYSERR;
634 *server_ch = &connect_handler;
635 *server_dh = &disconnect_handler;
637 if (daemon_handle_v4 != NULL)
638 hostlist_task_v4 = prepare_daemon (daemon_handle_v4);
639 if (daemon_handle_v6 != NULL)
640 hostlist_task_v6 = prepare_daemon (daemon_handle_v6);
642 notify = GNUNET_PEERINFO_notify (cfg, process_notify, NULL);
648 * Stop server offering our hostlist.
651 GNUNET_HOSTLIST_server_stop ()
653 #if DEBUG_HOSTLIST_SERVER
654 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Hostlist server shutdown\n");
658 GNUNET_PEERINFO_notify_cancel (notify);
661 if (GNUNET_SCHEDULER_NO_TASK != hostlist_task_v6)
663 GNUNET_SCHEDULER_cancel (hostlist_task_v6);
664 hostlist_task_v6 = GNUNET_SCHEDULER_NO_TASK;
666 if (GNUNET_SCHEDULER_NO_TASK != hostlist_task_v4)
668 GNUNET_SCHEDULER_cancel (hostlist_task_v4);
669 hostlist_task_v4 = GNUNET_SCHEDULER_NO_TASK;
673 GNUNET_PEERINFO_iterate_cancel (pitr);
676 if (NULL != daemon_handle_v4)
678 MHD_stop_daemon (daemon_handle_v4);
679 daemon_handle_v4 = NULL;
681 if (NULL != daemon_handle_v6)
683 MHD_stop_daemon (daemon_handle_v6);
684 daemon_handle_v6 = NULL;
686 if (response != NULL)
688 MHD_destroy_response (response);
691 if (peerinfo != NULL)
693 GNUNET_PEERINFO_disconnect (peerinfo);
701 /* end of hostlist-server.c */