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>"
34 server_log (void *arg, const char *fmt, va_list ap)
38 vsnprintf (text, sizeof (text), fmt, ap);
40 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Server: %s\n", text);
43 struct ServerConnection
48 /* should this connection get disconnected? GNUNET_YES/NO */
51 struct Session *session;
52 struct MHD_Connection * mhd_conn;
56 * Check if incoming connection is accepted.
57 * NOTE: Here every connection is accepted
58 * @param cls plugin as closure
59 * @param addr address of incoming connection
60 * @param addr_len address length of incoming connection
61 * @return MHD_YES if connection is accepted, MHD_NO if connection is rejected
65 server_accept_cb (void *cls, const struct sockaddr *addr, socklen_t addr_len)
67 struct Plugin * plugin = cls;
69 if (plugin->cur_connections <= plugin->max_connections)
73 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Server: Cannot accept new connections\n");
81 server_load_file (const char *file)
83 struct GNUNET_DISK_FileHandle *gn_file;
87 if (0 != STAT (file, &fstat))
89 text = GNUNET_malloc (fstat.st_size + 1);
91 GNUNET_DISK_file_open (file, GNUNET_DISK_OPEN_READ,
92 GNUNET_DISK_PERM_USER_READ);
98 if (GNUNET_SYSERR == GNUNET_DISK_file_read (gn_file, text, fstat.st_size))
101 GNUNET_DISK_file_close (gn_file);
104 text[fstat.st_size] = '\0';
105 GNUNET_DISK_file_close (gn_file);
114 server_load_certificate (struct Plugin *plugin)
121 /* Get crypto init string from config
122 * If not present just use default values */
123 GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg, plugin->name,
124 "CRYPTO_INIT", &plugin->crypto_init);
127 GNUNET_CONFIGURATION_get_value_filename (plugin->env->cfg, plugin->name,
128 "KEY_FILE", &key_file))
130 key_file = "https_key.key";
134 GNUNET_CONFIGURATION_get_value_filename (plugin->env->cfg, plugin->name,
135 "CERT_FILE", &cert_file))
137 cert_file = "https_cert.crt";
140 /* read key & certificates from file */
142 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
143 "Loading TLS certificate from key-file `%s' cert-file`%s'\n",
144 key_file, cert_file);
147 plugin->key = server_load_file (key_file);
148 plugin->cert = server_load_file (cert_file);
150 if ((plugin->key == NULL) || (plugin->cert == NULL))
152 struct GNUNET_OS_Process *cert_creation;
154 GNUNET_free_non_null (plugin->key);
156 GNUNET_free_non_null (plugin->cert);
160 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
161 "No usable TLS certificate found, creating certificate\n");
165 GNUNET_OS_start_process (NULL, NULL,
166 "gnunet-transport-certificate-creation",
167 "gnunet-transport-certificate-creation",
168 key_file, cert_file, NULL);
169 if (cert_creation == NULL)
171 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
173 ("Could not create a new TLS certificate, program `gnunet-transport-certificate-creation' could not be started!\n"));
174 GNUNET_free (key_file);
175 GNUNET_free (cert_file);
177 GNUNET_free_non_null (plugin->key);
178 GNUNET_free_non_null (plugin->cert);
179 GNUNET_free_non_null (plugin->crypto_init);
181 return GNUNET_SYSERR;
183 GNUNET_assert (GNUNET_OK == GNUNET_OS_process_wait (cert_creation));
184 GNUNET_OS_process_close (cert_creation);
186 plugin->key = server_load_file (key_file);
187 plugin->cert = server_load_file (cert_file);
190 if ((plugin->key == NULL) || (plugin->cert == NULL))
192 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
194 ("No usable TLS certificate found and creating one failed!\n"),
196 GNUNET_free (key_file);
197 GNUNET_free (cert_file);
199 GNUNET_free_non_null (plugin->key);
200 GNUNET_free_non_null (plugin->cert);
201 GNUNET_free_non_null (plugin->crypto_init);
203 return GNUNET_SYSERR;
205 GNUNET_free (key_file);
206 GNUNET_free (cert_file);
208 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "TLS certificate loaded\n");
217 * Callback called by MessageStreamTokenizer when a message has arrived
218 * @param cls current session as closure
219 * @param client clien
220 * @param message the message to be forwarded to transport service
223 server_receive_mst_cb (void *cls, void *client,
224 const struct GNUNET_MessageHeader *message)
226 struct Session *s = cls;
228 struct Plugin *plugin = s->plugin;
230 struct GNUNET_TIME_Relative delay;
232 delay = http_plugin_receive (s, &s->target, message, s, s->addr, s->addrlen);
234 s->next_receive = GNUNET_TIME_absolute_add(GNUNET_TIME_absolute_get(), delay);
236 if (delay.rel_value > 0)
239 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name, "Server: peer `%s' address `%s' next read delayed for %llu ms\n",
240 GNUNET_i2s (&s->target), GNUNET_a2s (s->addr, s->addrlen), delay);
246 * Callback called by MHD when it needs data to send
247 * @param cls current session
248 * @param pos position in buffer
249 * @param buf the buffer to write data to
250 * @param max max number of bytes available in buffer
251 * @return bytes written to buffer
254 server_send_callback (void *cls, uint64_t pos, char *buf, size_t max)
256 struct Session *s = cls;
258 struct HTTP_Message *msg;
265 if ((msg->size - msg->pos) <= max)
267 memcpy (buf, &msg->buf[msg->pos], (msg->size - msg->pos));
268 bytes_read = msg->size - msg->pos;
269 msg->pos += (msg->size - msg->pos);
273 memcpy (buf, &msg->buf[msg->pos], max);
278 /* removing message */
279 if (msg->pos == msg->size)
281 if (NULL != msg->transmit_cont)
282 msg->transmit_cont (msg->transmit_cont_cls, &s->target, GNUNET_OK);
283 GNUNET_CONTAINER_DLL_remove(s->msg_head, s->msg_tail, msg);
289 struct Plugin *plugin = s->plugin;
290 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
291 "Server: %X: sent %u bytes\n",
298 * Process GET or PUT request received via MHD. For
299 * GET, queue response that will send back our pending
300 * messages. For PUT, process incoming data and send
301 * to GNUnet core. In either case, check if a session
302 * already exists and create a new one if not.
305 server_access_cb (void *cls, struct MHD_Connection *mhd_connection,
306 const char *url, const char *method, const char *version,
307 const char *upload_data, size_t * upload_data_size,
308 void **httpSessionCache)
311 struct Plugin *plugin = cls;
312 struct ServerConnection *sc = *httpSessionCache;
313 struct Session *s = NULL;
316 struct MHD_Response *response;
318 GNUNET_assert (cls != NULL);
323 const union MHD_ConnectionInfo *conn_info;
325 struct GNUNET_PeerIdentity target;
326 int check = GNUNET_NO;
330 conn_info = MHD_get_connection_info (mhd_connection, MHD_CONNECTION_INFO_CLIENT_ADDRESS);
331 if (conn_info->client_addr->sa_family == AF_INET)
332 addrlen = sizeof (struct sockaddr_in);
333 else if (conn_info->client_addr->sa_family == AF_INET6)
334 addrlen = sizeof (struct sockaddr_in6);
338 if ((strlen(&url[1]) >= 105) && (url[104] == ';'))
341 char * tagc = (char *) &url[105];
342 memcpy(&hash, &url[1], 103);
344 if (GNUNET_OK == GNUNET_CRYPTO_hash_from_string ((const char *) &hash, &(target.hashPubKey)))
346 tag = strtoul (tagc, NULL, 10);
352 if (0 == strcmp (MHD_HTTP_METHOD_PUT, method))
353 direction = _RECEIVE;
354 if (0 == strcmp (MHD_HTTP_METHOD_GET, method))
357 if (check == GNUNET_NO)
360 plugin->cur_connections++;
363 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
364 "Server: New inbound connection from %s with tag %u\n", GNUNET_i2s(&target), tag);
366 /* find duplicate session */
372 if ((t->inbound) && (0 == memcmp (&t->target, &target, sizeof (struct GNUNET_PeerIdentity))) &&
373 /* FIXME add source address comparison */
381 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
382 "Server: Duplicate session, dismissing new connection from peer `%s'\n", GNUNET_i2s (&target));
387 /* find semi-session */
388 t = plugin->server_semi_head;
392 /* FIXME add source address comparison */
393 if ((0 == memcmp (&t->target, &target, sizeof (struct GNUNET_PeerIdentity))) &&
405 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
406 "Server: Found existing semi-session for `%s'\n", GNUNET_i2s (&target));
409 if ((direction == _SEND) && (t->server_send != NULL))
412 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
413 "Server: Duplicate GET session, dismissing new connection from peer `%s'\n", GNUNET_i2s (&target));
420 GNUNET_CONTAINER_DLL_remove(plugin->server_semi_head, plugin->server_semi_tail, s);
421 GNUNET_CONTAINER_DLL_insert(plugin->head, plugin->tail, s);
423 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
424 "Server: Found matching semi-session, merging session for peer `%s'\n", GNUNET_i2s (&target));
429 if ((direction == _RECEIVE) && (t->server_recv != NULL))
432 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
433 "Server: Duplicate PUT session, dismissing new connection from peer `%s'\n", GNUNET_i2s (&target));
440 GNUNET_CONTAINER_DLL_remove(plugin->server_semi_head, plugin->server_semi_tail, s);
441 GNUNET_CONTAINER_DLL_insert(plugin->head, plugin->tail, s);
443 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
444 "Server: Found matching semi-session, merging session for peer `%s'\n", GNUNET_i2s (&target));
450 /* create new session */
452 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
453 "Server: Creating new session for peer `%s' \n", GNUNET_i2s (&target));
456 s = create_session (plugin,
458 conn_info->client_addr,
463 s->inbound = GNUNET_YES;
464 s->next_receive = GNUNET_TIME_absolute_get_zero();
466 if (0 == strcmp (MHD_HTTP_METHOD_PUT, method))
468 if (0 == strcmp (MHD_HTTP_METHOD_GET, method))
470 GNUNET_CONTAINER_DLL_insert (plugin->server_semi_head, plugin->server_semi_tail, s);
475 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
476 "Server: Invalid connection request\n");
478 response = MHD_create_response_from_data (strlen (HTTP_ERROR_RESPONSE),HTTP_ERROR_RESPONSE, MHD_NO, MHD_NO);
479 res = MHD_queue_response (mhd_connection, MHD_HTTP_NOT_FOUND, response);
480 MHD_destroy_response (response);
483 sc = GNUNET_malloc (sizeof (struct ServerConnection));
484 sc->mhd_conn = mhd_connection;
485 sc->direction = direction;
487 if (direction == _SEND)
489 if (direction == _RECEIVE)
492 int to = (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT.rel_value / 1000);
494 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
495 "Server: Setting Timeout to %u\n", to);
497 #if MHD_VERSION >= 0x00090E00
498 MHD_set_connection_option (mhd_connection, MHD_CONNECTION_OPTION_TIMEOUT, to);
500 (*httpSessionCache) = sc;
504 /* existing connection */
505 sc = (*httpSessionCache);
508 /* connection is to be disconnected*/
509 if (sc->disconnect == GNUNET_YES)
511 response = MHD_create_response_from_data (strlen ("Thank you!"), "Thank you!", MHD_NO, MHD_NO);
512 res = MHD_queue_response (mhd_connection, MHD_HTTP_OK, response);
514 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
515 "Sent HTTP/1.1: 200 OK as PUT Response\n");
517 MHD_destroy_response (response);
521 GNUNET_assert (s != NULL);
522 if (sc->direction == _SEND)
524 response = MHD_create_response_from_callback (-1, 32 * 1024, &server_send_callback,
526 res = MHD_queue_response (mhd_connection, MHD_HTTP_OK, response);
527 MHD_destroy_response (response);
530 if (sc->direction == _RECEIVE)
532 if (*upload_data_size == 0)
535 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
536 "Server: Peer `%s' PUT on address `%s' connected\n",
537 GNUNET_i2s (&s->target), GNUNET_a2s (s->addr, s->addrlen));
543 if ((*upload_data_size > 0))
546 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
547 "Server: peer `%s' PUT on address `%s' received %Zu bytes\n",
548 GNUNET_i2s (&s->target), GNUNET_a2s (s->addr, s->addrlen), *upload_data_size);
550 struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get();
551 if (( s->next_receive.abs_value <= now.abs_value))
554 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
555 "Server: %X: PUT with %u bytes forwarded to MST\n", s,
558 if (s->msg_tk == NULL)
560 s->msg_tk = GNUNET_SERVER_mst_create (&server_receive_mst_cb, s);
562 res = GNUNET_SERVER_mst_receive (s->msg_tk, s, upload_data, *upload_data_size, GNUNET_NO, GNUNET_NO);
564 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
565 "Server: Received %Zu bytes\n", *upload_data_size);
567 (*upload_data_size) = 0;
572 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
573 "Server: %X no inbound bandwidth available! Next read was delayed by %llu ms\n", s, now.abs_value - s->next_receive.abs_value);
585 * Function that queries MHD's select sets and
586 * starts the task waiting for them.
587 * @param plugin plugin
588 * @param daemon_handle the MHD daemon handle
589 * @return gnunet task identifier
591 static GNUNET_SCHEDULER_TaskIdentifier
592 server_schedule (struct Plugin *plugin, struct MHD_Daemon *daemon_handle);
595 server_disconnect_cb (void *cls, struct MHD_Connection *connection,
596 void **httpSessionCache)
598 struct ServerConnection *sc = *httpSessionCache;
599 struct ServerConnection *tc = *httpSessionCache;
600 struct Session * s = NULL;
601 struct Session * t = NULL;
602 struct Plugin * plugin = NULL;
609 if (sc->direction == _SEND)
612 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
613 "Server: peer `%s' GET on address `%s' disconnected\n",
614 GNUNET_i2s (&s->target), GNUNET_a2s (s->addr, s->addrlen));
616 s->server_send = NULL;
618 if (s->server_recv != NULL)
621 tc->disconnect = GNUNET_YES;
622 #if MHD_VERSION >= 0x00090E00
623 MHD_set_connection_option (sc->mhd_conn, MHD_CONNECTION_OPTION_TIMEOUT, 1);
627 if (sc->direction == _RECEIVE)
630 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
631 "Server: peer `%s' PUT on address `%s' disconnected\n",
632 GNUNET_i2s (&s->target), GNUNET_a2s (s->addr, s->addrlen));
634 s->server_recv = NULL;
635 if (s->server_send != NULL)
638 tc->disconnect = GNUNET_YES;
639 #if MHD_VERSION >= 0x00090E00
640 MHD_set_connection_option (sc->mhd_conn, MHD_CONNECTION_OPTION_TIMEOUT, 1);
643 if (s->msg_tk != NULL)
645 GNUNET_SERVER_mst_destroy(s->msg_tk);
651 t = plugin->server_semi_head;
656 GNUNET_CONTAINER_DLL_remove(plugin->server_semi_head, plugin->server_semi_tail, s);
657 GNUNET_CONTAINER_DLL_insert(plugin->head, plugin->tail, s);
662 plugin->cur_connections--;
664 if (plugin->server_v4_task != GNUNET_SCHEDULER_NO_TASK)
666 GNUNET_SCHEDULER_cancel(plugin->server_v4_task);
667 plugin->server_v4_task = GNUNET_SCHEDULER_NO_TASK;
669 plugin->server_v4_task = server_schedule (plugin, plugin->server_v4);
671 if (plugin->server_v6_task != GNUNET_SCHEDULER_NO_TASK)
673 GNUNET_SCHEDULER_cancel(plugin->server_v6_task);
674 plugin->server_v6_task = GNUNET_SCHEDULER_NO_TASK;
676 plugin->server_v6_task = server_schedule (plugin, plugin->server_v6);
678 if ((s->server_send == NULL) && (s->server_recv == NULL))
681 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
682 "Server: peer `%s' on address `%s' disconnected\n",
683 GNUNET_i2s (&s->target), GNUNET_a2s (s->addr, s->addrlen));
685 if (s->msg_tk != NULL)
687 GNUNET_SERVER_mst_destroy(s->msg_tk);
691 notify_session_end(s->plugin, &s->target, s);
696 server_disconnect (struct Session *s)
698 struct Plugin *plugin = s->plugin;
699 struct Session *t = plugin->head;
703 if (t->inbound == GNUNET_YES)
705 if (t->server_send != NULL)
707 ((struct ServerConnection *) t->server_send)->disconnect = GNUNET_YES;
709 if (t->server_send != NULL)
711 ((struct ServerConnection *) t->server_send)->disconnect = GNUNET_YES;
722 server_send (struct Session *s, struct HTTP_Message * msg)
724 GNUNET_CONTAINER_DLL_insert (s->msg_head, s->msg_tail, msg);
731 * Call MHD IPv4 to process pending requests and then go back
732 * and schedule the next run.
733 * @param cls plugin as closure
734 * @param tc task context
737 server_v4_run (void *cls,
738 const struct GNUNET_SCHEDULER_TaskContext *tc)
740 struct Plugin *plugin = cls;
741 GNUNET_assert (cls != NULL);
743 plugin->server_v4_task = GNUNET_SCHEDULER_NO_TASK;
745 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
748 GNUNET_assert (MHD_YES == MHD_run (plugin->server_v4));
749 plugin->server_v4_task = server_schedule (plugin, plugin->server_v4);
754 * Call MHD IPv6 to process pending requests and then go back
755 * and schedule the next run.
756 * @param cls plugin as closure
757 * @param tc task context
760 server_v6_run (void *cls,
761 const struct GNUNET_SCHEDULER_TaskContext *tc)
763 struct Plugin *plugin = cls;
764 GNUNET_assert (cls != NULL);
766 plugin->server_v6_task = GNUNET_SCHEDULER_NO_TASK;
768 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
771 GNUNET_assert (MHD_YES == MHD_run (plugin->server_v6));
772 plugin->server_v6_task = server_schedule (plugin, plugin->server_v6);
776 * Function that queries MHD's select sets and
777 * starts the task waiting for them.
778 * @param plugin plugin
779 * @param daemon_handle the MHD daemon handle
780 * @return gnunet task identifier
782 static GNUNET_SCHEDULER_TaskIdentifier
783 server_schedule (struct Plugin *plugin, struct MHD_Daemon *daemon_handle)
785 GNUNET_SCHEDULER_TaskIdentifier ret;
789 struct GNUNET_NETWORK_FDSet *wrs;
790 struct GNUNET_NETWORK_FDSet *wws;
791 struct GNUNET_NETWORK_FDSet *wes;
793 unsigned long long timeout;
795 struct GNUNET_TIME_Relative tv;
797 ret = GNUNET_SCHEDULER_NO_TASK;
801 wrs = GNUNET_NETWORK_fdset_create ();
802 wes = GNUNET_NETWORK_fdset_create ();
803 wws = GNUNET_NETWORK_fdset_create ();
805 GNUNET_assert (MHD_YES == MHD_get_fdset (daemon_handle, &rs, &ws, &es, &max));
806 haveto = MHD_get_timeout (daemon_handle, &timeout);
807 if (haveto == MHD_YES)
808 tv.rel_value = (uint64_t) timeout;
810 tv = GNUNET_TIME_UNIT_SECONDS;
811 GNUNET_NETWORK_fdset_copy_native (wrs, &rs, max + 1);
812 GNUNET_NETWORK_fdset_copy_native (wws, &ws, max + 1);
813 GNUNET_NETWORK_fdset_copy_native (wes, &es, max + 1);
814 if (daemon_handle == plugin->server_v4)
816 if (plugin->server_v4_task != GNUNET_SCHEDULER_NO_TASK)
818 GNUNET_SCHEDULER_cancel (plugin->server_v4_task);
819 plugin->server_v4_task = GNUNET_SCHEDULER_NO_TASK;
823 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
824 GNUNET_SCHEDULER_NO_TASK, tv, wrs, wws,
825 &server_v4_run, plugin);
827 if (daemon_handle == plugin->server_v6)
829 if (plugin->server_v6_task != GNUNET_SCHEDULER_NO_TASK)
831 GNUNET_SCHEDULER_cancel (plugin->server_v6_task);
832 plugin->server_v6_task = GNUNET_SCHEDULER_NO_TASK;
836 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
837 GNUNET_SCHEDULER_NO_TASK, tv, wrs, wws,
838 &server_v6_run, plugin);
840 GNUNET_NETWORK_fdset_destroy (wrs);
841 GNUNET_NETWORK_fdset_destroy (wws);
842 GNUNET_NETWORK_fdset_destroy (wes);
847 server_start (struct Plugin *plugin)
850 unsigned int timeout;
853 res = server_load_certificate (plugin);
854 if (res == GNUNET_SYSERR)
856 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
857 "Could not load or create server certificate! Loading plugin failed!\n");
863 #if MHD_VERSION >= 0x00090E00
864 timeout = GNUNET_CONSTANTS_DISCONNECT_SESSION_TIMEOUT.rel_value / 1000;
865 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
866 "MHD can set timeout per connection! Default time out %u sec.\n", timeout);
868 timeout = GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT.rel_value / 1000;
869 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
870 "MHD cannot set timeout per connection! Default time out %u sec.\n", timeout);
872 plugin->server_v4 = NULL;
873 if (plugin->ipv4 == GNUNET_YES)
875 plugin->server_v4 = MHD_start_daemon (
882 MHD_NO_FLAG, plugin->port,
883 &server_accept_cb, plugin,
884 &server_access_cb, plugin,
885 MHD_OPTION_SOCK_ADDR,
886 (struct sockaddr_in *)
887 plugin->server_addr_v4,
888 MHD_OPTION_CONNECTION_LIMIT,
890 plugin->max_connections,
892 MHD_OPTION_HTTPS_PRIORITIES,
894 MHD_OPTION_HTTPS_MEM_KEY,
896 MHD_OPTION_HTTPS_MEM_CERT,
899 MHD_OPTION_CONNECTION_TIMEOUT,
901 MHD_OPTION_CONNECTION_MEMORY_LIMIT,
903 GNUNET_SERVER_MAX_MESSAGE_SIZE),
904 MHD_OPTION_NOTIFY_COMPLETED,
905 &server_disconnect_cb, plugin,
906 MHD_OPTION_EXTERNAL_LOGGER,
907 server_log, NULL, MHD_OPTION_END);
908 if (plugin->server_v4 == NULL)
911 plugin->server_v6 = NULL;
912 if (plugin->ipv6 == GNUNET_YES)
914 plugin->server_v6 = MHD_start_daemon (
921 MHD_USE_IPv6, plugin->port,
922 &server_accept_cb, plugin,
923 &server_access_cb, plugin,
924 MHD_OPTION_SOCK_ADDR,
925 (struct sockaddr_in6 *)
926 plugin->server_addr_v6,
927 MHD_OPTION_CONNECTION_LIMIT,
929 plugin->max_connections,
931 MHD_OPTION_HTTPS_PRIORITIES,
933 MHD_OPTION_HTTPS_MEM_KEY,
935 MHD_OPTION_HTTPS_MEM_CERT,
938 MHD_OPTION_CONNECTION_TIMEOUT,
940 MHD_OPTION_CONNECTION_MEMORY_LIMIT,
942 GNUNET_SERVER_MAX_MESSAGE_SIZE),
943 MHD_OPTION_NOTIFY_COMPLETED,
944 &server_disconnect_cb, plugin,
945 MHD_OPTION_EXTERNAL_LOGGER,
946 server_log, NULL, MHD_OPTION_END);
948 if (plugin->server_v6 == NULL)
952 if (plugin->server_v4 != NULL)
953 plugin->server_v4_task = server_schedule (plugin, plugin->server_v4);
954 if (plugin->server_v6 != NULL)
955 plugin->server_v6_task = server_schedule (plugin, plugin->server_v6);
958 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
959 "%s server component started on port %u\n", plugin->name,
966 server_stop (struct Plugin *plugin)
968 struct Session *s = NULL;
969 struct Session *t = NULL;
971 if (plugin->server_v4_task != GNUNET_SCHEDULER_NO_TASK)
973 GNUNET_SCHEDULER_cancel (plugin->server_v4_task);
974 plugin->server_v4_task = GNUNET_SCHEDULER_NO_TASK;
977 if (plugin->server_v6_task != GNUNET_SCHEDULER_NO_TASK)
979 GNUNET_SCHEDULER_cancel (plugin->server_v6_task);
980 plugin->server_v6_task = GNUNET_SCHEDULER_NO_TASK;
983 if (plugin->server_v4 != NULL)
985 MHD_stop_daemon (plugin->server_v4);
986 plugin->server_v4 = NULL;
988 if (plugin->server_v6 != NULL)
990 MHD_stop_daemon (plugin->server_v6);
991 plugin->server_v6 = NULL;
994 /* cleaning up semi-sessions never propagated */
995 s = plugin->server_semi_head;
999 if (s->msg_tk != NULL)
1000 GNUNET_SERVER_mst_destroy(s->msg_tk);
1006 GNUNET_free_non_null (plugin->crypto_init);
1007 GNUNET_free_non_null (plugin->cert);
1008 GNUNET_free_non_null (plugin->key);
1012 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1013 "%s server component stopped\n", plugin->name);
1019 /* end of plugin_transport_http.c */