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);
489 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 t6 = GNUNET_malloc(sizeof(struct IPv6HttpAddress));
612 if (IN6_IS_ADDR_LINKLOCAL (&((struct sockaddr_in6 *) addr)->sin6_addr))
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 struct HTTP_PeerContext *pc = ps->peercontext;
690 GNUNET_assert(ps != NULL);
691 GNUNET_assert(pc != NULL);
693 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
694 "Connection %X: Forwarding message to transport service, type %u and size %u from `%s' (`%s')\n",
696 ntohs(message->type),
697 ntohs(message->size),
698 GNUNET_i2s(&(ps->peercontext)->identity),http_plugin_address_to_string(NULL,ps->addr,ps->addrlen));
700 pc->plugin->env->receive (ps->peercontext->plugin->env->cls,
708 * Check if ip is allowed to connect.
711 mhd_accept_cb (void *cls,
712 const struct sockaddr *addr, socklen_t addr_len)
715 struct Plugin *plugin = cls;
717 /* Every connection is accepted, nothing more to do here */
721 int mhd_send_callback (void *cls, uint64_t pos, char *buf, int max)
725 struct Session * ps = cls;
726 struct HTTP_PeerContext * pc;
727 struct HTTP_Message * msg;
729 GNUNET_assert (ps!=NULL);
730 pc = ps->peercontext;
731 msg = ps->pending_msgs_tail;
732 if (ps->send_force_disconnect==GNUNET_YES)
734 #if DEBUG_CONNECTIONS
735 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: outbound forced to disconnect\n",ps);
742 if ((msg->size-msg->pos) <= max)
744 memcpy(buf,&msg->buf[msg->pos],(msg->size-msg->pos));
745 bytes_read = msg->size-msg->pos;
746 msg->pos+=(msg->size-msg->pos);
750 memcpy(buf,&msg->buf[msg->pos],max);
755 if (msg->pos==msg->size)
757 if (NULL!=msg->transmit_cont)
758 msg->transmit_cont (msg->transmit_cont_cls,&pc->identity,GNUNET_OK);
759 remove_http_message(ps,msg);
766 * Process GET or PUT request received via MHD. For
767 * GET, queue response that will send back our pending
768 * messages. For PUT, process incoming data and send
769 * to GNUnet core. In either case, check if a session
770 * already exists and create a new one if not.
773 mdh_access_cb (void *cls,
774 struct MHD_Connection *mhd_connection,
778 const char *upload_data,
779 size_t * upload_data_size, void **httpSessionCache)
781 struct Plugin *plugin = cls;
782 struct MHD_Response *response;
783 const union MHD_ConnectionInfo * conn_info;
785 struct sockaddr_in *addrin;
786 struct sockaddr_in6 *addrin6;
788 char address[INET6_ADDRSTRLEN+14];
789 struct GNUNET_PeerIdentity pi_in;
792 struct IPv4HttpAddress ipv4addr;
793 struct IPv6HttpAddress ipv6addr;
795 struct HTTP_PeerContext *pc;
796 struct Session *ps = NULL;
797 struct Session *ps_tmp = NULL;
800 int send_error_to_client;
804 GNUNET_assert(cls !=NULL);
805 send_error_to_client = GNUNET_NO;
807 if (NULL == *httpSessionCache)
809 /* check url for peer identity , if invalid send HTTP 404*/
810 size_t len = strlen(&url[1]);
811 char * peer = GNUNET_malloc(104+1);
813 if ((len>104) && (url[104]==';'))
815 char * id = GNUNET_malloc((len-104)+1);
816 strcpy(id,&url[105]);
817 memcpy(peer,&url[1],103);
819 id_num = strtoul ( id, NULL , 10);
822 res = GNUNET_CRYPTO_hash_from_string (peer, &(pi_in.hashPubKey));
824 if ( GNUNET_SYSERR == res )
826 response = MHD_create_response_from_data (strlen (HTTP_ERROR_RESPONSE),HTTP_ERROR_RESPONSE, MHD_NO, MHD_NO);
827 res = MHD_queue_response (mhd_connection, MHD_HTTP_NOT_FOUND, response);
828 MHD_destroy_response (response);
829 #if DEBUG_CONNECTIONS
831 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Peer has no valid ident, sent HTTP 1.1/404\n");
833 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Peer has no valid ident, could not send error\n");
840 ps = *httpSessionCache;
841 pc = ps->peercontext;
844 if (NULL == *httpSessionCache)
846 /* get peer context */
847 pc = GNUNET_CONTAINER_multihashmap_get (plugin->peers, &pi_in.hashPubKey);
851 pc = GNUNET_malloc(sizeof (struct HTTP_PeerContext));
853 pc->session_id_counter=1;
854 pc->last_session = NULL;
855 memcpy(&pc->identity, &pi_in, sizeof(struct GNUNET_PeerIdentity));
856 GNUNET_CONTAINER_multihashmap_put(plugin->peers, &pc->identity.hashPubKey, pc, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
857 GNUNET_STATISTICS_update (plugin->env->stats,
858 gettext_noop ("# HTTP peers active"),
863 conn_info = MHD_get_connection_info(mhd_connection, MHD_CONNECTION_INFO_CLIENT_ADDRESS );
864 /* Incoming IPv4 connection */
865 if ( AF_INET == conn_info->client_addr->sin_family)
867 addrin = conn_info->client_addr;
868 inet_ntop(addrin->sin_family, &(addrin->sin_addr),address,INET_ADDRSTRLEN);
869 memcpy(&ipv4addr.ipv4_addr,&(addrin->sin_addr),sizeof(struct in_addr));
870 ipv4addr.u_port = addrin->sin_port;
872 addr_len = sizeof(struct IPv4HttpAddress);
874 /* Incoming IPv6 connection */
875 if ( AF_INET6 == conn_info->client_addr->sin_family)
877 addrin6 = (struct sockaddr_in6 *) conn_info->client_addr;
878 inet_ntop(addrin6->sin6_family, &(addrin6->sin6_addr),address,INET6_ADDRSTRLEN);
879 memcpy(&ipv6addr.ipv6_addr,&(addrin6->sin6_addr),sizeof(struct in6_addr));
880 ipv6addr.u6_port = addrin6->sin6_port;
882 addr_len = sizeof(struct IPv6HttpAddress);
886 /* only inbound sessions here */
891 if ((ps_tmp->direction==INBOUND) && (ps_tmp->session_id == id_num) && (id_num!=0))
893 if ((ps_tmp->recv_force_disconnect!=GNUNET_YES) && (ps_tmp->send_force_disconnect!=GNUNET_YES))
902 ps = GNUNET_malloc(sizeof (struct Session));
903 ps->addr = GNUNET_malloc(addr_len);
904 memcpy(ps->addr,addr,addr_len);
905 ps->addrlen = addr_len;
906 ps->direction=INBOUND;
907 ps->pending_msgs_head = NULL;
908 ps->pending_msgs_tail = NULL;
909 ps->send_connected=GNUNET_NO;
910 ps->send_active=GNUNET_NO;
911 ps->recv_connected=GNUNET_NO;
912 ps->recv_active=GNUNET_NO;
914 ps->session_id =id_num;
915 ps->url = create_url (plugin, ps->addr, ps->addrlen, ps->session_id);
916 GNUNET_CONTAINER_DLL_insert(pc->head,pc->tail,ps);
917 GNUNET_STATISTICS_update (plugin->env->stats,
918 gettext_noop ("# HTTP inbound sessions for peers active"),
923 *httpSessionCache = ps;
924 if (ps->msgtok==NULL)
925 ps->msgtok = GNUNET_SERVER_mst_create (&mhd_write_mst_cb, ps);
927 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: HTTP Daemon has new an incoming `%s' request from peer `%s' (`%s')\n",
930 GNUNET_i2s(&pc->identity),
931 http_plugin_address_to_string(NULL, ps->addr, ps->addrlen));
935 /* Is it a PUT or a GET request */
936 if (0 == strcmp (MHD_HTTP_METHOD_PUT, method))
938 if (ps->recv_force_disconnect == GNUNET_YES)
940 #if DEBUG_CONNECTIONS
941 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: inbound connection was forced to disconnect\n",ps);
943 ps->recv_active = GNUNET_NO;
946 if ((*upload_data_size == 0) && (ps->recv_active==GNUNET_NO))
948 ps->recv_endpoint = mhd_connection;
949 ps->recv_connected = GNUNET_YES;
950 ps->recv_active = GNUNET_YES;
951 ps->recv_force_disconnect = GNUNET_NO;
952 #if DEBUG_CONNECTIONS
953 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: inbound PUT connection connected\n",ps);
958 /* Transmission of all data complete */
959 if ((*upload_data_size == 0) && (ps->recv_active == GNUNET_YES))
961 response = MHD_create_response_from_data (strlen (HTTP_PUT_RESPONSE),HTTP_PUT_RESPONSE, MHD_NO, MHD_NO);
962 res = MHD_queue_response (mhd_connection, MHD_HTTP_OK, response);
963 #if DEBUG_CONNECTIONS
964 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: Sent HTTP/1.1: 200 OK as PUT Response\n",ps);
966 MHD_destroy_response (response);
967 ps->recv_active=GNUNET_NO;
972 if ((*upload_data_size > 0) && (ps->recv_active == GNUNET_YES))
974 res = GNUNET_SERVER_mst_receive(ps->msgtok, ps, upload_data,*upload_data_size, GNUNET_NO, GNUNET_NO);
975 (*upload_data_size) = 0;
981 if ( 0 == strcmp (MHD_HTTP_METHOD_GET, method) )
983 if (ps->send_force_disconnect == GNUNET_YES)
985 #if DEBUG_CONNECTIONS
986 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: outbound connection was forced to disconnect\n",ps);
988 ps->send_active = GNUNET_NO;
991 ps->send_connected = GNUNET_YES;
992 ps->send_active = GNUNET_YES;
993 ps->send_endpoint = mhd_connection;
994 ps->send_force_disconnect = GNUNET_NO;
995 #if DEBUG_CONNECTIONS
996 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: inbound GET connection connected\n",ps);
998 response = MHD_create_response_from_callback(-1,32 * 1024, &mhd_send_callback, ps, NULL);
999 res = MHD_queue_response (mhd_connection, MHD_HTTP_OK, response);
1000 MHD_destroy_response (response);
1007 * Function that queries MHD's select sets and
1008 * starts the task waiting for them.
1010 static GNUNET_SCHEDULER_TaskIdentifier
1011 http_server_daemon_prepare (void * cls, struct MHD_Daemon *daemon_handle)
1013 struct Plugin *plugin = cls;
1014 GNUNET_SCHEDULER_TaskIdentifier ret;
1018 struct GNUNET_NETWORK_FDSet *wrs;
1019 struct GNUNET_NETWORK_FDSet *wws;
1020 struct GNUNET_NETWORK_FDSet *wes;
1022 unsigned long long timeout;
1024 struct GNUNET_TIME_Relative tv;
1026 GNUNET_assert(cls !=NULL);
1027 ret = GNUNET_SCHEDULER_NO_TASK;
1031 wrs = GNUNET_NETWORK_fdset_create ();
1032 wes = GNUNET_NETWORK_fdset_create ();
1033 wws = GNUNET_NETWORK_fdset_create ();
1035 GNUNET_assert (MHD_YES ==
1036 MHD_get_fdset (daemon_handle,
1041 haveto = MHD_get_timeout (daemon_handle, &timeout);
1042 if (haveto == MHD_YES)
1043 tv.value = (uint64_t) timeout;
1045 tv = GNUNET_TIME_UNIT_FOREVER_REL;
1046 GNUNET_NETWORK_fdset_copy_native (wrs, &rs, max);
1047 GNUNET_NETWORK_fdset_copy_native (wws, &ws, max);
1048 GNUNET_NETWORK_fdset_copy_native (wes, &es, max);
1049 if (daemon_handle == plugin->http_server_daemon_v4)
1051 if (plugin->http_server_task_v4 != GNUNET_SCHEDULER_NO_TASK)
1053 GNUNET_SCHEDULER_cancel(plugin->env->sched, plugin->http_server_task_v4);
1054 plugin->http_server_daemon_v4 = GNUNET_SCHEDULER_NO_TASK;
1057 ret = GNUNET_SCHEDULER_add_select (plugin->env->sched,
1058 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1059 GNUNET_SCHEDULER_NO_TASK,
1063 &http_server_daemon_v4_run,
1066 if (daemon_handle == plugin->http_server_daemon_v6)
1068 if (plugin->http_server_task_v6 != GNUNET_SCHEDULER_NO_TASK)
1070 GNUNET_SCHEDULER_cancel(plugin->env->sched, plugin->http_server_task_v6);
1071 plugin->http_server_task_v6 = GNUNET_SCHEDULER_NO_TASK;
1074 ret = GNUNET_SCHEDULER_add_select (plugin->env->sched,
1075 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1076 GNUNET_SCHEDULER_NO_TASK,
1080 &http_server_daemon_v6_run,
1083 GNUNET_NETWORK_fdset_destroy (wrs);
1084 GNUNET_NETWORK_fdset_destroy (wws);
1085 GNUNET_NETWORK_fdset_destroy (wes);
1090 * Call MHD to process pending requests and then go back
1091 * and schedule the next run.
1093 static void http_server_daemon_v4_run (void *cls,
1094 const struct GNUNET_SCHEDULER_TaskContext *tc)
1096 struct Plugin *plugin = cls;
1098 GNUNET_assert(cls !=NULL);
1099 plugin->http_server_task_v4 = GNUNET_SCHEDULER_NO_TASK;
1101 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1104 GNUNET_assert (MHD_YES == MHD_run (plugin->http_server_daemon_v4));
1105 plugin->http_server_task_v4 = http_server_daemon_prepare (plugin, plugin->http_server_daemon_v4);
1111 * Call MHD to process pending requests and then go back
1112 * and schedule the next run.
1114 static void http_server_daemon_v6_run (void *cls,
1115 const struct GNUNET_SCHEDULER_TaskContext *tc)
1117 struct Plugin *plugin = cls;
1119 GNUNET_assert(cls !=NULL);
1120 plugin->http_server_task_v6 = GNUNET_SCHEDULER_NO_TASK;
1122 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1125 GNUNET_assert (MHD_YES == MHD_run (plugin->http_server_daemon_v6));
1126 plugin->http_server_task_v6 = http_server_daemon_prepare (plugin, plugin->http_server_daemon_v6);
1130 static size_t curl_get_header_cb( void *ptr, size_t size, size_t nmemb, void *stream)
1132 struct Session * ps = stream;
1134 long http_result = 0;
1136 /* Getting last http result code */
1137 if (ps->recv_connected==GNUNET_NO)
1139 GNUNET_assert(NULL!=ps);
1140 res = curl_easy_getinfo(ps->recv_endpoint, CURLINFO_RESPONSE_CODE, &http_result);
1141 if (CURLE_OK == res)
1143 if (http_result == 200)
1145 ps->recv_connected = GNUNET_YES;
1146 ps->recv_active = GNUNET_YES;
1147 #if DEBUG_CONNECTIONS
1148 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: connected to recieve data\n",ps);
1150 // Calling send_check_connections again since receive is established
1151 send_check_connections (ps->peercontext->plugin, ps);
1158 size_t len = size * nmemb;
1160 if ((size * nmemb) < SIZE_MAX)
1161 tmp = GNUNET_malloc (len+1);
1163 if ((tmp != NULL) && (len > 0))
1165 memcpy(tmp,ptr,len);
1168 if (tmp[len-2] == 13)
1171 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: Header: %s\n",ps,tmp);
1177 return size * nmemb;
1180 static size_t curl_put_header_cb( void *ptr, size_t size, size_t nmemb, void *stream)
1182 struct Session * ps = stream;
1185 size_t len = size * nmemb;
1186 long http_result = 0;
1189 /* Getting last http result code */
1190 GNUNET_assert(NULL!=ps);
1191 res = curl_easy_getinfo(ps->send_endpoint, CURLINFO_RESPONSE_CODE, &http_result);
1192 if (CURLE_OK == res)
1194 if ((http_result == 100) && (ps->send_connected==GNUNET_NO))
1196 ps->send_connected = GNUNET_YES;
1197 ps->send_active = GNUNET_YES;
1198 #if DEBUG_CONNECTIONS
1199 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: connected to send data\n",ps);
1202 if ((http_result == 200) && (ps->send_connected==GNUNET_YES))
1204 ps->send_connected = GNUNET_NO;
1205 ps->send_active = GNUNET_NO;
1206 #if DEBUG_CONNECTIONS
1207 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: sending disconnected\n",ps);
1213 if ((size * nmemb) < SIZE_MAX)
1214 tmp = GNUNET_malloc (len+1);
1216 if ((tmp != NULL) && (len > 0))
1218 memcpy(tmp,ptr,len);
1221 if (tmp[len-2] == 13)
1228 return size * nmemb;
1232 * Callback method used with libcurl
1233 * Method is called when libcurl needs to read data during sending
1234 * @param stream pointer where to write data
1235 * @param size size of an individual element
1236 * @param nmemb count of elements that can be written to the buffer
1237 * @param ptr source pointer, passed to the libcurl handle
1238 * @return bytes written to stream
1240 static size_t curl_send_cb(void *stream, size_t size, size_t nmemb, void *ptr)
1242 struct Session * ps = ptr;
1243 struct HTTP_Message * msg = ps->pending_msgs_tail;
1247 if (ps->send_active == GNUNET_NO)
1248 return CURL_READFUNC_PAUSE;
1251 if ((ps->pending_msgs_tail == NULL) && (ps->send_active == GNUNET_YES))
1253 #if DEBUG_CONNECTIONS
1254 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: No Message to send, pausing connection\n",ps);
1256 ps->send_active = GNUNET_NO;
1257 return CURL_READFUNC_PAUSE;
1260 msg = ps->pending_msgs_tail;
1262 if (msg->pos < msg->size)
1264 /* data fit in buffer */
1265 if ((msg->size - msg->pos) <= (size * nmemb))
1267 len = (msg->size - msg->pos);
1268 memcpy(stream, &msg->buf[msg->pos], len);
1275 memcpy(stream, &msg->buf[msg->pos], len);
1280 /* no data to send */
1286 if ( msg->pos == msg->size)
1288 #if DEBUG_CONNECTIONS
1289 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: Message with %u bytes sent, removing message from queue \n",ps, msg->pos);
1291 /* Calling transmit continuation */
1292 if (( NULL != ps->pending_msgs_tail) && (NULL != ps->pending_msgs_tail->transmit_cont))
1293 msg->transmit_cont (ps->pending_msgs_tail->transmit_cont_cls,&(ps->peercontext)->identity,GNUNET_OK);
1294 remove_http_message(ps, msg);
1299 static void curl_receive_mst_cb (void *cls,
1301 const struct GNUNET_MessageHeader *message)
1303 struct Session *ps = cls;
1304 struct HTTP_PeerContext *pc = ps->peercontext;
1305 GNUNET_assert(ps != NULL);
1306 GNUNET_assert(pc != NULL);
1308 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1309 "Connection %X: Forwarding message to transport service, type %u and size %u from `%s' (`%s')\n",
1311 ntohs(message->type),
1312 ntohs(message->size),
1313 GNUNET_i2s(&(pc->identity)),http_plugin_address_to_string(NULL,ps->addr,ps->addrlen));
1315 pc->plugin->env->receive (pc->plugin->env->cls,
1324 * Callback method used with libcurl
1325 * Method is called when libcurl needs to write data during sending
1326 * @param stream pointer where to write data
1327 * @param size size of an individual element
1328 * @param nmemb count of elements that can be written to the buffer
1329 * @param ptr destination pointer, passed to the libcurl handle
1330 * @return bytes read from stream
1332 static size_t curl_receive_cb( void *stream, size_t size, size_t nmemb, void *ptr)
1334 struct Session * ps = ptr;
1335 #if DEBUG_CONNECTIONS
1336 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: %u bytes received\n",ps, size*nmemb);
1338 GNUNET_SERVER_mst_receive(ps->msgtok, ps, stream, size*nmemb, GNUNET_NO, GNUNET_NO);
1339 return (size * nmemb);
1343 static void curl_perform (void *cls,
1344 const struct GNUNET_SCHEDULER_TaskContext *tc)
1346 struct Plugin *plugin = cls;
1347 static unsigned int handles_last_run;
1349 struct CURLMsg *msg;
1351 struct Session *ps = NULL;
1352 struct HTTP_PeerContext *pc = NULL;
1353 struct HTTP_Message * cur_msg = NULL;
1357 GNUNET_assert(cls !=NULL);
1359 plugin->http_curl_task = GNUNET_SCHEDULER_NO_TASK;
1360 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1366 mret = curl_multi_perform (plugin->multi_handle, &running);
1367 if ((running < handles_last_run) && (running>0))
1372 msg = curl_multi_info_read (plugin->multi_handle, &running);
1375 /* get session for affected curl handle */
1376 GNUNET_assert ( msg->easy_handle != NULL );
1377 curl_easy_getinfo(msg->easy_handle, CURLINFO_PRIVATE, &tmp);
1378 ps = (struct Session *) tmp;
1379 GNUNET_assert ( ps != NULL );
1380 pc = ps->peercontext;
1381 GNUNET_assert ( pc != NULL );
1386 if ( (msg->data.result != CURLE_OK) &&
1387 (msg->data.result != CURLE_GOT_NOTHING) )
1389 /* sending msg failed*/
1390 if (msg->easy_handle == ps->send_endpoint)
1392 #if DEBUG_CONNECTIONS
1393 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1394 _("Connection %X: HTTP PUT to peer `%s' (`%s') failed: `%s' `%s'\n"),
1396 GNUNET_i2s(&pc->identity),
1397 http_plugin_address_to_string(NULL, ps->addr, ps->addrlen),
1398 "curl_multi_perform",
1399 curl_easy_strerror (msg->data.result));
1401 ps->send_connected = GNUNET_NO;
1402 ps->send_active = GNUNET_NO;
1403 curl_multi_remove_handle(plugin->multi_handle,ps->send_endpoint);
1404 //curl_easy_cleanup(ps->send_endpoint);
1405 //ps->send_endpoint=NULL;
1406 cur_msg = ps->pending_msgs_tail;
1407 if (( NULL != cur_msg) && ( NULL != cur_msg->transmit_cont))
1408 cur_msg->transmit_cont (cur_msg->transmit_cont_cls,&pc->identity,GNUNET_SYSERR);
1410 /* GET connection failed */
1411 if (msg->easy_handle == ps->recv_endpoint)
1413 #if DEBUG_CONNECTIONS
1414 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1415 _("Connection %X: HTTP GET to peer `%s' (`%s') failed: `%s' `%s'\n"),
1417 GNUNET_i2s(&pc->identity),
1418 http_plugin_address_to_string(NULL, ps->addr, ps->addrlen),
1419 "curl_multi_perform",
1420 curl_easy_strerror (msg->data.result));
1422 ps->recv_connected = GNUNET_NO;
1423 ps->recv_active = GNUNET_NO;
1424 curl_multi_remove_handle(plugin->multi_handle,ps->recv_endpoint);
1425 //curl_easy_cleanup(ps->recv_endpoint);
1426 //ps->recv_endpoint=NULL;
1431 if (msg->easy_handle == ps->send_endpoint)
1433 GNUNET_assert (CURLE_OK == curl_easy_getinfo(msg->easy_handle, CURLINFO_RESPONSE_CODE, &http_result));
1434 #if DEBUG_CONNECTIONS
1435 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1436 "Connection %X: HTTP PUT connection to peer `%s' (`%s') was closed with HTTP code %u\n",
1438 GNUNET_i2s(&pc->identity),
1439 http_plugin_address_to_string(NULL, ps->addr, ps->addrlen),
1442 /* Calling transmit continuation */
1443 cur_msg = ps->pending_msgs_tail;
1444 if (( NULL != cur_msg) && (NULL != cur_msg->transmit_cont))
1446 /* HTTP 1xx : Last message before here was informational */
1447 if ((http_result >=100) && (http_result < 200))
1448 cur_msg->transmit_cont (cur_msg->transmit_cont_cls,&pc->identity,GNUNET_OK);
1449 /* HTTP 2xx: successful operations */
1450 if ((http_result >=200) && (http_result < 300))
1451 cur_msg->transmit_cont (cur_msg->transmit_cont_cls,&pc->identity,GNUNET_OK);
1452 /* HTTP 3xx..5xx: error */
1453 if ((http_result >=300) && (http_result < 600))
1454 cur_msg->transmit_cont (cur_msg->transmit_cont_cls,&pc->identity,GNUNET_SYSERR);
1456 ps->send_connected = GNUNET_NO;
1457 ps->send_active = GNUNET_NO;
1458 curl_multi_remove_handle(plugin->multi_handle,ps->send_endpoint);
1459 //curl_easy_cleanup(ps->send_endpoint);
1460 //ps->send_endpoint =NULL;
1462 if (msg->easy_handle == ps->recv_endpoint)
1464 #if DEBUG_CONNECTIONS
1465 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1466 "Connection %X: HTTP GET connection to peer `%s' (`%s') was closed with HTTP code %u\n",
1468 GNUNET_i2s(&pc->identity),
1469 http_plugin_address_to_string(NULL, ps->addr, ps->addrlen),
1472 ps->recv_connected = GNUNET_NO;
1473 ps->recv_active = GNUNET_NO;
1474 curl_multi_remove_handle(plugin->multi_handle,ps->recv_endpoint);
1475 //curl_easy_cleanup(ps->recv_endpoint);
1476 //ps->recv_endpoint=NULL;
1479 if ((ps->recv_connected == GNUNET_NO) && (ps->send_connected == GNUNET_NO))
1480 remove_session (pc, ps, GNUNET_YES, GNUNET_SYSERR);
1487 while ( (running > 0) );
1489 handles_last_run = running;
1491 while (mret == CURLM_CALL_MULTI_PERFORM);
1492 curl_schedule(plugin);
1497 * Function setting up file descriptors and scheduling task to run
1498 * @param ses session to send data to
1499 * @return GNUNET_SYSERR for hard failure, GNUNET_OK for ok
1501 static int curl_schedule(void *cls)
1503 struct Plugin *plugin = cls;
1508 struct GNUNET_NETWORK_FDSet *grs;
1509 struct GNUNET_NETWORK_FDSet *gws;
1513 GNUNET_assert(cls !=NULL);
1515 /* Cancel previous scheduled task */
1516 if (plugin->http_curl_task != GNUNET_SCHEDULER_NO_TASK)
1518 GNUNET_SCHEDULER_cancel(plugin->env->sched, plugin->http_curl_task);
1519 plugin->http_curl_task = GNUNET_SCHEDULER_NO_TASK;
1525 mret = curl_multi_fdset (plugin->multi_handle, &rs, &ws, &es, &max);
1526 if (mret != CURLM_OK)
1528 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1529 _("%s failed at %s:%d: `%s'\n"),
1530 "curl_multi_fdset", __FILE__, __LINE__,
1531 curl_multi_strerror (mret));
1532 return GNUNET_SYSERR;
1534 mret = curl_multi_timeout (plugin->multi_handle, &to);
1535 if (mret != CURLM_OK)
1537 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1538 _("%s failed at %s:%d: `%s'\n"),
1539 "curl_multi_timeout", __FILE__, __LINE__,
1540 curl_multi_strerror (mret));
1541 return GNUNET_SYSERR;
1544 grs = GNUNET_NETWORK_fdset_create ();
1545 gws = GNUNET_NETWORK_fdset_create ();
1546 GNUNET_NETWORK_fdset_copy_native (grs, &rs, max + 1);
1547 GNUNET_NETWORK_fdset_copy_native (gws, &ws, max + 1);
1548 plugin->http_curl_task = GNUNET_SCHEDULER_add_select (plugin->env->sched,
1549 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1550 GNUNET_SCHEDULER_NO_TASK,
1551 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 0),
1556 GNUNET_NETWORK_fdset_destroy (gws);
1557 GNUNET_NETWORK_fdset_destroy (grs);
1562 * Function setting up curl handle and selecting message to send
1564 * @param ses session to send data to
1565 * @param con connection
1566 * @return GNUNET_SYSERR on failure, GNUNET_NO if connecting, GNUNET_YES if ok
1568 static ssize_t send_check_connections (void *cls, struct Session *ps)
1570 struct Plugin *plugin = cls;
1572 struct HTTP_Message * msg;
1574 struct GNUNET_TIME_Relative timeout = GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT;
1576 GNUNET_assert(cls !=NULL);
1578 if (ps->direction == OUTBOUND)
1580 /* RECV DIRECTION */
1581 /* Check if session is connected to receive data, otherwise connect to peer */
1582 if (ps->recv_connected == GNUNET_NO)
1584 int fresh = GNUNET_NO;
1585 if (ps->recv_endpoint == NULL)
1588 ps->recv_endpoint = curl_easy_init();
1591 curl_easy_setopt(ps->recv_endpoint, CURLOPT_VERBOSE, 1L);
1593 curl_easy_setopt(ps->recv_endpoint, CURLOPT_URL, ps->url);
1594 curl_easy_setopt(ps->recv_endpoint, CURLOPT_HEADERFUNCTION, &curl_get_header_cb);
1595 curl_easy_setopt(ps->recv_endpoint, CURLOPT_WRITEHEADER, ps);
1596 curl_easy_setopt(ps->recv_endpoint, CURLOPT_READFUNCTION, curl_send_cb);
1597 curl_easy_setopt(ps->recv_endpoint, CURLOPT_READDATA, ps);
1598 curl_easy_setopt(ps->recv_endpoint, CURLOPT_WRITEFUNCTION, curl_receive_cb);
1599 curl_easy_setopt(ps->recv_endpoint, CURLOPT_WRITEDATA, ps);
1600 curl_easy_setopt(ps->recv_endpoint, CURLOPT_TIMEOUT, (long) timeout.value);
1601 curl_easy_setopt(ps->recv_endpoint, CURLOPT_PRIVATE, ps);
1602 curl_easy_setopt(ps->recv_endpoint, CURLOPT_CONNECTTIMEOUT, HTTP_CONNECT_TIMEOUT);
1603 curl_easy_setopt(ps->recv_endpoint, CURLOPT_BUFFERSIZE, GNUNET_SERVER_MAX_MESSAGE_SIZE);
1605 if (fresh==GNUNET_YES)
1607 mret = curl_multi_add_handle(plugin->multi_handle, ps->recv_endpoint);
1608 if (mret != CURLM_OK)
1610 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1611 _("Connection: %X: %s failed at %s:%d: `%s'\n"),
1613 "curl_multi_add_handle", __FILE__, __LINE__,
1614 curl_multi_strerror (mret));
1615 return GNUNET_SYSERR;
1618 if (curl_schedule (plugin) == GNUNET_SYSERR)
1620 #if DEBUG_CONNECTIONS
1621 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: could not schedule curl task\n",ps);
1623 return GNUNET_SYSERR;
1625 #if DEBUG_CONNECTIONS
1626 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: inbound not connected, initiating connection\n",ps);
1630 /* waiting for receive direction */
1631 if (ps->recv_connected==GNUNET_NO)
1634 /* SEND DIRECTION */
1635 /* Check if session is connected to send data, otherwise connect to peer */
1636 if ((ps->send_connected == GNUNET_YES) && (ps->send_endpoint!= NULL))
1638 if (ps->send_active == GNUNET_YES)
1640 #if DEBUG_CONNECTIONS
1641 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: outbound active, enqueueing message\n",ps);
1645 if (ps->send_active == GNUNET_NO)
1647 #if DEBUG_CONNECTIONS
1648 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: outbound paused, unpausing existing connection and enqueueing message\n",ps);
1650 if (CURLE_OK == curl_easy_pause(ps->send_endpoint,CURLPAUSE_CONT))
1652 ps->send_active=GNUNET_YES;
1656 return GNUNET_SYSERR;
1659 /* not connected, initiate connection */
1660 if (ps->send_connected==GNUNET_NO)
1662 int fresh = GNUNET_NO;
1663 if (NULL == ps->send_endpoint)
1665 ps->send_endpoint = curl_easy_init();
1668 GNUNET_assert (ps->send_endpoint != NULL);
1669 GNUNET_assert (NULL != ps->pending_msgs_tail);
1670 #if DEBUG_CONNECTIONS
1671 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: outbound not connected, initiating connection\n",ps);
1673 ps->send_active = GNUNET_NO;
1674 msg = ps->pending_msgs_tail;
1677 curl_easy_setopt(ps->send_endpoint, CURLOPT_VERBOSE, 1L);
1679 curl_easy_setopt(ps->send_endpoint, CURLOPT_URL, ps->url);
1680 curl_easy_setopt(ps->send_endpoint, CURLOPT_PUT, 1L);
1681 curl_easy_setopt(ps->send_endpoint, CURLOPT_HEADERFUNCTION, &curl_put_header_cb);
1682 curl_easy_setopt(ps->send_endpoint, CURLOPT_WRITEHEADER, ps);
1683 curl_easy_setopt(ps->send_endpoint, CURLOPT_READFUNCTION, curl_send_cb);
1684 curl_easy_setopt(ps->send_endpoint, CURLOPT_READDATA, ps);
1685 curl_easy_setopt(ps->send_endpoint, CURLOPT_WRITEFUNCTION, curl_receive_cb);
1686 curl_easy_setopt(ps->send_endpoint, CURLOPT_READDATA, ps);
1687 curl_easy_setopt(ps->send_endpoint, CURLOPT_TIMEOUT, (long) timeout.value);
1688 curl_easy_setopt(ps->send_endpoint, CURLOPT_PRIVATE, ps);
1689 curl_easy_setopt(ps->send_endpoint, CURLOPT_CONNECTTIMEOUT, HTTP_CONNECT_TIMEOUT);
1690 curl_easy_setopt(ps->send_endpoint, CURLOPT_BUFFERSIZE, GNUNET_SERVER_MAX_MESSAGE_SIZE);
1692 if (fresh==GNUNET_YES)
1694 mret = curl_multi_add_handle(plugin->multi_handle, ps->send_endpoint);
1695 if (mret != CURLM_OK)
1697 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1698 _("Connection: %X: %s failed at %s:%d: `%s'\n"),
1700 "curl_multi_add_handle", __FILE__, __LINE__,
1701 curl_multi_strerror (mret));
1702 return GNUNET_SYSERR;
1706 if (curl_schedule (plugin) == GNUNET_SYSERR)
1707 return GNUNET_SYSERR;
1710 if (ps->direction == INBOUND)
1712 GNUNET_assert (NULL != ps->pending_msgs_tail);
1713 if ((ps->recv_connected==GNUNET_YES) && (ps->send_connected==GNUNET_YES) &&
1714 (ps->recv_force_disconnect==GNUNET_NO) && (ps->recv_force_disconnect==GNUNET_NO))
1717 return GNUNET_SYSERR;
1720 static struct Session * send_select_session (void * cls, struct HTTP_PeerContext *pc, const void * addr, size_t addrlen, int force_address, struct Session * session)
1722 struct Session * tmp = NULL;
1723 int addr_given = GNUNET_NO;
1725 if ((addr!=NULL) && (addrlen>0))
1726 addr_given = GNUNET_YES;
1728 if (force_address == GNUNET_YES)
1730 /* check session given as argument */
1731 if ((session != NULL) && (addr_given == GNUNET_YES))
1733 if (0 == memcmp(session->addr, addr, addrlen))
1735 /* connection can not be used, since it is disconnected */
1736 if ((session->recv_force_disconnect==GNUNET_NO) && (session->send_force_disconnect==GNUNET_NO))
1738 #if DEBUG_SESSION_SELECTION
1739 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Session %X selected: Using session passed by transport to send to forced address \n", session);
1745 /* check last session used */
1746 if ((pc->last_session != NULL)&& (addr_given == GNUNET_YES))
1748 if (0 == memcmp(pc->last_session->addr, addr, addrlen))
1750 /* connection can not be used, since it is disconnected */
1751 if ((pc->last_session->recv_force_disconnect==GNUNET_NO) && (pc->last_session->send_force_disconnect==GNUNET_NO))
1753 #if DEBUG_SESSION_SELECTION
1754 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Session %X selected: Using last session used to send to forced address \n", pc->last_session);
1756 return pc->last_session;
1760 /* find session in existing sessions */
1762 while ((tmp!=NULL) && (addr_given == GNUNET_YES))
1765 if (0 == memcmp(tmp->addr, addr, addrlen))
1767 /* connection can not be used, since it is disconnected */
1768 if ((tmp->recv_force_disconnect==GNUNET_NO) && (tmp->send_force_disconnect==GNUNET_NO))
1770 #if DEBUG_SESSION_SELECTION
1771 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Session %X selected: Using existing session to send to forced address \n", session);
1779 /* no session to use */
1782 if ((force_address == GNUNET_NO) || (force_address == GNUNET_SYSERR))
1784 /* check session given as argument */
1785 if (session != NULL)
1787 /* connection can not be used, since it is disconnected */
1788 if ((session->recv_force_disconnect==GNUNET_NO) && (session->send_force_disconnect==GNUNET_NO))
1790 #if DEBUG_SESSION_SELECTION
1791 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Session %X selected: Using session passed by transport to send not-forced address \n", session);
1797 /* check last session used */
1798 if (pc->last_session != NULL)
1800 /* connection can not be used, since it is disconnected */
1801 if ((pc->last_session->recv_force_disconnect==GNUNET_NO) && (pc->last_session->send_force_disconnect==GNUNET_NO))
1803 #if DEBUG_SESSION_SELECTION
1804 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Session %X selected: Using last session to send to not-forced address \n", pc->last_session);
1806 return pc->last_session;
1809 /* find session in existing sessions */
1813 /* connection can not be used, since it is disconnected */
1814 if ((tmp->recv_force_disconnect==GNUNET_NO) && (tmp->send_force_disconnect==GNUNET_NO))
1816 #if DEBUG_SESSION_SELECTION
1817 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Session %X selected: Using existing session to send to not-forced address \n", tmp);
1829 * Function that can be used by the transport service to transmit
1830 * a message using the plugin. Note that in the case of a
1831 * peer disconnecting, the continuation MUST be called
1832 * prior to the disconnect notification itself. This function
1833 * will be called with this peer's HELLO message to initiate
1834 * a fresh connection to another peer.
1836 * @param cls closure
1837 * @param target who should receive this message
1838 * @param msgbuf the message to transmit
1839 * @param msgbuf_size number of bytes in 'msgbuf'
1840 * @param priority how important is the message (most plugins will
1841 * ignore message priority and just FIFO)
1842 * @param timeout how long to wait at most for the transmission (does not
1843 * require plugins to discard the message after the timeout,
1844 * just advisory for the desired delay; most plugins will ignore
1846 * @param session which session must be used (or NULL for "any")
1847 * @param addr the address to use (can be NULL if the plugin
1848 * is "on its own" (i.e. re-use existing TCP connection))
1849 * @param addrlen length of the address in bytes
1850 * @param force_address GNUNET_YES if the plugin MUST use the given address,
1851 * GNUNET_NO means the plugin may use any other address and
1852 * GNUNET_SYSERR means that only reliable existing
1853 * bi-directional connections should be used (regardless
1855 * @param cont continuation to call once the message has
1856 * been transmitted (or if the transport is ready
1857 * for the next transmission call; or if the
1858 * peer disconnected...); can be NULL
1859 * @param cont_cls closure for cont
1860 * @return number of bytes used (on the physical network, with overheads);
1861 * -1 on hard errors (i.e. address invalid); 0 is a legal value
1862 * and does NOT mean that the message was not transmitted (DV)
1865 http_plugin_send (void *cls,
1866 const struct GNUNET_PeerIdentity *target,
1869 unsigned int priority,
1870 struct GNUNET_TIME_Relative to,
1871 struct Session *session,
1875 GNUNET_TRANSPORT_TransmitContinuation cont,
1878 struct Plugin *plugin = cls;
1879 struct HTTP_Message *msg;
1880 struct HTTP_PeerContext * pc;
1881 struct Session * ps = NULL;
1883 GNUNET_assert(cls !=NULL);
1886 char * force = GNUNET_malloc(40);
1887 if (force_address == GNUNET_YES)
1888 strcpy(force,"forced addr.");
1889 if (force_address == GNUNET_NO)
1890 strcpy(force,"any addr.");
1891 if (force_address == GNUNET_SYSERR)
1892 strcpy(force,"reliable bi-direc. address addr.");
1894 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Transport tells me to send %u bytes to `%s' using %s (%s) and session: %X\n",
1898 http_plugin_address_to_string(NULL, addr, addrlen),
1904 pc = GNUNET_CONTAINER_multihashmap_get (plugin->peers, &target->hashPubKey);
1908 pc = GNUNET_malloc(sizeof (struct HTTP_PeerContext));
1909 pc->plugin = plugin;
1910 pc->session_id_counter=1;
1911 pc->last_session = NULL;
1912 memcpy(&pc->identity, target, sizeof(struct GNUNET_PeerIdentity));
1913 GNUNET_CONTAINER_multihashmap_put(plugin->peers, &pc->identity.hashPubKey, pc, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
1914 GNUNET_STATISTICS_update (plugin->env->stats,
1915 gettext_noop ("# HTTP peers active"),
1920 ps = send_select_session (plugin, pc, addr, addrlen, force_address, session);
1922 /* session not existing, but address forced -> creating new session */
1925 if ((addr!=NULL) && (addrlen!=0))
1927 ps = GNUNET_malloc(sizeof (struct Session));
1928 #if DEBUG_SESSION_SELECTION
1929 if (force_address == GNUNET_YES)
1930 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"No existing connection & forced address: creating new session %X to peer %s\n", ps, GNUNET_i2s(target));
1931 if (force_address != GNUNET_YES)
1932 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"No existing connection: creating new session %X to peer %s\n", ps, GNUNET_i2s(target));
1934 if ((addrlen!=0) && (addr!=NULL))
1936 ps->addr = GNUNET_malloc(addrlen);
1937 memcpy(ps->addr,addr,addrlen);
1938 ps->addrlen = addrlen;
1945 ps->direction=OUTBOUND;
1946 ps->recv_connected = GNUNET_NO;
1947 ps->recv_force_disconnect = GNUNET_NO;
1948 ps->send_connected = GNUNET_NO;
1949 ps->send_force_disconnect = GNUNET_NO;
1950 ps->pending_msgs_head = NULL;
1951 ps->pending_msgs_tail = NULL;
1953 ps->session_id = pc->session_id_counter;
1954 pc->session_id_counter++;
1955 ps->url = create_url (plugin, ps->addr, ps->addrlen, ps->session_id);
1956 if (ps->msgtok == NULL)
1957 ps->msgtok = GNUNET_SERVER_mst_create (&curl_receive_mst_cb, ps);
1958 GNUNET_CONTAINER_DLL_insert(pc->head,pc->tail,ps);
1961 GNUNET_STATISTICS_update (plugin->env->stats,
1962 gettext_noop ("# HTTP outbound sessions for peers active"),
1969 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));
1971 return GNUNET_SYSERR;
1976 msg = GNUNET_malloc (sizeof (struct HTTP_Message) + msgbuf_size);
1978 msg->size = msgbuf_size;
1980 msg->buf = (char *) &msg[1];
1981 msg->transmit_cont = cont;
1982 msg->transmit_cont_cls = cont_cls;
1983 memcpy (msg->buf,msgbuf, msgbuf_size);
1984 GNUNET_CONTAINER_DLL_insert(ps->pending_msgs_head,ps->pending_msgs_tail,msg);
1986 if (send_check_connections (plugin, ps) != GNUNET_SYSERR)
1988 if (force_address != GNUNET_YES)
1989 pc->last_session = ps;
1991 if (pc->last_session==NULL)
1992 pc->last_session = ps;
1996 return GNUNET_SYSERR;
2002 * Function that can be used to force the plugin to disconnect
2003 * from the given peer and cancel all previous transmissions
2004 * (and their continuationc).
2006 * @param cls closure
2007 * @param target peer from which to disconnect
2010 http_plugin_disconnect (void *cls,
2011 const struct GNUNET_PeerIdentity *target)
2015 struct Plugin *plugin = cls;
2016 struct HTTP_PeerContext *pc = NULL;
2017 struct Session *ps = NULL;
2018 //struct Session *tmp = NULL;
2020 pc = GNUNET_CONTAINER_multihashmap_get (plugin->peers, &target->hashPubKey);
2027 /* Telling transport that session is getting disconnected */
2028 plugin->env->session_end(plugin, target, ps);
2029 if (ps->direction==OUTBOUND)
2031 if (ps->send_endpoint!=NULL)
2033 //GNUNET_assert(CURLM_OK == curl_multi_remove_handle(plugin->multi_handle,ps->send_endpoint));
2034 //curl_easy_cleanup(ps->send_endpoint);
2035 //ps->send_endpoint=NULL;
2036 ps->send_force_disconnect = GNUNET_YES;
2038 if (ps->recv_endpoint!=NULL)
2040 //GNUNET_assert(CURLM_OK == curl_multi_remove_handle(plugin->multi_handle,ps->recv_endpoint));
2041 //curl_easy_cleanup(ps->recv_endpoint);
2042 //ps->recv_endpoint=NULL;
2043 ps->recv_force_disconnect = GNUNET_YES;
2047 if (ps->direction==INBOUND)
2049 ps->recv_force_disconnect = GNUNET_YES;
2050 ps->send_force_disconnect = GNUNET_YES;
2053 while (ps->pending_msgs_head!=NULL)
2055 remove_http_message(ps, ps->pending_msgs_head);
2057 ps->recv_active = GNUNET_NO;
2058 ps->send_active = GNUNET_NO;
2065 * Convert the transports address to a nice, human-readable
2068 * @param cls closure
2069 * @param type name of the transport that generated the address
2070 * @param addr one of the addresses of the host, NULL for the last address
2071 * the specific address format depends on the transport
2072 * @param addrlen length of the address
2073 * @param numeric should (IP) addresses be displayed in numeric form?
2074 * @param timeout after how long should we give up?
2075 * @param asc function to call on each string
2076 * @param asc_cls closure for asc
2079 http_plugin_address_pretty_printer (void *cls,
2084 struct GNUNET_TIME_Relative timeout,
2085 GNUNET_TRANSPORT_AddressStringCallback
2088 const struct IPv4HttpAddress *t4;
2089 const struct IPv6HttpAddress *t6;
2090 struct sockaddr_in a4;
2091 struct sockaddr_in6 a6;
2097 GNUNET_assert(cls !=NULL);
2098 if (addrlen == sizeof (struct IPv6HttpAddress))
2100 address = GNUNET_malloc (INET6_ADDRSTRLEN);
2102 a6.sin6_addr = t6->ipv6_addr;
2103 inet_ntop(AF_INET6, &(a6.sin6_addr),address,INET6_ADDRSTRLEN);
2104 port = ntohs(t6->u6_port);
2106 else if (addrlen == sizeof (struct IPv4HttpAddress))
2108 address = GNUNET_malloc (INET_ADDRSTRLEN);
2110 a4.sin_addr.s_addr = t4->ipv4_addr;
2111 inet_ntop(AF_INET, &(a4.sin_addr),address,INET_ADDRSTRLEN);
2112 port = ntohs(t4->u_port);
2116 /* invalid address */
2117 GNUNET_break_op (0);
2118 asc (asc_cls, NULL);
2121 res = GNUNET_asprintf(&ret,"%s://%s:%u/", PROTOCOL_PREFIX, address, port);
2122 GNUNET_free (address);
2123 GNUNET_assert(res != 0);
2125 GNUNET_free_non_null (ret);
2131 * Another peer has suggested an address for this
2132 * peer and transport plugin. Check that this could be a valid
2133 * address. If so, consider adding it to the list
2136 * @param cls closure
2137 * @param addr pointer to the address
2138 * @param addrlen length of addr
2139 * @return GNUNET_OK if this is a plausible address for this peer
2143 http_plugin_address_suggested (void *cls,
2144 const void *addr, size_t addrlen)
2146 struct Plugin *plugin = cls;
2147 struct IPv4HttpAddress *v4;
2148 struct IPv6HttpAddress *v6;
2151 GNUNET_assert(cls !=NULL);
2152 if ((addrlen != sizeof (struct IPv4HttpAddress)) &&
2153 (addrlen != sizeof (struct IPv6HttpAddress)))
2155 return GNUNET_SYSERR;
2157 if (addrlen == sizeof (struct IPv4HttpAddress))
2159 v4 = (struct IPv4HttpAddress *) addr;
2160 /* Not skipping loopback
2161 if (INADDR_LOOPBACK == ntohl(v4->ipv4_addr))
2163 return GNUNET_SYSERR;
2165 port = ntohs (v4->u_port);
2166 if (port != plugin->port_inbound)
2168 return GNUNET_SYSERR;
2171 if (addrlen == sizeof (struct IPv6HttpAddress))
2173 v6 = (struct IPv6HttpAddress *) addr;
2174 if (IN6_IS_ADDR_LINKLOCAL (&v6->ipv6_addr))
2176 return GNUNET_SYSERR;
2178 port = ntohs (v6->u6_port);
2179 if (port != plugin->port_inbound)
2181 return GNUNET_SYSERR;
2190 * Function called for a quick conversion of the binary address to
2191 * a numeric address. Note that the caller must not free the
2192 * address and that the next call to this function is allowed
2193 * to override the address again.
2195 * @param cls closure
2196 * @param addr binary address
2197 * @param addrlen length of the address
2198 * @return string representing the same address
2201 http_plugin_address_to_string (void *cls,
2205 const struct IPv4HttpAddress *t4;
2206 const struct IPv6HttpAddress *t6;
2207 struct sockaddr_in a4;
2208 struct sockaddr_in6 a6;
2214 if (addrlen == sizeof (struct IPv6HttpAddress))
2216 address = GNUNET_malloc (INET6_ADDRSTRLEN);
2218 a6.sin6_addr = t6->ipv6_addr;
2219 inet_ntop(AF_INET6, &(a6.sin6_addr),address,INET6_ADDRSTRLEN);
2220 port = ntohs(t6->u6_port);
2222 else if (addrlen == sizeof (struct IPv4HttpAddress))
2224 address = GNUNET_malloc (INET_ADDRSTRLEN);
2226 a4.sin_addr.s_addr = t4->ipv4_addr;
2227 inet_ntop(AF_INET, &(a4.sin_addr),address,INET_ADDRSTRLEN);
2228 port = ntohs(t4->u_port);
2232 /* invalid address */
2235 res = GNUNET_asprintf(&ret,"%s:%u",address,port);
2236 GNUNET_free (address);
2237 GNUNET_assert(res != 0);
2243 * Exit point from the plugin.
2246 libgnunet_plugin_transport_http_done (void *cls)
2248 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
2249 struct Plugin *plugin = api->cls;
2251 GNUNET_assert(cls !=NULL);
2253 if (plugin->http_server_daemon_v4 != NULL)
2255 MHD_stop_daemon (plugin->http_server_daemon_v4);
2256 plugin->http_server_daemon_v4 = NULL;
2258 if (plugin->http_server_daemon_v6 != NULL)
2260 MHD_stop_daemon (plugin->http_server_daemon_v6);
2261 plugin->http_server_daemon_v6 = NULL;
2264 if ( plugin->http_server_task_v4 != GNUNET_SCHEDULER_NO_TASK)
2266 GNUNET_SCHEDULER_cancel(plugin->env->sched, plugin->http_server_task_v4);
2267 plugin->http_server_task_v4 = GNUNET_SCHEDULER_NO_TASK;
2270 if ( plugin->http_server_task_v6 != GNUNET_SCHEDULER_NO_TASK)
2272 GNUNET_SCHEDULER_cancel(plugin->env->sched, plugin->http_server_task_v6);
2273 plugin->http_server_task_v6 = GNUNET_SCHEDULER_NO_TASK;
2277 /* free all peer information */
2278 if (plugin->peers!=NULL)
2280 GNUNET_CONTAINER_multihashmap_iterate (plugin->peers,
2281 &remove_peer_context_Iterator,
2283 GNUNET_CONTAINER_multihashmap_destroy (plugin->peers);
2285 if (plugin->multi_handle!=NULL)
2287 mret = curl_multi_cleanup(plugin->multi_handle);
2289 if ( CURLM_OK != mret)
2290 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"curl multihandle clean up failed\n");
2292 plugin->multi_handle = NULL;
2294 curl_global_cleanup();
2296 if ( plugin->http_curl_task != GNUNET_SCHEDULER_NO_TASK)
2298 GNUNET_SCHEDULER_cancel(plugin->env->sched, plugin->http_curl_task);
2299 plugin->http_curl_task = GNUNET_SCHEDULER_NO_TASK;
2302 GNUNET_free_non_null (plugin->bind4_address);
2303 GNUNET_free_non_null (plugin->bind6_address);
2304 GNUNET_free_non_null(plugin->bind_hostname);
2305 GNUNET_free (plugin);
2308 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Unload http plugin complete...\n");
2315 * Entry point for the plugin.
2318 libgnunet_plugin_transport_http_init (void *cls)
2320 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
2321 struct Plugin *plugin;
2322 struct GNUNET_TRANSPORT_PluginFunctions *api;
2323 struct GNUNET_TIME_Relative gn_timeout;
2324 long long unsigned int port;
2326 GNUNET_assert(cls !=NULL);
2328 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Starting http plugin...\n");
2331 plugin = GNUNET_malloc (sizeof (struct Plugin));
2332 plugin->stats = env->stats;
2334 plugin->peers = NULL;
2335 plugin->bind4_address = NULL;
2336 plugin->use_ipv6 = GNUNET_YES;
2337 plugin->use_ipv4 = GNUNET_YES;
2339 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
2341 api->send = &http_plugin_send;
2342 api->disconnect = &http_plugin_disconnect;
2343 api->address_pretty_printer = &http_plugin_address_pretty_printer;
2344 api->check_address = &http_plugin_address_suggested;
2345 api->address_to_string = &http_plugin_address_to_string;
2347 /* Hashing our identity to use it in URLs */
2348 GNUNET_CRYPTO_hash_to_enc ( &(plugin->env->my_identity->hashPubKey), &plugin->my_ascii_hash_ident);
2350 /* Reading port number from config file */
2351 if (GNUNET_CONFIGURATION_have_value (env->cfg,
2352 "transport-http", "USE_IPv6"))
2354 plugin->use_ipv6 = GNUNET_CONFIGURATION_get_value_yesno (env->cfg,
2358 /* Reading port number from config file */
2359 if (GNUNET_CONFIGURATION_have_value (env->cfg,
2360 "transport-http", "USE_IPv4"))
2362 plugin->use_ipv4 = GNUNET_CONFIGURATION_get_value_yesno (env->cfg,
2366 /* Reading port number from config file */
2368 GNUNET_CONFIGURATION_get_value_number (env->cfg,
2374 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
2376 _("Require valid port number for transport plugin `%s' in configuration!\n"),
2378 libgnunet_plugin_transport_http_done (api);
2382 /* Reading ipv4 addresse to bind to from config file */
2383 if ((plugin->use_ipv4==GNUNET_YES) && (GNUNET_CONFIGURATION_have_value (env->cfg,
2384 "transport-http", "BINDTO4")))
2386 GNUNET_break (GNUNET_OK ==
2387 GNUNET_CONFIGURATION_get_value_string (env->cfg,
2390 &plugin->bind_hostname));
2391 plugin->bind4_address = GNUNET_malloc(sizeof(struct sockaddr_in));
2392 plugin->bind4_address->sin_family = AF_INET;
2393 plugin->bind4_address->sin_port = htons (port);
2395 if (inet_pton(AF_INET,plugin->bind_hostname, &plugin->bind4_address->sin_addr)<=0)
2397 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
2399 _("Misconfigured address to bind to in configuration!\n"),
2401 GNUNET_free(plugin->bind4_address);
2402 GNUNET_free(plugin->bind_hostname);
2403 plugin->bind_hostname = NULL;
2404 plugin->bind4_address = NULL;
2408 /* Reading ipv4 addresse to bind to from config file */
2409 if ((plugin->use_ipv6==GNUNET_YES) && (GNUNET_CONFIGURATION_have_value (env->cfg,
2410 "transport-http", "BINDTO6")))
2412 GNUNET_break (GNUNET_OK ==
2413 GNUNET_CONFIGURATION_get_value_string (env->cfg,
2416 &plugin->bind_hostname));
2418 plugin->bind6_address = GNUNET_malloc(sizeof(struct sockaddr_in6));
2419 plugin->bind6_address->sin6_family = AF_INET6;
2420 plugin->bind6_address->sin6_port = htons (port);
2422 if (inet_pton(AF_INET6,plugin->bind_hostname, &plugin->bind6_address->sin6_addr)<=0)
2424 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
2426 _("Misconfigured address to bind to in configuration!\n"),
2428 GNUNET_free(plugin->bind6_address);
2429 GNUNET_free(plugin->bind_hostname);
2430 plugin->bind_hostname = NULL;
2431 plugin->bind6_address = NULL;
2435 GNUNET_assert ((port > 0) && (port <= 65535));
2436 plugin->port_inbound = port;
2437 gn_timeout = GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT;
2438 unsigned int timeout = (gn_timeout.value) / 1000;
2439 if ((plugin->http_server_daemon_v6 == NULL) && (plugin->use_ipv6 == GNUNET_YES) && (port != 0))
2441 struct sockaddr * tmp = (struct sockaddr *) plugin->bind6_address;
2442 plugin->http_server_daemon_v6 = MHD_start_daemon (
2443 #if DEBUG_CONNECTIONS
2449 plugin , &mdh_access_cb, plugin,
2450 MHD_OPTION_SOCK_ADDR, tmp,
2451 MHD_OPTION_CONNECTION_LIMIT, (unsigned int) 32,
2452 //MHD_OPTION_PER_IP_CONNECTION_LIMIT, (unsigned int) 6,
2453 MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) timeout,
2454 MHD_OPTION_CONNECTION_MEMORY_LIMIT, (size_t) (16 * 1024),
2455 MHD_OPTION_NOTIFY_COMPLETED, &mhd_termination_cb, NULL,
2458 if ((plugin->http_server_daemon_v4 == NULL) && (plugin->use_ipv4 == GNUNET_YES) && (port != 0))
2460 plugin->http_server_daemon_v4 = MHD_start_daemon (
2461 #if DEBUG_CONNECTIONS
2467 plugin , &mdh_access_cb, plugin,
2468 MHD_OPTION_SOCK_ADDR, (struct sockaddr_in *)plugin->bind4_address,
2469 MHD_OPTION_CONNECTION_LIMIT, (unsigned int) 32,
2470 //MHD_OPTION_PER_IP_CONNECTION_LIMIT, (unsigned int) 6,
2471 MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) timeout,
2472 MHD_OPTION_CONNECTION_MEMORY_LIMIT, (size_t) (16 * 1024),
2473 MHD_OPTION_NOTIFY_COMPLETED, &mhd_termination_cb, NULL,
2476 if (plugin->http_server_daemon_v4 != NULL)
2477 plugin->http_server_task_v4 = http_server_daemon_prepare (plugin, plugin->http_server_daemon_v4);
2478 if (plugin->http_server_daemon_v6 != NULL)
2479 plugin->http_server_task_v6 = http_server_daemon_prepare (plugin, plugin->http_server_daemon_v6);
2482 if (plugin->http_server_task_v4 != GNUNET_SCHEDULER_NO_TASK)
2485 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);
2488 else if ((plugin->http_server_task_v6 != GNUNET_SCHEDULER_NO_TASK) && (plugin->http_server_task_v4 != GNUNET_SCHEDULER_NO_TASK))
2491 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);
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 IPv4 and IPv6 bound to %s with port %u\n",(plugin->bind_hostname!=NULL) ? plugin->bind_hostname : "every address", port);
2503 if ((plugin->use_ipv6 == GNUNET_YES) && (plugin->use_ipv4 == GNUNET_YES))
2504 GNUNET_asprintf(&tmp,"with IPv4 and IPv6 enabled");
2505 if ((plugin->use_ipv6 == GNUNET_NO) && (plugin->use_ipv4 == GNUNET_YES))
2506 GNUNET_asprintf(&tmp,"with IPv4 enabled");
2507 if ((plugin->use_ipv6 == GNUNET_YES) && (plugin->use_ipv4 == GNUNET_NO))
2508 GNUNET_asprintf(&tmp,"with IPv6 enabled");
2509 if ((plugin->use_ipv6 == GNUNET_NO) && (plugin->use_ipv4 == GNUNET_NO))
2510 GNUNET_asprintf(&tmp,"with NO IP PROTOCOL enabled");
2511 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,"HTTP Server with %s could not be started on port %u! https plugin failed!\n",tmp, port);
2513 libgnunet_plugin_transport_http_done (api);
2517 /* Initializing cURL */
2518 curl_global_init(CURL_GLOBAL_ALL);
2519 plugin->multi_handle = curl_multi_init();
2521 if ( NULL == plugin->multi_handle )
2523 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
2525 _("Could not initialize curl multi handle, failed to start http plugin!\n"),
2527 libgnunet_plugin_transport_http_done (api);
2531 plugin->peers = GNUNET_CONTAINER_multihashmap_create (10);
2532 GNUNET_OS_network_interfaces_list (&process_interfaces, plugin);
2537 /* end of plugin_transport_http.c */