2 This file is part of GNUnet
3 Copyright (C) 2002-2014, 2017 GNUnet e.V.
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your 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 Affero General Public License for more details.
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
22 * @file transport/plugin_transport_http_server.c
23 * @brief HTTP/S server transport plugin
24 * @author Matthias Wachs
25 * @author David Barksdale
26 * @author Christian Grothoff
29 #include "gnunet_util_lib.h"
30 #include "gnunet_statistics_service.h"
31 #include "gnunet_transport_plugin.h"
32 #include "gnunet_nat_service.h"
33 #include "plugin_transport_http_common.h"
34 #include <microhttpd.h>
39 #define PLUGIN_NAME "https_server"
40 #define LIBGNUNET_PLUGIN_TRANSPORT_INIT \
41 libgnunet_plugin_transport_https_server_init
42 #define LIBGNUNET_PLUGIN_TRANSPORT_DONE \
43 libgnunet_plugin_transport_https_server_done
45 #define PLUGIN_NAME "http_server"
46 #define LIBGNUNET_PLUGIN_TRANSPORT_INIT \
47 libgnunet_plugin_transport_http_server_init
48 #define LIBGNUNET_PLUGIN_TRANSPORT_DONE \
49 libgnunet_plugin_transport_http_server_done
52 #define HTTP_ERROR_RESPONSE \
53 "<!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>"
58 #define LOG(kind, ...) GNUNET_log_from (kind, "transport-" PLUGIN_NAME, \
63 * Information we keep with MHD for an HTTP request.
68 * The session this server request belongs to
69 * Can be NULL, when session was disconnected and freed
71 struct GNUNET_ATS_Session *session;
76 struct MHD_Connection *mhd_conn;
81 struct MHD_Daemon *mhd_daemon;
84 * Options requested by peer
87 #define OPTION_LONG_POLL 1 /* GET request wants long-poll semantics */
95 * For PUT requests: Is this the first or last callback with size 0
96 * For GET requests: Have we sent a message
101 * Currently suspended
108 * Wrapper to manage addresses
110 struct HttpAddressWrapper
115 struct HttpAddressWrapper *next;
118 * Linked list previous
120 struct HttpAddressWrapper *prev;
123 * An address we are using.
125 struct HttpAddress *address;
128 * Length of the address.
135 * Message to send using http
140 * next pointer for double linked list
142 struct HTTP_Message *next;
145 * previous pointer for double linked list
147 struct HTTP_Message *prev;
150 * buffer containing data to send
155 * amount of data already sent
165 * HTTP/S specific overhead
170 * Continuation function to call once the transmission buffer
171 * has again space available. NULL if there is no
172 * continuation to call.
174 GNUNET_TRANSPORT_TransmitContinuation transmit_cont;
177 * Closure for transmit_cont.
179 void *transmit_cont_cls;
184 * Session handle for connections.
186 struct GNUNET_ATS_Session
189 * To whom are we talking to (set to our identity
190 * if we are still waiting for the welcome message)
192 struct GNUNET_PeerIdentity target;
195 * Pointer to the global plugin struct.
197 struct HTTP_Server_Plugin *plugin;
200 * next pointer for double linked list
202 struct HTTP_Message *msg_head;
205 * previous pointer for double linked list
207 struct HTTP_Message *msg_tail;
210 * Message stream tokenizer for incoming data
212 struct GNUNET_MessageStreamTokenizer *msg_tk;
217 struct ServerRequest *server_recv;
222 struct ServerRequest *server_send;
227 struct GNUNET_HELLO_Address *address;
230 * Absolute time when to receive data again
231 * Used for receive throttling
233 struct GNUNET_TIME_Absolute next_receive;
236 * Absolute time when this connection will time out.
238 struct GNUNET_TIME_Absolute timeout;
241 * Session timeout task
243 struct GNUNET_SCHEDULER_Task *timeout_task;
246 * Task to resume MHD handling when receiving is allowed again
248 struct GNUNET_SCHEDULER_Task *recv_wakeup_task;
251 * Number of bytes waiting for transmission to this peer.
253 unsigned long long bytes_in_queue;
256 * Number of messages waiting for transmission to this peer.
258 unsigned int msgs_in_queue;
261 * Unique HTTP/S connection tag for this connection
268 enum GNUNET_NetworkType scope;
271 * #GNUNET_YES if this session is known to the service.
273 int known_to_service;
278 * Encapsulation of all of the state of the plugin.
280 struct HTTP_Server_Plugin
285 struct GNUNET_TRANSPORT_PluginEnvironment *env;
288 * Hash map of open sessions.
290 struct GNUNET_CONTAINER_MultiPeerMap *sessions;
293 * Function to call about session status changes.
295 GNUNET_TRANSPORT_SessionInfoCallback sic;
298 * Closure for @e sic.
315 char *external_hostname;
318 * External hostname the plugin can be connected to, can be different to
319 * the host's FQDN, used e.g. for reverse proxying
321 struct GNUNET_HELLO_Address *ext_addr;
324 * NAT handle & address management
326 struct GNUNET_NAT_Handle *nat;
329 * IPv4 addresses DLL head
331 struct HttpAddressWrapper *addr_head;
334 * IPv4 addresses DLL tail
336 struct HttpAddressWrapper *addr_tail;
339 * IPv4 server socket to bind to
341 struct sockaddr_in *server_addr_v4;
344 * IPv6 server socket to bind to
346 struct sockaddr_in6 *server_addr_v6;
351 struct MHD_Daemon *server_v4;
356 struct MHD_Daemon *server_v6;
364 * Use RC4-128 instead of AES:
365 * NONE:+VERS-TLS1.0:+ARCFOUR-128:+SHA1:+RSA:+COMP-NULL
384 struct GNUNET_SCHEDULER_Task *server_v4_task;
389 struct GNUNET_SCHEDULER_Task *server_v6_task;
392 * Task calling transport service about external address
394 struct GNUNET_SCHEDULER_Task *notify_ext_task;
397 * Notify transport only about external address
399 unsigned int external_only;
402 * The IPv4 server is scheduled to run asap
404 int server_v4_immediately;
407 * The IPv6 server is scheduled to run asap
409 int server_v6_immediately;
412 * Verify external address
414 int verify_external_hostname;
417 * Maximum number of sockets the plugin can use
418 * Each http request /request connections are two connections
420 unsigned int max_request;
423 * Current number of sockets the plugin can use
424 * Each http connection are two requests
426 unsigned int cur_request;
429 * Did we immediately end the session in disconnect_cb
439 * My options to be included in the address
459 * Regex for parsing URLs. FIXME: this seems overkill.
466 * If a session monitor is attached, notify it about the new
469 * @param plugin our plugin
470 * @param session session that changed state
471 * @param state new state of the session
474 notify_session_monitor (struct HTTP_Server_Plugin *plugin,
475 struct GNUNET_ATS_Session *session,
476 enum GNUNET_TRANSPORT_SessionState state)
478 struct GNUNET_TRANSPORT_SessionInfo info;
480 if (NULL == plugin->sic)
482 memset (&info, 0, sizeof(info));
484 info.is_inbound = GNUNET_YES;
485 info.num_msg_pending = session->msgs_in_queue;
486 info.num_bytes_pending = session->bytes_in_queue;
487 info.receive_delay = session->next_receive;
488 info.session_timeout = session->timeout;
489 info.address = session->address;
490 plugin->sic (plugin->sic_cls,
497 * Wake up an MHD connection which was suspended
499 * @param cls the session
502 server_wake_up (void *cls)
504 struct GNUNET_ATS_Session *s = cls;
506 s->recv_wakeup_task = NULL;
507 LOG (GNUNET_ERROR_TYPE_DEBUG,
508 "Session %p: Waking up PUT handle\n",
510 GNUNET_assert (s->server_recv->suspended);
511 MHD_resume_connection (s->server_recv->mhd_conn);
512 s->server_recv->suspended = false;
517 * Reschedule the execution of both IPv4 and IPv6 server.
519 * @param plugin the plugin
520 * @param server which server to schedule v4 or v6?
521 * @param now #GNUNET_YES to schedule execution immediately, #GNUNET_NO to wait
525 server_reschedule (struct HTTP_Server_Plugin *plugin,
526 struct MHD_Daemon *server,
531 * Deletes the session. Must not be used afterwards.
533 * @param s the session to delete
536 server_delete_session (struct GNUNET_ATS_Session *s)
538 struct HTTP_Server_Plugin *plugin = s->plugin;
539 struct HTTP_Message *msg;
541 if (NULL != s->timeout_task)
543 GNUNET_SCHEDULER_cancel (s->timeout_task);
544 s->timeout_task = NULL;
545 s->timeout = GNUNET_TIME_UNIT_ZERO_ABS;
547 if (NULL != s->recv_wakeup_task)
549 GNUNET_SCHEDULER_cancel (s->recv_wakeup_task);
550 s->recv_wakeup_task = NULL;
551 if (NULL != s->server_recv)
553 GNUNET_assert (s->server_recv->suspended);
554 s->server_recv->suspended = false;
555 MHD_resume_connection (s->server_recv->mhd_conn);
558 GNUNET_assert (GNUNET_OK ==
559 GNUNET_CONTAINER_multipeermap_remove (plugin->sessions,
562 while (NULL != (msg = s->msg_head))
564 GNUNET_CONTAINER_DLL_remove (s->msg_head,
567 if (NULL != msg->transmit_cont)
568 msg->transmit_cont (msg->transmit_cont_cls,
572 msg->pos + msg->overhead);
573 GNUNET_assert (s->msgs_in_queue > 0);
575 GNUNET_assert (s->bytes_in_queue >= msg->size);
576 s->bytes_in_queue -= msg->size;
580 GNUNET_assert (0 == s->msgs_in_queue);
581 GNUNET_assert (0 == s->bytes_in_queue);
583 if (NULL != s->server_send)
585 LOG (GNUNET_ERROR_TYPE_DEBUG,
586 "Server: %p / %p Terminating inbound PUT session to peer `%s'\n",
588 GNUNET_i2s (&s->target));
589 s->server_send->session = NULL;
590 MHD_set_connection_option (s->server_send->mhd_conn,
591 MHD_CONNECTION_OPTION_TIMEOUT,
592 1 /* 0 = no timeout, so this is MIN */);
593 if (s->server_send->suspended)
595 s->server_send->suspended = false;
596 MHD_resume_connection (s->server_send->mhd_conn);
598 server_reschedule (plugin,
599 s->server_send->mhd_daemon,
603 if (NULL != s->server_recv)
605 LOG (GNUNET_ERROR_TYPE_DEBUG,
606 "Server: %p / %p Terminating inbound GET session to peer `%s'\n",
607 s, s->server_recv, GNUNET_i2s (&s->target));
608 s->server_recv->session = NULL;
609 MHD_set_connection_option (s->server_recv->mhd_conn,
610 MHD_CONNECTION_OPTION_TIMEOUT,
611 1 /* 0 = no timeout, so this is MIN */);
612 server_reschedule (plugin,
613 s->server_recv->mhd_daemon,
616 notify_session_monitor (plugin,
618 GNUNET_TRANSPORT_SS_DONE);
619 if (GNUNET_YES == s->known_to_service)
621 plugin->env->session_end (plugin->env->cls,
624 s->known_to_service = GNUNET_NO;
626 if (NULL != s->msg_tk)
628 GNUNET_MST_destroy (s->msg_tk);
631 GNUNET_HELLO_address_free (s->address);
632 LOG (GNUNET_ERROR_TYPE_DEBUG,
633 "Session %p destroyed\n",
640 * Disconnect session @a s by telling MHD to close the
641 * connections (reducing timeout, etc.).
643 * @param cls closure with the `struct HTTP_Server_Plugin`
644 * @param s the session
645 * @return #GNUNET_OK on success
648 http_server_plugin_disconnect_session (void *cls,
649 struct GNUNET_ATS_Session *s)
651 server_delete_session (s);
657 * Session was idle, so disconnect it
659 * @param cls the session
662 server_session_timeout (void *cls)
664 struct GNUNET_ATS_Session *s = cls;
665 struct GNUNET_TIME_Relative left;
667 s->timeout_task = NULL;
668 left = GNUNET_TIME_absolute_get_remaining (s->timeout);
669 if (0 != left.rel_value_us)
671 /* not actually our turn yet, but let's at least update
672 the monitor, it may think we're about to die ... */
673 notify_session_monitor (s->plugin,
675 GNUNET_TRANSPORT_SS_UP);
676 s->timeout_task = GNUNET_SCHEDULER_add_delayed (left,
677 &server_session_timeout,
681 GNUNET_log (TIMEOUT_LOG,
682 "Session %p was idle for %s, disconnecting\n",
684 GNUNET_STRINGS_relative_time_to_string (
685 HTTP_SERVER_SESSION_TIMEOUT,
687 server_delete_session (s);
692 * Increment session timeout due to activity session @a s
694 * @param s the session
697 server_reschedule_session_timeout (struct GNUNET_ATS_Session *s)
699 GNUNET_assert (NULL != s->timeout_task);
700 s->timeout = GNUNET_TIME_relative_to_absolute (
701 GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
706 * Function that can be used by the transport service to transmit
707 * a message using the plugin. Note that in the case of a
708 * peer disconnecting, the continuation MUST be called
709 * prior to the disconnect notification itself. This function
710 * will be called with this peer's HELLO message to initiate
711 * a fresh connection to another peer.
714 * @param session which session must be used
715 * @param msgbuf the message to transmit
716 * @param msgbuf_size number of bytes in @a msgbuf
717 * @param priority how important is the message (most plugins will
718 * ignore message priority and just FIFO)
719 * @param to how long to wait at most for the transmission (does not
720 * require plugins to discard the message after the timeout,
721 * just advisory for the desired delay; most plugins will ignore
723 * @param cont continuation to call once the message has
724 * been transmitted (or if the transport is ready
725 * for the next transmission call; or if the
726 * peer disconnected...); can be NULL
727 * @param cont_cls closure for @a cont
728 * @return number of bytes used (on the physical network, with overheads);
729 * -1 on hard errors (i.e. address invalid); 0 is a legal value
730 * and does NOT mean that the message was not transmitted (DV)
733 http_server_plugin_send (void *cls,
734 struct GNUNET_ATS_Session *session,
737 unsigned int priority,
738 struct GNUNET_TIME_Relative to,
739 GNUNET_TRANSPORT_TransmitContinuation cont,
742 struct HTTP_Server_Plugin *plugin = cls;
743 struct HTTP_Message *msg;
744 ssize_t bytes_sent = 0;
747 LOG (GNUNET_ERROR_TYPE_DEBUG,
748 "Session %p/request %p: Sending message with %u to peer `%s'\n",
750 session->server_send,
752 GNUNET_i2s (&session->target));
754 /* create new message and schedule */
755 bytes_sent = sizeof(struct HTTP_Message) + msgbuf_size;
756 msg = GNUNET_malloc (bytes_sent);
758 msg->size = msgbuf_size;
760 msg->buf = (char *) &msg[1];
761 msg->transmit_cont = cont;
762 msg->transmit_cont_cls = cont_cls;
763 GNUNET_memcpy (msg->buf,
766 GNUNET_CONTAINER_DLL_insert_tail (session->msg_head,
769 session->msgs_in_queue++;
770 session->bytes_in_queue += msg->size;
771 notify_session_monitor (plugin,
773 GNUNET_TRANSPORT_SS_UP);
774 GNUNET_asprintf (&stat_txt,
775 "# bytes currently in %s_server buffers",
777 GNUNET_STATISTICS_update (plugin->env->stats,
778 stat_txt, msgbuf_size, GNUNET_NO);
779 GNUNET_free (stat_txt);
781 if (NULL != session->server_send)
783 if (session->server_send->suspended)
785 MHD_resume_connection (session->server_send->mhd_conn);
786 session->server_send->suspended = false;
788 server_reschedule (session->plugin,
789 session->server_send->mhd_daemon,
797 * Terminate session during shutdown.
799 * @param cls the `struct HTTP_Server_Plugin *`
800 * @param peer for which this is a session
801 * @param value the `struct GNUNET_ATS_Session` to clean up
802 * @return #GNUNET_OK (continue to iterate)
805 destroy_session_shutdown_cb (void *cls,
806 const struct GNUNET_PeerIdentity *peer,
809 struct GNUNET_ATS_Session *s = value;
810 struct ServerRequest *sc_send;
811 struct ServerRequest *sc_recv;
813 sc_send = s->server_send;
814 sc_recv = s->server_recv;
815 server_delete_session (s);
817 sc_send->session = NULL;
819 sc_recv->session = NULL;
828 * @param cls the `struct HTTP_Server_Plugin *`
829 * @param peer for which this is a session
830 * @param value the `struct GNUNET_ATS_Session` to clean up
831 * @return #GNUNET_OK (continue to iterate)
834 destroy_session_cb (void *cls,
835 const struct GNUNET_PeerIdentity *peer,
838 struct GNUNET_ATS_Session *s = value;
840 server_delete_session (s);
846 * Function that can be used to force the plugin to disconnect
847 * from the given peer and cancel all previous transmissions
848 * (and their continuationc).
851 * @param target peer from which to disconnect
854 http_server_plugin_disconnect_peer (void *cls,
855 const struct GNUNET_PeerIdentity *target)
857 struct HTTP_Server_Plugin *plugin = cls;
859 LOG (GNUNET_ERROR_TYPE_DEBUG,
860 "Transport tells me to disconnect `%s'\n",
861 GNUNET_i2s (target));
862 GNUNET_CONTAINER_multipeermap_get_multiple (plugin->sessions,
870 * Another peer has suggested an address for this
871 * peer and transport plugin. Check that this could be a valid
872 * address. If so, consider adding it to the list
876 * @param addr pointer to the address
877 * @param addrlen length of @a addr
878 * @return #GNUNET_OK if this is a plausible address for this peer
882 http_server_plugin_address_suggested (void *cls,
886 struct HTTP_Server_Plugin *plugin = cls;
887 struct HttpAddressWrapper *next;
888 struct HttpAddressWrapper *pos;
889 const struct HttpAddress *haddr = addr;
891 if ((NULL != plugin->ext_addr) &&
892 (GNUNET_YES == (http_common_cmp_addresses (addr, addrlen,
893 plugin->ext_addr->address,
897 /* Checking HTTP_OPTIONS_VERIFY_CERTIFICATE option for external hostname */
898 if ((ntohl (haddr->options) & HTTP_OPTIONS_VERIFY_CERTIFICATE) !=
899 (plugin->options & HTTP_OPTIONS_VERIFY_CERTIFICATE))
900 return GNUNET_NO; /* VERIFY option not set as required! */
903 next = plugin->addr_head;
904 while (NULL != (pos = next))
907 if (GNUNET_YES == (http_common_cmp_addresses (addr,
918 * Creates a new outbound session the transport
919 * service will use to send data to the peer.
921 * Since HTTP/S server cannot create sessions, always returns NULL.
923 * @param cls the plugin
924 * @param address the address
925 * @return always NULL
927 static struct GNUNET_ATS_Session *
928 http_server_plugin_get_session (void *cls,
929 const struct GNUNET_HELLO_Address *address)
936 * Call MHD IPv4 to process pending requests and then go back
937 * and schedule the next run.
939 * @param cls plugin as closure
942 server_v4_run (void *cls)
944 struct HTTP_Server_Plugin *plugin = cls;
946 plugin->server_v4_task = NULL;
947 plugin->server_v4_immediately = GNUNET_NO;
948 GNUNET_assert (MHD_YES == MHD_run (plugin->server_v4));
949 server_reschedule (plugin, plugin->server_v4, GNUNET_NO);
954 * Call MHD IPv6 to process pending requests and then go back
955 * and schedule the next run.
957 * @param cls plugin as closure
960 server_v6_run (void *cls)
962 struct HTTP_Server_Plugin *plugin = cls;
964 plugin->server_v6_task = NULL;
965 plugin->server_v6_immediately = GNUNET_NO;
966 GNUNET_assert (MHD_YES == MHD_run (plugin->server_v6));
967 server_reschedule (plugin, plugin->server_v6, GNUNET_NO);
972 * Function that queries MHD's select sets and
973 * starts the task waiting for them.
975 * @param plugin plugin
976 * @param daemon_handle the MHD daemon handle
977 * @param now schedule now
978 * @return gnunet task identifier
980 static struct GNUNET_SCHEDULER_Task *
981 server_schedule (struct HTTP_Server_Plugin *plugin,
982 struct MHD_Daemon *daemon_handle,
985 struct GNUNET_SCHEDULER_Task *ret;
989 struct GNUNET_NETWORK_FDSet *wrs;
990 struct GNUNET_NETWORK_FDSet *wws;
992 MHD_UNSIGNED_LONG_LONG timeout;
993 static unsigned long long last_timeout = 0;
995 struct GNUNET_TIME_Relative tv;
997 if (GNUNET_YES == plugin->in_shutdown)
1004 wrs = GNUNET_NETWORK_fdset_create ();
1005 wws = GNUNET_NETWORK_fdset_create ();
1007 GNUNET_assert (MHD_YES ==
1008 MHD_get_fdset (daemon_handle,
1013 haveto = MHD_get_timeout (daemon_handle, &timeout);
1014 if (haveto == MHD_YES)
1016 if (timeout != last_timeout)
1018 LOG (GNUNET_ERROR_TYPE_DEBUG,
1019 "SELECT Timeout changed from %llu to %llu (ms)\n",
1020 last_timeout, timeout);
1021 last_timeout = timeout;
1023 if (timeout <= GNUNET_TIME_UNIT_SECONDS.rel_value_us / 1000LL)
1024 tv.rel_value_us = (uint64_t) timeout * 1000LL;
1026 tv = GNUNET_TIME_UNIT_SECONDS;
1029 tv = GNUNET_TIME_UNIT_SECONDS;
1030 /* Force immediate run, since we have outbound data to send */
1031 if (now == GNUNET_YES)
1032 tv = GNUNET_TIME_UNIT_MILLISECONDS;
1033 GNUNET_NETWORK_fdset_copy_native (wrs, &rs, max + 1);
1034 GNUNET_NETWORK_fdset_copy_native (wws, &ws, max + 1);
1036 if (daemon_handle == plugin->server_v4)
1038 if (plugin->server_v4_task != NULL)
1040 GNUNET_SCHEDULER_cancel (plugin->server_v4_task);
1041 plugin->server_v4_task = NULL;
1044 LOG (GNUNET_ERROR_TYPE_DEBUG,
1045 "Scheduling IPv4 server task in %llu ms\n",
1049 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1051 &server_v4_run, plugin);
1053 if (daemon_handle == plugin->server_v6)
1055 if (plugin->server_v6_task != NULL)
1057 GNUNET_SCHEDULER_cancel (plugin->server_v6_task);
1058 plugin->server_v6_task = NULL;
1061 LOG (GNUNET_ERROR_TYPE_DEBUG,
1062 "Scheduling IPv6 server task in %llu ms\n", tv);
1065 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1067 &server_v6_run, plugin);
1069 GNUNET_NETWORK_fdset_destroy (wrs);
1070 GNUNET_NETWORK_fdset_destroy (wws);
1076 * Reschedule the execution of both IPv4 and IPv6 server
1078 * @param plugin the plugin
1079 * @param server which server to schedule v4 or v6?
1080 * @param now #GNUNET_YES to schedule execution immediately, #GNUNET_NO to wait
1084 server_reschedule (struct HTTP_Server_Plugin *plugin,
1085 struct MHD_Daemon *server,
1088 if ((server == plugin->server_v4) && (plugin->server_v4 != NULL))
1090 if (GNUNET_YES == plugin->server_v4_immediately)
1091 return; /* No rescheduling, server will run asap */
1093 if (GNUNET_YES == now)
1094 plugin->server_v4_immediately = GNUNET_YES;
1096 if (plugin->server_v4_task != NULL)
1098 GNUNET_SCHEDULER_cancel (plugin->server_v4_task);
1099 plugin->server_v4_task = NULL;
1101 plugin->server_v4_task = server_schedule (plugin, plugin->server_v4, now);
1104 if ((server == plugin->server_v6) && (plugin->server_v6 != NULL))
1106 if (GNUNET_YES == plugin->server_v6_immediately)
1107 return; /* No rescheduling, server will run asap */
1109 if (GNUNET_YES == now)
1110 plugin->server_v6_immediately = GNUNET_YES;
1112 if (plugin->server_v6_task != NULL)
1114 GNUNET_SCHEDULER_cancel (plugin->server_v6_task);
1115 plugin->server_v6_task = NULL;
1117 plugin->server_v6_task = server_schedule (plugin, plugin->server_v6, now);
1123 * Function that is called to get the keepalive factor.
1124 * GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT is divided by this number to
1125 * calculate the interval between keepalive packets.
1127 * @param cls closure with the `struct HTTP_Server_Plugin`
1128 * @return keepalive factor
1131 http_server_query_keepalive_factor (void *cls)
1138 * Function that will be called whenever the transport service wants to
1139 * notify the plugin that a session is still active and in use and
1140 * therefore the session timeout for this session has to be updated
1142 * @param cls closure
1143 * @param peer which peer was the session for
1144 * @param session which session is being updated
1147 http_server_plugin_update_session_timeout (void *cls,
1149 GNUNET_PeerIdentity *peer,
1150 struct GNUNET_ATS_Session *session)
1152 server_reschedule_session_timeout (session);
1157 * Tell MHD that the connection should timeout after @a to seconds.
1159 * @param plugin our plugin
1160 * @param s session for which the timeout changes
1161 * @param to timeout in seconds
1164 server_mhd_connection_timeout (struct HTTP_Server_Plugin *plugin,
1165 struct GNUNET_ATS_Session *s,
1168 /* Setting timeouts for other connections */
1169 if (NULL != s->server_recv)
1171 LOG (GNUNET_ERROR_TYPE_DEBUG,
1172 "Setting timeout for %p to %u sec.\n",
1173 s->server_recv, to);
1174 MHD_set_connection_option (s->server_recv->mhd_conn,
1175 MHD_CONNECTION_OPTION_TIMEOUT,
1177 server_reschedule (plugin, s->server_recv->mhd_daemon, GNUNET_NO);
1179 if (NULL != s->server_send)
1181 LOG (GNUNET_ERROR_TYPE_DEBUG,
1182 "Setting timeout for %p to %u sec.\n",
1183 s->server_send, to);
1184 MHD_set_connection_option (s->server_send->mhd_conn,
1185 MHD_CONNECTION_OPTION_TIMEOUT,
1187 server_reschedule (plugin, s->server_send->mhd_daemon, GNUNET_NO);
1193 * Parse incoming URL for tag and target
1195 * @param plugin plugin
1196 * @param url incoming url
1197 * @param target where to store the target
1198 * @param tag where to store the tag
1199 * @param options where to store the options
1200 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
1203 server_parse_url (struct HTTP_Server_Plugin *plugin,
1205 struct GNUNET_PeerIdentity *target,
1209 regmatch_t matches[4];
1210 const char *tag_start;
1211 const char *target_start;
1215 unsigned long int rc;
1219 ("^.*/([0-9A-Z]+);([0-9]+)(,[0-9]+)?$")
1224 return GNUNET_SYSERR;
1227 if (regexec (&plugin->url_regex, url, 4, matches, 0))
1229 LOG (GNUNET_ERROR_TYPE_DEBUG,
1230 "URL `%s' did not match regex\n", url);
1231 return GNUNET_SYSERR;
1234 target_start = &url[matches[1].rm_so];
1235 tag_start = &url[matches[2].rm_so];
1238 rc = strtoul (tag_start, &tag_end, 10);
1239 if (&url[matches[2].rm_eo] != tag_end)
1241 LOG (GNUNET_ERROR_TYPE_DEBUG,
1242 "URL tag did not line up with submatch\n");
1243 return GNUNET_SYSERR;
1247 LOG (GNUNET_ERROR_TYPE_DEBUG,
1248 "URL tag is zero\n");
1249 return GNUNET_SYSERR;
1251 if ((rc == ULONG_MAX) && (ERANGE == errno))
1253 LOG (GNUNET_ERROR_TYPE_DEBUG,
1254 "URL tag > ULONG_MAX\n");
1255 return GNUNET_SYSERR;
1257 if (rc > UINT32_MAX)
1259 LOG (GNUNET_ERROR_TYPE_DEBUG,
1260 "URL tag > UINT32_MAX\n");
1261 return GNUNET_SYSERR;
1263 (*tag) = (uint32_t) rc;
1264 LOG (GNUNET_ERROR_TYPE_DEBUG,
1265 "Found tag `%u' in url\n",
1268 /* convert peer id */
1269 hash_length = matches[1].rm_eo - matches[1].rm_so;
1270 if (hash_length != plugin->peer_id_length)
1272 LOG (GNUNET_ERROR_TYPE_DEBUG,
1273 "URL target is %u bytes, expecting %u\n",
1274 hash_length, plugin->peer_id_length);
1275 return GNUNET_SYSERR;
1278 GNUNET_CRYPTO_eddsa_public_key_from_string (target_start,
1280 &target->public_key))
1282 LOG (GNUNET_ERROR_TYPE_DEBUG,
1283 "URL target conversion failed\n");
1284 return GNUNET_SYSERR;
1286 LOG (GNUNET_ERROR_TYPE_DEBUG,
1287 "Found target `%s' in URL\n",
1288 GNUNET_i2s_full (target));
1290 /* convert options */
1291 if (-1 == matches[3].rm_so)
1297 rc = strtoul (&url[matches[3].rm_so + 1], &options_end, 10);
1298 if (&url[matches[3].rm_eo] != options_end)
1300 LOG (GNUNET_ERROR_TYPE_DEBUG,
1301 "URL options did not line up with submatch\n");
1302 return GNUNET_SYSERR;
1304 if ((rc == ULONG_MAX) && (ERANGE == errno))
1306 LOG (GNUNET_ERROR_TYPE_DEBUG,
1307 "URL options > ULONG_MAX\n");
1308 return GNUNET_SYSERR;
1310 if (rc > UINT32_MAX)
1312 LOG (GNUNET_ERROR_TYPE_DEBUG,
1313 "URL options > UINT32_MAX\n");
1314 return GNUNET_SYSERR;
1316 (*options) = (uint32_t) rc;
1317 LOG (GNUNET_ERROR_TYPE_DEBUG,
1318 "Found options `%u' in url\n",
1326 * Closure for #session_tag_it().
1328 struct GNUNET_ATS_SessionTagContext
1331 * Set to session matching the tag.
1333 struct GNUNET_ATS_Session *res;
1336 * Tag we are looking for.
1343 * Find a session with a matching tag.
1345 * @param cls the `struct GNUNET_ATS_SessionTagContext *`
1346 * @param key peer identity (unused)
1347 * @param value the `struct GNUNET_ATS_Session *`
1348 * @return #GNUNET_NO if we found the session, #GNUNET_OK if not
1351 session_tag_it (void *cls,
1352 const struct GNUNET_PeerIdentity *key,
1355 struct GNUNET_ATS_SessionTagContext *stc = cls;
1356 struct GNUNET_ATS_Session *s = value;
1358 if (s->tag == stc->tag)
1368 * Lookup a mhd connection and create one if none is found
1370 * @param plugin the plugin handle
1371 * @param mhd_connection the incoming mhd_connection
1372 * @param url incoming requested URL
1373 * @param method PUT or GET
1374 * @return the server connecetion
1376 static struct ServerRequest *
1377 server_lookup_connection (struct HTTP_Server_Plugin *plugin,
1378 struct MHD_Connection *mhd_connection,
1382 struct GNUNET_ATS_Session *s = NULL;
1383 struct ServerRequest *sc = NULL;
1384 const union MHD_ConnectionInfo *conn_info;
1385 struct HttpAddress *addr;
1386 struct GNUNET_PeerIdentity target;
1388 struct GNUNET_ATS_SessionTagContext stc;
1390 int direction = GNUNET_SYSERR;
1392 enum GNUNET_NetworkType scope;
1394 conn_info = MHD_get_connection_info (mhd_connection,
1395 MHD_CONNECTION_INFO_CLIENT_ADDRESS);
1396 if ((conn_info->client_addr->sa_family != AF_INET) &&
1397 (conn_info->client_addr->sa_family != AF_INET6))
1399 LOG (GNUNET_ERROR_TYPE_DEBUG,
1400 "New %s request from %s\n",
1404 options = 0; /* make gcc happy */
1405 if (GNUNET_SYSERR ==
1406 server_parse_url (plugin, url, &target, &stc.tag, &options))
1408 LOG (GNUNET_ERROR_TYPE_DEBUG,
1409 "Invalid url %s\n", url);
1412 if (0 == strcmp (MHD_HTTP_METHOD_PUT, method))
1413 direction = _RECEIVE;
1414 else if (0 == strcmp (MHD_HTTP_METHOD_GET, method))
1418 LOG (GNUNET_ERROR_TYPE_DEBUG,
1419 "Invalid method %s for request from %s\n",
1424 plugin->cur_request++;
1425 LOG (GNUNET_ERROR_TYPE_DEBUG,
1426 "New %s request from %s with tag %u (%u of %u)\n",
1428 GNUNET_i2s (&target),
1430 plugin->cur_request, plugin->max_request);
1431 /* find existing session */
1433 GNUNET_CONTAINER_multipeermap_get_multiple (plugin->sessions,
1437 if (NULL == (s = stc.res))
1439 /* create new session */
1441 switch (conn_info->client_addr->sa_family)
1444 addr = http_common_address_from_socket (plugin->protocol,
1445 conn_info->client_addr,
1446 sizeof(struct sockaddr_in));
1447 addr_len = http_common_address_get_size (addr);
1448 scope = plugin->env->get_address_type (plugin->env->cls,
1449 conn_info->client_addr,
1450 sizeof(struct sockaddr_in));
1454 addr = http_common_address_from_socket (plugin->protocol,
1455 conn_info->client_addr,
1456 sizeof(struct sockaddr_in6));
1457 addr_len = http_common_address_get_size (addr);
1458 scope = plugin->env->get_address_type (plugin->env->cls,
1459 conn_info->client_addr,
1460 sizeof(struct sockaddr_in6));
1464 /* external host name */
1467 s = GNUNET_new (struct GNUNET_ATS_Session);
1471 s->address = GNUNET_HELLO_address_allocate (&s->target,
1475 GNUNET_HELLO_ADDRESS_INFO_INBOUND);
1476 s->next_receive = GNUNET_TIME_UNIT_ZERO_ABS;
1478 s->timeout = GNUNET_TIME_relative_to_absolute (HTTP_SERVER_SESSION_TIMEOUT);
1479 s->timeout_task = GNUNET_SCHEDULER_add_delayed (HTTP_SERVER_SESSION_TIMEOUT,
1480 &server_session_timeout,
1482 (void) GNUNET_CONTAINER_multipeermap_put (plugin->sessions,
1485 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
1486 notify_session_monitor (plugin,
1488 GNUNET_TRANSPORT_SS_INIT);
1489 notify_session_monitor (plugin,
1491 GNUNET_TRANSPORT_SS_HANDSHAKE);
1492 LOG (GNUNET_ERROR_TYPE_DEBUG,
1493 "Creating new session %p for peer `%s' connecting from `%s'\n",
1494 s, GNUNET_i2s (&target),
1495 http_common_plugin_address_to_string (plugin->protocol,
1498 GNUNET_free_non_null (addr);
1501 if ((_RECEIVE == direction) &&
1502 (NULL != s->server_recv))
1504 LOG (GNUNET_ERROR_TYPE_DEBUG,
1505 "Duplicate PUT request from `%s' tag %u, dismissing new request\n",
1506 GNUNET_i2s (&target),
1510 if ((_SEND == direction) && (NULL != s->server_send))
1512 LOG (GNUNET_ERROR_TYPE_DEBUG,
1513 "Duplicate GET request from `%s' tag %u, dismissing new request\n",
1514 GNUNET_i2s (&target),
1518 sc = GNUNET_new (struct ServerRequest);
1519 if (conn_info->client_addr->sa_family == AF_INET)
1520 sc->mhd_daemon = plugin->server_v4;
1521 if (conn_info->client_addr->sa_family == AF_INET6)
1522 sc->mhd_daemon = plugin->server_v6;
1523 sc->mhd_conn = mhd_connection;
1524 sc->direction = direction;
1525 sc->connected = GNUNET_NO;
1527 sc->options = options;
1528 if (direction == _SEND)
1530 s->server_send = sc;
1532 if (direction == _RECEIVE)
1534 s->server_recv = sc;
1537 if ((GNUNET_NO == s->known_to_service) &&
1538 (NULL != s->server_send) &&
1539 (NULL != s->server_recv))
1541 s->known_to_service = GNUNET_YES;
1542 notify_session_monitor (plugin,
1544 GNUNET_TRANSPORT_SS_UP);
1545 plugin->env->session_start (plugin->env->cls,
1551 to = (HTTP_SERVER_SESSION_TIMEOUT.rel_value_us / 1000LL / 1000LL);
1552 server_mhd_connection_timeout (plugin, s, to);
1558 * Callback called by MHD when it needs data to send
1560 * @param cls current session
1561 * @param pos position in buffer
1562 * @param buf the buffer to write data to
1563 * @param max max number of bytes available in @a buf
1564 * @return bytes written to @a buf
1567 server_send_callback (void *cls,
1572 struct ServerRequest *sc = cls;
1573 struct GNUNET_ATS_Session *s = sc->session;
1574 ssize_t bytes_read = 0;
1575 struct HTTP_Message *msg;
1580 /* session is disconnecting */
1584 sc = s->server_send;
1591 bytes_read = GNUNET_MIN (msg->size - msg->pos,
1593 GNUNET_memcpy (buf, &msg->buf[msg->pos], bytes_read);
1594 msg->pos += bytes_read;
1596 /* removing message */
1597 if (msg->pos == msg->size)
1599 GNUNET_CONTAINER_DLL_remove (s->msg_head,
1602 if (NULL != msg->transmit_cont)
1603 msg->transmit_cont (msg->transmit_cont_cls, &s->target, GNUNET_OK,
1604 msg->size, msg->size + msg->overhead);
1605 GNUNET_assert (s->msgs_in_queue > 0);
1607 GNUNET_assert (s->bytes_in_queue >= msg->size);
1608 s->bytes_in_queue -= msg->size;
1610 notify_session_monitor (s->plugin,
1612 GNUNET_TRANSPORT_SS_UPDATE);
1617 sc->connected = GNUNET_YES;
1618 LOG (GNUNET_ERROR_TYPE_DEBUG,
1619 "Sent %u bytes to peer `%s' with session %p \n",
1621 GNUNET_i2s (&s->target),
1623 GNUNET_asprintf (&stat_txt,
1624 "# bytes currently in %s_server buffers",
1625 s->plugin->protocol);
1626 GNUNET_STATISTICS_update (s->plugin->env->stats,
1630 GNUNET_free (stat_txt);
1631 GNUNET_asprintf (&stat_txt,
1632 "# bytes transmitted via %s_server",
1633 s->plugin->protocol);
1634 GNUNET_STATISTICS_update (s->plugin->env->stats,
1635 stat_txt, bytes_read, GNUNET_NO);
1636 GNUNET_free (stat_txt);
1638 else if ((sc->options & OPTION_LONG_POLL) && sc->connected)
1640 LOG (GNUNET_ERROR_TYPE_DEBUG,
1641 "Completing GET response to peer `%s' with session %p\n",
1642 GNUNET_i2s (&s->target),
1644 return MHD_CONTENT_READER_END_OF_STREAM;
1648 MHD_suspend_connection (s->server_send->mhd_conn);
1649 s->server_send->suspended = true;
1657 * Callback called by MessageStreamTokenizer when a message has arrived
1659 * @param cls current session as closure
1660 * @param message the message to be forwarded to transport service
1661 * @return #GNUNET_OK (all OK)
1664 server_receive_mst_cb (void *cls,
1665 const struct GNUNET_MessageHeader *message)
1667 struct GNUNET_ATS_Session *s = cls;
1668 struct HTTP_Server_Plugin *plugin = s->plugin;
1669 struct GNUNET_TIME_Relative delay;
1672 if (GNUNET_NO == s->known_to_service)
1674 s->known_to_service = GNUNET_YES;
1675 plugin->env->session_start (plugin->env->cls,
1679 notify_session_monitor (plugin,
1681 GNUNET_TRANSPORT_SS_UP);
1683 delay = plugin->env->receive (plugin->env->cls,
1687 GNUNET_asprintf (&stat_txt,
1688 "# bytes received via %s_server",
1690 GNUNET_STATISTICS_update (plugin->env->stats,
1692 ntohs (message->size),
1694 GNUNET_free (stat_txt);
1695 s->next_receive = GNUNET_TIME_relative_to_absolute (delay);
1696 if (delay.rel_value_us > 0)
1698 LOG (GNUNET_ERROR_TYPE_DEBUG,
1699 "Peer `%s' address `%s' next read delayed for %s\n",
1700 GNUNET_i2s (&s->target),
1701 http_common_plugin_address_to_string (plugin->protocol,
1702 s->address->address,
1703 s->address->address_length),
1704 GNUNET_STRINGS_relative_time_to_string (delay,
1707 server_reschedule_session_timeout (s);
1713 * Add headers to a request indicating that we allow Cross-Origin Resource
1716 * @param response response object to modify
1719 add_cors_headers (struct MHD_Response *response)
1721 MHD_add_response_header (response,
1722 "Access-Control-Allow-Origin",
1724 MHD_add_response_header (response,
1725 "Access-Control-Allow-Methods",
1726 "GET, PUT, OPTIONS");
1727 MHD_add_response_header (response,
1728 "Access-Control-Max-Age",
1734 * MHD callback for a new incoming connection
1736 * @param cls the plugin handle
1737 * @param mhd_connection the mhd connection
1738 * @param url the requested URL
1739 * @param method GET or PUT
1740 * @param version HTTP version
1741 * @param upload_data upload data
1742 * @param upload_data_size size of @a upload_data
1743 * @param httpSessionCache the session cache to remember the connection
1744 * @return MHD_YES if connection is accepted, MHD_NO on reject
1747 server_access_cb (void *cls,
1748 struct MHD_Connection *mhd_connection,
1751 const char *version,
1752 const char *upload_data,
1753 size_t *upload_data_size,
1754 void **httpSessionCache)
1756 struct HTTP_Server_Plugin *plugin = cls;
1757 struct ServerRequest *sc = *httpSessionCache;
1758 struct GNUNET_ATS_Session *s;
1759 struct MHD_Response *response;
1762 LOG (GNUNET_ERROR_TYPE_DEBUG,
1764 "Access from connection %p (%u of %u) for `%s' `%s' url `%s' with upload data size %u\n"),
1766 plugin->cur_request,
1767 plugin->max_request,
1771 (*upload_data_size));
1774 /* CORS pre-flight request */
1775 if (0 == strcmp (MHD_HTTP_METHOD_OPTIONS, method))
1777 response = MHD_create_response_from_buffer (0, NULL,
1778 MHD_RESPMEM_PERSISTENT);
1779 add_cors_headers (response);
1780 res = MHD_queue_response (mhd_connection, MHD_HTTP_OK, response);
1781 MHD_destroy_response (response);
1784 /* new connection */
1785 sc = server_lookup_connection (plugin, mhd_connection, url, method);
1788 /* attach to new / existing session */
1789 (*httpSessionCache) = sc;
1793 /* existing session already has matching connection, refuse */
1794 response = MHD_create_response_from_buffer (strlen (HTTP_ERROR_RESPONSE),
1795 HTTP_ERROR_RESPONSE,
1796 MHD_RESPMEM_PERSISTENT);
1797 MHD_add_response_header (response,
1798 MHD_HTTP_HEADER_CONTENT_TYPE,
1800 add_cors_headers (response);
1801 res = MHD_queue_response (mhd_connection, MHD_HTTP_NOT_FOUND, response);
1802 MHD_destroy_response (response);
1806 /* 'old' connection */
1807 if (NULL == (s = sc->session))
1809 /* Session was already disconnected;
1810 sent HTTP/1.1: 200 OK as response */
1811 response = MHD_create_response_from_buffer (strlen ("Thank you!"),
1813 MHD_RESPMEM_PERSISTENT);
1814 add_cors_headers (response);
1815 MHD_queue_response (mhd_connection, MHD_HTTP_OK, response);
1816 MHD_destroy_response (response);
1820 if (sc->direction == _SEND)
1822 response = MHD_create_response_from_callback (MHD_SIZE_UNKNOWN, 32 * 1024,
1823 &server_send_callback, sc,
1825 add_cors_headers (response);
1826 MHD_queue_response (mhd_connection, MHD_HTTP_OK, response);
1827 MHD_destroy_response (response);
1830 if (sc->direction == _RECEIVE)
1832 if ((*upload_data_size == 0) && (sc->connected == GNUNET_NO))
1834 /* (*upload_data_size == 0) first callback when header are passed */
1835 LOG (GNUNET_ERROR_TYPE_DEBUG,
1836 "Session %p / Connection %p: Peer `%s' PUT on address `%s' connected\n",
1838 GNUNET_i2s (&s->target),
1839 http_common_plugin_address_to_string (plugin->protocol,
1840 s->address->address,
1841 s->address->address_length));
1842 sc->connected = GNUNET_YES;
1845 else if ((*upload_data_size == 0) && (sc->connected == GNUNET_YES))
1847 /* (*upload_data_size == 0) when upload is complete */
1848 LOG (GNUNET_ERROR_TYPE_DEBUG,
1849 "Session %p / Connection %p: Peer `%s' PUT on address `%s' finished upload\n",
1851 GNUNET_i2s (&s->target),
1852 http_common_plugin_address_to_string (plugin->protocol,
1853 s->address->address,
1854 s->address->address_length));
1855 sc->connected = GNUNET_NO;
1856 /* Sent HTTP/1.1: 200 OK as PUT Response\ */
1857 response = MHD_create_response_from_buffer (strlen ("Thank you!"),
1859 MHD_RESPMEM_PERSISTENT);
1860 add_cors_headers (response);
1861 MHD_queue_response (mhd_connection, MHD_HTTP_OK, response);
1862 MHD_destroy_response (response);
1865 else if ((*upload_data_size > 0) && (sc->connected == GNUNET_YES))
1867 struct GNUNET_TIME_Relative delay;
1869 /* (*upload_data_size > 0) for every segment received */
1870 LOG (GNUNET_ERROR_TYPE_DEBUG,
1871 "Session %p / Connection %p: Peer `%s' PUT on address `%s' received %u bytes\n",
1873 GNUNET_i2s (&s->target),
1874 http_common_plugin_address_to_string (plugin->protocol,
1875 s->address->address,
1876 s->address->address_length),
1878 delay = GNUNET_TIME_absolute_get_remaining (s->next_receive);
1879 if (0 == delay.rel_value_us)
1881 LOG (GNUNET_ERROR_TYPE_DEBUG,
1882 "PUT with %u bytes forwarded to MST\n",
1884 if (s->msg_tk == NULL)
1886 s->msg_tk = GNUNET_MST_create (&server_receive_mst_cb,
1889 GNUNET_MST_from_buffer (s->msg_tk,
1892 GNUNET_NO, GNUNET_NO);
1893 server_mhd_connection_timeout (plugin, s,
1894 GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT.
1895 rel_value_us / 1000LL
1897 (*upload_data_size) = 0;
1901 /* delay processing */
1902 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1903 "Session %p / Connection %p: no inbound bandwidth available! Next read was delayed by %s\n",
1906 GNUNET_STRINGS_relative_time_to_string (delay,
1908 GNUNET_assert (s->server_recv->mhd_conn == mhd_connection);
1909 MHD_suspend_connection (s->server_recv->mhd_conn);
1910 s->server_recv->suspended = true;
1911 if (NULL == s->recv_wakeup_task)
1913 = GNUNET_SCHEDULER_add_delayed (delay,
1930 * Callback from MHD when a connection disconnects
1932 * @param cls closure with the `struct HTTP_Server_Plugin *`
1933 * @param connection the disconnected MHD connection
1934 * @param httpSessionCache the pointer to distinguish
1937 server_disconnect_cb (void *cls,
1938 struct MHD_Connection *connection,
1939 void **httpSessionCache)
1941 struct HTTP_Server_Plugin *plugin = cls;
1942 struct ServerRequest *sc = *httpSessionCache;
1944 LOG (GNUNET_ERROR_TYPE_DEBUG,
1945 "Disconnect for connection %p\n",
1949 /* CORS pre-flight request finished */
1953 if (NULL != sc->session)
1955 if (sc->direction == _SEND)
1957 LOG (GNUNET_ERROR_TYPE_DEBUG,
1958 "Peer `%s' connection %p, GET on address `%s' disconnected\n",
1959 GNUNET_i2s (&sc->session->target),
1960 sc->session->server_send,
1961 http_common_plugin_address_to_string (plugin->protocol,
1962 sc->session->address->address,
1963 sc->session->address->
1966 sc->session->server_send = NULL;
1968 else if (sc->direction == _RECEIVE)
1970 LOG (GNUNET_ERROR_TYPE_DEBUG,
1971 "Peer `%s' connection %p PUT on address `%s' disconnected\n",
1972 GNUNET_i2s (&sc->session->target),
1973 sc->session->server_recv,
1974 http_common_plugin_address_to_string (plugin->protocol,
1975 sc->session->address->address,
1976 sc->session->address->
1978 sc->session->server_recv = NULL;
1979 if (NULL != sc->session->msg_tk)
1981 GNUNET_MST_destroy (sc->session->msg_tk);
1982 sc->session->msg_tk = NULL;
1987 plugin->cur_request--;
1992 * Callback from MHD when a connection starts/stops
1994 * @param cls closure with the `struct HTTP_Server_Plugin *`
1995 * @param connection connection handle
1996 * @param socket_context socket-specific pointer
1997 * @param toe reason for connection notification
1998 * @see #MHD_OPTION_NOTIFY_CONNECTION
2001 server_connection_cb (void *cls,
2002 struct MHD_Connection *connection,
2003 void **socket_context,
2004 enum MHD_ConnectionNotificationCode toe)
2006 struct HTTP_Server_Plugin *plugin = cls;
2007 const union MHD_ConnectionInfo *info;
2009 if (MHD_CONNECTION_NOTIFY_STARTED == toe)
2012 /* Reschedule to remove closed socket from our select set */
2013 info = MHD_get_connection_info (connection,
2014 MHD_CONNECTION_INFO_DAEMON);
2015 GNUNET_assert (NULL != info);
2016 server_reschedule (plugin,
2023 * Check if incoming connection is accepted.
2025 * @param cls plugin as closure
2026 * @param addr address of incoming connection
2027 * @param addr_len number of bytes in @a addr
2028 * @return MHD_YES if connection is accepted, MHD_NO if connection is rejected
2031 server_accept_cb (void *cls,
2032 const struct sockaddr *addr,
2035 struct HTTP_Server_Plugin *plugin = cls;
2037 if (plugin->cur_request <= plugin->max_request)
2039 LOG (GNUNET_ERROR_TYPE_DEBUG,
2040 _ ("Accepting connection (%u of %u) from `%s'\n"),
2041 plugin->cur_request, plugin->max_request,
2042 GNUNET_a2s (addr, addr_len));
2047 LOG (GNUNET_ERROR_TYPE_WARNING,
2049 "Server reached maximum number connections (%u), rejecting new connection\n"),
2050 plugin->max_request);
2057 * Log function called by MHD.
2060 * @param fmt format string
2061 * @param ap arguments for the format string (va_start() and va_end()
2062 * will be called by MHD)
2065 server_log (void *arg,
2075 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2083 * Load ssl certificate from file
2085 * @param file filename
2086 * @return content of the file
2089 server_load_file (const char *file)
2091 struct GNUNET_DISK_FileHandle *gn_file;
2095 if (GNUNET_OK != GNUNET_DISK_file_size (file,
2096 &fsize, GNUNET_NO, GNUNET_YES))
2098 text = GNUNET_malloc (fsize + 1);
2100 GNUNET_DISK_file_open (file, GNUNET_DISK_OPEN_READ,
2101 GNUNET_DISK_PERM_USER_READ);
2102 if (NULL == gn_file)
2107 if (GNUNET_SYSERR == GNUNET_DISK_file_read (gn_file, text, fsize))
2110 GNUNET_DISK_file_close (gn_file);
2114 GNUNET_DISK_file_close (gn_file);
2124 * Load ssl certificate
2126 * @param plugin the plugin
2127 * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
2130 server_load_certificate (struct HTTP_Server_Plugin *plugin)
2132 int res = GNUNET_OK;
2138 GNUNET_CONFIGURATION_get_value_filename (plugin->env->cfg,
2140 "KEY_FILE", &key_file))
2142 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
2143 plugin->name, "CERT_FILE");
2144 return GNUNET_SYSERR;
2147 GNUNET_CONFIGURATION_get_value_filename (plugin->env->cfg,
2149 "CERT_FILE", &cert_file))
2151 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
2152 plugin->name, "CERT_FILE");
2153 GNUNET_free (key_file);
2154 return GNUNET_SYSERR;
2156 /* Get crypto init string from config. If not present, use
2159 GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg,
2162 &plugin->crypto_init))
2163 LOG (GNUNET_ERROR_TYPE_DEBUG,
2164 "Using crypto init string `%s'\n",
2165 plugin->crypto_init);
2167 LOG (GNUNET_ERROR_TYPE_DEBUG,
2168 "Using default crypto init string \n");
2170 /* read key & certificates from file */
2171 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2172 "Trying to loading TLS certificate from key-file `%s' cert-file`%s'\n",
2173 key_file, cert_file);
2175 plugin->key = server_load_file (key_file);
2176 plugin->cert = server_load_file (cert_file);
2178 if ((plugin->key == NULL) || (plugin->cert == NULL))
2180 struct GNUNET_OS_Process *cert_creation;
2182 GNUNET_free_non_null (plugin->key);
2184 GNUNET_free_non_null (plugin->cert);
2185 plugin->cert = NULL;
2187 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2188 "No usable TLS certificate found, creating certificate\n");
2191 GNUNET_OS_start_process (GNUNET_NO, GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
2193 "gnunet-transport-certificate-creation",
2194 "gnunet-transport-certificate-creation",
2198 if (NULL == cert_creation)
2200 LOG (GNUNET_ERROR_TYPE_ERROR,
2202 "Could not create a new TLS certificate, program `gnunet-transport-certificate-creation' could not be started!\n"));
2203 GNUNET_free (key_file);
2204 GNUNET_free (cert_file);
2206 GNUNET_free_non_null (plugin->key);
2208 GNUNET_free_non_null (plugin->cert);
2209 plugin->cert = NULL;
2210 GNUNET_free_non_null (plugin->crypto_init);
2211 plugin->crypto_init = NULL;
2213 return GNUNET_SYSERR;
2215 GNUNET_assert (GNUNET_OK == GNUNET_OS_process_wait (cert_creation));
2216 GNUNET_OS_process_destroy (cert_creation);
2218 plugin->key = server_load_file (key_file);
2219 plugin->cert = server_load_file (cert_file);
2222 if ((plugin->key == NULL) || (plugin->cert == NULL))
2224 LOG (GNUNET_ERROR_TYPE_ERROR,
2226 "No usable TLS certificate found and creating one at `%s/%s' failed!\n"),
2227 key_file, cert_file);
2228 GNUNET_free (key_file);
2229 GNUNET_free (cert_file);
2231 GNUNET_free_non_null (plugin->key);
2233 GNUNET_free_non_null (plugin->cert);
2234 plugin->cert = NULL;
2235 GNUNET_free_non_null (plugin->crypto_init);
2236 plugin->crypto_init = NULL;
2238 return GNUNET_SYSERR;
2240 GNUNET_free (key_file);
2241 GNUNET_free (cert_file);
2242 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2243 "TLS certificate loaded\n");
2252 * Invoke `MHD_start_daemon` with the various options we need to
2253 * setup the HTTP server with the given listen address.
2255 * @param plugin our plugin
2256 * @param addr listen address to use
2257 * @param v6 MHD_NO_FLAG or MHD_USE_IPv6, depending on context
2258 * @return NULL on error
2260 static struct MHD_Daemon *
2261 run_mhd_start_daemon (struct HTTP_Server_Plugin *plugin,
2262 const struct sockaddr_in *addr,
2265 struct MHD_Daemon *server;
2266 unsigned int timeout;
2268 #if MHD_VERSION >= 0x00090E00
2269 timeout = HTTP_SERVER_NOT_VALIDATED_TIMEOUT.rel_value_us / 1000LL / 1000LL;
2270 LOG (GNUNET_ERROR_TYPE_DEBUG,
2271 "MHD can set timeout per connection! Default time out %u sec.\n",
2274 timeout = HTTP_SERVER_SESSION_TIMEOUT.rel_value_us / 1000LL / 1000LL;
2275 LOG (GNUNET_ERROR_TYPE_WARNING,
2276 "MHD cannot set timeout per connection! Default time out %u sec.\n",
2279 server = MHD_start_daemon (
2286 MHD_USE_SUSPEND_RESUME
2289 &server_accept_cb, plugin,
2290 &server_access_cb, plugin,
2291 MHD_OPTION_SOCK_ADDR,
2293 MHD_OPTION_CONNECTION_LIMIT,
2294 (unsigned int) plugin->max_request,
2296 MHD_OPTION_HTTPS_PRIORITIES,
2297 plugin->crypto_init,
2298 MHD_OPTION_HTTPS_MEM_KEY,
2300 MHD_OPTION_HTTPS_MEM_CERT,
2303 MHD_OPTION_CONNECTION_TIMEOUT,
2305 MHD_OPTION_CONNECTION_MEMORY_LIMIT,
2307 * GNUNET_MAX_MESSAGE_SIZE),
2308 MHD_OPTION_NOTIFY_COMPLETED,
2309 &server_disconnect_cb, plugin,
2310 MHD_OPTION_NOTIFY_CONNECTION,
2311 &server_connection_cb, plugin,
2312 MHD_OPTION_EXTERNAL_LOGGER,
2316 if ((NULL != server) &&
2317 (0 != (plugin->options & HTTP_OPTIONS_TCP_STEALTH)))
2319 const union MHD_DaemonInfo *di;
2321 di = MHD_get_daemon_info (server,
2322 MHD_DAEMON_INFO_LISTEN_FD,
2324 if ((0 != setsockopt ((int) di->listen_fd,
2327 plugin->env->my_identity,
2328 sizeof(struct GNUNET_PeerIdentity))))
2330 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2331 _ ("TCP_STEALTH not supported on this platform.\n"));
2332 MHD_stop_daemon (server);
2342 * Start the HTTP server
2344 * @param plugin the plugin handle
2345 * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
2348 server_start (struct HTTP_Server_Plugin *plugin)
2352 GNUNET_assert (NULL != plugin);
2354 if (GNUNET_SYSERR == server_load_certificate (plugin))
2356 LOG (GNUNET_ERROR_TYPE_ERROR,
2358 "Could not load or create server certificate! Loading plugin failed!\n"));
2359 return GNUNET_SYSERR;
2364 plugin->server_v4 = NULL;
2365 if (GNUNET_YES == plugin->use_ipv4)
2368 = run_mhd_start_daemon (plugin,
2370 sockaddr_in *) plugin->server_addr_v4,
2373 if (NULL == plugin->server_v4)
2375 LOG (GNUNET_ERROR_TYPE_ERROR,
2376 "Failed to start %s IPv4 server component on port %u\n",
2381 server_reschedule (plugin,
2387 plugin->server_v6 = NULL;
2388 if (GNUNET_YES == plugin->use_ipv6)
2391 = run_mhd_start_daemon (plugin,
2393 sockaddr_in *) plugin->server_addr_v6,
2395 if (NULL == plugin->server_v6)
2397 LOG (GNUNET_ERROR_TYPE_ERROR,
2398 "Failed to start %s IPv6 server component on port %u\n",
2404 server_reschedule (plugin,
2410 if ((NULL == plugin->server_v6) &&
2411 (NULL == plugin->server_v4))
2413 LOG (GNUNET_ERROR_TYPE_ERROR,
2414 "%s %s server component started on port %u\n",
2418 return GNUNET_SYSERR;
2420 if ((NULL != plugin->server_v6) &&
2421 (NULL != plugin->server_v4))
2422 msg = "IPv4 and IPv6";
2423 else if (NULL != plugin->server_v6)
2425 else if (NULL != plugin->server_v4)
2427 LOG (GNUNET_ERROR_TYPE_DEBUG,
2428 "%s %s server component started on port %u\n",
2437 * Add an address to the server's set of addresses and notify transport
2439 * @param cls the plugin handle
2440 * @param add_remove #GNUNET_YES on add, #GNUNET_NO on remove
2441 * @param addr the address
2442 * @param addrlen address length
2445 server_add_address (void *cls,
2447 const struct sockaddr *addr,
2450 struct HTTP_Server_Plugin *plugin = cls;
2451 struct GNUNET_HELLO_Address *address;
2452 struct HttpAddressWrapper *w = NULL;
2454 w = GNUNET_new (struct HttpAddressWrapper);
2455 w->address = http_common_address_from_socket (plugin->protocol,
2458 if (NULL == w->address)
2463 w->addrlen = http_common_address_get_size (w->address);
2465 GNUNET_CONTAINER_DLL_insert (plugin->addr_head,
2468 LOG (GNUNET_ERROR_TYPE_DEBUG,
2469 "Notifying transport to add address `%s'\n",
2470 http_common_plugin_address_to_string (plugin->protocol,
2473 /* modify our published address list */
2475 address = GNUNET_HELLO_address_allocate (plugin->env->my_identity,
2476 "https_client", w->address,
2478 GNUNET_HELLO_ADDRESS_INFO_NONE);
2480 address = GNUNET_HELLO_address_allocate (plugin->env->my_identity,
2481 "http_client", w->address,
2483 GNUNET_HELLO_ADDRESS_INFO_NONE);
2486 plugin->env->notify_address (plugin->env->cls,
2489 GNUNET_HELLO_address_free (address);
2494 * Remove an address from the server's set of addresses and notify transport
2496 * @param cls the plugin handle
2497 * @param add_remove #GNUNET_YES on add, #GNUNET_NO on remove
2498 * @param addr the address
2499 * @param addrlen address length
2502 server_remove_address (void *cls,
2504 const struct sockaddr *addr,
2507 struct HTTP_Server_Plugin *plugin = cls;
2508 struct GNUNET_HELLO_Address *address;
2509 struct HttpAddressWrapper *w = plugin->addr_head;
2513 saddr = http_common_address_from_socket (plugin->protocol,
2518 saddr_len = http_common_address_get_size (saddr);
2523 http_common_cmp_addresses (w->address,
2530 GNUNET_free (saddr);
2535 LOG (GNUNET_ERROR_TYPE_DEBUG,
2536 "Notifying transport to remove address `%s'\n",
2537 http_common_plugin_address_to_string (plugin->protocol,
2540 GNUNET_CONTAINER_DLL_remove (plugin->addr_head,
2543 /* modify our published address list */
2545 address = GNUNET_HELLO_address_allocate (plugin->env->my_identity,
2546 "https_client", w->address,
2548 GNUNET_HELLO_ADDRESS_INFO_NONE);
2550 address = GNUNET_HELLO_address_allocate (plugin->env->my_identity,
2551 "http_client", w->address,
2553 GNUNET_HELLO_ADDRESS_INFO_NONE);
2555 plugin->env->notify_address (plugin->env->cls, add_remove, address);
2556 GNUNET_HELLO_address_free (address);
2557 GNUNET_free (w->address);
2563 * Our external IP address/port mapping has changed.
2565 * @param cls closure, the 'struct LocalAddrList'
2566 * @param app_ctx[in,out] location where the app can store stuff
2567 * on add and retrieve it on remove
2568 * @param add_remove #GNUNET_YES to mean the new public IP address, #GNUNET_NO to mean
2569 * the previous (now invalid) one
2570 * @param ac address class the address belongs to
2571 * @param addr either the previous or the new public IP address
2572 * @param addrlen actual lenght of the address
2575 server_nat_port_map_callback (void *cls,
2578 enum GNUNET_NAT_AddressClass ac,
2579 const struct sockaddr *addr,
2582 struct HTTP_Server_Plugin *plugin = cls;
2585 LOG (GNUNET_ERROR_TYPE_DEBUG,
2586 "NAT called to %s address `%s'\n",
2587 (add_remove == GNUNET_NO) ? "remove" : "add",
2588 GNUNET_a2s (addr, addrlen));
2590 if (AF_INET == addr->sa_family)
2592 struct sockaddr_in *s4 = (struct sockaddr_in *) addr;
2594 if (GNUNET_NO == plugin->use_ipv4)
2597 if ((NULL != plugin->server_addr_v4) &&
2598 (0 != memcmp (&plugin->server_addr_v4->sin_addr,
2600 sizeof(struct in_addr))))
2602 LOG (GNUNET_ERROR_TYPE_DEBUG,
2603 "Skipping address `%s' (not bindto address)\n",
2604 GNUNET_a2s (addr, addrlen));
2609 if (AF_INET6 == addr->sa_family)
2611 struct sockaddr_in6 *s6 = (struct sockaddr_in6 *) addr;
2612 if (GNUNET_NO == plugin->use_ipv6)
2615 if ((NULL != plugin->server_addr_v6) &&
2616 (0 != memcmp (&plugin->server_addr_v6->sin6_addr,
2617 &s6->sin6_addr, sizeof(struct in6_addr))))
2619 LOG (GNUNET_ERROR_TYPE_DEBUG,
2620 "Skipping address `%s' (not bindto address)\n",
2621 GNUNET_a2s (addr, addrlen));
2629 server_add_address (cls, add_remove, addr, addrlen);
2633 server_remove_address (cls, add_remove, addr, addrlen);
2640 * Get valid server addresses
2642 * @param plugin the plugin handle
2643 * @param service_name the servicename
2644 * @param cfg configuration handle
2645 * @param addrs addresses
2646 * @param addr_lens address length
2647 * @return number of addresses
2650 server_get_addresses (struct HTTP_Server_Plugin *plugin,
2651 const char *service_name,
2652 const struct GNUNET_CONFIGURATION_Handle *cfg,
2653 struct sockaddr ***addrs,
2654 socklen_t **addr_lens)
2657 unsigned long long port;
2658 struct addrinfo hints;
2659 struct addrinfo *res;
2660 struct addrinfo *pos;
2661 struct addrinfo *next;
2665 struct sockaddr **saddrs;
2666 socklen_t *saddrlens;
2672 disablev6 = ! plugin->use_ipv6;
2675 if (GNUNET_CONFIGURATION_have_value (cfg, service_name, "PORT"))
2677 GNUNET_break (GNUNET_OK ==
2678 GNUNET_CONFIGURATION_get_value_number (cfg, service_name,
2682 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2684 "Require valid port number for service in configuration!\n"));
2685 return GNUNET_SYSERR;
2690 LOG (GNUNET_ERROR_TYPE_INFO,
2691 "Starting in listen only mode\n");
2692 return -1; /* listen only */
2696 if (GNUNET_CONFIGURATION_have_value (cfg, service_name,
2699 GNUNET_break (GNUNET_OK ==
2700 GNUNET_CONFIGURATION_get_value_string (cfg, service_name,
2701 "BINDTO", &hostname));
2706 if (NULL != hostname)
2708 LOG (GNUNET_ERROR_TYPE_DEBUG,
2709 "Resolving `%s' since that is where `%s' will bind to.\n",
2710 hostname, service_name);
2711 memset (&hints, 0, sizeof(struct addrinfo));
2713 hints.ai_family = AF_INET;
2714 if ((0 != (ret = getaddrinfo (hostname, NULL, &hints, &res))) ||
2717 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2718 _ ("Failed to resolve `%s': %s\n"),
2720 gai_strerror (ret));
2721 GNUNET_free (hostname);
2722 return GNUNET_SYSERR;
2726 while (NULL != (pos = next))
2728 next = pos->ai_next;
2729 if ((disablev6) && (pos->ai_family == AF_INET6))
2735 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2736 _ ("Failed to find %saddress for `%s'.\n"),
2737 disablev6 ? "IPv4 " : "", hostname);
2739 GNUNET_free (hostname);
2740 return GNUNET_SYSERR;
2743 saddrs = GNUNET_malloc ((resi + 1) * sizeof(struct sockaddr *));
2744 saddrlens = GNUNET_malloc ((resi + 1) * sizeof(socklen_t));
2747 while (NULL != (pos = next))
2749 next = pos->ai_next;
2750 if ((disablev6) && (pos->ai_family == AF_INET6))
2752 if ((pos->ai_protocol != IPPROTO_TCP) && (0 != pos->ai_protocol))
2753 continue; /* not TCP */
2754 if ((pos->ai_socktype != SOCK_STREAM) && (0 != pos->ai_socktype))
2755 continue; /* huh? */
2756 LOG (GNUNET_ERROR_TYPE_DEBUG,
2757 "Service will bind to `%s'\n",
2758 GNUNET_a2s (pos->ai_addr,
2760 if (pos->ai_family == AF_INET)
2762 GNUNET_assert (pos->ai_addrlen == sizeof(struct sockaddr_in));
2763 saddrlens[i] = pos->ai_addrlen;
2764 saddrs[i] = GNUNET_malloc (saddrlens[i]);
2765 GNUNET_memcpy (saddrs[i], pos->ai_addr, saddrlens[i]);
2766 ((struct sockaddr_in *) saddrs[i])->sin_port = htons (port);
2770 GNUNET_assert (pos->ai_family == AF_INET6);
2771 GNUNET_assert (pos->ai_addrlen == sizeof(struct sockaddr_in6));
2772 saddrlens[i] = pos->ai_addrlen;
2773 saddrs[i] = GNUNET_malloc (saddrlens[i]);
2774 GNUNET_memcpy (saddrs[i], pos->ai_addr, saddrlens[i]);
2775 ((struct sockaddr_in6 *) saddrs[i])->sin6_port = htons (port);
2779 GNUNET_free (hostname);
2785 /* will bind against everything, just set port */
2791 saddrs = GNUNET_malloc ((resi + 1) * sizeof(struct sockaddr *));
2792 saddrlens = GNUNET_malloc ((resi + 1) * sizeof(socklen_t));
2794 saddrlens[i] = sizeof(struct sockaddr_in);
2795 saddrs[i] = GNUNET_malloc (saddrlens[i]);
2796 #if HAVE_SOCKADDR_IN_SIN_LEN
2797 ((struct sockaddr_in *) saddrs[i])->sin_len = saddrlens[i];
2799 ((struct sockaddr_in *) saddrs[i])->sin_family = AF_INET;
2800 ((struct sockaddr_in *) saddrs[i])->sin_port = htons (port);
2806 saddrs = GNUNET_malloc ((resi + 1) * sizeof(struct sockaddr *));
2807 saddrlens = GNUNET_malloc ((resi + 1) * sizeof(socklen_t));
2809 saddrlens[i] = sizeof(struct sockaddr_in6);
2810 saddrs[i] = GNUNET_malloc (saddrlens[i]);
2811 #if HAVE_SOCKADDR_IN_SIN_LEN
2812 ((struct sockaddr_in6 *) saddrs[i])->sin6_len = saddrlens[0];
2814 ((struct sockaddr_in6 *) saddrs[i])->sin6_family = AF_INET6;
2815 ((struct sockaddr_in6 *) saddrs[i])->sin6_port = htons (port);
2817 saddrlens[i] = sizeof(struct sockaddr_in);
2818 saddrs[i] = GNUNET_malloc (saddrlens[i]);
2819 #if HAVE_SOCKADDR_IN_SIN_LEN
2820 ((struct sockaddr_in *) saddrs[i])->sin_len = saddrlens[1];
2822 ((struct sockaddr_in *) saddrs[i])->sin_family = AF_INET;
2823 ((struct sockaddr_in *) saddrs[i])->sin_port = htons (port);
2827 *addr_lens = saddrlens;
2833 * Ask NAT for addresses
2835 * @param plugin the plugin handle
2838 server_start_report_addresses (struct HTTP_Server_Plugin *plugin)
2840 int res = GNUNET_OK;
2841 struct sockaddr **addrs;
2842 socklen_t *addrlens;
2844 res = server_get_addresses (plugin,
2848 LOG (GNUNET_ERROR_TYPE_DEBUG,
2849 _ ("Found %u addresses to report to NAT service\n"),
2852 if (GNUNET_SYSERR == res)
2859 = GNUNET_NAT_register (plugin->env->cfg,
2863 (const struct sockaddr **) addrs,
2865 &server_nat_port_map_callback,
2871 GNUNET_assert (NULL != addrs[res]);
2872 GNUNET_free (addrs[res]);
2874 GNUNET_free_non_null (addrs);
2875 GNUNET_free_non_null (addrlens);
2880 * Stop NAT for addresses
2882 * @param plugin the plugin handle
2885 server_stop_report_addresses (struct HTTP_Server_Plugin *plugin)
2887 struct HttpAddressWrapper *w;
2889 /* Stop NAT handle */
2890 if (NULL != plugin->nat)
2892 GNUNET_NAT_unregister (plugin->nat);
2895 /* Clean up addresses */
2896 while (NULL != plugin->addr_head)
2898 w = plugin->addr_head;
2899 GNUNET_CONTAINER_DLL_remove (plugin->addr_head,
2902 GNUNET_free (w->address);
2909 * Check if IPv6 supported on this system
2911 * @param plugin the plugin handle
2912 * @return #GNUNET_YES on success, else #GNUNET_NO
2915 server_check_ipv6_support (struct HTTP_Server_Plugin *plugin)
2917 struct GNUNET_NETWORK_Handle *desc = NULL;
2918 int res = GNUNET_NO;
2920 /* Probe IPv6 support */
2921 desc = GNUNET_NETWORK_socket_create (PF_INET6,
2926 if ((errno == ENOBUFS) ||
2927 (errno == ENOMEM) ||
2928 (errno == ENFILE) ||
2931 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR,
2934 LOG (GNUNET_ERROR_TYPE_WARNING,
2935 _ ("Disabling IPv6 since it is not supported on this system!\n"));
2940 GNUNET_break (GNUNET_OK ==
2941 GNUNET_NETWORK_socket_close (desc));
2945 LOG (GNUNET_ERROR_TYPE_DEBUG,
2946 "Testing IPv6 on this system: %s\n",
2947 (res == GNUNET_YES) ? "successful" : "failed");
2953 * Notify server about our external hostname
2958 server_notify_external_hostname (void *cls)
2960 struct HTTP_Server_Plugin *plugin = cls;
2961 struct HttpAddress *ext_addr;
2962 size_t ext_addr_len;
2966 plugin->notify_ext_task = NULL;
2967 GNUNET_asprintf (&url,
2970 plugin->external_hostname);
2971 urlen = strlen (url) + 1;
2972 ext_addr = GNUNET_malloc (sizeof(struct HttpAddress) + urlen);
2973 ext_addr->options = htonl (plugin->options);
2974 ext_addr->urlen = htonl (urlen);
2975 ext_addr_len = sizeof(struct HttpAddress) + urlen;
2976 GNUNET_memcpy (&ext_addr[1], url, urlen);
2979 LOG (GNUNET_ERROR_TYPE_DEBUG,
2980 "Notifying transport about external hostname address `%s'\n",
2981 plugin->external_hostname);
2984 if (GNUNET_YES == plugin->verify_external_hostname)
2985 LOG (GNUNET_ERROR_TYPE_INFO,
2986 "Enabling SSL verification for external hostname address `%s'\n",
2987 plugin->external_hostname);
2989 = GNUNET_HELLO_address_allocate (plugin->env->my_identity,
2993 GNUNET_HELLO_ADDRESS_INFO_NONE);
2994 plugin->env->notify_address (plugin->env->cls,
2997 GNUNET_free (ext_addr);
3000 = GNUNET_HELLO_address_allocate (plugin->env->my_identity,
3004 GNUNET_HELLO_ADDRESS_INFO_NONE);
3005 plugin->env->notify_address (plugin->env->cls,
3008 GNUNET_free (ext_addr);
3014 * Configure the plugin
3016 * @param plugin plugin handle
3017 * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
3020 server_configure_plugin (struct HTTP_Server_Plugin *plugin)
3022 unsigned long long port;
3023 unsigned long long max_connections;
3024 char *bind4_address = NULL;
3025 char *bind6_address = NULL;
3026 char *eh_tmp = NULL;
3027 int external_hostname_use_port;
3030 if (GNUNET_CONFIGURATION_have_value
3031 (plugin->env->cfg, plugin->name, "USE_IPv4"))
3034 GNUNET_CONFIGURATION_get_value_yesno (plugin->env->cfg,
3039 plugin->use_ipv4 = GNUNET_YES;
3040 LOG (GNUNET_ERROR_TYPE_DEBUG,
3041 _ ("IPv4 support is %s\n"),
3042 (plugin->use_ipv4 == GNUNET_YES) ? "enabled" : "disabled");
3045 if (GNUNET_CONFIGURATION_have_value
3046 (plugin->env->cfg, plugin->name, "USE_IPv6"))
3049 GNUNET_CONFIGURATION_get_value_yesno (plugin->env->cfg,
3054 plugin->use_ipv6 = GNUNET_YES;
3055 LOG (GNUNET_ERROR_TYPE_DEBUG,
3056 _ ("IPv6 support is %s\n"),
3057 (plugin->use_ipv6 == GNUNET_YES) ? "enabled" : "disabled");
3059 if ((plugin->use_ipv4 == GNUNET_NO) && (plugin->use_ipv6 == GNUNET_NO))
3061 LOG (GNUNET_ERROR_TYPE_ERROR,
3062 _ ("Neither IPv4 nor IPv6 are enabled! Fix in configuration\n"));
3063 return GNUNET_SYSERR;
3066 /* Reading port number from config file */
3068 GNUNET_CONFIGURATION_get_value_number (plugin->env->cfg,
3070 "PORT", &port)) || (port > 65535))
3072 LOG (GNUNET_ERROR_TYPE_ERROR,
3073 _ ("Port is required! Fix in configuration\n"));
3074 return GNUNET_SYSERR;
3076 plugin->port = port;
3078 LOG (GNUNET_ERROR_TYPE_INFO,
3079 _ ("Using port %u\n"), plugin->port);
3081 if ((plugin->use_ipv4 == GNUNET_YES) &&
3083 GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg,
3088 LOG (GNUNET_ERROR_TYPE_DEBUG,
3089 "Binding %s plugin to specific IPv4 address: `%s'\n",
3092 plugin->server_addr_v4 = GNUNET_new (struct sockaddr_in);
3093 if (1 != inet_pton (AF_INET,
3095 &plugin->server_addr_v4->sin_addr))
3097 LOG (GNUNET_ERROR_TYPE_ERROR,
3098 _ ("Specific IPv4 address `%s' in configuration file is invalid!\n"),
3100 GNUNET_free (bind4_address);
3101 GNUNET_free (plugin->server_addr_v4);
3102 plugin->server_addr_v4 = NULL;
3103 return GNUNET_SYSERR;
3107 LOG (GNUNET_ERROR_TYPE_DEBUG,
3108 "Binding to IPv4 address %s\n",
3110 plugin->server_addr_v4->sin_family = AF_INET;
3111 plugin->server_addr_v4->sin_port = htons (plugin->port);
3113 GNUNET_free (bind4_address);
3116 if ((plugin->use_ipv6 == GNUNET_YES) &&
3118 GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg,
3123 LOG (GNUNET_ERROR_TYPE_DEBUG,
3124 "Binding %s plugin to specific IPv6 address: `%s'\n",
3125 plugin->protocol, bind6_address);
3126 plugin->server_addr_v6 = GNUNET_new (struct sockaddr_in6);
3128 inet_pton (AF_INET6,
3130 &plugin->server_addr_v6->sin6_addr))
3132 LOG (GNUNET_ERROR_TYPE_ERROR,
3133 _ ("Specific IPv6 address `%s' in configuration file is invalid!\n"),
3135 GNUNET_free (bind6_address);
3136 GNUNET_free (plugin->server_addr_v6);
3137 plugin->server_addr_v6 = NULL;
3138 return GNUNET_SYSERR;
3142 LOG (GNUNET_ERROR_TYPE_DEBUG,
3143 "Binding to IPv6 address %s\n",
3145 plugin->server_addr_v6->sin6_family = AF_INET6;
3146 plugin->server_addr_v6->sin6_port = htons (plugin->port);
3148 GNUNET_free (bind6_address);
3151 plugin->verify_external_hostname = GNUNET_NO;
3153 plugin->verify_external_hostname
3154 = GNUNET_CONFIGURATION_get_value_yesno (plugin->env->cfg,
3156 "VERIFY_EXTERNAL_HOSTNAME");
3157 if (GNUNET_SYSERR == plugin->verify_external_hostname)
3158 plugin->verify_external_hostname = GNUNET_NO;
3159 if (GNUNET_YES == plugin->verify_external_hostname)
3160 plugin->options |= HTTP_OPTIONS_VERIFY_CERTIFICATE;
3162 external_hostname_use_port
3163 = GNUNET_CONFIGURATION_get_value_yesno (plugin->env->cfg,
3165 "EXTERNAL_HOSTNAME_USE_PORT");
3166 if (GNUNET_SYSERR == external_hostname_use_port)
3167 external_hostname_use_port = GNUNET_NO;
3171 GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg,
3173 "EXTERNAL_HOSTNAME",
3178 char *pos_url = NULL;
3180 if (NULL != strstr (eh_tmp, "://"))
3181 tmp = &strstr (eh_tmp, "://")[3];
3185 if (GNUNET_YES == external_hostname_use_port)
3187 if ((strlen (tmp) > 1) && (NULL != (pos = strchr (tmp, '/'))))
3191 GNUNET_asprintf (&plugin->external_hostname,
3198 GNUNET_asprintf (&plugin->external_hostname,
3204 plugin->external_hostname = GNUNET_strdup (tmp);
3205 GNUNET_free (eh_tmp);
3207 LOG (GNUNET_ERROR_TYPE_INFO,
3208 _ ("Using external hostname `%s'\n"),
3209 plugin->external_hostname);
3210 plugin->notify_ext_task = GNUNET_SCHEDULER_add_now (
3211 &server_notify_external_hostname,
3214 /* Use only configured external hostname */
3215 if (GNUNET_CONFIGURATION_have_value
3218 "EXTERNAL_HOSTNAME_ONLY"))
3220 plugin->external_only =
3221 GNUNET_CONFIGURATION_get_value_yesno (plugin->env->cfg,
3223 "EXTERNAL_HOSTNAME_ONLY");
3226 plugin->external_only = GNUNET_NO;
3228 if (GNUNET_YES == plugin->external_only)
3229 LOG (GNUNET_ERROR_TYPE_DEBUG,
3230 _ ("Notifying transport only about hostname `%s'\n"),
3231 plugin->external_hostname);
3234 LOG (GNUNET_ERROR_TYPE_DEBUG,
3235 "No external hostname configured\n");
3237 /* Optional parameters */
3239 GNUNET_CONFIGURATION_get_value_number (plugin->env->cfg,
3243 max_connections = 128;
3244 plugin->max_request = max_connections;
3246 LOG (GNUNET_ERROR_TYPE_DEBUG,
3247 _ ("Maximum number of connections is %u\n"),
3248 plugin->max_request);
3250 plugin->peer_id_length = strlen (GNUNET_i2s_full (plugin->env->my_identity));
3257 * Exit point from the plugin.
3263 LIBGNUNET_PLUGIN_TRANSPORT_DONE (void *cls)
3265 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
3266 struct HTTP_Server_Plugin *plugin = api->cls;
3268 if (NULL == api->cls)
3270 /* Free for stub mode */
3274 plugin->in_shutdown = GNUNET_YES;
3275 LOG (GNUNET_ERROR_TYPE_INFO,
3276 _ ("Shutting down plugin `%s'\n"),
3279 if (NULL != plugin->notify_ext_task)
3281 GNUNET_SCHEDULER_cancel (plugin->notify_ext_task);
3282 plugin->notify_ext_task = NULL;
3285 if (NULL != plugin->ext_addr)
3287 LOG (GNUNET_ERROR_TYPE_DEBUG,
3288 "Notifying transport to remove address `%s'\n",
3289 http_common_plugin_address_to_string (plugin->protocol,
3290 plugin->ext_addr->address,
3291 plugin->ext_addr->address_length));
3293 plugin->env->notify_address (plugin->env->cls,
3297 plugin->env->notify_address (plugin->env->cls,
3301 GNUNET_HELLO_address_free (plugin->ext_addr);
3302 plugin->ext_addr = NULL;
3305 /* Stop to report addresses to transport service */
3306 server_stop_report_addresses (plugin);
3307 if (NULL != plugin->server_v4_task)
3309 GNUNET_SCHEDULER_cancel (plugin->server_v4_task);
3310 plugin->server_v4_task = NULL;
3313 if (NULL != plugin->server_v6_task)
3315 GNUNET_SCHEDULER_cancel (plugin->server_v6_task);
3316 plugin->server_v6_task = NULL;
3319 GNUNET_free_non_null (plugin->crypto_init);
3320 GNUNET_free_non_null (plugin->cert);
3321 GNUNET_free_non_null (plugin->key);
3323 GNUNET_CONTAINER_multipeermap_iterate (plugin->sessions,
3324 &destroy_session_shutdown_cb,
3326 GNUNET_CONTAINER_multipeermap_destroy (plugin->sessions);
3327 plugin->sessions = NULL;
3328 if (NULL != plugin->server_v4)
3330 MHD_stop_daemon (plugin->server_v4);
3331 plugin->server_v4 = NULL;
3333 if (NULL != plugin->server_v6)
3335 MHD_stop_daemon (plugin->server_v6);
3336 plugin->server_v6 = NULL;
3339 GNUNET_free_non_null (plugin->external_hostname);
3340 GNUNET_free_non_null (plugin->ext_addr);
3341 GNUNET_free_non_null (plugin->server_addr_v4);
3342 GNUNET_free_non_null (plugin->server_addr_v6);
3343 regfree (&plugin->url_regex);
3345 LOG (GNUNET_ERROR_TYPE_DEBUG,
3346 _ ("Shutdown for plugin `%s' complete\n"),
3349 GNUNET_free (plugin);
3356 * Function called for a quick conversion of the binary address to
3357 * a numeric address. Note that the caller must not free the
3358 * address and that the next call to this function is allowed
3359 * to override the address again.
3362 * @param addr binary address
3363 * @param addrlen length of the address
3364 * @return string representing the same address
3367 http_server_plugin_address_to_string (void *cls,
3371 return http_common_plugin_address_to_string (PLUGIN_NAME,
3378 * Function obtain the network type for a session
3380 * @param cls closure (`struct HTTP_Server_Plugin *`)
3381 * @param session the session
3382 * @return the network type in HBO or #GNUNET_SYSERR
3384 static enum GNUNET_NetworkType
3385 http_server_plugin_get_network (void *cls,
3386 struct GNUNET_ATS_Session *session)
3388 return session->scope;
3393 * Function obtain the network type for an address.
3395 * @param cls closure (`struct Plugin *`)
3396 * @param address the address
3397 * @return the network type
3399 static enum GNUNET_NetworkType
3400 http_server_plugin_get_network_for_address (void *cls,
3402 GNUNET_HELLO_Address *address)
3404 struct HTTP_Server_Plugin *plugin = cls;
3406 return http_common_get_network_for_address (plugin->env,
3412 * Function that will be called whenever the transport service wants to
3413 * notify the plugin that the inbound quota changed and that the plugin
3414 * should update it's delay for the next receive value
3416 * @param cls closure
3417 * @param peer which peer was the session for
3418 * @param session which session is being updated
3419 * @param delay new delay to use for receiving
3422 http_server_plugin_update_inbound_delay (void *cls,
3423 const struct GNUNET_PeerIdentity *peer,
3424 struct GNUNET_ATS_Session *session,
3425 struct GNUNET_TIME_Relative delay)
3427 session->next_receive = GNUNET_TIME_relative_to_absolute (delay);
3428 LOG (GNUNET_ERROR_TYPE_DEBUG,
3429 "New inbound delay %s\n",
3430 GNUNET_STRINGS_relative_time_to_string (delay,
3432 if (NULL != session->recv_wakeup_task)
3434 GNUNET_SCHEDULER_cancel (session->recv_wakeup_task);
3435 session->recv_wakeup_task
3436 = GNUNET_SCHEDULER_add_delayed (delay,
3444 * Return information about the given session to the
3447 * @param cls the `struct Plugin` with the monitor callback (`sic`)
3448 * @param peer peer we send information about
3449 * @param value our `struct GNUNET_ATS_Session` to send information about
3450 * @return #GNUNET_OK (continue to iterate)
3453 send_session_info_iter (void *cls,
3454 const struct GNUNET_PeerIdentity *peer,
3457 struct HTTP_Server_Plugin *plugin = cls;
3458 struct GNUNET_ATS_Session *session = value;
3460 notify_session_monitor (plugin,
3462 GNUNET_TRANSPORT_SS_INIT);
3468 * Begin monitoring sessions of a plugin. There can only
3469 * be one active monitor per plugin (i.e. if there are
3470 * multiple monitors, the transport service needs to
3471 * multiplex the generated events over all of them).
3473 * @param cls closure of the plugin
3474 * @param sic callback to invoke, NULL to disable monitor;
3475 * plugin will being by iterating over all active
3476 * sessions immediately and then enter monitor mode
3477 * @param sic_cls closure for @a sic
3480 http_server_plugin_setup_monitor (void *cls,
3481 GNUNET_TRANSPORT_SessionInfoCallback sic,
3484 struct HTTP_Server_Plugin *plugin = cls;
3487 plugin->sic_cls = sic_cls;
3490 GNUNET_CONTAINER_multipeermap_iterate (plugin->sessions,
3491 &send_session_info_iter,
3493 /* signal end of first iteration */
3494 sic (sic_cls, NULL, NULL);
3500 * Entry point for the plugin.
3506 LIBGNUNET_PLUGIN_TRANSPORT_INIT (void *cls)
3508 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
3509 struct GNUNET_TRANSPORT_PluginFunctions *api;
3510 struct HTTP_Server_Plugin *plugin;
3512 if (NULL == env->receive)
3514 /* run in 'stub' mode (i.e. as part of gnunet-peerinfo), don't fully
3515 initialze the plugin or the API */
3516 api = GNUNET_new (struct GNUNET_TRANSPORT_PluginFunctions);
3518 api->address_to_string = &http_server_plugin_address_to_string;
3519 api->string_to_address = &http_common_plugin_string_to_address;
3520 api->address_pretty_printer = &http_common_plugin_address_pretty_printer;
3523 plugin = GNUNET_new (struct HTTP_Server_Plugin);
3525 plugin->sessions = GNUNET_CONTAINER_multipeermap_create (128,
3528 api = GNUNET_new (struct GNUNET_TRANSPORT_PluginFunctions);
3530 api->send = &http_server_plugin_send;
3531 api->disconnect_peer = &http_server_plugin_disconnect_peer;
3532 api->disconnect_session = &http_server_plugin_disconnect_session;
3533 api->query_keepalive_factor = &http_server_query_keepalive_factor;
3534 api->check_address = &http_server_plugin_address_suggested;
3535 api->get_session = &http_server_plugin_get_session;
3537 api->address_to_string = &http_server_plugin_address_to_string;
3538 api->string_to_address = &http_common_plugin_string_to_address;
3539 api->address_pretty_printer = &http_common_plugin_address_pretty_printer;
3540 api->get_network = &http_server_plugin_get_network;
3541 api->get_network_for_address = &http_server_plugin_get_network_for_address;
3542 api->update_session_timeout = &http_server_plugin_update_session_timeout;
3543 api->update_inbound_delay = &http_server_plugin_update_inbound_delay;
3544 api->setup_monitor = &http_server_plugin_setup_monitor;
3546 plugin->name = "transport-https_server";
3547 plugin->protocol = "https";
3549 plugin->name = "transport-http_server";
3550 plugin->protocol = "http";
3554 GNUNET_CONFIGURATION_get_value_yesno (env->cfg,
3559 plugin->options |= HTTP_OPTIONS_TCP_STEALTH;
3561 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3562 _ ("TCP_STEALTH not supported on this platform.\n"));
3563 LIBGNUNET_PLUGIN_TRANSPORT_DONE (api);
3568 /* Compile URL regex */
3569 if (regcomp (&plugin->url_regex,
3573 LOG (GNUNET_ERROR_TYPE_ERROR,
3574 _ ("Unable to compile URL regex\n"));
3575 LIBGNUNET_PLUGIN_TRANSPORT_DONE (api);
3579 /* Configure plugin */
3580 if (GNUNET_SYSERR == server_configure_plugin (plugin))
3582 LIBGNUNET_PLUGIN_TRANSPORT_DONE (api);
3586 /* Check IPv6 support */
3587 if (GNUNET_YES == plugin->use_ipv6)
3588 plugin->use_ipv6 = server_check_ipv6_support (plugin);
3590 /* Report addresses to transport service */
3591 if (GNUNET_NO == plugin->external_only)
3592 server_start_report_addresses (plugin);
3594 if (GNUNET_SYSERR == server_start (plugin))
3596 LIBGNUNET_PLUGIN_TRANSPORT_DONE (api);
3603 /* end of plugin_transport_http_server.c */