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'\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'\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;
401 server_disconnect_cb (void *cls, struct MHD_Connection *connection,
402 void **httpSessionCache)
404 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "server: server_disconnect_cb\n");
406 struct Session *s = *httpSessionCache;
410 notify_session_end(s->plugin, &s->target, s);
416 server_disconnect (struct Session *s)
422 server_send (struct Session *s, const char *msgbuf, size_t msgbuf_size)
428 * Function that queries MHD's select sets and
429 * starts the task waiting for them.
430 * @param plugin plugin
431 * @param daemon_handle the MHD daemon handle
432 * @return gnunet task identifier
434 static GNUNET_SCHEDULER_TaskIdentifier
435 server_schedule (struct Plugin *plugin, struct MHD_Daemon *daemon_handle);
438 * Call MHD IPv4 to process pending requests and then go back
439 * and schedule the next run.
440 * @param cls plugin as closure
441 * @param tc task context
444 server_v4_run (void *cls,
445 const struct GNUNET_SCHEDULER_TaskContext *tc)
447 struct Plugin *plugin = cls;
448 GNUNET_assert (cls != NULL);
450 plugin->server_v4_task = GNUNET_SCHEDULER_NO_TASK;
452 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
455 GNUNET_assert (MHD_YES == MHD_run (plugin->server_v4));
456 plugin->server_v4_task = server_schedule (plugin, plugin->server_v4);
461 * Call MHD IPv6 to process pending requests and then go back
462 * and schedule the next run.
463 * @param cls plugin as closure
464 * @param tc task context
467 server_v6_run (void *cls,
468 const struct GNUNET_SCHEDULER_TaskContext *tc)
470 struct Plugin *plugin = cls;
471 GNUNET_assert (cls != NULL);
473 plugin->server_v6_task = GNUNET_SCHEDULER_NO_TASK;
475 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
478 GNUNET_assert (MHD_YES == MHD_run (plugin->server_v6));
479 plugin->server_v6_task = server_schedule (plugin, plugin->server_v6);
483 * Function that queries MHD's select sets and
484 * starts the task waiting for them.
485 * @param plugin plugin
486 * @param daemon_handle the MHD daemon handle
487 * @return gnunet task identifier
489 static GNUNET_SCHEDULER_TaskIdentifier
490 server_schedule (struct Plugin *plugin, struct MHD_Daemon *daemon_handle)
492 GNUNET_SCHEDULER_TaskIdentifier ret;
496 struct GNUNET_NETWORK_FDSet *wrs;
497 struct GNUNET_NETWORK_FDSet *wws;
498 struct GNUNET_NETWORK_FDSet *wes;
500 unsigned long long timeout;
502 struct GNUNET_TIME_Relative tv;
504 ret = GNUNET_SCHEDULER_NO_TASK;
508 wrs = GNUNET_NETWORK_fdset_create ();
509 wes = GNUNET_NETWORK_fdset_create ();
510 wws = GNUNET_NETWORK_fdset_create ();
512 GNUNET_assert (MHD_YES == MHD_get_fdset (daemon_handle, &rs, &ws, &es, &max));
513 haveto = MHD_get_timeout (daemon_handle, &timeout);
514 if (haveto == MHD_YES)
515 tv.rel_value = (uint64_t) timeout;
517 tv = GNUNET_TIME_UNIT_SECONDS;
518 GNUNET_NETWORK_fdset_copy_native (wrs, &rs, max + 1);
519 GNUNET_NETWORK_fdset_copy_native (wws, &ws, max + 1);
520 GNUNET_NETWORK_fdset_copy_native (wes, &es, max + 1);
521 if (daemon_handle == plugin->server_v4)
523 if (plugin->server_v4_task != GNUNET_SCHEDULER_NO_TASK)
525 GNUNET_SCHEDULER_cancel (plugin->server_v4_task);
526 plugin->server_v4_task = GNUNET_SCHEDULER_NO_TASK;
530 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
531 GNUNET_SCHEDULER_NO_TASK, tv, wrs, wws,
532 &server_v4_run, plugin);
534 if (daemon_handle == plugin->server_v6)
536 if (plugin->server_v6_task != GNUNET_SCHEDULER_NO_TASK)
538 GNUNET_SCHEDULER_cancel (plugin->server_v6_task);
539 plugin->server_v6_task = GNUNET_SCHEDULER_NO_TASK;
543 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
544 GNUNET_SCHEDULER_NO_TASK, tv, wrs, wws,
545 &server_v6_run, plugin);
547 GNUNET_NETWORK_fdset_destroy (wrs);
548 GNUNET_NETWORK_fdset_destroy (wws);
549 GNUNET_NETWORK_fdset_destroy (wes);
554 server_start (struct Plugin *plugin)
559 res = server_load_certificate (plugin);
560 if (res == GNUNET_SYSERR)
562 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "TABORT\n");
567 plugin->server_v4 = NULL;
568 if (plugin->ipv4 == GNUNET_YES)
570 plugin->server_v4 = MHD_start_daemon (
577 MHD_NO_FLAG, plugin->port,
578 &server_accept_cb, plugin,
579 &server_access_cb, plugin,
580 //MHD_OPTION_SOCK_ADDR,
581 //(struct sockaddr_in *)
582 //plugin->bind4_address,
583 MHD_OPTION_CONNECTION_LIMIT,
585 plugin->max_connections,
587 MHD_OPTION_HTTPS_PRIORITIES,
589 MHD_OPTION_HTTPS_MEM_KEY,
591 MHD_OPTION_HTTPS_MEM_CERT,
594 MHD_OPTION_CONNECTION_TIMEOUT,
596 MHD_OPTION_CONNECTION_MEMORY_LIMIT,
598 GNUNET_SERVER_MAX_MESSAGE_SIZE),
599 MHD_OPTION_NOTIFY_COMPLETED,
600 &server_disconnect_cb, plugin,
601 MHD_OPTION_EXTERNAL_LOGGER,
602 server_log, NULL, MHD_OPTION_END);
603 if (plugin->server_v4 == NULL)
606 plugin->server_v6 = NULL;
607 if (plugin->ipv6 == GNUNET_YES)
609 plugin->server_v6 = MHD_start_daemon (
616 MHD_USE_IPv6, plugin->port,
617 &server_accept_cb, plugin,
618 &server_access_cb, plugin,
619 //MHD_OPTION_SOCK_ADDR,
621 MHD_OPTION_CONNECTION_LIMIT,
623 plugin->max_connections,
625 MHD_OPTION_HTTPS_PRIORITIES,
627 MHD_OPTION_HTTPS_MEM_KEY,
629 MHD_OPTION_HTTPS_MEM_CERT,
632 MHD_OPTION_CONNECTION_TIMEOUT,
634 MHD_OPTION_CONNECTION_MEMORY_LIMIT,
636 GNUNET_SERVER_MAX_MESSAGE_SIZE),
637 MHD_OPTION_NOTIFY_COMPLETED,
638 &server_disconnect_cb, plugin,
639 MHD_OPTION_EXTERNAL_LOGGER,
640 server_log, NULL, MHD_OPTION_END);
642 if (plugin->server_v6 == NULL)
646 if (plugin->server_v4 != NULL)
647 plugin->server_v4_task = server_schedule (plugin, plugin->server_v4);
648 if (plugin->server_v6 != NULL)
649 plugin->server_v6_task = server_schedule (plugin, plugin->server_v6);
652 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
653 "%s server component started on port %u\n", plugin->name,
660 server_stop (struct Plugin *plugin)
662 if (plugin->server_v4_task != GNUNET_SCHEDULER_NO_TASK)
664 GNUNET_SCHEDULER_cancel (plugin->server_v4_task);
665 plugin->server_v4_task = GNUNET_SCHEDULER_NO_TASK;
668 if (plugin->server_v6_task != GNUNET_SCHEDULER_NO_TASK)
670 GNUNET_SCHEDULER_cancel (plugin->server_v6_task);
671 plugin->server_v6_task = GNUNET_SCHEDULER_NO_TASK;
674 if (plugin->server_v4 != NULL)
676 MHD_stop_daemon (plugin->server_v4);
677 plugin->server_v4 = NULL;
679 if (plugin->server_v6 != NULL)
681 MHD_stop_daemon (plugin->server_v6);
682 plugin->server_v6 = NULL;
686 GNUNET_free_non_null (plugin->crypto_init);
687 GNUNET_free_non_null (plugin->cert);
688 GNUNET_free_non_null (plugin->key);
692 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
693 "%s server component stopped\n", plugin->name);
699 /* end of plugin_transport_http.c */