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 */
381 * Function called for a quick conversion of the binary address to
382 * a numeric address. Note that the caller must not free the
383 * address and that the next call to this function is allowed
384 * to override the address again.
387 * @param addr binary address
388 * @param addrlen length of the address
389 * @return string representing the same address
392 http_plugin_address_to_string (void *cls,
398 * Call MHD to process pending ipv4 requests and then go back
399 * and schedule the next run.
401 static void http_server_daemon_v4_run (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
403 * Call MHD to process pending ipv6 requests and then go back
404 * and schedule the next run.
406 static void http_server_daemon_v6_run (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
409 * Function setting up curl handle and selecting message to send
411 * @param ses session to send data to
412 * @param con connection
413 * @return bytes sent to peer
415 static ssize_t send_check_connections (void *cls, struct Session *ps);
418 * Function setting up file descriptors and scheduling task to run
420 * @param ses session to send data to
423 static int curl_schedule(void *cls );
427 static char * create_url(void * cls, const void * addr, size_t addrlen, size_t id)
429 struct Plugin *plugin = cls;
432 GNUNET_assert ((addr!=NULL) && (addrlen != 0));
433 GNUNET_asprintf(&url,
434 "%s://%s/%s;%u", PROTOCOL_PREFIX,
435 http_plugin_address_to_string(NULL, addr, addrlen),
436 (char *) (&plugin->my_ascii_hash_ident),id);
442 * Removes a message from the linked list of messages
443 * @param con connection to remove message from
444 * @param msg message to remove
445 * @return GNUNET_SYSERR if msg not found, GNUNET_OK on success
447 static int remove_http_message (struct Session * ps, struct HTTP_Message * msg)
449 GNUNET_CONTAINER_DLL_remove(ps->pending_msgs_head,ps->pending_msgs_tail,msg);
454 int remove_peer_context_Iterator (void *cls, const GNUNET_HashCode *key, void *value);
457 * Removes a session from the linked list of sessions
458 * @param pc peer context
460 * @param call_msg_cont GNUNET_YES to call pending message continuations, otherwise no
461 * @param call_msg_cont_result, result to call message continuations with
462 * @return GNUNET_SYSERR if msg not found, GNUNET_OK on success
464 static int remove_session (struct HTTP_PeerContext * pc, struct Session * ps, int call_msg_cont, int call_msg_cont_result)
466 struct HTTP_Message * msg;
467 struct Plugin * plugin = ps->peercontext->plugin;
469 #if DEBUG_CONNECTIONS
470 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);
472 plugin->env->session_end(plugin, &pc->identity, ps);
474 GNUNET_free_non_null (ps->addr);
475 GNUNET_SERVER_mst_destroy (ps->msgtok);
476 GNUNET_free(ps->url);
478 if (ps->direction==INBOUND)
480 if (ps->recv_endpoint != NULL)
482 curl_easy_cleanup(ps->recv_endpoint);
483 ps->recv_endpoint = NULL;
485 if (ps->send_endpoint != NULL)
487 curl_easy_cleanup(ps->send_endpoint);
488 ps->send_endpoint = NULL;
492 msg = ps->pending_msgs_head;
495 if ((call_msg_cont == GNUNET_YES) && (msg->transmit_cont!=NULL))
497 msg->transmit_cont (msg->transmit_cont_cls,&pc->identity,call_msg_cont_result);
499 GNUNET_CONTAINER_DLL_remove(ps->pending_msgs_head,ps->pending_msgs_head,msg);
501 msg = ps->pending_msgs_head;
504 GNUNET_CONTAINER_DLL_remove(pc->head,pc->tail,ps);
508 /* no sessions left remove peer */
512 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"No sessions left for peer `%s', removing context\n",GNUNET_i2s(&pc->identity));
514 remove_peer_context_Iterator(plugin, &pc->identity.hashPubKey, pc);
520 int remove_peer_context_Iterator (void *cls, const GNUNET_HashCode *key, void *value)
522 struct Plugin *plugin = cls;
523 struct HTTP_PeerContext * pc = value;
524 struct Session * ps = pc->head;
525 struct Session * tmp = NULL;
526 struct HTTP_Message * msg = NULL;
527 struct HTTP_Message * msg_tmp = NULL;
529 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Freeing context for peer `%s'\n",GNUNET_i2s(&pc->identity));
531 GNUNET_CONTAINER_multihashmap_remove (plugin->peers, &pc->identity.hashPubKey, pc);
534 plugin->env->session_end(plugin, &pc->identity, ps);
537 GNUNET_free_non_null (ps->addr);
538 GNUNET_free(ps->url);
539 if (ps->msgtok != NULL)
540 GNUNET_SERVER_mst_destroy (ps->msgtok);
542 msg = ps->pending_msgs_head;
549 if (ps->direction==OUTBOUND)
551 if (ps->send_endpoint!=NULL)
552 curl_easy_cleanup(ps->send_endpoint);
553 if (ps->recv_endpoint!=NULL)
554 curl_easy_cleanup(ps->recv_endpoint);
561 GNUNET_STATISTICS_update (plugin->env->stats,
562 gettext_noop ("# HTTP peers active"),
569 * Add the IP of our network interface to the list of
570 * our external IP addresses.
572 * @param cls the 'struct Plugin*'
573 * @param name name of the interface
574 * @param isDefault do we think this may be our default interface
575 * @param addr address of the interface
576 * @param addrlen number of bytes in addr
577 * @return GNUNET_OK to continue iterating
580 process_interfaces (void *cls,
583 const struct sockaddr *addr, socklen_t addrlen)
585 struct Plugin *plugin = cls;
586 struct IPv4HttpAddress * t4;
587 struct IPv6HttpAddress * t6;
591 GNUNET_assert(cls !=NULL);
592 af = addr->sa_family;
593 if ((af == AF_INET) && (plugin->use_ipv4 == GNUNET_YES) && (plugin->bind6_address == NULL))
595 struct in_addr bnd_cmp = ((struct sockaddr_in *) addr)->sin_addr;
596 t4 = GNUNET_malloc(sizeof(struct IPv4HttpAddress));
597 /* Not skipping loopback addresses
598 if (INADDR_LOOPBACK == ntohl(((struct sockaddr_in *) addr)->sin_addr.s_addr))
604 t4->ipv4_addr = ((struct sockaddr_in *) addr)->sin_addr.s_addr;
605 t4->u_port = htons (plugin->port_inbound);
606 if (plugin->bind4_address != NULL)
608 if (0 == memcmp(&plugin->bind4_address->sin_addr, &bnd_cmp, sizeof (struct in_addr)))
610 plugin->env->notify_address(plugin->env->cls,"https",t4, sizeof (struct IPv4HttpAddress), GNUNET_TIME_UNIT_FOREVER_REL);
615 plugin->env->notify_address(plugin->env->cls,"https",t4, sizeof (struct IPv4HttpAddress), GNUNET_TIME_UNIT_FOREVER_REL);
619 else if ((af == AF_INET6) && (plugin->use_ipv6 == GNUNET_YES) && (plugin->bind4_address == NULL))
621 struct in6_addr bnd_cmp6 = ((struct sockaddr_in6 *) addr)->sin6_addr;
622 if (IN6_IS_ADDR_LINKLOCAL (&((struct sockaddr_in6 *) addr)->sin6_addr))
626 t6 = GNUNET_malloc(sizeof(struct IPv6HttpAddress));
627 GNUNET_assert(t6 != NULL);
628 if (plugin->bind6_address != NULL)
630 if (0 == memcmp(&plugin->bind6_address->sin6_addr, &bnd_cmp6, sizeof (struct in6_addr)))
632 memcpy (&t6->ipv6_addr,
633 &((struct sockaddr_in6 *) addr)->sin6_addr,
634 sizeof (struct in6_addr));
635 t6->u6_port = htons (plugin->port_inbound);
636 plugin->env->notify_address(plugin->env->cls,"https",t6,sizeof (struct IPv6HttpAddress) , GNUNET_TIME_UNIT_FOREVER_REL);
641 memcpy (&t6->ipv6_addr,
642 &((struct sockaddr_in6 *) addr)->sin6_addr,
643 sizeof (struct in6_addr));
644 t6->u6_port = htons (plugin->port_inbound);
645 plugin->env->notify_address(plugin->env->cls,"https",t6,sizeof (struct IPv6HttpAddress) , GNUNET_TIME_UNIT_FOREVER_REL);
652 void mhd_logger (void * arg, const char * fmt, va_list ap)
655 vsnprintf(text, 1024, fmt, ap);
657 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,"MHD: %s \n", text);
661 * Callback called by MHD when a connection is terminated
663 static void mhd_termination_cb (void *cls, struct MHD_Connection * connection, void **httpSessionCache)
665 struct Session * ps = *httpSessionCache;
668 struct HTTP_PeerContext * pc = ps->peercontext;
670 if (connection==ps->recv_endpoint)
672 #if DEBUG_CONNECTIONS
673 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: inbound connection from peer `%s' was terminated\n", ps, GNUNET_i2s(&pc->identity));
675 ps->recv_active = GNUNET_NO;
676 ps->recv_connected = GNUNET_NO;
677 ps->recv_endpoint = NULL;
679 if (connection==ps->send_endpoint)
682 ps->send_active = GNUNET_NO;
683 ps->send_connected = GNUNET_NO;
684 ps->send_endpoint = NULL;
685 #if DEBUG_CONNECTIONS
686 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: outbound connection from peer `%s' was terminated\n", ps, GNUNET_i2s(&pc->identity));
690 /* if both connections disconnected, remove session */
691 if ((ps->send_connected == GNUNET_NO) && (ps->recv_connected == GNUNET_NO))
693 GNUNET_STATISTICS_update (pc->plugin->env->stats,
694 gettext_noop ("# HTTPS inbound sessions for peers active"),
697 remove_session(pc,ps,GNUNET_YES,GNUNET_SYSERR);
701 static void mhd_write_mst_cb (void *cls,
703 const struct GNUNET_MessageHeader *message)
706 struct Session *ps = cls;
707 GNUNET_assert(ps != NULL);
709 struct HTTP_PeerContext *pc = ps->peercontext;
710 GNUNET_assert(pc != NULL);
712 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
713 "Connection %X: Forwarding message to transport service, type %u and size %u from `%s' (`%s')\n",
715 ntohs(message->type),
716 ntohs(message->size),
717 GNUNET_i2s(&(ps->peercontext)->identity),http_plugin_address_to_string(NULL,ps->addr,ps->addrlen));
719 pc->plugin->env->receive (ps->peercontext->plugin->env->cls,
727 * Check if ip is allowed to connect.
730 mhd_accept_cb (void *cls,
731 const struct sockaddr *addr, socklen_t addr_len)
734 struct Plugin *plugin = cls;
736 /* Every connection is accepted, nothing more to do here */
740 int mhd_send_callback (void *cls, uint64_t pos, char *buf, int max)
744 struct Session * ps = cls;
745 struct HTTP_PeerContext * pc;
746 struct HTTP_Message * msg;
748 GNUNET_assert (ps!=NULL);
749 pc = ps->peercontext;
750 msg = ps->pending_msgs_tail;
751 if (ps->send_force_disconnect==GNUNET_YES)
753 #if DEBUG_CONNECTIONS
754 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: outbound forced to disconnect\n",ps);
761 if ((msg->size-msg->pos) <= max)
763 memcpy(buf,&msg->buf[msg->pos],(msg->size-msg->pos));
764 bytes_read = msg->size-msg->pos;
765 msg->pos+=(msg->size-msg->pos);
769 memcpy(buf,&msg->buf[msg->pos],max);
774 if (msg->pos==msg->size)
776 if (NULL!=msg->transmit_cont)
777 msg->transmit_cont (msg->transmit_cont_cls,&pc->identity,GNUNET_OK);
778 remove_http_message(ps,msg);
785 * Process GET or PUT request received via MHD. For
786 * GET, queue response that will send back our pending
787 * messages. For PUT, process incoming data and send
788 * to GNUnet core. In either case, check if a session
789 * already exists and create a new one if not.
792 mdh_access_cb (void *cls,
793 struct MHD_Connection *mhd_connection,
797 const char *upload_data,
798 size_t * upload_data_size, void **httpSessionCache)
800 struct Plugin *plugin = cls;
801 struct MHD_Response *response;
802 const union MHD_ConnectionInfo * conn_info;
804 struct sockaddr_in *addrin;
805 struct sockaddr_in6 *addrin6;
807 char address[INET6_ADDRSTRLEN+14];
808 struct GNUNET_PeerIdentity pi_in;
811 struct IPv4HttpAddress ipv4addr;
812 struct IPv6HttpAddress ipv6addr;
814 struct HTTP_PeerContext *pc;
815 struct Session *ps = NULL;
816 struct Session *ps_tmp = NULL;
819 int send_error_to_client;
823 GNUNET_assert(cls !=NULL);
824 send_error_to_client = GNUNET_NO;
826 if (NULL == *httpSessionCache)
828 /* check url for peer identity , if invalid send HTTP 404*/
829 size_t len = strlen(&url[1]);
830 char * peer = GNUNET_malloc(104+1);
832 if ((len>104) && (url[104]==';'))
834 char * id = GNUNET_malloc((len-104)+1);
835 strcpy(id,&url[105]);
836 memcpy(peer,&url[1],103);
838 id_num = strtoul ( id, NULL , 10);
841 res = GNUNET_CRYPTO_hash_from_string (peer, &(pi_in.hashPubKey));
843 if ( GNUNET_SYSERR == res )
845 response = MHD_create_response_from_data (strlen (HTTP_ERROR_RESPONSE),HTTP_ERROR_RESPONSE, MHD_NO, MHD_NO);
846 res = MHD_queue_response (mhd_connection, MHD_HTTP_NOT_FOUND, response);
847 MHD_destroy_response (response);
848 #if DEBUG_CONNECTIONS
850 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Peer has no valid ident, sent HTTP 1.1/404\n");
852 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Peer has no valid ident, could not send error\n");
859 ps = *httpSessionCache;
860 pc = ps->peercontext;
863 if (NULL == *httpSessionCache)
865 /* get peer context */
866 pc = GNUNET_CONTAINER_multihashmap_get (plugin->peers, &pi_in.hashPubKey);
870 pc = GNUNET_malloc(sizeof (struct HTTP_PeerContext));
872 pc->session_id_counter=1;
873 pc->last_session = NULL;
874 memcpy(&pc->identity, &pi_in, sizeof(struct GNUNET_PeerIdentity));
875 GNUNET_CONTAINER_multihashmap_put(plugin->peers, &pc->identity.hashPubKey, pc, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
876 GNUNET_STATISTICS_update (plugin->env->stats,
877 gettext_noop ("# HTTP peers active"),
882 conn_info = MHD_get_connection_info(mhd_connection, MHD_CONNECTION_INFO_CLIENT_ADDRESS );
883 /* Incoming IPv4 connection */
884 if ( AF_INET == conn_info->client_addr->sin_family)
886 addrin = conn_info->client_addr;
887 inet_ntop(addrin->sin_family, &(addrin->sin_addr),address,INET_ADDRSTRLEN);
888 memcpy(&ipv4addr.ipv4_addr,&(addrin->sin_addr),sizeof(struct in_addr));
889 ipv4addr.u_port = addrin->sin_port;
891 addr_len = sizeof(struct IPv4HttpAddress);
893 /* Incoming IPv6 connection */
894 if ( AF_INET6 == conn_info->client_addr->sin_family)
896 addrin6 = (struct sockaddr_in6 *) conn_info->client_addr;
897 inet_ntop(addrin6->sin6_family, &(addrin6->sin6_addr),address,INET6_ADDRSTRLEN);
898 memcpy(&ipv6addr.ipv6_addr,&(addrin6->sin6_addr),sizeof(struct in6_addr));
899 ipv6addr.u6_port = addrin6->sin6_port;
901 addr_len = sizeof(struct IPv6HttpAddress);
904 GNUNET_assert (addr != NULL);
905 GNUNET_assert (addr_len != 0);
908 /* only inbound sessions here */
913 if ((ps_tmp->direction==INBOUND) && (ps_tmp->session_id == id_num) && (id_num!=0))
915 if ((ps_tmp->recv_force_disconnect!=GNUNET_YES) && (ps_tmp->send_force_disconnect!=GNUNET_YES))
924 ps = GNUNET_malloc(sizeof (struct Session));
925 ps->addr = GNUNET_malloc(addr_len);
926 memcpy(ps->addr,addr,addr_len);
927 ps->addrlen = addr_len;
928 ps->direction=INBOUND;
929 ps->pending_msgs_head = NULL;
930 ps->pending_msgs_tail = NULL;
931 ps->send_connected=GNUNET_NO;
932 ps->send_active=GNUNET_NO;
933 ps->recv_connected=GNUNET_NO;
934 ps->recv_active=GNUNET_NO;
936 ps->session_id =id_num;
937 ps->url = create_url (plugin, ps->addr, ps->addrlen, ps->session_id);
938 GNUNET_CONTAINER_DLL_insert(pc->head,pc->tail,ps);
939 GNUNET_STATISTICS_update (plugin->env->stats,
940 gettext_noop ("# HTTPS inbound sessions for peers active"),
945 *httpSessionCache = ps;
946 if (ps->msgtok==NULL)
947 ps->msgtok = GNUNET_SERVER_mst_create (&mhd_write_mst_cb, ps);
949 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: HTTPS Daemon has new an incoming `%s' request from peer `%s' (`%s')\n",
952 GNUNET_i2s(&pc->identity),
953 http_plugin_address_to_string(NULL, ps->addr, ps->addrlen));
957 /* Is it a PUT or a GET request */
958 if (0 == strcmp (MHD_HTTP_METHOD_PUT, method))
960 if (ps->recv_force_disconnect == GNUNET_YES)
962 #if DEBUG_CONNECTIONS
963 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: inbound connection was forced to disconnect\n",ps);
965 ps->recv_active = GNUNET_NO;
968 if ((*upload_data_size == 0) && (ps->recv_active==GNUNET_NO))
970 ps->recv_endpoint = mhd_connection;
971 ps->recv_connected = GNUNET_YES;
972 ps->recv_active = GNUNET_YES;
973 ps->recv_force_disconnect = GNUNET_NO;
974 #if DEBUG_CONNECTIONS
975 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: inbound PUT connection connected\n",ps);
980 /* Transmission of all data complete */
981 if ((*upload_data_size == 0) && (ps->recv_active == GNUNET_YES))
983 response = MHD_create_response_from_data (strlen (HTTP_PUT_RESPONSE),HTTP_PUT_RESPONSE, MHD_NO, MHD_NO);
984 res = MHD_queue_response (mhd_connection, MHD_HTTP_OK, response);
985 #if DEBUG_CONNECTIONS
986 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: Sent HTTP/1.1: 200 OK as PUT Response\n",ps);
988 MHD_destroy_response (response);
989 ps->recv_active=GNUNET_NO;
994 if ((*upload_data_size > 0) && (ps->recv_active == GNUNET_YES))
996 res = GNUNET_SERVER_mst_receive(ps->msgtok, ps, upload_data,*upload_data_size, GNUNET_NO, GNUNET_NO);
997 (*upload_data_size) = 0;
1003 if ( 0 == strcmp (MHD_HTTP_METHOD_GET, method) )
1005 if (ps->send_force_disconnect == GNUNET_YES)
1007 #if DEBUG_CONNECTIONS
1008 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: outbound connection was forced to disconnect\n",ps);
1010 ps->send_active = GNUNET_NO;
1013 ps->send_connected = GNUNET_YES;
1014 ps->send_active = GNUNET_YES;
1015 ps->send_endpoint = mhd_connection;
1016 ps->send_force_disconnect = GNUNET_NO;
1017 #if DEBUG_CONNECTIONS
1018 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: inbound GET connection connected\n",ps);
1020 response = MHD_create_response_from_callback(-1,32 * 1024, &mhd_send_callback, ps, NULL);
1021 res = MHD_queue_response (mhd_connection, MHD_HTTP_OK, response);
1022 MHD_destroy_response (response);
1029 * Function that queries MHD's select sets and
1030 * starts the task waiting for them.
1032 static GNUNET_SCHEDULER_TaskIdentifier
1033 http_server_daemon_prepare (void * cls, struct MHD_Daemon *daemon_handle)
1035 struct Plugin *plugin = cls;
1036 GNUNET_SCHEDULER_TaskIdentifier ret;
1040 struct GNUNET_NETWORK_FDSet *wrs;
1041 struct GNUNET_NETWORK_FDSet *wws;
1042 struct GNUNET_NETWORK_FDSet *wes;
1044 unsigned long long timeout;
1046 struct GNUNET_TIME_Relative tv;
1048 GNUNET_assert(cls !=NULL);
1049 ret = GNUNET_SCHEDULER_NO_TASK;
1053 wrs = GNUNET_NETWORK_fdset_create ();
1054 wes = GNUNET_NETWORK_fdset_create ();
1055 wws = GNUNET_NETWORK_fdset_create ();
1057 GNUNET_assert (MHD_YES ==
1058 MHD_get_fdset (daemon_handle,
1063 haveto = MHD_get_timeout (daemon_handle, &timeout);
1064 if (haveto == MHD_YES)
1065 tv.value = (uint64_t) timeout;
1067 tv = GNUNET_TIME_UNIT_FOREVER_REL;
1068 GNUNET_NETWORK_fdset_copy_native (wrs, &rs, max);
1069 GNUNET_NETWORK_fdset_copy_native (wws, &ws, max);
1070 GNUNET_NETWORK_fdset_copy_native (wes, &es, max);
1071 if (daemon_handle == plugin->http_server_daemon_v4)
1073 if (plugin->http_server_task_v4 != GNUNET_SCHEDULER_NO_TASK)
1075 GNUNET_SCHEDULER_cancel(plugin->env->sched, plugin->http_server_task_v4);
1076 plugin->http_server_daemon_v4 = GNUNET_SCHEDULER_NO_TASK;
1079 ret = GNUNET_SCHEDULER_add_select (plugin->env->sched,
1080 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1081 GNUNET_SCHEDULER_NO_TASK,
1085 &http_server_daemon_v4_run,
1088 if (daemon_handle == plugin->http_server_daemon_v6)
1090 if (plugin->http_server_task_v6 != GNUNET_SCHEDULER_NO_TASK)
1092 GNUNET_SCHEDULER_cancel(plugin->env->sched, plugin->http_server_task_v6);
1093 plugin->http_server_task_v6 = GNUNET_SCHEDULER_NO_TASK;
1096 ret = GNUNET_SCHEDULER_add_select (plugin->env->sched,
1097 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1098 GNUNET_SCHEDULER_NO_TASK,
1102 &http_server_daemon_v6_run,
1105 GNUNET_NETWORK_fdset_destroy (wrs);
1106 GNUNET_NETWORK_fdset_destroy (wws);
1107 GNUNET_NETWORK_fdset_destroy (wes);
1112 * Call MHD to process pending requests and then go back
1113 * and schedule the next run.
1115 static void http_server_daemon_v4_run (void *cls,
1116 const struct GNUNET_SCHEDULER_TaskContext *tc)
1118 struct Plugin *plugin = cls;
1120 GNUNET_assert(cls !=NULL);
1121 plugin->http_server_task_v4 = GNUNET_SCHEDULER_NO_TASK;
1123 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1126 GNUNET_assert (MHD_YES == MHD_run (plugin->http_server_daemon_v4));
1127 plugin->http_server_task_v4 = http_server_daemon_prepare (plugin, plugin->http_server_daemon_v4);
1133 * Call MHD to process pending requests and then go back
1134 * and schedule the next run.
1136 static void http_server_daemon_v6_run (void *cls,
1137 const struct GNUNET_SCHEDULER_TaskContext *tc)
1139 struct Plugin *plugin = cls;
1141 GNUNET_assert(cls !=NULL);
1142 plugin->http_server_task_v6 = GNUNET_SCHEDULER_NO_TASK;
1144 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1147 GNUNET_assert (MHD_YES == MHD_run (plugin->http_server_daemon_v6));
1148 plugin->http_server_task_v6 = http_server_daemon_prepare (plugin, plugin->http_server_daemon_v6);
1152 static size_t curl_get_header_cb( void *ptr, size_t size, size_t nmemb, void *stream)
1154 struct Session * ps = stream;
1156 long http_result = 0;
1158 /* Getting last http result code */
1159 GNUNET_assert(NULL!=ps);
1160 if (ps->recv_connected==GNUNET_NO)
1162 res = curl_easy_getinfo(ps->recv_endpoint, CURLINFO_RESPONSE_CODE, &http_result);
1163 if (CURLE_OK == res)
1165 if (http_result == 200)
1167 ps->recv_connected = GNUNET_YES;
1168 ps->recv_active = GNUNET_YES;
1169 #if DEBUG_CONNECTIONS
1170 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: connected to recieve data\n",ps);
1172 // Calling send_check_connections again since receive is established
1173 send_check_connections (ps->peercontext->plugin, ps);
1180 size_t len = size * nmemb;
1183 if ((size * nmemb) < SIZE_MAX)
1184 tmp = GNUNET_malloc (len+1);
1186 if ((tmp != NULL) && (len > 0))
1188 memcpy(tmp,ptr,len);
1191 if (tmp[len-2] == 13)
1195 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: Header: %s\n",ps,tmp);
1202 return size * nmemb;
1205 static size_t curl_put_header_cb( void *ptr, size_t size, size_t nmemb, void *stream)
1207 struct Session * ps = stream;
1210 size_t len = size * nmemb;
1211 long http_result = 0;
1214 /* Getting last http result code */
1215 GNUNET_assert(NULL!=ps);
1216 res = curl_easy_getinfo(ps->send_endpoint, CURLINFO_RESPONSE_CODE, &http_result);
1217 if (CURLE_OK == res)
1219 if ((http_result == 100) && (ps->send_connected==GNUNET_NO))
1221 ps->send_connected = GNUNET_YES;
1222 ps->send_active = GNUNET_YES;
1223 #if DEBUG_CONNECTIONS
1224 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: connected to send data\n",ps);
1227 if ((http_result == 200) && (ps->send_connected==GNUNET_YES))
1229 ps->send_connected = GNUNET_NO;
1230 ps->send_active = GNUNET_NO;
1231 #if DEBUG_CONNECTIONS
1232 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: sending disconnected\n",ps);
1238 if ((size * nmemb) < SIZE_MAX)
1239 tmp = GNUNET_malloc (len+1);
1241 if ((tmp != NULL) && (len > 0))
1243 memcpy(tmp,ptr,len);
1246 if (tmp[len-2] == 13)
1253 return size * nmemb;
1257 * Callback method used with libcurl
1258 * Method is called when libcurl needs to read data during sending
1259 * @param stream pointer where to write data
1260 * @param size size of an individual element
1261 * @param nmemb count of elements that can be written to the buffer
1262 * @param ptr source pointer, passed to the libcurl handle
1263 * @return bytes written to stream
1265 static size_t curl_send_cb(void *stream, size_t size, size_t nmemb, void *ptr)
1267 struct Session * ps = ptr;
1268 struct HTTP_Message * msg = ps->pending_msgs_tail;
1272 if (ps->send_active == GNUNET_NO)
1273 return CURL_READFUNC_PAUSE;
1275 if ((ps->pending_msgs_tail == NULL) && (ps->send_active == GNUNET_YES))
1277 #if DEBUG_CONNECTIONS
1278 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: No Message to send, pausing connection\n",ps);
1280 ps->send_active = GNUNET_NO;
1281 return CURL_READFUNC_PAUSE;
1284 GNUNET_assert (msg!=NULL);
1287 if (msg->pos < msg->size)
1289 /* data fit in buffer */
1290 if ((msg->size - msg->pos) <= (size * nmemb))
1292 len = (msg->size - msg->pos);
1293 memcpy(stream, &msg->buf[msg->pos], len);
1300 memcpy(stream, &msg->buf[msg->pos], len);
1305 /* no data to send */
1311 if ( msg->pos == msg->size)
1314 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: Message with %u bytes sent, removing message from queue \n",ps, msg->pos);
1316 /* Calling transmit continuation */
1317 if (NULL != msg->transmit_cont)
1318 msg->transmit_cont (msg->transmit_cont_cls,&(ps->peercontext)->identity,GNUNET_OK);
1319 remove_http_message(ps, msg);
1324 static void curl_receive_mst_cb (void *cls,
1326 const struct GNUNET_MessageHeader *message)
1328 struct Session *ps = cls;
1329 GNUNET_assert(ps != NULL);
1331 struct HTTP_PeerContext *pc = ps->peercontext;
1332 GNUNET_assert(pc != NULL);
1335 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1336 "Connection %X: Forwarding message to transport service, type %u and size %u from `%s' (`%s')\n",
1338 ntohs(message->type),
1339 ntohs(message->size),
1340 GNUNET_i2s(&(pc->identity)),http_plugin_address_to_string(NULL,ps->addr,ps->addrlen));
1342 pc->plugin->env->receive (pc->plugin->env->cls,
1351 * Callback method used with libcurl
1352 * Method is called when libcurl needs to write data during sending
1353 * @param stream pointer where to write data
1354 * @param size size of an individual element
1355 * @param nmemb count of elements that can be written to the buffer
1356 * @param ptr destination pointer, passed to the libcurl handle
1357 * @return bytes read from stream
1359 static size_t curl_receive_cb( void *stream, size_t size, size_t nmemb, void *ptr)
1361 struct Session * ps = ptr;
1362 #if DEBUG_CONNECTIONS
1363 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: %u bytes received\n",ps, size*nmemb);
1365 GNUNET_SERVER_mst_receive(ps->msgtok, ps, stream, size*nmemb, GNUNET_NO, GNUNET_NO);
1366 return (size * nmemb);
1370 static void curl_perform (void *cls,
1371 const struct GNUNET_SCHEDULER_TaskContext *tc)
1373 struct Plugin *plugin = cls;
1374 static unsigned int handles_last_run;
1376 struct CURLMsg *msg;
1378 struct Session *ps = NULL;
1379 struct HTTP_PeerContext *pc = NULL;
1380 struct HTTP_Message * cur_msg = NULL;
1384 GNUNET_assert(cls !=NULL);
1386 plugin->http_curl_task = GNUNET_SCHEDULER_NO_TASK;
1387 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1393 mret = curl_multi_perform (plugin->multi_handle, &running);
1394 if ((running < handles_last_run) && (running>0))
1399 msg = curl_multi_info_read (plugin->multi_handle, &running);
1402 /* get session for affected curl handle */
1403 GNUNET_assert ( msg->easy_handle != NULL );
1404 curl_easy_getinfo(msg->easy_handle, CURLINFO_PRIVATE, &tmp);
1405 ps = (struct Session *) tmp;
1406 GNUNET_assert ( ps != NULL );
1407 pc = ps->peercontext;
1408 GNUNET_assert ( pc != NULL );
1413 if ( (msg->data.result != CURLE_OK) &&
1414 (msg->data.result != CURLE_GOT_NOTHING) )
1416 /* sending msg failed*/
1417 if (msg->easy_handle == ps->send_endpoint)
1419 #if DEBUG_CONNECTIONS
1420 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1421 _("Connection %X: HTTPS PUT to peer `%s' (`%s') failed: `%s' `%s'\n"),
1423 GNUNET_i2s(&pc->identity),
1424 http_plugin_address_to_string(NULL, ps->addr, ps->addrlen),
1425 "curl_multi_perform",
1426 curl_easy_strerror (msg->data.result));
1428 ps->send_connected = GNUNET_NO;
1429 ps->send_active = GNUNET_NO;
1430 curl_multi_remove_handle(plugin->multi_handle,ps->send_endpoint);
1431 //curl_easy_cleanup(ps->send_endpoint);
1432 //ps->send_endpoint=NULL;
1433 cur_msg = ps->pending_msgs_tail;
1434 if (( NULL != cur_msg) && ( NULL != cur_msg->transmit_cont))
1435 cur_msg->transmit_cont (cur_msg->transmit_cont_cls,&pc->identity,GNUNET_SYSERR);
1437 /* GET connection failed */
1438 if (msg->easy_handle == ps->recv_endpoint)
1440 #if DEBUG_CONNECTIONS
1441 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1442 _("Connection %X: HTTPS GET to peer `%s' (`%s') failed: `%s' `%s'\n"),
1444 GNUNET_i2s(&pc->identity),
1445 http_plugin_address_to_string(NULL, ps->addr, ps->addrlen),
1446 "curl_multi_perform",
1447 curl_easy_strerror (msg->data.result));
1449 ps->recv_connected = GNUNET_NO;
1450 ps->recv_active = GNUNET_NO;
1451 curl_multi_remove_handle(plugin->multi_handle,ps->recv_endpoint);
1452 //curl_easy_cleanup(ps->recv_endpoint);
1453 //ps->recv_endpoint=NULL;
1458 if (msg->easy_handle == ps->send_endpoint)
1460 GNUNET_assert (CURLE_OK == curl_easy_getinfo(msg->easy_handle, CURLINFO_RESPONSE_CODE, &http_result));
1461 #if DEBUG_CONNECTIONS
1462 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1463 "Connection %X: HTTPS PUT connection to peer `%s' (`%s') was closed with HTTP code %u\n",
1465 GNUNET_i2s(&pc->identity),
1466 http_plugin_address_to_string(NULL, ps->addr, ps->addrlen),
1469 /* Calling transmit continuation */
1470 cur_msg = ps->pending_msgs_tail;
1471 if (( NULL != cur_msg) && (NULL != cur_msg->transmit_cont))
1473 /* HTTP 1xx : Last message before here was informational */
1474 if ((http_result >=100) && (http_result < 200))
1475 cur_msg->transmit_cont (cur_msg->transmit_cont_cls,&pc->identity,GNUNET_OK);
1476 /* HTTP 2xx: successful operations */
1477 if ((http_result >=200) && (http_result < 300))
1478 cur_msg->transmit_cont (cur_msg->transmit_cont_cls,&pc->identity,GNUNET_OK);
1479 /* HTTP 3xx..5xx: error */
1480 if ((http_result >=300) && (http_result < 600))
1481 cur_msg->transmit_cont (cur_msg->transmit_cont_cls,&pc->identity,GNUNET_SYSERR);
1483 ps->send_connected = GNUNET_NO;
1484 ps->send_active = GNUNET_NO;
1485 curl_multi_remove_handle(plugin->multi_handle,ps->send_endpoint);
1486 //curl_easy_cleanup(ps->send_endpoint);
1487 //ps->send_endpoint =NULL;
1489 if (msg->easy_handle == ps->recv_endpoint)
1491 #if DEBUG_CONNECTIONS
1492 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1493 "Connection %X: HTTP GET connection to peer `%s' (`%s') was closed with HTTP code %u\n",
1495 GNUNET_i2s(&pc->identity),
1496 http_plugin_address_to_string(NULL, ps->addr, ps->addrlen),
1499 ps->recv_connected = GNUNET_NO;
1500 ps->recv_active = GNUNET_NO;
1501 curl_multi_remove_handle(plugin->multi_handle,ps->recv_endpoint);
1502 //curl_easy_cleanup(ps->recv_endpoint);
1503 //ps->recv_endpoint=NULL;
1506 if ((ps->recv_connected == GNUNET_NO) && (ps->send_connected == GNUNET_NO))
1507 remove_session (pc, ps, GNUNET_YES, GNUNET_SYSERR);
1514 while ( (running > 0) );
1516 handles_last_run = running;
1518 while (mret == CURLM_CALL_MULTI_PERFORM);
1519 curl_schedule(plugin);
1524 * Function setting up file descriptors and scheduling task to run
1525 * @param ses session to send data to
1526 * @return GNUNET_SYSERR for hard failure, GNUNET_OK for ok
1529 http_plugin_disconnect (void *cls,
1530 const struct GNUNET_PeerIdentity *target)
1534 struct Plugin *plugin = cls;
1535 struct HTTP_PeerContext *pc = NULL;
1536 struct Session *ps = NULL;
1537 //struct Session *tmp = NULL;
1539 pc = GNUNET_CONTAINER_multihashmap_get (plugin->peers, &target->hashPubKey);
1546 /* Telling transport that session is getting disconnected */
1547 plugin->env->session_end(plugin, target, ps);
1548 if (ps->direction==OUTBOUND)
1550 if (ps->send_endpoint!=NULL)
1552 //GNUNET_assert(CURLM_OK == curl_multi_remove_handle(plugin->multi_handle,ps->send_endpoint));
1553 //curl_easy_cleanup(ps->send_endpoint);
1554 //ps->send_endpoint=NULL;
1555 ps->send_force_disconnect = GNUNET_YES;
1557 if (ps->recv_endpoint!=NULL)
1559 //GNUNET_assert(CURLM_OK == curl_multi_remove_handle(plugin->multi_handle,ps->recv_endpoint));
1560 //curl_easy_cleanup(ps->recv_endpoint);
1561 //ps->recv_endpoint=NULL;
1562 ps->recv_force_disconnect = GNUNET_YES;
1566 if (ps->direction==INBOUND)
1568 ps->recv_force_disconnect = GNUNET_YES;
1569 ps->send_force_disconnect = GNUNET_YES;
1572 while (ps->pending_msgs_head!=NULL)
1574 remove_http_message(ps, ps->pending_msgs_head);
1576 ps->recv_active = GNUNET_NO;
1577 ps->send_active = GNUNET_NO;
1583 static int curl_schedule(void *cls)
1585 struct Plugin *plugin = cls;
1590 struct GNUNET_NETWORK_FDSet *grs;
1591 struct GNUNET_NETWORK_FDSet *gws;
1595 GNUNET_assert(cls !=NULL);
1597 /* Cancel previous scheduled task */
1598 if (plugin->http_curl_task != GNUNET_SCHEDULER_NO_TASK)
1600 GNUNET_SCHEDULER_cancel(plugin->env->sched, plugin->http_curl_task);
1601 plugin->http_curl_task = GNUNET_SCHEDULER_NO_TASK;
1607 mret = curl_multi_fdset (plugin->multi_handle, &rs, &ws, &es, &max);
1608 if (mret != CURLM_OK)
1610 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1611 _("%s failed at %s:%d: `%s'\n"),
1612 "curl_multi_fdset", __FILE__, __LINE__,
1613 curl_multi_strerror (mret));
1614 return GNUNET_SYSERR;
1616 mret = curl_multi_timeout (plugin->multi_handle, &to);
1617 if (mret != CURLM_OK)
1619 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1620 _("%s failed at %s:%d: `%s'\n"),
1621 "curl_multi_timeout", __FILE__, __LINE__,
1622 curl_multi_strerror (mret));
1623 return GNUNET_SYSERR;
1626 grs = GNUNET_NETWORK_fdset_create ();
1627 gws = GNUNET_NETWORK_fdset_create ();
1628 GNUNET_NETWORK_fdset_copy_native (grs, &rs, max + 1);
1629 GNUNET_NETWORK_fdset_copy_native (gws, &ws, max + 1);
1630 plugin->http_curl_task = GNUNET_SCHEDULER_add_select (plugin->env->sched,
1631 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1632 GNUNET_SCHEDULER_NO_TASK,
1633 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 0),
1638 GNUNET_NETWORK_fdset_destroy (gws);
1639 GNUNET_NETWORK_fdset_destroy (grs);
1644 * Function setting up curl handle and selecting message to send
1646 * @param ses session to send data to
1647 * @param con connection
1648 * @return GNUNET_SYSERR on failure, GNUNET_NO if connecting, GNUNET_YES if ok
1650 static ssize_t send_check_connections (void *cls, struct Session *ps)
1652 struct Plugin *plugin = cls;
1654 struct HTTP_Message * msg;
1656 struct GNUNET_TIME_Relative timeout = GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT;
1658 GNUNET_assert(cls !=NULL);
1660 if (ps->direction == OUTBOUND)
1662 /* RECV DIRECTION */
1663 /* Check if session is connected to receive data, otherwise connect to peer */
1664 if (ps->recv_connected == GNUNET_NO)
1666 int fresh = GNUNET_NO;
1667 if (ps->recv_endpoint == NULL)
1670 ps->recv_endpoint = curl_easy_init();
1673 curl_easy_setopt(ps->recv_endpoint, CURLOPT_VERBOSE, 1L);
1675 curl_easy_setopt(ps->recv_endpoint, CURLOPT_URL, ps->url);
1676 curl_easy_setopt (ps->recv_endpoint, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1);
1677 //curl_easy_setopt (ps->recv_endpoint, CURLOPT_SSL_CIPHER_LIST, cipher_suite);
1678 curl_easy_setopt(ps->recv_endpoint, CURLOPT_SSL_VERIFYPEER, 0);
1679 curl_easy_setopt(ps->recv_endpoint, CURLOPT_SSL_VERIFYHOST, 0);
1680 curl_easy_setopt(ps->recv_endpoint, CURLOPT_HEADERFUNCTION, &curl_get_header_cb);
1681 curl_easy_setopt(ps->recv_endpoint, CURLOPT_WRITEHEADER, ps);
1682 curl_easy_setopt(ps->recv_endpoint, CURLOPT_READFUNCTION, curl_send_cb);
1683 curl_easy_setopt(ps->recv_endpoint, CURLOPT_READDATA, ps);
1684 curl_easy_setopt(ps->recv_endpoint, CURLOPT_WRITEFUNCTION, curl_receive_cb);
1685 curl_easy_setopt(ps->recv_endpoint, CURLOPT_WRITEDATA, ps);
1686 curl_easy_setopt(ps->recv_endpoint, CURLOPT_TIMEOUT, (long) timeout.value);
1687 curl_easy_setopt(ps->recv_endpoint, CURLOPT_PRIVATE, ps);
1688 curl_easy_setopt(ps->recv_endpoint, CURLOPT_CONNECTTIMEOUT, HTTP_CONNECT_TIMEOUT);
1689 curl_easy_setopt(ps->recv_endpoint, CURLOPT_BUFFERSIZE, GNUNET_SERVER_MAX_MESSAGE_SIZE);
1691 if (fresh==GNUNET_YES)
1693 mret = curl_multi_add_handle(plugin->multi_handle, ps->recv_endpoint);
1694 if (mret != CURLM_OK)
1696 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1697 _("Connection: %X: %s failed at %s:%d: `%s'\n"),
1699 "curl_multi_add_handle", __FILE__, __LINE__,
1700 curl_multi_strerror (mret));
1701 return GNUNET_SYSERR;
1704 if (curl_schedule (plugin) == GNUNET_SYSERR)
1706 #if DEBUG_CONNECTIONS
1707 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: could not schedule curl task\n",ps);
1709 return GNUNET_SYSERR;
1711 #if DEBUG_CONNECTIONS
1712 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: inbound not connected, initiating connection\n",ps);
1716 /* waiting for receive direction */
1717 if (ps->recv_connected==GNUNET_NO)
1720 /* SEND DIRECTION */
1721 /* Check if session is connected to send data, otherwise connect to peer */
1722 if ((ps->send_connected == GNUNET_YES) && (ps->send_endpoint!= NULL))
1724 if (ps->send_active == GNUNET_YES)
1726 #if DEBUG_CONNECTIONS
1727 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: outbound active, enqueueing message\n",ps);
1731 if (ps->send_active == GNUNET_NO)
1733 #if DEBUG_CONNECTIONS
1734 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: outbound paused, unpausing existing connection and enqueueing message\n",ps);
1736 if (CURLE_OK == curl_easy_pause(ps->send_endpoint,CURLPAUSE_CONT))
1738 ps->send_active=GNUNET_YES;
1742 return GNUNET_SYSERR;
1745 /* not connected, initiate connection */
1746 if (ps->send_connected==GNUNET_NO)
1748 int fresh = GNUNET_NO;
1749 if (NULL == ps->send_endpoint)
1751 ps->send_endpoint = curl_easy_init();
1754 GNUNET_assert (ps->send_endpoint != NULL);
1755 GNUNET_assert (NULL != ps->pending_msgs_tail);
1756 #if DEBUG_CONNECTIONS
1757 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: outbound not connected, initiating connection\n",ps);
1759 ps->send_active = GNUNET_NO;
1760 msg = ps->pending_msgs_tail;
1763 curl_easy_setopt(ps->send_endpoint, CURLOPT_VERBOSE, 1L);
1765 curl_easy_setopt(ps->send_endpoint, CURLOPT_URL, ps->url);
1766 curl_easy_setopt(ps->send_endpoint, CURLOPT_PUT, 1L);
1767 curl_easy_setopt(ps->send_endpoint, CURLOPT_HEADERFUNCTION, &curl_put_header_cb);
1768 curl_easy_setopt(ps->send_endpoint, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1);
1769 curl_easy_setopt(ps->send_endpoint, CURLOPT_SSL_VERIFYPEER, 0);
1770 curl_easy_setopt(ps->send_endpoint, CURLOPT_SSL_VERIFYHOST, 0);
1772 curl_easy_setopt(ps->send_endpoint, CURLOPT_WRITEHEADER, ps);
1773 curl_easy_setopt(ps->send_endpoint, CURLOPT_READFUNCTION, curl_send_cb);
1774 curl_easy_setopt(ps->send_endpoint, CURLOPT_READDATA, ps);
1775 curl_easy_setopt(ps->send_endpoint, CURLOPT_WRITEFUNCTION, curl_receive_cb);
1776 curl_easy_setopt(ps->send_endpoint, CURLOPT_READDATA, ps);
1777 curl_easy_setopt(ps->send_endpoint, CURLOPT_TIMEOUT, (long) timeout.value);
1778 curl_easy_setopt(ps->send_endpoint, CURLOPT_PRIVATE, ps);
1779 curl_easy_setopt(ps->send_endpoint, CURLOPT_CONNECTTIMEOUT, HTTP_CONNECT_TIMEOUT);
1780 curl_easy_setopt(ps->send_endpoint, CURLOPT_BUFFERSIZE, GNUNET_SERVER_MAX_MESSAGE_SIZE);
1782 if (fresh==GNUNET_YES)
1784 mret = curl_multi_add_handle(plugin->multi_handle, ps->send_endpoint);
1785 if (mret != CURLM_OK)
1787 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1788 _("Connection: %X: %s failed at %s:%d: `%s'\n"),
1790 "curl_multi_add_handle", __FILE__, __LINE__,
1791 curl_multi_strerror (mret));
1792 return GNUNET_SYSERR;
1796 if (curl_schedule (plugin) == GNUNET_SYSERR)
1797 return GNUNET_SYSERR;
1800 if (ps->direction == INBOUND)
1802 GNUNET_assert (NULL != ps->pending_msgs_tail);
1803 if ((ps->recv_connected==GNUNET_YES) && (ps->send_connected==GNUNET_YES) &&
1804 (ps->recv_force_disconnect==GNUNET_NO) && (ps->recv_force_disconnect==GNUNET_NO))
1807 return GNUNET_SYSERR;
1810 static struct Session * send_select_session (void * cls, struct HTTP_PeerContext *pc, const void * addr, size_t addrlen, int force_address, struct Session * session)
1812 struct Session * tmp = NULL;
1813 int addr_given = GNUNET_NO;
1815 if ((addr!=NULL) && (addrlen>0))
1816 addr_given = GNUNET_YES;
1818 if (force_address == GNUNET_YES)
1820 /* check session given as argument */
1821 if ((session != NULL) && (addr_given == GNUNET_YES))
1823 if (0 == memcmp(session->addr, addr, addrlen))
1825 /* connection can not be used, since it is disconnected */
1826 if ((session->recv_force_disconnect==GNUNET_NO) && (session->send_force_disconnect==GNUNET_NO))
1828 #if DEBUG_SESSION_SELECTION
1829 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Session %X selected: Using session passed by transport to send to forced address \n", session);
1835 /* check last session used */
1836 if ((pc->last_session != NULL)&& (addr_given == GNUNET_YES))
1838 if (0 == memcmp(pc->last_session->addr, addr, addrlen))
1840 /* connection can not be used, since it is disconnected */
1841 if ((pc->last_session->recv_force_disconnect==GNUNET_NO) && (pc->last_session->send_force_disconnect==GNUNET_NO))
1843 #if DEBUG_SESSION_SELECTION
1844 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Session %X selected: Using last session used to send to forced address \n", pc->last_session);
1846 return pc->last_session;
1850 /* find session in existing sessions */
1852 while ((tmp!=NULL) && (addr_given == GNUNET_YES))
1855 if (0 == memcmp(tmp->addr, addr, addrlen))
1857 /* connection can not be used, since it is disconnected */
1858 if ((tmp->recv_force_disconnect==GNUNET_NO) && (tmp->send_force_disconnect==GNUNET_NO))
1860 #if DEBUG_SESSION_SELECTION
1861 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Session %X selected: Using existing session to send to forced address \n", session);
1869 /* no session to use */
1872 if ((force_address == GNUNET_NO) || (force_address == GNUNET_SYSERR))
1874 /* check session given as argument */
1875 if (session != NULL)
1877 /* connection can not be used, since it is disconnected */
1878 if ((session->recv_force_disconnect==GNUNET_NO) && (session->send_force_disconnect==GNUNET_NO))
1880 #if DEBUG_SESSION_SELECTION
1881 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Session %X selected: Using session passed by transport to send not-forced address \n", session);
1887 /* check last session used */
1888 if (pc->last_session != NULL)
1890 /* connection can not be used, since it is disconnected */
1891 if ((pc->last_session->recv_force_disconnect==GNUNET_NO) && (pc->last_session->send_force_disconnect==GNUNET_NO))
1893 #if DEBUG_SESSION_SELECTION
1894 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Session %X selected: Using last session to send to not-forced address \n", pc->last_session);
1896 return pc->last_session;
1899 /* find session in existing sessions */
1903 /* connection can not be used, since it is disconnected */
1904 if ((tmp->recv_force_disconnect==GNUNET_NO) && (tmp->send_force_disconnect==GNUNET_NO))
1906 #if DEBUG_SESSION_SELECTION
1907 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Session %X selected: Using existing session to send to not-forced address \n", tmp);
1919 * Function that can be used by the transport service to transmit
1920 * a message using the plugin. Note that in the case of a
1921 * peer disconnecting, the continuation MUST be called
1922 * prior to the disconnect notification itself. This function
1923 * will be called with this peer's HELLO message to initiate
1924 * a fresh connection to another peer.
1926 * @param cls closure
1927 * @param target who should receive this message
1928 * @param msgbuf the message to transmit
1929 * @param msgbuf_size number of bytes in 'msgbuf'
1930 * @param priority how important is the message (most plugins will
1931 * ignore message priority and just FIFO)
1932 * @param timeout how long to wait at most for the transmission (does not
1933 * require plugins to discard the message after the timeout,
1934 * just advisory for the desired delay; most plugins will ignore
1936 * @param session which session must be used (or NULL for "any")
1937 * @param addr the address to use (can be NULL if the plugin
1938 * is "on its own" (i.e. re-use existing TCP connection))
1939 * @param addrlen length of the address in bytes
1940 * @param force_address GNUNET_YES if the plugin MUST use the given address,
1941 * GNUNET_NO means the plugin may use any other address and
1942 * GNUNET_SYSERR means that only reliable existing
1943 * bi-directional connections should be used (regardless
1945 * @param cont continuation to call once the message has
1946 * been transmitted (or if the transport is ready
1947 * for the next transmission call; or if the
1948 * peer disconnected...); can be NULL
1949 * @param cont_cls closure for cont
1950 * @return number of bytes used (on the physical network, with overheads);
1951 * -1 on hard errors (i.e. address invalid); 0 is a legal value
1952 * and does NOT mean that the message was not transmitted (DV)
1955 http_plugin_send (void *cls,
1956 const struct GNUNET_PeerIdentity *target,
1959 unsigned int priority,
1960 struct GNUNET_TIME_Relative to,
1961 struct Session *session,
1965 GNUNET_TRANSPORT_TransmitContinuation cont,
1968 struct Plugin *plugin = cls;
1969 struct HTTP_Message *msg;
1970 struct HTTP_PeerContext * pc;
1971 struct Session * ps = NULL;
1973 GNUNET_assert(cls !=NULL);
1976 char * force = GNUNET_malloc(40);
1977 if (force_address == GNUNET_YES)
1978 strcpy(force,"forced addr.");
1979 if (force_address == GNUNET_NO)
1980 strcpy(force,"any addr.");
1981 if (force_address == GNUNET_SYSERR)
1982 strcpy(force,"reliable bi-direc. address addr.");
1984 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Transport tells me to send %u bytes to `%s' using %s (%s) and session: %X\n",
1988 http_plugin_address_to_string(NULL, addr, addrlen),
1994 pc = GNUNET_CONTAINER_multihashmap_get (plugin->peers, &target->hashPubKey);
1998 pc = GNUNET_malloc(sizeof (struct HTTP_PeerContext));
1999 pc->plugin = plugin;
2000 pc->session_id_counter=1;
2001 pc->last_session = NULL;
2002 memcpy(&pc->identity, target, sizeof(struct GNUNET_PeerIdentity));
2003 GNUNET_CONTAINER_multihashmap_put(plugin->peers, &pc->identity.hashPubKey, pc, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
2004 GNUNET_STATISTICS_update (plugin->env->stats,
2005 gettext_noop ("# HTTP peers active"),
2010 ps = send_select_session (plugin, pc, addr, addrlen, force_address, session);
2012 /* session not existing, but address forced -> creating new session */
2015 if ((addr!=NULL) && (addrlen!=0))
2017 ps = GNUNET_malloc(sizeof (struct Session));
2018 #if DEBUG_SESSION_SELECTION
2019 if (force_address == GNUNET_YES)
2020 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"No existing connection & forced address: creating new session %X to peer %s\n", ps, GNUNET_i2s(target));
2021 if (force_address != GNUNET_YES)
2022 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"No existing connection: creating new session %X to peer %s\n", ps, GNUNET_i2s(target));
2024 if ((addrlen!=0) && (addr!=NULL))
2026 ps->addr = GNUNET_malloc(addrlen);
2027 memcpy(ps->addr,addr,addrlen);
2028 ps->addrlen = addrlen;
2035 ps->direction=OUTBOUND;
2036 ps->recv_connected = GNUNET_NO;
2037 ps->recv_force_disconnect = GNUNET_NO;
2038 ps->send_connected = GNUNET_NO;
2039 ps->send_force_disconnect = GNUNET_NO;
2040 ps->pending_msgs_head = NULL;
2041 ps->pending_msgs_tail = NULL;
2043 ps->session_id = pc->session_id_counter;
2044 pc->session_id_counter++;
2045 ps->url = create_url (plugin, ps->addr, ps->addrlen, ps->session_id);
2046 if (ps->msgtok == NULL)
2047 ps->msgtok = GNUNET_SERVER_mst_create (&curl_receive_mst_cb, ps);
2048 GNUNET_CONTAINER_DLL_insert(pc->head,pc->tail,ps);
2051 GNUNET_STATISTICS_update (plugin->env->stats,
2052 gettext_noop ("# HTTP outbound sessions for peers active"),
2059 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));
2061 return GNUNET_SYSERR;
2066 msg = GNUNET_malloc (sizeof (struct HTTP_Message) + msgbuf_size);
2068 msg->size = msgbuf_size;
2070 msg->buf = (char *) &msg[1];
2071 msg->transmit_cont = cont;
2072 msg->transmit_cont_cls = cont_cls;
2073 memcpy (msg->buf,msgbuf, msgbuf_size);
2074 GNUNET_CONTAINER_DLL_insert(ps->pending_msgs_head,ps->pending_msgs_tail,msg);
2076 if (send_check_connections (plugin, ps) != GNUNET_SYSERR)
2078 if (force_address != GNUNET_YES)
2079 pc->last_session = ps;
2081 if (pc->last_session==NULL)
2082 pc->last_session = ps;
2086 return GNUNET_SYSERR;
2092 * Function that can be used to force the plugin to disconnect
2093 * from the given peer and cancel all previous transmissions
2094 * (and their continuationc).
2096 * @param cls closure
2097 * @param target peer from which to disconnect
2100 * Convert the transports address to a nice, human-readable
2103 * @param cls closure
2104 * @param type name of the transport that generated the address
2105 * @param addr one of the addresses of the host, NULL for the last address
2106 * the specific address format depends on the transport
2107 * @param addrlen length of the address
2108 * @param numeric should (IP) addresses be displayed in numeric form?
2109 * @param timeout after how long should we give up?
2110 * @param asc function to call on each string
2111 * @param asc_cls closure for asc
2114 http_plugin_address_pretty_printer (void *cls,
2119 struct GNUNET_TIME_Relative timeout,
2120 GNUNET_TRANSPORT_AddressStringCallback
2123 const struct IPv4HttpAddress *t4;
2124 const struct IPv6HttpAddress *t6;
2125 struct sockaddr_in a4;
2126 struct sockaddr_in6 a6;
2132 GNUNET_assert(cls !=NULL);
2133 if (addrlen == sizeof (struct IPv6HttpAddress))
2135 address = GNUNET_malloc (INET6_ADDRSTRLEN);
2137 a6.sin6_addr = t6->ipv6_addr;
2138 inet_ntop(AF_INET6, &(a6.sin6_addr),address,INET6_ADDRSTRLEN);
2139 port = ntohs(t6->u6_port);
2141 else if (addrlen == sizeof (struct IPv4HttpAddress))
2143 address = GNUNET_malloc (INET_ADDRSTRLEN);
2145 a4.sin_addr.s_addr = t4->ipv4_addr;
2146 inet_ntop(AF_INET, &(a4.sin_addr),address,INET_ADDRSTRLEN);
2147 port = ntohs(t4->u_port);
2151 /* invalid address */
2152 GNUNET_break_op (0);
2153 asc (asc_cls, NULL);
2156 res = GNUNET_asprintf(&ret,"%s://%s:%u/", PROTOCOL_PREFIX, address,port);
2157 GNUNET_free (address);
2158 GNUNET_assert(res != 0);
2160 GNUNET_free_non_null (ret);
2166 * Another peer has suggested an address for this
2167 * peer and transport plugin. Check that this could be a valid
2168 * address. If so, consider adding it to the list
2171 * @param cls closure
2172 * @param addr pointer to the address
2173 * @param addrlen length of addr
2174 * @return GNUNET_OK if this is a plausible address for this peer
2178 http_plugin_address_suggested (void *cls,
2179 const void *addr, size_t addrlen)
2181 struct Plugin *plugin = cls;
2182 struct IPv4HttpAddress *v4;
2183 struct IPv6HttpAddress *v6;
2186 GNUNET_assert(cls !=NULL);
2187 if ((addrlen != sizeof (struct IPv4HttpAddress)) &&
2188 (addrlen != sizeof (struct IPv6HttpAddress)))
2190 return GNUNET_SYSERR;
2192 if (addrlen == sizeof (struct IPv4HttpAddress))
2194 v4 = (struct IPv4HttpAddress *) addr;
2195 /* Not skipping loopback
2196 if (INADDR_LOOPBACK == ntohl(v4->ipv4_addr))
2198 return GNUNET_SYSERR;
2200 port = ntohs (v4->u_port);
2201 if (port != plugin->port_inbound)
2203 return GNUNET_SYSERR;
2206 if (addrlen == sizeof (struct IPv6HttpAddress))
2208 v6 = (struct IPv6HttpAddress *) addr;
2209 if (IN6_IS_ADDR_LINKLOCAL (&v6->ipv6_addr))
2211 return GNUNET_SYSERR;
2213 port = ntohs (v6->u6_port);
2214 if (port != plugin->port_inbound)
2216 return GNUNET_SYSERR;
2225 * Function called for a quick conversion of the binary address to
2226 * a numeric address. Note that the caller must not free the
2227 * address and that the next call to this function is allowed
2228 * to override the address again.
2230 * @param cls closure
2231 * @param addr binary address
2232 * @param addrlen length of the address
2233 * @return string representing the same address
2236 http_plugin_address_to_string (void *cls,
2240 const struct IPv4HttpAddress *t4;
2241 const struct IPv6HttpAddress *t6;
2242 struct sockaddr_in a4;
2243 struct sockaddr_in6 a6;
2249 if (addrlen == sizeof (struct IPv6HttpAddress))
2251 address = GNUNET_malloc (INET6_ADDRSTRLEN);
2253 a6.sin6_addr = t6->ipv6_addr;
2254 inet_ntop(AF_INET6, &(a6.sin6_addr),address,INET6_ADDRSTRLEN);
2255 port = ntohs(t6->u6_port);
2257 else if (addrlen == sizeof (struct IPv4HttpAddress))
2259 address = GNUNET_malloc (INET_ADDRSTRLEN);
2261 a4.sin_addr.s_addr = t4->ipv4_addr;
2262 inet_ntop(AF_INET, &(a4.sin_addr),address,INET_ADDRSTRLEN);
2263 port = ntohs(t4->u_port);
2267 /* invalid address */
2270 res = GNUNET_asprintf(&ret,"%s:%u",address,port);
2271 GNUNET_free (address);
2272 GNUNET_assert(res != 0);
2277 load_certificate( const char * file )
2279 struct GNUNET_DISK_FileHandle * gn_file;
2284 if (0!=STAT(file, &fstat))
2286 text = GNUNET_malloc (fstat.st_size+1);
2287 gn_file = GNUNET_DISK_file_open(file,GNUNET_DISK_OPEN_READ, GNUNET_DISK_PERM_USER_READ);
2293 if (GNUNET_SYSERR == GNUNET_DISK_file_read(gn_file, text, fstat.st_size))
2298 text[fstat.st_size] = '\0';
2299 GNUNET_DISK_file_close(gn_file);
2306 * Exit point from the plugin.
2309 libgnunet_plugin_transport_https_done (void *cls)
2311 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
2312 struct Plugin *plugin = api->cls;
2314 GNUNET_assert(cls !=NULL);
2316 if (plugin->http_server_daemon_v4 != NULL)
2318 MHD_stop_daemon (plugin->http_server_daemon_v4);
2319 plugin->http_server_daemon_v4 = NULL;
2321 if (plugin->http_server_daemon_v6 != NULL)
2323 MHD_stop_daemon (plugin->http_server_daemon_v6);
2324 plugin->http_server_daemon_v6 = NULL;
2327 if ( plugin->http_server_task_v4 != GNUNET_SCHEDULER_NO_TASK)
2329 GNUNET_SCHEDULER_cancel(plugin->env->sched, plugin->http_server_task_v4);
2330 plugin->http_server_task_v4 = GNUNET_SCHEDULER_NO_TASK;
2333 if ( plugin->http_server_task_v6 != GNUNET_SCHEDULER_NO_TASK)
2335 GNUNET_SCHEDULER_cancel(plugin->env->sched, plugin->http_server_task_v6);
2336 plugin->http_server_task_v6 = GNUNET_SCHEDULER_NO_TASK;
2340 /* free all peer information */
2341 if (plugin->peers!=NULL)
2343 GNUNET_CONTAINER_multihashmap_iterate (plugin->peers,
2344 &remove_peer_context_Iterator,
2346 GNUNET_CONTAINER_multihashmap_destroy (plugin->peers);
2348 if (plugin->multi_handle!=NULL)
2350 mret = curl_multi_cleanup(plugin->multi_handle);
2352 if ( CURLM_OK != mret)
2353 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"curl multihandle clean up failed\n");
2355 plugin->multi_handle = NULL;
2357 curl_global_cleanup();
2359 if ( plugin->http_curl_task != GNUNET_SCHEDULER_NO_TASK)
2361 GNUNET_SCHEDULER_cancel(plugin->env->sched, plugin->http_curl_task);
2362 plugin->http_curl_task = GNUNET_SCHEDULER_NO_TASK;
2365 GNUNET_free_non_null (plugin->bind4_address);
2366 GNUNET_free_non_null (plugin->bind6_address);
2367 GNUNET_free_non_null (plugin->bind_hostname);
2368 GNUNET_free_non_null (plugin->crypto_init);
2369 GNUNET_free (plugin);
2372 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Unload http plugin complete...\n");
2379 * Entry point for the plugin.
2382 libgnunet_plugin_transport_https_init (void *cls)
2384 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
2385 struct Plugin *plugin;
2386 struct GNUNET_TRANSPORT_PluginFunctions *api;
2387 struct GNUNET_TIME_Relative gn_timeout;
2388 long long unsigned int port;
2393 GNUNET_assert(cls !=NULL);
2395 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Starting https plugin...\n");
2398 plugin = GNUNET_malloc (sizeof (struct Plugin));
2399 plugin->stats = env->stats;
2401 plugin->peers = NULL;
2402 plugin->bind4_address = NULL;
2403 plugin->use_ipv6 = GNUNET_YES;
2404 plugin->use_ipv4 = GNUNET_YES;
2406 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
2408 api->send = &http_plugin_send;
2409 api->disconnect = &http_plugin_disconnect;
2410 api->address_pretty_printer = &http_plugin_address_pretty_printer;
2411 api->check_address = &http_plugin_address_suggested;
2412 api->address_to_string = &http_plugin_address_to_string;
2414 /* Hashing our identity to use it in URLs */
2415 GNUNET_CRYPTO_hash_to_enc ( &(plugin->env->my_identity->hashPubKey), &plugin->my_ascii_hash_ident);
2417 /* Use IPv6 yes/no */
2418 if (GNUNET_CONFIGURATION_have_value (env->cfg,
2419 "transport-https", "USE_IPv6"))
2421 plugin->use_ipv6 = GNUNET_CONFIGURATION_get_value_yesno (env->cfg,
2425 /* Use IPv4 yes/no */
2426 if (GNUNET_CONFIGURATION_have_value (env->cfg,
2427 "transport-https", "USE_IPv4"))
2429 plugin->use_ipv4 = GNUNET_CONFIGURATION_get_value_yesno (env->cfg,
2433 /* Reading port number from config file */
2435 GNUNET_CONFIGURATION_get_value_number (env->cfg,
2441 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
2443 _("Require valid port number for transport plugin `%s' in configuration!\n"),
2445 libgnunet_plugin_transport_https_done (api);
2449 /* Reading ipv4 addresse to bind to from config file */
2450 if ((plugin->use_ipv4==GNUNET_YES) && (GNUNET_CONFIGURATION_have_value (env->cfg,
2451 "transport-https", "BINDTO4")))
2453 GNUNET_break (GNUNET_OK ==
2454 GNUNET_CONFIGURATION_get_value_string (env->cfg,
2457 &plugin->bind_hostname));
2458 plugin->bind4_address = GNUNET_malloc(sizeof(struct sockaddr_in));
2459 plugin->bind4_address->sin_family = AF_INET;
2460 plugin->bind4_address->sin_port = htons (port);
2462 if (inet_pton(AF_INET,plugin->bind_hostname, &plugin->bind4_address->sin_addr)<=0)
2464 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
2466 _("Misconfigured address to bind to in configuration!\n"),
2468 GNUNET_free(plugin->bind4_address);
2469 GNUNET_free(plugin->bind_hostname);
2470 plugin->bind_hostname = NULL;
2471 plugin->bind4_address = NULL;
2475 /* Get crypto init string from config */
2476 if (GNUNET_CONFIGURATION_have_value (env->cfg,
2477 "transport-https", "CRYPTO_INIT"))
2479 GNUNET_CONFIGURATION_get_value_string (env->cfg,
2482 &plugin->crypto_init);
2486 GNUNET_asprintf(&plugin->crypto_init,"NORMAL");
2489 /* Get private key file from config */
2490 if (GNUNET_CONFIGURATION_have_value (env->cfg,
2491 "transport-https", "CERT_FILE"))
2493 GNUNET_CONFIGURATION_get_value_string (env->cfg,
2500 GNUNET_asprintf(&cert_file,"https.cert");
2503 /* Get private key file from config */
2504 if (GNUNET_CONFIGURATION_have_value (env->cfg,
2505 "transport-https", "KEY_FILE"))
2507 GNUNET_CONFIGURATION_get_value_string (env->cfg,
2514 GNUNET_asprintf(&key_file,"https.key");
2517 /* Get private key file from config */
2518 if (GNUNET_CONFIGURATION_have_value (env->cfg,
2519 "transport-https", "CERT_FILE"))
2521 GNUNET_CONFIGURATION_get_value_string (env->cfg,
2528 GNUNET_asprintf(&cert_file,"https.cert");
2531 /* Should plugin use ipv6? */
2532 if ((plugin->use_ipv6==GNUNET_YES) && (GNUNET_CONFIGURATION_have_value (env->cfg,
2533 "transport-https", "BINDTO6")))
2535 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (env->cfg,
2538 &plugin->bind_hostname))
2540 plugin->bind6_address = GNUNET_malloc(sizeof(struct sockaddr_in6));
2541 plugin->bind6_address->sin6_family = AF_INET6;
2542 plugin->bind6_address->sin6_port = htons (port);
2544 if (inet_pton(AF_INET6,plugin->bind_hostname, &plugin->bind6_address->sin6_addr)<=0)
2546 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
2548 _("Misconfigured address to bind to in configuration!\n"),
2550 GNUNET_free(plugin->bind6_address);
2551 GNUNET_free(plugin->bind_hostname);
2552 plugin->bind_hostname = NULL;
2553 plugin->bind6_address = NULL;
2558 /* read key & certificates from file */
2559 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Loading TLS certificate `%s' `%s'\n", key_file, cert_file);
2561 plugin->key = load_certificate( key_file );
2562 plugin->cert = load_certificate( cert_file );
2564 if ((plugin->key==NULL) || (plugin->cert==NULL))
2568 GNUNET_asprintf(&cmd,"gnunet-transport-certificate-creation %s %s", key_file, cert_file);
2569 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "No usable TLS certificate found, creating certificate \n");
2574 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
2576 _("Could not create a new TLS certificate, shell script `%s' failed!\n"),cmd,
2578 GNUNET_free (key_file);
2579 GNUNET_free (cert_file);
2580 libgnunet_plugin_transport_https_done(api);
2587 plugin->key = load_certificate( key_file );
2588 plugin->cert = load_certificate( cert_file );
2590 if ((plugin->key==NULL) || (plugin->cert==NULL))
2592 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
2594 _("No usable TLS certificate found and creating one failed! \n"),
2596 GNUNET_free (key_file);
2597 GNUNET_free (cert_file);
2598 libgnunet_plugin_transport_https_done(api);
2603 GNUNET_free (key_file);
2604 GNUNET_free (cert_file);
2607 GNUNET_assert((plugin->key!=NULL) && (plugin->cert!=NULL));
2608 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "TLS certificate loaded\n", key_file, cert_file);
2610 GNUNET_assert ((port > 0) && (port <= 65535));
2611 plugin->port_inbound = port;
2612 gn_timeout = GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT;
2613 unsigned int timeout = (gn_timeout.value) / 1000;
2614 if ((plugin->http_server_daemon_v6 == NULL) && (plugin->use_ipv6 == GNUNET_YES) && (port != 0))
2616 struct sockaddr * tmp = (struct sockaddr *) plugin->bind6_address;
2617 plugin->http_server_daemon_v6 = MHD_start_daemon (
2620 #if DEBUG_CONNECTIONS
2622 MHD_USE_IPv6 | MHD_USE_SSL,
2625 plugin , &mdh_access_cb, plugin,
2626 /*MHD_OPTION_HTTPS_PRIORITIES, "NORMAL:",*/
2627 /*MHD_OPTION_HTTPS_PRIORITIES, "PERFORMANCE:",*/
2628 /* MHD_OPTION_HTTPS_PRIORITIES, "NONE:+VERS-TLS1.0:+ARCFOUR-128:+SHA1:+RSA:+COMP-NULL", */
2629 /*MHD_OPTION_HTTPS_PRIORITIES, "NONE:+VERS-TLS1.0:+ARCFOUR-128:+MD5:+RSA:+COMP-NULL",*/
2630 MHD_OPTION_HTTPS_PRIORITIES, plugin->crypto_init,
2631 MHD_OPTION_HTTPS_MEM_KEY, plugin->key,
2632 MHD_OPTION_HTTPS_MEM_CERT, plugin->cert,
2633 MHD_OPTION_SOCK_ADDR, tmp,
2634 MHD_OPTION_CONNECTION_LIMIT, (unsigned int) 32,
2635 //MHD_OPTION_PER_IP_CONNECTION_LIMIT, (unsigned int) 6,
2636 MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) timeout,
2637 MHD_OPTION_CONNECTION_MEMORY_LIMIT, (size_t) (16 * 1024),
2638 MHD_OPTION_NOTIFY_COMPLETED, &mhd_termination_cb, NULL,
2639 MHD_OPTION_EXTERNAL_LOGGER, mhd_logger, plugin->mhd_log,
2642 if ((plugin->http_server_daemon_v4 == NULL) && (plugin->use_ipv4 == GNUNET_YES) && (port != 0))
2644 plugin->http_server_daemon_v4 = MHD_start_daemon (
2647 #if DEBUG_CONNECTIONS
2649 MHD_NO_FLAG | MHD_USE_SSL,
2652 plugin , &mdh_access_cb, plugin,
2653 /*MHD_OPTION_HTTPS_PRIORITIES, "NORMAL:",*/
2654 /*MHD_OPTION_HTTPS_PRIORITIES, "PERFORMANCE:",*/
2655 /* MHD_OPTION_HTTPS_PRIORITIES, "NONE:+VERS-TLS1.0:+ARCFOUR-128:+SHA1:+RSA:+COMP-NULL", */
2656 /*MHD_OPTION_HTTPS_PRIORITIES, "NONE:+VERS-TLS1.0:+ARCFOUR-128:+MD5:+RSA:+COMP-NULL",*/
2657 MHD_OPTION_HTTPS_PRIORITIES, plugin->crypto_init,
2658 MHD_OPTION_HTTPS_MEM_KEY, plugin->key,
2659 MHD_OPTION_HTTPS_MEM_CERT, plugin->cert,
2660 MHD_OPTION_SOCK_ADDR, (struct sockaddr_in *)plugin->bind4_address,
2661 MHD_OPTION_CONNECTION_LIMIT, (unsigned int) 32,
2662 //MHD_OPTION_PER_IP_CONNECTION_LIMIT, (unsigned int) 6,
2663 MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) timeout,
2664 MHD_OPTION_CONNECTION_MEMORY_LIMIT, (size_t) (16 * 1024),
2665 MHD_OPTION_NOTIFY_COMPLETED, &mhd_termination_cb, NULL,
2666 MHD_OPTION_EXTERNAL_LOGGER, mhd_logger, plugin->mhd_log,
2669 if (plugin->http_server_daemon_v4 != NULL)
2670 plugin->http_server_task_v4 = http_server_daemon_prepare (plugin, plugin->http_server_daemon_v4);
2671 if (plugin->http_server_daemon_v6 != NULL)
2672 plugin->http_server_task_v6 = http_server_daemon_prepare (plugin, plugin->http_server_daemon_v6);
2675 if (plugin->http_server_task_v4 != GNUNET_SCHEDULER_NO_TASK)
2678 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);
2681 else if ((plugin->http_server_task_v6 != GNUNET_SCHEDULER_NO_TASK) && (plugin->http_server_task_v4 != GNUNET_SCHEDULER_NO_TASK))
2684 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);
2687 else if ((plugin->http_server_task_v6 != GNUNET_SCHEDULER_NO_TASK) && (plugin->http_server_task_v4 == GNUNET_SCHEDULER_NO_TASK))
2690 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);
2696 if ((plugin->use_ipv6 == GNUNET_YES) && (plugin->use_ipv4 == GNUNET_YES))
2697 GNUNET_asprintf(&tmp,"with IPv4 and IPv6 enabled");
2698 if ((plugin->use_ipv6 == GNUNET_NO) && (plugin->use_ipv4 == GNUNET_YES))
2699 GNUNET_asprintf(&tmp,"with IPv4 enabled");
2700 if ((plugin->use_ipv6 == GNUNET_YES) && (plugin->use_ipv4 == GNUNET_NO))
2701 GNUNET_asprintf(&tmp,"with IPv6 enabled");
2702 if ((plugin->use_ipv6 == GNUNET_NO) && (plugin->use_ipv4 == GNUNET_NO))
2703 GNUNET_asprintf(&tmp,"with NO IP PROTOCOL enabled");
2704 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,"HTTPS Server with %s could not be started on port %u! https plugin failed!\n",tmp, port);
2706 libgnunet_plugin_transport_https_done (api);
2710 /* Initializing cURL */
2711 curl_global_init(CURL_GLOBAL_ALL);
2712 plugin->multi_handle = curl_multi_init();
2714 if ( NULL == plugin->multi_handle )
2716 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
2718 _("Could not initialize curl multi handle, failed to start http plugin!\n"),
2720 libgnunet_plugin_transport_https_done (api);
2724 plugin->peers = GNUNET_CONTAINER_multihashmap_create (10);
2725 GNUNET_OS_network_interfaces_list (&process_interfaces, plugin);
2730 /* end of plugin_transport_https.c */