2 This file is part of GNUnet
3 (C) 2002-2013 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file transport/plugin_transport_http_server.c
23 * @brief HTTP/S server transport plugin
24 * @author Matthias Wachs
28 #include "gnunet_common.h"
29 #include "gnunet_server_lib.h"
30 #include "gnunet_statistics_service.h"
31 #include "gnunet_transport_plugin.h"
32 #include "gnunet_nat_lib.h"
33 #include "plugin_transport_http_common.h"
34 #include <microhttpd.h>
39 #define PLUGIN_NAME "https_server"
40 #define LIBGNUNET_PLUGIN_TRANSPORT_INIT libgnunet_plugin_transport_https_server_init
41 #define LIBGNUNET_PLUGIN_TRANSPORT_DONE libgnunet_plugin_transport_https_server_done
43 #define PLUGIN_NAME "http_server"
44 #define LIBGNUNET_PLUGIN_TRANSPORT_INIT libgnunet_plugin_transport_http_server_init
45 #define LIBGNUNET_PLUGIN_TRANSPORT_DONE libgnunet_plugin_transport_http_server_done
48 #define HTTP_ERROR_RESPONSE "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\"><HTML><HEAD><TITLE>404 Not Found</TITLE></HEAD><BODY><H1>Not Found</H1>The requested URL was not found on this server.<P><HR><ADDRESS></ADDRESS></BODY></HTML>"
53 /* Enable output for debbuging URL's of incoming requests */
54 #define DEBUG_URL_PARSE GNUNET_NO
58 * Encapsulation of all of the state of the plugin.
63 * Session handle for connections.
68 * Stored in a linked list.
73 * Stored in a linked list.
78 * To whom are we talking to (set to our identity
79 * if we are still waiting for the welcome message)
81 struct GNUNET_PeerIdentity target;
84 * Pointer to the global plugin struct.
86 struct HTTP_Server_Plugin *plugin;
89 * next pointer for double linked list
91 struct HTTP_Message *msg_head;
94 * previous pointer for double linked list
96 struct HTTP_Message *msg_tail;
99 * Message stream tokenizer for incoming data
101 struct GNUNET_SERVER_MessageStreamTokenizer *msg_tk;
106 struct ServerConnection *server_recv;
111 struct ServerConnection *server_send;
124 * Unique HTTP/S connection tag for this connection
129 * ATS network type in NBO
131 uint32_t ats_address_network_type;
134 * Was session given to transport service?
139 * Did we immediately end the session in disconnect_cb
144 * Are incoming connection established at the moment
146 int connect_in_progress;
149 * Absolute time when to receive data again
150 * Used for receive throttling
152 struct GNUNET_TIME_Absolute next_receive;
155 * Session timeout task
157 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
161 struct ServerConnection
166 /* Should this connection get disconnected? GNUNET_YES/NO */
169 /* For PUT connections: Is this the first or last callback with size 0 */
172 /* The session this server connection belongs to */
173 struct Session *session;
175 /* The MHD connection */
176 struct MHD_Connection *mhd_conn;
179 struct MHD_Daemon *mhd_daemon;
184 * Encapsulation of all of the state of the plugin.
186 struct HTTP_Server_Plugin
191 struct GNUNET_TRANSPORT_PluginEnvironment *env;
194 * Linked list head of open sessions.
197 struct Session *head;
200 * Linked list tail of open sessions.
202 struct Session *tail;
215 * My options to be included in the address
222 char *external_hostname;
225 * Verify external address
227 int verify_external_hostname;
231 * Maximum number of sockets the plugin can use
232 * Each http inbound /outbound connections are two connections
234 unsigned int max_connections;
237 * Current number of sockets the plugin can use
238 * Each http inbound /outbound connections are two connections
240 unsigned int cur_connections;
243 * Did we immediately end the session in disconnect_cb
253 * External hostname the plugin can be connected to, can be different to
254 * the host's FQDN, used e.g. for reverse proxying
256 struct HttpAddress *ext_addr;
259 * Notify transport only about external address
261 unsigned int external_only;
264 * External address length
284 * Task calling transport service about external address
286 GNUNET_SCHEDULER_TaskIdentifier notify_ext_task;
289 * NAT handle & address management
291 struct GNUNET_NAT_Handle *nat;
294 * List of own addresses
298 * IPv4 addresses DLL head
300 struct HttpAddressWrapper *addr_head;
303 * IPv4 addresses DLL tail
305 struct HttpAddressWrapper *addr_tail;
308 * IPv4 server socket to bind to
310 struct sockaddr_in *server_addr_v4;
313 * IPv6 server socket to bind to
315 struct sockaddr_in6 *server_addr_v6;
320 GNUNET_SCHEDULER_TaskIdentifier server_v4_task;
325 GNUNET_SCHEDULER_TaskIdentifier server_v6_task;
328 * The IPv4 server is scheduled to run asap
330 int server_v4_immediately;
333 * The IPv6 server is scheduled to run asap
335 int server_v6_immediately;
340 struct MHD_Daemon *server_v4;
345 struct MHD_Daemon *server_v6;
353 * Use RC4-128 instead of AES:
354 * NONE:+VERS-TLS1.0:+ARCFOUR-128:+SHA1:+RSA:+COMP-NULL
374 * Wrapper to manage addresses
376 struct HttpAddressWrapper
381 struct HttpAddressWrapper *next;
384 * Linked list previous
386 struct HttpAddressWrapper *prev;
388 struct HttpAddress *address;
395 * Message to send using http
400 * next pointer for double linked list
402 struct HTTP_Message *next;
405 * previous pointer for double linked list
407 struct HTTP_Message *prev;
410 * buffer containing data to send
415 * amount of data already sent
425 * HTTP/S specific overhead
430 * Continuation function to call once the transmission buffer
431 * has again space available. NULL if there is no
432 * continuation to call.
434 GNUNET_TRANSPORT_TransmitContinuation transmit_cont;
437 * Closure for transmit_cont.
439 void *transmit_cont_cls;
444 * The http_server plugin handle
446 static struct HTTP_Server_Plugin * p;
450 * Start session timeout for session s
451 * @param s the session
454 server_start_session_timeout (struct Session *s);
458 * Increment session timeout due to activity for session s
459 * @param s the session
462 server_reschedule_session_timeout (struct Session *s);
466 * Cancel timeout for session s
467 * @param s the session
470 server_stop_session_timeout (struct Session *s);
474 * Disconnect a session s
475 * @param s the session
478 server_disconnect (struct Session *s);
482 * Does session s exist?
484 * @param plugin the plugin handle
485 * @param s the session
486 * @return GNUNET_YES on success, GNUNET_NO on error
489 server_exist_session (struct HTTP_Server_Plugin *plugin, struct Session *s);
493 * Reschedule the execution of both IPv4 and IPv6 server
494 * @param plugin the plugin
495 * @param server which server to schedule v4 or v6?
496 * @param now GNUNET_YES to schedule execution immediately, GNUNET_NO to wait
500 server_reschedule (struct HTTP_Server_Plugin *plugin, struct MHD_Daemon *server,
505 * Function that can be used by the transport service to transmit
506 * a message using the plugin. Note that in the case of a
507 * peer disconnecting, the continuation MUST be called
508 * prior to the disconnect notification itself. This function
509 * will be called with this peer's HELLO message to initiate
510 * a fresh connection to another peer.
513 * @param session which session must be used
514 * @param msgbuf the message to transmit
515 * @param msgbuf_size number of bytes in 'msgbuf'
516 * @param priority how important is the message (most plugins will
517 * ignore message priority and just FIFO)
518 * @param to how long to wait at most for the transmission (does not
519 * require plugins to discard the message after the timeout,
520 * just advisory for the desired delay; most plugins will ignore
522 * @param cont continuation to call once the message has
523 * been transmitted (or if the transport is ready
524 * for the next transmission call; or if the
525 * peer disconnected...); can be NULL
526 * @param cont_cls closure for cont
527 * @return number of bytes used (on the physical network, with overheads);
528 * -1 on hard errors (i.e. address invalid); 0 is a legal value
529 * and does NOT mean that the message was not transmitted (DV)
532 http_server_plugin_send (void *cls,
533 struct Session *session,
534 const char *msgbuf, size_t msgbuf_size,
535 unsigned int priority,
536 struct GNUNET_TIME_Relative to,
537 GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
539 struct HTTP_Server_Plugin *plugin = cls;
540 struct HTTP_Message *msg;
544 GNUNET_assert (plugin != NULL);
545 GNUNET_assert (session != NULL);
547 if (GNUNET_NO == server_exist_session (plugin, session))
550 return GNUNET_SYSERR;
552 if (NULL == session->server_send)
554 if (GNUNET_NO == session->connect_in_progress)
556 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, session->plugin->name,
557 "Session %p/connection %p: Sending message with %u bytes to peer `%s' with FAILED\n",
558 session, session->server_send,
559 msgbuf_size, GNUNET_i2s (&session->target));
561 return GNUNET_SYSERR;
566 if (GNUNET_YES == session->server_send->disconnect)
567 return GNUNET_SYSERR;
571 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, session->plugin->name,
572 "Session %p/connection %p: Sending message with %u to peer `%s' with \n",
573 session, session->server_send,
574 msgbuf_size, GNUNET_i2s (&session->target));
576 /* create new message and schedule */
577 bytes_sent = sizeof (struct HTTP_Message) + msgbuf_size;
578 msg = GNUNET_malloc (bytes_sent);
580 msg->size = msgbuf_size;
582 msg->buf = (char *) &msg[1];
583 msg->transmit_cont = cont;
584 msg->transmit_cont_cls = cont_cls;
585 memcpy (msg->buf, msgbuf, msgbuf_size);
587 GNUNET_CONTAINER_DLL_insert_tail (session->msg_head, session->msg_tail, msg);
589 GNUNET_asprintf (&stat_txt, "# bytes currently in %s_server buffers", plugin->protocol);
590 GNUNET_STATISTICS_update (plugin->env->stats,
591 stat_txt, msgbuf_size, GNUNET_NO);
592 GNUNET_free (stat_txt);
594 if (NULL != session->server_send)
596 server_reschedule (session->plugin,
597 session->server_send->mhd_daemon,
599 server_reschedule_session_timeout (session);
607 * Function that can be used to force the plugin to disconnect
608 * from the given peer and cancel all previous transmissions
609 * (and their continuationc).
612 * @param target peer from which to disconnect
615 http_server_plugin_disconnect (void *cls, const struct GNUNET_PeerIdentity *target)
617 struct HTTP_Server_Plugin *plugin = cls;
618 struct Session *next = NULL;
619 struct Session *pos = NULL;
621 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
622 "Transport tells me to disconnect `%s'\n",
623 GNUNET_i2s (target));
626 while (NULL != (pos = next))
629 if (0 == memcmp (target, &pos->target, sizeof (struct GNUNET_PeerIdentity)))
631 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
632 "Disconnecting session %p to `%s'\n",
633 pos, GNUNET_i2s (target));
634 server_disconnect (pos);
642 * Another peer has suggested an address for this
643 * peer and transport plugin. Check that this could be a valid
644 * address. If so, consider adding it to the list
648 * @param addr pointer to the address
649 * @param addrlen length of addr
650 * @return GNUNET_OK if this is a plausible address for this peer
654 http_server_plugin_address_suggested (void *cls, const void *addr,
657 struct HttpAddressWrapper *next;
658 struct HttpAddressWrapper *pos;
659 const struct HttpAddress *haddr = addr;
661 if ((NULL != p->ext_addr) &&
662 GNUNET_YES == (http_common_cmp_addresses (addr, addrlen,
663 p->ext_addr, p->ext_addr_len)))
665 /* Checking HTTP_OPTIONS_VERIFY_CERTIFICATE option for external hostname */
666 if ((ntohl(haddr->options) & HTTP_OPTIONS_VERIFY_CERTIFICATE) !=
667 (p->options & HTTP_OPTIONS_VERIFY_CERTIFICATE))
668 return GNUNET_NO; /* VERIFY option not set as required! */
673 while (NULL != (pos = next))
676 if (GNUNET_YES == (http_common_cmp_addresses(addr,
688 * Creates a new outbound session the transport
689 * service will use to send data to the peer
691 * Since HTTP/S server cannot create sessions, always return NULL
693 * @param cls the plugin
694 * @param address the address
695 * @return always NULL
697 static struct Session *
698 http_server_plugin_get_session (void *cls,
699 const struct GNUNET_HELLO_Address *address)
706 * Deleting the session
707 * Must not be used afterwards
709 * @param s the session to delete
712 server_delete_session (struct Session *s)
714 struct HTTP_Server_Plugin *plugin = s->plugin;
715 server_stop_session_timeout(s);
717 GNUNET_CONTAINER_DLL_remove (plugin->head, plugin->tail, s);
718 struct HTTP_Message *msg = s->msg_head;
719 struct HTTP_Message *tmp = NULL;
725 GNUNET_CONTAINER_DLL_remove (s->msg_head, s->msg_tail, msg);
726 if (msg->transmit_cont != NULL)
728 msg->transmit_cont (msg->transmit_cont_cls, &s->target, GNUNET_SYSERR,
729 msg->size, msg->pos + msg->overhead);
735 if (s->msg_tk != NULL)
737 GNUNET_SERVER_mst_destroy (s->msg_tk);
740 GNUNET_free (s->addr);
741 GNUNET_free_non_null (s->server_recv);
742 GNUNET_free_non_null (s->server_send);
743 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
744 "Session %p destroyed\n", s);
750 * Cancel timeout for session s
752 * @param s the session
755 server_stop_session_timeout (struct Session *s)
757 GNUNET_assert (NULL != s);
759 if (GNUNET_SCHEDULER_NO_TASK != s->timeout_task)
761 GNUNET_SCHEDULER_cancel (s->timeout_task);
762 s->timeout_task = GNUNET_SCHEDULER_NO_TASK;
763 GNUNET_log (TIMEOUT_LOG, "Timeout stopped for session %p\n", s);
769 * Function that queries MHD's select sets and
770 * starts the task waiting for them.
771 * @param plugin plugin
772 * @param daemon_handle the MHD daemon handle
773 * @param now schedule immediately
774 * @return gnunet task identifier
776 static GNUNET_SCHEDULER_TaskIdentifier
777 server_schedule (struct HTTP_Server_Plugin *plugin,
778 struct MHD_Daemon *daemon_handle,
783 * Reschedule the execution of both IPv4 and IPv6 server
784 * @param plugin the plugin
785 * @param server which server to schedule v4 or v6?
786 * @param now GNUNET_YES to schedule execution immediately, GNUNET_NO to wait
790 server_reschedule (struct HTTP_Server_Plugin *plugin, struct MHD_Daemon *server,
793 if ((server == plugin->server_v4) && (plugin->server_v4 != NULL))
795 if (GNUNET_YES == plugin->server_v4_immediately)
796 return; /* No rescheduling, server will run asap */
798 if (GNUNET_YES == now)
799 plugin->server_v4_immediately = GNUNET_YES;
801 if (plugin->server_v4_task != GNUNET_SCHEDULER_NO_TASK)
803 GNUNET_SCHEDULER_cancel (plugin->server_v4_task);
804 plugin->server_v4_task = GNUNET_SCHEDULER_NO_TASK;
806 plugin->server_v4_task = server_schedule (plugin, plugin->server_v4, now);
809 if ((server == plugin->server_v6) && (plugin->server_v6 != NULL))
811 if (GNUNET_YES == plugin->server_v6_immediately)
812 return; /* No rescheduling, server will run asap */
814 if (GNUNET_YES == now)
815 plugin->server_v6_immediately = GNUNET_YES;
817 if (plugin->server_v6_task != GNUNET_SCHEDULER_NO_TASK)
819 GNUNET_SCHEDULER_cancel (plugin->server_v6_task);
820 plugin->server_v6_task = GNUNET_SCHEDULER_NO_TASK;
822 plugin->server_v6_task = server_schedule (plugin, plugin->server_v6, now);
828 * Disconnect session s
830 * @param s the session
831 * @return GNUNET_OK on success
834 server_disconnect (struct Session *s)
836 struct ServerConnection * send = NULL;
837 struct ServerConnection * recv = NULL;
839 if (GNUNET_NO == server_exist_session (p, s))
842 return GNUNET_SYSERR;
845 send = (struct ServerConnection *) s->server_send;
846 if (s->server_send != NULL)
848 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, s->plugin->name,
849 "Server: %p / %p Terminating inbound PUT session to peer `%s'\n",
850 s, s->server_send, GNUNET_i2s (&s->target));
852 send->disconnect = GNUNET_YES;
853 #if MHD_VERSION >= 0x00090E00
854 MHD_set_connection_option (send->mhd_conn, MHD_CONNECTION_OPTION_TIMEOUT,
857 server_reschedule (s->plugin, send->mhd_daemon, GNUNET_YES);
860 recv = (struct ServerConnection *) s->server_recv;
863 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, s->plugin->name,
864 "Server: %p / %p Terminating inbound GET session to peer `%s'\n",
865 s, s->server_recv, GNUNET_i2s (&s->target));
867 recv->disconnect = GNUNET_YES;
868 #if MHD_VERSION >= 0x00090E00
869 MHD_set_connection_option (recv->mhd_conn, MHD_CONNECTION_OPTION_TIMEOUT,
872 server_reschedule (s->plugin, recv->mhd_daemon, GNUNET_YES);
880 * Tell MHD that the connection should timeout after @a to seconds.
882 * @param plugin our plugin
883 * @param s session for which the timeout changes
884 * @param to timeout in seconds
887 server_mhd_connection_timeout (struct HTTP_Server_Plugin *plugin,
891 #if MHD_VERSION >= 0x00090E00
892 /* Setting timeouts for other connections */
893 if (NULL != s->server_recv)
895 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
896 "Setting timeout for %p to %u sec.\n",
898 MHD_set_connection_option (s->server_recv->mhd_conn,
899 MHD_CONNECTION_OPTION_TIMEOUT,
901 server_reschedule (plugin, s->server_recv->mhd_daemon, GNUNET_NO);
903 if (NULL != s->server_send)
905 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
906 "Setting timeout for %p to %u sec.\n",
908 MHD_set_connection_option (s->server_send->mhd_conn,
909 MHD_CONNECTION_OPTION_TIMEOUT,
911 server_reschedule (plugin, s->server_send->mhd_daemon, GNUNET_NO);
917 * Parse incoming URL for tag and target
919 * @param plugin plugin
920 * @param url incoming url
921 * @param target where to store the target
922 * @param tag where to store the tag
923 * @return GNUNET_OK on success, GNUNET_SYSERR on error
927 server_parse_url (struct HTTP_Server_Plugin *plugin, const char * url, struct GNUNET_PeerIdentity * target, uint32_t *tag)
929 char * tag_start = NULL;
930 char * tag_end = NULL;
931 char * target_start = NULL;
932 char * separator = NULL;
933 char hash[plugin->peer_id_length+1];
935 unsigned long int ctag;
938 * URL is valid if it is in the form [prefix with (multiple) '/'][peerid[103];tag]*/
943 return GNUNET_SYSERR;
948 separator = strrchr (url, ';');
950 if (NULL == separator)
952 if (DEBUG_URL_PARSE) GNUNET_break (0);
953 return GNUNET_SYSERR;
955 tag_start = separator + 1;
957 if (strlen (tag_start) == 0)
959 /* No tag after separator */
960 if (DEBUG_URL_PARSE) GNUNET_break (0);
961 return GNUNET_SYSERR;
963 ctag = strtoul (tag_start, &tag_end, 10);
966 /* tag == 0 , invalid */
967 if (DEBUG_URL_PARSE) GNUNET_break (0);
968 return GNUNET_SYSERR;
970 if ((ctag == ULONG_MAX) && (ERANGE == errno))
972 /* out of range: > ULONG_MAX */
973 if (DEBUG_URL_PARSE) GNUNET_break (0);
974 return GNUNET_SYSERR;
976 if (ctag > UINT32_MAX)
978 /* out of range: > UINT32_MAX */
979 if (DEBUG_URL_PARSE) GNUNET_break (0);
980 return GNUNET_SYSERR;
982 (*tag) = (uint32_t) ctag;
985 /* no char after tag */
986 if (DEBUG_URL_PARSE) GNUNET_break (0);
987 return GNUNET_SYSERR;
989 if (url[strlen(url)] != tag_end[0])
991 /* there are more not converted chars after tag */
992 if (DEBUG_URL_PARSE) GNUNET_break (0);
993 return GNUNET_SYSERR;
996 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
997 "Found tag `%u' in url\n", (*tag));
999 /* convert peer id */
1000 target_start = strrchr (url, '/');
1001 if (NULL == target_start)
1003 /* no leading '/' */
1004 target_start = (char *) url;
1007 hash_length = separator - target_start;
1008 if (hash_length != plugin->peer_id_length)
1010 /* no char after tag */
1011 if (DEBUG_URL_PARSE) GNUNET_break (0);
1012 return GNUNET_SYSERR;
1014 memcpy (hash, target_start, hash_length);
1015 hash[hash_length] = '\0';
1017 if (GNUNET_OK != GNUNET_CRYPTO_hash_from_string ((const char *) hash, &(target->hashPubKey)))
1019 /* hash conversion failed */
1020 if (DEBUG_URL_PARSE) GNUNET_break (0);
1021 return GNUNET_SYSERR;
1024 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1025 "Found target `%s' in url\n", GNUNET_h2s_full(&target->hashPubKey));
1031 * Lookup a mhd connection and create one if none is found
1033 * @param plugin the plugin handle
1034 * @param mhd_connection the incoming mhd_connection
1035 * @param url incoming requested URL
1036 * @param method PUT or GET
1037 * @return the server connecetion
1039 static struct ServerConnection *
1040 server_lookup_connection (struct HTTP_Server_Plugin *plugin,
1041 struct MHD_Connection *mhd_connection, const char *url,
1044 struct Session *s = NULL;
1045 struct ServerConnection *sc = NULL;
1046 const union MHD_ConnectionInfo *conn_info;
1047 struct GNUNET_ATS_Information ats;
1048 struct HttpAddress *addr;
1050 struct GNUNET_PeerIdentity target;
1052 int direction = GNUNET_SYSERR;
1055 conn_info = MHD_get_connection_info (mhd_connection,
1056 MHD_CONNECTION_INFO_CLIENT_ADDRESS);
1057 if ((conn_info->client_addr->sa_family != AF_INET) &&
1058 (conn_info->client_addr->sa_family != AF_INET6))
1060 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1061 "New %s connection from %s\n", method, url);
1063 if (GNUNET_SYSERR == server_parse_url (plugin, url, &target, &tag))
1065 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1066 "Invalid url %s\n", url);
1069 if (0 == strcmp (MHD_HTTP_METHOD_PUT, method))
1070 direction = _RECEIVE;
1071 else if (0 == strcmp (MHD_HTTP_METHOD_GET, method))
1075 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1076 "Invalid method %s connection from %s\n", method, url);
1080 plugin->cur_connections++;
1081 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1082 "New %s connection from %s with tag %u (%u of %u)\n",
1084 GNUNET_i2s (&target), tag,
1085 plugin->cur_connections, plugin->max_connections);
1086 /* find duplicate session */
1090 if ((0 == memcmp (&s->target, &target, sizeof (struct GNUNET_PeerIdentity))) &&
1097 if ((_RECEIVE == direction) && (NULL != s->server_recv))
1099 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1100 "Duplicate PUT connection from `%s' tag %u, dismissing new connection\n",
1101 GNUNET_i2s (&target),
1106 if ((_SEND == direction) && (NULL != s->server_send))
1108 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1109 "Duplicate GET connection from `%s' tag %u, dismissing new connection\n",
1110 GNUNET_i2s (&target),
1117 /* create new session */
1118 switch (conn_info->client_addr->sa_family)
1121 addr = http_common_address_from_socket (plugin->protocol, conn_info->client_addr, sizeof (struct sockaddr_in));
1122 addr_len = http_common_address_get_size (addr);
1123 ats = plugin->env->get_address_type (plugin->env->cls, conn_info->client_addr, sizeof (struct sockaddr_in));
1126 addr = http_common_address_from_socket (plugin->protocol, conn_info->client_addr, sizeof (struct sockaddr_in6));
1127 addr_len = http_common_address_get_size (addr);
1128 ats = plugin->env->get_address_type (plugin->env->cls, conn_info->client_addr, sizeof (struct sockaddr_in6));
1131 /* external host name */
1132 ats.type = htonl (GNUNET_ATS_NETWORK_TYPE);
1133 ats.type = htonl (GNUNET_ATS_NET_WAN);
1137 s = GNUNET_malloc (sizeof (struct Session));
1138 memcpy (&s->target, &target, sizeof (struct GNUNET_PeerIdentity));
1141 s->addrlen = addr_len;
1142 s->ats_address_network_type = ats.value;
1143 s->next_receive = GNUNET_TIME_UNIT_ZERO_ABS;
1145 s->server_recv = NULL;
1146 s->server_send = NULL;
1147 s->session_passed = GNUNET_NO;
1148 s->session_ended = GNUNET_NO;
1149 s->connect_in_progress = GNUNET_YES;
1150 server_start_session_timeout(s);
1151 GNUNET_CONTAINER_DLL_insert (plugin->head, plugin->tail, s);
1153 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1154 "Creating new session %p for peer `%s' connecting from `%s'\n",
1155 s, GNUNET_i2s (&target),
1156 http_common_plugin_address_to_string (NULL, p->protocol, addr, addr_len));
1158 sc = GNUNET_malloc (sizeof (struct ServerConnection));
1159 if (conn_info->client_addr->sa_family == AF_INET)
1160 sc->mhd_daemon = plugin->server_v4;
1161 if (conn_info->client_addr->sa_family == AF_INET6)
1162 sc->mhd_daemon = plugin->server_v6;
1163 sc->mhd_conn = mhd_connection;
1164 sc->direction = direction;
1165 sc->connected = GNUNET_NO;
1167 if (direction == _SEND)
1168 s->server_send = sc;
1169 if (direction == _RECEIVE)
1170 s->server_recv = sc;
1172 if ((NULL != s->server_send) && (NULL != s->server_recv))
1174 s->connect_in_progress = GNUNET_NO; /* PUT and GET are connected */
1175 plugin->env->session_start (NULL, &s->target, PLUGIN_NAME, NULL, 0 ,s, NULL, 0);
1178 #if MHD_VERSION >= 0x00090E00
1179 if ((NULL == s->server_recv) || (NULL == s->server_send))
1181 to = (HTTP_SERVER_NOT_VALIDATED_TIMEOUT.rel_value_us / 1000LL / 1000LL);
1182 MHD_set_connection_option (mhd_connection,
1183 MHD_CONNECTION_OPTION_TIMEOUT, to);
1184 server_reschedule (plugin, sc->mhd_daemon, GNUNET_NO);
1188 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1189 "Session %p for peer `%s' fully connected\n",
1190 s, GNUNET_i2s (&target));
1191 to = (SERVER_SESSION_TIMEOUT.rel_value_us / 1000LL / 1000LL);
1192 server_mhd_connection_timeout (plugin, s, to);
1195 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1196 "Setting timeout for %p to %u sec.\n", sc, to);
1203 * Lookup a session for a server connection
1205 * @param plugin the plugin
1206 * @param sc the server connection
1207 * @return the session found or NULL
1209 static struct Session *
1210 server_lookup_session (struct HTTP_Server_Plugin *plugin,
1211 struct ServerConnection * sc)
1215 for (s = plugin->head; NULL != s; s = s->next)
1216 if ((s->server_recv == sc) || (s->server_send == sc))
1222 server_exist_session (struct HTTP_Server_Plugin *plugin, struct Session *s)
1224 struct Session * head;
1226 GNUNET_assert (NULL != plugin);
1227 GNUNET_assert (NULL != s);
1229 for (head = plugin->head; head != NULL; head = head->next)
1239 * Callback called by MHD when it needs data to send
1241 * @param cls current session
1242 * @param pos position in buffer
1243 * @param buf the buffer to write data to
1244 * @param max max number of bytes available in buffer
1245 * @return bytes written to buffer
1248 server_send_callback (void *cls, uint64_t pos, char *buf, size_t max)
1250 struct Session *s = cls;
1251 ssize_t bytes_read = 0;
1252 struct HTTP_Message *msg;
1255 GNUNET_assert (NULL != p);
1256 if (GNUNET_NO == server_exist_session (p, s))
1262 bytes_read = GNUNET_MIN (msg->size - msg->pos,
1264 memcpy (buf, &msg->buf[msg->pos], bytes_read);
1265 msg->pos += bytes_read;
1267 /* removing message */
1268 if (msg->pos == msg->size)
1270 GNUNET_CONTAINER_DLL_remove (s->msg_head, s->msg_tail, msg);
1271 if (NULL != msg->transmit_cont)
1272 msg->transmit_cont (msg->transmit_cont_cls, &s->target, GNUNET_OK,
1273 msg->size, msg->size + msg->overhead);
1279 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, s->plugin->name,
1280 "Sent %u bytes to peer `%s' with session %p \n", bytes_read, GNUNET_i2s (&s->target), s);
1281 GNUNET_asprintf (&stat_txt, "# bytes currently in %s_server buffers", p->protocol);
1282 GNUNET_STATISTICS_update (p->env->stats,
1283 stat_txt, -bytes_read, GNUNET_NO);
1284 GNUNET_free (stat_txt);
1285 GNUNET_asprintf (&stat_txt, "# bytes transmitted via %s_server", p->protocol);
1286 GNUNET_STATISTICS_update (p->env->stats,
1287 stat_txt, bytes_read, GNUNET_NO);
1288 GNUNET_free (stat_txt);
1295 * Callback called by MessageStreamTokenizer when a message has arrived
1297 * @param cls current session as closure
1298 * @param client client
1299 * @param message the message to be forwarded to transport service
1303 server_receive_mst_cb (void *cls, void *client,
1304 const struct GNUNET_MessageHeader *message)
1306 struct Session *s = cls;
1307 struct HTTP_Server_Plugin *plugin = s->plugin;
1308 struct GNUNET_ATS_Information atsi;
1309 struct GNUNET_TIME_Relative delay;
1312 GNUNET_assert (NULL != p);
1313 if (GNUNET_NO == server_exist_session(p, s))
1317 atsi.type = htonl (GNUNET_ATS_NETWORK_TYPE);
1318 atsi.value = s->ats_address_network_type;
1319 GNUNET_break (s->ats_address_network_type != ntohl (GNUNET_ATS_NET_UNSPECIFIED));
1322 delay = plugin->env->receive (plugin->env->cls,
1327 plugin->env->update_address_metrics (plugin->env->cls,
1329 NULL, 0, s, &atsi, 1);
1331 GNUNET_asprintf (&stat_txt, "# bytes received via %s_server", plugin->protocol);
1332 GNUNET_STATISTICS_update (plugin->env->stats,
1333 stat_txt, ntohs (message->size), GNUNET_NO);
1334 GNUNET_free (stat_txt);
1336 s->session_passed = GNUNET_YES;
1337 s->next_receive = GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (), delay);
1338 if (delay.rel_value_us > 0)
1340 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1341 "Peer `%s' address `%s' next read delayed for %s\n",
1342 GNUNET_i2s (&s->target),
1343 http_common_plugin_address_to_string (NULL, p->protocol, s->addr, s->addrlen),
1344 GNUNET_STRINGS_relative_time_to_string (delay,
1347 server_reschedule_session_timeout (s);
1353 * MHD callback for a new incoming connection
1355 * @param cls the plugin handle
1356 * @param mhd_connection the mhd connection
1357 * @param url the requested URL
1358 * @param method GET or PUT
1359 * @param version HTTP version
1360 * @param upload_data upload data
1361 * @param upload_data_size sizeof upload data
1362 * @param httpSessionCache the session cache to remember the connection
1363 * @return MHD_YES if connection is accepted, MHD_NO on reject
1366 server_access_cb (void *cls, struct MHD_Connection *mhd_connection,
1367 const char *url, const char *method, const char *version,
1368 const char *upload_data, size_t * upload_data_size,
1369 void **httpSessionCache)
1371 struct HTTP_Server_Plugin *plugin = cls;
1374 struct ServerConnection *sc = *httpSessionCache;
1376 struct MHD_Response *response;
1378 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1379 _("Access from connection %p (%u of %u) for `%s' `%s' url `%s' with upload data size %u\n"),
1381 plugin->cur_connections, plugin->max_connections,
1382 method, version, url, (*upload_data_size));
1384 GNUNET_assert (cls != NULL);
1387 /* new connection */
1388 sc = server_lookup_connection (plugin, mhd_connection, url, method);
1391 (*httpSessionCache) = sc;
1395 response = MHD_create_response_from_data (strlen (HTTP_ERROR_RESPONSE), HTTP_ERROR_RESPONSE, MHD_NO, MHD_NO);
1396 MHD_add_response_header (response,
1397 MHD_HTTP_HEADER_CONTENT_TYPE,
1399 res = MHD_queue_response (mhd_connection, MHD_HTTP_NOT_FOUND, response);
1400 MHD_destroy_response (response);
1406 /* 'old' connection */
1407 if (NULL == server_lookup_session (plugin, sc))
1409 /* Session was already disconnected */
1414 /* existing connection */
1415 sc = (*httpSessionCache);
1417 GNUNET_assert (NULL != s);
1418 /* connection is to be disconnected */
1419 if (sc->disconnect == GNUNET_YES)
1421 /* Sent HTTP/1.1: 200 OK as response */
1422 response = MHD_create_response_from_data (strlen ("Thank you!"),
1425 MHD_queue_response (mhd_connection, MHD_HTTP_OK, response);
1426 MHD_destroy_response (response);
1429 GNUNET_assert (s != NULL);
1431 if (sc->direction == _SEND)
1433 response = MHD_create_response_from_callback (MHD_SIZE_UNKNOWN,
1435 &server_send_callback, s,
1437 MHD_queue_response (mhd_connection, MHD_HTTP_OK, response);
1438 MHD_destroy_response (response);
1441 if (sc->direction == _RECEIVE)
1443 if ((*upload_data_size == 0) && (sc->connected == GNUNET_NO))
1445 /* (*upload_data_size == 0) first callback when header are passed */
1446 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1447 "Session %p / Connection %p: Peer `%s' PUT on address `%s' connected\n",
1449 GNUNET_i2s (&s->target),
1450 http_common_plugin_address_to_string (NULL,
1454 sc->connected = GNUNET_YES;
1457 else if ((*upload_data_size == 0) && (sc->connected == GNUNET_YES))
1459 /* (*upload_data_size == 0) when upload is complete */
1460 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1461 "Session %p / Connection %p: Peer `%s' PUT on address `%s' finished upload\n",
1463 GNUNET_i2s (&s->target),
1464 http_common_plugin_address_to_string (NULL,
1468 sc->connected = GNUNET_NO;
1469 /* Sent HTTP/1.1: 200 OK as PUT Response\ */
1470 response = MHD_create_response_from_data (strlen ("Thank you!"),
1473 MHD_queue_response (mhd_connection, MHD_HTTP_OK, response);
1474 MHD_destroy_response (response);
1477 else if ((*upload_data_size > 0) && (sc->connected == GNUNET_YES))
1479 /* (*upload_data_size > 0) for every segment received */
1480 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1481 "Session %p / Connection %p: Peer `%s' PUT on address `%s' received %u bytes\n",
1483 GNUNET_i2s (&s->target),
1484 http_common_plugin_address_to_string (NULL,
1489 struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get ();
1491 if ((s->next_receive.abs_value_us <= now.abs_value_us))
1493 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1494 "PUT with %u bytes forwarded to MST\n",
1496 if (s->msg_tk == NULL)
1498 s->msg_tk = GNUNET_SERVER_mst_create (&server_receive_mst_cb, s);
1500 GNUNET_SERVER_mst_receive (s->msg_tk, s, upload_data,
1501 *upload_data_size, GNUNET_NO, GNUNET_NO);
1502 server_mhd_connection_timeout (plugin, s,
1503 GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT.rel_value_us / 1000LL / 1000LL);
1504 (*upload_data_size) = 0;
1508 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1509 "Session %p / Connection %p: no inbound bandwidth available! Next read was delayed by %s\n",
1511 GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_duration (s->next_receive),
1527 * Callback from MHD when a connection disconnects
1529 * @param cls closure
1530 * @param connection the disconnected MHD connection
1531 * @param httpSessionCache the pointer to distinguish
1534 server_disconnect_cb (void *cls, struct MHD_Connection *connection,
1535 void **httpSessionCache)
1537 struct ServerConnection *sc = *httpSessionCache;
1538 struct Session *s = NULL;
1539 struct Session *t = NULL;
1540 struct HTTP_Server_Plugin *plugin = NULL;
1542 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, p->name,
1543 "Disconnect for connection %p \n", sc);
1548 if (NULL == (s = server_lookup_session (p, sc)))
1551 GNUNET_assert (NULL != p);
1552 for (t = p->head; t != NULL; t = t->next)
1561 if (sc->direction == _SEND)
1564 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1565 "Peer `%s' connection %p, GET on address `%s' disconnected\n",
1566 GNUNET_i2s (&s->target), s->server_send,
1567 http_common_plugin_address_to_string (NULL, p->protocol, s->addr, s->addrlen));
1568 s->server_send = NULL;
1569 if (NULL != (s->server_recv))
1571 s->server_recv->disconnect = GNUNET_YES;
1572 GNUNET_assert (NULL != s->server_recv->mhd_conn);
1573 #if MHD_VERSION >= 0x00090E00
1574 MHD_set_connection_option (s->server_recv->mhd_conn, MHD_CONNECTION_OPTION_TIMEOUT,
1577 server_reschedule (plugin, s->server_recv->mhd_daemon, GNUNET_NO);
1580 if (sc->direction == _RECEIVE)
1582 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1583 "Peer `%s' connection %p PUT on address `%s' disconnected\n",
1584 GNUNET_i2s (&s->target), s->server_recv,
1585 http_common_plugin_address_to_string (NULL, p->protocol, s->addr, s->addrlen));
1586 s->server_recv = NULL;
1587 /* Do not terminate session when PUT disconnects
1588 if (NULL != (s->server_send))
1590 s->server_send->disconnect = GNUNET_YES;
1591 GNUNET_assert (NULL != s->server_send->mhd_conn);
1592 #if MHD_VERSION >= 0x00090E00
1593 MHD_set_connection_option (s->server_send->mhd_conn, MHD_CONNECTION_OPTION_TIMEOUT,
1596 server_reschedule (plugin, s->server_send->mhd_daemon, GNUNET_NO);
1598 if (s->msg_tk != NULL)
1600 GNUNET_SERVER_mst_destroy (s->msg_tk);
1606 plugin->cur_connections--;
1608 if ((s->server_send == NULL) && (s->server_recv == NULL))
1610 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1611 "Peer `%s' on address `%s' disconnected\n",
1612 GNUNET_i2s (&s->target),
1613 http_common_plugin_address_to_string (NULL, p->protocol, s->addr, s->addrlen));
1615 if ((GNUNET_YES == s->session_passed) && (GNUNET_NO == s->session_ended))
1617 /* Notify transport immediately that this session is invalid */
1618 s->session_ended = GNUNET_YES;
1619 plugin->env->session_end (plugin->env->cls, &s->target, s);
1621 server_delete_session (s);
1628 * Check if incoming connection is accepted.
1630 * @param cls plugin as closure
1631 * @param addr address of incoming connection
1632 * @param addr_len address length of incoming connection
1633 * @return MHD_YES if connection is accepted, MHD_NO if connection is rejected
1637 server_accept_cb (void *cls, const struct sockaddr *addr, socklen_t addr_len)
1639 struct HTTP_Server_Plugin *plugin = cls;
1641 if (plugin->cur_connections <= plugin->max_connections)
1643 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1644 _("Accepting connection (%u of %u) from `%s'\n"),
1645 plugin->cur_connections, plugin->max_connections,
1646 GNUNET_a2s (addr, addr_len));
1651 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, plugin->name,
1652 _("Server reached maximum number connections (%u), rejecting new connection\n"),
1653 plugin->max_connections);
1659 server_log (void *arg, const char *fmt, va_list ap)
1663 vsnprintf (text, sizeof (text), fmt, ap);
1665 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Server: %s\n", text);
1670 * Call MHD IPv4 to process pending requests and then go back
1671 * and schedule the next run.
1672 * @param cls plugin as closure
1673 * @param tc task context
1676 server_v4_run (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1678 struct HTTP_Server_Plugin *plugin = cls;
1680 GNUNET_assert (cls != NULL);
1682 plugin->server_v4_task = GNUNET_SCHEDULER_NO_TASK;
1683 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1686 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1687 "Running IPv4 server\n");
1689 plugin->server_v4_immediately = GNUNET_NO;
1690 GNUNET_assert (MHD_YES == MHD_run (plugin->server_v4));
1691 server_reschedule (plugin, plugin->server_v4, GNUNET_NO);
1696 * Call MHD IPv6 to process pending requests and then go back
1697 * and schedule the next run.
1698 * @param cls plugin as closure
1699 * @param tc task context
1702 server_v6_run (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1704 struct HTTP_Server_Plugin *plugin = cls;
1706 GNUNET_assert (cls != NULL);
1707 plugin->server_v6_task = GNUNET_SCHEDULER_NO_TASK;
1708 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1711 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1712 "Running IPv6 server\n");
1714 plugin->server_v6_immediately = GNUNET_NO;
1715 GNUNET_assert (MHD_YES == MHD_run (plugin->server_v6));
1716 server_reschedule (plugin, plugin->server_v6, GNUNET_NO);
1720 #define UNSIGNED_MHD_LONG_LONG unsigned MHD_LONG_LONG
1723 * Function that queries MHD's select sets and
1724 * starts the task waiting for them.
1726 * @param plugin plugin
1727 * @param daemon_handle the MHD daemon handle
1728 * @return gnunet task identifier
1730 static GNUNET_SCHEDULER_TaskIdentifier
1731 server_schedule (struct HTTP_Server_Plugin *plugin,
1732 struct MHD_Daemon *daemon_handle,
1735 GNUNET_SCHEDULER_TaskIdentifier ret;
1739 struct GNUNET_NETWORK_FDSet *wrs;
1740 struct GNUNET_NETWORK_FDSet *wws;
1741 struct GNUNET_NETWORK_FDSet *wes;
1743 UNSIGNED_MHD_LONG_LONG timeout;
1744 static unsigned long long last_timeout = 0;
1747 struct GNUNET_TIME_Relative tv;
1749 if (GNUNET_YES == plugin->in_shutdown)
1750 return GNUNET_SCHEDULER_NO_TASK;
1752 ret = GNUNET_SCHEDULER_NO_TASK;
1756 wrs = GNUNET_NETWORK_fdset_create ();
1757 wes = GNUNET_NETWORK_fdset_create ();
1758 wws = GNUNET_NETWORK_fdset_create ();
1760 GNUNET_assert (MHD_YES == MHD_get_fdset (daemon_handle, &rs, &ws, &es, &max));
1761 haveto = MHD_get_timeout (daemon_handle, &timeout);
1762 if (haveto == MHD_YES)
1764 if (timeout != last_timeout)
1767 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1768 "SELECT Timeout changed from %llu to %llu (ms)\n",
1769 last_timeout, timeout);
1770 last_timeout = timeout;
1772 if (timeout <= GNUNET_TIME_UNIT_SECONDS.rel_value_us / 1000LL)
1773 tv.rel_value_us = (uint64_t) timeout * 1000LL;
1775 tv = GNUNET_TIME_UNIT_SECONDS;
1778 tv = GNUNET_TIME_UNIT_SECONDS;
1779 /* Force immediate run, since we have outbound data to send */
1780 if (now == GNUNET_YES)
1781 tv = GNUNET_TIME_UNIT_MILLISECONDS;
1782 GNUNET_NETWORK_fdset_copy_native (wrs, &rs, max + 1);
1783 GNUNET_NETWORK_fdset_copy_native (wws, &ws, max + 1);
1784 GNUNET_NETWORK_fdset_copy_native (wes, &es, max + 1);
1786 if (daemon_handle == plugin->server_v4)
1788 if (plugin->server_v4_task != GNUNET_SCHEDULER_NO_TASK)
1790 GNUNET_SCHEDULER_cancel (plugin->server_v4_task);
1791 plugin->server_v4_task = GNUNET_SCHEDULER_NO_TASK;
1794 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1795 "Scheduling IPv4 server task in %llu ms\n", tv);
1798 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1800 &server_v4_run, plugin);
1802 if (daemon_handle == plugin->server_v6)
1804 if (plugin->server_v6_task != GNUNET_SCHEDULER_NO_TASK)
1806 GNUNET_SCHEDULER_cancel (plugin->server_v6_task);
1807 plugin->server_v6_task = GNUNET_SCHEDULER_NO_TASK;
1810 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1811 "Scheduling IPv6 server task in %llu ms\n", tv);
1814 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1816 &server_v6_run, plugin);
1818 GNUNET_NETWORK_fdset_destroy (wrs);
1819 GNUNET_NETWORK_fdset_destroy (wws);
1820 GNUNET_NETWORK_fdset_destroy (wes);
1827 * Load ssl certificate from file
1829 * @param file filename
1830 * @return content of the file
1833 server_load_file (const char *file)
1835 struct GNUNET_DISK_FileHandle *gn_file;
1839 if (GNUNET_OK != GNUNET_DISK_file_size (file,
1840 &fsize, GNUNET_NO, GNUNET_YES))
1842 text = GNUNET_malloc (fsize + 1);
1844 GNUNET_DISK_file_open (file, GNUNET_DISK_OPEN_READ,
1845 GNUNET_DISK_PERM_USER_READ);
1846 if (gn_file == NULL)
1851 if (GNUNET_SYSERR == GNUNET_DISK_file_read (gn_file, text, fsize))
1854 GNUNET_DISK_file_close (gn_file);
1858 GNUNET_DISK_file_close (gn_file);
1866 * Load ssl certificate
1868 * @param plugin the plugin
1869 * @return GNUNET_OK on success, GNUNET_SYSERR on failure
1872 server_load_certificate (struct HTTP_Server_Plugin *plugin)
1874 int res = GNUNET_OK;
1880 /* Get crypto init string from config
1881 * If not present just use default values */
1884 GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg,
1889 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
1890 "Failed to get servicehome!\n");
1891 return GNUNET_SYSERR;
1896 GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg,
1899 &plugin->crypto_init))
1900 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1901 "Using crypto init string `%s'\n",
1902 plugin->crypto_init);
1904 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1905 "Using default crypto init string \n");
1908 GNUNET_CONFIGURATION_get_value_filename (plugin->env->cfg, plugin->name,
1909 "KEY_FILE", &key_file))
1912 GNUNET_asprintf (&key_file, "%s/%s", sh, "https_key.key");
1917 GNUNET_CONFIGURATION_get_value_filename (plugin->env->cfg, plugin->name,
1918 "CERT_FILE", &cert_file))
1921 GNUNET_asprintf (&cert_file, "%s/%s", sh, "https_cert.crt");
1924 /* read key & certificates from file */
1925 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1926 "Trying to loading TLS certificate from key-file `%s' cert-file`%s'\n",
1927 key_file, cert_file);
1929 plugin->key = server_load_file (key_file);
1930 plugin->cert = server_load_file (cert_file);
1932 if ((plugin->key == NULL) || (plugin->cert == NULL))
1934 struct GNUNET_OS_Process *cert_creation;
1936 GNUNET_free_non_null (plugin->key);
1938 GNUNET_free_non_null (plugin->cert);
1939 plugin->cert = NULL;
1941 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1942 "No usable TLS certificate found, creating certificate\n");
1945 GNUNET_OS_start_process (GNUNET_NO, GNUNET_OS_INHERIT_STD_OUT_AND_ERR, NULL, NULL,
1946 "gnunet-transport-certificate-creation",
1947 "gnunet-transport-certificate-creation",
1948 key_file, cert_file, NULL);
1949 if (cert_creation == NULL)
1951 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
1953 ("Could not create a new TLS certificate, program `gnunet-transport-certificate-creation' could not be started!\n"));
1954 GNUNET_free (key_file);
1955 GNUNET_free (cert_file);
1957 GNUNET_free_non_null (plugin->key);
1959 GNUNET_free_non_null (plugin->cert);
1960 plugin->cert = NULL;
1961 GNUNET_free_non_null (plugin->crypto_init);
1962 plugin->crypto_init = NULL;
1964 return GNUNET_SYSERR;
1966 GNUNET_assert (GNUNET_OK == GNUNET_OS_process_wait (cert_creation));
1967 GNUNET_OS_process_destroy (cert_creation);
1969 plugin->key = server_load_file (key_file);
1970 plugin->cert = server_load_file (cert_file);
1973 if ((plugin->key == NULL) || (plugin->cert == NULL))
1975 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
1977 ("No usable TLS certificate found and creating one failed!\n"),
1979 GNUNET_free (key_file);
1980 GNUNET_free (cert_file);
1982 GNUNET_free_non_null (plugin->key);
1984 GNUNET_free_non_null (plugin->cert);
1985 plugin->cert = NULL;
1986 GNUNET_free_non_null (plugin->crypto_init);
1987 plugin->crypto_init = NULL;
1989 return GNUNET_SYSERR;
1991 GNUNET_free (key_file);
1992 GNUNET_free (cert_file);
1993 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "TLS certificate loaded\n");
2000 * Start the HTTP server
2002 * @param plugin the plugin handle
2003 * @return GNUNET_OK on success, GNUNET_SYSERR on failure
2006 server_start (struct HTTP_Server_Plugin *plugin)
2008 unsigned int timeout;
2010 GNUNET_assert (NULL != plugin);
2013 if (GNUNET_SYSERR == server_load_certificate (plugin))
2015 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
2016 "Could not load or create server certificate! Loading plugin failed!\n");
2017 return GNUNET_SYSERR;
2022 #if MHD_VERSION >= 0x00090E00
2023 timeout = HTTP_SERVER_NOT_VALIDATED_TIMEOUT.rel_value_us / 1000LL / 1000LL;
2024 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2025 "MHD can set timeout per connection! Default time out %u sec.\n",
2028 timeout = SERVER_SESSION_TIMEOUT.rel_value_us / 1000LL / 1000LL;
2029 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, plugin->name,
2030 "MHD cannot set timeout per connection! Default time out %u sec.\n",
2034 plugin->server_v4 = NULL;
2035 if (plugin->use_ipv4 == GNUNET_YES)
2037 plugin->server_v4 = MHD_start_daemon (
2044 MHD_NO_FLAG, plugin->port,
2045 &server_accept_cb, plugin,
2046 &server_access_cb, plugin,
2047 MHD_OPTION_SOCK_ADDR,
2048 (struct sockaddr_in *)
2049 plugin->server_addr_v4,
2050 MHD_OPTION_CONNECTION_LIMIT,
2052 plugin->max_connections,
2054 MHD_OPTION_HTTPS_PRIORITIES,
2055 plugin->crypto_init,
2056 MHD_OPTION_HTTPS_MEM_KEY,
2058 MHD_OPTION_HTTPS_MEM_CERT,
2061 MHD_OPTION_CONNECTION_TIMEOUT,
2063 MHD_OPTION_CONNECTION_MEMORY_LIMIT,
2065 GNUNET_SERVER_MAX_MESSAGE_SIZE),
2066 MHD_OPTION_NOTIFY_COMPLETED,
2067 &server_disconnect_cb, plugin,
2068 MHD_OPTION_EXTERNAL_LOGGER,
2069 server_log, NULL, MHD_OPTION_END);
2070 if (plugin->server_v4 == NULL)
2072 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
2073 "Failed to start %s IPv4 server component on port %u\n",
2074 plugin->name, plugin->port);
2077 server_reschedule (plugin, plugin->server_v4, GNUNET_NO);
2081 plugin->server_v6 = NULL;
2082 if (plugin->use_ipv6 == GNUNET_YES)
2084 plugin->server_v6 = MHD_start_daemon (
2091 MHD_USE_IPv6, plugin->port,
2092 &server_accept_cb, plugin,
2093 &server_access_cb, plugin,
2094 MHD_OPTION_SOCK_ADDR,
2095 (struct sockaddr_in6 *)
2096 plugin->server_addr_v6,
2097 MHD_OPTION_CONNECTION_LIMIT,
2099 plugin->max_connections,
2101 MHD_OPTION_HTTPS_PRIORITIES,
2102 plugin->crypto_init,
2103 MHD_OPTION_HTTPS_MEM_KEY,
2105 MHD_OPTION_HTTPS_MEM_CERT,
2108 MHD_OPTION_CONNECTION_TIMEOUT,
2110 MHD_OPTION_CONNECTION_MEMORY_LIMIT,
2112 GNUNET_SERVER_MAX_MESSAGE_SIZE),
2113 MHD_OPTION_NOTIFY_COMPLETED,
2114 &server_disconnect_cb, plugin,
2115 MHD_OPTION_EXTERNAL_LOGGER,
2116 server_log, NULL, MHD_OPTION_END);
2117 if (plugin->server_v6 == NULL)
2119 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
2120 "Failed to start %s IPv6 server component on port %u\n",
2121 plugin->name, plugin->port);
2124 server_reschedule (plugin, plugin->server_v6, GNUNET_NO);
2128 if ((plugin->server_v6 == NULL) && (plugin->server_v4 == NULL))
2130 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
2131 "%s %s server component started on port %u\n",
2132 msg, plugin->name, plugin->port);
2134 return GNUNET_SYSERR;
2136 else if ((plugin->server_v6 != NULL) && (plugin->server_v4 != NULL))
2137 msg = "IPv4 and IPv6";
2138 else if (plugin->server_v6 != NULL)
2140 else if (plugin->server_v4 != NULL)
2142 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2143 "%s %s server component started on port %u\n",
2144 msg, plugin->name, plugin->port);
2150 server_stop (struct HTTP_Server_Plugin *plugin)
2152 if (plugin->server_v4 != NULL)
2154 MHD_stop_daemon (plugin->server_v4);
2155 plugin->server_v4 = NULL;
2157 if ( plugin->server_v6 != NULL)
2159 MHD_stop_daemon (plugin->server_v6);
2160 plugin->server_v6 = NULL;
2164 if (plugin->server_v4_task != GNUNET_SCHEDULER_NO_TASK)
2166 GNUNET_SCHEDULER_cancel (plugin->server_v4_task);
2167 plugin->server_v4_task = GNUNET_SCHEDULER_NO_TASK;
2170 if (plugin->server_v6_task != GNUNET_SCHEDULER_NO_TASK)
2172 GNUNET_SCHEDULER_cancel (plugin->server_v6_task);
2173 plugin->server_v6_task = GNUNET_SCHEDULER_NO_TASK;
2178 GNUNET_free_non_null (plugin->crypto_init);
2179 GNUNET_free_non_null (plugin->cert);
2180 GNUNET_free_non_null (plugin->key);
2183 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2184 "%s server component stopped\n", plugin->name);
2189 * Add an address to the server's set of addresses and notify transport
2191 * @param cls the plugin handle
2192 * @param add_remove GNUNET_YES on add, GNUNET_NO on remove
2193 * @param addr the address
2194 * @param addrlen address length
2197 server_add_address (void *cls, int add_remove, const struct sockaddr *addr,
2200 struct HTTP_Server_Plugin *plugin = cls;
2201 struct HttpAddressWrapper *w = NULL;
2203 w = GNUNET_malloc (sizeof (struct HttpAddressWrapper));
2204 w->address = http_common_address_from_socket (plugin->protocol, addr, addrlen);
2205 if (NULL == w->address)
2210 w->addrlen = http_common_address_get_size (w->address);
2212 GNUNET_CONTAINER_DLL_insert(plugin->addr_head, plugin->addr_tail, w);
2213 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2214 "Notifying transport to add address `%s'\n",
2215 http_common_plugin_address_to_string (NULL, p->protocol, w->address, w->addrlen));
2217 plugin->env->notify_address (plugin->env->cls, add_remove, w->address, w->addrlen, "https_client");
2219 plugin->env->notify_address (plugin->env->cls, add_remove, w->address, w->addrlen, "http_client");
2225 * Remove an address from the server's set of addresses and notify transport
2227 * @param cls the plugin handle
2228 * @param add_remove GNUNET_YES on add, GNUNET_NO on remove
2229 * @param addr the address
2230 * @param addrlen address length
2233 server_remove_address (void *cls, int add_remove, const struct sockaddr *addr,
2236 struct HTTP_Server_Plugin *plugin = cls;
2237 struct HttpAddressWrapper *w = plugin->addr_head;
2239 void * saddr = http_common_address_from_socket (plugin->protocol, addr, addrlen);
2242 saddr_len = http_common_address_get_size (saddr);
2246 if (GNUNET_YES == http_common_cmp_addresses(w->address, w->addrlen, saddr, saddr_len))
2250 GNUNET_free (saddr);
2255 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2256 "Notifying transport to remove address `%s'\n",
2257 http_common_plugin_address_to_string (NULL, p->protocol, w->address, w->addrlen));
2258 GNUNET_CONTAINER_DLL_remove (plugin->addr_head, plugin->addr_tail, w);
2260 plugin->env->notify_address (plugin->env->cls, add_remove, w->address, w->addrlen, "https_client");
2262 plugin->env->notify_address (plugin->env->cls, add_remove, w->address, w->addrlen, "http_client");
2264 GNUNET_free (w->address);
2271 * Our external IP address/port mapping has changed.
2273 * @param cls closure, the 'struct LocalAddrList'
2274 * @param add_remove GNUNET_YES to mean the new public IP address, GNUNET_NO to mean
2275 * the previous (now invalid) one
2276 * @param addr either the previous or the new public IP address
2277 * @param addrlen actual lenght of the address
2280 server_nat_port_map_callback (void *cls, int add_remove, const struct sockaddr *addr,
2283 GNUNET_assert (cls != NULL);
2284 struct HTTP_Server_Plugin *plugin = cls;
2286 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2287 "NAT called to %s address `%s'\n",
2288 (add_remove == GNUNET_NO) ? "remove" : "add",
2289 GNUNET_a2s (addr, addrlen));
2291 if (AF_INET == addr->sa_family)
2293 struct sockaddr_in *s4 = (struct sockaddr_in *) addr;
2295 if (GNUNET_NO == plugin->use_ipv4)
2298 if ((NULL != plugin->server_addr_v4) &&
2299 (0 != memcmp (&plugin->server_addr_v4->sin_addr,
2300 &s4->sin_addr, sizeof (struct in_addr))))
2302 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2303 "Skipping address `%s' (not bindto address)\n",
2304 GNUNET_a2s (addr, addrlen));
2309 if (AF_INET6 == addr->sa_family)
2311 struct sockaddr_in6 *s6 = (struct sockaddr_in6 *) addr;
2312 if (GNUNET_NO == plugin->use_ipv6)
2315 if ((NULL != plugin->server_addr_v6) &&
2316 (0 != memcmp (&plugin->server_addr_v6->sin6_addr,
2317 &s6->sin6_addr, sizeof (struct in6_addr))))
2319 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2320 "Skipping address `%s' (not bindto address)\n",
2321 GNUNET_a2s (addr, addrlen));
2329 server_add_address (cls, add_remove, addr, addrlen);
2332 server_remove_address (cls, add_remove, addr, addrlen);
2339 * Get valid server addresses
2341 * @param plugin the plugin handle
2342 * @param serviceName the servicename
2343 * @param cfg configuration handle
2344 * @param addrs addresses
2345 * @param addr_lens address length
2346 * @return number of addresses
2349 server_get_addresses (struct HTTP_Server_Plugin *plugin,
2350 const char *serviceName,
2351 const struct GNUNET_CONFIGURATION_Handle *cfg,
2352 struct sockaddr ***addrs, socklen_t ** addr_lens)
2355 unsigned long long port;
2356 struct addrinfo hints;
2357 struct addrinfo *res;
2358 struct addrinfo *pos;
2359 struct addrinfo *next;
2363 struct sockaddr **saddrs;
2364 socklen_t *saddrlens;
2370 disablev6 = !plugin->use_ipv6;
2373 if (GNUNET_CONFIGURATION_have_value (cfg, serviceName, "PORT"))
2375 GNUNET_break (GNUNET_OK ==
2376 GNUNET_CONFIGURATION_get_value_number (cfg, serviceName,
2380 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2382 ("Require valid port number for service in configuration!\n"));
2383 return GNUNET_SYSERR;
2388 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, plugin->name,
2389 "Starting in listen only mode\n");
2390 return -1; /* listen only */
2394 if (GNUNET_CONFIGURATION_have_value (cfg, serviceName, "BINDTO"))
2396 GNUNET_break (GNUNET_OK ==
2397 GNUNET_CONFIGURATION_get_value_string (cfg, serviceName,
2398 "BINDTO", &hostname));
2403 if (hostname != NULL)
2405 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2406 "Resolving `%s' since that is where `%s' will bind to.\n",
2407 hostname, serviceName);
2408 memset (&hints, 0, sizeof (struct addrinfo));
2410 hints.ai_family = AF_INET;
2411 if ((0 != (ret = getaddrinfo (hostname, NULL, &hints, &res))) ||
2414 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Failed to resolve `%s': %s\n"),
2415 hostname, gai_strerror (ret));
2416 GNUNET_free (hostname);
2417 return GNUNET_SYSERR;
2421 while (NULL != (pos = next))
2423 next = pos->ai_next;
2424 if ((disablev6) && (pos->ai_family == AF_INET6))
2430 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2431 _("Failed to find %saddress for `%s'.\n"),
2432 disablev6 ? "IPv4 " : "", hostname);
2434 GNUNET_free (hostname);
2435 return GNUNET_SYSERR;
2438 saddrs = GNUNET_malloc ((resi + 1) * sizeof (struct sockaddr *));
2439 saddrlens = GNUNET_malloc ((resi + 1) * sizeof (socklen_t));
2442 while (NULL != (pos = next))
2444 next = pos->ai_next;
2445 if ((disablev6) && (pos->ai_family == AF_INET6))
2447 if ((pos->ai_protocol != IPPROTO_TCP) && (pos->ai_protocol != 0))
2448 continue; /* not TCP */
2449 if ((pos->ai_socktype != SOCK_STREAM) && (pos->ai_socktype != 0))
2450 continue; /* huh? */
2451 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2452 "Service will bind to `%s'\n", GNUNET_a2s (pos->ai_addr,
2454 if (pos->ai_family == AF_INET)
2456 GNUNET_assert (pos->ai_addrlen == sizeof (struct sockaddr_in));
2457 saddrlens[i] = pos->ai_addrlen;
2458 saddrs[i] = GNUNET_malloc (saddrlens[i]);
2459 memcpy (saddrs[i], pos->ai_addr, saddrlens[i]);
2460 ((struct sockaddr_in *) saddrs[i])->sin_port = htons (port);
2464 GNUNET_assert (pos->ai_family == AF_INET6);
2465 GNUNET_assert (pos->ai_addrlen == sizeof (struct sockaddr_in6));
2466 saddrlens[i] = pos->ai_addrlen;
2467 saddrs[i] = GNUNET_malloc (saddrlens[i]);
2468 memcpy (saddrs[i], pos->ai_addr, saddrlens[i]);
2469 ((struct sockaddr_in6 *) saddrs[i])->sin6_port = htons (port);
2473 GNUNET_free (hostname);
2479 /* will bind against everything, just set port */
2485 saddrs = GNUNET_malloc ((resi + 1) * sizeof (struct sockaddr *));
2486 saddrlens = GNUNET_malloc ((resi + 1) * sizeof (socklen_t));
2488 saddrlens[i] = sizeof (struct sockaddr_in);
2489 saddrs[i] = GNUNET_malloc (saddrlens[i]);
2490 #if HAVE_SOCKADDR_IN_SIN_LEN
2491 ((struct sockaddr_in *) saddrs[i])->sin_len = saddrlens[i];
2493 ((struct sockaddr_in *) saddrs[i])->sin_family = AF_INET;
2494 ((struct sockaddr_in *) saddrs[i])->sin_port = htons (port);
2500 saddrs = GNUNET_malloc ((resi + 1) * sizeof (struct sockaddr *));
2501 saddrlens = GNUNET_malloc ((resi + 1) * sizeof (socklen_t));
2503 saddrlens[i] = sizeof (struct sockaddr_in6);
2504 saddrs[i] = GNUNET_malloc (saddrlens[i]);
2505 #if HAVE_SOCKADDR_IN_SIN_LEN
2506 ((struct sockaddr_in6 *) saddrs[i])->sin6_len = saddrlens[0];
2508 ((struct sockaddr_in6 *) saddrs[i])->sin6_family = AF_INET6;
2509 ((struct sockaddr_in6 *) saddrs[i])->sin6_port = htons (port);
2511 saddrlens[i] = sizeof (struct sockaddr_in);
2512 saddrs[i] = GNUNET_malloc (saddrlens[i]);
2513 #if HAVE_SOCKADDR_IN_SIN_LEN
2514 ((struct sockaddr_in *) saddrs[i])->sin_len = saddrlens[1];
2516 ((struct sockaddr_in *) saddrs[i])->sin_family = AF_INET;
2517 ((struct sockaddr_in *) saddrs[i])->sin_port = htons (port);
2521 *addr_lens = saddrlens;
2527 * Ask NAT for addresses
2529 * @param plugin the plugin handle
2532 server_start_report_addresses (struct HTTP_Server_Plugin *plugin)
2534 int res = GNUNET_OK;
2535 struct sockaddr **addrs;
2536 socklen_t *addrlens;
2538 res = server_get_addresses (plugin,
2539 plugin->name, plugin->env->cfg,
2541 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2542 _("Found %u addresses to report to NAT service\n"), res);
2544 if (GNUNET_SYSERR == res)
2551 GNUNET_NAT_register (plugin->env->cfg, GNUNET_YES, plugin->port,
2553 (const struct sockaddr **) addrs, addrlens,
2554 &server_nat_port_map_callback, NULL, plugin);
2558 GNUNET_assert (addrs[res] != NULL);
2559 GNUNET_free (addrs[res]);
2561 GNUNET_free_non_null (addrs);
2562 GNUNET_free_non_null (addrlens);
2567 * Stop NAT for addresses
2569 * @param plugin the plugin handle
2572 server_stop_report_addresses (struct HTTP_Server_Plugin *plugin)
2574 /* Stop NAT handle */
2575 if (NULL != plugin->nat)
2576 GNUNET_NAT_unregister (plugin->nat);
2578 /* Clean up addresses */
2579 struct HttpAddressWrapper *w;
2581 while (plugin->addr_head != NULL)
2583 w = plugin->addr_head;
2584 GNUNET_CONTAINER_DLL_remove (plugin->addr_head, plugin->addr_tail, w);
2585 GNUNET_free (w->address);
2592 * Check if IPv6 supported on this system
2594 * @param plugin the plugin handle
2595 * @return GNUNET_YES on success, else GNUNET_NO
2598 server_check_ipv6_support (struct HTTP_Server_Plugin *plugin)
2600 struct GNUNET_NETWORK_Handle *desc = NULL;
2601 int res = GNUNET_NO;
2603 /* Probe IPv6 support */
2604 desc = GNUNET_NETWORK_socket_create (PF_INET6, SOCK_STREAM, 0);
2607 if ((errno == ENOBUFS) || (errno == ENOMEM) || (errno == ENFILE) ||
2610 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "socket");
2612 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, plugin->name,
2614 ("Disabling IPv6 since it is not supported on this system!\n"));
2619 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (desc));
2623 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2624 "Testing IPv6 on this system: %s\n",
2625 (res == GNUNET_YES) ? "successful" : "failed");
2631 * Notify server about our external hostname
2634 * @param tc task context (unused)
2637 server_notify_external_hostname (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2639 struct HTTP_Server_Plugin *plugin = cls;
2643 plugin->notify_ext_task = GNUNET_SCHEDULER_NO_TASK;
2644 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
2647 GNUNET_asprintf(&url, "%s://%s", plugin->protocol, plugin->external_hostname);
2649 urlen = strlen (url) + 1;
2650 plugin->ext_addr = GNUNET_malloc (sizeof (struct HttpAddress) + urlen);
2651 plugin->ext_addr->options = htonl(plugin->options);
2652 plugin->ext_addr->urlen = htonl (urlen);
2653 plugin->ext_addr_len = sizeof (struct HttpAddress) + urlen;
2654 memcpy (&plugin->ext_addr[1], url, urlen);
2656 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2657 "Notifying transport about external hostname address `%s'\n", plugin->external_hostname);
2660 if (GNUNET_YES == plugin->verify_external_hostname)
2661 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, plugin->name,
2662 "Enabling SSL verification for external hostname address `%s'\n", plugin->external_hostname);
2663 plugin->env->notify_address (plugin->env->cls, GNUNET_YES,
2664 plugin->ext_addr, plugin->ext_addr_len,
2667 plugin->env->notify_address (plugin->env->cls, GNUNET_YES,
2668 plugin->ext_addr, plugin->ext_addr_len,
2675 * Configure the plugin
2677 * @param plugin plugin handle
2678 * @return GNUNET_OK on success, GNUNET_SYSERR on failure
2681 server_configure_plugin (struct HTTP_Server_Plugin *plugin)
2683 unsigned long long port;
2684 unsigned long long max_connections;
2685 char *bind4_address = NULL;
2686 char *bind6_address = NULL;
2687 char *eh_tmp = NULL;
2688 int external_hostname_use_port;
2691 if (GNUNET_CONFIGURATION_have_value
2692 (plugin->env->cfg, plugin->name, "USE_IPv4"))
2695 GNUNET_CONFIGURATION_get_value_yesno (plugin->env->cfg, plugin->name,
2699 plugin->use_ipv4 = GNUNET_YES;
2700 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2701 _("IPv4 support is %s\n"),
2702 (plugin->use_ipv4 == GNUNET_YES) ? "enabled" : "disabled");
2705 if (GNUNET_CONFIGURATION_have_value
2706 (plugin->env->cfg, plugin->name, "USE_IPv6"))
2709 GNUNET_CONFIGURATION_get_value_yesno (plugin->env->cfg, plugin->name,
2713 plugin->use_ipv6 = GNUNET_YES;
2714 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2715 _("IPv6 support is %s\n"),
2716 (plugin->use_ipv6 == GNUNET_YES) ? "enabled" : "disabled");
2718 if ((plugin->use_ipv4 == GNUNET_NO) && (plugin->use_ipv6 == GNUNET_NO))
2720 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
2722 ("Neither IPv4 nor IPv6 are enabled! Fix in configuration\n"),
2724 return GNUNET_SYSERR;
2727 /* Reading port number from config file */
2729 GNUNET_CONFIGURATION_get_value_number (plugin->env->cfg, plugin->name,
2730 "PORT", &port)) || (port > 65535))
2732 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
2733 _("Port is required! Fix in configuration\n"),
2735 return GNUNET_SYSERR;
2737 plugin->port = port;
2739 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, plugin->name,
2740 _("Using port %u\n"), plugin->port);
2742 if ((plugin->use_ipv4 == GNUNET_YES) &&
2743 (GNUNET_YES == GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg,
2744 plugin->name, "BINDTO", &bind4_address)))
2746 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2747 "Binding %s plugin to specific IPv4 address: `%s'\n",
2748 plugin->protocol, bind4_address);
2749 plugin->server_addr_v4 = GNUNET_malloc (sizeof (struct sockaddr_in));
2750 if (1 != inet_pton (AF_INET, bind4_address,
2751 &plugin->server_addr_v4->sin_addr))
2753 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
2755 ("Specific IPv4 address `%s' in configuration file is invalid!\n"),
2757 GNUNET_free (bind4_address);
2758 GNUNET_free (plugin->server_addr_v4);
2759 plugin->server_addr_v4 = NULL;
2760 return GNUNET_SYSERR;
2764 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2765 _("Binding to IPv4 address %s\n"), bind4_address);
2766 plugin->server_addr_v4->sin_family = AF_INET;
2767 plugin->server_addr_v4->sin_port = htons (plugin->port);
2769 GNUNET_free (bind4_address);
2772 if ((plugin->use_ipv6 == GNUNET_YES) &&
2774 GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg, plugin->name,
2775 "BINDTO6", &bind6_address)))
2777 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2778 "Binding %s plugin to specific IPv6 address: `%s'\n",
2779 plugin->protocol, bind6_address);
2780 plugin->server_addr_v6 = GNUNET_malloc (sizeof (struct sockaddr_in6));
2782 inet_pton (AF_INET6, bind6_address, &plugin->server_addr_v6->sin6_addr))
2784 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
2786 ("Specific IPv6 address `%s' in configuration file is invalid!\n"),
2788 GNUNET_free (bind6_address);
2789 GNUNET_free (plugin->server_addr_v6);
2790 plugin->server_addr_v6 = NULL;
2791 return GNUNET_SYSERR;
2795 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2796 _("Binding to IPv6 address %s\n"), bind6_address);
2797 plugin->server_addr_v6->sin6_family = AF_INET6;
2798 plugin->server_addr_v6->sin6_port = htons (plugin->port);
2800 GNUNET_free (bind6_address);
2803 plugin->verify_external_hostname = GNUNET_NO;
2805 plugin->verify_external_hostname = GNUNET_CONFIGURATION_get_value_yesno (plugin->env->cfg, plugin->name,
2806 "VERIFY_EXTERNAL_HOSTNAME");
2807 if (GNUNET_SYSERR == plugin->verify_external_hostname)
2808 plugin->verify_external_hostname = GNUNET_NO;
2809 if (GNUNET_YES == plugin->verify_external_hostname)
2810 plugin->options |= HTTP_OPTIONS_VERIFY_CERTIFICATE;
2812 external_hostname_use_port = GNUNET_CONFIGURATION_get_value_yesno (plugin->env->cfg, plugin->name,
2813 "EXTERNAL_HOSTNAME_USE_PORT");
2814 if (GNUNET_SYSERR == external_hostname_use_port)
2815 external_hostname_use_port = GNUNET_NO;
2818 if (GNUNET_YES == GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg, plugin->name,
2819 "EXTERNAL_HOSTNAME", &eh_tmp))
2823 char * pos_url = NULL;
2825 if (NULL != strstr(eh_tmp, "://"))
2827 tmp = &strstr(eh_tmp, "://")[3];
2832 if (GNUNET_YES == external_hostname_use_port)
2834 if ( (strlen (tmp) > 1) && (NULL != (pos = strchr(tmp, '/'))) )
2838 GNUNET_asprintf (&plugin->external_hostname, "%s:%u/%s", tmp, (uint16_t) port, (NULL == pos_url) ? "" : pos_url);
2841 GNUNET_asprintf (&plugin->external_hostname, "%s:%u", tmp, (uint16_t) port);
2844 plugin->external_hostname = GNUNET_strdup (tmp);
2845 GNUNET_free (eh_tmp);
2847 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, plugin->name,
2848 _("Using external hostname `%s'\n"), plugin->external_hostname);
2849 plugin->notify_ext_task = GNUNET_SCHEDULER_add_now (&server_notify_external_hostname, plugin);
2851 /* Use only configured external hostname */
2852 if (GNUNET_CONFIGURATION_have_value
2853 (plugin->env->cfg, plugin->name, "EXTERNAL_HOSTNAME_ONLY"))
2855 plugin->external_only =
2856 GNUNET_CONFIGURATION_get_value_yesno (plugin->env->cfg, plugin->name,
2857 "EXTERNAL_HOSTNAME_ONLY");
2860 plugin->external_only = GNUNET_NO;
2862 if (GNUNET_YES == plugin->external_only)
2863 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2864 _("Notifying transport only about hostname `%s'\n"), plugin->external_hostname);
2867 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2868 "No external hostname configured\n");
2870 /* Optional parameters */
2871 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (plugin->env->cfg,
2873 "MAX_CONNECTIONS", &max_connections))
2874 max_connections = 128;
2875 plugin->max_connections = max_connections;
2877 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2878 _("Maximum number of connections is %u\n"),
2879 plugin->max_connections);
2882 plugin->peer_id_length = strlen (GNUNET_h2s_full (&plugin->env->my_identity->hashPubKey));
2889 * Session was idle, so disconnect it
2891 * @param cls the session
2892 * @param tc task context
2895 server_session_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2897 GNUNET_assert (NULL != cls);
2898 struct Session *s = cls;
2900 s->timeout_task = GNUNET_SCHEDULER_NO_TASK;
2901 GNUNET_log (TIMEOUT_LOG,
2902 "Session %p was idle for %s, disconnecting\n",
2904 GNUNET_STRINGS_relative_time_to_string (SERVER_SESSION_TIMEOUT,
2907 /* call session destroy function */
2908 GNUNET_assert (GNUNET_OK == server_disconnect (s));
2913 * Start session timeout for session s
2915 * @param s the session
2918 server_start_session_timeout (struct Session *s)
2920 GNUNET_assert (NULL != s);
2921 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == s->timeout_task);
2922 s->timeout_task = GNUNET_SCHEDULER_add_delayed (SERVER_SESSION_TIMEOUT,
2923 &server_session_timeout,
2925 GNUNET_log (TIMEOUT_LOG,
2926 "Timeout for session %p set to %s\n",
2928 GNUNET_STRINGS_relative_time_to_string (SERVER_SESSION_TIMEOUT,
2934 * Increment session timeout due to activity session s
2936 * @param s the session
2939 server_reschedule_session_timeout (struct Session *s)
2941 GNUNET_assert (NULL != s);
2942 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK != s->timeout_task);
2944 GNUNET_SCHEDULER_cancel (s->timeout_task);
2945 s->timeout_task = GNUNET_SCHEDULER_add_delayed (SERVER_SESSION_TIMEOUT,
2946 &server_session_timeout,
2948 GNUNET_log (TIMEOUT_LOG,
2949 "Timeout rescheduled for session %p set to %s\n",
2951 GNUNET_STRINGS_relative_time_to_string (SERVER_SESSION_TIMEOUT,
2957 * Exit point from the plugin.
2963 LIBGNUNET_PLUGIN_TRANSPORT_DONE (void *cls)
2965 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
2966 struct HTTP_Server_Plugin *plugin = api->cls;
2967 struct Session *pos;
2968 struct Session *next;
2970 if (NULL == api->cls)
2972 /* Free for stub mode */
2976 plugin->in_shutdown = GNUNET_YES;
2977 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2978 _("Shutting down plugin `%s'\n"),
2981 if (GNUNET_SCHEDULER_NO_TASK != plugin->notify_ext_task)
2983 GNUNET_SCHEDULER_cancel (plugin->notify_ext_task);
2984 plugin->notify_ext_task = GNUNET_SCHEDULER_NO_TASK;
2987 if (NULL != plugin->ext_addr)
2989 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2990 "Notifying transport to remove address `%s'\n",
2991 http_common_plugin_address_to_string (NULL,
2994 plugin->ext_addr_len));
2996 plugin->env->notify_address (plugin->env->cls,
2999 plugin->ext_addr_len,
3002 plugin->env->notify_address (plugin->env->cls,
3005 plugin->ext_addr_len,
3011 /* Stop to report addresses to transport service */
3012 server_stop_report_addresses (plugin);
3013 server_stop (plugin);
3014 next = plugin->head;
3015 while (NULL != (pos = next))
3018 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
3019 "Removing left over session %p\n", pos);
3021 if ((GNUNET_YES == pos->session_passed) && (GNUNET_NO == pos->session_ended))
3023 /* Notify transport immediately that this session is invalid */
3024 pos->session_ended = GNUNET_YES;
3025 plugin->env->session_end (plugin->env->cls, &pos->target, pos);
3028 server_delete_session (pos);
3032 GNUNET_free_non_null (plugin->external_hostname);
3033 GNUNET_free_non_null (plugin->ext_addr);
3034 GNUNET_free_non_null (plugin->server_addr_v4);
3035 GNUNET_free_non_null (plugin->server_addr_v6);
3037 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
3038 _("Shutdown for plugin `%s' complete\n"),
3041 GNUNET_free (plugin);
3046 const char *http_plugin_address_to_string (void *cls,
3051 return http_common_plugin_address_to_string (cls, PLUGIN_NAME, addr, addrlen);
3053 return http_common_plugin_address_to_string (cls, PLUGIN_NAME, addr, addrlen);
3060 * Function obtain the network type for a session
3062 * @param cls closure ('struct Plugin*')
3063 * @param session the session
3064 * @return the network type in HBO or GNUNET_SYSERR
3066 static enum GNUNET_ATS_Network_Type
3067 http_server_get_network (void *cls,
3068 struct Session *session)
3070 GNUNET_assert (NULL != session);
3071 return ntohl (session->ats_address_network_type);
3076 * Entry point for the plugin.
3082 LIBGNUNET_PLUGIN_TRANSPORT_INIT (void *cls)
3084 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
3085 struct GNUNET_TRANSPORT_PluginFunctions *api;
3086 struct HTTP_Server_Plugin *plugin;
3088 plugin = GNUNET_malloc (sizeof (struct HTTP_Server_Plugin));
3092 if (NULL == env->receive)
3094 /* run in 'stub' mode (i.e. as part of gnunet-peerinfo), don't fully
3095 initialze the plugin or the API */
3096 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
3098 api->address_to_string = &http_plugin_address_to_string;
3099 api->string_to_address = &http_common_plugin_string_to_address;
3100 api->address_pretty_printer = &http_common_plugin_address_pretty_printer;
3104 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
3106 api->send = &http_server_plugin_send;
3107 api->disconnect = &http_server_plugin_disconnect;
3108 api->check_address = &http_server_plugin_address_suggested;
3109 api->get_session = &http_server_plugin_get_session;
3111 api->address_to_string = &http_plugin_address_to_string;
3112 api->string_to_address = &http_common_plugin_string_to_address;
3113 api->address_pretty_printer = &http_common_plugin_address_pretty_printer;
3114 api->get_network = &http_server_get_network;
3117 plugin->name = "transport-https_server";
3118 plugin->protocol = "https";
3120 plugin->name = "transport-http_server";
3121 plugin->protocol = "http";
3124 /* Configure plugin */
3125 if (GNUNET_SYSERR == server_configure_plugin (plugin))
3127 LIBGNUNET_PLUGIN_TRANSPORT_DONE (api);
3131 /* Check IPv6 support */
3132 if (GNUNET_YES == plugin->use_ipv6)
3133 plugin->use_ipv6 = server_check_ipv6_support (plugin);
3135 /* Report addresses to transport service */
3136 if (GNUNET_NO == plugin->external_only)
3137 server_start_report_addresses (plugin);
3139 if (GNUNET_SYSERR == server_start (plugin))
3141 LIBGNUNET_PLUGIN_TRANSPORT_DONE (api);
3147 /* end of plugin_transport_http_server.c */