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_CURL GNUNET_NO
47 #define DEBUG_CONNECTIONS GNUNET_NO
48 #define DEBUG_SESSION_SELECTION GNUNET_NO
50 #define INBOUND GNUNET_NO
51 #define OUTBOUND GNUNET_YES
53 #define PROTOCOL_PREFIX "https"
56 * Text of the response sent back after the last bytes of a PUT
57 * request have been received (just to formally obey the HTTP
60 #define HTTP_PUT_RESPONSE "Thank you!"
63 * After how long do we expire an address that we
64 * learned from another peer if it is not reconfirmed
67 #define LEARNED_ADDRESS_EXPIRATION GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_HOURS, 6)
70 * Page returned if request invalid
72 #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>"
75 * Timeout for a http connect
77 #define HTTP_CONNECT_TIMEOUT 30
80 * Network format for IPv4 addresses.
82 struct IPv4HttpAddress
85 * IPv4 address, in network byte order.
87 uint32_t ipv4_addr GNUNET_PACKED;
90 * Port number, in network byte order.
92 uint16_t u_port GNUNET_PACKED;
98 * Network format for IPv6 addresses.
100 struct IPv6HttpAddress
105 struct in6_addr ipv6_addr GNUNET_PACKED;
108 * Port number, in network byte order.
110 uint16_t u6_port GNUNET_PACKED;
116 * Message to send using http
121 * next pointer for double linked list
123 struct HTTP_Message * next;
126 * previous pointer for double linked list
128 struct HTTP_Message * prev;
131 * buffer containing data to send
136 * amount of data already sent
146 * Continuation function to call once the transmission buffer
147 * has again space available. NULL if there is no
148 * continuation to call.
150 GNUNET_TRANSPORT_TransmitContinuation transmit_cont;
153 * Closure for transmit_cont.
155 void *transmit_cont_cls;
159 struct HTTP_PeerContext
164 struct GNUNET_PeerIdentity identity;
167 * Pointer to the global plugin struct.
169 struct Plugin *plugin;
172 * Linked list of connections with this peer
175 struct Session * head;
178 * Linked list of connections with this peer
181 struct Session * tail;
184 * id for next session
186 size_t session_id_counter;
189 * Last session used to send data
191 struct Session * last_session;
200 struct SessionHeader header;
203 * next session in linked list
205 struct Session * next;
208 * previous session in linked list
210 struct Session * prev;
213 * address of this session
228 * Message queue for outbound messages
231 struct HTTP_Message * pending_msgs_head;
234 * Message queue for outbound messages
237 struct HTTP_Message * pending_msgs_tail;
240 * partner peer this connection belongs to
242 struct HTTP_PeerContext * peercontext;
245 * message stream tokenizer for incoming data
247 struct GNUNET_SERVER_MessageStreamTokenizer *msgtok;
251 * outbound: OUTBOUND (GNUNET_YES)
252 * inbound : INBOUND (GNUNET_NO)
254 unsigned int direction;
257 * is session connected to send data?
259 unsigned int send_connected;
262 * is send connection active?
264 unsigned int send_active;
267 * connection disconnect forced (e.g. from transport)
269 unsigned int send_force_disconnect;
272 * is session connected to receive data?
274 unsigned int recv_connected;
277 * is receive connection active?
279 unsigned int recv_active;
282 * connection disconnect forced (e.g. from transport)
284 unsigned int recv_force_disconnect;
287 * id for next session
288 * NOTE: 0 is not an ID, zero is not defined. A correct ID is always > 0
293 * entity managing sending data
294 * outbound session: CURL *
295 * inbound session: mhd_connection *
297 void * send_endpoint;
300 * entity managing recieving data
301 * outbound session: CURL *
302 * inbound session: mhd_connection *
304 void * recv_endpoint;
308 * Encapsulation of all of the state of the plugin.
315 struct GNUNET_TRANSPORT_PluginEnvironment *env;
318 * Handle for reporting statistics.
320 struct GNUNET_STATISTICS_Handle *stats;
322 unsigned int port_inbound;
324 struct GNUNET_CONTAINER_MultiHashMap *peers;
327 * Daemon for listening for new IPv4 connections.
329 struct MHD_Daemon *http_server_daemon_v4;
332 * Daemon for listening for new IPv6connections.
334 struct MHD_Daemon *http_server_daemon_v6;
337 * Our primary task for http daemon handling IPv4 connections
339 GNUNET_SCHEDULER_TaskIdentifier http_server_task_v4;
342 * Our primary task for http daemon handling IPv6 connections
344 GNUNET_SCHEDULER_TaskIdentifier http_server_task_v6;
347 * The task sending data
349 GNUNET_SCHEDULER_TaskIdentifier http_curl_task;
354 CURLM * multi_handle;
357 * Our ASCII encoded, hashed peer identity
358 * This string is used to distinguish between connections and is added to the urls
360 struct GNUNET_CRYPTO_HashAsciiEncoded my_ascii_hash_ident;
362 struct sockaddr_in * bind4_address;
363 struct sockaddr_in6 * bind6_address;
364 char * bind_hostname;
368 /* The certificate MHD uses as an \0 terminated string */
371 /* The private key MHD uses as an \0 terminated string */
379 * Function called for a quick conversion of the binary address to
380 * a numeric address. Note that the caller must not free the
381 * address and that the next call to this function is allowed
382 * to override the address again.
385 * @param addr binary address
386 * @param addrlen length of the address
387 * @return string representing the same address
390 http_plugin_address_to_string (void *cls,
396 * Call MHD to process pending ipv4 requests and then go back
397 * and schedule the next run.
399 static void http_server_daemon_v4_run (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
401 * Call MHD to process pending ipv6 requests and then go back
402 * and schedule the next run.
404 static void http_server_daemon_v6_run (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
407 * Function setting up curl handle and selecting message to send
409 * @param ses session to send data to
410 * @param con connection
411 * @return bytes sent to peer
413 static ssize_t send_check_connections (void *cls, struct Session *ps);
416 * Function setting up file descriptors and scheduling task to run
418 * @param ses session to send data to
421 static int curl_schedule(void *cls );
425 static char * create_url(void * cls, const void * addr, size_t addrlen, size_t id)
427 struct Plugin *plugin = cls;
430 GNUNET_assert ((addr!=NULL) && (addrlen != 0));
431 GNUNET_asprintf(&url,
432 "%s://%s/%s;%u", PROTOCOL_PREFIX,
433 http_plugin_address_to_string(NULL, addr, addrlen),
434 (char *) (&plugin->my_ascii_hash_ident),id);
440 * Removes a message from the linked list of messages
441 * @param con connection to remove message from
442 * @param msg message to remove
443 * @return GNUNET_SYSERR if msg not found, GNUNET_OK on success
445 static int remove_http_message (struct Session * ps, struct HTTP_Message * msg)
447 GNUNET_CONTAINER_DLL_remove(ps->pending_msgs_head,ps->pending_msgs_tail,msg);
452 int remove_peer_context_Iterator (void *cls, const GNUNET_HashCode *key, void *value);
455 * Removes a session from the linked list of sessions
456 * @param pc peer context
458 * @param call_msg_cont GNUNET_YES to call pending message continuations, otherwise no
459 * @param call_msg_cont_result, result to call message continuations with
460 * @return GNUNET_SYSERR if msg not found, GNUNET_OK on success
462 static int remove_session (struct HTTP_PeerContext * pc, struct Session * ps, int call_msg_cont, int call_msg_cont_result)
464 struct HTTP_Message * msg;
465 struct Plugin * plugin = ps->peercontext->plugin;
467 #if DEBUG_CONNECTIONS
468 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);
470 plugin->env->session_end(plugin, &pc->identity, ps);
472 GNUNET_free_non_null (ps->addr);
473 GNUNET_SERVER_mst_destroy (ps->msgtok);
474 GNUNET_free(ps->url);
476 if (ps->direction==INBOUND)
478 if (ps->recv_endpoint != NULL)
480 curl_easy_cleanup(ps->recv_endpoint);
481 ps->recv_endpoint = NULL;
483 if (ps->send_endpoint != NULL)
485 curl_easy_cleanup(ps->send_endpoint);
486 ps->send_endpoint = NULL;
490 msg = ps->pending_msgs_head;
493 if ((call_msg_cont == GNUNET_YES) && (msg->transmit_cont!=NULL))
495 msg->transmit_cont (msg->transmit_cont_cls,&pc->identity,call_msg_cont_result);
497 GNUNET_CONTAINER_DLL_remove(ps->pending_msgs_head,ps->pending_msgs_head,msg);
499 msg = ps->pending_msgs_head;
502 GNUNET_CONTAINER_DLL_remove(pc->head,pc->tail,ps);
506 /* no sessions left remove peer */
510 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"No sessions left for peer `%s', removing context\n",GNUNET_i2s(&pc->identity));
512 remove_peer_context_Iterator(plugin, &pc->identity.hashPubKey, pc);
518 int remove_peer_context_Iterator (void *cls, const GNUNET_HashCode *key, void *value)
520 struct Plugin *plugin = cls;
521 struct HTTP_PeerContext * pc = value;
522 struct Session * ps = pc->head;
523 struct Session * tmp = NULL;
524 struct HTTP_Message * msg = NULL;
525 struct HTTP_Message * msg_tmp = NULL;
527 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Freeing context for peer `%s'\n",GNUNET_i2s(&pc->identity));
529 GNUNET_CONTAINER_multihashmap_remove (plugin->peers, &pc->identity.hashPubKey, pc);
532 plugin->env->session_end(plugin, &pc->identity, ps);
535 GNUNET_free_non_null (ps->addr);
536 GNUNET_free(ps->url);
537 if (ps->msgtok != NULL)
538 GNUNET_SERVER_mst_destroy (ps->msgtok);
540 msg = ps->pending_msgs_head;
547 if (ps->direction==OUTBOUND)
549 if (ps->send_endpoint!=NULL)
550 curl_easy_cleanup(ps->send_endpoint);
551 if (ps->recv_endpoint!=NULL)
552 curl_easy_cleanup(ps->recv_endpoint);
559 GNUNET_STATISTICS_update (plugin->env->stats,
560 gettext_noop ("# HTTP peers active"),
567 * Add the IP of our network interface to the list of
568 * our external IP addresses.
570 * @param cls the 'struct Plugin*'
571 * @param name name of the interface
572 * @param isDefault do we think this may be our default interface
573 * @param addr address of the interface
574 * @param addrlen number of bytes in addr
575 * @return GNUNET_OK to continue iterating
578 process_interfaces (void *cls,
581 const struct sockaddr *addr, socklen_t addrlen)
583 struct Plugin *plugin = cls;
584 struct IPv4HttpAddress * t4;
585 struct IPv6HttpAddress * t6;
589 GNUNET_assert(cls !=NULL);
590 af = addr->sa_family;
591 if ((af == AF_INET) && (plugin->use_ipv4 == GNUNET_YES) && (plugin->bind6_address == NULL))
593 struct in_addr bnd_cmp = ((struct sockaddr_in *) addr)->sin_addr;
594 t4 = GNUNET_malloc(sizeof(struct IPv4HttpAddress));
595 /* Not skipping loopback addresses
596 if (INADDR_LOOPBACK == ntohl(((struct sockaddr_in *) addr)->sin_addr.s_addr))
602 t4->ipv4_addr = ((struct sockaddr_in *) addr)->sin_addr.s_addr;
603 t4->u_port = htons (plugin->port_inbound);
604 if (plugin->bind4_address != NULL)
606 if (0 == memcmp(&plugin->bind4_address->sin_addr, &bnd_cmp, sizeof (struct in_addr)))
608 plugin->env->notify_address(plugin->env->cls,"https",t4, sizeof (struct IPv4HttpAddress), GNUNET_TIME_UNIT_FOREVER_REL);
613 plugin->env->notify_address(plugin->env->cls,"https",t4, sizeof (struct IPv4HttpAddress), GNUNET_TIME_UNIT_FOREVER_REL);
617 else if ((af == AF_INET6) && (plugin->use_ipv6 == GNUNET_YES) && (plugin->bind4_address == NULL))
619 struct in6_addr bnd_cmp6 = ((struct sockaddr_in6 *) addr)->sin6_addr;
620 if (IN6_IS_ADDR_LINKLOCAL (&((struct sockaddr_in6 *) addr)->sin6_addr))
624 t6 = GNUNET_malloc(sizeof(struct IPv6HttpAddress));
625 if (plugin->bind6_address != NULL)
627 if (0 == memcmp(&plugin->bind6_address->sin6_addr, &bnd_cmp6, sizeof (struct in6_addr)))
629 memcpy (&t6->ipv6_addr,
630 &((struct sockaddr_in6 *) addr)->sin6_addr,
631 sizeof (struct in6_addr));
632 t6->u6_port = htons (plugin->port_inbound);
633 plugin->env->notify_address(plugin->env->cls,"https",t6,sizeof (struct IPv6HttpAddress) , GNUNET_TIME_UNIT_FOREVER_REL);
638 memcpy (&t6->ipv6_addr,
639 &((struct sockaddr_in6 *) addr)->sin6_addr,
640 sizeof (struct in6_addr));
641 t6->u6_port = htons (plugin->port_inbound);
642 plugin->env->notify_address(plugin->env->cls,"https",t6,sizeof (struct IPv6HttpAddress) , GNUNET_TIME_UNIT_FOREVER_REL);
651 * Callback called by MHD when a connection is terminated
653 static void mhd_termination_cb (void *cls, struct MHD_Connection * connection, void **httpSessionCache)
655 struct Session * ps = *httpSessionCache;
658 struct HTTP_PeerContext * pc = ps->peercontext;
660 if (connection==ps->recv_endpoint)
662 #if DEBUG_CONNECTIONS
663 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: inbound connection from peer `%s' was terminated\n", ps, GNUNET_i2s(&pc->identity));
665 ps->recv_active = GNUNET_NO;
666 ps->recv_connected = GNUNET_NO;
667 ps->recv_endpoint = NULL;
669 if (connection==ps->send_endpoint)
672 ps->send_active = GNUNET_NO;
673 ps->send_connected = GNUNET_NO;
674 ps->send_endpoint = NULL;
675 #if DEBUG_CONNECTIONS
676 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: outbound connection from peer `%s' was terminated\n", ps, GNUNET_i2s(&pc->identity));
680 /* if both connections disconnected, remove session */
681 if ((ps->send_connected == GNUNET_NO) && (ps->recv_connected == GNUNET_NO))
683 GNUNET_STATISTICS_update (pc->plugin->env->stats,
684 gettext_noop ("# HTTPS inbound sessions for peers active"),
687 remove_session(pc,ps,GNUNET_YES,GNUNET_SYSERR);
691 static void mhd_write_mst_cb (void *cls,
693 const struct GNUNET_MessageHeader *message)
696 struct Session *ps = cls;
697 struct HTTP_PeerContext *pc = ps->peercontext;
698 GNUNET_assert(ps != NULL);
699 GNUNET_assert(pc != NULL);
701 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
702 "Connection %X: Forwarding message to transport service, type %u and size %u from `%s' (`%s')\n",
704 ntohs(message->type),
705 ntohs(message->size),
706 GNUNET_i2s(&(ps->peercontext)->identity),http_plugin_address_to_string(NULL,ps->addr,ps->addrlen));
708 pc->plugin->env->receive (ps->peercontext->plugin->env->cls,
716 * Check if ip is allowed to connect.
719 mhd_accept_cb (void *cls,
720 const struct sockaddr *addr, socklen_t addr_len)
723 struct Plugin *plugin = cls;
725 /* Every connection is accepted, nothing more to do here */
729 int mhd_send_callback (void *cls, uint64_t pos, char *buf, int max)
733 struct Session * ps = cls;
734 struct HTTP_PeerContext * pc;
735 struct HTTP_Message * msg;
737 GNUNET_assert (ps!=NULL);
738 pc = ps->peercontext;
739 msg = ps->pending_msgs_tail;
740 if (ps->send_force_disconnect==GNUNET_YES)
742 #if DEBUG_CONNECTIONS
743 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: outbound forced to disconnect\n",ps);
750 if ((msg->size-msg->pos) <= max)
752 memcpy(buf,&msg->buf[msg->pos],(msg->size-msg->pos));
753 bytes_read = msg->size-msg->pos;
754 msg->pos+=(msg->size-msg->pos);
758 memcpy(buf,&msg->buf[msg->pos],max);
763 if (msg->pos==msg->size)
765 if (NULL!=msg->transmit_cont)
766 msg->transmit_cont (msg->transmit_cont_cls,&pc->identity,GNUNET_OK);
767 remove_http_message(ps,msg);
774 * Process GET or PUT request received via MHD. For
775 * GET, queue response that will send back our pending
776 * messages. For PUT, process incoming data and send
777 * to GNUnet core. In either case, check if a session
778 * already exists and create a new one if not.
781 mdh_access_cb (void *cls,
782 struct MHD_Connection *mhd_connection,
786 const char *upload_data,
787 size_t * upload_data_size, void **httpSessionCache)
789 struct Plugin *plugin = cls;
790 struct MHD_Response *response;
791 const union MHD_ConnectionInfo * conn_info;
793 struct sockaddr_in *addrin;
794 struct sockaddr_in6 *addrin6;
796 char address[INET6_ADDRSTRLEN+14];
797 struct GNUNET_PeerIdentity pi_in;
800 struct IPv4HttpAddress ipv4addr;
801 struct IPv6HttpAddress ipv6addr;
803 struct HTTP_PeerContext *pc;
804 struct Session *ps = NULL;
805 struct Session *ps_tmp = NULL;
808 int send_error_to_client;
812 GNUNET_assert(cls !=NULL);
813 send_error_to_client = GNUNET_NO;
815 if (NULL == *httpSessionCache)
817 /* check url for peer identity , if invalid send HTTP 404*/
818 size_t len = strlen(&url[1]);
819 char * peer = GNUNET_malloc(104+1);
821 if ((len>104) && (url[104]==';'))
823 char * id = GNUNET_malloc((len-104)+1);
824 strcpy(id,&url[105]);
825 memcpy(peer,&url[1],103);
827 id_num = strtoul ( id, NULL , 10);
830 res = GNUNET_CRYPTO_hash_from_string (peer, &(pi_in.hashPubKey));
832 if ( GNUNET_SYSERR == res )
834 response = MHD_create_response_from_data (strlen (HTTP_ERROR_RESPONSE),HTTP_ERROR_RESPONSE, MHD_NO, MHD_NO);
835 res = MHD_queue_response (mhd_connection, MHD_HTTP_NOT_FOUND, response);
836 MHD_destroy_response (response);
837 #if DEBUG_CONNECTIONS
839 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Peer has no valid ident, sent HTTP 1.1/404\n");
841 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Peer has no valid ident, could not send error\n");
848 ps = *httpSessionCache;
849 pc = ps->peercontext;
852 if (NULL == *httpSessionCache)
854 /* get peer context */
855 pc = GNUNET_CONTAINER_multihashmap_get (plugin->peers, &pi_in.hashPubKey);
859 pc = GNUNET_malloc(sizeof (struct HTTP_PeerContext));
861 pc->session_id_counter=1;
862 pc->last_session = NULL;
863 memcpy(&pc->identity, &pi_in, sizeof(struct GNUNET_PeerIdentity));
864 GNUNET_CONTAINER_multihashmap_put(plugin->peers, &pc->identity.hashPubKey, pc, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
865 GNUNET_STATISTICS_update (plugin->env->stats,
866 gettext_noop ("# HTTP peers active"),
871 conn_info = MHD_get_connection_info(mhd_connection, MHD_CONNECTION_INFO_CLIENT_ADDRESS );
872 /* Incoming IPv4 connection */
873 if ( AF_INET == conn_info->client_addr->sin_family)
875 addrin = conn_info->client_addr;
876 inet_ntop(addrin->sin_family, &(addrin->sin_addr),address,INET_ADDRSTRLEN);
877 memcpy(&ipv4addr.ipv4_addr,&(addrin->sin_addr),sizeof(struct in_addr));
878 ipv4addr.u_port = addrin->sin_port;
880 addr_len = sizeof(struct IPv4HttpAddress);
882 /* Incoming IPv6 connection */
883 if ( AF_INET6 == conn_info->client_addr->sin_family)
885 addrin6 = (struct sockaddr_in6 *) conn_info->client_addr;
886 inet_ntop(addrin6->sin6_family, &(addrin6->sin6_addr),address,INET6_ADDRSTRLEN);
887 memcpy(&ipv6addr.ipv6_addr,&(addrin6->sin6_addr),sizeof(struct in6_addr));
888 ipv6addr.u6_port = addrin6->sin6_port;
890 addr_len = sizeof(struct IPv6HttpAddress);
894 /* only inbound sessions here */
899 if ((ps_tmp->direction==INBOUND) && (ps_tmp->session_id == id_num) && (id_num!=0))
901 if ((ps_tmp->recv_force_disconnect!=GNUNET_YES) && (ps_tmp->send_force_disconnect!=GNUNET_YES))
910 ps = GNUNET_malloc(sizeof (struct Session));
911 ps->addr = GNUNET_malloc(addr_len);
912 memcpy(ps->addr,addr,addr_len);
913 ps->addrlen = addr_len;
914 ps->direction=INBOUND;
915 ps->pending_msgs_head = NULL;
916 ps->pending_msgs_tail = NULL;
917 ps->send_connected=GNUNET_NO;
918 ps->send_active=GNUNET_NO;
919 ps->recv_connected=GNUNET_NO;
920 ps->recv_active=GNUNET_NO;
922 ps->session_id =id_num;
923 ps->url = create_url (plugin, ps->addr, ps->addrlen, ps->session_id);
924 GNUNET_CONTAINER_DLL_insert(pc->head,pc->tail,ps);
925 GNUNET_STATISTICS_update (plugin->env->stats,
926 gettext_noop ("# HTTPS inbound sessions for peers active"),
931 *httpSessionCache = ps;
932 if (ps->msgtok==NULL)
933 ps->msgtok = GNUNET_SERVER_mst_create (&mhd_write_mst_cb, ps);
935 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: HTTPS Daemon has new an incoming `%s' request from peer `%s' (`%s')\n",
938 GNUNET_i2s(&pc->identity),
939 http_plugin_address_to_string(NULL, ps->addr, ps->addrlen));
943 /* Is it a PUT or a GET request */
944 if (0 == strcmp (MHD_HTTP_METHOD_PUT, method))
946 if (ps->recv_force_disconnect == GNUNET_YES)
948 #if DEBUG_CONNECTIONS
949 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: inbound connection was forced to disconnect\n",ps);
951 ps->recv_active = GNUNET_NO;
954 if ((*upload_data_size == 0) && (ps->recv_active==GNUNET_NO))
956 ps->recv_endpoint = mhd_connection;
957 ps->recv_connected = GNUNET_YES;
958 ps->recv_active = GNUNET_YES;
959 ps->recv_force_disconnect = GNUNET_NO;
960 #if DEBUG_CONNECTIONS
961 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: inbound PUT connection connected\n",ps);
966 /* Transmission of all data complete */
967 if ((*upload_data_size == 0) && (ps->recv_active == GNUNET_YES))
969 response = MHD_create_response_from_data (strlen (HTTP_PUT_RESPONSE),HTTP_PUT_RESPONSE, MHD_NO, MHD_NO);
970 res = MHD_queue_response (mhd_connection, MHD_HTTP_OK, response);
971 #if DEBUG_CONNECTIONS
972 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: Sent HTTP/1.1: 200 OK as PUT Response\n",ps);
974 MHD_destroy_response (response);
975 ps->recv_active=GNUNET_NO;
980 if ((*upload_data_size > 0) && (ps->recv_active == GNUNET_YES))
982 res = GNUNET_SERVER_mst_receive(ps->msgtok, ps, upload_data,*upload_data_size, GNUNET_NO, GNUNET_NO);
983 (*upload_data_size) = 0;
989 if ( 0 == strcmp (MHD_HTTP_METHOD_GET, method) )
991 if (ps->send_force_disconnect == GNUNET_YES)
993 #if DEBUG_CONNECTIONS
994 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: outbound connection was forced to disconnect\n",ps);
996 ps->send_active = GNUNET_NO;
999 ps->send_connected = GNUNET_YES;
1000 ps->send_active = GNUNET_YES;
1001 ps->send_endpoint = mhd_connection;
1002 ps->send_force_disconnect = GNUNET_NO;
1003 #if DEBUG_CONNECTIONS
1004 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: inbound GET connection connected\n",ps);
1006 response = MHD_create_response_from_callback(-1,32 * 1024, &mhd_send_callback, ps, NULL);
1007 res = MHD_queue_response (mhd_connection, MHD_HTTP_OK, response);
1008 MHD_destroy_response (response);
1015 * Function that queries MHD's select sets and
1016 * starts the task waiting for them.
1018 static GNUNET_SCHEDULER_TaskIdentifier
1019 http_server_daemon_prepare (void * cls, struct MHD_Daemon *daemon_handle)
1021 struct Plugin *plugin = cls;
1022 GNUNET_SCHEDULER_TaskIdentifier ret;
1026 struct GNUNET_NETWORK_FDSet *wrs;
1027 struct GNUNET_NETWORK_FDSet *wws;
1028 struct GNUNET_NETWORK_FDSet *wes;
1030 unsigned long long timeout;
1032 struct GNUNET_TIME_Relative tv;
1034 GNUNET_assert(cls !=NULL);
1035 ret = GNUNET_SCHEDULER_NO_TASK;
1039 wrs = GNUNET_NETWORK_fdset_create ();
1040 wes = GNUNET_NETWORK_fdset_create ();
1041 wws = GNUNET_NETWORK_fdset_create ();
1043 GNUNET_assert (MHD_YES ==
1044 MHD_get_fdset (daemon_handle,
1049 haveto = MHD_get_timeout (daemon_handle, &timeout);
1050 if (haveto == MHD_YES)
1051 tv.value = (uint64_t) timeout;
1053 tv = GNUNET_TIME_UNIT_FOREVER_REL;
1054 GNUNET_NETWORK_fdset_copy_native (wrs, &rs, max);
1055 GNUNET_NETWORK_fdset_copy_native (wws, &ws, max);
1056 GNUNET_NETWORK_fdset_copy_native (wes, &es, max);
1057 if (daemon_handle == plugin->http_server_daemon_v4)
1059 if (plugin->http_server_task_v4 != GNUNET_SCHEDULER_NO_TASK)
1061 GNUNET_SCHEDULER_cancel(plugin->env->sched, plugin->http_server_task_v4);
1062 plugin->http_server_daemon_v4 = GNUNET_SCHEDULER_NO_TASK;
1065 ret = GNUNET_SCHEDULER_add_select (plugin->env->sched,
1066 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1067 GNUNET_SCHEDULER_NO_TASK,
1071 &http_server_daemon_v4_run,
1074 if (daemon_handle == plugin->http_server_daemon_v6)
1076 if (plugin->http_server_task_v6 != GNUNET_SCHEDULER_NO_TASK)
1078 GNUNET_SCHEDULER_cancel(plugin->env->sched, plugin->http_server_task_v6);
1079 plugin->http_server_task_v6 = 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_v6_run,
1091 GNUNET_NETWORK_fdset_destroy (wrs);
1092 GNUNET_NETWORK_fdset_destroy (wws);
1093 GNUNET_NETWORK_fdset_destroy (wes);
1098 * Call MHD to process pending requests and then go back
1099 * and schedule the next run.
1101 static void http_server_daemon_v4_run (void *cls,
1102 const struct GNUNET_SCHEDULER_TaskContext *tc)
1104 struct Plugin *plugin = cls;
1106 GNUNET_assert(cls !=NULL);
1107 plugin->http_server_task_v4 = GNUNET_SCHEDULER_NO_TASK;
1109 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1112 GNUNET_assert (MHD_YES == MHD_run (plugin->http_server_daemon_v4));
1113 plugin->http_server_task_v4 = http_server_daemon_prepare (plugin, plugin->http_server_daemon_v4);
1119 * Call MHD to process pending requests and then go back
1120 * and schedule the next run.
1122 static void http_server_daemon_v6_run (void *cls,
1123 const struct GNUNET_SCHEDULER_TaskContext *tc)
1125 struct Plugin *plugin = cls;
1127 GNUNET_assert(cls !=NULL);
1128 plugin->http_server_task_v6 = GNUNET_SCHEDULER_NO_TASK;
1130 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1133 GNUNET_assert (MHD_YES == MHD_run (plugin->http_server_daemon_v6));
1134 plugin->http_server_task_v6 = http_server_daemon_prepare (plugin, plugin->http_server_daemon_v6);
1138 static size_t curl_get_header_cb( void *ptr, size_t size, size_t nmemb, void *stream)
1140 struct Session * ps = stream;
1142 long http_result = 0;
1144 /* Getting last http result code */
1145 GNUNET_assert(NULL!=ps);
1146 if (ps->recv_connected==GNUNET_NO)
1148 res = curl_easy_getinfo(ps->recv_endpoint, CURLINFO_RESPONSE_CODE, &http_result);
1149 if (CURLE_OK == res)
1151 if (http_result == 200)
1153 ps->recv_connected = GNUNET_YES;
1154 ps->recv_active = GNUNET_YES;
1155 #if DEBUG_CONNECTIONS
1156 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: connected to recieve data\n",ps);
1158 // Calling send_check_connections again since receive is established
1159 send_check_connections (ps->peercontext->plugin, ps);
1166 size_t len = size * nmemb;
1169 if ((size * nmemb) < SIZE_MAX)
1170 tmp = GNUNET_malloc (len+1);
1172 if ((tmp != NULL) && (len > 0))
1174 memcpy(tmp,ptr,len);
1177 if (tmp[len-2] == 13)
1181 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: Header: %s\n",ps,tmp);
1188 return size * nmemb;
1191 static size_t curl_put_header_cb( void *ptr, size_t size, size_t nmemb, void *stream)
1193 struct Session * ps = stream;
1196 size_t len = size * nmemb;
1197 long http_result = 0;
1200 /* Getting last http result code */
1201 GNUNET_assert(NULL!=ps);
1202 res = curl_easy_getinfo(ps->send_endpoint, CURLINFO_RESPONSE_CODE, &http_result);
1203 if (CURLE_OK == res)
1205 if ((http_result == 100) && (ps->send_connected==GNUNET_NO))
1207 ps->send_connected = GNUNET_YES;
1208 ps->send_active = GNUNET_YES;
1209 #if DEBUG_CONNECTIONS
1210 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: connected to send data\n",ps);
1213 if ((http_result == 200) && (ps->send_connected==GNUNET_YES))
1215 ps->send_connected = GNUNET_NO;
1216 ps->send_active = GNUNET_NO;
1217 #if DEBUG_CONNECTIONS
1218 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: sending disconnected\n",ps);
1224 if ((size * nmemb) < SIZE_MAX)
1225 tmp = GNUNET_malloc (len+1);
1227 if ((tmp != NULL) && (len > 0))
1229 memcpy(tmp,ptr,len);
1232 if (tmp[len-2] == 13)
1239 return size * nmemb;
1243 * Callback method used with libcurl
1244 * Method is called when libcurl needs to read data during sending
1245 * @param stream pointer where to write data
1246 * @param size size of an individual element
1247 * @param nmemb count of elements that can be written to the buffer
1248 * @param ptr source pointer, passed to the libcurl handle
1249 * @return bytes written to stream
1251 static size_t curl_send_cb(void *stream, size_t size, size_t nmemb, void *ptr)
1253 struct Session * ps = ptr;
1254 struct HTTP_Message * msg = ps->pending_msgs_tail;
1258 if (ps->send_active == GNUNET_NO)
1259 return CURL_READFUNC_PAUSE;
1261 if ((ps->pending_msgs_tail == NULL) && (ps->send_active == GNUNET_YES))
1263 #if DEBUG_CONNECTIONS
1264 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: No Message to send, pausing connection\n",ps);
1266 ps->send_active = GNUNET_NO;
1267 return CURL_READFUNC_PAUSE;
1270 GNUNET_assert (msg!=NULL);
1273 if (msg->pos < msg->size)
1275 /* data fit in buffer */
1276 if ((msg->size - msg->pos) <= (size * nmemb))
1278 len = (msg->size - msg->pos);
1279 memcpy(stream, &msg->buf[msg->pos], len);
1286 memcpy(stream, &msg->buf[msg->pos], len);
1291 /* no data to send */
1297 if ( msg->pos == msg->size)
1299 #if DEBUG_CONNECTIONS
1300 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: Message with %u bytes sent, removing message from queue \n",ps, msg->pos);
1302 /* Calling transmit continuation */
1303 if (( NULL != ps->pending_msgs_tail) && (NULL != ps->pending_msgs_tail->transmit_cont))
1304 msg->transmit_cont (ps->pending_msgs_tail->transmit_cont_cls,&(ps->peercontext)->identity,GNUNET_OK);
1305 remove_http_message(ps, msg);
1310 static void curl_receive_mst_cb (void *cls,
1312 const struct GNUNET_MessageHeader *message)
1314 struct Session *ps = cls;
1315 GNUNET_assert(ps != NULL);
1317 struct HTTP_PeerContext *pc = ps->peercontext;
1318 GNUNET_assert(pc != NULL);
1321 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1322 "Connection %X: Forwarding message to transport service, type %u and size %u from `%s' (`%s')\n",
1324 ntohs(message->type),
1325 ntohs(message->size),
1326 GNUNET_i2s(&(pc->identity)),http_plugin_address_to_string(NULL,ps->addr,ps->addrlen));
1328 pc->plugin->env->receive (pc->plugin->env->cls,
1337 * Callback method used with libcurl
1338 * Method is called when libcurl needs to write data during sending
1339 * @param stream pointer where to write data
1340 * @param size size of an individual element
1341 * @param nmemb count of elements that can be written to the buffer
1342 * @param ptr destination pointer, passed to the libcurl handle
1343 * @return bytes read from stream
1345 static size_t curl_receive_cb( void *stream, size_t size, size_t nmemb, void *ptr)
1347 struct Session * ps = ptr;
1348 #if DEBUG_CONNECTIONS
1349 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: %u bytes received\n",ps, size*nmemb);
1351 GNUNET_SERVER_mst_receive(ps->msgtok, ps, stream, size*nmemb, GNUNET_NO, GNUNET_NO);
1352 return (size * nmemb);
1356 static void curl_perform (void *cls,
1357 const struct GNUNET_SCHEDULER_TaskContext *tc)
1359 struct Plugin *plugin = cls;
1360 static unsigned int handles_last_run;
1362 struct CURLMsg *msg;
1364 struct Session *ps = NULL;
1365 struct HTTP_PeerContext *pc = NULL;
1366 struct HTTP_Message * cur_msg = NULL;
1370 GNUNET_assert(cls !=NULL);
1372 plugin->http_curl_task = GNUNET_SCHEDULER_NO_TASK;
1373 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1379 mret = curl_multi_perform (plugin->multi_handle, &running);
1380 if ((running < handles_last_run) && (running>0))
1385 msg = curl_multi_info_read (plugin->multi_handle, &running);
1388 /* get session for affected curl handle */
1389 GNUNET_assert ( msg->easy_handle != NULL );
1390 curl_easy_getinfo(msg->easy_handle, CURLINFO_PRIVATE, &tmp);
1391 ps = (struct Session *) tmp;
1392 GNUNET_assert ( ps != NULL );
1393 pc = ps->peercontext;
1394 GNUNET_assert ( pc != NULL );
1399 if ( (msg->data.result != CURLE_OK) &&
1400 (msg->data.result != CURLE_GOT_NOTHING) )
1402 /* sending msg failed*/
1403 if (msg->easy_handle == ps->send_endpoint)
1405 #if DEBUG_CONNECTIONS
1406 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1407 _("Connection %X: HTTPS PUT to peer `%s' (`%s') failed: `%s' `%s'\n"),
1409 GNUNET_i2s(&pc->identity),
1410 http_plugin_address_to_string(NULL, ps->addr, ps->addrlen),
1411 "curl_multi_perform",
1412 curl_easy_strerror (msg->data.result));
1414 ps->send_connected = GNUNET_NO;
1415 ps->send_active = GNUNET_NO;
1416 curl_multi_remove_handle(plugin->multi_handle,ps->send_endpoint);
1417 //curl_easy_cleanup(ps->send_endpoint);
1418 //ps->send_endpoint=NULL;
1419 cur_msg = ps->pending_msgs_tail;
1420 if (( NULL != cur_msg) && ( NULL != cur_msg->transmit_cont))
1421 cur_msg->transmit_cont (cur_msg->transmit_cont_cls,&pc->identity,GNUNET_SYSERR);
1423 /* GET connection failed */
1424 if (msg->easy_handle == ps->recv_endpoint)
1426 #if DEBUG_CONNECTIONS
1427 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1428 _("Connection %X: HTTPS GET to peer `%s' (`%s') failed: `%s' `%s'\n"),
1430 GNUNET_i2s(&pc->identity),
1431 http_plugin_address_to_string(NULL, ps->addr, ps->addrlen),
1432 "curl_multi_perform",
1433 curl_easy_strerror (msg->data.result));
1435 ps->recv_connected = GNUNET_NO;
1436 ps->recv_active = GNUNET_NO;
1437 curl_multi_remove_handle(plugin->multi_handle,ps->recv_endpoint);
1438 //curl_easy_cleanup(ps->recv_endpoint);
1439 //ps->recv_endpoint=NULL;
1444 if (msg->easy_handle == ps->send_endpoint)
1446 GNUNET_assert (CURLE_OK == curl_easy_getinfo(msg->easy_handle, CURLINFO_RESPONSE_CODE, &http_result));
1447 #if DEBUG_CONNECTIONS
1448 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1449 "Connection %X: HTTPS PUT connection to peer `%s' (`%s') was closed with HTTP code %u\n",
1451 GNUNET_i2s(&pc->identity),
1452 http_plugin_address_to_string(NULL, ps->addr, ps->addrlen),
1455 /* Calling transmit continuation */
1456 cur_msg = ps->pending_msgs_tail;
1457 if (( NULL != cur_msg) && (NULL != cur_msg->transmit_cont))
1459 /* HTTP 1xx : Last message before here was informational */
1460 if ((http_result >=100) && (http_result < 200))
1461 cur_msg->transmit_cont (cur_msg->transmit_cont_cls,&pc->identity,GNUNET_OK);
1462 /* HTTP 2xx: successful operations */
1463 if ((http_result >=200) && (http_result < 300))
1464 cur_msg->transmit_cont (cur_msg->transmit_cont_cls,&pc->identity,GNUNET_OK);
1465 /* HTTP 3xx..5xx: error */
1466 if ((http_result >=300) && (http_result < 600))
1467 cur_msg->transmit_cont (cur_msg->transmit_cont_cls,&pc->identity,GNUNET_SYSERR);
1469 ps->send_connected = GNUNET_NO;
1470 ps->send_active = GNUNET_NO;
1471 curl_multi_remove_handle(plugin->multi_handle,ps->send_endpoint);
1472 //curl_easy_cleanup(ps->send_endpoint);
1473 //ps->send_endpoint =NULL;
1475 if (msg->easy_handle == ps->recv_endpoint)
1477 #if DEBUG_CONNECTIONS
1478 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1479 "Connection %X: HTTP GET connection to peer `%s' (`%s') was closed with HTTP code %u\n",
1481 GNUNET_i2s(&pc->identity),
1482 http_plugin_address_to_string(NULL, ps->addr, ps->addrlen),
1485 ps->recv_connected = GNUNET_NO;
1486 ps->recv_active = GNUNET_NO;
1487 curl_multi_remove_handle(plugin->multi_handle,ps->recv_endpoint);
1488 //curl_easy_cleanup(ps->recv_endpoint);
1489 //ps->recv_endpoint=NULL;
1492 if ((ps->recv_connected == GNUNET_NO) && (ps->send_connected == GNUNET_NO))
1493 remove_session (pc, ps, GNUNET_YES, GNUNET_SYSERR);
1500 while ( (running > 0) );
1502 handles_last_run = running;
1504 while (mret == CURLM_CALL_MULTI_PERFORM);
1505 curl_schedule(plugin);
1510 * Function setting up file descriptors and scheduling task to run
1511 * @param ses session to send data to
1512 * @return GNUNET_SYSERR for hard failure, GNUNET_OK for ok
1515 http_plugin_disconnect (void *cls,
1516 const struct GNUNET_PeerIdentity *target)
1520 struct Plugin *plugin = cls;
1521 struct HTTP_PeerContext *pc = NULL;
1522 struct Session *ps = NULL;
1523 //struct Session *tmp = NULL;
1525 pc = GNUNET_CONTAINER_multihashmap_get (plugin->peers, &target->hashPubKey);
1532 /* Telling transport that session is getting disconnected */
1533 plugin->env->session_end(plugin, target, ps);
1534 if (ps->direction==OUTBOUND)
1536 if (ps->send_endpoint!=NULL)
1538 //GNUNET_assert(CURLM_OK == curl_multi_remove_handle(plugin->multi_handle,ps->send_endpoint));
1539 //curl_easy_cleanup(ps->send_endpoint);
1540 //ps->send_endpoint=NULL;
1541 ps->send_force_disconnect = GNUNET_YES;
1543 if (ps->recv_endpoint!=NULL)
1545 //GNUNET_assert(CURLM_OK == curl_multi_remove_handle(plugin->multi_handle,ps->recv_endpoint));
1546 //curl_easy_cleanup(ps->recv_endpoint);
1547 //ps->recv_endpoint=NULL;
1548 ps->recv_force_disconnect = GNUNET_YES;
1552 if (ps->direction==INBOUND)
1554 ps->recv_force_disconnect = GNUNET_YES;
1555 ps->send_force_disconnect = GNUNET_YES;
1558 while (ps->pending_msgs_head!=NULL)
1560 remove_http_message(ps, ps->pending_msgs_head);
1562 ps->recv_active = GNUNET_NO;
1563 ps->send_active = GNUNET_NO;
1569 static int curl_schedule(void *cls)
1571 struct Plugin *plugin = cls;
1576 struct GNUNET_NETWORK_FDSet *grs;
1577 struct GNUNET_NETWORK_FDSet *gws;
1581 GNUNET_assert(cls !=NULL);
1583 /* Cancel previous scheduled task */
1584 if (plugin->http_curl_task != GNUNET_SCHEDULER_NO_TASK)
1586 GNUNET_SCHEDULER_cancel(plugin->env->sched, plugin->http_curl_task);
1587 plugin->http_curl_task = GNUNET_SCHEDULER_NO_TASK;
1593 mret = curl_multi_fdset (plugin->multi_handle, &rs, &ws, &es, &max);
1594 if (mret != CURLM_OK)
1596 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1597 _("%s failed at %s:%d: `%s'\n"),
1598 "curl_multi_fdset", __FILE__, __LINE__,
1599 curl_multi_strerror (mret));
1600 return GNUNET_SYSERR;
1602 mret = curl_multi_timeout (plugin->multi_handle, &to);
1603 if (mret != CURLM_OK)
1605 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1606 _("%s failed at %s:%d: `%s'\n"),
1607 "curl_multi_timeout", __FILE__, __LINE__,
1608 curl_multi_strerror (mret));
1609 return GNUNET_SYSERR;
1612 grs = GNUNET_NETWORK_fdset_create ();
1613 gws = GNUNET_NETWORK_fdset_create ();
1614 GNUNET_NETWORK_fdset_copy_native (grs, &rs, max + 1);
1615 GNUNET_NETWORK_fdset_copy_native (gws, &ws, max + 1);
1616 plugin->http_curl_task = GNUNET_SCHEDULER_add_select (plugin->env->sched,
1617 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1618 GNUNET_SCHEDULER_NO_TASK,
1619 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 0),
1624 GNUNET_NETWORK_fdset_destroy (gws);
1625 GNUNET_NETWORK_fdset_destroy (grs);
1630 * Function setting up curl handle and selecting message to send
1632 * @param ses session to send data to
1633 * @param con connection
1634 * @return GNUNET_SYSERR on failure, GNUNET_NO if connecting, GNUNET_YES if ok
1636 static ssize_t send_check_connections (void *cls, struct Session *ps)
1638 struct Plugin *plugin = cls;
1640 struct HTTP_Message * msg;
1642 struct GNUNET_TIME_Relative timeout = GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT;
1644 GNUNET_assert(cls !=NULL);
1646 if (ps->direction == OUTBOUND)
1648 /* RECV DIRECTION */
1649 /* Check if session is connected to receive data, otherwise connect to peer */
1650 if (ps->recv_connected == GNUNET_NO)
1652 int fresh = GNUNET_NO;
1653 if (ps->recv_endpoint == NULL)
1656 ps->recv_endpoint = curl_easy_init();
1659 curl_easy_setopt(ps->recv_endpoint, CURLOPT_VERBOSE, 1L);
1661 curl_easy_setopt(ps->recv_endpoint, CURLOPT_URL, ps->url);
1662 curl_easy_setopt (ps->recv_endpoint, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1);
1663 //curl_easy_setopt (ps->recv_endpoint, CURLOPT_SSL_CIPHER_LIST, cipher_suite);
1664 curl_easy_setopt(ps->recv_endpoint, CURLOPT_SSL_VERIFYPEER, 0);
1665 curl_easy_setopt(ps->recv_endpoint, CURLOPT_SSL_VERIFYHOST, 0);
1666 curl_easy_setopt(ps->recv_endpoint, CURLOPT_HEADERFUNCTION, &curl_get_header_cb);
1667 curl_easy_setopt(ps->recv_endpoint, CURLOPT_WRITEHEADER, ps);
1668 curl_easy_setopt(ps->recv_endpoint, CURLOPT_READFUNCTION, curl_send_cb);
1669 curl_easy_setopt(ps->recv_endpoint, CURLOPT_READDATA, ps);
1670 curl_easy_setopt(ps->recv_endpoint, CURLOPT_WRITEFUNCTION, curl_receive_cb);
1671 curl_easy_setopt(ps->recv_endpoint, CURLOPT_WRITEDATA, ps);
1672 curl_easy_setopt(ps->recv_endpoint, CURLOPT_TIMEOUT, (long) timeout.value);
1673 curl_easy_setopt(ps->recv_endpoint, CURLOPT_PRIVATE, ps);
1674 curl_easy_setopt(ps->recv_endpoint, CURLOPT_CONNECTTIMEOUT, HTTP_CONNECT_TIMEOUT);
1675 curl_easy_setopt(ps->recv_endpoint, CURLOPT_BUFFERSIZE, GNUNET_SERVER_MAX_MESSAGE_SIZE);
1677 if (fresh==GNUNET_YES)
1679 mret = curl_multi_add_handle(plugin->multi_handle, ps->recv_endpoint);
1680 if (mret != CURLM_OK)
1682 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1683 _("Connection: %X: %s failed at %s:%d: `%s'\n"),
1685 "curl_multi_add_handle", __FILE__, __LINE__,
1686 curl_multi_strerror (mret));
1687 return GNUNET_SYSERR;
1690 if (curl_schedule (plugin) == GNUNET_SYSERR)
1692 #if DEBUG_CONNECTIONS
1693 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: could not schedule curl task\n",ps);
1695 return GNUNET_SYSERR;
1697 #if DEBUG_CONNECTIONS
1698 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: inbound not connected, initiating connection\n",ps);
1702 /* waiting for receive direction */
1703 if (ps->recv_connected==GNUNET_NO)
1706 /* SEND DIRECTION */
1707 /* Check if session is connected to send data, otherwise connect to peer */
1708 if ((ps->send_connected == GNUNET_YES) && (ps->send_endpoint!= NULL))
1710 if (ps->send_active == GNUNET_YES)
1712 #if DEBUG_CONNECTIONS
1713 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: outbound active, enqueueing message\n",ps);
1717 if (ps->send_active == GNUNET_NO)
1719 #if DEBUG_CONNECTIONS
1720 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: outbound paused, unpausing existing connection and enqueueing message\n",ps);
1722 if (CURLE_OK == curl_easy_pause(ps->send_endpoint,CURLPAUSE_CONT))
1724 ps->send_active=GNUNET_YES;
1728 return GNUNET_SYSERR;
1731 /* not connected, initiate connection */
1732 if (ps->send_connected==GNUNET_NO)
1734 int fresh = GNUNET_NO;
1735 if (NULL == ps->send_endpoint)
1737 ps->send_endpoint = curl_easy_init();
1740 GNUNET_assert (ps->send_endpoint != NULL);
1741 GNUNET_assert (NULL != ps->pending_msgs_tail);
1742 #if DEBUG_CONNECTIONS
1743 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: outbound not connected, initiating connection\n",ps);
1745 ps->send_active = GNUNET_NO;
1746 msg = ps->pending_msgs_tail;
1749 curl_easy_setopt(ps->send_endpoint, CURLOPT_VERBOSE, 1L);
1751 curl_easy_setopt(ps->send_endpoint, CURLOPT_URL, ps->url);
1752 curl_easy_setopt(ps->send_endpoint, CURLOPT_PUT, 1L);
1753 curl_easy_setopt(ps->send_endpoint, CURLOPT_HEADERFUNCTION, &curl_put_header_cb);
1754 curl_easy_setopt(ps->send_endpoint, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1);
1755 curl_easy_setopt(ps->send_endpoint, CURLOPT_SSL_VERIFYPEER, 0);
1756 curl_easy_setopt(ps->send_endpoint, CURLOPT_SSL_VERIFYHOST, 0);
1758 curl_easy_setopt(ps->send_endpoint, CURLOPT_WRITEHEADER, ps);
1759 curl_easy_setopt(ps->send_endpoint, CURLOPT_READFUNCTION, curl_send_cb);
1760 curl_easy_setopt(ps->send_endpoint, CURLOPT_READDATA, ps);
1761 curl_easy_setopt(ps->send_endpoint, CURLOPT_WRITEFUNCTION, curl_receive_cb);
1762 curl_easy_setopt(ps->send_endpoint, CURLOPT_READDATA, ps);
1763 curl_easy_setopt(ps->send_endpoint, CURLOPT_TIMEOUT, (long) timeout.value);
1764 curl_easy_setopt(ps->send_endpoint, CURLOPT_PRIVATE, ps);
1765 curl_easy_setopt(ps->send_endpoint, CURLOPT_CONNECTTIMEOUT, HTTP_CONNECT_TIMEOUT);
1766 curl_easy_setopt(ps->send_endpoint, CURLOPT_BUFFERSIZE, GNUNET_SERVER_MAX_MESSAGE_SIZE);
1768 if (fresh==GNUNET_YES)
1770 mret = curl_multi_add_handle(plugin->multi_handle, ps->send_endpoint);
1771 if (mret != CURLM_OK)
1773 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1774 _("Connection: %X: %s failed at %s:%d: `%s'\n"),
1776 "curl_multi_add_handle", __FILE__, __LINE__,
1777 curl_multi_strerror (mret));
1778 return GNUNET_SYSERR;
1782 if (curl_schedule (plugin) == GNUNET_SYSERR)
1783 return GNUNET_SYSERR;
1786 if (ps->direction == INBOUND)
1788 GNUNET_assert (NULL != ps->pending_msgs_tail);
1789 if ((ps->recv_connected==GNUNET_YES) && (ps->send_connected==GNUNET_YES) &&
1790 (ps->recv_force_disconnect==GNUNET_NO) && (ps->recv_force_disconnect==GNUNET_NO))
1793 return GNUNET_SYSERR;
1796 static struct Session * send_select_session (void * cls, struct HTTP_PeerContext *pc, const void * addr, size_t addrlen, int force_address, struct Session * session)
1798 struct Session * tmp = NULL;
1799 int addr_given = GNUNET_NO;
1801 if ((addr!=NULL) && (addrlen>0))
1802 addr_given = GNUNET_YES;
1804 if (force_address == GNUNET_YES)
1806 /* check session given as argument */
1807 if ((session != NULL) && (addr_given == GNUNET_YES))
1809 if (0 == memcmp(session->addr, addr, addrlen))
1811 /* connection can not be used, since it is disconnected */
1812 if ((session->recv_force_disconnect==GNUNET_NO) && (session->send_force_disconnect==GNUNET_NO))
1814 #if DEBUG_SESSION_SELECTION
1815 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Session %X selected: Using session passed by transport to send to forced address \n", session);
1821 /* check last session used */
1822 if ((pc->last_session != NULL)&& (addr_given == GNUNET_YES))
1824 if (0 == memcmp(pc->last_session->addr, addr, addrlen))
1826 /* connection can not be used, since it is disconnected */
1827 if ((pc->last_session->recv_force_disconnect==GNUNET_NO) && (pc->last_session->send_force_disconnect==GNUNET_NO))
1829 #if DEBUG_SESSION_SELECTION
1830 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Session %X selected: Using last session used to send to forced address \n", pc->last_session);
1832 return pc->last_session;
1836 /* find session in existing sessions */
1838 while ((tmp!=NULL) && (addr_given == GNUNET_YES))
1841 if (0 == memcmp(tmp->addr, addr, addrlen))
1843 /* connection can not be used, since it is disconnected */
1844 if ((tmp->recv_force_disconnect==GNUNET_NO) && (tmp->send_force_disconnect==GNUNET_NO))
1846 #if DEBUG_SESSION_SELECTION
1847 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Session %X selected: Using existing session to send to forced address \n", session);
1855 /* no session to use */
1858 if ((force_address == GNUNET_NO) || (force_address == GNUNET_SYSERR))
1860 /* check session given as argument */
1861 if (session != NULL)
1863 /* connection can not be used, since it is disconnected */
1864 if ((session->recv_force_disconnect==GNUNET_NO) && (session->send_force_disconnect==GNUNET_NO))
1866 #if DEBUG_SESSION_SELECTION
1867 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Session %X selected: Using session passed by transport to send not-forced address \n", session);
1873 /* check last session used */
1874 if (pc->last_session != NULL)
1876 /* connection can not be used, since it is disconnected */
1877 if ((pc->last_session->recv_force_disconnect==GNUNET_NO) && (pc->last_session->send_force_disconnect==GNUNET_NO))
1879 #if DEBUG_SESSION_SELECTION
1880 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Session %X selected: Using last session to send to not-forced address \n", pc->last_session);
1882 return pc->last_session;
1885 /* find session in existing sessions */
1889 /* connection can not be used, since it is disconnected */
1890 if ((tmp->recv_force_disconnect==GNUNET_NO) && (tmp->send_force_disconnect==GNUNET_NO))
1892 #if DEBUG_SESSION_SELECTION
1893 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Session %X selected: Using existing session to send to not-forced address \n", tmp);
1905 * Function that can be used by the transport service to transmit
1906 * a message using the plugin. Note that in the case of a
1907 * peer disconnecting, the continuation MUST be called
1908 * prior to the disconnect notification itself. This function
1909 * will be called with this peer's HELLO message to initiate
1910 * a fresh connection to another peer.
1912 * @param cls closure
1913 * @param target who should receive this message
1914 * @param msgbuf the message to transmit
1915 * @param msgbuf_size number of bytes in 'msgbuf'
1916 * @param priority how important is the message (most plugins will
1917 * ignore message priority and just FIFO)
1918 * @param timeout how long to wait at most for the transmission (does not
1919 * require plugins to discard the message after the timeout,
1920 * just advisory for the desired delay; most plugins will ignore
1922 * @param session which session must be used (or NULL for "any")
1923 * @param addr the address to use (can be NULL if the plugin
1924 * is "on its own" (i.e. re-use existing TCP connection))
1925 * @param addrlen length of the address in bytes
1926 * @param force_address GNUNET_YES if the plugin MUST use the given address,
1927 * GNUNET_NO means the plugin may use any other address and
1928 * GNUNET_SYSERR means that only reliable existing
1929 * bi-directional connections should be used (regardless
1931 * @param cont continuation to call once the message has
1932 * been transmitted (or if the transport is ready
1933 * for the next transmission call; or if the
1934 * peer disconnected...); can be NULL
1935 * @param cont_cls closure for cont
1936 * @return number of bytes used (on the physical network, with overheads);
1937 * -1 on hard errors (i.e. address invalid); 0 is a legal value
1938 * and does NOT mean that the message was not transmitted (DV)
1941 http_plugin_send (void *cls,
1942 const struct GNUNET_PeerIdentity *target,
1945 unsigned int priority,
1946 struct GNUNET_TIME_Relative to,
1947 struct Session *session,
1951 GNUNET_TRANSPORT_TransmitContinuation cont,
1954 struct Plugin *plugin = cls;
1955 struct HTTP_Message *msg;
1956 struct HTTP_PeerContext * pc;
1957 struct Session * ps = NULL;
1959 GNUNET_assert(cls !=NULL);
1962 char * force = GNUNET_malloc(40);
1963 if (force_address == GNUNET_YES)
1964 strcpy(force,"forced addr.");
1965 if (force_address == GNUNET_NO)
1966 strcpy(force,"any addr.");
1967 if (force_address == GNUNET_SYSERR)
1968 strcpy(force,"reliable bi-direc. address addr.");
1970 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Transport tells me to send %u bytes to `%s' using %s (%s) and session: %X\n",
1974 http_plugin_address_to_string(NULL, addr, addrlen),
1980 pc = GNUNET_CONTAINER_multihashmap_get (plugin->peers, &target->hashPubKey);
1984 pc = GNUNET_malloc(sizeof (struct HTTP_PeerContext));
1985 pc->plugin = plugin;
1986 pc->session_id_counter=1;
1987 pc->last_session = NULL;
1988 memcpy(&pc->identity, target, sizeof(struct GNUNET_PeerIdentity));
1989 GNUNET_CONTAINER_multihashmap_put(plugin->peers, &pc->identity.hashPubKey, pc, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
1990 GNUNET_STATISTICS_update (plugin->env->stats,
1991 gettext_noop ("# HTTP peers active"),
1996 ps = send_select_session (plugin, pc, addr, addrlen, force_address, session);
1998 /* session not existing, but address forced -> creating new session */
2001 if ((addr!=NULL) && (addrlen!=0))
2003 ps = GNUNET_malloc(sizeof (struct Session));
2004 #if DEBUG_SESSION_SELECTION
2005 if (force_address == GNUNET_YES)
2006 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"No existing connection & forced address: creating new session %X to peer %s\n", ps, GNUNET_i2s(target));
2007 if (force_address != GNUNET_YES)
2008 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"No existing connection: creating new session %X to peer %s\n", ps, GNUNET_i2s(target));
2010 if ((addrlen!=0) && (addr!=NULL))
2012 ps->addr = GNUNET_malloc(addrlen);
2013 memcpy(ps->addr,addr,addrlen);
2014 ps->addrlen = addrlen;
2021 ps->direction=OUTBOUND;
2022 ps->recv_connected = GNUNET_NO;
2023 ps->recv_force_disconnect = GNUNET_NO;
2024 ps->send_connected = GNUNET_NO;
2025 ps->send_force_disconnect = GNUNET_NO;
2026 ps->pending_msgs_head = NULL;
2027 ps->pending_msgs_tail = NULL;
2029 ps->session_id = pc->session_id_counter;
2030 pc->session_id_counter++;
2031 ps->url = create_url (plugin, ps->addr, ps->addrlen, ps->session_id);
2032 if (ps->msgtok == NULL)
2033 ps->msgtok = GNUNET_SERVER_mst_create (&curl_receive_mst_cb, ps);
2034 GNUNET_CONTAINER_DLL_insert(pc->head,pc->tail,ps);
2037 GNUNET_STATISTICS_update (plugin->env->stats,
2038 gettext_noop ("# HTTP outbound sessions for peers active"),
2045 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));
2047 return GNUNET_SYSERR;
2052 msg = GNUNET_malloc (sizeof (struct HTTP_Message) + msgbuf_size);
2054 msg->size = msgbuf_size;
2056 msg->buf = (char *) &msg[1];
2057 msg->transmit_cont = cont;
2058 msg->transmit_cont_cls = cont_cls;
2059 memcpy (msg->buf,msgbuf, msgbuf_size);
2060 GNUNET_CONTAINER_DLL_insert(ps->pending_msgs_head,ps->pending_msgs_tail,msg);
2062 if (send_check_connections (plugin, ps) != GNUNET_SYSERR)
2064 if (force_address != GNUNET_YES)
2065 pc->last_session = ps;
2067 if (pc->last_session==NULL)
2068 pc->last_session = ps;
2072 return GNUNET_SYSERR;
2078 * Function that can be used to force the plugin to disconnect
2079 * from the given peer and cancel all previous transmissions
2080 * (and their continuationc).
2082 * @param cls closure
2083 * @param target peer from which to disconnect
2086 * Convert the transports address to a nice, human-readable
2089 * @param cls closure
2090 * @param type name of the transport that generated the address
2091 * @param addr one of the addresses of the host, NULL for the last address
2092 * the specific address format depends on the transport
2093 * @param addrlen length of the address
2094 * @param numeric should (IP) addresses be displayed in numeric form?
2095 * @param timeout after how long should we give up?
2096 * @param asc function to call on each string
2097 * @param asc_cls closure for asc
2100 http_plugin_address_pretty_printer (void *cls,
2105 struct GNUNET_TIME_Relative timeout,
2106 GNUNET_TRANSPORT_AddressStringCallback
2109 const struct IPv4HttpAddress *t4;
2110 const struct IPv6HttpAddress *t6;
2111 struct sockaddr_in a4;
2112 struct sockaddr_in6 a6;
2118 GNUNET_assert(cls !=NULL);
2119 if (addrlen == sizeof (struct IPv6HttpAddress))
2121 address = GNUNET_malloc (INET6_ADDRSTRLEN);
2123 a6.sin6_addr = t6->ipv6_addr;
2124 inet_ntop(AF_INET6, &(a6.sin6_addr),address,INET6_ADDRSTRLEN);
2125 port = ntohs(t6->u6_port);
2127 else if (addrlen == sizeof (struct IPv4HttpAddress))
2129 address = GNUNET_malloc (INET_ADDRSTRLEN);
2131 a4.sin_addr.s_addr = t4->ipv4_addr;
2132 inet_ntop(AF_INET, &(a4.sin_addr),address,INET_ADDRSTRLEN);
2133 port = ntohs(t4->u_port);
2137 /* invalid address */
2138 GNUNET_break_op (0);
2139 asc (asc_cls, NULL);
2142 res = GNUNET_asprintf(&ret,"%s://%s:%u/", PROTOCOL_PREFIX, address,port);
2143 GNUNET_free (address);
2144 GNUNET_assert(res != 0);
2146 GNUNET_free_non_null (ret);
2152 * Another peer has suggested an address for this
2153 * peer and transport plugin. Check that this could be a valid
2154 * address. If so, consider adding it to the list
2157 * @param cls closure
2158 * @param addr pointer to the address
2159 * @param addrlen length of addr
2160 * @return GNUNET_OK if this is a plausible address for this peer
2164 http_plugin_address_suggested (void *cls,
2165 const void *addr, size_t addrlen)
2167 struct Plugin *plugin = cls;
2168 struct IPv4HttpAddress *v4;
2169 struct IPv6HttpAddress *v6;
2172 GNUNET_assert(cls !=NULL);
2173 if ((addrlen != sizeof (struct IPv4HttpAddress)) &&
2174 (addrlen != sizeof (struct IPv6HttpAddress)))
2176 return GNUNET_SYSERR;
2178 if (addrlen == sizeof (struct IPv4HttpAddress))
2180 v4 = (struct IPv4HttpAddress *) addr;
2181 /* Not skipping loopback
2182 if (INADDR_LOOPBACK == ntohl(v4->ipv4_addr))
2184 return GNUNET_SYSERR;
2186 port = ntohs (v4->u_port);
2187 if (port != plugin->port_inbound)
2189 return GNUNET_SYSERR;
2192 if (addrlen == sizeof (struct IPv6HttpAddress))
2194 v6 = (struct IPv6HttpAddress *) addr;
2195 if (IN6_IS_ADDR_LINKLOCAL (&v6->ipv6_addr))
2197 return GNUNET_SYSERR;
2199 port = ntohs (v6->u6_port);
2200 if (port != plugin->port_inbound)
2202 return GNUNET_SYSERR;
2211 * Function called for a quick conversion of the binary address to
2212 * a numeric address. Note that the caller must not free the
2213 * address and that the next call to this function is allowed
2214 * to override the address again.
2216 * @param cls closure
2217 * @param addr binary address
2218 * @param addrlen length of the address
2219 * @return string representing the same address
2222 http_plugin_address_to_string (void *cls,
2226 const struct IPv4HttpAddress *t4;
2227 const struct IPv6HttpAddress *t6;
2228 struct sockaddr_in a4;
2229 struct sockaddr_in6 a6;
2235 if (addrlen == sizeof (struct IPv6HttpAddress))
2237 address = GNUNET_malloc (INET6_ADDRSTRLEN);
2239 a6.sin6_addr = t6->ipv6_addr;
2240 inet_ntop(AF_INET6, &(a6.sin6_addr),address,INET6_ADDRSTRLEN);
2241 port = ntohs(t6->u6_port);
2243 else if (addrlen == sizeof (struct IPv4HttpAddress))
2245 address = GNUNET_malloc (INET_ADDRSTRLEN);
2247 a4.sin_addr.s_addr = t4->ipv4_addr;
2248 inet_ntop(AF_INET, &(a4.sin_addr),address,INET_ADDRSTRLEN);
2249 port = ntohs(t4->u_port);
2253 /* invalid address */
2256 res = GNUNET_asprintf(&ret,"%s:%u",address,port);
2257 GNUNET_free (address);
2258 GNUNET_assert(res != 0);
2263 load_certificate( const char * file )
2265 struct GNUNET_DISK_FileHandle * gn_file;
2270 if (0!=STAT(file, &fstat))
2272 text = GNUNET_malloc (fstat.st_size);
2273 gn_file = GNUNET_DISK_file_open(file,GNUNET_DISK_OPEN_READ, GNUNET_DISK_PERM_USER_READ);
2279 if (GNUNET_SYSERR == GNUNET_DISK_file_read(gn_file, text, fstat.st_size))
2284 text[fstat.st_size] = '\0';
2285 GNUNET_DISK_file_close(gn_file);
2292 * Exit point from the plugin.
2295 libgnunet_plugin_transport_https_done (void *cls)
2297 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
2298 struct Plugin *plugin = api->cls;
2300 GNUNET_assert(cls !=NULL);
2302 if (plugin->http_server_daemon_v4 != NULL)
2304 MHD_stop_daemon (plugin->http_server_daemon_v4);
2305 plugin->http_server_daemon_v4 = NULL;
2307 if (plugin->http_server_daemon_v6 != NULL)
2309 MHD_stop_daemon (plugin->http_server_daemon_v6);
2310 plugin->http_server_daemon_v6 = NULL;
2313 if ( plugin->http_server_task_v4 != GNUNET_SCHEDULER_NO_TASK)
2315 GNUNET_SCHEDULER_cancel(plugin->env->sched, plugin->http_server_task_v4);
2316 plugin->http_server_task_v4 = GNUNET_SCHEDULER_NO_TASK;
2319 if ( plugin->http_server_task_v6 != GNUNET_SCHEDULER_NO_TASK)
2321 GNUNET_SCHEDULER_cancel(plugin->env->sched, plugin->http_server_task_v6);
2322 plugin->http_server_task_v6 = GNUNET_SCHEDULER_NO_TASK;
2326 /* free all peer information */
2327 if (plugin->peers!=NULL)
2329 GNUNET_CONTAINER_multihashmap_iterate (plugin->peers,
2330 &remove_peer_context_Iterator,
2332 GNUNET_CONTAINER_multihashmap_destroy (plugin->peers);
2334 if (plugin->multi_handle!=NULL)
2336 mret = curl_multi_cleanup(plugin->multi_handle);
2338 if ( CURLM_OK != mret)
2339 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"curl multihandle clean up failed\n");
2341 plugin->multi_handle = NULL;
2343 curl_global_cleanup();
2345 if ( plugin->http_curl_task != GNUNET_SCHEDULER_NO_TASK)
2347 GNUNET_SCHEDULER_cancel(plugin->env->sched, plugin->http_curl_task);
2348 plugin->http_curl_task = GNUNET_SCHEDULER_NO_TASK;
2351 GNUNET_free_non_null (plugin->bind4_address);
2352 GNUNET_free_non_null (plugin->bind6_address);
2353 GNUNET_free_non_null (plugin->bind_hostname);
2354 GNUNET_free_non_null (plugin->crypto_init);
2355 GNUNET_free (plugin);
2358 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Unload http plugin complete...\n");
2365 * Entry point for the plugin.
2368 libgnunet_plugin_transport_https_init (void *cls)
2370 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
2371 struct Plugin *plugin;
2372 struct GNUNET_TRANSPORT_PluginFunctions *api;
2373 struct GNUNET_TIME_Relative gn_timeout;
2374 long long unsigned int port;
2379 GNUNET_assert(cls !=NULL);
2381 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Starting https plugin...\n");
2384 plugin = GNUNET_malloc (sizeof (struct Plugin));
2385 plugin->stats = env->stats;
2387 plugin->peers = NULL;
2388 plugin->bind4_address = NULL;
2389 plugin->use_ipv6 = GNUNET_YES;
2390 plugin->use_ipv4 = GNUNET_YES;
2392 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
2394 api->send = &http_plugin_send;
2395 api->disconnect = &http_plugin_disconnect;
2396 api->address_pretty_printer = &http_plugin_address_pretty_printer;
2397 api->check_address = &http_plugin_address_suggested;
2398 api->address_to_string = &http_plugin_address_to_string;
2400 /* Hashing our identity to use it in URLs */
2401 GNUNET_CRYPTO_hash_to_enc ( &(plugin->env->my_identity->hashPubKey), &plugin->my_ascii_hash_ident);
2403 /* Use IPv6 yes/no */
2404 if (GNUNET_CONFIGURATION_have_value (env->cfg,
2405 "transport-https", "USE_IPv6"))
2407 plugin->use_ipv6 = GNUNET_CONFIGURATION_get_value_yesno (env->cfg,
2411 /* Use IPv4 yes/no */
2412 if (GNUNET_CONFIGURATION_have_value (env->cfg,
2413 "transport-https", "USE_IPv4"))
2415 plugin->use_ipv4 = GNUNET_CONFIGURATION_get_value_yesno (env->cfg,
2419 /* Reading port number from config file */
2421 GNUNET_CONFIGURATION_get_value_number (env->cfg,
2427 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
2429 _("Require valid port number for transport plugin `%s' in configuration!\n"),
2431 libgnunet_plugin_transport_https_done (api);
2435 /* Reading ipv4 addresse to bind to from config file */
2436 if ((plugin->use_ipv4==GNUNET_YES) && (GNUNET_CONFIGURATION_have_value (env->cfg,
2437 "transport-https", "BINDTO4")))
2439 GNUNET_break (GNUNET_OK ==
2440 GNUNET_CONFIGURATION_get_value_string (env->cfg,
2443 &plugin->bind_hostname));
2444 plugin->bind4_address = GNUNET_malloc(sizeof(struct sockaddr_in));
2445 plugin->bind4_address->sin_family = AF_INET;
2446 plugin->bind4_address->sin_port = htons (port);
2448 if (inet_pton(AF_INET,plugin->bind_hostname, &plugin->bind4_address->sin_addr)<=0)
2450 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
2452 _("Misconfigured address to bind to in configuration!\n"),
2454 GNUNET_free(plugin->bind4_address);
2455 GNUNET_free(plugin->bind_hostname);
2456 plugin->bind_hostname = NULL;
2457 plugin->bind4_address = NULL;
2461 /* Get crypto init string from config */
2462 if (GNUNET_CONFIGURATION_have_value (env->cfg,
2463 "transport-https", "CRYPTO_INIT"))
2465 GNUNET_CONFIGURATION_get_value_string (env->cfg,
2468 &plugin->crypto_init);
2472 GNUNET_asprintf(&plugin->crypto_init,"NORMAL");
2475 /* Get private key file from config */
2476 if (GNUNET_CONFIGURATION_have_value (env->cfg,
2477 "transport-https", "CERT_FILE"))
2479 GNUNET_CONFIGURATION_get_value_string (env->cfg,
2486 GNUNET_asprintf(&cert_file,"https.cert");
2489 /* Get private key file from config */
2490 if (GNUNET_CONFIGURATION_have_value (env->cfg,
2491 "transport-https", "KEY_FILE"))
2493 GNUNET_CONFIGURATION_get_value_string (env->cfg,
2500 GNUNET_asprintf(&key_file,"https.key");
2503 /* Get private key file from config */
2504 if (GNUNET_CONFIGURATION_have_value (env->cfg,
2505 "transport-https", "CERT_FILE"))
2507 GNUNET_CONFIGURATION_get_value_string (env->cfg,
2514 GNUNET_asprintf(&cert_file,"https.cert");
2517 /* Reading ipv4 addresse to bind to from config file */
2518 if ((plugin->use_ipv6==GNUNET_YES) && (GNUNET_CONFIGURATION_have_value (env->cfg,
2519 "transport-https", "BINDTO6")))
2521 GNUNET_break (GNUNET_OK ==
2522 GNUNET_CONFIGURATION_get_value_string (env->cfg,
2525 &plugin->bind_hostname));
2527 plugin->bind6_address = GNUNET_malloc(sizeof(struct sockaddr_in6));
2528 plugin->bind6_address->sin6_family = AF_INET6;
2529 plugin->bind6_address->sin6_port = htons (port);
2531 if (inet_pton(AF_INET6,plugin->bind_hostname, &plugin->bind6_address->sin6_addr)<=0)
2533 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
2535 _("Misconfigured address to bind to in configuration!\n"),
2537 GNUNET_free(plugin->bind6_address);
2538 GNUNET_free(plugin->bind_hostname);
2539 plugin->bind_hostname = NULL;
2540 plugin->bind6_address = NULL;
2544 /* read key & certificates from file */
2545 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Loading TLS certificate `%s' `%s'\n", key_file, cert_file);
2547 plugin->key = load_certificate( key_file );
2548 plugin->cert = load_certificate( cert_file );
2550 if ((plugin->key==NULL) || (plugin->cert==NULL))
2554 GNUNET_asprintf(&cmd,"gnunet-transport-certificate-creation %s %s", key_file, cert_file);
2555 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "No usable TLS certificate found, creating certificate \n");
2560 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
2562 _("Could not create a new TLS certificate, shell script `%s' failed!\n"),cmd,
2564 GNUNET_free (key_file);
2565 GNUNET_free (cert_file);
2566 libgnunet_plugin_transport_https_done(api);
2573 plugin->key = load_certificate( key_file );
2574 plugin->cert = load_certificate( cert_file );
2576 if ((plugin->key==NULL) || (plugin->cert==NULL))
2578 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
2580 _("No usable TLS certificate found and creating one failed! \n"),
2582 GNUNET_free (key_file);
2583 GNUNET_free (cert_file);
2584 libgnunet_plugin_transport_https_done(api);
2589 GNUNET_free (key_file);
2590 GNUNET_free (cert_file);
2593 GNUNET_assert((plugin->key!=NULL) && (plugin->cert!=NULL));
2594 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "TLS certificate loaded\n", key_file, cert_file);
2596 GNUNET_assert ((port > 0) && (port <= 65535));
2597 plugin->port_inbound = port;
2598 gn_timeout = GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT;
2599 unsigned int timeout = (gn_timeout.value) / 1000;
2600 if ((plugin->http_server_daemon_v6 == NULL) && (plugin->use_ipv6 == GNUNET_YES) && (port != 0))
2602 struct sockaddr * tmp = (struct sockaddr *) plugin->bind6_address;
2603 plugin->http_server_daemon_v6 = MHD_start_daemon (
2604 #if DEBUG_CONNECTIONS
2607 MHD_USE_IPv6 | MHD_USE_SSL,
2610 plugin , &mdh_access_cb, plugin,
2611 /*MHD_OPTION_HTTPS_PRIORITIES, "NORMAL:",*/
2612 /*MHD_OPTION_HTTPS_PRIORITIES, "PERFORMANCE:",*/
2613 /* MHD_OPTION_HTTPS_PRIORITIES, "NONE:+VERS-TLS1.0:+ARCFOUR-128:+SHA1:+RSA:+COMP-NULL", */
2614 /*MHD_OPTION_HTTPS_PRIORITIES, "NONE:+VERS-TLS1.0:+ARCFOUR-128:+MD5:+RSA:+COMP-NULL",*/
2615 MHD_OPTION_HTTPS_PRIORITIES, plugin->crypto_init,
2616 MHD_OPTION_HTTPS_MEM_KEY, plugin->key,
2617 MHD_OPTION_HTTPS_MEM_CERT, plugin->cert,
2618 MHD_OPTION_SOCK_ADDR, tmp,
2619 MHD_OPTION_CONNECTION_LIMIT, (unsigned int) 32,
2620 //MHD_OPTION_PER_IP_CONNECTION_LIMIT, (unsigned int) 6,
2621 MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) timeout,
2622 MHD_OPTION_CONNECTION_MEMORY_LIMIT, (size_t) (16 * 1024),
2623 MHD_OPTION_NOTIFY_COMPLETED, &mhd_termination_cb, NULL,
2626 if ((plugin->http_server_daemon_v4 == NULL) && (plugin->use_ipv4 == GNUNET_YES) && (port != 0))
2628 plugin->http_server_daemon_v4 = MHD_start_daemon (
2629 #if DEBUG_CONNECTIONS
2632 MHD_NO_FLAG | MHD_USE_SSL,
2635 plugin , &mdh_access_cb, plugin,
2636 /*MHD_OPTION_HTTPS_PRIORITIES, "NORMAL:",*/
2637 /*MHD_OPTION_HTTPS_PRIORITIES, "PERFORMANCE:",*/
2638 /* MHD_OPTION_HTTPS_PRIORITIES, "NONE:+VERS-TLS1.0:+ARCFOUR-128:+SHA1:+RSA:+COMP-NULL", */
2639 /*MHD_OPTION_HTTPS_PRIORITIES, "NONE:+VERS-TLS1.0:+ARCFOUR-128:+MD5:+RSA:+COMP-NULL",*/
2640 MHD_OPTION_HTTPS_PRIORITIES, plugin->crypto_init,
2641 MHD_OPTION_HTTPS_MEM_KEY, plugin->key,
2642 MHD_OPTION_HTTPS_MEM_CERT, plugin->cert,
2643 MHD_OPTION_SOCK_ADDR, (struct sockaddr_in *)plugin->bind4_address,
2644 MHD_OPTION_CONNECTION_LIMIT, (unsigned int) 32,
2645 //MHD_OPTION_PER_IP_CONNECTION_LIMIT, (unsigned int) 6,
2646 MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) timeout,
2647 MHD_OPTION_CONNECTION_MEMORY_LIMIT, (size_t) (16 * 1024),
2648 MHD_OPTION_NOTIFY_COMPLETED, &mhd_termination_cb, NULL,
2651 if (plugin->http_server_daemon_v4 != NULL)
2652 plugin->http_server_task_v4 = http_server_daemon_prepare (plugin, plugin->http_server_daemon_v4);
2653 if (plugin->http_server_daemon_v6 != NULL)
2654 plugin->http_server_task_v6 = http_server_daemon_prepare (plugin, plugin->http_server_daemon_v6);
2657 if (plugin->http_server_task_v4 != GNUNET_SCHEDULER_NO_TASK)
2660 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);
2663 else if ((plugin->http_server_task_v6 != GNUNET_SCHEDULER_NO_TASK) && (plugin->http_server_task_v4 != GNUNET_SCHEDULER_NO_TASK))
2666 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);
2669 else if ((plugin->http_server_task_v6 != GNUNET_SCHEDULER_NO_TASK) && (plugin->http_server_task_v4 == GNUNET_SCHEDULER_NO_TASK))
2672 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);
2678 if ((plugin->use_ipv6 == GNUNET_YES) && (plugin->use_ipv4 == GNUNET_YES))
2679 GNUNET_asprintf(&tmp,"with IPv4 and IPv6 enabled");
2680 if ((plugin->use_ipv6 == GNUNET_NO) && (plugin->use_ipv4 == GNUNET_YES))
2681 GNUNET_asprintf(&tmp,"with IPv4 enabled");
2682 if ((plugin->use_ipv6 == GNUNET_YES) && (plugin->use_ipv4 == GNUNET_NO))
2683 GNUNET_asprintf(&tmp,"with IPv6 enabled");
2684 if ((plugin->use_ipv6 == GNUNET_NO) && (plugin->use_ipv4 == GNUNET_NO))
2685 GNUNET_asprintf(&tmp,"with NO IP PROTOCOL enabled");
2686 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,"HTTPS Server with %s could not be started on port %u! https plugin failed!\n",tmp, port);
2688 libgnunet_plugin_transport_https_done (api);
2692 /* Initializing cURL */
2693 curl_global_init(CURL_GLOBAL_ALL);
2694 plugin->multi_handle = curl_multi_init();
2696 if ( NULL == plugin->multi_handle )
2698 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
2700 _("Could not initialize curl multi handle, failed to start http plugin!\n"),
2702 libgnunet_plugin_transport_https_done (api);
2706 plugin->peers = GNUNET_CONTAINER_multihashmap_create (10);
2707 GNUNET_OS_network_interfaces_list (&process_interfaces, plugin);
2712 /* end of plugin_transport_https.c */