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, int now)
248 if (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 (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;
316 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, s->plugin->name,
317 "Server: %X can sent maximum %u \n", s, max);
319 struct HTTP_Message *msg;
327 if ((msg->size - msg->pos) <= max)
329 memcpy (buf, &msg->buf[msg->pos], (msg->size - msg->pos));
330 bytes_read = msg->size - msg->pos;
331 msg->pos += (msg->size - msg->pos);
335 memcpy (buf, &msg->buf[msg->pos], max);
340 /* removing message */
341 if (msg->pos == msg->size)
343 if (NULL != msg->transmit_cont)
344 msg->transmit_cont (msg->transmit_cont_cls, &s->target, GNUNET_OK);
345 GNUNET_CONTAINER_DLL_remove (s->msg_head, s->msg_tail, msg);
351 struct Plugin *plugin = s->plugin;
352 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, 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;
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;
380 MHD_get_connection_info (mhd_connection,
381 MHD_CONNECTION_INFO_CLIENT_ADDRESS);
382 if ((conn_info->client_addr->sa_family != AF_INET) &&
383 (conn_info->client_addr->sa_family != AF_INET6))
386 if ((strlen (&url[1]) >= 105) && (url[104] == ';'))
389 char *tagc = (char *) &url[105];
391 memcpy (&hash, &url[1], 103);
394 GNUNET_CRYPTO_hash_from_string ((const char *) &hash,
395 &(target.hashPubKey)))
397 tag = strtoul (tagc, NULL, 10);
403 if (0 == strcmp (MHD_HTTP_METHOD_PUT, method))
404 direction = _RECEIVE;
405 if (0 == strcmp (MHD_HTTP_METHOD_GET, method))
408 if (check == GNUNET_NO)
411 plugin->cur_connections++;
414 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
415 "Server: New inbound connection from %s with tag %u\n",
416 GNUNET_i2s (&target), tag);
418 /* find duplicate session */
425 (0 == memcmp (&t->target, &target, sizeof (struct GNUNET_PeerIdentity)))
427 /* FIXME add source address comparison */
435 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
436 "Server: Duplicate session, dismissing new connection from peer `%s'\n",
437 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)))
460 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
461 "Server: Found existing semi-session for `%s'\n",
462 GNUNET_i2s (&target));
465 if ((direction == _SEND) && (t->server_send != NULL))
468 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
469 "Server: Duplicate GET session, dismissing new connection from peer `%s'\n",
470 GNUNET_i2s (&target));
477 GNUNET_CONTAINER_DLL_remove (plugin->server_semi_head,
478 plugin->server_semi_tail, s);
479 GNUNET_CONTAINER_DLL_insert (plugin->head, plugin->tail, s);
481 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
482 "Server: Found matching semi-session, merging session for peer `%s'\n",
483 GNUNET_i2s (&target));
488 if ((direction == _RECEIVE) && (t->server_recv != NULL))
491 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
492 "Server: Duplicate PUT session, dismissing new connection from peer `%s'\n",
493 GNUNET_i2s (&target));
500 GNUNET_CONTAINER_DLL_remove (plugin->server_semi_head,
501 plugin->server_semi_tail, s);
502 GNUNET_CONTAINER_DLL_insert (plugin->head, plugin->tail, s);
504 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
505 "Server: Found matching semi-session, merging session for peer `%s'\n",
506 GNUNET_i2s (&target));
512 /* 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));
519 switch (conn_info->client_addr->sa_family)
522 s4 = ((struct sockaddr_in *) conn_info->client_addr);
523 a4.u4_port = s4->sin_port;
524 memcpy (&a4.ipv4_addr, &s4->sin_addr, sizeof (struct in_addr));
526 a_len = sizeof (struct IPv4HttpAddress);
529 s6 = ((struct sockaddr_in6 *) conn_info->client_addr);
530 a6.u6_port = s6->sin6_port;
531 memcpy (&a6.ipv6_addr, &s6->sin6_addr, sizeof (struct in6_addr));
533 a_len = sizeof (struct IPv6HttpAddress);
540 s = create_session (plugin, &target, a, a_len, NULL, NULL);
542 s->inbound = GNUNET_YES;
543 s->next_receive = GNUNET_TIME_absolute_get_zero ();
545 if (0 == strcmp (MHD_HTTP_METHOD_PUT, method))
547 if (0 == strcmp (MHD_HTTP_METHOD_GET, method))
549 GNUNET_CONTAINER_DLL_insert (plugin->server_semi_head,
550 plugin->server_semi_tail, s);
555 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
556 "Server: Invalid connection request\n");
561 sc = GNUNET_malloc (sizeof (struct ServerConnection));
562 sc->mhd_conn = mhd_connection;
563 sc->direction = direction;
565 if (direction == _SEND)
567 if (direction == _RECEIVE)
570 #if MHD_VERSION >= 0x00090E00
571 int to = (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT.rel_value / 1000);
574 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
575 "Server: Setting timeout for %X to %u sec.\n", sc, to);
577 MHD_set_connection_option (mhd_connection, MHD_CONNECTION_OPTION_TIMEOUT, to);
578 server_reschedule (plugin, GNUNET_NO);
584 * Process GET or PUT request received via MHD. For
585 * GET, queue response that will send back our pending
586 * messages. For PUT, process incoming data and send
587 * to GNUnet core. In either case, check if a session
588 * already exists and create a new one if not.
591 server_access_cb (void *cls, struct MHD_Connection *mhd_connection,
592 const char *url, const char *method, const char *version,
593 const char *upload_data, size_t * upload_data_size,
594 void **httpSessionCache)
597 struct Plugin *plugin = cls;
598 struct ServerConnection *sc = *httpSessionCache;
599 struct Session *s = NULL;
602 struct MHD_Response *response;
604 GNUNET_assert (cls != NULL);
608 sc = server_lookup_session (plugin, mhd_connection, url, method);
610 (*httpSessionCache) = sc;
614 MHD_create_response_from_data (strlen (HTTP_ERROR_RESPONSE),
615 HTTP_ERROR_RESPONSE, MHD_NO, MHD_NO);
616 res = MHD_queue_response (mhd_connection, MHD_HTTP_NOT_FOUND, response);
617 MHD_destroy_response (response);
622 /* existing connection */
623 sc = (*httpSessionCache);
626 /* connection is to be disconnected */
627 if (sc->disconnect == GNUNET_YES)
630 MHD_create_response_from_data (strlen ("Thank you!"), "Thank you!",
632 res = MHD_queue_response (mhd_connection, MHD_HTTP_OK, response);
634 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
635 "Sent HTTP/1.1: 200 OK as PUT Response\n");
637 MHD_destroy_response (response);
641 GNUNET_assert (s != NULL);
642 /* Check if both directions are connected */
643 if ((sc->session->server_recv == NULL) || (sc->session->server_send == NULL))
648 if (sc->direction == _SEND)
651 MHD_create_response_from_callback (-1, 32 * 1024, &server_send_callback,
653 res = MHD_queue_response (mhd_connection, MHD_HTTP_OK, response);
654 MHD_destroy_response (response);
657 if (sc->direction == _RECEIVE)
659 if (*upload_data_size == 0)
662 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
663 "Server: Peer `%s' PUT on address `%s' connected\n",
664 GNUNET_i2s (&s->target),
665 http_plugin_address_to_string (NULL, s->addr, s->addrlen));
671 if ((*upload_data_size > 0))
674 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
675 "Server: peer `%s' PUT on address `%s' received %u bytes\n",
676 GNUNET_i2s (&s->target),
677 http_plugin_address_to_string (NULL,s->addr, s->addrlen),
680 struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get ();
682 if ((s->next_receive.abs_value <= now.abs_value))
685 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
686 "Server: %X: PUT with %u bytes forwarded to MST\n", s,
689 if (s->msg_tk == NULL)
691 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;
702 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
703 "Server: Received %u bytes\n", *upload_data_size);
706 /* Setting timeouts for other connections */
707 if (s->server_recv != NULL)
711 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
712 "Server: Setting timeout for %X to %u sec.\n", t,
715 MHD_set_connection_option (t->mhd_conn, MHD_CONNECTION_OPTION_TIMEOUT,
718 if (s->server_send != NULL)
722 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
723 "Server: Setting timeout for %X to %u sec.\n", t,
726 MHD_set_connection_option (t->mhd_conn, MHD_CONNECTION_OPTION_TIMEOUT,
729 server_reschedule (plugin, GNUNET_NO);
731 (*upload_data_size) = 0;
736 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
737 "Server: %X no inbound bandwidth available! Next read was delayed by %llu ms\n",
738 s, now.abs_value - s->next_receive.abs_value);
750 server_disconnect_cb (void *cls, struct MHD_Connection *connection,
751 void **httpSessionCache)
753 struct ServerConnection *sc = *httpSessionCache;
754 struct ServerConnection *tc = *httpSessionCache;
755 struct Session *s = NULL;
756 struct Session *t = NULL;
757 struct Plugin *plugin = NULL;
764 if (sc->direction == _SEND)
767 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
768 "Server: %X peer `%s' GET on address `%s' disconnected\n",
769 s->server_send, GNUNET_i2s (&s->target),
770 http_plugin_address_to_string (NULL, s->addr, s->addrlen));
772 s->server_send = NULL;
774 if (s->server_recv != NULL)
777 tc->disconnect = GNUNET_YES;
778 #if MHD_VERSION >= 0x00090E00
779 MHD_set_connection_option (sc->mhd_conn, MHD_CONNECTION_OPTION_TIMEOUT,
784 if (sc->direction == _RECEIVE)
787 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
788 "Server: %X peer `%s' PUT on address `%s' disconnected\n",
789 s->server_recv, GNUNET_i2s (&s->target),
790 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 server_reschedule (plugin, GNUNET_NO);
826 if ((s->server_send == NULL) && (s->server_recv == NULL))
829 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
830 "Server: peer `%s' on address `%s' disconnected\n",
831 GNUNET_i2s (&s->target),
832 http_plugin_address_to_string (NULL, s->addr, s->addrlen));
834 if (s->msg_tk != NULL)
836 GNUNET_SERVER_mst_destroy (s->msg_tk);
840 notify_session_end (s->plugin, &s->target, s);
845 server_disconnect (struct Session *s)
847 struct Plugin *plugin = s->plugin;
848 struct Session *t = plugin->head;
852 if (t->inbound == GNUNET_YES)
854 if (t->server_send != NULL)
856 ((struct ServerConnection *) t->server_send)->disconnect = GNUNET_YES;
858 if (t->server_send != NULL)
860 ((struct ServerConnection *) t->server_send)->disconnect = GNUNET_YES;
869 server_send (struct Session *s, struct HTTP_Message *msg)
871 GNUNET_CONTAINER_DLL_insert (s->msg_head, s->msg_tail, msg);
872 server_reschedule (s->plugin, GNUNET_YES);
879 * Call MHD IPv4 to process pending requests and then go back
880 * and schedule the next run.
881 * @param cls plugin as closure
882 * @param tc task context
885 server_v4_run (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
887 struct Plugin *plugin = cls;
889 GNUNET_assert (cls != NULL);
891 plugin->server_v4_task = GNUNET_SCHEDULER_NO_TASK;
893 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
896 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
897 "Running IPv4 server\n");
899 GNUNET_assert (MHD_YES == MHD_run (plugin->server_v4));
900 if (plugin->server_v4 != NULL)
901 plugin->server_v4_task =
902 server_schedule (plugin, plugin->server_v4, GNUNET_NO);
907 * Call MHD IPv6 to process pending requests and then go back
908 * and schedule the next run.
909 * @param cls plugin as closure
910 * @param tc task context
913 server_v6_run (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
915 struct Plugin *plugin = cls;
917 GNUNET_assert (cls != NULL);
919 plugin->server_v6_task = GNUNET_SCHEDULER_NO_TASK;
921 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
924 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
925 "Running IPv6 server\n");
927 GNUNET_assert (MHD_YES == MHD_run (plugin->server_v6));
928 if (plugin->server_v6 != NULL)
929 plugin->server_v6_task =
930 server_schedule (plugin, plugin->server_v6, GNUNET_NO);
934 * Function that queries MHD's select sets and
935 * starts the task waiting for them.
936 * @param plugin plugin
937 * @param daemon_handle the MHD daemon handle
938 * @return gnunet task identifier
940 static GNUNET_SCHEDULER_TaskIdentifier
941 server_schedule (struct Plugin *plugin, struct MHD_Daemon *daemon_handle,
944 GNUNET_SCHEDULER_TaskIdentifier ret;
948 struct GNUNET_NETWORK_FDSet *wrs;
949 struct GNUNET_NETWORK_FDSet *wws;
950 struct GNUNET_NETWORK_FDSet *wes;
952 unsigned long long timeout;
953 static unsigned long long last_timeout = 0;
956 struct GNUNET_TIME_Relative tv;
958 ret = GNUNET_SCHEDULER_NO_TASK;
962 wrs = GNUNET_NETWORK_fdset_create ();
963 wes = GNUNET_NETWORK_fdset_create ();
964 wws = GNUNET_NETWORK_fdset_create ();
966 GNUNET_assert (MHD_YES == MHD_get_fdset (daemon_handle, &rs, &ws, &es, &max));
967 haveto = MHD_get_timeout (daemon_handle, &timeout);
968 if (haveto == MHD_YES)
970 if (timeout != last_timeout)
973 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
974 "SELECT Timeout changed from %llu to %llu\n",
975 last_timeout, timeout);
977 last_timeout = timeout;
979 tv.rel_value = (uint64_t) timeout;
982 tv = GNUNET_TIME_UNIT_SECONDS;
983 /* Force immediate run, since we have outbound data to send */
984 if (now == GNUNET_YES)
985 tv = GNUNET_TIME_UNIT_MILLISECONDS;
986 GNUNET_NETWORK_fdset_copy_native (wrs, &rs, max + 1);
987 GNUNET_NETWORK_fdset_copy_native (wws, &ws, max + 1);
988 GNUNET_NETWORK_fdset_copy_native (wes, &es, max + 1);
990 if (daemon_handle == plugin->server_v4)
992 if (plugin->server_v4_task != GNUNET_SCHEDULER_NO_TASK)
994 GNUNET_SCHEDULER_cancel (plugin->server_v4_task);
995 plugin->server_v4_task = GNUNET_SCHEDULER_NO_TASK;
998 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
999 "Scheduling IPv4 server task in %llu ms\n", tv);
1002 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1003 GNUNET_SCHEDULER_NO_TASK, tv, wrs, wws,
1004 &server_v4_run, plugin);
1006 if (daemon_handle == plugin->server_v6)
1008 if (plugin->server_v6_task != GNUNET_SCHEDULER_NO_TASK)
1010 GNUNET_SCHEDULER_cancel (plugin->server_v6_task);
1011 plugin->server_v6_task = GNUNET_SCHEDULER_NO_TASK;
1014 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
1015 "Scheduling IPv6 server task in %llu ms\n", tv);
1018 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1019 GNUNET_SCHEDULER_NO_TASK, tv, wrs, wws,
1020 &server_v6_run, plugin);
1022 GNUNET_NETWORK_fdset_destroy (wrs);
1023 GNUNET_NETWORK_fdset_destroy (wws);
1024 GNUNET_NETWORK_fdset_destroy (wes);
1029 server_start (struct Plugin *plugin)
1031 int res = GNUNET_OK;
1032 unsigned int timeout;
1035 res = server_load_certificate (plugin);
1036 if (res == GNUNET_SYSERR)
1038 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
1039 "Could not load or create server certificate! Loading plugin failed!\n");
1045 #if MHD_VERSION >= 0x00090E00
1046 timeout = GNUNET_CONSTANTS_DISCONNECT_SESSION_TIMEOUT.rel_value / 1000;
1047 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1048 "MHD can set timeout per connection! Default time out %u sec.\n",
1051 timeout = GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT.rel_value / 1000;
1052 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, plugin->name,
1053 "MHD cannot set timeout per connection! Default time out %u sec.\n",
1056 plugin->server_v4 = NULL;
1057 if (plugin->ipv4 == GNUNET_YES)
1059 plugin->server_v4 = MHD_start_daemon (
1066 MHD_NO_FLAG, plugin->port,
1067 &server_accept_cb, plugin,
1068 &server_access_cb, plugin,
1069 MHD_OPTION_SOCK_ADDR,
1070 (struct sockaddr_in *)
1071 plugin->server_addr_v4,
1072 MHD_OPTION_CONNECTION_LIMIT,
1074 plugin->max_connections,
1076 MHD_OPTION_HTTPS_PRIORITIES,
1077 plugin->crypto_init,
1078 MHD_OPTION_HTTPS_MEM_KEY,
1080 MHD_OPTION_HTTPS_MEM_CERT,
1083 MHD_OPTION_CONNECTION_TIMEOUT,
1085 MHD_OPTION_CONNECTION_MEMORY_LIMIT,
1087 GNUNET_SERVER_MAX_MESSAGE_SIZE),
1088 MHD_OPTION_NOTIFY_COMPLETED,
1089 &server_disconnect_cb, plugin,
1090 MHD_OPTION_EXTERNAL_LOGGER,
1091 server_log, NULL, MHD_OPTION_END);
1093 plugin->server_v6 = NULL;
1094 if (plugin->ipv6 == GNUNET_YES)
1096 plugin->server_v6 = MHD_start_daemon (
1103 MHD_USE_IPv6, plugin->port,
1104 &server_accept_cb, plugin,
1105 &server_access_cb, plugin,
1106 MHD_OPTION_SOCK_ADDR,
1107 (struct sockaddr_in6 *)
1108 plugin->server_addr_v6,
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);
1132 if ((plugin->ipv4 == GNUNET_YES) && (plugin->server_v4 == NULL))
1134 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
1135 "Failed to start %s IPv4 server component on port %u\n",
1136 plugin->name, plugin->port);
1137 return GNUNET_SYSERR;
1139 if ((plugin->ipv6 == GNUNET_YES) && (plugin->server_v6 == NULL))
1141 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
1142 "Failed to start %s IPv6 server component on port %u\n",
1143 plugin->name, plugin->port);
1144 return GNUNET_SYSERR;
1147 server_reschedule (plugin, GNUNET_NO);
1150 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1151 "%s server component started on port %u\n", plugin->name,
1158 server_stop (struct Plugin *plugin)
1160 struct Session *s = NULL;
1161 struct Session *t = NULL;
1164 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
1168 struct MHD_Daemon *server_v4_tmp = plugin->server_v4;
1170 plugin->server_v4 = NULL;
1171 struct MHD_Daemon *server_v6_tmp = plugin->server_v6;
1173 plugin->server_v6 = NULL;
1175 if (plugin->server_v4_task != GNUNET_SCHEDULER_NO_TASK)
1177 GNUNET_SCHEDULER_cancel (plugin->server_v4_task);
1178 plugin->server_v4_task = GNUNET_SCHEDULER_NO_TASK;
1181 if (plugin->server_v6_task != GNUNET_SCHEDULER_NO_TASK)
1183 GNUNET_SCHEDULER_cancel (plugin->server_v6_task);
1184 plugin->server_v6_task = GNUNET_SCHEDULER_NO_TASK;
1187 if (server_v6_tmp != NULL)
1189 MHD_stop_daemon (server_v4_tmp);
1191 if (server_v6_tmp != NULL)
1193 MHD_stop_daemon (server_v6_tmp);
1196 /* cleaning up semi-sessions never propagated */
1197 s = plugin->server_semi_head;
1201 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
1202 "Deleting semi-sessions %p\n", s);
1205 struct HTTP_Message * msg = s->msg_head;
1206 struct HTTP_Message * tmp = s->msg_head;
1211 GNUNET_CONTAINER_DLL_remove(s->msg_head,s->msg_tail, msg);
1212 if (msg->transmit_cont != NULL)
1214 msg->transmit_cont(msg->transmit_cont_cls, &s->target, GNUNET_SYSERR);
1225 GNUNET_free_non_null (plugin->crypto_init);
1226 GNUNET_free_non_null (plugin->cert);
1227 GNUNET_free_non_null (plugin->key);
1231 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1232 "%s server component stopped\n", plugin->name);
1238 /* end of plugin_transport_http.c */