2 This file is part of GNUnet
3 Copyright (C) 2002-2014 Christian Grothoff (and other contributing authors)
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 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.
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 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
494 * @param tc task context
497 server_wake_up (void *cls,
498 const struct GNUNET_SCHEDULER_TaskContext *tc)
500 struct Session *s = cls;
502 s->recv_wakeup_task = NULL;
503 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
505 LOG (GNUNET_ERROR_TYPE_DEBUG,
506 "Session %p: Waking up PUT handle\n",
508 MHD_resume_connection (s->server_recv->mhd_conn);
513 * Reschedule the execution of both IPv4 and IPv6 server.
515 * @param plugin the plugin
516 * @param server which server to schedule v4 or v6?
517 * @param now #GNUNET_YES to schedule execution immediately, #GNUNET_NO to wait
521 server_reschedule (struct HTTP_Server_Plugin *plugin,
522 struct MHD_Daemon *server,
527 * Deletes the session. Must not be used afterwards.
529 * @param s the session to delete
532 server_delete_session (struct Session *s)
534 struct HTTP_Server_Plugin *plugin = s->plugin;
535 struct HTTP_Message *msg;
537 if (NULL != s->timeout_task)
539 GNUNET_SCHEDULER_cancel (s->timeout_task);
540 s->timeout_task = NULL;
541 s->timeout = GNUNET_TIME_UNIT_ZERO_ABS;
543 if (NULL != s->recv_wakeup_task)
545 GNUNET_SCHEDULER_cancel (s->recv_wakeup_task);
546 s->recv_wakeup_task = NULL;
547 if (NULL != s->server_recv)
548 MHD_resume_connection (s->server_recv->mhd_conn);
550 GNUNET_assert (GNUNET_OK ==
551 GNUNET_CONTAINER_multipeermap_remove (plugin->sessions,
554 while (NULL != (msg = s->msg_head))
556 GNUNET_CONTAINER_DLL_remove (s->msg_head,
559 if (NULL != msg->transmit_cont)
560 msg->transmit_cont (msg->transmit_cont_cls,
564 msg->pos + msg->overhead);
565 GNUNET_assert (s->msgs_in_queue > 0);
567 GNUNET_assert (s->bytes_in_queue >= msg->size);
568 s->bytes_in_queue -= msg->size;
572 GNUNET_assert (0 == s->msgs_in_queue);
573 GNUNET_assert (0 == s->bytes_in_queue);
575 if (NULL != s->server_send)
577 LOG (GNUNET_ERROR_TYPE_DEBUG,
578 "Server: %p / %p Terminating inbound PUT session to peer `%s'\n",
580 GNUNET_i2s (&s->target));
581 s->server_send->session = NULL;
582 MHD_set_connection_option (s->server_send->mhd_conn,
583 MHD_CONNECTION_OPTION_TIMEOUT,
584 1 /* 0 = no timeout, so this is MIN */);
585 server_reschedule (plugin, s->server_send->mhd_daemon, GNUNET_YES);
588 if (NULL != s->server_recv)
590 LOG (GNUNET_ERROR_TYPE_DEBUG,
591 "Server: %p / %p Terminating inbound GET session to peer `%s'\n",
592 s, s->server_recv, GNUNET_i2s (&s->target));
593 s->server_recv->session = NULL;
594 MHD_set_connection_option (s->server_recv->mhd_conn,
595 MHD_CONNECTION_OPTION_TIMEOUT,
596 1 /* 0 = no timeout, so this is MIN */);
597 server_reschedule (plugin, s->server_recv->mhd_daemon, GNUNET_YES);
599 notify_session_monitor (plugin,
601 GNUNET_TRANSPORT_SS_DONE);
602 if (GNUNET_YES == s->known_to_service)
604 plugin->env->session_end (plugin->env->cls,
607 s->known_to_service = GNUNET_NO;
609 if (NULL != s->msg_tk)
611 GNUNET_SERVER_mst_destroy (s->msg_tk);
614 GNUNET_HELLO_address_free (s->address);
615 LOG (GNUNET_ERROR_TYPE_DEBUG,
616 "Session %p destroyed\n",
624 * Disconnect session @a s by telling MHD to close the
625 * connections (reducing timeout, etc.).
627 * @param cls closure with the `struct HTTP_Server_Plugin`
628 * @param s the session
629 * @return #GNUNET_OK on success
632 http_server_plugin_disconnect_session (void *cls,
635 server_delete_session (s);
641 * Session was idle, so disconnect it
643 * @param cls the session
644 * @param tc task context
647 server_session_timeout (void *cls,
648 const struct GNUNET_SCHEDULER_TaskContext *tc)
650 struct Session *s = cls;
651 struct GNUNET_TIME_Relative left;
653 s->timeout_task = NULL;
654 left = GNUNET_TIME_absolute_get_remaining (s->timeout);
655 if (0 != left.rel_value_us)
657 /* not actually our turn yet, but let's at least update
658 the monitor, it may think we're about to die ... */
659 notify_session_monitor (s->plugin,
661 GNUNET_TRANSPORT_SS_UP);
662 s->timeout_task = GNUNET_SCHEDULER_add_delayed (left,
663 &server_session_timeout,
667 GNUNET_log (TIMEOUT_LOG,
668 "Session %p was idle for %s, disconnecting\n",
670 GNUNET_STRINGS_relative_time_to_string (HTTP_SERVER_SESSION_TIMEOUT,
672 server_delete_session (s);
677 * Increment session timeout due to activity session @a s
679 * @param s the session
682 server_reschedule_session_timeout (struct Session *s)
684 GNUNET_assert (NULL != s->timeout_task);
685 s->timeout = GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
690 * Function that can be used by the transport service to transmit
691 * a message using the plugin. Note that in the case of a
692 * peer disconnecting, the continuation MUST be called
693 * prior to the disconnect notification itself. This function
694 * will be called with this peer's HELLO message to initiate
695 * a fresh connection to another peer.
698 * @param session which session must be used
699 * @param msgbuf the message to transmit
700 * @param msgbuf_size number of bytes in @a msgbuf
701 * @param priority how important is the message (most plugins will
702 * ignore message priority and just FIFO)
703 * @param to how long to wait at most for the transmission (does not
704 * require plugins to discard the message after the timeout,
705 * just advisory for the desired delay; most plugins will ignore
707 * @param cont continuation to call once the message has
708 * been transmitted (or if the transport is ready
709 * for the next transmission call; or if the
710 * peer disconnected...); can be NULL
711 * @param cont_cls closure for @a cont
712 * @return number of bytes used (on the physical network, with overheads);
713 * -1 on hard errors (i.e. address invalid); 0 is a legal value
714 * and does NOT mean that the message was not transmitted (DV)
717 http_server_plugin_send (void *cls,
718 struct Session *session,
721 unsigned int priority,
722 struct GNUNET_TIME_Relative to,
723 GNUNET_TRANSPORT_TransmitContinuation cont,
726 struct HTTP_Server_Plugin *plugin = cls;
727 struct HTTP_Message *msg;
728 ssize_t bytes_sent = 0;
731 LOG (GNUNET_ERROR_TYPE_DEBUG,
732 "Session %p/request %p: Sending message with %u to peer `%s'\n",
734 session->server_send,
736 GNUNET_i2s (&session->target));
738 /* create new message and schedule */
739 bytes_sent = sizeof (struct HTTP_Message) + msgbuf_size;
740 msg = GNUNET_malloc (bytes_sent);
742 msg->size = msgbuf_size;
744 msg->buf = (char *) &msg[1];
745 msg->transmit_cont = cont;
746 msg->transmit_cont_cls = cont_cls;
750 GNUNET_CONTAINER_DLL_insert_tail (session->msg_head,
753 session->msgs_in_queue++;
754 session->bytes_in_queue += msg->size;
755 notify_session_monitor (plugin,
757 GNUNET_TRANSPORT_SS_UP);
758 GNUNET_asprintf (&stat_txt,
759 "# bytes currently in %s_server buffers",
761 GNUNET_STATISTICS_update (plugin->env->stats,
762 stat_txt, msgbuf_size, GNUNET_NO);
763 GNUNET_free (stat_txt);
765 if (NULL != session->server_send)
766 server_reschedule (session->plugin,
767 session->server_send->mhd_daemon,
774 * Terminate session during shutdown.
776 * @param cls the `struct HTTP_Server_Plugin *`
777 * @param peer for which this is a session
778 * @param value the `struct Session` to clean up
779 * @return #GNUNET_OK (continue to iterate)
782 destroy_session_shutdown_cb (void *cls,
783 const struct GNUNET_PeerIdentity *peer,
786 struct Session *s = value;
787 struct ServerRequest *sc_send;
788 struct ServerRequest *sc_recv;
790 sc_send = s->server_send;
791 sc_recv = s->server_recv;
792 server_delete_session (s);
794 GNUNET_free_non_null (sc_send);
795 GNUNET_free_non_null (sc_recv);
803 * @param cls the `struct HTTP_Server_Plugin *`
804 * @param peer for which this is a session
805 * @param value the `struct Session` to clean up
806 * @return #GNUNET_OK (continue to iterate)
809 destroy_session_cb (void *cls,
810 const struct GNUNET_PeerIdentity *peer,
813 struct Session *s = value;
815 server_delete_session (s);
820 * Function that can be used to force the plugin to disconnect
821 * from the given peer and cancel all previous transmissions
822 * (and their continuationc).
825 * @param target peer from which to disconnect
828 http_server_plugin_disconnect_peer (void *cls,
829 const struct GNUNET_PeerIdentity *target)
831 struct HTTP_Server_Plugin *plugin = cls;
833 LOG (GNUNET_ERROR_TYPE_DEBUG,
834 "Transport tells me to disconnect `%s'\n",
835 GNUNET_i2s (target));
836 GNUNET_CONTAINER_multipeermap_get_multiple (plugin->sessions,
844 * Another peer has suggested an address for this
845 * peer and transport plugin. Check that this could be a valid
846 * address. If so, consider adding it to the list
850 * @param addr pointer to the address
851 * @param addrlen length of @a addr
852 * @return #GNUNET_OK if this is a plausible address for this peer
856 http_server_plugin_address_suggested (void *cls,
860 struct HTTP_Server_Plugin *plugin = cls;
861 struct HttpAddressWrapper *next;
862 struct HttpAddressWrapper *pos;
863 const struct HttpAddress *haddr = addr;
865 if ((NULL != plugin->ext_addr) &&
866 GNUNET_YES == (http_common_cmp_addresses (addr, addrlen,
867 plugin->ext_addr->address,
868 plugin->ext_addr->address_length)))
870 /* Checking HTTP_OPTIONS_VERIFY_CERTIFICATE option for external hostname */
871 if ((ntohl (haddr->options) & HTTP_OPTIONS_VERIFY_CERTIFICATE) !=
872 (plugin->options & HTTP_OPTIONS_VERIFY_CERTIFICATE))
873 return GNUNET_NO; /* VERIFY option not set as required! */
876 next = plugin->addr_head;
877 while (NULL != (pos = next))
880 if (GNUNET_YES == (http_common_cmp_addresses(addr,
891 * Creates a new outbound session the transport
892 * service will use to send data to the peer.
894 * Since HTTP/S server cannot create sessions, always returns NULL.
896 * @param cls the plugin
897 * @param address the address
898 * @return always NULL
900 static struct Session *
901 http_server_plugin_get_session (void *cls,
902 const struct GNUNET_HELLO_Address *address)
909 * Call MHD IPv4 to process pending requests and then go back
910 * and schedule the next run.
912 * @param cls plugin as closure
913 * @param tc task context
916 server_v4_run (void *cls,
917 const struct GNUNET_SCHEDULER_TaskContext *tc)
919 struct HTTP_Server_Plugin *plugin = cls;
921 plugin->server_v4_task = NULL;
922 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
924 plugin->server_v4_immediately = GNUNET_NO;
925 GNUNET_assert (MHD_YES == MHD_run (plugin->server_v4));
926 server_reschedule (plugin, plugin->server_v4, GNUNET_NO);
931 * Call MHD IPv6 to process pending requests and then go back
932 * and schedule the next run.
934 * @param cls plugin as closure
935 * @param tc task context
938 server_v6_run (void *cls,
939 const struct GNUNET_SCHEDULER_TaskContext *tc)
941 struct HTTP_Server_Plugin *plugin = cls;
943 plugin->server_v6_task = NULL;
944 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
946 plugin->server_v6_immediately = GNUNET_NO;
947 GNUNET_assert (MHD_YES == MHD_run (plugin->server_v6));
948 server_reschedule (plugin, plugin->server_v6, GNUNET_NO);
953 * Function that queries MHD's select sets and
954 * starts the task waiting for them.
956 * @param plugin plugin
957 * @param daemon_handle the MHD daemon handle
958 * @param now schedule now
959 * @return gnunet task identifier
961 static struct GNUNET_SCHEDULER_Task *
962 server_schedule (struct HTTP_Server_Plugin *plugin,
963 struct MHD_Daemon *daemon_handle,
966 struct GNUNET_SCHEDULER_Task * ret;
970 struct GNUNET_NETWORK_FDSet *wrs;
971 struct GNUNET_NETWORK_FDSet *wws;
973 MHD_UNSIGNED_LONG_LONG timeout;
974 static unsigned long long last_timeout = 0;
976 struct GNUNET_TIME_Relative tv;
978 if (GNUNET_YES == plugin->in_shutdown)
985 wrs = GNUNET_NETWORK_fdset_create ();
986 wws = GNUNET_NETWORK_fdset_create ();
988 GNUNET_assert (MHD_YES ==
989 MHD_get_fdset (daemon_handle,
994 haveto = MHD_get_timeout (daemon_handle, &timeout);
995 if (haveto == MHD_YES)
997 if (timeout != last_timeout)
999 LOG (GNUNET_ERROR_TYPE_DEBUG,
1000 "SELECT Timeout changed from %llu to %llu (ms)\n",
1001 last_timeout, timeout);
1002 last_timeout = timeout;
1004 if (timeout <= GNUNET_TIME_UNIT_SECONDS.rel_value_us / 1000LL)
1005 tv.rel_value_us = (uint64_t) timeout * 1000LL;
1007 tv = GNUNET_TIME_UNIT_SECONDS;
1010 tv = GNUNET_TIME_UNIT_SECONDS;
1011 /* Force immediate run, since we have outbound data to send */
1012 if (now == GNUNET_YES)
1013 tv = GNUNET_TIME_UNIT_MILLISECONDS;
1014 GNUNET_NETWORK_fdset_copy_native (wrs, &rs, max + 1);
1015 GNUNET_NETWORK_fdset_copy_native (wws, &ws, max + 1);
1017 if (daemon_handle == plugin->server_v4)
1019 if (plugin->server_v4_task != NULL)
1021 GNUNET_SCHEDULER_cancel (plugin->server_v4_task);
1022 plugin->server_v4_task = NULL;
1025 LOG (GNUNET_ERROR_TYPE_DEBUG,
1026 "Scheduling IPv4 server task in %llu ms\n",
1030 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1032 &server_v4_run, plugin);
1034 if (daemon_handle == plugin->server_v6)
1036 if (plugin->server_v6_task != NULL)
1038 GNUNET_SCHEDULER_cancel (plugin->server_v6_task);
1039 plugin->server_v6_task = NULL;
1042 LOG (GNUNET_ERROR_TYPE_DEBUG,
1043 "Scheduling IPv6 server task in %llu ms\n", tv);
1046 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1048 &server_v6_run, plugin);
1050 GNUNET_NETWORK_fdset_destroy (wrs);
1051 GNUNET_NETWORK_fdset_destroy (wws);
1057 * Reschedule the execution of both IPv4 and IPv6 server
1059 * @param plugin the plugin
1060 * @param server which server to schedule v4 or v6?
1061 * @param now #GNUNET_YES to schedule execution immediately, #GNUNET_NO to wait
1065 server_reschedule (struct HTTP_Server_Plugin *plugin,
1066 struct MHD_Daemon *server,
1069 if ((server == plugin->server_v4) && (plugin->server_v4 != NULL))
1071 if (GNUNET_YES == plugin->server_v4_immediately)
1072 return; /* No rescheduling, server will run asap */
1074 if (GNUNET_YES == now)
1075 plugin->server_v4_immediately = GNUNET_YES;
1077 if (plugin->server_v4_task != NULL)
1079 GNUNET_SCHEDULER_cancel (plugin->server_v4_task);
1080 plugin->server_v4_task = NULL;
1082 plugin->server_v4_task = server_schedule (plugin, plugin->server_v4, now);
1085 if ((server == plugin->server_v6) && (plugin->server_v6 != NULL))
1087 if (GNUNET_YES == plugin->server_v6_immediately)
1088 return; /* No rescheduling, server will run asap */
1090 if (GNUNET_YES == now)
1091 plugin->server_v6_immediately = GNUNET_YES;
1093 if (plugin->server_v6_task != NULL)
1095 GNUNET_SCHEDULER_cancel (plugin->server_v6_task);
1096 plugin->server_v6_task = NULL;
1098 plugin->server_v6_task = server_schedule (plugin, plugin->server_v6, now);
1104 * Function that is called to get the keepalive factor.
1105 * GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT is divided by this number to
1106 * calculate the interval between keepalive packets.
1108 * @param cls closure with the `struct HTTP_Server_Plugin`
1109 * @return keepalive factor
1112 http_server_query_keepalive_factor (void *cls)
1119 * Function that will be called whenever the transport service wants to
1120 * notify the plugin that a session is still active and in use and
1121 * therefore the session timeout for this session has to be updated
1123 * @param cls closure
1124 * @param peer which peer was the session for
1125 * @param session which session is being updated
1128 http_server_plugin_update_session_timeout (void *cls,
1129 const struct GNUNET_PeerIdentity *peer,
1130 struct Session *session)
1132 server_reschedule_session_timeout (session);
1137 * Tell MHD that the connection should timeout after @a to seconds.
1139 * @param plugin our plugin
1140 * @param s session for which the timeout changes
1141 * @param to timeout in seconds
1144 server_mhd_connection_timeout (struct HTTP_Server_Plugin *plugin,
1148 /* Setting timeouts for other connections */
1149 if (NULL != s->server_recv)
1151 LOG (GNUNET_ERROR_TYPE_DEBUG,
1152 "Setting timeout for %p to %u sec.\n",
1153 s->server_recv, to);
1154 MHD_set_connection_option (s->server_recv->mhd_conn,
1155 MHD_CONNECTION_OPTION_TIMEOUT,
1157 server_reschedule (plugin, s->server_recv->mhd_daemon, GNUNET_NO);
1159 if (NULL != s->server_send)
1161 LOG (GNUNET_ERROR_TYPE_DEBUG,
1162 "Setting timeout for %p to %u sec.\n",
1163 s->server_send, to);
1164 MHD_set_connection_option (s->server_send->mhd_conn,
1165 MHD_CONNECTION_OPTION_TIMEOUT,
1167 server_reschedule (plugin, s->server_send->mhd_daemon, GNUNET_NO);
1173 * Parse incoming URL for tag and target
1175 * @param plugin plugin
1176 * @param url incoming url
1177 * @param target where to store the target
1178 * @param tag where to store the tag
1179 * @param options where to store the options
1180 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
1183 server_parse_url (struct HTTP_Server_Plugin *plugin,
1185 struct GNUNET_PeerIdentity *target,
1189 regmatch_t matches[4];
1190 const char *tag_start;
1191 const char *target_start;
1195 unsigned long int rc;
1199 ("^.*/([0-9A-Z]+);([0-9]+)(,[0-9]+)?$")
1204 return GNUNET_SYSERR;
1207 if (regexec(&plugin->url_regex, url, 4, matches, 0))
1209 LOG (GNUNET_ERROR_TYPE_DEBUG,
1210 "URL `%s' did not match regex\n", url);
1211 return GNUNET_SYSERR;
1214 target_start = &url[matches[1].rm_so];
1215 tag_start = &url[matches[2].rm_so];
1218 rc = strtoul (tag_start, &tag_end, 10);
1219 if (&url[matches[2].rm_eo] != tag_end)
1221 LOG (GNUNET_ERROR_TYPE_DEBUG,
1222 "URL tag did not line up with submatch\n");
1223 return GNUNET_SYSERR;
1227 LOG (GNUNET_ERROR_TYPE_DEBUG,
1228 "URL tag is zero\n");
1229 return GNUNET_SYSERR;
1231 if ((rc == ULONG_MAX) && (ERANGE == errno))
1233 LOG (GNUNET_ERROR_TYPE_DEBUG,
1234 "URL tag > ULONG_MAX\n");
1235 return GNUNET_SYSERR;
1237 if (rc > UINT32_MAX)
1239 LOG (GNUNET_ERROR_TYPE_DEBUG,
1240 "URL tag > UINT32_MAX\n");
1241 return GNUNET_SYSERR;
1243 (*tag) = (uint32_t)rc;
1244 LOG (GNUNET_ERROR_TYPE_DEBUG,
1245 "Found tag `%u' in url\n",
1248 /* convert peer id */
1249 hash_length = matches[1].rm_eo - matches[1].rm_so;
1250 if (hash_length != plugin->peer_id_length)
1252 LOG (GNUNET_ERROR_TYPE_DEBUG,
1253 "URL target is %u bytes, expecting %u\n",
1254 hash_length, plugin->peer_id_length);
1255 return GNUNET_SYSERR;
1258 GNUNET_CRYPTO_eddsa_public_key_from_string (target_start,
1260 &target->public_key))
1262 LOG (GNUNET_ERROR_TYPE_DEBUG,
1263 "URL target conversion failed\n");
1264 return GNUNET_SYSERR;
1266 LOG (GNUNET_ERROR_TYPE_DEBUG,
1267 "Found target `%s' in URL\n",
1268 GNUNET_i2s_full (target));
1270 /* convert options */
1271 if (-1 == matches[3].rm_so)
1277 rc = strtoul (&url[matches[3].rm_so + 1], &options_end, 10);
1278 if (&url[matches[3].rm_eo] != options_end)
1280 LOG (GNUNET_ERROR_TYPE_DEBUG,
1281 "URL options did not line up with submatch\n");
1282 return GNUNET_SYSERR;
1284 if ((rc == ULONG_MAX) && (ERANGE == errno))
1286 LOG (GNUNET_ERROR_TYPE_DEBUG,
1287 "URL options > ULONG_MAX\n");
1288 return GNUNET_SYSERR;
1290 if (rc > UINT32_MAX)
1292 LOG (GNUNET_ERROR_TYPE_DEBUG,
1293 "URL options > UINT32_MAX\n");
1294 return GNUNET_SYSERR;
1296 (*options) = (uint32_t) rc;
1297 LOG (GNUNET_ERROR_TYPE_DEBUG,
1298 "Found options `%u' in url\n",
1306 * Closure for #session_tag_it().
1308 struct SessionTagContext
1311 * Set to session matching the tag.
1313 struct Session *res;
1316 * Tag we are looking for.
1323 * Find a session with a matching tag.
1325 * @param cls the `struct SessionTagContext *`
1326 * @param key peer identity (unused)
1327 * @param value the `struct Session *`
1328 * @return #GNUNET_NO if we found the session, #GNUNET_OK if not
1331 session_tag_it (void *cls,
1332 const struct GNUNET_PeerIdentity *key,
1335 struct SessionTagContext *stc = cls;
1336 struct Session *s = value;
1338 if (s->tag == stc->tag)
1348 * Lookup a mhd connection and create one if none is found
1350 * @param plugin the plugin handle
1351 * @param mhd_connection the incoming mhd_connection
1352 * @param url incoming requested URL
1353 * @param method PUT or GET
1354 * @return the server connecetion
1356 static struct ServerRequest *
1357 server_lookup_connection (struct HTTP_Server_Plugin *plugin,
1358 struct MHD_Connection *mhd_connection,
1362 struct Session *s = NULL;
1363 struct ServerRequest *sc = NULL;
1364 const union MHD_ConnectionInfo *conn_info;
1365 struct HttpAddress *addr;
1366 struct GNUNET_PeerIdentity target;
1368 struct SessionTagContext stc;
1370 int direction = GNUNET_SYSERR;
1372 enum GNUNET_ATS_Network_Type scope;
1374 conn_info = MHD_get_connection_info (mhd_connection,
1375 MHD_CONNECTION_INFO_CLIENT_ADDRESS);
1376 if ((conn_info->client_addr->sa_family != AF_INET) &&
1377 (conn_info->client_addr->sa_family != AF_INET6))
1379 LOG (GNUNET_ERROR_TYPE_DEBUG,
1380 "New %s request from %s\n",
1384 if (GNUNET_SYSERR ==
1385 server_parse_url (plugin, url, &target, &stc.tag, &options))
1387 LOG (GNUNET_ERROR_TYPE_DEBUG,
1388 "Invalid url %s\n", url);
1391 if (0 == strcmp (MHD_HTTP_METHOD_PUT, method))
1392 direction = _RECEIVE;
1393 else if (0 == strcmp (MHD_HTTP_METHOD_GET, method))
1397 LOG (GNUNET_ERROR_TYPE_DEBUG,
1398 "Invalid method %s for request from %s\n",
1403 plugin->cur_request++;
1404 LOG (GNUNET_ERROR_TYPE_DEBUG,
1405 "New %s request from %s with tag %u (%u of %u)\n",
1407 GNUNET_i2s (&target),
1409 plugin->cur_request, plugin->max_request);
1410 /* find existing session */
1412 GNUNET_CONTAINER_multipeermap_get_multiple (plugin->sessions,
1416 if (NULL == (s = stc.res))
1418 /* create new session */
1420 switch (conn_info->client_addr->sa_family)
1423 addr = http_common_address_from_socket (plugin->protocol,
1424 conn_info->client_addr,
1425 sizeof (struct sockaddr_in));
1426 addr_len = http_common_address_get_size (addr);
1427 scope = plugin->env->get_address_type (plugin->env->cls,
1428 conn_info->client_addr,
1429 sizeof (struct sockaddr_in));
1432 addr = http_common_address_from_socket (plugin->protocol,
1433 conn_info->client_addr,
1434 sizeof (struct sockaddr_in6));
1435 addr_len = http_common_address_get_size (addr);
1436 scope = plugin->env->get_address_type (plugin->env->cls,
1437 conn_info->client_addr,
1438 sizeof (struct sockaddr_in6));
1441 /* external host name */
1444 s = GNUNET_new (struct Session);
1448 s->address = GNUNET_HELLO_address_allocate (&s->target,
1452 GNUNET_HELLO_ADDRESS_INFO_INBOUND);
1453 s->next_receive = GNUNET_TIME_UNIT_ZERO_ABS;
1455 s->timeout = GNUNET_TIME_relative_to_absolute (HTTP_SERVER_SESSION_TIMEOUT);
1456 s->timeout_task = GNUNET_SCHEDULER_add_delayed (HTTP_SERVER_SESSION_TIMEOUT,
1457 &server_session_timeout,
1459 (void) GNUNET_CONTAINER_multipeermap_put (plugin->sessions,
1462 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
1463 notify_session_monitor (plugin,
1465 GNUNET_TRANSPORT_SS_INIT);
1466 notify_session_monitor (plugin,
1468 GNUNET_TRANSPORT_SS_HANDSHAKE);
1469 LOG (GNUNET_ERROR_TYPE_DEBUG,
1470 "Creating new session %p for peer `%s' connecting from `%s'\n",
1471 s, GNUNET_i2s (&target),
1472 http_common_plugin_address_to_string (plugin->protocol,
1475 GNUNET_free_non_null (addr);
1478 if ( (_RECEIVE == direction) &&
1479 (NULL != s->server_recv) )
1481 LOG (GNUNET_ERROR_TYPE_DEBUG,
1482 "Duplicate PUT request from `%s' tag %u, dismissing new request\n",
1483 GNUNET_i2s (&target),
1487 if ((_SEND == direction) && (NULL != s->server_send))
1489 LOG (GNUNET_ERROR_TYPE_DEBUG,
1490 "Duplicate GET request from `%s' tag %u, dismissing new request\n",
1491 GNUNET_i2s (&target),
1495 sc = GNUNET_new (struct ServerRequest);
1496 if (conn_info->client_addr->sa_family == AF_INET)
1497 sc->mhd_daemon = plugin->server_v4;
1498 if (conn_info->client_addr->sa_family == AF_INET6)
1499 sc->mhd_daemon = plugin->server_v6;
1500 sc->mhd_conn = mhd_connection;
1501 sc->direction = direction;
1502 sc->connected = GNUNET_NO;
1504 sc->options = options;
1505 if (direction == _SEND)
1507 s->server_send = sc;
1509 if (direction == _RECEIVE)
1511 s->server_recv = sc;
1514 if ((GNUNET_NO == s->known_to_service) &&
1515 (NULL != s->server_send) &&
1516 (NULL != s->server_recv) )
1518 s->known_to_service = GNUNET_YES;
1519 notify_session_monitor (plugin,
1521 GNUNET_TRANSPORT_SS_UP);
1522 plugin->env->session_start (plugin->env->cls,
1528 to = (HTTP_SERVER_SESSION_TIMEOUT.rel_value_us / 1000LL / 1000LL);
1529 server_mhd_connection_timeout (plugin, s, to);
1535 * Callback called by MHD when it needs data to send
1537 * @param cls current session
1538 * @param pos position in buffer
1539 * @param buf the buffer to write data to
1540 * @param max max number of bytes available in @a buf
1541 * @return bytes written to @a buf
1544 server_send_callback (void *cls,
1549 struct ServerRequest *sc = cls;
1550 struct Session *s = sc->session;
1551 ssize_t bytes_read = 0;
1552 struct HTTP_Message *msg;
1557 /* session is disconnecting */
1561 sc = s->server_send;
1568 bytes_read = GNUNET_MIN (msg->size - msg->pos,
1570 memcpy (buf, &msg->buf[msg->pos], bytes_read);
1571 msg->pos += bytes_read;
1573 /* removing message */
1574 if (msg->pos == msg->size)
1576 GNUNET_CONTAINER_DLL_remove (s->msg_head,
1579 if (NULL != msg->transmit_cont)
1580 msg->transmit_cont (msg->transmit_cont_cls, &s->target, GNUNET_OK,
1581 msg->size, msg->size + msg->overhead);
1582 GNUNET_assert (s->msgs_in_queue > 0);
1584 GNUNET_assert (s->bytes_in_queue >= msg->size);
1585 s->bytes_in_queue -= msg->size;
1587 notify_session_monitor (s->plugin,
1589 GNUNET_TRANSPORT_SS_UPDATE);
1594 sc->connected = GNUNET_YES;
1595 LOG (GNUNET_ERROR_TYPE_DEBUG,
1596 "Sent %u bytes to peer `%s' with session %p \n",
1598 GNUNET_i2s (&s->target),
1600 GNUNET_asprintf (&stat_txt,
1601 "# bytes currently in %s_server buffers",
1602 s->plugin->protocol);
1603 GNUNET_STATISTICS_update (s->plugin->env->stats,
1607 GNUNET_free (stat_txt);
1608 GNUNET_asprintf (&stat_txt,
1609 "# bytes transmitted via %s_server",
1610 s->plugin->protocol);
1611 GNUNET_STATISTICS_update (s->plugin->env->stats,
1612 stat_txt, bytes_read, GNUNET_NO);
1613 GNUNET_free (stat_txt);
1615 else if ((sc->options & OPTION_LONG_POLL) && sc->connected)
1617 LOG (GNUNET_ERROR_TYPE_DEBUG,
1618 "Completing GET response to peer `%s' with session %p\n",
1619 GNUNET_i2s (&s->target),
1621 return MHD_CONTENT_READER_END_OF_STREAM;
1628 * Callback called by MessageStreamTokenizer when a message has arrived
1630 * @param cls current session as closure
1631 * @param client client
1632 * @param message the message to be forwarded to transport service
1633 * @return #GNUNET_OK
1636 server_receive_mst_cb (void *cls,
1638 const struct GNUNET_MessageHeader *message)
1640 struct Session *s = cls;
1641 struct HTTP_Server_Plugin *plugin = s->plugin;
1642 struct GNUNET_TIME_Relative delay;
1645 if (GNUNET_NO == s->known_to_service)
1647 s->known_to_service = GNUNET_YES;
1648 plugin->env->session_start (plugin->env->cls,
1652 notify_session_monitor (plugin,
1654 GNUNET_TRANSPORT_SS_UP);
1656 delay = plugin->env->receive (plugin->env->cls,
1660 GNUNET_asprintf (&stat_txt,
1661 "# bytes received via %s_server",
1663 GNUNET_STATISTICS_update (plugin->env->stats,
1664 stat_txt, ntohs (message->size), GNUNET_NO);
1665 GNUNET_free (stat_txt);
1666 s->next_receive = GNUNET_TIME_relative_to_absolute (delay);
1667 if (delay.rel_value_us > 0)
1669 LOG (GNUNET_ERROR_TYPE_DEBUG,
1670 "Peer `%s' address `%s' next read delayed for %s\n",
1671 GNUNET_i2s (&s->target),
1672 http_common_plugin_address_to_string (plugin->protocol,
1673 s->address->address,
1674 s->address->address_length),
1675 GNUNET_STRINGS_relative_time_to_string (delay,
1678 server_reschedule_session_timeout (s);
1684 * Add headers to a request indicating that we allow Cross-Origin Resource
1687 * @param response response object to modify
1690 add_cors_headers(struct MHD_Response *response)
1692 MHD_add_response_header (response,
1693 "Access-Control-Allow-Origin",
1695 MHD_add_response_header (response,
1696 "Access-Control-Allow-Methods",
1697 "GET, PUT, OPTIONS");
1698 MHD_add_response_header (response,
1699 "Access-Control-Max-Age",
1705 * MHD callback for a new incoming connection
1707 * @param cls the plugin handle
1708 * @param mhd_connection the mhd connection
1709 * @param url the requested URL
1710 * @param method GET or PUT
1711 * @param version HTTP version
1712 * @param upload_data upload data
1713 * @param upload_data_size size of @a upload_data
1714 * @param httpSessionCache the session cache to remember the connection
1715 * @return MHD_YES if connection is accepted, MHD_NO on reject
1718 server_access_cb (void *cls,
1719 struct MHD_Connection *mhd_connection,
1722 const char *version,
1723 const char *upload_data,
1724 size_t *upload_data_size,
1725 void **httpSessionCache)
1727 struct HTTP_Server_Plugin *plugin = cls;
1728 struct ServerRequest *sc = *httpSessionCache;
1730 struct MHD_Response *response;
1733 LOG (GNUNET_ERROR_TYPE_DEBUG,
1734 _("Access from connection %p (%u of %u) for `%s' `%s' url `%s' with upload data size %u\n"),
1736 plugin->cur_request,
1737 plugin->max_request,
1741 (*upload_data_size));
1744 /* CORS pre-flight request */
1745 if (0 == strcmp (MHD_HTTP_METHOD_OPTIONS, method))
1747 response = MHD_create_response_from_buffer (0, NULL,
1748 MHD_RESPMEM_PERSISTENT);
1749 add_cors_headers(response);
1750 res = MHD_queue_response (mhd_connection, MHD_HTTP_OK, response);
1751 MHD_destroy_response (response);
1754 /* new connection */
1755 sc = server_lookup_connection (plugin, mhd_connection, url, method);
1758 /* attach to new / existing session */
1759 (*httpSessionCache) = sc;
1763 /* existing session already has matching connection, refuse */
1764 response = MHD_create_response_from_buffer (strlen (HTTP_ERROR_RESPONSE),
1765 HTTP_ERROR_RESPONSE,
1766 MHD_RESPMEM_PERSISTENT);
1767 MHD_add_response_header (response,
1768 MHD_HTTP_HEADER_CONTENT_TYPE,
1770 add_cors_headers(response);
1771 res = MHD_queue_response (mhd_connection, MHD_HTTP_NOT_FOUND, response);
1772 MHD_destroy_response (response);
1776 /* 'old' connection */
1777 if (NULL == (s = sc->session))
1779 /* Session was already disconnected;
1780 sent HTTP/1.1: 200 OK as response */
1781 response = MHD_create_response_from_buffer (strlen ("Thank you!"),
1783 MHD_RESPMEM_PERSISTENT);
1784 add_cors_headers(response);
1785 MHD_queue_response (mhd_connection, MHD_HTTP_OK, response);
1786 MHD_destroy_response (response);
1790 if (sc->direction == _SEND)
1792 response = MHD_create_response_from_callback (MHD_SIZE_UNKNOWN, 32 * 1024,
1793 &server_send_callback, sc, NULL);
1794 add_cors_headers(response);
1795 MHD_queue_response (mhd_connection, MHD_HTTP_OK, response);
1796 MHD_destroy_response (response);
1799 if (sc->direction == _RECEIVE)
1801 if ((*upload_data_size == 0) && (sc->connected == GNUNET_NO))
1803 /* (*upload_data_size == 0) first callback when header are passed */
1804 LOG (GNUNET_ERROR_TYPE_DEBUG,
1805 "Session %p / Connection %p: Peer `%s' PUT on address `%s' connected\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_YES;
1814 else if ((*upload_data_size == 0) && (sc->connected == GNUNET_YES))
1816 /* (*upload_data_size == 0) when upload is complete */
1817 LOG (GNUNET_ERROR_TYPE_DEBUG,
1818 "Session %p / Connection %p: Peer `%s' PUT on address `%s' finished upload\n",
1820 GNUNET_i2s (&s->target),
1821 http_common_plugin_address_to_string (plugin->protocol,
1822 s->address->address,
1823 s->address->address_length));
1824 sc->connected = GNUNET_NO;
1825 /* Sent HTTP/1.1: 200 OK as PUT Response\ */
1826 response = MHD_create_response_from_buffer (strlen ("Thank you!"),
1828 MHD_RESPMEM_PERSISTENT);
1829 add_cors_headers(response);
1830 MHD_queue_response (mhd_connection, MHD_HTTP_OK, response);
1831 MHD_destroy_response (response);
1834 else if ((*upload_data_size > 0) && (sc->connected == GNUNET_YES))
1836 struct GNUNET_TIME_Relative delay;
1838 /* (*upload_data_size > 0) for every segment received */
1839 LOG (GNUNET_ERROR_TYPE_DEBUG,
1840 "Session %p / Connection %p: Peer `%s' PUT on address `%s' received %u bytes\n",
1842 GNUNET_i2s (&s->target),
1843 http_common_plugin_address_to_string (plugin->protocol,
1844 s->address->address,
1845 s->address->address_length),
1847 delay = GNUNET_TIME_absolute_get_remaining (s->next_receive);
1848 if (0 == delay.rel_value_us)
1850 LOG (GNUNET_ERROR_TYPE_DEBUG,
1851 "PUT with %u bytes forwarded to MST\n",
1853 if (s->msg_tk == NULL)
1855 s->msg_tk = GNUNET_SERVER_mst_create (&server_receive_mst_cb, s);
1857 GNUNET_SERVER_mst_receive (s->msg_tk, s, upload_data, *upload_data_size,
1858 GNUNET_NO, GNUNET_NO);
1859 server_mhd_connection_timeout (plugin, s,
1860 GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT.rel_value_us / 1000LL
1862 (*upload_data_size) = 0;
1866 /* delay processing */
1867 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1868 "Session %p / Connection %p: no inbound bandwidth available! Next read was delayed by %s\n",
1869 s, sc, GNUNET_STRINGS_relative_time_to_string (delay, GNUNET_YES));
1870 GNUNET_assert(s->server_recv->mhd_conn == mhd_connection);
1871 MHD_suspend_connection (s->server_recv->mhd_conn);
1872 if (NULL == s->recv_wakeup_task)
1873 s->recv_wakeup_task = GNUNET_SCHEDULER_add_delayed (delay,
1874 &server_wake_up, s);
1889 * Callback from MHD when a connection disconnects
1891 * @param cls closure with the `struct HTTP_Server_Plugin *`
1892 * @param connection the disconnected MHD connection
1893 * @param httpSessionCache the pointer to distinguish
1896 server_disconnect_cb (void *cls,
1897 struct MHD_Connection *connection,
1898 void **httpSessionCache)
1900 struct HTTP_Server_Plugin *plugin = cls;
1901 struct ServerRequest *sc = *httpSessionCache;
1903 LOG (GNUNET_ERROR_TYPE_DEBUG,
1904 "Disconnect for connection %p\n",
1908 /* CORS pre-flight request finished */
1912 if (NULL != sc->session)
1914 if (sc->direction == _SEND)
1916 LOG (GNUNET_ERROR_TYPE_DEBUG,
1917 "Peer `%s' connection %p, GET on address `%s' disconnected\n",
1918 GNUNET_i2s (&sc->session->target),
1919 sc->session->server_send,
1920 http_common_plugin_address_to_string (plugin->protocol,
1921 sc->session->address->address,
1922 sc->session->address->address_length));
1924 sc->session->server_send = NULL;
1926 else if (sc->direction == _RECEIVE)
1928 LOG (GNUNET_ERROR_TYPE_DEBUG,
1929 "Peer `%s' connection %p PUT on address `%s' disconnected\n",
1930 GNUNET_i2s (&sc->session->target),
1931 sc->session->server_recv,
1932 http_common_plugin_address_to_string (plugin->protocol,
1933 sc->session->address->address,
1934 sc->session->address->address_length));
1935 sc->session->server_recv = NULL;
1936 if (NULL != sc->session->msg_tk)
1938 GNUNET_SERVER_mst_destroy (sc->session->msg_tk);
1939 sc->session->msg_tk = NULL;
1944 plugin->cur_request--;
1949 * Check if incoming connection is accepted.
1951 * @param cls plugin as closure
1952 * @param addr address of incoming connection
1953 * @param addr_len number of bytes in @a addr
1954 * @return MHD_YES if connection is accepted, MHD_NO if connection is rejected
1957 server_accept_cb (void *cls,
1958 const struct sockaddr *addr,
1961 struct HTTP_Server_Plugin *plugin = cls;
1963 if (plugin->cur_request <= plugin->max_request)
1965 LOG (GNUNET_ERROR_TYPE_DEBUG,
1966 _("Accepting connection (%u of %u) from `%s'\n"),
1967 plugin->cur_request, plugin->max_request,
1968 GNUNET_a2s (addr, addr_len));
1973 LOG (GNUNET_ERROR_TYPE_WARNING,
1974 _("Server reached maximum number connections (%u), rejecting new connection\n"),
1975 plugin->max_request);
1982 * Log function called by MHD.
1985 * @param fmt format string
1986 * @param ap arguments for the format string (va_start() and va_end()
1987 * will be called by MHD)
1990 server_log (void *arg,
2000 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2008 * Load ssl certificate from file
2010 * @param file filename
2011 * @return content of the file
2014 server_load_file (const char *file)
2016 struct GNUNET_DISK_FileHandle *gn_file;
2020 if (GNUNET_OK != GNUNET_DISK_file_size (file,
2021 &fsize, GNUNET_NO, GNUNET_YES))
2023 text = GNUNET_malloc (fsize + 1);
2025 GNUNET_DISK_file_open (file, GNUNET_DISK_OPEN_READ,
2026 GNUNET_DISK_PERM_USER_READ);
2027 if (NULL == gn_file)
2032 if (GNUNET_SYSERR == GNUNET_DISK_file_read (gn_file, text, fsize))
2035 GNUNET_DISK_file_close (gn_file);
2039 GNUNET_DISK_file_close (gn_file);
2047 * Load ssl certificate
2049 * @param plugin the plugin
2050 * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
2053 server_load_certificate (struct HTTP_Server_Plugin *plugin)
2055 int res = GNUNET_OK;
2061 GNUNET_CONFIGURATION_get_value_filename (plugin->env->cfg,
2063 "KEY_FILE", &key_file))
2065 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
2066 plugin->name, "CERT_FILE");
2067 return GNUNET_SYSERR;
2070 GNUNET_CONFIGURATION_get_value_filename (plugin->env->cfg,
2072 "CERT_FILE", &cert_file))
2074 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
2075 plugin->name, "CERT_FILE");
2076 GNUNET_free (key_file);
2077 return GNUNET_SYSERR;
2079 /* Get crypto init string from config. If not present, use
2082 GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg,
2085 &plugin->crypto_init))
2086 LOG (GNUNET_ERROR_TYPE_DEBUG,
2087 "Using crypto init string `%s'\n",
2088 plugin->crypto_init);
2090 LOG (GNUNET_ERROR_TYPE_DEBUG,
2091 "Using default crypto init string \n");
2093 /* read key & certificates from file */
2094 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2095 "Trying to loading TLS certificate from key-file `%s' cert-file`%s'\n",
2096 key_file, cert_file);
2098 plugin->key = server_load_file (key_file);
2099 plugin->cert = server_load_file (cert_file);
2101 if ((plugin->key == NULL) || (plugin->cert == NULL))
2103 struct GNUNET_OS_Process *cert_creation;
2105 GNUNET_free_non_null (plugin->key);
2107 GNUNET_free_non_null (plugin->cert);
2108 plugin->cert = NULL;
2110 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2111 "No usable TLS certificate found, creating certificate\n");
2114 GNUNET_OS_start_process (GNUNET_NO, GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
2116 "gnunet-transport-certificate-creation",
2117 "gnunet-transport-certificate-creation",
2121 if (NULL == cert_creation)
2123 LOG (GNUNET_ERROR_TYPE_ERROR,
2124 _("Could not create a new TLS certificate, program `gnunet-transport-certificate-creation' could not be started!\n"));
2125 GNUNET_free (key_file);
2126 GNUNET_free (cert_file);
2128 GNUNET_free_non_null (plugin->key);
2130 GNUNET_free_non_null (plugin->cert);
2131 plugin->cert = NULL;
2132 GNUNET_free_non_null (plugin->crypto_init);
2133 plugin->crypto_init = NULL;
2135 return GNUNET_SYSERR;
2137 GNUNET_assert (GNUNET_OK == GNUNET_OS_process_wait (cert_creation));
2138 GNUNET_OS_process_destroy (cert_creation);
2140 plugin->key = server_load_file (key_file);
2141 plugin->cert = server_load_file (cert_file);
2144 if ((plugin->key == NULL) || (plugin->cert == NULL))
2146 LOG (GNUNET_ERROR_TYPE_ERROR,
2147 _("No usable TLS certificate found and creating one at `%s/%s' failed!\n"),
2148 key_file, cert_file);
2149 GNUNET_free (key_file);
2150 GNUNET_free (cert_file);
2152 GNUNET_free_non_null (plugin->key);
2154 GNUNET_free_non_null (plugin->cert);
2155 plugin->cert = NULL;
2156 GNUNET_free_non_null (plugin->crypto_init);
2157 plugin->crypto_init = NULL;
2159 return GNUNET_SYSERR;
2161 GNUNET_free (key_file);
2162 GNUNET_free (cert_file);
2163 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2164 "TLS certificate loaded\n");
2171 * Invoke `MHD_start_daemon` with the various options we need to
2172 * setup the HTTP server with the given listen address.
2174 * @param plugin our plugin
2175 * @param addr listen address to use
2176 * @param v6 MHD_NO_FLAG or MHD_USE_IPv6, depending on context
2177 * @return NULL on error
2179 static struct MHD_Daemon *
2180 run_mhd_start_daemon (struct HTTP_Server_Plugin *plugin,
2181 const struct sockaddr_in *addr,
2184 struct MHD_Daemon *server;
2185 unsigned int timeout;
2187 #if MHD_VERSION >= 0x00090E00
2188 timeout = HTTP_SERVER_NOT_VALIDATED_TIMEOUT.rel_value_us / 1000LL / 1000LL;
2189 LOG (GNUNET_ERROR_TYPE_DEBUG,
2190 "MHD can set timeout per connection! Default time out %u sec.\n",
2193 timeout = HTTP_SERVER_SESSION_TIMEOUT.rel_value_us / 1000LL / 1000LL;
2194 LOG (GNUNET_ERROR_TYPE_WARNING,
2195 "MHD cannot set timeout per connection! Default time out %u sec.\n",
2198 server = MHD_start_daemon (
2205 MHD_USE_SUSPEND_RESUME |
2208 &server_accept_cb, plugin,
2209 &server_access_cb, plugin,
2210 MHD_OPTION_SOCK_ADDR,
2212 MHD_OPTION_CONNECTION_LIMIT,
2213 (unsigned int) plugin->max_request,
2215 MHD_OPTION_HTTPS_PRIORITIES,
2216 plugin->crypto_init,
2217 MHD_OPTION_HTTPS_MEM_KEY,
2219 MHD_OPTION_HTTPS_MEM_CERT,
2222 MHD_OPTION_CONNECTION_TIMEOUT,
2224 MHD_OPTION_CONNECTION_MEMORY_LIMIT,
2226 GNUNET_SERVER_MAX_MESSAGE_SIZE),
2227 MHD_OPTION_NOTIFY_COMPLETED,
2228 &server_disconnect_cb, plugin,
2229 MHD_OPTION_EXTERNAL_LOGGER,
2233 if ( (NULL != server) &&
2234 (0 != (plugin->options & HTTP_OPTIONS_TCP_STEALTH)) )
2236 const union MHD_DaemonInfo *di;
2238 di = MHD_get_daemon_info (server,
2239 MHD_DAEMON_INFO_LISTEN_FD,
2241 if ( (0 != setsockopt ((int) di->listen_fd,
2244 plugin->env->my_identity,
2245 sizeof (struct GNUNET_PeerIdentity))) )
2247 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2248 _("TCP_STEALTH not supported on this platform.\n"));
2249 MHD_stop_daemon (server);
2259 * Start the HTTP server
2261 * @param plugin the plugin handle
2262 * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
2265 server_start (struct HTTP_Server_Plugin *plugin)
2269 GNUNET_assert (NULL != plugin);
2271 if (GNUNET_SYSERR == server_load_certificate (plugin))
2273 LOG (GNUNET_ERROR_TYPE_ERROR,
2274 _("Could not load or create server certificate! Loading plugin failed!\n"));
2275 return GNUNET_SYSERR;
2281 plugin->server_v4 = NULL;
2282 if (GNUNET_YES == plugin->use_ipv4)
2285 = run_mhd_start_daemon (plugin,
2286 (const struct sockaddr_in *) plugin->server_addr_v4,
2289 if (NULL == plugin->server_v4)
2291 LOG (GNUNET_ERROR_TYPE_ERROR,
2292 "Failed to start %s IPv4 server component on port %u\n",
2297 server_reschedule (plugin,
2303 plugin->server_v6 = NULL;
2304 if (GNUNET_YES == plugin->use_ipv6)
2307 = run_mhd_start_daemon (plugin,
2308 (const struct sockaddr_in *) plugin->server_addr_v6,
2310 if (NULL == plugin->server_v6)
2312 LOG (GNUNET_ERROR_TYPE_ERROR,
2313 "Failed to start %s IPv6 server component on port %u\n",
2319 server_reschedule (plugin,
2325 if ( (NULL == plugin->server_v6) &&
2326 (NULL == plugin->server_v4) )
2328 LOG (GNUNET_ERROR_TYPE_ERROR,
2329 "%s %s server component started on port %u\n",
2333 return GNUNET_SYSERR;
2335 if ((NULL != plugin->server_v6) &&
2336 (NULL != plugin->server_v4))
2337 msg = "IPv4 and IPv6";
2338 else if (NULL != plugin->server_v6)
2340 else if (NULL != plugin->server_v4)
2342 LOG (GNUNET_ERROR_TYPE_DEBUG,
2343 "%s %s server component started on port %u\n",
2352 * Add an address to the server's set of addresses and notify transport
2354 * @param cls the plugin handle
2355 * @param add_remove #GNUNET_YES on add, #GNUNET_NO on remove
2356 * @param addr the address
2357 * @param addrlen address length
2360 server_add_address (void *cls,
2362 const struct sockaddr *addr,
2365 struct HTTP_Server_Plugin *plugin = cls;
2366 struct GNUNET_HELLO_Address *address;
2367 struct HttpAddressWrapper *w = NULL;
2369 w = GNUNET_new (struct HttpAddressWrapper);
2370 w->address = http_common_address_from_socket (plugin->protocol,
2373 if (NULL == w->address)
2378 w->addrlen = http_common_address_get_size (w->address);
2380 GNUNET_CONTAINER_DLL_insert (plugin->addr_head,
2383 LOG (GNUNET_ERROR_TYPE_DEBUG,
2384 "Notifying transport to add address `%s'\n",
2385 http_common_plugin_address_to_string (plugin->protocol,
2388 /* modify our published address list */
2390 address = GNUNET_HELLO_address_allocate (plugin->env->my_identity,
2391 "https_client", w->address, w->addrlen, GNUNET_HELLO_ADDRESS_INFO_NONE);
2393 address = GNUNET_HELLO_address_allocate (plugin->env->my_identity,
2394 "http_client", w->address, w->addrlen, GNUNET_HELLO_ADDRESS_INFO_NONE);
2397 plugin->env->notify_address (plugin->env->cls,
2400 GNUNET_HELLO_address_free (address);
2405 * Remove an address from the server's set of addresses and notify transport
2407 * @param cls the plugin handle
2408 * @param add_remove #GNUNET_YES on add, #GNUNET_NO on remove
2409 * @param addr the address
2410 * @param addrlen address length
2413 server_remove_address (void *cls,
2415 const struct sockaddr *addr,
2418 struct HTTP_Server_Plugin *plugin = cls;
2419 struct GNUNET_HELLO_Address *address;
2420 struct HttpAddressWrapper *w = plugin->addr_head;
2424 saddr = http_common_address_from_socket (plugin->protocol,
2429 saddr_len = http_common_address_get_size (saddr);
2434 http_common_cmp_addresses (w->address,
2441 GNUNET_free (saddr);
2446 LOG (GNUNET_ERROR_TYPE_DEBUG,
2447 "Notifying transport to remove address `%s'\n",
2448 http_common_plugin_address_to_string (plugin->protocol,
2451 GNUNET_CONTAINER_DLL_remove (plugin->addr_head,
2454 /* modify our published address list */
2456 address = GNUNET_HELLO_address_allocate (plugin->env->my_identity,
2457 "https_client", w->address, w->addrlen, GNUNET_HELLO_ADDRESS_INFO_NONE);
2459 address = GNUNET_HELLO_address_allocate (plugin->env->my_identity,
2460 "http_client", w->address, w->addrlen, GNUNET_HELLO_ADDRESS_INFO_NONE);
2462 plugin->env->notify_address (plugin->env->cls, add_remove, address);
2463 GNUNET_HELLO_address_free (address);
2464 GNUNET_free (w->address);
2471 * Our external IP address/port mapping has changed.
2473 * @param cls closure, the 'struct LocalAddrList'
2474 * @param add_remove #GNUNET_YES to mean the new public IP address, #GNUNET_NO to mean
2475 * the previous (now invalid) one
2476 * @param addr either the previous or the new public IP address
2477 * @param addrlen actual lenght of the address
2480 server_nat_port_map_callback (void *cls,
2482 const struct sockaddr *addr,
2485 struct HTTP_Server_Plugin *plugin = cls;
2487 LOG (GNUNET_ERROR_TYPE_DEBUG,
2488 "NAT called to %s address `%s'\n",
2489 (add_remove == GNUNET_NO) ? "remove" : "add",
2490 GNUNET_a2s (addr, addrlen));
2492 if (AF_INET == addr->sa_family)
2494 struct sockaddr_in *s4 = (struct sockaddr_in *) addr;
2496 if (GNUNET_NO == plugin->use_ipv4)
2499 if ((NULL != plugin->server_addr_v4) &&
2500 (0 != memcmp (&plugin->server_addr_v4->sin_addr,
2501 &s4->sin_addr, sizeof (struct in_addr))))
2503 LOG (GNUNET_ERROR_TYPE_DEBUG,
2504 "Skipping address `%s' (not bindto address)\n",
2505 GNUNET_a2s (addr, addrlen));
2510 if (AF_INET6 == addr->sa_family)
2512 struct sockaddr_in6 *s6 = (struct sockaddr_in6 *) addr;
2513 if (GNUNET_NO == plugin->use_ipv6)
2516 if ((NULL != plugin->server_addr_v6) &&
2517 (0 != memcmp (&plugin->server_addr_v6->sin6_addr,
2518 &s6->sin6_addr, sizeof (struct in6_addr))))
2520 LOG (GNUNET_ERROR_TYPE_DEBUG,
2521 "Skipping address `%s' (not bindto address)\n",
2522 GNUNET_a2s (addr, addrlen));
2530 server_add_address (cls, add_remove, addr, addrlen);
2533 server_remove_address (cls, add_remove, addr, addrlen);
2540 * Get valid server addresses
2542 * @param plugin the plugin handle
2543 * @param service_name the servicename
2544 * @param cfg configuration handle
2545 * @param addrs addresses
2546 * @param addr_lens address length
2547 * @return number of addresses
2550 server_get_addresses (struct HTTP_Server_Plugin *plugin,
2551 const char *service_name,
2552 const struct GNUNET_CONFIGURATION_Handle *cfg,
2553 struct sockaddr ***addrs,
2554 socklen_t ** addr_lens)
2557 unsigned long long port;
2558 struct addrinfo hints;
2559 struct addrinfo *res;
2560 struct addrinfo *pos;
2561 struct addrinfo *next;
2565 struct sockaddr **saddrs;
2566 socklen_t *saddrlens;
2572 disablev6 = !plugin->use_ipv6;
2575 if (GNUNET_CONFIGURATION_have_value (cfg, service_name, "PORT"))
2577 GNUNET_break (GNUNET_OK ==
2578 GNUNET_CONFIGURATION_get_value_number (cfg, service_name,
2582 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2583 _("Require valid port number for service in configuration!\n"));
2584 return GNUNET_SYSERR;
2589 LOG (GNUNET_ERROR_TYPE_INFO,
2590 "Starting in listen only mode\n");
2591 return -1; /* listen only */
2595 if (GNUNET_CONFIGURATION_have_value (cfg, service_name,
2598 GNUNET_break (GNUNET_OK ==
2599 GNUNET_CONFIGURATION_get_value_string (cfg, service_name,
2600 "BINDTO", &hostname));
2605 if (NULL != hostname)
2607 LOG (GNUNET_ERROR_TYPE_DEBUG,
2608 "Resolving `%s' since that is where `%s' will bind to.\n",
2609 hostname, service_name);
2610 memset (&hints, 0, sizeof (struct addrinfo));
2612 hints.ai_family = AF_INET;
2613 if ((0 != (ret = getaddrinfo (hostname, NULL, &hints, &res))) ||
2616 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2617 _("Failed to resolve `%s': %s\n"),
2619 gai_strerror (ret));
2620 GNUNET_free (hostname);
2621 return GNUNET_SYSERR;
2625 while (NULL != (pos = next))
2627 next = pos->ai_next;
2628 if ((disablev6) && (pos->ai_family == AF_INET6))
2634 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2635 _("Failed to find %saddress for `%s'.\n"),
2636 disablev6 ? "IPv4 " : "", hostname);
2638 GNUNET_free (hostname);
2639 return GNUNET_SYSERR;
2642 saddrs = GNUNET_malloc ((resi + 1) * sizeof (struct sockaddr *));
2643 saddrlens = GNUNET_malloc ((resi + 1) * sizeof (socklen_t));
2646 while (NULL != (pos = next))
2648 next = pos->ai_next;
2649 if ((disablev6) && (pos->ai_family == AF_INET6))
2651 if ((pos->ai_protocol != IPPROTO_TCP) && (0 != pos->ai_protocol))
2652 continue; /* not TCP */
2653 if ((pos->ai_socktype != SOCK_STREAM) && (0 != pos->ai_socktype))
2654 continue; /* huh? */
2655 LOG (GNUNET_ERROR_TYPE_DEBUG,
2656 "Service will bind to `%s'\n",
2657 GNUNET_a2s (pos->ai_addr,
2659 if (pos->ai_family == AF_INET)
2661 GNUNET_assert (pos->ai_addrlen == sizeof (struct sockaddr_in));
2662 saddrlens[i] = pos->ai_addrlen;
2663 saddrs[i] = GNUNET_malloc (saddrlens[i]);
2664 memcpy (saddrs[i], pos->ai_addr, saddrlens[i]);
2665 ((struct sockaddr_in *) saddrs[i])->sin_port = htons (port);
2669 GNUNET_assert (pos->ai_family == AF_INET6);
2670 GNUNET_assert (pos->ai_addrlen == sizeof (struct sockaddr_in6));
2671 saddrlens[i] = pos->ai_addrlen;
2672 saddrs[i] = GNUNET_malloc (saddrlens[i]);
2673 memcpy (saddrs[i], pos->ai_addr, saddrlens[i]);
2674 ((struct sockaddr_in6 *) saddrs[i])->sin6_port = htons (port);
2678 GNUNET_free (hostname);
2684 /* will bind against everything, just set port */
2690 saddrs = GNUNET_malloc ((resi + 1) * sizeof (struct sockaddr *));
2691 saddrlens = GNUNET_malloc ((resi + 1) * sizeof (socklen_t));
2693 saddrlens[i] = sizeof (struct sockaddr_in);
2694 saddrs[i] = GNUNET_malloc (saddrlens[i]);
2695 #if HAVE_SOCKADDR_IN_SIN_LEN
2696 ((struct sockaddr_in *) saddrs[i])->sin_len = saddrlens[i];
2698 ((struct sockaddr_in *) saddrs[i])->sin_family = AF_INET;
2699 ((struct sockaddr_in *) saddrs[i])->sin_port = htons (port);
2705 saddrs = GNUNET_malloc ((resi + 1) * sizeof (struct sockaddr *));
2706 saddrlens = GNUNET_malloc ((resi + 1) * sizeof (socklen_t));
2708 saddrlens[i] = sizeof (struct sockaddr_in6);
2709 saddrs[i] = GNUNET_malloc (saddrlens[i]);
2710 #if HAVE_SOCKADDR_IN_SIN_LEN
2711 ((struct sockaddr_in6 *) saddrs[i])->sin6_len = saddrlens[0];
2713 ((struct sockaddr_in6 *) saddrs[i])->sin6_family = AF_INET6;
2714 ((struct sockaddr_in6 *) saddrs[i])->sin6_port = htons (port);
2716 saddrlens[i] = sizeof (struct sockaddr_in);
2717 saddrs[i] = GNUNET_malloc (saddrlens[i]);
2718 #if HAVE_SOCKADDR_IN_SIN_LEN
2719 ((struct sockaddr_in *) saddrs[i])->sin_len = saddrlens[1];
2721 ((struct sockaddr_in *) saddrs[i])->sin_family = AF_INET;
2722 ((struct sockaddr_in *) saddrs[i])->sin_port = htons (port);
2726 *addr_lens = saddrlens;
2732 * Ask NAT for addresses
2734 * @param plugin the plugin handle
2737 server_start_report_addresses (struct HTTP_Server_Plugin *plugin)
2739 int res = GNUNET_OK;
2740 struct sockaddr **addrs;
2741 socklen_t *addrlens;
2743 res = server_get_addresses (plugin,
2747 LOG (GNUNET_ERROR_TYPE_DEBUG,
2748 _("Found %u addresses to report to NAT service\n"),
2751 if (GNUNET_SYSERR == res)
2758 GNUNET_NAT_register (plugin->env->cfg,
2762 (const struct sockaddr **) addrs, addrlens,
2763 &server_nat_port_map_callback, NULL, plugin);
2767 GNUNET_assert (NULL != addrs[res]);
2768 GNUNET_free (addrs[res]);
2770 GNUNET_free_non_null (addrs);
2771 GNUNET_free_non_null (addrlens);
2776 * Stop NAT for addresses
2778 * @param plugin the plugin handle
2781 server_stop_report_addresses (struct HTTP_Server_Plugin *plugin)
2783 struct HttpAddressWrapper *w;
2785 /* Stop NAT handle */
2786 if (NULL != plugin->nat)
2788 GNUNET_NAT_unregister (plugin->nat);
2791 /* Clean up addresses */
2792 while (NULL != plugin->addr_head)
2794 w = plugin->addr_head;
2795 GNUNET_CONTAINER_DLL_remove (plugin->addr_head,
2798 GNUNET_free (w->address);
2805 * Check if IPv6 supported on this system
2807 * @param plugin the plugin handle
2808 * @return #GNUNET_YES on success, else #GNUNET_NO
2811 server_check_ipv6_support (struct HTTP_Server_Plugin *plugin)
2813 struct GNUNET_NETWORK_Handle *desc = NULL;
2814 int res = GNUNET_NO;
2816 /* Probe IPv6 support */
2817 desc = GNUNET_NETWORK_socket_create (PF_INET6, SOCK_STREAM, 0);
2820 if ((errno == ENOBUFS) || (errno == ENOMEM) || (errno == ENFILE) ||
2823 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "socket");
2825 LOG (GNUNET_ERROR_TYPE_WARNING,
2826 _("Disabling IPv6 since it is not supported on this system!\n"));
2831 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (desc));
2835 LOG (GNUNET_ERROR_TYPE_DEBUG,
2836 "Testing IPv6 on this system: %s\n",
2837 (res == GNUNET_YES) ? "successful" : "failed");
2843 * Notify server about our external hostname
2846 * @param tc task context (unused)
2849 server_notify_external_hostname (void *cls,
2850 const struct GNUNET_SCHEDULER_TaskContext *tc)
2852 struct HTTP_Server_Plugin *plugin = cls;
2853 struct HttpAddress *ext_addr;
2854 size_t ext_addr_len;
2858 plugin->notify_ext_task = NULL;
2859 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
2862 GNUNET_asprintf(&url,
2865 plugin->external_hostname);
2867 urlen = strlen (url) + 1;
2868 ext_addr = GNUNET_malloc (sizeof (struct HttpAddress) + urlen);
2869 ext_addr->options = htonl (plugin->options);
2870 ext_addr->urlen = htonl (urlen);
2871 ext_addr_len = sizeof (struct HttpAddress) + urlen;
2872 memcpy (&ext_addr[1], url, urlen);
2875 LOG (GNUNET_ERROR_TYPE_DEBUG,
2876 "Notifying transport about external hostname address `%s'\n",
2877 plugin->external_hostname);
2880 if (GNUNET_YES == plugin->verify_external_hostname)
2881 LOG (GNUNET_ERROR_TYPE_INFO,
2882 "Enabling SSL verification for external hostname address `%s'\n",
2883 plugin->external_hostname);
2884 plugin->ext_addr = GNUNET_HELLO_address_allocate (plugin->env->my_identity,
2885 "https_client", ext_addr, ext_addr_len, GNUNET_HELLO_ADDRESS_INFO_NONE );
2886 plugin->env->notify_address (plugin->env->cls, GNUNET_YES, plugin->ext_addr);
2887 GNUNET_free (ext_addr);
2889 plugin->ext_addr = GNUNET_HELLO_address_allocate (plugin->env->my_identity,
2890 "http_client", ext_addr, ext_addr_len, GNUNET_HELLO_ADDRESS_INFO_NONE );
2891 plugin->env->notify_address (plugin->env->cls, GNUNET_YES, plugin->ext_addr);
2892 GNUNET_free (ext_addr);
2898 * Configure the plugin
2900 * @param plugin plugin handle
2901 * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
2904 server_configure_plugin (struct HTTP_Server_Plugin *plugin)
2906 unsigned long long port;
2907 unsigned long long max_connections;
2908 char *bind4_address = NULL;
2909 char *bind6_address = NULL;
2910 char *eh_tmp = NULL;
2911 int external_hostname_use_port;
2914 if (GNUNET_CONFIGURATION_have_value
2915 (plugin->env->cfg, plugin->name, "USE_IPv4"))
2918 GNUNET_CONFIGURATION_get_value_yesno (plugin->env->cfg,
2923 plugin->use_ipv4 = GNUNET_YES;
2924 LOG (GNUNET_ERROR_TYPE_DEBUG,
2925 _("IPv4 support is %s\n"),
2926 (plugin->use_ipv4 == GNUNET_YES) ? "enabled" : "disabled");
2929 if (GNUNET_CONFIGURATION_have_value
2930 (plugin->env->cfg, plugin->name, "USE_IPv6"))
2933 GNUNET_CONFIGURATION_get_value_yesno (plugin->env->cfg,
2938 plugin->use_ipv6 = GNUNET_YES;
2939 LOG (GNUNET_ERROR_TYPE_DEBUG,
2940 _("IPv6 support is %s\n"),
2941 (plugin->use_ipv6 == GNUNET_YES) ? "enabled" : "disabled");
2943 if ((plugin->use_ipv4 == GNUNET_NO) && (plugin->use_ipv6 == GNUNET_NO))
2945 LOG (GNUNET_ERROR_TYPE_ERROR,
2946 _("Neither IPv4 nor IPv6 are enabled! Fix in configuration\n"));
2947 return GNUNET_SYSERR;
2950 /* Reading port number from config file */
2952 GNUNET_CONFIGURATION_get_value_number (plugin->env->cfg,
2954 "PORT", &port)) || (port > 65535))
2956 LOG (GNUNET_ERROR_TYPE_ERROR,
2957 _("Port is required! Fix in configuration\n"));
2958 return GNUNET_SYSERR;
2960 plugin->port = port;
2962 LOG (GNUNET_ERROR_TYPE_INFO,
2963 _("Using port %u\n"), plugin->port);
2965 if ((plugin->use_ipv4 == GNUNET_YES) &&
2966 (GNUNET_YES == GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg,
2967 plugin->name, "BINDTO", &bind4_address)))
2969 LOG (GNUNET_ERROR_TYPE_DEBUG,
2970 "Binding %s plugin to specific IPv4 address: `%s'\n",
2971 plugin->protocol, bind4_address);
2972 plugin->server_addr_v4 = GNUNET_new (struct sockaddr_in);
2973 if (1 != inet_pton (AF_INET, bind4_address,
2974 &plugin->server_addr_v4->sin_addr))
2976 LOG (GNUNET_ERROR_TYPE_ERROR,
2977 _("Specific IPv4 address `%s' in configuration file is invalid!\n"),
2979 GNUNET_free (bind4_address);
2980 GNUNET_free (plugin->server_addr_v4);
2981 plugin->server_addr_v4 = NULL;
2982 return GNUNET_SYSERR;
2986 LOG (GNUNET_ERROR_TYPE_DEBUG,
2987 _("Binding to IPv4 address %s\n"),
2989 plugin->server_addr_v4->sin_family = AF_INET;
2990 plugin->server_addr_v4->sin_port = htons (plugin->port);
2992 GNUNET_free (bind4_address);
2995 if ((plugin->use_ipv6 == GNUNET_YES) &&
2997 GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg,
2999 "BINDTO6", &bind6_address)))
3001 LOG (GNUNET_ERROR_TYPE_DEBUG,
3002 "Binding %s plugin to specific IPv6 address: `%s'\n",
3003 plugin->protocol, bind6_address);
3004 plugin->server_addr_v6 = GNUNET_new (struct sockaddr_in6);
3006 inet_pton (AF_INET6, bind6_address, &plugin->server_addr_v6->sin6_addr))
3008 LOG (GNUNET_ERROR_TYPE_ERROR,
3009 _("Specific IPv6 address `%s' in configuration file is invalid!\n"),
3011 GNUNET_free (bind6_address);
3012 GNUNET_free (plugin->server_addr_v6);
3013 plugin->server_addr_v6 = NULL;
3014 return GNUNET_SYSERR;
3018 LOG (GNUNET_ERROR_TYPE_DEBUG,
3019 _("Binding to IPv6 address %s\n"),
3021 plugin->server_addr_v6->sin6_family = AF_INET6;
3022 plugin->server_addr_v6->sin6_port = htons (plugin->port);
3024 GNUNET_free (bind6_address);
3027 plugin->verify_external_hostname = GNUNET_NO;
3029 plugin->verify_external_hostname = GNUNET_CONFIGURATION_get_value_yesno (plugin->env->cfg,
3031 "VERIFY_EXTERNAL_HOSTNAME");
3032 if (GNUNET_SYSERR == plugin->verify_external_hostname)
3033 plugin->verify_external_hostname = GNUNET_NO;
3034 if (GNUNET_YES == plugin->verify_external_hostname)
3035 plugin->options |= HTTP_OPTIONS_VERIFY_CERTIFICATE;
3037 external_hostname_use_port = GNUNET_CONFIGURATION_get_value_yesno (plugin->env->cfg,
3039 "EXTERNAL_HOSTNAME_USE_PORT");
3040 if (GNUNET_SYSERR == external_hostname_use_port)
3041 external_hostname_use_port = GNUNET_NO;
3045 GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg,
3047 "EXTERNAL_HOSTNAME",
3052 char *pos_url = NULL;
3054 if (NULL != strstr(eh_tmp, "://"))
3055 tmp = &strstr(eh_tmp, "://")[3];
3059 if (GNUNET_YES == external_hostname_use_port)
3061 if ( (strlen (tmp) > 1) && (NULL != (pos = strchr(tmp, '/'))) )
3065 GNUNET_asprintf (&plugin->external_hostname,
3069 (NULL == pos_url) ? "" : pos_url);
3072 GNUNET_asprintf (&plugin->external_hostname,
3078 plugin->external_hostname = GNUNET_strdup (tmp);
3079 GNUNET_free (eh_tmp);
3081 LOG (GNUNET_ERROR_TYPE_INFO,
3082 _("Using external hostname `%s'\n"),
3083 plugin->external_hostname);
3084 plugin->notify_ext_task = GNUNET_SCHEDULER_add_now (&server_notify_external_hostname,
3087 /* Use only configured external hostname */
3088 if (GNUNET_CONFIGURATION_have_value
3091 "EXTERNAL_HOSTNAME_ONLY"))
3093 plugin->external_only =
3094 GNUNET_CONFIGURATION_get_value_yesno (plugin->env->cfg,
3096 "EXTERNAL_HOSTNAME_ONLY");
3099 plugin->external_only = GNUNET_NO;
3101 if (GNUNET_YES == plugin->external_only)
3102 LOG (GNUNET_ERROR_TYPE_DEBUG,
3103 _("Notifying transport only about hostname `%s'\n"),
3104 plugin->external_hostname);
3107 LOG (GNUNET_ERROR_TYPE_DEBUG,
3108 "No external hostname configured\n");
3110 /* Optional parameters */
3112 GNUNET_CONFIGURATION_get_value_number (plugin->env->cfg,
3116 max_connections = 128;
3117 plugin->max_request = max_connections;
3119 LOG (GNUNET_ERROR_TYPE_DEBUG,
3120 _("Maximum number of connections is %u\n"),
3121 plugin->max_request);
3123 plugin->peer_id_length = strlen (GNUNET_i2s_full (plugin->env->my_identity));
3130 * Exit point from the plugin.
3136 LIBGNUNET_PLUGIN_TRANSPORT_DONE (void *cls)
3138 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
3139 struct HTTP_Server_Plugin *plugin = api->cls;
3141 if (NULL == api->cls)
3143 /* Free for stub mode */
3147 plugin->in_shutdown = GNUNET_YES;
3148 LOG (GNUNET_ERROR_TYPE_INFO,
3149 _("Shutting down plugin `%s'\n"),
3152 if (NULL != plugin->notify_ext_task)
3154 GNUNET_SCHEDULER_cancel (plugin->notify_ext_task);
3155 plugin->notify_ext_task = NULL;
3158 if (NULL != plugin->ext_addr)
3160 LOG (GNUNET_ERROR_TYPE_DEBUG,
3161 "Notifying transport to remove address `%s'\n",
3162 http_common_plugin_address_to_string (plugin->protocol,
3163 plugin->ext_addr->address,
3164 plugin->ext_addr->address_length));
3166 plugin->env->notify_address (plugin->env->cls,
3170 plugin->env->notify_address (plugin->env->cls,
3174 GNUNET_HELLO_address_free (plugin->ext_addr);
3175 plugin->ext_addr = NULL;
3178 /* Stop to report addresses to transport service */
3179 server_stop_report_addresses (plugin);
3180 if (NULL != plugin->server_v4)
3182 MHD_stop_daemon (plugin->server_v4);
3183 plugin->server_v4 = NULL;
3185 if (NULL != plugin->server_v6)
3187 MHD_stop_daemon (plugin->server_v6);
3188 plugin->server_v6 = NULL;
3190 if (NULL != plugin->server_v4_task)
3192 GNUNET_SCHEDULER_cancel (plugin->server_v4_task);
3193 plugin->server_v4_task = NULL;
3196 if (NULL != plugin->server_v6_task)
3198 GNUNET_SCHEDULER_cancel (plugin->server_v6_task);
3199 plugin->server_v6_task = NULL;
3202 GNUNET_free_non_null (plugin->crypto_init);
3203 GNUNET_free_non_null (plugin->cert);
3204 GNUNET_free_non_null (plugin->key);
3206 GNUNET_CONTAINER_multipeermap_iterate (plugin->sessions,
3207 &destroy_session_shutdown_cb,
3209 GNUNET_CONTAINER_multipeermap_destroy (plugin->sessions);
3210 plugin->sessions = NULL;
3212 GNUNET_free_non_null (plugin->external_hostname);
3213 GNUNET_free_non_null (plugin->ext_addr);
3214 GNUNET_free_non_null (plugin->server_addr_v4);
3215 GNUNET_free_non_null (plugin->server_addr_v6);
3216 regfree (&plugin->url_regex);
3218 LOG (GNUNET_ERROR_TYPE_DEBUG,
3219 _("Shutdown for plugin `%s' complete\n"),
3222 GNUNET_free (plugin);
3229 * Function called for a quick conversion of the binary address to
3230 * a numeric address. Note that the caller must not free the
3231 * address and that the next call to this function is allowed
3232 * to override the address again.
3235 * @param addr binary address
3236 * @param addrlen length of the address
3237 * @return string representing the same address
3240 http_server_plugin_address_to_string (void *cls,
3244 return http_common_plugin_address_to_string (PLUGIN_NAME,
3251 * Function obtain the network type for a session
3253 * @param cls closure ('struct HTTP_Server_Plugin*')
3254 * @param session the session
3255 * @return the network type in HBO or #GNUNET_SYSERR
3257 static enum GNUNET_ATS_Network_Type
3258 http_server_plugin_get_network (void *cls,
3259 struct Session *session)
3261 return session->scope;
3266 * Function that will be called whenever the transport service wants to
3267 * notify the plugin that the inbound quota changed and that the plugin
3268 * should update it's delay for the next receive value
3270 * @param cls closure
3271 * @param peer which peer was the session for
3272 * @param session which session is being updated
3273 * @param delay new delay to use for receiving
3276 http_server_plugin_update_inbound_delay (void *cls,
3277 const struct GNUNET_PeerIdentity *peer,
3278 struct Session *session,
3279 struct GNUNET_TIME_Relative delay)
3281 session->next_receive = GNUNET_TIME_relative_to_absolute (delay);
3282 LOG (GNUNET_ERROR_TYPE_DEBUG,
3283 "New inbound delay %s\n",
3284 GNUNET_STRINGS_relative_time_to_string (delay,
3286 if (NULL != session->recv_wakeup_task)
3288 GNUNET_SCHEDULER_cancel (session->recv_wakeup_task);
3289 session->recv_wakeup_task
3290 = GNUNET_SCHEDULER_add_delayed (delay,
3298 * Return information about the given session to the
3301 * @param cls the `struct Plugin` with the monitor callback (`sic`)
3302 * @param peer peer we send information about
3303 * @param value our `struct Session` to send information about
3304 * @return #GNUNET_OK (continue to iterate)
3307 send_session_info_iter (void *cls,
3308 const struct GNUNET_PeerIdentity *peer,
3311 struct HTTP_Server_Plugin *plugin = cls;
3312 struct Session *session = value;
3314 notify_session_monitor (plugin,
3316 GNUNET_TRANSPORT_SS_INIT);
3322 * Begin monitoring sessions of a plugin. There can only
3323 * be one active monitor per plugin (i.e. if there are
3324 * multiple monitors, the transport service needs to
3325 * multiplex the generated events over all of them).
3327 * @param cls closure of the plugin
3328 * @param sic callback to invoke, NULL to disable monitor;
3329 * plugin will being by iterating over all active
3330 * sessions immediately and then enter monitor mode
3331 * @param sic_cls closure for @a sic
3334 http_server_plugin_setup_monitor (void *cls,
3335 GNUNET_TRANSPORT_SessionInfoCallback sic,
3338 struct HTTP_Server_Plugin *plugin = cls;
3341 plugin->sic_cls = sic_cls;
3344 GNUNET_CONTAINER_multipeermap_iterate (plugin->sessions,
3345 &send_session_info_iter,
3347 /* signal end of first iteration */
3348 sic (sic_cls, NULL, NULL);
3354 * Entry point for the plugin.
3360 LIBGNUNET_PLUGIN_TRANSPORT_INIT (void *cls)
3362 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
3363 struct GNUNET_TRANSPORT_PluginFunctions *api;
3364 struct HTTP_Server_Plugin *plugin;
3366 if (NULL == env->receive)
3368 /* run in 'stub' mode (i.e. as part of gnunet-peerinfo), don't fully
3369 initialze the plugin or the API */
3370 api = GNUNET_new (struct GNUNET_TRANSPORT_PluginFunctions);
3372 api->address_to_string = &http_server_plugin_address_to_string;
3373 api->string_to_address = &http_common_plugin_string_to_address;
3374 api->address_pretty_printer = &http_common_plugin_address_pretty_printer;
3377 plugin = GNUNET_new (struct HTTP_Server_Plugin);
3379 plugin->sessions = GNUNET_CONTAINER_multipeermap_create (128,
3382 api = GNUNET_new (struct GNUNET_TRANSPORT_PluginFunctions);
3384 api->send = &http_server_plugin_send;
3385 api->disconnect_peer = &http_server_plugin_disconnect_peer;
3386 api->disconnect_session = &http_server_plugin_disconnect_session;
3387 api->query_keepalive_factor = &http_server_query_keepalive_factor;
3388 api->check_address = &http_server_plugin_address_suggested;
3389 api->get_session = &http_server_plugin_get_session;
3391 api->address_to_string = &http_server_plugin_address_to_string;
3392 api->string_to_address = &http_common_plugin_string_to_address;
3393 api->address_pretty_printer = &http_common_plugin_address_pretty_printer;
3394 api->get_network = &http_server_plugin_get_network;
3395 api->update_session_timeout = &http_server_plugin_update_session_timeout;
3396 api->update_inbound_delay = &http_server_plugin_update_inbound_delay;
3397 api->setup_monitor = &http_server_plugin_setup_monitor;
3399 plugin->name = "transport-https_server";
3400 plugin->protocol = "https";
3402 plugin->name = "transport-http_server";
3403 plugin->protocol = "http";
3407 GNUNET_CONFIGURATION_get_value_yesno (env->cfg,
3412 plugin->options |= HTTP_OPTIONS_TCP_STEALTH;
3414 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3415 _("TCP_STEALTH not supported on this platform.\n"));
3416 LIBGNUNET_PLUGIN_TRANSPORT_DONE (api);
3421 /* Compile URL regex */
3422 if (regcomp(&plugin->url_regex,
3426 LOG (GNUNET_ERROR_TYPE_ERROR,
3427 _("Unable to compile URL regex\n"));
3428 LIBGNUNET_PLUGIN_TRANSPORT_DONE (api);
3432 /* Configure plugin */
3433 if (GNUNET_SYSERR == server_configure_plugin (plugin))
3435 LIBGNUNET_PLUGIN_TRANSPORT_DONE (api);
3439 /* Check IPv6 support */
3440 if (GNUNET_YES == plugin->use_ipv6)
3441 plugin->use_ipv6 = server_check_ipv6_support (plugin);
3443 /* Report addresses to transport service */
3444 if (GNUNET_NO == plugin->external_only)
3445 server_start_report_addresses (plugin);
3447 if (GNUNET_SYSERR == server_start (plugin))
3449 LIBGNUNET_PLUGIN_TRANSPORT_DONE (api);
3455 /* end of plugin_transport_http_server.c */