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 (GNUNET_OK != GNUNET_DISK_file_size (file,
102 &fsize, GNUNET_NO, GNUNET_YES))
104 text = GNUNET_malloc (fsize + 1);
106 GNUNET_DISK_file_open (file, GNUNET_DISK_OPEN_READ,
107 GNUNET_DISK_PERM_USER_READ);
113 if (GNUNET_SYSERR == GNUNET_DISK_file_read (gn_file, text, fsize))
116 GNUNET_DISK_file_close (gn_file);
120 GNUNET_DISK_file_close (gn_file);
129 server_load_certificate (struct Plugin *plugin)
136 /* Get crypto init string from config
137 * If not present just use default values */
139 GNUNET_assert (GNUNET_OK ==
140 GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg,
143 &plugin->crypto_init));
146 GNUNET_CONFIGURATION_get_value_filename (plugin->env->cfg, plugin->name,
147 "KEY_FILE", &key_file))
149 key_file = GNUNET_strdup ("https_key.key");
153 GNUNET_CONFIGURATION_get_value_filename (plugin->env->cfg, plugin->name,
154 "CERT_FILE", &cert_file))
156 GNUNET_asprintf (&cert_file, "%s", "https_cert.crt");
159 /* read key & certificates from file */
160 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
161 "Loading TLS certificate from key-file `%s' cert-file`%s'\n",
162 key_file, cert_file);
164 plugin->key = server_load_file (key_file);
165 plugin->cert = server_load_file (cert_file);
167 if ((plugin->key == NULL) || (plugin->cert == NULL))
169 struct GNUNET_OS_Process *cert_creation;
171 GNUNET_free_non_null (plugin->key);
173 GNUNET_free_non_null (plugin->cert);
177 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
178 "No usable TLS certificate found, creating certificate\n");
182 GNUNET_OS_start_process (GNUNET_NO, NULL, NULL,
183 "gnunet-transport-certificate-creation",
184 "gnunet-transport-certificate-creation",
185 key_file, cert_file, NULL);
186 if (cert_creation == NULL)
188 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
190 ("Could not create a new TLS certificate, program `gnunet-transport-certificate-creation' could not be started!\n"));
191 GNUNET_free (key_file);
192 GNUNET_free (cert_file);
194 GNUNET_free_non_null (plugin->key);
196 GNUNET_free_non_null (plugin->cert);
198 GNUNET_free_non_null (plugin->crypto_init);
199 plugin->crypto_init = NULL;
201 return GNUNET_SYSERR;
203 GNUNET_assert (GNUNET_OK == GNUNET_OS_process_wait (cert_creation));
204 GNUNET_OS_process_close (cert_creation);
206 plugin->key = server_load_file (key_file);
207 plugin->cert = server_load_file (cert_file);
210 if ((plugin->key == NULL) || (plugin->cert == NULL))
212 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
214 ("No usable TLS certificate found and creating one failed!\n"),
216 GNUNET_free (key_file);
217 GNUNET_free (cert_file);
219 GNUNET_free_non_null (plugin->key);
221 GNUNET_free_non_null (plugin->cert);
223 GNUNET_free_non_null (plugin->crypto_init);
224 plugin->crypto_init = NULL;
226 return GNUNET_SYSERR;
228 GNUNET_free (key_file);
229 GNUNET_free (cert_file);
231 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "TLS certificate loaded\n");
240 * Reschedule the execution of both IPv4 and IPv6 server
241 * @param plugin the plugin
242 * @param server which server to schedule v4 or v6?
243 * @param now GNUNET_YES to schedule execution immediately, GNUNET_NO to wait
248 server_reschedule (struct Plugin *plugin, struct MHD_Daemon *server, int now)
250 if ((server == plugin->server_v4) && (plugin->server_v4 != NULL))
252 if (plugin->server_v4_task != GNUNET_SCHEDULER_NO_TASK)
254 GNUNET_SCHEDULER_cancel (plugin->server_v4_task);
255 plugin->server_v4_task = GNUNET_SCHEDULER_NO_TASK;
257 plugin->server_v4_task = server_schedule (plugin, plugin->server_v4, now);
260 if ((server == plugin->server_v6) && (plugin->server_v6 != NULL))
262 if (plugin->server_v6_task != GNUNET_SCHEDULER_NO_TASK)
264 GNUNET_SCHEDULER_cancel (plugin->server_v6_task);
265 plugin->server_v6_task = GNUNET_SCHEDULER_NO_TASK;
267 plugin->server_v6_task = server_schedule (plugin, plugin->server_v6, now);
272 * Callback called by MessageStreamTokenizer when a message has arrived
273 * @param cls current session as closure
274 * @param client clien
275 * @param message the message to be forwarded to transport service
278 server_receive_mst_cb (void *cls, void *client,
279 const struct GNUNET_MessageHeader *message)
281 struct Session *s = cls;
282 struct Plugin *plugin = s->plugin;
283 struct GNUNET_TIME_Relative delay;
285 delay = http_plugin_receive (s, &s->target, message, s, s->addr, s->addrlen);
288 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (), delay);
290 if (delay.rel_value > 0)
292 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
293 "Server: peer `%s' address `%s' next read delayed for %llu ms\n",
294 GNUNET_i2s (&s->target),
295 http_plugin_address_to_string (NULL, s->addr, s->addrlen),
301 * Callback called by MHD when it needs data to send
302 * @param cls current session
303 * @param pos position in buffer
304 * @param buf the buffer to write data to
305 * @param max max number of bytes available in buffer
306 * @return bytes written to buffer
309 server_send_callback (void *cls, uint64_t pos, char *buf, size_t max)
311 struct Session *s = cls;
313 struct HTTP_Message *msg;
321 if ((msg->size - msg->pos) <= max)
323 memcpy (buf, &msg->buf[msg->pos], (msg->size - msg->pos));
324 bytes_read = msg->size - msg->pos;
325 msg->pos += (msg->size - msg->pos);
329 memcpy (buf, &msg->buf[msg->pos], max);
334 /* removing message */
335 if (msg->pos == msg->size)
337 if (NULL != msg->transmit_cont)
338 msg->transmit_cont (msg->transmit_cont_cls, &s->target, GNUNET_OK);
339 GNUNET_CONTAINER_DLL_remove (s->msg_head, s->msg_tail, msg);
344 struct Plugin *plugin = s->plugin;
345 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
346 "Server: %X: sent %u bytes\n", s, bytes_read);
351 static struct ServerConnection *
352 server_lookup_session (struct Plugin *plugin,
353 struct MHD_Connection *mhd_connection, const char *url,
356 struct Session *s = NULL;
358 struct ServerConnection *sc = NULL;
359 const union MHD_ConnectionInfo *conn_info;
360 struct GNUNET_ATS_Information ats;
361 struct IPv4HttpAddress a4;
362 struct IPv6HttpAddress a6;
363 struct sockaddr_in *s4;
364 struct sockaddr_in6 *s6;
367 struct GNUNET_PeerIdentity target;
368 int check = GNUNET_NO;
370 int direction = GNUNET_SYSERR;
373 MHD_get_connection_info (mhd_connection,
374 MHD_CONNECTION_INFO_CLIENT_ADDRESS);
375 if ((conn_info->client_addr->sa_family != AF_INET) &&
376 (conn_info->client_addr->sa_family != AF_INET6))
379 if ((strlen (&url[1]) >= 105) && (url[104] == ';'))
382 char *tagc = (char *) &url[105];
384 memcpy (&hash, &url[1], 103);
387 GNUNET_CRYPTO_hash_from_string ((const char *) &hash,
388 &(target.hashPubKey)))
390 tag = strtoul (tagc, NULL, 10);
396 if (0 == strcmp (MHD_HTTP_METHOD_PUT, method))
397 direction = _RECEIVE;
398 else if (0 == strcmp (MHD_HTTP_METHOD_GET, method))
407 if (check == GNUNET_NO)
410 plugin->cur_connections++;
411 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
412 "Server: New inbound connection from %s with tag %u\n",
413 GNUNET_i2s (&target), tag);
414 /* find duplicate session */
421 (0 == memcmp (&t->target, &target, sizeof (struct GNUNET_PeerIdentity)))
423 /* FIXME add source address comparison */
430 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
431 "Server: Duplicate session, dismissing new connection from peer `%s'\n",
432 GNUNET_i2s (&target));
436 /* find semi-session */
437 t = plugin->server_semi_head;
441 /* FIXME add source address comparison */
442 if ((0 == memcmp (&t->target, &target, sizeof (struct GNUNET_PeerIdentity)))
452 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
453 "Server: Found existing semi-session for `%s'\n",
454 GNUNET_i2s (&target));
456 if ((direction == _SEND) && (t->server_send != NULL))
458 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
459 "Server: Duplicate GET session, dismissing new connection from peer `%s'\n",
460 GNUNET_i2s (&target));
466 GNUNET_CONTAINER_DLL_remove (plugin->server_semi_head,
467 plugin->server_semi_tail, s);
468 GNUNET_CONTAINER_DLL_insert (plugin->head, plugin->tail, s);
469 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
470 "Server: Found matching semi-session, merging session for peer `%s'\n",
471 GNUNET_i2s (&target));
475 if ((direction == _RECEIVE) && (t->server_recv != NULL))
477 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
478 "Server: Duplicate PUT session, dismissing new connection from peer `%s'\n",
479 GNUNET_i2s (&target));
485 GNUNET_CONTAINER_DLL_remove (plugin->server_semi_head,
486 plugin->server_semi_tail, s);
487 GNUNET_CONTAINER_DLL_insert (plugin->head, plugin->tail, s);
488 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
489 "Server: Found matching semi-session, merging session for peer `%s'\n",
490 GNUNET_i2s (&target));
495 /* create new session */
496 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
497 "Server: Creating new session for peer `%s' \n",
498 GNUNET_i2s (&target));
499 switch (conn_info->client_addr->sa_family)
502 s4 = ((struct sockaddr_in *) conn_info->client_addr);
503 a4.u4_port = s4->sin_port;
504 memcpy (&a4.ipv4_addr, &s4->sin_addr, sizeof (struct in_addr));
506 a_len = sizeof (struct IPv4HttpAddress);
507 ats = plugin->env->get_address_type (plugin->env->cls, (const struct sockaddr *) s4, sizeof (struct sockaddr_in));
510 s6 = ((struct sockaddr_in6 *) conn_info->client_addr);
511 a6.u6_port = s6->sin6_port;
512 memcpy (&a6.ipv6_addr, &s6->sin6_addr, sizeof (struct in6_addr));
514 a_len = sizeof (struct IPv6HttpAddress);
515 ats = plugin->env->get_address_type (plugin->env->cls, (const struct sockaddr *) s6, sizeof (struct sockaddr_in6));
521 s = create_session (plugin, &target, a, a_len, NULL, NULL);
522 s->ats_address_network_type = ats.value;
524 s->inbound = GNUNET_YES;
525 s->next_receive = GNUNET_TIME_absolute_get_zero ();
527 if (0 == strcmp (MHD_HTTP_METHOD_PUT, method))
529 if (0 == strcmp (MHD_HTTP_METHOD_GET, method))
531 GNUNET_CONTAINER_DLL_insert (plugin->server_semi_head,
532 plugin->server_semi_tail, s);
536 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
537 "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);
555 MHD_set_connection_option (mhd_connection, MHD_CONNECTION_OPTION_TIMEOUT, to);
557 struct MHD_Daemon *d = NULL;
559 if (s->addrlen == sizeof (struct IPv6HttpAddress))
560 d = plugin->server_v6;
561 if (s->addrlen == sizeof (struct IPv4HttpAddress))
562 d = plugin->server_v4;
564 server_reschedule (plugin, d, GNUNET_NO);
570 * Process GET or PUT request received via MHD. For
571 * GET, queue response that will send back our pending
572 * messages. For PUT, process incoming data and send
573 * to GNUnet core. In either case, check if a session
574 * already exists and create a new one if not.
577 server_access_cb (void *cls, struct MHD_Connection *mhd_connection,
578 const char *url, const char *method, const char *version,
579 const char *upload_data, size_t * upload_data_size,
580 void **httpSessionCache)
583 struct Plugin *plugin = cls;
584 struct ServerConnection *sc = *httpSessionCache;
585 struct Session *s = NULL;
588 struct MHD_Response *response;
590 GNUNET_assert (cls != NULL);
594 sc = server_lookup_session (plugin, mhd_connection, url, method);
596 (*httpSessionCache) = sc;
600 MHD_create_response_from_data (strlen (HTTP_ERROR_RESPONSE),
601 HTTP_ERROR_RESPONSE, MHD_NO, MHD_NO);
602 res = MHD_queue_response (mhd_connection, MHD_HTTP_NOT_FOUND, response);
603 MHD_destroy_response (response);
608 /* existing connection */
609 sc = (*httpSessionCache);
612 /* connection is to be disconnected */
613 if (sc->disconnect == GNUNET_YES)
615 /* Sent HTTP/1.1: 200 OK as PUT Response\ */
617 MHD_create_response_from_data (strlen ("Thank you!"), "Thank you!",
619 res = MHD_queue_response (mhd_connection, MHD_HTTP_OK, response);
620 MHD_destroy_response (response);
624 GNUNET_assert (s != NULL);
625 /* Check if both directions are connected */
626 if ((sc->session->server_recv == NULL) || (sc->session->server_send == NULL))
628 /* Delayed read from since not both semi-connections are connected */
632 if (sc->direction == _SEND)
635 MHD_create_response_from_callback (-1, 32 * 1024, &server_send_callback,
637 MHD_queue_response (mhd_connection, MHD_HTTP_OK, response);
638 MHD_destroy_response (response);
641 if (sc->direction == _RECEIVE)
643 if (*upload_data_size == 0)
645 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
646 "Server: Peer `%s' PUT on address `%s' connected\n",
647 GNUNET_i2s (&s->target),
648 http_plugin_address_to_string (NULL, s->addr,
654 if ((*upload_data_size > 0))
656 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
657 "Server: peer `%s' PUT on address `%s' received %u bytes\n",
658 GNUNET_i2s (&s->target),
659 http_plugin_address_to_string (NULL, s->addr,
662 struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get ();
664 if ((s->next_receive.abs_value <= now.abs_value))
666 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
667 "Server: %X: PUT with %u bytes forwarded to MST\n", s,
669 if (s->msg_tk == NULL)
671 s->msg_tk = GNUNET_SERVER_mst_create (&server_receive_mst_cb, s);
673 GNUNET_SERVER_mst_receive (s->msg_tk, s, upload_data,
674 *upload_data_size, GNUNET_NO, GNUNET_NO);
676 #if MHD_VERSION >= 0x00090E00
677 int to = (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT.rel_value / 1000);
678 struct ServerConnection *t = NULL;
680 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
681 "Server: Received %u bytes\n", *upload_data_size);
682 /* Setting timeouts for other connections */
683 if (s->server_recv != NULL)
686 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
687 "Server: Setting timeout for %X to %u sec.\n", t,
689 MHD_set_connection_option (t->mhd_conn, MHD_CONNECTION_OPTION_TIMEOUT,
692 if (s->server_send != NULL)
695 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
696 "Server: Setting timeout for %X to %u sec.\n", t,
698 MHD_set_connection_option (t->mhd_conn, MHD_CONNECTION_OPTION_TIMEOUT,
701 struct MHD_Daemon *d = NULL;
703 if (s->addrlen == sizeof (struct IPv6HttpAddress))
704 d = plugin->server_v6;
705 if (s->addrlen == sizeof (struct IPv4HttpAddress))
706 d = plugin->server_v4;
707 server_reschedule (plugin, d, GNUNET_NO);
709 (*upload_data_size) = 0;
713 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
714 "Server: %X no inbound bandwidth available! Next read was delayed by %llu ms\n",
715 s, now.abs_value - s->next_receive.abs_value);
726 server_disconnect_cb (void *cls, struct MHD_Connection *connection,
727 void **httpSessionCache)
729 struct ServerConnection *sc = *httpSessionCache;
730 struct ServerConnection *tc = NULL;
731 struct Session *s = NULL;
732 struct Session *t = NULL;
733 struct Plugin *plugin = NULL;
740 if (sc->direction == _SEND)
743 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
744 "Server: %X peer `%s' GET on address `%s' disconnected\n",
745 s->server_send, GNUNET_i2s (&s->target),
746 http_plugin_address_to_string (NULL, s->addr, s->addrlen));
748 s->server_send = NULL;
750 if (s->server_recv != NULL)
753 tc->disconnect = GNUNET_YES;
754 #if MHD_VERSION >= 0x00090E00
755 MHD_set_connection_option (sc->mhd_conn, MHD_CONNECTION_OPTION_TIMEOUT,
760 if (sc->direction == _RECEIVE)
762 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
763 "Server: %X peer `%s' PUT on address `%s' disconnected\n",
764 s->server_recv, GNUNET_i2s (&s->target),
765 http_plugin_address_to_string (NULL, s->addr, s->addrlen));
766 s->server_recv = NULL;
767 if (s->server_send != NULL)
770 tc->disconnect = GNUNET_YES;
771 #if MHD_VERSION >= 0x00090E00
772 MHD_set_connection_option (sc->mhd_conn, MHD_CONNECTION_OPTION_TIMEOUT,
776 if (s->msg_tk != NULL)
778 GNUNET_SERVER_mst_destroy (s->msg_tk);
784 t = plugin->server_semi_head;
789 GNUNET_CONTAINER_DLL_remove (plugin->server_semi_head,
790 plugin->server_semi_tail, s);
791 GNUNET_CONTAINER_DLL_insert (plugin->head, plugin->tail, s);
796 plugin->cur_connections--;
798 struct MHD_Daemon *d = NULL;
800 if (s->addrlen == sizeof (struct IPv6HttpAddress))
801 d = plugin->server_v6;
802 if (s->addrlen == sizeof (struct IPv4HttpAddress))
803 d = plugin->server_v4;
804 server_reschedule (plugin, d, GNUNET_NO);
806 if ((s->server_send == NULL) && (s->server_recv == NULL))
808 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
809 "Server: peer `%s' on address `%s' disconnected\n",
810 GNUNET_i2s (&s->target),
811 http_plugin_address_to_string (NULL, s->addr, s->addrlen));
812 if (s->msg_tk != NULL)
814 GNUNET_SERVER_mst_destroy (s->msg_tk);
818 notify_session_end (s->plugin, &s->target, s);
823 server_disconnect (struct Session *s)
825 struct Plugin *plugin = s->plugin;
826 struct Session *t = plugin->head;
830 if (t->inbound == GNUNET_YES)
832 if (t->server_send != NULL)
834 ((struct ServerConnection *) t->server_send)->disconnect = GNUNET_YES;
836 if (t->server_send != NULL)
838 ((struct ServerConnection *) t->server_send)->disconnect = GNUNET_YES;
847 server_send (struct Session *s, struct HTTP_Message *msg)
849 GNUNET_CONTAINER_DLL_insert (s->msg_head, s->msg_tail, msg);
851 if (s->addrlen == sizeof (struct IPv4HttpAddress))
853 server_reschedule (s->plugin, s->plugin->server_v4, GNUNET_YES);
855 else if (s->addrlen == sizeof (struct IPv6HttpAddress))
857 server_reschedule (s->plugin, s->plugin->server_v6, GNUNET_YES);
860 return GNUNET_SYSERR;
867 * Call MHD IPv4 to process pending requests and then go back
868 * and schedule the next run.
869 * @param cls plugin as closure
870 * @param tc task context
873 server_v4_run (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
875 struct Plugin *plugin = cls;
877 GNUNET_assert (cls != NULL);
879 plugin->server_v4_task = GNUNET_SCHEDULER_NO_TASK;
881 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
884 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
885 "Running IPv4 server\n");
887 GNUNET_assert (MHD_YES == MHD_run (plugin->server_v4));
888 if (plugin->server_v4 != NULL)
889 plugin->server_v4_task =
890 server_schedule (plugin, plugin->server_v4, GNUNET_NO);
895 * Call MHD IPv6 to process pending requests and then go back
896 * and schedule the next run.
897 * @param cls plugin as closure
898 * @param tc task context
901 server_v6_run (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
903 struct Plugin *plugin = cls;
905 GNUNET_assert (cls != NULL);
907 plugin->server_v6_task = GNUNET_SCHEDULER_NO_TASK;
909 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
912 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
913 "Running IPv6 server\n");
915 GNUNET_assert (MHD_YES == MHD_run (plugin->server_v6));
916 if (plugin->server_v6 != NULL)
917 plugin->server_v6_task =
918 server_schedule (plugin, plugin->server_v6, GNUNET_NO);
922 * Function that queries MHD's select sets and
923 * starts the task waiting for them.
924 * @param plugin plugin
925 * @param daemon_handle the MHD daemon handle
926 * @return gnunet task identifier
928 static GNUNET_SCHEDULER_TaskIdentifier
929 server_schedule (struct Plugin *plugin, struct MHD_Daemon *daemon_handle,
932 GNUNET_SCHEDULER_TaskIdentifier ret;
936 struct GNUNET_NETWORK_FDSet *wrs;
937 struct GNUNET_NETWORK_FDSet *wws;
938 struct GNUNET_NETWORK_FDSet *wes;
940 unsigned long long timeout;
941 static unsigned long long last_timeout = 0;
944 struct GNUNET_TIME_Relative tv;
946 ret = GNUNET_SCHEDULER_NO_TASK;
950 wrs = GNUNET_NETWORK_fdset_create ();
951 wes = GNUNET_NETWORK_fdset_create ();
952 wws = GNUNET_NETWORK_fdset_create ();
954 GNUNET_assert (MHD_YES == MHD_get_fdset (daemon_handle, &rs, &ws, &es, &max));
955 haveto = MHD_get_timeout (daemon_handle, &timeout);
956 if (haveto == MHD_YES)
958 if (timeout != last_timeout)
961 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
962 "SELECT Timeout changed from %llu to %llu\n",
963 last_timeout, timeout);
965 last_timeout = timeout;
967 tv.rel_value = (uint64_t) timeout;
970 tv = GNUNET_TIME_UNIT_SECONDS;
971 /* Force immediate run, since we have outbound data to send */
972 if (now == GNUNET_YES)
973 tv = GNUNET_TIME_UNIT_MILLISECONDS;
974 GNUNET_NETWORK_fdset_copy_native (wrs, &rs, max + 1);
975 GNUNET_NETWORK_fdset_copy_native (wws, &ws, max + 1);
976 GNUNET_NETWORK_fdset_copy_native (wes, &es, max + 1);
978 if (daemon_handle == plugin->server_v4)
980 if (plugin->server_v4_task != GNUNET_SCHEDULER_NO_TASK)
982 GNUNET_SCHEDULER_cancel (plugin->server_v4_task);
983 plugin->server_v4_task = GNUNET_SCHEDULER_NO_TASK;
986 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
987 "Scheduling IPv4 server task in %llu ms\n", tv);
990 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
991 GNUNET_SCHEDULER_NO_TASK, tv, wrs, wws,
992 &server_v4_run, plugin);
994 if (daemon_handle == plugin->server_v6)
996 if (plugin->server_v6_task != GNUNET_SCHEDULER_NO_TASK)
998 GNUNET_SCHEDULER_cancel (plugin->server_v6_task);
999 plugin->server_v6_task = GNUNET_SCHEDULER_NO_TASK;
1002 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1003 "Scheduling IPv6 server task in %llu ms\n", tv);
1006 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1007 GNUNET_SCHEDULER_NO_TASK, tv, wrs, wws,
1008 &server_v6_run, plugin);
1010 GNUNET_NETWORK_fdset_destroy (wrs);
1011 GNUNET_NETWORK_fdset_destroy (wws);
1012 GNUNET_NETWORK_fdset_destroy (wes);
1017 server_start (struct Plugin *plugin)
1019 int res = GNUNET_OK;
1020 unsigned int timeout;
1023 res = server_load_certificate (plugin);
1024 if (res == GNUNET_SYSERR)
1026 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
1027 "Could not load or create server certificate! Loading plugin failed!\n");
1033 #if MHD_VERSION >= 0x00090E00
1034 timeout = HTTP_NOT_VALIDATED_TIMEOUT.rel_value / 1000;
1035 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1036 "MHD can set timeout per connection! Default time out %u sec.\n",
1039 timeout = GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT.rel_value / 1000;
1040 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, plugin->name,
1041 "MHD cannot set timeout per connection! Default time out %u sec.\n",
1044 plugin->server_v4 = NULL;
1045 if (plugin->ipv4 == GNUNET_YES)
1047 plugin->server_v4 = MHD_start_daemon (
1054 MHD_NO_FLAG, plugin->port,
1055 &server_accept_cb, plugin,
1056 &server_access_cb, plugin,
1057 MHD_OPTION_SOCK_ADDR,
1058 (struct sockaddr_in *)
1059 plugin->server_addr_v4,
1060 MHD_OPTION_CONNECTION_LIMIT,
1062 plugin->max_connections,
1064 MHD_OPTION_HTTPS_PRIORITIES,
1065 plugin->crypto_init,
1066 MHD_OPTION_HTTPS_MEM_KEY,
1068 MHD_OPTION_HTTPS_MEM_CERT,
1071 MHD_OPTION_CONNECTION_TIMEOUT,
1073 MHD_OPTION_CONNECTION_MEMORY_LIMIT,
1075 GNUNET_SERVER_MAX_MESSAGE_SIZE),
1076 MHD_OPTION_NOTIFY_COMPLETED,
1077 &server_disconnect_cb, plugin,
1078 MHD_OPTION_EXTERNAL_LOGGER,
1079 server_log, NULL, MHD_OPTION_END);
1081 plugin->server_v6 = NULL;
1082 if (plugin->ipv6 == GNUNET_YES)
1084 plugin->server_v6 = MHD_start_daemon (
1091 MHD_USE_IPv6, plugin->port,
1092 &server_accept_cb, plugin,
1093 &server_access_cb, plugin,
1094 MHD_OPTION_SOCK_ADDR,
1095 (struct sockaddr_in6 *)
1096 plugin->server_addr_v6,
1097 MHD_OPTION_CONNECTION_LIMIT,
1099 plugin->max_connections,
1101 MHD_OPTION_HTTPS_PRIORITIES,
1102 plugin->crypto_init,
1103 MHD_OPTION_HTTPS_MEM_KEY,
1105 MHD_OPTION_HTTPS_MEM_CERT,
1108 MHD_OPTION_CONNECTION_TIMEOUT,
1110 MHD_OPTION_CONNECTION_MEMORY_LIMIT,
1112 GNUNET_SERVER_MAX_MESSAGE_SIZE),
1113 MHD_OPTION_NOTIFY_COMPLETED,
1114 &server_disconnect_cb, plugin,
1115 MHD_OPTION_EXTERNAL_LOGGER,
1116 server_log, NULL, MHD_OPTION_END);
1120 if ((plugin->ipv4 == GNUNET_YES) && (plugin->server_v4 == NULL))
1122 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
1123 "Failed to start %s IPv4 server component on port %u\n",
1124 plugin->name, plugin->port);
1125 return GNUNET_SYSERR;
1127 server_reschedule (plugin, plugin->server_v4, GNUNET_NO);
1129 if ((plugin->ipv6 == GNUNET_YES) && (plugin->server_v6 == NULL))
1131 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
1132 "Failed to start %s IPv6 server component on port %u\n",
1133 plugin->name, plugin->port);
1134 return GNUNET_SYSERR;
1136 server_reschedule (plugin, plugin->server_v6, GNUNET_NO);
1140 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1141 "%s server component started on port %u\n", plugin->name,
1148 server_stop (struct Plugin *plugin)
1150 struct Session *s = NULL;
1151 struct Session *t = NULL;
1153 struct MHD_Daemon *server_v4_tmp = plugin->server_v4;
1155 plugin->server_v4 = NULL;
1156 struct MHD_Daemon *server_v6_tmp = plugin->server_v6;
1158 plugin->server_v6 = NULL;
1160 if (plugin->server_v4_task != GNUNET_SCHEDULER_NO_TASK)
1162 GNUNET_SCHEDULER_cancel (plugin->server_v4_task);
1163 plugin->server_v4_task = GNUNET_SCHEDULER_NO_TASK;
1166 if (plugin->server_v6_task != GNUNET_SCHEDULER_NO_TASK)
1168 GNUNET_SCHEDULER_cancel (plugin->server_v6_task);
1169 plugin->server_v6_task = GNUNET_SCHEDULER_NO_TASK;
1172 if (server_v6_tmp != NULL)
1174 MHD_stop_daemon (server_v4_tmp);
1176 if (server_v6_tmp != NULL)
1178 MHD_stop_daemon (server_v6_tmp);
1181 /* cleaning up semi-sessions never propagated */
1182 s = plugin->server_semi_head;
1186 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1187 "Deleting semi-sessions %p\n", s);
1190 struct HTTP_Message *msg = s->msg_head;
1191 struct HTTP_Message *tmp = NULL;
1197 GNUNET_CONTAINER_DLL_remove (s->msg_head, s->msg_tail, msg);
1198 if (msg->transmit_cont != NULL)
1200 msg->transmit_cont (msg->transmit_cont_cls, &s->target, GNUNET_SYSERR);
1211 GNUNET_free_non_null (plugin->crypto_init);
1212 GNUNET_free_non_null (plugin->cert);
1213 GNUNET_free_non_null (plugin->key);
1217 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1218 "%s server component stopped\n", plugin->name);
1224 /* end of plugin_transport_http.c */