b15fdc40819d91bf6e02ea94d008ad8b286abd00
[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                                 RESPONSE_UPDATE_FREQUENCY,
96                                 &update_response,
97                                 NULL);
98 }
99
100
101 /**
102  * Callback that processes each of the known HELLOs for the
103  * hostlist response construction.
104  */
105 static void
106 host_processor (void *cls,
107                 const struct GNUNET_PeerIdentity * peer,
108                 const struct GNUNET_HELLO_Message *hello,
109                 uint32_t trust)
110 {
111   struct HostSet *results = cls;
112   size_t old;
113   size_t s;
114   
115   if (peer == NULL)
116     {
117       finish_response (results);
118       return;
119     }
120   old = results->size;
121   s = GNUNET_HELLO_size(hello);
122   if (old + s >= GNUNET_MAX_MALLOC_CHECKED)
123     return; /* too large, skip! */
124   GNUNET_array_grow (results->data,
125                      results->size,
126                      old + s);
127   memcpy (&results->data[old], hello, s);
128 }
129
130
131 /**
132  * Task that will produce a new response object.
133  */
134 static void
135 update_response (void *cls,
136                  const struct GNUNET_SCHEDULER_TaskContext *tc)
137 {
138   struct HostSet *results;
139
140   results = GNUNET_malloc(sizeof(struct HostSet));
141   GNUNET_PEERINFO_for_all (cfg, sched, 
142                            NULL,
143                            0, 
144                            GNUNET_TIME_UNIT_MINUTES,
145                            &host_processor,
146                            results);
147   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
148               _("Created new hostlist response with %u bytes\n"),
149               results->size);  
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   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
181               _("Received request for our hostlist\n"));
182   if (NULL == *con_cls)
183     {
184       (*con_cls) = &dummy;
185       return MHD_YES;           /* send 100 continue */
186     }
187   if (*upload_data_size != 0)
188     return MHD_NO;              /* do not support upload data */
189   if (response == NULL)
190     return MHD_NO;              /* internal error, no response yet */
191   return MHD_queue_response (connection, MHD_HTTP_OK, response);
192 }
193
194
195 /**
196  * Function that queries MHD's select sets and
197  * starts the task waiting for them.
198  */
199 static void 
200 prepare_daemon (void);
201
202
203 /**
204  * Call MHD to process pending requests and then go back
205  * and schedule the next run.
206  */
207 static void
208 run_daemon (void *cls,
209             const struct GNUNET_SCHEDULER_TaskContext *tc)
210 {
211   GNUNET_assert (MHD_YES == MHD_run (daemon_handle));
212   prepare_daemon ();
213 }
214
215
216 /**
217  * Function that queries MHD's select sets and
218  * starts the task waiting for them.
219  */
220 static void 
221 prepare_daemon ()
222 {
223   fd_set rs;
224   fd_set ws;
225   fd_set es;
226   struct GNUNET_NETWORK_FDSet *wrs;
227   struct GNUNET_NETWORK_FDSet *wws;
228   struct GNUNET_NETWORK_FDSet *wes;
229   int max;
230   unsigned long long timeout;
231   int haveto;
232   struct GNUNET_TIME_Relative tv;
233   
234   FD_ZERO(&rs);
235   FD_ZERO(&ws);
236   FD_ZERO(&es);
237   wrs = GNUNET_NETWORK_fdset_create ();
238   wes = GNUNET_NETWORK_fdset_create ();
239   wws = GNUNET_NETWORK_fdset_create ();
240   max = -1;
241   GNUNET_assert (MHD_YES ==
242                  MHD_get_fdset (daemon_handle,
243                                 &rs,
244                                 &ws,
245                                 &es,
246                                 &max));
247   haveto = MHD_get_timeout (daemon_handle, &timeout);
248   if (haveto == MHD_YES)
249     tv.value = (uint64_t) timeout;
250   else
251     tv = GNUNET_TIME_UNIT_FOREVER_REL;
252   GNUNET_NETWORK_fdset_copy_native (wrs, &rs, max);
253   GNUNET_NETWORK_fdset_copy_native (wws, &ws, max);
254   GNUNET_NETWORK_fdset_copy_native (wes, &es, max);
255   hostlist_task 
256     = GNUNET_SCHEDULER_add_select (sched,
257                                    GNUNET_SCHEDULER_PRIORITY_HIGH,
258                                    GNUNET_SCHEDULER_NO_TASK,
259                                    tv,
260                                    wrs,
261                                    wws,
262                                    &run_daemon,
263                                    NULL);
264   GNUNET_NETWORK_fdset_destroy (wrs);
265   GNUNET_NETWORK_fdset_destroy (wws);
266   GNUNET_NETWORK_fdset_destroy (wes);
267 }
268
269
270
271 /**
272  * Start server offering our hostlist.
273  *
274  * @return GNUNET_OK on success
275  */
276 int
277 GNUNET_HOSTLIST_server_start (const struct GNUNET_CONFIGURATION_Handle *c,
278                               struct GNUNET_SCHEDULER_Handle *s,
279                               struct GNUNET_STATISTICS_Handle *st)
280 {
281   unsigned long long port;
282
283   sched = s;
284   cfg = c;
285   if (-1 == GNUNET_CONFIGURATION_get_value_number (cfg,
286                                                    "HOSTLIST",
287                                                    "HTTPPORT", 
288                                                    &port))
289     return GNUNET_SYSERR;
290   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
291               _("Hostlist service starts on port %llu\n"),
292               port);
293   daemon_handle = MHD_start_daemon (MHD_USE_IPv6,
294                                     (unsigned short) port,
295                                     &accept_policy_callback,
296                                     NULL,
297                                     &access_handler_callback,
298                                     NULL,
299                                     MHD_OPTION_CONNECTION_LIMIT, 16,
300                                     MHD_OPTION_PER_IP_CONNECTION_LIMIT, 1,
301                                     MHD_OPTION_CONNECTION_TIMEOUT, 16,
302                                     MHD_OPTION_CONNECTION_MEMORY_LIMIT,
303                                     16 * 1024, MHD_OPTION_END);
304   if (daemon_handle == NULL)
305     {
306       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
307                   _("Could not start hostlist HTTP server on port %u\n"),
308                   (unsigned short) port);
309       return GNUNET_SYSERR;    
310     }
311   prepare_daemon ();
312   return GNUNET_OK;
313 }
314
315 /**
316  * Stop server offering our hostlist.
317  */
318 void
319 GNUNET_HOSTLIST_server_stop ()
320 {
321   GNUNET_SCHEDULER_cancel (sched, hostlist_task);
322   hostlist_task = GNUNET_SCHEDULER_NO_TASK;
323   MHD_stop_daemon (daemon_handle);
324   daemon_handle = NULL;
325 }
326
327 /* end of hostlist-server.c */