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 */
133 GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg, plugin->name,
134 "CRYPTO_INIT", &plugin->crypto_init);
137 GNUNET_CONFIGURATION_get_value_filename (plugin->env->cfg, plugin->name,
138 "KEY_FILE", &key_file))
140 key_file = "https_key.key";
144 GNUNET_CONFIGURATION_get_value_filename (plugin->env->cfg, plugin->name,
145 "CERT_FILE", &cert_file))
147 cert_file = "https_cert.crt";
150 /* read key & certificates from file */
152 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
153 "Loading TLS certificate from key-file `%s' cert-file`%s'\n",
154 key_file, cert_file);
157 plugin->key = server_load_file (key_file);
158 plugin->cert = server_load_file (cert_file);
160 if ((plugin->key == NULL) || (plugin->cert == NULL))
162 struct GNUNET_OS_Process *cert_creation;
164 GNUNET_free_non_null (plugin->key);
166 GNUNET_free_non_null (plugin->cert);
170 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
171 "No usable TLS certificate found, creating certificate\n");
175 GNUNET_OS_start_process (NULL, NULL,
176 "gnunet-transport-certificate-creation",
177 "gnunet-transport-certificate-creation",
178 key_file, cert_file, NULL);
179 if (cert_creation == NULL)
181 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
183 ("Could not create a new TLS certificate, program `gnunet-transport-certificate-creation' could not be started!\n"));
184 GNUNET_free (key_file);
185 GNUNET_free (cert_file);
187 GNUNET_free_non_null (plugin->key);
189 GNUNET_free_non_null (plugin->cert);
191 GNUNET_free_non_null (plugin->crypto_init);
192 plugin->crypto_init = NULL;
194 return GNUNET_SYSERR;
196 GNUNET_assert (GNUNET_OK == GNUNET_OS_process_wait (cert_creation));
197 GNUNET_OS_process_close (cert_creation);
199 plugin->key = server_load_file (key_file);
200 plugin->cert = server_load_file (cert_file);
203 if ((plugin->key == NULL) || (plugin->cert == NULL))
205 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
207 ("No usable TLS certificate found and creating one failed!\n"),
209 GNUNET_free (key_file);
210 GNUNET_free (cert_file);
212 GNUNET_free_non_null (plugin->key);
214 GNUNET_free_non_null (plugin->cert);
216 GNUNET_free_non_null (plugin->crypto_init);
217 plugin->crypto_init = NULL;
219 return GNUNET_SYSERR;
221 GNUNET_free (key_file);
222 GNUNET_free (cert_file);
224 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "TLS certificate loaded\n");
233 * Reschedule the execution of both IPv4 and IPv6 server
234 * @param plugin the plugin
235 * @param now GNUNET_YES to schedule execution immediately, GNUNET_NO to wait
240 server_reschedule (struct Plugin *plugin, int now)
242 if (plugin->server_v4 != NULL)
244 if (plugin->server_v4_task != GNUNET_SCHEDULER_NO_TASK)
246 GNUNET_SCHEDULER_cancel(plugin->server_v4_task);
247 plugin->server_v4_task = GNUNET_SCHEDULER_NO_TASK;
249 plugin->server_v4_task = server_schedule (plugin, plugin->server_v4, now);
252 if (plugin->server_v6 != NULL)
254 if (plugin->server_v6_task != GNUNET_SCHEDULER_NO_TASK)
256 GNUNET_SCHEDULER_cancel(plugin->server_v6_task);
257 plugin->server_v6_task = GNUNET_SCHEDULER_NO_TASK;
259 plugin->server_v6_task = server_schedule (plugin, plugin->server_v6, now);
264 * Callback called by MessageStreamTokenizer when a message has arrived
265 * @param cls current session as closure
266 * @param client clien
267 * @param message the message to be forwarded to transport service
270 server_receive_mst_cb (void *cls, void *client,
271 const struct GNUNET_MessageHeader *message)
273 struct Session *s = cls;
275 struct Plugin *plugin = s->plugin;
277 struct GNUNET_TIME_Relative delay;
279 delay = http_plugin_receive (s, &s->target, message, s, s->addr, s->addrlen);
281 s->next_receive = GNUNET_TIME_absolute_add(GNUNET_TIME_absolute_get(), delay);
283 if (delay.rel_value > 0)
286 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name, "Server: peer `%s' address `%s' next read delayed for %llu ms\n",
287 GNUNET_i2s (&s->target), GNUNET_a2s (s->addr, s->addrlen), delay);
293 * Callback called by MHD when it needs data to send
294 * @param cls current session
295 * @param pos position in buffer
296 * @param buf the buffer to write data to
297 * @param max max number of bytes available in buffer
298 * @return bytes written to buffer
301 server_send_callback (void *cls, uint64_t pos, char *buf, size_t max)
303 struct Session *s = cls;
305 struct HTTP_Message *msg;
312 if ((msg->size - msg->pos) <= max)
314 memcpy (buf, &msg->buf[msg->pos], (msg->size - msg->pos));
315 bytes_read = msg->size - msg->pos;
316 msg->pos += (msg->size - msg->pos);
320 memcpy (buf, &msg->buf[msg->pos], max);
325 /* removing message */
326 if (msg->pos == msg->size)
328 if (NULL != msg->transmit_cont)
329 msg->transmit_cont (msg->transmit_cont_cls, &s->target, GNUNET_OK);
330 GNUNET_CONTAINER_DLL_remove(s->msg_head, s->msg_tail, msg);
336 struct Plugin *plugin = s->plugin;
337 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
338 "Server: %X: sent %u bytes\n",
344 static struct ServerConnection *
345 server_lookup_session (struct Plugin *plugin,
346 struct MHD_Connection *mhd_connection,
350 struct Session *s = NULL;
352 struct ServerConnection *sc = NULL;
353 const union MHD_ConnectionInfo *conn_info;
355 struct GNUNET_PeerIdentity target;
357 int check = GNUNET_NO;
361 conn_info = MHD_get_connection_info (mhd_connection, MHD_CONNECTION_INFO_CLIENT_ADDRESS);
362 if (conn_info->client_addr->sa_family == AF_INET)
363 addrlen = sizeof (struct sockaddr_in);
364 else if (conn_info->client_addr->sa_family == AF_INET6)
365 addrlen = sizeof (struct sockaddr_in6);
369 if ((strlen(&url[1]) >= 105) && (url[104] == ';'))
372 char * tagc = (char *) &url[105];
373 memcpy(&hash, &url[1], 103);
375 if (GNUNET_OK == GNUNET_CRYPTO_hash_from_string ((const char *) &hash,
376 &(target.hashPubKey)))
378 tag = strtoul (tagc, NULL, 10);
384 if (0 == strcmp (MHD_HTTP_METHOD_PUT, method))
385 direction = _RECEIVE;
386 if (0 == strcmp (MHD_HTTP_METHOD_GET, method))
389 if (check == GNUNET_NO)
392 plugin->cur_connections++;
395 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
396 "Server: New inbound connection from %s with tag %u\n", GNUNET_i2s(&target), tag);
398 /* find duplicate session */
404 if ((t->inbound) && (0 == memcmp (&t->target, &target, sizeof (struct GNUNET_PeerIdentity))) &&
405 /* FIXME add source address comparison */
413 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
414 "Server: Duplicate session, dismissing new connection from peer `%s'\n", GNUNET_i2s (&target));
419 /* find semi-session */
420 t = plugin->server_semi_head;
424 /* FIXME add source address comparison */
425 if ((0 == memcmp (&t->target, &target, sizeof (struct GNUNET_PeerIdentity))) &&
437 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
438 "Server: Found existing semi-session for `%s'\n", GNUNET_i2s (&target));
441 if ((direction == _SEND) && (t->server_send != NULL))
444 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
445 "Server: Duplicate GET session, dismissing new connection from peer `%s'\n", GNUNET_i2s (&target));
452 GNUNET_CONTAINER_DLL_remove(plugin->server_semi_head, plugin->server_semi_tail, s);
453 GNUNET_CONTAINER_DLL_insert(plugin->head, plugin->tail, s);
455 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
456 "Server: Found matching semi-session, merging session for peer `%s'\n", GNUNET_i2s (&target));
461 if ((direction == _RECEIVE) && (t->server_recv != NULL))
464 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
465 "Server: Duplicate PUT session, dismissing new connection from peer `%s'\n", GNUNET_i2s (&target));
472 GNUNET_CONTAINER_DLL_remove(plugin->server_semi_head, plugin->server_semi_tail, s);
473 GNUNET_CONTAINER_DLL_insert(plugin->head, plugin->tail, s);
475 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
476 "Server: Found matching semi-session, merging session for peer `%s'\n", GNUNET_i2s (&target));
482 /* create new session */
484 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
485 "Server: Creating new session for peer `%s' \n", GNUNET_i2s (&target));
488 s = create_session (plugin,
490 conn_info->client_addr,
495 s->inbound = GNUNET_YES;
496 s->next_receive = GNUNET_TIME_absolute_get_zero();
498 if (0 == strcmp (MHD_HTTP_METHOD_PUT, method))
500 if (0 == strcmp (MHD_HTTP_METHOD_GET, method))
502 GNUNET_CONTAINER_DLL_insert (plugin->server_semi_head, plugin->server_semi_tail, s);
507 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
508 "Server: Invalid connection request\n");
513 sc = GNUNET_malloc (sizeof (struct ServerConnection));
514 sc->mhd_conn = mhd_connection;
515 sc->direction = direction;
517 if (direction == _SEND)
519 if (direction == _RECEIVE)
522 #if MHD_VERSION >= 0x00090E00
523 int to = (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT.rel_value / 1000);
525 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
526 "Server: Setting timeout for %X to %u sec.\n", sc, to);
528 MHD_set_connection_option (mhd_connection, MHD_CONNECTION_OPTION_TIMEOUT, to);
529 server_reschedule (plugin, GNUNET_NO);
535 * Process GET or PUT request received via MHD. For
536 * GET, queue response that will send back our pending
537 * messages. For PUT, process incoming data and send
538 * to GNUnet core. In either case, check if a session
539 * already exists and create a new one if not.
542 server_access_cb (void *cls, struct MHD_Connection *mhd_connection,
543 const char *url, const char *method, const char *version,
544 const char *upload_data, size_t * upload_data_size,
545 void **httpSessionCache)
548 struct Plugin *plugin = cls;
549 struct ServerConnection *sc = *httpSessionCache;
550 struct Session *s = NULL;
553 struct MHD_Response *response;
555 GNUNET_assert (cls != NULL);
559 sc = server_lookup_session(plugin, mhd_connection, url, method);
561 (*httpSessionCache) = sc;
564 response = MHD_create_response_from_data (strlen (HTTP_ERROR_RESPONSE),HTTP_ERROR_RESPONSE, MHD_NO, MHD_NO);
565 res = MHD_queue_response (mhd_connection, MHD_HTTP_NOT_FOUND, response);
566 MHD_destroy_response (response);
571 /* existing connection */
572 sc = (*httpSessionCache);
575 /* connection is to be disconnected*/
576 if (sc->disconnect == GNUNET_YES)
578 response = MHD_create_response_from_data (strlen ("Thank you!"), "Thank you!", MHD_NO, MHD_NO);
579 res = MHD_queue_response (mhd_connection, MHD_HTTP_OK, response);
581 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
582 "Sent HTTP/1.1: 200 OK as PUT Response\n");
584 MHD_destroy_response (response);
588 GNUNET_assert (s != NULL);
589 if (sc->direction == _SEND)
591 response = MHD_create_response_from_callback (-1, 32 * 1024, &server_send_callback,
593 res = MHD_queue_response (mhd_connection, MHD_HTTP_OK, response);
594 MHD_destroy_response (response);
597 if (sc->direction == _RECEIVE)
599 if (*upload_data_size == 0)
602 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
603 "Server: Peer `%s' PUT on address `%s' connected\n",
604 GNUNET_i2s (&s->target), GNUNET_a2s (s->addr, s->addrlen));
610 if ((*upload_data_size > 0))
613 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
614 "Server: peer `%s' PUT on address `%s' received %Zu bytes\n",
615 GNUNET_i2s (&s->target), GNUNET_a2s (s->addr, s->addrlen), *upload_data_size);
617 struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get();
618 if (( s->next_receive.abs_value <= now.abs_value))
621 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
622 "Server: %X: PUT with %u bytes forwarded to MST\n", s,
625 if (s->msg_tk == NULL)
627 s->msg_tk = GNUNET_SERVER_mst_create (&server_receive_mst_cb, s);
629 res = GNUNET_SERVER_mst_receive (s->msg_tk, s, upload_data, *upload_data_size, GNUNET_NO, GNUNET_NO);
631 #if MHD_VERSION >= 0x00090E00
632 int to = (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT.rel_value / 1000);
633 struct ServerConnection *t = NULL;
636 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
637 "Server: Received %Zu bytes\n", *upload_data_size);
640 /* Setting timeouts for other connections */
641 if (s->server_recv != NULL)
645 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
646 "Server: Setting timeout for %X to %u sec.\n", t, to);
648 MHD_set_connection_option (t->mhd_conn, MHD_CONNECTION_OPTION_TIMEOUT, to);
650 if (s->server_send != NULL)
654 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
655 "Server: Setting timeout for %X to %u sec.\n", t, to);
657 MHD_set_connection_option (t->mhd_conn, MHD_CONNECTION_OPTION_TIMEOUT, to);
659 server_reschedule (plugin, GNUNET_NO);
661 (*upload_data_size) = 0;
666 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
667 "Server: %X no inbound bandwidth available! Next read was delayed by %llu ms\n", s, now.abs_value - s->next_receive.abs_value);
679 server_disconnect_cb (void *cls, struct MHD_Connection *connection,
680 void **httpSessionCache)
682 struct ServerConnection *sc = *httpSessionCache;
683 struct ServerConnection *tc = *httpSessionCache;
684 struct Session * s = NULL;
685 struct Session * t = NULL;
686 struct Plugin * plugin = NULL;
693 if (sc->direction == _SEND)
696 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
697 "Server: %X peer `%s' GET on address `%s' disconnected\n",
699 GNUNET_i2s (&s->target), GNUNET_a2s (s->addr, s->addrlen));
701 s->server_send = NULL;
703 if (s->server_recv != NULL)
706 tc->disconnect = GNUNET_YES;
707 #if MHD_VERSION >= 0x00090E00
708 MHD_set_connection_option (sc->mhd_conn, MHD_CONNECTION_OPTION_TIMEOUT, 1);
712 if (sc->direction == _RECEIVE)
715 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
716 "Server: %X peer `%s' PUT on address `%s' disconnected\n",
718 GNUNET_i2s (&s->target), GNUNET_a2s (s->addr, s->addrlen));
720 s->server_recv = NULL;
721 if (s->server_send != NULL)
724 tc->disconnect = GNUNET_YES;
725 #if MHD_VERSION >= 0x00090E00
726 MHD_set_connection_option (sc->mhd_conn, MHD_CONNECTION_OPTION_TIMEOUT, 1);
729 if (s->msg_tk != NULL)
731 GNUNET_SERVER_mst_destroy(s->msg_tk);
737 t = plugin->server_semi_head;
742 GNUNET_CONTAINER_DLL_remove(plugin->server_semi_head, plugin->server_semi_tail, s);
743 GNUNET_CONTAINER_DLL_insert(plugin->head, plugin->tail, s);
748 plugin->cur_connections--;
750 server_reschedule (plugin, GNUNET_NO);
752 if ((s->server_send == NULL) && (s->server_recv == NULL))
755 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
756 "Server: peer `%s' on address `%s' disconnected\n",
757 GNUNET_i2s (&s->target), GNUNET_a2s (s->addr, s->addrlen));
759 if (s->msg_tk != NULL)
761 GNUNET_SERVER_mst_destroy(s->msg_tk);
765 notify_session_end(s->plugin, &s->target, s);
770 server_disconnect (struct Session *s)
772 struct Plugin *plugin = s->plugin;
773 struct Session *t = plugin->head;
777 if (t->inbound == GNUNET_YES)
779 if (t->server_send != NULL)
781 ((struct ServerConnection *) t->server_send)->disconnect = GNUNET_YES;
783 if (t->server_send != NULL)
785 ((struct ServerConnection *) t->server_send)->disconnect = GNUNET_YES;
796 server_send (struct Session *s, struct HTTP_Message * msg)
798 GNUNET_CONTAINER_DLL_insert (s->msg_head, s->msg_tail, msg);
799 server_reschedule (s->plugin, GNUNET_YES);
806 * Call MHD IPv4 to process pending requests and then go back
807 * and schedule the next run.
808 * @param cls plugin as closure
809 * @param tc task context
812 server_v4_run (void *cls,
813 const struct GNUNET_SCHEDULER_TaskContext *tc)
815 struct Plugin *plugin = cls;
816 GNUNET_assert (cls != NULL);
818 plugin->server_v4_task = GNUNET_SCHEDULER_NO_TASK;
820 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
823 GNUNET_assert (MHD_YES == MHD_run (plugin->server_v4));
824 plugin->server_v4_task = server_schedule (plugin, plugin->server_v4, GNUNET_NO);
829 * Call MHD IPv6 to process pending requests and then go back
830 * and schedule the next run.
831 * @param cls plugin as closure
832 * @param tc task context
835 server_v6_run (void *cls,
836 const struct GNUNET_SCHEDULER_TaskContext *tc)
838 struct Plugin *plugin = cls;
839 GNUNET_assert (cls != NULL);
841 plugin->server_v6_task = GNUNET_SCHEDULER_NO_TASK;
843 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
846 GNUNET_assert (MHD_YES == MHD_run (plugin->server_v6));
847 plugin->server_v6_task = server_schedule (plugin, plugin->server_v6, GNUNET_NO);
851 * Function that queries MHD's select sets and
852 * starts the task waiting for them.
853 * @param plugin plugin
854 * @param daemon_handle the MHD daemon handle
855 * @return gnunet task identifier
857 static GNUNET_SCHEDULER_TaskIdentifier
858 server_schedule (struct Plugin *plugin, struct MHD_Daemon *daemon_handle, int now)
860 GNUNET_SCHEDULER_TaskIdentifier ret;
864 struct GNUNET_NETWORK_FDSet *wrs;
865 struct GNUNET_NETWORK_FDSet *wws;
866 struct GNUNET_NETWORK_FDSet *wes;
868 unsigned long long timeout;
869 static unsigned long long last_timeout = 0;
872 struct GNUNET_TIME_Relative tv;
874 ret = GNUNET_SCHEDULER_NO_TASK;
878 wrs = GNUNET_NETWORK_fdset_create ();
879 wes = GNUNET_NETWORK_fdset_create ();
880 wws = GNUNET_NETWORK_fdset_create ();
882 GNUNET_assert (MHD_YES == MHD_get_fdset (daemon_handle, &rs, &ws, &es, &max));
883 haveto = MHD_get_timeout (daemon_handle, &timeout);
884 if (haveto == MHD_YES)
886 if (timeout != last_timeout)
889 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
890 "SELECT Timeout changed from %llu to %llu\n",
891 last_timeout, timeout);
893 last_timeout = timeout;
895 tv.rel_value = (uint64_t) timeout;
898 tv = GNUNET_TIME_UNIT_SECONDS;
899 /* Force immediate run, since we have outbound data to send */
900 if (now == GNUNET_YES)
901 tv = GNUNET_TIME_UNIT_MILLISECONDS;
902 GNUNET_NETWORK_fdset_copy_native (wrs, &rs, max + 1);
903 GNUNET_NETWORK_fdset_copy_native (wws, &ws, max + 1);
904 GNUNET_NETWORK_fdset_copy_native (wes, &es, max + 1);
905 if (daemon_handle == plugin->server_v4)
907 if (plugin->server_v4_task != GNUNET_SCHEDULER_NO_TASK)
909 GNUNET_SCHEDULER_cancel (plugin->server_v4_task);
910 plugin->server_v4_task = GNUNET_SCHEDULER_NO_TASK;
914 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
915 GNUNET_SCHEDULER_NO_TASK, tv, wrs, wws,
916 &server_v4_run, plugin);
918 if (daemon_handle == plugin->server_v6)
920 if (plugin->server_v6_task != GNUNET_SCHEDULER_NO_TASK)
922 GNUNET_SCHEDULER_cancel (plugin->server_v6_task);
923 plugin->server_v6_task = GNUNET_SCHEDULER_NO_TASK;
927 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
928 GNUNET_SCHEDULER_NO_TASK, tv, wrs, wws,
929 &server_v6_run, plugin);
931 GNUNET_NETWORK_fdset_destroy (wrs);
932 GNUNET_NETWORK_fdset_destroy (wws);
933 GNUNET_NETWORK_fdset_destroy (wes);
938 server_start (struct Plugin *plugin)
941 unsigned int timeout;
944 res = server_load_certificate (plugin);
945 if (res == GNUNET_SYSERR)
947 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
948 "Could not load or create server certificate! Loading plugin failed!\n");
954 #if MHD_VERSION >= 0x00090E00
955 timeout = GNUNET_CONSTANTS_DISCONNECT_SESSION_TIMEOUT.rel_value / 1000;
956 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
957 "MHD can set timeout per connection! Default time out %u sec.\n", timeout);
959 timeout = GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT.rel_value / 1000;
960 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
961 "MHD cannot set timeout per connection! Default time out %u sec.\n", timeout);
963 plugin->server_v4 = NULL;
964 if (plugin->ipv4 == GNUNET_YES)
966 plugin->server_v4 = MHD_start_daemon (
973 MHD_NO_FLAG, plugin->port,
974 &server_accept_cb, plugin,
975 &server_access_cb, plugin,
976 MHD_OPTION_SOCK_ADDR,
977 (struct sockaddr_in *)
978 plugin->server_addr_v4,
979 MHD_OPTION_CONNECTION_LIMIT,
981 plugin->max_connections,
983 MHD_OPTION_HTTPS_PRIORITIES,
985 MHD_OPTION_HTTPS_MEM_KEY,
987 MHD_OPTION_HTTPS_MEM_CERT,
990 MHD_OPTION_CONNECTION_TIMEOUT,
992 MHD_OPTION_CONNECTION_MEMORY_LIMIT,
994 GNUNET_SERVER_MAX_MESSAGE_SIZE),
995 MHD_OPTION_NOTIFY_COMPLETED,
996 &server_disconnect_cb, plugin,
997 MHD_OPTION_EXTERNAL_LOGGER,
998 server_log, NULL, MHD_OPTION_END);
999 if (plugin->server_v4 == NULL)
1000 res = GNUNET_SYSERR;
1002 plugin->server_v6 = NULL;
1003 if (plugin->ipv6 == GNUNET_YES)
1005 plugin->server_v6 = MHD_start_daemon (
1012 MHD_USE_IPv6, plugin->port,
1013 &server_accept_cb, plugin,
1014 &server_access_cb, plugin,
1015 MHD_OPTION_SOCK_ADDR,
1016 (struct sockaddr_in6 *)
1017 plugin->server_addr_v6,
1018 MHD_OPTION_CONNECTION_LIMIT,
1020 plugin->max_connections,
1022 MHD_OPTION_HTTPS_PRIORITIES,
1023 plugin->crypto_init,
1024 MHD_OPTION_HTTPS_MEM_KEY,
1026 MHD_OPTION_HTTPS_MEM_CERT,
1029 MHD_OPTION_CONNECTION_TIMEOUT,
1031 MHD_OPTION_CONNECTION_MEMORY_LIMIT,
1033 GNUNET_SERVER_MAX_MESSAGE_SIZE),
1034 MHD_OPTION_NOTIFY_COMPLETED,
1035 &server_disconnect_cb, plugin,
1036 MHD_OPTION_EXTERNAL_LOGGER,
1037 server_log, NULL, MHD_OPTION_END);
1039 if (plugin->server_v6 == NULL)
1040 res = GNUNET_SYSERR;
1043 server_reschedule (plugin, GNUNET_NO);
1046 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1047 "%s server component started on port %u\n", plugin->name,
1054 server_stop (struct Plugin *plugin)
1056 struct Session *s = NULL;
1057 struct Session *t = NULL;
1059 if (plugin->server_v4_task != GNUNET_SCHEDULER_NO_TASK)
1061 GNUNET_SCHEDULER_cancel (plugin->server_v4_task);
1062 plugin->server_v4_task = GNUNET_SCHEDULER_NO_TASK;
1065 if (plugin->server_v6_task != GNUNET_SCHEDULER_NO_TASK)
1067 GNUNET_SCHEDULER_cancel (plugin->server_v6_task);
1068 plugin->server_v6_task = GNUNET_SCHEDULER_NO_TASK;
1071 if (plugin->server_v4 != NULL)
1073 MHD_stop_daemon (plugin->server_v4);
1074 plugin->server_v4 = NULL;
1076 if (plugin->server_v6 != NULL)
1078 MHD_stop_daemon (plugin->server_v6);
1079 plugin->server_v6 = NULL;
1082 /* cleaning up semi-sessions never propagated */
1083 s = plugin->server_semi_head;
1092 GNUNET_free_non_null (plugin->crypto_init);
1093 GNUNET_free_non_null (plugin->cert);
1094 GNUNET_free_non_null (plugin->key);
1098 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1099 "%s server component stopped\n", plugin->name);
1105 /* end of plugin_transport_http.c */