2 This file is part of GNUnet
3 Copyright (C) 2002-2014, 2017 GNUnet e.V.
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details.
17 * @file transport/plugin_transport_http_server.c
18 * @brief HTTP/S server transport plugin
19 * @author Matthias Wachs
20 * @author David Barksdale
21 * @author Christian Grothoff
24 #include "gnunet_util_lib.h"
25 #include "gnunet_statistics_service.h"
26 #include "gnunet_transport_plugin.h"
27 #include "gnunet_nat_service.h"
28 #include "plugin_transport_http_common.h"
29 #include <microhttpd.h>
35 #define PLUGIN_NAME "https_server"
36 #define LIBGNUNET_PLUGIN_TRANSPORT_INIT libgnunet_plugin_transport_https_server_init
37 #define LIBGNUNET_PLUGIN_TRANSPORT_DONE libgnunet_plugin_transport_https_server_done
39 #define PLUGIN_NAME "http_server"
40 #define LIBGNUNET_PLUGIN_TRANSPORT_INIT libgnunet_plugin_transport_http_server_init
41 #define LIBGNUNET_PLUGIN_TRANSPORT_DONE libgnunet_plugin_transport_http_server_done
44 #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>"
49 #define LOG(kind,...) GNUNET_log_from (kind, "transport-" PLUGIN_NAME,__VA_ARGS__)
53 * Information we keep with MHD for an HTTP request.
58 * The session this server request belongs to
59 * Can be NULL, when session was disconnected and freed
61 struct GNUNET_ATS_Session *session;
66 struct MHD_Connection *mhd_conn;
71 struct MHD_Daemon *mhd_daemon;
74 * Options requested by peer
77 #define OPTION_LONG_POLL 1 /* GET request wants long-poll semantics */
85 * For PUT requests: Is this the first or last callback with size 0
86 * 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 GNUNET_assert (s->server_recv->suspended);
505 MHD_resume_connection (s->server_recv->mhd_conn);
506 s->server_recv->suspended = false;
511 * Reschedule the execution of both IPv4 and IPv6 server.
513 * @param plugin the plugin
514 * @param server which server to schedule v4 or v6?
515 * @param now #GNUNET_YES to schedule execution immediately, #GNUNET_NO to wait
519 server_reschedule (struct HTTP_Server_Plugin *plugin,
520 struct MHD_Daemon *server,
525 * Deletes the session. Must not be used afterwards.
527 * @param s the session to delete
530 server_delete_session (struct GNUNET_ATS_Session *s)
532 struct HTTP_Server_Plugin *plugin = s->plugin;
533 struct HTTP_Message *msg;
535 if (NULL != s->timeout_task)
537 GNUNET_SCHEDULER_cancel (s->timeout_task);
538 s->timeout_task = NULL;
539 s->timeout = GNUNET_TIME_UNIT_ZERO_ABS;
541 if (NULL != s->recv_wakeup_task)
543 GNUNET_SCHEDULER_cancel (s->recv_wakeup_task);
544 s->recv_wakeup_task = NULL;
545 if (NULL != s->server_recv)
547 GNUNET_assert (s->server_recv->suspended);
548 s->server_recv->suspended = false;
549 MHD_resume_connection (s->server_recv->mhd_conn);
552 GNUNET_assert (GNUNET_OK ==
553 GNUNET_CONTAINER_multipeermap_remove (plugin->sessions,
556 while (NULL != (msg = s->msg_head))
558 GNUNET_CONTAINER_DLL_remove (s->msg_head,
561 if (NULL != msg->transmit_cont)
562 msg->transmit_cont (msg->transmit_cont_cls,
566 msg->pos + msg->overhead);
567 GNUNET_assert (s->msgs_in_queue > 0);
569 GNUNET_assert (s->bytes_in_queue >= msg->size);
570 s->bytes_in_queue -= msg->size;
574 GNUNET_assert (0 == s->msgs_in_queue);
575 GNUNET_assert (0 == s->bytes_in_queue);
577 if (NULL != s->server_send)
579 LOG (GNUNET_ERROR_TYPE_DEBUG,
580 "Server: %p / %p Terminating inbound PUT session to peer `%s'\n",
582 GNUNET_i2s (&s->target));
583 s->server_send->session = NULL;
584 MHD_set_connection_option (s->server_send->mhd_conn,
585 MHD_CONNECTION_OPTION_TIMEOUT,
586 1 /* 0 = no timeout, so this is MIN */);
587 if (s->server_send->suspended)
589 s->server_send->suspended = false;
590 MHD_resume_connection (s->server_send->mhd_conn);
592 server_reschedule (plugin,
593 s->server_send->mhd_daemon,
597 if (NULL != s->server_recv)
599 LOG (GNUNET_ERROR_TYPE_DEBUG,
600 "Server: %p / %p Terminating inbound GET session to peer `%s'\n",
601 s, s->server_recv, GNUNET_i2s (&s->target));
602 s->server_recv->session = NULL;
603 MHD_set_connection_option (s->server_recv->mhd_conn,
604 MHD_CONNECTION_OPTION_TIMEOUT,
605 1 /* 0 = no timeout, so this is MIN */);
606 server_reschedule (plugin,
607 s->server_recv->mhd_daemon,
610 notify_session_monitor (plugin,
612 GNUNET_TRANSPORT_SS_DONE);
613 if (GNUNET_YES == s->known_to_service)
615 plugin->env->session_end (plugin->env->cls,
618 s->known_to_service = GNUNET_NO;
620 if (NULL != s->msg_tk)
622 GNUNET_MST_destroy (s->msg_tk);
625 GNUNET_HELLO_address_free (s->address);
626 LOG (GNUNET_ERROR_TYPE_DEBUG,
627 "Session %p destroyed\n",
634 * Disconnect session @a s by telling MHD to close the
635 * connections (reducing timeout, etc.).
637 * @param cls closure with the `struct HTTP_Server_Plugin`
638 * @param s the session
639 * @return #GNUNET_OK on success
642 http_server_plugin_disconnect_session (void *cls,
643 struct GNUNET_ATS_Session *s)
645 server_delete_session (s);
651 * Session was idle, so disconnect it
653 * @param cls the session
656 server_session_timeout (void *cls)
658 struct GNUNET_ATS_Session *s = cls;
659 struct GNUNET_TIME_Relative left;
661 s->timeout_task = NULL;
662 left = GNUNET_TIME_absolute_get_remaining (s->timeout);
663 if (0 != left.rel_value_us)
665 /* not actually our turn yet, but let's at least update
666 the monitor, it may think we're about to die ... */
667 notify_session_monitor (s->plugin,
669 GNUNET_TRANSPORT_SS_UP);
670 s->timeout_task = GNUNET_SCHEDULER_add_delayed (left,
671 &server_session_timeout,
675 GNUNET_log (TIMEOUT_LOG,
676 "Session %p was idle for %s, disconnecting\n",
678 GNUNET_STRINGS_relative_time_to_string (HTTP_SERVER_SESSION_TIMEOUT,
680 server_delete_session (s);
685 * Increment session timeout due to activity session @a s
687 * @param s the session
690 server_reschedule_session_timeout (struct GNUNET_ATS_Session *s)
692 GNUNET_assert (NULL != s->timeout_task);
693 s->timeout = GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
698 * Function that can be used by the transport service to transmit
699 * a message using the plugin. Note that in the case of a
700 * peer disconnecting, the continuation MUST be called
701 * prior to the disconnect notification itself. This function
702 * will be called with this peer's HELLO message to initiate
703 * a fresh connection to another peer.
706 * @param session which session must be used
707 * @param msgbuf the message to transmit
708 * @param msgbuf_size number of bytes in @a msgbuf
709 * @param priority how important is the message (most plugins will
710 * ignore message priority and just FIFO)
711 * @param to how long to wait at most for the transmission (does not
712 * require plugins to discard the message after the timeout,
713 * just advisory for the desired delay; most plugins will ignore
715 * @param cont continuation to call once the message has
716 * been transmitted (or if the transport is ready
717 * for the next transmission call; or if the
718 * peer disconnected...); can be NULL
719 * @param cont_cls closure for @a cont
720 * @return number of bytes used (on the physical network, with overheads);
721 * -1 on hard errors (i.e. address invalid); 0 is a legal value
722 * and does NOT mean that the message was not transmitted (DV)
725 http_server_plugin_send (void *cls,
726 struct GNUNET_ATS_Session *session,
729 unsigned int priority,
730 struct GNUNET_TIME_Relative to,
731 GNUNET_TRANSPORT_TransmitContinuation cont,
734 struct HTTP_Server_Plugin *plugin = cls;
735 struct HTTP_Message *msg;
736 ssize_t bytes_sent = 0;
739 LOG (GNUNET_ERROR_TYPE_DEBUG,
740 "Session %p/request %p: Sending message with %u to peer `%s'\n",
742 session->server_send,
744 GNUNET_i2s (&session->target));
746 /* create new message and schedule */
747 bytes_sent = sizeof (struct HTTP_Message) + msgbuf_size;
748 msg = GNUNET_malloc (bytes_sent);
750 msg->size = msgbuf_size;
752 msg->buf = (char *) &msg[1];
753 msg->transmit_cont = cont;
754 msg->transmit_cont_cls = cont_cls;
755 GNUNET_memcpy (msg->buf,
758 GNUNET_CONTAINER_DLL_insert_tail (session->msg_head,
761 session->msgs_in_queue++;
762 session->bytes_in_queue += msg->size;
763 notify_session_monitor (plugin,
765 GNUNET_TRANSPORT_SS_UP);
766 GNUNET_asprintf (&stat_txt,
767 "# bytes currently in %s_server buffers",
769 GNUNET_STATISTICS_update (plugin->env->stats,
770 stat_txt, msgbuf_size, GNUNET_NO);
771 GNUNET_free (stat_txt);
773 if (NULL != session->server_send)
775 if (session->server_send->suspended)
777 MHD_resume_connection (session->server_send->mhd_conn);
778 session->server_send->suspended = false;
780 server_reschedule (session->plugin,
781 session->server_send->mhd_daemon,
789 * Terminate session during shutdown.
791 * @param cls the `struct HTTP_Server_Plugin *`
792 * @param peer for which this is a session
793 * @param value the `struct GNUNET_ATS_Session` to clean up
794 * @return #GNUNET_OK (continue to iterate)
797 destroy_session_shutdown_cb (void *cls,
798 const struct GNUNET_PeerIdentity *peer,
801 struct GNUNET_ATS_Session *s = value;
802 struct ServerRequest *sc_send;
803 struct ServerRequest *sc_recv;
805 sc_send = s->server_send;
806 sc_recv = s->server_recv;
807 server_delete_session (s);
809 sc_send->session = NULL;
811 sc_recv->session = NULL;
820 * @param cls the `struct HTTP_Server_Plugin *`
821 * @param peer for which this is a session
822 * @param value the `struct GNUNET_ATS_Session` to clean up
823 * @return #GNUNET_OK (continue to iterate)
826 destroy_session_cb (void *cls,
827 const struct GNUNET_PeerIdentity *peer,
830 struct GNUNET_ATS_Session *s = value;
832 server_delete_session (s);
838 * Function that can be used to force the plugin to disconnect
839 * from the given peer and cancel all previous transmissions
840 * (and their continuationc).
843 * @param target peer from which to disconnect
846 http_server_plugin_disconnect_peer (void *cls,
847 const struct GNUNET_PeerIdentity *target)
849 struct HTTP_Server_Plugin *plugin = cls;
851 LOG (GNUNET_ERROR_TYPE_DEBUG,
852 "Transport tells me to disconnect `%s'\n",
853 GNUNET_i2s (target));
854 GNUNET_CONTAINER_multipeermap_get_multiple (plugin->sessions,
862 * Another peer has suggested an address for this
863 * peer and transport plugin. Check that this could be a valid
864 * address. If so, consider adding it to the list
868 * @param addr pointer to the address
869 * @param addrlen length of @a addr
870 * @return #GNUNET_OK if this is a plausible address for this peer
874 http_server_plugin_address_suggested (void *cls,
878 struct HTTP_Server_Plugin *plugin = cls;
879 struct HttpAddressWrapper *next;
880 struct HttpAddressWrapper *pos;
881 const struct HttpAddress *haddr = addr;
883 if ((NULL != plugin->ext_addr) &&
884 GNUNET_YES == (http_common_cmp_addresses (addr, addrlen,
885 plugin->ext_addr->address,
886 plugin->ext_addr->address_length)))
888 /* Checking HTTP_OPTIONS_VERIFY_CERTIFICATE option for external hostname */
889 if ((ntohl (haddr->options) & HTTP_OPTIONS_VERIFY_CERTIFICATE) !=
890 (plugin->options & HTTP_OPTIONS_VERIFY_CERTIFICATE))
891 return GNUNET_NO; /* VERIFY option not set as required! */
894 next = plugin->addr_head;
895 while (NULL != (pos = next))
898 if (GNUNET_YES == (http_common_cmp_addresses(addr,
909 * Creates a new outbound session the transport
910 * service will use to send data to the peer.
912 * Since HTTP/S server cannot create sessions, always returns NULL.
914 * @param cls the plugin
915 * @param address the address
916 * @return always NULL
918 static struct GNUNET_ATS_Session *
919 http_server_plugin_get_session (void *cls,
920 const struct GNUNET_HELLO_Address *address)
927 * Call MHD IPv4 to process pending requests and then go back
928 * and schedule the next run.
930 * @param cls plugin as closure
933 server_v4_run (void *cls)
935 struct HTTP_Server_Plugin *plugin = cls;
937 plugin->server_v4_task = NULL;
938 plugin->server_v4_immediately = GNUNET_NO;
939 GNUNET_assert (MHD_YES == MHD_run (plugin->server_v4));
940 server_reschedule (plugin, plugin->server_v4, GNUNET_NO);
945 * Call MHD IPv6 to process pending requests and then go back
946 * and schedule the next run.
948 * @param cls plugin as closure
951 server_v6_run (void *cls)
953 struct HTTP_Server_Plugin *plugin = cls;
955 plugin->server_v6_task = NULL;
956 plugin->server_v6_immediately = GNUNET_NO;
957 GNUNET_assert (MHD_YES == MHD_run (plugin->server_v6));
958 server_reschedule (plugin, plugin->server_v6, GNUNET_NO);
963 * Function that queries MHD's select sets and
964 * starts the task waiting for them.
966 * @param plugin plugin
967 * @param daemon_handle the MHD daemon handle
968 * @param now schedule now
969 * @return gnunet task identifier
971 static struct GNUNET_SCHEDULER_Task *
972 server_schedule (struct HTTP_Server_Plugin *plugin,
973 struct MHD_Daemon *daemon_handle,
976 struct GNUNET_SCHEDULER_Task * ret;
980 struct GNUNET_NETWORK_FDSet *wrs;
981 struct GNUNET_NETWORK_FDSet *wws;
983 MHD_UNSIGNED_LONG_LONG timeout;
984 static unsigned long long last_timeout = 0;
986 struct GNUNET_TIME_Relative tv;
988 if (GNUNET_YES == plugin->in_shutdown)
995 wrs = GNUNET_NETWORK_fdset_create ();
996 wws = GNUNET_NETWORK_fdset_create ();
998 GNUNET_assert (MHD_YES ==
999 MHD_get_fdset (daemon_handle,
1004 haveto = MHD_get_timeout (daemon_handle, &timeout);
1005 if (haveto == MHD_YES)
1007 if (timeout != last_timeout)
1009 LOG (GNUNET_ERROR_TYPE_DEBUG,
1010 "SELECT Timeout changed from %llu to %llu (ms)\n",
1011 last_timeout, timeout);
1012 last_timeout = timeout;
1014 if (timeout <= GNUNET_TIME_UNIT_SECONDS.rel_value_us / 1000LL)
1015 tv.rel_value_us = (uint64_t) timeout * 1000LL;
1017 tv = GNUNET_TIME_UNIT_SECONDS;
1020 tv = GNUNET_TIME_UNIT_SECONDS;
1021 /* Force immediate run, since we have outbound data to send */
1022 if (now == GNUNET_YES)
1023 tv = GNUNET_TIME_UNIT_MILLISECONDS;
1024 GNUNET_NETWORK_fdset_copy_native (wrs, &rs, max + 1);
1025 GNUNET_NETWORK_fdset_copy_native (wws, &ws, max + 1);
1027 if (daemon_handle == plugin->server_v4)
1029 if (plugin->server_v4_task != NULL)
1031 GNUNET_SCHEDULER_cancel (plugin->server_v4_task);
1032 plugin->server_v4_task = NULL;
1035 LOG (GNUNET_ERROR_TYPE_DEBUG,
1036 "Scheduling IPv4 server task in %llu ms\n",
1040 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1042 &server_v4_run, plugin);
1044 if (daemon_handle == plugin->server_v6)
1046 if (plugin->server_v6_task != NULL)
1048 GNUNET_SCHEDULER_cancel (plugin->server_v6_task);
1049 plugin->server_v6_task = NULL;
1052 LOG (GNUNET_ERROR_TYPE_DEBUG,
1053 "Scheduling IPv6 server task in %llu ms\n", tv);
1056 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1058 &server_v6_run, plugin);
1060 GNUNET_NETWORK_fdset_destroy (wrs);
1061 GNUNET_NETWORK_fdset_destroy (wws);
1067 * Reschedule the execution of both IPv4 and IPv6 server
1069 * @param plugin the plugin
1070 * @param server which server to schedule v4 or v6?
1071 * @param now #GNUNET_YES to schedule execution immediately, #GNUNET_NO to wait
1075 server_reschedule (struct HTTP_Server_Plugin *plugin,
1076 struct MHD_Daemon *server,
1079 if ((server == plugin->server_v4) && (plugin->server_v4 != NULL))
1081 if (GNUNET_YES == plugin->server_v4_immediately)
1082 return; /* No rescheduling, server will run asap */
1084 if (GNUNET_YES == now)
1085 plugin->server_v4_immediately = GNUNET_YES;
1087 if (plugin->server_v4_task != NULL)
1089 GNUNET_SCHEDULER_cancel (plugin->server_v4_task);
1090 plugin->server_v4_task = NULL;
1092 plugin->server_v4_task = server_schedule (plugin, plugin->server_v4, now);
1095 if ((server == plugin->server_v6) && (plugin->server_v6 != NULL))
1097 if (GNUNET_YES == plugin->server_v6_immediately)
1098 return; /* No rescheduling, server will run asap */
1100 if (GNUNET_YES == now)
1101 plugin->server_v6_immediately = GNUNET_YES;
1103 if (plugin->server_v6_task != NULL)
1105 GNUNET_SCHEDULER_cancel (plugin->server_v6_task);
1106 plugin->server_v6_task = NULL;
1108 plugin->server_v6_task = server_schedule (plugin, plugin->server_v6, now);
1114 * Function that is called to get the keepalive factor.
1115 * GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT is divided by this number to
1116 * calculate the interval between keepalive packets.
1118 * @param cls closure with the `struct HTTP_Server_Plugin`
1119 * @return keepalive factor
1122 http_server_query_keepalive_factor (void *cls)
1129 * Function that will be called whenever the transport service wants to
1130 * notify the plugin that a session is still active and in use and
1131 * therefore the session timeout for this session has to be updated
1133 * @param cls closure
1134 * @param peer which peer was the session for
1135 * @param session which session is being updated
1138 http_server_plugin_update_session_timeout (void *cls,
1139 const struct GNUNET_PeerIdentity *peer,
1140 struct GNUNET_ATS_Session *session)
1142 server_reschedule_session_timeout (session);
1147 * Tell MHD that the connection should timeout after @a to seconds.
1149 * @param plugin our plugin
1150 * @param s session for which the timeout changes
1151 * @param to timeout in seconds
1154 server_mhd_connection_timeout (struct HTTP_Server_Plugin *plugin,
1155 struct GNUNET_ATS_Session *s,
1158 /* Setting timeouts for other connections */
1159 if (NULL != s->server_recv)
1161 LOG (GNUNET_ERROR_TYPE_DEBUG,
1162 "Setting timeout for %p to %u sec.\n",
1163 s->server_recv, to);
1164 MHD_set_connection_option (s->server_recv->mhd_conn,
1165 MHD_CONNECTION_OPTION_TIMEOUT,
1167 server_reschedule (plugin, s->server_recv->mhd_daemon, GNUNET_NO);
1169 if (NULL != s->server_send)
1171 LOG (GNUNET_ERROR_TYPE_DEBUG,
1172 "Setting timeout for %p to %u sec.\n",
1173 s->server_send, to);
1174 MHD_set_connection_option (s->server_send->mhd_conn,
1175 MHD_CONNECTION_OPTION_TIMEOUT,
1177 server_reschedule (plugin, s->server_send->mhd_daemon, GNUNET_NO);
1183 * Parse incoming URL for tag and target
1185 * @param plugin plugin
1186 * @param url incoming url
1187 * @param target where to store the target
1188 * @param tag where to store the tag
1189 * @param options where to store the options
1190 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
1193 server_parse_url (struct HTTP_Server_Plugin *plugin,
1195 struct GNUNET_PeerIdentity *target,
1199 regmatch_t matches[4];
1200 const char *tag_start;
1201 const char *target_start;
1205 unsigned long int rc;
1209 ("^.*/([0-9A-Z]+);([0-9]+)(,[0-9]+)?$")
1214 return GNUNET_SYSERR;
1217 if (regexec(&plugin->url_regex, url, 4, matches, 0))
1219 LOG (GNUNET_ERROR_TYPE_DEBUG,
1220 "URL `%s' did not match regex\n", url);
1221 return GNUNET_SYSERR;
1224 target_start = &url[matches[1].rm_so];
1225 tag_start = &url[matches[2].rm_so];
1228 rc = strtoul (tag_start, &tag_end, 10);
1229 if (&url[matches[2].rm_eo] != tag_end)
1231 LOG (GNUNET_ERROR_TYPE_DEBUG,
1232 "URL tag did not line up with submatch\n");
1233 return GNUNET_SYSERR;
1237 LOG (GNUNET_ERROR_TYPE_DEBUG,
1238 "URL tag is zero\n");
1239 return GNUNET_SYSERR;
1241 if ((rc == ULONG_MAX) && (ERANGE == errno))
1243 LOG (GNUNET_ERROR_TYPE_DEBUG,
1244 "URL tag > ULONG_MAX\n");
1245 return GNUNET_SYSERR;
1247 if (rc > UINT32_MAX)
1249 LOG (GNUNET_ERROR_TYPE_DEBUG,
1250 "URL tag > UINT32_MAX\n");
1251 return GNUNET_SYSERR;
1253 (*tag) = (uint32_t)rc;
1254 LOG (GNUNET_ERROR_TYPE_DEBUG,
1255 "Found tag `%u' in url\n",
1258 /* convert peer id */
1259 hash_length = matches[1].rm_eo - matches[1].rm_so;
1260 if (hash_length != plugin->peer_id_length)
1262 LOG (GNUNET_ERROR_TYPE_DEBUG,
1263 "URL target is %u bytes, expecting %u\n",
1264 hash_length, plugin->peer_id_length);
1265 return GNUNET_SYSERR;
1268 GNUNET_CRYPTO_eddsa_public_key_from_string (target_start,
1270 &target->public_key))
1272 LOG (GNUNET_ERROR_TYPE_DEBUG,
1273 "URL target conversion failed\n");
1274 return GNUNET_SYSERR;
1276 LOG (GNUNET_ERROR_TYPE_DEBUG,
1277 "Found target `%s' in URL\n",
1278 GNUNET_i2s_full (target));
1280 /* convert options */
1281 if (-1 == matches[3].rm_so)
1287 rc = strtoul (&url[matches[3].rm_so + 1], &options_end, 10);
1288 if (&url[matches[3].rm_eo] != options_end)
1290 LOG (GNUNET_ERROR_TYPE_DEBUG,
1291 "URL options did not line up with submatch\n");
1292 return GNUNET_SYSERR;
1294 if ((rc == ULONG_MAX) && (ERANGE == errno))
1296 LOG (GNUNET_ERROR_TYPE_DEBUG,
1297 "URL options > ULONG_MAX\n");
1298 return GNUNET_SYSERR;
1300 if (rc > UINT32_MAX)
1302 LOG (GNUNET_ERROR_TYPE_DEBUG,
1303 "URL options > UINT32_MAX\n");
1304 return GNUNET_SYSERR;
1306 (*options) = (uint32_t) rc;
1307 LOG (GNUNET_ERROR_TYPE_DEBUG,
1308 "Found options `%u' in url\n",
1316 * Closure for #session_tag_it().
1318 struct GNUNET_ATS_SessionTagContext
1321 * Set to session matching the tag.
1323 struct GNUNET_ATS_Session *res;
1326 * Tag we are looking for.
1333 * Find a session with a matching tag.
1335 * @param cls the `struct GNUNET_ATS_SessionTagContext *`
1336 * @param key peer identity (unused)
1337 * @param value the `struct GNUNET_ATS_Session *`
1338 * @return #GNUNET_NO if we found the session, #GNUNET_OK if not
1341 session_tag_it (void *cls,
1342 const struct GNUNET_PeerIdentity *key,
1345 struct GNUNET_ATS_SessionTagContext *stc = cls;
1346 struct GNUNET_ATS_Session *s = value;
1348 if (s->tag == stc->tag)
1358 * Lookup a mhd connection and create one if none is found
1360 * @param plugin the plugin handle
1361 * @param mhd_connection the incoming mhd_connection
1362 * @param url incoming requested URL
1363 * @param method PUT or GET
1364 * @return the server connecetion
1366 static struct ServerRequest *
1367 server_lookup_connection (struct HTTP_Server_Plugin *plugin,
1368 struct MHD_Connection *mhd_connection,
1372 struct GNUNET_ATS_Session *s = NULL;
1373 struct ServerRequest *sc = NULL;
1374 const union MHD_ConnectionInfo *conn_info;
1375 struct HttpAddress *addr;
1376 struct GNUNET_PeerIdentity target;
1378 struct GNUNET_ATS_SessionTagContext stc;
1380 int direction = GNUNET_SYSERR;
1382 enum GNUNET_ATS_Network_Type scope;
1384 conn_info = MHD_get_connection_info (mhd_connection,
1385 MHD_CONNECTION_INFO_CLIENT_ADDRESS);
1386 if ((conn_info->client_addr->sa_family != AF_INET) &&
1387 (conn_info->client_addr->sa_family != AF_INET6))
1389 LOG (GNUNET_ERROR_TYPE_DEBUG,
1390 "New %s request from %s\n",
1394 options = 0; /* make gcc happy */
1395 if (GNUNET_SYSERR ==
1396 server_parse_url (plugin, url, &target, &stc.tag, &options))
1398 LOG (GNUNET_ERROR_TYPE_DEBUG,
1399 "Invalid url %s\n", url);
1402 if (0 == strcmp (MHD_HTTP_METHOD_PUT, method))
1403 direction = _RECEIVE;
1404 else if (0 == strcmp (MHD_HTTP_METHOD_GET, method))
1408 LOG (GNUNET_ERROR_TYPE_DEBUG,
1409 "Invalid method %s for request from %s\n",
1414 plugin->cur_request++;
1415 LOG (GNUNET_ERROR_TYPE_DEBUG,
1416 "New %s request from %s with tag %u (%u of %u)\n",
1418 GNUNET_i2s (&target),
1420 plugin->cur_request, plugin->max_request);
1421 /* find existing session */
1423 GNUNET_CONTAINER_multipeermap_get_multiple (plugin->sessions,
1427 if (NULL == (s = stc.res))
1429 /* create new session */
1431 switch (conn_info->client_addr->sa_family)
1434 addr = http_common_address_from_socket (plugin->protocol,
1435 conn_info->client_addr,
1436 sizeof (struct sockaddr_in));
1437 addr_len = http_common_address_get_size (addr);
1438 scope = plugin->env->get_address_type (plugin->env->cls,
1439 conn_info->client_addr,
1440 sizeof (struct sockaddr_in));
1443 addr = http_common_address_from_socket (plugin->protocol,
1444 conn_info->client_addr,
1445 sizeof (struct sockaddr_in6));
1446 addr_len = http_common_address_get_size (addr);
1447 scope = plugin->env->get_address_type (plugin->env->cls,
1448 conn_info->client_addr,
1449 sizeof (struct sockaddr_in6));
1452 /* external host name */
1455 s = GNUNET_new (struct GNUNET_ATS_Session);
1459 s->address = GNUNET_HELLO_address_allocate (&s->target,
1463 GNUNET_HELLO_ADDRESS_INFO_INBOUND);
1464 s->next_receive = GNUNET_TIME_UNIT_ZERO_ABS;
1466 s->timeout = GNUNET_TIME_relative_to_absolute (HTTP_SERVER_SESSION_TIMEOUT);
1467 s->timeout_task = GNUNET_SCHEDULER_add_delayed (HTTP_SERVER_SESSION_TIMEOUT,
1468 &server_session_timeout,
1470 (void) GNUNET_CONTAINER_multipeermap_put (plugin->sessions,
1473 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
1474 notify_session_monitor (plugin,
1476 GNUNET_TRANSPORT_SS_INIT);
1477 notify_session_monitor (plugin,
1479 GNUNET_TRANSPORT_SS_HANDSHAKE);
1480 LOG (GNUNET_ERROR_TYPE_DEBUG,
1481 "Creating new session %p for peer `%s' connecting from `%s'\n",
1482 s, GNUNET_i2s (&target),
1483 http_common_plugin_address_to_string (plugin->protocol,
1486 GNUNET_free_non_null (addr);
1489 if ( (_RECEIVE == direction) &&
1490 (NULL != s->server_recv) )
1492 LOG (GNUNET_ERROR_TYPE_DEBUG,
1493 "Duplicate PUT request from `%s' tag %u, dismissing new request\n",
1494 GNUNET_i2s (&target),
1498 if ((_SEND == direction) && (NULL != s->server_send))
1500 LOG (GNUNET_ERROR_TYPE_DEBUG,
1501 "Duplicate GET request from `%s' tag %u, dismissing new request\n",
1502 GNUNET_i2s (&target),
1506 sc = GNUNET_new (struct ServerRequest);
1507 if (conn_info->client_addr->sa_family == AF_INET)
1508 sc->mhd_daemon = plugin->server_v4;
1509 if (conn_info->client_addr->sa_family == AF_INET6)
1510 sc->mhd_daemon = plugin->server_v6;
1511 sc->mhd_conn = mhd_connection;
1512 sc->direction = direction;
1513 sc->connected = GNUNET_NO;
1515 sc->options = options;
1516 if (direction == _SEND)
1518 s->server_send = sc;
1520 if (direction == _RECEIVE)
1522 s->server_recv = sc;
1525 if ((GNUNET_NO == s->known_to_service) &&
1526 (NULL != s->server_send) &&
1527 (NULL != s->server_recv) )
1529 s->known_to_service = GNUNET_YES;
1530 notify_session_monitor (plugin,
1532 GNUNET_TRANSPORT_SS_UP);
1533 plugin->env->session_start (plugin->env->cls,
1539 to = (HTTP_SERVER_SESSION_TIMEOUT.rel_value_us / 1000LL / 1000LL);
1540 server_mhd_connection_timeout (plugin, s, to);
1546 * Callback called by MHD when it needs data to send
1548 * @param cls current session
1549 * @param pos position in buffer
1550 * @param buf the buffer to write data to
1551 * @param max max number of bytes available in @a buf
1552 * @return bytes written to @a buf
1555 server_send_callback (void *cls,
1560 struct ServerRequest *sc = cls;
1561 struct GNUNET_ATS_Session *s = sc->session;
1562 ssize_t bytes_read = 0;
1563 struct HTTP_Message *msg;
1568 /* session is disconnecting */
1572 sc = s->server_send;
1579 bytes_read = GNUNET_MIN (msg->size - msg->pos,
1581 GNUNET_memcpy (buf, &msg->buf[msg->pos], bytes_read);
1582 msg->pos += bytes_read;
1584 /* removing message */
1585 if (msg->pos == msg->size)
1587 GNUNET_CONTAINER_DLL_remove (s->msg_head,
1590 if (NULL != msg->transmit_cont)
1591 msg->transmit_cont (msg->transmit_cont_cls, &s->target, GNUNET_OK,
1592 msg->size, msg->size + msg->overhead);
1593 GNUNET_assert (s->msgs_in_queue > 0);
1595 GNUNET_assert (s->bytes_in_queue >= msg->size);
1596 s->bytes_in_queue -= msg->size;
1598 notify_session_monitor (s->plugin,
1600 GNUNET_TRANSPORT_SS_UPDATE);
1605 sc->connected = GNUNET_YES;
1606 LOG (GNUNET_ERROR_TYPE_DEBUG,
1607 "Sent %u bytes to peer `%s' with session %p \n",
1609 GNUNET_i2s (&s->target),
1611 GNUNET_asprintf (&stat_txt,
1612 "# bytes currently in %s_server buffers",
1613 s->plugin->protocol);
1614 GNUNET_STATISTICS_update (s->plugin->env->stats,
1618 GNUNET_free (stat_txt);
1619 GNUNET_asprintf (&stat_txt,
1620 "# bytes transmitted via %s_server",
1621 s->plugin->protocol);
1622 GNUNET_STATISTICS_update (s->plugin->env->stats,
1623 stat_txt, bytes_read, GNUNET_NO);
1624 GNUNET_free (stat_txt);
1626 else if ((sc->options & OPTION_LONG_POLL) && sc->connected)
1628 LOG (GNUNET_ERROR_TYPE_DEBUG,
1629 "Completing GET response to peer `%s' with session %p\n",
1630 GNUNET_i2s (&s->target),
1632 return MHD_CONTENT_READER_END_OF_STREAM;
1636 MHD_suspend_connection (s->server_send->mhd_conn);
1637 s->server_send->suspended = true;
1645 * Callback called by MessageStreamTokenizer when a message has arrived
1647 * @param cls current session as closure
1648 * @param message the message to be forwarded to transport service
1649 * @return #GNUNET_OK (all OK)
1652 server_receive_mst_cb (void *cls,
1653 const struct GNUNET_MessageHeader *message)
1655 struct GNUNET_ATS_Session *s = cls;
1656 struct HTTP_Server_Plugin *plugin = s->plugin;
1657 struct GNUNET_TIME_Relative delay;
1660 if (GNUNET_NO == s->known_to_service)
1662 s->known_to_service = GNUNET_YES;
1663 plugin->env->session_start (plugin->env->cls,
1667 notify_session_monitor (plugin,
1669 GNUNET_TRANSPORT_SS_UP);
1671 delay = plugin->env->receive (plugin->env->cls,
1675 GNUNET_asprintf (&stat_txt,
1676 "# bytes received via %s_server",
1678 GNUNET_STATISTICS_update (plugin->env->stats,
1680 ntohs (message->size),
1682 GNUNET_free (stat_txt);
1683 s->next_receive = GNUNET_TIME_relative_to_absolute (delay);
1684 if (delay.rel_value_us > 0)
1686 LOG (GNUNET_ERROR_TYPE_DEBUG,
1687 "Peer `%s' address `%s' next read delayed for %s\n",
1688 GNUNET_i2s (&s->target),
1689 http_common_plugin_address_to_string (plugin->protocol,
1690 s->address->address,
1691 s->address->address_length),
1692 GNUNET_STRINGS_relative_time_to_string (delay,
1695 server_reschedule_session_timeout (s);
1701 * Add headers to a request indicating that we allow Cross-Origin Resource
1704 * @param response response object to modify
1707 add_cors_headers(struct MHD_Response *response)
1709 MHD_add_response_header (response,
1710 "Access-Control-Allow-Origin",
1712 MHD_add_response_header (response,
1713 "Access-Control-Allow-Methods",
1714 "GET, PUT, OPTIONS");
1715 MHD_add_response_header (response,
1716 "Access-Control-Max-Age",
1722 * MHD callback for a new incoming connection
1724 * @param cls the plugin handle
1725 * @param mhd_connection the mhd connection
1726 * @param url the requested URL
1727 * @param method GET or PUT
1728 * @param version HTTP version
1729 * @param upload_data upload data
1730 * @param upload_data_size size of @a upload_data
1731 * @param httpSessionCache the session cache to remember the connection
1732 * @return MHD_YES if connection is accepted, MHD_NO on reject
1735 server_access_cb (void *cls,
1736 struct MHD_Connection *mhd_connection,
1739 const char *version,
1740 const char *upload_data,
1741 size_t *upload_data_size,
1742 void **httpSessionCache)
1744 struct HTTP_Server_Plugin *plugin = cls;
1745 struct ServerRequest *sc = *httpSessionCache;
1746 struct GNUNET_ATS_Session *s;
1747 struct MHD_Response *response;
1750 LOG (GNUNET_ERROR_TYPE_DEBUG,
1751 _("Access from connection %p (%u of %u) for `%s' `%s' url `%s' with upload data size %u\n"),
1753 plugin->cur_request,
1754 plugin->max_request,
1758 (*upload_data_size));
1761 /* CORS pre-flight request */
1762 if (0 == strcmp (MHD_HTTP_METHOD_OPTIONS, method))
1764 response = MHD_create_response_from_buffer (0, NULL,
1765 MHD_RESPMEM_PERSISTENT);
1766 add_cors_headers(response);
1767 res = MHD_queue_response (mhd_connection, MHD_HTTP_OK, response);
1768 MHD_destroy_response (response);
1771 /* new connection */
1772 sc = server_lookup_connection (plugin, mhd_connection, url, method);
1775 /* attach to new / existing session */
1776 (*httpSessionCache) = sc;
1780 /* existing session already has matching connection, refuse */
1781 response = MHD_create_response_from_buffer (strlen (HTTP_ERROR_RESPONSE),
1782 HTTP_ERROR_RESPONSE,
1783 MHD_RESPMEM_PERSISTENT);
1784 MHD_add_response_header (response,
1785 MHD_HTTP_HEADER_CONTENT_TYPE,
1787 add_cors_headers(response);
1788 res = MHD_queue_response (mhd_connection, MHD_HTTP_NOT_FOUND, response);
1789 MHD_destroy_response (response);
1793 /* 'old' connection */
1794 if (NULL == (s = sc->session))
1796 /* Session was already disconnected;
1797 sent HTTP/1.1: 200 OK as response */
1798 response = MHD_create_response_from_buffer (strlen ("Thank you!"),
1800 MHD_RESPMEM_PERSISTENT);
1801 add_cors_headers(response);
1802 MHD_queue_response (mhd_connection, MHD_HTTP_OK, response);
1803 MHD_destroy_response (response);
1807 if (sc->direction == _SEND)
1809 response = MHD_create_response_from_callback (MHD_SIZE_UNKNOWN, 32 * 1024,
1810 &server_send_callback, sc, NULL);
1811 add_cors_headers(response);
1812 MHD_queue_response (mhd_connection, MHD_HTTP_OK, response);
1813 MHD_destroy_response (response);
1816 if (sc->direction == _RECEIVE)
1818 if ((*upload_data_size == 0) && (sc->connected == GNUNET_NO))
1820 /* (*upload_data_size == 0) first callback when header are passed */
1821 LOG (GNUNET_ERROR_TYPE_DEBUG,
1822 "Session %p / Connection %p: Peer `%s' PUT on address `%s' connected\n",
1824 GNUNET_i2s (&s->target),
1825 http_common_plugin_address_to_string (plugin->protocol,
1826 s->address->address,
1827 s->address->address_length));
1828 sc->connected = GNUNET_YES;
1831 else if ((*upload_data_size == 0) && (sc->connected == GNUNET_YES))
1833 /* (*upload_data_size == 0) when upload is complete */
1834 LOG (GNUNET_ERROR_TYPE_DEBUG,
1835 "Session %p / Connection %p: Peer `%s' PUT on address `%s' finished upload\n",
1837 GNUNET_i2s (&s->target),
1838 http_common_plugin_address_to_string (plugin->protocol,
1839 s->address->address,
1840 s->address->address_length));
1841 sc->connected = GNUNET_NO;
1842 /* Sent HTTP/1.1: 200 OK as PUT Response\ */
1843 response = MHD_create_response_from_buffer (strlen ("Thank you!"),
1845 MHD_RESPMEM_PERSISTENT);
1846 add_cors_headers(response);
1847 MHD_queue_response (mhd_connection, MHD_HTTP_OK, response);
1848 MHD_destroy_response (response);
1851 else if ((*upload_data_size > 0) && (sc->connected == GNUNET_YES))
1853 struct GNUNET_TIME_Relative delay;
1855 /* (*upload_data_size > 0) for every segment received */
1856 LOG (GNUNET_ERROR_TYPE_DEBUG,
1857 "Session %p / Connection %p: Peer `%s' PUT on address `%s' received %u bytes\n",
1859 GNUNET_i2s (&s->target),
1860 http_common_plugin_address_to_string (plugin->protocol,
1861 s->address->address,
1862 s->address->address_length),
1864 delay = GNUNET_TIME_absolute_get_remaining (s->next_receive);
1865 if (0 == delay.rel_value_us)
1867 LOG (GNUNET_ERROR_TYPE_DEBUG,
1868 "PUT with %u bytes forwarded to MST\n",
1870 if (s->msg_tk == NULL)
1872 s->msg_tk = GNUNET_MST_create (&server_receive_mst_cb,
1875 GNUNET_MST_from_buffer (s->msg_tk,
1878 GNUNET_NO, GNUNET_NO);
1879 server_mhd_connection_timeout (plugin, s,
1880 GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT.rel_value_us / 1000LL
1882 (*upload_data_size) = 0;
1886 /* delay processing */
1887 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1888 "Session %p / Connection %p: no inbound bandwidth available! Next read was delayed by %s\n",
1891 GNUNET_STRINGS_relative_time_to_string (delay,
1893 GNUNET_assert(s->server_recv->mhd_conn == mhd_connection);
1894 MHD_suspend_connection (s->server_recv->mhd_conn);
1895 s->server_recv->suspended = true;
1896 if (NULL == s->recv_wakeup_task)
1898 = GNUNET_SCHEDULER_add_delayed (delay,
1915 * Callback from MHD when a connection disconnects
1917 * @param cls closure with the `struct HTTP_Server_Plugin *`
1918 * @param connection the disconnected MHD connection
1919 * @param httpSessionCache the pointer to distinguish
1922 server_disconnect_cb (void *cls,
1923 struct MHD_Connection *connection,
1924 void **httpSessionCache)
1926 struct HTTP_Server_Plugin *plugin = cls;
1927 struct ServerRequest *sc = *httpSessionCache;
1929 LOG (GNUNET_ERROR_TYPE_DEBUG,
1930 "Disconnect for connection %p\n",
1934 /* CORS pre-flight request finished */
1938 if (NULL != sc->session)
1940 if (sc->direction == _SEND)
1942 LOG (GNUNET_ERROR_TYPE_DEBUG,
1943 "Peer `%s' connection %p, GET on address `%s' disconnected\n",
1944 GNUNET_i2s (&sc->session->target),
1945 sc->session->server_send,
1946 http_common_plugin_address_to_string (plugin->protocol,
1947 sc->session->address->address,
1948 sc->session->address->address_length));
1950 sc->session->server_send = NULL;
1952 else if (sc->direction == _RECEIVE)
1954 LOG (GNUNET_ERROR_TYPE_DEBUG,
1955 "Peer `%s' connection %p PUT on address `%s' disconnected\n",
1956 GNUNET_i2s (&sc->session->target),
1957 sc->session->server_recv,
1958 http_common_plugin_address_to_string (plugin->protocol,
1959 sc->session->address->address,
1960 sc->session->address->address_length));
1961 sc->session->server_recv = NULL;
1962 if (NULL != sc->session->msg_tk)
1964 GNUNET_MST_destroy (sc->session->msg_tk);
1965 sc->session->msg_tk = NULL;
1970 plugin->cur_request--;
1975 * Callback from MHD when a connection starts/stops
1977 * @param cls closure with the `struct HTTP_Server_Plugin *`
1978 * @param connection connection handle
1979 * @param socket_context socket-specific pointer
1980 * @param toe reason for connection notification
1981 * @see #MHD_OPTION_NOTIFY_CONNECTION
1984 server_connection_cb (void *cls,
1985 struct MHD_Connection *connection,
1986 void **socket_context,
1987 enum MHD_ConnectionNotificationCode toe)
1989 struct HTTP_Server_Plugin *plugin = cls;
1990 const union MHD_ConnectionInfo *info;
1992 if (MHD_CONNECTION_NOTIFY_STARTED == toe)
1995 /* Reschedule to remove closed socket from our select set */
1996 info = MHD_get_connection_info (connection,
1997 MHD_CONNECTION_INFO_DAEMON);
1998 GNUNET_assert (NULL != info);
1999 server_reschedule (plugin,
2006 * Check if incoming connection is accepted.
2008 * @param cls plugin as closure
2009 * @param addr address of incoming connection
2010 * @param addr_len number of bytes in @a addr
2011 * @return MHD_YES if connection is accepted, MHD_NO if connection is rejected
2014 server_accept_cb (void *cls,
2015 const struct sockaddr *addr,
2018 struct HTTP_Server_Plugin *plugin = cls;
2020 if (plugin->cur_request <= plugin->max_request)
2022 LOG (GNUNET_ERROR_TYPE_DEBUG,
2023 _("Accepting connection (%u of %u) from `%s'\n"),
2024 plugin->cur_request, plugin->max_request,
2025 GNUNET_a2s (addr, addr_len));
2030 LOG (GNUNET_ERROR_TYPE_WARNING,
2031 _("Server reached maximum number connections (%u), rejecting new connection\n"),
2032 plugin->max_request);
2039 * Log function called by MHD.
2042 * @param fmt format string
2043 * @param ap arguments for the format string (va_start() and va_end()
2044 * will be called by MHD)
2047 server_log (void *arg,
2057 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2065 * Load ssl certificate from file
2067 * @param file filename
2068 * @return content of the file
2071 server_load_file (const char *file)
2073 struct GNUNET_DISK_FileHandle *gn_file;
2077 if (GNUNET_OK != GNUNET_DISK_file_size (file,
2078 &fsize, GNUNET_NO, GNUNET_YES))
2080 text = GNUNET_malloc (fsize + 1);
2082 GNUNET_DISK_file_open (file, GNUNET_DISK_OPEN_READ,
2083 GNUNET_DISK_PERM_USER_READ);
2084 if (NULL == gn_file)
2089 if (GNUNET_SYSERR == GNUNET_DISK_file_read (gn_file, text, fsize))
2092 GNUNET_DISK_file_close (gn_file);
2096 GNUNET_DISK_file_close (gn_file);
2104 * Load ssl certificate
2106 * @param plugin the plugin
2107 * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
2110 server_load_certificate (struct HTTP_Server_Plugin *plugin)
2112 int res = GNUNET_OK;
2118 GNUNET_CONFIGURATION_get_value_filename (plugin->env->cfg,
2120 "KEY_FILE", &key_file))
2122 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
2123 plugin->name, "CERT_FILE");
2124 return GNUNET_SYSERR;
2127 GNUNET_CONFIGURATION_get_value_filename (plugin->env->cfg,
2129 "CERT_FILE", &cert_file))
2131 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
2132 plugin->name, "CERT_FILE");
2133 GNUNET_free (key_file);
2134 return GNUNET_SYSERR;
2136 /* Get crypto init string from config. If not present, use
2139 GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg,
2142 &plugin->crypto_init))
2143 LOG (GNUNET_ERROR_TYPE_DEBUG,
2144 "Using crypto init string `%s'\n",
2145 plugin->crypto_init);
2147 LOG (GNUNET_ERROR_TYPE_DEBUG,
2148 "Using default crypto init string \n");
2150 /* read key & certificates from file */
2151 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2152 "Trying to loading TLS certificate from key-file `%s' cert-file`%s'\n",
2153 key_file, cert_file);
2155 plugin->key = server_load_file (key_file);
2156 plugin->cert = server_load_file (cert_file);
2158 if ((plugin->key == NULL) || (plugin->cert == NULL))
2160 struct GNUNET_OS_Process *cert_creation;
2162 GNUNET_free_non_null (plugin->key);
2164 GNUNET_free_non_null (plugin->cert);
2165 plugin->cert = NULL;
2167 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2168 "No usable TLS certificate found, creating certificate\n");
2171 GNUNET_OS_start_process (GNUNET_NO, GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
2173 "gnunet-transport-certificate-creation",
2174 "gnunet-transport-certificate-creation",
2178 if (NULL == cert_creation)
2180 LOG (GNUNET_ERROR_TYPE_ERROR,
2181 _("Could not create a new TLS certificate, program `gnunet-transport-certificate-creation' could not be started!\n"));
2182 GNUNET_free (key_file);
2183 GNUNET_free (cert_file);
2185 GNUNET_free_non_null (plugin->key);
2187 GNUNET_free_non_null (plugin->cert);
2188 plugin->cert = NULL;
2189 GNUNET_free_non_null (plugin->crypto_init);
2190 plugin->crypto_init = NULL;
2192 return GNUNET_SYSERR;
2194 GNUNET_assert (GNUNET_OK == GNUNET_OS_process_wait (cert_creation));
2195 GNUNET_OS_process_destroy (cert_creation);
2197 plugin->key = server_load_file (key_file);
2198 plugin->cert = server_load_file (cert_file);
2201 if ((plugin->key == NULL) || (plugin->cert == NULL))
2203 LOG (GNUNET_ERROR_TYPE_ERROR,
2204 _("No usable TLS certificate found and creating one at `%s/%s' failed!\n"),
2205 key_file, cert_file);
2206 GNUNET_free (key_file);
2207 GNUNET_free (cert_file);
2209 GNUNET_free_non_null (plugin->key);
2211 GNUNET_free_non_null (plugin->cert);
2212 plugin->cert = NULL;
2213 GNUNET_free_non_null (plugin->crypto_init);
2214 plugin->crypto_init = NULL;
2216 return GNUNET_SYSERR;
2218 GNUNET_free (key_file);
2219 GNUNET_free (cert_file);
2220 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2221 "TLS certificate loaded\n");
2228 * Invoke `MHD_start_daemon` with the various options we need to
2229 * setup the HTTP server with the given listen address.
2231 * @param plugin our plugin
2232 * @param addr listen address to use
2233 * @param v6 MHD_NO_FLAG or MHD_USE_IPv6, depending on context
2234 * @return NULL on error
2236 static struct MHD_Daemon *
2237 run_mhd_start_daemon (struct HTTP_Server_Plugin *plugin,
2238 const struct sockaddr_in *addr,
2241 struct MHD_Daemon *server;
2242 unsigned int timeout;
2244 #if MHD_VERSION >= 0x00090E00
2245 timeout = HTTP_SERVER_NOT_VALIDATED_TIMEOUT.rel_value_us / 1000LL / 1000LL;
2246 LOG (GNUNET_ERROR_TYPE_DEBUG,
2247 "MHD can set timeout per connection! Default time out %u sec.\n",
2250 timeout = HTTP_SERVER_SESSION_TIMEOUT.rel_value_us / 1000LL / 1000LL;
2251 LOG (GNUNET_ERROR_TYPE_WARNING,
2252 "MHD cannot set timeout per connection! Default time out %u sec.\n",
2255 server = MHD_start_daemon (
2262 MHD_USE_SUSPEND_RESUME |
2265 &server_accept_cb, plugin,
2266 &server_access_cb, plugin,
2267 MHD_OPTION_SOCK_ADDR,
2269 MHD_OPTION_CONNECTION_LIMIT,
2270 (unsigned int) plugin->max_request,
2272 MHD_OPTION_HTTPS_PRIORITIES,
2273 plugin->crypto_init,
2274 MHD_OPTION_HTTPS_MEM_KEY,
2276 MHD_OPTION_HTTPS_MEM_CERT,
2279 MHD_OPTION_CONNECTION_TIMEOUT,
2281 MHD_OPTION_CONNECTION_MEMORY_LIMIT,
2283 GNUNET_MAX_MESSAGE_SIZE),
2284 MHD_OPTION_NOTIFY_COMPLETED,
2285 &server_disconnect_cb, plugin,
2286 MHD_OPTION_NOTIFY_CONNECTION,
2287 &server_connection_cb, plugin,
2288 MHD_OPTION_EXTERNAL_LOGGER,
2292 if ( (NULL != server) &&
2293 (0 != (plugin->options & HTTP_OPTIONS_TCP_STEALTH)) )
2295 const union MHD_DaemonInfo *di;
2297 di = MHD_get_daemon_info (server,
2298 MHD_DAEMON_INFO_LISTEN_FD,
2300 if ( (0 != setsockopt ((int) di->listen_fd,
2303 plugin->env->my_identity,
2304 sizeof (struct GNUNET_PeerIdentity))) )
2306 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2307 _("TCP_STEALTH not supported on this platform.\n"));
2308 MHD_stop_daemon (server);
2318 * Start the HTTP server
2320 * @param plugin the plugin handle
2321 * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
2324 server_start (struct HTTP_Server_Plugin *plugin)
2328 GNUNET_assert (NULL != plugin);
2330 if (GNUNET_SYSERR == server_load_certificate (plugin))
2332 LOG (GNUNET_ERROR_TYPE_ERROR,
2333 _("Could not load or create server certificate! Loading plugin failed!\n"));
2334 return GNUNET_SYSERR;
2340 plugin->server_v4 = NULL;
2341 if (GNUNET_YES == plugin->use_ipv4)
2344 = run_mhd_start_daemon (plugin,
2345 (const struct sockaddr_in *) plugin->server_addr_v4,
2348 if (NULL == plugin->server_v4)
2350 LOG (GNUNET_ERROR_TYPE_ERROR,
2351 "Failed to start %s IPv4 server component on port %u\n",
2356 server_reschedule (plugin,
2362 plugin->server_v6 = NULL;
2363 if (GNUNET_YES == plugin->use_ipv6)
2366 = run_mhd_start_daemon (plugin,
2367 (const struct sockaddr_in *) plugin->server_addr_v6,
2369 if (NULL == plugin->server_v6)
2371 LOG (GNUNET_ERROR_TYPE_ERROR,
2372 "Failed to start %s IPv6 server component on port %u\n",
2378 server_reschedule (plugin,
2384 if ( (NULL == plugin->server_v6) &&
2385 (NULL == plugin->server_v4) )
2387 LOG (GNUNET_ERROR_TYPE_ERROR,
2388 "%s %s server component started on port %u\n",
2392 return GNUNET_SYSERR;
2394 if ((NULL != plugin->server_v6) &&
2395 (NULL != plugin->server_v4))
2396 msg = "IPv4 and IPv6";
2397 else if (NULL != plugin->server_v6)
2399 else if (NULL != plugin->server_v4)
2401 LOG (GNUNET_ERROR_TYPE_DEBUG,
2402 "%s %s server component started on port %u\n",
2411 * Add an address to the server's set of addresses and notify transport
2413 * @param cls the plugin handle
2414 * @param add_remove #GNUNET_YES on add, #GNUNET_NO on remove
2415 * @param addr the address
2416 * @param addrlen address length
2419 server_add_address (void *cls,
2421 const struct sockaddr *addr,
2424 struct HTTP_Server_Plugin *plugin = cls;
2425 struct GNUNET_HELLO_Address *address;
2426 struct HttpAddressWrapper *w = NULL;
2428 w = GNUNET_new (struct HttpAddressWrapper);
2429 w->address = http_common_address_from_socket (plugin->protocol,
2432 if (NULL == w->address)
2437 w->addrlen = http_common_address_get_size (w->address);
2439 GNUNET_CONTAINER_DLL_insert (plugin->addr_head,
2442 LOG (GNUNET_ERROR_TYPE_DEBUG,
2443 "Notifying transport to add address `%s'\n",
2444 http_common_plugin_address_to_string (plugin->protocol,
2447 /* modify our published address list */
2449 address = GNUNET_HELLO_address_allocate (plugin->env->my_identity,
2450 "https_client", w->address, w->addrlen, GNUNET_HELLO_ADDRESS_INFO_NONE);
2452 address = GNUNET_HELLO_address_allocate (plugin->env->my_identity,
2453 "http_client", w->address, w->addrlen, GNUNET_HELLO_ADDRESS_INFO_NONE);
2456 plugin->env->notify_address (plugin->env->cls,
2459 GNUNET_HELLO_address_free (address);
2464 * Remove an address from the server's set of addresses and notify transport
2466 * @param cls the plugin handle
2467 * @param add_remove #GNUNET_YES on add, #GNUNET_NO on remove
2468 * @param addr the address
2469 * @param addrlen address length
2472 server_remove_address (void *cls,
2474 const struct sockaddr *addr,
2477 struct HTTP_Server_Plugin *plugin = cls;
2478 struct GNUNET_HELLO_Address *address;
2479 struct HttpAddressWrapper *w = plugin->addr_head;
2483 saddr = http_common_address_from_socket (plugin->protocol,
2488 saddr_len = http_common_address_get_size (saddr);
2493 http_common_cmp_addresses (w->address,
2500 GNUNET_free (saddr);
2505 LOG (GNUNET_ERROR_TYPE_DEBUG,
2506 "Notifying transport to remove address `%s'\n",
2507 http_common_plugin_address_to_string (plugin->protocol,
2510 GNUNET_CONTAINER_DLL_remove (plugin->addr_head,
2513 /* modify our published address list */
2515 address = GNUNET_HELLO_address_allocate (plugin->env->my_identity,
2516 "https_client", w->address, w->addrlen, GNUNET_HELLO_ADDRESS_INFO_NONE);
2518 address = GNUNET_HELLO_address_allocate (plugin->env->my_identity,
2519 "http_client", w->address, w->addrlen, GNUNET_HELLO_ADDRESS_INFO_NONE);
2521 plugin->env->notify_address (plugin->env->cls, add_remove, address);
2522 GNUNET_HELLO_address_free (address);
2523 GNUNET_free (w->address);
2530 * Our external IP address/port mapping has changed.
2532 * @param cls closure, the 'struct LocalAddrList'
2533 * @param add_remove #GNUNET_YES to mean the new public IP address, #GNUNET_NO to mean
2534 * the previous (now invalid) one
2535 * @param ac address class the address belongs to
2536 * @param addr either the previous or the new public IP address
2537 * @param addrlen actual lenght of the address
2540 server_nat_port_map_callback (void *cls,
2542 enum GNUNET_NAT_AddressClass ac,
2543 const struct sockaddr *addr,
2546 struct HTTP_Server_Plugin *plugin = cls;
2548 LOG (GNUNET_ERROR_TYPE_DEBUG,
2549 "NAT called to %s address `%s'\n",
2550 (add_remove == GNUNET_NO) ? "remove" : "add",
2551 GNUNET_a2s (addr, addrlen));
2553 if (AF_INET == addr->sa_family)
2555 struct sockaddr_in *s4 = (struct sockaddr_in *) addr;
2557 if (GNUNET_NO == plugin->use_ipv4)
2560 if ((NULL != plugin->server_addr_v4) &&
2561 (0 != memcmp (&plugin->server_addr_v4->sin_addr,
2563 sizeof (struct in_addr))))
2565 LOG (GNUNET_ERROR_TYPE_DEBUG,
2566 "Skipping address `%s' (not bindto address)\n",
2567 GNUNET_a2s (addr, addrlen));
2572 if (AF_INET6 == addr->sa_family)
2574 struct sockaddr_in6 *s6 = (struct sockaddr_in6 *) addr;
2575 if (GNUNET_NO == plugin->use_ipv6)
2578 if ((NULL != plugin->server_addr_v6) &&
2579 (0 != memcmp (&plugin->server_addr_v6->sin6_addr,
2580 &s6->sin6_addr, sizeof (struct in6_addr))))
2582 LOG (GNUNET_ERROR_TYPE_DEBUG,
2583 "Skipping address `%s' (not bindto address)\n",
2584 GNUNET_a2s (addr, addrlen));
2592 server_add_address (cls, add_remove, addr, addrlen);
2595 server_remove_address (cls, add_remove, addr, addrlen);
2602 * Get valid server addresses
2604 * @param plugin the plugin handle
2605 * @param service_name the servicename
2606 * @param cfg configuration handle
2607 * @param addrs addresses
2608 * @param addr_lens address length
2609 * @return number of addresses
2612 server_get_addresses (struct HTTP_Server_Plugin *plugin,
2613 const char *service_name,
2614 const struct GNUNET_CONFIGURATION_Handle *cfg,
2615 struct sockaddr ***addrs,
2616 socklen_t ** addr_lens)
2619 unsigned long long port;
2620 struct addrinfo hints;
2621 struct addrinfo *res;
2622 struct addrinfo *pos;
2623 struct addrinfo *next;
2627 struct sockaddr **saddrs;
2628 socklen_t *saddrlens;
2634 disablev6 = !plugin->use_ipv6;
2637 if (GNUNET_CONFIGURATION_have_value (cfg, service_name, "PORT"))
2639 GNUNET_break (GNUNET_OK ==
2640 GNUNET_CONFIGURATION_get_value_number (cfg, service_name,
2644 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2645 _("Require valid port number for service in configuration!\n"));
2646 return GNUNET_SYSERR;
2651 LOG (GNUNET_ERROR_TYPE_INFO,
2652 "Starting in listen only mode\n");
2653 return -1; /* listen only */
2657 if (GNUNET_CONFIGURATION_have_value (cfg, service_name,
2660 GNUNET_break (GNUNET_OK ==
2661 GNUNET_CONFIGURATION_get_value_string (cfg, service_name,
2662 "BINDTO", &hostname));
2667 if (NULL != hostname)
2669 LOG (GNUNET_ERROR_TYPE_DEBUG,
2670 "Resolving `%s' since that is where `%s' will bind to.\n",
2671 hostname, service_name);
2672 memset (&hints, 0, sizeof (struct addrinfo));
2674 hints.ai_family = AF_INET;
2675 if ((0 != (ret = getaddrinfo (hostname, NULL, &hints, &res))) ||
2678 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2679 _("Failed to resolve `%s': %s\n"),
2681 gai_strerror (ret));
2682 GNUNET_free (hostname);
2683 return GNUNET_SYSERR;
2687 while (NULL != (pos = next))
2689 next = pos->ai_next;
2690 if ((disablev6) && (pos->ai_family == AF_INET6))
2696 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2697 _("Failed to find %saddress for `%s'.\n"),
2698 disablev6 ? "IPv4 " : "", hostname);
2700 GNUNET_free (hostname);
2701 return GNUNET_SYSERR;
2704 saddrs = GNUNET_malloc ((resi + 1) * sizeof (struct sockaddr *));
2705 saddrlens = GNUNET_malloc ((resi + 1) * sizeof (socklen_t));
2708 while (NULL != (pos = next))
2710 next = pos->ai_next;
2711 if ((disablev6) && (pos->ai_family == AF_INET6))
2713 if ((pos->ai_protocol != IPPROTO_TCP) && (0 != pos->ai_protocol))
2714 continue; /* not TCP */
2715 if ((pos->ai_socktype != SOCK_STREAM) && (0 != pos->ai_socktype))
2716 continue; /* huh? */
2717 LOG (GNUNET_ERROR_TYPE_DEBUG,
2718 "Service will bind to `%s'\n",
2719 GNUNET_a2s (pos->ai_addr,
2721 if (pos->ai_family == AF_INET)
2723 GNUNET_assert (pos->ai_addrlen == sizeof (struct sockaddr_in));
2724 saddrlens[i] = pos->ai_addrlen;
2725 saddrs[i] = GNUNET_malloc (saddrlens[i]);
2726 GNUNET_memcpy (saddrs[i], pos->ai_addr, saddrlens[i]);
2727 ((struct sockaddr_in *) saddrs[i])->sin_port = htons (port);
2731 GNUNET_assert (pos->ai_family == AF_INET6);
2732 GNUNET_assert (pos->ai_addrlen == sizeof (struct sockaddr_in6));
2733 saddrlens[i] = pos->ai_addrlen;
2734 saddrs[i] = GNUNET_malloc (saddrlens[i]);
2735 GNUNET_memcpy (saddrs[i], pos->ai_addr, saddrlens[i]);
2736 ((struct sockaddr_in6 *) saddrs[i])->sin6_port = htons (port);
2740 GNUNET_free (hostname);
2746 /* will bind against everything, just set port */
2752 saddrs = GNUNET_malloc ((resi + 1) * sizeof (struct sockaddr *));
2753 saddrlens = GNUNET_malloc ((resi + 1) * sizeof (socklen_t));
2755 saddrlens[i] = sizeof (struct sockaddr_in);
2756 saddrs[i] = GNUNET_malloc (saddrlens[i]);
2757 #if HAVE_SOCKADDR_IN_SIN_LEN
2758 ((struct sockaddr_in *) saddrs[i])->sin_len = saddrlens[i];
2760 ((struct sockaddr_in *) saddrs[i])->sin_family = AF_INET;
2761 ((struct sockaddr_in *) saddrs[i])->sin_port = htons (port);
2767 saddrs = GNUNET_malloc ((resi + 1) * sizeof (struct sockaddr *));
2768 saddrlens = GNUNET_malloc ((resi + 1) * sizeof (socklen_t));
2770 saddrlens[i] = sizeof (struct sockaddr_in6);
2771 saddrs[i] = GNUNET_malloc (saddrlens[i]);
2772 #if HAVE_SOCKADDR_IN_SIN_LEN
2773 ((struct sockaddr_in6 *) saddrs[i])->sin6_len = saddrlens[0];
2775 ((struct sockaddr_in6 *) saddrs[i])->sin6_family = AF_INET6;
2776 ((struct sockaddr_in6 *) saddrs[i])->sin6_port = htons (port);
2778 saddrlens[i] = sizeof (struct sockaddr_in);
2779 saddrs[i] = GNUNET_malloc (saddrlens[i]);
2780 #if HAVE_SOCKADDR_IN_SIN_LEN
2781 ((struct sockaddr_in *) saddrs[i])->sin_len = saddrlens[1];
2783 ((struct sockaddr_in *) saddrs[i])->sin_family = AF_INET;
2784 ((struct sockaddr_in *) saddrs[i])->sin_port = htons (port);
2788 *addr_lens = saddrlens;
2794 * Ask NAT for addresses
2796 * @param plugin the plugin handle
2799 server_start_report_addresses (struct HTTP_Server_Plugin *plugin)
2801 int res = GNUNET_OK;
2802 struct sockaddr **addrs;
2803 socklen_t *addrlens;
2805 res = server_get_addresses (plugin,
2809 LOG (GNUNET_ERROR_TYPE_DEBUG,
2810 _("Found %u addresses to report to NAT service\n"),
2813 if (GNUNET_SYSERR == res)
2820 = GNUNET_NAT_register (plugin->env->cfg,
2821 "transport-http_server",
2824 (const struct sockaddr **) addrs,
2826 &server_nat_port_map_callback,
2832 GNUNET_assert (NULL != addrs[res]);
2833 GNUNET_free (addrs[res]);
2835 GNUNET_free_non_null (addrs);
2836 GNUNET_free_non_null (addrlens);
2841 * Stop NAT for addresses
2843 * @param plugin the plugin handle
2846 server_stop_report_addresses (struct HTTP_Server_Plugin *plugin)
2848 struct HttpAddressWrapper *w;
2850 /* Stop NAT handle */
2851 if (NULL != plugin->nat)
2853 GNUNET_NAT_unregister (plugin->nat);
2856 /* Clean up addresses */
2857 while (NULL != plugin->addr_head)
2859 w = plugin->addr_head;
2860 GNUNET_CONTAINER_DLL_remove (plugin->addr_head,
2863 GNUNET_free (w->address);
2870 * Check if IPv6 supported on this system
2872 * @param plugin the plugin handle
2873 * @return #GNUNET_YES on success, else #GNUNET_NO
2876 server_check_ipv6_support (struct HTTP_Server_Plugin *plugin)
2878 struct GNUNET_NETWORK_Handle *desc = NULL;
2879 int res = GNUNET_NO;
2881 /* Probe IPv6 support */
2882 desc = GNUNET_NETWORK_socket_create (PF_INET6,
2887 if ( (errno == ENOBUFS) ||
2888 (errno == ENOMEM) ||
2889 (errno == ENFILE) ||
2892 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR,
2895 LOG (GNUNET_ERROR_TYPE_WARNING,
2896 _("Disabling IPv6 since it is not supported on this system!\n"));
2901 GNUNET_break (GNUNET_OK ==
2902 GNUNET_NETWORK_socket_close (desc));
2906 LOG (GNUNET_ERROR_TYPE_DEBUG,
2907 "Testing IPv6 on this system: %s\n",
2908 (res == GNUNET_YES) ? "successful" : "failed");
2914 * Notify server about our external hostname
2919 server_notify_external_hostname (void *cls)
2921 struct HTTP_Server_Plugin *plugin = cls;
2922 struct HttpAddress *ext_addr;
2923 size_t ext_addr_len;
2927 plugin->notify_ext_task = NULL;
2928 GNUNET_asprintf (&url,
2931 plugin->external_hostname);
2932 urlen = strlen (url) + 1;
2933 ext_addr = GNUNET_malloc (sizeof (struct HttpAddress) + urlen);
2934 ext_addr->options = htonl (plugin->options);
2935 ext_addr->urlen = htonl (urlen);
2936 ext_addr_len = sizeof (struct HttpAddress) + urlen;
2937 GNUNET_memcpy (&ext_addr[1], url, urlen);
2940 LOG (GNUNET_ERROR_TYPE_DEBUG,
2941 "Notifying transport about external hostname address `%s'\n",
2942 plugin->external_hostname);
2945 if (GNUNET_YES == plugin->verify_external_hostname)
2946 LOG (GNUNET_ERROR_TYPE_INFO,
2947 "Enabling SSL verification for external hostname address `%s'\n",
2948 plugin->external_hostname);
2950 = GNUNET_HELLO_address_allocate (plugin->env->my_identity,
2954 GNUNET_HELLO_ADDRESS_INFO_NONE);
2955 plugin->env->notify_address (plugin->env->cls,
2958 GNUNET_free (ext_addr);
2961 = GNUNET_HELLO_address_allocate (plugin->env->my_identity,
2965 GNUNET_HELLO_ADDRESS_INFO_NONE);
2966 plugin->env->notify_address (plugin->env->cls,
2969 GNUNET_free (ext_addr);
2975 * Configure the plugin
2977 * @param plugin plugin handle
2978 * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
2981 server_configure_plugin (struct HTTP_Server_Plugin *plugin)
2983 unsigned long long port;
2984 unsigned long long max_connections;
2985 char *bind4_address = NULL;
2986 char *bind6_address = NULL;
2987 char *eh_tmp = NULL;
2988 int external_hostname_use_port;
2991 if (GNUNET_CONFIGURATION_have_value
2992 (plugin->env->cfg, plugin->name, "USE_IPv4"))
2995 GNUNET_CONFIGURATION_get_value_yesno (plugin->env->cfg,
3000 plugin->use_ipv4 = GNUNET_YES;
3001 LOG (GNUNET_ERROR_TYPE_DEBUG,
3002 _("IPv4 support is %s\n"),
3003 (plugin->use_ipv4 == GNUNET_YES) ? "enabled" : "disabled");
3006 if (GNUNET_CONFIGURATION_have_value
3007 (plugin->env->cfg, plugin->name, "USE_IPv6"))
3010 GNUNET_CONFIGURATION_get_value_yesno (plugin->env->cfg,
3015 plugin->use_ipv6 = GNUNET_YES;
3016 LOG (GNUNET_ERROR_TYPE_DEBUG,
3017 _("IPv6 support is %s\n"),
3018 (plugin->use_ipv6 == GNUNET_YES) ? "enabled" : "disabled");
3020 if ((plugin->use_ipv4 == GNUNET_NO) && (plugin->use_ipv6 == GNUNET_NO))
3022 LOG (GNUNET_ERROR_TYPE_ERROR,
3023 _("Neither IPv4 nor IPv6 are enabled! Fix in configuration\n"));
3024 return GNUNET_SYSERR;
3027 /* Reading port number from config file */
3029 GNUNET_CONFIGURATION_get_value_number (plugin->env->cfg,
3031 "PORT", &port)) || (port > 65535))
3033 LOG (GNUNET_ERROR_TYPE_ERROR,
3034 _("Port is required! Fix in configuration\n"));
3035 return GNUNET_SYSERR;
3037 plugin->port = port;
3039 LOG (GNUNET_ERROR_TYPE_INFO,
3040 _("Using port %u\n"), plugin->port);
3042 if ( (plugin->use_ipv4 == GNUNET_YES) &&
3044 GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg,
3049 LOG (GNUNET_ERROR_TYPE_DEBUG,
3050 "Binding %s plugin to specific IPv4 address: `%s'\n",
3053 plugin->server_addr_v4 = GNUNET_new (struct sockaddr_in);
3054 if (1 != inet_pton (AF_INET,
3056 &plugin->server_addr_v4->sin_addr))
3058 LOG (GNUNET_ERROR_TYPE_ERROR,
3059 _("Specific IPv4 address `%s' in configuration file is invalid!\n"),
3061 GNUNET_free (bind4_address);
3062 GNUNET_free (plugin->server_addr_v4);
3063 plugin->server_addr_v4 = NULL;
3064 return GNUNET_SYSERR;
3068 LOG (GNUNET_ERROR_TYPE_DEBUG,
3069 "Binding to IPv4 address %s\n",
3071 plugin->server_addr_v4->sin_family = AF_INET;
3072 plugin->server_addr_v4->sin_port = htons (plugin->port);
3074 GNUNET_free (bind4_address);
3077 if ((plugin->use_ipv6 == GNUNET_YES) &&
3079 GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg,
3084 LOG (GNUNET_ERROR_TYPE_DEBUG,
3085 "Binding %s plugin to specific IPv6 address: `%s'\n",
3086 plugin->protocol, bind6_address);
3087 plugin->server_addr_v6 = GNUNET_new (struct sockaddr_in6);
3089 inet_pton (AF_INET6,
3091 &plugin->server_addr_v6->sin6_addr))
3093 LOG (GNUNET_ERROR_TYPE_ERROR,
3094 _("Specific IPv6 address `%s' in configuration file is invalid!\n"),
3096 GNUNET_free (bind6_address);
3097 GNUNET_free (plugin->server_addr_v6);
3098 plugin->server_addr_v6 = NULL;
3099 return GNUNET_SYSERR;
3103 LOG (GNUNET_ERROR_TYPE_DEBUG,
3104 "Binding to IPv6 address %s\n",
3106 plugin->server_addr_v6->sin6_family = AF_INET6;
3107 plugin->server_addr_v6->sin6_port = htons (plugin->port);
3109 GNUNET_free (bind6_address);
3112 plugin->verify_external_hostname = GNUNET_NO;
3114 plugin->verify_external_hostname
3115 = GNUNET_CONFIGURATION_get_value_yesno (plugin->env->cfg,
3117 "VERIFY_EXTERNAL_HOSTNAME");
3118 if (GNUNET_SYSERR == plugin->verify_external_hostname)
3119 plugin->verify_external_hostname = GNUNET_NO;
3120 if (GNUNET_YES == plugin->verify_external_hostname)
3121 plugin->options |= HTTP_OPTIONS_VERIFY_CERTIFICATE;
3123 external_hostname_use_port
3124 = GNUNET_CONFIGURATION_get_value_yesno (plugin->env->cfg,
3126 "EXTERNAL_HOSTNAME_USE_PORT");
3127 if (GNUNET_SYSERR == external_hostname_use_port)
3128 external_hostname_use_port = GNUNET_NO;
3132 GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg,
3134 "EXTERNAL_HOSTNAME",
3139 char *pos_url = NULL;
3141 if (NULL != strstr(eh_tmp, "://"))
3142 tmp = &strstr(eh_tmp, "://")[3];
3146 if (GNUNET_YES == external_hostname_use_port)
3148 if ( (strlen (tmp) > 1) && (NULL != (pos = strchr(tmp, '/'))) )
3152 GNUNET_asprintf (&plugin->external_hostname,
3159 GNUNET_asprintf (&plugin->external_hostname,
3165 plugin->external_hostname = GNUNET_strdup (tmp);
3166 GNUNET_free (eh_tmp);
3168 LOG (GNUNET_ERROR_TYPE_INFO,
3169 _("Using external hostname `%s'\n"),
3170 plugin->external_hostname);
3171 plugin->notify_ext_task = GNUNET_SCHEDULER_add_now (&server_notify_external_hostname,
3174 /* Use only configured external hostname */
3175 if (GNUNET_CONFIGURATION_have_value
3178 "EXTERNAL_HOSTNAME_ONLY"))
3180 plugin->external_only =
3181 GNUNET_CONFIGURATION_get_value_yesno (plugin->env->cfg,
3183 "EXTERNAL_HOSTNAME_ONLY");
3186 plugin->external_only = GNUNET_NO;
3188 if (GNUNET_YES == plugin->external_only)
3189 LOG (GNUNET_ERROR_TYPE_DEBUG,
3190 _("Notifying transport only about hostname `%s'\n"),
3191 plugin->external_hostname);
3194 LOG (GNUNET_ERROR_TYPE_DEBUG,
3195 "No external hostname configured\n");
3197 /* Optional parameters */
3199 GNUNET_CONFIGURATION_get_value_number (plugin->env->cfg,
3203 max_connections = 128;
3204 plugin->max_request = max_connections;
3206 LOG (GNUNET_ERROR_TYPE_DEBUG,
3207 _("Maximum number of connections is %u\n"),
3208 plugin->max_request);
3210 plugin->peer_id_length = strlen (GNUNET_i2s_full (plugin->env->my_identity));
3217 * Exit point from the plugin.
3223 LIBGNUNET_PLUGIN_TRANSPORT_DONE (void *cls)
3225 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
3226 struct HTTP_Server_Plugin *plugin = api->cls;
3228 if (NULL == api->cls)
3230 /* Free for stub mode */
3234 plugin->in_shutdown = GNUNET_YES;
3235 LOG (GNUNET_ERROR_TYPE_INFO,
3236 _("Shutting down plugin `%s'\n"),
3239 if (NULL != plugin->notify_ext_task)
3241 GNUNET_SCHEDULER_cancel (plugin->notify_ext_task);
3242 plugin->notify_ext_task = NULL;
3245 if (NULL != plugin->ext_addr)
3247 LOG (GNUNET_ERROR_TYPE_DEBUG,
3248 "Notifying transport to remove address `%s'\n",
3249 http_common_plugin_address_to_string (plugin->protocol,
3250 plugin->ext_addr->address,
3251 plugin->ext_addr->address_length));
3253 plugin->env->notify_address (plugin->env->cls,
3257 plugin->env->notify_address (plugin->env->cls,
3261 GNUNET_HELLO_address_free (plugin->ext_addr);
3262 plugin->ext_addr = NULL;
3265 /* Stop to report addresses to transport service */
3266 server_stop_report_addresses (plugin);
3267 if (NULL != plugin->server_v4_task)
3269 GNUNET_SCHEDULER_cancel (plugin->server_v4_task);
3270 plugin->server_v4_task = NULL;
3273 if (NULL != plugin->server_v6_task)
3275 GNUNET_SCHEDULER_cancel (plugin->server_v6_task);
3276 plugin->server_v6_task = NULL;
3279 GNUNET_free_non_null (plugin->crypto_init);
3280 GNUNET_free_non_null (plugin->cert);
3281 GNUNET_free_non_null (plugin->key);
3283 GNUNET_CONTAINER_multipeermap_iterate (plugin->sessions,
3284 &destroy_session_shutdown_cb,
3286 GNUNET_CONTAINER_multipeermap_destroy (plugin->sessions);
3287 plugin->sessions = NULL;
3288 if (NULL != plugin->server_v4)
3290 MHD_stop_daemon (plugin->server_v4);
3291 plugin->server_v4 = NULL;
3293 if (NULL != plugin->server_v6)
3295 MHD_stop_daemon (plugin->server_v6);
3296 plugin->server_v6 = NULL;
3299 GNUNET_free_non_null (plugin->external_hostname);
3300 GNUNET_free_non_null (plugin->ext_addr);
3301 GNUNET_free_non_null (plugin->server_addr_v4);
3302 GNUNET_free_non_null (plugin->server_addr_v6);
3303 regfree (&plugin->url_regex);
3305 LOG (GNUNET_ERROR_TYPE_DEBUG,
3306 _("Shutdown for plugin `%s' complete\n"),
3309 GNUNET_free (plugin);
3316 * Function called for a quick conversion of the binary address to
3317 * a numeric address. Note that the caller must not free the
3318 * address and that the next call to this function is allowed
3319 * to override the address again.
3322 * @param addr binary address
3323 * @param addrlen length of the address
3324 * @return string representing the same address
3327 http_server_plugin_address_to_string (void *cls,
3331 return http_common_plugin_address_to_string (PLUGIN_NAME,
3338 * Function obtain the network type for a session
3340 * @param cls closure (`struct HTTP_Server_Plugin *`)
3341 * @param session the session
3342 * @return the network type in HBO or #GNUNET_SYSERR
3344 static enum GNUNET_ATS_Network_Type
3345 http_server_plugin_get_network (void *cls,
3346 struct GNUNET_ATS_Session *session)
3348 return session->scope;
3353 * Function obtain the network type for an address.
3355 * @param cls closure (`struct Plugin *`)
3356 * @param address the address
3357 * @return the network type
3359 static enum GNUNET_ATS_Network_Type
3360 http_server_plugin_get_network_for_address (void *cls,
3361 const struct GNUNET_HELLO_Address *address)
3363 struct HTTP_Server_Plugin *plugin = cls;
3365 return http_common_get_network_for_address (plugin->env,
3371 * Function that will be called whenever the transport service wants to
3372 * notify the plugin that the inbound quota changed and that the plugin
3373 * should update it's delay for the next receive value
3375 * @param cls closure
3376 * @param peer which peer was the session for
3377 * @param session which session is being updated
3378 * @param delay new delay to use for receiving
3381 http_server_plugin_update_inbound_delay (void *cls,
3382 const struct GNUNET_PeerIdentity *peer,
3383 struct GNUNET_ATS_Session *session,
3384 struct GNUNET_TIME_Relative delay)
3386 session->next_receive = GNUNET_TIME_relative_to_absolute (delay);
3387 LOG (GNUNET_ERROR_TYPE_DEBUG,
3388 "New inbound delay %s\n",
3389 GNUNET_STRINGS_relative_time_to_string (delay,
3391 if (NULL != session->recv_wakeup_task)
3393 GNUNET_SCHEDULER_cancel (session->recv_wakeup_task);
3394 session->recv_wakeup_task
3395 = GNUNET_SCHEDULER_add_delayed (delay,
3403 * Return information about the given session to the
3406 * @param cls the `struct Plugin` with the monitor callback (`sic`)
3407 * @param peer peer we send information about
3408 * @param value our `struct GNUNET_ATS_Session` to send information about
3409 * @return #GNUNET_OK (continue to iterate)
3412 send_session_info_iter (void *cls,
3413 const struct GNUNET_PeerIdentity *peer,
3416 struct HTTP_Server_Plugin *plugin = cls;
3417 struct GNUNET_ATS_Session *session = value;
3419 notify_session_monitor (plugin,
3421 GNUNET_TRANSPORT_SS_INIT);
3427 * Begin monitoring sessions of a plugin. There can only
3428 * be one active monitor per plugin (i.e. if there are
3429 * multiple monitors, the transport service needs to
3430 * multiplex the generated events over all of them).
3432 * @param cls closure of the plugin
3433 * @param sic callback to invoke, NULL to disable monitor;
3434 * plugin will being by iterating over all active
3435 * sessions immediately and then enter monitor mode
3436 * @param sic_cls closure for @a sic
3439 http_server_plugin_setup_monitor (void *cls,
3440 GNUNET_TRANSPORT_SessionInfoCallback sic,
3443 struct HTTP_Server_Plugin *plugin = cls;
3446 plugin->sic_cls = sic_cls;
3449 GNUNET_CONTAINER_multipeermap_iterate (plugin->sessions,
3450 &send_session_info_iter,
3452 /* signal end of first iteration */
3453 sic (sic_cls, NULL, NULL);
3459 * Entry point for the plugin.
3465 LIBGNUNET_PLUGIN_TRANSPORT_INIT (void *cls)
3467 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
3468 struct GNUNET_TRANSPORT_PluginFunctions *api;
3469 struct HTTP_Server_Plugin *plugin;
3471 if (NULL == env->receive)
3473 /* run in 'stub' mode (i.e. as part of gnunet-peerinfo), don't fully
3474 initialze the plugin or the API */
3475 api = GNUNET_new (struct GNUNET_TRANSPORT_PluginFunctions);
3477 api->address_to_string = &http_server_plugin_address_to_string;
3478 api->string_to_address = &http_common_plugin_string_to_address;
3479 api->address_pretty_printer = &http_common_plugin_address_pretty_printer;
3482 plugin = GNUNET_new (struct HTTP_Server_Plugin);
3484 plugin->sessions = GNUNET_CONTAINER_multipeermap_create (128,
3487 api = GNUNET_new (struct GNUNET_TRANSPORT_PluginFunctions);
3489 api->send = &http_server_plugin_send;
3490 api->disconnect_peer = &http_server_plugin_disconnect_peer;
3491 api->disconnect_session = &http_server_plugin_disconnect_session;
3492 api->query_keepalive_factor = &http_server_query_keepalive_factor;
3493 api->check_address = &http_server_plugin_address_suggested;
3494 api->get_session = &http_server_plugin_get_session;
3496 api->address_to_string = &http_server_plugin_address_to_string;
3497 api->string_to_address = &http_common_plugin_string_to_address;
3498 api->address_pretty_printer = &http_common_plugin_address_pretty_printer;
3499 api->get_network = &http_server_plugin_get_network;
3500 api->get_network_for_address = &http_server_plugin_get_network_for_address;
3501 api->update_session_timeout = &http_server_plugin_update_session_timeout;
3502 api->update_inbound_delay = &http_server_plugin_update_inbound_delay;
3503 api->setup_monitor = &http_server_plugin_setup_monitor;
3505 plugin->name = "transport-https_server";
3506 plugin->protocol = "https";
3508 plugin->name = "transport-http_server";
3509 plugin->protocol = "http";
3513 GNUNET_CONFIGURATION_get_value_yesno (env->cfg,
3518 plugin->options |= HTTP_OPTIONS_TCP_STEALTH;
3520 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3521 _("TCP_STEALTH not supported on this platform.\n"));
3522 LIBGNUNET_PLUGIN_TRANSPORT_DONE (api);
3527 /* Compile URL regex */
3528 if (regcomp (&plugin->url_regex,
3532 LOG (GNUNET_ERROR_TYPE_ERROR,
3533 _("Unable to compile URL regex\n"));
3534 LIBGNUNET_PLUGIN_TRANSPORT_DONE (api);
3538 /* Configure plugin */
3539 if (GNUNET_SYSERR == server_configure_plugin (plugin))
3541 LIBGNUNET_PLUGIN_TRANSPORT_DONE (api);
3545 /* Check IPv6 support */
3546 if (GNUNET_YES == plugin->use_ipv6)
3547 plugin->use_ipv6 = server_check_ipv6_support (plugin);
3549 /* Report addresses to transport service */
3550 if (GNUNET_NO == plugin->external_only)
3551 server_start_report_addresses (plugin);
3553 if (GNUNET_SYSERR == server_start (plugin))
3555 LIBGNUNET_PLUGIN_TRANSPORT_DONE (api);
3561 /* end of plugin_transport_http_server.c */