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 struct GNUNET_CONFIGURATION_Handle *cfg;
51 static struct GNUNET_SCHEDULER_Handle *sched;
54 * Our canonical response.
56 static struct MHD_Response *response;
59 * Context for host processor.
70 * Task that will produce a new response object.
73 update_response (void *cls,
74 const struct GNUNET_SCHEDULER_TaskContext *tc);
78 * Function that assembles our response.
81 finish_response (struct HostSet *results)
84 MHD_destroy_response (response);
85 response = MHD_create_response_from_data (results->size,
86 results->data, MHD_YES, MHD_NO);
87 GNUNET_free (results);
88 /* schedule next update of the response */
89 GNUNET_SCHEDULER_add_delayed (sched,
91 GNUNET_SCHEDULER_PRIORITY_IDLE,
92 GNUNET_SCHEDULER_NO_PREREQUISITE_TASK,
93 RESPONSE_UPDATE_FREQUENCY,
100 * Callback that processes each of the known HELLOs for the
101 * hostlist response construction.
104 host_processor (void *cls,
105 const struct GNUNET_PeerIdentity * peer,
106 const struct GNUNET_HELLO_Message *hello,
109 struct HostSet *results = cls;
114 finish_response (results);
116 s = GNUNET_HELLO_size(hello);
117 if (old + s >= GNUNET_MAX_MALLOC_CHECKED)
118 return; /* too large, skip! */
119 GNUNET_array_grow (results->data,
122 memcpy (&results->data[old], hello, s);
127 * Task that will produce a new response object.
130 update_response (void *cls,
131 const struct GNUNET_SCHEDULER_TaskContext *tc)
133 struct HostSet *results;
135 results = GNUNET_malloc(sizeof(struct HostSet));
136 GNUNET_PEERINFO_for_all (cfg, sched,
139 GNUNET_TIME_UNIT_MINUTES,
146 * Hostlist access policy (very permissive, allows everything).
149 accept_policy_callback (void *cls,
150 const struct sockaddr *addr, socklen_t addrlen)
152 return MHD_YES; /* accept all */
157 * Main request handler.
160 access_handler_callback (void *cls,
161 struct MHD_Connection *connection,
165 const char *upload_data,
166 unsigned int *upload_data_size, void **con_cls)
170 if (0 != strcmp (method, MHD_HTTP_METHOD_GET))
172 if (NULL == *con_cls)
175 return MHD_YES; /* send 100 continue */
177 if (*upload_data_size != 0)
178 return MHD_NO; /* do not support upload data */
179 if (response == NULL)
180 return MHD_NO; /* internal error, no response yet */
181 return MHD_queue_response (connection, MHD_HTTP_OK, response);
186 * Function that queries MHD's select sets and
187 * starts the task waiting for them.
190 prepare_daemon (void);
194 run_daemon (void *cls,
195 const struct GNUNET_SCHEDULER_TaskContext *tc)
197 GNUNET_assert (MHD_YES == MHD_run (daemon_handle));
203 * Function that queries MHD's select sets and
204 * starts the task waiting for them.
213 unsigned long long timeout;
215 struct GNUNET_TIME_Relative tv;
221 GNUNET_assert (MHD_YES ==
222 MHD_get_fdset (daemon_handle,
227 haveto = MHD_get_timeout (daemon_handle, &timeout);
228 if (haveto == MHD_YES)
229 tv.value = (uint64_t) timeout;
231 tv = GNUNET_TIME_UNIT_FOREVER_REL;
232 GNUNET_SCHEDULER_add_select (sched,
234 GNUNET_SCHEDULER_PRIORITY_HIGH,
235 GNUNET_SCHEDULER_NO_PREREQUISITE_TASK,
247 * Start server offering our hostlist.
249 * @return GNUNET_OK on success
252 GNUNET_HOSTLIST_server_start (struct GNUNET_CONFIGURATION_Handle *c,
253 struct GNUNET_SCHEDULER_Handle *s,
254 struct GNUNET_STATISTICS_Handle *st)
256 unsigned long long port;
260 if (-1 == GNUNET_CONFIGURATION_get_value_number (cfg,
264 return GNUNET_SYSERR;
265 daemon_handle = MHD_start_daemon (MHD_USE_IPv6,
266 (unsigned short) port,
267 &accept_policy_callback,
269 &access_handler_callback,
271 MHD_OPTION_CONNECTION_LIMIT, 16,
272 MHD_OPTION_PER_IP_CONNECTION_LIMIT, 1,
273 MHD_OPTION_CONNECTION_TIMEOUT, 16,
274 MHD_OPTION_CONNECTION_MEMORY_LIMIT,
275 16 * 1024, MHD_OPTION_END);
276 if (daemon_handle == NULL)
278 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
279 _("Could not start hostlist HTTP server on port %u\n"),
280 (unsigned short) port);
281 return GNUNET_SYSERR;
288 * Stop server offering our hostlist.
291 GNUNET_HOSTLIST_server_stop ()
293 MHD_stop_daemon (daemon_handle);
294 daemon_handle = NULL;
297 /* end of hostlist-server.c */