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.
83 * Port number, in network byte order.
91 * Network format for IPv6 addresses.
93 struct IPv6HttpAddress
98 struct in6_addr ipv6_addr;
101 * Port number, in network byte order.
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 * Create a new session
343 * @param addr_in address the peer is using inbound
344 * @param addr_out address the peer is using outbound
345 * @param peer identity
346 * @return created session object
348 static struct Session *
349 create_session (void * cls,
354 const struct GNUNET_PeerIdentity *peer)
356 struct Plugin *plugin = cls;
357 struct Session * cs = GNUNET_malloc ( sizeof( struct Session) );
359 GNUNET_assert(cls !=NULL);
362 cs->addr_in = GNUNET_malloc (addrlen_in);
363 cs->addr_in_len = addrlen_in;
364 memcpy(cs->addr_in,addr_in,addrlen_in);
367 if (addrlen_out != 0)
369 cs->addr_out = GNUNET_malloc (addrlen_out);
370 cs->addr_out_len = addrlen_out;
371 memcpy(cs->addr_out,addr_out,addrlen_out);
374 memcpy(&cs->identity, peer, sizeof (struct GNUNET_PeerIdentity));
375 GNUNET_CRYPTO_hash_to_enc(&cs->identity.hashPubKey,&(cs->hash));
376 cs->outbound_connections_head = NULL;
377 cs->outbound_connections_tail = NULL;
382 * Check if session for this peer is already existing, otherwise create it
383 * @param cls the plugin used
384 * @param p peer to get session for
385 * @return session found or created
387 static struct Session * session_get (void * cls, const struct GNUNET_PeerIdentity *p)
389 struct Plugin *plugin = cls;
393 cs = GNUNET_CONTAINER_multihashmap_get (plugin->sessions, &p->hashPubKey);
396 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
397 "Session `%s' found\n", GNUNET_i2s(p));
401 cs = create_session(plugin, NULL, 0, NULL, 0, p);
402 res = GNUNET_CONTAINER_multihashmap_put ( plugin->sessions,
403 &cs->identity.hashPubKey,
405 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
406 if (res == GNUNET_OK)
407 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
408 "New Session `%s' inserted\n", GNUNET_i2s(p));
413 static char * create_url(void * cls, const void * addr, size_t addrlen)
415 struct Plugin *plugin = cls;
419 GNUNET_assert ((addr!=NULL) && (addrlen != 0));
420 if (addrlen == (sizeof (struct IPv4HttpAddress)))
422 address = GNUNET_malloc(INET_ADDRSTRLEN + 1);
423 inet_ntop(AF_INET, &((struct IPv4HttpAddress *) addr)->ipv4_addr,address,INET_ADDRSTRLEN);
424 GNUNET_asprintf (&url,
427 ntohs(((struct IPv4HttpAddress *) addr)->u_port),
428 (char *) (&plugin->my_ascii_hash_ident));
429 GNUNET_free(address);
431 else if (addrlen == (sizeof (struct IPv6HttpAddress)))
433 address = GNUNET_malloc(INET6_ADDRSTRLEN + 1);
434 inet_ntop(AF_INET6, &((struct IPv6HttpAddress *) addr)->ipv6_addr,address,INET6_ADDRSTRLEN);
435 GNUNET_asprintf(&url,
438 ntohs(((struct IPv6HttpAddress *) addr)->u6_port),
439 (char *) (&plugin->my_ascii_hash_ident));
440 GNUNET_free(address);
446 * Check if session already knows this address for a outbound connection to this peer
447 * If address not in session, add it to the session
448 * @param cls the plugin used
449 * @param p the session
450 * @param addr address
451 * @param addr_len address length
452 * @return the found or created address
454 static struct HTTP_Connection_out * session_check_outbound_address (void * cls, struct Session *cs, const void * addr, size_t addr_len)
456 struct Plugin *plugin = cls;
457 struct HTTP_Connection_out * cc = cs->outbound_connections_head;
458 struct HTTP_Connection_out * con = NULL;
460 GNUNET_assert((addr_len == sizeof (struct IPv4HttpAddress)) || (addr_len == sizeof (struct IPv6HttpAddress)));
464 if (addr_len == cc->addrlen)
466 if (0 == memcmp(cc->addr, addr, addr_len))
474 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"No connection info for this address was found\n",GNUNET_i2s(&cs->identity));
477 con = GNUNET_malloc(sizeof(struct HTTP_Connection_out) + addr_len);
478 con->addrlen = addr_len;
480 con->url=create_url(plugin, addr, addr_len);
481 con->connected = GNUNET_NO;
483 memcpy(con->addr, addr, addr_len);
484 GNUNET_CONTAINER_DLL_insert(cs->outbound_connections_head,cs->outbound_connections_tail,con);
491 * Check if session already knows this address for a inbound connection to this peer
492 * If address not in session, add it to the session
493 * @param cls the plugin used
494 * @param p the session
495 * @param addr address
496 * @param addr_len address length
497 * @return the found or created address
499 static struct HTTP_Connection_in * session_check_inbound_address (void * cls, struct Session *cs, const void * addr, size_t addr_len)
501 //struct Plugin *plugin = cls;
502 struct HTTP_Connection_in * cc = cs->inbound_connections_head;
503 struct HTTP_Connection_in * con = NULL;
505 GNUNET_assert((addr_len == sizeof (struct IPv4HttpAddress)) || (addr_len == sizeof (struct IPv6HttpAddress)));
509 if (addr_len == cc->addrlen)
511 if (0 == memcmp(cc->addr, addr, addr_len))
519 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"No connection info for this address was found\n",GNUNET_i2s(&cs->identity));
522 con = GNUNET_malloc(sizeof(struct HTTP_Connection_in) + addr_len);
523 con->addrlen = addr_len;
525 con->connected = GNUNET_NO;
527 memcpy(con->addr, addr, addr_len);
528 GNUNET_CONTAINER_DLL_insert(cs->inbound_connections_head,cs->inbound_connections_tail,con);
535 * Callback called by MHD when a connection is terminated
537 static void requestCompletedCallback (void *cls, struct MHD_Connection * connection, void **httpSessionCache)
539 struct HTTP_Connection_in * con;
541 con = *httpSessionCache;
544 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection from peer `%s' was terminated\n",GNUNET_i2s(&con->session->identity));
545 /* session set to inactive */
546 con->is_put_in_progress = GNUNET_NO;
547 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 "Received message with type %u and size %u from `%s'\n",
560 ntohs(message->type),
561 ntohs(message->size),
562 GNUNET_i2s(&(con->session->identity)));
563 con->session->plugin->env->receive (con->session->plugin->env->cls,
564 &con->session->identity,
565 message, 1, con->session,
571 * Check if ip is allowed to connect.
574 acceptPolicyCallback (void *cls,
575 const struct sockaddr *addr, socklen_t addr_len)
578 struct Plugin *plugin = cls;
580 /* Every connection is accepted, nothing more to do here */
585 * Process GET or PUT request received via MHD. For
586 * GET, queue response that will send back our pending
587 * messages. For PUT, process incoming data and send
588 * to GNUnet core. In either case, check if a session
589 * already exists and create a new one if not.
592 accessHandlerCallback (void *cls,
593 struct MHD_Connection *mhd_connection,
597 const char *upload_data,
598 size_t * upload_data_size, void **httpSessionCache)
600 struct Plugin *plugin = cls;
601 struct MHD_Response *response;
603 struct HTTP_Connection_in * con;
604 const union MHD_ConnectionInfo * conn_info;
605 struct sockaddr_in *addrin;
606 struct sockaddr_in6 *addrin6;
607 char address[INET6_ADDRSTRLEN+14];
608 struct GNUNET_PeerIdentity pi_in;
610 int send_error_to_client;
611 struct IPv4HttpAddress ipv4addr;
612 struct IPv6HttpAddress ipv6addr;
614 GNUNET_assert(cls !=NULL);
615 send_error_to_client = GNUNET_NO;
617 if ( NULL == *httpSessionCache)
619 /* check url for peer identity , if invalid send HTTP 404*/
620 res = GNUNET_CRYPTO_hash_from_string ( &url[1], &(pi_in.hashPubKey));
621 if ( GNUNET_SYSERR == res )
623 response = MHD_create_response_from_data (strlen (HTTP_ERROR_RESPONSE),HTTP_ERROR_RESPONSE, MHD_NO, MHD_NO);
624 res = MHD_queue_response (mhd_connection, MHD_HTTP_NOT_FOUND, response);
625 MHD_destroy_response (response);
627 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Peer has no valid ident, sent HTTP 1.1/404\n");
629 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Peer has no valid ident, could not send error\n");
633 /* get session for peer identity */
634 cs = session_get (plugin ,&pi_in);
636 conn_info = MHD_get_connection_info(mhd_connection, MHD_CONNECTION_INFO_CLIENT_ADDRESS );
637 /* Incoming IPv4 connection */
638 if ( AF_INET == conn_info->client_addr->sin_family)
640 addrin = conn_info->client_addr;
641 inet_ntop(addrin->sin_family, &(addrin->sin_addr),address,INET_ADDRSTRLEN);
642 memcpy(&ipv4addr.ipv4_addr,&(addrin->sin_addr),sizeof(struct in_addr));
643 ipv4addr.u_port = addrin->sin_port;
644 con = session_check_inbound_address (plugin, cs, (const void *) &ipv4addr, sizeof (struct IPv4HttpAddress));
646 /* Incoming IPv6 connection */
647 if ( AF_INET6 == conn_info->client_addr->sin_family)
649 addrin6 = (struct sockaddr_in6 *) conn_info->client_addr;
650 inet_ntop(addrin6->sin6_family, &(addrin6->sin6_addr),address,INET6_ADDRSTRLEN);
651 memcpy(&ipv6addr.ipv6_addr,&(addrin6->sin6_addr),sizeof(struct in_addr));
652 ipv6addr.u6_port = addrin6->sin6_port;
653 con = session_check_inbound_address (plugin, cs, &ipv6addr, sizeof (struct IPv6HttpAddress));
655 /* Set closure and update current session*/
657 *httpSessionCache = con;
658 if (con->msgtok==NULL)
659 con->msgtok = GNUNET_SERVER_mst_create (GNUNET_SERVER_MAX_MESSAGE_SIZE - 1, &messageTokenizerCallback, con);
661 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"HTTP Daemon has new an incoming `%s' request from peer `%s'@`%s'\n",method, GNUNET_i2s(&cs->identity),address);
665 con = *httpSessionCache;
669 /* Is it a PUT or a GET request */
670 if (0 == strcmp (MHD_HTTP_METHOD_PUT, method))
672 if ((*upload_data_size == 0) && (con->is_put_in_progress==GNUNET_NO))
674 con->is_put_in_progress = GNUNET_YES;
678 /* Transmission of all data complete */
679 if ((*upload_data_size == 0) && (con->is_put_in_progress == GNUNET_YES))
681 response = MHD_create_response_from_data (strlen (HTTP_PUT_RESPONSE),HTTP_PUT_RESPONSE, MHD_NO, MHD_NO);
682 res = MHD_queue_response (mhd_connection, MHD_HTTP_OK, response);
683 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Sent HTTP/1.1: 200 OK as PUT Response\n",HTTP_PUT_RESPONSE, strlen (HTTP_PUT_RESPONSE), res );
684 MHD_destroy_response (response);
687 con->is_put_in_progress = GNUNET_NO;
688 con->is_bad_request = GNUNET_NO;
693 if ((*upload_data_size > 0) && (con->is_put_in_progress == GNUNET_YES))
695 res = GNUNET_SERVER_mst_receive(con->msgtok, con, upload_data,*upload_data_size, GNUNET_NO, GNUNET_NO);
696 (*upload_data_size) = 0;
702 if ( 0 == strcmp (MHD_HTTP_METHOD_GET, method) )
704 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Got GET Request\n");
705 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"URL: `%s'\n",url);
706 response = MHD_create_response_from_data (strlen (HTTP_PUT_RESPONSE),HTTP_PUT_RESPONSE, MHD_NO, MHD_NO);
707 res = MHD_queue_response (mhd_connection, MHD_HTTP_OK, response);
708 MHD_destroy_response (response);
716 * Call MHD to process pending ipv4 requests and then go back
717 * and schedule the next run.
719 static void http_server_daemon_v4_run (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
721 * Call MHD to process pending ipv6 requests and then go back
722 * and schedule the next run.
724 static void http_server_daemon_v6_run (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
727 * Function that queries MHD's select sets and
728 * starts the task waiting for them.
730 static GNUNET_SCHEDULER_TaskIdentifier
731 http_server_daemon_prepare (void * cls, struct MHD_Daemon *daemon_handle)
733 struct Plugin *plugin = cls;
734 GNUNET_SCHEDULER_TaskIdentifier ret;
738 struct GNUNET_NETWORK_FDSet *wrs;
739 struct GNUNET_NETWORK_FDSet *wws;
740 struct GNUNET_NETWORK_FDSet *wes;
742 unsigned long long timeout;
744 struct GNUNET_TIME_Relative tv;
746 GNUNET_assert(cls !=NULL);
747 ret = GNUNET_SCHEDULER_NO_TASK;
751 wrs = GNUNET_NETWORK_fdset_create ();
752 wes = GNUNET_NETWORK_fdset_create ();
753 wws = GNUNET_NETWORK_fdset_create ();
755 GNUNET_assert (MHD_YES ==
756 MHD_get_fdset (daemon_handle,
761 haveto = MHD_get_timeout (daemon_handle, &timeout);
762 if (haveto == MHD_YES)
763 tv.value = (uint64_t) timeout;
765 tv = GNUNET_TIME_UNIT_FOREVER_REL;
766 GNUNET_NETWORK_fdset_copy_native (wrs, &rs, max);
767 GNUNET_NETWORK_fdset_copy_native (wws, &ws, max);
768 GNUNET_NETWORK_fdset_copy_native (wes, &es, max);
769 if (daemon_handle == plugin->http_server_daemon_v4)
771 ret = GNUNET_SCHEDULER_add_select (plugin->env->sched,
772 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
773 GNUNET_SCHEDULER_NO_TASK,
777 &http_server_daemon_v4_run,
780 if (daemon_handle == plugin->http_server_daemon_v6)
782 ret = GNUNET_SCHEDULER_add_select (plugin->env->sched,
783 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
784 GNUNET_SCHEDULER_NO_TASK,
788 &http_server_daemon_v6_run,
791 GNUNET_NETWORK_fdset_destroy (wrs);
792 GNUNET_NETWORK_fdset_destroy (wws);
793 GNUNET_NETWORK_fdset_destroy (wes);
798 * Call MHD to process pending requests and then go back
799 * and schedule the next run.
801 static void http_server_daemon_v4_run (void *cls,
802 const struct GNUNET_SCHEDULER_TaskContext *tc)
804 struct Plugin *plugin = cls;
806 GNUNET_assert(cls !=NULL);
807 if (plugin->http_server_task_v4 != GNUNET_SCHEDULER_NO_TASK)
808 plugin->http_server_task_v4 = GNUNET_SCHEDULER_NO_TASK;
810 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
813 GNUNET_assert (MHD_YES == MHD_run (plugin->http_server_daemon_v4));
814 plugin->http_server_task_v4 = http_server_daemon_prepare (plugin, plugin->http_server_daemon_v4);
820 * Call MHD to process pending requests and then go back
821 * and schedule the next run.
823 static void http_server_daemon_v6_run (void *cls,
824 const struct GNUNET_SCHEDULER_TaskContext *tc)
826 struct Plugin *plugin = cls;
828 GNUNET_assert(cls !=NULL);
829 if (plugin->http_server_task_v6 != GNUNET_SCHEDULER_NO_TASK)
830 plugin->http_server_task_v6 = GNUNET_SCHEDULER_NO_TASK;
832 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
835 GNUNET_assert (MHD_YES == MHD_run (plugin->http_server_daemon_v6));
836 plugin->http_server_task_v6 = http_server_daemon_prepare (plugin, plugin->http_server_daemon_v6);
841 * Removes a message from the linked list of messages
842 * @param ses session to remove message from
843 * @param msg message to remove
844 * @return GNUNET_SYSERR if msg not found, GNUNET_OK on success
847 static int remove_http_message(struct HTTP_Connection_out * con, struct HTTP_Message * msg)
849 GNUNET_CONTAINER_DLL_remove(con->pending_msgs_head,con->pending_msgs_tail,msg);
855 static size_t header_function( void *ptr, size_t size, size_t nmemb, void *stream)
858 size_t len = size * nmemb;
861 if ((size * nmemb) < SIZE_MAX)
862 tmp = GNUNET_malloc (len+1);
864 if ((tmp != NULL) && (len > 0))
869 if (tmp[len-2] == 13)
873 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Header: `%s'\n",tmp);
883 * Callback method used with libcurl
884 * Method is called when libcurl needs to read data during sending
885 * @param stream pointer where to write data
886 * @param size size of an individual element
887 * @param nmemb count of elements that can be written to the buffer
888 * @param ptr source pointer, passed to the libcurl handle
889 * @return bytes written to stream
891 static size_t send_read_callback(void *stream, size_t size, size_t nmemb, void *ptr)
893 struct HTTP_Connection_out * con = ptr;
894 struct HTTP_Message * msg = con->pending_msgs_tail;
898 if (con->pending_msgs_tail == NULL)
900 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection: %X: No Message to send, pausing connection\n",con);
901 con->send_paused = GNUNET_YES;
902 return CURL_READFUNC_PAUSE;
905 msg = con->pending_msgs_tail;
907 if (msg->pos < msg->size)
909 /* data fit in buffer */
910 if ((msg->size - msg->pos) <= (size * nmemb))
912 len = (msg->size - msg->pos);
913 memcpy(stream, &msg->buf[msg->pos], len);
920 memcpy(stream, &msg->buf[msg->pos], len);
925 /* no data to send */
931 if ( msg->pos == msg->size)
933 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection: %X: Message with %u bytes sent, removing message from queue \n",con, msg->pos);
934 /* Calling transmit continuation */
935 if (( NULL != con->pending_msgs_tail) && (NULL != con->pending_msgs_tail->transmit_cont))
936 msg->transmit_cont (con->pending_msgs_tail->transmit_cont_cls,&(con->session)->identity,GNUNET_OK);
937 remove_http_message(con, msg);
943 * Callback method used with libcurl
944 * Method is called when libcurl needs to write data during sending
945 * @param stream pointer where to write data
946 * @param size size of an individual element
947 * @param nmemb count of elements that can be written to the buffer
948 * @param ptr destination pointer, passed to the libcurl handle
949 * @return bytes read from stream
951 static size_t send_write_callback( void *stream, size_t size, size_t nmemb, void *ptr)
955 if ((size * nmemb) < SIZE_MAX)
956 data = GNUNET_malloc(size*nmemb +1);
959 memcpy( data, stream, size*nmemb);
960 data[size*nmemb] = '\0';
963 return (size * nmemb);
968 * Function setting up file descriptors and scheduling task to run
969 * @param ses session to send data to
970 * @return bytes sent to peer
972 static size_t send_schedule(void *cls, struct Session* ses );
975 * Function setting up curl handle and selecting message to send
976 * @param ses session to send data to
977 * @return bytes sent to peer
979 static ssize_t send_initiate (void *cls, struct Session* ses , struct HTTP_Connection_out *con)
981 struct Plugin *plugin = cls;
984 struct HTTP_Message * msg;
985 struct GNUNET_TIME_Relative timeout = GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT;
987 /* already connected, no need to initiate connection */
988 if ((con->connected == GNUNET_YES) && (con->curl_handle != NULL) && (con->send_paused == GNUNET_NO))
990 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection: %X: active, enqueueing message\n",con);
994 if ((con->connected == GNUNET_YES) && (con->curl_handle != NULL) && (con->send_paused == GNUNET_YES))
996 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection: %X: paused, unpausing existing connection and enqueueing message\n",con);
997 curl_easy_pause(con->curl_handle,CURLPAUSE_CONT);
998 con->send_paused=GNUNET_NO;
1002 /* not connected, initiate connection */
1003 GNUNET_assert(cls !=NULL);
1005 if ( NULL == con->curl_handle)
1006 con->curl_handle = curl_easy_init();
1007 GNUNET_assert (con->curl_handle != NULL);
1008 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection: %X: not existing, creating new connection\n",con);
1010 GNUNET_assert (NULL != con->pending_msgs_tail);
1011 msg = con->pending_msgs_tail;
1014 curl_easy_setopt(con->curl_handle, CURLOPT_VERBOSE, 1L);
1016 curl_easy_setopt(con->curl_handle, CURLOPT_URL, con->url);
1017 curl_easy_setopt(con->curl_handle, CURLOPT_PUT, 1L);
1018 curl_easy_setopt(con->curl_handle, CURLOPT_HEADERFUNCTION, &header_function);
1019 curl_easy_setopt(con->curl_handle, CURLOPT_WRITEHEADER, con);
1020 curl_easy_setopt(con->curl_handle, CURLOPT_READFUNCTION, send_read_callback);
1021 curl_easy_setopt(con->curl_handle, CURLOPT_READDATA, con);
1022 curl_easy_setopt(con->curl_handle, CURLOPT_WRITEFUNCTION, send_write_callback);
1023 curl_easy_setopt(con->curl_handle, CURLOPT_READDATA, con);
1024 curl_easy_setopt(con->curl_handle, CURLOPT_TIMEOUT, (long) timeout.value);
1025 curl_easy_setopt(con->curl_handle, CURLOPT_PRIVATE, con);
1026 curl_easy_setopt(con->curl_handle, CURLOPT_CONNECTTIMEOUT, HTTP_CONNECT_TIMEOUT_DBG);
1027 curl_easy_setopt(con->curl_handle, CURLOPT_BUFFERSIZE, GNUNET_SERVER_MAX_MESSAGE_SIZE);
1029 mret = curl_multi_add_handle(plugin->multi_handle, con->curl_handle);
1030 if (mret != CURLM_OK)
1032 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1033 _("%s failed at %s:%d: `%s'\n"),
1034 "curl_multi_add_handle", __FILE__, __LINE__,
1035 curl_multi_strerror (mret));
1039 con->connected = GNUNET_YES;
1041 bytes_sent = send_schedule (plugin, ses);
1045 static void send_execute (void *cls,
1046 const struct GNUNET_SCHEDULER_TaskContext *tc)
1048 struct Plugin *plugin = cls;
1049 static unsigned int handles_last_run;
1051 struct CURLMsg *msg;
1053 struct HTTP_Connection_out * con = NULL;
1054 struct Session * cs = NULL;
1057 GNUNET_assert(cls !=NULL);
1058 plugin->http_server_task_send = GNUNET_SCHEDULER_NO_TASK;
1059 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1065 mret = curl_multi_perform (plugin->multi_handle, &running);
1066 if (running < handles_last_run)
1071 msg = curl_multi_info_read (plugin->multi_handle, &running);
1072 GNUNET_break (msg != NULL);
1075 /* get session for affected curl handle */
1076 GNUNET_assert ( msg->easy_handle != NULL );
1077 curl_easy_getinfo(msg->easy_handle, CURLINFO_PRIVATE, (char *) &con);
1078 GNUNET_assert ( con != NULL );
1080 GNUNET_assert ( cs != NULL );
1085 if ( (msg->data.result != CURLE_OK) &&
1086 (msg->data.result != CURLE_GOT_NOTHING) )
1088 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1089 _("%s failed for `%s' at %s:%d: `%s'\n"),
1090 "curl_multi_perform",
1091 GNUNET_i2s(&cs->identity),
1094 curl_easy_strerror (msg->data.result));
1095 /* sending msg failed*/
1096 con->connected = GNUNET_NO;
1097 if (( NULL != con->pending_msgs_tail) && ( NULL != con->pending_msgs_tail->transmit_cont))
1098 con->pending_msgs_tail->transmit_cont (con->pending_msgs_tail->transmit_cont_cls,&con->session->identity,GNUNET_SYSERR);
1103 GNUNET_assert (CURLE_OK == curl_easy_getinfo(msg->easy_handle, CURLINFO_RESPONSE_CODE, &http_result));
1104 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1105 "Send to peer `%s' completed with code %u\n", GNUNET_i2s(&cs->identity), http_result );
1107 curl_easy_cleanup(con->curl_handle);
1108 con->connected = GNUNET_NO;
1109 con->curl_handle=NULL;
1111 /* Calling transmit continuation */
1112 if (( NULL != con->pending_msgs_tail) && (NULL != con->pending_msgs_tail->transmit_cont))
1114 /* HTTP 1xx : Last message before here was informational */
1115 if ((http_result >=100) && (http_result < 200))
1116 con->pending_msgs_tail->transmit_cont (con->pending_msgs_tail->transmit_cont_cls,&cs->identity,GNUNET_OK);
1117 /* HTTP 2xx: successful operations */
1118 if ((http_result >=200) && (http_result < 300))
1119 con->pending_msgs_tail->transmit_cont (con->pending_msgs_tail->transmit_cont_cls,&cs->identity,GNUNET_OK);
1120 /* HTTP 3xx..5xx: error */
1121 if ((http_result >=300) && (http_result < 600))
1122 con->pending_msgs_tail->transmit_cont (con->pending_msgs_tail->transmit_cont_cls,&cs->identity,GNUNET_SYSERR);
1125 if (con->pending_msgs_tail != NULL)
1127 if (con->pending_msgs_tail->pos>0)
1128 remove_http_message(con, con->pending_msgs_tail);
1129 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Message could not be removed from session `%s'\n", GNUNET_i2s(&cs->identity));
1137 while ( (running > 0) );
1139 handles_last_run = running;
1141 while (mret == CURLM_CALL_MULTI_PERFORM);
1142 send_schedule(plugin, cls);
1147 * Function setting up file descriptors and scheduling task to run
1148 * @param ses session to send data to
1149 * @return bytes sent to peer
1151 static size_t send_schedule(void *cls, struct Session* ses )
1153 struct Plugin *plugin = cls;
1158 struct GNUNET_NETWORK_FDSet *grs;
1159 struct GNUNET_NETWORK_FDSet *gws;
1163 GNUNET_assert(cls !=NULL);
1168 mret = curl_multi_fdset (plugin->multi_handle, &rs, &ws, &es, &max);
1169 if (mret != CURLM_OK)
1171 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1172 _("%s failed at %s:%d: `%s'\n"),
1173 "curl_multi_fdset", __FILE__, __LINE__,
1174 curl_multi_strerror (mret));
1177 mret = curl_multi_timeout (plugin->multi_handle, &to);
1178 if (mret != CURLM_OK)
1180 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1181 _("%s failed at %s:%d: `%s'\n"),
1182 "curl_multi_timeout", __FILE__, __LINE__,
1183 curl_multi_strerror (mret));
1187 grs = GNUNET_NETWORK_fdset_create ();
1188 gws = GNUNET_NETWORK_fdset_create ();
1189 GNUNET_NETWORK_fdset_copy_native (grs, &rs, max + 1);
1190 GNUNET_NETWORK_fdset_copy_native (gws, &ws, max + 1);
1191 plugin->http_server_task_send = GNUNET_SCHEDULER_add_select (plugin->env->sched,
1192 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1193 GNUNET_SCHEDULER_NO_TASK,
1194 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 0),
1199 GNUNET_NETWORK_fdset_destroy (gws);
1200 GNUNET_NETWORK_fdset_destroy (grs);
1202 /* FIXME: return bytes REALLY sent */
1208 * Function that can be used by the transport service to transmit
1209 * a message using the plugin.
1211 * @param cls closure
1212 * @param target who should receive this message
1213 * @param priority how important is the message
1214 * @param msgbuf the message to transmit
1215 * @param msgbuf_size number of bytes in 'msgbuf'
1216 * @param to when should we time out
1217 * @param session which session must be used (or NULL for "any")
1218 * @param addr the address to use (can be NULL if the plugin
1219 * is "on its own" (i.e. re-use existing TCP connection))
1220 * @param addrlen length of the address in bytes
1221 * @param force_address GNUNET_YES if the plugin MUST use the given address,
1222 * otherwise the plugin may use other addresses or
1223 * existing connections (if available)
1224 * @param cont continuation to call once the message has
1225 * been transmitted (or if the transport is ready
1226 * for the next transmission call; or if the
1227 * peer disconnected...)
1228 * @param cont_cls closure for cont
1229 * @return number of bytes used (on the physical network, with overheads);
1230 * -1 on hard errors (i.e. address invalid); 0 is a legal value
1231 * and does NOT mean that the message was not transmitted (DV)
1234 http_plugin_send (void *cls,
1235 const struct GNUNET_PeerIdentity *target,
1238 unsigned int priority,
1239 struct GNUNET_TIME_Relative to,
1240 struct Session *session,
1244 GNUNET_TRANSPORT_TransmitContinuation cont,
1247 struct Plugin *plugin = cls;
1251 struct HTTP_Message *msg;
1252 struct HTTP_Connection_out *con;
1255 GNUNET_assert(cls !=NULL);
1259 /* get session from hashmap */
1260 cs = session_get(plugin, target);
1261 con = session_check_outbound_address(plugin, cs, addr, addrlen);
1264 msg = GNUNET_malloc (sizeof (struct HTTP_Message) + msgbuf_size);
1266 msg->size = msgbuf_size;
1268 msg->buf = (char *) &msg[1];
1269 msg->transmit_cont = cont;
1270 msg->transmit_cont_cls = cont_cls;
1271 memcpy (msg->buf,msgbuf, msgbuf_size);
1273 /* must use this address */
1274 if (force_address == GNUNET_YES)
1276 /* enqueue in connection message queue */
1277 GNUNET_CONTAINER_DLL_insert(con->pending_msgs_head,con->pending_msgs_tail,msg);
1279 /* can use existing connection to send */
1282 /* enqueue in connection message queue */
1283 GNUNET_CONTAINER_DLL_insert(con->pending_msgs_head,con->pending_msgs_tail,msg);
1285 return send_initiate (plugin, cs, con);
1291 * Function that can be used to force the plugin to disconnect
1292 * from the given peer and cancel all previous transmissions
1293 * (and their continuationc).
1295 * @param cls closure
1296 * @param target peer from which to disconnect
1299 http_plugin_disconnect (void *cls,
1300 const struct GNUNET_PeerIdentity *target)
1302 struct Plugin *plugin = cls;
1303 struct HTTP_Connection_out *con;
1306 /* get session from hashmap */
1307 cs = session_get(plugin, target);
1308 con = cs->outbound_connections_head;
1312 if (con->curl_handle!=NULL)
1313 curl_easy_cleanup(con->curl_handle);
1314 con->curl_handle=NULL;
1315 con->connected = GNUNET_NO;
1316 while (con->pending_msgs_head!=NULL)
1318 remove_http_message(con, con->pending_msgs_head);
1326 * Convert the transports address to a nice, human-readable
1329 * @param cls closure
1330 * @param type name of the transport that generated the address
1331 * @param addr one of the addresses of the host, NULL for the last address
1332 * the specific address format depends on the transport
1333 * @param addrlen length of the address
1334 * @param numeric should (IP) addresses be displayed in numeric form?
1335 * @param timeout after how long should we give up?
1336 * @param asc function to call on each string
1337 * @param asc_cls closure for asc
1340 http_plugin_address_pretty_printer (void *cls,
1345 struct GNUNET_TIME_Relative timeout,
1346 GNUNET_TRANSPORT_AddressStringCallback
1349 const struct IPv4HttpAddress *t4;
1350 const struct IPv6HttpAddress *t6;
1351 struct sockaddr_in a4;
1352 struct sockaddr_in6 a6;
1358 GNUNET_assert(cls !=NULL);
1359 if (addrlen == sizeof (struct IPv6HttpAddress))
1361 address = GNUNET_malloc (INET6_ADDRSTRLEN);
1363 a6.sin6_addr = t6->ipv6_addr;
1364 inet_ntop(AF_INET6, &(a6.sin6_addr),address,INET6_ADDRSTRLEN);
1365 port = ntohs(t6->u6_port);
1367 else if (addrlen == sizeof (struct IPv4HttpAddress))
1369 address = GNUNET_malloc (INET_ADDRSTRLEN);
1371 a4.sin_addr.s_addr = t4->ipv4_addr;
1372 inet_ntop(AF_INET, &(a4.sin_addr),address,INET_ADDRSTRLEN);
1373 port = ntohs(t4->u_port);
1377 /* invalid address */
1378 GNUNET_break_op (0);
1379 asc (asc_cls, NULL);
1382 res = GNUNET_asprintf(&ret,"http://%s:%u/",address,port);
1383 GNUNET_free (address);
1384 GNUNET_assert(res != 0);
1392 * Another peer has suggested an address for this
1393 * peer and transport plugin. Check that this could be a valid
1394 * address. If so, consider adding it to the list
1397 * @param cls closure
1398 * @param addr pointer to the address
1399 * @param addrlen length of addr
1400 * @return GNUNET_OK if this is a plausible address for this peer
1404 http_plugin_address_suggested (void *cls,
1405 const void *addr, size_t addrlen)
1407 struct Plugin *plugin = cls;
1408 struct IPv4HttpAddress *v4;
1409 struct IPv6HttpAddress *v6;
1412 GNUNET_assert(cls !=NULL);
1413 if ((addrlen != sizeof (struct IPv4HttpAddress)) &&
1414 (addrlen != sizeof (struct IPv6HttpAddress)))
1416 return GNUNET_SYSERR;
1418 if (addrlen == sizeof (struct IPv4HttpAddress))
1420 v4 = (struct IPv4HttpAddress *) addr;
1421 if (INADDR_LOOPBACK == ntohl(v4->ipv4_addr))
1423 return GNUNET_SYSERR;
1425 port = ntohs (v4->u_port);
1426 if (port != plugin->port_inbound)
1428 return GNUNET_SYSERR;
1433 v6 = (struct IPv6HttpAddress *) addr;
1434 if (IN6_IS_ADDR_LINKLOCAL (&v6->ipv6_addr))
1436 return GNUNET_SYSERR;
1438 port = ntohs (v6->u6_port);
1439 if (port != plugin->port_inbound)
1441 return GNUNET_SYSERR;
1449 * Function called for a quick conversion of the binary address to
1450 * a numeric address. Note that the caller must not free the
1451 * address and that the next call to this function is allowed
1452 * to override the address again.
1454 * @param cls closure
1455 * @param addr binary address
1456 * @param addrlen length of the address
1457 * @return string representing the same address
1460 http_plugin_address_to_string (void *cls,
1464 const struct IPv4HttpAddress *t4;
1465 const struct IPv6HttpAddress *t6;
1466 struct sockaddr_in a4;
1467 struct sockaddr_in6 a6;
1473 GNUNET_assert(cls !=NULL);
1474 if (addrlen == sizeof (struct IPv6HttpAddress))
1476 address = GNUNET_malloc (INET6_ADDRSTRLEN);
1478 a6.sin6_addr = t6->ipv6_addr;
1479 inet_ntop(AF_INET6, &(a6.sin6_addr),address,INET6_ADDRSTRLEN);
1480 port = ntohs(t6->u6_port);
1482 else if (addrlen == sizeof (struct IPv4HttpAddress))
1484 address = GNUNET_malloc (INET_ADDRSTRLEN);
1486 a4.sin_addr.s_addr = t4->ipv4_addr;
1487 inet_ntop(AF_INET, &(a4.sin_addr),address,INET_ADDRSTRLEN);
1488 port = ntohs(t4->u_port);
1492 /* invalid address */
1495 res = GNUNET_asprintf(&ret,"%s:%u",address,port);
1496 GNUNET_free (address);
1497 GNUNET_assert(res != 0);
1502 * Add the IP of our network interface to the list of
1503 * our external IP addresses.
1505 * @param cls the 'struct Plugin*'
1506 * @param name name of the interface
1507 * @param isDefault do we think this may be our default interface
1508 * @param addr address of the interface
1509 * @param addrlen number of bytes in addr
1510 * @return GNUNET_OK to continue iterating
1513 process_interfaces (void *cls,
1516 const struct sockaddr *addr, socklen_t addrlen)
1518 struct Plugin *plugin = cls;
1519 struct IPv4HttpAddress t4;
1520 struct IPv6HttpAddress t6;
1525 GNUNET_assert(cls !=NULL);
1526 af = addr->sa_family;
1529 if (INADDR_LOOPBACK == ntohl(((struct sockaddr_in *) addr)->sin_addr.s_addr))
1531 /* skip loopback addresses */
1534 t4.ipv4_addr = ((struct sockaddr_in *) addr)->sin_addr.s_addr;
1535 t4.u_port = htons (plugin->port_inbound);
1539 else if (af == AF_INET6)
1541 if (IN6_IS_ADDR_LINKLOCAL (&((struct sockaddr_in6 *) addr)->sin6_addr))
1543 /* skip link local addresses */
1546 if (IN6_IS_ADDR_LOOPBACK (&((struct sockaddr_in6 *) addr)->sin6_addr))
1548 /* skip loopback addresses */
1551 memcpy (&t6.ipv6_addr,
1552 &((struct sockaddr_in6 *) addr)->sin6_addr,
1553 sizeof (struct in6_addr));
1554 t6.u6_port = htons (plugin->port_inbound);
1563 plugin->env->notify_address(plugin->env->cls,"http",arg, args, GNUNET_TIME_UNIT_FOREVER_REL);
1567 int hashMapFreeIterator (void *cls, const GNUNET_HashCode *key, void *value)
1569 struct Session * cs = value;
1570 struct HTTP_Connection_out * con = cs->outbound_connections_head;
1571 struct HTTP_Connection_out * tmp_con = cs->outbound_connections_head;
1572 struct HTTP_Message * msg = NULL;
1573 struct HTTP_Message * tmp_msg = NULL;
1575 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Freeing session for peer `%s'\n",GNUNET_i2s(&cs->identity));
1577 /* freeing connections */
1580 GNUNET_free(con->url);
1581 if (con->curl_handle!=NULL)
1582 curl_easy_cleanup(con->curl_handle);
1583 con->curl_handle = NULL;
1584 msg = con->pending_msgs_head;
1595 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"All sessions freed \n");
1602 * Exit point from the plugin.
1605 libgnunet_plugin_transport_http_done (void *cls)
1607 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
1608 struct Plugin *plugin = api->cls;
1611 GNUNET_assert(cls !=NULL);
1614 if ( plugin->http_server_task_v4 != GNUNET_SCHEDULER_NO_TASK)
1616 GNUNET_SCHEDULER_cancel(plugin->env->sched, plugin->http_server_task_v4);
1617 plugin->http_server_task_v4 = GNUNET_SCHEDULER_NO_TASK;
1620 if ( plugin->http_server_task_v6 != GNUNET_SCHEDULER_NO_TASK)
1622 GNUNET_SCHEDULER_cancel(plugin->env->sched, plugin->http_server_task_v6);
1623 plugin->http_server_task_v6 = GNUNET_SCHEDULER_NO_TASK;
1626 if ( plugin->http_server_task_send != GNUNET_SCHEDULER_NO_TASK)
1628 GNUNET_SCHEDULER_cancel(plugin->env->sched, plugin->http_server_task_send);
1629 plugin->http_server_task_send = GNUNET_SCHEDULER_NO_TASK;
1632 if (plugin->http_server_daemon_v4 != NULL)
1634 MHD_stop_daemon (plugin->http_server_daemon_v4);
1635 plugin->http_server_daemon_v4 = NULL;
1637 if (plugin->http_server_daemon_v6 != NULL)
1639 MHD_stop_daemon (plugin->http_server_daemon_v6);
1640 plugin->http_server_daemon_v6 = NULL;
1643 /* free all sessions */
1644 GNUNET_CONTAINER_multihashmap_iterate (plugin->sessions,
1645 &hashMapFreeIterator,
1648 GNUNET_CONTAINER_multihashmap_destroy (plugin->sessions);
1650 mret = curl_multi_cleanup(plugin->multi_handle);
1651 if ( CURLM_OK != mret)
1652 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"curl multihandle clean up failed");
1654 GNUNET_free (plugin);
1656 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Unload http plugin complete...\n");
1662 * Entry point for the plugin.
1665 libgnunet_plugin_transport_http_init (void *cls)
1667 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
1668 struct Plugin *plugin;
1669 struct GNUNET_TRANSPORT_PluginFunctions *api;
1670 struct GNUNET_TIME_Relative gn_timeout;
1671 long long unsigned int port;
1673 GNUNET_assert(cls !=NULL);
1674 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Starting http plugin...\n");
1676 plugin = GNUNET_malloc (sizeof (struct Plugin));
1678 plugin->sessions = NULL;
1680 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
1682 api->send = &http_plugin_send;
1683 api->disconnect = &http_plugin_disconnect;
1684 api->address_pretty_printer = &http_plugin_address_pretty_printer;
1685 api->check_address = &http_plugin_address_suggested;
1686 api->address_to_string = &http_plugin_address_to_string;
1688 /* Hashing our identity to use it in URLs */
1689 GNUNET_CRYPTO_hash_to_enc ( &(plugin->env->my_identity->hashPubKey), &plugin->my_ascii_hash_ident);
1691 /* Reading port number from config file */
1693 GNUNET_CONFIGURATION_get_value_number (env->cfg,
1699 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
1702 ("Require valid port number for transport plugin `%s' in configuration!\n"),
1704 libgnunet_plugin_transport_http_done (api);
1707 GNUNET_assert ((port > 0) && (port <= 65535));
1708 plugin->port_inbound = port;
1709 gn_timeout = GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT;
1710 if ((plugin->http_server_daemon_v4 == NULL) && (plugin->http_server_daemon_v6 == NULL) && (port != 0))
1712 plugin->http_server_daemon_v6 = MHD_start_daemon (MHD_USE_IPv6,
1714 &acceptPolicyCallback,
1715 plugin , &accessHandlerCallback, plugin,
1716 MHD_OPTION_CONNECTION_LIMIT, (unsigned int) 16,
1717 MHD_OPTION_PER_IP_CONNECTION_LIMIT, (unsigned int) 1,
1718 MHD_OPTION_CONNECTION_TIMEOUT, (gn_timeout.value / 1000),
1719 MHD_OPTION_CONNECTION_MEMORY_LIMIT, (size_t) (16 * 1024),
1720 MHD_OPTION_NOTIFY_COMPLETED, &requestCompletedCallback, NULL,
1722 plugin->http_server_daemon_v4 = MHD_start_daemon (MHD_NO_FLAG,
1724 &acceptPolicyCallback,
1725 plugin , &accessHandlerCallback, plugin,
1726 MHD_OPTION_CONNECTION_LIMIT, (unsigned int) 16,
1727 MHD_OPTION_PER_IP_CONNECTION_LIMIT, (unsigned int) 1,
1728 MHD_OPTION_CONNECTION_TIMEOUT, (gn_timeout.value / 1000),
1729 MHD_OPTION_CONNECTION_MEMORY_LIMIT, (size_t) (16 * 1024),
1730 MHD_OPTION_NOTIFY_COMPLETED, &requestCompletedCallback, NULL,
1733 if (plugin->http_server_daemon_v4 != NULL)
1734 plugin->http_server_task_v4 = http_server_daemon_prepare (plugin, plugin->http_server_daemon_v4);
1735 if (plugin->http_server_daemon_v6 != NULL)
1736 plugin->http_server_task_v6 = http_server_daemon_prepare (plugin, plugin->http_server_daemon_v6);
1738 if (plugin->http_server_task_v4 != GNUNET_SCHEDULER_NO_TASK)
1739 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Starting MHD with IPv4 on port %u\n",port);
1740 else if (plugin->http_server_task_v6 != GNUNET_SCHEDULER_NO_TASK)
1741 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Starting MHD with IPv4 and IPv6 on port %u\n",port);
1744 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"No MHD was started, transport plugin not functional!\n");
1745 libgnunet_plugin_transport_http_done (api);
1749 /* Initializing cURL */
1750 curl_global_init(CURL_GLOBAL_ALL);
1751 plugin->multi_handle = curl_multi_init();
1753 if ( NULL == plugin->multi_handle )
1755 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
1757 _("Could not initialize curl multi handle, failed to start http plugin!\n"),
1759 libgnunet_plugin_transport_http_done (api);
1763 plugin->sessions = GNUNET_CONTAINER_multihashmap_create (10);
1764 GNUNET_OS_network_interfaces_list (&process_interfaces, plugin);
1769 /* end of plugin_transport_http.c */