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
28 #include "gnunet_util_lib.h"
29 #include "gnunet_statistics_service.h"
30 #include "gnunet_transport_plugin.h"
31 #include "gnunet_nat_lib.h"
32 #include "plugin_transport_http_common.h"
33 #include <microhttpd.h>
39 #define PLUGIN_NAME "https_server"
40 #define LIBGNUNET_PLUGIN_TRANSPORT_INIT libgnunet_plugin_transport_https_server_init
41 #define LIBGNUNET_PLUGIN_TRANSPORT_DONE libgnunet_plugin_transport_https_server_done
43 #define PLUGIN_NAME "http_server"
44 #define LIBGNUNET_PLUGIN_TRANSPORT_INIT libgnunet_plugin_transport_http_server_init
45 #define LIBGNUNET_PLUGIN_TRANSPORT_DONE libgnunet_plugin_transport_http_server_done
48 #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>"
53 #define LOG(kind,...) GNUNET_log_from (kind, "transport-" PLUGIN_NAME,__VA_ARGS__)
57 * Information we keep with MHD for an HTTP request.
59 struct ServerConnection
62 * The session this server connection belongs to
64 struct Session *session;
69 struct MHD_Connection *mhd_conn;
74 struct MHD_Daemon *mhd_daemon;
77 * Options requested by peer
80 #define OPTION_LONG_POLL 1 /* GET request wants long-poll semantics */
88 * For PUT connections: Is this the first or last callback with size 0
89 * For GET connections: Have we sent a message
97 * Wrapper to manage addresses
99 struct HttpAddressWrapper
104 struct HttpAddressWrapper *next;
107 * Linked list previous
109 struct HttpAddressWrapper *prev;
112 * An address we are using.
114 struct HttpAddress *address;
117 * Length of the address.
124 * Message to send using http
129 * next pointer for double linked list
131 struct HTTP_Message *next;
134 * previous pointer for double linked list
136 struct HTTP_Message *prev;
139 * buffer containing data to send
144 * amount of data already sent
154 * HTTP/S specific overhead
159 * Continuation function to call once the transmission buffer
160 * has again space available. NULL if there is no
161 * continuation to call.
163 GNUNET_TRANSPORT_TransmitContinuation transmit_cont;
166 * Closure for transmit_cont.
168 void *transmit_cont_cls;
173 * Session handle for connections.
179 * To whom are we talking to (set to our identity
180 * if we are still waiting for the welcome message)
182 struct GNUNET_PeerIdentity target;
185 * Pointer to the global plugin struct.
187 struct HTTP_Server_Plugin *plugin;
190 * next pointer for double linked list
192 struct HTTP_Message *msg_head;
195 * previous pointer for double linked list
197 struct HTTP_Message *msg_tail;
200 * Message stream tokenizer for incoming data
202 struct GNUNET_SERVER_MessageStreamTokenizer *msg_tk;
207 struct ServerConnection *server_recv;
212 struct ServerConnection *server_send;
217 struct GNUNET_HELLO_Address *address;
220 * Absolute time when to receive data again
221 * Used for receive throttling
223 struct GNUNET_TIME_Absolute next_receive;
226 * Session timeout task
228 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
231 * Unique HTTP/S connection tag for this connection
236 * ATS network type in NBO
238 uint32_t ats_address_network_type;
241 * #GNUNET_YES if this session is known to the service.
243 int known_to_service;
249 * Encapsulation of all of the state of the plugin.
251 struct HTTP_Server_Plugin
256 struct GNUNET_TRANSPORT_PluginEnvironment *env;
259 * Hash map of open sessions.
261 struct GNUNET_CONTAINER_MultiPeerMap *sessions;
264 * Function to call about session status changes.
266 GNUNET_TRANSPORT_SessionInfoCallback sic;
269 * Closure for @e sic.
286 char *external_hostname;
289 * External hostname the plugin can be connected to, can be different to
290 * the host's FQDN, used e.g. for reverse proxying
292 struct GNUNET_HELLO_Address *ext_addr;
295 * NAT handle & address management
297 struct GNUNET_NAT_Handle *nat;
299 * IPv4 addresses DLL head
301 struct HttpAddressWrapper *addr_head;
304 * IPv4 addresses DLL tail
306 struct HttpAddressWrapper *addr_tail;
309 * IPv4 server socket to bind to
311 struct sockaddr_in *server_addr_v4;
314 * IPv6 server socket to bind to
316 struct sockaddr_in6 *server_addr_v6;
321 struct MHD_Daemon *server_v4;
326 struct MHD_Daemon *server_v6;
334 * Use RC4-128 instead of AES:
335 * NONE:+VERS-TLS1.0:+ARCFOUR-128:+SHA1:+RSA:+COMP-NULL
354 GNUNET_SCHEDULER_TaskIdentifier server_v4_task;
359 GNUNET_SCHEDULER_TaskIdentifier server_v6_task;
362 * Task calling transport service about external address
364 GNUNET_SCHEDULER_TaskIdentifier notify_ext_task;
367 * Notify transport only about external address
369 unsigned int external_only;
372 * The IPv4 server is scheduled to run asap
374 int server_v4_immediately;
377 * The IPv6 server is scheduled to run asap
379 int server_v6_immediately;
382 * Verify external address
384 int verify_external_hostname;
387 * Maximum number of sockets the plugin can use
388 * Each http inbound /outbound connections are two connections
390 unsigned int max_connections;
393 * Current number of sockets the plugin can use
394 * Each http inbound /outbound connections are two connections
396 unsigned int cur_connections;
399 * Did we immediately end the session in disconnect_cb
409 * My options to be included in the address
429 * Regex for parsing URLs. FIXME: this seems overkill.
437 * If a session monitor is attached, notify it about the new
440 * @param plugin our plugin
441 * @param session session that changed state
442 * @param state new state of the session
445 notify_session_monitor (struct HTTP_Server_Plugin *plugin,
446 struct Session *session,
447 enum GNUNET_TRANSPORT_SessionState state)
449 struct GNUNET_TRANSPORT_SessionInfo info;
451 if (NULL == plugin->sic)
453 memset (&info, 0, sizeof (info));
455 info.is_inbound = GNUNET_YES;
456 // info.num_msg_pending = session->msgs_in_queue; // FIXME
457 // info.num_bytes_pending = session->bytes_in_queue; // FIXME
458 // info.receive_delay = session->next_receive; // FIXME
459 // info.session_timeout = session->timeout; // FIXME
460 info.address = session->address;
461 plugin->sic (plugin->sic_cls,
468 * Reschedule the execution of both IPv4 and IPv6 server.
470 * @param plugin the plugin
471 * @param server which server to schedule v4 or v6?
472 * @param now #GNUNET_YES to schedule execution immediately, #GNUNET_NO to wait
476 server_reschedule (struct HTTP_Server_Plugin *plugin,
477 struct MHD_Daemon *server,
482 * Deletes the session. Must not be used afterwards.
484 * @param s the session to delete
487 server_delete_session (struct Session *s)
489 struct HTTP_Server_Plugin *plugin = s->plugin;
490 struct HTTP_Message *msg;
491 struct HTTP_Message *tmp;
492 struct ServerConnection *send;
493 struct ServerConnection *recv;
495 if (GNUNET_SCHEDULER_NO_TASK != s->timeout_task)
497 GNUNET_SCHEDULER_cancel (s->timeout_task);
498 s->timeout_task = GNUNET_SCHEDULER_NO_TASK;
500 GNUNET_assert (GNUNET_OK ==
501 GNUNET_CONTAINER_multipeermap_remove (plugin->sessions,
508 GNUNET_CONTAINER_DLL_remove (s->msg_head,
511 if (NULL != msg->transmit_cont)
512 msg->transmit_cont (msg->transmit_cont_cls,
516 msg->pos + msg->overhead);
520 send = s->server_send;
523 LOG (GNUNET_ERROR_TYPE_DEBUG,
524 "Server: %p / %p Terminating inbound PUT session to peer `%s'\n",
526 GNUNET_i2s (&s->target));
527 send->session = NULL;
528 MHD_set_connection_option (send->mhd_conn,
529 MHD_CONNECTION_OPTION_TIMEOUT,
530 1 /* 0 = no timeout, so this is MIN */);
531 server_reschedule (plugin,
535 recv = s->server_recv;
538 LOG (GNUNET_ERROR_TYPE_DEBUG,
539 "Server: %p / %p Terminating inbound GET session to peer `%s'\n",
540 s, recv, GNUNET_i2s (&s->target));
541 recv->session = NULL;
542 MHD_set_connection_option (recv->mhd_conn,
543 MHD_CONNECTION_OPTION_TIMEOUT,
544 1 /* 0 = no timeout, so this is MIN */);
545 server_reschedule (plugin,
549 if (GNUNET_YES == s->known_to_service)
550 plugin->env->session_end (plugin->env->cls,
553 if (NULL != s->msg_tk)
555 GNUNET_SERVER_mst_destroy (s->msg_tk);
558 GNUNET_HELLO_address_free (s->address);
559 LOG (GNUNET_ERROR_TYPE_DEBUG,
560 "Session %p destroyed\n",
567 * Disconnect session @a s by telling MHD to close the
568 * connections (reducing timeout, etc.).
570 * @param cls closure with the `struct HTTP_Server_Plugin`
571 * @param s the session
572 * @return #GNUNET_OK on success
575 http_server_plugin_disconnect_session (void *cls,
578 server_delete_session (s);
584 * Session was idle, so disconnect it
586 * @param cls the session
587 * @param tc task context
590 server_session_timeout (void *cls,
591 const struct GNUNET_SCHEDULER_TaskContext *tc)
593 struct Session *s = cls;
595 s->timeout_task = GNUNET_SCHEDULER_NO_TASK;
596 GNUNET_log (TIMEOUT_LOG,
597 "Session %p was idle for %s, disconnecting\n",
599 GNUNET_STRINGS_relative_time_to_string (HTTP_SERVER_SESSION_TIMEOUT,
601 server_delete_session (s);
606 * Increment session timeout due to activity session @a s
608 * @param s the session
611 server_reschedule_session_timeout (struct Session *s)
613 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK != s->timeout_task);
614 GNUNET_SCHEDULER_cancel (s->timeout_task);
615 s->timeout_task = GNUNET_SCHEDULER_add_delayed (HTTP_SERVER_SESSION_TIMEOUT,
616 &server_session_timeout,
618 GNUNET_log (TIMEOUT_LOG,
619 "Timeout rescheduled for session %p set to %s\n",
621 GNUNET_STRINGS_relative_time_to_string (HTTP_SERVER_SESSION_TIMEOUT,
627 * Function that can be used by the transport service to transmit
628 * a message using the plugin. Note that in the case of a
629 * peer disconnecting, the continuation MUST be called
630 * prior to the disconnect notification itself. This function
631 * will be called with this peer's HELLO message to initiate
632 * a fresh connection to another peer.
635 * @param session which session must be used
636 * @param msgbuf the message to transmit
637 * @param msgbuf_size number of bytes in @a msgbuf
638 * @param priority how important is the message (most plugins will
639 * ignore message priority and just FIFO)
640 * @param to how long to wait at most for the transmission (does not
641 * require plugins to discard the message after the timeout,
642 * just advisory for the desired delay; most plugins will ignore
644 * @param cont continuation to call once the message has
645 * been transmitted (or if the transport is ready
646 * for the next transmission call; or if the
647 * peer disconnected...); can be NULL
648 * @param cont_cls closure for @a cont
649 * @return number of bytes used (on the physical network, with overheads);
650 * -1 on hard errors (i.e. address invalid); 0 is a legal value
651 * and does NOT mean that the message was not transmitted (DV)
654 http_server_plugin_send (void *cls,
655 struct Session *session,
658 unsigned int priority,
659 struct GNUNET_TIME_Relative to,
660 GNUNET_TRANSPORT_TransmitContinuation cont,
663 struct HTTP_Server_Plugin *plugin = cls;
664 struct HTTP_Message *msg;
665 ssize_t bytes_sent = 0;
668 LOG (GNUNET_ERROR_TYPE_DEBUG,
669 "Session %p/connection %p: Sending message with %u to peer `%s'\n",
671 session->server_send,
673 GNUNET_i2s (&session->target));
675 /* create new message and schedule */
676 bytes_sent = sizeof (struct HTTP_Message) + msgbuf_size;
677 msg = GNUNET_malloc (bytes_sent);
679 msg->size = msgbuf_size;
681 msg->buf = (char *) &msg[1];
682 msg->transmit_cont = cont;
683 msg->transmit_cont_cls = cont_cls;
684 memcpy (msg->buf, msgbuf, msgbuf_size);
685 GNUNET_CONTAINER_DLL_insert_tail (session->msg_head,
688 GNUNET_asprintf (&stat_txt,
689 "# bytes currently in %s_server buffers",
691 GNUNET_STATISTICS_update (plugin->env->stats,
692 stat_txt, msgbuf_size, GNUNET_NO);
693 GNUNET_free (stat_txt);
695 if (NULL != session->server_send)
696 server_reschedule (session->plugin,
697 session->server_send->mhd_daemon,
706 * @param cls the `struct HTTP_Server_Plugin *`
707 * @param peer for which this is a session
708 * @param value the `struct Session` to clean up
709 * @return #GNUNET_OK (continue to iterate)
712 destroy_session_cb (void *cls,
713 const struct GNUNET_PeerIdentity *peer,
716 struct Session *s = value;
718 server_delete_session (s);
724 * Function that can be used to force the plugin to disconnect
725 * from the given peer and cancel all previous transmissions
726 * (and their continuationc).
729 * @param target peer from which to disconnect
732 http_server_plugin_disconnect_peer (void *cls,
733 const struct GNUNET_PeerIdentity *target)
735 struct HTTP_Server_Plugin *plugin = cls;
737 LOG (GNUNET_ERROR_TYPE_DEBUG,
738 "Transport tells me to disconnect `%s'\n",
739 GNUNET_i2s (target));
740 GNUNET_CONTAINER_multipeermap_get_multiple (plugin->sessions,
748 * Another peer has suggested an address for this
749 * peer and transport plugin. Check that this could be a valid
750 * address. If so, consider adding it to the list
754 * @param addr pointer to the address
755 * @param addrlen length of @a addr
756 * @return #GNUNET_OK if this is a plausible address for this peer
760 http_server_plugin_address_suggested (void *cls,
764 struct HTTP_Server_Plugin *plugin = cls;
765 struct HttpAddressWrapper *next;
766 struct HttpAddressWrapper *pos;
767 const struct HttpAddress *haddr = addr;
769 if ((NULL != plugin->ext_addr) &&
770 GNUNET_YES == (http_common_cmp_addresses (addr, addrlen,
771 plugin->ext_addr->address,
772 plugin->ext_addr->address_length)))
774 /* Checking HTTP_OPTIONS_VERIFY_CERTIFICATE option for external hostname */
775 if ((ntohl (haddr->options) & HTTP_OPTIONS_VERIFY_CERTIFICATE) !=
776 (plugin->options & HTTP_OPTIONS_VERIFY_CERTIFICATE))
777 return GNUNET_NO; /* VERIFY option not set as required! */
780 next = plugin->addr_head;
781 while (NULL != (pos = next))
784 if (GNUNET_YES == (http_common_cmp_addresses(addr,
795 * Creates a new outbound session the transport
796 * service will use to send data to the peer.
798 * Since HTTP/S server cannot create sessions, always returns NULL.
800 * @param cls the plugin
801 * @param address the address
802 * @return always NULL
804 static struct Session *
805 http_server_plugin_get_session (void *cls,
806 const struct GNUNET_HELLO_Address *address)
813 * Call MHD IPv4 to process pending requests and then go back
814 * and schedule the next run.
816 * @param cls plugin as closure
817 * @param tc task context
820 server_v4_run (void *cls,
821 const struct GNUNET_SCHEDULER_TaskContext *tc)
823 struct HTTP_Server_Plugin *plugin = cls;
825 plugin->server_v4_task = GNUNET_SCHEDULER_NO_TASK;
826 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
828 plugin->server_v4_immediately = GNUNET_NO;
829 GNUNET_assert (MHD_YES == MHD_run (plugin->server_v4));
830 server_reschedule (plugin, plugin->server_v4, GNUNET_NO);
835 * Call MHD IPv6 to process pending requests and then go back
836 * and schedule the next run.
838 * @param cls plugin as closure
839 * @param tc task context
842 server_v6_run (void *cls,
843 const struct GNUNET_SCHEDULER_TaskContext *tc)
845 struct HTTP_Server_Plugin *plugin = cls;
847 plugin->server_v6_task = GNUNET_SCHEDULER_NO_TASK;
848 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
850 plugin->server_v6_immediately = GNUNET_NO;
851 GNUNET_assert (MHD_YES == MHD_run (plugin->server_v6));
852 server_reschedule (plugin, plugin->server_v6, GNUNET_NO);
857 * Function that queries MHD's select sets and
858 * starts the task waiting for them.
860 * @param plugin plugin
861 * @param daemon_handle the MHD daemon handle
862 * @return gnunet task identifier
864 static GNUNET_SCHEDULER_TaskIdentifier
865 server_schedule (struct HTTP_Server_Plugin *plugin,
866 struct MHD_Daemon *daemon_handle,
869 GNUNET_SCHEDULER_TaskIdentifier ret;
873 struct GNUNET_NETWORK_FDSet *wrs;
874 struct GNUNET_NETWORK_FDSet *wws;
876 MHD_UNSIGNED_LONG_LONG timeout;
877 static unsigned long long last_timeout = 0;
879 struct GNUNET_TIME_Relative tv;
881 if (GNUNET_YES == plugin->in_shutdown)
882 return GNUNET_SCHEDULER_NO_TASK;
884 ret = GNUNET_SCHEDULER_NO_TASK;
888 wrs = GNUNET_NETWORK_fdset_create ();
889 wws = GNUNET_NETWORK_fdset_create ();
891 GNUNET_assert (MHD_YES ==
892 MHD_get_fdset (daemon_handle,
897 haveto = MHD_get_timeout (daemon_handle, &timeout);
898 if (haveto == MHD_YES)
900 if (timeout != last_timeout)
902 LOG (GNUNET_ERROR_TYPE_DEBUG,
903 "SELECT Timeout changed from %llu to %llu (ms)\n",
904 last_timeout, timeout);
905 last_timeout = timeout;
907 if (timeout <= GNUNET_TIME_UNIT_SECONDS.rel_value_us / 1000LL)
908 tv.rel_value_us = (uint64_t) timeout * 1000LL;
910 tv = GNUNET_TIME_UNIT_SECONDS;
913 tv = GNUNET_TIME_UNIT_SECONDS;
914 /* Force immediate run, since we have outbound data to send */
915 if (now == GNUNET_YES)
916 tv = GNUNET_TIME_UNIT_MILLISECONDS;
917 GNUNET_NETWORK_fdset_copy_native (wrs, &rs, max + 1);
918 GNUNET_NETWORK_fdset_copy_native (wws, &ws, max + 1);
920 if (daemon_handle == plugin->server_v4)
922 if (plugin->server_v4_task != GNUNET_SCHEDULER_NO_TASK)
924 GNUNET_SCHEDULER_cancel (plugin->server_v4_task);
925 plugin->server_v4_task = GNUNET_SCHEDULER_NO_TASK;
928 LOG (GNUNET_ERROR_TYPE_DEBUG,
929 "Scheduling IPv4 server task in %llu ms\n",
933 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
935 &server_v4_run, plugin);
937 if (daemon_handle == plugin->server_v6)
939 if (plugin->server_v6_task != GNUNET_SCHEDULER_NO_TASK)
941 GNUNET_SCHEDULER_cancel (plugin->server_v6_task);
942 plugin->server_v6_task = GNUNET_SCHEDULER_NO_TASK;
945 LOG (GNUNET_ERROR_TYPE_DEBUG,
946 "Scheduling IPv6 server task in %llu ms\n", tv);
949 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
951 &server_v6_run, plugin);
953 GNUNET_NETWORK_fdset_destroy (wrs);
954 GNUNET_NETWORK_fdset_destroy (wws);
960 * Reschedule the execution of both IPv4 and IPv6 server
962 * @param plugin the plugin
963 * @param server which server to schedule v4 or v6?
964 * @param now #GNUNET_YES to schedule execution immediately, #GNUNET_NO to wait
968 server_reschedule (struct HTTP_Server_Plugin *plugin,
969 struct MHD_Daemon *server,
972 if ((server == plugin->server_v4) && (plugin->server_v4 != NULL))
974 if (GNUNET_YES == plugin->server_v4_immediately)
975 return; /* No rescheduling, server will run asap */
977 if (GNUNET_YES == now)
978 plugin->server_v4_immediately = GNUNET_YES;
980 if (plugin->server_v4_task != GNUNET_SCHEDULER_NO_TASK)
982 GNUNET_SCHEDULER_cancel (plugin->server_v4_task);
983 plugin->server_v4_task = GNUNET_SCHEDULER_NO_TASK;
985 plugin->server_v4_task = server_schedule (plugin, plugin->server_v4, now);
988 if ((server == plugin->server_v6) && (plugin->server_v6 != NULL))
990 if (GNUNET_YES == plugin->server_v6_immediately)
991 return; /* No rescheduling, server will run asap */
993 if (GNUNET_YES == now)
994 plugin->server_v6_immediately = GNUNET_YES;
996 if (plugin->server_v6_task != GNUNET_SCHEDULER_NO_TASK)
998 GNUNET_SCHEDULER_cancel (plugin->server_v6_task);
999 plugin->server_v6_task = GNUNET_SCHEDULER_NO_TASK;
1001 plugin->server_v6_task = server_schedule (plugin, plugin->server_v6, now);
1007 * Function that is called to get the keepalive factor.
1008 * GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT is divided by this number to
1009 * calculate the interval between keepalive packets.
1011 * @param cls closure with the `struct HTTP_Server_Plugin`
1012 * @return keepalive factor
1015 http_server_query_keepalive_factor (void *cls)
1022 http_server_plugin_update_session_timeout (void *cls,
1023 const struct GNUNET_PeerIdentity *peer,
1024 struct Session *session)
1026 server_reschedule_session_timeout (session);
1031 * Tell MHD that the connection should timeout after @a to seconds.
1033 * @param plugin our plugin
1034 * @param s session for which the timeout changes
1035 * @param to timeout in seconds
1038 server_mhd_connection_timeout (struct HTTP_Server_Plugin *plugin,
1042 /* Setting timeouts for other connections */
1043 if (NULL != s->server_recv)
1045 LOG (GNUNET_ERROR_TYPE_DEBUG,
1046 "Setting timeout for %p to %u sec.\n",
1047 s->server_recv, to);
1048 MHD_set_connection_option (s->server_recv->mhd_conn,
1049 MHD_CONNECTION_OPTION_TIMEOUT,
1051 server_reschedule (plugin, s->server_recv->mhd_daemon, GNUNET_NO);
1053 if (NULL != s->server_send)
1055 LOG (GNUNET_ERROR_TYPE_DEBUG,
1056 "Setting timeout for %p to %u sec.\n",
1057 s->server_send, to);
1058 MHD_set_connection_option (s->server_send->mhd_conn,
1059 MHD_CONNECTION_OPTION_TIMEOUT,
1061 server_reschedule (plugin, s->server_send->mhd_daemon, GNUNET_NO);
1067 * Parse incoming URL for tag and target
1069 * @param plugin plugin
1070 * @param url incoming url
1071 * @param target where to store the target
1072 * @param tag where to store the tag
1073 * @param options where to store the options
1074 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
1077 server_parse_url (struct HTTP_Server_Plugin *plugin,
1079 struct GNUNET_PeerIdentity *target,
1083 regmatch_t matches[4];
1084 const char *tag_start;
1085 const char *target_start;
1089 unsigned long int rc;
1093 ("^.*/([0-9A-Z]+);([0-9]+)(,[0-9]+)?$")
1098 return GNUNET_SYSERR;
1101 if (regexec(&plugin->url_regex, url, 4, matches, 0))
1103 LOG (GNUNET_ERROR_TYPE_DEBUG,
1104 "URL `%s' did not match regex\n", url);
1105 return GNUNET_SYSERR;
1108 target_start = &url[matches[1].rm_so];
1109 tag_start = &url[matches[2].rm_so];
1112 rc = strtoul (tag_start, &tag_end, 10);
1113 if (&url[matches[2].rm_eo] != tag_end)
1115 LOG (GNUNET_ERROR_TYPE_DEBUG,
1116 "URL tag did not line up with submatch\n");
1117 return GNUNET_SYSERR;
1121 LOG (GNUNET_ERROR_TYPE_DEBUG,
1122 "URL tag is zero\n");
1123 return GNUNET_SYSERR;
1125 if ((rc == ULONG_MAX) && (ERANGE == errno))
1127 LOG (GNUNET_ERROR_TYPE_DEBUG,
1128 "URL tag > ULONG_MAX\n");
1129 return GNUNET_SYSERR;
1131 if (rc > UINT32_MAX)
1133 LOG (GNUNET_ERROR_TYPE_DEBUG,
1134 "URL tag > UINT32_MAX\n");
1135 return GNUNET_SYSERR;
1137 (*tag) = (uint32_t)rc;
1138 LOG (GNUNET_ERROR_TYPE_DEBUG,
1139 "Found tag `%u' in url\n",
1142 /* convert peer id */
1143 hash_length = matches[1].rm_eo - matches[1].rm_so;
1144 if (hash_length != plugin->peer_id_length)
1146 LOG (GNUNET_ERROR_TYPE_DEBUG,
1147 "URL target is %u bytes, expecting %u\n",
1148 hash_length, plugin->peer_id_length);
1149 return GNUNET_SYSERR;
1152 GNUNET_CRYPTO_eddsa_public_key_from_string (target_start,
1154 &target->public_key))
1156 LOG (GNUNET_ERROR_TYPE_DEBUG,
1157 "URL target conversion failed\n");
1158 return GNUNET_SYSERR;
1160 LOG (GNUNET_ERROR_TYPE_DEBUG,
1161 "Found target `%s' in URL\n",
1162 GNUNET_i2s_full (target));
1164 /* convert options */
1165 if (-1 == matches[3].rm_so) {
1168 rc = strtoul (&url[matches[3].rm_so + 1], &options_end, 10);
1169 if (&url[matches[3].rm_eo] != options_end)
1171 LOG (GNUNET_ERROR_TYPE_DEBUG,
1172 "URL options did not line up with submatch\n");
1173 return GNUNET_SYSERR;
1175 if ((rc == ULONG_MAX) && (ERANGE == errno))
1177 LOG (GNUNET_ERROR_TYPE_DEBUG,
1178 "URL options > ULONG_MAX\n");
1179 return GNUNET_SYSERR;
1181 if (rc > UINT32_MAX)
1183 LOG (GNUNET_ERROR_TYPE_DEBUG,
1184 "URL options > UINT32_MAX\n");
1185 return GNUNET_SYSERR;
1187 (*options) = (uint32_t)rc;
1188 LOG (GNUNET_ERROR_TYPE_DEBUG,
1189 "Found options `%u' in url\n", *options);
1196 * Closure for #session_tag_it().
1198 struct SessionTagContext
1201 * Set to session matching the tag.
1203 struct Session *res;
1206 * Tag we are looking for.
1213 * Find a session with a matching tag.
1215 * @param cls the `struct SessionTagContext *`
1216 * @param key peer identity (unused)
1217 * @param value the `struct Session *`
1218 * @return #GNUNET_NO if we found the session, #GNUNET_OK if not
1221 session_tag_it (void *cls,
1222 const struct GNUNET_PeerIdentity *key,
1225 struct SessionTagContext *stc = cls;
1226 struct Session *s = value;
1228 if (s->tag == stc->tag)
1238 * Lookup a mhd connection and create one if none is found
1240 * @param plugin the plugin handle
1241 * @param mhd_connection the incoming mhd_connection
1242 * @param url incoming requested URL
1243 * @param method PUT or GET
1244 * @return the server connecetion
1246 static struct ServerConnection *
1247 server_lookup_connection (struct HTTP_Server_Plugin *plugin,
1248 struct MHD_Connection *mhd_connection,
1252 struct Session *s = NULL;
1253 struct ServerConnection *sc = NULL;
1254 const union MHD_ConnectionInfo *conn_info;
1255 struct HttpAddress *addr;
1256 struct GNUNET_ATS_Information ats;
1257 struct GNUNET_PeerIdentity target;
1259 struct SessionTagContext stc;
1261 int direction = GNUNET_SYSERR;
1264 conn_info = MHD_get_connection_info (mhd_connection,
1265 MHD_CONNECTION_INFO_CLIENT_ADDRESS);
1266 if ((conn_info->client_addr->sa_family != AF_INET) &&
1267 (conn_info->client_addr->sa_family != AF_INET6))
1269 LOG (GNUNET_ERROR_TYPE_DEBUG,
1270 "New %s connection from %s\n",
1274 if (GNUNET_SYSERR ==
1275 server_parse_url (plugin, url, &target, &stc.tag, &options))
1277 LOG (GNUNET_ERROR_TYPE_DEBUG,
1278 "Invalid url %s\n", url);
1281 if (0 == strcmp (MHD_HTTP_METHOD_PUT, method))
1282 direction = _RECEIVE;
1283 else if (0 == strcmp (MHD_HTTP_METHOD_GET, method))
1287 LOG (GNUNET_ERROR_TYPE_DEBUG,
1288 "Invalid method %s connection from %s\n",
1293 plugin->cur_connections++;
1294 LOG (GNUNET_ERROR_TYPE_DEBUG,
1295 "New %s connection from %s with tag %u (%u of %u)\n",
1297 GNUNET_i2s (&target),
1299 plugin->cur_connections, plugin->max_connections);
1300 /* find existing session */
1302 GNUNET_CONTAINER_multipeermap_get_multiple (plugin->sessions,
1306 if (NULL == (s = stc.res))
1308 /* create new session */
1310 switch (conn_info->client_addr->sa_family)
1313 addr = http_common_address_from_socket (plugin->protocol,
1314 conn_info->client_addr,
1315 sizeof (struct sockaddr_in));
1316 addr_len = http_common_address_get_size (addr);
1317 ats = plugin->env->get_address_type (plugin->env->cls,
1318 conn_info->client_addr,
1319 sizeof (struct sockaddr_in));
1322 addr = http_common_address_from_socket (plugin->protocol,
1323 conn_info->client_addr,
1324 sizeof (struct sockaddr_in6));
1325 addr_len = http_common_address_get_size (addr);
1326 ats = plugin->env->get_address_type (plugin->env->cls,
1327 conn_info->client_addr,
1328 sizeof (struct sockaddr_in6));
1331 /* external host name */
1332 ats.type = htonl (GNUNET_ATS_NETWORK_TYPE);
1333 ats.type = htonl (GNUNET_ATS_NET_WAN);
1336 s = GNUNET_new (struct Session);
1339 s->address = GNUNET_HELLO_address_allocate (&s->target,
1343 GNUNET_HELLO_ADDRESS_INFO_INBOUND);
1344 s->ats_address_network_type = ats.value;
1345 s->next_receive = GNUNET_TIME_UNIT_ZERO_ABS;
1347 s->timeout_task = GNUNET_SCHEDULER_add_delayed (HTTP_SERVER_SESSION_TIMEOUT,
1348 &server_session_timeout,
1350 (void) GNUNET_CONTAINER_multipeermap_put (plugin->sessions,
1353 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
1355 LOG (GNUNET_ERROR_TYPE_DEBUG,
1356 "Creating new session %p for peer `%s' connecting from `%s'\n",
1357 s, GNUNET_i2s (&target),
1358 http_common_plugin_address_to_string (plugin->protocol,
1361 GNUNET_free_non_null (addr);
1364 if ( (_RECEIVE == direction) && (NULL != s->server_recv) )
1366 LOG (GNUNET_ERROR_TYPE_DEBUG,
1367 "Duplicate PUT connection from `%s' tag %u, dismissing new connection\n",
1368 GNUNET_i2s (&target),
1372 if ((_SEND == direction) && (NULL != s->server_send))
1374 LOG (GNUNET_ERROR_TYPE_DEBUG,
1375 "Duplicate GET connection from `%s' tag %u, dismissing new connection\n",
1376 GNUNET_i2s (&target),
1380 sc = GNUNET_new (struct ServerConnection);
1381 if (conn_info->client_addr->sa_family == AF_INET)
1382 sc->mhd_daemon = plugin->server_v4;
1383 if (conn_info->client_addr->sa_family == AF_INET6)
1384 sc->mhd_daemon = plugin->server_v6;
1385 sc->mhd_conn = mhd_connection;
1386 sc->direction = direction;
1387 sc->connected = GNUNET_NO;
1389 sc->options = options;
1390 if (direction == _SEND)
1391 s->server_send = sc;
1392 if (direction == _RECEIVE)
1393 s->server_recv = sc;
1395 if ((NULL != s->server_send) && (NULL != s->server_recv))
1397 s->known_to_service = GNUNET_YES;
1398 plugin->env->session_start (NULL, s->address ,s, NULL, 0);
1401 if ((NULL == s->server_recv) || (NULL == s->server_send))
1403 to = (HTTP_SERVER_NOT_VALIDATED_TIMEOUT.rel_value_us / 1000LL / 1000LL);
1404 MHD_set_connection_option (mhd_connection,
1405 MHD_CONNECTION_OPTION_TIMEOUT, to);
1406 server_reschedule (plugin, sc->mhd_daemon, GNUNET_NO);
1410 LOG (GNUNET_ERROR_TYPE_DEBUG,
1411 "Session %p for peer `%s' fully connected\n",
1412 s, GNUNET_i2s (&target));
1413 to = (HTTP_SERVER_SESSION_TIMEOUT.rel_value_us / 1000LL / 1000LL);
1414 server_mhd_connection_timeout (plugin, s, to);
1416 LOG (GNUNET_ERROR_TYPE_DEBUG,
1417 "Setting timeout for %p to %u sec.\n", sc, to);
1423 * Callback called by MHD when it needs data to send
1425 * @param cls current session
1426 * @param pos position in buffer
1427 * @param buf the buffer to write data to
1428 * @param max max number of bytes available in buffer
1429 * @return bytes written to buffer
1432 server_send_callback (void *cls,
1437 struct Session *s = cls;
1438 struct ServerConnection *sc;
1439 ssize_t bytes_read = 0;
1440 struct HTTP_Message *msg;
1443 sc = s->server_send;
1450 bytes_read = GNUNET_MIN (msg->size - msg->pos,
1452 memcpy (buf, &msg->buf[msg->pos], bytes_read);
1453 msg->pos += bytes_read;
1455 /* removing message */
1456 if (msg->pos == msg->size)
1458 GNUNET_CONTAINER_DLL_remove (s->msg_head,
1461 if (NULL != msg->transmit_cont)
1462 msg->transmit_cont (msg->transmit_cont_cls, &s->target, GNUNET_OK,
1463 msg->size, msg->size + msg->overhead);
1469 sc->connected = GNUNET_YES;
1470 LOG (GNUNET_ERROR_TYPE_DEBUG,
1471 "Sent %u bytes to peer `%s' with session %p \n",
1472 bytes_read, GNUNET_i2s (&s->target), s);
1473 GNUNET_asprintf (&stat_txt, "# bytes currently in %s_server buffers",
1474 s->plugin->protocol);
1475 GNUNET_STATISTICS_update (s->plugin->env->stats,
1476 stat_txt, -bytes_read, GNUNET_NO);
1477 GNUNET_free (stat_txt);
1478 GNUNET_asprintf (&stat_txt, "# bytes transmitted via %s_server",
1479 s->plugin->protocol);
1480 GNUNET_STATISTICS_update (s->plugin->env->stats,
1481 stat_txt, bytes_read, GNUNET_NO);
1482 GNUNET_free (stat_txt);
1484 else if ((sc->options & OPTION_LONG_POLL) && sc->connected)
1486 LOG (GNUNET_ERROR_TYPE_DEBUG,
1487 "Completing GET response to peer `%s' with session %p \n",
1488 GNUNET_i2s (&s->target), s);
1489 return MHD_CONTENT_READER_END_OF_STREAM;
1496 * Callback called by MessageStreamTokenizer when a message has arrived
1498 * @param cls current session as closure
1499 * @param client client
1500 * @param message the message to be forwarded to transport service
1501 * @return #GNUNET_OK
1504 server_receive_mst_cb (void *cls,
1506 const struct GNUNET_MessageHeader *message)
1508 struct Session *s = cls;
1509 struct HTTP_Server_Plugin *plugin = s->plugin;
1510 struct GNUNET_ATS_Information atsi;
1511 struct GNUNET_TIME_Relative delay;
1514 atsi.type = htonl (GNUNET_ATS_NETWORK_TYPE);
1515 atsi.value = s->ats_address_network_type;
1516 GNUNET_break (s->ats_address_network_type !=
1517 ntohl (GNUNET_ATS_NET_UNSPECIFIED));
1519 if (GNUNET_NO == s->known_to_service)
1521 s->known_to_service = GNUNET_YES;
1522 plugin->env->session_start (NULL, s->address, s, NULL, 0);
1524 delay = plugin->env->receive (plugin->env->cls,
1528 plugin->env->update_address_metrics (plugin->env->cls,
1531 GNUNET_asprintf (&stat_txt,
1532 "# bytes received via %s_server",
1534 GNUNET_STATISTICS_update (plugin->env->stats,
1535 stat_txt, ntohs (message->size), GNUNET_NO);
1536 GNUNET_free (stat_txt);
1537 s->next_receive = GNUNET_TIME_relative_to_absolute (delay);
1538 if (delay.rel_value_us > 0)
1540 LOG (GNUNET_ERROR_TYPE_DEBUG,
1541 "Peer `%s' address `%s' next read delayed for %s\n",
1542 GNUNET_i2s (&s->target),
1543 http_common_plugin_address_to_string (plugin->protocol,
1544 s->address->address,
1545 s->address->address_length),
1546 GNUNET_STRINGS_relative_time_to_string (delay,
1549 server_reschedule_session_timeout (s);
1555 * Add headers to a request indicating that we allow Cross-Origin Resource
1559 add_cors_headers(struct MHD_Response *response)
1561 MHD_add_response_header (response,
1562 "Access-Control-Allow-Origin",
1564 MHD_add_response_header (response,
1565 "Access-Control-Allow-Methods",
1566 "GET, PUT, OPTIONS");
1567 MHD_add_response_header (response,
1568 "Access-Control-Max-Age",
1574 * MHD callback for a new incoming connection
1576 * @param cls the plugin handle
1577 * @param mhd_connection the mhd connection
1578 * @param url the requested URL
1579 * @param method GET or PUT
1580 * @param version HTTP version
1581 * @param upload_data upload data
1582 * @param upload_data_size sizeof upload data
1583 * @param httpSessionCache the session cache to remember the connection
1584 * @return MHD_YES if connection is accepted, MHD_NO on reject
1587 server_access_cb (void *cls,
1588 struct MHD_Connection *mhd_connection,
1591 const char *version,
1592 const char *upload_data,
1593 size_t *upload_data_size,
1594 void **httpSessionCache)
1596 struct HTTP_Server_Plugin *plugin = cls;
1597 struct ServerConnection *sc = *httpSessionCache;
1599 struct MHD_Response *response;
1602 LOG (GNUNET_ERROR_TYPE_DEBUG,
1603 _("Access from connection %p (%u of %u) for `%s' `%s' url `%s' with upload data size %u\n"),
1605 plugin->cur_connections,
1606 plugin->max_connections,
1610 (*upload_data_size));
1613 /* CORS pre-flight request */
1614 if (0 == strcmp (MHD_HTTP_METHOD_OPTIONS, method))
1616 response = MHD_create_response_from_buffer (0, NULL,
1617 MHD_RESPMEM_PERSISTENT);
1618 add_cors_headers(response);
1619 res = MHD_queue_response (mhd_connection, MHD_HTTP_OK, response);
1620 MHD_destroy_response (response);
1623 /* new connection */
1624 sc = server_lookup_connection (plugin, mhd_connection, url, method);
1627 /* attach to new / existing session */
1628 (*httpSessionCache) = sc;
1632 /* existing session already has matching connection, refuse */
1633 response = MHD_create_response_from_data (strlen (HTTP_ERROR_RESPONSE),
1634 HTTP_ERROR_RESPONSE,
1637 MHD_add_response_header (response,
1638 MHD_HTTP_HEADER_CONTENT_TYPE,
1640 add_cors_headers(response);
1641 res = MHD_queue_response (mhd_connection, MHD_HTTP_NOT_FOUND, response);
1642 MHD_destroy_response (response);
1646 /* 'old' connection */
1647 if (NULL == (s = sc->session))
1649 /* Session was already disconnected;
1650 sent HTTP/1.1: 200 OK as response */
1651 response = MHD_create_response_from_data (strlen ("Thank you!"),
1654 add_cors_headers(response);
1655 MHD_queue_response (mhd_connection, MHD_HTTP_OK, response);
1656 MHD_destroy_response (response);
1660 if (sc->direction == _SEND)
1662 response = MHD_create_response_from_callback (MHD_SIZE_UNKNOWN,
1664 &server_send_callback, s,
1666 add_cors_headers(response);
1667 MHD_queue_response (mhd_connection, MHD_HTTP_OK, response);
1668 MHD_destroy_response (response);
1671 if (sc->direction == _RECEIVE)
1673 if ((*upload_data_size == 0) && (sc->connected == GNUNET_NO))
1675 /* (*upload_data_size == 0) first callback when header are passed */
1676 LOG (GNUNET_ERROR_TYPE_DEBUG,
1677 "Session %p / Connection %p: Peer `%s' PUT on address `%s' connected\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 sc->connected = GNUNET_YES;
1686 else if ((*upload_data_size == 0) && (sc->connected == GNUNET_YES))
1688 /* (*upload_data_size == 0) when upload is complete */
1689 LOG (GNUNET_ERROR_TYPE_DEBUG,
1690 "Session %p / Connection %p: Peer `%s' PUT on address `%s' finished upload\n",
1692 GNUNET_i2s (&s->target),
1693 http_common_plugin_address_to_string (plugin->protocol,
1694 s->address->address,
1695 s->address->address_length));
1696 sc->connected = GNUNET_NO;
1697 /* Sent HTTP/1.1: 200 OK as PUT Response\ */
1698 response = MHD_create_response_from_data (strlen ("Thank you!"),
1701 add_cors_headers(response);
1702 MHD_queue_response (mhd_connection, MHD_HTTP_OK, response);
1703 MHD_destroy_response (response);
1706 else if ((*upload_data_size > 0) && (sc->connected == GNUNET_YES))
1708 /* (*upload_data_size > 0) for every segment received */
1709 LOG (GNUNET_ERROR_TYPE_DEBUG,
1710 "Session %p / Connection %p: Peer `%s' PUT on address `%s' received %u bytes\n",
1712 GNUNET_i2s (&s->target),
1713 http_common_plugin_address_to_string (plugin->protocol,
1714 s->address->address,
1715 s->address->address_length),
1717 struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get ();
1719 if ((s->next_receive.abs_value_us <= now.abs_value_us))
1721 LOG (GNUNET_ERROR_TYPE_DEBUG,
1722 "PUT with %u bytes forwarded to MST\n",
1724 if (s->msg_tk == NULL)
1726 s->msg_tk = GNUNET_SERVER_mst_create (&server_receive_mst_cb, s);
1728 GNUNET_SERVER_mst_receive (s->msg_tk, s, upload_data,
1729 *upload_data_size, GNUNET_NO, GNUNET_NO);
1730 server_mhd_connection_timeout (plugin, s,
1731 GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT.rel_value_us / 1000LL / 1000LL);
1732 (*upload_data_size) = 0;
1736 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1737 "Session %p / Connection %p: no inbound bandwidth available! Next read was delayed by %s\n",
1739 GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_duration (s->next_receive),
1755 * Callback from MHD when a connection disconnects
1757 * @param cls closure with the `struct HTTP_Server_Plugin *`
1758 * @param connection the disconnected MHD connection
1759 * @param httpSessionCache the pointer to distinguish
1762 server_disconnect_cb (void *cls,
1763 struct MHD_Connection *connection,
1764 void **httpSessionCache)
1766 struct HTTP_Server_Plugin *plugin = cls;
1767 struct ServerConnection *sc = *httpSessionCache;
1770 LOG (GNUNET_ERROR_TYPE_DEBUG,
1771 "Disconnect for connection %p\n",
1774 return; /* never really got setup */
1775 if (NULL == (s = sc->session))
1776 return; /* session already dead */
1777 if (sc->direction == _SEND)
1779 LOG (GNUNET_ERROR_TYPE_DEBUG,
1780 "Peer `%s' connection %p, GET on address `%s' disconnected\n",
1781 GNUNET_i2s (&s->target),
1783 http_common_plugin_address_to_string (plugin->protocol,
1784 s->address->address,
1785 s->address->address_length));
1786 s->server_send = NULL;
1787 if (! ( (0 != (sc->options & OPTION_LONG_POLL)) &&
1788 (NULL != s->server_recv) ) )
1790 server_delete_session (s);
1792 plugin->cur_connections--;
1796 if (sc->direction == _RECEIVE)
1798 LOG (GNUNET_ERROR_TYPE_DEBUG,
1799 "Peer `%s' connection %p PUT on address `%s' disconnected\n",
1800 GNUNET_i2s (&s->target),
1802 http_common_plugin_address_to_string (plugin->protocol,
1803 s->address->address,
1804 s->address->address_length));
1805 s->server_recv = NULL;
1806 if (NULL != s->msg_tk)
1808 GNUNET_SERVER_mst_destroy (s->msg_tk);
1813 plugin->cur_connections--;
1815 if ( (NULL == s->server_send) &&
1816 (NULL == s->server_recv) )
1818 LOG (GNUNET_ERROR_TYPE_DEBUG,
1819 "Peer `%s' on address `%s' disconnected\n",
1820 GNUNET_i2s (&s->target),
1821 http_common_plugin_address_to_string (plugin->protocol,
1822 s->address->address,
1823 s->address->address_length));
1825 server_delete_session (s);
1831 * Check if incoming connection is accepted.
1833 * @param cls plugin as closure
1834 * @param addr address of incoming connection
1835 * @param addr_len address length of incoming connection
1836 * @return MHD_YES if connection is accepted, MHD_NO if connection is rejected
1839 server_accept_cb (void *cls,
1840 const struct sockaddr *addr,
1843 struct HTTP_Server_Plugin *plugin = cls;
1845 if (plugin->cur_connections <= plugin->max_connections)
1847 LOG (GNUNET_ERROR_TYPE_DEBUG,
1848 _("Accepting connection (%u of %u) from `%s'\n"),
1849 plugin->cur_connections, plugin->max_connections,
1850 GNUNET_a2s (addr, addr_len));
1855 LOG (GNUNET_ERROR_TYPE_WARNING,
1856 _("Server reached maximum number connections (%u), rejecting new connection\n"),
1857 plugin->max_connections);
1864 server_log (void *arg,
1870 vsnprintf (text, sizeof (text), fmt, ap);
1872 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1880 * Load ssl certificate from file
1882 * @param file filename
1883 * @return content of the file
1886 server_load_file (const char *file)
1888 struct GNUNET_DISK_FileHandle *gn_file;
1892 if (GNUNET_OK != GNUNET_DISK_file_size (file,
1893 &fsize, GNUNET_NO, GNUNET_YES))
1895 text = GNUNET_malloc (fsize + 1);
1897 GNUNET_DISK_file_open (file, GNUNET_DISK_OPEN_READ,
1898 GNUNET_DISK_PERM_USER_READ);
1899 if (gn_file == NULL)
1904 if (GNUNET_SYSERR == GNUNET_DISK_file_read (gn_file, text, fsize))
1907 GNUNET_DISK_file_close (gn_file);
1911 GNUNET_DISK_file_close (gn_file);
1919 * Load ssl certificate
1921 * @param plugin the plugin
1922 * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
1925 server_load_certificate (struct HTTP_Server_Plugin *plugin)
1927 int res = GNUNET_OK;
1933 GNUNET_CONFIGURATION_get_value_filename (plugin->env->cfg,
1935 "KEY_FILE", &key_file))
1937 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
1938 plugin->name, "CERT_FILE");
1939 return GNUNET_SYSERR;
1942 GNUNET_CONFIGURATION_get_value_filename (plugin->env->cfg,
1944 "CERT_FILE", &cert_file))
1946 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
1947 plugin->name, "CERT_FILE");
1948 GNUNET_free (key_file);
1949 return GNUNET_SYSERR;
1951 /* Get crypto init string from config. If not present, use
1954 GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg,
1957 &plugin->crypto_init))
1958 LOG (GNUNET_ERROR_TYPE_DEBUG,
1959 "Using crypto init string `%s'\n",
1960 plugin->crypto_init);
1962 LOG (GNUNET_ERROR_TYPE_DEBUG,
1963 "Using default crypto init string \n");
1965 /* read key & certificates from file */
1966 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1967 "Trying to loading TLS certificate from key-file `%s' cert-file`%s'\n",
1968 key_file, cert_file);
1970 plugin->key = server_load_file (key_file);
1971 plugin->cert = server_load_file (cert_file);
1973 if ((plugin->key == NULL) || (plugin->cert == NULL))
1975 struct GNUNET_OS_Process *cert_creation;
1977 GNUNET_free_non_null (plugin->key);
1979 GNUNET_free_non_null (plugin->cert);
1980 plugin->cert = NULL;
1982 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1983 "No usable TLS certificate found, creating certificate\n");
1986 GNUNET_OS_start_process (GNUNET_NO, GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
1988 "gnunet-transport-certificate-creation",
1989 "gnunet-transport-certificate-creation",
1990 key_file, cert_file, NULL);
1991 if (cert_creation == NULL)
1993 LOG (GNUNET_ERROR_TYPE_ERROR,
1994 _("Could not create a new TLS certificate, program `gnunet-transport-certificate-creation' could not be started!\n"));
1995 GNUNET_free (key_file);
1996 GNUNET_free (cert_file);
1998 GNUNET_free_non_null (plugin->key);
2000 GNUNET_free_non_null (plugin->cert);
2001 plugin->cert = NULL;
2002 GNUNET_free_non_null (plugin->crypto_init);
2003 plugin->crypto_init = NULL;
2005 return GNUNET_SYSERR;
2007 GNUNET_assert (GNUNET_OK == GNUNET_OS_process_wait (cert_creation));
2008 GNUNET_OS_process_destroy (cert_creation);
2010 plugin->key = server_load_file (key_file);
2011 plugin->cert = server_load_file (cert_file);
2014 if ((plugin->key == NULL) || (plugin->cert == NULL))
2016 LOG (GNUNET_ERROR_TYPE_ERROR,
2017 _("No usable TLS certificate found and creating one at `%s/%s' failed!\n"),
2018 key_file, cert_file);
2019 GNUNET_free (key_file);
2020 GNUNET_free (cert_file);
2022 GNUNET_free_non_null (plugin->key);
2024 GNUNET_free_non_null (plugin->cert);
2025 plugin->cert = NULL;
2026 GNUNET_free_non_null (plugin->crypto_init);
2027 plugin->crypto_init = NULL;
2029 return GNUNET_SYSERR;
2031 GNUNET_free (key_file);
2032 GNUNET_free (cert_file);
2033 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "TLS certificate loaded\n");
2040 * Start the HTTP server
2042 * @param plugin the plugin handle
2043 * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
2046 server_start (struct HTTP_Server_Plugin *plugin)
2048 unsigned int timeout;
2050 GNUNET_assert (NULL != plugin);
2053 if (GNUNET_SYSERR == server_load_certificate (plugin))
2055 LOG (GNUNET_ERROR_TYPE_ERROR,
2056 "Could not load or create server certificate! Loading plugin failed!\n");
2057 return GNUNET_SYSERR;
2062 #if MHD_VERSION >= 0x00090E00
2063 timeout = HTTP_SERVER_NOT_VALIDATED_TIMEOUT.rel_value_us / 1000LL / 1000LL;
2064 LOG (GNUNET_ERROR_TYPE_DEBUG,
2065 "MHD can set timeout per connection! Default time out %u sec.\n",
2068 timeout = HTTP_SERVER_SESSION_TIMEOUT.rel_value_us / 1000LL / 1000LL;
2069 LOG (GNUNET_ERROR_TYPE_WARNING,
2070 "MHD cannot set timeout per connection! Default time out %u sec.\n",
2074 plugin->server_v4 = NULL;
2075 if (plugin->use_ipv4 == GNUNET_YES)
2077 plugin->server_v4 = MHD_start_daemon (
2084 MHD_NO_FLAG, plugin->port,
2085 &server_accept_cb, plugin,
2086 &server_access_cb, plugin,
2087 MHD_OPTION_SOCK_ADDR,
2088 (struct sockaddr_in *)
2089 plugin->server_addr_v4,
2090 MHD_OPTION_CONNECTION_LIMIT,
2092 plugin->max_connections,
2094 MHD_OPTION_HTTPS_PRIORITIES,
2095 plugin->crypto_init,
2096 MHD_OPTION_HTTPS_MEM_KEY,
2098 MHD_OPTION_HTTPS_MEM_CERT,
2101 MHD_OPTION_CONNECTION_TIMEOUT,
2103 MHD_OPTION_CONNECTION_MEMORY_LIMIT,
2105 GNUNET_SERVER_MAX_MESSAGE_SIZE),
2106 MHD_OPTION_NOTIFY_COMPLETED,
2107 &server_disconnect_cb, plugin,
2108 MHD_OPTION_EXTERNAL_LOGGER,
2109 server_log, NULL, MHD_OPTION_END);
2110 if (plugin->server_v4 == NULL)
2112 LOG (GNUNET_ERROR_TYPE_ERROR,
2113 "Failed to start %s IPv4 server component on port %u\n",
2114 plugin->name, plugin->port);
2117 server_reschedule (plugin, plugin->server_v4, GNUNET_NO);
2121 plugin->server_v6 = NULL;
2122 if (plugin->use_ipv6 == GNUNET_YES)
2124 plugin->server_v6 = MHD_start_daemon (
2131 MHD_USE_IPv6, plugin->port,
2132 &server_accept_cb, plugin,
2133 &server_access_cb, plugin,
2134 MHD_OPTION_SOCK_ADDR,
2135 (struct sockaddr_in6 *)
2136 plugin->server_addr_v6,
2137 MHD_OPTION_CONNECTION_LIMIT,
2139 plugin->max_connections,
2141 MHD_OPTION_HTTPS_PRIORITIES,
2142 plugin->crypto_init,
2143 MHD_OPTION_HTTPS_MEM_KEY,
2145 MHD_OPTION_HTTPS_MEM_CERT,
2148 MHD_OPTION_CONNECTION_TIMEOUT,
2150 MHD_OPTION_CONNECTION_MEMORY_LIMIT,
2152 GNUNET_SERVER_MAX_MESSAGE_SIZE),
2153 MHD_OPTION_NOTIFY_COMPLETED,
2154 &server_disconnect_cb, plugin,
2155 MHD_OPTION_EXTERNAL_LOGGER,
2156 server_log, NULL, MHD_OPTION_END);
2157 if (plugin->server_v6 == NULL)
2159 LOG (GNUNET_ERROR_TYPE_ERROR,
2160 "Failed to start %s IPv6 server component on port %u\n",
2161 plugin->name, plugin->port);
2164 server_reschedule (plugin, plugin->server_v6, GNUNET_NO);
2168 if ((plugin->server_v6 == NULL) && (plugin->server_v4 == NULL))
2170 LOG (GNUNET_ERROR_TYPE_ERROR,
2171 "%s %s server component started on port %u\n",
2172 msg, plugin->name, plugin->port);
2174 return GNUNET_SYSERR;
2176 else if ((plugin->server_v6 != NULL) && (plugin->server_v4 != NULL))
2177 msg = "IPv4 and IPv6";
2178 else if (plugin->server_v6 != NULL)
2180 else if (plugin->server_v4 != NULL)
2182 LOG (GNUNET_ERROR_TYPE_DEBUG,
2183 "%s %s server component started on port %u\n",
2184 msg, plugin->name, plugin->port);
2190 server_stop (struct HTTP_Server_Plugin *plugin)
2192 if (plugin->server_v4 != NULL)
2194 MHD_stop_daemon (plugin->server_v4);
2195 plugin->server_v4 = NULL;
2197 if ( plugin->server_v6 != NULL)
2199 MHD_stop_daemon (plugin->server_v6);
2200 plugin->server_v6 = NULL;
2204 if (plugin->server_v4_task != GNUNET_SCHEDULER_NO_TASK)
2206 GNUNET_SCHEDULER_cancel (plugin->server_v4_task);
2207 plugin->server_v4_task = GNUNET_SCHEDULER_NO_TASK;
2210 if (plugin->server_v6_task != GNUNET_SCHEDULER_NO_TASK)
2212 GNUNET_SCHEDULER_cancel (plugin->server_v6_task);
2213 plugin->server_v6_task = GNUNET_SCHEDULER_NO_TASK;
2216 GNUNET_free_non_null (plugin->crypto_init);
2217 GNUNET_free_non_null (plugin->cert);
2218 GNUNET_free_non_null (plugin->key);
2221 LOG (GNUNET_ERROR_TYPE_DEBUG,
2222 "%s server component stopped\n",
2228 * Add an address to the server's set of addresses and notify transport
2230 * @param cls the plugin handle
2231 * @param add_remove #GNUNET_YES on add, #GNUNET_NO on remove
2232 * @param addr the address
2233 * @param addrlen address length
2236 server_add_address (void *cls,
2238 const struct sockaddr *addr,
2241 struct HTTP_Server_Plugin *plugin = cls;
2242 struct GNUNET_HELLO_Address *address;
2243 struct HttpAddressWrapper *w = NULL;
2245 w = GNUNET_new (struct HttpAddressWrapper);
2246 w->address = http_common_address_from_socket (plugin->protocol,
2249 if (NULL == w->address)
2254 w->addrlen = http_common_address_get_size (w->address);
2256 GNUNET_CONTAINER_DLL_insert (plugin->addr_head,
2259 LOG (GNUNET_ERROR_TYPE_DEBUG,
2260 "Notifying transport to add address `%s'\n",
2261 http_common_plugin_address_to_string (plugin->protocol,
2264 /* modify our published address list */
2266 address = GNUNET_HELLO_address_allocate (plugin->env->my_identity,
2267 "https_client", w->address, w->addrlen, GNUNET_HELLO_ADDRESS_INFO_NONE);
2269 address = GNUNET_HELLO_address_allocate (plugin->env->my_identity,
2270 "http_client", w->address, w->addrlen, GNUNET_HELLO_ADDRESS_INFO_NONE);
2273 plugin->env->notify_address (plugin->env->cls,
2276 GNUNET_HELLO_address_free (address);
2281 * Remove an address from the server's set of addresses and notify transport
2283 * @param cls the plugin handle
2284 * @param add_remove #GNUNET_YES on add, #GNUNET_NO on remove
2285 * @param addr the address
2286 * @param addrlen address length
2289 server_remove_address (void *cls,
2291 const struct sockaddr *addr,
2294 struct HTTP_Server_Plugin *plugin = cls;
2295 struct GNUNET_HELLO_Address *address;
2296 struct HttpAddressWrapper *w = plugin->addr_head;
2300 saddr = http_common_address_from_socket (plugin->protocol,
2305 saddr_len = http_common_address_get_size (saddr);
2310 http_common_cmp_addresses (w->address,
2317 GNUNET_free (saddr);
2322 LOG (GNUNET_ERROR_TYPE_DEBUG,
2323 "Notifying transport to remove address `%s'\n",
2324 http_common_plugin_address_to_string (plugin->protocol,
2327 GNUNET_CONTAINER_DLL_remove (plugin->addr_head,
2330 /* modify our published address list */
2332 address = GNUNET_HELLO_address_allocate (plugin->env->my_identity,
2333 "https_client", w->address, w->addrlen, GNUNET_HELLO_ADDRESS_INFO_NONE);
2335 address = GNUNET_HELLO_address_allocate (plugin->env->my_identity,
2336 "http_client", w->address, w->addrlen, GNUNET_HELLO_ADDRESS_INFO_NONE);
2338 plugin->env->notify_address (plugin->env->cls, add_remove, address);
2339 GNUNET_HELLO_address_free (address);
2340 GNUNET_free (w->address);
2347 * Our external IP address/port mapping has changed.
2349 * @param cls closure, the 'struct LocalAddrList'
2350 * @param add_remove #GNUNET_YES to mean the new public IP address, #GNUNET_NO to mean
2351 * the previous (now invalid) one
2352 * @param addr either the previous or the new public IP address
2353 * @param addrlen actual lenght of the address
2356 server_nat_port_map_callback (void *cls,
2358 const struct sockaddr *addr,
2361 struct HTTP_Server_Plugin *plugin = cls;
2363 LOG (GNUNET_ERROR_TYPE_DEBUG,
2364 "NAT called to %s address `%s'\n",
2365 (add_remove == GNUNET_NO) ? "remove" : "add",
2366 GNUNET_a2s (addr, addrlen));
2368 if (AF_INET == addr->sa_family)
2370 struct sockaddr_in *s4 = (struct sockaddr_in *) addr;
2372 if (GNUNET_NO == plugin->use_ipv4)
2375 if ((NULL != plugin->server_addr_v4) &&
2376 (0 != memcmp (&plugin->server_addr_v4->sin_addr,
2377 &s4->sin_addr, sizeof (struct in_addr))))
2379 LOG (GNUNET_ERROR_TYPE_DEBUG,
2380 "Skipping address `%s' (not bindto address)\n",
2381 GNUNET_a2s (addr, addrlen));
2386 if (AF_INET6 == addr->sa_family)
2388 struct sockaddr_in6 *s6 = (struct sockaddr_in6 *) addr;
2389 if (GNUNET_NO == plugin->use_ipv6)
2392 if ((NULL != plugin->server_addr_v6) &&
2393 (0 != memcmp (&plugin->server_addr_v6->sin6_addr,
2394 &s6->sin6_addr, sizeof (struct in6_addr))))
2396 LOG (GNUNET_ERROR_TYPE_DEBUG,
2397 "Skipping address `%s' (not bindto address)\n",
2398 GNUNET_a2s (addr, addrlen));
2406 server_add_address (cls, add_remove, addr, addrlen);
2409 server_remove_address (cls, add_remove, addr, addrlen);
2416 * Get valid server addresses
2418 * @param plugin the plugin handle
2419 * @param service_name the servicename
2420 * @param cfg configuration handle
2421 * @param addrs addresses
2422 * @param addr_lens address length
2423 * @return number of addresses
2426 server_get_addresses (struct HTTP_Server_Plugin *plugin,
2427 const char *service_name,
2428 const struct GNUNET_CONFIGURATION_Handle *cfg,
2429 struct sockaddr ***addrs,
2430 socklen_t ** addr_lens)
2433 unsigned long long port;
2434 struct addrinfo hints;
2435 struct addrinfo *res;
2436 struct addrinfo *pos;
2437 struct addrinfo *next;
2441 struct sockaddr **saddrs;
2442 socklen_t *saddrlens;
2448 disablev6 = !plugin->use_ipv6;
2451 if (GNUNET_CONFIGURATION_have_value (cfg, service_name, "PORT"))
2453 GNUNET_break (GNUNET_OK ==
2454 GNUNET_CONFIGURATION_get_value_number (cfg, service_name,
2458 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2460 ("Require valid port number for service in configuration!\n"));
2461 return GNUNET_SYSERR;
2466 LOG (GNUNET_ERROR_TYPE_INFO,
2467 "Starting in listen only mode\n");
2468 return -1; /* listen only */
2472 if (GNUNET_CONFIGURATION_have_value (cfg, service_name, "BINDTO"))
2474 GNUNET_break (GNUNET_OK ==
2475 GNUNET_CONFIGURATION_get_value_string (cfg, service_name,
2476 "BINDTO", &hostname));
2481 if (hostname != NULL)
2483 LOG (GNUNET_ERROR_TYPE_DEBUG,
2484 "Resolving `%s' since that is where `%s' will bind to.\n",
2485 hostname, service_name);
2486 memset (&hints, 0, sizeof (struct addrinfo));
2488 hints.ai_family = AF_INET;
2489 if ((0 != (ret = getaddrinfo (hostname, NULL, &hints, &res))) ||
2492 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Failed to resolve `%s': %s\n"),
2493 hostname, gai_strerror (ret));
2494 GNUNET_free (hostname);
2495 return GNUNET_SYSERR;
2499 while (NULL != (pos = next))
2501 next = pos->ai_next;
2502 if ((disablev6) && (pos->ai_family == AF_INET6))
2508 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2509 _("Failed to find %saddress for `%s'.\n"),
2510 disablev6 ? "IPv4 " : "", hostname);
2512 GNUNET_free (hostname);
2513 return GNUNET_SYSERR;
2516 saddrs = GNUNET_malloc ((resi + 1) * sizeof (struct sockaddr *));
2517 saddrlens = GNUNET_malloc ((resi + 1) * sizeof (socklen_t));
2520 while (NULL != (pos = next))
2522 next = pos->ai_next;
2523 if ((disablev6) && (pos->ai_family == AF_INET6))
2525 if ((pos->ai_protocol != IPPROTO_TCP) && (pos->ai_protocol != 0))
2526 continue; /* not TCP */
2527 if ((pos->ai_socktype != SOCK_STREAM) && (pos->ai_socktype != 0))
2528 continue; /* huh? */
2529 LOG (GNUNET_ERROR_TYPE_DEBUG,
2530 "Service will bind to `%s'\n",
2531 GNUNET_a2s (pos->ai_addr,
2533 if (pos->ai_family == AF_INET)
2535 GNUNET_assert (pos->ai_addrlen == sizeof (struct sockaddr_in));
2536 saddrlens[i] = pos->ai_addrlen;
2537 saddrs[i] = GNUNET_malloc (saddrlens[i]);
2538 memcpy (saddrs[i], pos->ai_addr, saddrlens[i]);
2539 ((struct sockaddr_in *) saddrs[i])->sin_port = htons (port);
2543 GNUNET_assert (pos->ai_family == AF_INET6);
2544 GNUNET_assert (pos->ai_addrlen == sizeof (struct sockaddr_in6));
2545 saddrlens[i] = pos->ai_addrlen;
2546 saddrs[i] = GNUNET_malloc (saddrlens[i]);
2547 memcpy (saddrs[i], pos->ai_addr, saddrlens[i]);
2548 ((struct sockaddr_in6 *) saddrs[i])->sin6_port = htons (port);
2552 GNUNET_free (hostname);
2558 /* will bind against everything, just set port */
2564 saddrs = GNUNET_malloc ((resi + 1) * sizeof (struct sockaddr *));
2565 saddrlens = GNUNET_malloc ((resi + 1) * sizeof (socklen_t));
2567 saddrlens[i] = sizeof (struct sockaddr_in);
2568 saddrs[i] = GNUNET_malloc (saddrlens[i]);
2569 #if HAVE_SOCKADDR_IN_SIN_LEN
2570 ((struct sockaddr_in *) saddrs[i])->sin_len = saddrlens[i];
2572 ((struct sockaddr_in *) saddrs[i])->sin_family = AF_INET;
2573 ((struct sockaddr_in *) saddrs[i])->sin_port = htons (port);
2579 saddrs = GNUNET_malloc ((resi + 1) * sizeof (struct sockaddr *));
2580 saddrlens = GNUNET_malloc ((resi + 1) * sizeof (socklen_t));
2582 saddrlens[i] = sizeof (struct sockaddr_in6);
2583 saddrs[i] = GNUNET_malloc (saddrlens[i]);
2584 #if HAVE_SOCKADDR_IN_SIN_LEN
2585 ((struct sockaddr_in6 *) saddrs[i])->sin6_len = saddrlens[0];
2587 ((struct sockaddr_in6 *) saddrs[i])->sin6_family = AF_INET6;
2588 ((struct sockaddr_in6 *) saddrs[i])->sin6_port = htons (port);
2590 saddrlens[i] = sizeof (struct sockaddr_in);
2591 saddrs[i] = GNUNET_malloc (saddrlens[i]);
2592 #if HAVE_SOCKADDR_IN_SIN_LEN
2593 ((struct sockaddr_in *) saddrs[i])->sin_len = saddrlens[1];
2595 ((struct sockaddr_in *) saddrs[i])->sin_family = AF_INET;
2596 ((struct sockaddr_in *) saddrs[i])->sin_port = htons (port);
2600 *addr_lens = saddrlens;
2606 * Ask NAT for addresses
2608 * @param plugin the plugin handle
2611 server_start_report_addresses (struct HTTP_Server_Plugin *plugin)
2613 int res = GNUNET_OK;
2614 struct sockaddr **addrs;
2615 socklen_t *addrlens;
2617 res = server_get_addresses (plugin,
2621 LOG (GNUNET_ERROR_TYPE_DEBUG,
2622 _("Found %u addresses to report to NAT service\n"),
2625 if (GNUNET_SYSERR == res)
2632 GNUNET_NAT_register (plugin->env->cfg,
2636 (const struct sockaddr **) addrs, addrlens,
2637 &server_nat_port_map_callback, NULL, plugin);
2641 GNUNET_assert (addrs[res] != NULL);
2642 GNUNET_free (addrs[res]);
2644 GNUNET_free_non_null (addrs);
2645 GNUNET_free_non_null (addrlens);
2650 * Stop NAT for addresses
2652 * @param plugin the plugin handle
2655 server_stop_report_addresses (struct HTTP_Server_Plugin *plugin)
2657 struct HttpAddressWrapper *w;
2659 /* Stop NAT handle */
2660 if (NULL != plugin->nat)
2662 GNUNET_NAT_unregister (plugin->nat);
2665 /* Clean up addresses */
2666 while (NULL != plugin->addr_head)
2668 w = plugin->addr_head;
2669 GNUNET_CONTAINER_DLL_remove (plugin->addr_head,
2672 GNUNET_free (w->address);
2679 * Check if IPv6 supported on this system
2681 * @param plugin the plugin handle
2682 * @return #GNUNET_YES on success, else #GNUNET_NO
2685 server_check_ipv6_support (struct HTTP_Server_Plugin *plugin)
2687 struct GNUNET_NETWORK_Handle *desc = NULL;
2688 int res = GNUNET_NO;
2690 /* Probe IPv6 support */
2691 desc = GNUNET_NETWORK_socket_create (PF_INET6, SOCK_STREAM, 0);
2694 if ((errno == ENOBUFS) || (errno == ENOMEM) || (errno == ENFILE) ||
2697 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "socket");
2699 LOG (GNUNET_ERROR_TYPE_WARNING,
2700 _("Disabling IPv6 since it is not supported on this system!\n"));
2705 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (desc));
2709 LOG (GNUNET_ERROR_TYPE_DEBUG,
2710 "Testing IPv6 on this system: %s\n",
2711 (res == GNUNET_YES) ? "successful" : "failed");
2717 * Notify server about our external hostname
2720 * @param tc task context (unused)
2723 server_notify_external_hostname (void *cls,
2724 const struct GNUNET_SCHEDULER_TaskContext *tc)
2726 struct HTTP_Server_Plugin *plugin = cls;
2727 struct HttpAddress *ext_addr;
2728 size_t ext_addr_len;
2732 plugin->notify_ext_task = GNUNET_SCHEDULER_NO_TASK;
2733 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
2736 GNUNET_asprintf(&url, "%s://%s", plugin->protocol, plugin->external_hostname);
2738 urlen = strlen (url) + 1;
2739 ext_addr = GNUNET_malloc (sizeof (struct HttpAddress) + urlen);
2740 ext_addr->options = htonl(plugin->options);
2741 ext_addr->urlen = htonl (urlen);
2742 ext_addr_len = sizeof (struct HttpAddress) + urlen;
2743 memcpy (&ext_addr[1], url, urlen);
2746 LOG (GNUNET_ERROR_TYPE_DEBUG,
2747 "Notifying transport about external hostname address `%s'\n",
2748 plugin->external_hostname);
2751 if (GNUNET_YES == plugin->verify_external_hostname)
2752 LOG (GNUNET_ERROR_TYPE_INFO,
2753 "Enabling SSL verification for external hostname address `%s'\n",
2754 plugin->external_hostname);
2755 plugin->ext_addr = GNUNET_HELLO_address_allocate (plugin->env->my_identity,
2756 "https_client", ext_addr, ext_addr_len, GNUNET_HELLO_ADDRESS_INFO_NONE );
2757 plugin->env->notify_address (plugin->env->cls, GNUNET_YES, plugin->ext_addr);
2758 GNUNET_free (ext_addr);
2760 plugin->ext_addr = GNUNET_HELLO_address_allocate (plugin->env->my_identity,
2761 "http_client", ext_addr, ext_addr_len, GNUNET_HELLO_ADDRESS_INFO_NONE );
2762 plugin->env->notify_address (plugin->env->cls, GNUNET_YES, plugin->ext_addr);
2763 GNUNET_free (ext_addr);
2769 * Configure the plugin
2771 * @param plugin plugin handle
2772 * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
2775 server_configure_plugin (struct HTTP_Server_Plugin *plugin)
2777 unsigned long long port;
2778 unsigned long long max_connections;
2779 char *bind4_address = NULL;
2780 char *bind6_address = NULL;
2781 char *eh_tmp = NULL;
2782 int external_hostname_use_port;
2785 if (GNUNET_CONFIGURATION_have_value
2786 (plugin->env->cfg, plugin->name, "USE_IPv4"))
2789 GNUNET_CONFIGURATION_get_value_yesno (plugin->env->cfg,
2794 plugin->use_ipv4 = GNUNET_YES;
2795 LOG (GNUNET_ERROR_TYPE_DEBUG,
2796 _("IPv4 support is %s\n"),
2797 (plugin->use_ipv4 == GNUNET_YES) ? "enabled" : "disabled");
2800 if (GNUNET_CONFIGURATION_have_value
2801 (plugin->env->cfg, plugin->name, "USE_IPv6"))
2804 GNUNET_CONFIGURATION_get_value_yesno (plugin->env->cfg,
2809 plugin->use_ipv6 = GNUNET_YES;
2810 LOG (GNUNET_ERROR_TYPE_DEBUG,
2811 _("IPv6 support is %s\n"),
2812 (plugin->use_ipv6 == GNUNET_YES) ? "enabled" : "disabled");
2814 if ((plugin->use_ipv4 == GNUNET_NO) && (plugin->use_ipv6 == GNUNET_NO))
2816 LOG (GNUNET_ERROR_TYPE_ERROR,
2817 _("Neither IPv4 nor IPv6 are enabled! Fix in configuration\n"));
2818 return GNUNET_SYSERR;
2821 /* Reading port number from config file */
2823 GNUNET_CONFIGURATION_get_value_number (plugin->env->cfg,
2825 "PORT", &port)) || (port > 65535))
2827 LOG (GNUNET_ERROR_TYPE_ERROR,
2828 _("Port is required! Fix in configuration\n"));
2829 return GNUNET_SYSERR;
2831 plugin->port = port;
2833 LOG (GNUNET_ERROR_TYPE_INFO,
2834 _("Using port %u\n"), plugin->port);
2836 if ((plugin->use_ipv4 == GNUNET_YES) &&
2837 (GNUNET_YES == GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg,
2838 plugin->name, "BINDTO", &bind4_address)))
2840 LOG (GNUNET_ERROR_TYPE_DEBUG,
2841 "Binding %s plugin to specific IPv4 address: `%s'\n",
2842 plugin->protocol, bind4_address);
2843 plugin->server_addr_v4 = GNUNET_new (struct sockaddr_in);
2844 if (1 != inet_pton (AF_INET, bind4_address,
2845 &plugin->server_addr_v4->sin_addr))
2847 LOG (GNUNET_ERROR_TYPE_ERROR,
2848 _("Specific IPv4 address `%s' in configuration file is invalid!\n"),
2850 GNUNET_free (bind4_address);
2851 GNUNET_free (plugin->server_addr_v4);
2852 plugin->server_addr_v4 = NULL;
2853 return GNUNET_SYSERR;
2857 LOG (GNUNET_ERROR_TYPE_DEBUG,
2858 _("Binding to IPv4 address %s\n"),
2860 plugin->server_addr_v4->sin_family = AF_INET;
2861 plugin->server_addr_v4->sin_port = htons (plugin->port);
2863 GNUNET_free (bind4_address);
2866 if ((plugin->use_ipv6 == GNUNET_YES) &&
2868 GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg,
2870 "BINDTO6", &bind6_address)))
2872 LOG (GNUNET_ERROR_TYPE_DEBUG,
2873 "Binding %s plugin to specific IPv6 address: `%s'\n",
2874 plugin->protocol, bind6_address);
2875 plugin->server_addr_v6 = GNUNET_new (struct sockaddr_in6);
2877 inet_pton (AF_INET6, bind6_address, &plugin->server_addr_v6->sin6_addr))
2879 LOG (GNUNET_ERROR_TYPE_ERROR,
2880 _("Specific IPv6 address `%s' in configuration file is invalid!\n"),
2882 GNUNET_free (bind6_address);
2883 GNUNET_free (plugin->server_addr_v6);
2884 plugin->server_addr_v6 = NULL;
2885 return GNUNET_SYSERR;
2889 LOG (GNUNET_ERROR_TYPE_DEBUG,
2890 _("Binding to IPv6 address %s\n"),
2892 plugin->server_addr_v6->sin6_family = AF_INET6;
2893 plugin->server_addr_v6->sin6_port = htons (plugin->port);
2895 GNUNET_free (bind6_address);
2898 plugin->verify_external_hostname = GNUNET_NO;
2900 plugin->verify_external_hostname = GNUNET_CONFIGURATION_get_value_yesno (plugin->env->cfg,
2902 "VERIFY_EXTERNAL_HOSTNAME");
2903 if (GNUNET_SYSERR == plugin->verify_external_hostname)
2904 plugin->verify_external_hostname = GNUNET_NO;
2905 if (GNUNET_YES == plugin->verify_external_hostname)
2906 plugin->options |= HTTP_OPTIONS_VERIFY_CERTIFICATE;
2908 external_hostname_use_port = GNUNET_CONFIGURATION_get_value_yesno (plugin->env->cfg,
2910 "EXTERNAL_HOSTNAME_USE_PORT");
2911 if (GNUNET_SYSERR == external_hostname_use_port)
2912 external_hostname_use_port = GNUNET_NO;
2916 GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg,
2918 "EXTERNAL_HOSTNAME",
2923 char *pos_url = NULL;
2925 if (NULL != strstr(eh_tmp, "://"))
2926 tmp = &strstr(eh_tmp, "://")[3];
2930 if (GNUNET_YES == external_hostname_use_port)
2932 if ( (strlen (tmp) > 1) && (NULL != (pos = strchr(tmp, '/'))) )
2936 GNUNET_asprintf (&plugin->external_hostname,
2940 (NULL == pos_url) ? "" : pos_url);
2943 GNUNET_asprintf (&plugin->external_hostname,
2949 plugin->external_hostname = GNUNET_strdup (tmp);
2950 GNUNET_free (eh_tmp);
2952 LOG (GNUNET_ERROR_TYPE_INFO,
2953 _("Using external hostname `%s'\n"),
2954 plugin->external_hostname);
2955 plugin->notify_ext_task = GNUNET_SCHEDULER_add_now (&server_notify_external_hostname,
2958 /* Use only configured external hostname */
2959 if (GNUNET_CONFIGURATION_have_value
2962 "EXTERNAL_HOSTNAME_ONLY"))
2964 plugin->external_only =
2965 GNUNET_CONFIGURATION_get_value_yesno (plugin->env->cfg,
2967 "EXTERNAL_HOSTNAME_ONLY");
2970 plugin->external_only = GNUNET_NO;
2972 if (GNUNET_YES == plugin->external_only)
2973 LOG (GNUNET_ERROR_TYPE_DEBUG,
2974 _("Notifying transport only about hostname `%s'\n"),
2975 plugin->external_hostname);
2978 LOG (GNUNET_ERROR_TYPE_DEBUG,
2979 "No external hostname configured\n");
2981 /* Optional parameters */
2983 GNUNET_CONFIGURATION_get_value_number (plugin->env->cfg,
2987 max_connections = 128;
2988 plugin->max_connections = max_connections;
2990 LOG (GNUNET_ERROR_TYPE_DEBUG,
2991 _("Maximum number of connections is %u\n"),
2992 plugin->max_connections);
2994 plugin->peer_id_length = strlen (GNUNET_i2s_full (plugin->env->my_identity));
3001 * Exit point from the plugin.
3007 LIBGNUNET_PLUGIN_TRANSPORT_DONE (void *cls)
3009 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
3010 struct HTTP_Server_Plugin *plugin = api->cls;
3012 if (NULL == api->cls)
3014 /* Free for stub mode */
3018 plugin->in_shutdown = GNUNET_YES;
3019 LOG (GNUNET_ERROR_TYPE_DEBUG,
3020 _("Shutting down plugin `%s'\n"),
3023 if (GNUNET_SCHEDULER_NO_TASK != plugin->notify_ext_task)
3025 GNUNET_SCHEDULER_cancel (plugin->notify_ext_task);
3026 plugin->notify_ext_task = GNUNET_SCHEDULER_NO_TASK;
3029 if (NULL != plugin->ext_addr)
3031 LOG (GNUNET_ERROR_TYPE_DEBUG,
3032 "Notifying transport to remove address `%s'\n",
3033 http_common_plugin_address_to_string (plugin->protocol,
3034 plugin->ext_addr->address,
3035 plugin->ext_addr->address_length));
3037 plugin->env->notify_address (plugin->env->cls,
3041 plugin->env->notify_address (plugin->env->cls,
3045 GNUNET_HELLO_address_free (plugin->ext_addr);
3046 plugin->ext_addr = NULL;
3049 /* Stop to report addresses to transport service */
3050 server_stop_report_addresses (plugin);
3051 server_stop (plugin);
3052 GNUNET_CONTAINER_multipeermap_iterate (plugin->sessions,
3053 &destroy_session_cb,
3055 GNUNET_CONTAINER_multipeermap_destroy (plugin->sessions);
3056 plugin->sessions = NULL;
3058 GNUNET_free_non_null (plugin->external_hostname);
3059 GNUNET_free_non_null (plugin->ext_addr);
3060 GNUNET_free_non_null (plugin->server_addr_v4);
3061 GNUNET_free_non_null (plugin->server_addr_v6);
3062 regfree (&plugin->url_regex);
3064 LOG (GNUNET_ERROR_TYPE_DEBUG,
3065 _("Shutdown for plugin `%s' complete\n"),
3068 GNUNET_free (plugin);
3075 * Function called by the pretty printer for the resolved address for
3076 * each human-readable address obtained. The callback can be called
3077 * several times. The last invocation must be with a @a address of
3078 * NULL and a @a res of #GNUNET_OK. Thus, to indicate conversion
3079 * errors, the callback might be called first with @a address NULL and
3080 * @a res being #GNUNET_SYSERR. In that case, there must still be a
3081 * subsequent call later with @a address NULL and @a res #GNUNET_OK.
3083 * @param cls closure
3084 * @param address one of the names for the host, NULL on last callback
3085 * @param res #GNUNET_OK if conversion was successful, #GNUNET_SYSERR on failure,
3086 * #GNUNET_OK on last callback
3089 http_server_plugin_address_to_string (void *cls,
3093 return http_common_plugin_address_to_string (PLUGIN_NAME, addr, addrlen);
3098 * Function obtain the network type for a session
3100 * @param cls closure ('struct HTTP_Server_Plugin*')
3101 * @param session the session
3102 * @return the network type in HBO or GNUNET_SYSERR
3104 static enum GNUNET_ATS_Network_Type
3105 http_server_get_network (void *cls,
3106 struct Session *session)
3108 return ntohl (session->ats_address_network_type);
3113 * Return information about the given session to the
3116 * @param cls the `struct Plugin` with the monitor callback (`sic`)
3117 * @param peer peer we send information about
3118 * @param value our `struct Session` to send information about
3119 * @return #GNUNET_OK (continue to iterate)
3122 send_session_info_iter (void *cls,
3123 const struct GNUNET_PeerIdentity *peer,
3126 struct HTTP_Server_Plugin *plugin = cls;
3127 struct Session *session = value;
3129 notify_session_monitor (plugin,
3131 GNUNET_TRANSPORT_SS_UP);
3137 * Begin monitoring sessions of a plugin. There can only
3138 * be one active monitor per plugin (i.e. if there are
3139 * multiple monitors, the transport service needs to
3140 * multiplex the generated events over all of them).
3142 * @param cls closure of the plugin
3143 * @param sic callback to invoke, NULL to disable monitor;
3144 * plugin will being by iterating over all active
3145 * sessions immediately and then enter monitor mode
3146 * @param sic_cls closure for @a sic
3149 http_server_plugin_setup_monitor (void *cls,
3150 GNUNET_TRANSPORT_SessionInfoCallback sic,
3153 struct HTTP_Server_Plugin *plugin = cls;
3156 plugin->sic_cls = sic_cls;
3159 GNUNET_CONTAINER_multipeermap_iterate (plugin->sessions,
3160 &send_session_info_iter,
3162 /* signal end of first iteration */
3163 sic (sic_cls, NULL, NULL);
3169 * Entry point for the plugin.
3175 LIBGNUNET_PLUGIN_TRANSPORT_INIT (void *cls)
3177 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
3178 struct GNUNET_TRANSPORT_PluginFunctions *api;
3179 struct HTTP_Server_Plugin *plugin;
3181 if (NULL == env->receive)
3183 /* run in 'stub' mode (i.e. as part of gnunet-peerinfo), don't fully
3184 initialze the plugin or the API */
3185 api = GNUNET_new (struct GNUNET_TRANSPORT_PluginFunctions);
3187 api->address_to_string = &http_server_plugin_address_to_string;
3188 api->string_to_address = &http_common_plugin_string_to_address;
3189 api->address_pretty_printer = &http_common_plugin_address_pretty_printer;
3192 plugin = GNUNET_new (struct HTTP_Server_Plugin);
3194 plugin->sessions = GNUNET_CONTAINER_multipeermap_create (128,
3196 api = GNUNET_new (struct GNUNET_TRANSPORT_PluginFunctions);
3198 api->send = &http_server_plugin_send;
3199 api->disconnect_peer = &http_server_plugin_disconnect_peer;
3200 api->disconnect_session = &http_server_plugin_disconnect_session;
3201 api->query_keepalive_factor = &http_server_query_keepalive_factor;
3202 api->check_address = &http_server_plugin_address_suggested;
3203 api->get_session = &http_server_plugin_get_session;
3205 api->address_to_string = &http_server_plugin_address_to_string;
3206 api->string_to_address = &http_common_plugin_string_to_address;
3207 api->address_pretty_printer = &http_common_plugin_address_pretty_printer;
3208 api->get_network = &http_server_get_network;
3209 api->update_session_timeout = &http_server_plugin_update_session_timeout;
3210 // api->update_inbound_delay = &http_server_plugin_update_inbound_delay; // FIXME: implement/support!
3211 api->setup_monitor = &http_server_plugin_setup_monitor;
3213 plugin->name = "transport-https_server";
3214 plugin->protocol = "https";
3216 plugin->name = "transport-http_server";
3217 plugin->protocol = "http";
3220 /* Compile URL regex */
3221 if (regcomp(&plugin->url_regex, URL_REGEX, REG_EXTENDED))
3223 LOG (GNUNET_ERROR_TYPE_ERROR,
3224 _("Unable to compile URL regex\n"));
3225 LIBGNUNET_PLUGIN_TRANSPORT_DONE (api);
3229 /* Configure plugin */
3230 if (GNUNET_SYSERR == server_configure_plugin (plugin))
3232 LIBGNUNET_PLUGIN_TRANSPORT_DONE (api);
3236 /* Check IPv6 support */
3237 if (GNUNET_YES == plugin->use_ipv6)
3238 plugin->use_ipv6 = server_check_ipv6_support (plugin);
3240 /* Report addresses to transport service */
3241 if (GNUNET_NO == plugin->external_only)
3242 server_start_report_addresses (plugin);
3244 if (GNUNET_SYSERR == server_start (plugin))
3246 LIBGNUNET_PLUGIN_TRANSPORT_DONE (api);
3252 /* end of plugin_transport_http_server.c */