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 * Continuation function to call once the transmission buffer
402 * has again space available. NULL if there is no
403 * continuation to call.
405 GNUNET_TRANSPORT_TransmitContinuation transmit_cont;
408 * Closure for transmit_cont.
410 void *transmit_cont_cls;
415 * The http_server plugin handle
417 static struct HTTP_Server_Plugin * p;
421 * Start session timeout for session s
422 * @param s the session
425 server_start_session_timeout (struct Session *s);
429 * Increment session timeout due to activity for session s
430 * @param s the session
433 server_reschedule_session_timeout (struct Session *s);
437 * Cancel timeout for session s
438 * @param s the session
441 server_stop_session_timeout (struct Session *s);
445 * Disconnect a session s
446 * @param s the session
449 server_disconnect (struct Session *s);
453 * Does session s exist?
455 * @param plugin the plugin handle
456 * @param s the session
457 * @return GNUNET_YES on success, GNUNET_NO on error
460 server_exist_session (struct HTTP_Server_Plugin *plugin, struct Session *s);
464 * Reschedule the execution of both IPv4 and IPv6 server
465 * @param plugin the plugin
466 * @param server which server to schedule v4 or v6?
467 * @param now GNUNET_YES to schedule execution immediately, GNUNET_NO to wait
471 server_reschedule (struct HTTP_Server_Plugin *plugin, struct MHD_Daemon *server,
476 * Function that can be used by the transport service to transmit
477 * a message using the plugin. Note that in the case of a
478 * peer disconnecting, the continuation MUST be called
479 * prior to the disconnect notification itself. This function
480 * will be called with this peer's HELLO message to initiate
481 * a fresh connection to another peer.
484 * @param session which session must be used
485 * @param msgbuf the message to transmit
486 * @param msgbuf_size number of bytes in 'msgbuf'
487 * @param priority how important is the message (most plugins will
488 * ignore message priority and just FIFO)
489 * @param to how long to wait at most for the transmission (does not
490 * require plugins to discard the message after the timeout,
491 * just advisory for the desired delay; most plugins will ignore
493 * @param cont continuation to call once the message has
494 * been transmitted (or if the transport is ready
495 * for the next transmission call; or if the
496 * peer disconnected...); can be NULL
497 * @param cont_cls closure for cont
498 * @return number of bytes used (on the physical network, with overheads);
499 * -1 on hard errors (i.e. address invalid); 0 is a legal value
500 * and does NOT mean that the message was not transmitted (DV)
503 http_server_plugin_send (void *cls,
504 struct Session *session,
505 const char *msgbuf, size_t msgbuf_size,
506 unsigned int priority,
507 struct GNUNET_TIME_Relative to,
508 GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
510 struct HTTP_Server_Plugin *plugin = cls;
511 struct HTTP_Message *msg;
514 GNUNET_assert (plugin != NULL);
515 GNUNET_assert (session != NULL);
517 if (GNUNET_NO == server_exist_session (plugin, session))
520 return GNUNET_SYSERR;
522 if (NULL == session->server_send)
525 return GNUNET_SYSERR;
529 if (GNUNET_YES == session->server_send->disconnect)
530 return GNUNET_SYSERR;
532 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, session->plugin->name,
533 "Session %p/connection %p: Sending message with %u to peer `%s' with \n",
534 session, session->server_send,
535 msgbuf_size, GNUNET_i2s (&session->target));
537 /* create new message and schedule */
538 bytes_sent = sizeof (struct HTTP_Message) + msgbuf_size;
539 msg = GNUNET_malloc (bytes_sent);
541 msg->size = msgbuf_size;
543 msg->buf = (char *) &msg[1];
544 msg->transmit_cont = cont;
545 msg->transmit_cont_cls = cont_cls;
546 memcpy (msg->buf, msgbuf, msgbuf_size);
548 GNUNET_CONTAINER_DLL_insert_tail (session->msg_head, session->msg_tail, msg);
550 server_reschedule (session->plugin, session->server_send->mhd_daemon, GNUNET_YES);
551 server_reschedule_session_timeout (session);
553 /* struct Plugin *plugin = cls; */
560 * Function that can be used to force the plugin to disconnect
561 * from the given peer and cancel all previous transmissions
562 * (and their continuationc).
565 * @param target peer from which to disconnect
568 http_server_plugin_disconnect (void *cls, const struct GNUNET_PeerIdentity *target)
570 struct HTTP_Server_Plugin *plugin = cls;
571 struct Session *next = NULL;
572 struct Session *pos = NULL;
574 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
575 "Transport tells me to disconnect `%s'\n",
576 GNUNET_i2s (target));
579 while (NULL != (pos = next))
582 if (0 == memcmp (target, &pos->target, sizeof (struct GNUNET_PeerIdentity)))
584 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
585 "Disconnecting session %p to `%s'\n",
586 pos, GNUNET_i2s (target));
587 server_disconnect (pos);
595 * Another peer has suggested an address for this
596 * peer and transport plugin. Check that this could be a valid
597 * address. If so, consider adding it to the list
601 * @param addr pointer to the address
602 * @param addrlen length of addr
603 * @return GNUNET_OK if this is a plausible address for this peer
607 http_server_plugin_address_suggested (void *cls, const void *addr,
610 struct HTTP_Server_Plugin *plugin = cls;
611 struct HttpAddressWrapper *next;
612 struct HttpAddressWrapper *pos;
615 if ((NULL != plugin->ext_addr) &&
616 GNUNET_YES == (http_common_cmp_addresses (addr, addrlen,
617 plugin->ext_addr, plugin->ext_addr_len)))
620 next = plugin->addr_head;
621 while (NULL != (pos = next))
624 if (GNUNET_YES == (http_common_cmp_addresses(addr,
637 * Creates a new outbound session the transport
638 * service will use to send data to the peer
640 * Since HTTP/S server cannot create sessions, always return NULL
642 * @param cls the plugin
643 * @param address the address
644 * @return always NULL
646 static struct Session *
647 http_server_plugin_get_session (void *cls,
648 const struct GNUNET_HELLO_Address *address)
655 * Deleting the session
656 * Must not be used afterwards
658 * @param s the session to delete
661 server_delete_session (struct Session *s)
663 struct HTTP_Server_Plugin *plugin = s->plugin;
664 server_stop_session_timeout(s);
666 GNUNET_CONTAINER_DLL_remove (plugin->head, plugin->tail, s);
667 struct HTTP_Message *msg = s->msg_head;
668 struct HTTP_Message *tmp = NULL;
674 GNUNET_CONTAINER_DLL_remove (s->msg_head, s->msg_tail, msg);
675 if (msg->transmit_cont != NULL)
677 msg->transmit_cont (msg->transmit_cont_cls, &s->target, GNUNET_SYSERR);
683 if (s->msg_tk != NULL)
685 GNUNET_SERVER_mst_destroy (s->msg_tk);
688 GNUNET_free (s->addr);
689 GNUNET_free_non_null (s->server_recv);
690 GNUNET_free_non_null (s->server_send);
693 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
694 "Session %p destroyed\n", s);
699 * Cancel timeout for session s
701 * @param s the session
704 server_stop_session_timeout (struct Session *s)
706 GNUNET_assert (NULL != s);
708 if (GNUNET_SCHEDULER_NO_TASK != s->timeout_task)
710 GNUNET_SCHEDULER_cancel (s->timeout_task);
711 s->timeout_task = GNUNET_SCHEDULER_NO_TASK;
712 GNUNET_log (TIMEOUT_LOG, "Timeout stopped for session %p\n", s);
718 * Function that queries MHD's select sets and
719 * starts the task waiting for them.
720 * @param plugin plugin
721 * @param daemon_handle the MHD daemon handle
722 * @param now schedule immediately
723 * @return gnunet task identifier
725 static GNUNET_SCHEDULER_TaskIdentifier
726 server_schedule (struct HTTP_Server_Plugin *plugin,
727 struct MHD_Daemon *daemon_handle,
732 * Reschedule the execution of both IPv4 and IPv6 server
733 * @param plugin the plugin
734 * @param server which server to schedule v4 or v6?
735 * @param now GNUNET_YES to schedule execution immediately, GNUNET_NO to wait
739 server_reschedule (struct HTTP_Server_Plugin *plugin, struct MHD_Daemon *server,
742 if ((server == plugin->server_v4) && (plugin->server_v4 != NULL))
744 if (GNUNET_YES == plugin->server_v4_immediately)
745 return; /* No rescheduling, server will run asap */
747 if (GNUNET_YES == now)
748 plugin->server_v4_immediately = GNUNET_YES;
750 if (plugin->server_v4_task != GNUNET_SCHEDULER_NO_TASK)
752 GNUNET_SCHEDULER_cancel (plugin->server_v4_task);
753 plugin->server_v4_task = GNUNET_SCHEDULER_NO_TASK;
755 plugin->server_v4_task = server_schedule (plugin, plugin->server_v4, now);
758 if ((server == plugin->server_v6) && (plugin->server_v6 != NULL))
760 if (GNUNET_YES == plugin->server_v6_immediately)
761 return; /* No rescheduling, server will run asap */
763 if (GNUNET_YES == now)
764 plugin->server_v6_immediately = GNUNET_YES;
766 if (plugin->server_v6_task != GNUNET_SCHEDULER_NO_TASK)
768 GNUNET_SCHEDULER_cancel (plugin->server_v6_task);
769 plugin->server_v6_task = GNUNET_SCHEDULER_NO_TASK;
771 plugin->server_v6_task = server_schedule (plugin, plugin->server_v6, now);
777 * Disconnect session s
779 * @param s the session
780 * @return GNUNET_OK on success
783 server_disconnect (struct Session *s)
785 struct ServerConnection * send = NULL;
786 struct ServerConnection * recv = NULL;
788 if (GNUNET_NO == server_exist_session (p, s))
791 return GNUNET_SYSERR;
794 send = (struct ServerConnection *) s->server_send;
795 if (s->server_send != NULL)
797 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, s->plugin->name,
798 "Server: %p / %p Terminating inbound PUT session to peer `%s'\n",
799 s, s->server_send, GNUNET_i2s (&s->target));
801 send->disconnect = GNUNET_YES;
802 #if MHD_VERSION >= 0x00090E00
803 MHD_set_connection_option (send->mhd_conn, MHD_CONNECTION_OPTION_TIMEOUT,
806 server_reschedule (s->plugin, send->mhd_daemon, GNUNET_YES);
809 recv = (struct ServerConnection *) s->server_recv;
812 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, s->plugin->name,
813 "Server: %p / %p Terminating inbound GET session to peer `%s'\n",
814 s, s->server_recv, GNUNET_i2s (&s->target));
816 recv->disconnect = GNUNET_YES;
817 #if MHD_VERSION >= 0x00090E00
818 MHD_set_connection_option (recv->mhd_conn, MHD_CONNECTION_OPTION_TIMEOUT,
821 server_reschedule (s->plugin, recv->mhd_daemon, GNUNET_YES);
827 server_mhd_connection_timeout (struct HTTP_Server_Plugin *plugin, struct Session *s, int to)
829 #if MHD_VERSION >= 0x00090E00
830 /* Setting timeouts for other connections */
831 if (s->server_recv != NULL)
833 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
834 "Setting timeout for %p to %u sec.\n", s->server_recv, to);
835 MHD_set_connection_option (s->server_recv->mhd_conn,
836 MHD_CONNECTION_OPTION_TIMEOUT,
838 server_reschedule (plugin, s->server_recv->mhd_daemon, GNUNET_NO);
840 if (s->server_send != NULL)
842 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
843 "Setting timeout for %p to %u sec.\n", s->server_send, to);
844 MHD_set_connection_option (s->server_send->mhd_conn,
845 MHD_CONNECTION_OPTION_TIMEOUT,
847 server_reschedule (plugin, s->server_send->mhd_daemon, GNUNET_NO);
853 * Parse incoming URL for tag and target
855 * @param plugin plugin
856 * @param url incoming url
857 * @param target where to store the target
858 * @param tag where to store the tag
859 * @return GNUNET_OK on success, GNUNET_SYSERR on error
863 server_parse_url (struct HTTP_Server_Plugin *plugin, const char * url, struct GNUNET_PeerIdentity * target, uint32_t *tag)
865 int debug = GNUNET_YES;
867 char * tag_start = NULL;
868 char * tag_end = NULL;
869 char * target_start = NULL;
870 char * separator = NULL;
871 char hash[plugin->peer_id_length+1];
875 * URL is valid if it is in the form [prefix with (multiple) '/'][peerid[103];tag]*/
880 return GNUNET_SYSERR;
885 separator = strrchr (url, ';');
887 if (NULL == separator)
889 if (debug) GNUNET_break (0);
890 return GNUNET_SYSERR;
892 tag_start = separator + 1;
894 if (strlen (tag_start) == 0)
896 /* No tag after separator */
897 if (debug) GNUNET_break (0);
898 return GNUNET_SYSERR;
900 (*tag) = strtoul (tag_start, &tag_end, 10);
903 /* tag == 0 , invalid */
904 if (debug) GNUNET_break (0);
905 return GNUNET_SYSERR;
907 if (((*tag) == ULONG_MAX) && (ERANGE == errno))
909 /* out of range: > ULONG_MAX */
910 if (debug) GNUNET_break (0);
911 return GNUNET_SYSERR;
915 /* no char after tag */
916 if (debug) GNUNET_break (0);
917 return GNUNET_SYSERR;
919 if (url[strlen(url)] != tag_end[0])
921 /* there are more not converted chars after tag */
922 if (debug) GNUNET_break (0);
923 return GNUNET_SYSERR;
926 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
927 "Found tag `%u' in url\n", (*tag));
929 /* convert peer id */
930 target_start = strrchr (url, '/');
931 if (NULL == target_start)
934 target_start = (char *) url;
937 hash_length = separator - target_start;
938 if (hash_length != plugin->peer_id_length)
940 /* no char after tag */
941 if (debug) GNUNET_break (0);
942 return GNUNET_SYSERR;
944 memcpy (hash, target_start, hash_length);
945 hash[hash_length] = '\0';
947 if (GNUNET_OK != GNUNET_CRYPTO_hash_from_string ((const char *) hash, &(target->hashPubKey)))
949 /* hash conversion failed */
950 if (debug) GNUNET_break (0);
951 return GNUNET_SYSERR;
954 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
955 "Found target `%s' in url\n", GNUNET_h2s_full(&target->hashPubKey));
961 * Lookup a mhd connection and create one if none is found
963 * @param plugin the plugin handle
964 * @param mhd_connection the incoming mhd_connection
965 * @param url incoming requested URL
966 * @param method PUT or GET
967 * @return the server connecetion
969 static struct ServerConnection *
970 server_lookup_connection (struct HTTP_Server_Plugin *plugin,
971 struct MHD_Connection *mhd_connection, const char *url,
974 struct Session *s = NULL;
975 struct ServerConnection *sc = NULL;
976 const union MHD_ConnectionInfo *conn_info;
977 struct GNUNET_ATS_Information ats;
982 struct GNUNET_PeerIdentity target;
984 int direction = GNUNET_SYSERR;
987 conn_info = MHD_get_connection_info (mhd_connection,
988 MHD_CONNECTION_INFO_CLIENT_ADDRESS);
989 if ((conn_info->client_addr->sa_family != AF_INET) &&
990 (conn_info->client_addr->sa_family != AF_INET6))
992 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
993 "New %s connection from %s\n", method, url);
995 if (GNUNET_SYSERR == server_parse_url (plugin, url, &target, &tag))
997 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
998 "Invalid url %s\n", url);
1001 if (0 == strcmp (MHD_HTTP_METHOD_PUT, method))
1002 direction = _RECEIVE;
1003 else if (0 == strcmp (MHD_HTTP_METHOD_GET, method))
1007 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1008 "Invalid method %s connection from %s\n", method, url);
1012 plugin->cur_connections++;
1013 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1014 "New %s connection from %s with tag %u (%u of %u)\n",
1016 GNUNET_i2s (&target), tag,
1017 plugin->cur_connections, plugin->max_connections);
1018 /* find duplicate session */
1022 if ((0 == memcmp (&s->target, &target, sizeof (struct GNUNET_PeerIdentity))) &&
1029 if ((_RECEIVE == direction) && (NULL != s->server_recv))
1031 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1032 "Duplicate PUT connection from `%s' tag %u, dismissing new connection\n",
1033 GNUNET_i2s (&target),
1038 if ((_SEND == direction) && (NULL != s->server_send))
1040 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1041 "Duplicate GET connection from `%s' tag %u, dismissing new connection\n",
1042 GNUNET_i2s (&target),
1049 /* create new session */
1050 switch (conn_info->client_addr->sa_family)
1053 addr = http_common_address_from_socket (plugin->protocol, conn_info->client_addr, sizeof (struct sockaddr_in));
1054 addr_len = http_common_address_get_size (addr);
1055 ats = plugin->env->get_address_type (plugin->env->cls, conn_info->client_addr, sizeof (struct sockaddr_in));
1058 addr = http_common_address_from_socket (plugin->protocol, conn_info->client_addr, sizeof (struct sockaddr_in6));
1059 addr_len = http_common_address_get_size (addr);
1060 ats = plugin->env->get_address_type (plugin->env->cls, conn_info->client_addr, sizeof (struct sockaddr_in6));
1066 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1067 "Creating new session for peer `%s' connecting from `%s'\n",
1068 GNUNET_i2s (&target),
1069 http_common_plugin_address_to_string (NULL, addr, addr_len));
1071 s = GNUNET_malloc (sizeof (struct Session));
1072 memcpy (&s->target, &target, sizeof (struct GNUNET_PeerIdentity));
1075 s->addrlen = addr_len;
1076 s->ats_address_network_type = ats.value;
1077 s->next_receive = GNUNET_TIME_UNIT_ZERO_ABS;
1079 s->server_recv = NULL;
1080 s->server_send = NULL;
1081 s->session_passed = GNUNET_NO;
1082 s->session_ended = GNUNET_NO;
1083 server_start_session_timeout(s);
1084 GNUNET_CONTAINER_DLL_insert (plugin->head, plugin->tail, s);
1086 sc = GNUNET_malloc (sizeof (struct ServerConnection));
1087 if (conn_info->client_addr->sa_family == AF_INET)
1088 sc->mhd_daemon = plugin->server_v4;
1089 if (conn_info->client_addr->sa_family == AF_INET6)
1090 sc->mhd_daemon = plugin->server_v6;
1091 sc->mhd_conn = mhd_connection;
1092 sc->direction = direction;
1093 sc->connected = GNUNET_NO;
1095 if (direction == _SEND)
1096 s->server_send = sc;
1097 if (direction == _RECEIVE)
1098 s->server_recv = sc;
1099 #if MHD_VERSION >= 0x00090E00
1100 if ((NULL == s->server_recv) || (NULL == s->server_send))
1102 to = (HTTP_SERVER_NOT_VALIDATED_TIMEOUT.rel_value / 1000);
1103 MHD_set_connection_option (mhd_connection, MHD_CONNECTION_OPTION_TIMEOUT, to);
1104 server_reschedule (plugin, sc->mhd_daemon, GNUNET_NO);
1108 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1109 "Session %p for peer `%s' fully connected\n",
1110 s, GNUNET_i2s (&target));
1111 to = (SERVER_SESSION_TIMEOUT.rel_value / 1000);
1112 server_mhd_connection_timeout (plugin, s, to);
1115 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1116 "Setting timeout for %p to %u sec.\n", sc, to);
1123 * Lookup a session for a server connection
1125 * @param plugin the plugin
1126 * @param sc the server connection
1127 * @return the session found or NULL
1129 static struct Session *
1130 server_lookup_session (struct HTTP_Server_Plugin *plugin,
1131 struct ServerConnection * sc)
1135 for (s = plugin->head; NULL != s; s = s->next)
1136 if ((s->server_recv == sc) || (s->server_send == sc))
1142 server_exist_session (struct HTTP_Server_Plugin *plugin, struct Session *s)
1144 struct Session * head;
1146 GNUNET_assert (NULL != plugin);
1147 GNUNET_assert (NULL != s);
1149 for (head = plugin->head; head != NULL; head = head->next)
1159 * Callback called by MHD when it needs data to send
1161 * @param cls current session
1162 * @param pos position in buffer
1163 * @param buf the buffer to write data to
1164 * @param max max number of bytes available in buffer
1165 * @return bytes written to buffer
1168 server_send_callback (void *cls, uint64_t pos, char *buf, size_t max)
1170 struct Session *s = cls;
1171 ssize_t bytes_read = 0;
1172 struct HTTP_Message *msg;
1174 GNUNET_assert (NULL != p);
1175 if (GNUNET_NO == server_exist_session (p, s))
1181 bytes_read = GNUNET_MIN (msg->size - msg->pos,
1183 memcpy (buf, &msg->buf[msg->pos], bytes_read);
1184 msg->pos += bytes_read;
1186 /* removing message */
1187 if (msg->pos == msg->size)
1189 GNUNET_CONTAINER_DLL_remove (s->msg_head, s->msg_tail, msg);
1190 if (NULL != msg->transmit_cont)
1191 msg->transmit_cont (msg->transmit_cont_cls, &s->target, GNUNET_OK);
1196 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, s->plugin->name,
1197 "Sent %u bytes to peer `%s' with session %p \n", bytes_read, GNUNET_i2s (&s->target), s);
1203 * Callback called by MessageStreamTokenizer when a message has arrived
1205 * @param cls current session as closure
1206 * @param client client
1207 * @param message the message to be forwarded to transport service
1211 server_receive_mst_cb (void *cls, void *client,
1212 const struct GNUNET_MessageHeader *message)
1214 struct Session *s = cls;
1215 struct GNUNET_ATS_Information atsi[2];
1216 struct GNUNET_TIME_Relative delay;
1218 GNUNET_assert (NULL != p);
1219 if (GNUNET_NO == server_exist_session(p, s))
1222 struct HTTP_Server_Plugin *plugin = s->plugin;
1224 atsi[0].type = htonl (GNUNET_ATS_QUALITY_NET_DISTANCE);
1225 atsi[0].value = htonl (1);
1226 atsi[1].type = htonl (GNUNET_ATS_NETWORK_TYPE);
1227 atsi[1].value = s->ats_address_network_type;
1228 GNUNET_break (s->ats_address_network_type != ntohl (GNUNET_ATS_NET_UNSPECIFIED));
1230 delay = plugin->env->receive (plugin->env->cls,
1233 (const struct GNUNET_ATS_Information *) &atsi, 2,
1234 s, s->addr, s->addrlen);
1235 s->session_passed = GNUNET_YES;
1236 s->next_receive = GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (), delay);
1237 if (delay.rel_value > 0)
1239 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1240 "Peer `%s' address `%s' next read delayed for %llu ms\n",
1241 GNUNET_i2s (&s->target),
1242 http_common_plugin_address_to_string (NULL, s->addr, s->addrlen),
1245 server_reschedule_session_timeout (s);
1251 * MHD callback for a new incoming connection
1253 * @param cls the plugin handle
1254 * @param mhd_connection the mhd connection
1255 * @param url the requested URL
1256 * @param method GET or PUT
1257 * @param version HTTP version
1258 * @param upload_data upload data
1259 * @param upload_data_size sizeof upload data
1260 * @param httpSessionCache the session cache to remember the connection
1261 * @return MHD_YES if connection is accepted, MHD_NO on reject
1264 server_access_cb (void *cls, struct MHD_Connection *mhd_connection,
1265 const char *url, const char *method, const char *version,
1266 const char *upload_data, size_t * upload_data_size,
1267 void **httpSessionCache)
1269 struct HTTP_Server_Plugin *plugin = cls;
1272 struct ServerConnection *sc = *httpSessionCache;
1274 struct MHD_Response *response;
1276 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1277 _("Access from connection %p (%u of %u) for `%s' `%s' url `%s' with upload data size %u\n"),
1279 plugin->cur_connections, plugin->max_connections,
1280 method, version, url, (*upload_data_size));
1282 GNUNET_assert (cls != NULL);
1285 /* new connection */
1286 sc = server_lookup_connection (plugin, mhd_connection, url, method);
1289 (*httpSessionCache) = sc;
1293 response = MHD_create_response_from_data (strlen (HTTP_ERROR_RESPONSE), HTTP_ERROR_RESPONSE, MHD_NO, MHD_NO);
1294 res = MHD_queue_response (mhd_connection, MHD_HTTP_NOT_FOUND, response);
1295 MHD_destroy_response (response);
1301 /* 'old' connection */
1302 if (NULL == server_lookup_session (plugin, sc))
1304 /* Session was already disconnected */
1309 /* existing connection */
1310 sc = (*httpSessionCache);
1312 GNUNET_assert (NULL != s);
1313 /* connection is to be disconnected */
1314 if (sc->disconnect == GNUNET_YES)
1316 /* Sent HTTP/1.1: 200 OK as response */
1317 response = MHD_create_response_from_data (strlen ("Thank you!"),
1320 res = MHD_queue_response (mhd_connection, MHD_HTTP_OK, response);
1321 MHD_destroy_response (response);
1324 GNUNET_assert (s != NULL);
1326 if (sc->direction == _SEND)
1328 response = MHD_create_response_from_callback (MHD_SIZE_UNKNOWN,
1330 &server_send_callback, s,
1332 MHD_queue_response (mhd_connection, MHD_HTTP_OK, response);
1333 MHD_destroy_response (response);
1336 if (sc->direction == _RECEIVE)
1338 if ((*upload_data_size == 0) && (sc->connected == GNUNET_NO))
1340 /* (*upload_data_size == 0) first callback when header are passed */
1341 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1342 "Session %p / Connection %p: Peer `%s' PUT on address `%s' connected\n",
1344 GNUNET_i2s (&s->target),
1345 http_common_plugin_address_to_string (NULL,
1348 sc->connected = GNUNET_YES;
1351 else if ((*upload_data_size == 0) && (sc->connected == GNUNET_YES))
1353 /* (*upload_data_size == 0) when upload is complete */
1354 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1355 "Session %p / Connection %p: Peer `%s' PUT on address `%s' finished upload\n",
1357 GNUNET_i2s (&s->target),
1358 http_common_plugin_address_to_string (NULL,
1361 sc->connected = GNUNET_NO;
1362 /* Sent HTTP/1.1: 200 OK as PUT Response\ */
1363 response = MHD_create_response_from_data (strlen ("Thank you!"),
1366 res = MHD_queue_response (mhd_connection, MHD_HTTP_OK, response);
1367 MHD_destroy_response (response);
1370 else if ((*upload_data_size > 0) && (sc->connected == GNUNET_YES))
1372 /* (*upload_data_size > 0) for every segment received */
1373 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1374 "Session %p / Connection %p: Peer `%s' PUT on address `%s' received %u bytes\n",
1376 GNUNET_i2s (&s->target),
1377 http_common_plugin_address_to_string (NULL,
1381 struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get ();
1383 if ((s->next_receive.abs_value <= now.abs_value))
1385 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1386 "PUT with %u bytes forwarded to MST\n",
1388 if (s->msg_tk == NULL)
1390 s->msg_tk = GNUNET_SERVER_mst_create (&server_receive_mst_cb, s);
1392 GNUNET_SERVER_mst_receive (s->msg_tk, s, upload_data,
1393 *upload_data_size, GNUNET_NO, GNUNET_NO);
1394 #if MHD_VERSION >= 0x00090E00
1395 server_mhd_connection_timeout (plugin, s, GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT.rel_value / 1000);
1397 (*upload_data_size) = 0;
1401 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1402 "Session %p / Connection %p: no inbound bandwidth available! Next read was delayed by %llu ms\n",
1403 s, sc, now.abs_value - s->next_receive.abs_value);
1418 * Callback from MHD when a connection disconnects
1420 * @param cls closure
1421 * @param connection the disconnected MHD connection
1422 * @param httpSessionCache the pointer to distinguish
1425 server_disconnect_cb (void *cls, struct MHD_Connection *connection,
1426 void **httpSessionCache)
1428 struct ServerConnection *sc = *httpSessionCache;
1429 struct Session *s = NULL;
1430 struct Session *t = NULL;
1431 struct HTTP_Server_Plugin *plugin = NULL;
1433 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, p->name,
1434 "Disconnect for connection %p \n", sc);
1439 if (NULL == (s = server_lookup_session (p, sc)))
1442 GNUNET_assert (NULL != p);
1443 for (t = p->head; t != NULL; t = t->next)
1452 if (sc->direction == _SEND)
1455 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1456 "Peer `%s' connection %p, GET on address `%s' disconnected\n",
1457 GNUNET_i2s (&s->target), s->server_send,
1458 http_common_plugin_address_to_string (NULL, s->addr, s->addrlen));
1459 s->server_send = NULL;
1460 if (NULL != (s->server_recv))
1462 s->server_recv->disconnect = GNUNET_YES;
1463 GNUNET_assert (NULL != s->server_recv->mhd_conn);
1464 #if MHD_VERSION >= 0x00090E00
1465 MHD_set_connection_option (s->server_recv->mhd_conn, MHD_CONNECTION_OPTION_TIMEOUT,
1468 server_reschedule (plugin, s->server_recv->mhd_daemon, GNUNET_NO);
1471 if (sc->direction == _RECEIVE)
1473 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1474 "Peer `%s' connection %p PUT on address `%s' disconnected\n",
1475 GNUNET_i2s (&s->target), s->server_recv,
1476 http_common_plugin_address_to_string (NULL, s->addr, s->addrlen));
1477 s->server_recv = NULL;
1478 /* Do not terminate session when PUT disconnects
1479 if (NULL != (s->server_send))
1481 s->server_send->disconnect = GNUNET_YES;
1482 GNUNET_assert (NULL != s->server_send->mhd_conn);
1483 #if MHD_VERSION >= 0x00090E00
1484 MHD_set_connection_option (s->server_send->mhd_conn, MHD_CONNECTION_OPTION_TIMEOUT,
1487 server_reschedule (plugin, s->server_send->mhd_daemon, GNUNET_NO);
1489 if (s->msg_tk != NULL)
1491 GNUNET_SERVER_mst_destroy (s->msg_tk);
1497 plugin->cur_connections--;
1499 if ((s->server_send == NULL) && (s->server_recv == NULL))
1501 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1502 "Peer `%s' on address `%s' disconnected\n",
1503 GNUNET_i2s (&s->target),
1504 http_common_plugin_address_to_string (NULL, s->addr, s->addrlen));
1506 if ((GNUNET_YES == s->session_passed) && (GNUNET_NO == s->session_ended))
1508 /* Notify transport immediately that this session is invalid */
1509 s->session_ended = GNUNET_YES;
1510 plugin->env->session_end (plugin->env->cls, &s->target, s);
1512 server_delete_session (s);
1519 * Check if incoming connection is accepted.
1521 * @param cls plugin as closure
1522 * @param addr address of incoming connection
1523 * @param addr_len address length of incoming connection
1524 * @return MHD_YES if connection is accepted, MHD_NO if connection is rejected
1528 server_accept_cb (void *cls, const struct sockaddr *addr, socklen_t addr_len)
1530 struct HTTP_Server_Plugin *plugin = cls;
1532 if (plugin->cur_connections <= plugin->max_connections)
1534 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1535 _("Accepting connection (%u of %u) from `%s'\n"),
1536 plugin->cur_connections, plugin->max_connections,
1537 GNUNET_a2s (addr, addr_len));
1542 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, plugin->name,
1543 _("Server reached maximum number connections (%u), rejecting new connection\n"),
1544 plugin->max_connections);
1550 server_log (void *arg, const char *fmt, va_list ap)
1554 vsnprintf (text, sizeof (text), fmt, ap);
1556 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Server: %s\n", text);
1561 * Call MHD IPv4 to process pending requests and then go back
1562 * and schedule the next run.
1563 * @param cls plugin as closure
1564 * @param tc task context
1567 server_v4_run (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1569 struct HTTP_Server_Plugin *plugin = cls;
1571 GNUNET_assert (cls != NULL);
1573 plugin->server_v4_task = GNUNET_SCHEDULER_NO_TASK;
1574 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1577 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1578 "Running IPv4 server\n");
1580 plugin->server_v4_immediately = GNUNET_NO;
1581 GNUNET_assert (MHD_YES == MHD_run (plugin->server_v4));
1582 server_reschedule (plugin, plugin->server_v4, GNUNET_NO);
1587 * Call MHD IPv6 to process pending requests and then go back
1588 * and schedule the next run.
1589 * @param cls plugin as closure
1590 * @param tc task context
1593 server_v6_run (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1595 struct HTTP_Server_Plugin *plugin = cls;
1597 GNUNET_assert (cls != NULL);
1598 plugin->server_v6_task = GNUNET_SCHEDULER_NO_TASK;
1599 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1602 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1603 "Running IPv6 server\n");
1605 plugin->server_v6_immediately = GNUNET_NO;
1606 GNUNET_assert (MHD_YES == MHD_run (plugin->server_v6));
1607 server_reschedule (plugin, plugin->server_v6, GNUNET_NO);
1612 * Function that queries MHD's select sets and
1613 * starts the task waiting for them.
1615 * @param plugin plugin
1616 * @param daemon_handle the MHD daemon handle
1617 * @return gnunet task identifier
1619 static GNUNET_SCHEDULER_TaskIdentifier
1620 server_schedule (struct HTTP_Server_Plugin *plugin,
1621 struct MHD_Daemon *daemon_handle,
1624 GNUNET_SCHEDULER_TaskIdentifier ret;
1628 struct GNUNET_NETWORK_FDSet *wrs;
1629 struct GNUNET_NETWORK_FDSet *wws;
1630 struct GNUNET_NETWORK_FDSet *wes;
1632 unsigned MHD_LONG_LONG timeout;
1633 static unsigned long long last_timeout = 0;
1636 struct GNUNET_TIME_Relative tv;
1638 if (GNUNET_YES == plugin->in_shutdown)
1639 return GNUNET_SCHEDULER_NO_TASK;
1641 ret = GNUNET_SCHEDULER_NO_TASK;
1645 wrs = GNUNET_NETWORK_fdset_create ();
1646 wes = GNUNET_NETWORK_fdset_create ();
1647 wws = GNUNET_NETWORK_fdset_create ();
1649 GNUNET_assert (MHD_YES == MHD_get_fdset (daemon_handle, &rs, &ws, &es, &max));
1650 haveto = MHD_get_timeout (daemon_handle, &timeout);
1651 if (haveto == MHD_YES)
1653 if (timeout != last_timeout)
1656 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1657 "SELECT Timeout changed from %llu to %llu\n",
1658 last_timeout, timeout);
1659 last_timeout = timeout;
1661 if (timeout <= GNUNET_TIME_UNIT_SECONDS.rel_value)
1662 tv.rel_value = (uint64_t) timeout;
1664 tv = GNUNET_TIME_UNIT_SECONDS;
1667 tv = GNUNET_TIME_UNIT_SECONDS;
1668 /* Force immediate run, since we have outbound data to send */
1669 if (now == GNUNET_YES)
1670 tv = GNUNET_TIME_UNIT_MILLISECONDS;
1671 GNUNET_NETWORK_fdset_copy_native (wrs, &rs, max + 1);
1672 GNUNET_NETWORK_fdset_copy_native (wws, &ws, max + 1);
1673 GNUNET_NETWORK_fdset_copy_native (wes, &es, max + 1);
1675 if (daemon_handle == plugin->server_v4)
1677 if (plugin->server_v4_task != GNUNET_SCHEDULER_NO_TASK)
1679 GNUNET_SCHEDULER_cancel (plugin->server_v4_task);
1680 plugin->server_v4_task = GNUNET_SCHEDULER_NO_TASK;
1683 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1684 "Scheduling IPv4 server task in %llu ms\n", tv);
1687 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1689 &server_v4_run, plugin);
1691 if (daemon_handle == plugin->server_v6)
1693 if (plugin->server_v6_task != GNUNET_SCHEDULER_NO_TASK)
1695 GNUNET_SCHEDULER_cancel (plugin->server_v6_task);
1696 plugin->server_v6_task = GNUNET_SCHEDULER_NO_TASK;
1699 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1700 "Scheduling IPv6 server task in %llu ms\n", tv);
1703 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1705 &server_v6_run, plugin);
1707 GNUNET_NETWORK_fdset_destroy (wrs);
1708 GNUNET_NETWORK_fdset_destroy (wws);
1709 GNUNET_NETWORK_fdset_destroy (wes);
1716 * Load ssl certificate from file
1718 * @param file filename
1719 * @return content of the file
1722 server_load_file (const char *file)
1724 struct GNUNET_DISK_FileHandle *gn_file;
1728 if (GNUNET_OK != GNUNET_DISK_file_size (file,
1729 &fsize, GNUNET_NO, GNUNET_YES))
1731 text = GNUNET_malloc (fsize + 1);
1733 GNUNET_DISK_file_open (file, GNUNET_DISK_OPEN_READ,
1734 GNUNET_DISK_PERM_USER_READ);
1735 if (gn_file == NULL)
1740 if (GNUNET_SYSERR == GNUNET_DISK_file_read (gn_file, text, fsize))
1743 GNUNET_DISK_file_close (gn_file);
1747 GNUNET_DISK_file_close (gn_file);
1755 * Load ssl certificate
1757 * @param plugin the plugin
1758 * @return GNUNET_OK on success, GNUNET_SYSERR on failure
1761 server_load_certificate (struct HTTP_Server_Plugin *plugin)
1763 int res = GNUNET_OK;
1769 /* Get crypto init string from config
1770 * If not present just use default values */
1773 GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg,
1778 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
1779 "Failed to get servicehome!\n");
1780 return GNUNET_SYSERR;
1785 GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg,
1788 &plugin->crypto_init))
1789 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1790 "Using crypto init string `%s'\n",
1791 plugin->crypto_init);
1793 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1794 "Using default crypto init string \n");
1797 GNUNET_CONFIGURATION_get_value_filename (plugin->env->cfg, plugin->name,
1798 "KEY_FILE", &key_file))
1801 GNUNET_asprintf (&key_file, "%s/%s", sh, "https_key.key");
1806 GNUNET_CONFIGURATION_get_value_filename (plugin->env->cfg, plugin->name,
1807 "CERT_FILE", &cert_file))
1810 GNUNET_asprintf (&cert_file, "%s/%s", sh, "https_cert.crt");
1813 /* read key & certificates from file */
1814 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1815 "Trying to loading TLS certificate from key-file `%s' cert-file`%s'\n",
1816 key_file, cert_file);
1818 plugin->key = server_load_file (key_file);
1819 plugin->cert = server_load_file (cert_file);
1821 if ((plugin->key == NULL) || (plugin->cert == NULL))
1823 struct GNUNET_OS_Process *cert_creation;
1825 GNUNET_free_non_null (plugin->key);
1827 GNUNET_free_non_null (plugin->cert);
1828 plugin->cert = NULL;
1830 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1831 "No usable TLS certificate found, creating certificate\n");
1834 GNUNET_OS_start_process (GNUNET_NO, GNUNET_OS_INHERIT_STD_OUT_AND_ERR, NULL, NULL,
1835 "gnunet-transport-certificate-creation",
1836 "gnunet-transport-certificate-creation",
1837 key_file, cert_file, NULL);
1838 if (cert_creation == NULL)
1840 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
1842 ("Could not create a new TLS certificate, program `gnunet-transport-certificate-creation' could not be started!\n"));
1843 GNUNET_free (key_file);
1844 GNUNET_free (cert_file);
1846 GNUNET_free_non_null (plugin->key);
1848 GNUNET_free_non_null (plugin->cert);
1849 plugin->cert = NULL;
1850 GNUNET_free_non_null (plugin->crypto_init);
1851 plugin->crypto_init = NULL;
1853 return GNUNET_SYSERR;
1855 GNUNET_assert (GNUNET_OK == GNUNET_OS_process_wait (cert_creation));
1856 GNUNET_OS_process_destroy (cert_creation);
1858 plugin->key = server_load_file (key_file);
1859 plugin->cert = server_load_file (cert_file);
1862 if ((plugin->key == NULL) || (plugin->cert == NULL))
1864 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
1866 ("No usable TLS certificate found and creating one failed!\n"),
1868 GNUNET_free (key_file);
1869 GNUNET_free (cert_file);
1871 GNUNET_free_non_null (plugin->key);
1873 GNUNET_free_non_null (plugin->cert);
1874 plugin->cert = NULL;
1875 GNUNET_free_non_null (plugin->crypto_init);
1876 plugin->crypto_init = NULL;
1878 return GNUNET_SYSERR;
1880 GNUNET_free (key_file);
1881 GNUNET_free (cert_file);
1882 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "TLS certificate loaded\n");
1889 * Start the HTTP server
1891 * @param plugin the plugin handle
1892 * @return GNUNET_OK on success, GNUNET_SYSERR on failure
1895 server_start (struct HTTP_Server_Plugin *plugin)
1897 unsigned int timeout;
1898 GNUNET_assert (NULL != plugin);
1901 if (GNUNET_SYSERR == server_load_certificate (plugin))
1903 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
1904 "Could not load or create server certificate! Loading plugin failed!\n");
1905 return GNUNET_SYSERR;
1910 #if MHD_VERSION >= 0x00090E00
1911 timeout = HTTP_SERVER_NOT_VALIDATED_TIMEOUT.rel_value / 1000;
1912 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1913 "MHD can set timeout per connection! Default time out %u sec.\n",
1916 timeout = SERVER_SESSION_TIMEOUT.rel_value / 1000;
1917 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, plugin->name,
1918 "MHD cannot set timeout per connection! Default time out %u sec.\n",
1921 plugin->server_v4 = NULL;
1922 if (plugin->use_ipv4 == GNUNET_YES)
1924 plugin->server_v4 = MHD_start_daemon (
1931 MHD_NO_FLAG, plugin->port,
1932 &server_accept_cb, plugin,
1933 &server_access_cb, plugin,
1934 MHD_OPTION_SOCK_ADDR,
1935 (struct sockaddr_in *)
1936 plugin->server_addr_v4,
1937 MHD_OPTION_CONNECTION_LIMIT,
1939 plugin->max_connections,
1941 MHD_OPTION_HTTPS_PRIORITIES,
1942 plugin->crypto_init,
1943 MHD_OPTION_HTTPS_MEM_KEY,
1945 MHD_OPTION_HTTPS_MEM_CERT,
1948 MHD_OPTION_CONNECTION_TIMEOUT,
1950 MHD_OPTION_CONNECTION_MEMORY_LIMIT,
1952 GNUNET_SERVER_MAX_MESSAGE_SIZE),
1953 MHD_OPTION_NOTIFY_COMPLETED,
1954 &server_disconnect_cb, plugin,
1955 MHD_OPTION_EXTERNAL_LOGGER,
1956 server_log, NULL, MHD_OPTION_END);
1958 plugin->server_v6 = NULL;
1959 if (plugin->use_ipv6 == GNUNET_YES)
1961 plugin->server_v6 = MHD_start_daemon (
1968 MHD_USE_IPv6, plugin->port,
1969 &server_accept_cb, plugin,
1970 &server_access_cb, plugin,
1971 MHD_OPTION_SOCK_ADDR,
1972 (struct sockaddr_in6 *)
1973 plugin->server_addr_v6,
1974 MHD_OPTION_CONNECTION_LIMIT,
1976 plugin->max_connections,
1978 MHD_OPTION_HTTPS_PRIORITIES,
1979 plugin->crypto_init,
1980 MHD_OPTION_HTTPS_MEM_KEY,
1982 MHD_OPTION_HTTPS_MEM_CERT,
1985 MHD_OPTION_CONNECTION_TIMEOUT,
1987 MHD_OPTION_CONNECTION_MEMORY_LIMIT,
1989 GNUNET_SERVER_MAX_MESSAGE_SIZE),
1990 MHD_OPTION_NOTIFY_COMPLETED,
1991 &server_disconnect_cb, plugin,
1992 MHD_OPTION_EXTERNAL_LOGGER,
1993 server_log, NULL, MHD_OPTION_END);
1997 if ((plugin->use_ipv4 == GNUNET_YES) && (plugin->server_v4 == NULL))
1999 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
2000 "Failed to start %s IPv4 server component on port %u\n",
2001 plugin->name, plugin->port);
2002 return GNUNET_SYSERR;
2004 server_reschedule (plugin, plugin->server_v4, GNUNET_NO);
2006 if ((plugin->use_ipv6 == GNUNET_YES) && (plugin->server_v6 == NULL))
2008 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
2009 "Failed to start %s IPv6 server component on port %u\n",
2010 plugin->name, plugin->port);
2011 return GNUNET_SYSERR;
2013 server_reschedule (plugin, plugin->server_v6, GNUNET_NO);
2014 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2015 "%s server component started on port %u\n", plugin->name,
2022 server_stop (struct HTTP_Server_Plugin *plugin)
2024 if (plugin->server_v4 != NULL)
2026 MHD_stop_daemon (plugin->server_v4);
2027 plugin->server_v4 = NULL;
2029 if ( plugin->server_v6 != NULL)
2031 MHD_stop_daemon (plugin->server_v6);
2032 plugin->server_v6 = NULL;
2036 if (plugin->server_v4_task != GNUNET_SCHEDULER_NO_TASK)
2038 GNUNET_SCHEDULER_cancel (plugin->server_v4_task);
2039 plugin->server_v4_task = GNUNET_SCHEDULER_NO_TASK;
2042 if (plugin->server_v6_task != GNUNET_SCHEDULER_NO_TASK)
2044 GNUNET_SCHEDULER_cancel (plugin->server_v6_task);
2045 plugin->server_v6_task = GNUNET_SCHEDULER_NO_TASK;
2050 GNUNET_free_non_null (plugin->crypto_init);
2051 GNUNET_free_non_null (plugin->cert);
2052 GNUNET_free_non_null (plugin->key);
2055 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2056 "%s server component stopped\n", plugin->name);
2061 * Add an address to the server's set of addresses and notify transport
2063 * @param cls the plugin handle
2064 * @param add_remove GNUNET_YES on add, GNUNET_NO on remove
2065 * @param addr the address
2066 * @param addrlen address length
2069 server_add_address (void *cls, int add_remove, const struct sockaddr *addr,
2072 struct HTTP_Server_Plugin *plugin = cls;
2073 struct HttpAddressWrapper *w = NULL;
2075 w = GNUNET_malloc (sizeof (struct HttpAddressWrapper));
2076 w->addr = http_common_address_from_socket (plugin->protocol, addr, addrlen);
2077 if (NULL == w->addr)
2082 w->addrlen = http_common_address_get_size (w->addr);
2084 GNUNET_CONTAINER_DLL_insert(plugin->addr_head, plugin->addr_tail, w);
2085 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2086 "Notifying transport to add address `%s'\n",
2087 http_common_plugin_address_to_string(NULL, w->addr, w->addrlen));
2089 plugin->env->notify_address (plugin->env->cls, add_remove, w->addr, w->addrlen, "https_client");
2091 plugin->env->notify_address (plugin->env->cls, add_remove, w->addr, w->addrlen, "http_client");
2097 * Remove an address from the server's set of addresses and notify transport
2099 * @param cls the plugin handle
2100 * @param add_remove GNUNET_YES on add, GNUNET_NO on remove
2101 * @param addr the address
2102 * @param addrlen address length
2105 server_remove_address (void *cls, int add_remove, const struct sockaddr *addr,
2108 struct HTTP_Server_Plugin *plugin = cls;
2109 struct HttpAddressWrapper *w = plugin->addr_head;
2111 void * saddr = http_common_address_from_socket (plugin->protocol, addr, addrlen);
2114 saddr_len = http_common_address_get_size (saddr);
2118 if (GNUNET_YES == http_common_cmp_addresses(w->addr, w->addrlen, saddr, saddr_len))
2122 GNUNET_free (saddr);
2127 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2128 "Notifying transport to remove address `%s'\n",
2129 http_common_plugin_address_to_string (NULL, w->addr, w->addrlen));
2130 GNUNET_CONTAINER_DLL_remove (plugin->addr_head, plugin->addr_tail, w);
2132 plugin->env->notify_address (plugin->env->cls, add_remove, w->addr, w->addrlen, "https_client");
2134 plugin->env->notify_address (plugin->env->cls, add_remove, w->addr, w->addrlen, "http_client");
2136 GNUNET_free (w->addr);
2143 * Our external IP address/port mapping has changed.
2145 * @param cls closure, the 'struct LocalAddrList'
2146 * @param add_remove GNUNET_YES to mean the new public IP address, GNUNET_NO to mean
2147 * the previous (now invalid) one
2148 * @param addr either the previous or the new public IP address
2149 * @param addrlen actual lenght of the address
2152 server_nat_port_map_callback (void *cls, int add_remove, const struct sockaddr *addr,
2155 GNUNET_assert (cls != NULL);
2156 struct HTTP_Server_Plugin *plugin = cls;
2158 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2159 "NAT called to %s address `%s'\n",
2160 (add_remove == GNUNET_NO) ? "remove" : "add",
2161 GNUNET_a2s (addr, addrlen));
2163 if (AF_INET == addr->sa_family)
2165 struct sockaddr_in *s4 = (struct sockaddr_in *) addr;
2167 if (GNUNET_NO == plugin->use_ipv4)
2170 if ((NULL != plugin->server_addr_v4) &&
2171 (0 != memcmp (&plugin->server_addr_v4->sin_addr,
2172 &s4->sin_addr, sizeof (struct in_addr))))
2174 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2175 "Skipping address `%s' (not bindto address)\n",
2176 GNUNET_a2s (addr, addrlen));
2181 if (AF_INET6 == addr->sa_family)
2183 struct sockaddr_in6 *s6 = (struct sockaddr_in6 *) addr;
2184 if (GNUNET_NO == plugin->use_ipv6)
2187 if ((NULL != plugin->server_addr_v6) &&
2188 (0 != memcmp (&plugin->server_addr_v6->sin6_addr,
2189 &s6->sin6_addr, sizeof (struct in6_addr))))
2191 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2192 "Skipping address `%s' (not bindto address)\n",
2193 GNUNET_a2s (addr, addrlen));
2201 server_add_address (cls, add_remove, addr, addrlen);
2204 server_remove_address (cls, add_remove, addr, addrlen);
2211 * Get valid server addresses
2213 * @param plugin the plugin handle
2214 * @param serviceName the servicename
2215 * @param cfg configuration handle
2216 * @param addrs addresses
2217 * @param addr_lens address length
2218 * @return number of addresses
2221 server_get_addresses (struct HTTP_Server_Plugin *plugin,
2222 const char *serviceName,
2223 const struct GNUNET_CONFIGURATION_Handle *cfg,
2224 struct sockaddr ***addrs, socklen_t ** addr_lens)
2227 unsigned long long port;
2228 struct addrinfo hints;
2229 struct addrinfo *res;
2230 struct addrinfo *pos;
2231 struct addrinfo *next;
2235 struct sockaddr **saddrs;
2236 socklen_t *saddrlens;
2242 disablev6 = !plugin->use_ipv6;
2245 if (GNUNET_CONFIGURATION_have_value (cfg, serviceName, "PORT"))
2247 GNUNET_break (GNUNET_OK ==
2248 GNUNET_CONFIGURATION_get_value_number (cfg, serviceName,
2252 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2254 ("Require valid port number for service in configuration!\n"));
2255 return GNUNET_SYSERR;
2260 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, plugin->name,
2261 "Starting in listen only mode\n");
2262 return -1; /* listen only */
2266 if (GNUNET_CONFIGURATION_have_value (cfg, serviceName, "BINDTO"))
2268 GNUNET_break (GNUNET_OK ==
2269 GNUNET_CONFIGURATION_get_value_string (cfg, serviceName,
2270 "BINDTO", &hostname));
2275 if (hostname != NULL)
2277 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2278 "Resolving `%s' since that is where `%s' will bind to.\n",
2279 hostname, serviceName);
2280 memset (&hints, 0, sizeof (struct addrinfo));
2282 hints.ai_family = AF_INET;
2283 if ((0 != (ret = getaddrinfo (hostname, NULL, &hints, &res))) ||
2286 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Failed to resolve `%s': %s\n"),
2287 hostname, gai_strerror (ret));
2288 GNUNET_free (hostname);
2289 return GNUNET_SYSERR;
2293 while (NULL != (pos = next))
2295 next = pos->ai_next;
2296 if ((disablev6) && (pos->ai_family == AF_INET6))
2302 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2303 _("Failed to find %saddress for `%s'.\n"),
2304 disablev6 ? "IPv4 " : "", hostname);
2306 GNUNET_free (hostname);
2307 return GNUNET_SYSERR;
2310 saddrs = GNUNET_malloc ((resi + 1) * sizeof (struct sockaddr *));
2311 saddrlens = GNUNET_malloc ((resi + 1) * sizeof (socklen_t));
2314 while (NULL != (pos = next))
2316 next = pos->ai_next;
2317 if ((disablev6) && (pos->ai_family == AF_INET6))
2319 if ((pos->ai_protocol != IPPROTO_TCP) && (pos->ai_protocol != 0))
2320 continue; /* not TCP */
2321 if ((pos->ai_socktype != SOCK_STREAM) && (pos->ai_socktype != 0))
2322 continue; /* huh? */
2323 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2324 "Service will bind to `%s'\n", GNUNET_a2s (pos->ai_addr,
2326 if (pos->ai_family == AF_INET)
2328 GNUNET_assert (pos->ai_addrlen == sizeof (struct sockaddr_in));
2329 saddrlens[i] = pos->ai_addrlen;
2330 saddrs[i] = GNUNET_malloc (saddrlens[i]);
2331 memcpy (saddrs[i], pos->ai_addr, saddrlens[i]);
2332 ((struct sockaddr_in *) saddrs[i])->sin_port = htons (port);
2336 GNUNET_assert (pos->ai_family == AF_INET6);
2337 GNUNET_assert (pos->ai_addrlen == sizeof (struct sockaddr_in6));
2338 saddrlens[i] = pos->ai_addrlen;
2339 saddrs[i] = GNUNET_malloc (saddrlens[i]);
2340 memcpy (saddrs[i], pos->ai_addr, saddrlens[i]);
2341 ((struct sockaddr_in6 *) saddrs[i])->sin6_port = htons (port);
2345 GNUNET_free (hostname);
2351 /* will bind against everything, just set port */
2357 saddrs = GNUNET_malloc ((resi + 1) * sizeof (struct sockaddr *));
2358 saddrlens = GNUNET_malloc ((resi + 1) * sizeof (socklen_t));
2360 saddrlens[i] = sizeof (struct sockaddr_in);
2361 saddrs[i] = GNUNET_malloc (saddrlens[i]);
2362 #if HAVE_SOCKADDR_IN_SIN_LEN
2363 ((struct sockaddr_in *) saddrs[i])->sin_len = saddrlens[i];
2365 ((struct sockaddr_in *) saddrs[i])->sin_family = AF_INET;
2366 ((struct sockaddr_in *) saddrs[i])->sin_port = htons (port);
2372 saddrs = GNUNET_malloc ((resi + 1) * sizeof (struct sockaddr *));
2373 saddrlens = GNUNET_malloc ((resi + 1) * sizeof (socklen_t));
2375 saddrlens[i] = sizeof (struct sockaddr_in6);
2376 saddrs[i] = GNUNET_malloc (saddrlens[i]);
2377 #if HAVE_SOCKADDR_IN_SIN_LEN
2378 ((struct sockaddr_in6 *) saddrs[i])->sin6_len = saddrlens[0];
2380 ((struct sockaddr_in6 *) saddrs[i])->sin6_family = AF_INET6;
2381 ((struct sockaddr_in6 *) saddrs[i])->sin6_port = htons (port);
2383 saddrlens[i] = sizeof (struct sockaddr_in);
2384 saddrs[i] = GNUNET_malloc (saddrlens[i]);
2385 #if HAVE_SOCKADDR_IN_SIN_LEN
2386 ((struct sockaddr_in *) saddrs[i])->sin_len = saddrlens[1];
2388 ((struct sockaddr_in *) saddrs[i])->sin_family = AF_INET;
2389 ((struct sockaddr_in *) saddrs[i])->sin_port = htons (port);
2393 *addr_lens = saddrlens;
2399 * Ask NAT for addresses
2401 * @param plugin the plugin handle
2404 server_start_report_addresses (struct HTTP_Server_Plugin *plugin)
2406 int res = GNUNET_OK;
2407 struct sockaddr **addrs;
2408 socklen_t *addrlens;
2410 res = server_get_addresses (plugin,
2411 plugin->name, plugin->env->cfg,
2413 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2414 _("Found %u addresses to report to NAT service\n"), res);
2416 if (GNUNET_SYSERR == res)
2423 GNUNET_NAT_register (plugin->env->cfg, GNUNET_YES, plugin->port,
2425 (const struct sockaddr **) addrs, addrlens,
2426 &server_nat_port_map_callback, NULL, plugin);
2430 GNUNET_assert (addrs[res] != NULL);
2431 GNUNET_free (addrs[res]);
2433 GNUNET_free_non_null (addrs);
2434 GNUNET_free_non_null (addrlens);
2439 * Stop NAT for addresses
2441 * @param plugin the plugin handle
2444 server_stop_report_addresses (struct HTTP_Server_Plugin *plugin)
2446 /* Stop NAT handle */
2447 if (NULL != plugin->nat)
2448 GNUNET_NAT_unregister (plugin->nat);
2450 /* Clean up addresses */
2451 struct HttpAddressWrapper *w;
2453 while (plugin->addr_head != NULL)
2455 w = plugin->addr_head;
2456 GNUNET_CONTAINER_DLL_remove (plugin->addr_head, plugin->addr_tail, w);
2457 GNUNET_free (w->addr);
2464 * Check if IPv6 supported on this system
2466 * @param plugin the plugin handle
2467 * @return GNUNET_YES on success, else GNUNET_NO
2470 server_check_ipv6_support (struct HTTP_Server_Plugin *plugin)
2472 struct GNUNET_NETWORK_Handle *desc = NULL;
2473 int res = GNUNET_NO;
2475 /* Probe IPv6 support */
2476 desc = GNUNET_NETWORK_socket_create (PF_INET6, SOCK_STREAM, 0);
2479 if ((errno == ENOBUFS) || (errno == ENOMEM) || (errno == ENFILE) ||
2482 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "socket");
2484 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, plugin->name,
2486 ("Disabling IPv6 since it is not supported on this system!\n"));
2491 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (desc));
2495 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2496 "Testing IPv6 on this system: %s\n",
2497 (res == GNUNET_YES) ? "successful" : "failed");
2503 * Function called when the service shuts down. Unloads our plugins
2504 * and cancels pending validations.
2506 * @param cls closure, unused
2507 * @param tc task context (unused)
2510 server_notify_external_hostname (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2512 struct HTTP_Server_Plugin *plugin = cls;
2514 plugin->notify_ext_task = GNUNET_SCHEDULER_NO_TASK;
2516 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
2519 GNUNET_asprintf(&plugin->ext_addr, "%s://%s", plugin->protocol, plugin->external_hostname);
2520 plugin->ext_addr_len = strlen (plugin->ext_addr) + 1;
2521 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2522 "Notifying transport about external hostname address `%s'\n", plugin->ext_addr);
2525 plugin->env->notify_address (plugin->env->cls, GNUNET_YES,
2526 plugin->ext_addr, plugin->ext_addr_len,
2529 plugin->env->notify_address (plugin->env->cls, GNUNET_YES,
2530 plugin->ext_addr, plugin->ext_addr_len,
2537 * Configure the plugin
2539 * @param plugin plugin handle
2540 * @return GNUNET_OK on success, GNUNET_SYSERR on failure
2543 server_configure_plugin (struct HTTP_Server_Plugin *plugin)
2545 unsigned long long port;
2546 unsigned long long max_connections;
2547 char *bind4_address = NULL;
2548 char *bind6_address = NULL;
2551 if (GNUNET_CONFIGURATION_have_value
2552 (plugin->env->cfg, plugin->name, "USE_IPv4"))
2555 GNUNET_CONFIGURATION_get_value_yesno (plugin->env->cfg, plugin->name,
2559 plugin->use_ipv4 = GNUNET_YES;
2560 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2561 _("IPv4 support is %s\n"),
2562 (plugin->use_ipv4 == GNUNET_YES) ? "enabled" : "disabled");
2565 if (GNUNET_CONFIGURATION_have_value
2566 (plugin->env->cfg, plugin->name, "USE_IPv6"))
2569 GNUNET_CONFIGURATION_get_value_yesno (plugin->env->cfg, plugin->name,
2573 plugin->use_ipv6 = GNUNET_YES;
2574 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2575 _("IPv6 support is %s\n"),
2576 (plugin->use_ipv6 == GNUNET_YES) ? "enabled" : "disabled");
2578 if ((plugin->use_ipv4 == GNUNET_NO) && (plugin->use_ipv6 == GNUNET_NO))
2580 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
2582 ("Neither IPv4 nor IPv6 are enabled! Fix in configuration\n"),
2584 return GNUNET_SYSERR;
2587 /* Reading port number from config file */
2589 GNUNET_CONFIGURATION_get_value_number (plugin->env->cfg, plugin->name,
2590 "PORT", &port)) || (port > 65535))
2592 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
2593 _("Port is required! Fix in configuration\n"),
2595 return GNUNET_SYSERR;
2597 plugin->port = port;
2599 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2600 _("Using port %u\n"), plugin->port);
2602 if ((plugin->use_ipv4 == GNUNET_YES) &&
2603 (GNUNET_YES == GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg,
2604 plugin->name, "BINDTO", &bind4_address)))
2606 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2607 "Binding %s plugin to specific IPv4 address: `%s'\n",
2608 plugin->protocol, bind4_address);
2609 plugin->server_addr_v4 = GNUNET_malloc (sizeof (struct sockaddr_in));
2610 if (1 != inet_pton (AF_INET, bind4_address,
2611 &plugin->server_addr_v4->sin_addr))
2613 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
2615 ("Specific IPv4 address `%s' in configuration file is invalid!\n"),
2617 GNUNET_free (bind4_address);
2618 GNUNET_free (plugin->server_addr_v4);
2619 plugin->server_addr_v4 = NULL;
2620 return GNUNET_SYSERR;
2624 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2625 _("Binding to IPv4 address %s\n"), bind4_address);
2626 plugin->server_addr_v4->sin_family = AF_INET;
2627 plugin->server_addr_v4->sin_port = htons (plugin->port);
2629 GNUNET_free (bind4_address);
2632 if ((plugin->use_ipv6 == GNUNET_YES) &&
2634 GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg, plugin->name,
2635 "BINDTO6", &bind6_address)))
2637 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2638 "Binding %s plugin to specific IPv6 address: `%s'\n",
2639 plugin->protocol, bind6_address);
2640 plugin->server_addr_v6 = GNUNET_malloc (sizeof (struct sockaddr_in6));
2642 inet_pton (AF_INET6, bind6_address, &plugin->server_addr_v6->sin6_addr))
2644 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
2646 ("Specific IPv6 address `%s' in configuration file is invalid!\n"),
2648 GNUNET_free (bind6_address);
2649 GNUNET_free (plugin->server_addr_v6);
2650 plugin->server_addr_v6 = NULL;
2651 return GNUNET_SYSERR;
2655 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2656 _("Binding to IPv6 address %s\n"), bind6_address);
2657 plugin->server_addr_v6->sin6_family = AF_INET6;
2658 plugin->server_addr_v6->sin6_port = htons (plugin->port);
2660 GNUNET_free (bind6_address);
2663 if (GNUNET_YES == GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg, plugin->name,
2664 "EXTERNAL_HOSTNAME", &plugin->external_hostname))
2667 if (NULL != strstr(plugin->external_hostname, "://"))
2669 tmp = strdup(&strstr(plugin->external_hostname, "://")[3]);
2670 GNUNET_free (plugin->external_hostname);
2671 plugin->external_hostname = tmp;
2674 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2675 _("Using external hostname `%s'\n"), plugin->external_hostname);
2676 plugin->notify_ext_task = GNUNET_SCHEDULER_add_now (&server_notify_external_hostname, plugin);
2678 /* Use only configured external hostname */
2679 if (GNUNET_CONFIGURATION_have_value
2680 (plugin->env->cfg, plugin->name, "EXTERNAL_HOSTNAME_ONLY"))
2682 plugin->external_only =
2683 GNUNET_CONFIGURATION_get_value_yesno (plugin->env->cfg, plugin->name,
2684 "EXTERNAL_HOSTNAME_ONLY");
2687 plugin->external_only = GNUNET_NO;
2689 if (GNUNET_YES == plugin->external_only)
2690 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2691 _("Notifying transport only about hostname `%s'\n"), plugin->external_hostname);
2694 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2695 "No external hostname configured\n");
2697 /* Optional parameters */
2698 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (plugin->env->cfg,
2700 "MAX_CONNECTIONS", &max_connections))
2701 max_connections = 128;
2702 plugin->max_connections = max_connections;
2704 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2705 _("Maximum number of connections is %u\n"),
2706 plugin->max_connections);
2709 plugin->peer_id_length = strlen (GNUNET_h2s_full (&plugin->env->my_identity->hashPubKey));
2716 * Session was idle, so disconnect it
2718 * @param cls the session
2719 * @param tc task context
2722 server_session_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2724 GNUNET_assert (NULL != cls);
2725 struct Session *s = cls;
2727 s->timeout_task = GNUNET_SCHEDULER_NO_TASK;
2728 GNUNET_log (TIMEOUT_LOG,
2729 "Session %p was idle for %llu ms, disconnecting\n",
2730 s, (unsigned long long) SERVER_SESSION_TIMEOUT.rel_value);
2732 /* call session destroy function */
2733 GNUNET_assert (GNUNET_OK == server_disconnect (s));
2738 * Start session timeout for session s
2740 * @param s the session
2743 server_start_session_timeout (struct Session *s)
2745 GNUNET_assert (NULL != s);
2746 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == s->timeout_task);
2747 s->timeout_task = GNUNET_SCHEDULER_add_delayed (SERVER_SESSION_TIMEOUT,
2748 &server_session_timeout,
2750 GNUNET_log (TIMEOUT_LOG,
2751 "Timeout for session %p set to %llu ms\n",
2752 s, (unsigned long long) SERVER_SESSION_TIMEOUT.rel_value);
2757 * Increment session timeout due to activity session s
2759 * @param s the session
2762 server_reschedule_session_timeout (struct Session *s)
2764 GNUNET_assert (NULL != s);
2765 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK != s->timeout_task);
2767 GNUNET_SCHEDULER_cancel (s->timeout_task);
2768 s->timeout_task = GNUNET_SCHEDULER_add_delayed (SERVER_SESSION_TIMEOUT,
2769 &server_session_timeout,
2771 GNUNET_log (TIMEOUT_LOG,
2772 "Timeout rescheduled for session %p set to %llu ms\n",
2773 s, (unsigned long long) SERVER_SESSION_TIMEOUT.rel_value);
2778 * Exit point from the plugin.
2784 LIBGNUNET_PLUGIN_TRANSPORT_DONE (void *cls)
2786 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
2787 struct HTTP_Server_Plugin *plugin = api->cls;
2788 struct Session *pos;
2789 struct Session *next;
2791 if (NULL == api->cls)
2793 /* Free for stub mode */
2797 plugin->in_shutdown = GNUNET_YES;
2798 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2799 _("Shutting down plugin `%s'\n"),
2802 if (GNUNET_SCHEDULER_NO_TASK != plugin->notify_ext_task)
2804 GNUNET_SCHEDULER_cancel (plugin->notify_ext_task);
2805 plugin->notify_ext_task = GNUNET_SCHEDULER_NO_TASK;
2808 if (NULL != plugin->ext_addr)
2810 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2811 "Notifying transport to remove address `%s'\n",
2812 http_common_plugin_address_to_string (NULL,
2814 plugin->ext_addr_len));
2816 plugin->env->notify_address (plugin->env->cls,
2819 plugin->ext_addr_len,
2822 plugin->env->notify_address (plugin->env->cls,
2825 plugin->ext_addr_len,
2831 /* Stop to report addresses to transport service */
2832 server_stop_report_addresses (plugin);
2833 server_stop (plugin);
2834 next = plugin->head;
2835 while (NULL != (pos = next))
2838 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2839 "Removing left over session %p\n", pos);
2841 if ((GNUNET_YES == pos->session_passed) && (GNUNET_NO == pos->session_ended))
2843 /* Notify transport immediately that this session is invalid */
2844 pos->session_ended = GNUNET_YES;
2845 plugin->env->session_end (plugin->env->cls, &pos->target, pos);
2848 server_delete_session (pos);
2852 GNUNET_free_non_null (plugin->external_hostname);
2853 GNUNET_free_non_null (plugin->ext_addr);
2854 GNUNET_free_non_null (plugin->server_addr_v4);
2855 GNUNET_free_non_null (plugin->server_addr_v6);
2857 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2858 _("Shutdown for plugin `%s' complete\n"),
2861 GNUNET_free (plugin);
2868 * Entry point for the plugin.
2874 LIBGNUNET_PLUGIN_TRANSPORT_INIT (void *cls)
2876 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
2877 struct GNUNET_TRANSPORT_PluginFunctions *api;
2878 struct HTTP_Server_Plugin *plugin;
2880 plugin = GNUNET_malloc (sizeof (struct HTTP_Server_Plugin));
2884 if (NULL == env->receive)
2886 /* run in 'stub' mode (i.e. as part of gnunet-peerinfo), don't fully
2887 initialze the plugin or the API */
2888 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
2890 api->address_to_string = &http_common_plugin_address_to_string;
2891 api->string_to_address = &http_common_plugin_string_to_address;
2892 api->address_pretty_printer = &http_common_plugin_address_pretty_printer;
2896 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
2898 api->send = &http_server_plugin_send;
2899 api->disconnect = &http_server_plugin_disconnect;
2900 api->check_address = &http_server_plugin_address_suggested;
2901 api->get_session = &http_server_plugin_get_session;
2903 api->address_to_string = &http_common_plugin_address_to_string;
2904 api->string_to_address = &http_common_plugin_string_to_address;
2905 api->address_pretty_printer = &http_common_plugin_address_pretty_printer;
2908 plugin->name = "transport-https_server";
2909 plugin->protocol = "https";
2911 plugin->name = "transport-http_server";
2912 plugin->protocol = "http";
2915 /* Configure plugin */
2916 if (GNUNET_SYSERR == server_configure_plugin (plugin))
2918 LIBGNUNET_PLUGIN_TRANSPORT_DONE (api);
2922 /* Check IPv6 support */
2923 if (GNUNET_YES == plugin->use_ipv6)
2924 plugin->use_ipv6 = server_check_ipv6_support (plugin);
2926 /* Report addresses to transport service */
2927 if (GNUNET_NO == plugin->external_only)
2928 server_start_report_addresses (plugin);
2930 if (GNUNET_SYSERR == server_start (plugin))
2932 LIBGNUNET_PLUGIN_TRANSPORT_DONE (api);
2939 /* end of plugin_transport_http_server.c */