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 = ps->peercontext;
1026 struct GNUNET_TIME_Relative delay;
1028 GNUNET_assert(ps != NULL);
1029 GNUNET_assert(pc != NULL);
1031 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1032 "Connection %X: Forwarding message to transport service, type %u and size %u from `%s' (`%s')\n",
1034 ntohs(message->type),
1035 ntohs(message->size),
1036 GNUNET_i2s(&(ps->peercontext)->identity),
1037 http_plugin_address_to_string(NULL,ps->addr,ps->addrlen));
1039 struct GNUNET_TRANSPORT_ATS_Information distance[2];
1040 distance[0].type = htonl (GNUNET_TRANSPORT_ATS_QUALITY_NET_DISTANCE);
1041 distance[0].value = htonl (1);
1042 distance[1].type = htonl (GNUNET_TRANSPORT_ATS_ARRAY_TERMINATOR);
1043 distance[1].value = htonl (0);
1044 delay = pc->plugin->env->receive (ps->peercontext->plugin->env->cls,
1047 (const struct GNUNET_TRANSPORT_ATS_Information *) &distance,
1053 if (pc->reset_task != GNUNET_SCHEDULER_NO_TASK)
1054 GNUNET_SCHEDULER_cancel (pc->reset_task);
1056 if (delay.rel_value > 0)
1059 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1060 "Connection %X: Inbound quota management: delay next read for %llu ms \n",
1064 pc->reset_task = GNUNET_SCHEDULER_add_delayed (delay, &reset_inbound_quota_delay, pc);
1070 * Check if incoming connection is accepted.
1071 * NOTE: Here every connection is accepted
1072 * @param cls plugin as closure
1073 * @param addr address of incoming connection
1074 * @param addr_len address length of incoming connection
1075 * @return MHD_YES if connection is accepted, MHD_NO if connection is rejected
1079 mhd_accept_cb (void *cls,
1080 const struct sockaddr *addr,
1083 struct Plugin *plugin = cls;
1084 GNUNET_assert (cls != NULL);
1086 if (plugin->max_connect_per_transport > plugin->current_connections)
1088 plugin->current_connections ++;
1096 * Callback called by MHD when it needs data to send
1097 * @param cls current session
1098 * @param pos position in buffer
1099 * @param buf the buffer to write data to
1100 * @param max max number of bytes available in buffer
1101 * @return bytes written to buffer
1104 mhd_send_callback (void *cls, uint64_t pos, char *buf, size_t max)
1106 struct Session * ps = cls;
1107 struct HTTP_PeerContext * pc;
1108 struct HTTP_Message * msg;
1111 GNUNET_assert (ps!=NULL);
1113 pc = ps->peercontext;
1114 msg = ps->pending_msgs_tail;
1115 if (ps->send_force_disconnect==GNUNET_YES)
1117 #if DEBUG_CONNECTIONS
1118 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1119 "Connection %X: outbound forced to disconnect\n",
1128 if ((msg->size-msg->pos) <= max)
1130 memcpy(buf,&msg->buf[msg->pos],(msg->size-msg->pos));
1131 bytes_read = msg->size-msg->pos;
1132 msg->pos+=(msg->size-msg->pos);
1136 memcpy(buf,&msg->buf[msg->pos],max);
1141 /* removing message */
1142 if (msg->pos==msg->size)
1144 if (NULL!=msg->transmit_cont)
1145 msg->transmit_cont (msg->transmit_cont_cls,&pc->identity,GNUNET_OK);
1146 ps->queue_length_cur -= msg->size;
1147 remove_http_message(ps,msg);
1150 #if DEBUG_CONNECTIONS
1151 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1152 "Connection %X: MHD has sent %u bytes\n",
1161 * Process GET or PUT request received via MHD. For
1162 * GET, queue response that will send back our pending
1163 * messages. For PUT, process incoming data and send
1164 * to GNUnet core. In either case, check if a session
1165 * already exists and create a new one if not.
1168 mhd_access_cb (void *cls,
1169 struct MHD_Connection *mhd_connection,
1172 const char *version,
1173 const char *upload_data,
1174 size_t * upload_data_size,
1175 void **httpSessionCache)
1177 struct Plugin *plugin = cls;
1178 struct MHD_Response *response;
1179 const union MHD_ConnectionInfo * conn_info;
1180 const struct sockaddr *client_addr;
1181 const struct sockaddr_in *addrin;
1182 const struct sockaddr_in6 *addrin6;
1183 char address[INET6_ADDRSTRLEN+14];
1184 struct GNUNET_PeerIdentity pi_in;
1186 struct IPv4HttpAddress ipv4addr;
1187 struct IPv6HttpAddress ipv6addr;
1188 struct HTTP_PeerContext *pc = NULL;
1189 struct Session *ps = NULL;
1190 struct Session *ps_tmp = NULL;
1191 int res = GNUNET_NO;
1193 size_t addr_len = 0 ;
1195 GNUNET_assert(cls !=NULL);
1197 if (NULL == *httpSessionCache)
1199 /* check url for peer identity , if invalid send HTTP 404*/
1200 size_t len = strlen(&url[1]);
1201 char * peer = GNUNET_malloc(104+1);
1203 if ( (len>104) && (url[104]==';'))
1205 char * id = GNUNET_malloc((len-104)+1);
1206 strcpy(id,&url[105]);
1207 memcpy(peer,&url[1],103);
1209 id_num = strtoul ( id, NULL , 10);
1212 res = GNUNET_CRYPTO_hash_from_string (peer, &(pi_in.hashPubKey));
1214 if ( GNUNET_SYSERR == res )
1216 response = MHD_create_response_from_data (strlen (HTTP_ERROR_RESPONSE),
1217 HTTP_ERROR_RESPONSE,
1219 res = MHD_queue_response (mhd_connection, MHD_HTTP_NOT_FOUND, response);
1220 MHD_destroy_response (response);
1221 #if DEBUG_CONNECTIONS
1223 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1224 "Peer has no valid ident, sent HTTP 1.1/404\n");
1226 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1227 "Peer has no valid ident, could not send error\n");
1234 ps = *httpSessionCache;
1235 pc = ps->peercontext;
1238 if (NULL == *httpSessionCache)
1240 /* get peer context */
1241 pc = GNUNET_CONTAINER_multihashmap_get (plugin->peers, &pi_in.hashPubKey);
1245 pc = GNUNET_malloc(sizeof (struct HTTP_PeerContext));
1246 pc->plugin = plugin;
1247 pc->session_id_counter=1;
1248 pc->last_session = NULL;
1249 memcpy(&pc->identity, &pi_in, sizeof(struct GNUNET_PeerIdentity));
1250 GNUNET_CONTAINER_multihashmap_put(plugin->peers,
1251 &pc->identity.hashPubKey,
1253 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
1254 GNUNET_STATISTICS_update (plugin->env->stats,
1255 gettext_noop ("# HTTP peers active"),
1260 conn_info = MHD_get_connection_info(mhd_connection, MHD_CONNECTION_INFO_CLIENT_ADDRESS );
1261 /* Incoming IPv4 connection */
1262 /* cast required for legacy MHD API < 0.9.6 */
1263 client_addr = (const struct sockaddr *) conn_info->client_addr;
1264 if ( AF_INET == client_addr->sa_family)
1266 addrin = (const struct sockaddr_in*) client_addr;
1267 inet_ntop(addrin->sin_family, &(addrin->sin_addr),address,INET_ADDRSTRLEN);
1268 memcpy(&ipv4addr.ipv4_addr,&(addrin->sin_addr),sizeof(struct in_addr));
1269 ipv4addr.port = addrin->sin_port;
1271 addr_len = sizeof(struct IPv4HttpAddress);
1273 /* Incoming IPv6 connection */
1274 if ( AF_INET6 == client_addr->sa_family)
1276 addrin6 = (const struct sockaddr_in6 *) client_addr;
1277 inet_ntop(addrin6->sin6_family, &(addrin6->sin6_addr),address,INET6_ADDRSTRLEN);
1278 memcpy(&ipv6addr.ipv6_addr,&(addrin6->sin6_addr),sizeof(struct in6_addr));
1279 ipv6addr.port = addrin6->sin6_port;
1281 addr_len = sizeof(struct IPv6HttpAddress);
1284 GNUNET_assert (addr != NULL);
1285 GNUNET_assert (addr_len != 0);
1288 /* only inbound sessions here */
1291 while (ps_tmp!=NULL)
1293 if ((ps_tmp->direction==INBOUND) && (ps_tmp->session_id == id_num) && (id_num!=0))
1295 if ((ps_tmp->recv_force_disconnect!=GNUNET_YES) && (ps_tmp->send_force_disconnect!=GNUNET_YES))
1299 ps_tmp=ps_tmp->next;
1304 ps = GNUNET_malloc(sizeof (struct Session));
1305 ps->addr = GNUNET_malloc(addr_len);
1306 memcpy(ps->addr,addr,addr_len);
1307 ps->addrlen = addr_len;
1308 ps->direction=INBOUND;
1309 ps->pending_msgs_head = NULL;
1310 ps->pending_msgs_tail = NULL;
1311 ps->send_connected=GNUNET_NO;
1312 ps->send_active=GNUNET_NO;
1313 ps->recv_connected=GNUNET_NO;
1314 ps->recv_active=GNUNET_NO;
1316 ps->session_id =id_num;
1317 ps->queue_length_cur = 0;
1318 ps->queue_length_max = GNUNET_SERVER_MAX_MESSAGE_SIZE;
1319 ps->url = create_url (plugin, ps->addr, ps->addrlen, ps->session_id);
1320 GNUNET_CONTAINER_DLL_insert(pc->head,pc->tail,ps);
1321 GNUNET_STATISTICS_update (plugin->env->stats,
1322 gettext_noop ("# HTTP inbound sessions for peers active"),
1327 *httpSessionCache = ps;
1328 if (ps->msgtok==NULL)
1329 ps->msgtok = GNUNET_SERVER_mst_create (&mhd_write_mst_cb, ps);
1331 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1332 "Connection %X: HTTP Daemon has new an incoming `%s' request from peer `%s' (`%s')\n",
1335 GNUNET_i2s(&pc->identity),
1336 http_plugin_address_to_string(NULL, ps->addr, ps->addrlen));
1340 /* Is it a PUT or a GET request */
1341 if (0 == strcmp (MHD_HTTP_METHOD_PUT, method))
1343 if (ps->recv_force_disconnect == GNUNET_YES)
1345 #if DEBUG_CONNECTIONS
1346 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1347 "Connection %X: inbound connection was forced to disconnect\n",ps);
1349 ps->recv_active = GNUNET_NO;
1352 if ((*upload_data_size == 0) && (ps->recv_active==GNUNET_NO))
1354 ps->recv_endpoint = mhd_connection;
1355 ps->recv_connected = GNUNET_YES;
1356 ps->recv_active = GNUNET_YES;
1357 ps->recv_force_disconnect = GNUNET_NO;
1358 #if DEBUG_CONNECTIONS
1359 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1360 "Connection %X: inbound PUT connection connected\n",ps);
1365 /* Transmission of all data complete */
1366 if ((*upload_data_size == 0) && (ps->recv_active == GNUNET_YES))
1368 response = MHD_create_response_from_data (strlen (HTTP_PUT_RESPONSE),
1371 res = MHD_queue_response (mhd_connection, MHD_HTTP_OK, response);
1372 #if DEBUG_CONNECTIONS
1373 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1374 "Connection %X: Sent HTTP/1.1: 200 OK as PUT Response\n",
1377 MHD_destroy_response (response);
1378 ps->recv_active=GNUNET_NO;
1382 /* Recieving data */
1383 if ((*upload_data_size > 0) && (ps->recv_active == GNUNET_YES))
1385 if (pc->delay.rel_value == 0)
1388 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1389 "Connection %X: PUT with %u bytes forwarded to MST\n",
1390 ps, *upload_data_size);
1392 res = GNUNET_SERVER_mst_receive(ps->msgtok, ps,
1393 upload_data, *upload_data_size,
1394 GNUNET_NO, GNUNET_NO);
1395 (*upload_data_size) = 0;
1400 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1401 "Connection %X: no inbound bandwidth available! Next read was delayed for %llu ms\n",
1403 ps->peercontext->delay.rel_value);
1411 if ( 0 == strcmp (MHD_HTTP_METHOD_GET, method) )
1413 if (ps->send_force_disconnect == GNUNET_YES)
1415 #if DEBUG_CONNECTIONS
1416 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1417 "Connection %X: outbound connection was forced to disconnect\n",
1420 ps->send_active = GNUNET_NO;
1423 ps->send_connected = GNUNET_YES;
1424 ps->send_active = GNUNET_YES;
1425 ps->send_endpoint = mhd_connection;
1426 ps->send_force_disconnect = GNUNET_NO;
1427 #if DEBUG_CONNECTIONS
1428 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1429 "Connection %X: inbound GET connection connected\n",
1432 response = MHD_create_response_from_callback(-1,32 * 1024, &mhd_send_callback, ps, NULL);
1433 res = MHD_queue_response (mhd_connection, MHD_HTTP_OK, response);
1434 MHD_destroy_response (response);
1442 * Function that queries MHD's select sets and
1443 * starts the task waiting for them.
1444 * @param plugin plugin
1445 * @param daemon_handle the MHD daemon handle
1446 * @return gnunet task identifier
1448 static GNUNET_SCHEDULER_TaskIdentifier
1449 http_server_daemon_prepare (struct Plugin *plugin,
1450 struct MHD_Daemon *daemon_handle)
1452 GNUNET_SCHEDULER_TaskIdentifier ret;
1456 struct GNUNET_NETWORK_FDSet *wrs;
1457 struct GNUNET_NETWORK_FDSet *wws;
1458 struct GNUNET_NETWORK_FDSet *wes;
1460 unsigned long long timeout;
1462 struct GNUNET_TIME_Relative tv;
1464 ret = GNUNET_SCHEDULER_NO_TASK;
1468 wrs = GNUNET_NETWORK_fdset_create ();
1469 wes = GNUNET_NETWORK_fdset_create ();
1470 wws = GNUNET_NETWORK_fdset_create ();
1472 GNUNET_assert (MHD_YES ==
1473 MHD_get_fdset (daemon_handle,
1478 haveto = MHD_get_timeout (daemon_handle, &timeout);
1479 if (haveto == MHD_YES)
1480 tv.rel_value = (uint64_t) timeout;
1482 tv = GNUNET_TIME_UNIT_SECONDS;
1483 GNUNET_NETWORK_fdset_copy_native (wrs, &rs, max + 1);
1484 GNUNET_NETWORK_fdset_copy_native (wws, &ws, max + 1);
1485 GNUNET_NETWORK_fdset_copy_native (wes, &es, max + 1);
1486 if (daemon_handle == plugin->http_server_daemon_v4)
1488 if (plugin->http_server_task_v4 != GNUNET_SCHEDULER_NO_TASK)
1490 GNUNET_SCHEDULER_cancel(plugin->http_server_task_v4);
1491 plugin->http_server_daemon_v4 = GNUNET_SCHEDULER_NO_TASK;
1494 ret = GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1495 GNUNET_SCHEDULER_NO_TASK,
1499 &http_server_daemon_v4_run,
1502 if (daemon_handle == plugin->http_server_daemon_v6)
1504 if (plugin->http_server_task_v6 != GNUNET_SCHEDULER_NO_TASK)
1506 GNUNET_SCHEDULER_cancel(plugin->http_server_task_v6);
1507 plugin->http_server_task_v6 = GNUNET_SCHEDULER_NO_TASK;
1510 ret = GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1511 GNUNET_SCHEDULER_NO_TASK,
1515 &http_server_daemon_v6_run,
1518 GNUNET_NETWORK_fdset_destroy (wrs);
1519 GNUNET_NETWORK_fdset_destroy (wws);
1520 GNUNET_NETWORK_fdset_destroy (wes);
1526 * Call MHD IPv4 to process pending requests and then go back
1527 * and schedule the next run.
1528 * @param cls plugin as closure
1529 * @param tc task context
1532 http_server_daemon_v4_run (void *cls,
1533 const struct GNUNET_SCHEDULER_TaskContext *tc)
1535 struct Plugin *plugin = cls;
1537 #if DEBUG_SCHEDULING
1538 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_READ_READY))
1539 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1540 "http_server_daemon_v4_run: GNUNET_SCHEDULER_REASON_READ_READY\n");
1541 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_WRITE_READY))
1542 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1543 "http_server_daemon_v4_run: GNUNET_SCHEDULER_REASON_WRITE_READY\n");
1544 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_TIMEOUT))
1545 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1546 "http_server_daemon_v4_run: GNUNET_SCHEDULER_REASON_TIMEOUT\n");
1547 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_STARTUP))
1548 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1549 "http_server_daemon_v4_run: GGNUNET_SCHEDULER_REASON_STARTUP\n");
1550 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1551 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1552 "http_server_daemon_v4_run: GGNUNET_SCHEDULER_REASON_SHUTDOWN\n");
1555 GNUNET_assert(cls !=NULL);
1556 plugin->http_server_task_v4 = GNUNET_SCHEDULER_NO_TASK;
1558 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1561 GNUNET_assert (MHD_YES == MHD_run (plugin->http_server_daemon_v4));
1562 plugin->http_server_task_v4 = http_server_daemon_prepare (plugin, plugin->http_server_daemon_v4);
1567 * Call MHD IPv6 to process pending requests and then go back
1568 * and schedule the next run.
1569 * @param cls plugin as closure
1570 * @param tc task context
1573 http_server_daemon_v6_run (void *cls,
1574 const struct GNUNET_SCHEDULER_TaskContext *tc)
1576 struct Plugin *plugin = cls;
1578 #if DEBUG_SCHEDULING
1579 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_READ_READY))
1580 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1581 "http_server_daemon_v6_run: GNUNET_SCHEDULER_REASON_READ_READY\n");
1582 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_WRITE_READY))
1583 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1584 "http_server_daemon_v6_run: GNUNET_SCHEDULER_REASON_WRITE_READY\n");
1585 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_TIMEOUT))
1586 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1587 "http_server_daemon_v6_run: GNUNET_SCHEDULER_REASON_TIMEOUT\n");
1588 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_STARTUP))
1589 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1590 "http_server_daemon_v6_run: GGNUNET_SCHEDULER_REASON_STARTUP\n");
1591 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1592 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1593 "http_server_daemon_v6_run: GGNUNET_SCHEDULER_REASON_SHUTDOWN\n");
1596 GNUNET_assert(cls !=NULL);
1597 plugin->http_server_task_v6 = GNUNET_SCHEDULER_NO_TASK;
1599 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1602 GNUNET_assert (MHD_YES == MHD_run (plugin->http_server_daemon_v6));
1603 plugin->http_server_task_v6 = http_server_daemon_prepare (plugin, plugin->http_server_daemon_v6);
1608 curl_get_header_cb( void *ptr,
1609 size_t size, size_t nmemb,
1612 struct Session * ps = stream;
1614 long http_result = 0;
1616 /* Getting last http result code */
1617 GNUNET_assert(NULL!=ps);
1618 if (ps->recv_connected==GNUNET_NO)
1620 res = curl_easy_getinfo(ps->recv_endpoint, CURLINFO_RESPONSE_CODE, &http_result);
1621 if (CURLE_OK == res)
1623 if (http_result == 200)
1625 ps->recv_connected = GNUNET_YES;
1626 ps->recv_active = GNUNET_YES;
1627 #if DEBUG_CONNECTIONS
1628 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: connected to recieve data\n",ps);
1630 // Calling send_check_connections again since receive is established
1631 send_check_connections (ps->peercontext->plugin, ps);
1638 size_t len = size * nmemb;
1640 if ((size * nmemb) < SIZE_MAX)
1641 tmp = GNUNET_malloc (len+1);
1643 if ((tmp != NULL) && (len > 0))
1645 memcpy(tmp,ptr,len);
1648 if (tmp[len-2] == 13)
1651 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1652 "Connection %X: Header: %s\n",
1655 GNUNET_free_non_null (tmp);
1658 return size * nmemb;
1663 * Callback called by libcurl when new headers arrive
1664 * Used to get HTTP result for curl operations
1665 * @param ptr stream to read from
1666 * @param size size of one char element
1667 * @param nmemb number of char elements
1668 * @param stream closure set by user
1669 * @return bytes read by function
1672 curl_put_header_cb(void *ptr,
1677 struct Session * ps = stream;
1680 size_t len = size * nmemb;
1681 long http_result = 0;
1684 /* Getting last http result code */
1685 GNUNET_assert(NULL!=ps);
1686 res = curl_easy_getinfo (ps->send_endpoint, CURLINFO_RESPONSE_CODE, &http_result);
1687 if (CURLE_OK == res)
1689 if ((http_result == 100) && (ps->send_connected==GNUNET_NO))
1691 ps->send_connected = GNUNET_YES;
1692 ps->send_active = GNUNET_YES;
1693 #if DEBUG_CONNECTIONS
1694 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1695 "Connection %X: connected to send data\n",
1699 if ((http_result == 200) && (ps->send_connected==GNUNET_YES))
1701 ps->send_connected = GNUNET_NO;
1702 ps->send_active = GNUNET_NO;
1703 #if DEBUG_CONNECTIONS
1704 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1705 "Connection %X: sending disconnected\n",
1712 if ((size * nmemb) < SIZE_MAX)
1713 tmp = GNUNET_malloc (len+1);
1715 if ((tmp != NULL) && (len > 0))
1717 memcpy(tmp,ptr,len);
1720 if (tmp[len-2] == 13)
1724 GNUNET_free_non_null (tmp);
1725 return size * nmemb;
1729 * Callback method used with libcurl
1730 * Method is called when libcurl needs to read data during sending
1731 * @param stream pointer where to write data
1732 * @param size size of an individual element
1733 * @param nmemb count of elements that can be written to the buffer
1734 * @param ptr source pointer, passed to the libcurl handle
1735 * @return bytes written to stream
1738 curl_send_cb(void *stream,
1739 size_t size, size_t nmemb,
1742 struct Session * ps = ptr;
1743 struct HTTP_Message * msg = ps->pending_msgs_tail;
1747 if (ps->send_active == GNUNET_NO)
1748 return CURL_READFUNC_PAUSE;
1749 if ( (ps->pending_msgs_tail == NULL) &&
1750 (ps->send_active == GNUNET_YES) )
1752 #if DEBUG_CONNECTIONS
1753 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1754 "Connection %X: No Message to send, pausing connection\n",
1757 ps->send_active = GNUNET_NO;
1758 return CURL_READFUNC_PAUSE;
1761 GNUNET_assert (msg!=NULL);
1764 if (msg->pos < msg->size)
1766 /* data fit in buffer */
1767 if ((msg->size - msg->pos) <= (size * nmemb))
1769 len = (msg->size - msg->pos);
1770 memcpy(stream, &msg->buf[msg->pos], len);
1777 memcpy(stream, &msg->buf[msg->pos], len);
1782 /* no data to send */
1788 if ( msg->pos == msg->size)
1790 #if DEBUG_CONNECTIONS
1791 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1792 "Connection %X: Message with %u bytes sent, removing message from queue\n",
1796 /* Calling transmit continuation */
1797 if (NULL != ps->pending_msgs_tail->transmit_cont)
1798 msg->transmit_cont (ps->pending_msgs_tail->transmit_cont_cls,
1799 &(ps->peercontext)->identity,
1801 ps->queue_length_cur -= msg->size;
1802 remove_http_message(ps, msg);
1809 curl_receive_mst_cb (void *cls,
1811 const struct GNUNET_MessageHeader *message)
1813 struct Session *ps = cls;
1814 struct GNUNET_TIME_Relative delay;
1815 GNUNET_assert(ps != NULL);
1817 struct HTTP_PeerContext *pc = ps->peercontext;
1818 GNUNET_assert(pc != NULL);
1820 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1821 "Connection %X: Forwarding message to transport service, type %u and size %u from `%s' (`%s')\n",
1823 ntohs(message->type),
1824 ntohs(message->size),
1825 GNUNET_i2s(&(pc->identity)),
1826 http_plugin_address_to_string(NULL,ps->addr,ps->addrlen));
1828 struct GNUNET_TRANSPORT_ATS_Information distance[2];
1829 distance[0].type = htonl (GNUNET_TRANSPORT_ATS_QUALITY_NET_DISTANCE);
1830 distance[0].value = htonl (1);
1831 distance[1].type = htonl (GNUNET_TRANSPORT_ATS_ARRAY_TERMINATOR);
1832 distance[1].value = htonl (0);
1834 delay = pc->plugin->env->receive (pc->plugin->env->cls,
1837 (const struct GNUNET_TRANSPORT_ATS_Information *) &distance, 2,
1842 if (pc->reset_task != GNUNET_SCHEDULER_NO_TASK)
1843 GNUNET_SCHEDULER_cancel (pc->reset_task);
1845 if (delay.rel_value > 0)
1848 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1849 "Connection %X: Inbound quota management: delay next read for %llu ms\n",
1850 ps, delay.rel_value);
1852 pc->reset_task = GNUNET_SCHEDULER_add_delayed (delay, &reset_inbound_quota_delay, pc);
1858 * Callback method used with libcurl
1859 * Method is called when libcurl needs to write data during sending
1860 * @param stream pointer where to write data
1861 * @param size size of an individual element
1862 * @param nmemb count of elements that can be written to the buffer
1863 * @param ptr destination pointer, passed to the libcurl handle
1864 * @return bytes read from stream
1867 curl_receive_cb( void *stream, size_t size, size_t nmemb, void *ptr)
1869 struct Session * ps = ptr;
1871 if (ps->peercontext->delay.rel_value > 0)
1874 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1875 "Connection %X: no inbound bandwidth available! Next read was delayed for %llu ms\n",
1876 ps, ps->peercontext->delay.rel_value);
1880 #if DEBUG_CONNECTIONS
1881 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1882 "Connection %X: %u bytes received\n",
1885 GNUNET_SERVER_mst_receive(ps->msgtok, ps,
1887 GNUNET_NO, GNUNET_NO);
1888 return (size * nmemb);
1893 curl_handle_finished (struct Plugin *plugin)
1895 struct Session *ps = NULL;
1896 struct HTTP_PeerContext *pc = NULL;
1897 struct CURLMsg *msg;
1898 struct HTTP_Message * cur_msg = NULL;
1905 msg = curl_multi_info_read (plugin->multi_handle, &msgs_in_queue);
1906 if ((msgs_in_queue == 0) || (msg == NULL))
1908 /* get session for affected curl handle */
1909 GNUNET_assert ( msg->easy_handle != NULL );
1910 curl_easy_getinfo(msg->easy_handle, CURLINFO_PRIVATE, &tmp);
1911 ps = (struct Session *) tmp;
1912 GNUNET_assert ( ps != NULL );
1913 pc = ps->peercontext;
1914 GNUNET_assert ( pc != NULL );
1918 if ( (msg->data.result != CURLE_OK) &&
1919 (msg->data.result != CURLE_GOT_NOTHING) )
1921 /* sending msg failed*/
1922 if (msg->easy_handle == ps->send_endpoint)
1924 #if DEBUG_CONNECTIONS
1925 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1926 _("Connection %X: HTTP PUT to peer `%s' (`%s') failed: `%s' `%s'\n"),
1928 GNUNET_i2s(&pc->identity),
1929 http_plugin_address_to_string(NULL, ps->addr, ps->addrlen),
1930 "curl_multi_perform",
1931 curl_easy_strerror (msg->data.result));
1933 ps->send_connected = GNUNET_NO;
1934 ps->send_active = GNUNET_NO;
1935 curl_multi_remove_handle(plugin->multi_handle,ps->send_endpoint);
1936 while (ps->pending_msgs_tail != NULL)
1938 cur_msg = ps->pending_msgs_tail;
1939 if ( NULL != cur_msg->transmit_cont)
1940 cur_msg->transmit_cont (cur_msg->transmit_cont_cls,&pc->identity,GNUNET_SYSERR);
1941 ps->queue_length_cur -= cur_msg->size;
1942 remove_http_message(ps,cur_msg);
1945 /* GET connection failed */
1946 if (msg->easy_handle == ps->recv_endpoint)
1948 #if DEBUG_CONNECTIONS
1949 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1950 _("Connection %X: HTTP GET to peer `%s' (`%s') failed: `%s' `%s'\n"),
1952 GNUNET_i2s(&pc->identity),
1953 http_plugin_address_to_string(NULL, ps->addr, ps->addrlen),
1954 "curl_multi_perform",
1955 curl_easy_strerror (msg->data.result));
1957 ps->recv_connected = GNUNET_NO;
1958 ps->recv_active = GNUNET_NO;
1959 curl_multi_remove_handle(plugin->multi_handle,ps->recv_endpoint);
1964 if (msg->easy_handle == ps->send_endpoint)
1966 GNUNET_assert (CURLE_OK == curl_easy_getinfo(msg->easy_handle, CURLINFO_RESPONSE_CODE, &http_result));
1967 #if DEBUG_CONNECTIONS
1968 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1969 "Connection %X: HTTP PUT connection to peer `%s' (`%s') was closed with HTTP code %u\n",
1971 GNUNET_i2s(&pc->identity),
1972 http_plugin_address_to_string(NULL, ps->addr, ps->addrlen),
1975 /* Calling transmit continuation */
1976 while (ps->pending_msgs_tail != NULL)
1978 cur_msg = ps->pending_msgs_tail;
1979 if ( NULL != cur_msg->transmit_cont)
1981 /* HTTP 1xx : Last message before here was informational */
1982 if ((http_result >=100) && (http_result < 200))
1983 cur_msg->transmit_cont (cur_msg->transmit_cont_cls,&pc->identity,GNUNET_OK);
1984 /* HTTP 2xx: successful operations */
1985 if ((http_result >=200) && (http_result < 300))
1986 cur_msg->transmit_cont (cur_msg->transmit_cont_cls,&pc->identity,GNUNET_OK);
1987 /* HTTP 3xx..5xx: error */
1988 if ((http_result >=300) && (http_result < 600))
1989 cur_msg->transmit_cont (cur_msg->transmit_cont_cls,&pc->identity,GNUNET_SYSERR);
1991 ps->queue_length_cur -= cur_msg->size;
1992 remove_http_message(ps,cur_msg);
1995 ps->send_connected = GNUNET_NO;
1996 ps->send_active = GNUNET_NO;
1997 curl_multi_remove_handle(plugin->multi_handle,ps->send_endpoint);
1999 if (msg->easy_handle == ps->recv_endpoint)
2001 #if DEBUG_CONNECTIONS
2002 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2003 "Connection %X: HTTP GET connection to peer `%s' (`%s') was closed with HTTP code %u\n",
2005 GNUNET_i2s(&pc->identity),
2006 http_plugin_address_to_string(NULL, ps->addr, ps->addrlen),
2009 ps->recv_connected = GNUNET_NO;
2010 ps->recv_active = GNUNET_NO;
2011 curl_multi_remove_handle(plugin->multi_handle,ps->recv_endpoint);
2013 plugin->current_connections--;
2015 if ((ps->recv_connected == GNUNET_NO) && (ps->send_connected == GNUNET_NO))
2016 remove_session (pc, ps, GNUNET_YES, GNUNET_SYSERR);
2022 while ( (msgs_in_queue > 0) );
2027 * Task performing curl operations
2028 * @param cls plugin as closure
2029 * @param tc gnunet scheduler task context
2031 static void curl_perform (void *cls,
2032 const struct GNUNET_SCHEDULER_TaskContext *tc)
2034 struct Plugin *plugin = cls;
2035 static unsigned int handles_last_run;
2039 GNUNET_assert(cls !=NULL);
2041 plugin->http_curl_task = GNUNET_SCHEDULER_NO_TASK;
2042 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
2047 mret = curl_multi_perform (plugin->multi_handle, &running);
2048 if ((running < handles_last_run) && (running>0))
2049 curl_handle_finished(plugin);
2050 handles_last_run = running;
2052 while (mret == CURLM_CALL_MULTI_PERFORM);
2053 curl_schedule(plugin);
2058 * Function setting up file descriptors and scheduling task to run
2060 * @param plugin plugin as closure
2061 * @return GNUNET_SYSERR for hard failure, GNUNET_OK for ok
2064 curl_schedule(struct Plugin *plugin)
2070 struct GNUNET_NETWORK_FDSet *grs;
2071 struct GNUNET_NETWORK_FDSet *gws;
2075 /* Cancel previous scheduled task */
2076 if (plugin->http_curl_task != GNUNET_SCHEDULER_NO_TASK)
2078 GNUNET_SCHEDULER_cancel(plugin->http_curl_task);
2079 plugin->http_curl_task = GNUNET_SCHEDULER_NO_TASK;
2086 mret = curl_multi_fdset (plugin->multi_handle, &rs, &ws, &es, &max);
2087 if (mret != CURLM_OK)
2089 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2090 _("%s failed at %s:%d: `%s'\n"),
2091 "curl_multi_fdset", __FILE__, __LINE__,
2092 curl_multi_strerror (mret));
2093 return GNUNET_SYSERR;
2095 mret = curl_multi_timeout (plugin->multi_handle, &to);
2096 if (mret != CURLM_OK)
2098 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2099 _("%s failed at %s:%d: `%s'\n"),
2100 "curl_multi_timeout", __FILE__, __LINE__,
2101 curl_multi_strerror (mret));
2102 return GNUNET_SYSERR;
2105 grs = GNUNET_NETWORK_fdset_create ();
2106 gws = GNUNET_NETWORK_fdset_create ();
2107 GNUNET_NETWORK_fdset_copy_native (grs, &rs, max + 1);
2108 GNUNET_NETWORK_fdset_copy_native (gws, &ws, max + 1);
2109 plugin->http_curl_task = GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
2110 GNUNET_SCHEDULER_NO_TASK,
2112 ? GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
2113 : GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, to),
2118 GNUNET_NETWORK_fdset_destroy (gws);
2119 GNUNET_NETWORK_fdset_destroy (grs);
2126 * Function to log curl debug messages with GNUNET_log
2127 * @param curl handle
2128 * @param type curl_infotype
2131 * @param cls closure
2135 curl_logger (CURL * curl,
2137 char * data, size_t size,
2140 if (type == CURLINFO_TEXT)
2143 memcpy(text,data,size);
2144 if (text[size-1] == '\n')
2149 text[size+1] = '\0';
2151 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2152 "CURL: Connection %X - %s",
2162 * Function setting up curl handle and selecting message to send
2164 * @param plugin plugin
2166 * @return GNUNET_SYSERR on failure, GNUNET_NO if connecting, GNUNET_YES if ok
2169 send_check_connections (struct Plugin *plugin,
2173 struct GNUNET_TIME_Relative timeout = GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT;
2175 if ((ps->direction == OUTBOUND) && (plugin->current_connections < plugin->max_connect_per_transport))
2177 /* RECV DIRECTION */
2178 /* Check if session is connected to receive data, otherwise connect to peer */
2180 if (ps->recv_connected == GNUNET_NO)
2182 int fresh = GNUNET_NO;
2183 if (ps->recv_endpoint == NULL)
2186 ps->recv_endpoint = curl_easy_init();
2189 curl_easy_setopt(ps->recv_endpoint, CURLOPT_VERBOSE, 1L);
2190 curl_easy_setopt(ps->recv_endpoint, CURLOPT_DEBUGFUNCTION , &curl_logger);
2191 curl_easy_setopt(ps->recv_endpoint, CURLOPT_DEBUGDATA , ps->recv_endpoint);
2194 curl_easy_setopt(ps->recv_endpoint, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1);
2195 curl_easy_setopt(ps->recv_endpoint, CURLOPT_SSL_VERIFYPEER, 0);
2196 curl_easy_setopt(ps->recv_endpoint, CURLOPT_SSL_VERIFYHOST, 0);
2198 curl_easy_setopt(ps->recv_endpoint, CURLOPT_URL, ps->url);
2199 curl_easy_setopt(ps->recv_endpoint, CURLOPT_HEADERFUNCTION, &curl_get_header_cb);
2200 curl_easy_setopt(ps->recv_endpoint, CURLOPT_WRITEHEADER, ps);
2201 curl_easy_setopt(ps->recv_endpoint, CURLOPT_READFUNCTION, curl_send_cb);
2202 curl_easy_setopt(ps->recv_endpoint, CURLOPT_READDATA, ps);
2203 curl_easy_setopt(ps->recv_endpoint, CURLOPT_WRITEFUNCTION, curl_receive_cb);
2204 curl_easy_setopt(ps->recv_endpoint, CURLOPT_WRITEDATA, ps);
2205 curl_easy_setopt(ps->recv_endpoint, CURLOPT_TIMEOUT, (long) timeout.rel_value);
2206 curl_easy_setopt(ps->recv_endpoint, CURLOPT_PRIVATE, ps);
2207 curl_easy_setopt(ps->recv_endpoint, CURLOPT_CONNECTTIMEOUT, HTTP_CONNECT_TIMEOUT);
2208 curl_easy_setopt(ps->recv_endpoint, CURLOPT_BUFFERSIZE, 2*GNUNET_SERVER_MAX_MESSAGE_SIZE);
2209 #if CURL_TCP_NODELAY
2210 curl_easy_setopt(ps->recv_endpoint, CURLOPT_TCP_NODELAY, 1);
2212 if (fresh==GNUNET_YES)
2214 mret = curl_multi_add_handle(plugin->multi_handle, ps->recv_endpoint);
2215 if (mret != CURLM_OK)
2217 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2218 _("Connection: %X: %s failed at %s:%d: `%s'\n"),
2220 "curl_multi_add_handle", __FILE__, __LINE__,
2221 curl_multi_strerror (mret));
2222 return GNUNET_SYSERR;
2225 if (plugin->http_curl_task != GNUNET_SCHEDULER_NO_TASK)
2227 GNUNET_SCHEDULER_cancel(plugin->http_curl_task);
2228 plugin->http_curl_task = GNUNET_SCHEDULER_NO_TASK;
2230 plugin->current_connections ++;
2231 plugin->http_curl_task = GNUNET_SCHEDULER_add_now (&curl_perform, plugin);
2234 /* waiting for receive direction */
2235 if (ps->recv_connected==GNUNET_NO)
2238 /* SEND DIRECTION */
2239 /* Check if session is connected to send data, otherwise connect to peer */
2240 if ((ps->send_connected == GNUNET_YES) && (ps->send_endpoint!= NULL))
2242 if (ps->send_active == GNUNET_YES)
2244 #if DEBUG_CONNECTIONS
2245 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2246 "Connection %X: outbound active, enqueueing message\n",
2251 if (ps->send_active == GNUNET_NO)
2253 #if DEBUG_CONNECTIONS
2254 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2255 "Connection %X: outbound paused, unpausing existing connection and enqueueing message\n",
2258 if (CURLE_OK == curl_easy_pause(ps->send_endpoint,CURLPAUSE_CONT))
2260 ps->send_active=GNUNET_YES;
2261 if (plugin->http_curl_task != GNUNET_SCHEDULER_NO_TASK)
2263 GNUNET_SCHEDULER_cancel(plugin->http_curl_task);
2264 plugin->http_curl_task = GNUNET_SCHEDULER_NO_TASK;
2266 plugin->http_curl_task = GNUNET_SCHEDULER_add_now (&curl_perform, plugin);
2270 return GNUNET_SYSERR;
2273 /* not connected, initiate connection */
2274 if ((ps->send_connected==GNUNET_NO) && (plugin->current_connections < plugin->max_connect_per_transport))
2276 int fresh = GNUNET_NO;
2277 if (NULL == ps->send_endpoint)
2279 ps->send_endpoint = curl_easy_init();
2282 GNUNET_assert (ps->send_endpoint != NULL);
2283 GNUNET_assert (NULL != ps->pending_msgs_tail);
2284 #if DEBUG_CONNECTIONS
2285 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2286 "Connection %X: outbound not connected, initiating connection\n",
2289 ps->send_active = GNUNET_NO;
2292 curl_easy_setopt(ps->send_endpoint, CURLOPT_VERBOSE, 1L);
2293 curl_easy_setopt(ps->send_endpoint, CURLOPT_DEBUGFUNCTION , &curl_logger);
2294 curl_easy_setopt(ps->send_endpoint, CURLOPT_DEBUGDATA , ps->send_endpoint);
2297 curl_easy_setopt (ps->send_endpoint, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1);
2298 curl_easy_setopt(ps->send_endpoint, CURLOPT_SSL_VERIFYPEER, 0);
2299 curl_easy_setopt(ps->send_endpoint, CURLOPT_SSL_VERIFYHOST, 0);
2301 curl_easy_setopt(ps->send_endpoint, CURLOPT_URL, ps->url);
2302 curl_easy_setopt(ps->send_endpoint, CURLOPT_PUT, 1L);
2303 curl_easy_setopt(ps->send_endpoint, CURLOPT_HEADERFUNCTION, &curl_put_header_cb);
2304 curl_easy_setopt(ps->send_endpoint, CURLOPT_WRITEHEADER, ps);
2305 curl_easy_setopt(ps->send_endpoint, CURLOPT_READFUNCTION, curl_send_cb);
2306 curl_easy_setopt(ps->send_endpoint, CURLOPT_READDATA, ps);
2307 curl_easy_setopt(ps->send_endpoint, CURLOPT_WRITEFUNCTION, curl_receive_cb);
2308 curl_easy_setopt(ps->send_endpoint, CURLOPT_READDATA, ps);
2309 curl_easy_setopt(ps->send_endpoint, CURLOPT_TIMEOUT, (long) timeout.rel_value);
2310 curl_easy_setopt(ps->send_endpoint, CURLOPT_PRIVATE, ps);
2311 curl_easy_setopt(ps->send_endpoint, CURLOPT_CONNECTTIMEOUT, HTTP_CONNECT_TIMEOUT);
2312 curl_easy_setopt(ps->send_endpoint, CURLOPT_BUFFERSIZE, 2 * GNUNET_SERVER_MAX_MESSAGE_SIZE);
2313 #if CURL_TCP_NODELAY
2314 curl_easy_setopt(ps->send_endpoint, CURLOPT_TCP_NODELAY, 1);
2316 if (fresh==GNUNET_YES)
2318 mret = curl_multi_add_handle(plugin->multi_handle, ps->send_endpoint);
2319 if (mret != CURLM_OK)
2321 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2322 _("Connection: %X: %s failed at %s:%d: `%s'\n"),
2324 "curl_multi_add_handle", __FILE__, __LINE__,
2325 curl_multi_strerror (mret));
2326 return GNUNET_SYSERR;
2330 if (plugin->http_curl_task != GNUNET_SCHEDULER_NO_TASK)
2332 GNUNET_SCHEDULER_cancel(plugin->http_curl_task);
2333 plugin->http_curl_task = GNUNET_SCHEDULER_NO_TASK;
2335 plugin->current_connections++;
2336 plugin->http_curl_task = GNUNET_SCHEDULER_add_now (&curl_perform, plugin);
2339 if (ps->direction == INBOUND)
2341 GNUNET_assert (NULL != ps->pending_msgs_tail);
2342 if ((ps->recv_connected==GNUNET_YES) && (ps->send_connected==GNUNET_YES) &&
2343 (ps->recv_force_disconnect==GNUNET_NO) && (ps->recv_force_disconnect==GNUNET_NO))
2346 return GNUNET_SYSERR;
2351 * select best session to transmit data to peer
2353 * @param pc peer context of target peer
2354 * @param addr address of target peer
2355 * @param addrlen address length
2356 * @param force_address does transport service enforce address?
2357 * @param session session passed by transport service
2358 * @return selected session
2361 static struct Session *
2362 send_select_session (struct HTTP_PeerContext *pc,
2363 const void * addr, size_t addrlen,
2365 struct Session * session)
2367 struct Session * tmp = NULL;
2368 int addr_given = GNUNET_NO;
2370 if ((addr!=NULL) && (addrlen>0))
2371 addr_given = GNUNET_YES;
2373 if (force_address == GNUNET_YES)
2375 /* check session given as argument */
2376 if ((session != NULL) && (addr_given == GNUNET_YES))
2378 if (0 == memcmp(session->addr, addr, addrlen))
2380 /* connection can not be used, since it is disconnected */
2381 if ( (session->recv_force_disconnect==GNUNET_NO) &&
2382 (session->send_force_disconnect==GNUNET_NO) )
2384 #if DEBUG_SESSION_SELECTION
2385 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2386 "Session %X selected: Using session passed by transport to send to forced address \n",
2393 /* check last session used */
2394 if ((pc->last_session != NULL)&& (addr_given == GNUNET_YES))
2396 if (0 == memcmp(pc->last_session->addr, addr, addrlen))
2398 /* connection can not be used, since it is disconnected */
2399 if ( (pc->last_session->recv_force_disconnect==GNUNET_NO) &&
2400 (pc->last_session->send_force_disconnect==GNUNET_NO) )
2402 #if DEBUG_SESSION_SELECTION
2403 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2404 "Session %X selected: Using last session used to send to forced address \n",
2407 return pc->last_session;
2411 /* find session in existing sessions */
2413 while ((tmp!=NULL) && (addr_given == GNUNET_YES))
2415 if (0 == memcmp(tmp->addr, addr, addrlen))
2417 /* connection can not be used, since it is disconnected */
2418 if ( (tmp->recv_force_disconnect==GNUNET_NO) &&
2419 (tmp->send_force_disconnect==GNUNET_NO) )
2421 #if DEBUG_SESSION_SELECTION
2422 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2423 "Session %X selected: Using existing session to send to forced address \n",
2431 /* no session to use */
2434 if ((force_address == GNUNET_NO) || (force_address == GNUNET_SYSERR))
2436 /* check session given as argument */
2437 if (session != NULL)
2439 /* connection can not be used, since it is disconnected */
2440 if ( (session->recv_force_disconnect==GNUNET_NO) &&
2441 (session->send_force_disconnect==GNUNET_NO) )
2443 #if DEBUG_SESSION_SELECTION
2444 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2445 "Session %X selected: Using session passed by transport to send not-forced address\n",
2451 /* check last session used */
2452 if (pc->last_session != NULL)
2454 /* connection can not be used, since it is disconnected */
2455 if ( (pc->last_session->recv_force_disconnect==GNUNET_NO) &&
2456 (pc->last_session->send_force_disconnect==GNUNET_NO) )
2458 #if DEBUG_SESSION_SELECTION
2459 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2460 "Session %X selected: Using last session to send to not-forced address\n",
2463 return pc->last_session;
2466 /* find session in existing sessions */
2470 /* connection can not be used, since it is disconnected */
2471 if ( (tmp->recv_force_disconnect==GNUNET_NO) &&
2472 (tmp->send_force_disconnect==GNUNET_NO) )
2474 #if DEBUG_SESSION_SELECTION
2475 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2476 "Session %X selected: Using existing session to send to not-forced address\n",
2490 * Function that can be used by the transport service to transmit
2491 * a message using the plugin. Note that in the case of a
2492 * peer disconnecting, the continuation MUST be called
2493 * prior to the disconnect notification itself. This function
2494 * will be called with this peer's HELLO message to initiate
2495 * a fresh connection to another peer.
2497 * @param cls closure
2498 * @param target who should receive this message
2499 * @param msgbuf the message to transmit
2500 * @param msgbuf_size number of bytes in 'msgbuf'
2501 * @param priority how important is the message (most plugins will
2502 * ignore message priority and just FIFO)
2503 * @param to how long to wait at most for the transmission (does not
2504 * require plugins to discard the message after the timeout,
2505 * just advisory for the desired delay; most plugins will ignore
2507 * @param session which session must be used (or NULL for "any")
2508 * @param addr the address to use (can be NULL if the plugin
2509 * is "on its own" (i.e. re-use existing TCP connection))
2510 * @param addrlen length of the address in bytes
2511 * @param force_address GNUNET_YES if the plugin MUST use the given address,
2512 * GNUNET_NO means the plugin may use any other address and
2513 * GNUNET_SYSERR means that only reliable existing
2514 * bi-directional connections should be used (regardless
2516 * @param cont continuation to call once the message has
2517 * been transmitted (or if the transport is ready
2518 * for the next transmission call; or if the
2519 * peer disconnected...); can be NULL
2520 * @param cont_cls closure for cont
2521 * @return number of bytes used (on the physical network, with overheads);
2522 * -1 on hard errors (i.e. address invalid); 0 is a legal value
2523 * and does NOT mean that the message was not transmitted (DV)
2526 http_plugin_send (void *cls,
2527 const struct GNUNET_PeerIdentity *target,
2530 unsigned int priority,
2531 struct GNUNET_TIME_Relative to,
2532 struct Session *session,
2536 GNUNET_TRANSPORT_TransmitContinuation cont,
2539 struct Plugin *plugin = cls;
2540 struct HTTP_Message *msg;
2541 struct HTTP_PeerContext * pc;
2542 struct Session * ps = NULL;
2544 GNUNET_assert(cls !=NULL);
2549 if (force_address == GNUNET_YES)
2550 GNUNET_asprintf(&force, "forced addr.");
2551 else if (force_address == GNUNET_NO)
2552 GNUNET_asprintf(&force, "any addr.");
2553 else if (force_address == GNUNET_SYSERR)
2554 GNUNET_asprintf(&force,"reliable bi-direc. address addr.");
2557 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2558 "Transport tells me to send %u bytes to `%s' using %s (%s) and session: %X\n",
2562 http_plugin_address_to_string(NULL, addr, addrlen),
2567 pc = GNUNET_CONTAINER_multihashmap_get (plugin->peers, &target->hashPubKey);
2571 pc = GNUNET_malloc(sizeof (struct HTTP_PeerContext));
2572 pc->plugin = plugin;
2573 pc->session_id_counter=1;
2574 pc->last_session = NULL;
2575 memcpy(&pc->identity, target, sizeof(struct GNUNET_PeerIdentity));
2576 GNUNET_CONTAINER_multihashmap_put (plugin->peers,
2577 &pc->identity.hashPubKey,
2579 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
2580 GNUNET_STATISTICS_update (plugin->env->stats,
2581 gettext_noop ("# HTTP peers active"),
2585 ps = send_select_session (pc, addr, addrlen, force_address, session);
2586 /* session not existing, but address forced -> creating new session */
2589 if ((addr!=NULL) && (addrlen!=0))
2591 ps = GNUNET_malloc(sizeof (struct Session));
2592 #if DEBUG_SESSION_SELECTION
2593 if (force_address == GNUNET_YES)
2594 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2595 "No existing connection & forced address: creating new session %X to peer %s\n",
2596 ps, GNUNET_i2s(target));
2597 if (force_address != GNUNET_YES)
2598 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2599 "No existing connection: creating new session %X to peer %s\n",
2600 ps, GNUNET_i2s(target));
2602 ps->addr = GNUNET_malloc(addrlen);
2603 memcpy(ps->addr,addr,addrlen);
2604 ps->addrlen = addrlen;
2605 ps->direction=OUTBOUND;
2606 ps->recv_connected = GNUNET_NO;
2607 ps->recv_force_disconnect = GNUNET_NO;
2608 ps->send_connected = GNUNET_NO;
2609 ps->send_force_disconnect = GNUNET_NO;
2610 ps->pending_msgs_head = NULL;
2611 ps->pending_msgs_tail = NULL;
2613 ps->session_id = pc->session_id_counter;
2614 ps->queue_length_cur = 0;
2615 ps->queue_length_max = GNUNET_SERVER_MAX_MESSAGE_SIZE;
2616 pc->session_id_counter++;
2617 ps->url = create_url (plugin, ps->addr, ps->addrlen, ps->session_id);
2618 if (ps->msgtok == NULL)
2619 ps->msgtok = GNUNET_SERVER_mst_create (&curl_receive_mst_cb, ps);
2620 GNUNET_CONTAINER_DLL_insert(pc->head,pc->tail,ps);
2621 GNUNET_STATISTICS_update (plugin->env->stats,
2622 gettext_noop ("# HTTP outbound sessions for peers active"),
2629 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2630 "No existing session found & and no address given: no way to send this message to peer `%s'!\n",
2631 GNUNET_i2s(target));
2633 return GNUNET_SYSERR;
2637 if (msgbuf_size >= (ps->queue_length_max - ps->queue_length_cur))
2639 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2640 "Queue %X full: %u bytes in queue available, message with %u is too big\n",
2642 (ps->queue_length_max - ps->queue_length_cur),
2644 //return GNUNET_SYSERR;
2648 msg = GNUNET_malloc (sizeof (struct HTTP_Message) + msgbuf_size);
2650 msg->size = msgbuf_size;
2652 msg->buf = (char *) &msg[1];
2653 msg->transmit_cont = cont;
2654 msg->transmit_cont_cls = cont_cls;
2655 memcpy (msg->buf,msgbuf, msgbuf_size);
2656 GNUNET_CONTAINER_DLL_insert (ps->pending_msgs_head,
2657 ps->pending_msgs_tail,
2659 ps->queue_length_cur += msgbuf_size;
2660 if (send_check_connections (plugin, ps) == GNUNET_SYSERR)
2661 return GNUNET_SYSERR;
2662 if (force_address != GNUNET_YES)
2663 pc->last_session = ps;
2664 if (pc->last_session==NULL)
2665 pc->last_session = ps;
2671 * Function that can be used to force the plugin to disconnect
2672 * from the given peer and cancel all previous transmissions
2673 * (and their continuationc).
2675 * @param cls closure
2676 * @param target peer from which to disconnect
2679 http_plugin_disconnect (void *cls,
2680 const struct GNUNET_PeerIdentity *target)
2682 struct Plugin *plugin = cls;
2683 struct HTTP_PeerContext *pc = NULL;
2684 struct Session *ps = NULL;
2686 pc = GNUNET_CONTAINER_multihashmap_get (plugin->peers, &target->hashPubKey);
2692 /* Telling transport that session is getting disconnected */
2693 plugin->env->session_end(plugin, target, ps);
2694 if (ps->direction==OUTBOUND)
2696 if (ps->send_endpoint!=NULL)
2698 //GNUNET_assert(CURLM_OK == curl_multi_remove_handle(plugin->multi_handle,ps->send_endpoint));
2699 //curl_easy_cleanup(ps->send_endpoint);
2700 //ps->send_endpoint=NULL;
2701 ps->send_force_disconnect = GNUNET_YES;
2703 if (ps->recv_endpoint!=NULL)
2705 //GNUNET_assert(CURLM_OK == curl_multi_remove_handle(plugin->multi_handle,ps->recv_endpoint));
2706 //curl_easy_cleanup(ps->recv_endpoint);
2707 //ps->recv_endpoint=NULL;
2708 ps->recv_force_disconnect = GNUNET_YES;
2711 if (ps->direction==INBOUND)
2713 ps->recv_force_disconnect = GNUNET_YES;
2714 ps->send_force_disconnect = GNUNET_YES;
2716 while (ps->pending_msgs_head!=NULL)
2717 remove_http_message(ps, ps->pending_msgs_head);
2718 ps->recv_active = GNUNET_NO;
2719 ps->send_active = GNUNET_NO;
2726 * Append our port and forward the result.
2728 * @param cls the 'struct PrettyPrinterContext*'
2729 * @param hostname hostname part of the address
2732 append_port (void *cls, const char *hostname)
2734 struct PrettyPrinterContext *ppc = cls;
2737 if (hostname == NULL)
2739 ppc->asc (ppc->asc_cls, NULL);
2743 GNUNET_asprintf (&ret, "%s://%s:%d", PROTOCOL_PREFIX, hostname, ppc->port);
2745 ppc->asc (ppc->asc_cls, ret);
2752 * Convert the transports address to a nice, human-readable
2755 * @param cls closure
2756 * @param type name of the transport that generated the address
2757 * @param addr one of the addresses of the host, NULL for the last address
2758 * the specific address format depends on the transport
2759 * @param addrlen length of the address
2760 * @param numeric should (IP) addresses be displayed in numeric form?
2761 * @param timeout after how long should we give up?
2762 * @param asc function to call on each string
2763 * @param asc_cls closure for asc
2766 http_plugin_address_pretty_printer (void *cls,
2771 struct GNUNET_TIME_Relative timeout,
2772 GNUNET_TRANSPORT_AddressStringCallback
2775 struct PrettyPrinterContext *ppc;
2778 struct sockaddr_in a4;
2779 struct sockaddr_in6 a6;
2780 const struct IPv4HttpAddress *t4;
2781 const struct IPv6HttpAddress *t6;
2784 if (addrlen == sizeof (struct IPv6HttpAddress))
2787 memset (&a6, 0, sizeof (a6));
2788 a6.sin6_family = AF_INET6;
2789 a6.sin6_port = t6->port;
2790 memcpy (&a6.sin6_addr,
2792 sizeof (struct in6_addr));
2793 port = ntohs (t6->port);
2797 else if (addrlen == sizeof (struct IPv4HttpAddress))
2800 memset (&a4, 0, sizeof (a4));
2801 a4.sin_family = AF_INET;
2802 a4.sin_port = t4->port;
2803 a4.sin_addr.s_addr = t4->ipv4_addr;
2804 port = ntohs (t4->ipv4_addr);
2810 /* invalid address */
2811 GNUNET_break_op (0);
2812 asc (asc_cls, NULL);
2815 ppc = GNUNET_malloc (sizeof (struct PrettyPrinterContext));
2817 ppc->asc_cls = asc_cls;
2819 GNUNET_RESOLVER_hostname_get (sb,
2821 !numeric, timeout, &append_port, ppc);
2827 * Another peer has suggested an address for this
2828 * peer and transport plugin. Check that this could be a valid
2829 * address. If so, consider adding it to the list
2832 * @param cls closure
2833 * @param addr pointer to the address
2834 * @param addrlen length of addr
2835 * @return GNUNET_OK if this is a plausible address for this peer
2839 http_plugin_address_suggested (void *cls,
2840 const void *addr, size_t addrlen)
2842 struct Plugin *plugin = cls;
2843 struct IPv4HttpAddress *v4;
2844 struct IPv6HttpAddress *v6;
2845 struct IPv4HttpAddressWrapper *w_tv4 = plugin->ipv4_addr_head;
2846 struct IPv6HttpAddressWrapper *w_tv6 = plugin->ipv6_addr_head;
2848 GNUNET_assert(cls !=NULL);
2849 if ((addrlen != sizeof (struct IPv4HttpAddress)) &&
2850 (addrlen != sizeof (struct IPv6HttpAddress)))
2851 return GNUNET_SYSERR;
2852 if (addrlen == sizeof (struct IPv4HttpAddress))
2854 v4 = (struct IPv4HttpAddress *) addr;
2855 if (plugin->bind4_address!=NULL)
2857 if (0 == memcmp (&plugin->bind4_address->sin_addr, &v4->ipv4_addr, sizeof(uint32_t)))
2860 return GNUNET_SYSERR;
2864 if (0==memcmp (&w_tv4->addr->ipv4_addr, &v4->ipv4_addr, sizeof(uint32_t)))
2866 w_tv4 = w_tv4->next;
2871 return GNUNET_SYSERR;
2873 if (addrlen == sizeof (struct IPv6HttpAddress))
2875 v6 = (struct IPv6HttpAddress *) addr;
2876 if (plugin->bind6_address!=NULL)
2878 if (0 == memcmp (&plugin->bind6_address->sin6_addr, &v6->ipv6_addr, sizeof(struct in6_addr)))
2881 return GNUNET_SYSERR;
2885 if (0 == memcmp (&w_tv6->addr->ipv6_addr, &v6->ipv6_addr, sizeof(struct in6_addr)))
2887 w_tv6 = w_tv6->next;
2892 return GNUNET_SYSERR;
2894 return GNUNET_SYSERR;
2899 * Function called for a quick conversion of the binary address to
2900 * a numeric address. Note that the caller must not free the
2901 * address and that the next call to this function is allowed
2902 * to override the address again.
2904 * @param cls closure
2905 * @param addr binary address
2906 * @param addrlen length of the address
2907 * @return string representing the same address
2910 http_plugin_address_to_string (void *cls,
2914 const struct IPv4HttpAddress *t4;
2915 const struct IPv6HttpAddress *t6;
2916 struct sockaddr_in a4;
2917 struct sockaddr_in6 a6;
2919 static char rbuf[INET6_ADDRSTRLEN + 13];
2923 if (addrlen == sizeof (struct IPv6HttpAddress))
2925 address = GNUNET_malloc (INET6_ADDRSTRLEN);
2927 a6.sin6_addr = t6->ipv6_addr;
2928 inet_ntop(AF_INET6, &(a6.sin6_addr),address,INET6_ADDRSTRLEN);
2929 port = ntohs(t6->port);
2931 else if (addrlen == sizeof (struct IPv4HttpAddress))
2933 address = GNUNET_malloc (INET_ADDRSTRLEN);
2935 a4.sin_addr.s_addr = t4->ipv4_addr;
2936 inet_ntop(AF_INET, &(a4.sin_addr),address,INET_ADDRSTRLEN);
2937 port = ntohs(t4->port);
2941 /* invalid address */
2945 res = GNUNET_snprintf (rbuf,
2951 GNUNET_free (address);
2952 GNUNET_assert(res != 0);
2957 * Function called by the NAT subsystem suggesting another peer wants
2958 * to connect to us via connection reversal. Try to connect back to the
2961 * @param cls closure
2962 * @param addr address to try
2963 * @param addrlen number of bytes in addr
2966 try_connection_reversal (void *cls,
2967 const struct sockaddr *addr,
2974 tcp_nat_cb_add_addr (void *cls,
2976 const struct sockaddr *addr,
2979 struct Plugin *plugin = cls;
2980 struct IPv4HttpAddress * t4 = NULL;
2981 struct IPv4HttpAddressWrapper * w_t4 = NULL;
2982 struct IPv6HttpAddress * t6 = NULL;
2983 struct IPv6HttpAddressWrapper * w_t6 = NULL;
2986 af = addr->sa_family;
2990 w_t4 = plugin->ipv4_addr_head;
2991 while (w_t4 != NULL)
2993 int res = memcmp(&w_t4->addr->ipv4_addr,
2994 &((struct sockaddr_in *) addr)->sin_addr,
2995 sizeof (struct in_addr));
3002 w_t4 = GNUNET_malloc(sizeof(struct IPv4HttpAddressWrapper));
3003 t4 = GNUNET_malloc(sizeof(struct IPv4HttpAddress));
3004 memcpy (&t4->ipv4_addr,
3005 &((struct sockaddr_in *) addr)->sin_addr,
3006 sizeof (struct in_addr));
3007 t4->port = htons (plugin->port_inbound);
3011 GNUNET_CONTAINER_DLL_insert(plugin->ipv4_addr_head,
3012 plugin->ipv4_addr_tail,w_t4);
3014 plugin->env->notify_address(plugin->env->cls,
3016 w_t4->addr, sizeof (struct IPv4HttpAddress));
3020 w_t6 = plugin->ipv6_addr_head;
3023 int res = memcmp(&w_t6->addr->ipv6_addr,
3024 &((struct sockaddr_in6 *) addr)->sin6_addr,
3025 sizeof (struct in6_addr));
3032 w_t6 = GNUNET_malloc(sizeof(struct IPv6HttpAddressWrapper));
3033 t6 = GNUNET_malloc(sizeof(struct IPv6HttpAddress));
3035 memcpy (&t6->ipv6_addr,
3036 &((struct sockaddr_in6 *) addr)->sin6_addr,
3037 sizeof (struct in6_addr));
3038 t6->port = htons (plugin->port_inbound);
3042 GNUNET_CONTAINER_DLL_insert(plugin->ipv6_addr_head,
3043 plugin->ipv6_addr_tail,w_t6);
3045 plugin->env->notify_address(plugin->env->cls,
3047 w_t6->addr, sizeof (struct IPv6HttpAddress));
3056 tcp_nat_cb_remove_addr (void *cls,
3058 const struct sockaddr *addr,
3061 struct Plugin *plugin = cls;
3062 struct IPv4HttpAddressWrapper * w_t4 = NULL;
3063 struct IPv6HttpAddressWrapper * w_t6 = NULL;
3066 af = addr->sa_family;
3070 w_t4 = plugin->ipv4_addr_head;
3071 while (w_t4 != NULL)
3073 int res = memcmp(&w_t4->addr->ipv4_addr,
3074 &((struct sockaddr_in *) addr)->sin_addr,
3075 sizeof (struct in_addr));
3082 plugin->env->notify_address(plugin->env->cls,
3084 w_t4->addr, sizeof (struct IPv4HttpAddress));
3086 GNUNET_CONTAINER_DLL_remove(plugin->ipv4_addr_head,
3087 plugin->ipv4_addr_tail,w_t4);
3088 GNUNET_free (w_t4->addr);
3092 w_t6 = plugin->ipv6_addr_head;
3093 while (w_t6 != NULL)
3095 int res = memcmp(&w_t6->addr->ipv6_addr,
3096 &((struct sockaddr_in6 *) addr)->sin6_addr,
3097 sizeof (struct in6_addr));
3104 plugin->env->notify_address(plugin->env->cls,
3106 w_t6->addr, sizeof (struct IPv6HttpAddress));
3108 GNUNET_CONTAINER_DLL_remove(plugin->ipv6_addr_head,
3109 plugin->ipv6_addr_tail,w_t6);
3110 GNUNET_free (w_t6->addr);
3120 * Our external IP address/port mapping has changed.
3122 * @param cls closure, the 'struct LocalAddrList'
3123 * @param add_remove GNUNET_YES to mean the new public IP address, GNUNET_NO to mean
3124 * the previous (now invalid) one
3125 * @param addr either the previous or the new public IP address
3126 * @param addrlen actual lenght of the address
3129 tcp_nat_port_map_callback (void *cls,
3131 const struct sockaddr *addr,
3134 GNUNET_assert(cls !=NULL );
3136 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
3137 "NPMC called to %s address `%s'\n",
3138 (add_remove == GNUNET_YES) ? "remove" : "add",
3139 GNUNET_a2s (addr, addrlen));
3141 /* convert 'addr' to our internal format */
3145 tcp_nat_cb_add_addr (cls, add_remove, addr, addrlen);
3148 tcp_nat_cb_remove_addr (cls, add_remove, addr, addrlen);
3155 * Notify transport service about address
3157 * @param cls the plugin
3161 address_notification (void *cls,
3162 const struct GNUNET_SCHEDULER_TaskContext *tc)
3164 struct Plugin *plugin = cls;
3166 GNUNET_OS_network_interfaces_list (&process_interfaces, plugin);
3171 * Exit point from the plugin.
3174 LIBGNUNET_PLUGIN_TRANSPORT_DONE (void *cls)
3176 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
3177 struct Plugin *plugin = api->cls;
3179 struct IPv4HttpAddressWrapper * w_t4;
3180 struct IPv6HttpAddressWrapper * w_t6;
3181 GNUNET_assert(cls !=NULL);
3183 if (plugin->nat != NULL)
3184 GNUNET_NAT_unregister (plugin->nat);
3186 if (plugin->http_server_daemon_v4 != NULL)
3188 MHD_stop_daemon (plugin->http_server_daemon_v4);
3189 plugin->http_server_daemon_v4 = NULL;
3191 if (plugin->http_server_daemon_v6 != NULL)
3193 MHD_stop_daemon (plugin->http_server_daemon_v6);
3194 plugin->http_server_daemon_v6 = NULL;
3196 if ( plugin->http_server_task_v4 != GNUNET_SCHEDULER_NO_TASK)
3198 GNUNET_SCHEDULER_cancel(plugin->http_server_task_v4);
3199 plugin->http_server_task_v4 = GNUNET_SCHEDULER_NO_TASK;
3201 if ( plugin->http_server_task_v6 != GNUNET_SCHEDULER_NO_TASK)
3203 GNUNET_SCHEDULER_cancel(plugin->http_server_task_v6);
3204 plugin->http_server_task_v6 = GNUNET_SCHEDULER_NO_TASK;
3207 while (plugin->ipv4_addr_head!=NULL)
3209 w_t4 = plugin->ipv4_addr_head;
3210 GNUNET_CONTAINER_DLL_remove(plugin->ipv4_addr_head,plugin->ipv4_addr_tail,w_t4);
3211 GNUNET_free(w_t4->addr);
3215 while (plugin->ipv6_addr_head!=NULL)
3217 w_t6 = plugin->ipv6_addr_head;
3218 GNUNET_CONTAINER_DLL_remove(plugin->ipv6_addr_head,plugin->ipv6_addr_tail,w_t6);
3219 GNUNET_free(w_t6->addr);
3223 /* free all peer information */
3224 if (plugin->peers!=NULL)
3226 GNUNET_CONTAINER_multihashmap_iterate (plugin->peers,
3227 &remove_peer_context_Iterator,
3229 GNUNET_CONTAINER_multihashmap_destroy (plugin->peers);
3231 if (plugin->multi_handle!=NULL)
3233 mret = curl_multi_cleanup(plugin->multi_handle);
3234 if (CURLM_OK != mret)
3235 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3236 "curl multihandle clean up failed\n");
3237 plugin->multi_handle = NULL;
3239 curl_global_cleanup();
3241 if ( plugin->http_curl_task != GNUNET_SCHEDULER_NO_TASK)
3243 GNUNET_SCHEDULER_cancel(plugin->http_curl_task);
3244 plugin->http_curl_task = GNUNET_SCHEDULER_NO_TASK;
3248 GNUNET_free_non_null (plugin->bind4_address);
3249 GNUNET_free_non_null (plugin->bind6_address);
3250 GNUNET_free_non_null(plugin->bind_hostname);
3252 GNUNET_free_non_null (plugin->crypto_init);
3253 GNUNET_free_non_null (plugin->cert);
3254 GNUNET_free_non_null (plugin->key);
3256 GNUNET_free (plugin);
3259 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3260 "Unload %s plugin complete...\n",
3268 load_certificate( const char * file )
3270 struct GNUNET_DISK_FileHandle * gn_file;
3274 if (0!=STAT(file, &fstat))
3276 text = GNUNET_malloc (fstat.st_size+1);
3277 gn_file = GNUNET_DISK_file_open(file, GNUNET_DISK_OPEN_READ, GNUNET_DISK_PERM_USER_READ);
3283 if (GNUNET_SYSERR == GNUNET_DISK_file_read(gn_file, text, fstat.st_size))
3286 GNUNET_DISK_file_close(gn_file);
3289 text[fstat.st_size] = '\0';
3290 GNUNET_DISK_file_close(gn_file);
3297 * Entry point for the plugin.
3300 LIBGNUNET_PLUGIN_TRANSPORT_INIT (void *cls)
3302 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
3303 struct Plugin *plugin;
3304 struct GNUNET_TRANSPORT_PluginFunctions *api;
3305 struct GNUNET_TIME_Relative gn_timeout;
3306 long long unsigned int port;
3307 unsigned long long tneigh;
3308 struct sockaddr **addrs;
3309 socklen_t *addrlens;
3311 char * component_name;
3313 char * key_file = NULL;
3314 char * cert_file = NULL;
3317 GNUNET_assert(cls !=NULL);
3319 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3320 "Starting %s plugin...\n",
3323 GNUNET_asprintf(&component_name,
3327 plugin = GNUNET_malloc (sizeof (struct Plugin));
3328 plugin->stats = env->stats;
3330 plugin->peers = NULL;
3331 plugin->bind4_address = NULL;
3332 plugin->bind6_address = NULL;
3333 plugin->use_ipv6 = GNUNET_YES;
3334 plugin->use_ipv4 = GNUNET_YES;
3335 plugin->use_localaddresses = GNUNET_NO;
3337 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
3339 api->send = &http_plugin_send;
3340 api->disconnect = &http_plugin_disconnect;
3341 api->address_pretty_printer = &http_plugin_address_pretty_printer;
3342 api->check_address = &http_plugin_address_suggested;
3343 api->address_to_string = &http_plugin_address_to_string;
3345 /* Hashing our identity to use it in URLs */
3346 GNUNET_CRYPTO_hash_to_enc (&(plugin->env->my_identity->hashPubKey),
3347 &plugin->my_ascii_hash_ident);
3350 if (GNUNET_CONFIGURATION_have_value (env->cfg, "TRANSPORT", "NEIGHBOUR_LIMIT"))
3352 GNUNET_CONFIGURATION_get_value_number (env->cfg,
3361 plugin->max_connect_per_transport = tneigh;
3365 if (GNUNET_CONFIGURATION_have_value (env->cfg,
3366 component_name, "USE_IPv6"))
3368 plugin->use_ipv6 = GNUNET_CONFIGURATION_get_value_yesno (env->cfg,
3373 if (GNUNET_CONFIGURATION_have_value (env->cfg,
3374 component_name, "USE_IPv4"))
3376 plugin->use_ipv4 = GNUNET_CONFIGURATION_get_value_yesno (env->cfg,
3377 component_name,"USE_IPv4");
3379 /* use local addresses? */
3381 if (GNUNET_CONFIGURATION_have_value (env->cfg,
3382 component_name, "USE_LOCALADDR"))
3384 plugin->use_localaddresses = GNUNET_CONFIGURATION_get_value_yesno (env->cfg,
3388 /* Reading port number from config file */
3390 GNUNET_CONFIGURATION_get_value_number (env->cfg,
3396 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
3398 _("Require valid port number for transport plugin `%s' in configuration!\n"),
3400 GNUNET_free(component_name);
3401 LIBGNUNET_PLUGIN_TRANSPORT_DONE (api);
3405 /* Reading ipv4 addresse to bind to from config file */
3406 if ( (plugin->use_ipv4==GNUNET_YES) &&
3407 (GNUNET_CONFIGURATION_have_value (env->cfg,
3408 component_name, "BINDTO4")))
3410 GNUNET_break (GNUNET_OK ==
3411 GNUNET_CONFIGURATION_get_value_string (env->cfg,
3414 &plugin->bind_hostname));
3415 plugin->bind4_address = GNUNET_malloc(sizeof(struct sockaddr_in));
3416 plugin->bind4_address->sin_family = AF_INET;
3417 plugin->bind4_address->sin_port = htons (port);
3419 if (plugin->bind_hostname!=NULL)
3421 if (inet_pton(AF_INET,plugin->bind_hostname, &plugin->bind4_address->sin_addr)<=0)
3423 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
3425 _("Misconfigured address to bind to in configuration!\n"));
3426 GNUNET_free(plugin->bind4_address);
3427 GNUNET_free(plugin->bind_hostname);
3428 plugin->bind_hostname = NULL;
3429 plugin->bind4_address = NULL;
3434 /* Reading ipv4 addresse to bind to from config file */
3435 if ( (plugin->use_ipv6==GNUNET_YES) &&
3436 (GNUNET_CONFIGURATION_have_value (env->cfg,
3437 component_name, "BINDTO6")))
3439 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (env->cfg,
3442 &plugin->bind_hostname))
3444 plugin->bind6_address = GNUNET_malloc(sizeof(struct sockaddr_in6));
3445 plugin->bind6_address->sin6_family = AF_INET6;
3446 plugin->bind6_address->sin6_port = htons (port);
3447 if (plugin->bind_hostname!=NULL)
3449 if (inet_pton(AF_INET6,plugin->bind_hostname, &plugin->bind6_address->sin6_addr)<=0)
3451 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
3453 _("Misconfigured address to bind to in configuration!\n"));
3454 GNUNET_free(plugin->bind6_address);
3455 GNUNET_free(plugin->bind_hostname);
3456 plugin->bind_hostname = NULL;
3457 plugin->bind6_address = NULL;
3464 /* Reading HTTPS crypto related configuration */
3465 /* Get crypto init string from config */
3466 if (GNUNET_CONFIGURATION_have_value (env->cfg,
3467 "transport-https", "CRYPTO_INIT"))
3469 GNUNET_CONFIGURATION_get_value_string (env->cfg,
3472 &plugin->crypto_init);
3476 GNUNET_asprintf(&plugin->crypto_init,"NORMAL");
3479 /* Get private key file from config */
3480 if (GNUNET_CONFIGURATION_have_value (env->cfg,
3481 "transport-https", "KEY_FILE"))
3483 GNUNET_CONFIGURATION_get_value_filename (env->cfg,
3489 GNUNET_asprintf(&key_file,"https.key");
3491 /* Get private key file from config */
3492 if (GNUNET_CONFIGURATION_have_value (env->cfg,"transport-https", "CERT_FILE"))
3494 GNUNET_CONFIGURATION_get_value_filename (env->cfg,
3499 if (cert_file==NULL)
3500 GNUNET_asprintf(&cert_file,"https.cert");
3502 /* read key & certificates from file */
3503 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3504 "Loading TLS certificate `%s' `%s'\n",
3505 key_file, cert_file);
3507 plugin->key = load_certificate( key_file );
3508 plugin->cert = load_certificate( cert_file );
3510 if ((plugin->key==NULL) || (plugin->cert==NULL))
3514 GNUNET_asprintf(&cmd,
3515 "gnunet-transport-certificate-creation %s %s",
3516 key_file, cert_file);
3517 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3518 "No usable TLS certificate found, creating certificate \n");
3522 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
3524 _("Could not create a new TLS certificate, shell script `%s' failed!\n"),cmd,
3526 GNUNET_free (key_file);
3527 GNUNET_free (cert_file);
3528 GNUNET_free (component_name);
3529 LIBGNUNET_PLUGIN_TRANSPORT_DONE(api);
3534 plugin->key = load_certificate( key_file );
3535 plugin->cert = load_certificate( cert_file );
3536 if ((plugin->key==NULL) || (plugin->cert==NULL))
3538 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
3540 _("No usable TLS certificate found and creating one failed! \n"),
3542 GNUNET_free (key_file);
3543 GNUNET_free (cert_file);
3544 GNUNET_free (component_name);
3546 LIBGNUNET_PLUGIN_TRANSPORT_DONE(api);
3550 GNUNET_free (key_file);
3551 GNUNET_free (cert_file);
3553 GNUNET_assert((plugin->key!=NULL) && (plugin->cert!=NULL));
3554 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "TLS certificate loaded\n");
3557 GNUNET_assert ((port > 0) && (port <= 65535));
3558 plugin->port_inbound = port;
3559 gn_timeout = GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT;
3560 unsigned int timeout = (gn_timeout.rel_value) / 1000;
3561 if ( (plugin->http_server_daemon_v6 == NULL) &&
3562 (plugin->use_ipv6 == GNUNET_YES) &&
3565 struct sockaddr * tmp = (struct sockaddr *) plugin->bind6_address;
3566 plugin->http_server_daemon_v6 = MHD_start_daemon (
3575 &mhd_accept_cb, plugin,
3576 &mhd_access_cb, plugin,
3577 MHD_OPTION_SOCK_ADDR, tmp,
3578 MHD_OPTION_CONNECTION_LIMIT, (unsigned int) plugin->max_connect_per_transport,
3580 MHD_OPTION_HTTPS_PRIORITIES, plugin->crypto_init,
3581 MHD_OPTION_HTTPS_MEM_KEY, plugin->key,
3582 MHD_OPTION_HTTPS_MEM_CERT, plugin->cert,
3584 MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) timeout,
3585 MHD_OPTION_CONNECTION_MEMORY_LIMIT, (size_t) (2 * GNUNET_SERVER_MAX_MESSAGE_SIZE),
3586 MHD_OPTION_NOTIFY_COMPLETED, &mhd_termination_cb, plugin,
3587 MHD_OPTION_EXTERNAL_LOGGER, mhd_logger, plugin->mhd_log,
3590 if ( (plugin->http_server_daemon_v4 == NULL) &&
3591 (plugin->use_ipv4 == GNUNET_YES) &&
3594 plugin->http_server_daemon_v4 = MHD_start_daemon (
3603 &mhd_accept_cb, plugin ,
3604 &mhd_access_cb, plugin,
3605 MHD_OPTION_SOCK_ADDR, (struct sockaddr_in *) plugin->bind4_address,
3606 MHD_OPTION_CONNECTION_LIMIT, (unsigned int) plugin->max_connect_per_transport,
3608 MHD_OPTION_HTTPS_PRIORITIES, plugin->crypto_init,
3610 MHD_OPTION_HTTPS_MEM_KEY, plugin->key,
3611 MHD_OPTION_HTTPS_MEM_CERT, plugin->cert,
3613 MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) timeout,
3614 MHD_OPTION_CONNECTION_MEMORY_LIMIT, (size_t) (2 * GNUNET_SERVER_MAX_MESSAGE_SIZE),
3615 MHD_OPTION_NOTIFY_COMPLETED, &mhd_termination_cb, plugin,
3616 MHD_OPTION_EXTERNAL_LOGGER, mhd_logger, plugin->mhd_log,
3619 if (plugin->http_server_daemon_v4 != NULL)
3620 plugin->http_server_task_v4 = http_server_daemon_prepare (plugin, plugin->http_server_daemon_v4);
3621 if (plugin->http_server_daemon_v6 != NULL)
3622 plugin->http_server_task_v6 = http_server_daemon_prepare (plugin, plugin->http_server_daemon_v6);
3625 if (plugin->http_server_task_v4 != GNUNET_SCHEDULER_NO_TASK)
3628 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3629 "Starting MHD with IPv4 bound to %s with port %u\n",
3630 (plugin->bind_hostname!=NULL) ? plugin->bind_hostname : "every address",port);
3633 else if ( (plugin->http_server_task_v6 != GNUNET_SCHEDULER_NO_TASK) &&
3634 (plugin->http_server_task_v4 != GNUNET_SCHEDULER_NO_TASK) )
3637 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3638 "Starting MHD with IPv6 bound to %s with port %u\n",
3639 (plugin->bind_hostname!=NULL) ? plugin->bind_hostname : "every address", port);
3642 else if ( (plugin->http_server_task_v6 != GNUNET_SCHEDULER_NO_TASK) &&
3643 (plugin->http_server_task_v4 == GNUNET_SCHEDULER_NO_TASK) )
3646 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3647 "Starting MHD with IPv4 and IPv6 bound to %s with port %u\n",
3648 (plugin->bind_hostname!=NULL) ? plugin->bind_hostname : "every address",
3655 if ((plugin->use_ipv6 == GNUNET_YES) && (plugin->use_ipv4 == GNUNET_YES))
3656 GNUNET_asprintf(&tmp,"with IPv4 and IPv6 enabled");
3657 if ((plugin->use_ipv6 == GNUNET_NO) && (plugin->use_ipv4 == GNUNET_YES))
3658 GNUNET_asprintf(&tmp,"with IPv4 enabled");
3659 if ((plugin->use_ipv6 == GNUNET_YES) && (plugin->use_ipv4 == GNUNET_NO))
3660 GNUNET_asprintf(&tmp,"with IPv6 enabled");
3661 if ((plugin->use_ipv6 == GNUNET_NO) && (plugin->use_ipv4 == GNUNET_NO))
3662 GNUNET_asprintf(&tmp,"with NO IP PROTOCOL enabled");
3663 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3664 _("HTTP Server with %s could not be started on port %u! %s plugin failed!\n"),
3665 tmp, port, PROTOCOL_PREFIX);
3667 GNUNET_free (component_name);
3668 LIBGNUNET_PLUGIN_TRANSPORT_DONE (api);
3672 /* Initializing cURL */
3673 curl_global_init(CURL_GLOBAL_ALL);
3674 plugin->multi_handle = curl_multi_init();
3676 if ( NULL == plugin->multi_handle )
3678 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
3680 _("Could not initialize curl multi handle, failed to start %s plugin!\n"),
3682 GNUNET_free(component_name);
3683 LIBGNUNET_PLUGIN_TRANSPORT_DONE (api);
3687 ret = GNUNET_SERVICE_get_server_addresses (component_name,
3692 if (ret != GNUNET_SYSERR)
3694 plugin->nat = GNUNET_NAT_register (env->cfg,
3698 (const struct sockaddr **) addrs,
3700 &tcp_nat_port_map_callback,
3701 &try_connection_reversal,
3706 GNUNET_assert (addrs[ret] != NULL);
3707 GNUNET_free (addrs[ret]);
3709 GNUNET_free_non_null (addrs);
3710 GNUNET_free_non_null (addrlens);
3714 plugin->nat = GNUNET_NAT_register (env->cfg,
3719 &try_connection_reversal,
3723 plugin->peers = GNUNET_CONTAINER_multihashmap_create (10);
3725 GNUNET_free(component_name);
3726 //GNUNET_SCHEDULER_add_now(address_notification, plugin);
3730 /* end of plugin_transport_http.c */