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 /* Enable output for debbuging URL's of incoming requests */
50 #define DEBUG_URL_PARSE GNUNET_NO
54 * Encapsulation of all of the state of the plugin.
60 * Session handle for connections.
65 * Stored in a linked list.
70 * Stored in a linked list.
75 * To whom are we talking to (set to our identity
76 * if we are still waiting for the welcome message)
78 struct GNUNET_PeerIdentity target;
81 * Pointer to the global plugin struct.
83 struct HTTP_Server_Plugin *plugin;
86 * next pointer for double linked list
88 struct HTTP_Message *msg_head;
91 * previous pointer for double linked list
93 struct HTTP_Message *msg_tail;
96 * Message stream tokenizer for incoming data
98 struct GNUNET_SERVER_MessageStreamTokenizer *msg_tk;
103 struct ServerConnection *server_recv;
108 struct ServerConnection *server_send;
121 * Unique HTTP/S connection tag for this connection
126 * ATS network type in NBO
128 uint32_t ats_address_network_type;
131 * Was session given to transport service?
136 * Did we immediately end the session in disconnect_cb
141 * Are incoming connection established at the moment
143 int connect_in_progress;
146 * Absolute time when to receive data again
147 * Used for receive throttling
149 struct GNUNET_TIME_Absolute next_receive;
152 * Session timeout task
154 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
158 struct ServerConnection
163 /* Should this connection get disconnected? GNUNET_YES/NO */
166 /* For PUT connections: Is this the first or last callback with size 0 */
169 /* The session this server connection belongs to */
170 struct Session *session;
172 /* The MHD connection */
173 struct MHD_Connection *mhd_conn;
176 struct MHD_Daemon *mhd_daemon;
181 * Encapsulation of all of the state of the plugin.
183 struct HTTP_Server_Plugin
188 struct GNUNET_TRANSPORT_PluginEnvironment *env;
191 * Linked list head of open sessions.
194 struct Session *head;
197 * Linked list tail of open sessions.
199 struct Session *tail;
214 char *external_hostname;
217 * Verify external address
219 int verify_external_hostname;
223 * Maximum number of sockets the plugin can use
224 * Each http inbound /outbound connections are two connections
226 unsigned int max_connections;
229 * Current number of sockets the plugin can use
230 * Each http inbound /outbound connections are two connections
232 unsigned int cur_connections;
235 * Did we immediately end the session in disconnect_cb
245 * External hostname the plugin can be connected to, can be different to
246 * the host's FQDN, used e.g. for reverse proxying
251 * Notify transport only about external address
253 unsigned int external_only;
256 * External address length
276 * Task calling transport service about external address
278 GNUNET_SCHEDULER_TaskIdentifier notify_ext_task;
281 * NAT handle & address management
283 struct GNUNET_NAT_Handle *nat;
286 * List of own addresses
290 * IPv4 addresses DLL head
292 struct HttpAddressWrapper *addr_head;
295 * IPv4 addresses DLL tail
297 struct HttpAddressWrapper *addr_tail;
300 * IPv4 server socket to bind to
302 struct sockaddr_in *server_addr_v4;
305 * IPv6 server socket to bind to
307 struct sockaddr_in6 *server_addr_v6;
312 GNUNET_SCHEDULER_TaskIdentifier server_v4_task;
317 GNUNET_SCHEDULER_TaskIdentifier server_v6_task;
320 * The IPv4 server is scheduled to run asap
322 int server_v4_immediately;
325 * The IPv6 server is scheduled to run asap
327 int server_v6_immediately;
332 struct MHD_Daemon *server_v4;
337 struct MHD_Daemon *server_v6;
345 * Use RC4-128 instead of AES:
346 * NONE:+VERS-TLS1.0:+ARCFOUR-128:+SHA1:+RSA:+COMP-NULL
366 * Wrapper to manage addresses
368 struct HttpAddressWrapper
373 struct HttpAddressWrapper *next;
376 * Linked list previous
378 struct HttpAddressWrapper *prev;
387 * Message to send using http
392 * next pointer for double linked list
394 struct HTTP_Message *next;
397 * previous pointer for double linked list
399 struct HTTP_Message *prev;
402 * buffer containing data to send
407 * amount of data already sent
417 * HTTP/S specific overhead
422 * Continuation function to call once the transmission buffer
423 * has again space available. NULL if there is no
424 * continuation to call.
426 GNUNET_TRANSPORT_TransmitContinuation transmit_cont;
429 * Closure for transmit_cont.
431 void *transmit_cont_cls;
436 * The http_server plugin handle
438 static struct HTTP_Server_Plugin * p;
442 * Start session timeout for session s
443 * @param s the session
446 server_start_session_timeout (struct Session *s);
450 * Increment session timeout due to activity for session s
451 * @param s the session
454 server_reschedule_session_timeout (struct Session *s);
458 * Cancel timeout for session s
459 * @param s the session
462 server_stop_session_timeout (struct Session *s);
466 * Disconnect a session s
467 * @param s the session
470 server_disconnect (struct Session *s);
474 * Does session s exist?
476 * @param plugin the plugin handle
477 * @param s the session
478 * @return GNUNET_YES on success, GNUNET_NO on error
481 server_exist_session (struct HTTP_Server_Plugin *plugin, struct Session *s);
485 * Reschedule the execution of both IPv4 and IPv6 server
486 * @param plugin the plugin
487 * @param server which server to schedule v4 or v6?
488 * @param now GNUNET_YES to schedule execution immediately, GNUNET_NO to wait
492 server_reschedule (struct HTTP_Server_Plugin *plugin, struct MHD_Daemon *server,
497 * Function that can be used by the transport service to transmit
498 * a message using the plugin. Note that in the case of a
499 * peer disconnecting, the continuation MUST be called
500 * prior to the disconnect notification itself. This function
501 * will be called with this peer's HELLO message to initiate
502 * a fresh connection to another peer.
505 * @param session which session must be used
506 * @param msgbuf the message to transmit
507 * @param msgbuf_size number of bytes in 'msgbuf'
508 * @param priority how important is the message (most plugins will
509 * ignore message priority and just FIFO)
510 * @param to how long to wait at most for the transmission (does not
511 * require plugins to discard the message after the timeout,
512 * just advisory for the desired delay; most plugins will ignore
514 * @param cont continuation to call once the message has
515 * been transmitted (or if the transport is ready
516 * for the next transmission call; or if the
517 * peer disconnected...); can be NULL
518 * @param cont_cls closure for cont
519 * @return number of bytes used (on the physical network, with overheads);
520 * -1 on hard errors (i.e. address invalid); 0 is a legal value
521 * and does NOT mean that the message was not transmitted (DV)
524 http_server_plugin_send (void *cls,
525 struct Session *session,
526 const char *msgbuf, size_t msgbuf_size,
527 unsigned int priority,
528 struct GNUNET_TIME_Relative to,
529 GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
531 struct HTTP_Server_Plugin *plugin = cls;
532 struct HTTP_Message *msg;
536 GNUNET_assert (plugin != NULL);
537 GNUNET_assert (session != NULL);
539 if (GNUNET_NO == server_exist_session (plugin, session))
542 return GNUNET_SYSERR;
544 if (NULL == session->server_send)
546 if (GNUNET_NO == session->connect_in_progress)
548 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, session->plugin->name,
549 "Session %p/connection %p: Sending message with %u bytes to peer `%s' with FAILED\n",
550 session, session->server_send,
551 msgbuf_size, GNUNET_i2s (&session->target));
553 return GNUNET_SYSERR;
558 if (GNUNET_YES == session->server_send->disconnect)
559 return GNUNET_SYSERR;
563 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, session->plugin->name,
564 "Session %p/connection %p: Sending message with %u to peer `%s' with \n",
565 session, session->server_send,
566 msgbuf_size, GNUNET_i2s (&session->target));
568 /* create new message and schedule */
569 bytes_sent = sizeof (struct HTTP_Message) + msgbuf_size;
570 msg = GNUNET_malloc (bytes_sent);
572 msg->size = msgbuf_size;
574 msg->buf = (char *) &msg[1];
575 msg->transmit_cont = cont;
576 msg->transmit_cont_cls = cont_cls;
577 memcpy (msg->buf, msgbuf, msgbuf_size);
579 GNUNET_CONTAINER_DLL_insert_tail (session->msg_head, session->msg_tail, msg);
581 GNUNET_asprintf (&stat_txt, "# bytes currently in %s_server buffers", plugin->protocol);
582 GNUNET_STATISTICS_update (plugin->env->stats,
583 stat_txt, msgbuf_size, GNUNET_NO);
584 GNUNET_free (stat_txt);
586 if (NULL != session->server_send)
588 server_reschedule (session->plugin,
589 session->server_send->mhd_daemon,
591 server_reschedule_session_timeout (session);
599 * Function that can be used to force the plugin to disconnect
600 * from the given peer and cancel all previous transmissions
601 * (and their continuationc).
604 * @param target peer from which to disconnect
607 http_server_plugin_disconnect (void *cls, const struct GNUNET_PeerIdentity *target)
609 struct HTTP_Server_Plugin *plugin = cls;
610 struct Session *next = NULL;
611 struct Session *pos = NULL;
613 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
614 "Transport tells me to disconnect `%s'\n",
615 GNUNET_i2s (target));
618 while (NULL != (pos = next))
621 if (0 == memcmp (target, &pos->target, sizeof (struct GNUNET_PeerIdentity)))
623 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
624 "Disconnecting session %p to `%s'\n",
625 pos, GNUNET_i2s (target));
626 server_disconnect (pos);
634 * Another peer has suggested an address for this
635 * peer and transport plugin. Check that this could be a valid
636 * address. If so, consider adding it to the list
640 * @param addr pointer to the address
641 * @param addrlen length of addr
642 * @return GNUNET_OK if this is a plausible address for this peer
646 http_server_plugin_address_suggested (void *cls, const void *addr,
649 struct HTTP_Server_Plugin *plugin = cls;
650 struct HttpAddressWrapper *next;
651 struct HttpAddressWrapper *pos;
654 if ((NULL != plugin->ext_addr) &&
655 GNUNET_YES == (http_common_cmp_addresses (addr, addrlen,
656 plugin->ext_addr, plugin->ext_addr_len)))
659 next = plugin->addr_head;
660 while (NULL != (pos = next))
663 if (GNUNET_YES == (http_common_cmp_addresses(addr,
676 * Creates a new outbound session the transport
677 * service will use to send data to the peer
679 * Since HTTP/S server cannot create sessions, always return NULL
681 * @param cls the plugin
682 * @param address the address
683 * @return always NULL
685 static struct Session *
686 http_server_plugin_get_session (void *cls,
687 const struct GNUNET_HELLO_Address *address)
694 * Deleting the session
695 * Must not be used afterwards
697 * @param s the session to delete
700 server_delete_session (struct Session *s)
702 struct HTTP_Server_Plugin *plugin = s->plugin;
703 server_stop_session_timeout(s);
705 GNUNET_CONTAINER_DLL_remove (plugin->head, plugin->tail, s);
706 struct HTTP_Message *msg = s->msg_head;
707 struct HTTP_Message *tmp = NULL;
713 GNUNET_CONTAINER_DLL_remove (s->msg_head, s->msg_tail, msg);
714 if (msg->transmit_cont != NULL)
716 msg->transmit_cont (msg->transmit_cont_cls, &s->target, GNUNET_SYSERR,
717 msg->size, msg->pos + msg->overhead);
723 if (s->msg_tk != NULL)
725 GNUNET_SERVER_mst_destroy (s->msg_tk);
728 GNUNET_free (s->addr);
729 GNUNET_free_non_null (s->server_recv);
730 GNUNET_free_non_null (s->server_send);
733 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
734 "Session %p destroyed\n", s);
739 * Cancel timeout for session s
741 * @param s the session
744 server_stop_session_timeout (struct Session *s)
746 GNUNET_assert (NULL != s);
748 if (GNUNET_SCHEDULER_NO_TASK != s->timeout_task)
750 GNUNET_SCHEDULER_cancel (s->timeout_task);
751 s->timeout_task = GNUNET_SCHEDULER_NO_TASK;
752 GNUNET_log (TIMEOUT_LOG, "Timeout stopped for session %p\n", s);
758 * Function that queries MHD's select sets and
759 * starts the task waiting for them.
760 * @param plugin plugin
761 * @param daemon_handle the MHD daemon handle
762 * @param now schedule immediately
763 * @return gnunet task identifier
765 static GNUNET_SCHEDULER_TaskIdentifier
766 server_schedule (struct HTTP_Server_Plugin *plugin,
767 struct MHD_Daemon *daemon_handle,
772 * Reschedule the execution of both IPv4 and IPv6 server
773 * @param plugin the plugin
774 * @param server which server to schedule v4 or v6?
775 * @param now GNUNET_YES to schedule execution immediately, GNUNET_NO to wait
779 server_reschedule (struct HTTP_Server_Plugin *plugin, struct MHD_Daemon *server,
782 if ((server == plugin->server_v4) && (plugin->server_v4 != NULL))
784 if (GNUNET_YES == plugin->server_v4_immediately)
785 return; /* No rescheduling, server will run asap */
787 if (GNUNET_YES == now)
788 plugin->server_v4_immediately = GNUNET_YES;
790 if (plugin->server_v4_task != GNUNET_SCHEDULER_NO_TASK)
792 GNUNET_SCHEDULER_cancel (plugin->server_v4_task);
793 plugin->server_v4_task = GNUNET_SCHEDULER_NO_TASK;
795 plugin->server_v4_task = server_schedule (plugin, plugin->server_v4, now);
798 if ((server == plugin->server_v6) && (plugin->server_v6 != NULL))
800 if (GNUNET_YES == plugin->server_v6_immediately)
801 return; /* No rescheduling, server will run asap */
803 if (GNUNET_YES == now)
804 plugin->server_v6_immediately = GNUNET_YES;
806 if (plugin->server_v6_task != GNUNET_SCHEDULER_NO_TASK)
808 GNUNET_SCHEDULER_cancel (plugin->server_v6_task);
809 plugin->server_v6_task = GNUNET_SCHEDULER_NO_TASK;
811 plugin->server_v6_task = server_schedule (plugin, plugin->server_v6, now);
817 * Disconnect session s
819 * @param s the session
820 * @return GNUNET_OK on success
823 server_disconnect (struct Session *s)
825 struct ServerConnection * send = NULL;
826 struct ServerConnection * recv = NULL;
828 if (GNUNET_NO == server_exist_session (p, s))
831 return GNUNET_SYSERR;
834 send = (struct ServerConnection *) s->server_send;
835 if (s->server_send != NULL)
837 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, s->plugin->name,
838 "Server: %p / %p Terminating inbound PUT session to peer `%s'\n",
839 s, s->server_send, GNUNET_i2s (&s->target));
841 send->disconnect = GNUNET_YES;
842 #if MHD_VERSION >= 0x00090E00
843 MHD_set_connection_option (send->mhd_conn, MHD_CONNECTION_OPTION_TIMEOUT,
846 server_reschedule (s->plugin, send->mhd_daemon, GNUNET_YES);
849 recv = (struct ServerConnection *) s->server_recv;
852 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, s->plugin->name,
853 "Server: %p / %p Terminating inbound GET session to peer `%s'\n",
854 s, s->server_recv, GNUNET_i2s (&s->target));
856 recv->disconnect = GNUNET_YES;
857 #if MHD_VERSION >= 0x00090E00
858 MHD_set_connection_option (recv->mhd_conn, MHD_CONNECTION_OPTION_TIMEOUT,
861 server_reschedule (s->plugin, recv->mhd_daemon, GNUNET_YES);
867 server_mhd_connection_timeout (struct HTTP_Server_Plugin *plugin, struct Session *s, int to)
869 #if MHD_VERSION >= 0x00090E00
870 /* Setting timeouts for other connections */
871 if (s->server_recv != NULL)
873 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
874 "Setting timeout for %p to %u sec.\n", s->server_recv, to);
875 MHD_set_connection_option (s->server_recv->mhd_conn,
876 MHD_CONNECTION_OPTION_TIMEOUT,
878 server_reschedule (plugin, s->server_recv->mhd_daemon, GNUNET_NO);
880 if (s->server_send != NULL)
882 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
883 "Setting timeout for %p to %u sec.\n", s->server_send, to);
884 MHD_set_connection_option (s->server_send->mhd_conn,
885 MHD_CONNECTION_OPTION_TIMEOUT,
887 server_reschedule (plugin, s->server_send->mhd_daemon, GNUNET_NO);
893 * Parse incoming URL for tag and target
895 * @param plugin plugin
896 * @param url incoming url
897 * @param target where to store the target
898 * @param tag where to store the tag
899 * @return GNUNET_OK on success, GNUNET_SYSERR on error
903 server_parse_url (struct HTTP_Server_Plugin *plugin, const char * url, struct GNUNET_PeerIdentity * target, uint32_t *tag)
905 char * tag_start = NULL;
906 char * tag_end = NULL;
907 char * target_start = NULL;
908 char * separator = NULL;
909 char hash[plugin->peer_id_length+1];
911 unsigned long int ctag;
914 * URL is valid if it is in the form [prefix with (multiple) '/'][peerid[103];tag]*/
919 return GNUNET_SYSERR;
924 separator = strrchr (url, ';');
926 if (NULL == separator)
928 if (DEBUG_URL_PARSE) GNUNET_break (0);
929 return GNUNET_SYSERR;
931 tag_start = separator + 1;
933 if (strlen (tag_start) == 0)
935 /* No tag after separator */
936 if (DEBUG_URL_PARSE) GNUNET_break (0);
937 return GNUNET_SYSERR;
939 ctag = strtoul (tag_start, &tag_end, 10);
942 /* tag == 0 , invalid */
943 if (DEBUG_URL_PARSE) GNUNET_break (0);
944 return GNUNET_SYSERR;
946 if ((ctag == ULONG_MAX) && (ERANGE == errno))
948 /* out of range: > ULONG_MAX */
949 if (DEBUG_URL_PARSE) GNUNET_break (0);
950 return GNUNET_SYSERR;
952 if (ctag > UINT32_MAX)
954 /* out of range: > UINT32_MAX */
955 if (DEBUG_URL_PARSE) GNUNET_break (0);
956 return GNUNET_SYSERR;
958 (*tag) = (uint32_t) ctag;
961 /* no char after tag */
962 if (DEBUG_URL_PARSE) GNUNET_break (0);
963 return GNUNET_SYSERR;
965 if (url[strlen(url)] != tag_end[0])
967 /* there are more not converted chars after tag */
968 if (DEBUG_URL_PARSE) GNUNET_break (0);
969 return GNUNET_SYSERR;
972 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
973 "Found tag `%u' in url\n", (*tag));
975 /* convert peer id */
976 target_start = strrchr (url, '/');
977 if (NULL == target_start)
980 target_start = (char *) url;
983 hash_length = separator - target_start;
984 if (hash_length != plugin->peer_id_length)
986 /* no char after tag */
987 if (DEBUG_URL_PARSE) GNUNET_break (0);
988 return GNUNET_SYSERR;
990 memcpy (hash, target_start, hash_length);
991 hash[hash_length] = '\0';
993 if (GNUNET_OK != GNUNET_CRYPTO_hash_from_string ((const char *) hash, &(target->hashPubKey)))
995 /* hash conversion failed */
996 if (DEBUG_URL_PARSE) GNUNET_break (0);
997 return GNUNET_SYSERR;
1000 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1001 "Found target `%s' in url\n", GNUNET_h2s_full(&target->hashPubKey));
1007 * Lookup a mhd connection and create one if none is found
1009 * @param plugin the plugin handle
1010 * @param mhd_connection the incoming mhd_connection
1011 * @param url incoming requested URL
1012 * @param method PUT or GET
1013 * @return the server connecetion
1015 static struct ServerConnection *
1016 server_lookup_connection (struct HTTP_Server_Plugin *plugin,
1017 struct MHD_Connection *mhd_connection, const char *url,
1020 struct Session *s = NULL;
1021 struct ServerConnection *sc = NULL;
1022 const union MHD_ConnectionInfo *conn_info;
1023 struct GNUNET_ATS_Information ats;
1028 struct GNUNET_PeerIdentity target;
1030 int direction = GNUNET_SYSERR;
1033 conn_info = MHD_get_connection_info (mhd_connection,
1034 MHD_CONNECTION_INFO_CLIENT_ADDRESS);
1035 if ((conn_info->client_addr->sa_family != AF_INET) &&
1036 (conn_info->client_addr->sa_family != AF_INET6))
1038 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1039 "New %s connection from %s\n", method, url);
1041 if (GNUNET_SYSERR == server_parse_url (plugin, url, &target, &tag))
1043 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1044 "Invalid url %s\n", url);
1047 if (0 == strcmp (MHD_HTTP_METHOD_PUT, method))
1048 direction = _RECEIVE;
1049 else if (0 == strcmp (MHD_HTTP_METHOD_GET, method))
1053 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1054 "Invalid method %s connection from %s\n", method, url);
1058 plugin->cur_connections++;
1059 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1060 "New %s connection from %s with tag %u (%u of %u)\n",
1062 GNUNET_i2s (&target), tag,
1063 plugin->cur_connections, plugin->max_connections);
1064 /* find duplicate session */
1068 if ((0 == memcmp (&s->target, &target, sizeof (struct GNUNET_PeerIdentity))) &&
1075 if ((_RECEIVE == direction) && (NULL != s->server_recv))
1077 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1078 "Duplicate PUT connection from `%s' tag %u, dismissing new connection\n",
1079 GNUNET_i2s (&target),
1084 if ((_SEND == direction) && (NULL != s->server_send))
1086 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1087 "Duplicate GET connection from `%s' tag %u, dismissing new connection\n",
1088 GNUNET_i2s (&target),
1095 /* create new session */
1096 switch (conn_info->client_addr->sa_family)
1099 addr = http_common_address_from_socket (plugin->protocol, conn_info->client_addr, sizeof (struct sockaddr_in));
1100 addr_len = http_common_address_get_size (addr);
1101 ats = plugin->env->get_address_type (plugin->env->cls, conn_info->client_addr, sizeof (struct sockaddr_in));
1104 addr = http_common_address_from_socket (plugin->protocol, conn_info->client_addr, sizeof (struct sockaddr_in6));
1105 addr_len = http_common_address_get_size (addr);
1106 ats = plugin->env->get_address_type (plugin->env->cls, conn_info->client_addr, sizeof (struct sockaddr_in6));
1112 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1113 "Creating new session for peer `%s' connecting from `%s'\n",
1114 GNUNET_i2s (&target),
1115 http_common_plugin_address_to_string (NULL, addr, addr_len));
1117 s = GNUNET_malloc (sizeof (struct Session));
1118 memcpy (&s->target, &target, sizeof (struct GNUNET_PeerIdentity));
1121 s->addrlen = addr_len;
1122 s->ats_address_network_type = ats.value;
1123 s->next_receive = GNUNET_TIME_UNIT_ZERO_ABS;
1125 s->server_recv = NULL;
1126 s->server_send = NULL;
1127 s->session_passed = GNUNET_NO;
1128 s->session_ended = GNUNET_NO;
1129 s->connect_in_progress = GNUNET_YES;
1130 server_start_session_timeout(s);
1131 GNUNET_CONTAINER_DLL_insert (plugin->head, plugin->tail, s);
1133 sc = GNUNET_malloc (sizeof (struct ServerConnection));
1134 if (conn_info->client_addr->sa_family == AF_INET)
1135 sc->mhd_daemon = plugin->server_v4;
1136 if (conn_info->client_addr->sa_family == AF_INET6)
1137 sc->mhd_daemon = plugin->server_v6;
1138 sc->mhd_conn = mhd_connection;
1139 sc->direction = direction;
1140 sc->connected = GNUNET_NO;
1142 if (direction == _SEND)
1143 s->server_send = sc;
1144 if (direction == _RECEIVE)
1145 s->server_recv = sc;
1147 if ((NULL != s->server_send) && (NULL != s->server_recv))
1148 s->connect_in_progress = GNUNET_NO; /* PUT and GET are connected */
1150 #if MHD_VERSION >= 0x00090E00
1151 if ((NULL == s->server_recv) || (NULL == s->server_send))
1153 to = (HTTP_SERVER_NOT_VALIDATED_TIMEOUT.rel_value / 1000);
1154 MHD_set_connection_option (mhd_connection, MHD_CONNECTION_OPTION_TIMEOUT, to);
1155 server_reschedule (plugin, sc->mhd_daemon, GNUNET_NO);
1159 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1160 "Session %p for peer `%s' fully connected\n",
1161 s, GNUNET_i2s (&target));
1162 to = (SERVER_SESSION_TIMEOUT.rel_value / 1000);
1163 server_mhd_connection_timeout (plugin, s, to);
1166 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1167 "Setting timeout for %p to %u sec.\n", sc, to);
1174 * Lookup a session for a server connection
1176 * @param plugin the plugin
1177 * @param sc the server connection
1178 * @return the session found or NULL
1180 static struct Session *
1181 server_lookup_session (struct HTTP_Server_Plugin *plugin,
1182 struct ServerConnection * sc)
1186 for (s = plugin->head; NULL != s; s = s->next)
1187 if ((s->server_recv == sc) || (s->server_send == sc))
1193 server_exist_session (struct HTTP_Server_Plugin *plugin, struct Session *s)
1195 struct Session * head;
1197 GNUNET_assert (NULL != plugin);
1198 GNUNET_assert (NULL != s);
1200 for (head = plugin->head; head != NULL; head = head->next)
1210 * Callback called by MHD when it needs data to send
1212 * @param cls current session
1213 * @param pos position in buffer
1214 * @param buf the buffer to write data to
1215 * @param max max number of bytes available in buffer
1216 * @return bytes written to buffer
1219 server_send_callback (void *cls, uint64_t pos, char *buf, size_t max)
1221 struct Session *s = cls;
1222 ssize_t bytes_read = 0;
1223 struct HTTP_Message *msg;
1226 GNUNET_assert (NULL != p);
1227 if (GNUNET_NO == server_exist_session (p, s))
1233 bytes_read = GNUNET_MIN (msg->size - msg->pos,
1235 memcpy (buf, &msg->buf[msg->pos], bytes_read);
1236 msg->pos += bytes_read;
1238 /* removing message */
1239 if (msg->pos == msg->size)
1241 GNUNET_CONTAINER_DLL_remove (s->msg_head, s->msg_tail, msg);
1242 if (NULL != msg->transmit_cont)
1243 msg->transmit_cont (msg->transmit_cont_cls, &s->target, GNUNET_OK,
1244 msg->size, msg->size + msg->overhead);
1250 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, s->plugin->name,
1251 "Sent %u bytes to peer `%s' with session %p \n", bytes_read, GNUNET_i2s (&s->target), s);
1252 GNUNET_asprintf (&stat_txt, "# bytes currently in %s_server buffers", p->protocol);
1253 GNUNET_STATISTICS_update (p->env->stats,
1254 stat_txt, -bytes_read, GNUNET_NO);
1255 GNUNET_free (stat_txt);
1256 GNUNET_asprintf (&stat_txt, "# bytes transmitted via %s_server", p->protocol);
1257 GNUNET_STATISTICS_update (p->env->stats,
1258 stat_txt, bytes_read, GNUNET_NO);
1259 GNUNET_free (stat_txt);
1266 * Callback called by MessageStreamTokenizer when a message has arrived
1268 * @param cls current session as closure
1269 * @param client client
1270 * @param message the message to be forwarded to transport service
1274 server_receive_mst_cb (void *cls, void *client,
1275 const struct GNUNET_MessageHeader *message)
1277 struct Session *s = cls;
1278 struct HTTP_Server_Plugin *plugin = s->plugin;
1279 struct GNUNET_ATS_Information atsi;
1280 struct GNUNET_TIME_Relative delay;
1283 GNUNET_assert (NULL != p);
1284 if (GNUNET_NO == server_exist_session(p, s))
1288 atsi.type = htonl (GNUNET_ATS_NETWORK_TYPE);
1289 atsi.value = s->ats_address_network_type;
1290 GNUNET_break (s->ats_address_network_type != ntohl (GNUNET_ATS_NET_UNSPECIFIED));
1293 delay = plugin->env->receive (plugin->env->cls,
1296 s, s->addr, s->addrlen);
1298 plugin->env->update_address_metrics (plugin->env->cls,
1305 GNUNET_asprintf (&stat_txt, "# bytes received via %s_server", plugin->protocol);
1306 GNUNET_STATISTICS_update (plugin->env->stats,
1307 stat_txt, ntohs (message->size), GNUNET_NO);
1308 GNUNET_free (stat_txt);
1310 s->session_passed = GNUNET_YES;
1311 s->next_receive = GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (), delay);
1312 if (delay.rel_value > 0)
1314 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1315 "Peer `%s' address `%s' next read delayed for %llu ms\n",
1316 GNUNET_i2s (&s->target),
1317 http_common_plugin_address_to_string (NULL, s->addr, s->addrlen),
1320 server_reschedule_session_timeout (s);
1326 * MHD callback for a new incoming connection
1328 * @param cls the plugin handle
1329 * @param mhd_connection the mhd connection
1330 * @param url the requested URL
1331 * @param method GET or PUT
1332 * @param version HTTP version
1333 * @param upload_data upload data
1334 * @param upload_data_size sizeof upload data
1335 * @param httpSessionCache the session cache to remember the connection
1336 * @return MHD_YES if connection is accepted, MHD_NO on reject
1339 server_access_cb (void *cls, struct MHD_Connection *mhd_connection,
1340 const char *url, const char *method, const char *version,
1341 const char *upload_data, size_t * upload_data_size,
1342 void **httpSessionCache)
1344 struct HTTP_Server_Plugin *plugin = cls;
1347 struct ServerConnection *sc = *httpSessionCache;
1349 struct MHD_Response *response;
1351 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1352 _("Access from connection %p (%u of %u) for `%s' `%s' url `%s' with upload data size %u\n"),
1354 plugin->cur_connections, plugin->max_connections,
1355 method, version, url, (*upload_data_size));
1357 GNUNET_assert (cls != NULL);
1360 /* new connection */
1361 sc = server_lookup_connection (plugin, mhd_connection, url, method);
1364 (*httpSessionCache) = sc;
1368 response = MHD_create_response_from_data (strlen (HTTP_ERROR_RESPONSE), HTTP_ERROR_RESPONSE, MHD_NO, MHD_NO);
1369 MHD_add_response_header (response,
1370 MHD_HTTP_HEADER_CONTENT_TYPE,
1372 res = MHD_queue_response (mhd_connection, MHD_HTTP_NOT_FOUND, response);
1373 MHD_destroy_response (response);
1379 /* 'old' connection */
1380 if (NULL == server_lookup_session (plugin, sc))
1382 /* Session was already disconnected */
1387 /* existing connection */
1388 sc = (*httpSessionCache);
1390 GNUNET_assert (NULL != s);
1391 /* connection is to be disconnected */
1392 if (sc->disconnect == GNUNET_YES)
1394 /* Sent HTTP/1.1: 200 OK as response */
1395 response = MHD_create_response_from_data (strlen ("Thank you!"),
1398 res = MHD_queue_response (mhd_connection, MHD_HTTP_OK, response);
1399 MHD_destroy_response (response);
1402 GNUNET_assert (s != NULL);
1404 if (sc->direction == _SEND)
1406 response = MHD_create_response_from_callback (MHD_SIZE_UNKNOWN,
1408 &server_send_callback, s,
1410 MHD_queue_response (mhd_connection, MHD_HTTP_OK, response);
1411 MHD_destroy_response (response);
1414 if (sc->direction == _RECEIVE)
1416 if ((*upload_data_size == 0) && (sc->connected == GNUNET_NO))
1418 /* (*upload_data_size == 0) first callback when header are passed */
1419 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1420 "Session %p / Connection %p: Peer `%s' PUT on address `%s' connected\n",
1422 GNUNET_i2s (&s->target),
1423 http_common_plugin_address_to_string (NULL,
1426 sc->connected = GNUNET_YES;
1429 else if ((*upload_data_size == 0) && (sc->connected == GNUNET_YES))
1431 /* (*upload_data_size == 0) when upload is complete */
1432 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1433 "Session %p / Connection %p: Peer `%s' PUT on address `%s' finished upload\n",
1435 GNUNET_i2s (&s->target),
1436 http_common_plugin_address_to_string (NULL,
1439 sc->connected = GNUNET_NO;
1440 /* Sent HTTP/1.1: 200 OK as PUT Response\ */
1441 response = MHD_create_response_from_data (strlen ("Thank you!"),
1444 res = MHD_queue_response (mhd_connection, MHD_HTTP_OK, response);
1445 MHD_destroy_response (response);
1448 else if ((*upload_data_size > 0) && (sc->connected == GNUNET_YES))
1450 /* (*upload_data_size > 0) for every segment received */
1451 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1452 "Session %p / Connection %p: Peer `%s' PUT on address `%s' received %u bytes\n",
1454 GNUNET_i2s (&s->target),
1455 http_common_plugin_address_to_string (NULL,
1459 struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get ();
1461 if ((s->next_receive.abs_value <= now.abs_value))
1463 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1464 "PUT with %u bytes forwarded to MST\n",
1466 if (s->msg_tk == NULL)
1468 s->msg_tk = GNUNET_SERVER_mst_create (&server_receive_mst_cb, s);
1470 GNUNET_SERVER_mst_receive (s->msg_tk, s, upload_data,
1471 *upload_data_size, GNUNET_NO, GNUNET_NO);
1472 #if MHD_VERSION >= 0x00090E00
1473 server_mhd_connection_timeout (plugin, s, GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT.rel_value / 1000);
1475 (*upload_data_size) = 0;
1479 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1480 "Session %p / Connection %p: no inbound bandwidth available! Next read was delayed by %llu ms\n",
1481 s, sc, now.abs_value - s->next_receive.abs_value);
1496 * Callback from MHD when a connection disconnects
1498 * @param cls closure
1499 * @param connection the disconnected MHD connection
1500 * @param httpSessionCache the pointer to distinguish
1503 server_disconnect_cb (void *cls, struct MHD_Connection *connection,
1504 void **httpSessionCache)
1506 struct ServerConnection *sc = *httpSessionCache;
1507 struct Session *s = NULL;
1508 struct Session *t = NULL;
1509 struct HTTP_Server_Plugin *plugin = NULL;
1511 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, p->name,
1512 "Disconnect for connection %p \n", sc);
1517 if (NULL == (s = server_lookup_session (p, sc)))
1520 GNUNET_assert (NULL != p);
1521 for (t = p->head; t != NULL; t = t->next)
1530 if (sc->direction == _SEND)
1533 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1534 "Peer `%s' connection %p, GET on address `%s' disconnected\n",
1535 GNUNET_i2s (&s->target), s->server_send,
1536 http_common_plugin_address_to_string (NULL, s->addr, s->addrlen));
1537 s->server_send = NULL;
1538 if (NULL != (s->server_recv))
1540 s->server_recv->disconnect = GNUNET_YES;
1541 GNUNET_assert (NULL != s->server_recv->mhd_conn);
1542 #if MHD_VERSION >= 0x00090E00
1543 MHD_set_connection_option (s->server_recv->mhd_conn, MHD_CONNECTION_OPTION_TIMEOUT,
1546 server_reschedule (plugin, s->server_recv->mhd_daemon, GNUNET_NO);
1549 if (sc->direction == _RECEIVE)
1551 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1552 "Peer `%s' connection %p PUT on address `%s' disconnected\n",
1553 GNUNET_i2s (&s->target), s->server_recv,
1554 http_common_plugin_address_to_string (NULL, s->addr, s->addrlen));
1555 s->server_recv = NULL;
1556 /* Do not terminate session when PUT disconnects
1557 if (NULL != (s->server_send))
1559 s->server_send->disconnect = GNUNET_YES;
1560 GNUNET_assert (NULL != s->server_send->mhd_conn);
1561 #if MHD_VERSION >= 0x00090E00
1562 MHD_set_connection_option (s->server_send->mhd_conn, MHD_CONNECTION_OPTION_TIMEOUT,
1565 server_reschedule (plugin, s->server_send->mhd_daemon, GNUNET_NO);
1567 if (s->msg_tk != NULL)
1569 GNUNET_SERVER_mst_destroy (s->msg_tk);
1575 plugin->cur_connections--;
1577 if ((s->server_send == NULL) && (s->server_recv == NULL))
1579 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1580 "Peer `%s' on address `%s' disconnected\n",
1581 GNUNET_i2s (&s->target),
1582 http_common_plugin_address_to_string (NULL, s->addr, s->addrlen));
1584 if ((GNUNET_YES == s->session_passed) && (GNUNET_NO == s->session_ended))
1586 /* Notify transport immediately that this session is invalid */
1587 s->session_ended = GNUNET_YES;
1588 plugin->env->session_end (plugin->env->cls, &s->target, s);
1590 server_delete_session (s);
1597 * Check if incoming connection is accepted.
1599 * @param cls plugin as closure
1600 * @param addr address of incoming connection
1601 * @param addr_len address length of incoming connection
1602 * @return MHD_YES if connection is accepted, MHD_NO if connection is rejected
1606 server_accept_cb (void *cls, const struct sockaddr *addr, socklen_t addr_len)
1608 struct HTTP_Server_Plugin *plugin = cls;
1610 if (plugin->cur_connections <= plugin->max_connections)
1612 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1613 _("Accepting connection (%u of %u) from `%s'\n"),
1614 plugin->cur_connections, plugin->max_connections,
1615 GNUNET_a2s (addr, addr_len));
1620 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, plugin->name,
1621 _("Server reached maximum number connections (%u), rejecting new connection\n"),
1622 plugin->max_connections);
1628 server_log (void *arg, const char *fmt, va_list ap)
1632 vsnprintf (text, sizeof (text), fmt, ap);
1634 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Server: %s\n", text);
1639 * Call MHD IPv4 to process pending requests and then go back
1640 * and schedule the next run.
1641 * @param cls plugin as closure
1642 * @param tc task context
1645 server_v4_run (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1647 struct HTTP_Server_Plugin *plugin = cls;
1649 GNUNET_assert (cls != NULL);
1651 plugin->server_v4_task = GNUNET_SCHEDULER_NO_TASK;
1652 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1655 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1656 "Running IPv4 server\n");
1658 plugin->server_v4_immediately = GNUNET_NO;
1659 GNUNET_assert (MHD_YES == MHD_run (plugin->server_v4));
1660 server_reschedule (plugin, plugin->server_v4, GNUNET_NO);
1665 * Call MHD IPv6 to process pending requests and then go back
1666 * and schedule the next run.
1667 * @param cls plugin as closure
1668 * @param tc task context
1671 server_v6_run (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1673 struct HTTP_Server_Plugin *plugin = cls;
1675 GNUNET_assert (cls != NULL);
1676 plugin->server_v6_task = GNUNET_SCHEDULER_NO_TASK;
1677 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1680 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1681 "Running IPv6 server\n");
1683 plugin->server_v6_immediately = GNUNET_NO;
1684 GNUNET_assert (MHD_YES == MHD_run (plugin->server_v6));
1685 server_reschedule (plugin, plugin->server_v6, GNUNET_NO);
1689 #define UNSIGNED_MHD_LONG_LONG unsigned MHD_LONG_LONG
1692 * Function that queries MHD's select sets and
1693 * starts the task waiting for them.
1695 * @param plugin plugin
1696 * @param daemon_handle the MHD daemon handle
1697 * @return gnunet task identifier
1699 static GNUNET_SCHEDULER_TaskIdentifier
1700 server_schedule (struct HTTP_Server_Plugin *plugin,
1701 struct MHD_Daemon *daemon_handle,
1704 GNUNET_SCHEDULER_TaskIdentifier ret;
1708 struct GNUNET_NETWORK_FDSet *wrs;
1709 struct GNUNET_NETWORK_FDSet *wws;
1710 struct GNUNET_NETWORK_FDSet *wes;
1712 UNSIGNED_MHD_LONG_LONG timeout;
1713 static unsigned long long last_timeout = 0;
1716 struct GNUNET_TIME_Relative tv;
1718 if (GNUNET_YES == plugin->in_shutdown)
1719 return GNUNET_SCHEDULER_NO_TASK;
1721 ret = GNUNET_SCHEDULER_NO_TASK;
1725 wrs = GNUNET_NETWORK_fdset_create ();
1726 wes = GNUNET_NETWORK_fdset_create ();
1727 wws = GNUNET_NETWORK_fdset_create ();
1729 GNUNET_assert (MHD_YES == MHD_get_fdset (daemon_handle, &rs, &ws, &es, &max));
1730 haveto = MHD_get_timeout (daemon_handle, &timeout);
1731 if (haveto == MHD_YES)
1733 if (timeout != last_timeout)
1736 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1737 "SELECT Timeout changed from %llu to %llu\n",
1738 last_timeout, timeout);
1739 last_timeout = timeout;
1741 if (timeout <= GNUNET_TIME_UNIT_SECONDS.rel_value)
1742 tv.rel_value = (uint64_t) timeout;
1744 tv = GNUNET_TIME_UNIT_SECONDS;
1747 tv = GNUNET_TIME_UNIT_SECONDS;
1748 /* Force immediate run, since we have outbound data to send */
1749 if (now == GNUNET_YES)
1750 tv = GNUNET_TIME_UNIT_MILLISECONDS;
1751 GNUNET_NETWORK_fdset_copy_native (wrs, &rs, max + 1);
1752 GNUNET_NETWORK_fdset_copy_native (wws, &ws, max + 1);
1753 GNUNET_NETWORK_fdset_copy_native (wes, &es, max + 1);
1755 if (daemon_handle == plugin->server_v4)
1757 if (plugin->server_v4_task != GNUNET_SCHEDULER_NO_TASK)
1759 GNUNET_SCHEDULER_cancel (plugin->server_v4_task);
1760 plugin->server_v4_task = GNUNET_SCHEDULER_NO_TASK;
1763 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1764 "Scheduling IPv4 server task in %llu ms\n", tv);
1767 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1769 &server_v4_run, plugin);
1771 if (daemon_handle == plugin->server_v6)
1773 if (plugin->server_v6_task != GNUNET_SCHEDULER_NO_TASK)
1775 GNUNET_SCHEDULER_cancel (plugin->server_v6_task);
1776 plugin->server_v6_task = GNUNET_SCHEDULER_NO_TASK;
1779 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1780 "Scheduling IPv6 server task in %llu ms\n", tv);
1783 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1785 &server_v6_run, plugin);
1787 GNUNET_NETWORK_fdset_destroy (wrs);
1788 GNUNET_NETWORK_fdset_destroy (wws);
1789 GNUNET_NETWORK_fdset_destroy (wes);
1796 * Load ssl certificate from file
1798 * @param file filename
1799 * @return content of the file
1802 server_load_file (const char *file)
1804 struct GNUNET_DISK_FileHandle *gn_file;
1808 if (GNUNET_OK != GNUNET_DISK_file_size (file,
1809 &fsize, GNUNET_NO, GNUNET_YES))
1811 text = GNUNET_malloc (fsize + 1);
1813 GNUNET_DISK_file_open (file, GNUNET_DISK_OPEN_READ,
1814 GNUNET_DISK_PERM_USER_READ);
1815 if (gn_file == NULL)
1820 if (GNUNET_SYSERR == GNUNET_DISK_file_read (gn_file, text, fsize))
1823 GNUNET_DISK_file_close (gn_file);
1827 GNUNET_DISK_file_close (gn_file);
1835 * Load ssl certificate
1837 * @param plugin the plugin
1838 * @return GNUNET_OK on success, GNUNET_SYSERR on failure
1841 server_load_certificate (struct HTTP_Server_Plugin *plugin)
1843 int res = GNUNET_OK;
1849 /* Get crypto init string from config
1850 * If not present just use default values */
1853 GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg,
1858 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
1859 "Failed to get servicehome!\n");
1860 return GNUNET_SYSERR;
1865 GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg,
1868 &plugin->crypto_init))
1869 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1870 "Using crypto init string `%s'\n",
1871 plugin->crypto_init);
1873 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1874 "Using default crypto init string \n");
1877 GNUNET_CONFIGURATION_get_value_filename (plugin->env->cfg, plugin->name,
1878 "KEY_FILE", &key_file))
1881 GNUNET_asprintf (&key_file, "%s/%s", sh, "https_key.key");
1886 GNUNET_CONFIGURATION_get_value_filename (plugin->env->cfg, plugin->name,
1887 "CERT_FILE", &cert_file))
1890 GNUNET_asprintf (&cert_file, "%s/%s", sh, "https_cert.crt");
1893 /* read key & certificates from file */
1894 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1895 "Trying to loading TLS certificate from key-file `%s' cert-file`%s'\n",
1896 key_file, cert_file);
1898 plugin->key = server_load_file (key_file);
1899 plugin->cert = server_load_file (cert_file);
1901 if ((plugin->key == NULL) || (plugin->cert == NULL))
1903 struct GNUNET_OS_Process *cert_creation;
1905 GNUNET_free_non_null (plugin->key);
1907 GNUNET_free_non_null (plugin->cert);
1908 plugin->cert = NULL;
1910 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1911 "No usable TLS certificate found, creating certificate\n");
1914 GNUNET_OS_start_process (GNUNET_NO, GNUNET_OS_INHERIT_STD_OUT_AND_ERR, NULL, NULL,
1915 "gnunet-transport-certificate-creation",
1916 "gnunet-transport-certificate-creation",
1917 key_file, cert_file, NULL);
1918 if (cert_creation == NULL)
1920 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
1922 ("Could not create a new TLS certificate, program `gnunet-transport-certificate-creation' could not be started!\n"));
1923 GNUNET_free (key_file);
1924 GNUNET_free (cert_file);
1926 GNUNET_free_non_null (plugin->key);
1928 GNUNET_free_non_null (plugin->cert);
1929 plugin->cert = NULL;
1930 GNUNET_free_non_null (plugin->crypto_init);
1931 plugin->crypto_init = NULL;
1933 return GNUNET_SYSERR;
1935 GNUNET_assert (GNUNET_OK == GNUNET_OS_process_wait (cert_creation));
1936 GNUNET_OS_process_destroy (cert_creation);
1938 plugin->key = server_load_file (key_file);
1939 plugin->cert = server_load_file (cert_file);
1942 if ((plugin->key == NULL) || (plugin->cert == NULL))
1944 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
1946 ("No usable TLS certificate found and creating one failed!\n"),
1948 GNUNET_free (key_file);
1949 GNUNET_free (cert_file);
1951 GNUNET_free_non_null (plugin->key);
1953 GNUNET_free_non_null (plugin->cert);
1954 plugin->cert = NULL;
1955 GNUNET_free_non_null (plugin->crypto_init);
1956 plugin->crypto_init = NULL;
1958 return GNUNET_SYSERR;
1960 GNUNET_free (key_file);
1961 GNUNET_free (cert_file);
1962 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "TLS certificate loaded\n");
1969 * Start the HTTP server
1971 * @param plugin the plugin handle
1972 * @return GNUNET_OK on success, GNUNET_SYSERR on failure
1975 server_start (struct HTTP_Server_Plugin *plugin)
1977 unsigned int timeout;
1978 GNUNET_assert (NULL != plugin);
1981 if (GNUNET_SYSERR == server_load_certificate (plugin))
1983 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
1984 "Could not load or create server certificate! Loading plugin failed!\n");
1985 return GNUNET_SYSERR;
1990 #if MHD_VERSION >= 0x00090E00
1991 timeout = HTTP_SERVER_NOT_VALIDATED_TIMEOUT.rel_value / 1000;
1992 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1993 "MHD can set timeout per connection! Default time out %u sec.\n",
1996 timeout = SERVER_SESSION_TIMEOUT.rel_value / 1000;
1997 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, plugin->name,
1998 "MHD cannot set timeout per connection! Default time out %u sec.\n",
2001 plugin->server_v4 = NULL;
2002 if (plugin->use_ipv4 == GNUNET_YES)
2004 plugin->server_v4 = MHD_start_daemon (
2011 MHD_NO_FLAG, plugin->port,
2012 &server_accept_cb, plugin,
2013 &server_access_cb, plugin,
2014 MHD_OPTION_SOCK_ADDR,
2015 (struct sockaddr_in *)
2016 plugin->server_addr_v4,
2017 MHD_OPTION_CONNECTION_LIMIT,
2019 plugin->max_connections,
2021 MHD_OPTION_HTTPS_PRIORITIES,
2022 plugin->crypto_init,
2023 MHD_OPTION_HTTPS_MEM_KEY,
2025 MHD_OPTION_HTTPS_MEM_CERT,
2028 MHD_OPTION_CONNECTION_TIMEOUT,
2030 MHD_OPTION_CONNECTION_MEMORY_LIMIT,
2032 GNUNET_SERVER_MAX_MESSAGE_SIZE),
2033 MHD_OPTION_NOTIFY_COMPLETED,
2034 &server_disconnect_cb, plugin,
2035 MHD_OPTION_EXTERNAL_LOGGER,
2036 server_log, NULL, MHD_OPTION_END);
2038 plugin->server_v6 = NULL;
2039 if (plugin->use_ipv6 == GNUNET_YES)
2041 plugin->server_v6 = MHD_start_daemon (
2048 MHD_USE_IPv6, plugin->port,
2049 &server_accept_cb, plugin,
2050 &server_access_cb, plugin,
2051 MHD_OPTION_SOCK_ADDR,
2052 (struct sockaddr_in6 *)
2053 plugin->server_addr_v6,
2054 MHD_OPTION_CONNECTION_LIMIT,
2056 plugin->max_connections,
2058 MHD_OPTION_HTTPS_PRIORITIES,
2059 plugin->crypto_init,
2060 MHD_OPTION_HTTPS_MEM_KEY,
2062 MHD_OPTION_HTTPS_MEM_CERT,
2065 MHD_OPTION_CONNECTION_TIMEOUT,
2067 MHD_OPTION_CONNECTION_MEMORY_LIMIT,
2069 GNUNET_SERVER_MAX_MESSAGE_SIZE),
2070 MHD_OPTION_NOTIFY_COMPLETED,
2071 &server_disconnect_cb, plugin,
2072 MHD_OPTION_EXTERNAL_LOGGER,
2073 server_log, NULL, MHD_OPTION_END);
2077 if ((plugin->use_ipv4 == GNUNET_YES) && (plugin->server_v4 == NULL))
2079 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
2080 "Failed to start %s IPv4 server component on port %u\n",
2081 plugin->name, plugin->port);
2082 return GNUNET_SYSERR;
2084 server_reschedule (plugin, plugin->server_v4, GNUNET_NO);
2086 if ((plugin->use_ipv6 == GNUNET_YES) && (plugin->server_v6 == NULL))
2088 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
2089 "Failed to start %s IPv6 server component on port %u\n",
2090 plugin->name, plugin->port);
2091 return GNUNET_SYSERR;
2093 server_reschedule (plugin, plugin->server_v6, GNUNET_NO);
2094 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2095 "%s server component started on port %u\n", plugin->name,
2102 server_stop (struct HTTP_Server_Plugin *plugin)
2104 if (plugin->server_v4 != NULL)
2106 MHD_stop_daemon (plugin->server_v4);
2107 plugin->server_v4 = NULL;
2109 if ( plugin->server_v6 != NULL)
2111 MHD_stop_daemon (plugin->server_v6);
2112 plugin->server_v6 = NULL;
2116 if (plugin->server_v4_task != GNUNET_SCHEDULER_NO_TASK)
2118 GNUNET_SCHEDULER_cancel (plugin->server_v4_task);
2119 plugin->server_v4_task = GNUNET_SCHEDULER_NO_TASK;
2122 if (plugin->server_v6_task != GNUNET_SCHEDULER_NO_TASK)
2124 GNUNET_SCHEDULER_cancel (plugin->server_v6_task);
2125 plugin->server_v6_task = GNUNET_SCHEDULER_NO_TASK;
2130 GNUNET_free_non_null (plugin->crypto_init);
2131 GNUNET_free_non_null (plugin->cert);
2132 GNUNET_free_non_null (plugin->key);
2135 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2136 "%s server component stopped\n", plugin->name);
2141 * Add an address to the server's set of addresses and notify transport
2143 * @param cls the plugin handle
2144 * @param add_remove GNUNET_YES on add, GNUNET_NO on remove
2145 * @param addr the address
2146 * @param addrlen address length
2149 server_add_address (void *cls, int add_remove, const struct sockaddr *addr,
2152 struct HTTP_Server_Plugin *plugin = cls;
2153 struct HttpAddressWrapper *w = NULL;
2155 w = GNUNET_malloc (sizeof (struct HttpAddressWrapper));
2156 w->addr = http_common_address_from_socket (plugin->protocol, addr, addrlen);
2157 if (NULL == w->addr)
2162 w->addrlen = http_common_address_get_size (w->addr);
2164 GNUNET_CONTAINER_DLL_insert(plugin->addr_head, plugin->addr_tail, w);
2165 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2166 "Notifying transport to add address `%s'\n",
2167 http_common_plugin_address_to_string(NULL, w->addr, w->addrlen));
2169 plugin->env->notify_address (plugin->env->cls, add_remove, w->addr, w->addrlen, "https_client");
2171 plugin->env->notify_address (plugin->env->cls, add_remove, w->addr, w->addrlen, "http_client");
2177 * Remove an address from the server's set of addresses and notify transport
2179 * @param cls the plugin handle
2180 * @param add_remove GNUNET_YES on add, GNUNET_NO on remove
2181 * @param addr the address
2182 * @param addrlen address length
2185 server_remove_address (void *cls, int add_remove, const struct sockaddr *addr,
2188 struct HTTP_Server_Plugin *plugin = cls;
2189 struct HttpAddressWrapper *w = plugin->addr_head;
2191 void * saddr = http_common_address_from_socket (plugin->protocol, addr, addrlen);
2194 saddr_len = http_common_address_get_size (saddr);
2198 if (GNUNET_YES == http_common_cmp_addresses(w->addr, w->addrlen, saddr, saddr_len))
2202 GNUNET_free (saddr);
2207 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2208 "Notifying transport to remove address `%s'\n",
2209 http_common_plugin_address_to_string (NULL, w->addr, w->addrlen));
2210 GNUNET_CONTAINER_DLL_remove (plugin->addr_head, plugin->addr_tail, w);
2212 plugin->env->notify_address (plugin->env->cls, add_remove, w->addr, w->addrlen, "https_client");
2214 plugin->env->notify_address (plugin->env->cls, add_remove, w->addr, w->addrlen, "http_client");
2216 GNUNET_free (w->addr);
2223 * Our external IP address/port mapping has changed.
2225 * @param cls closure, the 'struct LocalAddrList'
2226 * @param add_remove GNUNET_YES to mean the new public IP address, GNUNET_NO to mean
2227 * the previous (now invalid) one
2228 * @param addr either the previous or the new public IP address
2229 * @param addrlen actual lenght of the address
2232 server_nat_port_map_callback (void *cls, int add_remove, const struct sockaddr *addr,
2235 GNUNET_assert (cls != NULL);
2236 struct HTTP_Server_Plugin *plugin = cls;
2238 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2239 "NAT called to %s address `%s'\n",
2240 (add_remove == GNUNET_NO) ? "remove" : "add",
2241 GNUNET_a2s (addr, addrlen));
2243 if (AF_INET == addr->sa_family)
2245 struct sockaddr_in *s4 = (struct sockaddr_in *) addr;
2247 if (GNUNET_NO == plugin->use_ipv4)
2250 if ((NULL != plugin->server_addr_v4) &&
2251 (0 != memcmp (&plugin->server_addr_v4->sin_addr,
2252 &s4->sin_addr, sizeof (struct in_addr))))
2254 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2255 "Skipping address `%s' (not bindto address)\n",
2256 GNUNET_a2s (addr, addrlen));
2261 if (AF_INET6 == addr->sa_family)
2263 struct sockaddr_in6 *s6 = (struct sockaddr_in6 *) addr;
2264 if (GNUNET_NO == plugin->use_ipv6)
2267 if ((NULL != plugin->server_addr_v6) &&
2268 (0 != memcmp (&plugin->server_addr_v6->sin6_addr,
2269 &s6->sin6_addr, sizeof (struct in6_addr))))
2271 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2272 "Skipping address `%s' (not bindto address)\n",
2273 GNUNET_a2s (addr, addrlen));
2281 server_add_address (cls, add_remove, addr, addrlen);
2284 server_remove_address (cls, add_remove, addr, addrlen);
2291 * Get valid server addresses
2293 * @param plugin the plugin handle
2294 * @param serviceName the servicename
2295 * @param cfg configuration handle
2296 * @param addrs addresses
2297 * @param addr_lens address length
2298 * @return number of addresses
2301 server_get_addresses (struct HTTP_Server_Plugin *plugin,
2302 const char *serviceName,
2303 const struct GNUNET_CONFIGURATION_Handle *cfg,
2304 struct sockaddr ***addrs, socklen_t ** addr_lens)
2307 unsigned long long port;
2308 struct addrinfo hints;
2309 struct addrinfo *res;
2310 struct addrinfo *pos;
2311 struct addrinfo *next;
2315 struct sockaddr **saddrs;
2316 socklen_t *saddrlens;
2322 disablev6 = !plugin->use_ipv6;
2325 if (GNUNET_CONFIGURATION_have_value (cfg, serviceName, "PORT"))
2327 GNUNET_break (GNUNET_OK ==
2328 GNUNET_CONFIGURATION_get_value_number (cfg, serviceName,
2332 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2334 ("Require valid port number for service in configuration!\n"));
2335 return GNUNET_SYSERR;
2340 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, plugin->name,
2341 "Starting in listen only mode\n");
2342 return -1; /* listen only */
2346 if (GNUNET_CONFIGURATION_have_value (cfg, serviceName, "BINDTO"))
2348 GNUNET_break (GNUNET_OK ==
2349 GNUNET_CONFIGURATION_get_value_string (cfg, serviceName,
2350 "BINDTO", &hostname));
2355 if (hostname != NULL)
2357 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2358 "Resolving `%s' since that is where `%s' will bind to.\n",
2359 hostname, serviceName);
2360 memset (&hints, 0, sizeof (struct addrinfo));
2362 hints.ai_family = AF_INET;
2363 if ((0 != (ret = getaddrinfo (hostname, NULL, &hints, &res))) ||
2366 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Failed to resolve `%s': %s\n"),
2367 hostname, gai_strerror (ret));
2368 GNUNET_free (hostname);
2369 return GNUNET_SYSERR;
2373 while (NULL != (pos = next))
2375 next = pos->ai_next;
2376 if ((disablev6) && (pos->ai_family == AF_INET6))
2382 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2383 _("Failed to find %saddress for `%s'.\n"),
2384 disablev6 ? "IPv4 " : "", hostname);
2386 GNUNET_free (hostname);
2387 return GNUNET_SYSERR;
2390 saddrs = GNUNET_malloc ((resi + 1) * sizeof (struct sockaddr *));
2391 saddrlens = GNUNET_malloc ((resi + 1) * sizeof (socklen_t));
2394 while (NULL != (pos = next))
2396 next = pos->ai_next;
2397 if ((disablev6) && (pos->ai_family == AF_INET6))
2399 if ((pos->ai_protocol != IPPROTO_TCP) && (pos->ai_protocol != 0))
2400 continue; /* not TCP */
2401 if ((pos->ai_socktype != SOCK_STREAM) && (pos->ai_socktype != 0))
2402 continue; /* huh? */
2403 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2404 "Service will bind to `%s'\n", GNUNET_a2s (pos->ai_addr,
2406 if (pos->ai_family == AF_INET)
2408 GNUNET_assert (pos->ai_addrlen == sizeof (struct sockaddr_in));
2409 saddrlens[i] = pos->ai_addrlen;
2410 saddrs[i] = GNUNET_malloc (saddrlens[i]);
2411 memcpy (saddrs[i], pos->ai_addr, saddrlens[i]);
2412 ((struct sockaddr_in *) saddrs[i])->sin_port = htons (port);
2416 GNUNET_assert (pos->ai_family == AF_INET6);
2417 GNUNET_assert (pos->ai_addrlen == sizeof (struct sockaddr_in6));
2418 saddrlens[i] = pos->ai_addrlen;
2419 saddrs[i] = GNUNET_malloc (saddrlens[i]);
2420 memcpy (saddrs[i], pos->ai_addr, saddrlens[i]);
2421 ((struct sockaddr_in6 *) saddrs[i])->sin6_port = htons (port);
2425 GNUNET_free (hostname);
2431 /* will bind against everything, just set port */
2437 saddrs = GNUNET_malloc ((resi + 1) * sizeof (struct sockaddr *));
2438 saddrlens = GNUNET_malloc ((resi + 1) * sizeof (socklen_t));
2440 saddrlens[i] = sizeof (struct sockaddr_in);
2441 saddrs[i] = GNUNET_malloc (saddrlens[i]);
2442 #if HAVE_SOCKADDR_IN_SIN_LEN
2443 ((struct sockaddr_in *) saddrs[i])->sin_len = saddrlens[i];
2445 ((struct sockaddr_in *) saddrs[i])->sin_family = AF_INET;
2446 ((struct sockaddr_in *) saddrs[i])->sin_port = htons (port);
2452 saddrs = GNUNET_malloc ((resi + 1) * sizeof (struct sockaddr *));
2453 saddrlens = GNUNET_malloc ((resi + 1) * sizeof (socklen_t));
2455 saddrlens[i] = sizeof (struct sockaddr_in6);
2456 saddrs[i] = GNUNET_malloc (saddrlens[i]);
2457 #if HAVE_SOCKADDR_IN_SIN_LEN
2458 ((struct sockaddr_in6 *) saddrs[i])->sin6_len = saddrlens[0];
2460 ((struct sockaddr_in6 *) saddrs[i])->sin6_family = AF_INET6;
2461 ((struct sockaddr_in6 *) saddrs[i])->sin6_port = htons (port);
2463 saddrlens[i] = sizeof (struct sockaddr_in);
2464 saddrs[i] = GNUNET_malloc (saddrlens[i]);
2465 #if HAVE_SOCKADDR_IN_SIN_LEN
2466 ((struct sockaddr_in *) saddrs[i])->sin_len = saddrlens[1];
2468 ((struct sockaddr_in *) saddrs[i])->sin_family = AF_INET;
2469 ((struct sockaddr_in *) saddrs[i])->sin_port = htons (port);
2473 *addr_lens = saddrlens;
2479 * Ask NAT for addresses
2481 * @param plugin the plugin handle
2484 server_start_report_addresses (struct HTTP_Server_Plugin *plugin)
2486 int res = GNUNET_OK;
2487 struct sockaddr **addrs;
2488 socklen_t *addrlens;
2490 res = server_get_addresses (plugin,
2491 plugin->name, plugin->env->cfg,
2493 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2494 _("Found %u addresses to report to NAT service\n"), res);
2496 if (GNUNET_SYSERR == res)
2503 GNUNET_NAT_register (plugin->env->cfg, GNUNET_YES, plugin->port,
2505 (const struct sockaddr **) addrs, addrlens,
2506 &server_nat_port_map_callback, NULL, plugin);
2510 GNUNET_assert (addrs[res] != NULL);
2511 GNUNET_free (addrs[res]);
2513 GNUNET_free_non_null (addrs);
2514 GNUNET_free_non_null (addrlens);
2519 * Stop NAT for addresses
2521 * @param plugin the plugin handle
2524 server_stop_report_addresses (struct HTTP_Server_Plugin *plugin)
2526 /* Stop NAT handle */
2527 if (NULL != plugin->nat)
2528 GNUNET_NAT_unregister (plugin->nat);
2530 /* Clean up addresses */
2531 struct HttpAddressWrapper *w;
2533 while (plugin->addr_head != NULL)
2535 w = plugin->addr_head;
2536 GNUNET_CONTAINER_DLL_remove (plugin->addr_head, plugin->addr_tail, w);
2537 GNUNET_free (w->addr);
2544 * Check if IPv6 supported on this system
2546 * @param plugin the plugin handle
2547 * @return GNUNET_YES on success, else GNUNET_NO
2550 server_check_ipv6_support (struct HTTP_Server_Plugin *plugin)
2552 struct GNUNET_NETWORK_Handle *desc = NULL;
2553 int res = GNUNET_NO;
2555 /* Probe IPv6 support */
2556 desc = GNUNET_NETWORK_socket_create (PF_INET6, SOCK_STREAM, 0);
2559 if ((errno == ENOBUFS) || (errno == ENOMEM) || (errno == ENFILE) ||
2562 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "socket");
2564 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, plugin->name,
2566 ("Disabling IPv6 since it is not supported on this system!\n"));
2571 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (desc));
2575 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2576 "Testing IPv6 on this system: %s\n",
2577 (res == GNUNET_YES) ? "successful" : "failed");
2583 * Notify server about our external hostname
2586 * @param tc task context (unused)
2589 server_notify_external_hostname (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2591 struct HTTP_Server_Plugin *plugin = cls;
2593 plugin->notify_ext_task = GNUNET_SCHEDULER_NO_TASK;
2595 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
2600 GNUNET_asprintf(&plugin->ext_addr, "%s%s://%s", plugin->protocol,
2601 (GNUNET_YES == plugin->verify_external_hostname) ? "+" : "",
2602 plugin->external_hostname);
2604 GNUNET_asprintf(&plugin->ext_addr, "%s://%s", plugin->protocol,
2605 plugin->external_hostname);
2608 plugin->ext_addr_len = strlen (plugin->ext_addr) + 1;
2609 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2610 "Notifying transport about external hostname address `%s'\n", plugin->ext_addr);
2613 if (GNUNET_YES == plugin->verify_external_hostname)
2614 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, plugin->name,
2615 "Enabling SSL verification for external hostname address `%s'\n", plugin->ext_addr);
2616 plugin->env->notify_address (plugin->env->cls, GNUNET_YES,
2617 plugin->ext_addr, plugin->ext_addr_len,
2620 plugin->env->notify_address (plugin->env->cls, GNUNET_YES,
2621 plugin->ext_addr, plugin->ext_addr_len,
2628 * Configure the plugin
2630 * @param plugin plugin handle
2631 * @return GNUNET_OK on success, GNUNET_SYSERR on failure
2634 server_configure_plugin (struct HTTP_Server_Plugin *plugin)
2636 unsigned long long port;
2637 unsigned long long max_connections;
2638 char *bind4_address = NULL;
2639 char *bind6_address = NULL;
2642 if (GNUNET_CONFIGURATION_have_value
2643 (plugin->env->cfg, plugin->name, "USE_IPv4"))
2646 GNUNET_CONFIGURATION_get_value_yesno (plugin->env->cfg, plugin->name,
2650 plugin->use_ipv4 = GNUNET_YES;
2651 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2652 _("IPv4 support is %s\n"),
2653 (plugin->use_ipv4 == GNUNET_YES) ? "enabled" : "disabled");
2656 if (GNUNET_CONFIGURATION_have_value
2657 (plugin->env->cfg, plugin->name, "USE_IPv6"))
2660 GNUNET_CONFIGURATION_get_value_yesno (plugin->env->cfg, plugin->name,
2664 plugin->use_ipv6 = GNUNET_YES;
2665 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2666 _("IPv6 support is %s\n"),
2667 (plugin->use_ipv6 == GNUNET_YES) ? "enabled" : "disabled");
2669 if ((plugin->use_ipv4 == GNUNET_NO) && (plugin->use_ipv6 == GNUNET_NO))
2671 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
2673 ("Neither IPv4 nor IPv6 are enabled! Fix in configuration\n"),
2675 return GNUNET_SYSERR;
2678 /* Reading port number from config file */
2680 GNUNET_CONFIGURATION_get_value_number (plugin->env->cfg, plugin->name,
2681 "PORT", &port)) || (port > 65535))
2683 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
2684 _("Port is required! Fix in configuration\n"),
2686 return GNUNET_SYSERR;
2688 plugin->port = port;
2690 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2691 _("Using port %u\n"), plugin->port);
2693 if ((plugin->use_ipv4 == GNUNET_YES) &&
2694 (GNUNET_YES == GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg,
2695 plugin->name, "BINDTO", &bind4_address)))
2697 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2698 "Binding %s plugin to specific IPv4 address: `%s'\n",
2699 plugin->protocol, bind4_address);
2700 plugin->server_addr_v4 = GNUNET_malloc (sizeof (struct sockaddr_in));
2701 if (1 != inet_pton (AF_INET, bind4_address,
2702 &plugin->server_addr_v4->sin_addr))
2704 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
2706 ("Specific IPv4 address `%s' in configuration file is invalid!\n"),
2708 GNUNET_free (bind4_address);
2709 GNUNET_free (plugin->server_addr_v4);
2710 plugin->server_addr_v4 = NULL;
2711 return GNUNET_SYSERR;
2715 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2716 _("Binding to IPv4 address %s\n"), bind4_address);
2717 plugin->server_addr_v4->sin_family = AF_INET;
2718 plugin->server_addr_v4->sin_port = htons (plugin->port);
2720 GNUNET_free (bind4_address);
2723 if ((plugin->use_ipv6 == GNUNET_YES) &&
2725 GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg, plugin->name,
2726 "BINDTO6", &bind6_address)))
2728 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2729 "Binding %s plugin to specific IPv6 address: `%s'\n",
2730 plugin->protocol, bind6_address);
2731 plugin->server_addr_v6 = GNUNET_malloc (sizeof (struct sockaddr_in6));
2733 inet_pton (AF_INET6, bind6_address, &plugin->server_addr_v6->sin6_addr))
2735 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
2737 ("Specific IPv6 address `%s' in configuration file is invalid!\n"),
2739 GNUNET_free (bind6_address);
2740 GNUNET_free (plugin->server_addr_v6);
2741 plugin->server_addr_v6 = NULL;
2742 return GNUNET_SYSERR;
2746 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2747 _("Binding to IPv6 address %s\n"), bind6_address);
2748 plugin->server_addr_v6->sin6_family = AF_INET6;
2749 plugin->server_addr_v6->sin6_port = htons (plugin->port);
2751 GNUNET_free (bind6_address);
2754 plugin->verify_external_hostname = GNUNET_NO;
2756 plugin->verify_external_hostname = GNUNET_CONFIGURATION_get_value_yesno (plugin->env->cfg, plugin->name,
2757 "VERIFY_EXTERNAL_HOSTNAME");
2758 if (GNUNET_SYSERR == plugin->verify_external_hostname)
2759 plugin->verify_external_hostname = GNUNET_NO;
2762 if (GNUNET_YES == GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg, plugin->name,
2763 "EXTERNAL_HOSTNAME", &plugin->external_hostname))
2766 if (NULL != strstr(plugin->external_hostname, "://"))
2768 tmp = strdup(&strstr(plugin->external_hostname, "://")[3]);
2769 GNUNET_free (plugin->external_hostname);
2770 plugin->external_hostname = tmp;
2773 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2774 _("Using external hostname `%s'\n"), plugin->external_hostname);
2775 plugin->notify_ext_task = GNUNET_SCHEDULER_add_now (&server_notify_external_hostname, plugin);
2777 /* Use only configured external hostname */
2778 if (GNUNET_CONFIGURATION_have_value
2779 (plugin->env->cfg, plugin->name, "EXTERNAL_HOSTNAME_ONLY"))
2781 plugin->external_only =
2782 GNUNET_CONFIGURATION_get_value_yesno (plugin->env->cfg, plugin->name,
2783 "EXTERNAL_HOSTNAME_ONLY");
2786 plugin->external_only = GNUNET_NO;
2788 if (GNUNET_YES == plugin->external_only)
2789 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2790 _("Notifying transport only about hostname `%s'\n"), plugin->external_hostname);
2793 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2794 "No external hostname configured\n");
2796 /* Optional parameters */
2797 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (plugin->env->cfg,
2799 "MAX_CONNECTIONS", &max_connections))
2800 max_connections = 128;
2801 plugin->max_connections = max_connections;
2803 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2804 _("Maximum number of connections is %u\n"),
2805 plugin->max_connections);
2808 plugin->peer_id_length = strlen (GNUNET_h2s_full (&plugin->env->my_identity->hashPubKey));
2815 * Session was idle, so disconnect it
2817 * @param cls the session
2818 * @param tc task context
2821 server_session_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2823 GNUNET_assert (NULL != cls);
2824 struct Session *s = cls;
2826 s->timeout_task = GNUNET_SCHEDULER_NO_TASK;
2827 GNUNET_log (TIMEOUT_LOG,
2828 "Session %p was idle for %llu ms, disconnecting\n",
2829 s, (unsigned long long) SERVER_SESSION_TIMEOUT.rel_value);
2831 /* call session destroy function */
2832 GNUNET_assert (GNUNET_OK == server_disconnect (s));
2837 * Start session timeout for session s
2839 * @param s the session
2842 server_start_session_timeout (struct Session *s)
2844 GNUNET_assert (NULL != s);
2845 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == s->timeout_task);
2846 s->timeout_task = GNUNET_SCHEDULER_add_delayed (SERVER_SESSION_TIMEOUT,
2847 &server_session_timeout,
2849 GNUNET_log (TIMEOUT_LOG,
2850 "Timeout for session %p set to %llu ms\n",
2851 s, (unsigned long long) SERVER_SESSION_TIMEOUT.rel_value);
2856 * Increment session timeout due to activity session s
2858 * @param s the session
2861 server_reschedule_session_timeout (struct Session *s)
2863 GNUNET_assert (NULL != s);
2864 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK != s->timeout_task);
2866 GNUNET_SCHEDULER_cancel (s->timeout_task);
2867 s->timeout_task = GNUNET_SCHEDULER_add_delayed (SERVER_SESSION_TIMEOUT,
2868 &server_session_timeout,
2870 GNUNET_log (TIMEOUT_LOG,
2871 "Timeout rescheduled for session %p set to %llu ms\n",
2872 s, (unsigned long long) SERVER_SESSION_TIMEOUT.rel_value);
2877 * Exit point from the plugin.
2883 LIBGNUNET_PLUGIN_TRANSPORT_DONE (void *cls)
2885 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
2886 struct HTTP_Server_Plugin *plugin = api->cls;
2887 struct Session *pos;
2888 struct Session *next;
2890 if (NULL == api->cls)
2892 /* Free for stub mode */
2896 plugin->in_shutdown = GNUNET_YES;
2897 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2898 _("Shutting down plugin `%s'\n"),
2901 if (GNUNET_SCHEDULER_NO_TASK != plugin->notify_ext_task)
2903 GNUNET_SCHEDULER_cancel (plugin->notify_ext_task);
2904 plugin->notify_ext_task = GNUNET_SCHEDULER_NO_TASK;
2907 if (NULL != plugin->ext_addr)
2909 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2910 "Notifying transport to remove address `%s'\n",
2911 http_common_plugin_address_to_string (NULL,
2913 plugin->ext_addr_len));
2915 plugin->env->notify_address (plugin->env->cls,
2918 plugin->ext_addr_len,
2921 plugin->env->notify_address (plugin->env->cls,
2924 plugin->ext_addr_len,
2930 /* Stop to report addresses to transport service */
2931 server_stop_report_addresses (plugin);
2932 server_stop (plugin);
2933 next = plugin->head;
2934 while (NULL != (pos = next))
2937 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2938 "Removing left over session %p\n", pos);
2940 if ((GNUNET_YES == pos->session_passed) && (GNUNET_NO == pos->session_ended))
2942 /* Notify transport immediately that this session is invalid */
2943 pos->session_ended = GNUNET_YES;
2944 plugin->env->session_end (plugin->env->cls, &pos->target, pos);
2947 server_delete_session (pos);
2951 GNUNET_free_non_null (plugin->external_hostname);
2952 GNUNET_free_non_null (plugin->ext_addr);
2953 GNUNET_free_non_null (plugin->server_addr_v4);
2954 GNUNET_free_non_null (plugin->server_addr_v6);
2956 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2957 _("Shutdown for plugin `%s' complete\n"),
2960 GNUNET_free (plugin);
2967 * Entry point for the plugin.
2973 LIBGNUNET_PLUGIN_TRANSPORT_INIT (void *cls)
2975 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
2976 struct GNUNET_TRANSPORT_PluginFunctions *api;
2977 struct HTTP_Server_Plugin *plugin;
2979 plugin = GNUNET_malloc (sizeof (struct HTTP_Server_Plugin));
2983 if (NULL == env->receive)
2985 /* run in 'stub' mode (i.e. as part of gnunet-peerinfo), don't fully
2986 initialze the plugin or the API */
2987 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
2989 api->address_to_string = &http_common_plugin_address_to_string;
2990 api->string_to_address = &http_common_plugin_string_to_address;
2991 api->address_pretty_printer = &http_common_plugin_address_pretty_printer;
2995 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
2997 api->send = &http_server_plugin_send;
2998 api->disconnect = &http_server_plugin_disconnect;
2999 api->check_address = &http_server_plugin_address_suggested;
3000 api->get_session = &http_server_plugin_get_session;
3002 api->address_to_string = &http_common_plugin_address_to_string;
3003 api->string_to_address = &http_common_plugin_string_to_address;
3004 api->address_pretty_printer = &http_common_plugin_address_pretty_printer;
3007 plugin->name = "transport-https_server";
3008 plugin->protocol = "https";
3010 plugin->name = "transport-http_server";
3011 plugin->protocol = "http";
3014 /* Configure plugin */
3015 if (GNUNET_SYSERR == server_configure_plugin (plugin))
3017 LIBGNUNET_PLUGIN_TRANSPORT_DONE (api);
3021 /* Check IPv6 support */
3022 if (GNUNET_YES == plugin->use_ipv6)
3023 plugin->use_ipv6 = server_check_ipv6_support (plugin);
3025 /* Report addresses to transport service */
3026 if (GNUNET_NO == plugin->external_only)
3027 server_start_report_addresses (plugin);
3029 if (GNUNET_SYSERR == server_start (plugin))
3031 LIBGNUNET_PLUGIN_TRANSPORT_DONE (api);
3038 /* end of plugin_transport_http_server.c */