2 This file is part of GNUnet
3 (C) 2002-2013 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
27 #include "gnunet_util_lib.h"
28 #include "gnunet_statistics_service.h"
29 #include "gnunet_transport_plugin.h"
30 #include "gnunet_nat_lib.h"
31 #include "plugin_transport_http_common.h"
32 #include <microhttpd.h>
37 #define PLUGIN_NAME "https_server"
38 #define LIBGNUNET_PLUGIN_TRANSPORT_INIT libgnunet_plugin_transport_https_server_init
39 #define LIBGNUNET_PLUGIN_TRANSPORT_DONE libgnunet_plugin_transport_https_server_done
41 #define PLUGIN_NAME "http_server"
42 #define LIBGNUNET_PLUGIN_TRANSPORT_INIT libgnunet_plugin_transport_http_server_init
43 #define LIBGNUNET_PLUGIN_TRANSPORT_DONE libgnunet_plugin_transport_http_server_done
46 #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>"
52 * Enable output for debbuging URL's of incoming requests
54 #define DEBUG_URL_PARSE GNUNET_NO
58 * Encapsulation of all of the state of the plugin.
63 * Session handle for connections.
68 * Stored in a linked list.
73 * Stored in a linked list.
78 * To whom are we talking to (set to our identity
79 * if we are still waiting for the welcome message)
81 struct GNUNET_PeerIdentity target;
84 * Pointer to the global plugin struct.
86 struct HTTP_Server_Plugin *plugin;
89 * next pointer for double linked list
91 struct HTTP_Message *msg_head;
94 * previous pointer for double linked list
96 struct HTTP_Message *msg_tail;
99 * Message stream tokenizer for incoming data
101 struct GNUNET_SERVER_MessageStreamTokenizer *msg_tk;
106 struct ServerConnection *server_recv;
111 struct ServerConnection *server_send;
124 * Unique HTTP/S connection tag for this connection
129 * ATS network type in NBO
131 uint32_t ats_address_network_type;
134 * Was session given to transport service?
139 * Did we immediately end the session in disconnect_cb
144 * Are incoming connection established at the moment
146 int connect_in_progress;
149 * Absolute time when to receive data again
150 * Used for receive throttling
152 struct GNUNET_TIME_Absolute next_receive;
155 * Session timeout task
157 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
161 struct ServerConnection
169 * Should this connection get disconnected? GNUNET_YES/NO
174 * For PUT connections: Is this the first or last callback with size 0
179 * The session this server connection belongs to
181 struct Session *session;
186 struct MHD_Connection *mhd_conn;
191 struct MHD_Daemon *mhd_daemon;
196 * Encapsulation of all of the state of the plugin.
198 struct HTTP_Server_Plugin
203 struct GNUNET_TRANSPORT_PluginEnvironment *env;
206 * Linked list head of open sessions.
208 struct Session *head;
211 * Linked list tail of open sessions.
213 struct Session *tail;
226 * My options to be included in the address
233 char *external_hostname;
236 * Verify external address
238 int verify_external_hostname;
241 * Maximum number of sockets the plugin can use
242 * Each http inbound /outbound connections are two connections
244 unsigned int max_connections;
247 * Current number of sockets the plugin can use
248 * Each http inbound /outbound connections are two connections
250 unsigned int cur_connections;
253 * Did we immediately end the session in disconnect_cb
263 * External hostname the plugin can be connected to, can be different to
264 * the host's FQDN, used e.g. for reverse proxying
266 struct HttpAddress *ext_addr;
269 * Notify transport only about external address
271 unsigned int external_only;
274 * External address length
294 * Task calling transport service about external address
296 GNUNET_SCHEDULER_TaskIdentifier notify_ext_task;
299 * NAT handle & address management
301 struct GNUNET_NAT_Handle *nat;
304 * List of own addresses
308 * IPv4 addresses DLL head
310 struct HttpAddressWrapper *addr_head;
313 * IPv4 addresses DLL tail
315 struct HttpAddressWrapper *addr_tail;
318 * IPv4 server socket to bind to
320 struct sockaddr_in *server_addr_v4;
323 * IPv6 server socket to bind to
325 struct sockaddr_in6 *server_addr_v6;
330 GNUNET_SCHEDULER_TaskIdentifier server_v4_task;
335 GNUNET_SCHEDULER_TaskIdentifier server_v6_task;
338 * The IPv4 server is scheduled to run asap
340 int server_v4_immediately;
343 * The IPv6 server is scheduled to run asap
345 int server_v6_immediately;
350 struct MHD_Daemon *server_v4;
355 struct MHD_Daemon *server_v6;
363 * Use RC4-128 instead of AES:
364 * NONE:+VERS-TLS1.0:+ARCFOUR-128:+SHA1:+RSA:+COMP-NULL
384 * Wrapper to manage addresses
386 struct HttpAddressWrapper
391 struct HttpAddressWrapper *next;
394 * Linked list previous
396 struct HttpAddressWrapper *prev;
398 struct HttpAddress *address;
405 * Message to send using http
410 * next pointer for double linked list
412 struct HTTP_Message *next;
415 * previous pointer for double linked list
417 struct HTTP_Message *prev;
420 * buffer containing data to send
425 * amount of data already sent
435 * HTTP/S specific overhead
440 * Continuation function to call once the transmission buffer
441 * has again space available. NULL if there is no
442 * continuation to call.
444 GNUNET_TRANSPORT_TransmitContinuation transmit_cont;
447 * Closure for transmit_cont.
449 void *transmit_cont_cls;
454 * Start session timeout for session s
455 * @param s the session
458 server_start_session_timeout (struct Session *s);
462 * Increment session timeout due to activity for session s
463 * @param s the session
466 server_reschedule_session_timeout (struct Session *s);
470 * Cancel timeout for session s
471 * @param s the session
474 server_stop_session_timeout (struct Session *s);
478 * Disconnect session @a s
480 * @param cls closure with the `struct HTTP_Server_Plugin`
481 * @param s the session
482 * @return #GNUNET_OK on success
485 http_server_plugin_disconnect_session (void *cls,
490 * Does session s exist?
492 * @param plugin the plugin handle
493 * @param s the session
494 * @return #GNUNET_YES on success, #GNUNET_NO on error
497 server_exist_session (struct HTTP_Server_Plugin *plugin, struct Session *s);
501 * Reschedule the execution of both IPv4 and IPv6 server
502 * @param plugin the plugin
503 * @param server which server to schedule v4 or v6?
504 * @param now #GNUNET_YES to schedule execution immediately, #GNUNET_NO to wait
508 server_reschedule (struct HTTP_Server_Plugin *plugin,
509 struct MHD_Daemon *server,
514 * Function that can be used by the transport service to transmit
515 * a message using the plugin. Note that in the case of a
516 * peer disconnecting, the continuation MUST be called
517 * prior to the disconnect notification itself. This function
518 * will be called with this peer's HELLO message to initiate
519 * a fresh connection to another peer.
522 * @param session which session must be used
523 * @param msgbuf the message to transmit
524 * @param msgbuf_size number of bytes in @a msgbuf
525 * @param priority how important is the message (most plugins will
526 * ignore message priority and just FIFO)
527 * @param to how long to wait at most for the transmission (does not
528 * require plugins to discard the message after the timeout,
529 * just advisory for the desired delay; most plugins will ignore
531 * @param cont continuation to call once the message has
532 * been transmitted (or if the transport is ready
533 * for the next transmission call; or if the
534 * peer disconnected...); can be NULL
535 * @param cont_cls closure for @a cont
536 * @return number of bytes used (on the physical network, with overheads);
537 * -1 on hard errors (i.e. address invalid); 0 is a legal value
538 * and does NOT mean that the message was not transmitted (DV)
541 http_server_plugin_send (void *cls,
542 struct Session *session,
543 const char *msgbuf, size_t msgbuf_size,
544 unsigned int priority,
545 struct GNUNET_TIME_Relative to,
546 GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
548 struct HTTP_Server_Plugin *plugin = cls;
549 struct HTTP_Message *msg;
553 GNUNET_assert (plugin != NULL);
554 GNUNET_assert (session != NULL);
556 if (GNUNET_NO == server_exist_session (plugin, session))
559 return GNUNET_SYSERR;
561 if (NULL == session->server_send)
563 if (GNUNET_NO == session->connect_in_progress)
565 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, session->plugin->name,
566 "Session %p/connection %p: Sending message with %u bytes to peer `%s' with FAILED\n",
567 session, session->server_send,
568 msgbuf_size, GNUNET_i2s (&session->target));
570 return GNUNET_SYSERR;
575 if (GNUNET_YES == session->server_send->disconnect)
576 return GNUNET_SYSERR;
580 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, session->plugin->name,
581 "Session %p/connection %p: Sending message with %u to peer `%s' with \n",
582 session, session->server_send,
583 msgbuf_size, GNUNET_i2s (&session->target));
585 /* create new message and schedule */
586 bytes_sent = sizeof (struct HTTP_Message) + msgbuf_size;
587 msg = GNUNET_malloc (bytes_sent);
589 msg->size = msgbuf_size;
591 msg->buf = (char *) &msg[1];
592 msg->transmit_cont = cont;
593 msg->transmit_cont_cls = cont_cls;
594 memcpy (msg->buf, msgbuf, msgbuf_size);
596 GNUNET_CONTAINER_DLL_insert_tail (session->msg_head, session->msg_tail, msg);
598 GNUNET_asprintf (&stat_txt, "# bytes currently in %s_server buffers", plugin->protocol);
599 GNUNET_STATISTICS_update (plugin->env->stats,
600 stat_txt, msgbuf_size, GNUNET_NO);
601 GNUNET_free (stat_txt);
603 if (NULL != session->server_send)
605 server_reschedule (session->plugin,
606 session->server_send->mhd_daemon,
608 server_reschedule_session_timeout (session);
615 * Function that can be used to force the plugin to disconnect
616 * from the given peer and cancel all previous transmissions
617 * (and their continuationc).
620 * @param target peer from which to disconnect
623 http_server_plugin_disconnect_peer (void *cls,
624 const struct GNUNET_PeerIdentity *target)
626 struct HTTP_Server_Plugin *plugin = cls;
627 struct Session *next;
630 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
631 "Transport tells me to disconnect `%s'\n",
632 GNUNET_i2s (target));
634 while (NULL != (pos = next))
637 if (0 == memcmp (target, &pos->target, sizeof (struct GNUNET_PeerIdentity)))
639 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
640 "Disconnecting session %p to `%s'\n",
641 pos, GNUNET_i2s (target));
642 http_server_plugin_disconnect_session (plugin, pos);
649 * Another peer has suggested an address for this
650 * peer and transport plugin. Check that this could be a valid
651 * address. If so, consider adding it to the list
655 * @param addr pointer to the address
656 * @param addrlen length of @a addr
657 * @return #GNUNET_OK if this is a plausible address for this peer
661 http_server_plugin_address_suggested (void *cls,
665 struct HTTP_Server_Plugin *plugin = cls;
666 struct HttpAddressWrapper *next;
667 struct HttpAddressWrapper *pos;
668 const struct HttpAddress *haddr = addr;
670 if ((NULL != plugin->ext_addr) &&
671 GNUNET_YES == (http_common_cmp_addresses (addr, addrlen,
673 plugin->ext_addr_len)))
675 /* Checking HTTP_OPTIONS_VERIFY_CERTIFICATE option for external hostname */
676 if ((ntohl (haddr->options) & HTTP_OPTIONS_VERIFY_CERTIFICATE) !=
677 (plugin->options & HTTP_OPTIONS_VERIFY_CERTIFICATE))
678 return GNUNET_NO; /* VERIFY option not set as required! */
681 next = plugin->addr_head;
682 while (NULL != (pos = next))
685 if (GNUNET_YES == (http_common_cmp_addresses(addr,
696 * Creates a new outbound session the transport
697 * service will use to send data to the peer
699 * Since HTTP/S server cannot create sessions, always return NULL
701 * @param cls the plugin
702 * @param address the address
703 * @return always NULL
705 static struct Session *
706 http_server_plugin_get_session (void *cls,
707 const struct GNUNET_HELLO_Address *address)
714 * Deleting the session
715 * Must not be used afterwards
717 * @param cls closure with the `struct HTTP_ServerPlugin`
718 * @param s the session to delete
719 * @return #GNUNET_OK on success
722 server_delete_session (void *cls,
725 struct HTTP_Server_Plugin *plugin = cls;
726 struct HTTP_Message *msg;
727 struct HTTP_Message *tmp;
729 server_stop_session_timeout(s);
730 GNUNET_CONTAINER_DLL_remove (plugin->head, plugin->tail, s);
735 GNUNET_CONTAINER_DLL_remove (s->msg_head, s->msg_tail, msg);
736 if (NULL != msg->transmit_cont)
737 msg->transmit_cont (msg->transmit_cont_cls, &s->target, GNUNET_SYSERR,
738 msg->size, msg->pos + msg->overhead);
742 if (NULL != s->msg_tk)
744 GNUNET_SERVER_mst_destroy (s->msg_tk);
747 GNUNET_free (s->addr);
748 GNUNET_free_non_null (s->server_recv);
749 GNUNET_free_non_null (s->server_send);
750 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
752 "Session %p destroyed\n", s);
759 * Cancel timeout for session s
761 * @param s the session
764 server_stop_session_timeout (struct Session *s)
766 GNUNET_assert (NULL != s);
768 if (GNUNET_SCHEDULER_NO_TASK != s->timeout_task)
770 GNUNET_SCHEDULER_cancel (s->timeout_task);
771 s->timeout_task = GNUNET_SCHEDULER_NO_TASK;
772 GNUNET_log (TIMEOUT_LOG, "Timeout stopped for session %p\n", s);
778 * Function that queries MHD's select sets and
779 * starts the task waiting for them.
780 * @param plugin plugin
781 * @param daemon_handle the MHD daemon handle
782 * @param now schedule immediately
783 * @return task identifier
785 static GNUNET_SCHEDULER_TaskIdentifier
786 server_schedule (struct HTTP_Server_Plugin *plugin,
787 struct MHD_Daemon *daemon_handle,
792 * Reschedule the execution of both IPv4 and IPv6 server
793 * @param plugin the plugin
794 * @param server which server to schedule v4 or v6?
795 * @param now #GNUNET_YES to schedule execution immediately, #GNUNET_NO to wait
799 server_reschedule (struct HTTP_Server_Plugin *plugin,
800 struct MHD_Daemon *server,
803 if ((server == plugin->server_v4) && (plugin->server_v4 != NULL))
805 if (GNUNET_YES == plugin->server_v4_immediately)
806 return; /* No rescheduling, server will run asap */
808 if (GNUNET_YES == now)
809 plugin->server_v4_immediately = GNUNET_YES;
811 if (plugin->server_v4_task != GNUNET_SCHEDULER_NO_TASK)
813 GNUNET_SCHEDULER_cancel (plugin->server_v4_task);
814 plugin->server_v4_task = GNUNET_SCHEDULER_NO_TASK;
816 plugin->server_v4_task = server_schedule (plugin, plugin->server_v4, now);
819 if ((server == plugin->server_v6) && (plugin->server_v6 != NULL))
821 if (GNUNET_YES == plugin->server_v6_immediately)
822 return; /* No rescheduling, server will run asap */
824 if (GNUNET_YES == now)
825 plugin->server_v6_immediately = GNUNET_YES;
827 if (plugin->server_v6_task != GNUNET_SCHEDULER_NO_TASK)
829 GNUNET_SCHEDULER_cancel (plugin->server_v6_task);
830 plugin->server_v6_task = GNUNET_SCHEDULER_NO_TASK;
832 plugin->server_v6_task = server_schedule (plugin, plugin->server_v6, now);
838 * Disconnect session @a s
840 * @param cls closure with the `struct HTTP_Server_Plugin`
841 * @param s the session
842 * @return #GNUNET_OK on success
845 http_server_plugin_disconnect_session (void *cls,
848 struct HTTP_Server_Plugin *plugin = cls;
849 struct ServerConnection * send;
850 struct ServerConnection * recv;
852 if (GNUNET_NO == server_exist_session (plugin, s))
855 return GNUNET_SYSERR;
858 send = (struct ServerConnection *) s->server_send;
859 if (s->server_send != NULL)
861 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, s->plugin->name,
862 "Server: %p / %p Terminating inbound PUT session to peer `%s'\n",
863 s, s->server_send, GNUNET_i2s (&s->target));
865 send->disconnect = GNUNET_YES;
866 MHD_set_connection_option (send->mhd_conn,
867 MHD_CONNECTION_OPTION_TIMEOUT,
869 server_reschedule (s->plugin, send->mhd_daemon, GNUNET_YES);
872 recv = (struct ServerConnection *) s->server_recv;
875 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, s->plugin->name,
876 "Server: %p / %p Terminating inbound GET session to peer `%s'\n",
877 s, s->server_recv, GNUNET_i2s (&s->target));
879 recv->disconnect = GNUNET_YES;
880 MHD_set_connection_option (recv->mhd_conn,
881 MHD_CONNECTION_OPTION_TIMEOUT,
883 server_reschedule (s->plugin, recv->mhd_daemon, GNUNET_YES);
890 * Function that is called to get the keepalive factor.
891 * GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT is divided by this number to
892 * calculate the interval between keepalive packets.
894 * @param cls closure with the `struct Plugin`
895 * @return keepalive factor
898 http_server_query_keepalive_factor (void *cls)
905 * Tell MHD that the connection should timeout after @a to seconds.
907 * @param plugin our plugin
908 * @param s session for which the timeout changes
909 * @param to timeout in seconds
912 server_mhd_connection_timeout (struct HTTP_Server_Plugin *plugin,
916 /* Setting timeouts for other connections */
917 if (NULL != s->server_recv)
919 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
920 "Setting timeout for %p to %u sec.\n",
922 MHD_set_connection_option (s->server_recv->mhd_conn,
923 MHD_CONNECTION_OPTION_TIMEOUT,
925 server_reschedule (plugin, s->server_recv->mhd_daemon, GNUNET_NO);
927 if (NULL != s->server_send)
929 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
930 "Setting timeout for %p to %u sec.\n",
932 MHD_set_connection_option (s->server_send->mhd_conn,
933 MHD_CONNECTION_OPTION_TIMEOUT,
935 server_reschedule (plugin, s->server_send->mhd_daemon, GNUNET_NO);
941 * Parse incoming URL for tag and target
943 * @param plugin plugin
944 * @param url incoming url
945 * @param target where to store the target
946 * @param tag where to store the tag
947 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
950 server_parse_url (struct HTTP_Server_Plugin *plugin,
952 struct GNUNET_PeerIdentity *target,
955 char * tag_start = NULL;
956 char * tag_end = NULL;
957 char * target_start = NULL;
958 char * separator = NULL;
959 unsigned int hash_length;
960 unsigned long int ctag;
963 * URL is valid if it is in the form [prefix with (multiple) '/'][peerid[103];tag]*/
968 return GNUNET_SYSERR;
973 separator = strrchr (url, ';');
975 if (NULL == separator)
977 if (DEBUG_URL_PARSE) GNUNET_break (0);
978 return GNUNET_SYSERR;
980 tag_start = separator + 1;
982 if (strlen (tag_start) == 0)
984 /* No tag after separator */
985 if (DEBUG_URL_PARSE) GNUNET_break (0);
986 return GNUNET_SYSERR;
988 ctag = strtoul (tag_start, &tag_end, 10);
991 /* tag == 0 , invalid */
992 if (DEBUG_URL_PARSE) GNUNET_break (0);
993 return GNUNET_SYSERR;
995 if ((ctag == ULONG_MAX) && (ERANGE == errno))
997 /* out of range: > ULONG_MAX */
998 if (DEBUG_URL_PARSE) GNUNET_break (0);
999 return GNUNET_SYSERR;
1001 if (ctag > UINT32_MAX)
1003 /* out of range: > UINT32_MAX */
1004 if (DEBUG_URL_PARSE) GNUNET_break (0);
1005 return GNUNET_SYSERR;
1007 (*tag) = (uint32_t) ctag;
1008 if (NULL == tag_end)
1010 /* no char after tag */
1011 if (DEBUG_URL_PARSE) GNUNET_break (0);
1012 return GNUNET_SYSERR;
1014 if (url[strlen(url)] != tag_end[0])
1016 /* there are more not converted chars after tag */
1017 if (DEBUG_URL_PARSE) GNUNET_break (0);
1018 return GNUNET_SYSERR;
1020 if (DEBUG_URL_PARSE)
1021 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1022 "Found tag `%u' in url\n", (*tag));
1024 /* convert peer id */
1025 target_start = strrchr (url, '/');
1026 if (NULL == target_start)
1028 /* no leading '/' */
1029 target_start = (char *) url;
1032 hash_length = separator - target_start;
1033 if (hash_length != plugin->peer_id_length)
1035 /* no char after tag */
1036 if (DEBUG_URL_PARSE) GNUNET_break (0);
1037 return GNUNET_SYSERR;
1040 GNUNET_CRYPTO_eddsa_public_key_from_string (target_start,
1042 &target->public_key))
1044 /* hash conversion failed */
1045 if (DEBUG_URL_PARSE) GNUNET_break (0);
1046 return GNUNET_SYSERR;
1048 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1050 "Found target `%s' in URL\n",
1051 GNUNET_i2s_full (target));
1057 * Lookup a mhd connection and create one if none is found
1059 * @param plugin the plugin handle
1060 * @param mhd_connection the incoming mhd_connection
1061 * @param url incoming requested URL
1062 * @param method PUT or GET
1063 * @return the server connecetion
1065 static struct ServerConnection *
1066 server_lookup_connection (struct HTTP_Server_Plugin *plugin,
1067 struct MHD_Connection *mhd_connection, const char *url,
1070 struct Session *s = NULL;
1071 struct ServerConnection *sc = NULL;
1072 const union MHD_ConnectionInfo *conn_info;
1073 struct GNUNET_ATS_Information ats;
1074 struct HttpAddress *addr;
1076 struct GNUNET_PeerIdentity target;
1078 int direction = GNUNET_SYSERR;
1081 conn_info = MHD_get_connection_info (mhd_connection,
1082 MHD_CONNECTION_INFO_CLIENT_ADDRESS);
1083 if ((conn_info->client_addr->sa_family != AF_INET) &&
1084 (conn_info->client_addr->sa_family != AF_INET6))
1086 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1087 "New %s connection from %s\n", method, url);
1089 if (GNUNET_SYSERR == server_parse_url (plugin, url, &target, &tag))
1091 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1092 "Invalid url %s\n", url);
1095 if (0 == strcmp (MHD_HTTP_METHOD_PUT, method))
1096 direction = _RECEIVE;
1097 else if (0 == strcmp (MHD_HTTP_METHOD_GET, method))
1101 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1102 "Invalid method %s connection from %s\n", method, url);
1106 plugin->cur_connections++;
1107 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1108 "New %s connection from %s with tag %u (%u of %u)\n",
1110 GNUNET_i2s (&target), tag,
1111 plugin->cur_connections, plugin->max_connections);
1112 /* find duplicate session */
1116 if ((0 == memcmp (&s->target, &target, sizeof (struct GNUNET_PeerIdentity))) &&
1123 if ((_RECEIVE == direction) && (NULL != s->server_recv))
1125 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1126 "Duplicate PUT connection from `%s' tag %u, dismissing new connection\n",
1127 GNUNET_i2s (&target),
1132 if ((_SEND == direction) && (NULL != s->server_send))
1134 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1135 "Duplicate GET connection from `%s' tag %u, dismissing new connection\n",
1136 GNUNET_i2s (&target),
1143 /* create new session */
1144 switch (conn_info->client_addr->sa_family)
1147 addr = http_common_address_from_socket (plugin->protocol, conn_info->client_addr, sizeof (struct sockaddr_in));
1148 addr_len = http_common_address_get_size (addr);
1149 ats = plugin->env->get_address_type (plugin->env->cls, conn_info->client_addr, sizeof (struct sockaddr_in));
1152 addr = http_common_address_from_socket (plugin->protocol, conn_info->client_addr, sizeof (struct sockaddr_in6));
1153 addr_len = http_common_address_get_size (addr);
1154 ats = plugin->env->get_address_type (plugin->env->cls, conn_info->client_addr, sizeof (struct sockaddr_in6));
1157 /* external host name */
1158 ats.type = htonl (GNUNET_ATS_NETWORK_TYPE);
1159 ats.type = htonl (GNUNET_ATS_NET_WAN);
1163 s = GNUNET_new (struct Session);
1164 memcpy (&s->target, &target, sizeof (struct GNUNET_PeerIdentity));
1167 s->addrlen = addr_len;
1168 s->ats_address_network_type = ats.value;
1169 s->next_receive = GNUNET_TIME_UNIT_ZERO_ABS;
1171 s->server_recv = NULL;
1172 s->server_send = NULL;
1173 s->session_passed = GNUNET_NO;
1174 s->session_ended = GNUNET_NO;
1175 s->connect_in_progress = GNUNET_YES;
1176 server_start_session_timeout(s);
1177 GNUNET_CONTAINER_DLL_insert (plugin->head, plugin->tail, s);
1179 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1180 "Creating new session %p for peer `%s' connecting from `%s'\n",
1181 s, GNUNET_i2s (&target),
1182 http_common_plugin_address_to_string (NULL,
1186 sc = GNUNET_new (struct ServerConnection);
1187 if (conn_info->client_addr->sa_family == AF_INET)
1188 sc->mhd_daemon = plugin->server_v4;
1189 if (conn_info->client_addr->sa_family == AF_INET6)
1190 sc->mhd_daemon = plugin->server_v6;
1191 sc->mhd_conn = mhd_connection;
1192 sc->direction = direction;
1193 sc->connected = GNUNET_NO;
1195 if (direction == _SEND)
1196 s->server_send = sc;
1197 if (direction == _RECEIVE)
1198 s->server_recv = sc;
1200 if ((NULL != s->server_send) && (NULL != s->server_recv))
1202 s->connect_in_progress = GNUNET_NO; /* PUT and GET are connected */
1203 plugin->env->session_start (NULL, &s->target, PLUGIN_NAME, NULL, 0 ,s, NULL, 0);
1206 if ((NULL == s->server_recv) || (NULL == s->server_send))
1208 to = (HTTP_SERVER_NOT_VALIDATED_TIMEOUT.rel_value_us / 1000LL / 1000LL);
1209 MHD_set_connection_option (mhd_connection,
1210 MHD_CONNECTION_OPTION_TIMEOUT, to);
1211 server_reschedule (plugin, sc->mhd_daemon, GNUNET_NO);
1215 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1216 "Session %p for peer `%s' fully connected\n",
1217 s, GNUNET_i2s (&target));
1218 to = (HTTP_SERVER_SESSION_TIMEOUT.rel_value_us / 1000LL / 1000LL);
1219 server_mhd_connection_timeout (plugin, s, to);
1222 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1223 "Setting timeout for %p to %u sec.\n", sc, to);
1229 * Lookup a session for a server connection
1231 * @param plugin the plugin
1232 * @param sc the server connection
1233 * @return the session found or NULL
1235 static struct Session *
1236 server_lookup_session (struct HTTP_Server_Plugin *plugin,
1237 struct ServerConnection * sc)
1241 for (s = plugin->head; NULL != s; s = s->next)
1242 if ((s->server_recv == sc) || (s->server_send == sc))
1249 server_exist_session (struct HTTP_Server_Plugin *plugin,
1252 struct Session * head;
1254 for (head = plugin->head; head != NULL; head = head->next)
1262 * Callback called by MHD when it needs data to send
1264 * @param cls current session
1265 * @param pos position in buffer
1266 * @param buf the buffer to write data to
1267 * @param max max number of bytes available in buffer
1268 * @return bytes written to buffer
1271 server_send_callback (void *cls, uint64_t pos, char *buf, size_t max)
1273 struct Session *s = cls;
1274 ssize_t bytes_read = 0;
1275 struct HTTP_Message *msg;
1278 if (GNUNET_NO == server_exist_session (s->plugin, s))
1284 bytes_read = GNUNET_MIN (msg->size - msg->pos,
1286 memcpy (buf, &msg->buf[msg->pos], bytes_read);
1287 msg->pos += bytes_read;
1289 /* removing message */
1290 if (msg->pos == msg->size)
1292 GNUNET_CONTAINER_DLL_remove (s->msg_head, s->msg_tail, msg);
1293 if (NULL != msg->transmit_cont)
1294 msg->transmit_cont (msg->transmit_cont_cls, &s->target, GNUNET_OK,
1295 msg->size, msg->size + msg->overhead);
1301 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, s->plugin->name,
1302 "Sent %u bytes to peer `%s' with session %p \n", bytes_read, GNUNET_i2s (&s->target), s);
1303 GNUNET_asprintf (&stat_txt, "# bytes currently in %s_server buffers",
1304 s->plugin->protocol);
1305 GNUNET_STATISTICS_update (s->plugin->env->stats,
1306 stat_txt, -bytes_read, GNUNET_NO);
1307 GNUNET_free (stat_txt);
1308 GNUNET_asprintf (&stat_txt, "# bytes transmitted via %s_server",
1309 s->plugin->protocol);
1310 GNUNET_STATISTICS_update (s->plugin->env->stats,
1311 stat_txt, bytes_read, GNUNET_NO);
1312 GNUNET_free (stat_txt);
1319 * Callback called by MessageStreamTokenizer when a message has arrived
1321 * @param cls current session as closure
1322 * @param client client
1323 * @param message the message to be forwarded to transport service
1324 * @return #GNUNET_OK
1327 server_receive_mst_cb (void *cls, void *client,
1328 const struct GNUNET_MessageHeader *message)
1330 struct Session *s = cls;
1331 struct HTTP_Server_Plugin *plugin = s->plugin;
1332 struct GNUNET_ATS_Information atsi;
1333 struct GNUNET_TIME_Relative delay;
1336 if (GNUNET_NO == server_exist_session (s->plugin, s))
1340 atsi.type = htonl (GNUNET_ATS_NETWORK_TYPE);
1341 atsi.value = s->ats_address_network_type;
1342 GNUNET_break (s->ats_address_network_type != ntohl (GNUNET_ATS_NET_UNSPECIFIED));
1345 delay = plugin->env->receive (plugin->env->cls,
1350 plugin->env->update_address_metrics (plugin->env->cls,
1352 NULL, 0, s, &atsi, 1);
1354 GNUNET_asprintf (&stat_txt, "# bytes received via %s_server", plugin->protocol);
1355 GNUNET_STATISTICS_update (plugin->env->stats,
1356 stat_txt, ntohs (message->size), GNUNET_NO);
1357 GNUNET_free (stat_txt);
1359 s->session_passed = GNUNET_YES;
1360 s->next_receive = GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (), delay);
1361 if (delay.rel_value_us > 0)
1363 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1364 "Peer `%s' address `%s' next read delayed for %s\n",
1365 GNUNET_i2s (&s->target),
1366 http_common_plugin_address_to_string (NULL,
1368 s->addr, s->addrlen),
1369 GNUNET_STRINGS_relative_time_to_string (delay,
1372 server_reschedule_session_timeout (s);
1378 * MHD callback for a new incoming connection
1380 * @param cls the plugin handle
1381 * @param mhd_connection the mhd connection
1382 * @param url the requested URL
1383 * @param method GET or PUT
1384 * @param version HTTP version
1385 * @param upload_data upload data
1386 * @param upload_data_size sizeof upload data
1387 * @param httpSessionCache the session cache to remember the connection
1388 * @return MHD_YES if connection is accepted, MHD_NO on reject
1391 server_access_cb (void *cls, struct MHD_Connection *mhd_connection,
1392 const char *url, const char *method, const char *version,
1393 const char *upload_data, size_t * upload_data_size,
1394 void **httpSessionCache)
1396 struct HTTP_Server_Plugin *plugin = cls;
1399 struct ServerConnection *sc = *httpSessionCache;
1401 struct MHD_Response *response;
1403 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1404 _("Access from connection %p (%u of %u) for `%s' `%s' url `%s' with upload data size %u\n"),
1406 plugin->cur_connections, plugin->max_connections,
1407 method, version, url, (*upload_data_size));
1409 GNUNET_assert (cls != NULL);
1412 /* new connection */
1413 sc = server_lookup_connection (plugin, mhd_connection, url, method);
1416 (*httpSessionCache) = sc;
1420 response = MHD_create_response_from_data (strlen (HTTP_ERROR_RESPONSE), HTTP_ERROR_RESPONSE, MHD_NO, MHD_NO);
1421 MHD_add_response_header (response,
1422 MHD_HTTP_HEADER_CONTENT_TYPE,
1424 res = MHD_queue_response (mhd_connection, MHD_HTTP_NOT_FOUND, response);
1425 MHD_destroy_response (response);
1431 /* 'old' connection */
1432 if (NULL == server_lookup_session (plugin, sc))
1434 /* Session was already disconnected */
1439 /* existing connection */
1440 sc = (*httpSessionCache);
1442 GNUNET_assert (NULL != s);
1443 /* connection is to be disconnected */
1444 if (sc->disconnect == GNUNET_YES)
1446 /* Sent HTTP/1.1: 200 OK as response */
1447 response = MHD_create_response_from_data (strlen ("Thank you!"),
1450 MHD_queue_response (mhd_connection, MHD_HTTP_OK, response);
1451 MHD_destroy_response (response);
1454 GNUNET_assert (s != NULL);
1456 if (sc->direction == _SEND)
1458 response = MHD_create_response_from_callback (MHD_SIZE_UNKNOWN,
1460 &server_send_callback, s,
1462 MHD_queue_response (mhd_connection, MHD_HTTP_OK, response);
1463 MHD_destroy_response (response);
1466 if (sc->direction == _RECEIVE)
1468 if ((*upload_data_size == 0) && (sc->connected == GNUNET_NO))
1470 /* (*upload_data_size == 0) first callback when header are passed */
1471 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1472 "Session %p / Connection %p: Peer `%s' PUT on address `%s' connected\n",
1474 GNUNET_i2s (&s->target),
1475 http_common_plugin_address_to_string (NULL,
1479 sc->connected = GNUNET_YES;
1482 else if ((*upload_data_size == 0) && (sc->connected == GNUNET_YES))
1484 /* (*upload_data_size == 0) when upload is complete */
1485 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1486 "Session %p / Connection %p: Peer `%s' PUT on address `%s' finished upload\n",
1488 GNUNET_i2s (&s->target),
1489 http_common_plugin_address_to_string (NULL,
1493 sc->connected = GNUNET_NO;
1494 /* Sent HTTP/1.1: 200 OK as PUT Response\ */
1495 response = MHD_create_response_from_data (strlen ("Thank you!"),
1498 MHD_queue_response (mhd_connection, MHD_HTTP_OK, response);
1499 MHD_destroy_response (response);
1502 else if ((*upload_data_size > 0) && (sc->connected == GNUNET_YES))
1504 /* (*upload_data_size > 0) for every segment received */
1505 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1506 "Session %p / Connection %p: Peer `%s' PUT on address `%s' received %u bytes\n",
1508 GNUNET_i2s (&s->target),
1509 http_common_plugin_address_to_string (NULL,
1514 struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get ();
1516 if ((s->next_receive.abs_value_us <= now.abs_value_us))
1518 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1519 "PUT with %u bytes forwarded to MST\n",
1521 if (s->msg_tk == NULL)
1523 s->msg_tk = GNUNET_SERVER_mst_create (&server_receive_mst_cb, s);
1525 GNUNET_SERVER_mst_receive (s->msg_tk, s, upload_data,
1526 *upload_data_size, GNUNET_NO, GNUNET_NO);
1527 server_mhd_connection_timeout (plugin, s,
1528 GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT.rel_value_us / 1000LL / 1000LL);
1529 (*upload_data_size) = 0;
1533 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1534 "Session %p / Connection %p: no inbound bandwidth available! Next read was delayed by %s\n",
1536 GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_duration (s->next_receive),
1552 * Callback from MHD when a connection disconnects
1554 * @param cls closure with the `struct HTTP_Server_Plugin *`
1555 * @param connection the disconnected MHD connection
1556 * @param httpSessionCache the pointer to distinguish
1559 server_disconnect_cb (void *cls, struct MHD_Connection *connection,
1560 void **httpSessionCache)
1562 struct HTTP_Server_Plugin *plugin = cls;
1563 struct ServerConnection *sc = *httpSessionCache;
1564 struct Session *s = NULL;
1565 struct Session *t = NULL;
1567 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1569 "Disconnect for connection %p \n", sc);
1574 if (NULL == (s = server_lookup_session (plugin, sc)))
1576 for (t = plugin->head; t != NULL; t = t->next)
1582 if (sc->direction == _SEND)
1584 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1585 "Peer `%s' connection %p, GET on address `%s' disconnected\n",
1586 GNUNET_i2s (&s->target), s->server_send,
1587 http_common_plugin_address_to_string (NULL,
1589 s->addr, s->addrlen));
1590 s->server_send = NULL;
1591 if (NULL != (s->server_recv))
1593 s->server_recv->disconnect = GNUNET_YES;
1594 GNUNET_assert (NULL != s->server_recv->mhd_conn);
1595 #if MHD_VERSION >= 0x00090E00
1596 MHD_set_connection_option (s->server_recv->mhd_conn, MHD_CONNECTION_OPTION_TIMEOUT,
1599 server_reschedule (plugin, s->server_recv->mhd_daemon, GNUNET_NO);
1602 if (sc->direction == _RECEIVE)
1604 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1605 "Peer `%s' connection %p PUT on address `%s' disconnected\n",
1606 GNUNET_i2s (&s->target), s->server_recv,
1607 http_common_plugin_address_to_string (NULL,
1609 s->addr, s->addrlen));
1610 s->server_recv = NULL;
1611 /* Do not terminate session when PUT disconnects
1612 if (NULL != (s->server_send))
1614 s->server_send->disconnect = GNUNET_YES;
1615 GNUNET_assert (NULL != s->server_send->mhd_conn);
1616 #if MHD_VERSION >= 0x00090E00
1617 MHD_set_connection_option (s->server_send->mhd_conn, MHD_CONNECTION_OPTION_TIMEOUT,
1620 server_reschedule (plugin, s->server_send->mhd_daemon, GNUNET_NO);
1622 if (s->msg_tk != NULL)
1624 GNUNET_SERVER_mst_destroy (s->msg_tk);
1630 plugin->cur_connections--;
1632 if ((s->server_send == NULL) && (s->server_recv == NULL))
1634 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1635 "Peer `%s' on address `%s' disconnected\n",
1636 GNUNET_i2s (&s->target),
1637 http_common_plugin_address_to_string (NULL,
1639 s->addr, s->addrlen));
1641 if ((GNUNET_YES == s->session_passed) && (GNUNET_NO == s->session_ended))
1643 /* Notify transport immediately that this session is invalid */
1644 s->session_ended = GNUNET_YES;
1645 plugin->env->session_end (plugin->env->cls, &s->target, s);
1647 server_delete_session (plugin, s);
1653 * Check if incoming connection is accepted.
1655 * @param cls plugin as closure
1656 * @param addr address of incoming connection
1657 * @param addr_len address length of incoming connection
1658 * @return MHD_YES if connection is accepted, MHD_NO if connection is rejected
1661 server_accept_cb (void *cls, const struct sockaddr *addr, socklen_t addr_len)
1663 struct HTTP_Server_Plugin *plugin = cls;
1665 if (plugin->cur_connections <= plugin->max_connections)
1667 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1668 _("Accepting connection (%u of %u) from `%s'\n"),
1669 plugin->cur_connections, plugin->max_connections,
1670 GNUNET_a2s (addr, addr_len));
1675 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, plugin->name,
1676 _("Server reached maximum number connections (%u), rejecting new connection\n"),
1677 plugin->max_connections);
1684 server_log (void *arg, const char *fmt, va_list ap)
1688 vsnprintf (text, sizeof (text), fmt, ap);
1690 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Server: %s\n", text);
1695 * Call MHD IPv4 to process pending requests and then go back
1696 * and schedule the next run.
1697 * @param cls plugin as closure
1698 * @param tc task context
1701 server_v4_run (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1703 struct HTTP_Server_Plugin *plugin = cls;
1705 GNUNET_assert (cls != NULL);
1707 plugin->server_v4_task = GNUNET_SCHEDULER_NO_TASK;
1708 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1711 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1712 "Running IPv4 server\n");
1714 plugin->server_v4_immediately = GNUNET_NO;
1715 GNUNET_assert (MHD_YES == MHD_run (plugin->server_v4));
1716 server_reschedule (plugin, plugin->server_v4, GNUNET_NO);
1721 * Call MHD IPv6 to process pending requests and then go back
1722 * and schedule the next run.
1723 * @param cls plugin as closure
1724 * @param tc task context
1727 server_v6_run (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1729 struct HTTP_Server_Plugin *plugin = cls;
1731 GNUNET_assert (cls != NULL);
1732 plugin->server_v6_task = GNUNET_SCHEDULER_NO_TASK;
1733 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1736 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1737 "Running IPv6 server\n");
1739 plugin->server_v6_immediately = GNUNET_NO;
1740 GNUNET_assert (MHD_YES == MHD_run (plugin->server_v6));
1741 server_reschedule (plugin, plugin->server_v6, GNUNET_NO);
1746 * Function that queries MHD's select sets and
1747 * starts the task waiting for them.
1749 * @param plugin plugin
1750 * @param daemon_handle the MHD daemon handle
1751 * @return gnunet task identifier
1753 static GNUNET_SCHEDULER_TaskIdentifier
1754 server_schedule (struct HTTP_Server_Plugin *plugin,
1755 struct MHD_Daemon *daemon_handle,
1758 GNUNET_SCHEDULER_TaskIdentifier ret;
1762 struct GNUNET_NETWORK_FDSet *wrs;
1763 struct GNUNET_NETWORK_FDSet *wws;
1764 struct GNUNET_NETWORK_FDSet *wes;
1766 MHD_UNSIGNED_LONG_LONG timeout;
1767 static unsigned long long last_timeout = 0;
1770 struct GNUNET_TIME_Relative tv;
1772 if (GNUNET_YES == plugin->in_shutdown)
1773 return GNUNET_SCHEDULER_NO_TASK;
1775 ret = GNUNET_SCHEDULER_NO_TASK;
1779 wrs = GNUNET_NETWORK_fdset_create ();
1780 wes = GNUNET_NETWORK_fdset_create ();
1781 wws = GNUNET_NETWORK_fdset_create ();
1783 GNUNET_assert (MHD_YES == MHD_get_fdset (daemon_handle, &rs, &ws, &es, &max));
1784 haveto = MHD_get_timeout (daemon_handle, &timeout);
1785 if (haveto == MHD_YES)
1787 if (timeout != last_timeout)
1790 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1791 "SELECT Timeout changed from %llu to %llu (ms)\n",
1792 last_timeout, timeout);
1793 last_timeout = timeout;
1795 if (timeout <= GNUNET_TIME_UNIT_SECONDS.rel_value_us / 1000LL)
1796 tv.rel_value_us = (uint64_t) timeout * 1000LL;
1798 tv = GNUNET_TIME_UNIT_SECONDS;
1801 tv = GNUNET_TIME_UNIT_SECONDS;
1802 /* Force immediate run, since we have outbound data to send */
1803 if (now == GNUNET_YES)
1804 tv = GNUNET_TIME_UNIT_MILLISECONDS;
1805 GNUNET_NETWORK_fdset_copy_native (wrs, &rs, max + 1);
1806 GNUNET_NETWORK_fdset_copy_native (wws, &ws, max + 1);
1807 GNUNET_NETWORK_fdset_copy_native (wes, &es, max + 1);
1809 if (daemon_handle == plugin->server_v4)
1811 if (plugin->server_v4_task != GNUNET_SCHEDULER_NO_TASK)
1813 GNUNET_SCHEDULER_cancel (plugin->server_v4_task);
1814 plugin->server_v4_task = GNUNET_SCHEDULER_NO_TASK;
1817 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1818 "Scheduling IPv4 server task in %llu ms\n", tv);
1821 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1823 &server_v4_run, plugin);
1825 if (daemon_handle == plugin->server_v6)
1827 if (plugin->server_v6_task != GNUNET_SCHEDULER_NO_TASK)
1829 GNUNET_SCHEDULER_cancel (plugin->server_v6_task);
1830 plugin->server_v6_task = GNUNET_SCHEDULER_NO_TASK;
1833 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1834 "Scheduling IPv6 server task in %llu ms\n", tv);
1837 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1839 &server_v6_run, plugin);
1841 GNUNET_NETWORK_fdset_destroy (wrs);
1842 GNUNET_NETWORK_fdset_destroy (wws);
1843 GNUNET_NETWORK_fdset_destroy (wes);
1850 * Load ssl certificate from file
1852 * @param file filename
1853 * @return content of the file
1856 server_load_file (const char *file)
1858 struct GNUNET_DISK_FileHandle *gn_file;
1862 if (GNUNET_OK != GNUNET_DISK_file_size (file,
1863 &fsize, GNUNET_NO, GNUNET_YES))
1865 text = GNUNET_malloc (fsize + 1);
1867 GNUNET_DISK_file_open (file, GNUNET_DISK_OPEN_READ,
1868 GNUNET_DISK_PERM_USER_READ);
1869 if (gn_file == NULL)
1874 if (GNUNET_SYSERR == GNUNET_DISK_file_read (gn_file, text, fsize))
1877 GNUNET_DISK_file_close (gn_file);
1881 GNUNET_DISK_file_close (gn_file);
1889 * Load ssl certificate
1891 * @param plugin the plugin
1892 * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
1895 server_load_certificate (struct HTTP_Server_Plugin *plugin)
1897 int res = GNUNET_OK;
1903 GNUNET_CONFIGURATION_get_value_filename (plugin->env->cfg, plugin->name,
1904 "KEY_FILE", &key_file))
1906 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
1907 plugin->name, "CERT_FILE");
1908 return GNUNET_SYSERR;
1911 GNUNET_CONFIGURATION_get_value_filename (plugin->env->cfg, plugin->name,
1912 "CERT_FILE", &cert_file))
1914 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
1915 plugin->name, "CERT_FILE");
1916 GNUNET_free (key_file);
1917 return GNUNET_SYSERR;
1919 /* Get crypto init string from config. If not present, use
1922 GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg,
1925 &plugin->crypto_init))
1926 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1927 "Using crypto init string `%s'\n",
1928 plugin->crypto_init);
1930 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1931 "Using default crypto init string \n");
1933 /* read key & certificates from file */
1934 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1935 "Trying to loading TLS certificate from key-file `%s' cert-file`%s'\n",
1936 key_file, cert_file);
1938 plugin->key = server_load_file (key_file);
1939 plugin->cert = server_load_file (cert_file);
1941 if ((plugin->key == NULL) || (plugin->cert == NULL))
1943 struct GNUNET_OS_Process *cert_creation;
1945 GNUNET_free_non_null (plugin->key);
1947 GNUNET_free_non_null (plugin->cert);
1948 plugin->cert = NULL;
1950 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1951 "No usable TLS certificate found, creating certificate\n");
1954 GNUNET_OS_start_process (GNUNET_NO, GNUNET_OS_INHERIT_STD_OUT_AND_ERR, NULL, NULL,
1955 "gnunet-transport-certificate-creation",
1956 "gnunet-transport-certificate-creation",
1957 key_file, cert_file, NULL);
1958 if (cert_creation == NULL)
1960 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
1962 ("Could not create a new TLS certificate, program `gnunet-transport-certificate-creation' could not be started!\n"));
1963 GNUNET_free (key_file);
1964 GNUNET_free (cert_file);
1966 GNUNET_free_non_null (plugin->key);
1968 GNUNET_free_non_null (plugin->cert);
1969 plugin->cert = NULL;
1970 GNUNET_free_non_null (plugin->crypto_init);
1971 plugin->crypto_init = NULL;
1973 return GNUNET_SYSERR;
1975 GNUNET_assert (GNUNET_OK == GNUNET_OS_process_wait (cert_creation));
1976 GNUNET_OS_process_destroy (cert_creation);
1978 plugin->key = server_load_file (key_file);
1979 plugin->cert = server_load_file (cert_file);
1982 if ((plugin->key == NULL) || (plugin->cert == NULL))
1984 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
1986 _("No usable TLS certificate found and creating one at `%s/%s' failed!\n"),
1987 key_file, cert_file);
1988 GNUNET_free (key_file);
1989 GNUNET_free (cert_file);
1991 GNUNET_free_non_null (plugin->key);
1993 GNUNET_free_non_null (plugin->cert);
1994 plugin->cert = NULL;
1995 GNUNET_free_non_null (plugin->crypto_init);
1996 plugin->crypto_init = NULL;
1998 return GNUNET_SYSERR;
2000 GNUNET_free (key_file);
2001 GNUNET_free (cert_file);
2002 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "TLS certificate loaded\n");
2009 * Start the HTTP server
2011 * @param plugin the plugin handle
2012 * @return GNUNET_OK on success, GNUNET_SYSERR on failure
2015 server_start (struct HTTP_Server_Plugin *plugin)
2017 unsigned int timeout;
2019 GNUNET_assert (NULL != plugin);
2022 if (GNUNET_SYSERR == server_load_certificate (plugin))
2024 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
2025 "Could not load or create server certificate! Loading plugin failed!\n");
2026 return GNUNET_SYSERR;
2031 #if MHD_VERSION >= 0x00090E00
2032 timeout = HTTP_SERVER_NOT_VALIDATED_TIMEOUT.rel_value_us / 1000LL / 1000LL;
2033 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2034 "MHD can set timeout per connection! Default time out %u sec.\n",
2037 timeout = HTTP_SERVER_SESSION_TIMEOUT.rel_value_us / 1000LL / 1000LL;
2038 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, plugin->name,
2039 "MHD cannot set timeout per connection! Default time out %u sec.\n",
2043 plugin->server_v4 = NULL;
2044 if (plugin->use_ipv4 == GNUNET_YES)
2046 plugin->server_v4 = MHD_start_daemon (
2053 MHD_NO_FLAG, plugin->port,
2054 &server_accept_cb, plugin,
2055 &server_access_cb, plugin,
2056 MHD_OPTION_SOCK_ADDR,
2057 (struct sockaddr_in *)
2058 plugin->server_addr_v4,
2059 MHD_OPTION_CONNECTION_LIMIT,
2061 plugin->max_connections,
2063 MHD_OPTION_HTTPS_PRIORITIES,
2064 plugin->crypto_init,
2065 MHD_OPTION_HTTPS_MEM_KEY,
2067 MHD_OPTION_HTTPS_MEM_CERT,
2070 MHD_OPTION_CONNECTION_TIMEOUT,
2072 MHD_OPTION_CONNECTION_MEMORY_LIMIT,
2074 GNUNET_SERVER_MAX_MESSAGE_SIZE),
2075 MHD_OPTION_NOTIFY_COMPLETED,
2076 &server_disconnect_cb, plugin,
2077 MHD_OPTION_EXTERNAL_LOGGER,
2078 server_log, NULL, MHD_OPTION_END);
2079 if (plugin->server_v4 == NULL)
2081 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
2082 "Failed to start %s IPv4 server component on port %u\n",
2083 plugin->name, plugin->port);
2086 server_reschedule (plugin, plugin->server_v4, GNUNET_NO);
2090 plugin->server_v6 = NULL;
2091 if (plugin->use_ipv6 == GNUNET_YES)
2093 plugin->server_v6 = MHD_start_daemon (
2100 MHD_USE_IPv6, plugin->port,
2101 &server_accept_cb, plugin,
2102 &server_access_cb, plugin,
2103 MHD_OPTION_SOCK_ADDR,
2104 (struct sockaddr_in6 *)
2105 plugin->server_addr_v6,
2106 MHD_OPTION_CONNECTION_LIMIT,
2108 plugin->max_connections,
2110 MHD_OPTION_HTTPS_PRIORITIES,
2111 plugin->crypto_init,
2112 MHD_OPTION_HTTPS_MEM_KEY,
2114 MHD_OPTION_HTTPS_MEM_CERT,
2117 MHD_OPTION_CONNECTION_TIMEOUT,
2119 MHD_OPTION_CONNECTION_MEMORY_LIMIT,
2121 GNUNET_SERVER_MAX_MESSAGE_SIZE),
2122 MHD_OPTION_NOTIFY_COMPLETED,
2123 &server_disconnect_cb, plugin,
2124 MHD_OPTION_EXTERNAL_LOGGER,
2125 server_log, NULL, MHD_OPTION_END);
2126 if (plugin->server_v6 == NULL)
2128 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
2129 "Failed to start %s IPv6 server component on port %u\n",
2130 plugin->name, plugin->port);
2133 server_reschedule (plugin, plugin->server_v6, GNUNET_NO);
2137 if ((plugin->server_v6 == NULL) && (plugin->server_v4 == NULL))
2139 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
2140 "%s %s server component started on port %u\n",
2141 msg, plugin->name, plugin->port);
2143 return GNUNET_SYSERR;
2145 else if ((plugin->server_v6 != NULL) && (plugin->server_v4 != NULL))
2146 msg = "IPv4 and IPv6";
2147 else if (plugin->server_v6 != NULL)
2149 else if (plugin->server_v4 != NULL)
2151 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2152 "%s %s server component started on port %u\n",
2153 msg, plugin->name, plugin->port);
2159 server_stop (struct HTTP_Server_Plugin *plugin)
2161 if (plugin->server_v4 != NULL)
2163 MHD_stop_daemon (plugin->server_v4);
2164 plugin->server_v4 = NULL;
2166 if ( plugin->server_v6 != NULL)
2168 MHD_stop_daemon (plugin->server_v6);
2169 plugin->server_v6 = NULL;
2173 if (plugin->server_v4_task != GNUNET_SCHEDULER_NO_TASK)
2175 GNUNET_SCHEDULER_cancel (plugin->server_v4_task);
2176 plugin->server_v4_task = GNUNET_SCHEDULER_NO_TASK;
2179 if (plugin->server_v6_task != GNUNET_SCHEDULER_NO_TASK)
2181 GNUNET_SCHEDULER_cancel (plugin->server_v6_task);
2182 plugin->server_v6_task = GNUNET_SCHEDULER_NO_TASK;
2185 GNUNET_free_non_null (plugin->crypto_init);
2186 GNUNET_free_non_null (plugin->cert);
2187 GNUNET_free_non_null (plugin->key);
2190 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2191 "%s server component stopped\n", plugin->name);
2196 * Add an address to the server's set of addresses and notify transport
2198 * @param cls the plugin handle
2199 * @param add_remove GNUNET_YES on add, GNUNET_NO on remove
2200 * @param addr the address
2201 * @param addrlen address length
2204 server_add_address (void *cls, int add_remove, const struct sockaddr *addr,
2207 struct HTTP_Server_Plugin *plugin = cls;
2208 struct HttpAddressWrapper *w = NULL;
2210 w = GNUNET_malloc (sizeof (struct HttpAddressWrapper));
2211 w->address = http_common_address_from_socket (plugin->protocol, addr, addrlen);
2212 if (NULL == w->address)
2217 w->addrlen = http_common_address_get_size (w->address);
2219 GNUNET_CONTAINER_DLL_insert(plugin->addr_head, plugin->addr_tail, w);
2220 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2221 "Notifying transport to add address `%s'\n",
2222 http_common_plugin_address_to_string (NULL,
2224 w->address, w->addrlen));
2226 plugin->env->notify_address (plugin->env->cls, add_remove, w->address, w->addrlen, "https_client");
2228 plugin->env->notify_address (plugin->env->cls, add_remove, w->address, w->addrlen, "http_client");
2234 * Remove an address from the server's set of addresses and notify transport
2236 * @param cls the plugin handle
2237 * @param add_remove GNUNET_YES on add, GNUNET_NO on remove
2238 * @param addr the address
2239 * @param addrlen address length
2242 server_remove_address (void *cls, int add_remove, const struct sockaddr *addr,
2245 struct HTTP_Server_Plugin *plugin = cls;
2246 struct HttpAddressWrapper *w = plugin->addr_head;
2248 void * saddr = http_common_address_from_socket (plugin->protocol, addr, addrlen);
2251 saddr_len = http_common_address_get_size (saddr);
2255 if (GNUNET_YES == http_common_cmp_addresses(w->address, w->addrlen, saddr, saddr_len))
2259 GNUNET_free (saddr);
2264 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2265 "Notifying transport to remove address `%s'\n",
2266 http_common_plugin_address_to_string (NULL,
2268 w->address, w->addrlen));
2269 GNUNET_CONTAINER_DLL_remove (plugin->addr_head, plugin->addr_tail, w);
2271 plugin->env->notify_address (plugin->env->cls, add_remove, w->address, w->addrlen, "https_client");
2273 plugin->env->notify_address (plugin->env->cls, add_remove, w->address, w->addrlen, "http_client");
2275 GNUNET_free (w->address);
2282 * Our external IP address/port mapping has changed.
2284 * @param cls closure, the 'struct LocalAddrList'
2285 * @param add_remove GNUNET_YES to mean the new public IP address, GNUNET_NO to mean
2286 * the previous (now invalid) one
2287 * @param addr either the previous or the new public IP address
2288 * @param addrlen actual lenght of the address
2291 server_nat_port_map_callback (void *cls, int add_remove, const struct sockaddr *addr,
2294 GNUNET_assert (cls != NULL);
2295 struct HTTP_Server_Plugin *plugin = cls;
2297 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2298 "NAT called to %s address `%s'\n",
2299 (add_remove == GNUNET_NO) ? "remove" : "add",
2300 GNUNET_a2s (addr, addrlen));
2302 if (AF_INET == addr->sa_family)
2304 struct sockaddr_in *s4 = (struct sockaddr_in *) addr;
2306 if (GNUNET_NO == plugin->use_ipv4)
2309 if ((NULL != plugin->server_addr_v4) &&
2310 (0 != memcmp (&plugin->server_addr_v4->sin_addr,
2311 &s4->sin_addr, sizeof (struct in_addr))))
2313 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2314 "Skipping address `%s' (not bindto address)\n",
2315 GNUNET_a2s (addr, addrlen));
2320 if (AF_INET6 == addr->sa_family)
2322 struct sockaddr_in6 *s6 = (struct sockaddr_in6 *) addr;
2323 if (GNUNET_NO == plugin->use_ipv6)
2326 if ((NULL != plugin->server_addr_v6) &&
2327 (0 != memcmp (&plugin->server_addr_v6->sin6_addr,
2328 &s6->sin6_addr, sizeof (struct in6_addr))))
2330 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2331 "Skipping address `%s' (not bindto address)\n",
2332 GNUNET_a2s (addr, addrlen));
2340 server_add_address (cls, add_remove, addr, addrlen);
2343 server_remove_address (cls, add_remove, addr, addrlen);
2350 * Get valid server addresses
2352 * @param plugin the plugin handle
2353 * @param service_name the servicename
2354 * @param cfg configuration handle
2355 * @param addrs addresses
2356 * @param addr_lens address length
2357 * @return number of addresses
2360 server_get_addresses (struct HTTP_Server_Plugin *plugin,
2361 const char *service_name,
2362 const struct GNUNET_CONFIGURATION_Handle *cfg,
2363 struct sockaddr ***addrs, socklen_t ** addr_lens)
2366 unsigned long long port;
2367 struct addrinfo hints;
2368 struct addrinfo *res;
2369 struct addrinfo *pos;
2370 struct addrinfo *next;
2374 struct sockaddr **saddrs;
2375 socklen_t *saddrlens;
2381 disablev6 = !plugin->use_ipv6;
2384 if (GNUNET_CONFIGURATION_have_value (cfg, service_name, "PORT"))
2386 GNUNET_break (GNUNET_OK ==
2387 GNUNET_CONFIGURATION_get_value_number (cfg, service_name,
2391 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2393 ("Require valid port number for service in configuration!\n"));
2394 return GNUNET_SYSERR;
2399 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, plugin->name,
2400 "Starting in listen only mode\n");
2401 return -1; /* listen only */
2405 if (GNUNET_CONFIGURATION_have_value (cfg, service_name, "BINDTO"))
2407 GNUNET_break (GNUNET_OK ==
2408 GNUNET_CONFIGURATION_get_value_string (cfg, service_name,
2409 "BINDTO", &hostname));
2414 if (hostname != NULL)
2416 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2417 "Resolving `%s' since that is where `%s' will bind to.\n",
2418 hostname, service_name);
2419 memset (&hints, 0, sizeof (struct addrinfo));
2421 hints.ai_family = AF_INET;
2422 if ((0 != (ret = getaddrinfo (hostname, NULL, &hints, &res))) ||
2425 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Failed to resolve `%s': %s\n"),
2426 hostname, gai_strerror (ret));
2427 GNUNET_free (hostname);
2428 return GNUNET_SYSERR;
2432 while (NULL != (pos = next))
2434 next = pos->ai_next;
2435 if ((disablev6) && (pos->ai_family == AF_INET6))
2441 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2442 _("Failed to find %saddress for `%s'.\n"),
2443 disablev6 ? "IPv4 " : "", hostname);
2445 GNUNET_free (hostname);
2446 return GNUNET_SYSERR;
2449 saddrs = GNUNET_malloc ((resi + 1) * sizeof (struct sockaddr *));
2450 saddrlens = GNUNET_malloc ((resi + 1) * sizeof (socklen_t));
2453 while (NULL != (pos = next))
2455 next = pos->ai_next;
2456 if ((disablev6) && (pos->ai_family == AF_INET6))
2458 if ((pos->ai_protocol != IPPROTO_TCP) && (pos->ai_protocol != 0))
2459 continue; /* not TCP */
2460 if ((pos->ai_socktype != SOCK_STREAM) && (pos->ai_socktype != 0))
2461 continue; /* huh? */
2462 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2463 "Service will bind to `%s'\n", GNUNET_a2s (pos->ai_addr,
2465 if (pos->ai_family == AF_INET)
2467 GNUNET_assert (pos->ai_addrlen == sizeof (struct sockaddr_in));
2468 saddrlens[i] = pos->ai_addrlen;
2469 saddrs[i] = GNUNET_malloc (saddrlens[i]);
2470 memcpy (saddrs[i], pos->ai_addr, saddrlens[i]);
2471 ((struct sockaddr_in *) saddrs[i])->sin_port = htons (port);
2475 GNUNET_assert (pos->ai_family == AF_INET6);
2476 GNUNET_assert (pos->ai_addrlen == sizeof (struct sockaddr_in6));
2477 saddrlens[i] = pos->ai_addrlen;
2478 saddrs[i] = GNUNET_malloc (saddrlens[i]);
2479 memcpy (saddrs[i], pos->ai_addr, saddrlens[i]);
2480 ((struct sockaddr_in6 *) saddrs[i])->sin6_port = htons (port);
2484 GNUNET_free (hostname);
2490 /* will bind against everything, just set port */
2496 saddrs = GNUNET_malloc ((resi + 1) * sizeof (struct sockaddr *));
2497 saddrlens = GNUNET_malloc ((resi + 1) * sizeof (socklen_t));
2499 saddrlens[i] = sizeof (struct sockaddr_in);
2500 saddrs[i] = GNUNET_malloc (saddrlens[i]);
2501 #if HAVE_SOCKADDR_IN_SIN_LEN
2502 ((struct sockaddr_in *) saddrs[i])->sin_len = saddrlens[i];
2504 ((struct sockaddr_in *) saddrs[i])->sin_family = AF_INET;
2505 ((struct sockaddr_in *) saddrs[i])->sin_port = htons (port);
2511 saddrs = GNUNET_malloc ((resi + 1) * sizeof (struct sockaddr *));
2512 saddrlens = GNUNET_malloc ((resi + 1) * sizeof (socklen_t));
2514 saddrlens[i] = sizeof (struct sockaddr_in6);
2515 saddrs[i] = GNUNET_malloc (saddrlens[i]);
2516 #if HAVE_SOCKADDR_IN_SIN_LEN
2517 ((struct sockaddr_in6 *) saddrs[i])->sin6_len = saddrlens[0];
2519 ((struct sockaddr_in6 *) saddrs[i])->sin6_family = AF_INET6;
2520 ((struct sockaddr_in6 *) saddrs[i])->sin6_port = htons (port);
2522 saddrlens[i] = sizeof (struct sockaddr_in);
2523 saddrs[i] = GNUNET_malloc (saddrlens[i]);
2524 #if HAVE_SOCKADDR_IN_SIN_LEN
2525 ((struct sockaddr_in *) saddrs[i])->sin_len = saddrlens[1];
2527 ((struct sockaddr_in *) saddrs[i])->sin_family = AF_INET;
2528 ((struct sockaddr_in *) saddrs[i])->sin_port = htons (port);
2532 *addr_lens = saddrlens;
2538 * Ask NAT for addresses
2540 * @param plugin the plugin handle
2543 server_start_report_addresses (struct HTTP_Server_Plugin *plugin)
2545 int res = GNUNET_OK;
2546 struct sockaddr **addrs;
2547 socklen_t *addrlens;
2549 res = server_get_addresses (plugin,
2550 plugin->name, plugin->env->cfg,
2552 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2553 _("Found %u addresses to report to NAT service\n"), res);
2555 if (GNUNET_SYSERR == res)
2562 GNUNET_NAT_register (plugin->env->cfg, GNUNET_YES, plugin->port,
2564 (const struct sockaddr **) addrs, addrlens,
2565 &server_nat_port_map_callback, NULL, plugin);
2569 GNUNET_assert (addrs[res] != NULL);
2570 GNUNET_free (addrs[res]);
2572 GNUNET_free_non_null (addrs);
2573 GNUNET_free_non_null (addrlens);
2578 * Stop NAT for addresses
2580 * @param plugin the plugin handle
2583 server_stop_report_addresses (struct HTTP_Server_Plugin *plugin)
2585 /* Stop NAT handle */
2586 if (NULL != plugin->nat)
2587 GNUNET_NAT_unregister (plugin->nat);
2589 /* Clean up addresses */
2590 struct HttpAddressWrapper *w;
2592 while (plugin->addr_head != NULL)
2594 w = plugin->addr_head;
2595 GNUNET_CONTAINER_DLL_remove (plugin->addr_head, plugin->addr_tail, w);
2596 GNUNET_free (w->address);
2603 * Check if IPv6 supported on this system
2605 * @param plugin the plugin handle
2606 * @return GNUNET_YES on success, else GNUNET_NO
2609 server_check_ipv6_support (struct HTTP_Server_Plugin *plugin)
2611 struct GNUNET_NETWORK_Handle *desc = NULL;
2612 int res = GNUNET_NO;
2614 /* Probe IPv6 support */
2615 desc = GNUNET_NETWORK_socket_create (PF_INET6, SOCK_STREAM, 0);
2618 if ((errno == ENOBUFS) || (errno == ENOMEM) || (errno == ENFILE) ||
2621 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "socket");
2623 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, plugin->name,
2625 ("Disabling IPv6 since it is not supported on this system!\n"));
2630 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (desc));
2634 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2635 "Testing IPv6 on this system: %s\n",
2636 (res == GNUNET_YES) ? "successful" : "failed");
2642 * Notify server about our external hostname
2645 * @param tc task context (unused)
2648 server_notify_external_hostname (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2650 struct HTTP_Server_Plugin *plugin = cls;
2654 plugin->notify_ext_task = GNUNET_SCHEDULER_NO_TASK;
2655 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
2658 GNUNET_asprintf(&url, "%s://%s", plugin->protocol, plugin->external_hostname);
2660 urlen = strlen (url) + 1;
2661 plugin->ext_addr = GNUNET_malloc (sizeof (struct HttpAddress) + urlen);
2662 plugin->ext_addr->options = htonl(plugin->options);
2663 plugin->ext_addr->urlen = htonl (urlen);
2664 plugin->ext_addr_len = sizeof (struct HttpAddress) + urlen;
2665 memcpy (&plugin->ext_addr[1], url, urlen);
2667 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2668 "Notifying transport about external hostname address `%s'\n", plugin->external_hostname);
2671 if (GNUNET_YES == plugin->verify_external_hostname)
2672 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, plugin->name,
2673 "Enabling SSL verification for external hostname address `%s'\n", plugin->external_hostname);
2674 plugin->env->notify_address (plugin->env->cls, GNUNET_YES,
2675 plugin->ext_addr, plugin->ext_addr_len,
2678 plugin->env->notify_address (plugin->env->cls, GNUNET_YES,
2679 plugin->ext_addr, plugin->ext_addr_len,
2686 * Configure the plugin
2688 * @param plugin plugin handle
2689 * @return GNUNET_OK on success, GNUNET_SYSERR on failure
2692 server_configure_plugin (struct HTTP_Server_Plugin *plugin)
2694 unsigned long long port;
2695 unsigned long long max_connections;
2696 char *bind4_address = NULL;
2697 char *bind6_address = NULL;
2698 char *eh_tmp = NULL;
2699 int external_hostname_use_port;
2702 if (GNUNET_CONFIGURATION_have_value
2703 (plugin->env->cfg, plugin->name, "USE_IPv4"))
2706 GNUNET_CONFIGURATION_get_value_yesno (plugin->env->cfg, plugin->name,
2710 plugin->use_ipv4 = GNUNET_YES;
2711 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2712 _("IPv4 support is %s\n"),
2713 (plugin->use_ipv4 == GNUNET_YES) ? "enabled" : "disabled");
2716 if (GNUNET_CONFIGURATION_have_value
2717 (plugin->env->cfg, plugin->name, "USE_IPv6"))
2720 GNUNET_CONFIGURATION_get_value_yesno (plugin->env->cfg, plugin->name,
2724 plugin->use_ipv6 = GNUNET_YES;
2725 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2726 _("IPv6 support is %s\n"),
2727 (plugin->use_ipv6 == GNUNET_YES) ? "enabled" : "disabled");
2729 if ((plugin->use_ipv4 == GNUNET_NO) && (plugin->use_ipv6 == GNUNET_NO))
2731 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
2733 ("Neither IPv4 nor IPv6 are enabled! Fix in configuration\n"),
2735 return GNUNET_SYSERR;
2738 /* Reading port number from config file */
2740 GNUNET_CONFIGURATION_get_value_number (plugin->env->cfg, plugin->name,
2741 "PORT", &port)) || (port > 65535))
2743 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
2744 _("Port is required! Fix in configuration\n"),
2746 return GNUNET_SYSERR;
2748 plugin->port = port;
2750 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, plugin->name,
2751 _("Using port %u\n"), plugin->port);
2753 if ((plugin->use_ipv4 == GNUNET_YES) &&
2754 (GNUNET_YES == GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg,
2755 plugin->name, "BINDTO", &bind4_address)))
2757 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2758 "Binding %s plugin to specific IPv4 address: `%s'\n",
2759 plugin->protocol, bind4_address);
2760 plugin->server_addr_v4 = GNUNET_malloc (sizeof (struct sockaddr_in));
2761 if (1 != inet_pton (AF_INET, bind4_address,
2762 &plugin->server_addr_v4->sin_addr))
2764 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
2766 ("Specific IPv4 address `%s' in configuration file is invalid!\n"),
2768 GNUNET_free (bind4_address);
2769 GNUNET_free (plugin->server_addr_v4);
2770 plugin->server_addr_v4 = NULL;
2771 return GNUNET_SYSERR;
2775 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2776 _("Binding to IPv4 address %s\n"), bind4_address);
2777 plugin->server_addr_v4->sin_family = AF_INET;
2778 plugin->server_addr_v4->sin_port = htons (plugin->port);
2780 GNUNET_free (bind4_address);
2783 if ((plugin->use_ipv6 == GNUNET_YES) &&
2785 GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg, plugin->name,
2786 "BINDTO6", &bind6_address)))
2788 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2789 "Binding %s plugin to specific IPv6 address: `%s'\n",
2790 plugin->protocol, bind6_address);
2791 plugin->server_addr_v6 = GNUNET_malloc (sizeof (struct sockaddr_in6));
2793 inet_pton (AF_INET6, bind6_address, &plugin->server_addr_v6->sin6_addr))
2795 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
2797 ("Specific IPv6 address `%s' in configuration file is invalid!\n"),
2799 GNUNET_free (bind6_address);
2800 GNUNET_free (plugin->server_addr_v6);
2801 plugin->server_addr_v6 = NULL;
2802 return GNUNET_SYSERR;
2806 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2807 _("Binding to IPv6 address %s\n"), bind6_address);
2808 plugin->server_addr_v6->sin6_family = AF_INET6;
2809 plugin->server_addr_v6->sin6_port = htons (plugin->port);
2811 GNUNET_free (bind6_address);
2814 plugin->verify_external_hostname = GNUNET_NO;
2816 plugin->verify_external_hostname = GNUNET_CONFIGURATION_get_value_yesno (plugin->env->cfg, plugin->name,
2817 "VERIFY_EXTERNAL_HOSTNAME");
2818 if (GNUNET_SYSERR == plugin->verify_external_hostname)
2819 plugin->verify_external_hostname = GNUNET_NO;
2820 if (GNUNET_YES == plugin->verify_external_hostname)
2821 plugin->options |= HTTP_OPTIONS_VERIFY_CERTIFICATE;
2823 external_hostname_use_port = GNUNET_CONFIGURATION_get_value_yesno (plugin->env->cfg, plugin->name,
2824 "EXTERNAL_HOSTNAME_USE_PORT");
2825 if (GNUNET_SYSERR == external_hostname_use_port)
2826 external_hostname_use_port = GNUNET_NO;
2829 if (GNUNET_YES == GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg, plugin->name,
2830 "EXTERNAL_HOSTNAME", &eh_tmp))
2834 char * pos_url = NULL;
2836 if (NULL != strstr(eh_tmp, "://"))
2838 tmp = &strstr(eh_tmp, "://")[3];
2843 if (GNUNET_YES == external_hostname_use_port)
2845 if ( (strlen (tmp) > 1) && (NULL != (pos = strchr(tmp, '/'))) )
2849 GNUNET_asprintf (&plugin->external_hostname, "%s:%u/%s", tmp, (uint16_t) port, (NULL == pos_url) ? "" : pos_url);
2852 GNUNET_asprintf (&plugin->external_hostname, "%s:%u", tmp, (uint16_t) port);
2855 plugin->external_hostname = GNUNET_strdup (tmp);
2856 GNUNET_free (eh_tmp);
2858 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, plugin->name,
2859 _("Using external hostname `%s'\n"), plugin->external_hostname);
2860 plugin->notify_ext_task = GNUNET_SCHEDULER_add_now (&server_notify_external_hostname, plugin);
2862 /* Use only configured external hostname */
2863 if (GNUNET_CONFIGURATION_have_value
2864 (plugin->env->cfg, plugin->name, "EXTERNAL_HOSTNAME_ONLY"))
2866 plugin->external_only =
2867 GNUNET_CONFIGURATION_get_value_yesno (plugin->env->cfg, plugin->name,
2868 "EXTERNAL_HOSTNAME_ONLY");
2871 plugin->external_only = GNUNET_NO;
2873 if (GNUNET_YES == plugin->external_only)
2874 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2875 _("Notifying transport only about hostname `%s'\n"), plugin->external_hostname);
2878 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2879 "No external hostname configured\n");
2881 /* Optional parameters */
2882 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (plugin->env->cfg,
2884 "MAX_CONNECTIONS", &max_connections))
2885 max_connections = 128;
2886 plugin->max_connections = max_connections;
2888 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2889 _("Maximum number of connections is %u\n"),
2890 plugin->max_connections);
2893 plugin->peer_id_length = strlen (GNUNET_i2s_full (plugin->env->my_identity));
2900 * Session was idle, so disconnect it
2902 * @param cls the session
2903 * @param tc task context
2906 server_session_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2908 struct Session *s = cls;
2910 s->timeout_task = GNUNET_SCHEDULER_NO_TASK;
2911 GNUNET_log (TIMEOUT_LOG,
2912 "Session %p was idle for %s, disconnecting\n",
2914 GNUNET_STRINGS_relative_time_to_string (HTTP_SERVER_SESSION_TIMEOUT,
2917 /* call session destroy function */
2918 GNUNET_assert (GNUNET_OK ==
2919 http_server_plugin_disconnect_session (s->plugin, s));
2924 * Start session timeout for session s
2926 * @param s the session
2929 server_start_session_timeout (struct Session *s)
2931 GNUNET_assert (NULL != s);
2932 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == s->timeout_task);
2933 s->timeout_task = GNUNET_SCHEDULER_add_delayed (HTTP_SERVER_SESSION_TIMEOUT,
2934 &server_session_timeout,
2936 GNUNET_log (TIMEOUT_LOG,
2937 "Timeout for session %p set to %s\n",
2939 GNUNET_STRINGS_relative_time_to_string (HTTP_SERVER_SESSION_TIMEOUT,
2945 * Increment session timeout due to activity session s
2947 * @param s the session
2950 server_reschedule_session_timeout (struct Session *s)
2952 GNUNET_assert (NULL != s);
2953 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK != s->timeout_task);
2955 GNUNET_SCHEDULER_cancel (s->timeout_task);
2956 s->timeout_task = GNUNET_SCHEDULER_add_delayed (HTTP_SERVER_SESSION_TIMEOUT,
2957 &server_session_timeout,
2959 GNUNET_log (TIMEOUT_LOG,
2960 "Timeout rescheduled for session %p set to %s\n",
2962 GNUNET_STRINGS_relative_time_to_string (HTTP_SERVER_SESSION_TIMEOUT,
2968 * Exit point from the plugin.
2974 LIBGNUNET_PLUGIN_TRANSPORT_DONE (void *cls)
2976 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
2977 struct HTTP_Server_Plugin *plugin = api->cls;
2978 struct Session *pos;
2979 struct Session *next;
2981 if (NULL == api->cls)
2983 /* Free for stub mode */
2987 plugin->in_shutdown = GNUNET_YES;
2988 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2989 _("Shutting down plugin `%s'\n"),
2992 if (GNUNET_SCHEDULER_NO_TASK != plugin->notify_ext_task)
2994 GNUNET_SCHEDULER_cancel (plugin->notify_ext_task);
2995 plugin->notify_ext_task = GNUNET_SCHEDULER_NO_TASK;
2998 if (NULL != plugin->ext_addr)
3000 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
3001 "Notifying transport to remove address `%s'\n",
3002 http_common_plugin_address_to_string (NULL,
3005 plugin->ext_addr_len));
3007 plugin->env->notify_address (plugin->env->cls,
3010 plugin->ext_addr_len,
3013 plugin->env->notify_address (plugin->env->cls,
3016 plugin->ext_addr_len,
3021 /* Stop to report addresses to transport service */
3022 server_stop_report_addresses (plugin);
3023 server_stop (plugin);
3024 next = plugin->head;
3025 while (NULL != (pos = next))
3028 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
3029 "Removing left over session %p\n", pos);
3031 if ((GNUNET_YES == pos->session_passed) && (GNUNET_NO == pos->session_ended))
3033 /* Notify transport immediately that this session is invalid */
3034 pos->session_ended = GNUNET_YES;
3035 plugin->env->session_end (plugin->env->cls, &pos->target, pos);
3037 server_delete_session (plugin, pos);
3041 GNUNET_free_non_null (plugin->external_hostname);
3042 GNUNET_free_non_null (plugin->ext_addr);
3043 GNUNET_free_non_null (plugin->server_addr_v4);
3044 GNUNET_free_non_null (plugin->server_addr_v6);
3046 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
3047 _("Shutdown for plugin `%s' complete\n"),
3050 GNUNET_free (plugin);
3057 http_plugin_address_to_string (void *cls,
3062 return http_common_plugin_address_to_string (cls, PLUGIN_NAME, addr, addrlen);
3064 return http_common_plugin_address_to_string (cls, PLUGIN_NAME, addr, addrlen);
3070 * Function obtain the network type for a session
3072 * @param cls closure ('struct Plugin*')
3073 * @param session the session
3074 * @return the network type in HBO or GNUNET_SYSERR
3076 static enum GNUNET_ATS_Network_Type
3077 http_server_get_network (void *cls,
3078 struct Session *session)
3080 GNUNET_assert (NULL != session);
3081 return ntohl (session->ats_address_network_type);
3086 * Entry point for the plugin.
3092 LIBGNUNET_PLUGIN_TRANSPORT_INIT (void *cls)
3094 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
3095 struct GNUNET_TRANSPORT_PluginFunctions *api;
3096 struct HTTP_Server_Plugin *plugin;
3098 if (NULL == env->receive)
3100 /* run in 'stub' mode (i.e. as part of gnunet-peerinfo), don't fully
3101 initialze the plugin or the API */
3102 api = GNUNET_new (struct GNUNET_TRANSPORT_PluginFunctions);
3104 api->address_to_string = &http_plugin_address_to_string;
3105 api->string_to_address = &http_common_plugin_string_to_address;
3106 api->address_pretty_printer = &http_common_plugin_address_pretty_printer;
3109 plugin = GNUNET_new (struct HTTP_Server_Plugin);
3111 api = GNUNET_new (struct GNUNET_TRANSPORT_PluginFunctions);
3113 api->send = &http_server_plugin_send;
3114 api->disconnect_peer = &http_server_plugin_disconnect_peer;
3115 api->disconnect_session = &http_server_plugin_disconnect_session;
3116 api->query_keepalive_factor = &http_server_query_keepalive_factor;
3117 api->check_address = &http_server_plugin_address_suggested;
3118 api->get_session = &http_server_plugin_get_session;
3120 api->address_to_string = &http_plugin_address_to_string;
3121 api->string_to_address = &http_common_plugin_string_to_address;
3122 api->address_pretty_printer = &http_common_plugin_address_pretty_printer;
3123 api->get_network = &http_server_get_network;
3126 plugin->name = "transport-https_server";
3127 plugin->protocol = "https";
3129 plugin->name = "transport-http_server";
3130 plugin->protocol = "http";
3133 /* Configure plugin */
3134 if (GNUNET_SYSERR == server_configure_plugin (plugin))
3136 LIBGNUNET_PLUGIN_TRANSPORT_DONE (api);
3140 /* Check IPv6 support */
3141 if (GNUNET_YES == plugin->use_ipv6)
3142 plugin->use_ipv6 = server_check_ipv6_support (plugin);
3144 /* Report addresses to transport service */
3145 if (GNUNET_NO == plugin->external_only)
3146 server_start_report_addresses (plugin);
3148 if (GNUNET_SYSERR == server_start (plugin))
3150 LIBGNUNET_PLUGIN_TRANSPORT_DONE (api);
3156 /* end of plugin_transport_http_server.c */