2 This file is part of GNUnet
3 (C) 2002-2014 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
25 * @author David Barksdale
28 #include "gnunet_util_lib.h"
29 #include "gnunet_statistics_service.h"
30 #include "gnunet_transport_plugin.h"
31 #include "gnunet_nat_lib.h"
32 #include "plugin_transport_http_common.h"
33 #include <microhttpd.h>
38 #define PLUGIN_NAME "https_server"
39 #define LIBGNUNET_PLUGIN_TRANSPORT_INIT libgnunet_plugin_transport_https_server_init
40 #define LIBGNUNET_PLUGIN_TRANSPORT_DONE libgnunet_plugin_transport_https_server_done
42 #define PLUGIN_NAME "http_server"
43 #define LIBGNUNET_PLUGIN_TRANSPORT_INIT libgnunet_plugin_transport_http_server_init
44 #define LIBGNUNET_PLUGIN_TRANSPORT_DONE libgnunet_plugin_transport_http_server_done
47 #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>"
53 * Enable output for debbuging URL's of incoming requests
55 #define DEBUG_URL_PARSE GNUNET_NO
59 * Encapsulation of all of the state of the plugin.
64 * Session handle for connections.
69 * Stored in a linked list.
74 * Stored in a linked list.
79 * To whom are we talking to (set to our identity
80 * if we are still waiting for the welcome message)
82 struct GNUNET_PeerIdentity target;
85 * Pointer to the global plugin struct.
87 struct HTTP_Server_Plugin *plugin;
90 * next pointer for double linked list
92 struct HTTP_Message *msg_head;
95 * previous pointer for double linked list
97 struct HTTP_Message *msg_tail;
100 * Message stream tokenizer for incoming data
102 struct GNUNET_SERVER_MessageStreamTokenizer *msg_tk;
107 struct ServerConnection *server_recv;
112 struct ServerConnection *server_send;
117 struct GNUNET_HELLO_Address *address;
120 * Unique HTTP/S connection tag for this connection
125 * ATS network type in NBO
127 uint32_t ats_address_network_type;
130 * Was session given to transport service?
135 * Did we immediately end the session in disconnect_cb
140 * Are incoming connection established at the moment
142 int connect_in_progress;
145 * Absolute time when to receive data again
146 * Used for receive throttling
148 struct GNUNET_TIME_Absolute next_receive;
151 * Session timeout task
153 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
157 struct ServerConnection
165 * Should this connection get disconnected? GNUNET_YES/NO
170 * For PUT connections: Is this the first or last callback with size 0
175 * The session this server connection belongs to
177 struct Session *session;
182 struct MHD_Connection *mhd_conn;
187 struct MHD_Daemon *mhd_daemon;
192 * Encapsulation of all of the state of the plugin.
194 struct HTTP_Server_Plugin
199 struct GNUNET_TRANSPORT_PluginEnvironment *env;
202 * Linked list head of open sessions.
204 struct Session *head;
207 * Linked list tail of open sessions.
209 struct Session *tail;
222 * My options to be included in the address
229 char *external_hostname;
232 * Verify external address
234 int verify_external_hostname;
237 * Maximum number of sockets the plugin can use
238 * Each http inbound /outbound connections are two connections
240 unsigned int max_connections;
243 * Current number of sockets the plugin can use
244 * Each http inbound /outbound connections are two connections
246 unsigned int cur_connections;
249 * Did we immediately end the session in disconnect_cb
259 * External hostname the plugin can be connected to, can be different to
260 * the host's FQDN, used e.g. for reverse proxying
262 struct GNUNET_HELLO_Address *ext_addr;
265 * Notify transport only about external address
267 unsigned int external_only;
285 * Task calling transport service about external address
287 GNUNET_SCHEDULER_TaskIdentifier notify_ext_task;
290 * NAT handle & address management
292 struct GNUNET_NAT_Handle *nat;
295 * List of own addresses
299 * IPv4 addresses DLL head
301 struct HttpAddressWrapper *addr_head;
304 * IPv4 addresses DLL tail
306 struct HttpAddressWrapper *addr_tail;
309 * IPv4 server socket to bind to
311 struct sockaddr_in *server_addr_v4;
314 * IPv6 server socket to bind to
316 struct sockaddr_in6 *server_addr_v6;
321 GNUNET_SCHEDULER_TaskIdentifier server_v4_task;
326 GNUNET_SCHEDULER_TaskIdentifier server_v6_task;
329 * The IPv4 server is scheduled to run asap
331 int server_v4_immediately;
334 * The IPv6 server is scheduled to run asap
336 int server_v6_immediately;
341 struct MHD_Daemon *server_v4;
346 struct MHD_Daemon *server_v6;
354 * Use RC4-128 instead of AES:
355 * NONE:+VERS-TLS1.0:+ARCFOUR-128:+SHA1:+RSA:+COMP-NULL
375 * Wrapper to manage addresses
377 struct HttpAddressWrapper
382 struct HttpAddressWrapper *next;
385 * Linked list previous
387 struct HttpAddressWrapper *prev;
389 struct HttpAddress *address;
396 * Message to send using http
401 * next pointer for double linked list
403 struct HTTP_Message *next;
406 * previous pointer for double linked list
408 struct HTTP_Message *prev;
411 * buffer containing data to send
416 * amount of data already sent
426 * HTTP/S specific overhead
431 * Continuation function to call once the transmission buffer
432 * has again space available. NULL if there is no
433 * continuation to call.
435 GNUNET_TRANSPORT_TransmitContinuation transmit_cont;
438 * Closure for transmit_cont.
440 void *transmit_cont_cls;
445 * Start session timeout for session s
446 * @param s the session
449 server_start_session_timeout (struct Session *s);
453 * Increment session timeout due to activity for session s
454 * @param s the session
457 server_reschedule_session_timeout (struct Session *s);
461 * Cancel timeout for session s
462 * @param s the session
465 server_stop_session_timeout (struct Session *s);
469 * Disconnect session @a s
471 * @param cls closure with the `struct HTTP_Server_Plugin`
472 * @param s the session
473 * @return #GNUNET_OK on success
476 http_server_plugin_disconnect_session (void *cls,
481 * Does session s exist?
483 * @param plugin the plugin handle
484 * @param s the session
485 * @return #GNUNET_YES on success, #GNUNET_NO on error
488 server_exist_session (struct HTTP_Server_Plugin *plugin, struct Session *s);
492 * Reschedule the execution of both IPv4 and IPv6 server
493 * @param plugin the plugin
494 * @param server which server to schedule v4 or v6?
495 * @param now #GNUNET_YES to schedule execution immediately, #GNUNET_NO to wait
499 server_reschedule (struct HTTP_Server_Plugin *plugin,
500 struct MHD_Daemon *server,
505 * Function that can be used by the transport service to transmit
506 * a message using the plugin. Note that in the case of a
507 * peer disconnecting, the continuation MUST be called
508 * prior to the disconnect notification itself. This function
509 * will be called with this peer's HELLO message to initiate
510 * a fresh connection to another peer.
513 * @param session which session must be used
514 * @param msgbuf the message to transmit
515 * @param msgbuf_size number of bytes in @a msgbuf
516 * @param priority how important is the message (most plugins will
517 * ignore message priority and just FIFO)
518 * @param to how long to wait at most for the transmission (does not
519 * require plugins to discard the message after the timeout,
520 * just advisory for the desired delay; most plugins will ignore
522 * @param cont continuation to call once the message has
523 * been transmitted (or if the transport is ready
524 * for the next transmission call; or if the
525 * peer disconnected...); can be NULL
526 * @param cont_cls closure for @a cont
527 * @return number of bytes used (on the physical network, with overheads);
528 * -1 on hard errors (i.e. address invalid); 0 is a legal value
529 * and does NOT mean that the message was not transmitted (DV)
532 http_server_plugin_send (void *cls,
533 struct Session *session,
534 const char *msgbuf, size_t msgbuf_size,
535 unsigned int priority,
536 struct GNUNET_TIME_Relative to,
537 GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
539 struct HTTP_Server_Plugin *plugin = cls;
540 struct HTTP_Message *msg;
544 GNUNET_assert (plugin != NULL);
545 GNUNET_assert (session != NULL);
547 if (GNUNET_NO == server_exist_session (plugin, session))
550 return GNUNET_SYSERR;
552 if (NULL == session->server_send)
554 if (GNUNET_NO == session->connect_in_progress)
556 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, session->plugin->name,
557 "Session %p/connection %p: Sending message with %u bytes to peer `%s' with FAILED\n",
558 session, session->server_send,
559 msgbuf_size, GNUNET_i2s (&session->target));
561 return GNUNET_SYSERR;
566 if (GNUNET_YES == session->server_send->disconnect)
567 return GNUNET_SYSERR;
571 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, session->plugin->name,
572 "Session %p/connection %p: Sending message with %u to peer `%s' with \n",
573 session, session->server_send,
574 msgbuf_size, GNUNET_i2s (&session->target));
576 /* create new message and schedule */
577 bytes_sent = sizeof (struct HTTP_Message) + msgbuf_size;
578 msg = GNUNET_malloc (bytes_sent);
580 msg->size = msgbuf_size;
582 msg->buf = (char *) &msg[1];
583 msg->transmit_cont = cont;
584 msg->transmit_cont_cls = cont_cls;
585 memcpy (msg->buf, msgbuf, msgbuf_size);
587 GNUNET_CONTAINER_DLL_insert_tail (session->msg_head, session->msg_tail, msg);
589 GNUNET_asprintf (&stat_txt, "# bytes currently in %s_server buffers", plugin->protocol);
590 GNUNET_STATISTICS_update (plugin->env->stats,
591 stat_txt, msgbuf_size, GNUNET_NO);
592 GNUNET_free (stat_txt);
594 if (NULL != session->server_send)
596 server_reschedule (session->plugin,
597 session->server_send->mhd_daemon,
605 * Function that can be used to force the plugin to disconnect
606 * from the given peer and cancel all previous transmissions
607 * (and their continuationc).
610 * @param target peer from which to disconnect
613 http_server_plugin_disconnect_peer (void *cls,
614 const struct GNUNET_PeerIdentity *target)
616 struct HTTP_Server_Plugin *plugin = cls;
617 struct Session *next;
620 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
621 "Transport tells me to disconnect `%s'\n",
622 GNUNET_i2s (target));
624 while (NULL != (pos = next))
627 if (0 == memcmp (target, &pos->target, sizeof (struct GNUNET_PeerIdentity)))
629 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
630 "Disconnecting session %p to `%s'\n",
631 pos, GNUNET_i2s (target));
632 http_server_plugin_disconnect_session (plugin, pos);
639 * Another peer has suggested an address for this
640 * peer and transport plugin. Check that this could be a valid
641 * address. If so, consider adding it to the list
645 * @param addr pointer to the address
646 * @param addrlen length of @a addr
647 * @return #GNUNET_OK if this is a plausible address for this peer
651 http_server_plugin_address_suggested (void *cls,
655 struct HTTP_Server_Plugin *plugin = cls;
656 struct HttpAddressWrapper *next;
657 struct HttpAddressWrapper *pos;
658 const struct HttpAddress *haddr = addr;
660 if ((NULL != plugin->ext_addr) &&
661 GNUNET_YES == (http_common_cmp_addresses (addr, addrlen,
662 plugin->ext_addr->address,
663 plugin->ext_addr->address_length)))
665 /* Checking HTTP_OPTIONS_VERIFY_CERTIFICATE option for external hostname */
666 if ((ntohl (haddr->options) & HTTP_OPTIONS_VERIFY_CERTIFICATE) !=
667 (plugin->options & HTTP_OPTIONS_VERIFY_CERTIFICATE))
668 return GNUNET_NO; /* VERIFY option not set as required! */
671 next = plugin->addr_head;
672 while (NULL != (pos = next))
675 if (GNUNET_YES == (http_common_cmp_addresses(addr,
686 * Creates a new outbound session the transport
687 * service will use to send data to the peer
689 * Since HTTP/S server cannot create sessions, always return NULL
691 * @param cls the plugin
692 * @param address the address
693 * @return always NULL
695 static struct Session *
696 http_server_plugin_get_session (void *cls,
697 const struct GNUNET_HELLO_Address *address)
704 * Deleting the session
705 * Must not be used afterwards
707 * @param cls closure with the `struct HTTP_ServerPlugin`
708 * @param s the session to delete
709 * @return #GNUNET_OK on success
712 server_delete_session (void *cls,
715 struct HTTP_Server_Plugin *plugin = cls;
716 struct HTTP_Message *msg;
717 struct HTTP_Message *tmp;
719 server_stop_session_timeout(s);
720 GNUNET_CONTAINER_DLL_remove (plugin->head, plugin->tail, s);
725 GNUNET_CONTAINER_DLL_remove (s->msg_head, s->msg_tail, msg);
726 if (NULL != msg->transmit_cont)
727 msg->transmit_cont (msg->transmit_cont_cls, &s->target, GNUNET_SYSERR,
728 msg->size, msg->pos + msg->overhead);
732 if (NULL != s->msg_tk)
734 GNUNET_SERVER_mst_destroy (s->msg_tk);
737 GNUNET_HELLO_address_free (s->address);
738 GNUNET_free_non_null (s->server_recv);
739 GNUNET_free_non_null (s->server_send);
740 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
742 "Session %p destroyed\n", s);
749 * Cancel timeout for session s
751 * @param s the session
754 server_stop_session_timeout (struct Session *s)
756 GNUNET_assert (NULL != s);
758 if (GNUNET_SCHEDULER_NO_TASK != s->timeout_task)
760 GNUNET_SCHEDULER_cancel (s->timeout_task);
761 s->timeout_task = GNUNET_SCHEDULER_NO_TASK;
762 GNUNET_log (TIMEOUT_LOG, "Timeout stopped for session %p\n", s);
768 * Function that queries MHD's select sets and
769 * starts the task waiting for them.
770 * @param plugin plugin
771 * @param daemon_handle the MHD daemon handle
772 * @param now schedule immediately
773 * @return task identifier
775 static GNUNET_SCHEDULER_TaskIdentifier
776 server_schedule (struct HTTP_Server_Plugin *plugin,
777 struct MHD_Daemon *daemon_handle,
782 * Reschedule the execution of both IPv4 and IPv6 server
783 * @param plugin the plugin
784 * @param server which server to schedule v4 or v6?
785 * @param now #GNUNET_YES to schedule execution immediately, #GNUNET_NO to wait
789 server_reschedule (struct HTTP_Server_Plugin *plugin,
790 struct MHD_Daemon *server,
793 if ((server == plugin->server_v4) && (plugin->server_v4 != NULL))
795 if (GNUNET_YES == plugin->server_v4_immediately)
796 return; /* No rescheduling, server will run asap */
798 if (GNUNET_YES == now)
799 plugin->server_v4_immediately = GNUNET_YES;
801 if (plugin->server_v4_task != GNUNET_SCHEDULER_NO_TASK)
803 GNUNET_SCHEDULER_cancel (plugin->server_v4_task);
804 plugin->server_v4_task = GNUNET_SCHEDULER_NO_TASK;
806 plugin->server_v4_task = server_schedule (plugin, plugin->server_v4, now);
809 if ((server == plugin->server_v6) && (plugin->server_v6 != NULL))
811 if (GNUNET_YES == plugin->server_v6_immediately)
812 return; /* No rescheduling, server will run asap */
814 if (GNUNET_YES == now)
815 plugin->server_v6_immediately = GNUNET_YES;
817 if (plugin->server_v6_task != GNUNET_SCHEDULER_NO_TASK)
819 GNUNET_SCHEDULER_cancel (plugin->server_v6_task);
820 plugin->server_v6_task = GNUNET_SCHEDULER_NO_TASK;
822 plugin->server_v6_task = server_schedule (plugin, plugin->server_v6, now);
828 * Disconnect session @a s
830 * @param cls closure with the `struct HTTP_Server_Plugin`
831 * @param s the session
832 * @return #GNUNET_OK on success
835 http_server_plugin_disconnect_session (void *cls,
838 struct HTTP_Server_Plugin *plugin = cls;
839 struct ServerConnection * send;
840 struct ServerConnection * recv;
842 if (GNUNET_NO == server_exist_session (plugin, s))
845 return GNUNET_SYSERR;
848 send = (struct ServerConnection *) s->server_send;
849 if (s->server_send != NULL)
851 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, s->plugin->name,
852 "Server: %p / %p Terminating inbound PUT session to peer `%s'\n",
853 s, s->server_send, GNUNET_i2s (&s->target));
855 send->disconnect = GNUNET_YES;
856 MHD_set_connection_option (send->mhd_conn,
857 MHD_CONNECTION_OPTION_TIMEOUT,
859 server_reschedule (s->plugin, send->mhd_daemon, GNUNET_YES);
862 recv = (struct ServerConnection *) s->server_recv;
865 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, s->plugin->name,
866 "Server: %p / %p Terminating inbound GET session to peer `%s'\n",
867 s, s->server_recv, GNUNET_i2s (&s->target));
869 recv->disconnect = GNUNET_YES;
870 MHD_set_connection_option (recv->mhd_conn,
871 MHD_CONNECTION_OPTION_TIMEOUT,
873 server_reschedule (s->plugin, recv->mhd_daemon, GNUNET_YES);
880 * Function that is called to get the keepalive factor.
881 * GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT is divided by this number to
882 * calculate the interval between keepalive packets.
884 * @param cls closure with the `struct Plugin`
885 * @return keepalive factor
888 http_server_query_keepalive_factor (void *cls)
894 http_server_plugin_update_session_timeout (void *cls,
895 const struct GNUNET_PeerIdentity *peer,
896 struct Session *session)
898 struct HTTP_Server_Plugin *plugin = cls;
900 if (GNUNET_NO == server_exist_session (plugin, session))
903 server_reschedule_session_timeout (session);
908 * Tell MHD that the connection should timeout after @a to seconds.
910 * @param plugin our plugin
911 * @param s session for which the timeout changes
912 * @param to timeout in seconds
915 server_mhd_connection_timeout (struct HTTP_Server_Plugin *plugin,
919 /* Setting timeouts for other connections */
920 if (NULL != s->server_recv)
922 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
923 "Setting timeout for %p to %u sec.\n",
925 MHD_set_connection_option (s->server_recv->mhd_conn,
926 MHD_CONNECTION_OPTION_TIMEOUT,
928 server_reschedule (plugin, s->server_recv->mhd_daemon, GNUNET_NO);
930 if (NULL != s->server_send)
932 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
933 "Setting timeout for %p to %u sec.\n",
935 MHD_set_connection_option (s->server_send->mhd_conn,
936 MHD_CONNECTION_OPTION_TIMEOUT,
938 server_reschedule (plugin, s->server_send->mhd_daemon, GNUNET_NO);
944 * Parse incoming URL for tag and target
946 * @param plugin plugin
947 * @param url incoming url
948 * @param target where to store the target
949 * @param tag where to store the tag
950 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
953 server_parse_url (struct HTTP_Server_Plugin *plugin,
955 struct GNUNET_PeerIdentity *target,
958 char * tag_start = NULL;
959 char * tag_end = NULL;
960 char * target_start = NULL;
961 char * separator = NULL;
962 unsigned int hash_length;
963 unsigned long int ctag;
966 * URL is valid if it is in the form [prefix with (multiple) '/'][peerid[103];tag]*/
971 return GNUNET_SYSERR;
976 separator = strrchr (url, ';');
978 if (NULL == separator)
980 if (DEBUG_URL_PARSE) GNUNET_break (0);
981 return GNUNET_SYSERR;
983 tag_start = separator + 1;
985 if (strlen (tag_start) == 0)
987 /* No tag after separator */
988 if (DEBUG_URL_PARSE) GNUNET_break (0);
989 return GNUNET_SYSERR;
991 ctag = strtoul (tag_start, &tag_end, 10);
994 /* tag == 0 , invalid */
995 if (DEBUG_URL_PARSE) GNUNET_break (0);
996 return GNUNET_SYSERR;
998 if ((ctag == ULONG_MAX) && (ERANGE == errno))
1000 /* out of range: > ULONG_MAX */
1001 if (DEBUG_URL_PARSE) GNUNET_break (0);
1002 return GNUNET_SYSERR;
1004 if (ctag > UINT32_MAX)
1006 /* out of range: > UINT32_MAX */
1007 if (DEBUG_URL_PARSE) GNUNET_break (0);
1008 return GNUNET_SYSERR;
1010 (*tag) = (uint32_t) ctag;
1011 if (NULL == tag_end)
1013 /* no char after tag */
1014 if (DEBUG_URL_PARSE) GNUNET_break (0);
1015 return GNUNET_SYSERR;
1017 if (url[strlen(url)] != tag_end[0])
1019 /* there are more not converted chars after tag */
1020 if (DEBUG_URL_PARSE) GNUNET_break (0);
1021 return GNUNET_SYSERR;
1023 if (DEBUG_URL_PARSE)
1024 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1025 "Found tag `%u' in url\n", (*tag));
1027 /* convert peer id */
1028 target_start = strrchr (url, '/');
1029 if (NULL == target_start)
1031 /* no leading '/' */
1032 target_start = (char *) url;
1035 hash_length = separator - target_start;
1036 if (hash_length != plugin->peer_id_length)
1038 /* no char after tag */
1039 if (DEBUG_URL_PARSE) GNUNET_break (0);
1040 return GNUNET_SYSERR;
1043 GNUNET_CRYPTO_eddsa_public_key_from_string (target_start,
1045 &target->public_key))
1047 /* hash conversion failed */
1048 if (DEBUG_URL_PARSE) GNUNET_break (0);
1049 return GNUNET_SYSERR;
1051 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1053 "Found target `%s' in URL\n",
1054 GNUNET_i2s_full (target));
1060 * Lookup a mhd connection and create one if none is found
1062 * @param plugin the plugin handle
1063 * @param mhd_connection the incoming mhd_connection
1064 * @param url incoming requested URL
1065 * @param method PUT or GET
1066 * @return the server connecetion
1068 static struct ServerConnection *
1069 server_lookup_connection (struct HTTP_Server_Plugin *plugin,
1070 struct MHD_Connection *mhd_connection, const char *url,
1073 struct Session *s = NULL;
1074 struct ServerConnection *sc = NULL;
1075 const union MHD_ConnectionInfo *conn_info;
1076 struct HttpAddress *addr;
1078 struct GNUNET_ATS_Information ats;
1079 struct GNUNET_PeerIdentity target;
1082 int direction = GNUNET_SYSERR;
1085 conn_info = MHD_get_connection_info (mhd_connection,
1086 MHD_CONNECTION_INFO_CLIENT_ADDRESS);
1087 if ((conn_info->client_addr->sa_family != AF_INET) &&
1088 (conn_info->client_addr->sa_family != AF_INET6))
1090 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1091 "New %s connection from %s\n", method, url);
1093 if (GNUNET_SYSERR == server_parse_url (plugin, url, &target, &tag))
1095 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1096 "Invalid url %s\n", url);
1099 if (0 == strcmp (MHD_HTTP_METHOD_PUT, method))
1100 direction = _RECEIVE;
1101 else if (0 == strcmp (MHD_HTTP_METHOD_GET, method))
1105 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1106 "Invalid method %s connection from %s\n", method, url);
1110 plugin->cur_connections++;
1111 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1112 "New %s connection from %s with tag %u (%u of %u)\n",
1114 GNUNET_i2s (&target), tag,
1115 plugin->cur_connections, plugin->max_connections);
1116 /* find duplicate session */
1120 if ((0 == memcmp (&s->target, &target, sizeof (struct GNUNET_PeerIdentity))) &&
1127 if ((_RECEIVE == direction) && (NULL != s->server_recv))
1129 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1130 "Duplicate PUT connection from `%s' tag %u, dismissing new connection\n",
1131 GNUNET_i2s (&target),
1136 if ((_SEND == direction) && (NULL != s->server_send))
1138 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1139 "Duplicate GET connection from `%s' tag %u, dismissing new connection\n",
1140 GNUNET_i2s (&target),
1147 /* create new session */
1149 switch (conn_info->client_addr->sa_family)
1152 addr = http_common_address_from_socket (plugin->protocol, conn_info->client_addr, sizeof (struct sockaddr_in));
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_in));
1157 addr = http_common_address_from_socket (plugin->protocol, conn_info->client_addr, sizeof (struct sockaddr_in6));
1158 addr_len = http_common_address_get_size (addr);
1159 ats = plugin->env->get_address_type (plugin->env->cls, conn_info->client_addr, sizeof (struct sockaddr_in6));
1162 /* external host name */
1163 ats.type = htonl (GNUNET_ATS_NETWORK_TYPE);
1164 ats.type = htonl (GNUNET_ATS_NET_WAN);
1168 s = GNUNET_new (struct Session);
1169 memcpy (&s->target, &target, sizeof (struct GNUNET_PeerIdentity));
1171 s->address = GNUNET_HELLO_address_allocate (&s->target, PLUGIN_NAME,
1172 addr, addr_len, GNUNET_HELLO_ADDRESS_INFO_INBOUND);
1173 s->ats_address_network_type = ats.value;
1174 s->next_receive = GNUNET_TIME_UNIT_ZERO_ABS;
1176 s->server_recv = NULL;
1177 s->server_send = NULL;
1178 s->session_passed = GNUNET_NO;
1179 s->session_ended = GNUNET_NO;
1180 s->connect_in_progress = GNUNET_YES;
1181 server_start_session_timeout(s);
1182 GNUNET_CONTAINER_DLL_insert (plugin->head, plugin->tail, s);
1184 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1185 "Creating new session %p for peer `%s' connecting from `%s'\n",
1186 s, GNUNET_i2s (&target),
1187 http_common_plugin_address_to_string (NULL,
1190 GNUNET_free_non_null (addr);
1192 sc = GNUNET_new (struct ServerConnection);
1193 if (conn_info->client_addr->sa_family == AF_INET)
1194 sc->mhd_daemon = plugin->server_v4;
1195 if (conn_info->client_addr->sa_family == AF_INET6)
1196 sc->mhd_daemon = plugin->server_v6;
1197 sc->mhd_conn = mhd_connection;
1198 sc->direction = direction;
1199 sc->connected = GNUNET_NO;
1201 if (direction == _SEND)
1202 s->server_send = sc;
1203 if (direction == _RECEIVE)
1204 s->server_recv = sc;
1206 if ((NULL != s->server_send) && (NULL != s->server_recv))
1208 s->connect_in_progress = GNUNET_NO; /* PUT and GET are connected */
1209 plugin->env->session_start (NULL, s->address ,s, NULL, 0);
1212 if ((NULL == s->server_recv) || (NULL == s->server_send))
1214 to = (HTTP_SERVER_NOT_VALIDATED_TIMEOUT.rel_value_us / 1000LL / 1000LL);
1215 MHD_set_connection_option (mhd_connection,
1216 MHD_CONNECTION_OPTION_TIMEOUT, to);
1217 server_reschedule (plugin, sc->mhd_daemon, GNUNET_NO);
1221 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1222 "Session %p for peer `%s' fully connected\n",
1223 s, GNUNET_i2s (&target));
1224 to = (HTTP_SERVER_SESSION_TIMEOUT.rel_value_us / 1000LL / 1000LL);
1225 server_mhd_connection_timeout (plugin, s, to);
1228 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1229 "Setting timeout for %p to %u sec.\n", sc, to);
1235 * Lookup a session for a server connection
1237 * @param plugin the plugin
1238 * @param sc the server connection
1239 * @return the session found or NULL
1241 static struct Session *
1242 server_lookup_session (struct HTTP_Server_Plugin *plugin,
1243 struct ServerConnection * sc)
1247 for (s = plugin->head; NULL != s; s = s->next)
1248 if ((s->server_recv == sc) || (s->server_send == sc))
1255 server_exist_session (struct HTTP_Server_Plugin *plugin,
1258 struct Session * head;
1260 for (head = plugin->head; head != NULL; head = head->next)
1268 * Callback called by MHD when it needs data to send
1270 * @param cls current session
1271 * @param pos position in buffer
1272 * @param buf the buffer to write data to
1273 * @param max max number of bytes available in buffer
1274 * @return bytes written to buffer
1277 server_send_callback (void *cls, uint64_t pos, char *buf, size_t max)
1279 struct Session *s = cls;
1280 ssize_t bytes_read = 0;
1281 struct HTTP_Message *msg;
1284 if (GNUNET_NO == server_exist_session (s->plugin, s))
1290 bytes_read = GNUNET_MIN (msg->size - msg->pos,
1292 memcpy (buf, &msg->buf[msg->pos], bytes_read);
1293 msg->pos += bytes_read;
1295 /* removing message */
1296 if (msg->pos == msg->size)
1298 GNUNET_CONTAINER_DLL_remove (s->msg_head, s->msg_tail, msg);
1299 if (NULL != msg->transmit_cont)
1300 msg->transmit_cont (msg->transmit_cont_cls, &s->target, GNUNET_OK,
1301 msg->size, msg->size + msg->overhead);
1307 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, s->plugin->name,
1308 "Sent %u bytes to peer `%s' with session %p \n", bytes_read, GNUNET_i2s (&s->target), s);
1309 GNUNET_asprintf (&stat_txt, "# bytes currently in %s_server buffers",
1310 s->plugin->protocol);
1311 GNUNET_STATISTICS_update (s->plugin->env->stats,
1312 stat_txt, -bytes_read, GNUNET_NO);
1313 GNUNET_free (stat_txt);
1314 GNUNET_asprintf (&stat_txt, "# bytes transmitted via %s_server",
1315 s->plugin->protocol);
1316 GNUNET_STATISTICS_update (s->plugin->env->stats,
1317 stat_txt, bytes_read, GNUNET_NO);
1318 GNUNET_free (stat_txt);
1325 * Callback called by MessageStreamTokenizer when a message has arrived
1327 * @param cls current session as closure
1328 * @param client client
1329 * @param message the message to be forwarded to transport service
1330 * @return #GNUNET_OK
1333 server_receive_mst_cb (void *cls, void *client,
1334 const struct GNUNET_MessageHeader *message)
1336 struct Session *s = cls;
1337 struct HTTP_Server_Plugin *plugin = s->plugin;
1338 struct GNUNET_ATS_Information atsi;
1339 struct GNUNET_TIME_Relative delay;
1342 if (GNUNET_NO == server_exist_session (s->plugin, s))
1346 atsi.type = htonl (GNUNET_ATS_NETWORK_TYPE);
1347 atsi.value = s->ats_address_network_type;
1348 GNUNET_break (s->ats_address_network_type != ntohl (GNUNET_ATS_NET_UNSPECIFIED));
1350 delay = plugin->env->receive (plugin->env->cls, s->address, s, message);
1351 plugin->env->update_address_metrics (plugin->env->cls, s->address, s, &atsi, 1);
1353 GNUNET_asprintf (&stat_txt, "# bytes received via %s_server", plugin->protocol);
1354 GNUNET_STATISTICS_update (plugin->env->stats,
1355 stat_txt, ntohs (message->size), GNUNET_NO);
1356 GNUNET_free (stat_txt);
1358 s->session_passed = GNUNET_YES;
1359 s->next_receive = GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (), delay);
1360 if (delay.rel_value_us > 0)
1362 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1363 "Peer `%s' address `%s' next read delayed for %s\n",
1364 GNUNET_i2s (&s->target),
1365 http_common_plugin_address_to_string (NULL,
1366 plugin->protocol, s->address->address,
1367 s->address->address_length),
1368 GNUNET_STRINGS_relative_time_to_string (delay,
1371 server_reschedule_session_timeout (s);
1376 * Add headers to a request indicating that we allow Cross-Origin Resource
1380 add_cors_headers(struct MHD_Response *response)
1382 MHD_add_response_header (response,
1383 "Access-Control-Allow-Origin",
1385 MHD_add_response_header (response,
1386 "Access-Control-Allow-Methods",
1387 "GET, PUT, OPTIONS");
1388 MHD_add_response_header (response,
1389 "Access-Control-Max-Age",
1394 * MHD callback for a new incoming connection
1396 * @param cls the plugin handle
1397 * @param mhd_connection the mhd connection
1398 * @param url the requested URL
1399 * @param method GET or PUT
1400 * @param version HTTP version
1401 * @param upload_data upload data
1402 * @param upload_data_size sizeof upload data
1403 * @param httpSessionCache the session cache to remember the connection
1404 * @return MHD_YES if connection is accepted, MHD_NO on reject
1407 server_access_cb (void *cls, struct MHD_Connection *mhd_connection,
1408 const char *url, const char *method, const char *version,
1409 const char *upload_data, size_t * upload_data_size,
1410 void **httpSessionCache)
1412 struct HTTP_Server_Plugin *plugin = cls;
1415 struct ServerConnection *sc = *httpSessionCache;
1417 struct MHD_Response *response;
1419 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1420 _("Access from connection %p (%u of %u) for `%s' `%s' url `%s' with upload data size %u\n"),
1422 plugin->cur_connections, plugin->max_connections,
1423 method, version, url, (*upload_data_size));
1425 GNUNET_assert (cls != NULL);
1428 /* CORS pre-flight request */
1429 if (0 == strcmp (MHD_HTTP_METHOD_OPTIONS, method))
1431 response = MHD_create_response_from_buffer (0, NULL,
1432 MHD_RESPMEM_PERSISTENT);
1433 add_cors_headers(response);
1434 res = MHD_queue_response (mhd_connection, MHD_HTTP_OK, response);
1435 MHD_destroy_response (response);
1438 /* new connection */
1439 sc = server_lookup_connection (plugin, mhd_connection, url, method);
1442 (*httpSessionCache) = sc;
1446 response = MHD_create_response_from_data (strlen (HTTP_ERROR_RESPONSE), HTTP_ERROR_RESPONSE, MHD_NO, MHD_NO);
1447 MHD_add_response_header (response,
1448 MHD_HTTP_HEADER_CONTENT_TYPE,
1450 add_cors_headers(response);
1451 res = MHD_queue_response (mhd_connection, MHD_HTTP_NOT_FOUND, response);
1452 MHD_destroy_response (response);
1458 /* 'old' connection */
1459 if (NULL == server_lookup_session (plugin, sc))
1461 /* Session was already disconnected */
1466 /* existing connection */
1467 sc = (*httpSessionCache);
1469 GNUNET_assert (NULL != s);
1470 /* connection is to be disconnected */
1471 if (sc->disconnect == GNUNET_YES)
1473 /* Sent HTTP/1.1: 200 OK as response */
1474 response = MHD_create_response_from_data (strlen ("Thank you!"),
1477 add_cors_headers(response);
1478 MHD_queue_response (mhd_connection, MHD_HTTP_OK, response);
1479 MHD_destroy_response (response);
1482 GNUNET_assert (s != NULL);
1484 if (sc->direction == _SEND)
1486 response = MHD_create_response_from_callback (MHD_SIZE_UNKNOWN,
1488 &server_send_callback, s,
1490 add_cors_headers(response);
1491 MHD_queue_response (mhd_connection, MHD_HTTP_OK, response);
1492 MHD_destroy_response (response);
1495 if (sc->direction == _RECEIVE)
1497 if ((*upload_data_size == 0) && (sc->connected == GNUNET_NO))
1499 /* (*upload_data_size == 0) first callback when header are passed */
1500 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1501 "Session %p / Connection %p: Peer `%s' PUT on address `%s' connected\n",
1503 GNUNET_i2s (&s->target),
1504 http_common_plugin_address_to_string (NULL,
1505 plugin->protocol, s->address->address,
1506 s->address->address_length));
1507 sc->connected = GNUNET_YES;
1510 else if ((*upload_data_size == 0) && (sc->connected == GNUNET_YES))
1512 /* (*upload_data_size == 0) when upload is complete */
1513 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1514 "Session %p / Connection %p: Peer `%s' PUT on address `%s' finished upload\n",
1516 GNUNET_i2s (&s->target),
1517 http_common_plugin_address_to_string (NULL,
1518 plugin->protocol, s->address->address,
1519 s->address->address_length));
1520 sc->connected = GNUNET_NO;
1521 /* Sent HTTP/1.1: 200 OK as PUT Response\ */
1522 response = MHD_create_response_from_data (strlen ("Thank you!"),
1525 add_cors_headers(response);
1526 MHD_queue_response (mhd_connection, MHD_HTTP_OK, response);
1527 MHD_destroy_response (response);
1530 else if ((*upload_data_size > 0) && (sc->connected == GNUNET_YES))
1532 /* (*upload_data_size > 0) for every segment received */
1533 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1534 "Session %p / Connection %p: Peer `%s' PUT on address `%s' received %u bytes\n",
1536 GNUNET_i2s (&s->target),
1537 http_common_plugin_address_to_string (NULL,
1538 plugin->protocol, s->address->address,
1539 s->address->address_length),
1541 struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get ();
1543 if ((s->next_receive.abs_value_us <= now.abs_value_us))
1545 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1546 "PUT with %u bytes forwarded to MST\n",
1548 if (s->msg_tk == NULL)
1550 s->msg_tk = GNUNET_SERVER_mst_create (&server_receive_mst_cb, s);
1552 GNUNET_SERVER_mst_receive (s->msg_tk, s, upload_data,
1553 *upload_data_size, GNUNET_NO, GNUNET_NO);
1554 server_mhd_connection_timeout (plugin, s,
1555 GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT.rel_value_us / 1000LL / 1000LL);
1556 (*upload_data_size) = 0;
1560 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1561 "Session %p / Connection %p: no inbound bandwidth available! Next read was delayed by %s\n",
1563 GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_duration (s->next_receive),
1579 * Callback from MHD when a connection disconnects
1581 * @param cls closure with the `struct HTTP_Server_Plugin *`
1582 * @param connection the disconnected MHD connection
1583 * @param httpSessionCache the pointer to distinguish
1586 server_disconnect_cb (void *cls, struct MHD_Connection *connection,
1587 void **httpSessionCache)
1589 struct HTTP_Server_Plugin *plugin = cls;
1590 struct ServerConnection *sc = *httpSessionCache;
1591 struct Session *s = NULL;
1592 struct Session *t = NULL;
1594 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1596 "Disconnect for connection %p \n", sc);
1601 if (NULL == (s = server_lookup_session (plugin, sc)))
1603 for (t = plugin->head; t != NULL; t = t->next)
1609 if (sc->direction == _SEND)
1611 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1612 "Peer `%s' connection %p, GET on address `%s' disconnected\n",
1613 GNUNET_i2s (&s->target), s->server_send,
1614 http_common_plugin_address_to_string (NULL,
1615 plugin->protocol, s->address->address,
1616 s->address->address_length));
1617 s->server_send = NULL;
1618 if (NULL != (s->server_recv))
1620 s->server_recv->disconnect = GNUNET_YES;
1621 GNUNET_assert (NULL != s->server_recv->mhd_conn);
1622 #if MHD_VERSION >= 0x00090E00
1623 MHD_set_connection_option (s->server_recv->mhd_conn, MHD_CONNECTION_OPTION_TIMEOUT,
1626 server_reschedule (plugin, s->server_recv->mhd_daemon, GNUNET_NO);
1629 if (sc->direction == _RECEIVE)
1631 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1632 "Peer `%s' connection %p PUT on address `%s' disconnected\n",
1633 GNUNET_i2s (&s->target), s->server_recv,
1634 http_common_plugin_address_to_string (NULL,
1635 plugin->protocol, s->address->address,
1636 s->address->address_length));
1637 s->server_recv = NULL;
1638 /* Do not terminate session when PUT disconnects
1639 if (NULL != (s->server_send))
1641 s->server_send->disconnect = GNUNET_YES;
1642 GNUNET_assert (NULL != s->server_send->mhd_conn);
1643 #if MHD_VERSION >= 0x00090E00
1644 MHD_set_connection_option (s->server_send->mhd_conn, MHD_CONNECTION_OPTION_TIMEOUT,
1647 server_reschedule (plugin, s->server_send->mhd_daemon, GNUNET_NO);
1649 if (s->msg_tk != NULL)
1651 GNUNET_SERVER_mst_destroy (s->msg_tk);
1657 plugin->cur_connections--;
1659 if ((s->server_send == NULL) && (s->server_recv == NULL))
1661 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1662 "Peer `%s' on address `%s' disconnected\n",
1663 GNUNET_i2s (&s->target),
1664 http_common_plugin_address_to_string (NULL,
1665 plugin->protocol, s->address->address,
1666 s->address->address_length));
1668 if ((GNUNET_YES == s->session_passed) && (GNUNET_NO == s->session_ended))
1670 /* Notify transport immediately that this session is invalid */
1671 s->session_ended = GNUNET_YES;
1672 plugin->env->session_end (plugin->env->cls, s->address, s);
1674 server_delete_session (plugin, s);
1680 * Check if incoming connection is accepted.
1682 * @param cls plugin as closure
1683 * @param addr address of incoming connection
1684 * @param addr_len address length of incoming connection
1685 * @return MHD_YES if connection is accepted, MHD_NO if connection is rejected
1688 server_accept_cb (void *cls, const struct sockaddr *addr, socklen_t addr_len)
1690 struct HTTP_Server_Plugin *plugin = cls;
1692 if (plugin->cur_connections <= plugin->max_connections)
1694 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1695 _("Accepting connection (%u of %u) from `%s'\n"),
1696 plugin->cur_connections, plugin->max_connections,
1697 GNUNET_a2s (addr, addr_len));
1702 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, plugin->name,
1703 _("Server reached maximum number connections (%u), rejecting new connection\n"),
1704 plugin->max_connections);
1711 server_log (void *arg, const char *fmt, va_list ap)
1715 vsnprintf (text, sizeof (text), fmt, ap);
1717 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Server: %s\n", text);
1722 * Call MHD IPv4 to process pending requests and then go back
1723 * and schedule the next run.
1724 * @param cls plugin as closure
1725 * @param tc task context
1728 server_v4_run (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1730 struct HTTP_Server_Plugin *plugin = cls;
1732 GNUNET_assert (cls != NULL);
1734 plugin->server_v4_task = GNUNET_SCHEDULER_NO_TASK;
1735 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1738 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1739 "Running IPv4 server\n");
1741 plugin->server_v4_immediately = GNUNET_NO;
1742 GNUNET_assert (MHD_YES == MHD_run (plugin->server_v4));
1743 server_reschedule (plugin, plugin->server_v4, GNUNET_NO);
1748 * Call MHD IPv6 to process pending requests and then go back
1749 * and schedule the next run.
1750 * @param cls plugin as closure
1751 * @param tc task context
1754 server_v6_run (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1756 struct HTTP_Server_Plugin *plugin = cls;
1758 GNUNET_assert (cls != NULL);
1759 plugin->server_v6_task = GNUNET_SCHEDULER_NO_TASK;
1760 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1763 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1764 "Running IPv6 server\n");
1766 plugin->server_v6_immediately = GNUNET_NO;
1767 GNUNET_assert (MHD_YES == MHD_run (plugin->server_v6));
1768 server_reschedule (plugin, plugin->server_v6, GNUNET_NO);
1773 * Function that queries MHD's select sets and
1774 * starts the task waiting for them.
1776 * @param plugin plugin
1777 * @param daemon_handle the MHD daemon handle
1778 * @return gnunet task identifier
1780 static GNUNET_SCHEDULER_TaskIdentifier
1781 server_schedule (struct HTTP_Server_Plugin *plugin,
1782 struct MHD_Daemon *daemon_handle,
1785 GNUNET_SCHEDULER_TaskIdentifier ret;
1789 struct GNUNET_NETWORK_FDSet *wrs;
1790 struct GNUNET_NETWORK_FDSet *wws;
1791 struct GNUNET_NETWORK_FDSet *wes;
1793 MHD_UNSIGNED_LONG_LONG timeout;
1794 static unsigned long long last_timeout = 0;
1797 struct GNUNET_TIME_Relative tv;
1799 if (GNUNET_YES == plugin->in_shutdown)
1800 return GNUNET_SCHEDULER_NO_TASK;
1802 ret = GNUNET_SCHEDULER_NO_TASK;
1806 wrs = GNUNET_NETWORK_fdset_create ();
1807 wes = GNUNET_NETWORK_fdset_create ();
1808 wws = GNUNET_NETWORK_fdset_create ();
1810 GNUNET_assert (MHD_YES == MHD_get_fdset (daemon_handle, &rs, &ws, &es, &max));
1811 haveto = MHD_get_timeout (daemon_handle, &timeout);
1812 if (haveto == MHD_YES)
1814 if (timeout != last_timeout)
1817 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1818 "SELECT Timeout changed from %llu to %llu (ms)\n",
1819 last_timeout, timeout);
1820 last_timeout = timeout;
1822 if (timeout <= GNUNET_TIME_UNIT_SECONDS.rel_value_us / 1000LL)
1823 tv.rel_value_us = (uint64_t) timeout * 1000LL;
1825 tv = GNUNET_TIME_UNIT_SECONDS;
1828 tv = GNUNET_TIME_UNIT_SECONDS;
1829 /* Force immediate run, since we have outbound data to send */
1830 if (now == GNUNET_YES)
1831 tv = GNUNET_TIME_UNIT_MILLISECONDS;
1832 GNUNET_NETWORK_fdset_copy_native (wrs, &rs, max + 1);
1833 GNUNET_NETWORK_fdset_copy_native (wws, &ws, max + 1);
1834 GNUNET_NETWORK_fdset_copy_native (wes, &es, max + 1);
1836 if (daemon_handle == plugin->server_v4)
1838 if (plugin->server_v4_task != GNUNET_SCHEDULER_NO_TASK)
1840 GNUNET_SCHEDULER_cancel (plugin->server_v4_task);
1841 plugin->server_v4_task = GNUNET_SCHEDULER_NO_TASK;
1844 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1845 "Scheduling IPv4 server task in %llu ms\n", tv);
1848 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1850 &server_v4_run, plugin);
1852 if (daemon_handle == plugin->server_v6)
1854 if (plugin->server_v6_task != GNUNET_SCHEDULER_NO_TASK)
1856 GNUNET_SCHEDULER_cancel (plugin->server_v6_task);
1857 plugin->server_v6_task = GNUNET_SCHEDULER_NO_TASK;
1860 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1861 "Scheduling IPv6 server task in %llu ms\n", tv);
1864 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1866 &server_v6_run, plugin);
1868 GNUNET_NETWORK_fdset_destroy (wrs);
1869 GNUNET_NETWORK_fdset_destroy (wws);
1870 GNUNET_NETWORK_fdset_destroy (wes);
1877 * Load ssl certificate from file
1879 * @param file filename
1880 * @return content of the file
1883 server_load_file (const char *file)
1885 struct GNUNET_DISK_FileHandle *gn_file;
1889 if (GNUNET_OK != GNUNET_DISK_file_size (file,
1890 &fsize, GNUNET_NO, GNUNET_YES))
1892 text = GNUNET_malloc (fsize + 1);
1894 GNUNET_DISK_file_open (file, GNUNET_DISK_OPEN_READ,
1895 GNUNET_DISK_PERM_USER_READ);
1896 if (gn_file == NULL)
1901 if (GNUNET_SYSERR == GNUNET_DISK_file_read (gn_file, text, fsize))
1904 GNUNET_DISK_file_close (gn_file);
1908 GNUNET_DISK_file_close (gn_file);
1916 * Load ssl certificate
1918 * @param plugin the plugin
1919 * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
1922 server_load_certificate (struct HTTP_Server_Plugin *plugin)
1924 int res = GNUNET_OK;
1930 GNUNET_CONFIGURATION_get_value_filename (plugin->env->cfg, plugin->name,
1931 "KEY_FILE", &key_file))
1933 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
1934 plugin->name, "CERT_FILE");
1935 return GNUNET_SYSERR;
1938 GNUNET_CONFIGURATION_get_value_filename (plugin->env->cfg, plugin->name,
1939 "CERT_FILE", &cert_file))
1941 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
1942 plugin->name, "CERT_FILE");
1943 GNUNET_free (key_file);
1944 return GNUNET_SYSERR;
1946 /* Get crypto init string from config. If not present, use
1949 GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg,
1952 &plugin->crypto_init))
1953 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1954 "Using crypto init string `%s'\n",
1955 plugin->crypto_init);
1957 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1958 "Using default crypto init string \n");
1960 /* read key & certificates from file */
1961 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1962 "Trying to loading TLS certificate from key-file `%s' cert-file`%s'\n",
1963 key_file, cert_file);
1965 plugin->key = server_load_file (key_file);
1966 plugin->cert = server_load_file (cert_file);
1968 if ((plugin->key == NULL) || (plugin->cert == NULL))
1970 struct GNUNET_OS_Process *cert_creation;
1972 GNUNET_free_non_null (plugin->key);
1974 GNUNET_free_non_null (plugin->cert);
1975 plugin->cert = NULL;
1977 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1978 "No usable TLS certificate found, creating certificate\n");
1981 GNUNET_OS_start_process (GNUNET_NO, GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
1983 "gnunet-transport-certificate-creation",
1984 "gnunet-transport-certificate-creation",
1985 key_file, cert_file, NULL);
1986 if (cert_creation == NULL)
1988 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
1990 ("Could not create a new TLS certificate, program `gnunet-transport-certificate-creation' could not be started!\n"));
1991 GNUNET_free (key_file);
1992 GNUNET_free (cert_file);
1994 GNUNET_free_non_null (plugin->key);
1996 GNUNET_free_non_null (plugin->cert);
1997 plugin->cert = NULL;
1998 GNUNET_free_non_null (plugin->crypto_init);
1999 plugin->crypto_init = NULL;
2001 return GNUNET_SYSERR;
2003 GNUNET_assert (GNUNET_OK == GNUNET_OS_process_wait (cert_creation));
2004 GNUNET_OS_process_destroy (cert_creation);
2006 plugin->key = server_load_file (key_file);
2007 plugin->cert = server_load_file (cert_file);
2010 if ((plugin->key == NULL) || (plugin->cert == NULL))
2012 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
2014 _("No usable TLS certificate found and creating one at `%s/%s' failed!\n"),
2015 key_file, cert_file);
2016 GNUNET_free (key_file);
2017 GNUNET_free (cert_file);
2019 GNUNET_free_non_null (plugin->key);
2021 GNUNET_free_non_null (plugin->cert);
2022 plugin->cert = NULL;
2023 GNUNET_free_non_null (plugin->crypto_init);
2024 plugin->crypto_init = NULL;
2026 return GNUNET_SYSERR;
2028 GNUNET_free (key_file);
2029 GNUNET_free (cert_file);
2030 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "TLS certificate loaded\n");
2037 * Start the HTTP server
2039 * @param plugin the plugin handle
2040 * @return GNUNET_OK on success, GNUNET_SYSERR on failure
2043 server_start (struct HTTP_Server_Plugin *plugin)
2045 unsigned int timeout;
2047 GNUNET_assert (NULL != plugin);
2050 if (GNUNET_SYSERR == server_load_certificate (plugin))
2052 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
2053 "Could not load or create server certificate! Loading plugin failed!\n");
2054 return GNUNET_SYSERR;
2059 #if MHD_VERSION >= 0x00090E00
2060 timeout = HTTP_SERVER_NOT_VALIDATED_TIMEOUT.rel_value_us / 1000LL / 1000LL;
2061 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2062 "MHD can set timeout per connection! Default time out %u sec.\n",
2065 timeout = HTTP_SERVER_SESSION_TIMEOUT.rel_value_us / 1000LL / 1000LL;
2066 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, plugin->name,
2067 "MHD cannot set timeout per connection! Default time out %u sec.\n",
2071 plugin->server_v4 = NULL;
2072 if (plugin->use_ipv4 == GNUNET_YES)
2074 plugin->server_v4 = MHD_start_daemon (
2081 MHD_NO_FLAG, plugin->port,
2082 &server_accept_cb, plugin,
2083 &server_access_cb, plugin,
2084 MHD_OPTION_SOCK_ADDR,
2085 (struct sockaddr_in *)
2086 plugin->server_addr_v4,
2087 MHD_OPTION_CONNECTION_LIMIT,
2089 plugin->max_connections,
2091 MHD_OPTION_HTTPS_PRIORITIES,
2092 plugin->crypto_init,
2093 MHD_OPTION_HTTPS_MEM_KEY,
2095 MHD_OPTION_HTTPS_MEM_CERT,
2098 MHD_OPTION_CONNECTION_TIMEOUT,
2100 MHD_OPTION_CONNECTION_MEMORY_LIMIT,
2102 GNUNET_SERVER_MAX_MESSAGE_SIZE),
2103 MHD_OPTION_NOTIFY_COMPLETED,
2104 &server_disconnect_cb, plugin,
2105 MHD_OPTION_EXTERNAL_LOGGER,
2106 server_log, NULL, MHD_OPTION_END);
2107 if (plugin->server_v4 == NULL)
2109 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
2110 "Failed to start %s IPv4 server component on port %u\n",
2111 plugin->name, plugin->port);
2114 server_reschedule (plugin, plugin->server_v4, GNUNET_NO);
2118 plugin->server_v6 = NULL;
2119 if (plugin->use_ipv6 == GNUNET_YES)
2121 plugin->server_v6 = MHD_start_daemon (
2128 MHD_USE_IPv6, plugin->port,
2129 &server_accept_cb, plugin,
2130 &server_access_cb, plugin,
2131 MHD_OPTION_SOCK_ADDR,
2132 (struct sockaddr_in6 *)
2133 plugin->server_addr_v6,
2134 MHD_OPTION_CONNECTION_LIMIT,
2136 plugin->max_connections,
2138 MHD_OPTION_HTTPS_PRIORITIES,
2139 plugin->crypto_init,
2140 MHD_OPTION_HTTPS_MEM_KEY,
2142 MHD_OPTION_HTTPS_MEM_CERT,
2145 MHD_OPTION_CONNECTION_TIMEOUT,
2147 MHD_OPTION_CONNECTION_MEMORY_LIMIT,
2149 GNUNET_SERVER_MAX_MESSAGE_SIZE),
2150 MHD_OPTION_NOTIFY_COMPLETED,
2151 &server_disconnect_cb, plugin,
2152 MHD_OPTION_EXTERNAL_LOGGER,
2153 server_log, NULL, MHD_OPTION_END);
2154 if (plugin->server_v6 == NULL)
2156 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
2157 "Failed to start %s IPv6 server component on port %u\n",
2158 plugin->name, plugin->port);
2161 server_reschedule (plugin, plugin->server_v6, GNUNET_NO);
2165 if ((plugin->server_v6 == NULL) && (plugin->server_v4 == NULL))
2167 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
2168 "%s %s server component started on port %u\n",
2169 msg, plugin->name, plugin->port);
2171 return GNUNET_SYSERR;
2173 else if ((plugin->server_v6 != NULL) && (plugin->server_v4 != NULL))
2174 msg = "IPv4 and IPv6";
2175 else if (plugin->server_v6 != NULL)
2177 else if (plugin->server_v4 != NULL)
2179 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2180 "%s %s server component started on port %u\n",
2181 msg, plugin->name, plugin->port);
2187 server_stop (struct HTTP_Server_Plugin *plugin)
2189 if (plugin->server_v4 != NULL)
2191 MHD_stop_daemon (plugin->server_v4);
2192 plugin->server_v4 = NULL;
2194 if ( plugin->server_v6 != NULL)
2196 MHD_stop_daemon (plugin->server_v6);
2197 plugin->server_v6 = NULL;
2201 if (plugin->server_v4_task != GNUNET_SCHEDULER_NO_TASK)
2203 GNUNET_SCHEDULER_cancel (plugin->server_v4_task);
2204 plugin->server_v4_task = GNUNET_SCHEDULER_NO_TASK;
2207 if (plugin->server_v6_task != GNUNET_SCHEDULER_NO_TASK)
2209 GNUNET_SCHEDULER_cancel (plugin->server_v6_task);
2210 plugin->server_v6_task = GNUNET_SCHEDULER_NO_TASK;
2213 GNUNET_free_non_null (plugin->crypto_init);
2214 GNUNET_free_non_null (plugin->cert);
2215 GNUNET_free_non_null (plugin->key);
2218 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2219 "%s server component stopped\n", plugin->name);
2224 * Add an address to the server's set of addresses and notify transport
2226 * @param cls the plugin handle
2227 * @param add_remove GNUNET_YES on add, GNUNET_NO on remove
2228 * @param addr the address
2229 * @param addrlen address length
2232 server_add_address (void *cls, int add_remove, const struct sockaddr *addr,
2235 struct HTTP_Server_Plugin *plugin = cls;
2236 struct GNUNET_HELLO_Address *address;
2237 struct HttpAddressWrapper *w = NULL;
2239 w = GNUNET_new (struct HttpAddressWrapper);
2240 w->address = http_common_address_from_socket (plugin->protocol, addr, addrlen);
2241 if (NULL == w->address)
2246 w->addrlen = http_common_address_get_size (w->address);
2248 GNUNET_CONTAINER_DLL_insert(plugin->addr_head, plugin->addr_tail, w);
2249 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2250 "Notifying transport to add address `%s'\n",
2251 http_common_plugin_address_to_string (NULL,
2253 w->address, w->addrlen));
2254 /* modify our published address list */
2256 address = GNUNET_HELLO_address_allocate (plugin->env->my_identity,
2257 "https_client", w->address, w->addrlen, GNUNET_HELLO_ADDRESS_INFO_NONE);
2259 address = GNUNET_HELLO_address_allocate (plugin->env->my_identity,
2260 "http_client", w->address, w->addrlen, GNUNET_HELLO_ADDRESS_INFO_NONE);
2263 plugin->env->notify_address (plugin->env->cls, add_remove, address);
2264 GNUNET_HELLO_address_free (address);
2269 * Remove an address from the server's set of addresses and notify transport
2271 * @param cls the plugin handle
2272 * @param add_remove GNUNET_YES on add, GNUNET_NO on remove
2273 * @param addr the address
2274 * @param addrlen address length
2277 server_remove_address (void *cls, int add_remove, const struct sockaddr *addr,
2280 struct HTTP_Server_Plugin *plugin = cls;
2281 struct GNUNET_HELLO_Address *address;
2282 struct HttpAddressWrapper *w = plugin->addr_head;
2284 void * saddr = http_common_address_from_socket (plugin->protocol, addr, addrlen);
2287 saddr_len = http_common_address_get_size (saddr);
2291 if (GNUNET_YES == http_common_cmp_addresses(w->address, w->addrlen, saddr, saddr_len))
2295 GNUNET_free (saddr);
2300 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2301 "Notifying transport to remove address `%s'\n",
2302 http_common_plugin_address_to_string (NULL,
2304 w->address, w->addrlen));
2307 GNUNET_CONTAINER_DLL_remove (plugin->addr_head, plugin->addr_tail, w);
2309 /* modify our published address list */
2311 address = GNUNET_HELLO_address_allocate (plugin->env->my_identity,
2312 "https_client", w->address, w->addrlen, GNUNET_HELLO_ADDRESS_INFO_NONE);
2314 address = GNUNET_HELLO_address_allocate (plugin->env->my_identity,
2315 "http_client", w->address, w->addrlen, GNUNET_HELLO_ADDRESS_INFO_NONE);
2317 plugin->env->notify_address (plugin->env->cls, add_remove, address);
2318 GNUNET_HELLO_address_free (address);
2319 GNUNET_free (w->address);
2326 * Our external IP address/port mapping has changed.
2328 * @param cls closure, the 'struct LocalAddrList'
2329 * @param add_remove GNUNET_YES to mean the new public IP address, GNUNET_NO to mean
2330 * the previous (now invalid) one
2331 * @param addr either the previous or the new public IP address
2332 * @param addrlen actual lenght of the address
2335 server_nat_port_map_callback (void *cls, int add_remove, const struct sockaddr *addr,
2338 GNUNET_assert (cls != NULL);
2339 struct HTTP_Server_Plugin *plugin = cls;
2341 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2342 "NAT called to %s address `%s'\n",
2343 (add_remove == GNUNET_NO) ? "remove" : "add",
2344 GNUNET_a2s (addr, addrlen));
2346 if (AF_INET == addr->sa_family)
2348 struct sockaddr_in *s4 = (struct sockaddr_in *) addr;
2350 if (GNUNET_NO == plugin->use_ipv4)
2353 if ((NULL != plugin->server_addr_v4) &&
2354 (0 != memcmp (&plugin->server_addr_v4->sin_addr,
2355 &s4->sin_addr, sizeof (struct in_addr))))
2357 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2358 "Skipping address `%s' (not bindto address)\n",
2359 GNUNET_a2s (addr, addrlen));
2364 if (AF_INET6 == addr->sa_family)
2366 struct sockaddr_in6 *s6 = (struct sockaddr_in6 *) addr;
2367 if (GNUNET_NO == plugin->use_ipv6)
2370 if ((NULL != plugin->server_addr_v6) &&
2371 (0 != memcmp (&plugin->server_addr_v6->sin6_addr,
2372 &s6->sin6_addr, sizeof (struct in6_addr))))
2374 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2375 "Skipping address `%s' (not bindto address)\n",
2376 GNUNET_a2s (addr, addrlen));
2384 server_add_address (cls, add_remove, addr, addrlen);
2387 server_remove_address (cls, add_remove, addr, addrlen);
2394 * Get valid server addresses
2396 * @param plugin the plugin handle
2397 * @param service_name the servicename
2398 * @param cfg configuration handle
2399 * @param addrs addresses
2400 * @param addr_lens address length
2401 * @return number of addresses
2404 server_get_addresses (struct HTTP_Server_Plugin *plugin,
2405 const char *service_name,
2406 const struct GNUNET_CONFIGURATION_Handle *cfg,
2407 struct sockaddr ***addrs, socklen_t ** addr_lens)
2410 unsigned long long port;
2411 struct addrinfo hints;
2412 struct addrinfo *res;
2413 struct addrinfo *pos;
2414 struct addrinfo *next;
2418 struct sockaddr **saddrs;
2419 socklen_t *saddrlens;
2425 disablev6 = !plugin->use_ipv6;
2428 if (GNUNET_CONFIGURATION_have_value (cfg, service_name, "PORT"))
2430 GNUNET_break (GNUNET_OK ==
2431 GNUNET_CONFIGURATION_get_value_number (cfg, service_name,
2435 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2437 ("Require valid port number for service in configuration!\n"));
2438 return GNUNET_SYSERR;
2443 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, plugin->name,
2444 "Starting in listen only mode\n");
2445 return -1; /* listen only */
2449 if (GNUNET_CONFIGURATION_have_value (cfg, service_name, "BINDTO"))
2451 GNUNET_break (GNUNET_OK ==
2452 GNUNET_CONFIGURATION_get_value_string (cfg, service_name,
2453 "BINDTO", &hostname));
2458 if (hostname != NULL)
2460 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2461 "Resolving `%s' since that is where `%s' will bind to.\n",
2462 hostname, service_name);
2463 memset (&hints, 0, sizeof (struct addrinfo));
2465 hints.ai_family = AF_INET;
2466 if ((0 != (ret = getaddrinfo (hostname, NULL, &hints, &res))) ||
2469 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Failed to resolve `%s': %s\n"),
2470 hostname, gai_strerror (ret));
2471 GNUNET_free (hostname);
2472 return GNUNET_SYSERR;
2476 while (NULL != (pos = next))
2478 next = pos->ai_next;
2479 if ((disablev6) && (pos->ai_family == AF_INET6))
2485 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2486 _("Failed to find %saddress for `%s'.\n"),
2487 disablev6 ? "IPv4 " : "", hostname);
2489 GNUNET_free (hostname);
2490 return GNUNET_SYSERR;
2493 saddrs = GNUNET_malloc ((resi + 1) * sizeof (struct sockaddr *));
2494 saddrlens = GNUNET_malloc ((resi + 1) * sizeof (socklen_t));
2497 while (NULL != (pos = next))
2499 next = pos->ai_next;
2500 if ((disablev6) && (pos->ai_family == AF_INET6))
2502 if ((pos->ai_protocol != IPPROTO_TCP) && (pos->ai_protocol != 0))
2503 continue; /* not TCP */
2504 if ((pos->ai_socktype != SOCK_STREAM) && (pos->ai_socktype != 0))
2505 continue; /* huh? */
2506 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2507 "Service will bind to `%s'\n", GNUNET_a2s (pos->ai_addr,
2509 if (pos->ai_family == AF_INET)
2511 GNUNET_assert (pos->ai_addrlen == sizeof (struct sockaddr_in));
2512 saddrlens[i] = pos->ai_addrlen;
2513 saddrs[i] = GNUNET_malloc (saddrlens[i]);
2514 memcpy (saddrs[i], pos->ai_addr, saddrlens[i]);
2515 ((struct sockaddr_in *) saddrs[i])->sin_port = htons (port);
2519 GNUNET_assert (pos->ai_family == AF_INET6);
2520 GNUNET_assert (pos->ai_addrlen == sizeof (struct sockaddr_in6));
2521 saddrlens[i] = pos->ai_addrlen;
2522 saddrs[i] = GNUNET_malloc (saddrlens[i]);
2523 memcpy (saddrs[i], pos->ai_addr, saddrlens[i]);
2524 ((struct sockaddr_in6 *) saddrs[i])->sin6_port = htons (port);
2528 GNUNET_free (hostname);
2534 /* will bind against everything, just set port */
2540 saddrs = GNUNET_malloc ((resi + 1) * sizeof (struct sockaddr *));
2541 saddrlens = GNUNET_malloc ((resi + 1) * sizeof (socklen_t));
2543 saddrlens[i] = sizeof (struct sockaddr_in);
2544 saddrs[i] = GNUNET_malloc (saddrlens[i]);
2545 #if HAVE_SOCKADDR_IN_SIN_LEN
2546 ((struct sockaddr_in *) saddrs[i])->sin_len = saddrlens[i];
2548 ((struct sockaddr_in *) saddrs[i])->sin_family = AF_INET;
2549 ((struct sockaddr_in *) saddrs[i])->sin_port = htons (port);
2555 saddrs = GNUNET_malloc ((resi + 1) * sizeof (struct sockaddr *));
2556 saddrlens = GNUNET_malloc ((resi + 1) * sizeof (socklen_t));
2558 saddrlens[i] = sizeof (struct sockaddr_in6);
2559 saddrs[i] = GNUNET_malloc (saddrlens[i]);
2560 #if HAVE_SOCKADDR_IN_SIN_LEN
2561 ((struct sockaddr_in6 *) saddrs[i])->sin6_len = saddrlens[0];
2563 ((struct sockaddr_in6 *) saddrs[i])->sin6_family = AF_INET6;
2564 ((struct sockaddr_in6 *) saddrs[i])->sin6_port = htons (port);
2566 saddrlens[i] = sizeof (struct sockaddr_in);
2567 saddrs[i] = GNUNET_malloc (saddrlens[i]);
2568 #if HAVE_SOCKADDR_IN_SIN_LEN
2569 ((struct sockaddr_in *) saddrs[i])->sin_len = saddrlens[1];
2571 ((struct sockaddr_in *) saddrs[i])->sin_family = AF_INET;
2572 ((struct sockaddr_in *) saddrs[i])->sin_port = htons (port);
2576 *addr_lens = saddrlens;
2582 * Ask NAT for addresses
2584 * @param plugin the plugin handle
2587 server_start_report_addresses (struct HTTP_Server_Plugin *plugin)
2589 int res = GNUNET_OK;
2590 struct sockaddr **addrs;
2591 socklen_t *addrlens;
2593 res = server_get_addresses (plugin,
2594 plugin->name, plugin->env->cfg,
2596 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2597 _("Found %u addresses to report to NAT service\n"), res);
2599 if (GNUNET_SYSERR == res)
2606 GNUNET_NAT_register (plugin->env->cfg, GNUNET_YES, plugin->port,
2608 (const struct sockaddr **) addrs, addrlens,
2609 &server_nat_port_map_callback, NULL, plugin);
2613 GNUNET_assert (addrs[res] != NULL);
2614 GNUNET_free (addrs[res]);
2616 GNUNET_free_non_null (addrs);
2617 GNUNET_free_non_null (addrlens);
2622 * Stop NAT for addresses
2624 * @param plugin the plugin handle
2627 server_stop_report_addresses (struct HTTP_Server_Plugin *plugin)
2629 /* Stop NAT handle */
2630 if (NULL != plugin->nat)
2631 GNUNET_NAT_unregister (plugin->nat);
2633 /* Clean up addresses */
2634 struct HttpAddressWrapper *w;
2636 while (plugin->addr_head != NULL)
2638 w = plugin->addr_head;
2639 GNUNET_CONTAINER_DLL_remove (plugin->addr_head, plugin->addr_tail, w);
2640 GNUNET_free (w->address);
2647 * Check if IPv6 supported on this system
2649 * @param plugin the plugin handle
2650 * @return GNUNET_YES on success, else GNUNET_NO
2653 server_check_ipv6_support (struct HTTP_Server_Plugin *plugin)
2655 struct GNUNET_NETWORK_Handle *desc = NULL;
2656 int res = GNUNET_NO;
2658 /* Probe IPv6 support */
2659 desc = GNUNET_NETWORK_socket_create (PF_INET6, SOCK_STREAM, 0);
2662 if ((errno == ENOBUFS) || (errno == ENOMEM) || (errno == ENFILE) ||
2665 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "socket");
2667 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, plugin->name,
2669 ("Disabling IPv6 since it is not supported on this system!\n"));
2674 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (desc));
2678 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2679 "Testing IPv6 on this system: %s\n",
2680 (res == GNUNET_YES) ? "successful" : "failed");
2686 * Notify server about our external hostname
2689 * @param tc task context (unused)
2692 server_notify_external_hostname (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2694 struct HTTP_Server_Plugin *plugin = cls;
2695 struct HttpAddress *ext_addr;
2696 size_t ext_addr_len;
2700 plugin->notify_ext_task = GNUNET_SCHEDULER_NO_TASK;
2701 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
2704 GNUNET_asprintf(&url, "%s://%s", plugin->protocol, plugin->external_hostname);
2706 urlen = strlen (url) + 1;
2707 ext_addr = GNUNET_malloc (sizeof (struct HttpAddress) + urlen);
2708 ext_addr->options = htonl(plugin->options);
2709 ext_addr->urlen = htonl (urlen);
2710 ext_addr_len = sizeof (struct HttpAddress) + urlen;
2711 memcpy (&ext_addr[1], url, urlen);
2714 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2715 "Notifying transport about external hostname address `%s'\n", plugin->external_hostname);
2718 if (GNUNET_YES == plugin->verify_external_hostname)
2719 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, plugin->name,
2720 "Enabling SSL verification for external hostname address `%s'\n",
2721 plugin->external_hostname);
2722 plugin->ext_addr = GNUNET_HELLO_address_allocate (plugin->env->my_identity,
2723 "https_client", ext_addr, ext_addr_len, GNUNET_HELLO_ADDRESS_INFO_NONE );
2724 plugin->env->notify_address (plugin->env->cls, GNUNET_YES, plugin->ext_addr);
2725 GNUNET_free (ext_addr);
2727 plugin->ext_addr = GNUNET_HELLO_address_allocate (plugin->env->my_identity,
2728 "http_client", ext_addr, ext_addr_len, GNUNET_HELLO_ADDRESS_INFO_NONE );
2729 plugin->env->notify_address (plugin->env->cls, GNUNET_YES, plugin->ext_addr);
2730 GNUNET_free (ext_addr);
2736 * Configure the plugin
2738 * @param plugin plugin handle
2739 * @return GNUNET_OK on success, GNUNET_SYSERR on failure
2742 server_configure_plugin (struct HTTP_Server_Plugin *plugin)
2744 unsigned long long port;
2745 unsigned long long max_connections;
2746 char *bind4_address = NULL;
2747 char *bind6_address = NULL;
2748 char *eh_tmp = NULL;
2749 int external_hostname_use_port;
2752 if (GNUNET_CONFIGURATION_have_value
2753 (plugin->env->cfg, plugin->name, "USE_IPv4"))
2756 GNUNET_CONFIGURATION_get_value_yesno (plugin->env->cfg, plugin->name,
2760 plugin->use_ipv4 = GNUNET_YES;
2761 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2762 _("IPv4 support is %s\n"),
2763 (plugin->use_ipv4 == GNUNET_YES) ? "enabled" : "disabled");
2766 if (GNUNET_CONFIGURATION_have_value
2767 (plugin->env->cfg, plugin->name, "USE_IPv6"))
2770 GNUNET_CONFIGURATION_get_value_yesno (plugin->env->cfg, plugin->name,
2774 plugin->use_ipv6 = GNUNET_YES;
2775 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2776 _("IPv6 support is %s\n"),
2777 (plugin->use_ipv6 == GNUNET_YES) ? "enabled" : "disabled");
2779 if ((plugin->use_ipv4 == GNUNET_NO) && (plugin->use_ipv6 == GNUNET_NO))
2781 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
2783 ("Neither IPv4 nor IPv6 are enabled! Fix in configuration\n"),
2785 return GNUNET_SYSERR;
2788 /* Reading port number from config file */
2790 GNUNET_CONFIGURATION_get_value_number (plugin->env->cfg, plugin->name,
2791 "PORT", &port)) || (port > 65535))
2793 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
2794 _("Port is required! Fix in configuration\n"),
2796 return GNUNET_SYSERR;
2798 plugin->port = port;
2800 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, plugin->name,
2801 _("Using port %u\n"), plugin->port);
2803 if ((plugin->use_ipv4 == GNUNET_YES) &&
2804 (GNUNET_YES == GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg,
2805 plugin->name, "BINDTO", &bind4_address)))
2807 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2808 "Binding %s plugin to specific IPv4 address: `%s'\n",
2809 plugin->protocol, bind4_address);
2810 plugin->server_addr_v4 = GNUNET_new (struct sockaddr_in);
2811 if (1 != inet_pton (AF_INET, bind4_address,
2812 &plugin->server_addr_v4->sin_addr))
2814 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
2816 ("Specific IPv4 address `%s' in configuration file is invalid!\n"),
2818 GNUNET_free (bind4_address);
2819 GNUNET_free (plugin->server_addr_v4);
2820 plugin->server_addr_v4 = NULL;
2821 return GNUNET_SYSERR;
2825 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2826 _("Binding to IPv4 address %s\n"), bind4_address);
2827 plugin->server_addr_v4->sin_family = AF_INET;
2828 plugin->server_addr_v4->sin_port = htons (plugin->port);
2830 GNUNET_free (bind4_address);
2833 if ((plugin->use_ipv6 == GNUNET_YES) &&
2835 GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg, plugin->name,
2836 "BINDTO6", &bind6_address)))
2838 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2839 "Binding %s plugin to specific IPv6 address: `%s'\n",
2840 plugin->protocol, bind6_address);
2841 plugin->server_addr_v6 = GNUNET_new (struct sockaddr_in6);
2843 inet_pton (AF_INET6, bind6_address, &plugin->server_addr_v6->sin6_addr))
2845 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
2847 ("Specific IPv6 address `%s' in configuration file is invalid!\n"),
2849 GNUNET_free (bind6_address);
2850 GNUNET_free (plugin->server_addr_v6);
2851 plugin->server_addr_v6 = NULL;
2852 return GNUNET_SYSERR;
2856 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2857 _("Binding to IPv6 address %s\n"), bind6_address);
2858 plugin->server_addr_v6->sin6_family = AF_INET6;
2859 plugin->server_addr_v6->sin6_port = htons (plugin->port);
2861 GNUNET_free (bind6_address);
2864 plugin->verify_external_hostname = GNUNET_NO;
2866 plugin->verify_external_hostname = GNUNET_CONFIGURATION_get_value_yesno (plugin->env->cfg, plugin->name,
2867 "VERIFY_EXTERNAL_HOSTNAME");
2868 if (GNUNET_SYSERR == plugin->verify_external_hostname)
2869 plugin->verify_external_hostname = GNUNET_NO;
2870 if (GNUNET_YES == plugin->verify_external_hostname)
2871 plugin->options |= HTTP_OPTIONS_VERIFY_CERTIFICATE;
2873 external_hostname_use_port = GNUNET_CONFIGURATION_get_value_yesno (plugin->env->cfg, plugin->name,
2874 "EXTERNAL_HOSTNAME_USE_PORT");
2875 if (GNUNET_SYSERR == external_hostname_use_port)
2876 external_hostname_use_port = GNUNET_NO;
2879 if (GNUNET_YES == GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg, plugin->name,
2880 "EXTERNAL_HOSTNAME", &eh_tmp))
2884 char * pos_url = NULL;
2886 if (NULL != strstr(eh_tmp, "://"))
2888 tmp = &strstr(eh_tmp, "://")[3];
2893 if (GNUNET_YES == external_hostname_use_port)
2895 if ( (strlen (tmp) > 1) && (NULL != (pos = strchr(tmp, '/'))) )
2899 GNUNET_asprintf (&plugin->external_hostname, "%s:%u/%s", tmp, (uint16_t) port, (NULL == pos_url) ? "" : pos_url);
2902 GNUNET_asprintf (&plugin->external_hostname, "%s:%u", tmp, (uint16_t) port);
2905 plugin->external_hostname = GNUNET_strdup (tmp);
2906 GNUNET_free (eh_tmp);
2908 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, plugin->name,
2909 _("Using external hostname `%s'\n"), plugin->external_hostname);
2910 plugin->notify_ext_task = GNUNET_SCHEDULER_add_now (&server_notify_external_hostname, plugin);
2912 /* Use only configured external hostname */
2913 if (GNUNET_CONFIGURATION_have_value
2914 (plugin->env->cfg, plugin->name, "EXTERNAL_HOSTNAME_ONLY"))
2916 plugin->external_only =
2917 GNUNET_CONFIGURATION_get_value_yesno (plugin->env->cfg, plugin->name,
2918 "EXTERNAL_HOSTNAME_ONLY");
2921 plugin->external_only = GNUNET_NO;
2923 if (GNUNET_YES == plugin->external_only)
2924 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2925 _("Notifying transport only about hostname `%s'\n"), plugin->external_hostname);
2928 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2929 "No external hostname configured\n");
2931 /* Optional parameters */
2932 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (plugin->env->cfg,
2934 "MAX_CONNECTIONS", &max_connections))
2935 max_connections = 128;
2936 plugin->max_connections = max_connections;
2938 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2939 _("Maximum number of connections is %u\n"),
2940 plugin->max_connections);
2943 plugin->peer_id_length = strlen (GNUNET_i2s_full (plugin->env->my_identity));
2950 * Session was idle, so disconnect it
2952 * @param cls the session
2953 * @param tc task context
2956 server_session_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2958 struct Session *s = cls;
2960 s->timeout_task = GNUNET_SCHEDULER_NO_TASK;
2961 GNUNET_log (TIMEOUT_LOG,
2962 "Session %p was idle for %s, disconnecting\n",
2964 GNUNET_STRINGS_relative_time_to_string (HTTP_SERVER_SESSION_TIMEOUT,
2967 /* call session destroy function */
2968 GNUNET_assert (GNUNET_OK ==
2969 http_server_plugin_disconnect_session (s->plugin, s));
2974 * Start session timeout for session s
2976 * @param s the session
2979 server_start_session_timeout (struct Session *s)
2981 GNUNET_assert (NULL != s);
2982 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == s->timeout_task);
2983 s->timeout_task = GNUNET_SCHEDULER_add_delayed (HTTP_SERVER_SESSION_TIMEOUT,
2984 &server_session_timeout,
2986 GNUNET_log (TIMEOUT_LOG,
2987 "Timeout for session %p set to %s\n",
2989 GNUNET_STRINGS_relative_time_to_string (HTTP_SERVER_SESSION_TIMEOUT,
2995 * Increment session timeout due to activity session s
2997 * @param s the session
3000 server_reschedule_session_timeout (struct Session *s)
3002 GNUNET_assert (NULL != s);
3003 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK != s->timeout_task);
3005 GNUNET_SCHEDULER_cancel (s->timeout_task);
3006 s->timeout_task = GNUNET_SCHEDULER_add_delayed (HTTP_SERVER_SESSION_TIMEOUT,
3007 &server_session_timeout,
3009 GNUNET_log (TIMEOUT_LOG,
3010 "Timeout rescheduled for session %p set to %s\n",
3012 GNUNET_STRINGS_relative_time_to_string (HTTP_SERVER_SESSION_TIMEOUT,
3018 * Exit point from the plugin.
3024 LIBGNUNET_PLUGIN_TRANSPORT_DONE (void *cls)
3026 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
3027 struct HTTP_Server_Plugin *plugin = api->cls;
3028 struct Session *pos;
3029 struct Session *next;
3031 if (NULL == api->cls)
3033 /* Free for stub mode */
3037 plugin->in_shutdown = GNUNET_YES;
3038 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
3039 _("Shutting down plugin `%s'\n"),
3042 if (GNUNET_SCHEDULER_NO_TASK != plugin->notify_ext_task)
3044 GNUNET_SCHEDULER_cancel (plugin->notify_ext_task);
3045 plugin->notify_ext_task = GNUNET_SCHEDULER_NO_TASK;
3048 if (NULL != plugin->ext_addr)
3050 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
3051 "Notifying transport to remove address `%s'\n",
3052 http_common_plugin_address_to_string (NULL,
3054 plugin->ext_addr->address,
3055 plugin->ext_addr->address_length));
3057 plugin->env->notify_address (plugin->env->cls,
3061 plugin->env->notify_address (plugin->env->cls,
3065 GNUNET_HELLO_address_free (plugin->ext_addr);
3066 plugin->ext_addr = NULL;
3069 /* Stop to report addresses to transport service */
3070 server_stop_report_addresses (plugin);
3071 server_stop (plugin);
3072 next = plugin->head;
3073 while (NULL != (pos = next))
3076 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
3077 "Removing left over session %p\n", pos);
3079 if ((GNUNET_YES == pos->session_passed) && (GNUNET_NO == pos->session_ended))
3081 /* Notify transport immediately that this session is invalid */
3082 pos->session_ended = GNUNET_YES;
3083 plugin->env->session_end (plugin->env->cls, pos->address, pos);
3085 server_delete_session (plugin, pos);
3089 GNUNET_free_non_null (plugin->external_hostname);
3090 GNUNET_free_non_null (plugin->ext_addr);
3091 GNUNET_free_non_null (plugin->server_addr_v4);
3092 GNUNET_free_non_null (plugin->server_addr_v6);
3094 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
3095 _("Shutdown for plugin `%s' complete\n"),
3098 GNUNET_free (plugin);
3105 http_plugin_address_to_string (void *cls,
3110 return http_common_plugin_address_to_string (cls, PLUGIN_NAME, addr, addrlen);
3112 return http_common_plugin_address_to_string (cls, PLUGIN_NAME, addr, addrlen);
3118 * Function obtain the network type for a session
3120 * @param cls closure ('struct Plugin*')
3121 * @param session the session
3122 * @return the network type in HBO or GNUNET_SYSERR
3124 static enum GNUNET_ATS_Network_Type
3125 http_server_get_network (void *cls,
3126 struct Session *session)
3128 GNUNET_assert (NULL != session);
3129 return ntohl (session->ats_address_network_type);
3134 * Entry point for the plugin.
3140 LIBGNUNET_PLUGIN_TRANSPORT_INIT (void *cls)
3142 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
3143 struct GNUNET_TRANSPORT_PluginFunctions *api;
3144 struct HTTP_Server_Plugin *plugin;
3146 if (NULL == env->receive)
3148 /* run in 'stub' mode (i.e. as part of gnunet-peerinfo), don't fully
3149 initialze the plugin or the API */
3150 api = GNUNET_new (struct GNUNET_TRANSPORT_PluginFunctions);
3152 api->address_to_string = &http_plugin_address_to_string;
3153 api->string_to_address = &http_common_plugin_string_to_address;
3154 api->address_pretty_printer = &http_common_plugin_address_pretty_printer;
3157 plugin = GNUNET_new (struct HTTP_Server_Plugin);
3159 api = GNUNET_new (struct GNUNET_TRANSPORT_PluginFunctions);
3161 api->send = &http_server_plugin_send;
3162 api->disconnect_peer = &http_server_plugin_disconnect_peer;
3163 api->disconnect_session = &http_server_plugin_disconnect_session;
3164 api->query_keepalive_factor = &http_server_query_keepalive_factor;
3165 api->check_address = &http_server_plugin_address_suggested;
3166 api->get_session = &http_server_plugin_get_session;
3168 api->address_to_string = &http_plugin_address_to_string;
3169 api->string_to_address = &http_common_plugin_string_to_address;
3170 api->address_pretty_printer = &http_common_plugin_address_pretty_printer;
3171 api->get_network = &http_server_get_network;
3172 api->update_session_timeout = &http_server_plugin_update_session_timeout;
3174 plugin->name = "transport-https_server";
3175 plugin->protocol = "https";
3177 plugin->name = "transport-http_server";
3178 plugin->protocol = "http";
3181 /* Configure plugin */
3182 if (GNUNET_SYSERR == server_configure_plugin (plugin))
3184 LIBGNUNET_PLUGIN_TRANSPORT_DONE (api);
3188 /* Check IPv6 support */
3189 if (GNUNET_YES == plugin->use_ipv6)
3190 plugin->use_ipv6 = server_check_ipv6_support (plugin);
3192 /* Report addresses to transport service */
3193 if (GNUNET_NO == plugin->external_only)
3194 server_start_report_addresses (plugin);
3196 if (GNUNET_SYSERR == server_start (plugin))
3198 LIBGNUNET_PLUGIN_TRANSPORT_DONE (api);
3204 /* end of plugin_transport_http_server.c */