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
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;
116 struct GNUNET_HELLO_Address *address;
119 * Unique HTTP/S connection tag for this connection
124 * ATS network type in NBO
126 uint32_t ats_address_network_type;
129 * Was session given to transport service?
134 * Did we immediately end the session in disconnect_cb
139 * Are incoming connection established at the moment
141 int connect_in_progress;
144 * Absolute time when to receive data again
145 * Used for receive throttling
147 struct GNUNET_TIME_Absolute next_receive;
150 * Session timeout task
152 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
156 struct ServerConnection
164 * Should this connection get disconnected? GNUNET_YES/NO
169 * For PUT connections: Is this the first or last callback with size 0
174 * The session this server connection belongs to
176 struct Session *session;
181 struct MHD_Connection *mhd_conn;
186 struct MHD_Daemon *mhd_daemon;
191 * Encapsulation of all of the state of the plugin.
193 struct HTTP_Server_Plugin
198 struct GNUNET_TRANSPORT_PluginEnvironment *env;
201 * Linked list head of open sessions.
203 struct Session *head;
206 * Linked list tail of open sessions.
208 struct Session *tail;
221 * My options to be included in the address
228 char *external_hostname;
231 * Verify external address
233 int verify_external_hostname;
236 * Maximum number of sockets the plugin can use
237 * Each http inbound /outbound connections are two connections
239 unsigned int max_connections;
242 * Current number of sockets the plugin can use
243 * Each http inbound /outbound connections are two connections
245 unsigned int cur_connections;
248 * Did we immediately end the session in disconnect_cb
258 * External hostname the plugin can be connected to, can be different to
259 * the host's FQDN, used e.g. for reverse proxying
261 struct GNUNET_HELLO_Address *ext_addr;
264 * Notify transport only about external address
266 unsigned int external_only;
284 * Task calling transport service about external address
286 GNUNET_SCHEDULER_TaskIdentifier notify_ext_task;
289 * NAT handle & address management
291 struct GNUNET_NAT_Handle *nat;
294 * List of own addresses
298 * IPv4 addresses DLL head
300 struct HttpAddressWrapper *addr_head;
303 * IPv4 addresses DLL tail
305 struct HttpAddressWrapper *addr_tail;
308 * IPv4 server socket to bind to
310 struct sockaddr_in *server_addr_v4;
313 * IPv6 server socket to bind to
315 struct sockaddr_in6 *server_addr_v6;
320 GNUNET_SCHEDULER_TaskIdentifier server_v4_task;
325 GNUNET_SCHEDULER_TaskIdentifier server_v6_task;
328 * The IPv4 server is scheduled to run asap
330 int server_v4_immediately;
333 * The IPv6 server is scheduled to run asap
335 int server_v6_immediately;
340 struct MHD_Daemon *server_v4;
345 struct MHD_Daemon *server_v6;
353 * Use RC4-128 instead of AES:
354 * NONE:+VERS-TLS1.0:+ARCFOUR-128:+SHA1:+RSA:+COMP-NULL
374 * Wrapper to manage addresses
376 struct HttpAddressWrapper
381 struct HttpAddressWrapper *next;
384 * Linked list previous
386 struct HttpAddressWrapper *prev;
388 struct HttpAddress *address;
395 * Message to send using http
400 * next pointer for double linked list
402 struct HTTP_Message *next;
405 * previous pointer for double linked list
407 struct HTTP_Message *prev;
410 * buffer containing data to send
415 * amount of data already sent
425 * HTTP/S specific overhead
430 * Continuation function to call once the transmission buffer
431 * has again space available. NULL if there is no
432 * continuation to call.
434 GNUNET_TRANSPORT_TransmitContinuation transmit_cont;
437 * Closure for transmit_cont.
439 void *transmit_cont_cls;
444 * Start session timeout for session s
445 * @param s the session
448 server_start_session_timeout (struct Session *s);
452 * Increment session timeout due to activity for session s
453 * @param s the session
456 server_reschedule_session_timeout (struct Session *s);
460 * Cancel timeout for session s
461 * @param s the session
464 server_stop_session_timeout (struct Session *s);
468 * Disconnect session @a s
470 * @param cls closure with the `struct HTTP_Server_Plugin`
471 * @param s the session
472 * @return #GNUNET_OK on success
475 http_server_plugin_disconnect_session (void *cls,
480 * Does session s exist?
482 * @param plugin the plugin handle
483 * @param s the session
484 * @return #GNUNET_YES on success, #GNUNET_NO on error
487 server_exist_session (struct HTTP_Server_Plugin *plugin, struct Session *s);
491 * Reschedule the execution of both IPv4 and IPv6 server
492 * @param plugin the plugin
493 * @param server which server to schedule v4 or v6?
494 * @param now #GNUNET_YES to schedule execution immediately, #GNUNET_NO to wait
498 server_reschedule (struct HTTP_Server_Plugin *plugin,
499 struct MHD_Daemon *server,
504 * Function that can be used by the transport service to transmit
505 * a message using the plugin. Note that in the case of a
506 * peer disconnecting, the continuation MUST be called
507 * prior to the disconnect notification itself. This function
508 * will be called with this peer's HELLO message to initiate
509 * a fresh connection to another peer.
512 * @param session which session must be used
513 * @param msgbuf the message to transmit
514 * @param msgbuf_size number of bytes in @a msgbuf
515 * @param priority how important is the message (most plugins will
516 * ignore message priority and just FIFO)
517 * @param to how long to wait at most for the transmission (does not
518 * require plugins to discard the message after the timeout,
519 * just advisory for the desired delay; most plugins will ignore
521 * @param cont continuation to call once the message has
522 * been transmitted (or if the transport is ready
523 * for the next transmission call; or if the
524 * peer disconnected...); can be NULL
525 * @param cont_cls closure for @a cont
526 * @return number of bytes used (on the physical network, with overheads);
527 * -1 on hard errors (i.e. address invalid); 0 is a legal value
528 * and does NOT mean that the message was not transmitted (DV)
531 http_server_plugin_send (void *cls,
532 struct Session *session,
533 const char *msgbuf, size_t msgbuf_size,
534 unsigned int priority,
535 struct GNUNET_TIME_Relative to,
536 GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
538 struct HTTP_Server_Plugin *plugin = cls;
539 struct HTTP_Message *msg;
543 GNUNET_assert (plugin != NULL);
544 GNUNET_assert (session != NULL);
546 if (GNUNET_NO == server_exist_session (plugin, session))
549 return GNUNET_SYSERR;
551 if (NULL == session->server_send)
553 if (GNUNET_NO == session->connect_in_progress)
555 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, session->plugin->name,
556 "Session %p/connection %p: Sending message with %u bytes to peer `%s' with FAILED\n",
557 session, session->server_send,
558 msgbuf_size, GNUNET_i2s (&session->target));
560 return GNUNET_SYSERR;
565 if (GNUNET_YES == session->server_send->disconnect)
566 return GNUNET_SYSERR;
570 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, session->plugin->name,
571 "Session %p/connection %p: Sending message with %u to peer `%s' with \n",
572 session, session->server_send,
573 msgbuf_size, GNUNET_i2s (&session->target));
575 /* create new message and schedule */
576 bytes_sent = sizeof (struct HTTP_Message) + msgbuf_size;
577 msg = GNUNET_malloc (bytes_sent);
579 msg->size = msgbuf_size;
581 msg->buf = (char *) &msg[1];
582 msg->transmit_cont = cont;
583 msg->transmit_cont_cls = cont_cls;
584 memcpy (msg->buf, msgbuf, msgbuf_size);
586 GNUNET_CONTAINER_DLL_insert_tail (session->msg_head, session->msg_tail, msg);
588 GNUNET_asprintf (&stat_txt, "# bytes currently in %s_server buffers", plugin->protocol);
589 GNUNET_STATISTICS_update (plugin->env->stats,
590 stat_txt, msgbuf_size, GNUNET_NO);
591 GNUNET_free (stat_txt);
593 if (NULL != session->server_send)
595 server_reschedule (session->plugin,
596 session->server_send->mhd_daemon,
604 * Function that can be used to force the plugin to disconnect
605 * from the given peer and cancel all previous transmissions
606 * (and their continuationc).
609 * @param target peer from which to disconnect
612 http_server_plugin_disconnect_peer (void *cls,
613 const struct GNUNET_PeerIdentity *target)
615 struct HTTP_Server_Plugin *plugin = cls;
616 struct Session *next;
619 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
620 "Transport tells me to disconnect `%s'\n",
621 GNUNET_i2s (target));
623 while (NULL != (pos = next))
626 if (0 == memcmp (target, &pos->target, sizeof (struct GNUNET_PeerIdentity)))
628 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
629 "Disconnecting session %p to `%s'\n",
630 pos, GNUNET_i2s (target));
631 http_server_plugin_disconnect_session (plugin, pos);
638 * Another peer has suggested an address for this
639 * peer and transport plugin. Check that this could be a valid
640 * address. If so, consider adding it to the list
644 * @param addr pointer to the address
645 * @param addrlen length of @a addr
646 * @return #GNUNET_OK if this is a plausible address for this peer
650 http_server_plugin_address_suggested (void *cls,
654 struct HTTP_Server_Plugin *plugin = cls;
655 struct HttpAddressWrapper *next;
656 struct HttpAddressWrapper *pos;
657 const struct HttpAddress *haddr = addr;
659 if ((NULL != plugin->ext_addr) &&
660 GNUNET_YES == (http_common_cmp_addresses (addr, addrlen,
661 plugin->ext_addr->address,
662 plugin->ext_addr->address_length)))
664 /* Checking HTTP_OPTIONS_VERIFY_CERTIFICATE option for external hostname */
665 if ((ntohl (haddr->options) & HTTP_OPTIONS_VERIFY_CERTIFICATE) !=
666 (plugin->options & HTTP_OPTIONS_VERIFY_CERTIFICATE))
667 return GNUNET_NO; /* VERIFY option not set as required! */
670 next = plugin->addr_head;
671 while (NULL != (pos = next))
674 if (GNUNET_YES == (http_common_cmp_addresses(addr,
685 * Creates a new outbound session the transport
686 * service will use to send data to the peer
688 * Since HTTP/S server cannot create sessions, always return NULL
690 * @param cls the plugin
691 * @param address the address
692 * @return always NULL
694 static struct Session *
695 http_server_plugin_get_session (void *cls,
696 const struct GNUNET_HELLO_Address *address)
703 * Deleting the session
704 * Must not be used afterwards
706 * @param cls closure with the `struct HTTP_ServerPlugin`
707 * @param s the session to delete
708 * @return #GNUNET_OK on success
711 server_delete_session (void *cls,
714 struct HTTP_Server_Plugin *plugin = cls;
715 struct HTTP_Message *msg;
716 struct HTTP_Message *tmp;
718 server_stop_session_timeout(s);
719 GNUNET_CONTAINER_DLL_remove (plugin->head, plugin->tail, s);
724 GNUNET_CONTAINER_DLL_remove (s->msg_head, s->msg_tail, msg);
725 if (NULL != msg->transmit_cont)
726 msg->transmit_cont (msg->transmit_cont_cls, &s->target, GNUNET_SYSERR,
727 msg->size, msg->pos + msg->overhead);
731 if (NULL != s->msg_tk)
733 GNUNET_SERVER_mst_destroy (s->msg_tk);
736 GNUNET_HELLO_address_free (s->address);
737 GNUNET_free_non_null (s->server_recv);
738 GNUNET_free_non_null (s->server_send);
739 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
741 "Session %p destroyed\n", s);
748 * Cancel timeout for session s
750 * @param s the session
753 server_stop_session_timeout (struct Session *s)
755 GNUNET_assert (NULL != s);
757 if (GNUNET_SCHEDULER_NO_TASK != s->timeout_task)
759 GNUNET_SCHEDULER_cancel (s->timeout_task);
760 s->timeout_task = GNUNET_SCHEDULER_NO_TASK;
761 GNUNET_log (TIMEOUT_LOG, "Timeout stopped for session %p\n", s);
767 * Function that queries MHD's select sets and
768 * starts the task waiting for them.
769 * @param plugin plugin
770 * @param daemon_handle the MHD daemon handle
771 * @param now schedule immediately
772 * @return task identifier
774 static GNUNET_SCHEDULER_TaskIdentifier
775 server_schedule (struct HTTP_Server_Plugin *plugin,
776 struct MHD_Daemon *daemon_handle,
781 * Reschedule the execution of both IPv4 and IPv6 server
782 * @param plugin the plugin
783 * @param server which server to schedule v4 or v6?
784 * @param now #GNUNET_YES to schedule execution immediately, #GNUNET_NO to wait
788 server_reschedule (struct HTTP_Server_Plugin *plugin,
789 struct MHD_Daemon *server,
792 if ((server == plugin->server_v4) && (plugin->server_v4 != NULL))
794 if (GNUNET_YES == plugin->server_v4_immediately)
795 return; /* No rescheduling, server will run asap */
797 if (GNUNET_YES == now)
798 plugin->server_v4_immediately = GNUNET_YES;
800 if (plugin->server_v4_task != GNUNET_SCHEDULER_NO_TASK)
802 GNUNET_SCHEDULER_cancel (plugin->server_v4_task);
803 plugin->server_v4_task = GNUNET_SCHEDULER_NO_TASK;
805 plugin->server_v4_task = server_schedule (plugin, plugin->server_v4, now);
808 if ((server == plugin->server_v6) && (plugin->server_v6 != NULL))
810 if (GNUNET_YES == plugin->server_v6_immediately)
811 return; /* No rescheduling, server will run asap */
813 if (GNUNET_YES == now)
814 plugin->server_v6_immediately = GNUNET_YES;
816 if (plugin->server_v6_task != GNUNET_SCHEDULER_NO_TASK)
818 GNUNET_SCHEDULER_cancel (plugin->server_v6_task);
819 plugin->server_v6_task = GNUNET_SCHEDULER_NO_TASK;
821 plugin->server_v6_task = server_schedule (plugin, plugin->server_v6, now);
827 * Disconnect session @a s
829 * @param cls closure with the `struct HTTP_Server_Plugin`
830 * @param s the session
831 * @return #GNUNET_OK on success
834 http_server_plugin_disconnect_session (void *cls,
837 struct HTTP_Server_Plugin *plugin = cls;
838 struct ServerConnection * send;
839 struct ServerConnection * recv;
841 if (GNUNET_NO == server_exist_session (plugin, s))
844 return GNUNET_SYSERR;
847 send = (struct ServerConnection *) s->server_send;
848 if (s->server_send != NULL)
850 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, s->plugin->name,
851 "Server: %p / %p Terminating inbound PUT session to peer `%s'\n",
852 s, s->server_send, GNUNET_i2s (&s->target));
854 send->disconnect = GNUNET_YES;
855 MHD_set_connection_option (send->mhd_conn,
856 MHD_CONNECTION_OPTION_TIMEOUT,
858 server_reschedule (s->plugin, send->mhd_daemon, GNUNET_YES);
861 recv = (struct ServerConnection *) s->server_recv;
864 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, s->plugin->name,
865 "Server: %p / %p Terminating inbound GET session to peer `%s'\n",
866 s, s->server_recv, GNUNET_i2s (&s->target));
868 recv->disconnect = GNUNET_YES;
869 MHD_set_connection_option (recv->mhd_conn,
870 MHD_CONNECTION_OPTION_TIMEOUT,
872 server_reschedule (s->plugin, recv->mhd_daemon, GNUNET_YES);
879 * Function that is called to get the keepalive factor.
880 * GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT is divided by this number to
881 * calculate the interval between keepalive packets.
883 * @param cls closure with the `struct Plugin`
884 * @return keepalive factor
887 http_server_query_keepalive_factor (void *cls)
893 http_server_plugin_update_session_timeout (void *cls,
894 const struct GNUNET_PeerIdentity *peer,
895 struct Session *session)
897 struct HTTP_Server_Plugin *plugin = cls;
899 if (GNUNET_NO == server_exist_session (plugin, session))
902 server_reschedule_session_timeout (session);
907 * Tell MHD that the connection should timeout after @a to seconds.
909 * @param plugin our plugin
910 * @param s session for which the timeout changes
911 * @param to timeout in seconds
914 server_mhd_connection_timeout (struct HTTP_Server_Plugin *plugin,
918 /* Setting timeouts for other connections */
919 if (NULL != s->server_recv)
921 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
922 "Setting timeout for %p to %u sec.\n",
924 MHD_set_connection_option (s->server_recv->mhd_conn,
925 MHD_CONNECTION_OPTION_TIMEOUT,
927 server_reschedule (plugin, s->server_recv->mhd_daemon, GNUNET_NO);
929 if (NULL != s->server_send)
931 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
932 "Setting timeout for %p to %u sec.\n",
934 MHD_set_connection_option (s->server_send->mhd_conn,
935 MHD_CONNECTION_OPTION_TIMEOUT,
937 server_reschedule (plugin, s->server_send->mhd_daemon, GNUNET_NO);
943 * Parse incoming URL for tag and target
945 * @param plugin plugin
946 * @param url incoming url
947 * @param target where to store the target
948 * @param tag where to store the tag
949 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
952 server_parse_url (struct HTTP_Server_Plugin *plugin,
954 struct GNUNET_PeerIdentity *target,
957 char * tag_start = NULL;
958 char * tag_end = NULL;
959 char * target_start = NULL;
960 char * separator = NULL;
961 unsigned int hash_length;
962 unsigned long int ctag;
965 * URL is valid if it is in the form [prefix with (multiple) '/'][peerid[103];tag]*/
970 return GNUNET_SYSERR;
975 separator = strrchr (url, ';');
977 if (NULL == separator)
979 if (DEBUG_URL_PARSE) GNUNET_break (0);
980 return GNUNET_SYSERR;
982 tag_start = separator + 1;
984 if (strlen (tag_start) == 0)
986 /* No tag after separator */
987 if (DEBUG_URL_PARSE) GNUNET_break (0);
988 return GNUNET_SYSERR;
990 ctag = strtoul (tag_start, &tag_end, 10);
993 /* tag == 0 , invalid */
994 if (DEBUG_URL_PARSE) GNUNET_break (0);
995 return GNUNET_SYSERR;
997 if ((ctag == ULONG_MAX) && (ERANGE == errno))
999 /* out of range: > ULONG_MAX */
1000 if (DEBUG_URL_PARSE) GNUNET_break (0);
1001 return GNUNET_SYSERR;
1003 if (ctag > UINT32_MAX)
1005 /* out of range: > UINT32_MAX */
1006 if (DEBUG_URL_PARSE) GNUNET_break (0);
1007 return GNUNET_SYSERR;
1009 (*tag) = (uint32_t) ctag;
1010 if (NULL == tag_end)
1012 /* no char after tag */
1013 if (DEBUG_URL_PARSE) GNUNET_break (0);
1014 return GNUNET_SYSERR;
1016 if (url[strlen(url)] != tag_end[0])
1018 /* there are more not converted chars after tag */
1019 if (DEBUG_URL_PARSE) GNUNET_break (0);
1020 return GNUNET_SYSERR;
1022 if (DEBUG_URL_PARSE)
1023 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1024 "Found tag `%u' in url\n", (*tag));
1026 /* convert peer id */
1027 target_start = strrchr (url, '/');
1028 if (NULL == target_start)
1030 /* no leading '/' */
1031 target_start = (char *) url;
1034 hash_length = separator - target_start;
1035 if (hash_length != plugin->peer_id_length)
1037 /* no char after tag */
1038 if (DEBUG_URL_PARSE) GNUNET_break (0);
1039 return GNUNET_SYSERR;
1042 GNUNET_CRYPTO_eddsa_public_key_from_string (target_start,
1044 &target->public_key))
1046 /* hash conversion failed */
1047 if (DEBUG_URL_PARSE) GNUNET_break (0);
1048 return GNUNET_SYSERR;
1050 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1052 "Found target `%s' in URL\n",
1053 GNUNET_i2s_full (target));
1059 * Lookup a mhd connection and create one if none is found
1061 * @param plugin the plugin handle
1062 * @param mhd_connection the incoming mhd_connection
1063 * @param url incoming requested URL
1064 * @param method PUT or GET
1065 * @return the server connecetion
1067 static struct ServerConnection *
1068 server_lookup_connection (struct HTTP_Server_Plugin *plugin,
1069 struct MHD_Connection *mhd_connection, const char *url,
1072 struct Session *s = NULL;
1073 struct ServerConnection *sc = NULL;
1074 const union MHD_ConnectionInfo *conn_info;
1075 struct HttpAddress *addr;
1077 struct GNUNET_ATS_Information ats;
1078 struct GNUNET_PeerIdentity target;
1081 int direction = GNUNET_SYSERR;
1084 conn_info = MHD_get_connection_info (mhd_connection,
1085 MHD_CONNECTION_INFO_CLIENT_ADDRESS);
1086 if ((conn_info->client_addr->sa_family != AF_INET) &&
1087 (conn_info->client_addr->sa_family != AF_INET6))
1089 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1090 "New %s connection from %s\n", method, url);
1092 if (GNUNET_SYSERR == server_parse_url (plugin, url, &target, &tag))
1094 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1095 "Invalid url %s\n", url);
1098 if (0 == strcmp (MHD_HTTP_METHOD_PUT, method))
1099 direction = _RECEIVE;
1100 else if (0 == strcmp (MHD_HTTP_METHOD_GET, method))
1104 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1105 "Invalid method %s connection from %s\n", method, url);
1109 plugin->cur_connections++;
1110 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1111 "New %s connection from %s with tag %u (%u of %u)\n",
1113 GNUNET_i2s (&target), tag,
1114 plugin->cur_connections, plugin->max_connections);
1115 /* find duplicate session */
1119 if ((0 == memcmp (&s->target, &target, sizeof (struct GNUNET_PeerIdentity))) &&
1126 if ((_RECEIVE == direction) && (NULL != s->server_recv))
1128 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1129 "Duplicate PUT connection from `%s' tag %u, dismissing new connection\n",
1130 GNUNET_i2s (&target),
1135 if ((_SEND == direction) && (NULL != s->server_send))
1137 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1138 "Duplicate GET connection from `%s' tag %u, dismissing new connection\n",
1139 GNUNET_i2s (&target),
1146 /* create new session */
1148 switch (conn_info->client_addr->sa_family)
1151 addr = http_common_address_from_socket (plugin->protocol, conn_info->client_addr, sizeof (struct sockaddr_in));
1152 addr_len = http_common_address_get_size (addr);
1153 ats = plugin->env->get_address_type (plugin->env->cls, conn_info->client_addr, sizeof (struct sockaddr_in));
1156 addr = http_common_address_from_socket (plugin->protocol, conn_info->client_addr, sizeof (struct sockaddr_in6));
1157 addr_len = http_common_address_get_size (addr);
1158 ats = plugin->env->get_address_type (plugin->env->cls, conn_info->client_addr, sizeof (struct sockaddr_in6));
1161 /* external host name */
1162 ats.type = htonl (GNUNET_ATS_NETWORK_TYPE);
1163 ats.type = htonl (GNUNET_ATS_NET_WAN);
1167 s = GNUNET_new (struct Session);
1168 memcpy (&s->target, &target, sizeof (struct GNUNET_PeerIdentity));
1170 s->address = GNUNET_HELLO_address_allocate (&s->target, PLUGIN_NAME,
1171 addr, addr_len, GNUNET_HELLO_ADDRESS_INFO_INBOUND);
1172 s->ats_address_network_type = ats.value;
1173 s->next_receive = GNUNET_TIME_UNIT_ZERO_ABS;
1175 s->server_recv = NULL;
1176 s->server_send = NULL;
1177 s->session_passed = GNUNET_NO;
1178 s->session_ended = GNUNET_NO;
1179 s->connect_in_progress = GNUNET_YES;
1180 server_start_session_timeout(s);
1181 GNUNET_CONTAINER_DLL_insert (plugin->head, plugin->tail, s);
1183 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1184 "Creating new session %p for peer `%s' connecting from `%s'\n",
1185 s, GNUNET_i2s (&target),
1186 http_common_plugin_address_to_string (NULL,
1189 GNUNET_free_non_null (addr);
1191 sc = GNUNET_new (struct ServerConnection);
1192 if (conn_info->client_addr->sa_family == AF_INET)
1193 sc->mhd_daemon = plugin->server_v4;
1194 if (conn_info->client_addr->sa_family == AF_INET6)
1195 sc->mhd_daemon = plugin->server_v6;
1196 sc->mhd_conn = mhd_connection;
1197 sc->direction = direction;
1198 sc->connected = GNUNET_NO;
1200 if (direction == _SEND)
1201 s->server_send = sc;
1202 if (direction == _RECEIVE)
1203 s->server_recv = sc;
1205 if ((NULL != s->server_send) && (NULL != s->server_recv))
1207 s->connect_in_progress = GNUNET_NO; /* PUT and GET are connected */
1208 plugin->env->session_start (NULL, s->address ,s, NULL, 0);
1211 if ((NULL == s->server_recv) || (NULL == s->server_send))
1213 to = (HTTP_SERVER_NOT_VALIDATED_TIMEOUT.rel_value_us / 1000LL / 1000LL);
1214 MHD_set_connection_option (mhd_connection,
1215 MHD_CONNECTION_OPTION_TIMEOUT, to);
1216 server_reschedule (plugin, sc->mhd_daemon, GNUNET_NO);
1220 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1221 "Session %p for peer `%s' fully connected\n",
1222 s, GNUNET_i2s (&target));
1223 to = (HTTP_SERVER_SESSION_TIMEOUT.rel_value_us / 1000LL / 1000LL);
1224 server_mhd_connection_timeout (plugin, s, to);
1227 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1228 "Setting timeout for %p to %u sec.\n", sc, to);
1234 * Lookup a session for a server connection
1236 * @param plugin the plugin
1237 * @param sc the server connection
1238 * @return the session found or NULL
1240 static struct Session *
1241 server_lookup_session (struct HTTP_Server_Plugin *plugin,
1242 struct ServerConnection * sc)
1246 for (s = plugin->head; NULL != s; s = s->next)
1247 if ((s->server_recv == sc) || (s->server_send == sc))
1254 server_exist_session (struct HTTP_Server_Plugin *plugin,
1257 struct Session * head;
1259 for (head = plugin->head; head != NULL; head = head->next)
1267 * Callback called by MHD when it needs data to send
1269 * @param cls current session
1270 * @param pos position in buffer
1271 * @param buf the buffer to write data to
1272 * @param max max number of bytes available in buffer
1273 * @return bytes written to buffer
1276 server_send_callback (void *cls, uint64_t pos, char *buf, size_t max)
1278 struct Session *s = cls;
1279 ssize_t bytes_read = 0;
1280 struct HTTP_Message *msg;
1283 if (GNUNET_NO == server_exist_session (s->plugin, s))
1289 bytes_read = GNUNET_MIN (msg->size - msg->pos,
1291 memcpy (buf, &msg->buf[msg->pos], bytes_read);
1292 msg->pos += bytes_read;
1294 /* removing message */
1295 if (msg->pos == msg->size)
1297 GNUNET_CONTAINER_DLL_remove (s->msg_head, s->msg_tail, msg);
1298 if (NULL != msg->transmit_cont)
1299 msg->transmit_cont (msg->transmit_cont_cls, &s->target, GNUNET_OK,
1300 msg->size, msg->size + msg->overhead);
1306 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, s->plugin->name,
1307 "Sent %u bytes to peer `%s' with session %p \n", bytes_read, GNUNET_i2s (&s->target), s);
1308 GNUNET_asprintf (&stat_txt, "# bytes currently in %s_server buffers",
1309 s->plugin->protocol);
1310 GNUNET_STATISTICS_update (s->plugin->env->stats,
1311 stat_txt, -bytes_read, GNUNET_NO);
1312 GNUNET_free (stat_txt);
1313 GNUNET_asprintf (&stat_txt, "# bytes transmitted via %s_server",
1314 s->plugin->protocol);
1315 GNUNET_STATISTICS_update (s->plugin->env->stats,
1316 stat_txt, bytes_read, GNUNET_NO);
1317 GNUNET_free (stat_txt);
1324 * Callback called by MessageStreamTokenizer when a message has arrived
1326 * @param cls current session as closure
1327 * @param client client
1328 * @param message the message to be forwarded to transport service
1329 * @return #GNUNET_OK
1332 server_receive_mst_cb (void *cls, void *client,
1333 const struct GNUNET_MessageHeader *message)
1335 struct Session *s = cls;
1336 struct HTTP_Server_Plugin *plugin = s->plugin;
1337 struct GNUNET_ATS_Information atsi;
1338 struct GNUNET_TIME_Relative delay;
1341 if (GNUNET_NO == server_exist_session (s->plugin, s))
1345 atsi.type = htonl (GNUNET_ATS_NETWORK_TYPE);
1346 atsi.value = s->ats_address_network_type;
1347 GNUNET_break (s->ats_address_network_type != ntohl (GNUNET_ATS_NET_UNSPECIFIED));
1349 delay = plugin->env->receive (plugin->env->cls, s->address, s, message);
1350 plugin->env->update_address_metrics (plugin->env->cls, s->address, s, &atsi, 1);
1352 GNUNET_asprintf (&stat_txt, "# bytes received via %s_server", plugin->protocol);
1353 GNUNET_STATISTICS_update (plugin->env->stats,
1354 stat_txt, ntohs (message->size), GNUNET_NO);
1355 GNUNET_free (stat_txt);
1357 s->session_passed = GNUNET_YES;
1358 s->next_receive = GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (), delay);
1359 if (delay.rel_value_us > 0)
1361 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1362 "Peer `%s' address `%s' next read delayed for %s\n",
1363 GNUNET_i2s (&s->target),
1364 http_common_plugin_address_to_string (NULL,
1365 plugin->protocol, s->address->address,
1366 s->address->address_length),
1367 GNUNET_STRINGS_relative_time_to_string (delay,
1370 server_reschedule_session_timeout (s);
1376 * MHD callback for a new incoming connection
1378 * @param cls the plugin handle
1379 * @param mhd_connection the mhd connection
1380 * @param url the requested URL
1381 * @param method GET or PUT
1382 * @param version HTTP version
1383 * @param upload_data upload data
1384 * @param upload_data_size sizeof upload data
1385 * @param httpSessionCache the session cache to remember the connection
1386 * @return MHD_YES if connection is accepted, MHD_NO on reject
1389 server_access_cb (void *cls, struct MHD_Connection *mhd_connection,
1390 const char *url, const char *method, const char *version,
1391 const char *upload_data, size_t * upload_data_size,
1392 void **httpSessionCache)
1394 struct HTTP_Server_Plugin *plugin = cls;
1397 struct ServerConnection *sc = *httpSessionCache;
1399 struct MHD_Response *response;
1401 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1402 _("Access from connection %p (%u of %u) for `%s' `%s' url `%s' with upload data size %u\n"),
1404 plugin->cur_connections, plugin->max_connections,
1405 method, version, url, (*upload_data_size));
1407 GNUNET_assert (cls != NULL);
1410 /* new connection */
1411 sc = server_lookup_connection (plugin, mhd_connection, url, method);
1414 (*httpSessionCache) = sc;
1418 response = MHD_create_response_from_data (strlen (HTTP_ERROR_RESPONSE), HTTP_ERROR_RESPONSE, MHD_NO, MHD_NO);
1419 MHD_add_response_header (response,
1420 MHD_HTTP_HEADER_CONTENT_TYPE,
1422 res = MHD_queue_response (mhd_connection, MHD_HTTP_NOT_FOUND, response);
1423 MHD_destroy_response (response);
1429 /* 'old' connection */
1430 if (NULL == server_lookup_session (plugin, sc))
1432 /* Session was already disconnected */
1437 /* existing connection */
1438 sc = (*httpSessionCache);
1440 GNUNET_assert (NULL != s);
1441 /* connection is to be disconnected */
1442 if (sc->disconnect == GNUNET_YES)
1444 /* Sent HTTP/1.1: 200 OK as response */
1445 response = MHD_create_response_from_data (strlen ("Thank you!"),
1448 MHD_queue_response (mhd_connection, MHD_HTTP_OK, response);
1449 MHD_destroy_response (response);
1452 GNUNET_assert (s != NULL);
1454 if (sc->direction == _SEND)
1456 response = MHD_create_response_from_callback (MHD_SIZE_UNKNOWN,
1458 &server_send_callback, s,
1460 MHD_queue_response (mhd_connection, MHD_HTTP_OK, response);
1461 MHD_destroy_response (response);
1464 if (sc->direction == _RECEIVE)
1466 if ((*upload_data_size == 0) && (sc->connected == GNUNET_NO))
1468 /* (*upload_data_size == 0) first callback when header are passed */
1469 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1470 "Session %p / Connection %p: Peer `%s' PUT on address `%s' connected\n",
1472 GNUNET_i2s (&s->target),
1473 http_common_plugin_address_to_string (NULL,
1474 plugin->protocol, s->address->address,
1475 s->address->address_length));
1476 sc->connected = GNUNET_YES;
1479 else if ((*upload_data_size == 0) && (sc->connected == GNUNET_YES))
1481 /* (*upload_data_size == 0) when upload is complete */
1482 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1483 "Session %p / Connection %p: Peer `%s' PUT on address `%s' finished upload\n",
1485 GNUNET_i2s (&s->target),
1486 http_common_plugin_address_to_string (NULL,
1487 plugin->protocol, s->address->address,
1488 s->address->address_length));
1489 sc->connected = GNUNET_NO;
1490 /* Sent HTTP/1.1: 200 OK as PUT Response\ */
1491 response = MHD_create_response_from_data (strlen ("Thank you!"),
1494 MHD_queue_response (mhd_connection, MHD_HTTP_OK, response);
1495 MHD_destroy_response (response);
1498 else if ((*upload_data_size > 0) && (sc->connected == GNUNET_YES))
1500 /* (*upload_data_size > 0) for every segment received */
1501 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1502 "Session %p / Connection %p: Peer `%s' PUT on address `%s' received %u bytes\n",
1504 GNUNET_i2s (&s->target),
1505 http_common_plugin_address_to_string (NULL,
1506 plugin->protocol, s->address->address,
1507 s->address->address_length),
1509 struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get ();
1511 if ((s->next_receive.abs_value_us <= now.abs_value_us))
1513 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1514 "PUT with %u bytes forwarded to MST\n",
1516 if (s->msg_tk == NULL)
1518 s->msg_tk = GNUNET_SERVER_mst_create (&server_receive_mst_cb, s);
1520 GNUNET_SERVER_mst_receive (s->msg_tk, s, upload_data,
1521 *upload_data_size, GNUNET_NO, GNUNET_NO);
1522 server_mhd_connection_timeout (plugin, s,
1523 GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT.rel_value_us / 1000LL / 1000LL);
1524 (*upload_data_size) = 0;
1528 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1529 "Session %p / Connection %p: no inbound bandwidth available! Next read was delayed by %s\n",
1531 GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_duration (s->next_receive),
1547 * Callback from MHD when a connection disconnects
1549 * @param cls closure with the `struct HTTP_Server_Plugin *`
1550 * @param connection the disconnected MHD connection
1551 * @param httpSessionCache the pointer to distinguish
1554 server_disconnect_cb (void *cls, struct MHD_Connection *connection,
1555 void **httpSessionCache)
1557 struct HTTP_Server_Plugin *plugin = cls;
1558 struct ServerConnection *sc = *httpSessionCache;
1559 struct Session *s = NULL;
1560 struct Session *t = NULL;
1562 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1564 "Disconnect for connection %p \n", sc);
1569 if (NULL == (s = server_lookup_session (plugin, sc)))
1571 for (t = plugin->head; t != NULL; t = t->next)
1577 if (sc->direction == _SEND)
1579 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1580 "Peer `%s' connection %p, GET on address `%s' disconnected\n",
1581 GNUNET_i2s (&s->target), s->server_send,
1582 http_common_plugin_address_to_string (NULL,
1583 plugin->protocol, s->address->address,
1584 s->address->address_length));
1585 s->server_send = NULL;
1586 if (NULL != (s->server_recv))
1588 s->server_recv->disconnect = GNUNET_YES;
1589 GNUNET_assert (NULL != s->server_recv->mhd_conn);
1590 #if MHD_VERSION >= 0x00090E00
1591 MHD_set_connection_option (s->server_recv->mhd_conn, MHD_CONNECTION_OPTION_TIMEOUT,
1594 server_reschedule (plugin, s->server_recv->mhd_daemon, GNUNET_NO);
1597 if (sc->direction == _RECEIVE)
1599 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1600 "Peer `%s' connection %p PUT on address `%s' disconnected\n",
1601 GNUNET_i2s (&s->target), s->server_recv,
1602 http_common_plugin_address_to_string (NULL,
1603 plugin->protocol, s->address->address,
1604 s->address->address_length));
1605 s->server_recv = NULL;
1606 /* Do not terminate session when PUT disconnects
1607 if (NULL != (s->server_send))
1609 s->server_send->disconnect = GNUNET_YES;
1610 GNUNET_assert (NULL != s->server_send->mhd_conn);
1611 #if MHD_VERSION >= 0x00090E00
1612 MHD_set_connection_option (s->server_send->mhd_conn, MHD_CONNECTION_OPTION_TIMEOUT,
1615 server_reschedule (plugin, s->server_send->mhd_daemon, GNUNET_NO);
1617 if (s->msg_tk != NULL)
1619 GNUNET_SERVER_mst_destroy (s->msg_tk);
1625 plugin->cur_connections--;
1627 if ((s->server_send == NULL) && (s->server_recv == NULL))
1629 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1630 "Peer `%s' on address `%s' disconnected\n",
1631 GNUNET_i2s (&s->target),
1632 http_common_plugin_address_to_string (NULL,
1633 plugin->protocol, s->address->address,
1634 s->address->address_length));
1636 if ((GNUNET_YES == s->session_passed) && (GNUNET_NO == s->session_ended))
1638 /* Notify transport immediately that this session is invalid */
1639 s->session_ended = GNUNET_YES;
1640 plugin->env->session_end (plugin->env->cls, s->address, s);
1642 server_delete_session (plugin, s);
1648 * Check if incoming connection is accepted.
1650 * @param cls plugin as closure
1651 * @param addr address of incoming connection
1652 * @param addr_len address length of incoming connection
1653 * @return MHD_YES if connection is accepted, MHD_NO if connection is rejected
1656 server_accept_cb (void *cls, const struct sockaddr *addr, socklen_t addr_len)
1658 struct HTTP_Server_Plugin *plugin = cls;
1660 if (plugin->cur_connections <= plugin->max_connections)
1662 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1663 _("Accepting connection (%u of %u) from `%s'\n"),
1664 plugin->cur_connections, plugin->max_connections,
1665 GNUNET_a2s (addr, addr_len));
1670 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, plugin->name,
1671 _("Server reached maximum number connections (%u), rejecting new connection\n"),
1672 plugin->max_connections);
1679 server_log (void *arg, const char *fmt, va_list ap)
1683 vsnprintf (text, sizeof (text), fmt, ap);
1685 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Server: %s\n", text);
1690 * Call MHD IPv4 to process pending requests and then go back
1691 * and schedule the next run.
1692 * @param cls plugin as closure
1693 * @param tc task context
1696 server_v4_run (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1698 struct HTTP_Server_Plugin *plugin = cls;
1700 GNUNET_assert (cls != NULL);
1702 plugin->server_v4_task = GNUNET_SCHEDULER_NO_TASK;
1703 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1706 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1707 "Running IPv4 server\n");
1709 plugin->server_v4_immediately = GNUNET_NO;
1710 GNUNET_assert (MHD_YES == MHD_run (plugin->server_v4));
1711 server_reschedule (plugin, plugin->server_v4, GNUNET_NO);
1716 * Call MHD IPv6 to process pending requests and then go back
1717 * and schedule the next run.
1718 * @param cls plugin as closure
1719 * @param tc task context
1722 server_v6_run (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1724 struct HTTP_Server_Plugin *plugin = cls;
1726 GNUNET_assert (cls != NULL);
1727 plugin->server_v6_task = GNUNET_SCHEDULER_NO_TASK;
1728 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1731 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1732 "Running IPv6 server\n");
1734 plugin->server_v6_immediately = GNUNET_NO;
1735 GNUNET_assert (MHD_YES == MHD_run (plugin->server_v6));
1736 server_reschedule (plugin, plugin->server_v6, GNUNET_NO);
1741 * Function that queries MHD's select sets and
1742 * starts the task waiting for them.
1744 * @param plugin plugin
1745 * @param daemon_handle the MHD daemon handle
1746 * @return gnunet task identifier
1748 static GNUNET_SCHEDULER_TaskIdentifier
1749 server_schedule (struct HTTP_Server_Plugin *plugin,
1750 struct MHD_Daemon *daemon_handle,
1753 GNUNET_SCHEDULER_TaskIdentifier ret;
1757 struct GNUNET_NETWORK_FDSet *wrs;
1758 struct GNUNET_NETWORK_FDSet *wws;
1759 struct GNUNET_NETWORK_FDSet *wes;
1761 MHD_UNSIGNED_LONG_LONG timeout;
1762 static unsigned long long last_timeout = 0;
1765 struct GNUNET_TIME_Relative tv;
1767 if (GNUNET_YES == plugin->in_shutdown)
1768 return GNUNET_SCHEDULER_NO_TASK;
1770 ret = GNUNET_SCHEDULER_NO_TASK;
1774 wrs = GNUNET_NETWORK_fdset_create ();
1775 wes = GNUNET_NETWORK_fdset_create ();
1776 wws = GNUNET_NETWORK_fdset_create ();
1778 GNUNET_assert (MHD_YES == MHD_get_fdset (daemon_handle, &rs, &ws, &es, &max));
1779 haveto = MHD_get_timeout (daemon_handle, &timeout);
1780 if (haveto == MHD_YES)
1782 if (timeout != last_timeout)
1785 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1786 "SELECT Timeout changed from %llu to %llu (ms)\n",
1787 last_timeout, timeout);
1788 last_timeout = timeout;
1790 if (timeout <= GNUNET_TIME_UNIT_SECONDS.rel_value_us / 1000LL)
1791 tv.rel_value_us = (uint64_t) timeout * 1000LL;
1793 tv = GNUNET_TIME_UNIT_SECONDS;
1796 tv = GNUNET_TIME_UNIT_SECONDS;
1797 /* Force immediate run, since we have outbound data to send */
1798 if (now == GNUNET_YES)
1799 tv = GNUNET_TIME_UNIT_MILLISECONDS;
1800 GNUNET_NETWORK_fdset_copy_native (wrs, &rs, max + 1);
1801 GNUNET_NETWORK_fdset_copy_native (wws, &ws, max + 1);
1802 GNUNET_NETWORK_fdset_copy_native (wes, &es, max + 1);
1804 if (daemon_handle == plugin->server_v4)
1806 if (plugin->server_v4_task != GNUNET_SCHEDULER_NO_TASK)
1808 GNUNET_SCHEDULER_cancel (plugin->server_v4_task);
1809 plugin->server_v4_task = GNUNET_SCHEDULER_NO_TASK;
1812 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1813 "Scheduling IPv4 server task in %llu ms\n", tv);
1816 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1818 &server_v4_run, plugin);
1820 if (daemon_handle == plugin->server_v6)
1822 if (plugin->server_v6_task != GNUNET_SCHEDULER_NO_TASK)
1824 GNUNET_SCHEDULER_cancel (plugin->server_v6_task);
1825 plugin->server_v6_task = GNUNET_SCHEDULER_NO_TASK;
1828 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1829 "Scheduling IPv6 server task in %llu ms\n", tv);
1832 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1834 &server_v6_run, plugin);
1836 GNUNET_NETWORK_fdset_destroy (wrs);
1837 GNUNET_NETWORK_fdset_destroy (wws);
1838 GNUNET_NETWORK_fdset_destroy (wes);
1845 * Load ssl certificate from file
1847 * @param file filename
1848 * @return content of the file
1851 server_load_file (const char *file)
1853 struct GNUNET_DISK_FileHandle *gn_file;
1857 if (GNUNET_OK != GNUNET_DISK_file_size (file,
1858 &fsize, GNUNET_NO, GNUNET_YES))
1860 text = GNUNET_malloc (fsize + 1);
1862 GNUNET_DISK_file_open (file, GNUNET_DISK_OPEN_READ,
1863 GNUNET_DISK_PERM_USER_READ);
1864 if (gn_file == NULL)
1869 if (GNUNET_SYSERR == GNUNET_DISK_file_read (gn_file, text, fsize))
1872 GNUNET_DISK_file_close (gn_file);
1876 GNUNET_DISK_file_close (gn_file);
1884 * Load ssl certificate
1886 * @param plugin the plugin
1887 * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
1890 server_load_certificate (struct HTTP_Server_Plugin *plugin)
1892 int res = GNUNET_OK;
1898 GNUNET_CONFIGURATION_get_value_filename (plugin->env->cfg, plugin->name,
1899 "KEY_FILE", &key_file))
1901 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
1902 plugin->name, "CERT_FILE");
1903 return GNUNET_SYSERR;
1906 GNUNET_CONFIGURATION_get_value_filename (plugin->env->cfg, plugin->name,
1907 "CERT_FILE", &cert_file))
1909 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
1910 plugin->name, "CERT_FILE");
1911 GNUNET_free (key_file);
1912 return GNUNET_SYSERR;
1914 /* Get crypto init string from config. If not present, use
1917 GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg,
1920 &plugin->crypto_init))
1921 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1922 "Using crypto init string `%s'\n",
1923 plugin->crypto_init);
1925 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1926 "Using default crypto init string \n");
1928 /* read key & certificates from file */
1929 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1930 "Trying to loading TLS certificate from key-file `%s' cert-file`%s'\n",
1931 key_file, cert_file);
1933 plugin->key = server_load_file (key_file);
1934 plugin->cert = server_load_file (cert_file);
1936 if ((plugin->key == NULL) || (plugin->cert == NULL))
1938 struct GNUNET_OS_Process *cert_creation;
1940 GNUNET_free_non_null (plugin->key);
1942 GNUNET_free_non_null (plugin->cert);
1943 plugin->cert = NULL;
1945 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1946 "No usable TLS certificate found, creating certificate\n");
1949 GNUNET_OS_start_process (GNUNET_NO, GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
1951 "gnunet-transport-certificate-creation",
1952 "gnunet-transport-certificate-creation",
1953 key_file, cert_file, NULL);
1954 if (cert_creation == NULL)
1956 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
1958 ("Could not create a new TLS certificate, program `gnunet-transport-certificate-creation' could not be started!\n"));
1959 GNUNET_free (key_file);
1960 GNUNET_free (cert_file);
1962 GNUNET_free_non_null (plugin->key);
1964 GNUNET_free_non_null (plugin->cert);
1965 plugin->cert = NULL;
1966 GNUNET_free_non_null (plugin->crypto_init);
1967 plugin->crypto_init = NULL;
1969 return GNUNET_SYSERR;
1971 GNUNET_assert (GNUNET_OK == GNUNET_OS_process_wait (cert_creation));
1972 GNUNET_OS_process_destroy (cert_creation);
1974 plugin->key = server_load_file (key_file);
1975 plugin->cert = server_load_file (cert_file);
1978 if ((plugin->key == NULL) || (plugin->cert == NULL))
1980 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
1982 _("No usable TLS certificate found and creating one at `%s/%s' failed!\n"),
1983 key_file, cert_file);
1984 GNUNET_free (key_file);
1985 GNUNET_free (cert_file);
1987 GNUNET_free_non_null (plugin->key);
1989 GNUNET_free_non_null (plugin->cert);
1990 plugin->cert = NULL;
1991 GNUNET_free_non_null (plugin->crypto_init);
1992 plugin->crypto_init = NULL;
1994 return GNUNET_SYSERR;
1996 GNUNET_free (key_file);
1997 GNUNET_free (cert_file);
1998 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "TLS certificate loaded\n");
2005 * Start the HTTP server
2007 * @param plugin the plugin handle
2008 * @return GNUNET_OK on success, GNUNET_SYSERR on failure
2011 server_start (struct HTTP_Server_Plugin *plugin)
2013 unsigned int timeout;
2015 GNUNET_assert (NULL != plugin);
2018 if (GNUNET_SYSERR == server_load_certificate (plugin))
2020 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
2021 "Could not load or create server certificate! Loading plugin failed!\n");
2022 return GNUNET_SYSERR;
2027 #if MHD_VERSION >= 0x00090E00
2028 timeout = HTTP_SERVER_NOT_VALIDATED_TIMEOUT.rel_value_us / 1000LL / 1000LL;
2029 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2030 "MHD can set timeout per connection! Default time out %u sec.\n",
2033 timeout = HTTP_SERVER_SESSION_TIMEOUT.rel_value_us / 1000LL / 1000LL;
2034 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, plugin->name,
2035 "MHD cannot set timeout per connection! Default time out %u sec.\n",
2039 plugin->server_v4 = NULL;
2040 if (plugin->use_ipv4 == GNUNET_YES)
2042 plugin->server_v4 = MHD_start_daemon (
2049 MHD_NO_FLAG, plugin->port,
2050 &server_accept_cb, plugin,
2051 &server_access_cb, plugin,
2052 MHD_OPTION_SOCK_ADDR,
2053 (struct sockaddr_in *)
2054 plugin->server_addr_v4,
2055 MHD_OPTION_CONNECTION_LIMIT,
2057 plugin->max_connections,
2059 MHD_OPTION_HTTPS_PRIORITIES,
2060 plugin->crypto_init,
2061 MHD_OPTION_HTTPS_MEM_KEY,
2063 MHD_OPTION_HTTPS_MEM_CERT,
2066 MHD_OPTION_CONNECTION_TIMEOUT,
2068 MHD_OPTION_CONNECTION_MEMORY_LIMIT,
2070 GNUNET_SERVER_MAX_MESSAGE_SIZE),
2071 MHD_OPTION_NOTIFY_COMPLETED,
2072 &server_disconnect_cb, plugin,
2073 MHD_OPTION_EXTERNAL_LOGGER,
2074 server_log, NULL, MHD_OPTION_END);
2075 if (plugin->server_v4 == NULL)
2077 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
2078 "Failed to start %s IPv4 server component on port %u\n",
2079 plugin->name, plugin->port);
2082 server_reschedule (plugin, plugin->server_v4, GNUNET_NO);
2086 plugin->server_v6 = NULL;
2087 if (plugin->use_ipv6 == GNUNET_YES)
2089 plugin->server_v6 = MHD_start_daemon (
2096 MHD_USE_IPv6, plugin->port,
2097 &server_accept_cb, plugin,
2098 &server_access_cb, plugin,
2099 MHD_OPTION_SOCK_ADDR,
2100 (struct sockaddr_in6 *)
2101 plugin->server_addr_v6,
2102 MHD_OPTION_CONNECTION_LIMIT,
2104 plugin->max_connections,
2106 MHD_OPTION_HTTPS_PRIORITIES,
2107 plugin->crypto_init,
2108 MHD_OPTION_HTTPS_MEM_KEY,
2110 MHD_OPTION_HTTPS_MEM_CERT,
2113 MHD_OPTION_CONNECTION_TIMEOUT,
2115 MHD_OPTION_CONNECTION_MEMORY_LIMIT,
2117 GNUNET_SERVER_MAX_MESSAGE_SIZE),
2118 MHD_OPTION_NOTIFY_COMPLETED,
2119 &server_disconnect_cb, plugin,
2120 MHD_OPTION_EXTERNAL_LOGGER,
2121 server_log, NULL, MHD_OPTION_END);
2122 if (plugin->server_v6 == NULL)
2124 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
2125 "Failed to start %s IPv6 server component on port %u\n",
2126 plugin->name, plugin->port);
2129 server_reschedule (plugin, plugin->server_v6, GNUNET_NO);
2133 if ((plugin->server_v6 == NULL) && (plugin->server_v4 == NULL))
2135 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
2136 "%s %s server component started on port %u\n",
2137 msg, plugin->name, plugin->port);
2139 return GNUNET_SYSERR;
2141 else if ((plugin->server_v6 != NULL) && (plugin->server_v4 != NULL))
2142 msg = "IPv4 and IPv6";
2143 else if (plugin->server_v6 != NULL)
2145 else if (plugin->server_v4 != NULL)
2147 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2148 "%s %s server component started on port %u\n",
2149 msg, plugin->name, plugin->port);
2155 server_stop (struct HTTP_Server_Plugin *plugin)
2157 if (plugin->server_v4 != NULL)
2159 MHD_stop_daemon (plugin->server_v4);
2160 plugin->server_v4 = NULL;
2162 if ( plugin->server_v6 != NULL)
2164 MHD_stop_daemon (plugin->server_v6);
2165 plugin->server_v6 = NULL;
2169 if (plugin->server_v4_task != GNUNET_SCHEDULER_NO_TASK)
2171 GNUNET_SCHEDULER_cancel (plugin->server_v4_task);
2172 plugin->server_v4_task = GNUNET_SCHEDULER_NO_TASK;
2175 if (plugin->server_v6_task != GNUNET_SCHEDULER_NO_TASK)
2177 GNUNET_SCHEDULER_cancel (plugin->server_v6_task);
2178 plugin->server_v6_task = GNUNET_SCHEDULER_NO_TASK;
2181 GNUNET_free_non_null (plugin->crypto_init);
2182 GNUNET_free_non_null (plugin->cert);
2183 GNUNET_free_non_null (plugin->key);
2186 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2187 "%s server component stopped\n", plugin->name);
2192 * Add an address to the server's set of addresses and notify transport
2194 * @param cls the plugin handle
2195 * @param add_remove GNUNET_YES on add, GNUNET_NO on remove
2196 * @param addr the address
2197 * @param addrlen address length
2200 server_add_address (void *cls, int add_remove, const struct sockaddr *addr,
2203 struct HTTP_Server_Plugin *plugin = cls;
2204 struct GNUNET_HELLO_Address *address;
2205 struct HttpAddressWrapper *w = NULL;
2207 w = GNUNET_new (struct HttpAddressWrapper);
2208 w->address = http_common_address_from_socket (plugin->protocol, addr, addrlen);
2209 if (NULL == w->address)
2214 w->addrlen = http_common_address_get_size (w->address);
2216 GNUNET_CONTAINER_DLL_insert(plugin->addr_head, plugin->addr_tail, w);
2217 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2218 "Notifying transport to add address `%s'\n",
2219 http_common_plugin_address_to_string (NULL,
2221 w->address, w->addrlen));
2222 /* modify our published address list */
2224 address = GNUNET_HELLO_address_allocate (plugin->env->my_identity,
2225 "https_client", w->address, w->addrlen, GNUNET_HELLO_ADDRESS_INFO_NONE);
2227 address = GNUNET_HELLO_address_allocate (plugin->env->my_identity,
2228 "http_client", w->address, w->addrlen, GNUNET_HELLO_ADDRESS_INFO_NONE);
2231 plugin->env->notify_address (plugin->env->cls, add_remove, address);
2232 GNUNET_HELLO_address_free (address);
2237 * Remove an address from the server's set of addresses and notify transport
2239 * @param cls the plugin handle
2240 * @param add_remove GNUNET_YES on add, GNUNET_NO on remove
2241 * @param addr the address
2242 * @param addrlen address length
2245 server_remove_address (void *cls, int add_remove, const struct sockaddr *addr,
2248 struct HTTP_Server_Plugin *plugin = cls;
2249 struct GNUNET_HELLO_Address *address;
2250 struct HttpAddressWrapper *w = plugin->addr_head;
2252 void * saddr = http_common_address_from_socket (plugin->protocol, addr, addrlen);
2255 saddr_len = http_common_address_get_size (saddr);
2259 if (GNUNET_YES == http_common_cmp_addresses(w->address, w->addrlen, saddr, saddr_len))
2263 GNUNET_free (saddr);
2268 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2269 "Notifying transport to remove address `%s'\n",
2270 http_common_plugin_address_to_string (NULL,
2272 w->address, w->addrlen));
2275 GNUNET_CONTAINER_DLL_remove (plugin->addr_head, plugin->addr_tail, w);
2277 /* modify our published address list */
2279 address = GNUNET_HELLO_address_allocate (plugin->env->my_identity,
2280 "https_client", w->address, w->addrlen, GNUNET_HELLO_ADDRESS_INFO_NONE);
2282 address = GNUNET_HELLO_address_allocate (plugin->env->my_identity,
2283 "http_client", w->address, w->addrlen, GNUNET_HELLO_ADDRESS_INFO_NONE);
2285 plugin->env->notify_address (plugin->env->cls, add_remove, address);
2286 GNUNET_HELLO_address_free (address);
2287 GNUNET_free (w->address);
2294 * Our external IP address/port mapping has changed.
2296 * @param cls closure, the 'struct LocalAddrList'
2297 * @param add_remove GNUNET_YES to mean the new public IP address, GNUNET_NO to mean
2298 * the previous (now invalid) one
2299 * @param addr either the previous or the new public IP address
2300 * @param addrlen actual lenght of the address
2303 server_nat_port_map_callback (void *cls, int add_remove, const struct sockaddr *addr,
2306 GNUNET_assert (cls != NULL);
2307 struct HTTP_Server_Plugin *plugin = cls;
2309 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2310 "NAT called to %s address `%s'\n",
2311 (add_remove == GNUNET_NO) ? "remove" : "add",
2312 GNUNET_a2s (addr, addrlen));
2314 if (AF_INET == addr->sa_family)
2316 struct sockaddr_in *s4 = (struct sockaddr_in *) addr;
2318 if (GNUNET_NO == plugin->use_ipv4)
2321 if ((NULL != plugin->server_addr_v4) &&
2322 (0 != memcmp (&plugin->server_addr_v4->sin_addr,
2323 &s4->sin_addr, sizeof (struct in_addr))))
2325 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2326 "Skipping address `%s' (not bindto address)\n",
2327 GNUNET_a2s (addr, addrlen));
2332 if (AF_INET6 == addr->sa_family)
2334 struct sockaddr_in6 *s6 = (struct sockaddr_in6 *) addr;
2335 if (GNUNET_NO == plugin->use_ipv6)
2338 if ((NULL != plugin->server_addr_v6) &&
2339 (0 != memcmp (&plugin->server_addr_v6->sin6_addr,
2340 &s6->sin6_addr, sizeof (struct in6_addr))))
2342 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2343 "Skipping address `%s' (not bindto address)\n",
2344 GNUNET_a2s (addr, addrlen));
2352 server_add_address (cls, add_remove, addr, addrlen);
2355 server_remove_address (cls, add_remove, addr, addrlen);
2362 * Get valid server addresses
2364 * @param plugin the plugin handle
2365 * @param service_name the servicename
2366 * @param cfg configuration handle
2367 * @param addrs addresses
2368 * @param addr_lens address length
2369 * @return number of addresses
2372 server_get_addresses (struct HTTP_Server_Plugin *plugin,
2373 const char *service_name,
2374 const struct GNUNET_CONFIGURATION_Handle *cfg,
2375 struct sockaddr ***addrs, socklen_t ** addr_lens)
2378 unsigned long long port;
2379 struct addrinfo hints;
2380 struct addrinfo *res;
2381 struct addrinfo *pos;
2382 struct addrinfo *next;
2386 struct sockaddr **saddrs;
2387 socklen_t *saddrlens;
2393 disablev6 = !plugin->use_ipv6;
2396 if (GNUNET_CONFIGURATION_have_value (cfg, service_name, "PORT"))
2398 GNUNET_break (GNUNET_OK ==
2399 GNUNET_CONFIGURATION_get_value_number (cfg, service_name,
2403 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2405 ("Require valid port number for service in configuration!\n"));
2406 return GNUNET_SYSERR;
2411 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, plugin->name,
2412 "Starting in listen only mode\n");
2413 return -1; /* listen only */
2417 if (GNUNET_CONFIGURATION_have_value (cfg, service_name, "BINDTO"))
2419 GNUNET_break (GNUNET_OK ==
2420 GNUNET_CONFIGURATION_get_value_string (cfg, service_name,
2421 "BINDTO", &hostname));
2426 if (hostname != NULL)
2428 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2429 "Resolving `%s' since that is where `%s' will bind to.\n",
2430 hostname, service_name);
2431 memset (&hints, 0, sizeof (struct addrinfo));
2433 hints.ai_family = AF_INET;
2434 if ((0 != (ret = getaddrinfo (hostname, NULL, &hints, &res))) ||
2437 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Failed to resolve `%s': %s\n"),
2438 hostname, gai_strerror (ret));
2439 GNUNET_free (hostname);
2440 return GNUNET_SYSERR;
2444 while (NULL != (pos = next))
2446 next = pos->ai_next;
2447 if ((disablev6) && (pos->ai_family == AF_INET6))
2453 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2454 _("Failed to find %saddress for `%s'.\n"),
2455 disablev6 ? "IPv4 " : "", hostname);
2457 GNUNET_free (hostname);
2458 return GNUNET_SYSERR;
2461 saddrs = GNUNET_malloc ((resi + 1) * sizeof (struct sockaddr *));
2462 saddrlens = GNUNET_malloc ((resi + 1) * sizeof (socklen_t));
2465 while (NULL != (pos = next))
2467 next = pos->ai_next;
2468 if ((disablev6) && (pos->ai_family == AF_INET6))
2470 if ((pos->ai_protocol != IPPROTO_TCP) && (pos->ai_protocol != 0))
2471 continue; /* not TCP */
2472 if ((pos->ai_socktype != SOCK_STREAM) && (pos->ai_socktype != 0))
2473 continue; /* huh? */
2474 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2475 "Service will bind to `%s'\n", GNUNET_a2s (pos->ai_addr,
2477 if (pos->ai_family == AF_INET)
2479 GNUNET_assert (pos->ai_addrlen == sizeof (struct sockaddr_in));
2480 saddrlens[i] = pos->ai_addrlen;
2481 saddrs[i] = GNUNET_malloc (saddrlens[i]);
2482 memcpy (saddrs[i], pos->ai_addr, saddrlens[i]);
2483 ((struct sockaddr_in *) saddrs[i])->sin_port = htons (port);
2487 GNUNET_assert (pos->ai_family == AF_INET6);
2488 GNUNET_assert (pos->ai_addrlen == sizeof (struct sockaddr_in6));
2489 saddrlens[i] = pos->ai_addrlen;
2490 saddrs[i] = GNUNET_malloc (saddrlens[i]);
2491 memcpy (saddrs[i], pos->ai_addr, saddrlens[i]);
2492 ((struct sockaddr_in6 *) saddrs[i])->sin6_port = htons (port);
2496 GNUNET_free (hostname);
2502 /* will bind against everything, just set port */
2508 saddrs = GNUNET_malloc ((resi + 1) * sizeof (struct sockaddr *));
2509 saddrlens = GNUNET_malloc ((resi + 1) * sizeof (socklen_t));
2511 saddrlens[i] = sizeof (struct sockaddr_in);
2512 saddrs[i] = GNUNET_malloc (saddrlens[i]);
2513 #if HAVE_SOCKADDR_IN_SIN_LEN
2514 ((struct sockaddr_in *) saddrs[i])->sin_len = saddrlens[i];
2516 ((struct sockaddr_in *) saddrs[i])->sin_family = AF_INET;
2517 ((struct sockaddr_in *) saddrs[i])->sin_port = htons (port);
2523 saddrs = GNUNET_malloc ((resi + 1) * sizeof (struct sockaddr *));
2524 saddrlens = GNUNET_malloc ((resi + 1) * sizeof (socklen_t));
2526 saddrlens[i] = sizeof (struct sockaddr_in6);
2527 saddrs[i] = GNUNET_malloc (saddrlens[i]);
2528 #if HAVE_SOCKADDR_IN_SIN_LEN
2529 ((struct sockaddr_in6 *) saddrs[i])->sin6_len = saddrlens[0];
2531 ((struct sockaddr_in6 *) saddrs[i])->sin6_family = AF_INET6;
2532 ((struct sockaddr_in6 *) saddrs[i])->sin6_port = htons (port);
2534 saddrlens[i] = sizeof (struct sockaddr_in);
2535 saddrs[i] = GNUNET_malloc (saddrlens[i]);
2536 #if HAVE_SOCKADDR_IN_SIN_LEN
2537 ((struct sockaddr_in *) saddrs[i])->sin_len = saddrlens[1];
2539 ((struct sockaddr_in *) saddrs[i])->sin_family = AF_INET;
2540 ((struct sockaddr_in *) saddrs[i])->sin_port = htons (port);
2544 *addr_lens = saddrlens;
2550 * Ask NAT for addresses
2552 * @param plugin the plugin handle
2555 server_start_report_addresses (struct HTTP_Server_Plugin *plugin)
2557 int res = GNUNET_OK;
2558 struct sockaddr **addrs;
2559 socklen_t *addrlens;
2561 res = server_get_addresses (plugin,
2562 plugin->name, plugin->env->cfg,
2564 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2565 _("Found %u addresses to report to NAT service\n"), res);
2567 if (GNUNET_SYSERR == res)
2574 GNUNET_NAT_register (plugin->env->cfg, GNUNET_YES, plugin->port,
2576 (const struct sockaddr **) addrs, addrlens,
2577 &server_nat_port_map_callback, NULL, plugin);
2581 GNUNET_assert (addrs[res] != NULL);
2582 GNUNET_free (addrs[res]);
2584 GNUNET_free_non_null (addrs);
2585 GNUNET_free_non_null (addrlens);
2590 * Stop NAT for addresses
2592 * @param plugin the plugin handle
2595 server_stop_report_addresses (struct HTTP_Server_Plugin *plugin)
2597 /* Stop NAT handle */
2598 if (NULL != plugin->nat)
2599 GNUNET_NAT_unregister (plugin->nat);
2601 /* Clean up addresses */
2602 struct HttpAddressWrapper *w;
2604 while (plugin->addr_head != NULL)
2606 w = plugin->addr_head;
2607 GNUNET_CONTAINER_DLL_remove (plugin->addr_head, plugin->addr_tail, w);
2608 GNUNET_free (w->address);
2615 * Check if IPv6 supported on this system
2617 * @param plugin the plugin handle
2618 * @return GNUNET_YES on success, else GNUNET_NO
2621 server_check_ipv6_support (struct HTTP_Server_Plugin *plugin)
2623 struct GNUNET_NETWORK_Handle *desc = NULL;
2624 int res = GNUNET_NO;
2626 /* Probe IPv6 support */
2627 desc = GNUNET_NETWORK_socket_create (PF_INET6, SOCK_STREAM, 0);
2630 if ((errno == ENOBUFS) || (errno == ENOMEM) || (errno == ENFILE) ||
2633 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "socket");
2635 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, plugin->name,
2637 ("Disabling IPv6 since it is not supported on this system!\n"));
2642 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (desc));
2646 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2647 "Testing IPv6 on this system: %s\n",
2648 (res == GNUNET_YES) ? "successful" : "failed");
2654 * Notify server about our external hostname
2657 * @param tc task context (unused)
2660 server_notify_external_hostname (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2662 struct HTTP_Server_Plugin *plugin = cls;
2663 struct HttpAddress *ext_addr;
2664 size_t ext_addr_len;
2668 plugin->notify_ext_task = GNUNET_SCHEDULER_NO_TASK;
2669 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
2672 GNUNET_asprintf(&url, "%s://%s", plugin->protocol, plugin->external_hostname);
2674 urlen = strlen (url) + 1;
2675 ext_addr = GNUNET_malloc (sizeof (struct HttpAddress) + urlen);
2676 ext_addr->options = htonl(plugin->options);
2677 ext_addr->urlen = htonl (urlen);
2678 ext_addr_len = sizeof (struct HttpAddress) + urlen;
2679 memcpy (&ext_addr[1], url, urlen);
2682 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2683 "Notifying transport about external hostname address `%s'\n", plugin->external_hostname);
2686 if (GNUNET_YES == plugin->verify_external_hostname)
2687 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, plugin->name,
2688 "Enabling SSL verification for external hostname address `%s'\n",
2689 plugin->external_hostname);
2690 plugin->ext_addr = GNUNET_HELLO_address_allocate (plugin->env->my_identity,
2691 "https_client", ext_addr, ext_addr_len, GNUNET_HELLO_ADDRESS_INFO_NONE );
2692 plugin->env->notify_address (plugin->env->cls, GNUNET_YES, plugin->ext_addr);
2693 GNUNET_free (ext_addr);
2695 plugin->ext_addr = GNUNET_HELLO_address_allocate (plugin->env->my_identity,
2696 "http_client", ext_addr, ext_addr_len, GNUNET_HELLO_ADDRESS_INFO_NONE );
2697 plugin->env->notify_address (plugin->env->cls, GNUNET_YES, plugin->ext_addr);
2698 GNUNET_free (ext_addr);
2704 * Configure the plugin
2706 * @param plugin plugin handle
2707 * @return GNUNET_OK on success, GNUNET_SYSERR on failure
2710 server_configure_plugin (struct HTTP_Server_Plugin *plugin)
2712 unsigned long long port;
2713 unsigned long long max_connections;
2714 char *bind4_address = NULL;
2715 char *bind6_address = NULL;
2716 char *eh_tmp = NULL;
2717 int external_hostname_use_port;
2720 if (GNUNET_CONFIGURATION_have_value
2721 (plugin->env->cfg, plugin->name, "USE_IPv4"))
2724 GNUNET_CONFIGURATION_get_value_yesno (plugin->env->cfg, plugin->name,
2728 plugin->use_ipv4 = GNUNET_YES;
2729 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2730 _("IPv4 support is %s\n"),
2731 (plugin->use_ipv4 == GNUNET_YES) ? "enabled" : "disabled");
2734 if (GNUNET_CONFIGURATION_have_value
2735 (plugin->env->cfg, plugin->name, "USE_IPv6"))
2738 GNUNET_CONFIGURATION_get_value_yesno (plugin->env->cfg, plugin->name,
2742 plugin->use_ipv6 = GNUNET_YES;
2743 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2744 _("IPv6 support is %s\n"),
2745 (plugin->use_ipv6 == GNUNET_YES) ? "enabled" : "disabled");
2747 if ((plugin->use_ipv4 == GNUNET_NO) && (plugin->use_ipv6 == GNUNET_NO))
2749 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
2751 ("Neither IPv4 nor IPv6 are enabled! Fix in configuration\n"),
2753 return GNUNET_SYSERR;
2756 /* Reading port number from config file */
2758 GNUNET_CONFIGURATION_get_value_number (plugin->env->cfg, plugin->name,
2759 "PORT", &port)) || (port > 65535))
2761 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
2762 _("Port is required! Fix in configuration\n"),
2764 return GNUNET_SYSERR;
2766 plugin->port = port;
2768 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, plugin->name,
2769 _("Using port %u\n"), plugin->port);
2771 if ((plugin->use_ipv4 == GNUNET_YES) &&
2772 (GNUNET_YES == GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg,
2773 plugin->name, "BINDTO", &bind4_address)))
2775 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2776 "Binding %s plugin to specific IPv4 address: `%s'\n",
2777 plugin->protocol, bind4_address);
2778 plugin->server_addr_v4 = GNUNET_new (struct sockaddr_in);
2779 if (1 != inet_pton (AF_INET, bind4_address,
2780 &plugin->server_addr_v4->sin_addr))
2782 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
2784 ("Specific IPv4 address `%s' in configuration file is invalid!\n"),
2786 GNUNET_free (bind4_address);
2787 GNUNET_free (plugin->server_addr_v4);
2788 plugin->server_addr_v4 = NULL;
2789 return GNUNET_SYSERR;
2793 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2794 _("Binding to IPv4 address %s\n"), bind4_address);
2795 plugin->server_addr_v4->sin_family = AF_INET;
2796 plugin->server_addr_v4->sin_port = htons (plugin->port);
2798 GNUNET_free (bind4_address);
2801 if ((plugin->use_ipv6 == GNUNET_YES) &&
2803 GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg, plugin->name,
2804 "BINDTO6", &bind6_address)))
2806 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2807 "Binding %s plugin to specific IPv6 address: `%s'\n",
2808 plugin->protocol, bind6_address);
2809 plugin->server_addr_v6 = GNUNET_new (struct sockaddr_in6);
2811 inet_pton (AF_INET6, bind6_address, &plugin->server_addr_v6->sin6_addr))
2813 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
2815 ("Specific IPv6 address `%s' in configuration file is invalid!\n"),
2817 GNUNET_free (bind6_address);
2818 GNUNET_free (plugin->server_addr_v6);
2819 plugin->server_addr_v6 = NULL;
2820 return GNUNET_SYSERR;
2824 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2825 _("Binding to IPv6 address %s\n"), bind6_address);
2826 plugin->server_addr_v6->sin6_family = AF_INET6;
2827 plugin->server_addr_v6->sin6_port = htons (plugin->port);
2829 GNUNET_free (bind6_address);
2832 plugin->verify_external_hostname = GNUNET_NO;
2834 plugin->verify_external_hostname = GNUNET_CONFIGURATION_get_value_yesno (plugin->env->cfg, plugin->name,
2835 "VERIFY_EXTERNAL_HOSTNAME");
2836 if (GNUNET_SYSERR == plugin->verify_external_hostname)
2837 plugin->verify_external_hostname = GNUNET_NO;
2838 if (GNUNET_YES == plugin->verify_external_hostname)
2839 plugin->options |= HTTP_OPTIONS_VERIFY_CERTIFICATE;
2841 external_hostname_use_port = GNUNET_CONFIGURATION_get_value_yesno (plugin->env->cfg, plugin->name,
2842 "EXTERNAL_HOSTNAME_USE_PORT");
2843 if (GNUNET_SYSERR == external_hostname_use_port)
2844 external_hostname_use_port = GNUNET_NO;
2847 if (GNUNET_YES == GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg, plugin->name,
2848 "EXTERNAL_HOSTNAME", &eh_tmp))
2852 char * pos_url = NULL;
2854 if (NULL != strstr(eh_tmp, "://"))
2856 tmp = &strstr(eh_tmp, "://")[3];
2861 if (GNUNET_YES == external_hostname_use_port)
2863 if ( (strlen (tmp) > 1) && (NULL != (pos = strchr(tmp, '/'))) )
2867 GNUNET_asprintf (&plugin->external_hostname, "%s:%u/%s", tmp, (uint16_t) port, (NULL == pos_url) ? "" : pos_url);
2870 GNUNET_asprintf (&plugin->external_hostname, "%s:%u", tmp, (uint16_t) port);
2873 plugin->external_hostname = GNUNET_strdup (tmp);
2874 GNUNET_free (eh_tmp);
2876 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, plugin->name,
2877 _("Using external hostname `%s'\n"), plugin->external_hostname);
2878 plugin->notify_ext_task = GNUNET_SCHEDULER_add_now (&server_notify_external_hostname, plugin);
2880 /* Use only configured external hostname */
2881 if (GNUNET_CONFIGURATION_have_value
2882 (plugin->env->cfg, plugin->name, "EXTERNAL_HOSTNAME_ONLY"))
2884 plugin->external_only =
2885 GNUNET_CONFIGURATION_get_value_yesno (plugin->env->cfg, plugin->name,
2886 "EXTERNAL_HOSTNAME_ONLY");
2889 plugin->external_only = GNUNET_NO;
2891 if (GNUNET_YES == plugin->external_only)
2892 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2893 _("Notifying transport only about hostname `%s'\n"), plugin->external_hostname);
2896 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2897 "No external hostname configured\n");
2899 /* Optional parameters */
2900 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (plugin->env->cfg,
2902 "MAX_CONNECTIONS", &max_connections))
2903 max_connections = 128;
2904 plugin->max_connections = max_connections;
2906 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2907 _("Maximum number of connections is %u\n"),
2908 plugin->max_connections);
2911 plugin->peer_id_length = strlen (GNUNET_i2s_full (plugin->env->my_identity));
2918 * Session was idle, so disconnect it
2920 * @param cls the session
2921 * @param tc task context
2924 server_session_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2926 struct Session *s = cls;
2928 s->timeout_task = GNUNET_SCHEDULER_NO_TASK;
2929 GNUNET_log (TIMEOUT_LOG,
2930 "Session %p was idle for %s, disconnecting\n",
2932 GNUNET_STRINGS_relative_time_to_string (HTTP_SERVER_SESSION_TIMEOUT,
2935 /* call session destroy function */
2936 GNUNET_assert (GNUNET_OK ==
2937 http_server_plugin_disconnect_session (s->plugin, s));
2942 * Start session timeout for session s
2944 * @param s the session
2947 server_start_session_timeout (struct Session *s)
2949 GNUNET_assert (NULL != s);
2950 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == s->timeout_task);
2951 s->timeout_task = GNUNET_SCHEDULER_add_delayed (HTTP_SERVER_SESSION_TIMEOUT,
2952 &server_session_timeout,
2954 GNUNET_log (TIMEOUT_LOG,
2955 "Timeout for session %p set to %s\n",
2957 GNUNET_STRINGS_relative_time_to_string (HTTP_SERVER_SESSION_TIMEOUT,
2963 * Increment session timeout due to activity session s
2965 * @param s the session
2968 server_reschedule_session_timeout (struct Session *s)
2970 GNUNET_assert (NULL != s);
2971 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK != s->timeout_task);
2973 GNUNET_SCHEDULER_cancel (s->timeout_task);
2974 s->timeout_task = GNUNET_SCHEDULER_add_delayed (HTTP_SERVER_SESSION_TIMEOUT,
2975 &server_session_timeout,
2977 GNUNET_log (TIMEOUT_LOG,
2978 "Timeout rescheduled for session %p set to %s\n",
2980 GNUNET_STRINGS_relative_time_to_string (HTTP_SERVER_SESSION_TIMEOUT,
2986 * Exit point from the plugin.
2992 LIBGNUNET_PLUGIN_TRANSPORT_DONE (void *cls)
2994 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
2995 struct HTTP_Server_Plugin *plugin = api->cls;
2996 struct Session *pos;
2997 struct Session *next;
2999 if (NULL == api->cls)
3001 /* Free for stub mode */
3005 plugin->in_shutdown = GNUNET_YES;
3006 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
3007 _("Shutting down plugin `%s'\n"),
3010 if (GNUNET_SCHEDULER_NO_TASK != plugin->notify_ext_task)
3012 GNUNET_SCHEDULER_cancel (plugin->notify_ext_task);
3013 plugin->notify_ext_task = GNUNET_SCHEDULER_NO_TASK;
3016 if (NULL != plugin->ext_addr)
3018 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
3019 "Notifying transport to remove address `%s'\n",
3020 http_common_plugin_address_to_string (NULL,
3022 plugin->ext_addr->address,
3023 plugin->ext_addr->address_length));
3025 plugin->env->notify_address (plugin->env->cls,
3029 plugin->env->notify_address (plugin->env->cls,
3033 GNUNET_HELLO_address_free (plugin->ext_addr);
3034 plugin->ext_addr = NULL;
3037 /* Stop to report addresses to transport service */
3038 server_stop_report_addresses (plugin);
3039 server_stop (plugin);
3040 next = plugin->head;
3041 while (NULL != (pos = next))
3044 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
3045 "Removing left over session %p\n", pos);
3047 if ((GNUNET_YES == pos->session_passed) && (GNUNET_NO == pos->session_ended))
3049 /* Notify transport immediately that this session is invalid */
3050 pos->session_ended = GNUNET_YES;
3051 plugin->env->session_end (plugin->env->cls, pos->address, pos);
3053 server_delete_session (plugin, pos);
3057 GNUNET_free_non_null (plugin->external_hostname);
3058 GNUNET_free_non_null (plugin->ext_addr);
3059 GNUNET_free_non_null (plugin->server_addr_v4);
3060 GNUNET_free_non_null (plugin->server_addr_v6);
3062 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
3063 _("Shutdown for plugin `%s' complete\n"),
3066 GNUNET_free (plugin);
3073 http_plugin_address_to_string (void *cls,
3078 return http_common_plugin_address_to_string (cls, PLUGIN_NAME, addr, addrlen);
3080 return http_common_plugin_address_to_string (cls, PLUGIN_NAME, addr, addrlen);
3086 * Function obtain the network type for a session
3088 * @param cls closure ('struct Plugin*')
3089 * @param session the session
3090 * @return the network type in HBO or GNUNET_SYSERR
3092 static enum GNUNET_ATS_Network_Type
3093 http_server_get_network (void *cls,
3094 struct Session *session)
3096 GNUNET_assert (NULL != session);
3097 return ntohl (session->ats_address_network_type);
3102 * Entry point for the plugin.
3108 LIBGNUNET_PLUGIN_TRANSPORT_INIT (void *cls)
3110 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
3111 struct GNUNET_TRANSPORT_PluginFunctions *api;
3112 struct HTTP_Server_Plugin *plugin;
3114 if (NULL == env->receive)
3116 /* run in 'stub' mode (i.e. as part of gnunet-peerinfo), don't fully
3117 initialze the plugin or the API */
3118 api = GNUNET_new (struct GNUNET_TRANSPORT_PluginFunctions);
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;
3125 plugin = GNUNET_new (struct HTTP_Server_Plugin);
3127 api = GNUNET_new (struct GNUNET_TRANSPORT_PluginFunctions);
3129 api->send = &http_server_plugin_send;
3130 api->disconnect_peer = &http_server_plugin_disconnect_peer;
3131 api->disconnect_session = &http_server_plugin_disconnect_session;
3132 api->query_keepalive_factor = &http_server_query_keepalive_factor;
3133 api->check_address = &http_server_plugin_address_suggested;
3134 api->get_session = &http_server_plugin_get_session;
3136 api->address_to_string = &http_plugin_address_to_string;
3137 api->string_to_address = &http_common_plugin_string_to_address;
3138 api->address_pretty_printer = &http_common_plugin_address_pretty_printer;
3139 api->get_network = &http_server_get_network;
3140 api->update_session_timeout = &http_server_plugin_update_session_timeout;
3142 plugin->name = "transport-https_server";
3143 plugin->protocol = "https";
3145 plugin->name = "transport-http_server";
3146 plugin->protocol = "http";
3149 /* Configure plugin */
3150 if (GNUNET_SYSERR == server_configure_plugin (plugin))
3152 LIBGNUNET_PLUGIN_TRANSPORT_DONE (api);
3156 /* Check IPv6 support */
3157 if (GNUNET_YES == plugin->use_ipv6)
3158 plugin->use_ipv6 = server_check_ipv6_support (plugin);
3160 /* Report addresses to transport service */
3161 if (GNUNET_NO == plugin->external_only)
3162 server_start_report_addresses (plugin);
3164 if (GNUNET_SYSERR == server_start (plugin))
3166 LIBGNUNET_PLUGIN_TRANSPORT_DONE (api);
3172 /* end of plugin_transport_http_server.c */