2 This file is part of GNUnet
3 (C) 2002-2014 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
25 * @author David Barksdale
28 #include "gnunet_util_lib.h"
29 #include "gnunet_statistics_service.h"
30 #include "gnunet_transport_plugin.h"
31 #include "gnunet_nat_lib.h"
32 #include "plugin_transport_http_common.h"
33 #include <microhttpd.h>
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>"
54 * Session handle for connections.
59 * Stored in a linked list.
64 * Stored in a linked list.
69 * To whom are we talking to (set to our identity
70 * if we are still waiting for the welcome message)
72 struct GNUNET_PeerIdentity target;
75 * Pointer to the global plugin struct.
77 struct HTTP_Server_Plugin *plugin;
80 * next pointer for double linked list
82 struct HTTP_Message *msg_head;
85 * previous pointer for double linked list
87 struct HTTP_Message *msg_tail;
90 * Message stream tokenizer for incoming data
92 struct GNUNET_SERVER_MessageStreamTokenizer *msg_tk;
97 struct ServerConnection *server_recv;
102 struct ServerConnection *server_send;
107 struct GNUNET_HELLO_Address *address;
110 * Unique HTTP/S connection tag for this connection
115 * ATS network type in NBO
117 uint32_t ats_address_network_type;
120 * Was session given to transport service?
125 * Did we immediately end the session in disconnect_cb
130 * Absolute time when to receive data again
131 * Used for receive throttling
133 struct GNUNET_TIME_Absolute next_receive;
136 * Session timeout task
138 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
141 * Should this session get disconnected? GNUNET_YES/NO
147 struct ServerConnection
155 * For PUT connections: Is this the first or last callback with size 0
156 * For GET connections: Have we sent a message
161 * The session this server connection belongs to
163 struct Session *session;
168 struct MHD_Connection *mhd_conn;
173 struct MHD_Daemon *mhd_daemon;
176 * Options requested by peer
179 #define OPTION_LONG_POLL 1 /* GET request wants long-poll semantics */
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.
196 struct Session *head;
199 * Linked list tail of open sessions.
201 struct Session *tail;
214 * My options to be included in the address
221 char *external_hostname;
224 * Verify external address
226 int verify_external_hostname;
229 * Maximum number of sockets the plugin can use
230 * Each http inbound /outbound connections are two connections
232 unsigned int max_connections;
235 * Current number of sockets the plugin can use
236 * Each http inbound /outbound connections are two connections
238 unsigned int cur_connections;
241 * Did we immediately end the session in disconnect_cb
251 * External hostname the plugin can be connected to, can be different to
252 * the host's FQDN, used e.g. for reverse proxying
254 struct GNUNET_HELLO_Address *ext_addr;
257 * Notify transport only about external address
259 unsigned int external_only;
277 * Task calling transport service about external address
279 GNUNET_SCHEDULER_TaskIdentifier notify_ext_task;
282 * NAT handle & address management
284 struct GNUNET_NAT_Handle *nat;
287 * List of own addresses
291 * IPv4 addresses DLL head
293 struct HttpAddressWrapper *addr_head;
296 * IPv4 addresses DLL tail
298 struct HttpAddressWrapper *addr_tail;
301 * IPv4 server socket to bind to
303 struct sockaddr_in *server_addr_v4;
306 * IPv6 server socket to bind to
308 struct sockaddr_in6 *server_addr_v6;
313 GNUNET_SCHEDULER_TaskIdentifier server_v4_task;
318 GNUNET_SCHEDULER_TaskIdentifier server_v6_task;
321 * The IPv4 server is scheduled to run asap
323 int server_v4_immediately;
326 * The IPv6 server is scheduled to run asap
328 int server_v6_immediately;
333 struct MHD_Daemon *server_v4;
338 struct MHD_Daemon *server_v6;
341 * Regex for parsing URLs
351 * Use RC4-128 instead of AES:
352 * NONE:+VERS-TLS1.0:+ARCFOUR-128:+SHA1:+RSA:+COMP-NULL
372 * Wrapper to manage addresses
374 struct HttpAddressWrapper
379 struct HttpAddressWrapper *next;
382 * Linked list previous
384 struct HttpAddressWrapper *prev;
386 struct HttpAddress *address;
393 * Message to send using http
398 * next pointer for double linked list
400 struct HTTP_Message *next;
403 * previous pointer for double linked list
405 struct HTTP_Message *prev;
408 * buffer containing data to send
413 * amount of data already sent
423 * HTTP/S specific overhead
428 * Continuation function to call once the transmission buffer
429 * has again space available. NULL if there is no
430 * continuation to call.
432 GNUNET_TRANSPORT_TransmitContinuation transmit_cont;
435 * Closure for transmit_cont.
437 void *transmit_cont_cls;
442 * Start session timeout for session s
443 * @param s the session
446 server_start_session_timeout (struct Session *s);
450 * Increment session timeout due to activity for session s
451 * @param s the session
454 server_reschedule_session_timeout (struct Session *s);
458 * Cancel timeout for session s
459 * @param s the session
462 server_stop_session_timeout (struct Session *s);
466 * Disconnect session @a s
468 * @param cls closure with the `struct HTTP_Server_Plugin`
469 * @param s the session
470 * @return #GNUNET_OK on success
473 http_server_plugin_disconnect_session (void *cls,
478 * Does session s exist?
480 * @param plugin the plugin handle
481 * @param s the session
482 * @return #GNUNET_YES on success, #GNUNET_NO on error
485 server_exist_session (struct HTTP_Server_Plugin *plugin, struct Session *s);
489 * Reschedule the execution of both IPv4 and IPv6 server
490 * @param plugin the plugin
491 * @param server which server to schedule v4 or v6?
492 * @param now #GNUNET_YES to schedule execution immediately, #GNUNET_NO to wait
496 server_reschedule (struct HTTP_Server_Plugin *plugin,
497 struct MHD_Daemon *server,
502 * Function that can be used by the transport service to transmit
503 * a message using the plugin. Note that in the case of a
504 * peer disconnecting, the continuation MUST be called
505 * prior to the disconnect notification itself. This function
506 * will be called with this peer's HELLO message to initiate
507 * a fresh connection to another peer.
510 * @param session which session must be used
511 * @param msgbuf the message to transmit
512 * @param msgbuf_size number of bytes in @a msgbuf
513 * @param priority how important is the message (most plugins will
514 * ignore message priority and just FIFO)
515 * @param to how long to wait at most for the transmission (does not
516 * require plugins to discard the message after the timeout,
517 * just advisory for the desired delay; most plugins will ignore
519 * @param cont continuation to call once the message has
520 * been transmitted (or if the transport is ready
521 * for the next transmission call; or if the
522 * peer disconnected...); can be NULL
523 * @param cont_cls closure for @a cont
524 * @return number of bytes used (on the physical network, with overheads);
525 * -1 on hard errors (i.e. address invalid); 0 is a legal value
526 * and does NOT mean that the message was not transmitted (DV)
529 http_server_plugin_send (void *cls,
530 struct Session *session,
531 const char *msgbuf, size_t msgbuf_size,
532 unsigned int priority,
533 struct GNUNET_TIME_Relative to,
534 GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
536 struct HTTP_Server_Plugin *plugin = cls;
537 struct HTTP_Message *msg;
541 GNUNET_assert (plugin != NULL);
542 GNUNET_assert (session != NULL);
544 if (GNUNET_NO == server_exist_session (plugin, session))
547 return GNUNET_SYSERR;
549 if (session->disconnect)
550 return GNUNET_SYSERR;
553 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, session->plugin->name,
554 "Session %p/connection %p: Sending message with %u to peer `%s' with \n",
555 session, session->server_send,
556 msgbuf_size, GNUNET_i2s (&session->target));
558 /* create new message and schedule */
559 bytes_sent = sizeof (struct HTTP_Message) + msgbuf_size;
560 msg = GNUNET_malloc (bytes_sent);
562 msg->size = msgbuf_size;
564 msg->buf = (char *) &msg[1];
565 msg->transmit_cont = cont;
566 msg->transmit_cont_cls = cont_cls;
567 memcpy (msg->buf, msgbuf, msgbuf_size);
569 GNUNET_CONTAINER_DLL_insert_tail (session->msg_head, session->msg_tail, msg);
571 GNUNET_asprintf (&stat_txt, "# bytes currently in %s_server buffers", plugin->protocol);
572 GNUNET_STATISTICS_update (plugin->env->stats,
573 stat_txt, msgbuf_size, GNUNET_NO);
574 GNUNET_free (stat_txt);
576 if (NULL != session->server_send)
578 server_reschedule (session->plugin,
579 session->server_send->mhd_daemon,
587 * Function that can be used to force the plugin to disconnect
588 * from the given peer and cancel all previous transmissions
589 * (and their continuationc).
592 * @param target peer from which to disconnect
595 http_server_plugin_disconnect_peer (void *cls,
596 const struct GNUNET_PeerIdentity *target)
598 struct HTTP_Server_Plugin *plugin = cls;
599 struct Session *next;
602 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
603 "Transport tells me to disconnect `%s'\n",
604 GNUNET_i2s (target));
606 while (NULL != (pos = next))
609 if (0 == memcmp (target, &pos->target, sizeof (struct GNUNET_PeerIdentity)))
611 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
612 "Disconnecting session %p to `%s'\n",
613 pos, GNUNET_i2s (target));
614 http_server_plugin_disconnect_session (plugin, pos);
621 * Another peer has suggested an address for this
622 * peer and transport plugin. Check that this could be a valid
623 * address. If so, consider adding it to the list
627 * @param addr pointer to the address
628 * @param addrlen length of @a addr
629 * @return #GNUNET_OK if this is a plausible address for this peer
633 http_server_plugin_address_suggested (void *cls,
637 struct HTTP_Server_Plugin *plugin = cls;
638 struct HttpAddressWrapper *next;
639 struct HttpAddressWrapper *pos;
640 const struct HttpAddress *haddr = addr;
642 if ((NULL != plugin->ext_addr) &&
643 GNUNET_YES == (http_common_cmp_addresses (addr, addrlen,
644 plugin->ext_addr->address,
645 plugin->ext_addr->address_length)))
647 /* Checking HTTP_OPTIONS_VERIFY_CERTIFICATE option for external hostname */
648 if ((ntohl (haddr->options) & HTTP_OPTIONS_VERIFY_CERTIFICATE) !=
649 (plugin->options & HTTP_OPTIONS_VERIFY_CERTIFICATE))
650 return GNUNET_NO; /* VERIFY option not set as required! */
653 next = plugin->addr_head;
654 while (NULL != (pos = next))
657 if (GNUNET_YES == (http_common_cmp_addresses(addr,
668 * Creates a new outbound session the transport
669 * service will use to send data to the peer
671 * Since HTTP/S server cannot create sessions, always return NULL
673 * @param cls the plugin
674 * @param address the address
675 * @return always NULL
677 static struct Session *
678 http_server_plugin_get_session (void *cls,
679 const struct GNUNET_HELLO_Address *address)
686 * Deleting the session
687 * Must not be used afterwards
689 * @param cls closure with the `struct HTTP_ServerPlugin`
690 * @param s the session to delete
691 * @return #GNUNET_OK on success
694 server_delete_session (void *cls,
697 struct HTTP_Server_Plugin *plugin = cls;
698 struct HTTP_Message *msg;
699 struct HTTP_Message *tmp;
701 server_stop_session_timeout(s);
702 GNUNET_CONTAINER_DLL_remove (plugin->head, plugin->tail, s);
707 GNUNET_CONTAINER_DLL_remove (s->msg_head, s->msg_tail, msg);
708 if (NULL != msg->transmit_cont)
709 msg->transmit_cont (msg->transmit_cont_cls, &s->target, GNUNET_SYSERR,
710 msg->size, msg->pos + msg->overhead);
714 if (NULL != s->msg_tk)
716 GNUNET_SERVER_mst_destroy (s->msg_tk);
719 GNUNET_HELLO_address_free (s->address);
720 GNUNET_free_non_null (s->server_recv);
721 GNUNET_free_non_null (s->server_send);
722 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
724 "Session %p destroyed\n", s);
731 * Cancel timeout for session s
733 * @param s the session
736 server_stop_session_timeout (struct Session *s)
738 GNUNET_assert (NULL != s);
740 if (GNUNET_SCHEDULER_NO_TASK != s->timeout_task)
742 GNUNET_SCHEDULER_cancel (s->timeout_task);
743 s->timeout_task = GNUNET_SCHEDULER_NO_TASK;
744 GNUNET_log (TIMEOUT_LOG, "Timeout stopped for session %p\n", s);
750 * Function that queries MHD's select sets and
751 * starts the task waiting for them.
752 * @param plugin plugin
753 * @param daemon_handle the MHD daemon handle
754 * @param now schedule immediately
755 * @return task identifier
757 static GNUNET_SCHEDULER_TaskIdentifier
758 server_schedule (struct HTTP_Server_Plugin *plugin,
759 struct MHD_Daemon *daemon_handle,
764 * Reschedule the execution of both IPv4 and IPv6 server
765 * @param plugin the plugin
766 * @param server which server to schedule v4 or v6?
767 * @param now #GNUNET_YES to schedule execution immediately, #GNUNET_NO to wait
771 server_reschedule (struct HTTP_Server_Plugin *plugin,
772 struct MHD_Daemon *server,
775 if ((server == plugin->server_v4) && (plugin->server_v4 != NULL))
777 if (GNUNET_YES == plugin->server_v4_immediately)
778 return; /* No rescheduling, server will run asap */
780 if (GNUNET_YES == now)
781 plugin->server_v4_immediately = GNUNET_YES;
783 if (plugin->server_v4_task != GNUNET_SCHEDULER_NO_TASK)
785 GNUNET_SCHEDULER_cancel (plugin->server_v4_task);
786 plugin->server_v4_task = GNUNET_SCHEDULER_NO_TASK;
788 plugin->server_v4_task = server_schedule (plugin, plugin->server_v4, now);
791 if ((server == plugin->server_v6) && (plugin->server_v6 != NULL))
793 if (GNUNET_YES == plugin->server_v6_immediately)
794 return; /* No rescheduling, server will run asap */
796 if (GNUNET_YES == now)
797 plugin->server_v6_immediately = GNUNET_YES;
799 if (plugin->server_v6_task != GNUNET_SCHEDULER_NO_TASK)
801 GNUNET_SCHEDULER_cancel (plugin->server_v6_task);
802 plugin->server_v6_task = GNUNET_SCHEDULER_NO_TASK;
804 plugin->server_v6_task = server_schedule (plugin, plugin->server_v6, now);
810 * Disconnect session @a s
812 * @param cls closure with the `struct HTTP_Server_Plugin`
813 * @param s the session
814 * @return #GNUNET_OK on success
817 http_server_plugin_disconnect_session (void *cls,
820 struct HTTP_Server_Plugin *plugin = cls;
821 struct ServerConnection * send;
822 struct ServerConnection * recv;
824 if (GNUNET_NO == server_exist_session (plugin, s))
827 return GNUNET_SYSERR;
829 s->disconnect = GNUNET_YES;
830 send = (struct ServerConnection *) s->server_send;
833 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, s->plugin->name,
834 "Server: %p / %p Terminating inbound PUT session to peer `%s'\n",
835 s, send, GNUNET_i2s (&s->target));
837 MHD_set_connection_option (send->mhd_conn,
838 MHD_CONNECTION_OPTION_TIMEOUT,
840 server_reschedule (s->plugin, send->mhd_daemon, GNUNET_YES);
843 recv = (struct ServerConnection *) s->server_recv;
846 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, s->plugin->name,
847 "Server: %p / %p Terminating inbound GET session to peer `%s'\n",
848 s, recv, GNUNET_i2s (&s->target));
850 MHD_set_connection_option (recv->mhd_conn,
851 MHD_CONNECTION_OPTION_TIMEOUT,
853 server_reschedule (s->plugin, recv->mhd_daemon, GNUNET_YES);
860 * Function that is called to get the keepalive factor.
861 * GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT is divided by this number to
862 * calculate the interval between keepalive packets.
864 * @param cls closure with the `struct HTTP_Server_Plugin`
865 * @return keepalive factor
868 http_server_query_keepalive_factor (void *cls)
874 http_server_plugin_update_session_timeout (void *cls,
875 const struct GNUNET_PeerIdentity *peer,
876 struct Session *session)
878 struct HTTP_Server_Plugin *plugin = cls;
880 if (GNUNET_NO == server_exist_session (plugin, session))
883 server_reschedule_session_timeout (session);
888 * Tell MHD that the connection should timeout after @a to seconds.
890 * @param plugin our plugin
891 * @param s session for which the timeout changes
892 * @param to timeout in seconds
895 server_mhd_connection_timeout (struct HTTP_Server_Plugin *plugin,
899 /* Setting timeouts for other connections */
900 if (NULL != s->server_recv)
902 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
903 "Setting timeout for %p to %u sec.\n",
905 MHD_set_connection_option (s->server_recv->mhd_conn,
906 MHD_CONNECTION_OPTION_TIMEOUT,
908 server_reschedule (plugin, s->server_recv->mhd_daemon, GNUNET_NO);
910 if (NULL != s->server_send)
912 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
913 "Setting timeout for %p to %u sec.\n",
915 MHD_set_connection_option (s->server_send->mhd_conn,
916 MHD_CONNECTION_OPTION_TIMEOUT,
918 server_reschedule (plugin, s->server_send->mhd_daemon, GNUNET_NO);
924 * Parse incoming URL for tag and target
926 * @param plugin plugin
927 * @param url incoming url
928 * @param target where to store the target
929 * @param tag where to store the tag
930 * @param options where to store the options
931 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
934 server_parse_url (struct HTTP_Server_Plugin *plugin,
936 struct GNUNET_PeerIdentity *target,
940 regmatch_t matches[4];
941 const char *tag_start;
942 const char *target_start;
946 unsigned long int rc;
950 ("^.*/([0-9A-V]+);([0-9]+)(,[0-9]+)?$")
955 return GNUNET_SYSERR;
958 if (regexec(&plugin->url_regex, url, 4, matches, 0))
960 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
961 "URL `%s' did not match regex\n", url);
962 return GNUNET_SYSERR;
965 target_start = &url[matches[1].rm_so];
966 tag_start = &url[matches[2].rm_so];
969 rc = strtoul (tag_start, &tag_end, 10);
970 if (&url[matches[2].rm_eo] != tag_end)
972 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
973 "URL tag did not line up with submatch\n");
974 return GNUNET_SYSERR;
978 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
979 "URL tag is zero\n");
980 return GNUNET_SYSERR;
982 if ((rc == ULONG_MAX) && (ERANGE == errno))
984 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
985 "URL tag > ULONG_MAX\n");
986 return GNUNET_SYSERR;
990 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
991 "URL tag > UINT32_MAX\n");
992 return GNUNET_SYSERR;
994 (*tag) = (uint32_t)rc;
995 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
996 "Found tag `%u' in url\n", *tag);
998 /* convert peer id */
999 hash_length = matches[1].rm_eo - matches[1].rm_so;
1000 if (hash_length != plugin->peer_id_length)
1002 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1003 "URL target is %u bytes, expecting %u\n",
1004 hash_length, plugin->peer_id_length);
1005 return GNUNET_SYSERR;
1008 GNUNET_CRYPTO_eddsa_public_key_from_string (target_start,
1010 &target->public_key))
1012 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1013 "URL target conversion failed\n");
1014 return GNUNET_SYSERR;
1016 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1018 "Found target `%s' in URL\n",
1019 GNUNET_i2s_full (target));
1021 /* convert options */
1022 if (-1 == matches[3].rm_so) {
1025 rc = strtoul (&url[matches[3].rm_so + 1], &options_end, 10);
1026 if (&url[matches[3].rm_eo] != options_end)
1028 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1029 "URL options did not line up with submatch\n");
1030 return GNUNET_SYSERR;
1032 if ((rc == ULONG_MAX) && (ERANGE == errno))
1034 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1035 "URL options > ULONG_MAX\n");
1036 return GNUNET_SYSERR;
1038 if (rc > UINT32_MAX)
1040 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1041 "URL options > UINT32_MAX\n");
1042 return GNUNET_SYSERR;
1044 (*options) = (uint32_t)rc;
1045 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1046 "Found options `%u' in url\n", *options);
1053 * Lookup a mhd connection and create one if none is found
1055 * @param plugin the plugin handle
1056 * @param mhd_connection the incoming mhd_connection
1057 * @param url incoming requested URL
1058 * @param method PUT or GET
1059 * @return the server connecetion
1061 static struct ServerConnection *
1062 server_lookup_connection (struct HTTP_Server_Plugin *plugin,
1063 struct MHD_Connection *mhd_connection, const char *url,
1066 struct Session *s = NULL;
1067 struct ServerConnection *sc = NULL;
1068 const union MHD_ConnectionInfo *conn_info;
1069 struct HttpAddress *addr;
1071 struct GNUNET_ATS_Information ats;
1072 struct GNUNET_PeerIdentity target;
1076 int direction = GNUNET_SYSERR;
1079 conn_info = MHD_get_connection_info (mhd_connection,
1080 MHD_CONNECTION_INFO_CLIENT_ADDRESS);
1081 if ((conn_info->client_addr->sa_family != AF_INET) &&
1082 (conn_info->client_addr->sa_family != AF_INET6))
1084 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1085 "New %s connection from %s\n", method, url);
1087 if (GNUNET_SYSERR == server_parse_url (plugin, url, &target, &tag, &options))
1089 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1090 "Invalid url %s\n", url);
1093 if (0 == strcmp (MHD_HTTP_METHOD_PUT, method))
1094 direction = _RECEIVE;
1095 else if (0 == strcmp (MHD_HTTP_METHOD_GET, method))
1099 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1100 "Invalid method %s connection from %s\n", method, url);
1104 plugin->cur_connections++;
1105 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1106 "New %s connection from %s with tag %u (%u of %u)\n",
1108 GNUNET_i2s (&target), tag,
1109 plugin->cur_connections, plugin->max_connections);
1110 /* find duplicate session */
1114 if ((0 == memcmp (&s->target, &target, sizeof (struct GNUNET_PeerIdentity))) &&
1121 if ((_RECEIVE == direction) && (NULL != s->server_recv))
1123 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1124 "Duplicate PUT connection from `%s' tag %u, dismissing new connection\n",
1125 GNUNET_i2s (&target),
1130 if ((_SEND == direction) && (NULL != s->server_send))
1132 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1133 "Duplicate GET connection from `%s' tag %u, dismissing new connection\n",
1134 GNUNET_i2s (&target),
1141 /* create new session */
1143 switch (conn_info->client_addr->sa_family)
1146 addr = http_common_address_from_socket (plugin->protocol, conn_info->client_addr, sizeof (struct sockaddr_in));
1147 addr_len = http_common_address_get_size (addr);
1148 ats = plugin->env->get_address_type (plugin->env->cls, conn_info->client_addr, sizeof (struct sockaddr_in));
1151 addr = http_common_address_from_socket (plugin->protocol, conn_info->client_addr, sizeof (struct sockaddr_in6));
1152 addr_len = http_common_address_get_size (addr);
1153 ats = plugin->env->get_address_type (plugin->env->cls, conn_info->client_addr, sizeof (struct sockaddr_in6));
1156 /* external host name */
1157 ats.type = htonl (GNUNET_ATS_NETWORK_TYPE);
1158 ats.type = htonl (GNUNET_ATS_NET_WAN);
1162 s = GNUNET_new (struct Session);
1163 memcpy (&s->target, &target, sizeof (struct GNUNET_PeerIdentity));
1165 s->address = GNUNET_HELLO_address_allocate (&s->target, PLUGIN_NAME,
1166 addr, addr_len, GNUNET_HELLO_ADDRESS_INFO_INBOUND);
1167 s->ats_address_network_type = ats.value;
1168 s->next_receive = GNUNET_TIME_UNIT_ZERO_ABS;
1170 s->server_recv = NULL;
1171 s->server_send = NULL;
1172 s->session_passed = GNUNET_NO;
1173 s->session_ended = GNUNET_NO;
1174 server_start_session_timeout(s);
1175 GNUNET_CONTAINER_DLL_insert (plugin->head, plugin->tail, s);
1177 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1178 "Creating new session %p for peer `%s' connecting from `%s'\n",
1179 s, GNUNET_i2s (&target),
1180 http_common_plugin_address_to_string (NULL,
1183 GNUNET_free_non_null (addr);
1185 sc = GNUNET_new (struct ServerConnection);
1186 if (conn_info->client_addr->sa_family == AF_INET)
1187 sc->mhd_daemon = plugin->server_v4;
1188 if (conn_info->client_addr->sa_family == AF_INET6)
1189 sc->mhd_daemon = plugin->server_v6;
1190 sc->mhd_conn = mhd_connection;
1191 sc->direction = direction;
1192 sc->connected = GNUNET_NO;
1194 sc->options = options;
1195 if (direction == _SEND)
1196 s->server_send = sc;
1197 if (direction == _RECEIVE)
1198 s->server_recv = sc;
1200 if ((NULL != s->server_send) && (NULL != s->server_recv))
1202 plugin->env->session_start (NULL, s->address ,s, NULL, 0);
1205 if ((NULL == s->server_recv) || (NULL == s->server_send))
1207 to = (HTTP_SERVER_NOT_VALIDATED_TIMEOUT.rel_value_us / 1000LL / 1000LL);
1208 MHD_set_connection_option (mhd_connection,
1209 MHD_CONNECTION_OPTION_TIMEOUT, to);
1210 server_reschedule (plugin, sc->mhd_daemon, GNUNET_NO);
1214 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1215 "Session %p for peer `%s' fully connected\n",
1216 s, GNUNET_i2s (&target));
1217 to = (HTTP_SERVER_SESSION_TIMEOUT.rel_value_us / 1000LL / 1000LL);
1218 server_mhd_connection_timeout (plugin, s, to);
1221 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1222 "Setting timeout for %p to %u sec.\n", sc, to);
1228 * Lookup a session for a server connection
1230 * @param plugin the plugin
1231 * @param sc the server connection
1232 * @return the session found or NULL
1234 static struct Session *
1235 server_lookup_session (struct HTTP_Server_Plugin *plugin,
1236 struct ServerConnection * sc)
1240 for (s = plugin->head; NULL != s; s = s->next)
1241 if ((s->server_recv == sc) || (s->server_send == sc))
1248 server_exist_session (struct HTTP_Server_Plugin *plugin,
1251 struct Session * head;
1253 for (head = plugin->head; head != NULL; head = head->next)
1261 * Callback called by MHD when it needs data to send
1263 * @param cls current session
1264 * @param pos position in buffer
1265 * @param buf the buffer to write data to
1266 * @param max max number of bytes available in buffer
1267 * @return bytes written to buffer
1270 server_send_callback (void *cls, uint64_t pos, char *buf, size_t max)
1272 struct Session *s = cls;
1273 struct ServerConnection *sc;
1274 ssize_t bytes_read = 0;
1275 struct HTTP_Message *msg;
1278 if (GNUNET_NO == server_exist_session (s->plugin, s))
1280 sc = s->server_send;
1287 bytes_read = GNUNET_MIN (msg->size - msg->pos,
1289 memcpy (buf, &msg->buf[msg->pos], bytes_read);
1290 msg->pos += bytes_read;
1292 /* removing message */
1293 if (msg->pos == msg->size)
1295 GNUNET_CONTAINER_DLL_remove (s->msg_head, s->msg_tail, msg);
1296 if (NULL != msg->transmit_cont)
1297 msg->transmit_cont (msg->transmit_cont_cls, &s->target, GNUNET_OK,
1298 msg->size, msg->size + msg->overhead);
1304 sc->connected = GNUNET_YES;
1305 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, s->plugin->name,
1306 "Sent %u bytes to peer `%s' with session %p \n", bytes_read, GNUNET_i2s (&s->target), s);
1307 GNUNET_asprintf (&stat_txt, "# bytes currently in %s_server buffers",
1308 s->plugin->protocol);
1309 GNUNET_STATISTICS_update (s->plugin->env->stats,
1310 stat_txt, -bytes_read, GNUNET_NO);
1311 GNUNET_free (stat_txt);
1312 GNUNET_asprintf (&stat_txt, "# bytes transmitted via %s_server",
1313 s->plugin->protocol);
1314 GNUNET_STATISTICS_update (s->plugin->env->stats,
1315 stat_txt, bytes_read, GNUNET_NO);
1316 GNUNET_free (stat_txt);
1317 } else if ((sc->options & OPTION_LONG_POLL) && sc->connected) {
1318 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, s->plugin->name,
1319 "Completing GET response to peer `%s' with session %p \n",
1320 GNUNET_i2s (&s->target), s);
1321 return MHD_CONTENT_READER_END_OF_STREAM;
1328 * Callback called by MessageStreamTokenizer when a message has arrived
1330 * @param cls current session as closure
1331 * @param client client
1332 * @param message the message to be forwarded to transport service
1333 * @return #GNUNET_OK
1336 server_receive_mst_cb (void *cls, void *client,
1337 const struct GNUNET_MessageHeader *message)
1339 struct Session *s = cls;
1340 struct HTTP_Server_Plugin *plugin = s->plugin;
1341 struct GNUNET_ATS_Information atsi;
1342 struct GNUNET_TIME_Relative delay;
1345 if (GNUNET_NO == server_exist_session (s->plugin, s))
1349 atsi.type = htonl (GNUNET_ATS_NETWORK_TYPE);
1350 atsi.value = s->ats_address_network_type;
1351 GNUNET_break (s->ats_address_network_type != ntohl (GNUNET_ATS_NET_UNSPECIFIED));
1353 delay = plugin->env->receive (plugin->env->cls, s->address, s, message);
1354 plugin->env->update_address_metrics (plugin->env->cls, s->address, s, &atsi, 1);
1356 GNUNET_asprintf (&stat_txt, "# bytes received via %s_server", plugin->protocol);
1357 GNUNET_STATISTICS_update (plugin->env->stats,
1358 stat_txt, ntohs (message->size), GNUNET_NO);
1359 GNUNET_free (stat_txt);
1361 s->session_passed = GNUNET_YES;
1362 s->next_receive = GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (), delay);
1363 if (delay.rel_value_us > 0)
1365 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1366 "Peer `%s' address `%s' next read delayed for %s\n",
1367 GNUNET_i2s (&s->target),
1368 http_common_plugin_address_to_string (NULL,
1369 plugin->protocol, s->address->address,
1370 s->address->address_length),
1371 GNUNET_STRINGS_relative_time_to_string (delay,
1374 server_reschedule_session_timeout (s);
1379 * Add headers to a request indicating that we allow Cross-Origin Resource
1383 add_cors_headers(struct MHD_Response *response)
1385 MHD_add_response_header (response,
1386 "Access-Control-Allow-Origin",
1388 MHD_add_response_header (response,
1389 "Access-Control-Allow-Methods",
1390 "GET, PUT, OPTIONS");
1391 MHD_add_response_header (response,
1392 "Access-Control-Max-Age",
1397 * MHD callback for a new incoming connection
1399 * @param cls the plugin handle
1400 * @param mhd_connection the mhd connection
1401 * @param url the requested URL
1402 * @param method GET or PUT
1403 * @param version HTTP version
1404 * @param upload_data upload data
1405 * @param upload_data_size sizeof upload data
1406 * @param httpSessionCache the session cache to remember the connection
1407 * @return MHD_YES if connection is accepted, MHD_NO on reject
1410 server_access_cb (void *cls, struct MHD_Connection *mhd_connection,
1411 const char *url, const char *method, const char *version,
1412 const char *upload_data, size_t * upload_data_size,
1413 void **httpSessionCache)
1415 struct HTTP_Server_Plugin *plugin = cls;
1418 struct ServerConnection *sc = *httpSessionCache;
1420 struct MHD_Response *response;
1422 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1423 _("Access from connection %p (%u of %u) for `%s' `%s' url `%s' with upload data size %u\n"),
1425 plugin->cur_connections, plugin->max_connections,
1426 method, version, url, (*upload_data_size));
1428 GNUNET_assert (cls != NULL);
1431 /* CORS pre-flight request */
1432 if (0 == strcmp (MHD_HTTP_METHOD_OPTIONS, method))
1434 response = MHD_create_response_from_buffer (0, NULL,
1435 MHD_RESPMEM_PERSISTENT);
1436 add_cors_headers(response);
1437 res = MHD_queue_response (mhd_connection, MHD_HTTP_OK, response);
1438 MHD_destroy_response (response);
1441 /* new connection */
1442 sc = server_lookup_connection (plugin, mhd_connection, url, method);
1445 (*httpSessionCache) = sc;
1449 response = MHD_create_response_from_data (strlen (HTTP_ERROR_RESPONSE), HTTP_ERROR_RESPONSE, MHD_NO, MHD_NO);
1450 MHD_add_response_header (response,
1451 MHD_HTTP_HEADER_CONTENT_TYPE,
1453 add_cors_headers(response);
1454 res = MHD_queue_response (mhd_connection, MHD_HTTP_NOT_FOUND, response);
1455 MHD_destroy_response (response);
1461 /* 'old' connection */
1462 if (NULL == server_lookup_session (plugin, sc))
1464 /* Session was already disconnected */
1469 /* existing connection */
1470 sc = (*httpSessionCache);
1472 GNUNET_assert (NULL != s);
1473 /* connection is to be disconnected */
1474 if (s->disconnect == GNUNET_YES)
1476 /* Sent HTTP/1.1: 200 OK as response */
1477 response = MHD_create_response_from_data (strlen ("Thank you!"),
1480 add_cors_headers(response);
1481 MHD_queue_response (mhd_connection, MHD_HTTP_OK, response);
1482 MHD_destroy_response (response);
1485 GNUNET_assert (s != NULL);
1487 if (sc->direction == _SEND)
1489 response = MHD_create_response_from_callback (MHD_SIZE_UNKNOWN,
1491 &server_send_callback, s,
1493 add_cors_headers(response);
1494 MHD_queue_response (mhd_connection, MHD_HTTP_OK, response);
1495 MHD_destroy_response (response);
1498 if (sc->direction == _RECEIVE)
1500 if ((*upload_data_size == 0) && (sc->connected == GNUNET_NO))
1502 /* (*upload_data_size == 0) first callback when header are passed */
1503 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1504 "Session %p / Connection %p: Peer `%s' PUT on address `%s' connected\n",
1506 GNUNET_i2s (&s->target),
1507 http_common_plugin_address_to_string (NULL,
1508 plugin->protocol, s->address->address,
1509 s->address->address_length));
1510 sc->connected = GNUNET_YES;
1513 else if ((*upload_data_size == 0) && (sc->connected == GNUNET_YES))
1515 /* (*upload_data_size == 0) when upload is complete */
1516 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1517 "Session %p / Connection %p: Peer `%s' PUT on address `%s' finished upload\n",
1519 GNUNET_i2s (&s->target),
1520 http_common_plugin_address_to_string (NULL,
1521 plugin->protocol, s->address->address,
1522 s->address->address_length));
1523 sc->connected = GNUNET_NO;
1524 /* Sent HTTP/1.1: 200 OK as PUT Response\ */
1525 response = MHD_create_response_from_data (strlen ("Thank you!"),
1528 add_cors_headers(response);
1529 MHD_queue_response (mhd_connection, MHD_HTTP_OK, response);
1530 MHD_destroy_response (response);
1533 else if ((*upload_data_size > 0) && (sc->connected == GNUNET_YES))
1535 /* (*upload_data_size > 0) for every segment received */
1536 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1537 "Session %p / Connection %p: Peer `%s' PUT on address `%s' received %u bytes\n",
1539 GNUNET_i2s (&s->target),
1540 http_common_plugin_address_to_string (NULL,
1541 plugin->protocol, s->address->address,
1542 s->address->address_length),
1544 struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get ();
1546 if ((s->next_receive.abs_value_us <= now.abs_value_us))
1548 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1549 "PUT with %u bytes forwarded to MST\n",
1551 if (s->msg_tk == NULL)
1553 s->msg_tk = GNUNET_SERVER_mst_create (&server_receive_mst_cb, s);
1555 GNUNET_SERVER_mst_receive (s->msg_tk, s, upload_data,
1556 *upload_data_size, GNUNET_NO, GNUNET_NO);
1557 server_mhd_connection_timeout (plugin, s,
1558 GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT.rel_value_us / 1000LL / 1000LL);
1559 (*upload_data_size) = 0;
1563 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1564 "Session %p / Connection %p: no inbound bandwidth available! Next read was delayed by %s\n",
1566 GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_duration (s->next_receive),
1582 * Callback from MHD when a connection disconnects
1584 * @param cls closure with the `struct HTTP_Server_Plugin *`
1585 * @param connection the disconnected MHD connection
1586 * @param httpSessionCache the pointer to distinguish
1589 server_disconnect_cb (void *cls, struct MHD_Connection *connection,
1590 void **httpSessionCache)
1592 struct HTTP_Server_Plugin *plugin = cls;
1593 struct ServerConnection *sc = *httpSessionCache;
1594 struct Session *s = NULL;
1595 struct Session *t = NULL;
1597 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1599 "Disconnect for connection %p \n", sc);
1604 if (NULL == (s = server_lookup_session (plugin, sc)))
1606 for (t = plugin->head; t != NULL; t = t->next)
1612 if (sc->direction == _SEND)
1614 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1615 "Peer `%s' connection %p, GET on address `%s' disconnected\n",
1616 GNUNET_i2s (&s->target), s->server_send,
1617 http_common_plugin_address_to_string (NULL,
1618 plugin->protocol, s->address->address,
1619 s->address->address_length));
1620 s->server_send = NULL;
1621 if (!(sc->options & OPTION_LONG_POLL) && NULL != (s->server_recv))
1623 s->disconnect = GNUNET_YES;
1624 GNUNET_assert (NULL != s->server_recv->mhd_conn);
1625 #if MHD_VERSION >= 0x00090E00
1626 MHD_set_connection_option (s->server_recv->mhd_conn, MHD_CONNECTION_OPTION_TIMEOUT,
1629 server_reschedule (plugin, s->server_recv->mhd_daemon, GNUNET_NO);
1632 if (sc->direction == _RECEIVE)
1634 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1635 "Peer `%s' connection %p PUT on address `%s' disconnected\n",
1636 GNUNET_i2s (&s->target), s->server_recv,
1637 http_common_plugin_address_to_string (NULL,
1638 plugin->protocol, s->address->address,
1639 s->address->address_length));
1640 s->server_recv = NULL;
1641 if (s->msg_tk != NULL)
1643 GNUNET_SERVER_mst_destroy (s->msg_tk);
1649 plugin->cur_connections--;
1651 if (s->disconnect && (s->server_send == NULL) && (s->server_recv == NULL))
1653 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1654 "Peer `%s' on address `%s' disconnected\n",
1655 GNUNET_i2s (&s->target),
1656 http_common_plugin_address_to_string (NULL,
1657 plugin->protocol, s->address->address,
1658 s->address->address_length));
1660 if ((GNUNET_YES == s->session_passed) && (GNUNET_NO == s->session_ended))
1662 /* Notify transport immediately that this session is invalid */
1663 s->session_ended = GNUNET_YES;
1664 plugin->env->session_end (plugin->env->cls, s->address, s);
1666 server_delete_session (plugin, s);
1672 * Check if incoming connection is accepted.
1674 * @param cls plugin as closure
1675 * @param addr address of incoming connection
1676 * @param addr_len address length of incoming connection
1677 * @return MHD_YES if connection is accepted, MHD_NO if connection is rejected
1680 server_accept_cb (void *cls, const struct sockaddr *addr, socklen_t addr_len)
1682 struct HTTP_Server_Plugin *plugin = cls;
1684 if (plugin->cur_connections <= plugin->max_connections)
1686 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1687 _("Accepting connection (%u of %u) from `%s'\n"),
1688 plugin->cur_connections, plugin->max_connections,
1689 GNUNET_a2s (addr, addr_len));
1694 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, plugin->name,
1695 _("Server reached maximum number connections (%u), rejecting new connection\n"),
1696 plugin->max_connections);
1703 server_log (void *arg, const char *fmt, va_list ap)
1707 vsnprintf (text, sizeof (text), fmt, ap);
1709 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Server: %s\n", text);
1714 * Call MHD IPv4 to process pending requests and then go back
1715 * and schedule the next run.
1716 * @param cls plugin as closure
1717 * @param tc task context
1720 server_v4_run (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1722 struct HTTP_Server_Plugin *plugin = cls;
1724 GNUNET_assert (cls != NULL);
1726 plugin->server_v4_task = GNUNET_SCHEDULER_NO_TASK;
1727 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1730 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1731 "Running IPv4 server\n");
1733 plugin->server_v4_immediately = GNUNET_NO;
1734 GNUNET_assert (MHD_YES == MHD_run (plugin->server_v4));
1735 server_reschedule (plugin, plugin->server_v4, GNUNET_NO);
1740 * Call MHD IPv6 to process pending requests and then go back
1741 * and schedule the next run.
1742 * @param cls plugin as closure
1743 * @param tc task context
1746 server_v6_run (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1748 struct HTTP_Server_Plugin *plugin = cls;
1750 GNUNET_assert (cls != NULL);
1751 plugin->server_v6_task = GNUNET_SCHEDULER_NO_TASK;
1752 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1755 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1756 "Running IPv6 server\n");
1758 plugin->server_v6_immediately = GNUNET_NO;
1759 GNUNET_assert (MHD_YES == MHD_run (plugin->server_v6));
1760 server_reschedule (plugin, plugin->server_v6, GNUNET_NO);
1765 * Function that queries MHD's select sets and
1766 * starts the task waiting for them.
1768 * @param plugin plugin
1769 * @param daemon_handle the MHD daemon handle
1770 * @return gnunet task identifier
1772 static GNUNET_SCHEDULER_TaskIdentifier
1773 server_schedule (struct HTTP_Server_Plugin *plugin,
1774 struct MHD_Daemon *daemon_handle,
1777 GNUNET_SCHEDULER_TaskIdentifier ret;
1781 struct GNUNET_NETWORK_FDSet *wrs;
1782 struct GNUNET_NETWORK_FDSet *wws;
1783 struct GNUNET_NETWORK_FDSet *wes;
1785 MHD_UNSIGNED_LONG_LONG timeout;
1786 static unsigned long long last_timeout = 0;
1789 struct GNUNET_TIME_Relative tv;
1791 if (GNUNET_YES == plugin->in_shutdown)
1792 return GNUNET_SCHEDULER_NO_TASK;
1794 ret = GNUNET_SCHEDULER_NO_TASK;
1798 wrs = GNUNET_NETWORK_fdset_create ();
1799 wes = GNUNET_NETWORK_fdset_create ();
1800 wws = GNUNET_NETWORK_fdset_create ();
1802 GNUNET_assert (MHD_YES == MHD_get_fdset (daemon_handle, &rs, &ws, &es, &max));
1803 haveto = MHD_get_timeout (daemon_handle, &timeout);
1804 if (haveto == MHD_YES)
1806 if (timeout != last_timeout)
1809 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1810 "SELECT Timeout changed from %llu to %llu (ms)\n",
1811 last_timeout, timeout);
1812 last_timeout = timeout;
1814 if (timeout <= GNUNET_TIME_UNIT_SECONDS.rel_value_us / 1000LL)
1815 tv.rel_value_us = (uint64_t) timeout * 1000LL;
1817 tv = GNUNET_TIME_UNIT_SECONDS;
1820 tv = GNUNET_TIME_UNIT_SECONDS;
1821 /* Force immediate run, since we have outbound data to send */
1822 if (now == GNUNET_YES)
1823 tv = GNUNET_TIME_UNIT_MILLISECONDS;
1824 GNUNET_NETWORK_fdset_copy_native (wrs, &rs, max + 1);
1825 GNUNET_NETWORK_fdset_copy_native (wws, &ws, max + 1);
1826 GNUNET_NETWORK_fdset_copy_native (wes, &es, max + 1);
1828 if (daemon_handle == plugin->server_v4)
1830 if (plugin->server_v4_task != GNUNET_SCHEDULER_NO_TASK)
1832 GNUNET_SCHEDULER_cancel (plugin->server_v4_task);
1833 plugin->server_v4_task = GNUNET_SCHEDULER_NO_TASK;
1836 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1837 "Scheduling IPv4 server task in %llu ms\n", tv);
1840 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1842 &server_v4_run, plugin);
1844 if (daemon_handle == plugin->server_v6)
1846 if (plugin->server_v6_task != GNUNET_SCHEDULER_NO_TASK)
1848 GNUNET_SCHEDULER_cancel (plugin->server_v6_task);
1849 plugin->server_v6_task = GNUNET_SCHEDULER_NO_TASK;
1852 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1853 "Scheduling IPv6 server task in %llu ms\n", tv);
1856 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1858 &server_v6_run, plugin);
1860 GNUNET_NETWORK_fdset_destroy (wrs);
1861 GNUNET_NETWORK_fdset_destroy (wws);
1862 GNUNET_NETWORK_fdset_destroy (wes);
1869 * Load ssl certificate from file
1871 * @param file filename
1872 * @return content of the file
1875 server_load_file (const char *file)
1877 struct GNUNET_DISK_FileHandle *gn_file;
1881 if (GNUNET_OK != GNUNET_DISK_file_size (file,
1882 &fsize, GNUNET_NO, GNUNET_YES))
1884 text = GNUNET_malloc (fsize + 1);
1886 GNUNET_DISK_file_open (file, GNUNET_DISK_OPEN_READ,
1887 GNUNET_DISK_PERM_USER_READ);
1888 if (gn_file == NULL)
1893 if (GNUNET_SYSERR == GNUNET_DISK_file_read (gn_file, text, fsize))
1896 GNUNET_DISK_file_close (gn_file);
1900 GNUNET_DISK_file_close (gn_file);
1908 * Load ssl certificate
1910 * @param plugin the plugin
1911 * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
1914 server_load_certificate (struct HTTP_Server_Plugin *plugin)
1916 int res = GNUNET_OK;
1922 GNUNET_CONFIGURATION_get_value_filename (plugin->env->cfg, plugin->name,
1923 "KEY_FILE", &key_file))
1925 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
1926 plugin->name, "CERT_FILE");
1927 return GNUNET_SYSERR;
1930 GNUNET_CONFIGURATION_get_value_filename (plugin->env->cfg, plugin->name,
1931 "CERT_FILE", &cert_file))
1933 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
1934 plugin->name, "CERT_FILE");
1935 GNUNET_free (key_file);
1936 return GNUNET_SYSERR;
1938 /* Get crypto init string from config. If not present, use
1941 GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg,
1944 &plugin->crypto_init))
1945 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1946 "Using crypto init string `%s'\n",
1947 plugin->crypto_init);
1949 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1950 "Using default crypto init string \n");
1952 /* read key & certificates from file */
1953 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1954 "Trying to loading TLS certificate from key-file `%s' cert-file`%s'\n",
1955 key_file, cert_file);
1957 plugin->key = server_load_file (key_file);
1958 plugin->cert = server_load_file (cert_file);
1960 if ((plugin->key == NULL) || (plugin->cert == NULL))
1962 struct GNUNET_OS_Process *cert_creation;
1964 GNUNET_free_non_null (plugin->key);
1966 GNUNET_free_non_null (plugin->cert);
1967 plugin->cert = NULL;
1969 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1970 "No usable TLS certificate found, creating certificate\n");
1973 GNUNET_OS_start_process (GNUNET_NO, GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
1975 "gnunet-transport-certificate-creation",
1976 "gnunet-transport-certificate-creation",
1977 key_file, cert_file, NULL);
1978 if (cert_creation == NULL)
1980 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
1982 ("Could not create a new TLS certificate, program `gnunet-transport-certificate-creation' could not be started!\n"));
1983 GNUNET_free (key_file);
1984 GNUNET_free (cert_file);
1986 GNUNET_free_non_null (plugin->key);
1988 GNUNET_free_non_null (plugin->cert);
1989 plugin->cert = NULL;
1990 GNUNET_free_non_null (plugin->crypto_init);
1991 plugin->crypto_init = NULL;
1993 return GNUNET_SYSERR;
1995 GNUNET_assert (GNUNET_OK == GNUNET_OS_process_wait (cert_creation));
1996 GNUNET_OS_process_destroy (cert_creation);
1998 plugin->key = server_load_file (key_file);
1999 plugin->cert = server_load_file (cert_file);
2002 if ((plugin->key == NULL) || (plugin->cert == NULL))
2004 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
2006 _("No usable TLS certificate found and creating one at `%s/%s' failed!\n"),
2007 key_file, cert_file);
2008 GNUNET_free (key_file);
2009 GNUNET_free (cert_file);
2011 GNUNET_free_non_null (plugin->key);
2013 GNUNET_free_non_null (plugin->cert);
2014 plugin->cert = NULL;
2015 GNUNET_free_non_null (plugin->crypto_init);
2016 plugin->crypto_init = NULL;
2018 return GNUNET_SYSERR;
2020 GNUNET_free (key_file);
2021 GNUNET_free (cert_file);
2022 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "TLS certificate loaded\n");
2029 * Start the HTTP server
2031 * @param plugin the plugin handle
2032 * @return GNUNET_OK on success, GNUNET_SYSERR on failure
2035 server_start (struct HTTP_Server_Plugin *plugin)
2037 unsigned int timeout;
2039 GNUNET_assert (NULL != plugin);
2042 if (GNUNET_SYSERR == server_load_certificate (plugin))
2044 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
2045 "Could not load or create server certificate! Loading plugin failed!\n");
2046 return GNUNET_SYSERR;
2051 #if MHD_VERSION >= 0x00090E00
2052 timeout = HTTP_SERVER_NOT_VALIDATED_TIMEOUT.rel_value_us / 1000LL / 1000LL;
2053 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2054 "MHD can set timeout per connection! Default time out %u sec.\n",
2057 timeout = HTTP_SERVER_SESSION_TIMEOUT.rel_value_us / 1000LL / 1000LL;
2058 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, plugin->name,
2059 "MHD cannot set timeout per connection! Default time out %u sec.\n",
2063 plugin->server_v4 = NULL;
2064 if (plugin->use_ipv4 == GNUNET_YES)
2066 plugin->server_v4 = MHD_start_daemon (
2073 MHD_NO_FLAG, plugin->port,
2074 &server_accept_cb, plugin,
2075 &server_access_cb, plugin,
2076 MHD_OPTION_SOCK_ADDR,
2077 (struct sockaddr_in *)
2078 plugin->server_addr_v4,
2079 MHD_OPTION_CONNECTION_LIMIT,
2081 plugin->max_connections,
2083 MHD_OPTION_HTTPS_PRIORITIES,
2084 plugin->crypto_init,
2085 MHD_OPTION_HTTPS_MEM_KEY,
2087 MHD_OPTION_HTTPS_MEM_CERT,
2090 MHD_OPTION_CONNECTION_TIMEOUT,
2092 MHD_OPTION_CONNECTION_MEMORY_LIMIT,
2094 GNUNET_SERVER_MAX_MESSAGE_SIZE),
2095 MHD_OPTION_NOTIFY_COMPLETED,
2096 &server_disconnect_cb, plugin,
2097 MHD_OPTION_EXTERNAL_LOGGER,
2098 server_log, NULL, MHD_OPTION_END);
2099 if (plugin->server_v4 == NULL)
2101 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
2102 "Failed to start %s IPv4 server component on port %u\n",
2103 plugin->name, plugin->port);
2106 server_reschedule (plugin, plugin->server_v4, GNUNET_NO);
2110 plugin->server_v6 = NULL;
2111 if (plugin->use_ipv6 == GNUNET_YES)
2113 plugin->server_v6 = MHD_start_daemon (
2120 MHD_USE_IPv6, plugin->port,
2121 &server_accept_cb, plugin,
2122 &server_access_cb, plugin,
2123 MHD_OPTION_SOCK_ADDR,
2124 (struct sockaddr_in6 *)
2125 plugin->server_addr_v6,
2126 MHD_OPTION_CONNECTION_LIMIT,
2128 plugin->max_connections,
2130 MHD_OPTION_HTTPS_PRIORITIES,
2131 plugin->crypto_init,
2132 MHD_OPTION_HTTPS_MEM_KEY,
2134 MHD_OPTION_HTTPS_MEM_CERT,
2137 MHD_OPTION_CONNECTION_TIMEOUT,
2139 MHD_OPTION_CONNECTION_MEMORY_LIMIT,
2141 GNUNET_SERVER_MAX_MESSAGE_SIZE),
2142 MHD_OPTION_NOTIFY_COMPLETED,
2143 &server_disconnect_cb, plugin,
2144 MHD_OPTION_EXTERNAL_LOGGER,
2145 server_log, NULL, MHD_OPTION_END);
2146 if (plugin->server_v6 == NULL)
2148 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
2149 "Failed to start %s IPv6 server component on port %u\n",
2150 plugin->name, plugin->port);
2153 server_reschedule (plugin, plugin->server_v6, GNUNET_NO);
2157 if ((plugin->server_v6 == NULL) && (plugin->server_v4 == NULL))
2159 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
2160 "%s %s server component started on port %u\n",
2161 msg, plugin->name, plugin->port);
2163 return GNUNET_SYSERR;
2165 else if ((plugin->server_v6 != NULL) && (plugin->server_v4 != NULL))
2166 msg = "IPv4 and IPv6";
2167 else if (plugin->server_v6 != NULL)
2169 else if (plugin->server_v4 != NULL)
2171 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2172 "%s %s server component started on port %u\n",
2173 msg, plugin->name, plugin->port);
2179 server_stop (struct HTTP_Server_Plugin *plugin)
2181 if (plugin->server_v4 != NULL)
2183 MHD_stop_daemon (plugin->server_v4);
2184 plugin->server_v4 = NULL;
2186 if ( plugin->server_v6 != NULL)
2188 MHD_stop_daemon (plugin->server_v6);
2189 plugin->server_v6 = NULL;
2193 if (plugin->server_v4_task != GNUNET_SCHEDULER_NO_TASK)
2195 GNUNET_SCHEDULER_cancel (plugin->server_v4_task);
2196 plugin->server_v4_task = GNUNET_SCHEDULER_NO_TASK;
2199 if (plugin->server_v6_task != GNUNET_SCHEDULER_NO_TASK)
2201 GNUNET_SCHEDULER_cancel (plugin->server_v6_task);
2202 plugin->server_v6_task = GNUNET_SCHEDULER_NO_TASK;
2205 GNUNET_free_non_null (plugin->crypto_init);
2206 GNUNET_free_non_null (plugin->cert);
2207 GNUNET_free_non_null (plugin->key);
2210 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2211 "%s server component stopped\n", plugin->name);
2216 * Add an address to the server's set of addresses and notify transport
2218 * @param cls the plugin handle
2219 * @param add_remove GNUNET_YES on add, GNUNET_NO on remove
2220 * @param addr the address
2221 * @param addrlen address length
2224 server_add_address (void *cls, int add_remove, const struct sockaddr *addr,
2227 struct HTTP_Server_Plugin *plugin = cls;
2228 struct GNUNET_HELLO_Address *address;
2229 struct HttpAddressWrapper *w = NULL;
2231 w = GNUNET_new (struct HttpAddressWrapper);
2232 w->address = http_common_address_from_socket (plugin->protocol, addr, addrlen);
2233 if (NULL == w->address)
2238 w->addrlen = http_common_address_get_size (w->address);
2240 GNUNET_CONTAINER_DLL_insert(plugin->addr_head, plugin->addr_tail, w);
2241 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2242 "Notifying transport to add address `%s'\n",
2243 http_common_plugin_address_to_string (NULL,
2245 w->address, w->addrlen));
2246 /* modify our published address list */
2248 address = GNUNET_HELLO_address_allocate (plugin->env->my_identity,
2249 "https_client", w->address, w->addrlen, GNUNET_HELLO_ADDRESS_INFO_NONE);
2251 address = GNUNET_HELLO_address_allocate (plugin->env->my_identity,
2252 "http_client", w->address, w->addrlen, GNUNET_HELLO_ADDRESS_INFO_NONE);
2255 plugin->env->notify_address (plugin->env->cls, add_remove, address);
2256 GNUNET_HELLO_address_free (address);
2261 * Remove an address from the server's set of addresses and notify transport
2263 * @param cls the plugin handle
2264 * @param add_remove GNUNET_YES on add, GNUNET_NO on remove
2265 * @param addr the address
2266 * @param addrlen address length
2269 server_remove_address (void *cls, int add_remove, const struct sockaddr *addr,
2272 struct HTTP_Server_Plugin *plugin = cls;
2273 struct GNUNET_HELLO_Address *address;
2274 struct HttpAddressWrapper *w = plugin->addr_head;
2276 void * saddr = http_common_address_from_socket (plugin->protocol, addr, addrlen);
2279 saddr_len = http_common_address_get_size (saddr);
2283 if (GNUNET_YES == http_common_cmp_addresses(w->address, w->addrlen, saddr, saddr_len))
2287 GNUNET_free (saddr);
2292 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2293 "Notifying transport to remove address `%s'\n",
2294 http_common_plugin_address_to_string (NULL,
2296 w->address, w->addrlen));
2299 GNUNET_CONTAINER_DLL_remove (plugin->addr_head, plugin->addr_tail, w);
2301 /* modify our published address list */
2303 address = GNUNET_HELLO_address_allocate (plugin->env->my_identity,
2304 "https_client", w->address, w->addrlen, GNUNET_HELLO_ADDRESS_INFO_NONE);
2306 address = GNUNET_HELLO_address_allocate (plugin->env->my_identity,
2307 "http_client", w->address, w->addrlen, GNUNET_HELLO_ADDRESS_INFO_NONE);
2309 plugin->env->notify_address (plugin->env->cls, add_remove, address);
2310 GNUNET_HELLO_address_free (address);
2311 GNUNET_free (w->address);
2318 * Our external IP address/port mapping has changed.
2320 * @param cls closure, the 'struct LocalAddrList'
2321 * @param add_remove GNUNET_YES to mean the new public IP address, GNUNET_NO to mean
2322 * the previous (now invalid) one
2323 * @param addr either the previous or the new public IP address
2324 * @param addrlen actual lenght of the address
2327 server_nat_port_map_callback (void *cls, int add_remove, const struct sockaddr *addr,
2330 GNUNET_assert (cls != NULL);
2331 struct HTTP_Server_Plugin *plugin = cls;
2333 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2334 "NAT called to %s address `%s'\n",
2335 (add_remove == GNUNET_NO) ? "remove" : "add",
2336 GNUNET_a2s (addr, addrlen));
2338 if (AF_INET == addr->sa_family)
2340 struct sockaddr_in *s4 = (struct sockaddr_in *) addr;
2342 if (GNUNET_NO == plugin->use_ipv4)
2345 if ((NULL != plugin->server_addr_v4) &&
2346 (0 != memcmp (&plugin->server_addr_v4->sin_addr,
2347 &s4->sin_addr, sizeof (struct in_addr))))
2349 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2350 "Skipping address `%s' (not bindto address)\n",
2351 GNUNET_a2s (addr, addrlen));
2356 if (AF_INET6 == addr->sa_family)
2358 struct sockaddr_in6 *s6 = (struct sockaddr_in6 *) addr;
2359 if (GNUNET_NO == plugin->use_ipv6)
2362 if ((NULL != plugin->server_addr_v6) &&
2363 (0 != memcmp (&plugin->server_addr_v6->sin6_addr,
2364 &s6->sin6_addr, sizeof (struct in6_addr))))
2366 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2367 "Skipping address `%s' (not bindto address)\n",
2368 GNUNET_a2s (addr, addrlen));
2376 server_add_address (cls, add_remove, addr, addrlen);
2379 server_remove_address (cls, add_remove, addr, addrlen);
2386 * Get valid server addresses
2388 * @param plugin the plugin handle
2389 * @param service_name the servicename
2390 * @param cfg configuration handle
2391 * @param addrs addresses
2392 * @param addr_lens address length
2393 * @return number of addresses
2396 server_get_addresses (struct HTTP_Server_Plugin *plugin,
2397 const char *service_name,
2398 const struct GNUNET_CONFIGURATION_Handle *cfg,
2399 struct sockaddr ***addrs, socklen_t ** addr_lens)
2402 unsigned long long port;
2403 struct addrinfo hints;
2404 struct addrinfo *res;
2405 struct addrinfo *pos;
2406 struct addrinfo *next;
2410 struct sockaddr **saddrs;
2411 socklen_t *saddrlens;
2417 disablev6 = !plugin->use_ipv6;
2420 if (GNUNET_CONFIGURATION_have_value (cfg, service_name, "PORT"))
2422 GNUNET_break (GNUNET_OK ==
2423 GNUNET_CONFIGURATION_get_value_number (cfg, service_name,
2427 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2429 ("Require valid port number for service in configuration!\n"));
2430 return GNUNET_SYSERR;
2435 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, plugin->name,
2436 "Starting in listen only mode\n");
2437 return -1; /* listen only */
2441 if (GNUNET_CONFIGURATION_have_value (cfg, service_name, "BINDTO"))
2443 GNUNET_break (GNUNET_OK ==
2444 GNUNET_CONFIGURATION_get_value_string (cfg, service_name,
2445 "BINDTO", &hostname));
2450 if (hostname != NULL)
2452 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2453 "Resolving `%s' since that is where `%s' will bind to.\n",
2454 hostname, service_name);
2455 memset (&hints, 0, sizeof (struct addrinfo));
2457 hints.ai_family = AF_INET;
2458 if ((0 != (ret = getaddrinfo (hostname, NULL, &hints, &res))) ||
2461 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Failed to resolve `%s': %s\n"),
2462 hostname, gai_strerror (ret));
2463 GNUNET_free (hostname);
2464 return GNUNET_SYSERR;
2468 while (NULL != (pos = next))
2470 next = pos->ai_next;
2471 if ((disablev6) && (pos->ai_family == AF_INET6))
2477 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2478 _("Failed to find %saddress for `%s'.\n"),
2479 disablev6 ? "IPv4 " : "", hostname);
2481 GNUNET_free (hostname);
2482 return GNUNET_SYSERR;
2485 saddrs = GNUNET_malloc ((resi + 1) * sizeof (struct sockaddr *));
2486 saddrlens = GNUNET_malloc ((resi + 1) * sizeof (socklen_t));
2489 while (NULL != (pos = next))
2491 next = pos->ai_next;
2492 if ((disablev6) && (pos->ai_family == AF_INET6))
2494 if ((pos->ai_protocol != IPPROTO_TCP) && (pos->ai_protocol != 0))
2495 continue; /* not TCP */
2496 if ((pos->ai_socktype != SOCK_STREAM) && (pos->ai_socktype != 0))
2497 continue; /* huh? */
2498 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2499 "Service will bind to `%s'\n", GNUNET_a2s (pos->ai_addr,
2501 if (pos->ai_family == AF_INET)
2503 GNUNET_assert (pos->ai_addrlen == sizeof (struct sockaddr_in));
2504 saddrlens[i] = pos->ai_addrlen;
2505 saddrs[i] = GNUNET_malloc (saddrlens[i]);
2506 memcpy (saddrs[i], pos->ai_addr, saddrlens[i]);
2507 ((struct sockaddr_in *) saddrs[i])->sin_port = htons (port);
2511 GNUNET_assert (pos->ai_family == AF_INET6);
2512 GNUNET_assert (pos->ai_addrlen == sizeof (struct sockaddr_in6));
2513 saddrlens[i] = pos->ai_addrlen;
2514 saddrs[i] = GNUNET_malloc (saddrlens[i]);
2515 memcpy (saddrs[i], pos->ai_addr, saddrlens[i]);
2516 ((struct sockaddr_in6 *) saddrs[i])->sin6_port = htons (port);
2520 GNUNET_free (hostname);
2526 /* will bind against everything, just set port */
2532 saddrs = GNUNET_malloc ((resi + 1) * sizeof (struct sockaddr *));
2533 saddrlens = GNUNET_malloc ((resi + 1) * sizeof (socklen_t));
2535 saddrlens[i] = sizeof (struct sockaddr_in);
2536 saddrs[i] = GNUNET_malloc (saddrlens[i]);
2537 #if HAVE_SOCKADDR_IN_SIN_LEN
2538 ((struct sockaddr_in *) saddrs[i])->sin_len = saddrlens[i];
2540 ((struct sockaddr_in *) saddrs[i])->sin_family = AF_INET;
2541 ((struct sockaddr_in *) saddrs[i])->sin_port = htons (port);
2547 saddrs = GNUNET_malloc ((resi + 1) * sizeof (struct sockaddr *));
2548 saddrlens = GNUNET_malloc ((resi + 1) * sizeof (socklen_t));
2550 saddrlens[i] = sizeof (struct sockaddr_in6);
2551 saddrs[i] = GNUNET_malloc (saddrlens[i]);
2552 #if HAVE_SOCKADDR_IN_SIN_LEN
2553 ((struct sockaddr_in6 *) saddrs[i])->sin6_len = saddrlens[0];
2555 ((struct sockaddr_in6 *) saddrs[i])->sin6_family = AF_INET6;
2556 ((struct sockaddr_in6 *) saddrs[i])->sin6_port = htons (port);
2558 saddrlens[i] = sizeof (struct sockaddr_in);
2559 saddrs[i] = GNUNET_malloc (saddrlens[i]);
2560 #if HAVE_SOCKADDR_IN_SIN_LEN
2561 ((struct sockaddr_in *) saddrs[i])->sin_len = saddrlens[1];
2563 ((struct sockaddr_in *) saddrs[i])->sin_family = AF_INET;
2564 ((struct sockaddr_in *) saddrs[i])->sin_port = htons (port);
2568 *addr_lens = saddrlens;
2574 * Ask NAT for addresses
2576 * @param plugin the plugin handle
2579 server_start_report_addresses (struct HTTP_Server_Plugin *plugin)
2581 int res = GNUNET_OK;
2582 struct sockaddr **addrs;
2583 socklen_t *addrlens;
2585 res = server_get_addresses (plugin,
2586 plugin->name, plugin->env->cfg,
2588 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2589 _("Found %u addresses to report to NAT service\n"), res);
2591 if (GNUNET_SYSERR == res)
2598 GNUNET_NAT_register (plugin->env->cfg, GNUNET_YES, plugin->port,
2600 (const struct sockaddr **) addrs, addrlens,
2601 &server_nat_port_map_callback, NULL, plugin);
2605 GNUNET_assert (addrs[res] != NULL);
2606 GNUNET_free (addrs[res]);
2608 GNUNET_free_non_null (addrs);
2609 GNUNET_free_non_null (addrlens);
2614 * Stop NAT for addresses
2616 * @param plugin the plugin handle
2619 server_stop_report_addresses (struct HTTP_Server_Plugin *plugin)
2621 /* Stop NAT handle */
2622 if (NULL != plugin->nat)
2623 GNUNET_NAT_unregister (plugin->nat);
2625 /* Clean up addresses */
2626 struct HttpAddressWrapper *w;
2628 while (plugin->addr_head != NULL)
2630 w = plugin->addr_head;
2631 GNUNET_CONTAINER_DLL_remove (plugin->addr_head, plugin->addr_tail, w);
2632 GNUNET_free (w->address);
2639 * Check if IPv6 supported on this system
2641 * @param plugin the plugin handle
2642 * @return GNUNET_YES on success, else GNUNET_NO
2645 server_check_ipv6_support (struct HTTP_Server_Plugin *plugin)
2647 struct GNUNET_NETWORK_Handle *desc = NULL;
2648 int res = GNUNET_NO;
2650 /* Probe IPv6 support */
2651 desc = GNUNET_NETWORK_socket_create (PF_INET6, SOCK_STREAM, 0);
2654 if ((errno == ENOBUFS) || (errno == ENOMEM) || (errno == ENFILE) ||
2657 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "socket");
2659 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, plugin->name,
2661 ("Disabling IPv6 since it is not supported on this system!\n"));
2666 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (desc));
2670 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2671 "Testing IPv6 on this system: %s\n",
2672 (res == GNUNET_YES) ? "successful" : "failed");
2678 * Notify server about our external hostname
2681 * @param tc task context (unused)
2684 server_notify_external_hostname (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2686 struct HTTP_Server_Plugin *plugin = cls;
2687 struct HttpAddress *ext_addr;
2688 size_t ext_addr_len;
2692 plugin->notify_ext_task = GNUNET_SCHEDULER_NO_TASK;
2693 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
2696 GNUNET_asprintf(&url, "%s://%s", plugin->protocol, plugin->external_hostname);
2698 urlen = strlen (url) + 1;
2699 ext_addr = GNUNET_malloc (sizeof (struct HttpAddress) + urlen);
2700 ext_addr->options = htonl(plugin->options);
2701 ext_addr->urlen = htonl (urlen);
2702 ext_addr_len = sizeof (struct HttpAddress) + urlen;
2703 memcpy (&ext_addr[1], url, urlen);
2706 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2707 "Notifying transport about external hostname address `%s'\n", plugin->external_hostname);
2710 if (GNUNET_YES == plugin->verify_external_hostname)
2711 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, plugin->name,
2712 "Enabling SSL verification for external hostname address `%s'\n",
2713 plugin->external_hostname);
2714 plugin->ext_addr = GNUNET_HELLO_address_allocate (plugin->env->my_identity,
2715 "https_client", ext_addr, ext_addr_len, GNUNET_HELLO_ADDRESS_INFO_NONE );
2716 plugin->env->notify_address (plugin->env->cls, GNUNET_YES, plugin->ext_addr);
2717 GNUNET_free (ext_addr);
2719 plugin->ext_addr = GNUNET_HELLO_address_allocate (plugin->env->my_identity,
2720 "http_client", ext_addr, ext_addr_len, GNUNET_HELLO_ADDRESS_INFO_NONE );
2721 plugin->env->notify_address (plugin->env->cls, GNUNET_YES, plugin->ext_addr);
2722 GNUNET_free (ext_addr);
2728 * Configure the plugin
2730 * @param plugin plugin handle
2731 * @return GNUNET_OK on success, GNUNET_SYSERR on failure
2734 server_configure_plugin (struct HTTP_Server_Plugin *plugin)
2736 unsigned long long port;
2737 unsigned long long max_connections;
2738 char *bind4_address = NULL;
2739 char *bind6_address = NULL;
2740 char *eh_tmp = NULL;
2741 int external_hostname_use_port;
2744 if (GNUNET_CONFIGURATION_have_value
2745 (plugin->env->cfg, plugin->name, "USE_IPv4"))
2748 GNUNET_CONFIGURATION_get_value_yesno (plugin->env->cfg, plugin->name,
2752 plugin->use_ipv4 = GNUNET_YES;
2753 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2754 _("IPv4 support is %s\n"),
2755 (plugin->use_ipv4 == GNUNET_YES) ? "enabled" : "disabled");
2758 if (GNUNET_CONFIGURATION_have_value
2759 (plugin->env->cfg, plugin->name, "USE_IPv6"))
2762 GNUNET_CONFIGURATION_get_value_yesno (plugin->env->cfg, plugin->name,
2766 plugin->use_ipv6 = GNUNET_YES;
2767 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2768 _("IPv6 support is %s\n"),
2769 (plugin->use_ipv6 == GNUNET_YES) ? "enabled" : "disabled");
2771 if ((plugin->use_ipv4 == GNUNET_NO) && (plugin->use_ipv6 == GNUNET_NO))
2773 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
2775 ("Neither IPv4 nor IPv6 are enabled! Fix in configuration\n"),
2777 return GNUNET_SYSERR;
2780 /* Reading port number from config file */
2782 GNUNET_CONFIGURATION_get_value_number (plugin->env->cfg, plugin->name,
2783 "PORT", &port)) || (port > 65535))
2785 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
2786 _("Port is required! Fix in configuration\n"),
2788 return GNUNET_SYSERR;
2790 plugin->port = port;
2792 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, plugin->name,
2793 _("Using port %u\n"), plugin->port);
2795 if ((plugin->use_ipv4 == GNUNET_YES) &&
2796 (GNUNET_YES == GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg,
2797 plugin->name, "BINDTO", &bind4_address)))
2799 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2800 "Binding %s plugin to specific IPv4 address: `%s'\n",
2801 plugin->protocol, bind4_address);
2802 plugin->server_addr_v4 = GNUNET_new (struct sockaddr_in);
2803 if (1 != inet_pton (AF_INET, bind4_address,
2804 &plugin->server_addr_v4->sin_addr))
2806 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
2808 ("Specific IPv4 address `%s' in configuration file is invalid!\n"),
2810 GNUNET_free (bind4_address);
2811 GNUNET_free (plugin->server_addr_v4);
2812 plugin->server_addr_v4 = NULL;
2813 return GNUNET_SYSERR;
2817 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2818 _("Binding to IPv4 address %s\n"), bind4_address);
2819 plugin->server_addr_v4->sin_family = AF_INET;
2820 plugin->server_addr_v4->sin_port = htons (plugin->port);
2822 GNUNET_free (bind4_address);
2825 if ((plugin->use_ipv6 == GNUNET_YES) &&
2827 GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg, plugin->name,
2828 "BINDTO6", &bind6_address)))
2830 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2831 "Binding %s plugin to specific IPv6 address: `%s'\n",
2832 plugin->protocol, bind6_address);
2833 plugin->server_addr_v6 = GNUNET_new (struct sockaddr_in6);
2835 inet_pton (AF_INET6, bind6_address, &plugin->server_addr_v6->sin6_addr))
2837 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
2839 ("Specific IPv6 address `%s' in configuration file is invalid!\n"),
2841 GNUNET_free (bind6_address);
2842 GNUNET_free (plugin->server_addr_v6);
2843 plugin->server_addr_v6 = NULL;
2844 return GNUNET_SYSERR;
2848 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2849 _("Binding to IPv6 address %s\n"), bind6_address);
2850 plugin->server_addr_v6->sin6_family = AF_INET6;
2851 plugin->server_addr_v6->sin6_port = htons (plugin->port);
2853 GNUNET_free (bind6_address);
2856 plugin->verify_external_hostname = GNUNET_NO;
2858 plugin->verify_external_hostname = GNUNET_CONFIGURATION_get_value_yesno (plugin->env->cfg, plugin->name,
2859 "VERIFY_EXTERNAL_HOSTNAME");
2860 if (GNUNET_SYSERR == plugin->verify_external_hostname)
2861 plugin->verify_external_hostname = GNUNET_NO;
2862 if (GNUNET_YES == plugin->verify_external_hostname)
2863 plugin->options |= HTTP_OPTIONS_VERIFY_CERTIFICATE;
2865 external_hostname_use_port = GNUNET_CONFIGURATION_get_value_yesno (plugin->env->cfg, plugin->name,
2866 "EXTERNAL_HOSTNAME_USE_PORT");
2867 if (GNUNET_SYSERR == external_hostname_use_port)
2868 external_hostname_use_port = GNUNET_NO;
2871 if (GNUNET_YES == GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg, plugin->name,
2872 "EXTERNAL_HOSTNAME", &eh_tmp))
2876 char * pos_url = NULL;
2878 if (NULL != strstr(eh_tmp, "://"))
2880 tmp = &strstr(eh_tmp, "://")[3];
2885 if (GNUNET_YES == external_hostname_use_port)
2887 if ( (strlen (tmp) > 1) && (NULL != (pos = strchr(tmp, '/'))) )
2891 GNUNET_asprintf (&plugin->external_hostname, "%s:%u/%s", tmp, (uint16_t) port, (NULL == pos_url) ? "" : pos_url);
2894 GNUNET_asprintf (&plugin->external_hostname, "%s:%u", tmp, (uint16_t) port);
2897 plugin->external_hostname = GNUNET_strdup (tmp);
2898 GNUNET_free (eh_tmp);
2900 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, plugin->name,
2901 _("Using external hostname `%s'\n"), plugin->external_hostname);
2902 plugin->notify_ext_task = GNUNET_SCHEDULER_add_now (&server_notify_external_hostname, plugin);
2904 /* Use only configured external hostname */
2905 if (GNUNET_CONFIGURATION_have_value
2906 (plugin->env->cfg, plugin->name, "EXTERNAL_HOSTNAME_ONLY"))
2908 plugin->external_only =
2909 GNUNET_CONFIGURATION_get_value_yesno (plugin->env->cfg, plugin->name,
2910 "EXTERNAL_HOSTNAME_ONLY");
2913 plugin->external_only = GNUNET_NO;
2915 if (GNUNET_YES == plugin->external_only)
2916 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2917 _("Notifying transport only about hostname `%s'\n"), plugin->external_hostname);
2920 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2921 "No external hostname configured\n");
2923 /* Optional parameters */
2924 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (plugin->env->cfg,
2926 "MAX_CONNECTIONS", &max_connections))
2927 max_connections = 128;
2928 plugin->max_connections = max_connections;
2930 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2931 _("Maximum number of connections is %u\n"),
2932 plugin->max_connections);
2935 plugin->peer_id_length = strlen (GNUNET_i2s_full (plugin->env->my_identity));
2942 * Session was idle, so disconnect it
2944 * @param cls the session
2945 * @param tc task context
2948 server_session_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2950 struct Session *s = cls;
2952 s->timeout_task = GNUNET_SCHEDULER_NO_TASK;
2953 GNUNET_log (TIMEOUT_LOG,
2954 "Session %p was idle for %s, disconnecting\n",
2956 GNUNET_STRINGS_relative_time_to_string (HTTP_SERVER_SESSION_TIMEOUT,
2959 /* call session destroy function */
2960 GNUNET_assert (GNUNET_OK ==
2961 http_server_plugin_disconnect_session (s->plugin, s));
2966 * Start session timeout for session s
2968 * @param s the session
2971 server_start_session_timeout (struct Session *s)
2973 GNUNET_assert (NULL != s);
2974 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == s->timeout_task);
2975 s->timeout_task = GNUNET_SCHEDULER_add_delayed (HTTP_SERVER_SESSION_TIMEOUT,
2976 &server_session_timeout,
2978 GNUNET_log (TIMEOUT_LOG,
2979 "Timeout for session %p set to %s\n",
2981 GNUNET_STRINGS_relative_time_to_string (HTTP_SERVER_SESSION_TIMEOUT,
2987 * Increment session timeout due to activity session s
2989 * @param s the session
2992 server_reschedule_session_timeout (struct Session *s)
2994 GNUNET_assert (NULL != s);
2995 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK != s->timeout_task);
2997 GNUNET_SCHEDULER_cancel (s->timeout_task);
2998 s->timeout_task = GNUNET_SCHEDULER_add_delayed (HTTP_SERVER_SESSION_TIMEOUT,
2999 &server_session_timeout,
3001 GNUNET_log (TIMEOUT_LOG,
3002 "Timeout rescheduled for session %p set to %s\n",
3004 GNUNET_STRINGS_relative_time_to_string (HTTP_SERVER_SESSION_TIMEOUT,
3010 * Exit point from the plugin.
3016 LIBGNUNET_PLUGIN_TRANSPORT_DONE (void *cls)
3018 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
3019 struct HTTP_Server_Plugin *plugin = api->cls;
3020 struct Session *pos;
3021 struct Session *next;
3023 if (NULL == api->cls)
3025 /* Free for stub mode */
3029 plugin->in_shutdown = GNUNET_YES;
3030 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
3031 _("Shutting down plugin `%s'\n"),
3034 if (GNUNET_SCHEDULER_NO_TASK != plugin->notify_ext_task)
3036 GNUNET_SCHEDULER_cancel (plugin->notify_ext_task);
3037 plugin->notify_ext_task = GNUNET_SCHEDULER_NO_TASK;
3040 if (NULL != plugin->ext_addr)
3042 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
3043 "Notifying transport to remove address `%s'\n",
3044 http_common_plugin_address_to_string (NULL,
3046 plugin->ext_addr->address,
3047 plugin->ext_addr->address_length));
3049 plugin->env->notify_address (plugin->env->cls,
3053 plugin->env->notify_address (plugin->env->cls,
3057 GNUNET_HELLO_address_free (plugin->ext_addr);
3058 plugin->ext_addr = NULL;
3061 /* Stop to report addresses to transport service */
3062 server_stop_report_addresses (plugin);
3063 server_stop (plugin);
3064 next = plugin->head;
3065 while (NULL != (pos = next))
3068 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
3069 "Removing left over session %p\n", pos);
3071 if ((GNUNET_YES == pos->session_passed) && (GNUNET_NO == pos->session_ended))
3073 /* Notify transport immediately that this session is invalid */
3074 pos->session_ended = GNUNET_YES;
3075 plugin->env->session_end (plugin->env->cls, pos->address, pos);
3077 server_delete_session (plugin, pos);
3081 GNUNET_free_non_null (plugin->external_hostname);
3082 GNUNET_free_non_null (plugin->ext_addr);
3083 GNUNET_free_non_null (plugin->server_addr_v4);
3084 GNUNET_free_non_null (plugin->server_addr_v6);
3085 regfree(&plugin->url_regex);
3087 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
3088 _("Shutdown for plugin `%s' complete\n"),
3091 GNUNET_free (plugin);
3098 http_plugin_address_to_string (void *cls,
3103 return http_common_plugin_address_to_string (cls, PLUGIN_NAME, addr, addrlen);
3105 return http_common_plugin_address_to_string (cls, PLUGIN_NAME, addr, addrlen);
3111 * Function obtain the network type for a session
3113 * @param cls closure ('struct HTTP_Server_Plugin*')
3114 * @param session the session
3115 * @return the network type in HBO or GNUNET_SYSERR
3117 static enum GNUNET_ATS_Network_Type
3118 http_server_get_network (void *cls,
3119 struct Session *session)
3121 GNUNET_assert (NULL != session);
3122 return ntohl (session->ats_address_network_type);
3127 * Entry point for the plugin.
3133 LIBGNUNET_PLUGIN_TRANSPORT_INIT (void *cls)
3135 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
3136 struct GNUNET_TRANSPORT_PluginFunctions *api;
3137 struct HTTP_Server_Plugin *plugin;
3139 if (NULL == env->receive)
3141 /* run in 'stub' mode (i.e. as part of gnunet-peerinfo), don't fully
3142 initialze the plugin or the API */
3143 api = GNUNET_new (struct GNUNET_TRANSPORT_PluginFunctions);
3145 api->address_to_string = &http_plugin_address_to_string;
3146 api->string_to_address = &http_common_plugin_string_to_address;
3147 api->address_pretty_printer = &http_common_plugin_address_pretty_printer;
3150 plugin = GNUNET_new (struct HTTP_Server_Plugin);
3152 api = GNUNET_new (struct GNUNET_TRANSPORT_PluginFunctions);
3154 api->send = &http_server_plugin_send;
3155 api->disconnect_peer = &http_server_plugin_disconnect_peer;
3156 api->disconnect_session = &http_server_plugin_disconnect_session;
3157 api->query_keepalive_factor = &http_server_query_keepalive_factor;
3158 api->check_address = &http_server_plugin_address_suggested;
3159 api->get_session = &http_server_plugin_get_session;
3161 api->address_to_string = &http_plugin_address_to_string;
3162 api->string_to_address = &http_common_plugin_string_to_address;
3163 api->address_pretty_printer = &http_common_plugin_address_pretty_printer;
3164 api->get_network = &http_server_get_network;
3165 api->update_session_timeout = &http_server_plugin_update_session_timeout;
3167 plugin->name = "transport-https_server";
3168 plugin->protocol = "https";
3170 plugin->name = "transport-http_server";
3171 plugin->protocol = "http";
3174 /* Compile URL regex */
3175 if (regcomp(&plugin->url_regex, URL_REGEX, REG_EXTENDED)) {
3176 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
3177 _("Unable to compile URL regex\n"));
3178 LIBGNUNET_PLUGIN_TRANSPORT_DONE (api);
3182 /* Configure plugin */
3183 if (GNUNET_SYSERR == server_configure_plugin (plugin))
3185 LIBGNUNET_PLUGIN_TRANSPORT_DONE (api);
3189 /* Check IPv6 support */
3190 if (GNUNET_YES == plugin->use_ipv6)
3191 plugin->use_ipv6 = server_check_ipv6_support (plugin);
3193 /* Report addresses to transport service */
3194 if (GNUNET_NO == plugin->external_only)
3195 server_start_report_addresses (plugin);
3197 if (GNUNET_SYSERR == server_start (plugin))
3199 LIBGNUNET_PLUGIN_TRANSPORT_DONE (api);
3205 /* end of plugin_transport_http_server.c */