2 This file is part of GNUnet
3 (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file transport/plugin_transport_http_server.c
23 * @brief HTTP/S server transport plugin
24 * @author Matthias Wachs
28 #include "gnunet_common.h"
29 #include "gnunet_server_lib.h"
30 #include "gnunet_statistics_service.h"
31 #include "gnunet_transport_plugin.h"
32 #include "gnunet_nat_lib.h"
33 #include "plugin_transport_http_common.h"
34 #include <microhttpd.h>
39 #define PLUGIN_NAME "https_server"
40 #define LIBGNUNET_PLUGIN_TRANSPORT_INIT libgnunet_plugin_transport_https_server_init
41 #define LIBGNUNET_PLUGIN_TRANSPORT_DONE libgnunet_plugin_transport_https_server_done
43 #define PLUGIN_NAME "http_server"
44 #define LIBGNUNET_PLUGIN_TRANSPORT_INIT libgnunet_plugin_transport_http_server_init
45 #define LIBGNUNET_PLUGIN_TRANSPORT_DONE libgnunet_plugin_transport_http_server_done
48 #define HTTP_ERROR_RESPONSE "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\"><HTML><HEAD><TITLE>404 Not Found</TITLE></HEAD><BODY><H1>Not Found</H1>The requested URL was not found on this server.<P><HR><ADDRESS></ADDRESS></BODY></HTML>"
53 /* Enable output for debbuging URL's of incoming requests */
54 #define DEBUG_URL_PARSE GNUNET_NO
58 * Encapsulation of all of the state of the plugin.
63 * Session handle for connections.
68 * Stored in a linked list.
73 * Stored in a linked list.
78 * To whom are we talking to (set to our identity
79 * if we are still waiting for the welcome message)
81 struct GNUNET_PeerIdentity target;
84 * Pointer to the global plugin struct.
86 struct HTTP_Server_Plugin *plugin;
89 * next pointer for double linked list
91 struct HTTP_Message *msg_head;
94 * previous pointer for double linked list
96 struct HTTP_Message *msg_tail;
99 * Message stream tokenizer for incoming data
101 struct GNUNET_SERVER_MessageStreamTokenizer *msg_tk;
106 struct ServerConnection *server_recv;
111 struct ServerConnection *server_send;
124 * Unique HTTP/S connection tag for this connection
129 * ATS network type in NBO
131 uint32_t ats_address_network_type;
134 * Was session given to transport service?
139 * Did we immediately end the session in disconnect_cb
144 * Are incoming connection established at the moment
146 int connect_in_progress;
149 * Absolute time when to receive data again
150 * Used for receive throttling
152 struct GNUNET_TIME_Absolute next_receive;
155 * Session timeout task
157 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
161 struct ServerConnection
166 /* Should this connection get disconnected? GNUNET_YES/NO */
169 /* For PUT connections: Is this the first or last callback with size 0 */
172 /* The session this server connection belongs to */
173 struct Session *session;
175 /* The MHD connection */
176 struct MHD_Connection *mhd_conn;
179 struct MHD_Daemon *mhd_daemon;
184 * Encapsulation of all of the state of the plugin.
186 struct HTTP_Server_Plugin
191 struct GNUNET_TRANSPORT_PluginEnvironment *env;
194 * Linked list head of open sessions.
197 struct Session *head;
200 * Linked list tail of open sessions.
202 struct Session *tail;
215 * My options to be included in the address
222 char *external_hostname;
225 * Verify external address
227 int verify_external_hostname;
231 * Maximum number of sockets the plugin can use
232 * Each http inbound /outbound connections are two connections
234 unsigned int max_connections;
237 * Current number of sockets the plugin can use
238 * Each http inbound /outbound connections are two connections
240 unsigned int cur_connections;
243 * Did we immediately end the session in disconnect_cb
253 * External hostname the plugin can be connected to, can be different to
254 * the host's FQDN, used e.g. for reverse proxying
256 struct HttpAddress *ext_addr;
259 * Notify transport only about external address
261 unsigned int external_only;
264 * External address length
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 * The http_server plugin handle
446 static struct HTTP_Server_Plugin * p;
450 * Start session timeout for session s
451 * @param s the session
454 server_start_session_timeout (struct Session *s);
458 * Increment session timeout due to activity for session s
459 * @param s the session
462 server_reschedule_session_timeout (struct Session *s);
466 * Cancel timeout for session s
467 * @param s the session
470 server_stop_session_timeout (struct Session *s);
474 * Disconnect a session s
475 * @param s the session
478 server_disconnect (struct Session *s);
482 * Does session s exist?
484 * @param plugin the plugin handle
485 * @param s the session
486 * @return GNUNET_YES on success, GNUNET_NO on error
489 server_exist_session (struct HTTP_Server_Plugin *plugin, struct Session *s);
493 * Reschedule the execution of both IPv4 and IPv6 server
494 * @param plugin the plugin
495 * @param server which server to schedule v4 or v6?
496 * @param now GNUNET_YES to schedule execution immediately, GNUNET_NO to wait
500 server_reschedule (struct HTTP_Server_Plugin *plugin, struct MHD_Daemon *server,
505 * Function that can be used by the transport service to transmit
506 * a message using the plugin. Note that in the case of a
507 * peer disconnecting, the continuation MUST be called
508 * prior to the disconnect notification itself. This function
509 * will be called with this peer's HELLO message to initiate
510 * a fresh connection to another peer.
513 * @param session which session must be used
514 * @param msgbuf the message to transmit
515 * @param msgbuf_size number of bytes in 'msgbuf'
516 * @param priority how important is the message (most plugins will
517 * ignore message priority and just FIFO)
518 * @param to how long to wait at most for the transmission (does not
519 * require plugins to discard the message after the timeout,
520 * just advisory for the desired delay; most plugins will ignore
522 * @param cont continuation to call once the message has
523 * been transmitted (or if the transport is ready
524 * for the next transmission call; or if the
525 * peer disconnected...); can be NULL
526 * @param cont_cls closure for cont
527 * @return number of bytes used (on the physical network, with overheads);
528 * -1 on hard errors (i.e. address invalid); 0 is a legal value
529 * and does NOT mean that the message was not transmitted (DV)
532 http_server_plugin_send (void *cls,
533 struct Session *session,
534 const char *msgbuf, size_t msgbuf_size,
535 unsigned int priority,
536 struct GNUNET_TIME_Relative to,
537 GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
539 struct HTTP_Server_Plugin *plugin = cls;
540 struct HTTP_Message *msg;
544 GNUNET_assert (plugin != NULL);
545 GNUNET_assert (session != NULL);
547 if (GNUNET_NO == server_exist_session (plugin, session))
550 return GNUNET_SYSERR;
552 if (NULL == session->server_send)
554 if (GNUNET_NO == session->connect_in_progress)
556 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, session->plugin->name,
557 "Session %p/connection %p: Sending message with %u bytes to peer `%s' with FAILED\n",
558 session, session->server_send,
559 msgbuf_size, GNUNET_i2s (&session->target));
561 return GNUNET_SYSERR;
566 if (GNUNET_YES == session->server_send->disconnect)
567 return GNUNET_SYSERR;
571 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, session->plugin->name,
572 "Session %p/connection %p: Sending message with %u to peer `%s' with \n",
573 session, session->server_send,
574 msgbuf_size, GNUNET_i2s (&session->target));
576 /* create new message and schedule */
577 bytes_sent = sizeof (struct HTTP_Message) + msgbuf_size;
578 msg = GNUNET_malloc (bytes_sent);
580 msg->size = msgbuf_size;
582 msg->buf = (char *) &msg[1];
583 msg->transmit_cont = cont;
584 msg->transmit_cont_cls = cont_cls;
585 memcpy (msg->buf, msgbuf, msgbuf_size);
587 GNUNET_CONTAINER_DLL_insert_tail (session->msg_head, session->msg_tail, msg);
589 GNUNET_asprintf (&stat_txt, "# bytes currently in %s_server buffers", plugin->protocol);
590 GNUNET_STATISTICS_update (plugin->env->stats,
591 stat_txt, msgbuf_size, GNUNET_NO);
592 GNUNET_free (stat_txt);
594 if (NULL != session->server_send)
596 server_reschedule (session->plugin,
597 session->server_send->mhd_daemon,
599 server_reschedule_session_timeout (session);
607 * Function that can be used to force the plugin to disconnect
608 * from the given peer and cancel all previous transmissions
609 * (and their continuationc).
612 * @param target peer from which to disconnect
615 http_server_plugin_disconnect (void *cls, const struct GNUNET_PeerIdentity *target)
617 struct HTTP_Server_Plugin *plugin = cls;
618 struct Session *next = NULL;
619 struct Session *pos = NULL;
621 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
622 "Transport tells me to disconnect `%s'\n",
623 GNUNET_i2s (target));
626 while (NULL != (pos = next))
629 if (0 == memcmp (target, &pos->target, sizeof (struct GNUNET_PeerIdentity)))
631 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
632 "Disconnecting session %p to `%s'\n",
633 pos, GNUNET_i2s (target));
634 server_disconnect (pos);
642 * Another peer has suggested an address for this
643 * peer and transport plugin. Check that this could be a valid
644 * address. If so, consider adding it to the list
648 * @param addr pointer to the address
649 * @param addrlen length of addr
650 * @return GNUNET_OK if this is a plausible address for this peer
654 http_server_plugin_address_suggested (void *cls, const void *addr,
657 struct HTTP_Server_Plugin *plugin = cls;
658 struct HttpAddressWrapper *next;
659 struct HttpAddressWrapper *pos;
660 struct HttpAddress *h_addr;
661 h_addr = (struct HttpAddress *) addr;
663 if ((NULL != plugin->ext_addr) &&
664 GNUNET_YES == (http_common_cmp_addresses (addr, addrlen,
665 plugin->ext_addr, plugin->ext_addr_len)))
667 /* Checking HTTP_OPTIONS_VERIFY_CERTIFICATE option for external hostname */
668 if ((ntohl(h_addr->options) & HTTP_OPTIONS_VERIFY_CERTIFICATE) !=
669 (plugin->options & HTTP_OPTIONS_VERIFY_CERTIFICATE))
670 return GNUNET_NO; /* VERIFY option not set as required! */
674 next = plugin->addr_head;
675 while (NULL != (pos = next))
678 if (GNUNET_YES == (http_common_cmp_addresses(addr,
691 * Creates a new outbound session the transport
692 * service will use to send data to the peer
694 * Since HTTP/S server cannot create sessions, always return NULL
696 * @param cls the plugin
697 * @param address the address
698 * @return always NULL
700 static struct Session *
701 http_server_plugin_get_session (void *cls,
702 const struct GNUNET_HELLO_Address *address)
709 * Deleting the session
710 * Must not be used afterwards
712 * @param s the session to delete
715 server_delete_session (struct Session *s)
717 struct HTTP_Server_Plugin *plugin = s->plugin;
718 server_stop_session_timeout(s);
720 GNUNET_CONTAINER_DLL_remove (plugin->head, plugin->tail, s);
721 struct HTTP_Message *msg = s->msg_head;
722 struct HTTP_Message *tmp = NULL;
728 GNUNET_CONTAINER_DLL_remove (s->msg_head, s->msg_tail, msg);
729 if (msg->transmit_cont != NULL)
731 msg->transmit_cont (msg->transmit_cont_cls, &s->target, GNUNET_SYSERR,
732 msg->size, msg->pos + msg->overhead);
738 if (s->msg_tk != NULL)
740 GNUNET_SERVER_mst_destroy (s->msg_tk);
743 GNUNET_free (s->addr);
744 GNUNET_free_non_null (s->server_recv);
745 GNUNET_free_non_null (s->server_send);
746 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
747 "Session %p destroyed\n", s);
753 * Cancel timeout for session s
755 * @param s the session
758 server_stop_session_timeout (struct Session *s)
760 GNUNET_assert (NULL != s);
762 if (GNUNET_SCHEDULER_NO_TASK != s->timeout_task)
764 GNUNET_SCHEDULER_cancel (s->timeout_task);
765 s->timeout_task = GNUNET_SCHEDULER_NO_TASK;
766 GNUNET_log (TIMEOUT_LOG, "Timeout stopped for session %p\n", s);
772 * Function that queries MHD's select sets and
773 * starts the task waiting for them.
774 * @param plugin plugin
775 * @param daemon_handle the MHD daemon handle
776 * @param now schedule immediately
777 * @return gnunet task identifier
779 static GNUNET_SCHEDULER_TaskIdentifier
780 server_schedule (struct HTTP_Server_Plugin *plugin,
781 struct MHD_Daemon *daemon_handle,
786 * Reschedule the execution of both IPv4 and IPv6 server
787 * @param plugin the plugin
788 * @param server which server to schedule v4 or v6?
789 * @param now GNUNET_YES to schedule execution immediately, GNUNET_NO to wait
793 server_reschedule (struct HTTP_Server_Plugin *plugin, struct MHD_Daemon *server,
796 if ((server == plugin->server_v4) && (plugin->server_v4 != NULL))
798 if (GNUNET_YES == plugin->server_v4_immediately)
799 return; /* No rescheduling, server will run asap */
801 if (GNUNET_YES == now)
802 plugin->server_v4_immediately = GNUNET_YES;
804 if (plugin->server_v4_task != GNUNET_SCHEDULER_NO_TASK)
806 GNUNET_SCHEDULER_cancel (plugin->server_v4_task);
807 plugin->server_v4_task = GNUNET_SCHEDULER_NO_TASK;
809 plugin->server_v4_task = server_schedule (plugin, plugin->server_v4, now);
812 if ((server == plugin->server_v6) && (plugin->server_v6 != NULL))
814 if (GNUNET_YES == plugin->server_v6_immediately)
815 return; /* No rescheduling, server will run asap */
817 if (GNUNET_YES == now)
818 plugin->server_v6_immediately = GNUNET_YES;
820 if (plugin->server_v6_task != GNUNET_SCHEDULER_NO_TASK)
822 GNUNET_SCHEDULER_cancel (plugin->server_v6_task);
823 plugin->server_v6_task = GNUNET_SCHEDULER_NO_TASK;
825 plugin->server_v6_task = server_schedule (plugin, plugin->server_v6, now);
831 * Disconnect session s
833 * @param s the session
834 * @return GNUNET_OK on success
837 server_disconnect (struct Session *s)
839 struct ServerConnection * send = NULL;
840 struct ServerConnection * recv = NULL;
842 if (GNUNET_NO == server_exist_session (p, s))
845 return GNUNET_SYSERR;
848 send = (struct ServerConnection *) s->server_send;
849 if (s->server_send != NULL)
851 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, s->plugin->name,
852 "Server: %p / %p Terminating inbound PUT session to peer `%s'\n",
853 s, s->server_send, GNUNET_i2s (&s->target));
855 send->disconnect = GNUNET_YES;
856 #if MHD_VERSION >= 0x00090E00
857 MHD_set_connection_option (send->mhd_conn, MHD_CONNECTION_OPTION_TIMEOUT,
860 server_reschedule (s->plugin, send->mhd_daemon, GNUNET_YES);
863 recv = (struct ServerConnection *) s->server_recv;
866 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, s->plugin->name,
867 "Server: %p / %p Terminating inbound GET session to peer `%s'\n",
868 s, s->server_recv, GNUNET_i2s (&s->target));
870 recv->disconnect = GNUNET_YES;
871 #if MHD_VERSION >= 0x00090E00
872 MHD_set_connection_option (recv->mhd_conn, MHD_CONNECTION_OPTION_TIMEOUT,
875 server_reschedule (s->plugin, recv->mhd_daemon, GNUNET_YES);
881 server_mhd_connection_timeout (struct HTTP_Server_Plugin *plugin, struct Session *s, int to)
883 #if MHD_VERSION >= 0x00090E00
884 /* Setting timeouts for other connections */
885 if (s->server_recv != NULL)
887 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
888 "Setting timeout for %p to %u sec.\n", s->server_recv, to);
889 MHD_set_connection_option (s->server_recv->mhd_conn,
890 MHD_CONNECTION_OPTION_TIMEOUT,
892 server_reschedule (plugin, s->server_recv->mhd_daemon, GNUNET_NO);
894 if (s->server_send != NULL)
896 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
897 "Setting timeout for %p to %u sec.\n", s->server_send, to);
898 MHD_set_connection_option (s->server_send->mhd_conn,
899 MHD_CONNECTION_OPTION_TIMEOUT,
901 server_reschedule (plugin, s->server_send->mhd_daemon, GNUNET_NO);
907 * Parse incoming URL for tag and target
909 * @param plugin plugin
910 * @param url incoming url
911 * @param target where to store the target
912 * @param tag where to store the tag
913 * @return GNUNET_OK on success, GNUNET_SYSERR on error
917 server_parse_url (struct HTTP_Server_Plugin *plugin, const char * url, struct GNUNET_PeerIdentity * target, uint32_t *tag)
919 char * tag_start = NULL;
920 char * tag_end = NULL;
921 char * target_start = NULL;
922 char * separator = NULL;
923 char hash[plugin->peer_id_length+1];
925 unsigned long int ctag;
928 * URL is valid if it is in the form [prefix with (multiple) '/'][peerid[103];tag]*/
933 return GNUNET_SYSERR;
938 separator = strrchr (url, ';');
940 if (NULL == separator)
942 if (DEBUG_URL_PARSE) GNUNET_break (0);
943 return GNUNET_SYSERR;
945 tag_start = separator + 1;
947 if (strlen (tag_start) == 0)
949 /* No tag after separator */
950 if (DEBUG_URL_PARSE) GNUNET_break (0);
951 return GNUNET_SYSERR;
953 ctag = strtoul (tag_start, &tag_end, 10);
956 /* tag == 0 , invalid */
957 if (DEBUG_URL_PARSE) GNUNET_break (0);
958 return GNUNET_SYSERR;
960 if ((ctag == ULONG_MAX) && (ERANGE == errno))
962 /* out of range: > ULONG_MAX */
963 if (DEBUG_URL_PARSE) GNUNET_break (0);
964 return GNUNET_SYSERR;
966 if (ctag > UINT32_MAX)
968 /* out of range: > UINT32_MAX */
969 if (DEBUG_URL_PARSE) GNUNET_break (0);
970 return GNUNET_SYSERR;
972 (*tag) = (uint32_t) ctag;
975 /* no char after tag */
976 if (DEBUG_URL_PARSE) GNUNET_break (0);
977 return GNUNET_SYSERR;
979 if (url[strlen(url)] != tag_end[0])
981 /* there are more not converted chars after tag */
982 if (DEBUG_URL_PARSE) GNUNET_break (0);
983 return GNUNET_SYSERR;
986 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
987 "Found tag `%u' in url\n", (*tag));
989 /* convert peer id */
990 target_start = strrchr (url, '/');
991 if (NULL == target_start)
994 target_start = (char *) url;
997 hash_length = separator - target_start;
998 if (hash_length != plugin->peer_id_length)
1000 /* no char after tag */
1001 if (DEBUG_URL_PARSE) GNUNET_break (0);
1002 return GNUNET_SYSERR;
1004 memcpy (hash, target_start, hash_length);
1005 hash[hash_length] = '\0';
1007 if (GNUNET_OK != GNUNET_CRYPTO_hash_from_string ((const char *) hash, &(target->hashPubKey)))
1009 /* hash conversion failed */
1010 if (DEBUG_URL_PARSE) GNUNET_break (0);
1011 return GNUNET_SYSERR;
1014 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1015 "Found target `%s' in url\n", GNUNET_h2s_full(&target->hashPubKey));
1021 * Lookup a mhd connection and create one if none is found
1023 * @param plugin the plugin handle
1024 * @param mhd_connection the incoming mhd_connection
1025 * @param url incoming requested URL
1026 * @param method PUT or GET
1027 * @return the server connecetion
1029 static struct ServerConnection *
1030 server_lookup_connection (struct HTTP_Server_Plugin *plugin,
1031 struct MHD_Connection *mhd_connection, const char *url,
1034 struct Session *s = NULL;
1035 struct ServerConnection *sc = NULL;
1036 const union MHD_ConnectionInfo *conn_info;
1037 struct GNUNET_ATS_Information ats;
1039 struct HttpAddress *addr;
1042 struct GNUNET_PeerIdentity target;
1044 int direction = GNUNET_SYSERR;
1047 conn_info = MHD_get_connection_info (mhd_connection,
1048 MHD_CONNECTION_INFO_CLIENT_ADDRESS);
1049 if ((conn_info->client_addr->sa_family != AF_INET) &&
1050 (conn_info->client_addr->sa_family != AF_INET6))
1052 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1053 "New %s connection from %s\n", method, url);
1055 if (GNUNET_SYSERR == server_parse_url (plugin, url, &target, &tag))
1057 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1058 "Invalid url %s\n", url);
1061 if (0 == strcmp (MHD_HTTP_METHOD_PUT, method))
1062 direction = _RECEIVE;
1063 else if (0 == strcmp (MHD_HTTP_METHOD_GET, method))
1067 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1068 "Invalid method %s connection from %s\n", method, url);
1072 plugin->cur_connections++;
1073 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1074 "New %s connection from %s with tag %u (%u of %u)\n",
1076 GNUNET_i2s (&target), tag,
1077 plugin->cur_connections, plugin->max_connections);
1078 /* find duplicate session */
1082 if ((0 == memcmp (&s->target, &target, sizeof (struct GNUNET_PeerIdentity))) &&
1089 if ((_RECEIVE == direction) && (NULL != s->server_recv))
1091 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1092 "Duplicate PUT connection from `%s' tag %u, dismissing new connection\n",
1093 GNUNET_i2s (&target),
1098 if ((_SEND == direction) && (NULL != s->server_send))
1100 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1101 "Duplicate GET connection from `%s' tag %u, dismissing new connection\n",
1102 GNUNET_i2s (&target),
1109 /* create new session */
1110 switch (conn_info->client_addr->sa_family)
1113 addr = http_common_address_from_socket (plugin->protocol, conn_info->client_addr, sizeof (struct sockaddr_in));
1114 addr_len = http_common_address_get_size (addr);
1115 ats = plugin->env->get_address_type (plugin->env->cls, conn_info->client_addr, sizeof (struct sockaddr_in));
1118 addr = http_common_address_from_socket (plugin->protocol, conn_info->client_addr, sizeof (struct sockaddr_in6));
1119 addr_len = http_common_address_get_size (addr);
1120 ats = plugin->env->get_address_type (plugin->env->cls, conn_info->client_addr, sizeof (struct sockaddr_in6));
1123 /* external host name */
1124 ats.type = htonl (GNUNET_ATS_NETWORK_TYPE);
1125 ats.type = htonl (GNUNET_ATS_NET_WAN);
1129 s = GNUNET_malloc (sizeof (struct Session));
1130 memcpy (&s->target, &target, sizeof (struct GNUNET_PeerIdentity));
1133 s->addrlen = addr_len;
1134 s->ats_address_network_type = ats.value;
1135 s->next_receive = GNUNET_TIME_UNIT_ZERO_ABS;
1137 s->server_recv = NULL;
1138 s->server_send = NULL;
1139 s->session_passed = GNUNET_NO;
1140 s->session_ended = GNUNET_NO;
1141 s->connect_in_progress = GNUNET_YES;
1142 server_start_session_timeout(s);
1143 GNUNET_CONTAINER_DLL_insert (plugin->head, plugin->tail, s);
1145 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1146 "Creating new session %p for peer `%s' connecting from `%s'\n",
1147 s, GNUNET_i2s (&target),
1148 http_common_plugin_address_to_string (NULL, p->protocol, addr, addr_len));
1150 sc = GNUNET_malloc (sizeof (struct ServerConnection));
1151 if (conn_info->client_addr->sa_family == AF_INET)
1152 sc->mhd_daemon = plugin->server_v4;
1153 if (conn_info->client_addr->sa_family == AF_INET6)
1154 sc->mhd_daemon = plugin->server_v6;
1155 sc->mhd_conn = mhd_connection;
1156 sc->direction = direction;
1157 sc->connected = GNUNET_NO;
1159 if (direction == _SEND)
1160 s->server_send = sc;
1161 if (direction == _RECEIVE)
1162 s->server_recv = sc;
1164 if ((NULL != s->server_send) && (NULL != s->server_recv))
1166 s->connect_in_progress = GNUNET_NO; /* PUT and GET are connected */
1167 plugin->env->session_start (NULL, &s->target, PLUGIN_NAME, NULL, 0 ,s, NULL, 0);
1170 #if MHD_VERSION >= 0x00090E00
1171 if ((NULL == s->server_recv) || (NULL == s->server_send))
1173 to = (HTTP_SERVER_NOT_VALIDATED_TIMEOUT.rel_value / 1000);
1174 MHD_set_connection_option (mhd_connection, MHD_CONNECTION_OPTION_TIMEOUT, to);
1175 server_reschedule (plugin, sc->mhd_daemon, GNUNET_NO);
1179 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1180 "Session %p for peer `%s' fully connected\n",
1181 s, GNUNET_i2s (&target));
1182 to = (SERVER_SESSION_TIMEOUT.rel_value / 1000);
1183 server_mhd_connection_timeout (plugin, s, to);
1186 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1187 "Setting timeout for %p to %u sec.\n", sc, to);
1194 * Lookup a session for a server connection
1196 * @param plugin the plugin
1197 * @param sc the server connection
1198 * @return the session found or NULL
1200 static struct Session *
1201 server_lookup_session (struct HTTP_Server_Plugin *plugin,
1202 struct ServerConnection * sc)
1206 for (s = plugin->head; NULL != s; s = s->next)
1207 if ((s->server_recv == sc) || (s->server_send == sc))
1213 server_exist_session (struct HTTP_Server_Plugin *plugin, struct Session *s)
1215 struct Session * head;
1217 GNUNET_assert (NULL != plugin);
1218 GNUNET_assert (NULL != s);
1220 for (head = plugin->head; head != NULL; head = head->next)
1230 * Callback called by MHD when it needs data to send
1232 * @param cls current session
1233 * @param pos position in buffer
1234 * @param buf the buffer to write data to
1235 * @param max max number of bytes available in buffer
1236 * @return bytes written to buffer
1239 server_send_callback (void *cls, uint64_t pos, char *buf, size_t max)
1241 struct Session *s = cls;
1242 ssize_t bytes_read = 0;
1243 struct HTTP_Message *msg;
1246 GNUNET_assert (NULL != p);
1247 if (GNUNET_NO == server_exist_session (p, s))
1253 bytes_read = GNUNET_MIN (msg->size - msg->pos,
1255 memcpy (buf, &msg->buf[msg->pos], bytes_read);
1256 msg->pos += bytes_read;
1258 /* removing message */
1259 if (msg->pos == msg->size)
1261 GNUNET_CONTAINER_DLL_remove (s->msg_head, s->msg_tail, msg);
1262 if (NULL != msg->transmit_cont)
1263 msg->transmit_cont (msg->transmit_cont_cls, &s->target, GNUNET_OK,
1264 msg->size, msg->size + msg->overhead);
1270 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, s->plugin->name,
1271 "Sent %u bytes to peer `%s' with session %p \n", bytes_read, GNUNET_i2s (&s->target), s);
1272 GNUNET_asprintf (&stat_txt, "# bytes currently in %s_server buffers", p->protocol);
1273 GNUNET_STATISTICS_update (p->env->stats,
1274 stat_txt, -bytes_read, GNUNET_NO);
1275 GNUNET_free (stat_txt);
1276 GNUNET_asprintf (&stat_txt, "# bytes transmitted via %s_server", p->protocol);
1277 GNUNET_STATISTICS_update (p->env->stats,
1278 stat_txt, bytes_read, GNUNET_NO);
1279 GNUNET_free (stat_txt);
1286 * Callback called by MessageStreamTokenizer when a message has arrived
1288 * @param cls current session as closure
1289 * @param client client
1290 * @param message the message to be forwarded to transport service
1294 server_receive_mst_cb (void *cls, void *client,
1295 const struct GNUNET_MessageHeader *message)
1297 struct Session *s = cls;
1298 struct HTTP_Server_Plugin *plugin = s->plugin;
1299 struct GNUNET_ATS_Information atsi;
1300 struct GNUNET_TIME_Relative delay;
1303 GNUNET_assert (NULL != p);
1304 if (GNUNET_NO == server_exist_session(p, s))
1308 atsi.type = htonl (GNUNET_ATS_NETWORK_TYPE);
1309 atsi.value = s->ats_address_network_type;
1310 GNUNET_break (s->ats_address_network_type != ntohl (GNUNET_ATS_NET_UNSPECIFIED));
1313 delay = plugin->env->receive (plugin->env->cls,
1318 plugin->env->update_address_metrics (plugin->env->cls,
1320 NULL, 0, s, &atsi, 1);
1322 GNUNET_asprintf (&stat_txt, "# bytes received via %s_server", plugin->protocol);
1323 GNUNET_STATISTICS_update (plugin->env->stats,
1324 stat_txt, ntohs (message->size), GNUNET_NO);
1325 GNUNET_free (stat_txt);
1327 s->session_passed = GNUNET_YES;
1328 s->next_receive = GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (), delay);
1329 if (delay.rel_value > 0)
1331 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1332 "Peer `%s' address `%s' next read delayed for %llu ms\n",
1333 GNUNET_i2s (&s->target),
1334 http_common_plugin_address_to_string (NULL, p->protocol, s->addr, s->addrlen),
1337 server_reschedule_session_timeout (s);
1343 * MHD callback for a new incoming connection
1345 * @param cls the plugin handle
1346 * @param mhd_connection the mhd connection
1347 * @param url the requested URL
1348 * @param method GET or PUT
1349 * @param version HTTP version
1350 * @param upload_data upload data
1351 * @param upload_data_size sizeof upload data
1352 * @param httpSessionCache the session cache to remember the connection
1353 * @return MHD_YES if connection is accepted, MHD_NO on reject
1356 server_access_cb (void *cls, struct MHD_Connection *mhd_connection,
1357 const char *url, const char *method, const char *version,
1358 const char *upload_data, size_t * upload_data_size,
1359 void **httpSessionCache)
1361 struct HTTP_Server_Plugin *plugin = cls;
1364 struct ServerConnection *sc = *httpSessionCache;
1366 struct MHD_Response *response;
1368 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1369 _("Access from connection %p (%u of %u) for `%s' `%s' url `%s' with upload data size %u\n"),
1371 plugin->cur_connections, plugin->max_connections,
1372 method, version, url, (*upload_data_size));
1374 GNUNET_assert (cls != NULL);
1377 /* new connection */
1378 sc = server_lookup_connection (plugin, mhd_connection, url, method);
1381 (*httpSessionCache) = sc;
1385 response = MHD_create_response_from_data (strlen (HTTP_ERROR_RESPONSE), HTTP_ERROR_RESPONSE, MHD_NO, MHD_NO);
1386 MHD_add_response_header (response,
1387 MHD_HTTP_HEADER_CONTENT_TYPE,
1389 res = MHD_queue_response (mhd_connection, MHD_HTTP_NOT_FOUND, response);
1390 MHD_destroy_response (response);
1396 /* 'old' connection */
1397 if (NULL == server_lookup_session (plugin, sc))
1399 /* Session was already disconnected */
1404 /* existing connection */
1405 sc = (*httpSessionCache);
1407 GNUNET_assert (NULL != s);
1408 /* connection is to be disconnected */
1409 if (sc->disconnect == GNUNET_YES)
1411 /* Sent HTTP/1.1: 200 OK as response */
1412 response = MHD_create_response_from_data (strlen ("Thank you!"),
1415 res = MHD_queue_response (mhd_connection, MHD_HTTP_OK, response);
1416 MHD_destroy_response (response);
1419 GNUNET_assert (s != NULL);
1421 if (sc->direction == _SEND)
1423 response = MHD_create_response_from_callback (MHD_SIZE_UNKNOWN,
1425 &server_send_callback, s,
1427 MHD_queue_response (mhd_connection, MHD_HTTP_OK, response);
1428 MHD_destroy_response (response);
1431 if (sc->direction == _RECEIVE)
1433 if ((*upload_data_size == 0) && (sc->connected == GNUNET_NO))
1435 /* (*upload_data_size == 0) first callback when header are passed */
1436 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1437 "Session %p / Connection %p: Peer `%s' PUT on address `%s' connected\n",
1439 GNUNET_i2s (&s->target),
1440 http_common_plugin_address_to_string (NULL,
1444 sc->connected = GNUNET_YES;
1447 else if ((*upload_data_size == 0) && (sc->connected == GNUNET_YES))
1449 /* (*upload_data_size == 0) when upload is complete */
1450 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1451 "Session %p / Connection %p: Peer `%s' PUT on address `%s' finished upload\n",
1453 GNUNET_i2s (&s->target),
1454 http_common_plugin_address_to_string (NULL,
1458 sc->connected = GNUNET_NO;
1459 /* Sent HTTP/1.1: 200 OK as PUT Response\ */
1460 response = MHD_create_response_from_data (strlen ("Thank you!"),
1463 res = MHD_queue_response (mhd_connection, MHD_HTTP_OK, response);
1464 MHD_destroy_response (response);
1467 else if ((*upload_data_size > 0) && (sc->connected == GNUNET_YES))
1469 /* (*upload_data_size > 0) for every segment received */
1470 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1471 "Session %p / Connection %p: Peer `%s' PUT on address `%s' received %u bytes\n",
1473 GNUNET_i2s (&s->target),
1474 http_common_plugin_address_to_string (NULL,
1479 struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get ();
1481 if ((s->next_receive.abs_value <= now.abs_value))
1483 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1484 "PUT with %u bytes forwarded to MST\n",
1486 if (s->msg_tk == NULL)
1488 s->msg_tk = GNUNET_SERVER_mst_create (&server_receive_mst_cb, s);
1490 GNUNET_SERVER_mst_receive (s->msg_tk, s, upload_data,
1491 *upload_data_size, GNUNET_NO, GNUNET_NO);
1492 #if MHD_VERSION >= 0x00090E00
1493 server_mhd_connection_timeout (plugin, s, GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT.rel_value / 1000);
1495 (*upload_data_size) = 0;
1499 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1500 "Session %p / Connection %p: no inbound bandwidth available! Next read was delayed by %llu ms\n",
1501 s, sc, now.abs_value - s->next_receive.abs_value);
1516 * Callback from MHD when a connection disconnects
1518 * @param cls closure
1519 * @param connection the disconnected MHD connection
1520 * @param httpSessionCache the pointer to distinguish
1523 server_disconnect_cb (void *cls, struct MHD_Connection *connection,
1524 void **httpSessionCache)
1526 struct ServerConnection *sc = *httpSessionCache;
1527 struct Session *s = NULL;
1528 struct Session *t = NULL;
1529 struct HTTP_Server_Plugin *plugin = NULL;
1531 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, p->name,
1532 "Disconnect for connection %p \n", sc);
1537 if (NULL == (s = server_lookup_session (p, sc)))
1540 GNUNET_assert (NULL != p);
1541 for (t = p->head; t != NULL; t = t->next)
1550 if (sc->direction == _SEND)
1553 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1554 "Peer `%s' connection %p, GET on address `%s' disconnected\n",
1555 GNUNET_i2s (&s->target), s->server_send,
1556 http_common_plugin_address_to_string (NULL, p->protocol, s->addr, s->addrlen));
1557 s->server_send = NULL;
1558 if (NULL != (s->server_recv))
1560 s->server_recv->disconnect = GNUNET_YES;
1561 GNUNET_assert (NULL != s->server_recv->mhd_conn);
1562 #if MHD_VERSION >= 0x00090E00
1563 MHD_set_connection_option (s->server_recv->mhd_conn, MHD_CONNECTION_OPTION_TIMEOUT,
1566 server_reschedule (plugin, s->server_recv->mhd_daemon, GNUNET_NO);
1569 if (sc->direction == _RECEIVE)
1571 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1572 "Peer `%s' connection %p PUT on address `%s' disconnected\n",
1573 GNUNET_i2s (&s->target), s->server_recv,
1574 http_common_plugin_address_to_string (NULL, p->protocol, s->addr, s->addrlen));
1575 s->server_recv = NULL;
1576 /* Do not terminate session when PUT disconnects
1577 if (NULL != (s->server_send))
1579 s->server_send->disconnect = GNUNET_YES;
1580 GNUNET_assert (NULL != s->server_send->mhd_conn);
1581 #if MHD_VERSION >= 0x00090E00
1582 MHD_set_connection_option (s->server_send->mhd_conn, MHD_CONNECTION_OPTION_TIMEOUT,
1585 server_reschedule (plugin, s->server_send->mhd_daemon, GNUNET_NO);
1587 if (s->msg_tk != NULL)
1589 GNUNET_SERVER_mst_destroy (s->msg_tk);
1595 plugin->cur_connections--;
1597 if ((s->server_send == NULL) && (s->server_recv == NULL))
1599 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1600 "Peer `%s' on address `%s' disconnected\n",
1601 GNUNET_i2s (&s->target),
1602 http_common_plugin_address_to_string (NULL, p->protocol, s->addr, s->addrlen));
1604 if ((GNUNET_YES == s->session_passed) && (GNUNET_NO == s->session_ended))
1606 /* Notify transport immediately that this session is invalid */
1607 s->session_ended = GNUNET_YES;
1608 plugin->env->session_end (plugin->env->cls, &s->target, s);
1610 server_delete_session (s);
1617 * Check if incoming connection is accepted.
1619 * @param cls plugin as closure
1620 * @param addr address of incoming connection
1621 * @param addr_len address length of incoming connection
1622 * @return MHD_YES if connection is accepted, MHD_NO if connection is rejected
1626 server_accept_cb (void *cls, const struct sockaddr *addr, socklen_t addr_len)
1628 struct HTTP_Server_Plugin *plugin = cls;
1630 if (plugin->cur_connections <= plugin->max_connections)
1632 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1633 _("Accepting connection (%u of %u) from `%s'\n"),
1634 plugin->cur_connections, plugin->max_connections,
1635 GNUNET_a2s (addr, addr_len));
1640 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, plugin->name,
1641 _("Server reached maximum number connections (%u), rejecting new connection\n"),
1642 plugin->max_connections);
1648 server_log (void *arg, const char *fmt, va_list ap)
1652 vsnprintf (text, sizeof (text), fmt, ap);
1654 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Server: %s\n", text);
1659 * Call MHD IPv4 to process pending requests and then go back
1660 * and schedule the next run.
1661 * @param cls plugin as closure
1662 * @param tc task context
1665 server_v4_run (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1667 struct HTTP_Server_Plugin *plugin = cls;
1669 GNUNET_assert (cls != NULL);
1671 plugin->server_v4_task = GNUNET_SCHEDULER_NO_TASK;
1672 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1675 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1676 "Running IPv4 server\n");
1678 plugin->server_v4_immediately = GNUNET_NO;
1679 GNUNET_assert (MHD_YES == MHD_run (plugin->server_v4));
1680 server_reschedule (plugin, plugin->server_v4, GNUNET_NO);
1685 * Call MHD IPv6 to process pending requests and then go back
1686 * and schedule the next run.
1687 * @param cls plugin as closure
1688 * @param tc task context
1691 server_v6_run (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1693 struct HTTP_Server_Plugin *plugin = cls;
1695 GNUNET_assert (cls != NULL);
1696 plugin->server_v6_task = GNUNET_SCHEDULER_NO_TASK;
1697 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1700 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1701 "Running IPv6 server\n");
1703 plugin->server_v6_immediately = GNUNET_NO;
1704 GNUNET_assert (MHD_YES == MHD_run (plugin->server_v6));
1705 server_reschedule (plugin, plugin->server_v6, GNUNET_NO);
1709 #define UNSIGNED_MHD_LONG_LONG unsigned MHD_LONG_LONG
1712 * Function that queries MHD's select sets and
1713 * starts the task waiting for them.
1715 * @param plugin plugin
1716 * @param daemon_handle the MHD daemon handle
1717 * @return gnunet task identifier
1719 static GNUNET_SCHEDULER_TaskIdentifier
1720 server_schedule (struct HTTP_Server_Plugin *plugin,
1721 struct MHD_Daemon *daemon_handle,
1724 GNUNET_SCHEDULER_TaskIdentifier ret;
1728 struct GNUNET_NETWORK_FDSet *wrs;
1729 struct GNUNET_NETWORK_FDSet *wws;
1730 struct GNUNET_NETWORK_FDSet *wes;
1732 UNSIGNED_MHD_LONG_LONG timeout;
1733 static unsigned long long last_timeout = 0;
1736 struct GNUNET_TIME_Relative tv;
1738 if (GNUNET_YES == plugin->in_shutdown)
1739 return GNUNET_SCHEDULER_NO_TASK;
1741 ret = GNUNET_SCHEDULER_NO_TASK;
1745 wrs = GNUNET_NETWORK_fdset_create ();
1746 wes = GNUNET_NETWORK_fdset_create ();
1747 wws = GNUNET_NETWORK_fdset_create ();
1749 GNUNET_assert (MHD_YES == MHD_get_fdset (daemon_handle, &rs, &ws, &es, &max));
1750 haveto = MHD_get_timeout (daemon_handle, &timeout);
1751 if (haveto == MHD_YES)
1753 if (timeout != last_timeout)
1756 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1757 "SELECT Timeout changed from %llu to %llu\n",
1758 last_timeout, timeout);
1759 last_timeout = timeout;
1761 if (timeout <= GNUNET_TIME_UNIT_SECONDS.rel_value)
1762 tv.rel_value = (uint64_t) timeout;
1764 tv = GNUNET_TIME_UNIT_SECONDS;
1767 tv = GNUNET_TIME_UNIT_SECONDS;
1768 /* Force immediate run, since we have outbound data to send */
1769 if (now == GNUNET_YES)
1770 tv = GNUNET_TIME_UNIT_MILLISECONDS;
1771 GNUNET_NETWORK_fdset_copy_native (wrs, &rs, max + 1);
1772 GNUNET_NETWORK_fdset_copy_native (wws, &ws, max + 1);
1773 GNUNET_NETWORK_fdset_copy_native (wes, &es, max + 1);
1775 if (daemon_handle == plugin->server_v4)
1777 if (plugin->server_v4_task != GNUNET_SCHEDULER_NO_TASK)
1779 GNUNET_SCHEDULER_cancel (plugin->server_v4_task);
1780 plugin->server_v4_task = GNUNET_SCHEDULER_NO_TASK;
1783 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1784 "Scheduling IPv4 server task in %llu ms\n", tv);
1787 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1789 &server_v4_run, plugin);
1791 if (daemon_handle == plugin->server_v6)
1793 if (plugin->server_v6_task != GNUNET_SCHEDULER_NO_TASK)
1795 GNUNET_SCHEDULER_cancel (plugin->server_v6_task);
1796 plugin->server_v6_task = GNUNET_SCHEDULER_NO_TASK;
1799 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1800 "Scheduling IPv6 server task in %llu ms\n", tv);
1803 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1805 &server_v6_run, plugin);
1807 GNUNET_NETWORK_fdset_destroy (wrs);
1808 GNUNET_NETWORK_fdset_destroy (wws);
1809 GNUNET_NETWORK_fdset_destroy (wes);
1816 * Load ssl certificate from file
1818 * @param file filename
1819 * @return content of the file
1822 server_load_file (const char *file)
1824 struct GNUNET_DISK_FileHandle *gn_file;
1828 if (GNUNET_OK != GNUNET_DISK_file_size (file,
1829 &fsize, GNUNET_NO, GNUNET_YES))
1831 text = GNUNET_malloc (fsize + 1);
1833 GNUNET_DISK_file_open (file, GNUNET_DISK_OPEN_READ,
1834 GNUNET_DISK_PERM_USER_READ);
1835 if (gn_file == NULL)
1840 if (GNUNET_SYSERR == GNUNET_DISK_file_read (gn_file, text, fsize))
1843 GNUNET_DISK_file_close (gn_file);
1847 GNUNET_DISK_file_close (gn_file);
1855 * Load ssl certificate
1857 * @param plugin the plugin
1858 * @return GNUNET_OK on success, GNUNET_SYSERR on failure
1861 server_load_certificate (struct HTTP_Server_Plugin *plugin)
1863 int res = GNUNET_OK;
1869 /* Get crypto init string from config
1870 * If not present just use default values */
1873 GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg,
1878 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
1879 "Failed to get servicehome!\n");
1880 return GNUNET_SYSERR;
1885 GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg,
1888 &plugin->crypto_init))
1889 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1890 "Using crypto init string `%s'\n",
1891 plugin->crypto_init);
1893 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1894 "Using default crypto init string \n");
1897 GNUNET_CONFIGURATION_get_value_filename (plugin->env->cfg, plugin->name,
1898 "KEY_FILE", &key_file))
1901 GNUNET_asprintf (&key_file, "%s/%s", sh, "https_key.key");
1906 GNUNET_CONFIGURATION_get_value_filename (plugin->env->cfg, plugin->name,
1907 "CERT_FILE", &cert_file))
1910 GNUNET_asprintf (&cert_file, "%s/%s", sh, "https_cert.crt");
1913 /* read key & certificates from file */
1914 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1915 "Trying to loading TLS certificate from key-file `%s' cert-file`%s'\n",
1916 key_file, cert_file);
1918 plugin->key = server_load_file (key_file);
1919 plugin->cert = server_load_file (cert_file);
1921 if ((plugin->key == NULL) || (plugin->cert == NULL))
1923 struct GNUNET_OS_Process *cert_creation;
1925 GNUNET_free_non_null (plugin->key);
1927 GNUNET_free_non_null (plugin->cert);
1928 plugin->cert = NULL;
1930 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1931 "No usable TLS certificate found, creating certificate\n");
1934 GNUNET_OS_start_process (GNUNET_NO, GNUNET_OS_INHERIT_STD_OUT_AND_ERR, NULL, NULL,
1935 "gnunet-transport-certificate-creation",
1936 "gnunet-transport-certificate-creation",
1937 key_file, cert_file, NULL);
1938 if (cert_creation == NULL)
1940 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
1942 ("Could not create a new TLS certificate, program `gnunet-transport-certificate-creation' could not be started!\n"));
1943 GNUNET_free (key_file);
1944 GNUNET_free (cert_file);
1946 GNUNET_free_non_null (plugin->key);
1948 GNUNET_free_non_null (plugin->cert);
1949 plugin->cert = NULL;
1950 GNUNET_free_non_null (plugin->crypto_init);
1951 plugin->crypto_init = NULL;
1953 return GNUNET_SYSERR;
1955 GNUNET_assert (GNUNET_OK == GNUNET_OS_process_wait (cert_creation));
1956 GNUNET_OS_process_destroy (cert_creation);
1958 plugin->key = server_load_file (key_file);
1959 plugin->cert = server_load_file (cert_file);
1962 if ((plugin->key == NULL) || (plugin->cert == NULL))
1964 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
1966 ("No usable TLS certificate found and creating one failed!\n"),
1968 GNUNET_free (key_file);
1969 GNUNET_free (cert_file);
1971 GNUNET_free_non_null (plugin->key);
1973 GNUNET_free_non_null (plugin->cert);
1974 plugin->cert = NULL;
1975 GNUNET_free_non_null (plugin->crypto_init);
1976 plugin->crypto_init = NULL;
1978 return GNUNET_SYSERR;
1980 GNUNET_free (key_file);
1981 GNUNET_free (cert_file);
1982 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "TLS certificate loaded\n");
1989 * Start the HTTP server
1991 * @param plugin the plugin handle
1992 * @return GNUNET_OK on success, GNUNET_SYSERR on failure
1995 server_start (struct HTTP_Server_Plugin *plugin)
1997 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 / 1000;
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 = SERVER_SESSION_TIMEOUT.rel_value / 1000;
2017 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, plugin->name,
2018 "MHD cannot set timeout per connection! Default time out %u sec.\n",
2021 plugin->server_v4 = NULL;
2022 if (plugin->use_ipv4 == GNUNET_YES)
2024 plugin->server_v4 = MHD_start_daemon (
2031 MHD_NO_FLAG, plugin->port,
2032 &server_accept_cb, plugin,
2033 &server_access_cb, plugin,
2034 MHD_OPTION_SOCK_ADDR,
2035 (struct sockaddr_in *)
2036 plugin->server_addr_v4,
2037 MHD_OPTION_CONNECTION_LIMIT,
2039 plugin->max_connections,
2041 MHD_OPTION_HTTPS_PRIORITIES,
2042 plugin->crypto_init,
2043 MHD_OPTION_HTTPS_MEM_KEY,
2045 MHD_OPTION_HTTPS_MEM_CERT,
2048 MHD_OPTION_CONNECTION_TIMEOUT,
2050 MHD_OPTION_CONNECTION_MEMORY_LIMIT,
2052 GNUNET_SERVER_MAX_MESSAGE_SIZE),
2053 MHD_OPTION_NOTIFY_COMPLETED,
2054 &server_disconnect_cb, plugin,
2055 MHD_OPTION_EXTERNAL_LOGGER,
2056 server_log, NULL, MHD_OPTION_END);
2058 plugin->server_v6 = NULL;
2059 if (plugin->use_ipv6 == GNUNET_YES)
2061 plugin->server_v6 = MHD_start_daemon (
2068 MHD_USE_IPv6, plugin->port,
2069 &server_accept_cb, plugin,
2070 &server_access_cb, plugin,
2071 MHD_OPTION_SOCK_ADDR,
2072 (struct sockaddr_in6 *)
2073 plugin->server_addr_v6,
2074 MHD_OPTION_CONNECTION_LIMIT,
2076 plugin->max_connections,
2078 MHD_OPTION_HTTPS_PRIORITIES,
2079 plugin->crypto_init,
2080 MHD_OPTION_HTTPS_MEM_KEY,
2082 MHD_OPTION_HTTPS_MEM_CERT,
2085 MHD_OPTION_CONNECTION_TIMEOUT,
2087 MHD_OPTION_CONNECTION_MEMORY_LIMIT,
2089 GNUNET_SERVER_MAX_MESSAGE_SIZE),
2090 MHD_OPTION_NOTIFY_COMPLETED,
2091 &server_disconnect_cb, plugin,
2092 MHD_OPTION_EXTERNAL_LOGGER,
2093 server_log, NULL, MHD_OPTION_END);
2097 if ((plugin->use_ipv4 == GNUNET_YES) && (plugin->server_v4 == NULL))
2099 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
2100 "Failed to start %s IPv4 server component on port %u\n",
2101 plugin->name, plugin->port);
2102 return GNUNET_SYSERR;
2104 server_reschedule (plugin, plugin->server_v4, GNUNET_NO);
2106 if ((plugin->use_ipv6 == GNUNET_YES) && (plugin->server_v6 == NULL))
2108 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
2109 "Failed to start %s IPv6 server component on port %u\n",
2110 plugin->name, plugin->port);
2111 return GNUNET_SYSERR;
2113 server_reschedule (plugin, plugin->server_v6, GNUNET_NO);
2114 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2115 "%s server component started on port %u\n", plugin->name,
2122 server_stop (struct HTTP_Server_Plugin *plugin)
2124 if (plugin->server_v4 != NULL)
2126 MHD_stop_daemon (plugin->server_v4);
2127 plugin->server_v4 = NULL;
2129 if ( plugin->server_v6 != NULL)
2131 MHD_stop_daemon (plugin->server_v6);
2132 plugin->server_v6 = NULL;
2136 if (plugin->server_v4_task != GNUNET_SCHEDULER_NO_TASK)
2138 GNUNET_SCHEDULER_cancel (plugin->server_v4_task);
2139 plugin->server_v4_task = GNUNET_SCHEDULER_NO_TASK;
2142 if (plugin->server_v6_task != GNUNET_SCHEDULER_NO_TASK)
2144 GNUNET_SCHEDULER_cancel (plugin->server_v6_task);
2145 plugin->server_v6_task = GNUNET_SCHEDULER_NO_TASK;
2150 GNUNET_free_non_null (plugin->crypto_init);
2151 GNUNET_free_non_null (plugin->cert);
2152 GNUNET_free_non_null (plugin->key);
2155 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2156 "%s server component stopped\n", plugin->name);
2161 * Add an address to the server's set of addresses and notify transport
2163 * @param cls the plugin handle
2164 * @param add_remove GNUNET_YES on add, GNUNET_NO on remove
2165 * @param addr the address
2166 * @param addrlen address length
2169 server_add_address (void *cls, int add_remove, const struct sockaddr *addr,
2172 struct HTTP_Server_Plugin *plugin = cls;
2173 struct HttpAddressWrapper *w = NULL;
2175 w = GNUNET_malloc (sizeof (struct HttpAddressWrapper));
2176 w->address = http_common_address_from_socket (plugin->protocol, addr, addrlen);
2177 if (NULL == w->address)
2182 w->addrlen = http_common_address_get_size (w->address);
2184 GNUNET_CONTAINER_DLL_insert(plugin->addr_head, plugin->addr_tail, w);
2185 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2186 "Notifying transport to add address `%s'\n",
2187 http_common_plugin_address_to_string (NULL, p->protocol, w->address, w->addrlen));
2189 plugin->env->notify_address (plugin->env->cls, add_remove, w->address, w->addrlen, "https_client");
2191 plugin->env->notify_address (plugin->env->cls, add_remove, w->address, w->addrlen, "http_client");
2197 * Remove an address from the server's set of addresses and notify transport
2199 * @param cls the plugin handle
2200 * @param add_remove GNUNET_YES on add, GNUNET_NO on remove
2201 * @param addr the address
2202 * @param addrlen address length
2205 server_remove_address (void *cls, int add_remove, const struct sockaddr *addr,
2208 struct HTTP_Server_Plugin *plugin = cls;
2209 struct HttpAddressWrapper *w = plugin->addr_head;
2211 void * saddr = http_common_address_from_socket (plugin->protocol, addr, addrlen);
2214 saddr_len = http_common_address_get_size (saddr);
2218 if (GNUNET_YES == http_common_cmp_addresses(w->address, w->addrlen, saddr, saddr_len))
2222 GNUNET_free (saddr);
2227 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2228 "Notifying transport to remove address `%s'\n",
2229 http_common_plugin_address_to_string (NULL, p->protocol, w->address, w->addrlen));
2230 GNUNET_CONTAINER_DLL_remove (plugin->addr_head, plugin->addr_tail, w);
2232 plugin->env->notify_address (plugin->env->cls, add_remove, w->address, w->addrlen, "https_client");
2234 plugin->env->notify_address (plugin->env->cls, add_remove, w->address, w->addrlen, "http_client");
2236 GNUNET_free (w->address);
2243 * Our external IP address/port mapping has changed.
2245 * @param cls closure, the 'struct LocalAddrList'
2246 * @param add_remove GNUNET_YES to mean the new public IP address, GNUNET_NO to mean
2247 * the previous (now invalid) one
2248 * @param addr either the previous or the new public IP address
2249 * @param addrlen actual lenght of the address
2252 server_nat_port_map_callback (void *cls, int add_remove, const struct sockaddr *addr,
2255 GNUNET_assert (cls != NULL);
2256 struct HTTP_Server_Plugin *plugin = cls;
2258 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2259 "NAT called to %s address `%s'\n",
2260 (add_remove == GNUNET_NO) ? "remove" : "add",
2261 GNUNET_a2s (addr, addrlen));
2263 if (AF_INET == addr->sa_family)
2265 struct sockaddr_in *s4 = (struct sockaddr_in *) addr;
2267 if (GNUNET_NO == plugin->use_ipv4)
2270 if ((NULL != plugin->server_addr_v4) &&
2271 (0 != memcmp (&plugin->server_addr_v4->sin_addr,
2272 &s4->sin_addr, sizeof (struct in_addr))))
2274 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2275 "Skipping address `%s' (not bindto address)\n",
2276 GNUNET_a2s (addr, addrlen));
2281 if (AF_INET6 == addr->sa_family)
2283 struct sockaddr_in6 *s6 = (struct sockaddr_in6 *) addr;
2284 if (GNUNET_NO == plugin->use_ipv6)
2287 if ((NULL != plugin->server_addr_v6) &&
2288 (0 != memcmp (&plugin->server_addr_v6->sin6_addr,
2289 &s6->sin6_addr, sizeof (struct in6_addr))))
2291 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2292 "Skipping address `%s' (not bindto address)\n",
2293 GNUNET_a2s (addr, addrlen));
2301 server_add_address (cls, add_remove, addr, addrlen);
2304 server_remove_address (cls, add_remove, addr, addrlen);
2311 * Get valid server addresses
2313 * @param plugin the plugin handle
2314 * @param serviceName the servicename
2315 * @param cfg configuration handle
2316 * @param addrs addresses
2317 * @param addr_lens address length
2318 * @return number of addresses
2321 server_get_addresses (struct HTTP_Server_Plugin *plugin,
2322 const char *serviceName,
2323 const struct GNUNET_CONFIGURATION_Handle *cfg,
2324 struct sockaddr ***addrs, socklen_t ** addr_lens)
2327 unsigned long long port;
2328 struct addrinfo hints;
2329 struct addrinfo *res;
2330 struct addrinfo *pos;
2331 struct addrinfo *next;
2335 struct sockaddr **saddrs;
2336 socklen_t *saddrlens;
2342 disablev6 = !plugin->use_ipv6;
2345 if (GNUNET_CONFIGURATION_have_value (cfg, serviceName, "PORT"))
2347 GNUNET_break (GNUNET_OK ==
2348 GNUNET_CONFIGURATION_get_value_number (cfg, serviceName,
2352 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2354 ("Require valid port number for service in configuration!\n"));
2355 return GNUNET_SYSERR;
2360 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, plugin->name,
2361 "Starting in listen only mode\n");
2362 return -1; /* listen only */
2366 if (GNUNET_CONFIGURATION_have_value (cfg, serviceName, "BINDTO"))
2368 GNUNET_break (GNUNET_OK ==
2369 GNUNET_CONFIGURATION_get_value_string (cfg, serviceName,
2370 "BINDTO", &hostname));
2375 if (hostname != NULL)
2377 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2378 "Resolving `%s' since that is where `%s' will bind to.\n",
2379 hostname, serviceName);
2380 memset (&hints, 0, sizeof (struct addrinfo));
2382 hints.ai_family = AF_INET;
2383 if ((0 != (ret = getaddrinfo (hostname, NULL, &hints, &res))) ||
2386 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Failed to resolve `%s': %s\n"),
2387 hostname, gai_strerror (ret));
2388 GNUNET_free (hostname);
2389 return GNUNET_SYSERR;
2393 while (NULL != (pos = next))
2395 next = pos->ai_next;
2396 if ((disablev6) && (pos->ai_family == AF_INET6))
2402 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2403 _("Failed to find %saddress for `%s'.\n"),
2404 disablev6 ? "IPv4 " : "", hostname);
2406 GNUNET_free (hostname);
2407 return GNUNET_SYSERR;
2410 saddrs = GNUNET_malloc ((resi + 1) * sizeof (struct sockaddr *));
2411 saddrlens = GNUNET_malloc ((resi + 1) * sizeof (socklen_t));
2414 while (NULL != (pos = next))
2416 next = pos->ai_next;
2417 if ((disablev6) && (pos->ai_family == AF_INET6))
2419 if ((pos->ai_protocol != IPPROTO_TCP) && (pos->ai_protocol != 0))
2420 continue; /* not TCP */
2421 if ((pos->ai_socktype != SOCK_STREAM) && (pos->ai_socktype != 0))
2422 continue; /* huh? */
2423 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2424 "Service will bind to `%s'\n", GNUNET_a2s (pos->ai_addr,
2426 if (pos->ai_family == AF_INET)
2428 GNUNET_assert (pos->ai_addrlen == sizeof (struct sockaddr_in));
2429 saddrlens[i] = pos->ai_addrlen;
2430 saddrs[i] = GNUNET_malloc (saddrlens[i]);
2431 memcpy (saddrs[i], pos->ai_addr, saddrlens[i]);
2432 ((struct sockaddr_in *) saddrs[i])->sin_port = htons (port);
2436 GNUNET_assert (pos->ai_family == AF_INET6);
2437 GNUNET_assert (pos->ai_addrlen == sizeof (struct sockaddr_in6));
2438 saddrlens[i] = pos->ai_addrlen;
2439 saddrs[i] = GNUNET_malloc (saddrlens[i]);
2440 memcpy (saddrs[i], pos->ai_addr, saddrlens[i]);
2441 ((struct sockaddr_in6 *) saddrs[i])->sin6_port = htons (port);
2445 GNUNET_free (hostname);
2451 /* will bind against everything, just set port */
2457 saddrs = GNUNET_malloc ((resi + 1) * sizeof (struct sockaddr *));
2458 saddrlens = GNUNET_malloc ((resi + 1) * sizeof (socklen_t));
2460 saddrlens[i] = sizeof (struct sockaddr_in);
2461 saddrs[i] = GNUNET_malloc (saddrlens[i]);
2462 #if HAVE_SOCKADDR_IN_SIN_LEN
2463 ((struct sockaddr_in *) saddrs[i])->sin_len = saddrlens[i];
2465 ((struct sockaddr_in *) saddrs[i])->sin_family = AF_INET;
2466 ((struct sockaddr_in *) saddrs[i])->sin_port = htons (port);
2472 saddrs = GNUNET_malloc ((resi + 1) * sizeof (struct sockaddr *));
2473 saddrlens = GNUNET_malloc ((resi + 1) * sizeof (socklen_t));
2475 saddrlens[i] = sizeof (struct sockaddr_in6);
2476 saddrs[i] = GNUNET_malloc (saddrlens[i]);
2477 #if HAVE_SOCKADDR_IN_SIN_LEN
2478 ((struct sockaddr_in6 *) saddrs[i])->sin6_len = saddrlens[0];
2480 ((struct sockaddr_in6 *) saddrs[i])->sin6_family = AF_INET6;
2481 ((struct sockaddr_in6 *) saddrs[i])->sin6_port = htons (port);
2483 saddrlens[i] = sizeof (struct sockaddr_in);
2484 saddrs[i] = GNUNET_malloc (saddrlens[i]);
2485 #if HAVE_SOCKADDR_IN_SIN_LEN
2486 ((struct sockaddr_in *) saddrs[i])->sin_len = saddrlens[1];
2488 ((struct sockaddr_in *) saddrs[i])->sin_family = AF_INET;
2489 ((struct sockaddr_in *) saddrs[i])->sin_port = htons (port);
2493 *addr_lens = saddrlens;
2499 * Ask NAT for addresses
2501 * @param plugin the plugin handle
2504 server_start_report_addresses (struct HTTP_Server_Plugin *plugin)
2506 int res = GNUNET_OK;
2507 struct sockaddr **addrs;
2508 socklen_t *addrlens;
2510 res = server_get_addresses (plugin,
2511 plugin->name, plugin->env->cfg,
2513 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2514 _("Found %u addresses to report to NAT service\n"), res);
2516 if (GNUNET_SYSERR == res)
2523 GNUNET_NAT_register (plugin->env->cfg, GNUNET_YES, plugin->port,
2525 (const struct sockaddr **) addrs, addrlens,
2526 &server_nat_port_map_callback, NULL, plugin);
2530 GNUNET_assert (addrs[res] != NULL);
2531 GNUNET_free (addrs[res]);
2533 GNUNET_free_non_null (addrs);
2534 GNUNET_free_non_null (addrlens);
2539 * Stop NAT for addresses
2541 * @param plugin the plugin handle
2544 server_stop_report_addresses (struct HTTP_Server_Plugin *plugin)
2546 /* Stop NAT handle */
2547 if (NULL != plugin->nat)
2548 GNUNET_NAT_unregister (plugin->nat);
2550 /* Clean up addresses */
2551 struct HttpAddressWrapper *w;
2553 while (plugin->addr_head != NULL)
2555 w = plugin->addr_head;
2556 GNUNET_CONTAINER_DLL_remove (plugin->addr_head, plugin->addr_tail, w);
2557 GNUNET_free (w->address);
2564 * Check if IPv6 supported on this system
2566 * @param plugin the plugin handle
2567 * @return GNUNET_YES on success, else GNUNET_NO
2570 server_check_ipv6_support (struct HTTP_Server_Plugin *plugin)
2572 struct GNUNET_NETWORK_Handle *desc = NULL;
2573 int res = GNUNET_NO;
2575 /* Probe IPv6 support */
2576 desc = GNUNET_NETWORK_socket_create (PF_INET6, SOCK_STREAM, 0);
2579 if ((errno == ENOBUFS) || (errno == ENOMEM) || (errno == ENFILE) ||
2582 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "socket");
2584 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, plugin->name,
2586 ("Disabling IPv6 since it is not supported on this system!\n"));
2591 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (desc));
2595 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2596 "Testing IPv6 on this system: %s\n",
2597 (res == GNUNET_YES) ? "successful" : "failed");
2603 * Notify server about our external hostname
2606 * @param tc task context (unused)
2609 server_notify_external_hostname (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2611 struct HTTP_Server_Plugin *plugin = cls;
2615 plugin->notify_ext_task = GNUNET_SCHEDULER_NO_TASK;
2616 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
2619 GNUNET_asprintf(&url, "%s://%s", plugin->protocol, plugin->external_hostname);
2621 urlen = strlen (url) + 1;
2622 plugin->ext_addr = GNUNET_malloc (sizeof (struct HttpAddress) + urlen);
2623 plugin->ext_addr->options = htonl(plugin->options);
2624 plugin->ext_addr->urlen = htonl (urlen);
2625 plugin->ext_addr_len = sizeof (struct HttpAddress) + urlen;
2626 memcpy (&plugin->ext_addr[1], url, urlen);
2628 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2629 "Notifying transport about external hostname address `%s'\n", plugin->external_hostname);
2632 if (GNUNET_YES == plugin->verify_external_hostname)
2633 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, plugin->name,
2634 "Enabling SSL verification for external hostname address `%s'\n", plugin->external_hostname);
2635 plugin->env->notify_address (plugin->env->cls, GNUNET_YES,
2636 plugin->ext_addr, plugin->ext_addr_len,
2639 plugin->env->notify_address (plugin->env->cls, GNUNET_YES,
2640 plugin->ext_addr, plugin->ext_addr_len,
2647 * Configure the plugin
2649 * @param plugin plugin handle
2650 * @return GNUNET_OK on success, GNUNET_SYSERR on failure
2653 server_configure_plugin (struct HTTP_Server_Plugin *plugin)
2655 unsigned long long port;
2656 unsigned long long max_connections;
2657 char *bind4_address = NULL;
2658 char *bind6_address = NULL;
2659 char *eh_tmp = NULL;
2660 int external_hostname_use_port;
2663 if (GNUNET_CONFIGURATION_have_value
2664 (plugin->env->cfg, plugin->name, "USE_IPv4"))
2667 GNUNET_CONFIGURATION_get_value_yesno (plugin->env->cfg, plugin->name,
2671 plugin->use_ipv4 = GNUNET_YES;
2672 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2673 _("IPv4 support is %s\n"),
2674 (plugin->use_ipv4 == GNUNET_YES) ? "enabled" : "disabled");
2677 if (GNUNET_CONFIGURATION_have_value
2678 (plugin->env->cfg, plugin->name, "USE_IPv6"))
2681 GNUNET_CONFIGURATION_get_value_yesno (plugin->env->cfg, plugin->name,
2685 plugin->use_ipv6 = GNUNET_YES;
2686 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2687 _("IPv6 support is %s\n"),
2688 (plugin->use_ipv6 == GNUNET_YES) ? "enabled" : "disabled");
2690 if ((plugin->use_ipv4 == GNUNET_NO) && (plugin->use_ipv6 == GNUNET_NO))
2692 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
2694 ("Neither IPv4 nor IPv6 are enabled! Fix in configuration\n"),
2696 return GNUNET_SYSERR;
2699 /* Reading port number from config file */
2701 GNUNET_CONFIGURATION_get_value_number (plugin->env->cfg, plugin->name,
2702 "PORT", &port)) || (port > 65535))
2704 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
2705 _("Port is required! Fix in configuration\n"),
2707 return GNUNET_SYSERR;
2709 plugin->port = port;
2711 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, plugin->name,
2712 _("Using port %u\n"), plugin->port);
2714 if ((plugin->use_ipv4 == GNUNET_YES) &&
2715 (GNUNET_YES == GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg,
2716 plugin->name, "BINDTO", &bind4_address)))
2718 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2719 "Binding %s plugin to specific IPv4 address: `%s'\n",
2720 plugin->protocol, bind4_address);
2721 plugin->server_addr_v4 = GNUNET_malloc (sizeof (struct sockaddr_in));
2722 if (1 != inet_pton (AF_INET, bind4_address,
2723 &plugin->server_addr_v4->sin_addr))
2725 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
2727 ("Specific IPv4 address `%s' in configuration file is invalid!\n"),
2729 GNUNET_free (bind4_address);
2730 GNUNET_free (plugin->server_addr_v4);
2731 plugin->server_addr_v4 = NULL;
2732 return GNUNET_SYSERR;
2736 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2737 _("Binding to IPv4 address %s\n"), bind4_address);
2738 plugin->server_addr_v4->sin_family = AF_INET;
2739 plugin->server_addr_v4->sin_port = htons (plugin->port);
2741 GNUNET_free (bind4_address);
2744 if ((plugin->use_ipv6 == GNUNET_YES) &&
2746 GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg, plugin->name,
2747 "BINDTO6", &bind6_address)))
2749 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2750 "Binding %s plugin to specific IPv6 address: `%s'\n",
2751 plugin->protocol, bind6_address);
2752 plugin->server_addr_v6 = GNUNET_malloc (sizeof (struct sockaddr_in6));
2754 inet_pton (AF_INET6, bind6_address, &plugin->server_addr_v6->sin6_addr))
2756 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
2758 ("Specific IPv6 address `%s' in configuration file is invalid!\n"),
2760 GNUNET_free (bind6_address);
2761 GNUNET_free (plugin->server_addr_v6);
2762 plugin->server_addr_v6 = NULL;
2763 return GNUNET_SYSERR;
2767 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2768 _("Binding to IPv6 address %s\n"), bind6_address);
2769 plugin->server_addr_v6->sin6_family = AF_INET6;
2770 plugin->server_addr_v6->sin6_port = htons (plugin->port);
2772 GNUNET_free (bind6_address);
2775 plugin->verify_external_hostname = GNUNET_NO;
2777 plugin->verify_external_hostname = GNUNET_CONFIGURATION_get_value_yesno (plugin->env->cfg, plugin->name,
2778 "VERIFY_EXTERNAL_HOSTNAME");
2779 if (GNUNET_SYSERR == plugin->verify_external_hostname)
2780 plugin->verify_external_hostname = GNUNET_NO;
2781 if (GNUNET_YES == plugin->verify_external_hostname)
2782 plugin->options |= HTTP_OPTIONS_VERIFY_CERTIFICATE;
2784 external_hostname_use_port = GNUNET_CONFIGURATION_get_value_yesno (plugin->env->cfg, plugin->name,
2785 "EXTERNAL_HOSTNAME_USE_PORT");
2786 if (GNUNET_SYSERR == external_hostname_use_port)
2787 external_hostname_use_port = GNUNET_NO;
2790 if (GNUNET_YES == GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg, plugin->name,
2791 "EXTERNAL_HOSTNAME", &eh_tmp))
2795 char * pos_url = NULL;
2797 if (NULL != strstr(eh_tmp, "://"))
2799 tmp = &strstr(eh_tmp, "://")[3];
2804 if (GNUNET_YES == external_hostname_use_port)
2806 if ( (strlen (tmp) > 1) && (NULL != (pos = strchr(tmp, '/'))) )
2810 GNUNET_asprintf (&plugin->external_hostname, "%s:%u/%s", tmp, (uint16_t) port, (NULL == pos_url) ? "" : pos_url);
2813 GNUNET_asprintf (&plugin->external_hostname, "%s:%u", tmp, (uint16_t) port);
2816 plugin->external_hostname = GNUNET_strdup (tmp);
2817 GNUNET_free (eh_tmp);
2819 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, plugin->name,
2820 _("Using external hostname `%s'\n"), plugin->external_hostname);
2821 plugin->notify_ext_task = GNUNET_SCHEDULER_add_now (&server_notify_external_hostname, plugin);
2823 /* Use only configured external hostname */
2824 if (GNUNET_CONFIGURATION_have_value
2825 (plugin->env->cfg, plugin->name, "EXTERNAL_HOSTNAME_ONLY"))
2827 plugin->external_only =
2828 GNUNET_CONFIGURATION_get_value_yesno (plugin->env->cfg, plugin->name,
2829 "EXTERNAL_HOSTNAME_ONLY");
2832 plugin->external_only = GNUNET_NO;
2834 if (GNUNET_YES == plugin->external_only)
2835 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2836 _("Notifying transport only about hostname `%s'\n"), plugin->external_hostname);
2839 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2840 "No external hostname configured\n");
2842 /* Optional parameters */
2843 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (plugin->env->cfg,
2845 "MAX_CONNECTIONS", &max_connections))
2846 max_connections = 128;
2847 plugin->max_connections = max_connections;
2849 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2850 _("Maximum number of connections is %u\n"),
2851 plugin->max_connections);
2854 plugin->peer_id_length = strlen (GNUNET_h2s_full (&plugin->env->my_identity->hashPubKey));
2861 * Session was idle, so disconnect it
2863 * @param cls the session
2864 * @param tc task context
2867 server_session_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2869 GNUNET_assert (NULL != cls);
2870 struct Session *s = cls;
2872 s->timeout_task = GNUNET_SCHEDULER_NO_TASK;
2873 GNUNET_log (TIMEOUT_LOG,
2874 "Session %p was idle for %llu ms, disconnecting\n",
2875 s, (unsigned long long) SERVER_SESSION_TIMEOUT.rel_value);
2877 /* call session destroy function */
2878 GNUNET_assert (GNUNET_OK == server_disconnect (s));
2883 * Start session timeout for session s
2885 * @param s the session
2888 server_start_session_timeout (struct Session *s)
2890 GNUNET_assert (NULL != s);
2891 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == s->timeout_task);
2892 s->timeout_task = GNUNET_SCHEDULER_add_delayed (SERVER_SESSION_TIMEOUT,
2893 &server_session_timeout,
2895 GNUNET_log (TIMEOUT_LOG,
2896 "Timeout for session %p set to %llu ms\n",
2897 s, (unsigned long long) SERVER_SESSION_TIMEOUT.rel_value);
2902 * Increment session timeout due to activity session s
2904 * @param s the session
2907 server_reschedule_session_timeout (struct Session *s)
2909 GNUNET_assert (NULL != s);
2910 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK != s->timeout_task);
2912 GNUNET_SCHEDULER_cancel (s->timeout_task);
2913 s->timeout_task = GNUNET_SCHEDULER_add_delayed (SERVER_SESSION_TIMEOUT,
2914 &server_session_timeout,
2916 GNUNET_log (TIMEOUT_LOG,
2917 "Timeout rescheduled for session %p set to %llu ms\n",
2918 s, (unsigned long long) SERVER_SESSION_TIMEOUT.rel_value);
2923 * Exit point from the plugin.
2929 LIBGNUNET_PLUGIN_TRANSPORT_DONE (void *cls)
2931 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
2932 struct HTTP_Server_Plugin *plugin = api->cls;
2933 struct Session *pos;
2934 struct Session *next;
2936 if (NULL == api->cls)
2938 /* Free for stub mode */
2942 plugin->in_shutdown = GNUNET_YES;
2943 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2944 _("Shutting down plugin `%s'\n"),
2947 if (GNUNET_SCHEDULER_NO_TASK != plugin->notify_ext_task)
2949 GNUNET_SCHEDULER_cancel (plugin->notify_ext_task);
2950 plugin->notify_ext_task = GNUNET_SCHEDULER_NO_TASK;
2953 if (NULL != plugin->ext_addr)
2955 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2956 "Notifying transport to remove address `%s'\n",
2957 http_common_plugin_address_to_string (NULL,
2960 plugin->ext_addr_len));
2962 plugin->env->notify_address (plugin->env->cls,
2965 plugin->ext_addr_len,
2968 plugin->env->notify_address (plugin->env->cls,
2971 plugin->ext_addr_len,
2977 /* Stop to report addresses to transport service */
2978 server_stop_report_addresses (plugin);
2979 server_stop (plugin);
2980 next = plugin->head;
2981 while (NULL != (pos = next))
2984 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2985 "Removing left over session %p\n", pos);
2987 if ((GNUNET_YES == pos->session_passed) && (GNUNET_NO == pos->session_ended))
2989 /* Notify transport immediately that this session is invalid */
2990 pos->session_ended = GNUNET_YES;
2991 plugin->env->session_end (plugin->env->cls, &pos->target, pos);
2994 server_delete_session (pos);
2998 GNUNET_free_non_null (plugin->external_hostname);
2999 GNUNET_free_non_null (plugin->ext_addr);
3000 GNUNET_free_non_null (plugin->server_addr_v4);
3001 GNUNET_free_non_null (plugin->server_addr_v6);
3003 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
3004 _("Shutdown for plugin `%s' complete\n"),
3007 GNUNET_free (plugin);
3012 const char *http_plugin_address_to_string (void *cls,
3017 return http_common_plugin_address_to_string (cls, PLUGIN_NAME, addr, addrlen);
3019 return http_common_plugin_address_to_string (cls, PLUGIN_NAME, addr, addrlen);
3026 * Function obtain the network type for a session
3028 * @param cls closure ('struct Plugin*')
3029 * @param session the session
3030 * @return the network type in HBO or GNUNET_SYSERR
3032 static enum GNUNET_ATS_Network_Type
3033 http_server_get_network (void *cls,
3034 struct Session *session)
3036 GNUNET_assert (NULL != session);
3037 return ntohl (session->ats_address_network_type);
3042 * Entry point for the plugin.
3048 LIBGNUNET_PLUGIN_TRANSPORT_INIT (void *cls)
3050 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
3051 struct GNUNET_TRANSPORT_PluginFunctions *api;
3052 struct HTTP_Server_Plugin *plugin;
3054 plugin = GNUNET_malloc (sizeof (struct HTTP_Server_Plugin));
3058 if (NULL == env->receive)
3060 /* run in 'stub' mode (i.e. as part of gnunet-peerinfo), don't fully
3061 initialze the plugin or the API */
3062 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
3064 api->address_to_string = &http_plugin_address_to_string;
3065 api->string_to_address = &http_common_plugin_string_to_address;
3066 api->address_pretty_printer = &http_common_plugin_address_pretty_printer;
3070 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
3072 api->send = &http_server_plugin_send;
3073 api->disconnect = &http_server_plugin_disconnect;
3074 api->check_address = &http_server_plugin_address_suggested;
3075 api->get_session = &http_server_plugin_get_session;
3077 api->address_to_string = &http_plugin_address_to_string;
3078 api->string_to_address = &http_common_plugin_string_to_address;
3079 api->address_pretty_printer = &http_common_plugin_address_pretty_printer;
3080 api->get_network = &http_server_get_network;
3083 plugin->name = "transport-https_server";
3084 plugin->protocol = "https";
3086 plugin->name = "transport-http_server";
3087 plugin->protocol = "http";
3090 /* Configure plugin */
3091 if (GNUNET_SYSERR == server_configure_plugin (plugin))
3093 LIBGNUNET_PLUGIN_TRANSPORT_DONE (api);
3097 /* Check IPv6 support */
3098 if (GNUNET_YES == plugin->use_ipv6)
3099 plugin->use_ipv6 = server_check_ipv6_support (plugin);
3101 /* Report addresses to transport service */
3102 if (GNUNET_NO == plugin->external_only)
3103 server_start_report_addresses (plugin);
3105 if (GNUNET_SYSERR == server_start (plugin))
3107 LIBGNUNET_PLUGIN_TRANSPORT_DONE (api);
3114 /* end of plugin_transport_http_server.c */