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"
37 #define LIBGNUNET_PLUGIN_TRANSPORT_INIT libgnunet_plugin_transport_https_server_init
38 #define LIBGNUNET_PLUGIN_TRANSPORT_DONE libgnunet_plugin_transport_https_server_done
40 #define LIBGNUNET_PLUGIN_TRANSPORT_INIT libgnunet_plugin_transport_http_server_init
41 #define LIBGNUNET_PLUGIN_TRANSPORT_DONE libgnunet_plugin_transport_http_server_done
44 #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>"
49 /* Enable output for debbuging URL's of incoming requests */
50 #define DEBUG_URL_PARSE GNUNET_NO
54 * Encapsulation of all of the state of the plugin.
60 * Session handle for connections.
65 * Stored in a linked list.
70 * Stored in a linked list.
75 * To whom are we talking to (set to our identity
76 * if we are still waiting for the welcome message)
78 struct GNUNET_PeerIdentity target;
81 * Pointer to the global plugin struct.
83 struct HTTP_Server_Plugin *plugin;
86 * next pointer for double linked list
88 struct HTTP_Message *msg_head;
91 * previous pointer for double linked list
93 struct HTTP_Message *msg_tail;
96 * Message stream tokenizer for incoming data
98 struct GNUNET_SERVER_MessageStreamTokenizer *msg_tk;
103 struct ServerConnection *server_recv;
108 struct ServerConnection *server_send;
121 * Unique HTTP/S connection tag for this connection
126 * ATS network type in NBO
128 uint32_t ats_address_network_type;
131 * Was session given to transport service?
136 * Did we immediately end the session in disconnect_cb
141 * Are incoming connection established at the moment
143 int connect_in_progress;
146 * Absolute time when to receive data again
147 * Used for receive throttling
149 struct GNUNET_TIME_Absolute next_receive;
152 * Session timeout task
154 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
158 struct ServerConnection
163 /* Should this connection get disconnected? GNUNET_YES/NO */
166 /* For PUT connections: Is this the first or last callback with size 0 */
169 /* The session this server connection belongs to */
170 struct Session *session;
172 /* The MHD connection */
173 struct MHD_Connection *mhd_conn;
176 struct MHD_Daemon *mhd_daemon;
181 * Encapsulation of all of the state of the plugin.
183 struct HTTP_Server_Plugin
188 struct GNUNET_TRANSPORT_PluginEnvironment *env;
191 * Linked list head of open sessions.
194 struct Session *head;
197 * Linked list tail of open sessions.
199 struct Session *tail;
214 char *external_hostname;
217 * Maximum number of sockets the plugin can use
218 * Each http inbound /outbound connections are two connections
220 unsigned int max_connections;
223 * Current number of sockets the plugin can use
224 * Each http inbound /outbound connections are two connections
226 unsigned int cur_connections;
229 * Did we immediately end the session in disconnect_cb
239 * External hostname the plugin can be connected to, can be different to
240 * the host's FQDN, used e.g. for reverse proxying
245 * Notify transport only about external address
247 unsigned int external_only;
250 * External address length
270 * Task calling transport service about external address
272 GNUNET_SCHEDULER_TaskIdentifier notify_ext_task;
275 * NAT handle & address management
277 struct GNUNET_NAT_Handle *nat;
280 * List of own addresses
284 * IPv4 addresses DLL head
286 struct HttpAddressWrapper *addr_head;
289 * IPv4 addresses DLL tail
291 struct HttpAddressWrapper *addr_tail;
294 * IPv4 server socket to bind to
296 struct sockaddr_in *server_addr_v4;
299 * IPv6 server socket to bind to
301 struct sockaddr_in6 *server_addr_v6;
306 GNUNET_SCHEDULER_TaskIdentifier server_v4_task;
311 GNUNET_SCHEDULER_TaskIdentifier server_v6_task;
314 * The IPv4 server is scheduled to run asap
316 int server_v4_immediately;
319 * The IPv6 server is scheduled to run asap
321 int server_v6_immediately;
326 struct MHD_Daemon *server_v4;
331 struct MHD_Daemon *server_v6;
339 * Use RC4-128 instead of AES:
340 * NONE:+VERS-TLS1.0:+ARCFOUR-128:+SHA1:+RSA:+COMP-NULL
360 * Wrapper to manage addresses
362 struct HttpAddressWrapper
367 struct HttpAddressWrapper *next;
370 * Linked list previous
372 struct HttpAddressWrapper *prev;
381 * Message to send using http
386 * next pointer for double linked list
388 struct HTTP_Message *next;
391 * previous pointer for double linked list
393 struct HTTP_Message *prev;
396 * buffer containing data to send
401 * amount of data already sent
411 * HTTP/S specific overhead
416 * Continuation function to call once the transmission buffer
417 * has again space available. NULL if there is no
418 * continuation to call.
420 GNUNET_TRANSPORT_TransmitContinuation transmit_cont;
423 * Closure for transmit_cont.
425 void *transmit_cont_cls;
430 * The http_server plugin handle
432 static struct HTTP_Server_Plugin * p;
436 * Start session timeout for session s
437 * @param s the session
440 server_start_session_timeout (struct Session *s);
444 * Increment session timeout due to activity for session s
445 * @param s the session
448 server_reschedule_session_timeout (struct Session *s);
452 * Cancel timeout for session s
453 * @param s the session
456 server_stop_session_timeout (struct Session *s);
460 * Disconnect a session s
461 * @param s the session
464 server_disconnect (struct Session *s);
468 * Does session s exist?
470 * @param plugin the plugin handle
471 * @param s the session
472 * @return GNUNET_YES on success, GNUNET_NO on error
475 server_exist_session (struct HTTP_Server_Plugin *plugin, struct Session *s);
479 * Reschedule the execution of both IPv4 and IPv6 server
480 * @param plugin the plugin
481 * @param server which server to schedule v4 or v6?
482 * @param now GNUNET_YES to schedule execution immediately, GNUNET_NO to wait
486 server_reschedule (struct HTTP_Server_Plugin *plugin, struct MHD_Daemon *server,
491 * Function that can be used by the transport service to transmit
492 * a message using the plugin. Note that in the case of a
493 * peer disconnecting, the continuation MUST be called
494 * prior to the disconnect notification itself. This function
495 * will be called with this peer's HELLO message to initiate
496 * a fresh connection to another peer.
499 * @param session which session must be used
500 * @param msgbuf the message to transmit
501 * @param msgbuf_size number of bytes in 'msgbuf'
502 * @param priority how important is the message (most plugins will
503 * ignore message priority and just FIFO)
504 * @param to how long to wait at most for the transmission (does not
505 * require plugins to discard the message after the timeout,
506 * just advisory for the desired delay; most plugins will ignore
508 * @param cont continuation to call once the message has
509 * been transmitted (or if the transport is ready
510 * for the next transmission call; or if the
511 * peer disconnected...); can be NULL
512 * @param cont_cls closure for cont
513 * @return number of bytes used (on the physical network, with overheads);
514 * -1 on hard errors (i.e. address invalid); 0 is a legal value
515 * and does NOT mean that the message was not transmitted (DV)
518 http_server_plugin_send (void *cls,
519 struct Session *session,
520 const char *msgbuf, size_t msgbuf_size,
521 unsigned int priority,
522 struct GNUNET_TIME_Relative to,
523 GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
525 struct HTTP_Server_Plugin *plugin = cls;
526 struct HTTP_Message *msg;
530 GNUNET_assert (plugin != NULL);
531 GNUNET_assert (session != NULL);
533 if (GNUNET_NO == server_exist_session (plugin, session))
536 return GNUNET_SYSERR;
538 if (NULL == session->server_send)
540 if (GNUNET_NO == session->connect_in_progress)
542 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, session->plugin->name,
543 "Session %p/connection %p: Sending message with %u bytes to peer `%s' with FAILED\n",
544 session, session->server_send,
545 msgbuf_size, GNUNET_i2s (&session->target));
547 return GNUNET_SYSERR;
552 if (GNUNET_YES == session->server_send->disconnect)
553 return GNUNET_SYSERR;
557 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, session->plugin->name,
558 "Session %p/connection %p: Sending message with %u to peer `%s' with \n",
559 session, session->server_send,
560 msgbuf_size, GNUNET_i2s (&session->target));
562 /* create new message and schedule */
563 bytes_sent = sizeof (struct HTTP_Message) + msgbuf_size;
564 msg = GNUNET_malloc (bytes_sent);
566 msg->size = msgbuf_size;
568 msg->buf = (char *) &msg[1];
569 msg->transmit_cont = cont;
570 msg->transmit_cont_cls = cont_cls;
571 memcpy (msg->buf, msgbuf, msgbuf_size);
573 GNUNET_CONTAINER_DLL_insert_tail (session->msg_head, session->msg_tail, msg);
575 GNUNET_asprintf (&stat_txt, "# bytes currently in %s_server buffers", plugin->protocol);
576 GNUNET_STATISTICS_update (plugin->env->stats,
577 stat_txt, msgbuf_size, GNUNET_NO);
578 GNUNET_free (stat_txt);
580 if (NULL != session->server_send)
582 server_reschedule (session->plugin,
583 session->server_send->mhd_daemon,
585 server_reschedule_session_timeout (session);
593 * Function that can be used to force the plugin to disconnect
594 * from the given peer and cancel all previous transmissions
595 * (and their continuationc).
598 * @param target peer from which to disconnect
601 http_server_plugin_disconnect (void *cls, const struct GNUNET_PeerIdentity *target)
603 struct HTTP_Server_Plugin *plugin = cls;
604 struct Session *next = NULL;
605 struct Session *pos = NULL;
607 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
608 "Transport tells me to disconnect `%s'\n",
609 GNUNET_i2s (target));
612 while (NULL != (pos = next))
615 if (0 == memcmp (target, &pos->target, sizeof (struct GNUNET_PeerIdentity)))
617 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
618 "Disconnecting session %p to `%s'\n",
619 pos, GNUNET_i2s (target));
620 server_disconnect (pos);
628 * Another peer has suggested an address for this
629 * peer and transport plugin. Check that this could be a valid
630 * address. If so, consider adding it to the list
634 * @param addr pointer to the address
635 * @param addrlen length of addr
636 * @return GNUNET_OK if this is a plausible address for this peer
640 http_server_plugin_address_suggested (void *cls, const void *addr,
643 struct HTTP_Server_Plugin *plugin = cls;
644 struct HttpAddressWrapper *next;
645 struct HttpAddressWrapper *pos;
648 if ((NULL != plugin->ext_addr) &&
649 GNUNET_YES == (http_common_cmp_addresses (addr, addrlen,
650 plugin->ext_addr, plugin->ext_addr_len)))
653 next = plugin->addr_head;
654 while (NULL != (pos = next))
657 if (GNUNET_YES == (http_common_cmp_addresses(addr,
670 * Creates a new outbound session the transport
671 * service will use to send data to the peer
673 * Since HTTP/S server cannot create sessions, always return NULL
675 * @param cls the plugin
676 * @param address the address
677 * @return always NULL
679 static struct Session *
680 http_server_plugin_get_session (void *cls,
681 const struct GNUNET_HELLO_Address *address)
688 * Deleting the session
689 * Must not be used afterwards
691 * @param s the session to delete
694 server_delete_session (struct Session *s)
696 struct HTTP_Server_Plugin *plugin = s->plugin;
697 server_stop_session_timeout(s);
699 GNUNET_CONTAINER_DLL_remove (plugin->head, plugin->tail, s);
700 struct HTTP_Message *msg = s->msg_head;
701 struct HTTP_Message *tmp = NULL;
707 GNUNET_CONTAINER_DLL_remove (s->msg_head, s->msg_tail, msg);
708 if (msg->transmit_cont != NULL)
710 msg->transmit_cont (msg->transmit_cont_cls, &s->target, GNUNET_SYSERR,
711 msg->size, msg->pos + msg->overhead);
717 if (s->msg_tk != NULL)
719 GNUNET_SERVER_mst_destroy (s->msg_tk);
722 GNUNET_free (s->addr);
723 GNUNET_free_non_null (s->server_recv);
724 GNUNET_free_non_null (s->server_send);
727 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
728 "Session %p destroyed\n", s);
733 * Cancel timeout for session s
735 * @param s the session
738 server_stop_session_timeout (struct Session *s)
740 GNUNET_assert (NULL != s);
742 if (GNUNET_SCHEDULER_NO_TASK != s->timeout_task)
744 GNUNET_SCHEDULER_cancel (s->timeout_task);
745 s->timeout_task = GNUNET_SCHEDULER_NO_TASK;
746 GNUNET_log (TIMEOUT_LOG, "Timeout stopped for session %p\n", s);
752 * Function that queries MHD's select sets and
753 * starts the task waiting for them.
754 * @param plugin plugin
755 * @param daemon_handle the MHD daemon handle
756 * @param now schedule immediately
757 * @return gnunet task identifier
759 static GNUNET_SCHEDULER_TaskIdentifier
760 server_schedule (struct HTTP_Server_Plugin *plugin,
761 struct MHD_Daemon *daemon_handle,
766 * Reschedule the execution of both IPv4 and IPv6 server
767 * @param plugin the plugin
768 * @param server which server to schedule v4 or v6?
769 * @param now GNUNET_YES to schedule execution immediately, GNUNET_NO to wait
773 server_reschedule (struct HTTP_Server_Plugin *plugin, struct MHD_Daemon *server,
776 if ((server == plugin->server_v4) && (plugin->server_v4 != NULL))
778 if (GNUNET_YES == plugin->server_v4_immediately)
779 return; /* No rescheduling, server will run asap */
781 if (GNUNET_YES == now)
782 plugin->server_v4_immediately = GNUNET_YES;
784 if (plugin->server_v4_task != GNUNET_SCHEDULER_NO_TASK)
786 GNUNET_SCHEDULER_cancel (plugin->server_v4_task);
787 plugin->server_v4_task = GNUNET_SCHEDULER_NO_TASK;
789 plugin->server_v4_task = server_schedule (plugin, plugin->server_v4, now);
792 if ((server == plugin->server_v6) && (plugin->server_v6 != NULL))
794 if (GNUNET_YES == plugin->server_v6_immediately)
795 return; /* No rescheduling, server will run asap */
797 if (GNUNET_YES == now)
798 plugin->server_v6_immediately = GNUNET_YES;
800 if (plugin->server_v6_task != GNUNET_SCHEDULER_NO_TASK)
802 GNUNET_SCHEDULER_cancel (plugin->server_v6_task);
803 plugin->server_v6_task = GNUNET_SCHEDULER_NO_TASK;
805 plugin->server_v6_task = server_schedule (plugin, plugin->server_v6, now);
811 * Disconnect session s
813 * @param s the session
814 * @return GNUNET_OK on success
817 server_disconnect (struct Session *s)
819 struct ServerConnection * send = NULL;
820 struct ServerConnection * recv = NULL;
822 if (GNUNET_NO == server_exist_session (p, s))
825 return GNUNET_SYSERR;
828 send = (struct ServerConnection *) s->server_send;
829 if (s->server_send != NULL)
831 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, s->plugin->name,
832 "Server: %p / %p Terminating inbound PUT session to peer `%s'\n",
833 s, s->server_send, GNUNET_i2s (&s->target));
835 send->disconnect = GNUNET_YES;
836 #if MHD_VERSION >= 0x00090E00
837 MHD_set_connection_option (send->mhd_conn, MHD_CONNECTION_OPTION_TIMEOUT,
840 server_reschedule (s->plugin, send->mhd_daemon, GNUNET_YES);
843 recv = (struct ServerConnection *) s->server_recv;
846 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, s->plugin->name,
847 "Server: %p / %p Terminating inbound GET session to peer `%s'\n",
848 s, s->server_recv, GNUNET_i2s (&s->target));
850 recv->disconnect = GNUNET_YES;
851 #if MHD_VERSION >= 0x00090E00
852 MHD_set_connection_option (recv->mhd_conn, MHD_CONNECTION_OPTION_TIMEOUT,
855 server_reschedule (s->plugin, recv->mhd_daemon, GNUNET_YES);
861 server_mhd_connection_timeout (struct HTTP_Server_Plugin *plugin, struct Session *s, int to)
863 #if MHD_VERSION >= 0x00090E00
864 /* Setting timeouts for other connections */
865 if (s->server_recv != NULL)
867 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
868 "Setting timeout for %p to %u sec.\n", s->server_recv, to);
869 MHD_set_connection_option (s->server_recv->mhd_conn,
870 MHD_CONNECTION_OPTION_TIMEOUT,
872 server_reschedule (plugin, s->server_recv->mhd_daemon, GNUNET_NO);
874 if (s->server_send != NULL)
876 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
877 "Setting timeout for %p to %u sec.\n", s->server_send, to);
878 MHD_set_connection_option (s->server_send->mhd_conn,
879 MHD_CONNECTION_OPTION_TIMEOUT,
881 server_reschedule (plugin, s->server_send->mhd_daemon, GNUNET_NO);
887 * Parse incoming URL for tag and target
889 * @param plugin plugin
890 * @param url incoming url
891 * @param target where to store the target
892 * @param tag where to store the tag
893 * @return GNUNET_OK on success, GNUNET_SYSERR on error
897 server_parse_url (struct HTTP_Server_Plugin *plugin, const char * url, struct GNUNET_PeerIdentity * target, uint32_t *tag)
899 char * tag_start = NULL;
900 char * tag_end = NULL;
901 char * target_start = NULL;
902 char * separator = NULL;
903 char hash[plugin->peer_id_length+1];
905 unsigned long int ctag;
908 * URL is valid if it is in the form [prefix with (multiple) '/'][peerid[103];tag]*/
913 return GNUNET_SYSERR;
918 separator = strrchr (url, ';');
920 if (NULL == separator)
922 if (DEBUG_URL_PARSE) GNUNET_break (0);
923 return GNUNET_SYSERR;
925 tag_start = separator + 1;
927 if (strlen (tag_start) == 0)
929 /* No tag after separator */
930 if (DEBUG_URL_PARSE) GNUNET_break (0);
931 return GNUNET_SYSERR;
933 ctag = strtoul (tag_start, &tag_end, 10);
936 /* tag == 0 , invalid */
937 if (DEBUG_URL_PARSE) GNUNET_break (0);
938 return GNUNET_SYSERR;
940 if ((ctag == ULONG_MAX) && (ERANGE == errno))
942 /* out of range: > ULONG_MAX */
943 if (DEBUG_URL_PARSE) GNUNET_break (0);
944 return GNUNET_SYSERR;
946 if (ctag > UINT32_MAX)
948 /* out of range: > UINT32_MAX */
949 if (DEBUG_URL_PARSE) GNUNET_break (0);
950 return GNUNET_SYSERR;
952 (*tag) = (uint32_t) ctag;
955 /* no char after tag */
956 if (DEBUG_URL_PARSE) GNUNET_break (0);
957 return GNUNET_SYSERR;
959 if (url[strlen(url)] != tag_end[0])
961 /* there are more not converted chars after tag */
962 if (DEBUG_URL_PARSE) GNUNET_break (0);
963 return GNUNET_SYSERR;
966 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
967 "Found tag `%u' in url\n", (*tag));
969 /* convert peer id */
970 target_start = strrchr (url, '/');
971 if (NULL == target_start)
974 target_start = (char *) url;
977 hash_length = separator - target_start;
978 if (hash_length != plugin->peer_id_length)
980 /* no char after tag */
981 if (DEBUG_URL_PARSE) GNUNET_break (0);
982 return GNUNET_SYSERR;
984 memcpy (hash, target_start, hash_length);
985 hash[hash_length] = '\0';
987 if (GNUNET_OK != GNUNET_CRYPTO_hash_from_string ((const char *) hash, &(target->hashPubKey)))
989 /* hash conversion failed */
990 if (DEBUG_URL_PARSE) GNUNET_break (0);
991 return GNUNET_SYSERR;
994 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
995 "Found target `%s' in url\n", GNUNET_h2s_full(&target->hashPubKey));
1001 * Lookup a mhd connection and create one if none is found
1003 * @param plugin the plugin handle
1004 * @param mhd_connection the incoming mhd_connection
1005 * @param url incoming requested URL
1006 * @param method PUT or GET
1007 * @return the server connecetion
1009 static struct ServerConnection *
1010 server_lookup_connection (struct HTTP_Server_Plugin *plugin,
1011 struct MHD_Connection *mhd_connection, const char *url,
1014 struct Session *s = NULL;
1015 struct ServerConnection *sc = NULL;
1016 const union MHD_ConnectionInfo *conn_info;
1017 struct GNUNET_ATS_Information ats;
1022 struct GNUNET_PeerIdentity target;
1024 int direction = GNUNET_SYSERR;
1027 conn_info = MHD_get_connection_info (mhd_connection,
1028 MHD_CONNECTION_INFO_CLIENT_ADDRESS);
1029 if ((conn_info->client_addr->sa_family != AF_INET) &&
1030 (conn_info->client_addr->sa_family != AF_INET6))
1032 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1033 "New %s connection from %s\n", method, url);
1035 if (GNUNET_SYSERR == server_parse_url (plugin, url, &target, &tag))
1037 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1038 "Invalid url %s\n", url);
1041 if (0 == strcmp (MHD_HTTP_METHOD_PUT, method))
1042 direction = _RECEIVE;
1043 else if (0 == strcmp (MHD_HTTP_METHOD_GET, method))
1047 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1048 "Invalid method %s connection from %s\n", method, url);
1052 plugin->cur_connections++;
1053 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1054 "New %s connection from %s with tag %u (%u of %u)\n",
1056 GNUNET_i2s (&target), tag,
1057 plugin->cur_connections, plugin->max_connections);
1058 /* find duplicate session */
1062 if ((0 == memcmp (&s->target, &target, sizeof (struct GNUNET_PeerIdentity))) &&
1069 if ((_RECEIVE == direction) && (NULL != s->server_recv))
1071 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1072 "Duplicate PUT connection from `%s' tag %u, dismissing new connection\n",
1073 GNUNET_i2s (&target),
1078 if ((_SEND == direction) && (NULL != s->server_send))
1080 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1081 "Duplicate GET connection from `%s' tag %u, dismissing new connection\n",
1082 GNUNET_i2s (&target),
1089 /* create new session */
1090 switch (conn_info->client_addr->sa_family)
1093 addr = http_common_address_from_socket (plugin->protocol, conn_info->client_addr, sizeof (struct sockaddr_in));
1094 addr_len = http_common_address_get_size (addr);
1095 ats = plugin->env->get_address_type (plugin->env->cls, conn_info->client_addr, sizeof (struct sockaddr_in));
1098 addr = http_common_address_from_socket (plugin->protocol, conn_info->client_addr, sizeof (struct sockaddr_in6));
1099 addr_len = http_common_address_get_size (addr);
1100 ats = plugin->env->get_address_type (plugin->env->cls, conn_info->client_addr, sizeof (struct sockaddr_in6));
1106 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1107 "Creating new session for peer `%s' connecting from `%s'\n",
1108 GNUNET_i2s (&target),
1109 http_common_plugin_address_to_string (NULL, addr, addr_len));
1111 s = GNUNET_malloc (sizeof (struct Session));
1112 memcpy (&s->target, &target, sizeof (struct GNUNET_PeerIdentity));
1115 s->addrlen = addr_len;
1116 s->ats_address_network_type = ats.value;
1117 s->next_receive = GNUNET_TIME_UNIT_ZERO_ABS;
1119 s->server_recv = NULL;
1120 s->server_send = NULL;
1121 s->session_passed = GNUNET_NO;
1122 s->session_ended = GNUNET_NO;
1123 s->connect_in_progress = GNUNET_YES;
1124 server_start_session_timeout(s);
1125 GNUNET_CONTAINER_DLL_insert (plugin->head, plugin->tail, s);
1127 sc = GNUNET_malloc (sizeof (struct ServerConnection));
1128 if (conn_info->client_addr->sa_family == AF_INET)
1129 sc->mhd_daemon = plugin->server_v4;
1130 if (conn_info->client_addr->sa_family == AF_INET6)
1131 sc->mhd_daemon = plugin->server_v6;
1132 sc->mhd_conn = mhd_connection;
1133 sc->direction = direction;
1134 sc->connected = GNUNET_NO;
1136 if (direction == _SEND)
1137 s->server_send = sc;
1138 if (direction == _RECEIVE)
1139 s->server_recv = sc;
1141 if ((NULL != s->server_send) && (NULL != s->server_recv))
1142 s->connect_in_progress = GNUNET_NO; /* PUT and GET are connected */
1144 #if MHD_VERSION >= 0x00090E00
1145 if ((NULL == s->server_recv) || (NULL == s->server_send))
1147 to = (HTTP_SERVER_NOT_VALIDATED_TIMEOUT.rel_value / 1000);
1148 MHD_set_connection_option (mhd_connection, MHD_CONNECTION_OPTION_TIMEOUT, to);
1149 server_reschedule (plugin, sc->mhd_daemon, GNUNET_NO);
1153 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1154 "Session %p for peer `%s' fully connected\n",
1155 s, GNUNET_i2s (&target));
1156 to = (SERVER_SESSION_TIMEOUT.rel_value / 1000);
1157 server_mhd_connection_timeout (plugin, s, to);
1160 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1161 "Setting timeout for %p to %u sec.\n", sc, to);
1168 * Lookup a session for a server connection
1170 * @param plugin the plugin
1171 * @param sc the server connection
1172 * @return the session found or NULL
1174 static struct Session *
1175 server_lookup_session (struct HTTP_Server_Plugin *plugin,
1176 struct ServerConnection * sc)
1180 for (s = plugin->head; NULL != s; s = s->next)
1181 if ((s->server_recv == sc) || (s->server_send == sc))
1187 server_exist_session (struct HTTP_Server_Plugin *plugin, struct Session *s)
1189 struct Session * head;
1191 GNUNET_assert (NULL != plugin);
1192 GNUNET_assert (NULL != s);
1194 for (head = plugin->head; head != NULL; head = head->next)
1204 * Callback called by MHD when it needs data to send
1206 * @param cls current session
1207 * @param pos position in buffer
1208 * @param buf the buffer to write data to
1209 * @param max max number of bytes available in buffer
1210 * @return bytes written to buffer
1213 server_send_callback (void *cls, uint64_t pos, char *buf, size_t max)
1215 struct Session *s = cls;
1216 ssize_t bytes_read = 0;
1217 struct HTTP_Message *msg;
1220 GNUNET_assert (NULL != p);
1221 if (GNUNET_NO == server_exist_session (p, s))
1227 bytes_read = GNUNET_MIN (msg->size - msg->pos,
1229 memcpy (buf, &msg->buf[msg->pos], bytes_read);
1230 msg->pos += bytes_read;
1232 /* removing message */
1233 if (msg->pos == msg->size)
1235 GNUNET_CONTAINER_DLL_remove (s->msg_head, s->msg_tail, msg);
1236 if (NULL != msg->transmit_cont)
1237 msg->transmit_cont (msg->transmit_cont_cls, &s->target, GNUNET_OK,
1238 msg->size, msg->size + msg->overhead);
1244 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, s->plugin->name,
1245 "Sent %u bytes to peer `%s' with session %p \n", bytes_read, GNUNET_i2s (&s->target), s);
1246 GNUNET_asprintf (&stat_txt, "# bytes currently in %s_server buffers", p->protocol);
1247 GNUNET_STATISTICS_update (p->env->stats,
1248 stat_txt, -bytes_read, GNUNET_NO);
1249 GNUNET_free (stat_txt);
1250 GNUNET_asprintf (&stat_txt, "# bytes transmitted via %s_server", p->protocol);
1251 GNUNET_STATISTICS_update (p->env->stats,
1252 stat_txt, bytes_read, GNUNET_NO);
1253 GNUNET_free (stat_txt);
1260 * Callback called by MessageStreamTokenizer when a message has arrived
1262 * @param cls current session as closure
1263 * @param client client
1264 * @param message the message to be forwarded to transport service
1268 server_receive_mst_cb (void *cls, void *client,
1269 const struct GNUNET_MessageHeader *message)
1271 struct Session *s = cls;
1272 struct GNUNET_ATS_Information atsi[2];
1273 struct GNUNET_TIME_Relative delay;
1276 GNUNET_assert (NULL != p);
1277 if (GNUNET_NO == server_exist_session(p, s))
1280 struct HTTP_Server_Plugin *plugin = s->plugin;
1282 atsi[0].type = htonl (GNUNET_ATS_QUALITY_NET_DISTANCE);
1283 atsi[0].value = htonl (1);
1284 atsi[1].type = htonl (GNUNET_ATS_NETWORK_TYPE);
1285 atsi[1].value = s->ats_address_network_type;
1286 GNUNET_break (s->ats_address_network_type != ntohl (GNUNET_ATS_NET_UNSPECIFIED));
1289 delay = plugin->env->receive (plugin->env->cls,
1292 (const struct GNUNET_ATS_Information *) &atsi, 2,
1293 s, s->addr, s->addrlen);
1295 GNUNET_asprintf (&stat_txt, "# bytes received via %s_server", plugin->protocol);
1296 GNUNET_STATISTICS_update (plugin->env->stats,
1297 stat_txt, ntohs (message->size), GNUNET_NO);
1298 GNUNET_free (stat_txt);
1300 s->session_passed = GNUNET_YES;
1301 s->next_receive = GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (), delay);
1302 if (delay.rel_value > 0)
1304 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1305 "Peer `%s' address `%s' next read delayed for %llu ms\n",
1306 GNUNET_i2s (&s->target),
1307 http_common_plugin_address_to_string (NULL, s->addr, s->addrlen),
1310 server_reschedule_session_timeout (s);
1316 * MHD callback for a new incoming connection
1318 * @param cls the plugin handle
1319 * @param mhd_connection the mhd connection
1320 * @param url the requested URL
1321 * @param method GET or PUT
1322 * @param version HTTP version
1323 * @param upload_data upload data
1324 * @param upload_data_size sizeof upload data
1325 * @param httpSessionCache the session cache to remember the connection
1326 * @return MHD_YES if connection is accepted, MHD_NO on reject
1329 server_access_cb (void *cls, struct MHD_Connection *mhd_connection,
1330 const char *url, const char *method, const char *version,
1331 const char *upload_data, size_t * upload_data_size,
1332 void **httpSessionCache)
1334 struct HTTP_Server_Plugin *plugin = cls;
1337 struct ServerConnection *sc = *httpSessionCache;
1339 struct MHD_Response *response;
1341 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1342 _("Access from connection %p (%u of %u) for `%s' `%s' url `%s' with upload data size %u\n"),
1344 plugin->cur_connections, plugin->max_connections,
1345 method, version, url, (*upload_data_size));
1347 GNUNET_assert (cls != NULL);
1350 /* new connection */
1351 sc = server_lookup_connection (plugin, mhd_connection, url, method);
1354 (*httpSessionCache) = sc;
1358 response = MHD_create_response_from_data (strlen (HTTP_ERROR_RESPONSE), HTTP_ERROR_RESPONSE, MHD_NO, MHD_NO);
1359 MHD_add_response_header (response,
1360 MHD_HTTP_HEADER_CONTENT_TYPE,
1362 res = MHD_queue_response (mhd_connection, MHD_HTTP_NOT_FOUND, response);
1363 MHD_destroy_response (response);
1369 /* 'old' connection */
1370 if (NULL == server_lookup_session (plugin, sc))
1372 /* Session was already disconnected */
1377 /* existing connection */
1378 sc = (*httpSessionCache);
1380 GNUNET_assert (NULL != s);
1381 /* connection is to be disconnected */
1382 if (sc->disconnect == GNUNET_YES)
1384 /* Sent HTTP/1.1: 200 OK as response */
1385 response = MHD_create_response_from_data (strlen ("Thank you!"),
1388 res = MHD_queue_response (mhd_connection, MHD_HTTP_OK, response);
1389 MHD_destroy_response (response);
1392 GNUNET_assert (s != NULL);
1394 if (sc->direction == _SEND)
1396 response = MHD_create_response_from_callback (MHD_SIZE_UNKNOWN,
1398 &server_send_callback, s,
1400 MHD_queue_response (mhd_connection, MHD_HTTP_OK, response);
1401 MHD_destroy_response (response);
1404 if (sc->direction == _RECEIVE)
1406 if ((*upload_data_size == 0) && (sc->connected == GNUNET_NO))
1408 /* (*upload_data_size == 0) first callback when header are passed */
1409 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1410 "Session %p / Connection %p: Peer `%s' PUT on address `%s' connected\n",
1412 GNUNET_i2s (&s->target),
1413 http_common_plugin_address_to_string (NULL,
1416 sc->connected = GNUNET_YES;
1419 else if ((*upload_data_size == 0) && (sc->connected == GNUNET_YES))
1421 /* (*upload_data_size == 0) when upload is complete */
1422 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1423 "Session %p / Connection %p: Peer `%s' PUT on address `%s' finished upload\n",
1425 GNUNET_i2s (&s->target),
1426 http_common_plugin_address_to_string (NULL,
1429 sc->connected = GNUNET_NO;
1430 /* Sent HTTP/1.1: 200 OK as PUT Response\ */
1431 response = MHD_create_response_from_data (strlen ("Thank you!"),
1434 res = MHD_queue_response (mhd_connection, MHD_HTTP_OK, response);
1435 MHD_destroy_response (response);
1438 else if ((*upload_data_size > 0) && (sc->connected == GNUNET_YES))
1440 /* (*upload_data_size > 0) for every segment received */
1441 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1442 "Session %p / Connection %p: Peer `%s' PUT on address `%s' received %u bytes\n",
1444 GNUNET_i2s (&s->target),
1445 http_common_plugin_address_to_string (NULL,
1449 struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get ();
1451 if ((s->next_receive.abs_value <= now.abs_value))
1453 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1454 "PUT with %u bytes forwarded to MST\n",
1456 if (s->msg_tk == NULL)
1458 s->msg_tk = GNUNET_SERVER_mst_create (&server_receive_mst_cb, s);
1460 GNUNET_SERVER_mst_receive (s->msg_tk, s, upload_data,
1461 *upload_data_size, GNUNET_NO, GNUNET_NO);
1462 #if MHD_VERSION >= 0x00090E00
1463 server_mhd_connection_timeout (plugin, s, GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT.rel_value / 1000);
1465 (*upload_data_size) = 0;
1469 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1470 "Session %p / Connection %p: no inbound bandwidth available! Next read was delayed by %llu ms\n",
1471 s, sc, now.abs_value - s->next_receive.abs_value);
1486 * Callback from MHD when a connection disconnects
1488 * @param cls closure
1489 * @param connection the disconnected MHD connection
1490 * @param httpSessionCache the pointer to distinguish
1493 server_disconnect_cb (void *cls, struct MHD_Connection *connection,
1494 void **httpSessionCache)
1496 struct ServerConnection *sc = *httpSessionCache;
1497 struct Session *s = NULL;
1498 struct Session *t = NULL;
1499 struct HTTP_Server_Plugin *plugin = NULL;
1501 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, p->name,
1502 "Disconnect for connection %p \n", sc);
1507 if (NULL == (s = server_lookup_session (p, sc)))
1510 GNUNET_assert (NULL != p);
1511 for (t = p->head; t != NULL; t = t->next)
1520 if (sc->direction == _SEND)
1523 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1524 "Peer `%s' connection %p, GET on address `%s' disconnected\n",
1525 GNUNET_i2s (&s->target), s->server_send,
1526 http_common_plugin_address_to_string (NULL, s->addr, s->addrlen));
1527 s->server_send = NULL;
1528 if (NULL != (s->server_recv))
1530 s->server_recv->disconnect = GNUNET_YES;
1531 GNUNET_assert (NULL != s->server_recv->mhd_conn);
1532 #if MHD_VERSION >= 0x00090E00
1533 MHD_set_connection_option (s->server_recv->mhd_conn, MHD_CONNECTION_OPTION_TIMEOUT,
1536 server_reschedule (plugin, s->server_recv->mhd_daemon, GNUNET_NO);
1539 if (sc->direction == _RECEIVE)
1541 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1542 "Peer `%s' connection %p PUT on address `%s' disconnected\n",
1543 GNUNET_i2s (&s->target), s->server_recv,
1544 http_common_plugin_address_to_string (NULL, s->addr, s->addrlen));
1545 s->server_recv = NULL;
1546 /* Do not terminate session when PUT disconnects
1547 if (NULL != (s->server_send))
1549 s->server_send->disconnect = GNUNET_YES;
1550 GNUNET_assert (NULL != s->server_send->mhd_conn);
1551 #if MHD_VERSION >= 0x00090E00
1552 MHD_set_connection_option (s->server_send->mhd_conn, MHD_CONNECTION_OPTION_TIMEOUT,
1555 server_reschedule (plugin, s->server_send->mhd_daemon, GNUNET_NO);
1557 if (s->msg_tk != NULL)
1559 GNUNET_SERVER_mst_destroy (s->msg_tk);
1565 plugin->cur_connections--;
1567 if ((s->server_send == NULL) && (s->server_recv == NULL))
1569 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1570 "Peer `%s' on address `%s' disconnected\n",
1571 GNUNET_i2s (&s->target),
1572 http_common_plugin_address_to_string (NULL, s->addr, s->addrlen));
1574 if ((GNUNET_YES == s->session_passed) && (GNUNET_NO == s->session_ended))
1576 /* Notify transport immediately that this session is invalid */
1577 s->session_ended = GNUNET_YES;
1578 plugin->env->session_end (plugin->env->cls, &s->target, s);
1580 server_delete_session (s);
1587 * Check if incoming connection is accepted.
1589 * @param cls plugin as closure
1590 * @param addr address of incoming connection
1591 * @param addr_len address length of incoming connection
1592 * @return MHD_YES if connection is accepted, MHD_NO if connection is rejected
1596 server_accept_cb (void *cls, const struct sockaddr *addr, socklen_t addr_len)
1598 struct HTTP_Server_Plugin *plugin = cls;
1600 if (plugin->cur_connections <= plugin->max_connections)
1602 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1603 _("Accepting connection (%u of %u) from `%s'\n"),
1604 plugin->cur_connections, plugin->max_connections,
1605 GNUNET_a2s (addr, addr_len));
1610 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, plugin->name,
1611 _("Server reached maximum number connections (%u), rejecting new connection\n"),
1612 plugin->max_connections);
1618 server_log (void *arg, const char *fmt, va_list ap)
1622 vsnprintf (text, sizeof (text), fmt, ap);
1624 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Server: %s\n", text);
1629 * Call MHD IPv4 to process pending requests and then go back
1630 * and schedule the next run.
1631 * @param cls plugin as closure
1632 * @param tc task context
1635 server_v4_run (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1637 struct HTTP_Server_Plugin *plugin = cls;
1639 GNUNET_assert (cls != NULL);
1641 plugin->server_v4_task = GNUNET_SCHEDULER_NO_TASK;
1642 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1645 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1646 "Running IPv4 server\n");
1648 plugin->server_v4_immediately = GNUNET_NO;
1649 GNUNET_assert (MHD_YES == MHD_run (plugin->server_v4));
1650 server_reschedule (plugin, plugin->server_v4, GNUNET_NO);
1655 * Call MHD IPv6 to process pending requests and then go back
1656 * and schedule the next run.
1657 * @param cls plugin as closure
1658 * @param tc task context
1661 server_v6_run (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1663 struct HTTP_Server_Plugin *plugin = cls;
1665 GNUNET_assert (cls != NULL);
1666 plugin->server_v6_task = GNUNET_SCHEDULER_NO_TASK;
1667 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1670 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1671 "Running IPv6 server\n");
1673 plugin->server_v6_immediately = GNUNET_NO;
1674 GNUNET_assert (MHD_YES == MHD_run (plugin->server_v6));
1675 server_reschedule (plugin, plugin->server_v6, GNUNET_NO);
1679 #define UNSIGNED_MHD_LONG_LONG unsigned MHD_LONG_LONG
1682 * Function that queries MHD's select sets and
1683 * starts the task waiting for them.
1685 * @param plugin plugin
1686 * @param daemon_handle the MHD daemon handle
1687 * @return gnunet task identifier
1689 static GNUNET_SCHEDULER_TaskIdentifier
1690 server_schedule (struct HTTP_Server_Plugin *plugin,
1691 struct MHD_Daemon *daemon_handle,
1694 GNUNET_SCHEDULER_TaskIdentifier ret;
1698 struct GNUNET_NETWORK_FDSet *wrs;
1699 struct GNUNET_NETWORK_FDSet *wws;
1700 struct GNUNET_NETWORK_FDSet *wes;
1702 UNSIGNED_MHD_LONG_LONG timeout;
1703 static unsigned long long last_timeout = 0;
1706 struct GNUNET_TIME_Relative tv;
1708 if (GNUNET_YES == plugin->in_shutdown)
1709 return GNUNET_SCHEDULER_NO_TASK;
1711 ret = GNUNET_SCHEDULER_NO_TASK;
1715 wrs = GNUNET_NETWORK_fdset_create ();
1716 wes = GNUNET_NETWORK_fdset_create ();
1717 wws = GNUNET_NETWORK_fdset_create ();
1719 GNUNET_assert (MHD_YES == MHD_get_fdset (daemon_handle, &rs, &ws, &es, &max));
1720 haveto = MHD_get_timeout (daemon_handle, &timeout);
1721 if (haveto == MHD_YES)
1723 if (timeout != last_timeout)
1726 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1727 "SELECT Timeout changed from %llu to %llu\n",
1728 last_timeout, timeout);
1729 last_timeout = timeout;
1731 if (timeout <= GNUNET_TIME_UNIT_SECONDS.rel_value)
1732 tv.rel_value = (uint64_t) timeout;
1734 tv = GNUNET_TIME_UNIT_SECONDS;
1737 tv = GNUNET_TIME_UNIT_SECONDS;
1738 /* Force immediate run, since we have outbound data to send */
1739 if (now == GNUNET_YES)
1740 tv = GNUNET_TIME_UNIT_MILLISECONDS;
1741 GNUNET_NETWORK_fdset_copy_native (wrs, &rs, max + 1);
1742 GNUNET_NETWORK_fdset_copy_native (wws, &ws, max + 1);
1743 GNUNET_NETWORK_fdset_copy_native (wes, &es, max + 1);
1745 if (daemon_handle == plugin->server_v4)
1747 if (plugin->server_v4_task != GNUNET_SCHEDULER_NO_TASK)
1749 GNUNET_SCHEDULER_cancel (plugin->server_v4_task);
1750 plugin->server_v4_task = GNUNET_SCHEDULER_NO_TASK;
1753 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1754 "Scheduling IPv4 server task in %llu ms\n", tv);
1757 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1759 &server_v4_run, plugin);
1761 if (daemon_handle == plugin->server_v6)
1763 if (plugin->server_v6_task != GNUNET_SCHEDULER_NO_TASK)
1765 GNUNET_SCHEDULER_cancel (plugin->server_v6_task);
1766 plugin->server_v6_task = GNUNET_SCHEDULER_NO_TASK;
1769 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1770 "Scheduling IPv6 server task in %llu ms\n", tv);
1773 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1775 &server_v6_run, plugin);
1777 GNUNET_NETWORK_fdset_destroy (wrs);
1778 GNUNET_NETWORK_fdset_destroy (wws);
1779 GNUNET_NETWORK_fdset_destroy (wes);
1786 * Load ssl certificate from file
1788 * @param file filename
1789 * @return content of the file
1792 server_load_file (const char *file)
1794 struct GNUNET_DISK_FileHandle *gn_file;
1798 if (GNUNET_OK != GNUNET_DISK_file_size (file,
1799 &fsize, GNUNET_NO, GNUNET_YES))
1801 text = GNUNET_malloc (fsize + 1);
1803 GNUNET_DISK_file_open (file, GNUNET_DISK_OPEN_READ,
1804 GNUNET_DISK_PERM_USER_READ);
1805 if (gn_file == NULL)
1810 if (GNUNET_SYSERR == GNUNET_DISK_file_read (gn_file, text, fsize))
1813 GNUNET_DISK_file_close (gn_file);
1817 GNUNET_DISK_file_close (gn_file);
1825 * Load ssl certificate
1827 * @param plugin the plugin
1828 * @return GNUNET_OK on success, GNUNET_SYSERR on failure
1831 server_load_certificate (struct HTTP_Server_Plugin *plugin)
1833 int res = GNUNET_OK;
1839 /* Get crypto init string from config
1840 * If not present just use default values */
1843 GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg,
1848 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
1849 "Failed to get servicehome!\n");
1850 return GNUNET_SYSERR;
1855 GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg,
1858 &plugin->crypto_init))
1859 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1860 "Using crypto init string `%s'\n",
1861 plugin->crypto_init);
1863 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1864 "Using default crypto init string \n");
1867 GNUNET_CONFIGURATION_get_value_filename (plugin->env->cfg, plugin->name,
1868 "KEY_FILE", &key_file))
1871 GNUNET_asprintf (&key_file, "%s/%s", sh, "https_key.key");
1876 GNUNET_CONFIGURATION_get_value_filename (plugin->env->cfg, plugin->name,
1877 "CERT_FILE", &cert_file))
1880 GNUNET_asprintf (&cert_file, "%s/%s", sh, "https_cert.crt");
1883 /* read key & certificates from file */
1884 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1885 "Trying to loading TLS certificate from key-file `%s' cert-file`%s'\n",
1886 key_file, cert_file);
1888 plugin->key = server_load_file (key_file);
1889 plugin->cert = server_load_file (cert_file);
1891 if ((plugin->key == NULL) || (plugin->cert == NULL))
1893 struct GNUNET_OS_Process *cert_creation;
1895 GNUNET_free_non_null (plugin->key);
1897 GNUNET_free_non_null (plugin->cert);
1898 plugin->cert = NULL;
1900 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1901 "No usable TLS certificate found, creating certificate\n");
1904 GNUNET_OS_start_process (GNUNET_NO, GNUNET_OS_INHERIT_STD_OUT_AND_ERR, NULL, NULL,
1905 "gnunet-transport-certificate-creation",
1906 "gnunet-transport-certificate-creation",
1907 key_file, cert_file, NULL);
1908 if (cert_creation == NULL)
1910 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
1912 ("Could not create a new TLS certificate, program `gnunet-transport-certificate-creation' could not be started!\n"));
1913 GNUNET_free (key_file);
1914 GNUNET_free (cert_file);
1916 GNUNET_free_non_null (plugin->key);
1918 GNUNET_free_non_null (plugin->cert);
1919 plugin->cert = NULL;
1920 GNUNET_free_non_null (plugin->crypto_init);
1921 plugin->crypto_init = NULL;
1923 return GNUNET_SYSERR;
1925 GNUNET_assert (GNUNET_OK == GNUNET_OS_process_wait (cert_creation));
1926 GNUNET_OS_process_destroy (cert_creation);
1928 plugin->key = server_load_file (key_file);
1929 plugin->cert = server_load_file (cert_file);
1932 if ((plugin->key == NULL) || (plugin->cert == NULL))
1934 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
1936 ("No usable TLS certificate found and creating one failed!\n"),
1938 GNUNET_free (key_file);
1939 GNUNET_free (cert_file);
1941 GNUNET_free_non_null (plugin->key);
1943 GNUNET_free_non_null (plugin->cert);
1944 plugin->cert = NULL;
1945 GNUNET_free_non_null (plugin->crypto_init);
1946 plugin->crypto_init = NULL;
1948 return GNUNET_SYSERR;
1950 GNUNET_free (key_file);
1951 GNUNET_free (cert_file);
1952 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "TLS certificate loaded\n");
1959 * Start the HTTP server
1961 * @param plugin the plugin handle
1962 * @return GNUNET_OK on success, GNUNET_SYSERR on failure
1965 server_start (struct HTTP_Server_Plugin *plugin)
1967 unsigned int timeout;
1968 GNUNET_assert (NULL != plugin);
1971 if (GNUNET_SYSERR == server_load_certificate (plugin))
1973 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
1974 "Could not load or create server certificate! Loading plugin failed!\n");
1975 return GNUNET_SYSERR;
1980 #if MHD_VERSION >= 0x00090E00
1981 timeout = HTTP_SERVER_NOT_VALIDATED_TIMEOUT.rel_value / 1000;
1982 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1983 "MHD can set timeout per connection! Default time out %u sec.\n",
1986 timeout = SERVER_SESSION_TIMEOUT.rel_value / 1000;
1987 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, plugin->name,
1988 "MHD cannot set timeout per connection! Default time out %u sec.\n",
1991 plugin->server_v4 = NULL;
1992 if (plugin->use_ipv4 == GNUNET_YES)
1994 plugin->server_v4 = MHD_start_daemon (
2001 MHD_NO_FLAG, plugin->port,
2002 &server_accept_cb, plugin,
2003 &server_access_cb, plugin,
2004 MHD_OPTION_SOCK_ADDR,
2005 (struct sockaddr_in *)
2006 plugin->server_addr_v4,
2007 MHD_OPTION_CONNECTION_LIMIT,
2009 plugin->max_connections,
2011 MHD_OPTION_HTTPS_PRIORITIES,
2012 plugin->crypto_init,
2013 MHD_OPTION_HTTPS_MEM_KEY,
2015 MHD_OPTION_HTTPS_MEM_CERT,
2018 MHD_OPTION_CONNECTION_TIMEOUT,
2020 MHD_OPTION_CONNECTION_MEMORY_LIMIT,
2022 GNUNET_SERVER_MAX_MESSAGE_SIZE),
2023 MHD_OPTION_NOTIFY_COMPLETED,
2024 &server_disconnect_cb, plugin,
2025 MHD_OPTION_EXTERNAL_LOGGER,
2026 server_log, NULL, MHD_OPTION_END);
2028 plugin->server_v6 = NULL;
2029 if (plugin->use_ipv6 == GNUNET_YES)
2031 plugin->server_v6 = MHD_start_daemon (
2038 MHD_USE_IPv6, plugin->port,
2039 &server_accept_cb, plugin,
2040 &server_access_cb, plugin,
2041 MHD_OPTION_SOCK_ADDR,
2042 (struct sockaddr_in6 *)
2043 plugin->server_addr_v6,
2044 MHD_OPTION_CONNECTION_LIMIT,
2046 plugin->max_connections,
2048 MHD_OPTION_HTTPS_PRIORITIES,
2049 plugin->crypto_init,
2050 MHD_OPTION_HTTPS_MEM_KEY,
2052 MHD_OPTION_HTTPS_MEM_CERT,
2055 MHD_OPTION_CONNECTION_TIMEOUT,
2057 MHD_OPTION_CONNECTION_MEMORY_LIMIT,
2059 GNUNET_SERVER_MAX_MESSAGE_SIZE),
2060 MHD_OPTION_NOTIFY_COMPLETED,
2061 &server_disconnect_cb, plugin,
2062 MHD_OPTION_EXTERNAL_LOGGER,
2063 server_log, NULL, MHD_OPTION_END);
2067 if ((plugin->use_ipv4 == GNUNET_YES) && (plugin->server_v4 == NULL))
2069 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
2070 "Failed to start %s IPv4 server component on port %u\n",
2071 plugin->name, plugin->port);
2072 return GNUNET_SYSERR;
2074 server_reschedule (plugin, plugin->server_v4, GNUNET_NO);
2076 if ((plugin->use_ipv6 == GNUNET_YES) && (plugin->server_v6 == NULL))
2078 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
2079 "Failed to start %s IPv6 server component on port %u\n",
2080 plugin->name, plugin->port);
2081 return GNUNET_SYSERR;
2083 server_reschedule (plugin, plugin->server_v6, GNUNET_NO);
2084 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2085 "%s server component started on port %u\n", plugin->name,
2092 server_stop (struct HTTP_Server_Plugin *plugin)
2094 if (plugin->server_v4 != NULL)
2096 MHD_stop_daemon (plugin->server_v4);
2097 plugin->server_v4 = NULL;
2099 if ( plugin->server_v6 != NULL)
2101 MHD_stop_daemon (plugin->server_v6);
2102 plugin->server_v6 = NULL;
2106 if (plugin->server_v4_task != GNUNET_SCHEDULER_NO_TASK)
2108 GNUNET_SCHEDULER_cancel (plugin->server_v4_task);
2109 plugin->server_v4_task = GNUNET_SCHEDULER_NO_TASK;
2112 if (plugin->server_v6_task != GNUNET_SCHEDULER_NO_TASK)
2114 GNUNET_SCHEDULER_cancel (plugin->server_v6_task);
2115 plugin->server_v6_task = GNUNET_SCHEDULER_NO_TASK;
2120 GNUNET_free_non_null (plugin->crypto_init);
2121 GNUNET_free_non_null (plugin->cert);
2122 GNUNET_free_non_null (plugin->key);
2125 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2126 "%s server component stopped\n", plugin->name);
2131 * Add an address to the server's set of addresses and notify transport
2133 * @param cls the plugin handle
2134 * @param add_remove GNUNET_YES on add, GNUNET_NO on remove
2135 * @param addr the address
2136 * @param addrlen address length
2139 server_add_address (void *cls, int add_remove, const struct sockaddr *addr,
2142 struct HTTP_Server_Plugin *plugin = cls;
2143 struct HttpAddressWrapper *w = NULL;
2145 w = GNUNET_malloc (sizeof (struct HttpAddressWrapper));
2146 w->addr = http_common_address_from_socket (plugin->protocol, addr, addrlen);
2147 if (NULL == w->addr)
2152 w->addrlen = http_common_address_get_size (w->addr);
2154 GNUNET_CONTAINER_DLL_insert(plugin->addr_head, plugin->addr_tail, w);
2155 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2156 "Notifying transport to add address `%s'\n",
2157 http_common_plugin_address_to_string(NULL, w->addr, w->addrlen));
2159 plugin->env->notify_address (plugin->env->cls, add_remove, w->addr, w->addrlen, "https_client");
2161 plugin->env->notify_address (plugin->env->cls, add_remove, w->addr, w->addrlen, "http_client");
2167 * Remove an address from the server's set of addresses and notify transport
2169 * @param cls the plugin handle
2170 * @param add_remove GNUNET_YES on add, GNUNET_NO on remove
2171 * @param addr the address
2172 * @param addrlen address length
2175 server_remove_address (void *cls, int add_remove, const struct sockaddr *addr,
2178 struct HTTP_Server_Plugin *plugin = cls;
2179 struct HttpAddressWrapper *w = plugin->addr_head;
2181 void * saddr = http_common_address_from_socket (plugin->protocol, addr, addrlen);
2184 saddr_len = http_common_address_get_size (saddr);
2188 if (GNUNET_YES == http_common_cmp_addresses(w->addr, w->addrlen, saddr, saddr_len))
2192 GNUNET_free (saddr);
2197 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2198 "Notifying transport to remove address `%s'\n",
2199 http_common_plugin_address_to_string (NULL, w->addr, w->addrlen));
2200 GNUNET_CONTAINER_DLL_remove (plugin->addr_head, plugin->addr_tail, w);
2202 plugin->env->notify_address (plugin->env->cls, add_remove, w->addr, w->addrlen, "https_client");
2204 plugin->env->notify_address (plugin->env->cls, add_remove, w->addr, w->addrlen, "http_client");
2206 GNUNET_free (w->addr);
2213 * Our external IP address/port mapping has changed.
2215 * @param cls closure, the 'struct LocalAddrList'
2216 * @param add_remove GNUNET_YES to mean the new public IP address, GNUNET_NO to mean
2217 * the previous (now invalid) one
2218 * @param addr either the previous or the new public IP address
2219 * @param addrlen actual lenght of the address
2222 server_nat_port_map_callback (void *cls, int add_remove, const struct sockaddr *addr,
2225 GNUNET_assert (cls != NULL);
2226 struct HTTP_Server_Plugin *plugin = cls;
2228 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2229 "NAT called to %s address `%s'\n",
2230 (add_remove == GNUNET_NO) ? "remove" : "add",
2231 GNUNET_a2s (addr, addrlen));
2233 if (AF_INET == addr->sa_family)
2235 struct sockaddr_in *s4 = (struct sockaddr_in *) addr;
2237 if (GNUNET_NO == plugin->use_ipv4)
2240 if ((NULL != plugin->server_addr_v4) &&
2241 (0 != memcmp (&plugin->server_addr_v4->sin_addr,
2242 &s4->sin_addr, sizeof (struct in_addr))))
2244 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2245 "Skipping address `%s' (not bindto address)\n",
2246 GNUNET_a2s (addr, addrlen));
2251 if (AF_INET6 == addr->sa_family)
2253 struct sockaddr_in6 *s6 = (struct sockaddr_in6 *) addr;
2254 if (GNUNET_NO == plugin->use_ipv6)
2257 if ((NULL != plugin->server_addr_v6) &&
2258 (0 != memcmp (&plugin->server_addr_v6->sin6_addr,
2259 &s6->sin6_addr, sizeof (struct in6_addr))))
2261 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2262 "Skipping address `%s' (not bindto address)\n",
2263 GNUNET_a2s (addr, addrlen));
2271 server_add_address (cls, add_remove, addr, addrlen);
2274 server_remove_address (cls, add_remove, addr, addrlen);
2281 * Get valid server addresses
2283 * @param plugin the plugin handle
2284 * @param serviceName the servicename
2285 * @param cfg configuration handle
2286 * @param addrs addresses
2287 * @param addr_lens address length
2288 * @return number of addresses
2291 server_get_addresses (struct HTTP_Server_Plugin *plugin,
2292 const char *serviceName,
2293 const struct GNUNET_CONFIGURATION_Handle *cfg,
2294 struct sockaddr ***addrs, socklen_t ** addr_lens)
2297 unsigned long long port;
2298 struct addrinfo hints;
2299 struct addrinfo *res;
2300 struct addrinfo *pos;
2301 struct addrinfo *next;
2305 struct sockaddr **saddrs;
2306 socklen_t *saddrlens;
2312 disablev6 = !plugin->use_ipv6;
2315 if (GNUNET_CONFIGURATION_have_value (cfg, serviceName, "PORT"))
2317 GNUNET_break (GNUNET_OK ==
2318 GNUNET_CONFIGURATION_get_value_number (cfg, serviceName,
2322 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2324 ("Require valid port number for service in configuration!\n"));
2325 return GNUNET_SYSERR;
2330 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, plugin->name,
2331 "Starting in listen only mode\n");
2332 return -1; /* listen only */
2336 if (GNUNET_CONFIGURATION_have_value (cfg, serviceName, "BINDTO"))
2338 GNUNET_break (GNUNET_OK ==
2339 GNUNET_CONFIGURATION_get_value_string (cfg, serviceName,
2340 "BINDTO", &hostname));
2345 if (hostname != NULL)
2347 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2348 "Resolving `%s' since that is where `%s' will bind to.\n",
2349 hostname, serviceName);
2350 memset (&hints, 0, sizeof (struct addrinfo));
2352 hints.ai_family = AF_INET;
2353 if ((0 != (ret = getaddrinfo (hostname, NULL, &hints, &res))) ||
2356 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Failed to resolve `%s': %s\n"),
2357 hostname, gai_strerror (ret));
2358 GNUNET_free (hostname);
2359 return GNUNET_SYSERR;
2363 while (NULL != (pos = next))
2365 next = pos->ai_next;
2366 if ((disablev6) && (pos->ai_family == AF_INET6))
2372 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2373 _("Failed to find %saddress for `%s'.\n"),
2374 disablev6 ? "IPv4 " : "", hostname);
2376 GNUNET_free (hostname);
2377 return GNUNET_SYSERR;
2380 saddrs = GNUNET_malloc ((resi + 1) * sizeof (struct sockaddr *));
2381 saddrlens = GNUNET_malloc ((resi + 1) * sizeof (socklen_t));
2384 while (NULL != (pos = next))
2386 next = pos->ai_next;
2387 if ((disablev6) && (pos->ai_family == AF_INET6))
2389 if ((pos->ai_protocol != IPPROTO_TCP) && (pos->ai_protocol != 0))
2390 continue; /* not TCP */
2391 if ((pos->ai_socktype != SOCK_STREAM) && (pos->ai_socktype != 0))
2392 continue; /* huh? */
2393 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2394 "Service will bind to `%s'\n", GNUNET_a2s (pos->ai_addr,
2396 if (pos->ai_family == AF_INET)
2398 GNUNET_assert (pos->ai_addrlen == sizeof (struct sockaddr_in));
2399 saddrlens[i] = pos->ai_addrlen;
2400 saddrs[i] = GNUNET_malloc (saddrlens[i]);
2401 memcpy (saddrs[i], pos->ai_addr, saddrlens[i]);
2402 ((struct sockaddr_in *) saddrs[i])->sin_port = htons (port);
2406 GNUNET_assert (pos->ai_family == AF_INET6);
2407 GNUNET_assert (pos->ai_addrlen == sizeof (struct sockaddr_in6));
2408 saddrlens[i] = pos->ai_addrlen;
2409 saddrs[i] = GNUNET_malloc (saddrlens[i]);
2410 memcpy (saddrs[i], pos->ai_addr, saddrlens[i]);
2411 ((struct sockaddr_in6 *) saddrs[i])->sin6_port = htons (port);
2415 GNUNET_free (hostname);
2421 /* will bind against everything, just set port */
2427 saddrs = GNUNET_malloc ((resi + 1) * sizeof (struct sockaddr *));
2428 saddrlens = GNUNET_malloc ((resi + 1) * sizeof (socklen_t));
2430 saddrlens[i] = sizeof (struct sockaddr_in);
2431 saddrs[i] = GNUNET_malloc (saddrlens[i]);
2432 #if HAVE_SOCKADDR_IN_SIN_LEN
2433 ((struct sockaddr_in *) saddrs[i])->sin_len = saddrlens[i];
2435 ((struct sockaddr_in *) saddrs[i])->sin_family = AF_INET;
2436 ((struct sockaddr_in *) saddrs[i])->sin_port = htons (port);
2442 saddrs = GNUNET_malloc ((resi + 1) * sizeof (struct sockaddr *));
2443 saddrlens = GNUNET_malloc ((resi + 1) * sizeof (socklen_t));
2445 saddrlens[i] = sizeof (struct sockaddr_in6);
2446 saddrs[i] = GNUNET_malloc (saddrlens[i]);
2447 #if HAVE_SOCKADDR_IN_SIN_LEN
2448 ((struct sockaddr_in6 *) saddrs[i])->sin6_len = saddrlens[0];
2450 ((struct sockaddr_in6 *) saddrs[i])->sin6_family = AF_INET6;
2451 ((struct sockaddr_in6 *) saddrs[i])->sin6_port = htons (port);
2453 saddrlens[i] = sizeof (struct sockaddr_in);
2454 saddrs[i] = GNUNET_malloc (saddrlens[i]);
2455 #if HAVE_SOCKADDR_IN_SIN_LEN
2456 ((struct sockaddr_in *) saddrs[i])->sin_len = saddrlens[1];
2458 ((struct sockaddr_in *) saddrs[i])->sin_family = AF_INET;
2459 ((struct sockaddr_in *) saddrs[i])->sin_port = htons (port);
2463 *addr_lens = saddrlens;
2469 * Ask NAT for addresses
2471 * @param plugin the plugin handle
2474 server_start_report_addresses (struct HTTP_Server_Plugin *plugin)
2476 int res = GNUNET_OK;
2477 struct sockaddr **addrs;
2478 socklen_t *addrlens;
2480 res = server_get_addresses (plugin,
2481 plugin->name, plugin->env->cfg,
2483 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2484 _("Found %u addresses to report to NAT service\n"), res);
2486 if (GNUNET_SYSERR == res)
2493 GNUNET_NAT_register (plugin->env->cfg, GNUNET_YES, plugin->port,
2495 (const struct sockaddr **) addrs, addrlens,
2496 &server_nat_port_map_callback, NULL, plugin);
2500 GNUNET_assert (addrs[res] != NULL);
2501 GNUNET_free (addrs[res]);
2503 GNUNET_free_non_null (addrs);
2504 GNUNET_free_non_null (addrlens);
2509 * Stop NAT for addresses
2511 * @param plugin the plugin handle
2514 server_stop_report_addresses (struct HTTP_Server_Plugin *plugin)
2516 /* Stop NAT handle */
2517 if (NULL != plugin->nat)
2518 GNUNET_NAT_unregister (plugin->nat);
2520 /* Clean up addresses */
2521 struct HttpAddressWrapper *w;
2523 while (plugin->addr_head != NULL)
2525 w = plugin->addr_head;
2526 GNUNET_CONTAINER_DLL_remove (plugin->addr_head, plugin->addr_tail, w);
2527 GNUNET_free (w->addr);
2534 * Check if IPv6 supported on this system
2536 * @param plugin the plugin handle
2537 * @return GNUNET_YES on success, else GNUNET_NO
2540 server_check_ipv6_support (struct HTTP_Server_Plugin *plugin)
2542 struct GNUNET_NETWORK_Handle *desc = NULL;
2543 int res = GNUNET_NO;
2545 /* Probe IPv6 support */
2546 desc = GNUNET_NETWORK_socket_create (PF_INET6, SOCK_STREAM, 0);
2549 if ((errno == ENOBUFS) || (errno == ENOMEM) || (errno == ENFILE) ||
2552 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "socket");
2554 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, plugin->name,
2556 ("Disabling IPv6 since it is not supported on this system!\n"));
2561 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (desc));
2565 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2566 "Testing IPv6 on this system: %s\n",
2567 (res == GNUNET_YES) ? "successful" : "failed");
2573 * Function called when the service shuts down. Unloads our plugins
2574 * and cancels pending validations.
2576 * @param cls closure, unused
2577 * @param tc task context (unused)
2580 server_notify_external_hostname (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2582 struct HTTP_Server_Plugin *plugin = cls;
2584 plugin->notify_ext_task = GNUNET_SCHEDULER_NO_TASK;
2586 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
2589 GNUNET_asprintf(&plugin->ext_addr, "%s://%s", plugin->protocol, plugin->external_hostname);
2590 plugin->ext_addr_len = strlen (plugin->ext_addr) + 1;
2591 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2592 "Notifying transport about external hostname address `%s'\n", plugin->ext_addr);
2595 plugin->env->notify_address (plugin->env->cls, GNUNET_YES,
2596 plugin->ext_addr, plugin->ext_addr_len,
2599 plugin->env->notify_address (plugin->env->cls, GNUNET_YES,
2600 plugin->ext_addr, plugin->ext_addr_len,
2607 * Configure the plugin
2609 * @param plugin plugin handle
2610 * @return GNUNET_OK on success, GNUNET_SYSERR on failure
2613 server_configure_plugin (struct HTTP_Server_Plugin *plugin)
2615 unsigned long long port;
2616 unsigned long long max_connections;
2617 char *bind4_address = NULL;
2618 char *bind6_address = NULL;
2621 if (GNUNET_CONFIGURATION_have_value
2622 (plugin->env->cfg, plugin->name, "USE_IPv4"))
2625 GNUNET_CONFIGURATION_get_value_yesno (plugin->env->cfg, plugin->name,
2629 plugin->use_ipv4 = GNUNET_YES;
2630 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2631 _("IPv4 support is %s\n"),
2632 (plugin->use_ipv4 == GNUNET_YES) ? "enabled" : "disabled");
2635 if (GNUNET_CONFIGURATION_have_value
2636 (plugin->env->cfg, plugin->name, "USE_IPv6"))
2639 GNUNET_CONFIGURATION_get_value_yesno (plugin->env->cfg, plugin->name,
2643 plugin->use_ipv6 = GNUNET_YES;
2644 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2645 _("IPv6 support is %s\n"),
2646 (plugin->use_ipv6 == GNUNET_YES) ? "enabled" : "disabled");
2648 if ((plugin->use_ipv4 == GNUNET_NO) && (plugin->use_ipv6 == GNUNET_NO))
2650 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
2652 ("Neither IPv4 nor IPv6 are enabled! Fix in configuration\n"),
2654 return GNUNET_SYSERR;
2657 /* Reading port number from config file */
2659 GNUNET_CONFIGURATION_get_value_number (plugin->env->cfg, plugin->name,
2660 "PORT", &port)) || (port > 65535))
2662 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
2663 _("Port is required! Fix in configuration\n"),
2665 return GNUNET_SYSERR;
2667 plugin->port = port;
2669 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2670 _("Using port %u\n"), plugin->port);
2672 if ((plugin->use_ipv4 == GNUNET_YES) &&
2673 (GNUNET_YES == GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg,
2674 plugin->name, "BINDTO", &bind4_address)))
2676 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2677 "Binding %s plugin to specific IPv4 address: `%s'\n",
2678 plugin->protocol, bind4_address);
2679 plugin->server_addr_v4 = GNUNET_malloc (sizeof (struct sockaddr_in));
2680 if (1 != inet_pton (AF_INET, bind4_address,
2681 &plugin->server_addr_v4->sin_addr))
2683 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
2685 ("Specific IPv4 address `%s' in configuration file is invalid!\n"),
2687 GNUNET_free (bind4_address);
2688 GNUNET_free (plugin->server_addr_v4);
2689 plugin->server_addr_v4 = NULL;
2690 return GNUNET_SYSERR;
2694 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2695 _("Binding to IPv4 address %s\n"), bind4_address);
2696 plugin->server_addr_v4->sin_family = AF_INET;
2697 plugin->server_addr_v4->sin_port = htons (plugin->port);
2699 GNUNET_free (bind4_address);
2702 if ((plugin->use_ipv6 == GNUNET_YES) &&
2704 GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg, plugin->name,
2705 "BINDTO6", &bind6_address)))
2707 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2708 "Binding %s plugin to specific IPv6 address: `%s'\n",
2709 plugin->protocol, bind6_address);
2710 plugin->server_addr_v6 = GNUNET_malloc (sizeof (struct sockaddr_in6));
2712 inet_pton (AF_INET6, bind6_address, &plugin->server_addr_v6->sin6_addr))
2714 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
2716 ("Specific IPv6 address `%s' in configuration file is invalid!\n"),
2718 GNUNET_free (bind6_address);
2719 GNUNET_free (plugin->server_addr_v6);
2720 plugin->server_addr_v6 = NULL;
2721 return GNUNET_SYSERR;
2725 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2726 _("Binding to IPv6 address %s\n"), bind6_address);
2727 plugin->server_addr_v6->sin6_family = AF_INET6;
2728 plugin->server_addr_v6->sin6_port = htons (plugin->port);
2730 GNUNET_free (bind6_address);
2733 if (GNUNET_YES == GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg, plugin->name,
2734 "EXTERNAL_HOSTNAME", &plugin->external_hostname))
2737 if (NULL != strstr(plugin->external_hostname, "://"))
2739 tmp = strdup(&strstr(plugin->external_hostname, "://")[3]);
2740 GNUNET_free (plugin->external_hostname);
2741 plugin->external_hostname = tmp;
2744 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2745 _("Using external hostname `%s'\n"), plugin->external_hostname);
2746 plugin->notify_ext_task = GNUNET_SCHEDULER_add_now (&server_notify_external_hostname, plugin);
2748 /* Use only configured external hostname */
2749 if (GNUNET_CONFIGURATION_have_value
2750 (plugin->env->cfg, plugin->name, "EXTERNAL_HOSTNAME_ONLY"))
2752 plugin->external_only =
2753 GNUNET_CONFIGURATION_get_value_yesno (plugin->env->cfg, plugin->name,
2754 "EXTERNAL_HOSTNAME_ONLY");
2757 plugin->external_only = GNUNET_NO;
2759 if (GNUNET_YES == plugin->external_only)
2760 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2761 _("Notifying transport only about hostname `%s'\n"), plugin->external_hostname);
2764 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2765 "No external hostname configured\n");
2767 /* Optional parameters */
2768 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (plugin->env->cfg,
2770 "MAX_CONNECTIONS", &max_connections))
2771 max_connections = 128;
2772 plugin->max_connections = max_connections;
2774 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2775 _("Maximum number of connections is %u\n"),
2776 plugin->max_connections);
2779 plugin->peer_id_length = strlen (GNUNET_h2s_full (&plugin->env->my_identity->hashPubKey));
2786 * Session was idle, so disconnect it
2788 * @param cls the session
2789 * @param tc task context
2792 server_session_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2794 GNUNET_assert (NULL != cls);
2795 struct Session *s = cls;
2797 s->timeout_task = GNUNET_SCHEDULER_NO_TASK;
2798 GNUNET_log (TIMEOUT_LOG,
2799 "Session %p was idle for %llu ms, disconnecting\n",
2800 s, (unsigned long long) SERVER_SESSION_TIMEOUT.rel_value);
2802 /* call session destroy function */
2803 GNUNET_assert (GNUNET_OK == server_disconnect (s));
2808 * Start session timeout for session s
2810 * @param s the session
2813 server_start_session_timeout (struct Session *s)
2815 GNUNET_assert (NULL != s);
2816 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == s->timeout_task);
2817 s->timeout_task = GNUNET_SCHEDULER_add_delayed (SERVER_SESSION_TIMEOUT,
2818 &server_session_timeout,
2820 GNUNET_log (TIMEOUT_LOG,
2821 "Timeout for session %p set to %llu ms\n",
2822 s, (unsigned long long) SERVER_SESSION_TIMEOUT.rel_value);
2827 * Increment session timeout due to activity session s
2829 * @param s the session
2832 server_reschedule_session_timeout (struct Session *s)
2834 GNUNET_assert (NULL != s);
2835 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK != s->timeout_task);
2837 GNUNET_SCHEDULER_cancel (s->timeout_task);
2838 s->timeout_task = GNUNET_SCHEDULER_add_delayed (SERVER_SESSION_TIMEOUT,
2839 &server_session_timeout,
2841 GNUNET_log (TIMEOUT_LOG,
2842 "Timeout rescheduled for session %p set to %llu ms\n",
2843 s, (unsigned long long) SERVER_SESSION_TIMEOUT.rel_value);
2848 * Exit point from the plugin.
2854 LIBGNUNET_PLUGIN_TRANSPORT_DONE (void *cls)
2856 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
2857 struct HTTP_Server_Plugin *plugin = api->cls;
2858 struct Session *pos;
2859 struct Session *next;
2861 if (NULL == api->cls)
2863 /* Free for stub mode */
2867 plugin->in_shutdown = GNUNET_YES;
2868 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2869 _("Shutting down plugin `%s'\n"),
2872 if (GNUNET_SCHEDULER_NO_TASK != plugin->notify_ext_task)
2874 GNUNET_SCHEDULER_cancel (plugin->notify_ext_task);
2875 plugin->notify_ext_task = GNUNET_SCHEDULER_NO_TASK;
2878 if (NULL != plugin->ext_addr)
2880 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2881 "Notifying transport to remove address `%s'\n",
2882 http_common_plugin_address_to_string (NULL,
2884 plugin->ext_addr_len));
2886 plugin->env->notify_address (plugin->env->cls,
2889 plugin->ext_addr_len,
2892 plugin->env->notify_address (plugin->env->cls,
2895 plugin->ext_addr_len,
2901 /* Stop to report addresses to transport service */
2902 server_stop_report_addresses (plugin);
2903 server_stop (plugin);
2904 next = plugin->head;
2905 while (NULL != (pos = next))
2908 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2909 "Removing left over session %p\n", pos);
2911 if ((GNUNET_YES == pos->session_passed) && (GNUNET_NO == pos->session_ended))
2913 /* Notify transport immediately that this session is invalid */
2914 pos->session_ended = GNUNET_YES;
2915 plugin->env->session_end (plugin->env->cls, &pos->target, pos);
2918 server_delete_session (pos);
2922 GNUNET_free_non_null (plugin->external_hostname);
2923 GNUNET_free_non_null (plugin->ext_addr);
2924 GNUNET_free_non_null (plugin->server_addr_v4);
2925 GNUNET_free_non_null (plugin->server_addr_v6);
2927 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2928 _("Shutdown for plugin `%s' complete\n"),
2931 GNUNET_free (plugin);
2938 * Entry point for the plugin.
2944 LIBGNUNET_PLUGIN_TRANSPORT_INIT (void *cls)
2946 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
2947 struct GNUNET_TRANSPORT_PluginFunctions *api;
2948 struct HTTP_Server_Plugin *plugin;
2950 plugin = GNUNET_malloc (sizeof (struct HTTP_Server_Plugin));
2954 if (NULL == env->receive)
2956 /* run in 'stub' mode (i.e. as part of gnunet-peerinfo), don't fully
2957 initialze the plugin or the API */
2958 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
2960 api->address_to_string = &http_common_plugin_address_to_string;
2961 api->string_to_address = &http_common_plugin_string_to_address;
2962 api->address_pretty_printer = &http_common_plugin_address_pretty_printer;
2966 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
2968 api->send = &http_server_plugin_send;
2969 api->disconnect = &http_server_plugin_disconnect;
2970 api->check_address = &http_server_plugin_address_suggested;
2971 api->get_session = &http_server_plugin_get_session;
2973 api->address_to_string = &http_common_plugin_address_to_string;
2974 api->string_to_address = &http_common_plugin_string_to_address;
2975 api->address_pretty_printer = &http_common_plugin_address_pretty_printer;
2978 plugin->name = "transport-https_server";
2979 plugin->protocol = "https";
2981 plugin->name = "transport-http_server";
2982 plugin->protocol = "http";
2985 /* Configure plugin */
2986 if (GNUNET_SYSERR == server_configure_plugin (plugin))
2988 LIBGNUNET_PLUGIN_TRANSPORT_DONE (api);
2992 /* Check IPv6 support */
2993 if (GNUNET_YES == plugin->use_ipv6)
2994 plugin->use_ipv6 = server_check_ipv6_support (plugin);
2996 /* Report addresses to transport service */
2997 if (GNUNET_NO == plugin->external_only)
2998 server_start_report_addresses (plugin);
3000 if (GNUNET_SYSERR == server_start (plugin))
3002 LIBGNUNET_PLUGIN_TRANSPORT_DONE (api);
3009 /* end of plugin_transport_http_server.c */