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,
147 accept_policy_callback (void *cls,
148 const struct sockaddr *addr, socklen_t addrlen)
150 return MHD_YES; /* accept all */
155 access_handler_callback (void *cls,
156 struct MHD_Connection *connection,
160 const char *upload_data,
161 unsigned int *upload_data_size, void **con_cls)
165 if (0 != strcmp (method, MHD_HTTP_METHOD_GET))
167 if (NULL == *con_cls)
170 return MHD_YES; /* send 100 continue */
172 if (*upload_data_size != 0)
173 return MHD_NO; /* do not support upload data */
174 if (response == NULL)
175 return MHD_NO; /* internal error, no response yet */
176 return MHD_queue_response (connection, MHD_HTTP_OK, response);
181 * Start server offering our hostlist.
183 * @return GNUNET_OK on success
186 GNUNET_HOSTLIST_server_start (struct GNUNET_CONFIGURATION_Handle *c,
187 struct GNUNET_SCHEDULER_Handle *s,
188 struct GNUNET_STATISTICS_Handle *st)
190 unsigned long long port;
194 if (-1 == GNUNET_CONFIGURATION_get_value_number (cfg,
198 return GNUNET_SYSERR;
199 /* FIXME: must use *external* SELECT mode since our
200 code is NOT thread safe! Integrate with scheduler! */
201 daemon_handle = MHD_start_daemon (MHD_USE_SELECT_INTERNALLY | MHD_USE_IPv6,
202 (unsigned short) port,
203 &accept_policy_callback,
205 &access_handler_callback,
207 MHD_OPTION_CONNECTION_LIMIT, 16,
208 MHD_OPTION_PER_IP_CONNECTION_LIMIT, 1,
209 MHD_OPTION_CONNECTION_TIMEOUT, 16,
210 MHD_OPTION_CONNECTION_MEMORY_LIMIT,
211 16 * 1024, MHD_OPTION_END);
212 if (daemon_handle == NULL)
214 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
215 _("Could not start hostlist HTTP server on port %u\n"),
216 (unsigned short) port);
217 return GNUNET_SYSERR;
223 * Stop server offering our hostlist.
226 GNUNET_HOSTLIST_server_stop ()
228 MHD_stop_daemon (daemon_handle);
229 daemon_handle = NULL;
232 /* end of hostlist-server.c */