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);
583 GNUNET_free_non_null(addr_str);
589 * Removes a message from the linked list of messages
592 * @return GNUNET_SYSERR if msg not found, GNUNET_OK on success
595 remove_http_message (struct Session * ps,
596 struct HTTP_Message * msg)
598 GNUNET_CONTAINER_DLL_remove(ps->pending_msgs_head,
599 ps->pending_msgs_tail,
606 * Iterator to remove peer context
607 * @param cls the plugin
608 * @param key the peers public key hashcode
609 * @param value the peer context
610 * @return GNUNET_YES on success
613 remove_peer_context_Iterator (void *cls,
614 const GNUNET_HashCode *key,
617 struct Plugin *plugin = cls;
618 struct HTTP_PeerContext * pc = value;
619 struct Session * ps = pc->head;
620 struct Session * tmp = NULL;
621 struct HTTP_Message * msg = NULL;
622 struct HTTP_Message * msg_tmp = NULL;
625 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
626 "Freeing context for peer `%s'\n",
627 GNUNET_i2s(&pc->identity));
629 GNUNET_CONTAINER_multihashmap_remove (plugin->peers, &pc->identity.hashPubKey, pc);
632 plugin->env->session_end(plugin, &pc->identity, ps);
635 GNUNET_free_non_null (ps->addr);
636 GNUNET_free(ps->url);
637 if (ps->msgtok != NULL)
638 GNUNET_SERVER_mst_destroy (ps->msgtok);
640 msg = ps->pending_msgs_head;
647 if (ps->direction==OUTBOUND)
649 if (ps->send_endpoint!=NULL)
650 curl_easy_cleanup(ps->send_endpoint);
651 if (ps->recv_endpoint!=NULL)
652 curl_easy_cleanup(ps->recv_endpoint);
658 GNUNET_STATISTICS_update (plugin->env->stats,
659 gettext_noop ("# HTTP peers active"),
667 * Removes a session from the linked list of sessions
668 * @param pc peer context
670 * @param call_msg_cont GNUNET_YES to call pending message continuations, otherwise no
671 * @param call_msg_cont_result result to call message continuations with
672 * @return GNUNET_SYSERR if msg not found, GNUNET_OK on success
675 remove_session (struct HTTP_PeerContext * pc,
678 int call_msg_cont_result)
680 struct HTTP_Message * msg;
681 struct Plugin * plugin = ps->peercontext->plugin;
683 #if DEBUG_CONNECTIONS
684 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
685 "Connection %X: removing %s session %X with id %u\n",
687 (ps->direction == INBOUND)
692 plugin->env->session_end(plugin, &pc->identity, ps);
693 GNUNET_free_non_null (ps->addr);
694 GNUNET_SERVER_mst_destroy (ps->msgtok);
695 GNUNET_free(ps->url);
696 if (ps->direction==INBOUND)
698 if (ps->recv_endpoint != NULL)
700 curl_easy_cleanup(ps->recv_endpoint);
701 ps->recv_endpoint = NULL;
703 if (ps->send_endpoint != NULL)
705 curl_easy_cleanup(ps->send_endpoint);
706 ps->send_endpoint = NULL;
710 msg = ps->pending_msgs_head;
713 if ((call_msg_cont == GNUNET_YES) && (msg->transmit_cont!=NULL))
715 msg->transmit_cont (msg->transmit_cont_cls,
717 call_msg_cont_result);
719 GNUNET_CONTAINER_DLL_remove(ps->pending_msgs_head,
720 ps->pending_msgs_head,
723 msg = ps->pending_msgs_head;
726 GNUNET_CONTAINER_DLL_remove(pc->head,pc->tail,ps);
730 /* no sessions left remove peer */
734 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
735 "No sessions left for peer `%s', removing context\n",
736 GNUNET_i2s(&pc->identity));
738 remove_peer_context_Iterator(plugin, &pc->identity.hashPubKey, pc);
744 static int check_localaddress (const struct sockaddr *addr, socklen_t addrlen)
747 int local = GNUNET_NO;
748 int af = addr->sa_family;
753 uint32_t netmask = 0x7F000000;
754 uint32_t address = ntohl (((struct sockaddr_in *) addr)->sin_addr.s_addr);
755 res = (address >> 24) ^ (netmask >> 24);
761 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
762 "Checking IPv4 address `%s': %s\n", GNUNET_a2s (addr, addrlen), (local==GNUNET_YES) ? "local" : "global");
768 if (IN6_IS_ADDR_LOOPBACK (&((struct sockaddr_in6 *) addr)->sin6_addr) ||
769 IN6_IS_ADDR_LINKLOCAL (&((struct sockaddr_in6 *) addr)->sin6_addr))
774 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
775 "Checking IPv6 address `%s' : %s\n", GNUNET_a2s (addr, addrlen), (local==GNUNET_YES) ? "local" : "global");
784 * Add the IP of our network interface to the list of
785 * our external IP addresses.
787 * @param cls the 'struct Plugin*'
788 * @param name name of the interface
789 * @param isDefault do we think this may be our default interface
790 * @param addr address of the interface
791 * @param addrlen number of bytes in addr
792 * @return GNUNET_OK to continue iterating
795 process_interfaces (void *cls,
798 const struct sockaddr *addr, socklen_t addrlen)
800 struct Plugin *plugin = cls;
801 struct IPv4HttpAddress * t4;
802 struct IPv6HttpAddress * t6;
805 if (plugin->use_localaddresses == GNUNET_NO)
807 if (GNUNET_YES == check_localaddress (addr, addrlen))
810 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
812 "Not notifying transport of address `%s' (local address)\n",
813 GNUNET_a2s (addr, addrlen));
820 GNUNET_assert(cls !=NULL);
821 af = addr->sa_family;
822 if ((af == AF_INET) &&
823 (plugin->use_ipv4 == GNUNET_YES) &&
824 (plugin->bind6_address == NULL) ) {
826 struct in_addr bnd_cmp = ((struct sockaddr_in *) addr)->sin_addr;
827 t4 = GNUNET_malloc(sizeof(struct IPv4HttpAddress));
828 // Not skipping loopback addresses
831 t4->ipv4_addr = ((struct sockaddr_in *) addr)->sin_addr.s_addr;
832 t4->u_port = htons (plugin->port_inbound);
833 if (plugin->bind4_address != NULL) {
834 if (0 == memcmp(&plugin->bind4_address->sin_addr, &bnd_cmp, sizeof (struct in_addr)))
836 GNUNET_CONTAINER_DLL_insert(plugin->ipv4_addr_head,
837 plugin->ipv4_addr_tail,t4);
838 plugin->env->notify_address(plugin->env->cls,
840 t4, sizeof (struct IPv4HttpAddress));
848 GNUNET_CONTAINER_DLL_insert (plugin->ipv4_addr_head,
849 plugin->ipv4_addr_tail,
851 plugin->env->notify_address(plugin->env->cls,
853 t4, sizeof (struct IPv4HttpAddress));
857 if ((af == AF_INET6) &&
858 (plugin->use_ipv6 == GNUNET_YES) &&
859 (plugin->bind4_address == NULL) ) {
861 struct in6_addr bnd_cmp6 = ((struct sockaddr_in6 *) addr)->sin6_addr;
863 t6 = GNUNET_malloc(sizeof(struct IPv6HttpAddress));
864 GNUNET_assert(t6 != NULL);
866 if (plugin->bind6_address != NULL) {
867 if (0 == memcmp(&plugin->bind6_address->sin6_addr,
869 sizeof (struct in6_addr))) {
870 memcpy (&t6->ipv6_addr,
871 &((struct sockaddr_in6 *) addr)->sin6_addr,
872 sizeof (struct in6_addr));
873 t6->u6_port = htons (plugin->port_inbound);
874 plugin->env->notify_address(plugin->env->cls,
876 t6, sizeof (struct IPv6HttpAddress));
877 GNUNET_CONTAINER_DLL_insert(plugin->ipv6_addr_head,
878 plugin->ipv6_addr_tail,
885 memcpy (&t6->ipv6_addr,
886 &((struct sockaddr_in6 *) addr)->sin6_addr,
887 sizeof (struct in6_addr));
888 t6->u6_port = htons (plugin->port_inbound);
889 GNUNET_CONTAINER_DLL_insert(plugin->ipv6_addr_head,plugin->ipv6_addr_tail,t6);
891 plugin->env->notify_address(plugin->env->cls,
893 t6, sizeof (struct IPv6HttpAddress));
900 * External logging function for MHD
901 * @param arg arguments
902 * @param fmt format string
903 * @param ap list of arguments
906 mhd_logger (void * arg,
912 vsnprintf(text, sizeof(text), fmt, ap);
914 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
921 mhd_termination_cb (void *cls,
922 struct MHD_Connection * connection,
923 void **httpSessionCache)
925 struct Session * ps = *httpSessionCache;
928 struct HTTP_PeerContext * pc = ps->peercontext;
929 struct Plugin *plugin = cls;
931 GNUNET_assert (cls != NULL);
932 plugin->current_connections--;
934 if (connection==ps->recv_endpoint)
936 #if DEBUG_CONNECTIONS
937 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
938 "Connection %X: inbound connection from peer `%s' was terminated\n",
940 GNUNET_i2s(&pc->identity));
942 ps->recv_active = GNUNET_NO;
943 ps->recv_connected = GNUNET_NO;
944 ps->recv_endpoint = NULL;
946 if (connection==ps->send_endpoint)
948 ps->send_active = GNUNET_NO;
949 ps->send_connected = GNUNET_NO;
950 ps->send_endpoint = NULL;
951 #if DEBUG_CONNECTIONS
952 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
953 "Connection %X: outbound connection from peer `%s' was terminated\n",
955 GNUNET_i2s(&pc->identity));
959 /* if both connections disconnected, remove session */
960 if ( (ps->send_connected == GNUNET_NO) &&
961 (ps->recv_connected == GNUNET_NO) )
963 GNUNET_STATISTICS_update (pc->plugin->env->stats,
964 gettext_noop ("# HTTP inbound sessions for peers active"),
967 remove_session(pc,ps,GNUNET_YES,GNUNET_SYSERR);
973 * Callback called by MessageStreamTokenizer when a message has arrived
974 * @param cls current session as closure
975 * @param client clien
976 * @param message the message to be forwarded to transport service
979 mhd_write_mst_cb (void *cls,
981 const struct GNUNET_MessageHeader *message)
983 struct Session *ps = cls;
984 struct HTTP_PeerContext *pc = ps->peercontext;
985 struct GNUNET_TIME_Relative delay;
987 GNUNET_assert(ps != NULL);
988 GNUNET_assert(pc != NULL);
990 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
991 "Connection %X: Forwarding message to transport service, type %u and size %u from `%s' (`%s')\n",
993 ntohs(message->type),
994 ntohs(message->size),
995 GNUNET_i2s(&(ps->peercontext)->identity),
996 http_plugin_address_to_string(NULL,ps->addr,ps->addrlen));
998 struct GNUNET_TRANSPORT_ATS_Information distance[2];
999 distance[0].type = htonl (GNUNET_TRANSPORT_ATS_QUALITY_NET_DISTANCE);
1000 distance[0].value = htonl (1);
1001 distance[1].type = htonl (GNUNET_TRANSPORT_ATS_ARRAY_TERMINATOR);
1002 distance[1].value = htonl (0);
1003 delay = pc->plugin->env->receive (ps->peercontext->plugin->env->cls,
1006 (const struct GNUNET_TRANSPORT_ATS_Information *) &distance,
1012 if (pc->reset_task != GNUNET_SCHEDULER_NO_TASK)
1013 GNUNET_SCHEDULER_cancel (pc->reset_task);
1015 if (delay.rel_value > 0)
1018 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1019 "Connection %X: Inbound quota management: delay next read for %llu ms \n",
1023 pc->reset_task = GNUNET_SCHEDULER_add_delayed (delay, &reset_inbound_quota_delay, pc);
1029 * Check if incoming connection is accepted.
1030 * NOTE: Here every connection is accepted
1031 * @param cls plugin as closure
1032 * @param addr address of incoming connection
1033 * @param addr_len address length of incoming connection
1034 * @return MHD_YES if connection is accepted, MHD_NO if connection is rejected
1038 mhd_accept_cb (void *cls,
1039 const struct sockaddr *addr,
1042 struct Plugin *plugin = cls;
1043 GNUNET_assert (cls != NULL);
1045 if (plugin->max_connect_per_transport > plugin->current_connections)
1047 plugin->current_connections ++;
1055 * Callback called by MHD when it needs data to send
1056 * @param cls current session
1057 * @param pos position in buffer
1058 * @param buf the buffer to write data to
1059 * @param max max number of bytes available in buffer
1060 * @return bytes written to buffer
1063 mhd_send_callback (void *cls, uint64_t pos, char *buf, size_t max)
1065 struct Session * ps = cls;
1066 struct HTTP_PeerContext * pc;
1067 struct HTTP_Message * msg;
1070 GNUNET_assert (ps!=NULL);
1072 pc = ps->peercontext;
1073 msg = ps->pending_msgs_tail;
1074 if (ps->send_force_disconnect==GNUNET_YES)
1076 #if DEBUG_CONNECTIONS
1077 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1078 "Connection %X: outbound forced to disconnect\n",
1087 if ((msg->size-msg->pos) <= max)
1089 memcpy(buf,&msg->buf[msg->pos],(msg->size-msg->pos));
1090 bytes_read = msg->size-msg->pos;
1091 msg->pos+=(msg->size-msg->pos);
1095 memcpy(buf,&msg->buf[msg->pos],max);
1100 /* removing message */
1101 if (msg->pos==msg->size)
1103 if (NULL!=msg->transmit_cont)
1104 msg->transmit_cont (msg->transmit_cont_cls,&pc->identity,GNUNET_OK);
1105 ps->queue_length_cur -= msg->size;
1106 remove_http_message(ps,msg);
1109 #if DEBUG_CONNECTIONS
1110 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1111 "Connection %X: MHD has sent %u bytes\n",
1120 * Process GET or PUT request received via MHD. For
1121 * GET, queue response that will send back our pending
1122 * messages. For PUT, process incoming data and send
1123 * to GNUnet core. In either case, check if a session
1124 * already exists and create a new one if not.
1127 mhd_access_cb (void *cls,
1128 struct MHD_Connection *mhd_connection,
1131 const char *version,
1132 const char *upload_data,
1133 size_t * upload_data_size,
1134 void **httpSessionCache)
1136 struct Plugin *plugin = cls;
1137 struct MHD_Response *response;
1138 const union MHD_ConnectionInfo * conn_info;
1139 const struct sockaddr *client_addr;
1140 const struct sockaddr_in *addrin;
1141 const struct sockaddr_in6 *addrin6;
1142 char address[INET6_ADDRSTRLEN+14];
1143 struct GNUNET_PeerIdentity pi_in;
1145 struct IPv4HttpAddress ipv4addr;
1146 struct IPv6HttpAddress ipv6addr;
1147 struct HTTP_PeerContext *pc = NULL;
1148 struct Session *ps = NULL;
1149 struct Session *ps_tmp = NULL;
1150 int res = GNUNET_NO;
1152 size_t addr_len = 0 ;
1154 GNUNET_assert(cls !=NULL);
1156 if (NULL == *httpSessionCache)
1158 /* check url for peer identity , if invalid send HTTP 404*/
1159 size_t len = strlen(&url[1]);
1160 char * peer = GNUNET_malloc(104+1);
1162 if ( (len>104) && (url[104]==';'))
1164 char * id = GNUNET_malloc((len-104)+1);
1165 strcpy(id,&url[105]);
1166 memcpy(peer,&url[1],103);
1168 id_num = strtoul ( id, NULL , 10);
1171 res = GNUNET_CRYPTO_hash_from_string (peer, &(pi_in.hashPubKey));
1173 if ( GNUNET_SYSERR == res )
1175 response = MHD_create_response_from_data (strlen (HTTP_ERROR_RESPONSE),
1176 HTTP_ERROR_RESPONSE,
1178 res = MHD_queue_response (mhd_connection, MHD_HTTP_NOT_FOUND, response);
1179 MHD_destroy_response (response);
1180 #if DEBUG_CONNECTIONS
1182 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1183 "Peer has no valid ident, sent HTTP 1.1/404\n");
1185 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1186 "Peer has no valid ident, could not send error\n");
1193 ps = *httpSessionCache;
1194 pc = ps->peercontext;
1197 if (NULL == *httpSessionCache)
1199 /* get peer context */
1200 pc = GNUNET_CONTAINER_multihashmap_get (plugin->peers, &pi_in.hashPubKey);
1204 pc = GNUNET_malloc(sizeof (struct HTTP_PeerContext));
1205 pc->plugin = plugin;
1206 pc->session_id_counter=1;
1207 pc->last_session = NULL;
1208 memcpy(&pc->identity, &pi_in, sizeof(struct GNUNET_PeerIdentity));
1209 GNUNET_CONTAINER_multihashmap_put(plugin->peers,
1210 &pc->identity.hashPubKey,
1212 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
1213 GNUNET_STATISTICS_update (plugin->env->stats,
1214 gettext_noop ("# HTTP peers active"),
1219 conn_info = MHD_get_connection_info(mhd_connection, MHD_CONNECTION_INFO_CLIENT_ADDRESS );
1220 /* Incoming IPv4 connection */
1221 /* cast required for legacy MHD API < 0.9.6 */
1222 client_addr = (const struct sockaddr *) conn_info->client_addr;
1223 if ( AF_INET == client_addr->sa_family)
1225 addrin = (const struct sockaddr_in*) client_addr;
1226 inet_ntop(addrin->sin_family, &(addrin->sin_addr),address,INET_ADDRSTRLEN);
1227 memcpy(&ipv4addr.ipv4_addr,&(addrin->sin_addr),sizeof(struct in_addr));
1228 ipv4addr.u_port = addrin->sin_port;
1230 addr_len = sizeof(struct IPv4HttpAddress);
1232 /* Incoming IPv6 connection */
1233 if ( AF_INET6 == client_addr->sa_family)
1235 addrin6 = (const struct sockaddr_in6 *) client_addr;
1236 inet_ntop(addrin6->sin6_family, &(addrin6->sin6_addr),address,INET6_ADDRSTRLEN);
1237 memcpy(&ipv6addr.ipv6_addr,&(addrin6->sin6_addr),sizeof(struct in6_addr));
1238 ipv6addr.u6_port = addrin6->sin6_port;
1240 addr_len = sizeof(struct IPv6HttpAddress);
1243 GNUNET_assert (addr != NULL);
1244 GNUNET_assert (addr_len != 0);
1247 /* only inbound sessions here */
1250 while (ps_tmp!=NULL)
1252 if ((ps_tmp->direction==INBOUND) && (ps_tmp->session_id == id_num) && (id_num!=0))
1254 if ((ps_tmp->recv_force_disconnect!=GNUNET_YES) && (ps_tmp->send_force_disconnect!=GNUNET_YES))
1258 ps_tmp=ps_tmp->next;
1263 ps = GNUNET_malloc(sizeof (struct Session));
1264 ps->addr = GNUNET_malloc(addr_len);
1265 memcpy(ps->addr,addr,addr_len);
1266 ps->addrlen = addr_len;
1267 ps->direction=INBOUND;
1268 ps->pending_msgs_head = NULL;
1269 ps->pending_msgs_tail = NULL;
1270 ps->send_connected=GNUNET_NO;
1271 ps->send_active=GNUNET_NO;
1272 ps->recv_connected=GNUNET_NO;
1273 ps->recv_active=GNUNET_NO;
1275 ps->session_id =id_num;
1276 ps->queue_length_cur = 0;
1277 ps->queue_length_max = GNUNET_SERVER_MAX_MESSAGE_SIZE;
1278 ps->url = create_url (plugin, ps->addr, ps->addrlen, ps->session_id);
1279 GNUNET_CONTAINER_DLL_insert(pc->head,pc->tail,ps);
1280 GNUNET_STATISTICS_update (plugin->env->stats,
1281 gettext_noop ("# HTTP inbound sessions for peers active"),
1286 *httpSessionCache = ps;
1287 if (ps->msgtok==NULL)
1288 ps->msgtok = GNUNET_SERVER_mst_create (&mhd_write_mst_cb, ps);
1290 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1291 "Connection %X: HTTP Daemon has new an incoming `%s' request from peer `%s' (`%s')\n",
1294 GNUNET_i2s(&pc->identity),
1295 http_plugin_address_to_string(NULL, ps->addr, ps->addrlen));
1299 /* Is it a PUT or a GET request */
1300 if (0 == strcmp (MHD_HTTP_METHOD_PUT, method))
1302 if (ps->recv_force_disconnect == GNUNET_YES)
1304 #if DEBUG_CONNECTIONS
1305 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1306 "Connection %X: inbound connection was forced to disconnect\n",ps);
1308 ps->recv_active = GNUNET_NO;
1311 if ((*upload_data_size == 0) && (ps->recv_active==GNUNET_NO))
1313 ps->recv_endpoint = mhd_connection;
1314 ps->recv_connected = GNUNET_YES;
1315 ps->recv_active = GNUNET_YES;
1316 ps->recv_force_disconnect = GNUNET_NO;
1317 #if DEBUG_CONNECTIONS
1318 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1319 "Connection %X: inbound PUT connection connected\n",ps);
1324 /* Transmission of all data complete */
1325 if ((*upload_data_size == 0) && (ps->recv_active == GNUNET_YES))
1327 response = MHD_create_response_from_data (strlen (HTTP_PUT_RESPONSE),
1330 res = MHD_queue_response (mhd_connection, MHD_HTTP_OK, response);
1331 #if DEBUG_CONNECTIONS
1332 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1333 "Connection %X: Sent HTTP/1.1: 200 OK as PUT Response\n",
1336 MHD_destroy_response (response);
1337 ps->recv_active=GNUNET_NO;
1341 /* Recieving data */
1342 if ((*upload_data_size > 0) && (ps->recv_active == GNUNET_YES))
1344 if (pc->delay.rel_value == 0)
1347 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1348 "Connection %X: PUT with %u bytes forwarded to MST\n",
1349 ps, *upload_data_size);
1351 res = GNUNET_SERVER_mst_receive(ps->msgtok, ps,
1352 upload_data, *upload_data_size,
1353 GNUNET_NO, GNUNET_NO);
1354 (*upload_data_size) = 0;
1359 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1360 "Connection %X: no inbound bandwidth available! Next read was delayed for %llu ms\n",
1362 ps->peercontext->delay.rel_value);
1370 if ( 0 == strcmp (MHD_HTTP_METHOD_GET, method) )
1372 if (ps->send_force_disconnect == GNUNET_YES)
1374 #if DEBUG_CONNECTIONS
1375 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1376 "Connection %X: outbound connection was forced to disconnect\n",
1379 ps->send_active = GNUNET_NO;
1382 ps->send_connected = GNUNET_YES;
1383 ps->send_active = GNUNET_YES;
1384 ps->send_endpoint = mhd_connection;
1385 ps->send_force_disconnect = GNUNET_NO;
1386 #if DEBUG_CONNECTIONS
1387 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1388 "Connection %X: inbound GET connection connected\n",
1391 response = MHD_create_response_from_callback(-1,32 * 1024, &mhd_send_callback, ps, NULL);
1392 res = MHD_queue_response (mhd_connection, MHD_HTTP_OK, response);
1393 MHD_destroy_response (response);
1401 * Function that queries MHD's select sets and
1402 * starts the task waiting for them.
1403 * @param plugin plugin
1404 * @param daemon_handle the MHD daemon handle
1405 * @return gnunet task identifier
1407 static GNUNET_SCHEDULER_TaskIdentifier
1408 http_server_daemon_prepare (struct Plugin *plugin,
1409 struct MHD_Daemon *daemon_handle)
1411 GNUNET_SCHEDULER_TaskIdentifier ret;
1415 struct GNUNET_NETWORK_FDSet *wrs;
1416 struct GNUNET_NETWORK_FDSet *wws;
1417 struct GNUNET_NETWORK_FDSet *wes;
1419 unsigned long long timeout;
1421 struct GNUNET_TIME_Relative tv;
1423 ret = GNUNET_SCHEDULER_NO_TASK;
1427 wrs = GNUNET_NETWORK_fdset_create ();
1428 wes = GNUNET_NETWORK_fdset_create ();
1429 wws = GNUNET_NETWORK_fdset_create ();
1431 GNUNET_assert (MHD_YES ==
1432 MHD_get_fdset (daemon_handle,
1437 haveto = MHD_get_timeout (daemon_handle, &timeout);
1438 if (haveto == MHD_YES)
1439 tv.rel_value = (uint64_t) timeout;
1441 tv = GNUNET_TIME_UNIT_SECONDS;
1442 GNUNET_NETWORK_fdset_copy_native (wrs, &rs, max + 1);
1443 GNUNET_NETWORK_fdset_copy_native (wws, &ws, max + 1);
1444 GNUNET_NETWORK_fdset_copy_native (wes, &es, max + 1);
1445 if (daemon_handle == plugin->http_server_daemon_v4)
1447 if (plugin->http_server_task_v4 != GNUNET_SCHEDULER_NO_TASK)
1449 GNUNET_SCHEDULER_cancel(plugin->http_server_task_v4);
1450 plugin->http_server_daemon_v4 = GNUNET_SCHEDULER_NO_TASK;
1453 ret = GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1454 GNUNET_SCHEDULER_NO_TASK,
1458 &http_server_daemon_v4_run,
1461 if (daemon_handle == plugin->http_server_daemon_v6)
1463 if (plugin->http_server_task_v6 != GNUNET_SCHEDULER_NO_TASK)
1465 GNUNET_SCHEDULER_cancel(plugin->http_server_task_v6);
1466 plugin->http_server_task_v6 = GNUNET_SCHEDULER_NO_TASK;
1469 ret = GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1470 GNUNET_SCHEDULER_NO_TASK,
1474 &http_server_daemon_v6_run,
1477 GNUNET_NETWORK_fdset_destroy (wrs);
1478 GNUNET_NETWORK_fdset_destroy (wws);
1479 GNUNET_NETWORK_fdset_destroy (wes);
1485 * Call MHD IPv4 to process pending requests and then go back
1486 * and schedule the next run.
1487 * @param cls plugin as closure
1488 * @param tc task context
1491 http_server_daemon_v4_run (void *cls,
1492 const struct GNUNET_SCHEDULER_TaskContext *tc)
1494 struct Plugin *plugin = cls;
1496 #if DEBUG_SCHEDULING
1497 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_READ_READY))
1498 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1499 "http_server_daemon_v4_run: GNUNET_SCHEDULER_REASON_READ_READY\n");
1500 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_WRITE_READY))
1501 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1502 "http_server_daemon_v4_run: GNUNET_SCHEDULER_REASON_WRITE_READY\n");
1503 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_TIMEOUT))
1504 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1505 "http_server_daemon_v4_run: GNUNET_SCHEDULER_REASON_TIMEOUT\n");
1506 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_STARTUP))
1507 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1508 "http_server_daemon_v4_run: GGNUNET_SCHEDULER_REASON_STARTUP\n");
1509 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1510 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1511 "http_server_daemon_v4_run: GGNUNET_SCHEDULER_REASON_SHUTDOWN\n");
1514 GNUNET_assert(cls !=NULL);
1515 plugin->http_server_task_v4 = GNUNET_SCHEDULER_NO_TASK;
1517 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1520 GNUNET_assert (MHD_YES == MHD_run (plugin->http_server_daemon_v4));
1521 plugin->http_server_task_v4 = http_server_daemon_prepare (plugin, plugin->http_server_daemon_v4);
1526 * Call MHD IPv6 to process pending requests and then go back
1527 * and schedule the next run.
1528 * @param cls plugin as closure
1529 * @param tc task context
1532 http_server_daemon_v6_run (void *cls,
1533 const struct GNUNET_SCHEDULER_TaskContext *tc)
1535 struct Plugin *plugin = cls;
1537 #if DEBUG_SCHEDULING
1538 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_READ_READY))
1539 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1540 "http_server_daemon_v6_run: GNUNET_SCHEDULER_REASON_READ_READY\n");
1541 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_WRITE_READY))
1542 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1543 "http_server_daemon_v6_run: GNUNET_SCHEDULER_REASON_WRITE_READY\n");
1544 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_TIMEOUT))
1545 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1546 "http_server_daemon_v6_run: GNUNET_SCHEDULER_REASON_TIMEOUT\n");
1547 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_STARTUP))
1548 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1549 "http_server_daemon_v6_run: GGNUNET_SCHEDULER_REASON_STARTUP\n");
1550 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1551 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1552 "http_server_daemon_v6_run: GGNUNET_SCHEDULER_REASON_SHUTDOWN\n");
1555 GNUNET_assert(cls !=NULL);
1556 plugin->http_server_task_v6 = GNUNET_SCHEDULER_NO_TASK;
1558 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1561 GNUNET_assert (MHD_YES == MHD_run (plugin->http_server_daemon_v6));
1562 plugin->http_server_task_v6 = http_server_daemon_prepare (plugin, plugin->http_server_daemon_v6);
1567 curl_get_header_cb( void *ptr,
1568 size_t size, size_t nmemb,
1571 struct Session * ps = stream;
1573 long http_result = 0;
1575 /* Getting last http result code */
1576 GNUNET_assert(NULL!=ps);
1577 if (ps->recv_connected==GNUNET_NO)
1579 res = curl_easy_getinfo(ps->recv_endpoint, CURLINFO_RESPONSE_CODE, &http_result);
1580 if (CURLE_OK == res)
1582 if (http_result == 200)
1584 ps->recv_connected = GNUNET_YES;
1585 ps->recv_active = GNUNET_YES;
1586 #if DEBUG_CONNECTIONS
1587 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: connected to recieve data\n",ps);
1589 // Calling send_check_connections again since receive is established
1590 send_check_connections (ps->peercontext->plugin, ps);
1597 size_t len = size * nmemb;
1599 if ((size * nmemb) < SIZE_MAX)
1600 tmp = GNUNET_malloc (len+1);
1602 if ((tmp != NULL) && (len > 0))
1604 memcpy(tmp,ptr,len);
1607 if (tmp[len-2] == 13)
1610 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1611 "Connection %X: Header: %s\n",
1614 GNUNET_free_non_null (tmp);
1617 return size * nmemb;
1622 * Callback called by libcurl when new headers arrive
1623 * Used to get HTTP result for curl operations
1624 * @param ptr stream to read from
1625 * @param size size of one char element
1626 * @param nmemb number of char elements
1627 * @param stream closure set by user
1628 * @return bytes read by function
1631 curl_put_header_cb(void *ptr,
1636 struct Session * ps = stream;
1639 size_t len = size * nmemb;
1640 long http_result = 0;
1643 /* Getting last http result code */
1644 GNUNET_assert(NULL!=ps);
1645 res = curl_easy_getinfo (ps->send_endpoint, CURLINFO_RESPONSE_CODE, &http_result);
1646 if (CURLE_OK == res)
1648 if ((http_result == 100) && (ps->send_connected==GNUNET_NO))
1650 ps->send_connected = GNUNET_YES;
1651 ps->send_active = GNUNET_YES;
1652 #if DEBUG_CONNECTIONS
1653 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1654 "Connection %X: connected to send data\n",
1658 if ((http_result == 200) && (ps->send_connected==GNUNET_YES))
1660 ps->send_connected = GNUNET_NO;
1661 ps->send_active = GNUNET_NO;
1662 #if DEBUG_CONNECTIONS
1663 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1664 "Connection %X: sending disconnected\n",
1671 if ((size * nmemb) < SIZE_MAX)
1672 tmp = GNUNET_malloc (len+1);
1674 if ((tmp != NULL) && (len > 0))
1676 memcpy(tmp,ptr,len);
1679 if (tmp[len-2] == 13)
1683 GNUNET_free_non_null (tmp);
1684 return size * nmemb;
1688 * Callback method used with libcurl
1689 * Method is called when libcurl needs to read data during sending
1690 * @param stream pointer where to write data
1691 * @param size size of an individual element
1692 * @param nmemb count of elements that can be written to the buffer
1693 * @param ptr source pointer, passed to the libcurl handle
1694 * @return bytes written to stream
1697 curl_send_cb(void *stream,
1698 size_t size, size_t nmemb,
1701 struct Session * ps = ptr;
1702 struct HTTP_Message * msg = ps->pending_msgs_tail;
1706 if (ps->send_active == GNUNET_NO)
1707 return CURL_READFUNC_PAUSE;
1708 if ( (ps->pending_msgs_tail == NULL) &&
1709 (ps->send_active == GNUNET_YES) )
1711 #if DEBUG_CONNECTIONS
1712 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1713 "Connection %X: No Message to send, pausing connection\n",
1716 ps->send_active = GNUNET_NO;
1717 return CURL_READFUNC_PAUSE;
1720 GNUNET_assert (msg!=NULL);
1723 if (msg->pos < msg->size)
1725 /* data fit in buffer */
1726 if ((msg->size - msg->pos) <= (size * nmemb))
1728 len = (msg->size - msg->pos);
1729 memcpy(stream, &msg->buf[msg->pos], len);
1736 memcpy(stream, &msg->buf[msg->pos], len);
1741 /* no data to send */
1747 if ( msg->pos == msg->size)
1749 #if DEBUG_CONNECTIONS
1750 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1751 "Connection %X: Message with %u bytes sent, removing message from queue\n",
1755 /* Calling transmit continuation */
1756 if (NULL != ps->pending_msgs_tail->transmit_cont)
1757 msg->transmit_cont (ps->pending_msgs_tail->transmit_cont_cls,
1758 &(ps->peercontext)->identity,
1760 ps->queue_length_cur -= msg->size;
1761 remove_http_message(ps, msg);
1768 curl_receive_mst_cb (void *cls,
1770 const struct GNUNET_MessageHeader *message)
1772 struct Session *ps = cls;
1773 struct GNUNET_TIME_Relative delay;
1774 GNUNET_assert(ps != NULL);
1776 struct HTTP_PeerContext *pc = ps->peercontext;
1777 GNUNET_assert(pc != NULL);
1779 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1780 "Connection %X: Forwarding message to transport service, type %u and size %u from `%s' (`%s')\n",
1782 ntohs(message->type),
1783 ntohs(message->size),
1784 GNUNET_i2s(&(pc->identity)),
1785 http_plugin_address_to_string(NULL,ps->addr,ps->addrlen));
1787 struct GNUNET_TRANSPORT_ATS_Information distance[2];
1788 distance[0].type = htonl (GNUNET_TRANSPORT_ATS_QUALITY_NET_DISTANCE);
1789 distance[0].value = htonl (1);
1790 distance[1].type = htonl (GNUNET_TRANSPORT_ATS_ARRAY_TERMINATOR);
1791 distance[1].value = htonl (0);
1793 delay = pc->plugin->env->receive (pc->plugin->env->cls,
1796 (const struct GNUNET_TRANSPORT_ATS_Information *) &distance, 2,
1801 if (pc->reset_task != GNUNET_SCHEDULER_NO_TASK)
1802 GNUNET_SCHEDULER_cancel (pc->reset_task);
1804 if (delay.rel_value > 0)
1807 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1808 "Connection %X: Inbound quota management: delay next read for %llu ms\n",
1809 ps, delay.rel_value);
1811 pc->reset_task = GNUNET_SCHEDULER_add_delayed (delay, &reset_inbound_quota_delay, pc);
1817 * Callback method used with libcurl
1818 * Method is called when libcurl needs to write data during sending
1819 * @param stream pointer where to write data
1820 * @param size size of an individual element
1821 * @param nmemb count of elements that can be written to the buffer
1822 * @param ptr destination pointer, passed to the libcurl handle
1823 * @return bytes read from stream
1826 curl_receive_cb( void *stream, size_t size, size_t nmemb, void *ptr)
1828 struct Session * ps = ptr;
1830 if (ps->peercontext->delay.rel_value > 0)
1833 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1834 "Connection %X: no inbound bandwidth available! Next read was delayed for %llu ms\n",
1835 ps, ps->peercontext->delay.rel_value);
1839 #if DEBUG_CONNECTIONS
1840 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1841 "Connection %X: %u bytes received\n",
1844 GNUNET_SERVER_mst_receive(ps->msgtok, ps,
1846 GNUNET_NO, GNUNET_NO);
1847 return (size * nmemb);
1852 curl_handle_finished (struct Plugin *plugin)
1854 struct Session *ps = NULL;
1855 struct HTTP_PeerContext *pc = NULL;
1856 struct CURLMsg *msg;
1857 struct HTTP_Message * cur_msg = NULL;
1864 msg = curl_multi_info_read (plugin->multi_handle, &msgs_in_queue);
1865 if ((msgs_in_queue == 0) || (msg == NULL))
1867 /* get session for affected curl handle */
1868 GNUNET_assert ( msg->easy_handle != NULL );
1869 curl_easy_getinfo(msg->easy_handle, CURLINFO_PRIVATE, &tmp);
1870 ps = (struct Session *) tmp;
1871 GNUNET_assert ( ps != NULL );
1872 pc = ps->peercontext;
1873 GNUNET_assert ( pc != NULL );
1877 if ( (msg->data.result != CURLE_OK) &&
1878 (msg->data.result != CURLE_GOT_NOTHING) )
1880 /* sending msg failed*/
1881 if (msg->easy_handle == ps->send_endpoint)
1883 #if DEBUG_CONNECTIONS
1884 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1885 _("Connection %X: HTTP PUT to peer `%s' (`%s') failed: `%s' `%s'\n"),
1887 GNUNET_i2s(&pc->identity),
1888 http_plugin_address_to_string(NULL, ps->addr, ps->addrlen),
1889 "curl_multi_perform",
1890 curl_easy_strerror (msg->data.result));
1892 ps->send_connected = GNUNET_NO;
1893 ps->send_active = GNUNET_NO;
1894 curl_multi_remove_handle(plugin->multi_handle,ps->send_endpoint);
1895 while (ps->pending_msgs_tail != NULL)
1897 cur_msg = ps->pending_msgs_tail;
1898 if ( NULL != cur_msg->transmit_cont)
1899 cur_msg->transmit_cont (cur_msg->transmit_cont_cls,&pc->identity,GNUNET_SYSERR);
1900 ps->queue_length_cur -= cur_msg->size;
1901 remove_http_message(ps,cur_msg);
1904 /* GET connection failed */
1905 if (msg->easy_handle == ps->recv_endpoint)
1907 #if DEBUG_CONNECTIONS
1908 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1909 _("Connection %X: HTTP GET to peer `%s' (`%s') failed: `%s' `%s'\n"),
1911 GNUNET_i2s(&pc->identity),
1912 http_plugin_address_to_string(NULL, ps->addr, ps->addrlen),
1913 "curl_multi_perform",
1914 curl_easy_strerror (msg->data.result));
1916 ps->recv_connected = GNUNET_NO;
1917 ps->recv_active = GNUNET_NO;
1918 curl_multi_remove_handle(plugin->multi_handle,ps->recv_endpoint);
1923 if (msg->easy_handle == ps->send_endpoint)
1925 GNUNET_assert (CURLE_OK == curl_easy_getinfo(msg->easy_handle, CURLINFO_RESPONSE_CODE, &http_result));
1926 #if DEBUG_CONNECTIONS
1927 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1928 "Connection %X: HTTP PUT connection to peer `%s' (`%s') was closed with HTTP code %u\n",
1930 GNUNET_i2s(&pc->identity),
1931 http_plugin_address_to_string(NULL, ps->addr, ps->addrlen),
1934 /* Calling transmit continuation */
1935 while (ps->pending_msgs_tail != NULL)
1937 cur_msg = ps->pending_msgs_tail;
1938 if ( NULL != cur_msg->transmit_cont)
1940 /* HTTP 1xx : Last message before here was informational */
1941 if ((http_result >=100) && (http_result < 200))
1942 cur_msg->transmit_cont (cur_msg->transmit_cont_cls,&pc->identity,GNUNET_OK);
1943 /* HTTP 2xx: successful operations */
1944 if ((http_result >=200) && (http_result < 300))
1945 cur_msg->transmit_cont (cur_msg->transmit_cont_cls,&pc->identity,GNUNET_OK);
1946 /* HTTP 3xx..5xx: error */
1947 if ((http_result >=300) && (http_result < 600))
1948 cur_msg->transmit_cont (cur_msg->transmit_cont_cls,&pc->identity,GNUNET_SYSERR);
1950 ps->queue_length_cur -= cur_msg->size;
1951 remove_http_message(ps,cur_msg);
1954 ps->send_connected = GNUNET_NO;
1955 ps->send_active = GNUNET_NO;
1956 curl_multi_remove_handle(plugin->multi_handle,ps->send_endpoint);
1958 if (msg->easy_handle == ps->recv_endpoint)
1960 #if DEBUG_CONNECTIONS
1961 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1962 "Connection %X: HTTP GET connection to peer `%s' (`%s') was closed with HTTP code %u\n",
1964 GNUNET_i2s(&pc->identity),
1965 http_plugin_address_to_string(NULL, ps->addr, ps->addrlen),
1968 ps->recv_connected = GNUNET_NO;
1969 ps->recv_active = GNUNET_NO;
1970 curl_multi_remove_handle(plugin->multi_handle,ps->recv_endpoint);
1972 plugin->current_connections--;
1974 if ((ps->recv_connected == GNUNET_NO) && (ps->send_connected == GNUNET_NO))
1975 remove_session (pc, ps, GNUNET_YES, GNUNET_SYSERR);
1981 while ( (msgs_in_queue > 0) );
1986 * Task performing curl operations
1987 * @param cls plugin as closure
1988 * @param tc gnunet scheduler task context
1990 static void curl_perform (void *cls,
1991 const struct GNUNET_SCHEDULER_TaskContext *tc)
1993 struct Plugin *plugin = cls;
1994 static unsigned int handles_last_run;
1998 GNUNET_assert(cls !=NULL);
2000 plugin->http_curl_task = GNUNET_SCHEDULER_NO_TASK;
2001 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
2006 mret = curl_multi_perform (plugin->multi_handle, &running);
2007 if ((running < handles_last_run) && (running>0))
2008 curl_handle_finished(plugin);
2009 handles_last_run = running;
2011 while (mret == CURLM_CALL_MULTI_PERFORM);
2012 curl_schedule(plugin);
2017 * Function setting up file descriptors and scheduling task to run
2019 * @param plugin plugin as closure
2020 * @return GNUNET_SYSERR for hard failure, GNUNET_OK for ok
2023 curl_schedule(struct Plugin *plugin)
2029 struct GNUNET_NETWORK_FDSet *grs;
2030 struct GNUNET_NETWORK_FDSet *gws;
2034 /* Cancel previous scheduled task */
2035 if (plugin->http_curl_task != GNUNET_SCHEDULER_NO_TASK)
2037 GNUNET_SCHEDULER_cancel(plugin->http_curl_task);
2038 plugin->http_curl_task = GNUNET_SCHEDULER_NO_TASK;
2045 mret = curl_multi_fdset (plugin->multi_handle, &rs, &ws, &es, &max);
2046 if (mret != CURLM_OK)
2048 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2049 _("%s failed at %s:%d: `%s'\n"),
2050 "curl_multi_fdset", __FILE__, __LINE__,
2051 curl_multi_strerror (mret));
2052 return GNUNET_SYSERR;
2054 mret = curl_multi_timeout (plugin->multi_handle, &to);
2055 if (mret != CURLM_OK)
2057 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2058 _("%s failed at %s:%d: `%s'\n"),
2059 "curl_multi_timeout", __FILE__, __LINE__,
2060 curl_multi_strerror (mret));
2061 return GNUNET_SYSERR;
2064 grs = GNUNET_NETWORK_fdset_create ();
2065 gws = GNUNET_NETWORK_fdset_create ();
2066 GNUNET_NETWORK_fdset_copy_native (grs, &rs, max + 1);
2067 GNUNET_NETWORK_fdset_copy_native (gws, &ws, max + 1);
2068 plugin->http_curl_task = GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
2069 GNUNET_SCHEDULER_NO_TASK,
2071 ? GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
2072 : GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, to),
2077 GNUNET_NETWORK_fdset_destroy (gws);
2078 GNUNET_NETWORK_fdset_destroy (grs);
2085 * Function to log curl debug messages with GNUNET_log
2086 * @param curl handle
2087 * @param type curl_infotype
2090 * @param cls closure
2094 curl_logger (CURL * curl,
2096 char * data, size_t size,
2099 if (type == CURLINFO_TEXT)
2102 memcpy(text,data,size);
2103 if (text[size-1] == '\n')
2108 text[size+1] = '\0';
2110 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2111 "CURL: Connection %X - %s",
2121 * Function setting up curl handle and selecting message to send
2123 * @param plugin plugin
2125 * @return GNUNET_SYSERR on failure, GNUNET_NO if connecting, GNUNET_YES if ok
2128 send_check_connections (struct Plugin *plugin,
2132 struct GNUNET_TIME_Relative timeout = GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT;
2134 if ((ps->direction == OUTBOUND) && (plugin->current_connections < plugin->max_connect_per_transport))
2136 /* RECV DIRECTION */
2137 /* Check if session is connected to receive data, otherwise connect to peer */
2139 if (ps->recv_connected == GNUNET_NO)
2141 int fresh = GNUNET_NO;
2142 if (ps->recv_endpoint == NULL)
2145 ps->recv_endpoint = curl_easy_init();
2148 curl_easy_setopt(ps->recv_endpoint, CURLOPT_VERBOSE, 1L);
2149 curl_easy_setopt(ps->recv_endpoint, CURLOPT_DEBUGFUNCTION , &curl_logger);
2150 curl_easy_setopt(ps->recv_endpoint, CURLOPT_DEBUGDATA , ps->recv_endpoint);
2153 curl_easy_setopt(ps->recv_endpoint, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1);
2154 curl_easy_setopt(ps->recv_endpoint, CURLOPT_SSL_VERIFYPEER, 0);
2155 curl_easy_setopt(ps->recv_endpoint, CURLOPT_SSL_VERIFYHOST, 0);
2157 curl_easy_setopt(ps->recv_endpoint, CURLOPT_URL, ps->url);
2158 curl_easy_setopt(ps->recv_endpoint, CURLOPT_HEADERFUNCTION, &curl_get_header_cb);
2159 curl_easy_setopt(ps->recv_endpoint, CURLOPT_WRITEHEADER, ps);
2160 curl_easy_setopt(ps->recv_endpoint, CURLOPT_READFUNCTION, curl_send_cb);
2161 curl_easy_setopt(ps->recv_endpoint, CURLOPT_READDATA, ps);
2162 curl_easy_setopt(ps->recv_endpoint, CURLOPT_WRITEFUNCTION, curl_receive_cb);
2163 curl_easy_setopt(ps->recv_endpoint, CURLOPT_WRITEDATA, ps);
2164 curl_easy_setopt(ps->recv_endpoint, CURLOPT_TIMEOUT, (long) timeout.rel_value);
2165 curl_easy_setopt(ps->recv_endpoint, CURLOPT_PRIVATE, ps);
2166 curl_easy_setopt(ps->recv_endpoint, CURLOPT_CONNECTTIMEOUT, HTTP_CONNECT_TIMEOUT);
2167 curl_easy_setopt(ps->recv_endpoint, CURLOPT_BUFFERSIZE, 2*GNUNET_SERVER_MAX_MESSAGE_SIZE);
2168 #if CURL_TCP_NODELAY
2169 curl_easy_setopt(ps->recv_endpoint, CURLOPT_TCP_NODELAY, 1);
2171 if (fresh==GNUNET_YES)
2173 mret = curl_multi_add_handle(plugin->multi_handle, ps->recv_endpoint);
2174 if (mret != CURLM_OK)
2176 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2177 _("Connection: %X: %s failed at %s:%d: `%s'\n"),
2179 "curl_multi_add_handle", __FILE__, __LINE__,
2180 curl_multi_strerror (mret));
2181 return GNUNET_SYSERR;
2184 if (plugin->http_curl_task != GNUNET_SCHEDULER_NO_TASK)
2186 GNUNET_SCHEDULER_cancel(plugin->http_curl_task);
2187 plugin->http_curl_task = GNUNET_SCHEDULER_NO_TASK;
2189 plugin->current_connections ++;
2190 plugin->http_curl_task = GNUNET_SCHEDULER_add_now (&curl_perform, plugin);
2193 /* waiting for receive direction */
2194 if (ps->recv_connected==GNUNET_NO)
2197 /* SEND DIRECTION */
2198 /* Check if session is connected to send data, otherwise connect to peer */
2199 if ((ps->send_connected == GNUNET_YES) && (ps->send_endpoint!= NULL))
2201 if (ps->send_active == GNUNET_YES)
2203 #if DEBUG_CONNECTIONS
2204 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2205 "Connection %X: outbound active, enqueueing message\n",
2210 if (ps->send_active == GNUNET_NO)
2212 #if DEBUG_CONNECTIONS
2213 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2214 "Connection %X: outbound paused, unpausing existing connection and enqueueing message\n",
2217 if (CURLE_OK == curl_easy_pause(ps->send_endpoint,CURLPAUSE_CONT))
2219 ps->send_active=GNUNET_YES;
2220 if (plugin->http_curl_task != GNUNET_SCHEDULER_NO_TASK)
2222 GNUNET_SCHEDULER_cancel(plugin->http_curl_task);
2223 plugin->http_curl_task = GNUNET_SCHEDULER_NO_TASK;
2225 plugin->http_curl_task = GNUNET_SCHEDULER_add_now (&curl_perform, plugin);
2229 return GNUNET_SYSERR;
2232 /* not connected, initiate connection */
2233 if ((ps->send_connected==GNUNET_NO) && (plugin->current_connections < plugin->max_connect_per_transport))
2235 int fresh = GNUNET_NO;
2236 if (NULL == ps->send_endpoint)
2238 ps->send_endpoint = curl_easy_init();
2241 GNUNET_assert (ps->send_endpoint != NULL);
2242 GNUNET_assert (NULL != ps->pending_msgs_tail);
2243 #if DEBUG_CONNECTIONS
2244 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2245 "Connection %X: outbound not connected, initiating connection\n",
2248 ps->send_active = GNUNET_NO;
2251 curl_easy_setopt(ps->send_endpoint, CURLOPT_VERBOSE, 1L);
2252 curl_easy_setopt(ps->send_endpoint, CURLOPT_DEBUGFUNCTION , &curl_logger);
2253 curl_easy_setopt(ps->send_endpoint, CURLOPT_DEBUGDATA , ps->send_endpoint);
2256 curl_easy_setopt (ps->send_endpoint, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1);
2257 curl_easy_setopt(ps->send_endpoint, CURLOPT_SSL_VERIFYPEER, 0);
2258 curl_easy_setopt(ps->send_endpoint, CURLOPT_SSL_VERIFYHOST, 0);
2260 curl_easy_setopt(ps->send_endpoint, CURLOPT_URL, ps->url);
2261 curl_easy_setopt(ps->send_endpoint, CURLOPT_PUT, 1L);
2262 curl_easy_setopt(ps->send_endpoint, CURLOPT_HEADERFUNCTION, &curl_put_header_cb);
2263 curl_easy_setopt(ps->send_endpoint, CURLOPT_WRITEHEADER, ps);
2264 curl_easy_setopt(ps->send_endpoint, CURLOPT_READFUNCTION, curl_send_cb);
2265 curl_easy_setopt(ps->send_endpoint, CURLOPT_READDATA, ps);
2266 curl_easy_setopt(ps->send_endpoint, CURLOPT_WRITEFUNCTION, curl_receive_cb);
2267 curl_easy_setopt(ps->send_endpoint, CURLOPT_READDATA, ps);
2268 curl_easy_setopt(ps->send_endpoint, CURLOPT_TIMEOUT, (long) timeout.rel_value);
2269 curl_easy_setopt(ps->send_endpoint, CURLOPT_PRIVATE, ps);
2270 curl_easy_setopt(ps->send_endpoint, CURLOPT_CONNECTTIMEOUT, HTTP_CONNECT_TIMEOUT);
2271 curl_easy_setopt(ps->send_endpoint, CURLOPT_BUFFERSIZE, 2 * GNUNET_SERVER_MAX_MESSAGE_SIZE);
2272 #if CURL_TCP_NODELAY
2273 curl_easy_setopt(ps->send_endpoint, CURLOPT_TCP_NODELAY, 1);
2275 if (fresh==GNUNET_YES)
2277 mret = curl_multi_add_handle(plugin->multi_handle, ps->send_endpoint);
2278 if (mret != CURLM_OK)
2280 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2281 _("Connection: %X: %s failed at %s:%d: `%s'\n"),
2283 "curl_multi_add_handle", __FILE__, __LINE__,
2284 curl_multi_strerror (mret));
2285 return GNUNET_SYSERR;
2289 if (plugin->http_curl_task != GNUNET_SCHEDULER_NO_TASK)
2291 GNUNET_SCHEDULER_cancel(plugin->http_curl_task);
2292 plugin->http_curl_task = GNUNET_SCHEDULER_NO_TASK;
2294 plugin->current_connections++;
2295 plugin->http_curl_task = GNUNET_SCHEDULER_add_now (&curl_perform, plugin);
2298 if (ps->direction == INBOUND)
2300 GNUNET_assert (NULL != ps->pending_msgs_tail);
2301 if ((ps->recv_connected==GNUNET_YES) && (ps->send_connected==GNUNET_YES) &&
2302 (ps->recv_force_disconnect==GNUNET_NO) && (ps->recv_force_disconnect==GNUNET_NO))
2305 return GNUNET_SYSERR;
2310 * select best session to transmit data to peer
2312 * @param pc peer context of target peer
2313 * @param addr address of target peer
2314 * @param addrlen address length
2315 * @param force_address does transport service enforce address?
2316 * @param session session passed by transport service
2317 * @return selected session
2320 static struct Session *
2321 send_select_session (struct HTTP_PeerContext *pc,
2322 const void * addr, size_t addrlen,
2324 struct Session * session)
2326 struct Session * tmp = NULL;
2327 int addr_given = GNUNET_NO;
2329 if ((addr!=NULL) && (addrlen>0))
2330 addr_given = GNUNET_YES;
2332 if (force_address == GNUNET_YES)
2334 /* check session given as argument */
2335 if ((session != NULL) && (addr_given == GNUNET_YES))
2337 if (0 == memcmp(session->addr, addr, addrlen))
2339 /* connection can not be used, since it is disconnected */
2340 if ( (session->recv_force_disconnect==GNUNET_NO) &&
2341 (session->send_force_disconnect==GNUNET_NO) )
2343 #if DEBUG_SESSION_SELECTION
2344 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2345 "Session %X selected: Using session passed by transport to send to forced address \n",
2352 /* check last session used */
2353 if ((pc->last_session != NULL)&& (addr_given == GNUNET_YES))
2355 if (0 == memcmp(pc->last_session->addr, addr, addrlen))
2357 /* connection can not be used, since it is disconnected */
2358 if ( (pc->last_session->recv_force_disconnect==GNUNET_NO) &&
2359 (pc->last_session->send_force_disconnect==GNUNET_NO) )
2361 #if DEBUG_SESSION_SELECTION
2362 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2363 "Session %X selected: Using last session used to send to forced address \n",
2366 return pc->last_session;
2370 /* find session in existing sessions */
2372 while ((tmp!=NULL) && (addr_given == GNUNET_YES))
2374 if (0 == memcmp(tmp->addr, addr, addrlen))
2376 /* connection can not be used, since it is disconnected */
2377 if ( (tmp->recv_force_disconnect==GNUNET_NO) &&
2378 (tmp->send_force_disconnect==GNUNET_NO) )
2380 #if DEBUG_SESSION_SELECTION
2381 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2382 "Session %X selected: Using existing session to send to forced address \n",
2390 /* no session to use */
2393 if ((force_address == GNUNET_NO) || (force_address == GNUNET_SYSERR))
2395 /* check session given as argument */
2396 if (session != NULL)
2398 /* connection can not be used, since it is disconnected */
2399 if ( (session->recv_force_disconnect==GNUNET_NO) &&
2400 (session->send_force_disconnect==GNUNET_NO) )
2402 #if DEBUG_SESSION_SELECTION
2403 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2404 "Session %X selected: Using session passed by transport to send not-forced address\n",
2410 /* check last session used */
2411 if (pc->last_session != NULL)
2413 /* connection can not be used, since it is disconnected */
2414 if ( (pc->last_session->recv_force_disconnect==GNUNET_NO) &&
2415 (pc->last_session->send_force_disconnect==GNUNET_NO) )
2417 #if DEBUG_SESSION_SELECTION
2418 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2419 "Session %X selected: Using last session to send to not-forced address\n",
2422 return pc->last_session;
2425 /* find session in existing sessions */
2429 /* connection can not be used, since it is disconnected */
2430 if ( (tmp->recv_force_disconnect==GNUNET_NO) &&
2431 (tmp->send_force_disconnect==GNUNET_NO) )
2433 #if DEBUG_SESSION_SELECTION
2434 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2435 "Session %X selected: Using existing session to send to not-forced address\n",
2449 * Function that can be used by the transport service to transmit
2450 * a message using the plugin. Note that in the case of a
2451 * peer disconnecting, the continuation MUST be called
2452 * prior to the disconnect notification itself. This function
2453 * will be called with this peer's HELLO message to initiate
2454 * a fresh connection to another peer.
2456 * @param cls closure
2457 * @param target who should receive this message
2458 * @param msgbuf the message to transmit
2459 * @param msgbuf_size number of bytes in 'msgbuf'
2460 * @param priority how important is the message (most plugins will
2461 * ignore message priority and just FIFO)
2462 * @param to how long to wait at most for the transmission (does not
2463 * require plugins to discard the message after the timeout,
2464 * just advisory for the desired delay; most plugins will ignore
2466 * @param session which session must be used (or NULL for "any")
2467 * @param addr the address to use (can be NULL if the plugin
2468 * is "on its own" (i.e. re-use existing TCP connection))
2469 * @param addrlen length of the address in bytes
2470 * @param force_address GNUNET_YES if the plugin MUST use the given address,
2471 * GNUNET_NO means the plugin may use any other address and
2472 * GNUNET_SYSERR means that only reliable existing
2473 * bi-directional connections should be used (regardless
2475 * @param cont continuation to call once the message has
2476 * been transmitted (or if the transport is ready
2477 * for the next transmission call; or if the
2478 * peer disconnected...); can be NULL
2479 * @param cont_cls closure for cont
2480 * @return number of bytes used (on the physical network, with overheads);
2481 * -1 on hard errors (i.e. address invalid); 0 is a legal value
2482 * and does NOT mean that the message was not transmitted (DV)
2485 http_plugin_send (void *cls,
2486 const struct GNUNET_PeerIdentity *target,
2489 unsigned int priority,
2490 struct GNUNET_TIME_Relative to,
2491 struct Session *session,
2495 GNUNET_TRANSPORT_TransmitContinuation cont,
2498 struct Plugin *plugin = cls;
2499 struct HTTP_Message *msg;
2500 struct HTTP_PeerContext * pc;
2501 struct Session * ps = NULL;
2503 GNUNET_assert(cls !=NULL);
2508 if (force_address == GNUNET_YES)
2509 GNUNET_asprintf(&force, "forced addr.");
2510 else if (force_address == GNUNET_NO)
2511 GNUNET_asprintf(&force, "any addr.");
2512 else if (force_address == GNUNET_SYSERR)
2513 GNUNET_asprintf(&force,"reliable bi-direc. address addr.");
2516 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2517 "Transport tells me to send %u bytes to `%s' using %s (%s) and session: %X\n",
2521 http_plugin_address_to_string(NULL, addr, addrlen),
2526 pc = GNUNET_CONTAINER_multihashmap_get (plugin->peers, &target->hashPubKey);
2530 pc = GNUNET_malloc(sizeof (struct HTTP_PeerContext));
2531 pc->plugin = plugin;
2532 pc->session_id_counter=1;
2533 pc->last_session = NULL;
2534 memcpy(&pc->identity, target, sizeof(struct GNUNET_PeerIdentity));
2535 GNUNET_CONTAINER_multihashmap_put (plugin->peers,
2536 &pc->identity.hashPubKey,
2538 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
2539 GNUNET_STATISTICS_update (plugin->env->stats,
2540 gettext_noop ("# HTTP peers active"),
2544 ps = send_select_session (pc, addr, addrlen, force_address, session);
2545 /* session not existing, but address forced -> creating new session */
2548 if ((addr!=NULL) && (addrlen!=0))
2550 ps = GNUNET_malloc(sizeof (struct Session));
2551 #if DEBUG_SESSION_SELECTION
2552 if (force_address == GNUNET_YES)
2553 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2554 "No existing connection & forced address: creating new session %X to peer %s\n",
2555 ps, GNUNET_i2s(target));
2556 if (force_address != GNUNET_YES)
2557 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2558 "No existing connection: creating new session %X to peer %s\n",
2559 ps, GNUNET_i2s(target));
2561 ps->addr = GNUNET_malloc(addrlen);
2562 memcpy(ps->addr,addr,addrlen);
2563 ps->addrlen = addrlen;
2564 ps->direction=OUTBOUND;
2565 ps->recv_connected = GNUNET_NO;
2566 ps->recv_force_disconnect = GNUNET_NO;
2567 ps->send_connected = GNUNET_NO;
2568 ps->send_force_disconnect = GNUNET_NO;
2569 ps->pending_msgs_head = NULL;
2570 ps->pending_msgs_tail = NULL;
2572 ps->session_id = pc->session_id_counter;
2573 ps->queue_length_cur = 0;
2574 ps->queue_length_max = GNUNET_SERVER_MAX_MESSAGE_SIZE;
2575 pc->session_id_counter++;
2576 ps->url = create_url (plugin, ps->addr, ps->addrlen, ps->session_id);
2577 if (ps->msgtok == NULL)
2578 ps->msgtok = GNUNET_SERVER_mst_create (&curl_receive_mst_cb, ps);
2579 GNUNET_CONTAINER_DLL_insert(pc->head,pc->tail,ps);
2580 GNUNET_STATISTICS_update (plugin->env->stats,
2581 gettext_noop ("# HTTP outbound sessions for peers active"),
2588 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2589 "No existing session found & and no address given: no way to send this message to peer `%s'!\n",
2590 GNUNET_i2s(target));
2592 return GNUNET_SYSERR;
2596 if (msgbuf_size >= (ps->queue_length_max - ps->queue_length_cur))
2598 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2599 "Queue %X full: %u bytes in queue available, message with %u is too big\n",
2601 (ps->queue_length_max - ps->queue_length_cur),
2603 //return GNUNET_SYSERR;
2607 msg = GNUNET_malloc (sizeof (struct HTTP_Message) + msgbuf_size);
2609 msg->size = msgbuf_size;
2611 msg->buf = (char *) &msg[1];
2612 msg->transmit_cont = cont;
2613 msg->transmit_cont_cls = cont_cls;
2614 memcpy (msg->buf,msgbuf, msgbuf_size);
2615 GNUNET_CONTAINER_DLL_insert (ps->pending_msgs_head,
2616 ps->pending_msgs_tail,
2618 ps->queue_length_cur += msgbuf_size;
2619 if (send_check_connections (plugin, ps) == GNUNET_SYSERR)
2620 return GNUNET_SYSERR;
2621 if (force_address != GNUNET_YES)
2622 pc->last_session = ps;
2623 if (pc->last_session==NULL)
2624 pc->last_session = ps;
2630 * Function that can be used to force the plugin to disconnect
2631 * from the given peer and cancel all previous transmissions
2632 * (and their continuationc).
2634 * @param cls closure
2635 * @param target peer from which to disconnect
2638 http_plugin_disconnect (void *cls,
2639 const struct GNUNET_PeerIdentity *target)
2641 struct Plugin *plugin = cls;
2642 struct HTTP_PeerContext *pc = NULL;
2643 struct Session *ps = NULL;
2645 pc = GNUNET_CONTAINER_multihashmap_get (plugin->peers, &target->hashPubKey);
2651 /* Telling transport that session is getting disconnected */
2652 plugin->env->session_end(plugin, target, ps);
2653 if (ps->direction==OUTBOUND)
2655 if (ps->send_endpoint!=NULL)
2657 //GNUNET_assert(CURLM_OK == curl_multi_remove_handle(plugin->multi_handle,ps->send_endpoint));
2658 //curl_easy_cleanup(ps->send_endpoint);
2659 //ps->send_endpoint=NULL;
2660 ps->send_force_disconnect = GNUNET_YES;
2662 if (ps->recv_endpoint!=NULL)
2664 //GNUNET_assert(CURLM_OK == curl_multi_remove_handle(plugin->multi_handle,ps->recv_endpoint));
2665 //curl_easy_cleanup(ps->recv_endpoint);
2666 //ps->recv_endpoint=NULL;
2667 ps->recv_force_disconnect = GNUNET_YES;
2670 if (ps->direction==INBOUND)
2672 ps->recv_force_disconnect = GNUNET_YES;
2673 ps->send_force_disconnect = GNUNET_YES;
2675 while (ps->pending_msgs_head!=NULL)
2676 remove_http_message(ps, ps->pending_msgs_head);
2677 ps->recv_active = GNUNET_NO;
2678 ps->send_active = GNUNET_NO;
2685 * Convert the transports address to a nice, human-readable
2688 * @param cls closure
2689 * @param type name of the transport that generated the address
2690 * @param addr one of the addresses of the host, NULL for the last address
2691 * the specific address format depends on the transport
2692 * @param addrlen length of the address
2693 * @param numeric should (IP) addresses be displayed in numeric form?
2694 * @param timeout after how long should we give up?
2695 * @param asc function to call on each string
2696 * @param asc_cls closure for asc
2699 http_plugin_address_pretty_printer (void *cls,
2704 struct GNUNET_TIME_Relative timeout,
2705 GNUNET_TRANSPORT_AddressStringCallback
2708 const struct IPv4HttpAddress *t4;
2709 const struct IPv6HttpAddress *t6;
2710 struct sockaddr_in a4;
2711 struct sockaddr_in6 a6;
2717 GNUNET_assert(cls !=NULL);
2718 if (addrlen == sizeof (struct IPv6HttpAddress))
2720 address = GNUNET_malloc (INET6_ADDRSTRLEN);
2722 a6.sin6_addr = t6->ipv6_addr;
2723 inet_ntop(AF_INET6, &(a6.sin6_addr),address,INET6_ADDRSTRLEN);
2724 port = ntohs(t6->u6_port);
2726 else if (addrlen == sizeof (struct IPv4HttpAddress))
2728 address = GNUNET_malloc (INET_ADDRSTRLEN);
2730 a4.sin_addr.s_addr = t4->ipv4_addr;
2731 inet_ntop(AF_INET, &(a4.sin_addr),address,INET_ADDRSTRLEN);
2732 port = ntohs(t4->u_port);
2736 /* invalid address */
2737 GNUNET_break_op (0);
2738 asc (asc_cls, NULL);
2741 res = GNUNET_asprintf(&ret,"%s://%s:%u/", PROTOCOL_PREFIX, address, port);
2742 GNUNET_free (address);
2743 GNUNET_assert(res != 0);
2745 GNUNET_free_non_null (ret);
2751 * Another peer has suggested an address for this
2752 * peer and transport plugin. Check that this could be a valid
2753 * address. If so, consider adding it to the list
2756 * @param cls closure
2757 * @param addr pointer to the address
2758 * @param addrlen length of addr
2759 * @return GNUNET_OK if this is a plausible address for this peer
2763 http_plugin_address_suggested (void *cls,
2764 const void *addr, size_t addrlen)
2766 struct Plugin *plugin = cls;
2767 struct IPv4HttpAddress *v4;
2768 struct IPv6HttpAddress *v6;
2769 struct IPv4HttpAddress *tv4 = plugin->ipv4_addr_head;
2770 struct IPv6HttpAddress *tv6 = plugin->ipv6_addr_head;
2772 GNUNET_assert(cls !=NULL);
2773 if ((addrlen != sizeof (struct IPv4HttpAddress)) &&
2774 (addrlen != sizeof (struct IPv6HttpAddress)))
2775 return GNUNET_SYSERR;
2776 if (addrlen == sizeof (struct IPv4HttpAddress))
2778 v4 = (struct IPv4HttpAddress *) addr;
2779 if (plugin->bind4_address!=NULL)
2781 if (0 == memcmp (&plugin->bind4_address->sin_addr, &v4->ipv4_addr, sizeof(uint32_t)))
2784 return GNUNET_SYSERR;
2788 if (0==memcmp (&tv4->ipv4_addr, &v4->ipv4_addr, sizeof(uint32_t)))
2795 return GNUNET_SYSERR;
2797 if (addrlen == sizeof (struct IPv6HttpAddress))
2799 v6 = (struct IPv6HttpAddress *) addr;
2800 if (plugin->bind6_address!=NULL)
2802 if (0 == memcmp (&plugin->bind6_address->sin6_addr, &v6->ipv6_addr, sizeof(struct in6_addr)))
2805 return GNUNET_SYSERR;
2809 if (0 == memcmp (&tv6->ipv6_addr, &v6->ipv6_addr, sizeof(struct in6_addr)))
2816 return GNUNET_SYSERR;
2818 return GNUNET_SYSERR;
2823 * Function called for a quick conversion of the binary address to
2824 * a numeric address. Note that the caller must not free the
2825 * address and that the next call to this function is allowed
2826 * to override the address again.
2828 * @param cls closure
2829 * @param addr binary address
2830 * @param addrlen length of the address
2831 * @return string representing the same address
2834 http_plugin_address_to_string (void *cls,
2838 const struct IPv4HttpAddress *t4;
2839 const struct IPv6HttpAddress *t6;
2840 struct sockaddr_in a4;
2841 struct sockaddr_in6 a6;
2847 if (addrlen == sizeof (struct IPv6HttpAddress))
2849 address = GNUNET_malloc (INET6_ADDRSTRLEN);
2851 a6.sin6_addr = t6->ipv6_addr;
2852 inet_ntop(AF_INET6, &(a6.sin6_addr),address,INET6_ADDRSTRLEN);
2853 port = ntohs(t6->u6_port);
2855 else if (addrlen == sizeof (struct IPv4HttpAddress))
2857 address = GNUNET_malloc (INET_ADDRSTRLEN);
2859 a4.sin_addr.s_addr = t4->ipv4_addr;
2860 inet_ntop(AF_INET, &(a4.sin_addr),address,INET_ADDRSTRLEN);
2861 port = ntohs(t4->u_port);
2865 /* invalid address */
2868 res = GNUNET_asprintf(&ret,"%s:%u",address,port);
2869 GNUNET_free (address);
2870 GNUNET_assert(res != 0);
2875 * Notify transport service about address
2877 * @param cls the plugin
2881 address_notification (void *cls,
2882 const struct GNUNET_SCHEDULER_TaskContext *tc)
2884 struct Plugin *plugin = cls;
2886 GNUNET_OS_network_interfaces_list (&process_interfaces, plugin);
2890 * Exit point from the plugin.
2893 LIBGNUNET_PLUGIN_TRANSPORT_DONE (void *cls)
2895 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
2896 struct Plugin *plugin = api->cls;
2898 struct IPv4HttpAddress * ipv4addr;
2899 struct IPv6HttpAddress * ipv6addr;
2900 GNUNET_assert(cls !=NULL);
2902 if (plugin->http_server_daemon_v4 != NULL)
2904 MHD_stop_daemon (plugin->http_server_daemon_v4);
2905 plugin->http_server_daemon_v4 = NULL;
2907 if (plugin->http_server_daemon_v6 != NULL)
2909 MHD_stop_daemon (plugin->http_server_daemon_v6);
2910 plugin->http_server_daemon_v6 = NULL;
2912 if ( plugin->http_server_task_v4 != GNUNET_SCHEDULER_NO_TASK)
2914 GNUNET_SCHEDULER_cancel(plugin->http_server_task_v4);
2915 plugin->http_server_task_v4 = GNUNET_SCHEDULER_NO_TASK;
2917 if ( plugin->http_server_task_v6 != GNUNET_SCHEDULER_NO_TASK)
2919 GNUNET_SCHEDULER_cancel(plugin->http_server_task_v6);
2920 plugin->http_server_task_v6 = GNUNET_SCHEDULER_NO_TASK;
2923 while (plugin->ipv4_addr_head!=NULL)
2925 ipv4addr = plugin->ipv4_addr_head;
2926 GNUNET_CONTAINER_DLL_remove(plugin->ipv4_addr_head,plugin->ipv4_addr_tail,ipv4addr);
2927 GNUNET_free(ipv4addr);
2930 while (plugin->ipv6_addr_head!=NULL)
2932 ipv6addr = plugin->ipv6_addr_head;
2933 GNUNET_CONTAINER_DLL_remove(plugin->ipv6_addr_head,plugin->ipv6_addr_tail,ipv6addr);
2934 GNUNET_free(ipv6addr);
2937 /* free all peer information */
2938 if (plugin->peers!=NULL)
2940 GNUNET_CONTAINER_multihashmap_iterate (plugin->peers,
2941 &remove_peer_context_Iterator,
2943 GNUNET_CONTAINER_multihashmap_destroy (plugin->peers);
2945 if (plugin->multi_handle!=NULL)
2947 mret = curl_multi_cleanup(plugin->multi_handle);
2948 if (CURLM_OK != mret)
2949 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2950 "curl multihandle clean up failed\n");
2951 plugin->multi_handle = NULL;
2953 curl_global_cleanup();
2955 if ( plugin->http_curl_task != GNUNET_SCHEDULER_NO_TASK)
2957 GNUNET_SCHEDULER_cancel(plugin->http_curl_task);
2958 plugin->http_curl_task = GNUNET_SCHEDULER_NO_TASK;
2961 GNUNET_free_non_null (plugin->bind4_address);
2962 GNUNET_free_non_null (plugin->bind6_address);
2963 GNUNET_free_non_null(plugin->bind_hostname);
2965 GNUNET_free_non_null (plugin->crypto_init);
2966 GNUNET_free_non_null (plugin->cert);
2967 GNUNET_free_non_null (plugin->key);
2969 GNUNET_free (plugin);
2972 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2973 "Unload %s plugin complete...\n",
2981 load_certificate( const char * file )
2983 struct GNUNET_DISK_FileHandle * gn_file;
2987 if (0!=STAT(file, &fstat))
2989 text = GNUNET_malloc (fstat.st_size+1);
2990 gn_file = GNUNET_DISK_file_open(file, GNUNET_DISK_OPEN_READ, GNUNET_DISK_PERM_USER_READ);
2996 if (GNUNET_SYSERR == GNUNET_DISK_file_read(gn_file, text, fstat.st_size))
2999 GNUNET_DISK_file_close(gn_file);
3002 text[fstat.st_size] = '\0';
3003 GNUNET_DISK_file_close(gn_file);
3010 * Entry point for the plugin.
3013 LIBGNUNET_PLUGIN_TRANSPORT_INIT (void *cls)
3015 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
3016 struct Plugin *plugin;
3017 struct GNUNET_TRANSPORT_PluginFunctions *api;
3018 struct GNUNET_TIME_Relative gn_timeout;
3019 long long unsigned int port;
3020 unsigned long long tneigh;
3021 char * component_name;
3023 char * key_file = NULL;
3024 char * cert_file = NULL;
3027 GNUNET_assert(cls !=NULL);
3029 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3030 "Starting %s plugin...\n",
3033 GNUNET_asprintf(&component_name,
3037 plugin = GNUNET_malloc (sizeof (struct Plugin));
3038 plugin->stats = env->stats;
3040 plugin->peers = NULL;
3041 plugin->bind4_address = NULL;
3042 plugin->bind6_address = NULL;
3043 plugin->use_ipv6 = GNUNET_YES;
3044 plugin->use_ipv4 = GNUNET_YES;
3045 plugin->use_localaddresses = GNUNET_NO;
3047 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
3049 api->send = &http_plugin_send;
3050 api->disconnect = &http_plugin_disconnect;
3051 api->address_pretty_printer = &http_plugin_address_pretty_printer;
3052 api->check_address = &http_plugin_address_suggested;
3053 api->address_to_string = &http_plugin_address_to_string;
3055 /* Hashing our identity to use it in URLs */
3056 GNUNET_CRYPTO_hash_to_enc (&(plugin->env->my_identity->hashPubKey),
3057 &plugin->my_ascii_hash_ident);
3060 if (GNUNET_CONFIGURATION_have_value (env->cfg, "TRANSPORT", "NEIGHBOUR_LIMIT"))
3062 GNUNET_CONFIGURATION_get_value_number (env->cfg,
3071 plugin->max_connect_per_transport = tneigh;
3075 if (GNUNET_CONFIGURATION_have_value (env->cfg,
3076 component_name, "USE_IPv6"))
3078 plugin->use_ipv6 = GNUNET_CONFIGURATION_get_value_yesno (env->cfg,
3083 if (GNUNET_CONFIGURATION_have_value (env->cfg,
3084 component_name, "USE_IPv4"))
3086 plugin->use_ipv4 = GNUNET_CONFIGURATION_get_value_yesno (env->cfg,
3087 component_name,"USE_IPv4");
3089 /* use local addresses? */
3091 if (GNUNET_CONFIGURATION_have_value (env->cfg,
3092 component_name, "USE_LOCALADDR"))
3094 plugin->use_localaddresses = GNUNET_CONFIGURATION_get_value_yesno (env->cfg,
3098 /* Reading port number from config file */
3100 GNUNET_CONFIGURATION_get_value_number (env->cfg,
3106 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
3108 _("Require valid port number for transport plugin `%s' in configuration!\n"),
3110 GNUNET_free(component_name);
3111 LIBGNUNET_PLUGIN_TRANSPORT_DONE (api);
3115 /* Reading ipv4 addresse to bind to from config file */
3116 if ( (plugin->use_ipv4==GNUNET_YES) &&
3117 (GNUNET_CONFIGURATION_have_value (env->cfg,
3118 component_name, "BINDTO4")))
3120 GNUNET_break (GNUNET_OK ==
3121 GNUNET_CONFIGURATION_get_value_string (env->cfg,
3124 &plugin->bind_hostname));
3125 plugin->bind4_address = GNUNET_malloc(sizeof(struct sockaddr_in));
3126 plugin->bind4_address->sin_family = AF_INET;
3127 plugin->bind4_address->sin_port = htons (port);
3129 if (plugin->bind_hostname!=NULL)
3131 if (inet_pton(AF_INET,plugin->bind_hostname, &plugin->bind4_address->sin_addr)<=0)
3133 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
3135 _("Misconfigured address to bind to in configuration!\n"));
3136 GNUNET_free(plugin->bind4_address);
3137 GNUNET_free(plugin->bind_hostname);
3138 plugin->bind_hostname = NULL;
3139 plugin->bind4_address = NULL;
3144 /* Reading ipv4 addresse to bind to from config file */
3145 if ( (plugin->use_ipv6==GNUNET_YES) &&
3146 (GNUNET_CONFIGURATION_have_value (env->cfg,
3147 component_name, "BINDTO6")))
3149 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (env->cfg,
3152 &plugin->bind_hostname))
3154 plugin->bind6_address = GNUNET_malloc(sizeof(struct sockaddr_in6));
3155 plugin->bind6_address->sin6_family = AF_INET6;
3156 plugin->bind6_address->sin6_port = htons (port);
3157 if (plugin->bind_hostname!=NULL)
3159 if (inet_pton(AF_INET6,plugin->bind_hostname, &plugin->bind6_address->sin6_addr)<=0)
3161 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
3163 _("Misconfigured address to bind to in configuration!\n"));
3164 GNUNET_free(plugin->bind6_address);
3165 GNUNET_free(plugin->bind_hostname);
3166 plugin->bind_hostname = NULL;
3167 plugin->bind6_address = NULL;
3174 /* Reading HTTPS crypto related configuration */
3175 /* Get crypto init string from config */
3176 if (GNUNET_CONFIGURATION_have_value (env->cfg,
3177 "transport-https", "CRYPTO_INIT"))
3179 GNUNET_CONFIGURATION_get_value_string (env->cfg,
3182 &plugin->crypto_init);
3186 GNUNET_asprintf(&plugin->crypto_init,"NORMAL");
3189 /* Get private key file from config */
3190 if (GNUNET_CONFIGURATION_have_value (env->cfg,
3191 "transport-https", "KEY_FILE"))
3193 GNUNET_CONFIGURATION_get_value_filename (env->cfg,
3199 GNUNET_asprintf(&key_file,"https.key");
3201 /* Get private key file from config */
3202 if (GNUNET_CONFIGURATION_have_value (env->cfg,"transport-https", "CERT_FILE"))
3204 GNUNET_CONFIGURATION_get_value_filename (env->cfg,
3209 if (cert_file==NULL)
3210 GNUNET_asprintf(&cert_file,"https.cert");
3212 /* read key & certificates from file */
3213 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3214 "Loading TLS certificate `%s' `%s'\n",
3215 key_file, cert_file);
3217 plugin->key = load_certificate( key_file );
3218 plugin->cert = load_certificate( cert_file );
3220 if ((plugin->key==NULL) || (plugin->cert==NULL))
3224 GNUNET_asprintf(&cmd,
3225 "gnunet-transport-certificate-creation %s %s",
3226 key_file, cert_file);
3227 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3228 "No usable TLS certificate found, creating certificate \n");
3232 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
3234 _("Could not create a new TLS certificate, shell script `%s' failed!\n"),cmd,
3236 GNUNET_free (key_file);
3237 GNUNET_free (cert_file);
3238 GNUNET_free (component_name);
3239 LIBGNUNET_PLUGIN_TRANSPORT_DONE(api);
3244 plugin->key = load_certificate( key_file );
3245 plugin->cert = load_certificate( cert_file );
3246 if ((plugin->key==NULL) || (plugin->cert==NULL))
3248 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
3250 _("No usable TLS certificate found and creating one failed! \n"),
3252 GNUNET_free (key_file);
3253 GNUNET_free (cert_file);
3254 GNUNET_free (component_name);
3256 LIBGNUNET_PLUGIN_TRANSPORT_DONE(api);
3260 GNUNET_free (key_file);
3261 GNUNET_free (cert_file);
3263 GNUNET_assert((plugin->key!=NULL) && (plugin->cert!=NULL));
3264 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "TLS certificate loaded\n");
3267 GNUNET_assert ((port > 0) && (port <= 65535));
3268 plugin->port_inbound = port;
3269 gn_timeout = GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT;
3270 unsigned int timeout = (gn_timeout.rel_value) / 1000;
3271 if ( (plugin->http_server_daemon_v6 == NULL) &&
3272 (plugin->use_ipv6 == GNUNET_YES) &&
3275 struct sockaddr * tmp = (struct sockaddr *) plugin->bind6_address;
3276 plugin->http_server_daemon_v6 = MHD_start_daemon (
3285 &mhd_accept_cb, plugin,
3286 &mhd_access_cb, plugin,
3287 MHD_OPTION_SOCK_ADDR, tmp,
3288 MHD_OPTION_CONNECTION_LIMIT, (unsigned int) plugin->max_connect_per_transport,
3290 MHD_OPTION_HTTPS_PRIORITIES, plugin->crypto_init,
3291 MHD_OPTION_HTTPS_MEM_KEY, plugin->key,
3292 MHD_OPTION_HTTPS_MEM_CERT, plugin->cert,
3294 MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) timeout,
3295 MHD_OPTION_CONNECTION_MEMORY_LIMIT, (size_t) (2 * GNUNET_SERVER_MAX_MESSAGE_SIZE),
3296 MHD_OPTION_NOTIFY_COMPLETED, &mhd_termination_cb, plugin,
3297 MHD_OPTION_EXTERNAL_LOGGER, mhd_logger, plugin->mhd_log,
3300 if ( (plugin->http_server_daemon_v4 == NULL) &&
3301 (plugin->use_ipv4 == GNUNET_YES) &&
3304 plugin->http_server_daemon_v4 = MHD_start_daemon (
3313 &mhd_accept_cb, plugin ,
3314 &mhd_access_cb, plugin,
3315 MHD_OPTION_SOCK_ADDR, (struct sockaddr_in *) plugin->bind4_address,
3316 MHD_OPTION_CONNECTION_LIMIT, (unsigned int) plugin->max_connect_per_transport,
3318 MHD_OPTION_HTTPS_PRIORITIES, plugin->crypto_init,
3319 MHD_OPTION_HTTPS_MEM_KEY, plugin->key,
3320 MHD_OPTION_HTTPS_MEM_CERT, plugin->cert,
3322 MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) timeout,
3323 MHD_OPTION_CONNECTION_MEMORY_LIMIT, (size_t) (2 * GNUNET_SERVER_MAX_MESSAGE_SIZE),
3324 MHD_OPTION_NOTIFY_COMPLETED, &mhd_termination_cb, plugin,
3325 MHD_OPTION_EXTERNAL_LOGGER, mhd_logger, plugin->mhd_log,
3328 if (plugin->http_server_daemon_v4 != NULL)
3329 plugin->http_server_task_v4 = http_server_daemon_prepare (plugin, plugin->http_server_daemon_v4);
3330 if (plugin->http_server_daemon_v6 != NULL)
3331 plugin->http_server_task_v6 = http_server_daemon_prepare (plugin, plugin->http_server_daemon_v6);
3334 if (plugin->http_server_task_v4 != GNUNET_SCHEDULER_NO_TASK)
3337 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3338 "Starting MHD with IPv4 bound to %s with port %u\n",
3339 (plugin->bind_hostname!=NULL) ? plugin->bind_hostname : "every address",port);
3342 else if ( (plugin->http_server_task_v6 != GNUNET_SCHEDULER_NO_TASK) &&
3343 (plugin->http_server_task_v4 != GNUNET_SCHEDULER_NO_TASK) )
3346 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3347 "Starting MHD with IPv6 bound to %s with port %u\n",
3348 (plugin->bind_hostname!=NULL) ? plugin->bind_hostname : "every address", port);
3351 else if ( (plugin->http_server_task_v6 != GNUNET_SCHEDULER_NO_TASK) &&
3352 (plugin->http_server_task_v4 == GNUNET_SCHEDULER_NO_TASK) )
3355 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3356 "Starting MHD with IPv4 and IPv6 bound to %s with port %u\n",
3357 (plugin->bind_hostname!=NULL) ? plugin->bind_hostname : "every address",
3364 if ((plugin->use_ipv6 == GNUNET_YES) && (plugin->use_ipv4 == GNUNET_YES))
3365 GNUNET_asprintf(&tmp,"with IPv4 and IPv6 enabled");
3366 if ((plugin->use_ipv6 == GNUNET_NO) && (plugin->use_ipv4 == GNUNET_YES))
3367 GNUNET_asprintf(&tmp,"with IPv4 enabled");
3368 if ((plugin->use_ipv6 == GNUNET_YES) && (plugin->use_ipv4 == GNUNET_NO))
3369 GNUNET_asprintf(&tmp,"with IPv6 enabled");
3370 if ((plugin->use_ipv6 == GNUNET_NO) && (plugin->use_ipv4 == GNUNET_NO))
3371 GNUNET_asprintf(&tmp,"with NO IP PROTOCOL enabled");
3372 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3373 _("HTTP Server with %s could not be started on port %u! %s plugin failed!\n"),
3374 tmp, port, PROTOCOL_PREFIX);
3376 GNUNET_free (component_name);
3377 LIBGNUNET_PLUGIN_TRANSPORT_DONE (api);
3381 /* Initializing cURL */
3382 curl_global_init(CURL_GLOBAL_ALL);
3383 plugin->multi_handle = curl_multi_init();
3385 if ( NULL == plugin->multi_handle )
3387 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
3389 _("Could not initialize curl multi handle, failed to start %s plugin!\n"),
3391 GNUNET_free(component_name);
3392 LIBGNUNET_PLUGIN_TRANSPORT_DONE (api);
3396 plugin->peers = GNUNET_CONTAINER_multihashmap_create (10);
3398 GNUNET_free(component_name);
3399 GNUNET_SCHEDULER_add_now(address_notification, plugin);
3403 /* end of plugin_transport_http.c */