2 This file is part of GNUnet
3 (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009 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
28 #include "gnunet_common.h"
29 #include "gnunet_server_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"
37 #define LIBGNUNET_PLUGIN_TRANSPORT_INIT libgnunet_plugin_transport_https_server_init
38 #define LIBGNUNET_PLUGIN_TRANSPORT_DONE libgnunet_plugin_transport_https_server_done
40 #define LIBGNUNET_PLUGIN_TRANSPORT_INIT libgnunet_plugin_transport_http_server_init
41 #define LIBGNUNET_PLUGIN_TRANSPORT_DONE libgnunet_plugin_transport_http_server_done
44 #define HTTP_ERROR_RESPONSE "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\"><HTML><HEAD><TITLE>404 Not Found</TITLE></HEAD><BODY><H1>Not Found</H1>The requested URL was not found on this server.<P><HR><ADDRESS></ADDRESS></BODY></HTML>"
49 * Encapsulation of all of the state of the plugin.
55 * Session handle for connections.
60 * Stored in a linked list.
65 * Stored in a linked list.
70 * To whom are we talking to (set to our identity
71 * if we are still waiting for the welcome message)
73 struct GNUNET_PeerIdentity target;
76 * Pointer to the global plugin struct.
78 struct HTTP_Server_Plugin *plugin;
81 * next pointer for double linked list
83 struct HTTP_Message *msg_head;
86 * previous pointer for double linked list
88 struct HTTP_Message *msg_tail;
91 * Message stream tokenizer for incoming data
93 struct GNUNET_SERVER_MessageStreamTokenizer *msg_tk;
98 struct ServerConnection *server_recv;
103 struct ServerConnection *server_send;
116 * Unique HTTP/S connection tag for this connection
121 * ATS network type in NBO
123 uint32_t ats_address_network_type;
126 * Was session given to transport service?
131 * Did we immediately end the session in disconnect_cb
136 * Absolute time when to receive data again
137 * Used for receive throttling
139 struct GNUNET_TIME_Absolute next_receive;
142 * Session timeout task
144 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
148 struct ServerConnection
153 /* Should this connection get disconnected? GNUNET_YES/NO */
156 /* For PUT connections: Is this the first or last callback with size 0 */
159 /* The session this server connection belongs to */
160 struct Session *session;
162 /* The MHD connection */
163 struct MHD_Connection *mhd_conn;
166 struct MHD_Daemon *mhd_daemon;
171 * Encapsulation of all of the state of the plugin.
173 struct HTTP_Server_Plugin
178 struct GNUNET_TRANSPORT_PluginEnvironment *env;
181 * Linked list head of open sessions.
184 struct Session *head;
187 * Linked list tail of open sessions.
189 struct Session *tail;
204 char *external_hostname;
207 * Maximum number of sockets the plugin can use
208 * Each http inbound /outbound connections are two connections
210 unsigned int max_connections;
213 * Current number of sockets the plugin can use
214 * Each http inbound /outbound connections are two connections
216 unsigned int cur_connections;
219 * Did we immediately end the session in disconnect_cb
229 * External hostname the plugin can be connected to, can be different to
230 * the host's FQDN, used e.g. for reverse proxying
235 * Notify transport only about external address
237 unsigned int external_only;
240 * External address length
260 * Task calling transport service about external address
262 GNUNET_SCHEDULER_TaskIdentifier notify_ext_task;
265 * NAT handle & address management
267 struct GNUNET_NAT_Handle *nat;
270 * List of own addresses
274 * IPv4 addresses DLL head
276 struct HttpAddressWrapper *addr_head;
279 * IPv4 addresses DLL tail
281 struct HttpAddressWrapper *addr_tail;
284 * IPv4 server socket to bind to
286 struct sockaddr_in *server_addr_v4;
289 * IPv6 server socket to bind to
291 struct sockaddr_in6 *server_addr_v6;
296 GNUNET_SCHEDULER_TaskIdentifier server_v4_task;
301 GNUNET_SCHEDULER_TaskIdentifier server_v6_task;
304 * The IPv4 server is scheduled to run asap
306 int server_v4_immediately;
309 * The IPv6 server is scheduled to run asap
311 int server_v6_immediately;
316 struct MHD_Daemon *server_v4;
321 struct MHD_Daemon *server_v6;
329 * Use RC4-128 instead of AES:
330 * NONE:+VERS-TLS1.0:+ARCFOUR-128:+SHA1:+RSA:+COMP-NULL
350 * Wrapper to manage addresses
352 struct HttpAddressWrapper
357 struct HttpAddressWrapper *next;
360 * Linked list previous
362 struct HttpAddressWrapper *prev;
371 * Message to send using http
376 * next pointer for double linked list
378 struct HTTP_Message *next;
381 * previous pointer for double linked list
383 struct HTTP_Message *prev;
386 * buffer containing data to send
391 * amount of data already sent
401 * HTTP/S specific overhead
406 * Continuation function to call once the transmission buffer
407 * has again space available. NULL if there is no
408 * continuation to call.
410 GNUNET_TRANSPORT_TransmitContinuation transmit_cont;
413 * Closure for transmit_cont.
415 void *transmit_cont_cls;
420 * The http_server plugin handle
422 static struct HTTP_Server_Plugin * p;
426 * Start session timeout for session s
427 * @param s the session
430 server_start_session_timeout (struct Session *s);
434 * Increment session timeout due to activity for session s
435 * @param s the session
438 server_reschedule_session_timeout (struct Session *s);
442 * Cancel timeout for session s
443 * @param s the session
446 server_stop_session_timeout (struct Session *s);
450 * Disconnect a session s
451 * @param s the session
454 server_disconnect (struct Session *s);
458 * Does session s exist?
460 * @param plugin the plugin handle
461 * @param s the session
462 * @return GNUNET_YES on success, GNUNET_NO on error
465 server_exist_session (struct HTTP_Server_Plugin *plugin, struct Session *s);
469 * 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, struct MHD_Daemon *server,
481 * Function that can be used by the transport service to transmit
482 * a message using the plugin. Note that in the case of a
483 * peer disconnecting, the continuation MUST be called
484 * prior to the disconnect notification itself. This function
485 * will be called with this peer's HELLO message to initiate
486 * a fresh connection to another peer.
489 * @param session which session must be used
490 * @param msgbuf the message to transmit
491 * @param msgbuf_size number of bytes in 'msgbuf'
492 * @param priority how important is the message (most plugins will
493 * ignore message priority and just FIFO)
494 * @param to how long to wait at most for the transmission (does not
495 * require plugins to discard the message after the timeout,
496 * just advisory for the desired delay; most plugins will ignore
498 * @param cont continuation to call once the message has
499 * been transmitted (or if the transport is ready
500 * for the next transmission call; or if the
501 * peer disconnected...); can be NULL
502 * @param cont_cls closure for cont
503 * @return number of bytes used (on the physical network, with overheads);
504 * -1 on hard errors (i.e. address invalid); 0 is a legal value
505 * and does NOT mean that the message was not transmitted (DV)
508 http_server_plugin_send (void *cls,
509 struct Session *session,
510 const char *msgbuf, size_t msgbuf_size,
511 unsigned int priority,
512 struct GNUNET_TIME_Relative to,
513 GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
515 struct HTTP_Server_Plugin *plugin = cls;
516 struct HTTP_Message *msg;
520 GNUNET_assert (plugin != NULL);
521 GNUNET_assert (session != NULL);
523 if (GNUNET_NO == server_exist_session (plugin, session))
526 return GNUNET_SYSERR;
528 if (NULL == session->server_send)
531 return GNUNET_SYSERR;
535 if (GNUNET_YES == session->server_send->disconnect)
536 return GNUNET_SYSERR;
538 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, session->plugin->name,
539 "Session %p/connection %p: Sending message with %u to peer `%s' with \n",
540 session, session->server_send,
541 msgbuf_size, GNUNET_i2s (&session->target));
543 /* create new message and schedule */
544 bytes_sent = sizeof (struct HTTP_Message) + msgbuf_size;
545 msg = GNUNET_malloc (bytes_sent);
547 msg->size = msgbuf_size;
549 msg->buf = (char *) &msg[1];
550 msg->transmit_cont = cont;
551 msg->transmit_cont_cls = cont_cls;
552 memcpy (msg->buf, msgbuf, msgbuf_size);
554 GNUNET_CONTAINER_DLL_insert_tail (session->msg_head, session->msg_tail, msg);
556 GNUNET_asprintf (&stat_txt, "# bytes currently in %s_server buffers", plugin->protocol);
557 GNUNET_STATISTICS_update (plugin->env->stats,
558 stat_txt, msgbuf_size, GNUNET_NO);
559 GNUNET_free (stat_txt);
561 server_reschedule (session->plugin, session->server_send->mhd_daemon, GNUNET_YES);
562 server_reschedule_session_timeout (session);
564 /* struct Plugin *plugin = cls; */
571 * Function that can be used to force the plugin to disconnect
572 * from the given peer and cancel all previous transmissions
573 * (and their continuationc).
576 * @param target peer from which to disconnect
579 http_server_plugin_disconnect (void *cls, const struct GNUNET_PeerIdentity *target)
581 struct HTTP_Server_Plugin *plugin = cls;
582 struct Session *next = NULL;
583 struct Session *pos = NULL;
585 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
586 "Transport tells me to disconnect `%s'\n",
587 GNUNET_i2s (target));
590 while (NULL != (pos = next))
593 if (0 == memcmp (target, &pos->target, sizeof (struct GNUNET_PeerIdentity)))
595 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
596 "Disconnecting session %p to `%s'\n",
597 pos, GNUNET_i2s (target));
598 server_disconnect (pos);
606 * Another peer has suggested an address for this
607 * peer and transport plugin. Check that this could be a valid
608 * address. If so, consider adding it to the list
612 * @param addr pointer to the address
613 * @param addrlen length of addr
614 * @return GNUNET_OK if this is a plausible address for this peer
618 http_server_plugin_address_suggested (void *cls, const void *addr,
621 struct HTTP_Server_Plugin *plugin = cls;
622 struct HttpAddressWrapper *next;
623 struct HttpAddressWrapper *pos;
626 if ((NULL != plugin->ext_addr) &&
627 GNUNET_YES == (http_common_cmp_addresses (addr, addrlen,
628 plugin->ext_addr, plugin->ext_addr_len)))
631 next = plugin->addr_head;
632 while (NULL != (pos = next))
635 if (GNUNET_YES == (http_common_cmp_addresses(addr,
648 * Creates a new outbound session the transport
649 * service will use to send data to the peer
651 * Since HTTP/S server cannot create sessions, always return NULL
653 * @param cls the plugin
654 * @param address the address
655 * @return always NULL
657 static struct Session *
658 http_server_plugin_get_session (void *cls,
659 const struct GNUNET_HELLO_Address *address)
666 * Deleting the session
667 * Must not be used afterwards
669 * @param s the session to delete
672 server_delete_session (struct Session *s)
674 struct HTTP_Server_Plugin *plugin = s->plugin;
675 server_stop_session_timeout(s);
677 GNUNET_CONTAINER_DLL_remove (plugin->head, plugin->tail, s);
678 struct HTTP_Message *msg = s->msg_head;
679 struct HTTP_Message *tmp = NULL;
685 GNUNET_CONTAINER_DLL_remove (s->msg_head, s->msg_tail, msg);
686 if (msg->transmit_cont != NULL)
688 msg->transmit_cont (msg->transmit_cont_cls, &s->target, GNUNET_SYSERR,
689 msg->size, msg->pos + msg->overhead);
695 if (s->msg_tk != NULL)
697 GNUNET_SERVER_mst_destroy (s->msg_tk);
700 GNUNET_free (s->addr);
701 GNUNET_free_non_null (s->server_recv);
702 GNUNET_free_non_null (s->server_send);
705 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
706 "Session %p destroyed\n", s);
711 * Cancel timeout for session s
713 * @param s the session
716 server_stop_session_timeout (struct Session *s)
718 GNUNET_assert (NULL != s);
720 if (GNUNET_SCHEDULER_NO_TASK != s->timeout_task)
722 GNUNET_SCHEDULER_cancel (s->timeout_task);
723 s->timeout_task = GNUNET_SCHEDULER_NO_TASK;
724 GNUNET_log (TIMEOUT_LOG, "Timeout stopped for session %p\n", s);
730 * Function that queries MHD's select sets and
731 * starts the task waiting for them.
732 * @param plugin plugin
733 * @param daemon_handle the MHD daemon handle
734 * @param now schedule immediately
735 * @return gnunet task identifier
737 static GNUNET_SCHEDULER_TaskIdentifier
738 server_schedule (struct HTTP_Server_Plugin *plugin,
739 struct MHD_Daemon *daemon_handle,
744 * Reschedule the execution of both IPv4 and IPv6 server
745 * @param plugin the plugin
746 * @param server which server to schedule v4 or v6?
747 * @param now GNUNET_YES to schedule execution immediately, GNUNET_NO to wait
751 server_reschedule (struct HTTP_Server_Plugin *plugin, struct MHD_Daemon *server,
754 if ((server == plugin->server_v4) && (plugin->server_v4 != NULL))
756 if (GNUNET_YES == plugin->server_v4_immediately)
757 return; /* No rescheduling, server will run asap */
759 if (GNUNET_YES == now)
760 plugin->server_v4_immediately = GNUNET_YES;
762 if (plugin->server_v4_task != GNUNET_SCHEDULER_NO_TASK)
764 GNUNET_SCHEDULER_cancel (plugin->server_v4_task);
765 plugin->server_v4_task = GNUNET_SCHEDULER_NO_TASK;
767 plugin->server_v4_task = server_schedule (plugin, plugin->server_v4, now);
770 if ((server == plugin->server_v6) && (plugin->server_v6 != NULL))
772 if (GNUNET_YES == plugin->server_v6_immediately)
773 return; /* No rescheduling, server will run asap */
775 if (GNUNET_YES == now)
776 plugin->server_v6_immediately = GNUNET_YES;
778 if (plugin->server_v6_task != GNUNET_SCHEDULER_NO_TASK)
780 GNUNET_SCHEDULER_cancel (plugin->server_v6_task);
781 plugin->server_v6_task = GNUNET_SCHEDULER_NO_TASK;
783 plugin->server_v6_task = server_schedule (plugin, plugin->server_v6, now);
789 * Disconnect session s
791 * @param s the session
792 * @return GNUNET_OK on success
795 server_disconnect (struct Session *s)
797 struct ServerConnection * send = NULL;
798 struct ServerConnection * recv = NULL;
800 if (GNUNET_NO == server_exist_session (p, s))
803 return GNUNET_SYSERR;
806 send = (struct ServerConnection *) s->server_send;
807 if (s->server_send != NULL)
809 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, s->plugin->name,
810 "Server: %p / %p Terminating inbound PUT session to peer `%s'\n",
811 s, s->server_send, GNUNET_i2s (&s->target));
813 send->disconnect = GNUNET_YES;
814 #if MHD_VERSION >= 0x00090E00
815 MHD_set_connection_option (send->mhd_conn, MHD_CONNECTION_OPTION_TIMEOUT,
818 server_reschedule (s->plugin, send->mhd_daemon, GNUNET_YES);
821 recv = (struct ServerConnection *) s->server_recv;
824 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, s->plugin->name,
825 "Server: %p / %p Terminating inbound GET session to peer `%s'\n",
826 s, s->server_recv, GNUNET_i2s (&s->target));
828 recv->disconnect = GNUNET_YES;
829 #if MHD_VERSION >= 0x00090E00
830 MHD_set_connection_option (recv->mhd_conn, MHD_CONNECTION_OPTION_TIMEOUT,
833 server_reschedule (s->plugin, recv->mhd_daemon, GNUNET_YES);
839 server_mhd_connection_timeout (struct HTTP_Server_Plugin *plugin, struct Session *s, int to)
841 #if MHD_VERSION >= 0x00090E00
842 /* Setting timeouts for other connections */
843 if (s->server_recv != NULL)
845 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
846 "Setting timeout for %p to %u sec.\n", s->server_recv, to);
847 MHD_set_connection_option (s->server_recv->mhd_conn,
848 MHD_CONNECTION_OPTION_TIMEOUT,
850 server_reschedule (plugin, s->server_recv->mhd_daemon, GNUNET_NO);
852 if (s->server_send != NULL)
854 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
855 "Setting timeout for %p to %u sec.\n", s->server_send, to);
856 MHD_set_connection_option (s->server_send->mhd_conn,
857 MHD_CONNECTION_OPTION_TIMEOUT,
859 server_reschedule (plugin, s->server_send->mhd_daemon, GNUNET_NO);
865 * Parse incoming URL for tag and target
867 * @param plugin plugin
868 * @param url incoming url
869 * @param target where to store the target
870 * @param tag where to store the tag
871 * @return GNUNET_OK on success, GNUNET_SYSERR on error
875 server_parse_url (struct HTTP_Server_Plugin *plugin, const char * url, struct GNUNET_PeerIdentity * target, uint32_t *tag)
877 int debug = GNUNET_YES;
879 char * tag_start = NULL;
880 char * tag_end = NULL;
881 char * target_start = NULL;
882 char * separator = NULL;
883 char hash[plugin->peer_id_length+1];
885 unsigned long int ctag;
888 * URL is valid if it is in the form [prefix with (multiple) '/'][peerid[103];tag]*/
893 return GNUNET_SYSERR;
898 separator = strrchr (url, ';');
900 if (NULL == separator)
902 if (debug) GNUNET_break (0);
903 return GNUNET_SYSERR;
905 tag_start = separator + 1;
907 if (strlen (tag_start) == 0)
909 /* No tag after separator */
910 if (debug) GNUNET_break (0);
911 return GNUNET_SYSERR;
913 ctag = strtoul (tag_start, &tag_end, 10);
916 /* tag == 0 , invalid */
917 if (debug) GNUNET_break (0);
918 return GNUNET_SYSERR;
920 if ((ctag == ULONG_MAX) && (ERANGE == errno))
922 /* out of range: > ULONG_MAX */
923 if (debug) GNUNET_break (0);
924 return GNUNET_SYSERR;
926 if (ctag > UINT32_MAX)
928 /* out of range: > UINT32_MAX */
929 if (debug) GNUNET_break (0);
930 return GNUNET_SYSERR;
932 (*tag) = (uint32_t) ctag;
935 /* no char after tag */
936 if (debug) GNUNET_break (0);
937 return GNUNET_SYSERR;
939 if (url[strlen(url)] != tag_end[0])
941 /* there are more not converted chars after tag */
942 if (debug) GNUNET_break (0);
943 return GNUNET_SYSERR;
946 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
947 "Found tag `%u' in url\n", (*tag));
949 /* convert peer id */
950 target_start = strrchr (url, '/');
951 if (NULL == target_start)
954 target_start = (char *) url;
957 hash_length = separator - target_start;
958 if (hash_length != plugin->peer_id_length)
960 /* no char after tag */
961 if (debug) GNUNET_break (0);
962 return GNUNET_SYSERR;
964 memcpy (hash, target_start, hash_length);
965 hash[hash_length] = '\0';
967 if (GNUNET_OK != GNUNET_CRYPTO_hash_from_string ((const char *) hash, &(target->hashPubKey)))
969 /* hash conversion failed */
970 if (debug) GNUNET_break (0);
971 return GNUNET_SYSERR;
974 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
975 "Found target `%s' in url\n", GNUNET_h2s_full(&target->hashPubKey));
981 * Lookup a mhd connection and create one if none is found
983 * @param plugin the plugin handle
984 * @param mhd_connection the incoming mhd_connection
985 * @param url incoming requested URL
986 * @param method PUT or GET
987 * @return the server connecetion
989 static struct ServerConnection *
990 server_lookup_connection (struct HTTP_Server_Plugin *plugin,
991 struct MHD_Connection *mhd_connection, const char *url,
994 struct Session *s = NULL;
995 struct ServerConnection *sc = NULL;
996 const union MHD_ConnectionInfo *conn_info;
997 struct GNUNET_ATS_Information ats;
1002 struct GNUNET_PeerIdentity target;
1004 int direction = GNUNET_SYSERR;
1007 conn_info = MHD_get_connection_info (mhd_connection,
1008 MHD_CONNECTION_INFO_CLIENT_ADDRESS);
1009 if ((conn_info->client_addr->sa_family != AF_INET) &&
1010 (conn_info->client_addr->sa_family != AF_INET6))
1012 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1013 "New %s connection from %s\n", method, url);
1015 if (GNUNET_SYSERR == server_parse_url (plugin, url, &target, &tag))
1017 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1018 "Invalid url %s\n", url);
1021 if (0 == strcmp (MHD_HTTP_METHOD_PUT, method))
1022 direction = _RECEIVE;
1023 else if (0 == strcmp (MHD_HTTP_METHOD_GET, method))
1027 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1028 "Invalid method %s connection from %s\n", method, url);
1032 plugin->cur_connections++;
1033 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1034 "New %s connection from %s with tag %u (%u of %u)\n",
1036 GNUNET_i2s (&target), tag,
1037 plugin->cur_connections, plugin->max_connections);
1038 /* find duplicate session */
1042 if ((0 == memcmp (&s->target, &target, sizeof (struct GNUNET_PeerIdentity))) &&
1049 if ((_RECEIVE == direction) && (NULL != s->server_recv))
1051 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1052 "Duplicate PUT connection from `%s' tag %u, dismissing new connection\n",
1053 GNUNET_i2s (&target),
1058 if ((_SEND == direction) && (NULL != s->server_send))
1060 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1061 "Duplicate GET connection from `%s' tag %u, dismissing new connection\n",
1062 GNUNET_i2s (&target),
1069 /* create new session */
1070 switch (conn_info->client_addr->sa_family)
1073 addr = http_common_address_from_socket (plugin->protocol, conn_info->client_addr, sizeof (struct sockaddr_in));
1074 addr_len = http_common_address_get_size (addr);
1075 ats = plugin->env->get_address_type (plugin->env->cls, conn_info->client_addr, sizeof (struct sockaddr_in));
1078 addr = http_common_address_from_socket (plugin->protocol, conn_info->client_addr, sizeof (struct sockaddr_in6));
1079 addr_len = http_common_address_get_size (addr);
1080 ats = plugin->env->get_address_type (plugin->env->cls, conn_info->client_addr, sizeof (struct sockaddr_in6));
1086 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1087 "Creating new session for peer `%s' connecting from `%s'\n",
1088 GNUNET_i2s (&target),
1089 http_common_plugin_address_to_string (NULL, addr, addr_len));
1091 s = GNUNET_malloc (sizeof (struct Session));
1092 memcpy (&s->target, &target, sizeof (struct GNUNET_PeerIdentity));
1095 s->addrlen = addr_len;
1096 s->ats_address_network_type = ats.value;
1097 s->next_receive = GNUNET_TIME_UNIT_ZERO_ABS;
1099 s->server_recv = NULL;
1100 s->server_send = NULL;
1101 s->session_passed = GNUNET_NO;
1102 s->session_ended = GNUNET_NO;
1103 server_start_session_timeout(s);
1104 GNUNET_CONTAINER_DLL_insert (plugin->head, plugin->tail, s);
1106 sc = GNUNET_malloc (sizeof (struct ServerConnection));
1107 if (conn_info->client_addr->sa_family == AF_INET)
1108 sc->mhd_daemon = plugin->server_v4;
1109 if (conn_info->client_addr->sa_family == AF_INET6)
1110 sc->mhd_daemon = plugin->server_v6;
1111 sc->mhd_conn = mhd_connection;
1112 sc->direction = direction;
1113 sc->connected = GNUNET_NO;
1115 if (direction == _SEND)
1116 s->server_send = sc;
1117 if (direction == _RECEIVE)
1118 s->server_recv = sc;
1119 #if MHD_VERSION >= 0x00090E00
1120 if ((NULL == s->server_recv) || (NULL == s->server_send))
1122 to = (HTTP_SERVER_NOT_VALIDATED_TIMEOUT.rel_value / 1000);
1123 MHD_set_connection_option (mhd_connection, MHD_CONNECTION_OPTION_TIMEOUT, to);
1124 server_reschedule (plugin, sc->mhd_daemon, GNUNET_NO);
1128 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1129 "Session %p for peer `%s' fully connected\n",
1130 s, GNUNET_i2s (&target));
1131 to = (SERVER_SESSION_TIMEOUT.rel_value / 1000);
1132 server_mhd_connection_timeout (plugin, s, to);
1135 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1136 "Setting timeout for %p to %u sec.\n", sc, to);
1143 * Lookup a session for a server connection
1145 * @param plugin the plugin
1146 * @param sc the server connection
1147 * @return the session found or NULL
1149 static struct Session *
1150 server_lookup_session (struct HTTP_Server_Plugin *plugin,
1151 struct ServerConnection * sc)
1155 for (s = plugin->head; NULL != s; s = s->next)
1156 if ((s->server_recv == sc) || (s->server_send == sc))
1162 server_exist_session (struct HTTP_Server_Plugin *plugin, struct Session *s)
1164 struct Session * head;
1166 GNUNET_assert (NULL != plugin);
1167 GNUNET_assert (NULL != s);
1169 for (head = plugin->head; head != NULL; head = head->next)
1179 * Callback called by MHD when it needs data to send
1181 * @param cls current session
1182 * @param pos position in buffer
1183 * @param buf the buffer to write data to
1184 * @param max max number of bytes available in buffer
1185 * @return bytes written to buffer
1188 server_send_callback (void *cls, uint64_t pos, char *buf, size_t max)
1190 struct Session *s = cls;
1191 ssize_t bytes_read = 0;
1192 struct HTTP_Message *msg;
1195 GNUNET_assert (NULL != p);
1196 if (GNUNET_NO == server_exist_session (p, s))
1202 bytes_read = GNUNET_MIN (msg->size - msg->pos,
1204 memcpy (buf, &msg->buf[msg->pos], bytes_read);
1205 msg->pos += bytes_read;
1207 /* removing message */
1208 if (msg->pos == msg->size)
1210 GNUNET_CONTAINER_DLL_remove (s->msg_head, s->msg_tail, msg);
1211 if (NULL != msg->transmit_cont)
1212 msg->transmit_cont (msg->transmit_cont_cls, &s->target, GNUNET_OK,
1213 msg->size, msg->size + msg->overhead);
1219 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, s->plugin->name,
1220 "Sent %u bytes to peer `%s' with session %p \n", bytes_read, GNUNET_i2s (&s->target), s);
1221 GNUNET_asprintf (&stat_txt, "# bytes currently in %s_server buffers", p->protocol);
1222 GNUNET_STATISTICS_update (p->env->stats,
1223 stat_txt, -bytes_read, GNUNET_NO);
1224 GNUNET_free (stat_txt);
1225 GNUNET_asprintf (&stat_txt, "# bytes transmitted via %s_server", p->protocol);
1226 GNUNET_STATISTICS_update (p->env->stats,
1227 stat_txt, bytes_read, GNUNET_NO);
1228 GNUNET_free (stat_txt);
1235 * Callback called by MessageStreamTokenizer when a message has arrived
1237 * @param cls current session as closure
1238 * @param client client
1239 * @param message the message to be forwarded to transport service
1243 server_receive_mst_cb (void *cls, void *client,
1244 const struct GNUNET_MessageHeader *message)
1246 struct Session *s = cls;
1247 struct GNUNET_ATS_Information atsi[2];
1248 struct GNUNET_TIME_Relative delay;
1251 GNUNET_assert (NULL != p);
1252 if (GNUNET_NO == server_exist_session(p, s))
1255 struct HTTP_Server_Plugin *plugin = s->plugin;
1257 atsi[0].type = htonl (GNUNET_ATS_QUALITY_NET_DISTANCE);
1258 atsi[0].value = htonl (1);
1259 atsi[1].type = htonl (GNUNET_ATS_NETWORK_TYPE);
1260 atsi[1].value = s->ats_address_network_type;
1261 GNUNET_break (s->ats_address_network_type != ntohl (GNUNET_ATS_NET_UNSPECIFIED));
1264 delay = plugin->env->receive (plugin->env->cls,
1267 (const struct GNUNET_ATS_Information *) &atsi, 2,
1268 s, s->addr, s->addrlen);
1270 GNUNET_asprintf (&stat_txt, "# bytes received via %s_server", plugin->protocol);
1271 GNUNET_STATISTICS_update (plugin->env->stats,
1272 stat_txt, ntohs (message->size), GNUNET_NO);
1273 GNUNET_free (stat_txt);
1275 s->session_passed = GNUNET_YES;
1276 s->next_receive = GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (), delay);
1277 if (delay.rel_value > 0)
1279 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1280 "Peer `%s' address `%s' next read delayed for %llu ms\n",
1281 GNUNET_i2s (&s->target),
1282 http_common_plugin_address_to_string (NULL, s->addr, s->addrlen),
1285 server_reschedule_session_timeout (s);
1291 * MHD callback for a new incoming connection
1293 * @param cls the plugin handle
1294 * @param mhd_connection the mhd connection
1295 * @param url the requested URL
1296 * @param method GET or PUT
1297 * @param version HTTP version
1298 * @param upload_data upload data
1299 * @param upload_data_size sizeof upload data
1300 * @param httpSessionCache the session cache to remember the connection
1301 * @return MHD_YES if connection is accepted, MHD_NO on reject
1304 server_access_cb (void *cls, struct MHD_Connection *mhd_connection,
1305 const char *url, const char *method, const char *version,
1306 const char *upload_data, size_t * upload_data_size,
1307 void **httpSessionCache)
1309 struct HTTP_Server_Plugin *plugin = cls;
1312 struct ServerConnection *sc = *httpSessionCache;
1314 struct MHD_Response *response;
1316 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1317 _("Access from connection %p (%u of %u) for `%s' `%s' url `%s' with upload data size %u\n"),
1319 plugin->cur_connections, plugin->max_connections,
1320 method, version, url, (*upload_data_size));
1322 GNUNET_assert (cls != NULL);
1325 /* new connection */
1326 sc = server_lookup_connection (plugin, mhd_connection, url, method);
1329 (*httpSessionCache) = sc;
1333 response = MHD_create_response_from_data (strlen (HTTP_ERROR_RESPONSE), HTTP_ERROR_RESPONSE, MHD_NO, MHD_NO);
1334 res = MHD_queue_response (mhd_connection, MHD_HTTP_NOT_FOUND, response);
1335 MHD_destroy_response (response);
1341 /* 'old' connection */
1342 if (NULL == server_lookup_session (plugin, sc))
1344 /* Session was already disconnected */
1349 /* existing connection */
1350 sc = (*httpSessionCache);
1352 GNUNET_assert (NULL != s);
1353 /* connection is to be disconnected */
1354 if (sc->disconnect == GNUNET_YES)
1356 /* Sent HTTP/1.1: 200 OK as response */
1357 response = MHD_create_response_from_data (strlen ("Thank you!"),
1360 res = MHD_queue_response (mhd_connection, MHD_HTTP_OK, response);
1361 MHD_destroy_response (response);
1364 GNUNET_assert (s != NULL);
1366 if (sc->direction == _SEND)
1368 response = MHD_create_response_from_callback (MHD_SIZE_UNKNOWN,
1370 &server_send_callback, s,
1372 MHD_queue_response (mhd_connection, MHD_HTTP_OK, response);
1373 MHD_destroy_response (response);
1376 if (sc->direction == _RECEIVE)
1378 if ((*upload_data_size == 0) && (sc->connected == GNUNET_NO))
1380 /* (*upload_data_size == 0) first callback when header are passed */
1381 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1382 "Session %p / Connection %p: Peer `%s' PUT on address `%s' connected\n",
1384 GNUNET_i2s (&s->target),
1385 http_common_plugin_address_to_string (NULL,
1388 sc->connected = GNUNET_YES;
1391 else if ((*upload_data_size == 0) && (sc->connected == GNUNET_YES))
1393 /* (*upload_data_size == 0) when upload is complete */
1394 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1395 "Session %p / Connection %p: Peer `%s' PUT on address `%s' finished upload\n",
1397 GNUNET_i2s (&s->target),
1398 http_common_plugin_address_to_string (NULL,
1401 sc->connected = GNUNET_NO;
1402 /* Sent HTTP/1.1: 200 OK as PUT Response\ */
1403 response = MHD_create_response_from_data (strlen ("Thank you!"),
1406 res = MHD_queue_response (mhd_connection, MHD_HTTP_OK, response);
1407 MHD_destroy_response (response);
1410 else if ((*upload_data_size > 0) && (sc->connected == GNUNET_YES))
1412 /* (*upload_data_size > 0) for every segment received */
1413 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1414 "Session %p / Connection %p: Peer `%s' PUT on address `%s' received %u bytes\n",
1416 GNUNET_i2s (&s->target),
1417 http_common_plugin_address_to_string (NULL,
1421 struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get ();
1423 if ((s->next_receive.abs_value <= now.abs_value))
1425 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1426 "PUT with %u bytes forwarded to MST\n",
1428 if (s->msg_tk == NULL)
1430 s->msg_tk = GNUNET_SERVER_mst_create (&server_receive_mst_cb, s);
1432 GNUNET_SERVER_mst_receive (s->msg_tk, s, upload_data,
1433 *upload_data_size, GNUNET_NO, GNUNET_NO);
1434 #if MHD_VERSION >= 0x00090E00
1435 server_mhd_connection_timeout (plugin, s, GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT.rel_value / 1000);
1437 (*upload_data_size) = 0;
1441 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1442 "Session %p / Connection %p: no inbound bandwidth available! Next read was delayed by %llu ms\n",
1443 s, sc, now.abs_value - s->next_receive.abs_value);
1458 * Callback from MHD when a connection disconnects
1460 * @param cls closure
1461 * @param connection the disconnected MHD connection
1462 * @param httpSessionCache the pointer to distinguish
1465 server_disconnect_cb (void *cls, struct MHD_Connection *connection,
1466 void **httpSessionCache)
1468 struct ServerConnection *sc = *httpSessionCache;
1469 struct Session *s = NULL;
1470 struct Session *t = NULL;
1471 struct HTTP_Server_Plugin *plugin = NULL;
1473 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, p->name,
1474 "Disconnect for connection %p \n", sc);
1479 if (NULL == (s = server_lookup_session (p, sc)))
1482 GNUNET_assert (NULL != p);
1483 for (t = p->head; t != NULL; t = t->next)
1492 if (sc->direction == _SEND)
1495 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1496 "Peer `%s' connection %p, GET on address `%s' disconnected\n",
1497 GNUNET_i2s (&s->target), s->server_send,
1498 http_common_plugin_address_to_string (NULL, s->addr, s->addrlen));
1499 s->server_send = NULL;
1500 if (NULL != (s->server_recv))
1502 s->server_recv->disconnect = GNUNET_YES;
1503 GNUNET_assert (NULL != s->server_recv->mhd_conn);
1504 #if MHD_VERSION >= 0x00090E00
1505 MHD_set_connection_option (s->server_recv->mhd_conn, MHD_CONNECTION_OPTION_TIMEOUT,
1508 server_reschedule (plugin, s->server_recv->mhd_daemon, GNUNET_NO);
1511 if (sc->direction == _RECEIVE)
1513 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1514 "Peer `%s' connection %p PUT on address `%s' disconnected\n",
1515 GNUNET_i2s (&s->target), s->server_recv,
1516 http_common_plugin_address_to_string (NULL, s->addr, s->addrlen));
1517 s->server_recv = NULL;
1518 /* Do not terminate session when PUT disconnects
1519 if (NULL != (s->server_send))
1521 s->server_send->disconnect = GNUNET_YES;
1522 GNUNET_assert (NULL != s->server_send->mhd_conn);
1523 #if MHD_VERSION >= 0x00090E00
1524 MHD_set_connection_option (s->server_send->mhd_conn, MHD_CONNECTION_OPTION_TIMEOUT,
1527 server_reschedule (plugin, s->server_send->mhd_daemon, GNUNET_NO);
1529 if (s->msg_tk != NULL)
1531 GNUNET_SERVER_mst_destroy (s->msg_tk);
1537 plugin->cur_connections--;
1539 if ((s->server_send == NULL) && (s->server_recv == NULL))
1541 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1542 "Peer `%s' on address `%s' disconnected\n",
1543 GNUNET_i2s (&s->target),
1544 http_common_plugin_address_to_string (NULL, s->addr, s->addrlen));
1546 if ((GNUNET_YES == s->session_passed) && (GNUNET_NO == s->session_ended))
1548 /* Notify transport immediately that this session is invalid */
1549 s->session_ended = GNUNET_YES;
1550 plugin->env->session_end (plugin->env->cls, &s->target, s);
1552 server_delete_session (s);
1559 * Check if incoming connection is accepted.
1561 * @param cls plugin as closure
1562 * @param addr address of incoming connection
1563 * @param addr_len address length of incoming connection
1564 * @return MHD_YES if connection is accepted, MHD_NO if connection is rejected
1568 server_accept_cb (void *cls, const struct sockaddr *addr, socklen_t addr_len)
1570 struct HTTP_Server_Plugin *plugin = cls;
1572 if (plugin->cur_connections <= plugin->max_connections)
1574 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1575 _("Accepting connection (%u of %u) from `%s'\n"),
1576 plugin->cur_connections, plugin->max_connections,
1577 GNUNET_a2s (addr, addr_len));
1582 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, plugin->name,
1583 _("Server reached maximum number connections (%u), rejecting new connection\n"),
1584 plugin->max_connections);
1590 server_log (void *arg, const char *fmt, va_list ap)
1594 vsnprintf (text, sizeof (text), fmt, ap);
1596 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Server: %s\n", text);
1601 * Call MHD IPv4 to process pending requests and then go back
1602 * and schedule the next run.
1603 * @param cls plugin as closure
1604 * @param tc task context
1607 server_v4_run (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1609 struct HTTP_Server_Plugin *plugin = cls;
1611 GNUNET_assert (cls != NULL);
1613 plugin->server_v4_task = GNUNET_SCHEDULER_NO_TASK;
1614 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1617 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1618 "Running IPv4 server\n");
1620 plugin->server_v4_immediately = GNUNET_NO;
1621 GNUNET_assert (MHD_YES == MHD_run (plugin->server_v4));
1622 server_reschedule (plugin, plugin->server_v4, GNUNET_NO);
1627 * Call MHD IPv6 to process pending requests and then go back
1628 * and schedule the next run.
1629 * @param cls plugin as closure
1630 * @param tc task context
1633 server_v6_run (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1635 struct HTTP_Server_Plugin *plugin = cls;
1637 GNUNET_assert (cls != NULL);
1638 plugin->server_v6_task = GNUNET_SCHEDULER_NO_TASK;
1639 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1642 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1643 "Running IPv6 server\n");
1645 plugin->server_v6_immediately = GNUNET_NO;
1646 GNUNET_assert (MHD_YES == MHD_run (plugin->server_v6));
1647 server_reschedule (plugin, plugin->server_v6, GNUNET_NO);
1651 #define UNSIGNED_MHD_LONG_LONG unsigned MHD_LONG_LONG
1654 * Function that queries MHD's select sets and
1655 * starts the task waiting for them.
1657 * @param plugin plugin
1658 * @param daemon_handle the MHD daemon handle
1659 * @return gnunet task identifier
1661 static GNUNET_SCHEDULER_TaskIdentifier
1662 server_schedule (struct HTTP_Server_Plugin *plugin,
1663 struct MHD_Daemon *daemon_handle,
1666 GNUNET_SCHEDULER_TaskIdentifier ret;
1670 struct GNUNET_NETWORK_FDSet *wrs;
1671 struct GNUNET_NETWORK_FDSet *wws;
1672 struct GNUNET_NETWORK_FDSet *wes;
1674 UNSIGNED_MHD_LONG_LONG timeout;
1675 static unsigned long long last_timeout = 0;
1678 struct GNUNET_TIME_Relative tv;
1680 if (GNUNET_YES == plugin->in_shutdown)
1681 return GNUNET_SCHEDULER_NO_TASK;
1683 ret = GNUNET_SCHEDULER_NO_TASK;
1687 wrs = GNUNET_NETWORK_fdset_create ();
1688 wes = GNUNET_NETWORK_fdset_create ();
1689 wws = GNUNET_NETWORK_fdset_create ();
1691 GNUNET_assert (MHD_YES == MHD_get_fdset (daemon_handle, &rs, &ws, &es, &max));
1692 haveto = MHD_get_timeout (daemon_handle, &timeout);
1693 if (haveto == MHD_YES)
1695 if (timeout != last_timeout)
1698 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1699 "SELECT Timeout changed from %llu to %llu\n",
1700 last_timeout, timeout);
1701 last_timeout = timeout;
1703 if (timeout <= GNUNET_TIME_UNIT_SECONDS.rel_value)
1704 tv.rel_value = (uint64_t) timeout;
1706 tv = GNUNET_TIME_UNIT_SECONDS;
1709 tv = GNUNET_TIME_UNIT_SECONDS;
1710 /* Force immediate run, since we have outbound data to send */
1711 if (now == GNUNET_YES)
1712 tv = GNUNET_TIME_UNIT_MILLISECONDS;
1713 GNUNET_NETWORK_fdset_copy_native (wrs, &rs, max + 1);
1714 GNUNET_NETWORK_fdset_copy_native (wws, &ws, max + 1);
1715 GNUNET_NETWORK_fdset_copy_native (wes, &es, max + 1);
1717 if (daemon_handle == plugin->server_v4)
1719 if (plugin->server_v4_task != GNUNET_SCHEDULER_NO_TASK)
1721 GNUNET_SCHEDULER_cancel (plugin->server_v4_task);
1722 plugin->server_v4_task = GNUNET_SCHEDULER_NO_TASK;
1725 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1726 "Scheduling IPv4 server task in %llu ms\n", tv);
1729 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1731 &server_v4_run, plugin);
1733 if (daemon_handle == plugin->server_v6)
1735 if (plugin->server_v6_task != GNUNET_SCHEDULER_NO_TASK)
1737 GNUNET_SCHEDULER_cancel (plugin->server_v6_task);
1738 plugin->server_v6_task = GNUNET_SCHEDULER_NO_TASK;
1741 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1742 "Scheduling IPv6 server task in %llu ms\n", tv);
1745 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1747 &server_v6_run, plugin);
1749 GNUNET_NETWORK_fdset_destroy (wrs);
1750 GNUNET_NETWORK_fdset_destroy (wws);
1751 GNUNET_NETWORK_fdset_destroy (wes);
1758 * Load ssl certificate from file
1760 * @param file filename
1761 * @return content of the file
1764 server_load_file (const char *file)
1766 struct GNUNET_DISK_FileHandle *gn_file;
1770 if (GNUNET_OK != GNUNET_DISK_file_size (file,
1771 &fsize, GNUNET_NO, GNUNET_YES))
1773 text = GNUNET_malloc (fsize + 1);
1775 GNUNET_DISK_file_open (file, GNUNET_DISK_OPEN_READ,
1776 GNUNET_DISK_PERM_USER_READ);
1777 if (gn_file == NULL)
1782 if (GNUNET_SYSERR == GNUNET_DISK_file_read (gn_file, text, fsize))
1785 GNUNET_DISK_file_close (gn_file);
1789 GNUNET_DISK_file_close (gn_file);
1797 * Load ssl certificate
1799 * @param plugin the plugin
1800 * @return GNUNET_OK on success, GNUNET_SYSERR on failure
1803 server_load_certificate (struct HTTP_Server_Plugin *plugin)
1805 int res = GNUNET_OK;
1811 /* Get crypto init string from config
1812 * If not present just use default values */
1815 GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg,
1820 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
1821 "Failed to get servicehome!\n");
1822 return GNUNET_SYSERR;
1827 GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg,
1830 &plugin->crypto_init))
1831 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1832 "Using crypto init string `%s'\n",
1833 plugin->crypto_init);
1835 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1836 "Using default crypto init string \n");
1839 GNUNET_CONFIGURATION_get_value_filename (plugin->env->cfg, plugin->name,
1840 "KEY_FILE", &key_file))
1843 GNUNET_asprintf (&key_file, "%s/%s", sh, "https_key.key");
1848 GNUNET_CONFIGURATION_get_value_filename (plugin->env->cfg, plugin->name,
1849 "CERT_FILE", &cert_file))
1852 GNUNET_asprintf (&cert_file, "%s/%s", sh, "https_cert.crt");
1855 /* read key & certificates from file */
1856 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1857 "Trying to loading TLS certificate from key-file `%s' cert-file`%s'\n",
1858 key_file, cert_file);
1860 plugin->key = server_load_file (key_file);
1861 plugin->cert = server_load_file (cert_file);
1863 if ((plugin->key == NULL) || (plugin->cert == NULL))
1865 struct GNUNET_OS_Process *cert_creation;
1867 GNUNET_free_non_null (plugin->key);
1869 GNUNET_free_non_null (plugin->cert);
1870 plugin->cert = NULL;
1872 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1873 "No usable TLS certificate found, creating certificate\n");
1876 GNUNET_OS_start_process (GNUNET_NO, GNUNET_OS_INHERIT_STD_OUT_AND_ERR, NULL, NULL,
1877 "gnunet-transport-certificate-creation",
1878 "gnunet-transport-certificate-creation",
1879 key_file, cert_file, NULL);
1880 if (cert_creation == NULL)
1882 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
1884 ("Could not create a new TLS certificate, program `gnunet-transport-certificate-creation' could not be started!\n"));
1885 GNUNET_free (key_file);
1886 GNUNET_free (cert_file);
1888 GNUNET_free_non_null (plugin->key);
1890 GNUNET_free_non_null (plugin->cert);
1891 plugin->cert = NULL;
1892 GNUNET_free_non_null (plugin->crypto_init);
1893 plugin->crypto_init = NULL;
1895 return GNUNET_SYSERR;
1897 GNUNET_assert (GNUNET_OK == GNUNET_OS_process_wait (cert_creation));
1898 GNUNET_OS_process_destroy (cert_creation);
1900 plugin->key = server_load_file (key_file);
1901 plugin->cert = server_load_file (cert_file);
1904 if ((plugin->key == NULL) || (plugin->cert == NULL))
1906 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
1908 ("No usable TLS certificate found and creating one failed!\n"),
1910 GNUNET_free (key_file);
1911 GNUNET_free (cert_file);
1913 GNUNET_free_non_null (plugin->key);
1915 GNUNET_free_non_null (plugin->cert);
1916 plugin->cert = NULL;
1917 GNUNET_free_non_null (plugin->crypto_init);
1918 plugin->crypto_init = NULL;
1920 return GNUNET_SYSERR;
1922 GNUNET_free (key_file);
1923 GNUNET_free (cert_file);
1924 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "TLS certificate loaded\n");
1931 * Start the HTTP server
1933 * @param plugin the plugin handle
1934 * @return GNUNET_OK on success, GNUNET_SYSERR on failure
1937 server_start (struct HTTP_Server_Plugin *plugin)
1939 unsigned int timeout;
1940 GNUNET_assert (NULL != plugin);
1943 if (GNUNET_SYSERR == server_load_certificate (plugin))
1945 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
1946 "Could not load or create server certificate! Loading plugin failed!\n");
1947 return GNUNET_SYSERR;
1952 #if MHD_VERSION >= 0x00090E00
1953 timeout = HTTP_SERVER_NOT_VALIDATED_TIMEOUT.rel_value / 1000;
1954 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1955 "MHD can set timeout per connection! Default time out %u sec.\n",
1958 timeout = SERVER_SESSION_TIMEOUT.rel_value / 1000;
1959 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, plugin->name,
1960 "MHD cannot set timeout per connection! Default time out %u sec.\n",
1963 plugin->server_v4 = NULL;
1964 if (plugin->use_ipv4 == GNUNET_YES)
1966 plugin->server_v4 = MHD_start_daemon (
1973 MHD_NO_FLAG, plugin->port,
1974 &server_accept_cb, plugin,
1975 &server_access_cb, plugin,
1976 MHD_OPTION_SOCK_ADDR,
1977 (struct sockaddr_in *)
1978 plugin->server_addr_v4,
1979 MHD_OPTION_CONNECTION_LIMIT,
1981 plugin->max_connections,
1983 MHD_OPTION_HTTPS_PRIORITIES,
1984 plugin->crypto_init,
1985 MHD_OPTION_HTTPS_MEM_KEY,
1987 MHD_OPTION_HTTPS_MEM_CERT,
1990 MHD_OPTION_CONNECTION_TIMEOUT,
1992 MHD_OPTION_CONNECTION_MEMORY_LIMIT,
1994 GNUNET_SERVER_MAX_MESSAGE_SIZE),
1995 MHD_OPTION_NOTIFY_COMPLETED,
1996 &server_disconnect_cb, plugin,
1997 MHD_OPTION_EXTERNAL_LOGGER,
1998 server_log, NULL, MHD_OPTION_END);
2000 plugin->server_v6 = NULL;
2001 if (plugin->use_ipv6 == GNUNET_YES)
2003 plugin->server_v6 = MHD_start_daemon (
2010 MHD_USE_IPv6, plugin->port,
2011 &server_accept_cb, plugin,
2012 &server_access_cb, plugin,
2013 MHD_OPTION_SOCK_ADDR,
2014 (struct sockaddr_in6 *)
2015 plugin->server_addr_v6,
2016 MHD_OPTION_CONNECTION_LIMIT,
2018 plugin->max_connections,
2020 MHD_OPTION_HTTPS_PRIORITIES,
2021 plugin->crypto_init,
2022 MHD_OPTION_HTTPS_MEM_KEY,
2024 MHD_OPTION_HTTPS_MEM_CERT,
2027 MHD_OPTION_CONNECTION_TIMEOUT,
2029 MHD_OPTION_CONNECTION_MEMORY_LIMIT,
2031 GNUNET_SERVER_MAX_MESSAGE_SIZE),
2032 MHD_OPTION_NOTIFY_COMPLETED,
2033 &server_disconnect_cb, plugin,
2034 MHD_OPTION_EXTERNAL_LOGGER,
2035 server_log, NULL, MHD_OPTION_END);
2039 if ((plugin->use_ipv4 == GNUNET_YES) && (plugin->server_v4 == NULL))
2041 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
2042 "Failed to start %s IPv4 server component on port %u\n",
2043 plugin->name, plugin->port);
2044 return GNUNET_SYSERR;
2046 server_reschedule (plugin, plugin->server_v4, GNUNET_NO);
2048 if ((plugin->use_ipv6 == GNUNET_YES) && (plugin->server_v6 == NULL))
2050 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
2051 "Failed to start %s IPv6 server component on port %u\n",
2052 plugin->name, plugin->port);
2053 return GNUNET_SYSERR;
2055 server_reschedule (plugin, plugin->server_v6, GNUNET_NO);
2056 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2057 "%s server component started on port %u\n", plugin->name,
2064 server_stop (struct HTTP_Server_Plugin *plugin)
2066 if (plugin->server_v4 != NULL)
2068 MHD_stop_daemon (plugin->server_v4);
2069 plugin->server_v4 = NULL;
2071 if ( plugin->server_v6 != NULL)
2073 MHD_stop_daemon (plugin->server_v6);
2074 plugin->server_v6 = NULL;
2078 if (plugin->server_v4_task != GNUNET_SCHEDULER_NO_TASK)
2080 GNUNET_SCHEDULER_cancel (plugin->server_v4_task);
2081 plugin->server_v4_task = GNUNET_SCHEDULER_NO_TASK;
2084 if (plugin->server_v6_task != GNUNET_SCHEDULER_NO_TASK)
2086 GNUNET_SCHEDULER_cancel (plugin->server_v6_task);
2087 plugin->server_v6_task = GNUNET_SCHEDULER_NO_TASK;
2092 GNUNET_free_non_null (plugin->crypto_init);
2093 GNUNET_free_non_null (plugin->cert);
2094 GNUNET_free_non_null (plugin->key);
2097 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2098 "%s server component stopped\n", plugin->name);
2103 * Add an address to the server's set of addresses and notify transport
2105 * @param cls the plugin handle
2106 * @param add_remove GNUNET_YES on add, GNUNET_NO on remove
2107 * @param addr the address
2108 * @param addrlen address length
2111 server_add_address (void *cls, int add_remove, const struct sockaddr *addr,
2114 struct HTTP_Server_Plugin *plugin = cls;
2115 struct HttpAddressWrapper *w = NULL;
2117 w = GNUNET_malloc (sizeof (struct HttpAddressWrapper));
2118 w->addr = http_common_address_from_socket (plugin->protocol, addr, addrlen);
2119 if (NULL == w->addr)
2124 w->addrlen = http_common_address_get_size (w->addr);
2126 GNUNET_CONTAINER_DLL_insert(plugin->addr_head, plugin->addr_tail, w);
2127 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2128 "Notifying transport to add address `%s'\n",
2129 http_common_plugin_address_to_string(NULL, w->addr, w->addrlen));
2131 plugin->env->notify_address (plugin->env->cls, add_remove, w->addr, w->addrlen, "https_client");
2133 plugin->env->notify_address (plugin->env->cls, add_remove, w->addr, w->addrlen, "http_client");
2139 * Remove an address from the server's set of addresses and notify transport
2141 * @param cls the plugin handle
2142 * @param add_remove GNUNET_YES on add, GNUNET_NO on remove
2143 * @param addr the address
2144 * @param addrlen address length
2147 server_remove_address (void *cls, int add_remove, const struct sockaddr *addr,
2150 struct HTTP_Server_Plugin *plugin = cls;
2151 struct HttpAddressWrapper *w = plugin->addr_head;
2153 void * saddr = http_common_address_from_socket (plugin->protocol, addr, addrlen);
2156 saddr_len = http_common_address_get_size (saddr);
2160 if (GNUNET_YES == http_common_cmp_addresses(w->addr, w->addrlen, saddr, saddr_len))
2164 GNUNET_free (saddr);
2169 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2170 "Notifying transport to remove address `%s'\n",
2171 http_common_plugin_address_to_string (NULL, w->addr, w->addrlen));
2172 GNUNET_CONTAINER_DLL_remove (plugin->addr_head, plugin->addr_tail, w);
2174 plugin->env->notify_address (plugin->env->cls, add_remove, w->addr, w->addrlen, "https_client");
2176 plugin->env->notify_address (plugin->env->cls, add_remove, w->addr, w->addrlen, "http_client");
2178 GNUNET_free (w->addr);
2185 * Our external IP address/port mapping has changed.
2187 * @param cls closure, the 'struct LocalAddrList'
2188 * @param add_remove GNUNET_YES to mean the new public IP address, GNUNET_NO to mean
2189 * the previous (now invalid) one
2190 * @param addr either the previous or the new public IP address
2191 * @param addrlen actual lenght of the address
2194 server_nat_port_map_callback (void *cls, int add_remove, const struct sockaddr *addr,
2197 GNUNET_assert (cls != NULL);
2198 struct HTTP_Server_Plugin *plugin = cls;
2200 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2201 "NAT called to %s address `%s'\n",
2202 (add_remove == GNUNET_NO) ? "remove" : "add",
2203 GNUNET_a2s (addr, addrlen));
2205 if (AF_INET == addr->sa_family)
2207 struct sockaddr_in *s4 = (struct sockaddr_in *) addr;
2209 if (GNUNET_NO == plugin->use_ipv4)
2212 if ((NULL != plugin->server_addr_v4) &&
2213 (0 != memcmp (&plugin->server_addr_v4->sin_addr,
2214 &s4->sin_addr, sizeof (struct in_addr))))
2216 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2217 "Skipping address `%s' (not bindto address)\n",
2218 GNUNET_a2s (addr, addrlen));
2223 if (AF_INET6 == addr->sa_family)
2225 struct sockaddr_in6 *s6 = (struct sockaddr_in6 *) addr;
2226 if (GNUNET_NO == plugin->use_ipv6)
2229 if ((NULL != plugin->server_addr_v6) &&
2230 (0 != memcmp (&plugin->server_addr_v6->sin6_addr,
2231 &s6->sin6_addr, sizeof (struct in6_addr))))
2233 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2234 "Skipping address `%s' (not bindto address)\n",
2235 GNUNET_a2s (addr, addrlen));
2243 server_add_address (cls, add_remove, addr, addrlen);
2246 server_remove_address (cls, add_remove, addr, addrlen);
2253 * Get valid server addresses
2255 * @param plugin the plugin handle
2256 * @param serviceName the servicename
2257 * @param cfg configuration handle
2258 * @param addrs addresses
2259 * @param addr_lens address length
2260 * @return number of addresses
2263 server_get_addresses (struct HTTP_Server_Plugin *plugin,
2264 const char *serviceName,
2265 const struct GNUNET_CONFIGURATION_Handle *cfg,
2266 struct sockaddr ***addrs, socklen_t ** addr_lens)
2269 unsigned long long port;
2270 struct addrinfo hints;
2271 struct addrinfo *res;
2272 struct addrinfo *pos;
2273 struct addrinfo *next;
2277 struct sockaddr **saddrs;
2278 socklen_t *saddrlens;
2284 disablev6 = !plugin->use_ipv6;
2287 if (GNUNET_CONFIGURATION_have_value (cfg, serviceName, "PORT"))
2289 GNUNET_break (GNUNET_OK ==
2290 GNUNET_CONFIGURATION_get_value_number (cfg, serviceName,
2294 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2296 ("Require valid port number for service in configuration!\n"));
2297 return GNUNET_SYSERR;
2302 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, plugin->name,
2303 "Starting in listen only mode\n");
2304 return -1; /* listen only */
2308 if (GNUNET_CONFIGURATION_have_value (cfg, serviceName, "BINDTO"))
2310 GNUNET_break (GNUNET_OK ==
2311 GNUNET_CONFIGURATION_get_value_string (cfg, serviceName,
2312 "BINDTO", &hostname));
2317 if (hostname != NULL)
2319 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2320 "Resolving `%s' since that is where `%s' will bind to.\n",
2321 hostname, serviceName);
2322 memset (&hints, 0, sizeof (struct addrinfo));
2324 hints.ai_family = AF_INET;
2325 if ((0 != (ret = getaddrinfo (hostname, NULL, &hints, &res))) ||
2328 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Failed to resolve `%s': %s\n"),
2329 hostname, gai_strerror (ret));
2330 GNUNET_free (hostname);
2331 return GNUNET_SYSERR;
2335 while (NULL != (pos = next))
2337 next = pos->ai_next;
2338 if ((disablev6) && (pos->ai_family == AF_INET6))
2344 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2345 _("Failed to find %saddress for `%s'.\n"),
2346 disablev6 ? "IPv4 " : "", hostname);
2348 GNUNET_free (hostname);
2349 return GNUNET_SYSERR;
2352 saddrs = GNUNET_malloc ((resi + 1) * sizeof (struct sockaddr *));
2353 saddrlens = GNUNET_malloc ((resi + 1) * sizeof (socklen_t));
2356 while (NULL != (pos = next))
2358 next = pos->ai_next;
2359 if ((disablev6) && (pos->ai_family == AF_INET6))
2361 if ((pos->ai_protocol != IPPROTO_TCP) && (pos->ai_protocol != 0))
2362 continue; /* not TCP */
2363 if ((pos->ai_socktype != SOCK_STREAM) && (pos->ai_socktype != 0))
2364 continue; /* huh? */
2365 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2366 "Service will bind to `%s'\n", GNUNET_a2s (pos->ai_addr,
2368 if (pos->ai_family == AF_INET)
2370 GNUNET_assert (pos->ai_addrlen == sizeof (struct sockaddr_in));
2371 saddrlens[i] = pos->ai_addrlen;
2372 saddrs[i] = GNUNET_malloc (saddrlens[i]);
2373 memcpy (saddrs[i], pos->ai_addr, saddrlens[i]);
2374 ((struct sockaddr_in *) saddrs[i])->sin_port = htons (port);
2378 GNUNET_assert (pos->ai_family == AF_INET6);
2379 GNUNET_assert (pos->ai_addrlen == sizeof (struct sockaddr_in6));
2380 saddrlens[i] = pos->ai_addrlen;
2381 saddrs[i] = GNUNET_malloc (saddrlens[i]);
2382 memcpy (saddrs[i], pos->ai_addr, saddrlens[i]);
2383 ((struct sockaddr_in6 *) saddrs[i])->sin6_port = htons (port);
2387 GNUNET_free (hostname);
2393 /* will bind against everything, just set port */
2399 saddrs = GNUNET_malloc ((resi + 1) * sizeof (struct sockaddr *));
2400 saddrlens = GNUNET_malloc ((resi + 1) * sizeof (socklen_t));
2402 saddrlens[i] = sizeof (struct sockaddr_in);
2403 saddrs[i] = GNUNET_malloc (saddrlens[i]);
2404 #if HAVE_SOCKADDR_IN_SIN_LEN
2405 ((struct sockaddr_in *) saddrs[i])->sin_len = saddrlens[i];
2407 ((struct sockaddr_in *) saddrs[i])->sin_family = AF_INET;
2408 ((struct sockaddr_in *) saddrs[i])->sin_port = htons (port);
2414 saddrs = GNUNET_malloc ((resi + 1) * sizeof (struct sockaddr *));
2415 saddrlens = GNUNET_malloc ((resi + 1) * sizeof (socklen_t));
2417 saddrlens[i] = sizeof (struct sockaddr_in6);
2418 saddrs[i] = GNUNET_malloc (saddrlens[i]);
2419 #if HAVE_SOCKADDR_IN_SIN_LEN
2420 ((struct sockaddr_in6 *) saddrs[i])->sin6_len = saddrlens[0];
2422 ((struct sockaddr_in6 *) saddrs[i])->sin6_family = AF_INET6;
2423 ((struct sockaddr_in6 *) saddrs[i])->sin6_port = htons (port);
2425 saddrlens[i] = sizeof (struct sockaddr_in);
2426 saddrs[i] = GNUNET_malloc (saddrlens[i]);
2427 #if HAVE_SOCKADDR_IN_SIN_LEN
2428 ((struct sockaddr_in *) saddrs[i])->sin_len = saddrlens[1];
2430 ((struct sockaddr_in *) saddrs[i])->sin_family = AF_INET;
2431 ((struct sockaddr_in *) saddrs[i])->sin_port = htons (port);
2435 *addr_lens = saddrlens;
2441 * Ask NAT for addresses
2443 * @param plugin the plugin handle
2446 server_start_report_addresses (struct HTTP_Server_Plugin *plugin)
2448 int res = GNUNET_OK;
2449 struct sockaddr **addrs;
2450 socklen_t *addrlens;
2452 res = server_get_addresses (plugin,
2453 plugin->name, plugin->env->cfg,
2455 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2456 _("Found %u addresses to report to NAT service\n"), res);
2458 if (GNUNET_SYSERR == res)
2465 GNUNET_NAT_register (plugin->env->cfg, GNUNET_YES, plugin->port,
2467 (const struct sockaddr **) addrs, addrlens,
2468 &server_nat_port_map_callback, NULL, plugin);
2472 GNUNET_assert (addrs[res] != NULL);
2473 GNUNET_free (addrs[res]);
2475 GNUNET_free_non_null (addrs);
2476 GNUNET_free_non_null (addrlens);
2481 * Stop NAT for addresses
2483 * @param plugin the plugin handle
2486 server_stop_report_addresses (struct HTTP_Server_Plugin *plugin)
2488 /* Stop NAT handle */
2489 if (NULL != plugin->nat)
2490 GNUNET_NAT_unregister (plugin->nat);
2492 /* Clean up addresses */
2493 struct HttpAddressWrapper *w;
2495 while (plugin->addr_head != NULL)
2497 w = plugin->addr_head;
2498 GNUNET_CONTAINER_DLL_remove (plugin->addr_head, plugin->addr_tail, w);
2499 GNUNET_free (w->addr);
2506 * Check if IPv6 supported on this system
2508 * @param plugin the plugin handle
2509 * @return GNUNET_YES on success, else GNUNET_NO
2512 server_check_ipv6_support (struct HTTP_Server_Plugin *plugin)
2514 struct GNUNET_NETWORK_Handle *desc = NULL;
2515 int res = GNUNET_NO;
2517 /* Probe IPv6 support */
2518 desc = GNUNET_NETWORK_socket_create (PF_INET6, SOCK_STREAM, 0);
2521 if ((errno == ENOBUFS) || (errno == ENOMEM) || (errno == ENFILE) ||
2524 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "socket");
2526 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, plugin->name,
2528 ("Disabling IPv6 since it is not supported on this system!\n"));
2533 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (desc));
2537 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2538 "Testing IPv6 on this system: %s\n",
2539 (res == GNUNET_YES) ? "successful" : "failed");
2545 * Function called when the service shuts down. Unloads our plugins
2546 * and cancels pending validations.
2548 * @param cls closure, unused
2549 * @param tc task context (unused)
2552 server_notify_external_hostname (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2554 struct HTTP_Server_Plugin *plugin = cls;
2556 plugin->notify_ext_task = GNUNET_SCHEDULER_NO_TASK;
2558 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
2561 GNUNET_asprintf(&plugin->ext_addr, "%s://%s", plugin->protocol, plugin->external_hostname);
2562 plugin->ext_addr_len = strlen (plugin->ext_addr) + 1;
2563 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2564 "Notifying transport about external hostname address `%s'\n", plugin->ext_addr);
2567 plugin->env->notify_address (plugin->env->cls, GNUNET_YES,
2568 plugin->ext_addr, plugin->ext_addr_len,
2571 plugin->env->notify_address (plugin->env->cls, GNUNET_YES,
2572 plugin->ext_addr, plugin->ext_addr_len,
2579 * Configure the plugin
2581 * @param plugin plugin handle
2582 * @return GNUNET_OK on success, GNUNET_SYSERR on failure
2585 server_configure_plugin (struct HTTP_Server_Plugin *plugin)
2587 unsigned long long port;
2588 unsigned long long max_connections;
2589 char *bind4_address = NULL;
2590 char *bind6_address = NULL;
2593 if (GNUNET_CONFIGURATION_have_value
2594 (plugin->env->cfg, plugin->name, "USE_IPv4"))
2597 GNUNET_CONFIGURATION_get_value_yesno (plugin->env->cfg, plugin->name,
2601 plugin->use_ipv4 = GNUNET_YES;
2602 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2603 _("IPv4 support is %s\n"),
2604 (plugin->use_ipv4 == GNUNET_YES) ? "enabled" : "disabled");
2607 if (GNUNET_CONFIGURATION_have_value
2608 (plugin->env->cfg, plugin->name, "USE_IPv6"))
2611 GNUNET_CONFIGURATION_get_value_yesno (plugin->env->cfg, plugin->name,
2615 plugin->use_ipv6 = GNUNET_YES;
2616 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2617 _("IPv6 support is %s\n"),
2618 (plugin->use_ipv6 == GNUNET_YES) ? "enabled" : "disabled");
2620 if ((plugin->use_ipv4 == GNUNET_NO) && (plugin->use_ipv6 == GNUNET_NO))
2622 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
2624 ("Neither IPv4 nor IPv6 are enabled! Fix in configuration\n"),
2626 return GNUNET_SYSERR;
2629 /* Reading port number from config file */
2631 GNUNET_CONFIGURATION_get_value_number (plugin->env->cfg, plugin->name,
2632 "PORT", &port)) || (port > 65535))
2634 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
2635 _("Port is required! Fix in configuration\n"),
2637 return GNUNET_SYSERR;
2639 plugin->port = port;
2641 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2642 _("Using port %u\n"), plugin->port);
2644 if ((plugin->use_ipv4 == GNUNET_YES) &&
2645 (GNUNET_YES == GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg,
2646 plugin->name, "BINDTO", &bind4_address)))
2648 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2649 "Binding %s plugin to specific IPv4 address: `%s'\n",
2650 plugin->protocol, bind4_address);
2651 plugin->server_addr_v4 = GNUNET_malloc (sizeof (struct sockaddr_in));
2652 if (1 != inet_pton (AF_INET, bind4_address,
2653 &plugin->server_addr_v4->sin_addr))
2655 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
2657 ("Specific IPv4 address `%s' in configuration file is invalid!\n"),
2659 GNUNET_free (bind4_address);
2660 GNUNET_free (plugin->server_addr_v4);
2661 plugin->server_addr_v4 = NULL;
2662 return GNUNET_SYSERR;
2666 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2667 _("Binding to IPv4 address %s\n"), bind4_address);
2668 plugin->server_addr_v4->sin_family = AF_INET;
2669 plugin->server_addr_v4->sin_port = htons (plugin->port);
2671 GNUNET_free (bind4_address);
2674 if ((plugin->use_ipv6 == GNUNET_YES) &&
2676 GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg, plugin->name,
2677 "BINDTO6", &bind6_address)))
2679 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2680 "Binding %s plugin to specific IPv6 address: `%s'\n",
2681 plugin->protocol, bind6_address);
2682 plugin->server_addr_v6 = GNUNET_malloc (sizeof (struct sockaddr_in6));
2684 inet_pton (AF_INET6, bind6_address, &plugin->server_addr_v6->sin6_addr))
2686 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
2688 ("Specific IPv6 address `%s' in configuration file is invalid!\n"),
2690 GNUNET_free (bind6_address);
2691 GNUNET_free (plugin->server_addr_v6);
2692 plugin->server_addr_v6 = NULL;
2693 return GNUNET_SYSERR;
2697 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2698 _("Binding to IPv6 address %s\n"), bind6_address);
2699 plugin->server_addr_v6->sin6_family = AF_INET6;
2700 plugin->server_addr_v6->sin6_port = htons (plugin->port);
2702 GNUNET_free (bind6_address);
2705 if (GNUNET_YES == GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg, plugin->name,
2706 "EXTERNAL_HOSTNAME", &plugin->external_hostname))
2709 if (NULL != strstr(plugin->external_hostname, "://"))
2711 tmp = strdup(&strstr(plugin->external_hostname, "://")[3]);
2712 GNUNET_free (plugin->external_hostname);
2713 plugin->external_hostname = tmp;
2716 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2717 _("Using external hostname `%s'\n"), plugin->external_hostname);
2718 plugin->notify_ext_task = GNUNET_SCHEDULER_add_now (&server_notify_external_hostname, plugin);
2720 /* Use only configured external hostname */
2721 if (GNUNET_CONFIGURATION_have_value
2722 (plugin->env->cfg, plugin->name, "EXTERNAL_HOSTNAME_ONLY"))
2724 plugin->external_only =
2725 GNUNET_CONFIGURATION_get_value_yesno (plugin->env->cfg, plugin->name,
2726 "EXTERNAL_HOSTNAME_ONLY");
2729 plugin->external_only = GNUNET_NO;
2731 if (GNUNET_YES == plugin->external_only)
2732 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2733 _("Notifying transport only about hostname `%s'\n"), plugin->external_hostname);
2736 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2737 "No external hostname configured\n");
2739 /* Optional parameters */
2740 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (plugin->env->cfg,
2742 "MAX_CONNECTIONS", &max_connections))
2743 max_connections = 128;
2744 plugin->max_connections = max_connections;
2746 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2747 _("Maximum number of connections is %u\n"),
2748 plugin->max_connections);
2751 plugin->peer_id_length = strlen (GNUNET_h2s_full (&plugin->env->my_identity->hashPubKey));
2758 * Session was idle, so disconnect it
2760 * @param cls the session
2761 * @param tc task context
2764 server_session_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2766 GNUNET_assert (NULL != cls);
2767 struct Session *s = cls;
2769 s->timeout_task = GNUNET_SCHEDULER_NO_TASK;
2770 GNUNET_log (TIMEOUT_LOG,
2771 "Session %p was idle for %llu ms, disconnecting\n",
2772 s, (unsigned long long) SERVER_SESSION_TIMEOUT.rel_value);
2774 /* call session destroy function */
2775 GNUNET_assert (GNUNET_OK == server_disconnect (s));
2780 * Start session timeout for session s
2782 * @param s the session
2785 server_start_session_timeout (struct Session *s)
2787 GNUNET_assert (NULL != s);
2788 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == s->timeout_task);
2789 s->timeout_task = GNUNET_SCHEDULER_add_delayed (SERVER_SESSION_TIMEOUT,
2790 &server_session_timeout,
2792 GNUNET_log (TIMEOUT_LOG,
2793 "Timeout for session %p set to %llu ms\n",
2794 s, (unsigned long long) SERVER_SESSION_TIMEOUT.rel_value);
2799 * Increment session timeout due to activity session s
2801 * @param s the session
2804 server_reschedule_session_timeout (struct Session *s)
2806 GNUNET_assert (NULL != s);
2807 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK != s->timeout_task);
2809 GNUNET_SCHEDULER_cancel (s->timeout_task);
2810 s->timeout_task = GNUNET_SCHEDULER_add_delayed (SERVER_SESSION_TIMEOUT,
2811 &server_session_timeout,
2813 GNUNET_log (TIMEOUT_LOG,
2814 "Timeout rescheduled for session %p set to %llu ms\n",
2815 s, (unsigned long long) SERVER_SESSION_TIMEOUT.rel_value);
2820 * Exit point from the plugin.
2826 LIBGNUNET_PLUGIN_TRANSPORT_DONE (void *cls)
2828 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
2829 struct HTTP_Server_Plugin *plugin = api->cls;
2830 struct Session *pos;
2831 struct Session *next;
2833 if (NULL == api->cls)
2835 /* Free for stub mode */
2839 plugin->in_shutdown = GNUNET_YES;
2840 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2841 _("Shutting down plugin `%s'\n"),
2844 if (GNUNET_SCHEDULER_NO_TASK != plugin->notify_ext_task)
2846 GNUNET_SCHEDULER_cancel (plugin->notify_ext_task);
2847 plugin->notify_ext_task = GNUNET_SCHEDULER_NO_TASK;
2850 if (NULL != plugin->ext_addr)
2852 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2853 "Notifying transport to remove address `%s'\n",
2854 http_common_plugin_address_to_string (NULL,
2856 plugin->ext_addr_len));
2858 plugin->env->notify_address (plugin->env->cls,
2861 plugin->ext_addr_len,
2864 plugin->env->notify_address (plugin->env->cls,
2867 plugin->ext_addr_len,
2873 /* Stop to report addresses to transport service */
2874 server_stop_report_addresses (plugin);
2875 server_stop (plugin);
2876 next = plugin->head;
2877 while (NULL != (pos = next))
2880 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2881 "Removing left over session %p\n", pos);
2883 if ((GNUNET_YES == pos->session_passed) && (GNUNET_NO == pos->session_ended))
2885 /* Notify transport immediately that this session is invalid */
2886 pos->session_ended = GNUNET_YES;
2887 plugin->env->session_end (plugin->env->cls, &pos->target, pos);
2890 server_delete_session (pos);
2894 GNUNET_free_non_null (plugin->external_hostname);
2895 GNUNET_free_non_null (plugin->ext_addr);
2896 GNUNET_free_non_null (plugin->server_addr_v4);
2897 GNUNET_free_non_null (plugin->server_addr_v6);
2899 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2900 _("Shutdown for plugin `%s' complete\n"),
2903 GNUNET_free (plugin);
2910 * Entry point for the plugin.
2916 LIBGNUNET_PLUGIN_TRANSPORT_INIT (void *cls)
2918 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
2919 struct GNUNET_TRANSPORT_PluginFunctions *api;
2920 struct HTTP_Server_Plugin *plugin;
2922 plugin = GNUNET_malloc (sizeof (struct HTTP_Server_Plugin));
2926 if (NULL == env->receive)
2928 /* run in 'stub' mode (i.e. as part of gnunet-peerinfo), don't fully
2929 initialze the plugin or the API */
2930 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
2932 api->address_to_string = &http_common_plugin_address_to_string;
2933 api->string_to_address = &http_common_plugin_string_to_address;
2934 api->address_pretty_printer = &http_common_plugin_address_pretty_printer;
2938 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
2940 api->send = &http_server_plugin_send;
2941 api->disconnect = &http_server_plugin_disconnect;
2942 api->check_address = &http_server_plugin_address_suggested;
2943 api->get_session = &http_server_plugin_get_session;
2945 api->address_to_string = &http_common_plugin_address_to_string;
2946 api->string_to_address = &http_common_plugin_string_to_address;
2947 api->address_pretty_printer = &http_common_plugin_address_pretty_printer;
2950 plugin->name = "transport-https_server";
2951 plugin->protocol = "https";
2953 plugin->name = "transport-http_server";
2954 plugin->protocol = "http";
2957 /* Configure plugin */
2958 if (GNUNET_SYSERR == server_configure_plugin (plugin))
2960 LIBGNUNET_PLUGIN_TRANSPORT_DONE (api);
2964 /* Check IPv6 support */
2965 if (GNUNET_YES == plugin->use_ipv6)
2966 plugin->use_ipv6 = server_check_ipv6_support (plugin);
2968 /* Report addresses to transport service */
2969 if (GNUNET_NO == plugin->external_only)
2970 server_start_report_addresses (plugin);
2972 if (GNUNET_SYSERR == server_start (plugin))
2974 LIBGNUNET_PLUGIN_TRANSPORT_DONE (api);
2981 /* end of plugin_transport_http_server.c */