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 * @param now schedule now or with MHD delay
51 * @return gnunet task identifier
53 static GNUNET_SCHEDULER_TaskIdentifier
54 server_schedule (struct Plugin *plugin,
55 struct MHD_Daemon *daemon_handle,
59 server_log (void *arg, const char *fmt, va_list ap)
63 vsnprintf (text, sizeof (text), fmt, ap);
65 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Server: %s\n", text);
69 * Check if incoming connection is accepted.
70 * NOTE: Here every connection is accepted
71 * @param cls plugin as closure
72 * @param addr address of incoming connection
73 * @param addr_len address length of incoming connection
74 * @return MHD_YES if connection is accepted, MHD_NO if connection is rejected
78 server_accept_cb (void *cls, const struct sockaddr *addr, socklen_t addr_len)
80 struct Plugin *plugin = cls;
82 if (plugin->cur_connections <= plugin->max_connections)
86 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
87 "Server: Cannot accept new connections\n");
95 server_load_file (const char *file)
97 struct GNUNET_DISK_FileHandle *gn_file;
101 if (0 != STAT (file, &fstat))
103 text = GNUNET_malloc (fstat.st_size + 1);
105 GNUNET_DISK_file_open (file, GNUNET_DISK_OPEN_READ,
106 GNUNET_DISK_PERM_USER_READ);
112 if (GNUNET_SYSERR == GNUNET_DISK_file_read (gn_file, text, fstat.st_size))
115 GNUNET_DISK_file_close (gn_file);
118 text[fstat.st_size] = '\0';
119 GNUNET_DISK_file_close (gn_file);
128 server_load_certificate (struct Plugin *plugin)
135 /* Get crypto init string from config
136 * If not present just use default values */
138 GNUNET_assert (GNUNET_OK ==
139 GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg,
142 &plugin->crypto_init));
145 GNUNET_CONFIGURATION_get_value_filename (plugin->env->cfg, plugin->name,
146 "KEY_FILE", &key_file))
148 key_file = GNUNET_strdup ("https_key.key");
152 GNUNET_CONFIGURATION_get_value_filename (plugin->env->cfg, plugin->name,
153 "CERT_FILE", &cert_file))
155 GNUNET_asprintf (&cert_file, "%s", "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);
197 GNUNET_free_non_null (plugin->cert);
199 GNUNET_free_non_null (plugin->crypto_init);
200 plugin->crypto_init = NULL;
202 return GNUNET_SYSERR;
204 GNUNET_assert (GNUNET_OK == GNUNET_OS_process_wait (cert_creation));
205 GNUNET_OS_process_close (cert_creation);
207 plugin->key = server_load_file (key_file);
208 plugin->cert = server_load_file (cert_file);
211 if ((plugin->key == NULL) || (plugin->cert == NULL))
213 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
215 ("No usable TLS certificate found and creating one failed!\n"),
217 GNUNET_free (key_file);
218 GNUNET_free (cert_file);
220 GNUNET_free_non_null (plugin->key);
222 GNUNET_free_non_null (plugin->cert);
224 GNUNET_free_non_null (plugin->crypto_init);
225 plugin->crypto_init = NULL;
227 return GNUNET_SYSERR;
229 GNUNET_free (key_file);
230 GNUNET_free (cert_file);
232 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "TLS certificate loaded\n");
241 * Reschedule the execution of both IPv4 and IPv6 server
242 * @param plugin the plugin
243 * @param server which server to schedule v4 or v6?
244 * @param now GNUNET_YES to schedule execution immediately, GNUNET_NO to wait
249 server_reschedule (struct Plugin *plugin, struct MHD_Daemon *server, int now)
251 if ((server == plugin->server_v4) && (plugin->server_v4 != NULL))
253 if (plugin->server_v4_task != GNUNET_SCHEDULER_NO_TASK)
255 GNUNET_SCHEDULER_cancel (plugin->server_v4_task);
256 plugin->server_v4_task = GNUNET_SCHEDULER_NO_TASK;
258 plugin->server_v4_task = server_schedule (plugin, plugin->server_v4, now);
261 if ((server == plugin->server_v6) && (plugin->server_v6 != NULL))
263 if (plugin->server_v6_task != GNUNET_SCHEDULER_NO_TASK)
265 GNUNET_SCHEDULER_cancel (plugin->server_v6_task);
266 plugin->server_v6_task = GNUNET_SCHEDULER_NO_TASK;
268 plugin->server_v6_task = server_schedule (plugin, plugin->server_v6, now);
273 * Callback called by MessageStreamTokenizer when a message has arrived
274 * @param cls current session as closure
275 * @param client clien
276 * @param message the message to be forwarded to transport service
279 server_receive_mst_cb (void *cls, void *client,
280 const struct GNUNET_MessageHeader *message)
282 struct Session *s = cls;
285 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)
297 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
298 "Server: peer `%s' address `%s' next read delayed for %llu ms\n",
299 GNUNET_i2s (&s->target),
300 http_plugin_address_to_string (NULL, s->addr, s->addrlen),
307 * Callback called by MHD when it needs data to send
308 * @param cls current session
309 * @param pos position in buffer
310 * @param buf the buffer to write data to
311 * @param max max number of bytes available in buffer
312 * @return bytes written to buffer
315 server_send_callback (void *cls, uint64_t pos, char *buf, size_t max)
317 struct Session *s = cls;
320 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, s->plugin->name,
321 "Server: %X can sent maximum %u \n", s, max);
323 struct HTTP_Message *msg;
331 if ((msg->size - msg->pos) <= max)
333 memcpy (buf, &msg->buf[msg->pos], (msg->size - msg->pos));
334 bytes_read = msg->size - msg->pos;
335 msg->pos += (msg->size - msg->pos);
339 memcpy (buf, &msg->buf[msg->pos], max);
344 /* removing message */
345 if (msg->pos == msg->size)
347 if (NULL != msg->transmit_cont)
348 msg->transmit_cont (msg->transmit_cont_cls, &s->target, GNUNET_OK);
349 GNUNET_CONTAINER_DLL_remove (s->msg_head, s->msg_tail, msg);
355 struct Plugin *plugin = s->plugin;
357 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
358 "Server: %X: sent %u bytes\n", s, bytes_read);
363 static struct ServerConnection *
364 server_lookup_session (struct Plugin *plugin,
365 struct MHD_Connection *mhd_connection, const char *url,
368 struct Session *s = NULL;
370 struct ServerConnection *sc = NULL;
371 const union MHD_ConnectionInfo *conn_info;
372 struct GNUNET_ATS_Information ats;
373 struct IPv4HttpAddress a4;
374 struct IPv6HttpAddress a6;
375 struct sockaddr_in *s4;
376 struct sockaddr_in6 *s6;
379 struct GNUNET_PeerIdentity target;
380 int check = GNUNET_NO;
385 MHD_get_connection_info (mhd_connection,
386 MHD_CONNECTION_INFO_CLIENT_ADDRESS);
387 if ((conn_info->client_addr->sa_family != AF_INET) &&
388 (conn_info->client_addr->sa_family != AF_INET6))
391 if ((strlen (&url[1]) >= 105) && (url[104] == ';'))
394 char *tagc = (char *) &url[105];
396 memcpy (&hash, &url[1], 103);
399 GNUNET_CRYPTO_hash_from_string ((const char *) &hash,
400 &(target.hashPubKey)))
402 tag = strtoul (tagc, NULL, 10);
408 if (0 == strcmp (MHD_HTTP_METHOD_PUT, method))
409 direction = _RECEIVE;
410 else if (0 == strcmp (MHD_HTTP_METHOD_GET, method))
416 if (check == GNUNET_NO)
419 plugin->cur_connections++;
422 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
423 "Server: New inbound connection from %s with tag %u\n",
424 GNUNET_i2s (&target), tag);
426 /* find duplicate session */
433 (0 == memcmp (&t->target, &target, sizeof (struct GNUNET_PeerIdentity)))
435 /* FIXME add source address comparison */
443 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
444 "Server: Duplicate session, dismissing new connection from peer `%s'\n",
445 GNUNET_i2s (&target));
450 /* find semi-session */
451 t = plugin->server_semi_head;
455 /* FIXME add source address comparison */
456 if ((0 == memcmp (&t->target, &target, sizeof (struct GNUNET_PeerIdentity)))
468 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
469 "Server: Found existing semi-session for `%s'\n",
470 GNUNET_i2s (&target));
473 if ((direction == _SEND) && (t->server_send != NULL))
476 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
477 "Server: Duplicate GET session, dismissing new connection from peer `%s'\n",
478 GNUNET_i2s (&target));
485 GNUNET_CONTAINER_DLL_remove (plugin->server_semi_head,
486 plugin->server_semi_tail, s);
487 GNUNET_CONTAINER_DLL_insert (plugin->head, plugin->tail, s);
489 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
490 "Server: Found matching semi-session, merging session for peer `%s'\n",
491 GNUNET_i2s (&target));
496 if ((direction == _RECEIVE) && (t->server_recv != NULL))
499 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
500 "Server: Duplicate PUT session, dismissing new connection from peer `%s'\n",
501 GNUNET_i2s (&target));
508 GNUNET_CONTAINER_DLL_remove (plugin->server_semi_head,
509 plugin->server_semi_tail, s);
510 GNUNET_CONTAINER_DLL_insert (plugin->head, plugin->tail, s);
512 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
513 "Server: Found matching semi-session, merging session for peer `%s'\n",
514 GNUNET_i2s (&target));
520 /* create new session */
522 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
523 "Server: Creating new session for peer `%s' \n",
524 GNUNET_i2s (&target));
528 switch (conn_info->client_addr->sa_family)
531 s4 = ((struct sockaddr_in *) conn_info->client_addr);
532 a4.u4_port = s4->sin_port;
533 memcpy (&a4.ipv4_addr, &s4->sin_addr, sizeof (struct in_addr));
535 a_len = sizeof (struct IPv4HttpAddress);
536 ats = plugin->env->get_address_type (plugin->env->cls, (const struct sockaddr *) s4, sizeof (struct sockaddr_in));
539 s6 = ((struct sockaddr_in6 *) conn_info->client_addr);
540 a6.u6_port = s6->sin6_port;
541 memcpy (&a6.ipv6_addr, &s6->sin6_addr, sizeof (struct in6_addr));
543 a_len = sizeof (struct IPv6HttpAddress);
544 ats = plugin->env->get_address_type (plugin->env->cls, (const struct sockaddr *) s6, sizeof (struct sockaddr_in6));
550 s = create_session (plugin, &target, a, a_len, NULL, NULL);
551 s->ats_address_network_type = ats.value;
553 s->inbound = GNUNET_YES;
554 s->next_receive = GNUNET_TIME_absolute_get_zero ();
556 if (0 == strcmp (MHD_HTTP_METHOD_PUT, method))
558 if (0 == strcmp (MHD_HTTP_METHOD_GET, method))
560 GNUNET_CONTAINER_DLL_insert (plugin->server_semi_head,
561 plugin->server_semi_tail, s);
566 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
567 "Server: Invalid connection request\n");
572 sc = GNUNET_malloc (sizeof (struct ServerConnection));
573 sc->mhd_conn = mhd_connection;
574 sc->direction = direction;
576 if (direction == _SEND)
578 if (direction == _RECEIVE)
581 #if MHD_VERSION >= 0x00090E00
582 int to = (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT.rel_value / 1000);
585 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
586 "Server: Setting timeout for %X to %u sec.\n", sc, to);
588 MHD_set_connection_option (mhd_connection, MHD_CONNECTION_OPTION_TIMEOUT, to);
590 struct MHD_Daemon *d = NULL;
592 if (s->addrlen == sizeof (struct IPv6HttpAddress))
593 d = plugin->server_v6;
594 if (s->addrlen == sizeof (struct IPv4HttpAddress))
595 d = plugin->server_v4;
597 server_reschedule (plugin, d, GNUNET_NO);
603 * Process GET or PUT request received via MHD. For
604 * GET, queue response that will send back our pending
605 * messages. For PUT, process incoming data and send
606 * to GNUnet core. In either case, check if a session
607 * already exists and create a new one if not.
610 server_access_cb (void *cls, struct MHD_Connection *mhd_connection,
611 const char *url, const char *method, const char *version,
612 const char *upload_data, size_t * upload_data_size,
613 void **httpSessionCache)
616 struct Plugin *plugin = cls;
617 struct ServerConnection *sc = *httpSessionCache;
618 struct Session *s = NULL;
621 struct MHD_Response *response;
623 GNUNET_assert (cls != NULL);
627 sc = server_lookup_session (plugin, mhd_connection, url, method);
629 (*httpSessionCache) = sc;
633 MHD_create_response_from_data (strlen (HTTP_ERROR_RESPONSE),
634 HTTP_ERROR_RESPONSE, MHD_NO, MHD_NO);
635 res = MHD_queue_response (mhd_connection, MHD_HTTP_NOT_FOUND, response);
636 MHD_destroy_response (response);
641 /* existing connection */
642 sc = (*httpSessionCache);
645 /* connection is to be disconnected */
646 if (sc->disconnect == GNUNET_YES)
649 MHD_create_response_from_data (strlen ("Thank you!"), "Thank you!",
651 res = MHD_queue_response (mhd_connection, MHD_HTTP_OK, response);
653 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
654 "Sent HTTP/1.1: 200 OK as PUT Response\n");
656 MHD_destroy_response (response);
660 GNUNET_assert (s != NULL);
661 /* Check if both directions are connected */
662 if ((sc->session->server_recv == NULL) || (sc->session->server_send == NULL))
665 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
666 "Server: Delayed read from `%s' `%s' since not both semi-connections are connected\n",
667 GNUNET_i2s (&s->target),
668 http_plugin_address_to_string (NULL, s->addr, s->addrlen));
673 if (sc->direction == _SEND)
676 MHD_create_response_from_callback (-1, 32 * 1024, &server_send_callback,
678 MHD_queue_response (mhd_connection, MHD_HTTP_OK, response);
679 MHD_destroy_response (response);
682 if (sc->direction == _RECEIVE)
684 if (*upload_data_size == 0)
687 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
688 "Server: Peer `%s' PUT on address `%s' connected\n",
689 GNUNET_i2s (&s->target),
690 http_plugin_address_to_string (NULL, s->addr,
697 if ((*upload_data_size > 0))
700 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
701 "Server: peer `%s' PUT on address `%s' received %u bytes\n",
702 GNUNET_i2s (&s->target),
703 http_plugin_address_to_string (NULL, s->addr,
707 struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get ();
709 if ((s->next_receive.abs_value <= now.abs_value))
712 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
713 "Server: %X: PUT with %u bytes forwarded to MST\n", s,
716 if (s->msg_tk == NULL)
718 s->msg_tk = GNUNET_SERVER_mst_create (&server_receive_mst_cb, s);
720 GNUNET_SERVER_mst_receive (s->msg_tk, s, upload_data,
721 *upload_data_size, GNUNET_NO, GNUNET_NO);
723 #if MHD_VERSION >= 0x00090E00
724 int to = (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT.rel_value / 1000);
725 struct ServerConnection *t = NULL;
728 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
729 "Server: Received %u bytes\n", *upload_data_size);
732 /* Setting timeouts for other connections */
733 if (s->server_recv != NULL)
737 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
738 "Server: Setting timeout for %X to %u sec.\n", t,
741 MHD_set_connection_option (t->mhd_conn, MHD_CONNECTION_OPTION_TIMEOUT,
744 if (s->server_send != NULL)
748 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
749 "Server: Setting timeout for %X to %u sec.\n", t,
752 MHD_set_connection_option (t->mhd_conn, MHD_CONNECTION_OPTION_TIMEOUT,
755 struct MHD_Daemon *d = NULL;
757 if (s->addrlen == sizeof (struct IPv6HttpAddress))
758 d = plugin->server_v6;
759 if (s->addrlen == sizeof (struct IPv4HttpAddress))
760 d = plugin->server_v4;
761 server_reschedule (plugin, d, GNUNET_NO);
763 (*upload_data_size) = 0;
768 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
769 "Server: %X no inbound bandwidth available! Next read was delayed by %llu ms\n",
770 s, now.abs_value - s->next_receive.abs_value);
782 server_disconnect_cb (void *cls, struct MHD_Connection *connection,
783 void **httpSessionCache)
785 struct ServerConnection *sc = *httpSessionCache;
786 struct ServerConnection *tc = NULL;
787 struct Session *s = NULL;
788 struct Session *t = NULL;
789 struct Plugin *plugin = NULL;
796 if (sc->direction == _SEND)
799 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, 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));
804 s->server_send = NULL;
806 if (s->server_recv != NULL)
809 tc->disconnect = GNUNET_YES;
810 #if MHD_VERSION >= 0x00090E00
811 MHD_set_connection_option (sc->mhd_conn, MHD_CONNECTION_OPTION_TIMEOUT,
816 if (sc->direction == _RECEIVE)
819 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
820 "Server: %X peer `%s' PUT on address `%s' disconnected\n",
821 s->server_recv, GNUNET_i2s (&s->target),
822 http_plugin_address_to_string (NULL, s->addr, s->addrlen));
824 s->server_recv = NULL;
825 if (s->server_send != NULL)
828 tc->disconnect = GNUNET_YES;
829 #if MHD_VERSION >= 0x00090E00
830 MHD_set_connection_option (sc->mhd_conn, MHD_CONNECTION_OPTION_TIMEOUT,
834 if (s->msg_tk != NULL)
836 GNUNET_SERVER_mst_destroy (s->msg_tk);
842 t = plugin->server_semi_head;
847 GNUNET_CONTAINER_DLL_remove (plugin->server_semi_head,
848 plugin->server_semi_tail, s);
849 GNUNET_CONTAINER_DLL_insert (plugin->head, plugin->tail, s);
854 plugin->cur_connections--;
856 struct MHD_Daemon *d = NULL;
858 if (s->addrlen == sizeof (struct IPv6HttpAddress))
859 d = plugin->server_v6;
860 if (s->addrlen == sizeof (struct IPv4HttpAddress))
861 d = plugin->server_v4;
862 server_reschedule (plugin, d, GNUNET_NO);
864 if ((s->server_send == NULL) && (s->server_recv == NULL))
867 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
868 "Server: peer `%s' on address `%s' disconnected\n",
869 GNUNET_i2s (&s->target),
870 http_plugin_address_to_string (NULL, s->addr, s->addrlen));
872 if (s->msg_tk != NULL)
874 GNUNET_SERVER_mst_destroy (s->msg_tk);
878 notify_session_end (s->plugin, &s->target, s);
883 server_disconnect (struct Session *s)
885 struct Plugin *plugin = s->plugin;
886 struct Session *t = plugin->head;
890 if (t->inbound == GNUNET_YES)
892 if (t->server_send != NULL)
894 ((struct ServerConnection *) t->server_send)->disconnect = GNUNET_YES;
896 if (t->server_send != NULL)
898 ((struct ServerConnection *) t->server_send)->disconnect = GNUNET_YES;
907 server_send (struct Session *s, struct HTTP_Message *msg)
909 GNUNET_CONTAINER_DLL_insert (s->msg_head, s->msg_tail, msg);
911 if (s->addrlen == sizeof (struct IPv4HttpAddress))
913 server_reschedule (s->plugin, s->plugin->server_v4, GNUNET_YES);
915 else if (s->addrlen == sizeof (struct IPv6HttpAddress))
917 server_reschedule (s->plugin, s->plugin->server_v6, GNUNET_YES);
920 return GNUNET_SYSERR;
927 * Call MHD IPv4 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_v4_run (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
935 struct Plugin *plugin = cls;
937 GNUNET_assert (cls != NULL);
939 plugin->server_v4_task = GNUNET_SCHEDULER_NO_TASK;
941 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
944 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
945 "Running IPv4 server\n");
947 GNUNET_assert (MHD_YES == MHD_run (plugin->server_v4));
948 if (plugin->server_v4 != NULL)
949 plugin->server_v4_task =
950 server_schedule (plugin, plugin->server_v4, GNUNET_NO);
955 * Call MHD IPv6 to process pending requests and then go back
956 * and schedule the next run.
957 * @param cls plugin as closure
958 * @param tc task context
961 server_v6_run (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
963 struct Plugin *plugin = cls;
965 GNUNET_assert (cls != NULL);
967 plugin->server_v6_task = GNUNET_SCHEDULER_NO_TASK;
969 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
972 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
973 "Running IPv6 server\n");
975 GNUNET_assert (MHD_YES == MHD_run (plugin->server_v6));
976 if (plugin->server_v6 != NULL)
977 plugin->server_v6_task =
978 server_schedule (plugin, plugin->server_v6, GNUNET_NO);
982 * Function that queries MHD's select sets and
983 * starts the task waiting for them.
984 * @param plugin plugin
985 * @param daemon_handle the MHD daemon handle
986 * @return gnunet task identifier
988 static GNUNET_SCHEDULER_TaskIdentifier
989 server_schedule (struct Plugin *plugin, struct MHD_Daemon *daemon_handle,
992 GNUNET_SCHEDULER_TaskIdentifier ret;
996 struct GNUNET_NETWORK_FDSet *wrs;
997 struct GNUNET_NETWORK_FDSet *wws;
998 struct GNUNET_NETWORK_FDSet *wes;
1000 unsigned long long timeout;
1001 static unsigned long long last_timeout = 0;
1004 struct GNUNET_TIME_Relative tv;
1006 ret = GNUNET_SCHEDULER_NO_TASK;
1010 wrs = GNUNET_NETWORK_fdset_create ();
1011 wes = GNUNET_NETWORK_fdset_create ();
1012 wws = GNUNET_NETWORK_fdset_create ();
1014 GNUNET_assert (MHD_YES == MHD_get_fdset (daemon_handle, &rs, &ws, &es, &max));
1015 haveto = MHD_get_timeout (daemon_handle, &timeout);
1016 if (haveto == MHD_YES)
1018 if (timeout != last_timeout)
1021 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1022 "SELECT Timeout changed from %llu to %llu\n",
1023 last_timeout, timeout);
1025 last_timeout = timeout;
1027 tv.rel_value = (uint64_t) timeout;
1030 tv = GNUNET_TIME_UNIT_SECONDS;
1031 /* Force immediate run, since we have outbound data to send */
1032 if (now == GNUNET_YES)
1033 tv = GNUNET_TIME_UNIT_MILLISECONDS;
1034 GNUNET_NETWORK_fdset_copy_native (wrs, &rs, max + 1);
1035 GNUNET_NETWORK_fdset_copy_native (wws, &ws, max + 1);
1036 GNUNET_NETWORK_fdset_copy_native (wes, &es, max + 1);
1038 if (daemon_handle == plugin->server_v4)
1040 if (plugin->server_v4_task != GNUNET_SCHEDULER_NO_TASK)
1042 GNUNET_SCHEDULER_cancel (plugin->server_v4_task);
1043 plugin->server_v4_task = GNUNET_SCHEDULER_NO_TASK;
1046 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
1047 "Scheduling IPv4 server task in %llu ms\n", tv);
1050 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1051 GNUNET_SCHEDULER_NO_TASK, tv, wrs, wws,
1052 &server_v4_run, plugin);
1054 if (daemon_handle == plugin->server_v6)
1056 if (plugin->server_v6_task != GNUNET_SCHEDULER_NO_TASK)
1058 GNUNET_SCHEDULER_cancel (plugin->server_v6_task);
1059 plugin->server_v6_task = GNUNET_SCHEDULER_NO_TASK;
1062 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
1063 "Scheduling IPv6 server task in %llu ms\n", tv);
1066 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1067 GNUNET_SCHEDULER_NO_TASK, tv, wrs, wws,
1068 &server_v6_run, plugin);
1070 GNUNET_NETWORK_fdset_destroy (wrs);
1071 GNUNET_NETWORK_fdset_destroy (wws);
1072 GNUNET_NETWORK_fdset_destroy (wes);
1077 server_start (struct Plugin *plugin)
1079 int res = GNUNET_OK;
1080 unsigned int timeout;
1083 res = server_load_certificate (plugin);
1084 if (res == GNUNET_SYSERR)
1086 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
1087 "Could not load or create server certificate! Loading plugin failed!\n");
1093 #if MHD_VERSION >= 0x00090E00
1094 timeout = HTTP_NOT_VALIDATED_TIMEOUT.rel_value / 1000;
1095 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1096 "MHD can set timeout per connection! Default time out %u sec.\n",
1099 timeout = GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT.rel_value / 1000;
1100 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, plugin->name,
1101 "MHD cannot set timeout per connection! Default time out %u sec.\n",
1104 plugin->server_v4 = NULL;
1105 if (plugin->ipv4 == GNUNET_YES)
1107 plugin->server_v4 = MHD_start_daemon (
1114 MHD_NO_FLAG, plugin->port,
1115 &server_accept_cb, plugin,
1116 &server_access_cb, plugin,
1117 MHD_OPTION_SOCK_ADDR,
1118 (struct sockaddr_in *)
1119 plugin->server_addr_v4,
1120 MHD_OPTION_CONNECTION_LIMIT,
1122 plugin->max_connections,
1124 MHD_OPTION_HTTPS_PRIORITIES,
1125 plugin->crypto_init,
1126 MHD_OPTION_HTTPS_MEM_KEY,
1128 MHD_OPTION_HTTPS_MEM_CERT,
1131 MHD_OPTION_CONNECTION_TIMEOUT,
1133 MHD_OPTION_CONNECTION_MEMORY_LIMIT,
1135 GNUNET_SERVER_MAX_MESSAGE_SIZE),
1136 MHD_OPTION_NOTIFY_COMPLETED,
1137 &server_disconnect_cb, plugin,
1138 MHD_OPTION_EXTERNAL_LOGGER,
1139 server_log, NULL, MHD_OPTION_END);
1141 plugin->server_v6 = NULL;
1142 if (plugin->ipv6 == GNUNET_YES)
1144 plugin->server_v6 = MHD_start_daemon (
1151 MHD_USE_IPv6, plugin->port,
1152 &server_accept_cb, plugin,
1153 &server_access_cb, plugin,
1154 MHD_OPTION_SOCK_ADDR,
1155 (struct sockaddr_in6 *)
1156 plugin->server_addr_v6,
1157 MHD_OPTION_CONNECTION_LIMIT,
1159 plugin->max_connections,
1161 MHD_OPTION_HTTPS_PRIORITIES,
1162 plugin->crypto_init,
1163 MHD_OPTION_HTTPS_MEM_KEY,
1165 MHD_OPTION_HTTPS_MEM_CERT,
1168 MHD_OPTION_CONNECTION_TIMEOUT,
1170 MHD_OPTION_CONNECTION_MEMORY_LIMIT,
1172 GNUNET_SERVER_MAX_MESSAGE_SIZE),
1173 MHD_OPTION_NOTIFY_COMPLETED,
1174 &server_disconnect_cb, plugin,
1175 MHD_OPTION_EXTERNAL_LOGGER,
1176 server_log, NULL, MHD_OPTION_END);
1180 if ((plugin->ipv4 == GNUNET_YES) && (plugin->server_v4 == NULL))
1182 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
1183 "Failed to start %s IPv4 server component on port %u\n",
1184 plugin->name, plugin->port);
1185 return GNUNET_SYSERR;
1187 server_reschedule (plugin, plugin->server_v4, GNUNET_NO);
1189 if ((plugin->ipv6 == GNUNET_YES) && (plugin->server_v6 == NULL))
1191 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
1192 "Failed to start %s IPv6 server component on port %u\n",
1193 plugin->name, plugin->port);
1194 return GNUNET_SYSERR;
1196 server_reschedule (plugin, plugin->server_v6, GNUNET_NO);
1200 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1201 "%s server component started on port %u\n", plugin->name,
1208 server_stop (struct Plugin *plugin)
1210 struct Session *s = NULL;
1211 struct Session *t = NULL;
1214 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name, "server_stop\n");
1217 struct MHD_Daemon *server_v4_tmp = plugin->server_v4;
1219 plugin->server_v4 = NULL;
1220 struct MHD_Daemon *server_v6_tmp = plugin->server_v6;
1222 plugin->server_v6 = NULL;
1224 if (plugin->server_v4_task != GNUNET_SCHEDULER_NO_TASK)
1226 GNUNET_SCHEDULER_cancel (plugin->server_v4_task);
1227 plugin->server_v4_task = GNUNET_SCHEDULER_NO_TASK;
1230 if (plugin->server_v6_task != GNUNET_SCHEDULER_NO_TASK)
1232 GNUNET_SCHEDULER_cancel (plugin->server_v6_task);
1233 plugin->server_v6_task = GNUNET_SCHEDULER_NO_TASK;
1236 if (server_v6_tmp != NULL)
1238 MHD_stop_daemon (server_v4_tmp);
1240 if (server_v6_tmp != NULL)
1242 MHD_stop_daemon (server_v6_tmp);
1245 /* cleaning up semi-sessions never propagated */
1246 s = plugin->server_semi_head;
1250 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
1251 "Deleting semi-sessions %p\n", s);
1254 struct HTTP_Message *msg = s->msg_head;
1255 struct HTTP_Message *tmp = NULL;
1261 GNUNET_CONTAINER_DLL_remove (s->msg_head, s->msg_tail, msg);
1262 if (msg->transmit_cont != NULL)
1264 msg->transmit_cont (msg->transmit_cont_cls, &s->target, GNUNET_SYSERR);
1275 GNUNET_free_non_null (plugin->crypto_init);
1276 GNUNET_free_non_null (plugin->cert);
1277 GNUNET_free_non_null (plugin->key);
1281 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1282 "%s server component stopped\n", plugin->name);
1288 /* end of plugin_transport_http.c */