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
95 * Network format for IPv4 addresses.
97 struct IPv4HttpAddress
102 struct IPv4HttpAddress * next;
105 * Linked list previous
107 struct IPv4HttpAddress * prev;
110 * IPv4 address, in network byte order.
112 uint32_t ipv4_addr GNUNET_PACKED;
115 * Port number, in network byte order.
117 uint16_t u_port GNUNET_PACKED;
123 * Network format for IPv6 addresses.
125 struct IPv6HttpAddress
130 struct IPv6HttpAddress * next;
133 * Linked list previous
135 struct IPv6HttpAddress * prev;
140 struct in6_addr ipv6_addr GNUNET_PACKED;
143 * Port number, in network byte order.
145 uint16_t u6_port GNUNET_PACKED;
151 * Message to send using http
156 * next pointer for double linked list
158 struct HTTP_Message * next;
161 * previous pointer for double linked list
163 struct HTTP_Message * prev;
166 * buffer containing data to send
171 * amount of data already sent
181 * Continuation function to call once the transmission buffer
182 * has again space available. NULL if there is no
183 * continuation to call.
185 GNUNET_TRANSPORT_TransmitContinuation transmit_cont;
188 * Closure for transmit_cont.
190 void *transmit_cont_cls;
194 struct HTTP_PeerContext
199 struct GNUNET_PeerIdentity identity;
202 * Pointer to the global plugin struct.
204 struct Plugin *plugin;
207 * Linked list of connections with this peer
210 struct Session * head;
213 * Linked list of connections with this peer
216 struct Session * tail;
219 * id for next session
221 size_t session_id_counter;
224 * Last session used to send data
226 struct Session * last_session;
229 * The task resetting inbound quota delay
231 GNUNET_SCHEDULER_TaskIdentifier reset_task;
234 * Delay from transport service inbound quota tracker when to receive data again
236 struct GNUNET_TIME_Relative delay;
245 struct SessionHeader header;
248 * next session in linked list
250 struct Session * next;
253 * previous session in linked list
255 struct Session * prev;
258 * address of this session
273 * Message queue for outbound messages
276 struct HTTP_Message * pending_msgs_head;
279 * Message queue for outbound messages
282 struct HTTP_Message * pending_msgs_tail;
285 * partner peer this connection belongs to
287 struct HTTP_PeerContext * peercontext;
290 * message stream tokenizer for incoming data
292 struct GNUNET_SERVER_MessageStreamTokenizer *msgtok;
296 * outbound: OUTBOUND (GNUNET_YES)
297 * inbound : INBOUND (GNUNET_NO)
299 unsigned int direction;
302 * is session connected to send data?
304 unsigned int send_connected;
307 * is send connection active?
309 unsigned int send_active;
312 * connection disconnect forced (e.g. from transport)
314 unsigned int send_force_disconnect;
317 * is session connected to receive data?
319 unsigned int recv_connected;
322 * is receive connection active?
324 unsigned int recv_active;
327 * connection disconnect forced (e.g. from transport)
329 unsigned int recv_force_disconnect;
333 * id for next session
334 * NOTE: 0 is not an ID, zero is not defined. A correct ID is always > 0
339 * entity managing sending data
340 * outbound session: CURL *
341 * inbound session: mhd_connection *
343 void * send_endpoint;
346 * entity managing recieving data
347 * outbound session: CURL *
348 * inbound session: mhd_connection *
350 void * recv_endpoint;
355 size_t queue_length_cur;
360 size_t queue_length_max;
365 * Encapsulation of all of the state of the plugin.
372 struct GNUNET_TRANSPORT_PluginEnvironment *env;
375 * Handle for reporting statistics.
377 struct GNUNET_STATISTICS_Handle *stats;
382 uint16_t port_inbound;
384 struct GNUNET_CONTAINER_MultiHashMap *peers;
387 * Daemon for listening for new IPv4 connections.
389 struct MHD_Daemon *http_server_daemon_v4;
392 * Daemon for listening for new IPv6connections.
394 struct MHD_Daemon *http_server_daemon_v6;
397 * Our primary task for http daemon handling IPv4 connections
399 GNUNET_SCHEDULER_TaskIdentifier http_server_task_v4;
402 * Our primary task for http daemon handling IPv6 connections
404 GNUNET_SCHEDULER_TaskIdentifier http_server_task_v6;
407 * The task sending data
409 GNUNET_SCHEDULER_TaskIdentifier http_curl_task;
414 CURLM * multi_handle;
417 * Our handle to the NAT module.
419 struct GNUNET_NAT_Handle *nat;
424 struct IPv4HttpAddress * ipv4_addr_head;
429 struct IPv4HttpAddress * ipv4_addr_tail;
434 struct IPv6HttpAddress * ipv6_addr_head;
439 struct IPv6HttpAddress * ipv6_addr_tail;
442 * Our ASCII encoded, hashed peer identity
443 * This string is used to distinguish between connections and is added to the urls
445 struct GNUNET_CRYPTO_HashAsciiEncoded my_ascii_hash_ident;
448 * IPv4 Address the plugin binds to
450 struct sockaddr_in * bind4_address;
453 * IPv6 Address the plugins binds to
455 struct sockaddr_in6 * bind6_address;
458 * Hostname to bind to
460 char * bind_hostname;
473 * use local addresses?
475 int use_localaddresses;
478 * maximum number of connections
480 int max_connect_per_transport;
483 * Current number of connections;
485 int current_connections;
488 * Closure passed by MHD to the mhd_logger function
492 /* only needed for HTTPS plugin */
494 /* The certificate MHD uses as an \0 terminated string */
497 /* The private key MHD uses as an \0 terminated string */
500 /* crypto init string */
507 * Function called for a quick conversion of the binary address to
508 * a numeric address. Note that the caller must not free the
509 * address and that the next call to this function is allowed
510 * to override the address again.
513 * @param addr binary address
514 * @param addrlen length of the address
515 * @return string representing the same address
518 http_plugin_address_to_string (void *cls,
524 * Call MHD to process pending ipv4 requests and then go back
525 * and schedule the next run.
527 static void http_server_daemon_v4_run (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
529 * Call MHD to process pending ipv6 requests and then go back
530 * and schedule the next run.
532 static void http_server_daemon_v6_run (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
535 * Function setting up curl handle and selecting message to send
537 * @param plugin plugin
539 * @return GNUNET_SYSERR on failure, GNUNET_NO if connecting, GNUNET_YES if ok
541 static int send_check_connections (struct Plugin *plugin, struct Session *ps);
544 * Function setting up file descriptors and scheduling task to run
546 * @param plugin plugin as closure
547 * @return GNUNET_SYSERR for hard failure, GNUNET_OK for ok
549 static int curl_schedule (struct Plugin *plugin);
552 * Task scheduled to reset the inbound quota delay for a specific peer
553 * @param cls plugin as closure
554 * @param tc task context
556 static void reset_inbound_quota_delay (void *cls,
557 const struct GNUNET_SCHEDULER_TaskContext *tc)
559 struct HTTP_PeerContext * pc = cls;
561 GNUNET_assert(cls != NULL);
562 pc->reset_task = GNUNET_SCHEDULER_NO_TASK;
563 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
565 pc->delay = GNUNET_TIME_relative_get_zero ();
570 * Creates a valid url from passed address and id
571 * @param plugin plugin
572 * @param addr address to create url from
573 * @param addrlen address lenth
574 * @param id session id
575 * @return the created url
578 create_url(struct Plugin *plugin,
579 const void * addr, size_t addrlen,
583 char *addr_str = (char *) http_plugin_address_to_string(NULL, addr, addrlen);
585 GNUNET_assert ((addr!=NULL) && (addrlen != 0));
586 GNUNET_asprintf(&url,
587 "%s://%s/%s;%u", PROTOCOL_PREFIX, addr_str,
588 (char *) (&plugin->my_ascii_hash_ident),id);
594 * Removes a message from the linked list of messages
597 * @return GNUNET_SYSERR if msg not found, GNUNET_OK on success
600 remove_http_message (struct Session * ps,
601 struct HTTP_Message * msg)
603 GNUNET_CONTAINER_DLL_remove(ps->pending_msgs_head,
604 ps->pending_msgs_tail,
611 * Iterator to remove peer context
612 * @param cls the plugin
613 * @param key the peers public key hashcode
614 * @param value the peer context
615 * @return GNUNET_YES on success
618 remove_peer_context_Iterator (void *cls,
619 const GNUNET_HashCode *key,
622 struct Plugin *plugin = cls;
623 struct HTTP_PeerContext * pc = value;
624 struct Session * ps = pc->head;
625 struct Session * tmp = NULL;
626 struct HTTP_Message * msg = NULL;
627 struct HTTP_Message * msg_tmp = NULL;
630 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
631 "Freeing context for peer `%s'\n",
632 GNUNET_i2s(&pc->identity));
634 GNUNET_CONTAINER_multihashmap_remove (plugin->peers, &pc->identity.hashPubKey, pc);
637 plugin->env->session_end(plugin, &pc->identity, ps);
640 GNUNET_free_non_null (ps->addr);
641 GNUNET_free(ps->url);
642 if (ps->msgtok != NULL)
643 GNUNET_SERVER_mst_destroy (ps->msgtok);
645 msg = ps->pending_msgs_head;
652 if (ps->direction==OUTBOUND)
654 if (ps->send_endpoint!=NULL)
655 curl_easy_cleanup(ps->send_endpoint);
656 if (ps->recv_endpoint!=NULL)
657 curl_easy_cleanup(ps->recv_endpoint);
663 GNUNET_STATISTICS_update (plugin->env->stats,
664 gettext_noop ("# HTTP peers active"),
672 * Removes a session from the linked list of sessions
673 * @param pc peer context
675 * @param call_msg_cont GNUNET_YES to call pending message continuations, otherwise no
676 * @param call_msg_cont_result result to call message continuations with
677 * @return GNUNET_SYSERR if msg not found, GNUNET_OK on success
680 remove_session (struct HTTP_PeerContext * pc,
683 int call_msg_cont_result)
685 struct HTTP_Message * msg;
686 struct Plugin * plugin = ps->peercontext->plugin;
688 #if DEBUG_CONNECTIONS
689 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
690 "Connection %X: removing %s session %X with id %u\n",
692 (ps->direction == INBOUND)
697 plugin->env->session_end(plugin, &pc->identity, ps);
698 GNUNET_free_non_null (ps->addr);
699 GNUNET_SERVER_mst_destroy (ps->msgtok);
700 GNUNET_free(ps->url);
701 if (ps->direction==INBOUND)
703 if (ps->recv_endpoint != NULL)
705 curl_easy_cleanup(ps->recv_endpoint);
706 ps->recv_endpoint = NULL;
708 if (ps->send_endpoint != NULL)
710 curl_easy_cleanup(ps->send_endpoint);
711 ps->send_endpoint = NULL;
715 msg = ps->pending_msgs_head;
718 if ((call_msg_cont == GNUNET_YES) && (msg->transmit_cont!=NULL))
720 msg->transmit_cont (msg->transmit_cont_cls,
722 call_msg_cont_result);
724 GNUNET_CONTAINER_DLL_remove(ps->pending_msgs_head,
725 ps->pending_msgs_head,
728 msg = ps->pending_msgs_head;
731 GNUNET_CONTAINER_DLL_remove(pc->head,pc->tail,ps);
735 /* no sessions left remove peer */
739 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
740 "No sessions left for peer `%s', removing context\n",
741 GNUNET_i2s(&pc->identity));
743 remove_peer_context_Iterator(plugin, &pc->identity.hashPubKey, pc);
749 static int check_localaddress (const struct sockaddr *addr, socklen_t addrlen)
752 int local = GNUNET_NO;
753 int af = addr->sa_family;
758 uint32_t netmask = 0x7F000000;
759 uint32_t address = ntohl (((struct sockaddr_in *) addr)->sin_addr.s_addr);
760 res = (address >> 24) ^ (netmask >> 24);
766 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
767 "Checking IPv4 address `%s': %s\n", GNUNET_a2s (addr, addrlen), (local==GNUNET_YES) ? "local" : "global");
773 if (IN6_IS_ADDR_LOOPBACK (&((struct sockaddr_in6 *) addr)->sin6_addr) ||
774 IN6_IS_ADDR_LINKLOCAL (&((struct sockaddr_in6 *) addr)->sin6_addr))
779 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
780 "Checking IPv6 address `%s' : %s\n", GNUNET_a2s (addr, addrlen), (local==GNUNET_YES) ? "local" : "global");
789 * Add the IP of our network interface to the list of
790 * our external IP addresses.
792 * @param cls the 'struct Plugin*'
793 * @param name name of the interface
794 * @param isDefault do we think this may be our default interface
795 * @param addr address of the interface
796 * @param addrlen number of bytes in addr
797 * @return GNUNET_OK to continue iterating
800 process_interfaces (void *cls,
803 const struct sockaddr *addr, socklen_t addrlen)
805 struct Plugin *plugin = cls;
806 struct IPv4HttpAddress * t4;
807 struct IPv6HttpAddress * t6;
810 if (plugin->use_localaddresses == GNUNET_NO)
812 if (GNUNET_YES == check_localaddress (addr, addrlen))
815 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
817 "Not notifying transport of address `%s' (local address)\n",
818 GNUNET_a2s (addr, addrlen));
825 GNUNET_assert(cls !=NULL);
826 af = addr->sa_family;
827 if ((af == AF_INET) &&
828 (plugin->use_ipv4 == GNUNET_YES) &&
829 (plugin->bind6_address == NULL) ) {
831 struct in_addr bnd_cmp = ((struct sockaddr_in *) addr)->sin_addr;
832 t4 = GNUNET_malloc(sizeof(struct IPv4HttpAddress));
833 // Not skipping loopback addresses
836 t4->ipv4_addr = ((struct sockaddr_in *) addr)->sin_addr.s_addr;
837 t4->u_port = htons (plugin->port_inbound);
838 if (plugin->bind4_address != NULL) {
839 if (0 == memcmp(&plugin->bind4_address->sin_addr, &bnd_cmp, sizeof (struct in_addr)))
841 GNUNET_CONTAINER_DLL_insert(plugin->ipv4_addr_head,
842 plugin->ipv4_addr_tail,t4);
843 plugin->env->notify_address(plugin->env->cls,
845 t4, sizeof (struct IPv4HttpAddress));
853 GNUNET_CONTAINER_DLL_insert (plugin->ipv4_addr_head,
854 plugin->ipv4_addr_tail,
856 plugin->env->notify_address(plugin->env->cls,
858 t4, sizeof (struct IPv4HttpAddress));
862 if ((af == AF_INET6) &&
863 (plugin->use_ipv6 == GNUNET_YES) &&
864 (plugin->bind4_address == NULL) ) {
866 struct in6_addr bnd_cmp6 = ((struct sockaddr_in6 *) addr)->sin6_addr;
868 t6 = GNUNET_malloc(sizeof(struct IPv6HttpAddress));
869 GNUNET_assert(t6 != NULL);
871 if (plugin->bind6_address != NULL) {
872 if (0 == memcmp(&plugin->bind6_address->sin6_addr,
874 sizeof (struct in6_addr))) {
875 memcpy (&t6->ipv6_addr,
876 &((struct sockaddr_in6 *) addr)->sin6_addr,
877 sizeof (struct in6_addr));
878 t6->u6_port = htons (plugin->port_inbound);
879 plugin->env->notify_address(plugin->env->cls,
881 t6, sizeof (struct IPv6HttpAddress));
882 GNUNET_CONTAINER_DLL_insert(plugin->ipv6_addr_head,
883 plugin->ipv6_addr_tail,
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 GNUNET_CONTAINER_DLL_insert(plugin->ipv6_addr_head,plugin->ipv6_addr_tail,t6);
896 plugin->env->notify_address(plugin->env->cls,
898 t6, sizeof (struct IPv6HttpAddress));
905 * External logging function for MHD
906 * @param arg arguments
907 * @param fmt format string
908 * @param ap list of arguments
911 mhd_logger (void * arg,
917 vsnprintf(text, sizeof(text), fmt, ap);
919 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
926 mhd_termination_cb (void *cls,
927 struct MHD_Connection * connection,
928 void **httpSessionCache)
930 struct Session * ps = *httpSessionCache;
933 struct HTTP_PeerContext * pc = ps->peercontext;
934 struct Plugin *plugin = cls;
936 GNUNET_assert (cls != NULL);
937 plugin->current_connections--;
939 if (connection==ps->recv_endpoint)
941 #if DEBUG_CONNECTIONS
942 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
943 "Connection %X: inbound connection from peer `%s' was terminated\n",
945 GNUNET_i2s(&pc->identity));
947 ps->recv_active = GNUNET_NO;
948 ps->recv_connected = GNUNET_NO;
949 ps->recv_endpoint = NULL;
951 if (connection==ps->send_endpoint)
953 ps->send_active = GNUNET_NO;
954 ps->send_connected = GNUNET_NO;
955 ps->send_endpoint = NULL;
956 #if DEBUG_CONNECTIONS
957 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
958 "Connection %X: outbound connection from peer `%s' was terminated\n",
960 GNUNET_i2s(&pc->identity));
964 /* if both connections disconnected, remove session */
965 if ( (ps->send_connected == GNUNET_NO) &&
966 (ps->recv_connected == GNUNET_NO) )
968 GNUNET_STATISTICS_update (pc->plugin->env->stats,
969 gettext_noop ("# HTTP inbound sessions for peers active"),
972 remove_session(pc,ps,GNUNET_YES,GNUNET_SYSERR);
978 * Callback called by MessageStreamTokenizer when a message has arrived
979 * @param cls current session as closure
980 * @param client clien
981 * @param message the message to be forwarded to transport service
984 mhd_write_mst_cb (void *cls,
986 const struct GNUNET_MessageHeader *message)
988 struct Session *ps = cls;
989 struct HTTP_PeerContext *pc = ps->peercontext;
990 struct GNUNET_TIME_Relative delay;
992 GNUNET_assert(ps != NULL);
993 GNUNET_assert(pc != NULL);
995 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
996 "Connection %X: Forwarding message to transport service, type %u and size %u from `%s' (`%s')\n",
998 ntohs(message->type),
999 ntohs(message->size),
1000 GNUNET_i2s(&(ps->peercontext)->identity),
1001 http_plugin_address_to_string(NULL,ps->addr,ps->addrlen));
1003 struct GNUNET_TRANSPORT_ATS_Information distance[2];
1004 distance[0].type = htonl (GNUNET_TRANSPORT_ATS_QUALITY_NET_DISTANCE);
1005 distance[0].value = htonl (1);
1006 distance[1].type = htonl (GNUNET_TRANSPORT_ATS_ARRAY_TERMINATOR);
1007 distance[1].value = htonl (0);
1008 delay = pc->plugin->env->receive (ps->peercontext->plugin->env->cls,
1011 (const struct GNUNET_TRANSPORT_ATS_Information *) &distance,
1017 if (pc->reset_task != GNUNET_SCHEDULER_NO_TASK)
1018 GNUNET_SCHEDULER_cancel (pc->reset_task);
1020 if (delay.rel_value > 0)
1023 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1024 "Connection %X: Inbound quota management: delay next read for %llu ms \n",
1028 pc->reset_task = GNUNET_SCHEDULER_add_delayed (delay, &reset_inbound_quota_delay, pc);
1034 * Check if incoming connection is accepted.
1035 * NOTE: Here every connection is accepted
1036 * @param cls plugin as closure
1037 * @param addr address of incoming connection
1038 * @param addr_len address length of incoming connection
1039 * @return MHD_YES if connection is accepted, MHD_NO if connection is rejected
1043 mhd_accept_cb (void *cls,
1044 const struct sockaddr *addr,
1047 struct Plugin *plugin = cls;
1048 GNUNET_assert (cls != NULL);
1050 if (plugin->max_connect_per_transport > plugin->current_connections)
1052 plugin->current_connections ++;
1060 * Callback called by MHD when it needs data to send
1061 * @param cls current session
1062 * @param pos position in buffer
1063 * @param buf the buffer to write data to
1064 * @param max max number of bytes available in buffer
1065 * @return bytes written to buffer
1068 mhd_send_callback (void *cls, uint64_t pos, char *buf, size_t max)
1070 struct Session * ps = cls;
1071 struct HTTP_PeerContext * pc;
1072 struct HTTP_Message * msg;
1075 GNUNET_assert (ps!=NULL);
1077 pc = ps->peercontext;
1078 msg = ps->pending_msgs_tail;
1079 if (ps->send_force_disconnect==GNUNET_YES)
1081 #if DEBUG_CONNECTIONS
1082 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1083 "Connection %X: outbound forced to disconnect\n",
1092 if ((msg->size-msg->pos) <= max)
1094 memcpy(buf,&msg->buf[msg->pos],(msg->size-msg->pos));
1095 bytes_read = msg->size-msg->pos;
1096 msg->pos+=(msg->size-msg->pos);
1100 memcpy(buf,&msg->buf[msg->pos],max);
1105 /* removing message */
1106 if (msg->pos==msg->size)
1108 if (NULL!=msg->transmit_cont)
1109 msg->transmit_cont (msg->transmit_cont_cls,&pc->identity,GNUNET_OK);
1110 ps->queue_length_cur -= msg->size;
1111 remove_http_message(ps,msg);
1114 #if DEBUG_CONNECTIONS
1115 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1116 "Connection %X: MHD has sent %u bytes\n",
1125 * Process GET or PUT request received via MHD. For
1126 * GET, queue response that will send back our pending
1127 * messages. For PUT, process incoming data and send
1128 * to GNUnet core. In either case, check if a session
1129 * already exists and create a new one if not.
1132 mhd_access_cb (void *cls,
1133 struct MHD_Connection *mhd_connection,
1136 const char *version,
1137 const char *upload_data,
1138 size_t * upload_data_size,
1139 void **httpSessionCache)
1141 struct Plugin *plugin = cls;
1142 struct MHD_Response *response;
1143 const union MHD_ConnectionInfo * conn_info;
1144 const struct sockaddr *client_addr;
1145 const struct sockaddr_in *addrin;
1146 const struct sockaddr_in6 *addrin6;
1147 char address[INET6_ADDRSTRLEN+14];
1148 struct GNUNET_PeerIdentity pi_in;
1150 struct IPv4HttpAddress ipv4addr;
1151 struct IPv6HttpAddress ipv6addr;
1152 struct HTTP_PeerContext *pc = NULL;
1153 struct Session *ps = NULL;
1154 struct Session *ps_tmp = NULL;
1155 int res = GNUNET_NO;
1157 size_t addr_len = 0 ;
1159 GNUNET_assert(cls !=NULL);
1161 if (NULL == *httpSessionCache)
1163 /* check url for peer identity , if invalid send HTTP 404*/
1164 size_t len = strlen(&url[1]);
1165 char * peer = GNUNET_malloc(104+1);
1167 if ( (len>104) && (url[104]==';'))
1169 char * id = GNUNET_malloc((len-104)+1);
1170 strcpy(id,&url[105]);
1171 memcpy(peer,&url[1],103);
1173 id_num = strtoul ( id, NULL , 10);
1176 res = GNUNET_CRYPTO_hash_from_string (peer, &(pi_in.hashPubKey));
1178 if ( GNUNET_SYSERR == res )
1180 response = MHD_create_response_from_data (strlen (HTTP_ERROR_RESPONSE),
1181 HTTP_ERROR_RESPONSE,
1183 res = MHD_queue_response (mhd_connection, MHD_HTTP_NOT_FOUND, response);
1184 MHD_destroy_response (response);
1185 #if DEBUG_CONNECTIONS
1187 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1188 "Peer has no valid ident, sent HTTP 1.1/404\n");
1190 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1191 "Peer has no valid ident, could not send error\n");
1198 ps = *httpSessionCache;
1199 pc = ps->peercontext;
1202 if (NULL == *httpSessionCache)
1204 /* get peer context */
1205 pc = GNUNET_CONTAINER_multihashmap_get (plugin->peers, &pi_in.hashPubKey);
1209 pc = GNUNET_malloc(sizeof (struct HTTP_PeerContext));
1210 pc->plugin = plugin;
1211 pc->session_id_counter=1;
1212 pc->last_session = NULL;
1213 memcpy(&pc->identity, &pi_in, sizeof(struct GNUNET_PeerIdentity));
1214 GNUNET_CONTAINER_multihashmap_put(plugin->peers,
1215 &pc->identity.hashPubKey,
1217 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
1218 GNUNET_STATISTICS_update (plugin->env->stats,
1219 gettext_noop ("# HTTP peers active"),
1224 conn_info = MHD_get_connection_info(mhd_connection, MHD_CONNECTION_INFO_CLIENT_ADDRESS );
1225 /* Incoming IPv4 connection */
1226 /* cast required for legacy MHD API < 0.9.6 */
1227 client_addr = (const struct sockaddr *) conn_info->client_addr;
1228 if ( AF_INET == client_addr->sa_family)
1230 addrin = (const struct sockaddr_in*) client_addr;
1231 inet_ntop(addrin->sin_family, &(addrin->sin_addr),address,INET_ADDRSTRLEN);
1232 memcpy(&ipv4addr.ipv4_addr,&(addrin->sin_addr),sizeof(struct in_addr));
1233 ipv4addr.u_port = addrin->sin_port;
1235 addr_len = sizeof(struct IPv4HttpAddress);
1237 /* Incoming IPv6 connection */
1238 if ( AF_INET6 == client_addr->sa_family)
1240 addrin6 = (const struct sockaddr_in6 *) client_addr;
1241 inet_ntop(addrin6->sin6_family, &(addrin6->sin6_addr),address,INET6_ADDRSTRLEN);
1242 memcpy(&ipv6addr.ipv6_addr,&(addrin6->sin6_addr),sizeof(struct in6_addr));
1243 ipv6addr.u6_port = addrin6->sin6_port;
1245 addr_len = sizeof(struct IPv6HttpAddress);
1248 GNUNET_assert (addr != NULL);
1249 GNUNET_assert (addr_len != 0);
1252 /* only inbound sessions here */
1255 while (ps_tmp!=NULL)
1257 if ((ps_tmp->direction==INBOUND) && (ps_tmp->session_id == id_num) && (id_num!=0))
1259 if ((ps_tmp->recv_force_disconnect!=GNUNET_YES) && (ps_tmp->send_force_disconnect!=GNUNET_YES))
1263 ps_tmp=ps_tmp->next;
1268 ps = GNUNET_malloc(sizeof (struct Session));
1269 ps->addr = GNUNET_malloc(addr_len);
1270 memcpy(ps->addr,addr,addr_len);
1271 ps->addrlen = addr_len;
1272 ps->direction=INBOUND;
1273 ps->pending_msgs_head = NULL;
1274 ps->pending_msgs_tail = NULL;
1275 ps->send_connected=GNUNET_NO;
1276 ps->send_active=GNUNET_NO;
1277 ps->recv_connected=GNUNET_NO;
1278 ps->recv_active=GNUNET_NO;
1280 ps->session_id =id_num;
1281 ps->queue_length_cur = 0;
1282 ps->queue_length_max = GNUNET_SERVER_MAX_MESSAGE_SIZE;
1283 ps->url = create_url (plugin, ps->addr, ps->addrlen, ps->session_id);
1284 GNUNET_CONTAINER_DLL_insert(pc->head,pc->tail,ps);
1285 GNUNET_STATISTICS_update (plugin->env->stats,
1286 gettext_noop ("# HTTP inbound sessions for peers active"),
1291 *httpSessionCache = ps;
1292 if (ps->msgtok==NULL)
1293 ps->msgtok = GNUNET_SERVER_mst_create (&mhd_write_mst_cb, ps);
1295 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1296 "Connection %X: HTTP Daemon has new an incoming `%s' request from peer `%s' (`%s')\n",
1299 GNUNET_i2s(&pc->identity),
1300 http_plugin_address_to_string(NULL, ps->addr, ps->addrlen));
1304 /* Is it a PUT or a GET request */
1305 if (0 == strcmp (MHD_HTTP_METHOD_PUT, method))
1307 if (ps->recv_force_disconnect == GNUNET_YES)
1309 #if DEBUG_CONNECTIONS
1310 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1311 "Connection %X: inbound connection was forced to disconnect\n",ps);
1313 ps->recv_active = GNUNET_NO;
1316 if ((*upload_data_size == 0) && (ps->recv_active==GNUNET_NO))
1318 ps->recv_endpoint = mhd_connection;
1319 ps->recv_connected = GNUNET_YES;
1320 ps->recv_active = GNUNET_YES;
1321 ps->recv_force_disconnect = GNUNET_NO;
1322 #if DEBUG_CONNECTIONS
1323 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1324 "Connection %X: inbound PUT connection connected\n",ps);
1329 /* Transmission of all data complete */
1330 if ((*upload_data_size == 0) && (ps->recv_active == GNUNET_YES))
1332 response = MHD_create_response_from_data (strlen (HTTP_PUT_RESPONSE),
1335 res = MHD_queue_response (mhd_connection, MHD_HTTP_OK, response);
1336 #if DEBUG_CONNECTIONS
1337 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1338 "Connection %X: Sent HTTP/1.1: 200 OK as PUT Response\n",
1341 MHD_destroy_response (response);
1342 ps->recv_active=GNUNET_NO;
1346 /* Recieving data */
1347 if ((*upload_data_size > 0) && (ps->recv_active == GNUNET_YES))
1349 if (pc->delay.rel_value == 0)
1352 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1353 "Connection %X: PUT with %u bytes forwarded to MST\n",
1354 ps, *upload_data_size);
1356 res = GNUNET_SERVER_mst_receive(ps->msgtok, ps,
1357 upload_data, *upload_data_size,
1358 GNUNET_NO, GNUNET_NO);
1359 (*upload_data_size) = 0;
1364 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1365 "Connection %X: no inbound bandwidth available! Next read was delayed for %llu ms\n",
1367 ps->peercontext->delay.rel_value);
1375 if ( 0 == strcmp (MHD_HTTP_METHOD_GET, method) )
1377 if (ps->send_force_disconnect == GNUNET_YES)
1379 #if DEBUG_CONNECTIONS
1380 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1381 "Connection %X: outbound connection was forced to disconnect\n",
1384 ps->send_active = GNUNET_NO;
1387 ps->send_connected = GNUNET_YES;
1388 ps->send_active = GNUNET_YES;
1389 ps->send_endpoint = mhd_connection;
1390 ps->send_force_disconnect = GNUNET_NO;
1391 #if DEBUG_CONNECTIONS
1392 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1393 "Connection %X: inbound GET connection connected\n",
1396 response = MHD_create_response_from_callback(-1,32 * 1024, &mhd_send_callback, ps, NULL);
1397 res = MHD_queue_response (mhd_connection, MHD_HTTP_OK, response);
1398 MHD_destroy_response (response);
1406 * Function that queries MHD's select sets and
1407 * starts the task waiting for them.
1408 * @param plugin plugin
1409 * @param daemon_handle the MHD daemon handle
1410 * @return gnunet task identifier
1412 static GNUNET_SCHEDULER_TaskIdentifier
1413 http_server_daemon_prepare (struct Plugin *plugin,
1414 struct MHD_Daemon *daemon_handle)
1416 GNUNET_SCHEDULER_TaskIdentifier ret;
1420 struct GNUNET_NETWORK_FDSet *wrs;
1421 struct GNUNET_NETWORK_FDSet *wws;
1422 struct GNUNET_NETWORK_FDSet *wes;
1424 unsigned long long timeout;
1426 struct GNUNET_TIME_Relative tv;
1428 ret = GNUNET_SCHEDULER_NO_TASK;
1432 wrs = GNUNET_NETWORK_fdset_create ();
1433 wes = GNUNET_NETWORK_fdset_create ();
1434 wws = GNUNET_NETWORK_fdset_create ();
1436 GNUNET_assert (MHD_YES ==
1437 MHD_get_fdset (daemon_handle,
1442 haveto = MHD_get_timeout (daemon_handle, &timeout);
1443 if (haveto == MHD_YES)
1444 tv.rel_value = (uint64_t) timeout;
1446 tv = GNUNET_TIME_UNIT_SECONDS;
1447 GNUNET_NETWORK_fdset_copy_native (wrs, &rs, max + 1);
1448 GNUNET_NETWORK_fdset_copy_native (wws, &ws, max + 1);
1449 GNUNET_NETWORK_fdset_copy_native (wes, &es, max + 1);
1450 if (daemon_handle == plugin->http_server_daemon_v4)
1452 if (plugin->http_server_task_v4 != GNUNET_SCHEDULER_NO_TASK)
1454 GNUNET_SCHEDULER_cancel(plugin->http_server_task_v4);
1455 plugin->http_server_daemon_v4 = GNUNET_SCHEDULER_NO_TASK;
1458 ret = GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1459 GNUNET_SCHEDULER_NO_TASK,
1463 &http_server_daemon_v4_run,
1466 if (daemon_handle == plugin->http_server_daemon_v6)
1468 if (plugin->http_server_task_v6 != GNUNET_SCHEDULER_NO_TASK)
1470 GNUNET_SCHEDULER_cancel(plugin->http_server_task_v6);
1471 plugin->http_server_task_v6 = GNUNET_SCHEDULER_NO_TASK;
1474 ret = GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1475 GNUNET_SCHEDULER_NO_TASK,
1479 &http_server_daemon_v6_run,
1482 GNUNET_NETWORK_fdset_destroy (wrs);
1483 GNUNET_NETWORK_fdset_destroy (wws);
1484 GNUNET_NETWORK_fdset_destroy (wes);
1490 * Call MHD IPv4 to process pending requests and then go back
1491 * and schedule the next run.
1492 * @param cls plugin as closure
1493 * @param tc task context
1496 http_server_daemon_v4_run (void *cls,
1497 const struct GNUNET_SCHEDULER_TaskContext *tc)
1499 struct Plugin *plugin = cls;
1501 #if DEBUG_SCHEDULING
1502 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_READ_READY))
1503 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1504 "http_server_daemon_v4_run: GNUNET_SCHEDULER_REASON_READ_READY\n");
1505 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_WRITE_READY))
1506 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1507 "http_server_daemon_v4_run: GNUNET_SCHEDULER_REASON_WRITE_READY\n");
1508 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_TIMEOUT))
1509 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1510 "http_server_daemon_v4_run: GNUNET_SCHEDULER_REASON_TIMEOUT\n");
1511 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_STARTUP))
1512 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1513 "http_server_daemon_v4_run: GGNUNET_SCHEDULER_REASON_STARTUP\n");
1514 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1515 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1516 "http_server_daemon_v4_run: GGNUNET_SCHEDULER_REASON_SHUTDOWN\n");
1519 GNUNET_assert(cls !=NULL);
1520 plugin->http_server_task_v4 = GNUNET_SCHEDULER_NO_TASK;
1522 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1525 GNUNET_assert (MHD_YES == MHD_run (plugin->http_server_daemon_v4));
1526 plugin->http_server_task_v4 = http_server_daemon_prepare (plugin, plugin->http_server_daemon_v4);
1531 * Call MHD IPv6 to process pending requests and then go back
1532 * and schedule the next run.
1533 * @param cls plugin as closure
1534 * @param tc task context
1537 http_server_daemon_v6_run (void *cls,
1538 const struct GNUNET_SCHEDULER_TaskContext *tc)
1540 struct Plugin *plugin = cls;
1542 #if DEBUG_SCHEDULING
1543 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_READ_READY))
1544 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1545 "http_server_daemon_v6_run: GNUNET_SCHEDULER_REASON_READ_READY\n");
1546 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_WRITE_READY))
1547 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1548 "http_server_daemon_v6_run: GNUNET_SCHEDULER_REASON_WRITE_READY\n");
1549 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_TIMEOUT))
1550 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1551 "http_server_daemon_v6_run: GNUNET_SCHEDULER_REASON_TIMEOUT\n");
1552 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_STARTUP))
1553 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1554 "http_server_daemon_v6_run: GGNUNET_SCHEDULER_REASON_STARTUP\n");
1555 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1556 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1557 "http_server_daemon_v6_run: GGNUNET_SCHEDULER_REASON_SHUTDOWN\n");
1560 GNUNET_assert(cls !=NULL);
1561 plugin->http_server_task_v6 = GNUNET_SCHEDULER_NO_TASK;
1563 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1566 GNUNET_assert (MHD_YES == MHD_run (plugin->http_server_daemon_v6));
1567 plugin->http_server_task_v6 = http_server_daemon_prepare (plugin, plugin->http_server_daemon_v6);
1572 curl_get_header_cb( void *ptr,
1573 size_t size, size_t nmemb,
1576 struct Session * ps = stream;
1578 long http_result = 0;
1580 /* Getting last http result code */
1581 GNUNET_assert(NULL!=ps);
1582 if (ps->recv_connected==GNUNET_NO)
1584 res = curl_easy_getinfo(ps->recv_endpoint, CURLINFO_RESPONSE_CODE, &http_result);
1585 if (CURLE_OK == res)
1587 if (http_result == 200)
1589 ps->recv_connected = GNUNET_YES;
1590 ps->recv_active = GNUNET_YES;
1591 #if DEBUG_CONNECTIONS
1592 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: connected to recieve data\n",ps);
1594 // Calling send_check_connections again since receive is established
1595 send_check_connections (ps->peercontext->plugin, ps);
1602 size_t len = size * nmemb;
1604 if ((size * nmemb) < SIZE_MAX)
1605 tmp = GNUNET_malloc (len+1);
1607 if ((tmp != NULL) && (len > 0))
1609 memcpy(tmp,ptr,len);
1612 if (tmp[len-2] == 13)
1615 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1616 "Connection %X: Header: %s\n",
1619 GNUNET_free_non_null (tmp);
1622 return size * nmemb;
1627 * Callback called by libcurl when new headers arrive
1628 * Used to get HTTP result for curl operations
1629 * @param ptr stream to read from
1630 * @param size size of one char element
1631 * @param nmemb number of char elements
1632 * @param stream closure set by user
1633 * @return bytes read by function
1636 curl_put_header_cb(void *ptr,
1641 struct Session * ps = stream;
1644 size_t len = size * nmemb;
1645 long http_result = 0;
1648 /* Getting last http result code */
1649 GNUNET_assert(NULL!=ps);
1650 res = curl_easy_getinfo (ps->send_endpoint, CURLINFO_RESPONSE_CODE, &http_result);
1651 if (CURLE_OK == res)
1653 if ((http_result == 100) && (ps->send_connected==GNUNET_NO))
1655 ps->send_connected = GNUNET_YES;
1656 ps->send_active = GNUNET_YES;
1657 #if DEBUG_CONNECTIONS
1658 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1659 "Connection %X: connected to send data\n",
1663 if ((http_result == 200) && (ps->send_connected==GNUNET_YES))
1665 ps->send_connected = GNUNET_NO;
1666 ps->send_active = GNUNET_NO;
1667 #if DEBUG_CONNECTIONS
1668 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1669 "Connection %X: sending disconnected\n",
1676 if ((size * nmemb) < SIZE_MAX)
1677 tmp = GNUNET_malloc (len+1);
1679 if ((tmp != NULL) && (len > 0))
1681 memcpy(tmp,ptr,len);
1684 if (tmp[len-2] == 13)
1688 GNUNET_free_non_null (tmp);
1689 return size * nmemb;
1693 * Callback method used with libcurl
1694 * Method is called when libcurl needs to read data during sending
1695 * @param stream pointer where to write data
1696 * @param size size of an individual element
1697 * @param nmemb count of elements that can be written to the buffer
1698 * @param ptr source pointer, passed to the libcurl handle
1699 * @return bytes written to stream
1702 curl_send_cb(void *stream,
1703 size_t size, size_t nmemb,
1706 struct Session * ps = ptr;
1707 struct HTTP_Message * msg = ps->pending_msgs_tail;
1711 if (ps->send_active == GNUNET_NO)
1712 return CURL_READFUNC_PAUSE;
1713 if ( (ps->pending_msgs_tail == NULL) &&
1714 (ps->send_active == GNUNET_YES) )
1716 #if DEBUG_CONNECTIONS
1717 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1718 "Connection %X: No Message to send, pausing connection\n",
1721 ps->send_active = GNUNET_NO;
1722 return CURL_READFUNC_PAUSE;
1725 GNUNET_assert (msg!=NULL);
1728 if (msg->pos < msg->size)
1730 /* data fit in buffer */
1731 if ((msg->size - msg->pos) <= (size * nmemb))
1733 len = (msg->size - msg->pos);
1734 memcpy(stream, &msg->buf[msg->pos], len);
1741 memcpy(stream, &msg->buf[msg->pos], len);
1746 /* no data to send */
1752 if ( msg->pos == msg->size)
1754 #if DEBUG_CONNECTIONS
1755 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1756 "Connection %X: Message with %u bytes sent, removing message from queue\n",
1760 /* Calling transmit continuation */
1761 if (NULL != ps->pending_msgs_tail->transmit_cont)
1762 msg->transmit_cont (ps->pending_msgs_tail->transmit_cont_cls,
1763 &(ps->peercontext)->identity,
1765 ps->queue_length_cur -= msg->size;
1766 remove_http_message(ps, msg);
1773 curl_receive_mst_cb (void *cls,
1775 const struct GNUNET_MessageHeader *message)
1777 struct Session *ps = cls;
1778 struct GNUNET_TIME_Relative delay;
1779 GNUNET_assert(ps != NULL);
1781 struct HTTP_PeerContext *pc = ps->peercontext;
1782 GNUNET_assert(pc != NULL);
1784 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1785 "Connection %X: Forwarding message to transport service, type %u and size %u from `%s' (`%s')\n",
1787 ntohs(message->type),
1788 ntohs(message->size),
1789 GNUNET_i2s(&(pc->identity)),
1790 http_plugin_address_to_string(NULL,ps->addr,ps->addrlen));
1792 struct GNUNET_TRANSPORT_ATS_Information distance[2];
1793 distance[0].type = htonl (GNUNET_TRANSPORT_ATS_QUALITY_NET_DISTANCE);
1794 distance[0].value = htonl (1);
1795 distance[1].type = htonl (GNUNET_TRANSPORT_ATS_ARRAY_TERMINATOR);
1796 distance[1].value = htonl (0);
1798 delay = pc->plugin->env->receive (pc->plugin->env->cls,
1801 (const struct GNUNET_TRANSPORT_ATS_Information *) &distance, 2,
1806 if (pc->reset_task != GNUNET_SCHEDULER_NO_TASK)
1807 GNUNET_SCHEDULER_cancel (pc->reset_task);
1809 if (delay.rel_value > 0)
1812 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1813 "Connection %X: Inbound quota management: delay next read for %llu ms\n",
1814 ps, delay.rel_value);
1816 pc->reset_task = 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,
1846 "Connection %X: %u bytes received\n",
1849 GNUNET_SERVER_mst_receive(ps->msgtok, ps,
1851 GNUNET_NO, GNUNET_NO);
1852 return (size * nmemb);
1857 curl_handle_finished (struct Plugin *plugin)
1859 struct Session *ps = NULL;
1860 struct HTTP_PeerContext *pc = NULL;
1861 struct CURLMsg *msg;
1862 struct HTTP_Message * cur_msg = NULL;
1869 msg = curl_multi_info_read (plugin->multi_handle, &msgs_in_queue);
1870 if ((msgs_in_queue == 0) || (msg == NULL))
1872 /* get session for affected curl handle */
1873 GNUNET_assert ( msg->easy_handle != NULL );
1874 curl_easy_getinfo(msg->easy_handle, CURLINFO_PRIVATE, &tmp);
1875 ps = (struct Session *) tmp;
1876 GNUNET_assert ( ps != NULL );
1877 pc = ps->peercontext;
1878 GNUNET_assert ( pc != NULL );
1882 if ( (msg->data.result != CURLE_OK) &&
1883 (msg->data.result != CURLE_GOT_NOTHING) )
1885 /* sending msg failed*/
1886 if (msg->easy_handle == ps->send_endpoint)
1888 #if DEBUG_CONNECTIONS
1889 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1890 _("Connection %X: HTTP PUT to peer `%s' (`%s') failed: `%s' `%s'\n"),
1892 GNUNET_i2s(&pc->identity),
1893 http_plugin_address_to_string(NULL, ps->addr, ps->addrlen),
1894 "curl_multi_perform",
1895 curl_easy_strerror (msg->data.result));
1897 ps->send_connected = GNUNET_NO;
1898 ps->send_active = GNUNET_NO;
1899 curl_multi_remove_handle(plugin->multi_handle,ps->send_endpoint);
1900 while (ps->pending_msgs_tail != NULL)
1902 cur_msg = ps->pending_msgs_tail;
1903 if ( NULL != cur_msg->transmit_cont)
1904 cur_msg->transmit_cont (cur_msg->transmit_cont_cls,&pc->identity,GNUNET_SYSERR);
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,
1914 _("Connection %X: HTTP GET to peer `%s' (`%s') failed: `%s' `%s'\n"),
1916 GNUNET_i2s(&pc->identity),
1917 http_plugin_address_to_string(NULL, ps->addr, ps->addrlen),
1918 "curl_multi_perform",
1919 curl_easy_strerror (msg->data.result));
1921 ps->recv_connected = GNUNET_NO;
1922 ps->recv_active = GNUNET_NO;
1923 curl_multi_remove_handle(plugin->multi_handle,ps->recv_endpoint);
1928 if (msg->easy_handle == ps->send_endpoint)
1930 GNUNET_assert (CURLE_OK == curl_easy_getinfo(msg->easy_handle, CURLINFO_RESPONSE_CODE, &http_result));
1931 #if DEBUG_CONNECTIONS
1932 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1933 "Connection %X: HTTP PUT connection to peer `%s' (`%s') was closed with HTTP code %u\n",
1935 GNUNET_i2s(&pc->identity),
1936 http_plugin_address_to_string(NULL, ps->addr, ps->addrlen),
1939 /* Calling transmit continuation */
1940 while (ps->pending_msgs_tail != NULL)
1942 cur_msg = ps->pending_msgs_tail;
1943 if ( NULL != cur_msg->transmit_cont)
1945 /* HTTP 1xx : Last message before here was informational */
1946 if ((http_result >=100) && (http_result < 200))
1947 cur_msg->transmit_cont (cur_msg->transmit_cont_cls,&pc->identity,GNUNET_OK);
1948 /* HTTP 2xx: successful operations */
1949 if ((http_result >=200) && (http_result < 300))
1950 cur_msg->transmit_cont (cur_msg->transmit_cont_cls,&pc->identity,GNUNET_OK);
1951 /* HTTP 3xx..5xx: error */
1952 if ((http_result >=300) && (http_result < 600))
1953 cur_msg->transmit_cont (cur_msg->transmit_cont_cls,&pc->identity,GNUNET_SYSERR);
1955 ps->queue_length_cur -= cur_msg->size;
1956 remove_http_message(ps,cur_msg);
1959 ps->send_connected = GNUNET_NO;
1960 ps->send_active = GNUNET_NO;
1961 curl_multi_remove_handle(plugin->multi_handle,ps->send_endpoint);
1963 if (msg->easy_handle == ps->recv_endpoint)
1965 #if DEBUG_CONNECTIONS
1966 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1967 "Connection %X: HTTP GET connection to peer `%s' (`%s') was closed with HTTP code %u\n",
1969 GNUNET_i2s(&pc->identity),
1970 http_plugin_address_to_string(NULL, ps->addr, ps->addrlen),
1973 ps->recv_connected = GNUNET_NO;
1974 ps->recv_active = GNUNET_NO;
1975 curl_multi_remove_handle(plugin->multi_handle,ps->recv_endpoint);
1977 plugin->current_connections--;
1979 if ((ps->recv_connected == GNUNET_NO) && (ps->send_connected == GNUNET_NO))
1980 remove_session (pc, ps, GNUNET_YES, GNUNET_SYSERR);
1986 while ( (msgs_in_queue > 0) );
1991 * Task performing curl operations
1992 * @param cls plugin as closure
1993 * @param tc gnunet scheduler task context
1995 static void curl_perform (void *cls,
1996 const struct GNUNET_SCHEDULER_TaskContext *tc)
1998 struct Plugin *plugin = cls;
1999 static unsigned int handles_last_run;
2003 GNUNET_assert(cls !=NULL);
2005 plugin->http_curl_task = GNUNET_SCHEDULER_NO_TASK;
2006 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
2011 mret = curl_multi_perform (plugin->multi_handle, &running);
2012 if ((running < handles_last_run) && (running>0))
2013 curl_handle_finished(plugin);
2014 handles_last_run = running;
2016 while (mret == CURLM_CALL_MULTI_PERFORM);
2017 curl_schedule(plugin);
2022 * Function setting up file descriptors and scheduling task to run
2024 * @param plugin plugin as closure
2025 * @return GNUNET_SYSERR for hard failure, GNUNET_OK for ok
2028 curl_schedule(struct Plugin *plugin)
2034 struct GNUNET_NETWORK_FDSet *grs;
2035 struct GNUNET_NETWORK_FDSet *gws;
2039 /* Cancel previous scheduled task */
2040 if (plugin->http_curl_task != GNUNET_SCHEDULER_NO_TASK)
2042 GNUNET_SCHEDULER_cancel(plugin->http_curl_task);
2043 plugin->http_curl_task = GNUNET_SCHEDULER_NO_TASK;
2050 mret = curl_multi_fdset (plugin->multi_handle, &rs, &ws, &es, &max);
2051 if (mret != CURLM_OK)
2053 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2054 _("%s failed at %s:%d: `%s'\n"),
2055 "curl_multi_fdset", __FILE__, __LINE__,
2056 curl_multi_strerror (mret));
2057 return GNUNET_SYSERR;
2059 mret = curl_multi_timeout (plugin->multi_handle, &to);
2060 if (mret != CURLM_OK)
2062 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2063 _("%s failed at %s:%d: `%s'\n"),
2064 "curl_multi_timeout", __FILE__, __LINE__,
2065 curl_multi_strerror (mret));
2066 return GNUNET_SYSERR;
2069 grs = GNUNET_NETWORK_fdset_create ();
2070 gws = GNUNET_NETWORK_fdset_create ();
2071 GNUNET_NETWORK_fdset_copy_native (grs, &rs, max + 1);
2072 GNUNET_NETWORK_fdset_copy_native (gws, &ws, max + 1);
2073 plugin->http_curl_task = GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
2074 GNUNET_SCHEDULER_NO_TASK,
2076 ? GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
2077 : GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, to),
2082 GNUNET_NETWORK_fdset_destroy (gws);
2083 GNUNET_NETWORK_fdset_destroy (grs);
2090 * Function to log curl debug messages with GNUNET_log
2091 * @param curl handle
2092 * @param type curl_infotype
2095 * @param cls closure
2099 curl_logger (CURL * curl,
2101 char * data, size_t size,
2104 if (type == CURLINFO_TEXT)
2107 memcpy(text,data,size);
2108 if (text[size-1] == '\n')
2113 text[size+1] = '\0';
2115 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2116 "CURL: Connection %X - %s",
2126 * Function setting up curl handle and selecting message to send
2128 * @param plugin plugin
2130 * @return GNUNET_SYSERR on failure, GNUNET_NO if connecting, GNUNET_YES if ok
2133 send_check_connections (struct Plugin *plugin,
2137 struct GNUNET_TIME_Relative timeout = GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT;
2139 if ((ps->direction == OUTBOUND) && (plugin->current_connections < plugin->max_connect_per_transport))
2141 /* RECV DIRECTION */
2142 /* Check if session is connected to receive data, otherwise connect to peer */
2144 if (ps->recv_connected == GNUNET_NO)
2146 int fresh = GNUNET_NO;
2147 if (ps->recv_endpoint == NULL)
2150 ps->recv_endpoint = curl_easy_init();
2153 curl_easy_setopt(ps->recv_endpoint, CURLOPT_VERBOSE, 1L);
2154 curl_easy_setopt(ps->recv_endpoint, CURLOPT_DEBUGFUNCTION , &curl_logger);
2155 curl_easy_setopt(ps->recv_endpoint, CURLOPT_DEBUGDATA , ps->recv_endpoint);
2158 curl_easy_setopt(ps->recv_endpoint, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1);
2159 curl_easy_setopt(ps->recv_endpoint, CURLOPT_SSL_VERIFYPEER, 0);
2160 curl_easy_setopt(ps->recv_endpoint, CURLOPT_SSL_VERIFYHOST, 0);
2162 curl_easy_setopt(ps->recv_endpoint, CURLOPT_URL, ps->url);
2163 curl_easy_setopt(ps->recv_endpoint, CURLOPT_HEADERFUNCTION, &curl_get_header_cb);
2164 curl_easy_setopt(ps->recv_endpoint, CURLOPT_WRITEHEADER, ps);
2165 curl_easy_setopt(ps->recv_endpoint, CURLOPT_READFUNCTION, curl_send_cb);
2166 curl_easy_setopt(ps->recv_endpoint, CURLOPT_READDATA, ps);
2167 curl_easy_setopt(ps->recv_endpoint, CURLOPT_WRITEFUNCTION, curl_receive_cb);
2168 curl_easy_setopt(ps->recv_endpoint, CURLOPT_WRITEDATA, ps);
2169 curl_easy_setopt(ps->recv_endpoint, CURLOPT_TIMEOUT, (long) timeout.rel_value);
2170 curl_easy_setopt(ps->recv_endpoint, CURLOPT_PRIVATE, ps);
2171 curl_easy_setopt(ps->recv_endpoint, CURLOPT_CONNECTTIMEOUT, HTTP_CONNECT_TIMEOUT);
2172 curl_easy_setopt(ps->recv_endpoint, CURLOPT_BUFFERSIZE, 2*GNUNET_SERVER_MAX_MESSAGE_SIZE);
2173 #if CURL_TCP_NODELAY
2174 curl_easy_setopt(ps->recv_endpoint, CURLOPT_TCP_NODELAY, 1);
2176 if (fresh==GNUNET_YES)
2178 mret = curl_multi_add_handle(plugin->multi_handle, ps->recv_endpoint);
2179 if (mret != CURLM_OK)
2181 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2182 _("Connection: %X: %s failed at %s:%d: `%s'\n"),
2184 "curl_multi_add_handle", __FILE__, __LINE__,
2185 curl_multi_strerror (mret));
2186 return GNUNET_SYSERR;
2189 if (plugin->http_curl_task != GNUNET_SCHEDULER_NO_TASK)
2191 GNUNET_SCHEDULER_cancel(plugin->http_curl_task);
2192 plugin->http_curl_task = GNUNET_SCHEDULER_NO_TASK;
2194 plugin->current_connections ++;
2195 plugin->http_curl_task = GNUNET_SCHEDULER_add_now (&curl_perform, plugin);
2198 /* waiting for receive direction */
2199 if (ps->recv_connected==GNUNET_NO)
2202 /* SEND DIRECTION */
2203 /* Check if session is connected to send data, otherwise connect to peer */
2204 if ((ps->send_connected == GNUNET_YES) && (ps->send_endpoint!= NULL))
2206 if (ps->send_active == GNUNET_YES)
2208 #if DEBUG_CONNECTIONS
2209 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2210 "Connection %X: outbound active, enqueueing message\n",
2215 if (ps->send_active == GNUNET_NO)
2217 #if DEBUG_CONNECTIONS
2218 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2219 "Connection %X: outbound paused, unpausing existing connection and enqueueing message\n",
2222 if (CURLE_OK == curl_easy_pause(ps->send_endpoint,CURLPAUSE_CONT))
2224 ps->send_active=GNUNET_YES;
2225 if (plugin->http_curl_task != GNUNET_SCHEDULER_NO_TASK)
2227 GNUNET_SCHEDULER_cancel(plugin->http_curl_task);
2228 plugin->http_curl_task = GNUNET_SCHEDULER_NO_TASK;
2230 plugin->http_curl_task = GNUNET_SCHEDULER_add_now (&curl_perform, plugin);
2234 return GNUNET_SYSERR;
2237 /* not connected, initiate connection */
2238 if ((ps->send_connected==GNUNET_NO) && (plugin->current_connections < plugin->max_connect_per_transport))
2240 int fresh = GNUNET_NO;
2241 if (NULL == ps->send_endpoint)
2243 ps->send_endpoint = curl_easy_init();
2246 GNUNET_assert (ps->send_endpoint != NULL);
2247 GNUNET_assert (NULL != ps->pending_msgs_tail);
2248 #if DEBUG_CONNECTIONS
2249 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2250 "Connection %X: outbound not connected, initiating connection\n",
2253 ps->send_active = GNUNET_NO;
2256 curl_easy_setopt(ps->send_endpoint, CURLOPT_VERBOSE, 1L);
2257 curl_easy_setopt(ps->send_endpoint, CURLOPT_DEBUGFUNCTION , &curl_logger);
2258 curl_easy_setopt(ps->send_endpoint, CURLOPT_DEBUGDATA , ps->send_endpoint);
2261 curl_easy_setopt (ps->send_endpoint, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1);
2262 curl_easy_setopt(ps->send_endpoint, CURLOPT_SSL_VERIFYPEER, 0);
2263 curl_easy_setopt(ps->send_endpoint, CURLOPT_SSL_VERIFYHOST, 0);
2265 curl_easy_setopt(ps->send_endpoint, CURLOPT_URL, ps->url);
2266 curl_easy_setopt(ps->send_endpoint, CURLOPT_PUT, 1L);
2267 curl_easy_setopt(ps->send_endpoint, CURLOPT_HEADERFUNCTION, &curl_put_header_cb);
2268 curl_easy_setopt(ps->send_endpoint, CURLOPT_WRITEHEADER, ps);
2269 curl_easy_setopt(ps->send_endpoint, CURLOPT_READFUNCTION, curl_send_cb);
2270 curl_easy_setopt(ps->send_endpoint, CURLOPT_READDATA, ps);
2271 curl_easy_setopt(ps->send_endpoint, CURLOPT_WRITEFUNCTION, curl_receive_cb);
2272 curl_easy_setopt(ps->send_endpoint, CURLOPT_READDATA, ps);
2273 curl_easy_setopt(ps->send_endpoint, CURLOPT_TIMEOUT, (long) timeout.rel_value);
2274 curl_easy_setopt(ps->send_endpoint, CURLOPT_PRIVATE, ps);
2275 curl_easy_setopt(ps->send_endpoint, CURLOPT_CONNECTTIMEOUT, HTTP_CONNECT_TIMEOUT);
2276 curl_easy_setopt(ps->send_endpoint, CURLOPT_BUFFERSIZE, 2 * GNUNET_SERVER_MAX_MESSAGE_SIZE);
2277 #if CURL_TCP_NODELAY
2278 curl_easy_setopt(ps->send_endpoint, CURLOPT_TCP_NODELAY, 1);
2280 if (fresh==GNUNET_YES)
2282 mret = curl_multi_add_handle(plugin->multi_handle, ps->send_endpoint);
2283 if (mret != CURLM_OK)
2285 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2286 _("Connection: %X: %s failed at %s:%d: `%s'\n"),
2288 "curl_multi_add_handle", __FILE__, __LINE__,
2289 curl_multi_strerror (mret));
2290 return GNUNET_SYSERR;
2294 if (plugin->http_curl_task != GNUNET_SCHEDULER_NO_TASK)
2296 GNUNET_SCHEDULER_cancel(plugin->http_curl_task);
2297 plugin->http_curl_task = GNUNET_SCHEDULER_NO_TASK;
2299 plugin->current_connections++;
2300 plugin->http_curl_task = GNUNET_SCHEDULER_add_now (&curl_perform, plugin);
2303 if (ps->direction == INBOUND)
2305 GNUNET_assert (NULL != ps->pending_msgs_tail);
2306 if ((ps->recv_connected==GNUNET_YES) && (ps->send_connected==GNUNET_YES) &&
2307 (ps->recv_force_disconnect==GNUNET_NO) && (ps->recv_force_disconnect==GNUNET_NO))
2310 return GNUNET_SYSERR;
2315 * select best session to transmit data to peer
2317 * @param pc peer context of target peer
2318 * @param addr address of target peer
2319 * @param addrlen address length
2320 * @param force_address does transport service enforce address?
2321 * @param session session passed by transport service
2322 * @return selected session
2325 static struct Session *
2326 send_select_session (struct HTTP_PeerContext *pc,
2327 const void * addr, size_t addrlen,
2329 struct Session * session)
2331 struct Session * tmp = NULL;
2332 int addr_given = GNUNET_NO;
2334 if ((addr!=NULL) && (addrlen>0))
2335 addr_given = GNUNET_YES;
2337 if (force_address == GNUNET_YES)
2339 /* check session given as argument */
2340 if ((session != NULL) && (addr_given == GNUNET_YES))
2342 if (0 == memcmp(session->addr, addr, addrlen))
2344 /* connection can not be used, since it is disconnected */
2345 if ( (session->recv_force_disconnect==GNUNET_NO) &&
2346 (session->send_force_disconnect==GNUNET_NO) )
2348 #if DEBUG_SESSION_SELECTION
2349 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2350 "Session %X selected: Using session passed by transport to send to forced address \n",
2357 /* check last session used */
2358 if ((pc->last_session != NULL)&& (addr_given == GNUNET_YES))
2360 if (0 == memcmp(pc->last_session->addr, addr, addrlen))
2362 /* connection can not be used, since it is disconnected */
2363 if ( (pc->last_session->recv_force_disconnect==GNUNET_NO) &&
2364 (pc->last_session->send_force_disconnect==GNUNET_NO) )
2366 #if DEBUG_SESSION_SELECTION
2367 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2368 "Session %X selected: Using last session used to send to forced address \n",
2371 return pc->last_session;
2375 /* find session in existing sessions */
2377 while ((tmp!=NULL) && (addr_given == GNUNET_YES))
2379 if (0 == memcmp(tmp->addr, addr, addrlen))
2381 /* connection can not be used, since it is disconnected */
2382 if ( (tmp->recv_force_disconnect==GNUNET_NO) &&
2383 (tmp->send_force_disconnect==GNUNET_NO) )
2385 #if DEBUG_SESSION_SELECTION
2386 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2387 "Session %X selected: Using existing session to send to forced address \n",
2395 /* no session to use */
2398 if ((force_address == GNUNET_NO) || (force_address == GNUNET_SYSERR))
2400 /* check session given as argument */
2401 if (session != NULL)
2403 /* connection can not be used, since it is disconnected */
2404 if ( (session->recv_force_disconnect==GNUNET_NO) &&
2405 (session->send_force_disconnect==GNUNET_NO) )
2407 #if DEBUG_SESSION_SELECTION
2408 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2409 "Session %X selected: Using session passed by transport to send not-forced address\n",
2415 /* check last session used */
2416 if (pc->last_session != NULL)
2418 /* connection can not be used, since it is disconnected */
2419 if ( (pc->last_session->recv_force_disconnect==GNUNET_NO) &&
2420 (pc->last_session->send_force_disconnect==GNUNET_NO) )
2422 #if DEBUG_SESSION_SELECTION
2423 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2424 "Session %X selected: Using last session to send to not-forced address\n",
2427 return pc->last_session;
2430 /* find session in existing sessions */
2434 /* connection can not be used, since it is disconnected */
2435 if ( (tmp->recv_force_disconnect==GNUNET_NO) &&
2436 (tmp->send_force_disconnect==GNUNET_NO) )
2438 #if DEBUG_SESSION_SELECTION
2439 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2440 "Session %X selected: Using existing session to send to not-forced address\n",
2454 * Function that can be used by the transport service to transmit
2455 * a message using the plugin. Note that in the case of a
2456 * peer disconnecting, the continuation MUST be called
2457 * prior to the disconnect notification itself. This function
2458 * will be called with this peer's HELLO message to initiate
2459 * a fresh connection to another peer.
2461 * @param cls closure
2462 * @param target who should receive this message
2463 * @param msgbuf the message to transmit
2464 * @param msgbuf_size number of bytes in 'msgbuf'
2465 * @param priority how important is the message (most plugins will
2466 * ignore message priority and just FIFO)
2467 * @param to how long to wait at most for the transmission (does not
2468 * require plugins to discard the message after the timeout,
2469 * just advisory for the desired delay; most plugins will ignore
2471 * @param session which session must be used (or NULL for "any")
2472 * @param addr the address to use (can be NULL if the plugin
2473 * is "on its own" (i.e. re-use existing TCP connection))
2474 * @param addrlen length of the address in bytes
2475 * @param force_address GNUNET_YES if the plugin MUST use the given address,
2476 * GNUNET_NO means the plugin may use any other address and
2477 * GNUNET_SYSERR means that only reliable existing
2478 * bi-directional connections should be used (regardless
2480 * @param cont continuation to call once the message has
2481 * been transmitted (or if the transport is ready
2482 * for the next transmission call; or if the
2483 * peer disconnected...); can be NULL
2484 * @param cont_cls closure for cont
2485 * @return number of bytes used (on the physical network, with overheads);
2486 * -1 on hard errors (i.e. address invalid); 0 is a legal value
2487 * and does NOT mean that the message was not transmitted (DV)
2490 http_plugin_send (void *cls,
2491 const struct GNUNET_PeerIdentity *target,
2494 unsigned int priority,
2495 struct GNUNET_TIME_Relative to,
2496 struct Session *session,
2500 GNUNET_TRANSPORT_TransmitContinuation cont,
2503 struct Plugin *plugin = cls;
2504 struct HTTP_Message *msg;
2505 struct HTTP_PeerContext * pc;
2506 struct Session * ps = NULL;
2508 GNUNET_assert(cls !=NULL);
2513 if (force_address == GNUNET_YES)
2514 GNUNET_asprintf(&force, "forced addr.");
2515 else if (force_address == GNUNET_NO)
2516 GNUNET_asprintf(&force, "any addr.");
2517 else if (force_address == GNUNET_SYSERR)
2518 GNUNET_asprintf(&force,"reliable bi-direc. address addr.");
2521 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2522 "Transport tells me to send %u bytes to `%s' using %s (%s) and session: %X\n",
2526 http_plugin_address_to_string(NULL, addr, addrlen),
2531 pc = GNUNET_CONTAINER_multihashmap_get (plugin->peers, &target->hashPubKey);
2535 pc = GNUNET_malloc(sizeof (struct HTTP_PeerContext));
2536 pc->plugin = plugin;
2537 pc->session_id_counter=1;
2538 pc->last_session = NULL;
2539 memcpy(&pc->identity, target, sizeof(struct GNUNET_PeerIdentity));
2540 GNUNET_CONTAINER_multihashmap_put (plugin->peers,
2541 &pc->identity.hashPubKey,
2543 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
2544 GNUNET_STATISTICS_update (plugin->env->stats,
2545 gettext_noop ("# HTTP peers active"),
2549 ps = send_select_session (pc, addr, addrlen, force_address, session);
2550 /* session not existing, but address forced -> creating new session */
2553 if ((addr!=NULL) && (addrlen!=0))
2555 ps = GNUNET_malloc(sizeof (struct Session));
2556 #if DEBUG_SESSION_SELECTION
2557 if (force_address == GNUNET_YES)
2558 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2559 "No existing connection & forced address: creating new session %X to peer %s\n",
2560 ps, GNUNET_i2s(target));
2561 if (force_address != GNUNET_YES)
2562 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2563 "No existing connection: creating new session %X to peer %s\n",
2564 ps, GNUNET_i2s(target));
2566 ps->addr = GNUNET_malloc(addrlen);
2567 memcpy(ps->addr,addr,addrlen);
2568 ps->addrlen = addrlen;
2569 ps->direction=OUTBOUND;
2570 ps->recv_connected = GNUNET_NO;
2571 ps->recv_force_disconnect = GNUNET_NO;
2572 ps->send_connected = GNUNET_NO;
2573 ps->send_force_disconnect = GNUNET_NO;
2574 ps->pending_msgs_head = NULL;
2575 ps->pending_msgs_tail = NULL;
2577 ps->session_id = pc->session_id_counter;
2578 ps->queue_length_cur = 0;
2579 ps->queue_length_max = GNUNET_SERVER_MAX_MESSAGE_SIZE;
2580 pc->session_id_counter++;
2581 ps->url = create_url (plugin, ps->addr, ps->addrlen, ps->session_id);
2582 if (ps->msgtok == NULL)
2583 ps->msgtok = GNUNET_SERVER_mst_create (&curl_receive_mst_cb, ps);
2584 GNUNET_CONTAINER_DLL_insert(pc->head,pc->tail,ps);
2585 GNUNET_STATISTICS_update (plugin->env->stats,
2586 gettext_noop ("# HTTP outbound sessions for peers active"),
2593 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2594 "No existing session found & and no address given: no way to send this message to peer `%s'!\n",
2595 GNUNET_i2s(target));
2597 return GNUNET_SYSERR;
2601 if (msgbuf_size >= (ps->queue_length_max - ps->queue_length_cur))
2603 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2604 "Queue %X full: %u bytes in queue available, message with %u is too big\n",
2606 (ps->queue_length_max - ps->queue_length_cur),
2608 //return GNUNET_SYSERR;
2612 msg = GNUNET_malloc (sizeof (struct HTTP_Message) + msgbuf_size);
2614 msg->size = msgbuf_size;
2616 msg->buf = (char *) &msg[1];
2617 msg->transmit_cont = cont;
2618 msg->transmit_cont_cls = cont_cls;
2619 memcpy (msg->buf,msgbuf, msgbuf_size);
2620 GNUNET_CONTAINER_DLL_insert (ps->pending_msgs_head,
2621 ps->pending_msgs_tail,
2623 ps->queue_length_cur += msgbuf_size;
2624 if (send_check_connections (plugin, ps) == GNUNET_SYSERR)
2625 return GNUNET_SYSERR;
2626 if (force_address != GNUNET_YES)
2627 pc->last_session = ps;
2628 if (pc->last_session==NULL)
2629 pc->last_session = ps;
2635 * Function that can be used to force the plugin to disconnect
2636 * from the given peer and cancel all previous transmissions
2637 * (and their continuationc).
2639 * @param cls closure
2640 * @param target peer from which to disconnect
2643 http_plugin_disconnect (void *cls,
2644 const struct GNUNET_PeerIdentity *target)
2646 struct Plugin *plugin = cls;
2647 struct HTTP_PeerContext *pc = NULL;
2648 struct Session *ps = NULL;
2650 pc = GNUNET_CONTAINER_multihashmap_get (plugin->peers, &target->hashPubKey);
2656 /* Telling transport that session is getting disconnected */
2657 plugin->env->session_end(plugin, target, ps);
2658 if (ps->direction==OUTBOUND)
2660 if (ps->send_endpoint!=NULL)
2662 //GNUNET_assert(CURLM_OK == curl_multi_remove_handle(plugin->multi_handle,ps->send_endpoint));
2663 //curl_easy_cleanup(ps->send_endpoint);
2664 //ps->send_endpoint=NULL;
2665 ps->send_force_disconnect = GNUNET_YES;
2667 if (ps->recv_endpoint!=NULL)
2669 //GNUNET_assert(CURLM_OK == curl_multi_remove_handle(plugin->multi_handle,ps->recv_endpoint));
2670 //curl_easy_cleanup(ps->recv_endpoint);
2671 //ps->recv_endpoint=NULL;
2672 ps->recv_force_disconnect = GNUNET_YES;
2675 if (ps->direction==INBOUND)
2677 ps->recv_force_disconnect = GNUNET_YES;
2678 ps->send_force_disconnect = GNUNET_YES;
2680 while (ps->pending_msgs_head!=NULL)
2681 remove_http_message(ps, ps->pending_msgs_head);
2682 ps->recv_active = GNUNET_NO;
2683 ps->send_active = GNUNET_NO;
2690 * Convert the transports address to a nice, human-readable
2693 * @param cls closure
2694 * @param type name of the transport that generated the address
2695 * @param addr one of the addresses of the host, NULL for the last address
2696 * the specific address format depends on the transport
2697 * @param addrlen length of the address
2698 * @param numeric should (IP) addresses be displayed in numeric form?
2699 * @param timeout after how long should we give up?
2700 * @param asc function to call on each string
2701 * @param asc_cls closure for asc
2704 http_plugin_address_pretty_printer (void *cls,
2709 struct GNUNET_TIME_Relative timeout,
2710 GNUNET_TRANSPORT_AddressStringCallback
2713 const struct IPv4HttpAddress *t4;
2714 const struct IPv6HttpAddress *t6;
2715 struct sockaddr_in a4;
2716 struct sockaddr_in6 a6;
2722 GNUNET_assert(cls !=NULL);
2723 if (addrlen == sizeof (struct IPv6HttpAddress))
2725 address = GNUNET_malloc (INET6_ADDRSTRLEN);
2727 a6.sin6_addr = t6->ipv6_addr;
2728 inet_ntop(AF_INET6, &(a6.sin6_addr),address,INET6_ADDRSTRLEN);
2729 port = ntohs(t6->u6_port);
2731 else if (addrlen == sizeof (struct IPv4HttpAddress))
2733 address = GNUNET_malloc (INET_ADDRSTRLEN);
2735 a4.sin_addr.s_addr = t4->ipv4_addr;
2736 inet_ntop(AF_INET, &(a4.sin_addr),address,INET_ADDRSTRLEN);
2737 port = ntohs(t4->u_port);
2741 /* invalid address */
2742 GNUNET_break_op (0);
2743 asc (asc_cls, NULL);
2746 res = GNUNET_asprintf(&ret,"%s://%s:%u/", PROTOCOL_PREFIX, address, port);
2747 GNUNET_free (address);
2748 GNUNET_assert(res != 0);
2750 GNUNET_free_non_null (ret);
2756 * Another peer has suggested an address for this
2757 * peer and transport plugin. Check that this could be a valid
2758 * address. If so, consider adding it to the list
2761 * @param cls closure
2762 * @param addr pointer to the address
2763 * @param addrlen length of addr
2764 * @return GNUNET_OK if this is a plausible address for this peer
2768 http_plugin_address_suggested (void *cls,
2769 const void *addr, size_t addrlen)
2771 struct Plugin *plugin = cls;
2772 struct IPv4HttpAddress *v4;
2773 struct IPv6HttpAddress *v6;
2774 struct IPv4HttpAddress *tv4 = plugin->ipv4_addr_head;
2775 struct IPv6HttpAddress *tv6 = plugin->ipv6_addr_head;
2777 GNUNET_assert(cls !=NULL);
2778 if ((addrlen != sizeof (struct IPv4HttpAddress)) &&
2779 (addrlen != sizeof (struct IPv6HttpAddress)))
2780 return GNUNET_SYSERR;
2781 if (addrlen == sizeof (struct IPv4HttpAddress))
2783 v4 = (struct IPv4HttpAddress *) addr;
2784 if (plugin->bind4_address!=NULL)
2786 if (0 == memcmp (&plugin->bind4_address->sin_addr, &v4->ipv4_addr, sizeof(uint32_t)))
2789 return GNUNET_SYSERR;
2793 if (0==memcmp (&tv4->ipv4_addr, &v4->ipv4_addr, sizeof(uint32_t)))
2800 return GNUNET_SYSERR;
2802 if (addrlen == sizeof (struct IPv6HttpAddress))
2804 v6 = (struct IPv6HttpAddress *) addr;
2805 if (plugin->bind6_address!=NULL)
2807 if (0 == memcmp (&plugin->bind6_address->sin6_addr, &v6->ipv6_addr, sizeof(struct in6_addr)))
2810 return GNUNET_SYSERR;
2814 if (0 == memcmp (&tv6->ipv6_addr, &v6->ipv6_addr, sizeof(struct in6_addr)))
2821 return GNUNET_SYSERR;
2823 return GNUNET_SYSERR;
2828 * Function called for a quick conversion of the binary address to
2829 * a numeric address. Note that the caller must not free the
2830 * address and that the next call to this function is allowed
2831 * to override the address again.
2833 * @param cls closure
2834 * @param addr binary address
2835 * @param addrlen length of the address
2836 * @return string representing the same address
2839 http_plugin_address_to_string (void *cls,
2843 const struct IPv4HttpAddress *t4;
2844 const struct IPv6HttpAddress *t6;
2845 struct sockaddr_in a4;
2846 struct sockaddr_in6 a6;
2848 static char rbuf[INET6_ADDRSTRLEN + 13];
2852 if (addrlen == sizeof (struct IPv6HttpAddress))
2854 address = GNUNET_malloc (INET6_ADDRSTRLEN);
2856 a6.sin6_addr = t6->ipv6_addr;
2857 inet_ntop(AF_INET6, &(a6.sin6_addr),address,INET6_ADDRSTRLEN);
2858 port = ntohs(t6->u6_port);
2860 else if (addrlen == sizeof (struct IPv4HttpAddress))
2862 address = GNUNET_malloc (INET_ADDRSTRLEN);
2864 a4.sin_addr.s_addr = t4->ipv4_addr;
2865 inet_ntop(AF_INET, &(a4.sin_addr),address,INET_ADDRSTRLEN);
2866 port = ntohs(t4->u_port);
2870 /* invalid address */
2874 res = GNUNET_snprintf (rbuf,
2880 GNUNET_free (address);
2881 GNUNET_assert(res != 0);
2886 * Function called by the NAT subsystem suggesting another peer wants
2887 * to connect to us via connection reversal. Try to connect back to the
2890 * @param cls closure
2891 * @param addr address to try
2892 * @param addrlen number of bytes in addr
2895 try_connection_reversal (void *cls,
2896 const struct sockaddr *addr,
2903 * Our external IP address/port mapping has changed.
2905 * @param cls closure, the 'struct LocalAddrList'
2906 * @param add_remove GNUNET_YES to mean the new public IP address, GNUNET_NO to mean
2907 * the previous (now invalid) one
2908 * @param addr either the previous or the new public IP address
2909 * @param addrlen actual lenght of the address
2912 tcp_nat_port_map_callback (void *cls,
2914 const struct sockaddr *addr,
2918 struct Plugin *plugin = cls;
2919 struct IPv4HttpAddress *t4;
2920 struct IPv6HttpAddress *t6;
2926 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
2928 "NPMC called with %d for address `%s'\n",
2930 GNUNET_a2s (addr, addrlen));
2931 /* convert 'addr' to our internal format */
2933 GNUNET_assert(cls !=NULL);
2934 af = addr->sa_family;
2935 if ((af == AF_INET) &&
2936 (plugin->use_ipv4 == GNUNET_YES) &&
2937 (plugin->bind6_address == NULL) ) {
2939 struct in_addr bnd_cmp = ((struct sockaddr_in *) addr)->sin_addr;
2940 t4 = GNUNET_malloc(sizeof(struct IPv4HttpAddress));
2941 // Not skipping loopback addresses
2944 t4->ipv4_addr = ((struct sockaddr_in *) addr)->sin_addr.s_addr;
2945 t4->u_port = htons (plugin->port_inbound);
2946 if (plugin->bind4_address != NULL) {
2947 if (0 == memcmp(&plugin->bind4_address->sin_addr, &bnd_cmp, sizeof (struct in_addr)))
2949 GNUNET_CONTAINER_DLL_insert(plugin->ipv4_addr_head,
2950 plugin->ipv4_addr_tail,t4);
2951 plugin->env->notify_address(plugin->env->cls,
2953 t4, sizeof (struct IPv4HttpAddress));
2961 GNUNET_CONTAINER_DLL_insert (plugin->ipv4_addr_head,
2962 plugin->ipv4_addr_tail,
2964 plugin->env->notify_address(plugin->env->cls,
2966 t4, sizeof (struct IPv4HttpAddress));
2970 if ((af == AF_INET6) &&
2971 (plugin->use_ipv6 == GNUNET_YES) &&
2972 (plugin->bind4_address == NULL) ) {
2974 struct in6_addr bnd_cmp6 = ((struct sockaddr_in6 *) addr)->sin6_addr;
2976 t6 = GNUNET_malloc(sizeof(struct IPv6HttpAddress));
2977 GNUNET_assert(t6 != NULL);
2979 if (plugin->bind6_address != NULL) {
2980 if (0 == memcmp(&plugin->bind6_address->sin6_addr,
2982 sizeof (struct in6_addr))) {
2983 memcpy (&t6->ipv6_addr,
2984 &((struct sockaddr_in6 *) addr)->sin6_addr,
2985 sizeof (struct in6_addr));
2986 t6->u6_port = htons (plugin->port_inbound);
2987 plugin->env->notify_address(plugin->env->cls,
2989 t6, sizeof (struct IPv6HttpAddress));
2990 GNUNET_CONTAINER_DLL_insert(plugin->ipv6_addr_head,
2991 plugin->ipv6_addr_tail,
2998 memcpy (&t6->ipv6_addr,
2999 &((struct sockaddr_in6 *) addr)->sin6_addr,
3000 sizeof (struct in6_addr));
3001 t6->u6_port = htons (plugin->port_inbound);
3002 GNUNET_CONTAINER_DLL_insert(plugin->ipv6_addr_head,plugin->ipv6_addr_tail,t6);
3004 plugin->env->notify_address(plugin->env->cls,
3006 t6, sizeof (struct IPv6HttpAddress));
3009 /* modify our published address list */
3010 plugin->env->notify_address (plugin->env->cls,
3016 * Notify transport service about address
3018 * @param cls the plugin
3022 address_notification (void *cls,
3023 const struct GNUNET_SCHEDULER_TaskContext *tc)
3025 struct Plugin *plugin = cls;
3027 GNUNET_OS_network_interfaces_list (&process_interfaces, plugin);
3031 * Exit point from the plugin.
3034 LIBGNUNET_PLUGIN_TRANSPORT_DONE (void *cls)
3036 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
3037 struct Plugin *plugin = api->cls;
3039 struct IPv4HttpAddress * ipv4addr;
3040 struct IPv6HttpAddress * ipv6addr;
3041 GNUNET_assert(cls !=NULL);
3043 if (plugin->http_server_daemon_v4 != NULL)
3045 MHD_stop_daemon (plugin->http_server_daemon_v4);
3046 plugin->http_server_daemon_v4 = NULL;
3048 if (plugin->http_server_daemon_v6 != NULL)
3050 MHD_stop_daemon (plugin->http_server_daemon_v6);
3051 plugin->http_server_daemon_v6 = NULL;
3053 if ( plugin->http_server_task_v4 != GNUNET_SCHEDULER_NO_TASK)
3055 GNUNET_SCHEDULER_cancel(plugin->http_server_task_v4);
3056 plugin->http_server_task_v4 = GNUNET_SCHEDULER_NO_TASK;
3058 if ( plugin->http_server_task_v6 != GNUNET_SCHEDULER_NO_TASK)
3060 GNUNET_SCHEDULER_cancel(plugin->http_server_task_v6);
3061 plugin->http_server_task_v6 = GNUNET_SCHEDULER_NO_TASK;
3064 while (plugin->ipv4_addr_head!=NULL)
3066 ipv4addr = plugin->ipv4_addr_head;
3067 GNUNET_CONTAINER_DLL_remove(plugin->ipv4_addr_head,plugin->ipv4_addr_tail,ipv4addr);
3068 GNUNET_free(ipv4addr);
3071 while (plugin->ipv6_addr_head!=NULL)
3073 ipv6addr = plugin->ipv6_addr_head;
3074 GNUNET_CONTAINER_DLL_remove(plugin->ipv6_addr_head,plugin->ipv6_addr_tail,ipv6addr);
3075 GNUNET_free(ipv6addr);
3078 /* free all peer information */
3079 if (plugin->peers!=NULL)
3081 GNUNET_CONTAINER_multihashmap_iterate (plugin->peers,
3082 &remove_peer_context_Iterator,
3084 GNUNET_CONTAINER_multihashmap_destroy (plugin->peers);
3086 if (plugin->multi_handle!=NULL)
3088 mret = curl_multi_cleanup(plugin->multi_handle);
3089 if (CURLM_OK != mret)
3090 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3091 "curl multihandle clean up failed\n");
3092 plugin->multi_handle = NULL;
3094 curl_global_cleanup();
3096 if ( plugin->http_curl_task != GNUNET_SCHEDULER_NO_TASK)
3098 GNUNET_SCHEDULER_cancel(plugin->http_curl_task);
3099 plugin->http_curl_task = GNUNET_SCHEDULER_NO_TASK;
3102 if (plugin->nat != NULL)
3103 GNUNET_NAT_unregister (plugin->nat);
3105 GNUNET_free_non_null (plugin->bind4_address);
3106 GNUNET_free_non_null (plugin->bind6_address);
3107 GNUNET_free_non_null(plugin->bind_hostname);
3109 GNUNET_free_non_null (plugin->crypto_init);
3110 GNUNET_free_non_null (plugin->cert);
3111 GNUNET_free_non_null (plugin->key);
3113 GNUNET_free (plugin);
3116 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3117 "Unload %s plugin complete...\n",
3125 load_certificate( const char * file )
3127 struct GNUNET_DISK_FileHandle * gn_file;
3131 if (0!=STAT(file, &fstat))
3133 text = GNUNET_malloc (fstat.st_size+1);
3134 gn_file = GNUNET_DISK_file_open(file, GNUNET_DISK_OPEN_READ, GNUNET_DISK_PERM_USER_READ);
3140 if (GNUNET_SYSERR == GNUNET_DISK_file_read(gn_file, text, fstat.st_size))
3143 GNUNET_DISK_file_close(gn_file);
3146 text[fstat.st_size] = '\0';
3147 GNUNET_DISK_file_close(gn_file);
3154 * Entry point for the plugin.
3157 LIBGNUNET_PLUGIN_TRANSPORT_INIT (void *cls)
3159 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
3160 struct Plugin *plugin;
3161 struct GNUNET_TRANSPORT_PluginFunctions *api;
3162 struct GNUNET_TIME_Relative gn_timeout;
3163 long long unsigned int port;
3164 unsigned long long tneigh;
3166 char * component_name;
3168 char * key_file = NULL;
3169 char * cert_file = NULL;
3172 GNUNET_assert(cls !=NULL);
3174 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3175 "Starting %s plugin...\n",
3178 GNUNET_asprintf(&component_name,
3182 plugin = GNUNET_malloc (sizeof (struct Plugin));
3183 plugin->stats = env->stats;
3185 plugin->peers = NULL;
3186 plugin->bind4_address = NULL;
3187 plugin->bind6_address = NULL;
3188 plugin->use_ipv6 = GNUNET_YES;
3189 plugin->use_ipv4 = GNUNET_YES;
3190 plugin->use_localaddresses = GNUNET_NO;
3192 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
3194 api->send = &http_plugin_send;
3195 api->disconnect = &http_plugin_disconnect;
3196 api->address_pretty_printer = &http_plugin_address_pretty_printer;
3197 api->check_address = &http_plugin_address_suggested;
3198 api->address_to_string = &http_plugin_address_to_string;
3200 /* Hashing our identity to use it in URLs */
3201 GNUNET_CRYPTO_hash_to_enc (&(plugin->env->my_identity->hashPubKey),
3202 &plugin->my_ascii_hash_ident);
3205 if (GNUNET_CONFIGURATION_have_value (env->cfg, "TRANSPORT", "NEIGHBOUR_LIMIT"))
3207 GNUNET_CONFIGURATION_get_value_number (env->cfg,
3216 plugin->max_connect_per_transport = tneigh;
3220 if (GNUNET_CONFIGURATION_have_value (env->cfg,
3221 component_name, "USE_IPv6"))
3223 plugin->use_ipv6 = GNUNET_CONFIGURATION_get_value_yesno (env->cfg,
3228 if (GNUNET_CONFIGURATION_have_value (env->cfg,
3229 component_name, "USE_IPv4"))
3231 plugin->use_ipv4 = GNUNET_CONFIGURATION_get_value_yesno (env->cfg,
3232 component_name,"USE_IPv4");
3234 /* use local addresses? */
3236 if (GNUNET_CONFIGURATION_have_value (env->cfg,
3237 component_name, "USE_LOCALADDR"))
3239 plugin->use_localaddresses = GNUNET_CONFIGURATION_get_value_yesno (env->cfg,
3243 /* Reading port number from config file */
3245 GNUNET_CONFIGURATION_get_value_number (env->cfg,
3251 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
3253 _("Require valid port number for transport plugin `%s' in configuration!\n"),
3255 GNUNET_free(component_name);
3256 LIBGNUNET_PLUGIN_TRANSPORT_DONE (api);
3260 /* Reading ipv4 addresse to bind to from config file */
3261 if ( (plugin->use_ipv4==GNUNET_YES) &&
3262 (GNUNET_CONFIGURATION_have_value (env->cfg,
3263 component_name, "BINDTO4")))
3265 GNUNET_break (GNUNET_OK ==
3266 GNUNET_CONFIGURATION_get_value_string (env->cfg,
3269 &plugin->bind_hostname));
3270 plugin->bind4_address = GNUNET_malloc(sizeof(struct sockaddr_in));
3271 plugin->bind4_address->sin_family = AF_INET;
3272 plugin->bind4_address->sin_port = htons (port);
3274 if (plugin->bind_hostname!=NULL)
3276 if (inet_pton(AF_INET,plugin->bind_hostname, &plugin->bind4_address->sin_addr)<=0)
3278 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
3280 _("Misconfigured address to bind to in configuration!\n"));
3281 GNUNET_free(plugin->bind4_address);
3282 GNUNET_free(plugin->bind_hostname);
3283 plugin->bind_hostname = NULL;
3284 plugin->bind4_address = NULL;
3289 /* Reading ipv4 addresse to bind to from config file */
3290 if ( (plugin->use_ipv6==GNUNET_YES) &&
3291 (GNUNET_CONFIGURATION_have_value (env->cfg,
3292 component_name, "BINDTO6")))
3294 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (env->cfg,
3297 &plugin->bind_hostname))
3299 plugin->bind6_address = GNUNET_malloc(sizeof(struct sockaddr_in6));
3300 plugin->bind6_address->sin6_family = AF_INET6;
3301 plugin->bind6_address->sin6_port = htons (port);
3302 if (plugin->bind_hostname!=NULL)
3304 if (inet_pton(AF_INET6,plugin->bind_hostname, &plugin->bind6_address->sin6_addr)<=0)
3306 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
3308 _("Misconfigured address to bind to in configuration!\n"));
3309 GNUNET_free(plugin->bind6_address);
3310 GNUNET_free(plugin->bind_hostname);
3311 plugin->bind_hostname = NULL;
3312 plugin->bind6_address = NULL;
3319 /* Reading HTTPS crypto related configuration */
3320 /* Get crypto init string from config */
3321 if (GNUNET_CONFIGURATION_have_value (env->cfg,
3322 "transport-https", "CRYPTO_INIT"))
3324 GNUNET_CONFIGURATION_get_value_string (env->cfg,
3327 &plugin->crypto_init);
3331 GNUNET_asprintf(&plugin->crypto_init,"NORMAL");
3334 /* Get private key file from config */
3335 if (GNUNET_CONFIGURATION_have_value (env->cfg,
3336 "transport-https", "KEY_FILE"))
3338 GNUNET_CONFIGURATION_get_value_filename (env->cfg,
3344 GNUNET_asprintf(&key_file,"https.key");
3346 /* Get private key file from config */
3347 if (GNUNET_CONFIGURATION_have_value (env->cfg,"transport-https", "CERT_FILE"))
3349 GNUNET_CONFIGURATION_get_value_filename (env->cfg,
3354 if (cert_file==NULL)
3355 GNUNET_asprintf(&cert_file,"https.cert");
3357 /* read key & certificates from file */
3358 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3359 "Loading TLS certificate `%s' `%s'\n",
3360 key_file, cert_file);
3362 plugin->key = load_certificate( key_file );
3363 plugin->cert = load_certificate( cert_file );
3365 if ((plugin->key==NULL) || (plugin->cert==NULL))
3369 GNUNET_asprintf(&cmd,
3370 "gnunet-transport-certificate-creation %s %s",
3371 key_file, cert_file);
3372 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3373 "No usable TLS certificate found, creating certificate \n");
3377 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
3379 _("Could not create a new TLS certificate, shell script `%s' failed!\n"),cmd,
3381 GNUNET_free (key_file);
3382 GNUNET_free (cert_file);
3383 GNUNET_free (component_name);
3384 LIBGNUNET_PLUGIN_TRANSPORT_DONE(api);
3389 plugin->key = load_certificate( key_file );
3390 plugin->cert = load_certificate( cert_file );
3391 if ((plugin->key==NULL) || (plugin->cert==NULL))
3393 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
3395 _("No usable TLS certificate found and creating one failed! \n"),
3397 GNUNET_free (key_file);
3398 GNUNET_free (cert_file);
3399 GNUNET_free (component_name);
3401 LIBGNUNET_PLUGIN_TRANSPORT_DONE(api);
3405 GNUNET_free (key_file);
3406 GNUNET_free (cert_file);
3408 GNUNET_assert((plugin->key!=NULL) && (plugin->cert!=NULL));
3409 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "TLS certificate loaded\n");
3412 GNUNET_assert ((port > 0) && (port <= 65535));
3413 plugin->port_inbound = port;
3414 gn_timeout = GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT;
3415 unsigned int timeout = (gn_timeout.rel_value) / 1000;
3416 if ( (plugin->http_server_daemon_v6 == NULL) &&
3417 (plugin->use_ipv6 == GNUNET_YES) &&
3420 struct sockaddr * tmp = (struct sockaddr *) plugin->bind6_address;
3421 plugin->http_server_daemon_v6 = MHD_start_daemon (
3430 &mhd_accept_cb, plugin,
3431 &mhd_access_cb, plugin,
3432 MHD_OPTION_SOCK_ADDR, tmp,
3433 MHD_OPTION_CONNECTION_LIMIT, (unsigned int) plugin->max_connect_per_transport,
3435 MHD_OPTION_HTTPS_PRIORITIES, plugin->crypto_init,
3436 MHD_OPTION_HTTPS_MEM_KEY, plugin->key,
3437 MHD_OPTION_HTTPS_MEM_CERT, plugin->cert,
3439 MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) timeout,
3440 MHD_OPTION_CONNECTION_MEMORY_LIMIT, (size_t) (2 * GNUNET_SERVER_MAX_MESSAGE_SIZE),
3441 MHD_OPTION_NOTIFY_COMPLETED, &mhd_termination_cb, plugin,
3442 MHD_OPTION_EXTERNAL_LOGGER, mhd_logger, plugin->mhd_log,
3445 if ( (plugin->http_server_daemon_v4 == NULL) &&
3446 (plugin->use_ipv4 == GNUNET_YES) &&
3449 plugin->http_server_daemon_v4 = MHD_start_daemon (
3458 &mhd_accept_cb, plugin ,
3459 &mhd_access_cb, plugin,
3460 MHD_OPTION_SOCK_ADDR, (struct sockaddr_in *) plugin->bind4_address,
3461 MHD_OPTION_CONNECTION_LIMIT, (unsigned int) plugin->max_connect_per_transport,
3463 MHD_OPTION_HTTPS_PRIORITIES, plugin->crypto_init,
3464 MHD_OPTION_HTTPS_MEM_KEY, plugin->key,
3465 MHD_OPTION_HTTPS_MEM_CERT, plugin->cert,
3467 MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) timeout,
3468 MHD_OPTION_CONNECTION_MEMORY_LIMIT, (size_t) (2 * GNUNET_SERVER_MAX_MESSAGE_SIZE),
3469 MHD_OPTION_NOTIFY_COMPLETED, &mhd_termination_cb, plugin,
3470 MHD_OPTION_EXTERNAL_LOGGER, mhd_logger, plugin->mhd_log,
3473 if (plugin->http_server_daemon_v4 != NULL)
3474 plugin->http_server_task_v4 = http_server_daemon_prepare (plugin, plugin->http_server_daemon_v4);
3475 if (plugin->http_server_daemon_v6 != NULL)
3476 plugin->http_server_task_v6 = http_server_daemon_prepare (plugin, plugin->http_server_daemon_v6);
3479 if (plugin->http_server_task_v4 != GNUNET_SCHEDULER_NO_TASK)
3482 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3483 "Starting MHD with IPv4 bound to %s with port %u\n",
3484 (plugin->bind_hostname!=NULL) ? plugin->bind_hostname : "every address",port);
3487 else if ( (plugin->http_server_task_v6 != GNUNET_SCHEDULER_NO_TASK) &&
3488 (plugin->http_server_task_v4 != GNUNET_SCHEDULER_NO_TASK) )
3491 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3492 "Starting MHD with IPv6 bound to %s with port %u\n",
3493 (plugin->bind_hostname!=NULL) ? plugin->bind_hostname : "every address", port);
3496 else if ( (plugin->http_server_task_v6 != GNUNET_SCHEDULER_NO_TASK) &&
3497 (plugin->http_server_task_v4 == GNUNET_SCHEDULER_NO_TASK) )
3500 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3501 "Starting MHD with IPv4 and IPv6 bound to %s with port %u\n",
3502 (plugin->bind_hostname!=NULL) ? plugin->bind_hostname : "every address",
3509 if ((plugin->use_ipv6 == GNUNET_YES) && (plugin->use_ipv4 == GNUNET_YES))
3510 GNUNET_asprintf(&tmp,"with IPv4 and IPv6 enabled");
3511 if ((plugin->use_ipv6 == GNUNET_NO) && (plugin->use_ipv4 == GNUNET_YES))
3512 GNUNET_asprintf(&tmp,"with IPv4 enabled");
3513 if ((plugin->use_ipv6 == GNUNET_YES) && (plugin->use_ipv4 == GNUNET_NO))
3514 GNUNET_asprintf(&tmp,"with IPv6 enabled");
3515 if ((plugin->use_ipv6 == GNUNET_NO) && (plugin->use_ipv4 == GNUNET_NO))
3516 GNUNET_asprintf(&tmp,"with NO IP PROTOCOL enabled");
3517 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3518 _("HTTP Server with %s could not be started on port %u! %s plugin failed!\n"),
3519 tmp, port, PROTOCOL_PREFIX);
3521 GNUNET_free (component_name);
3522 LIBGNUNET_PLUGIN_TRANSPORT_DONE (api);
3526 /* Initializing cURL */
3527 curl_global_init(CURL_GLOBAL_ALL);
3528 plugin->multi_handle = curl_multi_init();
3530 if ( NULL == plugin->multi_handle )
3532 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
3534 _("Could not initialize curl multi handle, failed to start %s plugin!\n"),
3536 GNUNET_free(component_name);
3537 LIBGNUNET_PLUGIN_TRANSPORT_DONE (api);
3541 if (plugin->bind4_address != NULL)
3543 if (plugin->bind6_address != NULL)
3546 struct sockaddr **addrs;
3547 socklen_t *addrlens;
3549 ret = GNUNET_SERVICE_get_server_addresses ("transport-http",
3554 if (ret != GNUNET_SYSERR)
3557 struct sockaddr *tmp = addrs[counter];
3558 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
3560 "addresses %u\n",ret);
3563 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
3565 "address[%u] %s\n",counter, (tmp->sa_family == AF_INET) ? "AF_INET" : "AF_INET6" );
3567 tmp = addrs[counter];
3571 plugin->nat = GNUNET_NAT_register (env->cfg,
3575 (const struct sockaddr **) addrs,
3577 &tcp_nat_port_map_callback,
3578 &try_connection_reversal,
3583 GNUNET_assert (addrs[ret] != NULL);
3584 GNUNET_free (addrs[ret]);
3586 GNUNET_free_non_null (addrs);
3587 GNUNET_free_non_null (addrlens);
3591 plugin->nat = GNUNET_NAT_register (env->cfg,
3596 &try_connection_reversal,
3600 plugin->peers = GNUNET_CONTAINER_multihashmap_create (10);
3602 GNUNET_free(component_name);
3603 GNUNET_SCHEDULER_add_now(address_notification, plugin);
3607 /* end of plugin_transport_http.c */