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 GNUNET_assert(t6 != NULL);
626 if (plugin->bind6_address != NULL)
628 if (0 == memcmp(&plugin->bind6_address->sin6_addr, &bnd_cmp6, sizeof (struct in6_addr)))
630 memcpy (&t6->ipv6_addr,
631 &((struct sockaddr_in6 *) addr)->sin6_addr,
632 sizeof (struct in6_addr));
633 t6->u6_port = htons (plugin->port_inbound);
634 plugin->env->notify_address(plugin->env->cls,"https",t6,sizeof (struct IPv6HttpAddress) , GNUNET_TIME_UNIT_FOREVER_REL);
639 memcpy (&t6->ipv6_addr,
640 &((struct sockaddr_in6 *) addr)->sin6_addr,
641 sizeof (struct in6_addr));
642 t6->u6_port = htons (plugin->port_inbound);
643 plugin->env->notify_address(plugin->env->cls,"https",t6,sizeof (struct IPv6HttpAddress) , GNUNET_TIME_UNIT_FOREVER_REL);
652 * Callback called by MHD when a connection is terminated
654 static void mhd_termination_cb (void *cls, struct MHD_Connection * connection, void **httpSessionCache)
656 struct Session * ps = *httpSessionCache;
659 struct HTTP_PeerContext * pc = ps->peercontext;
661 if (connection==ps->recv_endpoint)
663 #if DEBUG_CONNECTIONS
664 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: inbound connection from peer `%s' was terminated\n", ps, GNUNET_i2s(&pc->identity));
666 ps->recv_active = GNUNET_NO;
667 ps->recv_connected = GNUNET_NO;
668 ps->recv_endpoint = NULL;
670 if (connection==ps->send_endpoint)
673 ps->send_active = GNUNET_NO;
674 ps->send_connected = GNUNET_NO;
675 ps->send_endpoint = NULL;
676 #if DEBUG_CONNECTIONS
677 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: outbound connection from peer `%s' was terminated\n", ps, GNUNET_i2s(&pc->identity));
681 /* if both connections disconnected, remove session */
682 if ((ps->send_connected == GNUNET_NO) && (ps->recv_connected == GNUNET_NO))
684 GNUNET_STATISTICS_update (pc->plugin->env->stats,
685 gettext_noop ("# HTTPS inbound sessions for peers active"),
688 remove_session(pc,ps,GNUNET_YES,GNUNET_SYSERR);
692 static void mhd_write_mst_cb (void *cls,
694 const struct GNUNET_MessageHeader *message)
697 struct Session *ps = cls;
698 GNUNET_assert(ps != NULL);
700 struct HTTP_PeerContext *pc = ps->peercontext;
701 GNUNET_assert(pc != NULL);
703 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
704 "Connection %X: Forwarding message to transport service, type %u and size %u from `%s' (`%s')\n",
706 ntohs(message->type),
707 ntohs(message->size),
708 GNUNET_i2s(&(ps->peercontext)->identity),http_plugin_address_to_string(NULL,ps->addr,ps->addrlen));
710 pc->plugin->env->receive (ps->peercontext->plugin->env->cls,
718 * Check if ip is allowed to connect.
721 mhd_accept_cb (void *cls,
722 const struct sockaddr *addr, socklen_t addr_len)
725 struct Plugin *plugin = cls;
727 /* Every connection is accepted, nothing more to do here */
731 int mhd_send_callback (void *cls, uint64_t pos, char *buf, int max)
735 struct Session * ps = cls;
736 struct HTTP_PeerContext * pc;
737 struct HTTP_Message * msg;
739 GNUNET_assert (ps!=NULL);
740 pc = ps->peercontext;
741 msg = ps->pending_msgs_tail;
742 if (ps->send_force_disconnect==GNUNET_YES)
744 #if DEBUG_CONNECTIONS
745 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: outbound forced to disconnect\n",ps);
752 if ((msg->size-msg->pos) <= max)
754 memcpy(buf,&msg->buf[msg->pos],(msg->size-msg->pos));
755 bytes_read = msg->size-msg->pos;
756 msg->pos+=(msg->size-msg->pos);
760 memcpy(buf,&msg->buf[msg->pos],max);
765 if (msg->pos==msg->size)
767 if (NULL!=msg->transmit_cont)
768 msg->transmit_cont (msg->transmit_cont_cls,&pc->identity,GNUNET_OK);
769 remove_http_message(ps,msg);
776 * Process GET or PUT request received via MHD. For
777 * GET, queue response that will send back our pending
778 * messages. For PUT, process incoming data and send
779 * to GNUnet core. In either case, check if a session
780 * already exists and create a new one if not.
783 mdh_access_cb (void *cls,
784 struct MHD_Connection *mhd_connection,
788 const char *upload_data,
789 size_t * upload_data_size, void **httpSessionCache)
791 struct Plugin *plugin = cls;
792 struct MHD_Response *response;
793 const union MHD_ConnectionInfo * conn_info;
795 struct sockaddr_in *addrin;
796 struct sockaddr_in6 *addrin6;
798 char address[INET6_ADDRSTRLEN+14];
799 struct GNUNET_PeerIdentity pi_in;
802 struct IPv4HttpAddress ipv4addr;
803 struct IPv6HttpAddress ipv6addr;
805 struct HTTP_PeerContext *pc;
806 struct Session *ps = NULL;
807 struct Session *ps_tmp = NULL;
810 int send_error_to_client;
814 GNUNET_assert(cls !=NULL);
815 send_error_to_client = GNUNET_NO;
817 if (NULL == *httpSessionCache)
819 /* check url for peer identity , if invalid send HTTP 404*/
820 size_t len = strlen(&url[1]);
821 char * peer = GNUNET_malloc(104+1);
823 if ((len>104) && (url[104]==';'))
825 char * id = GNUNET_malloc((len-104)+1);
826 strcpy(id,&url[105]);
827 memcpy(peer,&url[1],103);
829 id_num = strtoul ( id, NULL , 10);
832 res = GNUNET_CRYPTO_hash_from_string (peer, &(pi_in.hashPubKey));
834 if ( GNUNET_SYSERR == res )
836 response = MHD_create_response_from_data (strlen (HTTP_ERROR_RESPONSE),HTTP_ERROR_RESPONSE, MHD_NO, MHD_NO);
837 res = MHD_queue_response (mhd_connection, MHD_HTTP_NOT_FOUND, response);
838 MHD_destroy_response (response);
839 #if DEBUG_CONNECTIONS
841 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Peer has no valid ident, sent HTTP 1.1/404\n");
843 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Peer has no valid ident, could not send error\n");
850 ps = *httpSessionCache;
851 pc = ps->peercontext;
854 if (NULL == *httpSessionCache)
856 /* get peer context */
857 pc = GNUNET_CONTAINER_multihashmap_get (plugin->peers, &pi_in.hashPubKey);
861 pc = GNUNET_malloc(sizeof (struct HTTP_PeerContext));
863 pc->session_id_counter=1;
864 pc->last_session = NULL;
865 memcpy(&pc->identity, &pi_in, sizeof(struct GNUNET_PeerIdentity));
866 GNUNET_CONTAINER_multihashmap_put(plugin->peers, &pc->identity.hashPubKey, pc, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
867 GNUNET_STATISTICS_update (plugin->env->stats,
868 gettext_noop ("# HTTP peers active"),
873 conn_info = MHD_get_connection_info(mhd_connection, MHD_CONNECTION_INFO_CLIENT_ADDRESS );
874 /* Incoming IPv4 connection */
875 if ( AF_INET == conn_info->client_addr->sin_family)
877 addrin = conn_info->client_addr;
878 inet_ntop(addrin->sin_family, &(addrin->sin_addr),address,INET_ADDRSTRLEN);
879 memcpy(&ipv4addr.ipv4_addr,&(addrin->sin_addr),sizeof(struct in_addr));
880 ipv4addr.u_port = addrin->sin_port;
882 addr_len = sizeof(struct IPv4HttpAddress);
884 /* Incoming IPv6 connection */
885 if ( AF_INET6 == conn_info->client_addr->sin_family)
887 addrin6 = (struct sockaddr_in6 *) conn_info->client_addr;
888 inet_ntop(addrin6->sin6_family, &(addrin6->sin6_addr),address,INET6_ADDRSTRLEN);
889 memcpy(&ipv6addr.ipv6_addr,&(addrin6->sin6_addr),sizeof(struct in6_addr));
890 ipv6addr.u6_port = addrin6->sin6_port;
892 addr_len = sizeof(struct IPv6HttpAddress);
895 GNUNET_assert (addr != NULL);
896 GNUNET_assert (addr_len != 0);
899 /* only inbound sessions here */
904 if ((ps_tmp->direction==INBOUND) && (ps_tmp->session_id == id_num) && (id_num!=0))
906 if ((ps_tmp->recv_force_disconnect!=GNUNET_YES) && (ps_tmp->send_force_disconnect!=GNUNET_YES))
915 ps = GNUNET_malloc(sizeof (struct Session));
916 ps->addr = GNUNET_malloc(addr_len);
917 memcpy(ps->addr,addr,addr_len);
918 ps->addrlen = addr_len;
919 ps->direction=INBOUND;
920 ps->pending_msgs_head = NULL;
921 ps->pending_msgs_tail = NULL;
922 ps->send_connected=GNUNET_NO;
923 ps->send_active=GNUNET_NO;
924 ps->recv_connected=GNUNET_NO;
925 ps->recv_active=GNUNET_NO;
927 ps->session_id =id_num;
928 ps->url = create_url (plugin, ps->addr, ps->addrlen, ps->session_id);
929 GNUNET_CONTAINER_DLL_insert(pc->head,pc->tail,ps);
930 GNUNET_STATISTICS_update (plugin->env->stats,
931 gettext_noop ("# HTTPS inbound sessions for peers active"),
936 *httpSessionCache = ps;
937 if (ps->msgtok==NULL)
938 ps->msgtok = GNUNET_SERVER_mst_create (&mhd_write_mst_cb, ps);
940 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: HTTPS Daemon has new an incoming `%s' request from peer `%s' (`%s')\n",
943 GNUNET_i2s(&pc->identity),
944 http_plugin_address_to_string(NULL, ps->addr, ps->addrlen));
948 /* Is it a PUT or a GET request */
949 if (0 == strcmp (MHD_HTTP_METHOD_PUT, method))
951 if (ps->recv_force_disconnect == GNUNET_YES)
953 #if DEBUG_CONNECTIONS
954 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: inbound connection was forced to disconnect\n",ps);
956 ps->recv_active = GNUNET_NO;
959 if ((*upload_data_size == 0) && (ps->recv_active==GNUNET_NO))
961 ps->recv_endpoint = mhd_connection;
962 ps->recv_connected = GNUNET_YES;
963 ps->recv_active = GNUNET_YES;
964 ps->recv_force_disconnect = GNUNET_NO;
965 #if DEBUG_CONNECTIONS
966 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: inbound PUT connection connected\n",ps);
971 /* Transmission of all data complete */
972 if ((*upload_data_size == 0) && (ps->recv_active == GNUNET_YES))
974 response = MHD_create_response_from_data (strlen (HTTP_PUT_RESPONSE),HTTP_PUT_RESPONSE, MHD_NO, MHD_NO);
975 res = MHD_queue_response (mhd_connection, MHD_HTTP_OK, response);
976 #if DEBUG_CONNECTIONS
977 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: Sent HTTP/1.1: 200 OK as PUT Response\n",ps);
979 MHD_destroy_response (response);
980 ps->recv_active=GNUNET_NO;
985 if ((*upload_data_size > 0) && (ps->recv_active == GNUNET_YES))
987 res = GNUNET_SERVER_mst_receive(ps->msgtok, ps, upload_data,*upload_data_size, GNUNET_NO, GNUNET_NO);
988 (*upload_data_size) = 0;
994 if ( 0 == strcmp (MHD_HTTP_METHOD_GET, method) )
996 if (ps->send_force_disconnect == GNUNET_YES)
998 #if DEBUG_CONNECTIONS
999 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: outbound connection was forced to disconnect\n",ps);
1001 ps->send_active = GNUNET_NO;
1004 ps->send_connected = GNUNET_YES;
1005 ps->send_active = GNUNET_YES;
1006 ps->send_endpoint = mhd_connection;
1007 ps->send_force_disconnect = GNUNET_NO;
1008 #if DEBUG_CONNECTIONS
1009 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: inbound GET connection connected\n",ps);
1011 response = MHD_create_response_from_callback(-1,32 * 1024, &mhd_send_callback, ps, NULL);
1012 res = MHD_queue_response (mhd_connection, MHD_HTTP_OK, response);
1013 MHD_destroy_response (response);
1020 * Function that queries MHD's select sets and
1021 * starts the task waiting for them.
1023 static GNUNET_SCHEDULER_TaskIdentifier
1024 http_server_daemon_prepare (void * cls, struct MHD_Daemon *daemon_handle)
1026 struct Plugin *plugin = cls;
1027 GNUNET_SCHEDULER_TaskIdentifier ret;
1031 struct GNUNET_NETWORK_FDSet *wrs;
1032 struct GNUNET_NETWORK_FDSet *wws;
1033 struct GNUNET_NETWORK_FDSet *wes;
1035 unsigned long long timeout;
1037 struct GNUNET_TIME_Relative tv;
1039 GNUNET_assert(cls !=NULL);
1040 ret = GNUNET_SCHEDULER_NO_TASK;
1044 wrs = GNUNET_NETWORK_fdset_create ();
1045 wes = GNUNET_NETWORK_fdset_create ();
1046 wws = GNUNET_NETWORK_fdset_create ();
1048 GNUNET_assert (MHD_YES ==
1049 MHD_get_fdset (daemon_handle,
1054 haveto = MHD_get_timeout (daemon_handle, &timeout);
1055 if (haveto == MHD_YES)
1056 tv.value = (uint64_t) timeout;
1058 tv = GNUNET_TIME_UNIT_FOREVER_REL;
1059 GNUNET_NETWORK_fdset_copy_native (wrs, &rs, max);
1060 GNUNET_NETWORK_fdset_copy_native (wws, &ws, max);
1061 GNUNET_NETWORK_fdset_copy_native (wes, &es, max);
1062 if (daemon_handle == plugin->http_server_daemon_v4)
1064 if (plugin->http_server_task_v4 != GNUNET_SCHEDULER_NO_TASK)
1066 GNUNET_SCHEDULER_cancel(plugin->env->sched, plugin->http_server_task_v4);
1067 plugin->http_server_daemon_v4 = GNUNET_SCHEDULER_NO_TASK;
1070 ret = GNUNET_SCHEDULER_add_select (plugin->env->sched,
1071 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1072 GNUNET_SCHEDULER_NO_TASK,
1076 &http_server_daemon_v4_run,
1079 if (daemon_handle == plugin->http_server_daemon_v6)
1081 if (plugin->http_server_task_v6 != GNUNET_SCHEDULER_NO_TASK)
1083 GNUNET_SCHEDULER_cancel(plugin->env->sched, plugin->http_server_task_v6);
1084 plugin->http_server_task_v6 = GNUNET_SCHEDULER_NO_TASK;
1087 ret = GNUNET_SCHEDULER_add_select (plugin->env->sched,
1088 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1089 GNUNET_SCHEDULER_NO_TASK,
1093 &http_server_daemon_v6_run,
1096 GNUNET_NETWORK_fdset_destroy (wrs);
1097 GNUNET_NETWORK_fdset_destroy (wws);
1098 GNUNET_NETWORK_fdset_destroy (wes);
1103 * Call MHD to process pending requests and then go back
1104 * and schedule the next run.
1106 static void http_server_daemon_v4_run (void *cls,
1107 const struct GNUNET_SCHEDULER_TaskContext *tc)
1109 struct Plugin *plugin = cls;
1111 GNUNET_assert(cls !=NULL);
1112 plugin->http_server_task_v4 = GNUNET_SCHEDULER_NO_TASK;
1114 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1117 GNUNET_assert (MHD_YES == MHD_run (plugin->http_server_daemon_v4));
1118 plugin->http_server_task_v4 = http_server_daemon_prepare (plugin, plugin->http_server_daemon_v4);
1124 * Call MHD to process pending requests and then go back
1125 * and schedule the next run.
1127 static void http_server_daemon_v6_run (void *cls,
1128 const struct GNUNET_SCHEDULER_TaskContext *tc)
1130 struct Plugin *plugin = cls;
1132 GNUNET_assert(cls !=NULL);
1133 plugin->http_server_task_v6 = GNUNET_SCHEDULER_NO_TASK;
1135 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1138 GNUNET_assert (MHD_YES == MHD_run (plugin->http_server_daemon_v6));
1139 plugin->http_server_task_v6 = http_server_daemon_prepare (plugin, plugin->http_server_daemon_v6);
1143 static size_t curl_get_header_cb( void *ptr, size_t size, size_t nmemb, void *stream)
1145 struct Session * ps = stream;
1147 long http_result = 0;
1149 /* Getting last http result code */
1150 GNUNET_assert(NULL!=ps);
1151 if (ps->recv_connected==GNUNET_NO)
1153 res = curl_easy_getinfo(ps->recv_endpoint, CURLINFO_RESPONSE_CODE, &http_result);
1154 if (CURLE_OK == res)
1156 if (http_result == 200)
1158 ps->recv_connected = GNUNET_YES;
1159 ps->recv_active = GNUNET_YES;
1160 #if DEBUG_CONNECTIONS
1161 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: connected to recieve data\n",ps);
1163 // Calling send_check_connections again since receive is established
1164 send_check_connections (ps->peercontext->plugin, ps);
1171 size_t len = size * nmemb;
1174 if ((size * nmemb) < SIZE_MAX)
1175 tmp = GNUNET_malloc (len+1);
1177 if ((tmp != NULL) && (len > 0))
1179 memcpy(tmp,ptr,len);
1182 if (tmp[len-2] == 13)
1186 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: Header: %s\n",ps,tmp);
1193 return size * nmemb;
1196 static size_t curl_put_header_cb( void *ptr, size_t size, size_t nmemb, void *stream)
1198 struct Session * ps = stream;
1201 size_t len = size * nmemb;
1202 long http_result = 0;
1205 /* Getting last http result code */
1206 GNUNET_assert(NULL!=ps);
1207 res = curl_easy_getinfo(ps->send_endpoint, CURLINFO_RESPONSE_CODE, &http_result);
1208 if (CURLE_OK == res)
1210 if ((http_result == 100) && (ps->send_connected==GNUNET_NO))
1212 ps->send_connected = GNUNET_YES;
1213 ps->send_active = GNUNET_YES;
1214 #if DEBUG_CONNECTIONS
1215 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: connected to send data\n",ps);
1218 if ((http_result == 200) && (ps->send_connected==GNUNET_YES))
1220 ps->send_connected = GNUNET_NO;
1221 ps->send_active = GNUNET_NO;
1222 #if DEBUG_CONNECTIONS
1223 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: sending disconnected\n",ps);
1229 if ((size * nmemb) < SIZE_MAX)
1230 tmp = GNUNET_malloc (len+1);
1232 if ((tmp != NULL) && (len > 0))
1234 memcpy(tmp,ptr,len);
1237 if (tmp[len-2] == 13)
1244 return size * nmemb;
1248 * Callback method used with libcurl
1249 * Method is called when libcurl needs to read data during sending
1250 * @param stream pointer where to write data
1251 * @param size size of an individual element
1252 * @param nmemb count of elements that can be written to the buffer
1253 * @param ptr source pointer, passed to the libcurl handle
1254 * @return bytes written to stream
1256 static size_t curl_send_cb(void *stream, size_t size, size_t nmemb, void *ptr)
1258 struct Session * ps = ptr;
1259 struct HTTP_Message * msg = ps->pending_msgs_tail;
1263 if (ps->send_active == GNUNET_NO)
1264 return CURL_READFUNC_PAUSE;
1266 if ((ps->pending_msgs_tail == NULL) && (ps->send_active == GNUNET_YES))
1268 #if DEBUG_CONNECTIONS
1269 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: No Message to send, pausing connection\n",ps);
1271 ps->send_active = GNUNET_NO;
1272 return CURL_READFUNC_PAUSE;
1275 GNUNET_assert (msg!=NULL);
1278 if (msg->pos < msg->size)
1280 /* data fit in buffer */
1281 if ((msg->size - msg->pos) <= (size * nmemb))
1283 len = (msg->size - msg->pos);
1284 memcpy(stream, &msg->buf[msg->pos], len);
1291 memcpy(stream, &msg->buf[msg->pos], len);
1296 /* no data to send */
1302 if ( msg->pos == msg->size)
1304 #if DEBUG_CONNECTIONS
1305 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: Message with %u bytes sent, removing message from queue \n",ps, msg->pos);
1307 /* Calling transmit continuation */
1308 if (NULL != ps->pending_msgs_tail->transmit_cont)
1309 msg->transmit_cont (ps->pending_msgs_tail->transmit_cont_cls,&(ps->peercontext)->identity,GNUNET_OK);
1310 remove_http_message(ps, msg);
1315 static void curl_receive_mst_cb (void *cls,
1317 const struct GNUNET_MessageHeader *message)
1319 struct Session *ps = cls;
1320 GNUNET_assert(ps != NULL);
1322 struct HTTP_PeerContext *pc = ps->peercontext;
1323 GNUNET_assert(pc != NULL);
1326 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1327 "Connection %X: Forwarding message to transport service, type %u and size %u from `%s' (`%s')\n",
1329 ntohs(message->type),
1330 ntohs(message->size),
1331 GNUNET_i2s(&(pc->identity)),http_plugin_address_to_string(NULL,ps->addr,ps->addrlen));
1333 pc->plugin->env->receive (pc->plugin->env->cls,
1342 * Callback method used with libcurl
1343 * Method is called when libcurl needs to write data during sending
1344 * @param stream pointer where to write data
1345 * @param size size of an individual element
1346 * @param nmemb count of elements that can be written to the buffer
1347 * @param ptr destination pointer, passed to the libcurl handle
1348 * @return bytes read from stream
1350 static size_t curl_receive_cb( void *stream, size_t size, size_t nmemb, void *ptr)
1352 struct Session * ps = ptr;
1353 #if DEBUG_CONNECTIONS
1354 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: %u bytes received\n",ps, size*nmemb);
1356 GNUNET_SERVER_mst_receive(ps->msgtok, ps, stream, size*nmemb, GNUNET_NO, GNUNET_NO);
1357 return (size * nmemb);
1361 static void curl_perform (void *cls,
1362 const struct GNUNET_SCHEDULER_TaskContext *tc)
1364 struct Plugin *plugin = cls;
1365 static unsigned int handles_last_run;
1367 struct CURLMsg *msg;
1369 struct Session *ps = NULL;
1370 struct HTTP_PeerContext *pc = NULL;
1371 struct HTTP_Message * cur_msg = NULL;
1375 GNUNET_assert(cls !=NULL);
1377 plugin->http_curl_task = GNUNET_SCHEDULER_NO_TASK;
1378 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1384 mret = curl_multi_perform (plugin->multi_handle, &running);
1385 if ((running < handles_last_run) && (running>0))
1390 msg = curl_multi_info_read (plugin->multi_handle, &running);
1393 /* get session for affected curl handle */
1394 GNUNET_assert ( msg->easy_handle != NULL );
1395 curl_easy_getinfo(msg->easy_handle, CURLINFO_PRIVATE, &tmp);
1396 ps = (struct Session *) tmp;
1397 GNUNET_assert ( ps != NULL );
1398 pc = ps->peercontext;
1399 GNUNET_assert ( pc != NULL );
1404 if ( (msg->data.result != CURLE_OK) &&
1405 (msg->data.result != CURLE_GOT_NOTHING) )
1407 /* sending msg failed*/
1408 if (msg->easy_handle == ps->send_endpoint)
1410 #if DEBUG_CONNECTIONS
1411 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1412 _("Connection %X: HTTPS PUT to peer `%s' (`%s') failed: `%s' `%s'\n"),
1414 GNUNET_i2s(&pc->identity),
1415 http_plugin_address_to_string(NULL, ps->addr, ps->addrlen),
1416 "curl_multi_perform",
1417 curl_easy_strerror (msg->data.result));
1419 ps->send_connected = GNUNET_NO;
1420 ps->send_active = GNUNET_NO;
1421 curl_multi_remove_handle(plugin->multi_handle,ps->send_endpoint);
1422 //curl_easy_cleanup(ps->send_endpoint);
1423 //ps->send_endpoint=NULL;
1424 cur_msg = ps->pending_msgs_tail;
1425 if (( NULL != cur_msg) && ( NULL != cur_msg->transmit_cont))
1426 cur_msg->transmit_cont (cur_msg->transmit_cont_cls,&pc->identity,GNUNET_SYSERR);
1428 /* GET connection failed */
1429 if (msg->easy_handle == ps->recv_endpoint)
1431 #if DEBUG_CONNECTIONS
1432 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1433 _("Connection %X: HTTPS GET to peer `%s' (`%s') failed: `%s' `%s'\n"),
1435 GNUNET_i2s(&pc->identity),
1436 http_plugin_address_to_string(NULL, ps->addr, ps->addrlen),
1437 "curl_multi_perform",
1438 curl_easy_strerror (msg->data.result));
1440 ps->recv_connected = GNUNET_NO;
1441 ps->recv_active = GNUNET_NO;
1442 curl_multi_remove_handle(plugin->multi_handle,ps->recv_endpoint);
1443 //curl_easy_cleanup(ps->recv_endpoint);
1444 //ps->recv_endpoint=NULL;
1449 if (msg->easy_handle == ps->send_endpoint)
1451 GNUNET_assert (CURLE_OK == curl_easy_getinfo(msg->easy_handle, CURLINFO_RESPONSE_CODE, &http_result));
1452 #if DEBUG_CONNECTIONS
1453 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1454 "Connection %X: HTTPS PUT connection to peer `%s' (`%s') was closed with HTTP code %u\n",
1456 GNUNET_i2s(&pc->identity),
1457 http_plugin_address_to_string(NULL, ps->addr, ps->addrlen),
1460 /* Calling transmit continuation */
1461 cur_msg = ps->pending_msgs_tail;
1462 if (( NULL != cur_msg) && (NULL != cur_msg->transmit_cont))
1464 /* HTTP 1xx : Last message before here was informational */
1465 if ((http_result >=100) && (http_result < 200))
1466 cur_msg->transmit_cont (cur_msg->transmit_cont_cls,&pc->identity,GNUNET_OK);
1467 /* HTTP 2xx: successful operations */
1468 if ((http_result >=200) && (http_result < 300))
1469 cur_msg->transmit_cont (cur_msg->transmit_cont_cls,&pc->identity,GNUNET_OK);
1470 /* HTTP 3xx..5xx: error */
1471 if ((http_result >=300) && (http_result < 600))
1472 cur_msg->transmit_cont (cur_msg->transmit_cont_cls,&pc->identity,GNUNET_SYSERR);
1474 ps->send_connected = GNUNET_NO;
1475 ps->send_active = GNUNET_NO;
1476 curl_multi_remove_handle(plugin->multi_handle,ps->send_endpoint);
1477 //curl_easy_cleanup(ps->send_endpoint);
1478 //ps->send_endpoint =NULL;
1480 if (msg->easy_handle == ps->recv_endpoint)
1482 #if DEBUG_CONNECTIONS
1483 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1484 "Connection %X: HTTP GET connection to peer `%s' (`%s') was closed with HTTP code %u\n",
1486 GNUNET_i2s(&pc->identity),
1487 http_plugin_address_to_string(NULL, ps->addr, ps->addrlen),
1490 ps->recv_connected = GNUNET_NO;
1491 ps->recv_active = GNUNET_NO;
1492 curl_multi_remove_handle(plugin->multi_handle,ps->recv_endpoint);
1493 //curl_easy_cleanup(ps->recv_endpoint);
1494 //ps->recv_endpoint=NULL;
1497 if ((ps->recv_connected == GNUNET_NO) && (ps->send_connected == GNUNET_NO))
1498 remove_session (pc, ps, GNUNET_YES, GNUNET_SYSERR);
1505 while ( (running > 0) );
1507 handles_last_run = running;
1509 while (mret == CURLM_CALL_MULTI_PERFORM);
1510 curl_schedule(plugin);
1515 * Function setting up file descriptors and scheduling task to run
1516 * @param ses session to send data to
1517 * @return GNUNET_SYSERR for hard failure, GNUNET_OK for ok
1520 http_plugin_disconnect (void *cls,
1521 const struct GNUNET_PeerIdentity *target)
1525 struct Plugin *plugin = cls;
1526 struct HTTP_PeerContext *pc = NULL;
1527 struct Session *ps = NULL;
1528 //struct Session *tmp = NULL;
1530 pc = GNUNET_CONTAINER_multihashmap_get (plugin->peers, &target->hashPubKey);
1537 /* Telling transport that session is getting disconnected */
1538 plugin->env->session_end(plugin, target, ps);
1539 if (ps->direction==OUTBOUND)
1541 if (ps->send_endpoint!=NULL)
1543 //GNUNET_assert(CURLM_OK == curl_multi_remove_handle(plugin->multi_handle,ps->send_endpoint));
1544 //curl_easy_cleanup(ps->send_endpoint);
1545 //ps->send_endpoint=NULL;
1546 ps->send_force_disconnect = GNUNET_YES;
1548 if (ps->recv_endpoint!=NULL)
1550 //GNUNET_assert(CURLM_OK == curl_multi_remove_handle(plugin->multi_handle,ps->recv_endpoint));
1551 //curl_easy_cleanup(ps->recv_endpoint);
1552 //ps->recv_endpoint=NULL;
1553 ps->recv_force_disconnect = GNUNET_YES;
1557 if (ps->direction==INBOUND)
1559 ps->recv_force_disconnect = GNUNET_YES;
1560 ps->send_force_disconnect = GNUNET_YES;
1563 while (ps->pending_msgs_head!=NULL)
1565 remove_http_message(ps, ps->pending_msgs_head);
1567 ps->recv_active = GNUNET_NO;
1568 ps->send_active = GNUNET_NO;
1574 static int curl_schedule(void *cls)
1576 struct Plugin *plugin = cls;
1581 struct GNUNET_NETWORK_FDSet *grs;
1582 struct GNUNET_NETWORK_FDSet *gws;
1586 GNUNET_assert(cls !=NULL);
1588 /* Cancel previous scheduled task */
1589 if (plugin->http_curl_task != GNUNET_SCHEDULER_NO_TASK)
1591 GNUNET_SCHEDULER_cancel(plugin->env->sched, plugin->http_curl_task);
1592 plugin->http_curl_task = GNUNET_SCHEDULER_NO_TASK;
1598 mret = curl_multi_fdset (plugin->multi_handle, &rs, &ws, &es, &max);
1599 if (mret != CURLM_OK)
1601 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1602 _("%s failed at %s:%d: `%s'\n"),
1603 "curl_multi_fdset", __FILE__, __LINE__,
1604 curl_multi_strerror (mret));
1605 return GNUNET_SYSERR;
1607 mret = curl_multi_timeout (plugin->multi_handle, &to);
1608 if (mret != CURLM_OK)
1610 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1611 _("%s failed at %s:%d: `%s'\n"),
1612 "curl_multi_timeout", __FILE__, __LINE__,
1613 curl_multi_strerror (mret));
1614 return GNUNET_SYSERR;
1617 grs = GNUNET_NETWORK_fdset_create ();
1618 gws = GNUNET_NETWORK_fdset_create ();
1619 GNUNET_NETWORK_fdset_copy_native (grs, &rs, max + 1);
1620 GNUNET_NETWORK_fdset_copy_native (gws, &ws, max + 1);
1621 plugin->http_curl_task = GNUNET_SCHEDULER_add_select (plugin->env->sched,
1622 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1623 GNUNET_SCHEDULER_NO_TASK,
1624 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 0),
1629 GNUNET_NETWORK_fdset_destroy (gws);
1630 GNUNET_NETWORK_fdset_destroy (grs);
1635 * Function setting up curl handle and selecting message to send
1637 * @param ses session to send data to
1638 * @param con connection
1639 * @return GNUNET_SYSERR on failure, GNUNET_NO if connecting, GNUNET_YES if ok
1641 static ssize_t send_check_connections (void *cls, struct Session *ps)
1643 struct Plugin *plugin = cls;
1645 struct HTTP_Message * msg;
1647 struct GNUNET_TIME_Relative timeout = GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT;
1649 GNUNET_assert(cls !=NULL);
1651 if (ps->direction == OUTBOUND)
1653 /* RECV DIRECTION */
1654 /* Check if session is connected to receive data, otherwise connect to peer */
1655 if (ps->recv_connected == GNUNET_NO)
1657 int fresh = GNUNET_NO;
1658 if (ps->recv_endpoint == NULL)
1661 ps->recv_endpoint = curl_easy_init();
1664 curl_easy_setopt(ps->recv_endpoint, CURLOPT_VERBOSE, 1L);
1666 curl_easy_setopt(ps->recv_endpoint, CURLOPT_URL, ps->url);
1667 curl_easy_setopt (ps->recv_endpoint, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1);
1668 //curl_easy_setopt (ps->recv_endpoint, CURLOPT_SSL_CIPHER_LIST, cipher_suite);
1669 curl_easy_setopt(ps->recv_endpoint, CURLOPT_SSL_VERIFYPEER, 0);
1670 curl_easy_setopt(ps->recv_endpoint, CURLOPT_SSL_VERIFYHOST, 0);
1671 curl_easy_setopt(ps->recv_endpoint, CURLOPT_HEADERFUNCTION, &curl_get_header_cb);
1672 curl_easy_setopt(ps->recv_endpoint, CURLOPT_WRITEHEADER, ps);
1673 curl_easy_setopt(ps->recv_endpoint, CURLOPT_READFUNCTION, curl_send_cb);
1674 curl_easy_setopt(ps->recv_endpoint, CURLOPT_READDATA, ps);
1675 curl_easy_setopt(ps->recv_endpoint, CURLOPT_WRITEFUNCTION, curl_receive_cb);
1676 curl_easy_setopt(ps->recv_endpoint, CURLOPT_WRITEDATA, ps);
1677 curl_easy_setopt(ps->recv_endpoint, CURLOPT_TIMEOUT, (long) timeout.value);
1678 curl_easy_setopt(ps->recv_endpoint, CURLOPT_PRIVATE, ps);
1679 curl_easy_setopt(ps->recv_endpoint, CURLOPT_CONNECTTIMEOUT, HTTP_CONNECT_TIMEOUT);
1680 curl_easy_setopt(ps->recv_endpoint, CURLOPT_BUFFERSIZE, GNUNET_SERVER_MAX_MESSAGE_SIZE);
1682 if (fresh==GNUNET_YES)
1684 mret = curl_multi_add_handle(plugin->multi_handle, ps->recv_endpoint);
1685 if (mret != CURLM_OK)
1687 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1688 _("Connection: %X: %s failed at %s:%d: `%s'\n"),
1690 "curl_multi_add_handle", __FILE__, __LINE__,
1691 curl_multi_strerror (mret));
1692 return GNUNET_SYSERR;
1695 if (curl_schedule (plugin) == GNUNET_SYSERR)
1697 #if DEBUG_CONNECTIONS
1698 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: could not schedule curl task\n",ps);
1700 return GNUNET_SYSERR;
1702 #if DEBUG_CONNECTIONS
1703 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: inbound not connected, initiating connection\n",ps);
1707 /* waiting for receive direction */
1708 if (ps->recv_connected==GNUNET_NO)
1711 /* SEND DIRECTION */
1712 /* Check if session is connected to send data, otherwise connect to peer */
1713 if ((ps->send_connected == GNUNET_YES) && (ps->send_endpoint!= NULL))
1715 if (ps->send_active == GNUNET_YES)
1717 #if DEBUG_CONNECTIONS
1718 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: outbound active, enqueueing message\n",ps);
1722 if (ps->send_active == GNUNET_NO)
1724 #if DEBUG_CONNECTIONS
1725 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: outbound paused, unpausing existing connection and enqueueing message\n",ps);
1727 if (CURLE_OK == curl_easy_pause(ps->send_endpoint,CURLPAUSE_CONT))
1729 ps->send_active=GNUNET_YES;
1733 return GNUNET_SYSERR;
1736 /* not connected, initiate connection */
1737 if (ps->send_connected==GNUNET_NO)
1739 int fresh = GNUNET_NO;
1740 if (NULL == ps->send_endpoint)
1742 ps->send_endpoint = curl_easy_init();
1745 GNUNET_assert (ps->send_endpoint != NULL);
1746 GNUNET_assert (NULL != ps->pending_msgs_tail);
1747 #if DEBUG_CONNECTIONS
1748 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: outbound not connected, initiating connection\n",ps);
1750 ps->send_active = GNUNET_NO;
1751 msg = ps->pending_msgs_tail;
1754 curl_easy_setopt(ps->send_endpoint, CURLOPT_VERBOSE, 1L);
1756 curl_easy_setopt(ps->send_endpoint, CURLOPT_URL, ps->url);
1757 curl_easy_setopt(ps->send_endpoint, CURLOPT_PUT, 1L);
1758 curl_easy_setopt(ps->send_endpoint, CURLOPT_HEADERFUNCTION, &curl_put_header_cb);
1759 curl_easy_setopt(ps->send_endpoint, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1);
1760 curl_easy_setopt(ps->send_endpoint, CURLOPT_SSL_VERIFYPEER, 0);
1761 curl_easy_setopt(ps->send_endpoint, CURLOPT_SSL_VERIFYHOST, 0);
1763 curl_easy_setopt(ps->send_endpoint, CURLOPT_WRITEHEADER, ps);
1764 curl_easy_setopt(ps->send_endpoint, CURLOPT_READFUNCTION, curl_send_cb);
1765 curl_easy_setopt(ps->send_endpoint, CURLOPT_READDATA, ps);
1766 curl_easy_setopt(ps->send_endpoint, CURLOPT_WRITEFUNCTION, curl_receive_cb);
1767 curl_easy_setopt(ps->send_endpoint, CURLOPT_READDATA, ps);
1768 curl_easy_setopt(ps->send_endpoint, CURLOPT_TIMEOUT, (long) timeout.value);
1769 curl_easy_setopt(ps->send_endpoint, CURLOPT_PRIVATE, ps);
1770 curl_easy_setopt(ps->send_endpoint, CURLOPT_CONNECTTIMEOUT, HTTP_CONNECT_TIMEOUT);
1771 curl_easy_setopt(ps->send_endpoint, CURLOPT_BUFFERSIZE, GNUNET_SERVER_MAX_MESSAGE_SIZE);
1773 if (fresh==GNUNET_YES)
1775 mret = curl_multi_add_handle(plugin->multi_handle, ps->send_endpoint);
1776 if (mret != CURLM_OK)
1778 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1779 _("Connection: %X: %s failed at %s:%d: `%s'\n"),
1781 "curl_multi_add_handle", __FILE__, __LINE__,
1782 curl_multi_strerror (mret));
1783 return GNUNET_SYSERR;
1787 if (curl_schedule (plugin) == GNUNET_SYSERR)
1788 return GNUNET_SYSERR;
1791 if (ps->direction == INBOUND)
1793 GNUNET_assert (NULL != ps->pending_msgs_tail);
1794 if ((ps->recv_connected==GNUNET_YES) && (ps->send_connected==GNUNET_YES) &&
1795 (ps->recv_force_disconnect==GNUNET_NO) && (ps->recv_force_disconnect==GNUNET_NO))
1798 return GNUNET_SYSERR;
1801 static struct Session * send_select_session (void * cls, struct HTTP_PeerContext *pc, const void * addr, size_t addrlen, int force_address, struct Session * session)
1803 struct Session * tmp = NULL;
1804 int addr_given = GNUNET_NO;
1806 if ((addr!=NULL) && (addrlen>0))
1807 addr_given = GNUNET_YES;
1809 if (force_address == GNUNET_YES)
1811 /* check session given as argument */
1812 if ((session != NULL) && (addr_given == GNUNET_YES))
1814 if (0 == memcmp(session->addr, addr, addrlen))
1816 /* connection can not be used, since it is disconnected */
1817 if ((session->recv_force_disconnect==GNUNET_NO) && (session->send_force_disconnect==GNUNET_NO))
1819 #if DEBUG_SESSION_SELECTION
1820 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Session %X selected: Using session passed by transport to send to forced address \n", session);
1826 /* check last session used */
1827 if ((pc->last_session != NULL)&& (addr_given == GNUNET_YES))
1829 if (0 == memcmp(pc->last_session->addr, addr, addrlen))
1831 /* connection can not be used, since it is disconnected */
1832 if ((pc->last_session->recv_force_disconnect==GNUNET_NO) && (pc->last_session->send_force_disconnect==GNUNET_NO))
1834 #if DEBUG_SESSION_SELECTION
1835 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Session %X selected: Using last session used to send to forced address \n", pc->last_session);
1837 return pc->last_session;
1841 /* find session in existing sessions */
1843 while ((tmp!=NULL) && (addr_given == GNUNET_YES))
1846 if (0 == memcmp(tmp->addr, addr, addrlen))
1848 /* connection can not be used, since it is disconnected */
1849 if ((tmp->recv_force_disconnect==GNUNET_NO) && (tmp->send_force_disconnect==GNUNET_NO))
1851 #if DEBUG_SESSION_SELECTION
1852 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Session %X selected: Using existing session to send to forced address \n", session);
1860 /* no session to use */
1863 if ((force_address == GNUNET_NO) || (force_address == GNUNET_SYSERR))
1865 /* check session given as argument */
1866 if (session != NULL)
1868 /* connection can not be used, since it is disconnected */
1869 if ((session->recv_force_disconnect==GNUNET_NO) && (session->send_force_disconnect==GNUNET_NO))
1871 #if DEBUG_SESSION_SELECTION
1872 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Session %X selected: Using session passed by transport to send not-forced address \n", session);
1878 /* check last session used */
1879 if (pc->last_session != NULL)
1881 /* connection can not be used, since it is disconnected */
1882 if ((pc->last_session->recv_force_disconnect==GNUNET_NO) && (pc->last_session->send_force_disconnect==GNUNET_NO))
1884 #if DEBUG_SESSION_SELECTION
1885 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Session %X selected: Using last session to send to not-forced address \n", pc->last_session);
1887 return pc->last_session;
1890 /* find session in existing sessions */
1894 /* connection can not be used, since it is disconnected */
1895 if ((tmp->recv_force_disconnect==GNUNET_NO) && (tmp->send_force_disconnect==GNUNET_NO))
1897 #if DEBUG_SESSION_SELECTION
1898 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Session %X selected: Using existing session to send to not-forced address \n", tmp);
1910 * Function that can be used by the transport service to transmit
1911 * a message using the plugin. Note that in the case of a
1912 * peer disconnecting, the continuation MUST be called
1913 * prior to the disconnect notification itself. This function
1914 * will be called with this peer's HELLO message to initiate
1915 * a fresh connection to another peer.
1917 * @param cls closure
1918 * @param target who should receive this message
1919 * @param msgbuf the message to transmit
1920 * @param msgbuf_size number of bytes in 'msgbuf'
1921 * @param priority how important is the message (most plugins will
1922 * ignore message priority and just FIFO)
1923 * @param timeout how long to wait at most for the transmission (does not
1924 * require plugins to discard the message after the timeout,
1925 * just advisory for the desired delay; most plugins will ignore
1927 * @param session which session must be used (or NULL for "any")
1928 * @param addr the address to use (can be NULL if the plugin
1929 * is "on its own" (i.e. re-use existing TCP connection))
1930 * @param addrlen length of the address in bytes
1931 * @param force_address GNUNET_YES if the plugin MUST use the given address,
1932 * GNUNET_NO means the plugin may use any other address and
1933 * GNUNET_SYSERR means that only reliable existing
1934 * bi-directional connections should be used (regardless
1936 * @param cont continuation to call once the message has
1937 * been transmitted (or if the transport is ready
1938 * for the next transmission call; or if the
1939 * peer disconnected...); can be NULL
1940 * @param cont_cls closure for cont
1941 * @return number of bytes used (on the physical network, with overheads);
1942 * -1 on hard errors (i.e. address invalid); 0 is a legal value
1943 * and does NOT mean that the message was not transmitted (DV)
1946 http_plugin_send (void *cls,
1947 const struct GNUNET_PeerIdentity *target,
1950 unsigned int priority,
1951 struct GNUNET_TIME_Relative to,
1952 struct Session *session,
1956 GNUNET_TRANSPORT_TransmitContinuation cont,
1959 struct Plugin *plugin = cls;
1960 struct HTTP_Message *msg;
1961 struct HTTP_PeerContext * pc;
1962 struct Session * ps = NULL;
1964 GNUNET_assert(cls !=NULL);
1967 char * force = GNUNET_malloc(40);
1968 if (force_address == GNUNET_YES)
1969 strcpy(force,"forced addr.");
1970 if (force_address == GNUNET_NO)
1971 strcpy(force,"any addr.");
1972 if (force_address == GNUNET_SYSERR)
1973 strcpy(force,"reliable bi-direc. address addr.");
1975 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Transport tells me to send %u bytes to `%s' using %s (%s) and session: %X\n",
1979 http_plugin_address_to_string(NULL, addr, addrlen),
1985 pc = GNUNET_CONTAINER_multihashmap_get (plugin->peers, &target->hashPubKey);
1989 pc = GNUNET_malloc(sizeof (struct HTTP_PeerContext));
1990 pc->plugin = plugin;
1991 pc->session_id_counter=1;
1992 pc->last_session = NULL;
1993 memcpy(&pc->identity, target, sizeof(struct GNUNET_PeerIdentity));
1994 GNUNET_CONTAINER_multihashmap_put(plugin->peers, &pc->identity.hashPubKey, pc, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
1995 GNUNET_STATISTICS_update (plugin->env->stats,
1996 gettext_noop ("# HTTP peers active"),
2001 ps = send_select_session (plugin, pc, addr, addrlen, force_address, session);
2003 /* session not existing, but address forced -> creating new session */
2006 if ((addr!=NULL) && (addrlen!=0))
2008 ps = GNUNET_malloc(sizeof (struct Session));
2009 #if DEBUG_SESSION_SELECTION
2010 if (force_address == GNUNET_YES)
2011 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"No existing connection & forced address: creating new session %X to peer %s\n", ps, GNUNET_i2s(target));
2012 if (force_address != GNUNET_YES)
2013 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"No existing connection: creating new session %X to peer %s\n", ps, GNUNET_i2s(target));
2015 if ((addrlen!=0) && (addr!=NULL))
2017 ps->addr = GNUNET_malloc(addrlen);
2018 memcpy(ps->addr,addr,addrlen);
2019 ps->addrlen = addrlen;
2026 ps->direction=OUTBOUND;
2027 ps->recv_connected = GNUNET_NO;
2028 ps->recv_force_disconnect = GNUNET_NO;
2029 ps->send_connected = GNUNET_NO;
2030 ps->send_force_disconnect = GNUNET_NO;
2031 ps->pending_msgs_head = NULL;
2032 ps->pending_msgs_tail = NULL;
2034 ps->session_id = pc->session_id_counter;
2035 pc->session_id_counter++;
2036 ps->url = create_url (plugin, ps->addr, ps->addrlen, ps->session_id);
2037 if (ps->msgtok == NULL)
2038 ps->msgtok = GNUNET_SERVER_mst_create (&curl_receive_mst_cb, ps);
2039 GNUNET_CONTAINER_DLL_insert(pc->head,pc->tail,ps);
2042 GNUNET_STATISTICS_update (plugin->env->stats,
2043 gettext_noop ("# HTTP outbound sessions for peers active"),
2050 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));
2052 return GNUNET_SYSERR;
2057 msg = GNUNET_malloc (sizeof (struct HTTP_Message) + msgbuf_size);
2059 msg->size = msgbuf_size;
2061 msg->buf = (char *) &msg[1];
2062 msg->transmit_cont = cont;
2063 msg->transmit_cont_cls = cont_cls;
2064 memcpy (msg->buf,msgbuf, msgbuf_size);
2065 GNUNET_CONTAINER_DLL_insert(ps->pending_msgs_head,ps->pending_msgs_tail,msg);
2067 if (send_check_connections (plugin, ps) != GNUNET_SYSERR)
2069 if (force_address != GNUNET_YES)
2070 pc->last_session = ps;
2072 if (pc->last_session==NULL)
2073 pc->last_session = ps;
2077 return GNUNET_SYSERR;
2083 * Function that can be used to force the plugin to disconnect
2084 * from the given peer and cancel all previous transmissions
2085 * (and their continuationc).
2087 * @param cls closure
2088 * @param target peer from which to disconnect
2091 * Convert the transports address to a nice, human-readable
2094 * @param cls closure
2095 * @param type name of the transport that generated the address
2096 * @param addr one of the addresses of the host, NULL for the last address
2097 * the specific address format depends on the transport
2098 * @param addrlen length of the address
2099 * @param numeric should (IP) addresses be displayed in numeric form?
2100 * @param timeout after how long should we give up?
2101 * @param asc function to call on each string
2102 * @param asc_cls closure for asc
2105 http_plugin_address_pretty_printer (void *cls,
2110 struct GNUNET_TIME_Relative timeout,
2111 GNUNET_TRANSPORT_AddressStringCallback
2114 const struct IPv4HttpAddress *t4;
2115 const struct IPv6HttpAddress *t6;
2116 struct sockaddr_in a4;
2117 struct sockaddr_in6 a6;
2123 GNUNET_assert(cls !=NULL);
2124 if (addrlen == sizeof (struct IPv6HttpAddress))
2126 address = GNUNET_malloc (INET6_ADDRSTRLEN);
2128 a6.sin6_addr = t6->ipv6_addr;
2129 inet_ntop(AF_INET6, &(a6.sin6_addr),address,INET6_ADDRSTRLEN);
2130 port = ntohs(t6->u6_port);
2132 else if (addrlen == sizeof (struct IPv4HttpAddress))
2134 address = GNUNET_malloc (INET_ADDRSTRLEN);
2136 a4.sin_addr.s_addr = t4->ipv4_addr;
2137 inet_ntop(AF_INET, &(a4.sin_addr),address,INET_ADDRSTRLEN);
2138 port = ntohs(t4->u_port);
2142 /* invalid address */
2143 GNUNET_break_op (0);
2144 asc (asc_cls, NULL);
2147 res = GNUNET_asprintf(&ret,"%s://%s:%u/", PROTOCOL_PREFIX, address,port);
2148 GNUNET_free (address);
2149 GNUNET_assert(res != 0);
2151 GNUNET_free_non_null (ret);
2157 * Another peer has suggested an address for this
2158 * peer and transport plugin. Check that this could be a valid
2159 * address. If so, consider adding it to the list
2162 * @param cls closure
2163 * @param addr pointer to the address
2164 * @param addrlen length of addr
2165 * @return GNUNET_OK if this is a plausible address for this peer
2169 http_plugin_address_suggested (void *cls,
2170 const void *addr, size_t addrlen)
2172 struct Plugin *plugin = cls;
2173 struct IPv4HttpAddress *v4;
2174 struct IPv6HttpAddress *v6;
2177 GNUNET_assert(cls !=NULL);
2178 if ((addrlen != sizeof (struct IPv4HttpAddress)) &&
2179 (addrlen != sizeof (struct IPv6HttpAddress)))
2181 return GNUNET_SYSERR;
2183 if (addrlen == sizeof (struct IPv4HttpAddress))
2185 v4 = (struct IPv4HttpAddress *) addr;
2186 /* Not skipping loopback
2187 if (INADDR_LOOPBACK == ntohl(v4->ipv4_addr))
2189 return GNUNET_SYSERR;
2191 port = ntohs (v4->u_port);
2192 if (port != plugin->port_inbound)
2194 return GNUNET_SYSERR;
2197 if (addrlen == sizeof (struct IPv6HttpAddress))
2199 v6 = (struct IPv6HttpAddress *) addr;
2200 if (IN6_IS_ADDR_LINKLOCAL (&v6->ipv6_addr))
2202 return GNUNET_SYSERR;
2204 port = ntohs (v6->u6_port);
2205 if (port != plugin->port_inbound)
2207 return GNUNET_SYSERR;
2216 * Function called for a quick conversion of the binary address to
2217 * a numeric address. Note that the caller must not free the
2218 * address and that the next call to this function is allowed
2219 * to override the address again.
2221 * @param cls closure
2222 * @param addr binary address
2223 * @param addrlen length of the address
2224 * @return string representing the same address
2227 http_plugin_address_to_string (void *cls,
2231 const struct IPv4HttpAddress *t4;
2232 const struct IPv6HttpAddress *t6;
2233 struct sockaddr_in a4;
2234 struct sockaddr_in6 a6;
2240 if (addrlen == sizeof (struct IPv6HttpAddress))
2242 address = GNUNET_malloc (INET6_ADDRSTRLEN);
2244 a6.sin6_addr = t6->ipv6_addr;
2245 inet_ntop(AF_INET6, &(a6.sin6_addr),address,INET6_ADDRSTRLEN);
2246 port = ntohs(t6->u6_port);
2248 else if (addrlen == sizeof (struct IPv4HttpAddress))
2250 address = GNUNET_malloc (INET_ADDRSTRLEN);
2252 a4.sin_addr.s_addr = t4->ipv4_addr;
2253 inet_ntop(AF_INET, &(a4.sin_addr),address,INET_ADDRSTRLEN);
2254 port = ntohs(t4->u_port);
2258 /* invalid address */
2261 res = GNUNET_asprintf(&ret,"%s:%u",address,port);
2262 GNUNET_free (address);
2263 GNUNET_assert(res != 0);
2268 load_certificate( const char * file )
2270 struct GNUNET_DISK_FileHandle * gn_file;
2275 if (0!=STAT(file, &fstat))
2277 text = GNUNET_malloc (fstat.st_size+1);
2278 gn_file = GNUNET_DISK_file_open(file,GNUNET_DISK_OPEN_READ, GNUNET_DISK_PERM_USER_READ);
2284 if (GNUNET_SYSERR == GNUNET_DISK_file_read(gn_file, text, fstat.st_size))
2289 text[fstat.st_size] = '\0';
2290 GNUNET_DISK_file_close(gn_file);
2297 * Exit point from the plugin.
2300 libgnunet_plugin_transport_https_done (void *cls)
2302 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
2303 struct Plugin *plugin = api->cls;
2305 GNUNET_assert(cls !=NULL);
2307 if (plugin->http_server_daemon_v4 != NULL)
2309 MHD_stop_daemon (plugin->http_server_daemon_v4);
2310 plugin->http_server_daemon_v4 = NULL;
2312 if (plugin->http_server_daemon_v6 != NULL)
2314 MHD_stop_daemon (plugin->http_server_daemon_v6);
2315 plugin->http_server_daemon_v6 = NULL;
2318 if ( plugin->http_server_task_v4 != GNUNET_SCHEDULER_NO_TASK)
2320 GNUNET_SCHEDULER_cancel(plugin->env->sched, plugin->http_server_task_v4);
2321 plugin->http_server_task_v4 = GNUNET_SCHEDULER_NO_TASK;
2324 if ( plugin->http_server_task_v6 != GNUNET_SCHEDULER_NO_TASK)
2326 GNUNET_SCHEDULER_cancel(plugin->env->sched, plugin->http_server_task_v6);
2327 plugin->http_server_task_v6 = GNUNET_SCHEDULER_NO_TASK;
2331 /* free all peer information */
2332 if (plugin->peers!=NULL)
2334 GNUNET_CONTAINER_multihashmap_iterate (plugin->peers,
2335 &remove_peer_context_Iterator,
2337 GNUNET_CONTAINER_multihashmap_destroy (plugin->peers);
2339 if (plugin->multi_handle!=NULL)
2341 mret = curl_multi_cleanup(plugin->multi_handle);
2343 if ( CURLM_OK != mret)
2344 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"curl multihandle clean up failed\n");
2346 plugin->multi_handle = NULL;
2348 curl_global_cleanup();
2350 if ( plugin->http_curl_task != GNUNET_SCHEDULER_NO_TASK)
2352 GNUNET_SCHEDULER_cancel(plugin->env->sched, plugin->http_curl_task);
2353 plugin->http_curl_task = GNUNET_SCHEDULER_NO_TASK;
2356 GNUNET_free_non_null (plugin->bind4_address);
2357 GNUNET_free_non_null (plugin->bind6_address);
2358 GNUNET_free_non_null (plugin->bind_hostname);
2359 GNUNET_free_non_null (plugin->crypto_init);
2360 GNUNET_free (plugin);
2363 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Unload http plugin complete...\n");
2370 * Entry point for the plugin.
2373 libgnunet_plugin_transport_https_init (void *cls)
2375 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
2376 struct Plugin *plugin;
2377 struct GNUNET_TRANSPORT_PluginFunctions *api;
2378 struct GNUNET_TIME_Relative gn_timeout;
2379 long long unsigned int port;
2384 GNUNET_assert(cls !=NULL);
2386 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Starting https plugin...\n");
2389 plugin = GNUNET_malloc (sizeof (struct Plugin));
2390 plugin->stats = env->stats;
2392 plugin->peers = NULL;
2393 plugin->bind4_address = NULL;
2394 plugin->use_ipv6 = GNUNET_YES;
2395 plugin->use_ipv4 = GNUNET_YES;
2397 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
2399 api->send = &http_plugin_send;
2400 api->disconnect = &http_plugin_disconnect;
2401 api->address_pretty_printer = &http_plugin_address_pretty_printer;
2402 api->check_address = &http_plugin_address_suggested;
2403 api->address_to_string = &http_plugin_address_to_string;
2405 /* Hashing our identity to use it in URLs */
2406 GNUNET_CRYPTO_hash_to_enc ( &(plugin->env->my_identity->hashPubKey), &plugin->my_ascii_hash_ident);
2408 /* Use IPv6 yes/no */
2409 if (GNUNET_CONFIGURATION_have_value (env->cfg,
2410 "transport-https", "USE_IPv6"))
2412 plugin->use_ipv6 = GNUNET_CONFIGURATION_get_value_yesno (env->cfg,
2416 /* Use IPv4 yes/no */
2417 if (GNUNET_CONFIGURATION_have_value (env->cfg,
2418 "transport-https", "USE_IPv4"))
2420 plugin->use_ipv4 = GNUNET_CONFIGURATION_get_value_yesno (env->cfg,
2424 /* Reading port number from config file */
2426 GNUNET_CONFIGURATION_get_value_number (env->cfg,
2432 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
2434 _("Require valid port number for transport plugin `%s' in configuration!\n"),
2436 libgnunet_plugin_transport_https_done (api);
2440 /* Reading ipv4 addresse to bind to from config file */
2441 if ((plugin->use_ipv4==GNUNET_YES) && (GNUNET_CONFIGURATION_have_value (env->cfg,
2442 "transport-https", "BINDTO4")))
2444 GNUNET_break (GNUNET_OK ==
2445 GNUNET_CONFIGURATION_get_value_string (env->cfg,
2448 &plugin->bind_hostname));
2449 plugin->bind4_address = GNUNET_malloc(sizeof(struct sockaddr_in));
2450 plugin->bind4_address->sin_family = AF_INET;
2451 plugin->bind4_address->sin_port = htons (port);
2453 if (inet_pton(AF_INET,plugin->bind_hostname, &plugin->bind4_address->sin_addr)<=0)
2455 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
2457 _("Misconfigured address to bind to in configuration!\n"),
2459 GNUNET_free(plugin->bind4_address);
2460 GNUNET_free(plugin->bind_hostname);
2461 plugin->bind_hostname = NULL;
2462 plugin->bind4_address = NULL;
2466 /* Get crypto init string from config */
2467 if (GNUNET_CONFIGURATION_have_value (env->cfg,
2468 "transport-https", "CRYPTO_INIT"))
2470 GNUNET_CONFIGURATION_get_value_string (env->cfg,
2473 &plugin->crypto_init);
2477 GNUNET_asprintf(&plugin->crypto_init,"NORMAL");
2480 /* Get private key file from config */
2481 if (GNUNET_CONFIGURATION_have_value (env->cfg,
2482 "transport-https", "CERT_FILE"))
2484 GNUNET_CONFIGURATION_get_value_string (env->cfg,
2491 GNUNET_asprintf(&cert_file,"https.cert");
2494 /* Get private key file from config */
2495 if (GNUNET_CONFIGURATION_have_value (env->cfg,
2496 "transport-https", "KEY_FILE"))
2498 GNUNET_CONFIGURATION_get_value_string (env->cfg,
2505 GNUNET_asprintf(&key_file,"https.key");
2508 /* Get private key file from config */
2509 if (GNUNET_CONFIGURATION_have_value (env->cfg,
2510 "transport-https", "CERT_FILE"))
2512 GNUNET_CONFIGURATION_get_value_string (env->cfg,
2519 GNUNET_asprintf(&cert_file,"https.cert");
2522 /* Should plugin use ipv6? */
2523 if ((plugin->use_ipv6==GNUNET_YES) && (GNUNET_CONFIGURATION_have_value (env->cfg,
2524 "transport-https", "BINDTO6")))
2526 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (env->cfg,
2529 &plugin->bind_hostname))
2531 plugin->bind6_address = GNUNET_malloc(sizeof(struct sockaddr_in6));
2532 plugin->bind6_address->sin6_family = AF_INET6;
2533 plugin->bind6_address->sin6_port = htons (port);
2535 if (inet_pton(AF_INET6,plugin->bind_hostname, &plugin->bind6_address->sin6_addr)<=0)
2537 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
2539 _("Misconfigured address to bind to in configuration!\n"),
2541 GNUNET_free(plugin->bind6_address);
2542 GNUNET_free(plugin->bind_hostname);
2543 plugin->bind_hostname = NULL;
2544 plugin->bind6_address = NULL;
2549 /* read key & certificates from file */
2550 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Loading TLS certificate `%s' `%s'\n", key_file, cert_file);
2552 plugin->key = load_certificate( key_file );
2553 plugin->cert = load_certificate( cert_file );
2555 if ((plugin->key==NULL) || (plugin->cert==NULL))
2559 GNUNET_asprintf(&cmd,"gnunet-transport-certificate-creation %s %s", key_file, cert_file);
2560 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "No usable TLS certificate found, creating certificate \n");
2565 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
2567 _("Could not create a new TLS certificate, shell script `%s' failed!\n"),cmd,
2569 GNUNET_free (key_file);
2570 GNUNET_free (cert_file);
2571 libgnunet_plugin_transport_https_done(api);
2578 plugin->key = load_certificate( key_file );
2579 plugin->cert = load_certificate( cert_file );
2581 if ((plugin->key==NULL) || (plugin->cert==NULL))
2583 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
2585 _("No usable TLS certificate found and creating one failed! \n"),
2587 GNUNET_free (key_file);
2588 GNUNET_free (cert_file);
2589 libgnunet_plugin_transport_https_done(api);
2594 GNUNET_free (key_file);
2595 GNUNET_free (cert_file);
2598 GNUNET_assert((plugin->key!=NULL) && (plugin->cert!=NULL));
2599 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "TLS certificate loaded\n", key_file, cert_file);
2601 GNUNET_assert ((port > 0) && (port <= 65535));
2602 plugin->port_inbound = port;
2603 gn_timeout = GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT;
2604 unsigned int timeout = (gn_timeout.value) / 1000;
2605 if ((plugin->http_server_daemon_v6 == NULL) && (plugin->use_ipv6 == GNUNET_YES) && (port != 0))
2607 struct sockaddr * tmp = (struct sockaddr *) plugin->bind6_address;
2608 plugin->http_server_daemon_v6 = MHD_start_daemon (
2609 #if DEBUG_CONNECTIONS
2612 MHD_USE_IPv6 | MHD_USE_SSL,
2615 plugin , &mdh_access_cb, plugin,
2616 /*MHD_OPTION_HTTPS_PRIORITIES, "NORMAL:",*/
2617 /*MHD_OPTION_HTTPS_PRIORITIES, "PERFORMANCE:",*/
2618 /* MHD_OPTION_HTTPS_PRIORITIES, "NONE:+VERS-TLS1.0:+ARCFOUR-128:+SHA1:+RSA:+COMP-NULL", */
2619 /*MHD_OPTION_HTTPS_PRIORITIES, "NONE:+VERS-TLS1.0:+ARCFOUR-128:+MD5:+RSA:+COMP-NULL",*/
2620 MHD_OPTION_HTTPS_PRIORITIES, plugin->crypto_init,
2621 MHD_OPTION_HTTPS_MEM_KEY, plugin->key,
2622 MHD_OPTION_HTTPS_MEM_CERT, plugin->cert,
2623 MHD_OPTION_SOCK_ADDR, tmp,
2624 MHD_OPTION_CONNECTION_LIMIT, (unsigned int) 32,
2625 //MHD_OPTION_PER_IP_CONNECTION_LIMIT, (unsigned int) 6,
2626 MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) timeout,
2627 MHD_OPTION_CONNECTION_MEMORY_LIMIT, (size_t) (16 * 1024),
2628 MHD_OPTION_NOTIFY_COMPLETED, &mhd_termination_cb, NULL,
2631 if ((plugin->http_server_daemon_v4 == NULL) && (plugin->use_ipv4 == GNUNET_YES) && (port != 0))
2633 plugin->http_server_daemon_v4 = MHD_start_daemon (
2634 #if DEBUG_CONNECTIONS
2637 MHD_NO_FLAG | MHD_USE_SSL,
2640 plugin , &mdh_access_cb, plugin,
2641 /*MHD_OPTION_HTTPS_PRIORITIES, "NORMAL:",*/
2642 /*MHD_OPTION_HTTPS_PRIORITIES, "PERFORMANCE:",*/
2643 /* MHD_OPTION_HTTPS_PRIORITIES, "NONE:+VERS-TLS1.0:+ARCFOUR-128:+SHA1:+RSA:+COMP-NULL", */
2644 /*MHD_OPTION_HTTPS_PRIORITIES, "NONE:+VERS-TLS1.0:+ARCFOUR-128:+MD5:+RSA:+COMP-NULL",*/
2645 MHD_OPTION_HTTPS_PRIORITIES, plugin->crypto_init,
2646 MHD_OPTION_HTTPS_MEM_KEY, plugin->key,
2647 MHD_OPTION_HTTPS_MEM_CERT, plugin->cert,
2648 MHD_OPTION_SOCK_ADDR, (struct sockaddr_in *)plugin->bind4_address,
2649 MHD_OPTION_CONNECTION_LIMIT, (unsigned int) 32,
2650 //MHD_OPTION_PER_IP_CONNECTION_LIMIT, (unsigned int) 6,
2651 MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) timeout,
2652 MHD_OPTION_CONNECTION_MEMORY_LIMIT, (size_t) (16 * 1024),
2653 MHD_OPTION_NOTIFY_COMPLETED, &mhd_termination_cb, NULL,
2656 if (plugin->http_server_daemon_v4 != NULL)
2657 plugin->http_server_task_v4 = http_server_daemon_prepare (plugin, plugin->http_server_daemon_v4);
2658 if (plugin->http_server_daemon_v6 != NULL)
2659 plugin->http_server_task_v6 = http_server_daemon_prepare (plugin, plugin->http_server_daemon_v6);
2662 if (plugin->http_server_task_v4 != GNUNET_SCHEDULER_NO_TASK)
2665 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);
2668 else if ((plugin->http_server_task_v6 != GNUNET_SCHEDULER_NO_TASK) && (plugin->http_server_task_v4 != GNUNET_SCHEDULER_NO_TASK))
2671 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);
2674 else if ((plugin->http_server_task_v6 != GNUNET_SCHEDULER_NO_TASK) && (plugin->http_server_task_v4 == GNUNET_SCHEDULER_NO_TASK))
2677 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);
2683 if ((plugin->use_ipv6 == GNUNET_YES) && (plugin->use_ipv4 == GNUNET_YES))
2684 GNUNET_asprintf(&tmp,"with IPv4 and IPv6 enabled");
2685 if ((plugin->use_ipv6 == GNUNET_NO) && (plugin->use_ipv4 == GNUNET_YES))
2686 GNUNET_asprintf(&tmp,"with IPv4 enabled");
2687 if ((plugin->use_ipv6 == GNUNET_YES) && (plugin->use_ipv4 == GNUNET_NO))
2688 GNUNET_asprintf(&tmp,"with IPv6 enabled");
2689 if ((plugin->use_ipv6 == GNUNET_NO) && (plugin->use_ipv4 == GNUNET_NO))
2690 GNUNET_asprintf(&tmp,"with NO IP PROTOCOL enabled");
2691 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,"HTTPS Server with %s could not be started on port %u! https plugin failed!\n",tmp, port);
2693 libgnunet_plugin_transport_https_done (api);
2697 /* Initializing cURL */
2698 curl_global_init(CURL_GLOBAL_ALL);
2699 plugin->multi_handle = curl_multi_init();
2701 if ( NULL == plugin->multi_handle )
2703 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
2705 _("Could not initialize curl multi handle, failed to start http plugin!\n"),
2707 libgnunet_plugin_transport_https_done (api);
2711 plugin->peers = GNUNET_CONTAINER_multihashmap_create (10);
2712 GNUNET_OS_network_interfaces_list (&process_interfaces, plugin);
2717 /* end of plugin_transport_https.c */