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 u_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 u6_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 */
519 * Function called for a quick conversion of the binary address to
520 * a numeric address. Note that the caller must not free the
521 * address and that the next call to this function is allowed
522 * to override the address again.
525 * @param addr binary address
526 * @param addrlen length of the address
527 * @return string representing the same address
530 http_plugin_address_to_string (void *cls,
536 * Call MHD to process pending ipv4 requests and then go back
537 * and schedule the next run.
539 static void http_server_daemon_v4_run (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
541 * Call MHD to process pending ipv6 requests and then go back
542 * and schedule the next run.
544 static void http_server_daemon_v6_run (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
547 * Function setting up curl handle and selecting message to send
549 * @param plugin plugin
551 * @return GNUNET_SYSERR on failure, GNUNET_NO if connecting, GNUNET_YES if ok
553 static int send_check_connections (struct Plugin *plugin, struct Session *ps);
556 * Function setting up file descriptors and scheduling task to run
558 * @param plugin plugin as closure
559 * @return GNUNET_SYSERR for hard failure, GNUNET_OK for ok
561 static int curl_schedule (struct Plugin *plugin);
564 * Task scheduled to reset the inbound quota delay for a specific peer
565 * @param cls plugin as closure
566 * @param tc task context
568 static void reset_inbound_quota_delay (void *cls,
569 const struct GNUNET_SCHEDULER_TaskContext *tc)
571 struct HTTP_PeerContext * pc = cls;
573 GNUNET_assert(cls != NULL);
574 pc->reset_task = GNUNET_SCHEDULER_NO_TASK;
575 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
577 pc->delay = GNUNET_TIME_relative_get_zero ();
582 * Creates a valid url from passed address and id
583 * @param plugin plugin
584 * @param addr address to create url from
585 * @param addrlen address lenth
586 * @param id session id
587 * @return the created url
590 create_url(struct Plugin *plugin,
591 const void * addr, size_t addrlen,
595 char *addr_str = (char *) http_plugin_address_to_string(NULL, addr, addrlen);
597 GNUNET_assert ((addr!=NULL) && (addrlen != 0));
598 GNUNET_asprintf(&url,
599 "%s://%s/%s;%u", PROTOCOL_PREFIX, addr_str,
600 (char *) (&plugin->my_ascii_hash_ident),id);
606 * Removes a message from the linked list of messages
609 * @return GNUNET_SYSERR if msg not found, GNUNET_OK on success
612 remove_http_message (struct Session * ps,
613 struct HTTP_Message * msg)
615 GNUNET_CONTAINER_DLL_remove(ps->pending_msgs_head,
616 ps->pending_msgs_tail,
623 * Iterator to remove peer context
624 * @param cls the plugin
625 * @param key the peers public key hashcode
626 * @param value the peer context
627 * @return GNUNET_YES on success
630 remove_peer_context_Iterator (void *cls,
631 const GNUNET_HashCode *key,
634 struct Plugin *plugin = cls;
635 struct HTTP_PeerContext * pc = value;
636 struct Session * ps = pc->head;
637 struct Session * tmp = NULL;
638 struct HTTP_Message * msg = NULL;
639 struct HTTP_Message * msg_tmp = NULL;
642 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
643 "Freeing context for peer `%s'\n",
644 GNUNET_i2s(&pc->identity));
646 GNUNET_CONTAINER_multihashmap_remove (plugin->peers, &pc->identity.hashPubKey, pc);
649 plugin->env->session_end(plugin, &pc->identity, ps);
652 GNUNET_free_non_null (ps->addr);
653 GNUNET_free(ps->url);
654 if (ps->msgtok != NULL)
655 GNUNET_SERVER_mst_destroy (ps->msgtok);
657 msg = ps->pending_msgs_head;
664 if (ps->direction==OUTBOUND)
666 if (ps->send_endpoint!=NULL)
667 curl_easy_cleanup(ps->send_endpoint);
668 if (ps->recv_endpoint!=NULL)
669 curl_easy_cleanup(ps->recv_endpoint);
675 GNUNET_STATISTICS_update (plugin->env->stats,
676 gettext_noop ("# HTTP peers active"),
684 * Removes a session from the linked list of sessions
685 * @param pc peer context
687 * @param call_msg_cont GNUNET_YES to call pending message continuations, otherwise no
688 * @param call_msg_cont_result result to call message continuations with
689 * @return GNUNET_SYSERR if msg not found, GNUNET_OK on success
692 remove_session (struct HTTP_PeerContext * pc,
695 int call_msg_cont_result)
697 struct HTTP_Message * msg;
698 struct Plugin * plugin = ps->peercontext->plugin;
700 #if DEBUG_CONNECTIONS
701 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
702 "Connection %X: removing %s session %X with id %u\n",
704 (ps->direction == INBOUND)
709 plugin->env->session_end(plugin, &pc->identity, ps);
710 GNUNET_free_non_null (ps->addr);
711 GNUNET_SERVER_mst_destroy (ps->msgtok);
712 GNUNET_free(ps->url);
713 if (ps->direction==INBOUND)
715 if (ps->recv_endpoint != NULL)
717 curl_easy_cleanup(ps->recv_endpoint);
718 ps->recv_endpoint = NULL;
720 if (ps->send_endpoint != NULL)
722 curl_easy_cleanup(ps->send_endpoint);
723 ps->send_endpoint = NULL;
727 msg = ps->pending_msgs_head;
730 if ((call_msg_cont == GNUNET_YES) && (msg->transmit_cont!=NULL))
732 msg->transmit_cont (msg->transmit_cont_cls,
734 call_msg_cont_result);
736 GNUNET_CONTAINER_DLL_remove(ps->pending_msgs_head,
737 ps->pending_msgs_head,
740 msg = ps->pending_msgs_head;
743 GNUNET_CONTAINER_DLL_remove(pc->head,pc->tail,ps);
747 /* no sessions left remove peer */
751 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
752 "No sessions left for peer `%s', removing context\n",
753 GNUNET_i2s(&pc->identity));
755 remove_peer_context_Iterator(plugin, &pc->identity.hashPubKey, pc);
763 static int check_localaddress (const struct sockaddr *addr, socklen_t addrlen)
766 int local = GNUNET_NO;
767 int af = addr->sa_family;
772 uint32_t netmask = 0x7F000000;
773 uint32_t address = ntohl (((struct sockaddr_in *) addr)->sin_addr.s_addr);
774 res = (address >> 24) ^ (netmask >> 24);
780 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
781 "Checking IPv4 address `%s': %s\n", GNUNET_a2s (addr, addrlen), (local==GNUNET_YES) ? "local" : "global");
787 if (IN6_IS_ADDR_LOOPBACK (&((struct sockaddr_in6 *) addr)->sin6_addr) ||
788 IN6_IS_ADDR_LINKLOCAL (&((struct sockaddr_in6 *) addr)->sin6_addr))
793 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
794 "Checking IPv6 address `%s' : %s\n", GNUNET_a2s (addr, addrlen), (local==GNUNET_YES) ? "local" : "global");
804 * Add the IP of our network interface to the list of
805 * our external IP addresses.
807 * @param cls the 'struct Plugin*'
808 * @param name name of the interface
809 * @param isDefault do we think this may be our default interface
810 * @param addr address of the interface
811 * @param addrlen number of bytes in addr
812 * @return GNUNET_OK to continue iterating
815 process_interfaces (void *cls,
818 const struct sockaddr *addr, socklen_t addrlen)
820 struct Plugin *plugin = cls;
821 struct IPv4HttpAddress * t4;
822 struct IPv6HttpAddress * t6;
825 if (plugin->use_localaddresses == GNUNET_NO)
827 if (GNUNET_YES == check_localaddress (addr, addrlen))
830 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
832 "Not notifying transport of address `%s' (local address)\n",
833 GNUNET_a2s (addr, addrlen));
840 GNUNET_assert(cls !=NULL);
841 af = addr->sa_family;
842 if ((af == AF_INET) &&
843 (plugin->use_ipv4 == GNUNET_YES) &&
844 (plugin->bind6_address == NULL) ) {
846 struct in_addr bnd_cmp = ((struct sockaddr_in *) addr)->sin_addr;
847 t4 = GNUNET_malloc(sizeof(struct IPv4HttpAddress));
848 // Not skipping loopback addresses
851 t4->ipv4_addr = ((struct sockaddr_in *) addr)->sin_addr.s_addr;
852 t4->u_port = htons (plugin->port_inbound);
853 if (plugin->bind4_address != NULL) {
854 if (0 == memcmp(&plugin->bind4_address->sin_addr, &bnd_cmp, sizeof (struct in_addr)))
856 GNUNET_CONTAINER_DLL_insert(plugin->ipv4_addr_head,
857 plugin->ipv4_addr_tail,t4);
858 plugin->env->notify_address(plugin->env->cls,
860 t4, sizeof (struct IPv4HttpAddress));
868 GNUNET_CONTAINER_DLL_insert (plugin->ipv4_addr_head,
869 plugin->ipv4_addr_tail,
871 plugin->env->notify_address(plugin->env->cls,
873 t4, sizeof (struct IPv4HttpAddress));
877 if ((af == AF_INET6) &&
878 (plugin->use_ipv6 == GNUNET_YES) &&
879 (plugin->bind4_address == NULL) ) {
881 struct in6_addr bnd_cmp6 = ((struct sockaddr_in6 *) addr)->sin6_addr;
883 t6 = GNUNET_malloc(sizeof(struct IPv6HttpAddress));
884 GNUNET_assert(t6 != NULL);
886 if (plugin->bind6_address != NULL) {
887 if (0 == memcmp(&plugin->bind6_address->sin6_addr,
889 sizeof (struct in6_addr))) {
890 memcpy (&t6->ipv6_addr,
891 &((struct sockaddr_in6 *) addr)->sin6_addr,
892 sizeof (struct in6_addr));
893 t6->u6_port = htons (plugin->port_inbound);
894 plugin->env->notify_address(plugin->env->cls,
896 t6, sizeof (struct IPv6HttpAddress));
897 GNUNET_CONTAINER_DLL_insert(plugin->ipv6_addr_head,
898 plugin->ipv6_addr_tail,
905 memcpy (&t6->ipv6_addr,
906 &((struct sockaddr_in6 *) addr)->sin6_addr,
907 sizeof (struct in6_addr));
908 t6->u6_port = htons (plugin->port_inbound);
909 GNUNET_CONTAINER_DLL_insert(plugin->ipv6_addr_head,plugin->ipv6_addr_tail,t6);
911 plugin->env->notify_address(plugin->env->cls,
913 t6, sizeof (struct IPv6HttpAddress));
920 * External logging function for MHD
921 * @param arg arguments
922 * @param fmt format string
923 * @param ap list of arguments
926 mhd_logger (void * arg,
932 vsnprintf(text, sizeof(text), fmt, ap);
934 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
941 mhd_termination_cb (void *cls,
942 struct MHD_Connection * connection,
943 void **httpSessionCache)
945 struct Session * ps = *httpSessionCache;
948 struct HTTP_PeerContext * pc = ps->peercontext;
949 struct Plugin *plugin = cls;
951 GNUNET_assert (cls != NULL);
952 plugin->current_connections--;
954 if (connection==ps->recv_endpoint)
956 #if DEBUG_CONNECTIONS
957 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
958 "Connection %X: inbound connection from peer `%s' was terminated\n",
960 GNUNET_i2s(&pc->identity));
962 ps->recv_active = GNUNET_NO;
963 ps->recv_connected = GNUNET_NO;
964 ps->recv_endpoint = NULL;
966 if (connection==ps->send_endpoint)
968 ps->send_active = GNUNET_NO;
969 ps->send_connected = GNUNET_NO;
970 ps->send_endpoint = NULL;
971 #if DEBUG_CONNECTIONS
972 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
973 "Connection %X: outbound connection from peer `%s' was terminated\n",
975 GNUNET_i2s(&pc->identity));
979 /* if both connections disconnected, remove session */
980 if ( (ps->send_connected == GNUNET_NO) &&
981 (ps->recv_connected == GNUNET_NO) )
983 GNUNET_STATISTICS_update (pc->plugin->env->stats,
984 gettext_noop ("# HTTP inbound sessions for peers active"),
987 remove_session(pc,ps,GNUNET_YES,GNUNET_SYSERR);
993 * Callback called by MessageStreamTokenizer when a message has arrived
994 * @param cls current session as closure
995 * @param client clien
996 * @param message the message to be forwarded to transport service
999 mhd_write_mst_cb (void *cls,
1001 const struct GNUNET_MessageHeader *message)
1003 struct Session *ps = cls;
1004 struct HTTP_PeerContext *pc = ps->peercontext;
1005 struct GNUNET_TIME_Relative delay;
1007 GNUNET_assert(ps != NULL);
1008 GNUNET_assert(pc != NULL);
1010 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1011 "Connection %X: Forwarding message to transport service, type %u and size %u from `%s' (`%s')\n",
1013 ntohs(message->type),
1014 ntohs(message->size),
1015 GNUNET_i2s(&(ps->peercontext)->identity),
1016 http_plugin_address_to_string(NULL,ps->addr,ps->addrlen));
1018 struct GNUNET_TRANSPORT_ATS_Information distance[2];
1019 distance[0].type = htonl (GNUNET_TRANSPORT_ATS_QUALITY_NET_DISTANCE);
1020 distance[0].value = htonl (1);
1021 distance[1].type = htonl (GNUNET_TRANSPORT_ATS_ARRAY_TERMINATOR);
1022 distance[1].value = htonl (0);
1023 delay = pc->plugin->env->receive (ps->peercontext->plugin->env->cls,
1026 (const struct GNUNET_TRANSPORT_ATS_Information *) &distance,
1032 if (pc->reset_task != GNUNET_SCHEDULER_NO_TASK)
1033 GNUNET_SCHEDULER_cancel (pc->reset_task);
1035 if (delay.rel_value > 0)
1038 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1039 "Connection %X: Inbound quota management: delay next read for %llu ms \n",
1043 pc->reset_task = GNUNET_SCHEDULER_add_delayed (delay, &reset_inbound_quota_delay, pc);
1049 * Check if incoming connection is accepted.
1050 * NOTE: Here every connection is accepted
1051 * @param cls plugin as closure
1052 * @param addr address of incoming connection
1053 * @param addr_len address length of incoming connection
1054 * @return MHD_YES if connection is accepted, MHD_NO if connection is rejected
1058 mhd_accept_cb (void *cls,
1059 const struct sockaddr *addr,
1062 struct Plugin *plugin = cls;
1063 GNUNET_assert (cls != NULL);
1065 if (plugin->max_connect_per_transport > plugin->current_connections)
1067 plugin->current_connections ++;
1075 * Callback called by MHD when it needs data to send
1076 * @param cls current session
1077 * @param pos position in buffer
1078 * @param buf the buffer to write data to
1079 * @param max max number of bytes available in buffer
1080 * @return bytes written to buffer
1083 mhd_send_callback (void *cls, uint64_t pos, char *buf, size_t max)
1085 struct Session * ps = cls;
1086 struct HTTP_PeerContext * pc;
1087 struct HTTP_Message * msg;
1090 GNUNET_assert (ps!=NULL);
1092 pc = ps->peercontext;
1093 msg = ps->pending_msgs_tail;
1094 if (ps->send_force_disconnect==GNUNET_YES)
1096 #if DEBUG_CONNECTIONS
1097 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1098 "Connection %X: outbound forced to disconnect\n",
1107 if ((msg->size-msg->pos) <= max)
1109 memcpy(buf,&msg->buf[msg->pos],(msg->size-msg->pos));
1110 bytes_read = msg->size-msg->pos;
1111 msg->pos+=(msg->size-msg->pos);
1115 memcpy(buf,&msg->buf[msg->pos],max);
1120 /* removing message */
1121 if (msg->pos==msg->size)
1123 if (NULL!=msg->transmit_cont)
1124 msg->transmit_cont (msg->transmit_cont_cls,&pc->identity,GNUNET_OK);
1125 ps->queue_length_cur -= msg->size;
1126 remove_http_message(ps,msg);
1129 #if DEBUG_CONNECTIONS
1130 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1131 "Connection %X: MHD has sent %u bytes\n",
1140 * Process GET or PUT request received via MHD. For
1141 * GET, queue response that will send back our pending
1142 * messages. For PUT, process incoming data and send
1143 * to GNUnet core. In either case, check if a session
1144 * already exists and create a new one if not.
1147 mhd_access_cb (void *cls,
1148 struct MHD_Connection *mhd_connection,
1151 const char *version,
1152 const char *upload_data,
1153 size_t * upload_data_size,
1154 void **httpSessionCache)
1156 struct Plugin *plugin = cls;
1157 struct MHD_Response *response;
1158 const union MHD_ConnectionInfo * conn_info;
1159 const struct sockaddr *client_addr;
1160 const struct sockaddr_in *addrin;
1161 const struct sockaddr_in6 *addrin6;
1162 char address[INET6_ADDRSTRLEN+14];
1163 struct GNUNET_PeerIdentity pi_in;
1165 struct IPv4HttpAddress ipv4addr;
1166 struct IPv6HttpAddress ipv6addr;
1167 struct HTTP_PeerContext *pc = NULL;
1168 struct Session *ps = NULL;
1169 struct Session *ps_tmp = NULL;
1170 int res = GNUNET_NO;
1172 size_t addr_len = 0 ;
1174 GNUNET_assert(cls !=NULL);
1176 if (NULL == *httpSessionCache)
1178 /* check url for peer identity , if invalid send HTTP 404*/
1179 size_t len = strlen(&url[1]);
1180 char * peer = GNUNET_malloc(104+1);
1182 if ( (len>104) && (url[104]==';'))
1184 char * id = GNUNET_malloc((len-104)+1);
1185 strcpy(id,&url[105]);
1186 memcpy(peer,&url[1],103);
1188 id_num = strtoul ( id, NULL , 10);
1191 res = GNUNET_CRYPTO_hash_from_string (peer, &(pi_in.hashPubKey));
1193 if ( GNUNET_SYSERR == res )
1195 response = MHD_create_response_from_data (strlen (HTTP_ERROR_RESPONSE),
1196 HTTP_ERROR_RESPONSE,
1198 res = MHD_queue_response (mhd_connection, MHD_HTTP_NOT_FOUND, response);
1199 MHD_destroy_response (response);
1200 #if DEBUG_CONNECTIONS
1202 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1203 "Peer has no valid ident, sent HTTP 1.1/404\n");
1205 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1206 "Peer has no valid ident, could not send error\n");
1213 ps = *httpSessionCache;
1214 pc = ps->peercontext;
1217 if (NULL == *httpSessionCache)
1219 /* get peer context */
1220 pc = GNUNET_CONTAINER_multihashmap_get (plugin->peers, &pi_in.hashPubKey);
1224 pc = GNUNET_malloc(sizeof (struct HTTP_PeerContext));
1225 pc->plugin = plugin;
1226 pc->session_id_counter=1;
1227 pc->last_session = NULL;
1228 memcpy(&pc->identity, &pi_in, sizeof(struct GNUNET_PeerIdentity));
1229 GNUNET_CONTAINER_multihashmap_put(plugin->peers,
1230 &pc->identity.hashPubKey,
1232 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
1233 GNUNET_STATISTICS_update (plugin->env->stats,
1234 gettext_noop ("# HTTP peers active"),
1239 conn_info = MHD_get_connection_info(mhd_connection, MHD_CONNECTION_INFO_CLIENT_ADDRESS );
1240 /* Incoming IPv4 connection */
1241 /* cast required for legacy MHD API < 0.9.6 */
1242 client_addr = (const struct sockaddr *) conn_info->client_addr;
1243 if ( AF_INET == client_addr->sa_family)
1245 addrin = (const struct sockaddr_in*) client_addr;
1246 inet_ntop(addrin->sin_family, &(addrin->sin_addr),address,INET_ADDRSTRLEN);
1247 memcpy(&ipv4addr.ipv4_addr,&(addrin->sin_addr),sizeof(struct in_addr));
1248 ipv4addr.u_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,INET6_ADDRSTRLEN);
1257 memcpy(&ipv6addr.ipv6_addr,&(addrin6->sin6_addr),sizeof(struct in6_addr));
1258 ipv6addr.u6_port = addrin6->sin6_port;
1260 addr_len = sizeof(struct IPv6HttpAddress);
1263 GNUNET_assert (addr != NULL);
1264 GNUNET_assert (addr_len != 0);
1267 /* only inbound sessions here */
1270 while (ps_tmp!=NULL)
1272 if ((ps_tmp->direction==INBOUND) && (ps_tmp->session_id == id_num) && (id_num!=0))
1274 if ((ps_tmp->recv_force_disconnect!=GNUNET_YES) && (ps_tmp->send_force_disconnect!=GNUNET_YES))
1278 ps_tmp=ps_tmp->next;
1283 ps = GNUNET_malloc(sizeof (struct Session));
1284 ps->addr = GNUNET_malloc(addr_len);
1285 memcpy(ps->addr,addr,addr_len);
1286 ps->addrlen = addr_len;
1287 ps->direction=INBOUND;
1288 ps->pending_msgs_head = NULL;
1289 ps->pending_msgs_tail = NULL;
1290 ps->send_connected=GNUNET_NO;
1291 ps->send_active=GNUNET_NO;
1292 ps->recv_connected=GNUNET_NO;
1293 ps->recv_active=GNUNET_NO;
1295 ps->session_id =id_num;
1296 ps->queue_length_cur = 0;
1297 ps->queue_length_max = GNUNET_SERVER_MAX_MESSAGE_SIZE;
1298 ps->url = create_url (plugin, ps->addr, ps->addrlen, ps->session_id);
1299 GNUNET_CONTAINER_DLL_insert(pc->head,pc->tail,ps);
1300 GNUNET_STATISTICS_update (plugin->env->stats,
1301 gettext_noop ("# HTTP inbound sessions for peers active"),
1306 *httpSessionCache = ps;
1307 if (ps->msgtok==NULL)
1308 ps->msgtok = GNUNET_SERVER_mst_create (&mhd_write_mst_cb, ps);
1310 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1311 "Connection %X: HTTP Daemon has new an incoming `%s' request from peer `%s' (`%s')\n",
1314 GNUNET_i2s(&pc->identity),
1315 http_plugin_address_to_string(NULL, ps->addr, ps->addrlen));
1319 /* Is it a PUT or a GET request */
1320 if (0 == strcmp (MHD_HTTP_METHOD_PUT, method))
1322 if (ps->recv_force_disconnect == GNUNET_YES)
1324 #if DEBUG_CONNECTIONS
1325 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1326 "Connection %X: inbound connection was forced to disconnect\n",ps);
1328 ps->recv_active = GNUNET_NO;
1331 if ((*upload_data_size == 0) && (ps->recv_active==GNUNET_NO))
1333 ps->recv_endpoint = mhd_connection;
1334 ps->recv_connected = GNUNET_YES;
1335 ps->recv_active = GNUNET_YES;
1336 ps->recv_force_disconnect = GNUNET_NO;
1337 #if DEBUG_CONNECTIONS
1338 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1339 "Connection %X: inbound PUT connection connected\n",ps);
1344 /* Transmission of all data complete */
1345 if ((*upload_data_size == 0) && (ps->recv_active == GNUNET_YES))
1347 response = MHD_create_response_from_data (strlen (HTTP_PUT_RESPONSE),
1350 res = MHD_queue_response (mhd_connection, MHD_HTTP_OK, response);
1351 #if DEBUG_CONNECTIONS
1352 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1353 "Connection %X: Sent HTTP/1.1: 200 OK as PUT Response\n",
1356 MHD_destroy_response (response);
1357 ps->recv_active=GNUNET_NO;
1361 /* Recieving data */
1362 if ((*upload_data_size > 0) && (ps->recv_active == GNUNET_YES))
1364 if (pc->delay.rel_value == 0)
1367 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1368 "Connection %X: PUT with %u bytes forwarded to MST\n",
1369 ps, *upload_data_size);
1371 res = GNUNET_SERVER_mst_receive(ps->msgtok, ps,
1372 upload_data, *upload_data_size,
1373 GNUNET_NO, GNUNET_NO);
1374 (*upload_data_size) = 0;
1379 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1380 "Connection %X: no inbound bandwidth available! Next read was delayed for %llu ms\n",
1382 ps->peercontext->delay.rel_value);
1390 if ( 0 == strcmp (MHD_HTTP_METHOD_GET, method) )
1392 if (ps->send_force_disconnect == GNUNET_YES)
1394 #if DEBUG_CONNECTIONS
1395 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1396 "Connection %X: outbound connection was forced to disconnect\n",
1399 ps->send_active = GNUNET_NO;
1402 ps->send_connected = GNUNET_YES;
1403 ps->send_active = GNUNET_YES;
1404 ps->send_endpoint = mhd_connection;
1405 ps->send_force_disconnect = GNUNET_NO;
1406 #if DEBUG_CONNECTIONS
1407 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1408 "Connection %X: inbound GET connection connected\n",
1411 response = MHD_create_response_from_callback(-1,32 * 1024, &mhd_send_callback, ps, NULL);
1412 res = MHD_queue_response (mhd_connection, MHD_HTTP_OK, response);
1413 MHD_destroy_response (response);
1421 * Function that queries MHD's select sets and
1422 * starts the task waiting for them.
1423 * @param plugin plugin
1424 * @param daemon_handle the MHD daemon handle
1425 * @return gnunet task identifier
1427 static GNUNET_SCHEDULER_TaskIdentifier
1428 http_server_daemon_prepare (struct Plugin *plugin,
1429 struct MHD_Daemon *daemon_handle)
1431 GNUNET_SCHEDULER_TaskIdentifier ret;
1435 struct GNUNET_NETWORK_FDSet *wrs;
1436 struct GNUNET_NETWORK_FDSet *wws;
1437 struct GNUNET_NETWORK_FDSet *wes;
1439 unsigned long long timeout;
1441 struct GNUNET_TIME_Relative tv;
1443 ret = GNUNET_SCHEDULER_NO_TASK;
1447 wrs = GNUNET_NETWORK_fdset_create ();
1448 wes = GNUNET_NETWORK_fdset_create ();
1449 wws = GNUNET_NETWORK_fdset_create ();
1451 GNUNET_assert (MHD_YES ==
1452 MHD_get_fdset (daemon_handle,
1457 haveto = MHD_get_timeout (daemon_handle, &timeout);
1458 if (haveto == MHD_YES)
1459 tv.rel_value = (uint64_t) timeout;
1461 tv = GNUNET_TIME_UNIT_SECONDS;
1462 GNUNET_NETWORK_fdset_copy_native (wrs, &rs, max + 1);
1463 GNUNET_NETWORK_fdset_copy_native (wws, &ws, max + 1);
1464 GNUNET_NETWORK_fdset_copy_native (wes, &es, max + 1);
1465 if (daemon_handle == plugin->http_server_daemon_v4)
1467 if (plugin->http_server_task_v4 != GNUNET_SCHEDULER_NO_TASK)
1469 GNUNET_SCHEDULER_cancel(plugin->http_server_task_v4);
1470 plugin->http_server_daemon_v4 = GNUNET_SCHEDULER_NO_TASK;
1473 ret = GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1474 GNUNET_SCHEDULER_NO_TASK,
1478 &http_server_daemon_v4_run,
1481 if (daemon_handle == plugin->http_server_daemon_v6)
1483 if (plugin->http_server_task_v6 != GNUNET_SCHEDULER_NO_TASK)
1485 GNUNET_SCHEDULER_cancel(plugin->http_server_task_v6);
1486 plugin->http_server_task_v6 = GNUNET_SCHEDULER_NO_TASK;
1489 ret = GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1490 GNUNET_SCHEDULER_NO_TASK,
1494 &http_server_daemon_v6_run,
1497 GNUNET_NETWORK_fdset_destroy (wrs);
1498 GNUNET_NETWORK_fdset_destroy (wws);
1499 GNUNET_NETWORK_fdset_destroy (wes);
1505 * Call MHD IPv4 to process pending requests and then go back
1506 * and schedule the next run.
1507 * @param cls plugin as closure
1508 * @param tc task context
1511 http_server_daemon_v4_run (void *cls,
1512 const struct GNUNET_SCHEDULER_TaskContext *tc)
1514 struct Plugin *plugin = cls;
1516 #if DEBUG_SCHEDULING
1517 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_READ_READY))
1518 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1519 "http_server_daemon_v4_run: GNUNET_SCHEDULER_REASON_READ_READY\n");
1520 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_WRITE_READY))
1521 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1522 "http_server_daemon_v4_run: GNUNET_SCHEDULER_REASON_WRITE_READY\n");
1523 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_TIMEOUT))
1524 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1525 "http_server_daemon_v4_run: GNUNET_SCHEDULER_REASON_TIMEOUT\n");
1526 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_STARTUP))
1527 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1528 "http_server_daemon_v4_run: GGNUNET_SCHEDULER_REASON_STARTUP\n");
1529 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1530 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1531 "http_server_daemon_v4_run: GGNUNET_SCHEDULER_REASON_SHUTDOWN\n");
1534 GNUNET_assert(cls !=NULL);
1535 plugin->http_server_task_v4 = GNUNET_SCHEDULER_NO_TASK;
1537 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1540 GNUNET_assert (MHD_YES == MHD_run (plugin->http_server_daemon_v4));
1541 plugin->http_server_task_v4 = http_server_daemon_prepare (plugin, plugin->http_server_daemon_v4);
1546 * Call MHD IPv6 to process pending requests and then go back
1547 * and schedule the next run.
1548 * @param cls plugin as closure
1549 * @param tc task context
1552 http_server_daemon_v6_run (void *cls,
1553 const struct GNUNET_SCHEDULER_TaskContext *tc)
1555 struct Plugin *plugin = cls;
1557 #if DEBUG_SCHEDULING
1558 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_READ_READY))
1559 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1560 "http_server_daemon_v6_run: GNUNET_SCHEDULER_REASON_READ_READY\n");
1561 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_WRITE_READY))
1562 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1563 "http_server_daemon_v6_run: GNUNET_SCHEDULER_REASON_WRITE_READY\n");
1564 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_TIMEOUT))
1565 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1566 "http_server_daemon_v6_run: GNUNET_SCHEDULER_REASON_TIMEOUT\n");
1567 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_STARTUP))
1568 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1569 "http_server_daemon_v6_run: GGNUNET_SCHEDULER_REASON_STARTUP\n");
1570 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1571 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1572 "http_server_daemon_v6_run: GGNUNET_SCHEDULER_REASON_SHUTDOWN\n");
1575 GNUNET_assert(cls !=NULL);
1576 plugin->http_server_task_v6 = GNUNET_SCHEDULER_NO_TASK;
1578 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1581 GNUNET_assert (MHD_YES == MHD_run (plugin->http_server_daemon_v6));
1582 plugin->http_server_task_v6 = http_server_daemon_prepare (plugin, plugin->http_server_daemon_v6);
1587 curl_get_header_cb( void *ptr,
1588 size_t size, size_t nmemb,
1591 struct Session * ps = stream;
1593 long http_result = 0;
1595 /* Getting last http result code */
1596 GNUNET_assert(NULL!=ps);
1597 if (ps->recv_connected==GNUNET_NO)
1599 res = curl_easy_getinfo(ps->recv_endpoint, CURLINFO_RESPONSE_CODE, &http_result);
1600 if (CURLE_OK == res)
1602 if (http_result == 200)
1604 ps->recv_connected = GNUNET_YES;
1605 ps->recv_active = GNUNET_YES;
1606 #if DEBUG_CONNECTIONS
1607 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: connected to recieve data\n",ps);
1609 // Calling send_check_connections again since receive is established
1610 send_check_connections (ps->peercontext->plugin, ps);
1617 size_t len = size * nmemb;
1619 if ((size * nmemb) < SIZE_MAX)
1620 tmp = GNUNET_malloc (len+1);
1622 if ((tmp != NULL) && (len > 0))
1624 memcpy(tmp,ptr,len);
1627 if (tmp[len-2] == 13)
1630 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1631 "Connection %X: Header: %s\n",
1634 GNUNET_free_non_null (tmp);
1637 return size * nmemb;
1642 * Callback called by libcurl when new headers arrive
1643 * Used to get HTTP result for curl operations
1644 * @param ptr stream to read from
1645 * @param size size of one char element
1646 * @param nmemb number of char elements
1647 * @param stream closure set by user
1648 * @return bytes read by function
1651 curl_put_header_cb(void *ptr,
1656 struct Session * ps = stream;
1659 size_t len = size * nmemb;
1660 long http_result = 0;
1663 /* Getting last http result code */
1664 GNUNET_assert(NULL!=ps);
1665 res = curl_easy_getinfo (ps->send_endpoint, CURLINFO_RESPONSE_CODE, &http_result);
1666 if (CURLE_OK == res)
1668 if ((http_result == 100) && (ps->send_connected==GNUNET_NO))
1670 ps->send_connected = GNUNET_YES;
1671 ps->send_active = GNUNET_YES;
1672 #if DEBUG_CONNECTIONS
1673 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1674 "Connection %X: connected to send data\n",
1678 if ((http_result == 200) && (ps->send_connected==GNUNET_YES))
1680 ps->send_connected = GNUNET_NO;
1681 ps->send_active = GNUNET_NO;
1682 #if DEBUG_CONNECTIONS
1683 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1684 "Connection %X: sending disconnected\n",
1691 if ((size * nmemb) < SIZE_MAX)
1692 tmp = GNUNET_malloc (len+1);
1694 if ((tmp != NULL) && (len > 0))
1696 memcpy(tmp,ptr,len);
1699 if (tmp[len-2] == 13)
1703 GNUNET_free_non_null (tmp);
1704 return size * nmemb;
1708 * Callback method used with libcurl
1709 * Method is called when libcurl needs to read data during sending
1710 * @param stream pointer where to write data
1711 * @param size size of an individual element
1712 * @param nmemb count of elements that can be written to the buffer
1713 * @param ptr source pointer, passed to the libcurl handle
1714 * @return bytes written to stream
1717 curl_send_cb(void *stream,
1718 size_t size, size_t nmemb,
1721 struct Session * ps = ptr;
1722 struct HTTP_Message * msg = ps->pending_msgs_tail;
1726 if (ps->send_active == GNUNET_NO)
1727 return CURL_READFUNC_PAUSE;
1728 if ( (ps->pending_msgs_tail == NULL) &&
1729 (ps->send_active == GNUNET_YES) )
1731 #if DEBUG_CONNECTIONS
1732 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1733 "Connection %X: No Message to send, pausing connection\n",
1736 ps->send_active = GNUNET_NO;
1737 return CURL_READFUNC_PAUSE;
1740 GNUNET_assert (msg!=NULL);
1743 if (msg->pos < msg->size)
1745 /* data fit in buffer */
1746 if ((msg->size - msg->pos) <= (size * nmemb))
1748 len = (msg->size - msg->pos);
1749 memcpy(stream, &msg->buf[msg->pos], len);
1756 memcpy(stream, &msg->buf[msg->pos], len);
1761 /* no data to send */
1767 if ( msg->pos == msg->size)
1769 #if DEBUG_CONNECTIONS
1770 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1771 "Connection %X: Message with %u bytes sent, removing message from queue\n",
1775 /* Calling transmit continuation */
1776 if (NULL != ps->pending_msgs_tail->transmit_cont)
1777 msg->transmit_cont (ps->pending_msgs_tail->transmit_cont_cls,
1778 &(ps->peercontext)->identity,
1780 ps->queue_length_cur -= msg->size;
1781 remove_http_message(ps, msg);
1788 curl_receive_mst_cb (void *cls,
1790 const struct GNUNET_MessageHeader *message)
1792 struct Session *ps = cls;
1793 struct GNUNET_TIME_Relative delay;
1794 GNUNET_assert(ps != NULL);
1796 struct HTTP_PeerContext *pc = ps->peercontext;
1797 GNUNET_assert(pc != NULL);
1799 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1800 "Connection %X: Forwarding message to transport service, type %u and size %u from `%s' (`%s')\n",
1802 ntohs(message->type),
1803 ntohs(message->size),
1804 GNUNET_i2s(&(pc->identity)),
1805 http_plugin_address_to_string(NULL,ps->addr,ps->addrlen));
1807 struct GNUNET_TRANSPORT_ATS_Information distance[2];
1808 distance[0].type = htonl (GNUNET_TRANSPORT_ATS_QUALITY_NET_DISTANCE);
1809 distance[0].value = htonl (1);
1810 distance[1].type = htonl (GNUNET_TRANSPORT_ATS_ARRAY_TERMINATOR);
1811 distance[1].value = htonl (0);
1813 delay = pc->plugin->env->receive (pc->plugin->env->cls,
1816 (const struct GNUNET_TRANSPORT_ATS_Information *) &distance, 2,
1821 if (pc->reset_task != GNUNET_SCHEDULER_NO_TASK)
1822 GNUNET_SCHEDULER_cancel (pc->reset_task);
1824 if (delay.rel_value > 0)
1827 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1828 "Connection %X: Inbound quota management: delay next read for %llu ms\n",
1829 ps, delay.rel_value);
1831 pc->reset_task = GNUNET_SCHEDULER_add_delayed (delay, &reset_inbound_quota_delay, pc);
1837 * Callback method used with libcurl
1838 * Method is called when libcurl needs to write data during sending
1839 * @param stream pointer where to write data
1840 * @param size size of an individual element
1841 * @param nmemb count of elements that can be written to the buffer
1842 * @param ptr destination pointer, passed to the libcurl handle
1843 * @return bytes read from stream
1846 curl_receive_cb( void *stream, size_t size, size_t nmemb, void *ptr)
1848 struct Session * ps = ptr;
1850 if (ps->peercontext->delay.rel_value > 0)
1853 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1854 "Connection %X: no inbound bandwidth available! Next read was delayed for %llu ms\n",
1855 ps, ps->peercontext->delay.rel_value);
1859 #if DEBUG_CONNECTIONS
1860 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1861 "Connection %X: %u bytes received\n",
1864 GNUNET_SERVER_mst_receive(ps->msgtok, ps,
1866 GNUNET_NO, GNUNET_NO);
1867 return (size * nmemb);
1872 curl_handle_finished (struct Plugin *plugin)
1874 struct Session *ps = NULL;
1875 struct HTTP_PeerContext *pc = NULL;
1876 struct CURLMsg *msg;
1877 struct HTTP_Message * cur_msg = NULL;
1884 msg = curl_multi_info_read (plugin->multi_handle, &msgs_in_queue);
1885 if ((msgs_in_queue == 0) || (msg == NULL))
1887 /* get session for affected curl handle */
1888 GNUNET_assert ( msg->easy_handle != NULL );
1889 curl_easy_getinfo(msg->easy_handle, CURLINFO_PRIVATE, &tmp);
1890 ps = (struct Session *) tmp;
1891 GNUNET_assert ( ps != NULL );
1892 pc = ps->peercontext;
1893 GNUNET_assert ( pc != NULL );
1897 if ( (msg->data.result != CURLE_OK) &&
1898 (msg->data.result != CURLE_GOT_NOTHING) )
1900 /* sending msg failed*/
1901 if (msg->easy_handle == ps->send_endpoint)
1903 #if DEBUG_CONNECTIONS
1904 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1905 _("Connection %X: HTTP PUT to peer `%s' (`%s') failed: `%s' `%s'\n"),
1907 GNUNET_i2s(&pc->identity),
1908 http_plugin_address_to_string(NULL, ps->addr, ps->addrlen),
1909 "curl_multi_perform",
1910 curl_easy_strerror (msg->data.result));
1912 ps->send_connected = GNUNET_NO;
1913 ps->send_active = GNUNET_NO;
1914 curl_multi_remove_handle(plugin->multi_handle,ps->send_endpoint);
1915 while (ps->pending_msgs_tail != NULL)
1917 cur_msg = ps->pending_msgs_tail;
1918 if ( NULL != cur_msg->transmit_cont)
1919 cur_msg->transmit_cont (cur_msg->transmit_cont_cls,&pc->identity,GNUNET_SYSERR);
1920 ps->queue_length_cur -= cur_msg->size;
1921 remove_http_message(ps,cur_msg);
1924 /* GET connection failed */
1925 if (msg->easy_handle == ps->recv_endpoint)
1927 #if DEBUG_CONNECTIONS
1928 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1929 _("Connection %X: HTTP GET to peer `%s' (`%s') failed: `%s' `%s'\n"),
1931 GNUNET_i2s(&pc->identity),
1932 http_plugin_address_to_string(NULL, ps->addr, ps->addrlen),
1933 "curl_multi_perform",
1934 curl_easy_strerror (msg->data.result));
1936 ps->recv_connected = GNUNET_NO;
1937 ps->recv_active = GNUNET_NO;
1938 curl_multi_remove_handle(plugin->multi_handle,ps->recv_endpoint);
1943 if (msg->easy_handle == ps->send_endpoint)
1945 GNUNET_assert (CURLE_OK == curl_easy_getinfo(msg->easy_handle, CURLINFO_RESPONSE_CODE, &http_result));
1946 #if DEBUG_CONNECTIONS
1947 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1948 "Connection %X: HTTP PUT connection to peer `%s' (`%s') was closed with HTTP code %u\n",
1950 GNUNET_i2s(&pc->identity),
1951 http_plugin_address_to_string(NULL, ps->addr, ps->addrlen),
1954 /* Calling transmit continuation */
1955 while (ps->pending_msgs_tail != NULL)
1957 cur_msg = ps->pending_msgs_tail;
1958 if ( NULL != cur_msg->transmit_cont)
1960 /* HTTP 1xx : Last message before here was informational */
1961 if ((http_result >=100) && (http_result < 200))
1962 cur_msg->transmit_cont (cur_msg->transmit_cont_cls,&pc->identity,GNUNET_OK);
1963 /* HTTP 2xx: successful operations */
1964 if ((http_result >=200) && (http_result < 300))
1965 cur_msg->transmit_cont (cur_msg->transmit_cont_cls,&pc->identity,GNUNET_OK);
1966 /* HTTP 3xx..5xx: error */
1967 if ((http_result >=300) && (http_result < 600))
1968 cur_msg->transmit_cont (cur_msg->transmit_cont_cls,&pc->identity,GNUNET_SYSERR);
1970 ps->queue_length_cur -= cur_msg->size;
1971 remove_http_message(ps,cur_msg);
1974 ps->send_connected = GNUNET_NO;
1975 ps->send_active = GNUNET_NO;
1976 curl_multi_remove_handle(plugin->multi_handle,ps->send_endpoint);
1978 if (msg->easy_handle == ps->recv_endpoint)
1980 #if DEBUG_CONNECTIONS
1981 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1982 "Connection %X: HTTP GET connection to peer `%s' (`%s') was closed with HTTP code %u\n",
1984 GNUNET_i2s(&pc->identity),
1985 http_plugin_address_to_string(NULL, ps->addr, ps->addrlen),
1988 ps->recv_connected = GNUNET_NO;
1989 ps->recv_active = GNUNET_NO;
1990 curl_multi_remove_handle(plugin->multi_handle,ps->recv_endpoint);
1992 plugin->current_connections--;
1994 if ((ps->recv_connected == GNUNET_NO) && (ps->send_connected == GNUNET_NO))
1995 remove_session (pc, ps, GNUNET_YES, GNUNET_SYSERR);
2001 while ( (msgs_in_queue > 0) );
2006 * Task performing curl operations
2007 * @param cls plugin as closure
2008 * @param tc gnunet scheduler task context
2010 static void curl_perform (void *cls,
2011 const struct GNUNET_SCHEDULER_TaskContext *tc)
2013 struct Plugin *plugin = cls;
2014 static unsigned int handles_last_run;
2018 GNUNET_assert(cls !=NULL);
2020 plugin->http_curl_task = GNUNET_SCHEDULER_NO_TASK;
2021 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
2026 mret = curl_multi_perform (plugin->multi_handle, &running);
2027 if ((running < handles_last_run) && (running>0))
2028 curl_handle_finished(plugin);
2029 handles_last_run = running;
2031 while (mret == CURLM_CALL_MULTI_PERFORM);
2032 curl_schedule(plugin);
2037 * Function setting up file descriptors and scheduling task to run
2039 * @param plugin plugin as closure
2040 * @return GNUNET_SYSERR for hard failure, GNUNET_OK for ok
2043 curl_schedule(struct Plugin *plugin)
2049 struct GNUNET_NETWORK_FDSet *grs;
2050 struct GNUNET_NETWORK_FDSet *gws;
2054 /* Cancel previous scheduled task */
2055 if (plugin->http_curl_task != GNUNET_SCHEDULER_NO_TASK)
2057 GNUNET_SCHEDULER_cancel(plugin->http_curl_task);
2058 plugin->http_curl_task = GNUNET_SCHEDULER_NO_TASK;
2065 mret = curl_multi_fdset (plugin->multi_handle, &rs, &ws, &es, &max);
2066 if (mret != CURLM_OK)
2068 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2069 _("%s failed at %s:%d: `%s'\n"),
2070 "curl_multi_fdset", __FILE__, __LINE__,
2071 curl_multi_strerror (mret));
2072 return GNUNET_SYSERR;
2074 mret = curl_multi_timeout (plugin->multi_handle, &to);
2075 if (mret != CURLM_OK)
2077 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2078 _("%s failed at %s:%d: `%s'\n"),
2079 "curl_multi_timeout", __FILE__, __LINE__,
2080 curl_multi_strerror (mret));
2081 return GNUNET_SYSERR;
2084 grs = GNUNET_NETWORK_fdset_create ();
2085 gws = GNUNET_NETWORK_fdset_create ();
2086 GNUNET_NETWORK_fdset_copy_native (grs, &rs, max + 1);
2087 GNUNET_NETWORK_fdset_copy_native (gws, &ws, max + 1);
2088 plugin->http_curl_task = GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
2089 GNUNET_SCHEDULER_NO_TASK,
2091 ? GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
2092 : GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, to),
2097 GNUNET_NETWORK_fdset_destroy (gws);
2098 GNUNET_NETWORK_fdset_destroy (grs);
2105 * Function to log curl debug messages with GNUNET_log
2106 * @param curl handle
2107 * @param type curl_infotype
2110 * @param cls closure
2114 curl_logger (CURL * curl,
2116 char * data, size_t size,
2119 if (type == CURLINFO_TEXT)
2122 memcpy(text,data,size);
2123 if (text[size-1] == '\n')
2128 text[size+1] = '\0';
2130 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2131 "CURL: Connection %X - %s",
2141 * Function setting up curl handle and selecting message to send
2143 * @param plugin plugin
2145 * @return GNUNET_SYSERR on failure, GNUNET_NO if connecting, GNUNET_YES if ok
2148 send_check_connections (struct Plugin *plugin,
2152 struct GNUNET_TIME_Relative timeout = GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT;
2154 if ((ps->direction == OUTBOUND) && (plugin->current_connections < plugin->max_connect_per_transport))
2156 /* RECV DIRECTION */
2157 /* Check if session is connected to receive data, otherwise connect to peer */
2159 if (ps->recv_connected == GNUNET_NO)
2161 int fresh = GNUNET_NO;
2162 if (ps->recv_endpoint == NULL)
2165 ps->recv_endpoint = curl_easy_init();
2168 curl_easy_setopt(ps->recv_endpoint, CURLOPT_VERBOSE, 1L);
2169 curl_easy_setopt(ps->recv_endpoint, CURLOPT_DEBUGFUNCTION , &curl_logger);
2170 curl_easy_setopt(ps->recv_endpoint, CURLOPT_DEBUGDATA , ps->recv_endpoint);
2173 curl_easy_setopt(ps->recv_endpoint, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1);
2174 curl_easy_setopt(ps->recv_endpoint, CURLOPT_SSL_VERIFYPEER, 0);
2175 curl_easy_setopt(ps->recv_endpoint, CURLOPT_SSL_VERIFYHOST, 0);
2177 curl_easy_setopt(ps->recv_endpoint, CURLOPT_URL, ps->url);
2178 curl_easy_setopt(ps->recv_endpoint, CURLOPT_HEADERFUNCTION, &curl_get_header_cb);
2179 curl_easy_setopt(ps->recv_endpoint, CURLOPT_WRITEHEADER, ps);
2180 curl_easy_setopt(ps->recv_endpoint, CURLOPT_READFUNCTION, curl_send_cb);
2181 curl_easy_setopt(ps->recv_endpoint, CURLOPT_READDATA, ps);
2182 curl_easy_setopt(ps->recv_endpoint, CURLOPT_WRITEFUNCTION, curl_receive_cb);
2183 curl_easy_setopt(ps->recv_endpoint, CURLOPT_WRITEDATA, ps);
2184 curl_easy_setopt(ps->recv_endpoint, CURLOPT_TIMEOUT, (long) timeout.rel_value);
2185 curl_easy_setopt(ps->recv_endpoint, CURLOPT_PRIVATE, ps);
2186 curl_easy_setopt(ps->recv_endpoint, CURLOPT_CONNECTTIMEOUT, HTTP_CONNECT_TIMEOUT);
2187 curl_easy_setopt(ps->recv_endpoint, CURLOPT_BUFFERSIZE, 2*GNUNET_SERVER_MAX_MESSAGE_SIZE);
2188 #if CURL_TCP_NODELAY
2189 curl_easy_setopt(ps->recv_endpoint, CURLOPT_TCP_NODELAY, 1);
2191 if (fresh==GNUNET_YES)
2193 mret = curl_multi_add_handle(plugin->multi_handle, ps->recv_endpoint);
2194 if (mret != CURLM_OK)
2196 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2197 _("Connection: %X: %s failed at %s:%d: `%s'\n"),
2199 "curl_multi_add_handle", __FILE__, __LINE__,
2200 curl_multi_strerror (mret));
2201 return GNUNET_SYSERR;
2204 if (plugin->http_curl_task != GNUNET_SCHEDULER_NO_TASK)
2206 GNUNET_SCHEDULER_cancel(plugin->http_curl_task);
2207 plugin->http_curl_task = GNUNET_SCHEDULER_NO_TASK;
2209 plugin->current_connections ++;
2210 plugin->http_curl_task = GNUNET_SCHEDULER_add_now (&curl_perform, plugin);
2213 /* waiting for receive direction */
2214 if (ps->recv_connected==GNUNET_NO)
2217 /* SEND DIRECTION */
2218 /* Check if session is connected to send data, otherwise connect to peer */
2219 if ((ps->send_connected == GNUNET_YES) && (ps->send_endpoint!= NULL))
2221 if (ps->send_active == GNUNET_YES)
2223 #if DEBUG_CONNECTIONS
2224 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2225 "Connection %X: outbound active, enqueueing message\n",
2230 if (ps->send_active == GNUNET_NO)
2232 #if DEBUG_CONNECTIONS
2233 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2234 "Connection %X: outbound paused, unpausing existing connection and enqueueing message\n",
2237 if (CURLE_OK == curl_easy_pause(ps->send_endpoint,CURLPAUSE_CONT))
2239 ps->send_active=GNUNET_YES;
2240 if (plugin->http_curl_task != GNUNET_SCHEDULER_NO_TASK)
2242 GNUNET_SCHEDULER_cancel(plugin->http_curl_task);
2243 plugin->http_curl_task = GNUNET_SCHEDULER_NO_TASK;
2245 plugin->http_curl_task = GNUNET_SCHEDULER_add_now (&curl_perform, plugin);
2249 return GNUNET_SYSERR;
2252 /* not connected, initiate connection */
2253 if ((ps->send_connected==GNUNET_NO) && (plugin->current_connections < plugin->max_connect_per_transport))
2255 int fresh = GNUNET_NO;
2256 if (NULL == ps->send_endpoint)
2258 ps->send_endpoint = curl_easy_init();
2261 GNUNET_assert (ps->send_endpoint != NULL);
2262 GNUNET_assert (NULL != ps->pending_msgs_tail);
2263 #if DEBUG_CONNECTIONS
2264 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2265 "Connection %X: outbound not connected, initiating connection\n",
2268 ps->send_active = GNUNET_NO;
2271 curl_easy_setopt(ps->send_endpoint, CURLOPT_VERBOSE, 1L);
2272 curl_easy_setopt(ps->send_endpoint, CURLOPT_DEBUGFUNCTION , &curl_logger);
2273 curl_easy_setopt(ps->send_endpoint, CURLOPT_DEBUGDATA , ps->send_endpoint);
2276 curl_easy_setopt (ps->send_endpoint, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1);
2277 curl_easy_setopt(ps->send_endpoint, CURLOPT_SSL_VERIFYPEER, 0);
2278 curl_easy_setopt(ps->send_endpoint, CURLOPT_SSL_VERIFYHOST, 0);
2280 curl_easy_setopt(ps->send_endpoint, CURLOPT_URL, ps->url);
2281 curl_easy_setopt(ps->send_endpoint, CURLOPT_PUT, 1L);
2282 curl_easy_setopt(ps->send_endpoint, CURLOPT_HEADERFUNCTION, &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, curl_receive_cb);
2287 curl_easy_setopt(ps->send_endpoint, CURLOPT_READDATA, ps);
2288 curl_easy_setopt(ps->send_endpoint, CURLOPT_TIMEOUT, (long) timeout.rel_value);
2289 curl_easy_setopt(ps->send_endpoint, CURLOPT_PRIVATE, ps);
2290 curl_easy_setopt(ps->send_endpoint, CURLOPT_CONNECTTIMEOUT, HTTP_CONNECT_TIMEOUT);
2291 curl_easy_setopt(ps->send_endpoint, CURLOPT_BUFFERSIZE, 2 * GNUNET_SERVER_MAX_MESSAGE_SIZE);
2292 #if CURL_TCP_NODELAY
2293 curl_easy_setopt(ps->send_endpoint, CURLOPT_TCP_NODELAY, 1);
2295 if (fresh==GNUNET_YES)
2297 mret = curl_multi_add_handle(plugin->multi_handle, ps->send_endpoint);
2298 if (mret != CURLM_OK)
2300 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2301 _("Connection: %X: %s failed at %s:%d: `%s'\n"),
2303 "curl_multi_add_handle", __FILE__, __LINE__,
2304 curl_multi_strerror (mret));
2305 return GNUNET_SYSERR;
2309 if (plugin->http_curl_task != GNUNET_SCHEDULER_NO_TASK)
2311 GNUNET_SCHEDULER_cancel(plugin->http_curl_task);
2312 plugin->http_curl_task = GNUNET_SCHEDULER_NO_TASK;
2314 plugin->current_connections++;
2315 plugin->http_curl_task = GNUNET_SCHEDULER_add_now (&curl_perform, plugin);
2318 if (ps->direction == INBOUND)
2320 GNUNET_assert (NULL != ps->pending_msgs_tail);
2321 if ((ps->recv_connected==GNUNET_YES) && (ps->send_connected==GNUNET_YES) &&
2322 (ps->recv_force_disconnect==GNUNET_NO) && (ps->recv_force_disconnect==GNUNET_NO))
2325 return GNUNET_SYSERR;
2330 * select best session to transmit data to peer
2332 * @param pc peer context of target peer
2333 * @param addr address of target peer
2334 * @param addrlen address length
2335 * @param force_address does transport service enforce address?
2336 * @param session session passed by transport service
2337 * @return selected session
2340 static struct Session *
2341 send_select_session (struct HTTP_PeerContext *pc,
2342 const void * addr, size_t addrlen,
2344 struct Session * session)
2346 struct Session * tmp = NULL;
2347 int addr_given = GNUNET_NO;
2349 if ((addr!=NULL) && (addrlen>0))
2350 addr_given = GNUNET_YES;
2352 if (force_address == GNUNET_YES)
2354 /* check session given as argument */
2355 if ((session != NULL) && (addr_given == GNUNET_YES))
2357 if (0 == memcmp(session->addr, addr, addrlen))
2359 /* connection can not be used, since it is disconnected */
2360 if ( (session->recv_force_disconnect==GNUNET_NO) &&
2361 (session->send_force_disconnect==GNUNET_NO) )
2363 #if DEBUG_SESSION_SELECTION
2364 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2365 "Session %X selected: Using session passed by transport to send to forced address \n",
2372 /* check last session used */
2373 if ((pc->last_session != NULL)&& (addr_given == GNUNET_YES))
2375 if (0 == memcmp(pc->last_session->addr, addr, addrlen))
2377 /* connection can not be used, since it is disconnected */
2378 if ( (pc->last_session->recv_force_disconnect==GNUNET_NO) &&
2379 (pc->last_session->send_force_disconnect==GNUNET_NO) )
2381 #if DEBUG_SESSION_SELECTION
2382 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2383 "Session %X selected: Using last session used to send to forced address \n",
2386 return pc->last_session;
2390 /* find session in existing sessions */
2392 while ((tmp!=NULL) && (addr_given == GNUNET_YES))
2394 if (0 == memcmp(tmp->addr, addr, addrlen))
2396 /* connection can not be used, since it is disconnected */
2397 if ( (tmp->recv_force_disconnect==GNUNET_NO) &&
2398 (tmp->send_force_disconnect==GNUNET_NO) )
2400 #if DEBUG_SESSION_SELECTION
2401 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2402 "Session %X selected: Using existing session to send to forced address \n",
2410 /* no session to use */
2413 if ((force_address == GNUNET_NO) || (force_address == GNUNET_SYSERR))
2415 /* check session given as argument */
2416 if (session != NULL)
2418 /* connection can not be used, since it is disconnected */
2419 if ( (session->recv_force_disconnect==GNUNET_NO) &&
2420 (session->send_force_disconnect==GNUNET_NO) )
2422 #if DEBUG_SESSION_SELECTION
2423 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2424 "Session %X selected: Using session passed by transport to send not-forced address\n",
2430 /* check last session used */
2431 if (pc->last_session != NULL)
2433 /* connection can not be used, since it is disconnected */
2434 if ( (pc->last_session->recv_force_disconnect==GNUNET_NO) &&
2435 (pc->last_session->send_force_disconnect==GNUNET_NO) )
2437 #if DEBUG_SESSION_SELECTION
2438 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2439 "Session %X selected: Using last session to send to not-forced address\n",
2442 return pc->last_session;
2445 /* find session in existing sessions */
2449 /* connection can not be used, since it is disconnected */
2450 if ( (tmp->recv_force_disconnect==GNUNET_NO) &&
2451 (tmp->send_force_disconnect==GNUNET_NO) )
2453 #if DEBUG_SESSION_SELECTION
2454 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2455 "Session %X selected: Using existing session to send to not-forced address\n",
2469 * Function that can be used by the transport service to transmit
2470 * a message using the plugin. Note that in the case of a
2471 * peer disconnecting, the continuation MUST be called
2472 * prior to the disconnect notification itself. This function
2473 * will be called with this peer's HELLO message to initiate
2474 * a fresh connection to another peer.
2476 * @param cls closure
2477 * @param target who should receive this message
2478 * @param msgbuf the message to transmit
2479 * @param msgbuf_size number of bytes in 'msgbuf'
2480 * @param priority how important is the message (most plugins will
2481 * ignore message priority and just FIFO)
2482 * @param to how long to wait at most for the transmission (does not
2483 * require plugins to discard the message after the timeout,
2484 * just advisory for the desired delay; most plugins will ignore
2486 * @param session which session must be used (or NULL for "any")
2487 * @param addr the address to use (can be NULL if the plugin
2488 * is "on its own" (i.e. re-use existing TCP connection))
2489 * @param addrlen length of the address in bytes
2490 * @param force_address GNUNET_YES if the plugin MUST use the given address,
2491 * GNUNET_NO means the plugin may use any other address and
2492 * GNUNET_SYSERR means that only reliable existing
2493 * bi-directional connections should be used (regardless
2495 * @param cont continuation to call once the message has
2496 * been transmitted (or if the transport is ready
2497 * for the next transmission call; or if the
2498 * peer disconnected...); can be NULL
2499 * @param cont_cls closure for cont
2500 * @return number of bytes used (on the physical network, with overheads);
2501 * -1 on hard errors (i.e. address invalid); 0 is a legal value
2502 * and does NOT mean that the message was not transmitted (DV)
2505 http_plugin_send (void *cls,
2506 const struct GNUNET_PeerIdentity *target,
2509 unsigned int priority,
2510 struct GNUNET_TIME_Relative to,
2511 struct Session *session,
2515 GNUNET_TRANSPORT_TransmitContinuation cont,
2518 struct Plugin *plugin = cls;
2519 struct HTTP_Message *msg;
2520 struct HTTP_PeerContext * pc;
2521 struct Session * ps = NULL;
2523 GNUNET_assert(cls !=NULL);
2528 if (force_address == GNUNET_YES)
2529 GNUNET_asprintf(&force, "forced addr.");
2530 else if (force_address == GNUNET_NO)
2531 GNUNET_asprintf(&force, "any addr.");
2532 else if (force_address == GNUNET_SYSERR)
2533 GNUNET_asprintf(&force,"reliable bi-direc. address addr.");
2536 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2537 "Transport tells me to send %u bytes to `%s' using %s (%s) and session: %X\n",
2541 http_plugin_address_to_string(NULL, addr, addrlen),
2546 pc = GNUNET_CONTAINER_multihashmap_get (plugin->peers, &target->hashPubKey);
2550 pc = GNUNET_malloc(sizeof (struct HTTP_PeerContext));
2551 pc->plugin = plugin;
2552 pc->session_id_counter=1;
2553 pc->last_session = NULL;
2554 memcpy(&pc->identity, target, sizeof(struct GNUNET_PeerIdentity));
2555 GNUNET_CONTAINER_multihashmap_put (plugin->peers,
2556 &pc->identity.hashPubKey,
2558 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
2559 GNUNET_STATISTICS_update (plugin->env->stats,
2560 gettext_noop ("# HTTP peers active"),
2564 ps = send_select_session (pc, addr, addrlen, force_address, session);
2565 /* session not existing, but address forced -> creating new session */
2568 if ((addr!=NULL) && (addrlen!=0))
2570 ps = GNUNET_malloc(sizeof (struct Session));
2571 #if DEBUG_SESSION_SELECTION
2572 if (force_address == GNUNET_YES)
2573 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2574 "No existing connection & forced address: creating new session %X to peer %s\n",
2575 ps, GNUNET_i2s(target));
2576 if (force_address != GNUNET_YES)
2577 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2578 "No existing connection: creating new session %X to peer %s\n",
2579 ps, GNUNET_i2s(target));
2581 ps->addr = GNUNET_malloc(addrlen);
2582 memcpy(ps->addr,addr,addrlen);
2583 ps->addrlen = addrlen;
2584 ps->direction=OUTBOUND;
2585 ps->recv_connected = GNUNET_NO;
2586 ps->recv_force_disconnect = GNUNET_NO;
2587 ps->send_connected = GNUNET_NO;
2588 ps->send_force_disconnect = GNUNET_NO;
2589 ps->pending_msgs_head = NULL;
2590 ps->pending_msgs_tail = NULL;
2592 ps->session_id = pc->session_id_counter;
2593 ps->queue_length_cur = 0;
2594 ps->queue_length_max = GNUNET_SERVER_MAX_MESSAGE_SIZE;
2595 pc->session_id_counter++;
2596 ps->url = create_url (plugin, ps->addr, ps->addrlen, ps->session_id);
2597 if (ps->msgtok == NULL)
2598 ps->msgtok = GNUNET_SERVER_mst_create (&curl_receive_mst_cb, ps);
2599 GNUNET_CONTAINER_DLL_insert(pc->head,pc->tail,ps);
2600 GNUNET_STATISTICS_update (plugin->env->stats,
2601 gettext_noop ("# HTTP outbound sessions for peers active"),
2608 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2609 "No existing session found & and no address given: no way to send this message to peer `%s'!\n",
2610 GNUNET_i2s(target));
2612 return GNUNET_SYSERR;
2616 if (msgbuf_size >= (ps->queue_length_max - ps->queue_length_cur))
2618 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2619 "Queue %X full: %u bytes in queue available, message with %u is too big\n",
2621 (ps->queue_length_max - ps->queue_length_cur),
2623 //return GNUNET_SYSERR;
2627 msg = GNUNET_malloc (sizeof (struct HTTP_Message) + msgbuf_size);
2629 msg->size = msgbuf_size;
2631 msg->buf = (char *) &msg[1];
2632 msg->transmit_cont = cont;
2633 msg->transmit_cont_cls = cont_cls;
2634 memcpy (msg->buf,msgbuf, msgbuf_size);
2635 GNUNET_CONTAINER_DLL_insert (ps->pending_msgs_head,
2636 ps->pending_msgs_tail,
2638 ps->queue_length_cur += msgbuf_size;
2639 if (send_check_connections (plugin, ps) == GNUNET_SYSERR)
2640 return GNUNET_SYSERR;
2641 if (force_address != GNUNET_YES)
2642 pc->last_session = ps;
2643 if (pc->last_session==NULL)
2644 pc->last_session = ps;
2650 * Function that can be used to force the plugin to disconnect
2651 * from the given peer and cancel all previous transmissions
2652 * (and their continuationc).
2654 * @param cls closure
2655 * @param target peer from which to disconnect
2658 http_plugin_disconnect (void *cls,
2659 const struct GNUNET_PeerIdentity *target)
2661 struct Plugin *plugin = cls;
2662 struct HTTP_PeerContext *pc = NULL;
2663 struct Session *ps = NULL;
2665 pc = GNUNET_CONTAINER_multihashmap_get (plugin->peers, &target->hashPubKey);
2671 /* Telling transport that session is getting disconnected */
2672 plugin->env->session_end(plugin, target, ps);
2673 if (ps->direction==OUTBOUND)
2675 if (ps->send_endpoint!=NULL)
2677 //GNUNET_assert(CURLM_OK == curl_multi_remove_handle(plugin->multi_handle,ps->send_endpoint));
2678 //curl_easy_cleanup(ps->send_endpoint);
2679 //ps->send_endpoint=NULL;
2680 ps->send_force_disconnect = GNUNET_YES;
2682 if (ps->recv_endpoint!=NULL)
2684 //GNUNET_assert(CURLM_OK == curl_multi_remove_handle(plugin->multi_handle,ps->recv_endpoint));
2685 //curl_easy_cleanup(ps->recv_endpoint);
2686 //ps->recv_endpoint=NULL;
2687 ps->recv_force_disconnect = GNUNET_YES;
2690 if (ps->direction==INBOUND)
2692 ps->recv_force_disconnect = GNUNET_YES;
2693 ps->send_force_disconnect = GNUNET_YES;
2695 while (ps->pending_msgs_head!=NULL)
2696 remove_http_message(ps, ps->pending_msgs_head);
2697 ps->recv_active = GNUNET_NO;
2698 ps->send_active = GNUNET_NO;
2705 * Convert the transports address to a nice, human-readable
2708 * @param cls closure
2709 * @param type name of the transport that generated the address
2710 * @param addr one of the addresses of the host, NULL for the last address
2711 * the specific address format depends on the transport
2712 * @param addrlen length of the address
2713 * @param numeric should (IP) addresses be displayed in numeric form?
2714 * @param timeout after how long should we give up?
2715 * @param asc function to call on each string
2716 * @param asc_cls closure for asc
2719 http_plugin_address_pretty_printer (void *cls,
2724 struct GNUNET_TIME_Relative timeout,
2725 GNUNET_TRANSPORT_AddressStringCallback
2728 const struct IPv4HttpAddress *t4;
2729 const struct IPv6HttpAddress *t6;
2730 struct sockaddr_in a4;
2731 struct sockaddr_in6 a6;
2737 GNUNET_assert(cls !=NULL);
2738 if (addrlen == sizeof (struct IPv6HttpAddress))
2740 address = GNUNET_malloc (INET6_ADDRSTRLEN);
2742 a6.sin6_addr = t6->ipv6_addr;
2743 inet_ntop(AF_INET6, &(a6.sin6_addr),address,INET6_ADDRSTRLEN);
2744 port = ntohs(t6->u6_port);
2746 else if (addrlen == sizeof (struct IPv4HttpAddress))
2748 address = GNUNET_malloc (INET_ADDRSTRLEN);
2750 a4.sin_addr.s_addr = t4->ipv4_addr;
2751 inet_ntop(AF_INET, &(a4.sin_addr),address,INET_ADDRSTRLEN);
2752 port = ntohs(t4->u_port);
2756 /* invalid address */
2757 GNUNET_break_op (0);
2758 asc (asc_cls, NULL);
2761 res = GNUNET_asprintf(&ret,"%s://%s:%u/", PROTOCOL_PREFIX, address, port);
2762 GNUNET_free (address);
2763 GNUNET_assert(res != 0);
2765 GNUNET_free_non_null (ret);
2771 * Another peer has suggested an address for this
2772 * peer and transport plugin. Check that this could be a valid
2773 * address. If so, consider adding it to the list
2776 * @param cls closure
2777 * @param addr pointer to the address
2778 * @param addrlen length of addr
2779 * @return GNUNET_OK if this is a plausible address for this peer
2783 http_plugin_address_suggested (void *cls,
2784 const void *addr, size_t addrlen)
2786 struct Plugin *plugin = cls;
2787 struct IPv4HttpAddress *v4;
2788 struct IPv6HttpAddress *v6;
2789 struct IPv4HttpAddressWrapper *w_tv4 = plugin->ipv4_addr_head;
2790 struct IPv6HttpAddressWrapper *w_tv6 = plugin->ipv6_addr_head;
2792 GNUNET_assert(cls !=NULL);
2793 if ((addrlen != sizeof (struct IPv4HttpAddress)) &&
2794 (addrlen != sizeof (struct IPv6HttpAddress)))
2795 return GNUNET_SYSERR;
2796 if (addrlen == sizeof (struct IPv4HttpAddress))
2798 v4 = (struct IPv4HttpAddress *) addr;
2799 if (plugin->bind4_address!=NULL)
2801 if (0 == memcmp (&plugin->bind4_address->sin_addr, &v4->ipv4_addr, sizeof(uint32_t)))
2804 return GNUNET_SYSERR;
2808 if (0==memcmp (&w_tv4->addr->ipv4_addr, &v4->ipv4_addr, sizeof(uint32_t)))
2810 w_tv4 = w_tv4->next;
2815 return GNUNET_SYSERR;
2817 if (addrlen == sizeof (struct IPv6HttpAddress))
2819 v6 = (struct IPv6HttpAddress *) addr;
2820 if (plugin->bind6_address!=NULL)
2822 if (0 == memcmp (&plugin->bind6_address->sin6_addr, &v6->ipv6_addr, sizeof(struct in6_addr)))
2825 return GNUNET_SYSERR;
2829 if (0 == memcmp (&w_tv6->addr->ipv6_addr, &v6->ipv6_addr, sizeof(struct in6_addr)))
2831 w_tv6 = w_tv6->next;
2836 return GNUNET_SYSERR;
2838 return GNUNET_SYSERR;
2843 * Function called for a quick conversion of the binary address to
2844 * a numeric address. Note that the caller must not free the
2845 * address and that the next call to this function is allowed
2846 * to override the address again.
2848 * @param cls closure
2849 * @param addr binary address
2850 * @param addrlen length of the address
2851 * @return string representing the same address
2854 http_plugin_address_to_string (void *cls,
2858 const struct IPv4HttpAddress *t4;
2859 const struct IPv6HttpAddress *t6;
2860 struct sockaddr_in a4;
2861 struct sockaddr_in6 a6;
2863 static char rbuf[INET6_ADDRSTRLEN + 13];
2867 if (addrlen == sizeof (struct IPv6HttpAddress))
2869 address = GNUNET_malloc (INET6_ADDRSTRLEN);
2871 a6.sin6_addr = t6->ipv6_addr;
2872 inet_ntop(AF_INET6, &(a6.sin6_addr),address,INET6_ADDRSTRLEN);
2873 port = ntohs(t6->u6_port);
2875 else if (addrlen == sizeof (struct IPv4HttpAddress))
2877 address = GNUNET_malloc (INET_ADDRSTRLEN);
2879 a4.sin_addr.s_addr = t4->ipv4_addr;
2880 inet_ntop(AF_INET, &(a4.sin_addr),address,INET_ADDRSTRLEN);
2881 port = ntohs(t4->u_port);
2885 /* invalid address */
2889 res = GNUNET_snprintf (rbuf,
2895 GNUNET_free (address);
2896 GNUNET_assert(res != 0);
2901 * Function called by the NAT subsystem suggesting another peer wants
2902 * to connect to us via connection reversal. Try to connect back to the
2905 * @param cls closure
2906 * @param addr address to try
2907 * @param addrlen number of bytes in addr
2910 try_connection_reversal (void *cls,
2911 const struct sockaddr *addr,
2918 tcp_nat_cb_add_addr (void *cls,
2920 const struct sockaddr *addr,
2923 struct Plugin *plugin = cls;
2924 struct IPv4HttpAddress * t4 = NULL;
2925 struct IPv4HttpAddressWrapper * w_t4 = NULL;
2926 struct IPv6HttpAddress * t6 = NULL;
2927 struct IPv6HttpAddressWrapper * w_t6 = NULL;
2930 af = addr->sa_family;
2934 w_t4 = plugin->ipv4_addr_head;
2935 while (w_t4 != NULL)
2937 int res = memcmp(&w_t4->addr->ipv4_addr,
2938 &((struct sockaddr_in *) addr)->sin_addr,
2939 sizeof (struct in_addr));
2946 w_t4 = GNUNET_malloc(sizeof(struct IPv4HttpAddressWrapper));
2947 t4 = GNUNET_malloc(sizeof(struct IPv4HttpAddress));
2948 memcpy (&t4->ipv4_addr,
2949 &((struct sockaddr_in *) addr)->sin_addr,
2950 sizeof (struct in_addr));
2951 t4->u_port = htons (plugin->port_inbound);
2955 GNUNET_CONTAINER_DLL_insert(plugin->ipv4_addr_head,
2956 plugin->ipv4_addr_tail,w_t4);
2958 plugin->env->notify_address(plugin->env->cls,
2960 w_t4->addr, sizeof (struct IPv4HttpAddress));
2964 w_t6 = plugin->ipv6_addr_head;
2967 int res = memcmp(&w_t6->addr->ipv6_addr,
2968 &((struct sockaddr_in6 *) addr)->sin6_addr,
2969 sizeof (struct in6_addr));
2976 w_t6 = GNUNET_malloc(sizeof(struct IPv6HttpAddressWrapper));
2977 t6 = GNUNET_malloc(sizeof(struct IPv6HttpAddress));
2979 memcpy (&t6->ipv6_addr,
2980 &((struct sockaddr_in6 *) addr)->sin6_addr,
2981 sizeof (struct in6_addr));
2982 t6->u6_port = htons (plugin->port_inbound);
2986 GNUNET_CONTAINER_DLL_insert(plugin->ipv6_addr_head,
2987 plugin->ipv6_addr_tail,w_t6);
2989 plugin->env->notify_address(plugin->env->cls,
2991 w_t6->addr, sizeof (struct IPv6HttpAddress));
3000 tcp_nat_cb_remove_addr (void *cls,
3002 const struct sockaddr *addr,
3005 struct Plugin *plugin = cls;
3006 struct IPv4HttpAddressWrapper * w_t4 = NULL;
3007 struct IPv6HttpAddressWrapper * w_t6 = NULL;
3010 af = addr->sa_family;
3014 w_t4 = plugin->ipv4_addr_head;
3015 while (w_t4 != NULL)
3017 int res = memcmp(&w_t4->addr->ipv4_addr,
3018 &((struct sockaddr_in *) addr)->sin_addr,
3019 sizeof (struct in_addr));
3026 plugin->env->notify_address(plugin->env->cls,
3028 w_t4->addr, sizeof (struct IPv4HttpAddress));
3030 GNUNET_CONTAINER_DLL_remove(plugin->ipv4_addr_head,
3031 plugin->ipv4_addr_tail,w_t4);
3032 GNUNET_free (w_t4->addr);
3036 w_t6 = plugin->ipv6_addr_head;
3037 while (w_t6 != NULL)
3039 int res = memcmp(&w_t6->addr->ipv6_addr,
3040 &((struct sockaddr_in6 *) addr)->sin6_addr,
3041 sizeof (struct in6_addr));
3048 plugin->env->notify_address(plugin->env->cls,
3050 w_t6->addr, sizeof (struct IPv6HttpAddress));
3052 GNUNET_CONTAINER_DLL_remove(plugin->ipv6_addr_head,
3053 plugin->ipv6_addr_tail,w_t6);
3054 GNUNET_free (w_t6->addr);
3064 * Our external IP address/port mapping has changed.
3066 * @param cls closure, the 'struct LocalAddrList'
3067 * @param add_remove GNUNET_YES to mean the new public IP address, GNUNET_NO to mean
3068 * the previous (now invalid) one
3069 * @param addr either the previous or the new public IP address
3070 * @param addrlen actual lenght of the address
3073 tcp_nat_port_map_callback (void *cls,
3075 const struct sockaddr *addr,
3078 GNUNET_assert(cls !=NULL );
3080 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
3081 "NPMC called to %s address `%s'\n",
3082 (add_remove == GNUNET_YES) ? "remove" : "add",
3083 GNUNET_a2s (addr, addrlen));
3085 /* convert 'addr' to our internal format */
3089 tcp_nat_cb_add_addr (cls, add_remove, addr, addrlen);
3092 tcp_nat_cb_remove_addr (cls, add_remove, addr, addrlen);
3099 * Notify transport service about address
3101 * @param cls the plugin
3105 address_notification (void *cls,
3106 const struct GNUNET_SCHEDULER_TaskContext *tc)
3108 struct Plugin *plugin = cls;
3110 GNUNET_OS_network_interfaces_list (&process_interfaces, plugin);
3115 * Exit point from the plugin.
3118 LIBGNUNET_PLUGIN_TRANSPORT_DONE (void *cls)
3120 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
3121 struct Plugin *plugin = api->cls;
3123 struct IPv4HttpAddressWrapper * w_t4;
3124 struct IPv6HttpAddressWrapper * w_t6;
3125 GNUNET_assert(cls !=NULL);
3127 if (plugin->nat != NULL)
3128 GNUNET_NAT_unregister (plugin->nat);
3130 if (plugin->http_server_daemon_v4 != NULL)
3132 MHD_stop_daemon (plugin->http_server_daemon_v4);
3133 plugin->http_server_daemon_v4 = NULL;
3135 if (plugin->http_server_daemon_v6 != NULL)
3137 MHD_stop_daemon (plugin->http_server_daemon_v6);
3138 plugin->http_server_daemon_v6 = NULL;
3140 if ( plugin->http_server_task_v4 != GNUNET_SCHEDULER_NO_TASK)
3142 GNUNET_SCHEDULER_cancel(plugin->http_server_task_v4);
3143 plugin->http_server_task_v4 = GNUNET_SCHEDULER_NO_TASK;
3145 if ( plugin->http_server_task_v6 != GNUNET_SCHEDULER_NO_TASK)
3147 GNUNET_SCHEDULER_cancel(plugin->http_server_task_v6);
3148 plugin->http_server_task_v6 = GNUNET_SCHEDULER_NO_TASK;
3151 while (plugin->ipv4_addr_head!=NULL)
3153 w_t4 = plugin->ipv4_addr_head;
3154 GNUNET_CONTAINER_DLL_remove(plugin->ipv4_addr_head,plugin->ipv4_addr_tail,w_t4);
3155 GNUNET_free(w_t4->addr);
3159 while (plugin->ipv6_addr_head!=NULL)
3161 w_t6 = plugin->ipv6_addr_head;
3162 GNUNET_CONTAINER_DLL_remove(plugin->ipv6_addr_head,plugin->ipv6_addr_tail,w_t6);
3163 GNUNET_free(w_t6->addr);
3167 /* free all peer information */
3168 if (plugin->peers!=NULL)
3170 GNUNET_CONTAINER_multihashmap_iterate (plugin->peers,
3171 &remove_peer_context_Iterator,
3173 GNUNET_CONTAINER_multihashmap_destroy (plugin->peers);
3175 if (plugin->multi_handle!=NULL)
3177 mret = curl_multi_cleanup(plugin->multi_handle);
3178 if (CURLM_OK != mret)
3179 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3180 "curl multihandle clean up failed\n");
3181 plugin->multi_handle = NULL;
3183 curl_global_cleanup();
3185 if ( plugin->http_curl_task != GNUNET_SCHEDULER_NO_TASK)
3187 GNUNET_SCHEDULER_cancel(plugin->http_curl_task);
3188 plugin->http_curl_task = GNUNET_SCHEDULER_NO_TASK;
3192 GNUNET_free_non_null (plugin->bind4_address);
3193 GNUNET_free_non_null (plugin->bind6_address);
3194 GNUNET_free_non_null(plugin->bind_hostname);
3196 GNUNET_free_non_null (plugin->crypto_init);
3197 GNUNET_free_non_null (plugin->cert);
3198 GNUNET_free_non_null (plugin->key);
3200 GNUNET_free (plugin);
3203 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3204 "Unload %s plugin complete...\n",
3212 load_certificate( const char * file )
3214 struct GNUNET_DISK_FileHandle * gn_file;
3218 if (0!=STAT(file, &fstat))
3220 text = GNUNET_malloc (fstat.st_size+1);
3221 gn_file = GNUNET_DISK_file_open(file, GNUNET_DISK_OPEN_READ, GNUNET_DISK_PERM_USER_READ);
3227 if (GNUNET_SYSERR == GNUNET_DISK_file_read(gn_file, text, fstat.st_size))
3230 GNUNET_DISK_file_close(gn_file);
3233 text[fstat.st_size] = '\0';
3234 GNUNET_DISK_file_close(gn_file);
3241 * Entry point for the plugin.
3244 LIBGNUNET_PLUGIN_TRANSPORT_INIT (void *cls)
3246 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
3247 struct Plugin *plugin;
3248 struct GNUNET_TRANSPORT_PluginFunctions *api;
3249 struct GNUNET_TIME_Relative gn_timeout;
3250 long long unsigned int port;
3251 unsigned long long tneigh;
3252 struct sockaddr **addrs;
3253 socklen_t *addrlens;
3255 char * component_name;
3257 char * key_file = NULL;
3258 char * cert_file = NULL;
3261 GNUNET_assert(cls !=NULL);
3263 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3264 "Starting %s plugin...\n",
3267 GNUNET_asprintf(&component_name,
3271 plugin = GNUNET_malloc (sizeof (struct Plugin));
3272 plugin->stats = env->stats;
3274 plugin->peers = NULL;
3275 plugin->bind4_address = NULL;
3276 plugin->bind6_address = NULL;
3277 plugin->use_ipv6 = GNUNET_YES;
3278 plugin->use_ipv4 = GNUNET_YES;
3279 plugin->use_localaddresses = GNUNET_NO;
3281 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
3283 api->send = &http_plugin_send;
3284 api->disconnect = &http_plugin_disconnect;
3285 api->address_pretty_printer = &http_plugin_address_pretty_printer;
3286 api->check_address = &http_plugin_address_suggested;
3287 api->address_to_string = &http_plugin_address_to_string;
3289 /* Hashing our identity to use it in URLs */
3290 GNUNET_CRYPTO_hash_to_enc (&(plugin->env->my_identity->hashPubKey),
3291 &plugin->my_ascii_hash_ident);
3294 if (GNUNET_CONFIGURATION_have_value (env->cfg, "TRANSPORT", "NEIGHBOUR_LIMIT"))
3296 GNUNET_CONFIGURATION_get_value_number (env->cfg,
3305 plugin->max_connect_per_transport = tneigh;
3309 if (GNUNET_CONFIGURATION_have_value (env->cfg,
3310 component_name, "USE_IPv6"))
3312 plugin->use_ipv6 = GNUNET_CONFIGURATION_get_value_yesno (env->cfg,
3317 if (GNUNET_CONFIGURATION_have_value (env->cfg,
3318 component_name, "USE_IPv4"))
3320 plugin->use_ipv4 = GNUNET_CONFIGURATION_get_value_yesno (env->cfg,
3321 component_name,"USE_IPv4");
3323 /* use local addresses? */
3325 if (GNUNET_CONFIGURATION_have_value (env->cfg,
3326 component_name, "USE_LOCALADDR"))
3328 plugin->use_localaddresses = GNUNET_CONFIGURATION_get_value_yesno (env->cfg,
3332 /* Reading port number from config file */
3334 GNUNET_CONFIGURATION_get_value_number (env->cfg,
3340 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
3342 _("Require valid port number for transport plugin `%s' in configuration!\n"),
3344 GNUNET_free(component_name);
3345 LIBGNUNET_PLUGIN_TRANSPORT_DONE (api);
3349 /* Reading ipv4 addresse to bind to from config file */
3350 if ( (plugin->use_ipv4==GNUNET_YES) &&
3351 (GNUNET_CONFIGURATION_have_value (env->cfg,
3352 component_name, "BINDTO4")))
3354 GNUNET_break (GNUNET_OK ==
3355 GNUNET_CONFIGURATION_get_value_string (env->cfg,
3358 &plugin->bind_hostname));
3359 plugin->bind4_address = GNUNET_malloc(sizeof(struct sockaddr_in));
3360 plugin->bind4_address->sin_family = AF_INET;
3361 plugin->bind4_address->sin_port = htons (port);
3363 if (plugin->bind_hostname!=NULL)
3365 if (inet_pton(AF_INET,plugin->bind_hostname, &plugin->bind4_address->sin_addr)<=0)
3367 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
3369 _("Misconfigured address to bind to in configuration!\n"));
3370 GNUNET_free(plugin->bind4_address);
3371 GNUNET_free(plugin->bind_hostname);
3372 plugin->bind_hostname = NULL;
3373 plugin->bind4_address = NULL;
3378 /* Reading ipv4 addresse to bind to from config file */
3379 if ( (plugin->use_ipv6==GNUNET_YES) &&
3380 (GNUNET_CONFIGURATION_have_value (env->cfg,
3381 component_name, "BINDTO6")))
3383 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (env->cfg,
3386 &plugin->bind_hostname))
3388 plugin->bind6_address = GNUNET_malloc(sizeof(struct sockaddr_in6));
3389 plugin->bind6_address->sin6_family = AF_INET6;
3390 plugin->bind6_address->sin6_port = htons (port);
3391 if (plugin->bind_hostname!=NULL)
3393 if (inet_pton(AF_INET6,plugin->bind_hostname, &plugin->bind6_address->sin6_addr)<=0)
3395 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
3397 _("Misconfigured address to bind to in configuration!\n"));
3398 GNUNET_free(plugin->bind6_address);
3399 GNUNET_free(plugin->bind_hostname);
3400 plugin->bind_hostname = NULL;
3401 plugin->bind6_address = NULL;
3408 /* Reading HTTPS crypto related configuration */
3409 /* Get crypto init string from config */
3410 if (GNUNET_CONFIGURATION_have_value (env->cfg,
3411 "transport-https", "CRYPTO_INIT"))
3413 GNUNET_CONFIGURATION_get_value_string (env->cfg,
3416 &plugin->crypto_init);
3420 GNUNET_asprintf(&plugin->crypto_init,"NORMAL");
3423 /* Get private key file from config */
3424 if (GNUNET_CONFIGURATION_have_value (env->cfg,
3425 "transport-https", "KEY_FILE"))
3427 GNUNET_CONFIGURATION_get_value_filename (env->cfg,
3433 GNUNET_asprintf(&key_file,"https.key");
3435 /* Get private key file from config */
3436 if (GNUNET_CONFIGURATION_have_value (env->cfg,"transport-https", "CERT_FILE"))
3438 GNUNET_CONFIGURATION_get_value_filename (env->cfg,
3443 if (cert_file==NULL)
3444 GNUNET_asprintf(&cert_file,"https.cert");
3446 /* read key & certificates from file */
3447 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3448 "Loading TLS certificate `%s' `%s'\n",
3449 key_file, cert_file);
3451 plugin->key = load_certificate( key_file );
3452 plugin->cert = load_certificate( cert_file );
3454 if ((plugin->key==NULL) || (plugin->cert==NULL))
3458 GNUNET_asprintf(&cmd,
3459 "gnunet-transport-certificate-creation %s %s",
3460 key_file, cert_file);
3461 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3462 "No usable TLS certificate found, creating certificate \n");
3466 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
3468 _("Could not create a new TLS certificate, shell script `%s' failed!\n"),cmd,
3470 GNUNET_free (key_file);
3471 GNUNET_free (cert_file);
3472 GNUNET_free (component_name);
3473 LIBGNUNET_PLUGIN_TRANSPORT_DONE(api);
3478 plugin->key = load_certificate( key_file );
3479 plugin->cert = load_certificate( cert_file );
3480 if ((plugin->key==NULL) || (plugin->cert==NULL))
3482 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
3484 _("No usable TLS certificate found and creating one failed! \n"),
3486 GNUNET_free (key_file);
3487 GNUNET_free (cert_file);
3488 GNUNET_free (component_name);
3490 LIBGNUNET_PLUGIN_TRANSPORT_DONE(api);
3494 GNUNET_free (key_file);
3495 GNUNET_free (cert_file);
3497 GNUNET_assert((plugin->key!=NULL) && (plugin->cert!=NULL));
3498 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "TLS certificate loaded\n");
3501 GNUNET_assert ((port > 0) && (port <= 65535));
3502 plugin->port_inbound = port;
3503 gn_timeout = GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT;
3504 unsigned int timeout = (gn_timeout.rel_value) / 1000;
3505 if ( (plugin->http_server_daemon_v6 == NULL) &&
3506 (plugin->use_ipv6 == GNUNET_YES) &&
3509 struct sockaddr * tmp = (struct sockaddr *) plugin->bind6_address;
3510 plugin->http_server_daemon_v6 = MHD_start_daemon (
3519 &mhd_accept_cb, plugin,
3520 &mhd_access_cb, plugin,
3521 MHD_OPTION_SOCK_ADDR, tmp,
3522 MHD_OPTION_CONNECTION_LIMIT, (unsigned int) plugin->max_connect_per_transport,
3524 MHD_OPTION_HTTPS_PRIORITIES, plugin->crypto_init,
3525 MHD_OPTION_HTTPS_MEM_KEY, plugin->key,
3526 MHD_OPTION_HTTPS_MEM_CERT, plugin->cert,
3528 MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) timeout,
3529 MHD_OPTION_CONNECTION_MEMORY_LIMIT, (size_t) (2 * GNUNET_SERVER_MAX_MESSAGE_SIZE),
3530 MHD_OPTION_NOTIFY_COMPLETED, &mhd_termination_cb, plugin,
3531 MHD_OPTION_EXTERNAL_LOGGER, mhd_logger, plugin->mhd_log,
3534 if ( (plugin->http_server_daemon_v4 == NULL) &&
3535 (plugin->use_ipv4 == GNUNET_YES) &&
3538 plugin->http_server_daemon_v4 = MHD_start_daemon (
3547 &mhd_accept_cb, plugin ,
3548 &mhd_access_cb, plugin,
3549 MHD_OPTION_SOCK_ADDR, (struct sockaddr_in *) plugin->bind4_address,
3550 MHD_OPTION_CONNECTION_LIMIT, (unsigned int) plugin->max_connect_per_transport,
3552 MHD_OPTION_HTTPS_PRIORITIES, plugin->crypto_init,
3554 MHD_OPTION_HTTPS_MEM_KEY, plugin->key,
3555 MHD_OPTION_HTTPS_MEM_CERT, plugin->cert,
3557 MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) timeout,
3558 MHD_OPTION_CONNECTION_MEMORY_LIMIT, (size_t) (2 * GNUNET_SERVER_MAX_MESSAGE_SIZE),
3559 MHD_OPTION_NOTIFY_COMPLETED, &mhd_termination_cb, plugin,
3560 MHD_OPTION_EXTERNAL_LOGGER, mhd_logger, plugin->mhd_log,
3563 if (plugin->http_server_daemon_v4 != NULL)
3564 plugin->http_server_task_v4 = http_server_daemon_prepare (plugin, plugin->http_server_daemon_v4);
3565 if (plugin->http_server_daemon_v6 != NULL)
3566 plugin->http_server_task_v6 = 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!=NULL) ? plugin->bind_hostname : "every address",port);
3577 else if ( (plugin->http_server_task_v6 != GNUNET_SCHEDULER_NO_TASK) &&
3578 (plugin->http_server_task_v4 != GNUNET_SCHEDULER_NO_TASK) )
3581 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3582 "Starting MHD with IPv6 bound to %s with port %u\n",
3583 (plugin->bind_hostname!=NULL) ? plugin->bind_hostname : "every address", port);
3586 else if ( (plugin->http_server_task_v6 != GNUNET_SCHEDULER_NO_TASK) &&
3587 (plugin->http_server_task_v4 == GNUNET_SCHEDULER_NO_TASK) )
3590 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3591 "Starting MHD with IPv4 and IPv6 bound to %s with port %u\n",
3592 (plugin->bind_hostname!=NULL) ? plugin->bind_hostname : "every address",
3599 if ((plugin->use_ipv6 == GNUNET_YES) && (plugin->use_ipv4 == GNUNET_YES))
3600 GNUNET_asprintf(&tmp,"with IPv4 and IPv6 enabled");
3601 if ((plugin->use_ipv6 == GNUNET_NO) && (plugin->use_ipv4 == GNUNET_YES))
3602 GNUNET_asprintf(&tmp,"with IPv4 enabled");
3603 if ((plugin->use_ipv6 == GNUNET_YES) && (plugin->use_ipv4 == GNUNET_NO))
3604 GNUNET_asprintf(&tmp,"with IPv6 enabled");
3605 if ((plugin->use_ipv6 == GNUNET_NO) && (plugin->use_ipv4 == GNUNET_NO))
3606 GNUNET_asprintf(&tmp,"with NO IP PROTOCOL enabled");
3607 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3608 _("HTTP Server with %s could not be started on port %u! %s plugin failed!\n"),
3609 tmp, port, PROTOCOL_PREFIX);
3611 GNUNET_free (component_name);
3612 LIBGNUNET_PLUGIN_TRANSPORT_DONE (api);
3616 /* Initializing cURL */
3617 curl_global_init(CURL_GLOBAL_ALL);
3618 plugin->multi_handle = curl_multi_init();
3620 if ( NULL == plugin->multi_handle )
3622 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
3624 _("Could not initialize curl multi handle, failed to start %s plugin!\n"),
3626 GNUNET_free(component_name);
3627 LIBGNUNET_PLUGIN_TRANSPORT_DONE (api);
3631 ret = GNUNET_SERVICE_get_server_addresses (component_name,
3636 if (ret != GNUNET_SYSERR)
3638 plugin->nat = GNUNET_NAT_register (env->cfg,
3642 (const struct sockaddr **) addrs,
3644 &tcp_nat_port_map_callback,
3645 &try_connection_reversal,
3650 GNUNET_assert (addrs[ret] != NULL);
3651 GNUNET_free (addrs[ret]);
3653 GNUNET_free_non_null (addrs);
3654 GNUNET_free_non_null (addrlens);
3658 plugin->nat = GNUNET_NAT_register (env->cfg,
3663 &try_connection_reversal,
3667 plugin->peers = GNUNET_CONTAINER_multihashmap_create (10);
3669 GNUNET_free(component_name);
3670 //GNUNET_SCHEDULER_add_now(address_notification, plugin);
3674 /* end of plugin_transport_http.c */