clean up code fixes
[oweals/gnunet.git] / src / hostlist / hostlist-server.c
1 /*
2      This file is part of GNUnet.
3      (C) 2008, 2009, 2010 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  * Task that updates our HTTP response.
60  */
61 static GNUNET_SCHEDULER_TaskIdentifier response_task;
62
63 /**
64  * Our canonical response.
65  */
66 static struct MHD_Response *response;
67
68 /**
69  * Context for host processor.
70  */
71 struct HostSet
72 {
73   unsigned int size;
74
75   char *data;
76 };
77
78
79 /**
80  * Task that will produce a new response object.
81  */
82 static void
83 update_response (void *cls,
84                  const struct GNUNET_SCHEDULER_TaskContext *tc);
85
86
87 /**
88  * Function that assembles our response.
89  */
90 static void
91 finish_response (struct HostSet *results)
92 {
93   struct GNUNET_TIME_Relative freq;
94   
95   if (response != NULL)
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)
100     {
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,
106                                                     freq,
107                                                     &update_response,
108                                                     NULL);
109     }
110   else
111     {
112       MHD_destroy_response (response);
113       response = NULL;
114     }
115   GNUNET_free (results);
116 }
117
118
119 /**
120  * Callback that processes each of the known HELLOs for the
121  * hostlist response construction.
122  */
123 static void
124 host_processor (void *cls,
125                 const struct GNUNET_PeerIdentity * peer,
126                 const struct GNUNET_HELLO_Message *hello,
127                 uint32_t trust)
128 {
129   struct HostSet *results = cls;
130   size_t old;
131   size_t s;
132   
133   if (peer == NULL)
134     {
135       finish_response (results);
136       return;
137     }
138   old = results->size;
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,
143                      results->size,
144                      old + s);
145   memcpy (&results->data[old], hello, s);
146 }
147
148
149 /**
150  * Task that will produce a new response object.
151  */
152 static void
153 update_response (void *cls,
154                  const struct GNUNET_SCHEDULER_TaskContext *tc)
155 {
156   struct HostSet *results;
157
158   response_task = GNUNET_SCHEDULER_NO_TASK;
159   results = GNUNET_malloc(sizeof(struct HostSet));
160   GNUNET_PEERINFO_for_all (cfg, sched, 
161                            NULL,
162                            0, 
163                            GNUNET_TIME_UNIT_MINUTES,
164                            &host_processor,
165                            results);
166   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
167               _("Created new hostlist response with %u bytes\n"),
168               results->size);  
169 }
170
171
172 /**
173  * Hostlist access policy (very permissive, allows everything).
174  */
175 static int
176 accept_policy_callback (void *cls,
177                         const struct sockaddr *addr, socklen_t addrlen)
178 {
179   if (NULL == response)
180     {
181       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
182                   "Received request for hostlist, but I am not yet ready; rejecting!\n");
183       return MHD_NO;
184     }
185   return MHD_YES;               /* accept all */
186 }
187
188
189 /**
190  * Main request handler.
191  */
192 static int
193 access_handler_callback (void *cls,
194                          struct MHD_Connection *connection,
195                          const char *url,
196                          const char *method,
197                          const char *version,
198                          const char *upload_data,
199                          size_t*upload_data_size, void **con_cls)
200 {
201   static int dummy;
202   
203   if (0 != strcmp (method, MHD_HTTP_METHOD_GET))
204     return MHD_NO;
205   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
206               _("Received request for our hostlist\n"));
207   if (NULL == *con_cls)
208     {
209       (*con_cls) = &dummy;
210       return MHD_YES;           /* send 100 continue */
211     }
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);
217 }
218
219
220 /**
221  * Function that queries MHD's select sets and
222  * starts the task waiting for them.
223  */
224 static void 
225 prepare_daemon (void);
226
227
228 /**
229  * Call MHD to process pending requests and then go back
230  * and schedule the next run.
231  */
232 static void
233 run_daemon (void *cls,
234             const struct GNUNET_SCHEDULER_TaskContext *tc)
235 {
236   GNUNET_assert (MHD_YES == MHD_run (daemon_handle));
237   prepare_daemon ();
238 }
239
240
241 /**
242  * Function that queries MHD's select sets and
243  * starts the task waiting for them.
244  */
245 static void 
246 prepare_daemon ()
247 {
248   fd_set rs;
249   fd_set ws;
250   fd_set es;
251   struct GNUNET_NETWORK_FDSet *wrs;
252   struct GNUNET_NETWORK_FDSet *wws;
253   struct GNUNET_NETWORK_FDSet *wes;
254   int max;
255   unsigned long long timeout;
256   int haveto;
257   struct GNUNET_TIME_Relative tv;
258   
259   FD_ZERO(&rs);
260   FD_ZERO(&ws);
261   FD_ZERO(&es);
262   wrs = GNUNET_NETWORK_fdset_create ();
263   wes = GNUNET_NETWORK_fdset_create ();
264   wws = GNUNET_NETWORK_fdset_create ();
265   max = -1;
266   GNUNET_assert (MHD_YES ==
267                  MHD_get_fdset (daemon_handle,
268                                 &rs,
269                                 &ws,
270                                 &es,
271                                 &max));
272   haveto = MHD_get_timeout (daemon_handle, &timeout);
273   if (haveto == MHD_YES)
274     tv.value = (uint64_t) timeout;
275   else
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);
280   hostlist_task 
281     = GNUNET_SCHEDULER_add_select (sched,
282                                    GNUNET_SCHEDULER_PRIORITY_HIGH,
283                                    GNUNET_SCHEDULER_NO_TASK,
284                                    tv,
285                                    wrs,
286                                    wws,
287                                    &run_daemon,
288                                    NULL);
289   GNUNET_NETWORK_fdset_destroy (wrs);
290   GNUNET_NETWORK_fdset_destroy (wws);
291   GNUNET_NETWORK_fdset_destroy (wes);
292 }
293
294
295
296 /**
297  * Start server offering our hostlist.
298  *
299  * @return GNUNET_OK on success
300  */
301 int
302 GNUNET_HOSTLIST_server_start (const struct GNUNET_CONFIGURATION_Handle *c,
303                               struct GNUNET_SCHEDULER_Handle *s,
304                               struct GNUNET_STATISTICS_Handle *st)
305 {
306   unsigned long long port;
307
308   sched = s;
309   cfg = c;
310   if (-1 == GNUNET_CONFIGURATION_get_value_number (cfg,
311                                                    "HOSTLIST",
312                                                    "HTTPPORT", 
313                                                    &port))
314     return GNUNET_SYSERR;
315   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
316               _("Hostlist service starts on port %llu\n"),
317               port);
318   daemon_handle = MHD_start_daemon (MHD_USE_IPv6,
319                                     (unsigned short) port,
320                                     &accept_policy_callback,
321                                     NULL,
322                                     &access_handler_callback,
323                                     NULL,
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)
330     {
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;    
335     }
336   prepare_daemon ();
337   response_task = GNUNET_SCHEDULER_add_now (sched,
338                                             &update_response,
339                                             NULL);
340   return GNUNET_OK;
341 }
342
343 /**
344  * Stop server offering our hostlist.
345  */
346 void
347 GNUNET_HOSTLIST_server_stop ()
348 {
349   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
350               "Hostlist server shutdown\n");
351   if (GNUNET_SCHEDULER_NO_TASK != hostlist_task)
352     {
353       GNUNET_SCHEDULER_cancel (sched, hostlist_task);
354       hostlist_task = GNUNET_SCHEDULER_NO_TASK;
355     }
356   if (GNUNET_SCHEDULER_NO_TASK != response_task)
357     {
358       GNUNET_SCHEDULER_cancel (sched, response_task);
359       response_task = GNUNET_SCHEDULER_NO_TASK;
360     }
361   if (NULL != daemon_handle)
362     {
363       MHD_stop_daemon (daemon_handle);
364       daemon_handle = NULL;
365     }
366   if (response != NULL)
367     {
368       MHD_destroy_response (response);
369       response = NULL;
370     }
371 }
372
373 /* end of hostlist-server.c */