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"
34 * How often should we recalculate our response to hostlist requests?
36 #define RESPONSE_UPDATE_FREQUENCY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 5)
39 * Handle to the HTTP server as provided by libmicrohttpd.
41 static struct MHD_Daemon *daemon_handle;
46 static const struct GNUNET_CONFIGURATION_Handle *cfg;
51 static struct GNUNET_SCHEDULER_Handle *sched;
56 static GNUNET_SCHEDULER_TaskIdentifier hostlist_task;
59 * Task that updates our HTTP response.
61 static GNUNET_SCHEDULER_TaskIdentifier response_task;
64 * Our canonical response.
66 static struct MHD_Response *response;
69 * Context for host processor.
80 * Task that will produce a new response object.
83 update_response (void *cls,
84 const struct GNUNET_SCHEDULER_TaskContext *tc);
88 * Function that assembles our response.
91 finish_response (struct HostSet *results)
93 struct GNUNET_TIME_Relative freq;
96 MHD_destroy_response (response);
97 response = MHD_create_response_from_data (results->size,
98 results->data, MHD_YES, MHD_NO);
99 if (daemon_handle != NULL)
101 freq = RESPONSE_UPDATE_FREQUENCY;
102 if (results->size == 0)
103 freq = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 50);
104 /* schedule next update of the response */
105 response_task = GNUNET_SCHEDULER_add_delayed (sched,
112 MHD_destroy_response (response);
115 GNUNET_free (results);
120 * Callback that processes each of the known HELLOs for the
121 * hostlist response construction.
124 host_processor (void *cls,
125 const struct GNUNET_PeerIdentity * peer,
126 const struct GNUNET_HELLO_Message *hello,
129 struct HostSet *results = cls;
135 finish_response (results);
139 s = GNUNET_HELLO_size(hello);
140 if (old + s >= GNUNET_MAX_MALLOC_CHECKED)
141 return; /* too large, skip! */
142 GNUNET_array_grow (results->data,
145 memcpy (&results->data[old], hello, s);
150 * Task that will produce a new response object.
153 update_response (void *cls,
154 const struct GNUNET_SCHEDULER_TaskContext *tc)
156 struct HostSet *results;
158 response_task = GNUNET_SCHEDULER_NO_TASK;
159 results = GNUNET_malloc(sizeof(struct HostSet));
160 GNUNET_PEERINFO_for_all (cfg, sched,
163 GNUNET_TIME_UNIT_MINUTES,
166 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
167 _("Created new hostlist response with %u bytes\n"),
173 * Hostlist access policy (very permissive, allows everything).
176 accept_policy_callback (void *cls,
177 const struct sockaddr *addr, socklen_t addrlen)
179 if (NULL == response)
181 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
182 "Received request for hostlist, but I am not yet ready; rejecting!\n");
185 return MHD_YES; /* accept all */
190 * Main request handler.
193 access_handler_callback (void *cls,
194 struct MHD_Connection *connection,
198 const char *upload_data,
199 size_t*upload_data_size, void **con_cls)
203 if (0 != strcmp (method, MHD_HTTP_METHOD_GET))
205 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
206 _("Received request for our hostlist\n"));
207 if (NULL == *con_cls)
210 return MHD_YES; /* send 100 continue */
212 if (*upload_data_size != 0)
213 return MHD_NO; /* do not support upload data */
214 if (response == NULL)
215 return MHD_NO; /* internal error, no response yet */
216 return MHD_queue_response (connection, MHD_HTTP_OK, response);
221 * Function that queries MHD's select sets and
222 * starts the task waiting for them.
225 prepare_daemon (void);
229 * Call MHD to process pending requests and then go back
230 * and schedule the next run.
233 run_daemon (void *cls,
234 const struct GNUNET_SCHEDULER_TaskContext *tc)
236 GNUNET_assert (MHD_YES == MHD_run (daemon_handle));
242 * Function that queries MHD's select sets and
243 * starts the task waiting for them.
251 struct GNUNET_NETWORK_FDSet *wrs;
252 struct GNUNET_NETWORK_FDSet *wws;
253 struct GNUNET_NETWORK_FDSet *wes;
255 unsigned long long timeout;
257 struct GNUNET_TIME_Relative tv;
262 wrs = GNUNET_NETWORK_fdset_create ();
263 wes = GNUNET_NETWORK_fdset_create ();
264 wws = GNUNET_NETWORK_fdset_create ();
266 GNUNET_assert (MHD_YES ==
267 MHD_get_fdset (daemon_handle,
272 haveto = MHD_get_timeout (daemon_handle, &timeout);
273 if (haveto == MHD_YES)
274 tv.value = (uint64_t) timeout;
276 tv = GNUNET_TIME_UNIT_FOREVER_REL;
277 GNUNET_NETWORK_fdset_copy_native (wrs, &rs, max);
278 GNUNET_NETWORK_fdset_copy_native (wws, &ws, max);
279 GNUNET_NETWORK_fdset_copy_native (wes, &es, max);
281 = GNUNET_SCHEDULER_add_select (sched,
282 GNUNET_SCHEDULER_PRIORITY_HIGH,
283 GNUNET_SCHEDULER_NO_TASK,
289 GNUNET_NETWORK_fdset_destroy (wrs);
290 GNUNET_NETWORK_fdset_destroy (wws);
291 GNUNET_NETWORK_fdset_destroy (wes);
297 * Start server offering our hostlist.
299 * @return GNUNET_OK on success
302 GNUNET_HOSTLIST_server_start (const struct GNUNET_CONFIGURATION_Handle *c,
303 struct GNUNET_SCHEDULER_Handle *s,
304 struct GNUNET_STATISTICS_Handle *st)
306 unsigned long long port;
310 if (-1 == GNUNET_CONFIGURATION_get_value_number (cfg,
314 return GNUNET_SYSERR;
315 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
316 _("Hostlist service starts on port %llu\n"),
318 daemon_handle = MHD_start_daemon (MHD_USE_IPv6,
319 (unsigned short) port,
320 &accept_policy_callback,
322 &access_handler_callback,
324 MHD_OPTION_CONNECTION_LIMIT, 16,
325 MHD_OPTION_PER_IP_CONNECTION_LIMIT, 1,
326 MHD_OPTION_CONNECTION_TIMEOUT, 16,
327 MHD_OPTION_CONNECTION_MEMORY_LIMIT,
328 16 * 1024, MHD_OPTION_END);
329 if (daemon_handle == NULL)
331 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
332 _("Could not start hostlist HTTP server on port %u\n"),
333 (unsigned short) port);
334 return GNUNET_SYSERR;
337 response_task = GNUNET_SCHEDULER_add_now (sched,
344 * Stop server offering our hostlist.
347 GNUNET_HOSTLIST_server_stop ()
349 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
350 "Hostlist server shutdown\n");
351 if (GNUNET_SCHEDULER_NO_TASK != hostlist_task)
353 GNUNET_SCHEDULER_cancel (sched, hostlist_task);
354 hostlist_task = GNUNET_SCHEDULER_NO_TASK;
356 if (GNUNET_SCHEDULER_NO_TASK != response_task)
358 GNUNET_SCHEDULER_cancel (sched, response_task);
359 response_task = GNUNET_SCHEDULER_NO_TASK;
361 if (NULL != daemon_handle)
363 MHD_stop_daemon (daemon_handle);
364 daemon_handle = NULL;
366 if (response != NULL)
368 MHD_destroy_response (response);
373 /* end of hostlist-server.c */