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.c
23 * @brief http transport service plugin
24 * @author Matthias Wachs
28 #include "gnunet_common.h"
29 #include "gnunet_constants.h"
30 #include "gnunet_protocols.h"
31 #include "gnunet_connection_lib.h"
32 #include "gnunet_service_lib.h"
33 #include "gnunet_statistics_service.h"
34 #include "gnunet_transport_service.h"
35 #include "gnunet_resolver_service.h"
36 #include "gnunet_server_lib.h"
37 #include "gnunet_container_lib.h"
38 #include "gnunet_transport_plugin.h"
39 #include "gnunet_os_lib.h"
40 #include "microhttpd.h"
41 #include <curl/curl.h>
44 #define LIBGNUNET_PLUGIN_TRANSPORT_INIT libgnunet_plugin_transport_https_init
45 #define LIBGNUNET_PLUGIN_TRANSPORT_DONE libgnunet_plugin_transport_https_done
46 #define LIBGNUNET_PLUGIN_TRANSPORT_COMPONENT transport_https
47 #define PROTOCOL_PREFIX "https"
49 #define LIBGNUNET_PLUGIN_TRANSPORT_INIT libgnunet_plugin_transport_http_init
50 #define LIBGNUNET_PLUGIN_TRANSPORT_DONE libgnunet_plugin_transport_http_done
51 #define LIBGNUNET_PLUGIN_TRANSPORT_COMPONENT transport_http
52 #define PROTOCOL_PREFIX "http"
55 #define DEBUG_HTTP GNUNET_NO
56 #define DEBUG_CURL GNUNET_NO
57 #define DEBUG_MHD GNUNET_NO
58 #define DEBUG_CONNECTIONS GNUNET_NO
59 #define DEBUG_SESSION_SELECTION GNUNET_NO
60 #define DEBUG_SCHEDULING GNUNET_NO
61 #define CURL_TCP_NODELAY GNUNET_YES
63 #define INBOUND GNUNET_NO
64 #define OUTBOUND GNUNET_YES
69 * Text of the response sent back after the last bytes of a PUT
70 * request have been received (just to formally obey the HTTP
73 #define HTTP_PUT_RESPONSE "Thank you!"
76 * After how long do we expire an address that we
77 * learned from another peer if it is not reconfirmed
80 #define LEARNED_ADDRESS_EXPIRATION GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_HOURS, 6)
83 * Page returned if request invalid
85 #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>"
88 * Timeout for a http connect
90 #define HTTP_CONNECT_TIMEOUT 30
94 * Network format for IPv4 addresses.
96 struct IPv4HttpAddress
101 struct IPv4HttpAddress * next;
104 * Linked list previous
106 struct IPv4HttpAddress * prev;
109 * IPv4 address, in network byte order.
111 uint32_t ipv4_addr GNUNET_PACKED;
114 * Port number, in network byte order.
116 uint16_t u_port GNUNET_PACKED;
122 * Network format for IPv6 addresses.
124 struct IPv6HttpAddress
129 struct IPv6HttpAddress * next;
132 * Linked list previous
134 struct IPv6HttpAddress * prev;
139 struct in6_addr ipv6_addr GNUNET_PACKED;
142 * Port number, in network byte order.
144 uint16_t u6_port GNUNET_PACKED;
150 * Message to send using http
155 * next pointer for double linked list
157 struct HTTP_Message * next;
160 * previous pointer for double linked list
162 struct HTTP_Message * prev;
165 * buffer containing data to send
170 * amount of data already sent
180 * Continuation function to call once the transmission buffer
181 * has again space available. NULL if there is no
182 * continuation to call.
184 GNUNET_TRANSPORT_TransmitContinuation transmit_cont;
187 * Closure for transmit_cont.
189 void *transmit_cont_cls;
193 struct HTTP_PeerContext
198 struct GNUNET_PeerIdentity identity;
201 * Pointer to the global plugin struct.
203 struct Plugin *plugin;
206 * Linked list of connections with this peer
209 struct Session * head;
212 * Linked list of connections with this peer
215 struct Session * tail;
218 * id for next session
220 size_t session_id_counter;
223 * Last session used to send data
225 struct Session * last_session;
228 * The task resetting inbound quota delay
230 GNUNET_SCHEDULER_TaskIdentifier reset_task;
233 * Delay from transport service inbound quota tracker when to receive data again
235 struct GNUNET_TIME_Relative delay;
244 struct SessionHeader header;
247 * next session in linked list
249 struct Session * next;
252 * previous session in linked list
254 struct Session * prev;
257 * address of this session
272 * Message queue for outbound messages
275 struct HTTP_Message * pending_msgs_head;
278 * Message queue for outbound messages
281 struct HTTP_Message * pending_msgs_tail;
284 * partner peer this connection belongs to
286 struct HTTP_PeerContext * peercontext;
289 * message stream tokenizer for incoming data
291 struct GNUNET_SERVER_MessageStreamTokenizer *msgtok;
295 * outbound: OUTBOUND (GNUNET_YES)
296 * inbound : INBOUND (GNUNET_NO)
298 unsigned int direction;
301 * is session connected to send data?
303 unsigned int send_connected;
306 * is send connection active?
308 unsigned int send_active;
311 * connection disconnect forced (e.g. from transport)
313 unsigned int send_force_disconnect;
316 * is session connected to receive data?
318 unsigned int recv_connected;
321 * is receive connection active?
323 unsigned int recv_active;
326 * connection disconnect forced (e.g. from transport)
328 unsigned int recv_force_disconnect;
332 * id for next session
333 * NOTE: 0 is not an ID, zero is not defined. A correct ID is always > 0
338 * entity managing sending data
339 * outbound session: CURL *
340 * inbound session: mhd_connection *
342 void * send_endpoint;
345 * entity managing recieving data
346 * outbound session: CURL *
347 * inbound session: mhd_connection *
349 void * recv_endpoint;
354 size_t queue_length_cur;
359 size_t queue_length_max;
364 * Encapsulation of all of the state of the plugin.
371 struct GNUNET_TRANSPORT_PluginEnvironment *env;
374 * Handle for reporting statistics.
376 struct GNUNET_STATISTICS_Handle *stats;
381 uint16_t port_inbound;
383 struct GNUNET_CONTAINER_MultiHashMap *peers;
386 * Daemon for listening for new IPv4 connections.
388 struct MHD_Daemon *http_server_daemon_v4;
391 * Daemon for listening for new IPv6connections.
393 struct MHD_Daemon *http_server_daemon_v6;
396 * Our primary task for http daemon handling IPv4 connections
398 GNUNET_SCHEDULER_TaskIdentifier http_server_task_v4;
401 * Our primary task for http daemon handling IPv6 connections
403 GNUNET_SCHEDULER_TaskIdentifier http_server_task_v6;
406 * The task sending data
408 GNUNET_SCHEDULER_TaskIdentifier http_curl_task;
413 CURLM * multi_handle;
418 struct IPv4HttpAddress * ipv4_addr_head;
423 struct IPv4HttpAddress * ipv4_addr_tail;
428 struct IPv6HttpAddress * ipv6_addr_head;
433 struct IPv6HttpAddress * ipv6_addr_tail;
436 * Our ASCII encoded, hashed peer identity
437 * This string is used to distinguish between connections and is added to the urls
439 struct GNUNET_CRYPTO_HashAsciiEncoded my_ascii_hash_ident;
442 * IPv4 Address the plugin binds to
444 struct sockaddr_in * bind4_address;
447 * IPv6 Address the plugins binds to
449 struct sockaddr_in6 * bind6_address;
452 * Hostname to bind to
454 char * bind_hostname;
467 * use local addresses?
469 int use_localaddresses;
472 * maximum number of connections
474 int max_connect_per_transport;
477 * Current number of connections;
479 int current_connections;
482 * Closure passed by MHD to the mhd_logger function
486 /* only needed for HTTPS plugin */
488 /* The certificate MHD uses as an \0 terminated string */
491 /* The private key MHD uses as an \0 terminated string */
494 /* crypto init string */
501 * Function called for a quick conversion of the binary address to
502 * a numeric address. Note that the caller must not free the
503 * address and that the next call to this function is allowed
504 * to override the address again.
507 * @param addr binary address
508 * @param addrlen length of the address
509 * @return string representing the same address
512 http_plugin_address_to_string (void *cls,
518 * Call MHD to process pending ipv4 requests and then go back
519 * and schedule the next run.
521 static void http_server_daemon_v4_run (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
523 * Call MHD to process pending ipv6 requests and then go back
524 * and schedule the next run.
526 static void http_server_daemon_v6_run (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
529 * Function setting up curl handle and selecting message to send
531 * @param plugin plugin
533 * @return GNUNET_SYSERR on failure, GNUNET_NO if connecting, GNUNET_YES if ok
535 static int send_check_connections (struct Plugin *plugin, struct Session *ps);
538 * Function setting up file descriptors and scheduling task to run
540 * @param plugin plugin as closure
541 * @return GNUNET_SYSERR for hard failure, GNUNET_OK for ok
543 static int curl_schedule (struct Plugin *plugin);
546 * Task scheduled to reset the inbound quota delay for a specific peer
547 * @param cls plugin as closure
548 * @param tc task context
550 static void reset_inbound_quota_delay (void *cls,
551 const struct GNUNET_SCHEDULER_TaskContext *tc)
553 struct HTTP_PeerContext * pc = cls;
555 GNUNET_assert(cls != NULL);
556 pc->reset_task = GNUNET_SCHEDULER_NO_TASK;
557 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
559 pc->delay = GNUNET_TIME_relative_get_zero ();
564 * Creates a valid url from passed address and id
565 * @param plugin plugin
566 * @param addr address to create url from
567 * @param addrlen address lenth
568 * @param id session id
569 * @return the created url
572 create_url(struct Plugin *plugin,
573 const void * addr, size_t addrlen,
577 char *addr_str = (char *) http_plugin_address_to_string(NULL, addr, addrlen);
579 GNUNET_assert ((addr!=NULL) && (addrlen != 0));
580 GNUNET_asprintf(&url,
581 "%s://%s/%s;%u", PROTOCOL_PREFIX, addr_str,
582 (char *) (&plugin->my_ascii_hash_ident),id);
588 * Removes a message from the linked list of messages
591 * @return GNUNET_SYSERR if msg not found, GNUNET_OK on success
594 remove_http_message (struct Session * ps,
595 struct HTTP_Message * msg)
597 GNUNET_CONTAINER_DLL_remove(ps->pending_msgs_head,
598 ps->pending_msgs_tail,
605 * Iterator to remove peer context
606 * @param cls the plugin
607 * @param key the peers public key hashcode
608 * @param value the peer context
609 * @return GNUNET_YES on success
612 remove_peer_context_Iterator (void *cls,
613 const GNUNET_HashCode *key,
616 struct Plugin *plugin = cls;
617 struct HTTP_PeerContext * pc = value;
618 struct Session * ps = pc->head;
619 struct Session * tmp = NULL;
620 struct HTTP_Message * msg = NULL;
621 struct HTTP_Message * msg_tmp = NULL;
624 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
625 "Freeing context for peer `%s'\n",
626 GNUNET_i2s(&pc->identity));
628 GNUNET_CONTAINER_multihashmap_remove (plugin->peers, &pc->identity.hashPubKey, pc);
631 plugin->env->session_end(plugin, &pc->identity, ps);
634 GNUNET_free_non_null (ps->addr);
635 GNUNET_free(ps->url);
636 if (ps->msgtok != NULL)
637 GNUNET_SERVER_mst_destroy (ps->msgtok);
639 msg = ps->pending_msgs_head;
646 if (ps->direction==OUTBOUND)
648 if (ps->send_endpoint!=NULL)
649 curl_easy_cleanup(ps->send_endpoint);
650 if (ps->recv_endpoint!=NULL)
651 curl_easy_cleanup(ps->recv_endpoint);
657 GNUNET_STATISTICS_update (plugin->env->stats,
658 gettext_noop ("# HTTP peers active"),
666 * Removes a session from the linked list of sessions
667 * @param pc peer context
669 * @param call_msg_cont GNUNET_YES to call pending message continuations, otherwise no
670 * @param call_msg_cont_result result to call message continuations with
671 * @return GNUNET_SYSERR if msg not found, GNUNET_OK on success
674 remove_session (struct HTTP_PeerContext * pc,
677 int call_msg_cont_result)
679 struct HTTP_Message * msg;
680 struct Plugin * plugin = ps->peercontext->plugin;
682 #if DEBUG_CONNECTIONS
683 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
684 "Connection %X: removing %s session %X with id %u\n",
686 (ps->direction == INBOUND)
691 plugin->env->session_end(plugin, &pc->identity, ps);
692 GNUNET_free_non_null (ps->addr);
693 GNUNET_SERVER_mst_destroy (ps->msgtok);
694 GNUNET_free(ps->url);
695 if (ps->direction==INBOUND)
697 if (ps->recv_endpoint != NULL)
699 curl_easy_cleanup(ps->recv_endpoint);
700 ps->recv_endpoint = NULL;
702 if (ps->send_endpoint != NULL)
704 curl_easy_cleanup(ps->send_endpoint);
705 ps->send_endpoint = NULL;
709 msg = ps->pending_msgs_head;
712 if ((call_msg_cont == GNUNET_YES) && (msg->transmit_cont!=NULL))
714 msg->transmit_cont (msg->transmit_cont_cls,
716 call_msg_cont_result);
718 GNUNET_CONTAINER_DLL_remove(ps->pending_msgs_head,
719 ps->pending_msgs_head,
722 msg = ps->pending_msgs_head;
725 GNUNET_CONTAINER_DLL_remove(pc->head,pc->tail,ps);
729 /* no sessions left remove peer */
733 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
734 "No sessions left for peer `%s', removing context\n",
735 GNUNET_i2s(&pc->identity));
737 remove_peer_context_Iterator(plugin, &pc->identity.hashPubKey, pc);
743 static int check_localaddress (const struct sockaddr *addr, socklen_t addrlen)
746 int local = GNUNET_NO;
747 int af = addr->sa_family;
752 uint32_t netmask = 0x7F000000;
753 uint32_t address = ntohl (((struct sockaddr_in *) addr)->sin_addr.s_addr);
754 res = (address >> 24) ^ (netmask >> 24);
760 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
761 "Checking IPv4 address `%s': %s\n", GNUNET_a2s (addr, addrlen), (local==GNUNET_YES) ? "local" : "global");
767 if (IN6_IS_ADDR_LOOPBACK (&((struct sockaddr_in6 *) addr)->sin6_addr) ||
768 IN6_IS_ADDR_LINKLOCAL (&((struct sockaddr_in6 *) addr)->sin6_addr))
773 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
774 "Checking IPv6 address `%s' : %s\n", GNUNET_a2s (addr, addrlen), (local==GNUNET_YES) ? "local" : "global");
783 * Add the IP of our network interface to the list of
784 * our external IP addresses.
786 * @param cls the 'struct Plugin*'
787 * @param name name of the interface
788 * @param isDefault do we think this may be our default interface
789 * @param addr address of the interface
790 * @param addrlen number of bytes in addr
791 * @return GNUNET_OK to continue iterating
794 process_interfaces (void *cls,
797 const struct sockaddr *addr, socklen_t addrlen)
799 struct Plugin *plugin = cls;
800 struct IPv4HttpAddress * t4;
801 struct IPv6HttpAddress * t6;
804 if (plugin->use_localaddresses == GNUNET_NO)
806 if (GNUNET_YES == check_localaddress (addr, addrlen))
809 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
811 "Not notifying transport of address `%s' (local address)\n",
812 GNUNET_a2s (addr, addrlen));
819 GNUNET_assert(cls !=NULL);
820 af = addr->sa_family;
821 if ((af == AF_INET) &&
822 (plugin->use_ipv4 == GNUNET_YES) &&
823 (plugin->bind6_address == NULL) ) {
825 struct in_addr bnd_cmp = ((struct sockaddr_in *) addr)->sin_addr;
826 t4 = GNUNET_malloc(sizeof(struct IPv4HttpAddress));
827 // Not skipping loopback addresses
830 t4->ipv4_addr = ((struct sockaddr_in *) addr)->sin_addr.s_addr;
831 t4->u_port = htons (plugin->port_inbound);
832 if (plugin->bind4_address != NULL) {
833 if (0 == memcmp(&plugin->bind4_address->sin_addr, &bnd_cmp, sizeof (struct in_addr)))
835 GNUNET_CONTAINER_DLL_insert(plugin->ipv4_addr_head,
836 plugin->ipv4_addr_tail,t4);
837 plugin->env->notify_address(plugin->env->cls,
839 t4, sizeof (struct IPv4HttpAddress));
847 GNUNET_CONTAINER_DLL_insert (plugin->ipv4_addr_head,
848 plugin->ipv4_addr_tail,
850 plugin->env->notify_address(plugin->env->cls,
852 t4, sizeof (struct IPv4HttpAddress));
856 if ((af == AF_INET6) &&
857 (plugin->use_ipv6 == GNUNET_YES) &&
858 (plugin->bind4_address == NULL) ) {
860 struct in6_addr bnd_cmp6 = ((struct sockaddr_in6 *) addr)->sin6_addr;
862 t6 = GNUNET_malloc(sizeof(struct IPv6HttpAddress));
863 GNUNET_assert(t6 != NULL);
865 if (plugin->bind6_address != NULL) {
866 if (0 == memcmp(&plugin->bind6_address->sin6_addr,
868 sizeof (struct in6_addr))) {
869 memcpy (&t6->ipv6_addr,
870 &((struct sockaddr_in6 *) addr)->sin6_addr,
871 sizeof (struct in6_addr));
872 t6->u6_port = htons (plugin->port_inbound);
873 plugin->env->notify_address(plugin->env->cls,
875 t6, sizeof (struct IPv6HttpAddress));
876 GNUNET_CONTAINER_DLL_insert(plugin->ipv6_addr_head,
877 plugin->ipv6_addr_tail,
884 memcpy (&t6->ipv6_addr,
885 &((struct sockaddr_in6 *) addr)->sin6_addr,
886 sizeof (struct in6_addr));
887 t6->u6_port = htons (plugin->port_inbound);
888 GNUNET_CONTAINER_DLL_insert(plugin->ipv6_addr_head,plugin->ipv6_addr_tail,t6);
890 plugin->env->notify_address(plugin->env->cls,
892 t6, sizeof (struct IPv6HttpAddress));
899 * External logging function for MHD
900 * @param arg arguments
901 * @param fmt format string
902 * @param ap list of arguments
905 mhd_logger (void * arg,
911 vsnprintf(text, sizeof(text), fmt, ap);
913 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
920 mhd_termination_cb (void *cls,
921 struct MHD_Connection * connection,
922 void **httpSessionCache)
924 struct Session * ps = *httpSessionCache;
927 struct HTTP_PeerContext * pc = ps->peercontext;
928 struct Plugin *plugin = cls;
930 GNUNET_assert (cls != NULL);
931 plugin->current_connections--;
933 if (connection==ps->recv_endpoint)
935 #if DEBUG_CONNECTIONS
936 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
937 "Connection %X: inbound connection from peer `%s' was terminated\n",
939 GNUNET_i2s(&pc->identity));
941 ps->recv_active = GNUNET_NO;
942 ps->recv_connected = GNUNET_NO;
943 ps->recv_endpoint = NULL;
945 if (connection==ps->send_endpoint)
947 ps->send_active = GNUNET_NO;
948 ps->send_connected = GNUNET_NO;
949 ps->send_endpoint = NULL;
950 #if DEBUG_CONNECTIONS
951 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
952 "Connection %X: outbound connection from peer `%s' was terminated\n",
954 GNUNET_i2s(&pc->identity));
958 /* if both connections disconnected, remove session */
959 if ( (ps->send_connected == GNUNET_NO) &&
960 (ps->recv_connected == GNUNET_NO) )
962 GNUNET_STATISTICS_update (pc->plugin->env->stats,
963 gettext_noop ("# HTTP inbound sessions for peers active"),
966 remove_session(pc,ps,GNUNET_YES,GNUNET_SYSERR);
972 * Callback called by MessageStreamTokenizer when a message has arrived
973 * @param cls current session as closure
974 * @param client clien
975 * @param message the message to be forwarded to transport service
978 mhd_write_mst_cb (void *cls,
980 const struct GNUNET_MessageHeader *message)
982 struct Session *ps = cls;
983 struct HTTP_PeerContext *pc = ps->peercontext;
984 struct GNUNET_TIME_Relative delay;
986 GNUNET_assert(ps != NULL);
987 GNUNET_assert(pc != NULL);
989 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
990 "Connection %X: Forwarding message to transport service, type %u and size %u from `%s' (`%s')\n",
992 ntohs(message->type),
993 ntohs(message->size),
994 GNUNET_i2s(&(ps->peercontext)->identity),
995 http_plugin_address_to_string(NULL,ps->addr,ps->addrlen));
997 struct GNUNET_TRANSPORT_ATS_Information distance[2];
998 distance[0].type = htonl (GNUNET_TRANSPORT_ATS_QUALITY_NET_DISTANCE);
999 distance[0].value = htonl (1);
1000 distance[1].type = htonl (GNUNET_TRANSPORT_ATS_ARRAY_TERMINATOR);
1001 distance[1].value = htonl (0);
1002 delay = pc->plugin->env->receive (ps->peercontext->plugin->env->cls,
1005 (const struct GNUNET_TRANSPORT_ATS_Information *) &distance,
1011 if (pc->reset_task != GNUNET_SCHEDULER_NO_TASK)
1012 GNUNET_SCHEDULER_cancel (pc->reset_task);
1014 if (delay.rel_value > 0)
1017 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1018 "Connection %X: Inbound quota management: delay next read for %llu ms \n",
1022 pc->reset_task = GNUNET_SCHEDULER_add_delayed (delay, &reset_inbound_quota_delay, pc);
1028 * Check if incoming connection is accepted.
1029 * NOTE: Here every connection is accepted
1030 * @param cls plugin as closure
1031 * @param addr address of incoming connection
1032 * @param addr_len address length of incoming connection
1033 * @return MHD_YES if connection is accepted, MHD_NO if connection is rejected
1037 mhd_accept_cb (void *cls,
1038 const struct sockaddr *addr,
1041 struct Plugin *plugin = cls;
1042 GNUNET_assert (cls != NULL);
1044 if (plugin->max_connect_per_transport > plugin->current_connections)
1046 plugin->current_connections ++;
1054 * Callback called by MHD when it needs data to send
1055 * @param cls current session
1056 * @param pos position in buffer
1057 * @param buf the buffer to write data to
1058 * @param max max number of bytes available in buffer
1059 * @return bytes written to buffer
1062 mhd_send_callback (void *cls, uint64_t pos, char *buf, size_t max)
1064 struct Session * ps = cls;
1065 struct HTTP_PeerContext * pc;
1066 struct HTTP_Message * msg;
1069 GNUNET_assert (ps!=NULL);
1071 pc = ps->peercontext;
1072 msg = ps->pending_msgs_tail;
1073 if (ps->send_force_disconnect==GNUNET_YES)
1075 #if DEBUG_CONNECTIONS
1076 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1077 "Connection %X: outbound forced to disconnect\n",
1086 if ((msg->size-msg->pos) <= max)
1088 memcpy(buf,&msg->buf[msg->pos],(msg->size-msg->pos));
1089 bytes_read = msg->size-msg->pos;
1090 msg->pos+=(msg->size-msg->pos);
1094 memcpy(buf,&msg->buf[msg->pos],max);
1099 /* removing message */
1100 if (msg->pos==msg->size)
1102 if (NULL!=msg->transmit_cont)
1103 msg->transmit_cont (msg->transmit_cont_cls,&pc->identity,GNUNET_OK);
1104 ps->queue_length_cur -= msg->size;
1105 remove_http_message(ps,msg);
1108 #if DEBUG_CONNECTIONS
1109 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1110 "Connection %X: MHD has sent %u bytes\n",
1119 * Process GET or PUT request received via MHD. For
1120 * GET, queue response that will send back our pending
1121 * messages. For PUT, process incoming data and send
1122 * to GNUnet core. In either case, check if a session
1123 * already exists and create a new one if not.
1126 mhd_access_cb (void *cls,
1127 struct MHD_Connection *mhd_connection,
1130 const char *version,
1131 const char *upload_data,
1132 size_t * upload_data_size,
1133 void **httpSessionCache)
1135 struct Plugin *plugin = cls;
1136 struct MHD_Response *response;
1137 const union MHD_ConnectionInfo * conn_info;
1138 const struct sockaddr *client_addr;
1139 const struct sockaddr_in *addrin;
1140 const struct sockaddr_in6 *addrin6;
1141 char address[INET6_ADDRSTRLEN+14];
1142 struct GNUNET_PeerIdentity pi_in;
1144 struct IPv4HttpAddress ipv4addr;
1145 struct IPv6HttpAddress ipv6addr;
1146 struct HTTP_PeerContext *pc = NULL;
1147 struct Session *ps = NULL;
1148 struct Session *ps_tmp = NULL;
1149 int res = GNUNET_NO;
1151 size_t addr_len = 0 ;
1153 GNUNET_assert(cls !=NULL);
1155 if (NULL == *httpSessionCache)
1157 /* check url for peer identity , if invalid send HTTP 404*/
1158 size_t len = strlen(&url[1]);
1159 char * peer = GNUNET_malloc(104+1);
1161 if ( (len>104) && (url[104]==';'))
1163 char * id = GNUNET_malloc((len-104)+1);
1164 strcpy(id,&url[105]);
1165 memcpy(peer,&url[1],103);
1167 id_num = strtoul ( id, NULL , 10);
1170 res = GNUNET_CRYPTO_hash_from_string (peer, &(pi_in.hashPubKey));
1172 if ( GNUNET_SYSERR == res )
1174 response = MHD_create_response_from_data (strlen (HTTP_ERROR_RESPONSE),
1175 HTTP_ERROR_RESPONSE,
1177 res = MHD_queue_response (mhd_connection, MHD_HTTP_NOT_FOUND, response);
1178 MHD_destroy_response (response);
1179 #if DEBUG_CONNECTIONS
1181 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1182 "Peer has no valid ident, sent HTTP 1.1/404\n");
1184 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1185 "Peer has no valid ident, could not send error\n");
1192 ps = *httpSessionCache;
1193 pc = ps->peercontext;
1196 if (NULL == *httpSessionCache)
1198 /* get peer context */
1199 pc = GNUNET_CONTAINER_multihashmap_get (plugin->peers, &pi_in.hashPubKey);
1203 pc = GNUNET_malloc(sizeof (struct HTTP_PeerContext));
1204 pc->plugin = plugin;
1205 pc->session_id_counter=1;
1206 pc->last_session = NULL;
1207 memcpy(&pc->identity, &pi_in, sizeof(struct GNUNET_PeerIdentity));
1208 GNUNET_CONTAINER_multihashmap_put(plugin->peers,
1209 &pc->identity.hashPubKey,
1211 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
1212 GNUNET_STATISTICS_update (plugin->env->stats,
1213 gettext_noop ("# HTTP peers active"),
1218 conn_info = MHD_get_connection_info(mhd_connection, MHD_CONNECTION_INFO_CLIENT_ADDRESS );
1219 /* Incoming IPv4 connection */
1220 /* cast required for legacy MHD API < 0.9.6 */
1221 client_addr = (const struct sockaddr *) conn_info->client_addr;
1222 if ( AF_INET == client_addr->sa_family)
1224 addrin = (const struct sockaddr_in*) client_addr;
1225 inet_ntop(addrin->sin_family, &(addrin->sin_addr),address,INET_ADDRSTRLEN);
1226 memcpy(&ipv4addr.ipv4_addr,&(addrin->sin_addr),sizeof(struct in_addr));
1227 ipv4addr.u_port = addrin->sin_port;
1229 addr_len = sizeof(struct IPv4HttpAddress);
1231 /* Incoming IPv6 connection */
1232 if ( AF_INET6 == client_addr->sa_family)
1234 addrin6 = (const struct sockaddr_in6 *) client_addr;
1235 inet_ntop(addrin6->sin6_family, &(addrin6->sin6_addr),address,INET6_ADDRSTRLEN);
1236 memcpy(&ipv6addr.ipv6_addr,&(addrin6->sin6_addr),sizeof(struct in6_addr));
1237 ipv6addr.u6_port = addrin6->sin6_port;
1239 addr_len = sizeof(struct IPv6HttpAddress);
1242 GNUNET_assert (addr != NULL);
1243 GNUNET_assert (addr_len != 0);
1246 /* only inbound sessions here */
1249 while (ps_tmp!=NULL)
1251 if ((ps_tmp->direction==INBOUND) && (ps_tmp->session_id == id_num) && (id_num!=0))
1253 if ((ps_tmp->recv_force_disconnect!=GNUNET_YES) && (ps_tmp->send_force_disconnect!=GNUNET_YES))
1257 ps_tmp=ps_tmp->next;
1262 ps = GNUNET_malloc(sizeof (struct Session));
1263 ps->addr = GNUNET_malloc(addr_len);
1264 memcpy(ps->addr,addr,addr_len);
1265 ps->addrlen = addr_len;
1266 ps->direction=INBOUND;
1267 ps->pending_msgs_head = NULL;
1268 ps->pending_msgs_tail = NULL;
1269 ps->send_connected=GNUNET_NO;
1270 ps->send_active=GNUNET_NO;
1271 ps->recv_connected=GNUNET_NO;
1272 ps->recv_active=GNUNET_NO;
1274 ps->session_id =id_num;
1275 ps->queue_length_cur = 0;
1276 ps->queue_length_max = GNUNET_SERVER_MAX_MESSAGE_SIZE;
1277 ps->url = create_url (plugin, ps->addr, ps->addrlen, ps->session_id);
1278 GNUNET_CONTAINER_DLL_insert(pc->head,pc->tail,ps);
1279 GNUNET_STATISTICS_update (plugin->env->stats,
1280 gettext_noop ("# HTTP inbound sessions for peers active"),
1285 *httpSessionCache = ps;
1286 if (ps->msgtok==NULL)
1287 ps->msgtok = GNUNET_SERVER_mst_create (&mhd_write_mst_cb, ps);
1289 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1290 "Connection %X: HTTP Daemon has new an incoming `%s' request from peer `%s' (`%s')\n",
1293 GNUNET_i2s(&pc->identity),
1294 http_plugin_address_to_string(NULL, ps->addr, ps->addrlen));
1298 /* Is it a PUT or a GET request */
1299 if (0 == strcmp (MHD_HTTP_METHOD_PUT, method))
1301 if (ps->recv_force_disconnect == GNUNET_YES)
1303 #if DEBUG_CONNECTIONS
1304 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1305 "Connection %X: inbound connection was forced to disconnect\n",ps);
1307 ps->recv_active = GNUNET_NO;
1310 if ((*upload_data_size == 0) && (ps->recv_active==GNUNET_NO))
1312 ps->recv_endpoint = mhd_connection;
1313 ps->recv_connected = GNUNET_YES;
1314 ps->recv_active = GNUNET_YES;
1315 ps->recv_force_disconnect = GNUNET_NO;
1316 #if DEBUG_CONNECTIONS
1317 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1318 "Connection %X: inbound PUT connection connected\n",ps);
1323 /* Transmission of all data complete */
1324 if ((*upload_data_size == 0) && (ps->recv_active == GNUNET_YES))
1326 response = MHD_create_response_from_data (strlen (HTTP_PUT_RESPONSE),
1329 res = MHD_queue_response (mhd_connection, MHD_HTTP_OK, response);
1330 #if DEBUG_CONNECTIONS
1331 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1332 "Connection %X: Sent HTTP/1.1: 200 OK as PUT Response\n",
1335 MHD_destroy_response (response);
1336 ps->recv_active=GNUNET_NO;
1340 /* Recieving data */
1341 if ((*upload_data_size > 0) && (ps->recv_active == GNUNET_YES))
1343 if (pc->delay.rel_value == 0)
1346 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1347 "Connection %X: PUT with %u bytes forwarded to MST\n",
1348 ps, *upload_data_size);
1350 res = GNUNET_SERVER_mst_receive(ps->msgtok, ps,
1351 upload_data, *upload_data_size,
1352 GNUNET_NO, GNUNET_NO);
1353 (*upload_data_size) = 0;
1358 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1359 "Connection %X: no inbound bandwidth available! Next read was delayed for %llu ms\n",
1361 ps->peercontext->delay.rel_value);
1369 if ( 0 == strcmp (MHD_HTTP_METHOD_GET, method) )
1371 if (ps->send_force_disconnect == GNUNET_YES)
1373 #if DEBUG_CONNECTIONS
1374 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1375 "Connection %X: outbound connection was forced to disconnect\n",
1378 ps->send_active = GNUNET_NO;
1381 ps->send_connected = GNUNET_YES;
1382 ps->send_active = GNUNET_YES;
1383 ps->send_endpoint = mhd_connection;
1384 ps->send_force_disconnect = GNUNET_NO;
1385 #if DEBUG_CONNECTIONS
1386 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1387 "Connection %X: inbound GET connection connected\n",
1390 response = MHD_create_response_from_callback(-1,32 * 1024, &mhd_send_callback, ps, NULL);
1391 res = MHD_queue_response (mhd_connection, MHD_HTTP_OK, response);
1392 MHD_destroy_response (response);
1400 * Function that queries MHD's select sets and
1401 * starts the task waiting for them.
1402 * @param plugin plugin
1403 * @param daemon_handle the MHD daemon handle
1404 * @return gnunet task identifier
1406 static GNUNET_SCHEDULER_TaskIdentifier
1407 http_server_daemon_prepare (struct Plugin *plugin,
1408 struct MHD_Daemon *daemon_handle)
1410 GNUNET_SCHEDULER_TaskIdentifier ret;
1414 struct GNUNET_NETWORK_FDSet *wrs;
1415 struct GNUNET_NETWORK_FDSet *wws;
1416 struct GNUNET_NETWORK_FDSet *wes;
1418 unsigned long long timeout;
1420 struct GNUNET_TIME_Relative tv;
1422 ret = GNUNET_SCHEDULER_NO_TASK;
1426 wrs = GNUNET_NETWORK_fdset_create ();
1427 wes = GNUNET_NETWORK_fdset_create ();
1428 wws = GNUNET_NETWORK_fdset_create ();
1430 GNUNET_assert (MHD_YES ==
1431 MHD_get_fdset (daemon_handle,
1436 haveto = MHD_get_timeout (daemon_handle, &timeout);
1437 if (haveto == MHD_YES)
1438 tv.rel_value = (uint64_t) timeout;
1440 tv = GNUNET_TIME_UNIT_SECONDS;
1441 GNUNET_NETWORK_fdset_copy_native (wrs, &rs, max + 1);
1442 GNUNET_NETWORK_fdset_copy_native (wws, &ws, max + 1);
1443 GNUNET_NETWORK_fdset_copy_native (wes, &es, max + 1);
1444 if (daemon_handle == plugin->http_server_daemon_v4)
1446 if (plugin->http_server_task_v4 != GNUNET_SCHEDULER_NO_TASK)
1448 GNUNET_SCHEDULER_cancel(plugin->http_server_task_v4);
1449 plugin->http_server_daemon_v4 = GNUNET_SCHEDULER_NO_TASK;
1452 ret = GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1453 GNUNET_SCHEDULER_NO_TASK,
1457 &http_server_daemon_v4_run,
1460 if (daemon_handle == plugin->http_server_daemon_v6)
1462 if (plugin->http_server_task_v6 != GNUNET_SCHEDULER_NO_TASK)
1464 GNUNET_SCHEDULER_cancel(plugin->http_server_task_v6);
1465 plugin->http_server_task_v6 = GNUNET_SCHEDULER_NO_TASK;
1468 ret = GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1469 GNUNET_SCHEDULER_NO_TASK,
1473 &http_server_daemon_v6_run,
1476 GNUNET_NETWORK_fdset_destroy (wrs);
1477 GNUNET_NETWORK_fdset_destroy (wws);
1478 GNUNET_NETWORK_fdset_destroy (wes);
1484 * Call MHD IPv4 to process pending requests and then go back
1485 * and schedule the next run.
1486 * @param cls plugin as closure
1487 * @param tc task context
1490 http_server_daemon_v4_run (void *cls,
1491 const struct GNUNET_SCHEDULER_TaskContext *tc)
1493 struct Plugin *plugin = cls;
1495 #if DEBUG_SCHEDULING
1496 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_READ_READY))
1497 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1498 "http_server_daemon_v4_run: GNUNET_SCHEDULER_REASON_READ_READY\n");
1499 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_WRITE_READY))
1500 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1501 "http_server_daemon_v4_run: GNUNET_SCHEDULER_REASON_WRITE_READY\n");
1502 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_TIMEOUT))
1503 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1504 "http_server_daemon_v4_run: GNUNET_SCHEDULER_REASON_TIMEOUT\n");
1505 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_STARTUP))
1506 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1507 "http_server_daemon_v4_run: GGNUNET_SCHEDULER_REASON_STARTUP\n");
1508 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1509 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1510 "http_server_daemon_v4_run: GGNUNET_SCHEDULER_REASON_SHUTDOWN\n");
1513 GNUNET_assert(cls !=NULL);
1514 plugin->http_server_task_v4 = GNUNET_SCHEDULER_NO_TASK;
1516 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1519 GNUNET_assert (MHD_YES == MHD_run (plugin->http_server_daemon_v4));
1520 plugin->http_server_task_v4 = http_server_daemon_prepare (plugin, plugin->http_server_daemon_v4);
1525 * Call MHD IPv6 to process pending requests and then go back
1526 * and schedule the next run.
1527 * @param cls plugin as closure
1528 * @param tc task context
1531 http_server_daemon_v6_run (void *cls,
1532 const struct GNUNET_SCHEDULER_TaskContext *tc)
1534 struct Plugin *plugin = cls;
1536 #if DEBUG_SCHEDULING
1537 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_READ_READY))
1538 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1539 "http_server_daemon_v6_run: GNUNET_SCHEDULER_REASON_READ_READY\n");
1540 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_WRITE_READY))
1541 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1542 "http_server_daemon_v6_run: GNUNET_SCHEDULER_REASON_WRITE_READY\n");
1543 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_TIMEOUT))
1544 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1545 "http_server_daemon_v6_run: GNUNET_SCHEDULER_REASON_TIMEOUT\n");
1546 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_STARTUP))
1547 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1548 "http_server_daemon_v6_run: GGNUNET_SCHEDULER_REASON_STARTUP\n");
1549 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1550 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1551 "http_server_daemon_v6_run: GGNUNET_SCHEDULER_REASON_SHUTDOWN\n");
1554 GNUNET_assert(cls !=NULL);
1555 plugin->http_server_task_v6 = GNUNET_SCHEDULER_NO_TASK;
1557 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1560 GNUNET_assert (MHD_YES == MHD_run (plugin->http_server_daemon_v6));
1561 plugin->http_server_task_v6 = http_server_daemon_prepare (plugin, plugin->http_server_daemon_v6);
1566 curl_get_header_cb( void *ptr,
1567 size_t size, size_t nmemb,
1570 struct Session * ps = stream;
1572 long http_result = 0;
1574 /* Getting last http result code */
1575 GNUNET_assert(NULL!=ps);
1576 if (ps->recv_connected==GNUNET_NO)
1578 res = curl_easy_getinfo(ps->recv_endpoint, CURLINFO_RESPONSE_CODE, &http_result);
1579 if (CURLE_OK == res)
1581 if (http_result == 200)
1583 ps->recv_connected = GNUNET_YES;
1584 ps->recv_active = GNUNET_YES;
1585 #if DEBUG_CONNECTIONS
1586 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: connected to recieve data\n",ps);
1588 // Calling send_check_connections again since receive is established
1589 send_check_connections (ps->peercontext->plugin, ps);
1596 size_t len = size * nmemb;
1598 if ((size * nmemb) < SIZE_MAX)
1599 tmp = GNUNET_malloc (len+1);
1601 if ((tmp != NULL) && (len > 0))
1603 memcpy(tmp,ptr,len);
1606 if (tmp[len-2] == 13)
1609 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1610 "Connection %X: Header: %s\n",
1613 GNUNET_free_non_null (tmp);
1616 return size * nmemb;
1621 * Callback called by libcurl when new headers arrive
1622 * Used to get HTTP result for curl operations
1623 * @param ptr stream to read from
1624 * @param size size of one char element
1625 * @param nmemb number of char elements
1626 * @param stream closure set by user
1627 * @return bytes read by function
1630 curl_put_header_cb(void *ptr,
1635 struct Session * ps = stream;
1638 size_t len = size * nmemb;
1639 long http_result = 0;
1642 /* Getting last http result code */
1643 GNUNET_assert(NULL!=ps);
1644 res = curl_easy_getinfo (ps->send_endpoint, CURLINFO_RESPONSE_CODE, &http_result);
1645 if (CURLE_OK == res)
1647 if ((http_result == 100) && (ps->send_connected==GNUNET_NO))
1649 ps->send_connected = GNUNET_YES;
1650 ps->send_active = GNUNET_YES;
1651 #if DEBUG_CONNECTIONS
1652 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1653 "Connection %X: connected to send data\n",
1657 if ((http_result == 200) && (ps->send_connected==GNUNET_YES))
1659 ps->send_connected = GNUNET_NO;
1660 ps->send_active = GNUNET_NO;
1661 #if DEBUG_CONNECTIONS
1662 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1663 "Connection %X: sending disconnected\n",
1670 if ((size * nmemb) < SIZE_MAX)
1671 tmp = GNUNET_malloc (len+1);
1673 if ((tmp != NULL) && (len > 0))
1675 memcpy(tmp,ptr,len);
1678 if (tmp[len-2] == 13)
1682 GNUNET_free_non_null (tmp);
1683 return size * nmemb;
1687 * Callback method used with libcurl
1688 * Method is called when libcurl needs to read data during sending
1689 * @param stream pointer where to write data
1690 * @param size size of an individual element
1691 * @param nmemb count of elements that can be written to the buffer
1692 * @param ptr source pointer, passed to the libcurl handle
1693 * @return bytes written to stream
1696 curl_send_cb(void *stream,
1697 size_t size, size_t nmemb,
1700 struct Session * ps = ptr;
1701 struct HTTP_Message * msg = ps->pending_msgs_tail;
1705 if (ps->send_active == GNUNET_NO)
1706 return CURL_READFUNC_PAUSE;
1707 if ( (ps->pending_msgs_tail == NULL) &&
1708 (ps->send_active == GNUNET_YES) )
1710 #if DEBUG_CONNECTIONS
1711 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1712 "Connection %X: No Message to send, pausing connection\n",
1715 ps->send_active = GNUNET_NO;
1716 return CURL_READFUNC_PAUSE;
1719 GNUNET_assert (msg!=NULL);
1722 if (msg->pos < msg->size)
1724 /* data fit in buffer */
1725 if ((msg->size - msg->pos) <= (size * nmemb))
1727 len = (msg->size - msg->pos);
1728 memcpy(stream, &msg->buf[msg->pos], len);
1735 memcpy(stream, &msg->buf[msg->pos], len);
1740 /* no data to send */
1746 if ( msg->pos == msg->size)
1748 #if DEBUG_CONNECTIONS
1749 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1750 "Connection %X: Message with %u bytes sent, removing message from queue\n",
1754 /* Calling transmit continuation */
1755 if (NULL != ps->pending_msgs_tail->transmit_cont)
1756 msg->transmit_cont (ps->pending_msgs_tail->transmit_cont_cls,
1757 &(ps->peercontext)->identity,
1759 ps->queue_length_cur -= msg->size;
1760 remove_http_message(ps, msg);
1767 curl_receive_mst_cb (void *cls,
1769 const struct GNUNET_MessageHeader *message)
1771 struct Session *ps = cls;
1772 struct GNUNET_TIME_Relative delay;
1773 GNUNET_assert(ps != NULL);
1775 struct HTTP_PeerContext *pc = ps->peercontext;
1776 GNUNET_assert(pc != NULL);
1778 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1779 "Connection %X: Forwarding message to transport service, type %u and size %u from `%s' (`%s')\n",
1781 ntohs(message->type),
1782 ntohs(message->size),
1783 GNUNET_i2s(&(pc->identity)),
1784 http_plugin_address_to_string(NULL,ps->addr,ps->addrlen));
1786 struct GNUNET_TRANSPORT_ATS_Information distance[2];
1787 distance[0].type = htonl (GNUNET_TRANSPORT_ATS_QUALITY_NET_DISTANCE);
1788 distance[0].value = htonl (1);
1789 distance[1].type = htonl (GNUNET_TRANSPORT_ATS_ARRAY_TERMINATOR);
1790 distance[1].value = htonl (0);
1792 delay = pc->plugin->env->receive (pc->plugin->env->cls,
1795 (const struct GNUNET_TRANSPORT_ATS_Information *) &distance, 2,
1800 if (pc->reset_task != GNUNET_SCHEDULER_NO_TASK)
1801 GNUNET_SCHEDULER_cancel (pc->reset_task);
1803 if (delay.rel_value > 0)
1806 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1807 "Connection %X: Inbound quota management: delay next read for %llu ms\n",
1808 ps, delay.rel_value);
1810 pc->reset_task = GNUNET_SCHEDULER_add_delayed (delay, &reset_inbound_quota_delay, pc);
1816 * Callback method used with libcurl
1817 * Method is called when libcurl needs to write data during sending
1818 * @param stream pointer where to write data
1819 * @param size size of an individual element
1820 * @param nmemb count of elements that can be written to the buffer
1821 * @param ptr destination pointer, passed to the libcurl handle
1822 * @return bytes read from stream
1825 curl_receive_cb( void *stream, size_t size, size_t nmemb, void *ptr)
1827 struct Session * ps = ptr;
1829 if (ps->peercontext->delay.rel_value > 0)
1832 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1833 "Connection %X: no inbound bandwidth available! Next read was delayed for %llu ms\n",
1834 ps, ps->peercontext->delay.rel_value);
1838 #if DEBUG_CONNECTIONS
1839 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1840 "Connection %X: %u bytes received\n",
1843 GNUNET_SERVER_mst_receive(ps->msgtok, ps,
1845 GNUNET_NO, GNUNET_NO);
1846 return (size * nmemb);
1851 curl_handle_finished (struct Plugin *plugin)
1853 struct Session *ps = NULL;
1854 struct HTTP_PeerContext *pc = NULL;
1855 struct CURLMsg *msg;
1856 struct HTTP_Message * cur_msg = NULL;
1863 msg = curl_multi_info_read (plugin->multi_handle, &msgs_in_queue);
1864 if ((msgs_in_queue == 0) || (msg == NULL))
1866 /* get session for affected curl handle */
1867 GNUNET_assert ( msg->easy_handle != NULL );
1868 curl_easy_getinfo(msg->easy_handle, CURLINFO_PRIVATE, &tmp);
1869 ps = (struct Session *) tmp;
1870 GNUNET_assert ( ps != NULL );
1871 pc = ps->peercontext;
1872 GNUNET_assert ( pc != NULL );
1876 if ( (msg->data.result != CURLE_OK) &&
1877 (msg->data.result != CURLE_GOT_NOTHING) )
1879 /* sending msg failed*/
1880 if (msg->easy_handle == ps->send_endpoint)
1882 #if DEBUG_CONNECTIONS
1883 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1884 _("Connection %X: HTTP PUT to peer `%s' (`%s') failed: `%s' `%s'\n"),
1886 GNUNET_i2s(&pc->identity),
1887 http_plugin_address_to_string(NULL, ps->addr, ps->addrlen),
1888 "curl_multi_perform",
1889 curl_easy_strerror (msg->data.result));
1891 ps->send_connected = GNUNET_NO;
1892 ps->send_active = GNUNET_NO;
1893 curl_multi_remove_handle(plugin->multi_handle,ps->send_endpoint);
1894 while (ps->pending_msgs_tail != NULL)
1896 cur_msg = ps->pending_msgs_tail;
1897 if ( NULL != cur_msg->transmit_cont)
1898 cur_msg->transmit_cont (cur_msg->transmit_cont_cls,&pc->identity,GNUNET_SYSERR);
1899 ps->queue_length_cur -= cur_msg->size;
1900 remove_http_message(ps,cur_msg);
1903 /* GET connection failed */
1904 if (msg->easy_handle == ps->recv_endpoint)
1906 #if DEBUG_CONNECTIONS
1907 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1908 _("Connection %X: HTTP GET to peer `%s' (`%s') failed: `%s' `%s'\n"),
1910 GNUNET_i2s(&pc->identity),
1911 http_plugin_address_to_string(NULL, ps->addr, ps->addrlen),
1912 "curl_multi_perform",
1913 curl_easy_strerror (msg->data.result));
1915 ps->recv_connected = GNUNET_NO;
1916 ps->recv_active = GNUNET_NO;
1917 curl_multi_remove_handle(plugin->multi_handle,ps->recv_endpoint);
1922 if (msg->easy_handle == ps->send_endpoint)
1924 GNUNET_assert (CURLE_OK == curl_easy_getinfo(msg->easy_handle, CURLINFO_RESPONSE_CODE, &http_result));
1925 #if DEBUG_CONNECTIONS
1926 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1927 "Connection %X: HTTP PUT connection to peer `%s' (`%s') was closed with HTTP code %u\n",
1929 GNUNET_i2s(&pc->identity),
1930 http_plugin_address_to_string(NULL, ps->addr, ps->addrlen),
1933 /* Calling transmit continuation */
1934 while (ps->pending_msgs_tail != NULL)
1936 cur_msg = ps->pending_msgs_tail;
1937 if ( NULL != cur_msg->transmit_cont)
1939 /* HTTP 1xx : Last message before here was informational */
1940 if ((http_result >=100) && (http_result < 200))
1941 cur_msg->transmit_cont (cur_msg->transmit_cont_cls,&pc->identity,GNUNET_OK);
1942 /* HTTP 2xx: successful operations */
1943 if ((http_result >=200) && (http_result < 300))
1944 cur_msg->transmit_cont (cur_msg->transmit_cont_cls,&pc->identity,GNUNET_OK);
1945 /* HTTP 3xx..5xx: error */
1946 if ((http_result >=300) && (http_result < 600))
1947 cur_msg->transmit_cont (cur_msg->transmit_cont_cls,&pc->identity,GNUNET_SYSERR);
1949 ps->queue_length_cur -= cur_msg->size;
1950 remove_http_message(ps,cur_msg);
1953 ps->send_connected = GNUNET_NO;
1954 ps->send_active = GNUNET_NO;
1955 curl_multi_remove_handle(plugin->multi_handle,ps->send_endpoint);
1957 if (msg->easy_handle == ps->recv_endpoint)
1959 #if DEBUG_CONNECTIONS
1960 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1961 "Connection %X: HTTP GET connection to peer `%s' (`%s') was closed with HTTP code %u\n",
1963 GNUNET_i2s(&pc->identity),
1964 http_plugin_address_to_string(NULL, ps->addr, ps->addrlen),
1967 ps->recv_connected = GNUNET_NO;
1968 ps->recv_active = GNUNET_NO;
1969 curl_multi_remove_handle(plugin->multi_handle,ps->recv_endpoint);
1971 plugin->current_connections--;
1973 if ((ps->recv_connected == GNUNET_NO) && (ps->send_connected == GNUNET_NO))
1974 remove_session (pc, ps, GNUNET_YES, GNUNET_SYSERR);
1980 while ( (msgs_in_queue > 0) );
1985 * Task performing curl operations
1986 * @param cls plugin as closure
1987 * @param tc gnunet scheduler task context
1989 static void curl_perform (void *cls,
1990 const struct GNUNET_SCHEDULER_TaskContext *tc)
1992 struct Plugin *plugin = cls;
1993 static unsigned int handles_last_run;
1997 GNUNET_assert(cls !=NULL);
1999 plugin->http_curl_task = GNUNET_SCHEDULER_NO_TASK;
2000 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
2005 mret = curl_multi_perform (plugin->multi_handle, &running);
2006 if ((running < handles_last_run) && (running>0))
2007 curl_handle_finished(plugin);
2008 handles_last_run = running;
2010 while (mret == CURLM_CALL_MULTI_PERFORM);
2011 curl_schedule(plugin);
2016 * Function setting up file descriptors and scheduling task to run
2018 * @param plugin plugin as closure
2019 * @return GNUNET_SYSERR for hard failure, GNUNET_OK for ok
2022 curl_schedule(struct Plugin *plugin)
2028 struct GNUNET_NETWORK_FDSet *grs;
2029 struct GNUNET_NETWORK_FDSet *gws;
2033 /* Cancel previous scheduled task */
2034 if (plugin->http_curl_task != GNUNET_SCHEDULER_NO_TASK)
2036 GNUNET_SCHEDULER_cancel(plugin->http_curl_task);
2037 plugin->http_curl_task = GNUNET_SCHEDULER_NO_TASK;
2044 mret = curl_multi_fdset (plugin->multi_handle, &rs, &ws, &es, &max);
2045 if (mret != CURLM_OK)
2047 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2048 _("%s failed at %s:%d: `%s'\n"),
2049 "curl_multi_fdset", __FILE__, __LINE__,
2050 curl_multi_strerror (mret));
2051 return GNUNET_SYSERR;
2053 mret = curl_multi_timeout (plugin->multi_handle, &to);
2054 if (mret != CURLM_OK)
2056 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2057 _("%s failed at %s:%d: `%s'\n"),
2058 "curl_multi_timeout", __FILE__, __LINE__,
2059 curl_multi_strerror (mret));
2060 return GNUNET_SYSERR;
2063 grs = GNUNET_NETWORK_fdset_create ();
2064 gws = GNUNET_NETWORK_fdset_create ();
2065 GNUNET_NETWORK_fdset_copy_native (grs, &rs, max + 1);
2066 GNUNET_NETWORK_fdset_copy_native (gws, &ws, max + 1);
2067 plugin->http_curl_task = GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
2068 GNUNET_SCHEDULER_NO_TASK,
2070 ? GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
2071 : GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, to),
2076 GNUNET_NETWORK_fdset_destroy (gws);
2077 GNUNET_NETWORK_fdset_destroy (grs);
2084 * Function to log curl debug messages with GNUNET_log
2085 * @param curl handle
2086 * @param type curl_infotype
2089 * @param cls closure
2093 curl_logger (CURL * curl,
2095 char * data, size_t size,
2098 if (type == CURLINFO_TEXT)
2101 memcpy(text,data,size);
2102 if (text[size-1] == '\n')
2107 text[size+1] = '\0';
2109 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2110 "CURL: Connection %X - %s",
2120 * Function setting up curl handle and selecting message to send
2122 * @param plugin plugin
2124 * @return GNUNET_SYSERR on failure, GNUNET_NO if connecting, GNUNET_YES if ok
2127 send_check_connections (struct Plugin *plugin,
2131 struct GNUNET_TIME_Relative timeout = GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT;
2133 if ((ps->direction == OUTBOUND) && (plugin->current_connections < plugin->max_connect_per_transport))
2135 /* RECV DIRECTION */
2136 /* Check if session is connected to receive data, otherwise connect to peer */
2138 if (ps->recv_connected == GNUNET_NO)
2140 int fresh = GNUNET_NO;
2141 if (ps->recv_endpoint == NULL)
2144 ps->recv_endpoint = curl_easy_init();
2147 curl_easy_setopt(ps->recv_endpoint, CURLOPT_VERBOSE, 1L);
2148 curl_easy_setopt(ps->recv_endpoint, CURLOPT_DEBUGFUNCTION , &curl_logger);
2149 curl_easy_setopt(ps->recv_endpoint, CURLOPT_DEBUGDATA , ps->recv_endpoint);
2152 curl_easy_setopt(ps->recv_endpoint, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1);
2153 curl_easy_setopt(ps->recv_endpoint, CURLOPT_SSL_VERIFYPEER, 0);
2154 curl_easy_setopt(ps->recv_endpoint, CURLOPT_SSL_VERIFYHOST, 0);
2156 curl_easy_setopt(ps->recv_endpoint, CURLOPT_URL, ps->url);
2157 curl_easy_setopt(ps->recv_endpoint, CURLOPT_HEADERFUNCTION, &curl_get_header_cb);
2158 curl_easy_setopt(ps->recv_endpoint, CURLOPT_WRITEHEADER, ps);
2159 curl_easy_setopt(ps->recv_endpoint, CURLOPT_READFUNCTION, curl_send_cb);
2160 curl_easy_setopt(ps->recv_endpoint, CURLOPT_READDATA, ps);
2161 curl_easy_setopt(ps->recv_endpoint, CURLOPT_WRITEFUNCTION, curl_receive_cb);
2162 curl_easy_setopt(ps->recv_endpoint, CURLOPT_WRITEDATA, ps);
2163 curl_easy_setopt(ps->recv_endpoint, CURLOPT_TIMEOUT, (long) timeout.rel_value);
2164 curl_easy_setopt(ps->recv_endpoint, CURLOPT_PRIVATE, ps);
2165 curl_easy_setopt(ps->recv_endpoint, CURLOPT_CONNECTTIMEOUT, HTTP_CONNECT_TIMEOUT);
2166 curl_easy_setopt(ps->recv_endpoint, CURLOPT_BUFFERSIZE, 2*GNUNET_SERVER_MAX_MESSAGE_SIZE);
2167 #if CURL_TCP_NODELAY
2168 curl_easy_setopt(ps->recv_endpoint, CURLOPT_TCP_NODELAY, 1);
2170 if (fresh==GNUNET_YES)
2172 mret = curl_multi_add_handle(plugin->multi_handle, ps->recv_endpoint);
2173 if (mret != CURLM_OK)
2175 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2176 _("Connection: %X: %s failed at %s:%d: `%s'\n"),
2178 "curl_multi_add_handle", __FILE__, __LINE__,
2179 curl_multi_strerror (mret));
2180 return GNUNET_SYSERR;
2183 if (plugin->http_curl_task != GNUNET_SCHEDULER_NO_TASK)
2185 GNUNET_SCHEDULER_cancel(plugin->http_curl_task);
2186 plugin->http_curl_task = GNUNET_SCHEDULER_NO_TASK;
2188 plugin->current_connections ++;
2189 plugin->http_curl_task = GNUNET_SCHEDULER_add_now (&curl_perform, plugin);
2192 /* waiting for receive direction */
2193 if (ps->recv_connected==GNUNET_NO)
2196 /* SEND DIRECTION */
2197 /* Check if session is connected to send data, otherwise connect to peer */
2198 if ((ps->send_connected == GNUNET_YES) && (ps->send_endpoint!= NULL))
2200 if (ps->send_active == GNUNET_YES)
2202 #if DEBUG_CONNECTIONS
2203 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2204 "Connection %X: outbound active, enqueueing message\n",
2209 if (ps->send_active == GNUNET_NO)
2211 #if DEBUG_CONNECTIONS
2212 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2213 "Connection %X: outbound paused, unpausing existing connection and enqueueing message\n",
2216 if (CURLE_OK == curl_easy_pause(ps->send_endpoint,CURLPAUSE_CONT))
2218 ps->send_active=GNUNET_YES;
2219 if (plugin->http_curl_task != GNUNET_SCHEDULER_NO_TASK)
2221 GNUNET_SCHEDULER_cancel(plugin->http_curl_task);
2222 plugin->http_curl_task = GNUNET_SCHEDULER_NO_TASK;
2224 plugin->http_curl_task = GNUNET_SCHEDULER_add_now (&curl_perform, plugin);
2228 return GNUNET_SYSERR;
2231 /* not connected, initiate connection */
2232 if ((ps->send_connected==GNUNET_NO) && (plugin->current_connections < plugin->max_connect_per_transport))
2234 int fresh = GNUNET_NO;
2235 if (NULL == ps->send_endpoint)
2237 ps->send_endpoint = curl_easy_init();
2240 GNUNET_assert (ps->send_endpoint != NULL);
2241 GNUNET_assert (NULL != ps->pending_msgs_tail);
2242 #if DEBUG_CONNECTIONS
2243 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2244 "Connection %X: outbound not connected, initiating connection\n",
2247 ps->send_active = GNUNET_NO;
2250 curl_easy_setopt(ps->send_endpoint, CURLOPT_VERBOSE, 1L);
2251 curl_easy_setopt(ps->send_endpoint, CURLOPT_DEBUGFUNCTION , &curl_logger);
2252 curl_easy_setopt(ps->send_endpoint, CURLOPT_DEBUGDATA , ps->send_endpoint);
2255 curl_easy_setopt (ps->send_endpoint, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1);
2256 curl_easy_setopt(ps->send_endpoint, CURLOPT_SSL_VERIFYPEER, 0);
2257 curl_easy_setopt(ps->send_endpoint, CURLOPT_SSL_VERIFYHOST, 0);
2259 curl_easy_setopt(ps->send_endpoint, CURLOPT_URL, ps->url);
2260 curl_easy_setopt(ps->send_endpoint, CURLOPT_PUT, 1L);
2261 curl_easy_setopt(ps->send_endpoint, CURLOPT_HEADERFUNCTION, &curl_put_header_cb);
2262 curl_easy_setopt(ps->send_endpoint, CURLOPT_WRITEHEADER, ps);
2263 curl_easy_setopt(ps->send_endpoint, CURLOPT_READFUNCTION, curl_send_cb);
2264 curl_easy_setopt(ps->send_endpoint, CURLOPT_READDATA, ps);
2265 curl_easy_setopt(ps->send_endpoint, CURLOPT_WRITEFUNCTION, curl_receive_cb);
2266 curl_easy_setopt(ps->send_endpoint, CURLOPT_READDATA, ps);
2267 curl_easy_setopt(ps->send_endpoint, CURLOPT_TIMEOUT, (long) timeout.rel_value);
2268 curl_easy_setopt(ps->send_endpoint, CURLOPT_PRIVATE, ps);
2269 curl_easy_setopt(ps->send_endpoint, CURLOPT_CONNECTTIMEOUT, HTTP_CONNECT_TIMEOUT);
2270 curl_easy_setopt(ps->send_endpoint, CURLOPT_BUFFERSIZE, 2 * GNUNET_SERVER_MAX_MESSAGE_SIZE);
2271 #if CURL_TCP_NODELAY
2272 curl_easy_setopt(ps->send_endpoint, CURLOPT_TCP_NODELAY, 1);
2274 if (fresh==GNUNET_YES)
2276 mret = curl_multi_add_handle(plugin->multi_handle, ps->send_endpoint);
2277 if (mret != CURLM_OK)
2279 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2280 _("Connection: %X: %s failed at %s:%d: `%s'\n"),
2282 "curl_multi_add_handle", __FILE__, __LINE__,
2283 curl_multi_strerror (mret));
2284 return GNUNET_SYSERR;
2288 if (plugin->http_curl_task != GNUNET_SCHEDULER_NO_TASK)
2290 GNUNET_SCHEDULER_cancel(plugin->http_curl_task);
2291 plugin->http_curl_task = GNUNET_SCHEDULER_NO_TASK;
2293 plugin->current_connections++;
2294 plugin->http_curl_task = GNUNET_SCHEDULER_add_now (&curl_perform, plugin);
2297 if (ps->direction == INBOUND)
2299 GNUNET_assert (NULL != ps->pending_msgs_tail);
2300 if ((ps->recv_connected==GNUNET_YES) && (ps->send_connected==GNUNET_YES) &&
2301 (ps->recv_force_disconnect==GNUNET_NO) && (ps->recv_force_disconnect==GNUNET_NO))
2304 return GNUNET_SYSERR;
2309 * select best session to transmit data to peer
2311 * @param pc peer context of target peer
2312 * @param addr address of target peer
2313 * @param addrlen address length
2314 * @param force_address does transport service enforce address?
2315 * @param session session passed by transport service
2316 * @return selected session
2319 static struct Session *
2320 send_select_session (struct HTTP_PeerContext *pc,
2321 const void * addr, size_t addrlen,
2323 struct Session * session)
2325 struct Session * tmp = NULL;
2326 int addr_given = GNUNET_NO;
2328 if ((addr!=NULL) && (addrlen>0))
2329 addr_given = GNUNET_YES;
2331 if (force_address == GNUNET_YES)
2333 /* check session given as argument */
2334 if ((session != NULL) && (addr_given == GNUNET_YES))
2336 if (0 == memcmp(session->addr, addr, addrlen))
2338 /* connection can not be used, since it is disconnected */
2339 if ( (session->recv_force_disconnect==GNUNET_NO) &&
2340 (session->send_force_disconnect==GNUNET_NO) )
2342 #if DEBUG_SESSION_SELECTION
2343 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2344 "Session %X selected: Using session passed by transport to send to forced address \n",
2351 /* check last session used */
2352 if ((pc->last_session != NULL)&& (addr_given == GNUNET_YES))
2354 if (0 == memcmp(pc->last_session->addr, addr, addrlen))
2356 /* connection can not be used, since it is disconnected */
2357 if ( (pc->last_session->recv_force_disconnect==GNUNET_NO) &&
2358 (pc->last_session->send_force_disconnect==GNUNET_NO) )
2360 #if DEBUG_SESSION_SELECTION
2361 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2362 "Session %X selected: Using last session used to send to forced address \n",
2365 return pc->last_session;
2369 /* find session in existing sessions */
2371 while ((tmp!=NULL) && (addr_given == GNUNET_YES))
2373 if (0 == memcmp(tmp->addr, addr, addrlen))
2375 /* connection can not be used, since it is disconnected */
2376 if ( (tmp->recv_force_disconnect==GNUNET_NO) &&
2377 (tmp->send_force_disconnect==GNUNET_NO) )
2379 #if DEBUG_SESSION_SELECTION
2380 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2381 "Session %X selected: Using existing session to send to forced address \n",
2389 /* no session to use */
2392 if ((force_address == GNUNET_NO) || (force_address == GNUNET_SYSERR))
2394 /* check session given as argument */
2395 if (session != NULL)
2397 /* connection can not be used, since it is disconnected */
2398 if ( (session->recv_force_disconnect==GNUNET_NO) &&
2399 (session->send_force_disconnect==GNUNET_NO) )
2401 #if DEBUG_SESSION_SELECTION
2402 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2403 "Session %X selected: Using session passed by transport to send not-forced address\n",
2409 /* check last session used */
2410 if (pc->last_session != NULL)
2412 /* connection can not be used, since it is disconnected */
2413 if ( (pc->last_session->recv_force_disconnect==GNUNET_NO) &&
2414 (pc->last_session->send_force_disconnect==GNUNET_NO) )
2416 #if DEBUG_SESSION_SELECTION
2417 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2418 "Session %X selected: Using last session to send to not-forced address\n",
2421 return pc->last_session;
2424 /* find session in existing sessions */
2428 /* connection can not be used, since it is disconnected */
2429 if ( (tmp->recv_force_disconnect==GNUNET_NO) &&
2430 (tmp->send_force_disconnect==GNUNET_NO) )
2432 #if DEBUG_SESSION_SELECTION
2433 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2434 "Session %X selected: Using existing session to send to not-forced address\n",
2448 * Function that can be used by the transport service to transmit
2449 * a message using the plugin. Note that in the case of a
2450 * peer disconnecting, the continuation MUST be called
2451 * prior to the disconnect notification itself. This function
2452 * will be called with this peer's HELLO message to initiate
2453 * a fresh connection to another peer.
2455 * @param cls closure
2456 * @param target who should receive this message
2457 * @param msgbuf the message to transmit
2458 * @param msgbuf_size number of bytes in 'msgbuf'
2459 * @param priority how important is the message (most plugins will
2460 * ignore message priority and just FIFO)
2461 * @param to how long to wait at most for the transmission (does not
2462 * require plugins to discard the message after the timeout,
2463 * just advisory for the desired delay; most plugins will ignore
2465 * @param session which session must be used (or NULL for "any")
2466 * @param addr the address to use (can be NULL if the plugin
2467 * is "on its own" (i.e. re-use existing TCP connection))
2468 * @param addrlen length of the address in bytes
2469 * @param force_address GNUNET_YES if the plugin MUST use the given address,
2470 * GNUNET_NO means the plugin may use any other address and
2471 * GNUNET_SYSERR means that only reliable existing
2472 * bi-directional connections should be used (regardless
2474 * @param cont continuation to call once the message has
2475 * been transmitted (or if the transport is ready
2476 * for the next transmission call; or if the
2477 * peer disconnected...); can be NULL
2478 * @param cont_cls closure for cont
2479 * @return number of bytes used (on the physical network, with overheads);
2480 * -1 on hard errors (i.e. address invalid); 0 is a legal value
2481 * and does NOT mean that the message was not transmitted (DV)
2484 http_plugin_send (void *cls,
2485 const struct GNUNET_PeerIdentity *target,
2488 unsigned int priority,
2489 struct GNUNET_TIME_Relative to,
2490 struct Session *session,
2494 GNUNET_TRANSPORT_TransmitContinuation cont,
2497 struct Plugin *plugin = cls;
2498 struct HTTP_Message *msg;
2499 struct HTTP_PeerContext * pc;
2500 struct Session * ps = NULL;
2502 GNUNET_assert(cls !=NULL);
2507 if (force_address == GNUNET_YES)
2508 GNUNET_asprintf(&force, "forced addr.");
2509 else if (force_address == GNUNET_NO)
2510 GNUNET_asprintf(&force, "any addr.");
2511 else if (force_address == GNUNET_SYSERR)
2512 GNUNET_asprintf(&force,"reliable bi-direc. address addr.");
2515 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2516 "Transport tells me to send %u bytes to `%s' using %s (%s) and session: %X\n",
2520 http_plugin_address_to_string(NULL, addr, addrlen),
2525 pc = GNUNET_CONTAINER_multihashmap_get (plugin->peers, &target->hashPubKey);
2529 pc = GNUNET_malloc(sizeof (struct HTTP_PeerContext));
2530 pc->plugin = plugin;
2531 pc->session_id_counter=1;
2532 pc->last_session = NULL;
2533 memcpy(&pc->identity, target, sizeof(struct GNUNET_PeerIdentity));
2534 GNUNET_CONTAINER_multihashmap_put (plugin->peers,
2535 &pc->identity.hashPubKey,
2537 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
2538 GNUNET_STATISTICS_update (plugin->env->stats,
2539 gettext_noop ("# HTTP peers active"),
2543 ps = send_select_session (pc, addr, addrlen, force_address, session);
2544 /* session not existing, but address forced -> creating new session */
2547 if ((addr!=NULL) && (addrlen!=0))
2549 ps = GNUNET_malloc(sizeof (struct Session));
2550 #if DEBUG_SESSION_SELECTION
2551 if (force_address == GNUNET_YES)
2552 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2553 "No existing connection & forced address: creating new session %X to peer %s\n",
2554 ps, GNUNET_i2s(target));
2555 if (force_address != GNUNET_YES)
2556 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2557 "No existing connection: creating new session %X to peer %s\n",
2558 ps, GNUNET_i2s(target));
2560 ps->addr = GNUNET_malloc(addrlen);
2561 memcpy(ps->addr,addr,addrlen);
2562 ps->addrlen = addrlen;
2563 ps->direction=OUTBOUND;
2564 ps->recv_connected = GNUNET_NO;
2565 ps->recv_force_disconnect = GNUNET_NO;
2566 ps->send_connected = GNUNET_NO;
2567 ps->send_force_disconnect = GNUNET_NO;
2568 ps->pending_msgs_head = NULL;
2569 ps->pending_msgs_tail = NULL;
2571 ps->session_id = pc->session_id_counter;
2572 ps->queue_length_cur = 0;
2573 ps->queue_length_max = GNUNET_SERVER_MAX_MESSAGE_SIZE;
2574 pc->session_id_counter++;
2575 ps->url = create_url (plugin, ps->addr, ps->addrlen, ps->session_id);
2576 if (ps->msgtok == NULL)
2577 ps->msgtok = GNUNET_SERVER_mst_create (&curl_receive_mst_cb, ps);
2578 GNUNET_CONTAINER_DLL_insert(pc->head,pc->tail,ps);
2579 GNUNET_STATISTICS_update (plugin->env->stats,
2580 gettext_noop ("# HTTP outbound sessions for peers active"),
2587 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2588 "No existing session found & and no address given: no way to send this message to peer `%s'!\n",
2589 GNUNET_i2s(target));
2591 return GNUNET_SYSERR;
2595 if (msgbuf_size >= (ps->queue_length_max - ps->queue_length_cur))
2597 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2598 "Queue %X full: %u bytes in queue available, message with %u is too big\n",
2600 (ps->queue_length_max - ps->queue_length_cur),
2602 //return GNUNET_SYSERR;
2606 msg = GNUNET_malloc (sizeof (struct HTTP_Message) + msgbuf_size);
2608 msg->size = msgbuf_size;
2610 msg->buf = (char *) &msg[1];
2611 msg->transmit_cont = cont;
2612 msg->transmit_cont_cls = cont_cls;
2613 memcpy (msg->buf,msgbuf, msgbuf_size);
2614 GNUNET_CONTAINER_DLL_insert (ps->pending_msgs_head,
2615 ps->pending_msgs_tail,
2617 ps->queue_length_cur += msgbuf_size;
2618 if (send_check_connections (plugin, ps) == GNUNET_SYSERR)
2619 return GNUNET_SYSERR;
2620 if (force_address != GNUNET_YES)
2621 pc->last_session = ps;
2622 if (pc->last_session==NULL)
2623 pc->last_session = ps;
2629 * Function that can be used to force the plugin to disconnect
2630 * from the given peer and cancel all previous transmissions
2631 * (and their continuationc).
2633 * @param cls closure
2634 * @param target peer from which to disconnect
2637 http_plugin_disconnect (void *cls,
2638 const struct GNUNET_PeerIdentity *target)
2640 struct Plugin *plugin = cls;
2641 struct HTTP_PeerContext *pc = NULL;
2642 struct Session *ps = NULL;
2644 pc = GNUNET_CONTAINER_multihashmap_get (plugin->peers, &target->hashPubKey);
2650 /* Telling transport that session is getting disconnected */
2651 plugin->env->session_end(plugin, target, ps);
2652 if (ps->direction==OUTBOUND)
2654 if (ps->send_endpoint!=NULL)
2656 //GNUNET_assert(CURLM_OK == curl_multi_remove_handle(plugin->multi_handle,ps->send_endpoint));
2657 //curl_easy_cleanup(ps->send_endpoint);
2658 //ps->send_endpoint=NULL;
2659 ps->send_force_disconnect = GNUNET_YES;
2661 if (ps->recv_endpoint!=NULL)
2663 //GNUNET_assert(CURLM_OK == curl_multi_remove_handle(plugin->multi_handle,ps->recv_endpoint));
2664 //curl_easy_cleanup(ps->recv_endpoint);
2665 //ps->recv_endpoint=NULL;
2666 ps->recv_force_disconnect = GNUNET_YES;
2669 if (ps->direction==INBOUND)
2671 ps->recv_force_disconnect = GNUNET_YES;
2672 ps->send_force_disconnect = GNUNET_YES;
2674 while (ps->pending_msgs_head!=NULL)
2675 remove_http_message(ps, ps->pending_msgs_head);
2676 ps->recv_active = GNUNET_NO;
2677 ps->send_active = GNUNET_NO;
2684 * Convert the transports address to a nice, human-readable
2687 * @param cls closure
2688 * @param type name of the transport that generated the address
2689 * @param addr one of the addresses of the host, NULL for the last address
2690 * the specific address format depends on the transport
2691 * @param addrlen length of the address
2692 * @param numeric should (IP) addresses be displayed in numeric form?
2693 * @param timeout after how long should we give up?
2694 * @param asc function to call on each string
2695 * @param asc_cls closure for asc
2698 http_plugin_address_pretty_printer (void *cls,
2703 struct GNUNET_TIME_Relative timeout,
2704 GNUNET_TRANSPORT_AddressStringCallback
2707 const struct IPv4HttpAddress *t4;
2708 const struct IPv6HttpAddress *t6;
2709 struct sockaddr_in a4;
2710 struct sockaddr_in6 a6;
2716 GNUNET_assert(cls !=NULL);
2717 if (addrlen == sizeof (struct IPv6HttpAddress))
2719 address = GNUNET_malloc (INET6_ADDRSTRLEN);
2721 a6.sin6_addr = t6->ipv6_addr;
2722 inet_ntop(AF_INET6, &(a6.sin6_addr),address,INET6_ADDRSTRLEN);
2723 port = ntohs(t6->u6_port);
2725 else if (addrlen == sizeof (struct IPv4HttpAddress))
2727 address = GNUNET_malloc (INET_ADDRSTRLEN);
2729 a4.sin_addr.s_addr = t4->ipv4_addr;
2730 inet_ntop(AF_INET, &(a4.sin_addr),address,INET_ADDRSTRLEN);
2731 port = ntohs(t4->u_port);
2735 /* invalid address */
2736 GNUNET_break_op (0);
2737 asc (asc_cls, NULL);
2740 res = GNUNET_asprintf(&ret,"%s://%s:%u/", PROTOCOL_PREFIX, address, port);
2741 GNUNET_free (address);
2742 GNUNET_assert(res != 0);
2744 GNUNET_free_non_null (ret);
2750 * Another peer has suggested an address for this
2751 * peer and transport plugin. Check that this could be a valid
2752 * address. If so, consider adding it to the list
2755 * @param cls closure
2756 * @param addr pointer to the address
2757 * @param addrlen length of addr
2758 * @return GNUNET_OK if this is a plausible address for this peer
2762 http_plugin_address_suggested (void *cls,
2763 const void *addr, size_t addrlen)
2765 struct Plugin *plugin = cls;
2766 struct IPv4HttpAddress *v4;
2767 struct IPv6HttpAddress *v6;
2768 struct IPv4HttpAddress *tv4 = plugin->ipv4_addr_head;
2769 struct IPv6HttpAddress *tv6 = plugin->ipv6_addr_head;
2771 GNUNET_assert(cls !=NULL);
2772 if ((addrlen != sizeof (struct IPv4HttpAddress)) &&
2773 (addrlen != sizeof (struct IPv6HttpAddress)))
2774 return GNUNET_SYSERR;
2775 if (addrlen == sizeof (struct IPv4HttpAddress))
2777 v4 = (struct IPv4HttpAddress *) addr;
2778 if (plugin->bind4_address!=NULL)
2780 if (0 == memcmp (&plugin->bind4_address->sin_addr, &v4->ipv4_addr, sizeof(uint32_t)))
2783 return GNUNET_SYSERR;
2787 if (0==memcmp (&tv4->ipv4_addr, &v4->ipv4_addr, sizeof(uint32_t)))
2794 return GNUNET_SYSERR;
2796 if (addrlen == sizeof (struct IPv6HttpAddress))
2798 v6 = (struct IPv6HttpAddress *) addr;
2799 if (plugin->bind6_address!=NULL)
2801 if (0 == memcmp (&plugin->bind6_address->sin6_addr, &v6->ipv6_addr, sizeof(struct in6_addr)))
2804 return GNUNET_SYSERR;
2808 if (0 == memcmp (&tv6->ipv6_addr, &v6->ipv6_addr, sizeof(struct in6_addr)))
2815 return GNUNET_SYSERR;
2817 return GNUNET_SYSERR;
2822 * Function called for a quick conversion of the binary address to
2823 * a numeric address. Note that the caller must not free the
2824 * address and that the next call to this function is allowed
2825 * to override the address again.
2827 * @param cls closure
2828 * @param addr binary address
2829 * @param addrlen length of the address
2830 * @return string representing the same address
2833 http_plugin_address_to_string (void *cls,
2837 const struct IPv4HttpAddress *t4;
2838 const struct IPv6HttpAddress *t6;
2839 struct sockaddr_in a4;
2840 struct sockaddr_in6 a6;
2842 static char rbuf[INET6_ADDRSTRLEN + 13];
2846 if (addrlen == sizeof (struct IPv6HttpAddress))
2848 address = GNUNET_malloc (INET6_ADDRSTRLEN);
2850 a6.sin6_addr = t6->ipv6_addr;
2851 inet_ntop(AF_INET6, &(a6.sin6_addr),address,INET6_ADDRSTRLEN);
2852 port = ntohs(t6->u6_port);
2854 else if (addrlen == sizeof (struct IPv4HttpAddress))
2856 address = GNUNET_malloc (INET_ADDRSTRLEN);
2858 a4.sin_addr.s_addr = t4->ipv4_addr;
2859 inet_ntop(AF_INET, &(a4.sin_addr),address,INET_ADDRSTRLEN);
2860 port = ntohs(t4->u_port);
2864 /* invalid address */
2868 res = GNUNET_snprintf (rbuf,
2874 GNUNET_free (address);
2875 GNUNET_assert(res != 0);
2880 * Notify transport service about address
2882 * @param cls the plugin
2886 address_notification (void *cls,
2887 const struct GNUNET_SCHEDULER_TaskContext *tc)
2889 struct Plugin *plugin = cls;
2891 GNUNET_OS_network_interfaces_list (&process_interfaces, plugin);
2895 * Exit point from the plugin.
2898 LIBGNUNET_PLUGIN_TRANSPORT_DONE (void *cls)
2900 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
2901 struct Plugin *plugin = api->cls;
2903 struct IPv4HttpAddress * ipv4addr;
2904 struct IPv6HttpAddress * ipv6addr;
2905 GNUNET_assert(cls !=NULL);
2907 if (plugin->http_server_daemon_v4 != NULL)
2909 MHD_stop_daemon (plugin->http_server_daemon_v4);
2910 plugin->http_server_daemon_v4 = NULL;
2912 if (plugin->http_server_daemon_v6 != NULL)
2914 MHD_stop_daemon (plugin->http_server_daemon_v6);
2915 plugin->http_server_daemon_v6 = NULL;
2917 if ( plugin->http_server_task_v4 != GNUNET_SCHEDULER_NO_TASK)
2919 GNUNET_SCHEDULER_cancel(plugin->http_server_task_v4);
2920 plugin->http_server_task_v4 = GNUNET_SCHEDULER_NO_TASK;
2922 if ( plugin->http_server_task_v6 != GNUNET_SCHEDULER_NO_TASK)
2924 GNUNET_SCHEDULER_cancel(plugin->http_server_task_v6);
2925 plugin->http_server_task_v6 = GNUNET_SCHEDULER_NO_TASK;
2928 while (plugin->ipv4_addr_head!=NULL)
2930 ipv4addr = plugin->ipv4_addr_head;
2931 GNUNET_CONTAINER_DLL_remove(plugin->ipv4_addr_head,plugin->ipv4_addr_tail,ipv4addr);
2932 GNUNET_free(ipv4addr);
2935 while (plugin->ipv6_addr_head!=NULL)
2937 ipv6addr = plugin->ipv6_addr_head;
2938 GNUNET_CONTAINER_DLL_remove(plugin->ipv6_addr_head,plugin->ipv6_addr_tail,ipv6addr);
2939 GNUNET_free(ipv6addr);
2942 /* free all peer information */
2943 if (plugin->peers!=NULL)
2945 GNUNET_CONTAINER_multihashmap_iterate (plugin->peers,
2946 &remove_peer_context_Iterator,
2948 GNUNET_CONTAINER_multihashmap_destroy (plugin->peers);
2950 if (plugin->multi_handle!=NULL)
2952 mret = curl_multi_cleanup(plugin->multi_handle);
2953 if (CURLM_OK != mret)
2954 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2955 "curl multihandle clean up failed\n");
2956 plugin->multi_handle = NULL;
2958 curl_global_cleanup();
2960 if ( plugin->http_curl_task != GNUNET_SCHEDULER_NO_TASK)
2962 GNUNET_SCHEDULER_cancel(plugin->http_curl_task);
2963 plugin->http_curl_task = GNUNET_SCHEDULER_NO_TASK;
2966 GNUNET_free_non_null (plugin->bind4_address);
2967 GNUNET_free_non_null (plugin->bind6_address);
2968 GNUNET_free_non_null(plugin->bind_hostname);
2970 GNUNET_free_non_null (plugin->crypto_init);
2971 GNUNET_free_non_null (plugin->cert);
2972 GNUNET_free_non_null (plugin->key);
2974 GNUNET_free (plugin);
2977 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2978 "Unload %s plugin complete...\n",
2986 load_certificate( const char * file )
2988 struct GNUNET_DISK_FileHandle * gn_file;
2992 if (0!=STAT(file, &fstat))
2994 text = GNUNET_malloc (fstat.st_size+1);
2995 gn_file = GNUNET_DISK_file_open(file, GNUNET_DISK_OPEN_READ, GNUNET_DISK_PERM_USER_READ);
3001 if (GNUNET_SYSERR == GNUNET_DISK_file_read(gn_file, text, fstat.st_size))
3004 GNUNET_DISK_file_close(gn_file);
3007 text[fstat.st_size] = '\0';
3008 GNUNET_DISK_file_close(gn_file);
3015 * Entry point for the plugin.
3018 LIBGNUNET_PLUGIN_TRANSPORT_INIT (void *cls)
3020 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
3021 struct Plugin *plugin;
3022 struct GNUNET_TRANSPORT_PluginFunctions *api;
3023 struct GNUNET_TIME_Relative gn_timeout;
3024 long long unsigned int port;
3025 unsigned long long tneigh;
3026 char * component_name;
3028 char * key_file = NULL;
3029 char * cert_file = NULL;
3032 GNUNET_assert(cls !=NULL);
3034 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3035 "Starting %s plugin...\n",
3038 GNUNET_asprintf(&component_name,
3042 plugin = GNUNET_malloc (sizeof (struct Plugin));
3043 plugin->stats = env->stats;
3045 plugin->peers = NULL;
3046 plugin->bind4_address = NULL;
3047 plugin->bind6_address = NULL;
3048 plugin->use_ipv6 = GNUNET_YES;
3049 plugin->use_ipv4 = GNUNET_YES;
3050 plugin->use_localaddresses = GNUNET_NO;
3052 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
3054 api->send = &http_plugin_send;
3055 api->disconnect = &http_plugin_disconnect;
3056 api->address_pretty_printer = &http_plugin_address_pretty_printer;
3057 api->check_address = &http_plugin_address_suggested;
3058 api->address_to_string = &http_plugin_address_to_string;
3060 /* Hashing our identity to use it in URLs */
3061 GNUNET_CRYPTO_hash_to_enc (&(plugin->env->my_identity->hashPubKey),
3062 &plugin->my_ascii_hash_ident);
3065 if (GNUNET_CONFIGURATION_have_value (env->cfg, "TRANSPORT", "NEIGHBOUR_LIMIT"))
3067 GNUNET_CONFIGURATION_get_value_number (env->cfg,
3076 plugin->max_connect_per_transport = tneigh;
3080 if (GNUNET_CONFIGURATION_have_value (env->cfg,
3081 component_name, "USE_IPv6"))
3083 plugin->use_ipv6 = GNUNET_CONFIGURATION_get_value_yesno (env->cfg,
3088 if (GNUNET_CONFIGURATION_have_value (env->cfg,
3089 component_name, "USE_IPv4"))
3091 plugin->use_ipv4 = GNUNET_CONFIGURATION_get_value_yesno (env->cfg,
3092 component_name,"USE_IPv4");
3094 /* use local addresses? */
3096 if (GNUNET_CONFIGURATION_have_value (env->cfg,
3097 component_name, "USE_LOCALADDR"))
3099 plugin->use_localaddresses = GNUNET_CONFIGURATION_get_value_yesno (env->cfg,
3103 /* Reading port number from config file */
3105 GNUNET_CONFIGURATION_get_value_number (env->cfg,
3111 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
3113 _("Require valid port number for transport plugin `%s' in configuration!\n"),
3115 GNUNET_free(component_name);
3116 LIBGNUNET_PLUGIN_TRANSPORT_DONE (api);
3120 /* Reading ipv4 addresse to bind to from config file */
3121 if ( (plugin->use_ipv4==GNUNET_YES) &&
3122 (GNUNET_CONFIGURATION_have_value (env->cfg,
3123 component_name, "BINDTO4")))
3125 GNUNET_break (GNUNET_OK ==
3126 GNUNET_CONFIGURATION_get_value_string (env->cfg,
3129 &plugin->bind_hostname));
3130 plugin->bind4_address = GNUNET_malloc(sizeof(struct sockaddr_in));
3131 plugin->bind4_address->sin_family = AF_INET;
3132 plugin->bind4_address->sin_port = htons (port);
3134 if (plugin->bind_hostname!=NULL)
3136 if (inet_pton(AF_INET,plugin->bind_hostname, &plugin->bind4_address->sin_addr)<=0)
3138 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
3140 _("Misconfigured address to bind to in configuration!\n"));
3141 GNUNET_free(plugin->bind4_address);
3142 GNUNET_free(plugin->bind_hostname);
3143 plugin->bind_hostname = NULL;
3144 plugin->bind4_address = NULL;
3149 /* Reading ipv4 addresse to bind to from config file */
3150 if ( (plugin->use_ipv6==GNUNET_YES) &&
3151 (GNUNET_CONFIGURATION_have_value (env->cfg,
3152 component_name, "BINDTO6")))
3154 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (env->cfg,
3157 &plugin->bind_hostname))
3159 plugin->bind6_address = GNUNET_malloc(sizeof(struct sockaddr_in6));
3160 plugin->bind6_address->sin6_family = AF_INET6;
3161 plugin->bind6_address->sin6_port = htons (port);
3162 if (plugin->bind_hostname!=NULL)
3164 if (inet_pton(AF_INET6,plugin->bind_hostname, &plugin->bind6_address->sin6_addr)<=0)
3166 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
3168 _("Misconfigured address to bind to in configuration!\n"));
3169 GNUNET_free(plugin->bind6_address);
3170 GNUNET_free(plugin->bind_hostname);
3171 plugin->bind_hostname = NULL;
3172 plugin->bind6_address = NULL;
3179 /* Reading HTTPS crypto related configuration */
3180 /* Get crypto init string from config */
3181 if (GNUNET_CONFIGURATION_have_value (env->cfg,
3182 "transport-https", "CRYPTO_INIT"))
3184 GNUNET_CONFIGURATION_get_value_string (env->cfg,
3187 &plugin->crypto_init);
3191 GNUNET_asprintf(&plugin->crypto_init,"NORMAL");
3194 /* Get private key file from config */
3195 if (GNUNET_CONFIGURATION_have_value (env->cfg,
3196 "transport-https", "KEY_FILE"))
3198 GNUNET_CONFIGURATION_get_value_filename (env->cfg,
3204 GNUNET_asprintf(&key_file,"https.key");
3206 /* Get private key file from config */
3207 if (GNUNET_CONFIGURATION_have_value (env->cfg,"transport-https", "CERT_FILE"))
3209 GNUNET_CONFIGURATION_get_value_filename (env->cfg,
3214 if (cert_file==NULL)
3215 GNUNET_asprintf(&cert_file,"https.cert");
3217 /* read key & certificates from file */
3218 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3219 "Loading TLS certificate `%s' `%s'\n",
3220 key_file, cert_file);
3222 plugin->key = load_certificate( key_file );
3223 plugin->cert = load_certificate( cert_file );
3225 if ((plugin->key==NULL) || (plugin->cert==NULL))
3229 GNUNET_asprintf(&cmd,
3230 "gnunet-transport-certificate-creation %s %s",
3231 key_file, cert_file);
3232 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3233 "No usable TLS certificate found, creating certificate \n");
3237 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
3239 _("Could not create a new TLS certificate, shell script `%s' failed!\n"),cmd,
3241 GNUNET_free (key_file);
3242 GNUNET_free (cert_file);
3243 GNUNET_free (component_name);
3244 LIBGNUNET_PLUGIN_TRANSPORT_DONE(api);
3249 plugin->key = load_certificate( key_file );
3250 plugin->cert = load_certificate( cert_file );
3251 if ((plugin->key==NULL) || (plugin->cert==NULL))
3253 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
3255 _("No usable TLS certificate found and creating one failed! \n"),
3257 GNUNET_free (key_file);
3258 GNUNET_free (cert_file);
3259 GNUNET_free (component_name);
3261 LIBGNUNET_PLUGIN_TRANSPORT_DONE(api);
3265 GNUNET_free (key_file);
3266 GNUNET_free (cert_file);
3268 GNUNET_assert((plugin->key!=NULL) && (plugin->cert!=NULL));
3269 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "TLS certificate loaded\n");
3272 GNUNET_assert ((port > 0) && (port <= 65535));
3273 plugin->port_inbound = port;
3274 gn_timeout = GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT;
3275 unsigned int timeout = (gn_timeout.rel_value) / 1000;
3276 if ( (plugin->http_server_daemon_v6 == NULL) &&
3277 (plugin->use_ipv6 == GNUNET_YES) &&
3280 struct sockaddr * tmp = (struct sockaddr *) plugin->bind6_address;
3281 plugin->http_server_daemon_v6 = MHD_start_daemon (
3290 &mhd_accept_cb, plugin,
3291 &mhd_access_cb, plugin,
3292 MHD_OPTION_SOCK_ADDR, tmp,
3293 MHD_OPTION_CONNECTION_LIMIT, (unsigned int) plugin->max_connect_per_transport,
3295 MHD_OPTION_HTTPS_PRIORITIES, plugin->crypto_init,
3296 MHD_OPTION_HTTPS_MEM_KEY, plugin->key,
3297 MHD_OPTION_HTTPS_MEM_CERT, plugin->cert,
3299 MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) timeout,
3300 MHD_OPTION_CONNECTION_MEMORY_LIMIT, (size_t) (2 * GNUNET_SERVER_MAX_MESSAGE_SIZE),
3301 MHD_OPTION_NOTIFY_COMPLETED, &mhd_termination_cb, plugin,
3302 MHD_OPTION_EXTERNAL_LOGGER, mhd_logger, plugin->mhd_log,
3305 if ( (plugin->http_server_daemon_v4 == NULL) &&
3306 (plugin->use_ipv4 == GNUNET_YES) &&
3309 plugin->http_server_daemon_v4 = MHD_start_daemon (
3318 &mhd_accept_cb, plugin ,
3319 &mhd_access_cb, plugin,
3320 MHD_OPTION_SOCK_ADDR, (struct sockaddr_in *) plugin->bind4_address,
3321 MHD_OPTION_CONNECTION_LIMIT, (unsigned int) plugin->max_connect_per_transport,
3323 MHD_OPTION_HTTPS_PRIORITIES, plugin->crypto_init,
3324 MHD_OPTION_HTTPS_MEM_KEY, plugin->key,
3325 MHD_OPTION_HTTPS_MEM_CERT, plugin->cert,
3327 MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) timeout,
3328 MHD_OPTION_CONNECTION_MEMORY_LIMIT, (size_t) (2 * GNUNET_SERVER_MAX_MESSAGE_SIZE),
3329 MHD_OPTION_NOTIFY_COMPLETED, &mhd_termination_cb, plugin,
3330 MHD_OPTION_EXTERNAL_LOGGER, mhd_logger, plugin->mhd_log,
3333 if (plugin->http_server_daemon_v4 != NULL)
3334 plugin->http_server_task_v4 = http_server_daemon_prepare (plugin, plugin->http_server_daemon_v4);
3335 if (plugin->http_server_daemon_v6 != NULL)
3336 plugin->http_server_task_v6 = http_server_daemon_prepare (plugin, plugin->http_server_daemon_v6);
3339 if (plugin->http_server_task_v4 != GNUNET_SCHEDULER_NO_TASK)
3342 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3343 "Starting MHD with IPv4 bound to %s with port %u\n",
3344 (plugin->bind_hostname!=NULL) ? plugin->bind_hostname : "every address",port);
3347 else if ( (plugin->http_server_task_v6 != GNUNET_SCHEDULER_NO_TASK) &&
3348 (plugin->http_server_task_v4 != GNUNET_SCHEDULER_NO_TASK) )
3351 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3352 "Starting MHD with IPv6 bound to %s with port %u\n",
3353 (plugin->bind_hostname!=NULL) ? plugin->bind_hostname : "every address", port);
3356 else if ( (plugin->http_server_task_v6 != GNUNET_SCHEDULER_NO_TASK) &&
3357 (plugin->http_server_task_v4 == GNUNET_SCHEDULER_NO_TASK) )
3360 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3361 "Starting MHD with IPv4 and IPv6 bound to %s with port %u\n",
3362 (plugin->bind_hostname!=NULL) ? plugin->bind_hostname : "every address",
3369 if ((plugin->use_ipv6 == GNUNET_YES) && (plugin->use_ipv4 == GNUNET_YES))
3370 GNUNET_asprintf(&tmp,"with IPv4 and IPv6 enabled");
3371 if ((plugin->use_ipv6 == GNUNET_NO) && (plugin->use_ipv4 == GNUNET_YES))
3372 GNUNET_asprintf(&tmp,"with IPv4 enabled");
3373 if ((plugin->use_ipv6 == GNUNET_YES) && (plugin->use_ipv4 == GNUNET_NO))
3374 GNUNET_asprintf(&tmp,"with IPv6 enabled");
3375 if ((plugin->use_ipv6 == GNUNET_NO) && (plugin->use_ipv4 == GNUNET_NO))
3376 GNUNET_asprintf(&tmp,"with NO IP PROTOCOL enabled");
3377 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3378 _("HTTP Server with %s could not be started on port %u! %s plugin failed!\n"),
3379 tmp, port, PROTOCOL_PREFIX);
3381 GNUNET_free (component_name);
3382 LIBGNUNET_PLUGIN_TRANSPORT_DONE (api);
3386 /* Initializing cURL */
3387 curl_global_init(CURL_GLOBAL_ALL);
3388 plugin->multi_handle = curl_multi_init();
3390 if ( NULL == plugin->multi_handle )
3392 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
3394 _("Could not initialize curl multi handle, failed to start %s plugin!\n"),
3396 GNUNET_free(component_name);
3397 LIBGNUNET_PLUGIN_TRANSPORT_DONE (api);
3401 plugin->peers = GNUNET_CONTAINER_multihashmap_create (10);
3403 GNUNET_free(component_name);
3404 GNUNET_SCHEDULER_add_now(address_notification, plugin);
3408 /* end of plugin_transport_http.c */