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 "gnunet_nat_lib.h"
41 #include "microhttpd.h"
42 #include <curl/curl.h>
45 #define LIBGNUNET_PLUGIN_TRANSPORT_INIT libgnunet_plugin_transport_https_init
46 #define LIBGNUNET_PLUGIN_TRANSPORT_DONE libgnunet_plugin_transport_https_done
47 #define LIBGNUNET_PLUGIN_TRANSPORT_COMPONENT transport_https
48 #define PROTOCOL_PREFIX "https"
50 #define LIBGNUNET_PLUGIN_TRANSPORT_INIT libgnunet_plugin_transport_http_init
51 #define LIBGNUNET_PLUGIN_TRANSPORT_DONE libgnunet_plugin_transport_http_done
52 #define LIBGNUNET_PLUGIN_TRANSPORT_COMPONENT transport_http
53 #define PROTOCOL_PREFIX "http"
56 #define DEBUG_HTTP GNUNET_NO
57 #define DEBUG_CURL GNUNET_NO
58 #define DEBUG_MHD GNUNET_NO
59 #define DEBUG_CONNECTIONS GNUNET_NO
60 #define DEBUG_SESSION_SELECTION GNUNET_NO
61 #define DEBUG_SCHEDULING GNUNET_NO
62 #define CURL_TCP_NODELAY GNUNET_YES
64 #define INBOUND GNUNET_NO
65 #define OUTBOUND GNUNET_YES
70 * Text of the response sent back after the last bytes of a PUT
71 * request have been received (just to formally obey the HTTP
74 #define HTTP_PUT_RESPONSE "Thank you!"
77 * After how long do we expire an address that we
78 * learned from another peer if it is not reconfirmed
81 #define LEARNED_ADDRESS_EXPIRATION GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_HOURS, 6)
84 * Page returned if request invalid
86 #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>"
89 * Timeout for a http connect
91 #define HTTP_CONNECT_TIMEOUT 30
94 * Network format for IPv4 addresses.
96 struct IPv4HttpAddress
99 * IPv4 address, in network byte order.
101 uint32_t ipv4_addr GNUNET_PACKED;
104 * Port number, in network byte order.
106 uint16_t port GNUNET_PACKED;
110 * Wrapper to manage IPv4 addresses
112 struct IPv4HttpAddressWrapper
117 struct IPv4HttpAddressWrapper * next;
120 * Linked list previous
122 struct IPv4HttpAddressWrapper * prev;
124 struct IPv4HttpAddress * addr;
128 * Network format for IPv6 addresses.
130 struct IPv6HttpAddress
135 struct in6_addr ipv6_addr GNUNET_PACKED;
138 * Port number, in network byte order.
140 uint16_t port GNUNET_PACKED;
145 * Wrapper for IPv4 addresses.
147 struct IPv6HttpAddressWrapper
152 struct IPv6HttpAddressWrapper * next;
155 * Linked list previous
157 struct IPv6HttpAddressWrapper * prev;
159 struct IPv6HttpAddress * addr;
163 * Message to send using http
168 * next pointer for double linked list
170 struct HTTP_Message * next;
173 * previous pointer for double linked list
175 struct HTTP_Message * prev;
178 * buffer containing data to send
183 * amount of data already sent
193 * Continuation function to call once the transmission buffer
194 * has again space available. NULL if there is no
195 * continuation to call.
197 GNUNET_TRANSPORT_TransmitContinuation transmit_cont;
200 * Closure for transmit_cont.
202 void *transmit_cont_cls;
206 struct HTTP_PeerContext
211 struct GNUNET_PeerIdentity identity;
214 * Pointer to the global plugin struct.
216 struct Plugin *plugin;
219 * Linked list of connections with this peer
222 struct Session * head;
225 * Linked list of connections with this peer
228 struct Session * tail;
231 * id for next session
233 size_t session_id_counter;
236 * Last session used to send data
238 struct Session * last_session;
241 * The task resetting inbound quota delay
243 GNUNET_SCHEDULER_TaskIdentifier reset_task;
246 * Delay from transport service inbound quota tracker when to receive data again
248 struct GNUNET_TIME_Relative delay;
257 struct SessionHeader header;
260 * next session in linked list
262 struct Session * next;
265 * previous session in linked list
267 struct Session * prev;
270 * address of this session
285 * Message queue for outbound messages
288 struct HTTP_Message * pending_msgs_head;
291 * Message queue for outbound messages
294 struct HTTP_Message * pending_msgs_tail;
297 * partner peer this connection belongs to
299 struct HTTP_PeerContext * peercontext;
302 * message stream tokenizer for incoming data
304 struct GNUNET_SERVER_MessageStreamTokenizer *msgtok;
308 * outbound: OUTBOUND (GNUNET_YES)
309 * inbound : INBOUND (GNUNET_NO)
311 unsigned int direction;
314 * is session connected to send data?
316 unsigned int send_connected;
319 * is send connection active?
321 unsigned int send_active;
324 * connection disconnect forced (e.g. from transport)
326 unsigned int send_force_disconnect;
329 * is session connected to receive data?
331 unsigned int recv_connected;
334 * is receive connection active?
336 unsigned int recv_active;
339 * connection disconnect forced (e.g. from transport)
341 unsigned int recv_force_disconnect;
345 * id for next session
346 * NOTE: 0 is not an ID, zero is not defined. A correct ID is always > 0
351 * entity managing sending data
352 * outbound session: CURL *
353 * inbound session: mhd_connection *
355 void * send_endpoint;
358 * entity managing recieving data
359 * outbound session: CURL *
360 * inbound session: mhd_connection *
362 void * recv_endpoint;
367 size_t queue_length_cur;
372 size_t queue_length_max;
377 * Encapsulation of all of the state of the plugin.
384 struct GNUNET_TRANSPORT_PluginEnvironment *env;
387 * Handle for reporting statistics.
389 struct GNUNET_STATISTICS_Handle *stats;
394 uint16_t port_inbound;
396 struct GNUNET_CONTAINER_MultiHashMap *peers;
399 * Daemon for listening for new IPv4 connections.
401 struct MHD_Daemon *http_server_daemon_v4;
404 * Daemon for listening for new IPv6connections.
406 struct MHD_Daemon *http_server_daemon_v6;
409 * Our primary task for http daemon handling IPv4 connections
411 GNUNET_SCHEDULER_TaskIdentifier http_server_task_v4;
414 * Our primary task for http daemon handling IPv6 connections
416 GNUNET_SCHEDULER_TaskIdentifier http_server_task_v6;
419 * The task sending data
421 GNUNET_SCHEDULER_TaskIdentifier http_curl_task;
426 CURLM * multi_handle;
429 * Our handle to the NAT module.
431 struct GNUNET_NAT_Handle *nat;
436 struct IPv4HttpAddressWrapper * ipv4_addr_head;
441 struct IPv4HttpAddressWrapper * ipv4_addr_tail;
446 struct IPv6HttpAddressWrapper * ipv6_addr_head;
451 struct IPv6HttpAddressWrapper * ipv6_addr_tail;
454 * Our ASCII encoded, hashed peer identity
455 * This string is used to distinguish between connections and is added to the urls
457 struct GNUNET_CRYPTO_HashAsciiEncoded my_ascii_hash_ident;
460 * IPv4 Address the plugin binds to
462 struct sockaddr_in * bind4_address;
465 * IPv6 Address the plugins binds to
467 struct sockaddr_in6 * bind6_address;
470 * Hostname to bind to
472 char * bind_hostname;
485 * use local addresses?
487 int use_localaddresses;
490 * maximum number of connections
492 int max_connect_per_transport;
495 * Current number of connections;
497 int current_connections;
500 * Closure passed by MHD to the mhd_logger function
504 /* only needed for HTTPS plugin */
506 /* The certificate MHD uses as an \0 terminated string */
509 /* The private key MHD uses as an \0 terminated string */
512 /* crypto init string */
518 * Context for address to string conversion.
520 struct PrettyPrinterContext
523 * Function to call with the result.
525 GNUNET_TRANSPORT_AddressStringCallback asc;
533 * Port to add after the IP address.
540 * Function called for a quick conversion of the binary address to
541 * a numeric address. Note that the caller must not free the
542 * address and that the next call to this function is allowed
543 * to override the address again.
546 * @param addr binary address
547 * @param addrlen length of the address
548 * @return string representing the same address
551 http_plugin_address_to_string (void *cls,
557 * Call MHD to process pending ipv4 requests and then go back
558 * and schedule the next run.
560 static void http_server_daemon_v4_run (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
562 * Call MHD to process pending ipv6 requests and then go back
563 * and schedule the next run.
565 static void http_server_daemon_v6_run (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
568 * Function setting up curl handle and selecting message to send
570 * @param plugin plugin
572 * @return GNUNET_SYSERR on failure, GNUNET_NO if connecting, GNUNET_YES if ok
574 static int send_check_connections (struct Plugin *plugin, struct Session *ps);
577 * Function setting up file descriptors and scheduling task to run
579 * @param plugin plugin as closure
580 * @return GNUNET_SYSERR for hard failure, GNUNET_OK for ok
582 static int curl_schedule (struct Plugin *plugin);
585 * Task scheduled to reset the inbound quota delay for a specific peer
586 * @param cls plugin as closure
587 * @param tc task context
589 static void reset_inbound_quota_delay (void *cls,
590 const struct GNUNET_SCHEDULER_TaskContext *tc)
592 struct HTTP_PeerContext * pc = cls;
594 GNUNET_assert(cls != NULL);
595 pc->reset_task = GNUNET_SCHEDULER_NO_TASK;
596 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
598 pc->delay = GNUNET_TIME_relative_get_zero ();
603 * Creates a valid url from passed address and id
604 * @param plugin plugin
605 * @param addr address to create url from
606 * @param addrlen address lenth
607 * @param id session id
608 * @return the created url
611 create_url(struct Plugin *plugin,
612 const void * addr, size_t addrlen,
616 char *addr_str = (char *) http_plugin_address_to_string(NULL, addr, addrlen);
618 GNUNET_assert ((addr!=NULL) && (addrlen != 0));
619 GNUNET_asprintf(&url,
620 "%s://%s/%s;%u", PROTOCOL_PREFIX, addr_str,
621 (char *) (&plugin->my_ascii_hash_ident),id);
627 * Removes a message from the linked list of messages
630 * @return GNUNET_SYSERR if msg not found, GNUNET_OK on success
633 remove_http_message (struct Session * ps,
634 struct HTTP_Message * msg)
636 GNUNET_CONTAINER_DLL_remove(ps->pending_msgs_head,
637 ps->pending_msgs_tail,
644 * Iterator to remove peer context
645 * @param cls the plugin
646 * @param key the peers public key hashcode
647 * @param value the peer context
648 * @return GNUNET_YES on success
651 remove_peer_context_Iterator (void *cls,
652 const GNUNET_HashCode *key,
655 struct Plugin *plugin = cls;
656 struct HTTP_PeerContext * pc = value;
657 struct Session * ps = pc->head;
658 struct Session * tmp = NULL;
659 struct HTTP_Message * msg = NULL;
660 struct HTTP_Message * msg_tmp = NULL;
663 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
664 "Freeing context for peer `%s'\n",
665 GNUNET_i2s(&pc->identity));
667 GNUNET_assert (GNUNET_YES ==
668 GNUNET_CONTAINER_multihashmap_remove (plugin->peers,
669 &pc->identity.hashPubKey,
673 plugin->env->session_end(plugin, &pc->identity, ps);
676 GNUNET_free_non_null (ps->addr);
677 GNUNET_free(ps->url);
678 if (ps->msgtok != NULL)
679 GNUNET_SERVER_mst_destroy (ps->msgtok);
681 msg = ps->pending_msgs_head;
688 if (ps->direction==OUTBOUND)
690 if (ps->send_endpoint!=NULL)
691 curl_easy_cleanup(ps->send_endpoint);
692 if (ps->recv_endpoint!=NULL)
693 curl_easy_cleanup(ps->recv_endpoint);
699 GNUNET_STATISTICS_update (plugin->env->stats,
700 gettext_noop ("# HTTP peers active"),
708 * Removes a session from the linked list of sessions
709 * @param pc peer context
711 * @param call_msg_cont GNUNET_YES to call pending message continuations, otherwise no
712 * @param call_msg_cont_result result to call message continuations with
713 * @return GNUNET_SYSERR if msg not found, GNUNET_OK on success
716 remove_session (struct HTTP_PeerContext * pc,
719 int call_msg_cont_result)
721 struct HTTP_Message * msg;
722 struct Plugin * plugin = ps->peercontext->plugin;
724 #if DEBUG_CONNECTIONS
725 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
726 "Connection %X: removing %s session %X with id %u\n",
728 (ps->direction == INBOUND)
733 plugin->env->session_end(plugin, &pc->identity, ps);
734 GNUNET_free_non_null (ps->addr);
735 GNUNET_SERVER_mst_destroy (ps->msgtok);
736 GNUNET_free(ps->url);
737 if (ps->direction==INBOUND)
739 if (ps->recv_endpoint != NULL)
741 curl_easy_cleanup(ps->recv_endpoint);
742 ps->recv_endpoint = NULL;
744 if (ps->send_endpoint != NULL)
746 curl_easy_cleanup(ps->send_endpoint);
747 ps->send_endpoint = NULL;
751 msg = ps->pending_msgs_head;
754 if ((call_msg_cont == GNUNET_YES) && (msg->transmit_cont!=NULL))
756 msg->transmit_cont (msg->transmit_cont_cls,
758 call_msg_cont_result);
760 GNUNET_CONTAINER_DLL_remove(ps->pending_msgs_head,
761 ps->pending_msgs_head,
764 msg = ps->pending_msgs_head;
767 GNUNET_CONTAINER_DLL_remove(pc->head,pc->tail,ps);
771 /* no sessions left remove peer */
775 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
776 "No sessions left for peer `%s', removing context\n",
777 GNUNET_i2s(&pc->identity));
779 remove_peer_context_Iterator(plugin, &pc->identity.hashPubKey, pc);
787 static int check_localaddress (const struct sockaddr *addr, socklen_t addrlen)
790 int local = GNUNET_NO;
791 int af = addr->sa_family;
796 uint32_t netmask = 0x7F000000;
797 uint32_t address = ntohl (((struct sockaddr_in *) addr)->sin_addr.s_addr);
798 res = (address >> 24) ^ (netmask >> 24);
804 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
805 "Checking IPv4 address `%s': %s\n", GNUNET_a2s (addr, addrlen), (local==GNUNET_YES) ? "local" : "global");
811 if (IN6_IS_ADDR_LOOPBACK (&((struct sockaddr_in6 *) addr)->sin6_addr) ||
812 IN6_IS_ADDR_LINKLOCAL (&((struct sockaddr_in6 *) addr)->sin6_addr))
817 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
818 "Checking IPv6 address `%s' : %s\n", GNUNET_a2s (addr, addrlen), (local==GNUNET_YES) ? "local" : "global");
828 * Add the IP of our network interface to the list of
829 * our external IP addresses.
831 * @param cls the 'struct Plugin*'
832 * @param name name of the interface
833 * @param isDefault do we think this may be our default interface
834 * @param addr address of the interface
835 * @param addrlen number of bytes in addr
836 * @return GNUNET_OK to continue iterating
839 process_interfaces (void *cls,
842 const struct sockaddr *addr, socklen_t addrlen)
844 struct Plugin *plugin = cls;
845 struct IPv4HttpAddress * t4;
846 struct IPv6HttpAddress * t6;
849 if (plugin->use_localaddresses == GNUNET_NO)
851 if (GNUNET_YES == check_localaddress (addr, addrlen))
854 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
856 "Not notifying transport of address `%s' (local address)\n",
857 GNUNET_a2s (addr, addrlen));
864 GNUNET_assert(cls !=NULL);
865 af = addr->sa_family;
866 if ((af == AF_INET) &&
867 (plugin->use_ipv4 == GNUNET_YES) &&
868 (plugin->bind6_address == NULL) ) {
870 struct in_addr bnd_cmp = ((struct sockaddr_in *) addr)->sin_addr;
871 t4 = GNUNET_malloc(sizeof(struct IPv4HttpAddress));
872 // Not skipping loopback addresses
875 t4->ipv4_addr = ((struct sockaddr_in *) addr)->sin_addr.s_addr;
876 t4->port = htons (plugin->port_inbound);
877 if (plugin->bind4_address != NULL) {
878 if (0 == memcmp(&plugin->bind4_address->sin_addr, &bnd_cmp, sizeof (struct in_addr)))
880 GNUNET_CONTAINER_DLL_insert(plugin->ipv4_addr_head,
881 plugin->ipv4_addr_tail,t4);
882 plugin->env->notify_address(plugin->env->cls,
884 t4, sizeof (struct IPv4HttpAddress));
892 GNUNET_CONTAINER_DLL_insert (plugin->ipv4_addr_head,
893 plugin->ipv4_addr_tail,
895 plugin->env->notify_address(plugin->env->cls,
897 t4, sizeof (struct IPv4HttpAddress));
901 if ((af == AF_INET6) &&
902 (plugin->use_ipv6 == GNUNET_YES) &&
903 (plugin->bind4_address == NULL) ) {
905 struct in6_addr bnd_cmp6 = ((struct sockaddr_in6 *) addr)->sin6_addr;
907 t6 = GNUNET_malloc(sizeof(struct IPv6HttpAddress));
908 GNUNET_assert(t6 != NULL);
910 if (plugin->bind6_address != NULL) {
911 if (0 == memcmp(&plugin->bind6_address->sin6_addr,
913 sizeof (struct in6_addr))) {
914 memcpy (&t6->ipv6_addr,
915 &((struct sockaddr_in6 *) addr)->sin6_addr,
916 sizeof (struct in6_addr));
917 t6->port = htons (plugin->port_inbound);
918 plugin->env->notify_address(plugin->env->cls,
920 t6, sizeof (struct IPv6HttpAddress));
921 GNUNET_CONTAINER_DLL_insert(plugin->ipv6_addr_head,
922 plugin->ipv6_addr_tail,
929 memcpy (&t6->ipv6_addr,
930 &((struct sockaddr_in6 *) addr)->sin6_addr,
931 sizeof (struct in6_addr));
932 t6->port = htons (plugin->port_inbound);
933 GNUNET_CONTAINER_DLL_insert(plugin->ipv6_addr_head,plugin->ipv6_addr_tail,t6);
935 plugin->env->notify_address(plugin->env->cls,
937 t6, sizeof (struct IPv6HttpAddress));
944 * External logging function for MHD
945 * @param arg arguments
946 * @param fmt format string
947 * @param ap list of arguments
950 mhd_logger (void * arg,
956 vsnprintf(text, sizeof(text), fmt, ap);
958 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
965 mhd_termination_cb (void *cls,
966 struct MHD_Connection * connection,
967 void **httpSessionCache)
969 struct Session * ps = *httpSessionCache;
972 struct HTTP_PeerContext * pc = ps->peercontext;
973 struct Plugin *plugin = cls;
975 GNUNET_assert (cls != NULL);
976 plugin->current_connections--;
978 if (connection==ps->recv_endpoint)
980 #if DEBUG_CONNECTIONS
981 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
982 "Connection %X: inbound connection from peer `%s' was terminated\n",
984 GNUNET_i2s(&pc->identity));
986 ps->recv_active = GNUNET_NO;
987 ps->recv_connected = GNUNET_NO;
988 ps->recv_endpoint = NULL;
990 if (connection==ps->send_endpoint)
992 ps->send_active = GNUNET_NO;
993 ps->send_connected = GNUNET_NO;
994 ps->send_endpoint = NULL;
995 #if DEBUG_CONNECTIONS
996 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
997 "Connection %X: outbound connection from peer `%s' was terminated\n",
999 GNUNET_i2s(&pc->identity));
1003 /* if both connections disconnected, remove session */
1004 if ( (ps->send_connected == GNUNET_NO) &&
1005 (ps->recv_connected == GNUNET_NO) )
1007 GNUNET_STATISTICS_update (pc->plugin->env->stats,
1008 gettext_noop ("# HTTP inbound sessions for peers active"),
1011 remove_session(pc,ps,GNUNET_YES,GNUNET_SYSERR);
1017 * Callback called by MessageStreamTokenizer when a message has arrived
1018 * @param cls current session as closure
1019 * @param client clien
1020 * @param message the message to be forwarded to transport service
1023 mhd_write_mst_cb (void *cls,
1025 const struct GNUNET_MessageHeader *message)
1027 struct Session *ps = cls;
1028 struct HTTP_PeerContext *pc;
1029 struct GNUNET_TIME_Relative delay;
1031 GNUNET_assert(ps != NULL);
1032 pc = ps->peercontext;
1033 GNUNET_assert(pc != NULL);
1035 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1036 "Connection %X: Forwarding message to transport service, type %u and size %u from `%s' (`%s')\n",
1038 ntohs(message->type),
1039 ntohs(message->size),
1040 GNUNET_i2s(&(ps->peercontext)->identity),
1041 http_plugin_address_to_string(NULL,ps->addr,ps->addrlen));
1043 struct GNUNET_TRANSPORT_ATS_Information distance[2];
1044 distance[0].type = htonl (GNUNET_TRANSPORT_ATS_QUALITY_NET_DISTANCE);
1045 distance[0].value = htonl (1);
1046 distance[1].type = htonl (GNUNET_TRANSPORT_ATS_ARRAY_TERMINATOR);
1047 distance[1].value = htonl (0);
1048 delay = pc->plugin->env->receive (ps->peercontext->plugin->env->cls,
1051 (const struct GNUNET_TRANSPORT_ATS_Information *) &distance,
1057 if (pc->reset_task != GNUNET_SCHEDULER_NO_TASK)
1058 GNUNET_SCHEDULER_cancel (pc->reset_task);
1060 if (delay.rel_value > 0)
1063 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1064 "Connection %X: Inbound quota management: delay next read for %llu ms \n",
1068 pc->reset_task = GNUNET_SCHEDULER_add_delayed (delay, &reset_inbound_quota_delay, pc);
1074 * Check if incoming connection is accepted.
1075 * NOTE: Here every connection is accepted
1076 * @param cls plugin as closure
1077 * @param addr address of incoming connection
1078 * @param addr_len address length of incoming connection
1079 * @return MHD_YES if connection is accepted, MHD_NO if connection is rejected
1083 mhd_accept_cb (void *cls,
1084 const struct sockaddr *addr,
1087 struct Plugin *plugin = cls;
1088 GNUNET_assert (cls != NULL);
1090 if (plugin->max_connect_per_transport > plugin->current_connections)
1092 plugin->current_connections ++;
1100 * Callback called by MHD when it needs data to send
1101 * @param cls current session
1102 * @param pos position in buffer
1103 * @param buf the buffer to write data to
1104 * @param max max number of bytes available in buffer
1105 * @return bytes written to buffer
1108 mhd_send_callback (void *cls, uint64_t pos, char *buf, size_t max)
1110 struct Session * ps = cls;
1111 struct HTTP_PeerContext * pc;
1112 struct HTTP_Message * msg;
1115 GNUNET_assert (ps!=NULL);
1117 pc = ps->peercontext;
1118 msg = ps->pending_msgs_tail;
1119 if (ps->send_force_disconnect==GNUNET_YES)
1121 #if DEBUG_CONNECTIONS
1122 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1123 "Connection %X: outbound forced to disconnect\n",
1132 if ((msg->size-msg->pos) <= max)
1134 memcpy(buf,&msg->buf[msg->pos],(msg->size-msg->pos));
1135 bytes_read = msg->size-msg->pos;
1136 msg->pos+=(msg->size-msg->pos);
1140 memcpy(buf,&msg->buf[msg->pos],max);
1145 /* removing message */
1146 if (msg->pos==msg->size)
1148 if (NULL!=msg->transmit_cont)
1149 msg->transmit_cont (msg->transmit_cont_cls,&pc->identity,GNUNET_OK);
1150 ps->queue_length_cur -= msg->size;
1151 remove_http_message(ps,msg);
1154 #if DEBUG_CONNECTIONS
1155 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1156 "Connection %X: MHD has sent %u bytes\n",
1165 * Process GET or PUT request received via MHD. For
1166 * GET, queue response that will send back our pending
1167 * messages. For PUT, process incoming data and send
1168 * to GNUnet core. In either case, check if a session
1169 * already exists and create a new one if not.
1172 mhd_access_cb (void *cls,
1173 struct MHD_Connection *mhd_connection,
1176 const char *version,
1177 const char *upload_data,
1178 size_t * upload_data_size,
1179 void **httpSessionCache)
1181 struct Plugin *plugin = cls;
1182 struct MHD_Response *response;
1183 const union MHD_ConnectionInfo * conn_info;
1184 const struct sockaddr *client_addr;
1185 const struct sockaddr_in *addrin;
1186 const struct sockaddr_in6 *addrin6;
1187 char address[INET6_ADDRSTRLEN+14];
1188 struct GNUNET_PeerIdentity pi_in;
1190 struct IPv4HttpAddress ipv4addr;
1191 struct IPv6HttpAddress ipv6addr;
1192 struct HTTP_PeerContext *pc = NULL;
1193 struct Session *ps = NULL;
1194 struct Session *ps_tmp = NULL;
1195 int res = GNUNET_NO;
1197 size_t addr_len = 0 ;
1199 GNUNET_assert(cls !=NULL);
1201 if (NULL == *httpSessionCache)
1203 /* check url for peer identity , if invalid send HTTP 404*/
1204 size_t len = strlen(&url[1]);
1205 char * peer = GNUNET_malloc(104+1);
1207 if ( (len>104) && (url[104]==';'))
1209 char * id = GNUNET_malloc((len-104)+1);
1210 strcpy(id,&url[105]);
1211 memcpy(peer,&url[1],103);
1213 id_num = strtoul ( id, NULL , 10);
1216 res = GNUNET_CRYPTO_hash_from_string (peer, &(pi_in.hashPubKey));
1218 if ( GNUNET_SYSERR == res )
1220 response = MHD_create_response_from_data (strlen (HTTP_ERROR_RESPONSE),
1221 HTTP_ERROR_RESPONSE,
1223 res = MHD_queue_response (mhd_connection, MHD_HTTP_NOT_FOUND, response);
1224 MHD_destroy_response (response);
1225 #if DEBUG_CONNECTIONS
1227 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1228 "Peer has no valid ident, sent HTTP 1.1/404\n");
1230 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1231 "Peer has no valid ident, could not send error\n");
1238 ps = *httpSessionCache;
1239 pc = ps->peercontext;
1242 if (NULL == *httpSessionCache)
1244 /* get peer context */
1245 pc = GNUNET_CONTAINER_multihashmap_get (plugin->peers, &pi_in.hashPubKey);
1249 pc = GNUNET_malloc(sizeof (struct HTTP_PeerContext));
1250 pc->plugin = plugin;
1251 pc->session_id_counter=1;
1252 pc->last_session = NULL;
1253 memcpy(&pc->identity, &pi_in, sizeof(struct GNUNET_PeerIdentity));
1254 GNUNET_CONTAINER_multihashmap_put(plugin->peers,
1255 &pc->identity.hashPubKey,
1257 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
1258 GNUNET_STATISTICS_update (plugin->env->stats,
1259 gettext_noop ("# HTTP peers active"),
1264 conn_info = MHD_get_connection_info(mhd_connection, MHD_CONNECTION_INFO_CLIENT_ADDRESS );
1265 /* Incoming IPv4 connection */
1266 /* cast required for legacy MHD API < 0.9.6 */
1267 client_addr = (const struct sockaddr *) conn_info->client_addr;
1268 if ( AF_INET == client_addr->sa_family)
1270 addrin = (const struct sockaddr_in*) client_addr;
1271 inet_ntop(addrin->sin_family, &(addrin->sin_addr),address,INET_ADDRSTRLEN);
1272 memcpy(&ipv4addr.ipv4_addr,&(addrin->sin_addr),sizeof(struct in_addr));
1273 ipv4addr.port = addrin->sin_port;
1275 addr_len = sizeof(struct IPv4HttpAddress);
1277 /* Incoming IPv6 connection */
1278 if ( AF_INET6 == client_addr->sa_family)
1280 addrin6 = (const struct sockaddr_in6 *) client_addr;
1281 inet_ntop(addrin6->sin6_family, &(addrin6->sin6_addr),address,INET6_ADDRSTRLEN);
1282 memcpy(&ipv6addr.ipv6_addr,&(addrin6->sin6_addr),sizeof(struct in6_addr));
1283 ipv6addr.port = addrin6->sin6_port;
1285 addr_len = sizeof(struct IPv6HttpAddress);
1288 GNUNET_assert (addr != NULL);
1289 GNUNET_assert (addr_len != 0);
1292 /* only inbound sessions here */
1295 while (ps_tmp!=NULL)
1297 if ((ps_tmp->direction==INBOUND) && (ps_tmp->session_id == id_num) && (id_num!=0))
1299 if ((ps_tmp->recv_force_disconnect!=GNUNET_YES) && (ps_tmp->send_force_disconnect!=GNUNET_YES))
1303 ps_tmp=ps_tmp->next;
1308 ps = GNUNET_malloc(sizeof (struct Session));
1309 ps->addr = GNUNET_malloc(addr_len);
1310 memcpy(ps->addr,addr,addr_len);
1311 ps->addrlen = addr_len;
1312 ps->direction=INBOUND;
1313 ps->pending_msgs_head = NULL;
1314 ps->pending_msgs_tail = NULL;
1315 ps->send_connected=GNUNET_NO;
1316 ps->send_active=GNUNET_NO;
1317 ps->recv_connected=GNUNET_NO;
1318 ps->recv_active=GNUNET_NO;
1320 ps->session_id =id_num;
1321 ps->queue_length_cur = 0;
1322 ps->queue_length_max = GNUNET_SERVER_MAX_MESSAGE_SIZE;
1323 ps->url = create_url (plugin, ps->addr, ps->addrlen, ps->session_id);
1324 GNUNET_CONTAINER_DLL_insert(pc->head,pc->tail,ps);
1325 GNUNET_STATISTICS_update (plugin->env->stats,
1326 gettext_noop ("# HTTP inbound sessions for peers active"),
1331 *httpSessionCache = ps;
1332 if (ps->msgtok==NULL)
1333 ps->msgtok = GNUNET_SERVER_mst_create (&mhd_write_mst_cb, ps);
1335 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1336 "Connection %X: HTTP Daemon has new an incoming `%s' request from peer `%s' (`%s')\n",
1339 GNUNET_i2s(&pc->identity),
1340 http_plugin_address_to_string(NULL, ps->addr, ps->addrlen));
1344 /* Is it a PUT or a GET request */
1345 if (0 == strcmp (MHD_HTTP_METHOD_PUT, method))
1347 if (ps->recv_force_disconnect == GNUNET_YES)
1349 #if DEBUG_CONNECTIONS
1350 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1351 "Connection %X: inbound connection was forced to disconnect\n",ps);
1353 ps->recv_active = GNUNET_NO;
1356 if ((*upload_data_size == 0) && (ps->recv_active==GNUNET_NO))
1358 ps->recv_endpoint = mhd_connection;
1359 ps->recv_connected = GNUNET_YES;
1360 ps->recv_active = GNUNET_YES;
1361 ps->recv_force_disconnect = GNUNET_NO;
1362 #if DEBUG_CONNECTIONS
1363 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1364 "Connection %X: inbound PUT connection connected\n",ps);
1369 /* Transmission of all data complete */
1370 if ((*upload_data_size == 0) && (ps->recv_active == GNUNET_YES))
1372 response = MHD_create_response_from_data (strlen (HTTP_PUT_RESPONSE),
1375 res = MHD_queue_response (mhd_connection, MHD_HTTP_OK, response);
1376 #if DEBUG_CONNECTIONS
1377 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1378 "Connection %X: Sent HTTP/1.1: 200 OK as PUT Response\n",
1381 MHD_destroy_response (response);
1382 ps->recv_active=GNUNET_NO;
1386 /* Recieving data */
1387 if ((*upload_data_size > 0) && (ps->recv_active == GNUNET_YES))
1389 if (pc->delay.rel_value == 0)
1392 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1393 "Connection %X: PUT with %u bytes forwarded to MST\n",
1394 ps, *upload_data_size);
1396 res = GNUNET_SERVER_mst_receive(ps->msgtok, ps,
1397 upload_data, *upload_data_size,
1398 GNUNET_NO, GNUNET_NO);
1399 (*upload_data_size) = 0;
1404 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1405 "Connection %X: no inbound bandwidth available! Next read was delayed for %llu ms\n",
1407 ps->peercontext->delay.rel_value);
1415 if ( 0 == strcmp (MHD_HTTP_METHOD_GET, method) )
1417 if (ps->send_force_disconnect == GNUNET_YES)
1419 #if DEBUG_CONNECTIONS
1420 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1421 "Connection %X: outbound connection was forced to disconnect\n",
1424 ps->send_active = GNUNET_NO;
1427 ps->send_connected = GNUNET_YES;
1428 ps->send_active = GNUNET_YES;
1429 ps->send_endpoint = mhd_connection;
1430 ps->send_force_disconnect = GNUNET_NO;
1431 #if DEBUG_CONNECTIONS
1432 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1433 "Connection %X: inbound GET connection connected\n",
1436 response = MHD_create_response_from_callback(-1,32 * 1024, &mhd_send_callback, ps, NULL);
1437 res = MHD_queue_response (mhd_connection, MHD_HTTP_OK, response);
1438 MHD_destroy_response (response);
1446 * Function that queries MHD's select sets and
1447 * starts the task waiting for them.
1448 * @param plugin plugin
1449 * @param daemon_handle the MHD daemon handle
1450 * @return gnunet task identifier
1452 static GNUNET_SCHEDULER_TaskIdentifier
1453 http_server_daemon_prepare (struct Plugin *plugin,
1454 struct MHD_Daemon *daemon_handle)
1456 GNUNET_SCHEDULER_TaskIdentifier ret;
1460 struct GNUNET_NETWORK_FDSet *wrs;
1461 struct GNUNET_NETWORK_FDSet *wws;
1462 struct GNUNET_NETWORK_FDSet *wes;
1464 unsigned long long timeout;
1466 struct GNUNET_TIME_Relative tv;
1468 ret = GNUNET_SCHEDULER_NO_TASK;
1472 wrs = GNUNET_NETWORK_fdset_create ();
1473 wes = GNUNET_NETWORK_fdset_create ();
1474 wws = GNUNET_NETWORK_fdset_create ();
1476 GNUNET_assert (MHD_YES ==
1477 MHD_get_fdset (daemon_handle,
1482 haveto = MHD_get_timeout (daemon_handle, &timeout);
1483 if (haveto == MHD_YES)
1484 tv.rel_value = (uint64_t) timeout;
1486 tv = GNUNET_TIME_UNIT_SECONDS;
1487 GNUNET_NETWORK_fdset_copy_native (wrs, &rs, max + 1);
1488 GNUNET_NETWORK_fdset_copy_native (wws, &ws, max + 1);
1489 GNUNET_NETWORK_fdset_copy_native (wes, &es, max + 1);
1490 if (daemon_handle == plugin->http_server_daemon_v4)
1492 if (plugin->http_server_task_v4 != GNUNET_SCHEDULER_NO_TASK)
1494 GNUNET_SCHEDULER_cancel(plugin->http_server_task_v4);
1495 plugin->http_server_daemon_v4 = GNUNET_SCHEDULER_NO_TASK;
1498 ret = GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1499 GNUNET_SCHEDULER_NO_TASK,
1503 &http_server_daemon_v4_run,
1506 if (daemon_handle == plugin->http_server_daemon_v6)
1508 if (plugin->http_server_task_v6 != GNUNET_SCHEDULER_NO_TASK)
1510 GNUNET_SCHEDULER_cancel(plugin->http_server_task_v6);
1511 plugin->http_server_task_v6 = GNUNET_SCHEDULER_NO_TASK;
1514 ret = GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1515 GNUNET_SCHEDULER_NO_TASK,
1519 &http_server_daemon_v6_run,
1522 GNUNET_NETWORK_fdset_destroy (wrs);
1523 GNUNET_NETWORK_fdset_destroy (wws);
1524 GNUNET_NETWORK_fdset_destroy (wes);
1530 * Call MHD IPv4 to process pending requests and then go back
1531 * and schedule the next run.
1532 * @param cls plugin as closure
1533 * @param tc task context
1536 http_server_daemon_v4_run (void *cls,
1537 const struct GNUNET_SCHEDULER_TaskContext *tc)
1539 struct Plugin *plugin = cls;
1541 #if DEBUG_SCHEDULING
1542 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_READ_READY))
1543 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1544 "http_server_daemon_v4_run: GNUNET_SCHEDULER_REASON_READ_READY\n");
1545 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_WRITE_READY))
1546 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1547 "http_server_daemon_v4_run: GNUNET_SCHEDULER_REASON_WRITE_READY\n");
1548 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_TIMEOUT))
1549 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1550 "http_server_daemon_v4_run: GNUNET_SCHEDULER_REASON_TIMEOUT\n");
1551 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_STARTUP))
1552 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1553 "http_server_daemon_v4_run: GGNUNET_SCHEDULER_REASON_STARTUP\n");
1554 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1555 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1556 "http_server_daemon_v4_run: GGNUNET_SCHEDULER_REASON_SHUTDOWN\n");
1559 GNUNET_assert(cls !=NULL);
1560 plugin->http_server_task_v4 = GNUNET_SCHEDULER_NO_TASK;
1562 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1565 GNUNET_assert (MHD_YES == MHD_run (plugin->http_server_daemon_v4));
1566 plugin->http_server_task_v4 = http_server_daemon_prepare (plugin, plugin->http_server_daemon_v4);
1571 * Call MHD IPv6 to process pending requests and then go back
1572 * and schedule the next run.
1573 * @param cls plugin as closure
1574 * @param tc task context
1577 http_server_daemon_v6_run (void *cls,
1578 const struct GNUNET_SCHEDULER_TaskContext *tc)
1580 struct Plugin *plugin = cls;
1582 #if DEBUG_SCHEDULING
1583 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_READ_READY))
1584 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1585 "http_server_daemon_v6_run: GNUNET_SCHEDULER_REASON_READ_READY\n");
1586 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_WRITE_READY))
1587 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1588 "http_server_daemon_v6_run: GNUNET_SCHEDULER_REASON_WRITE_READY\n");
1589 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_TIMEOUT))
1590 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1591 "http_server_daemon_v6_run: GNUNET_SCHEDULER_REASON_TIMEOUT\n");
1592 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_STARTUP))
1593 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1594 "http_server_daemon_v6_run: GGNUNET_SCHEDULER_REASON_STARTUP\n");
1595 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1596 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1597 "http_server_daemon_v6_run: GGNUNET_SCHEDULER_REASON_SHUTDOWN\n");
1600 GNUNET_assert(cls !=NULL);
1601 plugin->http_server_task_v6 = GNUNET_SCHEDULER_NO_TASK;
1603 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1606 GNUNET_assert (MHD_YES == MHD_run (plugin->http_server_daemon_v6));
1607 plugin->http_server_task_v6 = http_server_daemon_prepare (plugin, plugin->http_server_daemon_v6);
1612 curl_get_header_cb( void *ptr,
1613 size_t size, size_t nmemb,
1616 struct Session * ps = stream;
1618 long http_result = 0;
1620 /* Getting last http result code */
1621 GNUNET_assert(NULL!=ps);
1622 if (ps->recv_connected==GNUNET_NO)
1624 res = curl_easy_getinfo(ps->recv_endpoint, CURLINFO_RESPONSE_CODE, &http_result);
1625 if (CURLE_OK == res)
1627 if (http_result == 200)
1629 ps->recv_connected = GNUNET_YES;
1630 ps->recv_active = GNUNET_YES;
1631 #if DEBUG_CONNECTIONS
1632 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: connected to recieve data\n",ps);
1634 // Calling send_check_connections again since receive is established
1635 send_check_connections (ps->peercontext->plugin, ps);
1642 size_t len = size * nmemb;
1644 if ((size * nmemb) < SIZE_MAX)
1645 tmp = GNUNET_malloc (len+1);
1647 if ((tmp != NULL) && (len > 0))
1649 memcpy(tmp,ptr,len);
1652 if (tmp[len-2] == 13)
1655 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1656 "Connection %X: Header: %s\n",
1659 GNUNET_free_non_null (tmp);
1662 return size * nmemb;
1667 * Callback called by libcurl when new headers arrive
1668 * Used to get HTTP result for curl operations
1669 * @param ptr stream to read from
1670 * @param size size of one char element
1671 * @param nmemb number of char elements
1672 * @param stream closure set by user
1673 * @return bytes read by function
1676 curl_put_header_cb(void *ptr,
1681 struct Session * ps = stream;
1684 size_t len = size * nmemb;
1685 long http_result = 0;
1688 /* Getting last http result code */
1689 GNUNET_assert(NULL!=ps);
1690 res = curl_easy_getinfo (ps->send_endpoint, CURLINFO_RESPONSE_CODE, &http_result);
1691 if (CURLE_OK == res)
1693 if ((http_result == 100) && (ps->send_connected==GNUNET_NO))
1695 ps->send_connected = GNUNET_YES;
1696 ps->send_active = GNUNET_YES;
1697 #if DEBUG_CONNECTIONS
1698 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1699 "Connection %X: connected to send data\n",
1703 if ((http_result == 200) && (ps->send_connected==GNUNET_YES))
1705 ps->send_connected = GNUNET_NO;
1706 ps->send_active = GNUNET_NO;
1707 #if DEBUG_CONNECTIONS
1708 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1709 "Connection %X: sending disconnected\n",
1716 if ((size * nmemb) < SIZE_MAX)
1717 tmp = GNUNET_malloc (len+1);
1719 if ((tmp != NULL) && (len > 0))
1721 memcpy(tmp,ptr,len);
1724 if (tmp[len-2] == 13)
1728 GNUNET_free_non_null (tmp);
1729 return size * nmemb;
1733 * Callback method used with libcurl
1734 * Method is called when libcurl needs to read data during sending
1735 * @param stream pointer where to write data
1736 * @param size size of an individual element
1737 * @param nmemb count of elements that can be written to the buffer
1738 * @param ptr source pointer, passed to the libcurl handle
1739 * @return bytes written to stream
1742 curl_send_cb(void *stream,
1743 size_t size, size_t nmemb,
1746 struct Session * ps = ptr;
1747 struct HTTP_Message * msg = ps->pending_msgs_tail;
1751 if (ps->send_active == GNUNET_NO)
1752 return CURL_READFUNC_PAUSE;
1753 if ( (ps->pending_msgs_tail == NULL) &&
1754 (ps->send_active == GNUNET_YES) )
1756 #if DEBUG_CONNECTIONS
1757 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1758 "Connection %X: No Message to send, pausing connection\n",
1761 ps->send_active = GNUNET_NO;
1762 return CURL_READFUNC_PAUSE;
1765 GNUNET_assert (msg!=NULL);
1768 if (msg->pos < msg->size)
1770 /* data fit in buffer */
1771 if ((msg->size - msg->pos) <= (size * nmemb))
1773 len = (msg->size - msg->pos);
1774 memcpy(stream, &msg->buf[msg->pos], len);
1781 memcpy(stream, &msg->buf[msg->pos], len);
1786 /* no data to send */
1792 if ( msg->pos == msg->size)
1794 #if DEBUG_CONNECTIONS
1795 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1796 "Connection %X: Message with %u bytes sent, removing message from queue\n",
1800 /* Calling transmit continuation */
1801 if (NULL != ps->pending_msgs_tail->transmit_cont)
1802 msg->transmit_cont (ps->pending_msgs_tail->transmit_cont_cls,
1803 &(ps->peercontext)->identity,
1805 ps->queue_length_cur -= msg->size;
1806 remove_http_message(ps, msg);
1813 curl_receive_mst_cb (void *cls,
1815 const struct GNUNET_MessageHeader *message)
1817 struct Session *ps = cls;
1818 struct GNUNET_TIME_Relative delay;
1819 GNUNET_assert(ps != NULL);
1821 struct HTTP_PeerContext *pc = ps->peercontext;
1822 GNUNET_assert(pc != NULL);
1824 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1825 "Connection %X: Forwarding message to transport service, type %u and size %u from `%s' (`%s')\n",
1827 ntohs(message->type),
1828 ntohs(message->size),
1829 GNUNET_i2s(&(pc->identity)),
1830 http_plugin_address_to_string(NULL,ps->addr,ps->addrlen));
1832 struct GNUNET_TRANSPORT_ATS_Information distance[2];
1833 distance[0].type = htonl (GNUNET_TRANSPORT_ATS_QUALITY_NET_DISTANCE);
1834 distance[0].value = htonl (1);
1835 distance[1].type = htonl (GNUNET_TRANSPORT_ATS_ARRAY_TERMINATOR);
1836 distance[1].value = htonl (0);
1838 delay = pc->plugin->env->receive (pc->plugin->env->cls,
1841 (const struct GNUNET_TRANSPORT_ATS_Information *) &distance, 2,
1846 if (pc->reset_task != GNUNET_SCHEDULER_NO_TASK)
1847 GNUNET_SCHEDULER_cancel (pc->reset_task);
1849 if (delay.rel_value > 0)
1852 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1853 "Connection %X: Inbound quota management: delay next read for %llu ms\n",
1854 ps, delay.rel_value);
1856 pc->reset_task = GNUNET_SCHEDULER_add_delayed (delay, &reset_inbound_quota_delay, pc);
1862 * Callback method used with libcurl
1863 * Method is called when libcurl needs to write data during sending
1864 * @param stream pointer where to write data
1865 * @param size size of an individual element
1866 * @param nmemb count of elements that can be written to the buffer
1867 * @param ptr destination pointer, passed to the libcurl handle
1868 * @return bytes read from stream
1871 curl_receive_cb( void *stream, size_t size, size_t nmemb, void *ptr)
1873 struct Session * ps = ptr;
1875 if (ps->peercontext->delay.rel_value > 0)
1878 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1879 "Connection %X: no inbound bandwidth available! Next read was delayed for %llu ms\n",
1880 ps, ps->peercontext->delay.rel_value);
1884 #if DEBUG_CONNECTIONS
1885 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1886 "Connection %X: %u bytes received\n",
1889 GNUNET_SERVER_mst_receive(ps->msgtok, ps,
1891 GNUNET_NO, GNUNET_NO);
1892 return (size * nmemb);
1897 curl_handle_finished (struct Plugin *plugin)
1899 struct Session *ps = NULL;
1900 struct HTTP_PeerContext *pc = NULL;
1901 struct CURLMsg *msg;
1902 struct HTTP_Message * cur_msg = NULL;
1909 msg = curl_multi_info_read (plugin->multi_handle, &msgs_in_queue);
1910 if ((msgs_in_queue == 0) || (msg == NULL))
1912 /* get session for affected curl handle */
1913 GNUNET_assert ( msg->easy_handle != NULL );
1914 curl_easy_getinfo(msg->easy_handle, CURLINFO_PRIVATE, &tmp);
1915 ps = (struct Session *) tmp;
1916 GNUNET_assert ( ps != NULL );
1917 pc = ps->peercontext;
1918 GNUNET_assert ( pc != NULL );
1922 if ( (msg->data.result != CURLE_OK) &&
1923 (msg->data.result != CURLE_GOT_NOTHING) )
1925 /* sending msg failed*/
1926 if (msg->easy_handle == ps->send_endpoint)
1928 #if DEBUG_CONNECTIONS
1929 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1930 _("Connection %X: HTTP PUT to peer `%s' (`%s') failed: `%s' `%s'\n"),
1932 GNUNET_i2s(&pc->identity),
1933 http_plugin_address_to_string(NULL, ps->addr, ps->addrlen),
1934 "curl_multi_perform",
1935 curl_easy_strerror (msg->data.result));
1937 ps->send_connected = GNUNET_NO;
1938 ps->send_active = GNUNET_NO;
1939 curl_multi_remove_handle(plugin->multi_handle,ps->send_endpoint);
1940 while (ps->pending_msgs_tail != NULL)
1942 cur_msg = ps->pending_msgs_tail;
1943 if ( NULL != cur_msg->transmit_cont)
1944 cur_msg->transmit_cont (cur_msg->transmit_cont_cls,&pc->identity,GNUNET_SYSERR);
1945 ps->queue_length_cur -= cur_msg->size;
1946 remove_http_message(ps,cur_msg);
1949 /* GET connection failed */
1950 if (msg->easy_handle == ps->recv_endpoint)
1952 #if DEBUG_CONNECTIONS
1953 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1954 _("Connection %X: HTTP GET to peer `%s' (`%s') failed: `%s' `%s'\n"),
1956 GNUNET_i2s(&pc->identity),
1957 http_plugin_address_to_string(NULL, ps->addr, ps->addrlen),
1958 "curl_multi_perform",
1959 curl_easy_strerror (msg->data.result));
1961 ps->recv_connected = GNUNET_NO;
1962 ps->recv_active = GNUNET_NO;
1963 curl_multi_remove_handle(plugin->multi_handle,ps->recv_endpoint);
1968 if (msg->easy_handle == ps->send_endpoint)
1970 GNUNET_assert (CURLE_OK == curl_easy_getinfo(msg->easy_handle, CURLINFO_RESPONSE_CODE, &http_result));
1971 #if DEBUG_CONNECTIONS
1972 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1973 "Connection %X: HTTP PUT connection to peer `%s' (`%s') was closed with HTTP code %u\n",
1975 GNUNET_i2s(&pc->identity),
1976 http_plugin_address_to_string(NULL, ps->addr, ps->addrlen),
1979 /* Calling transmit continuation */
1980 while (ps->pending_msgs_tail != NULL)
1982 cur_msg = ps->pending_msgs_tail;
1983 if ( NULL != cur_msg->transmit_cont)
1985 /* HTTP 1xx : Last message before here was informational */
1986 if ((http_result >=100) && (http_result < 200))
1987 cur_msg->transmit_cont (cur_msg->transmit_cont_cls,&pc->identity,GNUNET_OK);
1988 /* HTTP 2xx: successful operations */
1989 if ((http_result >=200) && (http_result < 300))
1990 cur_msg->transmit_cont (cur_msg->transmit_cont_cls,&pc->identity,GNUNET_OK);
1991 /* HTTP 3xx..5xx: error */
1992 if ((http_result >=300) && (http_result < 600))
1993 cur_msg->transmit_cont (cur_msg->transmit_cont_cls,&pc->identity,GNUNET_SYSERR);
1995 ps->queue_length_cur -= cur_msg->size;
1996 remove_http_message(ps,cur_msg);
1999 ps->send_connected = GNUNET_NO;
2000 ps->send_active = GNUNET_NO;
2001 curl_multi_remove_handle(plugin->multi_handle,ps->send_endpoint);
2003 if (msg->easy_handle == ps->recv_endpoint)
2005 #if DEBUG_CONNECTIONS
2006 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2007 "Connection %X: HTTP GET connection to peer `%s' (`%s') was closed with HTTP code %u\n",
2009 GNUNET_i2s(&pc->identity),
2010 http_plugin_address_to_string(NULL, ps->addr, ps->addrlen),
2013 ps->recv_connected = GNUNET_NO;
2014 ps->recv_active = GNUNET_NO;
2015 curl_multi_remove_handle(plugin->multi_handle,ps->recv_endpoint);
2017 plugin->current_connections--;
2019 if ((ps->recv_connected == GNUNET_NO) && (ps->send_connected == GNUNET_NO))
2020 remove_session (pc, ps, GNUNET_YES, GNUNET_SYSERR);
2026 while ( (msgs_in_queue > 0) );
2031 * Task performing curl operations
2032 * @param cls plugin as closure
2033 * @param tc gnunet scheduler task context
2035 static void curl_perform (void *cls,
2036 const struct GNUNET_SCHEDULER_TaskContext *tc)
2038 struct Plugin *plugin = cls;
2039 static unsigned int handles_last_run;
2043 GNUNET_assert(cls !=NULL);
2045 plugin->http_curl_task = GNUNET_SCHEDULER_NO_TASK;
2046 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
2051 mret = curl_multi_perform (plugin->multi_handle, &running);
2052 if ((running < handles_last_run) && (running>0))
2053 curl_handle_finished(plugin);
2054 handles_last_run = running;
2056 while (mret == CURLM_CALL_MULTI_PERFORM);
2057 curl_schedule(plugin);
2062 * Function setting up file descriptors and scheduling task to run
2064 * @param plugin plugin as closure
2065 * @return GNUNET_SYSERR for hard failure, GNUNET_OK for ok
2068 curl_schedule(struct Plugin *plugin)
2074 struct GNUNET_NETWORK_FDSet *grs;
2075 struct GNUNET_NETWORK_FDSet *gws;
2079 /* Cancel previous scheduled task */
2080 if (plugin->http_curl_task != GNUNET_SCHEDULER_NO_TASK)
2082 GNUNET_SCHEDULER_cancel(plugin->http_curl_task);
2083 plugin->http_curl_task = GNUNET_SCHEDULER_NO_TASK;
2090 mret = curl_multi_fdset (plugin->multi_handle, &rs, &ws, &es, &max);
2091 if (mret != CURLM_OK)
2093 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2094 _("%s failed at %s:%d: `%s'\n"),
2095 "curl_multi_fdset", __FILE__, __LINE__,
2096 curl_multi_strerror (mret));
2097 return GNUNET_SYSERR;
2099 mret = curl_multi_timeout (plugin->multi_handle, &to);
2100 if (mret != CURLM_OK)
2102 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2103 _("%s failed at %s:%d: `%s'\n"),
2104 "curl_multi_timeout", __FILE__, __LINE__,
2105 curl_multi_strerror (mret));
2106 return GNUNET_SYSERR;
2109 grs = GNUNET_NETWORK_fdset_create ();
2110 gws = GNUNET_NETWORK_fdset_create ();
2111 GNUNET_NETWORK_fdset_copy_native (grs, &rs, max + 1);
2112 GNUNET_NETWORK_fdset_copy_native (gws, &ws, max + 1);
2113 plugin->http_curl_task = GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
2114 GNUNET_SCHEDULER_NO_TASK,
2116 ? GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
2117 : GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, to),
2122 GNUNET_NETWORK_fdset_destroy (gws);
2123 GNUNET_NETWORK_fdset_destroy (grs);
2130 * Function to log curl debug messages with GNUNET_log
2131 * @param curl handle
2132 * @param type curl_infotype
2135 * @param cls closure
2139 curl_logger (CURL * curl,
2141 char * data, size_t size,
2144 if (type == CURLINFO_TEXT)
2147 memcpy(text,data,size);
2148 if (text[size-1] == '\n')
2153 text[size+1] = '\0';
2155 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2156 "CURL: Connection %X - %s",
2166 * Function setting up curl handle and selecting message to send
2168 * @param plugin plugin
2170 * @return GNUNET_SYSERR on failure, GNUNET_NO if connecting, GNUNET_YES if ok
2173 send_check_connections (struct Plugin *plugin,
2177 struct GNUNET_TIME_Relative timeout = GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT;
2179 if ((ps->direction == OUTBOUND) && (plugin->current_connections < plugin->max_connect_per_transport))
2181 /* RECV DIRECTION */
2182 /* Check if session is connected to receive data, otherwise connect to peer */
2184 if (ps->recv_connected == GNUNET_NO)
2186 int fresh = GNUNET_NO;
2187 if (ps->recv_endpoint == NULL)
2190 ps->recv_endpoint = curl_easy_init();
2193 curl_easy_setopt(ps->recv_endpoint, CURLOPT_VERBOSE, 1L);
2194 curl_easy_setopt(ps->recv_endpoint, CURLOPT_DEBUGFUNCTION , &curl_logger);
2195 curl_easy_setopt(ps->recv_endpoint, CURLOPT_DEBUGDATA , ps->recv_endpoint);
2198 curl_easy_setopt(ps->recv_endpoint, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1);
2199 curl_easy_setopt(ps->recv_endpoint, CURLOPT_SSL_VERIFYPEER, 0);
2200 curl_easy_setopt(ps->recv_endpoint, CURLOPT_SSL_VERIFYHOST, 0);
2202 curl_easy_setopt(ps->recv_endpoint, CURLOPT_URL, ps->url);
2203 curl_easy_setopt(ps->recv_endpoint, CURLOPT_HEADERFUNCTION, &curl_get_header_cb);
2204 curl_easy_setopt(ps->recv_endpoint, CURLOPT_WRITEHEADER, ps);
2205 curl_easy_setopt(ps->recv_endpoint, CURLOPT_READFUNCTION, curl_send_cb);
2206 curl_easy_setopt(ps->recv_endpoint, CURLOPT_READDATA, ps);
2207 curl_easy_setopt(ps->recv_endpoint, CURLOPT_WRITEFUNCTION, curl_receive_cb);
2208 curl_easy_setopt(ps->recv_endpoint, CURLOPT_WRITEDATA, ps);
2209 curl_easy_setopt(ps->recv_endpoint, CURLOPT_TIMEOUT, (long) timeout.rel_value);
2210 curl_easy_setopt(ps->recv_endpoint, CURLOPT_PRIVATE, ps);
2211 curl_easy_setopt(ps->recv_endpoint, CURLOPT_CONNECTTIMEOUT, HTTP_CONNECT_TIMEOUT);
2212 curl_easy_setopt(ps->recv_endpoint, CURLOPT_BUFFERSIZE, 2*GNUNET_SERVER_MAX_MESSAGE_SIZE);
2213 #if CURL_TCP_NODELAY
2214 curl_easy_setopt(ps->recv_endpoint, CURLOPT_TCP_NODELAY, 1);
2216 if (fresh==GNUNET_YES)
2218 mret = curl_multi_add_handle(plugin->multi_handle, ps->recv_endpoint);
2219 if (mret != CURLM_OK)
2221 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2222 _("Connection: %X: %s failed at %s:%d: `%s'\n"),
2224 "curl_multi_add_handle", __FILE__, __LINE__,
2225 curl_multi_strerror (mret));
2226 return GNUNET_SYSERR;
2229 if (plugin->http_curl_task != GNUNET_SCHEDULER_NO_TASK)
2231 GNUNET_SCHEDULER_cancel(plugin->http_curl_task);
2232 plugin->http_curl_task = GNUNET_SCHEDULER_NO_TASK;
2234 plugin->current_connections ++;
2235 plugin->http_curl_task = GNUNET_SCHEDULER_add_now (&curl_perform, plugin);
2238 /* waiting for receive direction */
2239 if (ps->recv_connected==GNUNET_NO)
2242 /* SEND DIRECTION */
2243 /* Check if session is connected to send data, otherwise connect to peer */
2244 if ((ps->send_connected == GNUNET_YES) && (ps->send_endpoint!= NULL))
2246 if (ps->send_active == GNUNET_YES)
2248 #if DEBUG_CONNECTIONS
2249 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2250 "Connection %X: outbound active, enqueueing message\n",
2255 if (ps->send_active == GNUNET_NO)
2257 #if DEBUG_CONNECTIONS
2258 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2259 "Connection %X: outbound paused, unpausing existing connection and enqueueing message\n",
2262 if (CURLE_OK == curl_easy_pause(ps->send_endpoint,CURLPAUSE_CONT))
2264 ps->send_active=GNUNET_YES;
2265 if (plugin->http_curl_task != GNUNET_SCHEDULER_NO_TASK)
2267 GNUNET_SCHEDULER_cancel(plugin->http_curl_task);
2268 plugin->http_curl_task = GNUNET_SCHEDULER_NO_TASK;
2270 plugin->http_curl_task = GNUNET_SCHEDULER_add_now (&curl_perform, plugin);
2274 return GNUNET_SYSERR;
2277 /* not connected, initiate connection */
2278 if ((ps->send_connected==GNUNET_NO) && (plugin->current_connections < plugin->max_connect_per_transport))
2280 int fresh = GNUNET_NO;
2281 if (NULL == ps->send_endpoint)
2283 ps->send_endpoint = curl_easy_init();
2286 GNUNET_assert (ps->send_endpoint != NULL);
2287 GNUNET_assert (NULL != ps->pending_msgs_tail);
2288 #if DEBUG_CONNECTIONS
2289 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2290 "Connection %X: outbound not connected, initiating connection\n",
2293 ps->send_active = GNUNET_NO;
2296 curl_easy_setopt(ps->send_endpoint, CURLOPT_VERBOSE, 1L);
2297 curl_easy_setopt(ps->send_endpoint, CURLOPT_DEBUGFUNCTION , &curl_logger);
2298 curl_easy_setopt(ps->send_endpoint, CURLOPT_DEBUGDATA , ps->send_endpoint);
2301 curl_easy_setopt (ps->send_endpoint, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1);
2302 curl_easy_setopt(ps->send_endpoint, CURLOPT_SSL_VERIFYPEER, 0);
2303 curl_easy_setopt(ps->send_endpoint, CURLOPT_SSL_VERIFYHOST, 0);
2305 curl_easy_setopt(ps->send_endpoint, CURLOPT_URL, ps->url);
2306 curl_easy_setopt(ps->send_endpoint, CURLOPT_PUT, 1L);
2307 curl_easy_setopt(ps->send_endpoint, CURLOPT_HEADERFUNCTION, &curl_put_header_cb);
2308 curl_easy_setopt(ps->send_endpoint, CURLOPT_WRITEHEADER, ps);
2309 curl_easy_setopt(ps->send_endpoint, CURLOPT_READFUNCTION, curl_send_cb);
2310 curl_easy_setopt(ps->send_endpoint, CURLOPT_READDATA, ps);
2311 curl_easy_setopt(ps->send_endpoint, CURLOPT_WRITEFUNCTION, curl_receive_cb);
2312 curl_easy_setopt(ps->send_endpoint, CURLOPT_READDATA, ps);
2313 curl_easy_setopt(ps->send_endpoint, CURLOPT_TIMEOUT, (long) timeout.rel_value);
2314 curl_easy_setopt(ps->send_endpoint, CURLOPT_PRIVATE, ps);
2315 curl_easy_setopt(ps->send_endpoint, CURLOPT_CONNECTTIMEOUT, HTTP_CONNECT_TIMEOUT);
2316 curl_easy_setopt(ps->send_endpoint, CURLOPT_BUFFERSIZE, 2 * GNUNET_SERVER_MAX_MESSAGE_SIZE);
2317 #if CURL_TCP_NODELAY
2318 curl_easy_setopt(ps->send_endpoint, CURLOPT_TCP_NODELAY, 1);
2320 if (fresh==GNUNET_YES)
2322 mret = curl_multi_add_handle(plugin->multi_handle, ps->send_endpoint);
2323 if (mret != CURLM_OK)
2325 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2326 _("Connection: %X: %s failed at %s:%d: `%s'\n"),
2328 "curl_multi_add_handle", __FILE__, __LINE__,
2329 curl_multi_strerror (mret));
2330 return GNUNET_SYSERR;
2334 if (plugin->http_curl_task != GNUNET_SCHEDULER_NO_TASK)
2336 GNUNET_SCHEDULER_cancel(plugin->http_curl_task);
2337 plugin->http_curl_task = GNUNET_SCHEDULER_NO_TASK;
2339 plugin->current_connections++;
2340 plugin->http_curl_task = GNUNET_SCHEDULER_add_now (&curl_perform, plugin);
2343 if (ps->direction == INBOUND)
2345 GNUNET_assert (NULL != ps->pending_msgs_tail);
2346 if ((ps->recv_connected==GNUNET_YES) && (ps->send_connected==GNUNET_YES) &&
2347 (ps->recv_force_disconnect==GNUNET_NO) && (ps->recv_force_disconnect==GNUNET_NO))
2350 return GNUNET_SYSERR;
2355 * select best session to transmit data to peer
2357 * @param pc peer context of target peer
2358 * @param addr address of target peer
2359 * @param addrlen address length
2360 * @param force_address does transport service enforce address?
2361 * @param session session passed by transport service
2362 * @return selected session
2365 static struct Session *
2366 send_select_session (struct HTTP_PeerContext *pc,
2367 const void * addr, size_t addrlen,
2369 struct Session * session)
2371 struct Session * tmp = NULL;
2372 int addr_given = GNUNET_NO;
2374 if ((addr!=NULL) && (addrlen>0))
2375 addr_given = GNUNET_YES;
2377 if (force_address == GNUNET_YES)
2379 /* check session given as argument */
2380 if ((session != NULL) && (addr_given == GNUNET_YES))
2382 if (0 == memcmp(session->addr, addr, addrlen))
2384 /* connection can not be used, since it is disconnected */
2385 if ( (session->recv_force_disconnect==GNUNET_NO) &&
2386 (session->send_force_disconnect==GNUNET_NO) )
2388 #if DEBUG_SESSION_SELECTION
2389 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2390 "Session %X selected: Using session passed by transport to send to forced address \n",
2397 /* check last session used */
2398 if ((pc->last_session != NULL)&& (addr_given == GNUNET_YES))
2400 if (0 == memcmp(pc->last_session->addr, addr, addrlen))
2402 /* connection can not be used, since it is disconnected */
2403 if ( (pc->last_session->recv_force_disconnect==GNUNET_NO) &&
2404 (pc->last_session->send_force_disconnect==GNUNET_NO) )
2406 #if DEBUG_SESSION_SELECTION
2407 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2408 "Session %X selected: Using last session used to send to forced address \n",
2411 return pc->last_session;
2415 /* find session in existing sessions */
2417 while ((tmp!=NULL) && (addr_given == GNUNET_YES))
2419 if (0 == memcmp(tmp->addr, addr, addrlen))
2421 /* connection can not be used, since it is disconnected */
2422 if ( (tmp->recv_force_disconnect==GNUNET_NO) &&
2423 (tmp->send_force_disconnect==GNUNET_NO) )
2425 #if DEBUG_SESSION_SELECTION
2426 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2427 "Session %X selected: Using existing session to send to forced address \n",
2435 /* no session to use */
2438 if ((force_address == GNUNET_NO) || (force_address == GNUNET_SYSERR))
2440 /* check session given as argument */
2441 if (session != NULL)
2443 /* connection can not be used, since it is disconnected */
2444 if ( (session->recv_force_disconnect==GNUNET_NO) &&
2445 (session->send_force_disconnect==GNUNET_NO) )
2447 #if DEBUG_SESSION_SELECTION
2448 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2449 "Session %X selected: Using session passed by transport to send not-forced address\n",
2455 /* check last session used */
2456 if (pc->last_session != NULL)
2458 /* connection can not be used, since it is disconnected */
2459 if ( (pc->last_session->recv_force_disconnect==GNUNET_NO) &&
2460 (pc->last_session->send_force_disconnect==GNUNET_NO) )
2462 #if DEBUG_SESSION_SELECTION
2463 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2464 "Session %X selected: Using last session to send to not-forced address\n",
2467 return pc->last_session;
2470 /* find session in existing sessions */
2474 /* connection can not be used, since it is disconnected */
2475 if ( (tmp->recv_force_disconnect==GNUNET_NO) &&
2476 (tmp->send_force_disconnect==GNUNET_NO) )
2478 #if DEBUG_SESSION_SELECTION
2479 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2480 "Session %X selected: Using existing session to send to not-forced address\n",
2494 * Function that can be used by the transport service to transmit
2495 * a message using the plugin. Note that in the case of a
2496 * peer disconnecting, the continuation MUST be called
2497 * prior to the disconnect notification itself. This function
2498 * will be called with this peer's HELLO message to initiate
2499 * a fresh connection to another peer.
2501 * @param cls closure
2502 * @param target who should receive this message
2503 * @param msgbuf the message to transmit
2504 * @param msgbuf_size number of bytes in 'msgbuf'
2505 * @param priority how important is the message (most plugins will
2506 * ignore message priority and just FIFO)
2507 * @param to how long to wait at most for the transmission (does not
2508 * require plugins to discard the message after the timeout,
2509 * just advisory for the desired delay; most plugins will ignore
2511 * @param session which session must be used (or NULL for "any")
2512 * @param addr the address to use (can be NULL if the plugin
2513 * is "on its own" (i.e. re-use existing TCP connection))
2514 * @param addrlen length of the address in bytes
2515 * @param force_address GNUNET_YES if the plugin MUST use the given address,
2516 * GNUNET_NO means the plugin may use any other address and
2517 * GNUNET_SYSERR means that only reliable existing
2518 * bi-directional connections should be used (regardless
2520 * @param cont continuation to call once the message has
2521 * been transmitted (or if the transport is ready
2522 * for the next transmission call; or if the
2523 * peer disconnected...); can be NULL
2524 * @param cont_cls closure for cont
2525 * @return number of bytes used (on the physical network, with overheads);
2526 * -1 on hard errors (i.e. address invalid); 0 is a legal value
2527 * and does NOT mean that the message was not transmitted (DV)
2530 http_plugin_send (void *cls,
2531 const struct GNUNET_PeerIdentity *target,
2534 unsigned int priority,
2535 struct GNUNET_TIME_Relative to,
2536 struct Session *session,
2540 GNUNET_TRANSPORT_TransmitContinuation cont,
2543 struct Plugin *plugin = cls;
2544 struct HTTP_Message *msg;
2545 struct HTTP_PeerContext * pc;
2546 struct Session * ps = NULL;
2548 GNUNET_assert(cls !=NULL);
2553 if (force_address == GNUNET_YES)
2554 GNUNET_asprintf(&force, "forced addr.");
2555 else if (force_address == GNUNET_NO)
2556 GNUNET_asprintf(&force, "any addr.");
2557 else if (force_address == GNUNET_SYSERR)
2558 GNUNET_asprintf(&force,"reliable bi-direc. address addr.");
2561 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2562 "Transport tells me to send %u bytes to `%s' using %s (%s) and session: %X\n",
2566 http_plugin_address_to_string(NULL, addr, addrlen),
2571 pc = GNUNET_CONTAINER_multihashmap_get (plugin->peers, &target->hashPubKey);
2575 pc = GNUNET_malloc(sizeof (struct HTTP_PeerContext));
2576 pc->plugin = plugin;
2577 pc->session_id_counter=1;
2578 pc->last_session = NULL;
2579 memcpy(&pc->identity, target, sizeof(struct GNUNET_PeerIdentity));
2580 GNUNET_CONTAINER_multihashmap_put (plugin->peers,
2581 &pc->identity.hashPubKey,
2583 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
2584 GNUNET_STATISTICS_update (plugin->env->stats,
2585 gettext_noop ("# HTTP peers active"),
2589 ps = send_select_session (pc, addr, addrlen, force_address, session);
2590 /* session not existing, but address forced -> creating new session */
2593 if ((addr!=NULL) && (addrlen!=0))
2595 ps = GNUNET_malloc(sizeof (struct Session));
2596 #if DEBUG_SESSION_SELECTION
2597 if (force_address == GNUNET_YES)
2598 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2599 "No existing connection & forced address: creating new session %X to peer %s\n",
2600 ps, GNUNET_i2s(target));
2601 if (force_address != GNUNET_YES)
2602 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2603 "No existing connection: creating new session %X to peer %s\n",
2604 ps, GNUNET_i2s(target));
2606 ps->addr = GNUNET_malloc(addrlen);
2607 memcpy(ps->addr,addr,addrlen);
2608 ps->addrlen = addrlen;
2609 ps->direction=OUTBOUND;
2610 ps->recv_connected = GNUNET_NO;
2611 ps->recv_force_disconnect = GNUNET_NO;
2612 ps->send_connected = GNUNET_NO;
2613 ps->send_force_disconnect = GNUNET_NO;
2614 ps->pending_msgs_head = NULL;
2615 ps->pending_msgs_tail = NULL;
2617 ps->session_id = pc->session_id_counter;
2618 ps->queue_length_cur = 0;
2619 ps->queue_length_max = GNUNET_SERVER_MAX_MESSAGE_SIZE;
2620 pc->session_id_counter++;
2621 ps->url = create_url (plugin, ps->addr, ps->addrlen, ps->session_id);
2622 if (ps->msgtok == NULL)
2623 ps->msgtok = GNUNET_SERVER_mst_create (&curl_receive_mst_cb, ps);
2624 GNUNET_CONTAINER_DLL_insert(pc->head,pc->tail,ps);
2625 GNUNET_STATISTICS_update (plugin->env->stats,
2626 gettext_noop ("# HTTP outbound sessions for peers active"),
2633 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2634 "No existing session found & and no address given: no way to send this message to peer `%s'!\n",
2635 GNUNET_i2s(target));
2637 return GNUNET_SYSERR;
2641 if (msgbuf_size >= (ps->queue_length_max - ps->queue_length_cur))
2643 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2644 "Queue %X full: %u bytes in queue available, message with %u is too big\n",
2646 (ps->queue_length_max - ps->queue_length_cur),
2648 //return GNUNET_SYSERR;
2652 msg = GNUNET_malloc (sizeof (struct HTTP_Message) + msgbuf_size);
2654 msg->size = msgbuf_size;
2656 msg->buf = (char *) &msg[1];
2657 msg->transmit_cont = cont;
2658 msg->transmit_cont_cls = cont_cls;
2659 memcpy (msg->buf,msgbuf, msgbuf_size);
2660 GNUNET_CONTAINER_DLL_insert (ps->pending_msgs_head,
2661 ps->pending_msgs_tail,
2663 ps->queue_length_cur += msgbuf_size;
2664 if (send_check_connections (plugin, ps) == GNUNET_SYSERR)
2665 return GNUNET_SYSERR;
2666 if (force_address != GNUNET_YES)
2667 pc->last_session = ps;
2668 if (pc->last_session==NULL)
2669 pc->last_session = ps;
2675 * Function that can be used to force the plugin to disconnect
2676 * from the given peer and cancel all previous transmissions
2677 * (and their continuationc).
2679 * @param cls closure
2680 * @param target peer from which to disconnect
2683 http_plugin_disconnect (void *cls,
2684 const struct GNUNET_PeerIdentity *target)
2686 struct Plugin *plugin = cls;
2687 struct HTTP_PeerContext *pc = NULL;
2688 struct Session *ps = NULL;
2690 pc = GNUNET_CONTAINER_multihashmap_get (plugin->peers, &target->hashPubKey);
2696 /* Telling transport that session is getting disconnected */
2697 plugin->env->session_end(plugin, target, ps);
2698 if (ps->direction==OUTBOUND)
2700 if (ps->send_endpoint!=NULL)
2702 //GNUNET_assert(CURLM_OK == curl_multi_remove_handle(plugin->multi_handle,ps->send_endpoint));
2703 //curl_easy_cleanup(ps->send_endpoint);
2704 //ps->send_endpoint=NULL;
2705 ps->send_force_disconnect = GNUNET_YES;
2707 if (ps->recv_endpoint!=NULL)
2709 //GNUNET_assert(CURLM_OK == curl_multi_remove_handle(plugin->multi_handle,ps->recv_endpoint));
2710 //curl_easy_cleanup(ps->recv_endpoint);
2711 //ps->recv_endpoint=NULL;
2712 ps->recv_force_disconnect = GNUNET_YES;
2715 if (ps->direction==INBOUND)
2717 ps->recv_force_disconnect = GNUNET_YES;
2718 ps->send_force_disconnect = GNUNET_YES;
2720 while (ps->pending_msgs_head!=NULL)
2721 remove_http_message(ps, ps->pending_msgs_head);
2722 ps->recv_active = GNUNET_NO;
2723 ps->send_active = GNUNET_NO;
2730 * Append our port and forward the result.
2732 * @param cls the 'struct PrettyPrinterContext*'
2733 * @param hostname hostname part of the address
2736 append_port (void *cls, const char *hostname)
2738 struct PrettyPrinterContext *ppc = cls;
2741 if (hostname == NULL)
2743 ppc->asc (ppc->asc_cls, NULL);
2747 GNUNET_asprintf (&ret, "%s://%s:%d", PROTOCOL_PREFIX, hostname, ppc->port);
2749 ppc->asc (ppc->asc_cls, ret);
2756 * Convert the transports address to a nice, human-readable
2759 * @param cls closure
2760 * @param type name of the transport that generated the address
2761 * @param addr one of the addresses of the host, NULL for the last address
2762 * the specific address format depends on the transport
2763 * @param addrlen length of the address
2764 * @param numeric should (IP) addresses be displayed in numeric form?
2765 * @param timeout after how long should we give up?
2766 * @param asc function to call on each string
2767 * @param asc_cls closure for asc
2770 http_plugin_address_pretty_printer (void *cls,
2775 struct GNUNET_TIME_Relative timeout,
2776 GNUNET_TRANSPORT_AddressStringCallback
2779 struct PrettyPrinterContext *ppc;
2782 struct sockaddr_in a4;
2783 struct sockaddr_in6 a6;
2784 const struct IPv4HttpAddress *t4;
2785 const struct IPv6HttpAddress *t6;
2788 if (addrlen == sizeof (struct IPv6HttpAddress))
2791 memset (&a6, 0, sizeof (a6));
2792 a6.sin6_family = AF_INET6;
2793 a6.sin6_port = t6->port;
2794 memcpy (&a6.sin6_addr,
2796 sizeof (struct in6_addr));
2797 port = ntohs (t6->port);
2801 else if (addrlen == sizeof (struct IPv4HttpAddress))
2804 memset (&a4, 0, sizeof (a4));
2805 a4.sin_family = AF_INET;
2806 a4.sin_port = t4->port;
2807 a4.sin_addr.s_addr = t4->ipv4_addr;
2808 port = ntohs (t4->ipv4_addr);
2814 /* invalid address */
2815 GNUNET_break_op (0);
2816 asc (asc_cls, NULL);
2819 ppc = GNUNET_malloc (sizeof (struct PrettyPrinterContext));
2821 ppc->asc_cls = asc_cls;
2823 GNUNET_RESOLVER_hostname_get (sb,
2825 !numeric, timeout, &append_port, ppc);
2831 * Another peer has suggested an address for this
2832 * peer and transport plugin. Check that this could be a valid
2833 * address. If so, consider adding it to the list
2836 * @param cls closure
2837 * @param addr pointer to the address
2838 * @param addrlen length of addr
2839 * @return GNUNET_OK if this is a plausible address for this peer
2843 http_plugin_address_suggested (void *cls,
2844 const void *addr, size_t addrlen)
2846 struct Plugin *plugin = cls;
2847 struct IPv4HttpAddress *v4;
2848 struct IPv6HttpAddress *v6;
2849 struct IPv4HttpAddressWrapper *w_tv4 = plugin->ipv4_addr_head;
2850 struct IPv6HttpAddressWrapper *w_tv6 = plugin->ipv6_addr_head;
2852 GNUNET_assert(cls !=NULL);
2853 if ((addrlen != sizeof (struct IPv4HttpAddress)) &&
2854 (addrlen != sizeof (struct IPv6HttpAddress)))
2855 return GNUNET_SYSERR;
2856 if (addrlen == sizeof (struct IPv4HttpAddress))
2858 v4 = (struct IPv4HttpAddress *) addr;
2859 if (plugin->bind4_address!=NULL)
2861 if (0 == memcmp (&plugin->bind4_address->sin_addr, &v4->ipv4_addr, sizeof(uint32_t)))
2864 return GNUNET_SYSERR;
2868 if (0==memcmp (&w_tv4->addr->ipv4_addr, &v4->ipv4_addr, sizeof(uint32_t)))
2870 w_tv4 = w_tv4->next;
2875 return GNUNET_SYSERR;
2877 if (addrlen == sizeof (struct IPv6HttpAddress))
2879 v6 = (struct IPv6HttpAddress *) addr;
2880 if (plugin->bind6_address!=NULL)
2882 if (0 == memcmp (&plugin->bind6_address->sin6_addr, &v6->ipv6_addr, sizeof(struct in6_addr)))
2885 return GNUNET_SYSERR;
2889 if (0 == memcmp (&w_tv6->addr->ipv6_addr, &v6->ipv6_addr, sizeof(struct in6_addr)))
2891 w_tv6 = w_tv6->next;
2896 return GNUNET_SYSERR;
2898 return GNUNET_SYSERR;
2903 * Function called for a quick conversion of the binary address to
2904 * a numeric address. Note that the caller must not free the
2905 * address and that the next call to this function is allowed
2906 * to override the address again.
2908 * @param cls closure
2909 * @param addr binary address
2910 * @param addrlen length of the address
2911 * @return string representing the same address
2914 http_plugin_address_to_string (void *cls,
2918 const struct IPv4HttpAddress *t4;
2919 const struct IPv6HttpAddress *t6;
2920 struct sockaddr_in a4;
2921 struct sockaddr_in6 a6;
2923 static char rbuf[INET6_ADDRSTRLEN + 13];
2927 if (addrlen == sizeof (struct IPv6HttpAddress))
2929 address = GNUNET_malloc (INET6_ADDRSTRLEN);
2931 a6.sin6_addr = t6->ipv6_addr;
2932 inet_ntop(AF_INET6, &(a6.sin6_addr),address,INET6_ADDRSTRLEN);
2933 port = ntohs(t6->port);
2935 else if (addrlen == sizeof (struct IPv4HttpAddress))
2937 address = GNUNET_malloc (INET_ADDRSTRLEN);
2939 a4.sin_addr.s_addr = t4->ipv4_addr;
2940 inet_ntop(AF_INET, &(a4.sin_addr),address,INET_ADDRSTRLEN);
2941 port = ntohs(t4->port);
2945 /* invalid address */
2949 res = GNUNET_snprintf (rbuf,
2955 GNUNET_free (address);
2956 GNUNET_assert(res != 0);
2961 * Function called by the NAT subsystem suggesting another peer wants
2962 * to connect to us via connection reversal. Try to connect back to the
2965 * @param cls closure
2966 * @param addr address to try
2967 * @param addrlen number of bytes in addr
2970 try_connection_reversal (void *cls,
2971 const struct sockaddr *addr,
2978 tcp_nat_cb_add_addr (void *cls,
2980 const struct sockaddr *addr,
2983 struct Plugin *plugin = cls;
2984 struct IPv4HttpAddress * t4 = NULL;
2985 struct IPv4HttpAddressWrapper * w_t4 = NULL;
2986 struct IPv6HttpAddress * t6 = NULL;
2987 struct IPv6HttpAddressWrapper * w_t6 = NULL;
2990 af = addr->sa_family;
2994 w_t4 = plugin->ipv4_addr_head;
2995 while (w_t4 != NULL)
2997 int res = memcmp(&w_t4->addr->ipv4_addr,
2998 &((struct sockaddr_in *) addr)->sin_addr,
2999 sizeof (struct in_addr));
3006 w_t4 = GNUNET_malloc(sizeof(struct IPv4HttpAddressWrapper));
3007 t4 = GNUNET_malloc(sizeof(struct IPv4HttpAddress));
3008 memcpy (&t4->ipv4_addr,
3009 &((struct sockaddr_in *) addr)->sin_addr,
3010 sizeof (struct in_addr));
3011 t4->port = htons (plugin->port_inbound);
3015 GNUNET_CONTAINER_DLL_insert(plugin->ipv4_addr_head,
3016 plugin->ipv4_addr_tail,w_t4);
3018 plugin->env->notify_address(plugin->env->cls,
3020 w_t4->addr, sizeof (struct IPv4HttpAddress));
3024 w_t6 = plugin->ipv6_addr_head;
3027 int res = memcmp(&w_t6->addr->ipv6_addr,
3028 &((struct sockaddr_in6 *) addr)->sin6_addr,
3029 sizeof (struct in6_addr));
3036 w_t6 = GNUNET_malloc(sizeof(struct IPv6HttpAddressWrapper));
3037 t6 = GNUNET_malloc(sizeof(struct IPv6HttpAddress));
3039 memcpy (&t6->ipv6_addr,
3040 &((struct sockaddr_in6 *) addr)->sin6_addr,
3041 sizeof (struct in6_addr));
3042 t6->port = htons (plugin->port_inbound);
3046 GNUNET_CONTAINER_DLL_insert(plugin->ipv6_addr_head,
3047 plugin->ipv6_addr_tail,w_t6);
3049 plugin->env->notify_address(plugin->env->cls,
3051 w_t6->addr, sizeof (struct IPv6HttpAddress));
3060 tcp_nat_cb_remove_addr (void *cls,
3062 const struct sockaddr *addr,
3065 struct Plugin *plugin = cls;
3066 struct IPv4HttpAddressWrapper * w_t4 = NULL;
3067 struct IPv6HttpAddressWrapper * w_t6 = NULL;
3070 af = addr->sa_family;
3074 w_t4 = plugin->ipv4_addr_head;
3075 while (w_t4 != NULL)
3077 int res = memcmp(&w_t4->addr->ipv4_addr,
3078 &((struct sockaddr_in *) addr)->sin_addr,
3079 sizeof (struct in_addr));
3086 plugin->env->notify_address(plugin->env->cls,
3088 w_t4->addr, sizeof (struct IPv4HttpAddress));
3090 GNUNET_CONTAINER_DLL_remove(plugin->ipv4_addr_head,
3091 plugin->ipv4_addr_tail,w_t4);
3092 GNUNET_free (w_t4->addr);
3096 w_t6 = plugin->ipv6_addr_head;
3097 while (w_t6 != NULL)
3099 int res = memcmp(&w_t6->addr->ipv6_addr,
3100 &((struct sockaddr_in6 *) addr)->sin6_addr,
3101 sizeof (struct in6_addr));
3108 plugin->env->notify_address(plugin->env->cls,
3110 w_t6->addr, sizeof (struct IPv6HttpAddress));
3112 GNUNET_CONTAINER_DLL_remove(plugin->ipv6_addr_head,
3113 plugin->ipv6_addr_tail,w_t6);
3114 GNUNET_free (w_t6->addr);
3124 * Our external IP address/port mapping has changed.
3126 * @param cls closure, the 'struct LocalAddrList'
3127 * @param add_remove GNUNET_YES to mean the new public IP address, GNUNET_NO to mean
3128 * the previous (now invalid) one
3129 * @param addr either the previous or the new public IP address
3130 * @param addrlen actual lenght of the address
3133 tcp_nat_port_map_callback (void *cls,
3135 const struct sockaddr *addr,
3138 GNUNET_assert(cls !=NULL );
3140 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
3141 "NPMC called to %s address `%s'\n",
3142 (add_remove == GNUNET_YES) ? "remove" : "add",
3143 GNUNET_a2s (addr, addrlen));
3145 /* convert 'addr' to our internal format */
3149 tcp_nat_cb_add_addr (cls, add_remove, addr, addrlen);
3152 tcp_nat_cb_remove_addr (cls, add_remove, addr, addrlen);
3159 * Notify transport service about address
3161 * @param cls the plugin
3165 address_notification (void *cls,
3166 const struct GNUNET_SCHEDULER_TaskContext *tc)
3168 struct Plugin *plugin = cls;
3170 GNUNET_OS_network_interfaces_list (&process_interfaces, plugin);
3175 * Exit point from the plugin.
3178 LIBGNUNET_PLUGIN_TRANSPORT_DONE (void *cls)
3180 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
3181 struct Plugin *plugin = api->cls;
3183 struct IPv4HttpAddressWrapper * w_t4;
3184 struct IPv6HttpAddressWrapper * w_t6;
3185 GNUNET_assert(cls !=NULL);
3187 if (plugin->nat != NULL)
3188 GNUNET_NAT_unregister (plugin->nat);
3190 if (plugin->http_server_daemon_v4 != NULL)
3192 MHD_stop_daemon (plugin->http_server_daemon_v4);
3193 plugin->http_server_daemon_v4 = NULL;
3195 if (plugin->http_server_daemon_v6 != NULL)
3197 MHD_stop_daemon (plugin->http_server_daemon_v6);
3198 plugin->http_server_daemon_v6 = NULL;
3200 if ( plugin->http_server_task_v4 != GNUNET_SCHEDULER_NO_TASK)
3202 GNUNET_SCHEDULER_cancel(plugin->http_server_task_v4);
3203 plugin->http_server_task_v4 = GNUNET_SCHEDULER_NO_TASK;
3205 if ( plugin->http_server_task_v6 != GNUNET_SCHEDULER_NO_TASK)
3207 GNUNET_SCHEDULER_cancel(plugin->http_server_task_v6);
3208 plugin->http_server_task_v6 = GNUNET_SCHEDULER_NO_TASK;
3211 while (plugin->ipv4_addr_head!=NULL)
3213 w_t4 = plugin->ipv4_addr_head;
3214 GNUNET_CONTAINER_DLL_remove(plugin->ipv4_addr_head,plugin->ipv4_addr_tail,w_t4);
3215 GNUNET_free(w_t4->addr);
3219 while (plugin->ipv6_addr_head!=NULL)
3221 w_t6 = plugin->ipv6_addr_head;
3222 GNUNET_CONTAINER_DLL_remove(plugin->ipv6_addr_head,plugin->ipv6_addr_tail,w_t6);
3223 GNUNET_free(w_t6->addr);
3227 /* free all peer information */
3228 if (plugin->peers!=NULL)
3230 GNUNET_CONTAINER_multihashmap_iterate (plugin->peers,
3231 &remove_peer_context_Iterator,
3233 GNUNET_CONTAINER_multihashmap_destroy (plugin->peers);
3235 if (plugin->multi_handle!=NULL)
3237 mret = curl_multi_cleanup(plugin->multi_handle);
3238 if (CURLM_OK != mret)
3239 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3240 "curl multihandle clean up failed\n");
3241 plugin->multi_handle = NULL;
3243 curl_global_cleanup();
3245 if ( plugin->http_curl_task != GNUNET_SCHEDULER_NO_TASK)
3247 GNUNET_SCHEDULER_cancel(plugin->http_curl_task);
3248 plugin->http_curl_task = GNUNET_SCHEDULER_NO_TASK;
3252 GNUNET_free_non_null (plugin->bind4_address);
3253 GNUNET_free_non_null (plugin->bind6_address);
3254 GNUNET_free_non_null (plugin->bind_hostname);
3256 GNUNET_free_non_null (plugin->crypto_init);
3257 GNUNET_free_non_null (plugin->cert);
3258 GNUNET_free_non_null (plugin->key);
3260 GNUNET_free (plugin);
3263 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3264 "Unload %s plugin complete...\n",
3272 load_certificate( const char * file )
3274 struct GNUNET_DISK_FileHandle * gn_file;
3278 if (0 != STAT(file, &fstat))
3280 text = GNUNET_malloc (fstat.st_size+1);
3281 gn_file = GNUNET_DISK_file_open(file, GNUNET_DISK_OPEN_READ, GNUNET_DISK_PERM_USER_READ);
3287 if (GNUNET_SYSERR == GNUNET_DISK_file_read (gn_file, text, fstat.st_size))
3290 GNUNET_DISK_file_close (gn_file);
3293 text[fstat.st_size] = '\0';
3294 GNUNET_DISK_file_close (gn_file);
3301 * Entry point for the plugin.
3304 LIBGNUNET_PLUGIN_TRANSPORT_INIT (void *cls)
3306 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
3307 struct Plugin *plugin;
3308 struct GNUNET_TRANSPORT_PluginFunctions *api;
3309 struct GNUNET_TIME_Relative gn_timeout;
3310 long long unsigned int port;
3311 unsigned long long tneigh;
3312 struct sockaddr **addrs;
3313 socklen_t *addrlens;
3315 char * component_name;
3317 char * key_file = NULL;
3318 char * cert_file = NULL;
3321 GNUNET_assert(cls !=NULL);
3323 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3324 "Starting %s plugin...\n",
3327 GNUNET_asprintf(&component_name,
3331 plugin = GNUNET_malloc (sizeof (struct Plugin));
3332 plugin->stats = env->stats;
3334 plugin->peers = NULL;
3335 plugin->bind4_address = NULL;
3336 plugin->bind6_address = NULL;
3337 plugin->use_ipv6 = GNUNET_YES;
3338 plugin->use_ipv4 = GNUNET_YES;
3339 plugin->use_localaddresses = GNUNET_NO;
3341 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
3343 api->send = &http_plugin_send;
3344 api->disconnect = &http_plugin_disconnect;
3345 api->address_pretty_printer = &http_plugin_address_pretty_printer;
3346 api->check_address = &http_plugin_address_suggested;
3347 api->address_to_string = &http_plugin_address_to_string;
3349 /* Hashing our identity to use it in URLs */
3350 GNUNET_CRYPTO_hash_to_enc (&(plugin->env->my_identity->hashPubKey),
3351 &plugin->my_ascii_hash_ident);
3355 GNUNET_CONFIGURATION_get_value_number (env->cfg,
3360 plugin->max_connect_per_transport = tneigh;
3364 if (GNUNET_CONFIGURATION_have_value (env->cfg,
3365 component_name, "USE_IPv6"))
3367 plugin->use_ipv6 = GNUNET_CONFIGURATION_get_value_yesno (env->cfg,
3372 if (GNUNET_CONFIGURATION_have_value (env->cfg,
3373 component_name, "USE_IPv4"))
3375 plugin->use_ipv4 = GNUNET_CONFIGURATION_get_value_yesno (env->cfg,
3376 component_name,"USE_IPv4");
3378 /* use local addresses? */
3380 if (GNUNET_CONFIGURATION_have_value (env->cfg,
3381 component_name, "USE_LOCALADDR"))
3383 plugin->use_localaddresses = GNUNET_CONFIGURATION_get_value_yesno (env->cfg,
3387 /* Reading port number from config file */
3389 GNUNET_CONFIGURATION_get_value_number (env->cfg,
3395 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
3397 _("Require valid port number for transport plugin `%s' in configuration!\n"),
3399 GNUNET_free(component_name);
3400 LIBGNUNET_PLUGIN_TRANSPORT_DONE (api);
3404 /* Reading ipv4 addresse to bind to from config file */
3405 if ( (plugin->use_ipv4==GNUNET_YES) &&
3406 (GNUNET_CONFIGURATION_have_value (env->cfg,
3407 component_name, "BINDTO4")))
3409 GNUNET_break (GNUNET_OK ==
3410 GNUNET_CONFIGURATION_get_value_string (env->cfg,
3413 &plugin->bind_hostname));
3414 plugin->bind4_address = GNUNET_malloc(sizeof(struct sockaddr_in));
3415 plugin->bind4_address->sin_family = AF_INET;
3416 plugin->bind4_address->sin_port = htons (port);
3418 if (plugin->bind_hostname!=NULL)
3420 if (inet_pton(AF_INET,plugin->bind_hostname, &plugin->bind4_address->sin_addr)<=0)
3422 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
3424 _("Misconfigured address to bind to in configuration!\n"));
3425 GNUNET_free(plugin->bind4_address);
3426 GNUNET_free(plugin->bind_hostname);
3427 plugin->bind_hostname = NULL;
3428 plugin->bind4_address = NULL;
3433 /* Reading ipv4 addresse to bind to from config file */
3434 if ( (plugin->use_ipv6==GNUNET_YES) &&
3435 (GNUNET_CONFIGURATION_have_value (env->cfg,
3436 component_name, "BINDTO6")))
3438 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (env->cfg,
3441 &plugin->bind_hostname))
3443 plugin->bind6_address = GNUNET_malloc(sizeof(struct sockaddr_in6));
3444 plugin->bind6_address->sin6_family = AF_INET6;
3445 plugin->bind6_address->sin6_port = htons (port);
3446 if (plugin->bind_hostname!=NULL)
3448 if (inet_pton(AF_INET6,plugin->bind_hostname, &plugin->bind6_address->sin6_addr)<=0)
3450 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
3452 _("Misconfigured address to bind to in configuration!\n"));
3453 GNUNET_free(plugin->bind6_address);
3454 GNUNET_free(plugin->bind_hostname);
3455 plugin->bind_hostname = NULL;
3456 plugin->bind6_address = NULL;
3463 /* Reading HTTPS crypto related configuration */
3464 /* Get crypto init string from config */
3466 GNUNET_CONFIGURATION_get_value_string (env->cfg,
3469 &plugin->crypto_init)) ||
3471 GNUNET_CONFIGURATION_get_value_filename (env->cfg,
3476 GNUNET_CONFIGURATION_get_value_filename (env->cfg,
3481 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
3483 _("Required configuration options missing in section `%s'\n"),
3485 GNUNET_free (component_name);
3486 GNUNET_free_non_null (key_file);
3487 GNUNET_free_non_null (cert_file);
3488 LIBGNUNET_PLUGIN_TRANSPORT_DONE (api);
3492 /* read key & certificates from file */
3493 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3494 "Loading TLS certificate `%s' `%s'\n",
3495 key_file, cert_file);
3497 plugin->key = load_certificate (key_file);
3498 plugin->cert = load_certificate (cert_file);
3500 if ( (plugin->key==NULL) || (plugin->cert==NULL) )
3505 GNUNET_free_non_null (plugin->key);
3507 GNUNET_free_non_null (plugin->cert);
3508 plugin->cert = NULL;
3509 GNUNET_asprintf(&cmd,
3510 "gnunet-transport-certificate-creation %s %s",
3511 key_file, cert_file);
3512 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3513 "No usable TLS certificate found, creating certificate\n");
3517 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
3519 _("Could not create a new TLS certificate, shell script `%s' failed!\n"),
3521 GNUNET_free (key_file);
3522 GNUNET_free (cert_file);
3523 GNUNET_free (component_name);
3524 LIBGNUNET_PLUGIN_TRANSPORT_DONE(api);
3529 plugin->key = load_certificate (key_file);
3530 plugin->cert = load_certificate (cert_file);
3531 if ((plugin->key==NULL) || (plugin->cert==NULL))
3533 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
3535 _("No usable TLS certificate found and creating one failed!\n"),
3537 GNUNET_free (key_file);
3538 GNUNET_free (cert_file);
3539 GNUNET_free (component_name);
3540 LIBGNUNET_PLUGIN_TRANSPORT_DONE (api);
3544 GNUNET_free (key_file);
3545 GNUNET_free (cert_file);
3547 GNUNET_assert((plugin->key!=NULL) && (plugin->cert!=NULL));
3548 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3549 "TLS certificate loaded\n");
3552 GNUNET_assert ((port > 0) && (port <= 65535));
3553 plugin->port_inbound = port;
3554 gn_timeout = GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT;
3555 unsigned int timeout = (gn_timeout.rel_value) / 1000;
3556 if ( (plugin->http_server_daemon_v6 == NULL) &&
3557 (plugin->use_ipv6 == GNUNET_YES) &&
3560 struct sockaddr * tmp = (struct sockaddr *) plugin->bind6_address;
3561 plugin->http_server_daemon_v6 = MHD_start_daemon (
3570 &mhd_accept_cb, plugin,
3571 &mhd_access_cb, plugin,
3572 MHD_OPTION_SOCK_ADDR, tmp,
3573 MHD_OPTION_CONNECTION_LIMIT, (unsigned int) plugin->max_connect_per_transport,
3575 MHD_OPTION_HTTPS_PRIORITIES, plugin->crypto_init,
3576 MHD_OPTION_HTTPS_MEM_KEY, plugin->key,
3577 MHD_OPTION_HTTPS_MEM_CERT, plugin->cert,
3579 MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) timeout,
3580 MHD_OPTION_CONNECTION_MEMORY_LIMIT, (size_t) (2 * GNUNET_SERVER_MAX_MESSAGE_SIZE),
3581 MHD_OPTION_NOTIFY_COMPLETED, &mhd_termination_cb, plugin,
3582 MHD_OPTION_EXTERNAL_LOGGER, mhd_logger, plugin->mhd_log,
3585 if ( (plugin->http_server_daemon_v4 == NULL) &&
3586 (plugin->use_ipv4 == GNUNET_YES) &&
3589 plugin->http_server_daemon_v4 = MHD_start_daemon (
3598 &mhd_accept_cb, plugin ,
3599 &mhd_access_cb, plugin,
3600 MHD_OPTION_SOCK_ADDR, (struct sockaddr_in *) plugin->bind4_address,
3601 MHD_OPTION_CONNECTION_LIMIT, (unsigned int) plugin->max_connect_per_transport,
3603 MHD_OPTION_HTTPS_PRIORITIES, plugin->crypto_init,
3605 MHD_OPTION_HTTPS_MEM_KEY, plugin->key,
3606 MHD_OPTION_HTTPS_MEM_CERT, plugin->cert,
3608 MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) timeout,
3609 MHD_OPTION_CONNECTION_MEMORY_LIMIT, (size_t) (2 * GNUNET_SERVER_MAX_MESSAGE_SIZE),
3610 MHD_OPTION_NOTIFY_COMPLETED, &mhd_termination_cb, plugin,
3611 MHD_OPTION_EXTERNAL_LOGGER, mhd_logger, plugin->mhd_log,
3614 if (plugin->http_server_daemon_v4 != NULL)
3615 plugin->http_server_task_v4 = http_server_daemon_prepare (plugin, plugin->http_server_daemon_v4);
3616 if (plugin->http_server_daemon_v6 != NULL)
3617 plugin->http_server_task_v6 = http_server_daemon_prepare (plugin, plugin->http_server_daemon_v6);
3620 if (plugin->http_server_task_v4 != GNUNET_SCHEDULER_NO_TASK)
3623 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3624 "Starting MHD with IPv4 bound to %s with port %u\n",
3625 (plugin->bind_hostname!=NULL) ? plugin->bind_hostname : "every address",port);
3628 else if ( (plugin->http_server_task_v6 != GNUNET_SCHEDULER_NO_TASK) &&
3629 (plugin->http_server_task_v4 != GNUNET_SCHEDULER_NO_TASK) )
3632 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3633 "Starting MHD with IPv6 bound to %s with port %u\n",
3634 (plugin->bind_hostname!=NULL) ? plugin->bind_hostname : "every address", port);
3637 else if ( (plugin->http_server_task_v6 != GNUNET_SCHEDULER_NO_TASK) &&
3638 (plugin->http_server_task_v4 == GNUNET_SCHEDULER_NO_TASK) )
3641 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3642 "Starting MHD with IPv4 and IPv6 bound to %s with port %u\n",
3643 (plugin->bind_hostname!=NULL) ? plugin->bind_hostname : "every address",
3650 if ((plugin->use_ipv6 == GNUNET_YES) && (plugin->use_ipv4 == GNUNET_YES))
3651 GNUNET_asprintf(&tmp,"with IPv4 and IPv6 enabled");
3652 if ((plugin->use_ipv6 == GNUNET_NO) && (plugin->use_ipv4 == GNUNET_YES))
3653 GNUNET_asprintf(&tmp,"with IPv4 enabled");
3654 if ((plugin->use_ipv6 == GNUNET_YES) && (plugin->use_ipv4 == GNUNET_NO))
3655 GNUNET_asprintf(&tmp,"with IPv6 enabled");
3656 if ((plugin->use_ipv6 == GNUNET_NO) && (plugin->use_ipv4 == GNUNET_NO))
3657 GNUNET_asprintf(&tmp,"with NO IP PROTOCOL enabled");
3658 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3659 _("HTTP Server with %s could not be started on port %u! %s plugin failed!\n"),
3660 tmp, port, PROTOCOL_PREFIX);
3662 GNUNET_free (component_name);
3663 LIBGNUNET_PLUGIN_TRANSPORT_DONE (api);
3667 /* Initializing cURL */
3668 curl_global_init(CURL_GLOBAL_ALL);
3669 plugin->multi_handle = curl_multi_init();
3671 if ( NULL == plugin->multi_handle )
3673 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
3675 _("Could not initialize curl multi handle, failed to start %s plugin!\n"),
3677 GNUNET_free(component_name);
3678 LIBGNUNET_PLUGIN_TRANSPORT_DONE (api);
3682 ret = GNUNET_SERVICE_get_server_addresses (component_name,
3687 if (ret != GNUNET_SYSERR)
3689 plugin->nat = GNUNET_NAT_register (env->cfg,
3693 (const struct sockaddr **) addrs,
3695 &tcp_nat_port_map_callback,
3696 &try_connection_reversal,
3701 GNUNET_assert (addrs[ret] != NULL);
3702 GNUNET_free (addrs[ret]);
3704 GNUNET_free_non_null (addrs);
3705 GNUNET_free_non_null (addrlens);
3709 plugin->nat = GNUNET_NAT_register (env->cfg,
3714 &try_connection_reversal,
3718 plugin->peers = GNUNET_CONTAINER_multihashmap_create (10);
3720 GNUNET_free(component_name);
3721 //GNUNET_SCHEDULER_add_now(address_notification, plugin);
3725 /* end of plugin_transport_http.c */