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>"
33 static struct Plugin * p;
36 * Function that queries MHD's select sets and
37 * starts the task waiting for them.
38 * @param plugin plugin
39 * @param daemon_handle the MHD daemon handle
40 * @param now schedule now or with MHD delay
41 * @return gnunet task identifier
43 static GNUNET_SCHEDULER_TaskIdentifier
44 server_schedule (struct Plugin *plugin,
45 struct MHD_Daemon *daemon_handle,
49 server_log (void *arg, const char *fmt, va_list ap)
53 vsnprintf (text, sizeof (text), fmt, ap);
55 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Server: %s\n", text);
59 * Check if incoming connection is accepted.
60 * NOTE: Here every connection is accepted
61 * @param cls plugin as closure
62 * @param addr address of incoming connection
63 * @param addr_len address length of incoming connection
64 * @return MHD_YES if connection is accepted, MHD_NO if connection is rejected
68 server_accept_cb (void *cls, const struct sockaddr *addr, socklen_t addr_len)
70 struct Plugin *plugin = cls;
72 if (plugin->cur_connections <= plugin->max_connections)
76 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
77 "Server: Cannot accept new connections\n");
85 server_load_file (const char *file)
87 struct GNUNET_DISK_FileHandle *gn_file;
91 if (GNUNET_OK != GNUNET_DISK_file_size (file,
92 &fsize, GNUNET_NO, GNUNET_YES))
94 text = GNUNET_malloc (fsize + 1);
96 GNUNET_DISK_file_open (file, GNUNET_DISK_OPEN_READ,
97 GNUNET_DISK_PERM_USER_READ);
103 if (GNUNET_SYSERR == GNUNET_DISK_file_read (gn_file, text, fsize))
106 GNUNET_DISK_file_close (gn_file);
110 GNUNET_DISK_file_close (gn_file);
119 server_load_certificate (struct Plugin *plugin)
126 /* Get crypto init string from config
127 * If not present just use default values */
129 GNUNET_assert (GNUNET_OK ==
130 GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg,
133 &plugin->crypto_init));
136 GNUNET_CONFIGURATION_get_value_filename (plugin->env->cfg, plugin->name,
137 "KEY_FILE", &key_file))
139 key_file = GNUNET_strdup ("https_key.key");
143 GNUNET_CONFIGURATION_get_value_filename (plugin->env->cfg, plugin->name,
144 "CERT_FILE", &cert_file))
146 GNUNET_asprintf (&cert_file, "%s", "https_cert.crt");
149 /* read key & certificates from file */
150 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
151 "Loading TLS certificate from key-file `%s' cert-file`%s'\n",
152 key_file, cert_file);
154 plugin->key = server_load_file (key_file);
155 plugin->cert = server_load_file (cert_file);
157 if ((plugin->key == NULL) || (plugin->cert == NULL))
159 struct GNUNET_OS_Process *cert_creation;
161 GNUNET_free_non_null (plugin->key);
163 GNUNET_free_non_null (plugin->cert);
167 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
168 "No usable TLS certificate found, creating certificate\n");
172 GNUNET_OS_start_process (GNUNET_NO, NULL, NULL,
173 "gnunet-transport-certificate-creation",
174 "gnunet-transport-certificate-creation",
175 key_file, cert_file, NULL);
176 if (cert_creation == NULL)
178 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
180 ("Could not create a new TLS certificate, program `gnunet-transport-certificate-creation' could not be started!\n"));
181 GNUNET_free (key_file);
182 GNUNET_free (cert_file);
184 GNUNET_free_non_null (plugin->key);
186 GNUNET_free_non_null (plugin->cert);
188 GNUNET_free_non_null (plugin->crypto_init);
189 plugin->crypto_init = NULL;
191 return GNUNET_SYSERR;
193 GNUNET_assert (GNUNET_OK == GNUNET_OS_process_wait (cert_creation));
194 GNUNET_OS_process_destroy (cert_creation);
196 plugin->key = server_load_file (key_file);
197 plugin->cert = server_load_file (cert_file);
200 if ((plugin->key == NULL) || (plugin->cert == NULL))
202 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
204 ("No usable TLS certificate found and creating one failed!\n"),
206 GNUNET_free (key_file);
207 GNUNET_free (cert_file);
209 GNUNET_free_non_null (plugin->key);
211 GNUNET_free_non_null (plugin->cert);
213 GNUNET_free_non_null (plugin->crypto_init);
214 plugin->crypto_init = NULL;
216 return GNUNET_SYSERR;
218 GNUNET_free (key_file);
219 GNUNET_free (cert_file);
220 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "TLS certificate loaded\n");
227 * Reschedule the execution of both IPv4 and IPv6 server
228 * @param plugin the plugin
229 * @param server which server to schedule v4 or v6?
230 * @param now GNUNET_YES to schedule execution immediately, GNUNET_NO to wait
235 server_reschedule (struct Plugin *plugin, struct MHD_Daemon *server, int now)
237 if ((server == plugin->server_v4) && (plugin->server_v4 != NULL))
239 if (GNUNET_YES == plugin->server_v4_immediately)
240 return; /* No rescheduling, server will run asap */
242 if (GNUNET_YES == now)
243 plugin->server_v4_immediately = GNUNET_YES;
245 if (plugin->server_v4_task != GNUNET_SCHEDULER_NO_TASK)
247 GNUNET_SCHEDULER_cancel (plugin->server_v4_task);
248 plugin->server_v4_task = GNUNET_SCHEDULER_NO_TASK;
250 plugin->server_v4_task = server_schedule (plugin, plugin->server_v4, now);
253 if ((server == plugin->server_v6) && (plugin->server_v6 != NULL))
255 if (GNUNET_YES == plugin->server_v6_immediately)
256 return; /* No rescheduling, server will run asap */
258 if (GNUNET_YES == now)
259 plugin->server_v6_immediately = GNUNET_YES;
261 if (plugin->server_v6_task != GNUNET_SCHEDULER_NO_TASK)
263 GNUNET_SCHEDULER_cancel (plugin->server_v6_task);
264 plugin->server_v6_task = GNUNET_SCHEDULER_NO_TASK;
266 plugin->server_v6_task = server_schedule (plugin, plugin->server_v6, now);
271 * Callback called by MessageStreamTokenizer when a message has arrived
272 * @param cls current session as closure
273 * @param client clien
274 * @param message the message to be forwarded to transport service
277 server_receive_mst_cb (void *cls, void *client,
278 const struct GNUNET_MessageHeader *message)
280 struct Session *s = cls;
282 GNUNET_assert (NULL != p);
283 if (GNUNET_NO == exist_session(p, s))
286 struct Plugin *plugin = s->plugin;
287 struct GNUNET_TIME_Relative delay;
289 delay = http_plugin_receive (s, &s->target, message, s, s->addr, s->addrlen);
292 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (), delay);
294 if (delay.rel_value > 0)
296 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
297 "Server: peer `%s' address `%s' next read delayed for %llu ms\n",
298 GNUNET_i2s (&s->target),
299 http_plugin_address_to_string (NULL, s->addr, s->addrlen),
306 * Callback called by MHD when it needs data to send
307 * @param cls current session
308 * @param pos position in buffer
309 * @param buf the buffer to write data to
310 * @param max max number of bytes available in buffer
311 * @return bytes written to buffer
314 server_send_callback (void *cls, uint64_t pos, char *buf, size_t max)
316 struct Session *s = cls;
317 GNUNET_assert (NULL != p);
318 if (GNUNET_NO == exist_session(p, s))
321 struct HTTP_Message *msg;
329 if ((msg->size - msg->pos) <= max)
331 memcpy (buf, &msg->buf[msg->pos], (msg->size - msg->pos));
332 bytes_read = msg->size - msg->pos;
333 msg->pos += (msg->size - msg->pos);
337 memcpy (buf, &msg->buf[msg->pos], max);
342 /* removing message */
343 if (msg->pos == msg->size)
345 if (NULL != msg->transmit_cont)
346 msg->transmit_cont (msg->transmit_cont_cls, &s->target, GNUNET_OK);
347 GNUNET_CONTAINER_DLL_remove (s->msg_head, s->msg_tail, msg);
352 struct Plugin *plugin = s->plugin;
353 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
354 "Server: %X: sent %u bytes\n", s, bytes_read);
359 static struct Session *
360 server_lookup_session (struct Plugin *plugin,
361 struct ServerConnection * sc)
364 for (s = plugin->head; NULL != s; s = s->next)
366 if ((s->server_recv == sc) || (s->server_send == sc))
370 for (s = plugin->server_semi_head; NULL != s; s = s->next)
372 if ((s->server_recv == sc) || (s->server_send == sc))
379 static struct ServerConnection *
380 server_lookup_serverconnection (struct Plugin *plugin,
381 struct MHD_Connection *mhd_connection, const char *url,
384 struct Session *s = NULL;
386 struct ServerConnection *sc = NULL;
387 const union MHD_ConnectionInfo *conn_info;
388 struct GNUNET_ATS_Information ats;
389 struct IPv4HttpAddress a4;
390 struct IPv6HttpAddress a6;
391 struct sockaddr_in *s4;
392 struct sockaddr_in6 *s6;
395 struct GNUNET_PeerIdentity target;
396 int check = GNUNET_NO;
398 int direction = GNUNET_SYSERR;
400 conn_info = MHD_get_connection_info (mhd_connection,
401 MHD_CONNECTION_INFO_CLIENT_ADDRESS);
402 if ((conn_info->client_addr->sa_family != AF_INET) &&
403 (conn_info->client_addr->sa_family != AF_INET6))
406 if ((strlen (&url[1]) >= 105) && (url[104] == ';'))
409 char *tagc = (char *) &url[105];
411 memcpy (&hash, &url[1], 103);
414 GNUNET_CRYPTO_hash_from_string ((const char *) &hash,
415 &(target.hashPubKey)))
417 tag = strtoul (tagc, NULL, 10);
423 if (0 == strcmp (MHD_HTTP_METHOD_PUT, method))
424 direction = _RECEIVE;
425 else if (0 == strcmp (MHD_HTTP_METHOD_GET, method))
434 if (check == GNUNET_NO)
437 plugin->cur_connections++;
438 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
439 "Server: New %s connection from %s with tag %u\n",
441 GNUNET_i2s (&target), tag);
443 /* find duplicate session */
448 (0 == memcmp (&t->target, &target, sizeof (struct GNUNET_PeerIdentity)))
450 /* FIXME add source address comparison */
457 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
458 "Server: Duplicate session, dismissing new connection from peer `%s'\n",
459 GNUNET_i2s (&target));
463 /* find semi-session */
464 t = plugin->server_semi_head;
468 /* FIXME add source address comparison */
469 if ((0 == memcmp (&t->target, &target, sizeof (struct GNUNET_PeerIdentity)))
479 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
480 "Server: Found existing semi-session for `%s'\n",
481 GNUNET_i2s (&target));
483 if ((direction == _SEND) && (t->server_send != NULL))
485 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
486 "Server: Duplicate GET session, dismissing new connection from peer `%s'\n",
487 GNUNET_i2s (&target));
493 GNUNET_CONTAINER_DLL_remove (plugin->server_semi_head,
494 plugin->server_semi_tail, s);
495 GNUNET_CONTAINER_DLL_insert (plugin->head, plugin->tail, s);
496 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
497 "Server: Found matching semi-session, merging session for peer `%s'\n",
498 GNUNET_i2s (&target));
500 plugin->inbound_sessions ++;
501 GNUNET_STATISTICS_set (plugin->env->stats,
502 "# HTTP inbound sessions",
503 plugin->inbound_sessions,
505 GNUNET_assert (NULL != s);
508 if ((direction == _RECEIVE) && (t->server_recv != NULL))
510 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
511 "Server: Duplicate PUT session, dismissing new connection from peer `%s'\n",
512 GNUNET_i2s (&target));
518 GNUNET_CONTAINER_DLL_remove (plugin->server_semi_head,
519 plugin->server_semi_tail, s);
520 GNUNET_CONTAINER_DLL_insert (plugin->head, plugin->tail, s);
521 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
522 "Server: Found matching semi-session, merging session for peer `%s'\n",
523 GNUNET_i2s (&target));
524 plugin->inbound_sessions ++;
525 GNUNET_STATISTICS_set (plugin->env->stats,
526 "# HTTP inbound sessions",
527 plugin->inbound_sessions,
529 GNUNET_assert (NULL != s);
534 /* create new session */
535 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
536 "Server: Creating new session for peer `%s' \n",
537 GNUNET_i2s (&target));
538 switch (conn_info->client_addr->sa_family)
541 s4 = ((struct sockaddr_in *) conn_info->client_addr);
542 a4.u4_port = s4->sin_port;
543 memcpy (&a4.ipv4_addr, &s4->sin_addr, sizeof (struct in_addr));
545 a_len = sizeof (struct IPv4HttpAddress);
546 ats = plugin->env->get_address_type (plugin->env->cls, (const struct sockaddr *) s4, sizeof (struct sockaddr_in));
549 s6 = ((struct sockaddr_in6 *) conn_info->client_addr);
550 a6.u6_port = s6->sin6_port;
551 memcpy (&a6.ipv6_addr, &s6->sin6_addr, sizeof (struct in6_addr));
553 a_len = sizeof (struct IPv6HttpAddress);
554 ats = plugin->env->get_address_type (plugin->env->cls, (const struct sockaddr *) s6, sizeof (struct sockaddr_in6));
560 s = create_session (plugin, &target, a, a_len);
561 GNUNET_assert (NULL != s);
562 s->ats_address_network_type = ats.value;
563 s->inbound = GNUNET_YES;
564 s->next_receive = GNUNET_TIME_UNIT_ZERO_ABS;
566 s->server_recv = NULL;
567 s->server_send = NULL;
569 GNUNET_CONTAINER_DLL_insert (plugin->server_semi_head,
570 plugin->server_semi_tail, s);
574 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
575 "Server: Invalid connection request\n");
579 sc = GNUNET_malloc (sizeof (struct ServerConnection));
580 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Create %p\n", sc);
581 sc->mhd_conn = mhd_connection;
582 sc->direction = direction;
584 if (direction == _SEND)
586 if (direction == _RECEIVE)
589 #if MHD_VERSION >= 0x00090E00
590 int to = (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT.rel_value / 1000);
592 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
593 "Server: Setting timeout for %X to %u sec.\n", sc, to);
594 MHD_set_connection_option (mhd_connection, MHD_CONNECTION_OPTION_TIMEOUT, to);
596 struct MHD_Daemon *d = NULL;
598 if (s->addrlen == sizeof (struct IPv6HttpAddress))
599 d = plugin->server_v6;
600 if (s->addrlen == sizeof (struct IPv4HttpAddress))
601 d = plugin->server_v4;
603 server_reschedule (plugin, d, GNUNET_NO);
609 * Process GET or PUT request received via MHD. For
610 * GET, queue response that will send back our pending
611 * messages. For PUT, process incoming data and send
612 * to GNUnet core. In either case, check if a session
613 * already exists and create a new one if not.
616 server_access_cb (void *cls, struct MHD_Connection *mhd_connection,
617 const char *url, const char *method, const char *version,
618 const char *upload_data, size_t * upload_data_size,
619 void **httpSessionCache)
621 struct Plugin *plugin = cls;
622 struct ServerConnection *sc = *httpSessionCache;
624 struct MHD_Response *response;
627 GNUNET_assert (cls != NULL);
631 sc = server_lookup_serverconnection (plugin, mhd_connection, url, method);
633 (*httpSessionCache) = sc;
637 MHD_create_response_from_data (strlen (HTTP_ERROR_RESPONSE),
638 HTTP_ERROR_RESPONSE, MHD_NO, MHD_NO);
639 res = MHD_queue_response (mhd_connection, MHD_HTTP_NOT_FOUND, response);
640 MHD_destroy_response (response);
646 if (NULL == server_lookup_session (plugin, sc))
648 /* Session was already disconnected */
654 /* existing connection */
655 sc = (*httpSessionCache);
658 GNUNET_assert (NULL != s);
660 /* connection is to be disconnected */
661 if (sc->disconnect == GNUNET_YES)
663 /* Sent HTTP/1.1: 200 OK as PUT Response\ */
665 MHD_create_response_from_data (strlen ("Thank you!"), "Thank you!",
667 res = MHD_queue_response (mhd_connection, MHD_HTTP_OK, response);
668 MHD_destroy_response (response);
672 GNUNET_assert (s != NULL);
673 /* Check if both directions are connected */
674 if ((sc->session->server_recv == NULL) || (sc->session->server_send == NULL))
676 /* Delayed read from since not both semi-connections are connected */
680 if (sc->direction == _SEND)
683 MHD_create_response_from_callback (-1, 32 * 1024, &server_send_callback,
685 MHD_queue_response (mhd_connection, MHD_HTTP_OK, response);
686 MHD_destroy_response (response);
689 if (sc->direction == _RECEIVE)
691 if (*upload_data_size == 0)
693 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
694 "Server: Peer `%s' PUT on address `%s' connected\n",
695 GNUNET_i2s (&s->target),
696 http_plugin_address_to_string (NULL, s->addr,
702 if ((*upload_data_size > 0))
704 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
705 "Server: peer `%s' PUT on address `%s' received %u bytes\n",
706 GNUNET_i2s (&s->target),
707 http_plugin_address_to_string (NULL, s->addr,
710 struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get ();
712 if ((s->next_receive.abs_value <= now.abs_value))
714 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
715 "Server: %X: PUT with %u bytes forwarded to MST\n", s,
717 if (s->msg_tk == NULL)
719 s->msg_tk = GNUNET_SERVER_mst_create (&server_receive_mst_cb, s);
721 GNUNET_SERVER_mst_receive (s->msg_tk, s, upload_data,
722 *upload_data_size, GNUNET_NO, GNUNET_NO);
724 #if MHD_VERSION >= 0x00090E00
725 int to = (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT.rel_value / 1000);
726 struct ServerConnection *t = NULL;
728 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
729 "Server: Received %u bytes\n", *upload_data_size);
730 /* Setting timeouts for other connections */
731 if (s->server_recv != NULL)
734 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
735 "Server: Setting timeout for %X to %u sec.\n", t,
737 MHD_set_connection_option (t->mhd_conn, MHD_CONNECTION_OPTION_TIMEOUT,
740 if (s->server_send != NULL)
743 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
744 "Server: Setting timeout for %X to %u sec.\n", t,
746 MHD_set_connection_option (t->mhd_conn, MHD_CONNECTION_OPTION_TIMEOUT,
749 struct MHD_Daemon *d = NULL;
751 if (s->addrlen == sizeof (struct IPv6HttpAddress))
752 d = plugin->server_v6;
753 if (s->addrlen == sizeof (struct IPv4HttpAddress))
754 d = plugin->server_v4;
755 server_reschedule (plugin, d, GNUNET_NO);
757 (*upload_data_size) = 0;
761 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
762 "Server: %X no inbound bandwidth available! Next read was delayed by %llu ms\n",
763 s, now.abs_value - s->next_receive.abs_value);
774 server_disconnect_cb (void *cls, struct MHD_Connection *connection,
775 void **httpSessionCache)
777 struct ServerConnection *sc = *httpSessionCache;
778 struct ServerConnection *tc = NULL;
779 struct Session *s = NULL;
780 struct Session *t = NULL;
781 struct Plugin *plugin = NULL;
786 if (NULL == (s = server_lookup_session (p, sc)))
790 GNUNET_assert (NULL != s);
791 GNUNET_assert (NULL != p);
792 if (GNUNET_NO == exist_session(p, s))
796 if (sc->direction == _SEND)
799 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
800 "Server: %X peer `%s' GET on address `%s' disconnected\n",
801 s->server_send, GNUNET_i2s (&s->target),
802 http_plugin_address_to_string (NULL, s->addr, s->addrlen));
803 s->server_send = NULL;
804 if (NULL != (tc = s->server_recv))
806 tc->disconnect = GNUNET_YES;
807 GNUNET_assert (NULL != tc->mhd_conn);
808 #if MHD_VERSION >= 0x00090E00
809 MHD_set_connection_option (tc->mhd_conn, MHD_CONNECTION_OPTION_TIMEOUT,
814 if (sc->direction == _RECEIVE)
816 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
817 "Server: %X peer `%s' PUT on address `%s' disconnected\n",
818 s->server_recv, GNUNET_i2s (&s->target),
819 http_plugin_address_to_string (NULL, s->addr, s->addrlen));
820 s->server_recv = NULL;
821 if (NULL != (tc = s->server_send))
823 tc->disconnect = GNUNET_YES;
824 GNUNET_assert (NULL != tc->mhd_conn);
825 #if MHD_VERSION >= 0x00090E00
826 MHD_set_connection_option (tc->mhd_conn, MHD_CONNECTION_OPTION_TIMEOUT,
830 if (s->msg_tk != NULL)
832 GNUNET_SERVER_mst_destroy (s->msg_tk);
839 t = plugin->server_semi_head;
844 GNUNET_CONTAINER_DLL_remove (plugin->server_semi_head,
845 plugin->server_semi_tail, s);
846 GNUNET_CONTAINER_DLL_insert (plugin->head, plugin->tail, s);
851 plugin->cur_connections--;
853 struct MHD_Daemon *d = NULL;
855 if (s->addrlen == sizeof (struct IPv6HttpAddress))
856 d = plugin->server_v6;
857 if (s->addrlen == sizeof (struct IPv4HttpAddress))
858 d = plugin->server_v4;
859 server_reschedule (plugin, d, GNUNET_NO);
861 if ((s->server_send == NULL) && (s->server_recv == NULL))
863 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
864 "Server: peer `%s' on address `%s' disconnected\n",
865 GNUNET_i2s (&s->target),
866 http_plugin_address_to_string (NULL, s->addr, s->addrlen));
867 if (s->msg_tk != NULL)
869 GNUNET_SERVER_mst_destroy (s->msg_tk);
873 GNUNET_assert (plugin->inbound_sessions > 0);
874 plugin->inbound_sessions --;
875 GNUNET_STATISTICS_set (plugin->env->stats,
876 "# HTTP inbound sessions",
877 plugin->inbound_sessions, GNUNET_NO);
879 notify_session_end (s->plugin, &s->target, s);
884 server_disconnect (struct Session *s)
886 if (s->server_send != NULL)
888 ((struct ServerConnection *) s->server_send)->disconnect = GNUNET_YES;
890 if (s->server_recv != NULL)
892 ((struct ServerConnection *) s->server_recv)->disconnect = GNUNET_YES;
899 server_send (struct Session *s, struct HTTP_Message *msg)
901 GNUNET_CONTAINER_DLL_insert (s->msg_head, s->msg_tail, msg);
903 if (s->addrlen == sizeof (struct IPv4HttpAddress))
905 server_reschedule (s->plugin, s->plugin->server_v4, GNUNET_YES);
907 else if (s->addrlen == sizeof (struct IPv6HttpAddress))
909 server_reschedule (s->plugin, s->plugin->server_v6, GNUNET_YES);
912 return GNUNET_SYSERR;
919 * Call MHD IPv4 to process pending requests and then go back
920 * and schedule the next run.
921 * @param cls plugin as closure
922 * @param tc task context
925 server_v4_run (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
927 struct Plugin *plugin = cls;
929 GNUNET_assert (cls != NULL);
931 plugin->server_v4_task = GNUNET_SCHEDULER_NO_TASK;
932 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
935 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
936 "Running IPv4 server\n");
938 plugin->server_v4_immediately = GNUNET_NO;
939 GNUNET_assert (MHD_YES == MHD_run (plugin->server_v4));
940 server_reschedule (plugin, plugin->server_v4, GNUNET_NO);
945 * Call MHD IPv6 to process pending requests and then go back
946 * and schedule the next run.
947 * @param cls plugin as closure
948 * @param tc task context
951 server_v6_run (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
953 struct Plugin *plugin = cls;
955 GNUNET_assert (cls != NULL);
956 plugin->server_v6_task = GNUNET_SCHEDULER_NO_TASK;
957 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
960 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
961 "Running IPv6 server\n");
963 plugin->server_v6_immediately = GNUNET_NO;
964 GNUNET_assert (MHD_YES == MHD_run (plugin->server_v6));
965 server_reschedule (plugin, plugin->server_v6, GNUNET_NO);
969 * Function that queries MHD's select sets and
970 * starts the task waiting for them.
971 * @param plugin plugin
972 * @param daemon_handle the MHD daemon handle
973 * @return gnunet task identifier
975 static GNUNET_SCHEDULER_TaskIdentifier
976 server_schedule (struct Plugin *plugin, struct MHD_Daemon *daemon_handle,
979 GNUNET_SCHEDULER_TaskIdentifier ret;
983 struct GNUNET_NETWORK_FDSet *wrs;
984 struct GNUNET_NETWORK_FDSet *wws;
985 struct GNUNET_NETWORK_FDSet *wes;
987 unsigned MHD_LONG_LONG timeout;
988 static unsigned long long last_timeout = 0;
991 struct GNUNET_TIME_Relative tv;
993 ret = GNUNET_SCHEDULER_NO_TASK;
997 wrs = GNUNET_NETWORK_fdset_create ();
998 wes = GNUNET_NETWORK_fdset_create ();
999 wws = GNUNET_NETWORK_fdset_create ();
1001 GNUNET_assert (MHD_YES == MHD_get_fdset (daemon_handle, &rs, &ws, &es, &max));
1002 haveto = MHD_get_timeout (daemon_handle, &timeout);
1003 if (haveto == MHD_YES)
1005 if (timeout != last_timeout)
1008 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1009 "SELECT Timeout changed from %llu to %llu\n",
1010 last_timeout, timeout);
1012 last_timeout = timeout;
1014 tv.rel_value = (uint64_t) timeout;
1017 tv = GNUNET_TIME_UNIT_SECONDS;
1018 /* Force immediate run, since we have outbound data to send */
1019 if (now == GNUNET_YES)
1020 tv = GNUNET_TIME_UNIT_MILLISECONDS;
1021 GNUNET_NETWORK_fdset_copy_native (wrs, &rs, max + 1);
1022 GNUNET_NETWORK_fdset_copy_native (wws, &ws, max + 1);
1023 GNUNET_NETWORK_fdset_copy_native (wes, &es, max + 1);
1025 if (daemon_handle == plugin->server_v4)
1027 if (plugin->server_v4_task != GNUNET_SCHEDULER_NO_TASK)
1029 GNUNET_SCHEDULER_cancel (plugin->server_v4_task);
1030 plugin->server_v4_task = GNUNET_SCHEDULER_NO_TASK;
1033 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1034 "Scheduling IPv4 server task in %llu ms\n", tv);
1037 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1039 &server_v4_run, plugin);
1041 if (daemon_handle == plugin->server_v6)
1043 if (plugin->server_v6_task != GNUNET_SCHEDULER_NO_TASK)
1045 GNUNET_SCHEDULER_cancel (plugin->server_v6_task);
1046 plugin->server_v6_task = GNUNET_SCHEDULER_NO_TASK;
1049 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1050 "Scheduling IPv6 server task in %llu ms\n", tv);
1053 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1055 &server_v6_run, plugin);
1057 GNUNET_NETWORK_fdset_destroy (wrs);
1058 GNUNET_NETWORK_fdset_destroy (wws);
1059 GNUNET_NETWORK_fdset_destroy (wes);
1064 server_start (struct Plugin *plugin)
1066 int res = GNUNET_OK;
1067 unsigned int timeout;
1069 GNUNET_assert (NULL != plugin);
1072 res = server_load_certificate (plugin);
1073 if (res == GNUNET_SYSERR)
1075 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
1076 "Could not load or create server certificate! Loading plugin failed!\n");
1082 #if MHD_VERSION >= 0x00090E00
1083 timeout = HTTP_NOT_VALIDATED_TIMEOUT.rel_value / 1000;
1084 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1085 "MHD can set timeout per connection! Default time out %u sec.\n",
1088 timeout = GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT.rel_value / 1000;
1089 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, plugin->name,
1090 "MHD cannot set timeout per connection! Default time out %u sec.\n",
1093 plugin->server_v4 = NULL;
1094 if (plugin->ipv4 == GNUNET_YES)
1096 plugin->server_v4 = MHD_start_daemon (
1103 MHD_NO_FLAG, plugin->port,
1104 &server_accept_cb, plugin,
1105 &server_access_cb, plugin,
1106 MHD_OPTION_SOCK_ADDR,
1107 (struct sockaddr_in *)
1108 plugin->server_addr_v4,
1109 MHD_OPTION_CONNECTION_LIMIT,
1111 plugin->max_connections,
1113 MHD_OPTION_HTTPS_PRIORITIES,
1114 plugin->crypto_init,
1115 MHD_OPTION_HTTPS_MEM_KEY,
1117 MHD_OPTION_HTTPS_MEM_CERT,
1120 MHD_OPTION_CONNECTION_TIMEOUT,
1122 MHD_OPTION_CONNECTION_MEMORY_LIMIT,
1124 GNUNET_SERVER_MAX_MESSAGE_SIZE),
1125 MHD_OPTION_NOTIFY_COMPLETED,
1126 &server_disconnect_cb, plugin,
1127 MHD_OPTION_EXTERNAL_LOGGER,
1128 server_log, NULL, MHD_OPTION_END);
1130 plugin->server_v6 = NULL;
1131 if (plugin->ipv6 == GNUNET_YES)
1133 plugin->server_v6 = MHD_start_daemon (
1140 MHD_USE_IPv6, plugin->port,
1141 &server_accept_cb, plugin,
1142 &server_access_cb, plugin,
1143 MHD_OPTION_SOCK_ADDR,
1144 (struct sockaddr_in6 *)
1145 plugin->server_addr_v6,
1146 MHD_OPTION_CONNECTION_LIMIT,
1148 plugin->max_connections,
1150 MHD_OPTION_HTTPS_PRIORITIES,
1151 plugin->crypto_init,
1152 MHD_OPTION_HTTPS_MEM_KEY,
1154 MHD_OPTION_HTTPS_MEM_CERT,
1157 MHD_OPTION_CONNECTION_TIMEOUT,
1159 MHD_OPTION_CONNECTION_MEMORY_LIMIT,
1161 GNUNET_SERVER_MAX_MESSAGE_SIZE),
1162 MHD_OPTION_NOTIFY_COMPLETED,
1163 &server_disconnect_cb, plugin,
1164 MHD_OPTION_EXTERNAL_LOGGER,
1165 server_log, NULL, MHD_OPTION_END);
1169 if ((plugin->ipv4 == GNUNET_YES) && (plugin->server_v4 == NULL))
1171 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
1172 "Failed to start %s IPv4 server component on port %u\n",
1173 plugin->name, plugin->port);
1174 return GNUNET_SYSERR;
1176 server_reschedule (plugin, plugin->server_v4, GNUNET_NO);
1178 if ((plugin->ipv6 == GNUNET_YES) && (plugin->server_v6 == NULL))
1180 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
1181 "Failed to start %s IPv6 server component on port %u\n",
1182 plugin->name, plugin->port);
1183 return GNUNET_SYSERR;
1185 server_reschedule (plugin, plugin->server_v6, GNUNET_NO);
1186 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1187 "%s server component started on port %u\n", plugin->name,
1193 server_stop (struct Plugin *plugin)
1195 struct Session *s = NULL;
1196 struct Session *t = NULL;
1198 struct MHD_Daemon *server_v4_tmp = plugin->server_v4;
1200 plugin->server_v4 = NULL;
1201 struct MHD_Daemon *server_v6_tmp = plugin->server_v6;
1203 plugin->server_v6 = NULL;
1205 if (plugin->server_v4_task != GNUNET_SCHEDULER_NO_TASK)
1207 GNUNET_SCHEDULER_cancel (plugin->server_v4_task);
1208 plugin->server_v4_task = GNUNET_SCHEDULER_NO_TASK;
1211 if (plugin->server_v6_task != GNUNET_SCHEDULER_NO_TASK)
1213 GNUNET_SCHEDULER_cancel (plugin->server_v6_task);
1214 plugin->server_v6_task = GNUNET_SCHEDULER_NO_TASK;
1217 if (server_v6_tmp != NULL)
1219 MHD_stop_daemon (server_v4_tmp);
1221 if (server_v6_tmp != NULL)
1223 MHD_stop_daemon (server_v6_tmp);
1226 /* cleaning up semi-sessions never propagated */
1227 s = plugin->server_semi_head;
1231 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1232 "Deleting semi-sessions %p\n", s);
1235 struct HTTP_Message *msg = s->msg_head;
1236 struct HTTP_Message *tmp = NULL;
1242 GNUNET_CONTAINER_DLL_remove (s->msg_head, s->msg_tail, msg);
1243 if (msg->transmit_cont != NULL)
1245 msg->transmit_cont (msg->transmit_cont_cls, &s->target, GNUNET_SYSERR);
1258 GNUNET_free_non_null (plugin->crypto_init);
1259 GNUNET_free_non_null (plugin->cert);
1260 GNUNET_free_non_null (plugin->key);
1263 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1264 "%s server component stopped\n", plugin->name);
1269 /* end of plugin_transport_http.c */