2 This file is part of GNUnet.
3 (C) 2008 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 * Our canonical response.
61 static struct MHD_Response *response;
64 * Context for host processor.
75 * Task that will produce a new response object.
78 update_response (void *cls,
79 const struct GNUNET_SCHEDULER_TaskContext *tc);
83 * Function that assembles our response.
86 finish_response (struct HostSet *results)
89 MHD_destroy_response (response);
90 response = MHD_create_response_from_data (results->size,
91 results->data, MHD_YES, MHD_NO);
92 GNUNET_free (results);
93 /* schedule next update of the response */
94 GNUNET_SCHEDULER_add_delayed (sched,
96 GNUNET_SCHEDULER_PRIORITY_IDLE,
97 GNUNET_SCHEDULER_NO_TASK,
98 RESPONSE_UPDATE_FREQUENCY,
105 * Callback that processes each of the known HELLOs for the
106 * hostlist response construction.
109 host_processor (void *cls,
110 const struct GNUNET_PeerIdentity * peer,
111 const struct GNUNET_HELLO_Message *hello,
114 struct HostSet *results = cls;
120 finish_response (results);
124 s = GNUNET_HELLO_size(hello);
125 if (old + s >= GNUNET_MAX_MALLOC_CHECKED)
126 return; /* too large, skip! */
127 GNUNET_array_grow (results->data,
130 memcpy (&results->data[old], hello, s);
135 * Task that will produce a new response object.
138 update_response (void *cls,
139 const struct GNUNET_SCHEDULER_TaskContext *tc)
141 struct HostSet *results;
143 results = GNUNET_malloc(sizeof(struct HostSet));
144 GNUNET_PEERINFO_for_all (cfg, sched,
147 GNUNET_TIME_UNIT_MINUTES,
154 * Hostlist access policy (very permissive, allows everything).
157 accept_policy_callback (void *cls,
158 const struct sockaddr *addr, socklen_t addrlen)
160 return MHD_YES; /* accept all */
165 * Main request handler.
168 access_handler_callback (void *cls,
169 struct MHD_Connection *connection,
173 const char *upload_data,
174 size_t*upload_data_size, void **con_cls)
178 if (0 != strcmp (method, MHD_HTTP_METHOD_GET))
180 if (NULL == *con_cls)
183 return MHD_YES; /* send 100 continue */
185 if (*upload_data_size != 0)
186 return MHD_NO; /* do not support upload data */
187 if (response == NULL)
188 return MHD_NO; /* internal error, no response yet */
189 return MHD_queue_response (connection, MHD_HTTP_OK, response);
194 * Function that queries MHD's select sets and
195 * starts the task waiting for them.
198 prepare_daemon (void);
202 * Call MHD to process pending requests and then go back
203 * and schedule the next run.
206 run_daemon (void *cls,
207 const struct GNUNET_SCHEDULER_TaskContext *tc)
209 GNUNET_assert (MHD_YES == MHD_run (daemon_handle));
215 * Function that queries MHD's select sets and
216 * starts the task waiting for them.
224 struct GNUNET_NETWORK_FDSet *wrs;
225 struct GNUNET_NETWORK_FDSet *wws;
226 struct GNUNET_NETWORK_FDSet *wes;
228 unsigned long long timeout;
230 struct GNUNET_TIME_Relative tv;
235 wrs = GNUNET_NETWORK_fdset_create ();
236 wes = GNUNET_NETWORK_fdset_create ();
237 wws = GNUNET_NETWORK_fdset_create ();
239 GNUNET_assert (MHD_YES ==
240 MHD_get_fdset (daemon_handle,
245 haveto = MHD_get_timeout (daemon_handle, &timeout);
246 if (haveto == MHD_YES)
247 tv.value = (uint64_t) timeout;
249 tv = GNUNET_TIME_UNIT_FOREVER_REL;
250 GNUNET_NETWORK_fdset_copy_native (wrs, &rs, max);
251 GNUNET_NETWORK_fdset_copy_native (wws, &ws, max);
252 GNUNET_NETWORK_fdset_copy_native (wes, &es, max);
254 = GNUNET_SCHEDULER_add_select (sched,
256 GNUNET_SCHEDULER_PRIORITY_HIGH,
257 GNUNET_SCHEDULER_NO_TASK,
263 GNUNET_NETWORK_fdset_destroy (wrs);
264 GNUNET_NETWORK_fdset_destroy (wws);
265 GNUNET_NETWORK_fdset_destroy (wes);
271 * Start server offering our hostlist.
273 * @return GNUNET_OK on success
276 GNUNET_HOSTLIST_server_start (const struct GNUNET_CONFIGURATION_Handle *c,
277 struct GNUNET_SCHEDULER_Handle *s,
278 struct GNUNET_STATISTICS_Handle *st)
280 unsigned long long port;
284 if (-1 == GNUNET_CONFIGURATION_get_value_number (cfg,
288 return GNUNET_SYSERR;
289 daemon_handle = MHD_start_daemon (MHD_USE_IPv6,
290 (unsigned short) port,
291 &accept_policy_callback,
293 &access_handler_callback,
295 MHD_OPTION_CONNECTION_LIMIT, 16,
296 MHD_OPTION_PER_IP_CONNECTION_LIMIT, 1,
297 MHD_OPTION_CONNECTION_TIMEOUT, 16,
298 MHD_OPTION_CONNECTION_MEMORY_LIMIT,
299 16 * 1024, MHD_OPTION_END);
300 if (daemon_handle == NULL)
302 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
303 _("Could not start hostlist HTTP server on port %u\n"),
304 (unsigned short) port);
305 return GNUNET_SYSERR;
312 * Stop server offering our hostlist.
315 GNUNET_HOSTLIST_server_stop ()
317 GNUNET_SCHEDULER_cancel (sched, hostlist_task);
318 hostlist_task = GNUNET_SCHEDULER_NO_TASK;
319 MHD_stop_daemon (daemon_handle);
320 daemon_handle = NULL;
323 /* end of hostlist-server.c */