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)
530 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, session->plugin->name,
531 "Session %p/connection %p: Sending message with %u bytes to peer `%s' with FAILED\n",
532 session, session->server_send,
533 msgbuf_size, GNUNET_i2s (&session->target));
535 return GNUNET_SYSERR;
539 if (GNUNET_YES == session->server_send->disconnect)
540 return GNUNET_SYSERR;
542 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, session->plugin->name,
543 "Session %p/connection %p: Sending message with %u to peer `%s' with \n",
544 session, session->server_send,
545 msgbuf_size, GNUNET_i2s (&session->target));
547 /* create new message and schedule */
548 bytes_sent = sizeof (struct HTTP_Message) + msgbuf_size;
549 msg = GNUNET_malloc (bytes_sent);
551 msg->size = msgbuf_size;
553 msg->buf = (char *) &msg[1];
554 msg->transmit_cont = cont;
555 msg->transmit_cont_cls = cont_cls;
556 memcpy (msg->buf, msgbuf, msgbuf_size);
558 GNUNET_CONTAINER_DLL_insert_tail (session->msg_head, session->msg_tail, msg);
560 GNUNET_asprintf (&stat_txt, "# bytes currently in %s_server buffers", plugin->protocol);
561 GNUNET_STATISTICS_update (plugin->env->stats,
562 stat_txt, msgbuf_size, GNUNET_NO);
563 GNUNET_free (stat_txt);
565 server_reschedule (session->plugin, session->server_send->mhd_daemon, GNUNET_YES);
566 server_reschedule_session_timeout (session);
568 /* struct Plugin *plugin = cls; */
575 * Function that can be used to force the plugin to disconnect
576 * from the given peer and cancel all previous transmissions
577 * (and their continuationc).
580 * @param target peer from which to disconnect
583 http_server_plugin_disconnect (void *cls, const struct GNUNET_PeerIdentity *target)
585 struct HTTP_Server_Plugin *plugin = cls;
586 struct Session *next = NULL;
587 struct Session *pos = NULL;
589 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
590 "Transport tells me to disconnect `%s'\n",
591 GNUNET_i2s (target));
594 while (NULL != (pos = next))
597 if (0 == memcmp (target, &pos->target, sizeof (struct GNUNET_PeerIdentity)))
599 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
600 "Disconnecting session %p to `%s'\n",
601 pos, GNUNET_i2s (target));
602 server_disconnect (pos);
610 * Another peer has suggested an address for this
611 * peer and transport plugin. Check that this could be a valid
612 * address. If so, consider adding it to the list
616 * @param addr pointer to the address
617 * @param addrlen length of addr
618 * @return GNUNET_OK if this is a plausible address for this peer
622 http_server_plugin_address_suggested (void *cls, const void *addr,
625 struct HTTP_Server_Plugin *plugin = cls;
626 struct HttpAddressWrapper *next;
627 struct HttpAddressWrapper *pos;
630 if ((NULL != plugin->ext_addr) &&
631 GNUNET_YES == (http_common_cmp_addresses (addr, addrlen,
632 plugin->ext_addr, plugin->ext_addr_len)))
635 next = plugin->addr_head;
636 while (NULL != (pos = next))
639 if (GNUNET_YES == (http_common_cmp_addresses(addr,
652 * Creates a new outbound session the transport
653 * service will use to send data to the peer
655 * Since HTTP/S server cannot create sessions, always return NULL
657 * @param cls the plugin
658 * @param address the address
659 * @return always NULL
661 static struct Session *
662 http_server_plugin_get_session (void *cls,
663 const struct GNUNET_HELLO_Address *address)
670 * Deleting the session
671 * Must not be used afterwards
673 * @param s the session to delete
676 server_delete_session (struct Session *s)
678 struct HTTP_Server_Plugin *plugin = s->plugin;
679 server_stop_session_timeout(s);
681 GNUNET_CONTAINER_DLL_remove (plugin->head, plugin->tail, s);
682 struct HTTP_Message *msg = s->msg_head;
683 struct HTTP_Message *tmp = NULL;
689 GNUNET_CONTAINER_DLL_remove (s->msg_head, s->msg_tail, msg);
690 if (msg->transmit_cont != NULL)
692 msg->transmit_cont (msg->transmit_cont_cls, &s->target, GNUNET_SYSERR,
693 msg->size, msg->pos + msg->overhead);
699 if (s->msg_tk != NULL)
701 GNUNET_SERVER_mst_destroy (s->msg_tk);
704 GNUNET_free (s->addr);
705 GNUNET_free_non_null (s->server_recv);
706 GNUNET_free_non_null (s->server_send);
709 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
710 "Session %p destroyed\n", s);
715 * Cancel timeout for session s
717 * @param s the session
720 server_stop_session_timeout (struct Session *s)
722 GNUNET_assert (NULL != s);
724 if (GNUNET_SCHEDULER_NO_TASK != s->timeout_task)
726 GNUNET_SCHEDULER_cancel (s->timeout_task);
727 s->timeout_task = GNUNET_SCHEDULER_NO_TASK;
728 GNUNET_log (TIMEOUT_LOG, "Timeout stopped for session %p\n", s);
734 * Function that queries MHD's select sets and
735 * starts the task waiting for them.
736 * @param plugin plugin
737 * @param daemon_handle the MHD daemon handle
738 * @param now schedule immediately
739 * @return gnunet task identifier
741 static GNUNET_SCHEDULER_TaskIdentifier
742 server_schedule (struct HTTP_Server_Plugin *plugin,
743 struct MHD_Daemon *daemon_handle,
748 * Reschedule the execution of both IPv4 and IPv6 server
749 * @param plugin the plugin
750 * @param server which server to schedule v4 or v6?
751 * @param now GNUNET_YES to schedule execution immediately, GNUNET_NO to wait
755 server_reschedule (struct HTTP_Server_Plugin *plugin, struct MHD_Daemon *server,
758 if ((server == plugin->server_v4) && (plugin->server_v4 != NULL))
760 if (GNUNET_YES == plugin->server_v4_immediately)
761 return; /* No rescheduling, server will run asap */
763 if (GNUNET_YES == now)
764 plugin->server_v4_immediately = GNUNET_YES;
766 if (plugin->server_v4_task != GNUNET_SCHEDULER_NO_TASK)
768 GNUNET_SCHEDULER_cancel (plugin->server_v4_task);
769 plugin->server_v4_task = GNUNET_SCHEDULER_NO_TASK;
771 plugin->server_v4_task = server_schedule (plugin, plugin->server_v4, now);
774 if ((server == plugin->server_v6) && (plugin->server_v6 != NULL))
776 if (GNUNET_YES == plugin->server_v6_immediately)
777 return; /* No rescheduling, server will run asap */
779 if (GNUNET_YES == now)
780 plugin->server_v6_immediately = GNUNET_YES;
782 if (plugin->server_v6_task != GNUNET_SCHEDULER_NO_TASK)
784 GNUNET_SCHEDULER_cancel (plugin->server_v6_task);
785 plugin->server_v6_task = GNUNET_SCHEDULER_NO_TASK;
787 plugin->server_v6_task = server_schedule (plugin, plugin->server_v6, now);
793 * Disconnect session s
795 * @param s the session
796 * @return GNUNET_OK on success
799 server_disconnect (struct Session *s)
801 struct ServerConnection * send = NULL;
802 struct ServerConnection * recv = NULL;
804 if (GNUNET_NO == server_exist_session (p, s))
807 return GNUNET_SYSERR;
810 send = (struct ServerConnection *) s->server_send;
811 if (s->server_send != NULL)
813 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, s->plugin->name,
814 "Server: %p / %p Terminating inbound PUT session to peer `%s'\n",
815 s, s->server_send, GNUNET_i2s (&s->target));
817 send->disconnect = GNUNET_YES;
818 #if MHD_VERSION >= 0x00090E00
819 MHD_set_connection_option (send->mhd_conn, MHD_CONNECTION_OPTION_TIMEOUT,
822 server_reschedule (s->plugin, send->mhd_daemon, GNUNET_YES);
825 recv = (struct ServerConnection *) s->server_recv;
828 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, s->plugin->name,
829 "Server: %p / %p Terminating inbound GET session to peer `%s'\n",
830 s, s->server_recv, GNUNET_i2s (&s->target));
832 recv->disconnect = GNUNET_YES;
833 #if MHD_VERSION >= 0x00090E00
834 MHD_set_connection_option (recv->mhd_conn, MHD_CONNECTION_OPTION_TIMEOUT,
837 server_reschedule (s->plugin, recv->mhd_daemon, GNUNET_YES);
843 server_mhd_connection_timeout (struct HTTP_Server_Plugin *plugin, struct Session *s, int to)
845 #if MHD_VERSION >= 0x00090E00
846 /* Setting timeouts for other connections */
847 if (s->server_recv != NULL)
849 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
850 "Setting timeout for %p to %u sec.\n", s->server_recv, to);
851 MHD_set_connection_option (s->server_recv->mhd_conn,
852 MHD_CONNECTION_OPTION_TIMEOUT,
854 server_reschedule (plugin, s->server_recv->mhd_daemon, GNUNET_NO);
856 if (s->server_send != NULL)
858 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
859 "Setting timeout for %p to %u sec.\n", s->server_send, to);
860 MHD_set_connection_option (s->server_send->mhd_conn,
861 MHD_CONNECTION_OPTION_TIMEOUT,
863 server_reschedule (plugin, s->server_send->mhd_daemon, GNUNET_NO);
869 * Parse incoming URL for tag and target
871 * @param plugin plugin
872 * @param url incoming url
873 * @param target where to store the target
874 * @param tag where to store the tag
875 * @return GNUNET_OK on success, GNUNET_SYSERR on error
879 server_parse_url (struct HTTP_Server_Plugin *plugin, const char * url, struct GNUNET_PeerIdentity * target, uint32_t *tag)
881 int debug = GNUNET_YES;
883 char * tag_start = NULL;
884 char * tag_end = NULL;
885 char * target_start = NULL;
886 char * separator = NULL;
887 char hash[plugin->peer_id_length+1];
889 unsigned long int ctag;
892 * URL is valid if it is in the form [prefix with (multiple) '/'][peerid[103];tag]*/
897 return GNUNET_SYSERR;
902 separator = strrchr (url, ';');
904 if (NULL == separator)
906 if (debug) GNUNET_break (0);
907 return GNUNET_SYSERR;
909 tag_start = separator + 1;
911 if (strlen (tag_start) == 0)
913 /* No tag after separator */
914 if (debug) GNUNET_break (0);
915 return GNUNET_SYSERR;
917 ctag = strtoul (tag_start, &tag_end, 10);
920 /* tag == 0 , invalid */
921 if (debug) GNUNET_break (0);
922 return GNUNET_SYSERR;
924 if ((ctag == ULONG_MAX) && (ERANGE == errno))
926 /* out of range: > ULONG_MAX */
927 if (debug) GNUNET_break (0);
928 return GNUNET_SYSERR;
930 if (ctag > UINT32_MAX)
932 /* out of range: > UINT32_MAX */
933 if (debug) GNUNET_break (0);
934 return GNUNET_SYSERR;
936 (*tag) = (uint32_t) ctag;
939 /* no char after tag */
940 if (debug) GNUNET_break (0);
941 return GNUNET_SYSERR;
943 if (url[strlen(url)] != tag_end[0])
945 /* there are more not converted chars after tag */
946 if (debug) GNUNET_break (0);
947 return GNUNET_SYSERR;
950 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
951 "Found tag `%u' in url\n", (*tag));
953 /* convert peer id */
954 target_start = strrchr (url, '/');
955 if (NULL == target_start)
958 target_start = (char *) url;
961 hash_length = separator - target_start;
962 if (hash_length != plugin->peer_id_length)
964 /* no char after tag */
965 if (debug) GNUNET_break (0);
966 return GNUNET_SYSERR;
968 memcpy (hash, target_start, hash_length);
969 hash[hash_length] = '\0';
971 if (GNUNET_OK != GNUNET_CRYPTO_hash_from_string ((const char *) hash, &(target->hashPubKey)))
973 /* hash conversion failed */
974 if (debug) GNUNET_break (0);
975 return GNUNET_SYSERR;
978 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
979 "Found target `%s' in url\n", GNUNET_h2s_full(&target->hashPubKey));
985 * Lookup a mhd connection and create one if none is found
987 * @param plugin the plugin handle
988 * @param mhd_connection the incoming mhd_connection
989 * @param url incoming requested URL
990 * @param method PUT or GET
991 * @return the server connecetion
993 static struct ServerConnection *
994 server_lookup_connection (struct HTTP_Server_Plugin *plugin,
995 struct MHD_Connection *mhd_connection, const char *url,
998 struct Session *s = NULL;
999 struct ServerConnection *sc = NULL;
1000 const union MHD_ConnectionInfo *conn_info;
1001 struct GNUNET_ATS_Information ats;
1006 struct GNUNET_PeerIdentity target;
1008 int direction = GNUNET_SYSERR;
1011 conn_info = MHD_get_connection_info (mhd_connection,
1012 MHD_CONNECTION_INFO_CLIENT_ADDRESS);
1013 if ((conn_info->client_addr->sa_family != AF_INET) &&
1014 (conn_info->client_addr->sa_family != AF_INET6))
1016 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1017 "New %s connection from %s\n", method, url);
1019 if (GNUNET_SYSERR == server_parse_url (plugin, url, &target, &tag))
1021 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1022 "Invalid url %s\n", url);
1025 if (0 == strcmp (MHD_HTTP_METHOD_PUT, method))
1026 direction = _RECEIVE;
1027 else if (0 == strcmp (MHD_HTTP_METHOD_GET, method))
1031 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1032 "Invalid method %s connection from %s\n", method, url);
1036 plugin->cur_connections++;
1037 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1038 "New %s connection from %s with tag %u (%u of %u)\n",
1040 GNUNET_i2s (&target), tag,
1041 plugin->cur_connections, plugin->max_connections);
1042 /* find duplicate session */
1046 if ((0 == memcmp (&s->target, &target, sizeof (struct GNUNET_PeerIdentity))) &&
1053 if ((_RECEIVE == direction) && (NULL != s->server_recv))
1055 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1056 "Duplicate PUT connection from `%s' tag %u, dismissing new connection\n",
1057 GNUNET_i2s (&target),
1062 if ((_SEND == direction) && (NULL != s->server_send))
1064 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1065 "Duplicate GET connection from `%s' tag %u, dismissing new connection\n",
1066 GNUNET_i2s (&target),
1073 /* create new session */
1074 switch (conn_info->client_addr->sa_family)
1077 addr = http_common_address_from_socket (plugin->protocol, conn_info->client_addr, sizeof (struct sockaddr_in));
1078 addr_len = http_common_address_get_size (addr);
1079 ats = plugin->env->get_address_type (plugin->env->cls, conn_info->client_addr, sizeof (struct sockaddr_in));
1082 addr = http_common_address_from_socket (plugin->protocol, conn_info->client_addr, sizeof (struct sockaddr_in6));
1083 addr_len = http_common_address_get_size (addr);
1084 ats = plugin->env->get_address_type (plugin->env->cls, conn_info->client_addr, sizeof (struct sockaddr_in6));
1090 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1091 "Creating new session for peer `%s' connecting from `%s'\n",
1092 GNUNET_i2s (&target),
1093 http_common_plugin_address_to_string (NULL, addr, addr_len));
1095 s = GNUNET_malloc (sizeof (struct Session));
1096 memcpy (&s->target, &target, sizeof (struct GNUNET_PeerIdentity));
1099 s->addrlen = addr_len;
1100 s->ats_address_network_type = ats.value;
1101 s->next_receive = GNUNET_TIME_UNIT_ZERO_ABS;
1103 s->server_recv = NULL;
1104 s->server_send = NULL;
1105 s->session_passed = GNUNET_NO;
1106 s->session_ended = GNUNET_NO;
1107 server_start_session_timeout(s);
1108 GNUNET_CONTAINER_DLL_insert (plugin->head, plugin->tail, s);
1110 sc = GNUNET_malloc (sizeof (struct ServerConnection));
1111 if (conn_info->client_addr->sa_family == AF_INET)
1112 sc->mhd_daemon = plugin->server_v4;
1113 if (conn_info->client_addr->sa_family == AF_INET6)
1114 sc->mhd_daemon = plugin->server_v6;
1115 sc->mhd_conn = mhd_connection;
1116 sc->direction = direction;
1117 sc->connected = GNUNET_NO;
1119 if (direction == _SEND)
1120 s->server_send = sc;
1121 if (direction == _RECEIVE)
1122 s->server_recv = sc;
1123 #if MHD_VERSION >= 0x00090E00
1124 if ((NULL == s->server_recv) || (NULL == s->server_send))
1126 to = (HTTP_SERVER_NOT_VALIDATED_TIMEOUT.rel_value / 1000);
1127 MHD_set_connection_option (mhd_connection, MHD_CONNECTION_OPTION_TIMEOUT, to);
1128 server_reschedule (plugin, sc->mhd_daemon, GNUNET_NO);
1132 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1133 "Session %p for peer `%s' fully connected\n",
1134 s, GNUNET_i2s (&target));
1135 to = (SERVER_SESSION_TIMEOUT.rel_value / 1000);
1136 server_mhd_connection_timeout (plugin, s, to);
1139 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1140 "Setting timeout for %p to %u sec.\n", sc, to);
1147 * Lookup a session for a server connection
1149 * @param plugin the plugin
1150 * @param sc the server connection
1151 * @return the session found or NULL
1153 static struct Session *
1154 server_lookup_session (struct HTTP_Server_Plugin *plugin,
1155 struct ServerConnection * sc)
1159 for (s = plugin->head; NULL != s; s = s->next)
1160 if ((s->server_recv == sc) || (s->server_send == sc))
1166 server_exist_session (struct HTTP_Server_Plugin *plugin, struct Session *s)
1168 struct Session * head;
1170 GNUNET_assert (NULL != plugin);
1171 GNUNET_assert (NULL != s);
1173 for (head = plugin->head; head != NULL; head = head->next)
1183 * Callback called by MHD when it needs data to send
1185 * @param cls current session
1186 * @param pos position in buffer
1187 * @param buf the buffer to write data to
1188 * @param max max number of bytes available in buffer
1189 * @return bytes written to buffer
1192 server_send_callback (void *cls, uint64_t pos, char *buf, size_t max)
1194 struct Session *s = cls;
1195 ssize_t bytes_read = 0;
1196 struct HTTP_Message *msg;
1199 GNUNET_assert (NULL != p);
1200 if (GNUNET_NO == server_exist_session (p, s))
1206 bytes_read = GNUNET_MIN (msg->size - msg->pos,
1208 memcpy (buf, &msg->buf[msg->pos], bytes_read);
1209 msg->pos += bytes_read;
1211 /* removing message */
1212 if (msg->pos == msg->size)
1214 GNUNET_CONTAINER_DLL_remove (s->msg_head, s->msg_tail, msg);
1215 if (NULL != msg->transmit_cont)
1216 msg->transmit_cont (msg->transmit_cont_cls, &s->target, GNUNET_OK,
1217 msg->size, msg->size + msg->overhead);
1223 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, s->plugin->name,
1224 "Sent %u bytes to peer `%s' with session %p \n", bytes_read, GNUNET_i2s (&s->target), s);
1225 GNUNET_asprintf (&stat_txt, "# bytes currently in %s_server buffers", p->protocol);
1226 GNUNET_STATISTICS_update (p->env->stats,
1227 stat_txt, -bytes_read, GNUNET_NO);
1228 GNUNET_free (stat_txt);
1229 GNUNET_asprintf (&stat_txt, "# bytes transmitted via %s_server", p->protocol);
1230 GNUNET_STATISTICS_update (p->env->stats,
1231 stat_txt, bytes_read, GNUNET_NO);
1232 GNUNET_free (stat_txt);
1239 * Callback called by MessageStreamTokenizer when a message has arrived
1241 * @param cls current session as closure
1242 * @param client client
1243 * @param message the message to be forwarded to transport service
1247 server_receive_mst_cb (void *cls, void *client,
1248 const struct GNUNET_MessageHeader *message)
1250 struct Session *s = cls;
1251 struct GNUNET_ATS_Information atsi[2];
1252 struct GNUNET_TIME_Relative delay;
1255 GNUNET_assert (NULL != p);
1256 if (GNUNET_NO == server_exist_session(p, s))
1259 struct HTTP_Server_Plugin *plugin = s->plugin;
1261 atsi[0].type = htonl (GNUNET_ATS_QUALITY_NET_DISTANCE);
1262 atsi[0].value = htonl (1);
1263 atsi[1].type = htonl (GNUNET_ATS_NETWORK_TYPE);
1264 atsi[1].value = s->ats_address_network_type;
1265 GNUNET_break (s->ats_address_network_type != ntohl (GNUNET_ATS_NET_UNSPECIFIED));
1268 delay = plugin->env->receive (plugin->env->cls,
1271 (const struct GNUNET_ATS_Information *) &atsi, 2,
1272 s, s->addr, s->addrlen);
1274 GNUNET_asprintf (&stat_txt, "# bytes received via %s_server", plugin->protocol);
1275 GNUNET_STATISTICS_update (plugin->env->stats,
1276 stat_txt, ntohs (message->size), GNUNET_NO);
1277 GNUNET_free (stat_txt);
1279 s->session_passed = GNUNET_YES;
1280 s->next_receive = GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (), delay);
1281 if (delay.rel_value > 0)
1283 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1284 "Peer `%s' address `%s' next read delayed for %llu ms\n",
1285 GNUNET_i2s (&s->target),
1286 http_common_plugin_address_to_string (NULL, s->addr, s->addrlen),
1289 server_reschedule_session_timeout (s);
1295 * MHD callback for a new incoming connection
1297 * @param cls the plugin handle
1298 * @param mhd_connection the mhd connection
1299 * @param url the requested URL
1300 * @param method GET or PUT
1301 * @param version HTTP version
1302 * @param upload_data upload data
1303 * @param upload_data_size sizeof upload data
1304 * @param httpSessionCache the session cache to remember the connection
1305 * @return MHD_YES if connection is accepted, MHD_NO on reject
1308 server_access_cb (void *cls, struct MHD_Connection *mhd_connection,
1309 const char *url, const char *method, const char *version,
1310 const char *upload_data, size_t * upload_data_size,
1311 void **httpSessionCache)
1313 struct HTTP_Server_Plugin *plugin = cls;
1316 struct ServerConnection *sc = *httpSessionCache;
1318 struct MHD_Response *response;
1320 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1321 _("Access from connection %p (%u of %u) for `%s' `%s' url `%s' with upload data size %u\n"),
1323 plugin->cur_connections, plugin->max_connections,
1324 method, version, url, (*upload_data_size));
1326 GNUNET_assert (cls != NULL);
1329 /* new connection */
1330 sc = server_lookup_connection (plugin, mhd_connection, url, method);
1333 (*httpSessionCache) = sc;
1337 response = MHD_create_response_from_data (strlen (HTTP_ERROR_RESPONSE), HTTP_ERROR_RESPONSE, MHD_NO, MHD_NO);
1338 res = MHD_queue_response (mhd_connection, MHD_HTTP_NOT_FOUND, response);
1339 MHD_destroy_response (response);
1345 /* 'old' connection */
1346 if (NULL == server_lookup_session (plugin, sc))
1348 /* Session was already disconnected */
1353 /* existing connection */
1354 sc = (*httpSessionCache);
1356 GNUNET_assert (NULL != s);
1357 /* connection is to be disconnected */
1358 if (sc->disconnect == GNUNET_YES)
1360 /* Sent HTTP/1.1: 200 OK as response */
1361 response = MHD_create_response_from_data (strlen ("Thank you!"),
1364 res = MHD_queue_response (mhd_connection, MHD_HTTP_OK, response);
1365 MHD_destroy_response (response);
1368 GNUNET_assert (s != NULL);
1370 if (sc->direction == _SEND)
1372 response = MHD_create_response_from_callback (MHD_SIZE_UNKNOWN,
1374 &server_send_callback, s,
1376 MHD_queue_response (mhd_connection, MHD_HTTP_OK, response);
1377 MHD_destroy_response (response);
1380 if (sc->direction == _RECEIVE)
1382 if ((*upload_data_size == 0) && (sc->connected == GNUNET_NO))
1384 /* (*upload_data_size == 0) first callback when header are passed */
1385 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1386 "Session %p / Connection %p: Peer `%s' PUT on address `%s' connected\n",
1388 GNUNET_i2s (&s->target),
1389 http_common_plugin_address_to_string (NULL,
1392 sc->connected = GNUNET_YES;
1395 else if ((*upload_data_size == 0) && (sc->connected == GNUNET_YES))
1397 /* (*upload_data_size == 0) when upload is complete */
1398 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1399 "Session %p / Connection %p: Peer `%s' PUT on address `%s' finished upload\n",
1401 GNUNET_i2s (&s->target),
1402 http_common_plugin_address_to_string (NULL,
1405 sc->connected = GNUNET_NO;
1406 /* Sent HTTP/1.1: 200 OK as PUT Response\ */
1407 response = MHD_create_response_from_data (strlen ("Thank you!"),
1410 res = MHD_queue_response (mhd_connection, MHD_HTTP_OK, response);
1411 MHD_destroy_response (response);
1414 else if ((*upload_data_size > 0) && (sc->connected == GNUNET_YES))
1416 /* (*upload_data_size > 0) for every segment received */
1417 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1418 "Session %p / Connection %p: Peer `%s' PUT on address `%s' received %u bytes\n",
1420 GNUNET_i2s (&s->target),
1421 http_common_plugin_address_to_string (NULL,
1425 struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get ();
1427 if ((s->next_receive.abs_value <= now.abs_value))
1429 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1430 "PUT with %u bytes forwarded to MST\n",
1432 if (s->msg_tk == NULL)
1434 s->msg_tk = GNUNET_SERVER_mst_create (&server_receive_mst_cb, s);
1436 GNUNET_SERVER_mst_receive (s->msg_tk, s, upload_data,
1437 *upload_data_size, GNUNET_NO, GNUNET_NO);
1438 #if MHD_VERSION >= 0x00090E00
1439 server_mhd_connection_timeout (plugin, s, GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT.rel_value / 1000);
1441 (*upload_data_size) = 0;
1445 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1446 "Session %p / Connection %p: no inbound bandwidth available! Next read was delayed by %llu ms\n",
1447 s, sc, now.abs_value - s->next_receive.abs_value);
1462 * Callback from MHD when a connection disconnects
1464 * @param cls closure
1465 * @param connection the disconnected MHD connection
1466 * @param httpSessionCache the pointer to distinguish
1469 server_disconnect_cb (void *cls, struct MHD_Connection *connection,
1470 void **httpSessionCache)
1472 struct ServerConnection *sc = *httpSessionCache;
1473 struct Session *s = NULL;
1474 struct Session *t = NULL;
1475 struct HTTP_Server_Plugin *plugin = NULL;
1477 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, p->name,
1478 "Disconnect for connection %p \n", sc);
1483 if (NULL == (s = server_lookup_session (p, sc)))
1486 GNUNET_assert (NULL != p);
1487 for (t = p->head; t != NULL; t = t->next)
1496 if (sc->direction == _SEND)
1499 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1500 "Peer `%s' connection %p, GET on address `%s' disconnected\n",
1501 GNUNET_i2s (&s->target), s->server_send,
1502 http_common_plugin_address_to_string (NULL, s->addr, s->addrlen));
1503 s->server_send = NULL;
1504 if (NULL != (s->server_recv))
1506 s->server_recv->disconnect = GNUNET_YES;
1507 GNUNET_assert (NULL != s->server_recv->mhd_conn);
1508 #if MHD_VERSION >= 0x00090E00
1509 MHD_set_connection_option (s->server_recv->mhd_conn, MHD_CONNECTION_OPTION_TIMEOUT,
1512 server_reschedule (plugin, s->server_recv->mhd_daemon, GNUNET_NO);
1515 if (sc->direction == _RECEIVE)
1517 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1518 "Peer `%s' connection %p PUT on address `%s' disconnected\n",
1519 GNUNET_i2s (&s->target), s->server_recv,
1520 http_common_plugin_address_to_string (NULL, s->addr, s->addrlen));
1521 s->server_recv = NULL;
1522 /* Do not terminate session when PUT disconnects
1523 if (NULL != (s->server_send))
1525 s->server_send->disconnect = GNUNET_YES;
1526 GNUNET_assert (NULL != s->server_send->mhd_conn);
1527 #if MHD_VERSION >= 0x00090E00
1528 MHD_set_connection_option (s->server_send->mhd_conn, MHD_CONNECTION_OPTION_TIMEOUT,
1531 server_reschedule (plugin, s->server_send->mhd_daemon, GNUNET_NO);
1533 if (s->msg_tk != NULL)
1535 GNUNET_SERVER_mst_destroy (s->msg_tk);
1541 plugin->cur_connections--;
1543 if ((s->server_send == NULL) && (s->server_recv == NULL))
1545 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1546 "Peer `%s' on address `%s' disconnected\n",
1547 GNUNET_i2s (&s->target),
1548 http_common_plugin_address_to_string (NULL, s->addr, s->addrlen));
1550 if ((GNUNET_YES == s->session_passed) && (GNUNET_NO == s->session_ended))
1552 /* Notify transport immediately that this session is invalid */
1553 s->session_ended = GNUNET_YES;
1554 plugin->env->session_end (plugin->env->cls, &s->target, s);
1556 server_delete_session (s);
1563 * Check if incoming connection is accepted.
1565 * @param cls plugin as closure
1566 * @param addr address of incoming connection
1567 * @param addr_len address length of incoming connection
1568 * @return MHD_YES if connection is accepted, MHD_NO if connection is rejected
1572 server_accept_cb (void *cls, const struct sockaddr *addr, socklen_t addr_len)
1574 struct HTTP_Server_Plugin *plugin = cls;
1576 if (plugin->cur_connections <= plugin->max_connections)
1578 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1579 _("Accepting connection (%u of %u) from `%s'\n"),
1580 plugin->cur_connections, plugin->max_connections,
1581 GNUNET_a2s (addr, addr_len));
1586 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, plugin->name,
1587 _("Server reached maximum number connections (%u), rejecting new connection\n"),
1588 plugin->max_connections);
1594 server_log (void *arg, const char *fmt, va_list ap)
1598 vsnprintf (text, sizeof (text), fmt, ap);
1600 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Server: %s\n", text);
1605 * Call MHD IPv4 to process pending requests and then go back
1606 * and schedule the next run.
1607 * @param cls plugin as closure
1608 * @param tc task context
1611 server_v4_run (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1613 struct HTTP_Server_Plugin *plugin = cls;
1615 GNUNET_assert (cls != NULL);
1617 plugin->server_v4_task = GNUNET_SCHEDULER_NO_TASK;
1618 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1621 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1622 "Running IPv4 server\n");
1624 plugin->server_v4_immediately = GNUNET_NO;
1625 GNUNET_assert (MHD_YES == MHD_run (plugin->server_v4));
1626 server_reschedule (plugin, plugin->server_v4, GNUNET_NO);
1631 * Call MHD IPv6 to process pending requests and then go back
1632 * and schedule the next run.
1633 * @param cls plugin as closure
1634 * @param tc task context
1637 server_v6_run (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1639 struct HTTP_Server_Plugin *plugin = cls;
1641 GNUNET_assert (cls != NULL);
1642 plugin->server_v6_task = GNUNET_SCHEDULER_NO_TASK;
1643 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1646 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1647 "Running IPv6 server\n");
1649 plugin->server_v6_immediately = GNUNET_NO;
1650 GNUNET_assert (MHD_YES == MHD_run (plugin->server_v6));
1651 server_reschedule (plugin, plugin->server_v6, GNUNET_NO);
1655 #define UNSIGNED_MHD_LONG_LONG unsigned MHD_LONG_LONG
1658 * Function that queries MHD's select sets and
1659 * starts the task waiting for them.
1661 * @param plugin plugin
1662 * @param daemon_handle the MHD daemon handle
1663 * @return gnunet task identifier
1665 static GNUNET_SCHEDULER_TaskIdentifier
1666 server_schedule (struct HTTP_Server_Plugin *plugin,
1667 struct MHD_Daemon *daemon_handle,
1670 GNUNET_SCHEDULER_TaskIdentifier ret;
1674 struct GNUNET_NETWORK_FDSet *wrs;
1675 struct GNUNET_NETWORK_FDSet *wws;
1676 struct GNUNET_NETWORK_FDSet *wes;
1678 UNSIGNED_MHD_LONG_LONG timeout;
1679 static unsigned long long last_timeout = 0;
1682 struct GNUNET_TIME_Relative tv;
1684 if (GNUNET_YES == plugin->in_shutdown)
1685 return GNUNET_SCHEDULER_NO_TASK;
1687 ret = GNUNET_SCHEDULER_NO_TASK;
1691 wrs = GNUNET_NETWORK_fdset_create ();
1692 wes = GNUNET_NETWORK_fdset_create ();
1693 wws = GNUNET_NETWORK_fdset_create ();
1695 GNUNET_assert (MHD_YES == MHD_get_fdset (daemon_handle, &rs, &ws, &es, &max));
1696 haveto = MHD_get_timeout (daemon_handle, &timeout);
1697 if (haveto == MHD_YES)
1699 if (timeout != last_timeout)
1702 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1703 "SELECT Timeout changed from %llu to %llu\n",
1704 last_timeout, timeout);
1705 last_timeout = timeout;
1707 if (timeout <= GNUNET_TIME_UNIT_SECONDS.rel_value)
1708 tv.rel_value = (uint64_t) timeout;
1710 tv = GNUNET_TIME_UNIT_SECONDS;
1713 tv = GNUNET_TIME_UNIT_SECONDS;
1714 /* Force immediate run, since we have outbound data to send */
1715 if (now == GNUNET_YES)
1716 tv = GNUNET_TIME_UNIT_MILLISECONDS;
1717 GNUNET_NETWORK_fdset_copy_native (wrs, &rs, max + 1);
1718 GNUNET_NETWORK_fdset_copy_native (wws, &ws, max + 1);
1719 GNUNET_NETWORK_fdset_copy_native (wes, &es, max + 1);
1721 if (daemon_handle == plugin->server_v4)
1723 if (plugin->server_v4_task != GNUNET_SCHEDULER_NO_TASK)
1725 GNUNET_SCHEDULER_cancel (plugin->server_v4_task);
1726 plugin->server_v4_task = GNUNET_SCHEDULER_NO_TASK;
1729 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1730 "Scheduling IPv4 server task in %llu ms\n", tv);
1733 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1735 &server_v4_run, plugin);
1737 if (daemon_handle == plugin->server_v6)
1739 if (plugin->server_v6_task != GNUNET_SCHEDULER_NO_TASK)
1741 GNUNET_SCHEDULER_cancel (plugin->server_v6_task);
1742 plugin->server_v6_task = GNUNET_SCHEDULER_NO_TASK;
1745 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1746 "Scheduling IPv6 server task in %llu ms\n", tv);
1749 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1751 &server_v6_run, plugin);
1753 GNUNET_NETWORK_fdset_destroy (wrs);
1754 GNUNET_NETWORK_fdset_destroy (wws);
1755 GNUNET_NETWORK_fdset_destroy (wes);
1762 * Load ssl certificate from file
1764 * @param file filename
1765 * @return content of the file
1768 server_load_file (const char *file)
1770 struct GNUNET_DISK_FileHandle *gn_file;
1774 if (GNUNET_OK != GNUNET_DISK_file_size (file,
1775 &fsize, GNUNET_NO, GNUNET_YES))
1777 text = GNUNET_malloc (fsize + 1);
1779 GNUNET_DISK_file_open (file, GNUNET_DISK_OPEN_READ,
1780 GNUNET_DISK_PERM_USER_READ);
1781 if (gn_file == NULL)
1786 if (GNUNET_SYSERR == GNUNET_DISK_file_read (gn_file, text, fsize))
1789 GNUNET_DISK_file_close (gn_file);
1793 GNUNET_DISK_file_close (gn_file);
1801 * Load ssl certificate
1803 * @param plugin the plugin
1804 * @return GNUNET_OK on success, GNUNET_SYSERR on failure
1807 server_load_certificate (struct HTTP_Server_Plugin *plugin)
1809 int res = GNUNET_OK;
1815 /* Get crypto init string from config
1816 * If not present just use default values */
1819 GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg,
1824 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
1825 "Failed to get servicehome!\n");
1826 return GNUNET_SYSERR;
1831 GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg,
1834 &plugin->crypto_init))
1835 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1836 "Using crypto init string `%s'\n",
1837 plugin->crypto_init);
1839 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1840 "Using default crypto init string \n");
1843 GNUNET_CONFIGURATION_get_value_filename (plugin->env->cfg, plugin->name,
1844 "KEY_FILE", &key_file))
1847 GNUNET_asprintf (&key_file, "%s/%s", sh, "https_key.key");
1852 GNUNET_CONFIGURATION_get_value_filename (plugin->env->cfg, plugin->name,
1853 "CERT_FILE", &cert_file))
1856 GNUNET_asprintf (&cert_file, "%s/%s", sh, "https_cert.crt");
1859 /* read key & certificates from file */
1860 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1861 "Trying to loading TLS certificate from key-file `%s' cert-file`%s'\n",
1862 key_file, cert_file);
1864 plugin->key = server_load_file (key_file);
1865 plugin->cert = server_load_file (cert_file);
1867 if ((plugin->key == NULL) || (plugin->cert == NULL))
1869 struct GNUNET_OS_Process *cert_creation;
1871 GNUNET_free_non_null (plugin->key);
1873 GNUNET_free_non_null (plugin->cert);
1874 plugin->cert = NULL;
1876 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1877 "No usable TLS certificate found, creating certificate\n");
1880 GNUNET_OS_start_process (GNUNET_NO, GNUNET_OS_INHERIT_STD_OUT_AND_ERR, NULL, NULL,
1881 "gnunet-transport-certificate-creation",
1882 "gnunet-transport-certificate-creation",
1883 key_file, cert_file, NULL);
1884 if (cert_creation == NULL)
1886 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
1888 ("Could not create a new TLS certificate, program `gnunet-transport-certificate-creation' could not be started!\n"));
1889 GNUNET_free (key_file);
1890 GNUNET_free (cert_file);
1892 GNUNET_free_non_null (plugin->key);
1894 GNUNET_free_non_null (plugin->cert);
1895 plugin->cert = NULL;
1896 GNUNET_free_non_null (plugin->crypto_init);
1897 plugin->crypto_init = NULL;
1899 return GNUNET_SYSERR;
1901 GNUNET_assert (GNUNET_OK == GNUNET_OS_process_wait (cert_creation));
1902 GNUNET_OS_process_destroy (cert_creation);
1904 plugin->key = server_load_file (key_file);
1905 plugin->cert = server_load_file (cert_file);
1908 if ((plugin->key == NULL) || (plugin->cert == NULL))
1910 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
1912 ("No usable TLS certificate found and creating one failed!\n"),
1914 GNUNET_free (key_file);
1915 GNUNET_free (cert_file);
1917 GNUNET_free_non_null (plugin->key);
1919 GNUNET_free_non_null (plugin->cert);
1920 plugin->cert = NULL;
1921 GNUNET_free_non_null (plugin->crypto_init);
1922 plugin->crypto_init = NULL;
1924 return GNUNET_SYSERR;
1926 GNUNET_free (key_file);
1927 GNUNET_free (cert_file);
1928 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "TLS certificate loaded\n");
1935 * Start the HTTP server
1937 * @param plugin the plugin handle
1938 * @return GNUNET_OK on success, GNUNET_SYSERR on failure
1941 server_start (struct HTTP_Server_Plugin *plugin)
1943 unsigned int timeout;
1944 GNUNET_assert (NULL != plugin);
1947 if (GNUNET_SYSERR == server_load_certificate (plugin))
1949 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
1950 "Could not load or create server certificate! Loading plugin failed!\n");
1951 return GNUNET_SYSERR;
1956 #if MHD_VERSION >= 0x00090E00
1957 timeout = HTTP_SERVER_NOT_VALIDATED_TIMEOUT.rel_value / 1000;
1958 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
1959 "MHD can set timeout per connection! Default time out %u sec.\n",
1962 timeout = SERVER_SESSION_TIMEOUT.rel_value / 1000;
1963 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, plugin->name,
1964 "MHD cannot set timeout per connection! Default time out %u sec.\n",
1967 plugin->server_v4 = NULL;
1968 if (plugin->use_ipv4 == GNUNET_YES)
1970 plugin->server_v4 = MHD_start_daemon (
1977 MHD_NO_FLAG, plugin->port,
1978 &server_accept_cb, plugin,
1979 &server_access_cb, plugin,
1980 MHD_OPTION_SOCK_ADDR,
1981 (struct sockaddr_in *)
1982 plugin->server_addr_v4,
1983 MHD_OPTION_CONNECTION_LIMIT,
1985 plugin->max_connections,
1987 MHD_OPTION_HTTPS_PRIORITIES,
1988 plugin->crypto_init,
1989 MHD_OPTION_HTTPS_MEM_KEY,
1991 MHD_OPTION_HTTPS_MEM_CERT,
1994 MHD_OPTION_CONNECTION_TIMEOUT,
1996 MHD_OPTION_CONNECTION_MEMORY_LIMIT,
1998 GNUNET_SERVER_MAX_MESSAGE_SIZE),
1999 MHD_OPTION_NOTIFY_COMPLETED,
2000 &server_disconnect_cb, plugin,
2001 MHD_OPTION_EXTERNAL_LOGGER,
2002 server_log, NULL, MHD_OPTION_END);
2004 plugin->server_v6 = NULL;
2005 if (plugin->use_ipv6 == GNUNET_YES)
2007 plugin->server_v6 = MHD_start_daemon (
2014 MHD_USE_IPv6, plugin->port,
2015 &server_accept_cb, plugin,
2016 &server_access_cb, plugin,
2017 MHD_OPTION_SOCK_ADDR,
2018 (struct sockaddr_in6 *)
2019 plugin->server_addr_v6,
2020 MHD_OPTION_CONNECTION_LIMIT,
2022 plugin->max_connections,
2024 MHD_OPTION_HTTPS_PRIORITIES,
2025 plugin->crypto_init,
2026 MHD_OPTION_HTTPS_MEM_KEY,
2028 MHD_OPTION_HTTPS_MEM_CERT,
2031 MHD_OPTION_CONNECTION_TIMEOUT,
2033 MHD_OPTION_CONNECTION_MEMORY_LIMIT,
2035 GNUNET_SERVER_MAX_MESSAGE_SIZE),
2036 MHD_OPTION_NOTIFY_COMPLETED,
2037 &server_disconnect_cb, plugin,
2038 MHD_OPTION_EXTERNAL_LOGGER,
2039 server_log, NULL, MHD_OPTION_END);
2043 if ((plugin->use_ipv4 == GNUNET_YES) && (plugin->server_v4 == NULL))
2045 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
2046 "Failed to start %s IPv4 server component on port %u\n",
2047 plugin->name, plugin->port);
2048 return GNUNET_SYSERR;
2050 server_reschedule (plugin, plugin->server_v4, GNUNET_NO);
2052 if ((plugin->use_ipv6 == GNUNET_YES) && (plugin->server_v6 == NULL))
2054 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
2055 "Failed to start %s IPv6 server component on port %u\n",
2056 plugin->name, plugin->port);
2057 return GNUNET_SYSERR;
2059 server_reschedule (plugin, plugin->server_v6, GNUNET_NO);
2060 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2061 "%s server component started on port %u\n", plugin->name,
2068 server_stop (struct HTTP_Server_Plugin *plugin)
2070 if (plugin->server_v4 != NULL)
2072 MHD_stop_daemon (plugin->server_v4);
2073 plugin->server_v4 = NULL;
2075 if ( plugin->server_v6 != NULL)
2077 MHD_stop_daemon (plugin->server_v6);
2078 plugin->server_v6 = NULL;
2082 if (plugin->server_v4_task != GNUNET_SCHEDULER_NO_TASK)
2084 GNUNET_SCHEDULER_cancel (plugin->server_v4_task);
2085 plugin->server_v4_task = GNUNET_SCHEDULER_NO_TASK;
2088 if (plugin->server_v6_task != GNUNET_SCHEDULER_NO_TASK)
2090 GNUNET_SCHEDULER_cancel (plugin->server_v6_task);
2091 plugin->server_v6_task = GNUNET_SCHEDULER_NO_TASK;
2096 GNUNET_free_non_null (plugin->crypto_init);
2097 GNUNET_free_non_null (plugin->cert);
2098 GNUNET_free_non_null (plugin->key);
2101 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2102 "%s server component stopped\n", plugin->name);
2107 * Add an address to the server's set of addresses and notify transport
2109 * @param cls the plugin handle
2110 * @param add_remove GNUNET_YES on add, GNUNET_NO on remove
2111 * @param addr the address
2112 * @param addrlen address length
2115 server_add_address (void *cls, int add_remove, const struct sockaddr *addr,
2118 struct HTTP_Server_Plugin *plugin = cls;
2119 struct HttpAddressWrapper *w = NULL;
2121 w = GNUNET_malloc (sizeof (struct HttpAddressWrapper));
2122 w->addr = http_common_address_from_socket (plugin->protocol, addr, addrlen);
2123 if (NULL == w->addr)
2128 w->addrlen = http_common_address_get_size (w->addr);
2130 GNUNET_CONTAINER_DLL_insert(plugin->addr_head, plugin->addr_tail, w);
2131 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2132 "Notifying transport to add address `%s'\n",
2133 http_common_plugin_address_to_string(NULL, w->addr, w->addrlen));
2135 plugin->env->notify_address (plugin->env->cls, add_remove, w->addr, w->addrlen, "https_client");
2137 plugin->env->notify_address (plugin->env->cls, add_remove, w->addr, w->addrlen, "http_client");
2143 * Remove an address from the server's set of addresses and notify transport
2145 * @param cls the plugin handle
2146 * @param add_remove GNUNET_YES on add, GNUNET_NO on remove
2147 * @param addr the address
2148 * @param addrlen address length
2151 server_remove_address (void *cls, int add_remove, const struct sockaddr *addr,
2154 struct HTTP_Server_Plugin *plugin = cls;
2155 struct HttpAddressWrapper *w = plugin->addr_head;
2157 void * saddr = http_common_address_from_socket (plugin->protocol, addr, addrlen);
2160 saddr_len = http_common_address_get_size (saddr);
2164 if (GNUNET_YES == http_common_cmp_addresses(w->addr, w->addrlen, saddr, saddr_len))
2168 GNUNET_free (saddr);
2173 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2174 "Notifying transport to remove address `%s'\n",
2175 http_common_plugin_address_to_string (NULL, w->addr, w->addrlen));
2176 GNUNET_CONTAINER_DLL_remove (plugin->addr_head, plugin->addr_tail, w);
2178 plugin->env->notify_address (plugin->env->cls, add_remove, w->addr, w->addrlen, "https_client");
2180 plugin->env->notify_address (plugin->env->cls, add_remove, w->addr, w->addrlen, "http_client");
2182 GNUNET_free (w->addr);
2189 * Our external IP address/port mapping has changed.
2191 * @param cls closure, the 'struct LocalAddrList'
2192 * @param add_remove GNUNET_YES to mean the new public IP address, GNUNET_NO to mean
2193 * the previous (now invalid) one
2194 * @param addr either the previous or the new public IP address
2195 * @param addrlen actual lenght of the address
2198 server_nat_port_map_callback (void *cls, int add_remove, const struct sockaddr *addr,
2201 GNUNET_assert (cls != NULL);
2202 struct HTTP_Server_Plugin *plugin = cls;
2204 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2205 "NAT called to %s address `%s'\n",
2206 (add_remove == GNUNET_NO) ? "remove" : "add",
2207 GNUNET_a2s (addr, addrlen));
2209 if (AF_INET == addr->sa_family)
2211 struct sockaddr_in *s4 = (struct sockaddr_in *) addr;
2213 if (GNUNET_NO == plugin->use_ipv4)
2216 if ((NULL != plugin->server_addr_v4) &&
2217 (0 != memcmp (&plugin->server_addr_v4->sin_addr,
2218 &s4->sin_addr, sizeof (struct in_addr))))
2220 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2221 "Skipping address `%s' (not bindto address)\n",
2222 GNUNET_a2s (addr, addrlen));
2227 if (AF_INET6 == addr->sa_family)
2229 struct sockaddr_in6 *s6 = (struct sockaddr_in6 *) addr;
2230 if (GNUNET_NO == plugin->use_ipv6)
2233 if ((NULL != plugin->server_addr_v6) &&
2234 (0 != memcmp (&plugin->server_addr_v6->sin6_addr,
2235 &s6->sin6_addr, sizeof (struct in6_addr))))
2237 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2238 "Skipping address `%s' (not bindto address)\n",
2239 GNUNET_a2s (addr, addrlen));
2247 server_add_address (cls, add_remove, addr, addrlen);
2250 server_remove_address (cls, add_remove, addr, addrlen);
2257 * Get valid server addresses
2259 * @param plugin the plugin handle
2260 * @param serviceName the servicename
2261 * @param cfg configuration handle
2262 * @param addrs addresses
2263 * @param addr_lens address length
2264 * @return number of addresses
2267 server_get_addresses (struct HTTP_Server_Plugin *plugin,
2268 const char *serviceName,
2269 const struct GNUNET_CONFIGURATION_Handle *cfg,
2270 struct sockaddr ***addrs, socklen_t ** addr_lens)
2273 unsigned long long port;
2274 struct addrinfo hints;
2275 struct addrinfo *res;
2276 struct addrinfo *pos;
2277 struct addrinfo *next;
2281 struct sockaddr **saddrs;
2282 socklen_t *saddrlens;
2288 disablev6 = !plugin->use_ipv6;
2291 if (GNUNET_CONFIGURATION_have_value (cfg, serviceName, "PORT"))
2293 GNUNET_break (GNUNET_OK ==
2294 GNUNET_CONFIGURATION_get_value_number (cfg, serviceName,
2298 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2300 ("Require valid port number for service in configuration!\n"));
2301 return GNUNET_SYSERR;
2306 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, plugin->name,
2307 "Starting in listen only mode\n");
2308 return -1; /* listen only */
2312 if (GNUNET_CONFIGURATION_have_value (cfg, serviceName, "BINDTO"))
2314 GNUNET_break (GNUNET_OK ==
2315 GNUNET_CONFIGURATION_get_value_string (cfg, serviceName,
2316 "BINDTO", &hostname));
2321 if (hostname != NULL)
2323 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2324 "Resolving `%s' since that is where `%s' will bind to.\n",
2325 hostname, serviceName);
2326 memset (&hints, 0, sizeof (struct addrinfo));
2328 hints.ai_family = AF_INET;
2329 if ((0 != (ret = getaddrinfo (hostname, NULL, &hints, &res))) ||
2332 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Failed to resolve `%s': %s\n"),
2333 hostname, gai_strerror (ret));
2334 GNUNET_free (hostname);
2335 return GNUNET_SYSERR;
2339 while (NULL != (pos = next))
2341 next = pos->ai_next;
2342 if ((disablev6) && (pos->ai_family == AF_INET6))
2348 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2349 _("Failed to find %saddress for `%s'.\n"),
2350 disablev6 ? "IPv4 " : "", hostname);
2352 GNUNET_free (hostname);
2353 return GNUNET_SYSERR;
2356 saddrs = GNUNET_malloc ((resi + 1) * sizeof (struct sockaddr *));
2357 saddrlens = GNUNET_malloc ((resi + 1) * sizeof (socklen_t));
2360 while (NULL != (pos = next))
2362 next = pos->ai_next;
2363 if ((disablev6) && (pos->ai_family == AF_INET6))
2365 if ((pos->ai_protocol != IPPROTO_TCP) && (pos->ai_protocol != 0))
2366 continue; /* not TCP */
2367 if ((pos->ai_socktype != SOCK_STREAM) && (pos->ai_socktype != 0))
2368 continue; /* huh? */
2369 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2370 "Service will bind to `%s'\n", GNUNET_a2s (pos->ai_addr,
2372 if (pos->ai_family == AF_INET)
2374 GNUNET_assert (pos->ai_addrlen == sizeof (struct sockaddr_in));
2375 saddrlens[i] = pos->ai_addrlen;
2376 saddrs[i] = GNUNET_malloc (saddrlens[i]);
2377 memcpy (saddrs[i], pos->ai_addr, saddrlens[i]);
2378 ((struct sockaddr_in *) saddrs[i])->sin_port = htons (port);
2382 GNUNET_assert (pos->ai_family == AF_INET6);
2383 GNUNET_assert (pos->ai_addrlen == sizeof (struct sockaddr_in6));
2384 saddrlens[i] = pos->ai_addrlen;
2385 saddrs[i] = GNUNET_malloc (saddrlens[i]);
2386 memcpy (saddrs[i], pos->ai_addr, saddrlens[i]);
2387 ((struct sockaddr_in6 *) saddrs[i])->sin6_port = htons (port);
2391 GNUNET_free (hostname);
2397 /* will bind against everything, just set port */
2403 saddrs = GNUNET_malloc ((resi + 1) * sizeof (struct sockaddr *));
2404 saddrlens = GNUNET_malloc ((resi + 1) * sizeof (socklen_t));
2406 saddrlens[i] = sizeof (struct sockaddr_in);
2407 saddrs[i] = GNUNET_malloc (saddrlens[i]);
2408 #if HAVE_SOCKADDR_IN_SIN_LEN
2409 ((struct sockaddr_in *) saddrs[i])->sin_len = saddrlens[i];
2411 ((struct sockaddr_in *) saddrs[i])->sin_family = AF_INET;
2412 ((struct sockaddr_in *) saddrs[i])->sin_port = htons (port);
2418 saddrs = GNUNET_malloc ((resi + 1) * sizeof (struct sockaddr *));
2419 saddrlens = GNUNET_malloc ((resi + 1) * sizeof (socklen_t));
2421 saddrlens[i] = sizeof (struct sockaddr_in6);
2422 saddrs[i] = GNUNET_malloc (saddrlens[i]);
2423 #if HAVE_SOCKADDR_IN_SIN_LEN
2424 ((struct sockaddr_in6 *) saddrs[i])->sin6_len = saddrlens[0];
2426 ((struct sockaddr_in6 *) saddrs[i])->sin6_family = AF_INET6;
2427 ((struct sockaddr_in6 *) saddrs[i])->sin6_port = htons (port);
2429 saddrlens[i] = sizeof (struct sockaddr_in);
2430 saddrs[i] = GNUNET_malloc (saddrlens[i]);
2431 #if HAVE_SOCKADDR_IN_SIN_LEN
2432 ((struct sockaddr_in *) saddrs[i])->sin_len = saddrlens[1];
2434 ((struct sockaddr_in *) saddrs[i])->sin_family = AF_INET;
2435 ((struct sockaddr_in *) saddrs[i])->sin_port = htons (port);
2439 *addr_lens = saddrlens;
2445 * Ask NAT for addresses
2447 * @param plugin the plugin handle
2450 server_start_report_addresses (struct HTTP_Server_Plugin *plugin)
2452 int res = GNUNET_OK;
2453 struct sockaddr **addrs;
2454 socklen_t *addrlens;
2456 res = server_get_addresses (plugin,
2457 plugin->name, plugin->env->cfg,
2459 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2460 _("Found %u addresses to report to NAT service\n"), res);
2462 if (GNUNET_SYSERR == res)
2469 GNUNET_NAT_register (plugin->env->cfg, GNUNET_YES, plugin->port,
2471 (const struct sockaddr **) addrs, addrlens,
2472 &server_nat_port_map_callback, NULL, plugin);
2476 GNUNET_assert (addrs[res] != NULL);
2477 GNUNET_free (addrs[res]);
2479 GNUNET_free_non_null (addrs);
2480 GNUNET_free_non_null (addrlens);
2485 * Stop NAT for addresses
2487 * @param plugin the plugin handle
2490 server_stop_report_addresses (struct HTTP_Server_Plugin *plugin)
2492 /* Stop NAT handle */
2493 if (NULL != plugin->nat)
2494 GNUNET_NAT_unregister (plugin->nat);
2496 /* Clean up addresses */
2497 struct HttpAddressWrapper *w;
2499 while (plugin->addr_head != NULL)
2501 w = plugin->addr_head;
2502 GNUNET_CONTAINER_DLL_remove (plugin->addr_head, plugin->addr_tail, w);
2503 GNUNET_free (w->addr);
2510 * Check if IPv6 supported on this system
2512 * @param plugin the plugin handle
2513 * @return GNUNET_YES on success, else GNUNET_NO
2516 server_check_ipv6_support (struct HTTP_Server_Plugin *plugin)
2518 struct GNUNET_NETWORK_Handle *desc = NULL;
2519 int res = GNUNET_NO;
2521 /* Probe IPv6 support */
2522 desc = GNUNET_NETWORK_socket_create (PF_INET6, SOCK_STREAM, 0);
2525 if ((errno == ENOBUFS) || (errno == ENOMEM) || (errno == ENFILE) ||
2528 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "socket");
2530 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, plugin->name,
2532 ("Disabling IPv6 since it is not supported on this system!\n"));
2537 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (desc));
2541 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2542 "Testing IPv6 on this system: %s\n",
2543 (res == GNUNET_YES) ? "successful" : "failed");
2549 * Function called when the service shuts down. Unloads our plugins
2550 * and cancels pending validations.
2552 * @param cls closure, unused
2553 * @param tc task context (unused)
2556 server_notify_external_hostname (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2558 struct HTTP_Server_Plugin *plugin = cls;
2560 plugin->notify_ext_task = GNUNET_SCHEDULER_NO_TASK;
2562 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
2565 GNUNET_asprintf(&plugin->ext_addr, "%s://%s", plugin->protocol, plugin->external_hostname);
2566 plugin->ext_addr_len = strlen (plugin->ext_addr) + 1;
2567 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2568 "Notifying transport about external hostname address `%s'\n", plugin->ext_addr);
2571 plugin->env->notify_address (plugin->env->cls, GNUNET_YES,
2572 plugin->ext_addr, plugin->ext_addr_len,
2575 plugin->env->notify_address (plugin->env->cls, GNUNET_YES,
2576 plugin->ext_addr, plugin->ext_addr_len,
2583 * Configure the plugin
2585 * @param plugin plugin handle
2586 * @return GNUNET_OK on success, GNUNET_SYSERR on failure
2589 server_configure_plugin (struct HTTP_Server_Plugin *plugin)
2591 unsigned long long port;
2592 unsigned long long max_connections;
2593 char *bind4_address = NULL;
2594 char *bind6_address = NULL;
2597 if (GNUNET_CONFIGURATION_have_value
2598 (plugin->env->cfg, plugin->name, "USE_IPv4"))
2601 GNUNET_CONFIGURATION_get_value_yesno (plugin->env->cfg, plugin->name,
2605 plugin->use_ipv4 = GNUNET_YES;
2606 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2607 _("IPv4 support is %s\n"),
2608 (plugin->use_ipv4 == GNUNET_YES) ? "enabled" : "disabled");
2611 if (GNUNET_CONFIGURATION_have_value
2612 (plugin->env->cfg, plugin->name, "USE_IPv6"))
2615 GNUNET_CONFIGURATION_get_value_yesno (plugin->env->cfg, plugin->name,
2619 plugin->use_ipv6 = GNUNET_YES;
2620 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2621 _("IPv6 support is %s\n"),
2622 (plugin->use_ipv6 == GNUNET_YES) ? "enabled" : "disabled");
2624 if ((plugin->use_ipv4 == GNUNET_NO) && (plugin->use_ipv6 == GNUNET_NO))
2626 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
2628 ("Neither IPv4 nor IPv6 are enabled! Fix in configuration\n"),
2630 return GNUNET_SYSERR;
2633 /* Reading port number from config file */
2635 GNUNET_CONFIGURATION_get_value_number (plugin->env->cfg, plugin->name,
2636 "PORT", &port)) || (port > 65535))
2638 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
2639 _("Port is required! Fix in configuration\n"),
2641 return GNUNET_SYSERR;
2643 plugin->port = port;
2645 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2646 _("Using port %u\n"), plugin->port);
2648 if ((plugin->use_ipv4 == GNUNET_YES) &&
2649 (GNUNET_YES == GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg,
2650 plugin->name, "BINDTO", &bind4_address)))
2652 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2653 "Binding %s plugin to specific IPv4 address: `%s'\n",
2654 plugin->protocol, bind4_address);
2655 plugin->server_addr_v4 = GNUNET_malloc (sizeof (struct sockaddr_in));
2656 if (1 != inet_pton (AF_INET, bind4_address,
2657 &plugin->server_addr_v4->sin_addr))
2659 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
2661 ("Specific IPv4 address `%s' in configuration file is invalid!\n"),
2663 GNUNET_free (bind4_address);
2664 GNUNET_free (plugin->server_addr_v4);
2665 plugin->server_addr_v4 = NULL;
2666 return GNUNET_SYSERR;
2670 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2671 _("Binding to IPv4 address %s\n"), bind4_address);
2672 plugin->server_addr_v4->sin_family = AF_INET;
2673 plugin->server_addr_v4->sin_port = htons (plugin->port);
2675 GNUNET_free (bind4_address);
2678 if ((plugin->use_ipv6 == GNUNET_YES) &&
2680 GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg, plugin->name,
2681 "BINDTO6", &bind6_address)))
2683 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2684 "Binding %s plugin to specific IPv6 address: `%s'\n",
2685 plugin->protocol, bind6_address);
2686 plugin->server_addr_v6 = GNUNET_malloc (sizeof (struct sockaddr_in6));
2688 inet_pton (AF_INET6, bind6_address, &plugin->server_addr_v6->sin6_addr))
2690 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
2692 ("Specific IPv6 address `%s' in configuration file is invalid!\n"),
2694 GNUNET_free (bind6_address);
2695 GNUNET_free (plugin->server_addr_v6);
2696 plugin->server_addr_v6 = NULL;
2697 return GNUNET_SYSERR;
2701 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2702 _("Binding to IPv6 address %s\n"), bind6_address);
2703 plugin->server_addr_v6->sin6_family = AF_INET6;
2704 plugin->server_addr_v6->sin6_port = htons (plugin->port);
2706 GNUNET_free (bind6_address);
2709 if (GNUNET_YES == GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg, plugin->name,
2710 "EXTERNAL_HOSTNAME", &plugin->external_hostname))
2713 if (NULL != strstr(plugin->external_hostname, "://"))
2715 tmp = strdup(&strstr(plugin->external_hostname, "://")[3]);
2716 GNUNET_free (plugin->external_hostname);
2717 plugin->external_hostname = tmp;
2720 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2721 _("Using external hostname `%s'\n"), plugin->external_hostname);
2722 plugin->notify_ext_task = GNUNET_SCHEDULER_add_now (&server_notify_external_hostname, plugin);
2724 /* Use only configured external hostname */
2725 if (GNUNET_CONFIGURATION_have_value
2726 (plugin->env->cfg, plugin->name, "EXTERNAL_HOSTNAME_ONLY"))
2728 plugin->external_only =
2729 GNUNET_CONFIGURATION_get_value_yesno (plugin->env->cfg, plugin->name,
2730 "EXTERNAL_HOSTNAME_ONLY");
2733 plugin->external_only = GNUNET_NO;
2735 if (GNUNET_YES == plugin->external_only)
2736 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2737 _("Notifying transport only about hostname `%s'\n"), plugin->external_hostname);
2740 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2741 "No external hostname configured\n");
2743 /* Optional parameters */
2744 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (plugin->env->cfg,
2746 "MAX_CONNECTIONS", &max_connections))
2747 max_connections = 128;
2748 plugin->max_connections = max_connections;
2750 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2751 _("Maximum number of connections is %u\n"),
2752 plugin->max_connections);
2755 plugin->peer_id_length = strlen (GNUNET_h2s_full (&plugin->env->my_identity->hashPubKey));
2762 * Session was idle, so disconnect it
2764 * @param cls the session
2765 * @param tc task context
2768 server_session_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2770 GNUNET_assert (NULL != cls);
2771 struct Session *s = cls;
2773 s->timeout_task = GNUNET_SCHEDULER_NO_TASK;
2774 GNUNET_log (TIMEOUT_LOG,
2775 "Session %p was idle for %llu ms, disconnecting\n",
2776 s, (unsigned long long) SERVER_SESSION_TIMEOUT.rel_value);
2778 /* call session destroy function */
2779 GNUNET_assert (GNUNET_OK == server_disconnect (s));
2784 * Start session timeout for session s
2786 * @param s the session
2789 server_start_session_timeout (struct Session *s)
2791 GNUNET_assert (NULL != s);
2792 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == s->timeout_task);
2793 s->timeout_task = GNUNET_SCHEDULER_add_delayed (SERVER_SESSION_TIMEOUT,
2794 &server_session_timeout,
2796 GNUNET_log (TIMEOUT_LOG,
2797 "Timeout for session %p set to %llu ms\n",
2798 s, (unsigned long long) SERVER_SESSION_TIMEOUT.rel_value);
2803 * Increment session timeout due to activity session s
2805 * @param s the session
2808 server_reschedule_session_timeout (struct Session *s)
2810 GNUNET_assert (NULL != s);
2811 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK != s->timeout_task);
2813 GNUNET_SCHEDULER_cancel (s->timeout_task);
2814 s->timeout_task = GNUNET_SCHEDULER_add_delayed (SERVER_SESSION_TIMEOUT,
2815 &server_session_timeout,
2817 GNUNET_log (TIMEOUT_LOG,
2818 "Timeout rescheduled for session %p set to %llu ms\n",
2819 s, (unsigned long long) SERVER_SESSION_TIMEOUT.rel_value);
2824 * Exit point from the plugin.
2830 LIBGNUNET_PLUGIN_TRANSPORT_DONE (void *cls)
2832 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
2833 struct HTTP_Server_Plugin *plugin = api->cls;
2834 struct Session *pos;
2835 struct Session *next;
2837 if (NULL == api->cls)
2839 /* Free for stub mode */
2843 plugin->in_shutdown = GNUNET_YES;
2844 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2845 _("Shutting down plugin `%s'\n"),
2848 if (GNUNET_SCHEDULER_NO_TASK != plugin->notify_ext_task)
2850 GNUNET_SCHEDULER_cancel (plugin->notify_ext_task);
2851 plugin->notify_ext_task = GNUNET_SCHEDULER_NO_TASK;
2854 if (NULL != plugin->ext_addr)
2856 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2857 "Notifying transport to remove address `%s'\n",
2858 http_common_plugin_address_to_string (NULL,
2860 plugin->ext_addr_len));
2862 plugin->env->notify_address (plugin->env->cls,
2865 plugin->ext_addr_len,
2868 plugin->env->notify_address (plugin->env->cls,
2871 plugin->ext_addr_len,
2877 /* Stop to report addresses to transport service */
2878 server_stop_report_addresses (plugin);
2879 server_stop (plugin);
2880 next = plugin->head;
2881 while (NULL != (pos = next))
2884 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2885 "Removing left over session %p\n", pos);
2887 if ((GNUNET_YES == pos->session_passed) && (GNUNET_NO == pos->session_ended))
2889 /* Notify transport immediately that this session is invalid */
2890 pos->session_ended = GNUNET_YES;
2891 plugin->env->session_end (plugin->env->cls, &pos->target, pos);
2894 server_delete_session (pos);
2898 GNUNET_free_non_null (plugin->external_hostname);
2899 GNUNET_free_non_null (plugin->ext_addr);
2900 GNUNET_free_non_null (plugin->server_addr_v4);
2901 GNUNET_free_non_null (plugin->server_addr_v6);
2903 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
2904 _("Shutdown for plugin `%s' complete\n"),
2907 GNUNET_free (plugin);
2914 * Entry point for the plugin.
2920 LIBGNUNET_PLUGIN_TRANSPORT_INIT (void *cls)
2922 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
2923 struct GNUNET_TRANSPORT_PluginFunctions *api;
2924 struct HTTP_Server_Plugin *plugin;
2926 plugin = GNUNET_malloc (sizeof (struct HTTP_Server_Plugin));
2930 if (NULL == env->receive)
2932 /* run in 'stub' mode (i.e. as part of gnunet-peerinfo), don't fully
2933 initialze the plugin or the API */
2934 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
2936 api->address_to_string = &http_common_plugin_address_to_string;
2937 api->string_to_address = &http_common_plugin_string_to_address;
2938 api->address_pretty_printer = &http_common_plugin_address_pretty_printer;
2942 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
2944 api->send = &http_server_plugin_send;
2945 api->disconnect = &http_server_plugin_disconnect;
2946 api->check_address = &http_server_plugin_address_suggested;
2947 api->get_session = &http_server_plugin_get_session;
2949 api->address_to_string = &http_common_plugin_address_to_string;
2950 api->string_to_address = &http_common_plugin_string_to_address;
2951 api->address_pretty_printer = &http_common_plugin_address_pretty_printer;
2954 plugin->name = "transport-https_server";
2955 plugin->protocol = "https";
2957 plugin->name = "transport-http_server";
2958 plugin->protocol = "http";
2961 /* Configure plugin */
2962 if (GNUNET_SYSERR == server_configure_plugin (plugin))
2964 LIBGNUNET_PLUGIN_TRANSPORT_DONE (api);
2968 /* Check IPv6 support */
2969 if (GNUNET_YES == plugin->use_ipv6)
2970 plugin->use_ipv6 = server_check_ipv6_support (plugin);
2972 /* Report addresses to transport service */
2973 if (GNUNET_NO == plugin->external_only)
2974 server_start_report_addresses (plugin);
2976 if (GNUNET_SYSERR == server_start (plugin))
2978 LIBGNUNET_PLUGIN_TRANSPORT_DONE (api);
2985 /* end of plugin_transport_http_server.c */