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);
43 struct ServerConnection
48 /* should this connection get disconnected? GNUNET_YES/NO */
51 struct Session *session;
52 struct MHD_Connection * mhd_conn;
56 * Check if incoming connection is accepted.
57 * NOTE: Here every connection is accepted
58 * @param cls plugin as closure
59 * @param addr address of incoming connection
60 * @param addr_len address length of incoming connection
61 * @return MHD_YES if connection is accepted, MHD_NO if connection is rejected
65 server_accept_cb (void *cls, const struct sockaddr *addr, socklen_t addr_len)
67 struct Plugin * plugin = cls;
69 if (plugin->cur_connections <= plugin->max_connections)
73 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "server: Cannot accept new connections\n");
80 * Callback called by MHD when it needs data to send
81 * @param cls current session
82 * @param pos position in buffer
83 * @param buf the buffer to write data to
84 * @param max max number of bytes available in buffer
85 * @return bytes written to buffer
89 server_send_cb (void *cls, uint64_t pos, char *buf, size_t max)
99 server_load_file (const char *file)
101 struct GNUNET_DISK_FileHandle *gn_file;
105 if (0 != STAT (file, &fstat))
107 text = GNUNET_malloc (fstat.st_size + 1);
109 GNUNET_DISK_file_open (file, GNUNET_DISK_OPEN_READ,
110 GNUNET_DISK_PERM_USER_READ);
116 if (GNUNET_SYSERR == GNUNET_DISK_file_read (gn_file, text, fstat.st_size))
119 GNUNET_DISK_file_close (gn_file);
122 text[fstat.st_size] = '\0';
123 GNUNET_DISK_file_close (gn_file);
132 server_load_certificate (struct Plugin *plugin)
139 /* Get crypto init string from config
140 * If not present just use default values */
141 GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg, plugin->name,
142 "CRYPTO_INIT", &plugin->crypto_init);
145 GNUNET_CONFIGURATION_get_value_filename (plugin->env->cfg, plugin->name,
146 "KEY_FILE", &key_file))
148 key_file = "https_key.key";
152 GNUNET_CONFIGURATION_get_value_filename (plugin->env->cfg, plugin->name,
153 "CERT_FILE", &cert_file))
155 cert_file = "https_cert.crt";
158 /* read key & certificates from file */
160 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
161 "Loading TLS certificate from key-file `%s' cert-file`%s'\n",
162 key_file, cert_file);
165 plugin->key = server_load_file (key_file);
166 plugin->cert = server_load_file (cert_file);
168 if ((plugin->key == NULL) || (plugin->cert == NULL))
170 struct GNUNET_OS_Process *cert_creation;
172 GNUNET_free_non_null (plugin->key);
174 GNUNET_free_non_null (plugin->cert);
178 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
179 "No usable TLS certificate found, creating certificate\n");
183 GNUNET_OS_start_process (NULL, NULL,
184 "gnunet-transport-certificate-creation",
185 "gnunet-transport-certificate-creation",
186 key_file, cert_file, NULL);
187 if (cert_creation == NULL)
189 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
191 ("Could not create a new TLS certificate, program `gnunet-transport-certificate-creation' could not be started!\n"));
192 GNUNET_free (key_file);
193 GNUNET_free (cert_file);
195 GNUNET_free_non_null (plugin->key);
196 GNUNET_free_non_null (plugin->cert);
197 GNUNET_free_non_null (plugin->crypto_init);
199 return GNUNET_SYSERR;
201 GNUNET_assert (GNUNET_OK == GNUNET_OS_process_wait (cert_creation));
202 GNUNET_OS_process_close (cert_creation);
204 plugin->key = server_load_file (key_file);
205 plugin->cert = server_load_file (cert_file);
208 if ((plugin->key == NULL) || (plugin->cert == NULL))
210 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
212 ("No usable TLS certificate found and creating one failed!\n"),
214 GNUNET_free (key_file);
215 GNUNET_free (cert_file);
217 GNUNET_free_non_null (plugin->key);
218 GNUNET_free_non_null (plugin->cert);
219 GNUNET_free_non_null (plugin->crypto_init);
221 return GNUNET_SYSERR;
223 GNUNET_free (key_file);
224 GNUNET_free (cert_file);
226 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "TLS certificate loaded\n");
235 * Process GET or PUT request received via MHD. For
236 * GET, queue response that will send back our pending
237 * messages. For PUT, process incoming data and send
238 * to GNUnet core. In either case, check if a session
239 * already exists and create a new one if not.
242 server_access_cb (void *cls, struct MHD_Connection *mhd_connection,
243 const char *url, const char *method, const char *version,
244 const char *upload_data, size_t * upload_data_size,
245 void **httpSessionCache)
247 struct Plugin *plugin = cls;
248 struct ServerConnection *sc = *httpSessionCache;
249 struct Session *s = NULL;
252 struct MHD_Response *response;
254 GNUNET_assert (cls != NULL);
259 const union MHD_ConnectionInfo *conn_info;
261 struct GNUNET_PeerIdentity target;
262 int check = GNUNET_NO;
266 conn_info = MHD_get_connection_info (mhd_connection, MHD_CONNECTION_INFO_CLIENT_ADDRESS);
267 if (conn_info->client_addr->sa_family == AF_INET)
268 addrlen = sizeof (struct sockaddr_in);
269 else if (conn_info->client_addr->sa_family == AF_INET6)
270 addrlen = sizeof (struct sockaddr_in6);
274 if ((strlen(&url[1]) >= 105) && (url[104] == ';'))
277 char * tagc = (char *) &url[105];
278 memcpy(&hash, &url[1], 103);
280 if (GNUNET_OK == GNUNET_CRYPTO_hash_from_string ((const char *) &hash, &(target.hashPubKey)))
282 tag = strtoul (tagc, NULL, 10);
288 if (0 == strcmp (MHD_HTTP_METHOD_PUT, method))
289 direction = _RECEIVE;
290 if (0 == strcmp (MHD_HTTP_METHOD_GET, method))
293 if (check == GNUNET_NO)
296 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "server: New inbound connection from %s with tag %u\n", GNUNET_h2s_full(&(target.hashPubKey)), tag);
298 /* find duplicate session */
304 if ((t->inbound) && (0 == memcmp (&t->target, &target, sizeof (struct GNUNET_PeerIdentity))) &&
305 /* FIXME add source address comparison */
313 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "server: Duplicate session, dismissing new connection from peer `%s'\n", GNUNET_i2s (&target));
318 /* find semi-session */
319 t = plugin->server_semi_head;
323 /* FIXME add source address comparison */
324 if ((0 == memcmp (&t->target, &target, sizeof (struct GNUNET_PeerIdentity))) &&
336 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "server: Found existing semi-session for `%s'\n", GNUNET_i2s (&target));
339 if ((direction == _SEND) && (t->server_send != NULL))
342 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "server: Duplicate GET session, dismissing new connection from peer `%s'\n", GNUNET_i2s (&target));
349 GNUNET_CONTAINER_DLL_remove(plugin->server_semi_head, plugin->server_semi_tail, s);
350 GNUNET_CONTAINER_DLL_insert(plugin->head, plugin->tail, s);
352 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "server: Found matching semi-session, merging session for peer `%s'\n", GNUNET_i2s (&target));
357 if ((direction == _RECEIVE) && (t->server_recv != NULL))
360 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "server: Duplicate PUT session, dismissing new connection from peer `%s'\n", GNUNET_i2s (&target));
367 GNUNET_CONTAINER_DLL_remove(plugin->server_semi_head, plugin->server_semi_tail, s);
368 GNUNET_CONTAINER_DLL_insert(plugin->head, plugin->tail, s);
370 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "server: Found matching semi-session, merging session for peer `%s'\n", GNUNET_i2s (&target));
376 /* create new session */
378 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "server: Creating new session for peer `%s' \n", GNUNET_i2s (&target));
381 s = create_session(plugin,
383 conn_info->client_addr,
388 s->inbound = GNUNET_YES;
390 if (0 == strcmp (MHD_HTTP_METHOD_PUT, method))
392 if (0 == strcmp (MHD_HTTP_METHOD_GET, method))
394 GNUNET_CONTAINER_DLL_insert (plugin->server_semi_head, plugin->server_semi_tail, s);
398 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "server: Invalid connection request\n");
399 response = MHD_create_response_from_data (strlen (HTTP_ERROR_RESPONSE),HTTP_ERROR_RESPONSE, MHD_NO, MHD_NO);
400 res = MHD_queue_response (mhd_connection, MHD_HTTP_NOT_FOUND, response);
401 MHD_destroy_response (response);
408 sc = GNUNET_malloc (sizeof (struct ServerConnection));
409 sc->mhd_conn = mhd_connection;
410 sc->direction = direction;
412 if (direction == _SEND)
414 if (direction == _RECEIVE)
417 (*httpSessionCache) = sc;
420 /* existing connection */
421 sc = (*httpSessionCache);
424 /* connection is to be disconnected*/
425 if (sc->disconnect == GNUNET_YES)
427 response = MHD_create_response_from_data (strlen ("Thank you!"), "Thank you!", MHD_NO, MHD_NO);
428 res = MHD_queue_response (mhd_connection, MHD_HTTP_OK, response);
430 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
431 "Sent HTTP/1.1: 200 OK as PUT Response\n");
433 MHD_destroy_response (response);
441 server_disconnect_cb (void *cls, struct MHD_Connection *connection,
442 void **httpSessionCache)
444 struct ServerConnection *sc = *httpSessionCache;
445 struct ServerConnection *tc = *httpSessionCache;
446 struct Session * s = NULL;
447 struct Session * t = NULL;
448 struct Plugin * plugin = NULL;
455 if (sc->direction == _SEND)
458 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
459 "Server: peer `%s' PUT on address `%s' disconnected\n",
460 GNUNET_i2s (&s->target), GNUNET_a2s (s->addr, s->addrlen));
462 s->server_send = NULL;
463 /* FIXME miminimize timeout here */
464 if (s->server_recv != NULL)
467 tc->disconnect = GNUNET_YES;
470 if (sc->direction == _RECEIVE)
473 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
474 "Server: peer `%s' GET on address `%s' disconnected\n",
475 GNUNET_i2s (&s->target), GNUNET_a2s (s->addr, s->addrlen));
477 s->server_recv = NULL;
479 if (s->server_send != NULL)
482 tc->disconnect = GNUNET_YES;
487 t = plugin->server_semi_head;
492 GNUNET_CONTAINER_DLL_remove(plugin->server_semi_head, plugin->server_semi_tail, s);
493 GNUNET_CONTAINER_DLL_insert(plugin->head, plugin->tail, s);
499 if ((s->server_send == NULL) && (s->server_recv == NULL))
502 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
503 "Server: peer `%s' on address `%s' disconnected\n",
504 GNUNET_i2s (&s->target), GNUNET_a2s (s->addr, s->addrlen));
506 notify_session_end(s->plugin, &s->target, s);
511 server_disconnect (struct Session *s)
513 struct Plugin *plugin = s->plugin;
514 struct Session *t = plugin->head;
518 if (t->inbound == GNUNET_YES)
520 if (t->server_send != NULL)
522 ((struct ServerConnection *) t->server_send)->disconnect = GNUNET_YES;
524 if (t->server_send != NULL)
526 ((struct ServerConnection *) t->server_send)->disconnect = GNUNET_YES;
537 server_send (struct Session *s, const char *msgbuf, size_t msgbuf_size)
543 * Function that queries MHD's select sets and
544 * starts the task waiting for them.
545 * @param plugin plugin
546 * @param daemon_handle the MHD daemon handle
547 * @return gnunet task identifier
549 static GNUNET_SCHEDULER_TaskIdentifier
550 server_schedule (struct Plugin *plugin, struct MHD_Daemon *daemon_handle);
553 * Call MHD IPv4 to process pending requests and then go back
554 * and schedule the next run.
555 * @param cls plugin as closure
556 * @param tc task context
559 server_v4_run (void *cls,
560 const struct GNUNET_SCHEDULER_TaskContext *tc)
562 struct Plugin *plugin = cls;
563 GNUNET_assert (cls != NULL);
565 plugin->server_v4_task = GNUNET_SCHEDULER_NO_TASK;
567 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
570 GNUNET_assert (MHD_YES == MHD_run (plugin->server_v4));
571 plugin->server_v4_task = server_schedule (plugin, plugin->server_v4);
576 * Call MHD IPv6 to process pending requests and then go back
577 * and schedule the next run.
578 * @param cls plugin as closure
579 * @param tc task context
582 server_v6_run (void *cls,
583 const struct GNUNET_SCHEDULER_TaskContext *tc)
585 struct Plugin *plugin = cls;
586 GNUNET_assert (cls != NULL);
588 plugin->server_v6_task = GNUNET_SCHEDULER_NO_TASK;
590 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
593 GNUNET_assert (MHD_YES == MHD_run (plugin->server_v6));
594 plugin->server_v6_task = server_schedule (plugin, plugin->server_v6);
598 * Function that queries MHD's select sets and
599 * starts the task waiting for them.
600 * @param plugin plugin
601 * @param daemon_handle the MHD daemon handle
602 * @return gnunet task identifier
604 static GNUNET_SCHEDULER_TaskIdentifier
605 server_schedule (struct Plugin *plugin, struct MHD_Daemon *daemon_handle)
607 GNUNET_SCHEDULER_TaskIdentifier ret;
611 struct GNUNET_NETWORK_FDSet *wrs;
612 struct GNUNET_NETWORK_FDSet *wws;
613 struct GNUNET_NETWORK_FDSet *wes;
615 unsigned long long timeout;
617 struct GNUNET_TIME_Relative tv;
619 ret = GNUNET_SCHEDULER_NO_TASK;
623 wrs = GNUNET_NETWORK_fdset_create ();
624 wes = GNUNET_NETWORK_fdset_create ();
625 wws = GNUNET_NETWORK_fdset_create ();
627 GNUNET_assert (MHD_YES == MHD_get_fdset (daemon_handle, &rs, &ws, &es, &max));
628 haveto = MHD_get_timeout (daemon_handle, &timeout);
629 if (haveto == MHD_YES)
630 tv.rel_value = (uint64_t) timeout;
632 tv = GNUNET_TIME_UNIT_SECONDS;
633 GNUNET_NETWORK_fdset_copy_native (wrs, &rs, max + 1);
634 GNUNET_NETWORK_fdset_copy_native (wws, &ws, max + 1);
635 GNUNET_NETWORK_fdset_copy_native (wes, &es, max + 1);
636 if (daemon_handle == plugin->server_v4)
638 if (plugin->server_v4_task != GNUNET_SCHEDULER_NO_TASK)
640 GNUNET_SCHEDULER_cancel (plugin->server_v4_task);
641 plugin->server_v4_task = GNUNET_SCHEDULER_NO_TASK;
645 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
646 GNUNET_SCHEDULER_NO_TASK, tv, wrs, wws,
647 &server_v4_run, plugin);
649 if (daemon_handle == plugin->server_v6)
651 if (plugin->server_v6_task != GNUNET_SCHEDULER_NO_TASK)
653 GNUNET_SCHEDULER_cancel (plugin->server_v6_task);
654 plugin->server_v6_task = GNUNET_SCHEDULER_NO_TASK;
658 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
659 GNUNET_SCHEDULER_NO_TASK, tv, wrs, wws,
660 &server_v6_run, plugin);
662 GNUNET_NETWORK_fdset_destroy (wrs);
663 GNUNET_NETWORK_fdset_destroy (wws);
664 GNUNET_NETWORK_fdset_destroy (wes);
669 server_start (struct Plugin *plugin)
674 res = server_load_certificate (plugin);
675 if (res == GNUNET_SYSERR)
677 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "TABORT\n");
682 plugin->server_v4 = NULL;
683 if (plugin->ipv4 == GNUNET_YES)
685 plugin->server_v4 = MHD_start_daemon (
692 MHD_NO_FLAG, plugin->port,
693 &server_accept_cb, plugin,
694 &server_access_cb, plugin,
695 //MHD_OPTION_SOCK_ADDR,
696 //(struct sockaddr_in *)
697 //plugin->bind4_address,
698 MHD_OPTION_CONNECTION_LIMIT,
700 plugin->max_connections,
702 MHD_OPTION_HTTPS_PRIORITIES,
704 MHD_OPTION_HTTPS_MEM_KEY,
706 MHD_OPTION_HTTPS_MEM_CERT,
709 MHD_OPTION_CONNECTION_TIMEOUT,
711 MHD_OPTION_CONNECTION_MEMORY_LIMIT,
713 GNUNET_SERVER_MAX_MESSAGE_SIZE),
714 MHD_OPTION_NOTIFY_COMPLETED,
715 &server_disconnect_cb, plugin,
716 MHD_OPTION_EXTERNAL_LOGGER,
717 server_log, NULL, MHD_OPTION_END);
718 if (plugin->server_v4 == NULL)
721 plugin->server_v6 = NULL;
722 if (plugin->ipv6 == GNUNET_YES)
724 plugin->server_v6 = MHD_start_daemon (
731 MHD_USE_IPv6, plugin->port,
732 &server_accept_cb, plugin,
733 &server_access_cb, plugin,
734 //MHD_OPTION_SOCK_ADDR,
736 MHD_OPTION_CONNECTION_LIMIT,
738 plugin->max_connections,
740 MHD_OPTION_HTTPS_PRIORITIES,
742 MHD_OPTION_HTTPS_MEM_KEY,
744 MHD_OPTION_HTTPS_MEM_CERT,
747 MHD_OPTION_CONNECTION_TIMEOUT,
749 MHD_OPTION_CONNECTION_MEMORY_LIMIT,
751 GNUNET_SERVER_MAX_MESSAGE_SIZE),
752 MHD_OPTION_NOTIFY_COMPLETED,
753 &server_disconnect_cb, plugin,
754 MHD_OPTION_EXTERNAL_LOGGER,
755 server_log, NULL, MHD_OPTION_END);
757 if (plugin->server_v6 == NULL)
761 if (plugin->server_v4 != NULL)
762 plugin->server_v4_task = server_schedule (plugin, plugin->server_v4);
763 if (plugin->server_v6 != NULL)
764 plugin->server_v6_task = server_schedule (plugin, plugin->server_v6);
767 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
768 "%s server component started on port %u\n", plugin->name,
775 server_stop (struct Plugin *plugin)
777 struct Session *s = NULL;
778 struct Session *t = NULL;
780 if (plugin->server_v4_task != GNUNET_SCHEDULER_NO_TASK)
782 GNUNET_SCHEDULER_cancel (plugin->server_v4_task);
783 plugin->server_v4_task = GNUNET_SCHEDULER_NO_TASK;
786 if (plugin->server_v6_task != GNUNET_SCHEDULER_NO_TASK)
788 GNUNET_SCHEDULER_cancel (plugin->server_v6_task);
789 plugin->server_v6_task = GNUNET_SCHEDULER_NO_TASK;
792 if (plugin->server_v4 != NULL)
794 MHD_stop_daemon (plugin->server_v4);
795 plugin->server_v4 = NULL;
797 if (plugin->server_v6 != NULL)
799 MHD_stop_daemon (plugin->server_v6);
800 plugin->server_v6 = NULL;
803 /* cleaning up semi-sessions never propagated */
804 s = plugin->server_semi_head;
813 GNUNET_free_non_null (plugin->crypto_init);
814 GNUNET_free_non_null (plugin->cert);
815 GNUNET_free_non_null (plugin->key);
819 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
820 "%s server component stopped\n", plugin->name);
826 /* end of plugin_transport_http.c */