5cc26c409519632c4498c4bb658271ae8b7c3b9c
[oweals/gnunet.git] / src / hostlist / hostlist-server.c
1 /*
2      This file is part of GNUnet.
3      (C) 2008 Christian Grothoff (and other contributing authors)
4
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.
9
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.
14
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.
19 */
20
21 /**
22  * @file hostlist/hostlist-server.c
23  * @author Christian Grothoff
24  * @brief application to provide an integrated hostlist HTTP server
25  */
26
27 #include "platform.h"
28 #include <microhttpd.h>
29 #include "hostlist-server.h"
30 #include "gnunet_hello_lib.h"
31 #include "gnunet_peerinfo_service.h"
32
33 /**
34  * How often should we recalculate our response to hostlist requests?
35  */
36 #define RESPONSE_UPDATE_FREQUENCY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 5)
37
38 /**
39  * Handle to the HTTP server as provided by libmicrohttpd.
40  */
41 static struct MHD_Daemon *daemon_handle;
42
43 /**
44  * Our configuration.
45  */
46 static const struct GNUNET_CONFIGURATION_Handle *cfg;
47
48 /**
49  * Our scheduler.
50  */
51 static struct GNUNET_SCHEDULER_Handle *sched;
52
53 /**
54  * Our primary task.
55  */
56 static GNUNET_SCHEDULER_TaskIdentifier hostlist_task;
57
58 /**
59  * Our canonical response.
60  */
61 static struct MHD_Response *response;
62
63 /**
64  * Context for host processor.
65  */
66 struct HostSet
67 {
68   unsigned int size;
69
70   char *data;
71 };
72
73
74 /**
75  * Task that will produce a new response object.
76  */
77 static void
78 update_response (void *cls,
79                  const struct GNUNET_SCHEDULER_TaskContext *tc);
80
81
82 /**
83  * Function that assembles our response.
84  */
85 static void
86 finish_response (struct HostSet *results)
87 {
88   if (response != NULL)
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,
95                                 GNUNET_NO,
96                                 GNUNET_SCHEDULER_PRIORITY_IDLE,
97                                 GNUNET_SCHEDULER_NO_TASK,
98                                 RESPONSE_UPDATE_FREQUENCY,
99                                 &update_response,
100                                 NULL);
101 }
102
103
104 /**
105  * Callback that processes each of the known HELLOs for the
106  * hostlist response construction.
107  */
108 static void
109 host_processor (void *cls,
110                 const struct GNUNET_PeerIdentity * peer,
111                 const struct GNUNET_HELLO_Message *hello,
112                 uint32_t trust)
113 {
114   struct HostSet *results = cls;
115   size_t old;
116   size_t s;
117   
118   if (peer == NULL)
119     {
120       finish_response (results);
121       return;
122     }
123   old = results->size;
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,
128                      results->size,
129                      old + s);
130   memcpy (&results->data[old], hello, s);
131 }
132
133
134 /**
135  * Task that will produce a new response object.
136  */
137 static void
138 update_response (void *cls,
139                  const struct GNUNET_SCHEDULER_TaskContext *tc)
140 {
141   struct HostSet *results;
142
143   results = GNUNET_malloc(sizeof(struct HostSet));
144   GNUNET_PEERINFO_for_all (cfg, sched, 
145                            NULL,
146                            0, 
147                            GNUNET_TIME_UNIT_MINUTES,
148                            &host_processor,
149                            results);
150 }
151
152
153 /**
154  * Hostlist access policy (very permissive, allows everything).
155  */
156 static int
157 accept_policy_callback (void *cls,
158                         const struct sockaddr *addr, socklen_t addrlen)
159 {
160   return MHD_YES;               /* accept all */
161 }
162
163
164 /**
165  * Main request handler.
166  */
167 static int
168 access_handler_callback (void *cls,
169                          struct MHD_Connection *connection,
170                          const char *url,
171                          const char *method,
172                          const char *version,
173                          const char *upload_data,
174                          size_t*upload_data_size, void **con_cls)
175 {
176   static int dummy;
177   
178   if (0 != strcmp (method, MHD_HTTP_METHOD_GET))
179     return MHD_NO;
180   if (NULL == *con_cls)
181     {
182       (*con_cls) = &dummy;
183       return MHD_YES;           /* send 100 continue */
184     }
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);
190 }
191
192
193 /**
194  * Function that queries MHD's select sets and
195  * starts the task waiting for them.
196  */
197 static void 
198 prepare_daemon (void);
199
200
201 /**
202  * Call MHD to process pending requests and then go back
203  * and schedule the next run.
204  */
205 static void
206 run_daemon (void *cls,
207             const struct GNUNET_SCHEDULER_TaskContext *tc)
208 {
209   GNUNET_assert (MHD_YES == MHD_run (daemon_handle));
210   prepare_daemon ();
211 }
212
213
214 /**
215  * Function that queries MHD's select sets and
216  * starts the task waiting for them.
217  */
218 static void 
219 prepare_daemon ()
220 {
221   fd_set rs;
222   fd_set ws;
223   fd_set es;
224   int max;
225   unsigned long long timeout;
226   int haveto;
227   struct GNUNET_TIME_Relative tv;
228   
229   FD_ZERO(&rs);
230   FD_ZERO(&ws);
231   FD_ZERO(&es);
232   max = -1;
233   GNUNET_assert (MHD_YES ==
234                  MHD_get_fdset (daemon_handle,
235                                 &rs,
236                                 &ws,
237                                 &es,
238                                 &max));
239   haveto = MHD_get_timeout (daemon_handle, &timeout);
240   if (haveto == MHD_YES)
241     tv.value = (uint64_t) timeout;
242   else
243     tv = GNUNET_TIME_UNIT_FOREVER_REL;
244   hostlist_task 
245     = GNUNET_SCHEDULER_add_select (sched,
246                                    GNUNET_NO,
247                                    GNUNET_SCHEDULER_PRIORITY_HIGH,
248                                    GNUNET_SCHEDULER_NO_TASK,
249                                    tv,
250                                    max,
251                                    &rs,
252                                    &ws,
253                                    &run_daemon,
254                                    NULL);
255 }
256
257
258
259 /**
260  * Start server offering our hostlist.
261  *
262  * @return GNUNET_OK on success
263  */
264 int
265 GNUNET_HOSTLIST_server_start (const struct GNUNET_CONFIGURATION_Handle *c,
266                               struct GNUNET_SCHEDULER_Handle *s,
267                               struct GNUNET_STATISTICS_Handle *st)
268 {
269   unsigned long long port;
270
271   sched = s;
272   cfg = c;
273   if (-1 == GNUNET_CONFIGURATION_get_value_number (cfg,
274                                                    "HOSTLIST",
275                                                    "HTTPPORT", 
276                                                    &port))
277     return GNUNET_SYSERR;
278   daemon_handle = MHD_start_daemon (MHD_USE_IPv6,
279                                     (unsigned short) port,
280                                     &accept_policy_callback,
281                                     NULL,
282                                     &access_handler_callback,
283                                     NULL,
284                                     MHD_OPTION_CONNECTION_LIMIT, 16,
285                                     MHD_OPTION_PER_IP_CONNECTION_LIMIT, 1,
286                                     MHD_OPTION_CONNECTION_TIMEOUT, 16,
287                                     MHD_OPTION_CONNECTION_MEMORY_LIMIT,
288                                     16 * 1024, MHD_OPTION_END);
289   if (daemon_handle == NULL)
290     {
291       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
292                   _("Could not start hostlist HTTP server on port %u\n"),
293                   (unsigned short) port);
294       return GNUNET_SYSERR;    
295     }
296   prepare_daemon ();
297   return GNUNET_OK;
298 }
299
300 /**
301  * Stop server offering our hostlist.
302  */
303 void
304 GNUNET_HOSTLIST_server_stop ()
305 {
306   GNUNET_SCHEDULER_cancel (sched, hostlist_task);
307   hostlist_task = GNUNET_SCHEDULER_NO_TASK;
308   MHD_stop_daemon (daemon_handle);
309   daemon_handle = NULL;
310 }
311
312 /* end of hostlist-server.c */