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_protocols.h"
29 #include "gnunet_connection_lib.h"
30 #include "gnunet_server_lib.h"
31 #include "gnunet_service_lib.h"
32 #include "gnunet_statistics_service.h"
33 #include "gnunet_transport_service.h"
34 #include "gnunet_transport_plugin.h"
36 #include "gnunet_container_lib.h"
37 #include "gnunet_nat_lib.h"
38 #include "plugin_transport_http_common.h"
39 #include "microhttpd.h"
42 #define LIBGNUNET_PLUGIN_TRANSPORT_INIT libgnunet_plugin_transport_https_server_init
43 #define LIBGNUNET_PLUGIN_TRANSPORT_DONE libgnunet_plugin_transport_https_server_done
45 #define LIBGNUNET_PLUGIN_TRANSPORT_INIT libgnunet_plugin_transport_http_server_init
46 #define LIBGNUNET_PLUGIN_TRANSPORT_DONE libgnunet_plugin_transport_http_server_done
49 #define HTTP_NOT_VALIDATED_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 15)
51 #define TESTING GNUNET_NO
54 #define TIMEOUT_LOG GNUNET_ERROR_TYPE_ERROR
55 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
57 #define TIMEOUT_LOG GNUNET_ERROR_TYPE_DEBUG
58 #define TIMEOUT GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT
61 #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>"
66 * Encapsulation of all of the state of the plugin.
71 * Session handle for connections.
76 * Stored in a linked list.
81 * Stored in a linked list.
86 * To whom are we talking to (set to our identity
87 * if we are still waiting for the welcome message)
89 struct GNUNET_PeerIdentity target;
92 * Pointer to the global plugin struct.
94 struct HTTP_Server_Plugin *plugin;
97 * next pointer for double linked list
99 struct HTTP_Message *msg_head;
102 * previous pointer for double linked list
104 struct HTTP_Message *msg_tail;
107 * Message stream tokenizer for incoming data
109 struct GNUNET_SERVER_MessageStreamTokenizer *msg_tk;
114 struct ServerConnection *server_recv;
119 struct ServerConnection *server_send;
129 * Inbound or outbound connection
130 * Outbound: GNUNET_NO (client is used to send and receive)
131 * Inbound : GNUNET_YES (server is used to send and receive)
136 * Unique HTTP/S connection tag for this connection
141 * ATS network type in NBO
143 uint32_t ats_address_network_type;
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;
157 struct ServerConnection
162 /* Should this connection get disconnected? GNUNET_YES/NO */
165 /* The session this server connection belongs to */
166 struct Session *session;
168 /* The MHD connection */
169 struct MHD_Connection *mhd_conn;
172 struct MHD_Daemon *mhd_daemon;
176 * Encapsulation of all of the state of the plugin.
178 struct HTTP_Server_Plugin
183 struct GNUNET_TRANSPORT_PluginEnvironment *env;
186 * Linked list head of open sessions.
189 struct Session *head;
192 * Linked list tail of open sessions.
194 struct Session *tail;
209 char *external_hostname;
212 * Maximum number of sockets the plugin can use
213 * Each http inbound /outbound connections are two connections
215 unsigned int max_connections;
218 * Current number of sockets the plugin can use
219 * Each http inbound /outbound connections are two connections
221 unsigned int cur_connections;
224 * External hostname the plugin can be connected to, can be different to
225 * the host's FQDN, used e.g. for reverse proxying
230 * External address length
250 * Task calling transport service about external address
252 GNUNET_SCHEDULER_TaskIdentifier notify_ext_task;
255 * NAT handle & address management
257 struct GNUNET_NAT_Handle *nat;
260 * Server semi connections
261 * A full session consists of 2 semi-connections: send and receive
262 * If not both directions are established the server keeps this sessions here
264 struct Session *server_semi_head;
266 struct Session *server_semi_tail;
269 * List of own addresses
273 * IPv4 addresses DLL head
275 struct HttpAddressWrapper *addr_head;
278 * IPv4 addresses DLL tail
280 struct HttpAddressWrapper *addr_tail;
283 * IPv4 server socket to bind to
285 struct sockaddr_in *server_addr_v4;
288 * IPv6 server socket to bind to
290 struct sockaddr_in6 *server_addr_v6;
295 GNUNET_SCHEDULER_TaskIdentifier server_v4_task;
300 GNUNET_SCHEDULER_TaskIdentifier server_v6_task;
303 * The IPv4 server is scheduled to run asap
305 int server_v4_immediately;
308 * The IPv6 server is scheduled to run asap
310 int server_v6_immediately;
315 struct MHD_Daemon *server_v4;
320 struct MHD_Daemon *server_v6;
328 * Use RC4-128 instead of AES:
329 * NONE:+VERS-TLS1.0:+ARCFOUR-128:+SHA1:+RSA:+COMP-NULL
348 * Wrapper to manage addresses
350 struct HttpAddressWrapper
355 struct HttpAddressWrapper *next;
358 * Linked list previous
360 struct HttpAddressWrapper *prev;
368 * Message to send using http
373 * next pointer for double linked list
375 struct HTTP_Message *next;
378 * previous pointer for double linked list
380 struct HTTP_Message *prev;
383 * buffer containing data to send
388 * amount of data already sent
398 * Continuation function to call once the transmission buffer
399 * has again space available. NULL if there is no
400 * continuation to call.
402 GNUNET_TRANSPORT_TransmitContinuation transmit_cont;
405 * Closure for transmit_cont.
407 void *transmit_cont_cls;
411 static struct HTTP_Server_Plugin * p;
414 * Start session timeout
417 server_start_session_timeout (struct Session *s);
420 * Increment session timeout due to activity
423 server_reschedule_session_timeout (struct Session *s);
429 server_stop_session_timeout (struct Session *s);
432 * Function that can be used by the transport service to transmit
433 * a message using the plugin. Note that in the case of a
434 * peer disconnecting, the continuation MUST be called
435 * prior to the disconnect notification itself. This function
436 * will be called with this peer's HELLO message to initiate
437 * a fresh connection to another peer.
440 * @param session which session must be used
441 * @param msgbuf the message to transmit
442 * @param msgbuf_size number of bytes in 'msgbuf'
443 * @param priority how important is the message (most plugins will
444 * ignore message priority and just FIFO)
445 * @param to how long to wait at most for the transmission (does not
446 * require plugins to discard the message after the timeout,
447 * just advisory for the desired delay; most plugins will ignore
449 * @param cont continuation to call once the message has
450 * been transmitted (or if the transport is ready
451 * for the next transmission call; or if the
452 * peer disconnected...); can be NULL
453 * @param cont_cls closure for cont
454 * @return number of bytes used (on the physical network, with overheads);
455 * -1 on hard errors (i.e. address invalid); 0 is a legal value
456 * and does NOT mean that the message was not transmitted (DV)
459 http_server_plugin_send (void *cls,
460 struct Session *session,
461 const char *msgbuf, size_t msgbuf_size,
462 unsigned int priority,
463 struct GNUNET_TIME_Relative to,
464 GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
466 struct HTTP_Server_Plugin *plugin = cls;
469 GNUNET_assert (plugin != NULL);
470 GNUNET_assert (session != NULL);
474 /* struct Plugin *plugin = cls; */
481 * Function that can be used to force the plugin to disconnect
482 * from the given peer and cancel all previous transmissions
483 * (and their continuationc).
486 * @param target peer from which to disconnect
489 http_server_plugin_disconnect (void *cls, const struct GNUNET_PeerIdentity *target)
491 // struct Plugin *plugin = cls;
497 * Another peer has suggested an address for this
498 * peer and transport plugin. Check that this could be a valid
499 * address. If so, consider adding it to the list
503 * @param addr pointer to the address
504 * @param addrlen length of addr
505 * @return GNUNET_OK if this is a plausible address for this peer
509 http_server_plugin_address_suggested (void *cls, const void *addr, size_t addrlen)
511 struct HTTP_Server_Plugin *plugin = cls;
512 struct HttpAddressWrapper *w = plugin->addr_head;
514 if ((NULL != plugin->ext_addr) && GNUNET_YES == (http_common_cmp_addresses (addr, addrlen, plugin->ext_addr, plugin->ext_addr_len)))
519 if (GNUNET_YES == (http_common_cmp_addresses(addr,
530 * Creates a new outbound session the transport
531 * service will use to send data to the peer
533 * Since HTTP/S server cannot create sessions, always return NULL
535 * @param cls the plugin
536 * @param address the address
537 * @return always NULL
539 static struct Session *
540 http_server_plugin_get_session (void *cls,
541 const struct GNUNET_HELLO_Address *address)
548 * Deleting the session
549 * Must not be used afterwards
553 server_delete_session (struct Session *s)
555 struct HTTP_Server_Plugin *plugin = s->plugin;
556 server_stop_session_timeout(s);
558 GNUNET_CONTAINER_DLL_remove (plugin->head, plugin->tail, s);
559 struct HTTP_Message *msg = s->msg_head;
560 struct HTTP_Message *tmp = NULL;
566 GNUNET_CONTAINER_DLL_remove (s->msg_head, s->msg_tail, msg);
567 if (msg->transmit_cont != NULL)
569 msg->transmit_cont (msg->transmit_cont_cls, &s->target, GNUNET_SYSERR);
575 if (s->msg_tk != NULL)
577 GNUNET_SERVER_mst_destroy (s->msg_tk);
580 GNUNET_free (s->addr);
581 GNUNET_free_non_null (s->server_recv);
582 GNUNET_free_non_null (s->server_send);
587 server_disconnect (struct Session *s)
589 struct ServerConnection * send;
590 struct ServerConnection * recv;
592 send = (struct ServerConnection *) s->server_send;
593 if (s->server_send != NULL)
595 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, s->plugin->name,
596 "Server: %p / %p Terminating inbound PUT session to peer `%s'\n",
597 s, s->server_send, GNUNET_i2s (&s->target));
599 send->disconnect = GNUNET_YES;
600 #if MHD_VERSION >= 0x00090E00
601 MHD_set_connection_option (send->mhd_conn, MHD_CONNECTION_OPTION_TIMEOUT,
606 recv = (struct ServerConnection *) s->server_recv;
609 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, s->plugin->name,
610 "Server: %p / %p Terminating inbound GET session to peer `%s'\n",
611 s, s->server_recv, GNUNET_i2s (&s->target));
613 recv->disconnect = GNUNET_YES;
614 #if MHD_VERSION >= 0x00090E00
615 MHD_set_connection_option (recv->mhd_conn, MHD_CONNECTION_OPTION_TIMEOUT,
620 /* Schedule connection immediately */
622 if (s->addrlen == sizeof (struct IPv4HttpAddress))
624 server_reschedule (s->plugin, s->plugin->server_v4, GNUNET_YES);
626 else if (s->addrlen == sizeof (struct IPv6HttpAddress))
628 server_reschedule (s->plugin, s->plugin->server_v6, GNUNET_YES);
640 server_stop_session_timeout (struct Session *s)
642 GNUNET_assert (NULL != s);
644 if (GNUNET_SCHEDULER_NO_TASK != s->timeout_task)
646 GNUNET_SCHEDULER_cancel (s->timeout_task);
647 s->timeout_task = GNUNET_SCHEDULER_NO_TASK;
648 GNUNET_log (TIMEOUT_LOG, "Timeout stopped for session %p\n", s);
653 * Function that queries MHD's select sets and
654 * starts the task waiting for them.
655 * @param plugin plugin
656 * @param daemon_handle the MHD daemon handle
657 * @return gnunet task identifier
659 static GNUNET_SCHEDULER_TaskIdentifier
660 server_schedule (struct HTTP_Server_Plugin *plugin, struct MHD_Daemon *daemon_handle,
664 * Reschedule the execution of both IPv4 and IPv6 server
665 * @param plugin the plugin
666 * @param server which server to schedule v4 or v6?
667 * @param now GNUNET_YES to schedule execution immediately, GNUNET_NO to wait
671 server_reschedule (struct HTTP_Server_Plugin *plugin, struct MHD_Daemon *server, int now)
673 if ((server == plugin->server_v4) && (plugin->server_v4 != NULL))
675 if (GNUNET_YES == plugin->server_v4_immediately)
676 return; /* No rescheduling, server will run asap */
678 if (GNUNET_YES == now)
679 plugin->server_v4_immediately = GNUNET_YES;
681 if (plugin->server_v4_task != GNUNET_SCHEDULER_NO_TASK)
683 GNUNET_SCHEDULER_cancel (plugin->server_v4_task);
684 plugin->server_v4_task = GNUNET_SCHEDULER_NO_TASK;
686 plugin->server_v4_task = server_schedule (plugin, plugin->server_v4, now);
689 if ((server == plugin->server_v6) && (plugin->server_v6 != NULL))
691 if (GNUNET_YES == plugin->server_v6_immediately)
692 return; /* No rescheduling, server will run asap */
694 if (GNUNET_YES == now)
695 plugin->server_v6_immediately = GNUNET_YES;
697 if (plugin->server_v6_task != GNUNET_SCHEDULER_NO_TASK)
699 GNUNET_SCHEDULER_cancel (plugin->server_v6_task);
700 plugin->server_v6_task = GNUNET_SCHEDULER_NO_TASK;
702 plugin->server_v6_task = server_schedule (plugin, plugin->server_v6, now);
707 static struct ServerConnection *
708 server_lookup_connection (struct HTTP_Server_Plugin *plugin,
709 struct MHD_Connection *mhd_connection, const char *url,
712 struct Session *s = NULL;
714 struct ServerConnection *sc = NULL;
715 const union MHD_ConnectionInfo *conn_info;
716 struct GNUNET_ATS_Information ats;
721 struct GNUNET_PeerIdentity target;
723 int direction = GNUNET_SYSERR;
725 /* url parsing variables */
733 conn_info = MHD_get_connection_info (mhd_connection,
734 MHD_CONNECTION_INFO_CLIENT_ADDRESS);
735 if ((conn_info->client_addr->sa_family != AF_INET) &&
736 (conn_info->client_addr->sa_family != AF_INET6))
739 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
740 "New %s connection from %s\n", method, url);
742 * URL is valid if it is in the form [peerid[103];tag]*/
743 url_len = strlen (url);
744 url_end = (char *) &url[url_len];
749 goto error; /* too short */
751 hash_start = strrchr (url, '/');
752 if (NULL == hash_start)
755 goto error; /* '/' delimiter not found */
757 if (hash_start >= url_end)
760 goto error; /* mal formed */
764 hash_end = strrchr (hash_start, ';');
765 if (NULL == hash_end)
766 goto error; /* ';' delimiter not found */
767 if (hash_end >= url_end)
770 goto error; /* mal formed */
773 if (hash_start >= hash_end)
776 goto error; /* mal formed */
779 if ((strlen(hash_start) - strlen(hash_end)) != 103)
782 goto error; /* invalid hash length */
786 memcpy (hash, hash_start, 103);
788 if (GNUNET_OK != GNUNET_CRYPTO_hash_from_string ((const char *) hash, &(target.hashPubKey)))
791 goto error; /* mal formed */
794 if (hash_end >= url_end)
797 goto error; /* mal formed */
800 tag_start = &hash_end[1];
803 tag = strtoul (tag_start, &tag_end, 10);
806 goto error; /* mal formed */
810 goto error; /* mal formed */
812 if (tag_end != url_end)
814 goto error; /* mal formed */
817 if (0 == strcmp (MHD_HTTP_METHOD_PUT, method))
818 direction = _RECEIVE;
819 else if (0 == strcmp (MHD_HTTP_METHOD_GET, method))
826 plugin->cur_connections++;
827 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
828 "New %s connection from %s with tag %u (%u of %u)\n",
830 GNUNET_i2s (&target), tag,
831 plugin->cur_connections, plugin->max_connections);
833 /* find duplicate session */
838 (0 == memcmp (&t->target, &target, sizeof (struct GNUNET_PeerIdentity)))
840 /* FIXME add source address comparison */
847 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
848 "Duplicate session, dismissing new connection from peer `%s'\n",
849 GNUNET_i2s (&target));
853 /* find semi-session */
854 t = plugin->server_semi_head;
858 /* FIXME add source address comparison */
859 if ((0 == memcmp (&t->target, &target, sizeof (struct GNUNET_PeerIdentity)))
869 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
870 "Found existing semi-session for `%s'\n",
871 GNUNET_i2s (&target));
873 if ((direction == _SEND) && (t->server_send != NULL))
875 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
876 "Duplicate GET session, dismissing new connection from peer `%s'\n",
877 GNUNET_i2s (&target));
883 GNUNET_CONTAINER_DLL_remove (plugin->server_semi_head,
884 plugin->server_semi_tail, s);
885 GNUNET_CONTAINER_DLL_insert (plugin->head, plugin->tail, s);
886 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
887 "Found matching semi-session, merging session for peer `%s'\n",
888 GNUNET_i2s (&target));
889 GNUNET_assert (NULL != s);
892 if ((direction == _RECEIVE) && (t->server_recv != NULL))
894 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
895 "Duplicate PUT session, dismissing new connection from peer `%s'\n",
896 GNUNET_i2s (&target));
902 GNUNET_CONTAINER_DLL_remove (plugin->server_semi_head,
903 plugin->server_semi_tail, s);
904 GNUNET_CONTAINER_DLL_insert (plugin->head, plugin->tail, s);
905 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
906 "Found matching semi-session, merging session for peer `%s'\n",
907 GNUNET_i2s (&target));
908 GNUNET_assert (NULL != s);
913 /* create new session */
915 switch (conn_info->client_addr->sa_family)
918 addr = http_common_address_from_socket (plugin->protocol, conn_info->client_addr, sizeof (struct sockaddr_in));
919 addr_len = http_common_address_get_size (addr);
920 ats = plugin->env->get_address_type (plugin->env->cls, conn_info->client_addr, sizeof (struct sockaddr_in));
923 addr = http_common_address_from_socket (plugin->protocol, conn_info->client_addr, sizeof (struct sockaddr_in6));
924 addr_len = http_common_address_get_size (addr);
925 ats = plugin->env->get_address_type (plugin->env->cls, conn_info->client_addr, sizeof (struct sockaddr_in6));
932 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
933 "Creating new session for peer `%s' connecting from `%s'\n",
934 GNUNET_i2s (&target),
935 http_common_plugin_address_to_string (NULL, addr, addr_len));
937 s = GNUNET_malloc (sizeof (struct Session));
938 memcpy (&s->target, &target, sizeof (struct GNUNET_PeerIdentity));
941 s->addrlen = addr_len;
942 s->ats_address_network_type = ats.value;
943 s->inbound = GNUNET_YES;
944 s->next_receive = GNUNET_TIME_UNIT_ZERO_ABS;
946 s->server_recv = NULL;
947 s->server_send = NULL;
949 server_start_session_timeout(s);
951 GNUNET_CONTAINER_DLL_insert (plugin->server_semi_head,
952 plugin->server_semi_tail, s);
955 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
956 "Invalid connection request\n");
961 sc = GNUNET_malloc (sizeof (struct ServerConnection));
963 if (conn_info->client_addr->sa_family == AF_INET)
964 sc->mhd_daemon = plugin->server_v4;
965 if (conn_info->client_addr->sa_family == AF_INET6)
966 sc->mhd_daemon = plugin->server_v6;
967 sc->mhd_conn = mhd_connection;
968 sc->direction = direction;
970 if (direction == _SEND)
972 if (direction == _RECEIVE)
975 #if MHD_VERSION >= 0x00090E00
976 int to = (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT.rel_value / 1000);
978 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
979 "Setting timeout for %p to %u sec.\n", sc, to);
980 MHD_set_connection_option (mhd_connection, MHD_CONNECTION_OPTION_TIMEOUT, to);
982 server_reschedule (plugin, sc->mhd_daemon, GNUNET_NO);
988 static struct Session *
989 server_lookup_session (struct HTTP_Server_Plugin *plugin,
990 struct ServerConnection * sc)
994 for (s = plugin->head; NULL != s; s = s->next)
995 if ((s->server_recv == sc) || (s->server_send == sc))
997 for (s = plugin->server_semi_head; NULL != s; s = s->next)
998 if ((s->server_recv == sc) || (s->server_send == sc))
1004 server_exist_session (struct HTTP_Server_Plugin *plugin, struct Session *s)
1006 struct Session * head;
1008 GNUNET_assert (NULL != plugin);
1009 GNUNET_assert (NULL != s);
1011 for (head = plugin->head; head != NULL; head = head->next)
1021 * Callback called by MHD when it needs data to send
1022 * @param cls current session
1023 * @param pos position in buffer
1024 * @param buf the buffer to write data to
1025 * @param max max number of bytes available in buffer
1026 * @return bytes written to buffer
1029 server_send_callback (void *cls, uint64_t pos, char *buf, size_t max)
1031 struct Session *s = cls;
1032 ssize_t bytes_read = 0;
1033 struct HTTP_Message *msg;
1035 GNUNET_assert (NULL != p);
1036 if (GNUNET_NO == server_exist_session (p, s))
1042 bytes_read = GNUNET_MIN (msg->size - msg->pos,
1044 memcpy (buf, &msg->buf[msg->pos], bytes_read);
1045 msg->pos += bytes_read;
1047 /* removing message */
1048 if (msg->pos == msg->size)
1050 GNUNET_CONTAINER_DLL_remove (s->msg_head, s->msg_tail, msg);
1051 if (NULL != msg->transmit_cont)
1052 msg->transmit_cont (msg->transmit_cont_cls, &s->target, GNUNET_OK);
1056 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, s->plugin->name,
1057 "Sent %u bytes\n", s, bytes_read);
1062 * Callback called by MessageStreamTokenizer when a message has arrived
1063 * @param cls current session as closure
1064 * @param client clien
1065 * @param message the message to be forwarded to transport service
1068 server_receive_mst_cb (void *cls, void *client,
1069 const struct GNUNET_MessageHeader *message)
1071 struct Session *s = cls;
1072 struct GNUNET_ATS_Information atsi[2];
1073 struct GNUNET_TIME_Relative delay;
1075 GNUNET_assert (NULL != p);
1076 if (GNUNET_NO == server_exist_session(p, s))
1079 struct HTTP_Server_Plugin *plugin = s->plugin;
1081 atsi[0].type = htonl (GNUNET_ATS_QUALITY_NET_DISTANCE);
1082 atsi[0].value = htonl (1);
1083 atsi[1].type = htonl (GNUNET_ATS_NETWORK_TYPE);
1084 atsi[1].value = s->ats_address_network_type;
1085 GNUNET_break (s->ats_address_network_type != ntohl (GNUNET_ATS_NET_UNSPECIFIED));
1087 delay = plugin->env->receive (plugin->env->cls,
1090 (const struct GNUNET_ATS_Information *) &atsi, 2,
1091 s, s->addr, s->addrlen);
1094 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (), delay);
1095 if (delay.rel_value > 0)
1097 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1098 "Server: peer `%s' address `%s' next read delayed for %llu ms\n",
1099 GNUNET_i2s (&s->target),
1100 http_common_plugin_address_to_string (NULL, s->addr, s->addrlen),
1103 server_reschedule_session_timeout (s);
1109 server_access_cb (void *cls, struct MHD_Connection *mhd_connection,
1110 const char *url, const char *method, const char *version,
1111 const char *upload_data, size_t * upload_data_size,
1112 void **httpSessionCache)
1114 struct HTTP_Server_Plugin *plugin = cls;
1117 struct ServerConnection *sc = *httpSessionCache;
1119 struct MHD_Response *response;
1121 GNUNET_assert (cls != NULL);
1124 /* new connection */
1125 sc = server_lookup_connection (plugin, mhd_connection, url, method);
1127 (*httpSessionCache) = sc;
1130 response = MHD_create_response_from_data (strlen (HTTP_ERROR_RESPONSE), HTTP_ERROR_RESPONSE, MHD_NO, MHD_NO);
1131 res = MHD_queue_response (mhd_connection, MHD_HTTP_NOT_FOUND, response);
1132 MHD_destroy_response (response);
1138 /* 'old' connection */
1139 if (NULL == server_lookup_session (plugin, sc))
1141 /* Session was already disconnected */
1146 /* existing connection */
1147 sc = (*httpSessionCache);
1150 GNUNET_assert (NULL != s);
1151 /* connection is to be disconnected */
1152 if (sc->disconnect == GNUNET_YES)
1154 /* Sent HTTP/1.1: 200 OK as PUT Response\ */
1156 MHD_create_response_from_data (strlen ("Thank you!"), "Thank you!",
1158 res = MHD_queue_response (mhd_connection, MHD_HTTP_OK, response);
1159 MHD_destroy_response (response);
1163 GNUNET_assert (s != NULL);
1164 /* Check if both directions are connected */
1165 if ((sc->session->server_recv == NULL) || (sc->session->server_send == NULL))
1167 /* Delayed read from since not both semi-connections are connected */
1171 if (sc->direction == _SEND)
1173 response = MHD_create_response_from_callback (MHD_SIZE_UNKNOWN,
1175 &server_send_callback, s,
1177 MHD_queue_response (mhd_connection, MHD_HTTP_OK, response);
1178 MHD_destroy_response (response);
1181 if (sc->direction == _RECEIVE)
1183 if (*upload_data_size == 0)
1185 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1186 "Peer `%s' PUT on address `%s' connected\n",
1187 GNUNET_i2s (&s->target),
1188 http_common_plugin_address_to_string (NULL,
1194 /* Receiving data */
1195 if ((*upload_data_size > 0))
1197 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1198 "Peer `%s' PUT on address `%s' received %u bytes\n",
1199 GNUNET_i2s (&s->target),
1200 http_common_plugin_address_to_string (NULL,
1204 struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get ();
1206 if ((s->next_receive.abs_value <= now.abs_value))
1208 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1209 "PUT with %u bytes forwarded to MST\n", s,
1211 if (s->msg_tk == NULL)
1213 s->msg_tk = GNUNET_SERVER_mst_create (&server_receive_mst_cb, s);
1215 GNUNET_SERVER_mst_receive (s->msg_tk, s, upload_data,
1216 *upload_data_size, GNUNET_NO, GNUNET_NO);
1218 #if MHD_VERSION >= 0x00090E00
1219 int to = (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT.rel_value / 1000);
1220 struct ServerConnection *t = NULL;
1222 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1223 "Received %u bytes\n", *upload_data_size);
1224 /* Setting timeouts for other connections */
1225 if (s->server_recv != NULL)
1228 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1229 "Setting timeout for %p to %u sec.\n", t,
1231 MHD_set_connection_option (t->mhd_conn,
1232 MHD_CONNECTION_OPTION_TIMEOUT,
1234 server_reschedule (plugin, t->mhd_daemon, GNUNET_NO);
1236 if (s->server_send != NULL)
1239 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1240 "Setting timeout for %p to %u sec.\n", t,
1242 MHD_set_connection_option (t->mhd_conn,
1243 MHD_CONNECTION_OPTION_TIMEOUT,
1245 server_reschedule (plugin, t->mhd_daemon, GNUNET_NO);
1248 (*upload_data_size) = 0;
1252 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1253 "Server: %p no inbound bandwidth available! Next read was delayed by %llu ms\n",
1254 s, now.abs_value - s->next_receive.abs_value);
1265 server_disconnect_cb (void *cls, struct MHD_Connection *connection,
1266 void **httpSessionCache)
1273 * Check if incoming connection is accepted.
1274 * NOTE: Here every connection is accepted
1275 * @param cls plugin as closure
1276 * @param addr address of incoming connection
1277 * @param addr_len address length of incoming connection
1278 * @return MHD_YES if connection is accepted, MHD_NO if connection is rejected
1282 server_accept_cb (void *cls, const struct sockaddr *addr, socklen_t addr_len)
1284 struct HTTP_Server_Plugin *plugin = cls;
1286 if (plugin->cur_connections <= plugin->max_connections)
1290 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, plugin->name,
1291 _("Server reached maximum number connections (%u), rejecting new connection\n"),
1292 plugin->max_connections);
1298 server_log (void *arg, const char *fmt, va_list ap)
1302 vsnprintf (text, sizeof (text), fmt, ap);
1304 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Server: %s\n", text);
1309 * Call MHD IPv4 to process pending requests and then go back
1310 * and schedule the next run.
1311 * @param cls plugin as closure
1312 * @param tc task context
1315 server_v4_run (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1317 struct HTTP_Server_Plugin *plugin = cls;
1319 GNUNET_assert (cls != NULL);
1321 plugin->server_v4_task = GNUNET_SCHEDULER_NO_TASK;
1322 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1325 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1326 "Running IPv4 server\n");
1328 plugin->server_v4_immediately = GNUNET_NO;
1329 GNUNET_assert (MHD_YES == MHD_run (plugin->server_v4));
1330 server_reschedule (plugin, plugin->server_v4, GNUNET_NO);
1335 * Call MHD IPv6 to process pending requests and then go back
1336 * and schedule the next run.
1337 * @param cls plugin as closure
1338 * @param tc task context
1341 server_v6_run (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1343 struct HTTP_Server_Plugin *plugin = cls;
1345 GNUNET_assert (cls != NULL);
1346 plugin->server_v6_task = GNUNET_SCHEDULER_NO_TASK;
1347 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1350 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1351 "Running IPv6 server\n");
1353 plugin->server_v6_immediately = GNUNET_NO;
1354 GNUNET_assert (MHD_YES == MHD_run (plugin->server_v6));
1355 server_reschedule (plugin, plugin->server_v6, GNUNET_NO);
1360 * Function that queries MHD's select sets and
1361 * starts the task waiting for them.
1362 * @param plugin plugin
1363 * @param daemon_handle the MHD daemon handle
1364 * @return gnunet task identifier
1366 static GNUNET_SCHEDULER_TaskIdentifier
1367 server_schedule (struct HTTP_Server_Plugin *plugin, struct MHD_Daemon *daemon_handle,
1370 GNUNET_SCHEDULER_TaskIdentifier ret;
1374 struct GNUNET_NETWORK_FDSet *wrs;
1375 struct GNUNET_NETWORK_FDSet *wws;
1376 struct GNUNET_NETWORK_FDSet *wes;
1378 unsigned MHD_LONG_LONG timeout;
1379 static unsigned long long last_timeout = 0;
1382 struct GNUNET_TIME_Relative tv;
1384 ret = GNUNET_SCHEDULER_NO_TASK;
1388 wrs = GNUNET_NETWORK_fdset_create ();
1389 wes = GNUNET_NETWORK_fdset_create ();
1390 wws = GNUNET_NETWORK_fdset_create ();
1392 GNUNET_assert (MHD_YES == MHD_get_fdset (daemon_handle, &rs, &ws, &es, &max));
1393 haveto = MHD_get_timeout (daemon_handle, &timeout);
1394 if (haveto == MHD_YES)
1396 if (timeout != last_timeout)
1399 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1400 "SELECT Timeout changed from %llu to %llu\n",
1401 last_timeout, timeout);
1403 last_timeout = timeout;
1405 tv.rel_value = (uint64_t) timeout;
1408 tv = GNUNET_TIME_UNIT_SECONDS;
1409 /* Force immediate run, since we have outbound data to send */
1410 if (now == GNUNET_YES)
1411 tv = GNUNET_TIME_UNIT_MILLISECONDS;
1412 GNUNET_NETWORK_fdset_copy_native (wrs, &rs, max + 1);
1413 GNUNET_NETWORK_fdset_copy_native (wws, &ws, max + 1);
1414 GNUNET_NETWORK_fdset_copy_native (wes, &es, max + 1);
1416 if (daemon_handle == plugin->server_v4)
1418 if (plugin->server_v4_task != GNUNET_SCHEDULER_NO_TASK)
1420 GNUNET_SCHEDULER_cancel (plugin->server_v4_task);
1421 plugin->server_v4_task = GNUNET_SCHEDULER_NO_TASK;
1424 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1425 "Scheduling IPv4 server task in %llu ms\n", tv);
1428 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1430 &server_v4_run, plugin);
1432 if (daemon_handle == plugin->server_v6)
1434 if (plugin->server_v6_task != GNUNET_SCHEDULER_NO_TASK)
1436 GNUNET_SCHEDULER_cancel (plugin->server_v6_task);
1437 plugin->server_v6_task = GNUNET_SCHEDULER_NO_TASK;
1440 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1441 "Scheduling IPv6 server task in %llu ms\n", tv);
1444 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1446 &server_v6_run, plugin);
1448 GNUNET_NETWORK_fdset_destroy (wrs);
1449 GNUNET_NETWORK_fdset_destroy (wws);
1450 GNUNET_NETWORK_fdset_destroy (wes);
1457 server_load_file (const char *file)
1459 struct GNUNET_DISK_FileHandle *gn_file;
1463 if (GNUNET_OK != GNUNET_DISK_file_size (file,
1464 &fsize, GNUNET_NO, GNUNET_YES))
1466 text = GNUNET_malloc (fsize + 1);
1468 GNUNET_DISK_file_open (file, GNUNET_DISK_OPEN_READ,
1469 GNUNET_DISK_PERM_USER_READ);
1470 if (gn_file == NULL)
1475 if (GNUNET_SYSERR == GNUNET_DISK_file_read (gn_file, text, fsize))
1478 GNUNET_DISK_file_close (gn_file);
1482 GNUNET_DISK_file_close (gn_file);
1491 server_load_certificate (struct HTTP_Server_Plugin *plugin)
1493 int res = GNUNET_OK;
1498 /* Get crypto init string from config
1499 * If not present just use default values */
1502 GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg,
1505 &plugin->crypto_init))
1506 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1507 "Using crypto init string `%s'\n",
1508 plugin->crypto_init);
1510 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1511 "Using default crypto init string \n");
1514 GNUNET_CONFIGURATION_get_value_filename (plugin->env->cfg, plugin->name,
1515 "KEY_FILE", &key_file))
1517 key_file = GNUNET_strdup ("https_key.key");
1521 GNUNET_CONFIGURATION_get_value_filename (plugin->env->cfg, plugin->name,
1522 "CERT_FILE", &cert_file))
1524 GNUNET_asprintf (&cert_file, "%s", "https_cert.crt");
1527 /* read key & certificates from file */
1528 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1529 "Trying to loading TLS certificate from key-file `%s' cert-file`%s'\n",
1530 key_file, cert_file);
1532 plugin->key = server_load_file (key_file);
1533 plugin->cert = server_load_file (cert_file);
1535 if ((plugin->key == NULL) || (plugin->cert == NULL))
1537 struct GNUNET_OS_Process *cert_creation;
1539 GNUNET_free_non_null (plugin->key);
1541 GNUNET_free_non_null (plugin->cert);
1542 plugin->cert = NULL;
1544 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1545 "No usable TLS certificate found, creating certificate\n");
1548 GNUNET_OS_start_process (GNUNET_NO, GNUNET_OS_INHERIT_STD_OUT_AND_ERR, NULL, NULL,
1549 "gnunet-transport-certificate-creation",
1550 "gnunet-transport-certificate-creation",
1551 key_file, cert_file, NULL);
1552 if (cert_creation == NULL)
1554 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
1556 ("Could not create a new TLS certificate, program `gnunet-transport-certificate-creation' could not be started!\n"));
1557 GNUNET_free (key_file);
1558 GNUNET_free (cert_file);
1560 GNUNET_free_non_null (plugin->key);
1562 GNUNET_free_non_null (plugin->cert);
1563 plugin->cert = NULL;
1564 GNUNET_free_non_null (plugin->crypto_init);
1565 plugin->crypto_init = NULL;
1567 return GNUNET_SYSERR;
1569 GNUNET_assert (GNUNET_OK == GNUNET_OS_process_wait (cert_creation));
1570 GNUNET_OS_process_destroy (cert_creation);
1572 plugin->key = server_load_file (key_file);
1573 plugin->cert = server_load_file (cert_file);
1576 if ((plugin->key == NULL) || (plugin->cert == NULL))
1578 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
1580 ("No usable TLS certificate found and creating one failed!\n"),
1582 GNUNET_free (key_file);
1583 GNUNET_free (cert_file);
1585 GNUNET_free_non_null (plugin->key);
1587 GNUNET_free_non_null (plugin->cert);
1588 plugin->cert = NULL;
1589 GNUNET_free_non_null (plugin->crypto_init);
1590 plugin->crypto_init = NULL;
1592 return GNUNET_SYSERR;
1594 GNUNET_free (key_file);
1595 GNUNET_free (cert_file);
1596 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "TLS certificate loaded\n");
1602 server_start (struct HTTP_Server_Plugin *plugin)
1604 unsigned int timeout;
1605 GNUNET_assert (NULL != plugin);
1608 if (GNUNET_SYSERR == server_load_certificate (plugin))
1610 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
1611 "Could not load or create server certificate! Loading plugin failed!\n");
1612 return GNUNET_SYSERR;
1617 #if MHD_VERSION >= 0x00090E00
1618 timeout = HTTP_NOT_VALIDATED_TIMEOUT.rel_value / 1000;
1619 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1620 "MHD can set timeout per connection! Default time out %u sec.\n",
1623 timeout = GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT.rel_value / 1000;
1624 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, plugin->name,
1625 "MHD cannot set timeout per connection! Default time out %u sec.\n",
1628 plugin->server_v4 = NULL;
1629 if (plugin->use_ipv4 == GNUNET_YES)
1631 plugin->server_v4 = MHD_start_daemon (
1638 MHD_NO_FLAG, plugin->port,
1639 &server_accept_cb, plugin,
1640 &server_access_cb, plugin,
1641 MHD_OPTION_SOCK_ADDR,
1642 (struct sockaddr_in *)
1643 plugin->server_addr_v4,
1644 MHD_OPTION_CONNECTION_LIMIT,
1646 plugin->max_connections,
1648 MHD_OPTION_HTTPS_PRIORITIES,
1649 plugin->crypto_init,
1650 MHD_OPTION_HTTPS_MEM_KEY,
1652 MHD_OPTION_HTTPS_MEM_CERT,
1655 MHD_OPTION_CONNECTION_TIMEOUT,
1657 MHD_OPTION_CONNECTION_MEMORY_LIMIT,
1659 GNUNET_SERVER_MAX_MESSAGE_SIZE),
1660 MHD_OPTION_NOTIFY_COMPLETED,
1661 &server_disconnect_cb, plugin,
1662 MHD_OPTION_EXTERNAL_LOGGER,
1663 server_log, NULL, MHD_OPTION_END);
1665 plugin->server_v6 = NULL;
1666 if (plugin->use_ipv6 == GNUNET_YES)
1668 plugin->server_v6 = MHD_start_daemon (
1675 MHD_USE_IPv6, plugin->port,
1676 &server_accept_cb, plugin,
1677 &server_access_cb, plugin,
1678 MHD_OPTION_SOCK_ADDR,
1679 (struct sockaddr_in6 *)
1680 plugin->server_addr_v6,
1681 MHD_OPTION_CONNECTION_LIMIT,
1683 plugin->max_connections,
1685 MHD_OPTION_HTTPS_PRIORITIES,
1686 plugin->crypto_init,
1687 MHD_OPTION_HTTPS_MEM_KEY,
1689 MHD_OPTION_HTTPS_MEM_CERT,
1692 MHD_OPTION_CONNECTION_TIMEOUT,
1694 MHD_OPTION_CONNECTION_MEMORY_LIMIT,
1696 GNUNET_SERVER_MAX_MESSAGE_SIZE),
1697 MHD_OPTION_NOTIFY_COMPLETED,
1698 &server_disconnect_cb, plugin,
1699 MHD_OPTION_EXTERNAL_LOGGER,
1700 server_log, NULL, MHD_OPTION_END);
1704 if ((plugin->use_ipv4 == GNUNET_YES) && (plugin->server_v4 == NULL))
1706 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
1707 "Failed to start %s IPv4 server component on port %u\n",
1708 plugin->name, plugin->port);
1709 return GNUNET_SYSERR;
1711 server_reschedule (plugin, plugin->server_v4, GNUNET_NO);
1713 if ((plugin->use_ipv6 == GNUNET_YES) && (plugin->server_v6 == NULL))
1715 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
1716 "Failed to start %s IPv6 server component on port %u\n",
1717 plugin->name, plugin->port);
1718 return GNUNET_SYSERR;
1720 server_reschedule (plugin, plugin->server_v6, GNUNET_NO);
1721 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1722 "%s server component started on port %u\n", plugin->name,
1729 server_stop (struct HTTP_Server_Plugin *plugin)
1731 struct Session *s = NULL;
1732 struct Session *t = NULL;
1734 struct MHD_Daemon *server_v4_tmp = plugin->server_v4;
1735 plugin->server_v4 = NULL;
1737 struct MHD_Daemon *server_v6_tmp = plugin->server_v6;
1738 plugin->server_v6 = NULL;
1740 if (plugin->server_v4_task != GNUNET_SCHEDULER_NO_TASK)
1742 GNUNET_SCHEDULER_cancel (plugin->server_v4_task);
1743 plugin->server_v4_task = GNUNET_SCHEDULER_NO_TASK;
1746 if (plugin->server_v6_task != GNUNET_SCHEDULER_NO_TASK)
1748 GNUNET_SCHEDULER_cancel (plugin->server_v6_task);
1749 plugin->server_v6_task = GNUNET_SCHEDULER_NO_TASK;
1752 if (server_v6_tmp != NULL)
1754 MHD_stop_daemon (server_v4_tmp);
1756 if (server_v6_tmp != NULL)
1758 MHD_stop_daemon (server_v6_tmp);
1761 /* cleaning up semi-sessions never propagated */
1762 s = plugin->server_semi_head;
1766 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1767 "Deleting semi-sessions %p\n", s);
1770 struct HTTP_Message *msg = s->msg_head;
1771 struct HTTP_Message *tmp = NULL;
1777 GNUNET_CONTAINER_DLL_remove (s->msg_head, s->msg_tail, msg);
1778 if (msg->transmit_cont != NULL)
1780 msg->transmit_cont (msg->transmit_cont_cls, &s->target, GNUNET_SYSERR);
1786 server_delete_session (s);
1793 GNUNET_free_non_null (plugin->crypto_init);
1794 GNUNET_free_non_null (plugin->cert);
1795 GNUNET_free_non_null (plugin->key);
1798 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1799 "%s server component stopped\n", plugin->name);
1803 server_add_address (void *cls, int add_remove, const struct sockaddr *addr,
1806 struct HTTP_Server_Plugin *plugin = cls;
1807 struct HttpAddressWrapper *w = NULL;
1809 if ((AF_INET == addr->sa_family) && (GNUNET_NO == plugin->use_ipv4))
1812 if ((AF_INET6 == addr->sa_family) && (GNUNET_NO == plugin->use_ipv6))
1815 w = GNUNET_malloc (sizeof (struct HttpAddressWrapper));
1816 w->addr = http_common_address_from_socket (plugin->protocol, addr, addrlen);
1817 if (NULL == w->addr)
1822 w->addrlen = http_common_address_get_size (w->addr);
1824 GNUNET_CONTAINER_DLL_insert(plugin->addr_head, plugin->addr_tail, w);
1825 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1826 "Notifying transport to add address `%s'\n",
1827 http_common_plugin_address_to_string(NULL, w->addr, w->addrlen));
1829 plugin->env->notify_address (plugin->env->cls, add_remove, w->addr, w->addrlen);
1834 server_remove_address (void *cls, int add_remove, const struct sockaddr *addr,
1837 struct HTTP_Server_Plugin *plugin = cls;
1838 struct HttpAddressWrapper *w = plugin->addr_head;
1840 void * saddr = http_common_address_from_socket (plugin->protocol, addr, addrlen);
1843 saddr_len = http_common_address_get_size (saddr);
1847 if (GNUNET_YES == http_common_cmp_addresses(w->addr, w->addrlen, saddr, saddr_len))
1851 GNUNET_free (saddr);
1856 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1857 "Notifying transport to remove address `%s'\n",
1858 http_common_plugin_address_to_string (NULL, w->addr, w->addrlen));
1859 GNUNET_CONTAINER_DLL_remove (plugin->addr_head, plugin->addr_tail, w);
1860 plugin->env->notify_address (plugin->env->cls, add_remove, w->addr, w->addrlen);
1861 GNUNET_free (w->addr);
1868 * Our external IP address/port mapping has changed.
1870 * @param cls closure, the 'struct LocalAddrList'
1871 * @param add_remove GNUNET_YES to mean the new public IP address, GNUNET_NO to mean
1872 * the previous (now invalid) one
1873 * @param addr either the previous or the new public IP address
1874 * @param addrlen actual lenght of the address
1877 server_nat_port_map_callback (void *cls, int add_remove, const struct sockaddr *addr,
1880 GNUNET_assert (cls != NULL);
1881 struct HTTP_Server_Plugin *plugin = cls;
1883 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1884 "NPMC called %s to address `%s'\n",
1885 (add_remove == GNUNET_NO) ? "remove" : "add",
1886 GNUNET_a2s (addr, addrlen));
1891 server_add_address (cls, add_remove, addr, addrlen);
1894 server_remove_address (cls, add_remove, addr, addrlen);
1901 server_get_addresses (struct HTTP_Server_Plugin *plugin,
1902 const char *serviceName,
1903 const struct GNUNET_CONFIGURATION_Handle *cfg,
1904 struct sockaddr ***addrs, socklen_t ** addr_lens)
1907 unsigned long long port;
1908 struct addrinfo hints;
1909 struct addrinfo *res;
1910 struct addrinfo *pos;
1911 struct addrinfo *next;
1915 struct sockaddr **saddrs;
1916 socklen_t *saddrlens;
1922 disablev6 = !plugin->use_ipv6;
1925 if (GNUNET_CONFIGURATION_have_value (cfg, serviceName, "PORT"))
1927 GNUNET_break (GNUNET_OK ==
1928 GNUNET_CONFIGURATION_get_value_number (cfg, serviceName,
1932 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1934 ("Require valid port number for service in configuration!\n"));
1935 return GNUNET_SYSERR;
1940 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, plugin->name,
1941 "Starting in listen only mode\n");
1942 return -1; /* listen only */
1946 if (GNUNET_CONFIGURATION_have_value (cfg, serviceName, "BINDTO"))
1948 GNUNET_break (GNUNET_OK ==
1949 GNUNET_CONFIGURATION_get_value_string (cfg, serviceName,
1950 "BINDTO", &hostname));
1955 if (hostname != NULL)
1957 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1958 "Resolving `%s' since that is where `%s' will bind to.\n",
1959 hostname, serviceName);
1960 memset (&hints, 0, sizeof (struct addrinfo));
1962 hints.ai_family = AF_INET;
1963 if ((0 != (ret = getaddrinfo (hostname, NULL, &hints, &res))) ||
1966 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Failed to resolve `%s': %s\n"),
1967 hostname, gai_strerror (ret));
1968 GNUNET_free (hostname);
1969 return GNUNET_SYSERR;
1973 while (NULL != (pos = next))
1975 next = pos->ai_next;
1976 if ((disablev6) && (pos->ai_family == AF_INET6))
1982 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1983 _("Failed to find %saddress for `%s'.\n"),
1984 disablev6 ? "IPv4 " : "", hostname);
1986 GNUNET_free (hostname);
1987 return GNUNET_SYSERR;
1990 saddrs = GNUNET_malloc ((resi + 1) * sizeof (struct sockaddr *));
1991 saddrlens = GNUNET_malloc ((resi + 1) * sizeof (socklen_t));
1994 while (NULL != (pos = next))
1996 next = pos->ai_next;
1997 if ((disablev6) && (pos->ai_family == AF_INET6))
1999 if ((pos->ai_protocol != IPPROTO_TCP) && (pos->ai_protocol != 0))
2000 continue; /* not TCP */
2001 if ((pos->ai_socktype != SOCK_STREAM) && (pos->ai_socktype != 0))
2002 continue; /* huh? */
2003 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2004 "Service will bind to `%s'\n", GNUNET_a2s (pos->ai_addr,
2006 if (pos->ai_family == AF_INET)
2008 GNUNET_assert (pos->ai_addrlen == sizeof (struct sockaddr_in));
2009 saddrlens[i] = pos->ai_addrlen;
2010 saddrs[i] = GNUNET_malloc (saddrlens[i]);
2011 memcpy (saddrs[i], pos->ai_addr, saddrlens[i]);
2012 ((struct sockaddr_in *) saddrs[i])->sin_port = htons (port);
2016 GNUNET_assert (pos->ai_family == AF_INET6);
2017 GNUNET_assert (pos->ai_addrlen == sizeof (struct sockaddr_in6));
2018 saddrlens[i] = pos->ai_addrlen;
2019 saddrs[i] = GNUNET_malloc (saddrlens[i]);
2020 memcpy (saddrs[i], pos->ai_addr, saddrlens[i]);
2021 ((struct sockaddr_in6 *) saddrs[i])->sin6_port = htons (port);
2025 GNUNET_free (hostname);
2031 /* will bind against everything, just set port */
2037 saddrs = GNUNET_malloc ((resi + 1) * sizeof (struct sockaddr *));
2038 saddrlens = GNUNET_malloc ((resi + 1) * sizeof (socklen_t));
2040 saddrlens[i] = sizeof (struct sockaddr_in);
2041 saddrs[i] = GNUNET_malloc (saddrlens[i]);
2042 #if HAVE_SOCKADDR_IN_SIN_LEN
2043 ((struct sockaddr_in *) saddrs[i])->sin_len = saddrlens[i];
2045 ((struct sockaddr_in *) saddrs[i])->sin_family = AF_INET;
2046 ((struct sockaddr_in *) saddrs[i])->sin_port = htons (port);
2052 saddrs = GNUNET_malloc ((resi + 1) * sizeof (struct sockaddr *));
2053 saddrlens = GNUNET_malloc ((resi + 1) * sizeof (socklen_t));
2055 saddrlens[i] = sizeof (struct sockaddr_in6);
2056 saddrs[i] = GNUNET_malloc (saddrlens[i]);
2057 #if HAVE_SOCKADDR_IN_SIN_LEN
2058 ((struct sockaddr_in6 *) saddrs[i])->sin6_len = saddrlens[0];
2060 ((struct sockaddr_in6 *) saddrs[i])->sin6_family = AF_INET6;
2061 ((struct sockaddr_in6 *) saddrs[i])->sin6_port = htons (port);
2063 saddrlens[i] = sizeof (struct sockaddr_in);
2064 saddrs[i] = GNUNET_malloc (saddrlens[i]);
2065 #if HAVE_SOCKADDR_IN_SIN_LEN
2066 ((struct sockaddr_in *) saddrs[i])->sin_len = saddrlens[1];
2068 ((struct sockaddr_in *) saddrs[i])->sin_family = AF_INET;
2069 ((struct sockaddr_in *) saddrs[i])->sin_port = htons (port);
2073 *addr_lens = saddrlens;
2078 server_start_report_addresses (struct HTTP_Server_Plugin *plugin)
2080 int res = GNUNET_OK;
2081 struct sockaddr **addrs;
2082 socklen_t *addrlens;
2084 res = server_get_addresses (plugin,
2085 plugin->name, plugin->env->cfg,
2087 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2088 _("Found %u addresses to report to NAT service\n"), res);
2090 if (GNUNET_SYSERR == res)
2097 GNUNET_NAT_register (plugin->env->cfg, GNUNET_YES, plugin->port,
2099 (const struct sockaddr **) addrs, addrlens,
2100 &server_nat_port_map_callback, NULL, plugin);
2104 GNUNET_assert (addrs[res] != NULL);
2105 GNUNET_free (addrs[res]);
2107 GNUNET_free_non_null (addrs);
2108 GNUNET_free_non_null (addrlens);
2113 server_stop_report_addresses (struct HTTP_Server_Plugin *plugin)
2115 /* Stop NAT handle */
2116 if (NULL != plugin->nat)
2117 GNUNET_NAT_unregister (plugin->nat);
2119 /* Clean up addresses */
2120 struct HttpAddressWrapper *w;
2122 while (plugin->addr_head != NULL)
2124 w = plugin->addr_head;
2125 GNUNET_CONTAINER_DLL_remove (plugin->addr_head, plugin->addr_tail, w);
2126 GNUNET_free (w->addr);
2133 * Check if IPv6 supported on this system
2136 server_check_ipv6_support (struct HTTP_Server_Plugin *plugin)
2138 struct GNUNET_NETWORK_Handle *desc = NULL;
2139 int res = GNUNET_NO;
2141 /* Probe IPv6 support */
2142 desc = GNUNET_NETWORK_socket_create (PF_INET6, SOCK_STREAM, 0);
2145 if ((errno == ENOBUFS) || (errno == ENOMEM) || (errno == ENFILE) ||
2148 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "socket");
2150 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, plugin->name,
2152 ("Disabling IPv6 since it is not supported on this system!\n"));
2157 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (desc));
2161 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2162 "Testing IPv6 on this system: %s\n",
2163 (res == GNUNET_YES) ? "successful" : "failed");
2169 * Function called when the service shuts down. Unloads our plugins
2170 * and cancels pending validations.
2172 * @param cls closure, unused
2173 * @param tc task context (unused)
2176 server_notify_external_hostname (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2178 struct HTTP_Server_Plugin *plugin = cls;
2180 plugin->notify_ext_task = GNUNET_SCHEDULER_NO_TASK;
2182 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
2185 GNUNET_asprintf(&plugin->ext_addr, "%s://%s", plugin->protocol, plugin->external_hostname);
2186 plugin->ext_addr_len = strlen (plugin->ext_addr) + 1;
2187 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2188 "Notifying transport about external hostname address `%s'\n", plugin->ext_addr);
2189 plugin->env->notify_address (plugin->env->cls, GNUNET_YES, plugin->ext_addr, plugin->ext_addr_len );
2194 server_configure_plugin (struct HTTP_Server_Plugin *plugin)
2196 unsigned long long port;
2197 unsigned long long max_connections;
2198 char *bind4_address = NULL;
2199 char *bind6_address = NULL;
2202 if (GNUNET_CONFIGURATION_have_value
2203 (plugin->env->cfg, plugin->name, "USE_IPv4"))
2206 GNUNET_CONFIGURATION_get_value_yesno (plugin->env->cfg, plugin->name,
2210 plugin->use_ipv4 = GNUNET_YES;
2211 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2212 _("IPv4 support is %s\n"),
2213 (plugin->use_ipv4 == GNUNET_YES) ? "enabled" : "disabled");
2216 if (GNUNET_CONFIGURATION_have_value
2217 (plugin->env->cfg, plugin->name, "USE_IPv6"))
2220 GNUNET_CONFIGURATION_get_value_yesno (plugin->env->cfg, plugin->name,
2224 plugin->use_ipv6 = GNUNET_YES;
2225 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2226 _("IPv6 support is %s\n"),
2227 (plugin->use_ipv6 == GNUNET_YES) ? "enabled" : "disabled");
2229 if ((plugin->use_ipv4 == GNUNET_NO) && (plugin->use_ipv6 == GNUNET_NO))
2231 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
2233 ("Neither IPv4 nor IPv6 are enabled! Fix in configuration\n"),
2235 return GNUNET_SYSERR;
2238 /* Reading port number from config file */
2240 GNUNET_CONFIGURATION_get_value_number (plugin->env->cfg, plugin->name,
2241 "PORT", &port)) || (port > 65535))
2243 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
2244 _("Port is required! Fix in configuration\n"),
2246 return GNUNET_SYSERR;
2248 plugin->port = port;
2250 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2251 _("Using port %u\n"), plugin->port);
2253 if ((plugin->use_ipv4 == GNUNET_YES) &&
2254 (GNUNET_YES == GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg,
2255 plugin->name, "BINDTO", &bind4_address)))
2257 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2258 "Binding %s plugin to specific IPv4 address: `%s'\n",
2259 plugin->protocol, bind4_address);
2260 plugin->server_addr_v4 = GNUNET_malloc (sizeof (struct sockaddr_in));
2261 if (1 != inet_pton (AF_INET, bind4_address,
2262 &plugin->server_addr_v4->sin_addr))
2264 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
2266 ("Specific IPv4 address `%s' in configuration file is invalid!\n"),
2268 GNUNET_free (bind4_address);
2269 GNUNET_free (plugin->server_addr_v4);
2270 plugin->server_addr_v4 = NULL;
2271 return GNUNET_SYSERR;
2275 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2276 _("Binding to IPv4 address %s\n"), bind4_address);
2277 plugin->server_addr_v4->sin_family = AF_INET;
2278 plugin->server_addr_v4->sin_port = htons (plugin->port);
2280 GNUNET_free (bind4_address);
2283 if ((plugin->use_ipv6 == GNUNET_YES) &&
2285 GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg, plugin->name,
2286 "BINDTO6", &bind6_address)))
2288 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2289 "Binding %s plugin to specific IPv6 address: `%s'\n",
2290 plugin->protocol, bind6_address);
2291 plugin->server_addr_v6 = GNUNET_malloc (sizeof (struct sockaddr_in6));
2293 inet_pton (AF_INET6, bind6_address, &plugin->server_addr_v6->sin6_addr))
2295 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
2297 ("Specific IPv6 address `%s' in configuration file is invalid!\n"),
2299 GNUNET_free (bind6_address);
2300 GNUNET_free (plugin->server_addr_v6);
2301 plugin->server_addr_v6 = NULL;
2302 return GNUNET_SYSERR;
2306 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2307 _("Binding to IPv6 address %s\n"), bind6_address);
2308 plugin->server_addr_v6->sin6_family = AF_INET6;
2309 plugin->server_addr_v6->sin6_port = htons (plugin->port);
2311 GNUNET_free (bind6_address);
2314 if (GNUNET_YES == GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg, plugin->name,
2315 "EXTERNAL_HOSTNAME", &plugin->external_hostname))
2317 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2318 _("Using external hostname `%s'\n"), plugin->external_hostname);
2319 plugin->notify_ext_task = GNUNET_SCHEDULER_add_now (&server_notify_external_hostname, plugin);
2322 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2323 "No external hostname configured\n");
2326 /* Optional parameters */
2327 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (plugin->env->cfg,
2329 "MAX_CONNECTIONS", &max_connections))
2330 max_connections = 128;
2331 plugin->max_connections = max_connections;
2333 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2334 _("Maximum number of connections is %u\n"),
2335 plugin->max_connections);
2340 * Session was idle, so disconnect it
2344 server_session_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2346 GNUNET_assert (NULL != cls);
2347 struct Session *s = cls;
2349 s->timeout_task = GNUNET_SCHEDULER_NO_TASK;
2350 GNUNET_log (TIMEOUT_LOG,
2351 "Session %p was idle for %llu ms, disconnecting\n",
2352 s, (unsigned long long) TIMEOUT.rel_value);
2354 /* call session destroy function */
2355 GNUNET_assert (GNUNET_OK == server_disconnect (s));
2359 * Start session timeout
2362 server_start_session_timeout (struct Session *s)
2364 GNUNET_assert (NULL != s);
2365 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == s->timeout_task);
2366 s->timeout_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
2367 &server_session_timeout,
2369 GNUNET_log (TIMEOUT_LOG,
2370 "Timeout for session %p set to %llu ms\n",
2371 s, (unsigned long long) TIMEOUT.rel_value);
2376 * Increment session timeout due to activity
2379 server_reschedule_session_timeout (struct Session *s)
2381 GNUNET_assert (NULL != s);
2382 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK != s->timeout_task);
2384 GNUNET_SCHEDULER_cancel (s->timeout_task);
2385 s->timeout_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
2386 &server_session_timeout,
2388 GNUNET_log (TIMEOUT_LOG,
2389 "Timeout rescheduled for session %p set to %llu ms\n",
2390 s, (unsigned long long) TIMEOUT.rel_value);
2394 * Exit point from the plugin.
2397 LIBGNUNET_PLUGIN_TRANSPORT_DONE (void *cls)
2399 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
2400 struct HTTP_Server_Plugin *plugin = api->cls;
2402 if (GNUNET_SCHEDULER_NO_TASK != plugin->notify_ext_task)
2404 GNUNET_SCHEDULER_cancel (plugin->notify_ext_task);
2405 plugin->notify_ext_task = GNUNET_SCHEDULER_NO_TASK;
2408 if (NULL != plugin->ext_addr)
2410 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2411 "Notifying transport to remove address `%s'\n",
2412 http_common_plugin_address_to_string (NULL,
2414 plugin->ext_addr_len));
2415 plugin->env->notify_address (plugin->env->cls,
2418 plugin->ext_addr_len);
2421 /* Stop to report addresses to transport service */
2422 server_stop_report_addresses (plugin);
2424 server_stop (plugin);
2427 GNUNET_free_non_null (plugin->external_hostname);
2428 GNUNET_free_non_null (plugin->ext_addr);
2429 GNUNET_free_non_null (plugin->server_addr_v4);
2430 GNUNET_free_non_null (plugin->server_addr_v6);
2432 GNUNET_free (plugin);
2439 * Entry point for the plugin.
2442 LIBGNUNET_PLUGIN_TRANSPORT_INIT (void *cls)
2444 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
2445 struct GNUNET_TRANSPORT_PluginFunctions *api;
2446 struct HTTP_Server_Plugin *plugin;
2448 plugin = GNUNET_malloc (sizeof (struct HTTP_Server_Plugin));
2451 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
2453 api->send = &http_server_plugin_send;
2454 api->disconnect = &http_server_plugin_disconnect;
2455 api->check_address = &http_server_plugin_address_suggested;
2456 api->get_session = &http_server_plugin_get_session;
2458 api->address_to_string = &http_common_plugin_address_to_string;
2459 api->string_to_address = &http_common_plugin_string_to_address;
2460 api->address_pretty_printer = &http_common_plugin_address_pretty_printer;
2463 plugin->name = "transport-https_server";
2464 plugin->protocol = "https";
2466 plugin->name = "transport-http_server";
2467 plugin->protocol = "http";
2470 /* Configure plugin */
2471 if (GNUNET_SYSERR == server_configure_plugin (plugin))
2473 LIBGNUNET_PLUGIN_TRANSPORT_DONE (api);
2477 /* Check IPv6 support */
2478 if (GNUNET_YES == plugin->use_ipv6)
2479 plugin->use_ipv6 = server_check_ipv6_support (plugin);
2481 /* Report addresses to transport service */
2482 server_start_report_addresses (plugin);
2484 if (GNUNET_SYSERR == server_start (plugin))
2486 LIBGNUNET_PLUGIN_TRANSPORT_DONE (api);
2496 /* end of plugin_transport_http_server.c */