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>
43 #define DEBUG_HTTP GNUNET_NO
44 #define DEBUG_CURL GNUNET_NO
45 #define DEBUG_CONNECTIONS GNUNET_NO
46 #define DEBUG_SESSION_SELECTION GNUNET_NO
48 #define INBOUND GNUNET_NO
49 #define OUTBOUND GNUNET_YES
51 #define PROTOCOL_PREFIX "http"
54 * Text of the response sent back after the last bytes of a PUT
55 * request have been received (just to formally obey the HTTP
58 #define HTTP_PUT_RESPONSE "Thank you!"
61 * After how long do we expire an address that we
62 * learned from another peer if it is not reconfirmed
65 #define LEARNED_ADDRESS_EXPIRATION GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_HOURS, 6)
68 * Page returned if request invalid
70 #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>"
73 * Timeout for a http connect
75 #define HTTP_CONNECT_TIMEOUT 30
79 * Network format for IPv4 addresses.
81 struct IPv4HttpAddress
84 * IPv4 address, in network byte order.
86 uint32_t ipv4_addr GNUNET_PACKED;
89 * Port number, in network byte order.
91 uint16_t u_port GNUNET_PACKED;
97 * Network format for IPv6 addresses.
99 struct IPv6HttpAddress
104 struct in6_addr ipv6_addr GNUNET_PACKED;
107 * Port number, in network byte order.
109 uint16_t u6_port GNUNET_PACKED;
115 * Message to send using http
120 * next pointer for double linked list
122 struct HTTP_Message * next;
125 * previous pointer for double linked list
127 struct HTTP_Message * prev;
130 * buffer containing data to send
135 * amount of data already sent
145 * Continuation function to call once the transmission buffer
146 * has again space available. NULL if there is no
147 * continuation to call.
149 GNUNET_TRANSPORT_TransmitContinuation transmit_cont;
152 * Closure for transmit_cont.
154 void *transmit_cont_cls;
158 struct HTTP_PeerContext
163 struct GNUNET_PeerIdentity identity;
166 * Pointer to the global plugin struct.
168 struct Plugin *plugin;
171 * Linked list of connections with this peer
174 struct Session * head;
177 * Linked list of connections with this peer
180 struct Session * tail;
183 * id for next session
185 size_t session_id_counter;
188 * Last session used to send data
190 struct Session * last_session;
199 struct SessionHeader header;
202 * next session in linked list
204 struct Session * next;
207 * previous session in linked list
209 struct Session * prev;
212 * address of this session
227 * Message queue for outbound messages
230 struct HTTP_Message * pending_msgs_head;
233 * Message queue for outbound messages
236 struct HTTP_Message * pending_msgs_tail;
239 * partner peer this connection belongs to
241 struct HTTP_PeerContext * peercontext;
244 * message stream tokenizer for incoming data
246 struct GNUNET_SERVER_MessageStreamTokenizer *msgtok;
250 * outbound: OUTBOUND (GNUNET_YES)
251 * inbound : INBOUND (GNUNET_NO)
253 unsigned int direction;
256 * is session connected to send data?
258 unsigned int send_connected;
261 * is send connection active?
263 unsigned int send_active;
266 * connection disconnect forced (e.g. from transport)
268 unsigned int send_force_disconnect;
271 * is session connected to receive data?
273 unsigned int recv_connected;
276 * is receive connection active?
278 unsigned int recv_active;
281 * connection disconnect forced (e.g. from transport)
283 unsigned int recv_force_disconnect;
286 * id for next session
287 * NOTE: 0 is not an ID, zero is not defined. A correct ID is always > 0
292 * entity managing sending data
293 * outbound session: CURL *
294 * inbound session: mhd_connection *
296 void * send_endpoint;
299 * entity managing recieving data
300 * outbound session: CURL *
301 * inbound session: mhd_connection *
303 void * recv_endpoint;
307 * Encapsulation of all of the state of the plugin.
314 struct GNUNET_TRANSPORT_PluginEnvironment *env;
317 * Handle for reporting statistics.
319 struct GNUNET_STATISTICS_Handle *stats;
321 unsigned int port_inbound;
323 struct GNUNET_CONTAINER_MultiHashMap *peers;
326 * Daemon for listening for new IPv4 connections.
328 struct MHD_Daemon *http_server_daemon_v4;
331 * Daemon for listening for new IPv6connections.
333 struct MHD_Daemon *http_server_daemon_v6;
336 * Our primary task for http daemon handling IPv4 connections
338 GNUNET_SCHEDULER_TaskIdentifier http_server_task_v4;
341 * Our primary task for http daemon handling IPv6 connections
343 GNUNET_SCHEDULER_TaskIdentifier http_server_task_v6;
346 * The task sending data
348 GNUNET_SCHEDULER_TaskIdentifier http_curl_task;
353 CURLM * multi_handle;
356 * Our ASCII encoded, hashed peer identity
357 * This string is used to distinguish between connections and is added to the urls
359 struct GNUNET_CRYPTO_HashAsciiEncoded my_ascii_hash_ident;
361 struct sockaddr_in * bind4_address;
362 struct sockaddr_in6 * bind6_address;
363 char * bind_hostname;
370 * Function called for a quick conversion of the binary address to
371 * a numeric address. Note that the caller must not free the
372 * address and that the next call to this function is allowed
373 * to override the address again.
376 * @param addr binary address
377 * @param addrlen length of the address
378 * @return string representing the same address
381 http_plugin_address_to_string (void *cls,
387 * Call MHD to process pending ipv4 requests and then go back
388 * and schedule the next run.
390 static void http_server_daemon_v4_run (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
392 * Call MHD to process pending ipv6 requests and then go back
393 * and schedule the next run.
395 static void http_server_daemon_v6_run (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
398 * Function setting up curl handle and selecting message to send
400 * @param ses session to send data to
401 * @param con connection
402 * @return bytes sent to peer
404 static ssize_t send_check_connections (void *cls, struct Session *ps);
407 * Function setting up file descriptors and scheduling task to run
409 * @param ses session to send data to
412 static int curl_schedule(void *cls );
416 static char * create_url(void * cls, const void * addr, size_t addrlen, size_t id)
418 struct Plugin *plugin = cls;
421 GNUNET_assert ((addr!=NULL) && (addrlen != 0));
422 GNUNET_asprintf(&url,
423 "%s://%s/%s;%u", PROTOCOL_PREFIX,
424 http_plugin_address_to_string(NULL, addr, addrlen),
425 (char *) (&plugin->my_ascii_hash_ident),id);
431 * Removes a message from the linked list of messages
432 * @param con connection to remove message from
433 * @param msg message to remove
434 * @return GNUNET_SYSERR if msg not found, GNUNET_OK on success
436 static int remove_http_message (struct Session * ps, struct HTTP_Message * msg)
438 GNUNET_CONTAINER_DLL_remove(ps->pending_msgs_head,ps->pending_msgs_tail,msg);
443 int remove_peer_context_Iterator (void *cls, const GNUNET_HashCode *key, void *value);
446 * Removes a session from the linked list of sessions
447 * @param pc peer context
449 * @param call_msg_cont GNUNET_YES to call pending message continuations, otherwise no
450 * @param call_msg_cont_result, result to call message continuations with
451 * @return GNUNET_SYSERR if msg not found, GNUNET_OK on success
453 static int remove_session (struct HTTP_PeerContext * pc, struct Session * ps, int call_msg_cont, int call_msg_cont_result)
455 struct HTTP_Message * msg;
456 struct Plugin * plugin = ps->peercontext->plugin;
458 #if DEBUG_CONNECTIONS
459 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);
461 plugin->env->session_end(plugin, &pc->identity, ps);
463 GNUNET_free_non_null (ps->addr);
464 GNUNET_SERVER_mst_destroy (ps->msgtok);
465 GNUNET_free(ps->url);
467 if (ps->direction==INBOUND)
469 if (ps->recv_endpoint != NULL)
471 curl_easy_cleanup(ps->recv_endpoint);
472 ps->recv_endpoint = NULL;
474 if (ps->send_endpoint != NULL)
476 curl_easy_cleanup(ps->send_endpoint);
477 ps->send_endpoint = NULL;
481 msg = ps->pending_msgs_head;
484 if ((call_msg_cont == GNUNET_YES) && (msg->transmit_cont!=NULL))
486 msg->transmit_cont (msg->transmit_cont_cls,&pc->identity,call_msg_cont_result);
488 GNUNET_CONTAINER_DLL_remove(ps->pending_msgs_head,ps->pending_msgs_head,msg);
490 msg = ps->pending_msgs_head;
493 GNUNET_CONTAINER_DLL_remove(pc->head,pc->tail,ps);
497 /* no sessions left remove peer */
501 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"No sessions left for peer `%s', removing context\n",GNUNET_i2s(&pc->identity));
503 remove_peer_context_Iterator(plugin, &pc->identity.hashPubKey, pc);
509 int remove_peer_context_Iterator (void *cls, const GNUNET_HashCode *key, void *value)
511 struct Plugin *plugin = cls;
512 struct HTTP_PeerContext * pc = value;
513 struct Session * ps = pc->head;
514 struct Session * tmp = NULL;
515 struct HTTP_Message * msg = NULL;
516 struct HTTP_Message * msg_tmp = NULL;
518 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Freeing context for peer `%s'\n",GNUNET_i2s(&pc->identity));
520 GNUNET_CONTAINER_multihashmap_remove (plugin->peers, &pc->identity.hashPubKey, pc);
523 plugin->env->session_end(plugin, &pc->identity, ps);
526 GNUNET_free_non_null (ps->addr);
527 GNUNET_free(ps->url);
528 if (ps->msgtok != NULL)
529 GNUNET_SERVER_mst_destroy (ps->msgtok);
531 msg = ps->pending_msgs_head;
538 if (ps->direction==OUTBOUND)
540 if (ps->send_endpoint!=NULL)
541 curl_easy_cleanup(ps->send_endpoint);
542 if (ps->recv_endpoint!=NULL)
543 curl_easy_cleanup(ps->recv_endpoint);
550 GNUNET_STATISTICS_update (plugin->env->stats,
551 gettext_noop ("# HTTP peers active"),
558 * Add the IP of our network interface to the list of
559 * our external IP addresses.
561 * @param cls the 'struct Plugin*'
562 * @param name name of the interface
563 * @param isDefault do we think this may be our default interface
564 * @param addr address of the interface
565 * @param addrlen number of bytes in addr
566 * @return GNUNET_OK to continue iterating
569 process_interfaces (void *cls,
572 const struct sockaddr *addr, socklen_t addrlen)
574 struct Plugin *plugin = cls;
575 struct IPv4HttpAddress * t4;
576 struct IPv6HttpAddress * t6;
580 GNUNET_assert(cls !=NULL);
581 af = addr->sa_family;
582 if ((af == AF_INET) && (plugin->use_ipv4 == GNUNET_YES) && (plugin->bind6_address == NULL))
584 struct in_addr bnd_cmp = ((struct sockaddr_in *) addr)->sin_addr;
585 t4 = GNUNET_malloc(sizeof(struct IPv4HttpAddress));
586 /* Not skipping loopback addresses
587 if (INADDR_LOOPBACK == ntohl(((struct sockaddr_in *) addr)->sin_addr.s_addr))
593 t4->ipv4_addr = ((struct sockaddr_in *) addr)->sin_addr.s_addr;
594 t4->u_port = htons (plugin->port_inbound);
595 if (plugin->bind4_address != NULL)
597 if (0 == memcmp(&plugin->bind4_address->sin_addr, &bnd_cmp, sizeof (struct in_addr)))
599 plugin->env->notify_address(plugin->env->cls,"http",t4, sizeof (struct IPv4HttpAddress), GNUNET_TIME_UNIT_FOREVER_REL);
604 plugin->env->notify_address(plugin->env->cls,"http",t4, sizeof (struct IPv4HttpAddress), GNUNET_TIME_UNIT_FOREVER_REL);
608 else if ((af == AF_INET6) && (plugin->use_ipv6 == GNUNET_YES) && (plugin->bind4_address == NULL))
610 struct in6_addr bnd_cmp6 = ((struct sockaddr_in6 *) addr)->sin6_addr;
611 if (IN6_IS_ADDR_LINKLOCAL (&((struct sockaddr_in6 *) addr)->sin6_addr))
615 t6 = GNUNET_malloc(sizeof(struct IPv6HttpAddress));
616 GNUNET_assert(t6 != NULL);
617 if (plugin->bind6_address != NULL)
619 if (0 == memcmp(&plugin->bind6_address->sin6_addr, &bnd_cmp6, sizeof (struct in6_addr)))
621 memcpy (&t6->ipv6_addr,
622 &((struct sockaddr_in6 *) addr)->sin6_addr,
623 sizeof (struct in6_addr));
624 t6->u6_port = htons (plugin->port_inbound);
625 plugin->env->notify_address(plugin->env->cls,"http",t6,sizeof (struct IPv6HttpAddress) , GNUNET_TIME_UNIT_FOREVER_REL);
630 memcpy (&t6->ipv6_addr,
631 &((struct sockaddr_in6 *) addr)->sin6_addr,
632 sizeof (struct in6_addr));
633 t6->u6_port = htons (plugin->port_inbound);
634 plugin->env->notify_address(plugin->env->cls,"http",t6,sizeof (struct IPv6HttpAddress) , GNUNET_TIME_UNIT_FOREVER_REL);
643 * Callback called by MHD when a connection is terminated
645 static void mhd_termination_cb (void *cls, struct MHD_Connection * connection, void **httpSessionCache)
647 struct Session * ps = *httpSessionCache;
650 struct HTTP_PeerContext * pc = ps->peercontext;
652 if (connection==ps->recv_endpoint)
654 #if DEBUG_CONNECTIONS
655 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: inbound connection from peer `%s' was terminated\n", ps, GNUNET_i2s(&pc->identity));
657 ps->recv_active = GNUNET_NO;
658 ps->recv_connected = GNUNET_NO;
659 ps->recv_endpoint = NULL;
661 if (connection==ps->send_endpoint)
664 ps->send_active = GNUNET_NO;
665 ps->send_connected = GNUNET_NO;
666 ps->send_endpoint = NULL;
667 #if DEBUG_CONNECTIONS
668 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: outbound connection from peer `%s' was terminated\n", ps, GNUNET_i2s(&pc->identity));
672 /* if both connections disconnected, remove session */
673 if ((ps->send_connected == GNUNET_NO) && (ps->recv_connected == GNUNET_NO))
675 GNUNET_STATISTICS_update (pc->plugin->env->stats,
676 gettext_noop ("# HTTP inbound sessions for peers active"),
679 remove_session(pc,ps,GNUNET_YES,GNUNET_SYSERR);
683 static void mhd_write_mst_cb (void *cls,
685 const struct GNUNET_MessageHeader *message)
688 struct Session *ps = cls;
689 GNUNET_assert(ps != NULL);
691 struct HTTP_PeerContext *pc = ps->peercontext;
692 GNUNET_assert(pc != NULL);
694 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
695 "Connection %X: Forwarding message to transport service, type %u and size %u from `%s' (`%s')\n",
697 ntohs(message->type),
698 ntohs(message->size),
699 GNUNET_i2s(&(ps->peercontext)->identity),http_plugin_address_to_string(NULL,ps->addr,ps->addrlen));
701 pc->plugin->env->receive (ps->peercontext->plugin->env->cls,
709 * Check if ip is allowed to connect.
712 mhd_accept_cb (void *cls,
713 const struct sockaddr *addr, socklen_t addr_len)
716 struct Plugin *plugin = cls;
718 /* Every connection is accepted, nothing more to do here */
722 int mhd_send_callback (void *cls, uint64_t pos, char *buf, int max)
726 struct Session * ps = cls;
727 struct HTTP_PeerContext * pc;
728 struct HTTP_Message * msg;
730 GNUNET_assert (ps!=NULL);
731 pc = ps->peercontext;
732 msg = ps->pending_msgs_tail;
733 if (ps->send_force_disconnect==GNUNET_YES)
735 #if DEBUG_CONNECTIONS
736 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: outbound forced to disconnect\n",ps);
743 if ((msg->size-msg->pos) <= max)
745 memcpy(buf,&msg->buf[msg->pos],(msg->size-msg->pos));
746 bytes_read = msg->size-msg->pos;
747 msg->pos+=(msg->size-msg->pos);
751 memcpy(buf,&msg->buf[msg->pos],max);
756 if (msg->pos==msg->size)
758 if (NULL!=msg->transmit_cont)
759 msg->transmit_cont (msg->transmit_cont_cls,&pc->identity,GNUNET_OK);
760 remove_http_message(ps,msg);
767 * Process GET or PUT request received via MHD. For
768 * GET, queue response that will send back our pending
769 * messages. For PUT, process incoming data and send
770 * to GNUnet core. In either case, check if a session
771 * already exists and create a new one if not.
774 mdh_access_cb (void *cls,
775 struct MHD_Connection *mhd_connection,
779 const char *upload_data,
780 size_t * upload_data_size, void **httpSessionCache)
782 struct Plugin *plugin = cls;
783 struct MHD_Response *response;
784 const union MHD_ConnectionInfo * conn_info;
786 struct sockaddr_in *addrin;
787 struct sockaddr_in6 *addrin6;
789 char address[INET6_ADDRSTRLEN+14];
790 struct GNUNET_PeerIdentity pi_in;
793 struct IPv4HttpAddress ipv4addr;
794 struct IPv6HttpAddress ipv6addr;
796 struct HTTP_PeerContext *pc;
797 struct Session *ps = NULL;
798 struct Session *ps_tmp = NULL;
801 int send_error_to_client;
803 size_t addr_len = 0 ;
805 GNUNET_assert(cls !=NULL);
806 send_error_to_client = GNUNET_NO;
808 if (NULL == *httpSessionCache)
810 /* check url for peer identity , if invalid send HTTP 404*/
811 size_t len = strlen(&url[1]);
812 char * peer = GNUNET_malloc(104+1);
814 if ((len>104) && (url[104]==';'))
816 char * id = GNUNET_malloc((len-104)+1);
817 strcpy(id,&url[105]);
818 memcpy(peer,&url[1],103);
820 id_num = strtoul ( id, NULL , 10);
823 res = GNUNET_CRYPTO_hash_from_string (peer, &(pi_in.hashPubKey));
825 if ( GNUNET_SYSERR == res )
827 response = MHD_create_response_from_data (strlen (HTTP_ERROR_RESPONSE),HTTP_ERROR_RESPONSE, MHD_NO, MHD_NO);
828 res = MHD_queue_response (mhd_connection, MHD_HTTP_NOT_FOUND, response);
829 MHD_destroy_response (response);
830 #if DEBUG_CONNECTIONS
832 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Peer has no valid ident, sent HTTP 1.1/404\n");
834 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Peer has no valid ident, could not send error\n");
841 ps = *httpSessionCache;
842 pc = ps->peercontext;
845 if (NULL == *httpSessionCache)
847 /* get peer context */
848 pc = GNUNET_CONTAINER_multihashmap_get (plugin->peers, &pi_in.hashPubKey);
852 pc = GNUNET_malloc(sizeof (struct HTTP_PeerContext));
854 pc->session_id_counter=1;
855 pc->last_session = NULL;
856 memcpy(&pc->identity, &pi_in, sizeof(struct GNUNET_PeerIdentity));
857 GNUNET_CONTAINER_multihashmap_put(plugin->peers, &pc->identity.hashPubKey, pc, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
858 GNUNET_STATISTICS_update (plugin->env->stats,
859 gettext_noop ("# HTTP peers active"),
864 conn_info = MHD_get_connection_info(mhd_connection, MHD_CONNECTION_INFO_CLIENT_ADDRESS );
865 /* Incoming IPv4 connection */
866 if ( AF_INET == conn_info->client_addr->sin_family)
868 addrin = conn_info->client_addr;
869 inet_ntop(addrin->sin_family, &(addrin->sin_addr),address,INET_ADDRSTRLEN);
870 memcpy(&ipv4addr.ipv4_addr,&(addrin->sin_addr),sizeof(struct in_addr));
871 ipv4addr.u_port = addrin->sin_port;
873 addr_len = sizeof(struct IPv4HttpAddress);
875 /* Incoming IPv6 connection */
876 if ( AF_INET6 == conn_info->client_addr->sin_family)
878 addrin6 = (struct sockaddr_in6 *) conn_info->client_addr;
879 inet_ntop(addrin6->sin6_family, &(addrin6->sin6_addr),address,INET6_ADDRSTRLEN);
880 memcpy(&ipv6addr.ipv6_addr,&(addrin6->sin6_addr),sizeof(struct in6_addr));
881 ipv6addr.u6_port = addrin6->sin6_port;
883 addr_len = sizeof(struct IPv6HttpAddress);
886 GNUNET_assert (addr != NULL);
887 GNUNET_assert (addr_len != 0);
890 /* only inbound sessions here */
895 if ((ps_tmp->direction==INBOUND) && (ps_tmp->session_id == id_num) && (id_num!=0))
897 if ((ps_tmp->recv_force_disconnect!=GNUNET_YES) && (ps_tmp->send_force_disconnect!=GNUNET_YES))
906 ps = GNUNET_malloc(sizeof (struct Session));
907 ps->addr = GNUNET_malloc(addr_len);
908 memcpy(ps->addr,addr,addr_len);
909 ps->addrlen = addr_len;
910 ps->direction=INBOUND;
911 ps->pending_msgs_head = NULL;
912 ps->pending_msgs_tail = NULL;
913 ps->send_connected=GNUNET_NO;
914 ps->send_active=GNUNET_NO;
915 ps->recv_connected=GNUNET_NO;
916 ps->recv_active=GNUNET_NO;
918 ps->session_id =id_num;
919 ps->url = create_url (plugin, ps->addr, ps->addrlen, ps->session_id);
920 GNUNET_CONTAINER_DLL_insert(pc->head,pc->tail,ps);
921 GNUNET_STATISTICS_update (plugin->env->stats,
922 gettext_noop ("# HTTP inbound sessions for peers active"),
927 *httpSessionCache = ps;
928 if (ps->msgtok==NULL)
929 ps->msgtok = GNUNET_SERVER_mst_create (&mhd_write_mst_cb, ps);
931 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: HTTP Daemon has new an incoming `%s' request from peer `%s' (`%s')\n",
934 GNUNET_i2s(&pc->identity),
935 http_plugin_address_to_string(NULL, ps->addr, ps->addrlen));
939 /* Is it a PUT or a GET request */
940 if (0 == strcmp (MHD_HTTP_METHOD_PUT, method))
942 if (ps->recv_force_disconnect == GNUNET_YES)
944 #if DEBUG_CONNECTIONS
945 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: inbound connection was forced to disconnect\n",ps);
947 ps->recv_active = GNUNET_NO;
950 if ((*upload_data_size == 0) && (ps->recv_active==GNUNET_NO))
952 ps->recv_endpoint = mhd_connection;
953 ps->recv_connected = GNUNET_YES;
954 ps->recv_active = GNUNET_YES;
955 ps->recv_force_disconnect = GNUNET_NO;
956 #if DEBUG_CONNECTIONS
957 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: inbound PUT connection connected\n",ps);
962 /* Transmission of all data complete */
963 if ((*upload_data_size == 0) && (ps->recv_active == GNUNET_YES))
965 response = MHD_create_response_from_data (strlen (HTTP_PUT_RESPONSE),HTTP_PUT_RESPONSE, MHD_NO, MHD_NO);
966 res = MHD_queue_response (mhd_connection, MHD_HTTP_OK, response);
967 #if DEBUG_CONNECTIONS
968 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: Sent HTTP/1.1: 200 OK as PUT Response\n",ps);
970 MHD_destroy_response (response);
971 ps->recv_active=GNUNET_NO;
976 if ((*upload_data_size > 0) && (ps->recv_active == GNUNET_YES))
978 res = GNUNET_SERVER_mst_receive(ps->msgtok, ps, upload_data,*upload_data_size, GNUNET_NO, GNUNET_NO);
979 (*upload_data_size) = 0;
985 if ( 0 == strcmp (MHD_HTTP_METHOD_GET, method) )
987 if (ps->send_force_disconnect == GNUNET_YES)
989 #if DEBUG_CONNECTIONS
990 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: outbound connection was forced to disconnect\n",ps);
992 ps->send_active = GNUNET_NO;
995 ps->send_connected = GNUNET_YES;
996 ps->send_active = GNUNET_YES;
997 ps->send_endpoint = mhd_connection;
998 ps->send_force_disconnect = GNUNET_NO;
999 #if DEBUG_CONNECTIONS
1000 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: inbound GET connection connected\n",ps);
1002 response = MHD_create_response_from_callback(-1,32 * 1024, &mhd_send_callback, ps, NULL);
1003 res = MHD_queue_response (mhd_connection, MHD_HTTP_OK, response);
1004 MHD_destroy_response (response);
1011 * Function that queries MHD's select sets and
1012 * starts the task waiting for them.
1014 static GNUNET_SCHEDULER_TaskIdentifier
1015 http_server_daemon_prepare (void * cls, struct MHD_Daemon *daemon_handle)
1017 struct Plugin *plugin = cls;
1018 GNUNET_SCHEDULER_TaskIdentifier ret;
1022 struct GNUNET_NETWORK_FDSet *wrs;
1023 struct GNUNET_NETWORK_FDSet *wws;
1024 struct GNUNET_NETWORK_FDSet *wes;
1026 unsigned long long timeout;
1028 struct GNUNET_TIME_Relative tv;
1030 GNUNET_assert(cls !=NULL);
1031 ret = GNUNET_SCHEDULER_NO_TASK;
1035 wrs = GNUNET_NETWORK_fdset_create ();
1036 wes = GNUNET_NETWORK_fdset_create ();
1037 wws = GNUNET_NETWORK_fdset_create ();
1039 GNUNET_assert (MHD_YES ==
1040 MHD_get_fdset (daemon_handle,
1045 haveto = MHD_get_timeout (daemon_handle, &timeout);
1046 if (haveto == MHD_YES)
1047 tv.value = (uint64_t) timeout;
1049 tv = GNUNET_TIME_UNIT_FOREVER_REL;
1050 GNUNET_NETWORK_fdset_copy_native (wrs, &rs, max);
1051 GNUNET_NETWORK_fdset_copy_native (wws, &ws, max);
1052 GNUNET_NETWORK_fdset_copy_native (wes, &es, max);
1053 if (daemon_handle == plugin->http_server_daemon_v4)
1055 if (plugin->http_server_task_v4 != GNUNET_SCHEDULER_NO_TASK)
1057 GNUNET_SCHEDULER_cancel(plugin->env->sched, plugin->http_server_task_v4);
1058 plugin->http_server_daemon_v4 = GNUNET_SCHEDULER_NO_TASK;
1061 ret = GNUNET_SCHEDULER_add_select (plugin->env->sched,
1062 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1063 GNUNET_SCHEDULER_NO_TASK,
1067 &http_server_daemon_v4_run,
1070 if (daemon_handle == plugin->http_server_daemon_v6)
1072 if (plugin->http_server_task_v6 != GNUNET_SCHEDULER_NO_TASK)
1074 GNUNET_SCHEDULER_cancel(plugin->env->sched, plugin->http_server_task_v6);
1075 plugin->http_server_task_v6 = GNUNET_SCHEDULER_NO_TASK;
1078 ret = GNUNET_SCHEDULER_add_select (plugin->env->sched,
1079 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1080 GNUNET_SCHEDULER_NO_TASK,
1084 &http_server_daemon_v6_run,
1087 GNUNET_NETWORK_fdset_destroy (wrs);
1088 GNUNET_NETWORK_fdset_destroy (wws);
1089 GNUNET_NETWORK_fdset_destroy (wes);
1094 * Call MHD to process pending requests and then go back
1095 * and schedule the next run.
1097 static void http_server_daemon_v4_run (void *cls,
1098 const struct GNUNET_SCHEDULER_TaskContext *tc)
1100 struct Plugin *plugin = cls;
1102 GNUNET_assert(cls !=NULL);
1103 plugin->http_server_task_v4 = GNUNET_SCHEDULER_NO_TASK;
1105 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1108 GNUNET_assert (MHD_YES == MHD_run (plugin->http_server_daemon_v4));
1109 plugin->http_server_task_v4 = http_server_daemon_prepare (plugin, plugin->http_server_daemon_v4);
1115 * Call MHD to process pending requests and then go back
1116 * and schedule the next run.
1118 static void http_server_daemon_v6_run (void *cls,
1119 const struct GNUNET_SCHEDULER_TaskContext *tc)
1121 struct Plugin *plugin = cls;
1123 GNUNET_assert(cls !=NULL);
1124 plugin->http_server_task_v6 = GNUNET_SCHEDULER_NO_TASK;
1126 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1129 GNUNET_assert (MHD_YES == MHD_run (plugin->http_server_daemon_v6));
1130 plugin->http_server_task_v6 = http_server_daemon_prepare (plugin, plugin->http_server_daemon_v6);
1134 static size_t curl_get_header_cb( void *ptr, size_t size, size_t nmemb, void *stream)
1136 struct Session * ps = stream;
1138 long http_result = 0;
1140 /* Getting last http result code */
1141 GNUNET_assert(NULL!=ps);
1142 if (ps->recv_connected==GNUNET_NO)
1144 res = curl_easy_getinfo(ps->recv_endpoint, CURLINFO_RESPONSE_CODE, &http_result);
1145 if (CURLE_OK == res)
1147 if (http_result == 200)
1149 ps->recv_connected = GNUNET_YES;
1150 ps->recv_active = GNUNET_YES;
1151 #if DEBUG_CONNECTIONS
1152 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: connected to recieve data\n",ps);
1154 // Calling send_check_connections again since receive is established
1155 send_check_connections (ps->peercontext->plugin, ps);
1162 size_t len = size * nmemb;
1164 if ((size * nmemb) < SIZE_MAX)
1165 tmp = GNUNET_malloc (len+1);
1167 if ((tmp != NULL) && (len > 0))
1169 memcpy(tmp,ptr,len);
1172 if (tmp[len-2] == 13)
1175 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: Header: %s\n",ps,tmp);
1181 return size * nmemb;
1184 static size_t curl_put_header_cb( void *ptr, size_t size, size_t nmemb, void *stream)
1186 struct Session * ps = stream;
1189 size_t len = size * nmemb;
1190 long http_result = 0;
1193 /* Getting last http result code */
1194 GNUNET_assert(NULL!=ps);
1195 res = curl_easy_getinfo(ps->send_endpoint, CURLINFO_RESPONSE_CODE, &http_result);
1196 if (CURLE_OK == res)
1198 if ((http_result == 100) && (ps->send_connected==GNUNET_NO))
1200 ps->send_connected = GNUNET_YES;
1201 ps->send_active = GNUNET_YES;
1202 #if DEBUG_CONNECTIONS
1203 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: connected to send data\n",ps);
1206 if ((http_result == 200) && (ps->send_connected==GNUNET_YES))
1208 ps->send_connected = GNUNET_NO;
1209 ps->send_active = GNUNET_NO;
1210 #if DEBUG_CONNECTIONS
1211 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: sending disconnected\n",ps);
1217 if ((size * nmemb) < SIZE_MAX)
1218 tmp = GNUNET_malloc (len+1);
1220 if ((tmp != NULL) && (len > 0))
1222 memcpy(tmp,ptr,len);
1225 if (tmp[len-2] == 13)
1232 return size * nmemb;
1236 * Callback method used with libcurl
1237 * Method is called when libcurl needs to read data during sending
1238 * @param stream pointer where to write data
1239 * @param size size of an individual element
1240 * @param nmemb count of elements that can be written to the buffer
1241 * @param ptr source pointer, passed to the libcurl handle
1242 * @return bytes written to stream
1244 static size_t curl_send_cb(void *stream, size_t size, size_t nmemb, void *ptr)
1246 struct Session * ps = ptr;
1247 struct HTTP_Message * msg = ps->pending_msgs_tail;
1251 if (ps->send_active == GNUNET_NO)
1252 return CURL_READFUNC_PAUSE;
1255 if ((ps->pending_msgs_tail == NULL) && (ps->send_active == GNUNET_YES))
1257 #if DEBUG_CONNECTIONS
1258 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: No Message to send, pausing connection\n",ps);
1260 ps->send_active = GNUNET_NO;
1261 return CURL_READFUNC_PAUSE;
1264 GNUNET_assert (msg!=NULL);
1267 if (msg->pos < msg->size)
1269 /* data fit in buffer */
1270 if ((msg->size - msg->pos) <= (size * nmemb))
1272 len = (msg->size - msg->pos);
1273 memcpy(stream, &msg->buf[msg->pos], len);
1280 memcpy(stream, &msg->buf[msg->pos], len);
1285 /* no data to send */
1291 if ( msg->pos == msg->size)
1293 #if DEBUG_CONNECTIONS
1294 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: Message with %u bytes sent, removing message from queue \n",ps, msg->pos);
1296 /* Calling transmit continuation */
1297 if (NULL != ps->pending_msgs_tail->transmit_cont)
1298 msg->transmit_cont (ps->pending_msgs_tail->transmit_cont_cls,&(ps->peercontext)->identity,GNUNET_OK);
1299 remove_http_message(ps, msg);
1304 static void curl_receive_mst_cb (void *cls,
1306 const struct GNUNET_MessageHeader *message)
1308 struct Session *ps = cls;
1309 GNUNET_assert(ps != NULL);
1311 struct HTTP_PeerContext *pc = ps->peercontext;
1312 GNUNET_assert(pc != NULL);
1314 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1315 "Connection %X: Forwarding message to transport service, type %u and size %u from `%s' (`%s')\n",
1317 ntohs(message->type),
1318 ntohs(message->size),
1319 GNUNET_i2s(&(pc->identity)),http_plugin_address_to_string(NULL,ps->addr,ps->addrlen));
1321 pc->plugin->env->receive (pc->plugin->env->cls,
1330 * Callback method used with libcurl
1331 * Method is called when libcurl needs to write data during sending
1332 * @param stream pointer where to write data
1333 * @param size size of an individual element
1334 * @param nmemb count of elements that can be written to the buffer
1335 * @param ptr destination pointer, passed to the libcurl handle
1336 * @return bytes read from stream
1338 static size_t curl_receive_cb( void *stream, size_t size, size_t nmemb, void *ptr)
1340 struct Session * ps = ptr;
1341 #if DEBUG_CONNECTIONS
1342 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: %u bytes received\n",ps, size*nmemb);
1344 GNUNET_SERVER_mst_receive(ps->msgtok, ps, stream, size*nmemb, GNUNET_NO, GNUNET_NO);
1345 return (size * nmemb);
1349 static void curl_perform (void *cls,
1350 const struct GNUNET_SCHEDULER_TaskContext *tc)
1352 struct Plugin *plugin = cls;
1353 static unsigned int handles_last_run;
1355 struct CURLMsg *msg;
1357 struct Session *ps = NULL;
1358 struct HTTP_PeerContext *pc = NULL;
1359 struct HTTP_Message * cur_msg = NULL;
1363 GNUNET_assert(cls !=NULL);
1365 plugin->http_curl_task = GNUNET_SCHEDULER_NO_TASK;
1366 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1372 mret = curl_multi_perform (plugin->multi_handle, &running);
1373 if ((running < handles_last_run) && (running>0))
1378 msg = curl_multi_info_read (plugin->multi_handle, &running);
1381 /* get session for affected curl handle */
1382 GNUNET_assert ( msg->easy_handle != NULL );
1383 curl_easy_getinfo(msg->easy_handle, CURLINFO_PRIVATE, &tmp);
1384 ps = (struct Session *) tmp;
1385 GNUNET_assert ( ps != NULL );
1386 pc = ps->peercontext;
1387 GNUNET_assert ( pc != NULL );
1392 if ( (msg->data.result != CURLE_OK) &&
1393 (msg->data.result != CURLE_GOT_NOTHING) )
1395 /* sending msg failed*/
1396 if (msg->easy_handle == ps->send_endpoint)
1398 #if DEBUG_CONNECTIONS
1399 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1400 _("Connection %X: HTTP PUT to peer `%s' (`%s') failed: `%s' `%s'\n"),
1402 GNUNET_i2s(&pc->identity),
1403 http_plugin_address_to_string(NULL, ps->addr, ps->addrlen),
1404 "curl_multi_perform",
1405 curl_easy_strerror (msg->data.result));
1407 ps->send_connected = GNUNET_NO;
1408 ps->send_active = GNUNET_NO;
1409 curl_multi_remove_handle(plugin->multi_handle,ps->send_endpoint);
1410 //curl_easy_cleanup(ps->send_endpoint);
1411 //ps->send_endpoint=NULL;
1412 cur_msg = ps->pending_msgs_tail;
1413 if (( NULL != cur_msg) && ( NULL != cur_msg->transmit_cont))
1414 cur_msg->transmit_cont (cur_msg->transmit_cont_cls,&pc->identity,GNUNET_SYSERR);
1416 /* GET connection failed */
1417 if (msg->easy_handle == ps->recv_endpoint)
1419 #if DEBUG_CONNECTIONS
1420 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1421 _("Connection %X: HTTP GET 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->recv_connected = GNUNET_NO;
1429 ps->recv_active = GNUNET_NO;
1430 curl_multi_remove_handle(plugin->multi_handle,ps->recv_endpoint);
1431 //curl_easy_cleanup(ps->recv_endpoint);
1432 //ps->recv_endpoint=NULL;
1437 if (msg->easy_handle == ps->send_endpoint)
1439 GNUNET_assert (CURLE_OK == curl_easy_getinfo(msg->easy_handle, CURLINFO_RESPONSE_CODE, &http_result));
1440 #if DEBUG_CONNECTIONS
1441 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1442 "Connection %X: HTTP PUT connection to peer `%s' (`%s') was closed with HTTP code %u\n",
1444 GNUNET_i2s(&pc->identity),
1445 http_plugin_address_to_string(NULL, ps->addr, ps->addrlen),
1448 /* Calling transmit continuation */
1449 cur_msg = ps->pending_msgs_tail;
1450 if (( NULL != cur_msg) && (NULL != cur_msg->transmit_cont))
1452 /* HTTP 1xx : Last message before here was informational */
1453 if ((http_result >=100) && (http_result < 200))
1454 cur_msg->transmit_cont (cur_msg->transmit_cont_cls,&pc->identity,GNUNET_OK);
1455 /* HTTP 2xx: successful operations */
1456 if ((http_result >=200) && (http_result < 300))
1457 cur_msg->transmit_cont (cur_msg->transmit_cont_cls,&pc->identity,GNUNET_OK);
1458 /* HTTP 3xx..5xx: error */
1459 if ((http_result >=300) && (http_result < 600))
1460 cur_msg->transmit_cont (cur_msg->transmit_cont_cls,&pc->identity,GNUNET_SYSERR);
1462 ps->send_connected = GNUNET_NO;
1463 ps->send_active = GNUNET_NO;
1464 curl_multi_remove_handle(plugin->multi_handle,ps->send_endpoint);
1465 //curl_easy_cleanup(ps->send_endpoint);
1466 //ps->send_endpoint =NULL;
1468 if (msg->easy_handle == ps->recv_endpoint)
1470 #if DEBUG_CONNECTIONS
1471 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1472 "Connection %X: HTTP GET connection to peer `%s' (`%s') was closed with HTTP code %u\n",
1474 GNUNET_i2s(&pc->identity),
1475 http_plugin_address_to_string(NULL, ps->addr, ps->addrlen),
1478 ps->recv_connected = GNUNET_NO;
1479 ps->recv_active = GNUNET_NO;
1480 curl_multi_remove_handle(plugin->multi_handle,ps->recv_endpoint);
1481 //curl_easy_cleanup(ps->recv_endpoint);
1482 //ps->recv_endpoint=NULL;
1485 if ((ps->recv_connected == GNUNET_NO) && (ps->send_connected == GNUNET_NO))
1486 remove_session (pc, ps, GNUNET_YES, GNUNET_SYSERR);
1493 while ( (running > 0) );
1495 handles_last_run = running;
1497 while (mret == CURLM_CALL_MULTI_PERFORM);
1498 curl_schedule(plugin);
1503 * Function setting up file descriptors and scheduling task to run
1504 * @param ses session to send data to
1505 * @return GNUNET_SYSERR for hard failure, GNUNET_OK for ok
1507 static int curl_schedule(void *cls)
1509 struct Plugin *plugin = cls;
1514 struct GNUNET_NETWORK_FDSet *grs;
1515 struct GNUNET_NETWORK_FDSet *gws;
1519 GNUNET_assert(cls !=NULL);
1521 /* Cancel previous scheduled task */
1522 if (plugin->http_curl_task != GNUNET_SCHEDULER_NO_TASK)
1524 GNUNET_SCHEDULER_cancel(plugin->env->sched, plugin->http_curl_task);
1525 plugin->http_curl_task = GNUNET_SCHEDULER_NO_TASK;
1531 mret = curl_multi_fdset (plugin->multi_handle, &rs, &ws, &es, &max);
1532 if (mret != CURLM_OK)
1534 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1535 _("%s failed at %s:%d: `%s'\n"),
1536 "curl_multi_fdset", __FILE__, __LINE__,
1537 curl_multi_strerror (mret));
1538 return GNUNET_SYSERR;
1540 mret = curl_multi_timeout (plugin->multi_handle, &to);
1541 if (mret != CURLM_OK)
1543 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1544 _("%s failed at %s:%d: `%s'\n"),
1545 "curl_multi_timeout", __FILE__, __LINE__,
1546 curl_multi_strerror (mret));
1547 return GNUNET_SYSERR;
1550 grs = GNUNET_NETWORK_fdset_create ();
1551 gws = GNUNET_NETWORK_fdset_create ();
1552 GNUNET_NETWORK_fdset_copy_native (grs, &rs, max + 1);
1553 GNUNET_NETWORK_fdset_copy_native (gws, &ws, max + 1);
1554 plugin->http_curl_task = GNUNET_SCHEDULER_add_select (plugin->env->sched,
1555 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1556 GNUNET_SCHEDULER_NO_TASK,
1557 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 0),
1562 GNUNET_NETWORK_fdset_destroy (gws);
1563 GNUNET_NETWORK_fdset_destroy (grs);
1568 * Function setting up curl handle and selecting message to send
1570 * @param ses session to send data to
1571 * @param con connection
1572 * @return GNUNET_SYSERR on failure, GNUNET_NO if connecting, GNUNET_YES if ok
1574 static ssize_t send_check_connections (void *cls, struct Session *ps)
1576 struct Plugin *plugin = cls;
1578 struct HTTP_Message * msg;
1580 struct GNUNET_TIME_Relative timeout = GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT;
1582 GNUNET_assert(cls !=NULL);
1584 if (ps->direction == OUTBOUND)
1586 /* RECV DIRECTION */
1587 /* Check if session is connected to receive data, otherwise connect to peer */
1588 if (ps->recv_connected == GNUNET_NO)
1590 int fresh = GNUNET_NO;
1591 if (ps->recv_endpoint == NULL)
1594 ps->recv_endpoint = curl_easy_init();
1597 curl_easy_setopt(ps->recv_endpoint, CURLOPT_VERBOSE, 1L);
1599 curl_easy_setopt(ps->recv_endpoint, CURLOPT_URL, ps->url);
1600 curl_easy_setopt(ps->recv_endpoint, CURLOPT_HEADERFUNCTION, &curl_get_header_cb);
1601 curl_easy_setopt(ps->recv_endpoint, CURLOPT_WRITEHEADER, ps);
1602 curl_easy_setopt(ps->recv_endpoint, CURLOPT_READFUNCTION, curl_send_cb);
1603 curl_easy_setopt(ps->recv_endpoint, CURLOPT_READDATA, ps);
1604 curl_easy_setopt(ps->recv_endpoint, CURLOPT_WRITEFUNCTION, curl_receive_cb);
1605 curl_easy_setopt(ps->recv_endpoint, CURLOPT_WRITEDATA, ps);
1606 curl_easy_setopt(ps->recv_endpoint, CURLOPT_TIMEOUT, (long) timeout.value);
1607 curl_easy_setopt(ps->recv_endpoint, CURLOPT_PRIVATE, ps);
1608 curl_easy_setopt(ps->recv_endpoint, CURLOPT_CONNECTTIMEOUT, HTTP_CONNECT_TIMEOUT);
1609 curl_easy_setopt(ps->recv_endpoint, CURLOPT_BUFFERSIZE, GNUNET_SERVER_MAX_MESSAGE_SIZE);
1611 if (fresh==GNUNET_YES)
1613 mret = curl_multi_add_handle(plugin->multi_handle, ps->recv_endpoint);
1614 if (mret != CURLM_OK)
1616 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1617 _("Connection: %X: %s failed at %s:%d: `%s'\n"),
1619 "curl_multi_add_handle", __FILE__, __LINE__,
1620 curl_multi_strerror (mret));
1621 return GNUNET_SYSERR;
1624 if (curl_schedule (plugin) == GNUNET_SYSERR)
1626 #if DEBUG_CONNECTIONS
1627 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: could not schedule curl task\n",ps);
1629 return GNUNET_SYSERR;
1631 #if DEBUG_CONNECTIONS
1632 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: inbound not connected, initiating connection\n",ps);
1636 /* waiting for receive direction */
1637 if (ps->recv_connected==GNUNET_NO)
1640 /* SEND DIRECTION */
1641 /* Check if session is connected to send data, otherwise connect to peer */
1642 if ((ps->send_connected == GNUNET_YES) && (ps->send_endpoint!= NULL))
1644 if (ps->send_active == GNUNET_YES)
1646 #if DEBUG_CONNECTIONS
1647 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: outbound active, enqueueing message\n",ps);
1651 if (ps->send_active == GNUNET_NO)
1653 #if DEBUG_CONNECTIONS
1654 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: outbound paused, unpausing existing connection and enqueueing message\n",ps);
1656 if (CURLE_OK == curl_easy_pause(ps->send_endpoint,CURLPAUSE_CONT))
1658 ps->send_active=GNUNET_YES;
1662 return GNUNET_SYSERR;
1665 /* not connected, initiate connection */
1666 if (ps->send_connected==GNUNET_NO)
1668 int fresh = GNUNET_NO;
1669 if (NULL == ps->send_endpoint)
1671 ps->send_endpoint = curl_easy_init();
1674 GNUNET_assert (ps->send_endpoint != NULL);
1675 GNUNET_assert (NULL != ps->pending_msgs_tail);
1676 #if DEBUG_CONNECTIONS
1677 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: outbound not connected, initiating connection\n",ps);
1679 ps->send_active = GNUNET_NO;
1680 msg = ps->pending_msgs_tail;
1683 curl_easy_setopt(ps->send_endpoint, CURLOPT_VERBOSE, 1L);
1685 curl_easy_setopt(ps->send_endpoint, CURLOPT_URL, ps->url);
1686 curl_easy_setopt(ps->send_endpoint, CURLOPT_PUT, 1L);
1687 curl_easy_setopt(ps->send_endpoint, CURLOPT_HEADERFUNCTION, &curl_put_header_cb);
1688 curl_easy_setopt(ps->send_endpoint, CURLOPT_WRITEHEADER, ps);
1689 curl_easy_setopt(ps->send_endpoint, CURLOPT_READFUNCTION, curl_send_cb);
1690 curl_easy_setopt(ps->send_endpoint, CURLOPT_READDATA, ps);
1691 curl_easy_setopt(ps->send_endpoint, CURLOPT_WRITEFUNCTION, curl_receive_cb);
1692 curl_easy_setopt(ps->send_endpoint, CURLOPT_READDATA, ps);
1693 curl_easy_setopt(ps->send_endpoint, CURLOPT_TIMEOUT, (long) timeout.value);
1694 curl_easy_setopt(ps->send_endpoint, CURLOPT_PRIVATE, ps);
1695 curl_easy_setopt(ps->send_endpoint, CURLOPT_CONNECTTIMEOUT, HTTP_CONNECT_TIMEOUT);
1696 curl_easy_setopt(ps->send_endpoint, CURLOPT_BUFFERSIZE, GNUNET_SERVER_MAX_MESSAGE_SIZE);
1698 if (fresh==GNUNET_YES)
1700 mret = curl_multi_add_handle(plugin->multi_handle, ps->send_endpoint);
1701 if (mret != CURLM_OK)
1703 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1704 _("Connection: %X: %s failed at %s:%d: `%s'\n"),
1706 "curl_multi_add_handle", __FILE__, __LINE__,
1707 curl_multi_strerror (mret));
1708 return GNUNET_SYSERR;
1712 if (curl_schedule (plugin) == GNUNET_SYSERR)
1713 return GNUNET_SYSERR;
1716 if (ps->direction == INBOUND)
1718 GNUNET_assert (NULL != ps->pending_msgs_tail);
1719 if ((ps->recv_connected==GNUNET_YES) && (ps->send_connected==GNUNET_YES) &&
1720 (ps->recv_force_disconnect==GNUNET_NO) && (ps->recv_force_disconnect==GNUNET_NO))
1723 return GNUNET_SYSERR;
1726 static struct Session * send_select_session (void * cls, struct HTTP_PeerContext *pc, const void * addr, size_t addrlen, int force_address, struct Session * session)
1728 struct Session * tmp = NULL;
1729 int addr_given = GNUNET_NO;
1731 if ((addr!=NULL) && (addrlen>0))
1732 addr_given = GNUNET_YES;
1734 if (force_address == GNUNET_YES)
1736 /* check session given as argument */
1737 if ((session != NULL) && (addr_given == GNUNET_YES))
1739 if (0 == memcmp(session->addr, addr, addrlen))
1741 /* connection can not be used, since it is disconnected */
1742 if ((session->recv_force_disconnect==GNUNET_NO) && (session->send_force_disconnect==GNUNET_NO))
1744 #if DEBUG_SESSION_SELECTION
1745 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Session %X selected: Using session passed by transport to send to forced address \n", session);
1751 /* check last session used */
1752 if ((pc->last_session != NULL)&& (addr_given == GNUNET_YES))
1754 if (0 == memcmp(pc->last_session->addr, addr, addrlen))
1756 /* connection can not be used, since it is disconnected */
1757 if ((pc->last_session->recv_force_disconnect==GNUNET_NO) && (pc->last_session->send_force_disconnect==GNUNET_NO))
1759 #if DEBUG_SESSION_SELECTION
1760 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Session %X selected: Using last session used to send to forced address \n", pc->last_session);
1762 return pc->last_session;
1766 /* find session in existing sessions */
1768 while ((tmp!=NULL) && (addr_given == GNUNET_YES))
1771 if (0 == memcmp(tmp->addr, addr, addrlen))
1773 /* connection can not be used, since it is disconnected */
1774 if ((tmp->recv_force_disconnect==GNUNET_NO) && (tmp->send_force_disconnect==GNUNET_NO))
1776 #if DEBUG_SESSION_SELECTION
1777 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Session %X selected: Using existing session to send to forced address \n", session);
1785 /* no session to use */
1788 if ((force_address == GNUNET_NO) || (force_address == GNUNET_SYSERR))
1790 /* check session given as argument */
1791 if (session != NULL)
1793 /* connection can not be used, since it is disconnected */
1794 if ((session->recv_force_disconnect==GNUNET_NO) && (session->send_force_disconnect==GNUNET_NO))
1796 #if DEBUG_SESSION_SELECTION
1797 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Session %X selected: Using session passed by transport to send not-forced address \n", session);
1803 /* check last session used */
1804 if (pc->last_session != NULL)
1806 /* connection can not be used, since it is disconnected */
1807 if ((pc->last_session->recv_force_disconnect==GNUNET_NO) && (pc->last_session->send_force_disconnect==GNUNET_NO))
1809 #if DEBUG_SESSION_SELECTION
1810 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Session %X selected: Using last session to send to not-forced address \n", pc->last_session);
1812 return pc->last_session;
1815 /* find session in existing sessions */
1819 /* connection can not be used, since it is disconnected */
1820 if ((tmp->recv_force_disconnect==GNUNET_NO) && (tmp->send_force_disconnect==GNUNET_NO))
1822 #if DEBUG_SESSION_SELECTION
1823 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Session %X selected: Using existing session to send to not-forced address \n", tmp);
1835 * Function that can be used by the transport service to transmit
1836 * a message using the plugin. Note that in the case of a
1837 * peer disconnecting, the continuation MUST be called
1838 * prior to the disconnect notification itself. This function
1839 * will be called with this peer's HELLO message to initiate
1840 * a fresh connection to another peer.
1842 * @param cls closure
1843 * @param target who should receive this message
1844 * @param msgbuf the message to transmit
1845 * @param msgbuf_size number of bytes in 'msgbuf'
1846 * @param priority how important is the message (most plugins will
1847 * ignore message priority and just FIFO)
1848 * @param timeout how long to wait at most for the transmission (does not
1849 * require plugins to discard the message after the timeout,
1850 * just advisory for the desired delay; most plugins will ignore
1852 * @param session which session must be used (or NULL for "any")
1853 * @param addr the address to use (can be NULL if the plugin
1854 * is "on its own" (i.e. re-use existing TCP connection))
1855 * @param addrlen length of the address in bytes
1856 * @param force_address GNUNET_YES if the plugin MUST use the given address,
1857 * GNUNET_NO means the plugin may use any other address and
1858 * GNUNET_SYSERR means that only reliable existing
1859 * bi-directional connections should be used (regardless
1861 * @param cont continuation to call once the message has
1862 * been transmitted (or if the transport is ready
1863 * for the next transmission call; or if the
1864 * peer disconnected...); can be NULL
1865 * @param cont_cls closure for cont
1866 * @return number of bytes used (on the physical network, with overheads);
1867 * -1 on hard errors (i.e. address invalid); 0 is a legal value
1868 * and does NOT mean that the message was not transmitted (DV)
1871 http_plugin_send (void *cls,
1872 const struct GNUNET_PeerIdentity *target,
1875 unsigned int priority,
1876 struct GNUNET_TIME_Relative to,
1877 struct Session *session,
1881 GNUNET_TRANSPORT_TransmitContinuation cont,
1884 struct Plugin *plugin = cls;
1885 struct HTTP_Message *msg;
1886 struct HTTP_PeerContext * pc;
1887 struct Session * ps = NULL;
1889 GNUNET_assert(cls !=NULL);
1892 char * force = GNUNET_malloc(40);
1893 if (force_address == GNUNET_YES)
1894 strcpy(force,"forced addr.");
1895 if (force_address == GNUNET_NO)
1896 strcpy(force,"any addr.");
1897 if (force_address == GNUNET_SYSERR)
1898 strcpy(force,"reliable bi-direc. address addr.");
1900 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Transport tells me to send %u bytes to `%s' using %s (%s) and session: %X\n",
1904 http_plugin_address_to_string(NULL, addr, addrlen),
1910 pc = GNUNET_CONTAINER_multihashmap_get (plugin->peers, &target->hashPubKey);
1914 pc = GNUNET_malloc(sizeof (struct HTTP_PeerContext));
1915 pc->plugin = plugin;
1916 pc->session_id_counter=1;
1917 pc->last_session = NULL;
1918 memcpy(&pc->identity, target, sizeof(struct GNUNET_PeerIdentity));
1919 GNUNET_CONTAINER_multihashmap_put(plugin->peers, &pc->identity.hashPubKey, pc, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
1920 GNUNET_STATISTICS_update (plugin->env->stats,
1921 gettext_noop ("# HTTP peers active"),
1926 ps = send_select_session (plugin, pc, addr, addrlen, force_address, session);
1928 /* session not existing, but address forced -> creating new session */
1931 if ((addr!=NULL) && (addrlen!=0))
1933 ps = GNUNET_malloc(sizeof (struct Session));
1934 #if DEBUG_SESSION_SELECTION
1935 if (force_address == GNUNET_YES)
1936 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"No existing connection & forced address: creating new session %X to peer %s\n", ps, GNUNET_i2s(target));
1937 if (force_address != GNUNET_YES)
1938 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"No existing connection: creating new session %X to peer %s\n", ps, GNUNET_i2s(target));
1940 if ((addrlen!=0) && (addr!=NULL))
1942 ps->addr = GNUNET_malloc(addrlen);
1943 memcpy(ps->addr,addr,addrlen);
1944 ps->addrlen = addrlen;
1951 ps->direction=OUTBOUND;
1952 ps->recv_connected = GNUNET_NO;
1953 ps->recv_force_disconnect = GNUNET_NO;
1954 ps->send_connected = GNUNET_NO;
1955 ps->send_force_disconnect = GNUNET_NO;
1956 ps->pending_msgs_head = NULL;
1957 ps->pending_msgs_tail = NULL;
1959 ps->session_id = pc->session_id_counter;
1960 pc->session_id_counter++;
1961 ps->url = create_url (plugin, ps->addr, ps->addrlen, ps->session_id);
1962 if (ps->msgtok == NULL)
1963 ps->msgtok = GNUNET_SERVER_mst_create (&curl_receive_mst_cb, ps);
1964 GNUNET_CONTAINER_DLL_insert(pc->head,pc->tail,ps);
1967 GNUNET_STATISTICS_update (plugin->env->stats,
1968 gettext_noop ("# HTTP outbound sessions for peers active"),
1975 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));
1977 return GNUNET_SYSERR;
1982 msg = GNUNET_malloc (sizeof (struct HTTP_Message) + msgbuf_size);
1984 msg->size = msgbuf_size;
1986 msg->buf = (char *) &msg[1];
1987 msg->transmit_cont = cont;
1988 msg->transmit_cont_cls = cont_cls;
1989 memcpy (msg->buf,msgbuf, msgbuf_size);
1990 GNUNET_CONTAINER_DLL_insert(ps->pending_msgs_head,ps->pending_msgs_tail,msg);
1992 if (send_check_connections (plugin, ps) != GNUNET_SYSERR)
1994 if (force_address != GNUNET_YES)
1995 pc->last_session = ps;
1997 if (pc->last_session==NULL)
1998 pc->last_session = ps;
2002 return GNUNET_SYSERR;
2008 * Function that can be used to force the plugin to disconnect
2009 * from the given peer and cancel all previous transmissions
2010 * (and their continuationc).
2012 * @param cls closure
2013 * @param target peer from which to disconnect
2016 http_plugin_disconnect (void *cls,
2017 const struct GNUNET_PeerIdentity *target)
2021 struct Plugin *plugin = cls;
2022 struct HTTP_PeerContext *pc = NULL;
2023 struct Session *ps = NULL;
2024 //struct Session *tmp = NULL;
2026 pc = GNUNET_CONTAINER_multihashmap_get (plugin->peers, &target->hashPubKey);
2033 /* Telling transport that session is getting disconnected */
2034 plugin->env->session_end(plugin, target, ps);
2035 if (ps->direction==OUTBOUND)
2037 if (ps->send_endpoint!=NULL)
2039 //GNUNET_assert(CURLM_OK == curl_multi_remove_handle(plugin->multi_handle,ps->send_endpoint));
2040 //curl_easy_cleanup(ps->send_endpoint);
2041 //ps->send_endpoint=NULL;
2042 ps->send_force_disconnect = GNUNET_YES;
2044 if (ps->recv_endpoint!=NULL)
2046 //GNUNET_assert(CURLM_OK == curl_multi_remove_handle(plugin->multi_handle,ps->recv_endpoint));
2047 //curl_easy_cleanup(ps->recv_endpoint);
2048 //ps->recv_endpoint=NULL;
2049 ps->recv_force_disconnect = GNUNET_YES;
2053 if (ps->direction==INBOUND)
2055 ps->recv_force_disconnect = GNUNET_YES;
2056 ps->send_force_disconnect = GNUNET_YES;
2059 while (ps->pending_msgs_head!=NULL)
2061 remove_http_message(ps, ps->pending_msgs_head);
2063 ps->recv_active = GNUNET_NO;
2064 ps->send_active = GNUNET_NO;
2071 * Convert the transports address to a nice, human-readable
2074 * @param cls closure
2075 * @param type name of the transport that generated the address
2076 * @param addr one of the addresses of the host, NULL for the last address
2077 * the specific address format depends on the transport
2078 * @param addrlen length of the address
2079 * @param numeric should (IP) addresses be displayed in numeric form?
2080 * @param timeout after how long should we give up?
2081 * @param asc function to call on each string
2082 * @param asc_cls closure for asc
2085 http_plugin_address_pretty_printer (void *cls,
2090 struct GNUNET_TIME_Relative timeout,
2091 GNUNET_TRANSPORT_AddressStringCallback
2094 const struct IPv4HttpAddress *t4;
2095 const struct IPv6HttpAddress *t6;
2096 struct sockaddr_in a4;
2097 struct sockaddr_in6 a6;
2103 GNUNET_assert(cls !=NULL);
2104 if (addrlen == sizeof (struct IPv6HttpAddress))
2106 address = GNUNET_malloc (INET6_ADDRSTRLEN);
2108 a6.sin6_addr = t6->ipv6_addr;
2109 inet_ntop(AF_INET6, &(a6.sin6_addr),address,INET6_ADDRSTRLEN);
2110 port = ntohs(t6->u6_port);
2112 else if (addrlen == sizeof (struct IPv4HttpAddress))
2114 address = GNUNET_malloc (INET_ADDRSTRLEN);
2116 a4.sin_addr.s_addr = t4->ipv4_addr;
2117 inet_ntop(AF_INET, &(a4.sin_addr),address,INET_ADDRSTRLEN);
2118 port = ntohs(t4->u_port);
2122 /* invalid address */
2123 GNUNET_break_op (0);
2124 asc (asc_cls, NULL);
2127 res = GNUNET_asprintf(&ret,"%s://%s:%u/", PROTOCOL_PREFIX, address, port);
2128 GNUNET_free (address);
2129 GNUNET_assert(res != 0);
2131 GNUNET_free_non_null (ret);
2137 * Another peer has suggested an address for this
2138 * peer and transport plugin. Check that this could be a valid
2139 * address. If so, consider adding it to the list
2142 * @param cls closure
2143 * @param addr pointer to the address
2144 * @param addrlen length of addr
2145 * @return GNUNET_OK if this is a plausible address for this peer
2149 http_plugin_address_suggested (void *cls,
2150 const void *addr, size_t addrlen)
2152 struct Plugin *plugin = cls;
2153 struct IPv4HttpAddress *v4;
2154 struct IPv6HttpAddress *v6;
2157 GNUNET_assert(cls !=NULL);
2158 if ((addrlen != sizeof (struct IPv4HttpAddress)) &&
2159 (addrlen != sizeof (struct IPv6HttpAddress)))
2161 return GNUNET_SYSERR;
2163 if (addrlen == sizeof (struct IPv4HttpAddress))
2165 v4 = (struct IPv4HttpAddress *) addr;
2166 /* Not skipping loopback
2167 if (INADDR_LOOPBACK == ntohl(v4->ipv4_addr))
2169 return GNUNET_SYSERR;
2171 port = ntohs (v4->u_port);
2172 if (port != plugin->port_inbound)
2174 return GNUNET_SYSERR;
2177 if (addrlen == sizeof (struct IPv6HttpAddress))
2179 v6 = (struct IPv6HttpAddress *) addr;
2180 if (IN6_IS_ADDR_LINKLOCAL (&v6->ipv6_addr))
2182 return GNUNET_SYSERR;
2184 port = ntohs (v6->u6_port);
2185 if (port != plugin->port_inbound)
2187 return GNUNET_SYSERR;
2196 * Function called for a quick conversion of the binary address to
2197 * a numeric address. Note that the caller must not free the
2198 * address and that the next call to this function is allowed
2199 * to override the address again.
2201 * @param cls closure
2202 * @param addr binary address
2203 * @param addrlen length of the address
2204 * @return string representing the same address
2207 http_plugin_address_to_string (void *cls,
2211 const struct IPv4HttpAddress *t4;
2212 const struct IPv6HttpAddress *t6;
2213 struct sockaddr_in a4;
2214 struct sockaddr_in6 a6;
2220 if (addrlen == sizeof (struct IPv6HttpAddress))
2222 address = GNUNET_malloc (INET6_ADDRSTRLEN);
2224 a6.sin6_addr = t6->ipv6_addr;
2225 inet_ntop(AF_INET6, &(a6.sin6_addr),address,INET6_ADDRSTRLEN);
2226 port = ntohs(t6->u6_port);
2228 else if (addrlen == sizeof (struct IPv4HttpAddress))
2230 address = GNUNET_malloc (INET_ADDRSTRLEN);
2232 a4.sin_addr.s_addr = t4->ipv4_addr;
2233 inet_ntop(AF_INET, &(a4.sin_addr),address,INET_ADDRSTRLEN);
2234 port = ntohs(t4->u_port);
2238 /* invalid address */
2241 res = GNUNET_asprintf(&ret,"%s:%u",address,port);
2242 GNUNET_free (address);
2243 GNUNET_assert(res != 0);
2249 * Exit point from the plugin.
2252 libgnunet_plugin_transport_http_done (void *cls)
2254 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
2255 struct Plugin *plugin = api->cls;
2257 GNUNET_assert(cls !=NULL);
2259 if (plugin->http_server_daemon_v4 != NULL)
2261 MHD_stop_daemon (plugin->http_server_daemon_v4);
2262 plugin->http_server_daemon_v4 = NULL;
2264 if (plugin->http_server_daemon_v6 != NULL)
2266 MHD_stop_daemon (plugin->http_server_daemon_v6);
2267 plugin->http_server_daemon_v6 = NULL;
2270 if ( plugin->http_server_task_v4 != GNUNET_SCHEDULER_NO_TASK)
2272 GNUNET_SCHEDULER_cancel(plugin->env->sched, plugin->http_server_task_v4);
2273 plugin->http_server_task_v4 = GNUNET_SCHEDULER_NO_TASK;
2276 if ( plugin->http_server_task_v6 != GNUNET_SCHEDULER_NO_TASK)
2278 GNUNET_SCHEDULER_cancel(plugin->env->sched, plugin->http_server_task_v6);
2279 plugin->http_server_task_v6 = GNUNET_SCHEDULER_NO_TASK;
2283 /* free all peer information */
2284 if (plugin->peers!=NULL)
2286 GNUNET_CONTAINER_multihashmap_iterate (plugin->peers,
2287 &remove_peer_context_Iterator,
2289 GNUNET_CONTAINER_multihashmap_destroy (plugin->peers);
2291 if (plugin->multi_handle!=NULL)
2293 mret = curl_multi_cleanup(plugin->multi_handle);
2295 if ( CURLM_OK != mret)
2296 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"curl multihandle clean up failed\n");
2298 plugin->multi_handle = NULL;
2300 curl_global_cleanup();
2302 if ( plugin->http_curl_task != GNUNET_SCHEDULER_NO_TASK)
2304 GNUNET_SCHEDULER_cancel(plugin->env->sched, plugin->http_curl_task);
2305 plugin->http_curl_task = GNUNET_SCHEDULER_NO_TASK;
2308 GNUNET_free_non_null (plugin->bind4_address);
2309 GNUNET_free_non_null (plugin->bind6_address);
2310 GNUNET_free_non_null(plugin->bind_hostname);
2311 GNUNET_free (plugin);
2314 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Unload http plugin complete...\n");
2321 * Entry point for the plugin.
2324 libgnunet_plugin_transport_http_init (void *cls)
2326 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
2327 struct Plugin *plugin;
2328 struct GNUNET_TRANSPORT_PluginFunctions *api;
2329 struct GNUNET_TIME_Relative gn_timeout;
2330 long long unsigned int port;
2332 GNUNET_assert(cls !=NULL);
2334 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Starting http plugin...\n");
2337 plugin = GNUNET_malloc (sizeof (struct Plugin));
2338 plugin->stats = env->stats;
2340 plugin->peers = NULL;
2341 plugin->bind4_address = NULL;
2342 plugin->use_ipv6 = GNUNET_YES;
2343 plugin->use_ipv4 = GNUNET_YES;
2345 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
2347 api->send = &http_plugin_send;
2348 api->disconnect = &http_plugin_disconnect;
2349 api->address_pretty_printer = &http_plugin_address_pretty_printer;
2350 api->check_address = &http_plugin_address_suggested;
2351 api->address_to_string = &http_plugin_address_to_string;
2353 /* Hashing our identity to use it in URLs */
2354 GNUNET_CRYPTO_hash_to_enc ( &(plugin->env->my_identity->hashPubKey), &plugin->my_ascii_hash_ident);
2356 /* Reading port number from config file */
2357 if (GNUNET_CONFIGURATION_have_value (env->cfg,
2358 "transport-http", "USE_IPv6"))
2360 plugin->use_ipv6 = GNUNET_CONFIGURATION_get_value_yesno (env->cfg,
2364 /* Reading port number from config file */
2365 if (GNUNET_CONFIGURATION_have_value (env->cfg,
2366 "transport-http", "USE_IPv4"))
2368 plugin->use_ipv4 = GNUNET_CONFIGURATION_get_value_yesno (env->cfg,
2372 /* Reading port number from config file */
2374 GNUNET_CONFIGURATION_get_value_number (env->cfg,
2380 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
2382 _("Require valid port number for transport plugin `%s' in configuration!\n"),
2384 libgnunet_plugin_transport_http_done (api);
2388 /* Reading ipv4 addresse to bind to from config file */
2389 if ((plugin->use_ipv4==GNUNET_YES) && (GNUNET_CONFIGURATION_have_value (env->cfg,
2390 "transport-http", "BINDTO4")))
2392 GNUNET_break (GNUNET_OK ==
2393 GNUNET_CONFIGURATION_get_value_string (env->cfg,
2396 &plugin->bind_hostname));
2397 plugin->bind4_address = GNUNET_malloc(sizeof(struct sockaddr_in));
2398 plugin->bind4_address->sin_family = AF_INET;
2399 plugin->bind4_address->sin_port = htons (port);
2401 if (inet_pton(AF_INET,plugin->bind_hostname, &plugin->bind4_address->sin_addr)<=0)
2403 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
2405 _("Misconfigured address to bind to in configuration!\n"),
2407 GNUNET_free(plugin->bind4_address);
2408 GNUNET_free(plugin->bind_hostname);
2409 plugin->bind_hostname = NULL;
2410 plugin->bind4_address = NULL;
2414 /* Reading ipv4 addresse to bind to from config file */
2415 if ((plugin->use_ipv6==GNUNET_YES) && (GNUNET_CONFIGURATION_have_value (env->cfg,
2416 "transport-http", "BINDTO6")))
2418 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (env->cfg,
2421 &plugin->bind_hostname))
2423 plugin->bind6_address = GNUNET_malloc(sizeof(struct sockaddr_in6));
2424 plugin->bind6_address->sin6_family = AF_INET6;
2425 plugin->bind6_address->sin6_port = htons (port);
2427 if (inet_pton(AF_INET6,plugin->bind_hostname, &plugin->bind6_address->sin6_addr)<=0)
2429 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
2431 _("Misconfigured address to bind to in configuration!\n"),
2433 GNUNET_free(plugin->bind6_address);
2434 GNUNET_free(plugin->bind_hostname);
2435 plugin->bind_hostname = NULL;
2436 plugin->bind6_address = NULL;
2441 GNUNET_assert ((port > 0) && (port <= 65535));
2442 plugin->port_inbound = port;
2443 gn_timeout = GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT;
2444 unsigned int timeout = (gn_timeout.value) / 1000;
2445 if ((plugin->http_server_daemon_v6 == NULL) && (plugin->use_ipv6 == GNUNET_YES) && (port != 0))
2447 struct sockaddr * tmp = (struct sockaddr *) plugin->bind6_address;
2448 plugin->http_server_daemon_v6 = MHD_start_daemon (
2449 #if DEBUG_CONNECTIONS
2455 plugin , &mdh_access_cb, plugin,
2456 MHD_OPTION_SOCK_ADDR, tmp,
2457 MHD_OPTION_CONNECTION_LIMIT, (unsigned int) 32,
2458 //MHD_OPTION_PER_IP_CONNECTION_LIMIT, (unsigned int) 6,
2459 MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) timeout,
2460 MHD_OPTION_CONNECTION_MEMORY_LIMIT, (size_t) (16 * 1024),
2461 MHD_OPTION_NOTIFY_COMPLETED, &mhd_termination_cb, NULL,
2464 if ((plugin->http_server_daemon_v4 == NULL) && (plugin->use_ipv4 == GNUNET_YES) && (port != 0))
2466 plugin->http_server_daemon_v4 = MHD_start_daemon (
2467 #if DEBUG_CONNECTIONS
2473 plugin , &mdh_access_cb, plugin,
2474 MHD_OPTION_SOCK_ADDR, (struct sockaddr_in *)plugin->bind4_address,
2475 MHD_OPTION_CONNECTION_LIMIT, (unsigned int) 32,
2476 //MHD_OPTION_PER_IP_CONNECTION_LIMIT, (unsigned int) 6,
2477 MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) timeout,
2478 MHD_OPTION_CONNECTION_MEMORY_LIMIT, (size_t) (16 * 1024),
2479 MHD_OPTION_NOTIFY_COMPLETED, &mhd_termination_cb, NULL,
2482 if (plugin->http_server_daemon_v4 != NULL)
2483 plugin->http_server_task_v4 = http_server_daemon_prepare (plugin, plugin->http_server_daemon_v4);
2484 if (plugin->http_server_daemon_v6 != NULL)
2485 plugin->http_server_task_v6 = http_server_daemon_prepare (plugin, plugin->http_server_daemon_v6);
2488 if (plugin->http_server_task_v4 != GNUNET_SCHEDULER_NO_TASK)
2491 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);
2494 else if ((plugin->http_server_task_v6 != GNUNET_SCHEDULER_NO_TASK) && (plugin->http_server_task_v4 != GNUNET_SCHEDULER_NO_TASK))
2497 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);
2500 else if ((plugin->http_server_task_v6 != GNUNET_SCHEDULER_NO_TASK) && (plugin->http_server_task_v4 == GNUNET_SCHEDULER_NO_TASK))
2503 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);
2509 if ((plugin->use_ipv6 == GNUNET_YES) && (plugin->use_ipv4 == GNUNET_YES))
2510 GNUNET_asprintf(&tmp,"with IPv4 and IPv6 enabled");
2511 if ((plugin->use_ipv6 == GNUNET_NO) && (plugin->use_ipv4 == GNUNET_YES))
2512 GNUNET_asprintf(&tmp,"with IPv4 enabled");
2513 if ((plugin->use_ipv6 == GNUNET_YES) && (plugin->use_ipv4 == GNUNET_NO))
2514 GNUNET_asprintf(&tmp,"with IPv6 enabled");
2515 if ((plugin->use_ipv6 == GNUNET_NO) && (plugin->use_ipv4 == GNUNET_NO))
2516 GNUNET_asprintf(&tmp,"with NO IP PROTOCOL enabled");
2517 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,"HTTP Server with %s could not be started on port %u! https plugin failed!\n",tmp, port);
2519 libgnunet_plugin_transport_http_done (api);
2523 /* Initializing cURL */
2524 curl_global_init(CURL_GLOBAL_ALL);
2525 plugin->multi_handle = curl_multi_init();
2527 if ( NULL == plugin->multi_handle )
2529 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
2531 _("Could not initialize curl multi handle, failed to start http plugin!\n"),
2533 libgnunet_plugin_transport_http_done (api);
2537 plugin->peers = GNUNET_CONTAINER_multihashmap_create (10);
2538 GNUNET_OS_network_interfaces_list (&process_interfaces, plugin);
2543 /* end of plugin_transport_http.c */