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_server.c
23 * @brief HTTP/S server transport plugin
24 * @author Matthias Wachs
28 #include "gnunet_common.h"
29 #include "gnunet_server_lib.h"
30 #include "gnunet_statistics_service.h"
31 #include "gnunet_transport_plugin.h"
32 #include "gnunet_nat_lib.h"
33 #include "plugin_transport_http_common.h"
34 #include "microhttpd.h"
37 #define LIBGNUNET_PLUGIN_TRANSPORT_INIT libgnunet_plugin_transport_https_server_init
38 #define LIBGNUNET_PLUGIN_TRANSPORT_DONE libgnunet_plugin_transport_https_server_done
40 #define LIBGNUNET_PLUGIN_TRANSPORT_INIT libgnunet_plugin_transport_http_server_init
41 #define LIBGNUNET_PLUGIN_TRANSPORT_DONE libgnunet_plugin_transport_http_server_done
44 #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>"
49 * Encapsulation of all of the state of the plugin.
55 * Session handle for connections.
60 * Stored in a linked list.
65 * Stored in a linked list.
70 * To whom are we talking to (set to our identity
71 * if we are still waiting for the welcome message)
73 struct GNUNET_PeerIdentity target;
76 * Pointer to the global plugin struct.
78 struct HTTP_Server_Plugin *plugin;
81 * next pointer for double linked list
83 struct HTTP_Message *msg_head;
86 * previous pointer for double linked list
88 struct HTTP_Message *msg_tail;
91 * Message stream tokenizer for incoming data
93 struct GNUNET_SERVER_MessageStreamTokenizer *msg_tk;
98 struct ServerConnection *server_recv;
103 struct ServerConnection *server_send;
116 * Unique HTTP/S connection tag for this connection
121 * ATS network type in NBO
123 uint32_t ats_address_network_type;
126 * Was session given to transport service?
131 * Did we immediately end the session in disconnect_cb
136 * Absolute time when to receive data again
137 * Used for receive throttling
139 struct GNUNET_TIME_Absolute next_receive;
142 * Session timeout task
144 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
148 struct ServerConnection
153 /* Should this connection get disconnected? GNUNET_YES/NO */
156 /* The session this server connection belongs to */
157 struct Session *session;
159 /* The MHD connection */
160 struct MHD_Connection *mhd_conn;
163 struct MHD_Daemon *mhd_daemon;
168 * Encapsulation of all of the state of the plugin.
170 struct HTTP_Server_Plugin
175 struct GNUNET_TRANSPORT_PluginEnvironment *env;
178 * Linked list head of open sessions.
181 struct Session *head;
184 * Linked list tail of open sessions.
186 struct Session *tail;
201 char *external_hostname;
204 * Maximum number of sockets the plugin can use
205 * Each http inbound /outbound connections are two connections
207 unsigned int max_connections;
210 * Current number of sockets the plugin can use
211 * Each http inbound /outbound connections are two connections
213 unsigned int cur_connections;
216 * External hostname the plugin can be connected to, can be different to
217 * the host's FQDN, used e.g. for reverse proxying
222 * Notify transport only about external address
224 unsigned int external_only;
227 * External address length
247 * Task calling transport service about external address
249 GNUNET_SCHEDULER_TaskIdentifier notify_ext_task;
252 * NAT handle & address management
254 struct GNUNET_NAT_Handle *nat;
257 * List of own addresses
261 * IPv4 addresses DLL head
263 struct HttpAddressWrapper *addr_head;
266 * IPv4 addresses DLL tail
268 struct HttpAddressWrapper *addr_tail;
271 * IPv4 server socket to bind to
273 struct sockaddr_in *server_addr_v4;
276 * IPv6 server socket to bind to
278 struct sockaddr_in6 *server_addr_v6;
283 GNUNET_SCHEDULER_TaskIdentifier server_v4_task;
288 GNUNET_SCHEDULER_TaskIdentifier server_v6_task;
291 * The IPv4 server is scheduled to run asap
293 int server_v4_immediately;
296 * The IPv6 server is scheduled to run asap
298 int server_v6_immediately;
303 struct MHD_Daemon *server_v4;
308 struct MHD_Daemon *server_v6;
316 * Use RC4-128 instead of AES:
317 * NONE:+VERS-TLS1.0:+ARCFOUR-128:+SHA1:+RSA:+COMP-NULL
337 * Wrapper to manage addresses
339 struct HttpAddressWrapper
344 struct HttpAddressWrapper *next;
347 * Linked list previous
349 struct HttpAddressWrapper *prev;
358 * Message to send using http
363 * next pointer for double linked list
365 struct HTTP_Message *next;
368 * previous pointer for double linked list
370 struct HTTP_Message *prev;
373 * buffer containing data to send
378 * amount of data already sent
388 * Continuation function to call once the transmission buffer
389 * has again space available. NULL if there is no
390 * continuation to call.
392 GNUNET_TRANSPORT_TransmitContinuation transmit_cont;
395 * Closure for transmit_cont.
397 void *transmit_cont_cls;
402 * The http_server plugin handle
404 static struct HTTP_Server_Plugin * p;
408 * Start session timeout for session s
409 * @param s the session
412 server_start_session_timeout (struct Session *s);
416 * Increment session timeout due to activity for session s
417 * @param s the session
420 server_reschedule_session_timeout (struct Session *s);
424 * Cancel timeout for session s
425 * @param s the session
428 server_stop_session_timeout (struct Session *s);
432 * Disconnect a session s
433 * @param s the session
436 server_disconnect (struct Session *s);
440 * Does session s exist?
442 * @param plugin the plugin handle
443 * @param s the session
444 * @return GNUNET_YES on success, GNUNET_NO on error
447 server_exist_session (struct HTTP_Server_Plugin *plugin, struct Session *s);
451 * Reschedule the execution of both IPv4 and IPv6 server
452 * @param plugin the plugin
453 * @param server which server to schedule v4 or v6?
454 * @param now GNUNET_YES to schedule execution immediately, GNUNET_NO to wait
458 server_reschedule (struct HTTP_Server_Plugin *plugin, struct MHD_Daemon *server,
463 * Function that can be used by the transport service to transmit
464 * a message using the plugin. Note that in the case of a
465 * peer disconnecting, the continuation MUST be called
466 * prior to the disconnect notification itself. This function
467 * will be called with this peer's HELLO message to initiate
468 * a fresh connection to another peer.
471 * @param session which session must be used
472 * @param msgbuf the message to transmit
473 * @param msgbuf_size number of bytes in 'msgbuf'
474 * @param priority how important is the message (most plugins will
475 * ignore message priority and just FIFO)
476 * @param to how long to wait at most for the transmission (does not
477 * require plugins to discard the message after the timeout,
478 * just advisory for the desired delay; most plugins will ignore
480 * @param cont continuation to call once the message has
481 * been transmitted (or if the transport is ready
482 * for the next transmission call; or if the
483 * peer disconnected...); can be NULL
484 * @param cont_cls closure for cont
485 * @return number of bytes used (on the physical network, with overheads);
486 * -1 on hard errors (i.e. address invalid); 0 is a legal value
487 * and does NOT mean that the message was not transmitted (DV)
490 http_server_plugin_send (void *cls,
491 struct Session *session,
492 const char *msgbuf, size_t msgbuf_size,
493 unsigned int priority,
494 struct GNUNET_TIME_Relative to,
495 GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
497 struct HTTP_Server_Plugin *plugin = cls;
498 struct HTTP_Message *msg;
501 GNUNET_assert (plugin != NULL);
502 GNUNET_assert (session != NULL);
504 if (GNUNET_NO == server_exist_session (plugin, session))
507 return GNUNET_SYSERR;
509 if ((NULL == session->server_send) || (NULL == session->server_recv))
512 return GNUNET_SYSERR;
516 if (GNUNET_YES == session->server_send->disconnect)
517 return GNUNET_SYSERR;
519 /* create new message and schedule */
520 bytes_sent = sizeof (struct HTTP_Message) + msgbuf_size;
521 msg = GNUNET_malloc (bytes_sent);
523 msg->size = msgbuf_size;
525 msg->buf = (char *) &msg[1];
526 msg->transmit_cont = cont;
527 msg->transmit_cont_cls = cont_cls;
528 memcpy (msg->buf, msgbuf, msgbuf_size);
530 GNUNET_CONTAINER_DLL_insert_tail (session->msg_head, session->msg_tail, msg);
532 server_reschedule (session->plugin, session->server_send->mhd_daemon, GNUNET_YES);
533 server_reschedule_session_timeout (session);
535 /* struct Plugin *plugin = cls; */
542 * Function that can be used to force the plugin to disconnect
543 * from the given peer and cancel all previous transmissions
544 * (and their continuationc).
547 * @param target peer from which to disconnect
550 http_server_plugin_disconnect (void *cls, const struct GNUNET_PeerIdentity *target)
552 struct HTTP_Server_Plugin *plugin = cls;
553 struct Session *next = NULL;
554 struct Session *pos = NULL;
556 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
557 "Transport tells me to disconnect `%s'\n",
558 GNUNET_i2s (target));
561 while (NULL != (pos = next))
564 if (0 == memcmp (target, &pos->target, sizeof (struct GNUNET_PeerIdentity)))
566 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
567 "Disconnecting session %p to `%s'\n",
568 pos, GNUNET_i2s (target));
569 GNUNET_assert (GNUNET_OK == server_disconnect (pos));
577 * Another peer has suggested an address for this
578 * peer and transport plugin. Check that this could be a valid
579 * address. If so, consider adding it to the list
583 * @param addr pointer to the address
584 * @param addrlen length of addr
585 * @return GNUNET_OK if this is a plausible address for this peer
589 http_server_plugin_address_suggested (void *cls, const void *addr,
592 struct HTTP_Server_Plugin *plugin = cls;
593 struct HttpAddressWrapper *next;
594 struct HttpAddressWrapper *pos;
597 if ((NULL != plugin->ext_addr) &&
598 GNUNET_YES == (http_common_cmp_addresses (addr, addrlen,
599 plugin->ext_addr, plugin->ext_addr_len)))
602 next = plugin->addr_head;
603 while (NULL != (pos = next))
606 if (GNUNET_YES == (http_common_cmp_addresses(addr,
619 * Creates a new outbound session the transport
620 * service will use to send data to the peer
622 * Since HTTP/S server cannot create sessions, always return NULL
624 * @param cls the plugin
625 * @param address the address
626 * @return always NULL
628 static struct Session *
629 http_server_plugin_get_session (void *cls,
630 const struct GNUNET_HELLO_Address *address)
637 * Deleting the session
638 * Must not be used afterwards
640 * @param s the session to delete
644 server_delete_session (struct Session *s)
646 struct HTTP_Server_Plugin *plugin = s->plugin;
647 server_stop_session_timeout(s);
649 if ((GNUNET_YES == s->session_passed) && (GNUNET_NO == s->session_ended))
650 plugin->env->session_end (plugin->env->cls, &s->target, s);
652 GNUNET_CONTAINER_DLL_remove (plugin->head, plugin->tail, s);
653 struct HTTP_Message *msg = s->msg_head;
654 struct HTTP_Message *tmp = NULL;
660 GNUNET_CONTAINER_DLL_remove (s->msg_head, s->msg_tail, msg);
661 if (msg->transmit_cont != NULL)
663 msg->transmit_cont (msg->transmit_cont_cls, &s->target, GNUNET_SYSERR);
669 if (s->msg_tk != NULL)
671 GNUNET_SERVER_mst_destroy (s->msg_tk);
674 GNUNET_free (s->addr);
675 GNUNET_free_non_null (s->server_recv);
676 GNUNET_free_non_null (s->server_send);
679 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
680 "Session %p destroyed\n", s);
685 * Cancel timeout for session s
687 * @param s the session
690 server_stop_session_timeout (struct Session *s)
692 GNUNET_assert (NULL != s);
694 if (GNUNET_SCHEDULER_NO_TASK != s->timeout_task)
696 GNUNET_SCHEDULER_cancel (s->timeout_task);
697 s->timeout_task = GNUNET_SCHEDULER_NO_TASK;
698 GNUNET_log (TIMEOUT_LOG, "Timeout stopped for session %p\n", s);
704 * Function that queries MHD's select sets and
705 * starts the task waiting for them.
706 * @param plugin plugin
707 * @param daemon_handle the MHD daemon handle
708 * @param now schedule immediately
709 * @return gnunet task identifier
711 static GNUNET_SCHEDULER_TaskIdentifier
712 server_schedule (struct HTTP_Server_Plugin *plugin,
713 struct MHD_Daemon *daemon_handle,
718 * Reschedule the execution of both IPv4 and IPv6 server
719 * @param plugin the plugin
720 * @param server which server to schedule v4 or v6?
721 * @param now GNUNET_YES to schedule execution immediately, GNUNET_NO to wait
725 server_reschedule (struct HTTP_Server_Plugin *plugin, struct MHD_Daemon *server,
728 if ((server == plugin->server_v4) && (plugin->server_v4 != NULL))
730 if (GNUNET_YES == plugin->server_v4_immediately)
731 return; /* No rescheduling, server will run asap */
733 if (GNUNET_YES == now)
734 plugin->server_v4_immediately = GNUNET_YES;
736 if (plugin->server_v4_task != GNUNET_SCHEDULER_NO_TASK)
738 GNUNET_SCHEDULER_cancel (plugin->server_v4_task);
739 plugin->server_v4_task = GNUNET_SCHEDULER_NO_TASK;
741 plugin->server_v4_task = server_schedule (plugin, plugin->server_v4, now);
744 if ((server == plugin->server_v6) && (plugin->server_v6 != NULL))
746 if (GNUNET_YES == plugin->server_v6_immediately)
747 return; /* No rescheduling, server will run asap */
749 if (GNUNET_YES == now)
750 plugin->server_v6_immediately = GNUNET_YES;
752 if (plugin->server_v6_task != GNUNET_SCHEDULER_NO_TASK)
754 GNUNET_SCHEDULER_cancel (plugin->server_v6_task);
755 plugin->server_v6_task = GNUNET_SCHEDULER_NO_TASK;
757 plugin->server_v6_task = server_schedule (plugin, plugin->server_v6, now);
763 * Disconnect session s
765 * @param s the session
766 * @return GNUNET_OK on success
769 server_disconnect (struct Session *s)
771 struct ServerConnection * send = NULL;
772 struct ServerConnection * recv = NULL;
774 send = (struct ServerConnection *) s->server_send;
775 if (s->server_send != NULL)
777 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, s->plugin->name,
778 "Server: %p / %p Terminating inbound PUT session to peer `%s'\n",
779 s, s->server_send, GNUNET_i2s (&s->target));
781 send->disconnect = GNUNET_YES;
782 #if MHD_VERSION >= 0x00090E00
783 MHD_set_connection_option (send->mhd_conn, MHD_CONNECTION_OPTION_TIMEOUT,
786 server_reschedule (s->plugin, send->mhd_daemon, GNUNET_YES);
789 recv = (struct ServerConnection *) s->server_recv;
792 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, s->plugin->name,
793 "Server: %p / %p Terminating inbound GET session to peer `%s'\n",
794 s, s->server_recv, GNUNET_i2s (&s->target));
796 recv->disconnect = GNUNET_YES;
797 #if MHD_VERSION >= 0x00090E00
798 MHD_set_connection_option (recv->mhd_conn, MHD_CONNECTION_OPTION_TIMEOUT,
801 server_reschedule (s->plugin, recv->mhd_daemon, GNUNET_YES);
807 server_mhd_connection_timeout (struct HTTP_Server_Plugin *plugin, struct Session *s, int to)
809 #if MHD_VERSION >= 0x00090E00
810 /* Setting timeouts for other connections */
811 if (s->server_recv != NULL)
813 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
814 "Setting timeout for %p to %u sec.\n", s->server_recv, to);
815 MHD_set_connection_option (s->server_recv->mhd_conn,
816 MHD_CONNECTION_OPTION_TIMEOUT,
818 server_reschedule (plugin, s->server_recv->mhd_daemon, GNUNET_NO);
820 if (s->server_send != NULL)
822 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
823 "Setting timeout for %p to %u sec.\n", s->server_send, to);
824 MHD_set_connection_option (s->server_send->mhd_conn,
825 MHD_CONNECTION_OPTION_TIMEOUT,
827 server_reschedule (plugin, s->server_send->mhd_daemon, GNUNET_NO);
834 * Lookup a mhd connection and create one if none is found
836 * @param plugin the plugin handle
837 * @param mhd_connection the incoming mhd_connection
838 * @param url incoming requested URL
839 * @param method PUT or GET
840 * @return the server connecetion
842 static struct ServerConnection *
843 server_lookup_connection (struct HTTP_Server_Plugin *plugin,
844 struct MHD_Connection *mhd_connection, const char *url,
847 struct Session *s = NULL;
848 struct ServerConnection *sc = NULL;
849 const union MHD_ConnectionInfo *conn_info;
850 struct GNUNET_ATS_Information ats;
855 struct GNUNET_PeerIdentity target;
857 int direction = GNUNET_SYSERR;
860 /* url parsing variables */
868 conn_info = MHD_get_connection_info (mhd_connection,
869 MHD_CONNECTION_INFO_CLIENT_ADDRESS);
870 if ((conn_info->client_addr->sa_family != AF_INET) &&
871 (conn_info->client_addr->sa_family != AF_INET6))
874 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
875 "New %s connection from %s\n", method, url);
877 * URL is valid if it is in the form [peerid[103];tag]*/
878 url_len = strlen (url);
879 url_end = (char *) &url[url_len];
884 goto error; /* too short */
886 hash_start = strrchr (url, '/');
887 if (NULL == hash_start)
890 goto error; /* '/' delimiter not found */
892 if (hash_start >= url_end)
895 goto error; /* mal formed */
899 hash_end = strrchr (hash_start, ';');
900 if (NULL == hash_end)
903 goto error; /* ';' delimiter not found */
906 if (hash_end >= url_end)
909 goto error; /* mal formed */
912 if (hash_start >= hash_end)
915 goto error; /* mal formed */
918 if ((strlen(hash_start) - strlen(hash_end)) != 103)
921 goto error; /* invalid hash length */
925 memcpy (hash, hash_start, 103);
927 if (GNUNET_OK != GNUNET_CRYPTO_hash_from_string ((const char *) hash, &(target.hashPubKey)))
930 goto error; /* mal formed */
933 if (hash_end >= url_end)
936 goto error; /* mal formed */
939 tag_start = &hash_end[1];
942 tag = strtoul (tag_start, &tag_end, 10);
946 goto error; /* mal formed */
951 goto error; /* mal formed */
953 if (tag_end != url_end)
956 goto error; /* mal formed */
959 if (0 == strcmp (MHD_HTTP_METHOD_PUT, method))
960 direction = _RECEIVE;
961 else if (0 == strcmp (MHD_HTTP_METHOD_GET, method))
968 plugin->cur_connections++;
969 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
970 "New %s connection from %s with tag %u (%u of %u)\n",
972 GNUNET_i2s (&target), tag,
973 plugin->cur_connections, plugin->max_connections);
975 /* find duplicate session */
979 if ((0 == memcmp (&s->target, &target, sizeof (struct GNUNET_PeerIdentity))) &&
986 if ((_RECEIVE == direction) && (NULL != s->server_recv))
988 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
989 "Duplicate PUT connection from `%s' tag %u, dismissing new connection\n",
990 GNUNET_i2s (&target),
994 if ((_SEND == direction) && (NULL != s->server_send))
996 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
997 "Duplicate GET connection from `%s' tag %u, dismissing new connection\n",
998 GNUNET_i2s (&target),
1005 /* create new session */
1006 switch (conn_info->client_addr->sa_family)
1009 addr = http_common_address_from_socket (plugin->protocol, conn_info->client_addr, sizeof (struct sockaddr_in));
1010 addr_len = http_common_address_get_size (addr);
1011 ats = plugin->env->get_address_type (plugin->env->cls, conn_info->client_addr, sizeof (struct sockaddr_in));
1014 addr = http_common_address_from_socket (plugin->protocol, conn_info->client_addr, sizeof (struct sockaddr_in6));
1015 addr_len = http_common_address_get_size (addr);
1016 ats = plugin->env->get_address_type (plugin->env->cls, conn_info->client_addr, sizeof (struct sockaddr_in6));
1023 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1024 "Creating new session for peer `%s' connecting from `%s'\n",
1025 GNUNET_i2s (&target),
1026 http_common_plugin_address_to_string (NULL, addr, addr_len));
1028 s = GNUNET_malloc (sizeof (struct Session));
1029 memcpy (&s->target, &target, sizeof (struct GNUNET_PeerIdentity));
1032 s->addrlen = addr_len;
1033 s->ats_address_network_type = ats.value;
1034 s->next_receive = GNUNET_TIME_UNIT_ZERO_ABS;
1036 s->server_recv = NULL;
1037 s->server_send = NULL;
1038 s->session_passed = GNUNET_NO;
1039 s->session_ended = GNUNET_NO;
1040 server_start_session_timeout(s);
1041 GNUNET_CONTAINER_DLL_insert (plugin->head, plugin->tail, s);
1044 sc = GNUNET_malloc (sizeof (struct ServerConnection));
1045 if (conn_info->client_addr->sa_family == AF_INET)
1046 sc->mhd_daemon = plugin->server_v4;
1047 if (conn_info->client_addr->sa_family == AF_INET6)
1048 sc->mhd_daemon = plugin->server_v6;
1049 sc->mhd_conn = mhd_connection;
1050 sc->direction = direction;
1052 if (direction == _SEND)
1053 s->server_send = sc;
1054 if (direction == _RECEIVE)
1055 s->server_recv = sc;
1057 #if MHD_VERSION >= 0x00090E00
1058 if ((NULL == s->server_recv) || (NULL == s->server_send))
1060 to = (HTTP_SERVER_NOT_VALIDATED_TIMEOUT.rel_value / 1000);
1061 MHD_set_connection_option (mhd_connection, MHD_CONNECTION_OPTION_TIMEOUT, to);
1062 server_reschedule (plugin, sc->mhd_daemon, GNUNET_NO);
1066 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1067 "Session %p for peer `%s' fully connected\n",
1068 s, GNUNET_i2s (&target));
1069 to = (SERVER_SESSION_TIMEOUT.rel_value / 1000);
1070 server_mhd_connection_timeout (plugin, s, to);
1073 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1074 "Setting timeout for %p to %u sec.\n", sc, to);
1078 /* Error condition */
1080 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1081 "Invalid connection request\n");
1087 * Lookup a session for a server connection
1089 * @param plugin the plugin
1090 * @param sc the server connection
1091 * @return the session found or NULL
1093 static struct Session *
1094 server_lookup_session (struct HTTP_Server_Plugin *plugin,
1095 struct ServerConnection * sc)
1099 for (s = plugin->head; NULL != s; s = s->next)
1100 if ((s->server_recv == sc) || (s->server_send == sc))
1106 server_exist_session (struct HTTP_Server_Plugin *plugin, struct Session *s)
1108 struct Session * head;
1110 GNUNET_assert (NULL != plugin);
1111 GNUNET_assert (NULL != s);
1113 for (head = plugin->head; head != NULL; head = head->next)
1123 * Callback called by MHD when it needs data to send
1125 * @param cls current session
1126 * @param pos position in buffer
1127 * @param buf the buffer to write data to
1128 * @param max max number of bytes available in buffer
1129 * @return bytes written to buffer
1132 server_send_callback (void *cls, uint64_t pos, char *buf, size_t max)
1134 struct Session *s = cls;
1135 ssize_t bytes_read = 0;
1136 struct HTTP_Message *msg;
1138 GNUNET_assert (NULL != p);
1139 if (GNUNET_NO == server_exist_session (p, s))
1145 bytes_read = GNUNET_MIN (msg->size - msg->pos,
1147 memcpy (buf, &msg->buf[msg->pos], bytes_read);
1148 msg->pos += bytes_read;
1150 /* removing message */
1151 if (msg->pos == msg->size)
1153 GNUNET_CONTAINER_DLL_remove (s->msg_head, s->msg_tail, msg);
1154 if (NULL != msg->transmit_cont)
1155 msg->transmit_cont (msg->transmit_cont_cls, &s->target, GNUNET_OK);
1159 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, s->plugin->name,
1160 "Sent %u bytes to peer `%s' with session %p \n", bytes_read, GNUNET_i2s (&s->target), s);
1169 * Callback called by MessageStreamTokenizer when a message has arrived
1171 * @param cls current session as closure
1172 * @param client client
1173 * @param message the message to be forwarded to transport service
1177 server_receive_mst_cb (void *cls, void *client,
1178 const struct GNUNET_MessageHeader *message)
1180 struct Session *s = cls;
1181 struct GNUNET_ATS_Information atsi[2];
1182 struct GNUNET_TIME_Relative delay;
1184 GNUNET_assert (NULL != p);
1185 if (GNUNET_NO == server_exist_session(p, s))
1188 struct HTTP_Server_Plugin *plugin = s->plugin;
1190 atsi[0].type = htonl (GNUNET_ATS_QUALITY_NET_DISTANCE);
1191 atsi[0].value = htonl (1);
1192 atsi[1].type = htonl (GNUNET_ATS_NETWORK_TYPE);
1193 atsi[1].value = s->ats_address_network_type;
1194 GNUNET_break (s->ats_address_network_type != ntohl (GNUNET_ATS_NET_UNSPECIFIED));
1196 delay = plugin->env->receive (plugin->env->cls,
1199 (const struct GNUNET_ATS_Information *) &atsi, 2,
1200 s, s->addr, s->addrlen);
1201 s->session_passed = GNUNET_YES;
1202 s->next_receive = GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (), delay);
1203 if (delay.rel_value > 0)
1205 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1206 "Peer `%s' address `%s' next read delayed for %llu ms\n",
1207 GNUNET_i2s (&s->target),
1208 http_common_plugin_address_to_string (NULL, s->addr, s->addrlen),
1211 server_reschedule_session_timeout (s);
1217 * MHD callback for a new incoming connection
1219 * @param cls the plugin handle
1220 * @param mhd_connection the mhd connection
1221 * @param url the requested URL
1222 * @param method GET or PUT
1223 * @param version HTTP version
1224 * @param upload_data upload data
1225 * @param upload_data_size sizeof upload data
1226 * @param httpSessionCache the session cache to remember the connection
1227 * @return MHD_YES if connection is accepted, MHD_NO on reject
1230 server_access_cb (void *cls, struct MHD_Connection *mhd_connection,
1231 const char *url, const char *method, const char *version,
1232 const char *upload_data, size_t * upload_data_size,
1233 void **httpSessionCache)
1235 struct HTTP_Server_Plugin *plugin = cls;
1238 struct ServerConnection *sc = *httpSessionCache;
1240 struct MHD_Response *response;
1242 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1243 _("Access from connection %p (%u of %u) for %s %s url `%s' \n"),
1245 plugin->cur_connections, plugin->max_connections,
1246 method, version, url);
1248 GNUNET_assert (cls != NULL);
1251 /* new connection */
1252 sc = server_lookup_connection (plugin, mhd_connection, url, method);
1254 (*httpSessionCache) = sc;
1257 response = MHD_create_response_from_data (strlen (HTTP_ERROR_RESPONSE), HTTP_ERROR_RESPONSE, MHD_NO, MHD_NO);
1258 res = MHD_queue_response (mhd_connection, MHD_HTTP_NOT_FOUND, response);
1259 MHD_destroy_response (response);
1265 /* 'old' connection */
1266 if (NULL == server_lookup_session (plugin, sc))
1268 /* Session was already disconnected */
1273 /* existing connection */
1274 sc = (*httpSessionCache);
1277 GNUNET_assert (NULL != s);
1278 /* connection is to be disconnected */
1279 if (sc->disconnect == GNUNET_YES)
1281 /* Sent HTTP/1.1: 200 OK as PUT Response\ */
1282 response = MHD_create_response_from_data (strlen ("Thank you!"),
1285 res = MHD_queue_response (mhd_connection, MHD_HTTP_OK, response);
1286 MHD_destroy_response (response);
1290 GNUNET_assert (s != NULL);
1291 /* Check if both directions are connected */
1292 if ((sc->session->server_recv == NULL) || (sc->session->server_send == NULL))
1294 /* Delayed read from since not both semi-connections are connected */
1298 if (sc->direction == _SEND)
1300 response = MHD_create_response_from_callback (MHD_SIZE_UNKNOWN,
1302 &server_send_callback, s,
1304 MHD_queue_response (mhd_connection, MHD_HTTP_OK, response);
1305 MHD_destroy_response (response);
1308 if (sc->direction == _RECEIVE)
1310 if (*upload_data_size == 0)
1312 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1313 "Peer `%s' PUT on address `%s' connected\n",
1314 GNUNET_i2s (&s->target),
1315 http_common_plugin_address_to_string (NULL,
1321 /* Receiving data */
1322 if ((*upload_data_size > 0))
1324 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1325 "Peer `%s' PUT on address `%s' received %u bytes\n",
1326 GNUNET_i2s (&s->target),
1327 http_common_plugin_address_to_string (NULL,
1331 struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get ();
1333 if ((s->next_receive.abs_value <= now.abs_value))
1335 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1336 "PUT with %u bytes forwarded to MST\n",
1338 if (s->msg_tk == NULL)
1340 s->msg_tk = GNUNET_SERVER_mst_create (&server_receive_mst_cb, s);
1342 GNUNET_SERVER_mst_receive (s->msg_tk, s, upload_data,
1343 *upload_data_size, GNUNET_NO, GNUNET_NO);
1344 #if MHD_VERSION >= 0x00090E00
1345 server_mhd_connection_timeout (plugin, s, GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT.rel_value / 1000);
1347 (*upload_data_size) = 0;
1351 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1352 "%p no inbound bandwidth available! Next read was delayed by %llu ms\n",
1353 s, now.abs_value - s->next_receive.abs_value);
1365 * Callback from MHD when a connection disconnects
1367 * @param cls closure
1368 * @param connection the disconnected MHD connection
1369 * @param httpSessionCache the pointer to distinguish
1372 server_disconnect_cb (void *cls, struct MHD_Connection *connection,
1373 void **httpSessionCache)
1375 struct ServerConnection *sc = *httpSessionCache;
1376 struct Session *s = NULL;
1377 struct Session *t = NULL;
1378 struct HTTP_Server_Plugin *plugin = NULL;
1383 if (NULL == (s = server_lookup_session (p, sc)))
1386 GNUNET_assert (NULL != p);
1387 for (t = p->head; t != NULL; t = t->next)
1396 if (sc->direction == _SEND)
1399 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1400 "Peer `%s' connection %p, GET on address `%s' disconnected\n",
1401 GNUNET_i2s (&s->target), s->server_send,
1402 http_common_plugin_address_to_string (NULL, s->addr, s->addrlen));
1403 s->server_send = NULL;
1404 if (NULL != (s->server_recv))
1406 s->server_recv->disconnect = GNUNET_YES;
1407 GNUNET_assert (NULL != s->server_recv->mhd_conn);
1408 #if MHD_VERSION >= 0x00090E00
1409 MHD_set_connection_option (s->server_recv->mhd_conn, MHD_CONNECTION_OPTION_TIMEOUT,
1412 server_reschedule (plugin, s->server_recv->mhd_daemon, GNUNET_NO);
1415 if (sc->direction == _RECEIVE)
1417 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1418 "Peer `%s' connection %p PUT on address `%s' disconnected\n",
1419 GNUNET_i2s (&s->target), s->server_recv,
1420 http_common_plugin_address_to_string (NULL, s->addr, s->addrlen));
1421 s->server_recv = NULL;
1422 if (NULL != (s->server_send))
1424 s->server_send->disconnect = GNUNET_YES;
1425 GNUNET_assert (NULL != s->server_send->mhd_conn);
1426 #if MHD_VERSION >= 0x00090E00
1427 MHD_set_connection_option (s->server_send->mhd_conn, MHD_CONNECTION_OPTION_TIMEOUT,
1430 server_reschedule (plugin, s->server_send->mhd_daemon, GNUNET_NO);
1432 if (s->msg_tk != NULL)
1434 GNUNET_SERVER_mst_destroy (s->msg_tk);
1440 plugin->cur_connections--;
1442 if ((s->server_send == NULL) && (s->server_recv == NULL))
1444 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1445 "Peer `%s' on address `%s' disconnected\n",
1446 GNUNET_i2s (&s->target),
1447 http_common_plugin_address_to_string (NULL, s->addr, s->addrlen));
1449 server_delete_session (s);
1451 else if ((GNUNET_YES == s->session_passed) && (GNUNET_NO == s->session_ended))
1453 /* Notify transport immediately that this session is invalid */
1454 s->session_ended = GNUNET_YES;
1455 plugin->env->session_end (plugin->env->cls, &s->target, s);
1461 * Check if incoming connection is accepted.
1463 * @param cls plugin as closure
1464 * @param addr address of incoming connection
1465 * @param addr_len address length of incoming connection
1466 * @return MHD_YES if connection is accepted, MHD_NO if connection is rejected
1470 server_accept_cb (void *cls, const struct sockaddr *addr, socklen_t addr_len)
1472 struct HTTP_Server_Plugin *plugin = cls;
1474 if (plugin->cur_connections <= plugin->max_connections)
1476 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1477 _("Accepting connection (%u of %u) from `%s'\n"),
1478 plugin->cur_connections, plugin->max_connections,
1479 GNUNET_a2s (addr, addr_len));
1484 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, plugin->name,
1485 _("Server reached maximum number connections (%u), rejecting new connection\n"),
1486 plugin->max_connections);
1492 server_log (void *arg, const char *fmt, va_list ap)
1496 vsnprintf (text, sizeof (text), fmt, ap);
1498 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Server: %s\n", text);
1503 * Call MHD IPv4 to process pending requests and then go back
1504 * and schedule the next run.
1505 * @param cls plugin as closure
1506 * @param tc task context
1509 server_v4_run (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1511 struct HTTP_Server_Plugin *plugin = cls;
1513 GNUNET_assert (cls != NULL);
1515 plugin->server_v4_task = GNUNET_SCHEDULER_NO_TASK;
1516 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1519 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1520 "Running IPv4 server\n");
1522 plugin->server_v4_immediately = GNUNET_NO;
1523 GNUNET_assert (MHD_YES == MHD_run (plugin->server_v4));
1524 server_reschedule (plugin, plugin->server_v4, GNUNET_NO);
1529 * Call MHD IPv6 to process pending requests and then go back
1530 * and schedule the next run.
1531 * @param cls plugin as closure
1532 * @param tc task context
1535 server_v6_run (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1537 struct HTTP_Server_Plugin *plugin = cls;
1539 GNUNET_assert (cls != NULL);
1540 plugin->server_v6_task = GNUNET_SCHEDULER_NO_TASK;
1541 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1544 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1545 "Running IPv6 server\n");
1547 plugin->server_v6_immediately = GNUNET_NO;
1548 GNUNET_assert (MHD_YES == MHD_run (plugin->server_v6));
1549 server_reschedule (plugin, plugin->server_v6, GNUNET_NO);
1554 * Function that queries MHD's select sets and
1555 * starts the task waiting for them.
1557 * @param plugin plugin
1558 * @param daemon_handle the MHD daemon handle
1559 * @return gnunet task identifier
1561 static GNUNET_SCHEDULER_TaskIdentifier
1562 server_schedule (struct HTTP_Server_Plugin *plugin,
1563 struct MHD_Daemon *daemon_handle,
1566 GNUNET_SCHEDULER_TaskIdentifier ret;
1570 struct GNUNET_NETWORK_FDSet *wrs;
1571 struct GNUNET_NETWORK_FDSet *wws;
1572 struct GNUNET_NETWORK_FDSet *wes;
1574 unsigned MHD_LONG_LONG timeout;
1575 static unsigned long long last_timeout = 0;
1578 struct GNUNET_TIME_Relative tv;
1580 ret = GNUNET_SCHEDULER_NO_TASK;
1584 wrs = GNUNET_NETWORK_fdset_create ();
1585 wes = GNUNET_NETWORK_fdset_create ();
1586 wws = GNUNET_NETWORK_fdset_create ();
1588 GNUNET_assert (MHD_YES == MHD_get_fdset (daemon_handle, &rs, &ws, &es, &max));
1589 haveto = MHD_get_timeout (daemon_handle, &timeout);
1590 if (haveto == MHD_YES)
1592 if (timeout != last_timeout)
1595 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1596 "SELECT Timeout changed from %llu to %llu\n",
1597 last_timeout, timeout);
1599 last_timeout = timeout;
1601 tv.rel_value = (uint64_t) timeout;
1604 tv = GNUNET_TIME_UNIT_SECONDS;
1605 /* Force immediate run, since we have outbound data to send */
1606 if (now == GNUNET_YES)
1607 tv = GNUNET_TIME_UNIT_MILLISECONDS;
1608 GNUNET_NETWORK_fdset_copy_native (wrs, &rs, max + 1);
1609 GNUNET_NETWORK_fdset_copy_native (wws, &ws, max + 1);
1610 GNUNET_NETWORK_fdset_copy_native (wes, &es, max + 1);
1612 if (daemon_handle == plugin->server_v4)
1614 if (plugin->server_v4_task != GNUNET_SCHEDULER_NO_TASK)
1616 GNUNET_SCHEDULER_cancel (plugin->server_v4_task);
1617 plugin->server_v4_task = GNUNET_SCHEDULER_NO_TASK;
1620 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1621 "Scheduling IPv4 server task in %llu ms\n", tv);
1624 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1626 &server_v4_run, plugin);
1628 if (daemon_handle == plugin->server_v6)
1630 if (plugin->server_v6_task != GNUNET_SCHEDULER_NO_TASK)
1632 GNUNET_SCHEDULER_cancel (plugin->server_v6_task);
1633 plugin->server_v6_task = GNUNET_SCHEDULER_NO_TASK;
1636 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1637 "Scheduling IPv6 server task in %llu ms\n", tv);
1640 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1642 &server_v6_run, plugin);
1644 GNUNET_NETWORK_fdset_destroy (wrs);
1645 GNUNET_NETWORK_fdset_destroy (wws);
1646 GNUNET_NETWORK_fdset_destroy (wes);
1653 * Load ssl certificate from file
1655 * @param file filename
1656 * @return content of the file
1659 server_load_file (const char *file)
1661 struct GNUNET_DISK_FileHandle *gn_file;
1665 if (GNUNET_OK != GNUNET_DISK_file_size (file,
1666 &fsize, GNUNET_NO, GNUNET_YES))
1668 text = GNUNET_malloc (fsize + 1);
1670 GNUNET_DISK_file_open (file, GNUNET_DISK_OPEN_READ,
1671 GNUNET_DISK_PERM_USER_READ);
1672 if (gn_file == NULL)
1677 if (GNUNET_SYSERR == GNUNET_DISK_file_read (gn_file, text, fsize))
1680 GNUNET_DISK_file_close (gn_file);
1684 GNUNET_DISK_file_close (gn_file);
1692 * Load ssl certificate
1694 * @param plugin the plugin
1695 * @return GNUNET_OK on success, GNUNET_SYSERR on failure
1698 server_load_certificate (struct HTTP_Server_Plugin *plugin)
1700 int res = GNUNET_OK;
1705 /* Get crypto init string from config
1706 * If not present just use default values */
1709 GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg,
1712 &plugin->crypto_init))
1713 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1714 "Using crypto init string `%s'\n",
1715 plugin->crypto_init);
1717 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1718 "Using default crypto init string \n");
1721 GNUNET_CONFIGURATION_get_value_filename (plugin->env->cfg, plugin->name,
1722 "KEY_FILE", &key_file))
1724 key_file = GNUNET_strdup ("https_key.key");
1728 GNUNET_CONFIGURATION_get_value_filename (plugin->env->cfg, plugin->name,
1729 "CERT_FILE", &cert_file))
1731 GNUNET_asprintf (&cert_file, "%s", "https_cert.crt");
1734 /* read key & certificates from file */
1735 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1736 "Trying to loading TLS certificate from key-file `%s' cert-file`%s'\n",
1737 key_file, cert_file);
1739 plugin->key = server_load_file (key_file);
1740 plugin->cert = server_load_file (cert_file);
1742 if ((plugin->key == NULL) || (plugin->cert == NULL))
1744 struct GNUNET_OS_Process *cert_creation;
1746 GNUNET_free_non_null (plugin->key);
1748 GNUNET_free_non_null (plugin->cert);
1749 plugin->cert = NULL;
1751 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1752 "No usable TLS certificate found, creating certificate\n");
1755 GNUNET_OS_start_process (GNUNET_NO, GNUNET_OS_INHERIT_STD_OUT_AND_ERR, NULL, NULL,
1756 "gnunet-transport-certificate-creation",
1757 "gnunet-transport-certificate-creation",
1758 key_file, cert_file, NULL);
1759 if (cert_creation == NULL)
1761 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
1763 ("Could not create a new TLS certificate, program `gnunet-transport-certificate-creation' could not be started!\n"));
1764 GNUNET_free (key_file);
1765 GNUNET_free (cert_file);
1767 GNUNET_free_non_null (plugin->key);
1769 GNUNET_free_non_null (plugin->cert);
1770 plugin->cert = NULL;
1771 GNUNET_free_non_null (plugin->crypto_init);
1772 plugin->crypto_init = NULL;
1774 return GNUNET_SYSERR;
1776 GNUNET_assert (GNUNET_OK == GNUNET_OS_process_wait (cert_creation));
1777 GNUNET_OS_process_destroy (cert_creation);
1779 plugin->key = server_load_file (key_file);
1780 plugin->cert = server_load_file (cert_file);
1783 if ((plugin->key == NULL) || (plugin->cert == NULL))
1785 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
1787 ("No usable TLS certificate found and creating one failed!\n"),
1789 GNUNET_free (key_file);
1790 GNUNET_free (cert_file);
1792 GNUNET_free_non_null (plugin->key);
1794 GNUNET_free_non_null (plugin->cert);
1795 plugin->cert = NULL;
1796 GNUNET_free_non_null (plugin->crypto_init);
1797 plugin->crypto_init = NULL;
1799 return GNUNET_SYSERR;
1801 GNUNET_free (key_file);
1802 GNUNET_free (cert_file);
1803 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "TLS certificate loaded\n");
1810 * Start the HTTP server
1812 * @param plugin the plugin handle
1813 * @return GNUNET_OK on success, GNUNET_SYSERR on failure
1816 server_start (struct HTTP_Server_Plugin *plugin)
1818 unsigned int timeout;
1819 GNUNET_assert (NULL != plugin);
1822 if (GNUNET_SYSERR == server_load_certificate (plugin))
1824 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
1825 "Could not load or create server certificate! Loading plugin failed!\n");
1826 return GNUNET_SYSERR;
1831 #if MHD_VERSION >= 0x00090E00
1832 timeout = HTTP_SERVER_NOT_VALIDATED_TIMEOUT.rel_value / 1000;
1833 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1834 "MHD can set timeout per connection! Default time out %u sec.\n",
1837 timeout = SERVER_SESSION_TIMEOUT.rel_value / 1000;
1838 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, plugin->name,
1839 "MHD cannot set timeout per connection! Default time out %u sec.\n",
1842 plugin->server_v4 = NULL;
1843 if (plugin->use_ipv4 == GNUNET_YES)
1845 plugin->server_v4 = MHD_start_daemon (
1852 MHD_NO_FLAG, plugin->port,
1853 &server_accept_cb, plugin,
1854 &server_access_cb, plugin,
1855 MHD_OPTION_SOCK_ADDR,
1856 (struct sockaddr_in *)
1857 plugin->server_addr_v4,
1858 MHD_OPTION_CONNECTION_LIMIT,
1860 plugin->max_connections,
1862 MHD_OPTION_HTTPS_PRIORITIES,
1863 plugin->crypto_init,
1864 MHD_OPTION_HTTPS_MEM_KEY,
1866 MHD_OPTION_HTTPS_MEM_CERT,
1869 MHD_OPTION_CONNECTION_TIMEOUT,
1871 MHD_OPTION_CONNECTION_MEMORY_LIMIT,
1873 GNUNET_SERVER_MAX_MESSAGE_SIZE),
1874 MHD_OPTION_NOTIFY_COMPLETED,
1875 &server_disconnect_cb, plugin,
1876 MHD_OPTION_EXTERNAL_LOGGER,
1877 server_log, NULL, MHD_OPTION_END);
1879 plugin->server_v6 = NULL;
1880 if (plugin->use_ipv6 == GNUNET_YES)
1882 plugin->server_v6 = MHD_start_daemon (
1889 MHD_USE_IPv6, plugin->port,
1890 &server_accept_cb, plugin,
1891 &server_access_cb, plugin,
1892 MHD_OPTION_SOCK_ADDR,
1893 (struct sockaddr_in6 *)
1894 plugin->server_addr_v6,
1895 MHD_OPTION_CONNECTION_LIMIT,
1897 plugin->max_connections,
1899 MHD_OPTION_HTTPS_PRIORITIES,
1900 plugin->crypto_init,
1901 MHD_OPTION_HTTPS_MEM_KEY,
1903 MHD_OPTION_HTTPS_MEM_CERT,
1906 MHD_OPTION_CONNECTION_TIMEOUT,
1908 MHD_OPTION_CONNECTION_MEMORY_LIMIT,
1910 GNUNET_SERVER_MAX_MESSAGE_SIZE),
1911 MHD_OPTION_NOTIFY_COMPLETED,
1912 &server_disconnect_cb, plugin,
1913 MHD_OPTION_EXTERNAL_LOGGER,
1914 server_log, NULL, MHD_OPTION_END);
1918 if ((plugin->use_ipv4 == GNUNET_YES) && (plugin->server_v4 == NULL))
1920 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
1921 "Failed to start %s IPv4 server component on port %u\n",
1922 plugin->name, plugin->port);
1923 return GNUNET_SYSERR;
1925 server_reschedule (plugin, plugin->server_v4, GNUNET_NO);
1927 if ((plugin->use_ipv6 == GNUNET_YES) && (plugin->server_v6 == NULL))
1929 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
1930 "Failed to start %s IPv6 server component on port %u\n",
1931 plugin->name, plugin->port);
1932 return GNUNET_SYSERR;
1934 server_reschedule (plugin, plugin->server_v6, GNUNET_NO);
1935 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1936 "%s server component started on port %u\n", plugin->name,
1943 server_stop (struct HTTP_Server_Plugin *plugin)
1945 if (plugin->server_v4 != NULL)
1947 MHD_stop_daemon (plugin->server_v4);
1948 plugin->server_v4 = NULL;
1950 if ( plugin->server_v6 != NULL)
1952 MHD_stop_daemon (plugin->server_v6);
1953 plugin->server_v6 = NULL;
1957 if (plugin->server_v4_task != GNUNET_SCHEDULER_NO_TASK)
1959 GNUNET_SCHEDULER_cancel (plugin->server_v4_task);
1960 plugin->server_v4_task = GNUNET_SCHEDULER_NO_TASK;
1963 if (plugin->server_v6_task != GNUNET_SCHEDULER_NO_TASK)
1965 GNUNET_SCHEDULER_cancel (plugin->server_v6_task);
1966 plugin->server_v6_task = GNUNET_SCHEDULER_NO_TASK;
1971 GNUNET_free_non_null (plugin->crypto_init);
1972 GNUNET_free_non_null (plugin->cert);
1973 GNUNET_free_non_null (plugin->key);
1976 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1977 "%s server component stopped\n", plugin->name);
1982 * Add an address to the server's set of addresses and notify transport
1984 * @param cls the plugin handle
1985 * @param add_remove GNUNET_YES on add, GNUNET_NO on remove
1986 * @param addr the address
1987 * @param addrlen address length
1990 server_add_address (void *cls, int add_remove, const struct sockaddr *addr,
1993 struct HTTP_Server_Plugin *plugin = cls;
1994 struct HttpAddressWrapper *w = NULL;
1996 w = GNUNET_malloc (sizeof (struct HttpAddressWrapper));
1997 w->addr = http_common_address_from_socket (plugin->protocol, addr, addrlen);
1998 if (NULL == w->addr)
2003 w->addrlen = http_common_address_get_size (w->addr);
2005 GNUNET_CONTAINER_DLL_insert(plugin->addr_head, plugin->addr_tail, w);
2006 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2007 "Notifying transport to add address `%s'\n",
2008 http_common_plugin_address_to_string(NULL, w->addr, w->addrlen));
2010 plugin->env->notify_address (plugin->env->cls, add_remove, w->addr, w->addrlen, "https_client");
2012 plugin->env->notify_address (plugin->env->cls, add_remove, w->addr, w->addrlen, "http_client");
2018 * Remove an address from the server's set of addresses and notify transport
2020 * @param cls the plugin handle
2021 * @param add_remove GNUNET_YES on add, GNUNET_NO on remove
2022 * @param addr the address
2023 * @param addrlen address length
2026 server_remove_address (void *cls, int add_remove, const struct sockaddr *addr,
2029 struct HTTP_Server_Plugin *plugin = cls;
2030 struct HttpAddressWrapper *w = plugin->addr_head;
2032 void * saddr = http_common_address_from_socket (plugin->protocol, addr, addrlen);
2035 saddr_len = http_common_address_get_size (saddr);
2039 if (GNUNET_YES == http_common_cmp_addresses(w->addr, w->addrlen, saddr, saddr_len))
2043 GNUNET_free (saddr);
2048 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2049 "Notifying transport to remove address `%s'\n",
2050 http_common_plugin_address_to_string (NULL, w->addr, w->addrlen));
2051 GNUNET_CONTAINER_DLL_remove (plugin->addr_head, plugin->addr_tail, w);
2053 plugin->env->notify_address (plugin->env->cls, add_remove, w->addr, w->addrlen, "https_client");
2055 plugin->env->notify_address (plugin->env->cls, add_remove, w->addr, w->addrlen, "http_client");
2057 GNUNET_free (w->addr);
2064 * Our external IP address/port mapping has changed.
2066 * @param cls closure, the 'struct LocalAddrList'
2067 * @param add_remove GNUNET_YES to mean the new public IP address, GNUNET_NO to mean
2068 * the previous (now invalid) one
2069 * @param addr either the previous or the new public IP address
2070 * @param addrlen actual lenght of the address
2073 server_nat_port_map_callback (void *cls, int add_remove, const struct sockaddr *addr,
2076 GNUNET_assert (cls != NULL);
2077 struct HTTP_Server_Plugin *plugin = cls;
2079 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2080 "NAT called to %s address `%s'\n",
2081 (add_remove == GNUNET_NO) ? "remove" : "add",
2082 GNUNET_a2s (addr, addrlen));
2084 if (AF_INET == addr->sa_family)
2086 struct sockaddr_in *s4 = (struct sockaddr_in *) addr;
2088 if (GNUNET_NO == plugin->use_ipv4)
2091 if ((NULL != plugin->server_addr_v4) &&
2092 (0 != memcmp (&plugin->server_addr_v4->sin_addr,
2093 &s4->sin_addr, sizeof (struct in_addr))))
2095 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2096 "Skipping address `%s' (not bindto address)\n",
2097 GNUNET_a2s (addr, addrlen));
2102 if (AF_INET6 == addr->sa_family)
2104 struct sockaddr_in6 *s6 = (struct sockaddr_in6 *) addr;
2105 if (GNUNET_NO == plugin->use_ipv6)
2108 if ((NULL != plugin->server_addr_v6) &&
2109 (0 != memcmp (&plugin->server_addr_v6->sin6_addr,
2110 &s6->sin6_addr, sizeof (struct in6_addr))))
2112 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2113 "Skipping address `%s' (not bindto address)\n",
2114 GNUNET_a2s (addr, addrlen));
2122 server_add_address (cls, add_remove, addr, addrlen);
2125 server_remove_address (cls, add_remove, addr, addrlen);
2132 * Get valid server addresses
2134 * @param plugin the plugin handle
2135 * @param serviceName the servicename
2136 * @param cfg configuration handle
2137 * @param addrs addresses
2138 * @param addr_lens address length
2139 * @return number of addresses
2142 server_get_addresses (struct HTTP_Server_Plugin *plugin,
2143 const char *serviceName,
2144 const struct GNUNET_CONFIGURATION_Handle *cfg,
2145 struct sockaddr ***addrs, socklen_t ** addr_lens)
2148 unsigned long long port;
2149 struct addrinfo hints;
2150 struct addrinfo *res;
2151 struct addrinfo *pos;
2152 struct addrinfo *next;
2156 struct sockaddr **saddrs;
2157 socklen_t *saddrlens;
2163 disablev6 = !plugin->use_ipv6;
2166 if (GNUNET_CONFIGURATION_have_value (cfg, serviceName, "PORT"))
2168 GNUNET_break (GNUNET_OK ==
2169 GNUNET_CONFIGURATION_get_value_number (cfg, serviceName,
2173 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2175 ("Require valid port number for service in configuration!\n"));
2176 return GNUNET_SYSERR;
2181 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, plugin->name,
2182 "Starting in listen only mode\n");
2183 return -1; /* listen only */
2187 if (GNUNET_CONFIGURATION_have_value (cfg, serviceName, "BINDTO"))
2189 GNUNET_break (GNUNET_OK ==
2190 GNUNET_CONFIGURATION_get_value_string (cfg, serviceName,
2191 "BINDTO", &hostname));
2196 if (hostname != NULL)
2198 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2199 "Resolving `%s' since that is where `%s' will bind to.\n",
2200 hostname, serviceName);
2201 memset (&hints, 0, sizeof (struct addrinfo));
2203 hints.ai_family = AF_INET;
2204 if ((0 != (ret = getaddrinfo (hostname, NULL, &hints, &res))) ||
2207 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Failed to resolve `%s': %s\n"),
2208 hostname, gai_strerror (ret));
2209 GNUNET_free (hostname);
2210 return GNUNET_SYSERR;
2214 while (NULL != (pos = next))
2216 next = pos->ai_next;
2217 if ((disablev6) && (pos->ai_family == AF_INET6))
2223 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2224 _("Failed to find %saddress for `%s'.\n"),
2225 disablev6 ? "IPv4 " : "", hostname);
2227 GNUNET_free (hostname);
2228 return GNUNET_SYSERR;
2231 saddrs = GNUNET_malloc ((resi + 1) * sizeof (struct sockaddr *));
2232 saddrlens = GNUNET_malloc ((resi + 1) * sizeof (socklen_t));
2235 while (NULL != (pos = next))
2237 next = pos->ai_next;
2238 if ((disablev6) && (pos->ai_family == AF_INET6))
2240 if ((pos->ai_protocol != IPPROTO_TCP) && (pos->ai_protocol != 0))
2241 continue; /* not TCP */
2242 if ((pos->ai_socktype != SOCK_STREAM) && (pos->ai_socktype != 0))
2243 continue; /* huh? */
2244 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2245 "Service will bind to `%s'\n", GNUNET_a2s (pos->ai_addr,
2247 if (pos->ai_family == AF_INET)
2249 GNUNET_assert (pos->ai_addrlen == sizeof (struct sockaddr_in));
2250 saddrlens[i] = pos->ai_addrlen;
2251 saddrs[i] = GNUNET_malloc (saddrlens[i]);
2252 memcpy (saddrs[i], pos->ai_addr, saddrlens[i]);
2253 ((struct sockaddr_in *) saddrs[i])->sin_port = htons (port);
2257 GNUNET_assert (pos->ai_family == AF_INET6);
2258 GNUNET_assert (pos->ai_addrlen == sizeof (struct sockaddr_in6));
2259 saddrlens[i] = pos->ai_addrlen;
2260 saddrs[i] = GNUNET_malloc (saddrlens[i]);
2261 memcpy (saddrs[i], pos->ai_addr, saddrlens[i]);
2262 ((struct sockaddr_in6 *) saddrs[i])->sin6_port = htons (port);
2266 GNUNET_free (hostname);
2272 /* will bind against everything, just set port */
2278 saddrs = GNUNET_malloc ((resi + 1) * sizeof (struct sockaddr *));
2279 saddrlens = GNUNET_malloc ((resi + 1) * sizeof (socklen_t));
2281 saddrlens[i] = sizeof (struct sockaddr_in);
2282 saddrs[i] = GNUNET_malloc (saddrlens[i]);
2283 #if HAVE_SOCKADDR_IN_SIN_LEN
2284 ((struct sockaddr_in *) saddrs[i])->sin_len = saddrlens[i];
2286 ((struct sockaddr_in *) saddrs[i])->sin_family = AF_INET;
2287 ((struct sockaddr_in *) saddrs[i])->sin_port = htons (port);
2293 saddrs = GNUNET_malloc ((resi + 1) * sizeof (struct sockaddr *));
2294 saddrlens = GNUNET_malloc ((resi + 1) * sizeof (socklen_t));
2296 saddrlens[i] = sizeof (struct sockaddr_in6);
2297 saddrs[i] = GNUNET_malloc (saddrlens[i]);
2298 #if HAVE_SOCKADDR_IN_SIN_LEN
2299 ((struct sockaddr_in6 *) saddrs[i])->sin6_len = saddrlens[0];
2301 ((struct sockaddr_in6 *) saddrs[i])->sin6_family = AF_INET6;
2302 ((struct sockaddr_in6 *) saddrs[i])->sin6_port = htons (port);
2304 saddrlens[i] = sizeof (struct sockaddr_in);
2305 saddrs[i] = GNUNET_malloc (saddrlens[i]);
2306 #if HAVE_SOCKADDR_IN_SIN_LEN
2307 ((struct sockaddr_in *) saddrs[i])->sin_len = saddrlens[1];
2309 ((struct sockaddr_in *) saddrs[i])->sin_family = AF_INET;
2310 ((struct sockaddr_in *) saddrs[i])->sin_port = htons (port);
2314 *addr_lens = saddrlens;
2320 * Ask NAT for addresses
2322 * @param plugin the plugin handle
2325 server_start_report_addresses (struct HTTP_Server_Plugin *plugin)
2327 int res = GNUNET_OK;
2328 struct sockaddr **addrs;
2329 socklen_t *addrlens;
2331 res = server_get_addresses (plugin,
2332 plugin->name, plugin->env->cfg,
2334 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2335 _("Found %u addresses to report to NAT service\n"), res);
2337 if (GNUNET_SYSERR == res)
2344 GNUNET_NAT_register (plugin->env->cfg, GNUNET_YES, plugin->port,
2346 (const struct sockaddr **) addrs, addrlens,
2347 &server_nat_port_map_callback, NULL, plugin);
2351 GNUNET_assert (addrs[res] != NULL);
2352 GNUNET_free (addrs[res]);
2354 GNUNET_free_non_null (addrs);
2355 GNUNET_free_non_null (addrlens);
2360 * Stop NAT for addresses
2362 * @param plugin the plugin handle
2365 server_stop_report_addresses (struct HTTP_Server_Plugin *plugin)
2367 /* Stop NAT handle */
2368 if (NULL != plugin->nat)
2369 GNUNET_NAT_unregister (plugin->nat);
2371 /* Clean up addresses */
2372 struct HttpAddressWrapper *w;
2374 while (plugin->addr_head != NULL)
2376 w = plugin->addr_head;
2377 GNUNET_CONTAINER_DLL_remove (plugin->addr_head, plugin->addr_tail, w);
2378 GNUNET_free (w->addr);
2385 * Check if IPv6 supported on this system
2387 * @param plugin the plugin handle
2388 * @return GNUNET_YES on success, else GNUNET_NO
2391 server_check_ipv6_support (struct HTTP_Server_Plugin *plugin)
2393 struct GNUNET_NETWORK_Handle *desc = NULL;
2394 int res = GNUNET_NO;
2396 /* Probe IPv6 support */
2397 desc = GNUNET_NETWORK_socket_create (PF_INET6, SOCK_STREAM, 0);
2400 if ((errno == ENOBUFS) || (errno == ENOMEM) || (errno == ENFILE) ||
2403 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "socket");
2405 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, plugin->name,
2407 ("Disabling IPv6 since it is not supported on this system!\n"));
2412 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (desc));
2416 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2417 "Testing IPv6 on this system: %s\n",
2418 (res == GNUNET_YES) ? "successful" : "failed");
2424 * Function called when the service shuts down. Unloads our plugins
2425 * and cancels pending validations.
2427 * @param cls closure, unused
2428 * @param tc task context (unused)
2431 server_notify_external_hostname (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2433 struct HTTP_Server_Plugin *plugin = cls;
2435 plugin->notify_ext_task = GNUNET_SCHEDULER_NO_TASK;
2437 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
2440 GNUNET_asprintf(&plugin->ext_addr, "%s://%s", plugin->protocol, plugin->external_hostname);
2441 plugin->ext_addr_len = strlen (plugin->ext_addr) + 1;
2442 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2443 "Notifying transport about external hostname address `%s'\n", plugin->ext_addr);
2446 plugin->env->notify_address (plugin->env->cls, GNUNET_YES,
2447 plugin->ext_addr, plugin->ext_addr_len,
2450 plugin->env->notify_address (plugin->env->cls, GNUNET_YES,
2451 plugin->ext_addr, plugin->ext_addr_len,
2458 * Configure the plugin
2460 * @param plugin plugin handle
2461 * @return GNUNET_OK on success, GNUNET_SYSERR on failure
2464 server_configure_plugin (struct HTTP_Server_Plugin *plugin)
2466 unsigned long long port;
2467 unsigned long long max_connections;
2468 char *bind4_address = NULL;
2469 char *bind6_address = NULL;
2472 if (GNUNET_CONFIGURATION_have_value
2473 (plugin->env->cfg, plugin->name, "USE_IPv4"))
2476 GNUNET_CONFIGURATION_get_value_yesno (plugin->env->cfg, plugin->name,
2480 plugin->use_ipv4 = GNUNET_YES;
2481 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2482 _("IPv4 support is %s\n"),
2483 (plugin->use_ipv4 == GNUNET_YES) ? "enabled" : "disabled");
2486 if (GNUNET_CONFIGURATION_have_value
2487 (plugin->env->cfg, plugin->name, "USE_IPv6"))
2490 GNUNET_CONFIGURATION_get_value_yesno (plugin->env->cfg, plugin->name,
2494 plugin->use_ipv6 = GNUNET_YES;
2495 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2496 _("IPv6 support is %s\n"),
2497 (plugin->use_ipv6 == GNUNET_YES) ? "enabled" : "disabled");
2499 if ((plugin->use_ipv4 == GNUNET_NO) && (plugin->use_ipv6 == GNUNET_NO))
2501 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
2503 ("Neither IPv4 nor IPv6 are enabled! Fix in configuration\n"),
2505 return GNUNET_SYSERR;
2508 /* Reading port number from config file */
2510 GNUNET_CONFIGURATION_get_value_number (plugin->env->cfg, plugin->name,
2511 "PORT", &port)) || (port > 65535))
2513 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
2514 _("Port is required! Fix in configuration\n"),
2516 return GNUNET_SYSERR;
2518 plugin->port = port;
2520 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2521 _("Using port %u\n"), plugin->port);
2523 if ((plugin->use_ipv4 == GNUNET_YES) &&
2524 (GNUNET_YES == GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg,
2525 plugin->name, "BINDTO", &bind4_address)))
2527 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2528 "Binding %s plugin to specific IPv4 address: `%s'\n",
2529 plugin->protocol, bind4_address);
2530 plugin->server_addr_v4 = GNUNET_malloc (sizeof (struct sockaddr_in));
2531 if (1 != inet_pton (AF_INET, bind4_address,
2532 &plugin->server_addr_v4->sin_addr))
2534 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
2536 ("Specific IPv4 address `%s' in configuration file is invalid!\n"),
2538 GNUNET_free (bind4_address);
2539 GNUNET_free (plugin->server_addr_v4);
2540 plugin->server_addr_v4 = NULL;
2541 return GNUNET_SYSERR;
2545 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2546 _("Binding to IPv4 address %s\n"), bind4_address);
2547 plugin->server_addr_v4->sin_family = AF_INET;
2548 plugin->server_addr_v4->sin_port = htons (plugin->port);
2550 GNUNET_free (bind4_address);
2553 if ((plugin->use_ipv6 == GNUNET_YES) &&
2555 GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg, plugin->name,
2556 "BINDTO6", &bind6_address)))
2558 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2559 "Binding %s plugin to specific IPv6 address: `%s'\n",
2560 plugin->protocol, bind6_address);
2561 plugin->server_addr_v6 = GNUNET_malloc (sizeof (struct sockaddr_in6));
2563 inet_pton (AF_INET6, bind6_address, &plugin->server_addr_v6->sin6_addr))
2565 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
2567 ("Specific IPv6 address `%s' in configuration file is invalid!\n"),
2569 GNUNET_free (bind6_address);
2570 GNUNET_free (plugin->server_addr_v6);
2571 plugin->server_addr_v6 = NULL;
2572 return GNUNET_SYSERR;
2576 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2577 _("Binding to IPv6 address %s\n"), bind6_address);
2578 plugin->server_addr_v6->sin6_family = AF_INET6;
2579 plugin->server_addr_v6->sin6_port = htons (plugin->port);
2581 GNUNET_free (bind6_address);
2584 if (GNUNET_YES == GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg, plugin->name,
2585 "EXTERNAL_HOSTNAME", &plugin->external_hostname))
2587 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2588 _("Using external hostname `%s'\n"), plugin->external_hostname);
2589 plugin->notify_ext_task = GNUNET_SCHEDULER_add_now (&server_notify_external_hostname, plugin);
2591 /* Use only configured external hostname */
2592 if (GNUNET_CONFIGURATION_have_value
2593 (plugin->env->cfg, plugin->name, "EXTERNAL_HOSTNAME_ONLY"))
2595 plugin->external_only =
2596 GNUNET_CONFIGURATION_get_value_yesno (plugin->env->cfg, plugin->name,
2597 "EXTERNAL_HOSTNAME_ONLY");
2600 plugin->external_only = GNUNET_NO;
2602 if (GNUNET_YES == plugin->external_only)
2603 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2604 _("Notifying transport only about hostname `%s'\n"), plugin->external_hostname);
2607 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2608 "No external hostname configured\n");
2610 /* Optional parameters */
2611 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (plugin->env->cfg,
2613 "MAX_CONNECTIONS", &max_connections))
2614 max_connections = 128;
2615 plugin->max_connections = max_connections;
2617 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2618 _("Maximum number of connections is %u\n"),
2619 plugin->max_connections);
2625 * Session was idle, so disconnect it
2627 * @param cls the session
2628 * @param tc task context
2631 server_session_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2633 GNUNET_assert (NULL != cls);
2634 struct Session *s = cls;
2636 s->timeout_task = GNUNET_SCHEDULER_NO_TASK;
2637 GNUNET_log (TIMEOUT_LOG,
2638 "Session %p was idle for %llu ms, disconnecting\n",
2639 s, (unsigned long long) SERVER_SESSION_TIMEOUT.rel_value);
2641 /* call session destroy function */
2642 GNUNET_assert (GNUNET_OK == server_disconnect (s));
2647 * Start session timeout for session s
2649 * @param s the session
2652 server_start_session_timeout (struct Session *s)
2654 GNUNET_assert (NULL != s);
2655 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == s->timeout_task);
2656 s->timeout_task = GNUNET_SCHEDULER_add_delayed (SERVER_SESSION_TIMEOUT,
2657 &server_session_timeout,
2659 GNUNET_log (TIMEOUT_LOG,
2660 "Timeout for session %p set to %llu ms\n",
2661 s, (unsigned long long) SERVER_SESSION_TIMEOUT.rel_value);
2666 * Increment session timeout due to activity session s
2668 * @param s the session
2671 server_reschedule_session_timeout (struct Session *s)
2673 GNUNET_assert (NULL != s);
2674 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK != s->timeout_task);
2676 GNUNET_SCHEDULER_cancel (s->timeout_task);
2677 s->timeout_task = GNUNET_SCHEDULER_add_delayed (SERVER_SESSION_TIMEOUT,
2678 &server_session_timeout,
2680 GNUNET_log (TIMEOUT_LOG,
2681 "Timeout rescheduled for session %p set to %llu ms\n",
2682 s, (unsigned long long) SERVER_SESSION_TIMEOUT.rel_value);
2687 * Exit point from the plugin.
2693 LIBGNUNET_PLUGIN_TRANSPORT_DONE (void *cls)
2695 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
2696 struct HTTP_Server_Plugin *plugin = api->cls;
2697 struct Session *pos;
2698 struct Session *next;
2700 if (NULL == api->cls)
2702 /* Free for stub mode */
2707 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2708 _("Shutting down plugin `%s'\n"),
2711 if (GNUNET_SCHEDULER_NO_TASK != plugin->notify_ext_task)
2713 GNUNET_SCHEDULER_cancel (plugin->notify_ext_task);
2714 plugin->notify_ext_task = GNUNET_SCHEDULER_NO_TASK;
2717 if (NULL != plugin->ext_addr)
2719 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2720 "Notifying transport to remove address `%s'\n",
2721 http_common_plugin_address_to_string (NULL,
2723 plugin->ext_addr_len));
2725 plugin->env->notify_address (plugin->env->cls,
2728 plugin->ext_addr_len,
2731 plugin->env->notify_address (plugin->env->cls,
2734 plugin->ext_addr_len,
2740 /* Stop to report addresses to transport service */
2741 server_stop_report_addresses (plugin);
2742 server_stop (plugin);
2743 next = plugin->head;
2744 while (NULL != (pos = next))
2747 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2748 "Removing left over session %p\n", pos);
2749 server_delete_session (pos);
2753 GNUNET_free_non_null (plugin->external_hostname);
2754 GNUNET_free_non_null (plugin->ext_addr);
2755 GNUNET_free_non_null (plugin->server_addr_v4);
2756 GNUNET_free_non_null (plugin->server_addr_v6);
2758 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2759 _("Shutdown for plugin `%s' complete\n"),
2762 GNUNET_free (plugin);
2769 * Entry point for the plugin.
2775 LIBGNUNET_PLUGIN_TRANSPORT_INIT (void *cls)
2777 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
2778 struct GNUNET_TRANSPORT_PluginFunctions *api;
2779 struct HTTP_Server_Plugin *plugin;
2781 plugin = GNUNET_malloc (sizeof (struct HTTP_Server_Plugin));
2785 if (NULL == env->receive)
2787 /* run in 'stub' mode (i.e. as part of gnunet-peerinfo), don't fully
2788 initialze the plugin or the API */
2789 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
2791 api->address_to_string = &http_common_plugin_address_to_string;
2792 api->string_to_address = &http_common_plugin_string_to_address;
2793 api->address_pretty_printer = &http_common_plugin_address_pretty_printer;
2797 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
2799 api->send = &http_server_plugin_send;
2800 api->disconnect = &http_server_plugin_disconnect;
2801 api->check_address = &http_server_plugin_address_suggested;
2802 api->get_session = &http_server_plugin_get_session;
2804 api->address_to_string = &http_common_plugin_address_to_string;
2805 api->string_to_address = &http_common_plugin_string_to_address;
2806 api->address_pretty_printer = &http_common_plugin_address_pretty_printer;
2809 plugin->name = "transport-https_server";
2810 plugin->protocol = "https";
2812 plugin->name = "transport-http_server";
2813 plugin->protocol = "http";
2816 /* Configure plugin */
2817 if (GNUNET_SYSERR == server_configure_plugin (plugin))
2819 LIBGNUNET_PLUGIN_TRANSPORT_DONE (api);
2823 /* Check IPv6 support */
2824 if (GNUNET_YES == plugin->use_ipv6)
2825 plugin->use_ipv6 = server_check_ipv6_support (plugin);
2827 /* Report addresses to transport service */
2828 if (GNUNET_NO == plugin->external_only)
2829 server_start_report_addresses (plugin);
2831 if (GNUNET_SYSERR == server_start (plugin))
2833 LIBGNUNET_PLUGIN_TRANSPORT_DONE (api);
2840 /* end of plugin_transport_http_server.c */