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_YES
44 #define DEBUG_CURL GNUNET_YES
45 #define DEBUG_CONNECTIONS GNUNET_NO
46 #define DEBUG_SESSION_SELECTION GNUNET_NO
48 #define INBOUND GNUNET_NO
49 #define OUTBOUND GNUNET_YES
52 * Text of the response sent back after the last bytes of a PUT
53 * request have been received (just to formally obey the HTTP
56 #define HTTP_PUT_RESPONSE "Thank you!"
59 * After how long do we expire an address that we
60 * learned from another peer if it is not reconfirmed
63 #define LEARNED_ADDRESS_EXPIRATION GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_HOURS, 6)
66 * Page returned if request invalid
68 #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>"
71 * Timeout for a http connect
73 #define HTTP_CONNECT_TIMEOUT 30
77 * Network format for IPv4 addresses.
79 struct IPv4HttpAddress
82 * IPv4 address, in network byte order.
84 uint32_t ipv4_addr GNUNET_PACKED;
87 * Port number, in network byte order.
89 uint16_t u_port GNUNET_PACKED;
95 * Network format for IPv6 addresses.
97 struct IPv6HttpAddress
102 struct in6_addr ipv6_addr GNUNET_PACKED;
105 * Port number, in network byte order.
107 uint16_t u6_port GNUNET_PACKED;
113 * Message to send using http
118 * next pointer for double linked list
120 struct HTTP_Message * next;
123 * previous pointer for double linked list
125 struct HTTP_Message * prev;
128 * buffer containing data to send
133 * amount of data already sent
143 * Continuation function to call once the transmission buffer
144 * has again space available. NULL if there is no
145 * continuation to call.
147 GNUNET_TRANSPORT_TransmitContinuation transmit_cont;
150 * Closure for transmit_cont.
152 void *transmit_cont_cls;
156 struct HTTP_PeerContext
161 struct GNUNET_PeerIdentity identity;
164 * Pointer to the global plugin struct.
166 struct Plugin *plugin;
169 * Linked list of connections with this peer
172 struct Session * head;
175 * Linked list of connections with this peer
178 struct Session * tail;
181 * id for next session
183 size_t session_id_counter;
186 * Last session used to send data
188 struct Session * last_session;
197 struct SessionHeader header;
200 * next session in linked list
202 struct Session * next;
205 * previous session in linked list
207 struct Session * prev;
210 * address of this session
225 * Message queue for outbound messages
228 struct HTTP_Message * pending_msgs_head;
231 * Message queue for outbound messages
234 struct HTTP_Message * pending_msgs_tail;
237 * partner peer this connection belongs to
239 struct HTTP_PeerContext * peercontext;
242 * message stream tokenizer for incoming data
244 struct GNUNET_SERVER_MessageStreamTokenizer *msgtok;
248 * outbound: OUTBOUND (GNUNET_YES)
249 * inbound : INBOUND (GNUNET_NO)
251 unsigned int direction;
254 * is session connected to send data?
256 unsigned int send_connected;
259 * is send connection active?
261 unsigned int send_active;
264 * connection disconnect forced (e.g. from transport)
266 unsigned int send_force_disconnect;
269 * is session connected to receive data?
271 unsigned int recv_connected;
274 * is receive connection active?
276 unsigned int recv_active;
279 * connection disconnect forced (e.g. from transport)
281 unsigned int recv_force_disconnect;
284 * id for next session
285 * NOTE: 0 is not an ID, zero is not defined. A correct ID is always > 0
290 * entity managing sending data
291 * outbound session: CURL *
292 * inbound session: mhd_connection *
294 void * send_endpoint;
297 * entity managing recieving data
298 * outbound session: CURL *
299 * inbound session: mhd_connection *
301 void * recv_endpoint;
305 * Encapsulation of all of the state of the plugin.
312 struct GNUNET_TRANSPORT_PluginEnvironment *env;
315 * Handle for reporting statistics.
317 struct GNUNET_STATISTICS_Handle *stats;
319 unsigned int port_inbound;
321 struct GNUNET_CONTAINER_MultiHashMap *peers;
324 * Daemon for listening for new IPv4 connections.
326 struct MHD_Daemon *http_server_daemon_v4;
329 * Daemon for listening for new IPv6connections.
331 struct MHD_Daemon *http_server_daemon_v6;
334 * Our primary task for http daemon handling IPv4 connections
336 GNUNET_SCHEDULER_TaskIdentifier http_server_task_v4;
339 * Our primary task for http daemon handling IPv6 connections
341 GNUNET_SCHEDULER_TaskIdentifier http_server_task_v6;
344 * The task sending data
346 GNUNET_SCHEDULER_TaskIdentifier http_curl_task;
351 CURLM * multi_handle;
354 * Our ASCII encoded, hashed peer identity
355 * This string is used to distinguish between connections and is added to the urls
357 struct GNUNET_CRYPTO_HashAsciiEncoded my_ascii_hash_ident;
359 struct sockaddr_in * bind4_address;
360 struct sockaddr_in6 * bind6_address;
361 char * bind_hostname;
368 * Function called for a quick conversion of the binary address to
369 * a numeric address. Note that the caller must not free the
370 * address and that the next call to this function is allowed
371 * to override the address again.
374 * @param addr binary address
375 * @param addrlen length of the address
376 * @return string representing the same address
379 http_plugin_address_to_string (void *cls,
385 * Call MHD to process pending ipv4 requests and then go back
386 * and schedule the next run.
388 static void http_server_daemon_v4_run (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
390 * Call MHD to process pending ipv6 requests and then go back
391 * and schedule the next run.
393 static void http_server_daemon_v6_run (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
396 * Function setting up curl handle and selecting message to send
398 * @param ses session to send data to
399 * @param con connection
400 * @return bytes sent to peer
402 static ssize_t send_check_connections (void *cls, struct Session *ps);
405 * Function setting up file descriptors and scheduling task to run
407 * @param ses session to send data to
410 static int curl_schedule(void *cls );
414 static char * create_url(void * cls, const void * addr, size_t addrlen, size_t id)
416 struct Plugin *plugin = cls;
419 GNUNET_assert ((addr!=NULL) && (addrlen != 0));
420 GNUNET_asprintf(&url,
422 http_plugin_address_to_string(NULL, addr, addrlen),
423 (char *) (&plugin->my_ascii_hash_ident),id);
429 * Removes a message from the linked list of messages
430 * @param con connection to remove message from
431 * @param msg message to remove
432 * @return GNUNET_SYSERR if msg not found, GNUNET_OK on success
434 static int remove_http_message (struct Session * ps, struct HTTP_Message * msg)
436 GNUNET_CONTAINER_DLL_remove(ps->pending_msgs_head,ps->pending_msgs_tail,msg);
441 int remove_peer_context_Iterator (void *cls, const GNUNET_HashCode *key, void *value);
444 * Removes a session from the linked list of sessions
445 * @param pc peer context
447 * @param call_msg_cont GNUNET_YES to call pending message continuations, otherwise no
448 * @param call_msg_cont_result, result to call message continuations with
449 * @return GNUNET_SYSERR if msg not found, GNUNET_OK on success
451 static int remove_session (struct HTTP_PeerContext * pc, struct Session * ps, int call_msg_cont, int call_msg_cont_result)
453 struct HTTP_Message * msg;
454 struct Plugin * plugin = ps->peercontext->plugin;
456 #if DEBUG_CONNECTIONS
457 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);
459 plugin->env->session_end(plugin, &pc->identity, ps);
461 GNUNET_free_non_null (ps->addr);
462 GNUNET_SERVER_mst_destroy (ps->msgtok);
463 GNUNET_free(ps->url);
465 if (ps->direction==INBOUND)
467 if (ps->recv_endpoint != NULL)
469 curl_easy_cleanup(ps->recv_endpoint);
470 ps->recv_endpoint = NULL;
472 if (ps->send_endpoint != NULL)
474 curl_easy_cleanup(ps->send_endpoint);
475 ps->send_endpoint = NULL;
479 msg = ps->pending_msgs_head;
482 if ((call_msg_cont == GNUNET_YES) && (msg->transmit_cont!=NULL))
484 msg->transmit_cont (msg->transmit_cont_cls,&pc->identity,call_msg_cont_result);
487 GNUNET_CONTAINER_DLL_remove(ps->pending_msgs_head,ps->pending_msgs_head,msg);
488 msg = ps->pending_msgs_head;
491 GNUNET_CONTAINER_DLL_remove(pc->head,pc->tail,ps);
495 /* no sessions left remove peer */
499 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"No sessions left for peer `%s', removing context\n",GNUNET_i2s(&pc->identity));
501 remove_peer_context_Iterator(plugin, &pc->identity.hashPubKey, pc);
507 int remove_peer_context_Iterator (void *cls, const GNUNET_HashCode *key, void *value)
509 struct Plugin *plugin = cls;
510 struct HTTP_PeerContext * pc = value;
511 struct Session * ps = pc->head;
512 struct Session * tmp = NULL;
513 struct HTTP_Message * msg = NULL;
514 struct HTTP_Message * msg_tmp = NULL;
516 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Freeing context for peer `%s'\n",GNUNET_i2s(&pc->identity));
518 GNUNET_CONTAINER_multihashmap_remove (plugin->peers, &pc->identity.hashPubKey, pc);
521 plugin->env->session_end(plugin, &pc->identity, ps);
524 GNUNET_free_non_null (ps->addr);
525 GNUNET_free(ps->url);
526 if (ps->msgtok != NULL)
527 GNUNET_SERVER_mst_destroy (ps->msgtok);
529 msg = ps->pending_msgs_head;
536 if (ps->direction==OUTBOUND)
538 if (ps->send_endpoint!=NULL)
539 curl_easy_cleanup(ps->send_endpoint);
540 if (ps->recv_endpoint!=NULL)
541 curl_easy_cleanup(ps->recv_endpoint);
548 GNUNET_STATISTICS_update (plugin->env->stats,
549 gettext_noop ("# HTTP peers active"),
556 * Add the IP of our network interface to the list of
557 * our external IP addresses.
559 * @param cls the 'struct Plugin*'
560 * @param name name of the interface
561 * @param isDefault do we think this may be our default interface
562 * @param addr address of the interface
563 * @param addrlen number of bytes in addr
564 * @return GNUNET_OK to continue iterating
567 process_interfaces (void *cls,
570 const struct sockaddr *addr, socklen_t addrlen)
572 struct Plugin *plugin = cls;
573 struct IPv4HttpAddress * t4;
574 struct IPv6HttpAddress * t6;
578 GNUNET_assert(cls !=NULL);
579 af = addr->sa_family;
580 if ((af == AF_INET) && (plugin->use_ipv4 == GNUNET_YES) && (plugin->bind6_address == NULL))
582 struct in_addr bnd_cmp = ((struct sockaddr_in *) addr)->sin_addr;
583 t4 = GNUNET_malloc(sizeof(struct IPv4HttpAddress));
584 /* Not skipping loopback addresses
585 if (INADDR_LOOPBACK == ntohl(((struct sockaddr_in *) addr)->sin_addr.s_addr))
591 t4->ipv4_addr = ((struct sockaddr_in *) addr)->sin_addr.s_addr;
592 t4->u_port = htons (plugin->port_inbound);
593 if (plugin->bind4_address != NULL)
595 if (0 == memcmp(&plugin->bind4_address->sin_addr, &bnd_cmp, sizeof (struct in_addr)))
597 plugin->env->notify_address(plugin->env->cls,"http",t4, sizeof (struct IPv4HttpAddress), GNUNET_TIME_UNIT_FOREVER_REL);
602 plugin->env->notify_address(plugin->env->cls,"http",t4, sizeof (struct IPv4HttpAddress), GNUNET_TIME_UNIT_FOREVER_REL);
606 else if ((af == AF_INET6) && (plugin->use_ipv6 == GNUNET_YES) && (plugin->bind4_address == NULL))
608 struct in6_addr bnd_cmp6 = ((struct sockaddr_in6 *) addr)->sin6_addr;
609 t6 = GNUNET_malloc(sizeof(struct IPv6HttpAddress));
610 if (IN6_IS_ADDR_LINKLOCAL (&((struct sockaddr_in6 *) addr)->sin6_addr))
615 if (plugin->bind6_address != NULL)
617 if (0 == memcmp(&plugin->bind6_address->sin6_addr, &bnd_cmp6, sizeof (struct in6_addr)))
619 memcpy (&t6->ipv6_addr,
620 &((struct sockaddr_in6 *) addr)->sin6_addr,
621 sizeof (struct in6_addr));
622 t6->u6_port = htons (plugin->port_inbound);
623 plugin->env->notify_address(plugin->env->cls,"http",t6,sizeof (struct IPv6HttpAddress) , GNUNET_TIME_UNIT_FOREVER_REL);
628 memcpy (&t6->ipv6_addr,
629 &((struct sockaddr_in6 *) addr)->sin6_addr,
630 sizeof (struct in6_addr));
631 t6->u6_port = htons (plugin->port_inbound);
632 plugin->env->notify_address(plugin->env->cls,"http",t6,sizeof (struct IPv6HttpAddress) , GNUNET_TIME_UNIT_FOREVER_REL);
641 * Callback called by MHD when a connection is terminated
643 static void mhd_termination_cb (void *cls, struct MHD_Connection * connection, void **httpSessionCache)
645 struct Session * ps = *httpSessionCache;
648 struct HTTP_PeerContext * pc = ps->peercontext;
650 if (connection==ps->recv_endpoint)
652 #if DEBUG_CONNECTIONS
653 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: inbound connection from peer `%s' was terminated\n", ps, GNUNET_i2s(&pc->identity));
655 ps->recv_active = GNUNET_NO;
656 ps->recv_connected = GNUNET_NO;
657 ps->recv_endpoint = NULL;
659 if (connection==ps->send_endpoint)
662 ps->send_active = GNUNET_NO;
663 ps->send_connected = GNUNET_NO;
664 ps->send_endpoint = NULL;
665 #if DEBUG_CONNECTIONS
666 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: outbound connection from peer `%s' was terminated\n", ps, GNUNET_i2s(&pc->identity));
670 /* if both connections disconnected, remove session */
671 if ((ps->send_connected == GNUNET_NO) && (ps->recv_connected == GNUNET_NO))
673 GNUNET_STATISTICS_update (pc->plugin->env->stats,
674 gettext_noop ("# HTTP inbound sessions for peers active"),
677 remove_session(pc,ps,GNUNET_YES,GNUNET_SYSERR);
681 static void mhd_write_mst_cb (void *cls,
683 const struct GNUNET_MessageHeader *message)
686 struct Session *ps = cls;
687 struct HTTP_PeerContext *pc = ps->peercontext;
688 GNUNET_assert(ps != NULL);
689 GNUNET_assert(pc != NULL);
691 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
692 "Connection %X: Forwarding message to transport service, type %u and size %u from `%s' (`%s')\n",
694 ntohs(message->type),
695 ntohs(message->size),
696 GNUNET_i2s(&(ps->peercontext)->identity),http_plugin_address_to_string(NULL,ps->addr,ps->addrlen));
698 pc->plugin->env->receive (ps->peercontext->plugin->env->cls,
706 * Check if ip is allowed to connect.
709 mhd_accept_cb (void *cls,
710 const struct sockaddr *addr, socklen_t addr_len)
713 struct Plugin *plugin = cls;
715 /* Every connection is accepted, nothing more to do here */
719 int mhd_send_callback (void *cls, uint64_t pos, char *buf, int max)
723 struct Session * ps = cls;
724 struct HTTP_PeerContext * pc;
725 struct HTTP_Message * msg;
727 GNUNET_assert (ps!=NULL);
728 pc = ps->peercontext;
729 msg = ps->pending_msgs_tail;
730 if (ps->send_force_disconnect==GNUNET_YES)
732 #if DEBUG_CONNECTIONS
733 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: outbound forced to disconnect\n",ps);
740 if ((msg->size-msg->pos) <= max)
742 memcpy(buf,&msg->buf[msg->pos],(msg->size-msg->pos));
743 bytes_read = msg->size-msg->pos;
744 msg->pos+=(msg->size-msg->pos);
748 memcpy(buf,&msg->buf[msg->pos],max);
753 if (msg->pos==msg->size)
755 if (NULL!=msg->transmit_cont)
756 msg->transmit_cont (msg->transmit_cont_cls,&pc->identity,GNUNET_OK);
757 remove_http_message(ps,msg);
764 * Process GET or PUT request received via MHD. For
765 * GET, queue response that will send back our pending
766 * messages. For PUT, process incoming data and send
767 * to GNUnet core. In either case, check if a session
768 * already exists and create a new one if not.
771 mdh_access_cb (void *cls,
772 struct MHD_Connection *mhd_connection,
776 const char *upload_data,
777 size_t * upload_data_size, void **httpSessionCache)
779 struct Plugin *plugin = cls;
780 struct MHD_Response *response;
781 const union MHD_ConnectionInfo * conn_info;
783 struct sockaddr_in *addrin;
784 struct sockaddr_in6 *addrin6;
786 char address[INET6_ADDRSTRLEN+14];
787 struct GNUNET_PeerIdentity pi_in;
790 struct IPv4HttpAddress ipv4addr;
791 struct IPv6HttpAddress ipv6addr;
793 struct HTTP_PeerContext *pc;
794 struct Session *ps = NULL;
795 struct Session *ps_tmp = NULL;
798 int send_error_to_client;
802 GNUNET_assert(cls !=NULL);
803 send_error_to_client = GNUNET_NO;
805 if (NULL == *httpSessionCache)
807 /* check url for peer identity , if invalid send HTTP 404*/
808 size_t len = strlen(&url[1]);
809 char * peer = GNUNET_malloc(104+1);
811 if ((len>104) && (url[104]==';'))
813 char * id = GNUNET_malloc((len-104)+1);
814 strcpy(id,&url[105]);
815 memcpy(peer,&url[1],103);
817 id_num = strtoul ( id, NULL , 10);
820 res = GNUNET_CRYPTO_hash_from_string (peer, &(pi_in.hashPubKey));
822 if ( GNUNET_SYSERR == res )
824 response = MHD_create_response_from_data (strlen (HTTP_ERROR_RESPONSE),HTTP_ERROR_RESPONSE, MHD_NO, MHD_NO);
825 res = MHD_queue_response (mhd_connection, MHD_HTTP_NOT_FOUND, response);
826 MHD_destroy_response (response);
827 #if DEBUG_CONNECTIONS
829 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Peer has no valid ident, sent HTTP 1.1/404\n");
831 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Peer has no valid ident, could not send error\n");
838 ps = *httpSessionCache;
839 pc = ps->peercontext;
842 if (NULL == *httpSessionCache)
844 /* get peer context */
845 pc = GNUNET_CONTAINER_multihashmap_get (plugin->peers, &pi_in.hashPubKey);
849 pc = GNUNET_malloc(sizeof (struct HTTP_PeerContext));
851 pc->session_id_counter=1;
852 pc->last_session = NULL;
853 memcpy(&pc->identity, &pi_in, sizeof(struct GNUNET_PeerIdentity));
854 GNUNET_CONTAINER_multihashmap_put(plugin->peers, &pc->identity.hashPubKey, pc, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
855 GNUNET_STATISTICS_update (plugin->env->stats,
856 gettext_noop ("# HTTP peers active"),
861 conn_info = MHD_get_connection_info(mhd_connection, MHD_CONNECTION_INFO_CLIENT_ADDRESS );
862 /* Incoming IPv4 connection */
863 if ( AF_INET == conn_info->client_addr->sin_family)
865 addrin = conn_info->client_addr;
866 inet_ntop(addrin->sin_family, &(addrin->sin_addr),address,INET_ADDRSTRLEN);
867 memcpy(&ipv4addr.ipv4_addr,&(addrin->sin_addr),sizeof(struct in_addr));
868 ipv4addr.u_port = addrin->sin_port;
870 addr_len = sizeof(struct IPv4HttpAddress);
872 /* Incoming IPv6 connection */
873 if ( AF_INET6 == conn_info->client_addr->sin_family)
875 addrin6 = (struct sockaddr_in6 *) conn_info->client_addr;
876 inet_ntop(addrin6->sin6_family, &(addrin6->sin6_addr),address,INET6_ADDRSTRLEN);
877 memcpy(&ipv6addr.ipv6_addr,&(addrin6->sin6_addr),sizeof(struct in6_addr));
878 ipv6addr.u6_port = addrin6->sin6_port;
880 addr_len = sizeof(struct IPv6HttpAddress);
884 /* only inbound sessions here */
889 if ((ps_tmp->direction==INBOUND) && (ps_tmp->session_id == id_num) && (id_num!=0))
891 if ((ps_tmp->recv_force_disconnect!=GNUNET_YES) && (ps_tmp->send_force_disconnect!=GNUNET_YES))
900 ps = GNUNET_malloc(sizeof (struct Session));
901 ps->addr = GNUNET_malloc(addr_len);
902 memcpy(ps->addr,addr,addr_len);
903 ps->addrlen = addr_len;
904 ps->direction=INBOUND;
905 ps->pending_msgs_head = NULL;
906 ps->pending_msgs_tail = NULL;
907 ps->send_connected=GNUNET_NO;
908 ps->send_active=GNUNET_NO;
909 ps->recv_connected=GNUNET_NO;
910 ps->recv_active=GNUNET_NO;
912 ps->session_id =id_num;
913 ps->url = create_url (plugin, ps->addr, ps->addrlen, ps->session_id);
914 GNUNET_CONTAINER_DLL_insert(pc->head,pc->tail,ps);
915 GNUNET_STATISTICS_update (plugin->env->stats,
916 gettext_noop ("# HTTP inbound sessions for peers active"),
921 *httpSessionCache = ps;
922 if (ps->msgtok==NULL)
923 ps->msgtok = GNUNET_SERVER_mst_create (&mhd_write_mst_cb, ps);
925 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: HTTP Daemon has new an incoming `%s' request from peer `%s' (`%s')\n",
928 GNUNET_i2s(&pc->identity),
929 http_plugin_address_to_string(NULL, ps->addr, ps->addrlen));
933 /* Is it a PUT or a GET request */
934 if (0 == strcmp (MHD_HTTP_METHOD_PUT, method))
936 if (ps->recv_force_disconnect == GNUNET_YES)
938 #if DEBUG_CONNECTIONS
939 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: inbound connection was forced to disconnect\n",ps);
941 ps->recv_active = GNUNET_NO;
944 if ((*upload_data_size == 0) && (ps->recv_active==GNUNET_NO))
946 ps->recv_endpoint = mhd_connection;
947 ps->recv_connected = GNUNET_YES;
948 ps->recv_active = GNUNET_YES;
949 ps->recv_force_disconnect = GNUNET_NO;
950 #if DEBUG_CONNECTIONS
951 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: inbound PUT connection connected\n",ps);
956 /* Transmission of all data complete */
957 if ((*upload_data_size == 0) && (ps->recv_active == GNUNET_YES))
959 response = MHD_create_response_from_data (strlen (HTTP_PUT_RESPONSE),HTTP_PUT_RESPONSE, MHD_NO, MHD_NO);
960 res = MHD_queue_response (mhd_connection, MHD_HTTP_OK, response);
961 #if DEBUG_CONNECTIONS
962 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: Sent HTTP/1.1: 200 OK as PUT Response\n",ps);
964 MHD_destroy_response (response);
965 ps->recv_active=GNUNET_NO;
970 if ((*upload_data_size > 0) && (ps->recv_active == GNUNET_YES))
972 res = GNUNET_SERVER_mst_receive(ps->msgtok, ps, upload_data,*upload_data_size, GNUNET_NO, GNUNET_NO);
973 (*upload_data_size) = 0;
979 if ( 0 == strcmp (MHD_HTTP_METHOD_GET, method) )
981 if (ps->send_force_disconnect == GNUNET_YES)
983 #if DEBUG_CONNECTIONS
984 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: outbound connection was forced to disconnect\n",ps);
986 ps->send_active = GNUNET_NO;
989 ps->send_connected = GNUNET_YES;
990 ps->send_active = GNUNET_YES;
991 ps->send_endpoint = mhd_connection;
992 ps->send_force_disconnect = GNUNET_NO;
993 #if DEBUG_CONNECTIONS
994 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: inbound GET connection connected\n",ps);
996 response = MHD_create_response_from_callback(-1,32 * 1024, &mhd_send_callback, ps, NULL);
997 res = MHD_queue_response (mhd_connection, MHD_HTTP_OK, response);
998 MHD_destroy_response (response);
1005 * Function that queries MHD's select sets and
1006 * starts the task waiting for them.
1008 static GNUNET_SCHEDULER_TaskIdentifier
1009 http_server_daemon_prepare (void * cls, struct MHD_Daemon *daemon_handle)
1011 struct Plugin *plugin = cls;
1012 GNUNET_SCHEDULER_TaskIdentifier ret;
1016 struct GNUNET_NETWORK_FDSet *wrs;
1017 struct GNUNET_NETWORK_FDSet *wws;
1018 struct GNUNET_NETWORK_FDSet *wes;
1020 unsigned long long timeout;
1022 struct GNUNET_TIME_Relative tv;
1024 GNUNET_assert(cls !=NULL);
1025 ret = GNUNET_SCHEDULER_NO_TASK;
1029 wrs = GNUNET_NETWORK_fdset_create ();
1030 wes = GNUNET_NETWORK_fdset_create ();
1031 wws = GNUNET_NETWORK_fdset_create ();
1033 GNUNET_assert (MHD_YES ==
1034 MHD_get_fdset (daemon_handle,
1039 haveto = MHD_get_timeout (daemon_handle, &timeout);
1040 if (haveto == MHD_YES)
1041 tv.value = (uint64_t) timeout;
1043 tv = GNUNET_TIME_UNIT_FOREVER_REL;
1044 GNUNET_NETWORK_fdset_copy_native (wrs, &rs, max);
1045 GNUNET_NETWORK_fdset_copy_native (wws, &ws, max);
1046 GNUNET_NETWORK_fdset_copy_native (wes, &es, max);
1047 if (daemon_handle == plugin->http_server_daemon_v4)
1049 if (plugin->http_server_task_v4 != GNUNET_SCHEDULER_NO_TASK)
1051 GNUNET_SCHEDULER_cancel(plugin->env->sched, plugin->http_server_task_v4);
1052 plugin->http_server_daemon_v4 = GNUNET_SCHEDULER_NO_TASK;
1055 ret = GNUNET_SCHEDULER_add_select (plugin->env->sched,
1056 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1057 GNUNET_SCHEDULER_NO_TASK,
1061 &http_server_daemon_v4_run,
1064 if (daemon_handle == plugin->http_server_daemon_v6)
1066 if (plugin->http_server_task_v6 != GNUNET_SCHEDULER_NO_TASK)
1068 GNUNET_SCHEDULER_cancel(plugin->env->sched, plugin->http_server_task_v6);
1069 plugin->http_server_task_v6 = GNUNET_SCHEDULER_NO_TASK;
1072 ret = GNUNET_SCHEDULER_add_select (plugin->env->sched,
1073 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1074 GNUNET_SCHEDULER_NO_TASK,
1078 &http_server_daemon_v6_run,
1081 GNUNET_NETWORK_fdset_destroy (wrs);
1082 GNUNET_NETWORK_fdset_destroy (wws);
1083 GNUNET_NETWORK_fdset_destroy (wes);
1088 * Call MHD to process pending requests and then go back
1089 * and schedule the next run.
1091 static void http_server_daemon_v4_run (void *cls,
1092 const struct GNUNET_SCHEDULER_TaskContext *tc)
1094 struct Plugin *plugin = cls;
1096 GNUNET_assert(cls !=NULL);
1097 plugin->http_server_task_v4 = GNUNET_SCHEDULER_NO_TASK;
1099 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1102 GNUNET_assert (MHD_YES == MHD_run (plugin->http_server_daemon_v4));
1103 plugin->http_server_task_v4 = http_server_daemon_prepare (plugin, plugin->http_server_daemon_v4);
1109 * Call MHD to process pending requests and then go back
1110 * and schedule the next run.
1112 static void http_server_daemon_v6_run (void *cls,
1113 const struct GNUNET_SCHEDULER_TaskContext *tc)
1115 struct Plugin *plugin = cls;
1117 GNUNET_assert(cls !=NULL);
1118 plugin->http_server_task_v6 = GNUNET_SCHEDULER_NO_TASK;
1120 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1123 GNUNET_assert (MHD_YES == MHD_run (plugin->http_server_daemon_v6));
1124 plugin->http_server_task_v6 = http_server_daemon_prepare (plugin, plugin->http_server_daemon_v6);
1128 static size_t curl_get_header_cb( void *ptr, size_t size, size_t nmemb, void *stream)
1130 struct Session * ps = stream;
1132 size_t len = size * nmemb;
1133 long http_result = 0;
1135 /* Getting last http result code */
1136 if (ps->recv_connected==GNUNET_NO)
1138 GNUNET_assert(NULL!=ps);
1139 res = curl_easy_getinfo(ps->recv_endpoint, CURLINFO_RESPONSE_CODE, &http_result);
1140 if (CURLE_OK == res)
1142 if (http_result == 200)
1144 ps->recv_connected = GNUNET_YES;
1145 ps->recv_active = GNUNET_YES;
1146 #if DEBUG_CONNECTIONS
1147 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: connected to recieve data\n",ps);
1149 // Calling send_check_connections again since receive is established
1150 send_check_connections (ps->peercontext->plugin, ps);
1156 if ((size * nmemb) < SIZE_MAX)
1157 tmp = GNUNET_malloc (len+1);
1159 if ((tmp != NULL) && (len > 0))
1161 memcpy(tmp,ptr,len);
1164 if (tmp[len-2] == 13)
1167 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: Header: %s\n",ps,tmp);
1172 return size * nmemb;
1175 static size_t curl_put_header_cb( void *ptr, size_t size, size_t nmemb, void *stream)
1177 struct Session * ps = stream;
1180 size_t len = size * nmemb;
1181 long http_result = 0;
1184 /* Getting last http result code */
1185 GNUNET_assert(NULL!=ps);
1186 res = curl_easy_getinfo(ps->send_endpoint, CURLINFO_RESPONSE_CODE, &http_result);
1187 if (CURLE_OK == res)
1189 if ((http_result == 100) && (ps->send_connected==GNUNET_NO))
1191 ps->send_connected = GNUNET_YES;
1192 ps->send_active = GNUNET_YES;
1193 #if DEBUG_CONNECTIONS
1194 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: connected to send data\n",ps);
1197 if ((http_result == 200) && (ps->send_connected==GNUNET_YES))
1199 ps->send_connected = GNUNET_NO;
1200 ps->send_active = GNUNET_NO;
1201 #if DEBUG_CONNECTIONS
1202 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: sending disconnected\n",ps);
1208 if ((size * nmemb) < SIZE_MAX)
1209 tmp = GNUNET_malloc (len+1);
1211 if ((tmp != NULL) && (len > 0))
1213 memcpy(tmp,ptr,len);
1216 if (tmp[len-2] == 13)
1223 return size * nmemb;
1227 * Callback method used with libcurl
1228 * Method is called when libcurl needs to read data during sending
1229 * @param stream pointer where to write data
1230 * @param size size of an individual element
1231 * @param nmemb count of elements that can be written to the buffer
1232 * @param ptr source pointer, passed to the libcurl handle
1233 * @return bytes written to stream
1235 static size_t curl_send_cb(void *stream, size_t size, size_t nmemb, void *ptr)
1237 struct Session * ps = ptr;
1238 struct HTTP_Message * msg = ps->pending_msgs_tail;
1242 if (ps->send_active == GNUNET_NO)
1243 return CURL_READFUNC_PAUSE;
1246 if ((ps->pending_msgs_tail == NULL) && (ps->send_active == GNUNET_YES))
1248 #if DEBUG_CONNECTIONS
1249 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: No Message to send, pausing connection\n",ps);
1251 ps->send_active = GNUNET_NO;
1252 return CURL_READFUNC_PAUSE;
1255 msg = ps->pending_msgs_tail;
1257 if (msg->pos < msg->size)
1259 /* data fit in buffer */
1260 if ((msg->size - msg->pos) <= (size * nmemb))
1262 len = (msg->size - msg->pos);
1263 memcpy(stream, &msg->buf[msg->pos], len);
1270 memcpy(stream, &msg->buf[msg->pos], len);
1275 /* no data to send */
1281 if ( msg->pos == msg->size)
1283 #if DEBUG_CONNECTIONS
1284 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: Message with %u bytes sent, removing message from queue \n",ps, msg->pos);
1286 /* Calling transmit continuation */
1287 if (( NULL != ps->pending_msgs_tail) && (NULL != ps->pending_msgs_tail->transmit_cont))
1288 msg->transmit_cont (ps->pending_msgs_tail->transmit_cont_cls,&(ps->peercontext)->identity,GNUNET_OK);
1289 remove_http_message(ps, msg);
1294 static void curl_receive_mst_cb (void *cls,
1296 const struct GNUNET_MessageHeader *message)
1298 struct Session *ps = cls;
1299 struct HTTP_PeerContext *pc = ps->peercontext;
1300 GNUNET_assert(ps != NULL);
1301 GNUNET_assert(pc != NULL);
1303 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1304 "Connection %X: Forwarding message to transport service, type %u and size %u from `%s' (`%s')\n",
1306 ntohs(message->type),
1307 ntohs(message->size),
1308 GNUNET_i2s(&(pc->identity)),http_plugin_address_to_string(NULL,ps->addr,ps->addrlen));
1310 pc->plugin->env->receive (pc->plugin->env->cls,
1319 * Callback method used with libcurl
1320 * Method is called when libcurl needs to write data during sending
1321 * @param stream pointer where to write data
1322 * @param size size of an individual element
1323 * @param nmemb count of elements that can be written to the buffer
1324 * @param ptr destination pointer, passed to the libcurl handle
1325 * @return bytes read from stream
1327 static size_t curl_receive_cb( void *stream, size_t size, size_t nmemb, void *ptr)
1329 struct Session * ps = ptr;
1330 #if DEBUG_CONNECTIONS
1331 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: %u bytes received\n",ps, size*nmemb);
1333 GNUNET_SERVER_mst_receive(ps->msgtok, ps, stream, size*nmemb, GNUNET_NO, GNUNET_NO);
1334 return (size * nmemb);
1338 static void curl_perform (void *cls,
1339 const struct GNUNET_SCHEDULER_TaskContext *tc)
1341 struct Plugin *plugin = cls;
1342 static unsigned int handles_last_run;
1344 struct CURLMsg *msg;
1346 struct Session *ps = NULL;
1347 struct HTTP_PeerContext *pc = NULL;
1348 struct HTTP_Message * cur_msg = NULL;
1352 GNUNET_assert(cls !=NULL);
1354 plugin->http_curl_task = GNUNET_SCHEDULER_NO_TASK;
1355 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1361 mret = curl_multi_perform (plugin->multi_handle, &running);
1362 if ((running < handles_last_run) && (running>0))
1367 msg = curl_multi_info_read (plugin->multi_handle, &running);
1370 /* get session for affected curl handle */
1371 GNUNET_assert ( msg->easy_handle != NULL );
1372 curl_easy_getinfo(msg->easy_handle, CURLINFO_PRIVATE, &tmp);
1373 ps = (struct Session *) tmp;
1374 GNUNET_assert ( ps != NULL );
1375 pc = ps->peercontext;
1376 GNUNET_assert ( pc != NULL );
1381 if ( (msg->data.result != CURLE_OK) &&
1382 (msg->data.result != CURLE_GOT_NOTHING) )
1384 /* sending msg failed*/
1385 if (msg->easy_handle == ps->send_endpoint)
1387 #if DEBUG_CONNECTIONS
1388 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1389 _("Connection %X: HTTP PUT to peer `%s' (`%s') failed: `%s' `%s'\n"),
1391 GNUNET_i2s(&pc->identity),
1392 http_plugin_address_to_string(NULL, ps->addr, ps->addrlen),
1393 "curl_multi_perform",
1394 curl_easy_strerror (msg->data.result));
1396 ps->send_connected = GNUNET_NO;
1397 ps->send_active = GNUNET_NO;
1398 curl_multi_remove_handle(plugin->multi_handle,ps->send_endpoint);
1399 //curl_easy_cleanup(ps->send_endpoint);
1400 //ps->send_endpoint=NULL;
1401 cur_msg = ps->pending_msgs_tail;
1402 if (( NULL != cur_msg) && ( NULL != cur_msg->transmit_cont))
1403 cur_msg->transmit_cont (cur_msg->transmit_cont_cls,&pc->identity,GNUNET_SYSERR);
1405 /* GET connection failed */
1406 if (msg->easy_handle == ps->recv_endpoint)
1408 #if DEBUG_CONNECTIONS
1409 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1410 _("Connection %X: HTTP GET to peer `%s' (`%s') failed: `%s' `%s'\n"),
1412 GNUNET_i2s(&pc->identity),
1413 http_plugin_address_to_string(NULL, ps->addr, ps->addrlen),
1414 "curl_multi_perform",
1415 curl_easy_strerror (msg->data.result));
1417 ps->recv_connected = GNUNET_NO;
1418 ps->recv_active = GNUNET_NO;
1419 curl_multi_remove_handle(plugin->multi_handle,ps->recv_endpoint);
1420 //curl_easy_cleanup(ps->recv_endpoint);
1421 //ps->recv_endpoint=NULL;
1426 if (msg->easy_handle == ps->send_endpoint)
1428 GNUNET_assert (CURLE_OK == curl_easy_getinfo(msg->easy_handle, CURLINFO_RESPONSE_CODE, &http_result));
1429 #if DEBUG_CONNECTIONS
1430 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1431 "Connection %X: HTTP PUT connection to peer `%s' (`%s') was closed with HTTP code %u\n",
1433 GNUNET_i2s(&pc->identity),
1434 http_plugin_address_to_string(NULL, ps->addr, ps->addrlen),
1437 /* Calling transmit continuation */
1438 cur_msg = ps->pending_msgs_tail;
1439 if (( NULL != cur_msg) && (NULL != cur_msg->transmit_cont))
1441 /* HTTP 1xx : Last message before here was informational */
1442 if ((http_result >=100) && (http_result < 200))
1443 cur_msg->transmit_cont (cur_msg->transmit_cont_cls,&pc->identity,GNUNET_OK);
1444 /* HTTP 2xx: successful operations */
1445 if ((http_result >=200) && (http_result < 300))
1446 cur_msg->transmit_cont (cur_msg->transmit_cont_cls,&pc->identity,GNUNET_OK);
1447 /* HTTP 3xx..5xx: error */
1448 if ((http_result >=300) && (http_result < 600))
1449 cur_msg->transmit_cont (cur_msg->transmit_cont_cls,&pc->identity,GNUNET_SYSERR);
1451 ps->send_connected = GNUNET_NO;
1452 ps->send_active = GNUNET_NO;
1453 curl_multi_remove_handle(plugin->multi_handle,ps->send_endpoint);
1454 //curl_easy_cleanup(ps->send_endpoint);
1455 //ps->send_endpoint =NULL;
1457 if (msg->easy_handle == ps->recv_endpoint)
1459 #if DEBUG_CONNECTIONS
1460 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1461 "Connection %X: HTTP GET connection to peer `%s' (`%s') was closed with HTTP code %u\n",
1463 GNUNET_i2s(&pc->identity),
1464 http_plugin_address_to_string(NULL, ps->addr, ps->addrlen),
1467 ps->recv_connected = GNUNET_NO;
1468 ps->recv_active = GNUNET_NO;
1469 curl_multi_remove_handle(plugin->multi_handle,ps->recv_endpoint);
1470 //curl_easy_cleanup(ps->recv_endpoint);
1471 //ps->recv_endpoint=NULL;
1474 if ((ps->recv_connected == GNUNET_NO) && (ps->send_connected == GNUNET_NO))
1475 remove_session (pc, ps, GNUNET_YES, GNUNET_SYSERR);
1482 while ( (running > 0) );
1484 handles_last_run = running;
1486 while (mret == CURLM_CALL_MULTI_PERFORM);
1487 curl_schedule(plugin);
1492 * Function setting up file descriptors and scheduling task to run
1493 * @param ses session to send data to
1494 * @return GNUNET_SYSERR for hard failure, GNUNET_OK for ok
1496 static int curl_schedule(void *cls)
1498 struct Plugin *plugin = cls;
1503 struct GNUNET_NETWORK_FDSet *grs;
1504 struct GNUNET_NETWORK_FDSet *gws;
1508 GNUNET_assert(cls !=NULL);
1510 /* Cancel previous scheduled task */
1511 if (plugin->http_curl_task != GNUNET_SCHEDULER_NO_TASK)
1513 GNUNET_SCHEDULER_cancel(plugin->env->sched, plugin->http_curl_task);
1514 plugin->http_curl_task = GNUNET_SCHEDULER_NO_TASK;
1520 mret = curl_multi_fdset (plugin->multi_handle, &rs, &ws, &es, &max);
1521 if (mret != CURLM_OK)
1523 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1524 _("%s failed at %s:%d: `%s'\n"),
1525 "curl_multi_fdset", __FILE__, __LINE__,
1526 curl_multi_strerror (mret));
1527 return GNUNET_SYSERR;
1529 mret = curl_multi_timeout (plugin->multi_handle, &to);
1530 if (mret != CURLM_OK)
1532 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1533 _("%s failed at %s:%d: `%s'\n"),
1534 "curl_multi_timeout", __FILE__, __LINE__,
1535 curl_multi_strerror (mret));
1536 return GNUNET_SYSERR;
1539 grs = GNUNET_NETWORK_fdset_create ();
1540 gws = GNUNET_NETWORK_fdset_create ();
1541 GNUNET_NETWORK_fdset_copy_native (grs, &rs, max + 1);
1542 GNUNET_NETWORK_fdset_copy_native (gws, &ws, max + 1);
1543 plugin->http_curl_task = GNUNET_SCHEDULER_add_select (plugin->env->sched,
1544 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1545 GNUNET_SCHEDULER_NO_TASK,
1546 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 0),
1551 GNUNET_NETWORK_fdset_destroy (gws);
1552 GNUNET_NETWORK_fdset_destroy (grs);
1557 * Function setting up curl handle and selecting message to send
1559 * @param ses session to send data to
1560 * @param con connection
1561 * @return GNUNET_SYSERR on failure, GNUNET_NO if connecting, GNUNET_YES if ok
1563 static ssize_t send_check_connections (void *cls, struct Session *ps)
1565 struct Plugin *plugin = cls;
1567 struct HTTP_Message * msg;
1569 struct GNUNET_TIME_Relative timeout = GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT;
1571 GNUNET_assert(cls !=NULL);
1573 if (ps->direction == OUTBOUND)
1575 /* RECV DIRECTION */
1576 /* Check if session is connected to receive data, otherwise connect to peer */
1577 if (ps->recv_connected == GNUNET_NO)
1579 int fresh = GNUNET_NO;
1580 if (ps->recv_endpoint == NULL)
1582 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1583 "created handle\n");
1585 ps->recv_endpoint = curl_easy_init();
1588 curl_easy_setopt(ps->recv_endpoint, CURLOPT_VERBOSE, 1L);
1590 curl_easy_setopt(ps->recv_endpoint, CURLOPT_URL, ps->url);
1591 curl_easy_setopt(ps->recv_endpoint, CURLOPT_HEADERFUNCTION, &curl_get_header_cb);
1592 curl_easy_setopt(ps->recv_endpoint, CURLOPT_WRITEHEADER, ps);
1593 curl_easy_setopt(ps->recv_endpoint, CURLOPT_READFUNCTION, curl_send_cb);
1594 curl_easy_setopt(ps->recv_endpoint, CURLOPT_READDATA, ps);
1595 curl_easy_setopt(ps->recv_endpoint, CURLOPT_WRITEFUNCTION, curl_receive_cb);
1596 curl_easy_setopt(ps->recv_endpoint, CURLOPT_WRITEDATA, ps);
1597 curl_easy_setopt(ps->recv_endpoint, CURLOPT_TIMEOUT, (long) timeout.value);
1598 curl_easy_setopt(ps->recv_endpoint, CURLOPT_PRIVATE, ps);
1599 curl_easy_setopt(ps->recv_endpoint, CURLOPT_CONNECTTIMEOUT, HTTP_CONNECT_TIMEOUT);
1600 curl_easy_setopt(ps->recv_endpoint, CURLOPT_BUFFERSIZE, GNUNET_SERVER_MAX_MESSAGE_SIZE);
1602 if (fresh==GNUNET_YES)
1604 mret = curl_multi_add_handle(plugin->multi_handle, ps->recv_endpoint);
1605 if (mret != CURLM_OK)
1607 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1608 _("Connection: %X: %s failed at %s:%d: `%s'\n"),
1610 "curl_multi_add_handle", __FILE__, __LINE__,
1611 curl_multi_strerror (mret));
1612 return GNUNET_SYSERR;
1615 if (curl_schedule (plugin) == GNUNET_SYSERR)
1617 #if DEBUG_CONNECTIONS
1618 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: could not schedule curl task\n",ps);
1620 return GNUNET_SYSERR;
1622 #if DEBUG_CONNECTIONS
1623 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: inbound not connected, initiating connection\n",ps);
1627 /* waiting for receive direction */
1628 if (ps->recv_connected==GNUNET_NO)
1631 /* SEND DIRECTION */
1632 /* Check if session is connected to send data, otherwise connect to peer */
1633 if ((ps->send_connected == GNUNET_YES) && (ps->send_endpoint!= NULL))
1635 if (ps->send_active == GNUNET_YES)
1637 #if DEBUG_CONNECTIONS
1638 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: outbound active, enqueueing message\n",ps);
1642 if (ps->send_active == GNUNET_NO)
1644 #if DEBUG_CONNECTIONS
1645 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: outbound paused, unpausing existing connection and enqueueing message\n",ps);
1647 if (CURLE_OK == curl_easy_pause(ps->send_endpoint,CURLPAUSE_CONT))
1649 ps->send_active=GNUNET_YES;
1653 return GNUNET_SYSERR;
1656 /* not connected, initiate connection */
1657 if (ps->send_connected==GNUNET_NO)
1659 int fresh = GNUNET_NO;
1660 if (NULL == ps->send_endpoint)
1662 ps->send_endpoint = curl_easy_init();
1665 GNUNET_assert (ps->send_endpoint != NULL);
1666 GNUNET_assert (NULL != ps->pending_msgs_tail);
1667 #if DEBUG_CONNECTIONS
1668 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: outbound not connected, initiating connection\n",ps);
1670 ps->send_active = GNUNET_NO;
1671 msg = ps->pending_msgs_tail;
1674 curl_easy_setopt(ps->send_endpoint, CURLOPT_VERBOSE, 1L);
1676 curl_easy_setopt(ps->send_endpoint, CURLOPT_URL, ps->url);
1677 curl_easy_setopt(ps->send_endpoint, CURLOPT_PUT, 1L);
1678 curl_easy_setopt(ps->send_endpoint, CURLOPT_HEADERFUNCTION, &curl_put_header_cb);
1679 curl_easy_setopt(ps->send_endpoint, CURLOPT_WRITEHEADER, ps);
1680 curl_easy_setopt(ps->send_endpoint, CURLOPT_READFUNCTION, curl_send_cb);
1681 curl_easy_setopt(ps->send_endpoint, CURLOPT_READDATA, ps);
1682 curl_easy_setopt(ps->send_endpoint, CURLOPT_WRITEFUNCTION, curl_receive_cb);
1683 curl_easy_setopt(ps->send_endpoint, CURLOPT_READDATA, ps);
1684 curl_easy_setopt(ps->send_endpoint, CURLOPT_TIMEOUT, (long) timeout.value);
1685 curl_easy_setopt(ps->send_endpoint, CURLOPT_PRIVATE, ps);
1686 curl_easy_setopt(ps->send_endpoint, CURLOPT_CONNECTTIMEOUT, HTTP_CONNECT_TIMEOUT);
1687 curl_easy_setopt(ps->send_endpoint, CURLOPT_BUFFERSIZE, GNUNET_SERVER_MAX_MESSAGE_SIZE);
1689 if (fresh==GNUNET_YES)
1691 mret = curl_multi_add_handle(plugin->multi_handle, ps->send_endpoint);
1692 if (mret != CURLM_OK)
1694 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1695 _("Connection: %X: %s failed at %s:%d: `%s'\n"),
1697 "curl_multi_add_handle", __FILE__, __LINE__,
1698 curl_multi_strerror (mret));
1699 return GNUNET_SYSERR;
1703 if (curl_schedule (plugin) == GNUNET_SYSERR)
1704 return GNUNET_SYSERR;
1707 if (ps->direction == INBOUND)
1709 GNUNET_assert (NULL != ps->pending_msgs_tail);
1710 if ((ps->recv_connected==GNUNET_YES) && (ps->send_connected==GNUNET_YES) &&
1711 (ps->recv_force_disconnect==GNUNET_NO) && (ps->recv_force_disconnect==GNUNET_NO))
1714 return GNUNET_SYSERR;
1717 static struct Session * send_select_session (void * cls, struct HTTP_PeerContext *pc, const void * addr, size_t addrlen, int force_address, struct Session * session)
1719 struct Session * tmp = NULL;
1720 int addr_given = GNUNET_NO;
1722 if ((addr!=NULL) && (addrlen>0))
1723 addr_given = GNUNET_YES;
1725 if (force_address == GNUNET_YES)
1727 /* check session given as argument */
1728 if ((session != NULL) && (addr_given == GNUNET_YES))
1730 if (0 == memcmp(session->addr, addr, addrlen))
1732 /* connection can not be used, since it is disconnected */
1733 if ((session->recv_force_disconnect==GNUNET_NO) && (session->send_force_disconnect==GNUNET_NO))
1735 #if DEBUG_SESSION_SELECTION
1736 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Session %X selected: Using session passed by transport to send to forced address \n", session);
1742 /* check last session used */
1743 if ((pc->last_session != NULL)&& (addr_given == GNUNET_YES))
1745 if (0 == memcmp(pc->last_session->addr, addr, addrlen))
1747 /* connection can not be used, since it is disconnected */
1748 if ((pc->last_session->recv_force_disconnect==GNUNET_NO) && (pc->last_session->send_force_disconnect==GNUNET_NO))
1750 #if DEBUG_SESSION_SELECTION
1751 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Session %X selected: Using last session used to send to forced address \n", pc->last_session);
1753 return pc->last_session;
1757 /* find session in existing sessions */
1759 while ((tmp!=NULL) && (addr_given == GNUNET_YES))
1762 if (0 == memcmp(tmp->addr, addr, addrlen))
1764 /* connection can not be used, since it is disconnected */
1765 if ((tmp->recv_force_disconnect==GNUNET_NO) && (tmp->send_force_disconnect==GNUNET_NO))
1767 #if DEBUG_SESSION_SELECTION
1768 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Session %X selected: Using existing session to send to forced address \n", session);
1776 /* no session to use */
1779 if ((force_address == GNUNET_NO) || (force_address == GNUNET_SYSERR))
1781 /* check session given as argument */
1782 if (session != NULL)
1784 /* connection can not be used, since it is disconnected */
1785 if ((session->recv_force_disconnect==GNUNET_NO) && (session->send_force_disconnect==GNUNET_NO))
1787 #if DEBUG_SESSION_SELECTION
1788 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Session %X selected: Using session passed by transport to send not-forced address \n", session);
1794 /* check last session used */
1795 if (pc->last_session != NULL)
1797 /* connection can not be used, since it is disconnected */
1798 if ((pc->last_session->recv_force_disconnect==GNUNET_NO) && (pc->last_session->send_force_disconnect==GNUNET_NO))
1800 #if DEBUG_SESSION_SELECTION
1801 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Session %X selected: Using last session to send to not-forced address \n", pc->last_session);
1803 return pc->last_session;
1806 /* find session in existing sessions */
1810 /* connection can not be used, since it is disconnected */
1811 if ((tmp->recv_force_disconnect==GNUNET_NO) && (tmp->send_force_disconnect==GNUNET_NO))
1813 #if DEBUG_SESSION_SELECTION
1814 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Session %X selected: Using existing session to send to not-forced address \n", tmp);
1826 * Function that can be used by the transport service to transmit
1827 * a message using the plugin. Note that in the case of a
1828 * peer disconnecting, the continuation MUST be called
1829 * prior to the disconnect notification itself. This function
1830 * will be called with this peer's HELLO message to initiate
1831 * a fresh connection to another peer.
1833 * @param cls closure
1834 * @param target who should receive this message
1835 * @param msgbuf the message to transmit
1836 * @param msgbuf_size number of bytes in 'msgbuf'
1837 * @param priority how important is the message (most plugins will
1838 * ignore message priority and just FIFO)
1839 * @param timeout how long to wait at most for the transmission (does not
1840 * require plugins to discard the message after the timeout,
1841 * just advisory for the desired delay; most plugins will ignore
1843 * @param session which session must be used (or NULL for "any")
1844 * @param addr the address to use (can be NULL if the plugin
1845 * is "on its own" (i.e. re-use existing TCP connection))
1846 * @param addrlen length of the address in bytes
1847 * @param force_address GNUNET_YES if the plugin MUST use the given address,
1848 * GNUNET_NO means the plugin may use any other address and
1849 * GNUNET_SYSERR means that only reliable existing
1850 * bi-directional connections should be used (regardless
1852 * @param cont continuation to call once the message has
1853 * been transmitted (or if the transport is ready
1854 * for the next transmission call; or if the
1855 * peer disconnected...); can be NULL
1856 * @param cont_cls closure for cont
1857 * @return number of bytes used (on the physical network, with overheads);
1858 * -1 on hard errors (i.e. address invalid); 0 is a legal value
1859 * and does NOT mean that the message was not transmitted (DV)
1862 http_plugin_send (void *cls,
1863 const struct GNUNET_PeerIdentity *target,
1866 unsigned int priority,
1867 struct GNUNET_TIME_Relative to,
1868 struct Session *session,
1872 GNUNET_TRANSPORT_TransmitContinuation cont,
1875 struct Plugin *plugin = cls;
1876 struct HTTP_Message *msg;
1877 struct HTTP_PeerContext * pc;
1878 struct Session * ps = NULL;
1880 GNUNET_assert(cls !=NULL);
1883 char * force = GNUNET_malloc(40);
1884 if (force_address == GNUNET_YES)
1885 strcpy(force,"forced addr.");
1886 if (force_address == GNUNET_NO)
1887 strcpy(force,"any addr.");
1888 if (force_address == GNUNET_SYSERR)
1889 strcpy(force,"reliable bi-direc. address addr.");
1891 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Transport tells me to send %u bytes to `%s' using %s (%s) and session: %X\n",
1895 http_plugin_address_to_string(NULL, addr, addrlen),
1901 pc = GNUNET_CONTAINER_multihashmap_get (plugin->peers, &target->hashPubKey);
1905 pc = GNUNET_malloc(sizeof (struct HTTP_PeerContext));
1906 pc->plugin = plugin;
1907 pc->session_id_counter=1;
1908 pc->last_session = NULL;
1909 memcpy(&pc->identity, target, sizeof(struct GNUNET_PeerIdentity));
1910 GNUNET_CONTAINER_multihashmap_put(plugin->peers, &pc->identity.hashPubKey, pc, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
1911 GNUNET_STATISTICS_update (plugin->env->stats,
1912 gettext_noop ("# HTTP peers active"),
1917 ps = send_select_session (plugin, pc, addr, addrlen, force_address, session);
1919 /* session not existing, but address forced -> creating new session */
1922 if ((addr!=NULL) && (addrlen!=0))
1924 ps = GNUNET_malloc(sizeof (struct Session));
1925 #if DEBUG_SESSION_SELECTION
1926 if (force_address == GNUNET_YES)
1927 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"No existing connection & forced address: creating new session %X to peer %s\n", ps, GNUNET_i2s(target));
1928 if (force_address != GNUNET_YES)
1929 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"No existing connection: creating new session %X to peer %s\n", ps, GNUNET_i2s(target));
1931 if ((addrlen!=0) && (addr!=NULL))
1933 ps->addr = GNUNET_malloc(addrlen);
1934 memcpy(ps->addr,addr,addrlen);
1935 ps->addrlen = addrlen;
1942 ps->direction=OUTBOUND;
1943 ps->recv_connected = GNUNET_NO;
1944 ps->recv_force_disconnect = GNUNET_NO;
1945 ps->send_connected = GNUNET_NO;
1946 ps->send_force_disconnect = GNUNET_NO;
1947 ps->pending_msgs_head = NULL;
1948 ps->pending_msgs_tail = NULL;
1950 ps->session_id = pc->session_id_counter;
1951 pc->session_id_counter++;
1952 ps->url = create_url (plugin, ps->addr, ps->addrlen, ps->session_id);
1953 if (ps->msgtok == NULL)
1954 ps->msgtok = GNUNET_SERVER_mst_create (&curl_receive_mst_cb, ps);
1955 GNUNET_CONTAINER_DLL_insert(pc->head,pc->tail,ps);
1958 GNUNET_STATISTICS_update (plugin->env->stats,
1959 gettext_noop ("# HTTP outbound sessions for peers active"),
1966 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));
1968 return GNUNET_SYSERR;
1973 msg = GNUNET_malloc (sizeof (struct HTTP_Message) + msgbuf_size);
1975 msg->size = msgbuf_size;
1977 msg->buf = (char *) &msg[1];
1978 msg->transmit_cont = cont;
1979 msg->transmit_cont_cls = cont_cls;
1980 memcpy (msg->buf,msgbuf, msgbuf_size);
1981 GNUNET_CONTAINER_DLL_insert(ps->pending_msgs_head,ps->pending_msgs_tail,msg);
1983 if (send_check_connections (plugin, ps) != GNUNET_SYSERR)
1985 if (force_address != GNUNET_YES)
1986 pc->last_session = ps;
1988 if (pc->last_session==NULL)
1989 pc->last_session = ps;
1993 return GNUNET_SYSERR;
1999 * Function that can be used to force the plugin to disconnect
2000 * from the given peer and cancel all previous transmissions
2001 * (and their continuationc).
2003 * @param cls closure
2004 * @param target peer from which to disconnect
2007 http_plugin_disconnect (void *cls,
2008 const struct GNUNET_PeerIdentity *target)
2012 struct Plugin *plugin = cls;
2013 struct HTTP_PeerContext *pc = NULL;
2014 struct Session *ps = NULL;
2015 //struct Session *tmp = NULL;
2017 pc = GNUNET_CONTAINER_multihashmap_get (plugin->peers, &target->hashPubKey);
2024 /* Telling transport that session is getting disconnected */
2025 plugin->env->session_end(plugin, target, ps);
2026 if (ps->direction==OUTBOUND)
2028 if (ps->send_endpoint!=NULL)
2030 //GNUNET_assert(CURLM_OK == curl_multi_remove_handle(plugin->multi_handle,ps->send_endpoint));
2031 //curl_easy_cleanup(ps->send_endpoint);
2032 //ps->send_endpoint=NULL;
2033 ps->send_force_disconnect = GNUNET_YES;
2035 if (ps->recv_endpoint!=NULL)
2037 //GNUNET_assert(CURLM_OK == curl_multi_remove_handle(plugin->multi_handle,ps->recv_endpoint));
2038 //curl_easy_cleanup(ps->recv_endpoint);
2039 //ps->recv_endpoint=NULL;
2040 ps->recv_force_disconnect = GNUNET_YES;
2044 if (ps->direction==INBOUND)
2046 ps->recv_force_disconnect = GNUNET_YES;
2047 ps->send_force_disconnect = GNUNET_YES;
2050 while (ps->pending_msgs_head!=NULL)
2052 remove_http_message(ps, ps->pending_msgs_head);
2054 ps->recv_active = GNUNET_NO;
2055 ps->send_active = GNUNET_NO;
2062 * Convert the transports address to a nice, human-readable
2065 * @param cls closure
2066 * @param type name of the transport that generated the address
2067 * @param addr one of the addresses of the host, NULL for the last address
2068 * the specific address format depends on the transport
2069 * @param addrlen length of the address
2070 * @param numeric should (IP) addresses be displayed in numeric form?
2071 * @param timeout after how long should we give up?
2072 * @param asc function to call on each string
2073 * @param asc_cls closure for asc
2076 http_plugin_address_pretty_printer (void *cls,
2081 struct GNUNET_TIME_Relative timeout,
2082 GNUNET_TRANSPORT_AddressStringCallback
2085 const struct IPv4HttpAddress *t4;
2086 const struct IPv6HttpAddress *t6;
2087 struct sockaddr_in a4;
2088 struct sockaddr_in6 a6;
2094 GNUNET_assert(cls !=NULL);
2095 if (addrlen == sizeof (struct IPv6HttpAddress))
2097 address = GNUNET_malloc (INET6_ADDRSTRLEN);
2099 a6.sin6_addr = t6->ipv6_addr;
2100 inet_ntop(AF_INET6, &(a6.sin6_addr),address,INET6_ADDRSTRLEN);
2101 port = ntohs(t6->u6_port);
2103 else if (addrlen == sizeof (struct IPv4HttpAddress))
2105 address = GNUNET_malloc (INET_ADDRSTRLEN);
2107 a4.sin_addr.s_addr = t4->ipv4_addr;
2108 inet_ntop(AF_INET, &(a4.sin_addr),address,INET_ADDRSTRLEN);
2109 port = ntohs(t4->u_port);
2113 /* invalid address */
2114 GNUNET_break_op (0);
2115 asc (asc_cls, NULL);
2118 res = GNUNET_asprintf(&ret,"http://%s:%u/",address,port);
2119 GNUNET_free (address);
2120 GNUNET_assert(res != 0);
2122 GNUNET_free_non_null (ret);
2128 * Another peer has suggested an address for this
2129 * peer and transport plugin. Check that this could be a valid
2130 * address. If so, consider adding it to the list
2133 * @param cls closure
2134 * @param addr pointer to the address
2135 * @param addrlen length of addr
2136 * @return GNUNET_OK if this is a plausible address for this peer
2140 http_plugin_address_suggested (void *cls,
2141 const void *addr, size_t addrlen)
2143 struct Plugin *plugin = cls;
2144 struct IPv4HttpAddress *v4;
2145 struct IPv6HttpAddress *v6;
2148 GNUNET_assert(cls !=NULL);
2149 if ((addrlen != sizeof (struct IPv4HttpAddress)) &&
2150 (addrlen != sizeof (struct IPv6HttpAddress)))
2152 return GNUNET_SYSERR;
2154 if (addrlen == sizeof (struct IPv4HttpAddress))
2156 v4 = (struct IPv4HttpAddress *) addr;
2157 /* Not skipping loopback
2158 if (INADDR_LOOPBACK == ntohl(v4->ipv4_addr))
2160 return GNUNET_SYSERR;
2162 port = ntohs (v4->u_port);
2163 if (port != plugin->port_inbound)
2165 return GNUNET_SYSERR;
2168 if (addrlen == sizeof (struct IPv6HttpAddress))
2170 v6 = (struct IPv6HttpAddress *) addr;
2171 if (IN6_IS_ADDR_LINKLOCAL (&v6->ipv6_addr))
2173 return GNUNET_SYSERR;
2175 port = ntohs (v6->u6_port);
2176 if (port != plugin->port_inbound)
2178 return GNUNET_SYSERR;
2187 * Function called for a quick conversion of the binary address to
2188 * a numeric address. Note that the caller must not free the
2189 * address and that the next call to this function is allowed
2190 * to override the address again.
2192 * @param cls closure
2193 * @param addr binary address
2194 * @param addrlen length of the address
2195 * @return string representing the same address
2198 http_plugin_address_to_string (void *cls,
2202 const struct IPv4HttpAddress *t4;
2203 const struct IPv6HttpAddress *t6;
2204 struct sockaddr_in a4;
2205 struct sockaddr_in6 a6;
2211 if (addrlen == sizeof (struct IPv6HttpAddress))
2213 address = GNUNET_malloc (INET6_ADDRSTRLEN);
2215 a6.sin6_addr = t6->ipv6_addr;
2216 inet_ntop(AF_INET6, &(a6.sin6_addr),address,INET6_ADDRSTRLEN);
2217 port = ntohs(t6->u6_port);
2219 else if (addrlen == sizeof (struct IPv4HttpAddress))
2221 address = GNUNET_malloc (INET_ADDRSTRLEN);
2223 a4.sin_addr.s_addr = t4->ipv4_addr;
2224 inet_ntop(AF_INET, &(a4.sin_addr),address,INET_ADDRSTRLEN);
2225 port = ntohs(t4->u_port);
2229 /* invalid address */
2232 res = GNUNET_asprintf(&ret,"%s:%u",address,port);
2233 GNUNET_free (address);
2234 GNUNET_assert(res != 0);
2240 * Exit point from the plugin.
2243 libgnunet_plugin_transport_http_done (void *cls)
2245 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
2246 struct Plugin *plugin = api->cls;
2248 GNUNET_assert(cls !=NULL);
2250 if (plugin->http_server_daemon_v4 != NULL)
2252 MHD_stop_daemon (plugin->http_server_daemon_v4);
2253 plugin->http_server_daemon_v4 = NULL;
2255 if (plugin->http_server_daemon_v6 != NULL)
2257 MHD_stop_daemon (plugin->http_server_daemon_v6);
2258 plugin->http_server_daemon_v6 = NULL;
2261 if ( plugin->http_server_task_v4 != GNUNET_SCHEDULER_NO_TASK)
2263 GNUNET_SCHEDULER_cancel(plugin->env->sched, plugin->http_server_task_v4);
2264 plugin->http_server_task_v4 = GNUNET_SCHEDULER_NO_TASK;
2267 if ( plugin->http_server_task_v6 != GNUNET_SCHEDULER_NO_TASK)
2269 GNUNET_SCHEDULER_cancel(plugin->env->sched, plugin->http_server_task_v6);
2270 plugin->http_server_task_v6 = GNUNET_SCHEDULER_NO_TASK;
2274 /* free all peer information */
2275 if (plugin->peers!=NULL)
2277 GNUNET_CONTAINER_multihashmap_iterate (plugin->peers,
2278 &remove_peer_context_Iterator,
2280 GNUNET_CONTAINER_multihashmap_destroy (plugin->peers);
2282 if (plugin->multi_handle!=NULL)
2284 mret = curl_multi_cleanup(plugin->multi_handle);
2286 if ( CURLM_OK != mret)
2287 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"curl multihandle clean up failed\n");
2289 plugin->multi_handle = NULL;
2291 curl_global_cleanup();
2293 if ( plugin->http_curl_task != GNUNET_SCHEDULER_NO_TASK)
2295 GNUNET_SCHEDULER_cancel(plugin->env->sched, plugin->http_curl_task);
2296 plugin->http_curl_task = GNUNET_SCHEDULER_NO_TASK;
2299 GNUNET_free_non_null (plugin->bind4_address);
2300 GNUNET_free_non_null (plugin->bind6_address);
2301 GNUNET_free_non_null(plugin->bind_hostname);
2302 GNUNET_free (plugin);
2305 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Unload http plugin complete...\n");
2312 * Entry point for the plugin.
2315 libgnunet_plugin_transport_http_init (void *cls)
2317 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
2318 struct Plugin *plugin;
2319 struct GNUNET_TRANSPORT_PluginFunctions *api;
2320 struct GNUNET_TIME_Relative gn_timeout;
2321 long long unsigned int port;
2323 GNUNET_assert(cls !=NULL);
2325 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Starting http plugin...\n");
2328 plugin = GNUNET_malloc (sizeof (struct Plugin));
2329 plugin->stats = env->stats;
2331 plugin->peers = NULL;
2332 plugin->bind4_address = NULL;
2333 plugin->use_ipv6 = GNUNET_YES;
2334 plugin->use_ipv4 = GNUNET_YES;
2336 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
2338 api->send = &http_plugin_send;
2339 api->disconnect = &http_plugin_disconnect;
2340 api->address_pretty_printer = &http_plugin_address_pretty_printer;
2341 api->check_address = &http_plugin_address_suggested;
2342 api->address_to_string = &http_plugin_address_to_string;
2344 /* Hashing our identity to use it in URLs */
2345 GNUNET_CRYPTO_hash_to_enc ( &(plugin->env->my_identity->hashPubKey), &plugin->my_ascii_hash_ident);
2347 /* Reading port number from config file */
2348 if (GNUNET_CONFIGURATION_have_value (env->cfg,
2349 "transport-http", "USE_IPv6"))
2351 plugin->use_ipv6 = GNUNET_CONFIGURATION_get_value_yesno (env->cfg,
2355 /* Reading port number from config file */
2356 if (GNUNET_CONFIGURATION_have_value (env->cfg,
2357 "transport-http", "USE_IPv4"))
2359 plugin->use_ipv4 = GNUNET_CONFIGURATION_get_value_yesno (env->cfg,
2363 /* Reading port number from config file */
2365 GNUNET_CONFIGURATION_get_value_number (env->cfg,
2371 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
2373 _("Require valid port number for transport plugin `%s' in configuration!\n"),
2375 libgnunet_plugin_transport_http_done (api);
2379 /* Reading ipv4 addresse to bind to from config file */
2380 if ((plugin->use_ipv4==GNUNET_YES) && (GNUNET_CONFIGURATION_have_value (env->cfg,
2381 "transport-http", "BINDTO4")))
2383 GNUNET_break (GNUNET_OK ==
2384 GNUNET_CONFIGURATION_get_value_string (env->cfg,
2387 &plugin->bind_hostname));
2388 plugin->bind4_address = GNUNET_malloc(sizeof(struct sockaddr_in));
2389 plugin->bind4_address->sin_family = AF_INET;
2390 plugin->bind4_address->sin_port = htons (port);
2392 if (inet_pton(AF_INET,plugin->bind_hostname, &plugin->bind4_address->sin_addr)<=0)
2394 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
2396 _("Misconfigured address to bind to in configuration!\n"),
2398 GNUNET_free(plugin->bind4_address);
2399 GNUNET_free(plugin->bind_hostname);
2400 plugin->bind_hostname = NULL;
2401 plugin->bind4_address = NULL;
2405 /* Reading ipv4 addresse to bind to from config file */
2406 if ((plugin->use_ipv6==GNUNET_YES) && (GNUNET_CONFIGURATION_have_value (env->cfg,
2407 "transport-http", "BINDTO6")))
2409 GNUNET_break (GNUNET_OK ==
2410 GNUNET_CONFIGURATION_get_value_string (env->cfg,
2413 &plugin->bind_hostname));
2415 plugin->bind6_address = GNUNET_malloc(sizeof(struct sockaddr_in6));
2416 plugin->bind6_address->sin6_family = AF_INET6;
2417 plugin->bind6_address->sin6_port = htons (port);
2419 if (inet_pton(AF_INET6,plugin->bind_hostname, &plugin->bind6_address->sin6_addr)<=0)
2421 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
2423 _("Misconfigured address to bind to in configuration!\n"),
2425 GNUNET_free(plugin->bind6_address);
2426 GNUNET_free(plugin->bind_hostname);
2427 plugin->bind_hostname = NULL;
2428 plugin->bind6_address = NULL;
2432 GNUNET_assert ((port > 0) && (port <= 65535));
2433 plugin->port_inbound = port;
2434 gn_timeout = GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT;
2435 unsigned int timeout = (gn_timeout.value) / 1000;
2436 if ((plugin->http_server_daemon_v6 == NULL) && (plugin->use_ipv6 == GNUNET_YES) && (port != 0))
2438 struct sockaddr * tmp = (struct sockaddr *) plugin->bind6_address;
2439 plugin->http_server_daemon_v6 = MHD_start_daemon (
2440 #if DEBUG_CONNECTIONS
2446 plugin , &mdh_access_cb, plugin,
2447 MHD_OPTION_SOCK_ADDR, tmp,
2448 MHD_OPTION_CONNECTION_LIMIT, (unsigned int) 32,
2449 //MHD_OPTION_PER_IP_CONNECTION_LIMIT, (unsigned int) 6,
2450 MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) timeout,
2451 MHD_OPTION_CONNECTION_MEMORY_LIMIT, (size_t) (16 * 1024),
2452 MHD_OPTION_NOTIFY_COMPLETED, &mhd_termination_cb, NULL,
2455 if ((plugin->http_server_daemon_v4 == NULL) && (plugin->use_ipv4 == GNUNET_YES) && (port != 0))
2457 plugin->http_server_daemon_v4 = MHD_start_daemon (
2458 #if DEBUG_CONNECTIONS
2464 plugin , &mdh_access_cb, plugin,
2465 MHD_OPTION_SOCK_ADDR, (struct sockaddr_in *)plugin->bind4_address,
2466 MHD_OPTION_CONNECTION_LIMIT, (unsigned int) 32,
2467 //MHD_OPTION_PER_IP_CONNECTION_LIMIT, (unsigned int) 6,
2468 MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) timeout,
2469 MHD_OPTION_CONNECTION_MEMORY_LIMIT, (size_t) (16 * 1024),
2470 MHD_OPTION_NOTIFY_COMPLETED, &mhd_termination_cb, NULL,
2473 if (plugin->http_server_daemon_v4 != NULL)
2474 plugin->http_server_task_v4 = http_server_daemon_prepare (plugin, plugin->http_server_daemon_v4);
2475 if (plugin->http_server_daemon_v6 != NULL)
2476 plugin->http_server_task_v6 = http_server_daemon_prepare (plugin, plugin->http_server_daemon_v6);
2479 if (plugin->http_server_task_v4 != GNUNET_SCHEDULER_NO_TASK)
2482 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);
2485 else if ((plugin->http_server_task_v6 != GNUNET_SCHEDULER_NO_TASK) && (plugin->http_server_task_v4 != GNUNET_SCHEDULER_NO_TASK))
2488 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);
2491 else if ((plugin->http_server_task_v6 != GNUNET_SCHEDULER_NO_TASK) && (plugin->http_server_task_v4 == GNUNET_SCHEDULER_NO_TASK))
2494 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);
2500 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"No MHD was started, transport plugin not functional!\n");
2502 libgnunet_plugin_transport_http_done (api);
2506 /* Initializing cURL */
2507 curl_global_init(CURL_GLOBAL_ALL);
2508 plugin->multi_handle = curl_multi_init();
2510 if ( NULL == plugin->multi_handle )
2512 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
2514 _("Could not initialize curl multi handle, failed to start http plugin!\n"),
2516 libgnunet_plugin_transport_http_done (api);
2520 plugin->peers = GNUNET_CONTAINER_multihashmap_create (10);
2521 GNUNET_OS_network_interfaces_list (&process_interfaces, plugin);
2526 /* end of plugin_transport_http.c */