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 "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;
332 * id for next session
333 * NOTE: 0 is not an ID, zero is not defined. A correct ID is always > 0
338 * entity managing sending data
339 * outbound session: CURL *
340 * inbound session: mhd_connection *
342 void * send_endpoint;
345 * entity managing recieving data
346 * outbound session: CURL *
347 * inbound session: mhd_connection *
349 void * recv_endpoint;
354 size_t queue_length_cur;
359 size_t queue_length_max;
364 * Encapsulation of all of the state of the plugin.
371 struct GNUNET_TRANSPORT_PluginEnvironment *env;
374 * Handle for reporting statistics.
376 struct GNUNET_STATISTICS_Handle *stats;
381 uint16_t port_inbound;
383 struct GNUNET_CONTAINER_MultiHashMap *peers;
386 * Daemon for listening for new IPv4 connections.
388 struct MHD_Daemon *http_server_daemon_v4;
391 * Daemon for listening for new IPv6connections.
393 struct MHD_Daemon *http_server_daemon_v6;
396 * Our primary task for http daemon handling IPv4 connections
398 GNUNET_SCHEDULER_TaskIdentifier http_server_task_v4;
401 * Our primary task for http daemon handling IPv6 connections
403 GNUNET_SCHEDULER_TaskIdentifier http_server_task_v6;
406 * The task sending data
408 GNUNET_SCHEDULER_TaskIdentifier http_curl_task;
413 CURLM * multi_handle;
418 struct IPv4HttpAddress * ipv4_addr_head;
423 struct IPv4HttpAddress * ipv4_addr_tail;
428 struct IPv6HttpAddress * ipv6_addr_head;
433 struct IPv6HttpAddress * ipv6_addr_tail;
436 * Our ASCII encoded, hashed peer identity
437 * This string is used to distinguish between connections and is added to the urls
439 struct GNUNET_CRYPTO_HashAsciiEncoded my_ascii_hash_ident;
442 * IPv4 Address the plugin binds to
444 struct sockaddr_in * bind4_address;
447 * IPv6 Address the plugins binds to
449 struct sockaddr_in6 * bind6_address;
452 * Hostname to bind to
454 char * bind_hostname;
467 * Closure passed by MHD to the mhd_logger function
471 /* only needed for HTTPS plugin */
473 /* The certificate MHD uses as an \0 terminated string */
476 /* The private key MHD uses as an \0 terminated string */
479 /* crypto init string */
486 * Function called for a quick conversion of the binary address to
487 * a numeric address. Note that the caller must not free the
488 * address and that the next call to this function is allowed
489 * to override the address again.
492 * @param addr binary address
493 * @param addrlen length of the address
494 * @return string representing the same address
497 http_plugin_address_to_string (void *cls,
503 * Call MHD to process pending ipv4 requests and then go back
504 * and schedule the next run.
506 static void http_server_daemon_v4_run (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
508 * Call MHD to process pending ipv6 requests and then go back
509 * and schedule the next run.
511 static void http_server_daemon_v6_run (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
514 * Function setting up curl handle and selecting message to send
516 * @param plugin plugin
518 * @return GNUNET_SYSERR on failure, GNUNET_NO if connecting, GNUNET_YES if ok
520 static int send_check_connections (struct Plugin *plugin, struct Session *ps);
523 * Function setting up file descriptors and scheduling task to run
525 * @param plugin plugin as closure
526 * @return GNUNET_SYSERR for hard failure, GNUNET_OK for ok
528 static int curl_schedule (struct Plugin *plugin);
531 * Task scheduled to reset the inbound quota delay for a specific peer
532 * @param cls plugin as closure
533 * @param tc task context
535 static void reset_inbound_quota_delay (void *cls,
536 const struct GNUNET_SCHEDULER_TaskContext *tc)
538 struct HTTP_PeerContext * pc;
540 GNUNET_assert(cls !=NULL);
542 pc = (struct HTTP_PeerContext *) cls;
543 pc->reset_task = GNUNET_SCHEDULER_NO_TASK;
545 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
548 pc->delay = GNUNET_TIME_relative_get_zero ();
553 * Creates a valid url from passed address and id
554 * @param plugin plugin
555 * @param addr address to create url from
556 * @param addrlen address lenth
557 * @param id session id
558 * @return the created url
560 static char * create_url(struct Plugin *plugin, const void * addr, size_t addrlen, size_t id)
563 char *addr_str = (char *) http_plugin_address_to_string(NULL, addr, addrlen);
565 GNUNET_assert ((addr!=NULL) && (addrlen != 0));
566 GNUNET_asprintf(&url,
567 "%s://%s/%s;%u", PROTOCOL_PREFIX, addr_str,
568 (char *) (&plugin->my_ascii_hash_ident),id);
569 GNUNET_free_non_null(addr_str);
574 * Removes a message from the linked list of messages
577 * @return GNUNET_SYSERR if msg not found, GNUNET_OK on success
579 static int remove_http_message (struct Session * ps, struct HTTP_Message * msg)
581 GNUNET_CONTAINER_DLL_remove(ps->pending_msgs_head,ps->pending_msgs_tail,msg);
587 * Iterator to remove peer context
588 * @param cls the plugin
589 * @param key the peers public key hashcode
590 * @param value the peer context
591 * @return GNUNET_YES on success
593 int remove_peer_context_Iterator (void *cls, const GNUNET_HashCode *key, void *value)
595 struct Plugin *plugin = cls;
596 struct HTTP_PeerContext * pc = value;
597 struct Session * ps = pc->head;
598 struct Session * tmp = NULL;
599 struct HTTP_Message * msg = NULL;
600 struct HTTP_Message * msg_tmp = NULL;
602 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Freeing context for peer `%s'\n",GNUNET_i2s(&pc->identity));
604 GNUNET_CONTAINER_multihashmap_remove (plugin->peers, &pc->identity.hashPubKey, pc);
607 plugin->env->session_end(plugin, &pc->identity, ps);
610 GNUNET_free_non_null (ps->addr);
611 GNUNET_free(ps->url);
612 if (ps->msgtok != NULL)
613 GNUNET_SERVER_mst_destroy (ps->msgtok);
615 msg = ps->pending_msgs_head;
622 if (ps->direction==OUTBOUND)
624 if (ps->send_endpoint!=NULL)
625 curl_easy_cleanup(ps->send_endpoint);
626 if (ps->recv_endpoint!=NULL)
627 curl_easy_cleanup(ps->recv_endpoint);
634 GNUNET_STATISTICS_update (plugin->env->stats,
635 gettext_noop ("# HTTP peers active"),
643 * Removes a session from the linked list of sessions
644 * @param pc peer context
646 * @param call_msg_cont GNUNET_YES to call pending message continuations, otherwise no
647 * @param call_msg_cont_result result to call message continuations with
648 * @return GNUNET_SYSERR if msg not found, GNUNET_OK on success
650 static int remove_session (struct HTTP_PeerContext * pc, struct Session * ps, int call_msg_cont, int call_msg_cont_result)
652 struct HTTP_Message * msg;
653 struct Plugin * plugin = ps->peercontext->plugin;
655 #if DEBUG_CONNECTIONS
656 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);
658 plugin->env->session_end(plugin, &pc->identity, ps);
660 GNUNET_free_non_null (ps->addr);
661 GNUNET_SERVER_mst_destroy (ps->msgtok);
662 GNUNET_free(ps->url);
664 if (ps->direction==INBOUND)
666 if (ps->recv_endpoint != NULL)
668 curl_easy_cleanup(ps->recv_endpoint);
669 ps->recv_endpoint = NULL;
671 if (ps->send_endpoint != NULL)
673 curl_easy_cleanup(ps->send_endpoint);
674 ps->send_endpoint = NULL;
678 msg = ps->pending_msgs_head;
681 if ((call_msg_cont == GNUNET_YES) && (msg->transmit_cont!=NULL))
683 msg->transmit_cont (msg->transmit_cont_cls,&pc->identity,call_msg_cont_result);
685 GNUNET_CONTAINER_DLL_remove(ps->pending_msgs_head,ps->pending_msgs_head,msg);
687 msg = ps->pending_msgs_head;
690 GNUNET_CONTAINER_DLL_remove(pc->head,pc->tail,ps);
694 /* no sessions left remove peer */
698 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"No sessions left for peer `%s', removing context\n",GNUNET_i2s(&pc->identity));
700 remove_peer_context_Iterator(plugin, &pc->identity.hashPubKey, pc);
708 * Add the IP of our network interface to the list of
709 * our external IP addresses.
711 * @param cls the 'struct Plugin*'
712 * @param name name of the interface
713 * @param isDefault do we think this may be our default interface
714 * @param addr address of the interface
715 * @param addrlen number of bytes in addr
716 * @return GNUNET_OK to continue iterating
719 process_interfaces (void *cls,
722 const struct sockaddr *addr, socklen_t addrlen)
724 struct Plugin *plugin = cls;
725 struct IPv4HttpAddress * t4;
726 struct IPv6HttpAddress * t6;
730 GNUNET_assert(cls !=NULL);
731 af = addr->sa_family;
732 if ((af == AF_INET) && (plugin->use_ipv4 == GNUNET_YES) && (plugin->bind6_address == NULL))
734 struct in_addr bnd_cmp = ((struct sockaddr_in *) addr)->sin_addr;
735 t4 = GNUNET_malloc(sizeof(struct IPv4HttpAddress));
736 /* Not skipping loopback addresses
737 if (INADDR_LOOPBACK == ntohl(((struct sockaddr_in *) addr)->sin_addr.s_addr))
743 t4->ipv4_addr = ((struct sockaddr_in *) addr)->sin_addr.s_addr;
744 t4->u_port = htons (plugin->port_inbound);
745 if (plugin->bind4_address != NULL)
747 if (0 == memcmp(&plugin->bind4_address->sin_addr, &bnd_cmp, sizeof (struct in_addr)))
749 GNUNET_CONTAINER_DLL_insert(plugin->ipv4_addr_head,plugin->ipv4_addr_tail,t4);
750 plugin->env->notify_address(plugin->env->cls,PROTOCOL_PREFIX,t4, sizeof (struct IPv4HttpAddress), GNUNET_TIME_UNIT_FOREVER_REL);
758 GNUNET_CONTAINER_DLL_insert(plugin->ipv4_addr_head,plugin->ipv4_addr_tail,t4);
759 plugin->env->notify_address(plugin->env->cls,PROTOCOL_PREFIX,t4, sizeof (struct IPv4HttpAddress), GNUNET_TIME_UNIT_FOREVER_REL);
763 else if ((af == AF_INET6) && (plugin->use_ipv6 == GNUNET_YES) && (plugin->bind4_address == NULL))
765 struct in6_addr bnd_cmp6 = ((struct sockaddr_in6 *) addr)->sin6_addr;
766 if (IN6_IS_ADDR_LINKLOCAL (&((struct sockaddr_in6 *) addr)->sin6_addr))
770 t6 = GNUNET_malloc(sizeof(struct IPv6HttpAddress));
771 GNUNET_assert(t6 != NULL);
772 if (plugin->bind6_address != NULL)
774 if (0 == memcmp(&plugin->bind6_address->sin6_addr, &bnd_cmp6, sizeof (struct in6_addr)))
776 memcpy (&t6->ipv6_addr,
777 &((struct sockaddr_in6 *) addr)->sin6_addr,
778 sizeof (struct in6_addr));
779 t6->u6_port = htons (plugin->port_inbound);
780 plugin->env->notify_address(plugin->env->cls,PROTOCOL_PREFIX,t6,sizeof (struct IPv6HttpAddress) , GNUNET_TIME_UNIT_FOREVER_REL);
781 GNUNET_CONTAINER_DLL_insert(plugin->ipv6_addr_head,plugin->ipv6_addr_tail,t6);
787 memcpy (&t6->ipv6_addr,
788 &((struct sockaddr_in6 *) addr)->sin6_addr,
789 sizeof (struct in6_addr));
790 t6->u6_port = htons (plugin->port_inbound);
791 GNUNET_CONTAINER_DLL_insert(plugin->ipv6_addr_head,plugin->ipv6_addr_tail,t6);
792 plugin->env->notify_address(plugin->env->cls,PROTOCOL_PREFIX,t6,sizeof (struct IPv6HttpAddress) , GNUNET_TIME_UNIT_FOREVER_REL);
799 * External logging function for MHD
800 * @param arg arguments
801 * @param fmt format string
802 * @param ap list of arguments
804 void mhd_logger (void * arg, const char * fmt, va_list ap)
807 vsnprintf(text, 1024, fmt, ap);
809 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,"MHD: %s \n", text);
813 static void mhd_termination_cb (void *cls, struct MHD_Connection * connection, void **httpSessionCache)
815 struct Session * ps = *httpSessionCache;
818 struct HTTP_PeerContext * pc = ps->peercontext;
820 if (connection==ps->recv_endpoint)
822 #if DEBUG_CONNECTIONS
823 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: inbound connection from peer `%s' was terminated\n", ps, GNUNET_i2s(&pc->identity));
825 ps->recv_active = GNUNET_NO;
826 ps->recv_connected = GNUNET_NO;
827 ps->recv_endpoint = NULL;
829 if (connection==ps->send_endpoint)
832 ps->send_active = GNUNET_NO;
833 ps->send_connected = GNUNET_NO;
834 ps->send_endpoint = NULL;
835 #if DEBUG_CONNECTIONS
836 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: outbound connection from peer `%s' was terminated\n", ps, GNUNET_i2s(&pc->identity));
840 /* if both connections disconnected, remove session */
841 if ((ps->send_connected == GNUNET_NO) && (ps->recv_connected == GNUNET_NO))
843 GNUNET_STATISTICS_update (pc->plugin->env->stats,
844 gettext_noop ("# HTTP inbound sessions for peers active"),
847 remove_session(pc,ps,GNUNET_YES,GNUNET_SYSERR);
852 * Callback called by MessageStreamTokenizer when a message has arrived
853 * @param cls current session as closure
854 * @param client clien
855 * @param message the message to be forwarded to transport service
858 static void mhd_write_mst_cb (void *cls,
860 const struct GNUNET_MessageHeader *message)
862 struct GNUNET_TIME_Relative delay;
863 struct Session *ps = cls;
864 GNUNET_assert(ps != NULL);
866 struct HTTP_PeerContext *pc = ps->peercontext;
867 GNUNET_assert(pc != NULL);
869 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
870 "Connection %X: Forwarding message to transport service, type %u and size %u from `%s' (`%s')\n",
872 ntohs(message->type),
873 ntohs(message->size),
874 GNUNET_i2s(&(ps->peercontext)->identity),http_plugin_address_to_string(NULL,ps->addr,ps->addrlen));
876 struct GNUNET_TRANSPORT_ATS_Information distance[2];
877 distance[0].type = htonl (GNUNET_TRANSPORT_ATS_QUALITY_NET_DISTANCE);
878 distance[0].value = htonl (1);
879 distance[1].type = htonl (GNUNET_TRANSPORT_ATS_ARRAY_TERMINATOR);
880 distance[1].value = htonl (0);
882 delay = pc->plugin->env->receive (ps->peercontext->plugin->env->cls,
885 (const struct GNUNET_TRANSPORT_ATS_Information *) &distance,
891 if (pc->reset_task != GNUNET_SCHEDULER_NO_TASK)
892 GNUNET_SCHEDULER_cancel (pc->reset_task);
894 if (delay.rel_value > 0)
897 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: Inbound quota management: delay next read for %llu ms \n", ps, delay.rel_value);
899 pc->reset_task = GNUNET_SCHEDULER_add_delayed (delay, &reset_inbound_quota_delay, pc);
904 * Check if incoming connection is accepted.
905 * NOTE: Here every connection is accepted
906 * @param cls plugin as closure
907 * @param addr address of incoming connection
908 * @param addr_len address length of incoming connection
909 * @return MHD_YES if connection is accepted, MHD_NO if connection is rejected
913 mhd_accept_cb (void *cls, const struct sockaddr *addr, socklen_t addr_len)
916 struct Plugin *plugin = cls;
918 /* Every connection is accepted, nothing more to do here */
924 * Callback called by MHD when it needs data to send
925 * @param cls current session
926 * @param pos position in buffer
927 * @param buf the buffer to write data to
928 * @param max max number of bytes available in buffer
929 * @return bytes written to buffer
932 mhd_send_callback (void *cls, uint64_t pos, char *buf, size_t max)
934 struct Session * ps = cls;
935 struct HTTP_PeerContext * pc;
936 struct HTTP_Message * msg;
939 GNUNET_assert (ps!=NULL);
941 pc = ps->peercontext;
942 msg = ps->pending_msgs_tail;
943 if (ps->send_force_disconnect==GNUNET_YES)
945 #if DEBUG_CONNECTIONS
946 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: outbound forced to disconnect\n",ps);
953 if ((msg->size-msg->pos) <= max)
955 memcpy(buf,&msg->buf[msg->pos],(msg->size-msg->pos));
956 bytes_read = msg->size-msg->pos;
957 msg->pos+=(msg->size-msg->pos);
961 memcpy(buf,&msg->buf[msg->pos],max);
966 if (msg->pos==msg->size)
968 if (NULL!=msg->transmit_cont)
969 msg->transmit_cont (msg->transmit_cont_cls,&pc->identity,GNUNET_OK);
970 ps->queue_length_cur -= msg->size;
971 remove_http_message(ps,msg);
974 #if DEBUG_CONNECTIONS
975 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: MHD has sent %u bytes\n", ps, bytes_read);
981 * Process GET or PUT request received via MHD. For
982 * GET, queue response that will send back our pending
983 * messages. For PUT, process incoming data and send
984 * to GNUnet core. In either case, check if a session
985 * already exists and create a new one if not.
988 mhd_access_cb (void *cls,
989 struct MHD_Connection *mhd_connection,
993 const char *upload_data,
994 size_t * upload_data_size, void **httpSessionCache)
996 struct Plugin *plugin = cls;
997 struct MHD_Response *response;
998 const union MHD_ConnectionInfo * conn_info;
999 const struct sockaddr *client_addr;
1000 const struct sockaddr_in *addrin;
1001 const struct sockaddr_in6 *addrin6;
1003 char address[INET6_ADDRSTRLEN+14];
1004 struct GNUNET_PeerIdentity pi_in;
1007 struct IPv4HttpAddress ipv4addr;
1008 struct IPv6HttpAddress ipv6addr;
1010 struct HTTP_PeerContext *pc = NULL;
1011 struct Session *ps = NULL;
1012 struct Session *ps_tmp = NULL;
1014 int res = GNUNET_NO;
1015 int send_error_to_client;
1017 size_t addr_len = 0 ;
1019 GNUNET_assert(cls !=NULL);
1020 send_error_to_client = GNUNET_NO;
1022 if (NULL == *httpSessionCache)
1024 /* check url for peer identity , if invalid send HTTP 404*/
1025 size_t len = strlen(&url[1]);
1026 char * peer = GNUNET_malloc(104+1);
1028 if ((len>104) && (url[104]==';'))
1030 char * id = GNUNET_malloc((len-104)+1);
1031 strcpy(id,&url[105]);
1032 memcpy(peer,&url[1],103);
1034 id_num = strtoul ( id, NULL , 10);
1037 res = GNUNET_CRYPTO_hash_from_string (peer, &(pi_in.hashPubKey));
1039 if ( GNUNET_SYSERR == res )
1041 response = MHD_create_response_from_data (strlen (HTTP_ERROR_RESPONSE),HTTP_ERROR_RESPONSE, MHD_NO, MHD_NO);
1042 res = MHD_queue_response (mhd_connection, MHD_HTTP_NOT_FOUND, response);
1043 MHD_destroy_response (response);
1044 #if DEBUG_CONNECTIONS
1046 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Peer has no valid ident, sent HTTP 1.1/404\n");
1048 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Peer has no valid ident, could not send error\n");
1055 ps = *httpSessionCache;
1056 pc = ps->peercontext;
1059 if (NULL == *httpSessionCache)
1061 /* get peer context */
1062 pc = GNUNET_CONTAINER_multihashmap_get (plugin->peers, &pi_in.hashPubKey);
1066 pc = GNUNET_malloc(sizeof (struct HTTP_PeerContext));
1067 pc->plugin = plugin;
1068 pc->session_id_counter=1;
1069 pc->last_session = NULL;
1070 memcpy(&pc->identity, &pi_in, sizeof(struct GNUNET_PeerIdentity));
1071 GNUNET_CONTAINER_multihashmap_put(plugin->peers, &pc->identity.hashPubKey, pc, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
1072 GNUNET_STATISTICS_update (plugin->env->stats,
1073 gettext_noop ("# HTTP peers active"),
1078 conn_info = MHD_get_connection_info(mhd_connection, MHD_CONNECTION_INFO_CLIENT_ADDRESS );
1079 /* Incoming IPv4 connection */
1080 /* cast required for legacy MHD API < 0.9.6 */
1081 client_addr = (const struct sockaddr *) conn_info->client_addr;
1082 if ( AF_INET == client_addr->sa_family)
1084 addrin = (const struct sockaddr_in*) client_addr;
1085 inet_ntop(addrin->sin_family, &(addrin->sin_addr),address,INET_ADDRSTRLEN);
1086 memcpy(&ipv4addr.ipv4_addr,&(addrin->sin_addr),sizeof(struct in_addr));
1087 ipv4addr.u_port = addrin->sin_port;
1089 addr_len = sizeof(struct IPv4HttpAddress);
1091 /* Incoming IPv6 connection */
1092 if ( AF_INET6 == client_addr->sa_family)
1094 addrin6 = (const struct sockaddr_in6 *) client_addr;
1095 inet_ntop(addrin6->sin6_family, &(addrin6->sin6_addr),address,INET6_ADDRSTRLEN);
1096 memcpy(&ipv6addr.ipv6_addr,&(addrin6->sin6_addr),sizeof(struct in6_addr));
1097 ipv6addr.u6_port = addrin6->sin6_port;
1099 addr_len = sizeof(struct IPv6HttpAddress);
1102 GNUNET_assert (addr != NULL);
1103 GNUNET_assert (addr_len != 0);
1106 /* only inbound sessions here */
1109 while (ps_tmp!=NULL)
1111 if ((ps_tmp->direction==INBOUND) && (ps_tmp->session_id == id_num) && (id_num!=0))
1113 if ((ps_tmp->recv_force_disconnect!=GNUNET_YES) && (ps_tmp->send_force_disconnect!=GNUNET_YES))
1117 ps_tmp=ps_tmp->next;
1122 ps = GNUNET_malloc(sizeof (struct Session));
1123 ps->addr = GNUNET_malloc(addr_len);
1124 memcpy(ps->addr,addr,addr_len);
1125 ps->addrlen = addr_len;
1126 ps->direction=INBOUND;
1127 ps->pending_msgs_head = NULL;
1128 ps->pending_msgs_tail = NULL;
1129 ps->send_connected=GNUNET_NO;
1130 ps->send_active=GNUNET_NO;
1131 ps->recv_connected=GNUNET_NO;
1132 ps->recv_active=GNUNET_NO;
1134 ps->session_id =id_num;
1135 ps->queue_length_cur = 0;
1136 ps->queue_length_max = GNUNET_SERVER_MAX_MESSAGE_SIZE;
1137 ps->url = create_url (plugin, ps->addr, ps->addrlen, ps->session_id);
1138 GNUNET_CONTAINER_DLL_insert(pc->head,pc->tail,ps);
1139 GNUNET_STATISTICS_update (plugin->env->stats,
1140 gettext_noop ("# HTTP inbound sessions for peers active"),
1145 *httpSessionCache = ps;
1146 if (ps->msgtok==NULL)
1147 ps->msgtok = GNUNET_SERVER_mst_create (&mhd_write_mst_cb, ps);
1149 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: HTTP Daemon has new an incoming `%s' request from peer `%s' (`%s')\n",
1152 GNUNET_i2s(&pc->identity),
1153 http_plugin_address_to_string(NULL, ps->addr, ps->addrlen));
1157 /* Is it a PUT or a GET request */
1158 if (0 == strcmp (MHD_HTTP_METHOD_PUT, method))
1160 if (ps->recv_force_disconnect == GNUNET_YES)
1162 #if DEBUG_CONNECTIONS
1163 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: inbound connection was forced to disconnect\n",ps);
1165 ps->recv_active = GNUNET_NO;
1168 if ((*upload_data_size == 0) && (ps->recv_active==GNUNET_NO))
1170 ps->recv_endpoint = mhd_connection;
1171 ps->recv_connected = GNUNET_YES;
1172 ps->recv_active = GNUNET_YES;
1173 ps->recv_force_disconnect = GNUNET_NO;
1174 #if DEBUG_CONNECTIONS
1175 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: inbound PUT connection connected\n",ps);
1180 /* Transmission of all data complete */
1181 if ((*upload_data_size == 0) && (ps->recv_active == GNUNET_YES))
1183 response = MHD_create_response_from_data (strlen (HTTP_PUT_RESPONSE),HTTP_PUT_RESPONSE, MHD_NO, MHD_NO);
1184 res = MHD_queue_response (mhd_connection, MHD_HTTP_OK, response);
1185 #if DEBUG_CONNECTIONS
1186 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: Sent HTTP/1.1: 200 OK as PUT Response\n",ps);
1188 MHD_destroy_response (response);
1189 ps->recv_active=GNUNET_NO;
1193 /* Recieving data */
1194 if ((*upload_data_size > 0) && (ps->recv_active == GNUNET_YES))
1196 if (pc->delay.rel_value == 0)
1199 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: PUT with %u bytes forwarded to MST\n", ps, *upload_data_size);
1201 res = GNUNET_SERVER_mst_receive(ps->msgtok, ps, upload_data, *upload_data_size, GNUNET_NO, GNUNET_NO);
1202 (*upload_data_size) = 0;
1207 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: no inbound bandwidth available! Next read was delayed for %llu ms\n", ps, ps->peercontext->delay.rel_value);
1215 if ( 0 == strcmp (MHD_HTTP_METHOD_GET, method) )
1217 if (ps->send_force_disconnect == GNUNET_YES)
1219 #if DEBUG_CONNECTIONS
1220 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: outbound connection was forced to disconnect\n",ps);
1222 ps->send_active = GNUNET_NO;
1225 ps->send_connected = GNUNET_YES;
1226 ps->send_active = GNUNET_YES;
1227 ps->send_endpoint = mhd_connection;
1228 ps->send_force_disconnect = GNUNET_NO;
1229 #if DEBUG_CONNECTIONS
1230 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: inbound GET connection connected\n",ps);
1232 response = MHD_create_response_from_callback(-1,32 * 1024, &mhd_send_callback, ps, NULL);
1233 res = MHD_queue_response (mhd_connection, MHD_HTTP_OK, response);
1234 MHD_destroy_response (response);
1241 * Function that queries MHD's select sets and
1242 * starts the task waiting for them.
1243 * @param plugin plugin
1244 * @param daemon_handle the MHD daemon handle
1245 * @return gnunet task identifier
1247 static GNUNET_SCHEDULER_TaskIdentifier
1248 http_server_daemon_prepare (struct Plugin *plugin , struct MHD_Daemon *daemon_handle)
1250 GNUNET_SCHEDULER_TaskIdentifier ret;
1254 struct GNUNET_NETWORK_FDSet *wrs;
1255 struct GNUNET_NETWORK_FDSet *wws;
1256 struct GNUNET_NETWORK_FDSet *wes;
1258 unsigned long long timeout;
1260 struct GNUNET_TIME_Relative tv;
1262 ret = GNUNET_SCHEDULER_NO_TASK;
1266 wrs = GNUNET_NETWORK_fdset_create ();
1267 wes = GNUNET_NETWORK_fdset_create ();
1268 wws = GNUNET_NETWORK_fdset_create ();
1270 GNUNET_assert (MHD_YES ==
1271 MHD_get_fdset (daemon_handle,
1276 haveto = MHD_get_timeout (daemon_handle, &timeout);
1277 if (haveto == MHD_YES)
1278 tv.rel_value = (uint64_t) timeout;
1280 tv = GNUNET_TIME_UNIT_SECONDS;
1281 GNUNET_NETWORK_fdset_copy_native (wrs, &rs, max + 1);
1282 GNUNET_NETWORK_fdset_copy_native (wws, &ws, max + 1);
1283 GNUNET_NETWORK_fdset_copy_native (wes, &es, max + 1);
1284 if (daemon_handle == plugin->http_server_daemon_v4)
1286 if (plugin->http_server_task_v4 != GNUNET_SCHEDULER_NO_TASK)
1288 GNUNET_SCHEDULER_cancel(plugin->http_server_task_v4);
1289 plugin->http_server_daemon_v4 = GNUNET_SCHEDULER_NO_TASK;
1292 ret = GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1293 GNUNET_SCHEDULER_NO_TASK,
1297 &http_server_daemon_v4_run,
1300 if (daemon_handle == plugin->http_server_daemon_v6)
1302 if (plugin->http_server_task_v6 != GNUNET_SCHEDULER_NO_TASK)
1304 GNUNET_SCHEDULER_cancel(plugin->http_server_task_v6);
1305 plugin->http_server_task_v6 = GNUNET_SCHEDULER_NO_TASK;
1308 ret = GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1309 GNUNET_SCHEDULER_NO_TASK,
1313 &http_server_daemon_v6_run,
1316 GNUNET_NETWORK_fdset_destroy (wrs);
1317 GNUNET_NETWORK_fdset_destroy (wws);
1318 GNUNET_NETWORK_fdset_destroy (wes);
1323 * Call MHD IPv4 to process pending requests and then go back
1324 * and schedule the next run.
1325 * @param cls plugin as closure
1326 * @param tc task context
1328 static void http_server_daemon_v4_run (void *cls,
1329 const struct GNUNET_SCHEDULER_TaskContext *tc)
1331 struct Plugin *plugin = cls;
1333 #if DEBUG_SCHEDULING
1334 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_READ_READY))
1335 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"http_server_daemon_v4_run: GNUNET_SCHEDULER_REASON_READ_READY\n");
1336 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_WRITE_READY))
1337 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"http_server_daemon_v4_run: GNUNET_SCHEDULER_REASON_WRITE_READY\n");
1338 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_TIMEOUT))
1339 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"http_server_daemon_v4_run: GNUNET_SCHEDULER_REASON_TIMEOUT\n");
1340 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_STARTUP))
1341 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"http_server_daemon_v4_run: GGNUNET_SCHEDULER_REASON_STARTUP\n");
1342 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1343 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"http_server_daemon_v4_run: GGNUNET_SCHEDULER_REASON_SHUTDOWN\n");
1346 GNUNET_assert(cls !=NULL);
1347 plugin->http_server_task_v4 = GNUNET_SCHEDULER_NO_TASK;
1349 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1352 GNUNET_assert (MHD_YES == MHD_run (plugin->http_server_daemon_v4));
1353 plugin->http_server_task_v4 = http_server_daemon_prepare (plugin, plugin->http_server_daemon_v4);
1358 * Call MHD IPv6 to process pending requests and then go back
1359 * and schedule the next run.
1360 * @param cls plugin as closure
1361 * @param tc task context
1363 static void http_server_daemon_v6_run (void *cls,
1364 const struct GNUNET_SCHEDULER_TaskContext *tc)
1366 struct Plugin *plugin = cls;
1368 #if DEBUG_SCHEDULING
1369 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_READ_READY))
1370 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"http_server_daemon_v6_run: GNUNET_SCHEDULER_REASON_READ_READY\n");
1371 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_WRITE_READY))
1372 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"http_server_daemon_v6_run: GNUNET_SCHEDULER_REASON_WRITE_READY\n");
1373 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_TIMEOUT))
1374 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"http_server_daemon_v6_run: GNUNET_SCHEDULER_REASON_TIMEOUT\n");
1375 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_STARTUP))
1376 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"http_server_daemon_v6_run: GGNUNET_SCHEDULER_REASON_STARTUP\n");
1377 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1378 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"http_server_daemon_v6_run: GGNUNET_SCHEDULER_REASON_SHUTDOWN\n");
1381 GNUNET_assert(cls !=NULL);
1382 plugin->http_server_task_v6 = GNUNET_SCHEDULER_NO_TASK;
1384 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1387 GNUNET_assert (MHD_YES == MHD_run (plugin->http_server_daemon_v6));
1388 plugin->http_server_task_v6 = http_server_daemon_prepare (plugin, plugin->http_server_daemon_v6);
1391 static size_t curl_get_header_cb( void *ptr, size_t size, size_t nmemb, void *stream)
1393 struct Session * ps = stream;
1395 long http_result = 0;
1397 /* Getting last http result code */
1398 GNUNET_assert(NULL!=ps);
1399 if (ps->recv_connected==GNUNET_NO)
1401 res = curl_easy_getinfo(ps->recv_endpoint, CURLINFO_RESPONSE_CODE, &http_result);
1402 if (CURLE_OK == res)
1404 if (http_result == 200)
1406 ps->recv_connected = GNUNET_YES;
1407 ps->recv_active = GNUNET_YES;
1408 #if DEBUG_CONNECTIONS
1409 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: connected to recieve data\n",ps);
1411 // Calling send_check_connections again since receive is established
1412 send_check_connections (ps->peercontext->plugin, ps);
1419 size_t len = size * nmemb;
1421 if ((size * nmemb) < SIZE_MAX)
1422 tmp = GNUNET_malloc (len+1);
1424 if ((tmp != NULL) && (len > 0))
1426 memcpy(tmp,ptr,len);
1429 if (tmp[len-2] == 13)
1432 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: Header: %s\n",ps,tmp);
1434 GNUNET_free_non_null (tmp);
1437 return size * nmemb;
1441 * Callback called by libcurl when new headers arrive
1442 * Used to get HTTP result for curl operations
1443 * @param ptr stream to read from
1444 * @param size size of one char element
1445 * @param nmemb number of char elements
1446 * @param stream closure set by user
1447 * @return bytes read by function
1450 static size_t curl_put_header_cb( void *ptr, size_t size, size_t nmemb, void *stream)
1452 struct Session * ps = stream;
1455 size_t len = size * nmemb;
1456 long http_result = 0;
1459 /* Getting last http result code */
1460 GNUNET_assert(NULL!=ps);
1461 res = curl_easy_getinfo(ps->send_endpoint, CURLINFO_RESPONSE_CODE, &http_result);
1462 if (CURLE_OK == res)
1464 if ((http_result == 100) && (ps->send_connected==GNUNET_NO))
1466 ps->send_connected = GNUNET_YES;
1467 ps->send_active = GNUNET_YES;
1468 #if DEBUG_CONNECTIONS
1469 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: connected to send data\n",ps);
1472 if ((http_result == 200) && (ps->send_connected==GNUNET_YES))
1474 ps->send_connected = GNUNET_NO;
1475 ps->send_active = GNUNET_NO;
1476 #if DEBUG_CONNECTIONS
1477 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: sending disconnected\n",ps);
1483 if ((size * nmemb) < SIZE_MAX)
1484 tmp = GNUNET_malloc (len+1);
1486 if ((tmp != NULL) && (len > 0))
1488 memcpy(tmp,ptr,len);
1491 if (tmp[len-2] == 13)
1496 GNUNET_free_non_null (tmp);
1498 return size * nmemb;
1502 * Callback method used with libcurl
1503 * Method is called when libcurl needs to read data during sending
1504 * @param stream pointer where to write data
1505 * @param size size of an individual element
1506 * @param nmemb count of elements that can be written to the buffer
1507 * @param ptr source pointer, passed to the libcurl handle
1508 * @return bytes written to stream
1510 static size_t curl_send_cb(void *stream, size_t size, size_t nmemb, void *ptr)
1512 struct Session * ps = ptr;
1513 struct HTTP_Message * msg = ps->pending_msgs_tail;
1517 if (ps->send_active == GNUNET_NO)
1519 return CURL_READFUNC_PAUSE;
1522 if ((ps->pending_msgs_tail == NULL) && (ps->send_active == GNUNET_YES))
1524 #if DEBUG_CONNECTIONS
1525 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: No Message to send, pausing connection\n",ps);
1527 ps->send_active = GNUNET_NO;
1528 return CURL_READFUNC_PAUSE;
1531 GNUNET_assert (msg!=NULL);
1534 if (msg->pos < msg->size)
1536 /* data fit in buffer */
1537 if ((msg->size - msg->pos) <= (size * nmemb))
1539 len = (msg->size - msg->pos);
1540 memcpy(stream, &msg->buf[msg->pos], len);
1547 memcpy(stream, &msg->buf[msg->pos], len);
1552 /* no data to send */
1558 if ( msg->pos == msg->size)
1560 #if DEBUG_CONNECTIONS
1561 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: Message with %u bytes sent, removing message from queue \n",ps, msg->pos);
1563 /* Calling transmit continuation */
1564 if (NULL != ps->pending_msgs_tail->transmit_cont)
1565 msg->transmit_cont (ps->pending_msgs_tail->transmit_cont_cls,&(ps->peercontext)->identity,GNUNET_OK);
1566 ps->queue_length_cur -= msg->size;
1567 remove_http_message(ps, msg);
1572 static void curl_receive_mst_cb (void *cls,
1574 const struct GNUNET_MessageHeader *message)
1576 struct Session *ps = cls;
1577 struct GNUNET_TIME_Relative delay;
1578 GNUNET_assert(ps != NULL);
1580 struct HTTP_PeerContext *pc = ps->peercontext;
1581 GNUNET_assert(pc != NULL);
1583 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1584 "Connection %X: Forwarding message to transport service, type %u and size %u from `%s' (`%s')\n",
1586 ntohs(message->type),
1587 ntohs(message->size),
1588 GNUNET_i2s(&(pc->identity)),http_plugin_address_to_string(NULL,ps->addr,ps->addrlen));
1590 struct GNUNET_TRANSPORT_ATS_Information distance[2];
1591 distance[0].type = htonl (GNUNET_TRANSPORT_ATS_QUALITY_NET_DISTANCE);
1592 distance[0].value = htonl (1);
1593 distance[1].type = htonl (GNUNET_TRANSPORT_ATS_ARRAY_TERMINATOR);
1594 distance[1].value = htonl (0);
1596 delay = pc->plugin->env->receive (pc->plugin->env->cls,
1599 (const struct GNUNET_TRANSPORT_ATS_Information *) &distance, 2,
1605 if (pc->reset_task != GNUNET_SCHEDULER_NO_TASK)
1606 GNUNET_SCHEDULER_cancel (pc->reset_task);
1608 if (delay.rel_value > 0)
1611 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: Inbound quota management: delay next read for %llu ms \n", ps, delay.rel_value);
1613 pc->reset_task = GNUNET_SCHEDULER_add_delayed (delay, &reset_inbound_quota_delay, pc);
1619 * Callback method used with libcurl
1620 * Method is called when libcurl needs to write data during sending
1621 * @param stream pointer where to write data
1622 * @param size size of an individual element
1623 * @param nmemb count of elements that can be written to the buffer
1624 * @param ptr destination pointer, passed to the libcurl handle
1625 * @return bytes read from stream
1627 static size_t curl_receive_cb( void *stream, size_t size, size_t nmemb, void *ptr)
1629 struct Session * ps = ptr;
1631 if (ps->peercontext->delay.rel_value > 0)
1634 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: no inbound bandwidth available! Next read was delayed for %llu ms\n", ps, ps->peercontext->delay.rel_value);
1639 #if DEBUG_CONNECTIONS
1640 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: %u bytes received\n",ps, size*nmemb);
1642 GNUNET_SERVER_mst_receive(ps->msgtok, ps, stream, size*nmemb, GNUNET_NO, GNUNET_NO);
1643 return (size * nmemb);
1647 static void curl_handle_finished (struct Plugin *plugin)
1649 struct Session *ps = NULL;
1650 struct HTTP_PeerContext *pc = NULL;
1651 struct CURLMsg *msg;
1652 struct HTTP_Message * cur_msg = NULL;
1660 msg = curl_multi_info_read (plugin->multi_handle, &msgs_in_queue);
1661 if ((msgs_in_queue == 0) || (msg == NULL))
1663 /* get session for affected curl handle */
1664 GNUNET_assert ( msg->easy_handle != NULL );
1665 curl_easy_getinfo(msg->easy_handle, CURLINFO_PRIVATE, &tmp);
1666 ps = (struct Session *) tmp;
1667 GNUNET_assert ( ps != NULL );
1668 pc = ps->peercontext;
1669 GNUNET_assert ( pc != NULL );
1674 if ( (msg->data.result != CURLE_OK) &&
1675 (msg->data.result != CURLE_GOT_NOTHING) )
1677 /* sending msg failed*/
1678 if (msg->easy_handle == ps->send_endpoint)
1680 #if DEBUG_CONNECTIONS
1681 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1682 _("Connection %X: HTTP PUT to peer `%s' (`%s') failed: `%s' `%s'\n"),
1684 GNUNET_i2s(&pc->identity),
1685 http_plugin_address_to_string(NULL, ps->addr, ps->addrlen),
1686 "curl_multi_perform",
1687 curl_easy_strerror (msg->data.result));
1689 ps->send_connected = GNUNET_NO;
1690 ps->send_active = GNUNET_NO;
1691 curl_multi_remove_handle(plugin->multi_handle,ps->send_endpoint);
1692 //curl_easy_cleanup(ps->send_endpoint);
1693 //ps->send_endpoint=NULL;
1694 while (ps->pending_msgs_tail != NULL)
1696 cur_msg = ps->pending_msgs_tail;
1697 if ( NULL != cur_msg->transmit_cont)
1698 cur_msg->transmit_cont (cur_msg->transmit_cont_cls,&pc->identity,GNUNET_SYSERR);
1699 ps->queue_length_cur -= cur_msg->size;
1700 remove_http_message(ps,cur_msg);
1703 /* GET connection failed */
1704 if (msg->easy_handle == ps->recv_endpoint)
1706 #if DEBUG_CONNECTIONS
1707 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1708 _("Connection %X: HTTP GET to peer `%s' (`%s') failed: `%s' `%s'\n"),
1710 GNUNET_i2s(&pc->identity),
1711 http_plugin_address_to_string(NULL, ps->addr, ps->addrlen),
1712 "curl_multi_perform",
1713 curl_easy_strerror (msg->data.result));
1715 ps->recv_connected = GNUNET_NO;
1716 ps->recv_active = GNUNET_NO;
1717 curl_multi_remove_handle(plugin->multi_handle,ps->recv_endpoint);
1718 //curl_easy_cleanup(ps->recv_endpoint);
1719 //ps->recv_endpoint=NULL;
1724 if (msg->easy_handle == ps->send_endpoint)
1726 GNUNET_assert (CURLE_OK == curl_easy_getinfo(msg->easy_handle, CURLINFO_RESPONSE_CODE, &http_result));
1727 #if DEBUG_CONNECTIONS
1728 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1729 "Connection %X: HTTP PUT connection to peer `%s' (`%s') was closed with HTTP code %u\n",
1731 GNUNET_i2s(&pc->identity),
1732 http_plugin_address_to_string(NULL, ps->addr, ps->addrlen),
1735 /* Calling transmit continuation */
1736 while (ps->pending_msgs_tail != NULL)
1738 cur_msg = ps->pending_msgs_tail;
1739 if ( NULL != cur_msg->transmit_cont)
1741 /* HTTP 1xx : Last message before here was informational */
1742 if ((http_result >=100) && (http_result < 200))
1743 cur_msg->transmit_cont (cur_msg->transmit_cont_cls,&pc->identity,GNUNET_OK);
1744 /* HTTP 2xx: successful operations */
1745 if ((http_result >=200) && (http_result < 300))
1746 cur_msg->transmit_cont (cur_msg->transmit_cont_cls,&pc->identity,GNUNET_OK);
1747 /* HTTP 3xx..5xx: error */
1748 if ((http_result >=300) && (http_result < 600))
1749 cur_msg->transmit_cont (cur_msg->transmit_cont_cls,&pc->identity,GNUNET_SYSERR);
1751 ps->queue_length_cur -= cur_msg->size;
1752 remove_http_message(ps,cur_msg);
1755 ps->send_connected = GNUNET_NO;
1756 ps->send_active = GNUNET_NO;
1757 curl_multi_remove_handle(plugin->multi_handle,ps->send_endpoint);
1758 //curl_easy_cleanup(ps->send_endpoint);
1759 //ps->send_endpoint =NULL;
1761 if (msg->easy_handle == ps->recv_endpoint)
1763 #if DEBUG_CONNECTIONS
1764 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1765 "Connection %X: HTTP GET connection to peer `%s' (`%s') was closed with HTTP code %u\n",
1767 GNUNET_i2s(&pc->identity),
1768 http_plugin_address_to_string(NULL, ps->addr, ps->addrlen),
1771 ps->recv_connected = GNUNET_NO;
1772 ps->recv_active = GNUNET_NO;
1773 curl_multi_remove_handle(plugin->multi_handle,ps->recv_endpoint);
1774 //curl_easy_cleanup(ps->recv_endpoint);
1775 //ps->recv_endpoint=NULL;
1778 if ((ps->recv_connected == GNUNET_NO) && (ps->send_connected == GNUNET_NO))
1779 remove_session (pc, ps, GNUNET_YES, GNUNET_SYSERR);
1785 while ( (msgs_in_queue > 0) );
1790 * Task performing curl operations
1791 * @param cls plugin as closure
1792 * @param tc gnunet scheduler task context
1794 static void curl_perform (void *cls,
1795 const struct GNUNET_SCHEDULER_TaskContext *tc)
1797 struct Plugin *plugin = cls;
1798 static unsigned int handles_last_run;
1802 GNUNET_assert(cls !=NULL);
1804 plugin->http_curl_task = GNUNET_SCHEDULER_NO_TASK;
1805 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1810 mret = curl_multi_perform (plugin->multi_handle, &running);
1811 if ((running < handles_last_run) && (running>0))
1812 curl_handle_finished(plugin);
1813 handles_last_run = running;
1815 while (mret == CURLM_CALL_MULTI_PERFORM);
1816 curl_schedule(plugin);
1821 * Function setting up file descriptors and scheduling task to run
1823 * @param plugin plugin as closure
1824 * @return GNUNET_SYSERR for hard failure, GNUNET_OK for ok
1826 static int curl_schedule(struct Plugin *plugin)
1832 struct GNUNET_NETWORK_FDSet *grs;
1833 struct GNUNET_NETWORK_FDSet *gws;
1837 /* Cancel previous scheduled task */
1838 if (plugin->http_curl_task != GNUNET_SCHEDULER_NO_TASK)
1840 GNUNET_SCHEDULER_cancel(plugin->http_curl_task);
1841 plugin->http_curl_task = GNUNET_SCHEDULER_NO_TASK;
1848 mret = curl_multi_fdset (plugin->multi_handle, &rs, &ws, &es, &max);
1849 if (mret != CURLM_OK)
1851 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1852 _("%s failed at %s:%d: `%s'\n"),
1853 "curl_multi_fdset", __FILE__, __LINE__,
1854 curl_multi_strerror (mret));
1855 return GNUNET_SYSERR;
1857 mret = curl_multi_timeout (plugin->multi_handle, &to);
1858 if (mret != CURLM_OK)
1860 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1861 _("%s failed at %s:%d: `%s'\n"),
1862 "curl_multi_timeout", __FILE__, __LINE__,
1863 curl_multi_strerror (mret));
1864 return GNUNET_SYSERR;
1867 grs = GNUNET_NETWORK_fdset_create ();
1868 gws = GNUNET_NETWORK_fdset_create ();
1869 GNUNET_NETWORK_fdset_copy_native (grs, &rs, max + 1);
1870 GNUNET_NETWORK_fdset_copy_native (gws, &ws, max + 1);
1871 plugin->http_curl_task = GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1872 GNUNET_SCHEDULER_NO_TASK,
1873 (to == -1) ? GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5) : GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, to),
1878 GNUNET_NETWORK_fdset_destroy (gws);
1879 GNUNET_NETWORK_fdset_destroy (grs);
1884 * Function to log curl debug messages with GNUNET_log
1885 * @param curl handle
1886 * @param type curl_infotype
1889 * @param cls closure
1892 int curl_logger (CURL * curl, curl_infotype type , char * data, size_t size , void * cls)
1895 if (type == CURLINFO_TEXT)
1898 memcpy(text,data,size);
1899 if (text[size-1] == '\n')
1904 text[size+1] = '\0';
1906 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"CURL: Connection %X - %s", cls, text);
1912 * Function setting up curl handle and selecting message to send
1914 * @param plugin plugin
1916 * @return GNUNET_SYSERR on failure, GNUNET_NO if connecting, GNUNET_YES if ok
1918 static int send_check_connections (struct Plugin *plugin, struct Session *ps)
1921 struct HTTP_Message * msg;
1923 struct GNUNET_TIME_Relative timeout = GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT;
1925 if (ps->direction == OUTBOUND)
1927 /* RECV DIRECTION */
1928 /* Check if session is connected to receive data, otherwise connect to peer */
1929 if (ps->recv_connected == GNUNET_NO)
1931 int fresh = GNUNET_NO;
1932 if (ps->recv_endpoint == NULL)
1935 ps->recv_endpoint = curl_easy_init();
1938 curl_easy_setopt(ps->recv_endpoint, CURLOPT_VERBOSE, 1L);
1939 curl_easy_setopt(ps->recv_endpoint, CURLOPT_DEBUGFUNCTION , &curl_logger);
1940 curl_easy_setopt(ps->recv_endpoint, CURLOPT_DEBUGDATA , ps->recv_endpoint);
1943 curl_easy_setopt (ps->recv_endpoint, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1);
1944 curl_easy_setopt(ps->recv_endpoint, CURLOPT_SSL_VERIFYPEER, 0);
1945 curl_easy_setopt(ps->recv_endpoint, CURLOPT_SSL_VERIFYHOST, 0);
1947 curl_easy_setopt(ps->recv_endpoint, CURLOPT_URL, ps->url);
1948 curl_easy_setopt(ps->recv_endpoint, CURLOPT_HEADERFUNCTION, &curl_get_header_cb);
1949 curl_easy_setopt(ps->recv_endpoint, CURLOPT_WRITEHEADER, ps);
1950 curl_easy_setopt(ps->recv_endpoint, CURLOPT_READFUNCTION, curl_send_cb);
1951 curl_easy_setopt(ps->recv_endpoint, CURLOPT_READDATA, ps);
1952 curl_easy_setopt(ps->recv_endpoint, CURLOPT_WRITEFUNCTION, curl_receive_cb);
1953 curl_easy_setopt(ps->recv_endpoint, CURLOPT_WRITEDATA, ps);
1954 curl_easy_setopt(ps->recv_endpoint, CURLOPT_TIMEOUT, (long) timeout.rel_value);
1955 curl_easy_setopt(ps->recv_endpoint, CURLOPT_PRIVATE, ps);
1956 curl_easy_setopt(ps->recv_endpoint, CURLOPT_CONNECTTIMEOUT, HTTP_CONNECT_TIMEOUT);
1957 curl_easy_setopt(ps->recv_endpoint, CURLOPT_BUFFERSIZE, 2*GNUNET_SERVER_MAX_MESSAGE_SIZE);
1958 #if CURL_TCP_NODELAY
1959 curl_easy_setopt(ps->recv_endpoint, CURLOPT_TCP_NODELAY, 1);
1962 if (fresh==GNUNET_YES)
1964 mret = curl_multi_add_handle(plugin->multi_handle, ps->recv_endpoint);
1965 if (mret != CURLM_OK)
1967 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1968 _("Connection: %X: %s failed at %s:%d: `%s'\n"),
1970 "curl_multi_add_handle", __FILE__, __LINE__,
1971 curl_multi_strerror (mret));
1972 return GNUNET_SYSERR;
1975 if (plugin->http_curl_task != GNUNET_SCHEDULER_NO_TASK)
1977 GNUNET_SCHEDULER_cancel(plugin->http_curl_task);
1978 plugin->http_curl_task = GNUNET_SCHEDULER_NO_TASK;
1980 plugin->http_curl_task = GNUNET_SCHEDULER_add_now (&curl_perform, plugin);
1983 /* waiting for receive direction */
1984 if (ps->recv_connected==GNUNET_NO)
1987 /* SEND DIRECTION */
1988 /* Check if session is connected to send data, otherwise connect to peer */
1989 if ((ps->send_connected == GNUNET_YES) && (ps->send_endpoint!= NULL))
1991 if (ps->send_active == GNUNET_YES)
1993 #if DEBUG_CONNECTIONS
1994 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: outbound active, enqueueing message\n",ps);
1998 if (ps->send_active == GNUNET_NO)
2000 #if DEBUG_CONNECTIONS
2001 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: outbound paused, unpausing existing connection and enqueueing message\n",ps);
2003 if (CURLE_OK == curl_easy_pause(ps->send_endpoint,CURLPAUSE_CONT))
2005 ps->send_active=GNUNET_YES;
2006 if (plugin->http_curl_task != GNUNET_SCHEDULER_NO_TASK)
2008 GNUNET_SCHEDULER_cancel(plugin->http_curl_task);
2009 plugin->http_curl_task = GNUNET_SCHEDULER_NO_TASK;
2011 plugin->http_curl_task = GNUNET_SCHEDULER_add_now (&curl_perform, plugin);
2015 return GNUNET_SYSERR;
2018 /* not connected, initiate connection */
2019 if (ps->send_connected==GNUNET_NO)
2021 int fresh = GNUNET_NO;
2022 if (NULL == ps->send_endpoint)
2024 ps->send_endpoint = curl_easy_init();
2027 GNUNET_assert (ps->send_endpoint != NULL);
2028 GNUNET_assert (NULL != ps->pending_msgs_tail);
2029 #if DEBUG_CONNECTIONS
2030 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: outbound not connected, initiating connection\n",ps);
2032 ps->send_active = GNUNET_NO;
2033 msg = ps->pending_msgs_tail;
2036 curl_easy_setopt(ps->send_endpoint, CURLOPT_VERBOSE, 1L);
2037 curl_easy_setopt(ps->send_endpoint, CURLOPT_DEBUGFUNCTION , &curl_logger);
2038 curl_easy_setopt(ps->send_endpoint, CURLOPT_DEBUGDATA , ps->send_endpoint);
2041 curl_easy_setopt (ps->send_endpoint, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1);
2042 curl_easy_setopt(ps->send_endpoint, CURLOPT_SSL_VERIFYPEER, 0);
2043 curl_easy_setopt(ps->send_endpoint, CURLOPT_SSL_VERIFYHOST, 0);
2045 curl_easy_setopt(ps->send_endpoint, CURLOPT_URL, ps->url);
2046 curl_easy_setopt(ps->send_endpoint, CURLOPT_PUT, 1L);
2047 curl_easy_setopt(ps->send_endpoint, CURLOPT_HEADERFUNCTION, &curl_put_header_cb);
2048 curl_easy_setopt(ps->send_endpoint, CURLOPT_WRITEHEADER, ps);
2049 curl_easy_setopt(ps->send_endpoint, CURLOPT_READFUNCTION, curl_send_cb);
2050 curl_easy_setopt(ps->send_endpoint, CURLOPT_READDATA, ps);
2051 curl_easy_setopt(ps->send_endpoint, CURLOPT_WRITEFUNCTION, curl_receive_cb);
2052 curl_easy_setopt(ps->send_endpoint, CURLOPT_READDATA, ps);
2053 curl_easy_setopt(ps->send_endpoint, CURLOPT_TIMEOUT, (long) timeout.rel_value);
2054 curl_easy_setopt(ps->send_endpoint, CURLOPT_PRIVATE, ps);
2055 curl_easy_setopt(ps->send_endpoint, CURLOPT_CONNECTTIMEOUT, HTTP_CONNECT_TIMEOUT);
2056 curl_easy_setopt(ps->send_endpoint, CURLOPT_BUFFERSIZE, 2 * GNUNET_SERVER_MAX_MESSAGE_SIZE);
2057 #if CURL_TCP_NODELAY
2058 curl_easy_setopt(ps->send_endpoint, CURLOPT_TCP_NODELAY, 1);
2061 if (fresh==GNUNET_YES)
2063 mret = curl_multi_add_handle(plugin->multi_handle, ps->send_endpoint);
2064 if (mret != CURLM_OK)
2066 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2067 _("Connection: %X: %s failed at %s:%d: `%s'\n"),
2069 "curl_multi_add_handle", __FILE__, __LINE__,
2070 curl_multi_strerror (mret));
2071 return GNUNET_SYSERR;
2075 if (plugin->http_curl_task != GNUNET_SCHEDULER_NO_TASK)
2077 GNUNET_SCHEDULER_cancel(plugin->http_curl_task);
2078 plugin->http_curl_task = GNUNET_SCHEDULER_NO_TASK;
2080 plugin->http_curl_task = GNUNET_SCHEDULER_add_now (&curl_perform, plugin);
2083 if (ps->direction == INBOUND)
2085 GNUNET_assert (NULL != ps->pending_msgs_tail);
2086 if ((ps->recv_connected==GNUNET_YES) && (ps->send_connected==GNUNET_YES) &&
2087 (ps->recv_force_disconnect==GNUNET_NO) && (ps->recv_force_disconnect==GNUNET_NO))
2090 return GNUNET_SYSERR;
2094 * select best session to transmit data to peer
2096 * @param pc peer context of target peer
2097 * @param addr address of target peer
2098 * @param addrlen address length
2099 * @param force_address does transport service enforce address?
2100 * @param session session passed by transport service
2101 * @return selected session
2104 static struct Session * send_select_session (struct HTTP_PeerContext *pc, const void * addr, size_t addrlen, int force_address, struct Session * session)
2106 struct Session * tmp = NULL;
2107 int addr_given = GNUNET_NO;
2109 if ((addr!=NULL) && (addrlen>0))
2110 addr_given = GNUNET_YES;
2112 if (force_address == GNUNET_YES)
2114 /* check session given as argument */
2115 if ((session != NULL) && (addr_given == GNUNET_YES))
2117 if (0 == memcmp(session->addr, addr, addrlen))
2119 /* connection can not be used, since it is disconnected */
2120 if ((session->recv_force_disconnect==GNUNET_NO) && (session->send_force_disconnect==GNUNET_NO))
2122 #if DEBUG_SESSION_SELECTION
2123 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Session %X selected: Using session passed by transport to send to forced address \n", session);
2129 /* check last session used */
2130 if ((pc->last_session != NULL)&& (addr_given == GNUNET_YES))
2132 if (0 == memcmp(pc->last_session->addr, addr, addrlen))
2134 /* connection can not be used, since it is disconnected */
2135 if ((pc->last_session->recv_force_disconnect==GNUNET_NO) && (pc->last_session->send_force_disconnect==GNUNET_NO))
2137 #if DEBUG_SESSION_SELECTION
2138 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Session %X selected: Using last session used to send to forced address \n", pc->last_session);
2140 return pc->last_session;
2144 /* find session in existing sessions */
2146 while ((tmp!=NULL) && (addr_given == GNUNET_YES))
2149 if (0 == memcmp(tmp->addr, addr, addrlen))
2151 /* connection can not be used, since it is disconnected */
2152 if ((tmp->recv_force_disconnect==GNUNET_NO) && (tmp->send_force_disconnect==GNUNET_NO))
2154 #if DEBUG_SESSION_SELECTION
2155 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Session %X selected: Using existing session to send to forced address \n", session);
2163 /* no session to use */
2166 if ((force_address == GNUNET_NO) || (force_address == GNUNET_SYSERR))
2168 /* check session given as argument */
2169 if (session != NULL)
2171 /* connection can not be used, since it is disconnected */
2172 if ((session->recv_force_disconnect==GNUNET_NO) && (session->send_force_disconnect==GNUNET_NO))
2174 #if DEBUG_SESSION_SELECTION
2175 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Session %X selected: Using session passed by transport to send not-forced address \n", session);
2181 /* check last session used */
2182 if (pc->last_session != NULL)
2184 /* connection can not be used, since it is disconnected */
2185 if ((pc->last_session->recv_force_disconnect==GNUNET_NO) && (pc->last_session->send_force_disconnect==GNUNET_NO))
2187 #if DEBUG_SESSION_SELECTION
2188 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Session %X selected: Using last session to send to not-forced address \n", pc->last_session);
2190 return pc->last_session;
2193 /* find session in existing sessions */
2197 /* connection can not be used, since it is disconnected */
2198 if ((tmp->recv_force_disconnect==GNUNET_NO) && (tmp->send_force_disconnect==GNUNET_NO))
2200 #if DEBUG_SESSION_SELECTION
2201 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Session %X selected: Using existing session to send to not-forced address \n", tmp);
2213 * Function that can be used by the transport service to transmit
2214 * a message using the plugin. Note that in the case of a
2215 * peer disconnecting, the continuation MUST be called
2216 * prior to the disconnect notification itself. This function
2217 * will be called with this peer's HELLO message to initiate
2218 * a fresh connection to another peer.
2220 * @param cls closure
2221 * @param target who should receive this message
2222 * @param msgbuf the message to transmit
2223 * @param msgbuf_size number of bytes in 'msgbuf'
2224 * @param priority how important is the message (most plugins will
2225 * ignore message priority and just FIFO)
2226 * @param to how long to wait at most for the transmission (does not
2227 * require plugins to discard the message after the timeout,
2228 * just advisory for the desired delay; most plugins will ignore
2230 * @param session which session must be used (or NULL for "any")
2231 * @param addr the address to use (can be NULL if the plugin
2232 * is "on its own" (i.e. re-use existing TCP connection))
2233 * @param addrlen length of the address in bytes
2234 * @param force_address GNUNET_YES if the plugin MUST use the given address,
2235 * GNUNET_NO means the plugin may use any other address and
2236 * GNUNET_SYSERR means that only reliable existing
2237 * bi-directional connections should be used (regardless
2239 * @param cont continuation to call once the message has
2240 * been transmitted (or if the transport is ready
2241 * for the next transmission call; or if the
2242 * peer disconnected...); can be NULL
2243 * @param cont_cls closure for cont
2244 * @return number of bytes used (on the physical network, with overheads);
2245 * -1 on hard errors (i.e. address invalid); 0 is a legal value
2246 * and does NOT mean that the message was not transmitted (DV)
2249 http_plugin_send (void *cls,
2250 const struct GNUNET_PeerIdentity *target,
2253 unsigned int priority,
2254 struct GNUNET_TIME_Relative to,
2255 struct Session *session,
2259 GNUNET_TRANSPORT_TransmitContinuation cont,
2262 struct Plugin *plugin = cls;
2263 struct HTTP_Message *msg;
2264 struct HTTP_PeerContext * pc;
2265 struct Session * ps = NULL;
2267 GNUNET_assert(cls !=NULL);
2271 if (force_address == GNUNET_YES)
2272 GNUNET_asprintf(&force, "forced addr.");
2273 if (force_address == GNUNET_NO)
2274 GNUNET_asprintf(&force, "any addr.");
2275 if (force_address == GNUNET_SYSERR)
2276 GNUNET_asprintf(&force,"reliable bi-direc. address addr.");
2278 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Transport tells me to send %u bytes to `%s' using %s (%s) and session: %X\n",
2282 http_plugin_address_to_string(NULL, addr, addrlen),
2288 pc = GNUNET_CONTAINER_multihashmap_get (plugin->peers, &target->hashPubKey);
2292 pc = GNUNET_malloc(sizeof (struct HTTP_PeerContext));
2293 pc->plugin = plugin;
2294 pc->session_id_counter=1;
2295 pc->last_session = NULL;
2296 memcpy(&pc->identity, target, sizeof(struct GNUNET_PeerIdentity));
2297 GNUNET_CONTAINER_multihashmap_put(plugin->peers, &pc->identity.hashPubKey, pc, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
2298 GNUNET_STATISTICS_update (plugin->env->stats,
2299 gettext_noop ("# HTTP peers active"),
2304 ps = send_select_session (pc, addr, addrlen, force_address, session);
2306 /* session not existing, but address forced -> creating new session */
2309 if ((addr!=NULL) && (addrlen!=0))
2311 ps = GNUNET_malloc(sizeof (struct Session));
2312 #if DEBUG_SESSION_SELECTION
2313 if (force_address == GNUNET_YES)
2314 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"No existing connection & forced address: creating new session %X to peer %s\n", ps, GNUNET_i2s(target));
2315 if (force_address != GNUNET_YES)
2316 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"No existing connection: creating new session %X to peer %s\n", ps, GNUNET_i2s(target));
2318 ps->addr = GNUNET_malloc(addrlen);
2319 memcpy(ps->addr,addr,addrlen);
2320 ps->addrlen = addrlen;
2327 ps->direction=OUTBOUND;
2328 ps->recv_connected = GNUNET_NO;
2329 ps->recv_force_disconnect = GNUNET_NO;
2330 ps->send_connected = GNUNET_NO;
2331 ps->send_force_disconnect = GNUNET_NO;
2332 ps->pending_msgs_head = NULL;
2333 ps->pending_msgs_tail = NULL;
2335 ps->session_id = pc->session_id_counter;
2336 ps->queue_length_cur = 0;
2337 ps->queue_length_max = GNUNET_SERVER_MAX_MESSAGE_SIZE;
2338 pc->session_id_counter++;
2339 ps->url = create_url (plugin, ps->addr, ps->addrlen, ps->session_id);
2340 if (ps->msgtok == NULL)
2341 ps->msgtok = GNUNET_SERVER_mst_create (&curl_receive_mst_cb, ps);
2342 GNUNET_CONTAINER_DLL_insert(pc->head,pc->tail,ps);
2343 GNUNET_STATISTICS_update (plugin->env->stats,
2344 gettext_noop ("# HTTP outbound sessions for peers active"),
2351 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));
2353 return GNUNET_SYSERR;
2357 if (msgbuf_size >= (ps->queue_length_max - ps->queue_length_cur))
2359 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);
2360 //return GNUNET_SYSERR;
2364 msg = GNUNET_malloc (sizeof (struct HTTP_Message) + msgbuf_size);
2366 msg->size = msgbuf_size;
2368 msg->buf = (char *) &msg[1];
2369 msg->transmit_cont = cont;
2370 msg->transmit_cont_cls = cont_cls;
2371 memcpy (msg->buf,msgbuf, msgbuf_size);
2372 GNUNET_CONTAINER_DLL_insert (ps->pending_msgs_head,
2373 ps->pending_msgs_tail,
2375 ps->queue_length_cur += msgbuf_size;
2376 if (send_check_connections (plugin, ps) == GNUNET_SYSERR)
2377 return GNUNET_SYSERR;
2378 if (force_address != GNUNET_YES)
2379 pc->last_session = ps;
2381 if (pc->last_session==NULL)
2382 pc->last_session = ps;
2389 * Function that can be used to force the plugin to disconnect
2390 * from the given peer and cancel all previous transmissions
2391 * (and their continuationc).
2393 * @param cls closure
2394 * @param target peer from which to disconnect
2397 http_plugin_disconnect (void *cls,
2398 const struct GNUNET_PeerIdentity *target)
2402 struct Plugin *plugin = cls;
2403 struct HTTP_PeerContext *pc = NULL;
2404 struct Session *ps = NULL;
2405 //struct Session *tmp = NULL;
2407 pc = GNUNET_CONTAINER_multihashmap_get (plugin->peers, &target->hashPubKey);
2414 /* Telling transport that session is getting disconnected */
2415 plugin->env->session_end(plugin, target, ps);
2416 if (ps->direction==OUTBOUND)
2418 if (ps->send_endpoint!=NULL)
2420 //GNUNET_assert(CURLM_OK == curl_multi_remove_handle(plugin->multi_handle,ps->send_endpoint));
2421 //curl_easy_cleanup(ps->send_endpoint);
2422 //ps->send_endpoint=NULL;
2423 ps->send_force_disconnect = GNUNET_YES;
2425 if (ps->recv_endpoint!=NULL)
2427 //GNUNET_assert(CURLM_OK == curl_multi_remove_handle(plugin->multi_handle,ps->recv_endpoint));
2428 //curl_easy_cleanup(ps->recv_endpoint);
2429 //ps->recv_endpoint=NULL;
2430 ps->recv_force_disconnect = GNUNET_YES;
2434 if (ps->direction==INBOUND)
2436 ps->recv_force_disconnect = GNUNET_YES;
2437 ps->send_force_disconnect = GNUNET_YES;
2440 while (ps->pending_msgs_head!=NULL)
2442 remove_http_message(ps, ps->pending_msgs_head);
2444 ps->recv_active = GNUNET_NO;
2445 ps->send_active = GNUNET_NO;
2452 * Convert the transports address to a nice, human-readable
2455 * @param cls closure
2456 * @param type name of the transport that generated the address
2457 * @param addr one of the addresses of the host, NULL for the last address
2458 * the specific address format depends on the transport
2459 * @param addrlen length of the address
2460 * @param numeric should (IP) addresses be displayed in numeric form?
2461 * @param timeout after how long should we give up?
2462 * @param asc function to call on each string
2463 * @param asc_cls closure for asc
2466 http_plugin_address_pretty_printer (void *cls,
2471 struct GNUNET_TIME_Relative timeout,
2472 GNUNET_TRANSPORT_AddressStringCallback
2475 const struct IPv4HttpAddress *t4;
2476 const struct IPv6HttpAddress *t6;
2477 struct sockaddr_in a4;
2478 struct sockaddr_in6 a6;
2484 GNUNET_assert(cls !=NULL);
2485 if (addrlen == sizeof (struct IPv6HttpAddress))
2487 address = GNUNET_malloc (INET6_ADDRSTRLEN);
2489 a6.sin6_addr = t6->ipv6_addr;
2490 inet_ntop(AF_INET6, &(a6.sin6_addr),address,INET6_ADDRSTRLEN);
2491 port = ntohs(t6->u6_port);
2493 else if (addrlen == sizeof (struct IPv4HttpAddress))
2495 address = GNUNET_malloc (INET_ADDRSTRLEN);
2497 a4.sin_addr.s_addr = t4->ipv4_addr;
2498 inet_ntop(AF_INET, &(a4.sin_addr),address,INET_ADDRSTRLEN);
2499 port = ntohs(t4->u_port);
2503 /* invalid address */
2504 GNUNET_break_op (0);
2505 asc (asc_cls, NULL);
2508 res = GNUNET_asprintf(&ret,"%s://%s:%u/", PROTOCOL_PREFIX, address, port);
2509 GNUNET_free (address);
2510 GNUNET_assert(res != 0);
2512 GNUNET_free_non_null (ret);
2518 * Another peer has suggested an address for this
2519 * peer and transport plugin. Check that this could be a valid
2520 * address. If so, consider adding it to the list
2523 * @param cls closure
2524 * @param addr pointer to the address
2525 * @param addrlen length of addr
2526 * @return GNUNET_OK if this is a plausible address for this peer
2530 http_plugin_address_suggested (void *cls,
2531 const void *addr, size_t addrlen)
2533 struct Plugin *plugin = cls;
2534 struct IPv4HttpAddress *v4;
2535 struct IPv6HttpAddress *v6;
2537 struct IPv4HttpAddress *tv4 = plugin->ipv4_addr_head;
2538 struct IPv6HttpAddress *tv6 = plugin->ipv6_addr_head;
2540 GNUNET_assert(cls !=NULL);
2541 if ((addrlen != sizeof (struct IPv4HttpAddress)) &&
2542 (addrlen != sizeof (struct IPv6HttpAddress)))
2544 return GNUNET_SYSERR;
2546 if (addrlen == sizeof (struct IPv4HttpAddress))
2548 v4 = (struct IPv4HttpAddress *) addr;
2550 if (plugin->bind4_address!=NULL)
2552 if (0 == memcmp (&plugin->bind4_address->sin_addr, &v4->ipv4_addr, sizeof(uint32_t)))
2555 return GNUNET_SYSERR;
2559 if (0==memcmp (&tv4->ipv4_addr, &v4->ipv4_addr, sizeof(uint32_t)))
2566 return GNUNET_SYSERR;
2568 if (addrlen == sizeof (struct IPv6HttpAddress))
2570 v6 = (struct IPv6HttpAddress *) addr;
2572 if (plugin->bind6_address!=NULL)
2574 if (0 == memcmp (&plugin->bind6_address->sin6_addr, &v6->ipv6_addr, sizeof(struct in6_addr)))
2577 return GNUNET_SYSERR;
2582 if (0 == memcmp (&tv6->ipv6_addr, &v6->ipv6_addr, sizeof(struct in6_addr)))
2589 return GNUNET_SYSERR;
2592 return GNUNET_SYSERR;
2597 * Function called for a quick conversion of the binary address to
2598 * a numeric address. Note that the caller must not free the
2599 * address and that the next call to this function is allowed
2600 * to override the address again.
2602 * @param cls closure
2603 * @param addr binary address
2604 * @param addrlen length of the address
2605 * @return string representing the same address
2608 http_plugin_address_to_string (void *cls,
2612 const struct IPv4HttpAddress *t4;
2613 const struct IPv6HttpAddress *t6;
2614 struct sockaddr_in a4;
2615 struct sockaddr_in6 a6;
2621 if (addrlen == sizeof (struct IPv6HttpAddress))
2623 address = GNUNET_malloc (INET6_ADDRSTRLEN);
2625 a6.sin6_addr = t6->ipv6_addr;
2626 inet_ntop(AF_INET6, &(a6.sin6_addr),address,INET6_ADDRSTRLEN);
2627 port = ntohs(t6->u6_port);
2629 else if (addrlen == sizeof (struct IPv4HttpAddress))
2631 address = GNUNET_malloc (INET_ADDRSTRLEN);
2633 a4.sin_addr.s_addr = t4->ipv4_addr;
2634 inet_ntop(AF_INET, &(a4.sin_addr),address,INET_ADDRSTRLEN);
2635 port = ntohs(t4->u_port);
2639 /* invalid address */
2642 res = GNUNET_asprintf(&ret,"%s:%u",address,port);
2643 GNUNET_free (address);
2644 GNUNET_assert(res != 0);
2650 * Exit point from the plugin.
2653 LIBGNUNET_PLUGIN_TRANSPORT_DONE (void *cls)
2655 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
2656 struct Plugin *plugin = api->cls;
2658 struct IPv4HttpAddress * ipv4addr;
2659 struct IPv6HttpAddress * ipv6addr;
2660 GNUNET_assert(cls !=NULL);
2662 if (plugin->http_server_daemon_v4 != NULL)
2664 MHD_stop_daemon (plugin->http_server_daemon_v4);
2665 plugin->http_server_daemon_v4 = NULL;
2667 if (plugin->http_server_daemon_v6 != NULL)
2669 MHD_stop_daemon (plugin->http_server_daemon_v6);
2670 plugin->http_server_daemon_v6 = NULL;
2673 if ( plugin->http_server_task_v4 != GNUNET_SCHEDULER_NO_TASK)
2675 GNUNET_SCHEDULER_cancel(plugin->http_server_task_v4);
2676 plugin->http_server_task_v4 = GNUNET_SCHEDULER_NO_TASK;
2679 if ( plugin->http_server_task_v6 != GNUNET_SCHEDULER_NO_TASK)
2681 GNUNET_SCHEDULER_cancel(plugin->http_server_task_v6);
2682 plugin->http_server_task_v6 = GNUNET_SCHEDULER_NO_TASK;
2685 while (plugin->ipv4_addr_head!=NULL)
2687 ipv4addr = plugin->ipv4_addr_head;
2688 GNUNET_CONTAINER_DLL_remove(plugin->ipv4_addr_head,plugin->ipv4_addr_tail,ipv4addr);
2689 GNUNET_free(ipv4addr);
2692 while (plugin->ipv6_addr_head!=NULL)
2694 ipv6addr = plugin->ipv6_addr_head;
2695 GNUNET_CONTAINER_DLL_remove(plugin->ipv6_addr_head,plugin->ipv6_addr_tail,ipv6addr);
2696 GNUNET_free(ipv6addr);
2699 /* free all peer information */
2700 if (plugin->peers!=NULL)
2702 GNUNET_CONTAINER_multihashmap_iterate (plugin->peers,
2703 &remove_peer_context_Iterator,
2705 GNUNET_CONTAINER_multihashmap_destroy (plugin->peers);
2707 if (plugin->multi_handle!=NULL)
2709 mret = curl_multi_cleanup(plugin->multi_handle);
2711 if ( CURLM_OK != mret)
2712 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"curl multihandle clean up failed\n");
2714 plugin->multi_handle = NULL;
2716 curl_global_cleanup();
2718 if ( plugin->http_curl_task != GNUNET_SCHEDULER_NO_TASK)
2720 GNUNET_SCHEDULER_cancel(plugin->http_curl_task);
2721 plugin->http_curl_task = GNUNET_SCHEDULER_NO_TASK;
2724 GNUNET_free_non_null (plugin->bind4_address);
2725 GNUNET_free_non_null (plugin->bind6_address);
2726 GNUNET_free_non_null(plugin->bind_hostname);
2728 GNUNET_free_non_null (plugin->crypto_init);
2729 GNUNET_free_non_null (plugin->cert);
2730 GNUNET_free_non_null (plugin->key);
2732 GNUNET_free (plugin);
2735 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Unload %s plugin complete...\n", PROTOCOL_PREFIX);
2742 load_certificate( const char * file )
2744 struct GNUNET_DISK_FileHandle * gn_file;
2749 if (0!=STAT(file, &fstat))
2751 text = GNUNET_malloc (fstat.st_size+1);
2752 gn_file = GNUNET_DISK_file_open(file,GNUNET_DISK_OPEN_READ, GNUNET_DISK_PERM_USER_READ);
2758 if (GNUNET_SYSERR == GNUNET_DISK_file_read(gn_file, text, fstat.st_size))
2761 GNUNET_DISK_file_close(gn_file);
2764 text[fstat.st_size] = '\0';
2765 GNUNET_DISK_file_close(gn_file);
2773 * Entry point for the plugin.
2776 LIBGNUNET_PLUGIN_TRANSPORT_INIT (void *cls)
2778 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
2779 struct Plugin *plugin;
2780 struct GNUNET_TRANSPORT_PluginFunctions *api;
2781 struct GNUNET_TIME_Relative gn_timeout;
2782 long long unsigned int port;
2783 char * component_name;
2785 char * key_file = NULL;
2786 char * cert_file = NULL;
2789 GNUNET_assert(cls !=NULL);
2791 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Starting %s plugin...\n", PROTOCOL_PREFIX);
2793 GNUNET_asprintf(&component_name,"transport-%s",PROTOCOL_PREFIX);
2795 plugin = GNUNET_malloc (sizeof (struct Plugin));
2796 plugin->stats = env->stats;
2798 plugin->peers = NULL;
2799 plugin->bind4_address = NULL;
2800 plugin->use_ipv6 = GNUNET_YES;
2801 plugin->use_ipv4 = GNUNET_YES;
2803 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
2805 api->send = &http_plugin_send;
2806 api->disconnect = &http_plugin_disconnect;
2807 api->address_pretty_printer = &http_plugin_address_pretty_printer;
2808 api->check_address = &http_plugin_address_suggested;
2809 api->address_to_string = &http_plugin_address_to_string;
2811 /* Hashing our identity to use it in URLs */
2812 GNUNET_CRYPTO_hash_to_enc ( &(plugin->env->my_identity->hashPubKey), &plugin->my_ascii_hash_ident);
2815 if (GNUNET_CONFIGURATION_have_value (env->cfg,
2816 component_name, "USE_IPv6"))
2818 plugin->use_ipv6 = GNUNET_CONFIGURATION_get_value_yesno (env->cfg,
2823 if (GNUNET_CONFIGURATION_have_value (env->cfg,
2824 component_name, "USE_IPv4"))
2826 plugin->use_ipv4 = GNUNET_CONFIGURATION_get_value_yesno (env->cfg,
2827 component_name,"USE_IPv4");
2829 /* Reading port number from config file */
2831 GNUNET_CONFIGURATION_get_value_number (env->cfg,
2837 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
2839 _("Require valid port number for transport plugin `%s' in configuration!\n"),
2841 GNUNET_free(component_name);
2842 LIBGNUNET_PLUGIN_TRANSPORT_DONE (api);
2846 /* Reading ipv4 addresse to bind to from config file */
2847 if ((plugin->use_ipv4==GNUNET_YES) && (GNUNET_CONFIGURATION_have_value (env->cfg,
2848 component_name, "BINDTO4")))
2850 GNUNET_break (GNUNET_OK ==
2851 GNUNET_CONFIGURATION_get_value_string (env->cfg,
2854 &plugin->bind_hostname));
2855 plugin->bind4_address = GNUNET_malloc(sizeof(struct sockaddr_in));
2856 plugin->bind4_address->sin_family = AF_INET;
2857 plugin->bind4_address->sin_port = htons (port);
2859 if (plugin->bind_hostname!=NULL)
2861 if (inet_pton(AF_INET,plugin->bind_hostname, &plugin->bind4_address->sin_addr)<=0)
2863 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
2865 _("Misconfigured address to bind to in configuration!\n"));
2866 GNUNET_free(plugin->bind4_address);
2867 GNUNET_free(plugin->bind_hostname);
2868 plugin->bind_hostname = NULL;
2869 plugin->bind4_address = NULL;
2874 /* Reading ipv4 addresse to bind to from config file */
2875 if ((plugin->use_ipv6==GNUNET_YES) && (GNUNET_CONFIGURATION_have_value (env->cfg,
2876 component_name, "BINDTO6")))
2878 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (env->cfg,
2881 &plugin->bind_hostname))
2883 plugin->bind6_address = GNUNET_malloc(sizeof(struct sockaddr_in6));
2884 plugin->bind6_address->sin6_family = AF_INET6;
2885 plugin->bind6_address->sin6_port = htons (port);
2886 if (plugin->bind_hostname!=NULL)
2888 if (inet_pton(AF_INET6,plugin->bind_hostname, &plugin->bind6_address->sin6_addr)<=0)
2890 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
2892 _("Misconfigured address to bind to in configuration!\n"));
2893 GNUNET_free(plugin->bind6_address);
2894 GNUNET_free(plugin->bind_hostname);
2895 plugin->bind_hostname = NULL;
2896 plugin->bind6_address = NULL;
2903 /* Reading HTTPS crypto related configuration */
2904 /* Get crypto init string from config */
2905 if (GNUNET_CONFIGURATION_have_value (env->cfg,
2906 "transport-https", "CRYPTO_INIT"))
2908 GNUNET_CONFIGURATION_get_value_string (env->cfg,
2911 &plugin->crypto_init);
2915 GNUNET_asprintf(&plugin->crypto_init,"NORMAL");
2918 /* Get private key file from config */
2919 if (GNUNET_CONFIGURATION_have_value (env->cfg,
2920 "transport-https", "KEY_FILE"))
2922 GNUNET_CONFIGURATION_get_value_string (env->cfg,
2928 GNUNET_asprintf(&key_file,"https.key");
2930 /* Get private key file from config */
2931 if (GNUNET_CONFIGURATION_have_value (env->cfg,"transport-https", "CERT_FILE"))
2933 GNUNET_CONFIGURATION_get_value_string (env->cfg,
2938 if (cert_file==NULL)
2939 GNUNET_asprintf(&cert_file,"https.cert");
2941 /* read key & certificates from file */
2942 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Loading TLS certificate `%s' `%s'\n", key_file, cert_file);
2944 plugin->key = load_certificate( key_file );
2945 plugin->cert = load_certificate( cert_file );
2947 if ((plugin->key==NULL) || (plugin->cert==NULL))
2951 GNUNET_asprintf(&cmd,"gnunet-transport-certificate-creation %s %s", key_file, cert_file);
2952 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "No usable TLS certificate found, creating certificate \n");
2957 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
2959 _("Could not create a new TLS certificate, shell script `%s' failed!\n"),cmd,
2961 GNUNET_free (key_file);
2962 GNUNET_free (cert_file);
2963 GNUNET_free (component_name);
2965 LIBGNUNET_PLUGIN_TRANSPORT_DONE(api);
2972 plugin->key = load_certificate( key_file );
2973 plugin->cert = load_certificate( cert_file );
2975 if ((plugin->key==NULL) || (plugin->cert==NULL))
2977 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
2979 _("No usable TLS certificate found and creating one failed! \n"),
2981 GNUNET_free (key_file);
2982 GNUNET_free (cert_file);
2983 GNUNET_free (component_name);
2985 LIBGNUNET_PLUGIN_TRANSPORT_DONE(api);
2989 GNUNET_free (key_file);
2990 GNUNET_free (cert_file);
2992 GNUNET_assert((plugin->key!=NULL) && (plugin->cert!=NULL));
2993 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "TLS certificate loaded\n");
2996 GNUNET_assert ((port > 0) && (port <= 65535));
2997 plugin->port_inbound = port;
2998 gn_timeout = GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT;
2999 unsigned int timeout = (gn_timeout.rel_value) / 1000;
3000 if ((plugin->http_server_daemon_v6 == NULL) && (plugin->use_ipv6 == GNUNET_YES) && (port != 0))
3002 struct sockaddr * tmp = (struct sockaddr *) plugin->bind6_address;
3003 plugin->http_server_daemon_v6 = MHD_start_daemon (
3013 plugin , &mhd_access_cb, plugin,
3014 MHD_OPTION_SOCK_ADDR, tmp,
3015 MHD_OPTION_CONNECTION_LIMIT, (unsigned int) 32,
3016 //MHD_OPTION_PER_IP_CONNECTION_LIMIT, (unsigned int) 6,
3018 MHD_OPTION_HTTPS_PRIORITIES, plugin->crypto_init,
3019 MHD_OPTION_HTTPS_MEM_KEY, plugin->key,
3020 MHD_OPTION_HTTPS_MEM_CERT, plugin->cert,
3022 MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) timeout,
3023 MHD_OPTION_CONNECTION_MEMORY_LIMIT, (size_t) (2 * GNUNET_SERVER_MAX_MESSAGE_SIZE),
3024 MHD_OPTION_NOTIFY_COMPLETED, &mhd_termination_cb, NULL,
3025 MHD_OPTION_EXTERNAL_LOGGER, mhd_logger, plugin->mhd_log,
3028 if ((plugin->http_server_daemon_v4 == NULL) && (plugin->use_ipv4 == GNUNET_YES) && (port != 0))
3030 plugin->http_server_daemon_v4 = MHD_start_daemon (
3040 plugin , &mhd_access_cb, plugin,
3041 MHD_OPTION_SOCK_ADDR, (struct sockaddr_in *)plugin->bind4_address,
3042 MHD_OPTION_CONNECTION_LIMIT, (unsigned int) 32,
3043 //MHD_OPTION_PER_IP_CONNECTION_LIMIT, (unsigned int) 6,
3045 MHD_OPTION_HTTPS_PRIORITIES, plugin->crypto_init,
3046 MHD_OPTION_HTTPS_MEM_KEY, plugin->key,
3047 MHD_OPTION_HTTPS_MEM_CERT, plugin->cert,
3049 MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) timeout,
3050 MHD_OPTION_CONNECTION_MEMORY_LIMIT, (size_t) (2 * GNUNET_SERVER_MAX_MESSAGE_SIZE),
3051 MHD_OPTION_NOTIFY_COMPLETED, &mhd_termination_cb, NULL,
3052 MHD_OPTION_EXTERNAL_LOGGER, mhd_logger, plugin->mhd_log,
3055 if (plugin->http_server_daemon_v4 != NULL)
3056 plugin->http_server_task_v4 = http_server_daemon_prepare (plugin, plugin->http_server_daemon_v4);
3057 if (plugin->http_server_daemon_v6 != NULL)
3058 plugin->http_server_task_v6 = http_server_daemon_prepare (plugin, plugin->http_server_daemon_v6);
3061 if (plugin->http_server_task_v4 != GNUNET_SCHEDULER_NO_TASK)
3064 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);
3067 else if ((plugin->http_server_task_v6 != GNUNET_SCHEDULER_NO_TASK) && (plugin->http_server_task_v4 != GNUNET_SCHEDULER_NO_TASK))
3070 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);
3073 else if ((plugin->http_server_task_v6 != GNUNET_SCHEDULER_NO_TASK) && (plugin->http_server_task_v4 == GNUNET_SCHEDULER_NO_TASK))
3076 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);
3082 if ((plugin->use_ipv6 == GNUNET_YES) && (plugin->use_ipv4 == GNUNET_YES))
3083 GNUNET_asprintf(&tmp,"with IPv4 and IPv6 enabled");
3084 if ((plugin->use_ipv6 == GNUNET_NO) && (plugin->use_ipv4 == GNUNET_YES))
3085 GNUNET_asprintf(&tmp,"with IPv4 enabled");
3086 if ((plugin->use_ipv6 == GNUNET_YES) && (plugin->use_ipv4 == GNUNET_NO))
3087 GNUNET_asprintf(&tmp,"with IPv6 enabled");
3088 if ((plugin->use_ipv6 == GNUNET_NO) && (plugin->use_ipv4 == GNUNET_NO))
3089 GNUNET_asprintf(&tmp,"with NO IP PROTOCOL enabled");
3090 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);
3092 GNUNET_free (component_name);
3093 LIBGNUNET_PLUGIN_TRANSPORT_DONE (api);
3097 /* Initializing cURL */
3098 curl_global_init(CURL_GLOBAL_ALL);
3099 plugin->multi_handle = curl_multi_init();
3101 if ( NULL == plugin->multi_handle )
3103 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
3105 _("Could not initialize curl multi handle, failed to start %s plugin!\n"),
3107 GNUNET_free(component_name);
3108 LIBGNUNET_PLUGIN_TRANSPORT_DONE (api);
3112 plugin->peers = GNUNET_CONTAINER_multihashmap_create (10);
3113 GNUNET_OS_network_interfaces_list (&process_interfaces, plugin);
3115 GNUNET_free(component_name);
3119 /* end of gnunet_transport_plugin.http.c */