2 This file is part of GNUnet
3 (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file transport/plugin_transport_http.c
23 * @brief http transport service plugin
24 * @author Matthias Wachs
28 #include "gnunet_common.h"
29 #include "gnunet_constants.h"
30 #include "gnunet_protocols.h"
31 #include "gnunet_connection_lib.h"
32 #include "gnunet_service_lib.h"
33 #include "gnunet_statistics_service.h"
34 #include "gnunet_transport_service.h"
35 #include "gnunet_resolver_service.h"
36 #include "gnunet_server_lib.h"
37 #include "gnunet_container_lib.h"
38 #include "plugin_transport.h"
39 #include "gnunet_os_lib.h"
40 #include "microhttpd.h"
41 #include <curl/curl.h>
43 #define DEBUG_HTTP GNUNET_YES
44 #define DEBUG_CURL GNUNET_YES
45 #define DEBUG_CONNECTIONS GNUNET_YES
46 #define DEBUG_SESSION_SELECTION GNUNET_YES
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;
1089 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: curl_get_header_cb\n",ps);
1092 size_t len = size * nmemb;
1093 long http_result = 0;
1095 /* Getting last http result code */
1096 if (ps->recv_connected==GNUNET_NO)
1098 GNUNET_assert(NULL!=ps);
1099 res = curl_easy_getinfo(ps->recv_endpoint, CURLINFO_RESPONSE_CODE, &http_result);
1100 if (CURLE_OK == res)
1102 if (http_result == 200)
1104 ps->recv_connected = GNUNET_YES;
1105 ps->recv_active = GNUNET_YES;
1106 #if DEBUG_CONNECTIONS
1107 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: connected to recieve data\n",ps);
1109 // Calling send_check_connections again since receive is established
1110 send_check_connections (ps->peercontext->plugin, ps);
1116 if ((size * nmemb) < SIZE_MAX)
1117 tmp = GNUNET_malloc (len+1);
1119 if ((tmp != NULL) && (len > 0))
1121 memcpy(tmp,ptr,len);
1124 if (tmp[len-2] == 13)
1127 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: Header: %s\n",ps,tmp);
1132 return size * nmemb;
1135 static size_t curl_put_header_cb( void *ptr, size_t size, size_t nmemb, void *stream)
1137 struct Session * ps = stream;
1140 size_t len = size * nmemb;
1141 long http_result = 0;
1144 /* Getting last http result code */
1145 GNUNET_assert(NULL!=ps);
1146 res = curl_easy_getinfo(ps->send_endpoint, CURLINFO_RESPONSE_CODE, &http_result);
1147 if (CURLE_OK == res)
1149 if ((http_result == 100) && (ps->send_connected==GNUNET_NO))
1151 ps->send_connected = GNUNET_YES;
1152 ps->send_active = GNUNET_YES;
1153 #if DEBUG_CONNECTIONS
1154 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: connected to send data\n",ps);
1157 if ((http_result == 200) && (ps->send_connected==GNUNET_YES))
1159 ps->send_connected = GNUNET_NO;
1160 ps->send_active = GNUNET_NO;
1161 #if DEBUG_CONNECTIONS
1162 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: sending disconnected\n",ps);
1168 if ((size * nmemb) < SIZE_MAX)
1169 tmp = GNUNET_malloc (len+1);
1171 if ((tmp != NULL) && (len > 0))
1173 memcpy(tmp,ptr,len);
1176 if (tmp[len-2] == 13)
1183 return size * nmemb;
1187 * Callback method used with libcurl
1188 * Method is called when libcurl needs to read data during sending
1189 * @param stream pointer where to write data
1190 * @param size size of an individual element
1191 * @param nmemb count of elements that can be written to the buffer
1192 * @param ptr source pointer, passed to the libcurl handle
1193 * @return bytes written to stream
1195 static size_t curl_send_cb(void *stream, size_t size, size_t nmemb, void *ptr)
1197 struct Session * ps = ptr;
1198 struct HTTP_Message * msg = ps->pending_msgs_tail;
1202 if (ps->send_active == GNUNET_NO)
1203 return CURL_READFUNC_PAUSE;
1206 if ((ps->pending_msgs_tail == NULL) && (ps->send_active == GNUNET_YES))
1208 #if DEBUG_CONNECTIONS
1209 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: No Message to send, pausing connection\n",ps);
1211 ps->send_active = GNUNET_NO;
1212 return CURL_READFUNC_PAUSE;
1215 msg = ps->pending_msgs_tail;
1217 if (msg->pos < msg->size)
1219 /* data fit in buffer */
1220 if ((msg->size - msg->pos) <= (size * nmemb))
1222 len = (msg->size - msg->pos);
1223 memcpy(stream, &msg->buf[msg->pos], len);
1230 memcpy(stream, &msg->buf[msg->pos], len);
1235 /* no data to send */
1241 if ( msg->pos == msg->size)
1243 #if DEBUG_CONNECTIONS
1244 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: Message with %u bytes sent, removing message from queue \n",ps, msg->pos);
1246 /* Calling transmit continuation */
1247 if (( NULL != ps->pending_msgs_tail) && (NULL != ps->pending_msgs_tail->transmit_cont))
1248 msg->transmit_cont (ps->pending_msgs_tail->transmit_cont_cls,&(ps->peercontext)->identity,GNUNET_OK);
1249 remove_http_message(ps, msg);
1254 static void curl_receive_mst_cb (void *cls,
1256 const struct GNUNET_MessageHeader *message)
1258 struct Session *ps = cls;
1259 struct HTTP_PeerContext *pc = ps->peercontext;
1260 GNUNET_assert(ps != NULL);
1261 GNUNET_assert(pc != NULL);
1263 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1264 "Connection %X: Forwarding message to transport service, type %u and size %u from `%s' (`%s')\n",
1266 ntohs(message->type),
1267 ntohs(message->size),
1268 GNUNET_i2s(&(pc->identity)),http_plugin_address_to_string(NULL,ps->addr,ps->addrlen));
1270 pc->plugin->env->receive (pc->plugin->env->cls,
1279 * Callback method used with libcurl
1280 * Method is called when libcurl needs to write data during sending
1281 * @param stream pointer where to write data
1282 * @param size size of an individual element
1283 * @param nmemb count of elements that can be written to the buffer
1284 * @param ptr destination pointer, passed to the libcurl handle
1285 * @return bytes read from stream
1287 static size_t curl_receive_cb( void *stream, size_t size, size_t nmemb, void *ptr)
1289 struct Session * ps = ptr;
1290 #if DEBUG_CONNECTIONS
1291 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: %u bytes received\n",ps, size*nmemb);
1293 GNUNET_SERVER_mst_receive(ps->msgtok, ps, stream, size*nmemb, GNUNET_NO, GNUNET_NO);
1294 return (size * nmemb);
1298 static void curl_perform (void *cls,
1299 const struct GNUNET_SCHEDULER_TaskContext *tc)
1301 struct Plugin *plugin = cls;
1302 static unsigned int handles_last_run;
1304 struct CURLMsg *msg;
1306 struct Session *ps = NULL;
1307 struct HTTP_PeerContext *pc = NULL;
1308 struct HTTP_Message * cur_msg = NULL;
1312 GNUNET_assert(cls !=NULL);
1314 plugin->http_curl_task = GNUNET_SCHEDULER_NO_TASK;
1315 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1321 mret = curl_multi_perform (plugin->multi_handle, &running);
1322 if ((running < handles_last_run) && (running>0))
1327 msg = curl_multi_info_read (plugin->multi_handle, &running);
1330 /* get session for affected curl handle */
1331 GNUNET_assert ( msg->easy_handle != NULL );
1332 curl_easy_getinfo(msg->easy_handle, CURLINFO_PRIVATE, &tmp);
1333 ps = (struct Session *) tmp;
1334 GNUNET_assert ( ps != NULL );
1335 pc = ps->peercontext;
1336 GNUNET_assert ( pc != NULL );
1341 if ( (msg->data.result != CURLE_OK) &&
1342 (msg->data.result != CURLE_GOT_NOTHING) )
1344 /* sending msg failed*/
1345 if (msg->easy_handle == ps->send_endpoint)
1347 #if DEBUG_CONNECTIONS
1348 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1349 _("Connection %X: HTTP PUT to peer `%s' (`%s') failed: `%s' `%s'\n"),
1351 GNUNET_i2s(&pc->identity),
1352 http_plugin_address_to_string(NULL, ps->addr, ps->addrlen),
1353 "curl_multi_perform",
1354 curl_easy_strerror (msg->data.result));
1356 ps->send_connected = GNUNET_NO;
1357 ps->send_active = GNUNET_NO;
1358 curl_multi_remove_handle(plugin->multi_handle,ps->send_endpoint);
1359 curl_easy_cleanup(ps->send_endpoint);
1360 ps->send_endpoint=NULL;
1361 cur_msg = ps->pending_msgs_tail;
1362 if (( NULL != cur_msg) && ( NULL != cur_msg->transmit_cont))
1363 cur_msg->transmit_cont (cur_msg->transmit_cont_cls,&pc->identity,GNUNET_SYSERR);
1365 /* GET connection failed */
1366 if (msg->easy_handle == ps->recv_endpoint)
1368 #if DEBUG_CONNECTIONS
1369 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1370 _("Connection %X: HTTP GET to peer `%s' (`%s') failed: `%s' `%s'\n"),
1372 GNUNET_i2s(&pc->identity),
1373 http_plugin_address_to_string(NULL, ps->addr, ps->addrlen),
1374 "curl_multi_perform",
1375 curl_easy_strerror (msg->data.result));
1377 ps->recv_connected = GNUNET_NO;
1378 ps->recv_active = GNUNET_NO;
1379 curl_multi_remove_handle(plugin->multi_handle,ps->recv_endpoint);
1380 curl_easy_cleanup(ps->recv_endpoint);
1381 ps->recv_endpoint=NULL;
1386 if (msg->easy_handle == ps->send_endpoint)
1388 GNUNET_assert (CURLE_OK == curl_easy_getinfo(msg->easy_handle, CURLINFO_RESPONSE_CODE, &http_result));
1389 #if DEBUG_CONNECTIONS
1390 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1391 "Connection %X: HTTP PUT connection to peer `%s' (`%s') was closed with HTTP code %u\n",
1393 GNUNET_i2s(&pc->identity),
1394 http_plugin_address_to_string(NULL, ps->addr, ps->addrlen),
1397 /* Calling transmit continuation */
1398 cur_msg = ps->pending_msgs_tail;
1399 if (( NULL != cur_msg) && (NULL != cur_msg->transmit_cont))
1401 /* HTTP 1xx : Last message before here was informational */
1402 if ((http_result >=100) && (http_result < 200))
1403 cur_msg->transmit_cont (cur_msg->transmit_cont_cls,&pc->identity,GNUNET_OK);
1404 /* HTTP 2xx: successful operations */
1405 if ((http_result >=200) && (http_result < 300))
1406 cur_msg->transmit_cont (cur_msg->transmit_cont_cls,&pc->identity,GNUNET_OK);
1407 /* HTTP 3xx..5xx: error */
1408 if ((http_result >=300) && (http_result < 600))
1409 cur_msg->transmit_cont (cur_msg->transmit_cont_cls,&pc->identity,GNUNET_SYSERR);
1411 ps->send_connected = GNUNET_NO;
1412 ps->send_active = GNUNET_NO;
1413 curl_multi_remove_handle(plugin->multi_handle,ps->send_endpoint);
1414 curl_easy_cleanup(ps->send_endpoint);
1415 ps->send_endpoint =NULL;
1417 if (msg->easy_handle == ps->recv_endpoint)
1419 #if DEBUG_CONNECTIONS
1420 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1421 "Connection %X: HTTP GET connection to peer `%s' (`%s') was closed with HTTP code %u\n",
1423 GNUNET_i2s(&pc->identity),
1424 http_plugin_address_to_string(NULL, ps->addr, ps->addrlen),
1427 ps->recv_connected = GNUNET_NO;
1428 ps->recv_active = GNUNET_NO;
1429 curl_multi_remove_handle(plugin->multi_handle,ps->recv_endpoint);
1430 curl_easy_cleanup(ps->recv_endpoint);
1431 ps->recv_endpoint=NULL;
1434 if ((ps->recv_connected == GNUNET_NO) && (ps->send_connected == GNUNET_NO))
1435 remove_session (pc, ps, GNUNET_YES, GNUNET_SYSERR);
1442 while ( (running > 0) );
1444 handles_last_run = running;
1446 while (mret == CURLM_CALL_MULTI_PERFORM);
1447 curl_schedule(plugin);
1452 * Function setting up file descriptors and scheduling task to run
1453 * @param ses session to send data to
1454 * @return GNUNET_SYSERR for hard failure, GNUNET_OK for ok
1456 static int curl_schedule(void *cls)
1458 struct Plugin *plugin = cls;
1463 struct GNUNET_NETWORK_FDSet *grs;
1464 struct GNUNET_NETWORK_FDSet *gws;
1468 GNUNET_assert(cls !=NULL);
1470 /* Cancel previous scheduled task */
1471 if (plugin->http_curl_task != GNUNET_SCHEDULER_NO_TASK)
1473 GNUNET_SCHEDULER_cancel(plugin->env->sched, plugin->http_curl_task);
1474 plugin->http_curl_task = GNUNET_SCHEDULER_NO_TASK;
1480 mret = curl_multi_fdset (plugin->multi_handle, &rs, &ws, &es, &max);
1481 if (mret != CURLM_OK)
1483 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1484 _("%s failed at %s:%d: `%s'\n"),
1485 "curl_multi_fdset", __FILE__, __LINE__,
1486 curl_multi_strerror (mret));
1487 return GNUNET_SYSERR;
1489 mret = curl_multi_timeout (plugin->multi_handle, &to);
1490 if (mret != CURLM_OK)
1492 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1493 _("%s failed at %s:%d: `%s'\n"),
1494 "curl_multi_timeout", __FILE__, __LINE__,
1495 curl_multi_strerror (mret));
1496 return GNUNET_SYSERR;
1499 grs = GNUNET_NETWORK_fdset_create ();
1500 gws = GNUNET_NETWORK_fdset_create ();
1501 GNUNET_NETWORK_fdset_copy_native (grs, &rs, max + 1);
1502 GNUNET_NETWORK_fdset_copy_native (gws, &ws, max + 1);
1503 plugin->http_curl_task = GNUNET_SCHEDULER_add_select (plugin->env->sched,
1504 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1505 GNUNET_SCHEDULER_NO_TASK,
1506 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 0),
1511 GNUNET_NETWORK_fdset_destroy (gws);
1512 GNUNET_NETWORK_fdset_destroy (grs);
1517 * Function setting up curl handle and selecting message to send
1519 * @param ses session to send data to
1520 * @param con connection
1521 * @return GNUNET_SYSERR on failure, GNUNET_NO if connecting, GNUNET_YES if ok
1523 static ssize_t send_check_connections (void *cls, struct Session *ps)
1525 struct Plugin *plugin = cls;
1527 struct HTTP_Message * msg;
1529 struct GNUNET_TIME_Relative timeout = GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT;
1531 GNUNET_assert(cls !=NULL);
1533 if (ps->direction == OUTBOUND)
1535 /* RECV DIRECTION */
1536 /* Check if session is connected to receive data, otherwise connect to peer */
1537 if (ps->recv_connected == GNUNET_NO)
1539 if (ps->recv_endpoint == NULL)
1541 ps->recv_endpoint = curl_easy_init();
1543 curl_easy_setopt(ps->recv_endpoint, CURLOPT_VERBOSE, 1L);
1545 curl_easy_setopt(ps->recv_endpoint, CURLOPT_URL, ps->url);
1546 curl_easy_setopt(ps->recv_endpoint, CURLOPT_HEADERFUNCTION, &curl_get_header_cb);
1547 curl_easy_setopt(ps->recv_endpoint, CURLOPT_WRITEHEADER, ps);
1548 curl_easy_setopt(ps->recv_endpoint, CURLOPT_READFUNCTION, curl_send_cb);
1549 curl_easy_setopt(ps->recv_endpoint, CURLOPT_READDATA, ps);
1550 curl_easy_setopt(ps->recv_endpoint, CURLOPT_WRITEFUNCTION, curl_receive_cb);
1551 curl_easy_setopt(ps->recv_endpoint, CURLOPT_WRITEDATA, ps);
1552 curl_easy_setopt(ps->recv_endpoint, CURLOPT_TIMEOUT, (long) timeout.value);
1553 curl_easy_setopt(ps->recv_endpoint, CURLOPT_PRIVATE, ps);
1554 curl_easy_setopt(ps->recv_endpoint, CURLOPT_CONNECTTIMEOUT, HTTP_CONNECT_TIMEOUT);
1555 curl_easy_setopt(ps->recv_endpoint, CURLOPT_BUFFERSIZE, GNUNET_SERVER_MAX_MESSAGE_SIZE);
1557 mret = curl_multi_add_handle(plugin->multi_handle, ps->recv_endpoint);
1558 if (mret != CURLM_OK)
1560 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1561 _("Connection: %X: %s failed at %s:%d: `%s'\n"),
1563 "curl_multi_add_handle", __FILE__, __LINE__,
1564 curl_multi_strerror (mret));
1565 return GNUNET_SYSERR;
1567 if (curl_schedule (plugin) == GNUNET_SYSERR)
1569 #if DEBUG_CONNECTIONS
1570 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: could not schedule curl task\n",ps);
1572 return GNUNET_SYSERR;
1574 #if DEBUG_CONNECTIONS
1575 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: inbound not connected, initiating connection\n",ps);
1580 /* waiting for receive direction */
1581 if (ps->recv_connected==GNUNET_NO)
1584 /* SEND DIRECTION */
1585 /* Check if session is connected to send data, otherwise connect to peer */
1586 if ((ps->send_connected == GNUNET_YES) && (ps->send_endpoint!= NULL))
1588 if (ps->send_active == GNUNET_YES)
1590 #if DEBUG_CONNECTIONS
1591 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: outbound active, enqueueing message\n",ps);
1595 if (ps->send_active == GNUNET_NO)
1597 #if DEBUG_CONNECTIONS
1598 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: outbound paused, unpausing existing connection and enqueueing message\n",ps);
1600 if (CURLE_OK == curl_easy_pause(ps->send_endpoint,CURLPAUSE_CONT))
1602 ps->send_active=GNUNET_YES;
1606 return GNUNET_SYSERR;
1609 /* not connected, initiate connection */
1610 if ((ps->send_connected==GNUNET_NO) && (NULL == ps->send_endpoint))
1612 ps->send_endpoint = curl_easy_init();
1613 GNUNET_assert (ps->send_endpoint != NULL);
1614 GNUNET_assert (NULL != ps->pending_msgs_tail);
1615 #if DEBUG_CONNECTIONS
1616 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: outbound not connected, initiating connection\n",ps);
1618 ps->send_active = GNUNET_NO;
1619 msg = ps->pending_msgs_tail;
1622 curl_easy_setopt(ps->send_endpoint, CURLOPT_VERBOSE, 1L);
1624 curl_easy_setopt(ps->send_endpoint, CURLOPT_URL, ps->url);
1625 curl_easy_setopt(ps->send_endpoint, CURLOPT_PUT, 1L);
1626 curl_easy_setopt(ps->send_endpoint, CURLOPT_HEADERFUNCTION, &curl_put_header_cb);
1627 curl_easy_setopt(ps->send_endpoint, CURLOPT_WRITEHEADER, ps);
1628 curl_easy_setopt(ps->send_endpoint, CURLOPT_READFUNCTION, curl_send_cb);
1629 curl_easy_setopt(ps->send_endpoint, CURLOPT_READDATA, ps);
1630 curl_easy_setopt(ps->send_endpoint, CURLOPT_WRITEFUNCTION, curl_receive_cb);
1631 curl_easy_setopt(ps->send_endpoint, CURLOPT_READDATA, ps);
1632 curl_easy_setopt(ps->send_endpoint, CURLOPT_TIMEOUT, (long) timeout.value);
1633 curl_easy_setopt(ps->send_endpoint, CURLOPT_PRIVATE, ps);
1634 curl_easy_setopt(ps->send_endpoint, CURLOPT_CONNECTTIMEOUT, HTTP_CONNECT_TIMEOUT);
1635 curl_easy_setopt(ps->send_endpoint, CURLOPT_BUFFERSIZE, GNUNET_SERVER_MAX_MESSAGE_SIZE);
1637 mret = curl_multi_add_handle(plugin->multi_handle, ps->send_endpoint);
1638 if (mret != CURLM_OK)
1640 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1641 _("Connection: %X: %s failed at %s:%d: `%s'\n"),
1643 "curl_multi_add_handle", __FILE__, __LINE__,
1644 curl_multi_strerror (mret));
1645 return GNUNET_SYSERR;
1648 if (curl_schedule (plugin) == GNUNET_SYSERR)
1649 return GNUNET_SYSERR;
1652 if (ps->direction == INBOUND)
1654 GNUNET_assert (NULL != ps->pending_msgs_tail);
1655 if ((ps->recv_connected==GNUNET_YES) && (ps->send_connected==GNUNET_YES) &&
1656 (ps->recv_force_disconnect==GNUNET_NO) && (ps->recv_force_disconnect==GNUNET_NO))
1659 return GNUNET_SYSERR;
1662 static struct Session * send_select_session (void * cls, struct HTTP_PeerContext *pc, const void * addr, size_t addrlen, int force_address, struct Session * session)
1664 struct Session * tmp = NULL;
1665 int addr_given = GNUNET_NO;
1667 if ((addr!=NULL) && (addrlen>0))
1668 addr_given = GNUNET_YES;
1670 if (force_address == GNUNET_YES)
1672 /* check session given as argument */
1673 if ((session != NULL) && (addr_given == GNUNET_YES))
1675 if (0 == memcmp(session->addr, addr, addrlen))
1677 /* connection can not be used, since it is disconnected */
1678 if ((session->recv_force_disconnect==GNUNET_NO) && (session->send_force_disconnect==GNUNET_NO))
1680 #if DEBUG_SESSION_SELECTION
1681 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Session %X selected: Using session passed by transport to send to forced address \n", session);
1687 /* check last session used */
1688 if ((pc->last_session != NULL)&& (addr_given == GNUNET_YES))
1690 if (0 == memcmp(pc->last_session->addr, addr, addrlen))
1692 /* connection can not be used, since it is disconnected */
1693 if ((pc->last_session->recv_force_disconnect==GNUNET_NO) && (pc->last_session->send_force_disconnect==GNUNET_NO))
1695 #if DEBUG_SESSION_SELECTION
1696 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Session %X selected: Using last session used to send to forced address \n", pc->last_session);
1698 return pc->last_session;
1702 /* find session in existing sessions */
1704 while ((tmp!=NULL) && (addr_given == GNUNET_YES))
1707 if (0 == memcmp(tmp->addr, addr, addrlen))
1709 /* connection can not be used, since it is disconnected */
1710 if ((tmp->recv_force_disconnect==GNUNET_NO) && (tmp->send_force_disconnect==GNUNET_NO))
1712 #if DEBUG_SESSION_SELECTION
1713 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Session %X selected: Using existing session to send to forced address \n", session);
1721 /* no session to use */
1724 if ((force_address == GNUNET_NO) || (force_address == GNUNET_SYSERR))
1726 /* check session given as argument */
1727 if (session != NULL)
1729 /* connection can not be used, since it is disconnected */
1730 if ((session->recv_force_disconnect==GNUNET_NO) && (session->send_force_disconnect==GNUNET_NO))
1732 #if DEBUG_SESSION_SELECTION
1733 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Session %X selected: Using session passed by transport to send not-forced address \n", session);
1739 /* check last session used */
1740 if (pc->last_session != NULL)
1742 /* connection can not be used, since it is disconnected */
1743 if ((pc->last_session->recv_force_disconnect==GNUNET_NO) && (pc->last_session->send_force_disconnect==GNUNET_NO))
1745 #if DEBUG_SESSION_SELECTION
1746 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Session %X selected: Using last session to send to not-forced address \n", pc->last_session);
1748 return pc->last_session;
1751 /* find session in existing sessions */
1755 /* connection can not be used, since it is disconnected */
1756 if ((tmp->recv_force_disconnect==GNUNET_NO) && (tmp->send_force_disconnect==GNUNET_NO))
1758 #if DEBUG_SESSION_SELECTION
1759 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Session %X selected: Using existing session to send to not-forced address \n", tmp);
1771 * Function that can be used by the transport service to transmit
1772 * a message using the plugin. Note that in the case of a
1773 * peer disconnecting, the continuation MUST be called
1774 * prior to the disconnect notification itself. This function
1775 * will be called with this peer's HELLO message to initiate
1776 * a fresh connection to another peer.
1778 * @param cls closure
1779 * @param target who should receive this message
1780 * @param msgbuf the message to transmit
1781 * @param msgbuf_size number of bytes in 'msgbuf'
1782 * @param priority how important is the message (most plugins will
1783 * ignore message priority and just FIFO)
1784 * @param timeout how long to wait at most for the transmission (does not
1785 * require plugins to discard the message after the timeout,
1786 * just advisory for the desired delay; most plugins will ignore
1788 * @param session which session must be used (or NULL for "any")
1789 * @param addr the address to use (can be NULL if the plugin
1790 * is "on its own" (i.e. re-use existing TCP connection))
1791 * @param addrlen length of the address in bytes
1792 * @param force_address GNUNET_YES if the plugin MUST use the given address,
1793 * GNUNET_NO means the plugin may use any other address and
1794 * GNUNET_SYSERR means that only reliable existing
1795 * bi-directional connections should be used (regardless
1797 * @param cont continuation to call once the message has
1798 * been transmitted (or if the transport is ready
1799 * for the next transmission call; or if the
1800 * peer disconnected...); can be NULL
1801 * @param cont_cls closure for cont
1802 * @return number of bytes used (on the physical network, with overheads);
1803 * -1 on hard errors (i.e. address invalid); 0 is a legal value
1804 * and does NOT mean that the message was not transmitted (DV)
1807 http_plugin_send (void *cls,
1808 const struct GNUNET_PeerIdentity *target,
1811 unsigned int priority,
1812 struct GNUNET_TIME_Relative to,
1813 struct Session *session,
1817 GNUNET_TRANSPORT_TransmitContinuation cont,
1820 struct Plugin *plugin = cls;
1821 struct HTTP_Message *msg;
1822 struct HTTP_PeerContext * pc;
1823 struct Session * ps = NULL;
1825 GNUNET_assert(cls !=NULL);
1828 char * force = GNUNET_malloc(40);
1829 if (force_address == GNUNET_YES)
1830 strcpy(force,"forced addr.");
1831 if (force_address == GNUNET_NO)
1832 strcpy(force,"any addr.");
1833 if (force_address == GNUNET_SYSERR)
1834 strcpy(force,"reliable bi-direc. address addr.");
1836 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Transport tells me to send %u bytes to `%s' using %s (%s) and session: %X\n",
1840 http_plugin_address_to_string(NULL, addr, addrlen),
1846 pc = GNUNET_CONTAINER_multihashmap_get (plugin->peers, &target->hashPubKey);
1850 pc = GNUNET_malloc(sizeof (struct HTTP_PeerContext));
1851 pc->plugin = plugin;
1852 pc->session_id_counter=1;
1853 pc->last_session = NULL;
1854 memcpy(&pc->identity, target, sizeof(struct GNUNET_PeerIdentity));
1855 GNUNET_CONTAINER_multihashmap_put(plugin->peers, &pc->identity.hashPubKey, pc, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
1858 ps = send_select_session (plugin, pc, addr, addrlen, force_address, session);
1860 /* session not existing, but address forced -> creating new session */
1863 if ((addr!=NULL) && (addrlen!=0))
1865 ps = GNUNET_malloc(sizeof (struct Session));
1866 #if DEBUG_SESSION_SELECTION
1867 if (force_address == GNUNET_YES)
1868 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"No existing connection & forced address: creating new session %X to peer %s\n", ps, GNUNET_i2s(target));
1869 if (force_address != GNUNET_YES)
1870 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"No existing connection: creating new session %X to peer %s\n", ps, GNUNET_i2s(target));
1872 if ((addrlen!=0) && (addr!=NULL))
1874 ps->addr = GNUNET_malloc(addrlen);
1875 memcpy(ps->addr,addr,addrlen);
1876 ps->addrlen = addrlen;
1883 ps->direction=OUTBOUND;
1884 ps->recv_connected = GNUNET_NO;
1885 ps->recv_force_disconnect = GNUNET_NO;
1886 ps->send_connected = GNUNET_NO;
1887 ps->send_force_disconnect = GNUNET_NO;
1888 ps->pending_msgs_head = NULL;
1889 ps->pending_msgs_tail = NULL;
1891 ps->session_id = pc->session_id_counter;
1892 pc->session_id_counter++;
1893 ps->url = create_url (plugin, ps->addr, ps->addrlen, ps->session_id);
1894 if (ps->msgtok == NULL)
1895 ps->msgtok = GNUNET_SERVER_mst_create (&curl_receive_mst_cb, ps);
1896 GNUNET_CONTAINER_DLL_insert(pc->head,pc->tail,ps);
1901 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));
1903 return GNUNET_SYSERR;
1908 msg = GNUNET_malloc (sizeof (struct HTTP_Message) + msgbuf_size);
1910 msg->size = msgbuf_size;
1912 msg->buf = (char *) &msg[1];
1913 msg->transmit_cont = cont;
1914 msg->transmit_cont_cls = cont_cls;
1915 memcpy (msg->buf,msgbuf, msgbuf_size);
1916 GNUNET_CONTAINER_DLL_insert(ps->pending_msgs_head,ps->pending_msgs_tail,msg);
1918 if (send_check_connections (plugin, ps) != GNUNET_SYSERR)
1920 if (force_address != GNUNET_YES)
1921 pc->last_session = ps;
1923 if (pc->last_session==NULL)
1924 pc->last_session = ps;
1928 return GNUNET_SYSERR;
1934 * Function that can be used to force the plugin to disconnect
1935 * from the given peer and cancel all previous transmissions
1936 * (and their continuationc).
1938 * @param cls closure
1939 * @param target peer from which to disconnect
1942 http_plugin_disconnect (void *cls,
1943 const struct GNUNET_PeerIdentity *target)
1947 struct Plugin *plugin = cls;
1948 struct HTTP_PeerContext *pc = NULL;
1949 struct Session *ps = NULL;
1950 //struct Session *tmp = NULL;
1952 pc = GNUNET_CONTAINER_multihashmap_get (plugin->peers, &target->hashPubKey);
1959 /* Telling transport that session is getting disconnected */
1960 plugin->env->session_end(plugin, target, ps);
1961 if (ps->direction==OUTBOUND)
1963 if (ps->send_endpoint!=NULL)
1965 //GNUNET_assert(CURLM_OK == curl_multi_remove_handle(plugin->multi_handle,ps->send_endpoint));
1966 //curl_easy_cleanup(ps->send_endpoint);
1967 //ps->send_endpoint=NULL;
1968 ps->send_force_disconnect = GNUNET_YES;
1970 if (ps->recv_endpoint!=NULL)
1972 //GNUNET_assert(CURLM_OK == curl_multi_remove_handle(plugin->multi_handle,ps->recv_endpoint));
1973 //curl_easy_cleanup(ps->recv_endpoint);
1974 //ps->recv_endpoint=NULL;
1975 ps->recv_force_disconnect = GNUNET_YES;
1979 if (ps->direction==INBOUND)
1981 ps->recv_force_disconnect = GNUNET_YES;
1982 ps->send_force_disconnect = GNUNET_YES;
1985 while (ps->pending_msgs_head!=NULL)
1987 remove_http_message(ps, ps->pending_msgs_head);
1989 ps->recv_active = GNUNET_NO;
1990 ps->send_active = GNUNET_NO;
1997 * Convert the transports address to a nice, human-readable
2000 * @param cls closure
2001 * @param type name of the transport that generated the address
2002 * @param addr one of the addresses of the host, NULL for the last address
2003 * the specific address format depends on the transport
2004 * @param addrlen length of the address
2005 * @param numeric should (IP) addresses be displayed in numeric form?
2006 * @param timeout after how long should we give up?
2007 * @param asc function to call on each string
2008 * @param asc_cls closure for asc
2011 http_plugin_address_pretty_printer (void *cls,
2016 struct GNUNET_TIME_Relative timeout,
2017 GNUNET_TRANSPORT_AddressStringCallback
2020 const struct IPv4HttpAddress *t4;
2021 const struct IPv6HttpAddress *t6;
2022 struct sockaddr_in a4;
2023 struct sockaddr_in6 a6;
2029 GNUNET_assert(cls !=NULL);
2030 if (addrlen == sizeof (struct IPv6HttpAddress))
2032 address = GNUNET_malloc (INET6_ADDRSTRLEN);
2034 a6.sin6_addr = t6->ipv6_addr;
2035 inet_ntop(AF_INET6, &(a6.sin6_addr),address,INET6_ADDRSTRLEN);
2036 port = ntohs(t6->u6_port);
2038 else if (addrlen == sizeof (struct IPv4HttpAddress))
2040 address = GNUNET_malloc (INET_ADDRSTRLEN);
2042 a4.sin_addr.s_addr = t4->ipv4_addr;
2043 inet_ntop(AF_INET, &(a4.sin_addr),address,INET_ADDRSTRLEN);
2044 port = ntohs(t4->u_port);
2048 /* invalid address */
2049 GNUNET_break_op (0);
2050 asc (asc_cls, NULL);
2053 res = GNUNET_asprintf(&ret,"http://%s:%u/",address,port);
2054 GNUNET_free (address);
2055 GNUNET_assert(res != 0);
2057 GNUNET_free_non_null (ret);
2063 * Another peer has suggested an address for this
2064 * peer and transport plugin. Check that this could be a valid
2065 * address. If so, consider adding it to the list
2068 * @param cls closure
2069 * @param addr pointer to the address
2070 * @param addrlen length of addr
2071 * @return GNUNET_OK if this is a plausible address for this peer
2075 http_plugin_address_suggested (void *cls,
2076 const void *addr, size_t addrlen)
2078 struct Plugin *plugin = cls;
2079 struct IPv4HttpAddress *v4;
2080 struct IPv6HttpAddress *v6;
2083 GNUNET_assert(cls !=NULL);
2084 if ((addrlen != sizeof (struct IPv4HttpAddress)) &&
2085 (addrlen != sizeof (struct IPv6HttpAddress)))
2087 return GNUNET_SYSERR;
2089 if (addrlen == sizeof (struct IPv4HttpAddress))
2091 v4 = (struct IPv4HttpAddress *) addr;
2092 /* Not skipping loopback
2093 if (INADDR_LOOPBACK == ntohl(v4->ipv4_addr))
2095 return GNUNET_SYSERR;
2097 port = ntohs (v4->u_port);
2098 if (port != plugin->port_inbound)
2100 return GNUNET_SYSERR;
2103 if (addrlen == sizeof (struct IPv6HttpAddress))
2105 v6 = (struct IPv6HttpAddress *) addr;
2106 if (IN6_IS_ADDR_LINKLOCAL (&v6->ipv6_addr))
2108 return GNUNET_SYSERR;
2110 port = ntohs (v6->u6_port);
2111 if (port != plugin->port_inbound)
2113 return GNUNET_SYSERR;
2122 * Function called for a quick conversion of the binary address to
2123 * a numeric address. Note that the caller must not free the
2124 * address and that the next call to this function is allowed
2125 * to override the address again.
2127 * @param cls closure
2128 * @param addr binary address
2129 * @param addrlen length of the address
2130 * @return string representing the same address
2133 http_plugin_address_to_string (void *cls,
2137 const struct IPv4HttpAddress *t4;
2138 const struct IPv6HttpAddress *t6;
2139 struct sockaddr_in a4;
2140 struct sockaddr_in6 a6;
2146 if (addrlen == sizeof (struct IPv6HttpAddress))
2148 address = GNUNET_malloc (INET6_ADDRSTRLEN);
2150 a6.sin6_addr = t6->ipv6_addr;
2151 inet_ntop(AF_INET6, &(a6.sin6_addr),address,INET6_ADDRSTRLEN);
2152 port = ntohs(t6->u6_port);
2154 else if (addrlen == sizeof (struct IPv4HttpAddress))
2156 address = GNUNET_malloc (INET_ADDRSTRLEN);
2158 a4.sin_addr.s_addr = t4->ipv4_addr;
2159 inet_ntop(AF_INET, &(a4.sin_addr),address,INET_ADDRSTRLEN);
2160 port = ntohs(t4->u_port);
2164 /* invalid address */
2167 res = GNUNET_asprintf(&ret,"%s:%u",address,port);
2168 GNUNET_free (address);
2169 GNUNET_assert(res != 0);
2175 * Exit point from the plugin.
2178 libgnunet_plugin_transport_http_done (void *cls)
2180 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
2181 struct Plugin *plugin = api->cls;
2183 GNUNET_assert(cls !=NULL);
2185 if (plugin->http_server_daemon_v4 != NULL)
2187 MHD_stop_daemon (plugin->http_server_daemon_v4);
2188 plugin->http_server_daemon_v4 = NULL;
2190 if (plugin->http_server_daemon_v6 != NULL)
2192 MHD_stop_daemon (plugin->http_server_daemon_v6);
2193 plugin->http_server_daemon_v6 = NULL;
2196 if ( plugin->http_server_task_v4 != GNUNET_SCHEDULER_NO_TASK)
2198 GNUNET_SCHEDULER_cancel(plugin->env->sched, plugin->http_server_task_v4);
2199 plugin->http_server_task_v4 = GNUNET_SCHEDULER_NO_TASK;
2202 if ( plugin->http_server_task_v6 != GNUNET_SCHEDULER_NO_TASK)
2204 GNUNET_SCHEDULER_cancel(plugin->env->sched, plugin->http_server_task_v6);
2205 plugin->http_server_task_v6 = GNUNET_SCHEDULER_NO_TASK;
2209 /* free all peer information */
2210 if (plugin->peers!=NULL)
2212 GNUNET_CONTAINER_multihashmap_iterate (plugin->peers,
2213 &remove_peer_context_Iterator,
2215 GNUNET_CONTAINER_multihashmap_destroy (plugin->peers);
2217 if (plugin->multi_handle!=NULL)
2219 mret = curl_multi_cleanup(plugin->multi_handle);
2221 if ( CURLM_OK != mret)
2222 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"curl multihandle clean up failed\n");
2224 plugin->multi_handle = NULL;
2226 curl_global_cleanup();
2228 if ( plugin->http_curl_task != GNUNET_SCHEDULER_NO_TASK)
2230 GNUNET_SCHEDULER_cancel(plugin->env->sched, plugin->http_curl_task);
2231 plugin->http_curl_task = GNUNET_SCHEDULER_NO_TASK;
2234 GNUNET_free_non_null (plugin->bind4_address);
2235 GNUNET_free_non_null (plugin->bind6_address);
2236 GNUNET_free_non_null(plugin->bind_hostname);
2237 GNUNET_free (plugin);
2240 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Unload http plugin complete...\n");
2247 * Entry point for the plugin.
2250 libgnunet_plugin_transport_http_init (void *cls)
2252 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
2253 struct Plugin *plugin;
2254 struct GNUNET_TRANSPORT_PluginFunctions *api;
2255 struct GNUNET_TIME_Relative gn_timeout;
2256 long long unsigned int port;
2258 GNUNET_assert(cls !=NULL);
2260 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Starting http plugin...\n");
2263 plugin = GNUNET_malloc (sizeof (struct Plugin));
2264 plugin->stats = env->stats;
2266 plugin->peers = NULL;
2267 plugin->bind4_address = NULL;
2268 plugin->use_ipv6 = GNUNET_YES;
2269 plugin->use_ipv4 = GNUNET_YES;
2271 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
2273 api->send = &http_plugin_send;
2274 api->disconnect = &http_plugin_disconnect;
2275 api->address_pretty_printer = &http_plugin_address_pretty_printer;
2276 api->check_address = &http_plugin_address_suggested;
2277 api->address_to_string = &http_plugin_address_to_string;
2279 /* Hashing our identity to use it in URLs */
2280 GNUNET_CRYPTO_hash_to_enc ( &(plugin->env->my_identity->hashPubKey), &plugin->my_ascii_hash_ident);
2282 /* Reading port number from config file */
2283 if (GNUNET_CONFIGURATION_have_value (env->cfg,
2284 "transport-http", "USE_IPv6"))
2286 plugin->use_ipv6 = GNUNET_CONFIGURATION_get_value_yesno (env->cfg,
2290 /* Reading port number from config file */
2291 if (GNUNET_CONFIGURATION_have_value (env->cfg,
2292 "transport-http", "USE_IPv4"))
2294 plugin->use_ipv4 = GNUNET_CONFIGURATION_get_value_yesno (env->cfg,
2298 /* Reading port number from config file */
2300 GNUNET_CONFIGURATION_get_value_number (env->cfg,
2306 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
2308 _("Require valid port number for transport plugin `%s' in configuration!\n"),
2310 libgnunet_plugin_transport_http_done (api);
2314 /* Reading ipv4 addresse to bind to from config file */
2315 if ((plugin->use_ipv4==GNUNET_YES) && (GNUNET_CONFIGURATION_have_value (env->cfg,
2316 "transport-http", "BINDTO4")))
2318 GNUNET_break (GNUNET_OK ==
2319 GNUNET_CONFIGURATION_get_value_string (env->cfg,
2322 &plugin->bind_hostname));
2323 plugin->bind4_address = GNUNET_malloc(sizeof(struct sockaddr_in));
2324 plugin->bind4_address->sin_family = AF_INET;
2325 plugin->bind4_address->sin_port = htons (port);
2327 if (inet_pton(AF_INET,plugin->bind_hostname, &plugin->bind4_address->sin_addr)<=0)
2329 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
2331 _("Misconfigured address to bind to in configuration!\n"),
2333 GNUNET_free(plugin->bind4_address);
2334 GNUNET_free(plugin->bind_hostname);
2335 plugin->bind_hostname = NULL;
2336 plugin->bind4_address = NULL;
2340 /* Reading ipv4 addresse to bind to from config file */
2341 if ((plugin->use_ipv6==GNUNET_YES) && (GNUNET_CONFIGURATION_have_value (env->cfg,
2342 "transport-http", "BINDTO6")))
2344 GNUNET_break (GNUNET_OK ==
2345 GNUNET_CONFIGURATION_get_value_string (env->cfg,
2348 &plugin->bind_hostname));
2350 plugin->bind6_address = GNUNET_malloc(sizeof(struct sockaddr_in6));
2351 plugin->bind6_address->sin6_family = AF_INET6;
2352 plugin->bind6_address->sin6_port = htons (port);
2354 if (inet_pton(AF_INET6,plugin->bind_hostname, &plugin->bind6_address->sin6_addr)<=0)
2356 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
2358 _("Misconfigured address to bind to in configuration!\n"),
2360 GNUNET_free(plugin->bind6_address);
2361 GNUNET_free(plugin->bind_hostname);
2362 plugin->bind_hostname = NULL;
2363 plugin->bind6_address = NULL;
2367 GNUNET_assert ((port > 0) && (port <= 65535));
2368 plugin->port_inbound = port;
2369 gn_timeout = GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT;
2370 unsigned int timeout = (gn_timeout.value) / 1000;
2371 if ((plugin->http_server_daemon_v6 == NULL) && (plugin->use_ipv6 == GNUNET_YES) && (port != 0))
2373 struct sockaddr * tmp = (struct sockaddr *) plugin->bind6_address;
2374 plugin->http_server_daemon_v6 = MHD_start_daemon (
2375 #if DEBUG_CONNECTIONS
2381 plugin , &mdh_access_cb, plugin,
2382 MHD_OPTION_SOCK_ADDR, tmp,
2383 MHD_OPTION_CONNECTION_LIMIT, (unsigned int) 32,
2384 //MHD_OPTION_PER_IP_CONNECTION_LIMIT, (unsigned int) 6,
2385 MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) timeout,
2386 MHD_OPTION_CONNECTION_MEMORY_LIMIT, (size_t) (16 * 1024),
2387 MHD_OPTION_NOTIFY_COMPLETED, &mhd_termination_cb, NULL,
2390 if ((plugin->http_server_daemon_v4 == NULL) && (plugin->use_ipv4 == GNUNET_YES) && (port != 0))
2392 plugin->http_server_daemon_v4 = MHD_start_daemon (
2393 #if DEBUG_CONNECTIONS
2399 plugin , &mdh_access_cb, plugin,
2400 MHD_OPTION_SOCK_ADDR, (struct sockaddr_in *)plugin->bind4_address,
2401 MHD_OPTION_CONNECTION_LIMIT, (unsigned int) 32,
2402 //MHD_OPTION_PER_IP_CONNECTION_LIMIT, (unsigned int) 6,
2403 MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) timeout,
2404 MHD_OPTION_CONNECTION_MEMORY_LIMIT, (size_t) (16 * 1024),
2405 MHD_OPTION_NOTIFY_COMPLETED, &mhd_termination_cb, NULL,
2408 if (plugin->http_server_daemon_v4 != NULL)
2409 plugin->http_server_task_v4 = http_server_daemon_prepare (plugin, plugin->http_server_daemon_v4);
2410 if (plugin->http_server_daemon_v6 != NULL)
2411 plugin->http_server_task_v6 = http_server_daemon_prepare (plugin, plugin->http_server_daemon_v6);
2414 if (plugin->http_server_task_v4 != GNUNET_SCHEDULER_NO_TASK)
2417 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);
2420 else if ((plugin->http_server_task_v6 != GNUNET_SCHEDULER_NO_TASK) && (plugin->http_server_task_v4 != GNUNET_SCHEDULER_NO_TASK))
2423 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);
2426 else if ((plugin->http_server_task_v6 != GNUNET_SCHEDULER_NO_TASK) && (plugin->http_server_task_v4 == GNUNET_SCHEDULER_NO_TASK))
2429 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);
2435 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"No MHD was started, transport plugin not functional!\n");
2437 libgnunet_plugin_transport_http_done (api);
2441 /* Initializing cURL */
2442 curl_global_init(CURL_GLOBAL_ALL);
2443 plugin->multi_handle = curl_multi_init();
2445 if ( NULL == plugin->multi_handle )
2447 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
2449 _("Could not initialize curl multi handle, failed to start http plugin!\n"),
2451 libgnunet_plugin_transport_http_done (api);
2455 plugin->peers = GNUNET_CONTAINER_multihashmap_create (10);
2456 GNUNET_OS_network_interfaces_list (&process_interfaces, plugin);
2461 /* end of plugin_transport_http.c */