2 This file is part of GNUnet
3 (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., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, 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.
60 struct ServerConnection
63 * The session this server connection belongs to
65 struct Session *session;
70 struct MHD_Connection *mhd_conn;
75 struct MHD_Daemon *mhd_daemon;
78 * Options requested by peer
81 #define OPTION_LONG_POLL 1 /* GET request wants long-poll semantics */
89 * For PUT connections: Is this the first or last callback with size 0
90 * For GET connections: Have we sent a message
98 * Wrapper to manage addresses
100 struct HttpAddressWrapper
105 struct HttpAddressWrapper *next;
108 * Linked list previous
110 struct HttpAddressWrapper *prev;
113 * An address we are using.
115 struct HttpAddress *address;
118 * Length of the address.
125 * Message to send using http
130 * next pointer for double linked list
132 struct HTTP_Message *next;
135 * previous pointer for double linked list
137 struct HTTP_Message *prev;
140 * buffer containing data to send
145 * amount of data already sent
155 * HTTP/S specific overhead
160 * Continuation function to call once the transmission buffer
161 * has again space available. NULL if there is no
162 * continuation to call.
164 GNUNET_TRANSPORT_TransmitContinuation transmit_cont;
167 * Closure for transmit_cont.
169 void *transmit_cont_cls;
174 * Session handle for connections.
180 * To whom are we talking to (set to our identity
181 * if we are still waiting for the welcome message)
183 struct GNUNET_PeerIdentity target;
186 * Pointer to the global plugin struct.
188 struct HTTP_Server_Plugin *plugin;
191 * next pointer for double linked list
193 struct HTTP_Message *msg_head;
196 * previous pointer for double linked list
198 struct HTTP_Message *msg_tail;
201 * Message stream tokenizer for incoming data
203 struct GNUNET_SERVER_MessageStreamTokenizer *msg_tk;
208 struct ServerConnection *server_recv;
213 struct ServerConnection *server_send;
218 struct GNUNET_HELLO_Address *address;
221 * Absolute time when to receive data again
222 * Used for receive throttling
224 struct GNUNET_TIME_Absolute next_receive;
227 * Absolute time when this connection will time out.
229 struct GNUNET_TIME_Absolute timeout;
232 * Session timeout task
234 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
237 * Task to resume MHD handling when receiving is allowed again
239 GNUNET_SCHEDULER_TaskIdentifier recv_wakeup_task;
242 * Number of bytes waiting for transmission to this peer.
244 unsigned long long bytes_in_queue;
247 * Number of messages waiting for transmission to this peer.
249 unsigned int msgs_in_queue;
252 * Unique HTTP/S connection tag for this connection
257 * ATS network type in NBO
259 uint32_t ats_address_network_type;
262 * #GNUNET_YES if this session is known to the service.
264 int known_to_service;
270 * Encapsulation of all of the state of the plugin.
272 struct HTTP_Server_Plugin
277 struct GNUNET_TRANSPORT_PluginEnvironment *env;
280 * Hash map of open sessions.
282 struct GNUNET_CONTAINER_MultiPeerMap *sessions;
285 * Function to call about session status changes.
287 GNUNET_TRANSPORT_SessionInfoCallback sic;
290 * Closure for @e sic.
307 char *external_hostname;
310 * External hostname the plugin can be connected to, can be different to
311 * the host's FQDN, used e.g. for reverse proxying
313 struct GNUNET_HELLO_Address *ext_addr;
316 * NAT handle & address management
318 struct GNUNET_NAT_Handle *nat;
321 * IPv4 addresses DLL head
323 struct HttpAddressWrapper *addr_head;
326 * IPv4 addresses DLL tail
328 struct HttpAddressWrapper *addr_tail;
331 * IPv4 server socket to bind to
333 struct sockaddr_in *server_addr_v4;
336 * IPv6 server socket to bind to
338 struct sockaddr_in6 *server_addr_v6;
343 struct MHD_Daemon *server_v4;
348 struct MHD_Daemon *server_v6;
356 * Use RC4-128 instead of AES:
357 * NONE:+VERS-TLS1.0:+ARCFOUR-128:+SHA1:+RSA:+COMP-NULL
376 GNUNET_SCHEDULER_TaskIdentifier server_v4_task;
381 GNUNET_SCHEDULER_TaskIdentifier server_v6_task;
384 * Task calling transport service about external address
386 GNUNET_SCHEDULER_TaskIdentifier notify_ext_task;
389 * Notify transport only about external address
391 unsigned int external_only;
394 * The IPv4 server is scheduled to run asap
396 int server_v4_immediately;
399 * The IPv6 server is scheduled to run asap
401 int server_v6_immediately;
404 * Verify external address
406 int verify_external_hostname;
409 * Maximum number of sockets the plugin can use
410 * Each http inbound /outbound connections are two connections
412 unsigned int max_connections;
415 * Current number of sockets the plugin can use
416 * Each http inbound /outbound connections are two connections
418 unsigned int cur_connections;
421 * Did we immediately end the session in disconnect_cb
431 * My options to be included in the address
451 * Regex for parsing URLs. FIXME: this seems overkill.
459 * If a session monitor is attached, notify it about the new
462 * @param plugin our plugin
463 * @param session session that changed state
464 * @param state new state of the session
467 notify_session_monitor (struct HTTP_Server_Plugin *plugin,
468 struct Session *session,
469 enum GNUNET_TRANSPORT_SessionState state)
471 struct GNUNET_TRANSPORT_SessionInfo info;
473 if (NULL == plugin->sic)
475 memset (&info, 0, sizeof (info));
477 info.is_inbound = GNUNET_YES;
478 info.num_msg_pending = session->msgs_in_queue;
479 info.num_bytes_pending = session->bytes_in_queue;
480 info.receive_delay = session->next_receive;
481 info.session_timeout = session->timeout;
482 info.address = session->address;
483 plugin->sic (plugin->sic_cls,
490 * Wake up an MHD connection which was suspended
492 * @param cls the session
493 * @param tc task context
496 server_wake_up (void *cls,
497 const struct GNUNET_SCHEDULER_TaskContext *tc)
499 struct Session *s = cls;
501 s->recv_wakeup_task = GNUNET_SCHEDULER_NO_TASK;
502 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
504 LOG (GNUNET_ERROR_TYPE_DEBUG,
505 "Session %p: Waking up PUT handle\n",
507 MHD_resume_connection (s->server_recv->mhd_conn);
512 * Reschedule the execution of both IPv4 and IPv6 server.
514 * @param plugin the plugin
515 * @param server which server to schedule v4 or v6?
516 * @param now #GNUNET_YES to schedule execution immediately, #GNUNET_NO to wait
520 server_reschedule (struct HTTP_Server_Plugin *plugin,
521 struct MHD_Daemon *server,
526 * Deletes the session. Must not be used afterwards.
528 * @param s the session to delete
531 server_delete_session (struct Session *s)
533 struct HTTP_Server_Plugin *plugin = s->plugin;
534 struct HTTP_Message *msg;
536 if (GNUNET_SCHEDULER_NO_TASK != s->timeout_task)
538 GNUNET_SCHEDULER_cancel (s->timeout_task);
539 s->timeout_task = GNUNET_SCHEDULER_NO_TASK;
540 s->timeout = GNUNET_TIME_UNIT_ZERO_ABS;
542 if (GNUNET_SCHEDULER_NO_TASK != s->recv_wakeup_task)
544 GNUNET_SCHEDULER_cancel (s->recv_wakeup_task);
545 s->recv_wakeup_task = GNUNET_SCHEDULER_NO_TASK;
546 if (NULL != s->server_recv)
547 MHD_resume_connection (s->server_recv->mhd_conn);
549 GNUNET_assert (GNUNET_OK ==
550 GNUNET_CONTAINER_multipeermap_remove (plugin->sessions,
553 while (NULL != (msg = s->msg_head))
555 GNUNET_CONTAINER_DLL_remove (s->msg_head,
558 if (NULL != msg->transmit_cont)
559 msg->transmit_cont (msg->transmit_cont_cls,
563 msg->pos + msg->overhead);
564 GNUNET_assert (s->msgs_in_queue > 0);
566 GNUNET_assert (s->bytes_in_queue >= msg->size);
567 s->bytes_in_queue -= msg->size;
571 GNUNET_assert (0 == s->msgs_in_queue);
572 GNUNET_assert (0 == s->bytes_in_queue);
574 if (NULL != s->server_send)
576 LOG (GNUNET_ERROR_TYPE_DEBUG,
577 "Server: %p / %p Terminating inbound PUT session to peer `%s'\n",
579 GNUNET_i2s (&s->target));
580 s->server_send->session = NULL;
581 MHD_set_connection_option (s->server_send->mhd_conn,
582 MHD_CONNECTION_OPTION_TIMEOUT,
583 1 /* 0 = no timeout, so this is MIN */);
584 server_reschedule (plugin, s->server_send->mhd_daemon, GNUNET_YES);
587 if (NULL != s->server_recv)
589 LOG (GNUNET_ERROR_TYPE_DEBUG,
590 "Server: %p / %p Terminating inbound GET session to peer `%s'\n",
591 s, s->server_recv, GNUNET_i2s (&s->target));
592 s->server_recv->session = NULL;
593 MHD_set_connection_option (s->server_recv->mhd_conn,
594 MHD_CONNECTION_OPTION_TIMEOUT,
595 1 /* 0 = no timeout, so this is MIN */);
596 server_reschedule (plugin, s->server_recv->mhd_daemon, GNUNET_YES);
598 notify_session_monitor (plugin,
600 GNUNET_TRANSPORT_SS_DOWN);
601 if (GNUNET_YES == s->known_to_service)
602 plugin->env->session_end (plugin->env->cls,
605 if (NULL != s->msg_tk)
607 GNUNET_SERVER_mst_destroy (s->msg_tk);
610 GNUNET_HELLO_address_free (s->address);
611 LOG (GNUNET_ERROR_TYPE_DEBUG,
612 "Session %p destroyed\n",
620 * Disconnect session @a s by telling MHD to close the
621 * connections (reducing timeout, etc.).
623 * @param cls closure with the `struct HTTP_Server_Plugin`
624 * @param s the session
625 * @return #GNUNET_OK on success
628 http_server_plugin_disconnect_session (void *cls,
631 server_delete_session (s);
637 * Session was idle, so disconnect it
639 * @param cls the session
640 * @param tc task context
643 server_session_timeout (void *cls,
644 const struct GNUNET_SCHEDULER_TaskContext *tc)
646 struct Session *s = cls;
647 struct GNUNET_TIME_Relative left;
649 s->timeout_task = GNUNET_SCHEDULER_NO_TASK;
650 left = GNUNET_TIME_absolute_get_remaining (s->timeout);
651 if (0 != left.rel_value_us)
653 /* not actually our turn yet, but let's at least update
654 the monitor, it may think we're about to die ... */
655 notify_session_monitor (s->plugin,
657 GNUNET_TRANSPORT_SS_UP);
658 s->timeout_task = GNUNET_SCHEDULER_add_delayed (left,
659 &server_session_timeout,
663 GNUNET_log (TIMEOUT_LOG,
664 "Session %p was idle for %s, disconnecting\n",
666 GNUNET_STRINGS_relative_time_to_string (HTTP_SERVER_SESSION_TIMEOUT,
668 server_delete_session (s);
673 * Increment session timeout due to activity session @a s
675 * @param s the session
678 server_reschedule_session_timeout (struct Session *s)
680 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK != s->timeout_task);
681 s->timeout = GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
686 * Function that can be used by the transport service to transmit
687 * a message using the plugin. Note that in the case of a
688 * peer disconnecting, the continuation MUST be called
689 * prior to the disconnect notification itself. This function
690 * will be called with this peer's HELLO message to initiate
691 * a fresh connection to another peer.
694 * @param session which session must be used
695 * @param msgbuf the message to transmit
696 * @param msgbuf_size number of bytes in @a msgbuf
697 * @param priority how important is the message (most plugins will
698 * ignore message priority and just FIFO)
699 * @param to how long to wait at most for the transmission (does not
700 * require plugins to discard the message after the timeout,
701 * just advisory for the desired delay; most plugins will ignore
703 * @param cont continuation to call once the message has
704 * been transmitted (or if the transport is ready
705 * for the next transmission call; or if the
706 * peer disconnected...); can be NULL
707 * @param cont_cls closure for @a cont
708 * @return number of bytes used (on the physical network, with overheads);
709 * -1 on hard errors (i.e. address invalid); 0 is a legal value
710 * and does NOT mean that the message was not transmitted (DV)
713 http_server_plugin_send (void *cls,
714 struct Session *session,
717 unsigned int priority,
718 struct GNUNET_TIME_Relative to,
719 GNUNET_TRANSPORT_TransmitContinuation cont,
722 struct HTTP_Server_Plugin *plugin = cls;
723 struct HTTP_Message *msg;
724 ssize_t bytes_sent = 0;
727 LOG (GNUNET_ERROR_TYPE_DEBUG,
728 "Session %p/connection %p: Sending message with %u to peer `%s'\n",
730 session->server_send,
732 GNUNET_i2s (&session->target));
734 /* create new message and schedule */
735 bytes_sent = sizeof (struct HTTP_Message) + msgbuf_size;
736 msg = GNUNET_malloc (bytes_sent);
738 msg->size = msgbuf_size;
740 msg->buf = (char *) &msg[1];
741 msg->transmit_cont = cont;
742 msg->transmit_cont_cls = cont_cls;
746 GNUNET_CONTAINER_DLL_insert_tail (session->msg_head,
749 session->msgs_in_queue++;
750 session->bytes_in_queue += msg->size;
751 notify_session_monitor (plugin,
753 GNUNET_TRANSPORT_SS_UP);
754 GNUNET_asprintf (&stat_txt,
755 "# bytes currently in %s_server buffers",
757 GNUNET_STATISTICS_update (plugin->env->stats,
758 stat_txt, msgbuf_size, GNUNET_NO);
759 GNUNET_free (stat_txt);
761 if (NULL != session->server_send)
762 server_reschedule (session->plugin,
763 session->server_send->mhd_daemon,
770 * Terminate session during shutdown.
772 * @param cls the `struct HTTP_Server_Plugin *`
773 * @param peer for which this is a session
774 * @param value the `struct Session` to clean up
775 * @return #GNUNET_OK (continue to iterate)
778 destroy_session_shutdown_cb (void *cls,
779 const struct GNUNET_PeerIdentity *peer,
782 struct Session *s = value;
783 struct ServerConnection *sc_send;
784 struct ServerConnection *sc_recv;
786 sc_send = s->server_send;
787 sc_recv = s->server_recv;
788 server_delete_session (s);
790 GNUNET_free (sc_send);
791 GNUNET_free (sc_recv);
799 * @param cls the `struct HTTP_Server_Plugin *`
800 * @param peer for which this is a session
801 * @param value the `struct Session` to clean up
802 * @return #GNUNET_OK (continue to iterate)
805 destroy_session_cb (void *cls,
806 const struct GNUNET_PeerIdentity *peer,
809 struct Session *s = value;
811 server_delete_session (s);
816 * Function that can be used to force the plugin to disconnect
817 * from the given peer and cancel all previous transmissions
818 * (and their continuationc).
821 * @param target peer from which to disconnect
824 http_server_plugin_disconnect_peer (void *cls,
825 const struct GNUNET_PeerIdentity *target)
827 struct HTTP_Server_Plugin *plugin = cls;
829 LOG (GNUNET_ERROR_TYPE_DEBUG,
830 "Transport tells me to disconnect `%s'\n",
831 GNUNET_i2s (target));
832 GNUNET_CONTAINER_multipeermap_get_multiple (plugin->sessions,
840 * Another peer has suggested an address for this
841 * peer and transport plugin. Check that this could be a valid
842 * address. If so, consider adding it to the list
846 * @param addr pointer to the address
847 * @param addrlen length of @a addr
848 * @return #GNUNET_OK if this is a plausible address for this peer
852 http_server_plugin_address_suggested (void *cls,
856 struct HTTP_Server_Plugin *plugin = cls;
857 struct HttpAddressWrapper *next;
858 struct HttpAddressWrapper *pos;
859 const struct HttpAddress *haddr = addr;
861 if ((NULL != plugin->ext_addr) &&
862 GNUNET_YES == (http_common_cmp_addresses (addr, addrlen,
863 plugin->ext_addr->address,
864 plugin->ext_addr->address_length)))
866 /* Checking HTTP_OPTIONS_VERIFY_CERTIFICATE option for external hostname */
867 if ((ntohl (haddr->options) & HTTP_OPTIONS_VERIFY_CERTIFICATE) !=
868 (plugin->options & HTTP_OPTIONS_VERIFY_CERTIFICATE))
869 return GNUNET_NO; /* VERIFY option not set as required! */
872 next = plugin->addr_head;
873 while (NULL != (pos = next))
876 if (GNUNET_YES == (http_common_cmp_addresses(addr,
887 * Creates a new outbound session the transport
888 * service will use to send data to the peer.
890 * Since HTTP/S server cannot create sessions, always returns NULL.
892 * @param cls the plugin
893 * @param address the address
894 * @return always NULL
896 static struct Session *
897 http_server_plugin_get_session (void *cls,
898 const struct GNUNET_HELLO_Address *address)
905 * Call MHD IPv4 to process pending requests and then go back
906 * and schedule the next run.
908 * @param cls plugin as closure
909 * @param tc task context
912 server_v4_run (void *cls,
913 const struct GNUNET_SCHEDULER_TaskContext *tc)
915 struct HTTP_Server_Plugin *plugin = cls;
917 plugin->server_v4_task = GNUNET_SCHEDULER_NO_TASK;
918 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
920 plugin->server_v4_immediately = GNUNET_NO;
921 GNUNET_assert (MHD_YES == MHD_run (plugin->server_v4));
922 server_reschedule (plugin, plugin->server_v4, GNUNET_NO);
927 * Call MHD IPv6 to process pending requests and then go back
928 * and schedule the next run.
930 * @param cls plugin as closure
931 * @param tc task context
934 server_v6_run (void *cls,
935 const struct GNUNET_SCHEDULER_TaskContext *tc)
937 struct HTTP_Server_Plugin *plugin = cls;
939 plugin->server_v6_task = GNUNET_SCHEDULER_NO_TASK;
940 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
942 plugin->server_v6_immediately = GNUNET_NO;
943 GNUNET_assert (MHD_YES == MHD_run (plugin->server_v6));
944 server_reschedule (plugin, plugin->server_v6, GNUNET_NO);
949 * Function that queries MHD's select sets and
950 * starts the task waiting for them.
952 * @param plugin plugin
953 * @param daemon_handle the MHD daemon handle
954 * @return gnunet task identifier
956 static GNUNET_SCHEDULER_TaskIdentifier
957 server_schedule (struct HTTP_Server_Plugin *plugin,
958 struct MHD_Daemon *daemon_handle,
961 GNUNET_SCHEDULER_TaskIdentifier ret;
965 struct GNUNET_NETWORK_FDSet *wrs;
966 struct GNUNET_NETWORK_FDSet *wws;
968 MHD_UNSIGNED_LONG_LONG timeout;
969 static unsigned long long last_timeout = 0;
971 struct GNUNET_TIME_Relative tv;
973 if (GNUNET_YES == plugin->in_shutdown)
974 return GNUNET_SCHEDULER_NO_TASK;
976 ret = GNUNET_SCHEDULER_NO_TASK;
980 wrs = GNUNET_NETWORK_fdset_create ();
981 wws = GNUNET_NETWORK_fdset_create ();
983 GNUNET_assert (MHD_YES ==
984 MHD_get_fdset (daemon_handle,
989 haveto = MHD_get_timeout (daemon_handle, &timeout);
990 if (haveto == MHD_YES)
992 if (timeout != last_timeout)
994 LOG (GNUNET_ERROR_TYPE_DEBUG,
995 "SELECT Timeout changed from %llu to %llu (ms)\n",
996 last_timeout, timeout);
997 last_timeout = timeout;
999 if (timeout <= GNUNET_TIME_UNIT_SECONDS.rel_value_us / 1000LL)
1000 tv.rel_value_us = (uint64_t) timeout * 1000LL;
1002 tv = GNUNET_TIME_UNIT_SECONDS;
1005 tv = GNUNET_TIME_UNIT_SECONDS;
1006 /* Force immediate run, since we have outbound data to send */
1007 if (now == GNUNET_YES)
1008 tv = GNUNET_TIME_UNIT_MILLISECONDS;
1009 GNUNET_NETWORK_fdset_copy_native (wrs, &rs, max + 1);
1010 GNUNET_NETWORK_fdset_copy_native (wws, &ws, max + 1);
1012 if (daemon_handle == plugin->server_v4)
1014 if (plugin->server_v4_task != GNUNET_SCHEDULER_NO_TASK)
1016 GNUNET_SCHEDULER_cancel (plugin->server_v4_task);
1017 plugin->server_v4_task = GNUNET_SCHEDULER_NO_TASK;
1020 LOG (GNUNET_ERROR_TYPE_DEBUG,
1021 "Scheduling IPv4 server task in %llu ms\n",
1025 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1027 &server_v4_run, plugin);
1029 if (daemon_handle == plugin->server_v6)
1031 if (plugin->server_v6_task != GNUNET_SCHEDULER_NO_TASK)
1033 GNUNET_SCHEDULER_cancel (plugin->server_v6_task);
1034 plugin->server_v6_task = GNUNET_SCHEDULER_NO_TASK;
1037 LOG (GNUNET_ERROR_TYPE_DEBUG,
1038 "Scheduling IPv6 server task in %llu ms\n", tv);
1041 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1043 &server_v6_run, plugin);
1045 GNUNET_NETWORK_fdset_destroy (wrs);
1046 GNUNET_NETWORK_fdset_destroy (wws);
1052 * Reschedule the execution of both IPv4 and IPv6 server
1054 * @param plugin the plugin
1055 * @param server which server to schedule v4 or v6?
1056 * @param now #GNUNET_YES to schedule execution immediately, #GNUNET_NO to wait
1060 server_reschedule (struct HTTP_Server_Plugin *plugin,
1061 struct MHD_Daemon *server,
1064 if ((server == plugin->server_v4) && (plugin->server_v4 != NULL))
1066 if (GNUNET_YES == plugin->server_v4_immediately)
1067 return; /* No rescheduling, server will run asap */
1069 if (GNUNET_YES == now)
1070 plugin->server_v4_immediately = GNUNET_YES;
1072 if (plugin->server_v4_task != GNUNET_SCHEDULER_NO_TASK)
1074 GNUNET_SCHEDULER_cancel (plugin->server_v4_task);
1075 plugin->server_v4_task = GNUNET_SCHEDULER_NO_TASK;
1077 plugin->server_v4_task = server_schedule (plugin, plugin->server_v4, now);
1080 if ((server == plugin->server_v6) && (plugin->server_v6 != NULL))
1082 if (GNUNET_YES == plugin->server_v6_immediately)
1083 return; /* No rescheduling, server will run asap */
1085 if (GNUNET_YES == now)
1086 plugin->server_v6_immediately = GNUNET_YES;
1088 if (plugin->server_v6_task != GNUNET_SCHEDULER_NO_TASK)
1090 GNUNET_SCHEDULER_cancel (plugin->server_v6_task);
1091 plugin->server_v6_task = GNUNET_SCHEDULER_NO_TASK;
1093 plugin->server_v6_task = server_schedule (plugin, plugin->server_v6, now);
1099 * Function that is called to get the keepalive factor.
1100 * GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT is divided by this number to
1101 * calculate the interval between keepalive packets.
1103 * @param cls closure with the `struct HTTP_Server_Plugin`
1104 * @return keepalive factor
1107 http_server_query_keepalive_factor (void *cls)
1114 * Function that will be called whenever the transport service wants to
1115 * notify the plugin that a session is still active and in use and
1116 * therefore the session timeout for this session has to be updated
1118 * @param cls closure
1119 * @param peer which peer was the session for
1120 * @param session which session is being updated
1123 http_server_plugin_update_session_timeout (void *cls,
1124 const struct GNUNET_PeerIdentity *peer,
1125 struct Session *session)
1127 server_reschedule_session_timeout (session);
1132 * Tell MHD that the connection should timeout after @a to seconds.
1134 * @param plugin our plugin
1135 * @param s session for which the timeout changes
1136 * @param to timeout in seconds
1139 server_mhd_connection_timeout (struct HTTP_Server_Plugin *plugin,
1143 /* Setting timeouts for other connections */
1144 if (NULL != s->server_recv)
1146 LOG (GNUNET_ERROR_TYPE_DEBUG,
1147 "Setting timeout for %p to %u sec.\n",
1148 s->server_recv, to);
1149 MHD_set_connection_option (s->server_recv->mhd_conn,
1150 MHD_CONNECTION_OPTION_TIMEOUT,
1152 server_reschedule (plugin, s->server_recv->mhd_daemon, GNUNET_NO);
1154 if (NULL != s->server_send)
1156 LOG (GNUNET_ERROR_TYPE_DEBUG,
1157 "Setting timeout for %p to %u sec.\n",
1158 s->server_send, to);
1159 MHD_set_connection_option (s->server_send->mhd_conn,
1160 MHD_CONNECTION_OPTION_TIMEOUT,
1162 server_reschedule (plugin, s->server_send->mhd_daemon, GNUNET_NO);
1168 * Parse incoming URL for tag and target
1170 * @param plugin plugin
1171 * @param url incoming url
1172 * @param target where to store the target
1173 * @param tag where to store the tag
1174 * @param options where to store the options
1175 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
1178 server_parse_url (struct HTTP_Server_Plugin *plugin,
1180 struct GNUNET_PeerIdentity *target,
1184 regmatch_t matches[4];
1185 const char *tag_start;
1186 const char *target_start;
1190 unsigned long int rc;
1194 ("^.*/([0-9A-Z]+);([0-9]+)(,[0-9]+)?$")
1199 return GNUNET_SYSERR;
1202 if (regexec(&plugin->url_regex, url, 4, matches, 0))
1204 LOG (GNUNET_ERROR_TYPE_DEBUG,
1205 "URL `%s' did not match regex\n", url);
1206 return GNUNET_SYSERR;
1209 target_start = &url[matches[1].rm_so];
1210 tag_start = &url[matches[2].rm_so];
1213 rc = strtoul (tag_start, &tag_end, 10);
1214 if (&url[matches[2].rm_eo] != tag_end)
1216 LOG (GNUNET_ERROR_TYPE_DEBUG,
1217 "URL tag did not line up with submatch\n");
1218 return GNUNET_SYSERR;
1222 LOG (GNUNET_ERROR_TYPE_DEBUG,
1223 "URL tag is zero\n");
1224 return GNUNET_SYSERR;
1226 if ((rc == ULONG_MAX) && (ERANGE == errno))
1228 LOG (GNUNET_ERROR_TYPE_DEBUG,
1229 "URL tag > ULONG_MAX\n");
1230 return GNUNET_SYSERR;
1232 if (rc > UINT32_MAX)
1234 LOG (GNUNET_ERROR_TYPE_DEBUG,
1235 "URL tag > UINT32_MAX\n");
1236 return GNUNET_SYSERR;
1238 (*tag) = (uint32_t)rc;
1239 LOG (GNUNET_ERROR_TYPE_DEBUG,
1240 "Found tag `%u' in url\n",
1243 /* convert peer id */
1244 hash_length = matches[1].rm_eo - matches[1].rm_so;
1245 if (hash_length != plugin->peer_id_length)
1247 LOG (GNUNET_ERROR_TYPE_DEBUG,
1248 "URL target is %u bytes, expecting %u\n",
1249 hash_length, plugin->peer_id_length);
1250 return GNUNET_SYSERR;
1253 GNUNET_CRYPTO_eddsa_public_key_from_string (target_start,
1255 &target->public_key))
1257 LOG (GNUNET_ERROR_TYPE_DEBUG,
1258 "URL target conversion failed\n");
1259 return GNUNET_SYSERR;
1261 LOG (GNUNET_ERROR_TYPE_DEBUG,
1262 "Found target `%s' in URL\n",
1263 GNUNET_i2s_full (target));
1265 /* convert options */
1266 if (-1 == matches[3].rm_so)
1272 rc = strtoul (&url[matches[3].rm_so + 1], &options_end, 10);
1273 if (&url[matches[3].rm_eo] != options_end)
1275 LOG (GNUNET_ERROR_TYPE_DEBUG,
1276 "URL options did not line up with submatch\n");
1277 return GNUNET_SYSERR;
1279 if ((rc == ULONG_MAX) && (ERANGE == errno))
1281 LOG (GNUNET_ERROR_TYPE_DEBUG,
1282 "URL options > ULONG_MAX\n");
1283 return GNUNET_SYSERR;
1285 if (rc > UINT32_MAX)
1287 LOG (GNUNET_ERROR_TYPE_DEBUG,
1288 "URL options > UINT32_MAX\n");
1289 return GNUNET_SYSERR;
1291 (*options) = (uint32_t) rc;
1292 LOG (GNUNET_ERROR_TYPE_DEBUG,
1293 "Found options `%u' in url\n",
1301 * Closure for #session_tag_it().
1303 struct SessionTagContext
1306 * Set to session matching the tag.
1308 struct Session *res;
1311 * Tag we are looking for.
1318 * Find a session with a matching tag.
1320 * @param cls the `struct SessionTagContext *`
1321 * @param key peer identity (unused)
1322 * @param value the `struct Session *`
1323 * @return #GNUNET_NO if we found the session, #GNUNET_OK if not
1326 session_tag_it (void *cls,
1327 const struct GNUNET_PeerIdentity *key,
1330 struct SessionTagContext *stc = cls;
1331 struct Session *s = value;
1333 if (s->tag == stc->tag)
1343 * Lookup a mhd connection and create one if none is found
1345 * @param plugin the plugin handle
1346 * @param mhd_connection the incoming mhd_connection
1347 * @param url incoming requested URL
1348 * @param method PUT or GET
1349 * @return the server connecetion
1351 static struct ServerConnection *
1352 server_lookup_connection (struct HTTP_Server_Plugin *plugin,
1353 struct MHD_Connection *mhd_connection,
1357 struct Session *s = NULL;
1358 struct ServerConnection *sc = NULL;
1359 const union MHD_ConnectionInfo *conn_info;
1360 struct HttpAddress *addr;
1361 struct GNUNET_ATS_Information ats;
1362 struct GNUNET_PeerIdentity target;
1364 struct SessionTagContext stc;
1366 int direction = GNUNET_SYSERR;
1369 conn_info = MHD_get_connection_info (mhd_connection,
1370 MHD_CONNECTION_INFO_CLIENT_ADDRESS);
1371 if ((conn_info->client_addr->sa_family != AF_INET) &&
1372 (conn_info->client_addr->sa_family != AF_INET6))
1374 LOG (GNUNET_ERROR_TYPE_DEBUG,
1375 "New %s connection from %s\n",
1379 if (GNUNET_SYSERR ==
1380 server_parse_url (plugin, url, &target, &stc.tag, &options))
1382 LOG (GNUNET_ERROR_TYPE_DEBUG,
1383 "Invalid url %s\n", url);
1386 if (0 == strcmp (MHD_HTTP_METHOD_PUT, method))
1387 direction = _RECEIVE;
1388 else if (0 == strcmp (MHD_HTTP_METHOD_GET, method))
1392 LOG (GNUNET_ERROR_TYPE_DEBUG,
1393 "Invalid method %s connection from %s\n",
1398 plugin->cur_connections++;
1399 LOG (GNUNET_ERROR_TYPE_DEBUG,
1400 "New %s connection from %s with tag %u (%u of %u)\n",
1402 GNUNET_i2s (&target),
1404 plugin->cur_connections, plugin->max_connections);
1405 /* find existing session */
1407 GNUNET_CONTAINER_multipeermap_get_multiple (plugin->sessions,
1411 if (NULL == (s = stc.res))
1413 /* create new session */
1415 switch (conn_info->client_addr->sa_family)
1418 addr = http_common_address_from_socket (plugin->protocol,
1419 conn_info->client_addr,
1420 sizeof (struct sockaddr_in));
1421 addr_len = http_common_address_get_size (addr);
1422 ats = plugin->env->get_address_type (plugin->env->cls,
1423 conn_info->client_addr,
1424 sizeof (struct sockaddr_in));
1427 addr = http_common_address_from_socket (plugin->protocol,
1428 conn_info->client_addr,
1429 sizeof (struct sockaddr_in6));
1430 addr_len = http_common_address_get_size (addr);
1431 ats = plugin->env->get_address_type (plugin->env->cls,
1432 conn_info->client_addr,
1433 sizeof (struct sockaddr_in6));
1436 /* external host name */
1437 ats.type = htonl (GNUNET_ATS_NETWORK_TYPE);
1438 ats.type = htonl (GNUNET_ATS_NET_WAN);
1441 s = GNUNET_new (struct Session);
1444 s->address = GNUNET_HELLO_address_allocate (&s->target,
1448 GNUNET_HELLO_ADDRESS_INFO_INBOUND);
1449 s->ats_address_network_type = ats.value;
1450 s->next_receive = GNUNET_TIME_UNIT_ZERO_ABS;
1452 s->timeout = GNUNET_TIME_relative_to_absolute (HTTP_SERVER_SESSION_TIMEOUT);
1453 s->timeout_task = GNUNET_SCHEDULER_add_delayed (HTTP_SERVER_SESSION_TIMEOUT,
1454 &server_session_timeout,
1456 (void) GNUNET_CONTAINER_multipeermap_put (plugin->sessions,
1459 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
1460 notify_session_monitor (plugin,
1462 GNUNET_TRANSPORT_SS_HANDSHAKE);
1463 LOG (GNUNET_ERROR_TYPE_DEBUG,
1464 "Creating new session %p for peer `%s' connecting from `%s'\n",
1465 s, GNUNET_i2s (&target),
1466 http_common_plugin_address_to_string (plugin->protocol,
1469 GNUNET_free_non_null (addr);
1472 if ( (_RECEIVE == direction) &&
1473 (NULL != s->server_recv) )
1475 LOG (GNUNET_ERROR_TYPE_DEBUG,
1476 "Duplicate PUT connection from `%s' tag %u, dismissing new connection\n",
1477 GNUNET_i2s (&target),
1481 if ((_SEND == direction) && (NULL != s->server_send))
1483 LOG (GNUNET_ERROR_TYPE_DEBUG,
1484 "Duplicate GET connection from `%s' tag %u, dismissing new connection\n",
1485 GNUNET_i2s (&target),
1489 sc = GNUNET_new (struct ServerConnection);
1490 if (conn_info->client_addr->sa_family == AF_INET)
1491 sc->mhd_daemon = plugin->server_v4;
1492 if (conn_info->client_addr->sa_family == AF_INET6)
1493 sc->mhd_daemon = plugin->server_v6;
1494 sc->mhd_conn = mhd_connection;
1495 sc->direction = direction;
1496 sc->connected = GNUNET_NO;
1498 sc->options = options;
1499 if (direction == _SEND)
1501 s->server_send = sc;
1503 if (direction == _RECEIVE)
1505 s->server_recv = sc;
1508 if ((GNUNET_NO == s->known_to_service) &&
1509 (NULL != s->server_send) &&
1510 (NULL != s->server_recv) )
1512 s->known_to_service = GNUNET_YES;
1513 plugin->env->session_start (NULL, s->address ,s, NULL, 0);
1516 if ( (NULL == s->server_recv) ||
1517 (NULL == s->server_send) )
1519 to = (HTTP_SERVER_NOT_VALIDATED_TIMEOUT.rel_value_us / 1000LL / 1000LL);
1520 MHD_set_connection_option (mhd_connection,
1521 MHD_CONNECTION_OPTION_TIMEOUT, to);
1522 server_reschedule (plugin, sc->mhd_daemon, GNUNET_NO);
1526 LOG (GNUNET_ERROR_TYPE_DEBUG,
1527 "Session %p for peer `%s' fully connected\n",
1528 s, GNUNET_i2s (&target));
1529 to = (HTTP_SERVER_SESSION_TIMEOUT.rel_value_us / 1000LL / 1000LL);
1530 server_mhd_connection_timeout (plugin, s, to);
1532 LOG (GNUNET_ERROR_TYPE_DEBUG,
1533 "Setting timeout for %p to %u sec.\n", sc, to);
1539 * Callback called by MHD when it needs data to send
1541 * @param cls current session
1542 * @param pos position in buffer
1543 * @param buf the buffer to write data to
1544 * @param max max number of bytes available in @a buf
1545 * @return bytes written to @a buf
1548 server_send_callback (void *cls,
1553 struct Session *s = cls;
1554 struct ServerConnection *sc;
1555 ssize_t bytes_read = 0;
1556 struct HTTP_Message *msg;
1559 sc = s->server_send;
1566 bytes_read = GNUNET_MIN (msg->size - msg->pos,
1568 memcpy (buf, &msg->buf[msg->pos], bytes_read);
1569 msg->pos += bytes_read;
1571 /* removing message */
1572 if (msg->pos == msg->size)
1574 GNUNET_CONTAINER_DLL_remove (s->msg_head,
1577 if (NULL != msg->transmit_cont)
1578 msg->transmit_cont (msg->transmit_cont_cls, &s->target, GNUNET_OK,
1579 msg->size, msg->size + msg->overhead);
1580 GNUNET_assert (s->msgs_in_queue > 0);
1582 GNUNET_assert (s->bytes_in_queue >= msg->size);
1583 s->bytes_in_queue -= msg->size;
1585 notify_session_monitor (s->plugin,
1587 GNUNET_TRANSPORT_SS_UP);
1592 sc->connected = GNUNET_YES;
1593 LOG (GNUNET_ERROR_TYPE_DEBUG,
1594 "Sent %u bytes to peer `%s' with session %p \n",
1596 GNUNET_i2s (&s->target),
1598 GNUNET_asprintf (&stat_txt,
1599 "# bytes currently in %s_server buffers",
1600 s->plugin->protocol);
1601 GNUNET_STATISTICS_update (s->plugin->env->stats,
1605 GNUNET_free (stat_txt);
1606 GNUNET_asprintf (&stat_txt,
1607 "# bytes transmitted via %s_server",
1608 s->plugin->protocol);
1609 GNUNET_STATISTICS_update (s->plugin->env->stats,
1610 stat_txt, bytes_read, GNUNET_NO);
1611 GNUNET_free (stat_txt);
1613 else if ((sc->options & OPTION_LONG_POLL) && sc->connected)
1615 LOG (GNUNET_ERROR_TYPE_DEBUG,
1616 "Completing GET response to peer `%s' with session %p\n",
1617 GNUNET_i2s (&s->target),
1619 return MHD_CONTENT_READER_END_OF_STREAM;
1626 * Callback called by MessageStreamTokenizer when a message has arrived
1628 * @param cls current session as closure
1629 * @param client client
1630 * @param message the message to be forwarded to transport service
1631 * @return #GNUNET_OK
1634 server_receive_mst_cb (void *cls,
1636 const struct GNUNET_MessageHeader *message)
1638 struct Session *s = cls;
1639 struct HTTP_Server_Plugin *plugin = s->plugin;
1640 struct GNUNET_ATS_Information atsi;
1641 struct GNUNET_TIME_Relative delay;
1644 atsi.type = htonl (GNUNET_ATS_NETWORK_TYPE);
1645 atsi.value = s->ats_address_network_type;
1646 GNUNET_break (s->ats_address_network_type !=
1647 ntohl (GNUNET_ATS_NET_UNSPECIFIED));
1649 if (GNUNET_NO == s->known_to_service)
1651 s->known_to_service = GNUNET_YES;
1652 plugin->env->session_start (NULL,
1657 notify_session_monitor (plugin,
1659 GNUNET_TRANSPORT_SS_UP);
1661 delay = plugin->env->receive (plugin->env->cls,
1665 plugin->env->update_address_metrics (plugin->env->cls,
1668 GNUNET_asprintf (&stat_txt,
1669 "# bytes received via %s_server",
1671 GNUNET_STATISTICS_update (plugin->env->stats,
1672 stat_txt, ntohs (message->size), GNUNET_NO);
1673 GNUNET_free (stat_txt);
1674 s->next_receive = GNUNET_TIME_relative_to_absolute (delay);
1675 if (delay.rel_value_us > 0)
1677 LOG (GNUNET_ERROR_TYPE_DEBUG,
1678 "Peer `%s' address `%s' next read delayed for %s\n",
1679 GNUNET_i2s (&s->target),
1680 http_common_plugin_address_to_string (plugin->protocol,
1681 s->address->address,
1682 s->address->address_length),
1683 GNUNET_STRINGS_relative_time_to_string (delay,
1686 server_reschedule_session_timeout (s);
1692 * Add headers to a request indicating that we allow Cross-Origin Resource
1695 * @param response response object to modify
1698 add_cors_headers(struct MHD_Response *response)
1700 MHD_add_response_header (response,
1701 "Access-Control-Allow-Origin",
1703 MHD_add_response_header (response,
1704 "Access-Control-Allow-Methods",
1705 "GET, PUT, OPTIONS");
1706 MHD_add_response_header (response,
1707 "Access-Control-Max-Age",
1713 * MHD callback for a new incoming connection
1715 * @param cls the plugin handle
1716 * @param mhd_connection the mhd connection
1717 * @param url the requested URL
1718 * @param method GET or PUT
1719 * @param version HTTP version
1720 * @param upload_data upload data
1721 * @param upload_data_size size of @a upload_data
1722 * @param httpSessionCache the session cache to remember the connection
1723 * @return MHD_YES if connection is accepted, MHD_NO on reject
1726 server_access_cb (void *cls,
1727 struct MHD_Connection *mhd_connection,
1730 const char *version,
1731 const char *upload_data,
1732 size_t *upload_data_size,
1733 void **httpSessionCache)
1735 struct HTTP_Server_Plugin *plugin = cls;
1736 struct ServerConnection *sc = *httpSessionCache;
1738 struct MHD_Response *response;
1741 LOG (GNUNET_ERROR_TYPE_DEBUG,
1742 _("Access from connection %p (%u of %u) for `%s' `%s' url `%s' with upload data size %u\n"),
1744 plugin->cur_connections,
1745 plugin->max_connections,
1749 (*upload_data_size));
1752 /* CORS pre-flight request */
1753 if (0 == strcmp (MHD_HTTP_METHOD_OPTIONS, method))
1755 response = MHD_create_response_from_buffer (0, NULL,
1756 MHD_RESPMEM_PERSISTENT);
1757 add_cors_headers(response);
1758 res = MHD_queue_response (mhd_connection, MHD_HTTP_OK, response);
1759 MHD_destroy_response (response);
1762 /* new connection */
1763 sc = server_lookup_connection (plugin, mhd_connection, url, method);
1766 /* attach to new / existing session */
1767 (*httpSessionCache) = sc;
1771 /* existing session already has matching connection, refuse */
1772 response = MHD_create_response_from_data (strlen (HTTP_ERROR_RESPONSE),
1773 HTTP_ERROR_RESPONSE,
1776 MHD_add_response_header (response,
1777 MHD_HTTP_HEADER_CONTENT_TYPE,
1779 add_cors_headers(response);
1780 res = MHD_queue_response (mhd_connection, MHD_HTTP_NOT_FOUND, response);
1781 MHD_destroy_response (response);
1785 /* 'old' connection */
1786 if (NULL == (s = sc->session))
1788 /* Session was already disconnected;
1789 sent HTTP/1.1: 200 OK as response */
1790 response = MHD_create_response_from_data (strlen ("Thank you!"),
1793 add_cors_headers(response);
1794 MHD_queue_response (mhd_connection, MHD_HTTP_OK, response);
1795 MHD_destroy_response (response);
1799 if (sc->direction == _SEND)
1801 response = MHD_create_response_from_callback (MHD_SIZE_UNKNOWN,
1803 &server_send_callback, s,
1805 add_cors_headers(response);
1806 MHD_queue_response (mhd_connection, MHD_HTTP_OK, response);
1807 MHD_destroy_response (response);
1810 if (sc->direction == _RECEIVE)
1812 if ((*upload_data_size == 0) && (sc->connected == GNUNET_NO))
1814 /* (*upload_data_size == 0) first callback when header are passed */
1815 LOG (GNUNET_ERROR_TYPE_DEBUG,
1816 "Session %p / Connection %p: Peer `%s' PUT on address `%s' connected\n",
1818 GNUNET_i2s (&s->target),
1819 http_common_plugin_address_to_string (plugin->protocol,
1820 s->address->address,
1821 s->address->address_length));
1822 sc->connected = GNUNET_YES;
1825 else if ((*upload_data_size == 0) && (sc->connected == GNUNET_YES))
1827 /* (*upload_data_size == 0) when upload is complete */
1828 LOG (GNUNET_ERROR_TYPE_DEBUG,
1829 "Session %p / Connection %p: Peer `%s' PUT on address `%s' finished upload\n",
1831 GNUNET_i2s (&s->target),
1832 http_common_plugin_address_to_string (plugin->protocol,
1833 s->address->address,
1834 s->address->address_length));
1835 sc->connected = GNUNET_NO;
1836 /* Sent HTTP/1.1: 200 OK as PUT Response\ */
1837 response = MHD_create_response_from_data (strlen ("Thank you!"),
1840 add_cors_headers(response);
1841 MHD_queue_response (mhd_connection, MHD_HTTP_OK, response);
1842 MHD_destroy_response (response);
1845 else if ((*upload_data_size > 0) && (sc->connected == GNUNET_YES))
1847 struct GNUNET_TIME_Relative delay;
1849 /* (*upload_data_size > 0) for every segment received */
1850 LOG (GNUNET_ERROR_TYPE_DEBUG,
1851 "Session %p / Connection %p: Peer `%s' PUT on address `%s' received %u bytes\n",
1853 GNUNET_i2s (&s->target),
1854 http_common_plugin_address_to_string (plugin->protocol,
1855 s->address->address,
1856 s->address->address_length),
1858 delay = GNUNET_TIME_absolute_get_remaining (s->next_receive);
1859 if (0 == delay.rel_value_us)
1861 LOG (GNUNET_ERROR_TYPE_DEBUG,
1862 "PUT with %u bytes forwarded to MST\n",
1864 if (s->msg_tk == NULL)
1866 s->msg_tk = GNUNET_SERVER_mst_create (&server_receive_mst_cb, s);
1868 GNUNET_SERVER_mst_receive (s->msg_tk, s, upload_data,
1869 *upload_data_size, GNUNET_NO, GNUNET_NO);
1870 server_mhd_connection_timeout (plugin, s,
1871 GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT.rel_value_us / 1000LL / 1000LL);
1872 (*upload_data_size) = 0;
1876 /* delay processing */
1877 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1878 "Session %p / Connection %p: no inbound bandwidth available! Next read was delayed by %s\n",
1880 GNUNET_STRINGS_relative_time_to_string (delay,
1882 GNUNET_assert (s->server_recv->mhd_conn == mhd_connection);
1883 MHD_suspend_connection (s->server_recv->mhd_conn);
1884 if (GNUNET_SCHEDULER_NO_TASK == s->recv_wakeup_task)
1885 s->recv_wakeup_task = GNUNET_SCHEDULER_add_delayed (delay,
1902 * Callback from MHD when a connection disconnects
1904 * @param cls closure with the `struct HTTP_Server_Plugin *`
1905 * @param connection the disconnected MHD connection
1906 * @param httpSessionCache the pointer to distinguish
1909 server_disconnect_cb (void *cls,
1910 struct MHD_Connection *connection,
1911 void **httpSessionCache)
1913 struct HTTP_Server_Plugin *plugin = cls;
1914 struct ServerConnection *sc = *httpSessionCache;
1916 LOG (GNUNET_ERROR_TYPE_DEBUG,
1917 "Disconnect for connection %p\n",
1922 return; /* never really got setup */
1925 if (NULL != sc->session)
1927 if (sc->direction == _SEND)
1929 LOG (GNUNET_ERROR_TYPE_DEBUG,
1930 "Peer `%s' connection %p, GET on address `%s' disconnected\n",
1931 GNUNET_i2s (&sc->session->target),
1932 sc->session->server_send,
1933 http_common_plugin_address_to_string (plugin->protocol,
1934 sc->session->address->address,
1935 sc->session->address->address_length));
1937 sc->session->server_send = NULL;
1939 else if (sc->direction == _RECEIVE)
1941 LOG (GNUNET_ERROR_TYPE_DEBUG,
1942 "Peer `%s' connection %p PUT on address `%s' disconnected\n",
1943 GNUNET_i2s (&sc->session->target),
1944 sc->session->server_recv,
1945 http_common_plugin_address_to_string (plugin->protocol,
1946 sc->session->address->address,
1947 sc->session->address->address_length));
1948 sc->session->server_recv = NULL;
1949 if (NULL != sc->session->msg_tk)
1951 GNUNET_SERVER_mst_destroy (sc->session->msg_tk);
1952 sc->session->msg_tk = NULL;
1957 plugin->cur_connections--;
1962 * Check if incoming connection is accepted.
1964 * @param cls plugin as closure
1965 * @param addr address of incoming connection
1966 * @param addr_len number of bytes in @a addr
1967 * @return MHD_YES if connection is accepted, MHD_NO if connection is rejected
1970 server_accept_cb (void *cls,
1971 const struct sockaddr *addr,
1974 struct HTTP_Server_Plugin *plugin = cls;
1976 if (plugin->cur_connections <= plugin->max_connections)
1978 LOG (GNUNET_ERROR_TYPE_DEBUG,
1979 _("Accepting connection (%u of %u) from `%s'\n"),
1980 plugin->cur_connections, plugin->max_connections,
1981 GNUNET_a2s (addr, addr_len));
1986 LOG (GNUNET_ERROR_TYPE_WARNING,
1987 _("Server reached maximum number connections (%u), rejecting new connection\n"),
1988 plugin->max_connections);
1995 * Log function called by MHD.
1998 * @param fmt format string
1999 * @param ap arguments for the format string (va_start() and va_end()
2000 * will be called by MHD)
2003 server_log (void *arg,
2013 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2021 * Load ssl certificate from file
2023 * @param file filename
2024 * @return content of the file
2027 server_load_file (const char *file)
2029 struct GNUNET_DISK_FileHandle *gn_file;
2033 if (GNUNET_OK != GNUNET_DISK_file_size (file,
2034 &fsize, GNUNET_NO, GNUNET_YES))
2036 text = GNUNET_malloc (fsize + 1);
2038 GNUNET_DISK_file_open (file, GNUNET_DISK_OPEN_READ,
2039 GNUNET_DISK_PERM_USER_READ);
2040 if (NULL == gn_file)
2045 if (GNUNET_SYSERR == GNUNET_DISK_file_read (gn_file, text, fsize))
2048 GNUNET_DISK_file_close (gn_file);
2052 GNUNET_DISK_file_close (gn_file);
2060 * Load ssl certificate
2062 * @param plugin the plugin
2063 * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
2066 server_load_certificate (struct HTTP_Server_Plugin *plugin)
2068 int res = GNUNET_OK;
2074 GNUNET_CONFIGURATION_get_value_filename (plugin->env->cfg,
2076 "KEY_FILE", &key_file))
2078 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
2079 plugin->name, "CERT_FILE");
2080 return GNUNET_SYSERR;
2083 GNUNET_CONFIGURATION_get_value_filename (plugin->env->cfg,
2085 "CERT_FILE", &cert_file))
2087 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
2088 plugin->name, "CERT_FILE");
2089 GNUNET_free (key_file);
2090 return GNUNET_SYSERR;
2092 /* Get crypto init string from config. If not present, use
2095 GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg,
2098 &plugin->crypto_init))
2099 LOG (GNUNET_ERROR_TYPE_DEBUG,
2100 "Using crypto init string `%s'\n",
2101 plugin->crypto_init);
2103 LOG (GNUNET_ERROR_TYPE_DEBUG,
2104 "Using default crypto init string \n");
2106 /* read key & certificates from file */
2107 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2108 "Trying to loading TLS certificate from key-file `%s' cert-file`%s'\n",
2109 key_file, cert_file);
2111 plugin->key = server_load_file (key_file);
2112 plugin->cert = server_load_file (cert_file);
2114 if ((plugin->key == NULL) || (plugin->cert == NULL))
2116 struct GNUNET_OS_Process *cert_creation;
2118 GNUNET_free_non_null (plugin->key);
2120 GNUNET_free_non_null (plugin->cert);
2121 plugin->cert = NULL;
2123 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2124 "No usable TLS certificate found, creating certificate\n");
2127 GNUNET_OS_start_process (GNUNET_NO, GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
2129 "gnunet-transport-certificate-creation",
2130 "gnunet-transport-certificate-creation",
2131 key_file, cert_file, NULL);
2132 if (cert_creation == NULL)
2134 LOG (GNUNET_ERROR_TYPE_ERROR,
2135 _("Could not create a new TLS certificate, program `gnunet-transport-certificate-creation' could not be started!\n"));
2136 GNUNET_free (key_file);
2137 GNUNET_free (cert_file);
2139 GNUNET_free_non_null (plugin->key);
2141 GNUNET_free_non_null (plugin->cert);
2142 plugin->cert = NULL;
2143 GNUNET_free_non_null (plugin->crypto_init);
2144 plugin->crypto_init = NULL;
2146 return GNUNET_SYSERR;
2148 GNUNET_assert (GNUNET_OK == GNUNET_OS_process_wait (cert_creation));
2149 GNUNET_OS_process_destroy (cert_creation);
2151 plugin->key = server_load_file (key_file);
2152 plugin->cert = server_load_file (cert_file);
2155 if ((plugin->key == NULL) || (plugin->cert == NULL))
2157 LOG (GNUNET_ERROR_TYPE_ERROR,
2158 _("No usable TLS certificate found and creating one at `%s/%s' failed!\n"),
2159 key_file, cert_file);
2160 GNUNET_free (key_file);
2161 GNUNET_free (cert_file);
2163 GNUNET_free_non_null (plugin->key);
2165 GNUNET_free_non_null (plugin->cert);
2166 plugin->cert = NULL;
2167 GNUNET_free_non_null (plugin->crypto_init);
2168 plugin->crypto_init = NULL;
2170 return GNUNET_SYSERR;
2172 GNUNET_free (key_file);
2173 GNUNET_free (cert_file);
2174 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "TLS certificate loaded\n");
2181 * Start the HTTP server
2183 * @param plugin the plugin handle
2184 * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
2187 server_start (struct HTTP_Server_Plugin *plugin)
2189 unsigned int timeout;
2191 GNUNET_assert (NULL != plugin);
2194 if (GNUNET_SYSERR == server_load_certificate (plugin))
2196 LOG (GNUNET_ERROR_TYPE_ERROR,
2197 "Could not load or create server certificate! Loading plugin failed!\n");
2198 return GNUNET_SYSERR;
2203 #if MHD_VERSION >= 0x00090E00
2204 timeout = HTTP_SERVER_NOT_VALIDATED_TIMEOUT.rel_value_us / 1000LL / 1000LL;
2205 LOG (GNUNET_ERROR_TYPE_DEBUG,
2206 "MHD can set timeout per connection! Default time out %u sec.\n",
2209 timeout = HTTP_SERVER_SESSION_TIMEOUT.rel_value_us / 1000LL / 1000LL;
2210 LOG (GNUNET_ERROR_TYPE_WARNING,
2211 "MHD cannot set timeout per connection! Default time out %u sec.\n",
2215 plugin->server_v4 = NULL;
2216 if (plugin->use_ipv4 == GNUNET_YES)
2218 plugin->server_v4 = MHD_start_daemon (
2225 MHD_NO_FLAG, plugin->port,
2226 &server_accept_cb, plugin,
2227 &server_access_cb, plugin,
2228 MHD_OPTION_SOCK_ADDR,
2229 (struct sockaddr_in *)
2230 plugin->server_addr_v4,
2231 MHD_OPTION_CONNECTION_LIMIT,
2233 plugin->max_connections,
2235 MHD_OPTION_HTTPS_PRIORITIES,
2236 plugin->crypto_init,
2237 MHD_OPTION_HTTPS_MEM_KEY,
2239 MHD_OPTION_HTTPS_MEM_CERT,
2242 MHD_OPTION_CONNECTION_TIMEOUT,
2244 MHD_OPTION_CONNECTION_MEMORY_LIMIT,
2246 GNUNET_SERVER_MAX_MESSAGE_SIZE),
2247 MHD_OPTION_NOTIFY_COMPLETED,
2248 &server_disconnect_cb, plugin,
2249 MHD_OPTION_EXTERNAL_LOGGER,
2250 server_log, NULL, MHD_OPTION_END);
2251 if (plugin->server_v4 == NULL)
2253 LOG (GNUNET_ERROR_TYPE_ERROR,
2254 "Failed to start %s IPv4 server component on port %u\n",
2255 plugin->name, plugin->port);
2258 server_reschedule (plugin,
2264 plugin->server_v6 = NULL;
2265 if (plugin->use_ipv6 == GNUNET_YES)
2267 plugin->server_v6 = MHD_start_daemon (
2274 MHD_USE_IPv6, plugin->port,
2275 &server_accept_cb, plugin,
2276 &server_access_cb, plugin,
2277 MHD_OPTION_SOCK_ADDR,
2278 (struct sockaddr_in6 *)
2279 plugin->server_addr_v6,
2280 MHD_OPTION_CONNECTION_LIMIT,
2282 plugin->max_connections,
2284 MHD_OPTION_HTTPS_PRIORITIES,
2285 plugin->crypto_init,
2286 MHD_OPTION_HTTPS_MEM_KEY,
2288 MHD_OPTION_HTTPS_MEM_CERT,
2291 MHD_OPTION_CONNECTION_TIMEOUT,
2293 MHD_OPTION_CONNECTION_MEMORY_LIMIT,
2295 GNUNET_SERVER_MAX_MESSAGE_SIZE),
2296 MHD_OPTION_NOTIFY_COMPLETED,
2297 &server_disconnect_cb, plugin,
2298 MHD_OPTION_EXTERNAL_LOGGER,
2299 server_log, NULL, MHD_OPTION_END);
2300 if (plugin->server_v6 == NULL)
2302 LOG (GNUNET_ERROR_TYPE_ERROR,
2303 "Failed to start %s IPv6 server component on port %u\n",
2304 plugin->name, plugin->port);
2307 server_reschedule (plugin, plugin->server_v6, GNUNET_NO);
2311 if ((plugin->server_v6 == NULL) && (plugin->server_v4 == NULL))
2313 LOG (GNUNET_ERROR_TYPE_ERROR,
2314 "%s %s server component started on port %u\n",
2315 msg, plugin->name, plugin->port);
2317 return GNUNET_SYSERR;
2319 else if ((plugin->server_v6 != NULL) && (plugin->server_v4 != NULL))
2320 msg = "IPv4 and IPv6";
2321 else if (plugin->server_v6 != NULL)
2323 else if (plugin->server_v4 != NULL)
2325 LOG (GNUNET_ERROR_TYPE_DEBUG,
2326 "%s %s server component started on port %u\n",
2327 msg, plugin->name, plugin->port);
2333 * Add an address to the server's set of addresses and notify transport
2335 * @param cls the plugin handle
2336 * @param add_remove #GNUNET_YES on add, #GNUNET_NO on remove
2337 * @param addr the address
2338 * @param addrlen address length
2341 server_add_address (void *cls,
2343 const struct sockaddr *addr,
2346 struct HTTP_Server_Plugin *plugin = cls;
2347 struct GNUNET_HELLO_Address *address;
2348 struct HttpAddressWrapper *w = NULL;
2350 w = GNUNET_new (struct HttpAddressWrapper);
2351 w->address = http_common_address_from_socket (plugin->protocol,
2354 if (NULL == w->address)
2359 w->addrlen = http_common_address_get_size (w->address);
2361 GNUNET_CONTAINER_DLL_insert (plugin->addr_head,
2364 LOG (GNUNET_ERROR_TYPE_DEBUG,
2365 "Notifying transport to add address `%s'\n",
2366 http_common_plugin_address_to_string (plugin->protocol,
2369 /* modify our published address list */
2371 address = GNUNET_HELLO_address_allocate (plugin->env->my_identity,
2372 "https_client", w->address, w->addrlen, GNUNET_HELLO_ADDRESS_INFO_NONE);
2374 address = GNUNET_HELLO_address_allocate (plugin->env->my_identity,
2375 "http_client", w->address, w->addrlen, GNUNET_HELLO_ADDRESS_INFO_NONE);
2378 plugin->env->notify_address (plugin->env->cls,
2381 GNUNET_HELLO_address_free (address);
2386 * Remove an address from the server's set of addresses and notify transport
2388 * @param cls the plugin handle
2389 * @param add_remove #GNUNET_YES on add, #GNUNET_NO on remove
2390 * @param addr the address
2391 * @param addrlen address length
2394 server_remove_address (void *cls,
2396 const struct sockaddr *addr,
2399 struct HTTP_Server_Plugin *plugin = cls;
2400 struct GNUNET_HELLO_Address *address;
2401 struct HttpAddressWrapper *w = plugin->addr_head;
2405 saddr = http_common_address_from_socket (plugin->protocol,
2410 saddr_len = http_common_address_get_size (saddr);
2415 http_common_cmp_addresses (w->address,
2422 GNUNET_free (saddr);
2427 LOG (GNUNET_ERROR_TYPE_DEBUG,
2428 "Notifying transport to remove address `%s'\n",
2429 http_common_plugin_address_to_string (plugin->protocol,
2432 GNUNET_CONTAINER_DLL_remove (plugin->addr_head,
2435 /* modify our published address list */
2437 address = GNUNET_HELLO_address_allocate (plugin->env->my_identity,
2438 "https_client", w->address, w->addrlen, GNUNET_HELLO_ADDRESS_INFO_NONE);
2440 address = GNUNET_HELLO_address_allocate (plugin->env->my_identity,
2441 "http_client", w->address, w->addrlen, GNUNET_HELLO_ADDRESS_INFO_NONE);
2443 plugin->env->notify_address (plugin->env->cls, add_remove, address);
2444 GNUNET_HELLO_address_free (address);
2445 GNUNET_free (w->address);
2452 * Our external IP address/port mapping has changed.
2454 * @param cls closure, the 'struct LocalAddrList'
2455 * @param add_remove #GNUNET_YES to mean the new public IP address, #GNUNET_NO to mean
2456 * the previous (now invalid) one
2457 * @param addr either the previous or the new public IP address
2458 * @param addrlen actual lenght of the address
2461 server_nat_port_map_callback (void *cls,
2463 const struct sockaddr *addr,
2466 struct HTTP_Server_Plugin *plugin = cls;
2468 LOG (GNUNET_ERROR_TYPE_DEBUG,
2469 "NAT called to %s address `%s'\n",
2470 (add_remove == GNUNET_NO) ? "remove" : "add",
2471 GNUNET_a2s (addr, addrlen));
2473 if (AF_INET == addr->sa_family)
2475 struct sockaddr_in *s4 = (struct sockaddr_in *) addr;
2477 if (GNUNET_NO == plugin->use_ipv4)
2480 if ((NULL != plugin->server_addr_v4) &&
2481 (0 != memcmp (&plugin->server_addr_v4->sin_addr,
2482 &s4->sin_addr, sizeof (struct in_addr))))
2484 LOG (GNUNET_ERROR_TYPE_DEBUG,
2485 "Skipping address `%s' (not bindto address)\n",
2486 GNUNET_a2s (addr, addrlen));
2491 if (AF_INET6 == addr->sa_family)
2493 struct sockaddr_in6 *s6 = (struct sockaddr_in6 *) addr;
2494 if (GNUNET_NO == plugin->use_ipv6)
2497 if ((NULL != plugin->server_addr_v6) &&
2498 (0 != memcmp (&plugin->server_addr_v6->sin6_addr,
2499 &s6->sin6_addr, sizeof (struct in6_addr))))
2501 LOG (GNUNET_ERROR_TYPE_DEBUG,
2502 "Skipping address `%s' (not bindto address)\n",
2503 GNUNET_a2s (addr, addrlen));
2511 server_add_address (cls, add_remove, addr, addrlen);
2514 server_remove_address (cls, add_remove, addr, addrlen);
2521 * Get valid server addresses
2523 * @param plugin the plugin handle
2524 * @param service_name the servicename
2525 * @param cfg configuration handle
2526 * @param addrs addresses
2527 * @param addr_lens address length
2528 * @return number of addresses
2531 server_get_addresses (struct HTTP_Server_Plugin *plugin,
2532 const char *service_name,
2533 const struct GNUNET_CONFIGURATION_Handle *cfg,
2534 struct sockaddr ***addrs,
2535 socklen_t ** addr_lens)
2538 unsigned long long port;
2539 struct addrinfo hints;
2540 struct addrinfo *res;
2541 struct addrinfo *pos;
2542 struct addrinfo *next;
2546 struct sockaddr **saddrs;
2547 socklen_t *saddrlens;
2553 disablev6 = !plugin->use_ipv6;
2556 if (GNUNET_CONFIGURATION_have_value (cfg, service_name, "PORT"))
2558 GNUNET_break (GNUNET_OK ==
2559 GNUNET_CONFIGURATION_get_value_number (cfg, service_name,
2563 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2564 _("Require valid port number for service in configuration!\n"));
2565 return GNUNET_SYSERR;
2570 LOG (GNUNET_ERROR_TYPE_INFO,
2571 "Starting in listen only mode\n");
2572 return -1; /* listen only */
2576 if (GNUNET_CONFIGURATION_have_value (cfg, service_name,
2579 GNUNET_break (GNUNET_OK ==
2580 GNUNET_CONFIGURATION_get_value_string (cfg, service_name,
2581 "BINDTO", &hostname));
2586 if (NULL != hostname)
2588 LOG (GNUNET_ERROR_TYPE_DEBUG,
2589 "Resolving `%s' since that is where `%s' will bind to.\n",
2590 hostname, service_name);
2591 memset (&hints, 0, sizeof (struct addrinfo));
2593 hints.ai_family = AF_INET;
2594 if ((0 != (ret = getaddrinfo (hostname, NULL, &hints, &res))) ||
2597 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2598 _("Failed to resolve `%s': %s\n"),
2600 gai_strerror (ret));
2601 GNUNET_free (hostname);
2602 return GNUNET_SYSERR;
2606 while (NULL != (pos = next))
2608 next = pos->ai_next;
2609 if ((disablev6) && (pos->ai_family == AF_INET6))
2615 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2616 _("Failed to find %saddress for `%s'.\n"),
2617 disablev6 ? "IPv4 " : "", hostname);
2619 GNUNET_free (hostname);
2620 return GNUNET_SYSERR;
2623 saddrs = GNUNET_malloc ((resi + 1) * sizeof (struct sockaddr *));
2624 saddrlens = GNUNET_malloc ((resi + 1) * sizeof (socklen_t));
2627 while (NULL != (pos = next))
2629 next = pos->ai_next;
2630 if ((disablev6) && (pos->ai_family == AF_INET6))
2632 if ((pos->ai_protocol != IPPROTO_TCP) && (0 != pos->ai_protocol))
2633 continue; /* not TCP */
2634 if ((pos->ai_socktype != SOCK_STREAM) && (0 != pos->ai_socktype))
2635 continue; /* huh? */
2636 LOG (GNUNET_ERROR_TYPE_DEBUG,
2637 "Service will bind to `%s'\n",
2638 GNUNET_a2s (pos->ai_addr,
2640 if (pos->ai_family == AF_INET)
2642 GNUNET_assert (pos->ai_addrlen == sizeof (struct sockaddr_in));
2643 saddrlens[i] = pos->ai_addrlen;
2644 saddrs[i] = GNUNET_malloc (saddrlens[i]);
2645 memcpy (saddrs[i], pos->ai_addr, saddrlens[i]);
2646 ((struct sockaddr_in *) saddrs[i])->sin_port = htons (port);
2650 GNUNET_assert (pos->ai_family == AF_INET6);
2651 GNUNET_assert (pos->ai_addrlen == sizeof (struct sockaddr_in6));
2652 saddrlens[i] = pos->ai_addrlen;
2653 saddrs[i] = GNUNET_malloc (saddrlens[i]);
2654 memcpy (saddrs[i], pos->ai_addr, saddrlens[i]);
2655 ((struct sockaddr_in6 *) saddrs[i])->sin6_port = htons (port);
2659 GNUNET_free (hostname);
2665 /* will bind against everything, just set port */
2671 saddrs = GNUNET_malloc ((resi + 1) * sizeof (struct sockaddr *));
2672 saddrlens = GNUNET_malloc ((resi + 1) * sizeof (socklen_t));
2674 saddrlens[i] = sizeof (struct sockaddr_in);
2675 saddrs[i] = GNUNET_malloc (saddrlens[i]);
2676 #if HAVE_SOCKADDR_IN_SIN_LEN
2677 ((struct sockaddr_in *) saddrs[i])->sin_len = saddrlens[i];
2679 ((struct sockaddr_in *) saddrs[i])->sin_family = AF_INET;
2680 ((struct sockaddr_in *) saddrs[i])->sin_port = htons (port);
2686 saddrs = GNUNET_malloc ((resi + 1) * sizeof (struct sockaddr *));
2687 saddrlens = GNUNET_malloc ((resi + 1) * sizeof (socklen_t));
2689 saddrlens[i] = sizeof (struct sockaddr_in6);
2690 saddrs[i] = GNUNET_malloc (saddrlens[i]);
2691 #if HAVE_SOCKADDR_IN_SIN_LEN
2692 ((struct sockaddr_in6 *) saddrs[i])->sin6_len = saddrlens[0];
2694 ((struct sockaddr_in6 *) saddrs[i])->sin6_family = AF_INET6;
2695 ((struct sockaddr_in6 *) saddrs[i])->sin6_port = htons (port);
2697 saddrlens[i] = sizeof (struct sockaddr_in);
2698 saddrs[i] = GNUNET_malloc (saddrlens[i]);
2699 #if HAVE_SOCKADDR_IN_SIN_LEN
2700 ((struct sockaddr_in *) saddrs[i])->sin_len = saddrlens[1];
2702 ((struct sockaddr_in *) saddrs[i])->sin_family = AF_INET;
2703 ((struct sockaddr_in *) saddrs[i])->sin_port = htons (port);
2707 *addr_lens = saddrlens;
2713 * Ask NAT for addresses
2715 * @param plugin the plugin handle
2718 server_start_report_addresses (struct HTTP_Server_Plugin *plugin)
2720 int res = GNUNET_OK;
2721 struct sockaddr **addrs;
2722 socklen_t *addrlens;
2724 res = server_get_addresses (plugin,
2728 LOG (GNUNET_ERROR_TYPE_DEBUG,
2729 _("Found %u addresses to report to NAT service\n"),
2732 if (GNUNET_SYSERR == res)
2739 GNUNET_NAT_register (plugin->env->cfg,
2743 (const struct sockaddr **) addrs, addrlens,
2744 &server_nat_port_map_callback, NULL, plugin);
2748 GNUNET_assert (NULL != addrs[res]);
2749 GNUNET_free (addrs[res]);
2751 GNUNET_free_non_null (addrs);
2752 GNUNET_free_non_null (addrlens);
2757 * Stop NAT for addresses
2759 * @param plugin the plugin handle
2762 server_stop_report_addresses (struct HTTP_Server_Plugin *plugin)
2764 struct HttpAddressWrapper *w;
2766 /* Stop NAT handle */
2767 if (NULL != plugin->nat)
2769 GNUNET_NAT_unregister (plugin->nat);
2772 /* Clean up addresses */
2773 while (NULL != plugin->addr_head)
2775 w = plugin->addr_head;
2776 GNUNET_CONTAINER_DLL_remove (plugin->addr_head,
2779 GNUNET_free (w->address);
2786 * Check if IPv6 supported on this system
2788 * @param plugin the plugin handle
2789 * @return #GNUNET_YES on success, else #GNUNET_NO
2792 server_check_ipv6_support (struct HTTP_Server_Plugin *plugin)
2794 struct GNUNET_NETWORK_Handle *desc = NULL;
2795 int res = GNUNET_NO;
2797 /* Probe IPv6 support */
2798 desc = GNUNET_NETWORK_socket_create (PF_INET6, SOCK_STREAM, 0);
2801 if ((errno == ENOBUFS) || (errno == ENOMEM) || (errno == ENFILE) ||
2804 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "socket");
2806 LOG (GNUNET_ERROR_TYPE_WARNING,
2807 _("Disabling IPv6 since it is not supported on this system!\n"));
2812 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (desc));
2816 LOG (GNUNET_ERROR_TYPE_DEBUG,
2817 "Testing IPv6 on this system: %s\n",
2818 (res == GNUNET_YES) ? "successful" : "failed");
2824 * Notify server about our external hostname
2827 * @param tc task context (unused)
2830 server_notify_external_hostname (void *cls,
2831 const struct GNUNET_SCHEDULER_TaskContext *tc)
2833 struct HTTP_Server_Plugin *plugin = cls;
2834 struct HttpAddress *ext_addr;
2835 size_t ext_addr_len;
2839 plugin->notify_ext_task = GNUNET_SCHEDULER_NO_TASK;
2840 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
2843 GNUNET_asprintf(&url,
2846 plugin->external_hostname);
2848 urlen = strlen (url) + 1;
2849 ext_addr = GNUNET_malloc (sizeof (struct HttpAddress) + urlen);
2850 ext_addr->options = htonl(plugin->options);
2851 ext_addr->urlen = htonl (urlen);
2852 ext_addr_len = sizeof (struct HttpAddress) + urlen;
2853 memcpy (&ext_addr[1], url, urlen);
2856 LOG (GNUNET_ERROR_TYPE_DEBUG,
2857 "Notifying transport about external hostname address `%s'\n",
2858 plugin->external_hostname);
2861 if (GNUNET_YES == plugin->verify_external_hostname)
2862 LOG (GNUNET_ERROR_TYPE_INFO,
2863 "Enabling SSL verification for external hostname address `%s'\n",
2864 plugin->external_hostname);
2865 plugin->ext_addr = GNUNET_HELLO_address_allocate (plugin->env->my_identity,
2866 "https_client", ext_addr, ext_addr_len, GNUNET_HELLO_ADDRESS_INFO_NONE );
2867 plugin->env->notify_address (plugin->env->cls, GNUNET_YES, plugin->ext_addr);
2868 GNUNET_free (ext_addr);
2870 plugin->ext_addr = GNUNET_HELLO_address_allocate (plugin->env->my_identity,
2871 "http_client", ext_addr, ext_addr_len, GNUNET_HELLO_ADDRESS_INFO_NONE );
2872 plugin->env->notify_address (plugin->env->cls, GNUNET_YES, plugin->ext_addr);
2873 GNUNET_free (ext_addr);
2879 * Configure the plugin
2881 * @param plugin plugin handle
2882 * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
2885 server_configure_plugin (struct HTTP_Server_Plugin *plugin)
2887 unsigned long long port;
2888 unsigned long long max_connections;
2889 char *bind4_address = NULL;
2890 char *bind6_address = NULL;
2891 char *eh_tmp = NULL;
2892 int external_hostname_use_port;
2895 if (GNUNET_CONFIGURATION_have_value
2896 (plugin->env->cfg, plugin->name, "USE_IPv4"))
2899 GNUNET_CONFIGURATION_get_value_yesno (plugin->env->cfg,
2904 plugin->use_ipv4 = GNUNET_YES;
2905 LOG (GNUNET_ERROR_TYPE_DEBUG,
2906 _("IPv4 support is %s\n"),
2907 (plugin->use_ipv4 == GNUNET_YES) ? "enabled" : "disabled");
2910 if (GNUNET_CONFIGURATION_have_value
2911 (plugin->env->cfg, plugin->name, "USE_IPv6"))
2914 GNUNET_CONFIGURATION_get_value_yesno (plugin->env->cfg,
2919 plugin->use_ipv6 = GNUNET_YES;
2920 LOG (GNUNET_ERROR_TYPE_DEBUG,
2921 _("IPv6 support is %s\n"),
2922 (plugin->use_ipv6 == GNUNET_YES) ? "enabled" : "disabled");
2924 if ((plugin->use_ipv4 == GNUNET_NO) && (plugin->use_ipv6 == GNUNET_NO))
2926 LOG (GNUNET_ERROR_TYPE_ERROR,
2927 _("Neither IPv4 nor IPv6 are enabled! Fix in configuration\n"));
2928 return GNUNET_SYSERR;
2931 /* Reading port number from config file */
2933 GNUNET_CONFIGURATION_get_value_number (plugin->env->cfg,
2935 "PORT", &port)) || (port > 65535))
2937 LOG (GNUNET_ERROR_TYPE_ERROR,
2938 _("Port is required! Fix in configuration\n"));
2939 return GNUNET_SYSERR;
2941 plugin->port = port;
2943 LOG (GNUNET_ERROR_TYPE_INFO,
2944 _("Using port %u\n"), plugin->port);
2946 if ((plugin->use_ipv4 == GNUNET_YES) &&
2947 (GNUNET_YES == GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg,
2948 plugin->name, "BINDTO", &bind4_address)))
2950 LOG (GNUNET_ERROR_TYPE_DEBUG,
2951 "Binding %s plugin to specific IPv4 address: `%s'\n",
2952 plugin->protocol, bind4_address);
2953 plugin->server_addr_v4 = GNUNET_new (struct sockaddr_in);
2954 if (1 != inet_pton (AF_INET, bind4_address,
2955 &plugin->server_addr_v4->sin_addr))
2957 LOG (GNUNET_ERROR_TYPE_ERROR,
2958 _("Specific IPv4 address `%s' in configuration file is invalid!\n"),
2960 GNUNET_free (bind4_address);
2961 GNUNET_free (plugin->server_addr_v4);
2962 plugin->server_addr_v4 = NULL;
2963 return GNUNET_SYSERR;
2967 LOG (GNUNET_ERROR_TYPE_DEBUG,
2968 _("Binding to IPv4 address %s\n"),
2970 plugin->server_addr_v4->sin_family = AF_INET;
2971 plugin->server_addr_v4->sin_port = htons (plugin->port);
2973 GNUNET_free (bind4_address);
2976 if ((plugin->use_ipv6 == GNUNET_YES) &&
2978 GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg,
2980 "BINDTO6", &bind6_address)))
2982 LOG (GNUNET_ERROR_TYPE_DEBUG,
2983 "Binding %s plugin to specific IPv6 address: `%s'\n",
2984 plugin->protocol, bind6_address);
2985 plugin->server_addr_v6 = GNUNET_new (struct sockaddr_in6);
2987 inet_pton (AF_INET6, bind6_address, &plugin->server_addr_v6->sin6_addr))
2989 LOG (GNUNET_ERROR_TYPE_ERROR,
2990 _("Specific IPv6 address `%s' in configuration file is invalid!\n"),
2992 GNUNET_free (bind6_address);
2993 GNUNET_free (plugin->server_addr_v6);
2994 plugin->server_addr_v6 = NULL;
2995 return GNUNET_SYSERR;
2999 LOG (GNUNET_ERROR_TYPE_DEBUG,
3000 _("Binding to IPv6 address %s\n"),
3002 plugin->server_addr_v6->sin6_family = AF_INET6;
3003 plugin->server_addr_v6->sin6_port = htons (plugin->port);
3005 GNUNET_free (bind6_address);
3008 plugin->verify_external_hostname = GNUNET_NO;
3010 plugin->verify_external_hostname = GNUNET_CONFIGURATION_get_value_yesno (plugin->env->cfg,
3012 "VERIFY_EXTERNAL_HOSTNAME");
3013 if (GNUNET_SYSERR == plugin->verify_external_hostname)
3014 plugin->verify_external_hostname = GNUNET_NO;
3015 if (GNUNET_YES == plugin->verify_external_hostname)
3016 plugin->options |= HTTP_OPTIONS_VERIFY_CERTIFICATE;
3018 external_hostname_use_port = GNUNET_CONFIGURATION_get_value_yesno (plugin->env->cfg,
3020 "EXTERNAL_HOSTNAME_USE_PORT");
3021 if (GNUNET_SYSERR == external_hostname_use_port)
3022 external_hostname_use_port = GNUNET_NO;
3026 GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg,
3028 "EXTERNAL_HOSTNAME",
3033 char *pos_url = NULL;
3035 if (NULL != strstr(eh_tmp, "://"))
3036 tmp = &strstr(eh_tmp, "://")[3];
3040 if (GNUNET_YES == external_hostname_use_port)
3042 if ( (strlen (tmp) > 1) && (NULL != (pos = strchr(tmp, '/'))) )
3046 GNUNET_asprintf (&plugin->external_hostname,
3050 (NULL == pos_url) ? "" : pos_url);
3053 GNUNET_asprintf (&plugin->external_hostname,
3059 plugin->external_hostname = GNUNET_strdup (tmp);
3060 GNUNET_free (eh_tmp);
3062 LOG (GNUNET_ERROR_TYPE_INFO,
3063 _("Using external hostname `%s'\n"),
3064 plugin->external_hostname);
3065 plugin->notify_ext_task = GNUNET_SCHEDULER_add_now (&server_notify_external_hostname,
3068 /* Use only configured external hostname */
3069 if (GNUNET_CONFIGURATION_have_value
3072 "EXTERNAL_HOSTNAME_ONLY"))
3074 plugin->external_only =
3075 GNUNET_CONFIGURATION_get_value_yesno (plugin->env->cfg,
3077 "EXTERNAL_HOSTNAME_ONLY");
3080 plugin->external_only = GNUNET_NO;
3082 if (GNUNET_YES == plugin->external_only)
3083 LOG (GNUNET_ERROR_TYPE_DEBUG,
3084 _("Notifying transport only about hostname `%s'\n"),
3085 plugin->external_hostname);
3088 LOG (GNUNET_ERROR_TYPE_DEBUG,
3089 "No external hostname configured\n");
3091 /* Optional parameters */
3093 GNUNET_CONFIGURATION_get_value_number (plugin->env->cfg,
3097 max_connections = 128;
3098 plugin->max_connections = max_connections;
3100 LOG (GNUNET_ERROR_TYPE_DEBUG,
3101 _("Maximum number of connections is %u\n"),
3102 plugin->max_connections);
3104 plugin->peer_id_length = strlen (GNUNET_i2s_full (plugin->env->my_identity));
3111 * Exit point from the plugin.
3117 LIBGNUNET_PLUGIN_TRANSPORT_DONE (void *cls)
3119 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
3120 struct HTTP_Server_Plugin *plugin = api->cls;
3122 if (NULL == api->cls)
3124 /* Free for stub mode */
3128 plugin->in_shutdown = GNUNET_YES;
3129 LOG (GNUNET_ERROR_TYPE_INFO,
3130 _("Shutting down plugin `%s'\n"),
3133 if (GNUNET_SCHEDULER_NO_TASK != plugin->notify_ext_task)
3135 GNUNET_SCHEDULER_cancel (plugin->notify_ext_task);
3136 plugin->notify_ext_task = GNUNET_SCHEDULER_NO_TASK;
3139 if (NULL != plugin->ext_addr)
3141 LOG (GNUNET_ERROR_TYPE_DEBUG,
3142 "Notifying transport to remove address `%s'\n",
3143 http_common_plugin_address_to_string (plugin->protocol,
3144 plugin->ext_addr->address,
3145 plugin->ext_addr->address_length));
3147 plugin->env->notify_address (plugin->env->cls,
3151 plugin->env->notify_address (plugin->env->cls,
3155 GNUNET_HELLO_address_free (plugin->ext_addr);
3156 plugin->ext_addr = NULL;
3159 /* Stop to report addresses to transport service */
3160 server_stop_report_addresses (plugin);
3161 if (NULL != plugin->server_v4)
3163 MHD_stop_daemon (plugin->server_v4);
3164 plugin->server_v4 = NULL;
3166 if (NULL != plugin->server_v6)
3168 MHD_stop_daemon (plugin->server_v6);
3169 plugin->server_v6 = NULL;
3171 if (GNUNET_SCHEDULER_NO_TASK != plugin->server_v4_task)
3173 GNUNET_SCHEDULER_cancel (plugin->server_v4_task);
3174 plugin->server_v4_task = GNUNET_SCHEDULER_NO_TASK;
3177 if (GNUNET_SCHEDULER_NO_TASK != plugin->server_v6_task)
3179 GNUNET_SCHEDULER_cancel (plugin->server_v6_task);
3180 plugin->server_v6_task = GNUNET_SCHEDULER_NO_TASK;
3183 GNUNET_free_non_null (plugin->crypto_init);
3184 GNUNET_free_non_null (plugin->cert);
3185 GNUNET_free_non_null (plugin->key);
3187 GNUNET_CONTAINER_multipeermap_iterate (plugin->sessions,
3188 &destroy_session_shutdown_cb,
3190 GNUNET_CONTAINER_multipeermap_destroy (plugin->sessions);
3191 plugin->sessions = NULL;
3193 GNUNET_free_non_null (plugin->external_hostname);
3194 GNUNET_free_non_null (plugin->ext_addr);
3195 GNUNET_free_non_null (plugin->server_addr_v4);
3196 GNUNET_free_non_null (plugin->server_addr_v6);
3197 regfree (&plugin->url_regex);
3199 LOG (GNUNET_ERROR_TYPE_DEBUG,
3200 _("Shutdown for plugin `%s' complete\n"),
3203 GNUNET_free (plugin);
3210 * Function called by the pretty printer for the resolved address for
3211 * each human-readable address obtained. The callback can be called
3212 * several times. The last invocation must be with a @a address of
3213 * NULL and a @a res of #GNUNET_OK. Thus, to indicate conversion
3214 * errors, the callback might be called first with @a address NULL and
3215 * @a res being #GNUNET_SYSERR. In that case, there must still be a
3216 * subsequent call later with @a address NULL and @a res #GNUNET_OK.
3218 * @param cls closure
3219 * @param address one of the names for the host, NULL on last callback
3220 * @param res #GNUNET_OK if conversion was successful, #GNUNET_SYSERR on failure,
3221 * #GNUNET_OK on last callback
3224 http_server_plugin_address_to_string (void *cls,
3228 return http_common_plugin_address_to_string (PLUGIN_NAME,
3235 * Function obtain the network type for a session
3237 * @param cls closure ('struct HTTP_Server_Plugin*')
3238 * @param session the session
3239 * @return the network type in HBO or #GNUNET_SYSERR
3241 static enum GNUNET_ATS_Network_Type
3242 http_server_plugin_get_network (void *cls,
3243 struct Session *session)
3245 return ntohl (session->ats_address_network_type);
3250 * Function that will be called whenever the transport service wants to
3251 * notify the plugin that the inbound quota changed and that the plugin
3252 * should update it's delay for the next receive value
3254 * @param cls closure
3255 * @param peer which peer was the session for
3256 * @param session which session is being updated
3257 * @param delay new delay to use for receiving
3260 http_server_plugin_update_inbound_delay (void *cls,
3261 const struct GNUNET_PeerIdentity *peer,
3263 struct GNUNET_TIME_Relative delay)
3265 s->next_receive = GNUNET_TIME_relative_to_absolute (delay);
3266 LOG (GNUNET_ERROR_TYPE_DEBUG,
3267 "New inbound delay %s\n",
3268 GNUNET_STRINGS_relative_time_to_string (delay,
3270 if (GNUNET_SCHEDULER_NO_TASK != s->recv_wakeup_task)
3272 GNUNET_SCHEDULER_cancel (s->recv_wakeup_task);
3274 = GNUNET_SCHEDULER_add_delayed (delay,
3282 * Return information about the given session to the
3285 * @param cls the `struct Plugin` with the monitor callback (`sic`)
3286 * @param peer peer we send information about
3287 * @param value our `struct Session` to send information about
3288 * @return #GNUNET_OK (continue to iterate)
3291 send_session_info_iter (void *cls,
3292 const struct GNUNET_PeerIdentity *peer,
3295 struct HTTP_Server_Plugin *plugin = cls;
3296 struct Session *session = value;
3298 notify_session_monitor (plugin,
3300 GNUNET_TRANSPORT_SS_UP);
3306 * Begin monitoring sessions of a plugin. There can only
3307 * be one active monitor per plugin (i.e. if there are
3308 * multiple monitors, the transport service needs to
3309 * multiplex the generated events over all of them).
3311 * @param cls closure of the plugin
3312 * @param sic callback to invoke, NULL to disable monitor;
3313 * plugin will being by iterating over all active
3314 * sessions immediately and then enter monitor mode
3315 * @param sic_cls closure for @a sic
3318 http_server_plugin_setup_monitor (void *cls,
3319 GNUNET_TRANSPORT_SessionInfoCallback sic,
3322 struct HTTP_Server_Plugin *plugin = cls;
3325 plugin->sic_cls = sic_cls;
3328 GNUNET_CONTAINER_multipeermap_iterate (plugin->sessions,
3329 &send_session_info_iter,
3331 /* signal end of first iteration */
3332 sic (sic_cls, NULL, NULL);
3338 * Entry point for the plugin.
3344 LIBGNUNET_PLUGIN_TRANSPORT_INIT (void *cls)
3346 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
3347 struct GNUNET_TRANSPORT_PluginFunctions *api;
3348 struct HTTP_Server_Plugin *plugin;
3350 if (NULL == env->receive)
3352 /* run in 'stub' mode (i.e. as part of gnunet-peerinfo), don't fully
3353 initialze the plugin or the API */
3354 api = GNUNET_new (struct GNUNET_TRANSPORT_PluginFunctions);
3356 api->address_to_string = &http_server_plugin_address_to_string;
3357 api->string_to_address = &http_common_plugin_string_to_address;
3358 api->address_pretty_printer = &http_common_plugin_address_pretty_printer;
3361 plugin = GNUNET_new (struct HTTP_Server_Plugin);
3363 plugin->sessions = GNUNET_CONTAINER_multipeermap_create (128,
3365 api = GNUNET_new (struct GNUNET_TRANSPORT_PluginFunctions);
3367 api->send = &http_server_plugin_send;
3368 api->disconnect_peer = &http_server_plugin_disconnect_peer;
3369 api->disconnect_session = &http_server_plugin_disconnect_session;
3370 api->query_keepalive_factor = &http_server_query_keepalive_factor;
3371 api->check_address = &http_server_plugin_address_suggested;
3372 api->get_session = &http_server_plugin_get_session;
3374 api->address_to_string = &http_server_plugin_address_to_string;
3375 api->string_to_address = &http_common_plugin_string_to_address;
3376 api->address_pretty_printer = &http_common_plugin_address_pretty_printer;
3377 api->get_network = &http_server_plugin_get_network;
3378 api->update_session_timeout = &http_server_plugin_update_session_timeout;
3379 api->update_inbound_delay = &http_server_plugin_update_inbound_delay;
3380 api->setup_monitor = &http_server_plugin_setup_monitor;
3382 plugin->name = "transport-https_server";
3383 plugin->protocol = "https";
3385 plugin->name = "transport-http_server";
3386 plugin->protocol = "http";
3389 /* Compile URL regex */
3390 if (regcomp(&plugin->url_regex,
3394 LOG (GNUNET_ERROR_TYPE_ERROR,
3395 _("Unable to compile URL regex\n"));
3396 LIBGNUNET_PLUGIN_TRANSPORT_DONE (api);
3400 /* Configure plugin */
3401 if (GNUNET_SYSERR == server_configure_plugin (plugin))
3403 LIBGNUNET_PLUGIN_TRANSPORT_DONE (api);
3407 /* Check IPv6 support */
3408 if (GNUNET_YES == plugin->use_ipv6)
3409 plugin->use_ipv6 = server_check_ipv6_support (plugin);
3411 /* Report addresses to transport service */
3412 if (GNUNET_NO == plugin->external_only)
3413 server_start_report_addresses (plugin);
3415 if (GNUNET_SYSERR == server_start (plugin))
3417 LIBGNUNET_PLUGIN_TRANSPORT_DONE (api);
3423 /* end of plugin_transport_http_server.c */