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), GNUNET_a2s (s->addr, s->addrlen),
303 * Callback called by MHD when it needs data to send
304 * @param cls current session
305 * @param pos position in buffer
306 * @param buf the buffer to write data to
307 * @param max max number of bytes available in buffer
308 * @return bytes written to buffer
311 server_send_callback (void *cls, uint64_t pos, char *buf, size_t max)
313 struct Session *s = cls;
315 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, s->plugin->name,
316 "Server: %X can sent maximum %u \n", s, max);
318 struct HTTP_Message *msg;
326 if ((msg->size - msg->pos) <= max)
328 memcpy (buf, &msg->buf[msg->pos], (msg->size - msg->pos));
329 bytes_read = msg->size - msg->pos;
330 msg->pos += (msg->size - msg->pos);
334 memcpy (buf, &msg->buf[msg->pos], max);
339 /* removing message */
340 if (msg->pos == msg->size)
342 if (NULL != msg->transmit_cont)
343 msg->transmit_cont (msg->transmit_cont_cls, &s->target, GNUNET_OK);
344 GNUNET_CONTAINER_DLL_remove (s->msg_head, s->msg_tail, msg);
350 struct Plugin *plugin = s->plugin;
351 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
352 "Server: %X: sent %u bytes\n", s, bytes_read);
357 static struct ServerConnection *
358 server_lookup_session (struct Plugin *plugin,
359 struct MHD_Connection *mhd_connection, const char *url,
362 struct Session *s = NULL;
364 struct ServerConnection *sc = NULL;
365 const union MHD_ConnectionInfo *conn_info;
367 struct IPv4HttpAddress a4;
368 struct IPv6HttpAddress a6;
369 struct sockaddr_in *s4;
370 struct sockaddr_in6 *s6;
373 struct GNUNET_PeerIdentity target;
374 int check = GNUNET_NO;
379 MHD_get_connection_info (mhd_connection,
380 MHD_CONNECTION_INFO_CLIENT_ADDRESS);
381 if ((conn_info->client_addr->sa_family != AF_INET) &&
382 (conn_info->client_addr->sa_family != AF_INET6))
385 if ((strlen (&url[1]) >= 105) && (url[104] == ';'))
388 char *tagc = (char *) &url[105];
390 memcpy (&hash, &url[1], 103);
393 GNUNET_CRYPTO_hash_from_string ((const char *) &hash,
394 &(target.hashPubKey)))
396 tag = strtoul (tagc, NULL, 10);
402 if (0 == strcmp (MHD_HTTP_METHOD_PUT, method))
403 direction = _RECEIVE;
404 if (0 == strcmp (MHD_HTTP_METHOD_GET, method))
407 if (check == GNUNET_NO)
410 plugin->cur_connections++;
413 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
414 "Server: New inbound connection from %s with tag %u\n",
415 GNUNET_i2s (&target), tag);
417 /* find duplicate session */
424 (0 == memcmp (&t->target, &target, sizeof (struct GNUNET_PeerIdentity)))
426 /* FIXME add source address comparison */
434 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
435 "Server: Duplicate session, dismissing new connection from peer `%s'\n",
436 GNUNET_i2s (&target));
441 /* find semi-session */
442 t = plugin->server_semi_head;
446 /* FIXME add source address comparison */
447 if ((0 == memcmp (&t->target, &target, sizeof (struct GNUNET_PeerIdentity)))
459 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
460 "Server: Found existing semi-session for `%s'\n",
461 GNUNET_i2s (&target));
464 if ((direction == _SEND) && (t->server_send != NULL))
467 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
468 "Server: Duplicate GET session, dismissing new connection from peer `%s'\n",
469 GNUNET_i2s (&target));
476 GNUNET_CONTAINER_DLL_remove (plugin->server_semi_head,
477 plugin->server_semi_tail, s);
478 GNUNET_CONTAINER_DLL_insert (plugin->head, plugin->tail, s);
480 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
481 "Server: Found matching semi-session, merging session for peer `%s'\n",
482 GNUNET_i2s (&target));
487 if ((direction == _RECEIVE) && (t->server_recv != NULL))
490 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
491 "Server: Duplicate PUT session, dismissing new connection from peer `%s'\n",
492 GNUNET_i2s (&target));
499 GNUNET_CONTAINER_DLL_remove (plugin->server_semi_head,
500 plugin->server_semi_tail, s);
501 GNUNET_CONTAINER_DLL_insert (plugin->head, plugin->tail, s);
503 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
504 "Server: Found matching semi-session, merging session for peer `%s'\n",
505 GNUNET_i2s (&target));
511 /* create new session */
513 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
514 "Server: Creating new session for peer `%s' \n",
515 GNUNET_i2s (&target));
518 switch (conn_info->client_addr->sa_family)
521 s4 = ((struct sockaddr_in *) conn_info->client_addr);
522 a4.u4_port = s4->sin_port;
523 memcpy (&a4.ipv4_addr, &s4->sin_addr, sizeof (struct in_addr));
525 a_len = sizeof (struct IPv4HttpAddress);
528 s6 = ((struct sockaddr_in6 *) conn_info->client_addr);
529 a6.u6_port = s6->sin6_port;
530 memcpy (&a6.ipv6_addr, &s6->sin6_addr, sizeof (struct in6_addr));
532 a_len = sizeof (struct IPv6HttpAddress);
539 s = create_session (plugin, &target, a, a_len, NULL, NULL);
541 s->inbound = GNUNET_YES;
542 s->next_receive = GNUNET_TIME_absolute_get_zero ();
544 if (0 == strcmp (MHD_HTTP_METHOD_PUT, method))
546 if (0 == strcmp (MHD_HTTP_METHOD_GET, method))
548 GNUNET_CONTAINER_DLL_insert (plugin->server_semi_head,
549 plugin->server_semi_tail, s);
554 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
555 "Server: Invalid connection request\n");
560 sc = GNUNET_malloc (sizeof (struct ServerConnection));
561 sc->mhd_conn = mhd_connection;
562 sc->direction = direction;
564 if (direction == _SEND)
566 if (direction == _RECEIVE)
569 #if MHD_VERSION >= 0x00090E00
570 int to = (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT.rel_value / 1000);
573 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
574 "Server: Setting timeout for %X to %u sec.\n", sc, to);
576 MHD_set_connection_option (mhd_connection, MHD_CONNECTION_OPTION_TIMEOUT, to);
577 server_reschedule (plugin, GNUNET_NO);
583 * Process GET or PUT request received via MHD. For
584 * GET, queue response that will send back our pending
585 * messages. For PUT, process incoming data and send
586 * to GNUnet core. In either case, check if a session
587 * already exists and create a new one if not.
590 server_access_cb (void *cls, struct MHD_Connection *mhd_connection,
591 const char *url, const char *method, const char *version,
592 const char *upload_data, size_t * upload_data_size,
593 void **httpSessionCache)
596 struct Plugin *plugin = cls;
597 struct ServerConnection *sc = *httpSessionCache;
598 struct Session *s = NULL;
601 struct MHD_Response *response;
603 GNUNET_assert (cls != NULL);
607 sc = server_lookup_session (plugin, mhd_connection, url, method);
609 (*httpSessionCache) = sc;
613 MHD_create_response_from_data (strlen (HTTP_ERROR_RESPONSE),
614 HTTP_ERROR_RESPONSE, MHD_NO, MHD_NO);
615 res = MHD_queue_response (mhd_connection, MHD_HTTP_NOT_FOUND, response);
616 MHD_destroy_response (response);
621 /* existing connection */
622 sc = (*httpSessionCache);
625 /* connection is to be disconnected */
626 if (sc->disconnect == GNUNET_YES)
629 MHD_create_response_from_data (strlen ("Thank you!"), "Thank you!",
631 res = MHD_queue_response (mhd_connection, MHD_HTTP_OK, response);
633 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
634 "Sent HTTP/1.1: 200 OK as PUT Response\n");
636 MHD_destroy_response (response);
640 GNUNET_assert (s != NULL);
641 if (sc->direction == _SEND)
644 MHD_create_response_from_callback (-1, 32 * 1024, &server_send_callback,
646 res = MHD_queue_response (mhd_connection, MHD_HTTP_OK, response);
647 MHD_destroy_response (response);
650 if (sc->direction == _RECEIVE)
652 if (*upload_data_size == 0)
655 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
656 "Server: Peer `%s' PUT on address `%s' connected\n",
657 GNUNET_i2s (&s->target), GNUNET_a2s (s->addr,
664 if ((*upload_data_size > 0))
667 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
668 "Server: peer `%s' PUT on address `%s' received %Zu bytes\n",
669 GNUNET_i2s (&s->target), GNUNET_a2s (s->addr,
673 struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get ();
675 if ((s->next_receive.abs_value <= now.abs_value))
678 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
679 "Server: %X: PUT with %u bytes forwarded to MST\n", s,
682 if (s->msg_tk == NULL)
684 s->msg_tk = GNUNET_SERVER_mst_create (&server_receive_mst_cb, s);
687 GNUNET_SERVER_mst_receive (s->msg_tk, s, upload_data,
688 *upload_data_size, GNUNET_NO, GNUNET_NO);
690 #if MHD_VERSION >= 0x00090E00
691 int to = (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT.rel_value / 1000);
692 struct ServerConnection *t = NULL;
695 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
696 "Server: Received %Zu bytes\n", *upload_data_size);
699 /* Setting timeouts for other connections */
700 if (s->server_recv != NULL)
704 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
705 "Server: Setting timeout for %X to %u sec.\n", t,
708 MHD_set_connection_option (t->mhd_conn, MHD_CONNECTION_OPTION_TIMEOUT,
711 if (s->server_send != NULL)
715 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
716 "Server: Setting timeout for %X to %u sec.\n", t,
719 MHD_set_connection_option (t->mhd_conn, MHD_CONNECTION_OPTION_TIMEOUT,
722 server_reschedule (plugin, GNUNET_NO);
724 (*upload_data_size) = 0;
729 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
730 "Server: %X no inbound bandwidth available! Next read was delayed by %llu ms\n",
731 s, now.abs_value - s->next_receive.abs_value);
743 server_disconnect_cb (void *cls, struct MHD_Connection *connection,
744 void **httpSessionCache)
746 struct ServerConnection *sc = *httpSessionCache;
747 struct ServerConnection *tc = *httpSessionCache;
748 struct Session *s = NULL;
749 struct Session *t = NULL;
750 struct Plugin *plugin = NULL;
757 if (sc->direction == _SEND)
760 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
761 "Server: %X peer `%s' GET on address `%s' disconnected\n",
762 s->server_send, GNUNET_i2s (&s->target),
763 GNUNET_a2s (s->addr, s->addrlen));
765 s->server_send = NULL;
767 if (s->server_recv != NULL)
770 tc->disconnect = GNUNET_YES;
771 #if MHD_VERSION >= 0x00090E00
772 MHD_set_connection_option (sc->mhd_conn, MHD_CONNECTION_OPTION_TIMEOUT,
777 if (sc->direction == _RECEIVE)
780 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
781 "Server: %X peer `%s' PUT on address `%s' disconnected\n",
782 s->server_recv, GNUNET_i2s (&s->target),
783 GNUNET_a2s (s->addr, s->addrlen));
785 s->server_recv = NULL;
786 if (s->server_send != NULL)
789 tc->disconnect = GNUNET_YES;
790 #if MHD_VERSION >= 0x00090E00
791 MHD_set_connection_option (sc->mhd_conn, MHD_CONNECTION_OPTION_TIMEOUT,
795 if (s->msg_tk != NULL)
797 GNUNET_SERVER_mst_destroy (s->msg_tk);
803 t = plugin->server_semi_head;
808 GNUNET_CONTAINER_DLL_remove (plugin->server_semi_head,
809 plugin->server_semi_tail, s);
810 GNUNET_CONTAINER_DLL_insert (plugin->head, plugin->tail, s);
815 plugin->cur_connections--;
817 server_reschedule (plugin, GNUNET_NO);
819 if ((s->server_send == NULL) && (s->server_recv == NULL))
822 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
823 "Server: peer `%s' on address `%s' disconnected\n",
824 GNUNET_i2s (&s->target), GNUNET_a2s (s->addr, s->addrlen));
826 if (s->msg_tk != NULL)
828 GNUNET_SERVER_mst_destroy (s->msg_tk);
832 notify_session_end (s->plugin, &s->target, s);
837 server_disconnect (struct Session *s)
839 struct Plugin *plugin = s->plugin;
840 struct Session *t = plugin->head;
844 if (t->inbound == GNUNET_YES)
846 if (t->server_send != NULL)
848 ((struct ServerConnection *) t->server_send)->disconnect = GNUNET_YES;
850 if (t->server_send != NULL)
852 ((struct ServerConnection *) t->server_send)->disconnect = GNUNET_YES;
861 server_send (struct Session *s, struct HTTP_Message *msg)
863 GNUNET_CONTAINER_DLL_insert (s->msg_head, s->msg_tail, msg);
864 server_reschedule (s->plugin, GNUNET_YES);
871 * Call MHD IPv4 to process pending requests and then go back
872 * and schedule the next run.
873 * @param cls plugin as closure
874 * @param tc task context
877 server_v4_run (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
879 struct Plugin *plugin = cls;
881 GNUNET_assert (cls != NULL);
883 plugin->server_v4_task = GNUNET_SCHEDULER_NO_TASK;
885 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
888 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
889 "Running IPv6 server\n");
891 GNUNET_assert (MHD_YES == MHD_run (plugin->server_v4));
892 if (plugin->server_v4 != NULL)
893 plugin->server_v4_task =
894 server_schedule (plugin, plugin->server_v4, GNUNET_NO);
899 * Call MHD IPv6 to process pending requests and then go back
900 * and schedule the next run.
901 * @param cls plugin as closure
902 * @param tc task context
905 server_v6_run (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
907 struct Plugin *plugin = cls;
909 GNUNET_assert (cls != NULL);
911 plugin->server_v6_task = GNUNET_SCHEDULER_NO_TASK;
913 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
916 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
917 "Running IPv6 server\n");
919 GNUNET_assert (MHD_YES == MHD_run (plugin->server_v6));
920 if (plugin->server_v6 != NULL)
921 plugin->server_v6_task =
922 server_schedule (plugin, plugin->server_v6, GNUNET_NO);
926 * Function that queries MHD's select sets and
927 * starts the task waiting for them.
928 * @param plugin plugin
929 * @param daemon_handle the MHD daemon handle
930 * @return gnunet task identifier
932 static GNUNET_SCHEDULER_TaskIdentifier
933 server_schedule (struct Plugin *plugin, struct MHD_Daemon *daemon_handle,
936 GNUNET_SCHEDULER_TaskIdentifier ret;
940 struct GNUNET_NETWORK_FDSet *wrs;
941 struct GNUNET_NETWORK_FDSet *wws;
942 struct GNUNET_NETWORK_FDSet *wes;
944 unsigned long long timeout;
945 static unsigned long long last_timeout = 0;
948 struct GNUNET_TIME_Relative tv;
950 ret = GNUNET_SCHEDULER_NO_TASK;
954 wrs = GNUNET_NETWORK_fdset_create ();
955 wes = GNUNET_NETWORK_fdset_create ();
956 wws = GNUNET_NETWORK_fdset_create ();
958 GNUNET_assert (MHD_YES == MHD_get_fdset (daemon_handle, &rs, &ws, &es, &max));
959 haveto = MHD_get_timeout (daemon_handle, &timeout);
960 if (haveto == MHD_YES)
962 if (timeout != last_timeout)
965 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
966 "SELECT Timeout changed from %llu to %llu\n",
967 last_timeout, timeout);
969 last_timeout = timeout;
971 tv.rel_value = (uint64_t) timeout;
974 tv = GNUNET_TIME_UNIT_SECONDS;
975 /* Force immediate run, since we have outbound data to send */
976 if (now == GNUNET_YES)
977 tv = GNUNET_TIME_UNIT_MILLISECONDS;
978 GNUNET_NETWORK_fdset_copy_native (wrs, &rs, max + 1);
979 GNUNET_NETWORK_fdset_copy_native (wws, &ws, max + 1);
980 GNUNET_NETWORK_fdset_copy_native (wes, &es, max + 1);
982 if (daemon_handle == plugin->server_v4)
984 if (plugin->server_v4_task != GNUNET_SCHEDULER_NO_TASK)
986 GNUNET_SCHEDULER_cancel (plugin->server_v4_task);
987 plugin->server_v4_task = GNUNET_SCHEDULER_NO_TASK;
990 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
991 "Scheduling IPv4 server task in %llu ms\n", tv);
994 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
995 GNUNET_SCHEDULER_NO_TASK, tv, wrs, wws,
996 &server_v4_run, plugin);
998 if (daemon_handle == plugin->server_v6)
1000 if (plugin->server_v6_task != GNUNET_SCHEDULER_NO_TASK)
1002 GNUNET_SCHEDULER_cancel (plugin->server_v6_task);
1003 plugin->server_v6_task = GNUNET_SCHEDULER_NO_TASK;
1006 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
1007 "Scheduling IPv6 server task in %llu ms\n", tv);
1010 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1011 GNUNET_SCHEDULER_NO_TASK, tv, wrs, wws,
1012 &server_v6_run, plugin);
1014 GNUNET_NETWORK_fdset_destroy (wrs);
1015 GNUNET_NETWORK_fdset_destroy (wws);
1016 GNUNET_NETWORK_fdset_destroy (wes);
1021 server_start (struct Plugin *plugin)
1023 int res = GNUNET_OK;
1024 unsigned int timeout;
1027 res = server_load_certificate (plugin);
1028 if (res == GNUNET_SYSERR)
1030 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
1031 "Could not load or create server certificate! Loading plugin failed!\n");
1037 #if MHD_VERSION >= 0x00090E00
1038 timeout = GNUNET_CONSTANTS_DISCONNECT_SESSION_TIMEOUT.rel_value / 1000;
1039 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1040 "MHD can set timeout per connection! Default time out %u sec.\n",
1043 timeout = GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT.rel_value / 1000;
1044 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, plugin->name,
1045 "MHD cannot set timeout per connection! Default time out %u sec.\n",
1048 plugin->server_v4 = NULL;
1049 if (plugin->ipv4 == GNUNET_YES)
1051 plugin->server_v4 = MHD_start_daemon (
1058 MHD_NO_FLAG, plugin->port,
1059 &server_accept_cb, plugin,
1060 &server_access_cb, plugin,
1061 MHD_OPTION_SOCK_ADDR,
1062 (struct sockaddr_in *)
1063 plugin->server_addr_v4,
1064 MHD_OPTION_CONNECTION_LIMIT,
1066 plugin->max_connections,
1068 MHD_OPTION_HTTPS_PRIORITIES,
1069 plugin->crypto_init,
1070 MHD_OPTION_HTTPS_MEM_KEY,
1072 MHD_OPTION_HTTPS_MEM_CERT,
1075 MHD_OPTION_CONNECTION_TIMEOUT,
1077 MHD_OPTION_CONNECTION_MEMORY_LIMIT,
1079 GNUNET_SERVER_MAX_MESSAGE_SIZE),
1080 MHD_OPTION_NOTIFY_COMPLETED,
1081 &server_disconnect_cb, plugin,
1082 MHD_OPTION_EXTERNAL_LOGGER,
1083 server_log, NULL, MHD_OPTION_END);
1085 plugin->server_v6 = NULL;
1086 if (plugin->ipv6 == GNUNET_YES)
1088 plugin->server_v6 = MHD_start_daemon (
1095 MHD_USE_IPv6, plugin->port,
1096 &server_accept_cb, plugin,
1097 &server_access_cb, plugin,
1098 MHD_OPTION_SOCK_ADDR,
1099 (struct sockaddr_in6 *)
1100 plugin->server_addr_v6,
1101 MHD_OPTION_CONNECTION_LIMIT,
1103 plugin->max_connections,
1105 MHD_OPTION_HTTPS_PRIORITIES,
1106 plugin->crypto_init,
1107 MHD_OPTION_HTTPS_MEM_KEY,
1109 MHD_OPTION_HTTPS_MEM_CERT,
1112 MHD_OPTION_CONNECTION_TIMEOUT,
1114 MHD_OPTION_CONNECTION_MEMORY_LIMIT,
1116 GNUNET_SERVER_MAX_MESSAGE_SIZE),
1117 MHD_OPTION_NOTIFY_COMPLETED,
1118 &server_disconnect_cb, plugin,
1119 MHD_OPTION_EXTERNAL_LOGGER,
1120 server_log, NULL, MHD_OPTION_END);
1124 if ((plugin->ipv4 == GNUNET_YES) && (plugin->server_v4 == NULL))
1126 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
1127 "Failed to start %s IPv4 server component on port %u\n",
1128 plugin->name, plugin->port);
1129 return GNUNET_SYSERR;
1131 if ((plugin->ipv6 == GNUNET_YES) && (plugin->server_v6 == NULL))
1133 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
1134 "Failed to start %s IPv6 server component on port %u\n",
1135 plugin->name, plugin->port);
1136 return GNUNET_SYSERR;
1139 server_reschedule (plugin, GNUNET_NO);
1142 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1143 "%s server component started on port %u\n", plugin->name,
1150 server_stop (struct Plugin *plugin)
1152 struct Session *s = NULL;
1153 struct Session *t = NULL;
1156 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
1160 struct MHD_Daemon *server_v4_tmp = plugin->server_v4;
1162 plugin->server_v4 = NULL;
1163 struct MHD_Daemon *server_v6_tmp = plugin->server_v6;
1165 plugin->server_v6 = NULL;
1167 if (plugin->server_v4_task != GNUNET_SCHEDULER_NO_TASK)
1169 GNUNET_SCHEDULER_cancel (plugin->server_v4_task);
1170 plugin->server_v4_task = GNUNET_SCHEDULER_NO_TASK;
1173 if (plugin->server_v6_task != GNUNET_SCHEDULER_NO_TASK)
1175 GNUNET_SCHEDULER_cancel (plugin->server_v6_task);
1176 plugin->server_v6_task = GNUNET_SCHEDULER_NO_TASK;
1179 if (server_v6_tmp != NULL)
1181 MHD_stop_daemon (server_v4_tmp);
1183 if (server_v6_tmp != NULL)
1185 MHD_stop_daemon (server_v6_tmp);
1188 /* cleaning up semi-sessions never propagated */
1189 s = plugin->server_semi_head;
1193 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
1194 "Deleting semi-sessions %p\n", s);
1197 struct HTTP_Message * msg = s->msg_head;
1198 struct HTTP_Message * tmp = s->msg_head;
1203 GNUNET_CONTAINER_DLL_remove(s->msg_head,s->msg_tail, msg);
1204 if (msg->transmit_cont != NULL)
1206 msg->transmit_cont(msg->transmit_cont_cls, &s->target, GNUNET_SYSERR);
1217 GNUNET_free_non_null (plugin->crypto_init);
1218 GNUNET_free_non_null (plugin->cert);
1219 GNUNET_free_non_null (plugin->key);
1223 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1224 "%s server component stopped\n", plugin->name);
1230 /* end of plugin_transport_http.c */