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");
80 * Callback called by MHD when it needs data to send
81 * @param cls current session
82 * @param pos position in buffer
83 * @param buf the buffer to write data to
84 * @param max max number of bytes available in buffer
85 * @return bytes written to buffer
89 server_send_cb (void *cls, uint64_t pos, char *buf, size_t max)
99 server_load_file (const char *file)
101 struct GNUNET_DISK_FileHandle *gn_file;
105 if (0 != STAT (file, &fstat))
107 text = GNUNET_malloc (fstat.st_size + 1);
109 GNUNET_DISK_file_open (file, GNUNET_DISK_OPEN_READ,
110 GNUNET_DISK_PERM_USER_READ);
116 if (GNUNET_SYSERR == GNUNET_DISK_file_read (gn_file, text, fstat.st_size))
119 GNUNET_DISK_file_close (gn_file);
122 text[fstat.st_size] = '\0';
123 GNUNET_DISK_file_close (gn_file);
132 server_load_certificate (struct Plugin *plugin)
139 /* Get crypto init string from config
140 * If not present just use default values */
141 GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg, plugin->name,
142 "CRYPTO_INIT", &plugin->crypto_init);
145 GNUNET_CONFIGURATION_get_value_filename (plugin->env->cfg, plugin->name,
146 "KEY_FILE", &key_file))
148 key_file = "https_key.key";
152 GNUNET_CONFIGURATION_get_value_filename (plugin->env->cfg, plugin->name,
153 "CERT_FILE", &cert_file))
155 cert_file = "https_cert.crt";
158 /* 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);
165 plugin->key = server_load_file (key_file);
166 plugin->cert = server_load_file (cert_file);
168 if ((plugin->key == NULL) || (plugin->cert == NULL))
170 struct GNUNET_OS_Process *cert_creation;
172 GNUNET_free_non_null (plugin->key);
174 GNUNET_free_non_null (plugin->cert);
178 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
179 "No usable TLS certificate found, creating certificate\n");
183 GNUNET_OS_start_process (NULL, NULL,
184 "gnunet-transport-certificate-creation",
185 "gnunet-transport-certificate-creation",
186 key_file, cert_file, NULL);
187 if (cert_creation == NULL)
189 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
191 ("Could not create a new TLS certificate, program `gnunet-transport-certificate-creation' could not be started!\n"));
192 GNUNET_free (key_file);
193 GNUNET_free (cert_file);
195 GNUNET_free_non_null (plugin->key);
196 GNUNET_free_non_null (plugin->cert);
197 GNUNET_free_non_null (plugin->crypto_init);
199 return GNUNET_SYSERR;
201 GNUNET_assert (GNUNET_OK == GNUNET_OS_process_wait (cert_creation));
202 GNUNET_OS_process_close (cert_creation);
204 plugin->key = server_load_file (key_file);
205 plugin->cert = server_load_file (cert_file);
208 if ((plugin->key == NULL) || (plugin->cert == NULL))
210 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
212 ("No usable TLS certificate found and creating one failed!\n"),
214 GNUNET_free (key_file);
215 GNUNET_free (cert_file);
217 GNUNET_free_non_null (plugin->key);
218 GNUNET_free_non_null (plugin->cert);
219 GNUNET_free_non_null (plugin->crypto_init);
221 return GNUNET_SYSERR;
223 GNUNET_free (key_file);
224 GNUNET_free (cert_file);
226 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "TLS certificate loaded\n");
235 * Callback called by MessageStreamTokenizer when a message has arrived
236 * @param cls current session as closure
237 * @param client clien
238 * @param message the message to be forwarded to transport service
241 server_receive_mst_cb (void *cls, void *client,
242 const struct GNUNET_MessageHeader *message)
244 struct Session *s = cls;
245 struct Plugin *plugin = s->plugin;
246 struct GNUNET_TIME_Relative delay;
248 delay = http_plugin_receive (s, &s->target, message, s, s->addr, s->addrlen);
250 s->delay = GNUNET_TIME_absolute_add(GNUNET_TIME_absolute_get(), delay);
251 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name, "Server: SERVER DELAY %llu ms\n",
254 if (GNUNET_TIME_absolute_get().abs_value < s->delay.abs_value)
257 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name, "Server: peer `%s' address `%s' next read delayed for %llu ms\n",
258 GNUNET_i2s (&s->target), GNUNET_a2s (s->addr, s->addrlen), delay);
264 * Callback called by MHD when it needs data to send
265 * @param cls current session
266 * @param pos position in buffer
267 * @param buf the buffer to write data to
268 * @param max max number of bytes available in buffer
269 * @return bytes written to buffer
272 server_send_callback (void *cls, uint64_t pos, char *buf, size_t max)
274 struct Session *s = cls;
275 struct Plugin *plugin = s->plugin;
276 struct HTTP_Message *msg;
283 if ((msg->size - msg->pos) <= max)
285 memcpy (buf, &msg->buf[msg->pos], (msg->size - msg->pos));
286 bytes_read = msg->size - msg->pos;
287 msg->pos += (msg->size - msg->pos);
291 memcpy (buf, &msg->buf[msg->pos], max);
296 /* removing message */
297 if (msg->pos == msg->size)
299 if (NULL != msg->transmit_cont)
300 msg->transmit_cont (msg->transmit_cont_cls, &s->target, GNUNET_OK);
301 GNUNET_CONTAINER_DLL_remove(s->msg_head, s->msg_tail, msg);
307 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
308 "Server: %X: sent %u bytes\n",
315 * Process GET or PUT request received via MHD. For
316 * GET, queue response that will send back our pending
317 * messages. For PUT, process incoming data and send
318 * to GNUnet core. In either case, check if a session
319 * already exists and create a new one if not.
322 server_access_cb (void *cls, struct MHD_Connection *mhd_connection,
323 const char *url, const char *method, const char *version,
324 const char *upload_data, size_t * upload_data_size,
325 void **httpSessionCache)
327 struct Plugin *plugin = cls;
328 struct ServerConnection *sc = *httpSessionCache;
329 struct Session *s = NULL;
332 struct MHD_Response *response;
334 GNUNET_assert (cls != NULL);
339 const union MHD_ConnectionInfo *conn_info;
341 struct GNUNET_PeerIdentity target;
342 int check = GNUNET_NO;
346 conn_info = MHD_get_connection_info (mhd_connection, MHD_CONNECTION_INFO_CLIENT_ADDRESS);
347 if (conn_info->client_addr->sa_family == AF_INET)
348 addrlen = sizeof (struct sockaddr_in);
349 else if (conn_info->client_addr->sa_family == AF_INET6)
350 addrlen = sizeof (struct sockaddr_in6);
354 if ((strlen(&url[1]) >= 105) && (url[104] == ';'))
357 char * tagc = (char *) &url[105];
358 memcpy(&hash, &url[1], 103);
360 if (GNUNET_OK == GNUNET_CRYPTO_hash_from_string ((const char *) &hash, &(target.hashPubKey)))
362 tag = strtoul (tagc, NULL, 10);
368 if (0 == strcmp (MHD_HTTP_METHOD_PUT, method))
369 direction = _RECEIVE;
370 if (0 == strcmp (MHD_HTTP_METHOD_GET, method))
373 if (check == GNUNET_NO)
376 plugin->cur_connections++;
379 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
380 "Server: New inbound connection from %s with tag %u\n", GNUNET_i2s(&target), tag);
382 /* find duplicate session */
388 if ((t->inbound) && (0 == memcmp (&t->target, &target, sizeof (struct GNUNET_PeerIdentity))) &&
389 /* FIXME add source address comparison */
397 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
398 "Server: Duplicate session, dismissing new connection from peer `%s'\n", GNUNET_i2s (&target));
403 /* find semi-session */
404 t = plugin->server_semi_head;
408 /* FIXME add source address comparison */
409 if ((0 == memcmp (&t->target, &target, sizeof (struct GNUNET_PeerIdentity))) &&
421 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
422 "Server: Found existing semi-session for `%s'\n", GNUNET_i2s (&target));
425 if ((direction == _SEND) && (t->server_send != NULL))
428 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
429 "Server: Duplicate GET session, dismissing new connection from peer `%s'\n", GNUNET_i2s (&target));
436 GNUNET_CONTAINER_DLL_remove(plugin->server_semi_head, plugin->server_semi_tail, s);
437 GNUNET_CONTAINER_DLL_insert(plugin->head, plugin->tail, s);
439 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
440 "Server: Found matching semi-session, merging session for peer `%s'\n", GNUNET_i2s (&target));
445 if ((direction == _RECEIVE) && (t->server_recv != NULL))
448 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
449 "Server: Duplicate PUT session, dismissing new connection from peer `%s'\n", GNUNET_i2s (&target));
456 GNUNET_CONTAINER_DLL_remove(plugin->server_semi_head, plugin->server_semi_tail, s);
457 GNUNET_CONTAINER_DLL_insert(plugin->head, plugin->tail, s);
459 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
460 "Server: Found matching semi-session, merging session for peer `%s'\n", GNUNET_i2s (&target));
466 /* create new session */
468 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
469 "Server: Creating new session for peer `%s' \n", GNUNET_i2s (&target));
472 s = create_session (plugin,
474 conn_info->client_addr,
479 s->inbound = GNUNET_YES;
480 s->delay = GNUNET_TIME_absolute_get_zero();
482 if (0 == strcmp (MHD_HTTP_METHOD_PUT, method))
484 if (0 == strcmp (MHD_HTTP_METHOD_GET, method))
486 GNUNET_CONTAINER_DLL_insert (plugin->server_semi_head, plugin->server_semi_tail, s);
491 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
492 "Server: Invalid connection request\n");
494 response = MHD_create_response_from_data (strlen (HTTP_ERROR_RESPONSE),HTTP_ERROR_RESPONSE, MHD_NO, MHD_NO);
495 res = MHD_queue_response (mhd_connection, MHD_HTTP_NOT_FOUND, response);
496 MHD_destroy_response (response);
499 sc = GNUNET_malloc (sizeof (struct ServerConnection));
500 sc->mhd_conn = mhd_connection;
501 sc->direction = direction;
503 if (direction == _SEND)
505 if (direction == _RECEIVE)
508 int to = (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT.rel_value / 1000);
510 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
511 "Server: Setting Timeout to %u\n", to);
513 MHD_set_connection_option (mhd_connection, MHD_CONNECTION_OPTION_TIMEOUT, to);
515 (*httpSessionCache) = sc;
519 /* existing connection */
520 sc = (*httpSessionCache);
523 /* connection is to be disconnected*/
524 if (sc->disconnect == GNUNET_YES)
526 response = MHD_create_response_from_data (strlen ("Thank you!"), "Thank you!", MHD_NO, MHD_NO);
527 res = MHD_queue_response (mhd_connection, MHD_HTTP_OK, response);
529 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
530 "Sent HTTP/1.1: 200 OK as PUT Response\n");
532 MHD_destroy_response (response);
536 GNUNET_assert (s != NULL);
537 if (sc->direction == _SEND)
539 response = MHD_create_response_from_callback (-1, 32 * 1024, &server_send_callback,
541 res = MHD_queue_response (mhd_connection, MHD_HTTP_OK, response);
542 MHD_destroy_response (response);
545 if (sc->direction == _RECEIVE)
547 if (*upload_data_size == 0)
550 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
551 "Server: Peer `%s' PUT on address `%s' connected\n",
552 GNUNET_i2s (&s->target), GNUNET_a2s (s->addr, s->addrlen));
558 if ((*upload_data_size > 0))
561 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
562 "Server: peer `%s' PUT on address `%s' received %Zu bytes\n",
563 GNUNET_i2s (&s->target), GNUNET_a2s (s->addr, s->addrlen), *upload_data_size);
565 struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get();
566 if (( s->delay.abs_value < now.abs_value))
569 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
570 "Server: %X: PUT with %u bytes forwarded to MST\n", s,
573 if (s->msg_tk == NULL)
575 s->msg_tk = GNUNET_SERVER_mst_create (&server_receive_mst_cb, s);
577 res = GNUNET_SERVER_mst_receive (s->msg_tk, s, upload_data, *upload_data_size, GNUNET_NO, GNUNET_NO);
578 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
579 "Server: Received %Zu bytes\n",
581 (*upload_data_size) = 0;
587 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
588 "Connection %X: no inbound bandwidth available! Next read was delayed by %llu ms\n", s, now.abs_value - s->delay.abs_value);
601 * Function that queries MHD's select sets and
602 * starts the task waiting for them.
603 * @param plugin plugin
604 * @param daemon_handle the MHD daemon handle
605 * @return gnunet task identifier
607 static GNUNET_SCHEDULER_TaskIdentifier
608 server_schedule (struct Plugin *plugin, struct MHD_Daemon *daemon_handle);
611 server_disconnect_cb (void *cls, struct MHD_Connection *connection,
612 void **httpSessionCache)
614 struct ServerConnection *sc = *httpSessionCache;
615 struct ServerConnection *tc = *httpSessionCache;
616 struct Session * s = NULL;
617 struct Session * t = NULL;
618 struct Plugin * plugin = NULL;
625 if (sc->direction == _SEND)
628 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
629 "Server: peer `%s' GET on address `%s' disconnected\n",
630 GNUNET_i2s (&s->target), GNUNET_a2s (s->addr, s->addrlen));
632 s->server_send = NULL;
634 if (s->server_recv != NULL)
637 tc->disconnect = GNUNET_YES;
638 MHD_set_connection_option (sc->mhd_conn, MHD_CONNECTION_OPTION_TIMEOUT, 1);
641 if (sc->direction == _RECEIVE)
644 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
645 "Server: peer `%s' PUT on address `%s' disconnected\n",
646 GNUNET_i2s (&s->target), GNUNET_a2s (s->addr, s->addrlen));
648 s->server_recv = NULL;
649 if (s->server_send != NULL)
652 tc->disconnect = GNUNET_YES;
653 MHD_set_connection_option (sc->mhd_conn, MHD_CONNECTION_OPTION_TIMEOUT, 1);
655 if (s->msg_tk != NULL)
657 GNUNET_SERVER_mst_destroy(s->msg_tk);
663 t = plugin->server_semi_head;
668 GNUNET_CONTAINER_DLL_remove(plugin->server_semi_head, plugin->server_semi_tail, s);
669 GNUNET_CONTAINER_DLL_insert(plugin->head, plugin->tail, s);
674 plugin->cur_connections--;
676 if (plugin->server_v4_task != GNUNET_SCHEDULER_NO_TASK)
678 GNUNET_SCHEDULER_cancel(plugin->server_v4_task);
679 plugin->server_v4_task = GNUNET_SCHEDULER_NO_TASK;
681 plugin->server_v4_task = server_schedule (plugin, plugin->server_v4);
683 if (plugin->server_v6_task != GNUNET_SCHEDULER_NO_TASK)
685 GNUNET_SCHEDULER_cancel(plugin->server_v6_task);
686 plugin->server_v6_task = GNUNET_SCHEDULER_NO_TASK;
688 plugin->server_v6_task = server_schedule (plugin, plugin->server_v6);
690 if ((s->server_send == NULL) && (s->server_recv == NULL))
693 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
694 "Server: peer `%s' on address `%s' disconnected\n",
695 GNUNET_i2s (&s->target), GNUNET_a2s (s->addr, s->addrlen));
697 if (s->msg_tk != NULL)
699 GNUNET_SERVER_mst_destroy(s->msg_tk);
703 notify_session_end(s->plugin, &s->target, s);
708 server_disconnect (struct Session *s)
710 struct Plugin *plugin = s->plugin;
711 struct Session *t = plugin->head;
715 if (t->inbound == GNUNET_YES)
717 if (t->server_send != NULL)
719 ((struct ServerConnection *) t->server_send)->disconnect = GNUNET_YES;
721 if (t->server_send != NULL)
723 ((struct ServerConnection *) t->server_send)->disconnect = GNUNET_YES;
734 server_send (struct Session *s, struct HTTP_Message * msg)
736 GNUNET_CONTAINER_DLL_insert (s->msg_head, s->msg_tail, msg);
743 * Call MHD IPv4 to process pending requests and then go back
744 * and schedule the next run.
745 * @param cls plugin as closure
746 * @param tc task context
749 server_v4_run (void *cls,
750 const struct GNUNET_SCHEDULER_TaskContext *tc)
752 struct Plugin *plugin = cls;
753 GNUNET_assert (cls != NULL);
755 plugin->server_v4_task = GNUNET_SCHEDULER_NO_TASK;
757 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
760 GNUNET_assert (MHD_YES == MHD_run (plugin->server_v4));
761 plugin->server_v4_task = server_schedule (plugin, plugin->server_v4);
766 * Call MHD IPv6 to process pending requests and then go back
767 * and schedule the next run.
768 * @param cls plugin as closure
769 * @param tc task context
772 server_v6_run (void *cls,
773 const struct GNUNET_SCHEDULER_TaskContext *tc)
775 struct Plugin *plugin = cls;
776 GNUNET_assert (cls != NULL);
778 plugin->server_v6_task = GNUNET_SCHEDULER_NO_TASK;
780 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
783 GNUNET_assert (MHD_YES == MHD_run (plugin->server_v6));
784 plugin->server_v6_task = server_schedule (plugin, plugin->server_v6);
788 * Function that queries MHD's select sets and
789 * starts the task waiting for them.
790 * @param plugin plugin
791 * @param daemon_handle the MHD daemon handle
792 * @return gnunet task identifier
794 static GNUNET_SCHEDULER_TaskIdentifier
795 server_schedule (struct Plugin *plugin, struct MHD_Daemon *daemon_handle)
797 GNUNET_SCHEDULER_TaskIdentifier ret;
801 struct GNUNET_NETWORK_FDSet *wrs;
802 struct GNUNET_NETWORK_FDSet *wws;
803 struct GNUNET_NETWORK_FDSet *wes;
805 unsigned long long timeout;
807 struct GNUNET_TIME_Relative tv;
809 ret = GNUNET_SCHEDULER_NO_TASK;
813 wrs = GNUNET_NETWORK_fdset_create ();
814 wes = GNUNET_NETWORK_fdset_create ();
815 wws = GNUNET_NETWORK_fdset_create ();
817 GNUNET_assert (MHD_YES == MHD_get_fdset (daemon_handle, &rs, &ws, &es, &max));
818 haveto = MHD_get_timeout (daemon_handle, &timeout);
819 if (haveto == MHD_YES)
820 tv.rel_value = (uint64_t) timeout;
822 tv = GNUNET_TIME_UNIT_SECONDS;
823 GNUNET_NETWORK_fdset_copy_native (wrs, &rs, max + 1);
824 GNUNET_NETWORK_fdset_copy_native (wws, &ws, max + 1);
825 GNUNET_NETWORK_fdset_copy_native (wes, &es, max + 1);
826 if (daemon_handle == plugin->server_v4)
828 if (plugin->server_v4_task != GNUNET_SCHEDULER_NO_TASK)
830 GNUNET_SCHEDULER_cancel (plugin->server_v4_task);
831 plugin->server_v4_task = GNUNET_SCHEDULER_NO_TASK;
835 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
836 GNUNET_SCHEDULER_NO_TASK, tv, wrs, wws,
837 &server_v4_run, plugin);
839 if (daemon_handle == plugin->server_v6)
841 if (plugin->server_v6_task != GNUNET_SCHEDULER_NO_TASK)
843 GNUNET_SCHEDULER_cancel (plugin->server_v6_task);
844 plugin->server_v6_task = GNUNET_SCHEDULER_NO_TASK;
848 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
849 GNUNET_SCHEDULER_NO_TASK, tv, wrs, wws,
850 &server_v6_run, plugin);
852 GNUNET_NETWORK_fdset_destroy (wrs);
853 GNUNET_NETWORK_fdset_destroy (wws);
854 GNUNET_NETWORK_fdset_destroy (wes);
859 server_start (struct Plugin *plugin)
864 res = server_load_certificate (plugin);
865 if (res == GNUNET_SYSERR)
867 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "TABORT\n");
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 */