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_http.c
23 * @brief http 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 "microhttpd.h"
41 #include <curl/curl.h>
44 #define LIBGNUNET_PLUGIN_TRANSPORT_INIT libgnunet_plugin_transport_https_init
45 #define LIBGNUNET_PLUGIN_TRANSPORT_DONE libgnunet_plugin_transport_https_done
46 #define LIBGNUNET_PLUGIN_TRANSPORT_COMPONENT transport_https
47 #define PROTOCOL_PREFIX "https"
49 #define LIBGNUNET_PLUGIN_TRANSPORT_INIT libgnunet_plugin_transport_http_init
50 #define LIBGNUNET_PLUGIN_TRANSPORT_DONE libgnunet_plugin_transport_http_done
51 #define LIBGNUNET_PLUGIN_TRANSPORT_COMPONENT transport_http
52 #define PROTOCOL_PREFIX "http"
55 #define DEBUG_HTTP GNUNET_NO
56 #define DEBUG_CURL GNUNET_NO
57 #define DEBUG_MHD GNUNET_NO
58 #define DEBUG_CONNECTIONS GNUNET_NO
59 #define DEBUG_SESSION_SELECTION GNUNET_NO
60 #define DEBUG_SCHEDULING GNUNET_NO
61 #define CURL_TCP_NODELAY GNUNET_YES
63 #define INBOUND GNUNET_NO
64 #define OUTBOUND GNUNET_YES
69 * Text of the response sent back after the last bytes of a PUT
70 * request have been received (just to formally obey the HTTP
73 #define HTTP_PUT_RESPONSE "Thank you!"
76 * After how long do we expire an address that we
77 * learned from another peer if it is not reconfirmed
80 #define LEARNED_ADDRESS_EXPIRATION GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_HOURS, 6)
83 * Page returned if request invalid
85 #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>"
88 * Timeout for a http connect
90 #define HTTP_CONNECT_TIMEOUT 30
94 * Network format for IPv4 addresses.
96 struct IPv4HttpAddress
99 * IPv4 address, in network byte order.
101 uint32_t ipv4_addr GNUNET_PACKED;
104 * Port number, in network byte order.
106 uint16_t u_port GNUNET_PACKED;
112 * Network format for IPv6 addresses.
114 struct IPv6HttpAddress
119 struct in6_addr ipv6_addr GNUNET_PACKED;
122 * Port number, in network byte order.
124 uint16_t u6_port GNUNET_PACKED;
130 * Message to send using http
135 * next pointer for double linked list
137 struct HTTP_Message * next;
140 * previous pointer for double linked list
142 struct HTTP_Message * prev;
145 * buffer containing data to send
150 * amount of data already sent
160 * Continuation function to call once the transmission buffer
161 * has again space available. NULL if there is no
162 * continuation to call.
164 GNUNET_TRANSPORT_TransmitContinuation transmit_cont;
167 * Closure for transmit_cont.
169 void *transmit_cont_cls;
173 struct HTTP_PeerContext
178 struct GNUNET_PeerIdentity identity;
181 * Pointer to the global plugin struct.
183 struct Plugin *plugin;
186 * Linked list of connections with this peer
189 struct Session * head;
192 * Linked list of connections with this peer
195 struct Session * tail;
198 * id for next session
200 size_t session_id_counter;
203 * Last session used to send data
205 struct Session * last_session;
214 struct SessionHeader header;
217 * next session in linked list
219 struct Session * next;
222 * previous session in linked list
224 struct Session * prev;
227 * address of this session
242 * Message queue for outbound messages
245 struct HTTP_Message * pending_msgs_head;
248 * Message queue for outbound messages
251 struct HTTP_Message * pending_msgs_tail;
254 * partner peer this connection belongs to
256 struct HTTP_PeerContext * peercontext;
259 * message stream tokenizer for incoming data
261 struct GNUNET_SERVER_MessageStreamTokenizer *msgtok;
265 * outbound: OUTBOUND (GNUNET_YES)
266 * inbound : INBOUND (GNUNET_NO)
268 unsigned int direction;
271 * is session connected to send data?
273 unsigned int send_connected;
276 * is send connection active?
278 unsigned int send_active;
281 * connection disconnect forced (e.g. from transport)
283 unsigned int send_force_disconnect;
286 * is session connected to receive data?
288 unsigned int recv_connected;
291 * is receive connection active?
293 unsigned int recv_active;
296 * connection disconnect forced (e.g. from transport)
298 unsigned int recv_force_disconnect;
301 * id for next session
302 * NOTE: 0 is not an ID, zero is not defined. A correct ID is always > 0
307 * entity managing sending data
308 * outbound session: CURL *
309 * inbound session: mhd_connection *
311 void * send_endpoint;
314 * entity managing recieving data
315 * outbound session: CURL *
316 * inbound session: mhd_connection *
318 void * recv_endpoint;
322 * Encapsulation of all of the state of the plugin.
329 struct GNUNET_TRANSPORT_PluginEnvironment *env;
332 * Handle for reporting statistics.
334 struct GNUNET_STATISTICS_Handle *stats;
339 unsigned int port_inbound;
341 struct GNUNET_CONTAINER_MultiHashMap *peers;
344 * Daemon for listening for new IPv4 connections.
346 struct MHD_Daemon *http_server_daemon_v4;
349 * Daemon for listening for new IPv6connections.
351 struct MHD_Daemon *http_server_daemon_v6;
354 * Our primary task for http daemon handling IPv4 connections
356 GNUNET_SCHEDULER_TaskIdentifier http_server_task_v4;
359 * Our primary task for http daemon handling IPv6 connections
361 GNUNET_SCHEDULER_TaskIdentifier http_server_task_v6;
364 * The task sending data
366 GNUNET_SCHEDULER_TaskIdentifier http_curl_task;
371 CURLM * multi_handle;
374 * Our ASCII encoded, hashed peer identity
375 * This string is used to distinguish between connections and is added to the urls
377 struct GNUNET_CRYPTO_HashAsciiEncoded my_ascii_hash_ident;
380 * IPv4 Address the plugin binds to
382 struct sockaddr_in * bind4_address;
385 * IPv6 Address the plugins binds to
387 struct sockaddr_in6 * bind6_address;
390 * Hostname to bind to
392 char * bind_hostname;
405 * Closure passed by MHD to the mhd_logger function
409 /* only needed for HTTPS plugin */
411 /* The certificate MHD uses as an \0 terminated string */
414 /* The private key MHD uses as an \0 terminated string */
417 /* crypto init string */
424 * Function called for a quick conversion of the binary address to
425 * a numeric address. Note that the caller must not free the
426 * address and that the next call to this function is allowed
427 * to override the address again.
430 * @param addr binary address
431 * @param addrlen length of the address
432 * @return string representing the same address
435 http_plugin_address_to_string (void *cls,
441 * Call MHD to process pending ipv4 requests and then go back
442 * and schedule the next run.
444 static void http_server_daemon_v4_run (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
446 * Call MHD to process pending ipv6 requests and then go back
447 * and schedule the next run.
449 static void http_server_daemon_v6_run (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
452 * Function setting up curl handle and selecting message to send
453 * @param plugin plugin
454 * @param ses session to send data to
455 * @param con connection
456 * @return bytes sent to peer
458 static int send_check_connections (struct Plugin *plugin, struct Session *ps);
461 * Function setting up file descriptors and scheduling task to run
463 * @param ses session to send data to
466 static int curl_schedule (struct Plugin *plugin);
470 * Creates a valid url from passed address and id
471 * @param plugin plugin
472 * @param addr address to create url from
473 * @param addrlen address lenth
474 * @param id session id
475 * @return the created url
477 static char * create_url(struct Plugin *plugin, const void * addr, size_t addrlen, size_t id)
480 char *addr_str = (char *) http_plugin_address_to_string(NULL, addr, addrlen);
482 GNUNET_assert ((addr!=NULL) && (addrlen != 0));
483 GNUNET_asprintf(&url,
484 "%s://%s/%s;%u", PROTOCOL_PREFIX, addr_str,
485 (char *) (&plugin->my_ascii_hash_ident),id);
486 GNUNET_free_non_null(addr_str);
491 * Removes a message from the linked list of messages
494 * @return GNUNET_SYSERR if msg not found, GNUNET_OK on success
496 static int remove_http_message (struct Session * ps, struct HTTP_Message * msg)
498 GNUNET_CONTAINER_DLL_remove(ps->pending_msgs_head,ps->pending_msgs_tail,msg);
504 * Iterator to remove peer context
505 * @param cls the plugin
506 * @key the peers public key hashcode
507 * @value the peer context
508 * @return GNUNET_YES on success
510 int remove_peer_context_Iterator (void *cls, const GNUNET_HashCode *key, void *value)
512 struct Plugin *plugin = cls;
513 struct HTTP_PeerContext * pc = value;
514 struct Session * ps = pc->head;
515 struct Session * tmp = NULL;
516 struct HTTP_Message * msg = NULL;
517 struct HTTP_Message * msg_tmp = NULL;
519 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Freeing context for peer `%s'\n",GNUNET_i2s(&pc->identity));
521 GNUNET_CONTAINER_multihashmap_remove (plugin->peers, &pc->identity.hashPubKey, pc);
524 plugin->env->session_end(plugin, &pc->identity, ps);
527 GNUNET_free_non_null (ps->addr);
528 GNUNET_free(ps->url);
529 if (ps->msgtok != NULL)
530 GNUNET_SERVER_mst_destroy (ps->msgtok);
532 msg = ps->pending_msgs_head;
539 if (ps->direction==OUTBOUND)
541 if (ps->send_endpoint!=NULL)
542 curl_easy_cleanup(ps->send_endpoint);
543 if (ps->recv_endpoint!=NULL)
544 curl_easy_cleanup(ps->recv_endpoint);
551 GNUNET_STATISTICS_update (plugin->env->stats,
552 gettext_noop ("# HTTP peers active"),
560 * Removes a session from the linked list of sessions
561 * @param pc peer context
563 * @param call_msg_cont GNUNET_YES to call pending message continuations, otherwise no
564 * @param call_msg_cont_result result to call message continuations with
565 * @return GNUNET_SYSERR if msg not found, GNUNET_OK on success
567 static int remove_session (struct HTTP_PeerContext * pc, struct Session * ps, int call_msg_cont, int call_msg_cont_result)
569 struct HTTP_Message * msg;
570 struct Plugin * plugin = ps->peercontext->plugin;
572 #if DEBUG_CONNECTIONS
573 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);
575 plugin->env->session_end(plugin, &pc->identity, ps);
577 GNUNET_free_non_null (ps->addr);
578 GNUNET_SERVER_mst_destroy (ps->msgtok);
579 GNUNET_free(ps->url);
581 if (ps->direction==INBOUND)
583 if (ps->recv_endpoint != NULL)
585 curl_easy_cleanup(ps->recv_endpoint);
586 ps->recv_endpoint = NULL;
588 if (ps->send_endpoint != NULL)
590 curl_easy_cleanup(ps->send_endpoint);
591 ps->send_endpoint = NULL;
595 msg = ps->pending_msgs_head;
598 if ((call_msg_cont == GNUNET_YES) && (msg->transmit_cont!=NULL))
600 msg->transmit_cont (msg->transmit_cont_cls,&pc->identity,call_msg_cont_result);
602 GNUNET_CONTAINER_DLL_remove(ps->pending_msgs_head,ps->pending_msgs_head,msg);
604 msg = ps->pending_msgs_head;
607 GNUNET_CONTAINER_DLL_remove(pc->head,pc->tail,ps);
611 /* no sessions left remove peer */
615 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"No sessions left for peer `%s', removing context\n",GNUNET_i2s(&pc->identity));
617 remove_peer_context_Iterator(plugin, &pc->identity.hashPubKey, pc);
625 * Add the IP of our network interface to the list of
626 * our external IP addresses.
628 * @param cls the 'struct Plugin*'
629 * @param name name of the interface
630 * @param isDefault do we think this may be our default interface
631 * @param addr address of the interface
632 * @param addrlen number of bytes in addr
633 * @return GNUNET_OK to continue iterating
636 process_interfaces (void *cls,
639 const struct sockaddr *addr, socklen_t addrlen)
641 struct Plugin *plugin = cls;
642 struct IPv4HttpAddress * t4;
643 struct IPv6HttpAddress * t6;
647 GNUNET_assert(cls !=NULL);
648 af = addr->sa_family;
649 if ((af == AF_INET) && (plugin->use_ipv4 == GNUNET_YES) && (plugin->bind6_address == NULL))
651 struct in_addr bnd_cmp = ((struct sockaddr_in *) addr)->sin_addr;
652 t4 = GNUNET_malloc(sizeof(struct IPv4HttpAddress));
653 /* Not skipping loopback addresses
654 if (INADDR_LOOPBACK == ntohl(((struct sockaddr_in *) addr)->sin_addr.s_addr))
660 t4->ipv4_addr = ((struct sockaddr_in *) addr)->sin_addr.s_addr;
661 t4->u_port = htons (plugin->port_inbound);
662 if (plugin->bind4_address != NULL)
664 if (0 == memcmp(&plugin->bind4_address->sin_addr, &bnd_cmp, sizeof (struct in_addr)))
666 plugin->env->notify_address(plugin->env->cls,PROTOCOL_PREFIX,t4, sizeof (struct IPv4HttpAddress), GNUNET_TIME_UNIT_FOREVER_REL);
671 plugin->env->notify_address(plugin->env->cls,PROTOCOL_PREFIX,t4, sizeof (struct IPv4HttpAddress), GNUNET_TIME_UNIT_FOREVER_REL);
675 else if ((af == AF_INET6) && (plugin->use_ipv6 == GNUNET_YES) && (plugin->bind4_address == NULL))
677 struct in6_addr bnd_cmp6 = ((struct sockaddr_in6 *) addr)->sin6_addr;
678 if (IN6_IS_ADDR_LINKLOCAL (&((struct sockaddr_in6 *) addr)->sin6_addr))
682 t6 = GNUNET_malloc(sizeof(struct IPv6HttpAddress));
683 GNUNET_assert(t6 != NULL);
684 if (plugin->bind6_address != NULL)
686 if (0 == memcmp(&plugin->bind6_address->sin6_addr, &bnd_cmp6, sizeof (struct in6_addr)))
688 memcpy (&t6->ipv6_addr,
689 &((struct sockaddr_in6 *) addr)->sin6_addr,
690 sizeof (struct in6_addr));
691 t6->u6_port = htons (plugin->port_inbound);
692 plugin->env->notify_address(plugin->env->cls,PROTOCOL_PREFIX,t6,sizeof (struct IPv6HttpAddress) , GNUNET_TIME_UNIT_FOREVER_REL);
697 memcpy (&t6->ipv6_addr,
698 &((struct sockaddr_in6 *) addr)->sin6_addr,
699 sizeof (struct in6_addr));
700 t6->u6_port = htons (plugin->port_inbound);
701 plugin->env->notify_address(plugin->env->cls,PROTOCOL_PREFIX,t6,sizeof (struct IPv6HttpAddress) , GNUNET_TIME_UNIT_FOREVER_REL);
710 * External logging function for MHD
711 * @param arg arguments
712 * @param fmt format string
713 * @param ap list of arguments
715 void mhd_logger (void * arg, const char * fmt, va_list ap)
718 vsnprintf(text, 1024, fmt, ap);
720 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,"MHD: %s \n", text);
724 static void mhd_termination_cb (void *cls, struct MHD_Connection * connection, void **httpSessionCache)
726 struct Session * ps = *httpSessionCache;
729 struct HTTP_PeerContext * pc = ps->peercontext;
731 if (connection==ps->recv_endpoint)
733 #if DEBUG_CONNECTIONS
734 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: inbound connection from peer `%s' was terminated\n", ps, GNUNET_i2s(&pc->identity));
736 ps->recv_active = GNUNET_NO;
737 ps->recv_connected = GNUNET_NO;
738 ps->recv_endpoint = NULL;
740 if (connection==ps->send_endpoint)
743 ps->send_active = GNUNET_NO;
744 ps->send_connected = GNUNET_NO;
745 ps->send_endpoint = NULL;
746 #if DEBUG_CONNECTIONS
747 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: outbound connection from peer `%s' was terminated\n", ps, GNUNET_i2s(&pc->identity));
751 /* if both connections disconnected, remove session */
752 if ((ps->send_connected == GNUNET_NO) && (ps->recv_connected == GNUNET_NO))
754 GNUNET_STATISTICS_update (pc->plugin->env->stats,
755 gettext_noop ("# HTTP inbound sessions for peers active"),
758 remove_session(pc,ps,GNUNET_YES,GNUNET_SYSERR);
763 * Callback called by MessageStreamTokenizer when a message has arrived
764 * @param cls current session as closure
765 * @param client clien
766 * @param message the message to be forwarded to transport service
769 static void mhd_write_mst_cb (void *cls,
771 const struct GNUNET_MessageHeader *message)
774 struct Session *ps = cls;
775 GNUNET_assert(ps != NULL);
777 struct HTTP_PeerContext *pc = ps->peercontext;
778 GNUNET_assert(pc != NULL);
780 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
781 "Connection %X: Forwarding message to transport service, type %u and size %u from `%s' (`%s')\n",
783 ntohs(message->type),
784 ntohs(message->size),
785 GNUNET_i2s(&(ps->peercontext)->identity),http_plugin_address_to_string(NULL,ps->addr,ps->addrlen));
787 pc->plugin->env->receive (ps->peercontext->plugin->env->cls,
795 * Check if incoming connection is accepted.
796 * NOTE: Here every connection is accepted
797 * @param cls plugin as closure
798 * @param addr address of incoming connection
799 * @param addr_len address length of incoming connection
800 * @return MHD_YES if connection is accepted, MHD_NO if connection is rejected
804 mhd_accept_cb (void *cls, const struct sockaddr *addr, socklen_t addr_len)
807 struct Plugin *plugin = cls;
809 /* Every connection is accepted, nothing more to do here */
815 * Callback called by MHD when it needs data to send
816 * @param cls current session
817 * @param pos position in buffer
818 * @param buf the buffer to write data to
819 * @param max max number of bytes available in buffer
820 * @return bytes written to buffer
823 mhd_send_callback (void *cls, uint64_t pos, char *buf, size_t max)
825 struct Session * ps = cls;
826 struct HTTP_PeerContext * pc;
827 struct HTTP_Message * msg;
830 GNUNET_assert (ps!=NULL);
832 pc = ps->peercontext;
833 msg = ps->pending_msgs_tail;
834 if (ps->send_force_disconnect==GNUNET_YES)
836 #if DEBUG_CONNECTIONS
837 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: outbound forced to disconnect\n",ps);
844 if ((msg->size-msg->pos) <= max)
846 memcpy(buf,&msg->buf[msg->pos],(msg->size-msg->pos));
847 bytes_read = msg->size-msg->pos;
848 msg->pos+=(msg->size-msg->pos);
852 memcpy(buf,&msg->buf[msg->pos],max);
857 if (msg->pos==msg->size)
859 if (NULL!=msg->transmit_cont)
860 msg->transmit_cont (msg->transmit_cont_cls,&pc->identity,GNUNET_OK);
861 remove_http_message(ps,msg);
868 * Process GET or PUT request received via MHD. For
869 * GET, queue response that will send back our pending
870 * messages. For PUT, process incoming data and send
871 * to GNUnet core. In either case, check if a session
872 * already exists and create a new one if not.
875 mdh_access_cb (void *cls,
876 struct MHD_Connection *mhd_connection,
880 const char *upload_data,
881 size_t * upload_data_size, void **httpSessionCache)
883 struct Plugin *plugin = cls;
884 struct MHD_Response *response;
885 const union MHD_ConnectionInfo * conn_info;
887 struct sockaddr_in *addrin;
888 struct sockaddr_in6 *addrin6;
890 char address[INET6_ADDRSTRLEN+14];
891 struct GNUNET_PeerIdentity pi_in;
894 struct IPv4HttpAddress ipv4addr;
895 struct IPv6HttpAddress ipv6addr;
897 struct HTTP_PeerContext *pc;
898 struct Session *ps = NULL;
899 struct Session *ps_tmp = NULL;
902 int send_error_to_client;
904 size_t addr_len = 0 ;
906 GNUNET_assert(cls !=NULL);
907 send_error_to_client = GNUNET_NO;
909 if (NULL == *httpSessionCache)
911 /* check url for peer identity , if invalid send HTTP 404*/
912 size_t len = strlen(&url[1]);
913 char * peer = GNUNET_malloc(104+1);
915 if ((len>104) && (url[104]==';'))
917 char * id = GNUNET_malloc((len-104)+1);
918 strcpy(id,&url[105]);
919 memcpy(peer,&url[1],103);
921 id_num = strtoul ( id, NULL , 10);
924 res = GNUNET_CRYPTO_hash_from_string (peer, &(pi_in.hashPubKey));
926 if ( GNUNET_SYSERR == res )
928 response = MHD_create_response_from_data (strlen (HTTP_ERROR_RESPONSE),HTTP_ERROR_RESPONSE, MHD_NO, MHD_NO);
929 res = MHD_queue_response (mhd_connection, MHD_HTTP_NOT_FOUND, response);
930 MHD_destroy_response (response);
931 #if DEBUG_CONNECTIONS
933 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Peer has no valid ident, sent HTTP 1.1/404\n");
935 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Peer has no valid ident, could not send error\n");
942 ps = *httpSessionCache;
943 pc = ps->peercontext;
946 if (NULL == *httpSessionCache)
948 /* get peer context */
949 pc = GNUNET_CONTAINER_multihashmap_get (plugin->peers, &pi_in.hashPubKey);
953 pc = GNUNET_malloc(sizeof (struct HTTP_PeerContext));
955 pc->session_id_counter=1;
956 pc->last_session = NULL;
957 memcpy(&pc->identity, &pi_in, sizeof(struct GNUNET_PeerIdentity));
958 GNUNET_CONTAINER_multihashmap_put(plugin->peers, &pc->identity.hashPubKey, pc, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
959 GNUNET_STATISTICS_update (plugin->env->stats,
960 gettext_noop ("# HTTP peers active"),
965 conn_info = MHD_get_connection_info(mhd_connection, MHD_CONNECTION_INFO_CLIENT_ADDRESS );
966 /* Incoming IPv4 connection */
967 if ( AF_INET == conn_info->client_addr->sin_family)
969 addrin = conn_info->client_addr;
970 inet_ntop(addrin->sin_family, &(addrin->sin_addr),address,INET_ADDRSTRLEN);
971 memcpy(&ipv4addr.ipv4_addr,&(addrin->sin_addr),sizeof(struct in_addr));
972 ipv4addr.u_port = addrin->sin_port;
974 addr_len = sizeof(struct IPv4HttpAddress);
976 /* Incoming IPv6 connection */
977 if ( AF_INET6 == conn_info->client_addr->sin_family)
979 addrin6 = (struct sockaddr_in6 *) conn_info->client_addr;
980 inet_ntop(addrin6->sin6_family, &(addrin6->sin6_addr),address,INET6_ADDRSTRLEN);
981 memcpy(&ipv6addr.ipv6_addr,&(addrin6->sin6_addr),sizeof(struct in6_addr));
982 ipv6addr.u6_port = addrin6->sin6_port;
984 addr_len = sizeof(struct IPv6HttpAddress);
987 GNUNET_assert (addr != NULL);
988 GNUNET_assert (addr_len != 0);
991 /* only inbound sessions here */
996 if ((ps_tmp->direction==INBOUND) && (ps_tmp->session_id == id_num) && (id_num!=0))
998 if ((ps_tmp->recv_force_disconnect!=GNUNET_YES) && (ps_tmp->send_force_disconnect!=GNUNET_YES))
1002 ps_tmp=ps_tmp->next;
1007 ps = GNUNET_malloc(sizeof (struct Session));
1008 ps->addr = GNUNET_malloc(addr_len);
1009 memcpy(ps->addr,addr,addr_len);
1010 ps->addrlen = addr_len;
1011 ps->direction=INBOUND;
1012 ps->pending_msgs_head = NULL;
1013 ps->pending_msgs_tail = NULL;
1014 ps->send_connected=GNUNET_NO;
1015 ps->send_active=GNUNET_NO;
1016 ps->recv_connected=GNUNET_NO;
1017 ps->recv_active=GNUNET_NO;
1019 ps->session_id =id_num;
1020 ps->url = create_url (plugin, ps->addr, ps->addrlen, ps->session_id);
1021 GNUNET_CONTAINER_DLL_insert(pc->head,pc->tail,ps);
1022 GNUNET_STATISTICS_update (plugin->env->stats,
1023 gettext_noop ("# HTTP inbound sessions for peers active"),
1028 *httpSessionCache = ps;
1029 if (ps->msgtok==NULL)
1030 ps->msgtok = GNUNET_SERVER_mst_create (&mhd_write_mst_cb, ps);
1032 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: HTTP Daemon has new an incoming `%s' request from peer `%s' (`%s')\n",
1035 GNUNET_i2s(&pc->identity),
1036 http_plugin_address_to_string(NULL, ps->addr, ps->addrlen));
1040 /* Is it a PUT or a GET request */
1041 if (0 == strcmp (MHD_HTTP_METHOD_PUT, method))
1043 if (ps->recv_force_disconnect == GNUNET_YES)
1045 #if DEBUG_CONNECTIONS
1046 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: inbound connection was forced to disconnect\n",ps);
1048 ps->recv_active = GNUNET_NO;
1051 if ((*upload_data_size == 0) && (ps->recv_active==GNUNET_NO))
1053 ps->recv_endpoint = mhd_connection;
1054 ps->recv_connected = GNUNET_YES;
1055 ps->recv_active = GNUNET_YES;
1056 ps->recv_force_disconnect = GNUNET_NO;
1057 #if DEBUG_CONNECTIONS
1058 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: inbound PUT connection connected\n",ps);
1063 /* Transmission of all data complete */
1064 if ((*upload_data_size == 0) && (ps->recv_active == GNUNET_YES))
1066 response = MHD_create_response_from_data (strlen (HTTP_PUT_RESPONSE),HTTP_PUT_RESPONSE, MHD_NO, MHD_NO);
1067 res = MHD_queue_response (mhd_connection, MHD_HTTP_OK, response);
1068 #if DEBUG_CONNECTIONS
1069 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: Sent HTTP/1.1: 200 OK as PUT Response\n",ps);
1071 MHD_destroy_response (response);
1072 ps->recv_active=GNUNET_NO;
1076 /* Recieving data */
1077 if ((*upload_data_size > 0) && (ps->recv_active == GNUNET_YES))
1079 res = GNUNET_SERVER_mst_receive(ps->msgtok, ps, upload_data,*upload_data_size, GNUNET_NO, GNUNET_NO);
1080 (*upload_data_size) = 0;
1086 if ( 0 == strcmp (MHD_HTTP_METHOD_GET, method) )
1088 if (ps->send_force_disconnect == GNUNET_YES)
1090 #if DEBUG_CONNECTIONS
1091 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: outbound connection was forced to disconnect\n",ps);
1093 ps->send_active = GNUNET_NO;
1096 ps->send_connected = GNUNET_YES;
1097 ps->send_active = GNUNET_YES;
1098 ps->send_endpoint = mhd_connection;
1099 ps->send_force_disconnect = GNUNET_NO;
1100 #if DEBUG_CONNECTIONS
1101 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: inbound GET connection connected\n",ps);
1103 response = MHD_create_response_from_callback(-1,32 * 1024, &mhd_send_callback, ps, NULL);
1104 res = MHD_queue_response (mhd_connection, MHD_HTTP_OK, response);
1105 MHD_destroy_response (response);
1112 * Function that queries MHD's select sets and
1113 * starts the task waiting for them.
1114 * @param plugin plugin
1115 * @param daemon_handle the MHD daemon handle
1116 * @return gnunet task identifier
1118 static GNUNET_SCHEDULER_TaskIdentifier
1119 http_server_daemon_prepare (struct Plugin *plugin , struct MHD_Daemon *daemon_handle)
1121 GNUNET_SCHEDULER_TaskIdentifier ret;
1125 struct GNUNET_NETWORK_FDSet *wrs;
1126 struct GNUNET_NETWORK_FDSet *wws;
1127 struct GNUNET_NETWORK_FDSet *wes;
1129 unsigned long long timeout;
1131 struct GNUNET_TIME_Relative tv;
1133 ret = GNUNET_SCHEDULER_NO_TASK;
1137 wrs = GNUNET_NETWORK_fdset_create ();
1138 wes = GNUNET_NETWORK_fdset_create ();
1139 wws = GNUNET_NETWORK_fdset_create ();
1141 GNUNET_assert (MHD_YES ==
1142 MHD_get_fdset (daemon_handle,
1147 haveto = MHD_get_timeout (daemon_handle, &timeout);
1148 if (haveto == MHD_YES)
1149 tv.value = (uint64_t) timeout;
1151 tv = GNUNET_TIME_UNIT_SECONDS;
1152 GNUNET_NETWORK_fdset_copy_native (wrs, &rs, max);
1153 GNUNET_NETWORK_fdset_copy_native (wws, &ws, max);
1154 GNUNET_NETWORK_fdset_copy_native (wes, &es, max);
1155 if (daemon_handle == plugin->http_server_daemon_v4)
1157 if (plugin->http_server_task_v4 != GNUNET_SCHEDULER_NO_TASK)
1159 GNUNET_SCHEDULER_cancel(plugin->env->sched, plugin->http_server_task_v4);
1160 plugin->http_server_daemon_v4 = GNUNET_SCHEDULER_NO_TASK;
1163 ret = GNUNET_SCHEDULER_add_select (plugin->env->sched,
1164 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1165 GNUNET_SCHEDULER_NO_TASK,
1169 &http_server_daemon_v4_run,
1172 if (daemon_handle == plugin->http_server_daemon_v6)
1174 if (plugin->http_server_task_v6 != GNUNET_SCHEDULER_NO_TASK)
1176 GNUNET_SCHEDULER_cancel(plugin->env->sched, plugin->http_server_task_v6);
1177 plugin->http_server_task_v6 = GNUNET_SCHEDULER_NO_TASK;
1180 ret = GNUNET_SCHEDULER_add_select (plugin->env->sched,
1181 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1182 GNUNET_SCHEDULER_NO_TASK,
1186 &http_server_daemon_v6_run,
1189 GNUNET_NETWORK_fdset_destroy (wrs);
1190 GNUNET_NETWORK_fdset_destroy (wws);
1191 GNUNET_NETWORK_fdset_destroy (wes);
1196 * Call MHD IPv4 to process pending requests and then go back
1197 * and schedule the next run.
1198 * @param cls plugin as closure
1199 * @param tc task context
1201 static void http_server_daemon_v4_run (void *cls,
1202 const struct GNUNET_SCHEDULER_TaskContext *tc)
1204 struct Plugin *plugin = cls;
1206 #if DEBUG_SCHEDULING
1207 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_READ_READY))
1208 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,"http_server_daemon_v4_run: GNUNET_SCHEDULER_REASON_READ_READY\n");
1209 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_WRITE_READY))
1210 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,"http_server_daemon_v4_run: GNUNET_SCHEDULER_REASON_WRITE_READY\n");
1211 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_TIMEOUT))
1212 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,"http_server_daemon_v4_run: GNUNET_SCHEDULER_REASON_TIMEOUT\n");
1213 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_STARTUP))
1214 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,"http_server_daemon_v4_run: GGNUNET_SCHEDULER_REASON_STARTUP\n");
1215 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1216 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,"http_server_daemon_v4_run: GGNUNET_SCHEDULER_REASON_SHUTDOWN\n");
1219 GNUNET_assert(cls !=NULL);
1220 plugin->http_server_task_v4 = GNUNET_SCHEDULER_NO_TASK;
1222 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1225 GNUNET_assert (MHD_YES == MHD_run (plugin->http_server_daemon_v4));
1226 plugin->http_server_task_v4 = http_server_daemon_prepare (plugin, plugin->http_server_daemon_v4);
1231 * Call MHD IPv6 to process pending requests and then go back
1232 * and schedule the next run.
1233 * @param cls plugin as closure
1234 * @param tc task context
1236 static void http_server_daemon_v6_run (void *cls,
1237 const struct GNUNET_SCHEDULER_TaskContext *tc)
1239 struct Plugin *plugin = cls;
1241 #if DEBUG_SCHEDULING
1242 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_READ_READY))
1243 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,"http_server_daemon_v6_run: GNUNET_SCHEDULER_REASON_READ_READY\n");
1244 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_WRITE_READY))
1245 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,"http_server_daemon_v6_run: GNUNET_SCHEDULER_REASON_WRITE_READY\n");
1246 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_TIMEOUT))
1247 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,"http_server_daemon_v6_run: GNUNET_SCHEDULER_REASON_TIMEOUT\n");
1248 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_STARTUP))
1249 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,"http_server_daemon_v6_run: GGNUNET_SCHEDULER_REASON_STARTUP\n");
1250 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1251 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,"http_server_daemon_v6_run: GGNUNET_SCHEDULER_REASON_SHUTDOWN\n");
1254 GNUNET_assert(cls !=NULL);
1255 plugin->http_server_task_v6 = GNUNET_SCHEDULER_NO_TASK;
1257 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1260 GNUNET_assert (MHD_YES == MHD_run (plugin->http_server_daemon_v6));
1261 plugin->http_server_task_v6 = http_server_daemon_prepare (plugin, plugin->http_server_daemon_v6);
1264 static size_t curl_get_header_cb( void *ptr, size_t size, size_t nmemb, void *stream)
1266 struct Session * ps = stream;
1268 long http_result = 0;
1270 /* Getting last http result code */
1271 GNUNET_assert(NULL!=ps);
1272 if (ps->recv_connected==GNUNET_NO)
1274 res = curl_easy_getinfo(ps->recv_endpoint, CURLINFO_RESPONSE_CODE, &http_result);
1275 if (CURLE_OK == res)
1277 if (http_result == 200)
1279 ps->recv_connected = GNUNET_YES;
1280 ps->recv_active = GNUNET_YES;
1281 #if DEBUG_CONNECTIONS
1282 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: connected to recieve data\n",ps);
1284 // Calling send_check_connections again since receive is established
1285 send_check_connections (ps->peercontext->plugin, ps);
1292 size_t len = size * nmemb;
1294 if ((size * nmemb) < SIZE_MAX)
1295 tmp = GNUNET_malloc (len+1);
1297 if ((tmp != NULL) && (len > 0))
1299 memcpy(tmp,ptr,len);
1302 if (tmp[len-2] == 13)
1305 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: Header: %s\n",ps,tmp);
1307 GNUNET_free_non_null (tmp);
1310 return size * nmemb;
1314 * Callback called by libcurl when new headers arrive
1315 * Used to get HTTP result for curl operations
1316 * @param ptr stream to read from
1317 * @param size size of one char element
1318 * @param nmemb number of char elements
1319 * @param stream closure set by user
1320 * @return bytes read by function
1323 static size_t curl_put_header_cb( void *ptr, size_t size, size_t nmemb, void *stream)
1325 struct Session * ps = stream;
1328 size_t len = size * nmemb;
1329 long http_result = 0;
1332 /* Getting last http result code */
1333 GNUNET_assert(NULL!=ps);
1334 res = curl_easy_getinfo(ps->send_endpoint, CURLINFO_RESPONSE_CODE, &http_result);
1335 if (CURLE_OK == res)
1337 if ((http_result == 100) && (ps->send_connected==GNUNET_NO))
1339 ps->send_connected = GNUNET_YES;
1340 ps->send_active = GNUNET_YES;
1341 #if DEBUG_CONNECTIONS
1342 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: connected to send data\n",ps);
1345 if ((http_result == 200) && (ps->send_connected==GNUNET_YES))
1347 ps->send_connected = GNUNET_NO;
1348 ps->send_active = GNUNET_NO;
1349 #if DEBUG_CONNECTIONS
1350 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: sending disconnected\n",ps);
1356 if ((size * nmemb) < SIZE_MAX)
1357 tmp = GNUNET_malloc (len+1);
1359 if ((tmp != NULL) && (len > 0))
1361 memcpy(tmp,ptr,len);
1364 if (tmp[len-2] == 13)
1369 GNUNET_free_non_null (tmp);
1371 return size * nmemb;
1375 * Callback method used with libcurl
1376 * Method is called when libcurl needs to read data during sending
1377 * @param stream pointer where to write data
1378 * @param size size of an individual element
1379 * @param nmemb count of elements that can be written to the buffer
1380 * @param ptr source pointer, passed to the libcurl handle
1381 * @return bytes written to stream
1383 static size_t curl_send_cb(void *stream, size_t size, size_t nmemb, void *ptr)
1385 struct Session * ps = ptr;
1386 struct HTTP_Message * msg = ps->pending_msgs_tail;
1390 if (ps->send_active == GNUNET_NO)
1392 return CURL_READFUNC_PAUSE;
1395 if ((ps->pending_msgs_tail == NULL) && (ps->send_active == GNUNET_YES))
1397 #if DEBUG_CONNECTIONS
1398 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: No Message to send, pausing connection\n",ps);
1400 ps->send_active = GNUNET_NO;
1401 return CURL_READFUNC_PAUSE;
1404 GNUNET_assert (msg!=NULL);
1407 if (msg->pos < msg->size)
1409 /* data fit in buffer */
1410 if ((msg->size - msg->pos) <= (size * nmemb))
1412 len = (msg->size - msg->pos);
1413 memcpy(stream, &msg->buf[msg->pos], len);
1420 memcpy(stream, &msg->buf[msg->pos], len);
1425 /* no data to send */
1431 if ( msg->pos == msg->size)
1433 #if DEBUG_CONNECTIONS
1434 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,"Connection %X: Message with %u bytes sent, removing message from queue \n",ps, msg->pos);
1436 /* Calling transmit continuation */
1437 if (NULL != ps->pending_msgs_tail->transmit_cont)
1438 msg->transmit_cont (ps->pending_msgs_tail->transmit_cont_cls,&(ps->peercontext)->identity,GNUNET_OK);
1439 remove_http_message(ps, msg);
1444 static void curl_receive_mst_cb (void *cls,
1446 const struct GNUNET_MessageHeader *message)
1448 struct Session *ps = cls;
1449 GNUNET_assert(ps != NULL);
1451 struct HTTP_PeerContext *pc = ps->peercontext;
1452 GNUNET_assert(pc != NULL);
1454 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1455 "Connection %X: Forwarding message to transport service, type %u and size %u from `%s' (`%s')\n",
1457 ntohs(message->type),
1458 ntohs(message->size),
1459 GNUNET_i2s(&(pc->identity)),http_plugin_address_to_string(NULL,ps->addr,ps->addrlen));
1461 pc->plugin->env->receive (pc->plugin->env->cls,
1470 * Callback method used with libcurl
1471 * Method is called when libcurl needs to write data during sending
1472 * @param stream pointer where to write data
1473 * @param size size of an individual element
1474 * @param nmemb count of elements that can be written to the buffer
1475 * @param ptr destination pointer, passed to the libcurl handle
1476 * @return bytes read from stream
1478 static size_t curl_receive_cb( void *stream, size_t size, size_t nmemb, void *ptr)
1480 struct Session * ps = ptr;
1481 #if DEBUG_CONNECTIONS
1482 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: %u bytes received\n",ps, size*nmemb);
1484 GNUNET_SERVER_mst_receive(ps->msgtok, ps, stream, size*nmemb, GNUNET_NO, GNUNET_NO);
1485 return (size * nmemb);
1489 static void curl_handle_finished (struct Plugin *plugin)
1491 struct Session *ps = NULL;
1492 struct HTTP_PeerContext *pc = NULL;
1493 struct CURLMsg *msg;
1494 struct HTTP_Message * cur_msg = NULL;
1502 msg = curl_multi_info_read (plugin->multi_handle, &msgs_in_queue);
1503 if ((msgs_in_queue == 0) || (msg == NULL))
1505 /* get session for affected curl handle */
1506 GNUNET_assert ( msg->easy_handle != NULL );
1507 curl_easy_getinfo(msg->easy_handle, CURLINFO_PRIVATE, &tmp);
1508 ps = (struct Session *) tmp;
1509 GNUNET_assert ( ps != NULL );
1510 pc = ps->peercontext;
1511 GNUNET_assert ( pc != NULL );
1516 if ( (msg->data.result != CURLE_OK) &&
1517 (msg->data.result != CURLE_GOT_NOTHING) )
1519 /* sending msg failed*/
1520 if (msg->easy_handle == ps->send_endpoint)
1522 #if DEBUG_CONNECTIONS
1523 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1524 _("Connection %X: HTTP PUT to peer `%s' (`%s') failed: `%s' `%s'\n"),
1526 GNUNET_i2s(&pc->identity),
1527 http_plugin_address_to_string(NULL, ps->addr, ps->addrlen),
1528 "curl_multi_perform",
1529 curl_easy_strerror (msg->data.result));
1531 ps->send_connected = GNUNET_NO;
1532 ps->send_active = GNUNET_NO;
1533 curl_multi_remove_handle(plugin->multi_handle,ps->send_endpoint);
1534 //curl_easy_cleanup(ps->send_endpoint);
1535 //ps->send_endpoint=NULL;
1536 cur_msg = ps->pending_msgs_tail;
1537 if (( NULL != cur_msg) && ( NULL != cur_msg->transmit_cont))
1538 cur_msg->transmit_cont (cur_msg->transmit_cont_cls,&pc->identity,GNUNET_SYSERR);
1540 /* GET connection failed */
1541 if (msg->easy_handle == ps->recv_endpoint)
1543 #if DEBUG_CONNECTIONS
1544 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1545 _("Connection %X: HTTP GET to peer `%s' (`%s') failed: `%s' `%s'\n"),
1547 GNUNET_i2s(&pc->identity),
1548 http_plugin_address_to_string(NULL, ps->addr, ps->addrlen),
1549 "curl_multi_perform",
1550 curl_easy_strerror (msg->data.result));
1552 ps->recv_connected = GNUNET_NO;
1553 ps->recv_active = GNUNET_NO;
1554 curl_multi_remove_handle(plugin->multi_handle,ps->recv_endpoint);
1555 //curl_easy_cleanup(ps->recv_endpoint);
1556 //ps->recv_endpoint=NULL;
1561 if (msg->easy_handle == ps->send_endpoint)
1563 GNUNET_assert (CURLE_OK == curl_easy_getinfo(msg->easy_handle, CURLINFO_RESPONSE_CODE, &http_result));
1564 #if DEBUG_CONNECTIONS
1565 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1566 "Connection %X: HTTP PUT connection to peer `%s' (`%s') was closed with HTTP code %u\n",
1568 GNUNET_i2s(&pc->identity),
1569 http_plugin_address_to_string(NULL, ps->addr, ps->addrlen),
1572 /* Calling transmit continuation */
1573 cur_msg = ps->pending_msgs_tail;
1574 if (( NULL != cur_msg) && (NULL != cur_msg->transmit_cont))
1576 /* HTTP 1xx : Last message before here was informational */
1577 if ((http_result >=100) && (http_result < 200))
1578 cur_msg->transmit_cont (cur_msg->transmit_cont_cls,&pc->identity,GNUNET_OK);
1579 /* HTTP 2xx: successful operations */
1580 if ((http_result >=200) && (http_result < 300))
1581 cur_msg->transmit_cont (cur_msg->transmit_cont_cls,&pc->identity,GNUNET_OK);
1582 /* HTTP 3xx..5xx: error */
1583 if ((http_result >=300) && (http_result < 600))
1584 cur_msg->transmit_cont (cur_msg->transmit_cont_cls,&pc->identity,GNUNET_SYSERR);
1586 ps->send_connected = GNUNET_NO;
1587 ps->send_active = GNUNET_NO;
1588 curl_multi_remove_handle(plugin->multi_handle,ps->send_endpoint);
1589 //curl_easy_cleanup(ps->send_endpoint);
1590 //ps->send_endpoint =NULL;
1592 if (msg->easy_handle == ps->recv_endpoint)
1594 #if DEBUG_CONNECTIONS
1595 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1596 "Connection %X: HTTP GET connection to peer `%s' (`%s') was closed with HTTP code %u\n",
1598 GNUNET_i2s(&pc->identity),
1599 http_plugin_address_to_string(NULL, ps->addr, ps->addrlen),
1602 ps->recv_connected = GNUNET_NO;
1603 ps->recv_active = GNUNET_NO;
1604 curl_multi_remove_handle(plugin->multi_handle,ps->recv_endpoint);
1605 //curl_easy_cleanup(ps->recv_endpoint);
1606 //ps->recv_endpoint=NULL;
1609 if ((ps->recv_connected == GNUNET_NO) && (ps->send_connected == GNUNET_NO))
1610 remove_session (pc, ps, GNUNET_YES, GNUNET_SYSERR);
1616 while ( (msgs_in_queue > 0) );
1621 * Task performing curl operations
1622 * @param cls plugin as closure
1623 * @param tc gnunet scheduler task context
1625 static void curl_perform (void *cls,
1626 const struct GNUNET_SCHEDULER_TaskContext *tc)
1628 struct Plugin *plugin = cls;
1629 static unsigned int handles_last_run;
1633 GNUNET_assert(cls !=NULL);
1635 plugin->http_curl_task = GNUNET_SCHEDULER_NO_TASK;
1636 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1641 mret = curl_multi_perform (plugin->multi_handle, &running);
1642 if ((running < handles_last_run) && (running>0))
1643 curl_handle_finished(plugin);
1644 handles_last_run = running;
1646 while (mret == CURLM_CALL_MULTI_PERFORM);
1647 curl_schedule(plugin);
1652 * Function setting up file descriptors and scheduling task to run
1654 * @param cls plugin as closure
1655 * @return GNUNET_SYSERR for hard failure, GNUNET_OK for ok
1657 static int curl_schedule(struct Plugin *plugin)
1663 struct GNUNET_NETWORK_FDSet *grs;
1664 struct GNUNET_NETWORK_FDSet *gws;
1668 /* Cancel previous scheduled task */
1669 if (plugin->http_curl_task != GNUNET_SCHEDULER_NO_TASK)
1671 GNUNET_SCHEDULER_cancel(plugin->env->sched, plugin->http_curl_task);
1672 plugin->http_curl_task = GNUNET_SCHEDULER_NO_TASK;
1679 mret = curl_multi_fdset (plugin->multi_handle, &rs, &ws, &es, &max);
1680 if (mret != CURLM_OK)
1682 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1683 _("%s failed at %s:%d: `%s'\n"),
1684 "curl_multi_fdset", __FILE__, __LINE__,
1685 curl_multi_strerror (mret));
1686 return GNUNET_SYSERR;
1688 mret = curl_multi_timeout (plugin->multi_handle, &to);
1689 if (mret != CURLM_OK)
1691 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1692 _("%s failed at %s:%d: `%s'\n"),
1693 "curl_multi_timeout", __FILE__, __LINE__,
1694 curl_multi_strerror (mret));
1695 return GNUNET_SYSERR;
1698 grs = GNUNET_NETWORK_fdset_create ();
1699 gws = GNUNET_NETWORK_fdset_create ();
1700 GNUNET_NETWORK_fdset_copy_native (grs, &rs, max + 1);
1701 GNUNET_NETWORK_fdset_copy_native (gws, &ws, max + 1);
1702 plugin->http_curl_task = GNUNET_SCHEDULER_add_select (plugin->env->sched,
1703 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1704 GNUNET_SCHEDULER_NO_TASK,
1705 (to == -1) ? GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5) : GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, to),
1710 GNUNET_NETWORK_fdset_destroy (gws);
1711 GNUNET_NETWORK_fdset_destroy (grs);
1716 * Function to log curl debug messages with GNUNET_log
1717 * @param curl handle
1718 * @param type curl_infotype
1721 * @param cls closure
1724 int curl_logger (CURL * curl, curl_infotype type , char * data, size_t size , void * cls)
1727 if (type == CURLINFO_TEXT)
1730 memcpy(text,data,size);
1731 if (text[size-1] == '\n')
1736 text[size+1] = '\0';
1738 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"CURL: Connection %X - %s", cls, text);
1744 * Function setting up curl handle and selecting message to send
1746 * @param plugin plugin
1748 * @return GNUNET_SYSERR on failure, GNUNET_NO if connecting, GNUNET_YES if ok
1750 static int send_check_connections (struct Plugin *plugin, struct Session *ps)
1753 struct HTTP_Message * msg;
1755 struct GNUNET_TIME_Relative timeout = GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT;
1757 if (ps->direction == OUTBOUND)
1759 /* RECV DIRECTION */
1760 /* Check if session is connected to receive data, otherwise connect to peer */
1761 if (ps->recv_connected == GNUNET_NO)
1763 int fresh = GNUNET_NO;
1764 if (ps->recv_endpoint == NULL)
1767 ps->recv_endpoint = curl_easy_init();
1770 curl_easy_setopt(ps->recv_endpoint, CURLOPT_VERBOSE, 1L);
1771 curl_easy_setopt(ps->recv_endpoint, CURLOPT_DEBUGFUNCTION , &curl_logger);
1772 curl_easy_setopt(ps->recv_endpoint, CURLOPT_DEBUGDATA , ps->recv_endpoint);
1775 curl_easy_setopt (ps->recv_endpoint, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1);
1776 curl_easy_setopt(ps->recv_endpoint, CURLOPT_SSL_VERIFYPEER, 0);
1777 curl_easy_setopt(ps->recv_endpoint, CURLOPT_SSL_VERIFYHOST, 0);
1779 curl_easy_setopt(ps->recv_endpoint, CURLOPT_URL, ps->url);
1780 curl_easy_setopt(ps->recv_endpoint, CURLOPT_HEADERFUNCTION, &curl_get_header_cb);
1781 curl_easy_setopt(ps->recv_endpoint, CURLOPT_WRITEHEADER, ps);
1782 curl_easy_setopt(ps->recv_endpoint, CURLOPT_READFUNCTION, curl_send_cb);
1783 curl_easy_setopt(ps->recv_endpoint, CURLOPT_READDATA, ps);
1784 curl_easy_setopt(ps->recv_endpoint, CURLOPT_WRITEFUNCTION, curl_receive_cb);
1785 curl_easy_setopt(ps->recv_endpoint, CURLOPT_WRITEDATA, ps);
1786 curl_easy_setopt(ps->recv_endpoint, CURLOPT_TIMEOUT, (long) timeout.value);
1787 curl_easy_setopt(ps->recv_endpoint, CURLOPT_PRIVATE, ps);
1788 curl_easy_setopt(ps->recv_endpoint, CURLOPT_CONNECTTIMEOUT, HTTP_CONNECT_TIMEOUT);
1789 curl_easy_setopt(ps->recv_endpoint, CURLOPT_BUFFERSIZE, 2*GNUNET_SERVER_MAX_MESSAGE_SIZE);
1790 #if CURL_TCP_NODELAY
1791 curl_easy_setopt(ps->recv_endpoint, CURLOPT_TCP_NODELAY, 1);
1794 if (fresh==GNUNET_YES)
1796 mret = curl_multi_add_handle(plugin->multi_handle, ps->recv_endpoint);
1797 if (mret != CURLM_OK)
1799 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1800 _("Connection: %X: %s failed at %s:%d: `%s'\n"),
1802 "curl_multi_add_handle", __FILE__, __LINE__,
1803 curl_multi_strerror (mret));
1804 return GNUNET_SYSERR;
1807 if (plugin->http_curl_task != GNUNET_SCHEDULER_NO_TASK)
1809 GNUNET_SCHEDULER_cancel(plugin->env->sched, plugin->http_curl_task);
1810 plugin->http_curl_task = GNUNET_SCHEDULER_NO_TASK;
1812 plugin->http_curl_task = GNUNET_SCHEDULER_add_now (plugin->env->sched, &curl_perform, plugin);
1815 /* waiting for receive direction */
1816 if (ps->recv_connected==GNUNET_NO)
1819 /* SEND DIRECTION */
1820 /* Check if session is connected to send data, otherwise connect to peer */
1821 if ((ps->send_connected == GNUNET_YES) && (ps->send_endpoint!= NULL))
1823 if (ps->send_active == GNUNET_YES)
1825 #if DEBUG_CONNECTIONS
1826 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: outbound active, enqueueing message\n",ps);
1830 if (ps->send_active == GNUNET_NO)
1832 #if DEBUG_CONNECTIONS
1833 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: outbound paused, unpausing existing connection and enqueueing message\n",ps);
1835 if (CURLE_OK == curl_easy_pause(ps->send_endpoint,CURLPAUSE_CONT))
1837 ps->send_active=GNUNET_YES;
1838 if (plugin->http_curl_task != GNUNET_SCHEDULER_NO_TASK)
1840 GNUNET_SCHEDULER_cancel(plugin->env->sched, plugin->http_curl_task);
1841 plugin->http_curl_task = GNUNET_SCHEDULER_NO_TASK;
1843 plugin->http_curl_task = GNUNET_SCHEDULER_add_now (plugin->env->sched, &curl_perform, plugin);
1847 return GNUNET_SYSERR;
1850 /* not connected, initiate connection */
1851 if (ps->send_connected==GNUNET_NO)
1853 int fresh = GNUNET_NO;
1854 if (NULL == ps->send_endpoint)
1856 ps->send_endpoint = curl_easy_init();
1859 GNUNET_assert (ps->send_endpoint != NULL);
1860 GNUNET_assert (NULL != ps->pending_msgs_tail);
1861 #if DEBUG_CONNECTIONS
1862 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: outbound not connected, initiating connection\n",ps);
1864 ps->send_active = GNUNET_NO;
1865 msg = ps->pending_msgs_tail;
1868 curl_easy_setopt(ps->send_endpoint, CURLOPT_VERBOSE, 1L);
1869 curl_easy_setopt(ps->send_endpoint, CURLOPT_DEBUGFUNCTION , &curl_logger);
1870 curl_easy_setopt(ps->send_endpoint, CURLOPT_DEBUGDATA , ps->send_endpoint);
1873 curl_easy_setopt (ps->send_endpoint, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1);
1874 curl_easy_setopt(ps->send_endpoint, CURLOPT_SSL_VERIFYPEER, 0);
1875 curl_easy_setopt(ps->send_endpoint, CURLOPT_SSL_VERIFYHOST, 0);
1877 curl_easy_setopt(ps->send_endpoint, CURLOPT_URL, ps->url);
1878 curl_easy_setopt(ps->send_endpoint, CURLOPT_PUT, 1L);
1879 curl_easy_setopt(ps->send_endpoint, CURLOPT_HEADERFUNCTION, &curl_put_header_cb);
1880 curl_easy_setopt(ps->send_endpoint, CURLOPT_WRITEHEADER, ps);
1881 curl_easy_setopt(ps->send_endpoint, CURLOPT_READFUNCTION, curl_send_cb);
1882 curl_easy_setopt(ps->send_endpoint, CURLOPT_READDATA, ps);
1883 curl_easy_setopt(ps->send_endpoint, CURLOPT_WRITEFUNCTION, curl_receive_cb);
1884 curl_easy_setopt(ps->send_endpoint, CURLOPT_READDATA, ps);
1885 curl_easy_setopt(ps->send_endpoint, CURLOPT_TIMEOUT, (long) timeout.value);
1886 curl_easy_setopt(ps->send_endpoint, CURLOPT_PRIVATE, ps);
1887 curl_easy_setopt(ps->send_endpoint, CURLOPT_CONNECTTIMEOUT, HTTP_CONNECT_TIMEOUT);
1888 curl_easy_setopt(ps->send_endpoint, CURLOPT_BUFFERSIZE, 2 * GNUNET_SERVER_MAX_MESSAGE_SIZE);
1889 #if CURL_TCP_NODELAY
1890 curl_easy_setopt(ps->send_endpoint, CURLOPT_TCP_NODELAY, 1);
1893 if (fresh==GNUNET_YES)
1895 mret = curl_multi_add_handle(plugin->multi_handle, ps->send_endpoint);
1896 if (mret != CURLM_OK)
1898 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1899 _("Connection: %X: %s failed at %s:%d: `%s'\n"),
1901 "curl_multi_add_handle", __FILE__, __LINE__,
1902 curl_multi_strerror (mret));
1903 return GNUNET_SYSERR;
1907 if (plugin->http_curl_task != GNUNET_SCHEDULER_NO_TASK)
1909 GNUNET_SCHEDULER_cancel(plugin->env->sched, plugin->http_curl_task);
1910 plugin->http_curl_task = GNUNET_SCHEDULER_NO_TASK;
1912 plugin->http_curl_task = GNUNET_SCHEDULER_add_now (plugin->env->sched, &curl_perform, plugin);
1915 if (ps->direction == INBOUND)
1917 GNUNET_assert (NULL != ps->pending_msgs_tail);
1918 if ((ps->recv_connected==GNUNET_YES) && (ps->send_connected==GNUNET_YES) &&
1919 (ps->recv_force_disconnect==GNUNET_NO) && (ps->recv_force_disconnect==GNUNET_NO))
1922 return GNUNET_SYSERR;
1926 * select best session to transmit data to peer
1928 * @param cls closure
1929 * @param pc peer context of target peer
1930 * @param addr address of target peer
1931 * @param addrlen address length
1932 * @param force_address does transport service enforce address?
1933 * @param session session passed by transport service
1934 * @return selected session
1937 static struct Session * send_select_session (struct HTTP_PeerContext *pc, const void * addr, size_t addrlen, int force_address, struct Session * session)
1939 struct Session * tmp = NULL;
1940 int addr_given = GNUNET_NO;
1942 if ((addr!=NULL) && (addrlen>0))
1943 addr_given = GNUNET_YES;
1945 if (force_address == GNUNET_YES)
1947 /* check session given as argument */
1948 if ((session != NULL) && (addr_given == GNUNET_YES))
1950 if (0 == memcmp(session->addr, addr, addrlen))
1952 /* connection can not be used, since it is disconnected */
1953 if ((session->recv_force_disconnect==GNUNET_NO) && (session->send_force_disconnect==GNUNET_NO))
1955 #if DEBUG_SESSION_SELECTION
1956 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Session %X selected: Using session passed by transport to send to forced address \n", session);
1962 /* check last session used */
1963 if ((pc->last_session != NULL)&& (addr_given == GNUNET_YES))
1965 if (0 == memcmp(pc->last_session->addr, addr, addrlen))
1967 /* connection can not be used, since it is disconnected */
1968 if ((pc->last_session->recv_force_disconnect==GNUNET_NO) && (pc->last_session->send_force_disconnect==GNUNET_NO))
1970 #if DEBUG_SESSION_SELECTION
1971 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Session %X selected: Using last session used to send to forced address \n", pc->last_session);
1973 return pc->last_session;
1977 /* find session in existing sessions */
1979 while ((tmp!=NULL) && (addr_given == GNUNET_YES))
1982 if (0 == memcmp(tmp->addr, addr, addrlen))
1984 /* connection can not be used, since it is disconnected */
1985 if ((tmp->recv_force_disconnect==GNUNET_NO) && (tmp->send_force_disconnect==GNUNET_NO))
1987 #if DEBUG_SESSION_SELECTION
1988 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Session %X selected: Using existing session to send to forced address \n", session);
1996 /* no session to use */
1999 if ((force_address == GNUNET_NO) || (force_address == GNUNET_SYSERR))
2001 /* check session given as argument */
2002 if (session != NULL)
2004 /* connection can not be used, since it is disconnected */
2005 if ((session->recv_force_disconnect==GNUNET_NO) && (session->send_force_disconnect==GNUNET_NO))
2007 #if DEBUG_SESSION_SELECTION
2008 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Session %X selected: Using session passed by transport to send not-forced address \n", session);
2014 /* check last session used */
2015 if (pc->last_session != NULL)
2017 /* connection can not be used, since it is disconnected */
2018 if ((pc->last_session->recv_force_disconnect==GNUNET_NO) && (pc->last_session->send_force_disconnect==GNUNET_NO))
2020 #if DEBUG_SESSION_SELECTION
2021 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Session %X selected: Using last session to send to not-forced address \n", pc->last_session);
2023 return pc->last_session;
2026 /* find session in existing sessions */
2030 /* connection can not be used, since it is disconnected */
2031 if ((tmp->recv_force_disconnect==GNUNET_NO) && (tmp->send_force_disconnect==GNUNET_NO))
2033 #if DEBUG_SESSION_SELECTION
2034 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Session %X selected: Using existing session to send to not-forced address \n", tmp);
2046 * Function that can be used by the transport service to transmit
2047 * a message using the plugin. Note that in the case of a
2048 * peer disconnecting, the continuation MUST be called
2049 * prior to the disconnect notification itself. This function
2050 * will be called with this peer's HELLO message to initiate
2051 * a fresh connection to another peer.
2053 * @param cls closure
2054 * @param target who should receive this message
2055 * @param msgbuf the message to transmit
2056 * @param msgbuf_size number of bytes in 'msgbuf'
2057 * @param priority how important is the message (most plugins will
2058 * ignore message priority and just FIFO)
2059 * @param to how long to wait at most for the transmission (does not
2060 * require plugins to discard the message after the timeout,
2061 * just advisory for the desired delay; most plugins will ignore
2063 * @param session which session must be used (or NULL for "any")
2064 * @param addr the address to use (can be NULL if the plugin
2065 * is "on its own" (i.e. re-use existing TCP connection))
2066 * @param addrlen length of the address in bytes
2067 * @param force_address GNUNET_YES if the plugin MUST use the given address,
2068 * GNUNET_NO means the plugin may use any other address and
2069 * GNUNET_SYSERR means that only reliable existing
2070 * bi-directional connections should be used (regardless
2072 * @param cont continuation to call once the message has
2073 * been transmitted (or if the transport is ready
2074 * for the next transmission call; or if the
2075 * peer disconnected...); can be NULL
2076 * @param cont_cls closure for cont
2077 * @return number of bytes used (on the physical network, with overheads);
2078 * -1 on hard errors (i.e. address invalid); 0 is a legal value
2079 * and does NOT mean that the message was not transmitted (DV)
2082 http_plugin_send (void *cls,
2083 const struct GNUNET_PeerIdentity *target,
2086 unsigned int priority,
2087 struct GNUNET_TIME_Relative to,
2088 struct Session *session,
2092 GNUNET_TRANSPORT_TransmitContinuation cont,
2095 struct Plugin *plugin = cls;
2096 struct HTTP_Message *msg;
2097 struct HTTP_PeerContext * pc;
2098 struct Session * ps = NULL;
2100 GNUNET_assert(cls !=NULL);
2104 if (force_address == GNUNET_YES)
2105 GNUNET_asprintf(&force, "forced addr.");
2106 if (force_address == GNUNET_NO)
2107 GNUNET_asprintf(&force, "any addr.");
2108 if (force_address == GNUNET_SYSERR)
2109 GNUNET_asprintf(&force,"reliable bi-direc. address addr.");
2111 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Transport tells me to send %u bytes to `%s' using %s (%s) and session: %X\n",
2115 http_plugin_address_to_string(NULL, addr, addrlen),
2121 pc = GNUNET_CONTAINER_multihashmap_get (plugin->peers, &target->hashPubKey);
2125 pc = GNUNET_malloc(sizeof (struct HTTP_PeerContext));
2126 pc->plugin = plugin;
2127 pc->session_id_counter=1;
2128 pc->last_session = NULL;
2129 memcpy(&pc->identity, target, sizeof(struct GNUNET_PeerIdentity));
2130 GNUNET_CONTAINER_multihashmap_put(plugin->peers, &pc->identity.hashPubKey, pc, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
2131 GNUNET_STATISTICS_update (plugin->env->stats,
2132 gettext_noop ("# HTTP peers active"),
2137 ps = send_select_session (pc, addr, addrlen, force_address, session);
2139 /* session not existing, but address forced -> creating new session */
2142 if ((addr!=NULL) && (addrlen!=0))
2144 ps = GNUNET_malloc(sizeof (struct Session));
2145 #if DEBUG_SESSION_SELECTION
2146 if (force_address == GNUNET_YES)
2147 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"No existing connection & forced address: creating new session %X to peer %s\n", ps, GNUNET_i2s(target));
2148 if (force_address != GNUNET_YES)
2149 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"No existing connection: creating new session %X to peer %s\n", ps, GNUNET_i2s(target));
2151 if ((addrlen!=0) && (addr!=NULL))
2153 ps->addr = GNUNET_malloc(addrlen);
2154 memcpy(ps->addr,addr,addrlen);
2155 ps->addrlen = addrlen;
2162 ps->direction=OUTBOUND;
2163 ps->recv_connected = GNUNET_NO;
2164 ps->recv_force_disconnect = GNUNET_NO;
2165 ps->send_connected = GNUNET_NO;
2166 ps->send_force_disconnect = GNUNET_NO;
2167 ps->pending_msgs_head = NULL;
2168 ps->pending_msgs_tail = NULL;
2170 ps->session_id = pc->session_id_counter;
2171 pc->session_id_counter++;
2172 ps->url = create_url (plugin, ps->addr, ps->addrlen, ps->session_id);
2173 if (ps->msgtok == NULL)
2174 ps->msgtok = GNUNET_SERVER_mst_create (&curl_receive_mst_cb, ps);
2175 GNUNET_CONTAINER_DLL_insert(pc->head,pc->tail,ps);
2176 GNUNET_STATISTICS_update (plugin->env->stats,
2177 gettext_noop ("# HTTP outbound sessions for peers active"),
2184 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));
2186 return GNUNET_SYSERR;
2191 msg = GNUNET_malloc (sizeof (struct HTTP_Message) + msgbuf_size);
2193 msg->size = msgbuf_size;
2195 msg->buf = (char *) &msg[1];
2196 msg->transmit_cont = cont;
2197 msg->transmit_cont_cls = cont_cls;
2198 memcpy (msg->buf,msgbuf, msgbuf_size);
2199 GNUNET_CONTAINER_DLL_insert(ps->pending_msgs_head,ps->pending_msgs_tail,msg);
2201 if (send_check_connections (plugin, ps) == GNUNET_SYSERR)
2202 return GNUNET_SYSERR;
2203 if (force_address != GNUNET_YES)
2204 pc->last_session = ps;
2206 if (pc->last_session==NULL)
2207 pc->last_session = ps;
2214 * Function that can be used to force the plugin to disconnect
2215 * from the given peer and cancel all previous transmissions
2216 * (and their continuationc).
2218 * @param cls closure
2219 * @param target peer from which to disconnect
2222 http_plugin_disconnect (void *cls,
2223 const struct GNUNET_PeerIdentity *target)
2227 struct Plugin *plugin = cls;
2228 struct HTTP_PeerContext *pc = NULL;
2229 struct Session *ps = NULL;
2230 //struct Session *tmp = NULL;
2232 pc = GNUNET_CONTAINER_multihashmap_get (plugin->peers, &target->hashPubKey);
2239 /* Telling transport that session is getting disconnected */
2240 plugin->env->session_end(plugin, target, ps);
2241 if (ps->direction==OUTBOUND)
2243 if (ps->send_endpoint!=NULL)
2245 //GNUNET_assert(CURLM_OK == curl_multi_remove_handle(plugin->multi_handle,ps->send_endpoint));
2246 //curl_easy_cleanup(ps->send_endpoint);
2247 //ps->send_endpoint=NULL;
2248 ps->send_force_disconnect = GNUNET_YES;
2250 if (ps->recv_endpoint!=NULL)
2252 //GNUNET_assert(CURLM_OK == curl_multi_remove_handle(plugin->multi_handle,ps->recv_endpoint));
2253 //curl_easy_cleanup(ps->recv_endpoint);
2254 //ps->recv_endpoint=NULL;
2255 ps->recv_force_disconnect = GNUNET_YES;
2259 if (ps->direction==INBOUND)
2261 ps->recv_force_disconnect = GNUNET_YES;
2262 ps->send_force_disconnect = GNUNET_YES;
2265 while (ps->pending_msgs_head!=NULL)
2267 remove_http_message(ps, ps->pending_msgs_head);
2269 ps->recv_active = GNUNET_NO;
2270 ps->send_active = GNUNET_NO;
2277 * Convert the transports address to a nice, human-readable
2280 * @param cls closure
2281 * @param type name of the transport that generated the address
2282 * @param addr one of the addresses of the host, NULL for the last address
2283 * the specific address format depends on the transport
2284 * @param addrlen length of the address
2285 * @param numeric should (IP) addresses be displayed in numeric form?
2286 * @param timeout after how long should we give up?
2287 * @param asc function to call on each string
2288 * @param asc_cls closure for asc
2291 http_plugin_address_pretty_printer (void *cls,
2296 struct GNUNET_TIME_Relative timeout,
2297 GNUNET_TRANSPORT_AddressStringCallback
2300 const struct IPv4HttpAddress *t4;
2301 const struct IPv6HttpAddress *t6;
2302 struct sockaddr_in a4;
2303 struct sockaddr_in6 a6;
2309 GNUNET_assert(cls !=NULL);
2310 if (addrlen == sizeof (struct IPv6HttpAddress))
2312 address = GNUNET_malloc (INET6_ADDRSTRLEN);
2314 a6.sin6_addr = t6->ipv6_addr;
2315 inet_ntop(AF_INET6, &(a6.sin6_addr),address,INET6_ADDRSTRLEN);
2316 port = ntohs(t6->u6_port);
2318 else if (addrlen == sizeof (struct IPv4HttpAddress))
2320 address = GNUNET_malloc (INET_ADDRSTRLEN);
2322 a4.sin_addr.s_addr = t4->ipv4_addr;
2323 inet_ntop(AF_INET, &(a4.sin_addr),address,INET_ADDRSTRLEN);
2324 port = ntohs(t4->u_port);
2328 /* invalid address */
2329 GNUNET_break_op (0);
2330 asc (asc_cls, NULL);
2333 res = GNUNET_asprintf(&ret,"%s://%s:%u/", PROTOCOL_PREFIX, address, port);
2334 GNUNET_free (address);
2335 GNUNET_assert(res != 0);
2337 GNUNET_free_non_null (ret);
2343 * Another peer has suggested an address for this
2344 * peer and transport plugin. Check that this could be a valid
2345 * address. If so, consider adding it to the list
2348 * @param cls closure
2349 * @param addr pointer to the address
2350 * @param addrlen length of addr
2351 * @return GNUNET_OK if this is a plausible address for this peer
2355 http_plugin_address_suggested (void *cls,
2356 const void *addr, size_t addrlen)
2358 struct Plugin *plugin = cls;
2359 struct IPv4HttpAddress *v4;
2360 struct IPv6HttpAddress *v6;
2363 GNUNET_assert(cls !=NULL);
2364 if ((addrlen != sizeof (struct IPv4HttpAddress)) &&
2365 (addrlen != sizeof (struct IPv6HttpAddress)))
2367 return GNUNET_SYSERR;
2369 if (addrlen == sizeof (struct IPv4HttpAddress))
2371 v4 = (struct IPv4HttpAddress *) addr;
2372 /* Not skipping loopback
2373 if (INADDR_LOOPBACK == ntohl(v4->ipv4_addr))
2375 return GNUNET_SYSERR;
2377 port = ntohs (v4->u_port);
2378 if (port != plugin->port_inbound)
2380 return GNUNET_SYSERR;
2383 if (addrlen == sizeof (struct IPv6HttpAddress))
2385 v6 = (struct IPv6HttpAddress *) addr;
2386 if (IN6_IS_ADDR_LINKLOCAL (&v6->ipv6_addr))
2388 return GNUNET_SYSERR;
2390 port = ntohs (v6->u6_port);
2391 if (port != plugin->port_inbound)
2393 return GNUNET_SYSERR;
2402 * Function called for a quick conversion of the binary address to
2403 * a numeric address. Note that the caller must not free the
2404 * address and that the next call to this function is allowed
2405 * to override the address again.
2407 * @param cls closure
2408 * @param addr binary address
2409 * @param addrlen length of the address
2410 * @return string representing the same address
2413 http_plugin_address_to_string (void *cls,
2417 const struct IPv4HttpAddress *t4;
2418 const struct IPv6HttpAddress *t6;
2419 struct sockaddr_in a4;
2420 struct sockaddr_in6 a6;
2426 if (addrlen == sizeof (struct IPv6HttpAddress))
2428 address = GNUNET_malloc (INET6_ADDRSTRLEN);
2430 a6.sin6_addr = t6->ipv6_addr;
2431 inet_ntop(AF_INET6, &(a6.sin6_addr),address,INET6_ADDRSTRLEN);
2432 port = ntohs(t6->u6_port);
2434 else if (addrlen == sizeof (struct IPv4HttpAddress))
2436 address = GNUNET_malloc (INET_ADDRSTRLEN);
2438 a4.sin_addr.s_addr = t4->ipv4_addr;
2439 inet_ntop(AF_INET, &(a4.sin_addr),address,INET_ADDRSTRLEN);
2440 port = ntohs(t4->u_port);
2444 /* invalid address */
2447 res = GNUNET_asprintf(&ret,"%s:%u",address,port);
2448 GNUNET_free (address);
2449 GNUNET_assert(res != 0);
2455 * Exit point from the plugin.
2458 LIBGNUNET_PLUGIN_TRANSPORT_DONE (void *cls)
2460 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
2461 struct Plugin *plugin = api->cls;
2463 GNUNET_assert(cls !=NULL);
2465 if (plugin->http_server_daemon_v4 != NULL)
2467 MHD_stop_daemon (plugin->http_server_daemon_v4);
2468 plugin->http_server_daemon_v4 = NULL;
2470 if (plugin->http_server_daemon_v6 != NULL)
2472 MHD_stop_daemon (plugin->http_server_daemon_v6);
2473 plugin->http_server_daemon_v6 = NULL;
2476 if ( plugin->http_server_task_v4 != GNUNET_SCHEDULER_NO_TASK)
2478 GNUNET_SCHEDULER_cancel(plugin->env->sched, plugin->http_server_task_v4);
2479 plugin->http_server_task_v4 = GNUNET_SCHEDULER_NO_TASK;
2482 if ( plugin->http_server_task_v6 != GNUNET_SCHEDULER_NO_TASK)
2484 GNUNET_SCHEDULER_cancel(plugin->env->sched, plugin->http_server_task_v6);
2485 plugin->http_server_task_v6 = GNUNET_SCHEDULER_NO_TASK;
2488 /* free all peer information */
2489 if (plugin->peers!=NULL)
2491 GNUNET_CONTAINER_multihashmap_iterate (plugin->peers,
2492 &remove_peer_context_Iterator,
2494 GNUNET_CONTAINER_multihashmap_destroy (plugin->peers);
2496 if (plugin->multi_handle!=NULL)
2498 mret = curl_multi_cleanup(plugin->multi_handle);
2500 if ( CURLM_OK != mret)
2501 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"curl multihandle clean up failed\n");
2503 plugin->multi_handle = NULL;
2505 curl_global_cleanup();
2507 if ( plugin->http_curl_task != GNUNET_SCHEDULER_NO_TASK)
2509 GNUNET_SCHEDULER_cancel(plugin->env->sched, plugin->http_curl_task);
2510 plugin->http_curl_task = GNUNET_SCHEDULER_NO_TASK;
2513 GNUNET_free_non_null (plugin->bind4_address);
2514 GNUNET_free_non_null (plugin->bind6_address);
2515 GNUNET_free_non_null(plugin->bind_hostname);
2517 GNUNET_free_non_null (plugin->crypto_init);
2518 GNUNET_free_non_null (plugin->cert);
2519 GNUNET_free_non_null (plugin->key);
2521 GNUNET_free (plugin);
2524 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Unload %s plugin complete...\n", PROTOCOL_PREFIX);
2531 load_certificate( const char * file )
2533 struct GNUNET_DISK_FileHandle * gn_file;
2538 if (0!=STAT(file, &fstat))
2540 text = GNUNET_malloc (fstat.st_size+1);
2541 gn_file = GNUNET_DISK_file_open(file,GNUNET_DISK_OPEN_READ, GNUNET_DISK_PERM_USER_READ);
2547 if (GNUNET_SYSERR == GNUNET_DISK_file_read(gn_file, text, fstat.st_size))
2550 GNUNET_DISK_file_close(gn_file);
2553 text[fstat.st_size] = '\0';
2554 GNUNET_DISK_file_close(gn_file);
2562 * Entry point for the plugin.
2565 LIBGNUNET_PLUGIN_TRANSPORT_INIT (void *cls)
2567 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
2568 struct Plugin *plugin;
2569 struct GNUNET_TRANSPORT_PluginFunctions *api;
2570 struct GNUNET_TIME_Relative gn_timeout;
2571 long long unsigned int port;
2572 char * component_name;
2574 char * key_file = NULL;
2575 char * cert_file = NULL;
2578 GNUNET_assert(cls !=NULL);
2580 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Starting %s plugin...\n", PROTOCOL_PREFIX);
2582 GNUNET_asprintf(&component_name,"transport-%s",PROTOCOL_PREFIX);
2584 plugin = GNUNET_malloc (sizeof (struct Plugin));
2585 plugin->stats = env->stats;
2587 plugin->peers = NULL;
2588 plugin->bind4_address = NULL;
2589 plugin->use_ipv6 = GNUNET_YES;
2590 plugin->use_ipv4 = GNUNET_YES;
2592 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
2594 api->send = &http_plugin_send;
2595 api->disconnect = &http_plugin_disconnect;
2596 api->address_pretty_printer = &http_plugin_address_pretty_printer;
2597 api->check_address = &http_plugin_address_suggested;
2598 api->address_to_string = &http_plugin_address_to_string;
2600 /* Hashing our identity to use it in URLs */
2601 GNUNET_CRYPTO_hash_to_enc ( &(plugin->env->my_identity->hashPubKey), &plugin->my_ascii_hash_ident);
2604 if (GNUNET_CONFIGURATION_have_value (env->cfg,
2605 component_name, "USE_IPv6"))
2607 plugin->use_ipv6 = GNUNET_CONFIGURATION_get_value_yesno (env->cfg,
2612 if (GNUNET_CONFIGURATION_have_value (env->cfg,
2613 component_name, "USE_IPv4"))
2615 plugin->use_ipv4 = GNUNET_CONFIGURATION_get_value_yesno (env->cfg,
2616 component_name,"USE_IPv4");
2618 /* Reading port number from config file */
2620 GNUNET_CONFIGURATION_get_value_number (env->cfg,
2626 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
2628 _("Require valid port number for transport plugin `%s' in configuration!\n"),
2630 GNUNET_free(component_name);
2631 LIBGNUNET_PLUGIN_TRANSPORT_DONE (api);
2635 /* Reading ipv4 addresse to bind to from config file */
2636 if ((plugin->use_ipv4==GNUNET_YES) && (GNUNET_CONFIGURATION_have_value (env->cfg,
2637 component_name, "BINDTO4")))
2639 GNUNET_break (GNUNET_OK ==
2640 GNUNET_CONFIGURATION_get_value_string (env->cfg,
2643 &plugin->bind_hostname));
2644 plugin->bind4_address = GNUNET_malloc(sizeof(struct sockaddr_in));
2645 plugin->bind4_address->sin_family = AF_INET;
2646 plugin->bind4_address->sin_port = htons (port);
2648 if (plugin->bind_hostname!=NULL)
2650 if (inet_pton(AF_INET,plugin->bind_hostname, &plugin->bind4_address->sin_addr)<=0)
2652 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
2654 _("Misconfigured address to bind to in configuration!\n"));
2655 GNUNET_free(plugin->bind4_address);
2656 GNUNET_free(plugin->bind_hostname);
2657 plugin->bind_hostname = NULL;
2658 plugin->bind4_address = NULL;
2663 /* Reading ipv4 addresse to bind to from config file */
2664 if ((plugin->use_ipv6==GNUNET_YES) && (GNUNET_CONFIGURATION_have_value (env->cfg,
2665 component_name, "BINDTO6")))
2667 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (env->cfg,
2670 &plugin->bind_hostname))
2672 plugin->bind6_address = GNUNET_malloc(sizeof(struct sockaddr_in6));
2673 plugin->bind6_address->sin6_family = AF_INET6;
2674 plugin->bind6_address->sin6_port = htons (port);
2675 if (plugin->bind_hostname!=NULL)
2677 if (inet_pton(AF_INET6,plugin->bind_hostname, &plugin->bind6_address->sin6_addr)<=0)
2679 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
2681 _("Misconfigured address to bind to in configuration!\n"));
2682 GNUNET_free(plugin->bind6_address);
2683 GNUNET_free(plugin->bind_hostname);
2684 plugin->bind_hostname = NULL;
2685 plugin->bind6_address = NULL;
2692 /* Reading HTTPS crypto related configuration */
2693 /* Get crypto init string from config */
2694 if (GNUNET_CONFIGURATION_have_value (env->cfg,
2695 "transport-https", "CRYPTO_INIT"))
2697 GNUNET_CONFIGURATION_get_value_string (env->cfg,
2700 &plugin->crypto_init);
2704 GNUNET_asprintf(&plugin->crypto_init,"NORMAL");
2707 /* Get private key file from config */
2708 if (GNUNET_CONFIGURATION_have_value (env->cfg,
2709 "transport-https", "KEY_FILE"))
2711 GNUNET_CONFIGURATION_get_value_string (env->cfg,
2717 GNUNET_asprintf(&key_file,"https.key");
2719 /* Get private key file from config */
2720 if (GNUNET_CONFIGURATION_have_value (env->cfg,"transport-https", "CERT_FILE"))
2722 GNUNET_CONFIGURATION_get_value_string (env->cfg,
2727 if (cert_file==NULL)
2728 GNUNET_asprintf(&cert_file,"https.cert");
2730 /* read key & certificates from file */
2731 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Loading TLS certificate `%s' `%s'\n", key_file, cert_file);
2733 plugin->key = load_certificate( key_file );
2734 plugin->cert = load_certificate( cert_file );
2736 if ((plugin->key==NULL) || (plugin->cert==NULL))
2740 GNUNET_asprintf(&cmd,"gnunet-transport-certificate-creation %s %s", key_file, cert_file);
2741 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "No usable TLS certificate found, creating certificate \n");
2746 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
2748 _("Could not create a new TLS certificate, shell script `%s' failed!\n"),cmd,
2750 GNUNET_free (key_file);
2751 GNUNET_free (cert_file);
2752 GNUNET_free (component_name);
2754 LIBGNUNET_PLUGIN_TRANSPORT_DONE(api);
2761 plugin->key = load_certificate( key_file );
2762 plugin->cert = load_certificate( cert_file );
2764 if ((plugin->key==NULL) || (plugin->cert==NULL))
2766 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
2768 _("No usable TLS certificate found and creating one failed! \n"),
2770 GNUNET_free (key_file);
2771 GNUNET_free (cert_file);
2772 GNUNET_free (component_name);
2774 LIBGNUNET_PLUGIN_TRANSPORT_DONE(api);
2778 GNUNET_free (key_file);
2779 GNUNET_free (cert_file);
2781 GNUNET_assert((plugin->key!=NULL) && (plugin->cert!=NULL));
2782 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "TLS certificate loaded\n");
2785 GNUNET_assert ((port > 0) && (port <= 65535));
2786 plugin->port_inbound = port;
2787 gn_timeout = GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT;
2788 unsigned int timeout = (gn_timeout.value) / 1000;
2789 if ((plugin->http_server_daemon_v6 == NULL) && (plugin->use_ipv6 == GNUNET_YES) && (port != 0))
2791 struct sockaddr * tmp = (struct sockaddr *) plugin->bind6_address;
2792 plugin->http_server_daemon_v6 = MHD_start_daemon (
2802 plugin , &mdh_access_cb, plugin,
2803 MHD_OPTION_SOCK_ADDR, tmp,
2804 MHD_OPTION_CONNECTION_LIMIT, (unsigned int) 32,
2805 //MHD_OPTION_PER_IP_CONNECTION_LIMIT, (unsigned int) 6,
2807 MHD_OPTION_HTTPS_PRIORITIES, plugin->crypto_init,
2808 MHD_OPTION_HTTPS_MEM_KEY, plugin->key,
2809 MHD_OPTION_HTTPS_MEM_CERT, plugin->cert,
2811 MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) timeout,
2812 MHD_OPTION_CONNECTION_MEMORY_LIMIT, (size_t) (2 * GNUNET_SERVER_MAX_MESSAGE_SIZE),
2813 MHD_OPTION_NOTIFY_COMPLETED, &mhd_termination_cb, NULL,
2814 MHD_OPTION_EXTERNAL_LOGGER, mhd_logger, plugin->mhd_log,
2817 if ((plugin->http_server_daemon_v4 == NULL) && (plugin->use_ipv4 == GNUNET_YES) && (port != 0))
2819 plugin->http_server_daemon_v4 = MHD_start_daemon (
2829 plugin , &mdh_access_cb, plugin,
2830 MHD_OPTION_SOCK_ADDR, (struct sockaddr_in *)plugin->bind4_address,
2831 MHD_OPTION_CONNECTION_LIMIT, (unsigned int) 32,
2832 //MHD_OPTION_PER_IP_CONNECTION_LIMIT, (unsigned int) 6,
2834 MHD_OPTION_HTTPS_PRIORITIES, plugin->crypto_init,
2835 MHD_OPTION_HTTPS_MEM_KEY, plugin->key,
2836 MHD_OPTION_HTTPS_MEM_CERT, plugin->cert,
2838 MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) timeout,
2839 MHD_OPTION_CONNECTION_MEMORY_LIMIT, (size_t) (2 * GNUNET_SERVER_MAX_MESSAGE_SIZE),
2840 MHD_OPTION_NOTIFY_COMPLETED, &mhd_termination_cb, NULL,
2841 MHD_OPTION_EXTERNAL_LOGGER, mhd_logger, plugin->mhd_log,
2844 if (plugin->http_server_daemon_v4 != NULL)
2845 plugin->http_server_task_v4 = http_server_daemon_prepare (plugin, plugin->http_server_daemon_v4);
2846 if (plugin->http_server_daemon_v6 != NULL)
2847 plugin->http_server_task_v6 = http_server_daemon_prepare (plugin, plugin->http_server_daemon_v6);
2850 if (plugin->http_server_task_v4 != GNUNET_SCHEDULER_NO_TASK)
2853 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Starting MHD with IPv4 bound to %s with port %u\n",(plugin->bind_hostname!=NULL) ? plugin->bind_hostname : "every address",port);
2856 else if ((plugin->http_server_task_v6 != GNUNET_SCHEDULER_NO_TASK) && (plugin->http_server_task_v4 != GNUNET_SCHEDULER_NO_TASK))
2859 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Starting MHD with IPv6 bound to %s with port %u\n",(plugin->bind_hostname!=NULL) ? plugin->bind_hostname : "every address", port);
2862 else if ((plugin->http_server_task_v6 != GNUNET_SCHEDULER_NO_TASK) && (plugin->http_server_task_v4 == GNUNET_SCHEDULER_NO_TASK))
2865 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Starting MHD with IPv4 and IPv6 bound to %s with port %u\n",(plugin->bind_hostname!=NULL) ? plugin->bind_hostname : "every address", port);
2871 if ((plugin->use_ipv6 == GNUNET_YES) && (plugin->use_ipv4 == GNUNET_YES))
2872 GNUNET_asprintf(&tmp,"with IPv4 and IPv6 enabled");
2873 if ((plugin->use_ipv6 == GNUNET_NO) && (plugin->use_ipv4 == GNUNET_YES))
2874 GNUNET_asprintf(&tmp,"with IPv4 enabled");
2875 if ((plugin->use_ipv6 == GNUNET_YES) && (plugin->use_ipv4 == GNUNET_NO))
2876 GNUNET_asprintf(&tmp,"with IPv6 enabled");
2877 if ((plugin->use_ipv6 == GNUNET_NO) && (plugin->use_ipv4 == GNUNET_NO))
2878 GNUNET_asprintf(&tmp,"with NO IP PROTOCOL enabled");
2879 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,"HTTP Server with %s could not be started on port %u! %s plugin failed!\n",tmp, port, PROTOCOL_PREFIX);
2881 GNUNET_free (component_name);
2882 LIBGNUNET_PLUGIN_TRANSPORT_DONE (api);
2886 /* Initializing cURL */
2887 curl_global_init(CURL_GLOBAL_ALL);
2888 plugin->multi_handle = curl_multi_init();
2890 if ( NULL == plugin->multi_handle )
2892 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
2894 _("Could not initialize curl multi handle, failed to start %s plugin!\n"),
2896 GNUNET_free(component_name);
2897 LIBGNUNET_PLUGIN_TRANSPORT_DONE (api);
2901 plugin->peers = GNUNET_CONTAINER_multihashmap_create (10);
2902 GNUNET_OS_network_interfaces_list (&process_interfaces, plugin);
2904 GNUNET_free(component_name);
2908 /* end of plugin_transport_http.c */