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 DEBUG_CURL GNUNET_NO
46 #define DEBUG_CONNECTIONS GNUNET_NO
47 #define DEBUG_SESSION_SELECTION GNUNET_NO
49 #define INBOUND GNUNET_NO
50 #define OUTBOUND GNUNET_YES
53 * Text of the response sent back after the last bytes of a PUT
54 * request have been received (just to formally obey the HTTP
57 #define HTTP_PUT_RESPONSE "Thank you!"
60 * After how long do we expire an address that we
61 * learned from another peer if it is not reconfirmed
64 #define LEARNED_ADDRESS_EXPIRATION GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_HOURS, 6)
67 * Page returned if request invalid
69 #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>"
72 * Timeout for a http connect
74 #define HTTP_CONNECT_TIMEOUT 30
77 * Network format for IPv4 addresses.
79 struct IPv4HttpAddress
82 * IPv4 address, in network byte order.
84 uint32_t ipv4_addr GNUNET_PACKED;
87 * Port number, in network byte order.
89 uint16_t u_port GNUNET_PACKED;
95 * Network format for IPv6 addresses.
97 struct IPv6HttpAddress
102 struct in6_addr ipv6_addr GNUNET_PACKED;
105 * Port number, in network byte order.
107 uint16_t u6_port GNUNET_PACKED;
113 * Message to send using http
118 * next pointer for double linked list
120 struct HTTP_Message * next;
123 * previous pointer for double linked list
125 struct HTTP_Message * prev;
128 * buffer containing data to send
133 * amount of data already sent
143 * Continuation function to call once the transmission buffer
144 * has again space available. NULL if there is no
145 * continuation to call.
147 GNUNET_TRANSPORT_TransmitContinuation transmit_cont;
150 * Closure for transmit_cont.
152 void *transmit_cont_cls;
156 struct HTTP_PeerContext
161 struct GNUNET_PeerIdentity identity;
164 * Pointer to the global plugin struct.
166 struct Plugin *plugin;
169 * Linked list of connections with this peer
172 struct Session * head;
175 * Linked list of connections with this peer
178 struct Session * tail;
181 * id for next session
183 size_t session_id_counter;
186 * Last session used to send data
188 struct Session * last_session;
197 struct SessionHeader header;
200 * next session in linked list
202 struct Session * next;
205 * previous session in linked list
207 struct Session * prev;
210 * address of this session
225 * Message queue for outbound messages
228 struct HTTP_Message * pending_msgs_head;
231 * Message queue for outbound messages
234 struct HTTP_Message * pending_msgs_tail;
237 * partner peer this connection belongs to
239 struct HTTP_PeerContext * peercontext;
242 * message stream tokenizer for incoming data
244 struct GNUNET_SERVER_MessageStreamTokenizer *msgtok;
248 * outbound: OUTBOUND (GNUNET_YES)
249 * inbound : INBOUND (GNUNET_NO)
251 unsigned int direction;
254 * is session connected to send data?
256 unsigned int send_connected;
259 * is send connection active?
261 unsigned int send_active;
264 * connection disconnect forced (e.g. from transport)
266 unsigned int send_force_disconnect;
269 * is session connected to receive data?
271 unsigned int recv_connected;
274 * is receive connection active?
276 unsigned int recv_active;
279 * connection disconnect forced (e.g. from transport)
281 unsigned int recv_force_disconnect;
284 * id for next session
285 * NOTE: 0 is not an ID, zero is not defined. A correct ID is always > 0
290 * entity managing sending data
291 * outbound session: CURL *
292 * inbound session: mhd_connection *
294 void * send_endpoint;
297 * entity managing recieving data
298 * outbound session: CURL *
299 * inbound session: mhd_connection *
301 void * recv_endpoint;
305 * Encapsulation of all of the state of the plugin.
312 struct GNUNET_TRANSPORT_PluginEnvironment *env;
315 * Handle for reporting statistics.
317 struct GNUNET_STATISTICS_Handle *stats;
319 unsigned int port_inbound;
321 struct GNUNET_CONTAINER_MultiHashMap *peers;
324 * Daemon for listening for new IPv4 connections.
326 struct MHD_Daemon *http_server_daemon_v4;
329 * Daemon for listening for new IPv6connections.
331 struct MHD_Daemon *http_server_daemon_v6;
334 * Our primary task for http daemon handling IPv4 connections
336 GNUNET_SCHEDULER_TaskIdentifier http_server_task_v4;
339 * Our primary task for http daemon handling IPv6 connections
341 GNUNET_SCHEDULER_TaskIdentifier http_server_task_v6;
344 * The task sending data
346 GNUNET_SCHEDULER_TaskIdentifier http_curl_task;
351 CURLM * multi_handle;
354 * Our ASCII encoded, hashed peer identity
355 * This string is used to distinguish between connections and is added to the urls
357 struct GNUNET_CRYPTO_HashAsciiEncoded my_ascii_hash_ident;
359 struct sockaddr_in * bind4_address;
360 struct sockaddr_in6 * bind6_address;
361 char * bind_hostname;
365 /* The certificate MHD uses as an \0 terminated string */
368 /* The private key MHD uses as an \0 terminated string */
374 * Function called for a quick conversion of the binary address to
375 * a numeric address. Note that the caller must not free the
376 * address and that the next call to this function is allowed
377 * to override the address again.
380 * @param addr binary address
381 * @param addrlen length of the address
382 * @return string representing the same address
385 http_plugin_address_to_string (void *cls,
391 * Call MHD to process pending ipv4 requests and then go back
392 * and schedule the next run.
394 static void http_server_daemon_v4_run (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
396 * Call MHD to process pending ipv6 requests and then go back
397 * and schedule the next run.
399 static void http_server_daemon_v6_run (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
402 * Function setting up curl handle and selecting message to send
404 * @param ses session to send data to
405 * @param con connection
406 * @return bytes sent to peer
408 static ssize_t send_check_connections (void *cls, struct Session *ps);
411 * Function setting up file descriptors and scheduling task to run
413 * @param ses session to send data to
416 static int curl_schedule(void *cls );
420 static char * create_url(void * cls, const void * addr, size_t addrlen, size_t id)
422 struct Plugin *plugin = cls;
425 GNUNET_assert ((addr!=NULL) && (addrlen != 0));
426 GNUNET_asprintf(&url,
428 http_plugin_address_to_string(NULL, addr, addrlen),
429 (char *) (&plugin->my_ascii_hash_ident),id);
435 * Removes a message from the linked list of messages
436 * @param con connection to remove message from
437 * @param msg message to remove
438 * @return GNUNET_SYSERR if msg not found, GNUNET_OK on success
440 static int remove_http_message (struct Session * ps, struct HTTP_Message * msg)
442 GNUNET_CONTAINER_DLL_remove(ps->pending_msgs_head,ps->pending_msgs_tail,msg);
447 int remove_peer_context_Iterator (void *cls, const GNUNET_HashCode *key, void *value);
450 * Removes a session from the linked list of sessions
451 * @param pc peer context
453 * @param call_msg_cont GNUNET_YES to call pending message continuations, otherwise no
454 * @param call_msg_cont_result, result to call message continuations with
455 * @return GNUNET_SYSERR if msg not found, GNUNET_OK on success
457 static int remove_session (struct HTTP_PeerContext * pc, struct Session * ps, int call_msg_cont, int call_msg_cont_result)
459 struct HTTP_Message * msg;
460 struct Plugin * plugin = ps->peercontext->plugin;
462 #if DEBUG_CONNECTIONS
463 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);
465 plugin->env->session_end(plugin, &pc->identity, ps);
467 GNUNET_free_non_null (ps->addr);
468 GNUNET_SERVER_mst_destroy (ps->msgtok);
469 GNUNET_free(ps->url);
471 if (ps->direction==INBOUND)
473 if (ps->recv_endpoint != NULL)
475 curl_easy_cleanup(ps->recv_endpoint);
476 ps->recv_endpoint = NULL;
478 if (ps->send_endpoint != NULL)
480 curl_easy_cleanup(ps->send_endpoint);
481 ps->send_endpoint = NULL;
485 msg = ps->pending_msgs_head;
488 if ((call_msg_cont == GNUNET_YES) && (msg->transmit_cont!=NULL))
490 msg->transmit_cont (msg->transmit_cont_cls,&pc->identity,call_msg_cont_result);
493 GNUNET_CONTAINER_DLL_remove(ps->pending_msgs_head,ps->pending_msgs_head,msg);
494 msg = ps->pending_msgs_head;
497 GNUNET_CONTAINER_DLL_remove(pc->head,pc->tail,ps);
501 /* no sessions left remove peer */
505 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"No sessions left for peer `%s', removing context\n",GNUNET_i2s(&pc->identity));
507 remove_peer_context_Iterator(plugin, &pc->identity.hashPubKey, pc);
513 int remove_peer_context_Iterator (void *cls, const GNUNET_HashCode *key, void *value)
515 struct Plugin *plugin = cls;
516 struct HTTP_PeerContext * pc = value;
517 struct Session * ps = pc->head;
518 struct Session * tmp = NULL;
519 struct HTTP_Message * msg = NULL;
520 struct HTTP_Message * msg_tmp = NULL;
522 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Freeing context for peer `%s'\n",GNUNET_i2s(&pc->identity));
524 GNUNET_CONTAINER_multihashmap_remove (plugin->peers, &pc->identity.hashPubKey, pc);
527 plugin->env->session_end(plugin, &pc->identity, ps);
530 GNUNET_free_non_null (ps->addr);
531 GNUNET_free(ps->url);
532 if (ps->msgtok != NULL)
533 GNUNET_SERVER_mst_destroy (ps->msgtok);
535 msg = ps->pending_msgs_head;
542 if (ps->direction==OUTBOUND)
544 if (ps->send_endpoint!=NULL)
545 curl_easy_cleanup(ps->send_endpoint);
546 if (ps->recv_endpoint!=NULL)
547 curl_easy_cleanup(ps->recv_endpoint);
554 GNUNET_STATISTICS_update (plugin->env->stats,
555 gettext_noop ("# HTTP peers active"),
562 * Add the IP of our network interface to the list of
563 * our external IP addresses.
565 * @param cls the 'struct Plugin*'
566 * @param name name of the interface
567 * @param isDefault do we think this may be our default interface
568 * @param addr address of the interface
569 * @param addrlen number of bytes in addr
570 * @return GNUNET_OK to continue iterating
573 process_interfaces (void *cls,
576 const struct sockaddr *addr, socklen_t addrlen)
578 struct Plugin *plugin = cls;
579 struct IPv4HttpAddress * t4;
580 struct IPv6HttpAddress * t6;
584 GNUNET_assert(cls !=NULL);
585 af = addr->sa_family;
586 if ((af == AF_INET) && (plugin->use_ipv4 == GNUNET_YES) && (plugin->bind6_address == NULL))
588 struct in_addr bnd_cmp = ((struct sockaddr_in *) addr)->sin_addr;
589 t4 = GNUNET_malloc(sizeof(struct IPv4HttpAddress));
590 /* Not skipping loopback addresses
591 if (INADDR_LOOPBACK == ntohl(((struct sockaddr_in *) addr)->sin_addr.s_addr))
597 t4->ipv4_addr = ((struct sockaddr_in *) addr)->sin_addr.s_addr;
598 t4->u_port = htons (plugin->port_inbound);
599 if (plugin->bind4_address != NULL)
601 if (0 == memcmp(&plugin->bind4_address->sin_addr, &bnd_cmp, sizeof (struct in_addr)))
603 plugin->env->notify_address(plugin->env->cls,"https",t4, sizeof (struct IPv4HttpAddress), GNUNET_TIME_UNIT_FOREVER_REL);
608 plugin->env->notify_address(plugin->env->cls,"https",t4, sizeof (struct IPv4HttpAddress), GNUNET_TIME_UNIT_FOREVER_REL);
612 else if ((af == AF_INET6) && (plugin->use_ipv6 == GNUNET_YES) && (plugin->bind4_address == NULL))
614 struct in6_addr bnd_cmp6 = ((struct sockaddr_in6 *) addr)->sin6_addr;
615 t6 = GNUNET_malloc(sizeof(struct IPv6HttpAddress));
616 if (IN6_IS_ADDR_LINKLOCAL (&((struct sockaddr_in6 *) addr)->sin6_addr))
621 if (plugin->bind6_address != NULL)
623 if (0 == memcmp(&plugin->bind6_address->sin6_addr, &bnd_cmp6, sizeof (struct in6_addr)))
625 memcpy (&t6->ipv6_addr,
626 &((struct sockaddr_in6 *) addr)->sin6_addr,
627 sizeof (struct in6_addr));
628 t6->u6_port = htons (plugin->port_inbound);
629 plugin->env->notify_address(plugin->env->cls,"https",t6,sizeof (struct IPv6HttpAddress) , GNUNET_TIME_UNIT_FOREVER_REL);
634 memcpy (&t6->ipv6_addr,
635 &((struct sockaddr_in6 *) addr)->sin6_addr,
636 sizeof (struct in6_addr));
637 t6->u6_port = htons (plugin->port_inbound);
638 plugin->env->notify_address(plugin->env->cls,"https",t6,sizeof (struct IPv6HttpAddress) , GNUNET_TIME_UNIT_FOREVER_REL);
647 * Callback called by MHD when a connection is terminated
649 static void mhd_termination_cb (void *cls, struct MHD_Connection * connection, void **httpSessionCache)
651 struct Session * ps = *httpSessionCache;
654 struct HTTP_PeerContext * pc = ps->peercontext;
656 if (connection==ps->recv_endpoint)
658 #if DEBUG_CONNECTIONS
659 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: inbound connection from peer `%s' was terminated\n", ps, GNUNET_i2s(&pc->identity));
661 ps->recv_active = GNUNET_NO;
662 ps->recv_connected = GNUNET_NO;
663 ps->recv_endpoint = NULL;
665 if (connection==ps->send_endpoint)
668 ps->send_active = GNUNET_NO;
669 ps->send_connected = GNUNET_NO;
670 ps->send_endpoint = NULL;
671 #if DEBUG_CONNECTIONS
672 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: outbound connection from peer `%s' was terminated\n", ps, GNUNET_i2s(&pc->identity));
676 /* if both connections disconnected, remove session */
677 if ((ps->send_connected == GNUNET_NO) && (ps->recv_connected == GNUNET_NO))
679 GNUNET_STATISTICS_update (pc->plugin->env->stats,
680 gettext_noop ("# HTTPS inbound sessions for peers active"),
683 remove_session(pc,ps,GNUNET_YES,GNUNET_SYSERR);
687 static void mhd_write_mst_cb (void *cls,
689 const struct GNUNET_MessageHeader *message)
692 struct Session *ps = cls;
693 struct HTTP_PeerContext *pc = ps->peercontext;
694 GNUNET_assert(ps != NULL);
695 GNUNET_assert(pc != NULL);
697 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
698 "Connection %X: Forwarding message to transport service, type %u and size %u from `%s' (`%s')\n",
700 ntohs(message->type),
701 ntohs(message->size),
702 GNUNET_i2s(&(ps->peercontext)->identity),http_plugin_address_to_string(NULL,ps->addr,ps->addrlen));
704 pc->plugin->env->receive (ps->peercontext->plugin->env->cls,
712 * Check if ip is allowed to connect.
715 mhd_accept_cb (void *cls,
716 const struct sockaddr *addr, socklen_t addr_len)
719 struct Plugin *plugin = cls;
721 /* Every connection is accepted, nothing more to do here */
725 int mhd_send_callback (void *cls, uint64_t pos, char *buf, int max)
729 struct Session * ps = cls;
730 struct HTTP_PeerContext * pc;
731 struct HTTP_Message * msg;
733 GNUNET_assert (ps!=NULL);
734 pc = ps->peercontext;
735 msg = ps->pending_msgs_tail;
736 if (ps->send_force_disconnect==GNUNET_YES)
738 #if DEBUG_CONNECTIONS
739 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: outbound forced to disconnect\n",ps);
746 if ((msg->size-msg->pos) <= max)
748 memcpy(buf,&msg->buf[msg->pos],(msg->size-msg->pos));
749 bytes_read = msg->size-msg->pos;
750 msg->pos+=(msg->size-msg->pos);
754 memcpy(buf,&msg->buf[msg->pos],max);
759 if (msg->pos==msg->size)
761 if (NULL!=msg->transmit_cont)
762 msg->transmit_cont (msg->transmit_cont_cls,&pc->identity,GNUNET_OK);
763 remove_http_message(ps,msg);
770 * Process GET or PUT request received via MHD. For
771 * GET, queue response that will send back our pending
772 * messages. For PUT, process incoming data and send
773 * to GNUnet core. In either case, check if a session
774 * already exists and create a new one if not.
777 mdh_access_cb (void *cls,
778 struct MHD_Connection *mhd_connection,
782 const char *upload_data,
783 size_t * upload_data_size, void **httpSessionCache)
785 struct Plugin *plugin = cls;
786 struct MHD_Response *response;
787 const union MHD_ConnectionInfo * conn_info;
789 struct sockaddr_in *addrin;
790 struct sockaddr_in6 *addrin6;
792 char address[INET6_ADDRSTRLEN+14];
793 struct GNUNET_PeerIdentity pi_in;
796 struct IPv4HttpAddress ipv4addr;
797 struct IPv6HttpAddress ipv6addr;
799 struct HTTP_PeerContext *pc;
800 struct Session *ps = NULL;
801 struct Session *ps_tmp = NULL;
804 int send_error_to_client;
808 GNUNET_assert(cls !=NULL);
809 send_error_to_client = GNUNET_NO;
811 if (NULL == *httpSessionCache)
813 /* check url for peer identity , if invalid send HTTP 404*/
814 size_t len = strlen(&url[1]);
815 char * peer = GNUNET_malloc(104+1);
817 if ((len>104) && (url[104]==';'))
819 char * id = GNUNET_malloc((len-104)+1);
820 strcpy(id,&url[105]);
821 memcpy(peer,&url[1],103);
823 id_num = strtoul ( id, NULL , 10);
826 res = GNUNET_CRYPTO_hash_from_string (peer, &(pi_in.hashPubKey));
828 if ( GNUNET_SYSERR == res )
830 response = MHD_create_response_from_data (strlen (HTTP_ERROR_RESPONSE),HTTP_ERROR_RESPONSE, MHD_NO, MHD_NO);
831 res = MHD_queue_response (mhd_connection, MHD_HTTP_NOT_FOUND, response);
832 MHD_destroy_response (response);
833 #if DEBUG_CONNECTIONS
835 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Peer has no valid ident, sent HTTP 1.1/404\n");
837 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Peer has no valid ident, could not send error\n");
844 ps = *httpSessionCache;
845 pc = ps->peercontext;
848 if (NULL == *httpSessionCache)
850 /* get peer context */
851 pc = GNUNET_CONTAINER_multihashmap_get (plugin->peers, &pi_in.hashPubKey);
855 pc = GNUNET_malloc(sizeof (struct HTTP_PeerContext));
857 pc->session_id_counter=1;
858 pc->last_session = NULL;
859 memcpy(&pc->identity, &pi_in, sizeof(struct GNUNET_PeerIdentity));
860 GNUNET_CONTAINER_multihashmap_put(plugin->peers, &pc->identity.hashPubKey, pc, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
861 GNUNET_STATISTICS_update (plugin->env->stats,
862 gettext_noop ("# HTTP peers active"),
867 conn_info = MHD_get_connection_info(mhd_connection, MHD_CONNECTION_INFO_CLIENT_ADDRESS );
868 /* Incoming IPv4 connection */
869 if ( AF_INET == conn_info->client_addr->sin_family)
871 addrin = conn_info->client_addr;
872 inet_ntop(addrin->sin_family, &(addrin->sin_addr),address,INET_ADDRSTRLEN);
873 memcpy(&ipv4addr.ipv4_addr,&(addrin->sin_addr),sizeof(struct in_addr));
874 ipv4addr.u_port = addrin->sin_port;
876 addr_len = sizeof(struct IPv4HttpAddress);
878 /* Incoming IPv6 connection */
879 if ( AF_INET6 == conn_info->client_addr->sin_family)
881 addrin6 = (struct sockaddr_in6 *) conn_info->client_addr;
882 inet_ntop(addrin6->sin6_family, &(addrin6->sin6_addr),address,INET6_ADDRSTRLEN);
883 memcpy(&ipv6addr.ipv6_addr,&(addrin6->sin6_addr),sizeof(struct in6_addr));
884 ipv6addr.u6_port = addrin6->sin6_port;
886 addr_len = sizeof(struct IPv6HttpAddress);
890 /* only inbound sessions here */
895 if ((ps_tmp->direction==INBOUND) && (ps_tmp->session_id == id_num) && (id_num!=0))
897 if ((ps_tmp->recv_force_disconnect!=GNUNET_YES) && (ps_tmp->send_force_disconnect!=GNUNET_YES))
906 ps = GNUNET_malloc(sizeof (struct Session));
907 ps->addr = GNUNET_malloc(addr_len);
908 memcpy(ps->addr,addr,addr_len);
909 ps->addrlen = addr_len;
910 ps->direction=INBOUND;
911 ps->pending_msgs_head = NULL;
912 ps->pending_msgs_tail = NULL;
913 ps->send_connected=GNUNET_NO;
914 ps->send_active=GNUNET_NO;
915 ps->recv_connected=GNUNET_NO;
916 ps->recv_active=GNUNET_NO;
918 ps->session_id =id_num;
919 ps->url = create_url (plugin, ps->addr, ps->addrlen, ps->session_id);
920 GNUNET_CONTAINER_DLL_insert(pc->head,pc->tail,ps);
921 GNUNET_STATISTICS_update (plugin->env->stats,
922 gettext_noop ("# HTTPS inbound sessions for peers active"),
927 *httpSessionCache = ps;
928 if (ps->msgtok==NULL)
929 ps->msgtok = GNUNET_SERVER_mst_create (&mhd_write_mst_cb, ps);
931 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: HTTPS Daemon has new an incoming `%s' request from peer `%s' (`%s')\n",
934 GNUNET_i2s(&pc->identity),
935 http_plugin_address_to_string(NULL, ps->addr, ps->addrlen));
939 /* Is it a PUT or a GET request */
940 if (0 == strcmp (MHD_HTTP_METHOD_PUT, method))
942 if (ps->recv_force_disconnect == GNUNET_YES)
944 #if DEBUG_CONNECTIONS
945 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: inbound connection was forced to disconnect\n",ps);
947 ps->recv_active = GNUNET_NO;
950 if ((*upload_data_size == 0) && (ps->recv_active==GNUNET_NO))
952 ps->recv_endpoint = mhd_connection;
953 ps->recv_connected = GNUNET_YES;
954 ps->recv_active = GNUNET_YES;
955 ps->recv_force_disconnect = GNUNET_NO;
956 #if DEBUG_CONNECTIONS
957 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: inbound PUT connection connected\n",ps);
962 /* Transmission of all data complete */
963 if ((*upload_data_size == 0) && (ps->recv_active == GNUNET_YES))
965 response = MHD_create_response_from_data (strlen (HTTP_PUT_RESPONSE),HTTP_PUT_RESPONSE, MHD_NO, MHD_NO);
966 res = MHD_queue_response (mhd_connection, MHD_HTTP_OK, response);
967 #if DEBUG_CONNECTIONS
968 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: Sent HTTP/1.1: 200 OK as PUT Response\n",ps);
970 MHD_destroy_response (response);
971 ps->recv_active=GNUNET_NO;
976 if ((*upload_data_size > 0) && (ps->recv_active == GNUNET_YES))
978 res = GNUNET_SERVER_mst_receive(ps->msgtok, ps, upload_data,*upload_data_size, GNUNET_NO, GNUNET_NO);
979 (*upload_data_size) = 0;
985 if ( 0 == strcmp (MHD_HTTP_METHOD_GET, method) )
987 if (ps->send_force_disconnect == GNUNET_YES)
989 #if DEBUG_CONNECTIONS
990 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: outbound connection was forced to disconnect\n",ps);
992 ps->send_active = GNUNET_NO;
995 ps->send_connected = GNUNET_YES;
996 ps->send_active = GNUNET_YES;
997 ps->send_endpoint = mhd_connection;
998 ps->send_force_disconnect = GNUNET_NO;
999 #if DEBUG_CONNECTIONS
1000 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: inbound GET connection connected\n",ps);
1002 response = MHD_create_response_from_callback(-1,32 * 1024, &mhd_send_callback, ps, NULL);
1003 res = MHD_queue_response (mhd_connection, MHD_HTTP_OK, response);
1004 MHD_destroy_response (response);
1011 * Function that queries MHD's select sets and
1012 * starts the task waiting for them.
1014 static GNUNET_SCHEDULER_TaskIdentifier
1015 http_server_daemon_prepare (void * cls, struct MHD_Daemon *daemon_handle)
1017 struct Plugin *plugin = cls;
1018 GNUNET_SCHEDULER_TaskIdentifier ret;
1022 struct GNUNET_NETWORK_FDSet *wrs;
1023 struct GNUNET_NETWORK_FDSet *wws;
1024 struct GNUNET_NETWORK_FDSet *wes;
1026 unsigned long long timeout;
1028 struct GNUNET_TIME_Relative tv;
1030 GNUNET_assert(cls !=NULL);
1031 ret = GNUNET_SCHEDULER_NO_TASK;
1035 wrs = GNUNET_NETWORK_fdset_create ();
1036 wes = GNUNET_NETWORK_fdset_create ();
1037 wws = GNUNET_NETWORK_fdset_create ();
1039 GNUNET_assert (MHD_YES ==
1040 MHD_get_fdset (daemon_handle,
1045 haveto = MHD_get_timeout (daemon_handle, &timeout);
1046 if (haveto == MHD_YES)
1047 tv.value = (uint64_t) timeout;
1049 tv = GNUNET_TIME_UNIT_FOREVER_REL;
1050 GNUNET_NETWORK_fdset_copy_native (wrs, &rs, max);
1051 GNUNET_NETWORK_fdset_copy_native (wws, &ws, max);
1052 GNUNET_NETWORK_fdset_copy_native (wes, &es, max);
1053 if (daemon_handle == plugin->http_server_daemon_v4)
1055 if (plugin->http_server_task_v4 != GNUNET_SCHEDULER_NO_TASK)
1057 GNUNET_SCHEDULER_cancel(plugin->env->sched, plugin->http_server_task_v4);
1058 plugin->http_server_daemon_v4 = GNUNET_SCHEDULER_NO_TASK;
1061 ret = GNUNET_SCHEDULER_add_select (plugin->env->sched,
1062 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1063 GNUNET_SCHEDULER_NO_TASK,
1067 &http_server_daemon_v4_run,
1070 if (daemon_handle == plugin->http_server_daemon_v6)
1072 if (plugin->http_server_task_v6 != GNUNET_SCHEDULER_NO_TASK)
1074 GNUNET_SCHEDULER_cancel(plugin->env->sched, plugin->http_server_task_v6);
1075 plugin->http_server_task_v6 = GNUNET_SCHEDULER_NO_TASK;
1078 ret = GNUNET_SCHEDULER_add_select (plugin->env->sched,
1079 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1080 GNUNET_SCHEDULER_NO_TASK,
1084 &http_server_daemon_v6_run,
1087 GNUNET_NETWORK_fdset_destroy (wrs);
1088 GNUNET_NETWORK_fdset_destroy (wws);
1089 GNUNET_NETWORK_fdset_destroy (wes);
1094 * Call MHD to process pending requests and then go back
1095 * and schedule the next run.
1097 static void http_server_daemon_v4_run (void *cls,
1098 const struct GNUNET_SCHEDULER_TaskContext *tc)
1100 struct Plugin *plugin = cls;
1102 GNUNET_assert(cls !=NULL);
1103 plugin->http_server_task_v4 = GNUNET_SCHEDULER_NO_TASK;
1105 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1108 GNUNET_assert (MHD_YES == MHD_run (plugin->http_server_daemon_v4));
1109 plugin->http_server_task_v4 = http_server_daemon_prepare (plugin, plugin->http_server_daemon_v4);
1115 * Call MHD to process pending requests and then go back
1116 * and schedule the next run.
1118 static void http_server_daemon_v6_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_v6 = GNUNET_SCHEDULER_NO_TASK;
1126 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1129 GNUNET_assert (MHD_YES == MHD_run (plugin->http_server_daemon_v6));
1130 plugin->http_server_task_v6 = http_server_daemon_prepare (plugin, plugin->http_server_daemon_v6);
1134 static size_t curl_get_header_cb( void *ptr, size_t size, size_t nmemb, void *stream)
1136 struct Session * ps = stream;
1138 long http_result = 0;
1140 /* Getting last http result code */
1141 if (ps->recv_connected==GNUNET_NO)
1143 GNUNET_assert(NULL!=ps);
1144 res = curl_easy_getinfo(ps->recv_endpoint, CURLINFO_RESPONSE_CODE, &http_result);
1145 if (CURLE_OK == res)
1147 if (http_result == 200)
1149 ps->recv_connected = GNUNET_YES;
1150 ps->recv_active = GNUNET_YES;
1151 #if DEBUG_CONNECTIONS
1152 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: connected to recieve data\n",ps);
1154 // Calling send_check_connections again since receive is established
1155 send_check_connections (ps->peercontext->plugin, ps);
1162 size_t len = size * nmemb;
1165 if ((size * nmemb) < SIZE_MAX)
1166 tmp = GNUNET_malloc (len+1);
1168 if ((tmp != NULL) && (len > 0))
1170 memcpy(tmp,ptr,len);
1173 if (tmp[len-2] == 13)
1177 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: Header: %s\n",ps,tmp);
1184 return size * nmemb;
1187 static size_t curl_put_header_cb( void *ptr, size_t size, size_t nmemb, void *stream)
1189 struct Session * ps = stream;
1192 size_t len = size * nmemb;
1193 long http_result = 0;
1196 /* Getting last http result code */
1197 GNUNET_assert(NULL!=ps);
1198 res = curl_easy_getinfo(ps->send_endpoint, CURLINFO_RESPONSE_CODE, &http_result);
1199 if (CURLE_OK == res)
1201 if ((http_result == 100) && (ps->send_connected==GNUNET_NO))
1203 ps->send_connected = GNUNET_YES;
1204 ps->send_active = GNUNET_YES;
1205 #if DEBUG_CONNECTIONS
1206 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: connected to send data\n",ps);
1209 if ((http_result == 200) && (ps->send_connected==GNUNET_YES))
1211 ps->send_connected = GNUNET_NO;
1212 ps->send_active = GNUNET_NO;
1213 #if DEBUG_CONNECTIONS
1214 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: sending disconnected\n",ps);
1220 if ((size * nmemb) < SIZE_MAX)
1221 tmp = GNUNET_malloc (len+1);
1223 if ((tmp != NULL) && (len > 0))
1225 memcpy(tmp,ptr,len);
1228 if (tmp[len-2] == 13)
1235 return size * nmemb;
1239 * Callback method used with libcurl
1240 * Method is called when libcurl needs to read data during sending
1241 * @param stream pointer where to write data
1242 * @param size size of an individual element
1243 * @param nmemb count of elements that can be written to the buffer
1244 * @param ptr source pointer, passed to the libcurl handle
1245 * @return bytes written to stream
1247 static size_t curl_send_cb(void *stream, size_t size, size_t nmemb, void *ptr)
1249 struct Session * ps = ptr;
1250 struct HTTP_Message * msg = ps->pending_msgs_tail;
1254 if (ps->send_active == GNUNET_NO)
1255 return CURL_READFUNC_PAUSE;
1258 if ((ps->pending_msgs_tail == NULL) && (ps->send_active == GNUNET_YES))
1260 #if DEBUG_CONNECTIONS
1261 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: No Message to send, pausing connection\n",ps);
1263 ps->send_active = GNUNET_NO;
1264 return CURL_READFUNC_PAUSE;
1267 msg = ps->pending_msgs_tail;
1269 if (msg->pos < msg->size)
1271 /* data fit in buffer */
1272 if ((msg->size - msg->pos) <= (size * nmemb))
1274 len = (msg->size - msg->pos);
1275 memcpy(stream, &msg->buf[msg->pos], len);
1282 memcpy(stream, &msg->buf[msg->pos], len);
1287 /* no data to send */
1293 if ( msg->pos == msg->size)
1295 #if DEBUG_CONNECTIONS
1296 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: Message with %u bytes sent, removing message from queue \n",ps, msg->pos);
1298 /* Calling transmit continuation */
1299 if (( NULL != ps->pending_msgs_tail) && (NULL != ps->pending_msgs_tail->transmit_cont))
1300 msg->transmit_cont (ps->pending_msgs_tail->transmit_cont_cls,&(ps->peercontext)->identity,GNUNET_OK);
1301 remove_http_message(ps, msg);
1306 static void curl_receive_mst_cb (void *cls,
1308 const struct GNUNET_MessageHeader *message)
1310 struct Session *ps = cls;
1311 struct HTTP_PeerContext *pc = ps->peercontext;
1312 GNUNET_assert(ps != NULL);
1313 GNUNET_assert(pc != NULL);
1315 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1316 "Connection %X: Forwarding message to transport service, type %u and size %u from `%s' (`%s')\n",
1318 ntohs(message->type),
1319 ntohs(message->size),
1320 GNUNET_i2s(&(pc->identity)),http_plugin_address_to_string(NULL,ps->addr,ps->addrlen));
1322 pc->plugin->env->receive (pc->plugin->env->cls,
1331 * Callback method used with libcurl
1332 * Method is called when libcurl needs to write data during sending
1333 * @param stream pointer where to write data
1334 * @param size size of an individual element
1335 * @param nmemb count of elements that can be written to the buffer
1336 * @param ptr destination pointer, passed to the libcurl handle
1337 * @return bytes read from stream
1339 static size_t curl_receive_cb( void *stream, size_t size, size_t nmemb, void *ptr)
1341 struct Session * ps = ptr;
1342 #if DEBUG_CONNECTIONS
1343 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: %u bytes received\n",ps, size*nmemb);
1345 GNUNET_SERVER_mst_receive(ps->msgtok, ps, stream, size*nmemb, GNUNET_NO, GNUNET_NO);
1346 return (size * nmemb);
1350 static void curl_perform (void *cls,
1351 const struct GNUNET_SCHEDULER_TaskContext *tc)
1353 struct Plugin *plugin = cls;
1354 static unsigned int handles_last_run;
1356 struct CURLMsg *msg;
1358 struct Session *ps = NULL;
1359 struct HTTP_PeerContext *pc = NULL;
1360 struct HTTP_Message * cur_msg = NULL;
1364 GNUNET_assert(cls !=NULL);
1366 plugin->http_curl_task = GNUNET_SCHEDULER_NO_TASK;
1367 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1373 mret = curl_multi_perform (plugin->multi_handle, &running);
1374 if ((running < handles_last_run) && (running>0))
1379 msg = curl_multi_info_read (plugin->multi_handle, &running);
1382 /* get session for affected curl handle */
1383 GNUNET_assert ( msg->easy_handle != NULL );
1384 curl_easy_getinfo(msg->easy_handle, CURLINFO_PRIVATE, &tmp);
1385 ps = (struct Session *) tmp;
1386 GNUNET_assert ( ps != NULL );
1387 pc = ps->peercontext;
1388 GNUNET_assert ( pc != NULL );
1393 if ( (msg->data.result != CURLE_OK) &&
1394 (msg->data.result != CURLE_GOT_NOTHING) )
1396 /* sending msg failed*/
1397 if (msg->easy_handle == ps->send_endpoint)
1399 #if DEBUG_CONNECTIONS
1400 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1401 _("Connection %X: HTTPS PUT to peer `%s' (`%s') failed: `%s' `%s'\n"),
1403 GNUNET_i2s(&pc->identity),
1404 http_plugin_address_to_string(NULL, ps->addr, ps->addrlen),
1405 "curl_multi_perform",
1406 curl_easy_strerror (msg->data.result));
1408 ps->send_connected = GNUNET_NO;
1409 ps->send_active = GNUNET_NO;
1410 curl_multi_remove_handle(plugin->multi_handle,ps->send_endpoint);
1411 //curl_easy_cleanup(ps->send_endpoint);
1412 //ps->send_endpoint=NULL;
1413 cur_msg = ps->pending_msgs_tail;
1414 if (( NULL != cur_msg) && ( NULL != cur_msg->transmit_cont))
1415 cur_msg->transmit_cont (cur_msg->transmit_cont_cls,&pc->identity,GNUNET_SYSERR);
1417 /* GET connection failed */
1418 if (msg->easy_handle == ps->recv_endpoint)
1420 #if DEBUG_CONNECTIONS
1421 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1422 _("Connection %X: HTTPS GET to peer `%s' (`%s') failed: `%s' `%s'\n"),
1424 GNUNET_i2s(&pc->identity),
1425 http_plugin_address_to_string(NULL, ps->addr, ps->addrlen),
1426 "curl_multi_perform",
1427 curl_easy_strerror (msg->data.result));
1429 ps->recv_connected = GNUNET_NO;
1430 ps->recv_active = GNUNET_NO;
1431 curl_multi_remove_handle(plugin->multi_handle,ps->recv_endpoint);
1432 //curl_easy_cleanup(ps->recv_endpoint);
1433 //ps->recv_endpoint=NULL;
1438 if (msg->easy_handle == ps->send_endpoint)
1440 GNUNET_assert (CURLE_OK == curl_easy_getinfo(msg->easy_handle, CURLINFO_RESPONSE_CODE, &http_result));
1441 #if DEBUG_CONNECTIONS
1442 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1443 "Connection %X: HTTPS PUT connection to peer `%s' (`%s') was closed with HTTP code %u\n",
1445 GNUNET_i2s(&pc->identity),
1446 http_plugin_address_to_string(NULL, ps->addr, ps->addrlen),
1449 /* Calling transmit continuation */
1450 cur_msg = ps->pending_msgs_tail;
1451 if (( NULL != cur_msg) && (NULL != cur_msg->transmit_cont))
1453 /* HTTP 1xx : Last message before here was informational */
1454 if ((http_result >=100) && (http_result < 200))
1455 cur_msg->transmit_cont (cur_msg->transmit_cont_cls,&pc->identity,GNUNET_OK);
1456 /* HTTP 2xx: successful operations */
1457 if ((http_result >=200) && (http_result < 300))
1458 cur_msg->transmit_cont (cur_msg->transmit_cont_cls,&pc->identity,GNUNET_OK);
1459 /* HTTP 3xx..5xx: error */
1460 if ((http_result >=300) && (http_result < 600))
1461 cur_msg->transmit_cont (cur_msg->transmit_cont_cls,&pc->identity,GNUNET_SYSERR);
1463 ps->send_connected = GNUNET_NO;
1464 ps->send_active = GNUNET_NO;
1465 curl_multi_remove_handle(plugin->multi_handle,ps->send_endpoint);
1466 //curl_easy_cleanup(ps->send_endpoint);
1467 //ps->send_endpoint =NULL;
1469 if (msg->easy_handle == ps->recv_endpoint)
1471 #if DEBUG_CONNECTIONS
1472 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1473 "Connection %X: HTTP GET connection to peer `%s' (`%s') was closed with HTTP code %u\n",
1475 GNUNET_i2s(&pc->identity),
1476 http_plugin_address_to_string(NULL, ps->addr, ps->addrlen),
1479 ps->recv_connected = GNUNET_NO;
1480 ps->recv_active = GNUNET_NO;
1481 curl_multi_remove_handle(plugin->multi_handle,ps->recv_endpoint);
1482 //curl_easy_cleanup(ps->recv_endpoint);
1483 //ps->recv_endpoint=NULL;
1486 if ((ps->recv_connected == GNUNET_NO) && (ps->send_connected == GNUNET_NO))
1487 remove_session (pc, ps, GNUNET_YES, GNUNET_SYSERR);
1494 while ( (running > 0) );
1496 handles_last_run = running;
1498 while (mret == CURLM_CALL_MULTI_PERFORM);
1499 curl_schedule(plugin);
1504 * Function setting up file descriptors and scheduling task to run
1505 * @param ses session to send data to
1506 * @return GNUNET_SYSERR for hard failure, GNUNET_OK for ok
1509 http_plugin_disconnect (void *cls,
1510 const struct GNUNET_PeerIdentity *target)
1514 struct Plugin *plugin = cls;
1515 struct HTTP_PeerContext *pc = NULL;
1516 struct Session *ps = NULL;
1517 //struct Session *tmp = NULL;
1519 pc = GNUNET_CONTAINER_multihashmap_get (plugin->peers, &target->hashPubKey);
1526 /* Telling transport that session is getting disconnected */
1527 plugin->env->session_end(plugin, target, ps);
1528 if (ps->direction==OUTBOUND)
1530 if (ps->send_endpoint!=NULL)
1532 //GNUNET_assert(CURLM_OK == curl_multi_remove_handle(plugin->multi_handle,ps->send_endpoint));
1533 //curl_easy_cleanup(ps->send_endpoint);
1534 //ps->send_endpoint=NULL;
1535 ps->send_force_disconnect = GNUNET_YES;
1537 if (ps->recv_endpoint!=NULL)
1539 //GNUNET_assert(CURLM_OK == curl_multi_remove_handle(plugin->multi_handle,ps->recv_endpoint));
1540 //curl_easy_cleanup(ps->recv_endpoint);
1541 //ps->recv_endpoint=NULL;
1542 ps->recv_force_disconnect = GNUNET_YES;
1546 if (ps->direction==INBOUND)
1548 ps->recv_force_disconnect = GNUNET_YES;
1549 ps->send_force_disconnect = GNUNET_YES;
1552 while (ps->pending_msgs_head!=NULL)
1554 remove_http_message(ps, ps->pending_msgs_head);
1556 ps->recv_active = GNUNET_NO;
1557 ps->send_active = GNUNET_NO;
1563 static int curl_schedule(void *cls)
1565 struct Plugin *plugin = cls;
1570 struct GNUNET_NETWORK_FDSet *grs;
1571 struct GNUNET_NETWORK_FDSet *gws;
1575 GNUNET_assert(cls !=NULL);
1577 /* Cancel previous scheduled task */
1578 if (plugin->http_curl_task != GNUNET_SCHEDULER_NO_TASK)
1580 GNUNET_SCHEDULER_cancel(plugin->env->sched, plugin->http_curl_task);
1581 plugin->http_curl_task = GNUNET_SCHEDULER_NO_TASK;
1587 mret = curl_multi_fdset (plugin->multi_handle, &rs, &ws, &es, &max);
1588 if (mret != CURLM_OK)
1590 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1591 _("%s failed at %s:%d: `%s'\n"),
1592 "curl_multi_fdset", __FILE__, __LINE__,
1593 curl_multi_strerror (mret));
1594 return GNUNET_SYSERR;
1596 mret = curl_multi_timeout (plugin->multi_handle, &to);
1597 if (mret != CURLM_OK)
1599 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1600 _("%s failed at %s:%d: `%s'\n"),
1601 "curl_multi_timeout", __FILE__, __LINE__,
1602 curl_multi_strerror (mret));
1603 return GNUNET_SYSERR;
1606 grs = GNUNET_NETWORK_fdset_create ();
1607 gws = GNUNET_NETWORK_fdset_create ();
1608 GNUNET_NETWORK_fdset_copy_native (grs, &rs, max + 1);
1609 GNUNET_NETWORK_fdset_copy_native (gws, &ws, max + 1);
1610 plugin->http_curl_task = GNUNET_SCHEDULER_add_select (plugin->env->sched,
1611 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1612 GNUNET_SCHEDULER_NO_TASK,
1613 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 0),
1618 GNUNET_NETWORK_fdset_destroy (gws);
1619 GNUNET_NETWORK_fdset_destroy (grs);
1624 * Function setting up curl handle and selecting message to send
1626 * @param ses session to send data to
1627 * @param con connection
1628 * @return GNUNET_SYSERR on failure, GNUNET_NO if connecting, GNUNET_YES if ok
1630 static ssize_t send_check_connections (void *cls, struct Session *ps)
1632 struct Plugin *plugin = cls;
1634 struct HTTP_Message * msg;
1636 struct GNUNET_TIME_Relative timeout = GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT;
1638 GNUNET_assert(cls !=NULL);
1640 if (ps->direction == OUTBOUND)
1642 /* RECV DIRECTION */
1643 /* Check if session is connected to receive data, otherwise connect to peer */
1644 if (ps->recv_connected == GNUNET_NO)
1646 int fresh = GNUNET_NO;
1647 if (ps->recv_endpoint == NULL)
1650 ps->recv_endpoint = curl_easy_init();
1653 curl_easy_setopt(ps->recv_endpoint, CURLOPT_VERBOSE, 1L);
1655 curl_easy_setopt(ps->recv_endpoint, CURLOPT_URL, ps->url);
1656 curl_easy_setopt (ps->recv_endpoint, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1);
1657 //curl_easy_setopt (ps->recv_endpoint, CURLOPT_SSL_CIPHER_LIST, cipher_suite);
1658 curl_easy_setopt(ps->recv_endpoint, CURLOPT_SSL_VERIFYPEER, 0);
1659 curl_easy_setopt(ps->recv_endpoint, CURLOPT_SSL_VERIFYHOST, 0);
1660 curl_easy_setopt(ps->recv_endpoint, CURLOPT_HEADERFUNCTION, &curl_get_header_cb);
1661 curl_easy_setopt(ps->recv_endpoint, CURLOPT_WRITEHEADER, ps);
1662 curl_easy_setopt(ps->recv_endpoint, CURLOPT_READFUNCTION, curl_send_cb);
1663 curl_easy_setopt(ps->recv_endpoint, CURLOPT_READDATA, ps);
1664 curl_easy_setopt(ps->recv_endpoint, CURLOPT_WRITEFUNCTION, curl_receive_cb);
1665 curl_easy_setopt(ps->recv_endpoint, CURLOPT_WRITEDATA, ps);
1666 curl_easy_setopt(ps->recv_endpoint, CURLOPT_TIMEOUT, (long) timeout.value);
1667 curl_easy_setopt(ps->recv_endpoint, CURLOPT_PRIVATE, ps);
1668 curl_easy_setopt(ps->recv_endpoint, CURLOPT_CONNECTTIMEOUT, HTTP_CONNECT_TIMEOUT);
1669 curl_easy_setopt(ps->recv_endpoint, CURLOPT_BUFFERSIZE, GNUNET_SERVER_MAX_MESSAGE_SIZE);
1671 if (fresh==GNUNET_YES)
1673 mret = curl_multi_add_handle(plugin->multi_handle, ps->recv_endpoint);
1674 if (mret != CURLM_OK)
1676 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1677 _("Connection: %X: %s failed at %s:%d: `%s'\n"),
1679 "curl_multi_add_handle", __FILE__, __LINE__,
1680 curl_multi_strerror (mret));
1681 return GNUNET_SYSERR;
1684 if (curl_schedule (plugin) == GNUNET_SYSERR)
1686 #if DEBUG_CONNECTIONS
1687 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: could not schedule curl task\n",ps);
1689 return GNUNET_SYSERR;
1691 #if DEBUG_CONNECTIONS
1692 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: inbound not connected, initiating connection\n",ps);
1696 /* waiting for receive direction */
1697 if (ps->recv_connected==GNUNET_NO)
1700 /* SEND DIRECTION */
1701 /* Check if session is connected to send data, otherwise connect to peer */
1702 if ((ps->send_connected == GNUNET_YES) && (ps->send_endpoint!= NULL))
1704 if (ps->send_active == GNUNET_YES)
1706 #if DEBUG_CONNECTIONS
1707 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: outbound active, enqueueing message\n",ps);
1711 if (ps->send_active == GNUNET_NO)
1713 #if DEBUG_CONNECTIONS
1714 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: outbound paused, unpausing existing connection and enqueueing message\n",ps);
1716 if (CURLE_OK == curl_easy_pause(ps->send_endpoint,CURLPAUSE_CONT))
1718 ps->send_active=GNUNET_YES;
1722 return GNUNET_SYSERR;
1725 /* not connected, initiate connection */
1726 if (ps->send_connected==GNUNET_NO)
1728 int fresh = GNUNET_NO;
1729 if (NULL == ps->send_endpoint)
1731 ps->send_endpoint = curl_easy_init();
1734 GNUNET_assert (ps->send_endpoint != NULL);
1735 GNUNET_assert (NULL != ps->pending_msgs_tail);
1736 #if DEBUG_CONNECTIONS
1737 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: outbound not connected, initiating connection\n",ps);
1739 ps->send_active = GNUNET_NO;
1740 msg = ps->pending_msgs_tail;
1743 curl_easy_setopt(ps->send_endpoint, CURLOPT_VERBOSE, 1L);
1745 curl_easy_setopt(ps->send_endpoint, CURLOPT_URL, ps->url);
1746 curl_easy_setopt(ps->send_endpoint, CURLOPT_PUT, 1L);
1747 curl_easy_setopt(ps->send_endpoint, CURLOPT_HEADERFUNCTION, &curl_put_header_cb);
1748 curl_easy_setopt(ps->send_endpoint, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1);
1749 curl_easy_setopt(ps->send_endpoint, CURLOPT_SSL_VERIFYPEER, 0);
1750 curl_easy_setopt(ps->send_endpoint, CURLOPT_SSL_VERIFYHOST, 0);
1752 curl_easy_setopt(ps->send_endpoint, CURLOPT_WRITEHEADER, ps);
1753 curl_easy_setopt(ps->send_endpoint, CURLOPT_READFUNCTION, curl_send_cb);
1754 curl_easy_setopt(ps->send_endpoint, CURLOPT_READDATA, ps);
1755 curl_easy_setopt(ps->send_endpoint, CURLOPT_WRITEFUNCTION, curl_receive_cb);
1756 curl_easy_setopt(ps->send_endpoint, CURLOPT_READDATA, ps);
1757 curl_easy_setopt(ps->send_endpoint, CURLOPT_TIMEOUT, (long) timeout.value);
1758 curl_easy_setopt(ps->send_endpoint, CURLOPT_PRIVATE, ps);
1759 curl_easy_setopt(ps->send_endpoint, CURLOPT_CONNECTTIMEOUT, HTTP_CONNECT_TIMEOUT);
1760 curl_easy_setopt(ps->send_endpoint, CURLOPT_BUFFERSIZE, GNUNET_SERVER_MAX_MESSAGE_SIZE);
1762 if (fresh==GNUNET_YES)
1764 mret = curl_multi_add_handle(plugin->multi_handle, ps->send_endpoint);
1765 if (mret != CURLM_OK)
1767 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1768 _("Connection: %X: %s failed at %s:%d: `%s'\n"),
1770 "curl_multi_add_handle", __FILE__, __LINE__,
1771 curl_multi_strerror (mret));
1772 return GNUNET_SYSERR;
1776 if (curl_schedule (plugin) == GNUNET_SYSERR)
1777 return GNUNET_SYSERR;
1780 if (ps->direction == INBOUND)
1782 GNUNET_assert (NULL != ps->pending_msgs_tail);
1783 if ((ps->recv_connected==GNUNET_YES) && (ps->send_connected==GNUNET_YES) &&
1784 (ps->recv_force_disconnect==GNUNET_NO) && (ps->recv_force_disconnect==GNUNET_NO))
1787 return GNUNET_SYSERR;
1790 static struct Session * send_select_session (void * cls, struct HTTP_PeerContext *pc, const void * addr, size_t addrlen, int force_address, struct Session * session)
1792 struct Session * tmp = NULL;
1793 int addr_given = GNUNET_NO;
1795 if ((addr!=NULL) && (addrlen>0))
1796 addr_given = GNUNET_YES;
1798 if (force_address == GNUNET_YES)
1800 /* check session given as argument */
1801 if ((session != NULL) && (addr_given == GNUNET_YES))
1803 if (0 == memcmp(session->addr, addr, addrlen))
1805 /* connection can not be used, since it is disconnected */
1806 if ((session->recv_force_disconnect==GNUNET_NO) && (session->send_force_disconnect==GNUNET_NO))
1808 #if DEBUG_SESSION_SELECTION
1809 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Session %X selected: Using session passed by transport to send to forced address \n", session);
1815 /* check last session used */
1816 if ((pc->last_session != NULL)&& (addr_given == GNUNET_YES))
1818 if (0 == memcmp(pc->last_session->addr, addr, addrlen))
1820 /* connection can not be used, since it is disconnected */
1821 if ((pc->last_session->recv_force_disconnect==GNUNET_NO) && (pc->last_session->send_force_disconnect==GNUNET_NO))
1823 #if DEBUG_SESSION_SELECTION
1824 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Session %X selected: Using last session used to send to forced address \n", pc->last_session);
1826 return pc->last_session;
1830 /* find session in existing sessions */
1832 while ((tmp!=NULL) && (addr_given == GNUNET_YES))
1835 if (0 == memcmp(tmp->addr, addr, addrlen))
1837 /* connection can not be used, since it is disconnected */
1838 if ((tmp->recv_force_disconnect==GNUNET_NO) && (tmp->send_force_disconnect==GNUNET_NO))
1840 #if DEBUG_SESSION_SELECTION
1841 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Session %X selected: Using existing session to send to forced address \n", session);
1849 /* no session to use */
1852 if ((force_address == GNUNET_NO) || (force_address == GNUNET_SYSERR))
1854 /* check session given as argument */
1855 if (session != NULL)
1857 /* connection can not be used, since it is disconnected */
1858 if ((session->recv_force_disconnect==GNUNET_NO) && (session->send_force_disconnect==GNUNET_NO))
1860 #if DEBUG_SESSION_SELECTION
1861 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Session %X selected: Using session passed by transport to send not-forced address \n", session);
1867 /* check last session used */
1868 if (pc->last_session != NULL)
1870 /* connection can not be used, since it is disconnected */
1871 if ((pc->last_session->recv_force_disconnect==GNUNET_NO) && (pc->last_session->send_force_disconnect==GNUNET_NO))
1873 #if DEBUG_SESSION_SELECTION
1874 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Session %X selected: Using last session to send to not-forced address \n", pc->last_session);
1876 return pc->last_session;
1879 /* find session in existing sessions */
1883 /* connection can not be used, since it is disconnected */
1884 if ((tmp->recv_force_disconnect==GNUNET_NO) && (tmp->send_force_disconnect==GNUNET_NO))
1886 #if DEBUG_SESSION_SELECTION
1887 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Session %X selected: Using existing session to send to not-forced address \n", tmp);
1899 * Function that can be used by the transport service to transmit
1900 * a message using the plugin. Note that in the case of a
1901 * peer disconnecting, the continuation MUST be called
1902 * prior to the disconnect notification itself. This function
1903 * will be called with this peer's HELLO message to initiate
1904 * a fresh connection to another peer.
1906 * @param cls closure
1907 * @param target who should receive this message
1908 * @param msgbuf the message to transmit
1909 * @param msgbuf_size number of bytes in 'msgbuf'
1910 * @param priority how important is the message (most plugins will
1911 * ignore message priority and just FIFO)
1912 * @param timeout how long to wait at most for the transmission (does not
1913 * require plugins to discard the message after the timeout,
1914 * just advisory for the desired delay; most plugins will ignore
1916 * @param session which session must be used (or NULL for "any")
1917 * @param addr the address to use (can be NULL if the plugin
1918 * is "on its own" (i.e. re-use existing TCP connection))
1919 * @param addrlen length of the address in bytes
1920 * @param force_address GNUNET_YES if the plugin MUST use the given address,
1921 * GNUNET_NO means the plugin may use any other address and
1922 * GNUNET_SYSERR means that only reliable existing
1923 * bi-directional connections should be used (regardless
1925 * @param cont continuation to call once the message has
1926 * been transmitted (or if the transport is ready
1927 * for the next transmission call; or if the
1928 * peer disconnected...); can be NULL
1929 * @param cont_cls closure for cont
1930 * @return number of bytes used (on the physical network, with overheads);
1931 * -1 on hard errors (i.e. address invalid); 0 is a legal value
1932 * and does NOT mean that the message was not transmitted (DV)
1935 http_plugin_send (void *cls,
1936 const struct GNUNET_PeerIdentity *target,
1939 unsigned int priority,
1940 struct GNUNET_TIME_Relative to,
1941 struct Session *session,
1945 GNUNET_TRANSPORT_TransmitContinuation cont,
1948 struct Plugin *plugin = cls;
1949 struct HTTP_Message *msg;
1950 struct HTTP_PeerContext * pc;
1951 struct Session * ps = NULL;
1953 GNUNET_assert(cls !=NULL);
1956 char * force = GNUNET_malloc(40);
1957 if (force_address == GNUNET_YES)
1958 strcpy(force,"forced addr.");
1959 if (force_address == GNUNET_NO)
1960 strcpy(force,"any addr.");
1961 if (force_address == GNUNET_SYSERR)
1962 strcpy(force,"reliable bi-direc. address addr.");
1964 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Transport tells me to send %u bytes to `%s' using %s (%s) and session: %X\n",
1968 http_plugin_address_to_string(NULL, addr, addrlen),
1974 pc = GNUNET_CONTAINER_multihashmap_get (plugin->peers, &target->hashPubKey);
1978 pc = GNUNET_malloc(sizeof (struct HTTP_PeerContext));
1979 pc->plugin = plugin;
1980 pc->session_id_counter=1;
1981 pc->last_session = NULL;
1982 memcpy(&pc->identity, target, sizeof(struct GNUNET_PeerIdentity));
1983 GNUNET_CONTAINER_multihashmap_put(plugin->peers, &pc->identity.hashPubKey, pc, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
1984 GNUNET_STATISTICS_update (plugin->env->stats,
1985 gettext_noop ("# HTTP peers active"),
1990 ps = send_select_session (plugin, pc, addr, addrlen, force_address, session);
1992 /* session not existing, but address forced -> creating new session */
1995 if ((addr!=NULL) && (addrlen!=0))
1997 ps = GNUNET_malloc(sizeof (struct Session));
1998 #if DEBUG_SESSION_SELECTION
1999 if (force_address == GNUNET_YES)
2000 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"No existing connection & forced address: creating new session %X to peer %s\n", ps, GNUNET_i2s(target));
2001 if (force_address != GNUNET_YES)
2002 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"No existing connection: creating new session %X to peer %s\n", ps, GNUNET_i2s(target));
2004 if ((addrlen!=0) && (addr!=NULL))
2006 ps->addr = GNUNET_malloc(addrlen);
2007 memcpy(ps->addr,addr,addrlen);
2008 ps->addrlen = addrlen;
2015 ps->direction=OUTBOUND;
2016 ps->recv_connected = GNUNET_NO;
2017 ps->recv_force_disconnect = GNUNET_NO;
2018 ps->send_connected = GNUNET_NO;
2019 ps->send_force_disconnect = GNUNET_NO;
2020 ps->pending_msgs_head = NULL;
2021 ps->pending_msgs_tail = NULL;
2023 ps->session_id = pc->session_id_counter;
2024 pc->session_id_counter++;
2025 ps->url = create_url (plugin, ps->addr, ps->addrlen, ps->session_id);
2026 if (ps->msgtok == NULL)
2027 ps->msgtok = GNUNET_SERVER_mst_create (&curl_receive_mst_cb, ps);
2028 GNUNET_CONTAINER_DLL_insert(pc->head,pc->tail,ps);
2031 GNUNET_STATISTICS_update (plugin->env->stats,
2032 gettext_noop ("# HTTP outbound sessions for peers active"),
2039 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));
2041 return GNUNET_SYSERR;
2046 msg = GNUNET_malloc (sizeof (struct HTTP_Message) + msgbuf_size);
2048 msg->size = msgbuf_size;
2050 msg->buf = (char *) &msg[1];
2051 msg->transmit_cont = cont;
2052 msg->transmit_cont_cls = cont_cls;
2053 memcpy (msg->buf,msgbuf, msgbuf_size);
2054 GNUNET_CONTAINER_DLL_insert(ps->pending_msgs_head,ps->pending_msgs_tail,msg);
2056 if (send_check_connections (plugin, ps) != GNUNET_SYSERR)
2058 if (force_address != GNUNET_YES)
2059 pc->last_session = ps;
2061 if (pc->last_session==NULL)
2062 pc->last_session = ps;
2066 return GNUNET_SYSERR;
2072 * Function that can be used to force the plugin to disconnect
2073 * from the given peer and cancel all previous transmissions
2074 * (and their continuationc).
2076 * @param cls closure
2077 * @param target peer from which to disconnect
2080 * Convert the transports address to a nice, human-readable
2083 * @param cls closure
2084 * @param type name of the transport that generated the address
2085 * @param addr one of the addresses of the host, NULL for the last address
2086 * the specific address format depends on the transport
2087 * @param addrlen length of the address
2088 * @param numeric should (IP) addresses be displayed in numeric form?
2089 * @param timeout after how long should we give up?
2090 * @param asc function to call on each string
2091 * @param asc_cls closure for asc
2094 http_plugin_address_pretty_printer (void *cls,
2099 struct GNUNET_TIME_Relative timeout,
2100 GNUNET_TRANSPORT_AddressStringCallback
2103 const struct IPv4HttpAddress *t4;
2104 const struct IPv6HttpAddress *t6;
2105 struct sockaddr_in a4;
2106 struct sockaddr_in6 a6;
2112 GNUNET_assert(cls !=NULL);
2113 if (addrlen == sizeof (struct IPv6HttpAddress))
2115 address = GNUNET_malloc (INET6_ADDRSTRLEN);
2117 a6.sin6_addr = t6->ipv6_addr;
2118 inet_ntop(AF_INET6, &(a6.sin6_addr),address,INET6_ADDRSTRLEN);
2119 port = ntohs(t6->u6_port);
2121 else if (addrlen == sizeof (struct IPv4HttpAddress))
2123 address = GNUNET_malloc (INET_ADDRSTRLEN);
2125 a4.sin_addr.s_addr = t4->ipv4_addr;
2126 inet_ntop(AF_INET, &(a4.sin_addr),address,INET_ADDRSTRLEN);
2127 port = ntohs(t4->u_port);
2131 /* invalid address */
2132 GNUNET_break_op (0);
2133 asc (asc_cls, NULL);
2136 res = GNUNET_asprintf(&ret,"http://%s:%u/",address,port);
2137 GNUNET_free (address);
2138 GNUNET_assert(res != 0);
2140 GNUNET_free_non_null (ret);
2146 * Another peer has suggested an address for this
2147 * peer and transport plugin. Check that this could be a valid
2148 * address. If so, consider adding it to the list
2151 * @param cls closure
2152 * @param addr pointer to the address
2153 * @param addrlen length of addr
2154 * @return GNUNET_OK if this is a plausible address for this peer
2158 http_plugin_address_suggested (void *cls,
2159 const void *addr, size_t addrlen)
2161 struct Plugin *plugin = cls;
2162 struct IPv4HttpAddress *v4;
2163 struct IPv6HttpAddress *v6;
2166 GNUNET_assert(cls !=NULL);
2167 if ((addrlen != sizeof (struct IPv4HttpAddress)) &&
2168 (addrlen != sizeof (struct IPv6HttpAddress)))
2170 return GNUNET_SYSERR;
2172 if (addrlen == sizeof (struct IPv4HttpAddress))
2174 v4 = (struct IPv4HttpAddress *) addr;
2175 /* Not skipping loopback
2176 if (INADDR_LOOPBACK == ntohl(v4->ipv4_addr))
2178 return GNUNET_SYSERR;
2180 port = ntohs (v4->u_port);
2181 if (port != plugin->port_inbound)
2183 return GNUNET_SYSERR;
2186 if (addrlen == sizeof (struct IPv6HttpAddress))
2188 v6 = (struct IPv6HttpAddress *) addr;
2189 if (IN6_IS_ADDR_LINKLOCAL (&v6->ipv6_addr))
2191 return GNUNET_SYSERR;
2193 port = ntohs (v6->u6_port);
2194 if (port != plugin->port_inbound)
2196 return GNUNET_SYSERR;
2205 * Function called for a quick conversion of the binary address to
2206 * a numeric address. Note that the caller must not free the
2207 * address and that the next call to this function is allowed
2208 * to override the address again.
2210 * @param cls closure
2211 * @param addr binary address
2212 * @param addrlen length of the address
2213 * @return string representing the same address
2216 http_plugin_address_to_string (void *cls,
2220 const struct IPv4HttpAddress *t4;
2221 const struct IPv6HttpAddress *t6;
2222 struct sockaddr_in a4;
2223 struct sockaddr_in6 a6;
2229 if (addrlen == sizeof (struct IPv6HttpAddress))
2231 address = GNUNET_malloc (INET6_ADDRSTRLEN);
2233 a6.sin6_addr = t6->ipv6_addr;
2234 inet_ntop(AF_INET6, &(a6.sin6_addr),address,INET6_ADDRSTRLEN);
2235 port = ntohs(t6->u6_port);
2237 else if (addrlen == sizeof (struct IPv4HttpAddress))
2239 address = GNUNET_malloc (INET_ADDRSTRLEN);
2241 a4.sin_addr.s_addr = t4->ipv4_addr;
2242 inet_ntop(AF_INET, &(a4.sin_addr),address,INET_ADDRSTRLEN);
2243 port = ntohs(t4->u_port);
2247 /* invalid address */
2250 res = GNUNET_asprintf(&ret,"%s:%u",address,port);
2251 GNUNET_free (address);
2252 GNUNET_assert(res != 0);
2257 load_certificate( const char * file )
2259 struct GNUNET_DISK_FileHandle * gn_file;
2264 if (0!=STAT(file, &fstat))
2266 text = GNUNET_malloc (fstat.st_size);
2267 gn_file = GNUNET_DISK_file_open(file,GNUNET_DISK_OPEN_READ, GNUNET_DISK_PERM_USER_READ);
2273 if (GNUNET_SYSERR == GNUNET_DISK_file_read(gn_file, text, fstat.st_size))
2278 text[fstat.st_size] = '\0';
2279 GNUNET_DISK_file_close(gn_file);
2286 * Exit point from the plugin.
2289 libgnunet_plugin_transport_https_done (void *cls)
2291 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
2292 struct Plugin *plugin = api->cls;
2294 GNUNET_assert(cls !=NULL);
2296 if (plugin->http_server_daemon_v4 != NULL)
2298 MHD_stop_daemon (plugin->http_server_daemon_v4);
2299 plugin->http_server_daemon_v4 = NULL;
2301 if (plugin->http_server_daemon_v6 != NULL)
2303 MHD_stop_daemon (plugin->http_server_daemon_v6);
2304 plugin->http_server_daemon_v6 = NULL;
2307 if ( plugin->http_server_task_v4 != GNUNET_SCHEDULER_NO_TASK)
2309 GNUNET_SCHEDULER_cancel(plugin->env->sched, plugin->http_server_task_v4);
2310 plugin->http_server_task_v4 = GNUNET_SCHEDULER_NO_TASK;
2313 if ( plugin->http_server_task_v6 != GNUNET_SCHEDULER_NO_TASK)
2315 GNUNET_SCHEDULER_cancel(plugin->env->sched, plugin->http_server_task_v6);
2316 plugin->http_server_task_v6 = GNUNET_SCHEDULER_NO_TASK;
2320 /* free all peer information */
2321 if (plugin->peers!=NULL)
2323 GNUNET_CONTAINER_multihashmap_iterate (plugin->peers,
2324 &remove_peer_context_Iterator,
2326 GNUNET_CONTAINER_multihashmap_destroy (plugin->peers);
2328 if (plugin->multi_handle!=NULL)
2330 mret = curl_multi_cleanup(plugin->multi_handle);
2332 if ( CURLM_OK != mret)
2333 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"curl multihandle clean up failed\n");
2335 plugin->multi_handle = NULL;
2337 curl_global_cleanup();
2339 if ( plugin->http_curl_task != GNUNET_SCHEDULER_NO_TASK)
2341 GNUNET_SCHEDULER_cancel(plugin->env->sched, plugin->http_curl_task);
2342 plugin->http_curl_task = GNUNET_SCHEDULER_NO_TASK;
2345 GNUNET_free_non_null (plugin->bind4_address);
2346 GNUNET_free_non_null (plugin->bind6_address);
2347 GNUNET_free_non_null(plugin->bind_hostname);
2348 GNUNET_free (plugin);
2351 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Unload http plugin complete...\n");
2358 * Entry point for the plugin.
2361 libgnunet_plugin_transport_https_init (void *cls)
2363 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
2364 struct Plugin *plugin;
2365 struct GNUNET_TRANSPORT_PluginFunctions *api;
2366 struct GNUNET_TIME_Relative gn_timeout;
2367 long long unsigned int port;
2372 GNUNET_assert(cls !=NULL);
2374 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Starting https plugin...\n");
2377 plugin = GNUNET_malloc (sizeof (struct Plugin));
2378 plugin->stats = env->stats;
2380 plugin->peers = NULL;
2381 plugin->bind4_address = NULL;
2382 plugin->use_ipv6 = GNUNET_YES;
2383 plugin->use_ipv4 = GNUNET_YES;
2385 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
2387 api->send = &http_plugin_send;
2388 api->disconnect = &http_plugin_disconnect;
2389 api->address_pretty_printer = &http_plugin_address_pretty_printer;
2390 api->check_address = &http_plugin_address_suggested;
2391 api->address_to_string = &http_plugin_address_to_string;
2393 /* Hashing our identity to use it in URLs */
2394 GNUNET_CRYPTO_hash_to_enc ( &(plugin->env->my_identity->hashPubKey), &plugin->my_ascii_hash_ident);
2396 /* Use IPv6 yes/no */
2397 if (GNUNET_CONFIGURATION_have_value (env->cfg,
2398 "transport-https", "USE_IPv6"))
2400 plugin->use_ipv6 = GNUNET_CONFIGURATION_get_value_yesno (env->cfg,
2404 /* Use IPv4 yes/no */
2405 if (GNUNET_CONFIGURATION_have_value (env->cfg,
2406 "transport-https", "USE_IPv4"))
2408 plugin->use_ipv4 = GNUNET_CONFIGURATION_get_value_yesno (env->cfg,
2412 /* Reading port number from config file */
2414 GNUNET_CONFIGURATION_get_value_number (env->cfg,
2420 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
2422 _("Require valid port number for transport plugin `%s' in configuration!\n"),
2424 libgnunet_plugin_transport_https_done (api);
2428 /* Reading ipv4 addresse to bind to from config file */
2429 if ((plugin->use_ipv4==GNUNET_YES) && (GNUNET_CONFIGURATION_have_value (env->cfg,
2430 "transport-https", "BINDTO4")))
2432 GNUNET_break (GNUNET_OK ==
2433 GNUNET_CONFIGURATION_get_value_string (env->cfg,
2436 &plugin->bind_hostname));
2437 plugin->bind4_address = GNUNET_malloc(sizeof(struct sockaddr_in));
2438 plugin->bind4_address->sin_family = AF_INET;
2439 plugin->bind4_address->sin_port = htons (port);
2441 if (inet_pton(AF_INET,plugin->bind_hostname, &plugin->bind4_address->sin_addr)<=0)
2443 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
2445 _("Misconfigured address to bind to in configuration!\n"),
2447 GNUNET_free(plugin->bind4_address);
2448 GNUNET_free(plugin->bind_hostname);
2449 plugin->bind_hostname = NULL;
2450 plugin->bind4_address = NULL;
2454 /* Get private key file from config */
2455 if (GNUNET_CONFIGURATION_have_value (env->cfg,
2456 "transport-https", "KEY_FILE"))
2458 GNUNET_CONFIGURATION_get_value_string (env->cfg,
2465 GNUNET_asprintf(&key_file,"https.key");
2468 /* Get private key file from config */
2469 if (GNUNET_CONFIGURATION_have_value (env->cfg,
2470 "transport-https", "CERT_FILE"))
2472 GNUNET_CONFIGURATION_get_value_string (env->cfg,
2479 GNUNET_asprintf(&cert_file,"https.cert");
2482 /* Reading ipv4 addresse to bind to from config file */
2483 if ((plugin->use_ipv6==GNUNET_YES) && (GNUNET_CONFIGURATION_have_value (env->cfg,
2484 "transport-https", "BINDTO6")))
2486 GNUNET_break (GNUNET_OK ==
2487 GNUNET_CONFIGURATION_get_value_string (env->cfg,
2490 &plugin->bind_hostname));
2492 plugin->bind6_address = GNUNET_malloc(sizeof(struct sockaddr_in6));
2493 plugin->bind6_address->sin6_family = AF_INET6;
2494 plugin->bind6_address->sin6_port = htons (port);
2496 if (inet_pton(AF_INET6,plugin->bind_hostname, &plugin->bind6_address->sin6_addr)<=0)
2498 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
2500 _("Misconfigured address to bind to in configuration!\n"),
2502 GNUNET_free(plugin->bind6_address);
2503 GNUNET_free(plugin->bind_hostname);
2504 plugin->bind_hostname = NULL;
2505 plugin->bind6_address = NULL;
2509 /* read key & certificates from file */
2510 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Loading TLS certificate `%s' `%s'\n", key_file, cert_file);
2512 plugin->key = load_certificate( key_file );
2513 plugin->cert = load_certificate( cert_file );
2515 if ((plugin->key==NULL) || (plugin->cert==NULL))
2518 GNUNET_asprintf(&cmd,"gnunet-transport-certificate-creation %s %s", key_file, cert_file);
2519 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "No usable TLS certificate found, creating certificate \n");
2523 plugin->key = load_certificate( key_file );
2524 plugin->cert = load_certificate( cert_file );
2526 if ((plugin->key==NULL) || (plugin->cert==NULL))
2528 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "No usable TLS certificate found and creating one failed! \n");
2529 GNUNET_free (key_file);
2530 GNUNET_free (cert_file);
2531 libgnunet_plugin_transport_https_done(api);
2536 GNUNET_free (key_file);
2537 GNUNET_free (cert_file);
2540 GNUNET_assert((plugin->key!=NULL) && (plugin->cert!=NULL));
2541 GNUNET_assert ((port > 0) && (port <= 65535));
2542 plugin->port_inbound = port;
2543 gn_timeout = GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT;
2544 unsigned int timeout = (gn_timeout.value) / 1000;
2545 if ((plugin->http_server_daemon_v6 == NULL) && (plugin->use_ipv6 == GNUNET_YES) && (port != 0))
2547 struct sockaddr * tmp = (struct sockaddr *) plugin->bind6_address;
2548 plugin->http_server_daemon_v6 = MHD_start_daemon (
2549 #if DEBUG_CONNECTIONS
2552 MHD_USE_IPv6 | MHD_USE_SSL,
2555 plugin , &mdh_access_cb, plugin,
2556 MHD_OPTION_HTTPS_MEM_KEY, plugin->key,
2557 MHD_OPTION_HTTPS_MEM_CERT, plugin->cert,
2558 MHD_OPTION_SOCK_ADDR, tmp,
2559 MHD_OPTION_CONNECTION_LIMIT, (unsigned int) 32,
2560 //MHD_OPTION_PER_IP_CONNECTION_LIMIT, (unsigned int) 6,
2561 MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) timeout,
2562 MHD_OPTION_CONNECTION_MEMORY_LIMIT, (size_t) (16 * 1024),
2563 MHD_OPTION_NOTIFY_COMPLETED, &mhd_termination_cb, NULL,
2566 if ((plugin->http_server_daemon_v4 == NULL) && (plugin->use_ipv4 == GNUNET_YES) && (port != 0))
2568 plugin->http_server_daemon_v4 = MHD_start_daemon (
2569 #if DEBUG_CONNECTIONS
2572 MHD_NO_FLAG | MHD_USE_SSL,
2575 plugin , &mdh_access_cb, plugin,
2576 MHD_OPTION_HTTPS_MEM_KEY, plugin->key,
2577 MHD_OPTION_HTTPS_MEM_CERT, plugin->cert,
2578 MHD_OPTION_SOCK_ADDR, (struct sockaddr_in *)plugin->bind4_address,
2579 MHD_OPTION_CONNECTION_LIMIT, (unsigned int) 32,
2580 //MHD_OPTION_PER_IP_CONNECTION_LIMIT, (unsigned int) 6,
2581 MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) timeout,
2582 MHD_OPTION_CONNECTION_MEMORY_LIMIT, (size_t) (16 * 1024),
2583 MHD_OPTION_NOTIFY_COMPLETED, &mhd_termination_cb, NULL,
2586 if (plugin->http_server_daemon_v4 != NULL)
2587 plugin->http_server_task_v4 = http_server_daemon_prepare (plugin, plugin->http_server_daemon_v4);
2588 if (plugin->http_server_daemon_v6 != NULL)
2589 plugin->http_server_task_v6 = http_server_daemon_prepare (plugin, plugin->http_server_daemon_v6);
2592 if (plugin->http_server_task_v4 != GNUNET_SCHEDULER_NO_TASK)
2595 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);
2598 else if ((plugin->http_server_task_v6 != GNUNET_SCHEDULER_NO_TASK) && (plugin->http_server_task_v4 != GNUNET_SCHEDULER_NO_TASK))
2601 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);
2604 else if ((plugin->http_server_task_v6 != GNUNET_SCHEDULER_NO_TASK) && (plugin->http_server_task_v4 == GNUNET_SCHEDULER_NO_TASK))
2607 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);
2613 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"No MHD was started, transport plugin not functional!\n");
2615 libgnunet_plugin_transport_https_done (api);
2619 /* Initializing cURL */
2620 curl_global_init(CURL_GLOBAL_ALL);
2621 plugin->multi_handle = curl_multi_init();
2623 if ( NULL == plugin->multi_handle )
2625 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
2627 _("Could not initialize curl multi handle, failed to start http plugin!\n"),
2629 libgnunet_plugin_transport_https_done (api);
2633 plugin->peers = GNUNET_CONTAINER_multihashmap_create (10);
2634 GNUNET_OS_network_interfaces_list (&process_interfaces, plugin);
2639 /* end of plugin_transport_http.c */