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, int now);
56 server_log (void *arg, const char *fmt, va_list ap)
60 vsnprintf (text, sizeof (text), fmt, ap);
62 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Server: %s\n", text);
66 * Check if incoming connection is accepted.
67 * NOTE: Here every connection is accepted
68 * @param cls plugin as closure
69 * @param addr address of incoming connection
70 * @param addr_len address length of incoming connection
71 * @return MHD_YES if connection is accepted, MHD_NO if connection is rejected
75 server_accept_cb (void *cls, const struct sockaddr *addr, socklen_t addr_len)
77 struct Plugin * plugin = cls;
79 if (plugin->cur_connections <= plugin->max_connections)
83 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Server: Cannot accept new connections\n");
91 server_load_file (const char *file)
93 struct GNUNET_DISK_FileHandle *gn_file;
97 if (0 != STAT (file, &fstat))
99 text = GNUNET_malloc (fstat.st_size + 1);
101 GNUNET_DISK_file_open (file, GNUNET_DISK_OPEN_READ,
102 GNUNET_DISK_PERM_USER_READ);
108 if (GNUNET_SYSERR == GNUNET_DISK_file_read (gn_file, text, fstat.st_size))
111 GNUNET_DISK_file_close (gn_file);
114 text[fstat.st_size] = '\0';
115 GNUNET_DISK_file_close (gn_file);
124 server_load_certificate (struct Plugin *plugin)
131 /* Get crypto init string from config
132 * If not present just use default values */
133 GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg, plugin->name,
134 "CRYPTO_INIT", &plugin->crypto_init);
137 GNUNET_CONFIGURATION_get_value_filename (plugin->env->cfg, plugin->name,
138 "KEY_FILE", &key_file))
140 key_file = "https_key.key";
144 GNUNET_CONFIGURATION_get_value_filename (plugin->env->cfg, plugin->name,
145 "CERT_FILE", &cert_file))
147 cert_file = "https_cert.crt";
150 /* read key & certificates from file */
152 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
153 "Loading TLS certificate from key-file `%s' cert-file`%s'\n",
154 key_file, cert_file);
157 plugin->key = server_load_file (key_file);
158 plugin->cert = server_load_file (cert_file);
160 if ((plugin->key == NULL) || (plugin->cert == NULL))
162 struct GNUNET_OS_Process *cert_creation;
164 GNUNET_free_non_null (plugin->key);
166 GNUNET_free_non_null (plugin->cert);
170 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
171 "No usable TLS certificate found, creating certificate\n");
175 GNUNET_OS_start_process (NULL, NULL,
176 "gnunet-transport-certificate-creation",
177 "gnunet-transport-certificate-creation",
178 key_file, cert_file, NULL);
179 if (cert_creation == NULL)
181 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
183 ("Could not create a new TLS certificate, program `gnunet-transport-certificate-creation' could not be started!\n"));
184 GNUNET_free (key_file);
185 GNUNET_free (cert_file);
187 GNUNET_free_non_null (plugin->key);
189 GNUNET_free_non_null (plugin->cert);
191 GNUNET_free_non_null (plugin->crypto_init);
192 plugin->crypto_init = NULL;
194 return GNUNET_SYSERR;
196 GNUNET_assert (GNUNET_OK == GNUNET_OS_process_wait (cert_creation));
197 GNUNET_OS_process_close (cert_creation);
199 plugin->key = server_load_file (key_file);
200 plugin->cert = server_load_file (cert_file);
203 if ((plugin->key == NULL) || (plugin->cert == NULL))
205 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
207 ("No usable TLS certificate found and creating one failed!\n"),
209 GNUNET_free (key_file);
210 GNUNET_free (cert_file);
212 GNUNET_free_non_null (plugin->key);
214 GNUNET_free_non_null (plugin->cert);
216 GNUNET_free_non_null (plugin->crypto_init);
217 plugin->crypto_init = NULL;
219 return GNUNET_SYSERR;
221 GNUNET_free (key_file);
222 GNUNET_free (cert_file);
224 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "TLS certificate loaded\n");
233 * Reschedule the execution of both IPv4 and IPv6 server
234 * @param plugin the plugin
235 * @param now GNUNET_YES to schedule execution immediately, GNUNET_NO to wait
240 server_reschedule (struct Plugin *plugin, int now)
242 if (plugin->server_v4 != NULL)
244 if (plugin->server_v4_task != GNUNET_SCHEDULER_NO_TASK)
246 GNUNET_SCHEDULER_cancel(plugin->server_v4_task);
247 plugin->server_v4_task = GNUNET_SCHEDULER_NO_TASK;
249 plugin->server_v4_task = server_schedule (plugin, plugin->server_v4, now);
252 if (plugin->server_v6 != NULL)
254 if (plugin->server_v6_task != GNUNET_SCHEDULER_NO_TASK)
256 GNUNET_SCHEDULER_cancel(plugin->server_v6_task);
257 plugin->server_v6_task = GNUNET_SCHEDULER_NO_TASK;
259 plugin->server_v6_task = server_schedule (plugin, plugin->server_v6, now);
264 * Callback called by MessageStreamTokenizer when a message has arrived
265 * @param cls current session as closure
266 * @param client clien
267 * @param message the message to be forwarded to transport service
270 server_receive_mst_cb (void *cls, void *client,
271 const struct GNUNET_MessageHeader *message)
273 struct Session *s = cls;
275 struct Plugin *plugin = s->plugin;
277 struct GNUNET_TIME_Relative delay;
279 delay = http_plugin_receive (s, &s->target, message, s, s->addr, s->addrlen);
281 s->next_receive = GNUNET_TIME_absolute_add(GNUNET_TIME_absolute_get(), delay);
283 if (delay.rel_value > 0)
286 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name, "Server: peer `%s' address `%s' next read delayed for %llu ms\n",
287 GNUNET_i2s (&s->target), GNUNET_a2s (s->addr, s->addrlen), delay);
293 * Callback called by MHD when it needs data to send
294 * @param cls current session
295 * @param pos position in buffer
296 * @param buf the buffer to write data to
297 * @param max max number of bytes available in buffer
298 * @return bytes written to buffer
301 server_send_callback (void *cls, uint64_t pos, char *buf, size_t max)
303 struct Session *s = cls;
305 struct HTTP_Message *msg;
312 if ((msg->size - msg->pos) <= max)
314 memcpy (buf, &msg->buf[msg->pos], (msg->size - msg->pos));
315 bytes_read = msg->size - msg->pos;
316 msg->pos += (msg->size - msg->pos);
320 memcpy (buf, &msg->buf[msg->pos], max);
325 /* removing message */
326 if (msg->pos == msg->size)
328 if (NULL != msg->transmit_cont)
329 msg->transmit_cont (msg->transmit_cont_cls, &s->target, GNUNET_OK);
330 GNUNET_CONTAINER_DLL_remove(s->msg_head, s->msg_tail, msg);
336 struct Plugin *plugin = s->plugin;
337 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
338 "Server: %X: sent %u bytes\n",
344 static struct ServerConnection *
345 server_lookup_session (struct Plugin *plugin,
346 struct MHD_Connection *mhd_connection,
350 struct Session *s = NULL;
352 struct ServerConnection *sc = NULL;
353 const union MHD_ConnectionInfo *conn_info;
355 struct IPv4HttpAddress a4;
356 struct IPv6HttpAddress a6;
357 struct sockaddr_in * s4;
358 struct sockaddr_in6 * s6;
361 struct GNUNET_PeerIdentity target;
363 int check = GNUNET_NO;
367 conn_info = MHD_get_connection_info (mhd_connection, MHD_CONNECTION_INFO_CLIENT_ADDRESS);
368 if (conn_info->client_addr->sa_family == AF_INET)
369 addrlen = sizeof (struct sockaddr_in);
370 else if (conn_info->client_addr->sa_family == AF_INET6)
371 addrlen = sizeof (struct sockaddr_in6);
375 if ((strlen(&url[1]) >= 105) && (url[104] == ';'))
378 char * tagc = (char *) &url[105];
379 memcpy(&hash, &url[1], 103);
381 if (GNUNET_OK == GNUNET_CRYPTO_hash_from_string ((const char *) &hash,
382 &(target.hashPubKey)))
384 tag = strtoul (tagc, NULL, 10);
390 if (0 == strcmp (MHD_HTTP_METHOD_PUT, method))
391 direction = _RECEIVE;
392 if (0 == strcmp (MHD_HTTP_METHOD_GET, method))
395 if (check == GNUNET_NO)
398 plugin->cur_connections++;
401 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
402 "Server: New inbound connection from %s with tag %u\n", GNUNET_i2s(&target), tag);
404 /* find duplicate session */
410 if ((t->inbound) && (0 == memcmp (&t->target, &target, sizeof (struct GNUNET_PeerIdentity))) &&
411 /* FIXME add source address comparison */
419 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
420 "Server: Duplicate session, dismissing new connection from peer `%s'\n", GNUNET_i2s (&target));
425 /* find semi-session */
426 t = plugin->server_semi_head;
430 /* FIXME add source address comparison */
431 if ((0 == memcmp (&t->target, &target, sizeof (struct GNUNET_PeerIdentity))) &&
443 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
444 "Server: Found existing semi-session for `%s'\n", GNUNET_i2s (&target));
447 if ((direction == _SEND) && (t->server_send != NULL))
450 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
451 "Server: Duplicate GET session, dismissing new connection from peer `%s'\n", GNUNET_i2s (&target));
458 GNUNET_CONTAINER_DLL_remove(plugin->server_semi_head, plugin->server_semi_tail, s);
459 GNUNET_CONTAINER_DLL_insert(plugin->head, plugin->tail, s);
461 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
462 "Server: Found matching semi-session, merging session for peer `%s'\n", GNUNET_i2s (&target));
467 if ((direction == _RECEIVE) && (t->server_recv != NULL))
470 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
471 "Server: Duplicate PUT session, dismissing new connection from peer `%s'\n", GNUNET_i2s (&target));
478 GNUNET_CONTAINER_DLL_remove(plugin->server_semi_head, 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", GNUNET_i2s (&target));
488 /* create new session */
490 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
491 "Server: Creating new session for peer `%s' \n", GNUNET_i2s (&target));
494 switch (conn_info->client_addr->sa_family)
497 s4 = ((struct sockaddr_in * ) conn_info->client_addr);
498 a4.u4_port = s4->sin_port;
499 memcpy (&a4.ipv4_addr, &s4->sin_addr,
500 sizeof (struct in_addr));
502 a_len = sizeof (struct IPv4HttpAddress);
505 s6 = ((struct sockaddr_in6 * ) conn_info->client_addr);
506 a6.u6_port = s6->sin6_port;
507 memcpy (&a6.ipv6_addr, &s6->sin6_addr,
508 sizeof (struct in6_addr));
510 a_len = sizeof (struct IPv6HttpAddress);
516 s = create_session (plugin,
523 s->inbound = GNUNET_YES;
524 s->next_receive = GNUNET_TIME_absolute_get_zero();
526 if (0 == strcmp (MHD_HTTP_METHOD_PUT, method))
528 if (0 == strcmp (MHD_HTTP_METHOD_GET, method))
530 GNUNET_CONTAINER_DLL_insert (plugin->server_semi_head, plugin->server_semi_tail, s);
535 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
536 "Server: Invalid connection request\n");
541 sc = GNUNET_malloc (sizeof (struct ServerConnection));
542 sc->mhd_conn = mhd_connection;
543 sc->direction = direction;
545 if (direction == _SEND)
547 if (direction == _RECEIVE)
550 #if MHD_VERSION >= 0x00090E00
551 int to = (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT.rel_value / 1000);
553 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
554 "Server: Setting timeout for %X to %u sec.\n", sc, to);
556 MHD_set_connection_option (mhd_connection, MHD_CONNECTION_OPTION_TIMEOUT, to);
557 server_reschedule (plugin, GNUNET_NO);
563 * Process GET or PUT request received via MHD. For
564 * GET, queue response that will send back our pending
565 * messages. For PUT, process incoming data and send
566 * to GNUnet core. In either case, check if a session
567 * already exists and create a new one if not.
570 server_access_cb (void *cls, struct MHD_Connection *mhd_connection,
571 const char *url, const char *method, const char *version,
572 const char *upload_data, size_t * upload_data_size,
573 void **httpSessionCache)
576 struct Plugin *plugin = cls;
577 struct ServerConnection *sc = *httpSessionCache;
578 struct Session *s = NULL;
581 struct MHD_Response *response;
583 GNUNET_assert (cls != NULL);
587 sc = server_lookup_session(plugin, mhd_connection, url, method);
589 (*httpSessionCache) = sc;
592 response = MHD_create_response_from_data (strlen (HTTP_ERROR_RESPONSE),HTTP_ERROR_RESPONSE, MHD_NO, MHD_NO);
593 res = MHD_queue_response (mhd_connection, MHD_HTTP_NOT_FOUND, response);
594 MHD_destroy_response (response);
599 /* existing connection */
600 sc = (*httpSessionCache);
603 /* connection is to be disconnected*/
604 if (sc->disconnect == GNUNET_YES)
606 response = MHD_create_response_from_data (strlen ("Thank you!"), "Thank you!", MHD_NO, MHD_NO);
607 res = MHD_queue_response (mhd_connection, MHD_HTTP_OK, response);
609 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
610 "Sent HTTP/1.1: 200 OK as PUT Response\n");
612 MHD_destroy_response (response);
616 GNUNET_assert (s != NULL);
617 if (sc->direction == _SEND)
619 response = MHD_create_response_from_callback (-1, 32 * 1024, &server_send_callback,
621 res = MHD_queue_response (mhd_connection, MHD_HTTP_OK, response);
622 MHD_destroy_response (response);
625 if (sc->direction == _RECEIVE)
627 if (*upload_data_size == 0)
630 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
631 "Server: Peer `%s' PUT on address `%s' connected\n",
632 GNUNET_i2s (&s->target), GNUNET_a2s (s->addr, s->addrlen));
638 if ((*upload_data_size > 0))
641 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
642 "Server: peer `%s' PUT on address `%s' received %Zu bytes\n",
643 GNUNET_i2s (&s->target), GNUNET_a2s (s->addr, s->addrlen), *upload_data_size);
645 struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get();
646 if (( s->next_receive.abs_value <= now.abs_value))
649 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
650 "Server: %X: PUT with %u bytes forwarded to MST\n", s,
653 if (s->msg_tk == NULL)
655 s->msg_tk = GNUNET_SERVER_mst_create (&server_receive_mst_cb, s);
657 res = GNUNET_SERVER_mst_receive (s->msg_tk, s, upload_data, *upload_data_size, GNUNET_NO, GNUNET_NO);
659 #if MHD_VERSION >= 0x00090E00
660 int to = (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT.rel_value / 1000);
661 struct ServerConnection *t = NULL;
664 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
665 "Server: Received %Zu bytes\n", *upload_data_size);
668 /* Setting timeouts for other connections */
669 if (s->server_recv != NULL)
673 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
674 "Server: Setting timeout for %X to %u sec.\n", t, to);
676 MHD_set_connection_option (t->mhd_conn, MHD_CONNECTION_OPTION_TIMEOUT, to);
678 if (s->server_send != NULL)
682 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
683 "Server: Setting timeout for %X to %u sec.\n", t, to);
685 MHD_set_connection_option (t->mhd_conn, MHD_CONNECTION_OPTION_TIMEOUT, to);
687 server_reschedule (plugin, GNUNET_NO);
689 (*upload_data_size) = 0;
694 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
695 "Server: %X no inbound bandwidth available! Next read was delayed by %llu ms\n", s, now.abs_value - s->next_receive.abs_value);
707 server_disconnect_cb (void *cls, struct MHD_Connection *connection,
708 void **httpSessionCache)
710 struct ServerConnection *sc = *httpSessionCache;
711 struct ServerConnection *tc = *httpSessionCache;
712 struct Session * s = NULL;
713 struct Session * t = NULL;
714 struct Plugin * plugin = NULL;
721 if (sc->direction == _SEND)
724 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
725 "Server: %X peer `%s' GET on address `%s' disconnected\n",
727 GNUNET_i2s (&s->target), GNUNET_a2s (s->addr, s->addrlen));
729 s->server_send = NULL;
731 if (s->server_recv != NULL)
734 tc->disconnect = GNUNET_YES;
735 #if MHD_VERSION >= 0x00090E00
736 MHD_set_connection_option (sc->mhd_conn, MHD_CONNECTION_OPTION_TIMEOUT, 1);
740 if (sc->direction == _RECEIVE)
743 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
744 "Server: %X peer `%s' PUT on address `%s' disconnected\n",
746 GNUNET_i2s (&s->target), GNUNET_a2s (s->addr, s->addrlen));
748 s->server_recv = NULL;
749 if (s->server_send != NULL)
752 tc->disconnect = GNUNET_YES;
753 #if MHD_VERSION >= 0x00090E00
754 MHD_set_connection_option (sc->mhd_conn, MHD_CONNECTION_OPTION_TIMEOUT, 1);
757 if (s->msg_tk != NULL)
759 GNUNET_SERVER_mst_destroy(s->msg_tk);
765 t = plugin->server_semi_head;
770 GNUNET_CONTAINER_DLL_remove(plugin->server_semi_head, plugin->server_semi_tail, s);
771 GNUNET_CONTAINER_DLL_insert(plugin->head, plugin->tail, s);
776 plugin->cur_connections--;
778 server_reschedule (plugin, GNUNET_NO);
780 if ((s->server_send == NULL) && (s->server_recv == NULL))
783 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
784 "Server: peer `%s' on address `%s' disconnected\n",
785 GNUNET_i2s (&s->target), GNUNET_a2s (s->addr, s->addrlen));
787 if (s->msg_tk != NULL)
789 GNUNET_SERVER_mst_destroy(s->msg_tk);
793 notify_session_end(s->plugin, &s->target, s);
798 server_disconnect (struct Session *s)
800 struct Plugin *plugin = s->plugin;
801 struct Session *t = plugin->head;
805 if (t->inbound == GNUNET_YES)
807 if (t->server_send != NULL)
809 ((struct ServerConnection *) t->server_send)->disconnect = GNUNET_YES;
811 if (t->server_send != NULL)
813 ((struct ServerConnection *) t->server_send)->disconnect = GNUNET_YES;
824 server_send (struct Session *s, struct HTTP_Message * msg)
826 GNUNET_CONTAINER_DLL_insert (s->msg_head, s->msg_tail, msg);
827 server_reschedule (s->plugin, GNUNET_YES);
834 * Call MHD IPv4 to process pending requests and then go back
835 * and schedule the next run.
836 * @param cls plugin as closure
837 * @param tc task context
840 server_v4_run (void *cls,
841 const struct GNUNET_SCHEDULER_TaskContext *tc)
843 struct Plugin *plugin = cls;
844 GNUNET_assert (cls != NULL);
846 plugin->server_v4_task = GNUNET_SCHEDULER_NO_TASK;
848 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
851 GNUNET_assert (MHD_YES == MHD_run (plugin->server_v4));
852 if (plugin->server_v4 != NULL)
853 plugin->server_v4_task = server_schedule (plugin, plugin->server_v4, GNUNET_NO);
858 * Call MHD IPv6 to process pending requests and then go back
859 * and schedule the next run.
860 * @param cls plugin as closure
861 * @param tc task context
864 server_v6_run (void *cls,
865 const struct GNUNET_SCHEDULER_TaskContext *tc)
867 struct Plugin *plugin = cls;
868 GNUNET_assert (cls != NULL);
870 plugin->server_v6_task = GNUNET_SCHEDULER_NO_TASK;
872 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
875 GNUNET_assert (MHD_YES == MHD_run (plugin->server_v6));
876 if (plugin->server_v6 != NULL)
877 plugin->server_v6_task = server_schedule (plugin, plugin->server_v6, GNUNET_NO);
881 * Function that queries MHD's select sets and
882 * starts the task waiting for them.
883 * @param plugin plugin
884 * @param daemon_handle the MHD daemon handle
885 * @return gnunet task identifier
887 static GNUNET_SCHEDULER_TaskIdentifier
888 server_schedule (struct Plugin *plugin, struct MHD_Daemon *daemon_handle, int now)
890 GNUNET_SCHEDULER_TaskIdentifier ret;
894 struct GNUNET_NETWORK_FDSet *wrs;
895 struct GNUNET_NETWORK_FDSet *wws;
896 struct GNUNET_NETWORK_FDSet *wes;
898 unsigned long long timeout;
899 static unsigned long long last_timeout = 0;
902 struct GNUNET_TIME_Relative tv;
904 ret = GNUNET_SCHEDULER_NO_TASK;
908 wrs = GNUNET_NETWORK_fdset_create ();
909 wes = GNUNET_NETWORK_fdset_create ();
910 wws = GNUNET_NETWORK_fdset_create ();
912 GNUNET_assert (MHD_YES == MHD_get_fdset (daemon_handle, &rs, &ws, &es, &max));
913 haveto = MHD_get_timeout (daemon_handle, &timeout);
914 if (haveto == MHD_YES)
916 if (timeout != last_timeout)
919 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
920 "SELECT Timeout changed from %llu to %llu\n",
921 last_timeout, timeout);
923 last_timeout = timeout;
925 tv.rel_value = (uint64_t) timeout;
928 tv = GNUNET_TIME_UNIT_SECONDS;
929 /* Force immediate run, since we have outbound data to send */
930 if (now == GNUNET_YES)
931 tv = GNUNET_TIME_UNIT_MILLISECONDS;
932 GNUNET_NETWORK_fdset_copy_native (wrs, &rs, max + 1);
933 GNUNET_NETWORK_fdset_copy_native (wws, &ws, max + 1);
934 GNUNET_NETWORK_fdset_copy_native (wes, &es, max + 1);
935 if (daemon_handle == plugin->server_v4)
937 if (plugin->server_v4_task != GNUNET_SCHEDULER_NO_TASK)
939 GNUNET_SCHEDULER_cancel (plugin->server_v4_task);
940 plugin->server_v4_task = GNUNET_SCHEDULER_NO_TASK;
944 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
945 GNUNET_SCHEDULER_NO_TASK, tv, wrs, wws,
946 &server_v4_run, plugin);
948 if (daemon_handle == plugin->server_v6)
950 if (plugin->server_v6_task != GNUNET_SCHEDULER_NO_TASK)
952 GNUNET_SCHEDULER_cancel (plugin->server_v6_task);
953 plugin->server_v6_task = GNUNET_SCHEDULER_NO_TASK;
957 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
958 GNUNET_SCHEDULER_NO_TASK, tv, wrs, wws,
959 &server_v6_run, plugin);
961 GNUNET_NETWORK_fdset_destroy (wrs);
962 GNUNET_NETWORK_fdset_destroy (wws);
963 GNUNET_NETWORK_fdset_destroy (wes);
968 server_start (struct Plugin *plugin)
971 unsigned int timeout;
974 res = server_load_certificate (plugin);
975 if (res == GNUNET_SYSERR)
977 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
978 "Could not load or create server certificate! Loading plugin failed!\n");
984 #if MHD_VERSION >= 0x00090E00
985 timeout = GNUNET_CONSTANTS_DISCONNECT_SESSION_TIMEOUT.rel_value / 1000;
986 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
987 "MHD can set timeout per connection! Default time out %u sec.\n", timeout);
989 timeout = GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT.rel_value / 1000;
990 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, plugin->name,
991 "MHD cannot set timeout per connection! Default time out %u sec.\n", timeout);
993 plugin->server_v4 = NULL;
994 if (plugin->ipv4 == GNUNET_YES)
996 plugin->server_v4 = MHD_start_daemon (
1003 MHD_NO_FLAG, plugin->port,
1004 &server_accept_cb, plugin,
1005 &server_access_cb, plugin,
1006 MHD_OPTION_SOCK_ADDR,
1007 (struct sockaddr_in *)
1008 plugin->server_addr_v4,
1009 MHD_OPTION_CONNECTION_LIMIT,
1011 plugin->max_connections,
1013 MHD_OPTION_HTTPS_PRIORITIES,
1014 plugin->crypto_init,
1015 MHD_OPTION_HTTPS_MEM_KEY,
1017 MHD_OPTION_HTTPS_MEM_CERT,
1020 MHD_OPTION_CONNECTION_TIMEOUT,
1022 MHD_OPTION_CONNECTION_MEMORY_LIMIT,
1024 GNUNET_SERVER_MAX_MESSAGE_SIZE),
1025 MHD_OPTION_NOTIFY_COMPLETED,
1026 &server_disconnect_cb, plugin,
1027 MHD_OPTION_EXTERNAL_LOGGER,
1028 server_log, NULL, MHD_OPTION_END);
1030 plugin->server_v6 = NULL;
1031 if (plugin->ipv6 == GNUNET_YES)
1033 plugin->server_v6 = MHD_start_daemon (
1040 MHD_USE_IPv6, plugin->port,
1041 &server_accept_cb, plugin,
1042 &server_access_cb, plugin,
1043 MHD_OPTION_SOCK_ADDR,
1044 (struct sockaddr_in6 *)
1045 plugin->server_addr_v6,
1046 MHD_OPTION_CONNECTION_LIMIT,
1048 plugin->max_connections,
1050 MHD_OPTION_HTTPS_PRIORITIES,
1051 plugin->crypto_init,
1052 MHD_OPTION_HTTPS_MEM_KEY,
1054 MHD_OPTION_HTTPS_MEM_CERT,
1057 MHD_OPTION_CONNECTION_TIMEOUT,
1059 MHD_OPTION_CONNECTION_MEMORY_LIMIT,
1061 GNUNET_SERVER_MAX_MESSAGE_SIZE),
1062 MHD_OPTION_NOTIFY_COMPLETED,
1063 &server_disconnect_cb, plugin,
1064 MHD_OPTION_EXTERNAL_LOGGER,
1065 server_log, NULL, MHD_OPTION_END);
1069 if ((plugin->ipv4 == GNUNET_YES) && (plugin->server_v4 == NULL))
1071 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
1072 "Failed to start %s IPv4 server component on port %u\n", plugin->name,
1074 return GNUNET_SYSERR;
1076 if ((plugin->ipv6 == GNUNET_YES) && (plugin->server_v6 == NULL))
1078 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
1079 "Failed to start %s IPv6 server component on port %u\n", plugin->name,
1081 return GNUNET_SYSERR;
1084 server_reschedule (plugin, GNUNET_NO);
1087 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1088 "%s server component started on port %u\n", plugin->name,
1095 server_stop (struct Plugin *plugin)
1097 struct Session *s = NULL;
1098 struct Session *t = NULL;
1100 struct MHD_Daemon *server_v4_tmp = plugin->server_v4;
1101 plugin->server_v4 = NULL;
1102 struct MHD_Daemon *server_v6_tmp = plugin->server_v6;
1103 plugin->server_v6 = NULL;
1105 if (plugin->server_v4_task != GNUNET_SCHEDULER_NO_TASK)
1107 GNUNET_SCHEDULER_cancel (plugin->server_v4_task);
1108 plugin->server_v4_task = GNUNET_SCHEDULER_NO_TASK;
1111 if (plugin->server_v6_task != GNUNET_SCHEDULER_NO_TASK)
1113 GNUNET_SCHEDULER_cancel (plugin->server_v6_task);
1114 plugin->server_v6_task = GNUNET_SCHEDULER_NO_TASK;
1117 if (server_v6_tmp != NULL)
1119 MHD_stop_daemon (server_v4_tmp);
1121 if (server_v6_tmp != NULL)
1123 MHD_stop_daemon (server_v6_tmp);
1126 /* cleaning up semi-sessions never propagated */
1127 s = plugin->server_semi_head;
1136 GNUNET_free_non_null (plugin->crypto_init);
1137 GNUNET_free_non_null (plugin->cert);
1138 GNUNET_free_non_null (plugin->key);
1142 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1143 "%s server component stopped\n", plugin->name);
1149 /* end of plugin_transport_http.c */