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 * @return gnunet task identifier
52 static GNUNET_SCHEDULER_TaskIdentifier
53 server_schedule (struct Plugin *plugin, struct MHD_Daemon *daemon_handle, int now);
56 server_log (void *arg, const char *fmt, va_list ap)
60 vsnprintf (text, sizeof (text), fmt, ap);
62 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Server: %s\n", text);
66 * Check if incoming connection is accepted.
67 * NOTE: Here every connection is accepted
68 * @param cls plugin as closure
69 * @param addr address of incoming connection
70 * @param addr_len address length of incoming connection
71 * @return MHD_YES if connection is accepted, MHD_NO if connection is rejected
75 server_accept_cb (void *cls, const struct sockaddr *addr, socklen_t addr_len)
77 struct Plugin * plugin = cls;
79 if (plugin->cur_connections <= plugin->max_connections)
83 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Server: Cannot accept new connections\n");
91 server_load_file (const char *file)
93 struct GNUNET_DISK_FileHandle *gn_file;
97 if (0 != STAT (file, &fstat))
99 text = GNUNET_malloc (fstat.st_size + 1);
101 GNUNET_DISK_file_open (file, GNUNET_DISK_OPEN_READ,
102 GNUNET_DISK_PERM_USER_READ);
108 if (GNUNET_SYSERR == GNUNET_DISK_file_read (gn_file, text, fstat.st_size))
111 GNUNET_DISK_file_close (gn_file);
114 text[fstat.st_size] = '\0';
115 GNUNET_DISK_file_close (gn_file);
124 server_load_certificate (struct Plugin *plugin)
131 /* Get crypto init string from config
132 * If not present just use default values */
134 GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg, plugin->name,
135 "CRYPTO_INIT", &plugin->crypto_init));
138 GNUNET_CONFIGURATION_get_value_filename (plugin->env->cfg, plugin->name,
139 "KEY_FILE", &key_file))
141 key_file = GNUNET_strdup ("https_key.key");
145 GNUNET_CONFIGURATION_get_value_filename (plugin->env->cfg, plugin->name,
146 "CERT_FILE", &cert_file))
148 GNUNET_asprintf(&cert_file, "%s", "https_cert.crt");
151 /* read key & certificates from file */
153 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
154 "Loading TLS certificate from key-file `%s' cert-file`%s'\n",
155 key_file, cert_file);
158 plugin->key = server_load_file (key_file);
159 plugin->cert = server_load_file (cert_file);
161 if ((plugin->key == NULL) || (plugin->cert == NULL))
163 struct GNUNET_OS_Process *cert_creation;
165 GNUNET_free_non_null (plugin->key);
167 GNUNET_free_non_null (plugin->cert);
171 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
172 "No usable TLS certificate found, creating certificate\n");
176 GNUNET_OS_start_process (NULL, NULL,
177 "gnunet-transport-certificate-creation",
178 "gnunet-transport-certificate-creation",
179 key_file, cert_file, NULL);
180 if (cert_creation == NULL)
182 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
184 ("Could not create a new TLS certificate, program `gnunet-transport-certificate-creation' could not be started!\n"));
185 GNUNET_free (key_file);
186 GNUNET_free (cert_file);
188 GNUNET_free_non_null (plugin->key);
190 GNUNET_free_non_null (plugin->cert);
192 GNUNET_free_non_null (plugin->crypto_init);
193 plugin->crypto_init = NULL;
195 return GNUNET_SYSERR;
197 GNUNET_assert (GNUNET_OK == GNUNET_OS_process_wait (cert_creation));
198 GNUNET_OS_process_close (cert_creation);
200 plugin->key = server_load_file (key_file);
201 plugin->cert = server_load_file (cert_file);
204 if ((plugin->key == NULL) || (plugin->cert == NULL))
206 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
208 ("No usable TLS certificate found and creating one failed!\n"),
210 GNUNET_free (key_file);
211 GNUNET_free (cert_file);
213 GNUNET_free_non_null (plugin->key);
215 GNUNET_free_non_null (plugin->cert);
217 GNUNET_free_non_null (plugin->crypto_init);
218 plugin->crypto_init = NULL;
220 return GNUNET_SYSERR;
222 GNUNET_free (key_file);
223 GNUNET_free (cert_file);
225 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "TLS certificate loaded\n");
234 * Reschedule the execution of both IPv4 and IPv6 server
235 * @param plugin the plugin
236 * @param now GNUNET_YES to schedule execution immediately, GNUNET_NO to wait
241 server_reschedule (struct Plugin *plugin, int now)
243 if (plugin->server_v4 != NULL)
245 if (plugin->server_v4_task != GNUNET_SCHEDULER_NO_TASK)
247 GNUNET_SCHEDULER_cancel(plugin->server_v4_task);
248 plugin->server_v4_task = GNUNET_SCHEDULER_NO_TASK;
250 plugin->server_v4_task = server_schedule (plugin, plugin->server_v4, now);
253 if (plugin->server_v6 != NULL)
255 if (plugin->server_v6_task != GNUNET_SCHEDULER_NO_TASK)
257 GNUNET_SCHEDULER_cancel(plugin->server_v6_task);
258 plugin->server_v6_task = GNUNET_SCHEDULER_NO_TASK;
260 plugin->server_v6_task = server_schedule (plugin, plugin->server_v6, now);
265 * Callback called by MessageStreamTokenizer when a message has arrived
266 * @param cls current session as closure
267 * @param client clien
268 * @param message the message to be forwarded to transport service
271 server_receive_mst_cb (void *cls, void *client,
272 const struct GNUNET_MessageHeader *message)
274 struct Session *s = cls;
276 struct Plugin *plugin = s->plugin;
278 struct GNUNET_TIME_Relative delay;
280 delay = http_plugin_receive (s, &s->target, message, s, s->addr, s->addrlen);
282 s->next_receive = GNUNET_TIME_absolute_add(GNUNET_TIME_absolute_get(), delay);
284 if (delay.rel_value > 0)
287 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name, "Server: peer `%s' address `%s' next read delayed for %llu ms\n",
288 GNUNET_i2s (&s->target), GNUNET_a2s (s->addr, s->addrlen), delay);
294 * Callback called by MHD when it needs data to send
295 * @param cls current session
296 * @param pos position in buffer
297 * @param buf the buffer to write data to
298 * @param max max number of bytes available in buffer
299 * @return bytes written to buffer
302 server_send_callback (void *cls, uint64_t pos, char *buf, size_t max)
304 struct Session *s = cls;
306 struct HTTP_Message *msg;
313 if ((msg->size - msg->pos) <= max)
315 memcpy (buf, &msg->buf[msg->pos], (msg->size - msg->pos));
316 bytes_read = msg->size - msg->pos;
317 msg->pos += (msg->size - msg->pos);
321 memcpy (buf, &msg->buf[msg->pos], max);
326 /* removing message */
327 if (msg->pos == msg->size)
329 if (NULL != msg->transmit_cont)
330 msg->transmit_cont (msg->transmit_cont_cls, &s->target, GNUNET_OK);
331 GNUNET_CONTAINER_DLL_remove(s->msg_head, s->msg_tail, msg);
337 struct Plugin *plugin = s->plugin;
338 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
339 "Server: %X: sent %u bytes\n",
345 static struct ServerConnection *
346 server_lookup_session (struct Plugin *plugin,
347 struct MHD_Connection *mhd_connection,
351 struct Session *s = NULL;
353 struct ServerConnection *sc = NULL;
354 const union MHD_ConnectionInfo *conn_info;
356 struct IPv4HttpAddress a4;
357 struct IPv6HttpAddress a6;
358 struct sockaddr_in * s4;
359 struct sockaddr_in6 * s6;
362 struct GNUNET_PeerIdentity target;
363 int check = GNUNET_NO;
367 conn_info = MHD_get_connection_info (mhd_connection, MHD_CONNECTION_INFO_CLIENT_ADDRESS);
368 if ((conn_info->client_addr->sa_family != AF_INET) && (conn_info->client_addr->sa_family != AF_INET6))
371 if ((strlen(&url[1]) >= 105) && (url[104] == ';'))
374 char * tagc = (char *) &url[105];
375 memcpy(&hash, &url[1], 103);
377 if (GNUNET_OK == GNUNET_CRYPTO_hash_from_string ((const char *) &hash,
378 &(target.hashPubKey)))
380 tag = strtoul (tagc, NULL, 10);
386 if (0 == strcmp (MHD_HTTP_METHOD_PUT, method))
387 direction = _RECEIVE;
388 if (0 == strcmp (MHD_HTTP_METHOD_GET, method))
391 if (check == GNUNET_NO)
394 plugin->cur_connections++;
397 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
398 "Server: New inbound connection from %s with tag %u\n", GNUNET_i2s(&target), tag);
400 /* find duplicate session */
406 if ((t->inbound) && (0 == memcmp (&t->target, &target, sizeof (struct GNUNET_PeerIdentity))) &&
407 /* FIXME add source address comparison */
415 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
416 "Server: Duplicate session, dismissing new connection from peer `%s'\n", GNUNET_i2s (&target));
421 /* find semi-session */
422 t = plugin->server_semi_head;
426 /* FIXME add source address comparison */
427 if ((0 == memcmp (&t->target, &target, sizeof (struct GNUNET_PeerIdentity))) &&
439 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
440 "Server: Found existing semi-session for `%s'\n", GNUNET_i2s (&target));
443 if ((direction == _SEND) && (t->server_send != NULL))
446 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
447 "Server: Duplicate GET 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));
463 if ((direction == _RECEIVE) && (t->server_recv != NULL))
466 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
467 "Server: Duplicate PUT session, dismissing new connection from peer `%s'\n", GNUNET_i2s (&target));
474 GNUNET_CONTAINER_DLL_remove(plugin->server_semi_head, plugin->server_semi_tail, s);
475 GNUNET_CONTAINER_DLL_insert(plugin->head, plugin->tail, s);
477 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
478 "Server: Found matching semi-session, merging session for peer `%s'\n", GNUNET_i2s (&target));
484 /* create new session */
486 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
487 "Server: Creating new session for peer `%s' \n", GNUNET_i2s (&target));
490 switch (conn_info->client_addr->sa_family)
493 s4 = ((struct sockaddr_in * ) conn_info->client_addr);
494 a4.u4_port = s4->sin_port;
495 memcpy (&a4.ipv4_addr, &s4->sin_addr,
496 sizeof (struct in_addr));
498 a_len = sizeof (struct IPv4HttpAddress);
501 s6 = ((struct sockaddr_in6 * ) conn_info->client_addr);
502 a6.u6_port = s6->sin6_port;
503 memcpy (&a6.ipv6_addr, &s6->sin6_addr,
504 sizeof (struct in6_addr));
506 a_len = sizeof (struct IPv6HttpAddress);
513 s = create_session (plugin,
520 s->inbound = GNUNET_YES;
521 s->next_receive = GNUNET_TIME_absolute_get_zero();
523 if (0 == strcmp (MHD_HTTP_METHOD_PUT, method))
525 if (0 == strcmp (MHD_HTTP_METHOD_GET, method))
527 GNUNET_CONTAINER_DLL_insert (plugin->server_semi_head, plugin->server_semi_tail, s);
532 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
533 "Server: Invalid connection request\n");
538 sc = GNUNET_malloc (sizeof (struct ServerConnection));
539 sc->mhd_conn = mhd_connection;
540 sc->direction = direction;
542 if (direction == _SEND)
544 if (direction == _RECEIVE)
547 #if MHD_VERSION >= 0x00090E00
548 int to = (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT.rel_value / 1000);
550 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
551 "Server: Setting timeout for %X to %u sec.\n", sc, to);
553 MHD_set_connection_option (mhd_connection, MHD_CONNECTION_OPTION_TIMEOUT, to);
554 server_reschedule (plugin, GNUNET_NO);
560 * Process GET or PUT request received via MHD. For
561 * GET, queue response that will send back our pending
562 * messages. For PUT, process incoming data and send
563 * to GNUnet core. In either case, check if a session
564 * already exists and create a new one if not.
567 server_access_cb (void *cls, struct MHD_Connection *mhd_connection,
568 const char *url, const char *method, const char *version,
569 const char *upload_data, size_t * upload_data_size,
570 void **httpSessionCache)
573 struct Plugin *plugin = cls;
574 struct ServerConnection *sc = *httpSessionCache;
575 struct Session *s = NULL;
578 struct MHD_Response *response;
580 GNUNET_assert (cls != NULL);
584 sc = server_lookup_session(plugin, mhd_connection, url, method);
586 (*httpSessionCache) = sc;
589 response = MHD_create_response_from_data (strlen (HTTP_ERROR_RESPONSE),HTTP_ERROR_RESPONSE, MHD_NO, MHD_NO);
590 res = MHD_queue_response (mhd_connection, MHD_HTTP_NOT_FOUND, response);
591 MHD_destroy_response (response);
596 /* existing connection */
597 sc = (*httpSessionCache);
600 /* connection is to be disconnected*/
601 if (sc->disconnect == GNUNET_YES)
603 response = MHD_create_response_from_data (strlen ("Thank you!"), "Thank you!", MHD_NO, MHD_NO);
604 res = MHD_queue_response (mhd_connection, MHD_HTTP_OK, response);
606 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
607 "Sent HTTP/1.1: 200 OK as PUT Response\n");
609 MHD_destroy_response (response);
613 GNUNET_assert (s != NULL);
614 if (sc->direction == _SEND)
616 response = MHD_create_response_from_callback (-1, 32 * 1024, &server_send_callback,
618 res = MHD_queue_response (mhd_connection, MHD_HTTP_OK, response);
619 MHD_destroy_response (response);
622 if (sc->direction == _RECEIVE)
624 if (*upload_data_size == 0)
627 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
628 "Server: Peer `%s' PUT on address `%s' connected\n",
629 GNUNET_i2s (&s->target), GNUNET_a2s (s->addr, s->addrlen));
635 if ((*upload_data_size > 0))
638 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
639 "Server: peer `%s' PUT on address `%s' received %Zu bytes\n",
640 GNUNET_i2s (&s->target), GNUNET_a2s (s->addr, s->addrlen), *upload_data_size);
642 struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get();
643 if (( s->next_receive.abs_value <= now.abs_value))
646 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
647 "Server: %X: PUT with %u bytes forwarded to MST\n", s,
650 if (s->msg_tk == NULL)
652 s->msg_tk = GNUNET_SERVER_mst_create (&server_receive_mst_cb, s);
654 res = GNUNET_SERVER_mst_receive (s->msg_tk, s, upload_data, *upload_data_size, GNUNET_NO, GNUNET_NO);
656 #if MHD_VERSION >= 0x00090E00
657 int to = (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT.rel_value / 1000);
658 struct ServerConnection *t = NULL;
661 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
662 "Server: Received %Zu bytes\n", *upload_data_size);
665 /* Setting timeouts for other connections */
666 if (s->server_recv != NULL)
670 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
671 "Server: Setting timeout for %X to %u sec.\n", t, to);
673 MHD_set_connection_option (t->mhd_conn, MHD_CONNECTION_OPTION_TIMEOUT, to);
675 if (s->server_send != NULL)
679 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
680 "Server: Setting timeout for %X to %u sec.\n", t, to);
682 MHD_set_connection_option (t->mhd_conn, MHD_CONNECTION_OPTION_TIMEOUT, to);
684 server_reschedule (plugin, GNUNET_NO);
686 (*upload_data_size) = 0;
691 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
692 "Server: %X no inbound bandwidth available! Next read was delayed by %llu ms\n", s, now.abs_value - s->next_receive.abs_value);
704 server_disconnect_cb (void *cls, struct MHD_Connection *connection,
705 void **httpSessionCache)
707 struct ServerConnection *sc = *httpSessionCache;
708 struct ServerConnection *tc = *httpSessionCache;
709 struct Session * s = NULL;
710 struct Session * t = NULL;
711 struct Plugin * plugin = NULL;
718 if (sc->direction == _SEND)
721 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
722 "Server: %X peer `%s' GET on address `%s' disconnected\n",
724 GNUNET_i2s (&s->target), GNUNET_a2s (s->addr, s->addrlen));
726 s->server_send = NULL;
728 if (s->server_recv != NULL)
731 tc->disconnect = GNUNET_YES;
732 #if MHD_VERSION >= 0x00090E00
733 MHD_set_connection_option (sc->mhd_conn, MHD_CONNECTION_OPTION_TIMEOUT, 1);
737 if (sc->direction == _RECEIVE)
740 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
741 "Server: %X peer `%s' PUT on address `%s' disconnected\n",
743 GNUNET_i2s (&s->target), GNUNET_a2s (s->addr, s->addrlen));
745 s->server_recv = NULL;
746 if (s->server_send != NULL)
749 tc->disconnect = GNUNET_YES;
750 #if MHD_VERSION >= 0x00090E00
751 MHD_set_connection_option (sc->mhd_conn, MHD_CONNECTION_OPTION_TIMEOUT, 1);
754 if (s->msg_tk != NULL)
756 GNUNET_SERVER_mst_destroy(s->msg_tk);
762 t = plugin->server_semi_head;
767 GNUNET_CONTAINER_DLL_remove(plugin->server_semi_head, plugin->server_semi_tail, s);
768 GNUNET_CONTAINER_DLL_insert(plugin->head, plugin->tail, s);
773 plugin->cur_connections--;
775 server_reschedule (plugin, GNUNET_NO);
777 if ((s->server_send == NULL) && (s->server_recv == NULL))
780 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
781 "Server: peer `%s' on address `%s' disconnected\n",
782 GNUNET_i2s (&s->target), GNUNET_a2s (s->addr, s->addrlen));
784 if (s->msg_tk != NULL)
786 GNUNET_SERVER_mst_destroy(s->msg_tk);
790 notify_session_end(s->plugin, &s->target, s);
795 server_disconnect (struct Session *s)
797 struct Plugin *plugin = s->plugin;
798 struct Session *t = plugin->head;
802 if (t->inbound == GNUNET_YES)
804 if (t->server_send != NULL)
806 ((struct ServerConnection *) t->server_send)->disconnect = GNUNET_YES;
808 if (t->server_send != NULL)
810 ((struct ServerConnection *) t->server_send)->disconnect = GNUNET_YES;
821 server_send (struct Session *s, struct HTTP_Message * msg)
823 GNUNET_CONTAINER_DLL_insert (s->msg_head, s->msg_tail, msg);
824 server_reschedule (s->plugin, GNUNET_YES);
831 * Call MHD IPv4 to process pending requests and then go back
832 * and schedule the next run.
833 * @param cls plugin as closure
834 * @param tc task context
837 server_v4_run (void *cls,
838 const struct GNUNET_SCHEDULER_TaskContext *tc)
840 struct Plugin *plugin = cls;
841 GNUNET_assert (cls != NULL);
843 plugin->server_v4_task = GNUNET_SCHEDULER_NO_TASK;
845 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
848 GNUNET_assert (MHD_YES == MHD_run (plugin->server_v4));
849 if (plugin->server_v4 != NULL)
850 plugin->server_v4_task = server_schedule (plugin, plugin->server_v4, GNUNET_NO);
855 * Call MHD IPv6 to process pending requests and then go back
856 * and schedule the next run.
857 * @param cls plugin as closure
858 * @param tc task context
861 server_v6_run (void *cls,
862 const struct GNUNET_SCHEDULER_TaskContext *tc)
864 struct Plugin *plugin = cls;
865 GNUNET_assert (cls != NULL);
867 plugin->server_v6_task = GNUNET_SCHEDULER_NO_TASK;
869 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
872 GNUNET_assert (MHD_YES == MHD_run (plugin->server_v6));
873 if (plugin->server_v6 != NULL)
874 plugin->server_v6_task = server_schedule (plugin, plugin->server_v6, GNUNET_NO);
878 * Function that queries MHD's select sets and
879 * starts the task waiting for them.
880 * @param plugin plugin
881 * @param daemon_handle the MHD daemon handle
882 * @return gnunet task identifier
884 static GNUNET_SCHEDULER_TaskIdentifier
885 server_schedule (struct Plugin *plugin, struct MHD_Daemon *daemon_handle, int now)
887 GNUNET_SCHEDULER_TaskIdentifier ret;
891 struct GNUNET_NETWORK_FDSet *wrs;
892 struct GNUNET_NETWORK_FDSet *wws;
893 struct GNUNET_NETWORK_FDSet *wes;
895 unsigned long long timeout;
896 static unsigned long long last_timeout = 0;
899 struct GNUNET_TIME_Relative tv;
901 ret = GNUNET_SCHEDULER_NO_TASK;
905 wrs = GNUNET_NETWORK_fdset_create ();
906 wes = GNUNET_NETWORK_fdset_create ();
907 wws = GNUNET_NETWORK_fdset_create ();
909 GNUNET_assert (MHD_YES == MHD_get_fdset (daemon_handle, &rs, &ws, &es, &max));
910 haveto = MHD_get_timeout (daemon_handle, &timeout);
911 if (haveto == MHD_YES)
913 if (timeout != last_timeout)
916 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
917 "SELECT Timeout changed from %llu to %llu\n",
918 last_timeout, timeout);
920 last_timeout = timeout;
922 tv.rel_value = (uint64_t) timeout;
925 tv = GNUNET_TIME_UNIT_SECONDS;
926 /* Force immediate run, since we have outbound data to send */
927 if (now == GNUNET_YES)
928 tv = GNUNET_TIME_UNIT_MILLISECONDS;
929 GNUNET_NETWORK_fdset_copy_native (wrs, &rs, max + 1);
930 GNUNET_NETWORK_fdset_copy_native (wws, &ws, max + 1);
931 GNUNET_NETWORK_fdset_copy_native (wes, &es, max + 1);
932 if (daemon_handle == plugin->server_v4)
934 if (plugin->server_v4_task != GNUNET_SCHEDULER_NO_TASK)
936 GNUNET_SCHEDULER_cancel (plugin->server_v4_task);
937 plugin->server_v4_task = GNUNET_SCHEDULER_NO_TASK;
941 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
942 GNUNET_SCHEDULER_NO_TASK, tv, wrs, wws,
943 &server_v4_run, plugin);
945 if (daemon_handle == plugin->server_v6)
947 if (plugin->server_v6_task != GNUNET_SCHEDULER_NO_TASK)
949 GNUNET_SCHEDULER_cancel (plugin->server_v6_task);
950 plugin->server_v6_task = GNUNET_SCHEDULER_NO_TASK;
954 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
955 GNUNET_SCHEDULER_NO_TASK, tv, wrs, wws,
956 &server_v6_run, plugin);
958 GNUNET_NETWORK_fdset_destroy (wrs);
959 GNUNET_NETWORK_fdset_destroy (wws);
960 GNUNET_NETWORK_fdset_destroy (wes);
965 server_start (struct Plugin *plugin)
968 unsigned int timeout;
971 res = server_load_certificate (plugin);
972 if (res == GNUNET_SYSERR)
974 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
975 "Could not load or create server certificate! Loading plugin failed!\n");
981 #if MHD_VERSION >= 0x00090E00
982 timeout = GNUNET_CONSTANTS_DISCONNECT_SESSION_TIMEOUT.rel_value / 1000;
983 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
984 "MHD can set timeout per connection! Default time out %u sec.\n", timeout);
986 timeout = GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT.rel_value / 1000;
987 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, plugin->name,
988 "MHD cannot set timeout per connection! Default time out %u sec.\n", timeout);
990 plugin->server_v4 = NULL;
991 if (plugin->ipv4 == GNUNET_YES)
993 plugin->server_v4 = MHD_start_daemon (
1000 MHD_NO_FLAG, plugin->port,
1001 &server_accept_cb, plugin,
1002 &server_access_cb, plugin,
1003 MHD_OPTION_SOCK_ADDR,
1004 (struct sockaddr_in *)
1005 plugin->server_addr_v4,
1006 MHD_OPTION_CONNECTION_LIMIT,
1008 plugin->max_connections,
1010 MHD_OPTION_HTTPS_PRIORITIES,
1011 plugin->crypto_init,
1012 MHD_OPTION_HTTPS_MEM_KEY,
1014 MHD_OPTION_HTTPS_MEM_CERT,
1017 MHD_OPTION_CONNECTION_TIMEOUT,
1019 MHD_OPTION_CONNECTION_MEMORY_LIMIT,
1021 GNUNET_SERVER_MAX_MESSAGE_SIZE),
1022 MHD_OPTION_NOTIFY_COMPLETED,
1023 &server_disconnect_cb, plugin,
1024 MHD_OPTION_EXTERNAL_LOGGER,
1025 server_log, NULL, MHD_OPTION_END);
1027 plugin->server_v6 = NULL;
1028 if (plugin->ipv6 == GNUNET_YES)
1030 plugin->server_v6 = MHD_start_daemon (
1037 MHD_USE_IPv6, plugin->port,
1038 &server_accept_cb, plugin,
1039 &server_access_cb, plugin,
1040 MHD_OPTION_SOCK_ADDR,
1041 (struct sockaddr_in6 *)
1042 plugin->server_addr_v6,
1043 MHD_OPTION_CONNECTION_LIMIT,
1045 plugin->max_connections,
1047 MHD_OPTION_HTTPS_PRIORITIES,
1048 plugin->crypto_init,
1049 MHD_OPTION_HTTPS_MEM_KEY,
1051 MHD_OPTION_HTTPS_MEM_CERT,
1054 MHD_OPTION_CONNECTION_TIMEOUT,
1056 MHD_OPTION_CONNECTION_MEMORY_LIMIT,
1058 GNUNET_SERVER_MAX_MESSAGE_SIZE),
1059 MHD_OPTION_NOTIFY_COMPLETED,
1060 &server_disconnect_cb, plugin,
1061 MHD_OPTION_EXTERNAL_LOGGER,
1062 server_log, NULL, MHD_OPTION_END);
1066 if ((plugin->ipv4 == GNUNET_YES) && (plugin->server_v4 == NULL))
1068 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
1069 "Failed to start %s IPv4 server component on port %u\n", plugin->name,
1071 return GNUNET_SYSERR;
1073 if ((plugin->ipv6 == GNUNET_YES) && (plugin->server_v6 == NULL))
1075 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
1076 "Failed to start %s IPv6 server component on port %u\n", plugin->name,
1078 return GNUNET_SYSERR;
1081 server_reschedule (plugin, GNUNET_NO);
1084 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1085 "%s server component started on port %u\n", plugin->name,
1092 server_stop (struct Plugin *plugin)
1094 struct Session *s = NULL;
1095 struct Session *t = NULL;
1097 struct MHD_Daemon *server_v4_tmp = plugin->server_v4;
1098 plugin->server_v4 = NULL;
1099 struct MHD_Daemon *server_v6_tmp = plugin->server_v6;
1100 plugin->server_v6 = NULL;
1102 if (plugin->server_v4_task != GNUNET_SCHEDULER_NO_TASK)
1104 GNUNET_SCHEDULER_cancel (plugin->server_v4_task);
1105 plugin->server_v4_task = GNUNET_SCHEDULER_NO_TASK;
1108 if (plugin->server_v6_task != GNUNET_SCHEDULER_NO_TASK)
1110 GNUNET_SCHEDULER_cancel (plugin->server_v6_task);
1111 plugin->server_v6_task = GNUNET_SCHEDULER_NO_TASK;
1114 if (server_v6_tmp != NULL)
1116 MHD_stop_daemon (server_v4_tmp);
1118 if (server_v6_tmp != NULL)
1120 MHD_stop_daemon (server_v6_tmp);
1123 /* cleaning up semi-sessions never propagated */
1124 s = plugin->server_semi_head;
1133 GNUNET_free_non_null (plugin->crypto_init);
1134 GNUNET_free_non_null (plugin->cert);
1135 GNUNET_free_non_null (plugin->key);
1139 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1140 "%s server component stopped\n", plugin->name);
1146 /* end of plugin_transport_http.c */