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);
252 if (GNUNET_TIME_absolute_get().abs_value < s->delay.abs_value)
255 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name, "Server: peer `%s' address `%s' next read delayed for %llu ms\n",
256 GNUNET_i2s (&s->target), GNUNET_a2s (s->addr, s->addrlen), delay);
262 * Callback called by MHD when it needs data to send
263 * @param cls current session
264 * @param pos position in buffer
265 * @param buf the buffer to write data to
266 * @param max max number of bytes available in buffer
267 * @return bytes written to buffer
270 server_send_callback (void *cls, uint64_t pos, char *buf, size_t max)
272 struct Session *s = cls;
273 struct Plugin *plugin = s->plugin;
274 struct HTTP_Message *msg;
281 if ((msg->size - msg->pos) <= max)
283 memcpy (buf, &msg->buf[msg->pos], (msg->size - msg->pos));
284 bytes_read = msg->size - msg->pos;
285 msg->pos += (msg->size - msg->pos);
289 memcpy (buf, &msg->buf[msg->pos], max);
294 /* removing message */
295 if (msg->pos == msg->size)
297 if (NULL != msg->transmit_cont)
298 msg->transmit_cont (msg->transmit_cont_cls, &s->target, GNUNET_OK);
299 GNUNET_CONTAINER_DLL_remove(s->msg_head, s->msg_tail, msg);
305 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
306 "Server: %X: sent %u bytes\n",
313 * Process GET or PUT request received via MHD. For
314 * GET, queue response that will send back our pending
315 * messages. For PUT, process incoming data and send
316 * to GNUnet core. In either case, check if a session
317 * already exists and create a new one if not.
320 server_access_cb (void *cls, struct MHD_Connection *mhd_connection,
321 const char *url, const char *method, const char *version,
322 const char *upload_data, size_t * upload_data_size,
323 void **httpSessionCache)
325 struct Plugin *plugin = cls;
326 struct ServerConnection *sc = *httpSessionCache;
327 struct Session *s = NULL;
330 struct MHD_Response *response;
332 GNUNET_assert (cls != NULL);
337 const union MHD_ConnectionInfo *conn_info;
339 struct GNUNET_PeerIdentity target;
340 int check = GNUNET_NO;
344 conn_info = MHD_get_connection_info (mhd_connection, MHD_CONNECTION_INFO_CLIENT_ADDRESS);
345 if (conn_info->client_addr->sa_family == AF_INET)
346 addrlen = sizeof (struct sockaddr_in);
347 else if (conn_info->client_addr->sa_family == AF_INET6)
348 addrlen = sizeof (struct sockaddr_in6);
352 if ((strlen(&url[1]) >= 105) && (url[104] == ';'))
355 char * tagc = (char *) &url[105];
356 memcpy(&hash, &url[1], 103);
358 if (GNUNET_OK == GNUNET_CRYPTO_hash_from_string ((const char *) &hash, &(target.hashPubKey)))
360 tag = strtoul (tagc, NULL, 10);
366 if (0 == strcmp (MHD_HTTP_METHOD_PUT, method))
367 direction = _RECEIVE;
368 if (0 == strcmp (MHD_HTTP_METHOD_GET, method))
371 if (check == GNUNET_NO)
374 plugin->cur_connections++;
377 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
378 "Server: New inbound connection from %s with tag %u\n", GNUNET_i2s(&target), tag);
380 /* find duplicate session */
386 if ((t->inbound) && (0 == memcmp (&t->target, &target, sizeof (struct GNUNET_PeerIdentity))) &&
387 /* FIXME add source address comparison */
395 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
396 "Server: Duplicate session, dismissing new connection from peer `%s'\n", GNUNET_i2s (&target));
401 /* find semi-session */
402 t = plugin->server_semi_head;
406 /* FIXME add source address comparison */
407 if ((0 == memcmp (&t->target, &target, sizeof (struct GNUNET_PeerIdentity))) &&
419 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
420 "Server: Found existing semi-session for `%s'\n", GNUNET_i2s (&target));
423 if ((direction == _SEND) && (t->server_send != NULL))
426 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
427 "Server: Duplicate GET session, dismissing new connection from peer `%s'\n", GNUNET_i2s (&target));
434 GNUNET_CONTAINER_DLL_remove(plugin->server_semi_head, plugin->server_semi_tail, s);
435 GNUNET_CONTAINER_DLL_insert(plugin->head, plugin->tail, s);
437 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
438 "Server: Found matching semi-session, merging session for peer `%s'\n", GNUNET_i2s (&target));
443 if ((direction == _RECEIVE) && (t->server_recv != NULL))
446 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
447 "Server: Duplicate PUT session, dismissing new connection from peer `%s'\n", GNUNET_i2s (&target));
454 GNUNET_CONTAINER_DLL_remove(plugin->server_semi_head, plugin->server_semi_tail, s);
455 GNUNET_CONTAINER_DLL_insert(plugin->head, plugin->tail, s);
457 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
458 "Server: Found matching semi-session, merging session for peer `%s'\n", GNUNET_i2s (&target));
464 /* create new session */
466 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
467 "Server: Creating new session for peer `%s' \n", GNUNET_i2s (&target));
470 s = create_session (plugin,
472 conn_info->client_addr,
477 s->inbound = GNUNET_YES;
479 if (0 == strcmp (MHD_HTTP_METHOD_PUT, method))
481 if (0 == strcmp (MHD_HTTP_METHOD_GET, method))
483 GNUNET_CONTAINER_DLL_insert (plugin->server_semi_head, plugin->server_semi_tail, s);
488 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
489 "Server: Invalid connection request\n");
491 response = MHD_create_response_from_data (strlen (HTTP_ERROR_RESPONSE),HTTP_ERROR_RESPONSE, MHD_NO, MHD_NO);
492 res = MHD_queue_response (mhd_connection, MHD_HTTP_NOT_FOUND, response);
493 MHD_destroy_response (response);
496 sc = GNUNET_malloc (sizeof (struct ServerConnection));
497 sc->mhd_conn = mhd_connection;
498 sc->direction = direction;
500 if (direction == _SEND)
502 if (direction == _RECEIVE)
505 int to = (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT.rel_value / 1000);
507 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
508 "Server: Setting Timeout to %u\n", to);
510 MHD_set_connection_option (mhd_connection, MHD_CONNECTION_OPTION_TIMEOUT, to);
512 (*httpSessionCache) = sc;
516 /* existing connection */
517 sc = (*httpSessionCache);
520 /* connection is to be disconnected*/
521 if (sc->disconnect == GNUNET_YES)
523 response = MHD_create_response_from_data (strlen ("Thank you!"), "Thank you!", MHD_NO, MHD_NO);
524 res = MHD_queue_response (mhd_connection, MHD_HTTP_OK, response);
526 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
527 "Sent HTTP/1.1: 200 OK as PUT Response\n");
529 MHD_destroy_response (response);
533 GNUNET_assert (s != NULL);
534 if (sc->direction == _SEND)
536 response = MHD_create_response_from_callback (-1, 32 * 1024, &server_send_callback,
538 res = MHD_queue_response (mhd_connection, MHD_HTTP_OK, response);
539 MHD_destroy_response (response);
542 if (sc->direction == _RECEIVE)
544 if (*upload_data_size == 0)
547 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
548 "Server: Peer `%s' PUT on address `%s' connected\n",
549 GNUNET_i2s (&s->target), GNUNET_a2s (s->addr, s->addrlen));
555 if ((*upload_data_size > 0))
558 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
559 "Server: peer `%s' PUT on address `%s' received %Zu bytes\n",
560 GNUNET_i2s (&s->target), GNUNET_a2s (s->addr, s->addrlen), *upload_data_size);
562 if ((GNUNET_TIME_absolute_get().abs_value < s->delay.abs_value))
565 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
566 "Server: %X: PUT with %u bytes forwarded to MST\n", s,
569 if (s->msg_tk == NULL)
571 s->msg_tk = GNUNET_SERVER_mst_create (&server_receive_mst_cb, s);
573 res = GNUNET_SERVER_mst_receive (s->msg_tk, s, upload_data, *upload_data_size, GNUNET_NO, GNUNET_NO);
574 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
575 "Server: Received %Zu bytes\n",
577 (*upload_data_size) = 0;
583 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
584 "Connection %X: no inbound bandwidth available! Next read was delayed for %llu ms\n",
585 s, ps->peercontext->delay.rel_value);
598 * Function that queries MHD's select sets and
599 * starts the task waiting for them.
600 * @param plugin plugin
601 * @param daemon_handle the MHD daemon handle
602 * @return gnunet task identifier
604 static GNUNET_SCHEDULER_TaskIdentifier
605 server_schedule (struct Plugin *plugin, struct MHD_Daemon *daemon_handle);
608 server_disconnect_cb (void *cls, struct MHD_Connection *connection,
609 void **httpSessionCache)
611 struct ServerConnection *sc = *httpSessionCache;
612 struct ServerConnection *tc = *httpSessionCache;
613 struct Session * s = NULL;
614 struct Session * t = NULL;
615 struct Plugin * plugin = NULL;
622 if (sc->direction == _SEND)
625 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
626 "Server: peer `%s' GET on address `%s' disconnected\n",
627 GNUNET_i2s (&s->target), GNUNET_a2s (s->addr, s->addrlen));
629 s->server_send = NULL;
631 if (s->server_recv != NULL)
634 tc->disconnect = GNUNET_YES;
635 MHD_set_connection_option (sc->mhd_conn, MHD_CONNECTION_OPTION_TIMEOUT, 1);
638 if (sc->direction == _RECEIVE)
641 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
642 "Server: peer `%s' PUT on address `%s' disconnected\n",
643 GNUNET_i2s (&s->target), GNUNET_a2s (s->addr, s->addrlen));
645 s->server_recv = NULL;
646 if (s->server_send != NULL)
649 tc->disconnect = GNUNET_YES;
650 MHD_set_connection_option (sc->mhd_conn, MHD_CONNECTION_OPTION_TIMEOUT, 1);
652 if (s->msg_tk != NULL)
654 GNUNET_SERVER_mst_destroy(s->msg_tk);
660 t = plugin->server_semi_head;
665 GNUNET_CONTAINER_DLL_remove(plugin->server_semi_head, plugin->server_semi_tail, s);
666 GNUNET_CONTAINER_DLL_insert(plugin->head, plugin->tail, s);
671 plugin->cur_connections--;
673 if (plugin->server_v4_task != GNUNET_SCHEDULER_NO_TASK)
675 GNUNET_SCHEDULER_cancel(plugin->server_v4_task);
676 plugin->server_v4_task = GNUNET_SCHEDULER_NO_TASK;
678 plugin->server_v4_task = server_schedule (plugin, plugin->server_v4);
680 if (plugin->server_v6_task != GNUNET_SCHEDULER_NO_TASK)
682 GNUNET_SCHEDULER_cancel(plugin->server_v6_task);
683 plugin->server_v6_task = GNUNET_SCHEDULER_NO_TASK;
685 plugin->server_v6_task = server_schedule (plugin, plugin->server_v6);
687 if ((s->server_send == NULL) && (s->server_recv == NULL))
690 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
691 "Server: peer `%s' on address `%s' disconnected\n",
692 GNUNET_i2s (&s->target), GNUNET_a2s (s->addr, s->addrlen));
694 if (s->msg_tk != NULL)
696 GNUNET_SERVER_mst_destroy(s->msg_tk);
700 notify_session_end(s->plugin, &s->target, s);
705 server_disconnect (struct Session *s)
707 struct Plugin *plugin = s->plugin;
708 struct Session *t = plugin->head;
712 if (t->inbound == GNUNET_YES)
714 if (t->server_send != NULL)
716 ((struct ServerConnection *) t->server_send)->disconnect = GNUNET_YES;
718 if (t->server_send != NULL)
720 ((struct ServerConnection *) t->server_send)->disconnect = GNUNET_YES;
731 server_send (struct Session *s, struct HTTP_Message * msg)
733 GNUNET_CONTAINER_DLL_insert (s->msg_head, s->msg_tail, msg);
740 * Call MHD IPv4 to process pending requests and then go back
741 * and schedule the next run.
742 * @param cls plugin as closure
743 * @param tc task context
746 server_v4_run (void *cls,
747 const struct GNUNET_SCHEDULER_TaskContext *tc)
749 struct Plugin *plugin = cls;
750 GNUNET_assert (cls != NULL);
752 plugin->server_v4_task = GNUNET_SCHEDULER_NO_TASK;
754 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
757 GNUNET_assert (MHD_YES == MHD_run (plugin->server_v4));
758 plugin->server_v4_task = server_schedule (plugin, plugin->server_v4);
763 * Call MHD IPv6 to process pending requests and then go back
764 * and schedule the next run.
765 * @param cls plugin as closure
766 * @param tc task context
769 server_v6_run (void *cls,
770 const struct GNUNET_SCHEDULER_TaskContext *tc)
772 struct Plugin *plugin = cls;
773 GNUNET_assert (cls != NULL);
775 plugin->server_v6_task = GNUNET_SCHEDULER_NO_TASK;
777 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
780 GNUNET_assert (MHD_YES == MHD_run (plugin->server_v6));
781 plugin->server_v6_task = server_schedule (plugin, plugin->server_v6);
785 * Function that queries MHD's select sets and
786 * starts the task waiting for them.
787 * @param plugin plugin
788 * @param daemon_handle the MHD daemon handle
789 * @return gnunet task identifier
791 static GNUNET_SCHEDULER_TaskIdentifier
792 server_schedule (struct Plugin *plugin, struct MHD_Daemon *daemon_handle)
794 GNUNET_SCHEDULER_TaskIdentifier ret;
798 struct GNUNET_NETWORK_FDSet *wrs;
799 struct GNUNET_NETWORK_FDSet *wws;
800 struct GNUNET_NETWORK_FDSet *wes;
802 unsigned long long timeout;
804 struct GNUNET_TIME_Relative tv;
806 ret = GNUNET_SCHEDULER_NO_TASK;
810 wrs = GNUNET_NETWORK_fdset_create ();
811 wes = GNUNET_NETWORK_fdset_create ();
812 wws = GNUNET_NETWORK_fdset_create ();
814 GNUNET_assert (MHD_YES == MHD_get_fdset (daemon_handle, &rs, &ws, &es, &max));
815 haveto = MHD_get_timeout (daemon_handle, &timeout);
816 if (haveto == MHD_YES)
817 tv.rel_value = (uint64_t) timeout;
819 tv = GNUNET_TIME_UNIT_SECONDS;
820 GNUNET_NETWORK_fdset_copy_native (wrs, &rs, max + 1);
821 GNUNET_NETWORK_fdset_copy_native (wws, &ws, max + 1);
822 GNUNET_NETWORK_fdset_copy_native (wes, &es, max + 1);
823 if (daemon_handle == plugin->server_v4)
825 if (plugin->server_v4_task != GNUNET_SCHEDULER_NO_TASK)
827 GNUNET_SCHEDULER_cancel (plugin->server_v4_task);
828 plugin->server_v4_task = GNUNET_SCHEDULER_NO_TASK;
832 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
833 GNUNET_SCHEDULER_NO_TASK, tv, wrs, wws,
834 &server_v4_run, plugin);
836 if (daemon_handle == plugin->server_v6)
838 if (plugin->server_v6_task != GNUNET_SCHEDULER_NO_TASK)
840 GNUNET_SCHEDULER_cancel (plugin->server_v6_task);
841 plugin->server_v6_task = GNUNET_SCHEDULER_NO_TASK;
845 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
846 GNUNET_SCHEDULER_NO_TASK, tv, wrs, wws,
847 &server_v6_run, plugin);
849 GNUNET_NETWORK_fdset_destroy (wrs);
850 GNUNET_NETWORK_fdset_destroy (wws);
851 GNUNET_NETWORK_fdset_destroy (wes);
856 server_start (struct Plugin *plugin)
861 res = server_load_certificate (plugin);
862 if (res == GNUNET_SYSERR)
864 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "TABORT\n");
869 plugin->server_v4 = NULL;
870 if (plugin->ipv4 == GNUNET_YES)
872 plugin->server_v4 = MHD_start_daemon (
879 MHD_NO_FLAG, plugin->port,
880 &server_accept_cb, plugin,
881 &server_access_cb, plugin,
882 MHD_OPTION_SOCK_ADDR,
883 (struct sockaddr_in *)
884 plugin->server_addr_v4,
885 MHD_OPTION_CONNECTION_LIMIT,
887 plugin->max_connections,
889 MHD_OPTION_HTTPS_PRIORITIES,
891 MHD_OPTION_HTTPS_MEM_KEY,
893 MHD_OPTION_HTTPS_MEM_CERT,
896 MHD_OPTION_CONNECTION_TIMEOUT,
898 MHD_OPTION_CONNECTION_MEMORY_LIMIT,
900 GNUNET_SERVER_MAX_MESSAGE_SIZE),
901 MHD_OPTION_NOTIFY_COMPLETED,
902 &server_disconnect_cb, plugin,
903 MHD_OPTION_EXTERNAL_LOGGER,
904 server_log, NULL, MHD_OPTION_END);
905 if (plugin->server_v4 == NULL)
908 plugin->server_v6 = NULL;
909 if (plugin->ipv6 == GNUNET_YES)
911 plugin->server_v6 = MHD_start_daemon (
918 MHD_USE_IPv6, plugin->port,
919 &server_accept_cb, plugin,
920 &server_access_cb, plugin,
921 MHD_OPTION_SOCK_ADDR,
922 (struct sockaddr_in6 *)
923 plugin->server_addr_v6,
924 MHD_OPTION_CONNECTION_LIMIT,
926 plugin->max_connections,
928 MHD_OPTION_HTTPS_PRIORITIES,
930 MHD_OPTION_HTTPS_MEM_KEY,
932 MHD_OPTION_HTTPS_MEM_CERT,
935 MHD_OPTION_CONNECTION_TIMEOUT,
937 MHD_OPTION_CONNECTION_MEMORY_LIMIT,
939 GNUNET_SERVER_MAX_MESSAGE_SIZE),
940 MHD_OPTION_NOTIFY_COMPLETED,
941 &server_disconnect_cb, plugin,
942 MHD_OPTION_EXTERNAL_LOGGER,
943 server_log, NULL, MHD_OPTION_END);
945 if (plugin->server_v6 == NULL)
949 if (plugin->server_v4 != NULL)
950 plugin->server_v4_task = server_schedule (plugin, plugin->server_v4);
951 if (plugin->server_v6 != NULL)
952 plugin->server_v6_task = server_schedule (plugin, plugin->server_v6);
955 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
956 "%s server component started on port %u\n", plugin->name,
963 server_stop (struct Plugin *plugin)
965 struct Session *s = NULL;
966 struct Session *t = NULL;
968 if (plugin->server_v4_task != GNUNET_SCHEDULER_NO_TASK)
970 GNUNET_SCHEDULER_cancel (plugin->server_v4_task);
971 plugin->server_v4_task = GNUNET_SCHEDULER_NO_TASK;
974 if (plugin->server_v6_task != GNUNET_SCHEDULER_NO_TASK)
976 GNUNET_SCHEDULER_cancel (plugin->server_v6_task);
977 plugin->server_v6_task = GNUNET_SCHEDULER_NO_TASK;
980 if (plugin->server_v4 != NULL)
982 MHD_stop_daemon (plugin->server_v4);
983 plugin->server_v4 = NULL;
985 if (plugin->server_v6 != NULL)
987 MHD_stop_daemon (plugin->server_v6);
988 plugin->server_v6 = NULL;
991 /* cleaning up semi-sessions never propagated */
992 s = plugin->server_semi_head;
996 if (s->msg_tk != NULL)
997 GNUNET_SERVER_mst_destroy(s->msg_tk);
1003 GNUNET_free_non_null (plugin->crypto_init);
1004 GNUNET_free_non_null (plugin->cert);
1005 GNUNET_free_non_null (plugin->key);
1009 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1010 "%s server component stopped\n", plugin->name);
1016 /* end of plugin_transport_http.c */