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 * Our primary task for IPv4.
63 static GNUNET_SCHEDULER_TaskIdentifier hostlist_task_v4;
66 * Our primary task for IPv6.
68 static GNUNET_SCHEDULER_TaskIdentifier hostlist_task_v6;
71 * Task that updates our HTTP response.
73 static GNUNET_SCHEDULER_TaskIdentifier response_task;
76 * Our canonical response.
78 static struct MHD_Response *response;
81 * Context for host processor.
92 * Task that will produce a new response object.
95 update_response (void *cls,
96 const struct GNUNET_SCHEDULER_TaskContext *tc);
100 * Function that assembles our response.
103 finish_response (struct HostSet *results)
105 struct GNUNET_TIME_Relative freq;
107 if (response != NULL)
108 MHD_destroy_response (response);
109 #if DEBUG_HOSTLIST_SERVER
110 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
111 "Creating hostlist response with %u bytes\n",
112 (unsigned int) results->size);
114 response = MHD_create_response_from_data (results->size,
115 results->data, MHD_YES, MHD_NO);
116 if ( (daemon_handle_v4 != NULL) ||
117 (daemon_handle_v6 != NULL) )
119 freq = RESPONSE_UPDATE_FREQUENCY;
120 if (results->size == 0)
121 freq = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 50);
122 /* schedule next update of the response */
123 response_task = GNUNET_SCHEDULER_add_delayed (sched,
130 /* already past shutdown */
131 MHD_destroy_response (response);
134 GNUNET_free (results);
139 * Callback that processes each of the known HELLOs for the
140 * hostlist response construction.
143 host_processor (void *cls,
144 const struct GNUNET_PeerIdentity * peer,
145 const struct GNUNET_HELLO_Message *hello,
148 struct HostSet *results = cls;
154 finish_response (results);
158 s = GNUNET_HELLO_size(hello);
159 #if DEBUG_HOSTLIST_SERVER
160 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
161 "Received %u bytes of `%s' from peer `%s' for hostlist.\n",
166 if (old + s >= GNUNET_MAX_MALLOC_CHECKED)
167 return; /* too large, skip! */
168 GNUNET_array_grow (results->data,
171 memcpy (&results->data[old], hello, s);
176 * Task that will produce a new response object.
179 update_response (void *cls,
180 const struct GNUNET_SCHEDULER_TaskContext *tc)
182 struct HostSet *results;
184 response_task = GNUNET_SCHEDULER_NO_TASK;
185 results = GNUNET_malloc(sizeof(struct HostSet));
186 GNUNET_PEERINFO_for_all (cfg, sched,
189 GNUNET_TIME_UNIT_MINUTES,
196 * Hostlist access policy (very permissive, allows everything).
199 accept_policy_callback (void *cls,
200 const struct sockaddr *addr, socklen_t addrlen)
202 if (NULL == response)
204 #if DEBUG_HOSTLIST_SERVER
205 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
206 "Received request for hostlist, but I am not yet ready; rejecting!\n");
210 return MHD_YES; /* accept all */
215 * Main request handler.
218 access_handler_callback (void *cls,
219 struct MHD_Connection *connection,
223 const char *upload_data,
224 size_t*upload_data_size, void **con_cls)
228 if (0 != strcmp (method, MHD_HTTP_METHOD_GET))
230 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
231 _("Refusing `%s' request to hostlist server\n"),
235 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
236 _("Received request for our hostlist\n"));
237 if (NULL == *con_cls)
240 #if DEBUG_HOSTLIST_SERVER
241 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
242 _("Sending 100 CONTINUE reply\n"));
244 return MHD_YES; /* send 100 continue */
246 if (*upload_data_size != 0)
248 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
249 _("Refusing `%s' request with %llu bytes of upload data\n"),
251 (unsigned long long) *upload_data_size);
252 return MHD_NO; /* do not support upload data */
254 if (response == NULL)
256 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
257 _("Could not handle hostlist request since I do not have a response yet\n"));
258 return MHD_NO; /* internal error, no response yet */
260 return MHD_queue_response (connection, MHD_HTTP_OK, response);
265 * Function that queries MHD's select sets and
266 * starts the task waiting for them.
268 static GNUNET_SCHEDULER_TaskIdentifier
269 prepare_daemon (struct MHD_Daemon *daemon_handle);
272 * Call MHD to process pending requests and then go back
273 * and schedule the next run.
276 run_daemon (void *cls,
277 const struct GNUNET_SCHEDULER_TaskContext *tc)
279 struct MHD_Daemon *daemon_handle = cls;
281 if (daemon_handle == daemon_handle_v4)
282 hostlist_task_v4 = GNUNET_SCHEDULER_NO_TASK;
284 hostlist_task_v6 = GNUNET_SCHEDULER_NO_TASK;
286 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
288 GNUNET_assert (MHD_YES == MHD_run (daemon_handle));
289 if (daemon_handle == daemon_handle_v4)
290 hostlist_task_v4 = prepare_daemon (daemon_handle);
292 hostlist_task_v6 = prepare_daemon (daemon_handle);
297 * Function that queries MHD's select sets and
298 * starts the task waiting for them.
300 static GNUNET_SCHEDULER_TaskIdentifier
301 prepare_daemon (struct MHD_Daemon *daemon_handle)
303 GNUNET_SCHEDULER_TaskIdentifier ret;
307 struct GNUNET_NETWORK_FDSet *wrs;
308 struct GNUNET_NETWORK_FDSet *wws;
309 struct GNUNET_NETWORK_FDSet *wes;
311 unsigned long long timeout;
313 struct GNUNET_TIME_Relative tv;
318 wrs = GNUNET_NETWORK_fdset_create ();
319 wes = GNUNET_NETWORK_fdset_create ();
320 wws = GNUNET_NETWORK_fdset_create ();
322 GNUNET_assert (MHD_YES ==
323 MHD_get_fdset (daemon_handle,
328 haveto = MHD_get_timeout (daemon_handle, &timeout);
329 if (haveto == MHD_YES)
330 tv.value = (uint64_t) timeout;
332 tv = GNUNET_TIME_UNIT_FOREVER_REL;
333 GNUNET_NETWORK_fdset_copy_native (wrs, &rs, max);
334 GNUNET_NETWORK_fdset_copy_native (wws, &ws, max);
335 GNUNET_NETWORK_fdset_copy_native (wes, &es, max);
336 ret = GNUNET_SCHEDULER_add_select (sched,
337 GNUNET_SCHEDULER_PRIORITY_HIGH,
338 GNUNET_SCHEDULER_NO_TASK,
344 GNUNET_NETWORK_fdset_destroy (wrs);
345 GNUNET_NETWORK_fdset_destroy (wws);
346 GNUNET_NETWORK_fdset_destroy (wes);
353 * Start server offering our hostlist.
355 * @return GNUNET_OK on success
358 GNUNET_HOSTLIST_server_start (const struct GNUNET_CONFIGURATION_Handle *c,
359 struct GNUNET_SCHEDULER_Handle *s,
360 struct GNUNET_STATISTICS_Handle *st)
362 unsigned long long port;
366 if (-1 == GNUNET_CONFIGURATION_get_value_number (cfg,
370 return GNUNET_SYSERR;
371 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
372 _("Hostlist service starts on port %llu\n"),
374 daemon_handle_v6 = MHD_start_daemon (MHD_USE_IPv6
375 #if DEBUG_HOSTLIST_SERVER
379 (unsigned short) port,
380 &accept_policy_callback,
382 &access_handler_callback,
384 MHD_OPTION_CONNECTION_LIMIT, (unsigned int) 16,
385 MHD_OPTION_PER_IP_CONNECTION_LIMIT, (unsigned int) 1,
386 MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) 16,
387 MHD_OPTION_CONNECTION_MEMORY_LIMIT, (size_t) (16 * 1024),
389 daemon_handle_v4 = MHD_start_daemon (MHD_NO_FLAG
390 #if DEBUG_HOSTLIST_SERVER
394 (unsigned short) port,
395 &accept_policy_callback,
397 &access_handler_callback,
399 MHD_OPTION_CONNECTION_LIMIT, (unsigned int) 16,
400 MHD_OPTION_PER_IP_CONNECTION_LIMIT, (unsigned int) 1,
401 MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) 16,
402 MHD_OPTION_CONNECTION_MEMORY_LIMIT, (size_t) (16 * 1024),
405 if ( (daemon_handle_v6 == NULL) &&
406 (daemon_handle_v4 == NULL) )
408 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
409 _("Could not start hostlist HTTP server on port %u\n"),
410 (unsigned short) port);
411 return GNUNET_SYSERR;
413 if (daemon_handle_v4 != NULL)
414 hostlist_task_v4 = prepare_daemon (daemon_handle_v4);
415 if (daemon_handle_v6 != NULL)
416 hostlist_task_v6 = prepare_daemon (daemon_handle_v6);
417 response_task = GNUNET_SCHEDULER_add_now (sched,
424 * Stop server offering our hostlist.
427 GNUNET_HOSTLIST_server_stop ()
429 #if DEBUG_HOSTLIST_SERVER
430 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
431 "Hostlist server shutdown\n");
433 if (GNUNET_SCHEDULER_NO_TASK != hostlist_task_v6)
435 GNUNET_SCHEDULER_cancel (sched, hostlist_task_v6);
436 hostlist_task_v6 = GNUNET_SCHEDULER_NO_TASK;
438 if (GNUNET_SCHEDULER_NO_TASK != hostlist_task_v4)
440 GNUNET_SCHEDULER_cancel (sched, hostlist_task_v4);
441 hostlist_task_v4 = GNUNET_SCHEDULER_NO_TASK;
443 if (GNUNET_SCHEDULER_NO_TASK != response_task)
445 GNUNET_SCHEDULER_cancel (sched, response_task);
446 response_task = GNUNET_SCHEDULER_NO_TASK;
448 if (NULL != daemon_handle_v4)
450 MHD_stop_daemon (daemon_handle_v4);
451 daemon_handle_v4 = NULL;
453 if (NULL != daemon_handle_v6)
455 MHD_stop_daemon (daemon_handle_v6);
456 daemon_handle_v6 = NULL;
458 if (response != NULL)
460 MHD_destroy_response (response);
465 /* end of hostlist-server.c */