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"
33 #define DEBUG_HOSTLIST_SERVER GNUNET_NO
36 * How often should we recalculate our response to hostlist requests?
38 #define RESPONSE_UPDATE_FREQUENCY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 5)
41 * Handle to the HTTP server as provided by libmicrohttpd for IPv6.
43 static struct MHD_Daemon *daemon_handle_v6;
46 * Handle to the HTTP server as provided by libmicrohttpd for IPv4.
48 static struct MHD_Daemon *daemon_handle_v4;
53 static const struct GNUNET_CONFIGURATION_Handle *cfg;
58 static struct GNUNET_SCHEDULER_Handle *sched;
61 * For keeping statistics.
63 static struct GNUNET_STATISTICS_Handle *stats;
66 * Our primary task for IPv4.
68 static GNUNET_SCHEDULER_TaskIdentifier hostlist_task_v4;
71 * Our primary task for IPv6.
73 static GNUNET_SCHEDULER_TaskIdentifier hostlist_task_v6;
76 * Task that updates our HTTP response.
78 static GNUNET_SCHEDULER_TaskIdentifier response_task;
81 * Our canonical response.
83 static struct MHD_Response *response;
86 * NULL if we are not currenlty iterating over peer information.
88 static struct GNUNET_PEERINFO_IteratorContext *pitr;
91 * Context for host processor.
102 * Task that will produce a new response object.
105 update_response (void *cls,
106 const struct GNUNET_SCHEDULER_TaskContext *tc);
110 * Function that assembles our response.
113 finish_response (struct HostSet *results)
115 struct GNUNET_TIME_Relative freq;
117 if (response != NULL)
118 MHD_destroy_response (response);
119 #if DEBUG_HOSTLIST_SERVER
120 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
121 "Creating hostlist response with %u bytes\n",
122 (unsigned int) results->size);
124 response = MHD_create_response_from_data (results->size,
125 results->data, MHD_YES, MHD_NO);
126 if ( (daemon_handle_v4 != NULL) ||
127 (daemon_handle_v6 != NULL) )
129 freq = RESPONSE_UPDATE_FREQUENCY;
130 if (results->size == 0)
131 freq = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 250);
132 /* schedule next update of the response */
133 response_task = GNUNET_SCHEDULER_add_delayed (sched,
140 /* already past shutdown */
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);
204 has_addr = GNUNET_NO;
205 GNUNET_HELLO_iterate_addresses (hello,
209 if (GNUNET_NO == has_addr)
211 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
212 "HELLO for peer `%4s' has no address, not suitable for hostlist!\n",
214 GNUNET_STATISTICS_update (stats,
215 gettext_noop("HELLOs without addresses encountered (ignored)"),
221 s = GNUNET_HELLO_size(hello);
222 #if DEBUG_HOSTLIST_SERVER
223 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
224 "Received %u bytes of `%s' from peer `%s' for hostlist.\n",
229 if (old + s >= GNUNET_MAX_MALLOC_CHECKED)
231 GNUNET_STATISTICS_update (stats,
232 gettext_noop("bytes not included in hostlist (size limit)"),
235 return; /* too large, skip! */
237 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
238 "Adding peer `%s' to hostlist (%u bytes)\n",
241 GNUNET_array_grow (results->data,
244 memcpy (&results->data[old], hello, s);
249 * Task that will produce a new response object.
252 update_response (void *cls,
253 const struct GNUNET_SCHEDULER_TaskContext *tc)
255 struct HostSet *results;
257 response_task = GNUNET_SCHEDULER_NO_TASK;
258 results = GNUNET_malloc(sizeof(struct HostSet));
259 pitr = GNUNET_PEERINFO_iterate (cfg, sched,
262 GNUNET_TIME_UNIT_MINUTES,
269 * Hostlist access policy (very permissive, allows everything).
272 accept_policy_callback (void *cls,
273 const struct sockaddr *addr, socklen_t addrlen)
275 if (NULL == response)
277 #if DEBUG_HOSTLIST_SERVER
278 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
279 "Received request for hostlist, but I am not yet ready; rejecting!\n");
283 return MHD_YES; /* accept all */
288 * Main request handler.
291 access_handler_callback (void *cls,
292 struct MHD_Connection *connection,
296 const char *upload_data,
297 size_t*upload_data_size, void **con_cls)
301 if (0 != strcmp (method, MHD_HTTP_METHOD_GET))
303 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
304 _("Refusing `%s' request to hostlist server\n"),
306 GNUNET_STATISTICS_update (stats,
307 gettext_noop("hostlist requests refused (not HTTP GET)"),
312 if (NULL == *con_cls)
315 #if DEBUG_HOSTLIST_SERVER
316 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
317 _("Sending 100 CONTINUE reply\n"));
319 return MHD_YES; /* send 100 continue */
321 if (*upload_data_size != 0)
323 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
324 _("Refusing `%s' request with %llu bytes of upload data\n"),
326 (unsigned long long) *upload_data_size);
327 GNUNET_STATISTICS_update (stats,
328 gettext_noop("hostlist requests refused (upload data)"),
331 return MHD_NO; /* do not support upload data */
333 if (response == NULL)
335 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
336 _("Could not handle hostlist request since I do not have a response yet\n"));
337 GNUNET_STATISTICS_update (stats,
338 gettext_noop("hostlist requests refused (not ready)"),
341 return MHD_NO; /* internal error, no response yet */
343 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
344 _("Received request for our hostlist\n"));
345 GNUNET_STATISTICS_update (stats,
346 gettext_noop("hostlist requests processed"),
349 return MHD_queue_response (connection, MHD_HTTP_OK, response);
354 * Function that queries MHD's select sets and
355 * starts the task waiting for them.
357 static GNUNET_SCHEDULER_TaskIdentifier
358 prepare_daemon (struct MHD_Daemon *daemon_handle);
361 * Call MHD to process pending requests and then go back
362 * and schedule the next run.
365 run_daemon (void *cls,
366 const struct GNUNET_SCHEDULER_TaskContext *tc)
368 struct MHD_Daemon *daemon_handle = cls;
370 if (daemon_handle == daemon_handle_v4)
371 hostlist_task_v4 = GNUNET_SCHEDULER_NO_TASK;
373 hostlist_task_v6 = GNUNET_SCHEDULER_NO_TASK;
375 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
377 GNUNET_assert (MHD_YES == MHD_run (daemon_handle));
378 if (daemon_handle == daemon_handle_v4)
379 hostlist_task_v4 = prepare_daemon (daemon_handle);
381 hostlist_task_v6 = prepare_daemon (daemon_handle);
386 * Function that queries MHD's select sets and
387 * starts the task waiting for them.
389 static GNUNET_SCHEDULER_TaskIdentifier
390 prepare_daemon (struct MHD_Daemon *daemon_handle)
392 GNUNET_SCHEDULER_TaskIdentifier ret;
396 struct GNUNET_NETWORK_FDSet *wrs;
397 struct GNUNET_NETWORK_FDSet *wws;
398 struct GNUNET_NETWORK_FDSet *wes;
400 unsigned long long timeout;
402 struct GNUNET_TIME_Relative tv;
407 wrs = GNUNET_NETWORK_fdset_create ();
408 wes = GNUNET_NETWORK_fdset_create ();
409 wws = GNUNET_NETWORK_fdset_create ();
411 GNUNET_assert (MHD_YES ==
412 MHD_get_fdset (daemon_handle,
417 haveto = MHD_get_timeout (daemon_handle, &timeout);
418 if (haveto == MHD_YES)
419 tv.value = (uint64_t) timeout;
421 tv = GNUNET_TIME_UNIT_FOREVER_REL;
422 GNUNET_NETWORK_fdset_copy_native (wrs, &rs, max);
423 GNUNET_NETWORK_fdset_copy_native (wws, &ws, max);
424 GNUNET_NETWORK_fdset_copy_native (wes, &es, max);
425 ret = GNUNET_SCHEDULER_add_select (sched,
426 GNUNET_SCHEDULER_PRIORITY_HIGH,
427 GNUNET_SCHEDULER_NO_TASK,
433 GNUNET_NETWORK_fdset_destroy (wrs);
434 GNUNET_NETWORK_fdset_destroy (wws);
435 GNUNET_NETWORK_fdset_destroy (wes);
442 * Start server offering our hostlist.
444 * @return GNUNET_OK on success
447 GNUNET_HOSTLIST_server_start (const struct GNUNET_CONFIGURATION_Handle *c,
448 struct GNUNET_SCHEDULER_Handle *s,
449 struct GNUNET_STATISTICS_Handle *st)
451 unsigned long long port;
456 if (-1 == GNUNET_CONFIGURATION_get_value_number (cfg,
460 return GNUNET_SYSERR;
461 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
462 _("Hostlist service starts on port %llu\n"),
464 daemon_handle_v6 = MHD_start_daemon (MHD_USE_IPv6
465 #if DEBUG_HOSTLIST_SERVER
469 (unsigned short) port,
470 &accept_policy_callback,
472 &access_handler_callback,
474 MHD_OPTION_CONNECTION_LIMIT, (unsigned int) 16,
475 MHD_OPTION_PER_IP_CONNECTION_LIMIT, (unsigned int) 1,
476 MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) 16,
477 MHD_OPTION_CONNECTION_MEMORY_LIMIT, (size_t) (16 * 1024),
479 daemon_handle_v4 = MHD_start_daemon (MHD_NO_FLAG
480 #if DEBUG_HOSTLIST_SERVER
484 (unsigned short) port,
485 &accept_policy_callback,
487 &access_handler_callback,
489 MHD_OPTION_CONNECTION_LIMIT, (unsigned int) 16,
490 MHD_OPTION_PER_IP_CONNECTION_LIMIT, (unsigned int) 1,
491 MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) 16,
492 MHD_OPTION_CONNECTION_MEMORY_LIMIT, (size_t) (16 * 1024),
495 if ( (daemon_handle_v6 == NULL) &&
496 (daemon_handle_v4 == NULL) )
498 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
499 _("Could not start hostlist HTTP server on port %u\n"),
500 (unsigned short) port);
501 return GNUNET_SYSERR;
503 if (daemon_handle_v4 != NULL)
504 hostlist_task_v4 = prepare_daemon (daemon_handle_v4);
505 if (daemon_handle_v6 != NULL)
506 hostlist_task_v6 = prepare_daemon (daemon_handle_v6);
507 response_task = GNUNET_SCHEDULER_add_now (sched,
514 * Stop server offering our hostlist.
517 GNUNET_HOSTLIST_server_stop ()
519 #if DEBUG_HOSTLIST_SERVER
520 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
521 "Hostlist server shutdown\n");
523 if (GNUNET_SCHEDULER_NO_TASK != hostlist_task_v6)
525 GNUNET_SCHEDULER_cancel (sched, hostlist_task_v6);
526 hostlist_task_v6 = GNUNET_SCHEDULER_NO_TASK;
528 if (GNUNET_SCHEDULER_NO_TASK != hostlist_task_v4)
530 GNUNET_SCHEDULER_cancel (sched, hostlist_task_v4);
531 hostlist_task_v4 = GNUNET_SCHEDULER_NO_TASK;
535 GNUNET_PEERINFO_iterate_cancel (pitr);
538 if (GNUNET_SCHEDULER_NO_TASK != response_task)
540 GNUNET_SCHEDULER_cancel (sched, response_task);
541 response_task = GNUNET_SCHEDULER_NO_TASK;
543 if (NULL != daemon_handle_v4)
545 MHD_stop_daemon (daemon_handle_v4);
546 daemon_handle_v4 = NULL;
548 if (NULL != daemon_handle_v6)
550 MHD_stop_daemon (daemon_handle_v6);
551 daemon_handle_v6 = NULL;
553 if (response != NULL)
555 MHD_destroy_response (response);
560 /* end of hostlist-server.c */