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"
37 * Handle to the HTTP server as provided by libmicrohttpd for IPv6.
39 static struct MHD_Daemon *daemon_handle_v6;
42 * Handle to the HTTP server as provided by libmicrohttpd for IPv4.
44 static struct MHD_Daemon *daemon_handle_v4;
49 static const struct GNUNET_CONFIGURATION_Handle *cfg;
52 * For keeping statistics.
54 static struct GNUNET_STATISTICS_Handle *stats;
57 * Handle to the core service (NULL until we've connected to it).
59 static struct GNUNET_CORE_Handle *core;
62 * Handle to the peerinfo notify service (NULL until we've connected to it).
64 static struct GNUNET_PEERINFO_NotifyContext *notify;
67 * Our primary task for IPv4.
69 static GNUNET_SCHEDULER_TaskIdentifier hostlist_task_v4;
72 * Our primary task for IPv6.
74 static GNUNET_SCHEDULER_TaskIdentifier hostlist_task_v6;
77 * Our canonical response.
79 static struct MHD_Response *response;
82 * NULL if we are not currenlty iterating over peer information.
84 static struct GNUNET_PEERINFO_IteratorContext *pitr;
87 * Handle for accessing peerinfo service.
89 static struct GNUNET_PEERINFO_Handle *peerinfo;
92 * Set if we are allowed to advertise our hostlist to others.
94 static int advertising;
97 * Buffer for the hostlist address
99 static char *hostlist_uri;
104 * Context for host processor.
116 * Function that assembles our response.
119 finish_response (struct HostSet *results)
121 if (NULL != response)
122 MHD_destroy_response (response);
123 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
124 "Creating hostlist response with %u bytes\n",
125 (unsigned int) results->size);
127 MHD_create_response_from_data (results->size, results->data, MHD_YES,
129 if ((NULL == daemon_handle_v4) && (NULL == daemon_handle_v6))
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 address the address (ignored)
145 * @param expiration expiration time (call is ignored if this is in the past)
146 * @return GNUNET_SYSERR to stop iterating (unless expiration has occured)
149 check_has_addr (void *cls, const struct GNUNET_HELLO_Address *address,
150 struct GNUNET_TIME_Absolute expiration)
154 if (GNUNET_TIME_absolute_get_remaining (expiration).rel_value == 0)
156 GNUNET_STATISTICS_update (stats,
157 gettext_noop ("expired addresses encountered"), 1,
159 return GNUNET_YES; /* ignore this address */
162 return GNUNET_SYSERR;
167 * Callback that processes each of the known HELLOs for the
168 * hostlist response construction.
171 host_processor (void *cls, const struct GNUNET_PeerIdentity *peer,
172 const struct GNUNET_HELLO_Message *hello, const char *err_msg)
174 struct HostSet *results = cls;
181 GNUNET_assert (NULL == peer);
183 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
184 _("Error in communication with PEERINFO service: %s\n"),
191 finish_response (results);
196 has_addr = GNUNET_NO;
197 GNUNET_HELLO_iterate_addresses (hello, GNUNET_NO, &check_has_addr, &has_addr);
198 if (GNUNET_NO == has_addr)
200 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
201 "HELLO for peer `%4s' has no address, not suitable for hostlist!\n",
203 GNUNET_STATISTICS_update (stats,
205 ("HELLOs without addresses encountered (ignored)"),
210 s = GNUNET_HELLO_size (hello);
211 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
212 "Received %u bytes of `%s' from peer `%s' for hostlist.\n",
213 (unsigned int) s, "HELLO", GNUNET_i2s (peer));
214 if ((old + s >= GNUNET_MAX_MALLOC_CHECKED) ||
215 (old + s >= MAX_BYTES_PER_HOSTLISTS))
217 GNUNET_STATISTICS_update (stats,
219 ("bytes not included in hostlist (size limit)"),
221 return; /* too large, skip! */
223 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
224 "Adding peer `%s' to hostlist (%u bytes)\n", GNUNET_i2s (peer),
226 GNUNET_array_grow (results->data, results->size, old + s);
227 memcpy (&results->data[old], hello, s);
233 * Hostlist access policy (very permissive, allows everything).
236 accept_policy_callback (void *cls, const struct sockaddr *addr,
239 if (NULL == response)
241 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
242 "Received request for hostlist, but I am not yet ready; rejecting!\n");
245 return MHD_YES; /* accept all */
250 * Main request handler.
253 access_handler_callback (void *cls, struct MHD_Connection *connection,
254 const char *url, const char *method,
255 const char *version, const char *upload_data,
256 size_t * upload_data_size, void **con_cls)
260 if (0 != strcmp (method, MHD_HTTP_METHOD_GET))
262 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
263 _("Refusing `%s' request to hostlist server\n"), method);
264 GNUNET_STATISTICS_update (stats,
266 ("hostlist requests refused (not HTTP GET)"), 1,
270 if (NULL == *con_cls)
273 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending 100 CONTINUE reply\n");
274 return MHD_YES; /* send 100 continue */
276 if (0 != *upload_data_size)
278 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
279 _("Refusing `%s' request with %llu bytes of upload data\n"),
280 method, (unsigned long long) *upload_data_size);
281 GNUNET_STATISTICS_update (stats,
283 ("hostlist requests refused (upload data)"), 1,
285 return MHD_NO; /* do not support upload data */
287 if (NULL == response)
289 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
291 ("Could not handle hostlist request since I do not have a response yet\n"));
292 GNUNET_STATISTICS_update (stats,
294 ("hostlist requests refused (not ready)"), 1,
296 return MHD_NO; /* internal error, no response yet */
298 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Received request for our hostlist\n"));
299 GNUNET_STATISTICS_update (stats, gettext_noop ("hostlist requests processed"),
301 return MHD_queue_response (connection, MHD_HTTP_OK, response);
306 * Handler called by core when core is ready to transmit message
308 * @param size size of buffer to copy message to
309 * @param buf buffer to copy message to
312 adv_transmit_ready (void *cls, size_t size, void *buf)
314 static uint64_t hostlist_adv_count;
315 size_t transmission_size;
316 size_t uri_size; /* Including \0 termination! */
317 struct GNUNET_MessageHeader header;
322 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
323 "Transmission failed, buffer invalid!\n");
326 uri_size = strlen (hostlist_uri) + 1;
327 transmission_size = sizeof (struct GNUNET_MessageHeader) + uri_size;
328 header.type = htons (GNUNET_MESSAGE_TYPE_HOSTLIST_ADVERTISEMENT);
329 header.size = htons (transmission_size);
330 GNUNET_assert (size >= transmission_size);
331 memcpy (buf, &header, sizeof (struct GNUNET_MessageHeader));
333 memcpy (&cbuf[sizeof (struct GNUNET_MessageHeader)], hostlist_uri, uri_size);
334 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
335 "Sent advertisement message: Copied %u bytes into buffer!\n",
336 (unsigned int) transmission_size);
337 hostlist_adv_count++;
338 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " # Sent advertisement message: %u\n",
340 GNUNET_STATISTICS_update (stats,
341 gettext_noop ("# hostlist advertisements send"), 1,
343 return transmission_size;
348 * Method called whenever a given peer connects.
351 * @param peer peer identity this notification is about
354 connect_handler (void *cls, const struct GNUNET_PeerIdentity *peer)
360 if (NULL == hostlist_uri)
362 size = strlen (hostlist_uri) + 1;
363 if (size + sizeof (struct GNUNET_MessageHeader) >=
364 GNUNET_SERVER_MAX_MESSAGE_SIZE)
369 size += sizeof (struct GNUNET_MessageHeader);
375 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
376 "Asked core to transmit advertisement message with a size of %u bytes to peer `%s'\n",
377 size, GNUNET_i2s (peer));
379 GNUNET_CORE_notify_transmit_ready (core, GNUNET_YES, 0,
380 GNUNET_ADV_TIMEOUT, peer, size,
381 &adv_transmit_ready, NULL))
383 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
384 _("Advertisement message could not be queued by core\n"));
390 * Method called whenever a given peer disconnects.
393 * @param peer peer identity this notification is about
396 disconnect_handler (void *cls, const struct GNUNET_PeerIdentity *peer)
403 * PEERINFO calls this function to let us know about a possible peer
404 * that we might want to connect to.
406 * @param cls closure (not used)
407 * @param peer potential peer to connect to
408 * @param hello HELLO for this peer (or NULL)
409 * @param err_msg NULL if successful, otherwise contains error message
412 process_notify (void *cls, const struct GNUNET_PeerIdentity *peer,
413 const struct GNUNET_HELLO_Message *hello, const char *err_msg)
415 struct HostSet *results;
417 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
418 "Peerinfo is notifying us to rebuild our hostlist\n");
420 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
421 _("Error in communication with PEERINFO service: %s\n"),
424 return; /* re-build already in progress ... */
425 results = GNUNET_malloc (sizeof (struct HostSet));
426 GNUNET_assert (NULL != peerinfo);
428 GNUNET_PEERINFO_iterate (peerinfo, NULL, GNUNET_TIME_UNIT_MINUTES,
429 &host_processor, results);
434 * Function that queries MHD's select sets and
435 * starts the task waiting for them.
437 static GNUNET_SCHEDULER_TaskIdentifier
438 prepare_daemon (struct MHD_Daemon *daemon_handle);
442 * Call MHD to process pending requests and then go back
443 * and schedule the next run.
446 run_daemon (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
448 struct MHD_Daemon *daemon_handle = cls;
450 if (daemon_handle == daemon_handle_v4)
451 hostlist_task_v4 = GNUNET_SCHEDULER_NO_TASK;
453 hostlist_task_v6 = GNUNET_SCHEDULER_NO_TASK;
455 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
457 GNUNET_assert (MHD_YES == MHD_run (daemon_handle));
458 if (daemon_handle == daemon_handle_v4)
459 hostlist_task_v4 = prepare_daemon (daemon_handle);
461 hostlist_task_v6 = prepare_daemon (daemon_handle);
464 #define UNSIGNED_MHD_LONG_LONG unsigned MHD_LONG_LONG
467 * Function that queries MHD's select sets and
468 * starts the task waiting for them.
470 static GNUNET_SCHEDULER_TaskIdentifier
471 prepare_daemon (struct MHD_Daemon *daemon_handle)
473 GNUNET_SCHEDULER_TaskIdentifier ret;
477 struct GNUNET_NETWORK_FDSet *wrs;
478 struct GNUNET_NETWORK_FDSet *wws;
479 struct GNUNET_NETWORK_FDSet *wes;
481 UNSIGNED_MHD_LONG_LONG timeout;
483 struct GNUNET_TIME_Relative tv;
488 wrs = GNUNET_NETWORK_fdset_create ();
489 wes = GNUNET_NETWORK_fdset_create ();
490 wws = GNUNET_NETWORK_fdset_create ();
492 GNUNET_assert (MHD_YES == MHD_get_fdset (daemon_handle, &rs, &ws, &es, &max));
493 haveto = MHD_get_timeout (daemon_handle, &timeout);
494 if (haveto == MHD_YES)
495 tv.rel_value = (uint64_t) timeout;
497 tv = GNUNET_TIME_UNIT_FOREVER_REL;
498 GNUNET_NETWORK_fdset_copy_native (wrs, &rs, max + 1);
499 GNUNET_NETWORK_fdset_copy_native (wws, &ws, max + 1);
500 GNUNET_NETWORK_fdset_copy_native (wes, &es, max + 1);
502 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_HIGH,
504 &run_daemon, daemon_handle);
505 GNUNET_NETWORK_fdset_destroy (wrs);
506 GNUNET_NETWORK_fdset_destroy (wws);
507 GNUNET_NETWORK_fdset_destroy (wes);
513 * Start server offering our hostlist.
515 * @return GNUNET_OK on success
518 GNUNET_HOSTLIST_server_start (const struct GNUNET_CONFIGURATION_Handle *c,
519 struct GNUNET_STATISTICS_Handle *st,
520 struct GNUNET_CORE_Handle *co,
521 GNUNET_CORE_ConnectEventHandler *server_ch,
522 GNUNET_CORE_DisconnectEventHandler *server_dh,
525 unsigned long long port;
531 struct sockaddr_in v4;
532 struct sockaddr_in6 v6;
533 const struct sockaddr *sa;
535 advertising = advertise;
537 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
538 "Advertising not enabled on this hostlist server\n");
540 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
541 "Advertising enabled on this hostlist server\n");
544 peerinfo = GNUNET_PEERINFO_connect (cfg);
545 if (NULL == peerinfo)
547 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
548 _("Could not access PEERINFO service. Exiting.\n"));
549 return GNUNET_SYSERR;
552 GNUNET_CONFIGURATION_get_value_number (cfg, "HOSTLIST", "HTTPPORT",
554 return GNUNET_SYSERR;
555 if ((0 == port) || (port > UINT16_MAX))
557 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
558 _("Invalid port number %llu. Exiting.\n"), port);
559 return GNUNET_SYSERR;
563 GNUNET_CONFIGURATION_get_value_string (cfg, "HOSTLIST",
564 "EXTERNAL_DNS_NAME", &hostname))
565 hostname = GNUNET_RESOLVER_local_fqdn_get ();
567 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Hostlist service starts on %s:%llu\n"),
569 if (NULL != hostname)
571 size = strlen (hostname);
572 if (size + 15 > MAX_URL_LEN)
578 GNUNET_asprintf (&hostlist_uri, "http://%s:%u/", hostname,
579 (unsigned int) port);
580 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
581 _("Address to obtain hostlist: `%s'\n"), hostlist_uri);
583 GNUNET_free (hostname);
586 if (GNUNET_CONFIGURATION_have_value (cfg, "HOSTLIST", "BINDTOIP"))
588 GNUNET_break (GNUNET_OK ==
589 GNUNET_CONFIGURATION_get_value_string (cfg, "HOSTLIST",
596 if (1 == inet_pton (AF_INET, ip, &i4))
598 memset (&v4, 0, sizeof (v4));
599 v4.sin_family = AF_INET;
601 v4.sin_port = htons (port);
602 #if HAVE_SOCKADDR_IN_SIN_LEN
603 v4.sin_len = sizeof (v4);
605 sa = (const struct sockaddr *) &v4;
607 else if (1 == inet_pton (AF_INET6, ip, &i6))
609 memset (&v6, 0, sizeof (v6));
610 v6.sin6_family = AF_INET6;
612 v6.sin6_port = htons (port);
613 #if HAVE_SOCKADDR_IN_SIN_LEN
614 v6.sin6_len = sizeof (v6);
616 sa = (const struct sockaddr *) &v6;
620 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
621 _("`%s' is not a valid IP address! Ignoring BINDTOIP.\n"),
629 daemon_handle_v6 = MHD_start_daemon (MHD_USE_IPv6 | MHD_USE_DEBUG,
631 &accept_policy_callback, NULL,
632 &access_handler_callback, NULL,
633 MHD_OPTION_CONNECTION_LIMIT,
635 MHD_OPTION_PER_IP_CONNECTION_LIMIT,
637 MHD_OPTION_CONNECTION_TIMEOUT,
639 MHD_OPTION_CONNECTION_MEMORY_LIMIT,
640 (size_t) (16 * 1024),
641 MHD_OPTION_SOCK_ADDR,
644 daemon_handle_v4 = MHD_start_daemon (MHD_NO_FLAG | MHD_USE_DEBUG,
646 &accept_policy_callback, NULL,
647 &access_handler_callback, NULL,
648 MHD_OPTION_CONNECTION_LIMIT,
650 MHD_OPTION_PER_IP_CONNECTION_LIMIT,
652 MHD_OPTION_CONNECTION_TIMEOUT,
654 MHD_OPTION_CONNECTION_MEMORY_LIMIT,
655 (size_t) (16 * 1024),
656 MHD_OPTION_SOCK_ADDR,
660 if ((NULL == daemon_handle_v6) && (NULL == daemon_handle_v4))
662 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
663 _("Could not start hostlist HTTP server on port %u\n"),
664 (unsigned short) port);
665 return GNUNET_SYSERR;
669 *server_ch = &connect_handler;
670 *server_dh = &disconnect_handler;
671 if (daemon_handle_v4 != NULL)
672 hostlist_task_v4 = prepare_daemon (daemon_handle_v4);
673 if (daemon_handle_v6 != NULL)
674 hostlist_task_v6 = prepare_daemon (daemon_handle_v6);
676 notify = GNUNET_PEERINFO_notify (cfg, &process_notify, NULL);
683 * Stop server offering our hostlist.
686 GNUNET_HOSTLIST_server_stop ()
688 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Hostlist server shutdown\n");
689 if (GNUNET_SCHEDULER_NO_TASK != hostlist_task_v6)
691 GNUNET_SCHEDULER_cancel (hostlist_task_v6);
692 hostlist_task_v6 = GNUNET_SCHEDULER_NO_TASK;
694 if (GNUNET_SCHEDULER_NO_TASK != hostlist_task_v4)
696 GNUNET_SCHEDULER_cancel (hostlist_task_v4);
697 hostlist_task_v4 = GNUNET_SCHEDULER_NO_TASK;
699 if (NULL != daemon_handle_v4)
701 MHD_stop_daemon (daemon_handle_v4);
702 daemon_handle_v4 = NULL;
704 if (NULL != daemon_handle_v6)
706 MHD_stop_daemon (daemon_handle_v6);
707 daemon_handle_v6 = NULL;
709 if (NULL != response)
711 MHD_destroy_response (response);
716 GNUNET_PEERINFO_notify_cancel (notify);
721 GNUNET_PEERINFO_iterate_cancel (pitr);
724 if (NULL != peerinfo)
726 GNUNET_PEERINFO_disconnect (peerinfo);
734 /* end of hostlist-server.c */