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
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 * How often should we recalculate our response to hostlist requests?
40 #define RESPONSE_UPDATE_FREQUENCY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 5)
43 * Handle to the HTTP server as provided by libmicrohttpd for IPv6.
45 static struct MHD_Daemon *daemon_handle_v6;
48 * Handle to the HTTP server as provided by libmicrohttpd for IPv4.
50 static struct MHD_Daemon *daemon_handle_v4;
55 static const struct GNUNET_CONFIGURATION_Handle *cfg;
60 static struct GNUNET_SCHEDULER_Handle *sched;
63 * For keeping statistics.
65 static struct GNUNET_STATISTICS_Handle *stats;
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 * Task that updates our HTTP response.
80 static GNUNET_SCHEDULER_TaskIdentifier response_task;
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 * Context for host processor.
103 * Task that will produce a new response object.
106 update_response (void *cls,
107 const struct GNUNET_SCHEDULER_TaskContext *tc);
110 * Function that assembles our hostlist adv message.
113 create_hostlist_adv_message (void)
116 unsigned long long port;
119 char hostname[HOST_NAME_MAX];
120 char *protocol = "http://";
121 char *port_s = GNUNET_malloc(6 * sizeof(char));
123 if (0 != gethostname (hostname, sizeof (hostname) - 1))
125 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR,
126 "Could not get system's hostname, unable to create advertisement message");
129 if (-1 == GNUNET_CONFIGURATION_get_value_number (cfg,
133 return GNUNET_SYSERR;
135 sprintf(port_s, "%llu", port);
136 length = strlen(hostname)+strlen(protocol)+strlen(port_s)+2;
138 uri = GNUNET_malloc(length * sizeof(char));
139 uri = strcpy(uri, protocol);
140 uri = strcat(uri, hostname);
141 uri = strcat(uri, ":");
142 uri = strcat(uri, port_s);
143 uri = strcat(uri, "/");
144 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Address to obtain hostlist: %s\n", uri);
152 * Function that assembles our response.
155 finish_response (struct HostSet *results)
157 struct GNUNET_TIME_Relative freq;
159 if (response != NULL)
160 MHD_destroy_response (response);
161 #if DEBUG_HOSTLIST_SERVER
162 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
163 "Creating hostlist response with %u bytes\n",
164 (unsigned int) results->size);
166 response = MHD_create_response_from_data (results->size,
167 results->data, MHD_YES, MHD_NO);
168 if ( (daemon_handle_v4 != NULL) ||
169 (daemon_handle_v6 != NULL) )
171 freq = RESPONSE_UPDATE_FREQUENCY;
172 if (results->size == 0)
173 freq = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 250);
174 /* schedule next update of the response */
175 response_task = GNUNET_SCHEDULER_add_delayed (sched,
182 /* already past shutdown */
183 MHD_destroy_response (response);
186 GNUNET_STATISTICS_set (stats,
187 gettext_noop("bytes in hostlist"),
190 GNUNET_free (results);
195 * Set 'cls' to GNUNET_YES (we have an address!).
197 * @param cls closure, an 'int*'
198 * @param tname name of the transport (ignored)
199 * @param expiration expiration time (call is ignored if this is in the past)
200 * @param addr the address (ignored)
201 * @param addrlen length of the address (ignored)
202 * @return GNUNET_SYSERR to stop iterating (unless expiration has occured)
205 check_has_addr (void *cls,
207 struct GNUNET_TIME_Absolute expiration,
208 const void *addr, size_t addrlen)
212 if (GNUNET_TIME_absolute_get_remaining (expiration).value == 0)
214 GNUNET_STATISTICS_update (stats,
215 gettext_noop("expired addresses encountered"),
218 return GNUNET_YES; /* ignore this address */
221 return GNUNET_SYSERR;
226 * Callback that processes each of the known HELLOs for the
227 * hostlist response construction.
230 host_processor (void *cls,
231 const struct GNUNET_PeerIdentity * peer,
232 const struct GNUNET_HELLO_Message *hello,
235 struct HostSet *results = cls;
243 finish_response (results);
246 has_addr = GNUNET_NO;
247 GNUNET_HELLO_iterate_addresses (hello,
251 if (GNUNET_NO == has_addr)
253 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
254 "HELLO for peer `%4s' has no address, not suitable for hostlist!\n",
256 GNUNET_STATISTICS_update (stats,
257 gettext_noop("HELLOs without addresses encountered (ignored)"),
263 s = GNUNET_HELLO_size(hello);
264 #if DEBUG_HOSTLIST_SERVER
265 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
266 "Received %u bytes of `%s' from peer `%s' for hostlist.\n",
271 if (old + s >= GNUNET_MAX_MALLOC_CHECKED)
273 GNUNET_STATISTICS_update (stats,
274 gettext_noop("bytes not included in hostlist (size limit)"),
277 return; /* too large, skip! */
279 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
280 "Adding peer `%s' to hostlist (%u bytes)\n",
283 GNUNET_array_grow (results->data,
286 memcpy (&results->data[old], hello, s);
291 * Task that will produce a new response object.
294 update_response (void *cls,
295 const struct GNUNET_SCHEDULER_TaskContext *tc)
297 struct HostSet *results;
299 response_task = GNUNET_SCHEDULER_NO_TASK;
300 results = GNUNET_malloc(sizeof(struct HostSet));
301 pitr = GNUNET_PEERINFO_iterate (cfg, sched,
304 GNUNET_TIME_UNIT_MINUTES,
311 * Hostlist access policy (very permissive, allows everything).
314 accept_policy_callback (void *cls,
315 const struct sockaddr *addr, socklen_t addrlen)
317 if (NULL == response)
319 #if DEBUG_HOSTLIST_SERVER
320 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
321 "Received request for hostlist, but I am not yet ready; rejecting!\n");
325 return MHD_YES; /* accept all */
330 * Main request handler.
333 access_handler_callback (void *cls,
334 struct MHD_Connection *connection,
338 const char *upload_data,
339 size_t*upload_data_size, void **con_cls)
343 if (0 != strcmp (method, MHD_HTTP_METHOD_GET))
345 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
346 _("Refusing `%s' request to hostlist server\n"),
348 GNUNET_STATISTICS_update (stats,
349 gettext_noop("hostlist requests refused (not HTTP GET)"),
354 if (NULL == *con_cls)
357 #if DEBUG_HOSTLIST_SERVER
358 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
359 _("Sending 100 CONTINUE reply\n"));
361 return MHD_YES; /* send 100 continue */
363 if (*upload_data_size != 0)
365 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
366 _("Refusing `%s' request with %llu bytes of upload data\n"),
368 (unsigned long long) *upload_data_size);
369 GNUNET_STATISTICS_update (stats,
370 gettext_noop("hostlist requests refused (upload data)"),
373 return MHD_NO; /* do not support upload data */
375 if (response == NULL)
377 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
378 _("Could not handle hostlist request since I do not have a response yet\n"));
379 GNUNET_STATISTICS_update (stats,
380 gettext_noop("hostlist requests refused (not ready)"),
383 return MHD_NO; /* internal error, no response yet */
385 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
386 _("Received request for our hostlist\n"));
387 GNUNET_STATISTICS_update (stats,
388 gettext_noop("hostlist requests processed"),
391 return MHD_queue_response (connection, MHD_HTTP_OK, response);
395 * Method called whenever a given peer connects.
398 * @param peer peer identity this notification is about
399 * @param latency reported latency of the connection with 'other'
400 * @param distance reported distance (DV) to 'other'
403 connect_handler (void *cls,
405 GNUNET_PeerIdentity * peer,
406 struct GNUNET_TIME_Relative latency,
409 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
410 "A new peer connected to the server, preparing to send hostlist advertisement\n");
411 /* create a new advertisement message */
412 struct GNUNET_HOSTLIST_ADV_Message *adv_msg =
413 create_hostlist_adv_message();
419 * Method called whenever a given peer disconnects.
422 * @param peer peer identity this notification is about
425 disconnect_handler (void *cls,
427 GNUNET_PeerIdentity * peer)
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);
441 * Call MHD to process pending requests and then go back
442 * and schedule the next run.
445 run_daemon (void *cls,
446 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);
466 * Function that queries MHD's select sets and
467 * starts the task waiting for them.
469 static GNUNET_SCHEDULER_TaskIdentifier
470 prepare_daemon (struct MHD_Daemon *daemon_handle)
472 GNUNET_SCHEDULER_TaskIdentifier ret;
476 struct GNUNET_NETWORK_FDSet *wrs;
477 struct GNUNET_NETWORK_FDSet *wws;
478 struct GNUNET_NETWORK_FDSet *wes;
480 unsigned long long timeout;
482 struct GNUNET_TIME_Relative tv;
487 wrs = GNUNET_NETWORK_fdset_create ();
488 wes = GNUNET_NETWORK_fdset_create ();
489 wws = GNUNET_NETWORK_fdset_create ();
491 GNUNET_assert (MHD_YES ==
492 MHD_get_fdset (daemon_handle,
497 haveto = MHD_get_timeout (daemon_handle, &timeout);
498 if (haveto == MHD_YES)
499 tv.value = (uint64_t) timeout;
501 tv = GNUNET_TIME_UNIT_FOREVER_REL;
502 GNUNET_NETWORK_fdset_copy_native (wrs, &rs, max);
503 GNUNET_NETWORK_fdset_copy_native (wws, &ws, max);
504 GNUNET_NETWORK_fdset_copy_native (wes, &es, max);
505 ret = GNUNET_SCHEDULER_add_select (sched,
506 GNUNET_SCHEDULER_PRIORITY_HIGH,
507 GNUNET_SCHEDULER_NO_TASK,
513 GNUNET_NETWORK_fdset_destroy (wrs);
514 GNUNET_NETWORK_fdset_destroy (wws);
515 GNUNET_NETWORK_fdset_destroy (wes);
522 * Start server offering our hostlist.
524 * @return GNUNET_OK on success
527 GNUNET_HOSTLIST_server_start (const struct GNUNET_CONFIGURATION_Handle *c,
528 struct GNUNET_SCHEDULER_Handle *s,
529 struct GNUNET_STATISTICS_Handle *st,
530 GNUNET_CORE_ConnectEventHandler *server_ch,
531 GNUNET_CORE_DisconnectEventHandler *server_dh)
533 unsigned long long port;
538 if (-1 == GNUNET_CONFIGURATION_get_value_number (cfg,
542 return GNUNET_SYSERR;
543 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
544 _("Hostlist service starts on port %llu\n"),
546 daemon_handle_v6 = MHD_start_daemon (MHD_USE_IPv6
547 #if DEBUG_HOSTLIST_SERVER
551 (unsigned short) port,
552 &accept_policy_callback,
554 &access_handler_callback,
556 MHD_OPTION_CONNECTION_LIMIT, (unsigned int) 16,
557 MHD_OPTION_PER_IP_CONNECTION_LIMIT, (unsigned int) 1,
558 MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) 16,
559 MHD_OPTION_CONNECTION_MEMORY_LIMIT, (size_t) (16 * 1024),
561 daemon_handle_v4 = MHD_start_daemon (MHD_NO_FLAG
562 #if DEBUG_HOSTLIST_SERVER
566 (unsigned short) port,
567 &accept_policy_callback,
569 &access_handler_callback,
571 MHD_OPTION_CONNECTION_LIMIT, (unsigned int) 16,
572 MHD_OPTION_PER_IP_CONNECTION_LIMIT, (unsigned int) 1,
573 MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) 16,
574 MHD_OPTION_CONNECTION_MEMORY_LIMIT, (size_t) (16 * 1024),
577 if ( (daemon_handle_v6 == NULL) &&
578 (daemon_handle_v4 == NULL) )
580 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
581 _("Could not start hostlist HTTP server on port %u\n"),
582 (unsigned short) port);
583 return GNUNET_SYSERR;
586 *server_ch = &connect_handler;
587 *server_dh = &disconnect_handler;
589 if (daemon_handle_v4 != NULL)
590 hostlist_task_v4 = prepare_daemon (daemon_handle_v4);
591 if (daemon_handle_v6 != NULL)
592 hostlist_task_v6 = prepare_daemon (daemon_handle_v6);
593 response_task = GNUNET_SCHEDULER_add_now (sched,
600 * Stop server offering our hostlist.
603 GNUNET_HOSTLIST_server_stop ()
605 #if DEBUG_HOSTLIST_SERVER
606 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
607 "Hostlist server shutdown\n");
609 if (GNUNET_SCHEDULER_NO_TASK != hostlist_task_v6)
611 GNUNET_SCHEDULER_cancel (sched, hostlist_task_v6);
612 hostlist_task_v6 = GNUNET_SCHEDULER_NO_TASK;
614 if (GNUNET_SCHEDULER_NO_TASK != hostlist_task_v4)
616 GNUNET_SCHEDULER_cancel (sched, hostlist_task_v4);
617 hostlist_task_v4 = GNUNET_SCHEDULER_NO_TASK;
621 GNUNET_PEERINFO_iterate_cancel (pitr);
624 if (GNUNET_SCHEDULER_NO_TASK != response_task)
626 GNUNET_SCHEDULER_cancel (sched, response_task);
627 response_task = GNUNET_SCHEDULER_NO_TASK;
629 if (NULL != daemon_handle_v4)
631 MHD_stop_daemon (daemon_handle_v4);
632 daemon_handle_v4 = NULL;
634 if (NULL != daemon_handle_v6)
636 MHD_stop_daemon (daemon_handle_v6);
637 daemon_handle_v6 = NULL;
639 if (response != NULL)
641 MHD_destroy_response (response);
646 /* end of hostlist-server.c */