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;
35 struct ServerConnection
40 /* should this connection get disconnected? GNUNET_YES/NO */
43 struct Session *session;
44 struct MHD_Connection *mhd_conn;
48 * Function that queries MHD's select sets and
49 * starts the task waiting for them.
50 * @param plugin plugin
51 * @param daemon_handle the MHD daemon handle
52 * @param now schedule now or with MHD delay
53 * @return gnunet task identifier
55 static GNUNET_SCHEDULER_TaskIdentifier
56 server_schedule (struct Plugin *plugin,
57 struct MHD_Daemon *daemon_handle,
61 server_log (void *arg, const char *fmt, va_list ap)
65 vsnprintf (text, sizeof (text), fmt, ap);
67 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Server: %s\n", text);
71 * Check if incoming connection is accepted.
72 * NOTE: Here every connection is accepted
73 * @param cls plugin as closure
74 * @param addr address of incoming connection
75 * @param addr_len address length of incoming connection
76 * @return MHD_YES if connection is accepted, MHD_NO if connection is rejected
80 server_accept_cb (void *cls, const struct sockaddr *addr, socklen_t addr_len)
82 struct Plugin *plugin = cls;
84 if (plugin->cur_connections <= plugin->max_connections)
88 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
89 "Server: Cannot accept new connections\n");
97 server_load_file (const char *file)
99 struct GNUNET_DISK_FileHandle *gn_file;
103 if (GNUNET_OK != GNUNET_DISK_file_size (file,
104 &fsize, GNUNET_NO, GNUNET_YES))
106 text = GNUNET_malloc (fsize + 1);
108 GNUNET_DISK_file_open (file, GNUNET_DISK_OPEN_READ,
109 GNUNET_DISK_PERM_USER_READ);
115 if (GNUNET_SYSERR == GNUNET_DISK_file_read (gn_file, text, fsize))
118 GNUNET_DISK_file_close (gn_file);
122 GNUNET_DISK_file_close (gn_file);
131 server_load_certificate (struct Plugin *plugin)
138 /* Get crypto init string from config
139 * If not present just use default values */
141 GNUNET_assert (GNUNET_OK ==
142 GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg,
145 &plugin->crypto_init));
148 GNUNET_CONFIGURATION_get_value_filename (plugin->env->cfg, plugin->name,
149 "KEY_FILE", &key_file))
151 key_file = GNUNET_strdup ("https_key.key");
155 GNUNET_CONFIGURATION_get_value_filename (plugin->env->cfg, plugin->name,
156 "CERT_FILE", &cert_file))
158 GNUNET_asprintf (&cert_file, "%s", "https_cert.crt");
161 /* read key & certificates from file */
162 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
163 "Loading TLS certificate from key-file `%s' cert-file`%s'\n",
164 key_file, cert_file);
166 plugin->key = server_load_file (key_file);
167 plugin->cert = server_load_file (cert_file);
169 if ((plugin->key == NULL) || (plugin->cert == NULL))
171 struct GNUNET_OS_Process *cert_creation;
173 GNUNET_free_non_null (plugin->key);
175 GNUNET_free_non_null (plugin->cert);
179 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
180 "No usable TLS certificate found, creating certificate\n");
184 GNUNET_OS_start_process (GNUNET_NO, NULL, NULL,
185 "gnunet-transport-certificate-creation",
186 "gnunet-transport-certificate-creation",
187 key_file, cert_file, NULL);
188 if (cert_creation == NULL)
190 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
192 ("Could not create a new TLS certificate, program `gnunet-transport-certificate-creation' could not be started!\n"));
193 GNUNET_free (key_file);
194 GNUNET_free (cert_file);
196 GNUNET_free_non_null (plugin->key);
198 GNUNET_free_non_null (plugin->cert);
200 GNUNET_free_non_null (plugin->crypto_init);
201 plugin->crypto_init = NULL;
203 return GNUNET_SYSERR;
205 GNUNET_assert (GNUNET_OK == GNUNET_OS_process_wait (cert_creation));
206 GNUNET_OS_process_destroy (cert_creation);
208 plugin->key = server_load_file (key_file);
209 plugin->cert = server_load_file (cert_file);
212 if ((plugin->key == NULL) || (plugin->cert == NULL))
214 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
216 ("No usable TLS certificate found and creating one failed!\n"),
218 GNUNET_free (key_file);
219 GNUNET_free (cert_file);
221 GNUNET_free_non_null (plugin->key);
223 GNUNET_free_non_null (plugin->cert);
225 GNUNET_free_non_null (plugin->crypto_init);
226 plugin->crypto_init = NULL;
228 return GNUNET_SYSERR;
230 GNUNET_free (key_file);
231 GNUNET_free (cert_file);
232 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "TLS certificate loaded\n");
239 * Reschedule the execution of both IPv4 and IPv6 server
240 * @param plugin the plugin
241 * @param server which server to schedule v4 or v6?
242 * @param now GNUNET_YES to schedule execution immediately, GNUNET_NO to wait
247 server_reschedule (struct Plugin *plugin, struct MHD_Daemon *server, int now)
249 if ((server == plugin->server_v4) && (plugin->server_v4 != NULL))
251 if (plugin->server_v4_task != GNUNET_SCHEDULER_NO_TASK)
253 GNUNET_SCHEDULER_cancel (plugin->server_v4_task);
254 plugin->server_v4_task = GNUNET_SCHEDULER_NO_TASK;
256 plugin->server_v4_task = server_schedule (plugin, plugin->server_v4, now);
259 if ((server == plugin->server_v6) && (plugin->server_v6 != NULL))
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),
305 * Callback called by MHD when it needs data to send
306 * @param cls current session
307 * @param pos position in buffer
308 * @param buf the buffer to write data to
309 * @param max max number of bytes available in buffer
310 * @return bytes written to buffer
313 server_send_callback (void *cls, uint64_t pos, char *buf, size_t max)
315 struct Session *s = cls;
316 GNUNET_assert (NULL != p);
317 if (GNUNET_NO == exist_session(p, s))
320 struct HTTP_Message *msg;
328 if ((msg->size - msg->pos) <= max)
330 memcpy (buf, &msg->buf[msg->pos], (msg->size - msg->pos));
331 bytes_read = msg->size - msg->pos;
332 msg->pos += (msg->size - msg->pos);
336 memcpy (buf, &msg->buf[msg->pos], max);
341 /* removing message */
342 if (msg->pos == msg->size)
344 if (NULL != msg->transmit_cont)
345 msg->transmit_cont (msg->transmit_cont_cls, &s->target, GNUNET_OK);
346 GNUNET_CONTAINER_DLL_remove (s->msg_head, s->msg_tail, msg);
351 struct Plugin *plugin = s->plugin;
352 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
353 "Server: %X: sent %u bytes\n", s, bytes_read);
358 static struct ServerConnection *
359 server_lookup_session (struct Plugin *plugin,
360 struct MHD_Connection *mhd_connection, const char *url,
363 struct Session *s = NULL;
365 struct ServerConnection *sc = NULL;
366 const union MHD_ConnectionInfo *conn_info;
367 struct GNUNET_ATS_Information ats;
368 struct IPv4HttpAddress a4;
369 struct IPv6HttpAddress a6;
370 struct sockaddr_in *s4;
371 struct sockaddr_in6 *s6;
374 struct GNUNET_PeerIdentity target;
375 int check = GNUNET_NO;
377 int direction = GNUNET_SYSERR;
379 conn_info = MHD_get_connection_info (mhd_connection,
380 MHD_CONNECTION_INFO_CLIENT_ADDRESS);
381 if ((conn_info->client_addr->sa_family != AF_INET) &&
382 (conn_info->client_addr->sa_family != AF_INET6))
385 if ((strlen (&url[1]) >= 105) && (url[104] == ';'))
388 char *tagc = (char *) &url[105];
390 memcpy (&hash, &url[1], 103);
393 GNUNET_CRYPTO_hash_from_string ((const char *) &hash,
394 &(target.hashPubKey)))
396 tag = strtoul (tagc, NULL, 10);
402 if (0 == strcmp (MHD_HTTP_METHOD_PUT, method))
403 direction = _RECEIVE;
404 else if (0 == strcmp (MHD_HTTP_METHOD_GET, method))
413 if (check == GNUNET_NO)
416 plugin->cur_connections++;
417 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
418 "Server: New %s connection from %s with tag %u\n",
420 GNUNET_i2s (&target), tag);
422 /* find duplicate session */
427 (0 == memcmp (&t->target, &target, sizeof (struct GNUNET_PeerIdentity)))
429 /* FIXME add source address comparison */
436 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
437 "Server: Duplicate session, dismissing new connection from peer `%s'\n",
438 GNUNET_i2s (&target));
442 /* find semi-session */
443 t = plugin->server_semi_head;
447 /* FIXME add source address comparison */
448 if ((0 == memcmp (&t->target, &target, sizeof (struct GNUNET_PeerIdentity)))
458 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
459 "Server: Found existing semi-session for `%s'\n",
460 GNUNET_i2s (&target));
462 if ((direction == _SEND) && (t->server_send != NULL))
464 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
465 "Server: Duplicate GET session, dismissing new connection from peer `%s'\n",
466 GNUNET_i2s (&target));
472 GNUNET_CONTAINER_DLL_remove (plugin->server_semi_head,
473 plugin->server_semi_tail, s);
474 GNUNET_CONTAINER_DLL_insert (plugin->head, plugin->tail, s);
475 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
476 "Server: Found matching semi-session, merging session for peer `%s'\n",
477 GNUNET_i2s (&target));
479 plugin->inbound_sessions ++;
480 GNUNET_STATISTICS_set (plugin->env->stats,
481 "# HTTP inbound sessions",
482 plugin->inbound_sessions,
484 GNUNET_assert (NULL != s);
487 if ((direction == _RECEIVE) && (t->server_recv != NULL))
489 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
490 "Server: Duplicate PUT session, dismissing new connection from peer `%s'\n",
491 GNUNET_i2s (&target));
497 GNUNET_CONTAINER_DLL_remove (plugin->server_semi_head,
498 plugin->server_semi_tail, s);
499 GNUNET_CONTAINER_DLL_insert (plugin->head, plugin->tail, s);
500 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
501 "Server: Found matching semi-session, merging session for peer `%s'\n",
502 GNUNET_i2s (&target));
503 plugin->inbound_sessions ++;
504 GNUNET_STATISTICS_set (plugin->env->stats,
505 "# HTTP inbound sessions",
506 plugin->inbound_sessions,
508 GNUNET_assert (NULL != s);
513 /* create new session */
514 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
515 "Server: Creating new session for peer `%s' \n",
516 GNUNET_i2s (&target));
517 switch (conn_info->client_addr->sa_family)
520 s4 = ((struct sockaddr_in *) conn_info->client_addr);
521 a4.u4_port = s4->sin_port;
522 memcpy (&a4.ipv4_addr, &s4->sin_addr, sizeof (struct in_addr));
524 a_len = sizeof (struct IPv4HttpAddress);
525 ats = plugin->env->get_address_type (plugin->env->cls, (const struct sockaddr *) s4, sizeof (struct sockaddr_in));
528 s6 = ((struct sockaddr_in6 *) conn_info->client_addr);
529 a6.u6_port = s6->sin6_port;
530 memcpy (&a6.ipv6_addr, &s6->sin6_addr, sizeof (struct in6_addr));
532 a_len = sizeof (struct IPv6HttpAddress);
533 ats = plugin->env->get_address_type (plugin->env->cls, (const struct sockaddr *) s6, sizeof (struct sockaddr_in6));
539 s = create_session (plugin, &target, a, a_len, NULL, NULL);
540 GNUNET_assert (NULL != s);
541 s->ats_address_network_type = ats.value;
543 s->inbound = GNUNET_YES;
544 s->next_receive = GNUNET_TIME_absolute_get_zero ();
546 if (0 == strcmp (MHD_HTTP_METHOD_PUT, method))
548 if (0 == strcmp (MHD_HTTP_METHOD_GET, method))
550 GNUNET_CONTAINER_DLL_insert (plugin->server_semi_head,
551 plugin->server_semi_tail, s);
555 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
556 "Server: Invalid connection request\n");
560 sc = GNUNET_malloc (sizeof (struct ServerConnection));
561 sc->mhd_conn = mhd_connection;
562 sc->direction = direction;
564 if (direction == _SEND)
566 if (direction == _RECEIVE)
569 #if MHD_VERSION >= 0x00090E00
570 int to = (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT.rel_value / 1000);
572 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
573 "Server: Setting timeout for %X to %u sec.\n", sc, to);
574 MHD_set_connection_option (mhd_connection, MHD_CONNECTION_OPTION_TIMEOUT, to);
576 struct MHD_Daemon *d = NULL;
578 if (s->addrlen == sizeof (struct IPv6HttpAddress))
579 d = plugin->server_v6;
580 if (s->addrlen == sizeof (struct IPv4HttpAddress))
581 d = plugin->server_v4;
583 server_reschedule (plugin, d, GNUNET_NO);
589 * Process GET or PUT request received via MHD. For
590 * GET, queue response that will send back our pending
591 * messages. For PUT, process incoming data and send
592 * to GNUnet core. In either case, check if a session
593 * already exists and create a new one if not.
596 server_access_cb (void *cls, struct MHD_Connection *mhd_connection,
597 const char *url, const char *method, const char *version,
598 const char *upload_data, size_t * upload_data_size,
599 void **httpSessionCache)
602 struct Plugin *plugin = cls;
603 struct ServerConnection *sc = *httpSessionCache;
604 struct Session *s = NULL;
607 struct MHD_Response *response;
609 GNUNET_assert (cls != NULL);
613 sc = server_lookup_session (plugin, mhd_connection, url, method);
615 (*httpSessionCache) = sc;
619 MHD_create_response_from_data (strlen (HTTP_ERROR_RESPONSE),
620 HTTP_ERROR_RESPONSE, MHD_NO, MHD_NO);
621 res = MHD_queue_response (mhd_connection, MHD_HTTP_NOT_FOUND, response);
622 MHD_destroy_response (response);
627 /* existing connection */
628 sc = (*httpSessionCache);
631 GNUNET_assert (NULL != s);
633 /* connection is to be disconnected */
634 if (sc->disconnect == GNUNET_YES)
636 /* Sent HTTP/1.1: 200 OK as PUT Response\ */
638 MHD_create_response_from_data (strlen ("Thank you!"), "Thank you!",
640 res = MHD_queue_response (mhd_connection, MHD_HTTP_OK, response);
641 MHD_destroy_response (response);
645 GNUNET_assert (s != NULL);
646 /* Check if both directions are connected */
647 if ((sc->session->server_recv == NULL) || (sc->session->server_send == NULL))
649 /* Delayed read from since not both semi-connections are connected */
653 if (sc->direction == _SEND)
656 MHD_create_response_from_callback (-1, 32 * 1024, &server_send_callback,
658 MHD_queue_response (mhd_connection, MHD_HTTP_OK, response);
659 MHD_destroy_response (response);
662 if (sc->direction == _RECEIVE)
664 if (*upload_data_size == 0)
666 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
667 "Server: Peer `%s' PUT on address `%s' connected\n",
668 GNUNET_i2s (&s->target),
669 http_plugin_address_to_string (NULL, s->addr,
675 if ((*upload_data_size > 0))
677 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
678 "Server: peer `%s' PUT on address `%s' received %u bytes\n",
679 GNUNET_i2s (&s->target),
680 http_plugin_address_to_string (NULL, s->addr,
683 struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get ();
685 if ((s->next_receive.abs_value <= now.abs_value))
687 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
688 "Server: %X: PUT with %u bytes forwarded to MST\n", s,
690 if (s->msg_tk == NULL)
692 s->msg_tk = GNUNET_SERVER_mst_create (&server_receive_mst_cb, s);
694 GNUNET_SERVER_mst_receive (s->msg_tk, s, upload_data,
695 *upload_data_size, GNUNET_NO, GNUNET_NO);
697 #if MHD_VERSION >= 0x00090E00
698 int to = (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT.rel_value / 1000);
699 struct ServerConnection *t = NULL;
701 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
702 "Server: Received %u bytes\n", *upload_data_size);
703 /* Setting timeouts for other connections */
704 if (s->server_recv != NULL)
707 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
708 "Server: Setting timeout for %X to %u sec.\n", t,
710 MHD_set_connection_option (t->mhd_conn, MHD_CONNECTION_OPTION_TIMEOUT,
713 if (s->server_send != NULL)
716 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
717 "Server: Setting timeout for %X to %u sec.\n", t,
719 MHD_set_connection_option (t->mhd_conn, MHD_CONNECTION_OPTION_TIMEOUT,
722 struct MHD_Daemon *d = NULL;
724 if (s->addrlen == sizeof (struct IPv6HttpAddress))
725 d = plugin->server_v6;
726 if (s->addrlen == sizeof (struct IPv4HttpAddress))
727 d = plugin->server_v4;
728 server_reschedule (plugin, d, GNUNET_NO);
730 (*upload_data_size) = 0;
734 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
735 "Server: %X no inbound bandwidth available! Next read was delayed by %llu ms\n",
736 s, now.abs_value - s->next_receive.abs_value);
747 server_disconnect_cb (void *cls, struct MHD_Connection *connection,
748 void **httpSessionCache)
750 struct ServerConnection *sc = *httpSessionCache;
751 struct ServerConnection *tc = NULL;
752 struct Session *s = NULL;
753 struct Session *t = NULL;
754 struct Plugin *plugin = NULL;
760 GNUNET_assert (NULL != s);
761 GNUNET_assert (NULL != p);
762 if (GNUNET_NO == exist_session(p, s))
766 if (sc->direction == _SEND)
769 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
770 "Server: %X peer `%s' GET on address `%s' disconnected\n",
771 s->server_send, GNUNET_i2s (&s->target),
772 http_plugin_address_to_string (NULL, s->addr, s->addrlen));
774 s->server_send = NULL;
776 if (s->server_recv != NULL)
779 tc->disconnect = GNUNET_YES;
780 #if MHD_VERSION >= 0x00090E00
781 MHD_set_connection_option (sc->mhd_conn, MHD_CONNECTION_OPTION_TIMEOUT,
786 if (sc->direction == _RECEIVE)
788 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
789 "Server: %X peer `%s' PUT on address `%s' disconnected\n",
790 s->server_recv, GNUNET_i2s (&s->target),
791 http_plugin_address_to_string (NULL, s->addr, s->addrlen));
792 s->server_recv = NULL;
793 if (s->server_send != NULL)
796 tc->disconnect = GNUNET_YES;
797 #if MHD_VERSION >= 0x00090E00
798 MHD_set_connection_option (sc->mhd_conn, MHD_CONNECTION_OPTION_TIMEOUT,
802 if (s->msg_tk != NULL)
804 GNUNET_SERVER_mst_destroy (s->msg_tk);
810 t = plugin->server_semi_head;
815 GNUNET_CONTAINER_DLL_remove (plugin->server_semi_head,
816 plugin->server_semi_tail, s);
817 GNUNET_CONTAINER_DLL_insert (plugin->head, plugin->tail, s);
822 plugin->cur_connections--;
824 struct MHD_Daemon *d = NULL;
826 if (s->addrlen == sizeof (struct IPv6HttpAddress))
827 d = plugin->server_v6;
828 if (s->addrlen == sizeof (struct IPv4HttpAddress))
829 d = plugin->server_v4;
830 server_reschedule (plugin, d, GNUNET_NO);
832 if ((s->server_send == NULL) && (s->server_recv == NULL))
834 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
835 "Server: peer `%s' on address `%s' disconnected\n",
836 GNUNET_i2s (&s->target),
837 http_plugin_address_to_string (NULL, s->addr, s->addrlen));
838 if (s->msg_tk != NULL)
840 GNUNET_SERVER_mst_destroy (s->msg_tk);
844 GNUNET_assert (plugin->inbound_sessions > 0);
845 plugin->inbound_sessions --;
846 GNUNET_STATISTICS_set (plugin->env->stats,
847 "# HTTP inbound sessions",
848 plugin->inbound_sessions, GNUNET_NO);
850 notify_session_end (s->plugin, &s->target, s);
855 server_disconnect (struct Session *s)
857 struct Plugin *plugin = s->plugin;
858 struct Session *t = plugin->head;
862 if (t->inbound == GNUNET_YES)
864 if (t->server_send != NULL)
866 ((struct ServerConnection *) t->server_send)->disconnect = GNUNET_YES;
868 if (t->server_send != NULL)
870 ((struct ServerConnection *) t->server_send)->disconnect = GNUNET_YES;
879 server_send (struct Session *s, struct HTTP_Message *msg)
881 GNUNET_CONTAINER_DLL_insert (s->msg_head, s->msg_tail, msg);
883 if (s->addrlen == sizeof (struct IPv4HttpAddress))
885 server_reschedule (s->plugin, s->plugin->server_v4, GNUNET_YES);
887 else if (s->addrlen == sizeof (struct IPv6HttpAddress))
889 server_reschedule (s->plugin, s->plugin->server_v6, GNUNET_YES);
892 return GNUNET_SYSERR;
899 * Call MHD IPv4 to process pending requests and then go back
900 * and schedule the next run.
901 * @param cls plugin as closure
902 * @param tc task context
905 server_v4_run (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
907 struct Plugin *plugin = cls;
909 GNUNET_assert (cls != NULL);
911 plugin->server_v4_task = GNUNET_SCHEDULER_NO_TASK;
913 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
916 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
917 "Running IPv4 server\n");
919 GNUNET_assert (MHD_YES == MHD_run (plugin->server_v4));
920 if (plugin->server_v4 != NULL)
921 plugin->server_v4_task =
922 server_schedule (plugin, plugin->server_v4, GNUNET_NO);
927 * Call MHD IPv6 to process pending requests and then go back
928 * and schedule the next run.
929 * @param cls plugin as closure
930 * @param tc task context
933 server_v6_run (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
935 struct Plugin *plugin = cls;
937 GNUNET_assert (cls != NULL);
939 plugin->server_v6_task = GNUNET_SCHEDULER_NO_TASK;
941 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
944 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
945 "Running IPv6 server\n");
947 GNUNET_assert (MHD_YES == MHD_run (plugin->server_v6));
948 if (plugin->server_v6 != NULL)
949 plugin->server_v6_task =
950 server_schedule (plugin, plugin->server_v6, GNUNET_NO);
954 * Function that queries MHD's select sets and
955 * starts the task waiting for them.
956 * @param plugin plugin
957 * @param daemon_handle the MHD daemon handle
958 * @return gnunet task identifier
960 static GNUNET_SCHEDULER_TaskIdentifier
961 server_schedule (struct Plugin *plugin, struct MHD_Daemon *daemon_handle,
964 GNUNET_SCHEDULER_TaskIdentifier ret;
968 struct GNUNET_NETWORK_FDSet *wrs;
969 struct GNUNET_NETWORK_FDSet *wws;
970 struct GNUNET_NETWORK_FDSet *wes;
972 unsigned long long timeout;
973 static unsigned long long last_timeout = 0;
976 struct GNUNET_TIME_Relative tv;
978 ret = GNUNET_SCHEDULER_NO_TASK;
982 wrs = GNUNET_NETWORK_fdset_create ();
983 wes = GNUNET_NETWORK_fdset_create ();
984 wws = GNUNET_NETWORK_fdset_create ();
986 GNUNET_assert (MHD_YES == MHD_get_fdset (daemon_handle, &rs, &ws, &es, &max));
987 haveto = MHD_get_timeout (daemon_handle, &timeout);
988 if (haveto == MHD_YES)
990 if (timeout != last_timeout)
993 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
994 "SELECT Timeout changed from %llu to %llu\n",
995 last_timeout, timeout);
997 last_timeout = timeout;
999 tv.rel_value = (uint64_t) timeout;
1002 tv = GNUNET_TIME_UNIT_SECONDS;
1003 /* Force immediate run, since we have outbound data to send */
1004 if (now == GNUNET_YES)
1005 tv = GNUNET_TIME_UNIT_MILLISECONDS;
1006 GNUNET_NETWORK_fdset_copy_native (wrs, &rs, max + 1);
1007 GNUNET_NETWORK_fdset_copy_native (wws, &ws, max + 1);
1008 GNUNET_NETWORK_fdset_copy_native (wes, &es, max + 1);
1010 if (daemon_handle == plugin->server_v4)
1012 if (plugin->server_v4_task != GNUNET_SCHEDULER_NO_TASK)
1014 GNUNET_SCHEDULER_cancel (plugin->server_v4_task);
1015 plugin->server_v4_task = GNUNET_SCHEDULER_NO_TASK;
1018 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1019 "Scheduling IPv4 server task in %llu ms\n", tv);
1022 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1024 &server_v4_run, plugin);
1026 if (daemon_handle == plugin->server_v6)
1028 if (plugin->server_v6_task != GNUNET_SCHEDULER_NO_TASK)
1030 GNUNET_SCHEDULER_cancel (plugin->server_v6_task);
1031 plugin->server_v6_task = GNUNET_SCHEDULER_NO_TASK;
1034 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1035 "Scheduling IPv6 server task in %llu ms\n", tv);
1038 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1040 &server_v6_run, plugin);
1042 GNUNET_NETWORK_fdset_destroy (wrs);
1043 GNUNET_NETWORK_fdset_destroy (wws);
1044 GNUNET_NETWORK_fdset_destroy (wes);
1049 server_start (struct Plugin *plugin)
1051 int res = GNUNET_OK;
1052 unsigned int timeout;
1054 GNUNET_assert (NULL != plugin);
1057 res = server_load_certificate (plugin);
1058 if (res == GNUNET_SYSERR)
1060 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
1061 "Could not load or create server certificate! Loading plugin failed!\n");
1067 #if MHD_VERSION >= 0x00090E00
1068 timeout = HTTP_NOT_VALIDATED_TIMEOUT.rel_value / 1000;
1069 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1070 "MHD can set timeout per connection! Default time out %u sec.\n",
1073 timeout = GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT.rel_value / 1000;
1074 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, plugin->name,
1075 "MHD cannot set timeout per connection! Default time out %u sec.\n",
1078 plugin->server_v4 = NULL;
1079 if (plugin->ipv4 == GNUNET_YES)
1081 plugin->server_v4 = MHD_start_daemon (
1088 MHD_NO_FLAG, plugin->port,
1089 &server_accept_cb, plugin,
1090 &server_access_cb, plugin,
1091 MHD_OPTION_SOCK_ADDR,
1092 (struct sockaddr_in *)
1093 plugin->server_addr_v4,
1094 MHD_OPTION_CONNECTION_LIMIT,
1096 plugin->max_connections,
1098 MHD_OPTION_HTTPS_PRIORITIES,
1099 plugin->crypto_init,
1100 MHD_OPTION_HTTPS_MEM_KEY,
1102 MHD_OPTION_HTTPS_MEM_CERT,
1105 MHD_OPTION_CONNECTION_TIMEOUT,
1107 MHD_OPTION_CONNECTION_MEMORY_LIMIT,
1109 GNUNET_SERVER_MAX_MESSAGE_SIZE),
1110 MHD_OPTION_NOTIFY_COMPLETED,
1111 &server_disconnect_cb, plugin,
1112 MHD_OPTION_EXTERNAL_LOGGER,
1113 server_log, NULL, MHD_OPTION_END);
1115 plugin->server_v6 = NULL;
1116 if (plugin->ipv6 == GNUNET_YES)
1118 plugin->server_v6 = MHD_start_daemon (
1125 MHD_USE_IPv6, plugin->port,
1126 &server_accept_cb, plugin,
1127 &server_access_cb, plugin,
1128 MHD_OPTION_SOCK_ADDR,
1129 (struct sockaddr_in6 *)
1130 plugin->server_addr_v6,
1131 MHD_OPTION_CONNECTION_LIMIT,
1133 plugin->max_connections,
1135 MHD_OPTION_HTTPS_PRIORITIES,
1136 plugin->crypto_init,
1137 MHD_OPTION_HTTPS_MEM_KEY,
1139 MHD_OPTION_HTTPS_MEM_CERT,
1142 MHD_OPTION_CONNECTION_TIMEOUT,
1144 MHD_OPTION_CONNECTION_MEMORY_LIMIT,
1146 GNUNET_SERVER_MAX_MESSAGE_SIZE),
1147 MHD_OPTION_NOTIFY_COMPLETED,
1148 &server_disconnect_cb, plugin,
1149 MHD_OPTION_EXTERNAL_LOGGER,
1150 server_log, NULL, MHD_OPTION_END);
1154 if ((plugin->ipv4 == GNUNET_YES) && (plugin->server_v4 == NULL))
1156 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
1157 "Failed to start %s IPv4 server component on port %u\n",
1158 plugin->name, plugin->port);
1159 return GNUNET_SYSERR;
1161 server_reschedule (plugin, plugin->server_v4, GNUNET_NO);
1163 if ((plugin->ipv6 == GNUNET_YES) && (plugin->server_v6 == NULL))
1165 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
1166 "Failed to start %s IPv6 server component on port %u\n",
1167 plugin->name, plugin->port);
1168 return GNUNET_SYSERR;
1170 server_reschedule (plugin, plugin->server_v6, GNUNET_NO);
1171 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1172 "%s server component started on port %u\n", plugin->name,
1178 server_stop (struct Plugin *plugin)
1180 struct Session *s = NULL;
1181 struct Session *t = NULL;
1185 struct MHD_Daemon *server_v4_tmp = plugin->server_v4;
1187 plugin->server_v4 = NULL;
1188 struct MHD_Daemon *server_v6_tmp = plugin->server_v6;
1190 plugin->server_v6 = NULL;
1192 if (plugin->server_v4_task != GNUNET_SCHEDULER_NO_TASK)
1194 GNUNET_SCHEDULER_cancel (plugin->server_v4_task);
1195 plugin->server_v4_task = GNUNET_SCHEDULER_NO_TASK;
1198 if (plugin->server_v6_task != GNUNET_SCHEDULER_NO_TASK)
1200 GNUNET_SCHEDULER_cancel (plugin->server_v6_task);
1201 plugin->server_v6_task = GNUNET_SCHEDULER_NO_TASK;
1204 if (server_v6_tmp != NULL)
1206 MHD_stop_daemon (server_v4_tmp);
1208 if (server_v6_tmp != NULL)
1210 MHD_stop_daemon (server_v6_tmp);
1213 /* cleaning up semi-sessions never propagated */
1214 s = plugin->server_semi_head;
1218 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1219 "Deleting semi-sessions %p\n", s);
1222 struct HTTP_Message *msg = s->msg_head;
1223 struct HTTP_Message *tmp = NULL;
1229 GNUNET_CONTAINER_DLL_remove (s->msg_head, s->msg_tail, msg);
1230 if (msg->transmit_cont != NULL)
1232 msg->transmit_cont (msg->transmit_cont_cls, &s->target, GNUNET_SYSERR);
1243 GNUNET_free_non_null (plugin->crypto_init);
1244 GNUNET_free_non_null (plugin->cert);
1245 GNUNET_free_non_null (plugin->key);
1248 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1249 "%s server component stopped\n", plugin->name);
1254 /* end of plugin_transport_http.c */