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 *
358 * entity managing recieving data
359 * outbound session: CURL *
360 * inbound session: mhd_connection *
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;
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
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, const void *addr, size_t addrlen);
555 * Call MHD to process pending ipv4 requests and then go back
556 * and schedule the next run.
559 http_server_daemon_v4_run (void *cls,
560 const struct GNUNET_SCHEDULER_TaskContext *tc);
562 * Call MHD to process pending ipv6 requests and then go back
563 * and schedule the next run.
566 http_server_daemon_v6_run (void *cls,
567 const struct GNUNET_SCHEDULER_TaskContext *tc);
570 * Function setting up curl handle and selecting message to send
572 * @param plugin plugin
574 * @return GNUNET_SYSERR on failure, GNUNET_NO if connecting, GNUNET_YES if ok
577 send_check_connections (struct Plugin *plugin, struct Session *ps);
580 * Function setting up file descriptors and scheduling task to run
582 * @param plugin plugin as closure
583 * @return GNUNET_SYSERR for hard failure, GNUNET_OK for ok
586 curl_schedule (struct Plugin *plugin);
589 * Task scheduled to reset the inbound quota delay for a specific peer
590 * @param cls plugin as closure
591 * @param tc task context
594 reset_inbound_quota_delay (void *cls,
595 const struct GNUNET_SCHEDULER_TaskContext *tc)
597 struct HTTP_PeerContext *pc = cls;
599 GNUNET_assert (cls != NULL);
600 pc->reset_task = GNUNET_SCHEDULER_NO_TASK;
601 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
603 pc->delay = GNUNET_TIME_relative_get_zero ();
608 * Creates a valid url from passed address and id
609 * @param plugin plugin
610 * @param addr address to create url from
611 * @param addrlen address lenth
612 * @param id session id
613 * @return the created url
616 create_url (struct Plugin *plugin, const void *addr, size_t addrlen, size_t id)
619 char *addr_str = (char *) http_plugin_address_to_string (NULL, addr, addrlen);
621 GNUNET_assert ((addr != NULL) && (addrlen != 0));
622 GNUNET_asprintf (&url, "%s://%s/%s;%u", PROTOCOL_PREFIX, addr_str,
623 (char *) (&plugin->my_ascii_hash_ident), id);
629 * Removes a message from the linked list of messages
632 * @return GNUNET_SYSERR if msg not found, GNUNET_OK on success
635 remove_http_message (struct Session *ps, struct HTTP_Message *msg)
637 GNUNET_CONTAINER_DLL_remove (ps->pending_msgs_head, 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, const GNUNET_HashCode * key,
654 struct Plugin *plugin = cls;
655 struct HTTP_PeerContext *pc = value;
656 struct Session *ps = pc->head;
657 struct Session *tmp = NULL;
658 struct HTTP_Message *msg = NULL;
659 struct HTTP_Message *msg_tmp = NULL;
662 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Freeing context for peer `%s'\n",
663 GNUNET_i2s (&pc->identity));
665 GNUNET_assert (GNUNET_YES ==
666 GNUNET_CONTAINER_multihashmap_remove (plugin->peers,
667 &pc->identity.hashPubKey,
671 plugin->env->session_end (plugin, &pc->identity, ps);
674 GNUNET_free_non_null (ps->addr);
675 GNUNET_free (ps->url);
676 if (ps->msgtok != NULL)
677 GNUNET_SERVER_mst_destroy (ps->msgtok);
679 msg = ps->pending_msgs_head;
686 if (ps->direction == OUTBOUND)
688 if (ps->send_endpoint != NULL)
689 curl_easy_cleanup (ps->send_endpoint);
690 if (ps->recv_endpoint != NULL)
691 curl_easy_cleanup (ps->recv_endpoint);
697 GNUNET_STATISTICS_update (plugin->env->stats,
698 gettext_noop ("# HTTP peers active"), -1,
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, struct Session *ps,
714 int call_msg_cont, int call_msg_cont_result)
716 struct HTTP_Message *msg;
717 struct Plugin *plugin = ps->peercontext->plugin;
719 #if DEBUG_CONNECTIONS
720 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
721 "Connection %X: removing %s session %X with id %u\n", ps,
722 (ps->direction == INBOUND) ? "inbound" : "outbound", ps,
725 plugin->env->session_end (plugin, &pc->identity, ps);
726 GNUNET_free_non_null (ps->addr);
727 GNUNET_SERVER_mst_destroy (ps->msgtok);
728 GNUNET_free (ps->url);
729 if (ps->direction == INBOUND)
731 if (ps->recv_endpoint != NULL)
733 curl_easy_cleanup (ps->recv_endpoint);
734 ps->recv_endpoint = NULL;
736 if (ps->send_endpoint != NULL)
738 curl_easy_cleanup (ps->send_endpoint);
739 ps->send_endpoint = NULL;
743 msg = ps->pending_msgs_head;
746 if ((call_msg_cont == GNUNET_YES) && (msg->transmit_cont != NULL))
748 msg->transmit_cont (msg->transmit_cont_cls, &pc->identity,
749 call_msg_cont_result);
751 GNUNET_CONTAINER_DLL_remove (ps->pending_msgs_head, ps->pending_msgs_head,
754 msg = ps->pending_msgs_head;
757 GNUNET_CONTAINER_DLL_remove (pc->head, pc->tail, ps);
761 /* no sessions left remove peer */
762 if (pc->head == NULL)
765 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
766 "No sessions left for peer `%s', removing context\n",
767 GNUNET_i2s (&pc->identity));
769 remove_peer_context_Iterator (plugin, &pc->identity.hashPubKey, pc);
778 check_localaddress (const struct sockaddr *addr, socklen_t addrlen)
781 int local = GNUNET_NO;
782 int af = addr->sa_family;
788 uint32_t netmask = 0x7F000000;
789 uint32_t address = ntohl (((struct sockaddr_in *) addr)->sin_addr.s_addr);
791 res = (address >> 24) ^ (netmask >> 24);
797 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Checking IPv4 address `%s': %s\n",
798 GNUNET_a2s (addr, addrlen),
799 (local == GNUNET_YES) ? "local" : "global");
805 if (IN6_IS_ADDR_LOOPBACK (&((struct sockaddr_in6 *) addr)->sin6_addr) ||
806 IN6_IS_ADDR_LINKLOCAL (&((struct sockaddr_in6 *) addr)->sin6_addr))
811 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Checking IPv6 address `%s' : %s\n",
812 GNUNET_a2s (addr, addrlen),
813 (local == GNUNET_YES) ? "local" : "global");
823 * Add the IP of our network interface to the list of
824 * our external IP addresses.
826 * @param cls the 'struct Plugin*'
827 * @param name name of the interface
828 * @param isDefault do we think this may be our default interface
829 * @param addr address of the interface
830 * @param addrlen number of bytes in addr
831 * @return GNUNET_OK to continue iterating
834 process_interfaces (void *cls, const char *name, int isDefault,
835 const struct sockaddr *addr, socklen_t addrlen)
837 struct Plugin *plugin = cls;
838 struct IPv4HttpAddress *t4;
839 struct IPv6HttpAddress *t6;
842 if (plugin->use_localaddresses == GNUNET_NO)
844 if (GNUNET_YES == check_localaddress (addr, addrlen))
847 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, PROTOCOL_PREFIX,
848 "Not notifying transport of address `%s' (local address)\n",
849 GNUNET_a2s (addr, addrlen));
856 GNUNET_assert (cls != NULL);
857 af = addr->sa_family;
858 if ((af == AF_INET) && (plugin->use_ipv4 == GNUNET_YES) &&
859 (plugin->bind6_address == NULL))
862 struct in_addr bnd_cmp = ((struct sockaddr_in *) addr)->sin_addr;
864 t4 = GNUNET_malloc (sizeof (struct IPv4HttpAddress));
865 // Not skipping loopback addresses
868 t4->ipv4_addr = ((struct sockaddr_in *) addr)->sin_addr.s_addr;
869 t4->port = htons (plugin->port_inbound);
870 if (plugin->bind4_address != NULL)
873 memcmp (&plugin->bind4_address->sin_addr, &bnd_cmp,
874 sizeof (struct in_addr)))
876 GNUNET_CONTAINER_DLL_insert (plugin->ipv4_addr_head,
877 plugin->ipv4_addr_tail, t4);
878 plugin->env->notify_address (plugin->env->cls, GNUNET_YES, t4,
879 sizeof (struct IPv4HttpAddress));
887 GNUNET_CONTAINER_DLL_insert (plugin->ipv4_addr_head,
888 plugin->ipv4_addr_tail, t4);
889 plugin->env->notify_address (plugin->env->cls, GNUNET_YES, t4,
890 sizeof (struct IPv4HttpAddress));
894 if ((af == AF_INET6) && (plugin->use_ipv6 == GNUNET_YES) &&
895 (plugin->bind4_address == NULL))
898 struct in6_addr bnd_cmp6 = ((struct sockaddr_in6 *) addr)->sin6_addr;
900 t6 = GNUNET_malloc (sizeof (struct IPv6HttpAddress));
901 GNUNET_assert (t6 != NULL);
903 if (plugin->bind6_address != NULL)
906 memcmp (&plugin->bind6_address->sin6_addr, &bnd_cmp6,
907 sizeof (struct in6_addr)))
909 memcpy (&t6->ipv6_addr, &((struct sockaddr_in6 *) addr)->sin6_addr,
910 sizeof (struct in6_addr));
911 t6->port = htons (plugin->port_inbound);
912 plugin->env->notify_address (plugin->env->cls, GNUNET_YES, t6,
913 sizeof (struct IPv6HttpAddress));
914 GNUNET_CONTAINER_DLL_insert (plugin->ipv6_addr_head,
915 plugin->ipv6_addr_tail, t6);
921 memcpy (&t6->ipv6_addr, &((struct sockaddr_in6 *) addr)->sin6_addr,
922 sizeof (struct in6_addr));
923 t6->port = htons (plugin->port_inbound);
924 GNUNET_CONTAINER_DLL_insert (plugin->ipv6_addr_head, plugin->ipv6_addr_tail,
927 plugin->env->notify_address (plugin->env->cls, GNUNET_YES, t6,
928 sizeof (struct IPv6HttpAddress));
935 * External logging function for MHD
936 * @param arg arguments
937 * @param fmt format string
938 * @param ap list of arguments
941 mhd_logger (void *arg, const char *fmt, va_list ap)
945 vsnprintf (text, sizeof (text), fmt, ap);
947 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "MHD: %s\n", text);
952 mhd_termination_cb (void *cls, struct MHD_Connection *connection,
953 void **httpSessionCache)
955 struct Session *ps = *httpSessionCache;
959 struct HTTP_PeerContext *pc = ps->peercontext;
960 struct Plugin *plugin = cls;
962 GNUNET_assert (cls != NULL);
963 plugin->current_connections--;
965 if (connection == ps->recv_endpoint)
967 #if DEBUG_CONNECTIONS
968 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
969 "Connection %X: inbound connection from peer `%s' was terminated\n",
970 ps, GNUNET_i2s (&pc->identity));
972 ps->recv_active = GNUNET_NO;
973 ps->recv_connected = GNUNET_NO;
974 ps->recv_endpoint = NULL;
976 if (connection == ps->send_endpoint)
978 ps->send_active = GNUNET_NO;
979 ps->send_connected = GNUNET_NO;
980 ps->send_endpoint = NULL;
981 #if DEBUG_CONNECTIONS
982 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
983 "Connection %X: outbound connection from peer `%s' was terminated\n",
984 ps, GNUNET_i2s (&pc->identity));
988 /* if both connections disconnected, remove session */
989 if ((ps->send_connected == GNUNET_NO) && (ps->recv_connected == GNUNET_NO))
991 GNUNET_STATISTICS_update (pc->plugin->env->stats,
993 ("# HTTP inbound sessions for peers active"), -1,
995 remove_session (pc, ps, GNUNET_YES, GNUNET_SYSERR);
1001 * Callback called by MessageStreamTokenizer when a message has arrived
1002 * @param cls current session as closure
1003 * @param client clien
1004 * @param message the message to be forwarded to transport service
1007 mhd_write_mst_cb (void *cls, void *client,
1008 const struct GNUNET_MessageHeader *message)
1010 struct Session *ps = cls;
1011 struct HTTP_PeerContext *pc;
1012 struct GNUNET_TIME_Relative delay;
1014 GNUNET_assert (ps != NULL);
1015 pc = ps->peercontext;
1016 GNUNET_assert (pc != NULL);
1018 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1019 "Connection %X: Forwarding message to transport service, type %u and size %u from `%s' (`%s')\n",
1020 ps, ntohs (message->type), ntohs (message->size),
1021 GNUNET_i2s (&(ps->peercontext)->identity),
1022 http_plugin_address_to_string (NULL, ps->addr, ps->addrlen));
1024 struct GNUNET_TRANSPORT_ATS_Information distance[2];
1026 distance[0].type = htonl (GNUNET_TRANSPORT_ATS_QUALITY_NET_DISTANCE);
1027 distance[0].value = htonl (1);
1028 distance[1].type = htonl (GNUNET_TRANSPORT_ATS_ARRAY_TERMINATOR);
1029 distance[1].value = htonl (0);
1031 pc->plugin->env->receive (ps->peercontext->plugin->env->cls,
1032 &pc->identity, message,
1033 (const struct GNUNET_TRANSPORT_ATS_Information
1034 *) &distance, 2, ps, NULL, 0);
1036 if (pc->reset_task != GNUNET_SCHEDULER_NO_TASK)
1037 GNUNET_SCHEDULER_cancel (pc->reset_task);
1039 if (delay.rel_value > 0)
1042 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1043 "Connection %X: Inbound quota management: delay next read for %llu ms \n",
1044 ps, delay.rel_value);
1047 GNUNET_SCHEDULER_add_delayed (delay, &reset_inbound_quota_delay, pc);
1053 * Check if incoming connection is accepted.
1054 * NOTE: Here every connection is accepted
1055 * @param cls plugin as closure
1056 * @param addr address of incoming connection
1057 * @param addr_len address length of incoming connection
1058 * @return MHD_YES if connection is accepted, MHD_NO if connection is rejected
1062 mhd_accept_cb (void *cls, const struct sockaddr *addr, socklen_t addr_len)
1064 struct Plugin *plugin = cls;
1066 GNUNET_assert (cls != NULL);
1068 if (plugin->max_connect_per_transport > plugin->current_connections)
1070 plugin->current_connections++;
1079 * Callback called by MHD when it needs data to send
1080 * @param cls current session
1081 * @param pos position in buffer
1082 * @param buf the buffer to write data to
1083 * @param max max number of bytes available in buffer
1084 * @return bytes written to buffer
1087 mhd_send_callback (void *cls, uint64_t pos, char *buf, size_t max)
1089 struct Session *ps = cls;
1090 struct HTTP_PeerContext *pc;
1091 struct HTTP_Message *msg;
1094 GNUNET_assert (ps != NULL);
1096 pc = ps->peercontext;
1097 msg = ps->pending_msgs_tail;
1098 if (ps->send_force_disconnect == GNUNET_YES)
1100 #if DEBUG_CONNECTIONS
1101 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1102 "Connection %X: outbound forced to disconnect\n", ps);
1110 if ((msg->size - msg->pos) <= max)
1112 memcpy (buf, &msg->buf[msg->pos], (msg->size - msg->pos));
1113 bytes_read = msg->size - msg->pos;
1114 msg->pos += (msg->size - msg->pos);
1118 memcpy (buf, &msg->buf[msg->pos], max);
1123 /* removing message */
1124 if (msg->pos == msg->size)
1126 if (NULL != msg->transmit_cont)
1127 msg->transmit_cont (msg->transmit_cont_cls, &pc->identity, GNUNET_OK);
1128 ps->queue_length_cur -= msg->size;
1129 remove_http_message (ps, msg);
1132 #if DEBUG_CONNECTIONS
1133 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connection %X: MHD has sent %u bytes\n",
1141 * Process GET or PUT request received via MHD. For
1142 * GET, queue response that will send back our pending
1143 * messages. For PUT, process incoming data and send
1144 * to GNUnet core. In either case, check if a session
1145 * already exists and create a new one if not.
1148 mhd_access_cb (void *cls, struct MHD_Connection *mhd_connection,
1149 const char *url, const char *method, const char *version,
1150 const char *upload_data, size_t * upload_data_size,
1151 void **httpSessionCache)
1153 struct Plugin *plugin = cls;
1154 struct MHD_Response *response;
1155 const union MHD_ConnectionInfo *conn_info;
1156 const struct sockaddr *client_addr;
1157 const struct sockaddr_in *addrin;
1158 const struct sockaddr_in6 *addrin6;
1159 char address[INET6_ADDRSTRLEN + 14];
1160 struct GNUNET_PeerIdentity pi_in;
1162 struct IPv4HttpAddress ipv4addr;
1163 struct IPv6HttpAddress ipv6addr;
1164 struct HTTP_PeerContext *pc = NULL;
1165 struct Session *ps = NULL;
1166 struct Session *ps_tmp = NULL;
1167 int res = GNUNET_NO;
1169 size_t addr_len = 0;
1171 GNUNET_assert (cls != NULL);
1173 if (NULL == *httpSessionCache)
1175 /* check url for peer identity , if invalid send HTTP 404 */
1176 size_t len = strlen (&url[1]);
1177 char *peer = GNUNET_malloc (104 + 1);
1179 if ((len > 104) && (url[104] == ';'))
1181 char *id = GNUNET_malloc ((len - 104) + 1);
1183 strcpy (id, &url[105]);
1184 memcpy (peer, &url[1], 103);
1186 id_num = strtoul (id, NULL, 10);
1189 res = GNUNET_CRYPTO_hash_from_string (peer, &(pi_in.hashPubKey));
1191 if (GNUNET_SYSERR == res)
1194 MHD_create_response_from_data (strlen (HTTP_ERROR_RESPONSE),
1195 HTTP_ERROR_RESPONSE, MHD_NO, MHD_NO);
1196 res = MHD_queue_response (mhd_connection, MHD_HTTP_NOT_FOUND, response);
1197 MHD_destroy_response (response);
1198 #if DEBUG_CONNECTIONS
1200 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1201 "Peer has no valid ident, sent HTTP 1.1/404\n");
1203 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1204 "Peer has no valid ident, could not send error\n");
1211 ps = *httpSessionCache;
1212 pc = ps->peercontext;
1215 if (NULL == *httpSessionCache)
1217 /* get peer context */
1218 pc = GNUNET_CONTAINER_multihashmap_get (plugin->peers, &pi_in.hashPubKey);
1222 pc = GNUNET_malloc (sizeof (struct HTTP_PeerContext));
1223 pc->plugin = plugin;
1224 pc->session_id_counter = 1;
1225 pc->last_session = NULL;
1226 memcpy (&pc->identity, &pi_in, sizeof (struct GNUNET_PeerIdentity));
1227 GNUNET_CONTAINER_multihashmap_put (plugin->peers,
1228 &pc->identity.hashPubKey, pc,
1229 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
1230 GNUNET_STATISTICS_update (plugin->env->stats,
1231 gettext_noop ("# HTTP peers active"), 1,
1236 MHD_get_connection_info (mhd_connection,
1237 MHD_CONNECTION_INFO_CLIENT_ADDRESS);
1238 /* Incoming IPv4 connection */
1239 /* cast required for legacy MHD API < 0.9.6 */
1240 client_addr = (const struct sockaddr *) conn_info->client_addr;
1241 if (AF_INET == client_addr->sa_family)
1243 addrin = (const struct sockaddr_in *) client_addr;
1244 inet_ntop (addrin->sin_family, &(addrin->sin_addr), address,
1246 memcpy (&ipv4addr.ipv4_addr, &(addrin->sin_addr),
1247 sizeof (struct in_addr));
1248 ipv4addr.port = addrin->sin_port;
1250 addr_len = sizeof (struct IPv4HttpAddress);
1252 /* Incoming IPv6 connection */
1253 if (AF_INET6 == client_addr->sa_family)
1255 addrin6 = (const struct sockaddr_in6 *) client_addr;
1256 inet_ntop (addrin6->sin6_family, &(addrin6->sin6_addr), address,
1258 memcpy (&ipv6addr.ipv6_addr, &(addrin6->sin6_addr),
1259 sizeof (struct in6_addr));
1260 ipv6addr.port = addrin6->sin6_port;
1262 addr_len = sizeof (struct IPv6HttpAddress);
1265 GNUNET_assert (addr != NULL);
1266 GNUNET_assert (addr_len != 0);
1269 /* only inbound sessions here */
1272 while (ps_tmp != NULL)
1274 if ((ps_tmp->direction == INBOUND) && (ps_tmp->session_id == id_num) &&
1277 if ((ps_tmp->recv_force_disconnect != GNUNET_YES) &&
1278 (ps_tmp->send_force_disconnect != GNUNET_YES))
1282 ps_tmp = ps_tmp->next;
1287 ps = GNUNET_malloc (sizeof (struct Session));
1288 ps->addr = GNUNET_malloc (addr_len);
1289 memcpy (ps->addr, addr, addr_len);
1290 ps->addrlen = addr_len;
1291 ps->direction = INBOUND;
1292 ps->pending_msgs_head = NULL;
1293 ps->pending_msgs_tail = NULL;
1294 ps->send_connected = GNUNET_NO;
1295 ps->send_active = GNUNET_NO;
1296 ps->recv_connected = GNUNET_NO;
1297 ps->recv_active = GNUNET_NO;
1298 ps->peercontext = pc;
1299 ps->session_id = id_num;
1300 ps->queue_length_cur = 0;
1301 ps->queue_length_max = GNUNET_SERVER_MAX_MESSAGE_SIZE;
1302 ps->url = create_url (plugin, ps->addr, ps->addrlen, ps->session_id);
1303 GNUNET_CONTAINER_DLL_insert (pc->head, pc->tail, ps);
1304 GNUNET_STATISTICS_update (plugin->env->stats,
1306 ("# HTTP inbound sessions for peers active"), 1,
1310 *httpSessionCache = ps;
1311 if (ps->msgtok == NULL)
1312 ps->msgtok = GNUNET_SERVER_mst_create (&mhd_write_mst_cb, ps);
1314 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1315 "Connection %X: HTTP Daemon has new an incoming `%s' request from peer `%s' (`%s')\n",
1316 ps, method, GNUNET_i2s (&pc->identity),
1317 http_plugin_address_to_string (NULL, ps->addr, ps->addrlen));
1321 /* Is it a PUT or a GET request */
1322 if (0 == strcmp (MHD_HTTP_METHOD_PUT, method))
1324 if (ps->recv_force_disconnect == GNUNET_YES)
1326 #if DEBUG_CONNECTIONS
1327 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1328 "Connection %X: inbound connection was forced to disconnect\n",
1331 ps->recv_active = GNUNET_NO;
1334 if ((*upload_data_size == 0) && (ps->recv_active == GNUNET_NO))
1336 ps->recv_endpoint = mhd_connection;
1337 ps->recv_connected = GNUNET_YES;
1338 ps->recv_active = GNUNET_YES;
1339 ps->recv_force_disconnect = GNUNET_NO;
1340 #if DEBUG_CONNECTIONS
1341 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1342 "Connection %X: inbound PUT connection connected\n", ps);
1347 /* Transmission of all data complete */
1348 if ((*upload_data_size == 0) && (ps->recv_active == GNUNET_YES))
1351 MHD_create_response_from_data (strlen (HTTP_PUT_RESPONSE),
1352 HTTP_PUT_RESPONSE, MHD_NO, MHD_NO);
1353 res = MHD_queue_response (mhd_connection, MHD_HTTP_OK, response);
1354 #if DEBUG_CONNECTIONS
1355 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1356 "Connection %X: Sent HTTP/1.1: 200 OK as PUT Response\n", ps);
1358 MHD_destroy_response (response);
1359 ps->recv_active = GNUNET_NO;
1363 /* Recieving data */
1364 if ((*upload_data_size > 0) && (ps->recv_active == GNUNET_YES))
1366 if (pc->delay.rel_value == 0)
1369 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1370 "Connection %X: PUT with %u bytes forwarded to MST\n", ps,
1374 GNUNET_SERVER_mst_receive (ps->msgtok, ps, upload_data,
1375 *upload_data_size, GNUNET_NO, GNUNET_NO);
1376 (*upload_data_size) = 0;
1381 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1382 "Connection %X: no inbound bandwidth available! Next read was delayed for %llu ms\n",
1383 ps, ps->peercontext->delay.rel_value);
1391 if (0 == strcmp (MHD_HTTP_METHOD_GET, method))
1393 if (ps->send_force_disconnect == GNUNET_YES)
1395 #if DEBUG_CONNECTIONS
1396 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1397 "Connection %X: outbound connection was forced to disconnect\n",
1400 ps->send_active = GNUNET_NO;
1403 ps->send_connected = GNUNET_YES;
1404 ps->send_active = GNUNET_YES;
1405 ps->send_endpoint = mhd_connection;
1406 ps->send_force_disconnect = GNUNET_NO;
1407 #if DEBUG_CONNECTIONS
1408 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1409 "Connection %X: inbound GET connection connected\n", ps);
1412 MHD_create_response_from_callback (-1, 32 * 1024, &mhd_send_callback,
1414 res = MHD_queue_response (mhd_connection, MHD_HTTP_OK, response);
1415 MHD_destroy_response (response);
1423 * Function that queries MHD's select sets and
1424 * starts the task waiting for them.
1425 * @param plugin plugin
1426 * @param daemon_handle the MHD daemon handle
1427 * @return gnunet task identifier
1429 static GNUNET_SCHEDULER_TaskIdentifier
1430 http_server_daemon_prepare (struct Plugin *plugin,
1431 struct MHD_Daemon *daemon_handle)
1433 GNUNET_SCHEDULER_TaskIdentifier ret;
1437 struct GNUNET_NETWORK_FDSet *wrs;
1438 struct GNUNET_NETWORK_FDSet *wws;
1439 struct GNUNET_NETWORK_FDSet *wes;
1441 unsigned long long timeout;
1443 struct GNUNET_TIME_Relative tv;
1445 ret = GNUNET_SCHEDULER_NO_TASK;
1449 wrs = GNUNET_NETWORK_fdset_create ();
1450 wes = GNUNET_NETWORK_fdset_create ();
1451 wws = GNUNET_NETWORK_fdset_create ();
1453 GNUNET_assert (MHD_YES == MHD_get_fdset (daemon_handle, &rs, &ws, &es, &max));
1454 haveto = MHD_get_timeout (daemon_handle, &timeout);
1455 if (haveto == MHD_YES)
1456 tv.rel_value = (uint64_t) timeout;
1458 tv = GNUNET_TIME_UNIT_SECONDS;
1459 GNUNET_NETWORK_fdset_copy_native (wrs, &rs, max + 1);
1460 GNUNET_NETWORK_fdset_copy_native (wws, &ws, max + 1);
1461 GNUNET_NETWORK_fdset_copy_native (wes, &es, max + 1);
1462 if (daemon_handle == plugin->http_server_daemon_v4)
1464 if (plugin->http_server_task_v4 != GNUNET_SCHEDULER_NO_TASK)
1466 GNUNET_SCHEDULER_cancel (plugin->http_server_task_v4);
1467 plugin->http_server_daemon_v4 = GNUNET_SCHEDULER_NO_TASK;
1471 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1472 GNUNET_SCHEDULER_NO_TASK, tv, wrs, wws,
1473 &http_server_daemon_v4_run, plugin);
1475 if (daemon_handle == plugin->http_server_daemon_v6)
1477 if (plugin->http_server_task_v6 != GNUNET_SCHEDULER_NO_TASK)
1479 GNUNET_SCHEDULER_cancel (plugin->http_server_task_v6);
1480 plugin->http_server_task_v6 = GNUNET_SCHEDULER_NO_TASK;
1484 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1485 GNUNET_SCHEDULER_NO_TASK, tv, wrs, wws,
1486 &http_server_daemon_v6_run, plugin);
1488 GNUNET_NETWORK_fdset_destroy (wrs);
1489 GNUNET_NETWORK_fdset_destroy (wws);
1490 GNUNET_NETWORK_fdset_destroy (wes);
1496 * Call MHD IPv4 to process pending requests and then go back
1497 * and schedule the next run.
1498 * @param cls plugin as closure
1499 * @param tc task context
1502 http_server_daemon_v4_run (void *cls,
1503 const struct GNUNET_SCHEDULER_TaskContext *tc)
1505 struct Plugin *plugin = cls;
1507 #if DEBUG_SCHEDULING
1508 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_READ_READY))
1509 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1510 "http_server_daemon_v4_run: GNUNET_SCHEDULER_REASON_READ_READY\n");
1511 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_WRITE_READY))
1512 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1513 "http_server_daemon_v4_run: GNUNET_SCHEDULER_REASON_WRITE_READY\n");
1514 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_TIMEOUT))
1515 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1516 "http_server_daemon_v4_run: GNUNET_SCHEDULER_REASON_TIMEOUT\n");
1517 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_STARTUP))
1518 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1519 "http_server_daemon_v4_run: GGNUNET_SCHEDULER_REASON_STARTUP\n");
1520 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1521 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1522 "http_server_daemon_v4_run: GGNUNET_SCHEDULER_REASON_SHUTDOWN\n");
1525 GNUNET_assert (cls != NULL);
1526 plugin->http_server_task_v4 = GNUNET_SCHEDULER_NO_TASK;
1528 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1531 GNUNET_assert (MHD_YES == MHD_run (plugin->http_server_daemon_v4));
1532 plugin->http_server_task_v4 =
1533 http_server_daemon_prepare (plugin, plugin->http_server_daemon_v4);
1538 * Call MHD IPv6 to process pending requests and then go back
1539 * and schedule the next run.
1540 * @param cls plugin as closure
1541 * @param tc task context
1544 http_server_daemon_v6_run (void *cls,
1545 const struct GNUNET_SCHEDULER_TaskContext *tc)
1547 struct Plugin *plugin = cls;
1549 #if DEBUG_SCHEDULING
1550 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_READ_READY))
1551 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1552 "http_server_daemon_v6_run: GNUNET_SCHEDULER_REASON_READ_READY\n");
1553 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_WRITE_READY))
1554 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1555 "http_server_daemon_v6_run: GNUNET_SCHEDULER_REASON_WRITE_READY\n");
1556 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_TIMEOUT))
1557 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1558 "http_server_daemon_v6_run: GNUNET_SCHEDULER_REASON_TIMEOUT\n");
1559 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_STARTUP))
1560 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1561 "http_server_daemon_v6_run: GGNUNET_SCHEDULER_REASON_STARTUP\n");
1562 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1563 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1564 "http_server_daemon_v6_run: GGNUNET_SCHEDULER_REASON_SHUTDOWN\n");
1567 GNUNET_assert (cls != NULL);
1568 plugin->http_server_task_v6 = GNUNET_SCHEDULER_NO_TASK;
1570 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1573 GNUNET_assert (MHD_YES == MHD_run (plugin->http_server_daemon_v6));
1574 plugin->http_server_task_v6 =
1575 http_server_daemon_prepare (plugin, plugin->http_server_daemon_v6);
1580 curl_get_header_cb (void *ptr, size_t size, size_t nmemb, void *stream)
1582 struct Session *ps = stream;
1584 long http_result = 0;
1587 /* Getting last http result code */
1588 GNUNET_assert (NULL != ps);
1589 if (ps->recv_connected == GNUNET_NO)
1592 curl_easy_getinfo (ps->recv_endpoint, CURLINFO_RESPONSE_CODE,
1594 if (CURLE_OK == res)
1596 if (http_result == 200)
1598 ps->recv_connected = GNUNET_YES;
1599 ps->recv_active = GNUNET_YES;
1600 #if DEBUG_CONNECTIONS
1601 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1602 "Connection %X: connected to recieve data\n", ps);
1604 // Calling send_check_connections again since receive is established
1605 send_check_connections (ps->peercontext->plugin, ps);
1612 size_t len = size * nmemb;
1615 if ((size * nmemb) < SIZE_MAX)
1616 tmp = GNUNET_malloc (len + 1);
1618 if ((tmp != NULL) && (len > 0))
1620 memcpy (tmp, ptr, len);
1623 if (tmp[len - 2] == 13)
1624 tmp[len - 2] = '\0';
1626 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connection %X: Header: %s\n", ps,
1629 GNUNET_free_non_null (tmp);
1632 return size * nmemb;
1637 * Callback called by libcurl when new headers arrive
1638 * Used to get HTTP result for curl operations
1639 * @param ptr stream to read from
1640 * @param size size of one char element
1641 * @param nmemb number of char elements
1642 * @param stream closure set by user
1643 * @return bytes read by function
1646 curl_put_header_cb (void *ptr, size_t size, size_t nmemb, void *stream)
1648 struct Session *ps = stream;
1651 size_t len = size * nmemb;
1652 long http_result = 0;
1655 /* Getting last http result code */
1656 GNUNET_assert (NULL != ps);
1658 curl_easy_getinfo (ps->send_endpoint, CURLINFO_RESPONSE_CODE,
1660 if (CURLE_OK == res)
1662 if ((http_result == 100) && (ps->send_connected == GNUNET_NO))
1664 ps->send_connected = GNUNET_YES;
1665 ps->send_active = GNUNET_YES;
1666 #if DEBUG_CONNECTIONS
1667 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1668 "Connection %X: connected to send data\n", ps);
1671 if ((http_result == 200) && (ps->send_connected == GNUNET_YES))
1673 ps->send_connected = GNUNET_NO;
1674 ps->send_active = GNUNET_NO;
1675 #if DEBUG_CONNECTIONS
1676 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1677 "Connection %X: sending disconnected\n", ps);
1683 if ((size * nmemb) < SIZE_MAX)
1684 tmp = GNUNET_malloc (len + 1);
1686 if ((tmp != NULL) && (len > 0))
1688 memcpy (tmp, ptr, len);
1691 if (tmp[len - 2] == 13)
1692 tmp[len - 2] = '\0';
1695 GNUNET_free_non_null (tmp);
1696 return size * nmemb;
1700 * Callback method used with libcurl
1701 * Method is called when libcurl needs to read data during sending
1702 * @param stream pointer where to write data
1703 * @param size size of an individual element
1704 * @param nmemb count of elements that can be written to the buffer
1705 * @param ptr source pointer, passed to the libcurl handle
1706 * @return bytes written to stream
1709 curl_send_cb (void *stream, size_t size, size_t nmemb, void *ptr)
1711 struct Session *ps = ptr;
1712 struct HTTP_Message *msg = ps->pending_msgs_tail;
1716 if (ps->send_active == GNUNET_NO)
1717 return CURL_READFUNC_PAUSE;
1718 if ((ps->pending_msgs_tail == NULL) && (ps->send_active == GNUNET_YES))
1720 #if DEBUG_CONNECTIONS
1721 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1722 "Connection %X: No Message to send, pausing connection\n", ps);
1724 ps->send_active = GNUNET_NO;
1725 return CURL_READFUNC_PAUSE;
1728 GNUNET_assert (msg != NULL);
1731 if (msg->pos < msg->size)
1733 /* data fit in buffer */
1734 if ((msg->size - msg->pos) <= (size * nmemb))
1736 len = (msg->size - msg->pos);
1737 memcpy (stream, &msg->buf[msg->pos], len);
1744 memcpy (stream, &msg->buf[msg->pos], len);
1749 /* no data to send */
1755 if (msg->pos == msg->size)
1757 #if DEBUG_CONNECTIONS
1758 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1759 "Connection %X: Message with %u bytes sent, removing message from queue\n",
1762 /* Calling transmit continuation */
1763 if (NULL != ps->pending_msgs_tail->transmit_cont)
1764 msg->transmit_cont (ps->pending_msgs_tail->transmit_cont_cls,
1765 &(ps->peercontext)->identity, GNUNET_OK);
1766 ps->queue_length_cur -= msg->size;
1767 remove_http_message (ps, msg);
1774 curl_receive_mst_cb (void *cls, void *client,
1775 const struct GNUNET_MessageHeader *message)
1777 struct Session *ps = cls;
1778 struct GNUNET_TIME_Relative delay;
1780 GNUNET_assert (ps != NULL);
1782 struct HTTP_PeerContext *pc = ps->peercontext;
1784 GNUNET_assert (pc != NULL);
1786 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1787 "Connection %X: Forwarding message to transport service, type %u and size %u from `%s' (`%s')\n",
1788 ps, ntohs (message->type), ntohs (message->size),
1789 GNUNET_i2s (&(pc->identity)), http_plugin_address_to_string (NULL,
1793 struct GNUNET_TRANSPORT_ATS_Information distance[2];
1795 distance[0].type = htonl (GNUNET_TRANSPORT_ATS_QUALITY_NET_DISTANCE);
1796 distance[0].value = htonl (1);
1797 distance[1].type = htonl (GNUNET_TRANSPORT_ATS_ARRAY_TERMINATOR);
1798 distance[1].value = htonl (0);
1801 pc->plugin->env->receive (pc->plugin->env->cls, &pc->identity, message,
1802 (const struct GNUNET_TRANSPORT_ATS_Information
1803 *) &distance, 2, ps, ps->addr, ps->addrlen);
1805 if (pc->reset_task != GNUNET_SCHEDULER_NO_TASK)
1806 GNUNET_SCHEDULER_cancel (pc->reset_task);
1808 if (delay.rel_value > 0)
1811 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1812 "Connection %X: Inbound quota management: delay next read for %llu ms\n",
1813 ps, delay.rel_value);
1816 GNUNET_SCHEDULER_add_delayed (delay, &reset_inbound_quota_delay, pc);
1822 * Callback method used with libcurl
1823 * Method is called when libcurl needs to write data during sending
1824 * @param stream pointer where to write data
1825 * @param size size of an individual element
1826 * @param nmemb count of elements that can be written to the buffer
1827 * @param ptr destination pointer, passed to the libcurl handle
1828 * @return bytes read from stream
1831 curl_receive_cb (void *stream, size_t size, size_t nmemb, void *ptr)
1833 struct Session *ps = ptr;
1835 if (ps->peercontext->delay.rel_value > 0)
1838 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1839 "Connection %X: no inbound bandwidth available! Next read was delayed for %llu ms\n",
1840 ps, ps->peercontext->delay.rel_value);
1844 #if DEBUG_CONNECTIONS
1845 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connection %X: %u bytes received\n", ps,
1848 GNUNET_SERVER_mst_receive (ps->msgtok, ps, stream, size * nmemb, GNUNET_NO,
1850 return (size * nmemb);
1855 curl_handle_finished (struct Plugin *plugin)
1857 struct Session *ps = NULL;
1858 struct HTTP_PeerContext *pc = NULL;
1859 struct CURLMsg *msg;
1860 struct HTTP_Message *cur_msg = NULL;
1867 msg = curl_multi_info_read (plugin->multi_handle, &msgs_in_queue);
1868 if ((msgs_in_queue == 0) || (msg == NULL))
1870 /* get session for affected curl handle */
1871 GNUNET_assert (msg->easy_handle != NULL);
1872 curl_easy_getinfo (msg->easy_handle, CURLINFO_PRIVATE, &tmp);
1873 ps = (struct Session *) tmp;
1874 GNUNET_assert (ps != NULL);
1875 pc = ps->peercontext;
1876 GNUNET_assert (pc != NULL);
1880 if ((msg->data.result != CURLE_OK) &&
1881 (msg->data.result != CURLE_GOT_NOTHING))
1883 /* sending msg failed */
1884 if (msg->easy_handle == ps->send_endpoint)
1886 #if DEBUG_CONNECTIONS
1887 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1889 ("Connection %X: HTTP PUT to peer `%s' (`%s') failed: `%s' `%s'\n"),
1890 ps, GNUNET_i2s (&pc->identity),
1891 http_plugin_address_to_string (NULL, ps->addr,
1893 "curl_multi_perform",
1894 curl_easy_strerror (msg->data.result));
1896 ps->send_connected = GNUNET_NO;
1897 ps->send_active = GNUNET_NO;
1898 curl_multi_remove_handle (plugin->multi_handle, ps->send_endpoint);
1899 while (ps->pending_msgs_tail != NULL)
1901 cur_msg = ps->pending_msgs_tail;
1902 if (NULL != cur_msg->transmit_cont)
1903 cur_msg->transmit_cont (cur_msg->transmit_cont_cls, &pc->identity,
1905 ps->queue_length_cur -= cur_msg->size;
1906 remove_http_message (ps, cur_msg);
1909 /* GET connection failed */
1910 if (msg->easy_handle == ps->recv_endpoint)
1912 #if DEBUG_CONNECTIONS
1913 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1915 ("Connection %X: HTTP GET to peer `%s' (`%s') failed: `%s' `%s'\n"),
1916 ps, GNUNET_i2s (&pc->identity),
1917 http_plugin_address_to_string (NULL, ps->addr,
1919 "curl_multi_perform",
1920 curl_easy_strerror (msg->data.result));
1922 ps->recv_connected = GNUNET_NO;
1923 ps->recv_active = GNUNET_NO;
1924 curl_multi_remove_handle (plugin->multi_handle, ps->recv_endpoint);
1929 if (msg->easy_handle == ps->send_endpoint)
1931 GNUNET_assert (CURLE_OK ==
1932 curl_easy_getinfo (msg->easy_handle,
1933 CURLINFO_RESPONSE_CODE,
1935 #if DEBUG_CONNECTIONS
1936 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1937 "Connection %X: HTTP PUT connection to peer `%s' (`%s') was closed with HTTP code %u\n",
1938 ps, GNUNET_i2s (&pc->identity),
1939 http_plugin_address_to_string (NULL, ps->addr,
1940 ps->addrlen), http_result);
1942 /* Calling transmit continuation */
1943 while (ps->pending_msgs_tail != NULL)
1945 cur_msg = ps->pending_msgs_tail;
1946 if (NULL != cur_msg->transmit_cont)
1948 /* HTTP 1xx : Last message before here was informational */
1949 if ((http_result >= 100) && (http_result < 200))
1950 cur_msg->transmit_cont (cur_msg->transmit_cont_cls,
1951 &pc->identity, GNUNET_OK);
1952 /* HTTP 2xx: successful operations */
1953 if ((http_result >= 200) && (http_result < 300))
1954 cur_msg->transmit_cont (cur_msg->transmit_cont_cls,
1955 &pc->identity, GNUNET_OK);
1956 /* HTTP 3xx..5xx: error */
1957 if ((http_result >= 300) && (http_result < 600))
1958 cur_msg->transmit_cont (cur_msg->transmit_cont_cls,
1959 &pc->identity, GNUNET_SYSERR);
1961 ps->queue_length_cur -= cur_msg->size;
1962 remove_http_message (ps, cur_msg);
1965 ps->send_connected = GNUNET_NO;
1966 ps->send_active = GNUNET_NO;
1967 curl_multi_remove_handle (plugin->multi_handle, ps->send_endpoint);
1969 if (msg->easy_handle == ps->recv_endpoint)
1971 #if DEBUG_CONNECTIONS
1972 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1973 "Connection %X: HTTP GET connection to peer `%s' (`%s') was closed with HTTP code %u\n",
1974 ps, GNUNET_i2s (&pc->identity),
1975 http_plugin_address_to_string (NULL, ps->addr,
1976 ps->addrlen), http_result);
1978 ps->recv_connected = GNUNET_NO;
1979 ps->recv_active = GNUNET_NO;
1980 curl_multi_remove_handle (plugin->multi_handle, ps->recv_endpoint);
1982 plugin->current_connections--;
1984 if ((ps->recv_connected == GNUNET_NO) &&
1985 (ps->send_connected == GNUNET_NO))
1986 remove_session (pc, ps, GNUNET_YES, GNUNET_SYSERR);
1992 while ((msgs_in_queue > 0));
1997 * Task performing curl operations
1998 * @param cls plugin as closure
1999 * @param tc gnunet scheduler task context
2002 curl_perform (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2004 struct Plugin *plugin = cls;
2005 static unsigned int handles_last_run;
2009 GNUNET_assert (cls != NULL);
2011 plugin->http_curl_task = GNUNET_SCHEDULER_NO_TASK;
2012 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
2017 mret = curl_multi_perform (plugin->multi_handle, &running);
2018 if ((running < handles_last_run) && (running > 0))
2019 curl_handle_finished (plugin);
2020 handles_last_run = running;
2022 while (mret == CURLM_CALL_MULTI_PERFORM);
2023 curl_schedule (plugin);
2028 * Function setting up file descriptors and scheduling task to run
2030 * @param plugin plugin as closure
2031 * @return GNUNET_SYSERR for hard failure, GNUNET_OK for ok
2034 curl_schedule (struct Plugin *plugin)
2040 struct GNUNET_NETWORK_FDSet *grs;
2041 struct GNUNET_NETWORK_FDSet *gws;
2045 /* Cancel previous scheduled task */
2046 if (plugin->http_curl_task != GNUNET_SCHEDULER_NO_TASK)
2048 GNUNET_SCHEDULER_cancel (plugin->http_curl_task);
2049 plugin->http_curl_task = GNUNET_SCHEDULER_NO_TASK;
2056 mret = curl_multi_fdset (plugin->multi_handle, &rs, &ws, &es, &max);
2057 if (mret != CURLM_OK)
2059 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("%s failed at %s:%d: `%s'\n"),
2060 "curl_multi_fdset", __FILE__, __LINE__,
2061 curl_multi_strerror (mret));
2062 return GNUNET_SYSERR;
2064 mret = curl_multi_timeout (plugin->multi_handle, &to);
2065 if (mret != CURLM_OK)
2067 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("%s failed at %s:%d: `%s'\n"),
2068 "curl_multi_timeout", __FILE__, __LINE__,
2069 curl_multi_strerror (mret));
2070 return GNUNET_SYSERR;
2073 grs = GNUNET_NETWORK_fdset_create ();
2074 gws = GNUNET_NETWORK_fdset_create ();
2075 GNUNET_NETWORK_fdset_copy_native (grs, &rs, max + 1);
2076 GNUNET_NETWORK_fdset_copy_native (gws, &ws, max + 1);
2077 plugin->http_curl_task =
2078 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
2079 GNUNET_SCHEDULER_NO_TASK,
2082 GNUNET_TIME_relative_multiply
2083 (GNUNET_TIME_UNIT_SECONDS,
2085 GNUNET_TIME_relative_multiply
2086 (GNUNET_TIME_UNIT_MILLISECONDS, to), grs,
2087 gws, &curl_perform, plugin);
2088 GNUNET_NETWORK_fdset_destroy (gws);
2089 GNUNET_NETWORK_fdset_destroy (grs);
2096 * Function to log curl debug messages with GNUNET_log
2097 * @param curl handle
2098 * @param type curl_infotype
2101 * @param cls closure
2105 curl_logger (CURL * curl, curl_infotype type, char *data, size_t size,
2108 if (type == CURLINFO_TEXT)
2110 char text[size + 2];
2112 memcpy (text, data, size);
2113 if (text[size - 1] == '\n')
2118 text[size + 1] = '\0';
2120 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CURL: Connection %X - %s", cls, text);
2128 * Function setting up curl handle and selecting message to send
2130 * @param plugin plugin
2132 * @return GNUNET_SYSERR on failure, GNUNET_NO if connecting, GNUNET_YES if ok
2135 send_check_connections (struct Plugin *plugin, struct Session *ps)
2138 struct GNUNET_TIME_Relative timeout =
2139 GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT;
2141 if ((ps->direction == OUTBOUND) &&
2142 (plugin->current_connections < plugin->max_connect_per_transport))
2144 /* RECV DIRECTION */
2145 /* Check if session is connected to receive data, otherwise connect to peer */
2147 if (ps->recv_connected == GNUNET_NO)
2149 int fresh = GNUNET_NO;
2151 if (ps->recv_endpoint == NULL)
2154 ps->recv_endpoint = curl_easy_init ();
2157 curl_easy_setopt (ps->recv_endpoint, CURLOPT_VERBOSE, 1L);
2158 curl_easy_setopt (ps->recv_endpoint, CURLOPT_DEBUGFUNCTION, &curl_logger);
2159 curl_easy_setopt (ps->recv_endpoint, CURLOPT_DEBUGDATA,
2163 curl_easy_setopt (ps->recv_endpoint, CURLOPT_SSLVERSION,
2164 CURL_SSLVERSION_TLSv1);
2165 curl_easy_setopt (ps->recv_endpoint, CURLOPT_SSL_VERIFYPEER, 0);
2166 curl_easy_setopt (ps->recv_endpoint, CURLOPT_SSL_VERIFYHOST, 0);
2168 curl_easy_setopt (ps->recv_endpoint, CURLOPT_URL, ps->url);
2169 curl_easy_setopt (ps->recv_endpoint, CURLOPT_HEADERFUNCTION,
2170 &curl_get_header_cb);
2171 curl_easy_setopt (ps->recv_endpoint, CURLOPT_WRITEHEADER, ps);
2172 curl_easy_setopt (ps->recv_endpoint, CURLOPT_READFUNCTION, curl_send_cb);
2173 curl_easy_setopt (ps->recv_endpoint, CURLOPT_READDATA, ps);
2174 curl_easy_setopt (ps->recv_endpoint, CURLOPT_WRITEFUNCTION,
2176 curl_easy_setopt (ps->recv_endpoint, CURLOPT_WRITEDATA, ps);
2177 curl_easy_setopt (ps->recv_endpoint, CURLOPT_TIMEOUT,
2178 (long) timeout.rel_value);
2179 curl_easy_setopt (ps->recv_endpoint, CURLOPT_PRIVATE, ps);
2180 curl_easy_setopt (ps->recv_endpoint, CURLOPT_CONNECTTIMEOUT,
2181 HTTP_CONNECT_TIMEOUT);
2182 curl_easy_setopt (ps->recv_endpoint, CURLOPT_BUFFERSIZE,
2183 2 * GNUNET_SERVER_MAX_MESSAGE_SIZE);
2184 #if CURL_TCP_NODELAY
2185 curl_easy_setopt (ps->recv_endpoint, CURLOPT_TCP_NODELAY, 1);
2187 if (fresh == GNUNET_YES)
2189 mret = curl_multi_add_handle (plugin->multi_handle, ps->recv_endpoint);
2190 if (mret != CURLM_OK)
2192 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2193 _("Connection: %X: %s failed at %s:%d: `%s'\n"), ps,
2194 "curl_multi_add_handle", __FILE__, __LINE__,
2195 curl_multi_strerror (mret));
2196 return GNUNET_SYSERR;
2199 if (plugin->http_curl_task != GNUNET_SCHEDULER_NO_TASK)
2201 GNUNET_SCHEDULER_cancel (plugin->http_curl_task);
2202 plugin->http_curl_task = GNUNET_SCHEDULER_NO_TASK;
2204 plugin->current_connections++;
2205 plugin->http_curl_task = GNUNET_SCHEDULER_add_now (&curl_perform, plugin);
2208 /* waiting for receive direction */
2209 if (ps->recv_connected == GNUNET_NO)
2212 /* SEND DIRECTION */
2213 /* Check if session is connected to send data, otherwise connect to peer */
2214 if ((ps->send_connected == GNUNET_YES) && (ps->send_endpoint != NULL))
2216 if (ps->send_active == GNUNET_YES)
2218 #if DEBUG_CONNECTIONS
2219 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2220 "Connection %X: outbound active, enqueueing message\n", ps);
2224 if (ps->send_active == GNUNET_NO)
2226 #if DEBUG_CONNECTIONS
2227 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2228 "Connection %X: outbound paused, unpausing existing connection and enqueueing message\n",
2231 if (CURLE_OK == curl_easy_pause (ps->send_endpoint, CURLPAUSE_CONT))
2233 ps->send_active = GNUNET_YES;
2234 if (plugin->http_curl_task != GNUNET_SCHEDULER_NO_TASK)
2236 GNUNET_SCHEDULER_cancel (plugin->http_curl_task);
2237 plugin->http_curl_task = GNUNET_SCHEDULER_NO_TASK;
2239 plugin->http_curl_task =
2240 GNUNET_SCHEDULER_add_now (&curl_perform, plugin);
2244 return GNUNET_SYSERR;
2247 /* not connected, initiate connection */
2248 if ((ps->send_connected == GNUNET_NO) &&
2249 (plugin->current_connections < plugin->max_connect_per_transport))
2251 int fresh = GNUNET_NO;
2253 if (NULL == ps->send_endpoint)
2255 ps->send_endpoint = curl_easy_init ();
2258 GNUNET_assert (ps->send_endpoint != NULL);
2259 GNUNET_assert (NULL != ps->pending_msgs_tail);
2260 #if DEBUG_CONNECTIONS
2261 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2262 "Connection %X: outbound not connected, initiating connection\n",
2265 ps->send_active = GNUNET_NO;
2268 curl_easy_setopt (ps->send_endpoint, CURLOPT_VERBOSE, 1L);
2269 curl_easy_setopt (ps->send_endpoint, CURLOPT_DEBUGFUNCTION, &curl_logger);
2270 curl_easy_setopt (ps->send_endpoint, CURLOPT_DEBUGDATA,
2274 curl_easy_setopt (ps->send_endpoint, CURLOPT_SSLVERSION,
2275 CURL_SSLVERSION_TLSv1);
2276 curl_easy_setopt (ps->send_endpoint, CURLOPT_SSL_VERIFYPEER, 0);
2277 curl_easy_setopt (ps->send_endpoint, CURLOPT_SSL_VERIFYHOST, 0);
2279 curl_easy_setopt (ps->send_endpoint, CURLOPT_URL, ps->url);
2280 curl_easy_setopt (ps->send_endpoint, CURLOPT_PUT, 1L);
2281 curl_easy_setopt (ps->send_endpoint, CURLOPT_HEADERFUNCTION,
2282 &curl_put_header_cb);
2283 curl_easy_setopt (ps->send_endpoint, CURLOPT_WRITEHEADER, ps);
2284 curl_easy_setopt (ps->send_endpoint, CURLOPT_READFUNCTION, curl_send_cb);
2285 curl_easy_setopt (ps->send_endpoint, CURLOPT_READDATA, ps);
2286 curl_easy_setopt (ps->send_endpoint, CURLOPT_WRITEFUNCTION,
2288 curl_easy_setopt (ps->send_endpoint, CURLOPT_READDATA, ps);
2289 curl_easy_setopt (ps->send_endpoint, CURLOPT_TIMEOUT,
2290 (long) timeout.rel_value);
2291 curl_easy_setopt (ps->send_endpoint, CURLOPT_PRIVATE, ps);
2292 curl_easy_setopt (ps->send_endpoint, CURLOPT_CONNECTTIMEOUT,
2293 HTTP_CONNECT_TIMEOUT);
2294 curl_easy_setopt (ps->send_endpoint, CURLOPT_BUFFERSIZE,
2295 2 * GNUNET_SERVER_MAX_MESSAGE_SIZE);
2296 #if CURL_TCP_NODELAY
2297 curl_easy_setopt (ps->send_endpoint, CURLOPT_TCP_NODELAY, 1);
2299 if (fresh == GNUNET_YES)
2301 mret = curl_multi_add_handle (plugin->multi_handle, ps->send_endpoint);
2302 if (mret != CURLM_OK)
2304 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2305 _("Connection: %X: %s failed at %s:%d: `%s'\n"), ps,
2306 "curl_multi_add_handle", __FILE__, __LINE__,
2307 curl_multi_strerror (mret));
2308 return GNUNET_SYSERR;
2312 if (plugin->http_curl_task != GNUNET_SCHEDULER_NO_TASK)
2314 GNUNET_SCHEDULER_cancel (plugin->http_curl_task);
2315 plugin->http_curl_task = GNUNET_SCHEDULER_NO_TASK;
2317 plugin->current_connections++;
2318 plugin->http_curl_task = GNUNET_SCHEDULER_add_now (&curl_perform, plugin);
2321 if (ps->direction == INBOUND)
2323 GNUNET_assert (NULL != ps->pending_msgs_tail);
2324 if ((ps->recv_connected == GNUNET_YES) && (ps->send_connected == GNUNET_YES)
2325 && (ps->recv_force_disconnect == GNUNET_NO) &&
2326 (ps->recv_force_disconnect == GNUNET_NO))
2329 return GNUNET_SYSERR;
2334 * select best session to transmit data to peer
2336 * @param pc peer context of target peer
2337 * @param addr address of target peer
2338 * @param addrlen address length
2339 * @param force_address does transport service enforce address?
2340 * @param session session passed by transport service
2341 * @return selected session
2344 static struct Session *
2345 send_select_session (struct HTTP_PeerContext *pc, const void *addr,
2346 size_t addrlen, int force_address, struct Session *session)
2348 struct Session *tmp = NULL;
2349 int addr_given = GNUNET_NO;
2351 if ((addr != NULL) && (addrlen > 0))
2352 addr_given = GNUNET_YES;
2354 if (force_address == GNUNET_YES)
2356 /* check session given as argument */
2357 if ((session != NULL) && (addr_given == GNUNET_YES))
2359 if (0 == memcmp (session->addr, addr, addrlen))
2361 /* connection can not be used, since it is disconnected */
2362 if ((session->recv_force_disconnect == GNUNET_NO) &&
2363 (session->send_force_disconnect == GNUNET_NO))
2365 #if DEBUG_SESSION_SELECTION
2366 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2367 "Session %X selected: Using session passed by transport to send to forced address \n",
2374 /* check last session used */
2375 if ((pc->last_session != NULL) && (addr_given == GNUNET_YES))
2377 if (0 == memcmp (pc->last_session->addr, addr, addrlen))
2379 /* connection can not be used, since it is disconnected */
2380 if ((pc->last_session->recv_force_disconnect == GNUNET_NO) &&
2381 (pc->last_session->send_force_disconnect == GNUNET_NO))
2383 #if DEBUG_SESSION_SELECTION
2384 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2385 "Session %X selected: Using last session used to send to forced address \n",
2388 return pc->last_session;
2392 /* find session in existing sessions */
2394 while ((tmp != NULL) && (addr_given == GNUNET_YES))
2396 if (0 == memcmp (tmp->addr, addr, addrlen))
2398 /* connection can not be used, since it is disconnected */
2399 if ((tmp->recv_force_disconnect == GNUNET_NO) &&
2400 (tmp->send_force_disconnect == GNUNET_NO))
2402 #if DEBUG_SESSION_SELECTION
2403 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2404 "Session %X selected: Using existing session to send to forced address \n",
2412 /* no session to use */
2415 if ((force_address == GNUNET_NO) || (force_address == GNUNET_SYSERR))
2417 /* check session given as argument */
2418 if (session != NULL)
2420 /* connection can not be used, since it is disconnected */
2421 if ((session->recv_force_disconnect == GNUNET_NO) &&
2422 (session->send_force_disconnect == GNUNET_NO))
2424 #if DEBUG_SESSION_SELECTION
2425 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2426 "Session %X selected: Using session passed by transport to send not-forced address\n",
2432 /* check last session used */
2433 if (pc->last_session != NULL)
2435 /* connection can not be used, since it is disconnected */
2436 if ((pc->last_session->recv_force_disconnect == GNUNET_NO) &&
2437 (pc->last_session->send_force_disconnect == GNUNET_NO))
2439 #if DEBUG_SESSION_SELECTION
2440 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2441 "Session %X selected: Using last session to send to not-forced address\n",
2444 return pc->last_session;
2447 /* find session in existing sessions */
2451 /* connection can not be used, since it is disconnected */
2452 if ((tmp->recv_force_disconnect == GNUNET_NO) &&
2453 (tmp->send_force_disconnect == GNUNET_NO))
2455 #if DEBUG_SESSION_SELECTION
2456 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2457 "Session %X selected: Using existing session to send to not-forced address\n",
2471 * Function that can be used by the transport service to transmit
2472 * a message using the plugin. Note that in the case of a
2473 * peer disconnecting, the continuation MUST be called
2474 * prior to the disconnect notification itself. This function
2475 * will be called with this peer's HELLO message to initiate
2476 * a fresh connection to another peer.
2478 * @param cls closure
2479 * @param target who should receive this message
2480 * @param msgbuf the message to transmit
2481 * @param msgbuf_size number of bytes in 'msgbuf'
2482 * @param priority how important is the message (most plugins will
2483 * ignore message priority and just FIFO)
2484 * @param to how long to wait at most for the transmission (does not
2485 * require plugins to discard the message after the timeout,
2486 * just advisory for the desired delay; most plugins will ignore
2488 * @param session which session must be used (or NULL for "any")
2489 * @param addr the address to use (can be NULL if the plugin
2490 * is "on its own" (i.e. re-use existing TCP connection))
2491 * @param addrlen length of the address in bytes
2492 * @param force_address GNUNET_YES if the plugin MUST use the given address,
2493 * GNUNET_NO means the plugin may use any other address and
2494 * GNUNET_SYSERR means that only reliable existing
2495 * bi-directional connections should be used (regardless
2497 * @param cont continuation to call once the message has
2498 * been transmitted (or if the transport is ready
2499 * for the next transmission call; or if the
2500 * peer disconnected...); can be NULL
2501 * @param cont_cls closure for cont
2502 * @return number of bytes used (on the physical network, with overheads);
2503 * -1 on hard errors (i.e. address invalid); 0 is a legal value
2504 * and does NOT mean that the message was not transmitted (DV)
2507 http_plugin_send (void *cls, const struct GNUNET_PeerIdentity *target,
2508 const char *msgbuf, size_t msgbuf_size, unsigned int priority,
2509 struct GNUNET_TIME_Relative to, struct Session *session,
2510 const void *addr, size_t addrlen, int force_address,
2511 GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
2513 struct Plugin *plugin = cls;
2514 struct HTTP_Message *msg;
2515 struct HTTP_PeerContext *pc;
2516 struct Session *ps = NULL;
2518 GNUNET_assert (cls != NULL);
2523 if (force_address == GNUNET_YES)
2524 GNUNET_asprintf (&force, "forced addr.");
2525 else if (force_address == GNUNET_NO)
2526 GNUNET_asprintf (&force, "any addr.");
2527 else if (force_address == GNUNET_SYSERR)
2528 GNUNET_asprintf (&force, "reliable bi-direc. address addr.");
2531 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2532 "Transport tells me to send %u bytes to `%s' using %s (%s) and session: %X\n",
2533 msgbuf_size, GNUNET_i2s (target), force,
2534 http_plugin_address_to_string (NULL, addr, addrlen), session);
2535 GNUNET_free (force);
2538 pc = GNUNET_CONTAINER_multihashmap_get (plugin->peers, &target->hashPubKey);
2542 pc = GNUNET_malloc (sizeof (struct HTTP_PeerContext));
2543 pc->plugin = plugin;
2544 pc->session_id_counter = 1;
2545 pc->last_session = NULL;
2546 memcpy (&pc->identity, target, sizeof (struct GNUNET_PeerIdentity));
2547 GNUNET_CONTAINER_multihashmap_put (plugin->peers, &pc->identity.hashPubKey,
2549 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
2550 GNUNET_STATISTICS_update (plugin->env->stats,
2551 gettext_noop ("# HTTP peers active"), 1,
2554 ps = send_select_session (pc, addr, addrlen, force_address, session);
2555 /* session not existing, but address forced -> creating new session */
2558 if ((addr != NULL) && (addrlen != 0))
2560 ps = GNUNET_malloc (sizeof (struct Session));
2561 #if DEBUG_SESSION_SELECTION
2562 if (force_address == GNUNET_YES)
2563 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2564 "No existing connection & forced address: creating new session %X to peer %s\n",
2565 ps, GNUNET_i2s (target));
2566 if (force_address != GNUNET_YES)
2567 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2568 "No existing connection: creating new session %X to peer %s\n",
2569 ps, GNUNET_i2s (target));
2571 ps->addr = GNUNET_malloc (addrlen);
2572 memcpy (ps->addr, addr, addrlen);
2573 ps->addrlen = addrlen;
2574 ps->direction = OUTBOUND;
2575 ps->recv_connected = GNUNET_NO;
2576 ps->recv_force_disconnect = GNUNET_NO;
2577 ps->send_connected = GNUNET_NO;
2578 ps->send_force_disconnect = GNUNET_NO;
2579 ps->pending_msgs_head = NULL;
2580 ps->pending_msgs_tail = NULL;
2581 ps->peercontext = pc;
2582 ps->session_id = pc->session_id_counter;
2583 ps->queue_length_cur = 0;
2584 ps->queue_length_max = GNUNET_SERVER_MAX_MESSAGE_SIZE;
2585 pc->session_id_counter++;
2586 ps->url = create_url (plugin, ps->addr, ps->addrlen, ps->session_id);
2587 if (ps->msgtok == NULL)
2588 ps->msgtok = GNUNET_SERVER_mst_create (&curl_receive_mst_cb, ps);
2589 GNUNET_CONTAINER_DLL_insert (pc->head, pc->tail, ps);
2590 GNUNET_STATISTICS_update (plugin->env->stats,
2592 ("# HTTP outbound sessions for peers active"),
2598 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2599 "No existing session found & and no address given: no way to send this message to peer `%s'!\n",
2600 GNUNET_i2s (target));
2602 return GNUNET_SYSERR;
2606 if (msgbuf_size >= (ps->queue_length_max - ps->queue_length_cur))
2608 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2609 "Queue %X full: %u bytes in queue available, message with %u is too big\n",
2610 ps, (ps->queue_length_max - ps->queue_length_cur), msgbuf_size);
2611 //return GNUNET_SYSERR;
2615 msg = GNUNET_malloc (sizeof (struct HTTP_Message) + msgbuf_size);
2617 msg->size = msgbuf_size;
2619 msg->buf = (char *) &msg[1];
2620 msg->transmit_cont = cont;
2621 msg->transmit_cont_cls = cont_cls;
2622 memcpy (msg->buf, msgbuf, msgbuf_size);
2623 GNUNET_CONTAINER_DLL_insert (ps->pending_msgs_head, ps->pending_msgs_tail,
2625 ps->queue_length_cur += msgbuf_size;
2626 if (send_check_connections (plugin, ps) == GNUNET_SYSERR)
2627 return GNUNET_SYSERR;
2628 if (force_address != GNUNET_YES)
2629 pc->last_session = ps;
2630 if (pc->last_session == NULL)
2631 pc->last_session = ps;
2637 * Function that can be used to force the plugin to disconnect
2638 * from the given peer and cancel all previous transmissions
2639 * (and their continuationc).
2641 * @param cls closure
2642 * @param target peer from which to disconnect
2645 http_plugin_disconnect (void *cls, const struct GNUNET_PeerIdentity *target)
2647 struct Plugin *plugin = cls;
2648 struct HTTP_PeerContext *pc = NULL;
2649 struct Session *ps = NULL;
2651 pc = GNUNET_CONTAINER_multihashmap_get (plugin->peers, &target->hashPubKey);
2657 /* Telling transport that session is getting disconnected */
2658 plugin->env->session_end (plugin, target, ps);
2659 if (ps->direction == OUTBOUND)
2661 if (ps->send_endpoint != NULL)
2663 //GNUNET_assert(CURLM_OK == curl_multi_remove_handle(plugin->multi_handle,ps->send_endpoint));
2664 //curl_easy_cleanup(ps->send_endpoint);
2665 //ps->send_endpoint=NULL;
2666 ps->send_force_disconnect = GNUNET_YES;
2668 if (ps->recv_endpoint != NULL)
2670 //GNUNET_assert(CURLM_OK == curl_multi_remove_handle(plugin->multi_handle,ps->recv_endpoint));
2671 //curl_easy_cleanup(ps->recv_endpoint);
2672 //ps->recv_endpoint=NULL;
2673 ps->recv_force_disconnect = GNUNET_YES;
2676 if (ps->direction == INBOUND)
2678 ps->recv_force_disconnect = GNUNET_YES;
2679 ps->send_force_disconnect = GNUNET_YES;
2681 while (ps->pending_msgs_head != NULL)
2682 remove_http_message (ps, ps->pending_msgs_head);
2683 ps->recv_active = GNUNET_NO;
2684 ps->send_active = GNUNET_NO;
2691 * Append our port and forward the result.
2693 * @param cls the 'struct PrettyPrinterContext*'
2694 * @param hostname hostname part of the address
2697 append_port (void *cls, const char *hostname)
2699 struct PrettyPrinterContext *ppc = cls;
2702 if (hostname == NULL)
2704 ppc->asc (ppc->asc_cls, NULL);
2708 GNUNET_asprintf (&ret, "%s://%s:%d", PROTOCOL_PREFIX, hostname, ppc->port);
2710 ppc->asc (ppc->asc_cls, ret);
2717 * Convert the transports address to a nice, human-readable
2720 * @param cls closure
2721 * @param type name of the transport that generated the address
2722 * @param addr one of the addresses of the host, NULL for the last address
2723 * the specific address format depends on the transport
2724 * @param addrlen length of the address
2725 * @param numeric should (IP) addresses be displayed in numeric form?
2726 * @param timeout after how long should we give up?
2727 * @param asc function to call on each string
2728 * @param asc_cls closure for asc
2731 http_plugin_address_pretty_printer (void *cls, const char *type,
2732 const void *addr, size_t addrlen,
2734 struct GNUNET_TIME_Relative timeout,
2735 GNUNET_TRANSPORT_AddressStringCallback asc,
2738 struct PrettyPrinterContext *ppc;
2741 struct sockaddr_in a4;
2742 struct sockaddr_in6 a6;
2743 const struct IPv4HttpAddress *t4;
2744 const struct IPv6HttpAddress *t6;
2747 if (addrlen == sizeof (struct IPv6HttpAddress))
2750 memset (&a6, 0, sizeof (a6));
2751 a6.sin6_family = AF_INET6;
2752 a6.sin6_port = t6->port;
2753 memcpy (&a6.sin6_addr, &t6->ipv6_addr, sizeof (struct in6_addr));
2754 port = ntohs (t6->port);
2758 else if (addrlen == sizeof (struct IPv4HttpAddress))
2761 memset (&a4, 0, sizeof (a4));
2762 a4.sin_family = AF_INET;
2763 a4.sin_port = t4->port;
2764 a4.sin_addr.s_addr = t4->ipv4_addr;
2765 port = ntohs (t4->ipv4_addr);
2771 /* invalid address */
2772 GNUNET_break_op (0);
2773 asc (asc_cls, NULL);
2776 ppc = GNUNET_malloc (sizeof (struct PrettyPrinterContext));
2778 ppc->asc_cls = asc_cls;
2780 GNUNET_RESOLVER_hostname_get (sb, sbs, !numeric, timeout, &append_port, ppc);
2786 * Another peer has suggested an address for this
2787 * peer and transport plugin. Check that this could be a valid
2788 * address. If so, consider adding it to the list
2791 * @param cls closure
2792 * @param addr pointer to the address
2793 * @param addrlen length of addr
2794 * @return GNUNET_OK if this is a plausible address for this peer
2798 http_plugin_address_suggested (void *cls, const void *addr, size_t addrlen)
2800 struct Plugin *plugin = cls;
2801 struct IPv4HttpAddress *v4;
2802 struct IPv6HttpAddress *v6;
2803 struct IPv4HttpAddressWrapper *w_tv4 = plugin->ipv4_addr_head;
2804 struct IPv6HttpAddressWrapper *w_tv6 = plugin->ipv6_addr_head;
2806 GNUNET_assert (cls != NULL);
2807 if ((addrlen != sizeof (struct IPv4HttpAddress)) &&
2808 (addrlen != sizeof (struct IPv6HttpAddress)))
2809 return GNUNET_SYSERR;
2810 if (addrlen == sizeof (struct IPv4HttpAddress))
2812 v4 = (struct IPv4HttpAddress *) addr;
2813 if (plugin->bind4_address != NULL)
2816 memcmp (&plugin->bind4_address->sin_addr, &v4->ipv4_addr,
2820 return GNUNET_SYSERR;
2822 while (w_tv4 != NULL)
2825 memcmp (&w_tv4->addr->ipv4_addr, &v4->ipv4_addr, sizeof (uint32_t)))
2827 w_tv4 = w_tv4->next;
2832 return GNUNET_SYSERR;
2834 if (addrlen == sizeof (struct IPv6HttpAddress))
2836 v6 = (struct IPv6HttpAddress *) addr;
2837 if (plugin->bind6_address != NULL)
2840 memcmp (&plugin->bind6_address->sin6_addr, &v6->ipv6_addr,
2841 sizeof (struct in6_addr)))
2844 return GNUNET_SYSERR;
2846 while (w_tv6 != NULL)
2849 memcmp (&w_tv6->addr->ipv6_addr, &v6->ipv6_addr,
2850 sizeof (struct in6_addr)))
2852 w_tv6 = w_tv6->next;
2857 return GNUNET_SYSERR;
2859 return GNUNET_SYSERR;
2864 * Function called for a quick conversion of the binary address to
2865 * a numeric address. Note that the caller must not free the
2866 * address and that the next call to this function is allowed
2867 * to override the address again.
2869 * @param cls closure
2870 * @param addr binary address
2871 * @param addrlen length of the address
2872 * @return string representing the same address
2875 http_plugin_address_to_string (void *cls, const void *addr, size_t addrlen)
2877 const struct IPv4HttpAddress *t4;
2878 const struct IPv6HttpAddress *t6;
2879 struct sockaddr_in a4;
2880 struct sockaddr_in6 a6;
2882 static char rbuf[INET6_ADDRSTRLEN + 13];
2886 if (addrlen == sizeof (struct IPv6HttpAddress))
2888 address = GNUNET_malloc (INET6_ADDRSTRLEN);
2890 a6.sin6_addr = t6->ipv6_addr;
2891 inet_ntop (AF_INET6, &(a6.sin6_addr), address, INET6_ADDRSTRLEN);
2892 port = ntohs (t6->port);
2894 else if (addrlen == sizeof (struct IPv4HttpAddress))
2896 address = GNUNET_malloc (INET_ADDRSTRLEN);
2898 a4.sin_addr.s_addr = t4->ipv4_addr;
2899 inet_ntop (AF_INET, &(a4.sin_addr), address, INET_ADDRSTRLEN);
2900 port = ntohs (t4->port);
2904 /* invalid address */
2908 res = GNUNET_snprintf (rbuf, sizeof (rbuf),
2909 (addrlen == sizeof (struct IPv6HttpAddress)) ? "[%s]:%u" : "%s:%u",
2912 GNUNET_free (address);
2913 GNUNET_assert (res != 0);
2918 * Function called by the NAT subsystem suggesting another peer wants
2919 * to connect to us via connection reversal. Try to connect back to the
2922 * @param cls closure
2923 * @param addr address to try
2924 * @param addrlen number of bytes in addr
2927 try_connection_reversal (void *cls, const struct sockaddr *addr,
2934 tcp_nat_cb_add_addr (void *cls, int add_remove, const struct sockaddr *addr,
2937 struct Plugin *plugin = cls;
2938 struct IPv4HttpAddress *t4 = NULL;
2939 struct IPv4HttpAddressWrapper *w_t4 = NULL;
2940 struct IPv6HttpAddress *t6 = NULL;
2941 struct IPv6HttpAddressWrapper *w_t6 = NULL;
2944 af = addr->sa_family;
2948 w_t4 = plugin->ipv4_addr_head;
2949 while (w_t4 != NULL)
2951 int res = memcmp (&w_t4->addr->ipv4_addr,
2952 &((struct sockaddr_in *) addr)->sin_addr,
2953 sizeof (struct in_addr));
2961 w_t4 = GNUNET_malloc (sizeof (struct IPv4HttpAddressWrapper));
2962 t4 = GNUNET_malloc (sizeof (struct IPv4HttpAddress));
2963 memcpy (&t4->ipv4_addr, &((struct sockaddr_in *) addr)->sin_addr,
2964 sizeof (struct in_addr));
2965 t4->port = htons (plugin->port_inbound);
2969 GNUNET_CONTAINER_DLL_insert (plugin->ipv4_addr_head,
2970 plugin->ipv4_addr_tail, w_t4);
2972 plugin->env->notify_address (plugin->env->cls, add_remove, w_t4->addr,
2973 sizeof (struct IPv4HttpAddress));
2977 w_t6 = plugin->ipv6_addr_head;
2980 int res = memcmp (&w_t6->addr->ipv6_addr,
2981 &((struct sockaddr_in6 *) addr)->sin6_addr,
2982 sizeof (struct in6_addr));
2990 w_t6 = GNUNET_malloc (sizeof (struct IPv6HttpAddressWrapper));
2991 t6 = GNUNET_malloc (sizeof (struct IPv6HttpAddress));
2993 memcpy (&t6->ipv6_addr, &((struct sockaddr_in6 *) addr)->sin6_addr,
2994 sizeof (struct in6_addr));
2995 t6->port = htons (plugin->port_inbound);
2999 GNUNET_CONTAINER_DLL_insert (plugin->ipv6_addr_head,
3000 plugin->ipv6_addr_tail, w_t6);
3002 plugin->env->notify_address (plugin->env->cls, add_remove, w_t6->addr,
3003 sizeof (struct IPv6HttpAddress));
3012 tcp_nat_cb_remove_addr (void *cls, int add_remove, const struct sockaddr *addr,
3015 struct Plugin *plugin = cls;
3016 struct IPv4HttpAddressWrapper *w_t4 = NULL;
3017 struct IPv6HttpAddressWrapper *w_t6 = NULL;
3020 af = addr->sa_family;
3024 w_t4 = plugin->ipv4_addr_head;
3025 while (w_t4 != NULL)
3027 int res = memcmp (&w_t4->addr->ipv4_addr,
3028 &((struct sockaddr_in *) addr)->sin_addr,
3029 sizeof (struct in_addr));
3037 plugin->env->notify_address (plugin->env->cls, add_remove, w_t4->addr,
3038 sizeof (struct IPv4HttpAddress));
3040 GNUNET_CONTAINER_DLL_remove (plugin->ipv4_addr_head, plugin->ipv4_addr_tail,
3042 GNUNET_free (w_t4->addr);
3046 w_t6 = plugin->ipv6_addr_head;
3047 while (w_t6 != NULL)
3049 int res = memcmp (&w_t6->addr->ipv6_addr,
3050 &((struct sockaddr_in6 *) addr)->sin6_addr,
3051 sizeof (struct in6_addr));
3059 plugin->env->notify_address (plugin->env->cls, add_remove, w_t6->addr,
3060 sizeof (struct IPv6HttpAddress));
3062 GNUNET_CONTAINER_DLL_remove (plugin->ipv6_addr_head, plugin->ipv6_addr_tail,
3064 GNUNET_free (w_t6->addr);
3074 * Our external IP address/port mapping has changed.
3076 * @param cls closure, the 'struct LocalAddrList'
3077 * @param add_remove GNUNET_YES to mean the new public IP address, GNUNET_NO to mean
3078 * the previous (now invalid) one
3079 * @param addr either the previous or the new public IP address
3080 * @param addrlen actual lenght of the address
3083 tcp_nat_port_map_callback (void *cls, int add_remove,
3084 const struct sockaddr *addr, socklen_t addrlen)
3086 GNUNET_assert (cls != NULL);
3088 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "NPMC called to %s address `%s'\n",
3089 (add_remove == GNUNET_YES) ? "remove" : "add",
3090 GNUNET_a2s (addr, addrlen));
3092 /* convert 'addr' to our internal format */
3096 tcp_nat_cb_add_addr (cls, add_remove, addr, addrlen);
3099 tcp_nat_cb_remove_addr (cls, add_remove, addr, addrlen);
3105 * Exit point from the plugin.
3108 LIBGNUNET_PLUGIN_TRANSPORT_DONE (void *cls)
3110 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
3111 struct Plugin *plugin = api->cls;
3113 struct IPv4HttpAddressWrapper *w_t4;
3114 struct IPv6HttpAddressWrapper *w_t6;
3116 GNUNET_assert (cls != NULL);
3118 if (plugin->nat != NULL)
3119 GNUNET_NAT_unregister (plugin->nat);
3121 if (plugin->http_server_daemon_v4 != NULL)
3123 MHD_stop_daemon (plugin->http_server_daemon_v4);
3124 plugin->http_server_daemon_v4 = NULL;
3126 if (plugin->http_server_daemon_v6 != NULL)
3128 MHD_stop_daemon (plugin->http_server_daemon_v6);
3129 plugin->http_server_daemon_v6 = NULL;
3131 if (plugin->http_server_task_v4 != GNUNET_SCHEDULER_NO_TASK)
3133 GNUNET_SCHEDULER_cancel (plugin->http_server_task_v4);
3134 plugin->http_server_task_v4 = GNUNET_SCHEDULER_NO_TASK;
3136 if (plugin->http_server_task_v6 != GNUNET_SCHEDULER_NO_TASK)
3138 GNUNET_SCHEDULER_cancel (plugin->http_server_task_v6);
3139 plugin->http_server_task_v6 = GNUNET_SCHEDULER_NO_TASK;
3142 while (plugin->ipv4_addr_head != NULL)
3144 w_t4 = plugin->ipv4_addr_head;
3145 GNUNET_CONTAINER_DLL_remove (plugin->ipv4_addr_head, plugin->ipv4_addr_tail,
3147 GNUNET_free (w_t4->addr);
3151 while (plugin->ipv6_addr_head != NULL)
3153 w_t6 = plugin->ipv6_addr_head;
3154 GNUNET_CONTAINER_DLL_remove (plugin->ipv6_addr_head, plugin->ipv6_addr_tail,
3156 GNUNET_free (w_t6->addr);
3160 /* free all peer information */
3161 if (plugin->peers != NULL)
3163 GNUNET_CONTAINER_multihashmap_iterate (plugin->peers,
3164 &remove_peer_context_Iterator,
3166 GNUNET_CONTAINER_multihashmap_destroy (plugin->peers);
3168 if (plugin->multi_handle != NULL)
3170 mret = curl_multi_cleanup (plugin->multi_handle);
3171 if (CURLM_OK != mret)
3172 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3173 "curl multihandle clean up failed\n");
3174 plugin->multi_handle = NULL;
3176 curl_global_cleanup ();
3178 if (plugin->http_curl_task != GNUNET_SCHEDULER_NO_TASK)
3180 GNUNET_SCHEDULER_cancel (plugin->http_curl_task);
3181 plugin->http_curl_task = GNUNET_SCHEDULER_NO_TASK;
3185 GNUNET_free_non_null (plugin->bind4_address);
3186 GNUNET_free_non_null (plugin->bind6_address);
3187 GNUNET_free_non_null (plugin->bind_hostname);
3189 GNUNET_free_non_null (plugin->crypto_init);
3190 GNUNET_free_non_null (plugin->cert);
3191 GNUNET_free_non_null (plugin->key);
3193 GNUNET_free (plugin);
3196 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Unload %s plugin complete...\n",
3204 load_certificate (const char *file)
3206 struct GNUNET_DISK_FileHandle *gn_file;
3210 if (0 != STAT (file, &fstat))
3212 text = GNUNET_malloc (fstat.st_size + 1);
3214 GNUNET_DISK_file_open (file, GNUNET_DISK_OPEN_READ,
3215 GNUNET_DISK_PERM_USER_READ);
3216 if (gn_file == NULL)
3221 if (GNUNET_SYSERR == GNUNET_DISK_file_read (gn_file, text, fstat.st_size))
3224 GNUNET_DISK_file_close (gn_file);
3227 text[fstat.st_size] = '\0';
3228 GNUNET_DISK_file_close (gn_file);
3235 * Entry point for the plugin.
3238 LIBGNUNET_PLUGIN_TRANSPORT_INIT (void *cls)
3240 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
3241 struct Plugin *plugin;
3242 struct GNUNET_TRANSPORT_PluginFunctions *api;
3243 struct GNUNET_TIME_Relative gn_timeout;
3244 long long unsigned int port;
3245 unsigned long long tneigh;
3246 struct sockaddr **addrs;
3247 socklen_t *addrlens;
3249 char *component_name;
3252 char *key_file = NULL;
3253 char *cert_file = NULL;
3256 GNUNET_assert (cls != NULL);
3258 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Starting %s plugin...\n",
3261 GNUNET_asprintf (&component_name, "transport-%s", PROTOCOL_PREFIX);
3263 plugin = GNUNET_malloc (sizeof (struct Plugin));
3264 plugin->stats = env->stats;
3266 plugin->peers = NULL;
3267 plugin->bind4_address = NULL;
3268 plugin->bind6_address = NULL;
3269 plugin->use_ipv6 = GNUNET_YES;
3270 plugin->use_ipv4 = GNUNET_YES;
3271 plugin->use_localaddresses = GNUNET_NO;
3273 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
3275 api->send = &http_plugin_send;
3276 api->disconnect = &http_plugin_disconnect;
3277 api->address_pretty_printer = &http_plugin_address_pretty_printer;
3278 api->check_address = &http_plugin_address_suggested;
3279 api->address_to_string = &http_plugin_address_to_string;
3281 /* Hashing our identity to use it in URLs */
3282 GNUNET_CRYPTO_hash_to_enc (&(plugin->env->my_identity->hashPubKey),
3283 &plugin->my_ascii_hash_ident);
3287 GNUNET_CONFIGURATION_get_value_number (env->cfg, component_name,
3288 "MAX_CONNECTIONS", &tneigh))
3290 plugin->max_connect_per_transport = tneigh;
3294 if (GNUNET_CONFIGURATION_have_value (env->cfg, component_name, "USE_IPv6"))
3297 GNUNET_CONFIGURATION_get_value_yesno (env->cfg, component_name,
3301 if (GNUNET_CONFIGURATION_have_value (env->cfg, component_name, "USE_IPv4"))
3304 GNUNET_CONFIGURATION_get_value_yesno (env->cfg, component_name,
3307 /* use local addresses? */
3309 if (GNUNET_CONFIGURATION_have_value
3310 (env->cfg, component_name, "USE_LOCALADDR"))
3312 plugin->use_localaddresses =
3313 GNUNET_CONFIGURATION_get_value_yesno (env->cfg, component_name,
3316 /* Reading port number from config file */
3318 GNUNET_CONFIGURATION_get_value_number (env->cfg, component_name, "PORT",
3319 &port)) || (port > 65535))
3321 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, component_name,
3323 ("Require valid port number for transport plugin `%s' in configuration!\n"),
3325 GNUNET_free (component_name);
3326 LIBGNUNET_PLUGIN_TRANSPORT_DONE (api);
3330 /* Reading ipv4 addresse to bind to from config file */
3331 if ((plugin->use_ipv4 == GNUNET_YES) &&
3332 (GNUNET_CONFIGURATION_have_value (env->cfg, component_name, "BINDTO4")))
3334 GNUNET_break (GNUNET_OK ==
3335 GNUNET_CONFIGURATION_get_value_string (env->cfg,
3338 &plugin->bind_hostname));
3339 plugin->bind4_address = GNUNET_malloc (sizeof (struct sockaddr_in));
3340 plugin->bind4_address->sin_family = AF_INET;
3341 plugin->bind4_address->sin_port = htons (port);
3343 if (plugin->bind_hostname != NULL)
3346 (AF_INET, plugin->bind_hostname,
3347 &plugin->bind4_address->sin_addr) <= 0)
3349 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, component_name,
3351 ("Misconfigured address to bind to in configuration!\n"));
3352 GNUNET_free (plugin->bind4_address);
3353 GNUNET_free (plugin->bind_hostname);
3354 plugin->bind_hostname = NULL;
3355 plugin->bind4_address = NULL;
3360 /* Reading ipv4 addresse to bind to from config file */
3361 if ((plugin->use_ipv6 == GNUNET_YES) &&
3362 (GNUNET_CONFIGURATION_have_value (env->cfg, component_name, "BINDTO6")))
3365 GNUNET_CONFIGURATION_get_value_string (env->cfg, component_name,
3367 &plugin->bind_hostname))
3369 plugin->bind6_address = GNUNET_malloc (sizeof (struct sockaddr_in6));
3370 plugin->bind6_address->sin6_family = AF_INET6;
3371 plugin->bind6_address->sin6_port = htons (port);
3372 if (plugin->bind_hostname != NULL)
3375 (AF_INET6, plugin->bind_hostname,
3376 &plugin->bind6_address->sin6_addr) <= 0)
3378 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, component_name,
3380 ("Misconfigured address to bind to in configuration!\n"));
3381 GNUNET_free (plugin->bind6_address);
3382 GNUNET_free (plugin->bind_hostname);
3383 plugin->bind_hostname = NULL;
3384 plugin->bind6_address = NULL;
3391 /* Reading HTTPS crypto related configuration */
3392 /* Get crypto init string from config */
3394 GNUNET_CONFIGURATION_get_value_string (env->cfg, "transport-https",
3396 &plugin->crypto_init)) ||
3398 GNUNET_CONFIGURATION_get_value_filename (env->cfg, "transport-https",
3399 "KEY_FILE", &key_file)) ||
3401 GNUNET_CONFIGURATION_get_value_filename (env->cfg, "transport-https",
3402 "CERT_FILE", &cert_file)))
3404 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, "https",
3406 ("Required configuration options missing in section `%s'\n"),
3408 GNUNET_free (component_name);
3409 GNUNET_free_non_null (key_file);
3410 GNUNET_free_non_null (cert_file);
3411 LIBGNUNET_PLUGIN_TRANSPORT_DONE (api);
3415 /* read key & certificates from file */
3416 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Loading TLS certificate `%s' `%s'\n",
3417 key_file, cert_file);
3419 plugin->key = load_certificate (key_file);
3420 plugin->cert = load_certificate (cert_file);
3422 if ((plugin->key == NULL) || (plugin->cert == NULL))
3424 struct GNUNET_OS_Process *certcreation;
3426 GNUNET_free_non_null (plugin->key);
3428 GNUNET_free_non_null (plugin->cert);
3429 plugin->cert = NULL;
3431 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3432 "No usable TLS certificate found, creating certificate\n");
3436 GNUNET_OS_start_process (NULL, NULL,
3437 "gnunet-transport-certificate-creation",
3438 "gnunet-transport-certificate-creation",
3439 key_file, cert_file, NULL);
3440 if (certcreation == NULL)
3442 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, "https",
3444 ("Could not create a new TLS certificate, program `gnunet-transport-certificate-creation' could not be started!\n"));
3445 GNUNET_free (key_file);
3446 GNUNET_free (cert_file);
3447 GNUNET_free (component_name);
3448 LIBGNUNET_PLUGIN_TRANSPORT_DONE (api);
3451 GNUNET_assert (GNUNET_OK == GNUNET_OS_process_wait (certcreation));
3452 GNUNET_OS_process_close (certcreation);
3453 plugin->key = load_certificate (key_file);
3454 plugin->cert = load_certificate (cert_file);
3456 if ((plugin->key == NULL) || (plugin->cert == NULL))
3458 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, "https",
3460 ("No usable TLS certificate found and creating one failed!\n"),
3462 GNUNET_free (key_file);
3463 GNUNET_free (cert_file);
3464 GNUNET_free (component_name);
3465 LIBGNUNET_PLUGIN_TRANSPORT_DONE (api);
3468 GNUNET_free (key_file);
3469 GNUNET_free (cert_file);
3471 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "TLS certificate loaded\n");
3475 GNUNET_assert ((port > 0) && (port <= 65535));
3476 plugin->port_inbound = port;
3477 gn_timeout = GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT;
3478 unsigned int timeout = (gn_timeout.rel_value) / 1000;
3480 if ((plugin->http_server_daemon_v6 == NULL) &&
3481 (plugin->use_ipv6 == GNUNET_YES) && (port != 0))
3483 struct sockaddr *tmp = (struct sockaddr *) plugin->bind6_address;
3485 plugin->http_server_daemon_v6 = MHD_start_daemon (
3493 &mhd_accept_cb, plugin,
3494 &mhd_access_cb, plugin,
3495 MHD_OPTION_SOCK_ADDR,
3497 MHD_OPTION_CONNECTION_LIMIT,
3499 plugin->max_connect_per_transport,
3501 MHD_OPTION_HTTPS_PRIORITIES,
3502 plugin->crypto_init,
3503 MHD_OPTION_HTTPS_MEM_KEY,
3505 MHD_OPTION_HTTPS_MEM_CERT,
3508 MHD_OPTION_CONNECTION_TIMEOUT,
3509 (unsigned int) timeout,
3510 MHD_OPTION_CONNECTION_MEMORY_LIMIT,
3512 GNUNET_SERVER_MAX_MESSAGE_SIZE),
3513 MHD_OPTION_NOTIFY_COMPLETED,
3514 &mhd_termination_cb,
3516 MHD_OPTION_EXTERNAL_LOGGER,
3521 if ((plugin->http_server_daemon_v4 == NULL) &&
3522 (plugin->use_ipv4 == GNUNET_YES) && (port != 0))
3524 plugin->http_server_daemon_v4 = MHD_start_daemon (
3532 &mhd_accept_cb, plugin,
3533 &mhd_access_cb, plugin,
3534 MHD_OPTION_SOCK_ADDR,
3535 (struct sockaddr_in *)
3536 plugin->bind4_address,
3537 MHD_OPTION_CONNECTION_LIMIT,
3539 plugin->max_connect_per_transport,
3541 MHD_OPTION_HTTPS_PRIORITIES,
3542 plugin->crypto_init,
3543 MHD_OPTION_HTTPS_MEM_KEY,
3545 MHD_OPTION_HTTPS_MEM_CERT,
3548 MHD_OPTION_CONNECTION_TIMEOUT,
3549 (unsigned int) timeout,
3550 MHD_OPTION_CONNECTION_MEMORY_LIMIT,
3552 GNUNET_SERVER_MAX_MESSAGE_SIZE),
3553 MHD_OPTION_NOTIFY_COMPLETED,
3554 &mhd_termination_cb,
3556 MHD_OPTION_EXTERNAL_LOGGER,
3561 if (plugin->http_server_daemon_v4 != NULL)
3562 plugin->http_server_task_v4 =
3563 http_server_daemon_prepare (plugin, plugin->http_server_daemon_v4);
3564 if (plugin->http_server_daemon_v6 != NULL)
3565 plugin->http_server_task_v6 =
3566 http_server_daemon_prepare (plugin, plugin->http_server_daemon_v6);
3569 if (plugin->http_server_task_v4 != GNUNET_SCHEDULER_NO_TASK)
3572 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3573 "Starting MHD with IPv4 bound to %s with port %u\n",
3574 (plugin->bind_hostname !=
3575 NULL) ? plugin->bind_hostname : "every address", port);
3578 else if ((plugin->http_server_task_v6 != GNUNET_SCHEDULER_NO_TASK) &&
3579 (plugin->http_server_task_v4 != GNUNET_SCHEDULER_NO_TASK))
3582 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3583 "Starting MHD with IPv6 bound to %s with port %u\n",
3584 (plugin->bind_hostname !=
3585 NULL) ? plugin->bind_hostname : "every address", port);
3588 else if ((plugin->http_server_task_v6 != GNUNET_SCHEDULER_NO_TASK) &&
3589 (plugin->http_server_task_v4 == GNUNET_SCHEDULER_NO_TASK))
3592 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3593 "Starting MHD with IPv4 and IPv6 bound to %s with port %u\n",
3594 (plugin->bind_hostname !=
3595 NULL) ? plugin->bind_hostname : "every address", port);
3602 if ((plugin->use_ipv6 == GNUNET_YES) && (plugin->use_ipv4 == GNUNET_YES))
3603 GNUNET_asprintf (&tmp, "with IPv4 and IPv6 enabled");
3604 if ((plugin->use_ipv6 == GNUNET_NO) && (plugin->use_ipv4 == GNUNET_YES))
3605 GNUNET_asprintf (&tmp, "with IPv4 enabled");
3606 if ((plugin->use_ipv6 == GNUNET_YES) && (plugin->use_ipv4 == GNUNET_NO))
3607 GNUNET_asprintf (&tmp, "with IPv6 enabled");
3608 if ((plugin->use_ipv6 == GNUNET_NO) && (plugin->use_ipv4 == GNUNET_NO))
3609 GNUNET_asprintf (&tmp, "with NO IP PROTOCOL enabled");
3610 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3612 ("HTTP Server with %s could not be started on port %u! %s plugin failed!\n"),
3613 tmp, port, PROTOCOL_PREFIX);
3615 GNUNET_free (component_name);
3616 LIBGNUNET_PLUGIN_TRANSPORT_DONE (api);
3620 /* Initializing cURL */
3621 curl_global_init (CURL_GLOBAL_ALL);
3622 plugin->multi_handle = curl_multi_init ();
3624 if (NULL == plugin->multi_handle)
3626 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, component_name,
3628 ("Could not initialize curl multi handle, failed to start %s plugin!\n"),
3630 GNUNET_free (component_name);
3631 LIBGNUNET_PLUGIN_TRANSPORT_DONE (api);
3636 GNUNET_SERVICE_get_server_addresses (component_name, env->cfg, &addrs,
3639 if (ret != GNUNET_SYSERR)
3642 GNUNET_NAT_register (env->cfg, GNUNET_YES, port, (unsigned int) ret,
3643 (const struct sockaddr **) addrs, addrlens,
3644 &tcp_nat_port_map_callback,
3645 &try_connection_reversal, plugin);
3649 GNUNET_assert (addrs[ret] != NULL);
3650 GNUNET_free (addrs[ret]);
3652 GNUNET_free_non_null (addrs);
3653 GNUNET_free_non_null (addrlens);
3658 GNUNET_NAT_register (env->cfg, GNUNET_YES, 0, 0, NULL, NULL, NULL,
3659 &try_connection_reversal, plugin);
3662 plugin->peers = GNUNET_CONTAINER_multihashmap_create (10);
3664 GNUNET_free (component_name);
3665 //GNUNET_SCHEDULER_add_now(address_notification, plugin);
3669 /* end of plugin_transport_http.c */