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
99 * Wrapper to manage addresses
101 struct HttpAddressWrapper
106 struct HttpAddressWrapper *next;
109 * Linked list previous
111 struct HttpAddressWrapper *prev;
114 * An address we are using.
116 struct HttpAddress *address;
119 * Length of the address.
126 * Message to send using http
131 * next pointer for double linked list
133 struct HTTP_Message *next;
136 * previous pointer for double linked list
138 struct HTTP_Message *prev;
141 * buffer containing data to send
146 * amount of data already sent
156 * HTTP/S specific overhead
161 * Continuation function to call once the transmission buffer
162 * has again space available. NULL if there is no
163 * continuation to call.
165 GNUNET_TRANSPORT_TransmitContinuation transmit_cont;
168 * Closure for transmit_cont.
170 void *transmit_cont_cls;
175 * Session handle for connections.
177 struct GNUNET_ATS_Session
181 * To whom are we talking to (set to our identity
182 * if we are still waiting for the welcome message)
184 struct GNUNET_PeerIdentity target;
187 * Pointer to the global plugin struct.
189 struct HTTP_Server_Plugin *plugin;
192 * next pointer for double linked list
194 struct HTTP_Message *msg_head;
197 * previous pointer for double linked list
199 struct HTTP_Message *msg_tail;
202 * Message stream tokenizer for incoming data
204 struct GNUNET_MessageStreamTokenizer *msg_tk;
209 struct ServerRequest *server_recv;
214 struct ServerRequest *server_send;
219 struct GNUNET_HELLO_Address *address;
222 * Absolute time when to receive data again
223 * Used for receive throttling
225 struct GNUNET_TIME_Absolute next_receive;
228 * Absolute time when this connection will time out.
230 struct GNUNET_TIME_Absolute timeout;
233 * Session timeout task
235 struct GNUNET_SCHEDULER_Task * timeout_task;
238 * Task to resume MHD handling when receiving is allowed again
240 struct GNUNET_SCHEDULER_Task * recv_wakeup_task;
243 * Number of bytes waiting for transmission to this peer.
245 unsigned long long bytes_in_queue;
248 * Number of messages waiting for transmission to this peer.
250 unsigned int msgs_in_queue;
253 * Unique HTTP/S connection tag for this connection
260 enum GNUNET_ATS_Network_Type scope;
263 * #GNUNET_YES if this session is known to the service.
265 int known_to_service;
271 * Encapsulation of all of the state of the plugin.
273 struct HTTP_Server_Plugin
278 struct GNUNET_TRANSPORT_PluginEnvironment *env;
281 * Hash map of open sessions.
283 struct GNUNET_CONTAINER_MultiPeerMap *sessions;
286 * Function to call about session status changes.
288 GNUNET_TRANSPORT_SessionInfoCallback sic;
291 * Closure for @e sic.
308 char *external_hostname;
311 * External hostname the plugin can be connected to, can be different to
312 * the host's FQDN, used e.g. for reverse proxying
314 struct GNUNET_HELLO_Address *ext_addr;
317 * NAT handle & address management
319 struct GNUNET_NAT_Handle *nat;
322 * IPv4 addresses DLL head
324 struct HttpAddressWrapper *addr_head;
327 * IPv4 addresses DLL tail
329 struct HttpAddressWrapper *addr_tail;
332 * IPv4 server socket to bind to
334 struct sockaddr_in *server_addr_v4;
337 * IPv6 server socket to bind to
339 struct sockaddr_in6 *server_addr_v6;
344 struct MHD_Daemon *server_v4;
349 struct MHD_Daemon *server_v6;
357 * Use RC4-128 instead of AES:
358 * NONE:+VERS-TLS1.0:+ARCFOUR-128:+SHA1:+RSA:+COMP-NULL
377 struct GNUNET_SCHEDULER_Task *server_v4_task;
382 struct GNUNET_SCHEDULER_Task *server_v6_task;
385 * Task calling transport service about external address
387 struct GNUNET_SCHEDULER_Task *notify_ext_task;
390 * Notify transport only about external address
392 unsigned int external_only;
395 * The IPv4 server is scheduled to run asap
397 int server_v4_immediately;
400 * The IPv6 server is scheduled to run asap
402 int server_v6_immediately;
405 * Verify external address
407 int verify_external_hostname;
410 * Maximum number of sockets the plugin can use
411 * Each http request /request connections are two connections
413 unsigned int max_request;
416 * Current number of sockets the plugin can use
417 * Each http connection are two requests
419 unsigned int cur_request;
422 * Did we immediately end the session in disconnect_cb
432 * My options to be included in the address
452 * Regex for parsing URLs. FIXME: this seems overkill.
460 * If a session monitor is attached, notify it about the new
463 * @param plugin our plugin
464 * @param session session that changed state
465 * @param state new state of the session
468 notify_session_monitor (struct HTTP_Server_Plugin *plugin,
469 struct GNUNET_ATS_Session *session,
470 enum GNUNET_TRANSPORT_SessionState state)
472 struct GNUNET_TRANSPORT_SessionInfo info;
474 if (NULL == plugin->sic)
476 memset (&info, 0, sizeof (info));
478 info.is_inbound = GNUNET_YES;
479 info.num_msg_pending = session->msgs_in_queue;
480 info.num_bytes_pending = session->bytes_in_queue;
481 info.receive_delay = session->next_receive;
482 info.session_timeout = session->timeout;
483 info.address = session->address;
484 plugin->sic (plugin->sic_cls,
491 * Wake up an MHD connection which was suspended
493 * @param cls the session
496 server_wake_up (void *cls)
498 struct GNUNET_ATS_Session *s = cls;
500 s->recv_wakeup_task = NULL;
501 LOG (GNUNET_ERROR_TYPE_DEBUG,
502 "Session %p: Waking up PUT handle\n",
504 MHD_resume_connection (s->server_recv->mhd_conn);
509 * Reschedule the execution of both IPv4 and IPv6 server.
511 * @param plugin the plugin
512 * @param server which server to schedule v4 or v6?
513 * @param now #GNUNET_YES to schedule execution immediately, #GNUNET_NO to wait
517 server_reschedule (struct HTTP_Server_Plugin *plugin,
518 struct MHD_Daemon *server,
523 * Deletes the session. Must not be used afterwards.
525 * @param s the session to delete
528 server_delete_session (struct GNUNET_ATS_Session *s)
530 struct HTTP_Server_Plugin *plugin = s->plugin;
531 struct HTTP_Message *msg;
533 if (NULL != s->timeout_task)
535 GNUNET_SCHEDULER_cancel (s->timeout_task);
536 s->timeout_task = NULL;
537 s->timeout = GNUNET_TIME_UNIT_ZERO_ABS;
539 if (NULL != s->recv_wakeup_task)
541 GNUNET_SCHEDULER_cancel (s->recv_wakeup_task);
542 s->recv_wakeup_task = NULL;
543 if (NULL != s->server_recv)
544 MHD_resume_connection (s->server_recv->mhd_conn);
546 GNUNET_assert (GNUNET_OK ==
547 GNUNET_CONTAINER_multipeermap_remove (plugin->sessions,
550 while (NULL != (msg = s->msg_head))
552 GNUNET_CONTAINER_DLL_remove (s->msg_head,
555 if (NULL != msg->transmit_cont)
556 msg->transmit_cont (msg->transmit_cont_cls,
560 msg->pos + msg->overhead);
561 GNUNET_assert (s->msgs_in_queue > 0);
563 GNUNET_assert (s->bytes_in_queue >= msg->size);
564 s->bytes_in_queue -= msg->size;
568 GNUNET_assert (0 == s->msgs_in_queue);
569 GNUNET_assert (0 == s->bytes_in_queue);
571 if (NULL != s->server_send)
573 LOG (GNUNET_ERROR_TYPE_DEBUG,
574 "Server: %p / %p Terminating inbound PUT session to peer `%s'\n",
576 GNUNET_i2s (&s->target));
577 s->server_send->session = NULL;
578 MHD_set_connection_option (s->server_send->mhd_conn,
579 MHD_CONNECTION_OPTION_TIMEOUT,
580 1 /* 0 = no timeout, so this is MIN */);
581 server_reschedule (plugin,
582 s->server_send->mhd_daemon,
586 if (NULL != s->server_recv)
588 LOG (GNUNET_ERROR_TYPE_DEBUG,
589 "Server: %p / %p Terminating inbound GET session to peer `%s'\n",
590 s, s->server_recv, GNUNET_i2s (&s->target));
591 s->server_recv->session = NULL;
592 MHD_set_connection_option (s->server_recv->mhd_conn,
593 MHD_CONNECTION_OPTION_TIMEOUT,
594 1 /* 0 = no timeout, so this is MIN */);
595 server_reschedule (plugin,
596 s->server_recv->mhd_daemon,
599 notify_session_monitor (plugin,
601 GNUNET_TRANSPORT_SS_DONE);
602 if (GNUNET_YES == s->known_to_service)
604 plugin->env->session_end (plugin->env->cls,
607 s->known_to_service = GNUNET_NO;
609 if (NULL != s->msg_tk)
611 GNUNET_MST_destroy (s->msg_tk);
614 GNUNET_HELLO_address_free (s->address);
615 LOG (GNUNET_ERROR_TYPE_DEBUG,
616 "Session %p destroyed\n",
623 * Disconnect session @a s by telling MHD to close the
624 * connections (reducing timeout, etc.).
626 * @param cls closure with the `struct HTTP_Server_Plugin`
627 * @param s the session
628 * @return #GNUNET_OK on success
631 http_server_plugin_disconnect_session (void *cls,
632 struct GNUNET_ATS_Session *s)
634 server_delete_session (s);
640 * Session was idle, so disconnect it
642 * @param cls the session
645 server_session_timeout (void *cls)
647 struct GNUNET_ATS_Session *s = cls;
648 struct GNUNET_TIME_Relative left;
650 s->timeout_task = NULL;
651 left = GNUNET_TIME_absolute_get_remaining (s->timeout);
652 if (0 != left.rel_value_us)
654 /* not actually our turn yet, but let's at least update
655 the monitor, it may think we're about to die ... */
656 notify_session_monitor (s->plugin,
658 GNUNET_TRANSPORT_SS_UP);
659 s->timeout_task = GNUNET_SCHEDULER_add_delayed (left,
660 &server_session_timeout,
664 GNUNET_log (TIMEOUT_LOG,
665 "Session %p was idle for %s, disconnecting\n",
667 GNUNET_STRINGS_relative_time_to_string (HTTP_SERVER_SESSION_TIMEOUT,
669 server_delete_session (s);
674 * Increment session timeout due to activity session @a s
676 * @param s the session
679 server_reschedule_session_timeout (struct GNUNET_ATS_Session *s)
681 GNUNET_assert (NULL != s->timeout_task);
682 s->timeout = GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
687 * Function that can be used by the transport service to transmit
688 * a message using the plugin. Note that in the case of a
689 * peer disconnecting, the continuation MUST be called
690 * prior to the disconnect notification itself. This function
691 * will be called with this peer's HELLO message to initiate
692 * a fresh connection to another peer.
695 * @param session which session must be used
696 * @param msgbuf the message to transmit
697 * @param msgbuf_size number of bytes in @a msgbuf
698 * @param priority how important is the message (most plugins will
699 * ignore message priority and just FIFO)
700 * @param to how long to wait at most for the transmission (does not
701 * require plugins to discard the message after the timeout,
702 * just advisory for the desired delay; most plugins will ignore
704 * @param cont continuation to call once the message has
705 * been transmitted (or if the transport is ready
706 * for the next transmission call; or if the
707 * peer disconnected...); can be NULL
708 * @param cont_cls closure for @a cont
709 * @return number of bytes used (on the physical network, with overheads);
710 * -1 on hard errors (i.e. address invalid); 0 is a legal value
711 * and does NOT mean that the message was not transmitted (DV)
714 http_server_plugin_send (void *cls,
715 struct GNUNET_ATS_Session *session,
718 unsigned int priority,
719 struct GNUNET_TIME_Relative to,
720 GNUNET_TRANSPORT_TransmitContinuation cont,
723 struct HTTP_Server_Plugin *plugin = cls;
724 struct HTTP_Message *msg;
725 ssize_t bytes_sent = 0;
728 LOG (GNUNET_ERROR_TYPE_DEBUG,
729 "Session %p/request %p: Sending message with %u to peer `%s'\n",
731 session->server_send,
733 GNUNET_i2s (&session->target));
735 /* create new message and schedule */
736 bytes_sent = sizeof (struct HTTP_Message) + msgbuf_size;
737 msg = GNUNET_malloc (bytes_sent);
739 msg->size = msgbuf_size;
741 msg->buf = (char *) &msg[1];
742 msg->transmit_cont = cont;
743 msg->transmit_cont_cls = cont_cls;
744 GNUNET_memcpy (msg->buf,
747 GNUNET_CONTAINER_DLL_insert_tail (session->msg_head,
750 session->msgs_in_queue++;
751 session->bytes_in_queue += msg->size;
752 notify_session_monitor (plugin,
754 GNUNET_TRANSPORT_SS_UP);
755 GNUNET_asprintf (&stat_txt,
756 "# bytes currently in %s_server buffers",
758 GNUNET_STATISTICS_update (plugin->env->stats,
759 stat_txt, msgbuf_size, GNUNET_NO);
760 GNUNET_free (stat_txt);
762 if (NULL != session->server_send)
763 server_reschedule (session->plugin,
764 session->server_send->mhd_daemon,
771 * Terminate session during shutdown.
773 * @param cls the `struct HTTP_Server_Plugin *`
774 * @param peer for which this is a session
775 * @param value the `struct GNUNET_ATS_Session` to clean up
776 * @return #GNUNET_OK (continue to iterate)
779 destroy_session_shutdown_cb (void *cls,
780 const struct GNUNET_PeerIdentity *peer,
783 struct GNUNET_ATS_Session *s = value;
784 struct ServerRequest *sc_send;
785 struct ServerRequest *sc_recv;
787 sc_send = s->server_send;
788 sc_recv = s->server_recv;
789 server_delete_session (s);
791 sc_send->session = NULL;
793 sc_recv->session = NULL;
802 * @param cls the `struct HTTP_Server_Plugin *`
803 * @param peer for which this is a session
804 * @param value the `struct GNUNET_ATS_Session` to clean up
805 * @return #GNUNET_OK (continue to iterate)
808 destroy_session_cb (void *cls,
809 const struct GNUNET_PeerIdentity *peer,
812 struct GNUNET_ATS_Session *s = value;
814 server_delete_session (s);
820 * Function that can be used to force the plugin to disconnect
821 * from the given peer and cancel all previous transmissions
822 * (and their continuationc).
825 * @param target peer from which to disconnect
828 http_server_plugin_disconnect_peer (void *cls,
829 const struct GNUNET_PeerIdentity *target)
831 struct HTTP_Server_Plugin *plugin = cls;
833 LOG (GNUNET_ERROR_TYPE_DEBUG,
834 "Transport tells me to disconnect `%s'\n",
835 GNUNET_i2s (target));
836 GNUNET_CONTAINER_multipeermap_get_multiple (plugin->sessions,
844 * Another peer has suggested an address for this
845 * peer and transport plugin. Check that this could be a valid
846 * address. If so, consider adding it to the list
850 * @param addr pointer to the address
851 * @param addrlen length of @a addr
852 * @return #GNUNET_OK if this is a plausible address for this peer
856 http_server_plugin_address_suggested (void *cls,
860 struct HTTP_Server_Plugin *plugin = cls;
861 struct HttpAddressWrapper *next;
862 struct HttpAddressWrapper *pos;
863 const struct HttpAddress *haddr = addr;
865 if ((NULL != plugin->ext_addr) &&
866 GNUNET_YES == (http_common_cmp_addresses (addr, addrlen,
867 plugin->ext_addr->address,
868 plugin->ext_addr->address_length)))
870 /* Checking HTTP_OPTIONS_VERIFY_CERTIFICATE option for external hostname */
871 if ((ntohl (haddr->options) & HTTP_OPTIONS_VERIFY_CERTIFICATE) !=
872 (plugin->options & HTTP_OPTIONS_VERIFY_CERTIFICATE))
873 return GNUNET_NO; /* VERIFY option not set as required! */
876 next = plugin->addr_head;
877 while (NULL != (pos = next))
880 if (GNUNET_YES == (http_common_cmp_addresses(addr,
891 * Creates a new outbound session the transport
892 * service will use to send data to the peer.
894 * Since HTTP/S server cannot create sessions, always returns NULL.
896 * @param cls the plugin
897 * @param address the address
898 * @return always NULL
900 static struct GNUNET_ATS_Session *
901 http_server_plugin_get_session (void *cls,
902 const struct GNUNET_HELLO_Address *address)
909 * Call MHD IPv4 to process pending requests and then go back
910 * and schedule the next run.
912 * @param cls plugin as closure
915 server_v4_run (void *cls)
917 struct HTTP_Server_Plugin *plugin = cls;
919 plugin->server_v4_task = NULL;
920 plugin->server_v4_immediately = GNUNET_NO;
921 GNUNET_assert (MHD_YES == MHD_run (plugin->server_v4));
922 server_reschedule (plugin, plugin->server_v4, GNUNET_NO);
927 * Call MHD IPv6 to process pending requests and then go back
928 * and schedule the next run.
930 * @param cls plugin as closure
933 server_v6_run (void *cls)
935 struct HTTP_Server_Plugin *plugin = cls;
937 plugin->server_v6_task = NULL;
938 plugin->server_v6_immediately = GNUNET_NO;
939 GNUNET_assert (MHD_YES == MHD_run (plugin->server_v6));
940 server_reschedule (plugin, plugin->server_v6, GNUNET_NO);
945 * Function that queries MHD's select sets and
946 * starts the task waiting for them.
948 * @param plugin plugin
949 * @param daemon_handle the MHD daemon handle
950 * @param now schedule now
951 * @return gnunet task identifier
953 static struct GNUNET_SCHEDULER_Task *
954 server_schedule (struct HTTP_Server_Plugin *plugin,
955 struct MHD_Daemon *daemon_handle,
958 struct GNUNET_SCHEDULER_Task * ret;
962 struct GNUNET_NETWORK_FDSet *wrs;
963 struct GNUNET_NETWORK_FDSet *wws;
965 MHD_UNSIGNED_LONG_LONG timeout;
966 static unsigned long long last_timeout = 0;
968 struct GNUNET_TIME_Relative tv;
970 if (GNUNET_YES == plugin->in_shutdown)
977 wrs = GNUNET_NETWORK_fdset_create ();
978 wws = GNUNET_NETWORK_fdset_create ();
980 GNUNET_assert (MHD_YES ==
981 MHD_get_fdset (daemon_handle,
986 haveto = MHD_get_timeout (daemon_handle, &timeout);
987 if (haveto == MHD_YES)
989 if (timeout != last_timeout)
991 LOG (GNUNET_ERROR_TYPE_DEBUG,
992 "SELECT Timeout changed from %llu to %llu (ms)\n",
993 last_timeout, timeout);
994 last_timeout = timeout;
996 if (timeout <= GNUNET_TIME_UNIT_SECONDS.rel_value_us / 1000LL)
997 tv.rel_value_us = (uint64_t) timeout * 1000LL;
999 tv = GNUNET_TIME_UNIT_SECONDS;
1002 tv = GNUNET_TIME_UNIT_SECONDS;
1003 /* Force immediate run, since we have outbound data to send */
1004 if (now == GNUNET_YES)
1005 tv = GNUNET_TIME_UNIT_MILLISECONDS;
1006 GNUNET_NETWORK_fdset_copy_native (wrs, &rs, max + 1);
1007 GNUNET_NETWORK_fdset_copy_native (wws, &ws, max + 1);
1009 if (daemon_handle == plugin->server_v4)
1011 if (plugin->server_v4_task != NULL)
1013 GNUNET_SCHEDULER_cancel (plugin->server_v4_task);
1014 plugin->server_v4_task = NULL;
1017 LOG (GNUNET_ERROR_TYPE_DEBUG,
1018 "Scheduling IPv4 server task in %llu ms\n",
1022 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1024 &server_v4_run, plugin);
1026 if (daemon_handle == plugin->server_v6)
1028 if (plugin->server_v6_task != NULL)
1030 GNUNET_SCHEDULER_cancel (plugin->server_v6_task);
1031 plugin->server_v6_task = NULL;
1034 LOG (GNUNET_ERROR_TYPE_DEBUG,
1035 "Scheduling IPv6 server task in %llu ms\n", tv);
1038 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1040 &server_v6_run, plugin);
1042 GNUNET_NETWORK_fdset_destroy (wrs);
1043 GNUNET_NETWORK_fdset_destroy (wws);
1049 * Reschedule the execution of both IPv4 and IPv6 server
1051 * @param plugin the plugin
1052 * @param server which server to schedule v4 or v6?
1053 * @param now #GNUNET_YES to schedule execution immediately, #GNUNET_NO to wait
1057 server_reschedule (struct HTTP_Server_Plugin *plugin,
1058 struct MHD_Daemon *server,
1061 if ((server == plugin->server_v4) && (plugin->server_v4 != NULL))
1063 if (GNUNET_YES == plugin->server_v4_immediately)
1064 return; /* No rescheduling, server will run asap */
1066 if (GNUNET_YES == now)
1067 plugin->server_v4_immediately = GNUNET_YES;
1069 if (plugin->server_v4_task != NULL)
1071 GNUNET_SCHEDULER_cancel (plugin->server_v4_task);
1072 plugin->server_v4_task = NULL;
1074 plugin->server_v4_task = server_schedule (plugin, plugin->server_v4, now);
1077 if ((server == plugin->server_v6) && (plugin->server_v6 != NULL))
1079 if (GNUNET_YES == plugin->server_v6_immediately)
1080 return; /* No rescheduling, server will run asap */
1082 if (GNUNET_YES == now)
1083 plugin->server_v6_immediately = GNUNET_YES;
1085 if (plugin->server_v6_task != NULL)
1087 GNUNET_SCHEDULER_cancel (plugin->server_v6_task);
1088 plugin->server_v6_task = NULL;
1090 plugin->server_v6_task = server_schedule (plugin, plugin->server_v6, now);
1096 * Function that is called to get the keepalive factor.
1097 * GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT is divided by this number to
1098 * calculate the interval between keepalive packets.
1100 * @param cls closure with the `struct HTTP_Server_Plugin`
1101 * @return keepalive factor
1104 http_server_query_keepalive_factor (void *cls)
1111 * Function that will be called whenever the transport service wants to
1112 * notify the plugin that a session is still active and in use and
1113 * therefore the session timeout for this session has to be updated
1115 * @param cls closure
1116 * @param peer which peer was the session for
1117 * @param session which session is being updated
1120 http_server_plugin_update_session_timeout (void *cls,
1121 const struct GNUNET_PeerIdentity *peer,
1122 struct GNUNET_ATS_Session *session)
1124 server_reschedule_session_timeout (session);
1129 * Tell MHD that the connection should timeout after @a to seconds.
1131 * @param plugin our plugin
1132 * @param s session for which the timeout changes
1133 * @param to timeout in seconds
1136 server_mhd_connection_timeout (struct HTTP_Server_Plugin *plugin,
1137 struct GNUNET_ATS_Session *s,
1140 /* Setting timeouts for other connections */
1141 if (NULL != s->server_recv)
1143 LOG (GNUNET_ERROR_TYPE_DEBUG,
1144 "Setting timeout for %p to %u sec.\n",
1145 s->server_recv, to);
1146 MHD_set_connection_option (s->server_recv->mhd_conn,
1147 MHD_CONNECTION_OPTION_TIMEOUT,
1149 server_reschedule (plugin, s->server_recv->mhd_daemon, GNUNET_NO);
1151 if (NULL != s->server_send)
1153 LOG (GNUNET_ERROR_TYPE_DEBUG,
1154 "Setting timeout for %p to %u sec.\n",
1155 s->server_send, to);
1156 MHD_set_connection_option (s->server_send->mhd_conn,
1157 MHD_CONNECTION_OPTION_TIMEOUT,
1159 server_reschedule (plugin, s->server_send->mhd_daemon, GNUNET_NO);
1165 * Parse incoming URL for tag and target
1167 * @param plugin plugin
1168 * @param url incoming url
1169 * @param target where to store the target
1170 * @param tag where to store the tag
1171 * @param options where to store the options
1172 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
1175 server_parse_url (struct HTTP_Server_Plugin *plugin,
1177 struct GNUNET_PeerIdentity *target,
1181 regmatch_t matches[4];
1182 const char *tag_start;
1183 const char *target_start;
1187 unsigned long int rc;
1191 ("^.*/([0-9A-Z]+);([0-9]+)(,[0-9]+)?$")
1196 return GNUNET_SYSERR;
1199 if (regexec(&plugin->url_regex, url, 4, matches, 0))
1201 LOG (GNUNET_ERROR_TYPE_DEBUG,
1202 "URL `%s' did not match regex\n", url);
1203 return GNUNET_SYSERR;
1206 target_start = &url[matches[1].rm_so];
1207 tag_start = &url[matches[2].rm_so];
1210 rc = strtoul (tag_start, &tag_end, 10);
1211 if (&url[matches[2].rm_eo] != tag_end)
1213 LOG (GNUNET_ERROR_TYPE_DEBUG,
1214 "URL tag did not line up with submatch\n");
1215 return GNUNET_SYSERR;
1219 LOG (GNUNET_ERROR_TYPE_DEBUG,
1220 "URL tag is zero\n");
1221 return GNUNET_SYSERR;
1223 if ((rc == ULONG_MAX) && (ERANGE == errno))
1225 LOG (GNUNET_ERROR_TYPE_DEBUG,
1226 "URL tag > ULONG_MAX\n");
1227 return GNUNET_SYSERR;
1229 if (rc > UINT32_MAX)
1231 LOG (GNUNET_ERROR_TYPE_DEBUG,
1232 "URL tag > UINT32_MAX\n");
1233 return GNUNET_SYSERR;
1235 (*tag) = (uint32_t)rc;
1236 LOG (GNUNET_ERROR_TYPE_DEBUG,
1237 "Found tag `%u' in url\n",
1240 /* convert peer id */
1241 hash_length = matches[1].rm_eo - matches[1].rm_so;
1242 if (hash_length != plugin->peer_id_length)
1244 LOG (GNUNET_ERROR_TYPE_DEBUG,
1245 "URL target is %u bytes, expecting %u\n",
1246 hash_length, plugin->peer_id_length);
1247 return GNUNET_SYSERR;
1250 GNUNET_CRYPTO_eddsa_public_key_from_string (target_start,
1252 &target->public_key))
1254 LOG (GNUNET_ERROR_TYPE_DEBUG,
1255 "URL target conversion failed\n");
1256 return GNUNET_SYSERR;
1258 LOG (GNUNET_ERROR_TYPE_DEBUG,
1259 "Found target `%s' in URL\n",
1260 GNUNET_i2s_full (target));
1262 /* convert options */
1263 if (-1 == matches[3].rm_so)
1269 rc = strtoul (&url[matches[3].rm_so + 1], &options_end, 10);
1270 if (&url[matches[3].rm_eo] != options_end)
1272 LOG (GNUNET_ERROR_TYPE_DEBUG,
1273 "URL options did not line up with submatch\n");
1274 return GNUNET_SYSERR;
1276 if ((rc == ULONG_MAX) && (ERANGE == errno))
1278 LOG (GNUNET_ERROR_TYPE_DEBUG,
1279 "URL options > ULONG_MAX\n");
1280 return GNUNET_SYSERR;
1282 if (rc > UINT32_MAX)
1284 LOG (GNUNET_ERROR_TYPE_DEBUG,
1285 "URL options > UINT32_MAX\n");
1286 return GNUNET_SYSERR;
1288 (*options) = (uint32_t) rc;
1289 LOG (GNUNET_ERROR_TYPE_DEBUG,
1290 "Found options `%u' in url\n",
1298 * Closure for #session_tag_it().
1300 struct GNUNET_ATS_SessionTagContext
1303 * Set to session matching the tag.
1305 struct GNUNET_ATS_Session *res;
1308 * Tag we are looking for.
1315 * Find a session with a matching tag.
1317 * @param cls the `struct GNUNET_ATS_SessionTagContext *`
1318 * @param key peer identity (unused)
1319 * @param value the `struct GNUNET_ATS_Session *`
1320 * @return #GNUNET_NO if we found the session, #GNUNET_OK if not
1323 session_tag_it (void *cls,
1324 const struct GNUNET_PeerIdentity *key,
1327 struct GNUNET_ATS_SessionTagContext *stc = cls;
1328 struct GNUNET_ATS_Session *s = value;
1330 if (s->tag == stc->tag)
1340 * Lookup a mhd connection and create one if none is found
1342 * @param plugin the plugin handle
1343 * @param mhd_connection the incoming mhd_connection
1344 * @param url incoming requested URL
1345 * @param method PUT or GET
1346 * @return the server connecetion
1348 static struct ServerRequest *
1349 server_lookup_connection (struct HTTP_Server_Plugin *plugin,
1350 struct MHD_Connection *mhd_connection,
1354 struct GNUNET_ATS_Session *s = NULL;
1355 struct ServerRequest *sc = NULL;
1356 const union MHD_ConnectionInfo *conn_info;
1357 struct HttpAddress *addr;
1358 struct GNUNET_PeerIdentity target;
1360 struct GNUNET_ATS_SessionTagContext stc;
1362 int direction = GNUNET_SYSERR;
1364 enum GNUNET_ATS_Network_Type scope;
1366 conn_info = MHD_get_connection_info (mhd_connection,
1367 MHD_CONNECTION_INFO_CLIENT_ADDRESS);
1368 if ((conn_info->client_addr->sa_family != AF_INET) &&
1369 (conn_info->client_addr->sa_family != AF_INET6))
1371 LOG (GNUNET_ERROR_TYPE_DEBUG,
1372 "New %s request from %s\n",
1376 options = 0; /* make gcc happy */
1377 if (GNUNET_SYSERR ==
1378 server_parse_url (plugin, url, &target, &stc.tag, &options))
1380 LOG (GNUNET_ERROR_TYPE_DEBUG,
1381 "Invalid url %s\n", url);
1384 if (0 == strcmp (MHD_HTTP_METHOD_PUT, method))
1385 direction = _RECEIVE;
1386 else if (0 == strcmp (MHD_HTTP_METHOD_GET, method))
1390 LOG (GNUNET_ERROR_TYPE_DEBUG,
1391 "Invalid method %s for request from %s\n",
1396 plugin->cur_request++;
1397 LOG (GNUNET_ERROR_TYPE_DEBUG,
1398 "New %s request from %s with tag %u (%u of %u)\n",
1400 GNUNET_i2s (&target),
1402 plugin->cur_request, plugin->max_request);
1403 /* find existing session */
1405 GNUNET_CONTAINER_multipeermap_get_multiple (plugin->sessions,
1409 if (NULL == (s = stc.res))
1411 /* create new session */
1413 switch (conn_info->client_addr->sa_family)
1416 addr = http_common_address_from_socket (plugin->protocol,
1417 conn_info->client_addr,
1418 sizeof (struct sockaddr_in));
1419 addr_len = http_common_address_get_size (addr);
1420 scope = plugin->env->get_address_type (plugin->env->cls,
1421 conn_info->client_addr,
1422 sizeof (struct sockaddr_in));
1425 addr = http_common_address_from_socket (plugin->protocol,
1426 conn_info->client_addr,
1427 sizeof (struct sockaddr_in6));
1428 addr_len = http_common_address_get_size (addr);
1429 scope = plugin->env->get_address_type (plugin->env->cls,
1430 conn_info->client_addr,
1431 sizeof (struct sockaddr_in6));
1434 /* external host name */
1437 s = GNUNET_new (struct GNUNET_ATS_Session);
1441 s->address = GNUNET_HELLO_address_allocate (&s->target,
1445 GNUNET_HELLO_ADDRESS_INFO_INBOUND);
1446 s->next_receive = GNUNET_TIME_UNIT_ZERO_ABS;
1448 s->timeout = GNUNET_TIME_relative_to_absolute (HTTP_SERVER_SESSION_TIMEOUT);
1449 s->timeout_task = GNUNET_SCHEDULER_add_delayed (HTTP_SERVER_SESSION_TIMEOUT,
1450 &server_session_timeout,
1452 (void) GNUNET_CONTAINER_multipeermap_put (plugin->sessions,
1455 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
1456 notify_session_monitor (plugin,
1458 GNUNET_TRANSPORT_SS_INIT);
1459 notify_session_monitor (plugin,
1461 GNUNET_TRANSPORT_SS_HANDSHAKE);
1462 LOG (GNUNET_ERROR_TYPE_DEBUG,
1463 "Creating new session %p for peer `%s' connecting from `%s'\n",
1464 s, GNUNET_i2s (&target),
1465 http_common_plugin_address_to_string (plugin->protocol,
1468 GNUNET_free_non_null (addr);
1471 if ( (_RECEIVE == direction) &&
1472 (NULL != s->server_recv) )
1474 LOG (GNUNET_ERROR_TYPE_DEBUG,
1475 "Duplicate PUT request from `%s' tag %u, dismissing new request\n",
1476 GNUNET_i2s (&target),
1480 if ((_SEND == direction) && (NULL != s->server_send))
1482 LOG (GNUNET_ERROR_TYPE_DEBUG,
1483 "Duplicate GET request from `%s' tag %u, dismissing new request\n",
1484 GNUNET_i2s (&target),
1488 sc = GNUNET_new (struct ServerRequest);
1489 if (conn_info->client_addr->sa_family == AF_INET)
1490 sc->mhd_daemon = plugin->server_v4;
1491 if (conn_info->client_addr->sa_family == AF_INET6)
1492 sc->mhd_daemon = plugin->server_v6;
1493 sc->mhd_conn = mhd_connection;
1494 sc->direction = direction;
1495 sc->connected = GNUNET_NO;
1497 sc->options = options;
1498 if (direction == _SEND)
1500 s->server_send = sc;
1502 if (direction == _RECEIVE)
1504 s->server_recv = sc;
1507 if ((GNUNET_NO == s->known_to_service) &&
1508 (NULL != s->server_send) &&
1509 (NULL != s->server_recv) )
1511 s->known_to_service = GNUNET_YES;
1512 notify_session_monitor (plugin,
1514 GNUNET_TRANSPORT_SS_UP);
1515 plugin->env->session_start (plugin->env->cls,
1521 to = (HTTP_SERVER_SESSION_TIMEOUT.rel_value_us / 1000LL / 1000LL);
1522 server_mhd_connection_timeout (plugin, s, to);
1528 * Callback called by MHD when it needs data to send
1530 * @param cls current session
1531 * @param pos position in buffer
1532 * @param buf the buffer to write data to
1533 * @param max max number of bytes available in @a buf
1534 * @return bytes written to @a buf
1537 server_send_callback (void *cls,
1542 struct ServerRequest *sc = cls;
1543 struct GNUNET_ATS_Session *s = sc->session;
1544 ssize_t bytes_read = 0;
1545 struct HTTP_Message *msg;
1550 /* session is disconnecting */
1554 sc = s->server_send;
1561 bytes_read = GNUNET_MIN (msg->size - msg->pos,
1563 GNUNET_memcpy (buf, &msg->buf[msg->pos], bytes_read);
1564 msg->pos += bytes_read;
1566 /* removing message */
1567 if (msg->pos == msg->size)
1569 GNUNET_CONTAINER_DLL_remove (s->msg_head,
1572 if (NULL != msg->transmit_cont)
1573 msg->transmit_cont (msg->transmit_cont_cls, &s->target, GNUNET_OK,
1574 msg->size, msg->size + msg->overhead);
1575 GNUNET_assert (s->msgs_in_queue > 0);
1577 GNUNET_assert (s->bytes_in_queue >= msg->size);
1578 s->bytes_in_queue -= msg->size;
1580 notify_session_monitor (s->plugin,
1582 GNUNET_TRANSPORT_SS_UPDATE);
1587 sc->connected = GNUNET_YES;
1588 LOG (GNUNET_ERROR_TYPE_DEBUG,
1589 "Sent %u bytes to peer `%s' with session %p \n",
1591 GNUNET_i2s (&s->target),
1593 GNUNET_asprintf (&stat_txt,
1594 "# bytes currently in %s_server buffers",
1595 s->plugin->protocol);
1596 GNUNET_STATISTICS_update (s->plugin->env->stats,
1600 GNUNET_free (stat_txt);
1601 GNUNET_asprintf (&stat_txt,
1602 "# bytes transmitted via %s_server",
1603 s->plugin->protocol);
1604 GNUNET_STATISTICS_update (s->plugin->env->stats,
1605 stat_txt, bytes_read, GNUNET_NO);
1606 GNUNET_free (stat_txt);
1608 else if ((sc->options & OPTION_LONG_POLL) && sc->connected)
1610 LOG (GNUNET_ERROR_TYPE_DEBUG,
1611 "Completing GET response to peer `%s' with session %p\n",
1612 GNUNET_i2s (&s->target),
1614 return MHD_CONTENT_READER_END_OF_STREAM;
1621 * Callback called by MessageStreamTokenizer when a message has arrived
1623 * @param cls current session as closure
1624 * @param message the message to be forwarded to transport service
1625 * @return #GNUNET_OK
1628 server_receive_mst_cb (void *cls,
1629 const struct GNUNET_MessageHeader *message)
1631 struct GNUNET_ATS_Session *s = cls;
1632 struct HTTP_Server_Plugin *plugin = s->plugin;
1633 struct GNUNET_TIME_Relative delay;
1636 if (GNUNET_NO == s->known_to_service)
1638 s->known_to_service = GNUNET_YES;
1639 plugin->env->session_start (plugin->env->cls,
1643 notify_session_monitor (plugin,
1645 GNUNET_TRANSPORT_SS_UP);
1647 delay = plugin->env->receive (plugin->env->cls,
1651 GNUNET_asprintf (&stat_txt,
1652 "# bytes received via %s_server",
1654 GNUNET_STATISTICS_update (plugin->env->stats,
1656 ntohs (message->size),
1658 GNUNET_free (stat_txt);
1659 s->next_receive = GNUNET_TIME_relative_to_absolute (delay);
1660 if (delay.rel_value_us > 0)
1662 LOG (GNUNET_ERROR_TYPE_DEBUG,
1663 "Peer `%s' address `%s' next read delayed for %s\n",
1664 GNUNET_i2s (&s->target),
1665 http_common_plugin_address_to_string (plugin->protocol,
1666 s->address->address,
1667 s->address->address_length),
1668 GNUNET_STRINGS_relative_time_to_string (delay,
1671 server_reschedule_session_timeout (s);
1677 * Add headers to a request indicating that we allow Cross-Origin Resource
1680 * @param response response object to modify
1683 add_cors_headers(struct MHD_Response *response)
1685 MHD_add_response_header (response,
1686 "Access-Control-Allow-Origin",
1688 MHD_add_response_header (response,
1689 "Access-Control-Allow-Methods",
1690 "GET, PUT, OPTIONS");
1691 MHD_add_response_header (response,
1692 "Access-Control-Max-Age",
1698 * MHD callback for a new incoming connection
1700 * @param cls the plugin handle
1701 * @param mhd_connection the mhd connection
1702 * @param url the requested URL
1703 * @param method GET or PUT
1704 * @param version HTTP version
1705 * @param upload_data upload data
1706 * @param upload_data_size size of @a upload_data
1707 * @param httpSessionCache the session cache to remember the connection
1708 * @return MHD_YES if connection is accepted, MHD_NO on reject
1711 server_access_cb (void *cls,
1712 struct MHD_Connection *mhd_connection,
1715 const char *version,
1716 const char *upload_data,
1717 size_t *upload_data_size,
1718 void **httpSessionCache)
1720 struct HTTP_Server_Plugin *plugin = cls;
1721 struct ServerRequest *sc = *httpSessionCache;
1722 struct GNUNET_ATS_Session *s;
1723 struct MHD_Response *response;
1726 LOG (GNUNET_ERROR_TYPE_DEBUG,
1727 _("Access from connection %p (%u of %u) for `%s' `%s' url `%s' with upload data size %u\n"),
1729 plugin->cur_request,
1730 plugin->max_request,
1734 (*upload_data_size));
1737 /* CORS pre-flight request */
1738 if (0 == strcmp (MHD_HTTP_METHOD_OPTIONS, method))
1740 response = MHD_create_response_from_buffer (0, NULL,
1741 MHD_RESPMEM_PERSISTENT);
1742 add_cors_headers(response);
1743 res = MHD_queue_response (mhd_connection, MHD_HTTP_OK, response);
1744 MHD_destroy_response (response);
1747 /* new connection */
1748 sc = server_lookup_connection (plugin, mhd_connection, url, method);
1751 /* attach to new / existing session */
1752 (*httpSessionCache) = sc;
1756 /* existing session already has matching connection, refuse */
1757 response = MHD_create_response_from_buffer (strlen (HTTP_ERROR_RESPONSE),
1758 HTTP_ERROR_RESPONSE,
1759 MHD_RESPMEM_PERSISTENT);
1760 MHD_add_response_header (response,
1761 MHD_HTTP_HEADER_CONTENT_TYPE,
1763 add_cors_headers(response);
1764 res = MHD_queue_response (mhd_connection, MHD_HTTP_NOT_FOUND, response);
1765 MHD_destroy_response (response);
1769 /* 'old' connection */
1770 if (NULL == (s = sc->session))
1772 /* Session was already disconnected;
1773 sent HTTP/1.1: 200 OK as response */
1774 response = MHD_create_response_from_buffer (strlen ("Thank you!"),
1776 MHD_RESPMEM_PERSISTENT);
1777 add_cors_headers(response);
1778 MHD_queue_response (mhd_connection, MHD_HTTP_OK, response);
1779 MHD_destroy_response (response);
1783 if (sc->direction == _SEND)
1785 response = MHD_create_response_from_callback (MHD_SIZE_UNKNOWN, 32 * 1024,
1786 &server_send_callback, sc, NULL);
1787 add_cors_headers(response);
1788 MHD_queue_response (mhd_connection, MHD_HTTP_OK, response);
1789 MHD_destroy_response (response);
1792 if (sc->direction == _RECEIVE)
1794 if ((*upload_data_size == 0) && (sc->connected == GNUNET_NO))
1796 /* (*upload_data_size == 0) first callback when header are passed */
1797 LOG (GNUNET_ERROR_TYPE_DEBUG,
1798 "Session %p / Connection %p: Peer `%s' PUT on address `%s' connected\n",
1800 GNUNET_i2s (&s->target),
1801 http_common_plugin_address_to_string (plugin->protocol,
1802 s->address->address,
1803 s->address->address_length));
1804 sc->connected = GNUNET_YES;
1807 else if ((*upload_data_size == 0) && (sc->connected == GNUNET_YES))
1809 /* (*upload_data_size == 0) when upload is complete */
1810 LOG (GNUNET_ERROR_TYPE_DEBUG,
1811 "Session %p / Connection %p: Peer `%s' PUT on address `%s' finished upload\n",
1813 GNUNET_i2s (&s->target),
1814 http_common_plugin_address_to_string (plugin->protocol,
1815 s->address->address,
1816 s->address->address_length));
1817 sc->connected = GNUNET_NO;
1818 /* Sent HTTP/1.1: 200 OK as PUT Response\ */
1819 response = MHD_create_response_from_buffer (strlen ("Thank you!"),
1821 MHD_RESPMEM_PERSISTENT);
1822 add_cors_headers(response);
1823 MHD_queue_response (mhd_connection, MHD_HTTP_OK, response);
1824 MHD_destroy_response (response);
1827 else if ((*upload_data_size > 0) && (sc->connected == GNUNET_YES))
1829 struct GNUNET_TIME_Relative delay;
1831 /* (*upload_data_size > 0) for every segment received */
1832 LOG (GNUNET_ERROR_TYPE_DEBUG,
1833 "Session %p / Connection %p: Peer `%s' PUT on address `%s' received %u bytes\n",
1835 GNUNET_i2s (&s->target),
1836 http_common_plugin_address_to_string (plugin->protocol,
1837 s->address->address,
1838 s->address->address_length),
1840 delay = GNUNET_TIME_absolute_get_remaining (s->next_receive);
1841 if (0 == delay.rel_value_us)
1843 LOG (GNUNET_ERROR_TYPE_DEBUG,
1844 "PUT with %u bytes forwarded to MST\n",
1846 if (s->msg_tk == NULL)
1848 s->msg_tk = GNUNET_MST_create (&server_receive_mst_cb,
1851 GNUNET_MST_from_buffer (s->msg_tk,
1854 GNUNET_NO, GNUNET_NO);
1855 server_mhd_connection_timeout (plugin, s,
1856 GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT.rel_value_us / 1000LL
1858 (*upload_data_size) = 0;
1862 /* delay processing */
1863 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1864 "Session %p / Connection %p: no inbound bandwidth available! Next read was delayed by %s\n",
1867 GNUNET_STRINGS_relative_time_to_string (delay,
1869 GNUNET_assert(s->server_recv->mhd_conn == mhd_connection);
1870 MHD_suspend_connection (s->server_recv->mhd_conn);
1871 if (NULL == s->recv_wakeup_task)
1873 = GNUNET_SCHEDULER_add_delayed (delay,
1890 * Callback from MHD when a connection disconnects
1892 * @param cls closure with the `struct HTTP_Server_Plugin *`
1893 * @param connection the disconnected MHD connection
1894 * @param httpSessionCache the pointer to distinguish
1897 server_disconnect_cb (void *cls,
1898 struct MHD_Connection *connection,
1899 void **httpSessionCache)
1901 struct HTTP_Server_Plugin *plugin = cls;
1902 struct ServerRequest *sc = *httpSessionCache;
1904 LOG (GNUNET_ERROR_TYPE_DEBUG,
1905 "Disconnect for connection %p\n",
1909 /* CORS pre-flight request finished */
1913 if (NULL != sc->session)
1915 if (sc->direction == _SEND)
1917 LOG (GNUNET_ERROR_TYPE_DEBUG,
1918 "Peer `%s' connection %p, GET on address `%s' disconnected\n",
1919 GNUNET_i2s (&sc->session->target),
1920 sc->session->server_send,
1921 http_common_plugin_address_to_string (plugin->protocol,
1922 sc->session->address->address,
1923 sc->session->address->address_length));
1925 sc->session->server_send = NULL;
1927 else if (sc->direction == _RECEIVE)
1929 LOG (GNUNET_ERROR_TYPE_DEBUG,
1930 "Peer `%s' connection %p PUT on address `%s' disconnected\n",
1931 GNUNET_i2s (&sc->session->target),
1932 sc->session->server_recv,
1933 http_common_plugin_address_to_string (plugin->protocol,
1934 sc->session->address->address,
1935 sc->session->address->address_length));
1936 sc->session->server_recv = NULL;
1937 if (NULL != sc->session->msg_tk)
1939 GNUNET_MST_destroy (sc->session->msg_tk);
1940 sc->session->msg_tk = NULL;
1945 plugin->cur_request--;
1950 * Check if incoming connection is accepted.
1952 * @param cls plugin as closure
1953 * @param addr address of incoming connection
1954 * @param addr_len number of bytes in @a addr
1955 * @return MHD_YES if connection is accepted, MHD_NO if connection is rejected
1958 server_accept_cb (void *cls,
1959 const struct sockaddr *addr,
1962 struct HTTP_Server_Plugin *plugin = cls;
1964 if (plugin->cur_request <= plugin->max_request)
1966 LOG (GNUNET_ERROR_TYPE_DEBUG,
1967 _("Accepting connection (%u of %u) from `%s'\n"),
1968 plugin->cur_request, plugin->max_request,
1969 GNUNET_a2s (addr, addr_len));
1974 LOG (GNUNET_ERROR_TYPE_WARNING,
1975 _("Server reached maximum number connections (%u), rejecting new connection\n"),
1976 plugin->max_request);
1983 * Log function called by MHD.
1986 * @param fmt format string
1987 * @param ap arguments for the format string (va_start() and va_end()
1988 * will be called by MHD)
1991 server_log (void *arg,
2001 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2009 * Load ssl certificate from file
2011 * @param file filename
2012 * @return content of the file
2015 server_load_file (const char *file)
2017 struct GNUNET_DISK_FileHandle *gn_file;
2021 if (GNUNET_OK != GNUNET_DISK_file_size (file,
2022 &fsize, GNUNET_NO, GNUNET_YES))
2024 text = GNUNET_malloc (fsize + 1);
2026 GNUNET_DISK_file_open (file, GNUNET_DISK_OPEN_READ,
2027 GNUNET_DISK_PERM_USER_READ);
2028 if (NULL == gn_file)
2033 if (GNUNET_SYSERR == GNUNET_DISK_file_read (gn_file, text, fsize))
2036 GNUNET_DISK_file_close (gn_file);
2040 GNUNET_DISK_file_close (gn_file);
2048 * Load ssl certificate
2050 * @param plugin the plugin
2051 * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
2054 server_load_certificate (struct HTTP_Server_Plugin *plugin)
2056 int res = GNUNET_OK;
2062 GNUNET_CONFIGURATION_get_value_filename (plugin->env->cfg,
2064 "KEY_FILE", &key_file))
2066 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
2067 plugin->name, "CERT_FILE");
2068 return GNUNET_SYSERR;
2071 GNUNET_CONFIGURATION_get_value_filename (plugin->env->cfg,
2073 "CERT_FILE", &cert_file))
2075 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
2076 plugin->name, "CERT_FILE");
2077 GNUNET_free (key_file);
2078 return GNUNET_SYSERR;
2080 /* Get crypto init string from config. If not present, use
2083 GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg,
2086 &plugin->crypto_init))
2087 LOG (GNUNET_ERROR_TYPE_DEBUG,
2088 "Using crypto init string `%s'\n",
2089 plugin->crypto_init);
2091 LOG (GNUNET_ERROR_TYPE_DEBUG,
2092 "Using default crypto init string \n");
2094 /* read key & certificates from file */
2095 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2096 "Trying to loading TLS certificate from key-file `%s' cert-file`%s'\n",
2097 key_file, cert_file);
2099 plugin->key = server_load_file (key_file);
2100 plugin->cert = server_load_file (cert_file);
2102 if ((plugin->key == NULL) || (plugin->cert == NULL))
2104 struct GNUNET_OS_Process *cert_creation;
2106 GNUNET_free_non_null (plugin->key);
2108 GNUNET_free_non_null (plugin->cert);
2109 plugin->cert = NULL;
2111 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2112 "No usable TLS certificate found, creating certificate\n");
2115 GNUNET_OS_start_process (GNUNET_NO, GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
2117 "gnunet-transport-certificate-creation",
2118 "gnunet-transport-certificate-creation",
2122 if (NULL == cert_creation)
2124 LOG (GNUNET_ERROR_TYPE_ERROR,
2125 _("Could not create a new TLS certificate, program `gnunet-transport-certificate-creation' could not be started!\n"));
2126 GNUNET_free (key_file);
2127 GNUNET_free (cert_file);
2129 GNUNET_free_non_null (plugin->key);
2131 GNUNET_free_non_null (plugin->cert);
2132 plugin->cert = NULL;
2133 GNUNET_free_non_null (plugin->crypto_init);
2134 plugin->crypto_init = NULL;
2136 return GNUNET_SYSERR;
2138 GNUNET_assert (GNUNET_OK == GNUNET_OS_process_wait (cert_creation));
2139 GNUNET_OS_process_destroy (cert_creation);
2141 plugin->key = server_load_file (key_file);
2142 plugin->cert = server_load_file (cert_file);
2145 if ((plugin->key == NULL) || (plugin->cert == NULL))
2147 LOG (GNUNET_ERROR_TYPE_ERROR,
2148 _("No usable TLS certificate found and creating one at `%s/%s' failed!\n"),
2149 key_file, cert_file);
2150 GNUNET_free (key_file);
2151 GNUNET_free (cert_file);
2153 GNUNET_free_non_null (plugin->key);
2155 GNUNET_free_non_null (plugin->cert);
2156 plugin->cert = NULL;
2157 GNUNET_free_non_null (plugin->crypto_init);
2158 plugin->crypto_init = NULL;
2160 return GNUNET_SYSERR;
2162 GNUNET_free (key_file);
2163 GNUNET_free (cert_file);
2164 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2165 "TLS certificate loaded\n");
2172 * Invoke `MHD_start_daemon` with the various options we need to
2173 * setup the HTTP server with the given listen address.
2175 * @param plugin our plugin
2176 * @param addr listen address to use
2177 * @param v6 MHD_NO_FLAG or MHD_USE_IPv6, depending on context
2178 * @return NULL on error
2180 static struct MHD_Daemon *
2181 run_mhd_start_daemon (struct HTTP_Server_Plugin *plugin,
2182 const struct sockaddr_in *addr,
2185 struct MHD_Daemon *server;
2186 unsigned int timeout;
2188 #if MHD_VERSION >= 0x00090E00
2189 timeout = HTTP_SERVER_NOT_VALIDATED_TIMEOUT.rel_value_us / 1000LL / 1000LL;
2190 LOG (GNUNET_ERROR_TYPE_DEBUG,
2191 "MHD can set timeout per connection! Default time out %u sec.\n",
2194 timeout = HTTP_SERVER_SESSION_TIMEOUT.rel_value_us / 1000LL / 1000LL;
2195 LOG (GNUNET_ERROR_TYPE_WARNING,
2196 "MHD cannot set timeout per connection! Default time out %u sec.\n",
2199 server = MHD_start_daemon (
2206 MHD_USE_SUSPEND_RESUME |
2209 &server_accept_cb, plugin,
2210 &server_access_cb, plugin,
2211 MHD_OPTION_SOCK_ADDR,
2213 MHD_OPTION_CONNECTION_LIMIT,
2214 (unsigned int) plugin->max_request,
2216 MHD_OPTION_HTTPS_PRIORITIES,
2217 plugin->crypto_init,
2218 MHD_OPTION_HTTPS_MEM_KEY,
2220 MHD_OPTION_HTTPS_MEM_CERT,
2223 MHD_OPTION_CONNECTION_TIMEOUT,
2225 MHD_OPTION_CONNECTION_MEMORY_LIMIT,
2227 GNUNET_MAX_MESSAGE_SIZE),
2228 MHD_OPTION_NOTIFY_COMPLETED,
2229 &server_disconnect_cb, plugin,
2230 MHD_OPTION_EXTERNAL_LOGGER,
2234 if ( (NULL != server) &&
2235 (0 != (plugin->options & HTTP_OPTIONS_TCP_STEALTH)) )
2237 const union MHD_DaemonInfo *di;
2239 di = MHD_get_daemon_info (server,
2240 MHD_DAEMON_INFO_LISTEN_FD,
2242 if ( (0 != setsockopt ((int) di->listen_fd,
2245 plugin->env->my_identity,
2246 sizeof (struct GNUNET_PeerIdentity))) )
2248 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2249 _("TCP_STEALTH not supported on this platform.\n"));
2250 MHD_stop_daemon (server);
2260 * Start the HTTP server
2262 * @param plugin the plugin handle
2263 * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
2266 server_start (struct HTTP_Server_Plugin *plugin)
2270 GNUNET_assert (NULL != plugin);
2272 if (GNUNET_SYSERR == server_load_certificate (plugin))
2274 LOG (GNUNET_ERROR_TYPE_ERROR,
2275 _("Could not load or create server certificate! Loading plugin failed!\n"));
2276 return GNUNET_SYSERR;
2282 plugin->server_v4 = NULL;
2283 if (GNUNET_YES == plugin->use_ipv4)
2286 = run_mhd_start_daemon (plugin,
2287 (const struct sockaddr_in *) plugin->server_addr_v4,
2290 if (NULL == plugin->server_v4)
2292 LOG (GNUNET_ERROR_TYPE_ERROR,
2293 "Failed to start %s IPv4 server component on port %u\n",
2298 server_reschedule (plugin,
2304 plugin->server_v6 = NULL;
2305 if (GNUNET_YES == plugin->use_ipv6)
2308 = run_mhd_start_daemon (plugin,
2309 (const struct sockaddr_in *) plugin->server_addr_v6,
2311 if (NULL == plugin->server_v6)
2313 LOG (GNUNET_ERROR_TYPE_ERROR,
2314 "Failed to start %s IPv6 server component on port %u\n",
2320 server_reschedule (plugin,
2326 if ( (NULL == plugin->server_v6) &&
2327 (NULL == plugin->server_v4) )
2329 LOG (GNUNET_ERROR_TYPE_ERROR,
2330 "%s %s server component started on port %u\n",
2334 return GNUNET_SYSERR;
2336 if ((NULL != plugin->server_v6) &&
2337 (NULL != plugin->server_v4))
2338 msg = "IPv4 and IPv6";
2339 else if (NULL != plugin->server_v6)
2341 else if (NULL != plugin->server_v4)
2343 LOG (GNUNET_ERROR_TYPE_DEBUG,
2344 "%s %s server component started on port %u\n",
2353 * Add an address to the server's set of addresses and notify transport
2355 * @param cls the plugin handle
2356 * @param add_remove #GNUNET_YES on add, #GNUNET_NO on remove
2357 * @param addr the address
2358 * @param addrlen address length
2361 server_add_address (void *cls,
2363 const struct sockaddr *addr,
2366 struct HTTP_Server_Plugin *plugin = cls;
2367 struct GNUNET_HELLO_Address *address;
2368 struct HttpAddressWrapper *w = NULL;
2370 w = GNUNET_new (struct HttpAddressWrapper);
2371 w->address = http_common_address_from_socket (plugin->protocol,
2374 if (NULL == w->address)
2379 w->addrlen = http_common_address_get_size (w->address);
2381 GNUNET_CONTAINER_DLL_insert (plugin->addr_head,
2384 LOG (GNUNET_ERROR_TYPE_DEBUG,
2385 "Notifying transport to add address `%s'\n",
2386 http_common_plugin_address_to_string (plugin->protocol,
2389 /* modify our published address list */
2391 address = GNUNET_HELLO_address_allocate (plugin->env->my_identity,
2392 "https_client", w->address, w->addrlen, GNUNET_HELLO_ADDRESS_INFO_NONE);
2394 address = GNUNET_HELLO_address_allocate (plugin->env->my_identity,
2395 "http_client", w->address, w->addrlen, GNUNET_HELLO_ADDRESS_INFO_NONE);
2398 plugin->env->notify_address (plugin->env->cls,
2401 GNUNET_HELLO_address_free (address);
2406 * Remove an address from the server's set of addresses and notify transport
2408 * @param cls the plugin handle
2409 * @param add_remove #GNUNET_YES on add, #GNUNET_NO on remove
2410 * @param addr the address
2411 * @param addrlen address length
2414 server_remove_address (void *cls,
2416 const struct sockaddr *addr,
2419 struct HTTP_Server_Plugin *plugin = cls;
2420 struct GNUNET_HELLO_Address *address;
2421 struct HttpAddressWrapper *w = plugin->addr_head;
2425 saddr = http_common_address_from_socket (plugin->protocol,
2430 saddr_len = http_common_address_get_size (saddr);
2435 http_common_cmp_addresses (w->address,
2442 GNUNET_free (saddr);
2447 LOG (GNUNET_ERROR_TYPE_DEBUG,
2448 "Notifying transport to remove address `%s'\n",
2449 http_common_plugin_address_to_string (plugin->protocol,
2452 GNUNET_CONTAINER_DLL_remove (plugin->addr_head,
2455 /* modify our published address list */
2457 address = GNUNET_HELLO_address_allocate (plugin->env->my_identity,
2458 "https_client", w->address, w->addrlen, GNUNET_HELLO_ADDRESS_INFO_NONE);
2460 address = GNUNET_HELLO_address_allocate (plugin->env->my_identity,
2461 "http_client", w->address, w->addrlen, GNUNET_HELLO_ADDRESS_INFO_NONE);
2463 plugin->env->notify_address (plugin->env->cls, add_remove, address);
2464 GNUNET_HELLO_address_free (address);
2465 GNUNET_free (w->address);
2472 * Our external IP address/port mapping has changed.
2474 * @param cls closure, the 'struct LocalAddrList'
2475 * @param add_remove #GNUNET_YES to mean the new public IP address, #GNUNET_NO to mean
2476 * the previous (now invalid) one
2477 * @param ac address class the address belongs to
2478 * @param addr either the previous or the new public IP address
2479 * @param addrlen actual lenght of the address
2482 server_nat_port_map_callback (void *cls,
2484 enum GNUNET_NAT_AddressClass ac,
2485 const struct sockaddr *addr,
2488 struct HTTP_Server_Plugin *plugin = cls;
2490 LOG (GNUNET_ERROR_TYPE_DEBUG,
2491 "NAT called to %s address `%s'\n",
2492 (add_remove == GNUNET_NO) ? "remove" : "add",
2493 GNUNET_a2s (addr, addrlen));
2495 if (AF_INET == addr->sa_family)
2497 struct sockaddr_in *s4 = (struct sockaddr_in *) addr;
2499 if (GNUNET_NO == plugin->use_ipv4)
2502 if ((NULL != plugin->server_addr_v4) &&
2503 (0 != memcmp (&plugin->server_addr_v4->sin_addr,
2505 sizeof (struct in_addr))))
2507 LOG (GNUNET_ERROR_TYPE_DEBUG,
2508 "Skipping address `%s' (not bindto address)\n",
2509 GNUNET_a2s (addr, addrlen));
2514 if (AF_INET6 == addr->sa_family)
2516 struct sockaddr_in6 *s6 = (struct sockaddr_in6 *) addr;
2517 if (GNUNET_NO == plugin->use_ipv6)
2520 if ((NULL != plugin->server_addr_v6) &&
2521 (0 != memcmp (&plugin->server_addr_v6->sin6_addr,
2522 &s6->sin6_addr, sizeof (struct in6_addr))))
2524 LOG (GNUNET_ERROR_TYPE_DEBUG,
2525 "Skipping address `%s' (not bindto address)\n",
2526 GNUNET_a2s (addr, addrlen));
2534 server_add_address (cls, add_remove, addr, addrlen);
2537 server_remove_address (cls, add_remove, addr, addrlen);
2544 * Get valid server addresses
2546 * @param plugin the plugin handle
2547 * @param service_name the servicename
2548 * @param cfg configuration handle
2549 * @param addrs addresses
2550 * @param addr_lens address length
2551 * @return number of addresses
2554 server_get_addresses (struct HTTP_Server_Plugin *plugin,
2555 const char *service_name,
2556 const struct GNUNET_CONFIGURATION_Handle *cfg,
2557 struct sockaddr ***addrs,
2558 socklen_t ** addr_lens)
2561 unsigned long long port;
2562 struct addrinfo hints;
2563 struct addrinfo *res;
2564 struct addrinfo *pos;
2565 struct addrinfo *next;
2569 struct sockaddr **saddrs;
2570 socklen_t *saddrlens;
2576 disablev6 = !plugin->use_ipv6;
2579 if (GNUNET_CONFIGURATION_have_value (cfg, service_name, "PORT"))
2581 GNUNET_break (GNUNET_OK ==
2582 GNUNET_CONFIGURATION_get_value_number (cfg, service_name,
2586 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2587 _("Require valid port number for service in configuration!\n"));
2588 return GNUNET_SYSERR;
2593 LOG (GNUNET_ERROR_TYPE_INFO,
2594 "Starting in listen only mode\n");
2595 return -1; /* listen only */
2599 if (GNUNET_CONFIGURATION_have_value (cfg, service_name,
2602 GNUNET_break (GNUNET_OK ==
2603 GNUNET_CONFIGURATION_get_value_string (cfg, service_name,
2604 "BINDTO", &hostname));
2609 if (NULL != hostname)
2611 LOG (GNUNET_ERROR_TYPE_DEBUG,
2612 "Resolving `%s' since that is where `%s' will bind to.\n",
2613 hostname, service_name);
2614 memset (&hints, 0, sizeof (struct addrinfo));
2616 hints.ai_family = AF_INET;
2617 if ((0 != (ret = getaddrinfo (hostname, NULL, &hints, &res))) ||
2620 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2621 _("Failed to resolve `%s': %s\n"),
2623 gai_strerror (ret));
2624 GNUNET_free (hostname);
2625 return GNUNET_SYSERR;
2629 while (NULL != (pos = next))
2631 next = pos->ai_next;
2632 if ((disablev6) && (pos->ai_family == AF_INET6))
2638 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2639 _("Failed to find %saddress for `%s'.\n"),
2640 disablev6 ? "IPv4 " : "", hostname);
2642 GNUNET_free (hostname);
2643 return GNUNET_SYSERR;
2646 saddrs = GNUNET_malloc ((resi + 1) * sizeof (struct sockaddr *));
2647 saddrlens = GNUNET_malloc ((resi + 1) * sizeof (socklen_t));
2650 while (NULL != (pos = next))
2652 next = pos->ai_next;
2653 if ((disablev6) && (pos->ai_family == AF_INET6))
2655 if ((pos->ai_protocol != IPPROTO_TCP) && (0 != pos->ai_protocol))
2656 continue; /* not TCP */
2657 if ((pos->ai_socktype != SOCK_STREAM) && (0 != pos->ai_socktype))
2658 continue; /* huh? */
2659 LOG (GNUNET_ERROR_TYPE_DEBUG,
2660 "Service will bind to `%s'\n",
2661 GNUNET_a2s (pos->ai_addr,
2663 if (pos->ai_family == AF_INET)
2665 GNUNET_assert (pos->ai_addrlen == sizeof (struct sockaddr_in));
2666 saddrlens[i] = pos->ai_addrlen;
2667 saddrs[i] = GNUNET_malloc (saddrlens[i]);
2668 GNUNET_memcpy (saddrs[i], pos->ai_addr, saddrlens[i]);
2669 ((struct sockaddr_in *) saddrs[i])->sin_port = htons (port);
2673 GNUNET_assert (pos->ai_family == AF_INET6);
2674 GNUNET_assert (pos->ai_addrlen == sizeof (struct sockaddr_in6));
2675 saddrlens[i] = pos->ai_addrlen;
2676 saddrs[i] = GNUNET_malloc (saddrlens[i]);
2677 GNUNET_memcpy (saddrs[i], pos->ai_addr, saddrlens[i]);
2678 ((struct sockaddr_in6 *) saddrs[i])->sin6_port = htons (port);
2682 GNUNET_free (hostname);
2688 /* will bind against everything, just set port */
2694 saddrs = GNUNET_malloc ((resi + 1) * sizeof (struct sockaddr *));
2695 saddrlens = GNUNET_malloc ((resi + 1) * sizeof (socklen_t));
2697 saddrlens[i] = sizeof (struct sockaddr_in);
2698 saddrs[i] = GNUNET_malloc (saddrlens[i]);
2699 #if HAVE_SOCKADDR_IN_SIN_LEN
2700 ((struct sockaddr_in *) saddrs[i])->sin_len = saddrlens[i];
2702 ((struct sockaddr_in *) saddrs[i])->sin_family = AF_INET;
2703 ((struct sockaddr_in *) saddrs[i])->sin_port = htons (port);
2709 saddrs = GNUNET_malloc ((resi + 1) * sizeof (struct sockaddr *));
2710 saddrlens = GNUNET_malloc ((resi + 1) * sizeof (socklen_t));
2712 saddrlens[i] = sizeof (struct sockaddr_in6);
2713 saddrs[i] = GNUNET_malloc (saddrlens[i]);
2714 #if HAVE_SOCKADDR_IN_SIN_LEN
2715 ((struct sockaddr_in6 *) saddrs[i])->sin6_len = saddrlens[0];
2717 ((struct sockaddr_in6 *) saddrs[i])->sin6_family = AF_INET6;
2718 ((struct sockaddr_in6 *) saddrs[i])->sin6_port = htons (port);
2720 saddrlens[i] = sizeof (struct sockaddr_in);
2721 saddrs[i] = GNUNET_malloc (saddrlens[i]);
2722 #if HAVE_SOCKADDR_IN_SIN_LEN
2723 ((struct sockaddr_in *) saddrs[i])->sin_len = saddrlens[1];
2725 ((struct sockaddr_in *) saddrs[i])->sin_family = AF_INET;
2726 ((struct sockaddr_in *) saddrs[i])->sin_port = htons (port);
2730 *addr_lens = saddrlens;
2736 * Ask NAT for addresses
2738 * @param plugin the plugin handle
2741 server_start_report_addresses (struct HTTP_Server_Plugin *plugin)
2743 int res = GNUNET_OK;
2744 struct sockaddr **addrs;
2745 socklen_t *addrlens;
2747 res = server_get_addresses (plugin,
2751 LOG (GNUNET_ERROR_TYPE_DEBUG,
2752 _("Found %u addresses to report to NAT service\n"),
2755 if (GNUNET_SYSERR == res)
2762 = GNUNET_NAT_register (plugin->env->cfg,
2763 "transport-http_server",
2766 (const struct sockaddr **) addrs,
2768 &server_nat_port_map_callback,
2774 GNUNET_assert (NULL != addrs[res]);
2775 GNUNET_free (addrs[res]);
2777 GNUNET_free_non_null (addrs);
2778 GNUNET_free_non_null (addrlens);
2783 * Stop NAT for addresses
2785 * @param plugin the plugin handle
2788 server_stop_report_addresses (struct HTTP_Server_Plugin *plugin)
2790 struct HttpAddressWrapper *w;
2792 /* Stop NAT handle */
2793 if (NULL != plugin->nat)
2795 GNUNET_NAT_unregister (plugin->nat);
2798 /* Clean up addresses */
2799 while (NULL != plugin->addr_head)
2801 w = plugin->addr_head;
2802 GNUNET_CONTAINER_DLL_remove (plugin->addr_head,
2805 GNUNET_free (w->address);
2812 * Check if IPv6 supported on this system
2814 * @param plugin the plugin handle
2815 * @return #GNUNET_YES on success, else #GNUNET_NO
2818 server_check_ipv6_support (struct HTTP_Server_Plugin *plugin)
2820 struct GNUNET_NETWORK_Handle *desc = NULL;
2821 int res = GNUNET_NO;
2823 /* Probe IPv6 support */
2824 desc = GNUNET_NETWORK_socket_create (PF_INET6,
2829 if ( (errno == ENOBUFS) ||
2830 (errno == ENOMEM) ||
2831 (errno == ENFILE) ||
2834 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR,
2837 LOG (GNUNET_ERROR_TYPE_WARNING,
2838 _("Disabling IPv6 since it is not supported on this system!\n"));
2843 GNUNET_break (GNUNET_OK ==
2844 GNUNET_NETWORK_socket_close (desc));
2848 LOG (GNUNET_ERROR_TYPE_DEBUG,
2849 "Testing IPv6 on this system: %s\n",
2850 (res == GNUNET_YES) ? "successful" : "failed");
2856 * Notify server about our external hostname
2861 server_notify_external_hostname (void *cls)
2863 struct HTTP_Server_Plugin *plugin = cls;
2864 struct HttpAddress *ext_addr;
2865 size_t ext_addr_len;
2869 plugin->notify_ext_task = NULL;
2870 GNUNET_asprintf (&url,
2873 plugin->external_hostname);
2874 urlen = strlen (url) + 1;
2875 ext_addr = GNUNET_malloc (sizeof (struct HttpAddress) + urlen);
2876 ext_addr->options = htonl (plugin->options);
2877 ext_addr->urlen = htonl (urlen);
2878 ext_addr_len = sizeof (struct HttpAddress) + urlen;
2879 GNUNET_memcpy (&ext_addr[1], url, urlen);
2882 LOG (GNUNET_ERROR_TYPE_DEBUG,
2883 "Notifying transport about external hostname address `%s'\n",
2884 plugin->external_hostname);
2887 if (GNUNET_YES == plugin->verify_external_hostname)
2888 LOG (GNUNET_ERROR_TYPE_INFO,
2889 "Enabling SSL verification for external hostname address `%s'\n",
2890 plugin->external_hostname);
2892 = GNUNET_HELLO_address_allocate (plugin->env->my_identity,
2896 GNUNET_HELLO_ADDRESS_INFO_NONE);
2897 plugin->env->notify_address (plugin->env->cls,
2900 GNUNET_free (ext_addr);
2903 = GNUNET_HELLO_address_allocate (plugin->env->my_identity,
2907 GNUNET_HELLO_ADDRESS_INFO_NONE);
2908 plugin->env->notify_address (plugin->env->cls,
2911 GNUNET_free (ext_addr);
2917 * Configure the plugin
2919 * @param plugin plugin handle
2920 * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
2923 server_configure_plugin (struct HTTP_Server_Plugin *plugin)
2925 unsigned long long port;
2926 unsigned long long max_connections;
2927 char *bind4_address = NULL;
2928 char *bind6_address = NULL;
2929 char *eh_tmp = NULL;
2930 int external_hostname_use_port;
2933 if (GNUNET_CONFIGURATION_have_value
2934 (plugin->env->cfg, plugin->name, "USE_IPv4"))
2937 GNUNET_CONFIGURATION_get_value_yesno (plugin->env->cfg,
2942 plugin->use_ipv4 = GNUNET_YES;
2943 LOG (GNUNET_ERROR_TYPE_DEBUG,
2944 _("IPv4 support is %s\n"),
2945 (plugin->use_ipv4 == GNUNET_YES) ? "enabled" : "disabled");
2948 if (GNUNET_CONFIGURATION_have_value
2949 (plugin->env->cfg, plugin->name, "USE_IPv6"))
2952 GNUNET_CONFIGURATION_get_value_yesno (plugin->env->cfg,
2957 plugin->use_ipv6 = GNUNET_YES;
2958 LOG (GNUNET_ERROR_TYPE_DEBUG,
2959 _("IPv6 support is %s\n"),
2960 (plugin->use_ipv6 == GNUNET_YES) ? "enabled" : "disabled");
2962 if ((plugin->use_ipv4 == GNUNET_NO) && (plugin->use_ipv6 == GNUNET_NO))
2964 LOG (GNUNET_ERROR_TYPE_ERROR,
2965 _("Neither IPv4 nor IPv6 are enabled! Fix in configuration\n"));
2966 return GNUNET_SYSERR;
2969 /* Reading port number from config file */
2971 GNUNET_CONFIGURATION_get_value_number (plugin->env->cfg,
2973 "PORT", &port)) || (port > 65535))
2975 LOG (GNUNET_ERROR_TYPE_ERROR,
2976 _("Port is required! Fix in configuration\n"));
2977 return GNUNET_SYSERR;
2979 plugin->port = port;
2981 LOG (GNUNET_ERROR_TYPE_INFO,
2982 _("Using port %u\n"), plugin->port);
2984 if ( (plugin->use_ipv4 == GNUNET_YES) &&
2986 GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg,
2991 LOG (GNUNET_ERROR_TYPE_DEBUG,
2992 "Binding %s plugin to specific IPv4 address: `%s'\n",
2995 plugin->server_addr_v4 = GNUNET_new (struct sockaddr_in);
2996 if (1 != inet_pton (AF_INET,
2998 &plugin->server_addr_v4->sin_addr))
3000 LOG (GNUNET_ERROR_TYPE_ERROR,
3001 _("Specific IPv4 address `%s' in configuration file is invalid!\n"),
3003 GNUNET_free (bind4_address);
3004 GNUNET_free (plugin->server_addr_v4);
3005 plugin->server_addr_v4 = NULL;
3006 return GNUNET_SYSERR;
3010 LOG (GNUNET_ERROR_TYPE_DEBUG,
3011 "Binding to IPv4 address %s\n",
3013 plugin->server_addr_v4->sin_family = AF_INET;
3014 plugin->server_addr_v4->sin_port = htons (plugin->port);
3016 GNUNET_free (bind4_address);
3019 if ((plugin->use_ipv6 == GNUNET_YES) &&
3021 GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg,
3026 LOG (GNUNET_ERROR_TYPE_DEBUG,
3027 "Binding %s plugin to specific IPv6 address: `%s'\n",
3028 plugin->protocol, bind6_address);
3029 plugin->server_addr_v6 = GNUNET_new (struct sockaddr_in6);
3031 inet_pton (AF_INET6,
3033 &plugin->server_addr_v6->sin6_addr))
3035 LOG (GNUNET_ERROR_TYPE_ERROR,
3036 _("Specific IPv6 address `%s' in configuration file is invalid!\n"),
3038 GNUNET_free (bind6_address);
3039 GNUNET_free (plugin->server_addr_v6);
3040 plugin->server_addr_v6 = NULL;
3041 return GNUNET_SYSERR;
3045 LOG (GNUNET_ERROR_TYPE_DEBUG,
3046 "Binding to IPv6 address %s\n",
3048 plugin->server_addr_v6->sin6_family = AF_INET6;
3049 plugin->server_addr_v6->sin6_port = htons (plugin->port);
3051 GNUNET_free (bind6_address);
3054 plugin->verify_external_hostname = GNUNET_NO;
3056 plugin->verify_external_hostname
3057 = GNUNET_CONFIGURATION_get_value_yesno (plugin->env->cfg,
3059 "VERIFY_EXTERNAL_HOSTNAME");
3060 if (GNUNET_SYSERR == plugin->verify_external_hostname)
3061 plugin->verify_external_hostname = GNUNET_NO;
3062 if (GNUNET_YES == plugin->verify_external_hostname)
3063 plugin->options |= HTTP_OPTIONS_VERIFY_CERTIFICATE;
3065 external_hostname_use_port
3066 = GNUNET_CONFIGURATION_get_value_yesno (plugin->env->cfg,
3068 "EXTERNAL_HOSTNAME_USE_PORT");
3069 if (GNUNET_SYSERR == external_hostname_use_port)
3070 external_hostname_use_port = GNUNET_NO;
3074 GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg,
3076 "EXTERNAL_HOSTNAME",
3081 char *pos_url = NULL;
3083 if (NULL != strstr(eh_tmp, "://"))
3084 tmp = &strstr(eh_tmp, "://")[3];
3088 if (GNUNET_YES == external_hostname_use_port)
3090 if ( (strlen (tmp) > 1) && (NULL != (pos = strchr(tmp, '/'))) )
3094 GNUNET_asprintf (&plugin->external_hostname,
3101 GNUNET_asprintf (&plugin->external_hostname,
3107 plugin->external_hostname = GNUNET_strdup (tmp);
3108 GNUNET_free (eh_tmp);
3110 LOG (GNUNET_ERROR_TYPE_INFO,
3111 _("Using external hostname `%s'\n"),
3112 plugin->external_hostname);
3113 plugin->notify_ext_task = GNUNET_SCHEDULER_add_now (&server_notify_external_hostname,
3116 /* Use only configured external hostname */
3117 if (GNUNET_CONFIGURATION_have_value
3120 "EXTERNAL_HOSTNAME_ONLY"))
3122 plugin->external_only =
3123 GNUNET_CONFIGURATION_get_value_yesno (plugin->env->cfg,
3125 "EXTERNAL_HOSTNAME_ONLY");
3128 plugin->external_only = GNUNET_NO;
3130 if (GNUNET_YES == plugin->external_only)
3131 LOG (GNUNET_ERROR_TYPE_DEBUG,
3132 _("Notifying transport only about hostname `%s'\n"),
3133 plugin->external_hostname);
3136 LOG (GNUNET_ERROR_TYPE_DEBUG,
3137 "No external hostname configured\n");
3139 /* Optional parameters */
3141 GNUNET_CONFIGURATION_get_value_number (plugin->env->cfg,
3145 max_connections = 128;
3146 plugin->max_request = max_connections;
3148 LOG (GNUNET_ERROR_TYPE_DEBUG,
3149 _("Maximum number of connections is %u\n"),
3150 plugin->max_request);
3152 plugin->peer_id_length = strlen (GNUNET_i2s_full (plugin->env->my_identity));
3159 * Exit point from the plugin.
3165 LIBGNUNET_PLUGIN_TRANSPORT_DONE (void *cls)
3167 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
3168 struct HTTP_Server_Plugin *plugin = api->cls;
3170 if (NULL == api->cls)
3172 /* Free for stub mode */
3176 plugin->in_shutdown = GNUNET_YES;
3177 LOG (GNUNET_ERROR_TYPE_INFO,
3178 _("Shutting down plugin `%s'\n"),
3181 if (NULL != plugin->notify_ext_task)
3183 GNUNET_SCHEDULER_cancel (plugin->notify_ext_task);
3184 plugin->notify_ext_task = NULL;
3187 if (NULL != plugin->ext_addr)
3189 LOG (GNUNET_ERROR_TYPE_DEBUG,
3190 "Notifying transport to remove address `%s'\n",
3191 http_common_plugin_address_to_string (plugin->protocol,
3192 plugin->ext_addr->address,
3193 plugin->ext_addr->address_length));
3195 plugin->env->notify_address (plugin->env->cls,
3199 plugin->env->notify_address (plugin->env->cls,
3203 GNUNET_HELLO_address_free (plugin->ext_addr);
3204 plugin->ext_addr = NULL;
3207 /* Stop to report addresses to transport service */
3208 server_stop_report_addresses (plugin);
3209 if (NULL != plugin->server_v4_task)
3211 GNUNET_SCHEDULER_cancel (plugin->server_v4_task);
3212 plugin->server_v4_task = NULL;
3215 if (NULL != plugin->server_v6_task)
3217 GNUNET_SCHEDULER_cancel (plugin->server_v6_task);
3218 plugin->server_v6_task = NULL;
3221 GNUNET_free_non_null (plugin->crypto_init);
3222 GNUNET_free_non_null (plugin->cert);
3223 GNUNET_free_non_null (plugin->key);
3225 GNUNET_CONTAINER_multipeermap_iterate (plugin->sessions,
3226 &destroy_session_shutdown_cb,
3228 GNUNET_CONTAINER_multipeermap_destroy (plugin->sessions);
3229 plugin->sessions = NULL;
3230 if (NULL != plugin->server_v4)
3232 MHD_stop_daemon (plugin->server_v4);
3233 plugin->server_v4 = NULL;
3235 if (NULL != plugin->server_v6)
3237 MHD_stop_daemon (plugin->server_v6);
3238 plugin->server_v6 = NULL;
3241 GNUNET_free_non_null (plugin->external_hostname);
3242 GNUNET_free_non_null (plugin->ext_addr);
3243 GNUNET_free_non_null (plugin->server_addr_v4);
3244 GNUNET_free_non_null (plugin->server_addr_v6);
3245 regfree (&plugin->url_regex);
3247 LOG (GNUNET_ERROR_TYPE_DEBUG,
3248 _("Shutdown for plugin `%s' complete\n"),
3251 GNUNET_free (plugin);
3258 * Function called for a quick conversion of the binary address to
3259 * a numeric address. Note that the caller must not free the
3260 * address and that the next call to this function is allowed
3261 * to override the address again.
3264 * @param addr binary address
3265 * @param addrlen length of the address
3266 * @return string representing the same address
3269 http_server_plugin_address_to_string (void *cls,
3273 return http_common_plugin_address_to_string (PLUGIN_NAME,
3280 * Function obtain the network type for a session
3282 * @param cls closure (`struct HTTP_Server_Plugin *`)
3283 * @param session the session
3284 * @return the network type in HBO or #GNUNET_SYSERR
3286 static enum GNUNET_ATS_Network_Type
3287 http_server_plugin_get_network (void *cls,
3288 struct GNUNET_ATS_Session *session)
3290 return session->scope;
3295 * Function obtain the network type for an address.
3297 * @param cls closure (`struct Plugin *`)
3298 * @param address the address
3299 * @return the network type
3301 static enum GNUNET_ATS_Network_Type
3302 http_server_plugin_get_network_for_address (void *cls,
3303 const struct GNUNET_HELLO_Address *address)
3305 struct HTTP_Server_Plugin *plugin = cls;
3307 return http_common_get_network_for_address (plugin->env,
3313 * Function that will be called whenever the transport service wants to
3314 * notify the plugin that the inbound quota changed and that the plugin
3315 * should update it's delay for the next receive value
3317 * @param cls closure
3318 * @param peer which peer was the session for
3319 * @param session which session is being updated
3320 * @param delay new delay to use for receiving
3323 http_server_plugin_update_inbound_delay (void *cls,
3324 const struct GNUNET_PeerIdentity *peer,
3325 struct GNUNET_ATS_Session *session,
3326 struct GNUNET_TIME_Relative delay)
3328 session->next_receive = GNUNET_TIME_relative_to_absolute (delay);
3329 LOG (GNUNET_ERROR_TYPE_DEBUG,
3330 "New inbound delay %s\n",
3331 GNUNET_STRINGS_relative_time_to_string (delay,
3333 if (NULL != session->recv_wakeup_task)
3335 GNUNET_SCHEDULER_cancel (session->recv_wakeup_task);
3336 session->recv_wakeup_task
3337 = GNUNET_SCHEDULER_add_delayed (delay,
3345 * Return information about the given session to the
3348 * @param cls the `struct Plugin` with the monitor callback (`sic`)
3349 * @param peer peer we send information about
3350 * @param value our `struct GNUNET_ATS_Session` to send information about
3351 * @return #GNUNET_OK (continue to iterate)
3354 send_session_info_iter (void *cls,
3355 const struct GNUNET_PeerIdentity *peer,
3358 struct HTTP_Server_Plugin *plugin = cls;
3359 struct GNUNET_ATS_Session *session = value;
3361 notify_session_monitor (plugin,
3363 GNUNET_TRANSPORT_SS_INIT);
3369 * Begin monitoring sessions of a plugin. There can only
3370 * be one active monitor per plugin (i.e. if there are
3371 * multiple monitors, the transport service needs to
3372 * multiplex the generated events over all of them).
3374 * @param cls closure of the plugin
3375 * @param sic callback to invoke, NULL to disable monitor;
3376 * plugin will being by iterating over all active
3377 * sessions immediately and then enter monitor mode
3378 * @param sic_cls closure for @a sic
3381 http_server_plugin_setup_monitor (void *cls,
3382 GNUNET_TRANSPORT_SessionInfoCallback sic,
3385 struct HTTP_Server_Plugin *plugin = cls;
3388 plugin->sic_cls = sic_cls;
3391 GNUNET_CONTAINER_multipeermap_iterate (plugin->sessions,
3392 &send_session_info_iter,
3394 /* signal end of first iteration */
3395 sic (sic_cls, NULL, NULL);
3401 * Entry point for the plugin.
3407 LIBGNUNET_PLUGIN_TRANSPORT_INIT (void *cls)
3409 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
3410 struct GNUNET_TRANSPORT_PluginFunctions *api;
3411 struct HTTP_Server_Plugin *plugin;
3413 if (NULL == env->receive)
3415 /* run in 'stub' mode (i.e. as part of gnunet-peerinfo), don't fully
3416 initialze the plugin or the API */
3417 api = GNUNET_new (struct GNUNET_TRANSPORT_PluginFunctions);
3419 api->address_to_string = &http_server_plugin_address_to_string;
3420 api->string_to_address = &http_common_plugin_string_to_address;
3421 api->address_pretty_printer = &http_common_plugin_address_pretty_printer;
3424 plugin = GNUNET_new (struct HTTP_Server_Plugin);
3426 plugin->sessions = GNUNET_CONTAINER_multipeermap_create (128,
3429 api = GNUNET_new (struct GNUNET_TRANSPORT_PluginFunctions);
3431 api->send = &http_server_plugin_send;
3432 api->disconnect_peer = &http_server_plugin_disconnect_peer;
3433 api->disconnect_session = &http_server_plugin_disconnect_session;
3434 api->query_keepalive_factor = &http_server_query_keepalive_factor;
3435 api->check_address = &http_server_plugin_address_suggested;
3436 api->get_session = &http_server_plugin_get_session;
3438 api->address_to_string = &http_server_plugin_address_to_string;
3439 api->string_to_address = &http_common_plugin_string_to_address;
3440 api->address_pretty_printer = &http_common_plugin_address_pretty_printer;
3441 api->get_network = &http_server_plugin_get_network;
3442 api->get_network_for_address = &http_server_plugin_get_network_for_address;
3443 api->update_session_timeout = &http_server_plugin_update_session_timeout;
3444 api->update_inbound_delay = &http_server_plugin_update_inbound_delay;
3445 api->setup_monitor = &http_server_plugin_setup_monitor;
3447 plugin->name = "transport-https_server";
3448 plugin->protocol = "https";
3450 plugin->name = "transport-http_server";
3451 plugin->protocol = "http";
3455 GNUNET_CONFIGURATION_get_value_yesno (env->cfg,
3460 plugin->options |= HTTP_OPTIONS_TCP_STEALTH;
3462 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3463 _("TCP_STEALTH not supported on this platform.\n"));
3464 LIBGNUNET_PLUGIN_TRANSPORT_DONE (api);
3469 /* Compile URL regex */
3470 if (regcomp (&plugin->url_regex,
3474 LOG (GNUNET_ERROR_TYPE_ERROR,
3475 _("Unable to compile URL regex\n"));
3476 LIBGNUNET_PLUGIN_TRANSPORT_DONE (api);
3480 /* Configure plugin */
3481 if (GNUNET_SYSERR == server_configure_plugin (plugin))
3483 LIBGNUNET_PLUGIN_TRANSPORT_DONE (api);
3487 /* Check IPv6 support */
3488 if (GNUNET_YES == plugin->use_ipv6)
3489 plugin->use_ipv6 = server_check_ipv6_support (plugin);
3491 /* Report addresses to transport service */
3492 if (GNUNET_NO == plugin->external_only)
3493 server_start_report_addresses (plugin);
3495 if (GNUNET_SYSERR == server_start (plugin))
3497 LIBGNUNET_PLUGIN_TRANSPORT_DONE (api);
3503 /* end of plugin_transport_http_server.c */