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 2, 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_constants.h"
29 #include "gnunet_protocols.h"
30 #include "gnunet_connection_lib.h"
31 #include "gnunet_server_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>
44 #define DEBUG_CURL GNUNET_NO
45 #define DEBUG_HTTP GNUNET_NO
46 #define HTTP_CONNECT_TIMEOUT_DBG 10
49 * Text of the response sent back after the last bytes of a PUT
50 * request have been received (just to formally obey the HTTP
53 #define HTTP_PUT_RESPONSE "Thank you!"
56 * After how long do we expire an address that we
57 * learned from another peer if it is not reconfirmed
60 #define LEARNED_ADDRESS_EXPIRATION GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_HOURS, 6)
63 * Page returned if request invalid
65 #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>"
68 * Timeout for a http connect
70 #define HTTP_CONNECT_TIMEOUT 30
73 * Network format for IPv4 addresses.
75 struct IPv4HttpAddress
78 * IPv4 address, in network byte order.
80 uint32_t ipv4_addr GNUNET_PACKED;
83 * Port number, in network byte order.
85 uint16_t u_port GNUNET_PACKED;
91 * Network format for IPv6 addresses.
93 struct IPv6HttpAddress
98 struct in6_addr ipv6_addr GNUNET_PACKED;
101 * Port number, in network byte order.
103 uint16_t u6_port GNUNET_PACKED;
109 * Message to send using http
114 * next pointer for double linked list
116 struct HTTP_Message * next;
119 * previous pointer for double linked list
121 struct HTTP_Message * prev;
124 * buffer containing data to send
129 * amount of data already sent
139 * Continuation function to call once the transmission buffer
140 * has again space available. NULL if there is no
141 * continuation to call.
143 GNUNET_TRANSPORT_TransmitContinuation transmit_cont;
146 * Closure for transmit_cont.
148 void *transmit_cont_cls;
152 struct HTTP_Connection_out
154 struct HTTP_Connection_out * next;
156 struct HTTP_Connection_out * prev;
161 struct HTTP_Message * pending_msgs_head;
162 struct HTTP_Message * pending_msgs_tail;
165 unsigned int connected;
166 unsigned int send_paused;
169 * curl handle for this ransmission
172 struct Session * session;
175 struct HTTP_Connection_in
177 struct HTTP_Connection_in * next;
179 struct HTTP_Connection_in * prev;
184 unsigned int connected;
185 unsigned int send_paused;
187 struct GNUNET_SERVER_MessageStreamTokenizer * msgtok;
189 struct Session * session;
192 * Is there a HTTP/PUT in progress?
194 int is_put_in_progress;
197 * Is the http request invalid?
204 * Session handle for connections.
212 struct SessionHeader header;
215 * Stored in a linked list.
217 struct Session *next;
220 * Pointer to the global plugin struct.
222 struct Plugin *plugin;
225 * To whom are we talking to (set to our identity
226 * if we are still waiting for the welcome message)
228 struct GNUNET_PeerIdentity identity;
231 * Sender's ip address to distinguish between incoming connections
242 * Did we initiate the connection (GNUNET_YES) or the other peer (GNUNET_NO)?
247 * At what time did we reset last_received last?
249 struct GNUNET_TIME_Absolute last_quota_update;
252 * How many bytes have we received since the "last_quota_update"
255 uint64_t last_received;
258 * Number of bytes per ms that this peer is allowed
266 struct GNUNET_CRYPTO_HashAsciiEncoded hash;
269 * curl handle for outbound transmissions
274 * Message tokenizer for incoming data
276 //struct GNUNET_SERVER_MessageStreamTokenizer * msgtok;
278 struct HTTP_Connection_out *outbound_connections_head;
279 struct HTTP_Connection_out *outbound_connections_tail;
281 struct HTTP_Connection_in *inbound_connections_head;
282 struct HTTP_Connection_in *inbound_connections_tail;
286 * Encapsulation of all of the state of the plugin.
293 struct GNUNET_TRANSPORT_PluginEnvironment *env;
295 unsigned int port_inbound;
298 * Hashmap for all existing sessions.
300 struct GNUNET_CONTAINER_MultiHashMap *sessions;
303 * Daemon for listening for new IPv4 connections.
305 struct MHD_Daemon *http_server_daemon_v4;
308 * Daemon for listening for new IPv6connections.
310 struct MHD_Daemon *http_server_daemon_v6;
313 * Our primary task for http daemon handling IPv4 connections
315 GNUNET_SCHEDULER_TaskIdentifier http_server_task_v4;
318 * Our primary task for http daemon handling IPv6 connections
320 GNUNET_SCHEDULER_TaskIdentifier http_server_task_v6;
323 * The task sending data
325 GNUNET_SCHEDULER_TaskIdentifier http_server_task_send;
330 CURLM * multi_handle;
333 * Our ASCII encoded, hashed peer identity
334 * This string is used to distinguish between connections and is added to the urls
336 struct GNUNET_CRYPTO_HashAsciiEncoded my_ascii_hash_ident;
341 * Function called for a quick conversion of the binary address to
342 * a numeric address. Note that the caller must not free the
343 * address and that the next call to this function is allowed
344 * to override the address again.
347 * @param addr binary address
348 * @param addrlen length of the address
349 * @return string representing the same address
352 http_plugin_address_to_string (void *cls,
357 * Create a new session
358 * @param cls plugin as closure
359 * @param addr_in address the peer is using inbound
360 * @param addr_len_in address length
361 * @param addr_out address the peer is using outbound
362 * @param addr_len_out address length
363 * @param peer identity
364 * @return created session object
366 static struct Session *
367 create_session (void * cls,
372 const struct GNUNET_PeerIdentity *peer)
374 struct Plugin *plugin = cls;
375 struct Session * cs = GNUNET_malloc ( sizeof( struct Session) );
377 GNUNET_assert(cls !=NULL);
380 cs->addr_in = GNUNET_malloc (addrlen_in);
381 cs->addr_in_len = addrlen_in;
382 memcpy(cs->addr_in,addr_in,addrlen_in);
385 if (addrlen_out != 0)
387 cs->addr_out = GNUNET_malloc (addrlen_out);
388 cs->addr_out_len = addrlen_out;
389 memcpy(cs->addr_out,addr_out,addrlen_out);
392 memcpy(&cs->identity, peer, sizeof (struct GNUNET_PeerIdentity));
393 GNUNET_CRYPTO_hash_to_enc(&cs->identity.hashPubKey,&(cs->hash));
394 cs->outbound_connections_head = NULL;
395 cs->outbound_connections_tail = NULL;
400 * Check if session for this peer is already existing, otherwise create it
401 * @param cls the plugin used
402 * @param p peer to get session for
403 * @return session found or created
405 static struct Session * session_get (void * cls, const struct GNUNET_PeerIdentity *p)
407 struct Plugin *plugin = cls;
411 cs = GNUNET_CONTAINER_multihashmap_get (plugin->sessions, &p->hashPubKey);
414 cs = create_session(plugin, NULL, 0, NULL, 0, p);
415 res = GNUNET_CONTAINER_multihashmap_put ( plugin->sessions,
416 &cs->identity.hashPubKey,
418 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
419 if (res == GNUNET_OK)
420 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
421 "New Session `%s' inserted\n", GNUNET_i2s(p));
426 static char * create_url(void * cls, const void * addr, size_t addrlen)
428 struct Plugin *plugin = cls;
431 GNUNET_assert ((addr!=NULL) && (addrlen != 0));
432 GNUNET_asprintf(&url,
434 http_plugin_address_to_string(NULL, addr, addrlen),
435 (char *) (&plugin->my_ascii_hash_ident));
441 * Check if session already knows this address for a outbound connection to this peer
442 * If address not in session, add it to the session
443 * @param cls the plugin used
444 * @param cs the session
445 * @param addr address
446 * @param addr_len address length
447 * @return the found or created address
449 static struct HTTP_Connection_out * session_check_outbound_address (void * cls, struct Session *cs, const void * addr, size_t addr_len)
451 struct Plugin *plugin = cls;
452 struct HTTP_Connection_out * cc = cs->outbound_connections_head;
453 struct HTTP_Connection_out * con = NULL;
455 GNUNET_assert((addr_len == sizeof (struct IPv4HttpAddress)) || (addr_len == sizeof (struct IPv6HttpAddress)));
459 if (addr_len == cc->addrlen)
461 if (0 == memcmp(cc->addr, addr, addr_len))
472 con = GNUNET_malloc(sizeof(struct HTTP_Connection_out) + addr_len);
473 con->addrlen = addr_len;
475 con->url=create_url(plugin, addr, addr_len);
476 con->connected = GNUNET_NO;
478 memcpy(con->addr, addr, addr_len);
479 GNUNET_CONTAINER_DLL_insert(cs->outbound_connections_head,cs->outbound_connections_tail,con);
480 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Created new connection %X to peer `%s'\n",con,GNUNET_i2s(&cs->identity));
487 * Check if session already knows this address for a inbound connection to this peer
488 * If address not in session, add it to the session
489 * @param cls not needed, can be NULL
490 * @param cs the session
491 * @param addr address
492 * @param addr_len address length
493 * @return the found or created address
495 static struct HTTP_Connection_in * session_check_inbound_address (void * cls, struct Session *cs, const void * addr, size_t addr_len)
497 struct HTTP_Connection_in * cc = cs->inbound_connections_head;
498 struct HTTP_Connection_in * con = NULL;
500 GNUNET_assert((addr_len == sizeof (struct IPv4HttpAddress)) || (addr_len == sizeof (struct IPv6HttpAddress)));
504 if (addr_len == cc->addrlen)
506 if (0 == memcmp(cc->addr, addr, addr_len))
518 con = GNUNET_malloc(sizeof(struct HTTP_Connection_in) + addr_len);
519 con->addrlen = addr_len;
521 con->connected = GNUNET_NO;
523 memcpy(con->addr, addr, addr_len);
524 GNUNET_CONTAINER_DLL_insert(cs->inbound_connections_head,cs->inbound_connections_tail,con);
527 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X for inbound address %s (%s) was found\n",
529 GNUNET_i2s(&cs->identity),
530 http_plugin_address_to_string(NULL,con->addr,con->addrlen));
536 * Callback called by MHD when a connection is terminated
538 static void requestCompletedCallback (void *cls, struct MHD_Connection * connection, void **httpSessionCache)
540 struct HTTP_Connection_in * con;
542 con = *httpSessionCache;
545 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection from peer `%s' was terminated\n",GNUNET_i2s(&con->session->identity));
546 /* session set to inactive */
547 con->is_put_in_progress = GNUNET_NO;
548 con->is_bad_request = GNUNET_NO;
551 static void messageTokenizerCallback (void *cls,
553 const struct GNUNET_MessageHeader *message)
555 struct HTTP_Connection_in * con = cls;
556 GNUNET_assert(con != NULL);
558 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
559 "Forwarding message to transport service, type %u and size %u from `%s' (`%s')\n",
560 ntohs(message->type),
561 ntohs(message->size),
562 GNUNET_i2s(&(con->session->identity)),http_plugin_address_to_string(NULL,con->addr,con->addrlen));
564 con->session->plugin->env->receive (con->session->plugin->env->cls,
565 &con->session->identity,
566 message, 1, con->session,
572 * Check if ip is allowed to connect.
575 acceptPolicyCallback (void *cls,
576 const struct sockaddr *addr, socklen_t addr_len)
579 struct Plugin *plugin = cls;
581 /* Every connection is accepted, nothing more to do here */
585 int server_read_callback (void *cls, uint64_t pos, char *buf, int max)
588 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "server_read_callback\n");
593 * Process GET or PUT request received via MHD. For
594 * GET, queue response that will send back our pending
595 * messages. For PUT, process incoming data and send
596 * to GNUnet core. In either case, check if a session
597 * already exists and create a new one if not.
600 accessHandlerCallback (void *cls,
601 struct MHD_Connection *mhd_connection,
605 const char *upload_data,
606 size_t * upload_data_size, void **httpSessionCache)
608 struct Plugin *plugin = cls;
609 struct MHD_Response *response;
611 struct HTTP_Connection_in * con;
612 const union MHD_ConnectionInfo * conn_info;
613 struct sockaddr_in *addrin;
614 struct sockaddr_in6 *addrin6;
615 char address[INET6_ADDRSTRLEN+14];
616 struct GNUNET_PeerIdentity pi_in;
618 int send_error_to_client;
619 struct IPv4HttpAddress ipv4addr;
620 struct IPv6HttpAddress ipv6addr;
622 GNUNET_assert(cls !=NULL);
623 send_error_to_client = GNUNET_NO;
624 if (NULL == *httpSessionCache)
626 /* check url for peer identity , if invalid send HTTP 404*/
627 res = GNUNET_CRYPTO_hash_from_string ( &url[1], &(pi_in.hashPubKey));
628 if ( GNUNET_SYSERR == res )
630 response = MHD_create_response_from_data (strlen (HTTP_ERROR_RESPONSE),HTTP_ERROR_RESPONSE, MHD_NO, MHD_NO);
631 res = MHD_queue_response (mhd_connection, MHD_HTTP_NOT_FOUND, response);
632 MHD_destroy_response (response);
634 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Peer has no valid ident, sent HTTP 1.1/404\n");
636 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Peer has no valid ident, could not send error\n");
642 con = *httpSessionCache;
646 /* Is it a PUT or a GET request */
647 if (0 == strcmp (MHD_HTTP_METHOD_PUT, method))
649 if (NULL == *httpSessionCache)
651 /* get session for peer identity */
652 cs = session_get (plugin ,&pi_in);
654 conn_info = MHD_get_connection_info(mhd_connection, MHD_CONNECTION_INFO_CLIENT_ADDRESS );
655 /* Incoming IPv4 connection */
656 if ( AF_INET == conn_info->client_addr->sin_family)
658 addrin = conn_info->client_addr;
659 inet_ntop(addrin->sin_family, &(addrin->sin_addr),address,INET_ADDRSTRLEN);
660 memcpy(&ipv4addr.ipv4_addr,&(addrin->sin_addr),sizeof(struct in_addr));
661 ipv4addr.u_port = addrin->sin_port;
662 con = session_check_inbound_address (plugin, cs, (const void *) &ipv4addr, sizeof (struct IPv4HttpAddress));
664 /* Incoming IPv6 connection */
665 if ( AF_INET6 == conn_info->client_addr->sin_family)
667 addrin6 = (struct sockaddr_in6 *) conn_info->client_addr;
668 inet_ntop(addrin6->sin6_family, &(addrin6->sin6_addr),address,INET6_ADDRSTRLEN);
669 memcpy(&ipv6addr.ipv6_addr,&(addrin6->sin6_addr),sizeof(struct in6_addr));
670 ipv6addr.u6_port = addrin6->sin6_port;
671 con = session_check_inbound_address (plugin, cs, &ipv6addr, sizeof (struct IPv6HttpAddress));
673 /* Set closure and update current session*/
675 *httpSessionCache = con;
676 if (con->msgtok==NULL)
677 con->msgtok = GNUNET_SERVER_mst_create (GNUNET_SERVER_MAX_MESSAGE_SIZE - 1, &messageTokenizerCallback, con);
679 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"HTTP Daemon has new an incoming `%s' request from peer `%s' (`%s')\n",
681 GNUNET_i2s(&cs->identity),
682 http_plugin_address_to_string(NULL, con->addr, con->addrlen));
685 if ((*upload_data_size == 0) && (con->is_put_in_progress==GNUNET_NO))
687 con->is_put_in_progress = GNUNET_YES;
691 /* Transmission of all data complete */
692 if ((*upload_data_size == 0) && (con->is_put_in_progress == GNUNET_YES))
694 response = MHD_create_response_from_data (strlen (HTTP_PUT_RESPONSE),HTTP_PUT_RESPONSE, MHD_NO, MHD_NO);
695 res = MHD_queue_response (mhd_connection, MHD_HTTP_OK, response);
696 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Sent HTTP/1.1: 200 OK as PUT Response\n",HTTP_PUT_RESPONSE, strlen (HTTP_PUT_RESPONSE), res );
697 MHD_destroy_response (response);
700 con->is_put_in_progress = GNUNET_NO;
701 con->is_bad_request = GNUNET_NO;
706 if ((*upload_data_size > 0) && (con->is_put_in_progress == GNUNET_YES))
708 res = GNUNET_SERVER_mst_receive(con->msgtok, con, upload_data,*upload_data_size, GNUNET_NO, GNUNET_NO);
709 (*upload_data_size) = 0;
715 if ( 0 == strcmp (MHD_HTTP_METHOD_GET, method) )
717 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Got GET Request\n");
718 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"URL: `%s'\n",url);
720 /* check url for peer identity , if invalid send HTTP 404*/
721 res = GNUNET_CRYPTO_hash_from_string ( &url[1], &(pi_in.hashPubKey));
723 if ( GNUNET_SYSERR == res )
725 response = MHD_create_response_from_data (strlen (HTTP_ERROR_RESPONSE),HTTP_ERROR_RESPONSE, MHD_NO, MHD_NO);
726 res = MHD_queue_response (mhd_connection, MHD_HTTP_NOT_FOUND, response);
727 MHD_destroy_response (response);
729 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Peer has no valid ident, sent HTTP 1.1/404\n");
731 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Peer has no valid ident, could not send error\n");
735 response = MHD_create_response_from_callback(-1,32 * 1024, &server_read_callback, cs, NULL);
736 res = MHD_queue_response (mhd_connection, MHD_HTTP_NOT_FOUND, response);
737 MHD_destroy_response (response);
747 * Call MHD to process pending ipv4 requests and then go back
748 * and schedule the next run.
750 static void http_server_daemon_v4_run (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
752 * Call MHD to process pending ipv6 requests and then go back
753 * and schedule the next run.
755 static void http_server_daemon_v6_run (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
758 * Function that queries MHD's select sets and
759 * starts the task waiting for them.
761 static GNUNET_SCHEDULER_TaskIdentifier
762 http_server_daemon_prepare (void * cls, struct MHD_Daemon *daemon_handle)
764 struct Plugin *plugin = cls;
765 GNUNET_SCHEDULER_TaskIdentifier ret;
769 struct GNUNET_NETWORK_FDSet *wrs;
770 struct GNUNET_NETWORK_FDSet *wws;
771 struct GNUNET_NETWORK_FDSet *wes;
773 unsigned long long timeout;
775 struct GNUNET_TIME_Relative tv;
777 GNUNET_assert(cls !=NULL);
778 ret = GNUNET_SCHEDULER_NO_TASK;
782 wrs = GNUNET_NETWORK_fdset_create ();
783 wes = GNUNET_NETWORK_fdset_create ();
784 wws = GNUNET_NETWORK_fdset_create ();
786 GNUNET_assert (MHD_YES ==
787 MHD_get_fdset (daemon_handle,
792 haveto = MHD_get_timeout (daemon_handle, &timeout);
793 if (haveto == MHD_YES)
794 tv.value = (uint64_t) timeout;
796 tv = GNUNET_TIME_UNIT_FOREVER_REL;
797 GNUNET_NETWORK_fdset_copy_native (wrs, &rs, max);
798 GNUNET_NETWORK_fdset_copy_native (wws, &ws, max);
799 GNUNET_NETWORK_fdset_copy_native (wes, &es, max);
800 if (daemon_handle == plugin->http_server_daemon_v4)
802 ret = GNUNET_SCHEDULER_add_select (plugin->env->sched,
803 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
804 GNUNET_SCHEDULER_NO_TASK,
808 &http_server_daemon_v4_run,
811 if (daemon_handle == plugin->http_server_daemon_v6)
813 ret = GNUNET_SCHEDULER_add_select (plugin->env->sched,
814 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
815 GNUNET_SCHEDULER_NO_TASK,
819 &http_server_daemon_v6_run,
822 GNUNET_NETWORK_fdset_destroy (wrs);
823 GNUNET_NETWORK_fdset_destroy (wws);
824 GNUNET_NETWORK_fdset_destroy (wes);
829 * Call MHD to process pending requests and then go back
830 * and schedule the next run.
832 static void http_server_daemon_v4_run (void *cls,
833 const struct GNUNET_SCHEDULER_TaskContext *tc)
835 struct Plugin *plugin = cls;
837 GNUNET_assert(cls !=NULL);
838 if (plugin->http_server_task_v4 != GNUNET_SCHEDULER_NO_TASK)
839 plugin->http_server_task_v4 = GNUNET_SCHEDULER_NO_TASK;
841 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
844 GNUNET_assert (MHD_YES == MHD_run (plugin->http_server_daemon_v4));
845 plugin->http_server_task_v4 = http_server_daemon_prepare (plugin, plugin->http_server_daemon_v4);
851 * Call MHD to process pending requests and then go back
852 * and schedule the next run.
854 static void http_server_daemon_v6_run (void *cls,
855 const struct GNUNET_SCHEDULER_TaskContext *tc)
857 struct Plugin *plugin = cls;
859 GNUNET_assert(cls !=NULL);
860 if (plugin->http_server_task_v6 != GNUNET_SCHEDULER_NO_TASK)
861 plugin->http_server_task_v6 = GNUNET_SCHEDULER_NO_TASK;
863 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
866 GNUNET_assert (MHD_YES == MHD_run (plugin->http_server_daemon_v6));
867 plugin->http_server_task_v6 = http_server_daemon_prepare (plugin, plugin->http_server_daemon_v6);
872 * Removes a message from the linked list of messages
873 * @param con connection to remove message from
874 * @param msg message to remove
875 * @return GNUNET_SYSERR if msg not found, GNUNET_OK on success
878 static int remove_http_message(struct HTTP_Connection_out * con, struct HTTP_Message * msg)
880 GNUNET_CONTAINER_DLL_remove(con->pending_msgs_head,con->pending_msgs_tail,msg);
886 static size_t header_function( void *ptr, size_t size, size_t nmemb, void *stream)
889 size_t len = size * nmemb;
892 if ((size * nmemb) < SIZE_MAX)
893 tmp = GNUNET_malloc (len+1);
895 if ((tmp != NULL) && (len > 0))
900 if (tmp[len-2] == 13)
904 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Header: `%s'\n",tmp);
914 * Callback method used with libcurl
915 * Method is called when libcurl needs to read data during sending
916 * @param stream pointer where to write data
917 * @param size size of an individual element
918 * @param nmemb count of elements that can be written to the buffer
919 * @param ptr source pointer, passed to the libcurl handle
920 * @return bytes written to stream
922 static size_t send_read_callback(void *stream, size_t size, size_t nmemb, void *ptr)
924 struct HTTP_Connection_out * con = ptr;
925 struct HTTP_Message * msg = con->pending_msgs_tail;
929 if (con->pending_msgs_tail == NULL)
931 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: No Message to send, pausing connection\n",con);
932 con->send_paused = GNUNET_YES;
933 return CURL_READFUNC_PAUSE;
936 msg = con->pending_msgs_tail;
938 if (msg->pos < msg->size)
940 /* data fit in buffer */
941 if ((msg->size - msg->pos) <= (size * nmemb))
943 len = (msg->size - msg->pos);
944 memcpy(stream, &msg->buf[msg->pos], len);
951 memcpy(stream, &msg->buf[msg->pos], len);
956 /* no data to send */
962 if ( msg->pos == msg->size)
964 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: Message with %u bytes sent, removing message from queue \n",con, msg->pos);
965 /* Calling transmit continuation */
966 if (( NULL != con->pending_msgs_tail) && (NULL != con->pending_msgs_tail->transmit_cont))
967 msg->transmit_cont (con->pending_msgs_tail->transmit_cont_cls,&(con->session)->identity,GNUNET_OK);
968 remove_http_message(con, msg);
974 * Callback method used with libcurl
975 * Method is called when libcurl needs to write data during sending
976 * @param stream pointer where to write data
977 * @param size size of an individual element
978 * @param nmemb count of elements that can be written to the buffer
979 * @param ptr destination pointer, passed to the libcurl handle
980 * @return bytes read from stream
982 static size_t send_write_callback( void *stream, size_t size, size_t nmemb, void *ptr)
986 if ((size * nmemb) < SIZE_MAX)
987 data = GNUNET_malloc(size*nmemb +1);
990 memcpy( data, stream, size*nmemb);
991 data[size*nmemb] = '\0';
994 return (size * nmemb);
999 * Function setting up file descriptors and scheduling task to run
1000 * @param cls closure
1001 * @param ses session to send data to
1002 * @return bytes sent to peer
1004 static size_t send_schedule(void *cls, struct Session* ses );
1009 * Function setting up curl handle and selecting message to send
1011 * @param ses session to send data to
1012 * @param con connection
1013 * @return bytes sent to peer
1015 static ssize_t send_initiate (void *cls, struct Session* ses , struct HTTP_Connection_out *con)
1017 struct Plugin *plugin = cls;
1020 struct HTTP_Message * msg;
1021 struct GNUNET_TIME_Relative timeout = GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT;
1023 /* already connected, no need to initiate connection */
1024 if ((con->connected == GNUNET_YES) && (con->curl_handle != NULL) && (con->send_paused == GNUNET_NO))
1026 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: active, enqueueing message\n",con);
1030 if ((con->connected == GNUNET_YES) && (con->curl_handle != NULL) && (con->send_paused == GNUNET_YES))
1032 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: paused, unpausing existing connection and enqueueing message\n",con);
1033 curl_easy_pause(con->curl_handle,CURLPAUSE_CONT);
1034 con->send_paused=GNUNET_NO;
1038 /* not connected, initiate connection */
1039 GNUNET_assert(cls !=NULL);
1041 if ( NULL == con->curl_handle)
1042 con->curl_handle = curl_easy_init();
1043 GNUNET_assert (con->curl_handle != NULL);
1044 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection %X: not connected, initiating connection\n",con);
1046 GNUNET_assert (NULL != con->pending_msgs_tail);
1047 msg = con->pending_msgs_tail;
1050 curl_easy_setopt(con->curl_handle, CURLOPT_VERBOSE, 1L);
1052 curl_easy_setopt(con->curl_handle, CURLOPT_URL, con->url);
1053 curl_easy_setopt(con->curl_handle, CURLOPT_PUT, 1L);
1054 curl_easy_setopt(con->curl_handle, CURLOPT_HEADERFUNCTION, &header_function);
1055 curl_easy_setopt(con->curl_handle, CURLOPT_WRITEHEADER, con);
1056 curl_easy_setopt(con->curl_handle, CURLOPT_READFUNCTION, send_read_callback);
1057 curl_easy_setopt(con->curl_handle, CURLOPT_READDATA, con);
1058 curl_easy_setopt(con->curl_handle, CURLOPT_WRITEFUNCTION, send_write_callback);
1059 curl_easy_setopt(con->curl_handle, CURLOPT_READDATA, con);
1060 curl_easy_setopt(con->curl_handle, CURLOPT_TIMEOUT, (long) timeout.value);
1061 curl_easy_setopt(con->curl_handle, CURLOPT_PRIVATE, con);
1062 curl_easy_setopt(con->curl_handle, CURLOPT_CONNECTTIMEOUT, HTTP_CONNECT_TIMEOUT_DBG);
1063 curl_easy_setopt(con->curl_handle, CURLOPT_BUFFERSIZE, GNUNET_SERVER_MAX_MESSAGE_SIZE);
1065 mret = curl_multi_add_handle(plugin->multi_handle, con->curl_handle);
1066 if (mret != CURLM_OK)
1068 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1069 _("%s failed at %s:%d: `%s'\n"),
1070 "curl_multi_add_handle", __FILE__, __LINE__,
1071 curl_multi_strerror (mret));
1075 con->connected = GNUNET_YES;
1077 bytes_sent = send_schedule (plugin, ses);
1081 static void send_execute (void *cls,
1082 const struct GNUNET_SCHEDULER_TaskContext *tc)
1084 struct Plugin *plugin = cls;
1085 static unsigned int handles_last_run;
1087 struct CURLMsg *msg;
1089 struct HTTP_Connection_out * con = NULL;
1090 struct Session * cs = NULL;
1093 GNUNET_assert(cls !=NULL);
1094 plugin->http_server_task_send = GNUNET_SCHEDULER_NO_TASK;
1095 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1101 mret = curl_multi_perform (plugin->multi_handle, &running);
1102 if (running < handles_last_run)
1107 msg = curl_multi_info_read (plugin->multi_handle, &running);
1110 /* get session for affected curl handle */
1111 GNUNET_assert ( msg->easy_handle != NULL );
1112 curl_easy_getinfo(msg->easy_handle, CURLINFO_PRIVATE, (char *) &con);
1113 GNUNET_assert ( con != NULL );
1115 GNUNET_assert ( cs != NULL );
1120 if ( (msg->data.result != CURLE_OK) &&
1121 (msg->data.result != CURLE_GOT_NOTHING) )
1125 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1126 _("Connection %X to peer `%s' (`%s') failed: `%s' `%s'\n"),
1128 GNUNET_i2s(&cs->identity),
1129 http_plugin_address_to_string(NULL, con->addr, con->addrlen),
1130 "curl_multi_perform",
1131 curl_easy_strerror (msg->data.result));
1132 /* sending msg failed*/
1133 con->connected = GNUNET_NO;
1134 curl_easy_cleanup(con->curl_handle);
1135 con->curl_handle=NULL;
1136 if (( NULL != con->pending_msgs_tail) && ( NULL != con->pending_msgs_tail->transmit_cont))
1137 con->pending_msgs_tail->transmit_cont (con->pending_msgs_tail->transmit_cont_cls,&con->session->identity,GNUNET_SYSERR);
1142 GNUNET_assert (CURLE_OK == curl_easy_getinfo(msg->easy_handle, CURLINFO_RESPONSE_CODE, &http_result));
1143 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1144 "Send to peer `%s' completed with code %u\n", GNUNET_i2s(&cs->identity), http_result );
1146 curl_easy_cleanup(con->curl_handle);
1147 con->connected = GNUNET_NO;
1148 con->curl_handle=NULL;
1150 /* Calling transmit continuation */
1151 if (( NULL != con->pending_msgs_tail) && (NULL != con->pending_msgs_tail->transmit_cont))
1153 /* HTTP 1xx : Last message before here was informational */
1154 if ((http_result >=100) && (http_result < 200))
1155 con->pending_msgs_tail->transmit_cont (con->pending_msgs_tail->transmit_cont_cls,&cs->identity,GNUNET_OK);
1156 /* HTTP 2xx: successful operations */
1157 if ((http_result >=200) && (http_result < 300))
1158 con->pending_msgs_tail->transmit_cont (con->pending_msgs_tail->transmit_cont_cls,&cs->identity,GNUNET_OK);
1159 /* HTTP 3xx..5xx: error */
1160 if ((http_result >=300) && (http_result < 600))
1161 con->pending_msgs_tail->transmit_cont (con->pending_msgs_tail->transmit_cont_cls,&cs->identity,GNUNET_SYSERR);
1164 if (con->pending_msgs_tail != NULL)
1166 if (con->pending_msgs_tail->pos>0)
1167 remove_http_message(con, con->pending_msgs_tail);
1168 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Message could not be removed from session `%s'\n", GNUNET_i2s(&cs->identity));
1176 while ( (running > 0) );
1178 handles_last_run = running;
1180 while (mret == CURLM_CALL_MULTI_PERFORM);
1181 send_schedule(plugin, cls);
1186 * Function setting up file descriptors and scheduling task to run
1187 * @param ses session to send data to
1188 * @return bytes sent to peer
1190 static size_t send_schedule(void *cls, struct Session* ses )
1192 struct Plugin *plugin = cls;
1197 struct GNUNET_NETWORK_FDSet *grs;
1198 struct GNUNET_NETWORK_FDSet *gws;
1202 GNUNET_assert(cls !=NULL);
1207 mret = curl_multi_fdset (plugin->multi_handle, &rs, &ws, &es, &max);
1208 if (mret != CURLM_OK)
1210 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1211 _("%s failed at %s:%d: `%s'\n"),
1212 "curl_multi_fdset", __FILE__, __LINE__,
1213 curl_multi_strerror (mret));
1216 mret = curl_multi_timeout (plugin->multi_handle, &to);
1217 if (mret != CURLM_OK)
1219 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1220 _("%s failed at %s:%d: `%s'\n"),
1221 "curl_multi_timeout", __FILE__, __LINE__,
1222 curl_multi_strerror (mret));
1226 grs = GNUNET_NETWORK_fdset_create ();
1227 gws = GNUNET_NETWORK_fdset_create ();
1228 GNUNET_NETWORK_fdset_copy_native (grs, &rs, max + 1);
1229 GNUNET_NETWORK_fdset_copy_native (gws, &ws, max + 1);
1230 plugin->http_server_task_send = GNUNET_SCHEDULER_add_select (plugin->env->sched,
1231 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1232 GNUNET_SCHEDULER_NO_TASK,
1233 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 0),
1238 GNUNET_NETWORK_fdset_destroy (gws);
1239 GNUNET_NETWORK_fdset_destroy (grs);
1241 /* FIXME: return bytes REALLY sent */
1247 * Function that can be used by the transport service to transmit
1248 * a message using the plugin.
1250 * @param cls closure
1251 * @param target who should receive this message
1252 * @param priority how important is the message
1253 * @param msgbuf the message to transmit
1254 * @param msgbuf_size number of bytes in 'msgbuf'
1255 * @param to when should we time out
1256 * @param session which session must be used (or NULL for "any")
1257 * @param addr the address to use (can be NULL if the plugin
1258 * is "on its own" (i.e. re-use existing TCP connection))
1259 * @param addrlen length of the address in bytes
1260 * @param force_address GNUNET_YES if the plugin MUST use the given address,
1261 * otherwise the plugin may use other addresses or
1262 * existing connections (if available)
1263 * @param cont continuation to call once the message has
1264 * been transmitted (or if the transport is ready
1265 * for the next transmission call; or if the
1266 * peer disconnected...)
1267 * @param cont_cls closure for cont
1268 * @return number of bytes used (on the physical network, with overheads);
1269 * -1 on hard errors (i.e. address invalid); 0 is a legal value
1270 * and does NOT mean that the message was not transmitted (DV)
1273 http_plugin_send (void *cls,
1274 const struct GNUNET_PeerIdentity *target,
1277 unsigned int priority,
1278 struct GNUNET_TIME_Relative to,
1279 struct Session *session,
1283 GNUNET_TRANSPORT_TransmitContinuation cont,
1286 struct Plugin *plugin = cls;
1288 struct HTTP_Message *msg;
1289 struct HTTP_Connection_out *con;
1292 GNUNET_assert(cls !=NULL);
1293 GNUNET_assert ((addr!=NULL) && (addrlen != 0));
1295 /* get session from hashmap */
1296 cs = session_get(plugin, target);
1297 con = session_check_outbound_address(plugin, cs, addr, addrlen);
1298 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Transport tells me to send %u bytes to `%s' (%s), session: %X\n",
1299 msgbuf_size, GNUNET_i2s(&cs->identity),
1300 http_plugin_address_to_string(NULL, addr, addrlen),
1304 msg = GNUNET_malloc (sizeof (struct HTTP_Message) + msgbuf_size);
1306 msg->size = msgbuf_size;
1308 msg->buf = (char *) &msg[1];
1309 msg->transmit_cont = cont;
1310 msg->transmit_cont_cls = cont_cls;
1311 memcpy (msg->buf,msgbuf, msgbuf_size);
1313 /* must use this address */
1314 if (force_address == GNUNET_YES)
1316 /* enqueue in connection message queue */
1317 GNUNET_CONTAINER_DLL_insert(con->pending_msgs_head,con->pending_msgs_tail,msg);
1319 /* can use existing connection to send */
1322 /* enqueue in connection message queue */
1323 GNUNET_CONTAINER_DLL_insert(con->pending_msgs_head,con->pending_msgs_tail,msg);
1325 return send_initiate (plugin, cs, con);
1331 * Function that can be used to force the plugin to disconnect
1332 * from the given peer and cancel all previous transmissions
1333 * (and their continuationc).
1335 * @param cls closure
1336 * @param target peer from which to disconnect
1339 http_plugin_disconnect (void *cls,
1340 const struct GNUNET_PeerIdentity *target)
1342 struct Plugin *plugin = cls;
1343 struct HTTP_Connection_out *con;
1346 /* get session from hashmap */
1347 cs = session_get(plugin, target);
1348 con = cs->outbound_connections_head;
1352 if (con->curl_handle!=NULL)
1353 curl_easy_cleanup(con->curl_handle);
1354 con->curl_handle=NULL;
1355 con->connected = GNUNET_NO;
1356 while (con->pending_msgs_head!=NULL)
1358 remove_http_message(con, con->pending_msgs_head);
1366 * Convert the transports address to a nice, human-readable
1369 * @param cls closure
1370 * @param type name of the transport that generated the address
1371 * @param addr one of the addresses of the host, NULL for the last address
1372 * the specific address format depends on the transport
1373 * @param addrlen length of the address
1374 * @param numeric should (IP) addresses be displayed in numeric form?
1375 * @param timeout after how long should we give up?
1376 * @param asc function to call on each string
1377 * @param asc_cls closure for asc
1380 http_plugin_address_pretty_printer (void *cls,
1385 struct GNUNET_TIME_Relative timeout,
1386 GNUNET_TRANSPORT_AddressStringCallback
1389 const struct IPv4HttpAddress *t4;
1390 const struct IPv6HttpAddress *t6;
1391 struct sockaddr_in a4;
1392 struct sockaddr_in6 a6;
1398 GNUNET_assert(cls !=NULL);
1399 if (addrlen == sizeof (struct IPv6HttpAddress))
1401 address = GNUNET_malloc (INET6_ADDRSTRLEN);
1403 a6.sin6_addr = t6->ipv6_addr;
1404 inet_ntop(AF_INET6, &(a6.sin6_addr),address,INET6_ADDRSTRLEN);
1405 port = ntohs(t6->u6_port);
1407 else if (addrlen == sizeof (struct IPv4HttpAddress))
1409 address = GNUNET_malloc (INET_ADDRSTRLEN);
1411 a4.sin_addr.s_addr = t4->ipv4_addr;
1412 inet_ntop(AF_INET, &(a4.sin_addr),address,INET_ADDRSTRLEN);
1413 port = ntohs(t4->u_port);
1417 /* invalid address */
1418 GNUNET_break_op (0);
1419 asc (asc_cls, NULL);
1422 res = GNUNET_asprintf(&ret,"http://%s:%u/",address,port);
1423 GNUNET_free (address);
1424 GNUNET_assert(res != 0);
1432 * Another peer has suggested an address for this
1433 * peer and transport plugin. Check that this could be a valid
1434 * address. If so, consider adding it to the list
1437 * @param cls closure
1438 * @param addr pointer to the address
1439 * @param addrlen length of addr
1440 * @return GNUNET_OK if this is a plausible address for this peer
1444 http_plugin_address_suggested (void *cls,
1445 const void *addr, size_t addrlen)
1447 struct Plugin *plugin = cls;
1448 struct IPv4HttpAddress *v4;
1449 struct IPv6HttpAddress *v6;
1452 GNUNET_assert(cls !=NULL);
1453 if ((addrlen != sizeof (struct IPv4HttpAddress)) &&
1454 (addrlen != sizeof (struct IPv6HttpAddress)))
1456 return GNUNET_SYSERR;
1458 if (addrlen == sizeof (struct IPv4HttpAddress))
1460 v4 = (struct IPv4HttpAddress *) addr;
1461 if (INADDR_LOOPBACK == ntohl(v4->ipv4_addr))
1463 return GNUNET_SYSERR;
1465 port = ntohs (v4->u_port);
1466 if (port != plugin->port_inbound)
1468 return GNUNET_SYSERR;
1473 v6 = (struct IPv6HttpAddress *) addr;
1474 if (IN6_IS_ADDR_LINKLOCAL (&v6->ipv6_addr))
1476 return GNUNET_SYSERR;
1478 port = ntohs (v6->u6_port);
1479 if (port != plugin->port_inbound)
1481 return GNUNET_SYSERR;
1489 * Function called for a quick conversion of the binary address to
1490 * a numeric address. Note that the caller must not free the
1491 * address and that the next call to this function is allowed
1492 * to override the address again.
1494 * @param cls closure
1495 * @param addr binary address
1496 * @param addrlen length of the address
1497 * @return string representing the same address
1500 http_plugin_address_to_string (void *cls,
1504 const struct IPv4HttpAddress *t4;
1505 const struct IPv6HttpAddress *t6;
1506 struct sockaddr_in a4;
1507 struct sockaddr_in6 a6;
1513 if (addrlen == sizeof (struct IPv6HttpAddress))
1515 address = GNUNET_malloc (INET6_ADDRSTRLEN);
1517 a6.sin6_addr = t6->ipv6_addr;
1518 inet_ntop(AF_INET6, &(a6.sin6_addr),address,INET6_ADDRSTRLEN);
1519 port = ntohs(t6->u6_port);
1521 else if (addrlen == sizeof (struct IPv4HttpAddress))
1523 address = GNUNET_malloc (INET_ADDRSTRLEN);
1525 a4.sin_addr.s_addr = t4->ipv4_addr;
1526 inet_ntop(AF_INET, &(a4.sin_addr),address,INET_ADDRSTRLEN);
1527 port = ntohs(t4->u_port);
1531 /* invalid address */
1534 res = GNUNET_asprintf(&ret,"%s:%u",address,port);
1535 GNUNET_free (address);
1536 GNUNET_assert(res != 0);
1541 * Add the IP of our network interface to the list of
1542 * our external IP addresses.
1544 * @param cls the 'struct Plugin*'
1545 * @param name name of the interface
1546 * @param isDefault do we think this may be our default interface
1547 * @param addr address of the interface
1548 * @param addrlen number of bytes in addr
1549 * @return GNUNET_OK to continue iterating
1552 process_interfaces (void *cls,
1555 const struct sockaddr *addr, socklen_t addrlen)
1557 struct Plugin *plugin = cls;
1558 struct IPv4HttpAddress * t4;
1559 struct IPv6HttpAddress * t6;
1562 GNUNET_assert(cls !=NULL);
1563 af = addr->sa_family;
1566 t4 = GNUNET_malloc(sizeof(struct IPv4HttpAddress));
1567 if (INADDR_LOOPBACK == ntohl(((struct sockaddr_in *) addr)->sin_addr.s_addr))
1569 /* skip loopback addresses */
1572 t4->ipv4_addr = ((struct sockaddr_in *) addr)->sin_addr.s_addr;
1573 t4->u_port = htons (plugin->port_inbound);
1574 plugin->env->notify_address(plugin->env->cls,"http",t4, sizeof (struct IPv4HttpAddress), GNUNET_TIME_UNIT_FOREVER_REL);
1577 else if (af == AF_INET6)
1579 t6 = GNUNET_malloc(sizeof(struct IPv6HttpAddress));
1580 if (IN6_IS_ADDR_LINKLOCAL (&((struct sockaddr_in6 *) addr)->sin6_addr))
1582 /* skip link local addresses */
1585 if (IN6_IS_ADDR_LOOPBACK (&((struct sockaddr_in6 *) addr)->sin6_addr))
1587 /* skip loopback addresses */
1590 memcpy (&t6->ipv6_addr,
1591 &((struct sockaddr_in6 *) addr)->sin6_addr,
1592 sizeof (struct in6_addr));
1593 t6->u6_port = htons (plugin->port_inbound);
1594 plugin->env->notify_address(plugin->env->cls,"http",t6,sizeof (struct IPv6HttpAddress) , GNUNET_TIME_UNIT_FOREVER_REL);
1598 int hashMapFreeIterator (void *cls, const GNUNET_HashCode *key, void *value)
1600 struct Session * cs = value;
1601 struct HTTP_Connection_out * con = cs->outbound_connections_head;
1602 struct HTTP_Connection_out * tmp_con = cs->outbound_connections_head;
1603 struct HTTP_Message * msg = NULL;
1604 struct HTTP_Message * tmp_msg = NULL;
1606 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Freeing session for peer `%s'\n",GNUNET_i2s(&cs->identity));
1608 /* freeing connections */
1611 GNUNET_free(con->url);
1612 if (con->curl_handle!=NULL)
1613 curl_easy_cleanup(con->curl_handle);
1614 con->curl_handle = NULL;
1615 msg = con->pending_msgs_head;
1626 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"All sessions freed \n");
1633 * Exit point from the plugin.
1636 libgnunet_plugin_transport_http_done (void *cls)
1638 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
1639 struct Plugin *plugin = api->cls;
1642 GNUNET_assert(cls !=NULL);
1645 if ( plugin->http_server_task_v4 != GNUNET_SCHEDULER_NO_TASK)
1647 GNUNET_SCHEDULER_cancel(plugin->env->sched, plugin->http_server_task_v4);
1648 plugin->http_server_task_v4 = GNUNET_SCHEDULER_NO_TASK;
1651 if ( plugin->http_server_task_v6 != GNUNET_SCHEDULER_NO_TASK)
1653 GNUNET_SCHEDULER_cancel(plugin->env->sched, plugin->http_server_task_v6);
1654 plugin->http_server_task_v6 = GNUNET_SCHEDULER_NO_TASK;
1657 if ( plugin->http_server_task_send != GNUNET_SCHEDULER_NO_TASK)
1659 GNUNET_SCHEDULER_cancel(plugin->env->sched, plugin->http_server_task_send);
1660 plugin->http_server_task_send = GNUNET_SCHEDULER_NO_TASK;
1663 if (plugin->http_server_daemon_v4 != NULL)
1665 MHD_stop_daemon (plugin->http_server_daemon_v4);
1666 plugin->http_server_daemon_v4 = NULL;
1668 if (plugin->http_server_daemon_v6 != NULL)
1670 MHD_stop_daemon (plugin->http_server_daemon_v6);
1671 plugin->http_server_daemon_v6 = NULL;
1674 /* free all sessions */
1675 GNUNET_CONTAINER_multihashmap_iterate (plugin->sessions,
1676 &hashMapFreeIterator,
1679 GNUNET_CONTAINER_multihashmap_destroy (plugin->sessions);
1681 mret = curl_multi_cleanup(plugin->multi_handle);
1682 if ( CURLM_OK != mret)
1683 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"curl multihandle clean up failed");
1685 GNUNET_free (plugin);
1687 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Unload http plugin complete...\n");
1693 * Entry point for the plugin.
1696 libgnunet_plugin_transport_http_init (void *cls)
1698 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
1699 struct Plugin *plugin;
1700 struct GNUNET_TRANSPORT_PluginFunctions *api;
1701 struct GNUNET_TIME_Relative gn_timeout;
1702 long long unsigned int port;
1704 GNUNET_assert(cls !=NULL);
1705 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Starting http plugin...\n");
1707 plugin = GNUNET_malloc (sizeof (struct Plugin));
1709 plugin->sessions = NULL;
1711 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
1713 api->send = &http_plugin_send;
1714 api->disconnect = &http_plugin_disconnect;
1715 api->address_pretty_printer = &http_plugin_address_pretty_printer;
1716 api->check_address = &http_plugin_address_suggested;
1717 api->address_to_string = &http_plugin_address_to_string;
1719 /* Hashing our identity to use it in URLs */
1720 GNUNET_CRYPTO_hash_to_enc ( &(plugin->env->my_identity->hashPubKey), &plugin->my_ascii_hash_ident);
1722 /* Reading port number from config file */
1724 GNUNET_CONFIGURATION_get_value_number (env->cfg,
1730 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
1733 ("Require valid port number for transport plugin `%s' in configuration!\n"),
1735 libgnunet_plugin_transport_http_done (api);
1738 GNUNET_assert ((port > 0) && (port <= 65535));
1739 plugin->port_inbound = port;
1740 gn_timeout = GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT;
1741 if ((plugin->http_server_daemon_v4 == NULL) && (plugin->http_server_daemon_v6 == NULL) && (port != 0))
1743 plugin->http_server_daemon_v6 = MHD_start_daemon (MHD_USE_IPv6,
1745 &acceptPolicyCallback,
1746 plugin , &accessHandlerCallback, plugin,
1747 MHD_OPTION_CONNECTION_LIMIT, (unsigned int) 16,
1748 MHD_OPTION_PER_IP_CONNECTION_LIMIT, (unsigned int) 1,
1749 MHD_OPTION_CONNECTION_TIMEOUT, (gn_timeout.value / 1000),
1750 MHD_OPTION_CONNECTION_MEMORY_LIMIT, (size_t) (16 * 1024),
1751 MHD_OPTION_NOTIFY_COMPLETED, &requestCompletedCallback, NULL,
1753 plugin->http_server_daemon_v4 = MHD_start_daemon (MHD_NO_FLAG,
1755 &acceptPolicyCallback,
1756 plugin , &accessHandlerCallback, plugin,
1757 MHD_OPTION_CONNECTION_LIMIT, (unsigned int) 16,
1758 MHD_OPTION_PER_IP_CONNECTION_LIMIT, (unsigned int) 1,
1759 MHD_OPTION_CONNECTION_TIMEOUT, (gn_timeout.value / 1000),
1760 MHD_OPTION_CONNECTION_MEMORY_LIMIT, (size_t) (16 * 1024),
1761 MHD_OPTION_NOTIFY_COMPLETED, &requestCompletedCallback, NULL,
1764 if (plugin->http_server_daemon_v4 != NULL)
1765 plugin->http_server_task_v4 = http_server_daemon_prepare (plugin, plugin->http_server_daemon_v4);
1766 if (plugin->http_server_daemon_v6 != NULL)
1767 plugin->http_server_task_v6 = http_server_daemon_prepare (plugin, plugin->http_server_daemon_v6);
1769 if (plugin->http_server_task_v4 != GNUNET_SCHEDULER_NO_TASK)
1770 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Starting MHD with IPv4 on port %u\n",port);
1771 else if (plugin->http_server_task_v6 != GNUNET_SCHEDULER_NO_TASK)
1772 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Starting MHD with IPv4 and IPv6 on port %u\n",port);
1775 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"No MHD was started, transport plugin not functional!\n");
1776 libgnunet_plugin_transport_http_done (api);
1780 /* Initializing cURL */
1781 curl_global_init(CURL_GLOBAL_ALL);
1782 plugin->multi_handle = curl_multi_init();
1784 if ( NULL == plugin->multi_handle )
1786 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
1788 _("Could not initialize curl multi handle, failed to start http plugin!\n"),
1790 libgnunet_plugin_transport_http_done (api);
1794 plugin->sessions = GNUNET_CONTAINER_multihashmap_create (10);
1795 GNUNET_OS_network_interfaces_list (&process_interfaces, plugin);
1800 /* end of plugin_transport_http.c */