2 This file is part of GNUnet
3 (C) 2002-2013 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file transport/plugin_transport_http_server.c
23 * @brief HTTP/S server transport plugin
24 * @author Matthias Wachs
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>"
52 /* Enable output for debbuging URL's of incoming requests */
53 #define DEBUG_URL_PARSE GNUNET_NO
57 * Encapsulation of all of the state of the plugin.
62 * Session handle for connections.
67 * Stored in a linked list.
72 * Stored in a linked list.
77 * To whom are we talking to (set to our identity
78 * if we are still waiting for the welcome message)
80 struct GNUNET_PeerIdentity target;
83 * Pointer to the global plugin struct.
85 struct HTTP_Server_Plugin *plugin;
88 * next pointer for double linked list
90 struct HTTP_Message *msg_head;
93 * previous pointer for double linked list
95 struct HTTP_Message *msg_tail;
98 * Message stream tokenizer for incoming data
100 struct GNUNET_SERVER_MessageStreamTokenizer *msg_tk;
105 struct ServerConnection *server_recv;
110 struct ServerConnection *server_send;
123 * Unique HTTP/S connection tag for this connection
128 * ATS network type in NBO
130 uint32_t ats_address_network_type;
133 * Was session given to transport service?
138 * Did we immediately end the session in disconnect_cb
143 * Are incoming connection established at the moment
145 int connect_in_progress;
148 * Absolute time when to receive data again
149 * Used for receive throttling
151 struct GNUNET_TIME_Absolute next_receive;
154 * Session timeout task
156 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
160 struct ServerConnection
165 /* Should this connection get disconnected? GNUNET_YES/NO */
168 /* For PUT connections: Is this the first or last callback with size 0 */
171 /* The session this server connection belongs to */
172 struct Session *session;
174 /* The MHD connection */
175 struct MHD_Connection *mhd_conn;
178 struct MHD_Daemon *mhd_daemon;
183 * Encapsulation of all of the state of the plugin.
185 struct HTTP_Server_Plugin
190 struct GNUNET_TRANSPORT_PluginEnvironment *env;
193 * Linked list head of open sessions.
196 struct Session *head;
199 * Linked list tail of open sessions.
201 struct Session *tail;
214 * My options to be included in the address
221 char *external_hostname;
224 * Verify external address
226 int verify_external_hostname;
230 * Maximum number of sockets the plugin can use
231 * Each http inbound /outbound connections are two connections
233 unsigned int max_connections;
236 * Current number of sockets the plugin can use
237 * Each http inbound /outbound connections are two connections
239 unsigned int cur_connections;
242 * Did we immediately end the session in disconnect_cb
252 * External hostname the plugin can be connected to, can be different to
253 * the host's FQDN, used e.g. for reverse proxying
255 struct HttpAddress *ext_addr;
258 * Notify transport only about external address
260 unsigned int external_only;
263 * External address length
283 * Task calling transport service about external address
285 GNUNET_SCHEDULER_TaskIdentifier notify_ext_task;
288 * NAT handle & address management
290 struct GNUNET_NAT_Handle *nat;
293 * List of own addresses
297 * IPv4 addresses DLL head
299 struct HttpAddressWrapper *addr_head;
302 * IPv4 addresses DLL tail
304 struct HttpAddressWrapper *addr_tail;
307 * IPv4 server socket to bind to
309 struct sockaddr_in *server_addr_v4;
312 * IPv6 server socket to bind to
314 struct sockaddr_in6 *server_addr_v6;
319 GNUNET_SCHEDULER_TaskIdentifier server_v4_task;
324 GNUNET_SCHEDULER_TaskIdentifier server_v6_task;
327 * The IPv4 server is scheduled to run asap
329 int server_v4_immediately;
332 * The IPv6 server is scheduled to run asap
334 int server_v6_immediately;
339 struct MHD_Daemon *server_v4;
344 struct MHD_Daemon *server_v6;
352 * Use RC4-128 instead of AES:
353 * NONE:+VERS-TLS1.0:+ARCFOUR-128:+SHA1:+RSA:+COMP-NULL
373 * Wrapper to manage addresses
375 struct HttpAddressWrapper
380 struct HttpAddressWrapper *next;
383 * Linked list previous
385 struct HttpAddressWrapper *prev;
387 struct HttpAddress *address;
394 * Message to send using http
399 * next pointer for double linked list
401 struct HTTP_Message *next;
404 * previous pointer for double linked list
406 struct HTTP_Message *prev;
409 * buffer containing data to send
414 * amount of data already sent
424 * HTTP/S specific overhead
429 * Continuation function to call once the transmission buffer
430 * has again space available. NULL if there is no
431 * continuation to call.
433 GNUNET_TRANSPORT_TransmitContinuation transmit_cont;
436 * Closure for transmit_cont.
438 void *transmit_cont_cls;
443 * The http_server plugin handle
445 static struct HTTP_Server_Plugin * p;
449 * Start session timeout for session s
450 * @param s the session
453 server_start_session_timeout (struct Session *s);
457 * Increment session timeout due to activity for session s
458 * @param s the session
461 server_reschedule_session_timeout (struct Session *s);
465 * Cancel timeout for session s
466 * @param s the session
469 server_stop_session_timeout (struct Session *s);
473 * Disconnect a session s
474 * @param s the session
477 server_disconnect (struct Session *s);
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, 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 '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 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,
598 server_reschedule_session_timeout (session);
606 * Function that can be used to force the plugin to disconnect
607 * from the given peer and cancel all previous transmissions
608 * (and their continuationc).
611 * @param target peer from which to disconnect
614 http_server_plugin_disconnect (void *cls, const struct GNUNET_PeerIdentity *target)
616 struct HTTP_Server_Plugin *plugin = cls;
617 struct Session *next = NULL;
618 struct Session *pos = NULL;
620 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
621 "Transport tells me to disconnect `%s'\n",
622 GNUNET_i2s (target));
625 while (NULL != (pos = next))
628 if (0 == memcmp (target, &pos->target, sizeof (struct GNUNET_PeerIdentity)))
630 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
631 "Disconnecting session %p to `%s'\n",
632 pos, GNUNET_i2s (target));
633 server_disconnect (pos);
641 * Another peer has suggested an address for this
642 * peer and transport plugin. Check that this could be a valid
643 * address. If so, consider adding it to the list
647 * @param addr pointer to the address
648 * @param addrlen length of addr
649 * @return GNUNET_OK if this is a plausible address for this peer
653 http_server_plugin_address_suggested (void *cls, const void *addr,
656 struct HttpAddressWrapper *next;
657 struct HttpAddressWrapper *pos;
658 const struct HttpAddress *haddr = addr;
660 if ((NULL != p->ext_addr) &&
661 GNUNET_YES == (http_common_cmp_addresses (addr, addrlen,
662 p->ext_addr, p->ext_addr_len)))
664 /* Checking HTTP_OPTIONS_VERIFY_CERTIFICATE option for external hostname */
665 if ((ntohl(haddr->options) & HTTP_OPTIONS_VERIFY_CERTIFICATE) !=
666 (p->options & HTTP_OPTIONS_VERIFY_CERTIFICATE))
667 return GNUNET_NO; /* VERIFY option not set as required! */
672 while (NULL != (pos = next))
675 if (GNUNET_YES == (http_common_cmp_addresses(addr,
687 * Creates a new outbound session the transport
688 * service will use to send data to the peer
690 * Since HTTP/S server cannot create sessions, always return NULL
692 * @param cls the plugin
693 * @param address the address
694 * @return always NULL
696 static struct Session *
697 http_server_plugin_get_session (void *cls,
698 const struct GNUNET_HELLO_Address *address)
705 * Deleting the session
706 * Must not be used afterwards
708 * @param s the session to delete
711 server_delete_session (struct Session *s)
713 struct HTTP_Server_Plugin *plugin = s->plugin;
714 server_stop_session_timeout(s);
716 GNUNET_CONTAINER_DLL_remove (plugin->head, plugin->tail, s);
717 struct HTTP_Message *msg = s->msg_head;
718 struct HTTP_Message *tmp = NULL;
724 GNUNET_CONTAINER_DLL_remove (s->msg_head, s->msg_tail, msg);
725 if (msg->transmit_cont != NULL)
727 msg->transmit_cont (msg->transmit_cont_cls, &s->target, GNUNET_SYSERR,
728 msg->size, msg->pos + msg->overhead);
734 if (s->msg_tk != NULL)
736 GNUNET_SERVER_mst_destroy (s->msg_tk);
739 GNUNET_free (s->addr);
740 GNUNET_free_non_null (s->server_recv);
741 GNUNET_free_non_null (s->server_send);
742 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
743 "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 gnunet 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, 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 s
829 * @param s the session
830 * @return GNUNET_OK on success
833 server_disconnect (struct Session *s)
835 struct ServerConnection * send = NULL;
836 struct ServerConnection * recv = NULL;
838 if (GNUNET_NO == server_exist_session (p, s))
841 return GNUNET_SYSERR;
844 send = (struct ServerConnection *) s->server_send;
845 if (s->server_send != NULL)
847 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, s->plugin->name,
848 "Server: %p / %p Terminating inbound PUT session to peer `%s'\n",
849 s, s->server_send, GNUNET_i2s (&s->target));
851 send->disconnect = GNUNET_YES;
852 #if MHD_VERSION >= 0x00090E00
853 MHD_set_connection_option (send->mhd_conn, MHD_CONNECTION_OPTION_TIMEOUT,
856 server_reschedule (s->plugin, send->mhd_daemon, GNUNET_YES);
859 recv = (struct ServerConnection *) s->server_recv;
862 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, s->plugin->name,
863 "Server: %p / %p Terminating inbound GET session to peer `%s'\n",
864 s, s->server_recv, GNUNET_i2s (&s->target));
866 recv->disconnect = GNUNET_YES;
867 #if MHD_VERSION >= 0x00090E00
868 MHD_set_connection_option (recv->mhd_conn, MHD_CONNECTION_OPTION_TIMEOUT,
871 server_reschedule (s->plugin, recv->mhd_daemon, GNUNET_YES);
879 * Tell MHD that the connection should timeout after @a to seconds.
881 * @param plugin our plugin
882 * @param s session for which the timeout changes
883 * @param to timeout in seconds
886 server_mhd_connection_timeout (struct HTTP_Server_Plugin *plugin,
890 #if MHD_VERSION >= 0x00090E00
891 /* Setting timeouts for other connections */
892 if (NULL != s->server_recv)
894 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
895 "Setting timeout for %p to %u sec.\n",
897 MHD_set_connection_option (s->server_recv->mhd_conn,
898 MHD_CONNECTION_OPTION_TIMEOUT,
900 server_reschedule (plugin, s->server_recv->mhd_daemon, GNUNET_NO);
902 if (NULL != s->server_send)
904 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
905 "Setting timeout for %p to %u sec.\n",
907 MHD_set_connection_option (s->server_send->mhd_conn,
908 MHD_CONNECTION_OPTION_TIMEOUT,
910 server_reschedule (plugin, s->server_send->mhd_daemon, GNUNET_NO);
916 * Parse incoming URL for tag and target
918 * @param plugin plugin
919 * @param url incoming url
920 * @param target where to store the target
921 * @param tag where to store the tag
922 * @return GNUNET_OK on success, GNUNET_SYSERR on error
926 server_parse_url (struct HTTP_Server_Plugin *plugin,
928 struct GNUNET_PeerIdentity *target,
931 char * tag_start = NULL;
932 char * tag_end = NULL;
933 char * target_start = NULL;
934 char * separator = NULL;
935 unsigned int hash_length;
936 unsigned long int ctag;
939 * URL is valid if it is in the form [prefix with (multiple) '/'][peerid[103];tag]*/
944 return GNUNET_SYSERR;
949 separator = strrchr (url, ';');
951 if (NULL == separator)
953 if (DEBUG_URL_PARSE) GNUNET_break (0);
954 return GNUNET_SYSERR;
956 tag_start = separator + 1;
958 if (strlen (tag_start) == 0)
960 /* No tag after separator */
961 if (DEBUG_URL_PARSE) GNUNET_break (0);
962 return GNUNET_SYSERR;
964 ctag = strtoul (tag_start, &tag_end, 10);
967 /* tag == 0 , invalid */
968 if (DEBUG_URL_PARSE) GNUNET_break (0);
969 return GNUNET_SYSERR;
971 if ((ctag == ULONG_MAX) && (ERANGE == errno))
973 /* out of range: > ULONG_MAX */
974 if (DEBUG_URL_PARSE) GNUNET_break (0);
975 return GNUNET_SYSERR;
977 if (ctag > UINT32_MAX)
979 /* out of range: > UINT32_MAX */
980 if (DEBUG_URL_PARSE) GNUNET_break (0);
981 return GNUNET_SYSERR;
983 (*tag) = (uint32_t) ctag;
986 /* no char after tag */
987 if (DEBUG_URL_PARSE) GNUNET_break (0);
988 return GNUNET_SYSERR;
990 if (url[strlen(url)] != tag_end[0])
992 /* there are more not converted chars after tag */
993 if (DEBUG_URL_PARSE) GNUNET_break (0);
994 return GNUNET_SYSERR;
997 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
998 "Found tag `%u' in url\n", (*tag));
1000 /* convert peer id */
1001 target_start = strrchr (url, '/');
1002 if (NULL == target_start)
1004 /* no leading '/' */
1005 target_start = (char *) url;
1008 hash_length = separator - target_start;
1009 if (hash_length != plugin->peer_id_length)
1011 /* no char after tag */
1012 if (DEBUG_URL_PARSE) GNUNET_break (0);
1013 return GNUNET_SYSERR;
1016 GNUNET_CRYPTO_eddsa_public_key_from_string (target_start,
1018 &target->public_key))
1020 /* hash conversion failed */
1021 if (DEBUG_URL_PARSE) GNUNET_break (0);
1022 return GNUNET_SYSERR;
1024 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1026 "Found target `%s' in URL\n",
1027 GNUNET_i2s_full (target));
1033 * Lookup a mhd connection and create one if none is found
1035 * @param plugin the plugin handle
1036 * @param mhd_connection the incoming mhd_connection
1037 * @param url incoming requested URL
1038 * @param method PUT or GET
1039 * @return the server connecetion
1041 static struct ServerConnection *
1042 server_lookup_connection (struct HTTP_Server_Plugin *plugin,
1043 struct MHD_Connection *mhd_connection, const char *url,
1046 struct Session *s = NULL;
1047 struct ServerConnection *sc = NULL;
1048 const union MHD_ConnectionInfo *conn_info;
1049 struct GNUNET_ATS_Information ats;
1050 struct HttpAddress *addr;
1052 struct GNUNET_PeerIdentity target;
1054 int direction = GNUNET_SYSERR;
1057 conn_info = MHD_get_connection_info (mhd_connection,
1058 MHD_CONNECTION_INFO_CLIENT_ADDRESS);
1059 if ((conn_info->client_addr->sa_family != AF_INET) &&
1060 (conn_info->client_addr->sa_family != AF_INET6))
1062 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1063 "New %s connection from %s\n", method, url);
1065 if (GNUNET_SYSERR == server_parse_url (plugin, url, &target, &tag))
1067 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1068 "Invalid url %s\n", url);
1071 if (0 == strcmp (MHD_HTTP_METHOD_PUT, method))
1072 direction = _RECEIVE;
1073 else if (0 == strcmp (MHD_HTTP_METHOD_GET, method))
1077 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1078 "Invalid method %s connection from %s\n", method, url);
1082 plugin->cur_connections++;
1083 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1084 "New %s connection from %s with tag %u (%u of %u)\n",
1086 GNUNET_i2s (&target), tag,
1087 plugin->cur_connections, plugin->max_connections);
1088 /* find duplicate session */
1092 if ((0 == memcmp (&s->target, &target, sizeof (struct GNUNET_PeerIdentity))) &&
1099 if ((_RECEIVE == direction) && (NULL != s->server_recv))
1101 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1102 "Duplicate PUT connection from `%s' tag %u, dismissing new connection\n",
1103 GNUNET_i2s (&target),
1108 if ((_SEND == direction) && (NULL != s->server_send))
1110 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1111 "Duplicate GET connection from `%s' tag %u, dismissing new connection\n",
1112 GNUNET_i2s (&target),
1119 /* create new session */
1120 switch (conn_info->client_addr->sa_family)
1123 addr = http_common_address_from_socket (plugin->protocol, conn_info->client_addr, sizeof (struct sockaddr_in));
1124 addr_len = http_common_address_get_size (addr);
1125 ats = plugin->env->get_address_type (plugin->env->cls, conn_info->client_addr, sizeof (struct sockaddr_in));
1128 addr = http_common_address_from_socket (plugin->protocol, conn_info->client_addr, sizeof (struct sockaddr_in6));
1129 addr_len = http_common_address_get_size (addr);
1130 ats = plugin->env->get_address_type (plugin->env->cls, conn_info->client_addr, sizeof (struct sockaddr_in6));
1133 /* external host name */
1134 ats.type = htonl (GNUNET_ATS_NETWORK_TYPE);
1135 ats.type = htonl (GNUNET_ATS_NET_WAN);
1139 s = GNUNET_malloc (sizeof (struct Session));
1140 memcpy (&s->target, &target, sizeof (struct GNUNET_PeerIdentity));
1143 s->addrlen = addr_len;
1144 s->ats_address_network_type = ats.value;
1145 s->next_receive = GNUNET_TIME_UNIT_ZERO_ABS;
1147 s->server_recv = NULL;
1148 s->server_send = NULL;
1149 s->session_passed = GNUNET_NO;
1150 s->session_ended = GNUNET_NO;
1151 s->connect_in_progress = GNUNET_YES;
1152 server_start_session_timeout(s);
1153 GNUNET_CONTAINER_DLL_insert (plugin->head, plugin->tail, s);
1155 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1156 "Creating new session %p for peer `%s' connecting from `%s'\n",
1157 s, GNUNET_i2s (&target),
1158 http_common_plugin_address_to_string (NULL, p->protocol, addr, addr_len));
1160 sc = GNUNET_malloc (sizeof (struct ServerConnection));
1161 if (conn_info->client_addr->sa_family == AF_INET)
1162 sc->mhd_daemon = plugin->server_v4;
1163 if (conn_info->client_addr->sa_family == AF_INET6)
1164 sc->mhd_daemon = plugin->server_v6;
1165 sc->mhd_conn = mhd_connection;
1166 sc->direction = direction;
1167 sc->connected = GNUNET_NO;
1169 if (direction == _SEND)
1170 s->server_send = sc;
1171 if (direction == _RECEIVE)
1172 s->server_recv = sc;
1174 if ((NULL != s->server_send) && (NULL != s->server_recv))
1176 s->connect_in_progress = GNUNET_NO; /* PUT and GET are connected */
1177 plugin->env->session_start (NULL, &s->target, PLUGIN_NAME, NULL, 0 ,s, NULL, 0);
1180 #if MHD_VERSION >= 0x00090E00
1181 if ((NULL == s->server_recv) || (NULL == s->server_send))
1183 to = (HTTP_SERVER_NOT_VALIDATED_TIMEOUT.rel_value_us / 1000LL / 1000LL);
1184 MHD_set_connection_option (mhd_connection,
1185 MHD_CONNECTION_OPTION_TIMEOUT, to);
1186 server_reschedule (plugin, sc->mhd_daemon, GNUNET_NO);
1190 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1191 "Session %p for peer `%s' fully connected\n",
1192 s, GNUNET_i2s (&target));
1193 to = (HTTP_SERVER_SESSION_TIMEOUT.rel_value_us / 1000LL / 1000LL);
1194 server_mhd_connection_timeout (plugin, s, to);
1197 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1198 "Setting timeout for %p to %u sec.\n", sc, to);
1205 * Lookup a session for a server connection
1207 * @param plugin the plugin
1208 * @param sc the server connection
1209 * @return the session found or NULL
1211 static struct Session *
1212 server_lookup_session (struct HTTP_Server_Plugin *plugin,
1213 struct ServerConnection * sc)
1217 for (s = plugin->head; NULL != s; s = s->next)
1218 if ((s->server_recv == sc) || (s->server_send == sc))
1224 server_exist_session (struct HTTP_Server_Plugin *plugin, struct Session *s)
1226 struct Session * head;
1228 GNUNET_assert (NULL != plugin);
1229 GNUNET_assert (NULL != s);
1231 for (head = plugin->head; head != NULL; head = head->next)
1241 * Callback called by MHD when it needs data to send
1243 * @param cls current session
1244 * @param pos position in buffer
1245 * @param buf the buffer to write data to
1246 * @param max max number of bytes available in buffer
1247 * @return bytes written to buffer
1250 server_send_callback (void *cls, uint64_t pos, char *buf, size_t max)
1252 struct Session *s = cls;
1253 ssize_t bytes_read = 0;
1254 struct HTTP_Message *msg;
1257 GNUNET_assert (NULL != p);
1258 if (GNUNET_NO == server_exist_session (p, s))
1264 bytes_read = GNUNET_MIN (msg->size - msg->pos,
1266 memcpy (buf, &msg->buf[msg->pos], bytes_read);
1267 msg->pos += bytes_read;
1269 /* removing message */
1270 if (msg->pos == msg->size)
1272 GNUNET_CONTAINER_DLL_remove (s->msg_head, s->msg_tail, msg);
1273 if (NULL != msg->transmit_cont)
1274 msg->transmit_cont (msg->transmit_cont_cls, &s->target, GNUNET_OK,
1275 msg->size, msg->size + msg->overhead);
1281 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, s->plugin->name,
1282 "Sent %u bytes to peer `%s' with session %p \n", bytes_read, GNUNET_i2s (&s->target), s);
1283 GNUNET_asprintf (&stat_txt, "# bytes currently in %s_server buffers", p->protocol);
1284 GNUNET_STATISTICS_update (p->env->stats,
1285 stat_txt, -bytes_read, GNUNET_NO);
1286 GNUNET_free (stat_txt);
1287 GNUNET_asprintf (&stat_txt, "# bytes transmitted via %s_server", p->protocol);
1288 GNUNET_STATISTICS_update (p->env->stats,
1289 stat_txt, bytes_read, GNUNET_NO);
1290 GNUNET_free (stat_txt);
1297 * Callback called by MessageStreamTokenizer when a message has arrived
1299 * @param cls current session as closure
1300 * @param client client
1301 * @param message the message to be forwarded to transport service
1305 server_receive_mst_cb (void *cls, void *client,
1306 const struct GNUNET_MessageHeader *message)
1308 struct Session *s = cls;
1309 struct HTTP_Server_Plugin *plugin = s->plugin;
1310 struct GNUNET_ATS_Information atsi;
1311 struct GNUNET_TIME_Relative delay;
1314 GNUNET_assert (NULL != p);
1315 if (GNUNET_NO == server_exist_session(p, s))
1319 atsi.type = htonl (GNUNET_ATS_NETWORK_TYPE);
1320 atsi.value = s->ats_address_network_type;
1321 GNUNET_break (s->ats_address_network_type != ntohl (GNUNET_ATS_NET_UNSPECIFIED));
1324 delay = plugin->env->receive (plugin->env->cls,
1329 plugin->env->update_address_metrics (plugin->env->cls,
1331 NULL, 0, s, &atsi, 1);
1333 GNUNET_asprintf (&stat_txt, "# bytes received via %s_server", plugin->protocol);
1334 GNUNET_STATISTICS_update (plugin->env->stats,
1335 stat_txt, ntohs (message->size), GNUNET_NO);
1336 GNUNET_free (stat_txt);
1338 s->session_passed = GNUNET_YES;
1339 s->next_receive = GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (), delay);
1340 if (delay.rel_value_us > 0)
1342 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1343 "Peer `%s' address `%s' next read delayed for %s\n",
1344 GNUNET_i2s (&s->target),
1345 http_common_plugin_address_to_string (NULL, p->protocol, s->addr, s->addrlen),
1346 GNUNET_STRINGS_relative_time_to_string (delay,
1349 server_reschedule_session_timeout (s);
1355 * MHD callback for a new incoming connection
1357 * @param cls the plugin handle
1358 * @param mhd_connection the mhd connection
1359 * @param url the requested URL
1360 * @param method GET or PUT
1361 * @param version HTTP version
1362 * @param upload_data upload data
1363 * @param upload_data_size sizeof upload data
1364 * @param httpSessionCache the session cache to remember the connection
1365 * @return MHD_YES if connection is accepted, MHD_NO on reject
1368 server_access_cb (void *cls, struct MHD_Connection *mhd_connection,
1369 const char *url, const char *method, const char *version,
1370 const char *upload_data, size_t * upload_data_size,
1371 void **httpSessionCache)
1373 struct HTTP_Server_Plugin *plugin = cls;
1376 struct ServerConnection *sc = *httpSessionCache;
1378 struct MHD_Response *response;
1380 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1381 _("Access from connection %p (%u of %u) for `%s' `%s' url `%s' with upload data size %u\n"),
1383 plugin->cur_connections, plugin->max_connections,
1384 method, version, url, (*upload_data_size));
1386 GNUNET_assert (cls != NULL);
1389 /* new connection */
1390 sc = server_lookup_connection (plugin, mhd_connection, url, method);
1393 (*httpSessionCache) = sc;
1397 response = MHD_create_response_from_data (strlen (HTTP_ERROR_RESPONSE), HTTP_ERROR_RESPONSE, MHD_NO, MHD_NO);
1398 MHD_add_response_header (response,
1399 MHD_HTTP_HEADER_CONTENT_TYPE,
1401 res = MHD_queue_response (mhd_connection, MHD_HTTP_NOT_FOUND, response);
1402 MHD_destroy_response (response);
1408 /* 'old' connection */
1409 if (NULL == server_lookup_session (plugin, sc))
1411 /* Session was already disconnected */
1416 /* existing connection */
1417 sc = (*httpSessionCache);
1419 GNUNET_assert (NULL != s);
1420 /* connection is to be disconnected */
1421 if (sc->disconnect == GNUNET_YES)
1423 /* Sent HTTP/1.1: 200 OK as response */
1424 response = MHD_create_response_from_data (strlen ("Thank you!"),
1427 MHD_queue_response (mhd_connection, MHD_HTTP_OK, response);
1428 MHD_destroy_response (response);
1431 GNUNET_assert (s != NULL);
1433 if (sc->direction == _SEND)
1435 response = MHD_create_response_from_callback (MHD_SIZE_UNKNOWN,
1437 &server_send_callback, s,
1439 MHD_queue_response (mhd_connection, MHD_HTTP_OK, response);
1440 MHD_destroy_response (response);
1443 if (sc->direction == _RECEIVE)
1445 if ((*upload_data_size == 0) && (sc->connected == GNUNET_NO))
1447 /* (*upload_data_size == 0) first callback when header are passed */
1448 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1449 "Session %p / Connection %p: Peer `%s' PUT on address `%s' connected\n",
1451 GNUNET_i2s (&s->target),
1452 http_common_plugin_address_to_string (NULL,
1456 sc->connected = GNUNET_YES;
1459 else if ((*upload_data_size == 0) && (sc->connected == GNUNET_YES))
1461 /* (*upload_data_size == 0) when upload is complete */
1462 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1463 "Session %p / Connection %p: Peer `%s' PUT on address `%s' finished upload\n",
1465 GNUNET_i2s (&s->target),
1466 http_common_plugin_address_to_string (NULL,
1470 sc->connected = GNUNET_NO;
1471 /* Sent HTTP/1.1: 200 OK as PUT Response\ */
1472 response = MHD_create_response_from_data (strlen ("Thank you!"),
1475 MHD_queue_response (mhd_connection, MHD_HTTP_OK, response);
1476 MHD_destroy_response (response);
1479 else if ((*upload_data_size > 0) && (sc->connected == GNUNET_YES))
1481 /* (*upload_data_size > 0) for every segment received */
1482 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1483 "Session %p / Connection %p: Peer `%s' PUT on address `%s' received %u bytes\n",
1485 GNUNET_i2s (&s->target),
1486 http_common_plugin_address_to_string (NULL,
1491 struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get ();
1493 if ((s->next_receive.abs_value_us <= now.abs_value_us))
1495 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1496 "PUT with %u bytes forwarded to MST\n",
1498 if (s->msg_tk == NULL)
1500 s->msg_tk = GNUNET_SERVER_mst_create (&server_receive_mst_cb, s);
1502 GNUNET_SERVER_mst_receive (s->msg_tk, s, upload_data,
1503 *upload_data_size, GNUNET_NO, GNUNET_NO);
1504 server_mhd_connection_timeout (plugin, s,
1505 GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT.rel_value_us / 1000LL / 1000LL);
1506 (*upload_data_size) = 0;
1510 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1511 "Session %p / Connection %p: no inbound bandwidth available! Next read was delayed by %s\n",
1513 GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_duration (s->next_receive),
1529 * Callback from MHD when a connection disconnects
1531 * @param cls closure
1532 * @param connection the disconnected MHD connection
1533 * @param httpSessionCache the pointer to distinguish
1536 server_disconnect_cb (void *cls, struct MHD_Connection *connection,
1537 void **httpSessionCache)
1539 struct ServerConnection *sc = *httpSessionCache;
1540 struct Session *s = NULL;
1541 struct Session *t = NULL;
1542 struct HTTP_Server_Plugin *plugin = NULL;
1544 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, p->name,
1545 "Disconnect for connection %p \n", sc);
1550 if (NULL == (s = server_lookup_session (p, sc)))
1553 GNUNET_assert (NULL != p);
1554 for (t = p->head; t != NULL; t = t->next)
1563 if (sc->direction == _SEND)
1566 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1567 "Peer `%s' connection %p, GET on address `%s' disconnected\n",
1568 GNUNET_i2s (&s->target), s->server_send,
1569 http_common_plugin_address_to_string (NULL, p->protocol, s->addr, s->addrlen));
1570 s->server_send = NULL;
1571 if (NULL != (s->server_recv))
1573 s->server_recv->disconnect = GNUNET_YES;
1574 GNUNET_assert (NULL != s->server_recv->mhd_conn);
1575 #if MHD_VERSION >= 0x00090E00
1576 MHD_set_connection_option (s->server_recv->mhd_conn, MHD_CONNECTION_OPTION_TIMEOUT,
1579 server_reschedule (plugin, s->server_recv->mhd_daemon, GNUNET_NO);
1582 if (sc->direction == _RECEIVE)
1584 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1585 "Peer `%s' connection %p PUT on address `%s' disconnected\n",
1586 GNUNET_i2s (&s->target), s->server_recv,
1587 http_common_plugin_address_to_string (NULL, p->protocol, s->addr, s->addrlen));
1588 s->server_recv = NULL;
1589 /* Do not terminate session when PUT disconnects
1590 if (NULL != (s->server_send))
1592 s->server_send->disconnect = GNUNET_YES;
1593 GNUNET_assert (NULL != s->server_send->mhd_conn);
1594 #if MHD_VERSION >= 0x00090E00
1595 MHD_set_connection_option (s->server_send->mhd_conn, MHD_CONNECTION_OPTION_TIMEOUT,
1598 server_reschedule (plugin, s->server_send->mhd_daemon, GNUNET_NO);
1600 if (s->msg_tk != NULL)
1602 GNUNET_SERVER_mst_destroy (s->msg_tk);
1608 plugin->cur_connections--;
1610 if ((s->server_send == NULL) && (s->server_recv == NULL))
1612 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1613 "Peer `%s' on address `%s' disconnected\n",
1614 GNUNET_i2s (&s->target),
1615 http_common_plugin_address_to_string (NULL, p->protocol, s->addr, s->addrlen));
1617 if ((GNUNET_YES == s->session_passed) && (GNUNET_NO == s->session_ended))
1619 /* Notify transport immediately that this session is invalid */
1620 s->session_ended = GNUNET_YES;
1621 plugin->env->session_end (plugin->env->cls, &s->target, s);
1623 server_delete_session (s);
1630 * Check if incoming connection is accepted.
1632 * @param cls plugin as closure
1633 * @param addr address of incoming connection
1634 * @param addr_len address length of incoming connection
1635 * @return MHD_YES if connection is accepted, MHD_NO if connection is rejected
1639 server_accept_cb (void *cls, const struct sockaddr *addr, socklen_t addr_len)
1641 struct HTTP_Server_Plugin *plugin = cls;
1643 if (plugin->cur_connections <= plugin->max_connections)
1645 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1646 _("Accepting connection (%u of %u) from `%s'\n"),
1647 plugin->cur_connections, plugin->max_connections,
1648 GNUNET_a2s (addr, addr_len));
1653 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, plugin->name,
1654 _("Server reached maximum number connections (%u), rejecting new connection\n"),
1655 plugin->max_connections);
1661 server_log (void *arg, const char *fmt, va_list ap)
1665 vsnprintf (text, sizeof (text), fmt, ap);
1667 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Server: %s\n", text);
1672 * Call MHD IPv4 to process pending requests and then go back
1673 * and schedule the next run.
1674 * @param cls plugin as closure
1675 * @param tc task context
1678 server_v4_run (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1680 struct HTTP_Server_Plugin *plugin = cls;
1682 GNUNET_assert (cls != NULL);
1684 plugin->server_v4_task = GNUNET_SCHEDULER_NO_TASK;
1685 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1688 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1689 "Running IPv4 server\n");
1691 plugin->server_v4_immediately = GNUNET_NO;
1692 GNUNET_assert (MHD_YES == MHD_run (plugin->server_v4));
1693 server_reschedule (plugin, plugin->server_v4, GNUNET_NO);
1698 * Call MHD IPv6 to process pending requests and then go back
1699 * and schedule the next run.
1700 * @param cls plugin as closure
1701 * @param tc task context
1704 server_v6_run (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1706 struct HTTP_Server_Plugin *plugin = cls;
1708 GNUNET_assert (cls != NULL);
1709 plugin->server_v6_task = GNUNET_SCHEDULER_NO_TASK;
1710 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1713 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1714 "Running IPv6 server\n");
1716 plugin->server_v6_immediately = GNUNET_NO;
1717 GNUNET_assert (MHD_YES == MHD_run (plugin->server_v6));
1718 server_reschedule (plugin, plugin->server_v6, GNUNET_NO);
1722 #define UNSIGNED_MHD_LONG_LONG unsigned MHD_LONG_LONG
1725 * Function that queries MHD's select sets and
1726 * starts the task waiting for them.
1728 * @param plugin plugin
1729 * @param daemon_handle the MHD daemon handle
1730 * @return gnunet task identifier
1732 static GNUNET_SCHEDULER_TaskIdentifier
1733 server_schedule (struct HTTP_Server_Plugin *plugin,
1734 struct MHD_Daemon *daemon_handle,
1737 GNUNET_SCHEDULER_TaskIdentifier ret;
1741 struct GNUNET_NETWORK_FDSet *wrs;
1742 struct GNUNET_NETWORK_FDSet *wws;
1743 struct GNUNET_NETWORK_FDSet *wes;
1745 UNSIGNED_MHD_LONG_LONG timeout;
1746 static unsigned long long last_timeout = 0;
1749 struct GNUNET_TIME_Relative tv;
1751 if (GNUNET_YES == plugin->in_shutdown)
1752 return GNUNET_SCHEDULER_NO_TASK;
1754 ret = GNUNET_SCHEDULER_NO_TASK;
1758 wrs = GNUNET_NETWORK_fdset_create ();
1759 wes = GNUNET_NETWORK_fdset_create ();
1760 wws = GNUNET_NETWORK_fdset_create ();
1762 GNUNET_assert (MHD_YES == MHD_get_fdset (daemon_handle, &rs, &ws, &es, &max));
1763 haveto = MHD_get_timeout (daemon_handle, &timeout);
1764 if (haveto == MHD_YES)
1766 if (timeout != last_timeout)
1769 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1770 "SELECT Timeout changed from %llu to %llu (ms)\n",
1771 last_timeout, timeout);
1772 last_timeout = timeout;
1774 if (timeout <= GNUNET_TIME_UNIT_SECONDS.rel_value_us / 1000LL)
1775 tv.rel_value_us = (uint64_t) timeout * 1000LL;
1777 tv = GNUNET_TIME_UNIT_SECONDS;
1780 tv = GNUNET_TIME_UNIT_SECONDS;
1781 /* Force immediate run, since we have outbound data to send */
1782 if (now == GNUNET_YES)
1783 tv = GNUNET_TIME_UNIT_MILLISECONDS;
1784 GNUNET_NETWORK_fdset_copy_native (wrs, &rs, max + 1);
1785 GNUNET_NETWORK_fdset_copy_native (wws, &ws, max + 1);
1786 GNUNET_NETWORK_fdset_copy_native (wes, &es, max + 1);
1788 if (daemon_handle == plugin->server_v4)
1790 if (plugin->server_v4_task != GNUNET_SCHEDULER_NO_TASK)
1792 GNUNET_SCHEDULER_cancel (plugin->server_v4_task);
1793 plugin->server_v4_task = GNUNET_SCHEDULER_NO_TASK;
1796 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1797 "Scheduling IPv4 server task in %llu ms\n", tv);
1800 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1802 &server_v4_run, plugin);
1804 if (daemon_handle == plugin->server_v6)
1806 if (plugin->server_v6_task != GNUNET_SCHEDULER_NO_TASK)
1808 GNUNET_SCHEDULER_cancel (plugin->server_v6_task);
1809 plugin->server_v6_task = GNUNET_SCHEDULER_NO_TASK;
1812 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1813 "Scheduling IPv6 server task in %llu ms\n", tv);
1816 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1818 &server_v6_run, plugin);
1820 GNUNET_NETWORK_fdset_destroy (wrs);
1821 GNUNET_NETWORK_fdset_destroy (wws);
1822 GNUNET_NETWORK_fdset_destroy (wes);
1829 * Load ssl certificate from file
1831 * @param file filename
1832 * @return content of the file
1835 server_load_file (const char *file)
1837 struct GNUNET_DISK_FileHandle *gn_file;
1841 if (GNUNET_OK != GNUNET_DISK_file_size (file,
1842 &fsize, GNUNET_NO, GNUNET_YES))
1844 text = GNUNET_malloc (fsize + 1);
1846 GNUNET_DISK_file_open (file, GNUNET_DISK_OPEN_READ,
1847 GNUNET_DISK_PERM_USER_READ);
1848 if (gn_file == NULL)
1853 if (GNUNET_SYSERR == GNUNET_DISK_file_read (gn_file, text, fsize))
1856 GNUNET_DISK_file_close (gn_file);
1860 GNUNET_DISK_file_close (gn_file);
1868 * Load ssl certificate
1870 * @param plugin the plugin
1871 * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
1874 server_load_certificate (struct HTTP_Server_Plugin *plugin)
1876 int res = GNUNET_OK;
1882 GNUNET_CONFIGURATION_get_value_filename (plugin->env->cfg, plugin->name,
1883 "KEY_FILE", &key_file))
1885 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
1886 plugin->name, "CERT_FILE");
1887 return GNUNET_SYSERR;
1890 GNUNET_CONFIGURATION_get_value_filename (plugin->env->cfg, plugin->name,
1891 "CERT_FILE", &cert_file))
1893 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
1894 plugin->name, "CERT_FILE");
1895 GNUNET_free (key_file);
1896 return GNUNET_SYSERR;
1898 /* Get crypto init string from config. If not present, use
1901 GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg,
1904 &plugin->crypto_init))
1905 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1906 "Using crypto init string `%s'\n",
1907 plugin->crypto_init);
1909 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1910 "Using default crypto init string \n");
1912 /* read key & certificates from file */
1913 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1914 "Trying to loading TLS certificate from key-file `%s' cert-file`%s'\n",
1915 key_file, cert_file);
1917 plugin->key = server_load_file (key_file);
1918 plugin->cert = server_load_file (cert_file);
1920 if ((plugin->key == NULL) || (plugin->cert == NULL))
1922 struct GNUNET_OS_Process *cert_creation;
1924 GNUNET_free_non_null (plugin->key);
1926 GNUNET_free_non_null (plugin->cert);
1927 plugin->cert = NULL;
1929 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1930 "No usable TLS certificate found, creating certificate\n");
1933 GNUNET_OS_start_process (GNUNET_NO, GNUNET_OS_INHERIT_STD_OUT_AND_ERR, NULL, NULL,
1934 "gnunet-transport-certificate-creation",
1935 "gnunet-transport-certificate-creation",
1936 key_file, cert_file, NULL);
1937 if (cert_creation == NULL)
1939 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
1941 ("Could not create a new TLS certificate, program `gnunet-transport-certificate-creation' could not be started!\n"));
1942 GNUNET_free (key_file);
1943 GNUNET_free (cert_file);
1945 GNUNET_free_non_null (plugin->key);
1947 GNUNET_free_non_null (plugin->cert);
1948 plugin->cert = NULL;
1949 GNUNET_free_non_null (plugin->crypto_init);
1950 plugin->crypto_init = NULL;
1952 return GNUNET_SYSERR;
1954 GNUNET_assert (GNUNET_OK == GNUNET_OS_process_wait (cert_creation));
1955 GNUNET_OS_process_destroy (cert_creation);
1957 plugin->key = server_load_file (key_file);
1958 plugin->cert = server_load_file (cert_file);
1961 if ((plugin->key == NULL) || (plugin->cert == NULL))
1963 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
1965 _("No usable TLS certificate found and creating one at `%s/%s' failed!\n"),
1966 key_file, cert_file);
1967 GNUNET_free (key_file);
1968 GNUNET_free (cert_file);
1970 GNUNET_free_non_null (plugin->key);
1972 GNUNET_free_non_null (plugin->cert);
1973 plugin->cert = NULL;
1974 GNUNET_free_non_null (plugin->crypto_init);
1975 plugin->crypto_init = NULL;
1977 return GNUNET_SYSERR;
1979 GNUNET_free (key_file);
1980 GNUNET_free (cert_file);
1981 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "TLS certificate loaded\n");
1988 * Start the HTTP server
1990 * @param plugin the plugin handle
1991 * @return GNUNET_OK on success, GNUNET_SYSERR on failure
1994 server_start (struct HTTP_Server_Plugin *plugin)
1996 unsigned int timeout;
1998 GNUNET_assert (NULL != plugin);
2001 if (GNUNET_SYSERR == server_load_certificate (plugin))
2003 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
2004 "Could not load or create server certificate! Loading plugin failed!\n");
2005 return GNUNET_SYSERR;
2010 #if MHD_VERSION >= 0x00090E00
2011 timeout = HTTP_SERVER_NOT_VALIDATED_TIMEOUT.rel_value_us / 1000LL / 1000LL;
2012 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2013 "MHD can set timeout per connection! Default time out %u sec.\n",
2016 timeout = HTTP_SERVER_SESSION_TIMEOUT.rel_value_us / 1000LL / 1000LL;
2017 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, plugin->name,
2018 "MHD cannot set timeout per connection! Default time out %u sec.\n",
2022 plugin->server_v4 = NULL;
2023 if (plugin->use_ipv4 == GNUNET_YES)
2025 plugin->server_v4 = MHD_start_daemon (
2032 MHD_NO_FLAG, plugin->port,
2033 &server_accept_cb, plugin,
2034 &server_access_cb, plugin,
2035 MHD_OPTION_SOCK_ADDR,
2036 (struct sockaddr_in *)
2037 plugin->server_addr_v4,
2038 MHD_OPTION_CONNECTION_LIMIT,
2040 plugin->max_connections,
2042 MHD_OPTION_HTTPS_PRIORITIES,
2043 plugin->crypto_init,
2044 MHD_OPTION_HTTPS_MEM_KEY,
2046 MHD_OPTION_HTTPS_MEM_CERT,
2049 MHD_OPTION_CONNECTION_TIMEOUT,
2051 MHD_OPTION_CONNECTION_MEMORY_LIMIT,
2053 GNUNET_SERVER_MAX_MESSAGE_SIZE),
2054 MHD_OPTION_NOTIFY_COMPLETED,
2055 &server_disconnect_cb, plugin,
2056 MHD_OPTION_EXTERNAL_LOGGER,
2057 server_log, NULL, MHD_OPTION_END);
2058 if (plugin->server_v4 == NULL)
2060 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
2061 "Failed to start %s IPv4 server component on port %u\n",
2062 plugin->name, plugin->port);
2065 server_reschedule (plugin, plugin->server_v4, GNUNET_NO);
2069 plugin->server_v6 = NULL;
2070 if (plugin->use_ipv6 == GNUNET_YES)
2072 plugin->server_v6 = MHD_start_daemon (
2079 MHD_USE_IPv6, plugin->port,
2080 &server_accept_cb, plugin,
2081 &server_access_cb, plugin,
2082 MHD_OPTION_SOCK_ADDR,
2083 (struct sockaddr_in6 *)
2084 plugin->server_addr_v6,
2085 MHD_OPTION_CONNECTION_LIMIT,
2087 plugin->max_connections,
2089 MHD_OPTION_HTTPS_PRIORITIES,
2090 plugin->crypto_init,
2091 MHD_OPTION_HTTPS_MEM_KEY,
2093 MHD_OPTION_HTTPS_MEM_CERT,
2096 MHD_OPTION_CONNECTION_TIMEOUT,
2098 MHD_OPTION_CONNECTION_MEMORY_LIMIT,
2100 GNUNET_SERVER_MAX_MESSAGE_SIZE),
2101 MHD_OPTION_NOTIFY_COMPLETED,
2102 &server_disconnect_cb, plugin,
2103 MHD_OPTION_EXTERNAL_LOGGER,
2104 server_log, NULL, MHD_OPTION_END);
2105 if (plugin->server_v6 == NULL)
2107 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
2108 "Failed to start %s IPv6 server component on port %u\n",
2109 plugin->name, plugin->port);
2112 server_reschedule (plugin, plugin->server_v6, GNUNET_NO);
2116 if ((plugin->server_v6 == NULL) && (plugin->server_v4 == NULL))
2118 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
2119 "%s %s server component started on port %u\n",
2120 msg, plugin->name, plugin->port);
2122 return GNUNET_SYSERR;
2124 else if ((plugin->server_v6 != NULL) && (plugin->server_v4 != NULL))
2125 msg = "IPv4 and IPv6";
2126 else if (plugin->server_v6 != NULL)
2128 else if (plugin->server_v4 != NULL)
2130 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2131 "%s %s server component started on port %u\n",
2132 msg, plugin->name, plugin->port);
2138 server_stop (struct HTTP_Server_Plugin *plugin)
2140 if (plugin->server_v4 != NULL)
2142 MHD_stop_daemon (plugin->server_v4);
2143 plugin->server_v4 = NULL;
2145 if ( plugin->server_v6 != NULL)
2147 MHD_stop_daemon (plugin->server_v6);
2148 plugin->server_v6 = NULL;
2152 if (plugin->server_v4_task != GNUNET_SCHEDULER_NO_TASK)
2154 GNUNET_SCHEDULER_cancel (plugin->server_v4_task);
2155 plugin->server_v4_task = GNUNET_SCHEDULER_NO_TASK;
2158 if (plugin->server_v6_task != GNUNET_SCHEDULER_NO_TASK)
2160 GNUNET_SCHEDULER_cancel (plugin->server_v6_task);
2161 plugin->server_v6_task = GNUNET_SCHEDULER_NO_TASK;
2166 GNUNET_free_non_null (plugin->crypto_init);
2167 GNUNET_free_non_null (plugin->cert);
2168 GNUNET_free_non_null (plugin->key);
2171 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2172 "%s server component stopped\n", plugin->name);
2177 * Add an address to the server's set of addresses and notify transport
2179 * @param cls the plugin handle
2180 * @param add_remove GNUNET_YES on add, GNUNET_NO on remove
2181 * @param addr the address
2182 * @param addrlen address length
2185 server_add_address (void *cls, int add_remove, const struct sockaddr *addr,
2188 struct HTTP_Server_Plugin *plugin = cls;
2189 struct HttpAddressWrapper *w = NULL;
2191 w = GNUNET_malloc (sizeof (struct HttpAddressWrapper));
2192 w->address = http_common_address_from_socket (plugin->protocol, addr, addrlen);
2193 if (NULL == w->address)
2198 w->addrlen = http_common_address_get_size (w->address);
2200 GNUNET_CONTAINER_DLL_insert(plugin->addr_head, plugin->addr_tail, w);
2201 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2202 "Notifying transport to add address `%s'\n",
2203 http_common_plugin_address_to_string (NULL, p->protocol, w->address, w->addrlen));
2205 plugin->env->notify_address (plugin->env->cls, add_remove, w->address, w->addrlen, "https_client");
2207 plugin->env->notify_address (plugin->env->cls, add_remove, w->address, w->addrlen, "http_client");
2213 * Remove an address from the server's set of addresses and notify transport
2215 * @param cls the plugin handle
2216 * @param add_remove GNUNET_YES on add, GNUNET_NO on remove
2217 * @param addr the address
2218 * @param addrlen address length
2221 server_remove_address (void *cls, int add_remove, const struct sockaddr *addr,
2224 struct HTTP_Server_Plugin *plugin = cls;
2225 struct HttpAddressWrapper *w = plugin->addr_head;
2227 void * saddr = http_common_address_from_socket (plugin->protocol, addr, addrlen);
2230 saddr_len = http_common_address_get_size (saddr);
2234 if (GNUNET_YES == http_common_cmp_addresses(w->address, w->addrlen, saddr, saddr_len))
2238 GNUNET_free (saddr);
2243 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2244 "Notifying transport to remove address `%s'\n",
2245 http_common_plugin_address_to_string (NULL, p->protocol, w->address, w->addrlen));
2246 GNUNET_CONTAINER_DLL_remove (plugin->addr_head, plugin->addr_tail, w);
2248 plugin->env->notify_address (plugin->env->cls, add_remove, w->address, w->addrlen, "https_client");
2250 plugin->env->notify_address (plugin->env->cls, add_remove, w->address, w->addrlen, "http_client");
2252 GNUNET_free (w->address);
2259 * Our external IP address/port mapping has changed.
2261 * @param cls closure, the 'struct LocalAddrList'
2262 * @param add_remove GNUNET_YES to mean the new public IP address, GNUNET_NO to mean
2263 * the previous (now invalid) one
2264 * @param addr either the previous or the new public IP address
2265 * @param addrlen actual lenght of the address
2268 server_nat_port_map_callback (void *cls, int add_remove, const struct sockaddr *addr,
2271 GNUNET_assert (cls != NULL);
2272 struct HTTP_Server_Plugin *plugin = cls;
2274 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2275 "NAT called to %s address `%s'\n",
2276 (add_remove == GNUNET_NO) ? "remove" : "add",
2277 GNUNET_a2s (addr, addrlen));
2279 if (AF_INET == addr->sa_family)
2281 struct sockaddr_in *s4 = (struct sockaddr_in *) addr;
2283 if (GNUNET_NO == plugin->use_ipv4)
2286 if ((NULL != plugin->server_addr_v4) &&
2287 (0 != memcmp (&plugin->server_addr_v4->sin_addr,
2288 &s4->sin_addr, sizeof (struct in_addr))))
2290 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2291 "Skipping address `%s' (not bindto address)\n",
2292 GNUNET_a2s (addr, addrlen));
2297 if (AF_INET6 == addr->sa_family)
2299 struct sockaddr_in6 *s6 = (struct sockaddr_in6 *) addr;
2300 if (GNUNET_NO == plugin->use_ipv6)
2303 if ((NULL != plugin->server_addr_v6) &&
2304 (0 != memcmp (&plugin->server_addr_v6->sin6_addr,
2305 &s6->sin6_addr, sizeof (struct in6_addr))))
2307 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2308 "Skipping address `%s' (not bindto address)\n",
2309 GNUNET_a2s (addr, addrlen));
2317 server_add_address (cls, add_remove, addr, addrlen);
2320 server_remove_address (cls, add_remove, addr, addrlen);
2327 * Get valid server addresses
2329 * @param plugin the plugin handle
2330 * @param service_name the servicename
2331 * @param cfg configuration handle
2332 * @param addrs addresses
2333 * @param addr_lens address length
2334 * @return number of addresses
2337 server_get_addresses (struct HTTP_Server_Plugin *plugin,
2338 const char *service_name,
2339 const struct GNUNET_CONFIGURATION_Handle *cfg,
2340 struct sockaddr ***addrs, socklen_t ** addr_lens)
2343 unsigned long long port;
2344 struct addrinfo hints;
2345 struct addrinfo *res;
2346 struct addrinfo *pos;
2347 struct addrinfo *next;
2351 struct sockaddr **saddrs;
2352 socklen_t *saddrlens;
2358 disablev6 = !plugin->use_ipv6;
2361 if (GNUNET_CONFIGURATION_have_value (cfg, service_name, "PORT"))
2363 GNUNET_break (GNUNET_OK ==
2364 GNUNET_CONFIGURATION_get_value_number (cfg, service_name,
2368 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2370 ("Require valid port number for service in configuration!\n"));
2371 return GNUNET_SYSERR;
2376 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, plugin->name,
2377 "Starting in listen only mode\n");
2378 return -1; /* listen only */
2382 if (GNUNET_CONFIGURATION_have_value (cfg, service_name, "BINDTO"))
2384 GNUNET_break (GNUNET_OK ==
2385 GNUNET_CONFIGURATION_get_value_string (cfg, service_name,
2386 "BINDTO", &hostname));
2391 if (hostname != NULL)
2393 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2394 "Resolving `%s' since that is where `%s' will bind to.\n",
2395 hostname, service_name);
2396 memset (&hints, 0, sizeof (struct addrinfo));
2398 hints.ai_family = AF_INET;
2399 if ((0 != (ret = getaddrinfo (hostname, NULL, &hints, &res))) ||
2402 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Failed to resolve `%s': %s\n"),
2403 hostname, gai_strerror (ret));
2404 GNUNET_free (hostname);
2405 return GNUNET_SYSERR;
2409 while (NULL != (pos = next))
2411 next = pos->ai_next;
2412 if ((disablev6) && (pos->ai_family == AF_INET6))
2418 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2419 _("Failed to find %saddress for `%s'.\n"),
2420 disablev6 ? "IPv4 " : "", hostname);
2422 GNUNET_free (hostname);
2423 return GNUNET_SYSERR;
2426 saddrs = GNUNET_malloc ((resi + 1) * sizeof (struct sockaddr *));
2427 saddrlens = GNUNET_malloc ((resi + 1) * sizeof (socklen_t));
2430 while (NULL != (pos = next))
2432 next = pos->ai_next;
2433 if ((disablev6) && (pos->ai_family == AF_INET6))
2435 if ((pos->ai_protocol != IPPROTO_TCP) && (pos->ai_protocol != 0))
2436 continue; /* not TCP */
2437 if ((pos->ai_socktype != SOCK_STREAM) && (pos->ai_socktype != 0))
2438 continue; /* huh? */
2439 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2440 "Service will bind to `%s'\n", GNUNET_a2s (pos->ai_addr,
2442 if (pos->ai_family == AF_INET)
2444 GNUNET_assert (pos->ai_addrlen == sizeof (struct sockaddr_in));
2445 saddrlens[i] = pos->ai_addrlen;
2446 saddrs[i] = GNUNET_malloc (saddrlens[i]);
2447 memcpy (saddrs[i], pos->ai_addr, saddrlens[i]);
2448 ((struct sockaddr_in *) saddrs[i])->sin_port = htons (port);
2452 GNUNET_assert (pos->ai_family == AF_INET6);
2453 GNUNET_assert (pos->ai_addrlen == sizeof (struct sockaddr_in6));
2454 saddrlens[i] = pos->ai_addrlen;
2455 saddrs[i] = GNUNET_malloc (saddrlens[i]);
2456 memcpy (saddrs[i], pos->ai_addr, saddrlens[i]);
2457 ((struct sockaddr_in6 *) saddrs[i])->sin6_port = htons (port);
2461 GNUNET_free (hostname);
2467 /* will bind against everything, just set port */
2473 saddrs = GNUNET_malloc ((resi + 1) * sizeof (struct sockaddr *));
2474 saddrlens = GNUNET_malloc ((resi + 1) * sizeof (socklen_t));
2476 saddrlens[i] = sizeof (struct sockaddr_in);
2477 saddrs[i] = GNUNET_malloc (saddrlens[i]);
2478 #if HAVE_SOCKADDR_IN_SIN_LEN
2479 ((struct sockaddr_in *) saddrs[i])->sin_len = saddrlens[i];
2481 ((struct sockaddr_in *) saddrs[i])->sin_family = AF_INET;
2482 ((struct sockaddr_in *) saddrs[i])->sin_port = htons (port);
2488 saddrs = GNUNET_malloc ((resi + 1) * sizeof (struct sockaddr *));
2489 saddrlens = GNUNET_malloc ((resi + 1) * sizeof (socklen_t));
2491 saddrlens[i] = sizeof (struct sockaddr_in6);
2492 saddrs[i] = GNUNET_malloc (saddrlens[i]);
2493 #if HAVE_SOCKADDR_IN_SIN_LEN
2494 ((struct sockaddr_in6 *) saddrs[i])->sin6_len = saddrlens[0];
2496 ((struct sockaddr_in6 *) saddrs[i])->sin6_family = AF_INET6;
2497 ((struct sockaddr_in6 *) saddrs[i])->sin6_port = htons (port);
2499 saddrlens[i] = sizeof (struct sockaddr_in);
2500 saddrs[i] = GNUNET_malloc (saddrlens[i]);
2501 #if HAVE_SOCKADDR_IN_SIN_LEN
2502 ((struct sockaddr_in *) saddrs[i])->sin_len = saddrlens[1];
2504 ((struct sockaddr_in *) saddrs[i])->sin_family = AF_INET;
2505 ((struct sockaddr_in *) saddrs[i])->sin_port = htons (port);
2509 *addr_lens = saddrlens;
2515 * Ask NAT for addresses
2517 * @param plugin the plugin handle
2520 server_start_report_addresses (struct HTTP_Server_Plugin *plugin)
2522 int res = GNUNET_OK;
2523 struct sockaddr **addrs;
2524 socklen_t *addrlens;
2526 res = server_get_addresses (plugin,
2527 plugin->name, plugin->env->cfg,
2529 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2530 _("Found %u addresses to report to NAT service\n"), res);
2532 if (GNUNET_SYSERR == res)
2539 GNUNET_NAT_register (plugin->env->cfg, GNUNET_YES, plugin->port,
2541 (const struct sockaddr **) addrs, addrlens,
2542 &server_nat_port_map_callback, NULL, plugin);
2546 GNUNET_assert (addrs[res] != NULL);
2547 GNUNET_free (addrs[res]);
2549 GNUNET_free_non_null (addrs);
2550 GNUNET_free_non_null (addrlens);
2555 * Stop NAT for addresses
2557 * @param plugin the plugin handle
2560 server_stop_report_addresses (struct HTTP_Server_Plugin *plugin)
2562 /* Stop NAT handle */
2563 if (NULL != plugin->nat)
2564 GNUNET_NAT_unregister (plugin->nat);
2566 /* Clean up addresses */
2567 struct HttpAddressWrapper *w;
2569 while (plugin->addr_head != NULL)
2571 w = plugin->addr_head;
2572 GNUNET_CONTAINER_DLL_remove (plugin->addr_head, plugin->addr_tail, w);
2573 GNUNET_free (w->address);
2580 * Check if IPv6 supported on this system
2582 * @param plugin the plugin handle
2583 * @return GNUNET_YES on success, else GNUNET_NO
2586 server_check_ipv6_support (struct HTTP_Server_Plugin *plugin)
2588 struct GNUNET_NETWORK_Handle *desc = NULL;
2589 int res = GNUNET_NO;
2591 /* Probe IPv6 support */
2592 desc = GNUNET_NETWORK_socket_create (PF_INET6, SOCK_STREAM, 0);
2595 if ((errno == ENOBUFS) || (errno == ENOMEM) || (errno == ENFILE) ||
2598 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "socket");
2600 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, plugin->name,
2602 ("Disabling IPv6 since it is not supported on this system!\n"));
2607 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (desc));
2611 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2612 "Testing IPv6 on this system: %s\n",
2613 (res == GNUNET_YES) ? "successful" : "failed");
2619 * Notify server about our external hostname
2622 * @param tc task context (unused)
2625 server_notify_external_hostname (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2627 struct HTTP_Server_Plugin *plugin = cls;
2631 plugin->notify_ext_task = GNUNET_SCHEDULER_NO_TASK;
2632 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
2635 GNUNET_asprintf(&url, "%s://%s", plugin->protocol, plugin->external_hostname);
2637 urlen = strlen (url) + 1;
2638 plugin->ext_addr = GNUNET_malloc (sizeof (struct HttpAddress) + urlen);
2639 plugin->ext_addr->options = htonl(plugin->options);
2640 plugin->ext_addr->urlen = htonl (urlen);
2641 plugin->ext_addr_len = sizeof (struct HttpAddress) + urlen;
2642 memcpy (&plugin->ext_addr[1], url, urlen);
2644 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2645 "Notifying transport about external hostname address `%s'\n", plugin->external_hostname);
2648 if (GNUNET_YES == plugin->verify_external_hostname)
2649 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, plugin->name,
2650 "Enabling SSL verification for external hostname address `%s'\n", plugin->external_hostname);
2651 plugin->env->notify_address (plugin->env->cls, GNUNET_YES,
2652 plugin->ext_addr, plugin->ext_addr_len,
2655 plugin->env->notify_address (plugin->env->cls, GNUNET_YES,
2656 plugin->ext_addr, plugin->ext_addr_len,
2663 * Configure the plugin
2665 * @param plugin plugin handle
2666 * @return GNUNET_OK on success, GNUNET_SYSERR on failure
2669 server_configure_plugin (struct HTTP_Server_Plugin *plugin)
2671 unsigned long long port;
2672 unsigned long long max_connections;
2673 char *bind4_address = NULL;
2674 char *bind6_address = NULL;
2675 char *eh_tmp = NULL;
2676 int external_hostname_use_port;
2679 if (GNUNET_CONFIGURATION_have_value
2680 (plugin->env->cfg, plugin->name, "USE_IPv4"))
2683 GNUNET_CONFIGURATION_get_value_yesno (plugin->env->cfg, plugin->name,
2687 plugin->use_ipv4 = GNUNET_YES;
2688 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2689 _("IPv4 support is %s\n"),
2690 (plugin->use_ipv4 == GNUNET_YES) ? "enabled" : "disabled");
2693 if (GNUNET_CONFIGURATION_have_value
2694 (plugin->env->cfg, plugin->name, "USE_IPv6"))
2697 GNUNET_CONFIGURATION_get_value_yesno (plugin->env->cfg, plugin->name,
2701 plugin->use_ipv6 = GNUNET_YES;
2702 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2703 _("IPv6 support is %s\n"),
2704 (plugin->use_ipv6 == GNUNET_YES) ? "enabled" : "disabled");
2706 if ((plugin->use_ipv4 == GNUNET_NO) && (plugin->use_ipv6 == GNUNET_NO))
2708 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
2710 ("Neither IPv4 nor IPv6 are enabled! Fix in configuration\n"),
2712 return GNUNET_SYSERR;
2715 /* Reading port number from config file */
2717 GNUNET_CONFIGURATION_get_value_number (plugin->env->cfg, plugin->name,
2718 "PORT", &port)) || (port > 65535))
2720 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
2721 _("Port is required! Fix in configuration\n"),
2723 return GNUNET_SYSERR;
2725 plugin->port = port;
2727 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, plugin->name,
2728 _("Using port %u\n"), plugin->port);
2730 if ((plugin->use_ipv4 == GNUNET_YES) &&
2731 (GNUNET_YES == GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg,
2732 plugin->name, "BINDTO", &bind4_address)))
2734 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2735 "Binding %s plugin to specific IPv4 address: `%s'\n",
2736 plugin->protocol, bind4_address);
2737 plugin->server_addr_v4 = GNUNET_malloc (sizeof (struct sockaddr_in));
2738 if (1 != inet_pton (AF_INET, bind4_address,
2739 &plugin->server_addr_v4->sin_addr))
2741 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
2743 ("Specific IPv4 address `%s' in configuration file is invalid!\n"),
2745 GNUNET_free (bind4_address);
2746 GNUNET_free (plugin->server_addr_v4);
2747 plugin->server_addr_v4 = NULL;
2748 return GNUNET_SYSERR;
2752 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2753 _("Binding to IPv4 address %s\n"), bind4_address);
2754 plugin->server_addr_v4->sin_family = AF_INET;
2755 plugin->server_addr_v4->sin_port = htons (plugin->port);
2757 GNUNET_free (bind4_address);
2760 if ((plugin->use_ipv6 == GNUNET_YES) &&
2762 GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg, plugin->name,
2763 "BINDTO6", &bind6_address)))
2765 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2766 "Binding %s plugin to specific IPv6 address: `%s'\n",
2767 plugin->protocol, bind6_address);
2768 plugin->server_addr_v6 = GNUNET_malloc (sizeof (struct sockaddr_in6));
2770 inet_pton (AF_INET6, bind6_address, &plugin->server_addr_v6->sin6_addr))
2772 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
2774 ("Specific IPv6 address `%s' in configuration file is invalid!\n"),
2776 GNUNET_free (bind6_address);
2777 GNUNET_free (plugin->server_addr_v6);
2778 plugin->server_addr_v6 = NULL;
2779 return GNUNET_SYSERR;
2783 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2784 _("Binding to IPv6 address %s\n"), bind6_address);
2785 plugin->server_addr_v6->sin6_family = AF_INET6;
2786 plugin->server_addr_v6->sin6_port = htons (plugin->port);
2788 GNUNET_free (bind6_address);
2791 plugin->verify_external_hostname = GNUNET_NO;
2793 plugin->verify_external_hostname = GNUNET_CONFIGURATION_get_value_yesno (plugin->env->cfg, plugin->name,
2794 "VERIFY_EXTERNAL_HOSTNAME");
2795 if (GNUNET_SYSERR == plugin->verify_external_hostname)
2796 plugin->verify_external_hostname = GNUNET_NO;
2797 if (GNUNET_YES == plugin->verify_external_hostname)
2798 plugin->options |= HTTP_OPTIONS_VERIFY_CERTIFICATE;
2800 external_hostname_use_port = GNUNET_CONFIGURATION_get_value_yesno (plugin->env->cfg, plugin->name,
2801 "EXTERNAL_HOSTNAME_USE_PORT");
2802 if (GNUNET_SYSERR == external_hostname_use_port)
2803 external_hostname_use_port = GNUNET_NO;
2806 if (GNUNET_YES == GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg, plugin->name,
2807 "EXTERNAL_HOSTNAME", &eh_tmp))
2811 char * pos_url = NULL;
2813 if (NULL != strstr(eh_tmp, "://"))
2815 tmp = &strstr(eh_tmp, "://")[3];
2820 if (GNUNET_YES == external_hostname_use_port)
2822 if ( (strlen (tmp) > 1) && (NULL != (pos = strchr(tmp, '/'))) )
2826 GNUNET_asprintf (&plugin->external_hostname, "%s:%u/%s", tmp, (uint16_t) port, (NULL == pos_url) ? "" : pos_url);
2829 GNUNET_asprintf (&plugin->external_hostname, "%s:%u", tmp, (uint16_t) port);
2832 plugin->external_hostname = GNUNET_strdup (tmp);
2833 GNUNET_free (eh_tmp);
2835 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, plugin->name,
2836 _("Using external hostname `%s'\n"), plugin->external_hostname);
2837 plugin->notify_ext_task = GNUNET_SCHEDULER_add_now (&server_notify_external_hostname, plugin);
2839 /* Use only configured external hostname */
2840 if (GNUNET_CONFIGURATION_have_value
2841 (plugin->env->cfg, plugin->name, "EXTERNAL_HOSTNAME_ONLY"))
2843 plugin->external_only =
2844 GNUNET_CONFIGURATION_get_value_yesno (plugin->env->cfg, plugin->name,
2845 "EXTERNAL_HOSTNAME_ONLY");
2848 plugin->external_only = GNUNET_NO;
2850 if (GNUNET_YES == plugin->external_only)
2851 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2852 _("Notifying transport only about hostname `%s'\n"), plugin->external_hostname);
2855 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2856 "No external hostname configured\n");
2858 /* Optional parameters */
2859 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (plugin->env->cfg,
2861 "MAX_CONNECTIONS", &max_connections))
2862 max_connections = 128;
2863 plugin->max_connections = max_connections;
2865 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2866 _("Maximum number of connections is %u\n"),
2867 plugin->max_connections);
2870 plugin->peer_id_length = strlen (GNUNET_i2s_full (plugin->env->my_identity));
2877 * Session was idle, so disconnect it
2879 * @param cls the session
2880 * @param tc task context
2883 server_session_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2885 GNUNET_assert (NULL != cls);
2886 struct Session *s = cls;
2888 s->timeout_task = GNUNET_SCHEDULER_NO_TASK;
2889 GNUNET_log (TIMEOUT_LOG,
2890 "Session %p was idle for %s, disconnecting\n",
2892 GNUNET_STRINGS_relative_time_to_string (HTTP_SERVER_SESSION_TIMEOUT,
2895 /* call session destroy function */
2896 GNUNET_assert (GNUNET_OK == server_disconnect (s));
2901 * Start session timeout for session s
2903 * @param s the session
2906 server_start_session_timeout (struct Session *s)
2908 GNUNET_assert (NULL != s);
2909 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == s->timeout_task);
2910 s->timeout_task = GNUNET_SCHEDULER_add_delayed (HTTP_SERVER_SESSION_TIMEOUT,
2911 &server_session_timeout,
2913 GNUNET_log (TIMEOUT_LOG,
2914 "Timeout for session %p set to %s\n",
2916 GNUNET_STRINGS_relative_time_to_string (HTTP_SERVER_SESSION_TIMEOUT,
2922 * Increment session timeout due to activity session s
2924 * @param s the session
2927 server_reschedule_session_timeout (struct Session *s)
2929 GNUNET_assert (NULL != s);
2930 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK != s->timeout_task);
2932 GNUNET_SCHEDULER_cancel (s->timeout_task);
2933 s->timeout_task = GNUNET_SCHEDULER_add_delayed (HTTP_SERVER_SESSION_TIMEOUT,
2934 &server_session_timeout,
2936 GNUNET_log (TIMEOUT_LOG,
2937 "Timeout rescheduled for session %p set to %s\n",
2939 GNUNET_STRINGS_relative_time_to_string (HTTP_SERVER_SESSION_TIMEOUT,
2945 * Exit point from the plugin.
2951 LIBGNUNET_PLUGIN_TRANSPORT_DONE (void *cls)
2953 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
2954 struct HTTP_Server_Plugin *plugin = api->cls;
2955 struct Session *pos;
2956 struct Session *next;
2958 if (NULL == api->cls)
2960 /* Free for stub mode */
2964 plugin->in_shutdown = GNUNET_YES;
2965 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2966 _("Shutting down plugin `%s'\n"),
2969 if (GNUNET_SCHEDULER_NO_TASK != plugin->notify_ext_task)
2971 GNUNET_SCHEDULER_cancel (plugin->notify_ext_task);
2972 plugin->notify_ext_task = GNUNET_SCHEDULER_NO_TASK;
2975 if (NULL != plugin->ext_addr)
2977 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2978 "Notifying transport to remove address `%s'\n",
2979 http_common_plugin_address_to_string (NULL,
2982 plugin->ext_addr_len));
2984 plugin->env->notify_address (plugin->env->cls,
2987 plugin->ext_addr_len,
2990 plugin->env->notify_address (plugin->env->cls,
2993 plugin->ext_addr_len,
2999 /* Stop to report addresses to transport service */
3000 server_stop_report_addresses (plugin);
3001 server_stop (plugin);
3002 next = plugin->head;
3003 while (NULL != (pos = next))
3006 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
3007 "Removing left over session %p\n", pos);
3009 if ((GNUNET_YES == pos->session_passed) && (GNUNET_NO == pos->session_ended))
3011 /* Notify transport immediately that this session is invalid */
3012 pos->session_ended = GNUNET_YES;
3013 plugin->env->session_end (plugin->env->cls, &pos->target, pos);
3016 server_delete_session (pos);
3020 GNUNET_free_non_null (plugin->external_hostname);
3021 GNUNET_free_non_null (plugin->ext_addr);
3022 GNUNET_free_non_null (plugin->server_addr_v4);
3023 GNUNET_free_non_null (plugin->server_addr_v6);
3025 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
3026 _("Shutdown for plugin `%s' complete\n"),
3029 GNUNET_free (plugin);
3034 const char *http_plugin_address_to_string (void *cls,
3039 return http_common_plugin_address_to_string (cls, PLUGIN_NAME, addr, addrlen);
3041 return http_common_plugin_address_to_string (cls, PLUGIN_NAME, addr, addrlen);
3048 * Function obtain the network type for a session
3050 * @param cls closure ('struct Plugin*')
3051 * @param session the session
3052 * @return the network type in HBO or GNUNET_SYSERR
3054 static enum GNUNET_ATS_Network_Type
3055 http_server_get_network (void *cls,
3056 struct Session *session)
3058 GNUNET_assert (NULL != session);
3059 return ntohl (session->ats_address_network_type);
3064 * Entry point for the plugin.
3070 LIBGNUNET_PLUGIN_TRANSPORT_INIT (void *cls)
3072 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
3073 struct GNUNET_TRANSPORT_PluginFunctions *api;
3074 struct HTTP_Server_Plugin *plugin;
3076 plugin = GNUNET_malloc (sizeof (struct HTTP_Server_Plugin));
3080 if (NULL == env->receive)
3082 /* run in 'stub' mode (i.e. as part of gnunet-peerinfo), don't fully
3083 initialze the plugin or the API */
3084 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
3086 api->address_to_string = &http_plugin_address_to_string;
3087 api->string_to_address = &http_common_plugin_string_to_address;
3088 api->address_pretty_printer = &http_common_plugin_address_pretty_printer;
3092 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
3094 api->send = &http_server_plugin_send;
3095 api->disconnect = &http_server_plugin_disconnect;
3096 api->check_address = &http_server_plugin_address_suggested;
3097 api->get_session = &http_server_plugin_get_session;
3099 api->address_to_string = &http_plugin_address_to_string;
3100 api->string_to_address = &http_common_plugin_string_to_address;
3101 api->address_pretty_printer = &http_common_plugin_address_pretty_printer;
3102 api->get_network = &http_server_get_network;
3105 plugin->name = "transport-https_server";
3106 plugin->protocol = "https";
3108 plugin->name = "transport-http_server";
3109 plugin->protocol = "http";
3112 /* Configure plugin */
3113 if (GNUNET_SYSERR == server_configure_plugin (plugin))
3115 LIBGNUNET_PLUGIN_TRANSPORT_DONE (api);
3119 /* Check IPv6 support */
3120 if (GNUNET_YES == plugin->use_ipv6)
3121 plugin->use_ipv6 = server_check_ipv6_support (plugin);
3123 /* Report addresses to transport service */
3124 if (GNUNET_NO == plugin->external_only)
3125 server_start_report_addresses (plugin);
3127 if (GNUNET_SYSERR == server_start (plugin))
3129 LIBGNUNET_PLUGIN_TRANSPORT_DONE (api);
3135 /* end of plugin_transport_http_server.c */