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_https.c
23 * @brief https 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 "gnunet_disk_lib.h"
41 #include "microhttpd.h"
42 #include <curl/curl.h>
44 #define DEBUG_HTTPS GNUNET_NO
45 #define VERBOSE GNUNET_NO
46 #define DEBUG_MHD GNUNET_YES
47 #define DEBUG_CURL GNUNET_NO
48 #define DEBUG_CONNECTIONS GNUNET_NO
49 #define DEBUG_SESSION_SELECTION GNUNET_NO
51 #define CURL_TCP_NODELAY GNUNET_YES
53 #define INBOUND GNUNET_NO
54 #define OUTBOUND GNUNET_YES
56 #define PROTOCOL_PREFIX "https"
59 * Text of the response sent back after the last bytes of a PUT
60 * request have been received (just to formally obey the HTTP
63 #define HTTP_PUT_RESPONSE "Thank you!"
66 * After how long do we expire an address that we
67 * learned from another peer if it is not reconfirmed
70 #define LEARNED_ADDRESS_EXPIRATION GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_HOURS, 6)
73 * Page returned if request invalid
75 #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>"
78 * Timeout for a http connect
80 #define HTTP_CONNECT_TIMEOUT 30
83 * Network format for IPv4 addresses.
85 struct IPv4HttpAddress
88 * IPv4 address, in network byte order.
90 uint32_t ipv4_addr GNUNET_PACKED;
93 * Port number, in network byte order.
95 uint16_t u_port GNUNET_PACKED;
101 * Network format for IPv6 addresses.
103 struct IPv6HttpAddress
108 struct in6_addr ipv6_addr GNUNET_PACKED;
111 * Port number, in network byte order.
113 uint16_t u6_port GNUNET_PACKED;
119 * Message to send using http
124 * next pointer for double linked list
126 struct HTTP_Message * next;
129 * previous pointer for double linked list
131 struct HTTP_Message * prev;
134 * buffer containing data to send
139 * amount of data already sent
149 * Continuation function to call once the transmission buffer
150 * has again space available. NULL if there is no
151 * continuation to call.
153 GNUNET_TRANSPORT_TransmitContinuation transmit_cont;
156 * Closure for transmit_cont.
158 void *transmit_cont_cls;
162 struct HTTP_PeerContext
167 struct GNUNET_PeerIdentity identity;
170 * Pointer to the global plugin struct.
172 struct Plugin *plugin;
175 * Linked list of connections with this peer
178 struct Session * head;
181 * Linked list of connections with this peer
184 struct Session * tail;
187 * id for next session
189 size_t session_id_counter;
192 * Last session used to send data
194 struct Session * last_session;
203 struct SessionHeader header;
206 * next session in linked list
208 struct Session * next;
211 * previous session in linked list
213 struct Session * prev;
216 * address of this session
231 * Message queue for outbound messages
234 struct HTTP_Message * pending_msgs_head;
237 * Message queue for outbound messages
240 struct HTTP_Message * pending_msgs_tail;
243 * partner peer this connection belongs to
245 struct HTTP_PeerContext * peercontext;
248 * message stream tokenizer for incoming data
250 struct GNUNET_SERVER_MessageStreamTokenizer *msgtok;
254 * outbound: OUTBOUND (GNUNET_YES)
255 * inbound : INBOUND (GNUNET_NO)
257 unsigned int direction;
260 * is session connected to send data?
262 unsigned int send_connected;
265 * is send connection active?
267 unsigned int send_active;
270 * connection disconnect forced (e.g. from transport)
272 unsigned int send_force_disconnect;
275 * is session connected to receive data?
277 unsigned int recv_connected;
280 * is receive connection active?
282 unsigned int recv_active;
285 * connection disconnect forced (e.g. from transport)
287 unsigned int recv_force_disconnect;
290 * id for next session
291 * NOTE: 0 is not an ID, zero is not defined. A correct ID is always > 0
296 * entity managing sending data
297 * outbound session: CURL *
298 * inbound session: mhd_connection *
300 void * send_endpoint;
303 * entity managing recieving data
304 * outbound session: CURL *
305 * inbound session: mhd_connection *
307 void * recv_endpoint;
311 * Encapsulation of all of the state of the plugin.
318 struct GNUNET_TRANSPORT_PluginEnvironment *env;
321 * Handle for reporting statistics.
323 struct GNUNET_STATISTICS_Handle *stats;
325 unsigned int port_inbound;
327 struct GNUNET_CONTAINER_MultiHashMap *peers;
330 * Daemon for listening for new IPv4 connections.
332 struct MHD_Daemon *http_server_daemon_v4;
335 * Daemon for listening for new IPv6connections.
337 struct MHD_Daemon *http_server_daemon_v6;
340 * Our primary task for http daemon handling IPv4 connections
342 GNUNET_SCHEDULER_TaskIdentifier http_server_task_v4;
345 * Our primary task for http daemon handling IPv6 connections
347 GNUNET_SCHEDULER_TaskIdentifier http_server_task_v6;
350 * The task sending data
352 GNUNET_SCHEDULER_TaskIdentifier http_curl_task;
357 CURLM * multi_handle;
360 * Our ASCII encoded, hashed peer identity
361 * This string is used to distinguish between connections and is added to the urls
363 struct GNUNET_CRYPTO_HashAsciiEncoded my_ascii_hash_ident;
365 struct sockaddr_in * bind4_address;
366 struct sockaddr_in6 * bind6_address;
367 char * bind_hostname;
371 /* The certificate MHD uses as an \0 terminated string */
374 /* The private key MHD uses as an \0 terminated string */
384 * Function called for a quick conversion of the binary address to
385 * a numeric address. Note that the caller must not free the
386 * address and that the next call to this function is allowed
387 * to override the address again.
390 * @param addr binary address
391 * @param addrlen length of the address
392 * @return string representing the same address
395 http_plugin_address_to_string (void *cls,
401 * Call MHD to process pending ipv4 requests and then go back
402 * and schedule the next run.
404 static void http_server_daemon_v4_run (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
406 * Call MHD to process pending ipv6 requests and then go back
407 * and schedule the next run.
409 static void http_server_daemon_v6_run (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
412 * Function setting up curl handle and selecting message to send
414 * @param ses session to send data to
415 * @param con connection
416 * @return bytes sent to peer
418 static ssize_t send_check_connections (void *cls, struct Session *ps);
421 * Function setting up file descriptors and scheduling task to run
423 * @param ses session to send data to
426 static int curl_schedule(void *cls );
430 static char * create_url(void * cls, const void * addr, size_t addrlen, size_t id)
432 struct Plugin *plugin = cls;
434 char *addr_str = (char *) http_plugin_address_to_string(NULL, addr, addrlen);
436 GNUNET_assert ((addr!=NULL) && (addrlen != 0));
437 GNUNET_asprintf(&url,
438 "%s://%s/%s;%u", PROTOCOL_PREFIX, addr_str,
439 (char *) (&plugin->my_ascii_hash_ident),id);
440 GNUNET_free_non_null(addr_str);
445 * Removes a message from the linked list of messages
446 * @param con connection to remove message from
447 * @param msg message to remove
448 * @return GNUNET_SYSERR if msg not found, GNUNET_OK on success
450 static int remove_http_message (struct Session * ps, struct HTTP_Message * msg)
452 GNUNET_CONTAINER_DLL_remove(ps->pending_msgs_head,ps->pending_msgs_tail,msg);
457 int remove_peer_context_Iterator (void *cls, const GNUNET_HashCode *key, void *value);
460 * Removes a session from the linked list of sessions
461 * @param pc peer context
463 * @param call_msg_cont GNUNET_YES to call pending message continuations, otherwise no
464 * @param call_msg_cont_result, result to call message continuations with
465 * @return GNUNET_SYSERR if msg not found, GNUNET_OK on success
467 static int remove_session (struct HTTP_PeerContext * pc, struct Session * ps, int call_msg_cont, int call_msg_cont_result)
469 struct HTTP_Message * msg;
470 struct Plugin * plugin = ps->peercontext->plugin;
472 #if DEBUG_CONNECTIONS
473 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);
475 plugin->env->session_end(plugin, &pc->identity, ps);
477 GNUNET_free_non_null (ps->addr);
478 GNUNET_SERVER_mst_destroy (ps->msgtok);
479 GNUNET_free(ps->url);
481 if (ps->direction==INBOUND)
483 if (ps->recv_endpoint != NULL)
485 curl_easy_cleanup(ps->recv_endpoint);
486 ps->recv_endpoint = NULL;
488 if (ps->send_endpoint != NULL)
490 curl_easy_cleanup(ps->send_endpoint);
491 ps->send_endpoint = NULL;
495 msg = ps->pending_msgs_head;
498 if ((call_msg_cont == GNUNET_YES) && (msg->transmit_cont!=NULL))
500 msg->transmit_cont (msg->transmit_cont_cls,&pc->identity,call_msg_cont_result);
502 GNUNET_CONTAINER_DLL_remove(ps->pending_msgs_head,ps->pending_msgs_head,msg);
504 msg = ps->pending_msgs_head;
507 GNUNET_CONTAINER_DLL_remove(pc->head,pc->tail,ps);
511 /* no sessions left remove peer */
515 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"No sessions left for peer `%s', removing context\n",GNUNET_i2s(&pc->identity));
517 remove_peer_context_Iterator(plugin, &pc->identity.hashPubKey, pc);
523 int remove_peer_context_Iterator (void *cls, const GNUNET_HashCode *key, void *value)
525 struct Plugin *plugin = cls;
526 struct HTTP_PeerContext * pc = value;
527 struct Session * ps = pc->head;
528 struct Session * tmp = NULL;
529 struct HTTP_Message * msg = NULL;
530 struct HTTP_Message * msg_tmp = NULL;
532 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Freeing context for peer `%s'\n",GNUNET_i2s(&pc->identity));
534 GNUNET_CONTAINER_multihashmap_remove (plugin->peers, &pc->identity.hashPubKey, pc);
537 plugin->env->session_end(plugin, &pc->identity, ps);
540 GNUNET_free_non_null (ps->addr);
541 GNUNET_free(ps->url);
542 if (ps->msgtok != NULL)
543 GNUNET_SERVER_mst_destroy (ps->msgtok);
545 msg = ps->pending_msgs_head;
552 if (ps->direction==OUTBOUND)
554 if (ps->send_endpoint!=NULL)
555 curl_easy_cleanup(ps->send_endpoint);
556 if (ps->recv_endpoint!=NULL)
557 curl_easy_cleanup(ps->recv_endpoint);
564 GNUNET_STATISTICS_update (plugin->env->stats,
565 gettext_noop ("# HTTP peers active"),
572 * Add the IP of our network interface to the list of
573 * our external IP addresses.
575 * @param cls the 'struct Plugin*'
576 * @param name name of the interface
577 * @param isDefault do we think this may be our default interface
578 * @param addr address of the interface
579 * @param addrlen number of bytes in addr
580 * @return GNUNET_OK to continue iterating
583 process_interfaces (void *cls,
586 const struct sockaddr *addr, socklen_t addrlen)
588 struct Plugin *plugin = cls;
589 struct IPv4HttpAddress * t4;
590 struct IPv6HttpAddress * t6;
594 GNUNET_assert(cls !=NULL);
595 af = addr->sa_family;
596 if ((af == AF_INET) && (plugin->use_ipv4 == GNUNET_YES) && (plugin->bind6_address == NULL))
598 struct in_addr bnd_cmp = ((struct sockaddr_in *) addr)->sin_addr;
599 t4 = GNUNET_malloc(sizeof(struct IPv4HttpAddress));
600 /* Not skipping loopback addresses
601 if (INADDR_LOOPBACK == ntohl(((struct sockaddr_in *) addr)->sin_addr.s_addr))
607 t4->ipv4_addr = ((struct sockaddr_in *) addr)->sin_addr.s_addr;
608 t4->u_port = htons (plugin->port_inbound);
609 if (plugin->bind4_address != NULL)
611 if (0 == memcmp(&plugin->bind4_address->sin_addr, &bnd_cmp, sizeof (struct in_addr)))
613 plugin->env->notify_address(plugin->env->cls,"https",t4, sizeof (struct IPv4HttpAddress), GNUNET_TIME_UNIT_FOREVER_REL);
618 plugin->env->notify_address(plugin->env->cls,"https",t4, sizeof (struct IPv4HttpAddress), GNUNET_TIME_UNIT_FOREVER_REL);
622 else if ((af == AF_INET6) && (plugin->use_ipv6 == GNUNET_YES) && (plugin->bind4_address == NULL))
624 struct in6_addr bnd_cmp6 = ((struct sockaddr_in6 *) addr)->sin6_addr;
625 if (IN6_IS_ADDR_LINKLOCAL (&((struct sockaddr_in6 *) addr)->sin6_addr))
629 t6 = GNUNET_malloc(sizeof(struct IPv6HttpAddress));
630 GNUNET_assert(t6 != NULL);
631 if (plugin->bind6_address != NULL)
633 if (0 == memcmp(&plugin->bind6_address->sin6_addr, &bnd_cmp6, sizeof (struct in6_addr)))
635 memcpy (&t6->ipv6_addr,
636 &((struct sockaddr_in6 *) addr)->sin6_addr,
637 sizeof (struct in6_addr));
638 t6->u6_port = htons (plugin->port_inbound);
639 plugin->env->notify_address(plugin->env->cls,"https",t6,sizeof (struct IPv6HttpAddress) , GNUNET_TIME_UNIT_FOREVER_REL);
644 memcpy (&t6->ipv6_addr,
645 &((struct sockaddr_in6 *) addr)->sin6_addr,
646 sizeof (struct in6_addr));
647 t6->u6_port = htons (plugin->port_inbound);
648 plugin->env->notify_address(plugin->env->cls,"https",t6,sizeof (struct IPv6HttpAddress) , GNUNET_TIME_UNIT_FOREVER_REL);
655 void mhd_logger (void * arg, const char * fmt, va_list ap)
658 vsnprintf(text, 1024, fmt, ap);
660 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,"MHD: %s \n", text);
664 * Callback called by MHD when a connection is terminated
666 static void mhd_termination_cb (void *cls, struct MHD_Connection * connection, void **httpSessionCache)
668 struct Session * ps = *httpSessionCache;
671 struct HTTP_PeerContext * pc = ps->peercontext;
673 if (connection==ps->recv_endpoint)
675 #if DEBUG_CONNECTIONS
676 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: inbound connection from peer `%s' was terminated\n", ps, GNUNET_i2s(&pc->identity));
678 ps->recv_active = GNUNET_NO;
679 ps->recv_connected = GNUNET_NO;
680 ps->recv_endpoint = NULL;
682 if (connection==ps->send_endpoint)
685 ps->send_active = GNUNET_NO;
686 ps->send_connected = GNUNET_NO;
687 ps->send_endpoint = NULL;
688 #if DEBUG_CONNECTIONS
689 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: outbound connection from peer `%s' was terminated\n", ps, GNUNET_i2s(&pc->identity));
693 /* if both connections disconnected, remove session */
694 if ((ps->send_connected == GNUNET_NO) && (ps->recv_connected == GNUNET_NO))
696 GNUNET_STATISTICS_update (pc->plugin->env->stats,
697 gettext_noop ("# HTTPS inbound sessions for peers active"),
700 remove_session(pc,ps,GNUNET_YES,GNUNET_SYSERR);
704 static void mhd_write_mst_cb (void *cls,
706 const struct GNUNET_MessageHeader *message)
709 struct Session *ps = cls;
710 GNUNET_assert(ps != NULL);
712 struct HTTP_PeerContext *pc = ps->peercontext;
713 GNUNET_assert(pc != NULL);
715 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
716 "Connection %X: Forwarding message to transport service, type %u and size %u from `%s' (`%s')\n",
718 ntohs(message->type),
719 ntohs(message->size),
720 GNUNET_i2s(&(ps->peercontext)->identity),http_plugin_address_to_string(NULL,ps->addr,ps->addrlen));
722 pc->plugin->env->receive (ps->peercontext->plugin->env->cls,
730 * Check if ip is allowed to connect.
733 mhd_accept_cb (void *cls,
734 const struct sockaddr *addr, socklen_t addr_len)
737 struct Plugin *plugin = cls;
739 /* Every connection is accepted, nothing more to do here */
743 int mhd_send_callback (void *cls, uint64_t pos, char *buf, int max)
747 struct Session * ps = cls;
748 struct HTTP_PeerContext * pc;
749 struct HTTP_Message * msg;
751 GNUNET_assert (ps!=NULL);
752 pc = ps->peercontext;
753 msg = ps->pending_msgs_tail;
754 if (ps->send_force_disconnect==GNUNET_YES)
756 #if DEBUG_CONNECTIONS
757 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: outbound forced to disconnect\n",ps);
764 if ((msg->size-msg->pos) <= max)
766 memcpy(buf,&msg->buf[msg->pos],(msg->size-msg->pos));
767 bytes_read = msg->size-msg->pos;
768 msg->pos+=(msg->size-msg->pos);
772 memcpy(buf,&msg->buf[msg->pos],max);
777 if (msg->pos==msg->size)
779 if (NULL!=msg->transmit_cont)
780 msg->transmit_cont (msg->transmit_cont_cls,&pc->identity,GNUNET_OK);
781 remove_http_message(ps,msg);
788 * Process GET or PUT request received via MHD. For
789 * GET, queue response that will send back our pending
790 * messages. For PUT, process incoming data and send
791 * to GNUnet core. In either case, check if a session
792 * already exists and create a new one if not.
795 mdh_access_cb (void *cls,
796 struct MHD_Connection *mhd_connection,
800 const char *upload_data,
801 size_t * upload_data_size, void **httpSessionCache)
803 struct Plugin *plugin = cls;
804 struct MHD_Response *response;
805 const union MHD_ConnectionInfo * conn_info;
807 struct sockaddr_in *addrin;
808 struct sockaddr_in6 *addrin6;
810 char address[INET6_ADDRSTRLEN+14];
811 struct GNUNET_PeerIdentity pi_in;
814 struct IPv4HttpAddress ipv4addr;
815 struct IPv6HttpAddress ipv6addr;
817 struct HTTP_PeerContext *pc;
818 struct Session *ps = NULL;
819 struct Session *ps_tmp = NULL;
822 int send_error_to_client;
826 GNUNET_assert(cls !=NULL);
827 send_error_to_client = GNUNET_NO;
829 if (NULL == *httpSessionCache)
831 /* check url for peer identity , if invalid send HTTP 404*/
832 size_t len = strlen(&url[1]);
833 char * peer = GNUNET_malloc(104+1);
835 if ((len>104) && (url[104]==';'))
837 char * id = GNUNET_malloc((len-104)+1);
838 strcpy(id,&url[105]);
839 memcpy(peer,&url[1],103);
841 id_num = strtoul ( id, NULL , 10);
844 res = GNUNET_CRYPTO_hash_from_string (peer, &(pi_in.hashPubKey));
846 if ( GNUNET_SYSERR == res )
848 response = MHD_create_response_from_data (strlen (HTTP_ERROR_RESPONSE),HTTP_ERROR_RESPONSE, MHD_NO, MHD_NO);
849 res = MHD_queue_response (mhd_connection, MHD_HTTP_NOT_FOUND, response);
850 MHD_destroy_response (response);
851 #if DEBUG_CONNECTIONS
853 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Peer has no valid ident, sent HTTP 1.1/404\n");
855 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Peer has no valid ident, could not send error\n");
862 ps = *httpSessionCache;
863 pc = ps->peercontext;
866 if (NULL == *httpSessionCache)
868 /* get peer context */
869 pc = GNUNET_CONTAINER_multihashmap_get (plugin->peers, &pi_in.hashPubKey);
873 pc = GNUNET_malloc(sizeof (struct HTTP_PeerContext));
875 pc->session_id_counter=1;
876 pc->last_session = NULL;
877 memcpy(&pc->identity, &pi_in, sizeof(struct GNUNET_PeerIdentity));
878 GNUNET_CONTAINER_multihashmap_put(plugin->peers, &pc->identity.hashPubKey, pc, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
879 GNUNET_STATISTICS_update (plugin->env->stats,
880 gettext_noop ("# HTTP peers active"),
885 conn_info = MHD_get_connection_info(mhd_connection, MHD_CONNECTION_INFO_CLIENT_ADDRESS );
886 /* Incoming IPv4 connection */
887 if ( AF_INET == conn_info->client_addr->sin_family)
889 addrin = conn_info->client_addr;
890 inet_ntop(addrin->sin_family, &(addrin->sin_addr),address,INET_ADDRSTRLEN);
891 memcpy(&ipv4addr.ipv4_addr,&(addrin->sin_addr),sizeof(struct in_addr));
892 ipv4addr.u_port = addrin->sin_port;
894 addr_len = sizeof(struct IPv4HttpAddress);
896 /* Incoming IPv6 connection */
897 if ( AF_INET6 == conn_info->client_addr->sin_family)
899 addrin6 = (struct sockaddr_in6 *) conn_info->client_addr;
900 inet_ntop(addrin6->sin6_family, &(addrin6->sin6_addr),address,INET6_ADDRSTRLEN);
901 memcpy(&ipv6addr.ipv6_addr,&(addrin6->sin6_addr),sizeof(struct in6_addr));
902 ipv6addr.u6_port = addrin6->sin6_port;
904 addr_len = sizeof(struct IPv6HttpAddress);
907 GNUNET_assert (addr != NULL);
908 GNUNET_assert (addr_len != 0);
911 /* only inbound sessions here */
916 if ((ps_tmp->direction==INBOUND) && (ps_tmp->session_id == id_num) && (id_num!=0))
918 if ((ps_tmp->recv_force_disconnect!=GNUNET_YES) && (ps_tmp->send_force_disconnect!=GNUNET_YES))
927 ps = GNUNET_malloc(sizeof (struct Session));
928 ps->addr = GNUNET_malloc(addr_len);
929 memcpy(ps->addr,addr,addr_len);
930 ps->addrlen = addr_len;
931 ps->direction=INBOUND;
932 ps->pending_msgs_head = NULL;
933 ps->pending_msgs_tail = NULL;
934 ps->send_connected=GNUNET_NO;
935 ps->send_active=GNUNET_NO;
936 ps->recv_connected=GNUNET_NO;
937 ps->recv_active=GNUNET_NO;
939 ps->session_id =id_num;
940 ps->url = create_url (plugin, ps->addr, ps->addrlen, ps->session_id);
941 GNUNET_CONTAINER_DLL_insert(pc->head,pc->tail,ps);
942 GNUNET_STATISTICS_update (plugin->env->stats,
943 gettext_noop ("# HTTPS inbound sessions for peers active"),
948 *httpSessionCache = ps;
949 if (ps->msgtok==NULL)
950 ps->msgtok = GNUNET_SERVER_mst_create (&mhd_write_mst_cb, ps);
952 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: HTTPS Daemon has new an incoming `%s' request from peer `%s' (`%s')\n",
955 GNUNET_i2s(&pc->identity),
956 http_plugin_address_to_string(NULL, ps->addr, ps->addrlen));
960 /* Is it a PUT or a GET request */
961 if (0 == strcmp (MHD_HTTP_METHOD_PUT, method))
963 if (ps->recv_force_disconnect == GNUNET_YES)
965 #if DEBUG_CONNECTIONS
966 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: inbound connection was forced to disconnect\n",ps);
968 ps->recv_active = GNUNET_NO;
971 if ((*upload_data_size == 0) && (ps->recv_active==GNUNET_NO))
973 ps->recv_endpoint = mhd_connection;
974 ps->recv_connected = GNUNET_YES;
975 ps->recv_active = GNUNET_YES;
976 ps->recv_force_disconnect = GNUNET_NO;
977 #if DEBUG_CONNECTIONS
978 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: inbound PUT connection connected\n",ps);
983 /* Transmission of all data complete */
984 if ((*upload_data_size == 0) && (ps->recv_active == GNUNET_YES))
986 response = MHD_create_response_from_data (strlen (HTTP_PUT_RESPONSE),HTTP_PUT_RESPONSE, MHD_NO, MHD_NO);
987 res = MHD_queue_response (mhd_connection, MHD_HTTP_OK, response);
988 #if DEBUG_CONNECTIONS
989 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: Sent HTTP/1.1: 200 OK as PUT Response\n",ps);
991 MHD_destroy_response (response);
992 ps->recv_active=GNUNET_NO;
997 if ((*upload_data_size > 0) && (ps->recv_active == GNUNET_YES))
999 res = GNUNET_SERVER_mst_receive(ps->msgtok, ps, upload_data,*upload_data_size, GNUNET_NO, GNUNET_NO);
1000 (*upload_data_size) = 0;
1006 if ( 0 == strcmp (MHD_HTTP_METHOD_GET, method) )
1008 if (ps->send_force_disconnect == GNUNET_YES)
1010 #if DEBUG_CONNECTIONS
1011 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: outbound connection was forced to disconnect\n",ps);
1013 ps->send_active = GNUNET_NO;
1016 ps->send_connected = GNUNET_YES;
1017 ps->send_active = GNUNET_YES;
1018 ps->send_endpoint = mhd_connection;
1019 ps->send_force_disconnect = GNUNET_NO;
1020 #if DEBUG_CONNECTIONS
1021 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: inbound GET connection connected\n",ps);
1023 response = MHD_create_response_from_callback(-1,32 * 1024, &mhd_send_callback, ps, NULL);
1024 res = MHD_queue_response (mhd_connection, MHD_HTTP_OK, response);
1025 MHD_destroy_response (response);
1032 * Function that queries MHD's select sets and
1033 * starts the task waiting for them.
1035 static GNUNET_SCHEDULER_TaskIdentifier
1036 http_server_daemon_prepare (void * cls, struct MHD_Daemon *daemon_handle)
1038 struct Plugin *plugin = cls;
1039 GNUNET_SCHEDULER_TaskIdentifier ret;
1043 struct GNUNET_NETWORK_FDSet *wrs;
1044 struct GNUNET_NETWORK_FDSet *wws;
1045 struct GNUNET_NETWORK_FDSet *wes;
1047 unsigned long long timeout;
1049 struct GNUNET_TIME_Relative tv;
1051 GNUNET_assert(cls !=NULL);
1052 ret = GNUNET_SCHEDULER_NO_TASK;
1056 wrs = GNUNET_NETWORK_fdset_create ();
1057 wes = GNUNET_NETWORK_fdset_create ();
1058 wws = GNUNET_NETWORK_fdset_create ();
1060 GNUNET_assert (MHD_YES ==
1061 MHD_get_fdset (daemon_handle,
1066 haveto = MHD_get_timeout (daemon_handle, &timeout);
1067 if (haveto == MHD_YES)
1068 tv.value = (uint64_t) timeout;
1070 tv = GNUNET_TIME_UNIT_FOREVER_REL;
1071 GNUNET_NETWORK_fdset_copy_native (wrs, &rs, max);
1072 GNUNET_NETWORK_fdset_copy_native (wws, &ws, max);
1073 GNUNET_NETWORK_fdset_copy_native (wes, &es, max);
1074 if (daemon_handle == plugin->http_server_daemon_v4)
1076 if (plugin->http_server_task_v4 != GNUNET_SCHEDULER_NO_TASK)
1078 GNUNET_SCHEDULER_cancel(plugin->env->sched, plugin->http_server_task_v4);
1079 plugin->http_server_daemon_v4 = GNUNET_SCHEDULER_NO_TASK;
1082 ret = GNUNET_SCHEDULER_add_select (plugin->env->sched,
1083 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1084 GNUNET_SCHEDULER_NO_TASK,
1088 &http_server_daemon_v4_run,
1091 if (daemon_handle == plugin->http_server_daemon_v6)
1093 if (plugin->http_server_task_v6 != GNUNET_SCHEDULER_NO_TASK)
1095 GNUNET_SCHEDULER_cancel(plugin->env->sched, plugin->http_server_task_v6);
1096 plugin->http_server_task_v6 = GNUNET_SCHEDULER_NO_TASK;
1099 ret = GNUNET_SCHEDULER_add_select (plugin->env->sched,
1100 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1101 GNUNET_SCHEDULER_NO_TASK,
1105 &http_server_daemon_v6_run,
1108 GNUNET_NETWORK_fdset_destroy (wrs);
1109 GNUNET_NETWORK_fdset_destroy (wws);
1110 GNUNET_NETWORK_fdset_destroy (wes);
1115 * Call MHD to process pending requests and then go back
1116 * and schedule the next run.
1118 static void http_server_daemon_v4_run (void *cls,
1119 const struct GNUNET_SCHEDULER_TaskContext *tc)
1121 struct Plugin *plugin = cls;
1123 GNUNET_assert(cls !=NULL);
1124 plugin->http_server_task_v4 = GNUNET_SCHEDULER_NO_TASK;
1126 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1129 GNUNET_assert (MHD_YES == MHD_run (plugin->http_server_daemon_v4));
1130 plugin->http_server_task_v4 = http_server_daemon_prepare (plugin, plugin->http_server_daemon_v4);
1136 * Call MHD to process pending requests and then go back
1137 * and schedule the next run.
1139 static void http_server_daemon_v6_run (void *cls,
1140 const struct GNUNET_SCHEDULER_TaskContext *tc)
1142 struct Plugin *plugin = cls;
1144 GNUNET_assert(cls !=NULL);
1145 plugin->http_server_task_v6 = GNUNET_SCHEDULER_NO_TASK;
1147 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1150 GNUNET_assert (MHD_YES == MHD_run (plugin->http_server_daemon_v6));
1151 plugin->http_server_task_v6 = http_server_daemon_prepare (plugin, plugin->http_server_daemon_v6);
1155 static size_t curl_get_header_cb( void *ptr, size_t size, size_t nmemb, void *stream)
1157 struct Session * ps = stream;
1159 long http_result = 0;
1161 /* Getting last http result code */
1162 GNUNET_assert(NULL!=ps);
1163 if (ps->recv_connected==GNUNET_NO)
1165 res = curl_easy_getinfo(ps->recv_endpoint, CURLINFO_RESPONSE_CODE, &http_result);
1166 if (CURLE_OK == res)
1168 if (http_result == 200)
1170 ps->recv_connected = GNUNET_YES;
1171 ps->recv_active = GNUNET_YES;
1172 #if DEBUG_CONNECTIONS
1173 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: connected to recieve data\n",ps);
1175 // Calling send_check_connections again since receive is established
1176 send_check_connections (ps->peercontext->plugin, ps);
1183 size_t len = size * nmemb;
1186 if ((size * nmemb) < SIZE_MAX)
1187 tmp = GNUNET_malloc (len+1);
1189 if ((tmp != NULL) && (len > 0))
1191 memcpy(tmp,ptr,len);
1194 if (tmp[len-2] == 13)
1198 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: Header: %s\n",ps,tmp);
1205 return size * nmemb;
1208 static size_t curl_put_header_cb( void *ptr, size_t size, size_t nmemb, void *stream)
1210 struct Session * ps = stream;
1213 size_t len = size * nmemb;
1214 long http_result = 0;
1217 /* Getting last http result code */
1218 GNUNET_assert(NULL!=ps);
1219 res = curl_easy_getinfo(ps->send_endpoint, CURLINFO_RESPONSE_CODE, &http_result);
1220 if (CURLE_OK == res)
1222 if ((http_result == 100) && (ps->send_connected==GNUNET_NO))
1224 ps->send_connected = GNUNET_YES;
1225 ps->send_active = GNUNET_YES;
1226 #if DEBUG_CONNECTIONS
1227 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: connected to send data\n",ps);
1230 if ((http_result == 200) && (ps->send_connected==GNUNET_YES))
1232 ps->send_connected = GNUNET_NO;
1233 ps->send_active = GNUNET_NO;
1234 #if DEBUG_CONNECTIONS
1235 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: sending disconnected\n",ps);
1241 if ((size * nmemb) < SIZE_MAX)
1242 tmp = GNUNET_malloc (len+1);
1244 if ((tmp != NULL) && (len > 0))
1246 memcpy(tmp,ptr,len);
1249 if (tmp[len-2] == 13)
1256 return size * nmemb;
1260 * Callback method used with libcurl
1261 * Method is called when libcurl needs to read data during sending
1262 * @param stream pointer where to write data
1263 * @param size size of an individual element
1264 * @param nmemb count of elements that can be written to the buffer
1265 * @param ptr source pointer, passed to the libcurl handle
1266 * @return bytes written to stream
1268 static size_t curl_send_cb(void *stream, size_t size, size_t nmemb, void *ptr)
1270 struct Session * ps = ptr;
1271 struct HTTP_Message * msg = ps->pending_msgs_tail;
1275 if (ps->send_active == GNUNET_NO)
1276 return CURL_READFUNC_PAUSE;
1278 if ((ps->pending_msgs_tail == NULL) && (ps->send_active == GNUNET_YES))
1280 #if DEBUG_CONNECTIONS
1281 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: No Message to send, pausing connection\n",ps);
1283 ps->send_active = GNUNET_NO;
1284 return CURL_READFUNC_PAUSE;
1287 GNUNET_assert (msg!=NULL);
1290 if (msg->pos < msg->size)
1292 /* data fit in buffer */
1293 if ((msg->size - msg->pos) <= (size * nmemb))
1295 len = (msg->size - msg->pos);
1296 memcpy(stream, &msg->buf[msg->pos], len);
1303 memcpy(stream, &msg->buf[msg->pos], len);
1308 /* no data to send */
1314 if ( msg->pos == msg->size)
1317 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: Message with %u bytes sent, removing message from queue \n",ps, msg->pos);
1319 /* Calling transmit continuation */
1320 if (NULL != msg->transmit_cont)
1321 msg->transmit_cont (msg->transmit_cont_cls,&(ps->peercontext)->identity,GNUNET_OK);
1322 remove_http_message(ps, msg);
1327 static void curl_receive_mst_cb (void *cls,
1329 const struct GNUNET_MessageHeader *message)
1331 struct Session *ps = cls;
1332 GNUNET_assert(ps != NULL);
1334 struct HTTP_PeerContext *pc = ps->peercontext;
1335 GNUNET_assert(pc != NULL);
1338 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1339 "Connection %X: Forwarding message to transport service, type %u and size %u from `%s' (`%s')\n",
1341 ntohs(message->type),
1342 ntohs(message->size),
1343 GNUNET_i2s(&(pc->identity)),http_plugin_address_to_string(NULL,ps->addr,ps->addrlen));
1345 pc->plugin->env->receive (pc->plugin->env->cls,
1354 * Callback method used with libcurl
1355 * Method is called when libcurl needs to write data during sending
1356 * @param stream pointer where to write data
1357 * @param size size of an individual element
1358 * @param nmemb count of elements that can be written to the buffer
1359 * @param ptr destination pointer, passed to the libcurl handle
1360 * @return bytes read from stream
1362 static size_t curl_receive_cb( void *stream, size_t size, size_t nmemb, void *ptr)
1364 struct Session * ps = ptr;
1365 #if DEBUG_CONNECTIONS
1366 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: %u bytes received\n",ps, size*nmemb);
1368 GNUNET_SERVER_mst_receive(ps->msgtok, ps, stream, size*nmemb, GNUNET_NO, GNUNET_NO);
1369 return (size * nmemb);
1373 static void curl_perform (void *cls,
1374 const struct GNUNET_SCHEDULER_TaskContext *tc)
1376 struct Plugin *plugin = cls;
1377 static unsigned int handles_last_run;
1379 struct CURLMsg *msg;
1381 struct Session *ps = NULL;
1382 struct HTTP_PeerContext *pc = NULL;
1383 struct HTTP_Message * cur_msg = NULL;
1387 GNUNET_assert(cls !=NULL);
1389 plugin->http_curl_task = GNUNET_SCHEDULER_NO_TASK;
1390 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1396 mret = curl_multi_perform (plugin->multi_handle, &running);
1397 if ((running < handles_last_run) && (running>0))
1402 msg = curl_multi_info_read (plugin->multi_handle, &running);
1405 /* get session for affected curl handle */
1406 GNUNET_assert ( msg->easy_handle != NULL );
1407 curl_easy_getinfo(msg->easy_handle, CURLINFO_PRIVATE, &tmp);
1408 ps = (struct Session *) tmp;
1409 GNUNET_assert ( ps != NULL );
1410 pc = ps->peercontext;
1411 GNUNET_assert ( pc != NULL );
1416 if ( (msg->data.result != CURLE_OK) &&
1417 (msg->data.result != CURLE_GOT_NOTHING) )
1419 /* sending msg failed*/
1420 if (msg->easy_handle == ps->send_endpoint)
1422 #if DEBUG_CONNECTIONS
1423 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1424 _("Connection %X: HTTPS PUT to peer `%s' (`%s') failed: `%s' `%s'\n"),
1426 GNUNET_i2s(&pc->identity),
1427 http_plugin_address_to_string(NULL, ps->addr, ps->addrlen),
1428 "curl_multi_perform",
1429 curl_easy_strerror (msg->data.result));
1431 ps->send_connected = GNUNET_NO;
1432 ps->send_active = GNUNET_NO;
1433 curl_multi_remove_handle(plugin->multi_handle,ps->send_endpoint);
1434 //curl_easy_cleanup(ps->send_endpoint);
1435 //ps->send_endpoint=NULL;
1436 cur_msg = ps->pending_msgs_tail;
1437 if (( NULL != cur_msg) && ( NULL != cur_msg->transmit_cont))
1438 cur_msg->transmit_cont (cur_msg->transmit_cont_cls,&pc->identity,GNUNET_SYSERR);
1440 /* GET connection failed */
1441 if (msg->easy_handle == ps->recv_endpoint)
1443 #if DEBUG_CONNECTIONS
1444 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1445 _("Connection %X: HTTPS GET to peer `%s' (`%s') failed: `%s' `%s'\n"),
1447 GNUNET_i2s(&pc->identity),
1448 http_plugin_address_to_string(NULL, ps->addr, ps->addrlen),
1449 "curl_multi_perform",
1450 curl_easy_strerror (msg->data.result));
1452 ps->recv_connected = GNUNET_NO;
1453 ps->recv_active = GNUNET_NO;
1454 curl_multi_remove_handle(plugin->multi_handle,ps->recv_endpoint);
1455 //curl_easy_cleanup(ps->recv_endpoint);
1456 //ps->recv_endpoint=NULL;
1461 if (msg->easy_handle == ps->send_endpoint)
1463 GNUNET_assert (CURLE_OK == curl_easy_getinfo(msg->easy_handle, CURLINFO_RESPONSE_CODE, &http_result));
1464 #if DEBUG_CONNECTIONS
1465 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1466 "Connection %X: HTTPS PUT connection to peer `%s' (`%s') was closed with HTTP code %u\n",
1468 GNUNET_i2s(&pc->identity),
1469 http_plugin_address_to_string(NULL, ps->addr, ps->addrlen),
1472 /* Calling transmit continuation */
1473 cur_msg = ps->pending_msgs_tail;
1474 if (( NULL != cur_msg) && (NULL != cur_msg->transmit_cont))
1476 /* HTTP 1xx : Last message before here was informational */
1477 if ((http_result >=100) && (http_result < 200))
1478 cur_msg->transmit_cont (cur_msg->transmit_cont_cls,&pc->identity,GNUNET_OK);
1479 /* HTTP 2xx: successful operations */
1480 if ((http_result >=200) && (http_result < 300))
1481 cur_msg->transmit_cont (cur_msg->transmit_cont_cls,&pc->identity,GNUNET_OK);
1482 /* HTTP 3xx..5xx: error */
1483 if ((http_result >=300) && (http_result < 600))
1484 cur_msg->transmit_cont (cur_msg->transmit_cont_cls,&pc->identity,GNUNET_SYSERR);
1486 ps->send_connected = GNUNET_NO;
1487 ps->send_active = GNUNET_NO;
1488 curl_multi_remove_handle(plugin->multi_handle,ps->send_endpoint);
1489 //curl_easy_cleanup(ps->send_endpoint);
1490 //ps->send_endpoint =NULL;
1492 if (msg->easy_handle == ps->recv_endpoint)
1494 #if DEBUG_CONNECTIONS
1495 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1496 "Connection %X: HTTP GET connection to peer `%s' (`%s') was closed with HTTP code %u\n",
1498 GNUNET_i2s(&pc->identity),
1499 http_plugin_address_to_string(NULL, ps->addr, ps->addrlen),
1502 ps->recv_connected = GNUNET_NO;
1503 ps->recv_active = GNUNET_NO;
1504 curl_multi_remove_handle(plugin->multi_handle,ps->recv_endpoint);
1505 //curl_easy_cleanup(ps->recv_endpoint);
1506 //ps->recv_endpoint=NULL;
1509 if ((ps->recv_connected == GNUNET_NO) && (ps->send_connected == GNUNET_NO))
1510 remove_session (pc, ps, GNUNET_YES, GNUNET_SYSERR);
1517 while ( (running > 0) );
1519 handles_last_run = running;
1521 while (mret == CURLM_CALL_MULTI_PERFORM);
1522 curl_schedule(plugin);
1527 * Function setting up file descriptors and scheduling task to run
1528 * @param ses session to send data to
1529 * @return GNUNET_SYSERR for hard failure, GNUNET_OK for ok
1532 http_plugin_disconnect (void *cls,
1533 const struct GNUNET_PeerIdentity *target)
1537 struct Plugin *plugin = cls;
1538 struct HTTP_PeerContext *pc = NULL;
1539 struct Session *ps = NULL;
1540 //struct Session *tmp = NULL;
1542 pc = GNUNET_CONTAINER_multihashmap_get (plugin->peers, &target->hashPubKey);
1549 /* Telling transport that session is getting disconnected */
1550 plugin->env->session_end(plugin, target, ps);
1551 if (ps->direction==OUTBOUND)
1553 if (ps->send_endpoint!=NULL)
1555 //GNUNET_assert(CURLM_OK == curl_multi_remove_handle(plugin->multi_handle,ps->send_endpoint));
1556 //curl_easy_cleanup(ps->send_endpoint);
1557 //ps->send_endpoint=NULL;
1558 ps->send_force_disconnect = GNUNET_YES;
1560 if (ps->recv_endpoint!=NULL)
1562 //GNUNET_assert(CURLM_OK == curl_multi_remove_handle(plugin->multi_handle,ps->recv_endpoint));
1563 //curl_easy_cleanup(ps->recv_endpoint);
1564 //ps->recv_endpoint=NULL;
1565 ps->recv_force_disconnect = GNUNET_YES;
1569 if (ps->direction==INBOUND)
1571 ps->recv_force_disconnect = GNUNET_YES;
1572 ps->send_force_disconnect = GNUNET_YES;
1575 while (ps->pending_msgs_head!=NULL)
1577 remove_http_message(ps, ps->pending_msgs_head);
1579 ps->recv_active = GNUNET_NO;
1580 ps->send_active = GNUNET_NO;
1586 static int curl_schedule(void *cls)
1588 struct Plugin *plugin = cls;
1593 struct GNUNET_NETWORK_FDSet *grs;
1594 struct GNUNET_NETWORK_FDSet *gws;
1598 GNUNET_assert(cls !=NULL);
1600 /* Cancel previous scheduled task */
1601 if (plugin->http_curl_task != GNUNET_SCHEDULER_NO_TASK)
1603 GNUNET_SCHEDULER_cancel(plugin->env->sched, plugin->http_curl_task);
1604 plugin->http_curl_task = GNUNET_SCHEDULER_NO_TASK;
1610 mret = curl_multi_fdset (plugin->multi_handle, &rs, &ws, &es, &max);
1611 if (mret != CURLM_OK)
1613 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1614 _("%s failed at %s:%d: `%s'\n"),
1615 "curl_multi_fdset", __FILE__, __LINE__,
1616 curl_multi_strerror (mret));
1617 return GNUNET_SYSERR;
1619 mret = curl_multi_timeout (plugin->multi_handle, &to);
1620 if (mret != CURLM_OK)
1622 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1623 _("%s failed at %s:%d: `%s'\n"),
1624 "curl_multi_timeout", __FILE__, __LINE__,
1625 curl_multi_strerror (mret));
1626 return GNUNET_SYSERR;
1629 grs = GNUNET_NETWORK_fdset_create ();
1630 gws = GNUNET_NETWORK_fdset_create ();
1631 GNUNET_NETWORK_fdset_copy_native (grs, &rs, max + 1);
1632 GNUNET_NETWORK_fdset_copy_native (gws, &ws, max + 1);
1633 plugin->http_curl_task = GNUNET_SCHEDULER_add_select (plugin->env->sched,
1634 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1635 GNUNET_SCHEDULER_NO_TASK,
1636 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 0),
1641 GNUNET_NETWORK_fdset_destroy (gws);
1642 GNUNET_NETWORK_fdset_destroy (grs);
1647 * Function setting up curl handle and selecting message to send
1649 * @param ses session to send data to
1650 * @param con connection
1651 * @return GNUNET_SYSERR on failure, GNUNET_NO if connecting, GNUNET_YES if ok
1653 static ssize_t send_check_connections (void *cls, struct Session *ps)
1655 struct Plugin *plugin = cls;
1657 struct HTTP_Message * msg;
1659 struct GNUNET_TIME_Relative timeout = GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT;
1661 GNUNET_assert(cls !=NULL);
1663 if (ps->direction == OUTBOUND)
1665 /* RECV DIRECTION */
1666 /* Check if session is connected to receive data, otherwise connect to peer */
1667 if (ps->recv_connected == GNUNET_NO)
1669 int fresh = GNUNET_NO;
1670 if (ps->recv_endpoint == NULL)
1673 ps->recv_endpoint = curl_easy_init();
1676 curl_easy_setopt(ps->recv_endpoint, CURLOPT_VERBOSE, 1L);
1678 curl_easy_setopt(ps->recv_endpoint, CURLOPT_URL, ps->url);
1679 curl_easy_setopt (ps->recv_endpoint, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1);
1680 //curl_easy_setopt (ps->recv_endpoint, CURLOPT_SSL_CIPHER_LIST, cipher_suite);
1681 curl_easy_setopt(ps->recv_endpoint, CURLOPT_SSL_VERIFYPEER, 0);
1682 curl_easy_setopt(ps->recv_endpoint, CURLOPT_SSL_VERIFYHOST, 0);
1683 curl_easy_setopt(ps->recv_endpoint, CURLOPT_HEADERFUNCTION, &curl_get_header_cb);
1684 curl_easy_setopt(ps->recv_endpoint, CURLOPT_WRITEHEADER, ps);
1685 curl_easy_setopt(ps->recv_endpoint, CURLOPT_READFUNCTION, curl_send_cb);
1686 curl_easy_setopt(ps->recv_endpoint, CURLOPT_READDATA, ps);
1687 curl_easy_setopt(ps->recv_endpoint, CURLOPT_WRITEFUNCTION, curl_receive_cb);
1688 curl_easy_setopt(ps->recv_endpoint, CURLOPT_WRITEDATA, ps);
1689 curl_easy_setopt(ps->recv_endpoint, CURLOPT_TIMEOUT, (long) timeout.value);
1690 curl_easy_setopt(ps->recv_endpoint, CURLOPT_PRIVATE, ps);
1691 curl_easy_setopt(ps->recv_endpoint, CURLOPT_CONNECTTIMEOUT, HTTP_CONNECT_TIMEOUT);
1692 curl_easy_setopt(ps->recv_endpoint, CURLOPT_BUFFERSIZE, GNUNET_SERVER_MAX_MESSAGE_SIZE);
1693 #if CURL_TCP_NODELAY
1694 curl_easy_setopt(ps->recv_endpoint, CURLOPT_TCP_NODELAY, 1);
1697 if (fresh==GNUNET_YES)
1699 mret = curl_multi_add_handle(plugin->multi_handle, ps->recv_endpoint);
1700 if (mret != CURLM_OK)
1702 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1703 _("Connection: %X: %s failed at %s:%d: `%s'\n"),
1705 "curl_multi_add_handle", __FILE__, __LINE__,
1706 curl_multi_strerror (mret));
1707 return GNUNET_SYSERR;
1710 if (curl_schedule (plugin) == GNUNET_SYSERR)
1712 #if DEBUG_CONNECTIONS
1713 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: could not schedule curl task\n",ps);
1715 return GNUNET_SYSERR;
1717 #if DEBUG_CONNECTIONS
1718 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: inbound not connected, initiating connection\n",ps);
1722 /* waiting for receive direction */
1723 if (ps->recv_connected==GNUNET_NO)
1726 /* SEND DIRECTION */
1727 /* Check if session is connected to send data, otherwise connect to peer */
1728 if ((ps->send_connected == GNUNET_YES) && (ps->send_endpoint!= NULL))
1730 if (ps->send_active == GNUNET_YES)
1732 #if DEBUG_CONNECTIONS
1733 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: outbound active, enqueueing message\n",ps);
1737 if (ps->send_active == GNUNET_NO)
1739 #if DEBUG_CONNECTIONS
1740 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: outbound paused, unpausing existing connection and enqueueing message\n",ps);
1742 if (CURLE_OK == curl_easy_pause(ps->send_endpoint,CURLPAUSE_CONT))
1744 ps->send_active=GNUNET_YES;
1748 return GNUNET_SYSERR;
1751 /* not connected, initiate connection */
1752 if (ps->send_connected==GNUNET_NO)
1754 int fresh = GNUNET_NO;
1755 if (NULL == ps->send_endpoint)
1757 ps->send_endpoint = curl_easy_init();
1760 GNUNET_assert (ps->send_endpoint != NULL);
1761 GNUNET_assert (NULL != ps->pending_msgs_tail);
1762 #if DEBUG_CONNECTIONS
1763 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: outbound not connected, initiating connection\n",ps);
1765 ps->send_active = GNUNET_NO;
1766 msg = ps->pending_msgs_tail;
1769 curl_easy_setopt(ps->send_endpoint, CURLOPT_VERBOSE, 1L);
1771 curl_easy_setopt(ps->send_endpoint, CURLOPT_URL, ps->url);
1772 curl_easy_setopt(ps->send_endpoint, CURLOPT_PUT, 1L);
1773 curl_easy_setopt(ps->send_endpoint, CURLOPT_HEADERFUNCTION, &curl_put_header_cb);
1774 curl_easy_setopt(ps->send_endpoint, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1);
1775 curl_easy_setopt(ps->send_endpoint, CURLOPT_SSL_VERIFYPEER, 0);
1776 curl_easy_setopt(ps->send_endpoint, CURLOPT_SSL_VERIFYHOST, 0);
1778 curl_easy_setopt(ps->send_endpoint, CURLOPT_WRITEHEADER, ps);
1779 curl_easy_setopt(ps->send_endpoint, CURLOPT_READFUNCTION, curl_send_cb);
1780 curl_easy_setopt(ps->send_endpoint, CURLOPT_READDATA, ps);
1781 curl_easy_setopt(ps->send_endpoint, CURLOPT_WRITEFUNCTION, curl_receive_cb);
1782 curl_easy_setopt(ps->send_endpoint, CURLOPT_READDATA, ps);
1783 curl_easy_setopt(ps->send_endpoint, CURLOPT_TIMEOUT, (long) timeout.value);
1784 curl_easy_setopt(ps->send_endpoint, CURLOPT_PRIVATE, ps);
1785 curl_easy_setopt(ps->send_endpoint, CURLOPT_CONNECTTIMEOUT, HTTP_CONNECT_TIMEOUT);
1786 curl_easy_setopt(ps->send_endpoint, CURLOPT_BUFFERSIZE, GNUNET_SERVER_MAX_MESSAGE_SIZE);
1787 #if CURL_TCP_NODELAY
1788 curl_easy_setopt(ps->send_endpoint, CURLOPT_TCP_NODELAY, 1);
1791 if (fresh==GNUNET_YES)
1793 mret = curl_multi_add_handle(plugin->multi_handle, ps->send_endpoint);
1794 if (mret != CURLM_OK)
1796 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1797 _("Connection: %X: %s failed at %s:%d: `%s'\n"),
1799 "curl_multi_add_handle", __FILE__, __LINE__,
1800 curl_multi_strerror (mret));
1801 return GNUNET_SYSERR;
1805 if (curl_schedule (plugin) == GNUNET_SYSERR)
1806 return GNUNET_SYSERR;
1809 if (ps->direction == INBOUND)
1811 GNUNET_assert (NULL != ps->pending_msgs_tail);
1812 if ((ps->recv_connected==GNUNET_YES) && (ps->send_connected==GNUNET_YES) &&
1813 (ps->recv_force_disconnect==GNUNET_NO) && (ps->recv_force_disconnect==GNUNET_NO))
1816 return GNUNET_SYSERR;
1819 static struct Session * send_select_session (void * cls, struct HTTP_PeerContext *pc, const void * addr, size_t addrlen, int force_address, struct Session * session)
1821 struct Session * tmp = NULL;
1822 int addr_given = GNUNET_NO;
1824 if ((addr!=NULL) && (addrlen>0))
1825 addr_given = GNUNET_YES;
1827 if (force_address == GNUNET_YES)
1829 /* check session given as argument */
1830 if ((session != NULL) && (addr_given == GNUNET_YES))
1832 if (0 == memcmp(session->addr, addr, addrlen))
1834 /* connection can not be used, since it is disconnected */
1835 if ((session->recv_force_disconnect==GNUNET_NO) && (session->send_force_disconnect==GNUNET_NO))
1837 #if DEBUG_SESSION_SELECTION
1838 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Session %X selected: Using session passed by transport to send to forced address \n", session);
1844 /* check last session used */
1845 if ((pc->last_session != NULL)&& (addr_given == GNUNET_YES))
1847 if (0 == memcmp(pc->last_session->addr, addr, addrlen))
1849 /* connection can not be used, since it is disconnected */
1850 if ((pc->last_session->recv_force_disconnect==GNUNET_NO) && (pc->last_session->send_force_disconnect==GNUNET_NO))
1852 #if DEBUG_SESSION_SELECTION
1853 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Session %X selected: Using last session used to send to forced address \n", pc->last_session);
1855 return pc->last_session;
1859 /* find session in existing sessions */
1861 while ((tmp!=NULL) && (addr_given == GNUNET_YES))
1864 if (0 == memcmp(tmp->addr, addr, addrlen))
1866 /* connection can not be used, since it is disconnected */
1867 if ((tmp->recv_force_disconnect==GNUNET_NO) && (tmp->send_force_disconnect==GNUNET_NO))
1869 #if DEBUG_SESSION_SELECTION
1870 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Session %X selected: Using existing session to send to forced address \n", session);
1878 /* no session to use */
1881 if ((force_address == GNUNET_NO) || (force_address == GNUNET_SYSERR))
1883 /* check session given as argument */
1884 if (session != NULL)
1886 /* connection can not be used, since it is disconnected */
1887 if ((session->recv_force_disconnect==GNUNET_NO) && (session->send_force_disconnect==GNUNET_NO))
1889 #if DEBUG_SESSION_SELECTION
1890 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Session %X selected: Using session passed by transport to send not-forced address \n", session);
1896 /* check last session used */
1897 if (pc->last_session != NULL)
1899 /* connection can not be used, since it is disconnected */
1900 if ((pc->last_session->recv_force_disconnect==GNUNET_NO) && (pc->last_session->send_force_disconnect==GNUNET_NO))
1902 #if DEBUG_SESSION_SELECTION
1903 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Session %X selected: Using last session to send to not-forced address \n", pc->last_session);
1905 return pc->last_session;
1908 /* find session in existing sessions */
1912 /* connection can not be used, since it is disconnected */
1913 if ((tmp->recv_force_disconnect==GNUNET_NO) && (tmp->send_force_disconnect==GNUNET_NO))
1915 #if DEBUG_SESSION_SELECTION
1916 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Session %X selected: Using existing session to send to not-forced address \n", tmp);
1928 * Function that can be used by the transport service to transmit
1929 * a message using the plugin. Note that in the case of a
1930 * peer disconnecting, the continuation MUST be called
1931 * prior to the disconnect notification itself. This function
1932 * will be called with this peer's HELLO message to initiate
1933 * a fresh connection to another peer.
1935 * @param cls closure
1936 * @param target who should receive this message
1937 * @param msgbuf the message to transmit
1938 * @param msgbuf_size number of bytes in 'msgbuf'
1939 * @param priority how important is the message (most plugins will
1940 * ignore message priority and just FIFO)
1941 * @param timeout how long to wait at most for the transmission (does not
1942 * require plugins to discard the message after the timeout,
1943 * just advisory for the desired delay; most plugins will ignore
1945 * @param session which session must be used (or NULL for "any")
1946 * @param addr the address to use (can be NULL if the plugin
1947 * is "on its own" (i.e. re-use existing TCP connection))
1948 * @param addrlen length of the address in bytes
1949 * @param force_address GNUNET_YES if the plugin MUST use the given address,
1950 * GNUNET_NO means the plugin may use any other address and
1951 * GNUNET_SYSERR means that only reliable existing
1952 * bi-directional connections should be used (regardless
1954 * @param cont continuation to call once the message has
1955 * been transmitted (or if the transport is ready
1956 * for the next transmission call; or if the
1957 * peer disconnected...); can be NULL
1958 * @param cont_cls closure for cont
1959 * @return number of bytes used (on the physical network, with overheads);
1960 * -1 on hard errors (i.e. address invalid); 0 is a legal value
1961 * and does NOT mean that the message was not transmitted (DV)
1964 http_plugin_send (void *cls,
1965 const struct GNUNET_PeerIdentity *target,
1968 unsigned int priority,
1969 struct GNUNET_TIME_Relative to,
1970 struct Session *session,
1974 GNUNET_TRANSPORT_TransmitContinuation cont,
1977 struct Plugin *plugin = cls;
1978 struct HTTP_Message *msg;
1979 struct HTTP_PeerContext * pc;
1980 struct Session * ps = NULL;
1982 GNUNET_assert(cls !=NULL);
1985 char * force = GNUNET_malloc(40);
1986 if (force_address == GNUNET_YES)
1987 strcpy(force,"forced addr.");
1988 if (force_address == GNUNET_NO)
1989 strcpy(force,"any addr.");
1990 if (force_address == GNUNET_SYSERR)
1991 strcpy(force,"reliable bi-direc. address addr.");
1993 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Transport tells me to send %u bytes to `%s' using %s (%s) and session: %X\n",
1997 http_plugin_address_to_string(NULL, addr, addrlen),
2003 pc = GNUNET_CONTAINER_multihashmap_get (plugin->peers, &target->hashPubKey);
2007 pc = GNUNET_malloc(sizeof (struct HTTP_PeerContext));
2008 pc->plugin = plugin;
2009 pc->session_id_counter=1;
2010 pc->last_session = NULL;
2011 memcpy(&pc->identity, target, sizeof(struct GNUNET_PeerIdentity));
2012 GNUNET_CONTAINER_multihashmap_put(plugin->peers, &pc->identity.hashPubKey, pc, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
2013 GNUNET_STATISTICS_update (plugin->env->stats,
2014 gettext_noop ("# HTTP peers active"),
2019 ps = send_select_session (plugin, pc, addr, addrlen, force_address, session);
2021 /* session not existing, but address forced -> creating new session */
2024 if ((addr!=NULL) && (addrlen!=0))
2026 ps = GNUNET_malloc(sizeof (struct Session));
2027 #if DEBUG_SESSION_SELECTION
2028 if (force_address == GNUNET_YES)
2029 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"No existing connection & forced address: creating new session %X to peer %s\n", ps, GNUNET_i2s(target));
2030 if (force_address != GNUNET_YES)
2031 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"No existing connection: creating new session %X to peer %s\n", ps, GNUNET_i2s(target));
2033 if ((addrlen!=0) && (addr!=NULL))
2035 ps->addr = GNUNET_malloc(addrlen);
2036 memcpy(ps->addr,addr,addrlen);
2037 ps->addrlen = addrlen;
2044 ps->direction=OUTBOUND;
2045 ps->recv_connected = GNUNET_NO;
2046 ps->recv_force_disconnect = GNUNET_NO;
2047 ps->send_connected = GNUNET_NO;
2048 ps->send_force_disconnect = GNUNET_NO;
2049 ps->pending_msgs_head = NULL;
2050 ps->pending_msgs_tail = NULL;
2052 ps->session_id = pc->session_id_counter;
2053 pc->session_id_counter++;
2054 ps->url = create_url (plugin, ps->addr, ps->addrlen, ps->session_id);
2055 if (ps->msgtok == NULL)
2056 ps->msgtok = GNUNET_SERVER_mst_create (&curl_receive_mst_cb, ps);
2057 GNUNET_CONTAINER_DLL_insert(pc->head,pc->tail,ps);
2060 GNUNET_STATISTICS_update (plugin->env->stats,
2061 gettext_noop ("# HTTP outbound sessions for peers active"),
2068 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));
2070 return GNUNET_SYSERR;
2075 msg = GNUNET_malloc (sizeof (struct HTTP_Message) + msgbuf_size);
2077 msg->size = msgbuf_size;
2079 msg->buf = (char *) &msg[1];
2080 msg->transmit_cont = cont;
2081 msg->transmit_cont_cls = cont_cls;
2082 memcpy (msg->buf,msgbuf, msgbuf_size);
2083 GNUNET_CONTAINER_DLL_insert(ps->pending_msgs_head,ps->pending_msgs_tail,msg);
2085 if (send_check_connections (plugin, ps) != GNUNET_SYSERR)
2087 if (force_address != GNUNET_YES)
2088 pc->last_session = ps;
2090 if (pc->last_session==NULL)
2091 pc->last_session = ps;
2095 return GNUNET_SYSERR;
2101 * Function that can be used to force the plugin to disconnect
2102 * from the given peer and cancel all previous transmissions
2103 * (and their continuationc).
2105 * @param cls closure
2106 * @param target peer from which to disconnect
2109 * Convert the transports address to a nice, human-readable
2112 * @param cls closure
2113 * @param type name of the transport that generated the address
2114 * @param addr one of the addresses of the host, NULL for the last address
2115 * the specific address format depends on the transport
2116 * @param addrlen length of the address
2117 * @param numeric should (IP) addresses be displayed in numeric form?
2118 * @param timeout after how long should we give up?
2119 * @param asc function to call on each string
2120 * @param asc_cls closure for asc
2123 http_plugin_address_pretty_printer (void *cls,
2128 struct GNUNET_TIME_Relative timeout,
2129 GNUNET_TRANSPORT_AddressStringCallback
2132 const struct IPv4HttpAddress *t4;
2133 const struct IPv6HttpAddress *t6;
2134 struct sockaddr_in a4;
2135 struct sockaddr_in6 a6;
2141 GNUNET_assert(cls !=NULL);
2142 if (addrlen == sizeof (struct IPv6HttpAddress))
2144 address = GNUNET_malloc (INET6_ADDRSTRLEN);
2146 a6.sin6_addr = t6->ipv6_addr;
2147 inet_ntop(AF_INET6, &(a6.sin6_addr),address,INET6_ADDRSTRLEN);
2148 port = ntohs(t6->u6_port);
2150 else if (addrlen == sizeof (struct IPv4HttpAddress))
2152 address = GNUNET_malloc (INET_ADDRSTRLEN);
2154 a4.sin_addr.s_addr = t4->ipv4_addr;
2155 inet_ntop(AF_INET, &(a4.sin_addr),address,INET_ADDRSTRLEN);
2156 port = ntohs(t4->u_port);
2160 /* invalid address */
2161 GNUNET_break_op (0);
2162 asc (asc_cls, NULL);
2165 res = GNUNET_asprintf(&ret,"%s://%s:%u/", PROTOCOL_PREFIX, address,port);
2166 GNUNET_free (address);
2167 GNUNET_assert(res != 0);
2169 GNUNET_free_non_null (ret);
2175 * Another peer has suggested an address for this
2176 * peer and transport plugin. Check that this could be a valid
2177 * address. If so, consider adding it to the list
2180 * @param cls closure
2181 * @param addr pointer to the address
2182 * @param addrlen length of addr
2183 * @return GNUNET_OK if this is a plausible address for this peer
2187 http_plugin_address_suggested (void *cls,
2188 const void *addr, size_t addrlen)
2190 struct Plugin *plugin = cls;
2191 struct IPv4HttpAddress *v4;
2192 struct IPv6HttpAddress *v6;
2195 GNUNET_assert(cls !=NULL);
2196 if ((addrlen != sizeof (struct IPv4HttpAddress)) &&
2197 (addrlen != sizeof (struct IPv6HttpAddress)))
2199 return GNUNET_SYSERR;
2201 if (addrlen == sizeof (struct IPv4HttpAddress))
2203 v4 = (struct IPv4HttpAddress *) addr;
2204 /* Not skipping loopback
2205 if (INADDR_LOOPBACK == ntohl(v4->ipv4_addr))
2207 return GNUNET_SYSERR;
2209 port = ntohs (v4->u_port);
2210 if (port != plugin->port_inbound)
2212 return GNUNET_SYSERR;
2215 if (addrlen == sizeof (struct IPv6HttpAddress))
2217 v6 = (struct IPv6HttpAddress *) addr;
2218 if (IN6_IS_ADDR_LINKLOCAL (&v6->ipv6_addr))
2220 return GNUNET_SYSERR;
2222 port = ntohs (v6->u6_port);
2223 if (port != plugin->port_inbound)
2225 return GNUNET_SYSERR;
2234 * Function called for a quick conversion of the binary address to
2235 * a numeric address. Note that the caller must not free the
2236 * address and that the next call to this function is allowed
2237 * to override the address again.
2239 * @param cls closure
2240 * @param addr binary address
2241 * @param addrlen length of the address
2242 * @return string representing the same address
2245 http_plugin_address_to_string (void *cls,
2249 const struct IPv4HttpAddress *t4;
2250 const struct IPv6HttpAddress *t6;
2251 struct sockaddr_in a4;
2252 struct sockaddr_in6 a6;
2258 if (addrlen == sizeof (struct IPv6HttpAddress))
2260 address = GNUNET_malloc (INET6_ADDRSTRLEN);
2262 a6.sin6_addr = t6->ipv6_addr;
2263 inet_ntop(AF_INET6, &(a6.sin6_addr),address,INET6_ADDRSTRLEN);
2264 port = ntohs(t6->u6_port);
2266 else if (addrlen == sizeof (struct IPv4HttpAddress))
2268 address = GNUNET_malloc (INET_ADDRSTRLEN);
2270 a4.sin_addr.s_addr = t4->ipv4_addr;
2271 inet_ntop(AF_INET, &(a4.sin_addr),address,INET_ADDRSTRLEN);
2272 port = ntohs(t4->u_port);
2276 /* invalid address */
2279 res = GNUNET_asprintf(&ret,"%s:%u",address,port);
2280 GNUNET_free (address);
2281 GNUNET_assert(res != 0);
2286 load_certificate( const char * file )
2288 struct GNUNET_DISK_FileHandle * gn_file;
2293 if (0!=STAT(file, &fstat))
2295 text = GNUNET_malloc (fstat.st_size+1);
2296 gn_file = GNUNET_DISK_file_open(file,GNUNET_DISK_OPEN_READ, GNUNET_DISK_PERM_USER_READ);
2302 if (GNUNET_SYSERR == GNUNET_DISK_file_read(gn_file, text, fstat.st_size))
2305 GNUNET_DISK_file_close(gn_file);
2308 text[fstat.st_size] = '\0';
2309 GNUNET_DISK_file_close(gn_file);
2316 * Exit point from the plugin.
2319 libgnunet_plugin_transport_https_done (void *cls)
2321 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
2322 struct Plugin *plugin = api->cls;
2324 GNUNET_assert(cls !=NULL);
2326 if (plugin->http_server_daemon_v4 != NULL)
2328 MHD_stop_daemon (plugin->http_server_daemon_v4);
2329 plugin->http_server_daemon_v4 = NULL;
2331 if (plugin->http_server_daemon_v6 != NULL)
2333 MHD_stop_daemon (plugin->http_server_daemon_v6);
2334 plugin->http_server_daemon_v6 = NULL;
2337 if ( plugin->http_server_task_v4 != GNUNET_SCHEDULER_NO_TASK)
2339 GNUNET_SCHEDULER_cancel(plugin->env->sched, plugin->http_server_task_v4);
2340 plugin->http_server_task_v4 = GNUNET_SCHEDULER_NO_TASK;
2343 if ( plugin->http_server_task_v6 != GNUNET_SCHEDULER_NO_TASK)
2345 GNUNET_SCHEDULER_cancel(plugin->env->sched, plugin->http_server_task_v6);
2346 plugin->http_server_task_v6 = GNUNET_SCHEDULER_NO_TASK;
2350 /* free all peer information */
2351 if (plugin->peers!=NULL)
2353 GNUNET_CONTAINER_multihashmap_iterate (plugin->peers,
2354 &remove_peer_context_Iterator,
2356 GNUNET_CONTAINER_multihashmap_destroy (plugin->peers);
2358 if (plugin->multi_handle!=NULL)
2360 mret = curl_multi_cleanup(plugin->multi_handle);
2362 if ( CURLM_OK != mret)
2363 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"curl multihandle clean up failed\n");
2365 plugin->multi_handle = NULL;
2367 curl_global_cleanup();
2369 if ( plugin->http_curl_task != GNUNET_SCHEDULER_NO_TASK)
2371 GNUNET_SCHEDULER_cancel(plugin->env->sched, plugin->http_curl_task);
2372 plugin->http_curl_task = GNUNET_SCHEDULER_NO_TASK;
2375 GNUNET_free_non_null (plugin->bind4_address);
2376 GNUNET_free_non_null (plugin->bind6_address);
2377 GNUNET_free_non_null (plugin->bind_hostname);
2378 GNUNET_free_non_null (plugin->crypto_init);
2379 GNUNET_free_non_null (plugin->cert);
2380 GNUNET_free_non_null (plugin->key);
2381 GNUNET_free (plugin);
2384 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Unload http plugin complete...\n");
2391 * Entry point for the plugin.
2394 libgnunet_plugin_transport_https_init (void *cls)
2396 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
2397 struct Plugin *plugin;
2398 struct GNUNET_TRANSPORT_PluginFunctions *api;
2399 struct GNUNET_TIME_Relative gn_timeout;
2400 long long unsigned int port;
2402 char * key_file = NULL;
2403 char * cert_file = NULL;
2405 GNUNET_assert(cls !=NULL);
2407 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Starting https plugin...\n");
2410 plugin = GNUNET_malloc (sizeof (struct Plugin));
2411 plugin->stats = env->stats;
2413 plugin->peers = NULL;
2414 plugin->bind4_address = NULL;
2415 plugin->use_ipv6 = GNUNET_YES;
2416 plugin->use_ipv4 = GNUNET_YES;
2418 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
2420 api->send = &http_plugin_send;
2421 api->disconnect = &http_plugin_disconnect;
2422 api->address_pretty_printer = &http_plugin_address_pretty_printer;
2423 api->check_address = &http_plugin_address_suggested;
2424 api->address_to_string = &http_plugin_address_to_string;
2426 /* Hashing our identity to use it in URLs */
2427 GNUNET_CRYPTO_hash_to_enc ( &(plugin->env->my_identity->hashPubKey), &plugin->my_ascii_hash_ident);
2429 /* Use IPv6 yes/no */
2430 if (GNUNET_CONFIGURATION_have_value (env->cfg,
2431 "transport-https", "USE_IPv6"))
2433 plugin->use_ipv6 = GNUNET_CONFIGURATION_get_value_yesno (env->cfg,
2437 /* Use IPv4 yes/no */
2438 if (GNUNET_CONFIGURATION_have_value (env->cfg,
2439 "transport-https", "USE_IPv4"))
2441 plugin->use_ipv4 = GNUNET_CONFIGURATION_get_value_yesno (env->cfg,
2445 /* Reading port number from config file */
2447 GNUNET_CONFIGURATION_get_value_number (env->cfg,
2453 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
2455 _("Require valid port number for transport plugin `%s' in configuration!\n"),
2457 libgnunet_plugin_transport_https_done (api);
2461 /* Reading ipv4 addresse to bind to from config file */
2462 if ((plugin->use_ipv4==GNUNET_YES) && (GNUNET_CONFIGURATION_have_value (env->cfg,
2463 "transport-https", "BINDTO4")))
2465 GNUNET_break (GNUNET_OK ==
2466 GNUNET_CONFIGURATION_get_value_string (env->cfg,
2469 &plugin->bind_hostname));
2470 plugin->bind4_address = GNUNET_malloc(sizeof(struct sockaddr_in));
2471 plugin->bind4_address->sin_family = AF_INET;
2472 plugin->bind4_address->sin_port = htons (port);
2474 if (inet_pton(AF_INET,plugin->bind_hostname, &plugin->bind4_address->sin_addr)<=0)
2476 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
2478 _("Misconfigured address to bind to in configuration!\n"),
2480 GNUNET_free(plugin->bind4_address);
2481 GNUNET_free(plugin->bind_hostname);
2482 plugin->bind_hostname = NULL;
2483 plugin->bind4_address = NULL;
2487 /* Get crypto init string from config */
2488 if (GNUNET_CONFIGURATION_have_value (env->cfg,
2489 "transport-https", "CRYPTO_INIT"))
2491 GNUNET_CONFIGURATION_get_value_string (env->cfg,
2494 &plugin->crypto_init);
2498 GNUNET_asprintf(&plugin->crypto_init,"NORMAL");
2501 /* Get private key file from config */
2502 if (GNUNET_CONFIGURATION_have_value (env->cfg,
2503 "transport-https", "KEY_FILE"))
2505 GNUNET_CONFIGURATION_get_value_string (env->cfg,
2511 GNUNET_asprintf(&key_file,"https.key");
2513 /* Get private key file from config */
2514 if (GNUNET_CONFIGURATION_have_value (env->cfg,"transport-https", "CERT_FILE"))
2516 GNUNET_CONFIGURATION_get_value_string (env->cfg,
2521 if (cert_file==NULL)
2522 GNUNET_asprintf(&cert_file,"https.cert");
2524 /* Should plugin use ipv6? */
2525 if ((plugin->use_ipv6==GNUNET_YES) && (GNUNET_CONFIGURATION_have_value (env->cfg,
2526 "transport-https", "BINDTO6")))
2528 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (env->cfg,
2531 &plugin->bind_hostname))
2533 plugin->bind6_address = GNUNET_malloc(sizeof(struct sockaddr_in6));
2534 plugin->bind6_address->sin6_family = AF_INET6;
2535 plugin->bind6_address->sin6_port = htons (port);
2537 if (inet_pton(AF_INET6,plugin->bind_hostname, &plugin->bind6_address->sin6_addr)<=0)
2539 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
2541 _("Misconfigured address to bind to in configuration!\n"),
2543 GNUNET_free(plugin->bind6_address);
2544 GNUNET_free(plugin->bind_hostname);
2545 plugin->bind_hostname = NULL;
2546 plugin->bind6_address = NULL;
2551 /* read key & certificates from file */
2552 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Loading TLS certificate `%s' `%s'\n", key_file, cert_file);
2554 plugin->key = load_certificate( key_file );
2555 plugin->cert = load_certificate( cert_file );
2557 if ((plugin->key==NULL) || (plugin->cert==NULL))
2561 GNUNET_asprintf(&cmd,"gnunet-transport-certificate-creation %s %s", key_file, cert_file);
2562 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "No usable TLS certificate found, creating certificate \n");
2567 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
2569 _("Could not create a new TLS certificate, shell script `%s' failed!\n"),cmd,
2571 GNUNET_free (key_file);
2572 GNUNET_free (cert_file);
2574 libgnunet_plugin_transport_https_done(api);
2581 plugin->key = load_certificate( key_file );
2582 plugin->cert = load_certificate( cert_file );
2584 if ((plugin->key==NULL) || (plugin->cert==NULL))
2586 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
2588 _("No usable TLS certificate found and creating one failed! \n"),
2590 GNUNET_free (key_file);
2591 GNUNET_free (cert_file);
2592 libgnunet_plugin_transport_https_done(api);
2597 GNUNET_free (key_file);
2598 GNUNET_free (cert_file);
2601 GNUNET_assert((plugin->key!=NULL) && (plugin->cert!=NULL));
2602 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "TLS certificate loaded\n");
2604 GNUNET_assert ((port > 0) && (port <= 65535));
2605 plugin->port_inbound = port;
2606 gn_timeout = GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT;
2607 unsigned int timeout = (gn_timeout.value) / 1000;
2608 if ((plugin->http_server_daemon_v6 == NULL) && (plugin->use_ipv6 == GNUNET_YES) && (port != 0))
2610 struct sockaddr * tmp = (struct sockaddr *) plugin->bind6_address;
2611 plugin->http_server_daemon_v6 = MHD_start_daemon (
2615 MHD_USE_IPv6 | MHD_USE_SSL,
2618 plugin , &mdh_access_cb, plugin,
2619 MHD_OPTION_HTTPS_PRIORITIES, plugin->crypto_init,
2620 MHD_OPTION_HTTPS_MEM_KEY, plugin->key,
2621 MHD_OPTION_HTTPS_MEM_CERT, plugin->cert,
2622 MHD_OPTION_SOCK_ADDR, tmp,
2623 MHD_OPTION_CONNECTION_LIMIT, (unsigned int) 32,
2624 //MHD_OPTION_PER_IP_CONNECTION_LIMIT, (unsigned int) 6,
2625 MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) timeout,
2626 MHD_OPTION_CONNECTION_MEMORY_LIMIT, (size_t) 2 * GNUNET_SERVER_MAX_MESSAGE_SIZE,
2627 MHD_OPTION_NOTIFY_COMPLETED, &mhd_termination_cb, NULL,
2628 MHD_OPTION_EXTERNAL_LOGGER, mhd_logger, plugin->mhd_log,
2631 if ((plugin->http_server_daemon_v4 == NULL) && (plugin->use_ipv4 == GNUNET_YES) && (port != 0))
2633 plugin->http_server_daemon_v4 = MHD_start_daemon (
2637 MHD_NO_FLAG | MHD_USE_SSL,
2640 plugin , &mdh_access_cb, plugin,
2641 MHD_OPTION_HTTPS_PRIORITIES, plugin->crypto_init,
2642 MHD_OPTION_HTTPS_MEM_KEY, plugin->key,
2643 MHD_OPTION_HTTPS_MEM_CERT, plugin->cert,
2644 MHD_OPTION_SOCK_ADDR, (struct sockaddr_in *)plugin->bind4_address,
2645 MHD_OPTION_CONNECTION_LIMIT, (unsigned int) 32,
2646 //MHD_OPTION_PER_IP_CONNECTION_LIMIT, (unsigned int) 6,
2647 MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) timeout,
2648 MHD_OPTION_CONNECTION_MEMORY_LIMIT, (size_t) 2 * GNUNET_SERVER_MAX_MESSAGE_SIZE,
2649 MHD_OPTION_NOTIFY_COMPLETED, &mhd_termination_cb, NULL,
2650 MHD_OPTION_EXTERNAL_LOGGER, mhd_logger, plugin->mhd_log,
2653 if (plugin->http_server_daemon_v4 != NULL)
2654 plugin->http_server_task_v4 = http_server_daemon_prepare (plugin, plugin->http_server_daemon_v4);
2655 if (plugin->http_server_daemon_v6 != NULL)
2656 plugin->http_server_task_v6 = http_server_daemon_prepare (plugin, plugin->http_server_daemon_v6);
2659 if (plugin->http_server_task_v4 != GNUNET_SCHEDULER_NO_TASK)
2662 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Starting HTTPS Server with IPv4 bound to %s with port %u\n",(plugin->bind_hostname!=NULL) ? plugin->bind_hostname : "every address",port);
2665 else if ((plugin->http_server_task_v6 != GNUNET_SCHEDULER_NO_TASK) && (plugin->http_server_task_v4 != GNUNET_SCHEDULER_NO_TASK))
2668 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Starting HTTPS Server with IPv6 bound to %s with port %u\n",(plugin->bind_hostname!=NULL) ? plugin->bind_hostname : "every address", port);
2671 else if ((plugin->http_server_task_v6 != GNUNET_SCHEDULER_NO_TASK) && (plugin->http_server_task_v4 == GNUNET_SCHEDULER_NO_TASK))
2674 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Starting HTTPS Server with IPv4 and IPv6 bound to %s with port %u\n",(plugin->bind_hostname!=NULL) ? plugin->bind_hostname : "every address", port);
2680 if ((plugin->use_ipv6 == GNUNET_YES) && (plugin->use_ipv4 == GNUNET_YES))
2681 GNUNET_asprintf(&tmp,"with IPv4 and IPv6 enabled");
2682 if ((plugin->use_ipv6 == GNUNET_NO) && (plugin->use_ipv4 == GNUNET_YES))
2683 GNUNET_asprintf(&tmp,"with IPv4 enabled");
2684 if ((plugin->use_ipv6 == GNUNET_YES) && (plugin->use_ipv4 == GNUNET_NO))
2685 GNUNET_asprintf(&tmp,"with IPv6 enabled");
2686 if ((plugin->use_ipv6 == GNUNET_NO) && (plugin->use_ipv4 == GNUNET_NO))
2687 GNUNET_asprintf(&tmp,"with NO IP PROTOCOL enabled");
2688 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,"HTTPS Server with %s could not be started on port %u! https plugin failed!\n",tmp, port);
2690 libgnunet_plugin_transport_https_done (api);
2694 /* Initializing cURL */
2695 curl_global_init(CURL_GLOBAL_ALL);
2696 plugin->multi_handle = curl_multi_init();
2698 if ( NULL == plugin->multi_handle )
2700 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
2702 _("Could not initialize curl multi handle, failed to start http plugin!\n"),
2704 libgnunet_plugin_transport_https_done (api);
2708 plugin->peers = GNUNET_CONTAINER_multihashmap_create (10);
2709 GNUNET_OS_network_interfaces_list (&process_interfaces, plugin);
2714 /* end of plugin_transport_https.c */