2 This file is part of GNUnet
3 Copyright (C) 2002-2014 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_lib.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_SERVER_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, s->server_send->mhd_daemon, GNUNET_YES);
584 if (NULL != s->server_recv)
586 LOG (GNUNET_ERROR_TYPE_DEBUG,
587 "Server: %p / %p Terminating inbound GET session to peer `%s'\n",
588 s, s->server_recv, GNUNET_i2s (&s->target));
589 s->server_recv->session = NULL;
590 MHD_set_connection_option (s->server_recv->mhd_conn,
591 MHD_CONNECTION_OPTION_TIMEOUT,
592 1 /* 0 = no timeout, so this is MIN */);
593 server_reschedule (plugin, s->server_recv->mhd_daemon, GNUNET_YES);
595 notify_session_monitor (plugin,
597 GNUNET_TRANSPORT_SS_DONE);
598 if (GNUNET_YES == s->known_to_service)
600 plugin->env->session_end (plugin->env->cls,
603 s->known_to_service = GNUNET_NO;
605 if (NULL != s->msg_tk)
607 GNUNET_SERVER_mst_destroy (s->msg_tk);
610 GNUNET_HELLO_address_free (s->address);
611 LOG (GNUNET_ERROR_TYPE_DEBUG,
612 "Session %p destroyed\n",
620 * Disconnect session @a s by telling MHD to close the
621 * connections (reducing timeout, etc.).
623 * @param cls closure with the `struct HTTP_Server_Plugin`
624 * @param s the session
625 * @return #GNUNET_OK on success
628 http_server_plugin_disconnect_session (void *cls,
629 struct GNUNET_ATS_Session *s)
631 server_delete_session (s);
637 * Session was idle, so disconnect it
639 * @param cls the session
642 server_session_timeout (void *cls)
644 struct GNUNET_ATS_Session *s = cls;
645 struct GNUNET_TIME_Relative left;
647 s->timeout_task = NULL;
648 left = GNUNET_TIME_absolute_get_remaining (s->timeout);
649 if (0 != left.rel_value_us)
651 /* not actually our turn yet, but let's at least update
652 the monitor, it may think we're about to die ... */
653 notify_session_monitor (s->plugin,
655 GNUNET_TRANSPORT_SS_UP);
656 s->timeout_task = GNUNET_SCHEDULER_add_delayed (left,
657 &server_session_timeout,
661 GNUNET_log (TIMEOUT_LOG,
662 "Session %p was idle for %s, disconnecting\n",
664 GNUNET_STRINGS_relative_time_to_string (HTTP_SERVER_SESSION_TIMEOUT,
666 server_delete_session (s);
671 * Increment session timeout due to activity session @a s
673 * @param s the session
676 server_reschedule_session_timeout (struct GNUNET_ATS_Session *s)
678 GNUNET_assert (NULL != s->timeout_task);
679 s->timeout = GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
684 * Function that can be used by the transport service to transmit
685 * a message using the plugin. Note that in the case of a
686 * peer disconnecting, the continuation MUST be called
687 * prior to the disconnect notification itself. This function
688 * will be called with this peer's HELLO message to initiate
689 * a fresh connection to another peer.
692 * @param session which session must be used
693 * @param msgbuf the message to transmit
694 * @param msgbuf_size number of bytes in @a msgbuf
695 * @param priority how important is the message (most plugins will
696 * ignore message priority and just FIFO)
697 * @param to how long to wait at most for the transmission (does not
698 * require plugins to discard the message after the timeout,
699 * just advisory for the desired delay; most plugins will ignore
701 * @param cont continuation to call once the message has
702 * been transmitted (or if the transport is ready
703 * for the next transmission call; or if the
704 * peer disconnected...); can be NULL
705 * @param cont_cls closure for @a cont
706 * @return number of bytes used (on the physical network, with overheads);
707 * -1 on hard errors (i.e. address invalid); 0 is a legal value
708 * and does NOT mean that the message was not transmitted (DV)
711 http_server_plugin_send (void *cls,
712 struct GNUNET_ATS_Session *session,
715 unsigned int priority,
716 struct GNUNET_TIME_Relative to,
717 GNUNET_TRANSPORT_TransmitContinuation cont,
720 struct HTTP_Server_Plugin *plugin = cls;
721 struct HTTP_Message *msg;
722 ssize_t bytes_sent = 0;
725 LOG (GNUNET_ERROR_TYPE_DEBUG,
726 "Session %p/request %p: Sending message with %u to peer `%s'\n",
728 session->server_send,
730 GNUNET_i2s (&session->target));
732 /* create new message and schedule */
733 bytes_sent = sizeof (struct HTTP_Message) + msgbuf_size;
734 msg = GNUNET_malloc (bytes_sent);
736 msg->size = msgbuf_size;
738 msg->buf = (char *) &msg[1];
739 msg->transmit_cont = cont;
740 msg->transmit_cont_cls = cont_cls;
741 GNUNET_memcpy (msg->buf,
744 GNUNET_CONTAINER_DLL_insert_tail (session->msg_head,
747 session->msgs_in_queue++;
748 session->bytes_in_queue += msg->size;
749 notify_session_monitor (plugin,
751 GNUNET_TRANSPORT_SS_UP);
752 GNUNET_asprintf (&stat_txt,
753 "# bytes currently in %s_server buffers",
755 GNUNET_STATISTICS_update (plugin->env->stats,
756 stat_txt, msgbuf_size, GNUNET_NO);
757 GNUNET_free (stat_txt);
759 if (NULL != session->server_send)
760 server_reschedule (session->plugin,
761 session->server_send->mhd_daemon,
768 * Terminate session during shutdown.
770 * @param cls the `struct HTTP_Server_Plugin *`
771 * @param peer for which this is a session
772 * @param value the `struct GNUNET_ATS_Session` to clean up
773 * @return #GNUNET_OK (continue to iterate)
776 destroy_session_shutdown_cb (void *cls,
777 const struct GNUNET_PeerIdentity *peer,
780 struct GNUNET_ATS_Session *s = value;
781 struct ServerRequest *sc_send;
782 struct ServerRequest *sc_recv;
784 sc_send = s->server_send;
785 sc_recv = s->server_recv;
786 server_delete_session (s);
788 GNUNET_free_non_null (sc_send);
789 GNUNET_free_non_null (sc_recv);
797 * @param cls the `struct HTTP_Server_Plugin *`
798 * @param peer for which this is a session
799 * @param value the `struct GNUNET_ATS_Session` to clean up
800 * @return #GNUNET_OK (continue to iterate)
803 destroy_session_cb (void *cls,
804 const struct GNUNET_PeerIdentity *peer,
807 struct GNUNET_ATS_Session *s = value;
809 server_delete_session (s);
814 * Function that can be used to force the plugin to disconnect
815 * from the given peer and cancel all previous transmissions
816 * (and their continuationc).
819 * @param target peer from which to disconnect
822 http_server_plugin_disconnect_peer (void *cls,
823 const struct GNUNET_PeerIdentity *target)
825 struct HTTP_Server_Plugin *plugin = cls;
827 LOG (GNUNET_ERROR_TYPE_DEBUG,
828 "Transport tells me to disconnect `%s'\n",
829 GNUNET_i2s (target));
830 GNUNET_CONTAINER_multipeermap_get_multiple (plugin->sessions,
838 * Another peer has suggested an address for this
839 * peer and transport plugin. Check that this could be a valid
840 * address. If so, consider adding it to the list
844 * @param addr pointer to the address
845 * @param addrlen length of @a addr
846 * @return #GNUNET_OK if this is a plausible address for this peer
850 http_server_plugin_address_suggested (void *cls,
854 struct HTTP_Server_Plugin *plugin = cls;
855 struct HttpAddressWrapper *next;
856 struct HttpAddressWrapper *pos;
857 const struct HttpAddress *haddr = addr;
859 if ((NULL != plugin->ext_addr) &&
860 GNUNET_YES == (http_common_cmp_addresses (addr, addrlen,
861 plugin->ext_addr->address,
862 plugin->ext_addr->address_length)))
864 /* Checking HTTP_OPTIONS_VERIFY_CERTIFICATE option for external hostname */
865 if ((ntohl (haddr->options) & HTTP_OPTIONS_VERIFY_CERTIFICATE) !=
866 (plugin->options & HTTP_OPTIONS_VERIFY_CERTIFICATE))
867 return GNUNET_NO; /* VERIFY option not set as required! */
870 next = plugin->addr_head;
871 while (NULL != (pos = next))
874 if (GNUNET_YES == (http_common_cmp_addresses(addr,
885 * Creates a new outbound session the transport
886 * service will use to send data to the peer.
888 * Since HTTP/S server cannot create sessions, always returns NULL.
890 * @param cls the plugin
891 * @param address the address
892 * @return always NULL
894 static struct GNUNET_ATS_Session *
895 http_server_plugin_get_session (void *cls,
896 const struct GNUNET_HELLO_Address *address)
903 * Call MHD IPv4 to process pending requests and then go back
904 * and schedule the next run.
906 * @param cls plugin as closure
909 server_v4_run (void *cls)
911 struct HTTP_Server_Plugin *plugin = cls;
913 plugin->server_v4_task = NULL;
914 plugin->server_v4_immediately = GNUNET_NO;
915 GNUNET_assert (MHD_YES == MHD_run (plugin->server_v4));
916 server_reschedule (plugin, plugin->server_v4, GNUNET_NO);
921 * Call MHD IPv6 to process pending requests and then go back
922 * and schedule the next run.
924 * @param cls plugin as closure
927 server_v6_run (void *cls)
929 struct HTTP_Server_Plugin *plugin = cls;
931 plugin->server_v6_task = NULL;
932 plugin->server_v6_immediately = GNUNET_NO;
933 GNUNET_assert (MHD_YES == MHD_run (plugin->server_v6));
934 server_reschedule (plugin, plugin->server_v6, GNUNET_NO);
939 * Function that queries MHD's select sets and
940 * starts the task waiting for them.
942 * @param plugin plugin
943 * @param daemon_handle the MHD daemon handle
944 * @param now schedule now
945 * @return gnunet task identifier
947 static struct GNUNET_SCHEDULER_Task *
948 server_schedule (struct HTTP_Server_Plugin *plugin,
949 struct MHD_Daemon *daemon_handle,
952 struct GNUNET_SCHEDULER_Task * ret;
956 struct GNUNET_NETWORK_FDSet *wrs;
957 struct GNUNET_NETWORK_FDSet *wws;
959 MHD_UNSIGNED_LONG_LONG timeout;
960 static unsigned long long last_timeout = 0;
962 struct GNUNET_TIME_Relative tv;
964 if (GNUNET_YES == plugin->in_shutdown)
971 wrs = GNUNET_NETWORK_fdset_create ();
972 wws = GNUNET_NETWORK_fdset_create ();
974 GNUNET_assert (MHD_YES ==
975 MHD_get_fdset (daemon_handle,
980 haveto = MHD_get_timeout (daemon_handle, &timeout);
981 if (haveto == MHD_YES)
983 if (timeout != last_timeout)
985 LOG (GNUNET_ERROR_TYPE_DEBUG,
986 "SELECT Timeout changed from %llu to %llu (ms)\n",
987 last_timeout, timeout);
988 last_timeout = timeout;
990 if (timeout <= GNUNET_TIME_UNIT_SECONDS.rel_value_us / 1000LL)
991 tv.rel_value_us = (uint64_t) timeout * 1000LL;
993 tv = GNUNET_TIME_UNIT_SECONDS;
996 tv = GNUNET_TIME_UNIT_SECONDS;
997 /* Force immediate run, since we have outbound data to send */
998 if (now == GNUNET_YES)
999 tv = GNUNET_TIME_UNIT_MILLISECONDS;
1000 GNUNET_NETWORK_fdset_copy_native (wrs, &rs, max + 1);
1001 GNUNET_NETWORK_fdset_copy_native (wws, &ws, max + 1);
1003 if (daemon_handle == plugin->server_v4)
1005 if (plugin->server_v4_task != NULL)
1007 GNUNET_SCHEDULER_cancel (plugin->server_v4_task);
1008 plugin->server_v4_task = NULL;
1011 LOG (GNUNET_ERROR_TYPE_DEBUG,
1012 "Scheduling IPv4 server task in %llu ms\n",
1016 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1018 &server_v4_run, plugin);
1020 if (daemon_handle == plugin->server_v6)
1022 if (plugin->server_v6_task != NULL)
1024 GNUNET_SCHEDULER_cancel (plugin->server_v6_task);
1025 plugin->server_v6_task = NULL;
1028 LOG (GNUNET_ERROR_TYPE_DEBUG,
1029 "Scheduling IPv6 server task in %llu ms\n", tv);
1032 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1034 &server_v6_run, plugin);
1036 GNUNET_NETWORK_fdset_destroy (wrs);
1037 GNUNET_NETWORK_fdset_destroy (wws);
1043 * Reschedule the execution of both IPv4 and IPv6 server
1045 * @param plugin the plugin
1046 * @param server which server to schedule v4 or v6?
1047 * @param now #GNUNET_YES to schedule execution immediately, #GNUNET_NO to wait
1051 server_reschedule (struct HTTP_Server_Plugin *plugin,
1052 struct MHD_Daemon *server,
1055 if ((server == plugin->server_v4) && (plugin->server_v4 != NULL))
1057 if (GNUNET_YES == plugin->server_v4_immediately)
1058 return; /* No rescheduling, server will run asap */
1060 if (GNUNET_YES == now)
1061 plugin->server_v4_immediately = GNUNET_YES;
1063 if (plugin->server_v4_task != NULL)
1065 GNUNET_SCHEDULER_cancel (plugin->server_v4_task);
1066 plugin->server_v4_task = NULL;
1068 plugin->server_v4_task = server_schedule (plugin, plugin->server_v4, now);
1071 if ((server == plugin->server_v6) && (plugin->server_v6 != NULL))
1073 if (GNUNET_YES == plugin->server_v6_immediately)
1074 return; /* No rescheduling, server will run asap */
1076 if (GNUNET_YES == now)
1077 plugin->server_v6_immediately = GNUNET_YES;
1079 if (plugin->server_v6_task != NULL)
1081 GNUNET_SCHEDULER_cancel (plugin->server_v6_task);
1082 plugin->server_v6_task = NULL;
1084 plugin->server_v6_task = server_schedule (plugin, plugin->server_v6, now);
1090 * Function that is called to get the keepalive factor.
1091 * GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT is divided by this number to
1092 * calculate the interval between keepalive packets.
1094 * @param cls closure with the `struct HTTP_Server_Plugin`
1095 * @return keepalive factor
1098 http_server_query_keepalive_factor (void *cls)
1105 * Function that will be called whenever the transport service wants to
1106 * notify the plugin that a session is still active and in use and
1107 * therefore the session timeout for this session has to be updated
1109 * @param cls closure
1110 * @param peer which peer was the session for
1111 * @param session which session is being updated
1114 http_server_plugin_update_session_timeout (void *cls,
1115 const struct GNUNET_PeerIdentity *peer,
1116 struct GNUNET_ATS_Session *session)
1118 server_reschedule_session_timeout (session);
1123 * Tell MHD that the connection should timeout after @a to seconds.
1125 * @param plugin our plugin
1126 * @param s session for which the timeout changes
1127 * @param to timeout in seconds
1130 server_mhd_connection_timeout (struct HTTP_Server_Plugin *plugin,
1131 struct GNUNET_ATS_Session *s,
1134 /* Setting timeouts for other connections */
1135 if (NULL != s->server_recv)
1137 LOG (GNUNET_ERROR_TYPE_DEBUG,
1138 "Setting timeout for %p to %u sec.\n",
1139 s->server_recv, to);
1140 MHD_set_connection_option (s->server_recv->mhd_conn,
1141 MHD_CONNECTION_OPTION_TIMEOUT,
1143 server_reschedule (plugin, s->server_recv->mhd_daemon, GNUNET_NO);
1145 if (NULL != s->server_send)
1147 LOG (GNUNET_ERROR_TYPE_DEBUG,
1148 "Setting timeout for %p to %u sec.\n",
1149 s->server_send, to);
1150 MHD_set_connection_option (s->server_send->mhd_conn,
1151 MHD_CONNECTION_OPTION_TIMEOUT,
1153 server_reschedule (plugin, s->server_send->mhd_daemon, GNUNET_NO);
1159 * Parse incoming URL for tag and target
1161 * @param plugin plugin
1162 * @param url incoming url
1163 * @param target where to store the target
1164 * @param tag where to store the tag
1165 * @param options where to store the options
1166 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
1169 server_parse_url (struct HTTP_Server_Plugin *plugin,
1171 struct GNUNET_PeerIdentity *target,
1175 regmatch_t matches[4];
1176 const char *tag_start;
1177 const char *target_start;
1181 unsigned long int rc;
1185 ("^.*/([0-9A-Z]+);([0-9]+)(,[0-9]+)?$")
1190 return GNUNET_SYSERR;
1193 if (regexec(&plugin->url_regex, url, 4, matches, 0))
1195 LOG (GNUNET_ERROR_TYPE_DEBUG,
1196 "URL `%s' did not match regex\n", url);
1197 return GNUNET_SYSERR;
1200 target_start = &url[matches[1].rm_so];
1201 tag_start = &url[matches[2].rm_so];
1204 rc = strtoul (tag_start, &tag_end, 10);
1205 if (&url[matches[2].rm_eo] != tag_end)
1207 LOG (GNUNET_ERROR_TYPE_DEBUG,
1208 "URL tag did not line up with submatch\n");
1209 return GNUNET_SYSERR;
1213 LOG (GNUNET_ERROR_TYPE_DEBUG,
1214 "URL tag is zero\n");
1215 return GNUNET_SYSERR;
1217 if ((rc == ULONG_MAX) && (ERANGE == errno))
1219 LOG (GNUNET_ERROR_TYPE_DEBUG,
1220 "URL tag > ULONG_MAX\n");
1221 return GNUNET_SYSERR;
1223 if (rc > UINT32_MAX)
1225 LOG (GNUNET_ERROR_TYPE_DEBUG,
1226 "URL tag > UINT32_MAX\n");
1227 return GNUNET_SYSERR;
1229 (*tag) = (uint32_t)rc;
1230 LOG (GNUNET_ERROR_TYPE_DEBUG,
1231 "Found tag `%u' in url\n",
1234 /* convert peer id */
1235 hash_length = matches[1].rm_eo - matches[1].rm_so;
1236 if (hash_length != plugin->peer_id_length)
1238 LOG (GNUNET_ERROR_TYPE_DEBUG,
1239 "URL target is %u bytes, expecting %u\n",
1240 hash_length, plugin->peer_id_length);
1241 return GNUNET_SYSERR;
1244 GNUNET_CRYPTO_eddsa_public_key_from_string (target_start,
1246 &target->public_key))
1248 LOG (GNUNET_ERROR_TYPE_DEBUG,
1249 "URL target conversion failed\n");
1250 return GNUNET_SYSERR;
1252 LOG (GNUNET_ERROR_TYPE_DEBUG,
1253 "Found target `%s' in URL\n",
1254 GNUNET_i2s_full (target));
1256 /* convert options */
1257 if (-1 == matches[3].rm_so)
1263 rc = strtoul (&url[matches[3].rm_so + 1], &options_end, 10);
1264 if (&url[matches[3].rm_eo] != options_end)
1266 LOG (GNUNET_ERROR_TYPE_DEBUG,
1267 "URL options did not line up with submatch\n");
1268 return GNUNET_SYSERR;
1270 if ((rc == ULONG_MAX) && (ERANGE == errno))
1272 LOG (GNUNET_ERROR_TYPE_DEBUG,
1273 "URL options > ULONG_MAX\n");
1274 return GNUNET_SYSERR;
1276 if (rc > UINT32_MAX)
1278 LOG (GNUNET_ERROR_TYPE_DEBUG,
1279 "URL options > UINT32_MAX\n");
1280 return GNUNET_SYSERR;
1282 (*options) = (uint32_t) rc;
1283 LOG (GNUNET_ERROR_TYPE_DEBUG,
1284 "Found options `%u' in url\n",
1292 * Closure for #session_tag_it().
1294 struct GNUNET_ATS_SessionTagContext
1297 * Set to session matching the tag.
1299 struct GNUNET_ATS_Session *res;
1302 * Tag we are looking for.
1309 * Find a session with a matching tag.
1311 * @param cls the `struct GNUNET_ATS_SessionTagContext *`
1312 * @param key peer identity (unused)
1313 * @param value the `struct GNUNET_ATS_Session *`
1314 * @return #GNUNET_NO if we found the session, #GNUNET_OK if not
1317 session_tag_it (void *cls,
1318 const struct GNUNET_PeerIdentity *key,
1321 struct GNUNET_ATS_SessionTagContext *stc = cls;
1322 struct GNUNET_ATS_Session *s = value;
1324 if (s->tag == stc->tag)
1334 * Lookup a mhd connection and create one if none is found
1336 * @param plugin the plugin handle
1337 * @param mhd_connection the incoming mhd_connection
1338 * @param url incoming requested URL
1339 * @param method PUT or GET
1340 * @return the server connecetion
1342 static struct ServerRequest *
1343 server_lookup_connection (struct HTTP_Server_Plugin *plugin,
1344 struct MHD_Connection *mhd_connection,
1348 struct GNUNET_ATS_Session *s = NULL;
1349 struct ServerRequest *sc = NULL;
1350 const union MHD_ConnectionInfo *conn_info;
1351 struct HttpAddress *addr;
1352 struct GNUNET_PeerIdentity target;
1354 struct GNUNET_ATS_SessionTagContext stc;
1356 int direction = GNUNET_SYSERR;
1358 enum GNUNET_ATS_Network_Type scope;
1360 conn_info = MHD_get_connection_info (mhd_connection,
1361 MHD_CONNECTION_INFO_CLIENT_ADDRESS);
1362 if ((conn_info->client_addr->sa_family != AF_INET) &&
1363 (conn_info->client_addr->sa_family != AF_INET6))
1365 LOG (GNUNET_ERROR_TYPE_DEBUG,
1366 "New %s request from %s\n",
1370 options = 0; /* make gcc happy */
1371 if (GNUNET_SYSERR ==
1372 server_parse_url (plugin, url, &target, &stc.tag, &options))
1374 LOG (GNUNET_ERROR_TYPE_DEBUG,
1375 "Invalid url %s\n", url);
1378 if (0 == strcmp (MHD_HTTP_METHOD_PUT, method))
1379 direction = _RECEIVE;
1380 else if (0 == strcmp (MHD_HTTP_METHOD_GET, method))
1384 LOG (GNUNET_ERROR_TYPE_DEBUG,
1385 "Invalid method %s for request from %s\n",
1390 plugin->cur_request++;
1391 LOG (GNUNET_ERROR_TYPE_DEBUG,
1392 "New %s request from %s with tag %u (%u of %u)\n",
1394 GNUNET_i2s (&target),
1396 plugin->cur_request, plugin->max_request);
1397 /* find existing session */
1399 GNUNET_CONTAINER_multipeermap_get_multiple (plugin->sessions,
1403 if (NULL == (s = stc.res))
1405 /* create new session */
1407 switch (conn_info->client_addr->sa_family)
1410 addr = http_common_address_from_socket (plugin->protocol,
1411 conn_info->client_addr,
1412 sizeof (struct sockaddr_in));
1413 addr_len = http_common_address_get_size (addr);
1414 scope = plugin->env->get_address_type (plugin->env->cls,
1415 conn_info->client_addr,
1416 sizeof (struct sockaddr_in));
1419 addr = http_common_address_from_socket (plugin->protocol,
1420 conn_info->client_addr,
1421 sizeof (struct sockaddr_in6));
1422 addr_len = http_common_address_get_size (addr);
1423 scope = plugin->env->get_address_type (plugin->env->cls,
1424 conn_info->client_addr,
1425 sizeof (struct sockaddr_in6));
1428 /* external host name */
1431 s = GNUNET_new (struct GNUNET_ATS_Session);
1435 s->address = GNUNET_HELLO_address_allocate (&s->target,
1439 GNUNET_HELLO_ADDRESS_INFO_INBOUND);
1440 s->next_receive = GNUNET_TIME_UNIT_ZERO_ABS;
1442 s->timeout = GNUNET_TIME_relative_to_absolute (HTTP_SERVER_SESSION_TIMEOUT);
1443 s->timeout_task = GNUNET_SCHEDULER_add_delayed (HTTP_SERVER_SESSION_TIMEOUT,
1444 &server_session_timeout,
1446 (void) GNUNET_CONTAINER_multipeermap_put (plugin->sessions,
1449 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
1450 notify_session_monitor (plugin,
1452 GNUNET_TRANSPORT_SS_INIT);
1453 notify_session_monitor (plugin,
1455 GNUNET_TRANSPORT_SS_HANDSHAKE);
1456 LOG (GNUNET_ERROR_TYPE_DEBUG,
1457 "Creating new session %p for peer `%s' connecting from `%s'\n",
1458 s, GNUNET_i2s (&target),
1459 http_common_plugin_address_to_string (plugin->protocol,
1462 GNUNET_free_non_null (addr);
1465 if ( (_RECEIVE == direction) &&
1466 (NULL != s->server_recv) )
1468 LOG (GNUNET_ERROR_TYPE_DEBUG,
1469 "Duplicate PUT request from `%s' tag %u, dismissing new request\n",
1470 GNUNET_i2s (&target),
1474 if ((_SEND == direction) && (NULL != s->server_send))
1476 LOG (GNUNET_ERROR_TYPE_DEBUG,
1477 "Duplicate GET request from `%s' tag %u, dismissing new request\n",
1478 GNUNET_i2s (&target),
1482 sc = GNUNET_new (struct ServerRequest);
1483 if (conn_info->client_addr->sa_family == AF_INET)
1484 sc->mhd_daemon = plugin->server_v4;
1485 if (conn_info->client_addr->sa_family == AF_INET6)
1486 sc->mhd_daemon = plugin->server_v6;
1487 sc->mhd_conn = mhd_connection;
1488 sc->direction = direction;
1489 sc->connected = GNUNET_NO;
1491 sc->options = options;
1492 if (direction == _SEND)
1494 s->server_send = sc;
1496 if (direction == _RECEIVE)
1498 s->server_recv = sc;
1501 if ((GNUNET_NO == s->known_to_service) &&
1502 (NULL != s->server_send) &&
1503 (NULL != s->server_recv) )
1505 s->known_to_service = GNUNET_YES;
1506 notify_session_monitor (plugin,
1508 GNUNET_TRANSPORT_SS_UP);
1509 plugin->env->session_start (plugin->env->cls,
1515 to = (HTTP_SERVER_SESSION_TIMEOUT.rel_value_us / 1000LL / 1000LL);
1516 server_mhd_connection_timeout (plugin, s, to);
1522 * Callback called by MHD when it needs data to send
1524 * @param cls current session
1525 * @param pos position in buffer
1526 * @param buf the buffer to write data to
1527 * @param max max number of bytes available in @a buf
1528 * @return bytes written to @a buf
1531 server_send_callback (void *cls,
1536 struct ServerRequest *sc = cls;
1537 struct GNUNET_ATS_Session *s = sc->session;
1538 ssize_t bytes_read = 0;
1539 struct HTTP_Message *msg;
1544 /* session is disconnecting */
1548 sc = s->server_send;
1555 bytes_read = GNUNET_MIN (msg->size - msg->pos,
1557 GNUNET_memcpy (buf, &msg->buf[msg->pos], bytes_read);
1558 msg->pos += bytes_read;
1560 /* removing message */
1561 if (msg->pos == msg->size)
1563 GNUNET_CONTAINER_DLL_remove (s->msg_head,
1566 if (NULL != msg->transmit_cont)
1567 msg->transmit_cont (msg->transmit_cont_cls, &s->target, GNUNET_OK,
1568 msg->size, msg->size + msg->overhead);
1569 GNUNET_assert (s->msgs_in_queue > 0);
1571 GNUNET_assert (s->bytes_in_queue >= msg->size);
1572 s->bytes_in_queue -= msg->size;
1574 notify_session_monitor (s->plugin,
1576 GNUNET_TRANSPORT_SS_UPDATE);
1581 sc->connected = GNUNET_YES;
1582 LOG (GNUNET_ERROR_TYPE_DEBUG,
1583 "Sent %u bytes to peer `%s' with session %p \n",
1585 GNUNET_i2s (&s->target),
1587 GNUNET_asprintf (&stat_txt,
1588 "# bytes currently in %s_server buffers",
1589 s->plugin->protocol);
1590 GNUNET_STATISTICS_update (s->plugin->env->stats,
1594 GNUNET_free (stat_txt);
1595 GNUNET_asprintf (&stat_txt,
1596 "# bytes transmitted via %s_server",
1597 s->plugin->protocol);
1598 GNUNET_STATISTICS_update (s->plugin->env->stats,
1599 stat_txt, bytes_read, GNUNET_NO);
1600 GNUNET_free (stat_txt);
1602 else if ((sc->options & OPTION_LONG_POLL) && sc->connected)
1604 LOG (GNUNET_ERROR_TYPE_DEBUG,
1605 "Completing GET response to peer `%s' with session %p\n",
1606 GNUNET_i2s (&s->target),
1608 return MHD_CONTENT_READER_END_OF_STREAM;
1615 * Callback called by MessageStreamTokenizer when a message has arrived
1617 * @param cls current session as closure
1618 * @param client client
1619 * @param message the message to be forwarded to transport service
1620 * @return #GNUNET_OK
1623 server_receive_mst_cb (void *cls,
1625 const struct GNUNET_MessageHeader *message)
1627 struct GNUNET_ATS_Session *s = cls;
1628 struct HTTP_Server_Plugin *plugin = s->plugin;
1629 struct GNUNET_TIME_Relative delay;
1632 if (GNUNET_NO == s->known_to_service)
1634 s->known_to_service = GNUNET_YES;
1635 plugin->env->session_start (plugin->env->cls,
1639 notify_session_monitor (plugin,
1641 GNUNET_TRANSPORT_SS_UP);
1643 delay = plugin->env->receive (plugin->env->cls,
1647 GNUNET_asprintf (&stat_txt,
1648 "# bytes received via %s_server",
1650 GNUNET_STATISTICS_update (plugin->env->stats,
1651 stat_txt, ntohs (message->size), GNUNET_NO);
1652 GNUNET_free (stat_txt);
1653 s->next_receive = GNUNET_TIME_relative_to_absolute (delay);
1654 if (delay.rel_value_us > 0)
1656 LOG (GNUNET_ERROR_TYPE_DEBUG,
1657 "Peer `%s' address `%s' next read delayed for %s\n",
1658 GNUNET_i2s (&s->target),
1659 http_common_plugin_address_to_string (plugin->protocol,
1660 s->address->address,
1661 s->address->address_length),
1662 GNUNET_STRINGS_relative_time_to_string (delay,
1665 server_reschedule_session_timeout (s);
1671 * Add headers to a request indicating that we allow Cross-Origin Resource
1674 * @param response response object to modify
1677 add_cors_headers(struct MHD_Response *response)
1679 MHD_add_response_header (response,
1680 "Access-Control-Allow-Origin",
1682 MHD_add_response_header (response,
1683 "Access-Control-Allow-Methods",
1684 "GET, PUT, OPTIONS");
1685 MHD_add_response_header (response,
1686 "Access-Control-Max-Age",
1692 * MHD callback for a new incoming connection
1694 * @param cls the plugin handle
1695 * @param mhd_connection the mhd connection
1696 * @param url the requested URL
1697 * @param method GET or PUT
1698 * @param version HTTP version
1699 * @param upload_data upload data
1700 * @param upload_data_size size of @a upload_data
1701 * @param httpSessionCache the session cache to remember the connection
1702 * @return MHD_YES if connection is accepted, MHD_NO on reject
1705 server_access_cb (void *cls,
1706 struct MHD_Connection *mhd_connection,
1709 const char *version,
1710 const char *upload_data,
1711 size_t *upload_data_size,
1712 void **httpSessionCache)
1714 struct HTTP_Server_Plugin *plugin = cls;
1715 struct ServerRequest *sc = *httpSessionCache;
1716 struct GNUNET_ATS_Session *s;
1717 struct MHD_Response *response;
1720 LOG (GNUNET_ERROR_TYPE_DEBUG,
1721 _("Access from connection %p (%u of %u) for `%s' `%s' url `%s' with upload data size %u\n"),
1723 plugin->cur_request,
1724 plugin->max_request,
1728 (*upload_data_size));
1731 /* CORS pre-flight request */
1732 if (0 == strcmp (MHD_HTTP_METHOD_OPTIONS, method))
1734 response = MHD_create_response_from_buffer (0, NULL,
1735 MHD_RESPMEM_PERSISTENT);
1736 add_cors_headers(response);
1737 res = MHD_queue_response (mhd_connection, MHD_HTTP_OK, response);
1738 MHD_destroy_response (response);
1741 /* new connection */
1742 sc = server_lookup_connection (plugin, mhd_connection, url, method);
1745 /* attach to new / existing session */
1746 (*httpSessionCache) = sc;
1750 /* existing session already has matching connection, refuse */
1751 response = MHD_create_response_from_buffer (strlen (HTTP_ERROR_RESPONSE),
1752 HTTP_ERROR_RESPONSE,
1753 MHD_RESPMEM_PERSISTENT);
1754 MHD_add_response_header (response,
1755 MHD_HTTP_HEADER_CONTENT_TYPE,
1757 add_cors_headers(response);
1758 res = MHD_queue_response (mhd_connection, MHD_HTTP_NOT_FOUND, response);
1759 MHD_destroy_response (response);
1763 /* 'old' connection */
1764 if (NULL == (s = sc->session))
1766 /* Session was already disconnected;
1767 sent HTTP/1.1: 200 OK as response */
1768 response = MHD_create_response_from_buffer (strlen ("Thank you!"),
1770 MHD_RESPMEM_PERSISTENT);
1771 add_cors_headers(response);
1772 MHD_queue_response (mhd_connection, MHD_HTTP_OK, response);
1773 MHD_destroy_response (response);
1777 if (sc->direction == _SEND)
1779 response = MHD_create_response_from_callback (MHD_SIZE_UNKNOWN, 32 * 1024,
1780 &server_send_callback, sc, NULL);
1781 add_cors_headers(response);
1782 MHD_queue_response (mhd_connection, MHD_HTTP_OK, response);
1783 MHD_destroy_response (response);
1786 if (sc->direction == _RECEIVE)
1788 if ((*upload_data_size == 0) && (sc->connected == GNUNET_NO))
1790 /* (*upload_data_size == 0) first callback when header are passed */
1791 LOG (GNUNET_ERROR_TYPE_DEBUG,
1792 "Session %p / Connection %p: Peer `%s' PUT on address `%s' connected\n",
1794 GNUNET_i2s (&s->target),
1795 http_common_plugin_address_to_string (plugin->protocol,
1796 s->address->address,
1797 s->address->address_length));
1798 sc->connected = GNUNET_YES;
1801 else if ((*upload_data_size == 0) && (sc->connected == GNUNET_YES))
1803 /* (*upload_data_size == 0) when upload is complete */
1804 LOG (GNUNET_ERROR_TYPE_DEBUG,
1805 "Session %p / Connection %p: Peer `%s' PUT on address `%s' finished upload\n",
1807 GNUNET_i2s (&s->target),
1808 http_common_plugin_address_to_string (plugin->protocol,
1809 s->address->address,
1810 s->address->address_length));
1811 sc->connected = GNUNET_NO;
1812 /* Sent HTTP/1.1: 200 OK as PUT Response\ */
1813 response = MHD_create_response_from_buffer (strlen ("Thank you!"),
1815 MHD_RESPMEM_PERSISTENT);
1816 add_cors_headers(response);
1817 MHD_queue_response (mhd_connection, MHD_HTTP_OK, response);
1818 MHD_destroy_response (response);
1821 else if ((*upload_data_size > 0) && (sc->connected == GNUNET_YES))
1823 struct GNUNET_TIME_Relative delay;
1825 /* (*upload_data_size > 0) for every segment received */
1826 LOG (GNUNET_ERROR_TYPE_DEBUG,
1827 "Session %p / Connection %p: Peer `%s' PUT on address `%s' received %u bytes\n",
1829 GNUNET_i2s (&s->target),
1830 http_common_plugin_address_to_string (plugin->protocol,
1831 s->address->address,
1832 s->address->address_length),
1834 delay = GNUNET_TIME_absolute_get_remaining (s->next_receive);
1835 if (0 == delay.rel_value_us)
1837 LOG (GNUNET_ERROR_TYPE_DEBUG,
1838 "PUT with %u bytes forwarded to MST\n",
1840 if (s->msg_tk == NULL)
1842 s->msg_tk = GNUNET_SERVER_mst_create (&server_receive_mst_cb, s);
1844 GNUNET_SERVER_mst_receive (s->msg_tk, s, upload_data, *upload_data_size,
1845 GNUNET_NO, GNUNET_NO);
1846 server_mhd_connection_timeout (plugin, s,
1847 GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT.rel_value_us / 1000LL
1849 (*upload_data_size) = 0;
1853 /* delay processing */
1854 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1855 "Session %p / Connection %p: no inbound bandwidth available! Next read was delayed by %s\n",
1858 GNUNET_STRINGS_relative_time_to_string (delay,
1860 GNUNET_assert(s->server_recv->mhd_conn == mhd_connection);
1861 MHD_suspend_connection (s->server_recv->mhd_conn);
1862 if (NULL == s->recv_wakeup_task)
1864 = GNUNET_SCHEDULER_add_delayed (delay,
1881 * Callback from MHD when a connection disconnects
1883 * @param cls closure with the `struct HTTP_Server_Plugin *`
1884 * @param connection the disconnected MHD connection
1885 * @param httpSessionCache the pointer to distinguish
1888 server_disconnect_cb (void *cls,
1889 struct MHD_Connection *connection,
1890 void **httpSessionCache)
1892 struct HTTP_Server_Plugin *plugin = cls;
1893 struct ServerRequest *sc = *httpSessionCache;
1895 LOG (GNUNET_ERROR_TYPE_DEBUG,
1896 "Disconnect for connection %p\n",
1900 /* CORS pre-flight request finished */
1904 if (NULL != sc->session)
1906 if (sc->direction == _SEND)
1908 LOG (GNUNET_ERROR_TYPE_DEBUG,
1909 "Peer `%s' connection %p, GET on address `%s' disconnected\n",
1910 GNUNET_i2s (&sc->session->target),
1911 sc->session->server_send,
1912 http_common_plugin_address_to_string (plugin->protocol,
1913 sc->session->address->address,
1914 sc->session->address->address_length));
1916 sc->session->server_send = NULL;
1918 else if (sc->direction == _RECEIVE)
1920 LOG (GNUNET_ERROR_TYPE_DEBUG,
1921 "Peer `%s' connection %p PUT on address `%s' disconnected\n",
1922 GNUNET_i2s (&sc->session->target),
1923 sc->session->server_recv,
1924 http_common_plugin_address_to_string (plugin->protocol,
1925 sc->session->address->address,
1926 sc->session->address->address_length));
1927 sc->session->server_recv = NULL;
1928 if (NULL != sc->session->msg_tk)
1930 GNUNET_SERVER_mst_destroy (sc->session->msg_tk);
1931 sc->session->msg_tk = NULL;
1936 plugin->cur_request--;
1941 * Check if incoming connection is accepted.
1943 * @param cls plugin as closure
1944 * @param addr address of incoming connection
1945 * @param addr_len number of bytes in @a addr
1946 * @return MHD_YES if connection is accepted, MHD_NO if connection is rejected
1949 server_accept_cb (void *cls,
1950 const struct sockaddr *addr,
1953 struct HTTP_Server_Plugin *plugin = cls;
1955 if (plugin->cur_request <= plugin->max_request)
1957 LOG (GNUNET_ERROR_TYPE_DEBUG,
1958 _("Accepting connection (%u of %u) from `%s'\n"),
1959 plugin->cur_request, plugin->max_request,
1960 GNUNET_a2s (addr, addr_len));
1965 LOG (GNUNET_ERROR_TYPE_WARNING,
1966 _("Server reached maximum number connections (%u), rejecting new connection\n"),
1967 plugin->max_request);
1974 * Log function called by MHD.
1977 * @param fmt format string
1978 * @param ap arguments for the format string (va_start() and va_end()
1979 * will be called by MHD)
1982 server_log (void *arg,
1992 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2000 * Load ssl certificate from file
2002 * @param file filename
2003 * @return content of the file
2006 server_load_file (const char *file)
2008 struct GNUNET_DISK_FileHandle *gn_file;
2012 if (GNUNET_OK != GNUNET_DISK_file_size (file,
2013 &fsize, GNUNET_NO, GNUNET_YES))
2015 text = GNUNET_malloc (fsize + 1);
2017 GNUNET_DISK_file_open (file, GNUNET_DISK_OPEN_READ,
2018 GNUNET_DISK_PERM_USER_READ);
2019 if (NULL == gn_file)
2024 if (GNUNET_SYSERR == GNUNET_DISK_file_read (gn_file, text, fsize))
2027 GNUNET_DISK_file_close (gn_file);
2031 GNUNET_DISK_file_close (gn_file);
2039 * Load ssl certificate
2041 * @param plugin the plugin
2042 * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
2045 server_load_certificate (struct HTTP_Server_Plugin *plugin)
2047 int res = GNUNET_OK;
2053 GNUNET_CONFIGURATION_get_value_filename (plugin->env->cfg,
2055 "KEY_FILE", &key_file))
2057 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
2058 plugin->name, "CERT_FILE");
2059 return GNUNET_SYSERR;
2062 GNUNET_CONFIGURATION_get_value_filename (plugin->env->cfg,
2064 "CERT_FILE", &cert_file))
2066 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
2067 plugin->name, "CERT_FILE");
2068 GNUNET_free (key_file);
2069 return GNUNET_SYSERR;
2071 /* Get crypto init string from config. If not present, use
2074 GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg,
2077 &plugin->crypto_init))
2078 LOG (GNUNET_ERROR_TYPE_DEBUG,
2079 "Using crypto init string `%s'\n",
2080 plugin->crypto_init);
2082 LOG (GNUNET_ERROR_TYPE_DEBUG,
2083 "Using default crypto init string \n");
2085 /* read key & certificates from file */
2086 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2087 "Trying to loading TLS certificate from key-file `%s' cert-file`%s'\n",
2088 key_file, cert_file);
2090 plugin->key = server_load_file (key_file);
2091 plugin->cert = server_load_file (cert_file);
2093 if ((plugin->key == NULL) || (plugin->cert == NULL))
2095 struct GNUNET_OS_Process *cert_creation;
2097 GNUNET_free_non_null (plugin->key);
2099 GNUNET_free_non_null (plugin->cert);
2100 plugin->cert = NULL;
2102 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2103 "No usable TLS certificate found, creating certificate\n");
2106 GNUNET_OS_start_process (GNUNET_NO, GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
2108 "gnunet-transport-certificate-creation",
2109 "gnunet-transport-certificate-creation",
2113 if (NULL == cert_creation)
2115 LOG (GNUNET_ERROR_TYPE_ERROR,
2116 _("Could not create a new TLS certificate, program `gnunet-transport-certificate-creation' could not be started!\n"));
2117 GNUNET_free (key_file);
2118 GNUNET_free (cert_file);
2120 GNUNET_free_non_null (plugin->key);
2122 GNUNET_free_non_null (plugin->cert);
2123 plugin->cert = NULL;
2124 GNUNET_free_non_null (plugin->crypto_init);
2125 plugin->crypto_init = NULL;
2127 return GNUNET_SYSERR;
2129 GNUNET_assert (GNUNET_OK == GNUNET_OS_process_wait (cert_creation));
2130 GNUNET_OS_process_destroy (cert_creation);
2132 plugin->key = server_load_file (key_file);
2133 plugin->cert = server_load_file (cert_file);
2136 if ((plugin->key == NULL) || (plugin->cert == NULL))
2138 LOG (GNUNET_ERROR_TYPE_ERROR,
2139 _("No usable TLS certificate found and creating one at `%s/%s' failed!\n"),
2140 key_file, cert_file);
2141 GNUNET_free (key_file);
2142 GNUNET_free (cert_file);
2144 GNUNET_free_non_null (plugin->key);
2146 GNUNET_free_non_null (plugin->cert);
2147 plugin->cert = NULL;
2148 GNUNET_free_non_null (plugin->crypto_init);
2149 plugin->crypto_init = NULL;
2151 return GNUNET_SYSERR;
2153 GNUNET_free (key_file);
2154 GNUNET_free (cert_file);
2155 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2156 "TLS certificate loaded\n");
2163 * Invoke `MHD_start_daemon` with the various options we need to
2164 * setup the HTTP server with the given listen address.
2166 * @param plugin our plugin
2167 * @param addr listen address to use
2168 * @param v6 MHD_NO_FLAG or MHD_USE_IPv6, depending on context
2169 * @return NULL on error
2171 static struct MHD_Daemon *
2172 run_mhd_start_daemon (struct HTTP_Server_Plugin *plugin,
2173 const struct sockaddr_in *addr,
2176 struct MHD_Daemon *server;
2177 unsigned int timeout;
2179 #if MHD_VERSION >= 0x00090E00
2180 timeout = HTTP_SERVER_NOT_VALIDATED_TIMEOUT.rel_value_us / 1000LL / 1000LL;
2181 LOG (GNUNET_ERROR_TYPE_DEBUG,
2182 "MHD can set timeout per connection! Default time out %u sec.\n",
2185 timeout = HTTP_SERVER_SESSION_TIMEOUT.rel_value_us / 1000LL / 1000LL;
2186 LOG (GNUNET_ERROR_TYPE_WARNING,
2187 "MHD cannot set timeout per connection! Default time out %u sec.\n",
2190 server = MHD_start_daemon (
2197 MHD_USE_SUSPEND_RESUME |
2200 &server_accept_cb, plugin,
2201 &server_access_cb, plugin,
2202 MHD_OPTION_SOCK_ADDR,
2204 MHD_OPTION_CONNECTION_LIMIT,
2205 (unsigned int) plugin->max_request,
2207 MHD_OPTION_HTTPS_PRIORITIES,
2208 plugin->crypto_init,
2209 MHD_OPTION_HTTPS_MEM_KEY,
2211 MHD_OPTION_HTTPS_MEM_CERT,
2214 MHD_OPTION_CONNECTION_TIMEOUT,
2216 MHD_OPTION_CONNECTION_MEMORY_LIMIT,
2218 GNUNET_SERVER_MAX_MESSAGE_SIZE),
2219 MHD_OPTION_NOTIFY_COMPLETED,
2220 &server_disconnect_cb, plugin,
2221 MHD_OPTION_EXTERNAL_LOGGER,
2225 if ( (NULL != server) &&
2226 (0 != (plugin->options & HTTP_OPTIONS_TCP_STEALTH)) )
2228 const union MHD_DaemonInfo *di;
2230 di = MHD_get_daemon_info (server,
2231 MHD_DAEMON_INFO_LISTEN_FD,
2233 if ( (0 != setsockopt ((int) di->listen_fd,
2236 plugin->env->my_identity,
2237 sizeof (struct GNUNET_PeerIdentity))) )
2239 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2240 _("TCP_STEALTH not supported on this platform.\n"));
2241 MHD_stop_daemon (server);
2251 * Start the HTTP server
2253 * @param plugin the plugin handle
2254 * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
2257 server_start (struct HTTP_Server_Plugin *plugin)
2261 GNUNET_assert (NULL != plugin);
2263 if (GNUNET_SYSERR == server_load_certificate (plugin))
2265 LOG (GNUNET_ERROR_TYPE_ERROR,
2266 _("Could not load or create server certificate! Loading plugin failed!\n"));
2267 return GNUNET_SYSERR;
2273 plugin->server_v4 = NULL;
2274 if (GNUNET_YES == plugin->use_ipv4)
2277 = run_mhd_start_daemon (plugin,
2278 (const struct sockaddr_in *) plugin->server_addr_v4,
2281 if (NULL == plugin->server_v4)
2283 LOG (GNUNET_ERROR_TYPE_ERROR,
2284 "Failed to start %s IPv4 server component on port %u\n",
2289 server_reschedule (plugin,
2295 plugin->server_v6 = NULL;
2296 if (GNUNET_YES == plugin->use_ipv6)
2299 = run_mhd_start_daemon (plugin,
2300 (const struct sockaddr_in *) plugin->server_addr_v6,
2302 if (NULL == plugin->server_v6)
2304 LOG (GNUNET_ERROR_TYPE_ERROR,
2305 "Failed to start %s IPv6 server component on port %u\n",
2311 server_reschedule (plugin,
2317 if ( (NULL == plugin->server_v6) &&
2318 (NULL == plugin->server_v4) )
2320 LOG (GNUNET_ERROR_TYPE_ERROR,
2321 "%s %s server component started on port %u\n",
2325 return GNUNET_SYSERR;
2327 if ((NULL != plugin->server_v6) &&
2328 (NULL != plugin->server_v4))
2329 msg = "IPv4 and IPv6";
2330 else if (NULL != plugin->server_v6)
2332 else if (NULL != plugin->server_v4)
2334 LOG (GNUNET_ERROR_TYPE_DEBUG,
2335 "%s %s server component started on port %u\n",
2344 * Add an address to the server's set of addresses and notify transport
2346 * @param cls the plugin handle
2347 * @param add_remove #GNUNET_YES on add, #GNUNET_NO on remove
2348 * @param addr the address
2349 * @param addrlen address length
2352 server_add_address (void *cls,
2354 const struct sockaddr *addr,
2357 struct HTTP_Server_Plugin *plugin = cls;
2358 struct GNUNET_HELLO_Address *address;
2359 struct HttpAddressWrapper *w = NULL;
2361 w = GNUNET_new (struct HttpAddressWrapper);
2362 w->address = http_common_address_from_socket (plugin->protocol,
2365 if (NULL == w->address)
2370 w->addrlen = http_common_address_get_size (w->address);
2372 GNUNET_CONTAINER_DLL_insert (plugin->addr_head,
2375 LOG (GNUNET_ERROR_TYPE_DEBUG,
2376 "Notifying transport to add address `%s'\n",
2377 http_common_plugin_address_to_string (plugin->protocol,
2380 /* modify our published address list */
2382 address = GNUNET_HELLO_address_allocate (plugin->env->my_identity,
2383 "https_client", w->address, w->addrlen, GNUNET_HELLO_ADDRESS_INFO_NONE);
2385 address = GNUNET_HELLO_address_allocate (plugin->env->my_identity,
2386 "http_client", w->address, w->addrlen, GNUNET_HELLO_ADDRESS_INFO_NONE);
2389 plugin->env->notify_address (plugin->env->cls,
2392 GNUNET_HELLO_address_free (address);
2397 * Remove an address from the server's set of addresses and notify transport
2399 * @param cls the plugin handle
2400 * @param add_remove #GNUNET_YES on add, #GNUNET_NO on remove
2401 * @param addr the address
2402 * @param addrlen address length
2405 server_remove_address (void *cls,
2407 const struct sockaddr *addr,
2410 struct HTTP_Server_Plugin *plugin = cls;
2411 struct GNUNET_HELLO_Address *address;
2412 struct HttpAddressWrapper *w = plugin->addr_head;
2416 saddr = http_common_address_from_socket (plugin->protocol,
2421 saddr_len = http_common_address_get_size (saddr);
2426 http_common_cmp_addresses (w->address,
2433 GNUNET_free (saddr);
2438 LOG (GNUNET_ERROR_TYPE_DEBUG,
2439 "Notifying transport to remove address `%s'\n",
2440 http_common_plugin_address_to_string (plugin->protocol,
2443 GNUNET_CONTAINER_DLL_remove (plugin->addr_head,
2446 /* modify our published address list */
2448 address = GNUNET_HELLO_address_allocate (plugin->env->my_identity,
2449 "https_client", w->address, w->addrlen, GNUNET_HELLO_ADDRESS_INFO_NONE);
2451 address = GNUNET_HELLO_address_allocate (plugin->env->my_identity,
2452 "http_client", w->address, w->addrlen, GNUNET_HELLO_ADDRESS_INFO_NONE);
2454 plugin->env->notify_address (plugin->env->cls, add_remove, address);
2455 GNUNET_HELLO_address_free (address);
2456 GNUNET_free (w->address);
2463 * Our external IP address/port mapping has changed.
2465 * @param cls closure, the 'struct LocalAddrList'
2466 * @param add_remove #GNUNET_YES to mean the new public IP address, #GNUNET_NO to mean
2467 * the previous (now invalid) one
2468 * @param addr either the previous or the new public IP address
2469 * @param addrlen actual lenght of the address
2472 server_nat_port_map_callback (void *cls,
2474 const struct sockaddr *addr,
2477 struct HTTP_Server_Plugin *plugin = cls;
2479 LOG (GNUNET_ERROR_TYPE_DEBUG,
2480 "NAT called to %s address `%s'\n",
2481 (add_remove == GNUNET_NO) ? "remove" : "add",
2482 GNUNET_a2s (addr, addrlen));
2484 if (AF_INET == addr->sa_family)
2486 struct sockaddr_in *s4 = (struct sockaddr_in *) addr;
2488 if (GNUNET_NO == plugin->use_ipv4)
2491 if ((NULL != plugin->server_addr_v4) &&
2492 (0 != memcmp (&plugin->server_addr_v4->sin_addr,
2493 &s4->sin_addr, sizeof (struct in_addr))))
2495 LOG (GNUNET_ERROR_TYPE_DEBUG,
2496 "Skipping address `%s' (not bindto address)\n",
2497 GNUNET_a2s (addr, addrlen));
2502 if (AF_INET6 == addr->sa_family)
2504 struct sockaddr_in6 *s6 = (struct sockaddr_in6 *) addr;
2505 if (GNUNET_NO == plugin->use_ipv6)
2508 if ((NULL != plugin->server_addr_v6) &&
2509 (0 != memcmp (&plugin->server_addr_v6->sin6_addr,
2510 &s6->sin6_addr, sizeof (struct in6_addr))))
2512 LOG (GNUNET_ERROR_TYPE_DEBUG,
2513 "Skipping address `%s' (not bindto address)\n",
2514 GNUNET_a2s (addr, addrlen));
2522 server_add_address (cls, add_remove, addr, addrlen);
2525 server_remove_address (cls, add_remove, addr, addrlen);
2532 * Get valid server addresses
2534 * @param plugin the plugin handle
2535 * @param service_name the servicename
2536 * @param cfg configuration handle
2537 * @param addrs addresses
2538 * @param addr_lens address length
2539 * @return number of addresses
2542 server_get_addresses (struct HTTP_Server_Plugin *plugin,
2543 const char *service_name,
2544 const struct GNUNET_CONFIGURATION_Handle *cfg,
2545 struct sockaddr ***addrs,
2546 socklen_t ** addr_lens)
2549 unsigned long long port;
2550 struct addrinfo hints;
2551 struct addrinfo *res;
2552 struct addrinfo *pos;
2553 struct addrinfo *next;
2557 struct sockaddr **saddrs;
2558 socklen_t *saddrlens;
2564 disablev6 = !plugin->use_ipv6;
2567 if (GNUNET_CONFIGURATION_have_value (cfg, service_name, "PORT"))
2569 GNUNET_break (GNUNET_OK ==
2570 GNUNET_CONFIGURATION_get_value_number (cfg, service_name,
2574 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2575 _("Require valid port number for service in configuration!\n"));
2576 return GNUNET_SYSERR;
2581 LOG (GNUNET_ERROR_TYPE_INFO,
2582 "Starting in listen only mode\n");
2583 return -1; /* listen only */
2587 if (GNUNET_CONFIGURATION_have_value (cfg, service_name,
2590 GNUNET_break (GNUNET_OK ==
2591 GNUNET_CONFIGURATION_get_value_string (cfg, service_name,
2592 "BINDTO", &hostname));
2597 if (NULL != hostname)
2599 LOG (GNUNET_ERROR_TYPE_DEBUG,
2600 "Resolving `%s' since that is where `%s' will bind to.\n",
2601 hostname, service_name);
2602 memset (&hints, 0, sizeof (struct addrinfo));
2604 hints.ai_family = AF_INET;
2605 if ((0 != (ret = getaddrinfo (hostname, NULL, &hints, &res))) ||
2608 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2609 _("Failed to resolve `%s': %s\n"),
2611 gai_strerror (ret));
2612 GNUNET_free (hostname);
2613 return GNUNET_SYSERR;
2617 while (NULL != (pos = next))
2619 next = pos->ai_next;
2620 if ((disablev6) && (pos->ai_family == AF_INET6))
2626 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2627 _("Failed to find %saddress for `%s'.\n"),
2628 disablev6 ? "IPv4 " : "", hostname);
2630 GNUNET_free (hostname);
2631 return GNUNET_SYSERR;
2634 saddrs = GNUNET_malloc ((resi + 1) * sizeof (struct sockaddr *));
2635 saddrlens = GNUNET_malloc ((resi + 1) * sizeof (socklen_t));
2638 while (NULL != (pos = next))
2640 next = pos->ai_next;
2641 if ((disablev6) && (pos->ai_family == AF_INET6))
2643 if ((pos->ai_protocol != IPPROTO_TCP) && (0 != pos->ai_protocol))
2644 continue; /* not TCP */
2645 if ((pos->ai_socktype != SOCK_STREAM) && (0 != pos->ai_socktype))
2646 continue; /* huh? */
2647 LOG (GNUNET_ERROR_TYPE_DEBUG,
2648 "Service will bind to `%s'\n",
2649 GNUNET_a2s (pos->ai_addr,
2651 if (pos->ai_family == AF_INET)
2653 GNUNET_assert (pos->ai_addrlen == sizeof (struct sockaddr_in));
2654 saddrlens[i] = pos->ai_addrlen;
2655 saddrs[i] = GNUNET_malloc (saddrlens[i]);
2656 GNUNET_memcpy (saddrs[i], pos->ai_addr, saddrlens[i]);
2657 ((struct sockaddr_in *) saddrs[i])->sin_port = htons (port);
2661 GNUNET_assert (pos->ai_family == AF_INET6);
2662 GNUNET_assert (pos->ai_addrlen == sizeof (struct sockaddr_in6));
2663 saddrlens[i] = pos->ai_addrlen;
2664 saddrs[i] = GNUNET_malloc (saddrlens[i]);
2665 GNUNET_memcpy (saddrs[i], pos->ai_addr, saddrlens[i]);
2666 ((struct sockaddr_in6 *) saddrs[i])->sin6_port = htons (port);
2670 GNUNET_free (hostname);
2676 /* will bind against everything, just set port */
2682 saddrs = GNUNET_malloc ((resi + 1) * sizeof (struct sockaddr *));
2683 saddrlens = GNUNET_malloc ((resi + 1) * sizeof (socklen_t));
2685 saddrlens[i] = sizeof (struct sockaddr_in);
2686 saddrs[i] = GNUNET_malloc (saddrlens[i]);
2687 #if HAVE_SOCKADDR_IN_SIN_LEN
2688 ((struct sockaddr_in *) saddrs[i])->sin_len = saddrlens[i];
2690 ((struct sockaddr_in *) saddrs[i])->sin_family = AF_INET;
2691 ((struct sockaddr_in *) saddrs[i])->sin_port = htons (port);
2697 saddrs = GNUNET_malloc ((resi + 1) * sizeof (struct sockaddr *));
2698 saddrlens = GNUNET_malloc ((resi + 1) * sizeof (socklen_t));
2700 saddrlens[i] = sizeof (struct sockaddr_in6);
2701 saddrs[i] = GNUNET_malloc (saddrlens[i]);
2702 #if HAVE_SOCKADDR_IN_SIN_LEN
2703 ((struct sockaddr_in6 *) saddrs[i])->sin6_len = saddrlens[0];
2705 ((struct sockaddr_in6 *) saddrs[i])->sin6_family = AF_INET6;
2706 ((struct sockaddr_in6 *) saddrs[i])->sin6_port = htons (port);
2708 saddrlens[i] = sizeof (struct sockaddr_in);
2709 saddrs[i] = GNUNET_malloc (saddrlens[i]);
2710 #if HAVE_SOCKADDR_IN_SIN_LEN
2711 ((struct sockaddr_in *) saddrs[i])->sin_len = saddrlens[1];
2713 ((struct sockaddr_in *) saddrs[i])->sin_family = AF_INET;
2714 ((struct sockaddr_in *) saddrs[i])->sin_port = htons (port);
2718 *addr_lens = saddrlens;
2724 * Ask NAT for addresses
2726 * @param plugin the plugin handle
2729 server_start_report_addresses (struct HTTP_Server_Plugin *plugin)
2731 int res = GNUNET_OK;
2732 struct sockaddr **addrs;
2733 socklen_t *addrlens;
2735 res = server_get_addresses (plugin,
2739 LOG (GNUNET_ERROR_TYPE_DEBUG,
2740 _("Found %u addresses to report to NAT service\n"),
2743 if (GNUNET_SYSERR == res)
2750 GNUNET_NAT_register (plugin->env->cfg,
2754 (const struct sockaddr **) addrs, addrlens,
2755 &server_nat_port_map_callback, NULL, plugin, NULL);
2759 GNUNET_assert (NULL != addrs[res]);
2760 GNUNET_free (addrs[res]);
2762 GNUNET_free_non_null (addrs);
2763 GNUNET_free_non_null (addrlens);
2768 * Stop NAT for addresses
2770 * @param plugin the plugin handle
2773 server_stop_report_addresses (struct HTTP_Server_Plugin *plugin)
2775 struct HttpAddressWrapper *w;
2777 /* Stop NAT handle */
2778 if (NULL != plugin->nat)
2780 GNUNET_NAT_unregister (plugin->nat);
2783 /* Clean up addresses */
2784 while (NULL != plugin->addr_head)
2786 w = plugin->addr_head;
2787 GNUNET_CONTAINER_DLL_remove (plugin->addr_head,
2790 GNUNET_free (w->address);
2797 * Check if IPv6 supported on this system
2799 * @param plugin the plugin handle
2800 * @return #GNUNET_YES on success, else #GNUNET_NO
2803 server_check_ipv6_support (struct HTTP_Server_Plugin *plugin)
2805 struct GNUNET_NETWORK_Handle *desc = NULL;
2806 int res = GNUNET_NO;
2808 /* Probe IPv6 support */
2809 desc = GNUNET_NETWORK_socket_create (PF_INET6, SOCK_STREAM, 0);
2812 if ((errno == ENOBUFS) || (errno == ENOMEM) || (errno == ENFILE) ||
2815 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "socket");
2817 LOG (GNUNET_ERROR_TYPE_WARNING,
2818 _("Disabling IPv6 since it is not supported on this system!\n"));
2823 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (desc));
2827 LOG (GNUNET_ERROR_TYPE_DEBUG,
2828 "Testing IPv6 on this system: %s\n",
2829 (res == GNUNET_YES) ? "successful" : "failed");
2835 * Notify server about our external hostname
2840 server_notify_external_hostname (void *cls)
2842 struct HTTP_Server_Plugin *plugin = cls;
2843 struct HttpAddress *ext_addr;
2844 size_t ext_addr_len;
2848 plugin->notify_ext_task = NULL;
2849 GNUNET_asprintf (&url,
2852 plugin->external_hostname);
2853 urlen = strlen (url) + 1;
2854 ext_addr = GNUNET_malloc (sizeof (struct HttpAddress) + urlen);
2855 ext_addr->options = htonl (plugin->options);
2856 ext_addr->urlen = htonl (urlen);
2857 ext_addr_len = sizeof (struct HttpAddress) + urlen;
2858 GNUNET_memcpy (&ext_addr[1], url, urlen);
2861 LOG (GNUNET_ERROR_TYPE_DEBUG,
2862 "Notifying transport about external hostname address `%s'\n",
2863 plugin->external_hostname);
2866 if (GNUNET_YES == plugin->verify_external_hostname)
2867 LOG (GNUNET_ERROR_TYPE_INFO,
2868 "Enabling SSL verification for external hostname address `%s'\n",
2869 plugin->external_hostname);
2870 plugin->ext_addr = GNUNET_HELLO_address_allocate (plugin->env->my_identity,
2874 GNUNET_HELLO_ADDRESS_INFO_NONE);
2875 plugin->env->notify_address (plugin->env->cls,
2878 GNUNET_free (ext_addr);
2880 plugin->ext_addr = GNUNET_HELLO_address_allocate (plugin->env->my_identity,
2884 GNUNET_HELLO_ADDRESS_INFO_NONE);
2885 plugin->env->notify_address (plugin->env->cls,
2888 GNUNET_free (ext_addr);
2894 * Configure the plugin
2896 * @param plugin plugin handle
2897 * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
2900 server_configure_plugin (struct HTTP_Server_Plugin *plugin)
2902 unsigned long long port;
2903 unsigned long long max_connections;
2904 char *bind4_address = NULL;
2905 char *bind6_address = NULL;
2906 char *eh_tmp = NULL;
2907 int external_hostname_use_port;
2910 if (GNUNET_CONFIGURATION_have_value
2911 (plugin->env->cfg, plugin->name, "USE_IPv4"))
2914 GNUNET_CONFIGURATION_get_value_yesno (plugin->env->cfg,
2919 plugin->use_ipv4 = GNUNET_YES;
2920 LOG (GNUNET_ERROR_TYPE_DEBUG,
2921 _("IPv4 support is %s\n"),
2922 (plugin->use_ipv4 == GNUNET_YES) ? "enabled" : "disabled");
2925 if (GNUNET_CONFIGURATION_have_value
2926 (plugin->env->cfg, plugin->name, "USE_IPv6"))
2929 GNUNET_CONFIGURATION_get_value_yesno (plugin->env->cfg,
2934 plugin->use_ipv6 = GNUNET_YES;
2935 LOG (GNUNET_ERROR_TYPE_DEBUG,
2936 _("IPv6 support is %s\n"),
2937 (plugin->use_ipv6 == GNUNET_YES) ? "enabled" : "disabled");
2939 if ((plugin->use_ipv4 == GNUNET_NO) && (plugin->use_ipv6 == GNUNET_NO))
2941 LOG (GNUNET_ERROR_TYPE_ERROR,
2942 _("Neither IPv4 nor IPv6 are enabled! Fix in configuration\n"));
2943 return GNUNET_SYSERR;
2946 /* Reading port number from config file */
2948 GNUNET_CONFIGURATION_get_value_number (plugin->env->cfg,
2950 "PORT", &port)) || (port > 65535))
2952 LOG (GNUNET_ERROR_TYPE_ERROR,
2953 _("Port is required! Fix in configuration\n"));
2954 return GNUNET_SYSERR;
2956 plugin->port = port;
2958 LOG (GNUNET_ERROR_TYPE_INFO,
2959 _("Using port %u\n"), plugin->port);
2961 if ((plugin->use_ipv4 == GNUNET_YES) &&
2962 (GNUNET_YES == GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg,
2963 plugin->name, "BINDTO", &bind4_address)))
2965 LOG (GNUNET_ERROR_TYPE_DEBUG,
2966 "Binding %s plugin to specific IPv4 address: `%s'\n",
2967 plugin->protocol, bind4_address);
2968 plugin->server_addr_v4 = GNUNET_new (struct sockaddr_in);
2969 if (1 != inet_pton (AF_INET, bind4_address,
2970 &plugin->server_addr_v4->sin_addr))
2972 LOG (GNUNET_ERROR_TYPE_ERROR,
2973 _("Specific IPv4 address `%s' in configuration file is invalid!\n"),
2975 GNUNET_free (bind4_address);
2976 GNUNET_free (plugin->server_addr_v4);
2977 plugin->server_addr_v4 = NULL;
2978 return GNUNET_SYSERR;
2982 LOG (GNUNET_ERROR_TYPE_DEBUG,
2983 _("Binding to IPv4 address %s\n"),
2985 plugin->server_addr_v4->sin_family = AF_INET;
2986 plugin->server_addr_v4->sin_port = htons (plugin->port);
2988 GNUNET_free (bind4_address);
2991 if ((plugin->use_ipv6 == GNUNET_YES) &&
2993 GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg,
2995 "BINDTO6", &bind6_address)))
2997 LOG (GNUNET_ERROR_TYPE_DEBUG,
2998 "Binding %s plugin to specific IPv6 address: `%s'\n",
2999 plugin->protocol, bind6_address);
3000 plugin->server_addr_v6 = GNUNET_new (struct sockaddr_in6);
3002 inet_pton (AF_INET6, bind6_address, &plugin->server_addr_v6->sin6_addr))
3004 LOG (GNUNET_ERROR_TYPE_ERROR,
3005 _("Specific IPv6 address `%s' in configuration file is invalid!\n"),
3007 GNUNET_free (bind6_address);
3008 GNUNET_free (plugin->server_addr_v6);
3009 plugin->server_addr_v6 = NULL;
3010 return GNUNET_SYSERR;
3014 LOG (GNUNET_ERROR_TYPE_DEBUG,
3015 _("Binding to IPv6 address %s\n"),
3017 plugin->server_addr_v6->sin6_family = AF_INET6;
3018 plugin->server_addr_v6->sin6_port = htons (plugin->port);
3020 GNUNET_free (bind6_address);
3023 plugin->verify_external_hostname = GNUNET_NO;
3025 plugin->verify_external_hostname = GNUNET_CONFIGURATION_get_value_yesno (plugin->env->cfg,
3027 "VERIFY_EXTERNAL_HOSTNAME");
3028 if (GNUNET_SYSERR == plugin->verify_external_hostname)
3029 plugin->verify_external_hostname = GNUNET_NO;
3030 if (GNUNET_YES == plugin->verify_external_hostname)
3031 plugin->options |= HTTP_OPTIONS_VERIFY_CERTIFICATE;
3033 external_hostname_use_port = GNUNET_CONFIGURATION_get_value_yesno (plugin->env->cfg,
3035 "EXTERNAL_HOSTNAME_USE_PORT");
3036 if (GNUNET_SYSERR == external_hostname_use_port)
3037 external_hostname_use_port = GNUNET_NO;
3041 GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg,
3043 "EXTERNAL_HOSTNAME",
3048 char *pos_url = NULL;
3050 if (NULL != strstr(eh_tmp, "://"))
3051 tmp = &strstr(eh_tmp, "://")[3];
3055 if (GNUNET_YES == external_hostname_use_port)
3057 if ( (strlen (tmp) > 1) && (NULL != (pos = strchr(tmp, '/'))) )
3061 GNUNET_asprintf (&plugin->external_hostname,
3068 GNUNET_asprintf (&plugin->external_hostname,
3074 plugin->external_hostname = GNUNET_strdup (tmp);
3075 GNUNET_free (eh_tmp);
3077 LOG (GNUNET_ERROR_TYPE_INFO,
3078 _("Using external hostname `%s'\n"),
3079 plugin->external_hostname);
3080 plugin->notify_ext_task = GNUNET_SCHEDULER_add_now (&server_notify_external_hostname,
3083 /* Use only configured external hostname */
3084 if (GNUNET_CONFIGURATION_have_value
3087 "EXTERNAL_HOSTNAME_ONLY"))
3089 plugin->external_only =
3090 GNUNET_CONFIGURATION_get_value_yesno (plugin->env->cfg,
3092 "EXTERNAL_HOSTNAME_ONLY");
3095 plugin->external_only = GNUNET_NO;
3097 if (GNUNET_YES == plugin->external_only)
3098 LOG (GNUNET_ERROR_TYPE_DEBUG,
3099 _("Notifying transport only about hostname `%s'\n"),
3100 plugin->external_hostname);
3103 LOG (GNUNET_ERROR_TYPE_DEBUG,
3104 "No external hostname configured\n");
3106 /* Optional parameters */
3108 GNUNET_CONFIGURATION_get_value_number (plugin->env->cfg,
3112 max_connections = 128;
3113 plugin->max_request = max_connections;
3115 LOG (GNUNET_ERROR_TYPE_DEBUG,
3116 _("Maximum number of connections is %u\n"),
3117 plugin->max_request);
3119 plugin->peer_id_length = strlen (GNUNET_i2s_full (plugin->env->my_identity));
3126 * Exit point from the plugin.
3132 LIBGNUNET_PLUGIN_TRANSPORT_DONE (void *cls)
3134 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
3135 struct HTTP_Server_Plugin *plugin = api->cls;
3137 if (NULL == api->cls)
3139 /* Free for stub mode */
3143 plugin->in_shutdown = GNUNET_YES;
3144 LOG (GNUNET_ERROR_TYPE_INFO,
3145 _("Shutting down plugin `%s'\n"),
3148 if (NULL != plugin->notify_ext_task)
3150 GNUNET_SCHEDULER_cancel (plugin->notify_ext_task);
3151 plugin->notify_ext_task = NULL;
3154 if (NULL != plugin->ext_addr)
3156 LOG (GNUNET_ERROR_TYPE_DEBUG,
3157 "Notifying transport to remove address `%s'\n",
3158 http_common_plugin_address_to_string (plugin->protocol,
3159 plugin->ext_addr->address,
3160 plugin->ext_addr->address_length));
3162 plugin->env->notify_address (plugin->env->cls,
3166 plugin->env->notify_address (plugin->env->cls,
3170 GNUNET_HELLO_address_free (plugin->ext_addr);
3171 plugin->ext_addr = NULL;
3174 /* Stop to report addresses to transport service */
3175 server_stop_report_addresses (plugin);
3176 if (NULL != plugin->server_v4_task)
3178 GNUNET_SCHEDULER_cancel (plugin->server_v4_task);
3179 plugin->server_v4_task = NULL;
3182 if (NULL != plugin->server_v6_task)
3184 GNUNET_SCHEDULER_cancel (plugin->server_v6_task);
3185 plugin->server_v6_task = NULL;
3188 GNUNET_free_non_null (plugin->crypto_init);
3189 GNUNET_free_non_null (plugin->cert);
3190 GNUNET_free_non_null (plugin->key);
3192 GNUNET_CONTAINER_multipeermap_iterate (plugin->sessions,
3193 &destroy_session_shutdown_cb,
3195 GNUNET_CONTAINER_multipeermap_destroy (plugin->sessions);
3196 plugin->sessions = NULL;
3197 if (NULL != plugin->server_v4)
3199 MHD_stop_daemon (plugin->server_v4);
3200 plugin->server_v4 = NULL;
3202 if (NULL != plugin->server_v6)
3204 MHD_stop_daemon (plugin->server_v6);
3205 plugin->server_v6 = NULL;
3208 GNUNET_free_non_null (plugin->external_hostname);
3209 GNUNET_free_non_null (plugin->ext_addr);
3210 GNUNET_free_non_null (plugin->server_addr_v4);
3211 GNUNET_free_non_null (plugin->server_addr_v6);
3212 regfree (&plugin->url_regex);
3214 LOG (GNUNET_ERROR_TYPE_DEBUG,
3215 _("Shutdown for plugin `%s' complete\n"),
3218 GNUNET_free (plugin);
3225 * Function called for a quick conversion of the binary address to
3226 * a numeric address. Note that the caller must not free the
3227 * address and that the next call to this function is allowed
3228 * to override the address again.
3231 * @param addr binary address
3232 * @param addrlen length of the address
3233 * @return string representing the same address
3236 http_server_plugin_address_to_string (void *cls,
3240 return http_common_plugin_address_to_string (PLUGIN_NAME,
3247 * Function obtain the network type for a session
3249 * @param cls closure (`struct HTTP_Server_Plugin *`)
3250 * @param session the session
3251 * @return the network type in HBO or #GNUNET_SYSERR
3253 static enum GNUNET_ATS_Network_Type
3254 http_server_plugin_get_network (void *cls,
3255 struct GNUNET_ATS_Session *session)
3257 return session->scope;
3262 * Function obtain the network type for an address.
3264 * @param cls closure (`struct Plugin *`)
3265 * @param address the address
3266 * @return the network type
3268 static enum GNUNET_ATS_Network_Type
3269 http_server_plugin_get_network_for_address (void *cls,
3270 const struct GNUNET_HELLO_Address *address)
3272 struct HTTP_Server_Plugin *plugin = cls;
3274 return http_common_get_network_for_address (plugin->env,
3280 * Function that will be called whenever the transport service wants to
3281 * notify the plugin that the inbound quota changed and that the plugin
3282 * should update it's delay for the next receive value
3284 * @param cls closure
3285 * @param peer which peer was the session for
3286 * @param session which session is being updated
3287 * @param delay new delay to use for receiving
3290 http_server_plugin_update_inbound_delay (void *cls,
3291 const struct GNUNET_PeerIdentity *peer,
3292 struct GNUNET_ATS_Session *session,
3293 struct GNUNET_TIME_Relative delay)
3295 session->next_receive = GNUNET_TIME_relative_to_absolute (delay);
3296 LOG (GNUNET_ERROR_TYPE_DEBUG,
3297 "New inbound delay %s\n",
3298 GNUNET_STRINGS_relative_time_to_string (delay,
3300 if (NULL != session->recv_wakeup_task)
3302 GNUNET_SCHEDULER_cancel (session->recv_wakeup_task);
3303 session->recv_wakeup_task
3304 = GNUNET_SCHEDULER_add_delayed (delay,
3312 * Return information about the given session to the
3315 * @param cls the `struct Plugin` with the monitor callback (`sic`)
3316 * @param peer peer we send information about
3317 * @param value our `struct GNUNET_ATS_Session` to send information about
3318 * @return #GNUNET_OK (continue to iterate)
3321 send_session_info_iter (void *cls,
3322 const struct GNUNET_PeerIdentity *peer,
3325 struct HTTP_Server_Plugin *plugin = cls;
3326 struct GNUNET_ATS_Session *session = value;
3328 notify_session_monitor (plugin,
3330 GNUNET_TRANSPORT_SS_INIT);
3336 * Begin monitoring sessions of a plugin. There can only
3337 * be one active monitor per plugin (i.e. if there are
3338 * multiple monitors, the transport service needs to
3339 * multiplex the generated events over all of them).
3341 * @param cls closure of the plugin
3342 * @param sic callback to invoke, NULL to disable monitor;
3343 * plugin will being by iterating over all active
3344 * sessions immediately and then enter monitor mode
3345 * @param sic_cls closure for @a sic
3348 http_server_plugin_setup_monitor (void *cls,
3349 GNUNET_TRANSPORT_SessionInfoCallback sic,
3352 struct HTTP_Server_Plugin *plugin = cls;
3355 plugin->sic_cls = sic_cls;
3358 GNUNET_CONTAINER_multipeermap_iterate (plugin->sessions,
3359 &send_session_info_iter,
3361 /* signal end of first iteration */
3362 sic (sic_cls, NULL, NULL);
3368 * Entry point for the plugin.
3374 LIBGNUNET_PLUGIN_TRANSPORT_INIT (void *cls)
3376 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
3377 struct GNUNET_TRANSPORT_PluginFunctions *api;
3378 struct HTTP_Server_Plugin *plugin;
3380 if (NULL == env->receive)
3382 /* run in 'stub' mode (i.e. as part of gnunet-peerinfo), don't fully
3383 initialze the plugin or the API */
3384 api = GNUNET_new (struct GNUNET_TRANSPORT_PluginFunctions);
3386 api->address_to_string = &http_server_plugin_address_to_string;
3387 api->string_to_address = &http_common_plugin_string_to_address;
3388 api->address_pretty_printer = &http_common_plugin_address_pretty_printer;
3391 plugin = GNUNET_new (struct HTTP_Server_Plugin);
3393 plugin->sessions = GNUNET_CONTAINER_multipeermap_create (128,
3396 api = GNUNET_new (struct GNUNET_TRANSPORT_PluginFunctions);
3398 api->send = &http_server_plugin_send;
3399 api->disconnect_peer = &http_server_plugin_disconnect_peer;
3400 api->disconnect_session = &http_server_plugin_disconnect_session;
3401 api->query_keepalive_factor = &http_server_query_keepalive_factor;
3402 api->check_address = &http_server_plugin_address_suggested;
3403 api->get_session = &http_server_plugin_get_session;
3405 api->address_to_string = &http_server_plugin_address_to_string;
3406 api->string_to_address = &http_common_plugin_string_to_address;
3407 api->address_pretty_printer = &http_common_plugin_address_pretty_printer;
3408 api->get_network = &http_server_plugin_get_network;
3409 api->get_network_for_address = &http_server_plugin_get_network_for_address;
3410 api->update_session_timeout = &http_server_plugin_update_session_timeout;
3411 api->update_inbound_delay = &http_server_plugin_update_inbound_delay;
3412 api->setup_monitor = &http_server_plugin_setup_monitor;
3414 plugin->name = "transport-https_server";
3415 plugin->protocol = "https";
3417 plugin->name = "transport-http_server";
3418 plugin->protocol = "http";
3422 GNUNET_CONFIGURATION_get_value_yesno (env->cfg,
3427 plugin->options |= HTTP_OPTIONS_TCP_STEALTH;
3429 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3430 _("TCP_STEALTH not supported on this platform.\n"));
3431 LIBGNUNET_PLUGIN_TRANSPORT_DONE (api);
3436 /* Compile URL regex */
3437 if (regcomp(&plugin->url_regex,
3441 LOG (GNUNET_ERROR_TYPE_ERROR,
3442 _("Unable to compile URL regex\n"));
3443 LIBGNUNET_PLUGIN_TRANSPORT_DONE (api);
3447 /* Configure plugin */
3448 if (GNUNET_SYSERR == server_configure_plugin (plugin))
3450 LIBGNUNET_PLUGIN_TRANSPORT_DONE (api);
3454 /* Check IPv6 support */
3455 if (GNUNET_YES == plugin->use_ipv6)
3456 plugin->use_ipv6 = server_check_ipv6_support (plugin);
3458 /* Report addresses to transport service */
3459 if (GNUNET_NO == plugin->external_only)
3460 server_start_report_addresses (plugin);
3462 if (GNUNET_SYSERR == server_start (plugin))
3464 LIBGNUNET_PLUGIN_TRANSPORT_DONE (api);
3470 /* end of plugin_transport_http_server.c */