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
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., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
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>
40 #define PLUGIN_NAME "https_server"
41 #define LIBGNUNET_PLUGIN_TRANSPORT_INIT libgnunet_plugin_transport_https_server_init
42 #define LIBGNUNET_PLUGIN_TRANSPORT_DONE libgnunet_plugin_transport_https_server_done
44 #define PLUGIN_NAME "http_server"
45 #define LIBGNUNET_PLUGIN_TRANSPORT_INIT libgnunet_plugin_transport_http_server_init
46 #define LIBGNUNET_PLUGIN_TRANSPORT_DONE libgnunet_plugin_transport_http_server_done
49 #define HTTP_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 #define LOG(kind,...) GNUNET_log_from (kind, "transport-" PLUGIN_NAME,__VA_ARGS__)
58 * Information we keep with MHD for an HTTP request.
63 * The session this server request belongs to
64 * Can be NULL, when session was disconnected and freed
66 struct GNUNET_ATS_Session *session;
71 struct MHD_Connection *mhd_conn;
76 struct MHD_Daemon *mhd_daemon;
79 * Options requested by peer
82 #define OPTION_LONG_POLL 1 /* GET request wants long-poll semantics */
90 * For PUT requests: Is this the first or last callback with size 0
91 * For GET requests: Have we sent a message
104 * Wrapper to manage addresses
106 struct HttpAddressWrapper
111 struct HttpAddressWrapper *next;
114 * Linked list previous
116 struct HttpAddressWrapper *prev;
119 * An address we are using.
121 struct HttpAddress *address;
124 * Length of the address.
131 * Message to send using http
136 * next pointer for double linked list
138 struct HTTP_Message *next;
141 * previous pointer for double linked list
143 struct HTTP_Message *prev;
146 * buffer containing data to send
151 * amount of data already sent
161 * HTTP/S specific overhead
166 * Continuation function to call once the transmission buffer
167 * has again space available. NULL if there is no
168 * continuation to call.
170 GNUNET_TRANSPORT_TransmitContinuation transmit_cont;
173 * Closure for transmit_cont.
175 void *transmit_cont_cls;
180 * Session handle for connections.
182 struct GNUNET_ATS_Session
186 * To whom are we talking to (set to our identity
187 * if we are still waiting for the welcome message)
189 struct GNUNET_PeerIdentity target;
192 * Pointer to the global plugin struct.
194 struct HTTP_Server_Plugin *plugin;
197 * next pointer for double linked list
199 struct HTTP_Message *msg_head;
202 * previous pointer for double linked list
204 struct HTTP_Message *msg_tail;
207 * Message stream tokenizer for incoming data
209 struct GNUNET_MessageStreamTokenizer *msg_tk;
214 struct ServerRequest *server_recv;
219 struct ServerRequest *server_send;
224 struct GNUNET_HELLO_Address *address;
227 * Absolute time when to receive data again
228 * Used for receive throttling
230 struct GNUNET_TIME_Absolute next_receive;
233 * Absolute time when this connection will time out.
235 struct GNUNET_TIME_Absolute timeout;
238 * Session timeout task
240 struct GNUNET_SCHEDULER_Task * timeout_task;
243 * Task to resume MHD handling when receiving is allowed again
245 struct GNUNET_SCHEDULER_Task * recv_wakeup_task;
248 * Number of bytes waiting for transmission to this peer.
250 unsigned long long bytes_in_queue;
253 * Number of messages waiting for transmission to this peer.
255 unsigned int msgs_in_queue;
258 * Unique HTTP/S connection tag for this connection
265 enum GNUNET_ATS_Network_Type scope;
268 * #GNUNET_YES if this session is known to the service.
270 int known_to_service;
276 * Encapsulation of all of the state of the plugin.
278 struct HTTP_Server_Plugin
283 struct GNUNET_TRANSPORT_PluginEnvironment *env;
286 * Hash map of open sessions.
288 struct GNUNET_CONTAINER_MultiPeerMap *sessions;
291 * Function to call about session status changes.
293 GNUNET_TRANSPORT_SessionInfoCallback sic;
296 * Closure for @e sic.
313 char *external_hostname;
316 * External hostname the plugin can be connected to, can be different to
317 * the host's FQDN, used e.g. for reverse proxying
319 struct GNUNET_HELLO_Address *ext_addr;
322 * NAT handle & address management
324 struct GNUNET_NAT_Handle *nat;
327 * IPv4 addresses DLL head
329 struct HttpAddressWrapper *addr_head;
332 * IPv4 addresses DLL tail
334 struct HttpAddressWrapper *addr_tail;
337 * IPv4 server socket to bind to
339 struct sockaddr_in *server_addr_v4;
342 * IPv6 server socket to bind to
344 struct sockaddr_in6 *server_addr_v6;
349 struct MHD_Daemon *server_v4;
354 struct MHD_Daemon *server_v6;
362 * Use RC4-128 instead of AES:
363 * NONE:+VERS-TLS1.0:+ARCFOUR-128:+SHA1:+RSA:+COMP-NULL
382 struct GNUNET_SCHEDULER_Task *server_v4_task;
387 struct GNUNET_SCHEDULER_Task *server_v6_task;
390 * Task calling transport service about external address
392 struct GNUNET_SCHEDULER_Task *notify_ext_task;
395 * Notify transport only about external address
397 unsigned int external_only;
400 * The IPv4 server is scheduled to run asap
402 int server_v4_immediately;
405 * The IPv6 server is scheduled to run asap
407 int server_v6_immediately;
410 * Verify external address
412 int verify_external_hostname;
415 * Maximum number of sockets the plugin can use
416 * Each http request /request connections are two connections
418 unsigned int max_request;
421 * Current number of sockets the plugin can use
422 * Each http connection are two requests
424 unsigned int cur_request;
427 * Did we immediately end the session in disconnect_cb
437 * My options to be included in the address
457 * Regex for parsing URLs. FIXME: this seems overkill.
465 * If a session monitor is attached, notify it about the new
468 * @param plugin our plugin
469 * @param session session that changed state
470 * @param state new state of the session
473 notify_session_monitor (struct HTTP_Server_Plugin *plugin,
474 struct GNUNET_ATS_Session *session,
475 enum GNUNET_TRANSPORT_SessionState state)
477 struct GNUNET_TRANSPORT_SessionInfo info;
479 if (NULL == plugin->sic)
481 memset (&info, 0, sizeof (info));
483 info.is_inbound = GNUNET_YES;
484 info.num_msg_pending = session->msgs_in_queue;
485 info.num_bytes_pending = session->bytes_in_queue;
486 info.receive_delay = session->next_receive;
487 info.session_timeout = session->timeout;
488 info.address = session->address;
489 plugin->sic (plugin->sic_cls,
496 * Wake up an MHD connection which was suspended
498 * @param cls the session
501 server_wake_up (void *cls)
503 struct GNUNET_ATS_Session *s = cls;
505 s->recv_wakeup_task = NULL;
506 LOG (GNUNET_ERROR_TYPE_DEBUG,
507 "Session %p: Waking up PUT handle\n",
509 GNUNET_assert (s->server_recv->suspended);
510 MHD_resume_connection (s->server_recv->mhd_conn);
511 s->server_recv->suspended = false;
516 * Reschedule the execution of both IPv4 and IPv6 server.
518 * @param plugin the plugin
519 * @param server which server to schedule v4 or v6?
520 * @param now #GNUNET_YES to schedule execution immediately, #GNUNET_NO to wait
524 server_reschedule (struct HTTP_Server_Plugin *plugin,
525 struct MHD_Daemon *server,
530 * Deletes the session. Must not be used afterwards.
532 * @param s the session to delete
535 server_delete_session (struct GNUNET_ATS_Session *s)
537 struct HTTP_Server_Plugin *plugin = s->plugin;
538 struct HTTP_Message *msg;
540 if (NULL != s->timeout_task)
542 GNUNET_SCHEDULER_cancel (s->timeout_task);
543 s->timeout_task = NULL;
544 s->timeout = GNUNET_TIME_UNIT_ZERO_ABS;
546 if (NULL != s->recv_wakeup_task)
548 GNUNET_SCHEDULER_cancel (s->recv_wakeup_task);
549 s->recv_wakeup_task = NULL;
550 if (NULL != s->server_recv)
552 GNUNET_assert (s->server_recv->suspended);
553 MHD_resume_connection (s->server_recv->mhd_conn);
554 s->server_recv->suspended = false;
557 GNUNET_assert (GNUNET_OK ==
558 GNUNET_CONTAINER_multipeermap_remove (plugin->sessions,
561 while (NULL != (msg = s->msg_head))
563 GNUNET_CONTAINER_DLL_remove (s->msg_head,
566 if (NULL != msg->transmit_cont)
567 msg->transmit_cont (msg->transmit_cont_cls,
571 msg->pos + msg->overhead);
572 GNUNET_assert (s->msgs_in_queue > 0);
574 GNUNET_assert (s->bytes_in_queue >= msg->size);
575 s->bytes_in_queue -= msg->size;
579 GNUNET_assert (0 == s->msgs_in_queue);
580 GNUNET_assert (0 == s->bytes_in_queue);
582 if (NULL != s->server_send)
584 LOG (GNUNET_ERROR_TYPE_DEBUG,
585 "Server: %p / %p Terminating inbound PUT session to peer `%s'\n",
587 GNUNET_i2s (&s->target));
588 s->server_send->session = NULL;
589 MHD_set_connection_option (s->server_send->mhd_conn,
590 MHD_CONNECTION_OPTION_TIMEOUT,
591 1 /* 0 = no timeout, so this is MIN */);
592 server_reschedule (plugin,
593 s->server_send->mhd_daemon,
597 if (NULL != s->server_recv)
599 LOG (GNUNET_ERROR_TYPE_DEBUG,
600 "Server: %p / %p Terminating inbound GET session to peer `%s'\n",
601 s, s->server_recv, GNUNET_i2s (&s->target));
602 s->server_recv->session = NULL;
603 MHD_set_connection_option (s->server_recv->mhd_conn,
604 MHD_CONNECTION_OPTION_TIMEOUT,
605 1 /* 0 = no timeout, so this is MIN */);
606 server_reschedule (plugin,
607 s->server_recv->mhd_daemon,
610 notify_session_monitor (plugin,
612 GNUNET_TRANSPORT_SS_DONE);
613 if (GNUNET_YES == s->known_to_service)
615 plugin->env->session_end (plugin->env->cls,
618 s->known_to_service = GNUNET_NO;
620 if (NULL != s->msg_tk)
622 GNUNET_MST_destroy (s->msg_tk);
625 GNUNET_HELLO_address_free (s->address);
626 LOG (GNUNET_ERROR_TYPE_DEBUG,
627 "Session %p destroyed\n",
634 * Disconnect session @a s by telling MHD to close the
635 * connections (reducing timeout, etc.).
637 * @param cls closure with the `struct HTTP_Server_Plugin`
638 * @param s the session
639 * @return #GNUNET_OK on success
642 http_server_plugin_disconnect_session (void *cls,
643 struct GNUNET_ATS_Session *s)
645 server_delete_session (s);
651 * Session was idle, so disconnect it
653 * @param cls the session
656 server_session_timeout (void *cls)
658 struct GNUNET_ATS_Session *s = cls;
659 struct GNUNET_TIME_Relative left;
661 s->timeout_task = NULL;
662 left = GNUNET_TIME_absolute_get_remaining (s->timeout);
663 if (0 != left.rel_value_us)
665 /* not actually our turn yet, but let's at least update
666 the monitor, it may think we're about to die ... */
667 notify_session_monitor (s->plugin,
669 GNUNET_TRANSPORT_SS_UP);
670 s->timeout_task = GNUNET_SCHEDULER_add_delayed (left,
671 &server_session_timeout,
675 GNUNET_log (TIMEOUT_LOG,
676 "Session %p was idle for %s, disconnecting\n",
678 GNUNET_STRINGS_relative_time_to_string (HTTP_SERVER_SESSION_TIMEOUT,
680 server_delete_session (s);
685 * Increment session timeout due to activity session @a s
687 * @param s the session
690 server_reschedule_session_timeout (struct GNUNET_ATS_Session *s)
692 GNUNET_assert (NULL != s->timeout_task);
693 s->timeout = GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
698 * Function that can be used by the transport service to transmit
699 * a message using the plugin. Note that in the case of a
700 * peer disconnecting, the continuation MUST be called
701 * prior to the disconnect notification itself. This function
702 * will be called with this peer's HELLO message to initiate
703 * a fresh connection to another peer.
706 * @param session which session must be used
707 * @param msgbuf the message to transmit
708 * @param msgbuf_size number of bytes in @a msgbuf
709 * @param priority how important is the message (most plugins will
710 * ignore message priority and just FIFO)
711 * @param to how long to wait at most for the transmission (does not
712 * require plugins to discard the message after the timeout,
713 * just advisory for the desired delay; most plugins will ignore
715 * @param cont continuation to call once the message has
716 * been transmitted (or if the transport is ready
717 * for the next transmission call; or if the
718 * peer disconnected...); can be NULL
719 * @param cont_cls closure for @a cont
720 * @return number of bytes used (on the physical network, with overheads);
721 * -1 on hard errors (i.e. address invalid); 0 is a legal value
722 * and does NOT mean that the message was not transmitted (DV)
725 http_server_plugin_send (void *cls,
726 struct GNUNET_ATS_Session *session,
729 unsigned int priority,
730 struct GNUNET_TIME_Relative to,
731 GNUNET_TRANSPORT_TransmitContinuation cont,
734 struct HTTP_Server_Plugin *plugin = cls;
735 struct HTTP_Message *msg;
736 ssize_t bytes_sent = 0;
739 LOG (GNUNET_ERROR_TYPE_DEBUG,
740 "Session %p/request %p: Sending message with %u to peer `%s'\n",
742 session->server_send,
744 GNUNET_i2s (&session->target));
746 /* create new message and schedule */
747 bytes_sent = sizeof (struct HTTP_Message) + msgbuf_size;
748 msg = GNUNET_malloc (bytes_sent);
750 msg->size = msgbuf_size;
752 msg->buf = (char *) &msg[1];
753 msg->transmit_cont = cont;
754 msg->transmit_cont_cls = cont_cls;
755 GNUNET_memcpy (msg->buf,
758 GNUNET_CONTAINER_DLL_insert_tail (session->msg_head,
761 session->msgs_in_queue++;
762 session->bytes_in_queue += msg->size;
763 notify_session_monitor (plugin,
765 GNUNET_TRANSPORT_SS_UP);
766 GNUNET_asprintf (&stat_txt,
767 "# bytes currently in %s_server buffers",
769 GNUNET_STATISTICS_update (plugin->env->stats,
770 stat_txt, msgbuf_size, GNUNET_NO);
771 GNUNET_free (stat_txt);
773 if (NULL != session->server_send)
775 if (session->server_send->suspended)
777 MHD_resume_connection (session->server_send->mhd_conn);
778 session->server_send->suspended = false;
780 server_reschedule (session->plugin,
781 session->server_send->mhd_daemon,
789 * Terminate session during shutdown.
791 * @param cls the `struct HTTP_Server_Plugin *`
792 * @param peer for which this is a session
793 * @param value the `struct GNUNET_ATS_Session` to clean up
794 * @return #GNUNET_OK (continue to iterate)
797 destroy_session_shutdown_cb (void *cls,
798 const struct GNUNET_PeerIdentity *peer,
801 struct GNUNET_ATS_Session *s = value;
802 struct ServerRequest *sc_send;
803 struct ServerRequest *sc_recv;
805 sc_send = s->server_send;
806 sc_recv = s->server_recv;
807 server_delete_session (s);
809 sc_send->session = NULL;
811 sc_recv->session = NULL;
820 * @param cls the `struct HTTP_Server_Plugin *`
821 * @param peer for which this is a session
822 * @param value the `struct GNUNET_ATS_Session` to clean up
823 * @return #GNUNET_OK (continue to iterate)
826 destroy_session_cb (void *cls,
827 const struct GNUNET_PeerIdentity *peer,
830 struct GNUNET_ATS_Session *s = value;
832 server_delete_session (s);
838 * Function that can be used to force the plugin to disconnect
839 * from the given peer and cancel all previous transmissions
840 * (and their continuationc).
843 * @param target peer from which to disconnect
846 http_server_plugin_disconnect_peer (void *cls,
847 const struct GNUNET_PeerIdentity *target)
849 struct HTTP_Server_Plugin *plugin = cls;
851 LOG (GNUNET_ERROR_TYPE_DEBUG,
852 "Transport tells me to disconnect `%s'\n",
853 GNUNET_i2s (target));
854 GNUNET_CONTAINER_multipeermap_get_multiple (plugin->sessions,
862 * Another peer has suggested an address for this
863 * peer and transport plugin. Check that this could be a valid
864 * address. If so, consider adding it to the list
868 * @param addr pointer to the address
869 * @param addrlen length of @a addr
870 * @return #GNUNET_OK if this is a plausible address for this peer
874 http_server_plugin_address_suggested (void *cls,
878 struct HTTP_Server_Plugin *plugin = cls;
879 struct HttpAddressWrapper *next;
880 struct HttpAddressWrapper *pos;
881 const struct HttpAddress *haddr = addr;
883 if ((NULL != plugin->ext_addr) &&
884 GNUNET_YES == (http_common_cmp_addresses (addr, addrlen,
885 plugin->ext_addr->address,
886 plugin->ext_addr->address_length)))
888 /* Checking HTTP_OPTIONS_VERIFY_CERTIFICATE option for external hostname */
889 if ((ntohl (haddr->options) & HTTP_OPTIONS_VERIFY_CERTIFICATE) !=
890 (plugin->options & HTTP_OPTIONS_VERIFY_CERTIFICATE))
891 return GNUNET_NO; /* VERIFY option not set as required! */
894 next = plugin->addr_head;
895 while (NULL != (pos = next))
898 if (GNUNET_YES == (http_common_cmp_addresses(addr,
909 * Creates a new outbound session the transport
910 * service will use to send data to the peer.
912 * Since HTTP/S server cannot create sessions, always returns NULL.
914 * @param cls the plugin
915 * @param address the address
916 * @return always NULL
918 static struct GNUNET_ATS_Session *
919 http_server_plugin_get_session (void *cls,
920 const struct GNUNET_HELLO_Address *address)
927 * Call MHD IPv4 to process pending requests and then go back
928 * and schedule the next run.
930 * @param cls plugin as closure
933 server_v4_run (void *cls)
935 struct HTTP_Server_Plugin *plugin = cls;
937 plugin->server_v4_task = NULL;
938 plugin->server_v4_immediately = GNUNET_NO;
939 GNUNET_assert (MHD_YES == MHD_run (plugin->server_v4));
940 server_reschedule (plugin, plugin->server_v4, GNUNET_NO);
945 * Call MHD IPv6 to process pending requests and then go back
946 * and schedule the next run.
948 * @param cls plugin as closure
951 server_v6_run (void *cls)
953 struct HTTP_Server_Plugin *plugin = cls;
955 plugin->server_v6_task = NULL;
956 plugin->server_v6_immediately = GNUNET_NO;
957 GNUNET_assert (MHD_YES == MHD_run (plugin->server_v6));
958 server_reschedule (plugin, plugin->server_v6, GNUNET_NO);
963 * Function that queries MHD's select sets and
964 * starts the task waiting for them.
966 * @param plugin plugin
967 * @param daemon_handle the MHD daemon handle
968 * @param now schedule now
969 * @return gnunet task identifier
971 static struct GNUNET_SCHEDULER_Task *
972 server_schedule (struct HTTP_Server_Plugin *plugin,
973 struct MHD_Daemon *daemon_handle,
976 struct GNUNET_SCHEDULER_Task * ret;
980 struct GNUNET_NETWORK_FDSet *wrs;
981 struct GNUNET_NETWORK_FDSet *wws;
983 MHD_UNSIGNED_LONG_LONG timeout;
984 static unsigned long long last_timeout = 0;
986 struct GNUNET_TIME_Relative tv;
988 if (GNUNET_YES == plugin->in_shutdown)
995 wrs = GNUNET_NETWORK_fdset_create ();
996 wws = GNUNET_NETWORK_fdset_create ();
998 GNUNET_assert (MHD_YES ==
999 MHD_get_fdset (daemon_handle,
1004 haveto = MHD_get_timeout (daemon_handle, &timeout);
1005 if (haveto == MHD_YES)
1007 if (timeout != last_timeout)
1009 LOG (GNUNET_ERROR_TYPE_DEBUG,
1010 "SELECT Timeout changed from %llu to %llu (ms)\n",
1011 last_timeout, timeout);
1012 last_timeout = timeout;
1014 if (timeout <= GNUNET_TIME_UNIT_SECONDS.rel_value_us / 1000LL)
1015 tv.rel_value_us = (uint64_t) timeout * 1000LL;
1017 tv = GNUNET_TIME_UNIT_SECONDS;
1020 tv = GNUNET_TIME_UNIT_SECONDS;
1021 /* Force immediate run, since we have outbound data to send */
1022 if (now == GNUNET_YES)
1023 tv = GNUNET_TIME_UNIT_MILLISECONDS;
1024 GNUNET_NETWORK_fdset_copy_native (wrs, &rs, max + 1);
1025 GNUNET_NETWORK_fdset_copy_native (wws, &ws, max + 1);
1027 if (daemon_handle == plugin->server_v4)
1029 if (plugin->server_v4_task != NULL)
1031 GNUNET_SCHEDULER_cancel (plugin->server_v4_task);
1032 plugin->server_v4_task = NULL;
1035 LOG (GNUNET_ERROR_TYPE_DEBUG,
1036 "Scheduling IPv4 server task in %llu ms\n",
1040 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1042 &server_v4_run, plugin);
1044 if (daemon_handle == plugin->server_v6)
1046 if (plugin->server_v6_task != NULL)
1048 GNUNET_SCHEDULER_cancel (plugin->server_v6_task);
1049 plugin->server_v6_task = NULL;
1052 LOG (GNUNET_ERROR_TYPE_DEBUG,
1053 "Scheduling IPv6 server task in %llu ms\n", tv);
1056 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1058 &server_v6_run, plugin);
1060 GNUNET_NETWORK_fdset_destroy (wrs);
1061 GNUNET_NETWORK_fdset_destroy (wws);
1067 * Reschedule the execution of both IPv4 and IPv6 server
1069 * @param plugin the plugin
1070 * @param server which server to schedule v4 or v6?
1071 * @param now #GNUNET_YES to schedule execution immediately, #GNUNET_NO to wait
1075 server_reschedule (struct HTTP_Server_Plugin *plugin,
1076 struct MHD_Daemon *server,
1079 if ((server == plugin->server_v4) && (plugin->server_v4 != NULL))
1081 if (GNUNET_YES == plugin->server_v4_immediately)
1082 return; /* No rescheduling, server will run asap */
1084 if (GNUNET_YES == now)
1085 plugin->server_v4_immediately = GNUNET_YES;
1087 if (plugin->server_v4_task != NULL)
1089 GNUNET_SCHEDULER_cancel (plugin->server_v4_task);
1090 plugin->server_v4_task = NULL;
1092 plugin->server_v4_task = server_schedule (plugin, plugin->server_v4, now);
1095 if ((server == plugin->server_v6) && (plugin->server_v6 != NULL))
1097 if (GNUNET_YES == plugin->server_v6_immediately)
1098 return; /* No rescheduling, server will run asap */
1100 if (GNUNET_YES == now)
1101 plugin->server_v6_immediately = GNUNET_YES;
1103 if (plugin->server_v6_task != NULL)
1105 GNUNET_SCHEDULER_cancel (plugin->server_v6_task);
1106 plugin->server_v6_task = NULL;
1108 plugin->server_v6_task = server_schedule (plugin, plugin->server_v6, now);
1114 * Function that is called to get the keepalive factor.
1115 * GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT is divided by this number to
1116 * calculate the interval between keepalive packets.
1118 * @param cls closure with the `struct HTTP_Server_Plugin`
1119 * @return keepalive factor
1122 http_server_query_keepalive_factor (void *cls)
1129 * Function that will be called whenever the transport service wants to
1130 * notify the plugin that a session is still active and in use and
1131 * therefore the session timeout for this session has to be updated
1133 * @param cls closure
1134 * @param peer which peer was the session for
1135 * @param session which session is being updated
1138 http_server_plugin_update_session_timeout (void *cls,
1139 const struct GNUNET_PeerIdentity *peer,
1140 struct GNUNET_ATS_Session *session)
1142 server_reschedule_session_timeout (session);
1147 * Tell MHD that the connection should timeout after @a to seconds.
1149 * @param plugin our plugin
1150 * @param s session for which the timeout changes
1151 * @param to timeout in seconds
1154 server_mhd_connection_timeout (struct HTTP_Server_Plugin *plugin,
1155 struct GNUNET_ATS_Session *s,
1158 /* Setting timeouts for other connections */
1159 if (NULL != s->server_recv)
1161 LOG (GNUNET_ERROR_TYPE_DEBUG,
1162 "Setting timeout for %p to %u sec.\n",
1163 s->server_recv, to);
1164 MHD_set_connection_option (s->server_recv->mhd_conn,
1165 MHD_CONNECTION_OPTION_TIMEOUT,
1167 server_reschedule (plugin, s->server_recv->mhd_daemon, GNUNET_NO);
1169 if (NULL != s->server_send)
1171 LOG (GNUNET_ERROR_TYPE_DEBUG,
1172 "Setting timeout for %p to %u sec.\n",
1173 s->server_send, to);
1174 MHD_set_connection_option (s->server_send->mhd_conn,
1175 MHD_CONNECTION_OPTION_TIMEOUT,
1177 server_reschedule (plugin, s->server_send->mhd_daemon, GNUNET_NO);
1183 * Parse incoming URL for tag and target
1185 * @param plugin plugin
1186 * @param url incoming url
1187 * @param target where to store the target
1188 * @param tag where to store the tag
1189 * @param options where to store the options
1190 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
1193 server_parse_url (struct HTTP_Server_Plugin *plugin,
1195 struct GNUNET_PeerIdentity *target,
1199 regmatch_t matches[4];
1200 const char *tag_start;
1201 const char *target_start;
1205 unsigned long int rc;
1209 ("^.*/([0-9A-Z]+);([0-9]+)(,[0-9]+)?$")
1214 return GNUNET_SYSERR;
1217 if (regexec(&plugin->url_regex, url, 4, matches, 0))
1219 LOG (GNUNET_ERROR_TYPE_DEBUG,
1220 "URL `%s' did not match regex\n", url);
1221 return GNUNET_SYSERR;
1224 target_start = &url[matches[1].rm_so];
1225 tag_start = &url[matches[2].rm_so];
1228 rc = strtoul (tag_start, &tag_end, 10);
1229 if (&url[matches[2].rm_eo] != tag_end)
1231 LOG (GNUNET_ERROR_TYPE_DEBUG,
1232 "URL tag did not line up with submatch\n");
1233 return GNUNET_SYSERR;
1237 LOG (GNUNET_ERROR_TYPE_DEBUG,
1238 "URL tag is zero\n");
1239 return GNUNET_SYSERR;
1241 if ((rc == ULONG_MAX) && (ERANGE == errno))
1243 LOG (GNUNET_ERROR_TYPE_DEBUG,
1244 "URL tag > ULONG_MAX\n");
1245 return GNUNET_SYSERR;
1247 if (rc > UINT32_MAX)
1249 LOG (GNUNET_ERROR_TYPE_DEBUG,
1250 "URL tag > UINT32_MAX\n");
1251 return GNUNET_SYSERR;
1253 (*tag) = (uint32_t)rc;
1254 LOG (GNUNET_ERROR_TYPE_DEBUG,
1255 "Found tag `%u' in url\n",
1258 /* convert peer id */
1259 hash_length = matches[1].rm_eo - matches[1].rm_so;
1260 if (hash_length != plugin->peer_id_length)
1262 LOG (GNUNET_ERROR_TYPE_DEBUG,
1263 "URL target is %u bytes, expecting %u\n",
1264 hash_length, plugin->peer_id_length);
1265 return GNUNET_SYSERR;
1268 GNUNET_CRYPTO_eddsa_public_key_from_string (target_start,
1270 &target->public_key))
1272 LOG (GNUNET_ERROR_TYPE_DEBUG,
1273 "URL target conversion failed\n");
1274 return GNUNET_SYSERR;
1276 LOG (GNUNET_ERROR_TYPE_DEBUG,
1277 "Found target `%s' in URL\n",
1278 GNUNET_i2s_full (target));
1280 /* convert options */
1281 if (-1 == matches[3].rm_so)
1287 rc = strtoul (&url[matches[3].rm_so + 1], &options_end, 10);
1288 if (&url[matches[3].rm_eo] != options_end)
1290 LOG (GNUNET_ERROR_TYPE_DEBUG,
1291 "URL options did not line up with submatch\n");
1292 return GNUNET_SYSERR;
1294 if ((rc == ULONG_MAX) && (ERANGE == errno))
1296 LOG (GNUNET_ERROR_TYPE_DEBUG,
1297 "URL options > ULONG_MAX\n");
1298 return GNUNET_SYSERR;
1300 if (rc > UINT32_MAX)
1302 LOG (GNUNET_ERROR_TYPE_DEBUG,
1303 "URL options > UINT32_MAX\n");
1304 return GNUNET_SYSERR;
1306 (*options) = (uint32_t) rc;
1307 LOG (GNUNET_ERROR_TYPE_DEBUG,
1308 "Found options `%u' in url\n",
1316 * Closure for #session_tag_it().
1318 struct GNUNET_ATS_SessionTagContext
1321 * Set to session matching the tag.
1323 struct GNUNET_ATS_Session *res;
1326 * Tag we are looking for.
1333 * Find a session with a matching tag.
1335 * @param cls the `struct GNUNET_ATS_SessionTagContext *`
1336 * @param key peer identity (unused)
1337 * @param value the `struct GNUNET_ATS_Session *`
1338 * @return #GNUNET_NO if we found the session, #GNUNET_OK if not
1341 session_tag_it (void *cls,
1342 const struct GNUNET_PeerIdentity *key,
1345 struct GNUNET_ATS_SessionTagContext *stc = cls;
1346 struct GNUNET_ATS_Session *s = value;
1348 if (s->tag == stc->tag)
1358 * Lookup a mhd connection and create one if none is found
1360 * @param plugin the plugin handle
1361 * @param mhd_connection the incoming mhd_connection
1362 * @param url incoming requested URL
1363 * @param method PUT or GET
1364 * @return the server connecetion
1366 static struct ServerRequest *
1367 server_lookup_connection (struct HTTP_Server_Plugin *plugin,
1368 struct MHD_Connection *mhd_connection,
1372 struct GNUNET_ATS_Session *s = NULL;
1373 struct ServerRequest *sc = NULL;
1374 const union MHD_ConnectionInfo *conn_info;
1375 struct HttpAddress *addr;
1376 struct GNUNET_PeerIdentity target;
1378 struct GNUNET_ATS_SessionTagContext stc;
1380 int direction = GNUNET_SYSERR;
1382 enum GNUNET_ATS_Network_Type scope;
1384 conn_info = MHD_get_connection_info (mhd_connection,
1385 MHD_CONNECTION_INFO_CLIENT_ADDRESS);
1386 if ((conn_info->client_addr->sa_family != AF_INET) &&
1387 (conn_info->client_addr->sa_family != AF_INET6))
1389 LOG (GNUNET_ERROR_TYPE_DEBUG,
1390 "New %s request from %s\n",
1394 options = 0; /* make gcc happy */
1395 if (GNUNET_SYSERR ==
1396 server_parse_url (plugin, url, &target, &stc.tag, &options))
1398 LOG (GNUNET_ERROR_TYPE_DEBUG,
1399 "Invalid url %s\n", url);
1402 if (0 == strcmp (MHD_HTTP_METHOD_PUT, method))
1403 direction = _RECEIVE;
1404 else if (0 == strcmp (MHD_HTTP_METHOD_GET, method))
1408 LOG (GNUNET_ERROR_TYPE_DEBUG,
1409 "Invalid method %s for request from %s\n",
1414 plugin->cur_request++;
1415 LOG (GNUNET_ERROR_TYPE_DEBUG,
1416 "New %s request from %s with tag %u (%u of %u)\n",
1418 GNUNET_i2s (&target),
1420 plugin->cur_request, plugin->max_request);
1421 /* find existing session */
1423 GNUNET_CONTAINER_multipeermap_get_multiple (plugin->sessions,
1427 if (NULL == (s = stc.res))
1429 /* create new session */
1431 switch (conn_info->client_addr->sa_family)
1434 addr = http_common_address_from_socket (plugin->protocol,
1435 conn_info->client_addr,
1436 sizeof (struct sockaddr_in));
1437 addr_len = http_common_address_get_size (addr);
1438 scope = plugin->env->get_address_type (plugin->env->cls,
1439 conn_info->client_addr,
1440 sizeof (struct sockaddr_in));
1443 addr = http_common_address_from_socket (plugin->protocol,
1444 conn_info->client_addr,
1445 sizeof (struct sockaddr_in6));
1446 addr_len = http_common_address_get_size (addr);
1447 scope = plugin->env->get_address_type (plugin->env->cls,
1448 conn_info->client_addr,
1449 sizeof (struct sockaddr_in6));
1452 /* external host name */
1455 s = GNUNET_new (struct GNUNET_ATS_Session);
1459 s->address = GNUNET_HELLO_address_allocate (&s->target,
1463 GNUNET_HELLO_ADDRESS_INFO_INBOUND);
1464 s->next_receive = GNUNET_TIME_UNIT_ZERO_ABS;
1466 s->timeout = GNUNET_TIME_relative_to_absolute (HTTP_SERVER_SESSION_TIMEOUT);
1467 s->timeout_task = GNUNET_SCHEDULER_add_delayed (HTTP_SERVER_SESSION_TIMEOUT,
1468 &server_session_timeout,
1470 (void) GNUNET_CONTAINER_multipeermap_put (plugin->sessions,
1473 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
1474 notify_session_monitor (plugin,
1476 GNUNET_TRANSPORT_SS_INIT);
1477 notify_session_monitor (plugin,
1479 GNUNET_TRANSPORT_SS_HANDSHAKE);
1480 LOG (GNUNET_ERROR_TYPE_DEBUG,
1481 "Creating new session %p for peer `%s' connecting from `%s'\n",
1482 s, GNUNET_i2s (&target),
1483 http_common_plugin_address_to_string (plugin->protocol,
1486 GNUNET_free_non_null (addr);
1489 if ( (_RECEIVE == direction) &&
1490 (NULL != s->server_recv) )
1492 LOG (GNUNET_ERROR_TYPE_DEBUG,
1493 "Duplicate PUT request from `%s' tag %u, dismissing new request\n",
1494 GNUNET_i2s (&target),
1498 if ((_SEND == direction) && (NULL != s->server_send))
1500 LOG (GNUNET_ERROR_TYPE_DEBUG,
1501 "Duplicate GET request from `%s' tag %u, dismissing new request\n",
1502 GNUNET_i2s (&target),
1506 sc = GNUNET_new (struct ServerRequest);
1507 if (conn_info->client_addr->sa_family == AF_INET)
1508 sc->mhd_daemon = plugin->server_v4;
1509 if (conn_info->client_addr->sa_family == AF_INET6)
1510 sc->mhd_daemon = plugin->server_v6;
1511 sc->mhd_conn = mhd_connection;
1512 sc->direction = direction;
1513 sc->connected = GNUNET_NO;
1515 sc->options = options;
1516 if (direction == _SEND)
1518 s->server_send = sc;
1520 if (direction == _RECEIVE)
1522 s->server_recv = sc;
1525 if ((GNUNET_NO == s->known_to_service) &&
1526 (NULL != s->server_send) &&
1527 (NULL != s->server_recv) )
1529 s->known_to_service = GNUNET_YES;
1530 notify_session_monitor (plugin,
1532 GNUNET_TRANSPORT_SS_UP);
1533 plugin->env->session_start (plugin->env->cls,
1539 to = (HTTP_SERVER_SESSION_TIMEOUT.rel_value_us / 1000LL / 1000LL);
1540 server_mhd_connection_timeout (plugin, s, to);
1546 * Callback called by MHD when it needs data to send
1548 * @param cls current session
1549 * @param pos position in buffer
1550 * @param buf the buffer to write data to
1551 * @param max max number of bytes available in @a buf
1552 * @return bytes written to @a buf
1555 server_send_callback (void *cls,
1560 struct ServerRequest *sc = cls;
1561 struct GNUNET_ATS_Session *s = sc->session;
1562 ssize_t bytes_read = 0;
1563 struct HTTP_Message *msg;
1568 /* session is disconnecting */
1572 sc = s->server_send;
1579 bytes_read = GNUNET_MIN (msg->size - msg->pos,
1581 GNUNET_memcpy (buf, &msg->buf[msg->pos], bytes_read);
1582 msg->pos += bytes_read;
1584 /* removing message */
1585 if (msg->pos == msg->size)
1587 GNUNET_CONTAINER_DLL_remove (s->msg_head,
1590 if (NULL != msg->transmit_cont)
1591 msg->transmit_cont (msg->transmit_cont_cls, &s->target, GNUNET_OK,
1592 msg->size, msg->size + msg->overhead);
1593 GNUNET_assert (s->msgs_in_queue > 0);
1595 GNUNET_assert (s->bytes_in_queue >= msg->size);
1596 s->bytes_in_queue -= msg->size;
1598 notify_session_monitor (s->plugin,
1600 GNUNET_TRANSPORT_SS_UPDATE);
1605 sc->connected = GNUNET_YES;
1606 LOG (GNUNET_ERROR_TYPE_DEBUG,
1607 "Sent %u bytes to peer `%s' with session %p \n",
1609 GNUNET_i2s (&s->target),
1611 GNUNET_asprintf (&stat_txt,
1612 "# bytes currently in %s_server buffers",
1613 s->plugin->protocol);
1614 GNUNET_STATISTICS_update (s->plugin->env->stats,
1618 GNUNET_free (stat_txt);
1619 GNUNET_asprintf (&stat_txt,
1620 "# bytes transmitted via %s_server",
1621 s->plugin->protocol);
1622 GNUNET_STATISTICS_update (s->plugin->env->stats,
1623 stat_txt, bytes_read, GNUNET_NO);
1624 GNUNET_free (stat_txt);
1626 else if ((sc->options & OPTION_LONG_POLL) && sc->connected)
1628 LOG (GNUNET_ERROR_TYPE_DEBUG,
1629 "Completing GET response to peer `%s' with session %p\n",
1630 GNUNET_i2s (&s->target),
1632 return MHD_CONTENT_READER_END_OF_STREAM;
1636 MHD_suspend_connection (s->server_send->mhd_conn);
1637 s->server_send->suspended = true;
1645 * Callback called by MessageStreamTokenizer when a message has arrived
1647 * @param cls current session as closure
1648 * @param message the message to be forwarded to transport service
1649 * @return #GNUNET_OK
1652 server_receive_mst_cb (void *cls,
1653 const struct GNUNET_MessageHeader *message)
1655 struct GNUNET_ATS_Session *s = cls;
1656 struct HTTP_Server_Plugin *plugin = s->plugin;
1657 struct GNUNET_TIME_Relative delay;
1660 if (GNUNET_NO == s->known_to_service)
1662 s->known_to_service = GNUNET_YES;
1663 plugin->env->session_start (plugin->env->cls,
1667 notify_session_monitor (plugin,
1669 GNUNET_TRANSPORT_SS_UP);
1671 delay = plugin->env->receive (plugin->env->cls,
1675 GNUNET_asprintf (&stat_txt,
1676 "# bytes received via %s_server",
1678 GNUNET_STATISTICS_update (plugin->env->stats,
1680 ntohs (message->size),
1682 GNUNET_free (stat_txt);
1683 s->next_receive = GNUNET_TIME_relative_to_absolute (delay);
1684 if (delay.rel_value_us > 0)
1686 LOG (GNUNET_ERROR_TYPE_DEBUG,
1687 "Peer `%s' address `%s' next read delayed for %s\n",
1688 GNUNET_i2s (&s->target),
1689 http_common_plugin_address_to_string (plugin->protocol,
1690 s->address->address,
1691 s->address->address_length),
1692 GNUNET_STRINGS_relative_time_to_string (delay,
1695 server_reschedule_session_timeout (s);
1701 * Add headers to a request indicating that we allow Cross-Origin Resource
1704 * @param response response object to modify
1707 add_cors_headers(struct MHD_Response *response)
1709 MHD_add_response_header (response,
1710 "Access-Control-Allow-Origin",
1712 MHD_add_response_header (response,
1713 "Access-Control-Allow-Methods",
1714 "GET, PUT, OPTIONS");
1715 MHD_add_response_header (response,
1716 "Access-Control-Max-Age",
1722 * MHD callback for a new incoming connection
1724 * @param cls the plugin handle
1725 * @param mhd_connection the mhd connection
1726 * @param url the requested URL
1727 * @param method GET or PUT
1728 * @param version HTTP version
1729 * @param upload_data upload data
1730 * @param upload_data_size size of @a upload_data
1731 * @param httpSessionCache the session cache to remember the connection
1732 * @return MHD_YES if connection is accepted, MHD_NO on reject
1735 server_access_cb (void *cls,
1736 struct MHD_Connection *mhd_connection,
1739 const char *version,
1740 const char *upload_data,
1741 size_t *upload_data_size,
1742 void **httpSessionCache)
1744 struct HTTP_Server_Plugin *plugin = cls;
1745 struct ServerRequest *sc = *httpSessionCache;
1746 struct GNUNET_ATS_Session *s;
1747 struct MHD_Response *response;
1750 LOG (GNUNET_ERROR_TYPE_DEBUG,
1751 _("Access from connection %p (%u of %u) for `%s' `%s' url `%s' with upload data size %u\n"),
1753 plugin->cur_request,
1754 plugin->max_request,
1758 (*upload_data_size));
1761 /* CORS pre-flight request */
1762 if (0 == strcmp (MHD_HTTP_METHOD_OPTIONS, method))
1764 response = MHD_create_response_from_buffer (0, NULL,
1765 MHD_RESPMEM_PERSISTENT);
1766 add_cors_headers(response);
1767 res = MHD_queue_response (mhd_connection, MHD_HTTP_OK, response);
1768 MHD_destroy_response (response);
1771 /* new connection */
1772 sc = server_lookup_connection (plugin, mhd_connection, url, method);
1775 /* attach to new / existing session */
1776 (*httpSessionCache) = sc;
1780 /* existing session already has matching connection, refuse */
1781 response = MHD_create_response_from_buffer (strlen (HTTP_ERROR_RESPONSE),
1782 HTTP_ERROR_RESPONSE,
1783 MHD_RESPMEM_PERSISTENT);
1784 MHD_add_response_header (response,
1785 MHD_HTTP_HEADER_CONTENT_TYPE,
1787 add_cors_headers(response);
1788 res = MHD_queue_response (mhd_connection, MHD_HTTP_NOT_FOUND, response);
1789 MHD_destroy_response (response);
1793 /* 'old' connection */
1794 if (NULL == (s = sc->session))
1796 /* Session was already disconnected;
1797 sent HTTP/1.1: 200 OK as response */
1798 response = MHD_create_response_from_buffer (strlen ("Thank you!"),
1800 MHD_RESPMEM_PERSISTENT);
1801 add_cors_headers(response);
1802 MHD_queue_response (mhd_connection, MHD_HTTP_OK, response);
1803 MHD_destroy_response (response);
1807 if (sc->direction == _SEND)
1809 response = MHD_create_response_from_callback (MHD_SIZE_UNKNOWN, 32 * 1024,
1810 &server_send_callback, sc, NULL);
1811 add_cors_headers(response);
1812 MHD_queue_response (mhd_connection, MHD_HTTP_OK, response);
1813 MHD_destroy_response (response);
1816 if (sc->direction == _RECEIVE)
1818 if ((*upload_data_size == 0) && (sc->connected == GNUNET_NO))
1820 /* (*upload_data_size == 0) first callback when header are passed */
1821 LOG (GNUNET_ERROR_TYPE_DEBUG,
1822 "Session %p / Connection %p: Peer `%s' PUT on address `%s' connected\n",
1824 GNUNET_i2s (&s->target),
1825 http_common_plugin_address_to_string (plugin->protocol,
1826 s->address->address,
1827 s->address->address_length));
1828 sc->connected = GNUNET_YES;
1831 else if ((*upload_data_size == 0) && (sc->connected == GNUNET_YES))
1833 /* (*upload_data_size == 0) when upload is complete */
1834 LOG (GNUNET_ERROR_TYPE_DEBUG,
1835 "Session %p / Connection %p: Peer `%s' PUT on address `%s' finished upload\n",
1837 GNUNET_i2s (&s->target),
1838 http_common_plugin_address_to_string (plugin->protocol,
1839 s->address->address,
1840 s->address->address_length));
1841 sc->connected = GNUNET_NO;
1842 /* Sent HTTP/1.1: 200 OK as PUT Response\ */
1843 response = MHD_create_response_from_buffer (strlen ("Thank you!"),
1845 MHD_RESPMEM_PERSISTENT);
1846 add_cors_headers(response);
1847 MHD_queue_response (mhd_connection, MHD_HTTP_OK, response);
1848 MHD_destroy_response (response);
1851 else if ((*upload_data_size > 0) && (sc->connected == GNUNET_YES))
1853 struct GNUNET_TIME_Relative delay;
1855 /* (*upload_data_size > 0) for every segment received */
1856 LOG (GNUNET_ERROR_TYPE_DEBUG,
1857 "Session %p / Connection %p: Peer `%s' PUT on address `%s' received %u bytes\n",
1859 GNUNET_i2s (&s->target),
1860 http_common_plugin_address_to_string (plugin->protocol,
1861 s->address->address,
1862 s->address->address_length),
1864 delay = GNUNET_TIME_absolute_get_remaining (s->next_receive);
1865 if (0 == delay.rel_value_us)
1867 LOG (GNUNET_ERROR_TYPE_DEBUG,
1868 "PUT with %u bytes forwarded to MST\n",
1870 if (s->msg_tk == NULL)
1872 s->msg_tk = GNUNET_MST_create (&server_receive_mst_cb,
1875 GNUNET_MST_from_buffer (s->msg_tk,
1878 GNUNET_NO, GNUNET_NO);
1879 server_mhd_connection_timeout (plugin, s,
1880 GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT.rel_value_us / 1000LL
1882 (*upload_data_size) = 0;
1886 /* delay processing */
1887 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1888 "Session %p / Connection %p: no inbound bandwidth available! Next read was delayed by %s\n",
1891 GNUNET_STRINGS_relative_time_to_string (delay,
1893 GNUNET_assert(s->server_recv->mhd_conn == mhd_connection);
1894 MHD_suspend_connection (s->server_recv->mhd_conn);
1895 s->server_recv->suspended = true;
1896 if (NULL == s->recv_wakeup_task)
1898 = GNUNET_SCHEDULER_add_delayed (delay,
1915 * Callback from MHD when a connection disconnects
1917 * @param cls closure with the `struct HTTP_Server_Plugin *`
1918 * @param connection the disconnected MHD connection
1919 * @param httpSessionCache the pointer to distinguish
1922 server_disconnect_cb (void *cls,
1923 struct MHD_Connection *connection,
1924 void **httpSessionCache)
1926 struct HTTP_Server_Plugin *plugin = cls;
1927 struct ServerRequest *sc = *httpSessionCache;
1929 LOG (GNUNET_ERROR_TYPE_DEBUG,
1930 "Disconnect for connection %p\n",
1934 /* CORS pre-flight request finished */
1938 if (NULL != sc->session)
1940 if (sc->direction == _SEND)
1942 LOG (GNUNET_ERROR_TYPE_DEBUG,
1943 "Peer `%s' connection %p, GET on address `%s' disconnected\n",
1944 GNUNET_i2s (&sc->session->target),
1945 sc->session->server_send,
1946 http_common_plugin_address_to_string (plugin->protocol,
1947 sc->session->address->address,
1948 sc->session->address->address_length));
1950 sc->session->server_send = NULL;
1952 else if (sc->direction == _RECEIVE)
1954 LOG (GNUNET_ERROR_TYPE_DEBUG,
1955 "Peer `%s' connection %p PUT on address `%s' disconnected\n",
1956 GNUNET_i2s (&sc->session->target),
1957 sc->session->server_recv,
1958 http_common_plugin_address_to_string (plugin->protocol,
1959 sc->session->address->address,
1960 sc->session->address->address_length));
1961 sc->session->server_recv = NULL;
1962 if (NULL != sc->session->msg_tk)
1964 GNUNET_MST_destroy (sc->session->msg_tk);
1965 sc->session->msg_tk = NULL;
1970 plugin->cur_request--;
1975 * Check if incoming connection is accepted.
1977 * @param cls plugin as closure
1978 * @param addr address of incoming connection
1979 * @param addr_len number of bytes in @a addr
1980 * @return MHD_YES if connection is accepted, MHD_NO if connection is rejected
1983 server_accept_cb (void *cls,
1984 const struct sockaddr *addr,
1987 struct HTTP_Server_Plugin *plugin = cls;
1989 if (plugin->cur_request <= plugin->max_request)
1991 LOG (GNUNET_ERROR_TYPE_DEBUG,
1992 _("Accepting connection (%u of %u) from `%s'\n"),
1993 plugin->cur_request, plugin->max_request,
1994 GNUNET_a2s (addr, addr_len));
1999 LOG (GNUNET_ERROR_TYPE_WARNING,
2000 _("Server reached maximum number connections (%u), rejecting new connection\n"),
2001 plugin->max_request);
2008 * Log function called by MHD.
2011 * @param fmt format string
2012 * @param ap arguments for the format string (va_start() and va_end()
2013 * will be called by MHD)
2016 server_log (void *arg,
2026 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2034 * Load ssl certificate from file
2036 * @param file filename
2037 * @return content of the file
2040 server_load_file (const char *file)
2042 struct GNUNET_DISK_FileHandle *gn_file;
2046 if (GNUNET_OK != GNUNET_DISK_file_size (file,
2047 &fsize, GNUNET_NO, GNUNET_YES))
2049 text = GNUNET_malloc (fsize + 1);
2051 GNUNET_DISK_file_open (file, GNUNET_DISK_OPEN_READ,
2052 GNUNET_DISK_PERM_USER_READ);
2053 if (NULL == gn_file)
2058 if (GNUNET_SYSERR == GNUNET_DISK_file_read (gn_file, text, fsize))
2061 GNUNET_DISK_file_close (gn_file);
2065 GNUNET_DISK_file_close (gn_file);
2073 * Load ssl certificate
2075 * @param plugin the plugin
2076 * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
2079 server_load_certificate (struct HTTP_Server_Plugin *plugin)
2081 int res = GNUNET_OK;
2087 GNUNET_CONFIGURATION_get_value_filename (plugin->env->cfg,
2089 "KEY_FILE", &key_file))
2091 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
2092 plugin->name, "CERT_FILE");
2093 return GNUNET_SYSERR;
2096 GNUNET_CONFIGURATION_get_value_filename (plugin->env->cfg,
2098 "CERT_FILE", &cert_file))
2100 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
2101 plugin->name, "CERT_FILE");
2102 GNUNET_free (key_file);
2103 return GNUNET_SYSERR;
2105 /* Get crypto init string from config. If not present, use
2108 GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg,
2111 &plugin->crypto_init))
2112 LOG (GNUNET_ERROR_TYPE_DEBUG,
2113 "Using crypto init string `%s'\n",
2114 plugin->crypto_init);
2116 LOG (GNUNET_ERROR_TYPE_DEBUG,
2117 "Using default crypto init string \n");
2119 /* read key & certificates from file */
2120 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2121 "Trying to loading TLS certificate from key-file `%s' cert-file`%s'\n",
2122 key_file, cert_file);
2124 plugin->key = server_load_file (key_file);
2125 plugin->cert = server_load_file (cert_file);
2127 if ((plugin->key == NULL) || (plugin->cert == NULL))
2129 struct GNUNET_OS_Process *cert_creation;
2131 GNUNET_free_non_null (plugin->key);
2133 GNUNET_free_non_null (plugin->cert);
2134 plugin->cert = NULL;
2136 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2137 "No usable TLS certificate found, creating certificate\n");
2140 GNUNET_OS_start_process (GNUNET_NO, GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
2142 "gnunet-transport-certificate-creation",
2143 "gnunet-transport-certificate-creation",
2147 if (NULL == cert_creation)
2149 LOG (GNUNET_ERROR_TYPE_ERROR,
2150 _("Could not create a new TLS certificate, program `gnunet-transport-certificate-creation' could not be started!\n"));
2151 GNUNET_free (key_file);
2152 GNUNET_free (cert_file);
2154 GNUNET_free_non_null (plugin->key);
2156 GNUNET_free_non_null (plugin->cert);
2157 plugin->cert = NULL;
2158 GNUNET_free_non_null (plugin->crypto_init);
2159 plugin->crypto_init = NULL;
2161 return GNUNET_SYSERR;
2163 GNUNET_assert (GNUNET_OK == GNUNET_OS_process_wait (cert_creation));
2164 GNUNET_OS_process_destroy (cert_creation);
2166 plugin->key = server_load_file (key_file);
2167 plugin->cert = server_load_file (cert_file);
2170 if ((plugin->key == NULL) || (plugin->cert == NULL))
2172 LOG (GNUNET_ERROR_TYPE_ERROR,
2173 _("No usable TLS certificate found and creating one at `%s/%s' failed!\n"),
2174 key_file, cert_file);
2175 GNUNET_free (key_file);
2176 GNUNET_free (cert_file);
2178 GNUNET_free_non_null (plugin->key);
2180 GNUNET_free_non_null (plugin->cert);
2181 plugin->cert = NULL;
2182 GNUNET_free_non_null (plugin->crypto_init);
2183 plugin->crypto_init = NULL;
2185 return GNUNET_SYSERR;
2187 GNUNET_free (key_file);
2188 GNUNET_free (cert_file);
2189 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2190 "TLS certificate loaded\n");
2197 * Invoke `MHD_start_daemon` with the various options we need to
2198 * setup the HTTP server with the given listen address.
2200 * @param plugin our plugin
2201 * @param addr listen address to use
2202 * @param v6 MHD_NO_FLAG or MHD_USE_IPv6, depending on context
2203 * @return NULL on error
2205 static struct MHD_Daemon *
2206 run_mhd_start_daemon (struct HTTP_Server_Plugin *plugin,
2207 const struct sockaddr_in *addr,
2210 struct MHD_Daemon *server;
2211 unsigned int timeout;
2213 #if MHD_VERSION >= 0x00090E00
2214 timeout = HTTP_SERVER_NOT_VALIDATED_TIMEOUT.rel_value_us / 1000LL / 1000LL;
2215 LOG (GNUNET_ERROR_TYPE_DEBUG,
2216 "MHD can set timeout per connection! Default time out %u sec.\n",
2219 timeout = HTTP_SERVER_SESSION_TIMEOUT.rel_value_us / 1000LL / 1000LL;
2220 LOG (GNUNET_ERROR_TYPE_WARNING,
2221 "MHD cannot set timeout per connection! Default time out %u sec.\n",
2224 server = MHD_start_daemon (
2231 MHD_USE_SUSPEND_RESUME |
2234 &server_accept_cb, plugin,
2235 &server_access_cb, plugin,
2236 MHD_OPTION_SOCK_ADDR,
2238 MHD_OPTION_CONNECTION_LIMIT,
2239 (unsigned int) plugin->max_request,
2241 MHD_OPTION_HTTPS_PRIORITIES,
2242 plugin->crypto_init,
2243 MHD_OPTION_HTTPS_MEM_KEY,
2245 MHD_OPTION_HTTPS_MEM_CERT,
2248 MHD_OPTION_CONNECTION_TIMEOUT,
2250 MHD_OPTION_CONNECTION_MEMORY_LIMIT,
2252 GNUNET_MAX_MESSAGE_SIZE),
2253 MHD_OPTION_NOTIFY_COMPLETED,
2254 &server_disconnect_cb, plugin,
2255 MHD_OPTION_EXTERNAL_LOGGER,
2259 if ( (NULL != server) &&
2260 (0 != (plugin->options & HTTP_OPTIONS_TCP_STEALTH)) )
2262 const union MHD_DaemonInfo *di;
2264 di = MHD_get_daemon_info (server,
2265 MHD_DAEMON_INFO_LISTEN_FD,
2267 if ( (0 != setsockopt ((int) di->listen_fd,
2270 plugin->env->my_identity,
2271 sizeof (struct GNUNET_PeerIdentity))) )
2273 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2274 _("TCP_STEALTH not supported on this platform.\n"));
2275 MHD_stop_daemon (server);
2285 * Start the HTTP server
2287 * @param plugin the plugin handle
2288 * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
2291 server_start (struct HTTP_Server_Plugin *plugin)
2295 GNUNET_assert (NULL != plugin);
2297 if (GNUNET_SYSERR == server_load_certificate (plugin))
2299 LOG (GNUNET_ERROR_TYPE_ERROR,
2300 _("Could not load or create server certificate! Loading plugin failed!\n"));
2301 return GNUNET_SYSERR;
2307 plugin->server_v4 = NULL;
2308 if (GNUNET_YES == plugin->use_ipv4)
2311 = run_mhd_start_daemon (plugin,
2312 (const struct sockaddr_in *) plugin->server_addr_v4,
2315 if (NULL == plugin->server_v4)
2317 LOG (GNUNET_ERROR_TYPE_ERROR,
2318 "Failed to start %s IPv4 server component on port %u\n",
2323 server_reschedule (plugin,
2329 plugin->server_v6 = NULL;
2330 if (GNUNET_YES == plugin->use_ipv6)
2333 = run_mhd_start_daemon (plugin,
2334 (const struct sockaddr_in *) plugin->server_addr_v6,
2336 if (NULL == plugin->server_v6)
2338 LOG (GNUNET_ERROR_TYPE_ERROR,
2339 "Failed to start %s IPv6 server component on port %u\n",
2345 server_reschedule (plugin,
2351 if ( (NULL == plugin->server_v6) &&
2352 (NULL == plugin->server_v4) )
2354 LOG (GNUNET_ERROR_TYPE_ERROR,
2355 "%s %s server component started on port %u\n",
2359 return GNUNET_SYSERR;
2361 if ((NULL != plugin->server_v6) &&
2362 (NULL != plugin->server_v4))
2363 msg = "IPv4 and IPv6";
2364 else if (NULL != plugin->server_v6)
2366 else if (NULL != plugin->server_v4)
2368 LOG (GNUNET_ERROR_TYPE_DEBUG,
2369 "%s %s server component started on port %u\n",
2378 * Add an address to the server's set of addresses and notify transport
2380 * @param cls the plugin handle
2381 * @param add_remove #GNUNET_YES on add, #GNUNET_NO on remove
2382 * @param addr the address
2383 * @param addrlen address length
2386 server_add_address (void *cls,
2388 const struct sockaddr *addr,
2391 struct HTTP_Server_Plugin *plugin = cls;
2392 struct GNUNET_HELLO_Address *address;
2393 struct HttpAddressWrapper *w = NULL;
2395 w = GNUNET_new (struct HttpAddressWrapper);
2396 w->address = http_common_address_from_socket (plugin->protocol,
2399 if (NULL == w->address)
2404 w->addrlen = http_common_address_get_size (w->address);
2406 GNUNET_CONTAINER_DLL_insert (plugin->addr_head,
2409 LOG (GNUNET_ERROR_TYPE_DEBUG,
2410 "Notifying transport to add address `%s'\n",
2411 http_common_plugin_address_to_string (plugin->protocol,
2414 /* modify our published address list */
2416 address = GNUNET_HELLO_address_allocate (plugin->env->my_identity,
2417 "https_client", w->address, w->addrlen, GNUNET_HELLO_ADDRESS_INFO_NONE);
2419 address = GNUNET_HELLO_address_allocate (plugin->env->my_identity,
2420 "http_client", w->address, w->addrlen, GNUNET_HELLO_ADDRESS_INFO_NONE);
2423 plugin->env->notify_address (plugin->env->cls,
2426 GNUNET_HELLO_address_free (address);
2431 * Remove an address from the server's set of addresses and notify transport
2433 * @param cls the plugin handle
2434 * @param add_remove #GNUNET_YES on add, #GNUNET_NO on remove
2435 * @param addr the address
2436 * @param addrlen address length
2439 server_remove_address (void *cls,
2441 const struct sockaddr *addr,
2444 struct HTTP_Server_Plugin *plugin = cls;
2445 struct GNUNET_HELLO_Address *address;
2446 struct HttpAddressWrapper *w = plugin->addr_head;
2450 saddr = http_common_address_from_socket (plugin->protocol,
2455 saddr_len = http_common_address_get_size (saddr);
2460 http_common_cmp_addresses (w->address,
2467 GNUNET_free (saddr);
2472 LOG (GNUNET_ERROR_TYPE_DEBUG,
2473 "Notifying transport to remove address `%s'\n",
2474 http_common_plugin_address_to_string (plugin->protocol,
2477 GNUNET_CONTAINER_DLL_remove (plugin->addr_head,
2480 /* modify our published address list */
2482 address = GNUNET_HELLO_address_allocate (plugin->env->my_identity,
2483 "https_client", w->address, w->addrlen, GNUNET_HELLO_ADDRESS_INFO_NONE);
2485 address = GNUNET_HELLO_address_allocate (plugin->env->my_identity,
2486 "http_client", w->address, w->addrlen, GNUNET_HELLO_ADDRESS_INFO_NONE);
2488 plugin->env->notify_address (plugin->env->cls, add_remove, address);
2489 GNUNET_HELLO_address_free (address);
2490 GNUNET_free (w->address);
2497 * Our external IP address/port mapping has changed.
2499 * @param cls closure, the 'struct LocalAddrList'
2500 * @param add_remove #GNUNET_YES to mean the new public IP address, #GNUNET_NO to mean
2501 * the previous (now invalid) one
2502 * @param ac address class the address belongs to
2503 * @param addr either the previous or the new public IP address
2504 * @param addrlen actual lenght of the address
2507 server_nat_port_map_callback (void *cls,
2509 enum GNUNET_NAT_AddressClass ac,
2510 const struct sockaddr *addr,
2513 struct HTTP_Server_Plugin *plugin = cls;
2515 LOG (GNUNET_ERROR_TYPE_DEBUG,
2516 "NAT called to %s address `%s'\n",
2517 (add_remove == GNUNET_NO) ? "remove" : "add",
2518 GNUNET_a2s (addr, addrlen));
2520 if (AF_INET == addr->sa_family)
2522 struct sockaddr_in *s4 = (struct sockaddr_in *) addr;
2524 if (GNUNET_NO == plugin->use_ipv4)
2527 if ((NULL != plugin->server_addr_v4) &&
2528 (0 != memcmp (&plugin->server_addr_v4->sin_addr,
2530 sizeof (struct in_addr))))
2532 LOG (GNUNET_ERROR_TYPE_DEBUG,
2533 "Skipping address `%s' (not bindto address)\n",
2534 GNUNET_a2s (addr, addrlen));
2539 if (AF_INET6 == addr->sa_family)
2541 struct sockaddr_in6 *s6 = (struct sockaddr_in6 *) addr;
2542 if (GNUNET_NO == plugin->use_ipv6)
2545 if ((NULL != plugin->server_addr_v6) &&
2546 (0 != memcmp (&plugin->server_addr_v6->sin6_addr,
2547 &s6->sin6_addr, sizeof (struct in6_addr))))
2549 LOG (GNUNET_ERROR_TYPE_DEBUG,
2550 "Skipping address `%s' (not bindto address)\n",
2551 GNUNET_a2s (addr, addrlen));
2559 server_add_address (cls, add_remove, addr, addrlen);
2562 server_remove_address (cls, add_remove, addr, addrlen);
2569 * Get valid server addresses
2571 * @param plugin the plugin handle
2572 * @param service_name the servicename
2573 * @param cfg configuration handle
2574 * @param addrs addresses
2575 * @param addr_lens address length
2576 * @return number of addresses
2579 server_get_addresses (struct HTTP_Server_Plugin *plugin,
2580 const char *service_name,
2581 const struct GNUNET_CONFIGURATION_Handle *cfg,
2582 struct sockaddr ***addrs,
2583 socklen_t ** addr_lens)
2586 unsigned long long port;
2587 struct addrinfo hints;
2588 struct addrinfo *res;
2589 struct addrinfo *pos;
2590 struct addrinfo *next;
2594 struct sockaddr **saddrs;
2595 socklen_t *saddrlens;
2601 disablev6 = !plugin->use_ipv6;
2604 if (GNUNET_CONFIGURATION_have_value (cfg, service_name, "PORT"))
2606 GNUNET_break (GNUNET_OK ==
2607 GNUNET_CONFIGURATION_get_value_number (cfg, service_name,
2611 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2612 _("Require valid port number for service in configuration!\n"));
2613 return GNUNET_SYSERR;
2618 LOG (GNUNET_ERROR_TYPE_INFO,
2619 "Starting in listen only mode\n");
2620 return -1; /* listen only */
2624 if (GNUNET_CONFIGURATION_have_value (cfg, service_name,
2627 GNUNET_break (GNUNET_OK ==
2628 GNUNET_CONFIGURATION_get_value_string (cfg, service_name,
2629 "BINDTO", &hostname));
2634 if (NULL != hostname)
2636 LOG (GNUNET_ERROR_TYPE_DEBUG,
2637 "Resolving `%s' since that is where `%s' will bind to.\n",
2638 hostname, service_name);
2639 memset (&hints, 0, sizeof (struct addrinfo));
2641 hints.ai_family = AF_INET;
2642 if ((0 != (ret = getaddrinfo (hostname, NULL, &hints, &res))) ||
2645 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2646 _("Failed to resolve `%s': %s\n"),
2648 gai_strerror (ret));
2649 GNUNET_free (hostname);
2650 return GNUNET_SYSERR;
2654 while (NULL != (pos = next))
2656 next = pos->ai_next;
2657 if ((disablev6) && (pos->ai_family == AF_INET6))
2663 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2664 _("Failed to find %saddress for `%s'.\n"),
2665 disablev6 ? "IPv4 " : "", hostname);
2667 GNUNET_free (hostname);
2668 return GNUNET_SYSERR;
2671 saddrs = GNUNET_malloc ((resi + 1) * sizeof (struct sockaddr *));
2672 saddrlens = GNUNET_malloc ((resi + 1) * sizeof (socklen_t));
2675 while (NULL != (pos = next))
2677 next = pos->ai_next;
2678 if ((disablev6) && (pos->ai_family == AF_INET6))
2680 if ((pos->ai_protocol != IPPROTO_TCP) && (0 != pos->ai_protocol))
2681 continue; /* not TCP */
2682 if ((pos->ai_socktype != SOCK_STREAM) && (0 != pos->ai_socktype))
2683 continue; /* huh? */
2684 LOG (GNUNET_ERROR_TYPE_DEBUG,
2685 "Service will bind to `%s'\n",
2686 GNUNET_a2s (pos->ai_addr,
2688 if (pos->ai_family == AF_INET)
2690 GNUNET_assert (pos->ai_addrlen == sizeof (struct sockaddr_in));
2691 saddrlens[i] = pos->ai_addrlen;
2692 saddrs[i] = GNUNET_malloc (saddrlens[i]);
2693 GNUNET_memcpy (saddrs[i], pos->ai_addr, saddrlens[i]);
2694 ((struct sockaddr_in *) saddrs[i])->sin_port = htons (port);
2698 GNUNET_assert (pos->ai_family == AF_INET6);
2699 GNUNET_assert (pos->ai_addrlen == sizeof (struct sockaddr_in6));
2700 saddrlens[i] = pos->ai_addrlen;
2701 saddrs[i] = GNUNET_malloc (saddrlens[i]);
2702 GNUNET_memcpy (saddrs[i], pos->ai_addr, saddrlens[i]);
2703 ((struct sockaddr_in6 *) saddrs[i])->sin6_port = htons (port);
2707 GNUNET_free (hostname);
2713 /* will bind against everything, just set port */
2719 saddrs = GNUNET_malloc ((resi + 1) * sizeof (struct sockaddr *));
2720 saddrlens = GNUNET_malloc ((resi + 1) * sizeof (socklen_t));
2722 saddrlens[i] = sizeof (struct sockaddr_in);
2723 saddrs[i] = GNUNET_malloc (saddrlens[i]);
2724 #if HAVE_SOCKADDR_IN_SIN_LEN
2725 ((struct sockaddr_in *) saddrs[i])->sin_len = saddrlens[i];
2727 ((struct sockaddr_in *) saddrs[i])->sin_family = AF_INET;
2728 ((struct sockaddr_in *) saddrs[i])->sin_port = htons (port);
2734 saddrs = GNUNET_malloc ((resi + 1) * sizeof (struct sockaddr *));
2735 saddrlens = GNUNET_malloc ((resi + 1) * sizeof (socklen_t));
2737 saddrlens[i] = sizeof (struct sockaddr_in6);
2738 saddrs[i] = GNUNET_malloc (saddrlens[i]);
2739 #if HAVE_SOCKADDR_IN_SIN_LEN
2740 ((struct sockaddr_in6 *) saddrs[i])->sin6_len = saddrlens[0];
2742 ((struct sockaddr_in6 *) saddrs[i])->sin6_family = AF_INET6;
2743 ((struct sockaddr_in6 *) saddrs[i])->sin6_port = htons (port);
2745 saddrlens[i] = sizeof (struct sockaddr_in);
2746 saddrs[i] = GNUNET_malloc (saddrlens[i]);
2747 #if HAVE_SOCKADDR_IN_SIN_LEN
2748 ((struct sockaddr_in *) saddrs[i])->sin_len = saddrlens[1];
2750 ((struct sockaddr_in *) saddrs[i])->sin_family = AF_INET;
2751 ((struct sockaddr_in *) saddrs[i])->sin_port = htons (port);
2755 *addr_lens = saddrlens;
2761 * Ask NAT for addresses
2763 * @param plugin the plugin handle
2766 server_start_report_addresses (struct HTTP_Server_Plugin *plugin)
2768 int res = GNUNET_OK;
2769 struct sockaddr **addrs;
2770 socklen_t *addrlens;
2772 res = server_get_addresses (plugin,
2776 LOG (GNUNET_ERROR_TYPE_DEBUG,
2777 _("Found %u addresses to report to NAT service\n"),
2780 if (GNUNET_SYSERR == res)
2787 = GNUNET_NAT_register (plugin->env->cfg,
2788 "transport-http_server",
2791 (const struct sockaddr **) addrs,
2793 &server_nat_port_map_callback,
2799 GNUNET_assert (NULL != addrs[res]);
2800 GNUNET_free (addrs[res]);
2802 GNUNET_free_non_null (addrs);
2803 GNUNET_free_non_null (addrlens);
2808 * Stop NAT for addresses
2810 * @param plugin the plugin handle
2813 server_stop_report_addresses (struct HTTP_Server_Plugin *plugin)
2815 struct HttpAddressWrapper *w;
2817 /* Stop NAT handle */
2818 if (NULL != plugin->nat)
2820 GNUNET_NAT_unregister (plugin->nat);
2823 /* Clean up addresses */
2824 while (NULL != plugin->addr_head)
2826 w = plugin->addr_head;
2827 GNUNET_CONTAINER_DLL_remove (plugin->addr_head,
2830 GNUNET_free (w->address);
2837 * Check if IPv6 supported on this system
2839 * @param plugin the plugin handle
2840 * @return #GNUNET_YES on success, else #GNUNET_NO
2843 server_check_ipv6_support (struct HTTP_Server_Plugin *plugin)
2845 struct GNUNET_NETWORK_Handle *desc = NULL;
2846 int res = GNUNET_NO;
2848 /* Probe IPv6 support */
2849 desc = GNUNET_NETWORK_socket_create (PF_INET6,
2854 if ( (errno == ENOBUFS) ||
2855 (errno == ENOMEM) ||
2856 (errno == ENFILE) ||
2859 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR,
2862 LOG (GNUNET_ERROR_TYPE_WARNING,
2863 _("Disabling IPv6 since it is not supported on this system!\n"));
2868 GNUNET_break (GNUNET_OK ==
2869 GNUNET_NETWORK_socket_close (desc));
2873 LOG (GNUNET_ERROR_TYPE_DEBUG,
2874 "Testing IPv6 on this system: %s\n",
2875 (res == GNUNET_YES) ? "successful" : "failed");
2881 * Notify server about our external hostname
2886 server_notify_external_hostname (void *cls)
2888 struct HTTP_Server_Plugin *plugin = cls;
2889 struct HttpAddress *ext_addr;
2890 size_t ext_addr_len;
2894 plugin->notify_ext_task = NULL;
2895 GNUNET_asprintf (&url,
2898 plugin->external_hostname);
2899 urlen = strlen (url) + 1;
2900 ext_addr = GNUNET_malloc (sizeof (struct HttpAddress) + urlen);
2901 ext_addr->options = htonl (plugin->options);
2902 ext_addr->urlen = htonl (urlen);
2903 ext_addr_len = sizeof (struct HttpAddress) + urlen;
2904 GNUNET_memcpy (&ext_addr[1], url, urlen);
2907 LOG (GNUNET_ERROR_TYPE_DEBUG,
2908 "Notifying transport about external hostname address `%s'\n",
2909 plugin->external_hostname);
2912 if (GNUNET_YES == plugin->verify_external_hostname)
2913 LOG (GNUNET_ERROR_TYPE_INFO,
2914 "Enabling SSL verification for external hostname address `%s'\n",
2915 plugin->external_hostname);
2917 = GNUNET_HELLO_address_allocate (plugin->env->my_identity,
2921 GNUNET_HELLO_ADDRESS_INFO_NONE);
2922 plugin->env->notify_address (plugin->env->cls,
2925 GNUNET_free (ext_addr);
2928 = GNUNET_HELLO_address_allocate (plugin->env->my_identity,
2932 GNUNET_HELLO_ADDRESS_INFO_NONE);
2933 plugin->env->notify_address (plugin->env->cls,
2936 GNUNET_free (ext_addr);
2942 * Configure the plugin
2944 * @param plugin plugin handle
2945 * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
2948 server_configure_plugin (struct HTTP_Server_Plugin *plugin)
2950 unsigned long long port;
2951 unsigned long long max_connections;
2952 char *bind4_address = NULL;
2953 char *bind6_address = NULL;
2954 char *eh_tmp = NULL;
2955 int external_hostname_use_port;
2958 if (GNUNET_CONFIGURATION_have_value
2959 (plugin->env->cfg, plugin->name, "USE_IPv4"))
2962 GNUNET_CONFIGURATION_get_value_yesno (plugin->env->cfg,
2967 plugin->use_ipv4 = GNUNET_YES;
2968 LOG (GNUNET_ERROR_TYPE_DEBUG,
2969 _("IPv4 support is %s\n"),
2970 (plugin->use_ipv4 == GNUNET_YES) ? "enabled" : "disabled");
2973 if (GNUNET_CONFIGURATION_have_value
2974 (plugin->env->cfg, plugin->name, "USE_IPv6"))
2977 GNUNET_CONFIGURATION_get_value_yesno (plugin->env->cfg,
2982 plugin->use_ipv6 = GNUNET_YES;
2983 LOG (GNUNET_ERROR_TYPE_DEBUG,
2984 _("IPv6 support is %s\n"),
2985 (plugin->use_ipv6 == GNUNET_YES) ? "enabled" : "disabled");
2987 if ((plugin->use_ipv4 == GNUNET_NO) && (plugin->use_ipv6 == GNUNET_NO))
2989 LOG (GNUNET_ERROR_TYPE_ERROR,
2990 _("Neither IPv4 nor IPv6 are enabled! Fix in configuration\n"));
2991 return GNUNET_SYSERR;
2994 /* Reading port number from config file */
2996 GNUNET_CONFIGURATION_get_value_number (plugin->env->cfg,
2998 "PORT", &port)) || (port > 65535))
3000 LOG (GNUNET_ERROR_TYPE_ERROR,
3001 _("Port is required! Fix in configuration\n"));
3002 return GNUNET_SYSERR;
3004 plugin->port = port;
3006 LOG (GNUNET_ERROR_TYPE_INFO,
3007 _("Using port %u\n"), plugin->port);
3009 if ( (plugin->use_ipv4 == GNUNET_YES) &&
3011 GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg,
3016 LOG (GNUNET_ERROR_TYPE_DEBUG,
3017 "Binding %s plugin to specific IPv4 address: `%s'\n",
3020 plugin->server_addr_v4 = GNUNET_new (struct sockaddr_in);
3021 if (1 != inet_pton (AF_INET,
3023 &plugin->server_addr_v4->sin_addr))
3025 LOG (GNUNET_ERROR_TYPE_ERROR,
3026 _("Specific IPv4 address `%s' in configuration file is invalid!\n"),
3028 GNUNET_free (bind4_address);
3029 GNUNET_free (plugin->server_addr_v4);
3030 plugin->server_addr_v4 = NULL;
3031 return GNUNET_SYSERR;
3035 LOG (GNUNET_ERROR_TYPE_DEBUG,
3036 "Binding to IPv4 address %s\n",
3038 plugin->server_addr_v4->sin_family = AF_INET;
3039 plugin->server_addr_v4->sin_port = htons (plugin->port);
3041 GNUNET_free (bind4_address);
3044 if ((plugin->use_ipv6 == GNUNET_YES) &&
3046 GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg,
3051 LOG (GNUNET_ERROR_TYPE_DEBUG,
3052 "Binding %s plugin to specific IPv6 address: `%s'\n",
3053 plugin->protocol, bind6_address);
3054 plugin->server_addr_v6 = GNUNET_new (struct sockaddr_in6);
3056 inet_pton (AF_INET6,
3058 &plugin->server_addr_v6->sin6_addr))
3060 LOG (GNUNET_ERROR_TYPE_ERROR,
3061 _("Specific IPv6 address `%s' in configuration file is invalid!\n"),
3063 GNUNET_free (bind6_address);
3064 GNUNET_free (plugin->server_addr_v6);
3065 plugin->server_addr_v6 = NULL;
3066 return GNUNET_SYSERR;
3070 LOG (GNUNET_ERROR_TYPE_DEBUG,
3071 "Binding to IPv6 address %s\n",
3073 plugin->server_addr_v6->sin6_family = AF_INET6;
3074 plugin->server_addr_v6->sin6_port = htons (plugin->port);
3076 GNUNET_free (bind6_address);
3079 plugin->verify_external_hostname = GNUNET_NO;
3081 plugin->verify_external_hostname
3082 = GNUNET_CONFIGURATION_get_value_yesno (plugin->env->cfg,
3084 "VERIFY_EXTERNAL_HOSTNAME");
3085 if (GNUNET_SYSERR == plugin->verify_external_hostname)
3086 plugin->verify_external_hostname = GNUNET_NO;
3087 if (GNUNET_YES == plugin->verify_external_hostname)
3088 plugin->options |= HTTP_OPTIONS_VERIFY_CERTIFICATE;
3090 external_hostname_use_port
3091 = GNUNET_CONFIGURATION_get_value_yesno (plugin->env->cfg,
3093 "EXTERNAL_HOSTNAME_USE_PORT");
3094 if (GNUNET_SYSERR == external_hostname_use_port)
3095 external_hostname_use_port = GNUNET_NO;
3099 GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg,
3101 "EXTERNAL_HOSTNAME",
3106 char *pos_url = NULL;
3108 if (NULL != strstr(eh_tmp, "://"))
3109 tmp = &strstr(eh_tmp, "://")[3];
3113 if (GNUNET_YES == external_hostname_use_port)
3115 if ( (strlen (tmp) > 1) && (NULL != (pos = strchr(tmp, '/'))) )
3119 GNUNET_asprintf (&plugin->external_hostname,
3126 GNUNET_asprintf (&plugin->external_hostname,
3132 plugin->external_hostname = GNUNET_strdup (tmp);
3133 GNUNET_free (eh_tmp);
3135 LOG (GNUNET_ERROR_TYPE_INFO,
3136 _("Using external hostname `%s'\n"),
3137 plugin->external_hostname);
3138 plugin->notify_ext_task = GNUNET_SCHEDULER_add_now (&server_notify_external_hostname,
3141 /* Use only configured external hostname */
3142 if (GNUNET_CONFIGURATION_have_value
3145 "EXTERNAL_HOSTNAME_ONLY"))
3147 plugin->external_only =
3148 GNUNET_CONFIGURATION_get_value_yesno (plugin->env->cfg,
3150 "EXTERNAL_HOSTNAME_ONLY");
3153 plugin->external_only = GNUNET_NO;
3155 if (GNUNET_YES == plugin->external_only)
3156 LOG (GNUNET_ERROR_TYPE_DEBUG,
3157 _("Notifying transport only about hostname `%s'\n"),
3158 plugin->external_hostname);
3161 LOG (GNUNET_ERROR_TYPE_DEBUG,
3162 "No external hostname configured\n");
3164 /* Optional parameters */
3166 GNUNET_CONFIGURATION_get_value_number (plugin->env->cfg,
3170 max_connections = 128;
3171 plugin->max_request = max_connections;
3173 LOG (GNUNET_ERROR_TYPE_DEBUG,
3174 _("Maximum number of connections is %u\n"),
3175 plugin->max_request);
3177 plugin->peer_id_length = strlen (GNUNET_i2s_full (plugin->env->my_identity));
3184 * Exit point from the plugin.
3190 LIBGNUNET_PLUGIN_TRANSPORT_DONE (void *cls)
3192 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
3193 struct HTTP_Server_Plugin *plugin = api->cls;
3195 if (NULL == api->cls)
3197 /* Free for stub mode */
3201 plugin->in_shutdown = GNUNET_YES;
3202 LOG (GNUNET_ERROR_TYPE_INFO,
3203 _("Shutting down plugin `%s'\n"),
3206 if (NULL != plugin->notify_ext_task)
3208 GNUNET_SCHEDULER_cancel (plugin->notify_ext_task);
3209 plugin->notify_ext_task = NULL;
3212 if (NULL != plugin->ext_addr)
3214 LOG (GNUNET_ERROR_TYPE_DEBUG,
3215 "Notifying transport to remove address `%s'\n",
3216 http_common_plugin_address_to_string (plugin->protocol,
3217 plugin->ext_addr->address,
3218 plugin->ext_addr->address_length));
3220 plugin->env->notify_address (plugin->env->cls,
3224 plugin->env->notify_address (plugin->env->cls,
3228 GNUNET_HELLO_address_free (plugin->ext_addr);
3229 plugin->ext_addr = NULL;
3232 /* Stop to report addresses to transport service */
3233 server_stop_report_addresses (plugin);
3234 if (NULL != plugin->server_v4_task)
3236 GNUNET_SCHEDULER_cancel (plugin->server_v4_task);
3237 plugin->server_v4_task = NULL;
3240 if (NULL != plugin->server_v6_task)
3242 GNUNET_SCHEDULER_cancel (plugin->server_v6_task);
3243 plugin->server_v6_task = NULL;
3246 GNUNET_free_non_null (plugin->crypto_init);
3247 GNUNET_free_non_null (plugin->cert);
3248 GNUNET_free_non_null (plugin->key);
3250 GNUNET_CONTAINER_multipeermap_iterate (plugin->sessions,
3251 &destroy_session_shutdown_cb,
3253 GNUNET_CONTAINER_multipeermap_destroy (plugin->sessions);
3254 plugin->sessions = NULL;
3255 if (NULL != plugin->server_v4)
3257 MHD_stop_daemon (plugin->server_v4);
3258 plugin->server_v4 = NULL;
3260 if (NULL != plugin->server_v6)
3262 MHD_stop_daemon (plugin->server_v6);
3263 plugin->server_v6 = NULL;
3266 GNUNET_free_non_null (plugin->external_hostname);
3267 GNUNET_free_non_null (plugin->ext_addr);
3268 GNUNET_free_non_null (plugin->server_addr_v4);
3269 GNUNET_free_non_null (plugin->server_addr_v6);
3270 regfree (&plugin->url_regex);
3272 LOG (GNUNET_ERROR_TYPE_DEBUG,
3273 _("Shutdown for plugin `%s' complete\n"),
3276 GNUNET_free (plugin);
3283 * Function called for a quick conversion of the binary address to
3284 * a numeric address. Note that the caller must not free the
3285 * address and that the next call to this function is allowed
3286 * to override the address again.
3289 * @param addr binary address
3290 * @param addrlen length of the address
3291 * @return string representing the same address
3294 http_server_plugin_address_to_string (void *cls,
3298 return http_common_plugin_address_to_string (PLUGIN_NAME,
3305 * Function obtain the network type for a session
3307 * @param cls closure (`struct HTTP_Server_Plugin *`)
3308 * @param session the session
3309 * @return the network type in HBO or #GNUNET_SYSERR
3311 static enum GNUNET_ATS_Network_Type
3312 http_server_plugin_get_network (void *cls,
3313 struct GNUNET_ATS_Session *session)
3315 return session->scope;
3320 * Function obtain the network type for an address.
3322 * @param cls closure (`struct Plugin *`)
3323 * @param address the address
3324 * @return the network type
3326 static enum GNUNET_ATS_Network_Type
3327 http_server_plugin_get_network_for_address (void *cls,
3328 const struct GNUNET_HELLO_Address *address)
3330 struct HTTP_Server_Plugin *plugin = cls;
3332 return http_common_get_network_for_address (plugin->env,
3338 * Function that will be called whenever the transport service wants to
3339 * notify the plugin that the inbound quota changed and that the plugin
3340 * should update it's delay for the next receive value
3342 * @param cls closure
3343 * @param peer which peer was the session for
3344 * @param session which session is being updated
3345 * @param delay new delay to use for receiving
3348 http_server_plugin_update_inbound_delay (void *cls,
3349 const struct GNUNET_PeerIdentity *peer,
3350 struct GNUNET_ATS_Session *session,
3351 struct GNUNET_TIME_Relative delay)
3353 session->next_receive = GNUNET_TIME_relative_to_absolute (delay);
3354 LOG (GNUNET_ERROR_TYPE_DEBUG,
3355 "New inbound delay %s\n",
3356 GNUNET_STRINGS_relative_time_to_string (delay,
3358 if (NULL != session->recv_wakeup_task)
3360 GNUNET_SCHEDULER_cancel (session->recv_wakeup_task);
3361 session->recv_wakeup_task
3362 = GNUNET_SCHEDULER_add_delayed (delay,
3370 * Return information about the given session to the
3373 * @param cls the `struct Plugin` with the monitor callback (`sic`)
3374 * @param peer peer we send information about
3375 * @param value our `struct GNUNET_ATS_Session` to send information about
3376 * @return #GNUNET_OK (continue to iterate)
3379 send_session_info_iter (void *cls,
3380 const struct GNUNET_PeerIdentity *peer,
3383 struct HTTP_Server_Plugin *plugin = cls;
3384 struct GNUNET_ATS_Session *session = value;
3386 notify_session_monitor (plugin,
3388 GNUNET_TRANSPORT_SS_INIT);
3394 * Begin monitoring sessions of a plugin. There can only
3395 * be one active monitor per plugin (i.e. if there are
3396 * multiple monitors, the transport service needs to
3397 * multiplex the generated events over all of them).
3399 * @param cls closure of the plugin
3400 * @param sic callback to invoke, NULL to disable monitor;
3401 * plugin will being by iterating over all active
3402 * sessions immediately and then enter monitor mode
3403 * @param sic_cls closure for @a sic
3406 http_server_plugin_setup_monitor (void *cls,
3407 GNUNET_TRANSPORT_SessionInfoCallback sic,
3410 struct HTTP_Server_Plugin *plugin = cls;
3413 plugin->sic_cls = sic_cls;
3416 GNUNET_CONTAINER_multipeermap_iterate (plugin->sessions,
3417 &send_session_info_iter,
3419 /* signal end of first iteration */
3420 sic (sic_cls, NULL, NULL);
3426 * Entry point for the plugin.
3432 LIBGNUNET_PLUGIN_TRANSPORT_INIT (void *cls)
3434 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
3435 struct GNUNET_TRANSPORT_PluginFunctions *api;
3436 struct HTTP_Server_Plugin *plugin;
3438 if (NULL == env->receive)
3440 /* run in 'stub' mode (i.e. as part of gnunet-peerinfo), don't fully
3441 initialze the plugin or the API */
3442 api = GNUNET_new (struct GNUNET_TRANSPORT_PluginFunctions);
3444 api->address_to_string = &http_server_plugin_address_to_string;
3445 api->string_to_address = &http_common_plugin_string_to_address;
3446 api->address_pretty_printer = &http_common_plugin_address_pretty_printer;
3449 plugin = GNUNET_new (struct HTTP_Server_Plugin);
3451 plugin->sessions = GNUNET_CONTAINER_multipeermap_create (128,
3454 api = GNUNET_new (struct GNUNET_TRANSPORT_PluginFunctions);
3456 api->send = &http_server_plugin_send;
3457 api->disconnect_peer = &http_server_plugin_disconnect_peer;
3458 api->disconnect_session = &http_server_plugin_disconnect_session;
3459 api->query_keepalive_factor = &http_server_query_keepalive_factor;
3460 api->check_address = &http_server_plugin_address_suggested;
3461 api->get_session = &http_server_plugin_get_session;
3463 api->address_to_string = &http_server_plugin_address_to_string;
3464 api->string_to_address = &http_common_plugin_string_to_address;
3465 api->address_pretty_printer = &http_common_plugin_address_pretty_printer;
3466 api->get_network = &http_server_plugin_get_network;
3467 api->get_network_for_address = &http_server_plugin_get_network_for_address;
3468 api->update_session_timeout = &http_server_plugin_update_session_timeout;
3469 api->update_inbound_delay = &http_server_plugin_update_inbound_delay;
3470 api->setup_monitor = &http_server_plugin_setup_monitor;
3472 plugin->name = "transport-https_server";
3473 plugin->protocol = "https";
3475 plugin->name = "transport-http_server";
3476 plugin->protocol = "http";
3480 GNUNET_CONFIGURATION_get_value_yesno (env->cfg,
3485 plugin->options |= HTTP_OPTIONS_TCP_STEALTH;
3487 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3488 _("TCP_STEALTH not supported on this platform.\n"));
3489 LIBGNUNET_PLUGIN_TRANSPORT_DONE (api);
3494 /* Compile URL regex */
3495 if (regcomp (&plugin->url_regex,
3499 LOG (GNUNET_ERROR_TYPE_ERROR,
3500 _("Unable to compile URL regex\n"));
3501 LIBGNUNET_PLUGIN_TRANSPORT_DONE (api);
3505 /* Configure plugin */
3506 if (GNUNET_SYSERR == server_configure_plugin (plugin))
3508 LIBGNUNET_PLUGIN_TRANSPORT_DONE (api);
3512 /* Check IPv6 support */
3513 if (GNUNET_YES == plugin->use_ipv6)
3514 plugin->use_ipv6 = server_check_ipv6_support (plugin);
3516 /* Report addresses to transport service */
3517 if (GNUNET_NO == plugin->external_only)
3518 server_start_report_addresses (plugin);
3520 if (GNUNET_SYSERR == server_start (plugin))
3522 LIBGNUNET_PLUGIN_TRANSPORT_DONE (api);
3528 /* end of plugin_transport_http_server.c */