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_CONTAINER_multihashmap_remove (plugin->peers, &pc->identity.hashPubKey, pc);
670 plugin->env->session_end(plugin, &pc->identity, ps);
673 GNUNET_free_non_null (ps->addr);
674 GNUNET_free(ps->url);
675 if (ps->msgtok != NULL)
676 GNUNET_SERVER_mst_destroy (ps->msgtok);
678 msg = ps->pending_msgs_head;
685 if (ps->direction==OUTBOUND)
687 if (ps->send_endpoint!=NULL)
688 curl_easy_cleanup(ps->send_endpoint);
689 if (ps->recv_endpoint!=NULL)
690 curl_easy_cleanup(ps->recv_endpoint);
696 GNUNET_STATISTICS_update (plugin->env->stats,
697 gettext_noop ("# HTTP peers active"),
705 * Removes a session from the linked list of sessions
706 * @param pc peer context
708 * @param call_msg_cont GNUNET_YES to call pending message continuations, otherwise no
709 * @param call_msg_cont_result result to call message continuations with
710 * @return GNUNET_SYSERR if msg not found, GNUNET_OK on success
713 remove_session (struct HTTP_PeerContext * pc,
716 int call_msg_cont_result)
718 struct HTTP_Message * msg;
719 struct Plugin * plugin = ps->peercontext->plugin;
721 #if DEBUG_CONNECTIONS
722 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
723 "Connection %X: removing %s session %X with id %u\n",
725 (ps->direction == INBOUND)
730 plugin->env->session_end(plugin, &pc->identity, ps);
731 GNUNET_free_non_null (ps->addr);
732 GNUNET_SERVER_mst_destroy (ps->msgtok);
733 GNUNET_free(ps->url);
734 if (ps->direction==INBOUND)
736 if (ps->recv_endpoint != NULL)
738 curl_easy_cleanup(ps->recv_endpoint);
739 ps->recv_endpoint = NULL;
741 if (ps->send_endpoint != NULL)
743 curl_easy_cleanup(ps->send_endpoint);
744 ps->send_endpoint = NULL;
748 msg = ps->pending_msgs_head;
751 if ((call_msg_cont == GNUNET_YES) && (msg->transmit_cont!=NULL))
753 msg->transmit_cont (msg->transmit_cont_cls,
755 call_msg_cont_result);
757 GNUNET_CONTAINER_DLL_remove(ps->pending_msgs_head,
758 ps->pending_msgs_head,
761 msg = ps->pending_msgs_head;
764 GNUNET_CONTAINER_DLL_remove(pc->head,pc->tail,ps);
768 /* no sessions left remove peer */
772 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
773 "No sessions left for peer `%s', removing context\n",
774 GNUNET_i2s(&pc->identity));
776 remove_peer_context_Iterator(plugin, &pc->identity.hashPubKey, pc);
784 static int check_localaddress (const struct sockaddr *addr, socklen_t addrlen)
787 int local = GNUNET_NO;
788 int af = addr->sa_family;
793 uint32_t netmask = 0x7F000000;
794 uint32_t address = ntohl (((struct sockaddr_in *) addr)->sin_addr.s_addr);
795 res = (address >> 24) ^ (netmask >> 24);
801 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
802 "Checking IPv4 address `%s': %s\n", GNUNET_a2s (addr, addrlen), (local==GNUNET_YES) ? "local" : "global");
808 if (IN6_IS_ADDR_LOOPBACK (&((struct sockaddr_in6 *) addr)->sin6_addr) ||
809 IN6_IS_ADDR_LINKLOCAL (&((struct sockaddr_in6 *) addr)->sin6_addr))
814 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
815 "Checking IPv6 address `%s' : %s\n", GNUNET_a2s (addr, addrlen), (local==GNUNET_YES) ? "local" : "global");
825 * Add the IP of our network interface to the list of
826 * our external IP addresses.
828 * @param cls the 'struct Plugin*'
829 * @param name name of the interface
830 * @param isDefault do we think this may be our default interface
831 * @param addr address of the interface
832 * @param addrlen number of bytes in addr
833 * @return GNUNET_OK to continue iterating
836 process_interfaces (void *cls,
839 const struct sockaddr *addr, socklen_t addrlen)
841 struct Plugin *plugin = cls;
842 struct IPv4HttpAddress * t4;
843 struct IPv6HttpAddress * t6;
846 if (plugin->use_localaddresses == GNUNET_NO)
848 if (GNUNET_YES == check_localaddress (addr, addrlen))
851 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
853 "Not notifying transport of address `%s' (local address)\n",
854 GNUNET_a2s (addr, addrlen));
861 GNUNET_assert(cls !=NULL);
862 af = addr->sa_family;
863 if ((af == AF_INET) &&
864 (plugin->use_ipv4 == GNUNET_YES) &&
865 (plugin->bind6_address == NULL) ) {
867 struct in_addr bnd_cmp = ((struct sockaddr_in *) addr)->sin_addr;
868 t4 = GNUNET_malloc(sizeof(struct IPv4HttpAddress));
869 // Not skipping loopback addresses
872 t4->ipv4_addr = ((struct sockaddr_in *) addr)->sin_addr.s_addr;
873 t4->port = htons (plugin->port_inbound);
874 if (plugin->bind4_address != NULL) {
875 if (0 == memcmp(&plugin->bind4_address->sin_addr, &bnd_cmp, sizeof (struct in_addr)))
877 GNUNET_CONTAINER_DLL_insert(plugin->ipv4_addr_head,
878 plugin->ipv4_addr_tail,t4);
879 plugin->env->notify_address(plugin->env->cls,
881 t4, sizeof (struct IPv4HttpAddress));
889 GNUNET_CONTAINER_DLL_insert (plugin->ipv4_addr_head,
890 plugin->ipv4_addr_tail,
892 plugin->env->notify_address(plugin->env->cls,
894 t4, sizeof (struct IPv4HttpAddress));
898 if ((af == AF_INET6) &&
899 (plugin->use_ipv6 == GNUNET_YES) &&
900 (plugin->bind4_address == NULL) ) {
902 struct in6_addr bnd_cmp6 = ((struct sockaddr_in6 *) addr)->sin6_addr;
904 t6 = GNUNET_malloc(sizeof(struct IPv6HttpAddress));
905 GNUNET_assert(t6 != NULL);
907 if (plugin->bind6_address != NULL) {
908 if (0 == memcmp(&plugin->bind6_address->sin6_addr,
910 sizeof (struct in6_addr))) {
911 memcpy (&t6->ipv6_addr,
912 &((struct sockaddr_in6 *) addr)->sin6_addr,
913 sizeof (struct in6_addr));
914 t6->port = htons (plugin->port_inbound);
915 plugin->env->notify_address(plugin->env->cls,
917 t6, sizeof (struct IPv6HttpAddress));
918 GNUNET_CONTAINER_DLL_insert(plugin->ipv6_addr_head,
919 plugin->ipv6_addr_tail,
926 memcpy (&t6->ipv6_addr,
927 &((struct sockaddr_in6 *) addr)->sin6_addr,
928 sizeof (struct in6_addr));
929 t6->port = htons (plugin->port_inbound);
930 GNUNET_CONTAINER_DLL_insert(plugin->ipv6_addr_head,plugin->ipv6_addr_tail,t6);
932 plugin->env->notify_address(plugin->env->cls,
934 t6, sizeof (struct IPv6HttpAddress));
941 * External logging function for MHD
942 * @param arg arguments
943 * @param fmt format string
944 * @param ap list of arguments
947 mhd_logger (void * arg,
953 vsnprintf(text, sizeof(text), fmt, ap);
955 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
962 mhd_termination_cb (void *cls,
963 struct MHD_Connection * connection,
964 void **httpSessionCache)
966 struct Session * ps = *httpSessionCache;
969 struct HTTP_PeerContext * pc = ps->peercontext;
970 struct Plugin *plugin = cls;
972 GNUNET_assert (cls != NULL);
973 plugin->current_connections--;
975 if (connection==ps->recv_endpoint)
977 #if DEBUG_CONNECTIONS
978 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
979 "Connection %X: inbound connection from peer `%s' was terminated\n",
981 GNUNET_i2s(&pc->identity));
983 ps->recv_active = GNUNET_NO;
984 ps->recv_connected = GNUNET_NO;
985 ps->recv_endpoint = NULL;
987 if (connection==ps->send_endpoint)
989 ps->send_active = GNUNET_NO;
990 ps->send_connected = GNUNET_NO;
991 ps->send_endpoint = NULL;
992 #if DEBUG_CONNECTIONS
993 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
994 "Connection %X: outbound connection from peer `%s' was terminated\n",
996 GNUNET_i2s(&pc->identity));
1000 /* if both connections disconnected, remove session */
1001 if ( (ps->send_connected == GNUNET_NO) &&
1002 (ps->recv_connected == GNUNET_NO) )
1004 GNUNET_STATISTICS_update (pc->plugin->env->stats,
1005 gettext_noop ("# HTTP inbound sessions for peers active"),
1008 remove_session(pc,ps,GNUNET_YES,GNUNET_SYSERR);
1014 * Callback called by MessageStreamTokenizer when a message has arrived
1015 * @param cls current session as closure
1016 * @param client clien
1017 * @param message the message to be forwarded to transport service
1020 mhd_write_mst_cb (void *cls,
1022 const struct GNUNET_MessageHeader *message)
1024 struct Session *ps = cls;
1025 struct HTTP_PeerContext *pc;
1026 struct GNUNET_TIME_Relative delay;
1028 GNUNET_assert(ps != NULL);
1029 pc = ps->peercontext;
1030 GNUNET_assert(pc != NULL);
1032 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1033 "Connection %X: Forwarding message to transport service, type %u and size %u from `%s' (`%s')\n",
1035 ntohs(message->type),
1036 ntohs(message->size),
1037 GNUNET_i2s(&(ps->peercontext)->identity),
1038 http_plugin_address_to_string(NULL,ps->addr,ps->addrlen));
1040 struct GNUNET_TRANSPORT_ATS_Information distance[2];
1041 distance[0].type = htonl (GNUNET_TRANSPORT_ATS_QUALITY_NET_DISTANCE);
1042 distance[0].value = htonl (1);
1043 distance[1].type = htonl (GNUNET_TRANSPORT_ATS_ARRAY_TERMINATOR);
1044 distance[1].value = htonl (0);
1045 delay = pc->plugin->env->receive (ps->peercontext->plugin->env->cls,
1048 (const struct GNUNET_TRANSPORT_ATS_Information *) &distance,
1054 if (pc->reset_task != GNUNET_SCHEDULER_NO_TASK)
1055 GNUNET_SCHEDULER_cancel (pc->reset_task);
1057 if (delay.rel_value > 0)
1060 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1061 "Connection %X: Inbound quota management: delay next read for %llu ms \n",
1065 pc->reset_task = GNUNET_SCHEDULER_add_delayed (delay, &reset_inbound_quota_delay, pc);
1071 * Check if incoming connection is accepted.
1072 * NOTE: Here every connection is accepted
1073 * @param cls plugin as closure
1074 * @param addr address of incoming connection
1075 * @param addr_len address length of incoming connection
1076 * @return MHD_YES if connection is accepted, MHD_NO if connection is rejected
1080 mhd_accept_cb (void *cls,
1081 const struct sockaddr *addr,
1084 struct Plugin *plugin = cls;
1085 GNUNET_assert (cls != NULL);
1087 if (plugin->max_connect_per_transport > plugin->current_connections)
1089 plugin->current_connections ++;
1097 * Callback called by MHD when it needs data to send
1098 * @param cls current session
1099 * @param pos position in buffer
1100 * @param buf the buffer to write data to
1101 * @param max max number of bytes available in buffer
1102 * @return bytes written to buffer
1105 mhd_send_callback (void *cls, uint64_t pos, char *buf, size_t max)
1107 struct Session * ps = cls;
1108 struct HTTP_PeerContext * pc;
1109 struct HTTP_Message * msg;
1112 GNUNET_assert (ps!=NULL);
1114 pc = ps->peercontext;
1115 msg = ps->pending_msgs_tail;
1116 if (ps->send_force_disconnect==GNUNET_YES)
1118 #if DEBUG_CONNECTIONS
1119 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1120 "Connection %X: outbound forced to disconnect\n",
1129 if ((msg->size-msg->pos) <= max)
1131 memcpy(buf,&msg->buf[msg->pos],(msg->size-msg->pos));
1132 bytes_read = msg->size-msg->pos;
1133 msg->pos+=(msg->size-msg->pos);
1137 memcpy(buf,&msg->buf[msg->pos],max);
1142 /* removing message */
1143 if (msg->pos==msg->size)
1145 if (NULL!=msg->transmit_cont)
1146 msg->transmit_cont (msg->transmit_cont_cls,&pc->identity,GNUNET_OK);
1147 ps->queue_length_cur -= msg->size;
1148 remove_http_message(ps,msg);
1151 #if DEBUG_CONNECTIONS
1152 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1153 "Connection %X: MHD has sent %u bytes\n",
1162 * Process GET or PUT request received via MHD. For
1163 * GET, queue response that will send back our pending
1164 * messages. For PUT, process incoming data and send
1165 * to GNUnet core. In either case, check if a session
1166 * already exists and create a new one if not.
1169 mhd_access_cb (void *cls,
1170 struct MHD_Connection *mhd_connection,
1173 const char *version,
1174 const char *upload_data,
1175 size_t * upload_data_size,
1176 void **httpSessionCache)
1178 struct Plugin *plugin = cls;
1179 struct MHD_Response *response;
1180 const union MHD_ConnectionInfo * conn_info;
1181 const struct sockaddr *client_addr;
1182 const struct sockaddr_in *addrin;
1183 const struct sockaddr_in6 *addrin6;
1184 char address[INET6_ADDRSTRLEN+14];
1185 struct GNUNET_PeerIdentity pi_in;
1187 struct IPv4HttpAddress ipv4addr;
1188 struct IPv6HttpAddress ipv6addr;
1189 struct HTTP_PeerContext *pc = NULL;
1190 struct Session *ps = NULL;
1191 struct Session *ps_tmp = NULL;
1192 int res = GNUNET_NO;
1194 size_t addr_len = 0 ;
1196 GNUNET_assert(cls !=NULL);
1198 if (NULL == *httpSessionCache)
1200 /* check url for peer identity , if invalid send HTTP 404*/
1201 size_t len = strlen(&url[1]);
1202 char * peer = GNUNET_malloc(104+1);
1204 if ( (len>104) && (url[104]==';'))
1206 char * id = GNUNET_malloc((len-104)+1);
1207 strcpy(id,&url[105]);
1208 memcpy(peer,&url[1],103);
1210 id_num = strtoul ( id, NULL , 10);
1213 res = GNUNET_CRYPTO_hash_from_string (peer, &(pi_in.hashPubKey));
1215 if ( GNUNET_SYSERR == res )
1217 response = MHD_create_response_from_data (strlen (HTTP_ERROR_RESPONSE),
1218 HTTP_ERROR_RESPONSE,
1220 res = MHD_queue_response (mhd_connection, MHD_HTTP_NOT_FOUND, response);
1221 MHD_destroy_response (response);
1222 #if DEBUG_CONNECTIONS
1224 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1225 "Peer has no valid ident, sent HTTP 1.1/404\n");
1227 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1228 "Peer has no valid ident, could not send error\n");
1235 ps = *httpSessionCache;
1236 pc = ps->peercontext;
1239 if (NULL == *httpSessionCache)
1241 /* get peer context */
1242 pc = GNUNET_CONTAINER_multihashmap_get (plugin->peers, &pi_in.hashPubKey);
1246 pc = GNUNET_malloc(sizeof (struct HTTP_PeerContext));
1247 pc->plugin = plugin;
1248 pc->session_id_counter=1;
1249 pc->last_session = NULL;
1250 memcpy(&pc->identity, &pi_in, sizeof(struct GNUNET_PeerIdentity));
1251 GNUNET_CONTAINER_multihashmap_put(plugin->peers,
1252 &pc->identity.hashPubKey,
1254 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
1255 GNUNET_STATISTICS_update (plugin->env->stats,
1256 gettext_noop ("# HTTP peers active"),
1261 conn_info = MHD_get_connection_info(mhd_connection, MHD_CONNECTION_INFO_CLIENT_ADDRESS );
1262 /* Incoming IPv4 connection */
1263 /* cast required for legacy MHD API < 0.9.6 */
1264 client_addr = (const struct sockaddr *) conn_info->client_addr;
1265 if ( AF_INET == client_addr->sa_family)
1267 addrin = (const struct sockaddr_in*) client_addr;
1268 inet_ntop(addrin->sin_family, &(addrin->sin_addr),address,INET_ADDRSTRLEN);
1269 memcpy(&ipv4addr.ipv4_addr,&(addrin->sin_addr),sizeof(struct in_addr));
1270 ipv4addr.port = addrin->sin_port;
1272 addr_len = sizeof(struct IPv4HttpAddress);
1274 /* Incoming IPv6 connection */
1275 if ( AF_INET6 == client_addr->sa_family)
1277 addrin6 = (const struct sockaddr_in6 *) client_addr;
1278 inet_ntop(addrin6->sin6_family, &(addrin6->sin6_addr),address,INET6_ADDRSTRLEN);
1279 memcpy(&ipv6addr.ipv6_addr,&(addrin6->sin6_addr),sizeof(struct in6_addr));
1280 ipv6addr.port = addrin6->sin6_port;
1282 addr_len = sizeof(struct IPv6HttpAddress);
1285 GNUNET_assert (addr != NULL);
1286 GNUNET_assert (addr_len != 0);
1289 /* only inbound sessions here */
1292 while (ps_tmp!=NULL)
1294 if ((ps_tmp->direction==INBOUND) && (ps_tmp->session_id == id_num) && (id_num!=0))
1296 if ((ps_tmp->recv_force_disconnect!=GNUNET_YES) && (ps_tmp->send_force_disconnect!=GNUNET_YES))
1300 ps_tmp=ps_tmp->next;
1305 ps = GNUNET_malloc(sizeof (struct Session));
1306 ps->addr = GNUNET_malloc(addr_len);
1307 memcpy(ps->addr,addr,addr_len);
1308 ps->addrlen = addr_len;
1309 ps->direction=INBOUND;
1310 ps->pending_msgs_head = NULL;
1311 ps->pending_msgs_tail = NULL;
1312 ps->send_connected=GNUNET_NO;
1313 ps->send_active=GNUNET_NO;
1314 ps->recv_connected=GNUNET_NO;
1315 ps->recv_active=GNUNET_NO;
1317 ps->session_id =id_num;
1318 ps->queue_length_cur = 0;
1319 ps->queue_length_max = GNUNET_SERVER_MAX_MESSAGE_SIZE;
1320 ps->url = create_url (plugin, ps->addr, ps->addrlen, ps->session_id);
1321 GNUNET_CONTAINER_DLL_insert(pc->head,pc->tail,ps);
1322 GNUNET_STATISTICS_update (plugin->env->stats,
1323 gettext_noop ("# HTTP inbound sessions for peers active"),
1328 *httpSessionCache = ps;
1329 if (ps->msgtok==NULL)
1330 ps->msgtok = GNUNET_SERVER_mst_create (&mhd_write_mst_cb, ps);
1332 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1333 "Connection %X: HTTP Daemon has new an incoming `%s' request from peer `%s' (`%s')\n",
1336 GNUNET_i2s(&pc->identity),
1337 http_plugin_address_to_string(NULL, ps->addr, ps->addrlen));
1341 /* Is it a PUT or a GET request */
1342 if (0 == strcmp (MHD_HTTP_METHOD_PUT, method))
1344 if (ps->recv_force_disconnect == GNUNET_YES)
1346 #if DEBUG_CONNECTIONS
1347 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1348 "Connection %X: inbound connection was forced to disconnect\n",ps);
1350 ps->recv_active = GNUNET_NO;
1353 if ((*upload_data_size == 0) && (ps->recv_active==GNUNET_NO))
1355 ps->recv_endpoint = mhd_connection;
1356 ps->recv_connected = GNUNET_YES;
1357 ps->recv_active = GNUNET_YES;
1358 ps->recv_force_disconnect = GNUNET_NO;
1359 #if DEBUG_CONNECTIONS
1360 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1361 "Connection %X: inbound PUT connection connected\n",ps);
1366 /* Transmission of all data complete */
1367 if ((*upload_data_size == 0) && (ps->recv_active == GNUNET_YES))
1369 response = MHD_create_response_from_data (strlen (HTTP_PUT_RESPONSE),
1372 res = MHD_queue_response (mhd_connection, MHD_HTTP_OK, response);
1373 #if DEBUG_CONNECTIONS
1374 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1375 "Connection %X: Sent HTTP/1.1: 200 OK as PUT Response\n",
1378 MHD_destroy_response (response);
1379 ps->recv_active=GNUNET_NO;
1383 /* Recieving data */
1384 if ((*upload_data_size > 0) && (ps->recv_active == GNUNET_YES))
1386 if (pc->delay.rel_value == 0)
1389 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1390 "Connection %X: PUT with %u bytes forwarded to MST\n",
1391 ps, *upload_data_size);
1393 res = GNUNET_SERVER_mst_receive(ps->msgtok, ps,
1394 upload_data, *upload_data_size,
1395 GNUNET_NO, GNUNET_NO);
1396 (*upload_data_size) = 0;
1401 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1402 "Connection %X: no inbound bandwidth available! Next read was delayed for %llu ms\n",
1404 ps->peercontext->delay.rel_value);
1412 if ( 0 == strcmp (MHD_HTTP_METHOD_GET, method) )
1414 if (ps->send_force_disconnect == GNUNET_YES)
1416 #if DEBUG_CONNECTIONS
1417 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1418 "Connection %X: outbound connection was forced to disconnect\n",
1421 ps->send_active = GNUNET_NO;
1424 ps->send_connected = GNUNET_YES;
1425 ps->send_active = GNUNET_YES;
1426 ps->send_endpoint = mhd_connection;
1427 ps->send_force_disconnect = GNUNET_NO;
1428 #if DEBUG_CONNECTIONS
1429 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1430 "Connection %X: inbound GET connection connected\n",
1433 response = MHD_create_response_from_callback(-1,32 * 1024, &mhd_send_callback, ps, NULL);
1434 res = MHD_queue_response (mhd_connection, MHD_HTTP_OK, response);
1435 MHD_destroy_response (response);
1443 * Function that queries MHD's select sets and
1444 * starts the task waiting for them.
1445 * @param plugin plugin
1446 * @param daemon_handle the MHD daemon handle
1447 * @return gnunet task identifier
1449 static GNUNET_SCHEDULER_TaskIdentifier
1450 http_server_daemon_prepare (struct Plugin *plugin,
1451 struct MHD_Daemon *daemon_handle)
1453 GNUNET_SCHEDULER_TaskIdentifier ret;
1457 struct GNUNET_NETWORK_FDSet *wrs;
1458 struct GNUNET_NETWORK_FDSet *wws;
1459 struct GNUNET_NETWORK_FDSet *wes;
1461 unsigned long long timeout;
1463 struct GNUNET_TIME_Relative tv;
1465 ret = GNUNET_SCHEDULER_NO_TASK;
1469 wrs = GNUNET_NETWORK_fdset_create ();
1470 wes = GNUNET_NETWORK_fdset_create ();
1471 wws = GNUNET_NETWORK_fdset_create ();
1473 GNUNET_assert (MHD_YES ==
1474 MHD_get_fdset (daemon_handle,
1479 haveto = MHD_get_timeout (daemon_handle, &timeout);
1480 if (haveto == MHD_YES)
1481 tv.rel_value = (uint64_t) timeout;
1483 tv = GNUNET_TIME_UNIT_SECONDS;
1484 GNUNET_NETWORK_fdset_copy_native (wrs, &rs, max + 1);
1485 GNUNET_NETWORK_fdset_copy_native (wws, &ws, max + 1);
1486 GNUNET_NETWORK_fdset_copy_native (wes, &es, max + 1);
1487 if (daemon_handle == plugin->http_server_daemon_v4)
1489 if (plugin->http_server_task_v4 != GNUNET_SCHEDULER_NO_TASK)
1491 GNUNET_SCHEDULER_cancel(plugin->http_server_task_v4);
1492 plugin->http_server_daemon_v4 = GNUNET_SCHEDULER_NO_TASK;
1495 ret = GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1496 GNUNET_SCHEDULER_NO_TASK,
1500 &http_server_daemon_v4_run,
1503 if (daemon_handle == plugin->http_server_daemon_v6)
1505 if (plugin->http_server_task_v6 != GNUNET_SCHEDULER_NO_TASK)
1507 GNUNET_SCHEDULER_cancel(plugin->http_server_task_v6);
1508 plugin->http_server_task_v6 = GNUNET_SCHEDULER_NO_TASK;
1511 ret = GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1512 GNUNET_SCHEDULER_NO_TASK,
1516 &http_server_daemon_v6_run,
1519 GNUNET_NETWORK_fdset_destroy (wrs);
1520 GNUNET_NETWORK_fdset_destroy (wws);
1521 GNUNET_NETWORK_fdset_destroy (wes);
1527 * Call MHD IPv4 to process pending requests and then go back
1528 * and schedule the next run.
1529 * @param cls plugin as closure
1530 * @param tc task context
1533 http_server_daemon_v4_run (void *cls,
1534 const struct GNUNET_SCHEDULER_TaskContext *tc)
1536 struct Plugin *plugin = cls;
1538 #if DEBUG_SCHEDULING
1539 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_READ_READY))
1540 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1541 "http_server_daemon_v4_run: GNUNET_SCHEDULER_REASON_READ_READY\n");
1542 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_WRITE_READY))
1543 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1544 "http_server_daemon_v4_run: GNUNET_SCHEDULER_REASON_WRITE_READY\n");
1545 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_TIMEOUT))
1546 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1547 "http_server_daemon_v4_run: GNUNET_SCHEDULER_REASON_TIMEOUT\n");
1548 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_STARTUP))
1549 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1550 "http_server_daemon_v4_run: GGNUNET_SCHEDULER_REASON_STARTUP\n");
1551 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1552 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1553 "http_server_daemon_v4_run: GGNUNET_SCHEDULER_REASON_SHUTDOWN\n");
1556 GNUNET_assert(cls !=NULL);
1557 plugin->http_server_task_v4 = GNUNET_SCHEDULER_NO_TASK;
1559 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1562 GNUNET_assert (MHD_YES == MHD_run (plugin->http_server_daemon_v4));
1563 plugin->http_server_task_v4 = http_server_daemon_prepare (plugin, plugin->http_server_daemon_v4);
1568 * Call MHD IPv6 to process pending requests and then go back
1569 * and schedule the next run.
1570 * @param cls plugin as closure
1571 * @param tc task context
1574 http_server_daemon_v6_run (void *cls,
1575 const struct GNUNET_SCHEDULER_TaskContext *tc)
1577 struct Plugin *plugin = cls;
1579 #if DEBUG_SCHEDULING
1580 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_READ_READY))
1581 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1582 "http_server_daemon_v6_run: GNUNET_SCHEDULER_REASON_READ_READY\n");
1583 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_WRITE_READY))
1584 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1585 "http_server_daemon_v6_run: GNUNET_SCHEDULER_REASON_WRITE_READY\n");
1586 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_TIMEOUT))
1587 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1588 "http_server_daemon_v6_run: GNUNET_SCHEDULER_REASON_TIMEOUT\n");
1589 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_STARTUP))
1590 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1591 "http_server_daemon_v6_run: GGNUNET_SCHEDULER_REASON_STARTUP\n");
1592 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1593 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1594 "http_server_daemon_v6_run: GGNUNET_SCHEDULER_REASON_SHUTDOWN\n");
1597 GNUNET_assert(cls !=NULL);
1598 plugin->http_server_task_v6 = GNUNET_SCHEDULER_NO_TASK;
1600 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1603 GNUNET_assert (MHD_YES == MHD_run (plugin->http_server_daemon_v6));
1604 plugin->http_server_task_v6 = http_server_daemon_prepare (plugin, plugin->http_server_daemon_v6);
1609 curl_get_header_cb( void *ptr,
1610 size_t size, size_t nmemb,
1613 struct Session * ps = stream;
1615 long http_result = 0;
1617 /* Getting last http result code */
1618 GNUNET_assert(NULL!=ps);
1619 if (ps->recv_connected==GNUNET_NO)
1621 res = curl_easy_getinfo(ps->recv_endpoint, CURLINFO_RESPONSE_CODE, &http_result);
1622 if (CURLE_OK == res)
1624 if (http_result == 200)
1626 ps->recv_connected = GNUNET_YES;
1627 ps->recv_active = GNUNET_YES;
1628 #if DEBUG_CONNECTIONS
1629 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: connected to recieve data\n",ps);
1631 // Calling send_check_connections again since receive is established
1632 send_check_connections (ps->peercontext->plugin, ps);
1639 size_t len = size * nmemb;
1641 if ((size * nmemb) < SIZE_MAX)
1642 tmp = GNUNET_malloc (len+1);
1644 if ((tmp != NULL) && (len > 0))
1646 memcpy(tmp,ptr,len);
1649 if (tmp[len-2] == 13)
1652 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1653 "Connection %X: Header: %s\n",
1656 GNUNET_free_non_null (tmp);
1659 return size * nmemb;
1664 * Callback called by libcurl when new headers arrive
1665 * Used to get HTTP result for curl operations
1666 * @param ptr stream to read from
1667 * @param size size of one char element
1668 * @param nmemb number of char elements
1669 * @param stream closure set by user
1670 * @return bytes read by function
1673 curl_put_header_cb(void *ptr,
1678 struct Session * ps = stream;
1681 size_t len = size * nmemb;
1682 long http_result = 0;
1685 /* Getting last http result code */
1686 GNUNET_assert(NULL!=ps);
1687 res = curl_easy_getinfo (ps->send_endpoint, CURLINFO_RESPONSE_CODE, &http_result);
1688 if (CURLE_OK == res)
1690 if ((http_result == 100) && (ps->send_connected==GNUNET_NO))
1692 ps->send_connected = GNUNET_YES;
1693 ps->send_active = GNUNET_YES;
1694 #if DEBUG_CONNECTIONS
1695 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1696 "Connection %X: connected to send data\n",
1700 if ((http_result == 200) && (ps->send_connected==GNUNET_YES))
1702 ps->send_connected = GNUNET_NO;
1703 ps->send_active = GNUNET_NO;
1704 #if DEBUG_CONNECTIONS
1705 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1706 "Connection %X: sending disconnected\n",
1713 if ((size * nmemb) < SIZE_MAX)
1714 tmp = GNUNET_malloc (len+1);
1716 if ((tmp != NULL) && (len > 0))
1718 memcpy(tmp,ptr,len);
1721 if (tmp[len-2] == 13)
1725 GNUNET_free_non_null (tmp);
1726 return size * nmemb;
1730 * Callback method used with libcurl
1731 * Method is called when libcurl needs to read data during sending
1732 * @param stream pointer where to write data
1733 * @param size size of an individual element
1734 * @param nmemb count of elements that can be written to the buffer
1735 * @param ptr source pointer, passed to the libcurl handle
1736 * @return bytes written to stream
1739 curl_send_cb(void *stream,
1740 size_t size, size_t nmemb,
1743 struct Session * ps = ptr;
1744 struct HTTP_Message * msg = ps->pending_msgs_tail;
1748 if (ps->send_active == GNUNET_NO)
1749 return CURL_READFUNC_PAUSE;
1750 if ( (ps->pending_msgs_tail == NULL) &&
1751 (ps->send_active == GNUNET_YES) )
1753 #if DEBUG_CONNECTIONS
1754 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1755 "Connection %X: No Message to send, pausing connection\n",
1758 ps->send_active = GNUNET_NO;
1759 return CURL_READFUNC_PAUSE;
1762 GNUNET_assert (msg!=NULL);
1765 if (msg->pos < msg->size)
1767 /* data fit in buffer */
1768 if ((msg->size - msg->pos) <= (size * nmemb))
1770 len = (msg->size - msg->pos);
1771 memcpy(stream, &msg->buf[msg->pos], len);
1778 memcpy(stream, &msg->buf[msg->pos], len);
1783 /* no data to send */
1789 if ( msg->pos == msg->size)
1791 #if DEBUG_CONNECTIONS
1792 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1793 "Connection %X: Message with %u bytes sent, removing message from queue\n",
1797 /* Calling transmit continuation */
1798 if (NULL != ps->pending_msgs_tail->transmit_cont)
1799 msg->transmit_cont (ps->pending_msgs_tail->transmit_cont_cls,
1800 &(ps->peercontext)->identity,
1802 ps->queue_length_cur -= msg->size;
1803 remove_http_message(ps, msg);
1810 curl_receive_mst_cb (void *cls,
1812 const struct GNUNET_MessageHeader *message)
1814 struct Session *ps = cls;
1815 struct GNUNET_TIME_Relative delay;
1816 GNUNET_assert(ps != NULL);
1818 struct HTTP_PeerContext *pc = ps->peercontext;
1819 GNUNET_assert(pc != NULL);
1821 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1822 "Connection %X: Forwarding message to transport service, type %u and size %u from `%s' (`%s')\n",
1824 ntohs(message->type),
1825 ntohs(message->size),
1826 GNUNET_i2s(&(pc->identity)),
1827 http_plugin_address_to_string(NULL,ps->addr,ps->addrlen));
1829 struct GNUNET_TRANSPORT_ATS_Information distance[2];
1830 distance[0].type = htonl (GNUNET_TRANSPORT_ATS_QUALITY_NET_DISTANCE);
1831 distance[0].value = htonl (1);
1832 distance[1].type = htonl (GNUNET_TRANSPORT_ATS_ARRAY_TERMINATOR);
1833 distance[1].value = htonl (0);
1835 delay = pc->plugin->env->receive (pc->plugin->env->cls,
1838 (const struct GNUNET_TRANSPORT_ATS_Information *) &distance, 2,
1843 if (pc->reset_task != GNUNET_SCHEDULER_NO_TASK)
1844 GNUNET_SCHEDULER_cancel (pc->reset_task);
1846 if (delay.rel_value > 0)
1849 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1850 "Connection %X: Inbound quota management: delay next read for %llu ms\n",
1851 ps, delay.rel_value);
1853 pc->reset_task = GNUNET_SCHEDULER_add_delayed (delay, &reset_inbound_quota_delay, pc);
1859 * Callback method used with libcurl
1860 * Method is called when libcurl needs to write data during sending
1861 * @param stream pointer where to write data
1862 * @param size size of an individual element
1863 * @param nmemb count of elements that can be written to the buffer
1864 * @param ptr destination pointer, passed to the libcurl handle
1865 * @return bytes read from stream
1868 curl_receive_cb( void *stream, size_t size, size_t nmemb, void *ptr)
1870 struct Session * ps = ptr;
1872 if (ps->peercontext->delay.rel_value > 0)
1875 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1876 "Connection %X: no inbound bandwidth available! Next read was delayed for %llu ms\n",
1877 ps, ps->peercontext->delay.rel_value);
1881 #if DEBUG_CONNECTIONS
1882 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1883 "Connection %X: %u bytes received\n",
1886 GNUNET_SERVER_mst_receive(ps->msgtok, ps,
1888 GNUNET_NO, GNUNET_NO);
1889 return (size * nmemb);
1894 curl_handle_finished (struct Plugin *plugin)
1896 struct Session *ps = NULL;
1897 struct HTTP_PeerContext *pc = NULL;
1898 struct CURLMsg *msg;
1899 struct HTTP_Message * cur_msg = NULL;
1906 msg = curl_multi_info_read (plugin->multi_handle, &msgs_in_queue);
1907 if ((msgs_in_queue == 0) || (msg == NULL))
1909 /* get session for affected curl handle */
1910 GNUNET_assert ( msg->easy_handle != NULL );
1911 curl_easy_getinfo(msg->easy_handle, CURLINFO_PRIVATE, &tmp);
1912 ps = (struct Session *) tmp;
1913 GNUNET_assert ( ps != NULL );
1914 pc = ps->peercontext;
1915 GNUNET_assert ( pc != NULL );
1919 if ( (msg->data.result != CURLE_OK) &&
1920 (msg->data.result != CURLE_GOT_NOTHING) )
1922 /* sending msg failed*/
1923 if (msg->easy_handle == ps->send_endpoint)
1925 #if DEBUG_CONNECTIONS
1926 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1927 _("Connection %X: HTTP PUT to peer `%s' (`%s') failed: `%s' `%s'\n"),
1929 GNUNET_i2s(&pc->identity),
1930 http_plugin_address_to_string(NULL, ps->addr, ps->addrlen),
1931 "curl_multi_perform",
1932 curl_easy_strerror (msg->data.result));
1934 ps->send_connected = GNUNET_NO;
1935 ps->send_active = GNUNET_NO;
1936 curl_multi_remove_handle(plugin->multi_handle,ps->send_endpoint);
1937 while (ps->pending_msgs_tail != NULL)
1939 cur_msg = ps->pending_msgs_tail;
1940 if ( NULL != cur_msg->transmit_cont)
1941 cur_msg->transmit_cont (cur_msg->transmit_cont_cls,&pc->identity,GNUNET_SYSERR);
1942 ps->queue_length_cur -= cur_msg->size;
1943 remove_http_message(ps,cur_msg);
1946 /* GET connection failed */
1947 if (msg->easy_handle == ps->recv_endpoint)
1949 #if DEBUG_CONNECTIONS
1950 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1951 _("Connection %X: HTTP GET to peer `%s' (`%s') failed: `%s' `%s'\n"),
1953 GNUNET_i2s(&pc->identity),
1954 http_plugin_address_to_string(NULL, ps->addr, ps->addrlen),
1955 "curl_multi_perform",
1956 curl_easy_strerror (msg->data.result));
1958 ps->recv_connected = GNUNET_NO;
1959 ps->recv_active = GNUNET_NO;
1960 curl_multi_remove_handle(plugin->multi_handle,ps->recv_endpoint);
1965 if (msg->easy_handle == ps->send_endpoint)
1967 GNUNET_assert (CURLE_OK == curl_easy_getinfo(msg->easy_handle, CURLINFO_RESPONSE_CODE, &http_result));
1968 #if DEBUG_CONNECTIONS
1969 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1970 "Connection %X: HTTP PUT connection to peer `%s' (`%s') was closed with HTTP code %u\n",
1972 GNUNET_i2s(&pc->identity),
1973 http_plugin_address_to_string(NULL, ps->addr, ps->addrlen),
1976 /* Calling transmit continuation */
1977 while (ps->pending_msgs_tail != NULL)
1979 cur_msg = ps->pending_msgs_tail;
1980 if ( NULL != cur_msg->transmit_cont)
1982 /* HTTP 1xx : Last message before here was informational */
1983 if ((http_result >=100) && (http_result < 200))
1984 cur_msg->transmit_cont (cur_msg->transmit_cont_cls,&pc->identity,GNUNET_OK);
1985 /* HTTP 2xx: successful operations */
1986 if ((http_result >=200) && (http_result < 300))
1987 cur_msg->transmit_cont (cur_msg->transmit_cont_cls,&pc->identity,GNUNET_OK);
1988 /* HTTP 3xx..5xx: error */
1989 if ((http_result >=300) && (http_result < 600))
1990 cur_msg->transmit_cont (cur_msg->transmit_cont_cls,&pc->identity,GNUNET_SYSERR);
1992 ps->queue_length_cur -= cur_msg->size;
1993 remove_http_message(ps,cur_msg);
1996 ps->send_connected = GNUNET_NO;
1997 ps->send_active = GNUNET_NO;
1998 curl_multi_remove_handle(plugin->multi_handle,ps->send_endpoint);
2000 if (msg->easy_handle == ps->recv_endpoint)
2002 #if DEBUG_CONNECTIONS
2003 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2004 "Connection %X: HTTP GET connection to peer `%s' (`%s') was closed with HTTP code %u\n",
2006 GNUNET_i2s(&pc->identity),
2007 http_plugin_address_to_string(NULL, ps->addr, ps->addrlen),
2010 ps->recv_connected = GNUNET_NO;
2011 ps->recv_active = GNUNET_NO;
2012 curl_multi_remove_handle(plugin->multi_handle,ps->recv_endpoint);
2014 plugin->current_connections--;
2016 if ((ps->recv_connected == GNUNET_NO) && (ps->send_connected == GNUNET_NO))
2017 remove_session (pc, ps, GNUNET_YES, GNUNET_SYSERR);
2023 while ( (msgs_in_queue > 0) );
2028 * Task performing curl operations
2029 * @param cls plugin as closure
2030 * @param tc gnunet scheduler task context
2032 static void curl_perform (void *cls,
2033 const struct GNUNET_SCHEDULER_TaskContext *tc)
2035 struct Plugin *plugin = cls;
2036 static unsigned int handles_last_run;
2040 GNUNET_assert(cls !=NULL);
2042 plugin->http_curl_task = GNUNET_SCHEDULER_NO_TASK;
2043 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
2048 mret = curl_multi_perform (plugin->multi_handle, &running);
2049 if ((running < handles_last_run) && (running>0))
2050 curl_handle_finished(plugin);
2051 handles_last_run = running;
2053 while (mret == CURLM_CALL_MULTI_PERFORM);
2054 curl_schedule(plugin);
2059 * Function setting up file descriptors and scheduling task to run
2061 * @param plugin plugin as closure
2062 * @return GNUNET_SYSERR for hard failure, GNUNET_OK for ok
2065 curl_schedule(struct Plugin *plugin)
2071 struct GNUNET_NETWORK_FDSet *grs;
2072 struct GNUNET_NETWORK_FDSet *gws;
2076 /* Cancel previous scheduled task */
2077 if (plugin->http_curl_task != GNUNET_SCHEDULER_NO_TASK)
2079 GNUNET_SCHEDULER_cancel(plugin->http_curl_task);
2080 plugin->http_curl_task = GNUNET_SCHEDULER_NO_TASK;
2087 mret = curl_multi_fdset (plugin->multi_handle, &rs, &ws, &es, &max);
2088 if (mret != CURLM_OK)
2090 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2091 _("%s failed at %s:%d: `%s'\n"),
2092 "curl_multi_fdset", __FILE__, __LINE__,
2093 curl_multi_strerror (mret));
2094 return GNUNET_SYSERR;
2096 mret = curl_multi_timeout (plugin->multi_handle, &to);
2097 if (mret != CURLM_OK)
2099 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2100 _("%s failed at %s:%d: `%s'\n"),
2101 "curl_multi_timeout", __FILE__, __LINE__,
2102 curl_multi_strerror (mret));
2103 return GNUNET_SYSERR;
2106 grs = GNUNET_NETWORK_fdset_create ();
2107 gws = GNUNET_NETWORK_fdset_create ();
2108 GNUNET_NETWORK_fdset_copy_native (grs, &rs, max + 1);
2109 GNUNET_NETWORK_fdset_copy_native (gws, &ws, max + 1);
2110 plugin->http_curl_task = GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
2111 GNUNET_SCHEDULER_NO_TASK,
2113 ? GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
2114 : GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, to),
2119 GNUNET_NETWORK_fdset_destroy (gws);
2120 GNUNET_NETWORK_fdset_destroy (grs);
2127 * Function to log curl debug messages with GNUNET_log
2128 * @param curl handle
2129 * @param type curl_infotype
2132 * @param cls closure
2136 curl_logger (CURL * curl,
2138 char * data, size_t size,
2141 if (type == CURLINFO_TEXT)
2144 memcpy(text,data,size);
2145 if (text[size-1] == '\n')
2150 text[size+1] = '\0';
2152 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2153 "CURL: Connection %X - %s",
2163 * Function setting up curl handle and selecting message to send
2165 * @param plugin plugin
2167 * @return GNUNET_SYSERR on failure, GNUNET_NO if connecting, GNUNET_YES if ok
2170 send_check_connections (struct Plugin *plugin,
2174 struct GNUNET_TIME_Relative timeout = GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT;
2176 if ((ps->direction == OUTBOUND) && (plugin->current_connections < plugin->max_connect_per_transport))
2178 /* RECV DIRECTION */
2179 /* Check if session is connected to receive data, otherwise connect to peer */
2181 if (ps->recv_connected == GNUNET_NO)
2183 int fresh = GNUNET_NO;
2184 if (ps->recv_endpoint == NULL)
2187 ps->recv_endpoint = curl_easy_init();
2190 curl_easy_setopt(ps->recv_endpoint, CURLOPT_VERBOSE, 1L);
2191 curl_easy_setopt(ps->recv_endpoint, CURLOPT_DEBUGFUNCTION , &curl_logger);
2192 curl_easy_setopt(ps->recv_endpoint, CURLOPT_DEBUGDATA , ps->recv_endpoint);
2195 curl_easy_setopt(ps->recv_endpoint, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1);
2196 curl_easy_setopt(ps->recv_endpoint, CURLOPT_SSL_VERIFYPEER, 0);
2197 curl_easy_setopt(ps->recv_endpoint, CURLOPT_SSL_VERIFYHOST, 0);
2199 curl_easy_setopt(ps->recv_endpoint, CURLOPT_URL, ps->url);
2200 curl_easy_setopt(ps->recv_endpoint, CURLOPT_HEADERFUNCTION, &curl_get_header_cb);
2201 curl_easy_setopt(ps->recv_endpoint, CURLOPT_WRITEHEADER, ps);
2202 curl_easy_setopt(ps->recv_endpoint, CURLOPT_READFUNCTION, curl_send_cb);
2203 curl_easy_setopt(ps->recv_endpoint, CURLOPT_READDATA, ps);
2204 curl_easy_setopt(ps->recv_endpoint, CURLOPT_WRITEFUNCTION, curl_receive_cb);
2205 curl_easy_setopt(ps->recv_endpoint, CURLOPT_WRITEDATA, ps);
2206 curl_easy_setopt(ps->recv_endpoint, CURLOPT_TIMEOUT, (long) timeout.rel_value);
2207 curl_easy_setopt(ps->recv_endpoint, CURLOPT_PRIVATE, ps);
2208 curl_easy_setopt(ps->recv_endpoint, CURLOPT_CONNECTTIMEOUT, HTTP_CONNECT_TIMEOUT);
2209 curl_easy_setopt(ps->recv_endpoint, CURLOPT_BUFFERSIZE, 2*GNUNET_SERVER_MAX_MESSAGE_SIZE);
2210 #if CURL_TCP_NODELAY
2211 curl_easy_setopt(ps->recv_endpoint, CURLOPT_TCP_NODELAY, 1);
2213 if (fresh==GNUNET_YES)
2215 mret = curl_multi_add_handle(plugin->multi_handle, ps->recv_endpoint);
2216 if (mret != CURLM_OK)
2218 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2219 _("Connection: %X: %s failed at %s:%d: `%s'\n"),
2221 "curl_multi_add_handle", __FILE__, __LINE__,
2222 curl_multi_strerror (mret));
2223 return GNUNET_SYSERR;
2226 if (plugin->http_curl_task != GNUNET_SCHEDULER_NO_TASK)
2228 GNUNET_SCHEDULER_cancel(plugin->http_curl_task);
2229 plugin->http_curl_task = GNUNET_SCHEDULER_NO_TASK;
2231 plugin->current_connections ++;
2232 plugin->http_curl_task = GNUNET_SCHEDULER_add_now (&curl_perform, plugin);
2235 /* waiting for receive direction */
2236 if (ps->recv_connected==GNUNET_NO)
2239 /* SEND DIRECTION */
2240 /* Check if session is connected to send data, otherwise connect to peer */
2241 if ((ps->send_connected == GNUNET_YES) && (ps->send_endpoint!= NULL))
2243 if (ps->send_active == GNUNET_YES)
2245 #if DEBUG_CONNECTIONS
2246 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2247 "Connection %X: outbound active, enqueueing message\n",
2252 if (ps->send_active == GNUNET_NO)
2254 #if DEBUG_CONNECTIONS
2255 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2256 "Connection %X: outbound paused, unpausing existing connection and enqueueing message\n",
2259 if (CURLE_OK == curl_easy_pause(ps->send_endpoint,CURLPAUSE_CONT))
2261 ps->send_active=GNUNET_YES;
2262 if (plugin->http_curl_task != GNUNET_SCHEDULER_NO_TASK)
2264 GNUNET_SCHEDULER_cancel(plugin->http_curl_task);
2265 plugin->http_curl_task = GNUNET_SCHEDULER_NO_TASK;
2267 plugin->http_curl_task = GNUNET_SCHEDULER_add_now (&curl_perform, plugin);
2271 return GNUNET_SYSERR;
2274 /* not connected, initiate connection */
2275 if ((ps->send_connected==GNUNET_NO) && (plugin->current_connections < plugin->max_connect_per_transport))
2277 int fresh = GNUNET_NO;
2278 if (NULL == ps->send_endpoint)
2280 ps->send_endpoint = curl_easy_init();
2283 GNUNET_assert (ps->send_endpoint != NULL);
2284 GNUNET_assert (NULL != ps->pending_msgs_tail);
2285 #if DEBUG_CONNECTIONS
2286 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2287 "Connection %X: outbound not connected, initiating connection\n",
2290 ps->send_active = GNUNET_NO;
2293 curl_easy_setopt(ps->send_endpoint, CURLOPT_VERBOSE, 1L);
2294 curl_easy_setopt(ps->send_endpoint, CURLOPT_DEBUGFUNCTION , &curl_logger);
2295 curl_easy_setopt(ps->send_endpoint, CURLOPT_DEBUGDATA , ps->send_endpoint);
2298 curl_easy_setopt (ps->send_endpoint, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1);
2299 curl_easy_setopt(ps->send_endpoint, CURLOPT_SSL_VERIFYPEER, 0);
2300 curl_easy_setopt(ps->send_endpoint, CURLOPT_SSL_VERIFYHOST, 0);
2302 curl_easy_setopt(ps->send_endpoint, CURLOPT_URL, ps->url);
2303 curl_easy_setopt(ps->send_endpoint, CURLOPT_PUT, 1L);
2304 curl_easy_setopt(ps->send_endpoint, CURLOPT_HEADERFUNCTION, &curl_put_header_cb);
2305 curl_easy_setopt(ps->send_endpoint, CURLOPT_WRITEHEADER, ps);
2306 curl_easy_setopt(ps->send_endpoint, CURLOPT_READFUNCTION, curl_send_cb);
2307 curl_easy_setopt(ps->send_endpoint, CURLOPT_READDATA, ps);
2308 curl_easy_setopt(ps->send_endpoint, CURLOPT_WRITEFUNCTION, curl_receive_cb);
2309 curl_easy_setopt(ps->send_endpoint, CURLOPT_READDATA, ps);
2310 curl_easy_setopt(ps->send_endpoint, CURLOPT_TIMEOUT, (long) timeout.rel_value);
2311 curl_easy_setopt(ps->send_endpoint, CURLOPT_PRIVATE, ps);
2312 curl_easy_setopt(ps->send_endpoint, CURLOPT_CONNECTTIMEOUT, HTTP_CONNECT_TIMEOUT);
2313 curl_easy_setopt(ps->send_endpoint, CURLOPT_BUFFERSIZE, 2 * GNUNET_SERVER_MAX_MESSAGE_SIZE);
2314 #if CURL_TCP_NODELAY
2315 curl_easy_setopt(ps->send_endpoint, CURLOPT_TCP_NODELAY, 1);
2317 if (fresh==GNUNET_YES)
2319 mret = curl_multi_add_handle(plugin->multi_handle, ps->send_endpoint);
2320 if (mret != CURLM_OK)
2322 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2323 _("Connection: %X: %s failed at %s:%d: `%s'\n"),
2325 "curl_multi_add_handle", __FILE__, __LINE__,
2326 curl_multi_strerror (mret));
2327 return GNUNET_SYSERR;
2331 if (plugin->http_curl_task != GNUNET_SCHEDULER_NO_TASK)
2333 GNUNET_SCHEDULER_cancel(plugin->http_curl_task);
2334 plugin->http_curl_task = GNUNET_SCHEDULER_NO_TASK;
2336 plugin->current_connections++;
2337 plugin->http_curl_task = GNUNET_SCHEDULER_add_now (&curl_perform, plugin);
2340 if (ps->direction == INBOUND)
2342 GNUNET_assert (NULL != ps->pending_msgs_tail);
2343 if ((ps->recv_connected==GNUNET_YES) && (ps->send_connected==GNUNET_YES) &&
2344 (ps->recv_force_disconnect==GNUNET_NO) && (ps->recv_force_disconnect==GNUNET_NO))
2347 return GNUNET_SYSERR;
2352 * select best session to transmit data to peer
2354 * @param pc peer context of target peer
2355 * @param addr address of target peer
2356 * @param addrlen address length
2357 * @param force_address does transport service enforce address?
2358 * @param session session passed by transport service
2359 * @return selected session
2362 static struct Session *
2363 send_select_session (struct HTTP_PeerContext *pc,
2364 const void * addr, size_t addrlen,
2366 struct Session * session)
2368 struct Session * tmp = NULL;
2369 int addr_given = GNUNET_NO;
2371 if ((addr!=NULL) && (addrlen>0))
2372 addr_given = GNUNET_YES;
2374 if (force_address == GNUNET_YES)
2376 /* check session given as argument */
2377 if ((session != NULL) && (addr_given == GNUNET_YES))
2379 if (0 == memcmp(session->addr, addr, addrlen))
2381 /* connection can not be used, since it is disconnected */
2382 if ( (session->recv_force_disconnect==GNUNET_NO) &&
2383 (session->send_force_disconnect==GNUNET_NO) )
2385 #if DEBUG_SESSION_SELECTION
2386 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2387 "Session %X selected: Using session passed by transport to send to forced address \n",
2394 /* check last session used */
2395 if ((pc->last_session != NULL)&& (addr_given == GNUNET_YES))
2397 if (0 == memcmp(pc->last_session->addr, addr, addrlen))
2399 /* connection can not be used, since it is disconnected */
2400 if ( (pc->last_session->recv_force_disconnect==GNUNET_NO) &&
2401 (pc->last_session->send_force_disconnect==GNUNET_NO) )
2403 #if DEBUG_SESSION_SELECTION
2404 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2405 "Session %X selected: Using last session used to send to forced address \n",
2408 return pc->last_session;
2412 /* find session in existing sessions */
2414 while ((tmp!=NULL) && (addr_given == GNUNET_YES))
2416 if (0 == memcmp(tmp->addr, addr, addrlen))
2418 /* connection can not be used, since it is disconnected */
2419 if ( (tmp->recv_force_disconnect==GNUNET_NO) &&
2420 (tmp->send_force_disconnect==GNUNET_NO) )
2422 #if DEBUG_SESSION_SELECTION
2423 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2424 "Session %X selected: Using existing session to send to forced address \n",
2432 /* no session to use */
2435 if ((force_address == GNUNET_NO) || (force_address == GNUNET_SYSERR))
2437 /* check session given as argument */
2438 if (session != NULL)
2440 /* connection can not be used, since it is disconnected */
2441 if ( (session->recv_force_disconnect==GNUNET_NO) &&
2442 (session->send_force_disconnect==GNUNET_NO) )
2444 #if DEBUG_SESSION_SELECTION
2445 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2446 "Session %X selected: Using session passed by transport to send not-forced address\n",
2452 /* check last session used */
2453 if (pc->last_session != NULL)
2455 /* connection can not be used, since it is disconnected */
2456 if ( (pc->last_session->recv_force_disconnect==GNUNET_NO) &&
2457 (pc->last_session->send_force_disconnect==GNUNET_NO) )
2459 #if DEBUG_SESSION_SELECTION
2460 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2461 "Session %X selected: Using last session to send to not-forced address\n",
2464 return pc->last_session;
2467 /* find session in existing sessions */
2471 /* connection can not be used, since it is disconnected */
2472 if ( (tmp->recv_force_disconnect==GNUNET_NO) &&
2473 (tmp->send_force_disconnect==GNUNET_NO) )
2475 #if DEBUG_SESSION_SELECTION
2476 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2477 "Session %X selected: Using existing session to send to not-forced address\n",
2491 * Function that can be used by the transport service to transmit
2492 * a message using the plugin. Note that in the case of a
2493 * peer disconnecting, the continuation MUST be called
2494 * prior to the disconnect notification itself. This function
2495 * will be called with this peer's HELLO message to initiate
2496 * a fresh connection to another peer.
2498 * @param cls closure
2499 * @param target who should receive this message
2500 * @param msgbuf the message to transmit
2501 * @param msgbuf_size number of bytes in 'msgbuf'
2502 * @param priority how important is the message (most plugins will
2503 * ignore message priority and just FIFO)
2504 * @param to how long to wait at most for the transmission (does not
2505 * require plugins to discard the message after the timeout,
2506 * just advisory for the desired delay; most plugins will ignore
2508 * @param session which session must be used (or NULL for "any")
2509 * @param addr the address to use (can be NULL if the plugin
2510 * is "on its own" (i.e. re-use existing TCP connection))
2511 * @param addrlen length of the address in bytes
2512 * @param force_address GNUNET_YES if the plugin MUST use the given address,
2513 * GNUNET_NO means the plugin may use any other address and
2514 * GNUNET_SYSERR means that only reliable existing
2515 * bi-directional connections should be used (regardless
2517 * @param cont continuation to call once the message has
2518 * been transmitted (or if the transport is ready
2519 * for the next transmission call; or if the
2520 * peer disconnected...); can be NULL
2521 * @param cont_cls closure for cont
2522 * @return number of bytes used (on the physical network, with overheads);
2523 * -1 on hard errors (i.e. address invalid); 0 is a legal value
2524 * and does NOT mean that the message was not transmitted (DV)
2527 http_plugin_send (void *cls,
2528 const struct GNUNET_PeerIdentity *target,
2531 unsigned int priority,
2532 struct GNUNET_TIME_Relative to,
2533 struct Session *session,
2537 GNUNET_TRANSPORT_TransmitContinuation cont,
2540 struct Plugin *plugin = cls;
2541 struct HTTP_Message *msg;
2542 struct HTTP_PeerContext * pc;
2543 struct Session * ps = NULL;
2545 GNUNET_assert(cls !=NULL);
2550 if (force_address == GNUNET_YES)
2551 GNUNET_asprintf(&force, "forced addr.");
2552 else if (force_address == GNUNET_NO)
2553 GNUNET_asprintf(&force, "any addr.");
2554 else if (force_address == GNUNET_SYSERR)
2555 GNUNET_asprintf(&force,"reliable bi-direc. address addr.");
2558 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2559 "Transport tells me to send %u bytes to `%s' using %s (%s) and session: %X\n",
2563 http_plugin_address_to_string(NULL, addr, addrlen),
2568 pc = GNUNET_CONTAINER_multihashmap_get (plugin->peers, &target->hashPubKey);
2572 pc = GNUNET_malloc(sizeof (struct HTTP_PeerContext));
2573 pc->plugin = plugin;
2574 pc->session_id_counter=1;
2575 pc->last_session = NULL;
2576 memcpy(&pc->identity, target, sizeof(struct GNUNET_PeerIdentity));
2577 GNUNET_CONTAINER_multihashmap_put (plugin->peers,
2578 &pc->identity.hashPubKey,
2580 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
2581 GNUNET_STATISTICS_update (plugin->env->stats,
2582 gettext_noop ("# HTTP peers active"),
2586 ps = send_select_session (pc, addr, addrlen, force_address, session);
2587 /* session not existing, but address forced -> creating new session */
2590 if ((addr!=NULL) && (addrlen!=0))
2592 ps = GNUNET_malloc(sizeof (struct Session));
2593 #if DEBUG_SESSION_SELECTION
2594 if (force_address == GNUNET_YES)
2595 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2596 "No existing connection & forced address: creating new session %X to peer %s\n",
2597 ps, GNUNET_i2s(target));
2598 if (force_address != GNUNET_YES)
2599 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2600 "No existing connection: creating new session %X to peer %s\n",
2601 ps, GNUNET_i2s(target));
2603 ps->addr = GNUNET_malloc(addrlen);
2604 memcpy(ps->addr,addr,addrlen);
2605 ps->addrlen = addrlen;
2606 ps->direction=OUTBOUND;
2607 ps->recv_connected = GNUNET_NO;
2608 ps->recv_force_disconnect = GNUNET_NO;
2609 ps->send_connected = GNUNET_NO;
2610 ps->send_force_disconnect = GNUNET_NO;
2611 ps->pending_msgs_head = NULL;
2612 ps->pending_msgs_tail = NULL;
2614 ps->session_id = pc->session_id_counter;
2615 ps->queue_length_cur = 0;
2616 ps->queue_length_max = GNUNET_SERVER_MAX_MESSAGE_SIZE;
2617 pc->session_id_counter++;
2618 ps->url = create_url (plugin, ps->addr, ps->addrlen, ps->session_id);
2619 if (ps->msgtok == NULL)
2620 ps->msgtok = GNUNET_SERVER_mst_create (&curl_receive_mst_cb, ps);
2621 GNUNET_CONTAINER_DLL_insert(pc->head,pc->tail,ps);
2622 GNUNET_STATISTICS_update (plugin->env->stats,
2623 gettext_noop ("# HTTP outbound sessions for peers active"),
2630 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2631 "No existing session found & and no address given: no way to send this message to peer `%s'!\n",
2632 GNUNET_i2s(target));
2634 return GNUNET_SYSERR;
2638 if (msgbuf_size >= (ps->queue_length_max - ps->queue_length_cur))
2640 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2641 "Queue %X full: %u bytes in queue available, message with %u is too big\n",
2643 (ps->queue_length_max - ps->queue_length_cur),
2645 //return GNUNET_SYSERR;
2649 msg = GNUNET_malloc (sizeof (struct HTTP_Message) + msgbuf_size);
2651 msg->size = msgbuf_size;
2653 msg->buf = (char *) &msg[1];
2654 msg->transmit_cont = cont;
2655 msg->transmit_cont_cls = cont_cls;
2656 memcpy (msg->buf,msgbuf, msgbuf_size);
2657 GNUNET_CONTAINER_DLL_insert (ps->pending_msgs_head,
2658 ps->pending_msgs_tail,
2660 ps->queue_length_cur += msgbuf_size;
2661 if (send_check_connections (plugin, ps) == GNUNET_SYSERR)
2662 return GNUNET_SYSERR;
2663 if (force_address != GNUNET_YES)
2664 pc->last_session = ps;
2665 if (pc->last_session==NULL)
2666 pc->last_session = ps;
2672 * Function that can be used to force the plugin to disconnect
2673 * from the given peer and cancel all previous transmissions
2674 * (and their continuationc).
2676 * @param cls closure
2677 * @param target peer from which to disconnect
2680 http_plugin_disconnect (void *cls,
2681 const struct GNUNET_PeerIdentity *target)
2683 struct Plugin *plugin = cls;
2684 struct HTTP_PeerContext *pc = NULL;
2685 struct Session *ps = NULL;
2687 pc = GNUNET_CONTAINER_multihashmap_get (plugin->peers, &target->hashPubKey);
2693 /* Telling transport that session is getting disconnected */
2694 plugin->env->session_end(plugin, target, ps);
2695 if (ps->direction==OUTBOUND)
2697 if (ps->send_endpoint!=NULL)
2699 //GNUNET_assert(CURLM_OK == curl_multi_remove_handle(plugin->multi_handle,ps->send_endpoint));
2700 //curl_easy_cleanup(ps->send_endpoint);
2701 //ps->send_endpoint=NULL;
2702 ps->send_force_disconnect = GNUNET_YES;
2704 if (ps->recv_endpoint!=NULL)
2706 //GNUNET_assert(CURLM_OK == curl_multi_remove_handle(plugin->multi_handle,ps->recv_endpoint));
2707 //curl_easy_cleanup(ps->recv_endpoint);
2708 //ps->recv_endpoint=NULL;
2709 ps->recv_force_disconnect = GNUNET_YES;
2712 if (ps->direction==INBOUND)
2714 ps->recv_force_disconnect = GNUNET_YES;
2715 ps->send_force_disconnect = GNUNET_YES;
2717 while (ps->pending_msgs_head!=NULL)
2718 remove_http_message(ps, ps->pending_msgs_head);
2719 ps->recv_active = GNUNET_NO;
2720 ps->send_active = GNUNET_NO;
2727 * Append our port and forward the result.
2729 * @param cls the 'struct PrettyPrinterContext*'
2730 * @param hostname hostname part of the address
2733 append_port (void *cls, const char *hostname)
2735 struct PrettyPrinterContext *ppc = cls;
2738 if (hostname == NULL)
2740 ppc->asc (ppc->asc_cls, NULL);
2744 GNUNET_asprintf (&ret, "%s://%s:%d", PROTOCOL_PREFIX, hostname, ppc->port);
2746 ppc->asc (ppc->asc_cls, ret);
2753 * Convert the transports address to a nice, human-readable
2756 * @param cls closure
2757 * @param type name of the transport that generated the address
2758 * @param addr one of the addresses of the host, NULL for the last address
2759 * the specific address format depends on the transport
2760 * @param addrlen length of the address
2761 * @param numeric should (IP) addresses be displayed in numeric form?
2762 * @param timeout after how long should we give up?
2763 * @param asc function to call on each string
2764 * @param asc_cls closure for asc
2767 http_plugin_address_pretty_printer (void *cls,
2772 struct GNUNET_TIME_Relative timeout,
2773 GNUNET_TRANSPORT_AddressStringCallback
2776 struct PrettyPrinterContext *ppc;
2779 struct sockaddr_in a4;
2780 struct sockaddr_in6 a6;
2781 const struct IPv4HttpAddress *t4;
2782 const struct IPv6HttpAddress *t6;
2785 if (addrlen == sizeof (struct IPv6HttpAddress))
2788 memset (&a6, 0, sizeof (a6));
2789 a6.sin6_family = AF_INET6;
2790 a6.sin6_port = t6->port;
2791 memcpy (&a6.sin6_addr,
2793 sizeof (struct in6_addr));
2794 port = ntohs (t6->port);
2798 else if (addrlen == sizeof (struct IPv4HttpAddress))
2801 memset (&a4, 0, sizeof (a4));
2802 a4.sin_family = AF_INET;
2803 a4.sin_port = t4->port;
2804 a4.sin_addr.s_addr = t4->ipv4_addr;
2805 port = ntohs (t4->ipv4_addr);
2811 /* invalid address */
2812 GNUNET_break_op (0);
2813 asc (asc_cls, NULL);
2816 ppc = GNUNET_malloc (sizeof (struct PrettyPrinterContext));
2818 ppc->asc_cls = asc_cls;
2820 GNUNET_RESOLVER_hostname_get (sb,
2822 !numeric, timeout, &append_port, ppc);
2828 * Another peer has suggested an address for this
2829 * peer and transport plugin. Check that this could be a valid
2830 * address. If so, consider adding it to the list
2833 * @param cls closure
2834 * @param addr pointer to the address
2835 * @param addrlen length of addr
2836 * @return GNUNET_OK if this is a plausible address for this peer
2840 http_plugin_address_suggested (void *cls,
2841 const void *addr, size_t addrlen)
2843 struct Plugin *plugin = cls;
2844 struct IPv4HttpAddress *v4;
2845 struct IPv6HttpAddress *v6;
2846 struct IPv4HttpAddressWrapper *w_tv4 = plugin->ipv4_addr_head;
2847 struct IPv6HttpAddressWrapper *w_tv6 = plugin->ipv6_addr_head;
2849 GNUNET_assert(cls !=NULL);
2850 if ((addrlen != sizeof (struct IPv4HttpAddress)) &&
2851 (addrlen != sizeof (struct IPv6HttpAddress)))
2852 return GNUNET_SYSERR;
2853 if (addrlen == sizeof (struct IPv4HttpAddress))
2855 v4 = (struct IPv4HttpAddress *) addr;
2856 if (plugin->bind4_address!=NULL)
2858 if (0 == memcmp (&plugin->bind4_address->sin_addr, &v4->ipv4_addr, sizeof(uint32_t)))
2861 return GNUNET_SYSERR;
2865 if (0==memcmp (&w_tv4->addr->ipv4_addr, &v4->ipv4_addr, sizeof(uint32_t)))
2867 w_tv4 = w_tv4->next;
2872 return GNUNET_SYSERR;
2874 if (addrlen == sizeof (struct IPv6HttpAddress))
2876 v6 = (struct IPv6HttpAddress *) addr;
2877 if (plugin->bind6_address!=NULL)
2879 if (0 == memcmp (&plugin->bind6_address->sin6_addr, &v6->ipv6_addr, sizeof(struct in6_addr)))
2882 return GNUNET_SYSERR;
2886 if (0 == memcmp (&w_tv6->addr->ipv6_addr, &v6->ipv6_addr, sizeof(struct in6_addr)))
2888 w_tv6 = w_tv6->next;
2893 return GNUNET_SYSERR;
2895 return GNUNET_SYSERR;
2900 * Function called for a quick conversion of the binary address to
2901 * a numeric address. Note that the caller must not free the
2902 * address and that the next call to this function is allowed
2903 * to override the address again.
2905 * @param cls closure
2906 * @param addr binary address
2907 * @param addrlen length of the address
2908 * @return string representing the same address
2911 http_plugin_address_to_string (void *cls,
2915 const struct IPv4HttpAddress *t4;
2916 const struct IPv6HttpAddress *t6;
2917 struct sockaddr_in a4;
2918 struct sockaddr_in6 a6;
2920 static char rbuf[INET6_ADDRSTRLEN + 13];
2924 if (addrlen == sizeof (struct IPv6HttpAddress))
2926 address = GNUNET_malloc (INET6_ADDRSTRLEN);
2928 a6.sin6_addr = t6->ipv6_addr;
2929 inet_ntop(AF_INET6, &(a6.sin6_addr),address,INET6_ADDRSTRLEN);
2930 port = ntohs(t6->port);
2932 else if (addrlen == sizeof (struct IPv4HttpAddress))
2934 address = GNUNET_malloc (INET_ADDRSTRLEN);
2936 a4.sin_addr.s_addr = t4->ipv4_addr;
2937 inet_ntop(AF_INET, &(a4.sin_addr),address,INET_ADDRSTRLEN);
2938 port = ntohs(t4->port);
2942 /* invalid address */
2946 res = GNUNET_snprintf (rbuf,
2952 GNUNET_free (address);
2953 GNUNET_assert(res != 0);
2958 * Function called by the NAT subsystem suggesting another peer wants
2959 * to connect to us via connection reversal. Try to connect back to the
2962 * @param cls closure
2963 * @param addr address to try
2964 * @param addrlen number of bytes in addr
2967 try_connection_reversal (void *cls,
2968 const struct sockaddr *addr,
2975 tcp_nat_cb_add_addr (void *cls,
2977 const struct sockaddr *addr,
2980 struct Plugin *plugin = cls;
2981 struct IPv4HttpAddress * t4 = NULL;
2982 struct IPv4HttpAddressWrapper * w_t4 = NULL;
2983 struct IPv6HttpAddress * t6 = NULL;
2984 struct IPv6HttpAddressWrapper * w_t6 = NULL;
2987 af = addr->sa_family;
2991 w_t4 = plugin->ipv4_addr_head;
2992 while (w_t4 != NULL)
2994 int res = memcmp(&w_t4->addr->ipv4_addr,
2995 &((struct sockaddr_in *) addr)->sin_addr,
2996 sizeof (struct in_addr));
3003 w_t4 = GNUNET_malloc(sizeof(struct IPv4HttpAddressWrapper));
3004 t4 = GNUNET_malloc(sizeof(struct IPv4HttpAddress));
3005 memcpy (&t4->ipv4_addr,
3006 &((struct sockaddr_in *) addr)->sin_addr,
3007 sizeof (struct in_addr));
3008 t4->port = htons (plugin->port_inbound);
3012 GNUNET_CONTAINER_DLL_insert(plugin->ipv4_addr_head,
3013 plugin->ipv4_addr_tail,w_t4);
3015 plugin->env->notify_address(plugin->env->cls,
3017 w_t4->addr, sizeof (struct IPv4HttpAddress));
3021 w_t6 = plugin->ipv6_addr_head;
3024 int res = memcmp(&w_t6->addr->ipv6_addr,
3025 &((struct sockaddr_in6 *) addr)->sin6_addr,
3026 sizeof (struct in6_addr));
3033 w_t6 = GNUNET_malloc(sizeof(struct IPv6HttpAddressWrapper));
3034 t6 = GNUNET_malloc(sizeof(struct IPv6HttpAddress));
3036 memcpy (&t6->ipv6_addr,
3037 &((struct sockaddr_in6 *) addr)->sin6_addr,
3038 sizeof (struct in6_addr));
3039 t6->port = htons (plugin->port_inbound);
3043 GNUNET_CONTAINER_DLL_insert(plugin->ipv6_addr_head,
3044 plugin->ipv6_addr_tail,w_t6);
3046 plugin->env->notify_address(plugin->env->cls,
3048 w_t6->addr, sizeof (struct IPv6HttpAddress));
3057 tcp_nat_cb_remove_addr (void *cls,
3059 const struct sockaddr *addr,
3062 struct Plugin *plugin = cls;
3063 struct IPv4HttpAddressWrapper * w_t4 = NULL;
3064 struct IPv6HttpAddressWrapper * w_t6 = NULL;
3067 af = addr->sa_family;
3071 w_t4 = plugin->ipv4_addr_head;
3072 while (w_t4 != NULL)
3074 int res = memcmp(&w_t4->addr->ipv4_addr,
3075 &((struct sockaddr_in *) addr)->sin_addr,
3076 sizeof (struct in_addr));
3083 plugin->env->notify_address(plugin->env->cls,
3085 w_t4->addr, sizeof (struct IPv4HttpAddress));
3087 GNUNET_CONTAINER_DLL_remove(plugin->ipv4_addr_head,
3088 plugin->ipv4_addr_tail,w_t4);
3089 GNUNET_free (w_t4->addr);
3093 w_t6 = plugin->ipv6_addr_head;
3094 while (w_t6 != NULL)
3096 int res = memcmp(&w_t6->addr->ipv6_addr,
3097 &((struct sockaddr_in6 *) addr)->sin6_addr,
3098 sizeof (struct in6_addr));
3105 plugin->env->notify_address(plugin->env->cls,
3107 w_t6->addr, sizeof (struct IPv6HttpAddress));
3109 GNUNET_CONTAINER_DLL_remove(plugin->ipv6_addr_head,
3110 plugin->ipv6_addr_tail,w_t6);
3111 GNUNET_free (w_t6->addr);
3121 * Our external IP address/port mapping has changed.
3123 * @param cls closure, the 'struct LocalAddrList'
3124 * @param add_remove GNUNET_YES to mean the new public IP address, GNUNET_NO to mean
3125 * the previous (now invalid) one
3126 * @param addr either the previous or the new public IP address
3127 * @param addrlen actual lenght of the address
3130 tcp_nat_port_map_callback (void *cls,
3132 const struct sockaddr *addr,
3135 GNUNET_assert(cls !=NULL );
3137 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
3138 "NPMC called to %s address `%s'\n",
3139 (add_remove == GNUNET_YES) ? "remove" : "add",
3140 GNUNET_a2s (addr, addrlen));
3142 /* convert 'addr' to our internal format */
3146 tcp_nat_cb_add_addr (cls, add_remove, addr, addrlen);
3149 tcp_nat_cb_remove_addr (cls, add_remove, addr, addrlen);
3156 * Notify transport service about address
3158 * @param cls the plugin
3162 address_notification (void *cls,
3163 const struct GNUNET_SCHEDULER_TaskContext *tc)
3165 struct Plugin *plugin = cls;
3167 GNUNET_OS_network_interfaces_list (&process_interfaces, plugin);
3172 * Exit point from the plugin.
3175 LIBGNUNET_PLUGIN_TRANSPORT_DONE (void *cls)
3177 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
3178 struct Plugin *plugin = api->cls;
3180 struct IPv4HttpAddressWrapper * w_t4;
3181 struct IPv6HttpAddressWrapper * w_t6;
3182 GNUNET_assert(cls !=NULL);
3184 if (plugin->nat != NULL)
3185 GNUNET_NAT_unregister (plugin->nat);
3187 if (plugin->http_server_daemon_v4 != NULL)
3189 MHD_stop_daemon (plugin->http_server_daemon_v4);
3190 plugin->http_server_daemon_v4 = NULL;
3192 if (plugin->http_server_daemon_v6 != NULL)
3194 MHD_stop_daemon (plugin->http_server_daemon_v6);
3195 plugin->http_server_daemon_v6 = NULL;
3197 if ( plugin->http_server_task_v4 != GNUNET_SCHEDULER_NO_TASK)
3199 GNUNET_SCHEDULER_cancel(plugin->http_server_task_v4);
3200 plugin->http_server_task_v4 = GNUNET_SCHEDULER_NO_TASK;
3202 if ( plugin->http_server_task_v6 != GNUNET_SCHEDULER_NO_TASK)
3204 GNUNET_SCHEDULER_cancel(plugin->http_server_task_v6);
3205 plugin->http_server_task_v6 = GNUNET_SCHEDULER_NO_TASK;
3208 while (plugin->ipv4_addr_head!=NULL)
3210 w_t4 = plugin->ipv4_addr_head;
3211 GNUNET_CONTAINER_DLL_remove(plugin->ipv4_addr_head,plugin->ipv4_addr_tail,w_t4);
3212 GNUNET_free(w_t4->addr);
3216 while (plugin->ipv6_addr_head!=NULL)
3218 w_t6 = plugin->ipv6_addr_head;
3219 GNUNET_CONTAINER_DLL_remove(plugin->ipv6_addr_head,plugin->ipv6_addr_tail,w_t6);
3220 GNUNET_free(w_t6->addr);
3224 /* free all peer information */
3225 if (plugin->peers!=NULL)
3227 GNUNET_CONTAINER_multihashmap_iterate (plugin->peers,
3228 &remove_peer_context_Iterator,
3230 GNUNET_CONTAINER_multihashmap_destroy (plugin->peers);
3232 if (plugin->multi_handle!=NULL)
3234 mret = curl_multi_cleanup(plugin->multi_handle);
3235 if (CURLM_OK != mret)
3236 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3237 "curl multihandle clean up failed\n");
3238 plugin->multi_handle = NULL;
3240 curl_global_cleanup();
3242 if ( plugin->http_curl_task != GNUNET_SCHEDULER_NO_TASK)
3244 GNUNET_SCHEDULER_cancel(plugin->http_curl_task);
3245 plugin->http_curl_task = GNUNET_SCHEDULER_NO_TASK;
3249 GNUNET_free_non_null (plugin->bind4_address);
3250 GNUNET_free_non_null (plugin->bind6_address);
3251 GNUNET_free_non_null(plugin->bind_hostname);
3253 GNUNET_free_non_null (plugin->crypto_init);
3254 GNUNET_free_non_null (plugin->cert);
3255 GNUNET_free_non_null (plugin->key);
3257 GNUNET_free (plugin);
3260 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3261 "Unload %s plugin complete...\n",
3269 load_certificate( const char * file )
3271 struct GNUNET_DISK_FileHandle * gn_file;
3275 if (0!=STAT(file, &fstat))
3277 text = GNUNET_malloc (fstat.st_size+1);
3278 gn_file = GNUNET_DISK_file_open(file, GNUNET_DISK_OPEN_READ, GNUNET_DISK_PERM_USER_READ);
3284 if (GNUNET_SYSERR == GNUNET_DISK_file_read(gn_file, text, fstat.st_size))
3287 GNUNET_DISK_file_close(gn_file);
3290 text[fstat.st_size] = '\0';
3291 GNUNET_DISK_file_close(gn_file);
3298 * Entry point for the plugin.
3301 LIBGNUNET_PLUGIN_TRANSPORT_INIT (void *cls)
3303 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
3304 struct Plugin *plugin;
3305 struct GNUNET_TRANSPORT_PluginFunctions *api;
3306 struct GNUNET_TIME_Relative gn_timeout;
3307 long long unsigned int port;
3308 unsigned long long tneigh;
3309 struct sockaddr **addrs;
3310 socklen_t *addrlens;
3312 char * component_name;
3314 char * key_file = NULL;
3315 char * cert_file = NULL;
3318 GNUNET_assert(cls !=NULL);
3320 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3321 "Starting %s plugin...\n",
3324 GNUNET_asprintf(&component_name,
3328 plugin = GNUNET_malloc (sizeof (struct Plugin));
3329 plugin->stats = env->stats;
3331 plugin->peers = NULL;
3332 plugin->bind4_address = NULL;
3333 plugin->bind6_address = NULL;
3334 plugin->use_ipv6 = GNUNET_YES;
3335 plugin->use_ipv4 = GNUNET_YES;
3336 plugin->use_localaddresses = GNUNET_NO;
3338 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
3340 api->send = &http_plugin_send;
3341 api->disconnect = &http_plugin_disconnect;
3342 api->address_pretty_printer = &http_plugin_address_pretty_printer;
3343 api->check_address = &http_plugin_address_suggested;
3344 api->address_to_string = &http_plugin_address_to_string;
3346 /* Hashing our identity to use it in URLs */
3347 GNUNET_CRYPTO_hash_to_enc (&(plugin->env->my_identity->hashPubKey),
3348 &plugin->my_ascii_hash_ident);
3351 if (GNUNET_CONFIGURATION_have_value (env->cfg, "TRANSPORT", "NEIGHBOUR_LIMIT"))
3353 GNUNET_CONFIGURATION_get_value_number (env->cfg,
3362 plugin->max_connect_per_transport = tneigh;
3366 if (GNUNET_CONFIGURATION_have_value (env->cfg,
3367 component_name, "USE_IPv6"))
3369 plugin->use_ipv6 = GNUNET_CONFIGURATION_get_value_yesno (env->cfg,
3374 if (GNUNET_CONFIGURATION_have_value (env->cfg,
3375 component_name, "USE_IPv4"))
3377 plugin->use_ipv4 = GNUNET_CONFIGURATION_get_value_yesno (env->cfg,
3378 component_name,"USE_IPv4");
3380 /* use local addresses? */
3382 if (GNUNET_CONFIGURATION_have_value (env->cfg,
3383 component_name, "USE_LOCALADDR"))
3385 plugin->use_localaddresses = GNUNET_CONFIGURATION_get_value_yesno (env->cfg,
3389 /* Reading port number from config file */
3391 GNUNET_CONFIGURATION_get_value_number (env->cfg,
3397 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
3399 _("Require valid port number for transport plugin `%s' in configuration!\n"),
3401 GNUNET_free(component_name);
3402 LIBGNUNET_PLUGIN_TRANSPORT_DONE (api);
3406 /* Reading ipv4 addresse to bind to from config file */
3407 if ( (plugin->use_ipv4==GNUNET_YES) &&
3408 (GNUNET_CONFIGURATION_have_value (env->cfg,
3409 component_name, "BINDTO4")))
3411 GNUNET_break (GNUNET_OK ==
3412 GNUNET_CONFIGURATION_get_value_string (env->cfg,
3415 &plugin->bind_hostname));
3416 plugin->bind4_address = GNUNET_malloc(sizeof(struct sockaddr_in));
3417 plugin->bind4_address->sin_family = AF_INET;
3418 plugin->bind4_address->sin_port = htons (port);
3420 if (plugin->bind_hostname!=NULL)
3422 if (inet_pton(AF_INET,plugin->bind_hostname, &plugin->bind4_address->sin_addr)<=0)
3424 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
3426 _("Misconfigured address to bind to in configuration!\n"));
3427 GNUNET_free(plugin->bind4_address);
3428 GNUNET_free(plugin->bind_hostname);
3429 plugin->bind_hostname = NULL;
3430 plugin->bind4_address = NULL;
3435 /* Reading ipv4 addresse to bind to from config file */
3436 if ( (plugin->use_ipv6==GNUNET_YES) &&
3437 (GNUNET_CONFIGURATION_have_value (env->cfg,
3438 component_name, "BINDTO6")))
3440 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (env->cfg,
3443 &plugin->bind_hostname))
3445 plugin->bind6_address = GNUNET_malloc(sizeof(struct sockaddr_in6));
3446 plugin->bind6_address->sin6_family = AF_INET6;
3447 plugin->bind6_address->sin6_port = htons (port);
3448 if (plugin->bind_hostname!=NULL)
3450 if (inet_pton(AF_INET6,plugin->bind_hostname, &plugin->bind6_address->sin6_addr)<=0)
3452 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
3454 _("Misconfigured address to bind to in configuration!\n"));
3455 GNUNET_free(plugin->bind6_address);
3456 GNUNET_free(plugin->bind_hostname);
3457 plugin->bind_hostname = NULL;
3458 plugin->bind6_address = NULL;
3465 /* Reading HTTPS crypto related configuration */
3466 /* Get crypto init string from config */
3467 if (GNUNET_CONFIGURATION_have_value (env->cfg,
3468 "transport-https", "CRYPTO_INIT"))
3470 GNUNET_CONFIGURATION_get_value_string (env->cfg,
3473 &plugin->crypto_init);
3477 GNUNET_asprintf(&plugin->crypto_init,"NORMAL");
3480 /* Get private key file from config */
3481 if (GNUNET_CONFIGURATION_have_value (env->cfg,
3482 "transport-https", "KEY_FILE"))
3484 GNUNET_CONFIGURATION_get_value_filename (env->cfg,
3490 GNUNET_asprintf(&key_file,"https.key");
3492 /* Get private key file from config */
3493 if (GNUNET_CONFIGURATION_have_value (env->cfg,"transport-https", "CERT_FILE"))
3495 GNUNET_CONFIGURATION_get_value_filename (env->cfg,
3500 if (cert_file==NULL)
3501 GNUNET_asprintf(&cert_file,"https.cert");
3503 /* read key & certificates from file */
3504 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3505 "Loading TLS certificate `%s' `%s'\n",
3506 key_file, cert_file);
3508 plugin->key = load_certificate( key_file );
3509 plugin->cert = load_certificate( cert_file );
3511 if ((plugin->key==NULL) || (plugin->cert==NULL))
3515 GNUNET_asprintf(&cmd,
3516 "gnunet-transport-certificate-creation %s %s",
3517 key_file, cert_file);
3518 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3519 "No usable TLS certificate found, creating certificate \n");
3523 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
3525 _("Could not create a new TLS certificate, shell script `%s' failed!\n"),cmd,
3527 GNUNET_free (key_file);
3528 GNUNET_free (cert_file);
3529 GNUNET_free (component_name);
3530 LIBGNUNET_PLUGIN_TRANSPORT_DONE(api);
3535 plugin->key = load_certificate( key_file );
3536 plugin->cert = load_certificate( cert_file );
3537 if ((plugin->key==NULL) || (plugin->cert==NULL))
3539 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
3541 _("No usable TLS certificate found and creating one failed! \n"),
3543 GNUNET_free (key_file);
3544 GNUNET_free (cert_file);
3545 GNUNET_free (component_name);
3547 LIBGNUNET_PLUGIN_TRANSPORT_DONE(api);
3551 GNUNET_free (key_file);
3552 GNUNET_free (cert_file);
3554 GNUNET_assert((plugin->key!=NULL) && (plugin->cert!=NULL));
3555 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "TLS certificate loaded\n");
3558 GNUNET_assert ((port > 0) && (port <= 65535));
3559 plugin->port_inbound = port;
3560 gn_timeout = GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT;
3561 unsigned int timeout = (gn_timeout.rel_value) / 1000;
3562 if ( (plugin->http_server_daemon_v6 == NULL) &&
3563 (plugin->use_ipv6 == GNUNET_YES) &&
3566 struct sockaddr * tmp = (struct sockaddr *) plugin->bind6_address;
3567 plugin->http_server_daemon_v6 = MHD_start_daemon (
3576 &mhd_accept_cb, plugin,
3577 &mhd_access_cb, plugin,
3578 MHD_OPTION_SOCK_ADDR, tmp,
3579 MHD_OPTION_CONNECTION_LIMIT, (unsigned int) plugin->max_connect_per_transport,
3581 MHD_OPTION_HTTPS_PRIORITIES, plugin->crypto_init,
3582 MHD_OPTION_HTTPS_MEM_KEY, plugin->key,
3583 MHD_OPTION_HTTPS_MEM_CERT, plugin->cert,
3585 MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) timeout,
3586 MHD_OPTION_CONNECTION_MEMORY_LIMIT, (size_t) (2 * GNUNET_SERVER_MAX_MESSAGE_SIZE),
3587 MHD_OPTION_NOTIFY_COMPLETED, &mhd_termination_cb, plugin,
3588 MHD_OPTION_EXTERNAL_LOGGER, mhd_logger, plugin->mhd_log,
3591 if ( (plugin->http_server_daemon_v4 == NULL) &&
3592 (plugin->use_ipv4 == GNUNET_YES) &&
3595 plugin->http_server_daemon_v4 = MHD_start_daemon (
3604 &mhd_accept_cb, plugin ,
3605 &mhd_access_cb, plugin,
3606 MHD_OPTION_SOCK_ADDR, (struct sockaddr_in *) plugin->bind4_address,
3607 MHD_OPTION_CONNECTION_LIMIT, (unsigned int) plugin->max_connect_per_transport,
3609 MHD_OPTION_HTTPS_PRIORITIES, plugin->crypto_init,
3611 MHD_OPTION_HTTPS_MEM_KEY, plugin->key,
3612 MHD_OPTION_HTTPS_MEM_CERT, plugin->cert,
3614 MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) timeout,
3615 MHD_OPTION_CONNECTION_MEMORY_LIMIT, (size_t) (2 * GNUNET_SERVER_MAX_MESSAGE_SIZE),
3616 MHD_OPTION_NOTIFY_COMPLETED, &mhd_termination_cb, plugin,
3617 MHD_OPTION_EXTERNAL_LOGGER, mhd_logger, plugin->mhd_log,
3620 if (plugin->http_server_daemon_v4 != NULL)
3621 plugin->http_server_task_v4 = http_server_daemon_prepare (plugin, plugin->http_server_daemon_v4);
3622 if (plugin->http_server_daemon_v6 != NULL)
3623 plugin->http_server_task_v6 = http_server_daemon_prepare (plugin, plugin->http_server_daemon_v6);
3626 if (plugin->http_server_task_v4 != GNUNET_SCHEDULER_NO_TASK)
3629 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3630 "Starting MHD with IPv4 bound to %s with port %u\n",
3631 (plugin->bind_hostname!=NULL) ? plugin->bind_hostname : "every address",port);
3634 else if ( (plugin->http_server_task_v6 != GNUNET_SCHEDULER_NO_TASK) &&
3635 (plugin->http_server_task_v4 != GNUNET_SCHEDULER_NO_TASK) )
3638 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3639 "Starting MHD with IPv6 bound to %s with port %u\n",
3640 (plugin->bind_hostname!=NULL) ? plugin->bind_hostname : "every address", port);
3643 else if ( (plugin->http_server_task_v6 != GNUNET_SCHEDULER_NO_TASK) &&
3644 (plugin->http_server_task_v4 == GNUNET_SCHEDULER_NO_TASK) )
3647 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3648 "Starting MHD with IPv4 and IPv6 bound to %s with port %u\n",
3649 (plugin->bind_hostname!=NULL) ? plugin->bind_hostname : "every address",
3656 if ((plugin->use_ipv6 == GNUNET_YES) && (plugin->use_ipv4 == GNUNET_YES))
3657 GNUNET_asprintf(&tmp,"with IPv4 and IPv6 enabled");
3658 if ((plugin->use_ipv6 == GNUNET_NO) && (plugin->use_ipv4 == GNUNET_YES))
3659 GNUNET_asprintf(&tmp,"with IPv4 enabled");
3660 if ((plugin->use_ipv6 == GNUNET_YES) && (plugin->use_ipv4 == GNUNET_NO))
3661 GNUNET_asprintf(&tmp,"with IPv6 enabled");
3662 if ((plugin->use_ipv6 == GNUNET_NO) && (plugin->use_ipv4 == GNUNET_NO))
3663 GNUNET_asprintf(&tmp,"with NO IP PROTOCOL enabled");
3664 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3665 _("HTTP Server with %s could not be started on port %u! %s plugin failed!\n"),
3666 tmp, port, PROTOCOL_PREFIX);
3668 GNUNET_free (component_name);
3669 LIBGNUNET_PLUGIN_TRANSPORT_DONE (api);
3673 /* Initializing cURL */
3674 curl_global_init(CURL_GLOBAL_ALL);
3675 plugin->multi_handle = curl_multi_init();
3677 if ( NULL == plugin->multi_handle )
3679 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
3681 _("Could not initialize curl multi handle, failed to start %s plugin!\n"),
3683 GNUNET_free(component_name);
3684 LIBGNUNET_PLUGIN_TRANSPORT_DONE (api);
3688 ret = GNUNET_SERVICE_get_server_addresses (component_name,
3693 if (ret != GNUNET_SYSERR)
3695 plugin->nat = GNUNET_NAT_register (env->cfg,
3699 (const struct sockaddr **) addrs,
3701 &tcp_nat_port_map_callback,
3702 &try_connection_reversal,
3707 GNUNET_assert (addrs[ret] != NULL);
3708 GNUNET_free (addrs[ret]);
3710 GNUNET_free_non_null (addrs);
3711 GNUNET_free_non_null (addrlens);
3715 plugin->nat = GNUNET_NAT_register (env->cfg,
3720 &try_connection_reversal,
3724 plugin->peers = GNUNET_CONTAINER_multihashmap_create (10);
3726 GNUNET_free(component_name);
3727 //GNUNET_SCHEDULER_add_now(address_notification, plugin);
3731 /* end of plugin_transport_http.c */