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 struct ServerConnection
38 /* should this connection get disconnected? GNUNET_YES/NO */
41 struct Session *session;
42 struct MHD_Connection *mhd_conn;
46 * Function that queries MHD's select sets and
47 * starts the task waiting for them.
48 * @param plugin plugin
49 * @param daemon_handle the MHD daemon handle
50 * @return gnunet task identifier
52 static GNUNET_SCHEDULER_TaskIdentifier
53 server_schedule (struct Plugin *plugin, struct MHD_Daemon *daemon_handle,
57 server_log (void *arg, const char *fmt, va_list ap)
61 vsnprintf (text, sizeof (text), fmt, ap);
63 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Server: %s\n", text);
67 * Check if incoming connection is accepted.
68 * NOTE: Here every connection is accepted
69 * @param cls plugin as closure
70 * @param addr address of incoming connection
71 * @param addr_len address length of incoming connection
72 * @return MHD_YES if connection is accepted, MHD_NO if connection is rejected
76 server_accept_cb (void *cls, const struct sockaddr *addr, socklen_t addr_len)
78 struct Plugin *plugin = cls;
80 if (plugin->cur_connections <= plugin->max_connections)
84 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
85 "Server: Cannot accept new connections\n");
93 server_load_file (const char *file)
95 struct GNUNET_DISK_FileHandle *gn_file;
99 if (0 != STAT (file, &fstat))
101 text = GNUNET_malloc (fstat.st_size + 1);
103 GNUNET_DISK_file_open (file, GNUNET_DISK_OPEN_READ,
104 GNUNET_DISK_PERM_USER_READ);
110 if (GNUNET_SYSERR == GNUNET_DISK_file_read (gn_file, text, fstat.st_size))
113 GNUNET_DISK_file_close (gn_file);
116 text[fstat.st_size] = '\0';
117 GNUNET_DISK_file_close (gn_file);
126 server_load_certificate (struct Plugin *plugin)
133 /* Get crypto init string from config
134 * If not present just use default values */
136 GNUNET_assert (GNUNET_OK ==
137 GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg,
140 &plugin->crypto_init));
143 GNUNET_CONFIGURATION_get_value_filename (plugin->env->cfg, plugin->name,
144 "KEY_FILE", &key_file))
146 key_file = GNUNET_strdup ("https_key.key");
150 GNUNET_CONFIGURATION_get_value_filename (plugin->env->cfg, plugin->name,
151 "CERT_FILE", &cert_file))
153 GNUNET_asprintf (&cert_file, "%s", "https_cert.crt");
156 /* read key & certificates from file */
158 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
159 "Loading TLS certificate from key-file `%s' cert-file`%s'\n",
160 key_file, cert_file);
163 plugin->key = server_load_file (key_file);
164 plugin->cert = server_load_file (cert_file);
166 if ((plugin->key == NULL) || (plugin->cert == NULL))
168 struct GNUNET_OS_Process *cert_creation;
170 GNUNET_free_non_null (plugin->key);
172 GNUNET_free_non_null (plugin->cert);
176 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
177 "No usable TLS certificate found, creating certificate\n");
181 GNUNET_OS_start_process (NULL, NULL,
182 "gnunet-transport-certificate-creation",
183 "gnunet-transport-certificate-creation",
184 key_file, cert_file, NULL);
185 if (cert_creation == NULL)
187 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
189 ("Could not create a new TLS certificate, program `gnunet-transport-certificate-creation' could not be started!\n"));
190 GNUNET_free (key_file);
191 GNUNET_free (cert_file);
193 GNUNET_free_non_null (plugin->key);
195 GNUNET_free_non_null (plugin->cert);
197 GNUNET_free_non_null (plugin->crypto_init);
198 plugin->crypto_init = NULL;
200 return GNUNET_SYSERR;
202 GNUNET_assert (GNUNET_OK == GNUNET_OS_process_wait (cert_creation));
203 GNUNET_OS_process_close (cert_creation);
205 plugin->key = server_load_file (key_file);
206 plugin->cert = server_load_file (cert_file);
209 if ((plugin->key == NULL) || (plugin->cert == NULL))
211 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
213 ("No usable TLS certificate found and creating one failed!\n"),
215 GNUNET_free (key_file);
216 GNUNET_free (cert_file);
218 GNUNET_free_non_null (plugin->key);
220 GNUNET_free_non_null (plugin->cert);
222 GNUNET_free_non_null (plugin->crypto_init);
223 plugin->crypto_init = NULL;
225 return GNUNET_SYSERR;
227 GNUNET_free (key_file);
228 GNUNET_free (cert_file);
230 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 now GNUNET_YES to schedule execution immediately, GNUNET_NO to wait
246 server_reschedule (struct Plugin *plugin, struct MHD_Daemon *server, int now)
248 if ((server == plugin->server_v4) && (plugin->server_v4 != NULL))
250 if (plugin->server_v4_task != GNUNET_SCHEDULER_NO_TASK)
252 GNUNET_SCHEDULER_cancel (plugin->server_v4_task);
253 plugin->server_v4_task = GNUNET_SCHEDULER_NO_TASK;
255 plugin->server_v4_task = server_schedule (plugin, plugin->server_v4, now);
258 if ((server == plugin->server_v6) && (plugin->server_v6 != NULL))
260 if (plugin->server_v6_task != GNUNET_SCHEDULER_NO_TASK)
262 GNUNET_SCHEDULER_cancel (plugin->server_v6_task);
263 plugin->server_v6_task = GNUNET_SCHEDULER_NO_TASK;
265 plugin->server_v6_task = server_schedule (plugin, plugin->server_v6, now);
270 * Callback called by MessageStreamTokenizer when a message has arrived
271 * @param cls current session as closure
272 * @param client clien
273 * @param message the message to be forwarded to transport service
276 server_receive_mst_cb (void *cls, void *client,
277 const struct GNUNET_MessageHeader *message)
279 struct Session *s = cls;
282 struct Plugin *plugin = s->plugin;
284 struct GNUNET_TIME_Relative delay;
286 delay = http_plugin_receive (s, &s->target, message, s, s->addr, s->addrlen);
289 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (), delay);
291 if (delay.rel_value > 0)
294 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
295 "Server: peer `%s' address `%s' next read delayed for %llu ms\n",
296 GNUNET_i2s (&s->target),
297 http_plugin_address_to_string (NULL, s->addr, s->addrlen),
304 * Callback called by MHD when it needs data to send
305 * @param cls current session
306 * @param pos position in buffer
307 * @param buf the buffer to write data to
308 * @param max max number of bytes available in buffer
309 * @return bytes written to buffer
312 server_send_callback (void *cls, uint64_t pos, char *buf, size_t max)
314 struct Session *s = cls;
317 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, s->plugin->name,
318 "Server: %X can sent maximum %u \n", s, max);
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);
352 struct Plugin *plugin = s->plugin;
354 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
355 "Server: %X: sent %u bytes\n", s, bytes_read);
360 static struct ServerConnection *
361 server_lookup_session (struct Plugin *plugin,
362 struct MHD_Connection *mhd_connection, const char *url,
365 struct Session *s = NULL;
367 struct ServerConnection *sc = NULL;
368 const union MHD_ConnectionInfo *conn_info;
370 struct IPv4HttpAddress a4;
371 struct IPv6HttpAddress a6;
372 struct sockaddr_in *s4;
373 struct sockaddr_in6 *s6;
376 struct GNUNET_PeerIdentity target;
377 int check = GNUNET_NO;
382 MHD_get_connection_info (mhd_connection,
383 MHD_CONNECTION_INFO_CLIENT_ADDRESS);
384 if ((conn_info->client_addr->sa_family != AF_INET) &&
385 (conn_info->client_addr->sa_family != AF_INET6))
388 if ((strlen (&url[1]) >= 105) && (url[104] == ';'))
391 char *tagc = (char *) &url[105];
393 memcpy (&hash, &url[1], 103);
396 GNUNET_CRYPTO_hash_from_string ((const char *) &hash,
397 &(target.hashPubKey)))
399 tag = strtoul (tagc, NULL, 10);
405 if (0 == strcmp (MHD_HTTP_METHOD_PUT, method))
406 direction = _RECEIVE;
407 if (0 == strcmp (MHD_HTTP_METHOD_GET, method))
410 if (check == GNUNET_NO)
413 plugin->cur_connections++;
416 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
417 "Server: New inbound connection from %s with tag %u\n",
418 GNUNET_i2s (&target), tag);
420 /* find duplicate session */
427 (0 == memcmp (&t->target, &target, sizeof (struct GNUNET_PeerIdentity)))
429 /* FIXME add source address comparison */
437 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
438 "Server: Duplicate session, dismissing new connection from peer `%s'\n",
439 GNUNET_i2s (&target));
444 /* find semi-session */
445 t = plugin->server_semi_head;
449 /* FIXME add source address comparison */
450 if ((0 == memcmp (&t->target, &target, sizeof (struct GNUNET_PeerIdentity)))
462 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
463 "Server: Found existing semi-session for `%s'\n",
464 GNUNET_i2s (&target));
467 if ((direction == _SEND) && (t->server_send != NULL))
470 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
471 "Server: Duplicate GET session, dismissing new connection from peer `%s'\n",
472 GNUNET_i2s (&target));
479 GNUNET_CONTAINER_DLL_remove (plugin->server_semi_head,
480 plugin->server_semi_tail, s);
481 GNUNET_CONTAINER_DLL_insert (plugin->head, plugin->tail, s);
483 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
484 "Server: Found matching semi-session, merging session for peer `%s'\n",
485 GNUNET_i2s (&target));
490 if ((direction == _RECEIVE) && (t->server_recv != NULL))
493 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
494 "Server: Duplicate PUT session, dismissing new connection from peer `%s'\n",
495 GNUNET_i2s (&target));
502 GNUNET_CONTAINER_DLL_remove (plugin->server_semi_head,
503 plugin->server_semi_tail, s);
504 GNUNET_CONTAINER_DLL_insert (plugin->head, plugin->tail, s);
506 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
507 "Server: Found matching semi-session, merging session for peer `%s'\n",
508 GNUNET_i2s (&target));
514 /* create new session */
516 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
517 "Server: Creating new session for peer `%s' \n",
518 GNUNET_i2s (&target));
521 switch (conn_info->client_addr->sa_family)
524 s4 = ((struct sockaddr_in *) conn_info->client_addr);
525 a4.u4_port = s4->sin_port;
526 memcpy (&a4.ipv4_addr, &s4->sin_addr, sizeof (struct in_addr));
528 a_len = sizeof (struct IPv4HttpAddress);
531 s6 = ((struct sockaddr_in6 *) conn_info->client_addr);
532 a6.u6_port = s6->sin6_port;
533 memcpy (&a6.ipv6_addr, &s6->sin6_addr, sizeof (struct in6_addr));
535 a_len = sizeof (struct IPv6HttpAddress);
541 s = create_session (plugin, &target, a, a_len, NULL, NULL);
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);
556 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
557 "Server: Invalid connection request\n");
562 sc = GNUNET_malloc (sizeof (struct ServerConnection));
563 sc->mhd_conn = mhd_connection;
564 sc->direction = direction;
566 if (direction == _SEND)
568 if (direction == _RECEIVE)
571 #if MHD_VERSION >= 0x00090E00
572 int to = (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT.rel_value / 1000);
575 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
576 "Server: Setting timeout for %X to %u sec.\n", sc, to);
578 MHD_set_connection_option (mhd_connection, MHD_CONNECTION_OPTION_TIMEOUT, to);
580 struct MHD_Daemon *d = NULL;
582 if (s->addrlen == sizeof (struct IPv6HttpAddress))
583 d = plugin->server_v6;
584 if (s->addrlen == sizeof (struct IPv4HttpAddress))
585 d = plugin->server_v4;
587 server_reschedule (plugin, d, GNUNET_NO);
593 * Process GET or PUT request received via MHD. For
594 * GET, queue response that will send back our pending
595 * messages. For PUT, process incoming data and send
596 * to GNUnet core. In either case, check if a session
597 * already exists and create a new one if not.
600 server_access_cb (void *cls, struct MHD_Connection *mhd_connection,
601 const char *url, const char *method, const char *version,
602 const char *upload_data, size_t * upload_data_size,
603 void **httpSessionCache)
606 struct Plugin *plugin = cls;
607 struct ServerConnection *sc = *httpSessionCache;
608 struct Session *s = NULL;
611 struct MHD_Response *response;
613 GNUNET_assert (cls != NULL);
617 sc = server_lookup_session (plugin, mhd_connection, url, method);
619 (*httpSessionCache) = sc;
623 MHD_create_response_from_data (strlen (HTTP_ERROR_RESPONSE),
624 HTTP_ERROR_RESPONSE, MHD_NO, MHD_NO);
625 res = MHD_queue_response (mhd_connection, MHD_HTTP_NOT_FOUND, response);
626 MHD_destroy_response (response);
631 /* existing connection */
632 sc = (*httpSessionCache);
635 /* connection is to be disconnected */
636 if (sc->disconnect == GNUNET_YES)
639 MHD_create_response_from_data (strlen ("Thank you!"), "Thank you!",
641 res = MHD_queue_response (mhd_connection, MHD_HTTP_OK, response);
643 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
644 "Sent HTTP/1.1: 200 OK as PUT Response\n");
646 MHD_destroy_response (response);
650 GNUNET_assert (s != NULL);
651 /* Check if both directions are connected */
652 if ((sc->session->server_recv == NULL) || (sc->session->server_send == NULL))
655 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
656 "Server: Delayed read from `%s' `%s' since not both semi-connections are connected\n",
657 GNUNET_i2s (&s->target),
658 http_plugin_address_to_string (NULL, s->addr, s->addrlen));
663 if (sc->direction == _SEND)
666 MHD_create_response_from_callback (-1, 32 * 1024, &server_send_callback,
668 res = MHD_queue_response (mhd_connection, MHD_HTTP_OK, response);
669 MHD_destroy_response (response);
672 if (sc->direction == _RECEIVE)
674 if (*upload_data_size == 0)
677 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
678 "Server: Peer `%s' PUT on address `%s' connected\n",
679 GNUNET_i2s (&s->target),
680 http_plugin_address_to_string (NULL, s->addr,
687 if ((*upload_data_size > 0))
690 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
691 "Server: peer `%s' PUT on address `%s' received %u bytes\n",
692 GNUNET_i2s (&s->target),
693 http_plugin_address_to_string (NULL, s->addr,
697 struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get ();
699 if ((s->next_receive.abs_value <= now.abs_value))
702 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
703 "Server: %X: PUT with %u bytes forwarded to MST\n", s,
706 if (s->msg_tk == NULL)
708 s->msg_tk = GNUNET_SERVER_mst_create (&server_receive_mst_cb, s);
711 GNUNET_SERVER_mst_receive (s->msg_tk, s, upload_data,
712 *upload_data_size, GNUNET_NO, GNUNET_NO);
714 #if MHD_VERSION >= 0x00090E00
715 int to = (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT.rel_value / 1000);
716 struct ServerConnection *t = NULL;
719 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
720 "Server: Received %u bytes\n", *upload_data_size);
723 /* Setting timeouts for other connections */
724 if (s->server_recv != NULL)
728 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
729 "Server: Setting timeout for %X to %u sec.\n", t,
732 MHD_set_connection_option (t->mhd_conn, MHD_CONNECTION_OPTION_TIMEOUT,
735 if (s->server_send != NULL)
739 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
740 "Server: Setting timeout for %X to %u sec.\n", t,
743 MHD_set_connection_option (t->mhd_conn, MHD_CONNECTION_OPTION_TIMEOUT,
746 struct MHD_Daemon *d = NULL;
748 if (s->addrlen == sizeof (struct IPv6HttpAddress))
749 d = plugin->server_v6;
750 if (s->addrlen == sizeof (struct IPv4HttpAddress))
751 d = plugin->server_v4;
752 server_reschedule (plugin, d, GNUNET_NO);
754 (*upload_data_size) = 0;
759 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
760 "Server: %X no inbound bandwidth available! Next read was delayed by %llu ms\n",
761 s, now.abs_value - s->next_receive.abs_value);
773 server_disconnect_cb (void *cls, struct MHD_Connection *connection,
774 void **httpSessionCache)
776 struct ServerConnection *sc = *httpSessionCache;
777 struct ServerConnection *tc = *httpSessionCache;
778 struct Session *s = NULL;
779 struct Session *t = NULL;
780 struct Plugin *plugin = NULL;
787 if (sc->direction == _SEND)
790 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
791 "Server: %X peer `%s' GET on address `%s' disconnected\n",
792 s->server_send, GNUNET_i2s (&s->target),
793 http_plugin_address_to_string (NULL, s->addr, s->addrlen));
795 s->server_send = NULL;
797 if (s->server_recv != NULL)
800 tc->disconnect = GNUNET_YES;
801 #if MHD_VERSION >= 0x00090E00
802 MHD_set_connection_option (sc->mhd_conn, MHD_CONNECTION_OPTION_TIMEOUT,
807 if (sc->direction == _RECEIVE)
810 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
811 "Server: %X peer `%s' PUT on address `%s' disconnected\n",
812 s->server_recv, GNUNET_i2s (&s->target),
813 http_plugin_address_to_string (NULL, s->addr, s->addrlen));
815 s->server_recv = NULL;
816 if (s->server_send != NULL)
819 tc->disconnect = GNUNET_YES;
820 #if MHD_VERSION >= 0x00090E00
821 MHD_set_connection_option (sc->mhd_conn, MHD_CONNECTION_OPTION_TIMEOUT,
825 if (s->msg_tk != NULL)
827 GNUNET_SERVER_mst_destroy (s->msg_tk);
833 t = plugin->server_semi_head;
838 GNUNET_CONTAINER_DLL_remove (plugin->server_semi_head,
839 plugin->server_semi_tail, s);
840 GNUNET_CONTAINER_DLL_insert (plugin->head, plugin->tail, s);
845 plugin->cur_connections--;
847 struct MHD_Daemon *d = NULL;
849 if (s->addrlen == sizeof (struct IPv6HttpAddress))
850 d = plugin->server_v6;
851 if (s->addrlen == sizeof (struct IPv4HttpAddress))
852 d = plugin->server_v4;
853 server_reschedule (plugin, d, GNUNET_NO);
855 if ((s->server_send == NULL) && (s->server_recv == NULL))
858 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
859 "Server: peer `%s' on address `%s' disconnected\n",
860 GNUNET_i2s (&s->target),
861 http_plugin_address_to_string (NULL, s->addr, s->addrlen));
863 if (s->msg_tk != NULL)
865 GNUNET_SERVER_mst_destroy (s->msg_tk);
869 notify_session_end (s->plugin, &s->target, s);
874 server_disconnect (struct Session *s)
876 struct Plugin *plugin = s->plugin;
877 struct Session *t = plugin->head;
881 if (t->inbound == GNUNET_YES)
883 if (t->server_send != NULL)
885 ((struct ServerConnection *) t->server_send)->disconnect = GNUNET_YES;
887 if (t->server_send != NULL)
889 ((struct ServerConnection *) t->server_send)->disconnect = GNUNET_YES;
898 server_send (struct Session *s, struct HTTP_Message *msg)
900 GNUNET_CONTAINER_DLL_insert (s->msg_head, s->msg_tail, msg);
902 if (s->addrlen == sizeof (struct IPv4HttpAddress))
904 server_reschedule (s->plugin, s->plugin->server_v4, GNUNET_YES);
906 else if (s->addrlen == sizeof (struct IPv6HttpAddress))
908 server_reschedule (s->plugin, s->plugin->server_v6, GNUNET_YES);
911 return GNUNET_SYSERR;
918 * Call MHD IPv4 to process pending requests and then go back
919 * and schedule the next run.
920 * @param cls plugin as closure
921 * @param tc task context
924 server_v4_run (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
926 struct Plugin *plugin = cls;
928 GNUNET_assert (cls != NULL);
930 plugin->server_v4_task = GNUNET_SCHEDULER_NO_TASK;
932 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
935 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
936 "Running IPv4 server\n");
938 GNUNET_assert (MHD_YES == MHD_run (plugin->server_v4));
939 if (plugin->server_v4 != NULL)
940 plugin->server_v4_task =
941 server_schedule (plugin, plugin->server_v4, GNUNET_NO);
946 * Call MHD IPv6 to process pending requests and then go back
947 * and schedule the next run.
948 * @param cls plugin as closure
949 * @param tc task context
952 server_v6_run (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
954 struct Plugin *plugin = cls;
956 GNUNET_assert (cls != NULL);
958 plugin->server_v6_task = GNUNET_SCHEDULER_NO_TASK;
960 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
963 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
964 "Running IPv6 server\n");
966 GNUNET_assert (MHD_YES == MHD_run (plugin->server_v6));
967 if (plugin->server_v6 != NULL)
968 plugin->server_v6_task =
969 server_schedule (plugin, plugin->server_v6, GNUNET_NO);
973 * Function that queries MHD's select sets and
974 * starts the task waiting for them.
975 * @param plugin plugin
976 * @param daemon_handle the MHD daemon handle
977 * @return gnunet task identifier
979 static GNUNET_SCHEDULER_TaskIdentifier
980 server_schedule (struct Plugin *plugin, struct MHD_Daemon *daemon_handle,
983 GNUNET_SCHEDULER_TaskIdentifier ret;
987 struct GNUNET_NETWORK_FDSet *wrs;
988 struct GNUNET_NETWORK_FDSet *wws;
989 struct GNUNET_NETWORK_FDSet *wes;
991 unsigned long long timeout;
992 static unsigned long long last_timeout = 0;
995 struct GNUNET_TIME_Relative tv;
997 ret = GNUNET_SCHEDULER_NO_TASK;
1001 wrs = GNUNET_NETWORK_fdset_create ();
1002 wes = GNUNET_NETWORK_fdset_create ();
1003 wws = GNUNET_NETWORK_fdset_create ();
1005 GNUNET_assert (MHD_YES == MHD_get_fdset (daemon_handle, &rs, &ws, &es, &max));
1006 haveto = MHD_get_timeout (daemon_handle, &timeout);
1007 if (haveto == MHD_YES)
1009 if (timeout != last_timeout)
1012 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1013 "SELECT Timeout changed from %llu to %llu\n",
1014 last_timeout, timeout);
1016 last_timeout = timeout;
1018 tv.rel_value = (uint64_t) timeout;
1021 tv = GNUNET_TIME_UNIT_SECONDS;
1022 /* Force immediate run, since we have outbound data to send */
1023 if (now == GNUNET_YES)
1024 tv = GNUNET_TIME_UNIT_MILLISECONDS;
1025 GNUNET_NETWORK_fdset_copy_native (wrs, &rs, max + 1);
1026 GNUNET_NETWORK_fdset_copy_native (wws, &ws, max + 1);
1027 GNUNET_NETWORK_fdset_copy_native (wes, &es, max + 1);
1029 if (daemon_handle == plugin->server_v4)
1031 if (plugin->server_v4_task != GNUNET_SCHEDULER_NO_TASK)
1033 GNUNET_SCHEDULER_cancel (plugin->server_v4_task);
1034 plugin->server_v4_task = GNUNET_SCHEDULER_NO_TASK;
1037 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
1038 "Scheduling IPv4 server task in %llu ms\n", tv);
1041 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1042 GNUNET_SCHEDULER_NO_TASK, tv, wrs, wws,
1043 &server_v4_run, plugin);
1045 if (daemon_handle == plugin->server_v6)
1047 if (plugin->server_v6_task != GNUNET_SCHEDULER_NO_TASK)
1049 GNUNET_SCHEDULER_cancel (plugin->server_v6_task);
1050 plugin->server_v6_task = GNUNET_SCHEDULER_NO_TASK;
1053 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
1054 "Scheduling IPv6 server task in %llu ms\n", tv);
1057 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1058 GNUNET_SCHEDULER_NO_TASK, tv, wrs, wws,
1059 &server_v6_run, plugin);
1061 GNUNET_NETWORK_fdset_destroy (wrs);
1062 GNUNET_NETWORK_fdset_destroy (wws);
1063 GNUNET_NETWORK_fdset_destroy (wes);
1068 server_start (struct Plugin *plugin)
1070 int res = GNUNET_OK;
1071 unsigned int timeout;
1074 res = server_load_certificate (plugin);
1075 if (res == GNUNET_SYSERR)
1077 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
1078 "Could not load or create server certificate! Loading plugin failed!\n");
1084 #if MHD_VERSION >= 0x00090E00
1085 timeout = HTTP_NOT_VALIDATED_TIMEOUT.rel_value / 1000;
1086 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1087 "MHD can set timeout per connection! Default time out %u sec.\n",
1090 timeout = GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT.rel_value / 1000;
1091 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, plugin->name,
1092 "MHD cannot set timeout per connection! Default time out %u sec.\n",
1095 plugin->server_v4 = NULL;
1096 if (plugin->ipv4 == GNUNET_YES)
1098 plugin->server_v4 = MHD_start_daemon (
1105 MHD_NO_FLAG, plugin->port,
1106 &server_accept_cb, plugin,
1107 &server_access_cb, plugin,
1108 MHD_OPTION_SOCK_ADDR,
1109 (struct sockaddr_in *)
1110 plugin->server_addr_v4,
1111 MHD_OPTION_CONNECTION_LIMIT,
1113 plugin->max_connections,
1115 MHD_OPTION_HTTPS_PRIORITIES,
1116 plugin->crypto_init,
1117 MHD_OPTION_HTTPS_MEM_KEY,
1119 MHD_OPTION_HTTPS_MEM_CERT,
1122 MHD_OPTION_CONNECTION_TIMEOUT,
1124 MHD_OPTION_CONNECTION_MEMORY_LIMIT,
1126 GNUNET_SERVER_MAX_MESSAGE_SIZE),
1127 MHD_OPTION_NOTIFY_COMPLETED,
1128 &server_disconnect_cb, plugin,
1129 MHD_OPTION_EXTERNAL_LOGGER,
1130 server_log, NULL, MHD_OPTION_END);
1132 plugin->server_v6 = NULL;
1133 if (plugin->ipv6 == GNUNET_YES)
1135 plugin->server_v6 = MHD_start_daemon (
1142 MHD_USE_IPv6, plugin->port,
1143 &server_accept_cb, plugin,
1144 &server_access_cb, plugin,
1145 MHD_OPTION_SOCK_ADDR,
1146 (struct sockaddr_in6 *)
1147 plugin->server_addr_v6,
1148 MHD_OPTION_CONNECTION_LIMIT,
1150 plugin->max_connections,
1152 MHD_OPTION_HTTPS_PRIORITIES,
1153 plugin->crypto_init,
1154 MHD_OPTION_HTTPS_MEM_KEY,
1156 MHD_OPTION_HTTPS_MEM_CERT,
1159 MHD_OPTION_CONNECTION_TIMEOUT,
1161 MHD_OPTION_CONNECTION_MEMORY_LIMIT,
1163 GNUNET_SERVER_MAX_MESSAGE_SIZE),
1164 MHD_OPTION_NOTIFY_COMPLETED,
1165 &server_disconnect_cb, plugin,
1166 MHD_OPTION_EXTERNAL_LOGGER,
1167 server_log, NULL, MHD_OPTION_END);
1171 if ((plugin->ipv4 == GNUNET_YES) && (plugin->server_v4 == NULL))
1173 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
1174 "Failed to start %s IPv4 server component on port %u\n",
1175 plugin->name, plugin->port);
1176 return GNUNET_SYSERR;
1178 server_reschedule (plugin, plugin->server_v4, GNUNET_NO);
1180 if ((plugin->ipv6 == GNUNET_YES) && (plugin->server_v6 == NULL))
1182 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
1183 "Failed to start %s IPv6 server component on port %u\n",
1184 plugin->name, plugin->port);
1185 return GNUNET_SYSERR;
1187 server_reschedule (plugin, plugin->server_v6, GNUNET_NO);
1191 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1192 "%s server component started on port %u\n", plugin->name,
1199 server_stop (struct Plugin *plugin)
1201 struct Session *s = NULL;
1202 struct Session *t = NULL;
1205 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name, "server_stop\n");
1208 struct MHD_Daemon *server_v4_tmp = plugin->server_v4;
1210 plugin->server_v4 = NULL;
1211 struct MHD_Daemon *server_v6_tmp = plugin->server_v6;
1213 plugin->server_v6 = NULL;
1215 if (plugin->server_v4_task != GNUNET_SCHEDULER_NO_TASK)
1217 GNUNET_SCHEDULER_cancel (plugin->server_v4_task);
1218 plugin->server_v4_task = GNUNET_SCHEDULER_NO_TASK;
1221 if (plugin->server_v6_task != GNUNET_SCHEDULER_NO_TASK)
1223 GNUNET_SCHEDULER_cancel (plugin->server_v6_task);
1224 plugin->server_v6_task = GNUNET_SCHEDULER_NO_TASK;
1227 if (server_v6_tmp != NULL)
1229 MHD_stop_daemon (server_v4_tmp);
1231 if (server_v6_tmp != NULL)
1233 MHD_stop_daemon (server_v6_tmp);
1236 /* cleaning up semi-sessions never propagated */
1237 s = plugin->server_semi_head;
1241 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
1242 "Deleting semi-sessions %p\n", s);
1245 struct HTTP_Message *msg = s->msg_head;
1246 struct HTTP_Message *tmp = s->msg_head;
1252 GNUNET_CONTAINER_DLL_remove (s->msg_head, s->msg_tail, msg);
1253 if (msg->transmit_cont != NULL)
1255 msg->transmit_cont (msg->transmit_cont_cls, &s->target, GNUNET_SYSERR);
1266 GNUNET_free_non_null (plugin->crypto_init);
1267 GNUNET_free_non_null (plugin->cert);
1268 GNUNET_free_non_null (plugin->key);
1272 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1273 "%s server component stopped\n", plugin->name);
1279 /* end of plugin_transport_http.c */