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"
29 #define HTTP_ERROR_RESPONSE "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\"><HTML><HEAD><TITLE>404 Not Found</TITLE></HEAD><BODY><H1>Not Found</H1>The requested URL was not found on this server.<P><HR><ADDRESS></ADDRESS></BODY></HTML>"
34 server_log (void *arg, const char *fmt, va_list ap)
38 vsnprintf (text, sizeof (text), fmt, ap);
40 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "server: %s\n", text);
44 * Check if incoming connection is accepted.
45 * NOTE: Here every connection is accepted
46 * @param cls plugin as closure
47 * @param addr address of incoming connection
48 * @param addr_len address length of incoming connection
49 * @return MHD_YES if connection is accepted, MHD_NO if connection is rejected
53 server_accept_cb (void *cls, const struct sockaddr *addr, socklen_t addr_len)
55 struct Plugin * plugin = cls;
57 if (plugin->cur_connections <= plugin->max_connections)
61 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "server: Cannot accept new connections\n");
68 * Callback called by MHD when it needs data to send
69 * @param cls current session
70 * @param pos position in buffer
71 * @param buf the buffer to write data to
72 * @param max max number of bytes available in buffer
73 * @return bytes written to buffer
77 server_send_cb (void *cls, uint64_t pos, char *buf, size_t max)
87 server_load_file (const char *file)
89 struct GNUNET_DISK_FileHandle *gn_file;
93 if (0 != STAT (file, &fstat))
95 text = GNUNET_malloc (fstat.st_size + 1);
97 GNUNET_DISK_file_open (file, GNUNET_DISK_OPEN_READ,
98 GNUNET_DISK_PERM_USER_READ);
104 if (GNUNET_SYSERR == GNUNET_DISK_file_read (gn_file, text, fstat.st_size))
107 GNUNET_DISK_file_close (gn_file);
110 text[fstat.st_size] = '\0';
111 GNUNET_DISK_file_close (gn_file);
120 server_load_certificate (struct Plugin *plugin)
127 /* Get crypto init string from config
128 * If not present just use default values */
129 GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg, plugin->name,
130 "CRYPTO_INIT", &plugin->crypto_init);
133 GNUNET_CONFIGURATION_get_value_filename (plugin->env->cfg, plugin->name,
134 "KEY_FILE", &key_file))
136 key_file = "https_key.key";
140 GNUNET_CONFIGURATION_get_value_filename (plugin->env->cfg, plugin->name,
141 "CERT_FILE", &cert_file))
143 cert_file = "https_cert.crt";
146 /* read key & certificates from file */
148 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
149 "Loading TLS certificate from key-file `%s' cert-file`%s'\n",
150 key_file, cert_file);
153 plugin->key = server_load_file (key_file);
154 plugin->cert = server_load_file (cert_file);
156 if ((plugin->key == NULL) || (plugin->cert == NULL))
158 struct GNUNET_OS_Process *cert_creation;
160 GNUNET_free_non_null (plugin->key);
162 GNUNET_free_non_null (plugin->cert);
166 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
167 "No usable TLS certificate found, creating certificate\n");
171 GNUNET_OS_start_process (NULL, NULL,
172 "gnunet-transport-certificate-creation",
173 "gnunet-transport-certificate-creation",
174 key_file, cert_file, NULL);
175 if (cert_creation == NULL)
177 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
179 ("Could not create a new TLS certificate, program `gnunet-transport-certificate-creation' could not be started!\n"));
180 GNUNET_free (key_file);
181 GNUNET_free (cert_file);
183 GNUNET_free_non_null (plugin->key);
184 GNUNET_free_non_null (plugin->cert);
185 GNUNET_free_non_null (plugin->crypto_init);
187 return GNUNET_SYSERR;
189 GNUNET_assert (GNUNET_OK == GNUNET_OS_process_wait (cert_creation));
190 GNUNET_OS_process_close (cert_creation);
192 plugin->key = server_load_file (key_file);
193 plugin->cert = server_load_file (cert_file);
196 if ((plugin->key == NULL) || (plugin->cert == NULL))
198 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
200 ("No usable TLS certificate found and creating one failed!\n"),
202 GNUNET_free (key_file);
203 GNUNET_free (cert_file);
205 GNUNET_free_non_null (plugin->key);
206 GNUNET_free_non_null (plugin->cert);
207 GNUNET_free_non_null (plugin->crypto_init);
209 return GNUNET_SYSERR;
211 GNUNET_free (key_file);
212 GNUNET_free (cert_file);
214 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "TLS certificate loaded\n");
223 * Process GET or PUT request received via MHD. For
224 * GET, queue response that will send back our pending
225 * messages. For PUT, process incoming data and send
226 * to GNUnet core. In either case, check if a session
227 * already exists and create a new one if not.
230 server_access_cb (void *cls, struct MHD_Connection *mhd_connection,
231 const char *url, const char *method, const char *version,
232 const char *upload_data, size_t * upload_data_size,
233 void **httpSessionCache)
235 //struct Plugin *plugin = cls;
236 struct Session *s = *httpSessionCache;
238 //struct MHD_Response *response;
240 GNUNET_assert (cls != NULL);
246 const union MHD_ConnectionInfo *conn_info;
248 struct GNUNET_PeerIdentity target;
249 int check = GNUNET_NO;
253 conn_info = MHD_get_connection_info (mhd_connection, MHD_CONNECTION_INFO_CLIENT_ADDRESS);
254 if (conn_info->client_addr->sa_family == AF_INET)
255 addrlen = sizeof (struct sockaddr_in);
256 else if (conn_info->client_addr->sa_family == AF_INET6)
257 addrlen = sizeof (struct sockaddr_in6);
261 if ((strlen(&url[1]) >= 105) && (url[104] == ';'))
264 char * tagc = (char *) &url[105];
265 memcpy(&hash, &url[1], 103);
267 if (GNUNET_OK == GNUNET_CRYPTO_hash_from_string ((const char *) &hash, &(target.hashPubKey)))
269 tag = strtoul (tagc, NULL, 10);
275 if (0 == strcmp (MHD_HTTP_METHOD_PUT, method))
276 direction = _RECEIVE;
277 if (0 == strcmp (MHD_HTTP_METHOD_GET, method))
280 if (check == GNUNET_NO)
283 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "server: New inbound connection from %s with tag %u\n", GNUNET_h2s_full(&(target.hashPubKey)), tag);
285 /* find duplicate session */
291 if ((t->inbound) && (0 == memcmp (&t->target, &target, sizeof (struct GNUNET_PeerIdentity))) &&
292 /* FIXME add source address comparison */
300 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "server: Duplicate session, dismissing new connection from peer `%s'\n", GNUNET_i2s (&target));
305 /* find semi-session */
306 t = plugin->server_semi_head;
310 /* FIXME add source address comparison */
311 if ((0 == memcmp (&t->target, &target, sizeof (struct GNUNET_PeerIdentity))) &&
322 if ((direction == _SEND) && (t->server_get != NULL))
325 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "server: Duplicate GET session, dismissing new connection from peer `%s'\n", GNUNET_i2s (&target));
333 GNUNET_CONTAINER_DLL_remove(plugin->server_semi_head, plugin->server_semi_tail, s);
334 GNUNET_CONTAINER_DLL_insert(plugin->head, plugin->tail, s);
336 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "server: Found matching semi-session, merging session for peer `%s' `%s'\n", GNUNET_i2s (&target));
341 if ((direction == _RECEIVE) && (t->server_put != NULL))
344 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "server: Duplicate PUT session, dismissing new connection from peer `%s'\n", GNUNET_i2s (&target));
352 GNUNET_CONTAINER_DLL_remove(plugin->server_semi_head, plugin->server_semi_tail, s);
353 GNUNET_CONTAINER_DLL_insert(plugin->head, plugin->tail, s);
355 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "server: Found matching semi-session, merging session for peer `%s' `%s'\n", GNUNET_i2s (&target));
361 /* create new session */
363 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "server: Creating new session for peer `%s' \n", GNUNET_i2s (&target));
366 s = create_session(plugin,
368 conn_info->client_addr,
373 s->inbound = GNUNET_YES;
375 if (0 == strcmp (MHD_HTTP_METHOD_PUT, method))
377 if (0 == strcmp (MHD_HTTP_METHOD_GET, method))
379 GNUNET_CONTAINER_DLL_insert (plugin->server_semi_head, plugin->server_semi_tail, s);
383 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "server: Invalid connection request\n");
384 response = MHD_create_response_from_data (strlen (HTTP_ERROR_RESPONSE),HTTP_ERROR_RESPONSE, MHD_NO, MHD_NO);
385 res = MHD_queue_response (mhd_connection, MHD_HTTP_NOT_FOUND, response);
386 MHD_destroy_response (response);
391 (*httpSessionCache) = s;
402 server_disconnect_cb (void *cls, struct MHD_Connection *connection,
403 void **httpSessionCache)
405 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "server: server_disconnect_cb\n");
407 struct Session *s = *httpSessionCache;
411 notify_session_end(s->plugin, &s->target, s);
417 server_disconnect (struct Session *s)
423 server_send (struct Session *s, const char *msgbuf, size_t msgbuf_size)
429 * Function that queries MHD's select sets and
430 * starts the task waiting for them.
431 * @param plugin plugin
432 * @param daemon_handle the MHD daemon handle
433 * @return gnunet task identifier
435 static GNUNET_SCHEDULER_TaskIdentifier
436 server_schedule (struct Plugin *plugin, struct MHD_Daemon *daemon_handle);
439 * Call MHD IPv4 to process pending requests and then go back
440 * and schedule the next run.
441 * @param cls plugin as closure
442 * @param tc task context
445 server_v4_run (void *cls,
446 const struct GNUNET_SCHEDULER_TaskContext *tc)
448 struct Plugin *plugin = cls;
449 GNUNET_assert (cls != NULL);
451 plugin->server_v4_task = GNUNET_SCHEDULER_NO_TASK;
453 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
456 GNUNET_assert (MHD_YES == MHD_run (plugin->server_v4));
457 plugin->server_v4_task = server_schedule (plugin, plugin->server_v4);
462 * Call MHD IPv6 to process pending requests and then go back
463 * and schedule the next run.
464 * @param cls plugin as closure
465 * @param tc task context
468 server_v6_run (void *cls,
469 const struct GNUNET_SCHEDULER_TaskContext *tc)
471 struct Plugin *plugin = cls;
472 GNUNET_assert (cls != NULL);
474 plugin->server_v6_task = GNUNET_SCHEDULER_NO_TASK;
476 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
479 GNUNET_assert (MHD_YES == MHD_run (plugin->server_v6));
480 plugin->server_v6_task = server_schedule (plugin, plugin->server_v6);
484 * Function that queries MHD's select sets and
485 * starts the task waiting for them.
486 * @param plugin plugin
487 * @param daemon_handle the MHD daemon handle
488 * @return gnunet task identifier
490 static GNUNET_SCHEDULER_TaskIdentifier
491 server_schedule (struct Plugin *plugin, struct MHD_Daemon *daemon_handle)
493 GNUNET_SCHEDULER_TaskIdentifier ret;
497 struct GNUNET_NETWORK_FDSet *wrs;
498 struct GNUNET_NETWORK_FDSet *wws;
499 struct GNUNET_NETWORK_FDSet *wes;
501 unsigned long long timeout;
503 struct GNUNET_TIME_Relative tv;
505 ret = GNUNET_SCHEDULER_NO_TASK;
509 wrs = GNUNET_NETWORK_fdset_create ();
510 wes = GNUNET_NETWORK_fdset_create ();
511 wws = GNUNET_NETWORK_fdset_create ();
513 GNUNET_assert (MHD_YES == MHD_get_fdset (daemon_handle, &rs, &ws, &es, &max));
514 haveto = MHD_get_timeout (daemon_handle, &timeout);
515 if (haveto == MHD_YES)
516 tv.rel_value = (uint64_t) timeout;
518 tv = GNUNET_TIME_UNIT_SECONDS;
519 GNUNET_NETWORK_fdset_copy_native (wrs, &rs, max + 1);
520 GNUNET_NETWORK_fdset_copy_native (wws, &ws, max + 1);
521 GNUNET_NETWORK_fdset_copy_native (wes, &es, max + 1);
522 if (daemon_handle == plugin->server_v4)
524 if (plugin->server_v4_task != GNUNET_SCHEDULER_NO_TASK)
526 GNUNET_SCHEDULER_cancel (plugin->server_v4_task);
527 plugin->server_v4_task = GNUNET_SCHEDULER_NO_TASK;
531 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
532 GNUNET_SCHEDULER_NO_TASK, tv, wrs, wws,
533 &server_v4_run, plugin);
535 if (daemon_handle == plugin->server_v6)
537 if (plugin->server_v6_task != GNUNET_SCHEDULER_NO_TASK)
539 GNUNET_SCHEDULER_cancel (plugin->server_v6_task);
540 plugin->server_v6_task = GNUNET_SCHEDULER_NO_TASK;
544 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
545 GNUNET_SCHEDULER_NO_TASK, tv, wrs, wws,
546 &server_v6_run, plugin);
548 GNUNET_NETWORK_fdset_destroy (wrs);
549 GNUNET_NETWORK_fdset_destroy (wws);
550 GNUNET_NETWORK_fdset_destroy (wes);
555 server_start (struct Plugin *plugin)
560 res = server_load_certificate (plugin);
561 if (res == GNUNET_SYSERR)
563 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "TABORT\n");
568 plugin->server_v4 = NULL;
569 if (plugin->ipv4 == GNUNET_YES)
571 plugin->server_v4 = MHD_start_daemon (
578 MHD_NO_FLAG, plugin->port,
579 &server_accept_cb, plugin,
580 &server_access_cb, plugin,
581 //MHD_OPTION_SOCK_ADDR,
582 //(struct sockaddr_in *)
583 //plugin->bind4_address,
584 MHD_OPTION_CONNECTION_LIMIT,
586 plugin->max_connections,
588 MHD_OPTION_HTTPS_PRIORITIES,
590 MHD_OPTION_HTTPS_MEM_KEY,
592 MHD_OPTION_HTTPS_MEM_CERT,
595 MHD_OPTION_CONNECTION_TIMEOUT,
597 MHD_OPTION_CONNECTION_MEMORY_LIMIT,
599 GNUNET_SERVER_MAX_MESSAGE_SIZE),
600 MHD_OPTION_NOTIFY_COMPLETED,
601 &server_disconnect_cb, plugin,
602 MHD_OPTION_EXTERNAL_LOGGER,
603 server_log, NULL, MHD_OPTION_END);
604 if (plugin->server_v4 == NULL)
607 plugin->server_v6 = NULL;
608 if (plugin->ipv6 == GNUNET_YES)
610 plugin->server_v6 = MHD_start_daemon (
617 MHD_USE_IPv6, plugin->port,
618 &server_accept_cb, plugin,
619 &server_access_cb, plugin,
620 //MHD_OPTION_SOCK_ADDR,
622 MHD_OPTION_CONNECTION_LIMIT,
624 plugin->max_connections,
626 MHD_OPTION_HTTPS_PRIORITIES,
628 MHD_OPTION_HTTPS_MEM_KEY,
630 MHD_OPTION_HTTPS_MEM_CERT,
633 MHD_OPTION_CONNECTION_TIMEOUT,
635 MHD_OPTION_CONNECTION_MEMORY_LIMIT,
637 GNUNET_SERVER_MAX_MESSAGE_SIZE),
638 MHD_OPTION_NOTIFY_COMPLETED,
639 &server_disconnect_cb, plugin,
640 MHD_OPTION_EXTERNAL_LOGGER,
641 server_log, NULL, MHD_OPTION_END);
643 if (plugin->server_v6 == NULL)
647 if (plugin->server_v4 != NULL)
648 plugin->server_v4_task = server_schedule (plugin, plugin->server_v4);
649 if (plugin->server_v6 != NULL)
650 plugin->server_v6_task = server_schedule (plugin, plugin->server_v6);
653 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
654 "%s server component started on port %u\n", plugin->name,
661 server_stop (struct Plugin *plugin)
663 if (plugin->server_v4_task != GNUNET_SCHEDULER_NO_TASK)
665 GNUNET_SCHEDULER_cancel (plugin->server_v4_task);
666 plugin->server_v4_task = GNUNET_SCHEDULER_NO_TASK;
669 if (plugin->server_v6_task != GNUNET_SCHEDULER_NO_TASK)
671 GNUNET_SCHEDULER_cancel (plugin->server_v6_task);
672 plugin->server_v6_task = GNUNET_SCHEDULER_NO_TASK;
675 if (plugin->server_v4 != NULL)
677 MHD_stop_daemon (plugin->server_v4);
678 plugin->server_v4 = NULL;
680 if (plugin->server_v6 != NULL)
682 MHD_stop_daemon (plugin->server_v6);
683 plugin->server_v6 = NULL;
687 GNUNET_free_non_null (plugin->crypto_init);
688 GNUNET_free_non_null (plugin->cert);
689 GNUNET_free_non_null (plugin->key);
693 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
694 "%s server component stopped\n", plugin->name);
700 /* end of plugin_transport_http.c */