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_DEBUG, "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 */
159 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
160 "Loading TLS certificate from key-file `%s' cert-file`%s'\n",
161 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 (GNUNET_NO, 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 server which server to schedule v4 or v6?
242 * @param now GNUNET_YES to schedule execution immediately, GNUNET_NO to wait
247 server_reschedule (struct Plugin *plugin, struct MHD_Daemon *server, int now)
249 if ((server == plugin->server_v4) && (plugin->server_v4 != NULL))
251 if (plugin->server_v4_task != GNUNET_SCHEDULER_NO_TASK)
253 GNUNET_SCHEDULER_cancel (plugin->server_v4_task);
254 plugin->server_v4_task = GNUNET_SCHEDULER_NO_TASK;
256 plugin->server_v4_task = server_schedule (plugin, plugin->server_v4, now);
259 if ((server == plugin->server_v6) && (plugin->server_v6 != NULL))
261 if (plugin->server_v6_task != GNUNET_SCHEDULER_NO_TASK)
263 GNUNET_SCHEDULER_cancel (plugin->server_v6_task);
264 plugin->server_v6_task = GNUNET_SCHEDULER_NO_TASK;
266 plugin->server_v6_task = server_schedule (plugin, plugin->server_v6, now);
271 * Callback called by MessageStreamTokenizer when a message has arrived
272 * @param cls current session as closure
273 * @param client clien
274 * @param message the message to be forwarded to transport service
277 server_receive_mst_cb (void *cls, void *client,
278 const struct GNUNET_MessageHeader *message)
280 struct Session *s = cls;
281 struct Plugin *plugin = s->plugin;
282 struct GNUNET_TIME_Relative delay;
284 delay = http_plugin_receive (s, &s->target, message, s, s->addr, s->addrlen);
287 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (), delay);
289 if (delay.rel_value > 0)
291 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
292 "Server: peer `%s' address `%s' next read delayed for %llu ms\n",
293 GNUNET_i2s (&s->target),
294 http_plugin_address_to_string (NULL, s->addr, s->addrlen),
300 * Callback called by MHD when it needs data to send
301 * @param cls current session
302 * @param pos position in buffer
303 * @param buf the buffer to write data to
304 * @param max max number of bytes available in buffer
305 * @return bytes written to buffer
308 server_send_callback (void *cls, uint64_t pos, char *buf, size_t max)
310 struct Session *s = cls;
312 struct HTTP_Message *msg;
320 if ((msg->size - msg->pos) <= max)
322 memcpy (buf, &msg->buf[msg->pos], (msg->size - msg->pos));
323 bytes_read = msg->size - msg->pos;
324 msg->pos += (msg->size - msg->pos);
328 memcpy (buf, &msg->buf[msg->pos], max);
333 /* removing message */
334 if (msg->pos == msg->size)
336 if (NULL != msg->transmit_cont)
337 msg->transmit_cont (msg->transmit_cont_cls, &s->target, GNUNET_OK);
338 GNUNET_CONTAINER_DLL_remove (s->msg_head, s->msg_tail, msg);
343 struct Plugin *plugin = s->plugin;
344 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
345 "Server: %X: sent %u bytes\n", s, bytes_read);
350 static struct ServerConnection *
351 server_lookup_session (struct Plugin *plugin,
352 struct MHD_Connection *mhd_connection, const char *url,
355 struct Session *s = NULL;
357 struct ServerConnection *sc = NULL;
358 const union MHD_ConnectionInfo *conn_info;
359 struct GNUNET_ATS_Information ats;
360 struct IPv4HttpAddress a4;
361 struct IPv6HttpAddress a6;
362 struct sockaddr_in *s4;
363 struct sockaddr_in6 *s6;
366 struct GNUNET_PeerIdentity target;
367 int check = GNUNET_NO;
369 int direction = GNUNET_SYSERR;
372 MHD_get_connection_info (mhd_connection,
373 MHD_CONNECTION_INFO_CLIENT_ADDRESS);
374 if ((conn_info->client_addr->sa_family != AF_INET) &&
375 (conn_info->client_addr->sa_family != AF_INET6))
378 if ((strlen (&url[1]) >= 105) && (url[104] == ';'))
381 char *tagc = (char *) &url[105];
383 memcpy (&hash, &url[1], 103);
386 GNUNET_CRYPTO_hash_from_string ((const char *) &hash,
387 &(target.hashPubKey)))
389 tag = strtoul (tagc, NULL, 10);
395 if (0 == strcmp (MHD_HTTP_METHOD_PUT, method))
396 direction = _RECEIVE;
397 else if (0 == strcmp (MHD_HTTP_METHOD_GET, method))
406 if (check == GNUNET_NO)
409 plugin->cur_connections++;
410 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
411 "Server: New inbound connection from %s with tag %u\n",
412 GNUNET_i2s (&target), tag);
413 /* find duplicate session */
420 (0 == memcmp (&t->target, &target, sizeof (struct GNUNET_PeerIdentity)))
422 /* FIXME add source address comparison */
429 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
430 "Server: Duplicate session, dismissing new connection from peer `%s'\n",
431 GNUNET_i2s (&target));
435 /* find semi-session */
436 t = plugin->server_semi_head;
440 /* FIXME add source address comparison */
441 if ((0 == memcmp (&t->target, &target, sizeof (struct GNUNET_PeerIdentity)))
451 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
452 "Server: Found existing semi-session for `%s'\n",
453 GNUNET_i2s (&target));
455 if ((direction == _SEND) && (t->server_send != NULL))
457 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
458 "Server: Duplicate GET session, dismissing new connection from peer `%s'\n",
459 GNUNET_i2s (&target));
465 GNUNET_CONTAINER_DLL_remove (plugin->server_semi_head,
466 plugin->server_semi_tail, s);
467 GNUNET_CONTAINER_DLL_insert (plugin->head, plugin->tail, s);
468 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
469 "Server: Found matching semi-session, merging session for peer `%s'\n",
470 GNUNET_i2s (&target));
474 if ((direction == _RECEIVE) && (t->server_recv != NULL))
476 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
477 "Server: Duplicate PUT session, dismissing new connection from peer `%s'\n",
478 GNUNET_i2s (&target));
484 GNUNET_CONTAINER_DLL_remove (plugin->server_semi_head,
485 plugin->server_semi_tail, s);
486 GNUNET_CONTAINER_DLL_insert (plugin->head, plugin->tail, s);
487 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
488 "Server: Found matching semi-session, merging session for peer `%s'\n",
489 GNUNET_i2s (&target));
494 /* create new session */
495 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
496 "Server: Creating new session for peer `%s' \n",
497 GNUNET_i2s (&target));
498 switch (conn_info->client_addr->sa_family)
501 s4 = ((struct sockaddr_in *) conn_info->client_addr);
502 a4.u4_port = s4->sin_port;
503 memcpy (&a4.ipv4_addr, &s4->sin_addr, sizeof (struct in_addr));
505 a_len = sizeof (struct IPv4HttpAddress);
506 ats = plugin->env->get_address_type (plugin->env->cls, (const struct sockaddr *) s4, sizeof (struct sockaddr_in));
509 s6 = ((struct sockaddr_in6 *) conn_info->client_addr);
510 a6.u6_port = s6->sin6_port;
511 memcpy (&a6.ipv6_addr, &s6->sin6_addr, sizeof (struct in6_addr));
513 a_len = sizeof (struct IPv6HttpAddress);
514 ats = plugin->env->get_address_type (plugin->env->cls, (const struct sockaddr *) s6, sizeof (struct sockaddr_in6));
520 s = create_session (plugin, &target, a, a_len, NULL, NULL);
521 s->ats_address_network_type = ats.value;
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,
531 plugin->server_semi_tail, s);
535 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
536 "Server: Invalid connection request\n");
540 sc = GNUNET_malloc (sizeof (struct ServerConnection));
541 sc->mhd_conn = mhd_connection;
542 sc->direction = direction;
544 if (direction == _SEND)
546 if (direction == _RECEIVE)
549 #if MHD_VERSION >= 0x00090E00
550 int to = (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT.rel_value / 1000);
552 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
553 "Server: Setting timeout for %X to %u sec.\n", sc, to);
554 MHD_set_connection_option (mhd_connection, MHD_CONNECTION_OPTION_TIMEOUT, to);
556 struct MHD_Daemon *d = NULL;
558 if (s->addrlen == sizeof (struct IPv6HttpAddress))
559 d = plugin->server_v6;
560 if (s->addrlen == sizeof (struct IPv4HttpAddress))
561 d = plugin->server_v4;
563 server_reschedule (plugin, d, GNUNET_NO);
569 * Process GET or PUT request received via MHD. For
570 * GET, queue response that will send back our pending
571 * messages. For PUT, process incoming data and send
572 * to GNUnet core. In either case, check if a session
573 * already exists and create a new one if not.
576 server_access_cb (void *cls, struct MHD_Connection *mhd_connection,
577 const char *url, const char *method, const char *version,
578 const char *upload_data, size_t * upload_data_size,
579 void **httpSessionCache)
582 struct Plugin *plugin = cls;
583 struct ServerConnection *sc = *httpSessionCache;
584 struct Session *s = NULL;
587 struct MHD_Response *response;
589 GNUNET_assert (cls != NULL);
593 sc = server_lookup_session (plugin, mhd_connection, url, method);
595 (*httpSessionCache) = sc;
599 MHD_create_response_from_data (strlen (HTTP_ERROR_RESPONSE),
600 HTTP_ERROR_RESPONSE, MHD_NO, MHD_NO);
601 res = MHD_queue_response (mhd_connection, MHD_HTTP_NOT_FOUND, response);
602 MHD_destroy_response (response);
607 /* existing connection */
608 sc = (*httpSessionCache);
611 /* connection is to be disconnected */
612 if (sc->disconnect == GNUNET_YES)
614 /* Sent HTTP/1.1: 200 OK as PUT Response\ */
616 MHD_create_response_from_data (strlen ("Thank you!"), "Thank you!",
618 res = MHD_queue_response (mhd_connection, MHD_HTTP_OK, response);
619 MHD_destroy_response (response);
623 GNUNET_assert (s != NULL);
624 /* Check if both directions are connected */
625 if ((sc->session->server_recv == NULL) || (sc->session->server_send == NULL))
627 /* Delayed read from since not both semi-connections are connected */
631 if (sc->direction == _SEND)
634 MHD_create_response_from_callback (-1, 32 * 1024, &server_send_callback,
636 MHD_queue_response (mhd_connection, MHD_HTTP_OK, response);
637 MHD_destroy_response (response);
640 if (sc->direction == _RECEIVE)
642 if (*upload_data_size == 0)
644 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
645 "Server: Peer `%s' PUT on address `%s' connected\n",
646 GNUNET_i2s (&s->target),
647 http_plugin_address_to_string (NULL, s->addr,
653 if ((*upload_data_size > 0))
655 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
656 "Server: peer `%s' PUT on address `%s' received %u bytes\n",
657 GNUNET_i2s (&s->target),
658 http_plugin_address_to_string (NULL, s->addr,
661 struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get ();
663 if ((s->next_receive.abs_value <= now.abs_value))
665 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
666 "Server: %X: PUT with %u bytes forwarded to MST\n", s,
668 if (s->msg_tk == NULL)
670 s->msg_tk = GNUNET_SERVER_mst_create (&server_receive_mst_cb, s);
672 GNUNET_SERVER_mst_receive (s->msg_tk, s, upload_data,
673 *upload_data_size, GNUNET_NO, GNUNET_NO);
675 #if MHD_VERSION >= 0x00090E00
676 int to = (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT.rel_value / 1000);
677 struct ServerConnection *t = NULL;
679 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
680 "Server: Received %u bytes\n", *upload_data_size);
681 /* Setting timeouts for other connections */
682 if (s->server_recv != NULL)
685 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
686 "Server: Setting timeout for %X to %u sec.\n", t,
688 MHD_set_connection_option (t->mhd_conn, MHD_CONNECTION_OPTION_TIMEOUT,
691 if (s->server_send != NULL)
694 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
695 "Server: Setting timeout for %X to %u sec.\n", t,
697 MHD_set_connection_option (t->mhd_conn, MHD_CONNECTION_OPTION_TIMEOUT,
700 struct MHD_Daemon *d = NULL;
702 if (s->addrlen == sizeof (struct IPv6HttpAddress))
703 d = plugin->server_v6;
704 if (s->addrlen == sizeof (struct IPv4HttpAddress))
705 d = plugin->server_v4;
706 server_reschedule (plugin, d, GNUNET_NO);
708 (*upload_data_size) = 0;
712 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
713 "Server: %X no inbound bandwidth available! Next read was delayed by %llu ms\n",
714 s, now.abs_value - s->next_receive.abs_value);
725 server_disconnect_cb (void *cls, struct MHD_Connection *connection,
726 void **httpSessionCache)
728 struct ServerConnection *sc = *httpSessionCache;
729 struct ServerConnection *tc = NULL;
730 struct Session *s = NULL;
731 struct Session *t = NULL;
732 struct Plugin *plugin = NULL;
739 if (sc->direction == _SEND)
742 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
743 "Server: %X peer `%s' GET on address `%s' disconnected\n",
744 s->server_send, GNUNET_i2s (&s->target),
745 http_plugin_address_to_string (NULL, s->addr, s->addrlen));
747 s->server_send = NULL;
749 if (s->server_recv != NULL)
752 tc->disconnect = GNUNET_YES;
753 #if MHD_VERSION >= 0x00090E00
754 MHD_set_connection_option (sc->mhd_conn, MHD_CONNECTION_OPTION_TIMEOUT,
759 if (sc->direction == _RECEIVE)
761 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
762 "Server: %X peer `%s' PUT on address `%s' disconnected\n",
763 s->server_recv, GNUNET_i2s (&s->target),
764 http_plugin_address_to_string (NULL, s->addr, s->addrlen));
765 s->server_recv = NULL;
766 if (s->server_send != NULL)
769 tc->disconnect = GNUNET_YES;
770 #if MHD_VERSION >= 0x00090E00
771 MHD_set_connection_option (sc->mhd_conn, MHD_CONNECTION_OPTION_TIMEOUT,
775 if (s->msg_tk != NULL)
777 GNUNET_SERVER_mst_destroy (s->msg_tk);
783 t = plugin->server_semi_head;
788 GNUNET_CONTAINER_DLL_remove (plugin->server_semi_head,
789 plugin->server_semi_tail, s);
790 GNUNET_CONTAINER_DLL_insert (plugin->head, plugin->tail, s);
795 plugin->cur_connections--;
797 struct MHD_Daemon *d = NULL;
799 if (s->addrlen == sizeof (struct IPv6HttpAddress))
800 d = plugin->server_v6;
801 if (s->addrlen == sizeof (struct IPv4HttpAddress))
802 d = plugin->server_v4;
803 server_reschedule (plugin, d, GNUNET_NO);
805 if ((s->server_send == NULL) && (s->server_recv == NULL))
807 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
808 "Server: peer `%s' on address `%s' disconnected\n",
809 GNUNET_i2s (&s->target),
810 http_plugin_address_to_string (NULL, s->addr, s->addrlen));
811 if (s->msg_tk != NULL)
813 GNUNET_SERVER_mst_destroy (s->msg_tk);
817 notify_session_end (s->plugin, &s->target, s);
822 server_disconnect (struct Session *s)
824 struct Plugin *plugin = s->plugin;
825 struct Session *t = plugin->head;
829 if (t->inbound == GNUNET_YES)
831 if (t->server_send != NULL)
833 ((struct ServerConnection *) t->server_send)->disconnect = GNUNET_YES;
835 if (t->server_send != NULL)
837 ((struct ServerConnection *) t->server_send)->disconnect = GNUNET_YES;
846 server_send (struct Session *s, struct HTTP_Message *msg)
848 GNUNET_CONTAINER_DLL_insert (s->msg_head, s->msg_tail, msg);
850 if (s->addrlen == sizeof (struct IPv4HttpAddress))
852 server_reschedule (s->plugin, s->plugin->server_v4, GNUNET_YES);
854 else if (s->addrlen == sizeof (struct IPv6HttpAddress))
856 server_reschedule (s->plugin, s->plugin->server_v6, GNUNET_YES);
859 return GNUNET_SYSERR;
866 * Call MHD IPv4 to process pending requests and then go back
867 * and schedule the next run.
868 * @param cls plugin as closure
869 * @param tc task context
872 server_v4_run (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
874 struct Plugin *plugin = cls;
876 GNUNET_assert (cls != NULL);
878 plugin->server_v4_task = GNUNET_SCHEDULER_NO_TASK;
880 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
883 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
884 "Running IPv4 server\n");
886 GNUNET_assert (MHD_YES == MHD_run (plugin->server_v4));
887 if (plugin->server_v4 != NULL)
888 plugin->server_v4_task =
889 server_schedule (plugin, plugin->server_v4, GNUNET_NO);
894 * Call MHD IPv6 to process pending requests and then go back
895 * and schedule the next run.
896 * @param cls plugin as closure
897 * @param tc task context
900 server_v6_run (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
902 struct Plugin *plugin = cls;
904 GNUNET_assert (cls != NULL);
906 plugin->server_v6_task = GNUNET_SCHEDULER_NO_TASK;
908 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
911 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
912 "Running IPv6 server\n");
914 GNUNET_assert (MHD_YES == MHD_run (plugin->server_v6));
915 if (plugin->server_v6 != NULL)
916 plugin->server_v6_task =
917 server_schedule (plugin, plugin->server_v6, GNUNET_NO);
921 * Function that queries MHD's select sets and
922 * starts the task waiting for them.
923 * @param plugin plugin
924 * @param daemon_handle the MHD daemon handle
925 * @return gnunet task identifier
927 static GNUNET_SCHEDULER_TaskIdentifier
928 server_schedule (struct Plugin *plugin, struct MHD_Daemon *daemon_handle,
931 GNUNET_SCHEDULER_TaskIdentifier ret;
935 struct GNUNET_NETWORK_FDSet *wrs;
936 struct GNUNET_NETWORK_FDSet *wws;
937 struct GNUNET_NETWORK_FDSet *wes;
939 unsigned long long timeout;
940 static unsigned long long last_timeout = 0;
943 struct GNUNET_TIME_Relative tv;
945 ret = GNUNET_SCHEDULER_NO_TASK;
949 wrs = GNUNET_NETWORK_fdset_create ();
950 wes = GNUNET_NETWORK_fdset_create ();
951 wws = GNUNET_NETWORK_fdset_create ();
953 GNUNET_assert (MHD_YES == MHD_get_fdset (daemon_handle, &rs, &ws, &es, &max));
954 haveto = MHD_get_timeout (daemon_handle, &timeout);
955 if (haveto == MHD_YES)
957 if (timeout != last_timeout)
960 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
961 "SELECT Timeout changed from %llu to %llu\n",
962 last_timeout, timeout);
964 last_timeout = timeout;
966 tv.rel_value = (uint64_t) timeout;
969 tv = GNUNET_TIME_UNIT_SECONDS;
970 /* Force immediate run, since we have outbound data to send */
971 if (now == GNUNET_YES)
972 tv = GNUNET_TIME_UNIT_MILLISECONDS;
973 GNUNET_NETWORK_fdset_copy_native (wrs, &rs, max + 1);
974 GNUNET_NETWORK_fdset_copy_native (wws, &ws, max + 1);
975 GNUNET_NETWORK_fdset_copy_native (wes, &es, max + 1);
977 if (daemon_handle == plugin->server_v4)
979 if (plugin->server_v4_task != GNUNET_SCHEDULER_NO_TASK)
981 GNUNET_SCHEDULER_cancel (plugin->server_v4_task);
982 plugin->server_v4_task = GNUNET_SCHEDULER_NO_TASK;
985 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
986 "Scheduling IPv4 server task in %llu ms\n", tv);
989 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
990 GNUNET_SCHEDULER_NO_TASK, tv, wrs, wws,
991 &server_v4_run, plugin);
993 if (daemon_handle == plugin->server_v6)
995 if (plugin->server_v6_task != GNUNET_SCHEDULER_NO_TASK)
997 GNUNET_SCHEDULER_cancel (plugin->server_v6_task);
998 plugin->server_v6_task = GNUNET_SCHEDULER_NO_TASK;
1001 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1002 "Scheduling IPv6 server task in %llu ms\n", tv);
1005 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1006 GNUNET_SCHEDULER_NO_TASK, tv, wrs, wws,
1007 &server_v6_run, plugin);
1009 GNUNET_NETWORK_fdset_destroy (wrs);
1010 GNUNET_NETWORK_fdset_destroy (wws);
1011 GNUNET_NETWORK_fdset_destroy (wes);
1016 server_start (struct Plugin *plugin)
1018 int res = GNUNET_OK;
1019 unsigned int timeout;
1022 res = server_load_certificate (plugin);
1023 if (res == GNUNET_SYSERR)
1025 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
1026 "Could not load or create server certificate! Loading plugin failed!\n");
1032 #if MHD_VERSION >= 0x00090E00
1033 timeout = HTTP_NOT_VALIDATED_TIMEOUT.rel_value / 1000;
1034 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1035 "MHD can set timeout per connection! Default time out %u sec.\n",
1038 timeout = GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT.rel_value / 1000;
1039 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, plugin->name,
1040 "MHD cannot set timeout per connection! Default time out %u sec.\n",
1043 plugin->server_v4 = NULL;
1044 if (plugin->ipv4 == GNUNET_YES)
1046 plugin->server_v4 = MHD_start_daemon (
1053 MHD_NO_FLAG, plugin->port,
1054 &server_accept_cb, plugin,
1055 &server_access_cb, plugin,
1056 MHD_OPTION_SOCK_ADDR,
1057 (struct sockaddr_in *)
1058 plugin->server_addr_v4,
1059 MHD_OPTION_CONNECTION_LIMIT,
1061 plugin->max_connections,
1063 MHD_OPTION_HTTPS_PRIORITIES,
1064 plugin->crypto_init,
1065 MHD_OPTION_HTTPS_MEM_KEY,
1067 MHD_OPTION_HTTPS_MEM_CERT,
1070 MHD_OPTION_CONNECTION_TIMEOUT,
1072 MHD_OPTION_CONNECTION_MEMORY_LIMIT,
1074 GNUNET_SERVER_MAX_MESSAGE_SIZE),
1075 MHD_OPTION_NOTIFY_COMPLETED,
1076 &server_disconnect_cb, plugin,
1077 MHD_OPTION_EXTERNAL_LOGGER,
1078 server_log, NULL, MHD_OPTION_END);
1080 plugin->server_v6 = NULL;
1081 if (plugin->ipv6 == GNUNET_YES)
1083 plugin->server_v6 = MHD_start_daemon (
1090 MHD_USE_IPv6, plugin->port,
1091 &server_accept_cb, plugin,
1092 &server_access_cb, plugin,
1093 MHD_OPTION_SOCK_ADDR,
1094 (struct sockaddr_in6 *)
1095 plugin->server_addr_v6,
1096 MHD_OPTION_CONNECTION_LIMIT,
1098 plugin->max_connections,
1100 MHD_OPTION_HTTPS_PRIORITIES,
1101 plugin->crypto_init,
1102 MHD_OPTION_HTTPS_MEM_KEY,
1104 MHD_OPTION_HTTPS_MEM_CERT,
1107 MHD_OPTION_CONNECTION_TIMEOUT,
1109 MHD_OPTION_CONNECTION_MEMORY_LIMIT,
1111 GNUNET_SERVER_MAX_MESSAGE_SIZE),
1112 MHD_OPTION_NOTIFY_COMPLETED,
1113 &server_disconnect_cb, plugin,
1114 MHD_OPTION_EXTERNAL_LOGGER,
1115 server_log, NULL, MHD_OPTION_END);
1119 if ((plugin->ipv4 == GNUNET_YES) && (plugin->server_v4 == NULL))
1121 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
1122 "Failed to start %s IPv4 server component on port %u\n",
1123 plugin->name, plugin->port);
1124 return GNUNET_SYSERR;
1126 server_reschedule (plugin, plugin->server_v4, GNUNET_NO);
1128 if ((plugin->ipv6 == GNUNET_YES) && (plugin->server_v6 == NULL))
1130 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
1131 "Failed to start %s IPv6 server component on port %u\n",
1132 plugin->name, plugin->port);
1133 return GNUNET_SYSERR;
1135 server_reschedule (plugin, plugin->server_v6, GNUNET_NO);
1139 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1140 "%s server component started on port %u\n", plugin->name,
1147 server_stop (struct Plugin *plugin)
1149 struct Session *s = NULL;
1150 struct Session *t = NULL;
1152 struct MHD_Daemon *server_v4_tmp = plugin->server_v4;
1154 plugin->server_v4 = NULL;
1155 struct MHD_Daemon *server_v6_tmp = plugin->server_v6;
1157 plugin->server_v6 = NULL;
1159 if (plugin->server_v4_task != GNUNET_SCHEDULER_NO_TASK)
1161 GNUNET_SCHEDULER_cancel (plugin->server_v4_task);
1162 plugin->server_v4_task = GNUNET_SCHEDULER_NO_TASK;
1165 if (plugin->server_v6_task != GNUNET_SCHEDULER_NO_TASK)
1167 GNUNET_SCHEDULER_cancel (plugin->server_v6_task);
1168 plugin->server_v6_task = GNUNET_SCHEDULER_NO_TASK;
1171 if (server_v6_tmp != NULL)
1173 MHD_stop_daemon (server_v4_tmp);
1175 if (server_v6_tmp != NULL)
1177 MHD_stop_daemon (server_v6_tmp);
1180 /* cleaning up semi-sessions never propagated */
1181 s = plugin->server_semi_head;
1185 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1186 "Deleting semi-sessions %p\n", s);
1189 struct HTTP_Message *msg = s->msg_head;
1190 struct HTTP_Message *tmp = NULL;
1196 GNUNET_CONTAINER_DLL_remove (s->msg_head, s->msg_tail, msg);
1197 if (msg->transmit_cont != NULL)
1199 msg->transmit_cont (msg->transmit_cont_cls, &s->target, GNUNET_SYSERR);
1210 GNUNET_free_non_null (plugin->crypto_init);
1211 GNUNET_free_non_null (plugin->cert);
1212 GNUNET_free_non_null (plugin->key);
1216 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1217 "%s server component stopped\n", plugin->name);
1223 /* end of plugin_transport_http.c */