a481aa7a725c19ef0b92656497aec774bc67420a
[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 struct GNUNET_CONFIGURATION_Handle *cfg;
47
48 /**
49  * Our scheduler.
50  */
51 static struct GNUNET_SCHEDULER_Handle *sched;
52
53 /**
54  * Our canonical response.
55  */
56 static struct MHD_Response *response;
57
58 /**
59  * Context for host processor.
60  */
61 struct HostSet
62 {
63   size_t size;
64
65   char *data;
66 };
67
68
69 /**
70  * Task that will produce a new response object.
71  */
72 static void
73 update_response (void *cls,
74                  const struct GNUNET_SCHEDULER_TaskContext *tc);
75
76
77 /**
78  * Function that assembles our response.
79  */
80 static void
81 finish_response (struct HostSet *results)
82 {
83   if (response != NULL)
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,
90                                 GNUNET_NO,
91                                 GNUNET_SCHEDULER_PRIORITY_IDLE,
92                                 GNUNET_SCHEDULER_NO_PREREQUISITE_TASK,
93                                 RESPONSE_UPDATE_FREQUENCY,
94                                 &update_response,
95                                 NULL);
96 }
97
98
99 /**
100  * Callback that processes each of the known HELLOs for the
101  * hostlist response construction.
102  */
103 static void
104 host_processor (void *cls,
105                 const struct GNUNET_PeerIdentity * peer,
106                 const struct GNUNET_HELLO_Message *hello,
107                 uint32_t trust)
108 {
109   struct HostSet *results = cls;
110   size_t old;
111   size_t s;
112   
113   if (peer == NULL)
114     finish_response (results);
115   old = results->size;
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,
120                      results->size,
121                      old + s);
122   memcpy (&results->data[old], hello, s);
123 }
124
125
126 /**
127  * Task that will produce a new response object.
128  */
129 static void
130 update_response (void *cls,
131                  const struct GNUNET_SCHEDULER_TaskContext *tc)
132 {
133   struct HostSet *results;
134
135   results = GNUNET_malloc(sizeof(struct HostSet));
136   GNUNET_PEERINFO_for_all (cfg, sched, 
137                            NULL,
138                            0, 
139                            GNUNET_TIME_UNIT_MINUTES,
140                            &host_processor,
141                            results);
142 }
143
144
145 /**
146  * Hostlist access policy (very permissive, allows everything).
147  */
148 static int
149 accept_policy_callback (void *cls,
150                         const struct sockaddr *addr, socklen_t addrlen)
151 {
152   return MHD_YES;               /* accept all */
153 }
154
155
156 /**
157  * Main request handler.
158  */
159 static int
160 access_handler_callback (void *cls,
161                          struct MHD_Connection *connection,
162                          const char *url,
163                          const char *method,
164                          const char *version,
165                          const char *upload_data,
166                          unsigned int *upload_data_size, void **con_cls)
167 {
168   static int dummy;
169   
170   if (0 != strcmp (method, MHD_HTTP_METHOD_GET))
171     return MHD_NO;
172   if (NULL == *con_cls)
173     {
174       (*con_cls) = &dummy;
175       return MHD_YES;           /* send 100 continue */
176     }
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);
182 }
183
184
185 /**
186  * Function that queries MHD's select sets and
187  * starts the task waiting for them.
188  */
189 static void 
190 prepare_daemon (void);
191
192
193 static void
194 run_daemon (void *cls,
195             const struct GNUNET_SCHEDULER_TaskContext *tc)
196 {
197   GNUNET_assert (MHD_YES == MHD_run (daemon_handle));
198   prepare_daemon ();
199 }
200
201
202 /**
203  * Function that queries MHD's select sets and
204  * starts the task waiting for them.
205  */
206 static void 
207 prepare_daemon ()
208 {
209   fd_set rs;
210   fd_set ws;
211   fd_set es;
212   int max;
213   unsigned long long timeout;
214   int haveto;
215   struct GNUNET_TIME_Relative tv;
216   
217   FD_ZERO(&rs);
218   FD_ZERO(&ws);
219   FD_ZERO(&es);
220   max = -1;
221   GNUNET_assert (MHD_YES ==
222                  MHD_get_fdset (daemon_handle,
223                                 &rs,
224                                 &ws,
225                                 &es,
226                                 &max));
227   haveto = MHD_get_timeout (daemon_handle, &timeout);
228   if (haveto == MHD_YES)
229     tv.value = (uint64_t) timeout;
230   else
231     tv = GNUNET_TIME_UNIT_FOREVER_REL;
232   GNUNET_SCHEDULER_add_select (sched,
233                                GNUNET_NO,
234                                GNUNET_SCHEDULER_PRIORITY_HIGH,
235                                GNUNET_SCHEDULER_NO_PREREQUISITE_TASK,
236                                tv,
237                                max,
238                                &rs,
239                                &ws,
240                                &run_daemon,
241                                NULL);
242 }
243
244
245
246 /**
247  * Start server offering our hostlist.
248  *
249  * @return GNUNET_OK on success
250  */
251 int
252 GNUNET_HOSTLIST_server_start (struct GNUNET_CONFIGURATION_Handle *c,
253                               struct GNUNET_SCHEDULER_Handle *s,
254                               struct GNUNET_STATISTICS_Handle *st)
255 {
256   unsigned long long port;
257
258   sched = s;
259   cfg = c;
260   if (-1 == GNUNET_CONFIGURATION_get_value_number (cfg,
261                                                    "HOSTLIST",
262                                                    "PORT", 
263                                                    &port))
264     return GNUNET_SYSERR;
265   daemon_handle = MHD_start_daemon (MHD_USE_IPv6,
266                                     (unsigned short) port,
267                                     &accept_policy_callback,
268                                     NULL,
269                                     &access_handler_callback,
270                                     NULL,
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)
277     {
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;    
282     }
283   prepare_daemon ();
284   return GNUNET_OK;
285 }
286
287 /**
288  * Stop server offering our hostlist.
289  */
290 void
291 GNUNET_HOSTLIST_server_stop ()
292 {
293   MHD_stop_daemon (daemon_handle);
294   daemon_handle = NULL;
295 }
296
297 /* end of hostlist-server.c */