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 "plugin_transport.h"
39 #include "gnunet_os_lib.h"
40 #include "microhttpd.h"
41 #include <curl/curl.h>
44 #define LIBGNUNET_PLUGIN_TRANSPORT_INIT libgnunet_plugin_transport_https_init
45 #define LIBGNUNET_PLUGIN_TRANSPORT_DONE libgnunet_plugin_transport_https_done
46 #define LIBGNUNET_PLUGIN_TRANSPORT_COMPONENT transport_https
47 #define PROTOCOL_PREFIX "https"
49 #define LIBGNUNET_PLUGIN_TRANSPORT_INIT libgnunet_plugin_transport_http_init
50 #define LIBGNUNET_PLUGIN_TRANSPORT_DONE libgnunet_plugin_transport_http_done
51 #define LIBGNUNET_PLUGIN_TRANSPORT_COMPONENT transport_http
52 #define PROTOCOL_PREFIX "http"
55 #define DEBUG_HTTP GNUNET_NO
56 #define DEBUG_CURL GNUNET_NO
57 #define DEBUG_MHD GNUNET_NO
58 #define DEBUG_CONNECTIONS GNUNET_NO
59 #define DEBUG_SESSION_SELECTION GNUNET_NO
60 #define DEBUG_SCHEDULING GNUNET_NO
61 #define CURL_TCP_NODELAY GNUNET_YES
63 #define INBOUND GNUNET_NO
64 #define OUTBOUND GNUNET_YES
69 * Text of the response sent back after the last bytes of a PUT
70 * request have been received (just to formally obey the HTTP
73 #define HTTP_PUT_RESPONSE "Thank you!"
76 * After how long do we expire an address that we
77 * learned from another peer if it is not reconfirmed
80 #define LEARNED_ADDRESS_EXPIRATION GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_HOURS, 6)
83 * Page returned if request invalid
85 #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>"
88 * Timeout for a http connect
90 #define HTTP_CONNECT_TIMEOUT 30
94 * Network format for IPv4 addresses.
96 struct IPv4HttpAddress
101 struct IPv4HttpAddress * next;
104 * Linked list previous
106 struct IPv4HttpAddress * prev;
109 * IPv4 address, in network byte order.
111 uint32_t ipv4_addr GNUNET_PACKED;
114 * Port number, in network byte order.
116 uint16_t u_port GNUNET_PACKED;
122 * Network format for IPv6 addresses.
124 struct IPv6HttpAddress
129 struct IPv6HttpAddress * next;
132 * Linked list previous
134 struct IPv6HttpAddress * prev;
139 struct in6_addr ipv6_addr GNUNET_PACKED;
142 * Port number, in network byte order.
144 uint16_t u6_port GNUNET_PACKED;
150 * Message to send using http
155 * next pointer for double linked list
157 struct HTTP_Message * next;
160 * previous pointer for double linked list
162 struct HTTP_Message * prev;
165 * buffer containing data to send
170 * amount of data already sent
180 * Continuation function to call once the transmission buffer
181 * has again space available. NULL if there is no
182 * continuation to call.
184 GNUNET_TRANSPORT_TransmitContinuation transmit_cont;
187 * Closure for transmit_cont.
189 void *transmit_cont_cls;
193 struct HTTP_PeerContext
198 struct GNUNET_PeerIdentity identity;
201 * Pointer to the global plugin struct.
203 struct Plugin *plugin;
206 * Linked list of connections with this peer
209 struct Session * head;
212 * Linked list of connections with this peer
215 struct Session * tail;
218 * id for next session
220 size_t session_id_counter;
223 * Last session used to send data
225 struct Session * last_session;
228 * The task resetting inbound quota delay
230 GNUNET_SCHEDULER_TaskIdentifier reset_task;
233 * Delay from transport service inbound quota tracker when to receive data again
235 struct GNUNET_TIME_Relative delay;
244 struct SessionHeader header;
247 * next session in linked list
249 struct Session * next;
252 * previous session in linked list
254 struct Session * prev;
257 * address of this session
272 * Message queue for outbound messages
275 struct HTTP_Message * pending_msgs_head;
278 * Message queue for outbound messages
281 struct HTTP_Message * pending_msgs_tail;
284 * partner peer this connection belongs to
286 struct HTTP_PeerContext * peercontext;
289 * message stream tokenizer for incoming data
291 struct GNUNET_SERVER_MessageStreamTokenizer *msgtok;
295 * outbound: OUTBOUND (GNUNET_YES)
296 * inbound : INBOUND (GNUNET_NO)
298 unsigned int direction;
301 * is session connected to send data?
303 unsigned int send_connected;
306 * is send connection active?
308 unsigned int send_active;
311 * connection disconnect forced (e.g. from transport)
313 unsigned int send_force_disconnect;
316 * is session connected to receive data?
318 unsigned int recv_connected;
321 * is receive connection active?
323 unsigned int recv_active;
326 * connection disconnect forced (e.g. from transport)
328 unsigned int recv_force_disconnect;
331 * id for next session
332 * NOTE: 0 is not an ID, zero is not defined. A correct ID is always > 0
337 * entity managing sending data
338 * outbound session: CURL *
339 * inbound session: mhd_connection *
341 void * send_endpoint;
344 * entity managing recieving data
345 * outbound session: CURL *
346 * inbound session: mhd_connection *
348 void * recv_endpoint;
353 size_t queue_length_cur;
358 size_t queue_length_max;
363 * Encapsulation of all of the state of the plugin.
370 struct GNUNET_TRANSPORT_PluginEnvironment *env;
373 * Handle for reporting statistics.
375 struct GNUNET_STATISTICS_Handle *stats;
380 uint16_t port_inbound;
382 struct GNUNET_CONTAINER_MultiHashMap *peers;
385 * Daemon for listening for new IPv4 connections.
387 struct MHD_Daemon *http_server_daemon_v4;
390 * Daemon for listening for new IPv6connections.
392 struct MHD_Daemon *http_server_daemon_v6;
395 * Our primary task for http daemon handling IPv4 connections
397 GNUNET_SCHEDULER_TaskIdentifier http_server_task_v4;
400 * Our primary task for http daemon handling IPv6 connections
402 GNUNET_SCHEDULER_TaskIdentifier http_server_task_v6;
405 * The task sending data
407 GNUNET_SCHEDULER_TaskIdentifier http_curl_task;
412 CURLM * multi_handle;
417 struct IPv4HttpAddress * ipv4_addr_head;
422 struct IPv4HttpAddress * ipv4_addr_tail;
427 struct IPv6HttpAddress * ipv6_addr_head;
432 struct IPv6HttpAddress * ipv6_addr_tail;
435 * Our ASCII encoded, hashed peer identity
436 * This string is used to distinguish between connections and is added to the urls
438 struct GNUNET_CRYPTO_HashAsciiEncoded my_ascii_hash_ident;
441 * IPv4 Address the plugin binds to
443 struct sockaddr_in * bind4_address;
446 * IPv6 Address the plugins binds to
448 struct sockaddr_in6 * bind6_address;
451 * Hostname to bind to
453 char * bind_hostname;
466 * Closure passed by MHD to the mhd_logger function
470 /* only needed for HTTPS plugin */
472 /* The certificate MHD uses as an \0 terminated string */
475 /* The private key MHD uses as an \0 terminated string */
478 /* crypto init string */
485 * Function called for a quick conversion of the binary address to
486 * a numeric address. Note that the caller must not free the
487 * address and that the next call to this function is allowed
488 * to override the address again.
491 * @param addr binary address
492 * @param addrlen length of the address
493 * @return string representing the same address
496 http_plugin_address_to_string (void *cls,
502 * Call MHD to process pending ipv4 requests and then go back
503 * and schedule the next run.
505 static void http_server_daemon_v4_run (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
507 * Call MHD to process pending ipv6 requests and then go back
508 * and schedule the next run.
510 static void http_server_daemon_v6_run (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
513 * Function setting up curl handle and selecting message to send
514 * @param plugin plugin
515 * @param ses session to send data to
516 * @param con connection
517 * @return bytes sent to peer
519 static int send_check_connections (struct Plugin *plugin, struct Session *ps);
522 * Function setting up file descriptors and scheduling task to run
524 * @param ses session to send data to
527 static int curl_schedule (struct Plugin *plugin);
530 * Task scheduled to reset the inbound quota delay for a specific peer
531 * @param cls plugin as closure
532 * @param tc task context
534 static void reset_inbound_quota_delay (void *cls,
535 const struct GNUNET_SCHEDULER_TaskContext *tc)
537 struct HTTP_PeerContext * pc;
539 GNUNET_assert(cls !=NULL);
541 pc = (struct HTTP_PeerContext *) cls;
542 pc->reset_task = GNUNET_SCHEDULER_NO_TASK;
544 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
547 pc->delay = GNUNET_TIME_relative_get_zero ();
552 * Creates a valid url from passed address and id
553 * @param plugin plugin
554 * @param addr address to create url from
555 * @param addrlen address lenth
556 * @param id session id
557 * @return the created url
559 static char * create_url(struct Plugin *plugin, const void * addr, size_t addrlen, size_t id)
562 char *addr_str = (char *) http_plugin_address_to_string(NULL, addr, addrlen);
564 GNUNET_assert ((addr!=NULL) && (addrlen != 0));
565 GNUNET_asprintf(&url,
566 "%s://%s/%s;%u", PROTOCOL_PREFIX, addr_str,
567 (char *) (&plugin->my_ascii_hash_ident),id);
568 GNUNET_free_non_null(addr_str);
573 * Removes a message from the linked list of messages
576 * @return GNUNET_SYSERR if msg not found, GNUNET_OK on success
578 static int remove_http_message (struct Session * ps, struct HTTP_Message * msg)
580 GNUNET_CONTAINER_DLL_remove(ps->pending_msgs_head,ps->pending_msgs_tail,msg);
586 * Iterator to remove peer context
587 * @param cls the plugin
588 * @param key the peers public key hashcode
589 * @param value the peer context
590 * @return GNUNET_YES on success
592 int remove_peer_context_Iterator (void *cls, const GNUNET_HashCode *key, void *value)
594 struct Plugin *plugin = cls;
595 struct HTTP_PeerContext * pc = value;
596 struct Session * ps = pc->head;
597 struct Session * tmp = NULL;
598 struct HTTP_Message * msg = NULL;
599 struct HTTP_Message * msg_tmp = NULL;
601 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Freeing context for peer `%s'\n",GNUNET_i2s(&pc->identity));
603 GNUNET_CONTAINER_multihashmap_remove (plugin->peers, &pc->identity.hashPubKey, pc);
606 plugin->env->session_end(plugin, &pc->identity, ps);
609 GNUNET_free_non_null (ps->addr);
610 GNUNET_free(ps->url);
611 if (ps->msgtok != NULL)
612 GNUNET_SERVER_mst_destroy (ps->msgtok);
614 msg = ps->pending_msgs_head;
621 if (ps->direction==OUTBOUND)
623 if (ps->send_endpoint!=NULL)
624 curl_easy_cleanup(ps->send_endpoint);
625 if (ps->recv_endpoint!=NULL)
626 curl_easy_cleanup(ps->recv_endpoint);
633 GNUNET_STATISTICS_update (plugin->env->stats,
634 gettext_noop ("# HTTP peers active"),
642 * Removes a session from the linked list of sessions
643 * @param pc peer context
645 * @param call_msg_cont GNUNET_YES to call pending message continuations, otherwise no
646 * @param call_msg_cont_result result to call message continuations with
647 * @return GNUNET_SYSERR if msg not found, GNUNET_OK on success
649 static int remove_session (struct HTTP_PeerContext * pc, struct Session * ps, int call_msg_cont, int call_msg_cont_result)
651 struct HTTP_Message * msg;
652 struct Plugin * plugin = ps->peercontext->plugin;
654 #if DEBUG_CONNECTIONS
655 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: removing %s session %X with id %u\n", ps, (ps->direction == INBOUND) ? "inbound" : "outbound", ps, ps->session_id);
657 plugin->env->session_end(plugin, &pc->identity, ps);
659 GNUNET_free_non_null (ps->addr);
660 GNUNET_SERVER_mst_destroy (ps->msgtok);
661 GNUNET_free(ps->url);
663 if (ps->direction==INBOUND)
665 if (ps->recv_endpoint != NULL)
667 curl_easy_cleanup(ps->recv_endpoint);
668 ps->recv_endpoint = NULL;
670 if (ps->send_endpoint != NULL)
672 curl_easy_cleanup(ps->send_endpoint);
673 ps->send_endpoint = NULL;
677 msg = ps->pending_msgs_head;
680 if ((call_msg_cont == GNUNET_YES) && (msg->transmit_cont!=NULL))
682 msg->transmit_cont (msg->transmit_cont_cls,&pc->identity,call_msg_cont_result);
684 GNUNET_CONTAINER_DLL_remove(ps->pending_msgs_head,ps->pending_msgs_head,msg);
686 msg = ps->pending_msgs_head;
689 GNUNET_CONTAINER_DLL_remove(pc->head,pc->tail,ps);
693 /* no sessions left remove peer */
697 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"No sessions left for peer `%s', removing context\n",GNUNET_i2s(&pc->identity));
699 remove_peer_context_Iterator(plugin, &pc->identity.hashPubKey, pc);
707 * Add the IP of our network interface to the list of
708 * our external IP addresses.
710 * @param cls the 'struct Plugin*'
711 * @param name name of the interface
712 * @param isDefault do we think this may be our default interface
713 * @param addr address of the interface
714 * @param addrlen number of bytes in addr
715 * @return GNUNET_OK to continue iterating
718 process_interfaces (void *cls,
721 const struct sockaddr *addr, socklen_t addrlen)
723 struct Plugin *plugin = cls;
724 struct IPv4HttpAddress * t4;
725 struct IPv6HttpAddress * t6;
729 GNUNET_assert(cls !=NULL);
730 af = addr->sa_family;
731 if ((af == AF_INET) && (plugin->use_ipv4 == GNUNET_YES) && (plugin->bind6_address == NULL))
733 struct in_addr bnd_cmp = ((struct sockaddr_in *) addr)->sin_addr;
734 t4 = GNUNET_malloc(sizeof(struct IPv4HttpAddress));
735 /* Not skipping loopback addresses
736 if (INADDR_LOOPBACK == ntohl(((struct sockaddr_in *) addr)->sin_addr.s_addr))
742 t4->ipv4_addr = ((struct sockaddr_in *) addr)->sin_addr.s_addr;
743 t4->u_port = htons (plugin->port_inbound);
744 if (plugin->bind4_address != NULL)
746 if (0 == memcmp(&plugin->bind4_address->sin_addr, &bnd_cmp, sizeof (struct in_addr)))
748 GNUNET_CONTAINER_DLL_insert(plugin->ipv4_addr_head,plugin->ipv4_addr_tail,t4);
749 plugin->env->notify_address(plugin->env->cls,PROTOCOL_PREFIX,t4, sizeof (struct IPv4HttpAddress), GNUNET_TIME_UNIT_FOREVER_REL);
754 GNUNET_CONTAINER_DLL_insert(plugin->ipv4_addr_head,plugin->ipv4_addr_tail,t4);
755 plugin->env->notify_address(plugin->env->cls,PROTOCOL_PREFIX,t4, sizeof (struct IPv4HttpAddress), GNUNET_TIME_UNIT_FOREVER_REL);
759 else if ((af == AF_INET6) && (plugin->use_ipv6 == GNUNET_YES) && (plugin->bind4_address == NULL))
761 struct in6_addr bnd_cmp6 = ((struct sockaddr_in6 *) addr)->sin6_addr;
762 if (IN6_IS_ADDR_LINKLOCAL (&((struct sockaddr_in6 *) addr)->sin6_addr))
766 t6 = GNUNET_malloc(sizeof(struct IPv6HttpAddress));
767 GNUNET_assert(t6 != NULL);
768 if (plugin->bind6_address != NULL)
770 if (0 == memcmp(&plugin->bind6_address->sin6_addr, &bnd_cmp6, sizeof (struct in6_addr)))
772 memcpy (&t6->ipv6_addr,
773 &((struct sockaddr_in6 *) addr)->sin6_addr,
774 sizeof (struct in6_addr));
775 t6->u6_port = htons (plugin->port_inbound);
776 plugin->env->notify_address(plugin->env->cls,PROTOCOL_PREFIX,t6,sizeof (struct IPv6HttpAddress) , GNUNET_TIME_UNIT_FOREVER_REL);
777 GNUNET_CONTAINER_DLL_insert(plugin->ipv6_addr_head,plugin->ipv6_addr_tail,t6);
782 memcpy (&t6->ipv6_addr,
783 &((struct sockaddr_in6 *) addr)->sin6_addr,
784 sizeof (struct in6_addr));
785 t6->u6_port = htons (plugin->port_inbound);
786 GNUNET_CONTAINER_DLL_insert(plugin->ipv6_addr_head,plugin->ipv6_addr_tail,t6);
787 plugin->env->notify_address(plugin->env->cls,PROTOCOL_PREFIX,t6,sizeof (struct IPv6HttpAddress) , GNUNET_TIME_UNIT_FOREVER_REL);
795 * External logging function for MHD
796 * @param arg arguments
797 * @param fmt format string
798 * @param ap list of arguments
800 void mhd_logger (void * arg, const char * fmt, va_list ap)
803 vsnprintf(text, 1024, fmt, ap);
805 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,"MHD: %s \n", text);
809 static void mhd_termination_cb (void *cls, struct MHD_Connection * connection, void **httpSessionCache)
811 struct Session * ps = *httpSessionCache;
814 struct HTTP_PeerContext * pc = ps->peercontext;
816 if (connection==ps->recv_endpoint)
818 #if DEBUG_CONNECTIONS
819 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: inbound connection from peer `%s' was terminated\n", ps, GNUNET_i2s(&pc->identity));
821 ps->recv_active = GNUNET_NO;
822 ps->recv_connected = GNUNET_NO;
823 ps->recv_endpoint = NULL;
825 if (connection==ps->send_endpoint)
828 ps->send_active = GNUNET_NO;
829 ps->send_connected = GNUNET_NO;
830 ps->send_endpoint = NULL;
831 #if DEBUG_CONNECTIONS
832 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: outbound connection from peer `%s' was terminated\n", ps, GNUNET_i2s(&pc->identity));
836 /* if both connections disconnected, remove session */
837 if ((ps->send_connected == GNUNET_NO) && (ps->recv_connected == GNUNET_NO))
839 GNUNET_STATISTICS_update (pc->plugin->env->stats,
840 gettext_noop ("# HTTP inbound sessions for peers active"),
843 remove_session(pc,ps,GNUNET_YES,GNUNET_SYSERR);
848 * Callback called by MessageStreamTokenizer when a message has arrived
849 * @param cls current session as closure
850 * @param client clien
851 * @param message the message to be forwarded to transport service
854 static void mhd_write_mst_cb (void *cls,
856 const struct GNUNET_MessageHeader *message)
858 struct GNUNET_TIME_Relative delay;
859 struct Session *ps = cls;
860 GNUNET_assert(ps != NULL);
862 struct HTTP_PeerContext *pc = ps->peercontext;
863 GNUNET_assert(pc != NULL);
865 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
866 "Connection %X: Forwarding message to transport service, type %u and size %u from `%s' (`%s')\n",
868 ntohs(message->type),
869 ntohs(message->size),
870 GNUNET_i2s(&(ps->peercontext)->identity),http_plugin_address_to_string(NULL,ps->addr,ps->addrlen));
872 delay = pc->plugin->env->receive (ps->peercontext->plugin->env->cls,
878 if (pc->reset_task != GNUNET_SCHEDULER_NO_TASK)
879 GNUNET_SCHEDULER_cancel (pc->plugin->env->sched, pc->reset_task);
884 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,"Connection %X: Inbound quota management: delay next read for %llu ms \n", ps, delay.value);
886 pc->reset_task = GNUNET_SCHEDULER_add_delayed (pc->plugin->env->sched, delay, &reset_inbound_quota_delay, pc);
891 * Check if incoming connection is accepted.
892 * NOTE: Here every connection is accepted
893 * @param cls plugin as closure
894 * @param addr address of incoming connection
895 * @param addr_len address length of incoming connection
896 * @return MHD_YES if connection is accepted, MHD_NO if connection is rejected
900 mhd_accept_cb (void *cls, const struct sockaddr *addr, socklen_t addr_len)
903 struct Plugin *plugin = cls;
905 /* Every connection is accepted, nothing more to do here */
911 * Callback called by MHD when it needs data to send
912 * @param cls current session
913 * @param pos position in buffer
914 * @param buf the buffer to write data to
915 * @param max max number of bytes available in buffer
916 * @return bytes written to buffer
919 mhd_send_callback (void *cls, uint64_t pos, char *buf, size_t max)
921 struct Session * ps = cls;
922 struct HTTP_PeerContext * pc;
923 struct HTTP_Message * msg;
926 GNUNET_assert (ps!=NULL);
928 pc = ps->peercontext;
929 msg = ps->pending_msgs_tail;
930 if (ps->send_force_disconnect==GNUNET_YES)
932 #if DEBUG_CONNECTIONS
933 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: outbound forced to disconnect\n",ps);
940 if ((msg->size-msg->pos) <= max)
942 memcpy(buf,&msg->buf[msg->pos],(msg->size-msg->pos));
943 bytes_read = msg->size-msg->pos;
944 msg->pos+=(msg->size-msg->pos);
948 memcpy(buf,&msg->buf[msg->pos],max);
953 if (msg->pos==msg->size)
955 if (NULL!=msg->transmit_cont)
956 msg->transmit_cont (msg->transmit_cont_cls,&pc->identity,GNUNET_OK);
957 ps->queue_length_cur -= msg->size;
958 remove_http_message(ps,msg);
965 * Process GET or PUT request received via MHD. For
966 * GET, queue response that will send back our pending
967 * messages. For PUT, process incoming data and send
968 * to GNUnet core. In either case, check if a session
969 * already exists and create a new one if not.
972 mdh_access_cb (void *cls,
973 struct MHD_Connection *mhd_connection,
977 const char *upload_data,
978 size_t * upload_data_size, void **httpSessionCache)
980 struct Plugin *plugin = cls;
981 struct MHD_Response *response;
982 const union MHD_ConnectionInfo * conn_info;
984 struct sockaddr_in *addrin;
985 struct sockaddr_in6 *addrin6;
987 char address[INET6_ADDRSTRLEN+14];
988 struct GNUNET_PeerIdentity pi_in;
991 struct IPv4HttpAddress ipv4addr;
992 struct IPv6HttpAddress ipv6addr;
994 struct HTTP_PeerContext *pc = NULL;
995 struct Session *ps = NULL;
996 struct Session *ps_tmp = NULL;
999 int send_error_to_client;
1001 size_t addr_len = 0 ;
1003 GNUNET_assert(cls !=NULL);
1004 send_error_to_client = GNUNET_NO;
1006 if (NULL == *httpSessionCache)
1008 /* check url for peer identity , if invalid send HTTP 404*/
1009 size_t len = strlen(&url[1]);
1010 char * peer = GNUNET_malloc(104+1);
1012 if ((len>104) && (url[104]==';'))
1014 char * id = GNUNET_malloc((len-104)+1);
1015 strcpy(id,&url[105]);
1016 memcpy(peer,&url[1],103);
1018 id_num = strtoul ( id, NULL , 10);
1021 res = GNUNET_CRYPTO_hash_from_string (peer, &(pi_in.hashPubKey));
1023 if ( GNUNET_SYSERR == res )
1025 response = MHD_create_response_from_data (strlen (HTTP_ERROR_RESPONSE),HTTP_ERROR_RESPONSE, MHD_NO, MHD_NO);
1026 res = MHD_queue_response (mhd_connection, MHD_HTTP_NOT_FOUND, response);
1027 MHD_destroy_response (response);
1028 #if DEBUG_CONNECTIONS
1030 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Peer has no valid ident, sent HTTP 1.1/404\n");
1032 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Peer has no valid ident, could not send error\n");
1039 ps = *httpSessionCache;
1040 pc = ps->peercontext;
1043 if (NULL == *httpSessionCache)
1045 /* get peer context */
1046 pc = GNUNET_CONTAINER_multihashmap_get (plugin->peers, &pi_in.hashPubKey);
1050 pc = GNUNET_malloc(sizeof (struct HTTP_PeerContext));
1051 pc->plugin = plugin;
1052 pc->session_id_counter=1;
1053 pc->last_session = NULL;
1054 memcpy(&pc->identity, &pi_in, sizeof(struct GNUNET_PeerIdentity));
1055 GNUNET_CONTAINER_multihashmap_put(plugin->peers, &pc->identity.hashPubKey, pc, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
1056 GNUNET_STATISTICS_update (plugin->env->stats,
1057 gettext_noop ("# HTTP peers active"),
1062 conn_info = MHD_get_connection_info(mhd_connection, MHD_CONNECTION_INFO_CLIENT_ADDRESS );
1063 /* Incoming IPv4 connection */
1064 if ( AF_INET == conn_info->client_addr->sin_family)
1066 addrin = conn_info->client_addr;
1067 inet_ntop(addrin->sin_family, &(addrin->sin_addr),address,INET_ADDRSTRLEN);
1068 memcpy(&ipv4addr.ipv4_addr,&(addrin->sin_addr),sizeof(struct in_addr));
1069 ipv4addr.u_port = addrin->sin_port;
1071 addr_len = sizeof(struct IPv4HttpAddress);
1073 /* Incoming IPv6 connection */
1074 if ( AF_INET6 == conn_info->client_addr->sin_family)
1076 addrin6 = (struct sockaddr_in6 *) conn_info->client_addr;
1077 inet_ntop(addrin6->sin6_family, &(addrin6->sin6_addr),address,INET6_ADDRSTRLEN);
1078 memcpy(&ipv6addr.ipv6_addr,&(addrin6->sin6_addr),sizeof(struct in6_addr));
1079 ipv6addr.u6_port = addrin6->sin6_port;
1081 addr_len = sizeof(struct IPv6HttpAddress);
1084 GNUNET_assert (addr != NULL);
1085 GNUNET_assert (addr_len != 0);
1088 /* only inbound sessions here */
1091 while (ps_tmp!=NULL)
1093 if ((ps_tmp->direction==INBOUND) && (ps_tmp->session_id == id_num) && (id_num!=0))
1095 if ((ps_tmp->recv_force_disconnect!=GNUNET_YES) && (ps_tmp->send_force_disconnect!=GNUNET_YES))
1099 ps_tmp=ps_tmp->next;
1104 ps = GNUNET_malloc(sizeof (struct Session));
1105 ps->addr = GNUNET_malloc(addr_len);
1106 memcpy(ps->addr,addr,addr_len);
1107 ps->addrlen = addr_len;
1108 ps->direction=INBOUND;
1109 ps->pending_msgs_head = NULL;
1110 ps->pending_msgs_tail = NULL;
1111 ps->send_connected=GNUNET_NO;
1112 ps->send_active=GNUNET_NO;
1113 ps->recv_connected=GNUNET_NO;
1114 ps->recv_active=GNUNET_NO;
1116 ps->session_id =id_num;
1117 ps->queue_length_cur = 0;
1118 ps->queue_length_max = GNUNET_SERVER_MAX_MESSAGE_SIZE;
1119 ps->url = create_url (plugin, ps->addr, ps->addrlen, ps->session_id);
1120 GNUNET_CONTAINER_DLL_insert(pc->head,pc->tail,ps);
1121 GNUNET_STATISTICS_update (plugin->env->stats,
1122 gettext_noop ("# HTTP inbound sessions for peers active"),
1127 *httpSessionCache = ps;
1128 if (ps->msgtok==NULL)
1129 ps->msgtok = GNUNET_SERVER_mst_create (&mhd_write_mst_cb, ps);
1131 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: HTTP Daemon has new an incoming `%s' request from peer `%s' (`%s')\n",
1134 GNUNET_i2s(&pc->identity),
1135 http_plugin_address_to_string(NULL, ps->addr, ps->addrlen));
1139 /* Is it a PUT or a GET request */
1140 if (0 == strcmp (MHD_HTTP_METHOD_PUT, method))
1142 if (ps->recv_force_disconnect == GNUNET_YES)
1144 #if DEBUG_CONNECTIONS
1145 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: inbound connection was forced to disconnect\n",ps);
1147 ps->recv_active = GNUNET_NO;
1150 if ((*upload_data_size == 0) && (ps->recv_active==GNUNET_NO))
1152 ps->recv_endpoint = mhd_connection;
1153 ps->recv_connected = GNUNET_YES;
1154 ps->recv_active = GNUNET_YES;
1155 ps->recv_force_disconnect = GNUNET_NO;
1156 #if DEBUG_CONNECTIONS
1157 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: inbound PUT connection connected\n",ps);
1162 /* Transmission of all data complete */
1163 if ((*upload_data_size == 0) && (ps->recv_active == GNUNET_YES))
1165 response = MHD_create_response_from_data (strlen (HTTP_PUT_RESPONSE),HTTP_PUT_RESPONSE, MHD_NO, MHD_NO);
1166 res = MHD_queue_response (mhd_connection, MHD_HTTP_OK, response);
1167 #if DEBUG_CONNECTIONS
1168 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: Sent HTTP/1.1: 200 OK as PUT Response\n",ps);
1170 MHD_destroy_response (response);
1171 ps->recv_active=GNUNET_NO;
1175 /* Recieving data */
1176 if ((*upload_data_size > 0) && (ps->recv_active == GNUNET_YES))
1178 if (pc->delay.value == 0)
1180 res = GNUNET_SERVER_mst_receive(ps->msgtok, ps, upload_data,*upload_data_size, GNUNET_NO, GNUNET_NO);
1181 (*upload_data_size) = 0;
1186 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: no inbound bandwidth available! Next read was delayed for %llu ms\n", ps, ps->peercontext->delay.value);
1194 if ( 0 == strcmp (MHD_HTTP_METHOD_GET, method) )
1196 if (ps->send_force_disconnect == GNUNET_YES)
1198 #if DEBUG_CONNECTIONS
1199 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: outbound connection was forced to disconnect\n",ps);
1201 ps->send_active = GNUNET_NO;
1204 ps->send_connected = GNUNET_YES;
1205 ps->send_active = GNUNET_YES;
1206 ps->send_endpoint = mhd_connection;
1207 ps->send_force_disconnect = GNUNET_NO;
1208 #if DEBUG_CONNECTIONS
1209 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: inbound GET connection connected\n",ps);
1211 response = MHD_create_response_from_callback(-1,32 * 1024, &mhd_send_callback, ps, NULL);
1212 res = MHD_queue_response (mhd_connection, MHD_HTTP_OK, response);
1213 MHD_destroy_response (response);
1220 * Function that queries MHD's select sets and
1221 * starts the task waiting for them.
1222 * @param plugin plugin
1223 * @param daemon_handle the MHD daemon handle
1224 * @return gnunet task identifier
1226 static GNUNET_SCHEDULER_TaskIdentifier
1227 http_server_daemon_prepare (struct Plugin *plugin , struct MHD_Daemon *daemon_handle)
1229 GNUNET_SCHEDULER_TaskIdentifier ret;
1233 struct GNUNET_NETWORK_FDSet *wrs;
1234 struct GNUNET_NETWORK_FDSet *wws;
1235 struct GNUNET_NETWORK_FDSet *wes;
1237 unsigned long long timeout;
1239 struct GNUNET_TIME_Relative tv;
1241 ret = GNUNET_SCHEDULER_NO_TASK;
1245 wrs = GNUNET_NETWORK_fdset_create ();
1246 wes = GNUNET_NETWORK_fdset_create ();
1247 wws = GNUNET_NETWORK_fdset_create ();
1249 GNUNET_assert (MHD_YES ==
1250 MHD_get_fdset (daemon_handle,
1255 haveto = MHD_get_timeout (daemon_handle, &timeout);
1256 if (haveto == MHD_YES)
1257 tv.value = (uint64_t) timeout;
1259 tv = GNUNET_TIME_UNIT_SECONDS;
1260 GNUNET_NETWORK_fdset_copy_native (wrs, &rs, max);
1261 GNUNET_NETWORK_fdset_copy_native (wws, &ws, max);
1262 GNUNET_NETWORK_fdset_copy_native (wes, &es, max);
1263 if (daemon_handle == plugin->http_server_daemon_v4)
1265 if (plugin->http_server_task_v4 != GNUNET_SCHEDULER_NO_TASK)
1267 GNUNET_SCHEDULER_cancel(plugin->env->sched, plugin->http_server_task_v4);
1268 plugin->http_server_daemon_v4 = GNUNET_SCHEDULER_NO_TASK;
1271 ret = GNUNET_SCHEDULER_add_select (plugin->env->sched,
1272 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1273 GNUNET_SCHEDULER_NO_TASK,
1277 &http_server_daemon_v4_run,
1280 if (daemon_handle == plugin->http_server_daemon_v6)
1282 if (plugin->http_server_task_v6 != GNUNET_SCHEDULER_NO_TASK)
1284 GNUNET_SCHEDULER_cancel(plugin->env->sched, plugin->http_server_task_v6);
1285 plugin->http_server_task_v6 = GNUNET_SCHEDULER_NO_TASK;
1288 ret = GNUNET_SCHEDULER_add_select (plugin->env->sched,
1289 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1290 GNUNET_SCHEDULER_NO_TASK,
1294 &http_server_daemon_v6_run,
1297 GNUNET_NETWORK_fdset_destroy (wrs);
1298 GNUNET_NETWORK_fdset_destroy (wws);
1299 GNUNET_NETWORK_fdset_destroy (wes);
1304 * Call MHD IPv4 to process pending requests and then go back
1305 * and schedule the next run.
1306 * @param cls plugin as closure
1307 * @param tc task context
1309 static void http_server_daemon_v4_run (void *cls,
1310 const struct GNUNET_SCHEDULER_TaskContext *tc)
1312 struct Plugin *plugin = cls;
1314 #if DEBUG_SCHEDULING
1315 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_READ_READY))
1316 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,"http_server_daemon_v4_run: GNUNET_SCHEDULER_REASON_READ_READY\n");
1317 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_WRITE_READY))
1318 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,"http_server_daemon_v4_run: GNUNET_SCHEDULER_REASON_WRITE_READY\n");
1319 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_TIMEOUT))
1320 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,"http_server_daemon_v4_run: GNUNET_SCHEDULER_REASON_TIMEOUT\n");
1321 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_STARTUP))
1322 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,"http_server_daemon_v4_run: GGNUNET_SCHEDULER_REASON_STARTUP\n");
1323 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1324 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,"http_server_daemon_v4_run: GGNUNET_SCHEDULER_REASON_SHUTDOWN\n");
1327 GNUNET_assert(cls !=NULL);
1328 plugin->http_server_task_v4 = GNUNET_SCHEDULER_NO_TASK;
1330 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1333 GNUNET_assert (MHD_YES == MHD_run (plugin->http_server_daemon_v4));
1334 plugin->http_server_task_v4 = http_server_daemon_prepare (plugin, plugin->http_server_daemon_v4);
1339 * Call MHD IPv6 to process pending requests and then go back
1340 * and schedule the next run.
1341 * @param cls plugin as closure
1342 * @param tc task context
1344 static void http_server_daemon_v6_run (void *cls,
1345 const struct GNUNET_SCHEDULER_TaskContext *tc)
1347 struct Plugin *plugin = cls;
1349 #if DEBUG_SCHEDULING
1350 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_READ_READY))
1351 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,"http_server_daemon_v6_run: GNUNET_SCHEDULER_REASON_READ_READY\n");
1352 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_WRITE_READY))
1353 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,"http_server_daemon_v6_run: GNUNET_SCHEDULER_REASON_WRITE_READY\n");
1354 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_TIMEOUT))
1355 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,"http_server_daemon_v6_run: GNUNET_SCHEDULER_REASON_TIMEOUT\n");
1356 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_STARTUP))
1357 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,"http_server_daemon_v6_run: GGNUNET_SCHEDULER_REASON_STARTUP\n");
1358 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1359 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,"http_server_daemon_v6_run: GGNUNET_SCHEDULER_REASON_SHUTDOWN\n");
1362 GNUNET_assert(cls !=NULL);
1363 plugin->http_server_task_v6 = GNUNET_SCHEDULER_NO_TASK;
1365 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1368 GNUNET_assert (MHD_YES == MHD_run (plugin->http_server_daemon_v6));
1369 plugin->http_server_task_v6 = http_server_daemon_prepare (plugin, plugin->http_server_daemon_v6);
1372 static size_t curl_get_header_cb( void *ptr, size_t size, size_t nmemb, void *stream)
1374 struct Session * ps = stream;
1376 long http_result = 0;
1378 /* Getting last http result code */
1379 GNUNET_assert(NULL!=ps);
1380 if (ps->recv_connected==GNUNET_NO)
1382 res = curl_easy_getinfo(ps->recv_endpoint, CURLINFO_RESPONSE_CODE, &http_result);
1383 if (CURLE_OK == res)
1385 if (http_result == 200)
1387 ps->recv_connected = GNUNET_YES;
1388 ps->recv_active = GNUNET_YES;
1389 #if DEBUG_CONNECTIONS
1390 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: connected to recieve data\n",ps);
1392 // Calling send_check_connections again since receive is established
1393 send_check_connections (ps->peercontext->plugin, ps);
1400 size_t len = size * nmemb;
1402 if ((size * nmemb) < SIZE_MAX)
1403 tmp = GNUNET_malloc (len+1);
1405 if ((tmp != NULL) && (len > 0))
1407 memcpy(tmp,ptr,len);
1410 if (tmp[len-2] == 13)
1413 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: Header: %s\n",ps,tmp);
1415 GNUNET_free_non_null (tmp);
1418 return size * nmemb;
1422 * Callback called by libcurl when new headers arrive
1423 * Used to get HTTP result for curl operations
1424 * @param ptr stream to read from
1425 * @param size size of one char element
1426 * @param nmemb number of char elements
1427 * @param stream closure set by user
1428 * @return bytes read by function
1431 static size_t curl_put_header_cb( void *ptr, size_t size, size_t nmemb, void *stream)
1433 struct Session * ps = stream;
1436 size_t len = size * nmemb;
1437 long http_result = 0;
1440 /* Getting last http result code */
1441 GNUNET_assert(NULL!=ps);
1442 res = curl_easy_getinfo(ps->send_endpoint, CURLINFO_RESPONSE_CODE, &http_result);
1443 if (CURLE_OK == res)
1445 if ((http_result == 100) && (ps->send_connected==GNUNET_NO))
1447 ps->send_connected = GNUNET_YES;
1448 ps->send_active = GNUNET_YES;
1449 #if DEBUG_CONNECTIONS
1450 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: connected to send data\n",ps);
1453 if ((http_result == 200) && (ps->send_connected==GNUNET_YES))
1455 ps->send_connected = GNUNET_NO;
1456 ps->send_active = GNUNET_NO;
1457 #if DEBUG_CONNECTIONS
1458 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: sending disconnected\n",ps);
1464 if ((size * nmemb) < SIZE_MAX)
1465 tmp = GNUNET_malloc (len+1);
1467 if ((tmp != NULL) && (len > 0))
1469 memcpy(tmp,ptr,len);
1472 if (tmp[len-2] == 13)
1477 GNUNET_free_non_null (tmp);
1479 return size * nmemb;
1483 * Callback method used with libcurl
1484 * Method is called when libcurl needs to read data during sending
1485 * @param stream pointer where to write data
1486 * @param size size of an individual element
1487 * @param nmemb count of elements that can be written to the buffer
1488 * @param ptr source pointer, passed to the libcurl handle
1489 * @return bytes written to stream
1491 static size_t curl_send_cb(void *stream, size_t size, size_t nmemb, void *ptr)
1493 struct Session * ps = ptr;
1494 struct HTTP_Message * msg = ps->pending_msgs_tail;
1498 if (ps->send_active == GNUNET_NO)
1500 return CURL_READFUNC_PAUSE;
1503 if ((ps->pending_msgs_tail == NULL) && (ps->send_active == GNUNET_YES))
1505 #if DEBUG_CONNECTIONS
1506 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: No Message to send, pausing connection\n",ps);
1508 ps->send_active = GNUNET_NO;
1509 return CURL_READFUNC_PAUSE;
1512 GNUNET_assert (msg!=NULL);
1515 if (msg->pos < msg->size)
1517 /* data fit in buffer */
1518 if ((msg->size - msg->pos) <= (size * nmemb))
1520 len = (msg->size - msg->pos);
1521 memcpy(stream, &msg->buf[msg->pos], len);
1528 memcpy(stream, &msg->buf[msg->pos], len);
1533 /* no data to send */
1539 if ( msg->pos == msg->size)
1541 #if DEBUG_CONNECTIONS
1542 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,"Connection %X: Message with %u bytes sent, removing message from queue \n",ps, msg->pos);
1544 /* Calling transmit continuation */
1545 if (NULL != ps->pending_msgs_tail->transmit_cont)
1546 msg->transmit_cont (ps->pending_msgs_tail->transmit_cont_cls,&(ps->peercontext)->identity,GNUNET_OK);
1547 ps->queue_length_cur -= msg->size;
1548 remove_http_message(ps, msg);
1553 static void curl_receive_mst_cb (void *cls,
1555 const struct GNUNET_MessageHeader *message)
1557 struct Session *ps = cls;
1558 struct GNUNET_TIME_Relative delay;
1559 GNUNET_assert(ps != NULL);
1561 struct HTTP_PeerContext *pc = ps->peercontext;
1562 GNUNET_assert(pc != NULL);
1564 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1565 "Connection %X: Forwarding message to transport service, type %u and size %u from `%s' (`%s')\n",
1567 ntohs(message->type),
1568 ntohs(message->size),
1569 GNUNET_i2s(&(pc->identity)),http_plugin_address_to_string(NULL,ps->addr,ps->addrlen));
1571 delay = pc->plugin->env->receive (pc->plugin->env->cls,
1578 if (pc->reset_task != GNUNET_SCHEDULER_NO_TASK)
1579 GNUNET_SCHEDULER_cancel (pc->plugin->env->sched, pc->reset_task);
1581 if (delay.value > 0)
1584 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,"Connection %X: Inbound quota management: delay next read for %llu ms \n", ps, delay.value);
1586 pc->reset_task = GNUNET_SCHEDULER_add_delayed (pc->plugin->env->sched, delay, &reset_inbound_quota_delay, pc);
1592 * Callback method used with libcurl
1593 * Method is called when libcurl needs to write data during sending
1594 * @param stream pointer where to write data
1595 * @param size size of an individual element
1596 * @param nmemb count of elements that can be written to the buffer
1597 * @param ptr destination pointer, passed to the libcurl handle
1598 * @return bytes read from stream
1600 static size_t curl_receive_cb( void *stream, size_t size, size_t nmemb, void *ptr)
1602 struct Session * ps = ptr;
1604 if (ps->peercontext->delay.value > 0)
1607 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: no inbound bandwidth available! Next read was delayed for %llu ms\n", ps, ps->peercontext->delay.value);
1612 #if DEBUG_CONNECTIONS
1613 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: %u bytes received\n",ps, size*nmemb);
1615 GNUNET_SERVER_mst_receive(ps->msgtok, ps, stream, size*nmemb, GNUNET_NO, GNUNET_NO);
1616 return (size * nmemb);
1620 static void curl_handle_finished (struct Plugin *plugin)
1622 struct Session *ps = NULL;
1623 struct HTTP_PeerContext *pc = NULL;
1624 struct CURLMsg *msg;
1625 struct HTTP_Message * cur_msg = NULL;
1633 msg = curl_multi_info_read (plugin->multi_handle, &msgs_in_queue);
1634 if ((msgs_in_queue == 0) || (msg == NULL))
1636 /* get session for affected curl handle */
1637 GNUNET_assert ( msg->easy_handle != NULL );
1638 curl_easy_getinfo(msg->easy_handle, CURLINFO_PRIVATE, &tmp);
1639 ps = (struct Session *) tmp;
1640 GNUNET_assert ( ps != NULL );
1641 pc = ps->peercontext;
1642 GNUNET_assert ( pc != NULL );
1647 if ( (msg->data.result != CURLE_OK) &&
1648 (msg->data.result != CURLE_GOT_NOTHING) )
1650 /* sending msg failed*/
1651 if (msg->easy_handle == ps->send_endpoint)
1653 #if DEBUG_CONNECTIONS
1654 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1655 _("Connection %X: HTTP PUT to peer `%s' (`%s') failed: `%s' `%s'\n"),
1657 GNUNET_i2s(&pc->identity),
1658 http_plugin_address_to_string(NULL, ps->addr, ps->addrlen),
1659 "curl_multi_perform",
1660 curl_easy_strerror (msg->data.result));
1662 ps->send_connected = GNUNET_NO;
1663 ps->send_active = GNUNET_NO;
1664 curl_multi_remove_handle(plugin->multi_handle,ps->send_endpoint);
1665 //curl_easy_cleanup(ps->send_endpoint);
1666 //ps->send_endpoint=NULL;
1667 while (ps->pending_msgs_tail != NULL)
1669 cur_msg = ps->pending_msgs_tail;
1670 if ( NULL != cur_msg->transmit_cont)
1671 cur_msg->transmit_cont (cur_msg->transmit_cont_cls,&pc->identity,GNUNET_SYSERR);
1672 ps->queue_length_cur -= cur_msg->size;
1673 remove_http_message(ps,cur_msg);
1676 /* GET connection failed */
1677 if (msg->easy_handle == ps->recv_endpoint)
1679 #if DEBUG_CONNECTIONS
1680 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1681 _("Connection %X: HTTP GET to peer `%s' (`%s') failed: `%s' `%s'\n"),
1683 GNUNET_i2s(&pc->identity),
1684 http_plugin_address_to_string(NULL, ps->addr, ps->addrlen),
1685 "curl_multi_perform",
1686 curl_easy_strerror (msg->data.result));
1688 ps->recv_connected = GNUNET_NO;
1689 ps->recv_active = GNUNET_NO;
1690 curl_multi_remove_handle(plugin->multi_handle,ps->recv_endpoint);
1691 //curl_easy_cleanup(ps->recv_endpoint);
1692 //ps->recv_endpoint=NULL;
1697 if (msg->easy_handle == ps->send_endpoint)
1699 GNUNET_assert (CURLE_OK == curl_easy_getinfo(msg->easy_handle, CURLINFO_RESPONSE_CODE, &http_result));
1700 #if DEBUG_CONNECTIONS
1701 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1702 "Connection %X: HTTP PUT connection to peer `%s' (`%s') was closed with HTTP code %u\n",
1704 GNUNET_i2s(&pc->identity),
1705 http_plugin_address_to_string(NULL, ps->addr, ps->addrlen),
1708 /* Calling transmit continuation */
1709 while (ps->pending_msgs_tail != NULL)
1711 cur_msg = ps->pending_msgs_tail;
1712 if ( NULL != cur_msg->transmit_cont)
1714 /* HTTP 1xx : Last message before here was informational */
1715 if ((http_result >=100) && (http_result < 200))
1716 cur_msg->transmit_cont (cur_msg->transmit_cont_cls,&pc->identity,GNUNET_OK);
1717 /* HTTP 2xx: successful operations */
1718 if ((http_result >=200) && (http_result < 300))
1719 cur_msg->transmit_cont (cur_msg->transmit_cont_cls,&pc->identity,GNUNET_OK);
1720 /* HTTP 3xx..5xx: error */
1721 if ((http_result >=300) && (http_result < 600))
1722 cur_msg->transmit_cont (cur_msg->transmit_cont_cls,&pc->identity,GNUNET_SYSERR);
1724 ps->queue_length_cur -= cur_msg->size;
1725 remove_http_message(ps,cur_msg);
1728 ps->send_connected = GNUNET_NO;
1729 ps->send_active = GNUNET_NO;
1730 curl_multi_remove_handle(plugin->multi_handle,ps->send_endpoint);
1731 //curl_easy_cleanup(ps->send_endpoint);
1732 //ps->send_endpoint =NULL;
1734 if (msg->easy_handle == ps->recv_endpoint)
1736 #if DEBUG_CONNECTIONS
1737 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1738 "Connection %X: HTTP GET connection to peer `%s' (`%s') was closed with HTTP code %u\n",
1740 GNUNET_i2s(&pc->identity),
1741 http_plugin_address_to_string(NULL, ps->addr, ps->addrlen),
1744 ps->recv_connected = GNUNET_NO;
1745 ps->recv_active = GNUNET_NO;
1746 curl_multi_remove_handle(plugin->multi_handle,ps->recv_endpoint);
1747 //curl_easy_cleanup(ps->recv_endpoint);
1748 //ps->recv_endpoint=NULL;
1751 if ((ps->recv_connected == GNUNET_NO) && (ps->send_connected == GNUNET_NO))
1752 remove_session (pc, ps, GNUNET_YES, GNUNET_SYSERR);
1758 while ( (msgs_in_queue > 0) );
1763 * Task performing curl operations
1764 * @param cls plugin as closure
1765 * @param tc gnunet scheduler task context
1767 static void curl_perform (void *cls,
1768 const struct GNUNET_SCHEDULER_TaskContext *tc)
1770 struct Plugin *plugin = cls;
1771 static unsigned int handles_last_run;
1775 GNUNET_assert(cls !=NULL);
1777 plugin->http_curl_task = GNUNET_SCHEDULER_NO_TASK;
1778 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1783 mret = curl_multi_perform (plugin->multi_handle, &running);
1784 if ((running < handles_last_run) && (running>0))
1785 curl_handle_finished(plugin);
1786 handles_last_run = running;
1788 while (mret == CURLM_CALL_MULTI_PERFORM);
1789 curl_schedule(plugin);
1794 * Function setting up file descriptors and scheduling task to run
1796 * @param plugin plugin as closure
1797 * @return GNUNET_SYSERR for hard failure, GNUNET_OK for ok
1799 static int curl_schedule(struct Plugin *plugin)
1805 struct GNUNET_NETWORK_FDSet *grs;
1806 struct GNUNET_NETWORK_FDSet *gws;
1810 /* Cancel previous scheduled task */
1811 if (plugin->http_curl_task != GNUNET_SCHEDULER_NO_TASK)
1813 GNUNET_SCHEDULER_cancel(plugin->env->sched, plugin->http_curl_task);
1814 plugin->http_curl_task = GNUNET_SCHEDULER_NO_TASK;
1821 mret = curl_multi_fdset (plugin->multi_handle, &rs, &ws, &es, &max);
1822 if (mret != CURLM_OK)
1824 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1825 _("%s failed at %s:%d: `%s'\n"),
1826 "curl_multi_fdset", __FILE__, __LINE__,
1827 curl_multi_strerror (mret));
1828 return GNUNET_SYSERR;
1830 mret = curl_multi_timeout (plugin->multi_handle, &to);
1831 if (mret != CURLM_OK)
1833 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1834 _("%s failed at %s:%d: `%s'\n"),
1835 "curl_multi_timeout", __FILE__, __LINE__,
1836 curl_multi_strerror (mret));
1837 return GNUNET_SYSERR;
1840 grs = GNUNET_NETWORK_fdset_create ();
1841 gws = GNUNET_NETWORK_fdset_create ();
1842 GNUNET_NETWORK_fdset_copy_native (grs, &rs, max + 1);
1843 GNUNET_NETWORK_fdset_copy_native (gws, &ws, max + 1);
1844 plugin->http_curl_task = GNUNET_SCHEDULER_add_select (plugin->env->sched,
1845 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1846 GNUNET_SCHEDULER_NO_TASK,
1847 (to == -1) ? GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5) : GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, to),
1852 GNUNET_NETWORK_fdset_destroy (gws);
1853 GNUNET_NETWORK_fdset_destroy (grs);
1858 * Function to log curl debug messages with GNUNET_log
1859 * @param curl handle
1860 * @param type curl_infotype
1863 * @param cls closure
1866 int curl_logger (CURL * curl, curl_infotype type , char * data, size_t size , void * cls)
1869 if (type == CURLINFO_TEXT)
1872 memcpy(text,data,size);
1873 if (text[size-1] == '\n')
1878 text[size+1] = '\0';
1880 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"CURL: Connection %X - %s", cls, text);
1886 * Function setting up curl handle and selecting message to send
1888 * @param plugin plugin
1890 * @return GNUNET_SYSERR on failure, GNUNET_NO if connecting, GNUNET_YES if ok
1892 static int send_check_connections (struct Plugin *plugin, struct Session *ps)
1895 struct HTTP_Message * msg;
1897 struct GNUNET_TIME_Relative timeout = GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT;
1899 if (ps->direction == OUTBOUND)
1901 /* RECV DIRECTION */
1902 /* Check if session is connected to receive data, otherwise connect to peer */
1903 if (ps->recv_connected == GNUNET_NO)
1905 int fresh = GNUNET_NO;
1906 if (ps->recv_endpoint == NULL)
1909 ps->recv_endpoint = curl_easy_init();
1912 curl_easy_setopt(ps->recv_endpoint, CURLOPT_VERBOSE, 1L);
1913 curl_easy_setopt(ps->recv_endpoint, CURLOPT_DEBUGFUNCTION , &curl_logger);
1914 curl_easy_setopt(ps->recv_endpoint, CURLOPT_DEBUGDATA , ps->recv_endpoint);
1917 curl_easy_setopt (ps->recv_endpoint, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1);
1918 curl_easy_setopt(ps->recv_endpoint, CURLOPT_SSL_VERIFYPEER, 0);
1919 curl_easy_setopt(ps->recv_endpoint, CURLOPT_SSL_VERIFYHOST, 0);
1921 curl_easy_setopt(ps->recv_endpoint, CURLOPT_URL, ps->url);
1922 curl_easy_setopt(ps->recv_endpoint, CURLOPT_HEADERFUNCTION, &curl_get_header_cb);
1923 curl_easy_setopt(ps->recv_endpoint, CURLOPT_WRITEHEADER, ps);
1924 curl_easy_setopt(ps->recv_endpoint, CURLOPT_READFUNCTION, curl_send_cb);
1925 curl_easy_setopt(ps->recv_endpoint, CURLOPT_READDATA, ps);
1926 curl_easy_setopt(ps->recv_endpoint, CURLOPT_WRITEFUNCTION, curl_receive_cb);
1927 curl_easy_setopt(ps->recv_endpoint, CURLOPT_WRITEDATA, ps);
1928 curl_easy_setopt(ps->recv_endpoint, CURLOPT_TIMEOUT, (long) timeout.value);
1929 curl_easy_setopt(ps->recv_endpoint, CURLOPT_PRIVATE, ps);
1930 curl_easy_setopt(ps->recv_endpoint, CURLOPT_CONNECTTIMEOUT, HTTP_CONNECT_TIMEOUT);
1931 curl_easy_setopt(ps->recv_endpoint, CURLOPT_BUFFERSIZE, 2*GNUNET_SERVER_MAX_MESSAGE_SIZE);
1932 #if CURL_TCP_NODELAY
1933 curl_easy_setopt(ps->recv_endpoint, CURLOPT_TCP_NODELAY, 1);
1936 if (fresh==GNUNET_YES)
1938 mret = curl_multi_add_handle(plugin->multi_handle, ps->recv_endpoint);
1939 if (mret != CURLM_OK)
1941 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1942 _("Connection: %X: %s failed at %s:%d: `%s'\n"),
1944 "curl_multi_add_handle", __FILE__, __LINE__,
1945 curl_multi_strerror (mret));
1946 return GNUNET_SYSERR;
1949 if (plugin->http_curl_task != GNUNET_SCHEDULER_NO_TASK)
1951 GNUNET_SCHEDULER_cancel(plugin->env->sched, plugin->http_curl_task);
1952 plugin->http_curl_task = GNUNET_SCHEDULER_NO_TASK;
1954 plugin->http_curl_task = GNUNET_SCHEDULER_add_now (plugin->env->sched, &curl_perform, plugin);
1957 /* waiting for receive direction */
1958 if (ps->recv_connected==GNUNET_NO)
1961 /* SEND DIRECTION */
1962 /* Check if session is connected to send data, otherwise connect to peer */
1963 if ((ps->send_connected == GNUNET_YES) && (ps->send_endpoint!= NULL))
1965 if (ps->send_active == GNUNET_YES)
1967 #if DEBUG_CONNECTIONS
1968 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: outbound active, enqueueing message\n",ps);
1972 if (ps->send_active == GNUNET_NO)
1974 #if DEBUG_CONNECTIONS
1975 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: outbound paused, unpausing existing connection and enqueueing message\n",ps);
1977 if (CURLE_OK == curl_easy_pause(ps->send_endpoint,CURLPAUSE_CONT))
1979 ps->send_active=GNUNET_YES;
1980 if (plugin->http_curl_task != GNUNET_SCHEDULER_NO_TASK)
1982 GNUNET_SCHEDULER_cancel(plugin->env->sched, plugin->http_curl_task);
1983 plugin->http_curl_task = GNUNET_SCHEDULER_NO_TASK;
1985 plugin->http_curl_task = GNUNET_SCHEDULER_add_now (plugin->env->sched, &curl_perform, plugin);
1989 return GNUNET_SYSERR;
1992 /* not connected, initiate connection */
1993 if (ps->send_connected==GNUNET_NO)
1995 int fresh = GNUNET_NO;
1996 if (NULL == ps->send_endpoint)
1998 ps->send_endpoint = curl_easy_init();
2001 GNUNET_assert (ps->send_endpoint != NULL);
2002 GNUNET_assert (NULL != ps->pending_msgs_tail);
2003 #if DEBUG_CONNECTIONS
2004 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: outbound not connected, initiating connection\n",ps);
2006 ps->send_active = GNUNET_NO;
2007 msg = ps->pending_msgs_tail;
2010 curl_easy_setopt(ps->send_endpoint, CURLOPT_VERBOSE, 1L);
2011 curl_easy_setopt(ps->send_endpoint, CURLOPT_DEBUGFUNCTION , &curl_logger);
2012 curl_easy_setopt(ps->send_endpoint, CURLOPT_DEBUGDATA , ps->send_endpoint);
2015 curl_easy_setopt (ps->send_endpoint, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1);
2016 curl_easy_setopt(ps->send_endpoint, CURLOPT_SSL_VERIFYPEER, 0);
2017 curl_easy_setopt(ps->send_endpoint, CURLOPT_SSL_VERIFYHOST, 0);
2019 curl_easy_setopt(ps->send_endpoint, CURLOPT_URL, ps->url);
2020 curl_easy_setopt(ps->send_endpoint, CURLOPT_PUT, 1L);
2021 curl_easy_setopt(ps->send_endpoint, CURLOPT_HEADERFUNCTION, &curl_put_header_cb);
2022 curl_easy_setopt(ps->send_endpoint, CURLOPT_WRITEHEADER, ps);
2023 curl_easy_setopt(ps->send_endpoint, CURLOPT_READFUNCTION, curl_send_cb);
2024 curl_easy_setopt(ps->send_endpoint, CURLOPT_READDATA, ps);
2025 curl_easy_setopt(ps->send_endpoint, CURLOPT_WRITEFUNCTION, curl_receive_cb);
2026 curl_easy_setopt(ps->send_endpoint, CURLOPT_READDATA, ps);
2027 curl_easy_setopt(ps->send_endpoint, CURLOPT_TIMEOUT, (long) timeout.value);
2028 curl_easy_setopt(ps->send_endpoint, CURLOPT_PRIVATE, ps);
2029 curl_easy_setopt(ps->send_endpoint, CURLOPT_CONNECTTIMEOUT, HTTP_CONNECT_TIMEOUT);
2030 curl_easy_setopt(ps->send_endpoint, CURLOPT_BUFFERSIZE, 2 * GNUNET_SERVER_MAX_MESSAGE_SIZE);
2031 #if CURL_TCP_NODELAY
2032 curl_easy_setopt(ps->send_endpoint, CURLOPT_TCP_NODELAY, 1);
2035 if (fresh==GNUNET_YES)
2037 mret = curl_multi_add_handle(plugin->multi_handle, ps->send_endpoint);
2038 if (mret != CURLM_OK)
2040 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2041 _("Connection: %X: %s failed at %s:%d: `%s'\n"),
2043 "curl_multi_add_handle", __FILE__, __LINE__,
2044 curl_multi_strerror (mret));
2045 return GNUNET_SYSERR;
2049 if (plugin->http_curl_task != GNUNET_SCHEDULER_NO_TASK)
2051 GNUNET_SCHEDULER_cancel(plugin->env->sched, plugin->http_curl_task);
2052 plugin->http_curl_task = GNUNET_SCHEDULER_NO_TASK;
2054 plugin->http_curl_task = GNUNET_SCHEDULER_add_now (plugin->env->sched, &curl_perform, plugin);
2057 if (ps->direction == INBOUND)
2059 GNUNET_assert (NULL != ps->pending_msgs_tail);
2060 if ((ps->recv_connected==GNUNET_YES) && (ps->send_connected==GNUNET_YES) &&
2061 (ps->recv_force_disconnect==GNUNET_NO) && (ps->recv_force_disconnect==GNUNET_NO))
2064 return GNUNET_SYSERR;
2068 * select best session to transmit data to peer
2070 * @param pc peer context of target peer
2071 * @param addr address of target peer
2072 * @param addrlen address length
2073 * @param force_address does transport service enforce address?
2074 * @param session session passed by transport service
2075 * @return selected session
2078 static struct Session * send_select_session (struct HTTP_PeerContext *pc, const void * addr, size_t addrlen, int force_address, struct Session * session)
2080 struct Session * tmp = NULL;
2081 int addr_given = GNUNET_NO;
2083 if ((addr!=NULL) && (addrlen>0))
2084 addr_given = GNUNET_YES;
2086 if (force_address == GNUNET_YES)
2088 /* check session given as argument */
2089 if ((session != NULL) && (addr_given == GNUNET_YES))
2091 if (0 == memcmp(session->addr, addr, addrlen))
2093 /* connection can not be used, since it is disconnected */
2094 if ((session->recv_force_disconnect==GNUNET_NO) && (session->send_force_disconnect==GNUNET_NO))
2096 #if DEBUG_SESSION_SELECTION
2097 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Session %X selected: Using session passed by transport to send to forced address \n", session);
2103 /* check last session used */
2104 if ((pc->last_session != NULL)&& (addr_given == GNUNET_YES))
2106 if (0 == memcmp(pc->last_session->addr, addr, addrlen))
2108 /* connection can not be used, since it is disconnected */
2109 if ((pc->last_session->recv_force_disconnect==GNUNET_NO) && (pc->last_session->send_force_disconnect==GNUNET_NO))
2111 #if DEBUG_SESSION_SELECTION
2112 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Session %X selected: Using last session used to send to forced address \n", pc->last_session);
2114 return pc->last_session;
2118 /* find session in existing sessions */
2120 while ((tmp!=NULL) && (addr_given == GNUNET_YES))
2123 if (0 == memcmp(tmp->addr, addr, addrlen))
2125 /* connection can not be used, since it is disconnected */
2126 if ((tmp->recv_force_disconnect==GNUNET_NO) && (tmp->send_force_disconnect==GNUNET_NO))
2128 #if DEBUG_SESSION_SELECTION
2129 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Session %X selected: Using existing session to send to forced address \n", session);
2137 /* no session to use */
2140 if ((force_address == GNUNET_NO) || (force_address == GNUNET_SYSERR))
2142 /* check session given as argument */
2143 if (session != NULL)
2145 /* connection can not be used, since it is disconnected */
2146 if ((session->recv_force_disconnect==GNUNET_NO) && (session->send_force_disconnect==GNUNET_NO))
2148 #if DEBUG_SESSION_SELECTION
2149 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Session %X selected: Using session passed by transport to send not-forced address \n", session);
2155 /* check last session used */
2156 if (pc->last_session != NULL)
2158 /* connection can not be used, since it is disconnected */
2159 if ((pc->last_session->recv_force_disconnect==GNUNET_NO) && (pc->last_session->send_force_disconnect==GNUNET_NO))
2161 #if DEBUG_SESSION_SELECTION
2162 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Session %X selected: Using last session to send to not-forced address \n", pc->last_session);
2164 return pc->last_session;
2167 /* find session in existing sessions */
2171 /* connection can not be used, since it is disconnected */
2172 if ((tmp->recv_force_disconnect==GNUNET_NO) && (tmp->send_force_disconnect==GNUNET_NO))
2174 #if DEBUG_SESSION_SELECTION
2175 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Session %X selected: Using existing session to send to not-forced address \n", tmp);
2187 * Function that can be used by the transport service to transmit
2188 * a message using the plugin. Note that in the case of a
2189 * peer disconnecting, the continuation MUST be called
2190 * prior to the disconnect notification itself. This function
2191 * will be called with this peer's HELLO message to initiate
2192 * a fresh connection to another peer.
2194 * @param cls closure
2195 * @param target who should receive this message
2196 * @param msgbuf the message to transmit
2197 * @param msgbuf_size number of bytes in 'msgbuf'
2198 * @param priority how important is the message (most plugins will
2199 * ignore message priority and just FIFO)
2200 * @param to how long to wait at most for the transmission (does not
2201 * require plugins to discard the message after the timeout,
2202 * just advisory for the desired delay; most plugins will ignore
2204 * @param session which session must be used (or NULL for "any")
2205 * @param addr the address to use (can be NULL if the plugin
2206 * is "on its own" (i.e. re-use existing TCP connection))
2207 * @param addrlen length of the address in bytes
2208 * @param force_address GNUNET_YES if the plugin MUST use the given address,
2209 * GNUNET_NO means the plugin may use any other address and
2210 * GNUNET_SYSERR means that only reliable existing
2211 * bi-directional connections should be used (regardless
2213 * @param cont continuation to call once the message has
2214 * been transmitted (or if the transport is ready
2215 * for the next transmission call; or if the
2216 * peer disconnected...); can be NULL
2217 * @param cont_cls closure for cont
2218 * @return number of bytes used (on the physical network, with overheads);
2219 * -1 on hard errors (i.e. address invalid); 0 is a legal value
2220 * and does NOT mean that the message was not transmitted (DV)
2223 http_plugin_send (void *cls,
2224 const struct GNUNET_PeerIdentity *target,
2227 unsigned int priority,
2228 struct GNUNET_TIME_Relative to,
2229 struct Session *session,
2233 GNUNET_TRANSPORT_TransmitContinuation cont,
2236 struct Plugin *plugin = cls;
2237 struct HTTP_Message *msg;
2238 struct HTTP_PeerContext * pc;
2239 struct Session * ps = NULL;
2241 GNUNET_assert(cls !=NULL);
2245 if (force_address == GNUNET_YES)
2246 GNUNET_asprintf(&force, "forced addr.");
2247 if (force_address == GNUNET_NO)
2248 GNUNET_asprintf(&force, "any addr.");
2249 if (force_address == GNUNET_SYSERR)
2250 GNUNET_asprintf(&force,"reliable bi-direc. address addr.");
2252 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Transport tells me to send %u bytes to `%s' using %s (%s) and session: %X\n",
2256 http_plugin_address_to_string(NULL, addr, addrlen),
2262 pc = GNUNET_CONTAINER_multihashmap_get (plugin->peers, &target->hashPubKey);
2266 pc = GNUNET_malloc(sizeof (struct HTTP_PeerContext));
2267 pc->plugin = plugin;
2268 pc->session_id_counter=1;
2269 pc->last_session = NULL;
2270 memcpy(&pc->identity, target, sizeof(struct GNUNET_PeerIdentity));
2271 GNUNET_CONTAINER_multihashmap_put(plugin->peers, &pc->identity.hashPubKey, pc, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
2272 GNUNET_STATISTICS_update (plugin->env->stats,
2273 gettext_noop ("# HTTP peers active"),
2278 ps = send_select_session (pc, addr, addrlen, force_address, session);
2280 /* session not existing, but address forced -> creating new session */
2283 if ((addr!=NULL) && (addrlen!=0))
2285 ps = GNUNET_malloc(sizeof (struct Session));
2286 #if DEBUG_SESSION_SELECTION
2287 if (force_address == GNUNET_YES)
2288 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"No existing connection & forced address: creating new session %X to peer %s\n", ps, GNUNET_i2s(target));
2289 if (force_address != GNUNET_YES)
2290 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"No existing connection: creating new session %X to peer %s\n", ps, GNUNET_i2s(target));
2292 if ((addrlen!=0) && (addr!=NULL))
2294 ps->addr = GNUNET_malloc(addrlen);
2295 memcpy(ps->addr,addr,addrlen);
2296 ps->addrlen = addrlen;
2303 ps->direction=OUTBOUND;
2304 ps->recv_connected = GNUNET_NO;
2305 ps->recv_force_disconnect = GNUNET_NO;
2306 ps->send_connected = GNUNET_NO;
2307 ps->send_force_disconnect = GNUNET_NO;
2308 ps->pending_msgs_head = NULL;
2309 ps->pending_msgs_tail = NULL;
2311 ps->session_id = pc->session_id_counter;
2312 ps->queue_length_cur = 0;
2313 ps->queue_length_max = GNUNET_SERVER_MAX_MESSAGE_SIZE;
2314 pc->session_id_counter++;
2315 ps->url = create_url (plugin, ps->addr, ps->addrlen, ps->session_id);
2316 if (ps->msgtok == NULL)
2317 ps->msgtok = GNUNET_SERVER_mst_create (&curl_receive_mst_cb, ps);
2318 GNUNET_CONTAINER_DLL_insert(pc->head,pc->tail,ps);
2319 GNUNET_STATISTICS_update (plugin->env->stats,
2320 gettext_noop ("# HTTP outbound sessions for peers active"),
2327 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"No existing session found & and no address given: no way to send this message to peer `%s'!\n", GNUNET_i2s(target));
2329 return GNUNET_SYSERR;
2333 if (msgbuf_size >= (ps->queue_length_max - ps->queue_length_cur))
2335 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Queue %X full: %u bytes in queue available, message with %u is too big\n", ps, (ps->queue_length_max - ps->queue_length_cur), msgbuf_size);
2336 //return GNUNET_SYSERR;
2340 msg = GNUNET_malloc (sizeof (struct HTTP_Message) + msgbuf_size);
2342 msg->size = msgbuf_size;
2344 msg->buf = (char *) &msg[1];
2345 msg->transmit_cont = cont;
2346 msg->transmit_cont_cls = cont_cls;
2347 memcpy (msg->buf,msgbuf, msgbuf_size);
2349 GNUNET_CONTAINER_DLL_insert(ps->pending_msgs_head,ps->pending_msgs_tail,msg);
2350 ps->queue_length_cur += msgbuf_size;
2352 if (send_check_connections (plugin, ps) == GNUNET_SYSERR)
2353 return GNUNET_SYSERR;
2354 if (force_address != GNUNET_YES)
2355 pc->last_session = ps;
2357 if (pc->last_session==NULL)
2358 pc->last_session = ps;
2365 * Function that can be used to force the plugin to disconnect
2366 * from the given peer and cancel all previous transmissions
2367 * (and their continuationc).
2369 * @param cls closure
2370 * @param target peer from which to disconnect
2373 http_plugin_disconnect (void *cls,
2374 const struct GNUNET_PeerIdentity *target)
2378 struct Plugin *plugin = cls;
2379 struct HTTP_PeerContext *pc = NULL;
2380 struct Session *ps = NULL;
2381 //struct Session *tmp = NULL;
2383 pc = GNUNET_CONTAINER_multihashmap_get (plugin->peers, &target->hashPubKey);
2390 /* Telling transport that session is getting disconnected */
2391 plugin->env->session_end(plugin, target, ps);
2392 if (ps->direction==OUTBOUND)
2394 if (ps->send_endpoint!=NULL)
2396 //GNUNET_assert(CURLM_OK == curl_multi_remove_handle(plugin->multi_handle,ps->send_endpoint));
2397 //curl_easy_cleanup(ps->send_endpoint);
2398 //ps->send_endpoint=NULL;
2399 ps->send_force_disconnect = GNUNET_YES;
2401 if (ps->recv_endpoint!=NULL)
2403 //GNUNET_assert(CURLM_OK == curl_multi_remove_handle(plugin->multi_handle,ps->recv_endpoint));
2404 //curl_easy_cleanup(ps->recv_endpoint);
2405 //ps->recv_endpoint=NULL;
2406 ps->recv_force_disconnect = GNUNET_YES;
2410 if (ps->direction==INBOUND)
2412 ps->recv_force_disconnect = GNUNET_YES;
2413 ps->send_force_disconnect = GNUNET_YES;
2416 while (ps->pending_msgs_head!=NULL)
2418 remove_http_message(ps, ps->pending_msgs_head);
2420 ps->recv_active = GNUNET_NO;
2421 ps->send_active = GNUNET_NO;
2428 * Convert the transports address to a nice, human-readable
2431 * @param cls closure
2432 * @param type name of the transport that generated the address
2433 * @param addr one of the addresses of the host, NULL for the last address
2434 * the specific address format depends on the transport
2435 * @param addrlen length of the address
2436 * @param numeric should (IP) addresses be displayed in numeric form?
2437 * @param timeout after how long should we give up?
2438 * @param asc function to call on each string
2439 * @param asc_cls closure for asc
2442 http_plugin_address_pretty_printer (void *cls,
2447 struct GNUNET_TIME_Relative timeout,
2448 GNUNET_TRANSPORT_AddressStringCallback
2451 const struct IPv4HttpAddress *t4;
2452 const struct IPv6HttpAddress *t6;
2453 struct sockaddr_in a4;
2454 struct sockaddr_in6 a6;
2460 GNUNET_assert(cls !=NULL);
2461 if (addrlen == sizeof (struct IPv6HttpAddress))
2463 address = GNUNET_malloc (INET6_ADDRSTRLEN);
2465 a6.sin6_addr = t6->ipv6_addr;
2466 inet_ntop(AF_INET6, &(a6.sin6_addr),address,INET6_ADDRSTRLEN);
2467 port = ntohs(t6->u6_port);
2469 else if (addrlen == sizeof (struct IPv4HttpAddress))
2471 address = GNUNET_malloc (INET_ADDRSTRLEN);
2473 a4.sin_addr.s_addr = t4->ipv4_addr;
2474 inet_ntop(AF_INET, &(a4.sin_addr),address,INET_ADDRSTRLEN);
2475 port = ntohs(t4->u_port);
2479 /* invalid address */
2480 GNUNET_break_op (0);
2481 asc (asc_cls, NULL);
2484 res = GNUNET_asprintf(&ret,"%s://%s:%u/", PROTOCOL_PREFIX, address, port);
2485 GNUNET_free (address);
2486 GNUNET_assert(res != 0);
2488 GNUNET_free_non_null (ret);
2494 * Another peer has suggested an address for this
2495 * peer and transport plugin. Check that this could be a valid
2496 * address. If so, consider adding it to the list
2499 * @param cls closure
2500 * @param addr pointer to the address
2501 * @param addrlen length of addr
2502 * @return GNUNET_OK if this is a plausible address for this peer
2506 http_plugin_address_suggested (void *cls,
2507 const void *addr, size_t addrlen)
2509 struct Plugin *plugin = cls;
2510 struct IPv4HttpAddress *v4;
2511 struct IPv6HttpAddress *v6;
2513 struct IPv4HttpAddress *tv4 = plugin->ipv4_addr_head;
2514 struct IPv6HttpAddress *tv6 = plugin->ipv6_addr_head;
2516 GNUNET_assert(cls !=NULL);
2517 if ((addrlen != sizeof (struct IPv4HttpAddress)) &&
2518 (addrlen != sizeof (struct IPv6HttpAddress)))
2520 return GNUNET_SYSERR;
2522 if (addrlen == sizeof (struct IPv4HttpAddress))
2524 v4 = (struct IPv4HttpAddress *) addr;
2526 if (plugin->bind4_address!=NULL)
2528 if (0 == memcmp (&plugin->bind4_address->sin_addr, &v4->ipv4_addr, sizeof(uint32_t)))
2531 return GNUNET_SYSERR;
2535 if (0==memcmp (&tv4->ipv4_addr, &v4->ipv4_addr, sizeof(uint32_t)))
2542 return GNUNET_SYSERR;
2544 if (addrlen == sizeof (struct IPv6HttpAddress))
2546 v6 = (struct IPv6HttpAddress *) addr;
2548 if (plugin->bind6_address!=NULL)
2550 if (0 == memcmp (&plugin->bind6_address->sin6_addr, &v6->ipv6_addr, sizeof(struct in6_addr)))
2553 return GNUNET_SYSERR;
2558 if (0 == memcmp (&tv6->ipv6_addr, &v6->ipv6_addr, sizeof(struct in6_addr)))
2565 return GNUNET_SYSERR;
2568 return GNUNET_SYSERR;
2573 * Function called for a quick conversion of the binary address to
2574 * a numeric address. Note that the caller must not free the
2575 * address and that the next call to this function is allowed
2576 * to override the address again.
2578 * @param cls closure
2579 * @param addr binary address
2580 * @param addrlen length of the address
2581 * @return string representing the same address
2584 http_plugin_address_to_string (void *cls,
2588 const struct IPv4HttpAddress *t4;
2589 const struct IPv6HttpAddress *t6;
2590 struct sockaddr_in a4;
2591 struct sockaddr_in6 a6;
2597 if (addrlen == sizeof (struct IPv6HttpAddress))
2599 address = GNUNET_malloc (INET6_ADDRSTRLEN);
2601 a6.sin6_addr = t6->ipv6_addr;
2602 inet_ntop(AF_INET6, &(a6.sin6_addr),address,INET6_ADDRSTRLEN);
2603 port = ntohs(t6->u6_port);
2605 else if (addrlen == sizeof (struct IPv4HttpAddress))
2607 address = GNUNET_malloc (INET_ADDRSTRLEN);
2609 a4.sin_addr.s_addr = t4->ipv4_addr;
2610 inet_ntop(AF_INET, &(a4.sin_addr),address,INET_ADDRSTRLEN);
2611 port = ntohs(t4->u_port);
2615 /* invalid address */
2618 res = GNUNET_asprintf(&ret,"%s:%u",address,port);
2619 GNUNET_free (address);
2620 GNUNET_assert(res != 0);
2626 * Exit point from the plugin.
2629 LIBGNUNET_PLUGIN_TRANSPORT_DONE (void *cls)
2631 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
2632 struct Plugin *plugin = api->cls;
2634 struct IPv4HttpAddress * ipv4addr;
2635 struct IPv6HttpAddress * ipv6addr;
2636 GNUNET_assert(cls !=NULL);
2638 if (plugin->http_server_daemon_v4 != NULL)
2640 MHD_stop_daemon (plugin->http_server_daemon_v4);
2641 plugin->http_server_daemon_v4 = NULL;
2643 if (plugin->http_server_daemon_v6 != NULL)
2645 MHD_stop_daemon (plugin->http_server_daemon_v6);
2646 plugin->http_server_daemon_v6 = NULL;
2649 if ( plugin->http_server_task_v4 != GNUNET_SCHEDULER_NO_TASK)
2651 GNUNET_SCHEDULER_cancel(plugin->env->sched, plugin->http_server_task_v4);
2652 plugin->http_server_task_v4 = GNUNET_SCHEDULER_NO_TASK;
2655 if ( plugin->http_server_task_v6 != GNUNET_SCHEDULER_NO_TASK)
2657 GNUNET_SCHEDULER_cancel(plugin->env->sched, plugin->http_server_task_v6);
2658 plugin->http_server_task_v6 = GNUNET_SCHEDULER_NO_TASK;
2661 while (plugin->ipv4_addr_head!=NULL)
2663 ipv4addr = plugin->ipv4_addr_head;
2664 GNUNET_CONTAINER_DLL_remove(plugin->ipv4_addr_head,plugin->ipv4_addr_tail,ipv4addr);
2665 GNUNET_free(ipv4addr);
2668 while (plugin->ipv6_addr_head!=NULL)
2670 ipv6addr = plugin->ipv6_addr_head;
2671 GNUNET_CONTAINER_DLL_remove(plugin->ipv6_addr_head,plugin->ipv6_addr_tail,ipv6addr);
2672 GNUNET_free(ipv6addr);
2675 /* free all peer information */
2676 if (plugin->peers!=NULL)
2678 GNUNET_CONTAINER_multihashmap_iterate (plugin->peers,
2679 &remove_peer_context_Iterator,
2681 GNUNET_CONTAINER_multihashmap_destroy (plugin->peers);
2683 if (plugin->multi_handle!=NULL)
2685 mret = curl_multi_cleanup(plugin->multi_handle);
2687 if ( CURLM_OK != mret)
2688 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"curl multihandle clean up failed\n");
2690 plugin->multi_handle = NULL;
2692 curl_global_cleanup();
2694 if ( plugin->http_curl_task != GNUNET_SCHEDULER_NO_TASK)
2696 GNUNET_SCHEDULER_cancel(plugin->env->sched, plugin->http_curl_task);
2697 plugin->http_curl_task = GNUNET_SCHEDULER_NO_TASK;
2700 GNUNET_free_non_null (plugin->bind4_address);
2701 GNUNET_free_non_null (plugin->bind6_address);
2702 GNUNET_free_non_null(plugin->bind_hostname);
2704 GNUNET_free_non_null (plugin->crypto_init);
2705 GNUNET_free_non_null (plugin->cert);
2706 GNUNET_free_non_null (plugin->key);
2708 GNUNET_free (plugin);
2711 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Unload %s plugin complete...\n", PROTOCOL_PREFIX);
2718 load_certificate( const char * file )
2720 struct GNUNET_DISK_FileHandle * gn_file;
2725 if (0!=STAT(file, &fstat))
2727 text = GNUNET_malloc (fstat.st_size+1);
2728 gn_file = GNUNET_DISK_file_open(file,GNUNET_DISK_OPEN_READ, GNUNET_DISK_PERM_USER_READ);
2734 if (GNUNET_SYSERR == GNUNET_DISK_file_read(gn_file, text, fstat.st_size))
2737 GNUNET_DISK_file_close(gn_file);
2740 text[fstat.st_size] = '\0';
2741 GNUNET_DISK_file_close(gn_file);
2749 * Entry point for the plugin.
2752 LIBGNUNET_PLUGIN_TRANSPORT_INIT (void *cls)
2754 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
2755 struct Plugin *plugin;
2756 struct GNUNET_TRANSPORT_PluginFunctions *api;
2757 struct GNUNET_TIME_Relative gn_timeout;
2758 long long unsigned int port;
2759 char * component_name;
2761 char * key_file = NULL;
2762 char * cert_file = NULL;
2765 GNUNET_assert(cls !=NULL);
2767 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Starting %s plugin...\n", PROTOCOL_PREFIX);
2769 GNUNET_asprintf(&component_name,"transport-%s",PROTOCOL_PREFIX);
2771 plugin = GNUNET_malloc (sizeof (struct Plugin));
2772 plugin->stats = env->stats;
2774 plugin->peers = NULL;
2775 plugin->bind4_address = NULL;
2776 plugin->use_ipv6 = GNUNET_YES;
2777 plugin->use_ipv4 = GNUNET_YES;
2779 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
2781 api->send = &http_plugin_send;
2782 api->disconnect = &http_plugin_disconnect;
2783 api->address_pretty_printer = &http_plugin_address_pretty_printer;
2784 api->check_address = &http_plugin_address_suggested;
2785 api->address_to_string = &http_plugin_address_to_string;
2787 /* Hashing our identity to use it in URLs */
2788 GNUNET_CRYPTO_hash_to_enc ( &(plugin->env->my_identity->hashPubKey), &plugin->my_ascii_hash_ident);
2791 if (GNUNET_CONFIGURATION_have_value (env->cfg,
2792 component_name, "USE_IPv6"))
2794 plugin->use_ipv6 = GNUNET_CONFIGURATION_get_value_yesno (env->cfg,
2799 if (GNUNET_CONFIGURATION_have_value (env->cfg,
2800 component_name, "USE_IPv4"))
2802 plugin->use_ipv4 = GNUNET_CONFIGURATION_get_value_yesno (env->cfg,
2803 component_name,"USE_IPv4");
2805 /* Reading port number from config file */
2807 GNUNET_CONFIGURATION_get_value_number (env->cfg,
2813 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
2815 _("Require valid port number for transport plugin `%s' in configuration!\n"),
2817 GNUNET_free(component_name);
2818 LIBGNUNET_PLUGIN_TRANSPORT_DONE (api);
2822 /* Reading ipv4 addresse to bind to from config file */
2823 if ((plugin->use_ipv4==GNUNET_YES) && (GNUNET_CONFIGURATION_have_value (env->cfg,
2824 component_name, "BINDTO4")))
2826 GNUNET_break (GNUNET_OK ==
2827 GNUNET_CONFIGURATION_get_value_string (env->cfg,
2830 &plugin->bind_hostname));
2831 plugin->bind4_address = GNUNET_malloc(sizeof(struct sockaddr_in));
2832 plugin->bind4_address->sin_family = AF_INET;
2833 plugin->bind4_address->sin_port = htons (port);
2835 if (plugin->bind_hostname!=NULL)
2837 if (inet_pton(AF_INET,plugin->bind_hostname, &plugin->bind4_address->sin_addr)<=0)
2839 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
2841 _("Misconfigured address to bind to in configuration!\n"));
2842 GNUNET_free(plugin->bind4_address);
2843 GNUNET_free(plugin->bind_hostname);
2844 plugin->bind_hostname = NULL;
2845 plugin->bind4_address = NULL;
2850 /* Reading ipv4 addresse to bind to from config file */
2851 if ((plugin->use_ipv6==GNUNET_YES) && (GNUNET_CONFIGURATION_have_value (env->cfg,
2852 component_name, "BINDTO6")))
2854 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (env->cfg,
2857 &plugin->bind_hostname))
2859 plugin->bind6_address = GNUNET_malloc(sizeof(struct sockaddr_in6));
2860 plugin->bind6_address->sin6_family = AF_INET6;
2861 plugin->bind6_address->sin6_port = htons (port);
2862 if (plugin->bind_hostname!=NULL)
2864 if (inet_pton(AF_INET6,plugin->bind_hostname, &plugin->bind6_address->sin6_addr)<=0)
2866 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
2868 _("Misconfigured address to bind to in configuration!\n"));
2869 GNUNET_free(plugin->bind6_address);
2870 GNUNET_free(plugin->bind_hostname);
2871 plugin->bind_hostname = NULL;
2872 plugin->bind6_address = NULL;
2879 /* Reading HTTPS crypto related configuration */
2880 /* Get crypto init string from config */
2881 if (GNUNET_CONFIGURATION_have_value (env->cfg,
2882 "transport-https", "CRYPTO_INIT"))
2884 GNUNET_CONFIGURATION_get_value_string (env->cfg,
2887 &plugin->crypto_init);
2891 GNUNET_asprintf(&plugin->crypto_init,"NORMAL");
2894 /* Get private key file from config */
2895 if (GNUNET_CONFIGURATION_have_value (env->cfg,
2896 "transport-https", "KEY_FILE"))
2898 GNUNET_CONFIGURATION_get_value_string (env->cfg,
2904 GNUNET_asprintf(&key_file,"https.key");
2906 /* Get private key file from config */
2907 if (GNUNET_CONFIGURATION_have_value (env->cfg,"transport-https", "CERT_FILE"))
2909 GNUNET_CONFIGURATION_get_value_string (env->cfg,
2914 if (cert_file==NULL)
2915 GNUNET_asprintf(&cert_file,"https.cert");
2917 /* read key & certificates from file */
2918 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Loading TLS certificate `%s' `%s'\n", key_file, cert_file);
2920 plugin->key = load_certificate( key_file );
2921 plugin->cert = load_certificate( cert_file );
2923 if ((plugin->key==NULL) || (plugin->cert==NULL))
2927 GNUNET_asprintf(&cmd,"gnunet-transport-certificate-creation %s %s", key_file, cert_file);
2928 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "No usable TLS certificate found, creating certificate \n");
2933 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
2935 _("Could not create a new TLS certificate, shell script `%s' failed!\n"),cmd,
2937 GNUNET_free (key_file);
2938 GNUNET_free (cert_file);
2939 GNUNET_free (component_name);
2941 LIBGNUNET_PLUGIN_TRANSPORT_DONE(api);
2948 plugin->key = load_certificate( key_file );
2949 plugin->cert = load_certificate( cert_file );
2951 if ((plugin->key==NULL) || (plugin->cert==NULL))
2953 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
2955 _("No usable TLS certificate found and creating one failed! \n"),
2957 GNUNET_free (key_file);
2958 GNUNET_free (cert_file);
2959 GNUNET_free (component_name);
2961 LIBGNUNET_PLUGIN_TRANSPORT_DONE(api);
2965 GNUNET_free (key_file);
2966 GNUNET_free (cert_file);
2968 GNUNET_assert((plugin->key!=NULL) && (plugin->cert!=NULL));
2969 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "TLS certificate loaded\n");
2972 GNUNET_assert ((port > 0) && (port <= 65535));
2973 plugin->port_inbound = port;
2974 gn_timeout = GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT;
2975 unsigned int timeout = (gn_timeout.value) / 1000;
2976 if ((plugin->http_server_daemon_v6 == NULL) && (plugin->use_ipv6 == GNUNET_YES) && (port != 0))
2978 struct sockaddr * tmp = (struct sockaddr *) plugin->bind6_address;
2979 plugin->http_server_daemon_v6 = MHD_start_daemon (
2989 plugin , &mdh_access_cb, plugin,
2990 MHD_OPTION_SOCK_ADDR, tmp,
2991 MHD_OPTION_CONNECTION_LIMIT, (unsigned int) 32,
2992 //MHD_OPTION_PER_IP_CONNECTION_LIMIT, (unsigned int) 6,
2994 MHD_OPTION_HTTPS_PRIORITIES, plugin->crypto_init,
2995 MHD_OPTION_HTTPS_MEM_KEY, plugin->key,
2996 MHD_OPTION_HTTPS_MEM_CERT, plugin->cert,
2998 MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) timeout,
2999 MHD_OPTION_CONNECTION_MEMORY_LIMIT, (size_t) (2 * GNUNET_SERVER_MAX_MESSAGE_SIZE),
3000 MHD_OPTION_NOTIFY_COMPLETED, &mhd_termination_cb, NULL,
3001 MHD_OPTION_EXTERNAL_LOGGER, mhd_logger, plugin->mhd_log,
3004 if ((plugin->http_server_daemon_v4 == NULL) && (plugin->use_ipv4 == GNUNET_YES) && (port != 0))
3006 plugin->http_server_daemon_v4 = MHD_start_daemon (
3016 plugin , &mdh_access_cb, plugin,
3017 MHD_OPTION_SOCK_ADDR, (struct sockaddr_in *)plugin->bind4_address,
3018 MHD_OPTION_CONNECTION_LIMIT, (unsigned int) 32,
3019 //MHD_OPTION_PER_IP_CONNECTION_LIMIT, (unsigned int) 6,
3021 MHD_OPTION_HTTPS_PRIORITIES, plugin->crypto_init,
3022 MHD_OPTION_HTTPS_MEM_KEY, plugin->key,
3023 MHD_OPTION_HTTPS_MEM_CERT, plugin->cert,
3025 MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) timeout,
3026 MHD_OPTION_CONNECTION_MEMORY_LIMIT, (size_t) (2 * GNUNET_SERVER_MAX_MESSAGE_SIZE),
3027 MHD_OPTION_NOTIFY_COMPLETED, &mhd_termination_cb, NULL,
3028 MHD_OPTION_EXTERNAL_LOGGER, mhd_logger, plugin->mhd_log,
3031 if (plugin->http_server_daemon_v4 != NULL)
3032 plugin->http_server_task_v4 = http_server_daemon_prepare (plugin, plugin->http_server_daemon_v4);
3033 if (plugin->http_server_daemon_v6 != NULL)
3034 plugin->http_server_task_v6 = http_server_daemon_prepare (plugin, plugin->http_server_daemon_v6);
3037 if (plugin->http_server_task_v4 != GNUNET_SCHEDULER_NO_TASK)
3040 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Starting MHD with IPv4 bound to %s with port %u\n",(plugin->bind_hostname!=NULL) ? plugin->bind_hostname : "every address",port);
3043 else if ((plugin->http_server_task_v6 != GNUNET_SCHEDULER_NO_TASK) && (plugin->http_server_task_v4 != GNUNET_SCHEDULER_NO_TASK))
3046 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Starting MHD with IPv6 bound to %s with port %u\n",(plugin->bind_hostname!=NULL) ? plugin->bind_hostname : "every address", port);
3049 else if ((plugin->http_server_task_v6 != GNUNET_SCHEDULER_NO_TASK) && (plugin->http_server_task_v4 == GNUNET_SCHEDULER_NO_TASK))
3052 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Starting MHD with IPv4 and IPv6 bound to %s with port %u\n",(plugin->bind_hostname!=NULL) ? plugin->bind_hostname : "every address", port);
3058 if ((plugin->use_ipv6 == GNUNET_YES) && (plugin->use_ipv4 == GNUNET_YES))
3059 GNUNET_asprintf(&tmp,"with IPv4 and IPv6 enabled");
3060 if ((plugin->use_ipv6 == GNUNET_NO) && (plugin->use_ipv4 == GNUNET_YES))
3061 GNUNET_asprintf(&tmp,"with IPv4 enabled");
3062 if ((plugin->use_ipv6 == GNUNET_YES) && (plugin->use_ipv4 == GNUNET_NO))
3063 GNUNET_asprintf(&tmp,"with IPv6 enabled");
3064 if ((plugin->use_ipv6 == GNUNET_NO) && (plugin->use_ipv4 == GNUNET_NO))
3065 GNUNET_asprintf(&tmp,"with NO IP PROTOCOL enabled");
3066 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,"HTTP Server with %s could not be started on port %u! %s plugin failed!\n",tmp, port, PROTOCOL_PREFIX);
3068 GNUNET_free (component_name);
3069 LIBGNUNET_PLUGIN_TRANSPORT_DONE (api);
3073 /* Initializing cURL */
3074 curl_global_init(CURL_GLOBAL_ALL);
3075 plugin->multi_handle = curl_multi_init();
3077 if ( NULL == plugin->multi_handle )
3079 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
3081 _("Could not initialize curl multi handle, failed to start %s plugin!\n"),
3083 GNUNET_free(component_name);
3084 LIBGNUNET_PLUGIN_TRANSPORT_DONE (api);
3088 plugin->peers = GNUNET_CONTAINER_multihashmap_create (10);
3089 GNUNET_OS_network_interfaces_list (&process_interfaces, plugin);
3091 GNUNET_free(component_name);
3095 /* end of plugin_transport_http.c */