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
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);
442 * Removes a session from the linked list of sessions
443 * @param pc peer context
445 * @param call_msg_cont GNUNET_YES to call pending message continuations, otherwise no
446 * @param call_msg_cont_result, result to call message continuations with
447 * @return GNUNET_SYSERR if msg not found, GNUNET_OK on success
449 static int remove_session (struct HTTP_PeerContext * pc, struct Session * ps, int call_msg_cont, int call_msg_cont_result)
451 struct HTTP_Message * msg;
452 struct Plugin * plugin = ps->peercontext->plugin;
454 #if DEBUG_CONNECTIONS
455 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);
457 plugin->env->session_end(plugin, &pc->identity, ps);
459 GNUNET_free_non_null (ps->addr);
460 GNUNET_SERVER_mst_destroy (ps->msgtok);
461 GNUNET_free(ps->url);
463 msg = ps->pending_msgs_head;
466 if ((call_msg_cont == GNUNET_YES) && (msg->transmit_cont!=NULL))
468 msg->transmit_cont (msg->transmit_cont_cls,&pc->identity,call_msg_cont_result);
471 GNUNET_CONTAINER_DLL_remove(ps->pending_msgs_head,ps->pending_msgs_head,msg);
472 msg = ps->pending_msgs_head;
475 GNUNET_CONTAINER_DLL_remove(pc->head,pc->tail,ps);
481 int remove_peer_context_Iterator (void *cls, const GNUNET_HashCode *key, void *value)
483 struct Plugin *plugin = cls;
484 struct HTTP_PeerContext * pc = value;
485 struct Session * ps = pc->head;
486 struct Session * tmp = NULL;
487 struct HTTP_Message * msg = NULL;
488 struct HTTP_Message * msg_tmp = NULL;
490 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Freeing context for peer `%s'\n",GNUNET_i2s(&pc->identity));
494 plugin->env->session_end(plugin, &pc->identity, ps);
497 GNUNET_free_non_null (ps->addr);
498 GNUNET_free(ps->url);
499 if (ps->msgtok != NULL)
500 GNUNET_SERVER_mst_destroy (ps->msgtok);
502 msg = ps->pending_msgs_head;
509 if (ps->direction==OUTBOUND)
511 if (ps->send_endpoint!=NULL)
512 curl_easy_cleanup(ps->send_endpoint);
513 if (ps->recv_endpoint!=NULL)
514 curl_easy_cleanup(ps->recv_endpoint);
525 * Add the IP of our network interface to the list of
526 * our external IP addresses.
528 * @param cls the 'struct Plugin*'
529 * @param name name of the interface
530 * @param isDefault do we think this may be our default interface
531 * @param addr address of the interface
532 * @param addrlen number of bytes in addr
533 * @return GNUNET_OK to continue iterating
536 process_interfaces (void *cls,
539 const struct sockaddr *addr, socklen_t addrlen)
541 struct Plugin *plugin = cls;
542 struct IPv4HttpAddress * t4;
543 struct IPv6HttpAddress * t6;
547 GNUNET_assert(cls !=NULL);
548 af = addr->sa_family;
549 if ((af == AF_INET) && (plugin->use_ipv4 == GNUNET_YES) && (plugin->bind6_address == NULL))
551 struct in_addr bnd_cmp = ((struct sockaddr_in *) addr)->sin_addr;
552 t4 = GNUNET_malloc(sizeof(struct IPv4HttpAddress));
553 /* Not skipping loopback addresses
554 if (INADDR_LOOPBACK == ntohl(((struct sockaddr_in *) addr)->sin_addr.s_addr))
560 t4->ipv4_addr = ((struct sockaddr_in *) addr)->sin_addr.s_addr;
561 t4->u_port = htons (plugin->port_inbound);
562 if (plugin->bind4_address != NULL)
564 if (0 == memcmp(&plugin->bind4_address->sin_addr, &bnd_cmp, sizeof (struct in_addr)))
566 plugin->env->notify_address(plugin->env->cls,"http",t4, sizeof (struct IPv4HttpAddress), GNUNET_TIME_UNIT_FOREVER_REL);
571 plugin->env->notify_address(plugin->env->cls,"http",t4, sizeof (struct IPv4HttpAddress), GNUNET_TIME_UNIT_FOREVER_REL);
575 else if ((af == AF_INET6) && (plugin->use_ipv6 == GNUNET_YES) && (plugin->bind4_address == NULL))
577 struct in6_addr bnd_cmp6 = ((struct sockaddr_in6 *) addr)->sin6_addr;
578 t6 = GNUNET_malloc(sizeof(struct IPv6HttpAddress));
579 if (IN6_IS_ADDR_LINKLOCAL (&((struct sockaddr_in6 *) addr)->sin6_addr))
584 if (plugin->bind6_address != NULL)
586 if (0 == memcmp(&plugin->bind6_address->sin6_addr, &bnd_cmp6, sizeof (struct in6_addr)))
588 memcpy (&t6->ipv6_addr,
589 &((struct sockaddr_in6 *) addr)->sin6_addr,
590 sizeof (struct in6_addr));
591 t6->u6_port = htons (plugin->port_inbound);
592 plugin->env->notify_address(plugin->env->cls,"http",t6,sizeof (struct IPv6HttpAddress) , GNUNET_TIME_UNIT_FOREVER_REL);
597 memcpy (&t6->ipv6_addr,
598 &((struct sockaddr_in6 *) addr)->sin6_addr,
599 sizeof (struct in6_addr));
600 t6->u6_port = htons (plugin->port_inbound);
601 plugin->env->notify_address(plugin->env->cls,"http",t6,sizeof (struct IPv6HttpAddress) , GNUNET_TIME_UNIT_FOREVER_REL);
610 * Callback called by MHD when a connection is terminated
612 static void mhd_termination_cb (void *cls, struct MHD_Connection * connection, void **httpSessionCache)
614 struct Session * ps = *httpSessionCache;
617 struct HTTP_PeerContext * pc = ps->peercontext;
619 if (connection==ps->recv_endpoint)
621 #if DEBUG_CONNECTIONS
622 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: inbound connection from peer `%s' was terminated\n", ps, GNUNET_i2s(&pc->identity));
624 ps->recv_active = GNUNET_NO;
625 ps->recv_connected = GNUNET_NO;
626 ps->recv_endpoint = NULL;
628 if (connection==ps->send_endpoint)
631 ps->send_active = GNUNET_NO;
632 ps->send_connected = GNUNET_NO;
633 ps->send_endpoint = NULL;
634 #if DEBUG_CONNECTIONS
635 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: outbound connection from peer `%s' was terminated\n", ps, GNUNET_i2s(&pc->identity));
639 /* if both connections disconnected, remove session */
640 if ((ps->send_connected == GNUNET_NO) && (ps->recv_connected == GNUNET_NO))
642 remove_session(pc,ps,GNUNET_YES,GNUNET_SYSERR);
646 static void mhd_write_mst_cb (void *cls,
648 const struct GNUNET_MessageHeader *message)
651 struct Session *ps = cls;
652 struct HTTP_PeerContext *pc = ps->peercontext;
653 GNUNET_assert(ps != NULL);
654 GNUNET_assert(pc != NULL);
656 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
657 "Connection %X: Forwarding message to transport service, type %u and size %u from `%s' (`%s')\n",
659 ntohs(message->type),
660 ntohs(message->size),
661 GNUNET_i2s(&(ps->peercontext)->identity),http_plugin_address_to_string(NULL,ps->addr,ps->addrlen));
663 pc->plugin->env->receive (ps->peercontext->plugin->env->cls,
671 * Check if ip is allowed to connect.
674 mhd_accept_cb (void *cls,
675 const struct sockaddr *addr, socklen_t addr_len)
678 struct Plugin *plugin = cls;
680 /* Every connection is accepted, nothing more to do here */
684 int mhd_send_callback (void *cls, uint64_t pos, char *buf, int max)
688 struct Session * ps = cls;
689 struct HTTP_PeerContext * pc;
690 struct HTTP_Message * msg;
692 GNUNET_assert (ps!=NULL);
693 pc = ps->peercontext;
694 msg = ps->pending_msgs_tail;
695 if (ps->send_force_disconnect==GNUNET_YES)
697 #if DEBUG_CONNECTIONS
698 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: outbound forced to disconnect\n",ps);
705 if ((msg->size-msg->pos) <= max)
707 memcpy(buf,&msg->buf[msg->pos],(msg->size-msg->pos));
708 bytes_read = msg->size-msg->pos;
709 msg->pos+=(msg->size-msg->pos);
713 memcpy(buf,&msg->buf[msg->pos],max);
718 if (msg->pos==msg->size)
720 if (NULL!=msg->transmit_cont)
721 msg->transmit_cont (msg->transmit_cont_cls,&pc->identity,GNUNET_OK);
722 remove_http_message(ps,msg);
729 * Process GET or PUT request received via MHD. For
730 * GET, queue response that will send back our pending
731 * messages. For PUT, process incoming data and send
732 * to GNUnet core. In either case, check if a session
733 * already exists and create a new one if not.
736 mdh_access_cb (void *cls,
737 struct MHD_Connection *mhd_connection,
741 const char *upload_data,
742 size_t * upload_data_size, void **httpSessionCache)
744 struct Plugin *plugin = cls;
745 struct MHD_Response *response;
746 const union MHD_ConnectionInfo * conn_info;
748 struct sockaddr_in *addrin;
749 struct sockaddr_in6 *addrin6;
751 char address[INET6_ADDRSTRLEN+14];
752 struct GNUNET_PeerIdentity pi_in;
755 struct IPv4HttpAddress ipv4addr;
756 struct IPv6HttpAddress ipv6addr;
758 struct HTTP_PeerContext *pc;
759 struct Session *ps = NULL;
760 struct Session *ps_tmp = NULL;
763 int send_error_to_client;
767 GNUNET_assert(cls !=NULL);
768 send_error_to_client = GNUNET_NO;
770 if (NULL == *httpSessionCache)
772 /* check url for peer identity , if invalid send HTTP 404*/
773 size_t len = strlen(&url[1]);
774 char * peer = GNUNET_malloc(104+1);
776 if ((len>104) && (url[104]==';'))
778 char * id = GNUNET_malloc((len-104)+1);
779 strcpy(id,&url[105]);
780 memcpy(peer,&url[1],103);
782 id_num = strtoul ( id, NULL , 10);
785 res = GNUNET_CRYPTO_hash_from_string (peer, &(pi_in.hashPubKey));
787 if ( GNUNET_SYSERR == res )
789 response = MHD_create_response_from_data (strlen (HTTP_ERROR_RESPONSE),HTTP_ERROR_RESPONSE, MHD_NO, MHD_NO);
790 res = MHD_queue_response (mhd_connection, MHD_HTTP_NOT_FOUND, response);
791 MHD_destroy_response (response);
792 #if DEBUG_CONNECTIONS
794 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Peer has no valid ident, sent HTTP 1.1/404\n");
796 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Peer has no valid ident, could not send error\n");
803 ps = *httpSessionCache;
804 pc = ps->peercontext;
807 if (NULL == *httpSessionCache)
809 /* get peer context */
810 pc = GNUNET_CONTAINER_multihashmap_get (plugin->peers, &pi_in.hashPubKey);
814 pc = GNUNET_malloc(sizeof (struct HTTP_PeerContext));
816 pc->session_id_counter=1;
817 pc->last_session = NULL;
818 memcpy(&pc->identity, &pi_in, sizeof(struct GNUNET_PeerIdentity));
819 GNUNET_CONTAINER_multihashmap_put(plugin->peers, &pc->identity.hashPubKey, pc, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
822 conn_info = MHD_get_connection_info(mhd_connection, MHD_CONNECTION_INFO_CLIENT_ADDRESS );
823 /* Incoming IPv4 connection */
824 if ( AF_INET == conn_info->client_addr->sin_family)
826 addrin = conn_info->client_addr;
827 inet_ntop(addrin->sin_family, &(addrin->sin_addr),address,INET_ADDRSTRLEN);
828 memcpy(&ipv4addr.ipv4_addr,&(addrin->sin_addr),sizeof(struct in_addr));
829 ipv4addr.u_port = addrin->sin_port;
831 addr_len = sizeof(struct IPv4HttpAddress);
833 /* Incoming IPv6 connection */
834 if ( AF_INET6 == conn_info->client_addr->sin_family)
836 addrin6 = (struct sockaddr_in6 *) conn_info->client_addr;
837 inet_ntop(addrin6->sin6_family, &(addrin6->sin6_addr),address,INET6_ADDRSTRLEN);
838 memcpy(&ipv6addr.ipv6_addr,&(addrin6->sin6_addr),sizeof(struct in6_addr));
839 ipv6addr.u6_port = addrin6->sin6_port;
841 addr_len = sizeof(struct IPv6HttpAddress);
845 /* only inbound sessions here */
850 if ((ps_tmp->direction==INBOUND) && (ps_tmp->session_id == id_num) && (id_num!=0))
852 if ((ps_tmp->recv_force_disconnect!=GNUNET_YES) && (ps_tmp->send_force_disconnect!=GNUNET_YES))
861 ps = GNUNET_malloc(sizeof (struct Session));
862 ps->addr = GNUNET_malloc(addr_len);
863 memcpy(ps->addr,addr,addr_len);
864 ps->addrlen = addr_len;
865 ps->direction=INBOUND;
866 ps->pending_msgs_head = NULL;
867 ps->pending_msgs_tail = NULL;
868 ps->send_connected=GNUNET_NO;
869 ps->send_active=GNUNET_NO;
870 ps->recv_connected=GNUNET_NO;
871 ps->recv_active=GNUNET_NO;
873 ps->session_id =id_num;
874 ps->url = create_url (plugin, ps->addr, ps->addrlen, ps->session_id);
875 GNUNET_CONTAINER_DLL_insert(pc->head,pc->tail,ps);
878 *httpSessionCache = ps;
879 if (ps->msgtok==NULL)
880 ps->msgtok = GNUNET_SERVER_mst_create (&mhd_write_mst_cb, ps);
882 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: HTTP Daemon has new an incoming `%s' request from peer `%s' (`%s')\n",
885 GNUNET_i2s(&pc->identity),
886 http_plugin_address_to_string(NULL, ps->addr, ps->addrlen));
890 /* Is it a PUT or a GET request */
891 if (0 == strcmp (MHD_HTTP_METHOD_PUT, method))
893 if (ps->recv_force_disconnect == GNUNET_YES)
895 #if DEBUG_CONNECTIONS
896 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: inbound connection was forced to disconnect\n",ps);
898 ps->recv_active = GNUNET_NO;
901 if ((*upload_data_size == 0) && (ps->recv_active==GNUNET_NO))
903 ps->recv_endpoint = mhd_connection;
904 ps->recv_connected = GNUNET_YES;
905 ps->recv_active = GNUNET_YES;
906 ps->recv_force_disconnect = GNUNET_NO;
907 #if DEBUG_CONNECTIONS
908 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: inbound PUT connection connected\n",ps);
913 /* Transmission of all data complete */
914 if ((*upload_data_size == 0) && (ps->recv_active == GNUNET_YES))
916 response = MHD_create_response_from_data (strlen (HTTP_PUT_RESPONSE),HTTP_PUT_RESPONSE, MHD_NO, MHD_NO);
917 res = MHD_queue_response (mhd_connection, MHD_HTTP_OK, response);
918 #if DEBUG_CONNECTIONS
919 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: Sent HTTP/1.1: 200 OK as PUT Response\n",ps);
921 MHD_destroy_response (response);
922 ps->recv_active=GNUNET_NO;
927 if ((*upload_data_size > 0) && (ps->recv_active == GNUNET_YES))
929 res = GNUNET_SERVER_mst_receive(ps->msgtok, ps, upload_data,*upload_data_size, GNUNET_NO, GNUNET_NO);
930 (*upload_data_size) = 0;
936 if ( 0 == strcmp (MHD_HTTP_METHOD_GET, method) )
938 if (ps->send_force_disconnect == GNUNET_YES)
940 #if DEBUG_CONNECTIONS
941 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: outbound connection was forced to disconnect\n",ps);
943 ps->send_active = GNUNET_NO;
946 ps->send_connected = GNUNET_YES;
947 ps->send_active = GNUNET_YES;
948 ps->send_endpoint = mhd_connection;
949 ps->send_force_disconnect = GNUNET_NO;
950 #if DEBUG_CONNECTIONS
951 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: inbound GET connection connected\n",ps);
953 response = MHD_create_response_from_callback(-1,32 * 1024, &mhd_send_callback, ps, NULL);
954 res = MHD_queue_response (mhd_connection, MHD_HTTP_OK, response);
955 MHD_destroy_response (response);
962 * Function that queries MHD's select sets and
963 * starts the task waiting for them.
965 static GNUNET_SCHEDULER_TaskIdentifier
966 http_server_daemon_prepare (void * cls, struct MHD_Daemon *daemon_handle)
968 struct Plugin *plugin = cls;
969 GNUNET_SCHEDULER_TaskIdentifier ret;
973 struct GNUNET_NETWORK_FDSet *wrs;
974 struct GNUNET_NETWORK_FDSet *wws;
975 struct GNUNET_NETWORK_FDSet *wes;
977 unsigned long long timeout;
979 struct GNUNET_TIME_Relative tv;
981 GNUNET_assert(cls !=NULL);
982 ret = GNUNET_SCHEDULER_NO_TASK;
986 wrs = GNUNET_NETWORK_fdset_create ();
987 wes = GNUNET_NETWORK_fdset_create ();
988 wws = GNUNET_NETWORK_fdset_create ();
990 GNUNET_assert (MHD_YES ==
991 MHD_get_fdset (daemon_handle,
996 haveto = MHD_get_timeout (daemon_handle, &timeout);
997 if (haveto == MHD_YES)
998 tv.value = (uint64_t) timeout;
1000 tv = GNUNET_TIME_UNIT_FOREVER_REL;
1001 GNUNET_NETWORK_fdset_copy_native (wrs, &rs, max);
1002 GNUNET_NETWORK_fdset_copy_native (wws, &ws, max);
1003 GNUNET_NETWORK_fdset_copy_native (wes, &es, max);
1004 if (daemon_handle == plugin->http_server_daemon_v4)
1006 if (plugin->http_server_task_v4 != GNUNET_SCHEDULER_NO_TASK)
1008 GNUNET_SCHEDULER_cancel(plugin->env->sched, plugin->http_server_task_v4);
1009 plugin->http_server_daemon_v4 = GNUNET_SCHEDULER_NO_TASK;
1012 ret = GNUNET_SCHEDULER_add_select (plugin->env->sched,
1013 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1014 GNUNET_SCHEDULER_NO_TASK,
1018 &http_server_daemon_v4_run,
1021 if (daemon_handle == plugin->http_server_daemon_v6)
1023 if (plugin->http_server_task_v6 != GNUNET_SCHEDULER_NO_TASK)
1025 GNUNET_SCHEDULER_cancel(plugin->env->sched, plugin->http_server_task_v6);
1026 plugin->http_server_task_v6 = GNUNET_SCHEDULER_NO_TASK;
1029 ret = GNUNET_SCHEDULER_add_select (plugin->env->sched,
1030 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1031 GNUNET_SCHEDULER_NO_TASK,
1035 &http_server_daemon_v6_run,
1038 GNUNET_NETWORK_fdset_destroy (wrs);
1039 GNUNET_NETWORK_fdset_destroy (wws);
1040 GNUNET_NETWORK_fdset_destroy (wes);
1045 * Call MHD to process pending requests and then go back
1046 * and schedule the next run.
1048 static void http_server_daemon_v4_run (void *cls,
1049 const struct GNUNET_SCHEDULER_TaskContext *tc)
1051 struct Plugin *plugin = cls;
1053 GNUNET_assert(cls !=NULL);
1054 plugin->http_server_task_v4 = GNUNET_SCHEDULER_NO_TASK;
1056 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1059 GNUNET_assert (MHD_YES == MHD_run (plugin->http_server_daemon_v4));
1060 plugin->http_server_task_v4 = http_server_daemon_prepare (plugin, plugin->http_server_daemon_v4);
1066 * Call MHD to process pending requests and then go back
1067 * and schedule the next run.
1069 static void http_server_daemon_v6_run (void *cls,
1070 const struct GNUNET_SCHEDULER_TaskContext *tc)
1072 struct Plugin *plugin = cls;
1074 GNUNET_assert(cls !=NULL);
1075 plugin->http_server_task_v6 = GNUNET_SCHEDULER_NO_TASK;
1077 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1080 GNUNET_assert (MHD_YES == MHD_run (plugin->http_server_daemon_v6));
1081 plugin->http_server_task_v6 = http_server_daemon_prepare (plugin, plugin->http_server_daemon_v6);
1085 static size_t curl_get_header_cb( void *ptr, size_t size, size_t nmemb, void *stream)
1087 struct Session * ps = stream;
1090 size_t len = size * nmemb;
1091 long http_result = 0;
1093 /* Getting last http result code */
1094 if (ps->recv_connected==GNUNET_NO)
1096 GNUNET_assert(NULL!=ps);
1097 res = curl_easy_getinfo(ps->recv_endpoint, CURLINFO_RESPONSE_CODE, &http_result);
1098 if (CURLE_OK == res)
1100 if (http_result == 200)
1102 ps->recv_connected = GNUNET_YES;
1103 ps->recv_active = GNUNET_YES;
1104 #if DEBUG_CONNECTIONS
1105 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: connected to recieve data\n",ps);
1107 // Calling send_check_connections again since receive is established
1108 send_check_connections (ps->peercontext->plugin, ps);
1114 if ((size * nmemb) < SIZE_MAX)
1115 tmp = GNUNET_malloc (len+1);
1117 if ((tmp != NULL) && (len > 0))
1119 memcpy(tmp,ptr,len);
1122 if (tmp[len-2] == 13)
1129 return size * nmemb;
1132 static size_t curl_put_header_cb( void *ptr, size_t size, size_t nmemb, void *stream)
1134 struct Session * ps = stream;
1137 size_t len = size * nmemb;
1138 long http_result = 0;
1141 /* Getting last http result code */
1142 GNUNET_assert(NULL!=ps);
1143 res = curl_easy_getinfo(ps->send_endpoint, CURLINFO_RESPONSE_CODE, &http_result);
1144 if (CURLE_OK == res)
1146 if ((http_result == 100) && (ps->send_connected==GNUNET_NO))
1148 ps->send_connected = GNUNET_YES;
1149 ps->send_active = GNUNET_YES;
1150 #if DEBUG_CONNECTIONS
1151 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: connected to send data\n",ps);
1154 if ((http_result == 200) && (ps->send_connected==GNUNET_YES))
1156 ps->send_connected = GNUNET_NO;
1157 ps->send_active = GNUNET_NO;
1158 #if DEBUG_CONNECTIONS
1159 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: sending disconnected\n",ps);
1165 if ((size * nmemb) < SIZE_MAX)
1166 tmp = GNUNET_malloc (len+1);
1168 if ((tmp != NULL) && (len > 0))
1170 memcpy(tmp,ptr,len);
1173 if (tmp[len-2] == 13)
1180 return size * nmemb;
1184 * Callback method used with libcurl
1185 * Method is called when libcurl needs to read data during sending
1186 * @param stream pointer where to write data
1187 * @param size size of an individual element
1188 * @param nmemb count of elements that can be written to the buffer
1189 * @param ptr source pointer, passed to the libcurl handle
1190 * @return bytes written to stream
1192 static size_t curl_send_cb(void *stream, size_t size, size_t nmemb, void *ptr)
1194 struct Session * ps = ptr;
1195 struct HTTP_Message * msg = ps->pending_msgs_tail;
1199 if (ps->send_active == GNUNET_NO)
1200 return CURL_READFUNC_PAUSE;
1203 if ((ps->pending_msgs_tail == NULL) && (ps->send_active == GNUNET_YES))
1205 #if DEBUG_CONNECTIONS
1206 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: No Message to send, pausing connection\n",ps);
1208 ps->send_active = GNUNET_NO;
1209 return CURL_READFUNC_PAUSE;
1212 msg = ps->pending_msgs_tail;
1214 if (msg->pos < msg->size)
1216 /* data fit in buffer */
1217 if ((msg->size - msg->pos) <= (size * nmemb))
1219 len = (msg->size - msg->pos);
1220 memcpy(stream, &msg->buf[msg->pos], len);
1227 memcpy(stream, &msg->buf[msg->pos], len);
1232 /* no data to send */
1238 if ( msg->pos == msg->size)
1240 #if DEBUG_CONNECTIONS
1241 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: Message with %u bytes sent, removing message from queue \n",ps, msg->pos);
1243 /* Calling transmit continuation */
1244 if (( NULL != ps->pending_msgs_tail) && (NULL != ps->pending_msgs_tail->transmit_cont))
1245 msg->transmit_cont (ps->pending_msgs_tail->transmit_cont_cls,&(ps->peercontext)->identity,GNUNET_OK);
1246 remove_http_message(ps, msg);
1251 static void curl_receive_mst_cb (void *cls,
1253 const struct GNUNET_MessageHeader *message)
1255 struct Session *ps = cls;
1256 struct HTTP_PeerContext *pc = ps->peercontext;
1257 GNUNET_assert(ps != NULL);
1258 GNUNET_assert(pc != NULL);
1260 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1261 "Connection %X: Forwarding message to transport service, type %u and size %u from `%s' (`%s')\n",
1263 ntohs(message->type),
1264 ntohs(message->size),
1265 GNUNET_i2s(&(pc->identity)),http_plugin_address_to_string(NULL,ps->addr,ps->addrlen));
1267 pc->plugin->env->receive (pc->plugin->env->cls,
1276 * Callback method used with libcurl
1277 * Method is called when libcurl needs to write data during sending
1278 * @param stream pointer where to write data
1279 * @param size size of an individual element
1280 * @param nmemb count of elements that can be written to the buffer
1281 * @param ptr destination pointer, passed to the libcurl handle
1282 * @return bytes read from stream
1284 static size_t curl_receive_cb( void *stream, size_t size, size_t nmemb, void *ptr)
1286 struct Session * ps = ptr;
1287 #if DEBUG_CONNECTIONS
1288 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: %u bytes received\n",ps, size*nmemb);
1290 GNUNET_SERVER_mst_receive(ps->msgtok, ps, stream, size*nmemb, GNUNET_NO, GNUNET_NO);
1291 return (size * nmemb);
1295 static void curl_perform (void *cls,
1296 const struct GNUNET_SCHEDULER_TaskContext *tc)
1298 struct Plugin *plugin = cls;
1299 static unsigned int handles_last_run;
1301 struct CURLMsg *msg;
1303 struct Session *ps = NULL;
1304 struct HTTP_PeerContext *pc = NULL;
1305 struct HTTP_Message * cur_msg = NULL;
1308 GNUNET_assert(cls !=NULL);
1310 plugin->http_curl_task = GNUNET_SCHEDULER_NO_TASK;
1311 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1317 mret = curl_multi_perform (plugin->multi_handle, &running);
1318 if ((running < handles_last_run) && (running>0))
1323 msg = curl_multi_info_read (plugin->multi_handle, &running);
1326 /* get session for affected curl handle */
1327 GNUNET_assert ( msg->easy_handle != NULL );
1328 curl_easy_getinfo(msg->easy_handle, CURLINFO_PRIVATE, (char *) &ps);
1329 GNUNET_assert ( ps != NULL );
1330 pc = ps->peercontext;
1331 GNUNET_assert ( pc != NULL );
1336 if ( (msg->data.result != CURLE_OK) &&
1337 (msg->data.result != CURLE_GOT_NOTHING) )
1339 /* sending msg failed*/
1340 if (msg->easy_handle == ps->send_endpoint)
1342 #if DEBUG_CONNECTIONS
1343 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1344 _("Connection %X: HTTP PUT to peer `%s' (`%s') failed: `%s' `%s'\n"),
1346 GNUNET_i2s(&pc->identity),
1347 http_plugin_address_to_string(NULL, ps->addr, ps->addrlen),
1348 "curl_multi_perform",
1349 curl_easy_strerror (msg->data.result));
1351 ps->send_connected = GNUNET_NO;
1352 ps->send_active = GNUNET_NO;
1353 curl_multi_remove_handle(plugin->multi_handle,ps->send_endpoint);
1354 curl_easy_cleanup(ps->send_endpoint);
1355 ps->send_endpoint=NULL;
1356 cur_msg = ps->pending_msgs_tail;
1357 if (( NULL != cur_msg) && ( NULL != cur_msg->transmit_cont))
1358 cur_msg->transmit_cont (cur_msg->transmit_cont_cls,&pc->identity,GNUNET_SYSERR);
1360 /* GET connection failed */
1361 if (msg->easy_handle == ps->recv_endpoint)
1363 #if DEBUG_CONNECTIONS
1364 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1365 _("Connection %X: HTTP GET to peer `%s' (`%s') failed: `%s' `%s'\n"),
1367 GNUNET_i2s(&pc->identity),
1368 http_plugin_address_to_string(NULL, ps->addr, ps->addrlen),
1369 "curl_multi_perform",
1370 curl_easy_strerror (msg->data.result));
1372 ps->recv_connected = GNUNET_NO;
1373 ps->recv_active = GNUNET_NO;
1374 curl_multi_remove_handle(plugin->multi_handle,ps->recv_endpoint);
1375 curl_easy_cleanup(ps->recv_endpoint);
1376 ps->recv_endpoint=NULL;
1381 if (msg->easy_handle == ps->send_endpoint)
1383 GNUNET_assert (CURLE_OK == curl_easy_getinfo(msg->easy_handle, CURLINFO_RESPONSE_CODE, &http_result));
1384 #if DEBUG_CONNECTIONS
1385 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1386 "Connection %X: HTTP PUT connection to peer `%s' (`%s') was closed with HTTP code %u\n",
1388 GNUNET_i2s(&pc->identity),
1389 http_plugin_address_to_string(NULL, ps->addr, ps->addrlen),
1392 /* Calling transmit continuation */
1393 cur_msg = ps->pending_msgs_tail;
1394 if (( NULL != cur_msg) && (NULL != cur_msg->transmit_cont))
1396 /* HTTP 1xx : Last message before here was informational */
1397 if ((http_result >=100) && (http_result < 200))
1398 cur_msg->transmit_cont (cur_msg->transmit_cont_cls,&pc->identity,GNUNET_OK);
1399 /* HTTP 2xx: successful operations */
1400 if ((http_result >=200) && (http_result < 300))
1401 cur_msg->transmit_cont (cur_msg->transmit_cont_cls,&pc->identity,GNUNET_OK);
1402 /* HTTP 3xx..5xx: error */
1403 if ((http_result >=300) && (http_result < 600))
1404 cur_msg->transmit_cont (cur_msg->transmit_cont_cls,&pc->identity,GNUNET_SYSERR);
1406 ps->send_connected = GNUNET_NO;
1407 ps->send_active = GNUNET_NO;
1408 curl_multi_remove_handle(plugin->multi_handle,ps->send_endpoint);
1409 curl_easy_cleanup(ps->send_endpoint);
1410 ps->send_endpoint =NULL;
1412 if (msg->easy_handle == ps->recv_endpoint)
1414 #if DEBUG_CONNECTIONS
1415 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1416 "Connection %X: HTTP GET connection to peer `%s' (`%s') was closed with HTTP code %u\n",
1418 GNUNET_i2s(&pc->identity),
1419 http_plugin_address_to_string(NULL, ps->addr, ps->addrlen),
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;
1429 if ((ps->recv_connected == GNUNET_NO) && (ps->send_connected == GNUNET_NO))
1430 remove_session (pc, ps, GNUNET_YES, GNUNET_SYSERR);
1437 while ( (running > 0) );
1439 handles_last_run = running;
1441 while (mret == CURLM_CALL_MULTI_PERFORM);
1442 if (running>0) curl_schedule(plugin);
1447 * Function setting up file descriptors and scheduling task to run
1448 * @param ses session to send data to
1449 * @return GNUNET_SYSERR for hard failure, GNUNET_OK for ok
1451 static int curl_schedule(void *cls )
1453 struct Plugin *plugin = cls;
1458 struct GNUNET_NETWORK_FDSet *grs;
1459 struct GNUNET_NETWORK_FDSet *gws;
1463 GNUNET_assert(cls !=NULL);
1466 /* Cancel previous scheduled task */
1467 if (plugin->http_curl_task != GNUNET_SCHEDULER_NO_TASK)
1469 GNUNET_SCHEDULER_cancel(plugin->env->sched, plugin->http_curl_task);
1470 plugin->http_curl_task = GNUNET_SCHEDULER_NO_TASK;
1477 mret = curl_multi_fdset (plugin->multi_handle, &rs, &ws, &es, &max);
1478 if (mret != CURLM_OK)
1480 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1481 _("%s failed at %s:%d: `%s'\n"),
1482 "curl_multi_fdset", __FILE__, __LINE__,
1483 curl_multi_strerror (mret));
1484 return GNUNET_SYSERR;
1486 mret = curl_multi_timeout (plugin->multi_handle, &to);
1487 if (mret != CURLM_OK)
1489 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1490 _("%s failed at %s:%d: `%s'\n"),
1491 "curl_multi_timeout", __FILE__, __LINE__,
1492 curl_multi_strerror (mret));
1493 return GNUNET_SYSERR;
1496 grs = GNUNET_NETWORK_fdset_create ();
1497 gws = GNUNET_NETWORK_fdset_create ();
1498 GNUNET_NETWORK_fdset_copy_native (grs, &rs, max + 1);
1499 GNUNET_NETWORK_fdset_copy_native (gws, &ws, max + 1);
1500 plugin->http_curl_task = GNUNET_SCHEDULER_add_select (plugin->env->sched,
1501 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1502 GNUNET_SCHEDULER_NO_TASK,
1503 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 0),
1508 GNUNET_NETWORK_fdset_destroy (gws);
1509 GNUNET_NETWORK_fdset_destroy (grs);
1514 * Function setting up curl handle and selecting message to send
1516 * @param ses session to send data to
1517 * @param con connection
1518 * @return GNUNET_SYSERR on failure, GNUNET_NO if connecting, GNUNET_YES if ok
1520 static ssize_t send_check_connections (void *cls, struct Session *ps)
1522 struct Plugin *plugin = cls;
1524 struct HTTP_Message * msg;
1526 struct GNUNET_TIME_Relative timeout = GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT;
1528 GNUNET_assert(cls !=NULL);
1530 if (ps->direction == OUTBOUND)
1532 /* RECV DIRECTION */
1533 /* Check if session is connected to receive data, otherwise connect to peer */
1534 if (ps->recv_connected == GNUNET_NO)
1536 if (ps->recv_endpoint == NULL)
1538 ps->recv_endpoint = curl_easy_init();
1540 curl_easy_setopt(ps->recv_endpoint, CURLOPT_VERBOSE, 1L);
1542 curl_easy_setopt(ps->recv_endpoint, CURLOPT_URL, ps->url);
1543 curl_easy_setopt(ps->recv_endpoint, CURLOPT_HEADERFUNCTION, &curl_get_header_cb);
1544 curl_easy_setopt(ps->recv_endpoint, CURLOPT_WRITEHEADER, ps);
1545 curl_easy_setopt(ps->recv_endpoint, CURLOPT_READFUNCTION, curl_send_cb);
1546 curl_easy_setopt(ps->recv_endpoint, CURLOPT_READDATA, ps);
1547 curl_easy_setopt(ps->recv_endpoint, CURLOPT_WRITEFUNCTION, curl_receive_cb);
1548 curl_easy_setopt(ps->recv_endpoint, CURLOPT_WRITEDATA, ps);
1549 curl_easy_setopt(ps->recv_endpoint, CURLOPT_TIMEOUT, (long) timeout.value);
1550 curl_easy_setopt(ps->recv_endpoint, CURLOPT_PRIVATE, ps);
1551 curl_easy_setopt(ps->recv_endpoint, CURLOPT_CONNECTTIMEOUT, HTTP_CONNECT_TIMEOUT);
1552 curl_easy_setopt(ps->recv_endpoint, CURLOPT_BUFFERSIZE, GNUNET_SERVER_MAX_MESSAGE_SIZE);
1554 mret = curl_multi_add_handle(plugin->multi_handle, ps->recv_endpoint);
1555 if (mret != CURLM_OK)
1557 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1558 _("Connection: %X: %s failed at %s:%d: `%s'\n"),
1560 "curl_multi_add_handle", __FILE__, __LINE__,
1561 curl_multi_strerror (mret));
1562 return GNUNET_SYSERR;
1564 if (curl_schedule (plugin) == GNUNET_SYSERR)
1565 return GNUNET_SYSERR;
1566 #if DEBUG_CONNECTIONS
1567 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: inbound not connected, initiating connection\n",ps);
1572 /* waiting for receive direction */
1573 if (ps->recv_connected==GNUNET_NO)
1576 /* SEND DIRECTION */
1577 /* Check if session is connected to send data, otherwise connect to peer */
1578 if ((ps->send_connected == GNUNET_YES) && (ps->send_endpoint!= NULL))
1580 if (ps->send_active == GNUNET_YES)
1582 #if DEBUG_CONNECTIONS
1583 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: outbound active, enqueueing message\n",ps);
1587 if (ps->send_active == GNUNET_NO)
1589 #if DEBUG_CONNECTIONS
1590 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: outbound paused, unpausing existing connection and enqueueing message\n",ps);
1592 if (CURLE_OK == curl_easy_pause(ps->send_endpoint,CURLPAUSE_CONT))
1594 ps->send_active=GNUNET_YES;
1598 return GNUNET_SYSERR;
1601 /* not connected, initiate connection */
1602 if ((ps->send_connected==GNUNET_NO) && (NULL == ps->send_endpoint))
1604 ps->send_endpoint = curl_easy_init();
1605 GNUNET_assert (ps->send_endpoint != NULL);
1606 GNUNET_assert (NULL != ps->pending_msgs_tail);
1607 #if DEBUG_CONNECTIONS
1608 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: outbound not connected, initiating connection\n",ps);
1610 ps->send_active = GNUNET_NO;
1611 msg = ps->pending_msgs_tail;
1614 curl_easy_setopt(ps->send_endpoint, CURLOPT_VERBOSE, 1L);
1616 curl_easy_setopt(ps->send_endpoint, CURLOPT_URL, ps->url);
1617 curl_easy_setopt(ps->send_endpoint, CURLOPT_PUT, 1L);
1618 curl_easy_setopt(ps->send_endpoint, CURLOPT_HEADERFUNCTION, &curl_put_header_cb);
1619 curl_easy_setopt(ps->send_endpoint, CURLOPT_WRITEHEADER, ps);
1620 curl_easy_setopt(ps->send_endpoint, CURLOPT_READFUNCTION, curl_send_cb);
1621 curl_easy_setopt(ps->send_endpoint, CURLOPT_READDATA, ps);
1622 curl_easy_setopt(ps->send_endpoint, CURLOPT_WRITEFUNCTION, curl_receive_cb);
1623 curl_easy_setopt(ps->send_endpoint, CURLOPT_READDATA, ps);
1624 curl_easy_setopt(ps->send_endpoint, CURLOPT_TIMEOUT, (long) timeout.value);
1625 curl_easy_setopt(ps->send_endpoint, CURLOPT_PRIVATE, ps);
1626 curl_easy_setopt(ps->send_endpoint, CURLOPT_CONNECTTIMEOUT, HTTP_CONNECT_TIMEOUT);
1627 curl_easy_setopt(ps->send_endpoint, CURLOPT_BUFFERSIZE, GNUNET_SERVER_MAX_MESSAGE_SIZE);
1629 mret = curl_multi_add_handle(plugin->multi_handle, ps->send_endpoint);
1630 if (mret != CURLM_OK)
1632 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1633 _("Connection: %X: %s failed at %s:%d: `%s'\n"),
1635 "curl_multi_add_handle", __FILE__, __LINE__,
1636 curl_multi_strerror (mret));
1637 return GNUNET_SYSERR;
1640 if (curl_schedule (plugin) == GNUNET_SYSERR)
1641 return GNUNET_SYSERR;
1644 if (ps->direction == INBOUND)
1646 GNUNET_assert (NULL != ps->pending_msgs_tail);
1647 if ((ps->recv_connected==GNUNET_YES) && (ps->send_connected==GNUNET_YES) &&
1648 (ps->recv_force_disconnect==GNUNET_NO) && (ps->recv_force_disconnect==GNUNET_NO))
1651 return GNUNET_SYSERR;
1654 static struct Session * send_select_session (void * cls, struct HTTP_PeerContext *pc, const void * addr, size_t addrlen, int force_address, struct Session * session)
1656 struct Session * tmp = NULL;
1657 int addr_given = GNUNET_NO;
1659 if ((addr!=NULL) && (addrlen>0))
1660 addr_given = GNUNET_YES;
1662 if (force_address == GNUNET_YES)
1664 /* check session given as argument */
1665 if ((session != NULL) && (addr_given == GNUNET_YES))
1667 if (0 == memcmp(session->addr, addr, addrlen))
1669 /* connection can not be used, since it is disconnected */
1670 if ((session->recv_force_disconnect==GNUNET_NO) && (session->send_force_disconnect==GNUNET_NO))
1672 #if DEBUG_SESSION_SELECTION
1673 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Session %X selected: Using session passed by transport to send to forced address \n", session);
1679 /* check last session used */
1680 if ((pc->last_session != NULL)&& (addr_given == GNUNET_YES))
1682 if (0 == memcmp(pc->last_session->addr, addr, addrlen))
1684 /* connection can not be used, since it is disconnected */
1685 if ((pc->last_session->recv_force_disconnect==GNUNET_NO) && (pc->last_session->send_force_disconnect==GNUNET_NO))
1687 #if DEBUG_SESSION_SELECTION
1688 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Session %X selected: Using last session used to send to forced address \n", pc->last_session);
1690 return pc->last_session;
1694 /* find session in existing sessions */
1696 while ((tmp!=NULL) && (addr_given == GNUNET_YES))
1699 if (0 == memcmp(tmp->addr, addr, addrlen))
1701 /* connection can not be used, since it is disconnected */
1702 if ((tmp->recv_force_disconnect==GNUNET_NO) && (tmp->send_force_disconnect==GNUNET_NO))
1704 #if DEBUG_SESSION_SELECTION
1705 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Session %X selected: Using existing session to send to forced address \n", session);
1713 /* no session to use */
1716 if ((force_address == GNUNET_NO) || (force_address == GNUNET_SYSERR))
1718 /* check session given as argument */
1719 if (session != NULL)
1721 /* connection can not be used, since it is disconnected */
1722 if ((session->recv_force_disconnect==GNUNET_NO) && (session->send_force_disconnect==GNUNET_NO))
1724 #if DEBUG_SESSION_SELECTION
1725 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Session %X selected: Using session passed by transport to send not-forced address \n", session);
1731 /* check last session used */
1732 if (pc->last_session != NULL)
1734 /* connection can not be used, since it is disconnected */
1735 if ((pc->last_session->recv_force_disconnect==GNUNET_NO) && (pc->last_session->send_force_disconnect==GNUNET_NO))
1737 #if DEBUG_SESSION_SELECTION
1738 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Session %X selected: Using last session to send to not-forced address \n", pc->last_session);
1740 return pc->last_session;
1743 /* find session in existing sessions */
1747 /* connection can not be used, since it is disconnected */
1748 if ((tmp->recv_force_disconnect==GNUNET_NO) && (tmp->send_force_disconnect==GNUNET_NO))
1750 #if DEBUG_SESSION_SELECTION
1751 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Session %X selected: Using existing session to send to not-forced address \n", tmp);
1763 * Function that can be used by the transport service to transmit
1764 * a message using the plugin. Note that in the case of a
1765 * peer disconnecting, the continuation MUST be called
1766 * prior to the disconnect notification itself. This function
1767 * will be called with this peer's HELLO message to initiate
1768 * a fresh connection to another peer.
1770 * @param cls closure
1771 * @param target who should receive this message
1772 * @param msgbuf the message to transmit
1773 * @param msgbuf_size number of bytes in 'msgbuf'
1774 * @param priority how important is the message (most plugins will
1775 * ignore message priority and just FIFO)
1776 * @param timeout how long to wait at most for the transmission (does not
1777 * require plugins to discard the message after the timeout,
1778 * just advisory for the desired delay; most plugins will ignore
1780 * @param session which session must be used (or NULL for "any")
1781 * @param addr the address to use (can be NULL if the plugin
1782 * is "on its own" (i.e. re-use existing TCP connection))
1783 * @param addrlen length of the address in bytes
1784 * @param force_address GNUNET_YES if the plugin MUST use the given address,
1785 * GNUNET_NO means the plugin may use any other address and
1786 * GNUNET_SYSERR means that only reliable existing
1787 * bi-directional connections should be used (regardless
1789 * @param cont continuation to call once the message has
1790 * been transmitted (or if the transport is ready
1791 * for the next transmission call; or if the
1792 * peer disconnected...); can be NULL
1793 * @param cont_cls closure for cont
1794 * @return number of bytes used (on the physical network, with overheads);
1795 * -1 on hard errors (i.e. address invalid); 0 is a legal value
1796 * and does NOT mean that the message was not transmitted (DV)
1799 http_plugin_send (void *cls,
1800 const struct GNUNET_PeerIdentity *target,
1803 unsigned int priority,
1804 struct GNUNET_TIME_Relative to,
1805 struct Session *session,
1809 GNUNET_TRANSPORT_TransmitContinuation cont,
1812 struct Plugin *plugin = cls;
1813 struct HTTP_Message *msg;
1814 struct HTTP_PeerContext * pc;
1815 struct Session * ps = NULL;
1817 GNUNET_assert(cls !=NULL);
1820 char * force = GNUNET_malloc(40);
1821 if (force_address == GNUNET_YES)
1822 strcpy(force,"forced addr.");
1823 if (force_address == GNUNET_NO)
1824 strcpy(force,"any addr.");
1825 if (force_address == GNUNET_SYSERR)
1826 strcpy(force,"reliable bi-direc. address addr.");
1828 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Transport tells me to send %u bytes to `%s' using %s (%s) and session: %X\n",
1832 http_plugin_address_to_string(NULL, addr, addrlen),
1838 pc = GNUNET_CONTAINER_multihashmap_get (plugin->peers, &target->hashPubKey);
1842 pc = GNUNET_malloc(sizeof (struct HTTP_PeerContext));
1843 pc->plugin = plugin;
1844 pc->session_id_counter=1;
1845 pc->last_session = NULL;
1846 memcpy(&pc->identity, target, sizeof(struct GNUNET_PeerIdentity));
1847 GNUNET_CONTAINER_multihashmap_put(plugin->peers, &pc->identity.hashPubKey, pc, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
1850 ps = send_select_session (plugin, pc, addr, addrlen, force_address, session);
1852 /* session not existing, but address forced -> creating new session */
1855 if ((addr!=NULL) && (addrlen!=0))
1857 ps = GNUNET_malloc(sizeof (struct Session));
1858 #if DEBUG_SESSION_SELECTION
1859 if (force_address == GNUNET_YES)
1860 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"No existing connection & forced address: creating new session %X to peer %s\n", ps, GNUNET_i2s(target));
1861 if (force_address != GNUNET_YES)
1862 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"No existing connection: creating new session %X to peer %s\n", ps, GNUNET_i2s(target));
1864 if ((addrlen!=0) && (addr!=NULL))
1866 ps->addr = GNUNET_malloc(addrlen);
1867 memcpy(ps->addr,addr,addrlen);
1868 ps->addrlen = addrlen;
1875 ps->direction=OUTBOUND;
1876 ps->recv_connected = GNUNET_NO;
1877 ps->recv_force_disconnect = GNUNET_NO;
1878 ps->send_connected = GNUNET_NO;
1879 ps->send_force_disconnect = GNUNET_NO;
1880 ps->pending_msgs_head = NULL;
1881 ps->pending_msgs_tail = NULL;
1883 ps->session_id = pc->session_id_counter;
1884 pc->session_id_counter++;
1885 ps->url = create_url (plugin, ps->addr, ps->addrlen, ps->session_id);
1886 if (ps->msgtok == NULL)
1887 ps->msgtok = GNUNET_SERVER_mst_create (&curl_receive_mst_cb, ps);
1888 GNUNET_CONTAINER_DLL_insert(pc->head,pc->tail,ps);
1893 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));
1895 return GNUNET_SYSERR;
1900 msg = GNUNET_malloc (sizeof (struct HTTP_Message) + msgbuf_size);
1902 msg->size = msgbuf_size;
1904 msg->buf = (char *) &msg[1];
1905 msg->transmit_cont = cont;
1906 msg->transmit_cont_cls = cont_cls;
1907 memcpy (msg->buf,msgbuf, msgbuf_size);
1908 GNUNET_CONTAINER_DLL_insert(ps->pending_msgs_head,ps->pending_msgs_tail,msg);
1910 if (send_check_connections (plugin, ps) != GNUNET_SYSERR)
1912 if (force_address != GNUNET_YES)
1913 pc->last_session = ps;
1915 if (pc->last_session==NULL)
1916 pc->last_session = ps;
1920 return GNUNET_SYSERR;
1926 * Function that can be used to force the plugin to disconnect
1927 * from the given peer and cancel all previous transmissions
1928 * (and their continuationc).
1930 * @param cls closure
1931 * @param target peer from which to disconnect
1934 http_plugin_disconnect (void *cls,
1935 const struct GNUNET_PeerIdentity *target)
1939 struct Plugin *plugin = cls;
1940 struct HTTP_PeerContext *pc = NULL;
1941 struct Session *ps = NULL;
1942 //struct Session *tmp = NULL;
1944 pc = GNUNET_CONTAINER_multihashmap_get (plugin->peers, &target->hashPubKey);
1951 /* Telling transport that session is getting disconnected */
1952 plugin->env->session_end(plugin, target, ps);
1953 if (ps->direction==OUTBOUND)
1955 if (ps->send_endpoint!=NULL)
1957 //GNUNET_assert(CURLM_OK == curl_multi_remove_handle(plugin->multi_handle,ps->send_endpoint));
1958 //curl_easy_cleanup(ps->send_endpoint);
1959 //ps->send_endpoint=NULL;
1960 ps->send_force_disconnect = GNUNET_YES;
1962 if (ps->recv_endpoint!=NULL)
1964 //GNUNET_assert(CURLM_OK == curl_multi_remove_handle(plugin->multi_handle,ps->recv_endpoint));
1965 //curl_easy_cleanup(ps->recv_endpoint);
1966 //ps->recv_endpoint=NULL;
1967 ps->recv_force_disconnect = GNUNET_YES;
1971 if (ps->direction==INBOUND)
1973 ps->recv_force_disconnect = GNUNET_YES;
1974 ps->send_force_disconnect = GNUNET_YES;
1977 while (ps->pending_msgs_head!=NULL)
1979 remove_http_message(ps, ps->pending_msgs_head);
1981 ps->recv_active = GNUNET_NO;
1982 ps->send_active = GNUNET_NO;
1989 * Convert the transports address to a nice, human-readable
1992 * @param cls closure
1993 * @param type name of the transport that generated the address
1994 * @param addr one of the addresses of the host, NULL for the last address
1995 * the specific address format depends on the transport
1996 * @param addrlen length of the address
1997 * @param numeric should (IP) addresses be displayed in numeric form?
1998 * @param timeout after how long should we give up?
1999 * @param asc function to call on each string
2000 * @param asc_cls closure for asc
2003 http_plugin_address_pretty_printer (void *cls,
2008 struct GNUNET_TIME_Relative timeout,
2009 GNUNET_TRANSPORT_AddressStringCallback
2012 const struct IPv4HttpAddress *t4;
2013 const struct IPv6HttpAddress *t6;
2014 struct sockaddr_in a4;
2015 struct sockaddr_in6 a6;
2021 GNUNET_assert(cls !=NULL);
2022 if (addrlen == sizeof (struct IPv6HttpAddress))
2024 address = GNUNET_malloc (INET6_ADDRSTRLEN);
2026 a6.sin6_addr = t6->ipv6_addr;
2027 inet_ntop(AF_INET6, &(a6.sin6_addr),address,INET6_ADDRSTRLEN);
2028 port = ntohs(t6->u6_port);
2030 else if (addrlen == sizeof (struct IPv4HttpAddress))
2032 address = GNUNET_malloc (INET_ADDRSTRLEN);
2034 a4.sin_addr.s_addr = t4->ipv4_addr;
2035 inet_ntop(AF_INET, &(a4.sin_addr),address,INET_ADDRSTRLEN);
2036 port = ntohs(t4->u_port);
2040 /* invalid address */
2041 GNUNET_break_op (0);
2042 asc (asc_cls, NULL);
2045 res = GNUNET_asprintf(&ret,"http://%s:%u/",address,port);
2046 GNUNET_free (address);
2047 GNUNET_assert(res != 0);
2049 GNUNET_free_non_null (ret);
2055 * Another peer has suggested an address for this
2056 * peer and transport plugin. Check that this could be a valid
2057 * address. If so, consider adding it to the list
2060 * @param cls closure
2061 * @param addr pointer to the address
2062 * @param addrlen length of addr
2063 * @return GNUNET_OK if this is a plausible address for this peer
2067 http_plugin_address_suggested (void *cls,
2068 const void *addr, size_t addrlen)
2070 struct Plugin *plugin = cls;
2071 struct IPv4HttpAddress *v4;
2072 struct IPv6HttpAddress *v6;
2075 GNUNET_assert(cls !=NULL);
2076 if ((addrlen != sizeof (struct IPv4HttpAddress)) &&
2077 (addrlen != sizeof (struct IPv6HttpAddress)))
2079 return GNUNET_SYSERR;
2081 if (addrlen == sizeof (struct IPv4HttpAddress))
2083 v4 = (struct IPv4HttpAddress *) addr;
2084 /* Not skipping loopback
2085 if (INADDR_LOOPBACK == ntohl(v4->ipv4_addr))
2087 return GNUNET_SYSERR;
2089 port = ntohs (v4->u_port);
2090 if (port != plugin->port_inbound)
2092 return GNUNET_SYSERR;
2095 if (addrlen == sizeof (struct IPv6HttpAddress))
2097 v6 = (struct IPv6HttpAddress *) addr;
2098 if (IN6_IS_ADDR_LINKLOCAL (&v6->ipv6_addr))
2100 return GNUNET_SYSERR;
2102 port = ntohs (v6->u6_port);
2103 if (port != plugin->port_inbound)
2105 return GNUNET_SYSERR;
2114 * Function called for a quick conversion of the binary address to
2115 * a numeric address. Note that the caller must not free the
2116 * address and that the next call to this function is allowed
2117 * to override the address again.
2119 * @param cls closure
2120 * @param addr binary address
2121 * @param addrlen length of the address
2122 * @return string representing the same address
2125 http_plugin_address_to_string (void *cls,
2129 const struct IPv4HttpAddress *t4;
2130 const struct IPv6HttpAddress *t6;
2131 struct sockaddr_in a4;
2132 struct sockaddr_in6 a6;
2138 if (addrlen == sizeof (struct IPv6HttpAddress))
2140 address = GNUNET_malloc (INET6_ADDRSTRLEN);
2142 a6.sin6_addr = t6->ipv6_addr;
2143 inet_ntop(AF_INET6, &(a6.sin6_addr),address,INET6_ADDRSTRLEN);
2144 port = ntohs(t6->u6_port);
2146 else if (addrlen == sizeof (struct IPv4HttpAddress))
2148 address = GNUNET_malloc (INET_ADDRSTRLEN);
2150 a4.sin_addr.s_addr = t4->ipv4_addr;
2151 inet_ntop(AF_INET, &(a4.sin_addr),address,INET_ADDRSTRLEN);
2152 port = ntohs(t4->u_port);
2156 /* invalid address */
2159 res = GNUNET_asprintf(&ret,"%s:%u",address,port);
2160 GNUNET_free (address);
2161 GNUNET_assert(res != 0);
2167 * Exit point from the plugin.
2170 libgnunet_plugin_transport_http_done (void *cls)
2172 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
2173 struct Plugin *plugin = api->cls;
2175 GNUNET_assert(cls !=NULL);
2177 if (plugin->http_server_daemon_v4 != NULL)
2179 MHD_stop_daemon (plugin->http_server_daemon_v4);
2180 plugin->http_server_daemon_v4 = NULL;
2182 if (plugin->http_server_daemon_v6 != NULL)
2184 MHD_stop_daemon (plugin->http_server_daemon_v6);
2185 plugin->http_server_daemon_v6 = NULL;
2188 if ( plugin->http_server_task_v4 != GNUNET_SCHEDULER_NO_TASK)
2190 GNUNET_SCHEDULER_cancel(plugin->env->sched, plugin->http_server_task_v4);
2191 plugin->http_server_task_v4 = GNUNET_SCHEDULER_NO_TASK;
2194 if ( plugin->http_server_task_v6 != GNUNET_SCHEDULER_NO_TASK)
2196 GNUNET_SCHEDULER_cancel(plugin->env->sched, plugin->http_server_task_v6);
2197 plugin->http_server_task_v6 = GNUNET_SCHEDULER_NO_TASK;
2201 /* free all peer information */
2202 if (plugin->peers!=NULL)
2204 GNUNET_CONTAINER_multihashmap_iterate (plugin->peers,
2205 &remove_peer_context_Iterator,
2207 GNUNET_CONTAINER_multihashmap_destroy (plugin->peers);
2209 if (plugin->multi_handle!=NULL)
2211 mret = curl_multi_cleanup(plugin->multi_handle);
2213 if ( CURLM_OK != mret)
2214 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"curl multihandle clean up failed\n");
2216 plugin->multi_handle = NULL;
2218 curl_global_cleanup();
2220 if ( plugin->http_curl_task != GNUNET_SCHEDULER_NO_TASK)
2222 GNUNET_SCHEDULER_cancel(plugin->env->sched, plugin->http_curl_task);
2223 plugin->http_curl_task = GNUNET_SCHEDULER_NO_TASK;
2226 GNUNET_free_non_null (plugin->bind4_address);
2227 GNUNET_free_non_null (plugin->bind6_address);
2228 GNUNET_free_non_null(plugin->bind_hostname);
2229 GNUNET_free (plugin);
2232 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Unload http plugin complete...\n");
2239 * Entry point for the plugin.
2242 libgnunet_plugin_transport_http_init (void *cls)
2244 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
2245 struct Plugin *plugin;
2246 struct GNUNET_TRANSPORT_PluginFunctions *api;
2247 struct GNUNET_TIME_Relative gn_timeout;
2248 long long unsigned int port;
2250 GNUNET_assert(cls !=NULL);
2252 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Starting http plugin...\n");
2255 plugin = GNUNET_malloc (sizeof (struct Plugin));
2256 plugin->stats = env->stats;
2258 plugin->peers = NULL;
2259 plugin->bind4_address = NULL;
2260 plugin->use_ipv6 = GNUNET_YES;
2261 plugin->use_ipv4 = GNUNET_YES;
2263 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
2265 api->send = &http_plugin_send;
2266 api->disconnect = &http_plugin_disconnect;
2267 api->address_pretty_printer = &http_plugin_address_pretty_printer;
2268 api->check_address = &http_plugin_address_suggested;
2269 api->address_to_string = &http_plugin_address_to_string;
2271 /* Hashing our identity to use it in URLs */
2272 GNUNET_CRYPTO_hash_to_enc ( &(plugin->env->my_identity->hashPubKey), &plugin->my_ascii_hash_ident);
2274 /* Reading port number from config file */
2275 if (GNUNET_CONFIGURATION_have_value (env->cfg,
2276 "transport-http", "USE_IPv6"))
2278 plugin->use_ipv6 = GNUNET_CONFIGURATION_get_value_yesno (env->cfg,
2282 /* Reading port number from config file */
2283 if (GNUNET_CONFIGURATION_have_value (env->cfg,
2284 "transport-http", "USE_IPv4"))
2286 plugin->use_ipv4 = GNUNET_CONFIGURATION_get_value_yesno (env->cfg,
2290 /* Reading port number from config file */
2292 GNUNET_CONFIGURATION_get_value_number (env->cfg,
2298 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
2300 _("Require valid port number for transport plugin `%s' in configuration!\n"),
2302 libgnunet_plugin_transport_http_done (api);
2306 /* Reading ipv4 addresse to bind to from config file */
2307 if ((plugin->use_ipv4==GNUNET_YES) && (GNUNET_CONFIGURATION_have_value (env->cfg,
2308 "transport-http", "BINDTO4")))
2310 GNUNET_break (GNUNET_OK ==
2311 GNUNET_CONFIGURATION_get_value_string (env->cfg,
2314 &plugin->bind_hostname));
2315 plugin->bind4_address = GNUNET_malloc(sizeof(struct sockaddr_in));
2316 plugin->bind4_address->sin_family = AF_INET;
2317 plugin->bind4_address->sin_port = htons (port);
2319 if (inet_pton(AF_INET,plugin->bind_hostname, &plugin->bind4_address->sin_addr)<=0)
2321 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
2323 _("Misconfigured address to bind to in configuration!\n"),
2325 GNUNET_free(plugin->bind4_address);
2326 GNUNET_free(plugin->bind_hostname);
2327 plugin->bind_hostname = NULL;
2328 plugin->bind4_address = NULL;
2332 /* Reading ipv4 addresse to bind to from config file */
2333 if ((plugin->use_ipv6==GNUNET_YES) && (GNUNET_CONFIGURATION_have_value (env->cfg,
2334 "transport-http", "BINDTO6")))
2336 GNUNET_break (GNUNET_OK ==
2337 GNUNET_CONFIGURATION_get_value_string (env->cfg,
2340 &plugin->bind_hostname));
2342 plugin->bind6_address = GNUNET_malloc(sizeof(struct sockaddr_in6));
2343 plugin->bind6_address->sin6_family = AF_INET6;
2344 plugin->bind6_address->sin6_port = htons (port);
2346 if (inet_pton(AF_INET6,plugin->bind_hostname, &plugin->bind6_address->sin6_addr)<=0)
2348 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
2350 _("Misconfigured address to bind to in configuration!\n"),
2352 GNUNET_free(plugin->bind6_address);
2353 GNUNET_free(plugin->bind_hostname);
2354 plugin->bind_hostname = NULL;
2355 plugin->bind6_address = NULL;
2359 GNUNET_assert ((port > 0) && (port <= 65535));
2360 plugin->port_inbound = port;
2361 gn_timeout = GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT;
2362 unsigned int timeout = (gn_timeout.value) / 1000;
2363 if ((plugin->http_server_daemon_v6 == NULL) && (plugin->use_ipv6 == GNUNET_YES) && (port != 0))
2365 struct sockaddr * tmp = (struct sockaddr *) plugin->bind6_address;
2366 plugin->http_server_daemon_v6 = MHD_start_daemon (
2367 #if DEBUG_CONNECTIONS
2373 plugin , &mdh_access_cb, plugin,
2374 MHD_OPTION_SOCK_ADDR, tmp,
2375 MHD_OPTION_CONNECTION_LIMIT, (unsigned int) 32,
2376 //MHD_OPTION_PER_IP_CONNECTION_LIMIT, (unsigned int) 6,
2377 MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) timeout,
2378 MHD_OPTION_CONNECTION_MEMORY_LIMIT, (size_t) (16 * 1024),
2379 MHD_OPTION_NOTIFY_COMPLETED, &mhd_termination_cb, NULL,
2382 if ((plugin->http_server_daemon_v4 == NULL) && (plugin->use_ipv4 == GNUNET_YES) && (port != 0))
2384 plugin->http_server_daemon_v4 = MHD_start_daemon (
2385 #if DEBUG_CONNECTIONS
2391 plugin , &mdh_access_cb, plugin,
2392 MHD_OPTION_SOCK_ADDR, (struct sockaddr_in *)plugin->bind4_address,
2393 MHD_OPTION_CONNECTION_LIMIT, (unsigned int) 32,
2394 //MHD_OPTION_PER_IP_CONNECTION_LIMIT, (unsigned int) 6,
2395 MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) timeout,
2396 MHD_OPTION_CONNECTION_MEMORY_LIMIT, (size_t) (16 * 1024),
2397 MHD_OPTION_NOTIFY_COMPLETED, &mhd_termination_cb, NULL,
2400 if (plugin->http_server_daemon_v4 != NULL)
2401 plugin->http_server_task_v4 = http_server_daemon_prepare (plugin, plugin->http_server_daemon_v4);
2402 if (plugin->http_server_daemon_v6 != NULL)
2403 plugin->http_server_task_v6 = http_server_daemon_prepare (plugin, plugin->http_server_daemon_v6);
2406 if (plugin->http_server_task_v4 != GNUNET_SCHEDULER_NO_TASK)
2409 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);
2412 else if ((plugin->http_server_task_v6 != GNUNET_SCHEDULER_NO_TASK) && (plugin->http_server_task_v4 != GNUNET_SCHEDULER_NO_TASK))
2415 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);
2418 else if ((plugin->http_server_task_v6 != GNUNET_SCHEDULER_NO_TASK) && (plugin->http_server_task_v4 == GNUNET_SCHEDULER_NO_TASK))
2421 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);
2427 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"No MHD was started, transport plugin not functional!\n");
2429 libgnunet_plugin_transport_http_done (api);
2433 /* Initializing cURL */
2434 curl_global_init(CURL_GLOBAL_ALL);
2435 plugin->multi_handle = curl_multi_init();
2437 if ( NULL == plugin->multi_handle )
2439 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
2441 _("Could not initialize curl multi handle, failed to start http plugin!\n"),
2443 libgnunet_plugin_transport_http_done (api);
2447 plugin->peers = GNUNET_CONTAINER_multihashmap_create (10);
2448 GNUNET_OS_network_interfaces_list (&process_interfaces, plugin);
2453 /* end of plugin_transport_http.c */