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/>.
20 * @file transport/plugin_transport_http_server.c
21 * @brief HTTP/S server transport plugin
22 * @author Matthias Wachs
23 * @author David Barksdale
24 * @author Christian Grothoff
27 #include "gnunet_util_lib.h"
28 #include "gnunet_statistics_service.h"
29 #include "gnunet_transport_plugin.h"
30 #include "gnunet_nat_service.h"
31 #include "plugin_transport_http_common.h"
32 #include <microhttpd.h>
38 #define PLUGIN_NAME "https_server"
39 #define LIBGNUNET_PLUGIN_TRANSPORT_INIT libgnunet_plugin_transport_https_server_init
40 #define LIBGNUNET_PLUGIN_TRANSPORT_DONE libgnunet_plugin_transport_https_server_done
42 #define PLUGIN_NAME "http_server"
43 #define LIBGNUNET_PLUGIN_TRANSPORT_INIT libgnunet_plugin_transport_http_server_init
44 #define LIBGNUNET_PLUGIN_TRANSPORT_DONE libgnunet_plugin_transport_http_server_done
47 #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>"
52 #define LOG(kind,...) GNUNET_log_from (kind, "transport-" PLUGIN_NAME,__VA_ARGS__)
56 * Information we keep with MHD for an HTTP request.
61 * The session this server request belongs to
62 * Can be NULL, when session was disconnected and freed
64 struct GNUNET_ATS_Session *session;
69 struct MHD_Connection *mhd_conn;
74 struct MHD_Daemon *mhd_daemon;
77 * Options requested by peer
80 #define OPTION_LONG_POLL 1 /* GET request wants long-poll semantics */
88 * For PUT requests: Is this the first or last callback with size 0
89 * For GET requests: Have we sent a message
102 * Wrapper to manage addresses
104 struct HttpAddressWrapper
109 struct HttpAddressWrapper *next;
112 * Linked list previous
114 struct HttpAddressWrapper *prev;
117 * An address we are using.
119 struct HttpAddress *address;
122 * Length of the address.
129 * Message to send using http
134 * next pointer for double linked list
136 struct HTTP_Message *next;
139 * previous pointer for double linked list
141 struct HTTP_Message *prev;
144 * buffer containing data to send
149 * amount of data already sent
159 * HTTP/S specific overhead
164 * Continuation function to call once the transmission buffer
165 * has again space available. NULL if there is no
166 * continuation to call.
168 GNUNET_TRANSPORT_TransmitContinuation transmit_cont;
171 * Closure for transmit_cont.
173 void *transmit_cont_cls;
178 * Session handle for connections.
180 struct GNUNET_ATS_Session
184 * To whom are we talking to (set to our identity
185 * if we are still waiting for the welcome message)
187 struct GNUNET_PeerIdentity target;
190 * Pointer to the global plugin struct.
192 struct HTTP_Server_Plugin *plugin;
195 * next pointer for double linked list
197 struct HTTP_Message *msg_head;
200 * previous pointer for double linked list
202 struct HTTP_Message *msg_tail;
205 * Message stream tokenizer for incoming data
207 struct GNUNET_MessageStreamTokenizer *msg_tk;
212 struct ServerRequest *server_recv;
217 struct ServerRequest *server_send;
222 struct GNUNET_HELLO_Address *address;
225 * Absolute time when to receive data again
226 * Used for receive throttling
228 struct GNUNET_TIME_Absolute next_receive;
231 * Absolute time when this connection will time out.
233 struct GNUNET_TIME_Absolute timeout;
236 * Session timeout task
238 struct GNUNET_SCHEDULER_Task * timeout_task;
241 * Task to resume MHD handling when receiving is allowed again
243 struct GNUNET_SCHEDULER_Task * recv_wakeup_task;
246 * Number of bytes waiting for transmission to this peer.
248 unsigned long long bytes_in_queue;
251 * Number of messages waiting for transmission to this peer.
253 unsigned int msgs_in_queue;
256 * Unique HTTP/S connection tag for this connection
263 enum GNUNET_NetworkType scope;
266 * #GNUNET_YES if this session is known to the service.
268 int known_to_service;
274 * Encapsulation of all of the state of the plugin.
276 struct HTTP_Server_Plugin
281 struct GNUNET_TRANSPORT_PluginEnvironment *env;
284 * Hash map of open sessions.
286 struct GNUNET_CONTAINER_MultiPeerMap *sessions;
289 * Function to call about session status changes.
291 GNUNET_TRANSPORT_SessionInfoCallback sic;
294 * Closure for @e sic.
311 char *external_hostname;
314 * External hostname the plugin can be connected to, can be different to
315 * the host's FQDN, used e.g. for reverse proxying
317 struct GNUNET_HELLO_Address *ext_addr;
320 * NAT handle & address management
322 struct GNUNET_NAT_Handle *nat;
325 * IPv4 addresses DLL head
327 struct HttpAddressWrapper *addr_head;
330 * IPv4 addresses DLL tail
332 struct HttpAddressWrapper *addr_tail;
335 * IPv4 server socket to bind to
337 struct sockaddr_in *server_addr_v4;
340 * IPv6 server socket to bind to
342 struct sockaddr_in6 *server_addr_v6;
347 struct MHD_Daemon *server_v4;
352 struct MHD_Daemon *server_v6;
360 * Use RC4-128 instead of AES:
361 * NONE:+VERS-TLS1.0:+ARCFOUR-128:+SHA1:+RSA:+COMP-NULL
380 struct GNUNET_SCHEDULER_Task *server_v4_task;
385 struct GNUNET_SCHEDULER_Task *server_v6_task;
388 * Task calling transport service about external address
390 struct GNUNET_SCHEDULER_Task *notify_ext_task;
393 * Notify transport only about external address
395 unsigned int external_only;
398 * The IPv4 server is scheduled to run asap
400 int server_v4_immediately;
403 * The IPv6 server is scheduled to run asap
405 int server_v6_immediately;
408 * Verify external address
410 int verify_external_hostname;
413 * Maximum number of sockets the plugin can use
414 * Each http request /request connections are two connections
416 unsigned int max_request;
419 * Current number of sockets the plugin can use
420 * Each http connection are two requests
422 unsigned int cur_request;
425 * Did we immediately end the session in disconnect_cb
435 * My options to be included in the address
455 * Regex for parsing URLs. FIXME: this seems overkill.
463 * If a session monitor is attached, notify it about the new
466 * @param plugin our plugin
467 * @param session session that changed state
468 * @param state new state of the session
471 notify_session_monitor (struct HTTP_Server_Plugin *plugin,
472 struct GNUNET_ATS_Session *session,
473 enum GNUNET_TRANSPORT_SessionState state)
475 struct GNUNET_TRANSPORT_SessionInfo info;
477 if (NULL == plugin->sic)
479 memset (&info, 0, sizeof (info));
481 info.is_inbound = GNUNET_YES;
482 info.num_msg_pending = session->msgs_in_queue;
483 info.num_bytes_pending = session->bytes_in_queue;
484 info.receive_delay = session->next_receive;
485 info.session_timeout = session->timeout;
486 info.address = session->address;
487 plugin->sic (plugin->sic_cls,
494 * Wake up an MHD connection which was suspended
496 * @param cls the session
499 server_wake_up (void *cls)
501 struct GNUNET_ATS_Session *s = cls;
503 s->recv_wakeup_task = NULL;
504 LOG (GNUNET_ERROR_TYPE_DEBUG,
505 "Session %p: Waking up PUT handle\n",
507 GNUNET_assert (s->server_recv->suspended);
508 MHD_resume_connection (s->server_recv->mhd_conn);
509 s->server_recv->suspended = false;
514 * Reschedule the execution of both IPv4 and IPv6 server.
516 * @param plugin the plugin
517 * @param server which server to schedule v4 or v6?
518 * @param now #GNUNET_YES to schedule execution immediately, #GNUNET_NO to wait
522 server_reschedule (struct HTTP_Server_Plugin *plugin,
523 struct MHD_Daemon *server,
528 * Deletes the session. Must not be used afterwards.
530 * @param s the session to delete
533 server_delete_session (struct GNUNET_ATS_Session *s)
535 struct HTTP_Server_Plugin *plugin = s->plugin;
536 struct HTTP_Message *msg;
538 if (NULL != s->timeout_task)
540 GNUNET_SCHEDULER_cancel (s->timeout_task);
541 s->timeout_task = NULL;
542 s->timeout = GNUNET_TIME_UNIT_ZERO_ABS;
544 if (NULL != s->recv_wakeup_task)
546 GNUNET_SCHEDULER_cancel (s->recv_wakeup_task);
547 s->recv_wakeup_task = NULL;
548 if (NULL != s->server_recv)
550 GNUNET_assert (s->server_recv->suspended);
551 s->server_recv->suspended = false;
552 MHD_resume_connection (s->server_recv->mhd_conn);
555 GNUNET_assert (GNUNET_OK ==
556 GNUNET_CONTAINER_multipeermap_remove (plugin->sessions,
559 while (NULL != (msg = s->msg_head))
561 GNUNET_CONTAINER_DLL_remove (s->msg_head,
564 if (NULL != msg->transmit_cont)
565 msg->transmit_cont (msg->transmit_cont_cls,
569 msg->pos + msg->overhead);
570 GNUNET_assert (s->msgs_in_queue > 0);
572 GNUNET_assert (s->bytes_in_queue >= msg->size);
573 s->bytes_in_queue -= msg->size;
577 GNUNET_assert (0 == s->msgs_in_queue);
578 GNUNET_assert (0 == s->bytes_in_queue);
580 if (NULL != s->server_send)
582 LOG (GNUNET_ERROR_TYPE_DEBUG,
583 "Server: %p / %p Terminating inbound PUT session to peer `%s'\n",
585 GNUNET_i2s (&s->target));
586 s->server_send->session = NULL;
587 MHD_set_connection_option (s->server_send->mhd_conn,
588 MHD_CONNECTION_OPTION_TIMEOUT,
589 1 /* 0 = no timeout, so this is MIN */);
590 if (s->server_send->suspended)
592 s->server_send->suspended = false;
593 MHD_resume_connection (s->server_send->mhd_conn);
595 server_reschedule (plugin,
596 s->server_send->mhd_daemon,
600 if (NULL != s->server_recv)
602 LOG (GNUNET_ERROR_TYPE_DEBUG,
603 "Server: %p / %p Terminating inbound GET session to peer `%s'\n",
604 s, s->server_recv, GNUNET_i2s (&s->target));
605 s->server_recv->session = NULL;
606 MHD_set_connection_option (s->server_recv->mhd_conn,
607 MHD_CONNECTION_OPTION_TIMEOUT,
608 1 /* 0 = no timeout, so this is MIN */);
609 server_reschedule (plugin,
610 s->server_recv->mhd_daemon,
613 notify_session_monitor (plugin,
615 GNUNET_TRANSPORT_SS_DONE);
616 if (GNUNET_YES == s->known_to_service)
618 plugin->env->session_end (plugin->env->cls,
621 s->known_to_service = GNUNET_NO;
623 if (NULL != s->msg_tk)
625 GNUNET_MST_destroy (s->msg_tk);
628 GNUNET_HELLO_address_free (s->address);
629 LOG (GNUNET_ERROR_TYPE_DEBUG,
630 "Session %p destroyed\n",
637 * Disconnect session @a s by telling MHD to close the
638 * connections (reducing timeout, etc.).
640 * @param cls closure with the `struct HTTP_Server_Plugin`
641 * @param s the session
642 * @return #GNUNET_OK on success
645 http_server_plugin_disconnect_session (void *cls,
646 struct GNUNET_ATS_Session *s)
648 server_delete_session (s);
654 * Session was idle, so disconnect it
656 * @param cls the session
659 server_session_timeout (void *cls)
661 struct GNUNET_ATS_Session *s = cls;
662 struct GNUNET_TIME_Relative left;
664 s->timeout_task = NULL;
665 left = GNUNET_TIME_absolute_get_remaining (s->timeout);
666 if (0 != left.rel_value_us)
668 /* not actually our turn yet, but let's at least update
669 the monitor, it may think we're about to die ... */
670 notify_session_monitor (s->plugin,
672 GNUNET_TRANSPORT_SS_UP);
673 s->timeout_task = GNUNET_SCHEDULER_add_delayed (left,
674 &server_session_timeout,
678 GNUNET_log (TIMEOUT_LOG,
679 "Session %p was idle for %s, disconnecting\n",
681 GNUNET_STRINGS_relative_time_to_string (HTTP_SERVER_SESSION_TIMEOUT,
683 server_delete_session (s);
688 * Increment session timeout due to activity session @a s
690 * @param s the session
693 server_reschedule_session_timeout (struct GNUNET_ATS_Session *s)
695 GNUNET_assert (NULL != s->timeout_task);
696 s->timeout = GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
701 * Function that can be used by the transport service to transmit
702 * a message using the plugin. Note that in the case of a
703 * peer disconnecting, the continuation MUST be called
704 * prior to the disconnect notification itself. This function
705 * will be called with this peer's HELLO message to initiate
706 * a fresh connection to another peer.
709 * @param session which session must be used
710 * @param msgbuf the message to transmit
711 * @param msgbuf_size number of bytes in @a msgbuf
712 * @param priority how important is the message (most plugins will
713 * ignore message priority and just FIFO)
714 * @param to how long to wait at most for the transmission (does not
715 * require plugins to discard the message after the timeout,
716 * just advisory for the desired delay; most plugins will ignore
718 * @param cont continuation to call once the message has
719 * been transmitted (or if the transport is ready
720 * for the next transmission call; or if the
721 * peer disconnected...); can be NULL
722 * @param cont_cls closure for @a cont
723 * @return number of bytes used (on the physical network, with overheads);
724 * -1 on hard errors (i.e. address invalid); 0 is a legal value
725 * and does NOT mean that the message was not transmitted (DV)
728 http_server_plugin_send (void *cls,
729 struct GNUNET_ATS_Session *session,
732 unsigned int priority,
733 struct GNUNET_TIME_Relative to,
734 GNUNET_TRANSPORT_TransmitContinuation cont,
737 struct HTTP_Server_Plugin *plugin = cls;
738 struct HTTP_Message *msg;
739 ssize_t bytes_sent = 0;
742 LOG (GNUNET_ERROR_TYPE_DEBUG,
743 "Session %p/request %p: Sending message with %u to peer `%s'\n",
745 session->server_send,
747 GNUNET_i2s (&session->target));
749 /* create new message and schedule */
750 bytes_sent = sizeof (struct HTTP_Message) + msgbuf_size;
751 msg = GNUNET_malloc (bytes_sent);
753 msg->size = msgbuf_size;
755 msg->buf = (char *) &msg[1];
756 msg->transmit_cont = cont;
757 msg->transmit_cont_cls = cont_cls;
758 GNUNET_memcpy (msg->buf,
761 GNUNET_CONTAINER_DLL_insert_tail (session->msg_head,
764 session->msgs_in_queue++;
765 session->bytes_in_queue += msg->size;
766 notify_session_monitor (plugin,
768 GNUNET_TRANSPORT_SS_UP);
769 GNUNET_asprintf (&stat_txt,
770 "# bytes currently in %s_server buffers",
772 GNUNET_STATISTICS_update (plugin->env->stats,
773 stat_txt, msgbuf_size, GNUNET_NO);
774 GNUNET_free (stat_txt);
776 if (NULL != session->server_send)
778 if (session->server_send->suspended)
780 MHD_resume_connection (session->server_send->mhd_conn);
781 session->server_send->suspended = false;
783 server_reschedule (session->plugin,
784 session->server_send->mhd_daemon,
792 * Terminate session during shutdown.
794 * @param cls the `struct HTTP_Server_Plugin *`
795 * @param peer for which this is a session
796 * @param value the `struct GNUNET_ATS_Session` to clean up
797 * @return #GNUNET_OK (continue to iterate)
800 destroy_session_shutdown_cb (void *cls,
801 const struct GNUNET_PeerIdentity *peer,
804 struct GNUNET_ATS_Session *s = value;
805 struct ServerRequest *sc_send;
806 struct ServerRequest *sc_recv;
808 sc_send = s->server_send;
809 sc_recv = s->server_recv;
810 server_delete_session (s);
812 sc_send->session = NULL;
814 sc_recv->session = NULL;
823 * @param cls the `struct HTTP_Server_Plugin *`
824 * @param peer for which this is a session
825 * @param value the `struct GNUNET_ATS_Session` to clean up
826 * @return #GNUNET_OK (continue to iterate)
829 destroy_session_cb (void *cls,
830 const struct GNUNET_PeerIdentity *peer,
833 struct GNUNET_ATS_Session *s = value;
835 server_delete_session (s);
841 * Function that can be used to force the plugin to disconnect
842 * from the given peer and cancel all previous transmissions
843 * (and their continuationc).
846 * @param target peer from which to disconnect
849 http_server_plugin_disconnect_peer (void *cls,
850 const struct GNUNET_PeerIdentity *target)
852 struct HTTP_Server_Plugin *plugin = cls;
854 LOG (GNUNET_ERROR_TYPE_DEBUG,
855 "Transport tells me to disconnect `%s'\n",
856 GNUNET_i2s (target));
857 GNUNET_CONTAINER_multipeermap_get_multiple (plugin->sessions,
865 * Another peer has suggested an address for this
866 * peer and transport plugin. Check that this could be a valid
867 * address. If so, consider adding it to the list
871 * @param addr pointer to the address
872 * @param addrlen length of @a addr
873 * @return #GNUNET_OK if this is a plausible address for this peer
877 http_server_plugin_address_suggested (void *cls,
881 struct HTTP_Server_Plugin *plugin = cls;
882 struct HttpAddressWrapper *next;
883 struct HttpAddressWrapper *pos;
884 const struct HttpAddress *haddr = addr;
886 if ((NULL != plugin->ext_addr) &&
887 GNUNET_YES == (http_common_cmp_addresses (addr, addrlen,
888 plugin->ext_addr->address,
889 plugin->ext_addr->address_length)))
891 /* Checking HTTP_OPTIONS_VERIFY_CERTIFICATE option for external hostname */
892 if ((ntohl (haddr->options) & HTTP_OPTIONS_VERIFY_CERTIFICATE) !=
893 (plugin->options & HTTP_OPTIONS_VERIFY_CERTIFICATE))
894 return GNUNET_NO; /* VERIFY option not set as required! */
897 next = plugin->addr_head;
898 while (NULL != (pos = next))
901 if (GNUNET_YES == (http_common_cmp_addresses(addr,
912 * Creates a new outbound session the transport
913 * service will use to send data to the peer.
915 * Since HTTP/S server cannot create sessions, always returns NULL.
917 * @param cls the plugin
918 * @param address the address
919 * @return always NULL
921 static struct GNUNET_ATS_Session *
922 http_server_plugin_get_session (void *cls,
923 const struct GNUNET_HELLO_Address *address)
930 * Call MHD IPv4 to process pending requests and then go back
931 * and schedule the next run.
933 * @param cls plugin as closure
936 server_v4_run (void *cls)
938 struct HTTP_Server_Plugin *plugin = cls;
940 plugin->server_v4_task = NULL;
941 plugin->server_v4_immediately = GNUNET_NO;
942 GNUNET_assert (MHD_YES == MHD_run (plugin->server_v4));
943 server_reschedule (plugin, plugin->server_v4, GNUNET_NO);
948 * Call MHD IPv6 to process pending requests and then go back
949 * and schedule the next run.
951 * @param cls plugin as closure
954 server_v6_run (void *cls)
956 struct HTTP_Server_Plugin *plugin = cls;
958 plugin->server_v6_task = NULL;
959 plugin->server_v6_immediately = GNUNET_NO;
960 GNUNET_assert (MHD_YES == MHD_run (plugin->server_v6));
961 server_reschedule (plugin, plugin->server_v6, GNUNET_NO);
966 * Function that queries MHD's select sets and
967 * starts the task waiting for them.
969 * @param plugin plugin
970 * @param daemon_handle the MHD daemon handle
971 * @param now schedule now
972 * @return gnunet task identifier
974 static struct GNUNET_SCHEDULER_Task *
975 server_schedule (struct HTTP_Server_Plugin *plugin,
976 struct MHD_Daemon *daemon_handle,
979 struct GNUNET_SCHEDULER_Task * ret;
983 struct GNUNET_NETWORK_FDSet *wrs;
984 struct GNUNET_NETWORK_FDSet *wws;
986 MHD_UNSIGNED_LONG_LONG timeout;
987 static unsigned long long last_timeout = 0;
989 struct GNUNET_TIME_Relative tv;
991 if (GNUNET_YES == plugin->in_shutdown)
998 wrs = GNUNET_NETWORK_fdset_create ();
999 wws = GNUNET_NETWORK_fdset_create ();
1001 GNUNET_assert (MHD_YES ==
1002 MHD_get_fdset (daemon_handle,
1007 haveto = MHD_get_timeout (daemon_handle, &timeout);
1008 if (haveto == MHD_YES)
1010 if (timeout != last_timeout)
1012 LOG (GNUNET_ERROR_TYPE_DEBUG,
1013 "SELECT Timeout changed from %llu to %llu (ms)\n",
1014 last_timeout, timeout);
1015 last_timeout = timeout;
1017 if (timeout <= GNUNET_TIME_UNIT_SECONDS.rel_value_us / 1000LL)
1018 tv.rel_value_us = (uint64_t) timeout * 1000LL;
1020 tv = GNUNET_TIME_UNIT_SECONDS;
1023 tv = GNUNET_TIME_UNIT_SECONDS;
1024 /* Force immediate run, since we have outbound data to send */
1025 if (now == GNUNET_YES)
1026 tv = GNUNET_TIME_UNIT_MILLISECONDS;
1027 GNUNET_NETWORK_fdset_copy_native (wrs, &rs, max + 1);
1028 GNUNET_NETWORK_fdset_copy_native (wws, &ws, max + 1);
1030 if (daemon_handle == plugin->server_v4)
1032 if (plugin->server_v4_task != NULL)
1034 GNUNET_SCHEDULER_cancel (plugin->server_v4_task);
1035 plugin->server_v4_task = NULL;
1038 LOG (GNUNET_ERROR_TYPE_DEBUG,
1039 "Scheduling IPv4 server task in %llu ms\n",
1043 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1045 &server_v4_run, plugin);
1047 if (daemon_handle == plugin->server_v6)
1049 if (plugin->server_v6_task != NULL)
1051 GNUNET_SCHEDULER_cancel (plugin->server_v6_task);
1052 plugin->server_v6_task = NULL;
1055 LOG (GNUNET_ERROR_TYPE_DEBUG,
1056 "Scheduling IPv6 server task in %llu ms\n", tv);
1059 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1061 &server_v6_run, plugin);
1063 GNUNET_NETWORK_fdset_destroy (wrs);
1064 GNUNET_NETWORK_fdset_destroy (wws);
1070 * Reschedule the execution of both IPv4 and IPv6 server
1072 * @param plugin the plugin
1073 * @param server which server to schedule v4 or v6?
1074 * @param now #GNUNET_YES to schedule execution immediately, #GNUNET_NO to wait
1078 server_reschedule (struct HTTP_Server_Plugin *plugin,
1079 struct MHD_Daemon *server,
1082 if ((server == plugin->server_v4) && (plugin->server_v4 != NULL))
1084 if (GNUNET_YES == plugin->server_v4_immediately)
1085 return; /* No rescheduling, server will run asap */
1087 if (GNUNET_YES == now)
1088 plugin->server_v4_immediately = GNUNET_YES;
1090 if (plugin->server_v4_task != NULL)
1092 GNUNET_SCHEDULER_cancel (plugin->server_v4_task);
1093 plugin->server_v4_task = NULL;
1095 plugin->server_v4_task = server_schedule (plugin, plugin->server_v4, now);
1098 if ((server == plugin->server_v6) && (plugin->server_v6 != NULL))
1100 if (GNUNET_YES == plugin->server_v6_immediately)
1101 return; /* No rescheduling, server will run asap */
1103 if (GNUNET_YES == now)
1104 plugin->server_v6_immediately = GNUNET_YES;
1106 if (plugin->server_v6_task != NULL)
1108 GNUNET_SCHEDULER_cancel (plugin->server_v6_task);
1109 plugin->server_v6_task = NULL;
1111 plugin->server_v6_task = server_schedule (plugin, plugin->server_v6, now);
1117 * Function that is called to get the keepalive factor.
1118 * GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT is divided by this number to
1119 * calculate the interval between keepalive packets.
1121 * @param cls closure with the `struct HTTP_Server_Plugin`
1122 * @return keepalive factor
1125 http_server_query_keepalive_factor (void *cls)
1132 * Function that will be called whenever the transport service wants to
1133 * notify the plugin that a session is still active and in use and
1134 * therefore the session timeout for this session has to be updated
1136 * @param cls closure
1137 * @param peer which peer was the session for
1138 * @param session which session is being updated
1141 http_server_plugin_update_session_timeout (void *cls,
1142 const struct GNUNET_PeerIdentity *peer,
1143 struct GNUNET_ATS_Session *session)
1145 server_reschedule_session_timeout (session);
1150 * Tell MHD that the connection should timeout after @a to seconds.
1152 * @param plugin our plugin
1153 * @param s session for which the timeout changes
1154 * @param to timeout in seconds
1157 server_mhd_connection_timeout (struct HTTP_Server_Plugin *plugin,
1158 struct GNUNET_ATS_Session *s,
1161 /* Setting timeouts for other connections */
1162 if (NULL != s->server_recv)
1164 LOG (GNUNET_ERROR_TYPE_DEBUG,
1165 "Setting timeout for %p to %u sec.\n",
1166 s->server_recv, to);
1167 MHD_set_connection_option (s->server_recv->mhd_conn,
1168 MHD_CONNECTION_OPTION_TIMEOUT,
1170 server_reschedule (plugin, s->server_recv->mhd_daemon, GNUNET_NO);
1172 if (NULL != s->server_send)
1174 LOG (GNUNET_ERROR_TYPE_DEBUG,
1175 "Setting timeout for %p to %u sec.\n",
1176 s->server_send, to);
1177 MHD_set_connection_option (s->server_send->mhd_conn,
1178 MHD_CONNECTION_OPTION_TIMEOUT,
1180 server_reschedule (plugin, s->server_send->mhd_daemon, GNUNET_NO);
1186 * Parse incoming URL for tag and target
1188 * @param plugin plugin
1189 * @param url incoming url
1190 * @param target where to store the target
1191 * @param tag where to store the tag
1192 * @param options where to store the options
1193 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
1196 server_parse_url (struct HTTP_Server_Plugin *plugin,
1198 struct GNUNET_PeerIdentity *target,
1202 regmatch_t matches[4];
1203 const char *tag_start;
1204 const char *target_start;
1208 unsigned long int rc;
1212 ("^.*/([0-9A-Z]+);([0-9]+)(,[0-9]+)?$")
1217 return GNUNET_SYSERR;
1220 if (regexec(&plugin->url_regex, url, 4, matches, 0))
1222 LOG (GNUNET_ERROR_TYPE_DEBUG,
1223 "URL `%s' did not match regex\n", url);
1224 return GNUNET_SYSERR;
1227 target_start = &url[matches[1].rm_so];
1228 tag_start = &url[matches[2].rm_so];
1231 rc = strtoul (tag_start, &tag_end, 10);
1232 if (&url[matches[2].rm_eo] != tag_end)
1234 LOG (GNUNET_ERROR_TYPE_DEBUG,
1235 "URL tag did not line up with submatch\n");
1236 return GNUNET_SYSERR;
1240 LOG (GNUNET_ERROR_TYPE_DEBUG,
1241 "URL tag is zero\n");
1242 return GNUNET_SYSERR;
1244 if ((rc == ULONG_MAX) && (ERANGE == errno))
1246 LOG (GNUNET_ERROR_TYPE_DEBUG,
1247 "URL tag > ULONG_MAX\n");
1248 return GNUNET_SYSERR;
1250 if (rc > UINT32_MAX)
1252 LOG (GNUNET_ERROR_TYPE_DEBUG,
1253 "URL tag > UINT32_MAX\n");
1254 return GNUNET_SYSERR;
1256 (*tag) = (uint32_t)rc;
1257 LOG (GNUNET_ERROR_TYPE_DEBUG,
1258 "Found tag `%u' in url\n",
1261 /* convert peer id */
1262 hash_length = matches[1].rm_eo - matches[1].rm_so;
1263 if (hash_length != plugin->peer_id_length)
1265 LOG (GNUNET_ERROR_TYPE_DEBUG,
1266 "URL target is %u bytes, expecting %u\n",
1267 hash_length, plugin->peer_id_length);
1268 return GNUNET_SYSERR;
1271 GNUNET_CRYPTO_eddsa_public_key_from_string (target_start,
1273 &target->public_key))
1275 LOG (GNUNET_ERROR_TYPE_DEBUG,
1276 "URL target conversion failed\n");
1277 return GNUNET_SYSERR;
1279 LOG (GNUNET_ERROR_TYPE_DEBUG,
1280 "Found target `%s' in URL\n",
1281 GNUNET_i2s_full (target));
1283 /* convert options */
1284 if (-1 == matches[3].rm_so)
1290 rc = strtoul (&url[matches[3].rm_so + 1], &options_end, 10);
1291 if (&url[matches[3].rm_eo] != options_end)
1293 LOG (GNUNET_ERROR_TYPE_DEBUG,
1294 "URL options did not line up with submatch\n");
1295 return GNUNET_SYSERR;
1297 if ((rc == ULONG_MAX) && (ERANGE == errno))
1299 LOG (GNUNET_ERROR_TYPE_DEBUG,
1300 "URL options > ULONG_MAX\n");
1301 return GNUNET_SYSERR;
1303 if (rc > UINT32_MAX)
1305 LOG (GNUNET_ERROR_TYPE_DEBUG,
1306 "URL options > UINT32_MAX\n");
1307 return GNUNET_SYSERR;
1309 (*options) = (uint32_t) rc;
1310 LOG (GNUNET_ERROR_TYPE_DEBUG,
1311 "Found options `%u' in url\n",
1319 * Closure for #session_tag_it().
1321 struct GNUNET_ATS_SessionTagContext
1324 * Set to session matching the tag.
1326 struct GNUNET_ATS_Session *res;
1329 * Tag we are looking for.
1336 * Find a session with a matching tag.
1338 * @param cls the `struct GNUNET_ATS_SessionTagContext *`
1339 * @param key peer identity (unused)
1340 * @param value the `struct GNUNET_ATS_Session *`
1341 * @return #GNUNET_NO if we found the session, #GNUNET_OK if not
1344 session_tag_it (void *cls,
1345 const struct GNUNET_PeerIdentity *key,
1348 struct GNUNET_ATS_SessionTagContext *stc = cls;
1349 struct GNUNET_ATS_Session *s = value;
1351 if (s->tag == stc->tag)
1361 * Lookup a mhd connection and create one if none is found
1363 * @param plugin the plugin handle
1364 * @param mhd_connection the incoming mhd_connection
1365 * @param url incoming requested URL
1366 * @param method PUT or GET
1367 * @return the server connecetion
1369 static struct ServerRequest *
1370 server_lookup_connection (struct HTTP_Server_Plugin *plugin,
1371 struct MHD_Connection *mhd_connection,
1375 struct GNUNET_ATS_Session *s = NULL;
1376 struct ServerRequest *sc = NULL;
1377 const union MHD_ConnectionInfo *conn_info;
1378 struct HttpAddress *addr;
1379 struct GNUNET_PeerIdentity target;
1381 struct GNUNET_ATS_SessionTagContext stc;
1383 int direction = GNUNET_SYSERR;
1385 enum GNUNET_NetworkType scope;
1387 conn_info = MHD_get_connection_info (mhd_connection,
1388 MHD_CONNECTION_INFO_CLIENT_ADDRESS);
1389 if ((conn_info->client_addr->sa_family != AF_INET) &&
1390 (conn_info->client_addr->sa_family != AF_INET6))
1392 LOG (GNUNET_ERROR_TYPE_DEBUG,
1393 "New %s request from %s\n",
1397 options = 0; /* make gcc happy */
1398 if (GNUNET_SYSERR ==
1399 server_parse_url (plugin, url, &target, &stc.tag, &options))
1401 LOG (GNUNET_ERROR_TYPE_DEBUG,
1402 "Invalid url %s\n", url);
1405 if (0 == strcmp (MHD_HTTP_METHOD_PUT, method))
1406 direction = _RECEIVE;
1407 else if (0 == strcmp (MHD_HTTP_METHOD_GET, method))
1411 LOG (GNUNET_ERROR_TYPE_DEBUG,
1412 "Invalid method %s for request from %s\n",
1417 plugin->cur_request++;
1418 LOG (GNUNET_ERROR_TYPE_DEBUG,
1419 "New %s request from %s with tag %u (%u of %u)\n",
1421 GNUNET_i2s (&target),
1423 plugin->cur_request, plugin->max_request);
1424 /* find existing session */
1426 GNUNET_CONTAINER_multipeermap_get_multiple (plugin->sessions,
1430 if (NULL == (s = stc.res))
1432 /* create new session */
1434 switch (conn_info->client_addr->sa_family)
1437 addr = http_common_address_from_socket (plugin->protocol,
1438 conn_info->client_addr,
1439 sizeof (struct sockaddr_in));
1440 addr_len = http_common_address_get_size (addr);
1441 scope = plugin->env->get_address_type (plugin->env->cls,
1442 conn_info->client_addr,
1443 sizeof (struct sockaddr_in));
1446 addr = http_common_address_from_socket (plugin->protocol,
1447 conn_info->client_addr,
1448 sizeof (struct sockaddr_in6));
1449 addr_len = http_common_address_get_size (addr);
1450 scope = plugin->env->get_address_type (plugin->env->cls,
1451 conn_info->client_addr,
1452 sizeof (struct sockaddr_in6));
1455 /* external host name */
1458 s = GNUNET_new (struct GNUNET_ATS_Session);
1462 s->address = GNUNET_HELLO_address_allocate (&s->target,
1466 GNUNET_HELLO_ADDRESS_INFO_INBOUND);
1467 s->next_receive = GNUNET_TIME_UNIT_ZERO_ABS;
1469 s->timeout = GNUNET_TIME_relative_to_absolute (HTTP_SERVER_SESSION_TIMEOUT);
1470 s->timeout_task = GNUNET_SCHEDULER_add_delayed (HTTP_SERVER_SESSION_TIMEOUT,
1471 &server_session_timeout,
1473 (void) GNUNET_CONTAINER_multipeermap_put (plugin->sessions,
1476 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
1477 notify_session_monitor (plugin,
1479 GNUNET_TRANSPORT_SS_INIT);
1480 notify_session_monitor (plugin,
1482 GNUNET_TRANSPORT_SS_HANDSHAKE);
1483 LOG (GNUNET_ERROR_TYPE_DEBUG,
1484 "Creating new session %p for peer `%s' connecting from `%s'\n",
1485 s, GNUNET_i2s (&target),
1486 http_common_plugin_address_to_string (plugin->protocol,
1489 GNUNET_free_non_null (addr);
1492 if ( (_RECEIVE == direction) &&
1493 (NULL != s->server_recv) )
1495 LOG (GNUNET_ERROR_TYPE_DEBUG,
1496 "Duplicate PUT request from `%s' tag %u, dismissing new request\n",
1497 GNUNET_i2s (&target),
1501 if ((_SEND == direction) && (NULL != s->server_send))
1503 LOG (GNUNET_ERROR_TYPE_DEBUG,
1504 "Duplicate GET request from `%s' tag %u, dismissing new request\n",
1505 GNUNET_i2s (&target),
1509 sc = GNUNET_new (struct ServerRequest);
1510 if (conn_info->client_addr->sa_family == AF_INET)
1511 sc->mhd_daemon = plugin->server_v4;
1512 if (conn_info->client_addr->sa_family == AF_INET6)
1513 sc->mhd_daemon = plugin->server_v6;
1514 sc->mhd_conn = mhd_connection;
1515 sc->direction = direction;
1516 sc->connected = GNUNET_NO;
1518 sc->options = options;
1519 if (direction == _SEND)
1521 s->server_send = sc;
1523 if (direction == _RECEIVE)
1525 s->server_recv = sc;
1528 if ((GNUNET_NO == s->known_to_service) &&
1529 (NULL != s->server_send) &&
1530 (NULL != s->server_recv) )
1532 s->known_to_service = GNUNET_YES;
1533 notify_session_monitor (plugin,
1535 GNUNET_TRANSPORT_SS_UP);
1536 plugin->env->session_start (plugin->env->cls,
1542 to = (HTTP_SERVER_SESSION_TIMEOUT.rel_value_us / 1000LL / 1000LL);
1543 server_mhd_connection_timeout (plugin, s, to);
1549 * Callback called by MHD when it needs data to send
1551 * @param cls current session
1552 * @param pos position in buffer
1553 * @param buf the buffer to write data to
1554 * @param max max number of bytes available in @a buf
1555 * @return bytes written to @a buf
1558 server_send_callback (void *cls,
1563 struct ServerRequest *sc = cls;
1564 struct GNUNET_ATS_Session *s = sc->session;
1565 ssize_t bytes_read = 0;
1566 struct HTTP_Message *msg;
1571 /* session is disconnecting */
1575 sc = s->server_send;
1582 bytes_read = GNUNET_MIN (msg->size - msg->pos,
1584 GNUNET_memcpy (buf, &msg->buf[msg->pos], bytes_read);
1585 msg->pos += bytes_read;
1587 /* removing message */
1588 if (msg->pos == msg->size)
1590 GNUNET_CONTAINER_DLL_remove (s->msg_head,
1593 if (NULL != msg->transmit_cont)
1594 msg->transmit_cont (msg->transmit_cont_cls, &s->target, GNUNET_OK,
1595 msg->size, msg->size + msg->overhead);
1596 GNUNET_assert (s->msgs_in_queue > 0);
1598 GNUNET_assert (s->bytes_in_queue >= msg->size);
1599 s->bytes_in_queue -= msg->size;
1601 notify_session_monitor (s->plugin,
1603 GNUNET_TRANSPORT_SS_UPDATE);
1608 sc->connected = GNUNET_YES;
1609 LOG (GNUNET_ERROR_TYPE_DEBUG,
1610 "Sent %u bytes to peer `%s' with session %p \n",
1612 GNUNET_i2s (&s->target),
1614 GNUNET_asprintf (&stat_txt,
1615 "# bytes currently in %s_server buffers",
1616 s->plugin->protocol);
1617 GNUNET_STATISTICS_update (s->plugin->env->stats,
1621 GNUNET_free (stat_txt);
1622 GNUNET_asprintf (&stat_txt,
1623 "# bytes transmitted via %s_server",
1624 s->plugin->protocol);
1625 GNUNET_STATISTICS_update (s->plugin->env->stats,
1626 stat_txt, bytes_read, GNUNET_NO);
1627 GNUNET_free (stat_txt);
1629 else if ((sc->options & OPTION_LONG_POLL) && sc->connected)
1631 LOG (GNUNET_ERROR_TYPE_DEBUG,
1632 "Completing GET response to peer `%s' with session %p\n",
1633 GNUNET_i2s (&s->target),
1635 return MHD_CONTENT_READER_END_OF_STREAM;
1639 MHD_suspend_connection (s->server_send->mhd_conn);
1640 s->server_send->suspended = true;
1648 * Callback called by MessageStreamTokenizer when a message has arrived
1650 * @param cls current session as closure
1651 * @param message the message to be forwarded to transport service
1652 * @return #GNUNET_OK (all OK)
1655 server_receive_mst_cb (void *cls,
1656 const struct GNUNET_MessageHeader *message)
1658 struct GNUNET_ATS_Session *s = cls;
1659 struct HTTP_Server_Plugin *plugin = s->plugin;
1660 struct GNUNET_TIME_Relative delay;
1663 if (GNUNET_NO == s->known_to_service)
1665 s->known_to_service = GNUNET_YES;
1666 plugin->env->session_start (plugin->env->cls,
1670 notify_session_monitor (plugin,
1672 GNUNET_TRANSPORT_SS_UP);
1674 delay = plugin->env->receive (plugin->env->cls,
1678 GNUNET_asprintf (&stat_txt,
1679 "# bytes received via %s_server",
1681 GNUNET_STATISTICS_update (plugin->env->stats,
1683 ntohs (message->size),
1685 GNUNET_free (stat_txt);
1686 s->next_receive = GNUNET_TIME_relative_to_absolute (delay);
1687 if (delay.rel_value_us > 0)
1689 LOG (GNUNET_ERROR_TYPE_DEBUG,
1690 "Peer `%s' address `%s' next read delayed for %s\n",
1691 GNUNET_i2s (&s->target),
1692 http_common_plugin_address_to_string (plugin->protocol,
1693 s->address->address,
1694 s->address->address_length),
1695 GNUNET_STRINGS_relative_time_to_string (delay,
1698 server_reschedule_session_timeout (s);
1704 * Add headers to a request indicating that we allow Cross-Origin Resource
1707 * @param response response object to modify
1710 add_cors_headers(struct MHD_Response *response)
1712 MHD_add_response_header (response,
1713 "Access-Control-Allow-Origin",
1715 MHD_add_response_header (response,
1716 "Access-Control-Allow-Methods",
1717 "GET, PUT, OPTIONS");
1718 MHD_add_response_header (response,
1719 "Access-Control-Max-Age",
1725 * MHD callback for a new incoming connection
1727 * @param cls the plugin handle
1728 * @param mhd_connection the mhd connection
1729 * @param url the requested URL
1730 * @param method GET or PUT
1731 * @param version HTTP version
1732 * @param upload_data upload data
1733 * @param upload_data_size size of @a upload_data
1734 * @param httpSessionCache the session cache to remember the connection
1735 * @return MHD_YES if connection is accepted, MHD_NO on reject
1738 server_access_cb (void *cls,
1739 struct MHD_Connection *mhd_connection,
1742 const char *version,
1743 const char *upload_data,
1744 size_t *upload_data_size,
1745 void **httpSessionCache)
1747 struct HTTP_Server_Plugin *plugin = cls;
1748 struct ServerRequest *sc = *httpSessionCache;
1749 struct GNUNET_ATS_Session *s;
1750 struct MHD_Response *response;
1753 LOG (GNUNET_ERROR_TYPE_DEBUG,
1754 _("Access from connection %p (%u of %u) for `%s' `%s' url `%s' with upload data size %u\n"),
1756 plugin->cur_request,
1757 plugin->max_request,
1761 (*upload_data_size));
1764 /* CORS pre-flight request */
1765 if (0 == strcmp (MHD_HTTP_METHOD_OPTIONS, method))
1767 response = MHD_create_response_from_buffer (0, NULL,
1768 MHD_RESPMEM_PERSISTENT);
1769 add_cors_headers(response);
1770 res = MHD_queue_response (mhd_connection, MHD_HTTP_OK, response);
1771 MHD_destroy_response (response);
1774 /* new connection */
1775 sc = server_lookup_connection (plugin, mhd_connection, url, method);
1778 /* attach to new / existing session */
1779 (*httpSessionCache) = sc;
1783 /* existing session already has matching connection, refuse */
1784 response = MHD_create_response_from_buffer (strlen (HTTP_ERROR_RESPONSE),
1785 HTTP_ERROR_RESPONSE,
1786 MHD_RESPMEM_PERSISTENT);
1787 MHD_add_response_header (response,
1788 MHD_HTTP_HEADER_CONTENT_TYPE,
1790 add_cors_headers(response);
1791 res = MHD_queue_response (mhd_connection, MHD_HTTP_NOT_FOUND, response);
1792 MHD_destroy_response (response);
1796 /* 'old' connection */
1797 if (NULL == (s = sc->session))
1799 /* Session was already disconnected;
1800 sent HTTP/1.1: 200 OK as response */
1801 response = MHD_create_response_from_buffer (strlen ("Thank you!"),
1803 MHD_RESPMEM_PERSISTENT);
1804 add_cors_headers(response);
1805 MHD_queue_response (mhd_connection, MHD_HTTP_OK, response);
1806 MHD_destroy_response (response);
1810 if (sc->direction == _SEND)
1812 response = MHD_create_response_from_callback (MHD_SIZE_UNKNOWN, 32 * 1024,
1813 &server_send_callback, sc, NULL);
1814 add_cors_headers(response);
1815 MHD_queue_response (mhd_connection, MHD_HTTP_OK, response);
1816 MHD_destroy_response (response);
1819 if (sc->direction == _RECEIVE)
1821 if ((*upload_data_size == 0) && (sc->connected == GNUNET_NO))
1823 /* (*upload_data_size == 0) first callback when header are passed */
1824 LOG (GNUNET_ERROR_TYPE_DEBUG,
1825 "Session %p / Connection %p: Peer `%s' PUT on address `%s' connected\n",
1827 GNUNET_i2s (&s->target),
1828 http_common_plugin_address_to_string (plugin->protocol,
1829 s->address->address,
1830 s->address->address_length));
1831 sc->connected = GNUNET_YES;
1834 else if ((*upload_data_size == 0) && (sc->connected == GNUNET_YES))
1836 /* (*upload_data_size == 0) when upload is complete */
1837 LOG (GNUNET_ERROR_TYPE_DEBUG,
1838 "Session %p / Connection %p: Peer `%s' PUT on address `%s' finished upload\n",
1840 GNUNET_i2s (&s->target),
1841 http_common_plugin_address_to_string (plugin->protocol,
1842 s->address->address,
1843 s->address->address_length));
1844 sc->connected = GNUNET_NO;
1845 /* Sent HTTP/1.1: 200 OK as PUT Response\ */
1846 response = MHD_create_response_from_buffer (strlen ("Thank you!"),
1848 MHD_RESPMEM_PERSISTENT);
1849 add_cors_headers(response);
1850 MHD_queue_response (mhd_connection, MHD_HTTP_OK, response);
1851 MHD_destroy_response (response);
1854 else if ((*upload_data_size > 0) && (sc->connected == GNUNET_YES))
1856 struct GNUNET_TIME_Relative delay;
1858 /* (*upload_data_size > 0) for every segment received */
1859 LOG (GNUNET_ERROR_TYPE_DEBUG,
1860 "Session %p / Connection %p: Peer `%s' PUT on address `%s' received %u bytes\n",
1862 GNUNET_i2s (&s->target),
1863 http_common_plugin_address_to_string (plugin->protocol,
1864 s->address->address,
1865 s->address->address_length),
1867 delay = GNUNET_TIME_absolute_get_remaining (s->next_receive);
1868 if (0 == delay.rel_value_us)
1870 LOG (GNUNET_ERROR_TYPE_DEBUG,
1871 "PUT with %u bytes forwarded to MST\n",
1873 if (s->msg_tk == NULL)
1875 s->msg_tk = GNUNET_MST_create (&server_receive_mst_cb,
1878 GNUNET_MST_from_buffer (s->msg_tk,
1881 GNUNET_NO, GNUNET_NO);
1882 server_mhd_connection_timeout (plugin, s,
1883 GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT.rel_value_us / 1000LL
1885 (*upload_data_size) = 0;
1889 /* delay processing */
1890 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1891 "Session %p / Connection %p: no inbound bandwidth available! Next read was delayed by %s\n",
1894 GNUNET_STRINGS_relative_time_to_string (delay,
1896 GNUNET_assert(s->server_recv->mhd_conn == mhd_connection);
1897 MHD_suspend_connection (s->server_recv->mhd_conn);
1898 s->server_recv->suspended = true;
1899 if (NULL == s->recv_wakeup_task)
1901 = GNUNET_SCHEDULER_add_delayed (delay,
1918 * Callback from MHD when a connection disconnects
1920 * @param cls closure with the `struct HTTP_Server_Plugin *`
1921 * @param connection the disconnected MHD connection
1922 * @param httpSessionCache the pointer to distinguish
1925 server_disconnect_cb (void *cls,
1926 struct MHD_Connection *connection,
1927 void **httpSessionCache)
1929 struct HTTP_Server_Plugin *plugin = cls;
1930 struct ServerRequest *sc = *httpSessionCache;
1932 LOG (GNUNET_ERROR_TYPE_DEBUG,
1933 "Disconnect for connection %p\n",
1937 /* CORS pre-flight request finished */
1941 if (NULL != sc->session)
1943 if (sc->direction == _SEND)
1945 LOG (GNUNET_ERROR_TYPE_DEBUG,
1946 "Peer `%s' connection %p, GET on address `%s' disconnected\n",
1947 GNUNET_i2s (&sc->session->target),
1948 sc->session->server_send,
1949 http_common_plugin_address_to_string (plugin->protocol,
1950 sc->session->address->address,
1951 sc->session->address->address_length));
1953 sc->session->server_send = NULL;
1955 else if (sc->direction == _RECEIVE)
1957 LOG (GNUNET_ERROR_TYPE_DEBUG,
1958 "Peer `%s' connection %p PUT on address `%s' disconnected\n",
1959 GNUNET_i2s (&sc->session->target),
1960 sc->session->server_recv,
1961 http_common_plugin_address_to_string (plugin->protocol,
1962 sc->session->address->address,
1963 sc->session->address->address_length));
1964 sc->session->server_recv = NULL;
1965 if (NULL != sc->session->msg_tk)
1967 GNUNET_MST_destroy (sc->session->msg_tk);
1968 sc->session->msg_tk = NULL;
1973 plugin->cur_request--;
1978 * Callback from MHD when a connection starts/stops
1980 * @param cls closure with the `struct HTTP_Server_Plugin *`
1981 * @param connection connection handle
1982 * @param socket_context socket-specific pointer
1983 * @param toe reason for connection notification
1984 * @see #MHD_OPTION_NOTIFY_CONNECTION
1987 server_connection_cb (void *cls,
1988 struct MHD_Connection *connection,
1989 void **socket_context,
1990 enum MHD_ConnectionNotificationCode toe)
1992 struct HTTP_Server_Plugin *plugin = cls;
1993 const union MHD_ConnectionInfo *info;
1995 if (MHD_CONNECTION_NOTIFY_STARTED == toe)
1998 /* Reschedule to remove closed socket from our select set */
1999 info = MHD_get_connection_info (connection,
2000 MHD_CONNECTION_INFO_DAEMON);
2001 GNUNET_assert (NULL != info);
2002 server_reschedule (plugin,
2009 * Check if incoming connection is accepted.
2011 * @param cls plugin as closure
2012 * @param addr address of incoming connection
2013 * @param addr_len number of bytes in @a addr
2014 * @return MHD_YES if connection is accepted, MHD_NO if connection is rejected
2017 server_accept_cb (void *cls,
2018 const struct sockaddr *addr,
2021 struct HTTP_Server_Plugin *plugin = cls;
2023 if (plugin->cur_request <= plugin->max_request)
2025 LOG (GNUNET_ERROR_TYPE_DEBUG,
2026 _("Accepting connection (%u of %u) from `%s'\n"),
2027 plugin->cur_request, plugin->max_request,
2028 GNUNET_a2s (addr, addr_len));
2033 LOG (GNUNET_ERROR_TYPE_WARNING,
2034 _("Server reached maximum number connections (%u), rejecting new connection\n"),
2035 plugin->max_request);
2042 * Log function called by MHD.
2045 * @param fmt format string
2046 * @param ap arguments for the format string (va_start() and va_end()
2047 * will be called by MHD)
2050 server_log (void *arg,
2060 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2068 * Load ssl certificate from file
2070 * @param file filename
2071 * @return content of the file
2074 server_load_file (const char *file)
2076 struct GNUNET_DISK_FileHandle *gn_file;
2080 if (GNUNET_OK != GNUNET_DISK_file_size (file,
2081 &fsize, GNUNET_NO, GNUNET_YES))
2083 text = GNUNET_malloc (fsize + 1);
2085 GNUNET_DISK_file_open (file, GNUNET_DISK_OPEN_READ,
2086 GNUNET_DISK_PERM_USER_READ);
2087 if (NULL == gn_file)
2092 if (GNUNET_SYSERR == GNUNET_DISK_file_read (gn_file, text, fsize))
2095 GNUNET_DISK_file_close (gn_file);
2099 GNUNET_DISK_file_close (gn_file);
2107 * Load ssl certificate
2109 * @param plugin the plugin
2110 * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
2113 server_load_certificate (struct HTTP_Server_Plugin *plugin)
2115 int res = GNUNET_OK;
2121 GNUNET_CONFIGURATION_get_value_filename (plugin->env->cfg,
2123 "KEY_FILE", &key_file))
2125 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
2126 plugin->name, "CERT_FILE");
2127 return GNUNET_SYSERR;
2130 GNUNET_CONFIGURATION_get_value_filename (plugin->env->cfg,
2132 "CERT_FILE", &cert_file))
2134 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
2135 plugin->name, "CERT_FILE");
2136 GNUNET_free (key_file);
2137 return GNUNET_SYSERR;
2139 /* Get crypto init string from config. If not present, use
2142 GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg,
2145 &plugin->crypto_init))
2146 LOG (GNUNET_ERROR_TYPE_DEBUG,
2147 "Using crypto init string `%s'\n",
2148 plugin->crypto_init);
2150 LOG (GNUNET_ERROR_TYPE_DEBUG,
2151 "Using default crypto init string \n");
2153 /* read key & certificates from file */
2154 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2155 "Trying to loading TLS certificate from key-file `%s' cert-file`%s'\n",
2156 key_file, cert_file);
2158 plugin->key = server_load_file (key_file);
2159 plugin->cert = server_load_file (cert_file);
2161 if ((plugin->key == NULL) || (plugin->cert == NULL))
2163 struct GNUNET_OS_Process *cert_creation;
2165 GNUNET_free_non_null (plugin->key);
2167 GNUNET_free_non_null (plugin->cert);
2168 plugin->cert = NULL;
2170 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2171 "No usable TLS certificate found, creating certificate\n");
2174 GNUNET_OS_start_process (GNUNET_NO, GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
2176 "gnunet-transport-certificate-creation",
2177 "gnunet-transport-certificate-creation",
2181 if (NULL == cert_creation)
2183 LOG (GNUNET_ERROR_TYPE_ERROR,
2184 _("Could not create a new TLS certificate, program `gnunet-transport-certificate-creation' could not be started!\n"));
2185 GNUNET_free (key_file);
2186 GNUNET_free (cert_file);
2188 GNUNET_free_non_null (plugin->key);
2190 GNUNET_free_non_null (plugin->cert);
2191 plugin->cert = NULL;
2192 GNUNET_free_non_null (plugin->crypto_init);
2193 plugin->crypto_init = NULL;
2195 return GNUNET_SYSERR;
2197 GNUNET_assert (GNUNET_OK == GNUNET_OS_process_wait (cert_creation));
2198 GNUNET_OS_process_destroy (cert_creation);
2200 plugin->key = server_load_file (key_file);
2201 plugin->cert = server_load_file (cert_file);
2204 if ((plugin->key == NULL) || (plugin->cert == NULL))
2206 LOG (GNUNET_ERROR_TYPE_ERROR,
2207 _("No usable TLS certificate found and creating one at `%s/%s' failed!\n"),
2208 key_file, cert_file);
2209 GNUNET_free (key_file);
2210 GNUNET_free (cert_file);
2212 GNUNET_free_non_null (plugin->key);
2214 GNUNET_free_non_null (plugin->cert);
2215 plugin->cert = NULL;
2216 GNUNET_free_non_null (plugin->crypto_init);
2217 plugin->crypto_init = NULL;
2219 return GNUNET_SYSERR;
2221 GNUNET_free (key_file);
2222 GNUNET_free (cert_file);
2223 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2224 "TLS certificate loaded\n");
2231 * Invoke `MHD_start_daemon` with the various options we need to
2232 * setup the HTTP server with the given listen address.
2234 * @param plugin our plugin
2235 * @param addr listen address to use
2236 * @param v6 MHD_NO_FLAG or MHD_USE_IPv6, depending on context
2237 * @return NULL on error
2239 static struct MHD_Daemon *
2240 run_mhd_start_daemon (struct HTTP_Server_Plugin *plugin,
2241 const struct sockaddr_in *addr,
2244 struct MHD_Daemon *server;
2245 unsigned int timeout;
2247 #if MHD_VERSION >= 0x00090E00
2248 timeout = HTTP_SERVER_NOT_VALIDATED_TIMEOUT.rel_value_us / 1000LL / 1000LL;
2249 LOG (GNUNET_ERROR_TYPE_DEBUG,
2250 "MHD can set timeout per connection! Default time out %u sec.\n",
2253 timeout = HTTP_SERVER_SESSION_TIMEOUT.rel_value_us / 1000LL / 1000LL;
2254 LOG (GNUNET_ERROR_TYPE_WARNING,
2255 "MHD cannot set timeout per connection! Default time out %u sec.\n",
2258 server = MHD_start_daemon (
2265 MHD_USE_SUSPEND_RESUME |
2268 &server_accept_cb, plugin,
2269 &server_access_cb, plugin,
2270 MHD_OPTION_SOCK_ADDR,
2272 MHD_OPTION_CONNECTION_LIMIT,
2273 (unsigned int) plugin->max_request,
2275 MHD_OPTION_HTTPS_PRIORITIES,
2276 plugin->crypto_init,
2277 MHD_OPTION_HTTPS_MEM_KEY,
2279 MHD_OPTION_HTTPS_MEM_CERT,
2282 MHD_OPTION_CONNECTION_TIMEOUT,
2284 MHD_OPTION_CONNECTION_MEMORY_LIMIT,
2286 GNUNET_MAX_MESSAGE_SIZE),
2287 MHD_OPTION_NOTIFY_COMPLETED,
2288 &server_disconnect_cb, plugin,
2289 MHD_OPTION_NOTIFY_CONNECTION,
2290 &server_connection_cb, plugin,
2291 MHD_OPTION_EXTERNAL_LOGGER,
2295 if ( (NULL != server) &&
2296 (0 != (plugin->options & HTTP_OPTIONS_TCP_STEALTH)) )
2298 const union MHD_DaemonInfo *di;
2300 di = MHD_get_daemon_info (server,
2301 MHD_DAEMON_INFO_LISTEN_FD,
2303 if ( (0 != setsockopt ((int) di->listen_fd,
2306 plugin->env->my_identity,
2307 sizeof (struct GNUNET_PeerIdentity))) )
2309 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2310 _("TCP_STEALTH not supported on this platform.\n"));
2311 MHD_stop_daemon (server);
2321 * Start the HTTP server
2323 * @param plugin the plugin handle
2324 * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
2327 server_start (struct HTTP_Server_Plugin *plugin)
2331 GNUNET_assert (NULL != plugin);
2333 if (GNUNET_SYSERR == server_load_certificate (plugin))
2335 LOG (GNUNET_ERROR_TYPE_ERROR,
2336 _("Could not load or create server certificate! Loading plugin failed!\n"));
2337 return GNUNET_SYSERR;
2343 plugin->server_v4 = NULL;
2344 if (GNUNET_YES == plugin->use_ipv4)
2347 = run_mhd_start_daemon (plugin,
2348 (const struct sockaddr_in *) plugin->server_addr_v4,
2351 if (NULL == plugin->server_v4)
2353 LOG (GNUNET_ERROR_TYPE_ERROR,
2354 "Failed to start %s IPv4 server component on port %u\n",
2359 server_reschedule (plugin,
2365 plugin->server_v6 = NULL;
2366 if (GNUNET_YES == plugin->use_ipv6)
2369 = run_mhd_start_daemon (plugin,
2370 (const struct sockaddr_in *) plugin->server_addr_v6,
2372 if (NULL == plugin->server_v6)
2374 LOG (GNUNET_ERROR_TYPE_ERROR,
2375 "Failed to start %s IPv6 server component on port %u\n",
2381 server_reschedule (plugin,
2387 if ( (NULL == plugin->server_v6) &&
2388 (NULL == plugin->server_v4) )
2390 LOG (GNUNET_ERROR_TYPE_ERROR,
2391 "%s %s server component started on port %u\n",
2395 return GNUNET_SYSERR;
2397 if ((NULL != plugin->server_v6) &&
2398 (NULL != plugin->server_v4))
2399 msg = "IPv4 and IPv6";
2400 else if (NULL != plugin->server_v6)
2402 else if (NULL != plugin->server_v4)
2404 LOG (GNUNET_ERROR_TYPE_DEBUG,
2405 "%s %s server component started on port %u\n",
2414 * Add an address to the server's set of addresses and notify transport
2416 * @param cls the plugin handle
2417 * @param add_remove #GNUNET_YES on add, #GNUNET_NO on remove
2418 * @param addr the address
2419 * @param addrlen address length
2422 server_add_address (void *cls,
2424 const struct sockaddr *addr,
2427 struct HTTP_Server_Plugin *plugin = cls;
2428 struct GNUNET_HELLO_Address *address;
2429 struct HttpAddressWrapper *w = NULL;
2431 w = GNUNET_new (struct HttpAddressWrapper);
2432 w->address = http_common_address_from_socket (plugin->protocol,
2435 if (NULL == w->address)
2440 w->addrlen = http_common_address_get_size (w->address);
2442 GNUNET_CONTAINER_DLL_insert (plugin->addr_head,
2445 LOG (GNUNET_ERROR_TYPE_DEBUG,
2446 "Notifying transport to add address `%s'\n",
2447 http_common_plugin_address_to_string (plugin->protocol,
2450 /* modify our published address list */
2452 address = GNUNET_HELLO_address_allocate (plugin->env->my_identity,
2453 "https_client", w->address, w->addrlen, GNUNET_HELLO_ADDRESS_INFO_NONE);
2455 address = GNUNET_HELLO_address_allocate (plugin->env->my_identity,
2456 "http_client", w->address, w->addrlen, GNUNET_HELLO_ADDRESS_INFO_NONE);
2459 plugin->env->notify_address (plugin->env->cls,
2462 GNUNET_HELLO_address_free (address);
2467 * Remove an address from the server's set of addresses and notify transport
2469 * @param cls the plugin handle
2470 * @param add_remove #GNUNET_YES on add, #GNUNET_NO on remove
2471 * @param addr the address
2472 * @param addrlen address length
2475 server_remove_address (void *cls,
2477 const struct sockaddr *addr,
2480 struct HTTP_Server_Plugin *plugin = cls;
2481 struct GNUNET_HELLO_Address *address;
2482 struct HttpAddressWrapper *w = plugin->addr_head;
2486 saddr = http_common_address_from_socket (plugin->protocol,
2491 saddr_len = http_common_address_get_size (saddr);
2496 http_common_cmp_addresses (w->address,
2503 GNUNET_free (saddr);
2508 LOG (GNUNET_ERROR_TYPE_DEBUG,
2509 "Notifying transport to remove address `%s'\n",
2510 http_common_plugin_address_to_string (plugin->protocol,
2513 GNUNET_CONTAINER_DLL_remove (plugin->addr_head,
2516 /* modify our published address list */
2518 address = GNUNET_HELLO_address_allocate (plugin->env->my_identity,
2519 "https_client", w->address, w->addrlen, GNUNET_HELLO_ADDRESS_INFO_NONE);
2521 address = GNUNET_HELLO_address_allocate (plugin->env->my_identity,
2522 "http_client", w->address, w->addrlen, GNUNET_HELLO_ADDRESS_INFO_NONE);
2524 plugin->env->notify_address (plugin->env->cls, add_remove, address);
2525 GNUNET_HELLO_address_free (address);
2526 GNUNET_free (w->address);
2533 * Our external IP address/port mapping has changed.
2535 * @param cls closure, the 'struct LocalAddrList'
2536 * @param add_remove #GNUNET_YES to mean the new public IP address, #GNUNET_NO to mean
2537 * the previous (now invalid) one
2538 * @param ac address class the address belongs to
2539 * @param addr either the previous or the new public IP address
2540 * @param addrlen actual lenght of the address
2543 server_nat_port_map_callback (void *cls,
2545 enum GNUNET_NAT_AddressClass ac,
2546 const struct sockaddr *addr,
2549 struct HTTP_Server_Plugin *plugin = cls;
2551 LOG (GNUNET_ERROR_TYPE_DEBUG,
2552 "NAT called to %s address `%s'\n",
2553 (add_remove == GNUNET_NO) ? "remove" : "add",
2554 GNUNET_a2s (addr, addrlen));
2556 if (AF_INET == addr->sa_family)
2558 struct sockaddr_in *s4 = (struct sockaddr_in *) addr;
2560 if (GNUNET_NO == plugin->use_ipv4)
2563 if ((NULL != plugin->server_addr_v4) &&
2564 (0 != memcmp (&plugin->server_addr_v4->sin_addr,
2566 sizeof (struct in_addr))))
2568 LOG (GNUNET_ERROR_TYPE_DEBUG,
2569 "Skipping address `%s' (not bindto address)\n",
2570 GNUNET_a2s (addr, addrlen));
2575 if (AF_INET6 == addr->sa_family)
2577 struct sockaddr_in6 *s6 = (struct sockaddr_in6 *) addr;
2578 if (GNUNET_NO == plugin->use_ipv6)
2581 if ((NULL != plugin->server_addr_v6) &&
2582 (0 != memcmp (&plugin->server_addr_v6->sin6_addr,
2583 &s6->sin6_addr, sizeof (struct in6_addr))))
2585 LOG (GNUNET_ERROR_TYPE_DEBUG,
2586 "Skipping address `%s' (not bindto address)\n",
2587 GNUNET_a2s (addr, addrlen));
2595 server_add_address (cls, add_remove, addr, addrlen);
2598 server_remove_address (cls, add_remove, addr, addrlen);
2605 * Get valid server addresses
2607 * @param plugin the plugin handle
2608 * @param service_name the servicename
2609 * @param cfg configuration handle
2610 * @param addrs addresses
2611 * @param addr_lens address length
2612 * @return number of addresses
2615 server_get_addresses (struct HTTP_Server_Plugin *plugin,
2616 const char *service_name,
2617 const struct GNUNET_CONFIGURATION_Handle *cfg,
2618 struct sockaddr ***addrs,
2619 socklen_t ** addr_lens)
2622 unsigned long long port;
2623 struct addrinfo hints;
2624 struct addrinfo *res;
2625 struct addrinfo *pos;
2626 struct addrinfo *next;
2630 struct sockaddr **saddrs;
2631 socklen_t *saddrlens;
2637 disablev6 = !plugin->use_ipv6;
2640 if (GNUNET_CONFIGURATION_have_value (cfg, service_name, "PORT"))
2642 GNUNET_break (GNUNET_OK ==
2643 GNUNET_CONFIGURATION_get_value_number (cfg, service_name,
2647 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2648 _("Require valid port number for service in configuration!\n"));
2649 return GNUNET_SYSERR;
2654 LOG (GNUNET_ERROR_TYPE_INFO,
2655 "Starting in listen only mode\n");
2656 return -1; /* listen only */
2660 if (GNUNET_CONFIGURATION_have_value (cfg, service_name,
2663 GNUNET_break (GNUNET_OK ==
2664 GNUNET_CONFIGURATION_get_value_string (cfg, service_name,
2665 "BINDTO", &hostname));
2670 if (NULL != hostname)
2672 LOG (GNUNET_ERROR_TYPE_DEBUG,
2673 "Resolving `%s' since that is where `%s' will bind to.\n",
2674 hostname, service_name);
2675 memset (&hints, 0, sizeof (struct addrinfo));
2677 hints.ai_family = AF_INET;
2678 if ((0 != (ret = getaddrinfo (hostname, NULL, &hints, &res))) ||
2681 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2682 _("Failed to resolve `%s': %s\n"),
2684 gai_strerror (ret));
2685 GNUNET_free (hostname);
2686 return GNUNET_SYSERR;
2690 while (NULL != (pos = next))
2692 next = pos->ai_next;
2693 if ((disablev6) && (pos->ai_family == AF_INET6))
2699 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2700 _("Failed to find %saddress for `%s'.\n"),
2701 disablev6 ? "IPv4 " : "", hostname);
2703 GNUNET_free (hostname);
2704 return GNUNET_SYSERR;
2707 saddrs = GNUNET_malloc ((resi + 1) * sizeof (struct sockaddr *));
2708 saddrlens = GNUNET_malloc ((resi + 1) * sizeof (socklen_t));
2711 while (NULL != (pos = next))
2713 next = pos->ai_next;
2714 if ((disablev6) && (pos->ai_family == AF_INET6))
2716 if ((pos->ai_protocol != IPPROTO_TCP) && (0 != pos->ai_protocol))
2717 continue; /* not TCP */
2718 if ((pos->ai_socktype != SOCK_STREAM) && (0 != pos->ai_socktype))
2719 continue; /* huh? */
2720 LOG (GNUNET_ERROR_TYPE_DEBUG,
2721 "Service will bind to `%s'\n",
2722 GNUNET_a2s (pos->ai_addr,
2724 if (pos->ai_family == AF_INET)
2726 GNUNET_assert (pos->ai_addrlen == sizeof (struct sockaddr_in));
2727 saddrlens[i] = pos->ai_addrlen;
2728 saddrs[i] = GNUNET_malloc (saddrlens[i]);
2729 GNUNET_memcpy (saddrs[i], pos->ai_addr, saddrlens[i]);
2730 ((struct sockaddr_in *) saddrs[i])->sin_port = htons (port);
2734 GNUNET_assert (pos->ai_family == AF_INET6);
2735 GNUNET_assert (pos->ai_addrlen == sizeof (struct sockaddr_in6));
2736 saddrlens[i] = pos->ai_addrlen;
2737 saddrs[i] = GNUNET_malloc (saddrlens[i]);
2738 GNUNET_memcpy (saddrs[i], pos->ai_addr, saddrlens[i]);
2739 ((struct sockaddr_in6 *) saddrs[i])->sin6_port = htons (port);
2743 GNUNET_free (hostname);
2749 /* will bind against everything, just set port */
2755 saddrs = GNUNET_malloc ((resi + 1) * sizeof (struct sockaddr *));
2756 saddrlens = GNUNET_malloc ((resi + 1) * sizeof (socklen_t));
2758 saddrlens[i] = sizeof (struct sockaddr_in);
2759 saddrs[i] = GNUNET_malloc (saddrlens[i]);
2760 #if HAVE_SOCKADDR_IN_SIN_LEN
2761 ((struct sockaddr_in *) saddrs[i])->sin_len = saddrlens[i];
2763 ((struct sockaddr_in *) saddrs[i])->sin_family = AF_INET;
2764 ((struct sockaddr_in *) saddrs[i])->sin_port = htons (port);
2770 saddrs = GNUNET_malloc ((resi + 1) * sizeof (struct sockaddr *));
2771 saddrlens = GNUNET_malloc ((resi + 1) * sizeof (socklen_t));
2773 saddrlens[i] = sizeof (struct sockaddr_in6);
2774 saddrs[i] = GNUNET_malloc (saddrlens[i]);
2775 #if HAVE_SOCKADDR_IN_SIN_LEN
2776 ((struct sockaddr_in6 *) saddrs[i])->sin6_len = saddrlens[0];
2778 ((struct sockaddr_in6 *) saddrs[i])->sin6_family = AF_INET6;
2779 ((struct sockaddr_in6 *) saddrs[i])->sin6_port = htons (port);
2781 saddrlens[i] = sizeof (struct sockaddr_in);
2782 saddrs[i] = GNUNET_malloc (saddrlens[i]);
2783 #if HAVE_SOCKADDR_IN_SIN_LEN
2784 ((struct sockaddr_in *) saddrs[i])->sin_len = saddrlens[1];
2786 ((struct sockaddr_in *) saddrs[i])->sin_family = AF_INET;
2787 ((struct sockaddr_in *) saddrs[i])->sin_port = htons (port);
2791 *addr_lens = saddrlens;
2797 * Ask NAT for addresses
2799 * @param plugin the plugin handle
2802 server_start_report_addresses (struct HTTP_Server_Plugin *plugin)
2804 int res = GNUNET_OK;
2805 struct sockaddr **addrs;
2806 socklen_t *addrlens;
2808 res = server_get_addresses (plugin,
2812 LOG (GNUNET_ERROR_TYPE_DEBUG,
2813 _("Found %u addresses to report to NAT service\n"),
2816 if (GNUNET_SYSERR == res)
2823 = GNUNET_NAT_register (plugin->env->cfg,
2824 "transport-http_server",
2827 (const struct sockaddr **) addrs,
2829 &server_nat_port_map_callback,
2835 GNUNET_assert (NULL != addrs[res]);
2836 GNUNET_free (addrs[res]);
2838 GNUNET_free_non_null (addrs);
2839 GNUNET_free_non_null (addrlens);
2844 * Stop NAT for addresses
2846 * @param plugin the plugin handle
2849 server_stop_report_addresses (struct HTTP_Server_Plugin *plugin)
2851 struct HttpAddressWrapper *w;
2853 /* Stop NAT handle */
2854 if (NULL != plugin->nat)
2856 GNUNET_NAT_unregister (plugin->nat);
2859 /* Clean up addresses */
2860 while (NULL != plugin->addr_head)
2862 w = plugin->addr_head;
2863 GNUNET_CONTAINER_DLL_remove (plugin->addr_head,
2866 GNUNET_free (w->address);
2873 * Check if IPv6 supported on this system
2875 * @param plugin the plugin handle
2876 * @return #GNUNET_YES on success, else #GNUNET_NO
2879 server_check_ipv6_support (struct HTTP_Server_Plugin *plugin)
2881 struct GNUNET_NETWORK_Handle *desc = NULL;
2882 int res = GNUNET_NO;
2884 /* Probe IPv6 support */
2885 desc = GNUNET_NETWORK_socket_create (PF_INET6,
2890 if ( (errno == ENOBUFS) ||
2891 (errno == ENOMEM) ||
2892 (errno == ENFILE) ||
2895 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR,
2898 LOG (GNUNET_ERROR_TYPE_WARNING,
2899 _("Disabling IPv6 since it is not supported on this system!\n"));
2904 GNUNET_break (GNUNET_OK ==
2905 GNUNET_NETWORK_socket_close (desc));
2909 LOG (GNUNET_ERROR_TYPE_DEBUG,
2910 "Testing IPv6 on this system: %s\n",
2911 (res == GNUNET_YES) ? "successful" : "failed");
2917 * Notify server about our external hostname
2922 server_notify_external_hostname (void *cls)
2924 struct HTTP_Server_Plugin *plugin = cls;
2925 struct HttpAddress *ext_addr;
2926 size_t ext_addr_len;
2930 plugin->notify_ext_task = NULL;
2931 GNUNET_asprintf (&url,
2934 plugin->external_hostname);
2935 urlen = strlen (url) + 1;
2936 ext_addr = GNUNET_malloc (sizeof (struct HttpAddress) + urlen);
2937 ext_addr->options = htonl (plugin->options);
2938 ext_addr->urlen = htonl (urlen);
2939 ext_addr_len = sizeof (struct HttpAddress) + urlen;
2940 GNUNET_memcpy (&ext_addr[1], url, urlen);
2943 LOG (GNUNET_ERROR_TYPE_DEBUG,
2944 "Notifying transport about external hostname address `%s'\n",
2945 plugin->external_hostname);
2948 if (GNUNET_YES == plugin->verify_external_hostname)
2949 LOG (GNUNET_ERROR_TYPE_INFO,
2950 "Enabling SSL verification for external hostname address `%s'\n",
2951 plugin->external_hostname);
2953 = GNUNET_HELLO_address_allocate (plugin->env->my_identity,
2957 GNUNET_HELLO_ADDRESS_INFO_NONE);
2958 plugin->env->notify_address (plugin->env->cls,
2961 GNUNET_free (ext_addr);
2964 = GNUNET_HELLO_address_allocate (plugin->env->my_identity,
2968 GNUNET_HELLO_ADDRESS_INFO_NONE);
2969 plugin->env->notify_address (plugin->env->cls,
2972 GNUNET_free (ext_addr);
2978 * Configure the plugin
2980 * @param plugin plugin handle
2981 * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
2984 server_configure_plugin (struct HTTP_Server_Plugin *plugin)
2986 unsigned long long port;
2987 unsigned long long max_connections;
2988 char *bind4_address = NULL;
2989 char *bind6_address = NULL;
2990 char *eh_tmp = NULL;
2991 int external_hostname_use_port;
2994 if (GNUNET_CONFIGURATION_have_value
2995 (plugin->env->cfg, plugin->name, "USE_IPv4"))
2998 GNUNET_CONFIGURATION_get_value_yesno (plugin->env->cfg,
3003 plugin->use_ipv4 = GNUNET_YES;
3004 LOG (GNUNET_ERROR_TYPE_DEBUG,
3005 _("IPv4 support is %s\n"),
3006 (plugin->use_ipv4 == GNUNET_YES) ? "enabled" : "disabled");
3009 if (GNUNET_CONFIGURATION_have_value
3010 (plugin->env->cfg, plugin->name, "USE_IPv6"))
3013 GNUNET_CONFIGURATION_get_value_yesno (plugin->env->cfg,
3018 plugin->use_ipv6 = GNUNET_YES;
3019 LOG (GNUNET_ERROR_TYPE_DEBUG,
3020 _("IPv6 support is %s\n"),
3021 (plugin->use_ipv6 == GNUNET_YES) ? "enabled" : "disabled");
3023 if ((plugin->use_ipv4 == GNUNET_NO) && (plugin->use_ipv6 == GNUNET_NO))
3025 LOG (GNUNET_ERROR_TYPE_ERROR,
3026 _("Neither IPv4 nor IPv6 are enabled! Fix in configuration\n"));
3027 return GNUNET_SYSERR;
3030 /* Reading port number from config file */
3032 GNUNET_CONFIGURATION_get_value_number (plugin->env->cfg,
3034 "PORT", &port)) || (port > 65535))
3036 LOG (GNUNET_ERROR_TYPE_ERROR,
3037 _("Port is required! Fix in configuration\n"));
3038 return GNUNET_SYSERR;
3040 plugin->port = port;
3042 LOG (GNUNET_ERROR_TYPE_INFO,
3043 _("Using port %u\n"), plugin->port);
3045 if ( (plugin->use_ipv4 == GNUNET_YES) &&
3047 GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg,
3052 LOG (GNUNET_ERROR_TYPE_DEBUG,
3053 "Binding %s plugin to specific IPv4 address: `%s'\n",
3056 plugin->server_addr_v4 = GNUNET_new (struct sockaddr_in);
3057 if (1 != inet_pton (AF_INET,
3059 &plugin->server_addr_v4->sin_addr))
3061 LOG (GNUNET_ERROR_TYPE_ERROR,
3062 _("Specific IPv4 address `%s' in configuration file is invalid!\n"),
3064 GNUNET_free (bind4_address);
3065 GNUNET_free (plugin->server_addr_v4);
3066 plugin->server_addr_v4 = NULL;
3067 return GNUNET_SYSERR;
3071 LOG (GNUNET_ERROR_TYPE_DEBUG,
3072 "Binding to IPv4 address %s\n",
3074 plugin->server_addr_v4->sin_family = AF_INET;
3075 plugin->server_addr_v4->sin_port = htons (plugin->port);
3077 GNUNET_free (bind4_address);
3080 if ((plugin->use_ipv6 == GNUNET_YES) &&
3082 GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg,
3087 LOG (GNUNET_ERROR_TYPE_DEBUG,
3088 "Binding %s plugin to specific IPv6 address: `%s'\n",
3089 plugin->protocol, bind6_address);
3090 plugin->server_addr_v6 = GNUNET_new (struct sockaddr_in6);
3092 inet_pton (AF_INET6,
3094 &plugin->server_addr_v6->sin6_addr))
3096 LOG (GNUNET_ERROR_TYPE_ERROR,
3097 _("Specific IPv6 address `%s' in configuration file is invalid!\n"),
3099 GNUNET_free (bind6_address);
3100 GNUNET_free (plugin->server_addr_v6);
3101 plugin->server_addr_v6 = NULL;
3102 return GNUNET_SYSERR;
3106 LOG (GNUNET_ERROR_TYPE_DEBUG,
3107 "Binding to IPv6 address %s\n",
3109 plugin->server_addr_v6->sin6_family = AF_INET6;
3110 plugin->server_addr_v6->sin6_port = htons (plugin->port);
3112 GNUNET_free (bind6_address);
3115 plugin->verify_external_hostname = GNUNET_NO;
3117 plugin->verify_external_hostname
3118 = GNUNET_CONFIGURATION_get_value_yesno (plugin->env->cfg,
3120 "VERIFY_EXTERNAL_HOSTNAME");
3121 if (GNUNET_SYSERR == plugin->verify_external_hostname)
3122 plugin->verify_external_hostname = GNUNET_NO;
3123 if (GNUNET_YES == plugin->verify_external_hostname)
3124 plugin->options |= HTTP_OPTIONS_VERIFY_CERTIFICATE;
3126 external_hostname_use_port
3127 = GNUNET_CONFIGURATION_get_value_yesno (plugin->env->cfg,
3129 "EXTERNAL_HOSTNAME_USE_PORT");
3130 if (GNUNET_SYSERR == external_hostname_use_port)
3131 external_hostname_use_port = GNUNET_NO;
3135 GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg,
3137 "EXTERNAL_HOSTNAME",
3142 char *pos_url = NULL;
3144 if (NULL != strstr(eh_tmp, "://"))
3145 tmp = &strstr(eh_tmp, "://")[3];
3149 if (GNUNET_YES == external_hostname_use_port)
3151 if ( (strlen (tmp) > 1) && (NULL != (pos = strchr(tmp, '/'))) )
3155 GNUNET_asprintf (&plugin->external_hostname,
3162 GNUNET_asprintf (&plugin->external_hostname,
3168 plugin->external_hostname = GNUNET_strdup (tmp);
3169 GNUNET_free (eh_tmp);
3171 LOG (GNUNET_ERROR_TYPE_INFO,
3172 _("Using external hostname `%s'\n"),
3173 plugin->external_hostname);
3174 plugin->notify_ext_task = GNUNET_SCHEDULER_add_now (&server_notify_external_hostname,
3177 /* Use only configured external hostname */
3178 if (GNUNET_CONFIGURATION_have_value
3181 "EXTERNAL_HOSTNAME_ONLY"))
3183 plugin->external_only =
3184 GNUNET_CONFIGURATION_get_value_yesno (plugin->env->cfg,
3186 "EXTERNAL_HOSTNAME_ONLY");
3189 plugin->external_only = GNUNET_NO;
3191 if (GNUNET_YES == plugin->external_only)
3192 LOG (GNUNET_ERROR_TYPE_DEBUG,
3193 _("Notifying transport only about hostname `%s'\n"),
3194 plugin->external_hostname);
3197 LOG (GNUNET_ERROR_TYPE_DEBUG,
3198 "No external hostname configured\n");
3200 /* Optional parameters */
3202 GNUNET_CONFIGURATION_get_value_number (plugin->env->cfg,
3206 max_connections = 128;
3207 plugin->max_request = max_connections;
3209 LOG (GNUNET_ERROR_TYPE_DEBUG,
3210 _("Maximum number of connections is %u\n"),
3211 plugin->max_request);
3213 plugin->peer_id_length = strlen (GNUNET_i2s_full (plugin->env->my_identity));
3220 * Exit point from the plugin.
3226 LIBGNUNET_PLUGIN_TRANSPORT_DONE (void *cls)
3228 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
3229 struct HTTP_Server_Plugin *plugin = api->cls;
3231 if (NULL == api->cls)
3233 /* Free for stub mode */
3237 plugin->in_shutdown = GNUNET_YES;
3238 LOG (GNUNET_ERROR_TYPE_INFO,
3239 _("Shutting down plugin `%s'\n"),
3242 if (NULL != plugin->notify_ext_task)
3244 GNUNET_SCHEDULER_cancel (plugin->notify_ext_task);
3245 plugin->notify_ext_task = NULL;
3248 if (NULL != plugin->ext_addr)
3250 LOG (GNUNET_ERROR_TYPE_DEBUG,
3251 "Notifying transport to remove address `%s'\n",
3252 http_common_plugin_address_to_string (plugin->protocol,
3253 plugin->ext_addr->address,
3254 plugin->ext_addr->address_length));
3256 plugin->env->notify_address (plugin->env->cls,
3260 plugin->env->notify_address (plugin->env->cls,
3264 GNUNET_HELLO_address_free (plugin->ext_addr);
3265 plugin->ext_addr = NULL;
3268 /* Stop to report addresses to transport service */
3269 server_stop_report_addresses (plugin);
3270 if (NULL != plugin->server_v4_task)
3272 GNUNET_SCHEDULER_cancel (plugin->server_v4_task);
3273 plugin->server_v4_task = NULL;
3276 if (NULL != plugin->server_v6_task)
3278 GNUNET_SCHEDULER_cancel (plugin->server_v6_task);
3279 plugin->server_v6_task = NULL;
3282 GNUNET_free_non_null (plugin->crypto_init);
3283 GNUNET_free_non_null (plugin->cert);
3284 GNUNET_free_non_null (plugin->key);
3286 GNUNET_CONTAINER_multipeermap_iterate (plugin->sessions,
3287 &destroy_session_shutdown_cb,
3289 GNUNET_CONTAINER_multipeermap_destroy (plugin->sessions);
3290 plugin->sessions = NULL;
3291 if (NULL != plugin->server_v4)
3293 MHD_stop_daemon (plugin->server_v4);
3294 plugin->server_v4 = NULL;
3296 if (NULL != plugin->server_v6)
3298 MHD_stop_daemon (plugin->server_v6);
3299 plugin->server_v6 = NULL;
3302 GNUNET_free_non_null (plugin->external_hostname);
3303 GNUNET_free_non_null (plugin->ext_addr);
3304 GNUNET_free_non_null (plugin->server_addr_v4);
3305 GNUNET_free_non_null (plugin->server_addr_v6);
3306 regfree (&plugin->url_regex);
3308 LOG (GNUNET_ERROR_TYPE_DEBUG,
3309 _("Shutdown for plugin `%s' complete\n"),
3312 GNUNET_free (plugin);
3319 * Function called for a quick conversion of the binary address to
3320 * a numeric address. Note that the caller must not free the
3321 * address and that the next call to this function is allowed
3322 * to override the address again.
3325 * @param addr binary address
3326 * @param addrlen length of the address
3327 * @return string representing the same address
3330 http_server_plugin_address_to_string (void *cls,
3334 return http_common_plugin_address_to_string (PLUGIN_NAME,
3341 * Function obtain the network type for a session
3343 * @param cls closure (`struct HTTP_Server_Plugin *`)
3344 * @param session the session
3345 * @return the network type in HBO or #GNUNET_SYSERR
3347 static enum GNUNET_NetworkType
3348 http_server_plugin_get_network (void *cls,
3349 struct GNUNET_ATS_Session *session)
3351 return session->scope;
3356 * Function obtain the network type for an address.
3358 * @param cls closure (`struct Plugin *`)
3359 * @param address the address
3360 * @return the network type
3362 static enum GNUNET_NetworkType
3363 http_server_plugin_get_network_for_address (void *cls,
3364 const struct GNUNET_HELLO_Address *address)
3366 struct HTTP_Server_Plugin *plugin = cls;
3368 return http_common_get_network_for_address (plugin->env,
3374 * Function that will be called whenever the transport service wants to
3375 * notify the plugin that the inbound quota changed and that the plugin
3376 * should update it's delay for the next receive value
3378 * @param cls closure
3379 * @param peer which peer was the session for
3380 * @param session which session is being updated
3381 * @param delay new delay to use for receiving
3384 http_server_plugin_update_inbound_delay (void *cls,
3385 const struct GNUNET_PeerIdentity *peer,
3386 struct GNUNET_ATS_Session *session,
3387 struct GNUNET_TIME_Relative delay)
3389 session->next_receive = GNUNET_TIME_relative_to_absolute (delay);
3390 LOG (GNUNET_ERROR_TYPE_DEBUG,
3391 "New inbound delay %s\n",
3392 GNUNET_STRINGS_relative_time_to_string (delay,
3394 if (NULL != session->recv_wakeup_task)
3396 GNUNET_SCHEDULER_cancel (session->recv_wakeup_task);
3397 session->recv_wakeup_task
3398 = GNUNET_SCHEDULER_add_delayed (delay,
3406 * Return information about the given session to the
3409 * @param cls the `struct Plugin` with the monitor callback (`sic`)
3410 * @param peer peer we send information about
3411 * @param value our `struct GNUNET_ATS_Session` to send information about
3412 * @return #GNUNET_OK (continue to iterate)
3415 send_session_info_iter (void *cls,
3416 const struct GNUNET_PeerIdentity *peer,
3419 struct HTTP_Server_Plugin *plugin = cls;
3420 struct GNUNET_ATS_Session *session = value;
3422 notify_session_monitor (plugin,
3424 GNUNET_TRANSPORT_SS_INIT);
3430 * Begin monitoring sessions of a plugin. There can only
3431 * be one active monitor per plugin (i.e. if there are
3432 * multiple monitors, the transport service needs to
3433 * multiplex the generated events over all of them).
3435 * @param cls closure of the plugin
3436 * @param sic callback to invoke, NULL to disable monitor;
3437 * plugin will being by iterating over all active
3438 * sessions immediately and then enter monitor mode
3439 * @param sic_cls closure for @a sic
3442 http_server_plugin_setup_monitor (void *cls,
3443 GNUNET_TRANSPORT_SessionInfoCallback sic,
3446 struct HTTP_Server_Plugin *plugin = cls;
3449 plugin->sic_cls = sic_cls;
3452 GNUNET_CONTAINER_multipeermap_iterate (plugin->sessions,
3453 &send_session_info_iter,
3455 /* signal end of first iteration */
3456 sic (sic_cls, NULL, NULL);
3462 * Entry point for the plugin.
3468 LIBGNUNET_PLUGIN_TRANSPORT_INIT (void *cls)
3470 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
3471 struct GNUNET_TRANSPORT_PluginFunctions *api;
3472 struct HTTP_Server_Plugin *plugin;
3474 if (NULL == env->receive)
3476 /* run in 'stub' mode (i.e. as part of gnunet-peerinfo), don't fully
3477 initialze the plugin or the API */
3478 api = GNUNET_new (struct GNUNET_TRANSPORT_PluginFunctions);
3480 api->address_to_string = &http_server_plugin_address_to_string;
3481 api->string_to_address = &http_common_plugin_string_to_address;
3482 api->address_pretty_printer = &http_common_plugin_address_pretty_printer;
3485 plugin = GNUNET_new (struct HTTP_Server_Plugin);
3487 plugin->sessions = GNUNET_CONTAINER_multipeermap_create (128,
3490 api = GNUNET_new (struct GNUNET_TRANSPORT_PluginFunctions);
3492 api->send = &http_server_plugin_send;
3493 api->disconnect_peer = &http_server_plugin_disconnect_peer;
3494 api->disconnect_session = &http_server_plugin_disconnect_session;
3495 api->query_keepalive_factor = &http_server_query_keepalive_factor;
3496 api->check_address = &http_server_plugin_address_suggested;
3497 api->get_session = &http_server_plugin_get_session;
3499 api->address_to_string = &http_server_plugin_address_to_string;
3500 api->string_to_address = &http_common_plugin_string_to_address;
3501 api->address_pretty_printer = &http_common_plugin_address_pretty_printer;
3502 api->get_network = &http_server_plugin_get_network;
3503 api->get_network_for_address = &http_server_plugin_get_network_for_address;
3504 api->update_session_timeout = &http_server_plugin_update_session_timeout;
3505 api->update_inbound_delay = &http_server_plugin_update_inbound_delay;
3506 api->setup_monitor = &http_server_plugin_setup_monitor;
3508 plugin->name = "transport-https_server";
3509 plugin->protocol = "https";
3511 plugin->name = "transport-http_server";
3512 plugin->protocol = "http";
3516 GNUNET_CONFIGURATION_get_value_yesno (env->cfg,
3521 plugin->options |= HTTP_OPTIONS_TCP_STEALTH;
3523 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3524 _("TCP_STEALTH not supported on this platform.\n"));
3525 LIBGNUNET_PLUGIN_TRANSPORT_DONE (api);
3530 /* Compile URL regex */
3531 if (regcomp (&plugin->url_regex,
3535 LOG (GNUNET_ERROR_TYPE_ERROR,
3536 _("Unable to compile URL regex\n"));
3537 LIBGNUNET_PLUGIN_TRANSPORT_DONE (api);
3541 /* Configure plugin */
3542 if (GNUNET_SYSERR == server_configure_plugin (plugin))
3544 LIBGNUNET_PLUGIN_TRANSPORT_DONE (api);
3548 /* Check IPv6 support */
3549 if (GNUNET_YES == plugin->use_ipv6)
3550 plugin->use_ipv6 = server_check_ipv6_support (plugin);
3552 /* Report addresses to transport service */
3553 if (GNUNET_NO == plugin->external_only)
3554 server_start_report_addresses (plugin);
3556 if (GNUNET_SYSERR == server_start (plugin))
3558 LIBGNUNET_PLUGIN_TRANSPORT_DONE (api);
3564 /* end of plugin_transport_http_server.c */