2 This file is part of GNUnet
3 (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009 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 3, 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 transport/plugin_transport_http.c
23 * @brief http transport service plugin
24 * @author Matthias Wachs
27 #include "plugin_transport_http.h"
30 server_log (void *arg, const char *fmt, va_list ap)
34 vsnprintf (text, sizeof (text), fmt, ap);
36 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "server: %s\n", text);
40 * Check if incoming connection is accepted.
41 * NOTE: Here every connection is accepted
42 * @param cls plugin as closure
43 * @param addr address of incoming connection
44 * @param addr_len address length of incoming connection
45 * @return MHD_YES if connection is accepted, MHD_NO if connection is rejected
49 server_accept_cb (void *cls, const struct sockaddr *addr, socklen_t addr_len)
51 struct Plugin * plugin = cls;
52 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "server: server_accept_cb\n");
53 if (plugin->cur_connections <= plugin->max_connections)
61 * Callback called by MHD when it needs data to send
62 * @param cls current session
63 * @param pos position in buffer
64 * @param buf the buffer to write data to
65 * @param max max number of bytes available in buffer
66 * @return bytes written to buffer
70 server_send_cb (void *cls, uint64_t pos, char *buf, size_t max)
80 server_load_file (const char *file)
82 struct GNUNET_DISK_FileHandle *gn_file;
86 if (0 != STAT (file, &fstat))
88 text = GNUNET_malloc (fstat.st_size + 1);
90 GNUNET_DISK_file_open (file, GNUNET_DISK_OPEN_READ,
91 GNUNET_DISK_PERM_USER_READ);
97 if (GNUNET_SYSERR == GNUNET_DISK_file_read (gn_file, text, fstat.st_size))
100 GNUNET_DISK_file_close (gn_file);
103 text[fstat.st_size] = '\0';
104 GNUNET_DISK_file_close (gn_file);
113 server_load_certificate (struct Plugin *plugin)
120 /* Get crypto init string from config
121 * If not present just use default values */
122 GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg, plugin->name,
123 "CRYPTO_INIT", &plugin->crypto_init);
126 GNUNET_CONFIGURATION_get_value_filename (plugin->env->cfg, plugin->name,
127 "KEY_FILE", &key_file))
129 key_file = "https_key.key";
133 GNUNET_CONFIGURATION_get_value_filename (plugin->env->cfg, plugin->name,
134 "CERT_FILE", &cert_file))
136 cert_file = "https_cert.crt";
139 /* read key & certificates from file */
141 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
142 "Loading TLS certificate from key-file `%s' cert-file`%s'\n",
143 key_file, cert_file);
146 plugin->key = server_load_file (key_file);
147 plugin->cert = server_load_file (cert_file);
149 if ((plugin->key == NULL) || (plugin->cert == NULL))
151 struct GNUNET_OS_Process *cert_creation;
153 GNUNET_free_non_null (plugin->key);
155 GNUNET_free_non_null (plugin->cert);
159 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
160 "No usable TLS certificate found, creating certificate\n");
164 GNUNET_OS_start_process (NULL, NULL,
165 "gnunet-transport-certificate-creation",
166 "gnunet-transport-certificate-creation",
167 key_file, cert_file, NULL);
168 if (cert_creation == NULL)
170 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
172 ("Could not create a new TLS certificate, program `gnunet-transport-certificate-creation' could not be started!\n"));
173 GNUNET_free (key_file);
174 GNUNET_free (cert_file);
176 GNUNET_free_non_null (plugin->key);
177 GNUNET_free_non_null (plugin->cert);
178 GNUNET_free_non_null (plugin->crypto_init);
180 return GNUNET_SYSERR;
182 GNUNET_assert (GNUNET_OK == GNUNET_OS_process_wait (cert_creation));
183 GNUNET_OS_process_close (cert_creation);
185 plugin->key = server_load_file (key_file);
186 plugin->cert = server_load_file (cert_file);
189 if ((plugin->key == NULL) || (plugin->cert == NULL))
191 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
193 ("No usable TLS certificate found and creating one failed!\n"),
195 GNUNET_free (key_file);
196 GNUNET_free (cert_file);
198 GNUNET_free_non_null (plugin->key);
199 GNUNET_free_non_null (plugin->cert);
200 GNUNET_free_non_null (plugin->crypto_init);
202 return GNUNET_SYSERR;
204 GNUNET_free (key_file);
205 GNUNET_free (cert_file);
207 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "TLS certificate loaded\n");
216 * Process GET or PUT request received via MHD. For
217 * GET, queue response that will send back our pending
218 * messages. For PUT, process incoming data and send
219 * to GNUnet core. In either case, check if a session
220 * already exists and create a new one if not.
223 server_access_cb (void *cls, struct MHD_Connection *mhd_connection,
224 const char *url, const char *method, const char *version,
225 const char *upload_data, size_t * upload_data_size,
226 void **httpSessionCache)
228 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "server: server_access_cb\n");
233 server_disconnect_cb (void *cls, struct MHD_Connection *connection,
234 void **httpSessionCache)
236 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "server: server_disconnect_cb\n");
240 server_disconnect (struct Session *s)
246 server_send (struct Session *s, const char *msgbuf, size_t msgbuf_size)
252 * Function that queries MHD's select sets and
253 * starts the task waiting for them.
254 * @param plugin plugin
255 * @param daemon_handle the MHD daemon handle
256 * @return gnunet task identifier
258 static GNUNET_SCHEDULER_TaskIdentifier
259 server_schedule_daemon (struct Plugin *plugin, struct MHD_Daemon *daemon_handle);
262 * Call MHD IPv4 to process pending requests and then go back
263 * and schedule the next run.
264 * @param cls plugin as closure
265 * @param tc task context
268 http_server_daemon_v4_run (void *cls,
269 const struct GNUNET_SCHEDULER_TaskContext *tc)
271 struct Plugin *plugin = cls;
272 GNUNET_assert (cls != NULL);
274 plugin->server_v4_task = GNUNET_SCHEDULER_NO_TASK;
276 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
279 GNUNET_assert (MHD_YES == MHD_run (plugin->server_v4));
280 plugin->server_v4_task = server_schedule_daemon (plugin, plugin->server_v4);
285 * Call MHD IPv6 to process pending requests and then go back
286 * and schedule the next run.
287 * @param cls plugin as closure
288 * @param tc task context
291 http_server_daemon_v6_run (void *cls,
292 const struct GNUNET_SCHEDULER_TaskContext *tc)
294 struct Plugin *plugin = cls;
295 GNUNET_assert (cls != NULL);
297 plugin->server_v6_task = GNUNET_SCHEDULER_NO_TASK;
299 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
302 GNUNET_assert (MHD_YES == MHD_run (plugin->server_v6));
303 plugin->server_v6_task = server_schedule_daemon (plugin, plugin->server_v6);
307 * Function that queries MHD's select sets and
308 * starts the task waiting for them.
309 * @param plugin plugin
310 * @param daemon_handle the MHD daemon handle
311 * @return gnunet task identifier
313 static GNUNET_SCHEDULER_TaskIdentifier
314 server_schedule_daemon (struct Plugin *plugin, struct MHD_Daemon *daemon_handle)
316 GNUNET_SCHEDULER_TaskIdentifier ret;
320 struct GNUNET_NETWORK_FDSet *wrs;
321 struct GNUNET_NETWORK_FDSet *wws;
322 struct GNUNET_NETWORK_FDSet *wes;
324 unsigned long long timeout;
326 struct GNUNET_TIME_Relative tv;
328 ret = GNUNET_SCHEDULER_NO_TASK;
332 wrs = GNUNET_NETWORK_fdset_create ();
333 wes = GNUNET_NETWORK_fdset_create ();
334 wws = GNUNET_NETWORK_fdset_create ();
336 GNUNET_assert (MHD_YES == MHD_get_fdset (daemon_handle, &rs, &ws, &es, &max));
337 haveto = MHD_get_timeout (daemon_handle, &timeout);
338 if (haveto == MHD_YES)
339 tv.rel_value = (uint64_t) timeout;
341 tv = GNUNET_TIME_UNIT_SECONDS;
342 GNUNET_NETWORK_fdset_copy_native (wrs, &rs, max + 1);
343 GNUNET_NETWORK_fdset_copy_native (wws, &ws, max + 1);
344 GNUNET_NETWORK_fdset_copy_native (wes, &es, max + 1);
345 if (daemon_handle == plugin->server_v4)
347 if (plugin->server_v4_task != GNUNET_SCHEDULER_NO_TASK)
349 GNUNET_SCHEDULER_cancel (plugin->server_v4_task);
350 plugin->server_v4_task = GNUNET_SCHEDULER_NO_TASK;
354 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
355 GNUNET_SCHEDULER_NO_TASK, tv, wrs, wws,
356 &http_server_daemon_v4_run, plugin);
358 if (daemon_handle == plugin->server_v6)
360 if (plugin->server_v6_task != GNUNET_SCHEDULER_NO_TASK)
362 GNUNET_SCHEDULER_cancel (plugin->server_v6_task);
363 plugin->server_v6_task = GNUNET_SCHEDULER_NO_TASK;
367 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
368 GNUNET_SCHEDULER_NO_TASK, tv, wrs, wws,
369 &http_server_daemon_v6_run, plugin);
371 GNUNET_NETWORK_fdset_destroy (wrs);
372 GNUNET_NETWORK_fdset_destroy (wws);
373 GNUNET_NETWORK_fdset_destroy (wes);
378 server_start (struct Plugin *plugin)
383 res = server_load_certificate (plugin);
384 if (res == GNUNET_SYSERR)
386 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "TABORT\n");
391 plugin->server_v4 = NULL;
392 if (plugin->ipv4 == GNUNET_YES)
394 plugin->server_v4 = MHD_start_daemon (
401 MHD_NO_FLAG, plugin->port,
402 &server_accept_cb, plugin,
403 &server_access_cb, plugin,
404 //MHD_OPTION_SOCK_ADDR,
405 //(struct sockaddr_in *)
406 //plugin->bind4_address,
407 MHD_OPTION_CONNECTION_LIMIT,
409 plugin->max_connections,
411 MHD_OPTION_HTTPS_PRIORITIES,
413 MHD_OPTION_HTTPS_MEM_KEY,
415 MHD_OPTION_HTTPS_MEM_CERT,
418 MHD_OPTION_CONNECTION_TIMEOUT,
420 MHD_OPTION_CONNECTION_MEMORY_LIMIT,
422 GNUNET_SERVER_MAX_MESSAGE_SIZE),
423 MHD_OPTION_NOTIFY_COMPLETED,
424 &server_disconnect_cb, plugin,
425 MHD_OPTION_EXTERNAL_LOGGER,
426 server_log, NULL, MHD_OPTION_END);
427 if (plugin->server_v4 == NULL)
430 plugin->server_v6 = NULL;
431 if (plugin->ipv6 == GNUNET_YES)
433 plugin->server_v6 = MHD_start_daemon (
440 MHD_USE_IPv6, plugin->port,
441 &server_accept_cb, plugin,
442 &server_access_cb, plugin,
443 //MHD_OPTION_SOCK_ADDR,
445 MHD_OPTION_CONNECTION_LIMIT,
447 plugin->max_connections,
449 MHD_OPTION_HTTPS_PRIORITIES,
451 MHD_OPTION_HTTPS_MEM_KEY,
453 MHD_OPTION_HTTPS_MEM_CERT,
456 MHD_OPTION_CONNECTION_TIMEOUT,
458 MHD_OPTION_CONNECTION_MEMORY_LIMIT,
460 GNUNET_SERVER_MAX_MESSAGE_SIZE),
461 MHD_OPTION_NOTIFY_COMPLETED,
462 &server_disconnect_cb, plugin,
463 MHD_OPTION_EXTERNAL_LOGGER,
464 server_log, NULL, MHD_OPTION_END);
466 if (plugin->server_v6 == NULL)
470 if (plugin->server_v4 != NULL)
471 plugin->server_v4_task = server_schedule_daemon (plugin, plugin->server_v4);
472 if (plugin->server_v6 != NULL)
473 plugin->server_v6_task = server_schedule_daemon (plugin, plugin->server_v6);
476 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
477 "%s server component started on port %u\n", plugin->name,
484 server_stop (struct Plugin *plugin)
486 if (plugin->server_v4_task != GNUNET_SCHEDULER_NO_TASK)
488 GNUNET_SCHEDULER_cancel (plugin->server_v4_task);
489 plugin->server_v4_task = GNUNET_SCHEDULER_NO_TASK;
492 if (plugin->server_v6_task != GNUNET_SCHEDULER_NO_TASK)
494 GNUNET_SCHEDULER_cancel (plugin->server_v6_task);
495 plugin->server_v6_task = GNUNET_SCHEDULER_NO_TASK;
498 if (plugin->server_v4 != NULL)
500 MHD_stop_daemon (plugin->server_v4);
501 plugin->server_v4 = NULL;
503 if (plugin->server_v6 != NULL)
505 MHD_stop_daemon (plugin->server_v6);
506 plugin->server_v6 = NULL;
510 GNUNET_free_non_null (plugin->crypto_init);
511 GNUNET_free_non_null (plugin->cert);
512 GNUNET_free_non_null (plugin->key);
516 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
517 "%s server component stopped\n", plugin->name);
523 /* end of plugin_transport_http.c */