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_template.c
23 * @brief template for a new transport service
24 * @author Christian Grothoff
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 "plugin_transport.h"
37 #include "gnunet_os_lib.h"
38 #include "microhttpd.h"
39 #include <curl/curl.h>
42 #define DEBUG_CURL GNUNET_NO
43 #define DEBUG_HTTP GNUNET_NO
46 * Text of the response sent back after the last bytes of a PUT
47 * request have been received (just to formally obey the HTTP
50 #define HTTP_PUT_RESPONSE "Thank you!"
53 * After how long do we expire an address that we
54 * learned from another peer if it is not reconfirmed
57 #define LEARNED_ADDRESS_EXPIRATION GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_HOURS, 6)
60 * Page returned if request invalid
62 #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>"
65 * Timeout for a http connect
67 #define HTTP_CONNECT_TIMEOUT 30
70 * Timeout for a http connect
72 #define HTTP_MESSAGE_INITIAL_BUFFERSIZE GNUNET_SERVER_MAX_MESSAGE_SIZE
76 * Encapsulation of all of the state of the plugin.
81 * Network format for IPv4 addresses.
83 struct IPv4HttpAddress
86 * IPv4 address, in network byte order.
91 * Port number, in network byte order.
99 * Network format for IPv6 addresses.
101 struct IPv6HttpAddress
106 struct in6_addr ipv6_addr;
109 * Port number, in network byte order.
118 * Message to send using http
123 * Next field for linked list
125 struct HTTP_Message * next;
128 * buffer containing data to send
133 * amount of data already sent
138 * amount of data to sent
145 * Continuation function to call once the transmission buffer
146 * has again space available. NULL if there is no
147 * continuation to call.
149 GNUNET_TRANSPORT_TransmitContinuation transmit_cont;
152 * Closure for transmit_cont.
154 void *transmit_cont_cls;
159 * Session handle for connections.
165 * Stored in a linked list.
167 struct Session *next;
170 * Pointer to the global plugin struct.
172 struct Plugin *plugin;
175 * To whom are we talking to (set to our identity
176 * if we are still waiting for the welcome message)
178 struct GNUNET_PeerIdentity sender;
181 * Sender's ip address to distinguish between incoming connections
183 struct sockaddr_in * addr_inbound;
186 * Sender's ip address recieved by transport
188 struct sockaddr_in * addr_outbound;
191 * Did we initiate the connection (GNUNET_YES) or the other peer (GNUNET_NO)?
193 unsigned int is_client;
196 * Is the connection active (GNUNET_YES) or terminated (GNUNET_NO)?
198 unsigned int is_active;
201 * At what time did we reset last_received last?
203 struct GNUNET_TIME_Absolute last_quota_update;
206 * How many bytes have we received since the "last_quota_update"
209 uint64_t last_received;
212 * Number of bytes per ms that this peer is allowed
218 * Is there a HTTP/PUT in progress?
220 unsigned int is_put_in_progress;
223 * Is there a HTTP/PUT in progress?
225 unsigned int is_bad_request;
230 struct GNUNET_CRYPTO_HashAsciiEncoded hash;
232 struct HTTP_Message * pending_outbound_msg;;
234 struct HTTP_Message * pending_inbound_msg;
240 * Encapsulation of all of the state of the plugin.
247 struct GNUNET_TRANSPORT_PluginEnvironment *env;
250 * Handle to the network service.
252 struct GNUNET_SERVICE_Context *service;
254 unsigned int port_inbound;
257 * List of open sessions.
259 struct Session *sessions;
262 * Number of active sessions
265 unsigned int session_count;
269 * Daemon for listening for new IPv4 connections.
271 static struct MHD_Daemon *http_daemon_v4;
274 * Daemon for listening for new IPv6connections.
276 static struct MHD_Daemon *http_daemon_v6;
279 * Our primary task for http daemon handling IPv4 connections
281 static GNUNET_SCHEDULER_TaskIdentifier http_task_v4;
284 * Our primary task for http daemon handling IPv6 connections
286 static GNUNET_SCHEDULER_TaskIdentifier http_task_v6;
290 * The task sending data
292 static GNUNET_SCHEDULER_TaskIdentifier http_task_send;
296 * Information about this plugin
298 static struct Plugin *plugin;
303 static CURLM *multi_handle;
308 static char * hostname;
311 * Our ASCII encoded, hashed peer identity
312 * This string is used to distinguish between connections and is added to the urls
314 static struct GNUNET_CRYPTO_HashAsciiEncoded my_ascii_hash_ident;
316 struct GNUNET_TIME_Relative timeout;
319 * Finds a http session in our linked list using peer identity as a key
320 * @param peer peeridentity
321 * @return http session corresponding to peer identity
323 static struct Session * find_session_by_pi( const struct GNUNET_PeerIdentity *peer )
325 struct Session * cur;
326 GNUNET_HashCode hc_peer;
327 GNUNET_HashCode hc_current;
329 cur = plugin->sessions;
330 hc_peer = peer->hashPubKey;
333 hc_current = cur->sender.hashPubKey;
334 if ( 0 == GNUNET_CRYPTO_hash_cmp( &hc_peer, &hc_current))
336 cur = plugin->sessions->next;
342 * Finds a http session in our linked list using libcurl handle as a key
343 * Needed when sending data with libcurl to differentiate between sessions
344 * @param handle peeridentity
345 * @return http session corresponding to peer identity
347 static struct Session * find_session_by_curlhandle( CURL* handle )
349 struct Session * cur;
351 cur = plugin->sessions;
354 if ( handle == cur->curl_handle )
356 cur = plugin->sessions->next;
362 * Create a new session
364 * @param address address the peer is using inbound
365 * @param address address the peer is using outbound
366 * @param peer identity
367 * @return created session object
369 static struct Session * create_session (struct sockaddr_in *addr_in, struct sockaddr_in *addr_out, const struct GNUNET_PeerIdentity *peer)
371 struct Session * ses = GNUNET_malloc ( sizeof( struct Session) );
373 ses->addr_inbound = GNUNET_malloc ( sizeof (struct sockaddr_in) );
374 ses->addr_outbound = GNUNET_malloc ( sizeof (struct sockaddr_in) );
376 ses->plugin = plugin;
377 if ((NULL != addr_in) && (( AF_INET == addr_in->sin_family) || ( AF_INET6 == addr_in->sin_family)))
379 memcpy(ses->addr_inbound, addr_in, sizeof (struct sockaddr_in));
381 if ((NULL != addr_out) && (( AF_INET == addr_out->sin_family) || ( AF_INET6 == addr_out->sin_family)))
383 memcpy(ses->addr_outbound, addr_out, sizeof (struct sockaddr_in));
385 memcpy(&ses->sender, peer, sizeof (struct GNUNET_PeerIdentity));
386 GNUNET_CRYPTO_hash_to_enc(&ses->sender.hashPubKey,&(ses->hash));
387 ses->is_active = GNUNET_NO;
388 ses->pending_inbound_msg = GNUNET_malloc( sizeof (struct HTTP_Message));
389 ses->pending_inbound_msg->buf = GNUNET_malloc(GNUNET_SERVER_MAX_MESSAGE_SIZE);
390 ses->pending_inbound_msg->len = GNUNET_SERVER_MAX_MESSAGE_SIZE;
391 ses->pending_inbound_msg->pos = 0;
398 * Callback called by MHD when a connection is terminated
400 static void requestCompletedCallback (void *cls, struct MHD_Connection * connection, void **httpSessionCache)
404 cs = *httpSessionCache;
407 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection from peer `%s' was terminated\n",GNUNET_i2s(&cs->sender));
408 /* session set to inactive */
409 cs->is_active = GNUNET_NO;
410 cs->is_put_in_progress = GNUNET_NO;
416 * Check if we are allowed to connect to the given IP.
419 acceptPolicyCallback (void *cls,
420 const struct sockaddr *addr, socklen_t addr_len)
422 /* Every connection is accepted, nothing more to do here */
430 * Process GET or PUT request received via MHD. For
431 * GET, queue response that will send back our pending
432 * messages. For PUT, process incoming data and send
433 * to GNUnet core. In either case, check if a session
434 * already exists and create a new one if not.
437 accessHandlerCallback (void *cls,
438 struct MHD_Connection *session,
442 const char *upload_data,
443 size_t * upload_data_size, void **httpSessionCache)
445 struct MHD_Response *response;
447 struct Session * cs_temp;
448 const union MHD_ConnectionInfo * conn_info;
449 struct sockaddr_in *addrin;
450 struct sockaddr_in6 *addrin6;
451 char address[INET6_ADDRSTRLEN+14];
452 struct GNUNET_PeerIdentity pi_in;
454 struct GNUNET_MessageHeader *gn_msg;
455 int send_error_to_client;
458 send_error_to_client = GNUNET_NO;
460 if ( NULL == *httpSessionCache)
462 /* check url for peer identity */
463 res = GNUNET_CRYPTO_hash_from_string ( &url[1], &(pi_in.hashPubKey));
464 if ( GNUNET_SYSERR == res )
466 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Peer has no valid ident\n");
467 response = MHD_create_response_from_data (strlen (HTTP_ERROR_RESPONSE),HTTP_ERROR_RESPONSE, MHD_NO, MHD_NO);
468 res = MHD_queue_response (session, MHD_HTTP_NOT_FOUND, response);
469 MHD_destroy_response (response);
473 conn_info = MHD_get_connection_info(session, MHD_CONNECTION_INFO_CLIENT_ADDRESS );
474 /* Incoming IPv4 connection */
475 if ( AF_INET == conn_info->client_addr->sin_family)
477 addrin = conn_info->client_addr;
478 inet_ntop(addrin->sin_family, &(addrin->sin_addr),address,INET_ADDRSTRLEN);
480 /* Incoming IPv6 connection */
481 if ( AF_INET6 == conn_info->client_addr->sin_family)
483 addrin6 = (struct sockaddr_in6 *) conn_info->client_addr;
484 inet_ntop(addrin6->sin6_family, &(addrin6->sin6_addr),address,INET6_ADDRSTRLEN);
486 /* find existing session for address */
488 if (plugin->session_count > 0)
490 cs = plugin->sessions;
494 /* Comparison based on ip address */
495 // res = (0 == memcmp(&(conn_info->client_addr->sin_addr),&(cs->addr->sin_addr), sizeof (struct in_addr))) ? GNUNET_YES : GNUNET_NO;
497 /* Comparison based on ip address, port number and address family */
498 // res = (0 == memcmp((conn_info->client_addr),(cs->addr), sizeof (struct sockaddr_in))) ? GNUNET_YES : GNUNET_NO;
500 /* Comparison based on PeerIdentity */
501 res = (0 == memcmp(&pi_in,&(cs->sender), sizeof (struct GNUNET_PeerIdentity))) ? GNUNET_YES : GNUNET_NO;
503 if ( GNUNET_YES == res)
505 /* existing session for this address found */
506 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Session for peer `%s' found\n",GNUNET_i2s(&cs->sender));
512 /* no existing session, create a new one*/
515 /* create new session object */
516 cs = create_session(conn_info->client_addr, NULL, &pi_in);
518 /* Insert session into linked list */
519 if ( plugin->sessions == NULL)
521 plugin->sessions = cs;
522 plugin->session_count = 1;
524 cs_temp = plugin->sessions;
525 while ( cs_temp->next != NULL )
527 cs_temp = cs_temp->next;
532 plugin->session_count++;
534 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"New Session `%s' inserted, count %u \n", GNUNET_i2s(&cs->sender), plugin->session_count);
538 if (*httpSessionCache == NULL)
540 *httpSessionCache = cs;
541 /* Updating session */
542 memcpy(cs->addr_inbound,conn_info->client_addr, sizeof(struct sockaddr_in));
544 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"HTTP Daemon has new an incoming `%s' request from peer `%s' (`[%s]:%u')\n",method, GNUNET_i2s(&cs->sender),address,ntohs(cs->addr_inbound->sin_port));
548 cs = *httpSessionCache;
550 /* Is it a PUT or a GET request */
551 if ( 0 == strcmp (MHD_HTTP_METHOD_PUT, method) )
554 if ((*upload_data_size == 0) && (cs->is_put_in_progress == GNUNET_NO))
556 if (cs->pending_inbound_msg->pos !=0 )
558 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
559 _("Incoming message from peer `%s', while existing message with %u bytes was not forwarded to transport'\n"),
560 GNUNET_i2s(&cs->sender), cs->pending_inbound_msg->pos);
561 cs->pending_inbound_msg->pos = 0;
564 cs->is_put_in_progress = GNUNET_YES;
565 cs->is_bad_request = GNUNET_NO;
566 cs->is_active = GNUNET_YES;
570 if ((*upload_data_size > 0) && (cs->is_bad_request != GNUNET_YES))
572 if ((*upload_data_size + cs->pending_inbound_msg->pos < cs->pending_inbound_msg->len) && (*upload_data_size + cs->pending_inbound_msg->pos <= GNUNET_SERVER_MAX_MESSAGE_SIZE))
574 /* copy uploaded data to buffer */
575 memcpy(&cs->pending_inbound_msg->buf[cs->pending_inbound_msg->pos],upload_data,*upload_data_size);
576 cs->pending_inbound_msg->pos += *upload_data_size;
577 *upload_data_size = 0;
582 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"%u bytes not added to message of %u bytes, message to big\n",*upload_data_size, cs->pending_inbound_msg->pos);
583 cs->is_bad_request = GNUNET_YES;
584 /* (*upload_data_size) bytes not processed */
589 if ((cs->is_put_in_progress == GNUNET_YES) && (cs->is_bad_request == GNUNET_YES))
591 *upload_data_size = 0;
592 response = MHD_create_response_from_data (strlen (HTTP_PUT_RESPONSE),HTTP_PUT_RESPONSE, MHD_NO, MHD_NO);
593 res = MHD_queue_response (session, MHD_HTTP_REQUEST_ENTITY_TOO_LARGE, response);
596 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Sent HTTP/1.1: 413 ENTITY TOO LARGE as PUT Response\n");
597 cs->is_bad_request = GNUNET_NO;
598 cs->is_put_in_progress =GNUNET_NO;
600 MHD_destroy_response (response);
604 if ((*upload_data_size == 0) && (cs->is_put_in_progress == GNUNET_YES) && (cs->is_bad_request == GNUNET_NO))
606 send_error_to_client = GNUNET_YES;
607 struct GNUNET_MessageHeader * gn_msg = NULL;
608 /*check message and forward here */
610 if (cs->pending_inbound_msg->pos >= sizeof (struct GNUNET_MessageHeader))
612 gn_msg = GNUNET_malloc (cs->pending_inbound_msg->pos);
613 memcpy (gn_msg,cs->pending_inbound_msg->buf,cs->pending_inbound_msg->pos);
615 if ((ntohs(gn_msg->size) == cs->pending_inbound_msg->pos))
617 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Recieved GNUnet message type %u size %u and payload %u \n",ntohs (gn_msg->type), ntohs (gn_msg->size), ntohs (gn_msg->size)-sizeof(struct GNUNET_MessageHeader));
618 /* forwarding message to transport */
621 if ( AF_INET == cs->addr_inbound->sin_family)
623 tmp = GNUNET_malloc (INET_ADDRSTRLEN + 14);
624 inet_ntop(AF_INET, &(cs->addr_inbound)->sin_addr,address,INET_ADDRSTRLEN);
625 GNUNET_asprintf(&tmp,"%s:%u",address,ntohs(cs->addr_inbound->sin_port));
627 /* Incoming IPv6 connection */
628 if ( AF_INET6 == cs->addr_inbound->sin_family)
630 tmp = GNUNET_malloc (INET6_ADDRSTRLEN + 14);
631 inet_ntop(AF_INET6, &((struct sockaddr_in6 *) cs->addr_inbound)->sin6_addr,address,INET6_ADDRSTRLEN);
632 GNUNET_asprintf(&tmp,"[%s]:%u",address,ntohs(cs->addr_inbound->sin_port));
634 plugin->env->receive(plugin->env, &(cs->sender), gn_msg, 1, cs , tmp, strlen(tmp));
636 send_error_to_client = GNUNET_NO;
640 if (send_error_to_client == GNUNET_NO)
642 response = MHD_create_response_from_data (strlen (HTTP_PUT_RESPONSE),HTTP_PUT_RESPONSE, MHD_NO, MHD_NO);
643 res = MHD_queue_response (session, MHD_HTTP_OK, response);
644 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Sent HTTP/1.1: 200 OK as PUT Response\n",HTTP_PUT_RESPONSE, strlen (HTTP_PUT_RESPONSE), res );
645 MHD_destroy_response (response);
649 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Recieved malformed message with %u bytes\n", cs->pending_inbound_msg->pos);
650 response = MHD_create_response_from_data (strlen (HTTP_PUT_RESPONSE),HTTP_PUT_RESPONSE, MHD_NO, MHD_NO);
651 res = MHD_queue_response (session, MHD_HTTP_BAD_REQUEST, response);
652 MHD_destroy_response (response);
653 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Sent HTTP/1.1: 400 BAD REQUEST as PUT Response\n");
656 GNUNET_free_non_null (gn_msg);
657 cs->is_put_in_progress = GNUNET_NO;
658 cs->is_bad_request = GNUNET_NO;
659 cs->pending_inbound_msg->pos = 0;
663 if ( 0 == strcmp (MHD_HTTP_METHOD_GET, method) )
665 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Got GET Request\n");
666 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"URL: `%s'\n",url);
667 response = MHD_create_response_from_data (strlen (HTTP_PUT_RESPONSE),HTTP_PUT_RESPONSE, MHD_NO, MHD_NO);
668 res = MHD_queue_response (session, MHD_HTTP_OK, response);
669 MHD_destroy_response (response);
677 * Call MHD to process pending requests and then go back
678 * and schedule the next run.
680 static void http_daemon_run (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
683 * Function that queries MHD's select sets and
684 * starts the task waiting for them.
686 static GNUNET_SCHEDULER_TaskIdentifier
687 http_daemon_prepare (struct MHD_Daemon *daemon_handle)
689 GNUNET_SCHEDULER_TaskIdentifier ret;
693 struct GNUNET_NETWORK_FDSet *wrs;
694 struct GNUNET_NETWORK_FDSet *wws;
695 struct GNUNET_NETWORK_FDSet *wes;
697 unsigned long long timeout;
699 struct GNUNET_TIME_Relative tv;
704 wrs = GNUNET_NETWORK_fdset_create ();
705 wes = GNUNET_NETWORK_fdset_create ();
706 wws = GNUNET_NETWORK_fdset_create ();
708 GNUNET_assert (MHD_YES ==
709 MHD_get_fdset (daemon_handle,
714 haveto = MHD_get_timeout (daemon_handle, &timeout);
715 if (haveto == MHD_YES)
716 tv.value = (uint64_t) timeout;
718 tv = GNUNET_TIME_UNIT_FOREVER_REL;
719 GNUNET_NETWORK_fdset_copy_native (wrs, &rs, max);
720 GNUNET_NETWORK_fdset_copy_native (wws, &ws, max);
721 GNUNET_NETWORK_fdset_copy_native (wes, &es, max);
722 ret = GNUNET_SCHEDULER_add_select (plugin->env->sched,
723 GNUNET_SCHEDULER_PRIORITY_HIGH,
724 GNUNET_SCHEDULER_NO_TASK,
730 GNUNET_NETWORK_fdset_destroy (wrs);
731 GNUNET_NETWORK_fdset_destroy (wws);
732 GNUNET_NETWORK_fdset_destroy (wes);
737 * Call MHD to process pending requests and then go back
738 * and schedule the next run.
740 static void http_daemon_run (void *cls,
741 const struct GNUNET_SCHEDULER_TaskContext *tc)
743 struct MHD_Daemon *daemon_handle = cls;
745 if (daemon_handle == http_daemon_v4)
746 http_task_v4 = GNUNET_SCHEDULER_NO_TASK;
748 if (daemon_handle == http_daemon_v6)
749 http_task_v6 = GNUNET_SCHEDULER_NO_TASK;
751 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
754 GNUNET_assert (MHD_YES == MHD_run (daemon_handle));
755 if (daemon_handle == http_daemon_v4)
756 http_task_v4 = http_daemon_prepare (daemon_handle);
757 if (daemon_handle == http_daemon_v6)
758 http_task_v6 = http_daemon_prepare (daemon_handle);
763 * Removes a message from the linked list of messages
764 * @param ses session to remove message from
765 * @param msg message to remove
766 * @return GNUNET_SYSERR if msg not found, GNUNET_OK on success
769 static int remove_http_message(struct Session * ses, struct HTTP_Message * msg)
771 struct HTTP_Message * cur;
772 struct HTTP_Message * next;
774 cur = ses->pending_outbound_msg;
778 return GNUNET_SYSERR;
782 ses->pending_outbound_msg = cur->next;
783 GNUNET_free (cur->buf);
784 GNUNET_free (cur->dest_url);
790 while (cur->next!=msg)
792 if (cur->next != NULL)
795 return GNUNET_SYSERR;
798 cur->next = cur->next->next;
799 GNUNET_free (cur->next->buf);
800 GNUNET_free (cur->next->dest_url);
801 GNUNET_free (cur->next);
807 static size_t header_function( void *ptr, size_t size, size_t nmemb, void *stream)
810 unsigned int len = size * nmemb;
812 tmp = GNUNET_malloc ( len+1 );
814 if (tmp[len-2] == 13)
817 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Header: `%s'\n",tmp);
820 if (0==strcmp (tmp,"HTTP/1.1 100 Continue"))
822 res->http_result_code=100;
824 if (0==strcmp (tmp,"HTTP/1.1 200 OK"))
826 res->http_result_code=200;
828 if (0==strcmp (tmp,"HTTP/1.1 400 Bad Request"))
830 res->http_result_code=400;
832 if (0==strcmp (tmp,"HTTP/1.1 404 Not Found"))
834 res->http_result_code=404;
836 if (0==strcmp (tmp,"HTTP/1.1 413 Request entity too large"))
838 res->http_result_code=413;
846 * Callback method used with libcurl
847 * Method is called when libcurl needs to read data during sending
848 * @param stream pointer where to write data
849 * @param size size of an individual element
850 * @param nmemb count of elements that can be written to the buffer
851 * @param ptr source pointer, passed to the libcurl handle
852 * @return bytes written to stream
854 static size_t send_read_callback(void *stream, size_t size, size_t nmemb, void *ptr)
856 struct Session * ses = ptr;
857 struct HTTP_Message * msg = ses->pending_outbound_msg;
858 unsigned int bytes_sent;
861 if (msg->len > (size * nmemb))
862 return CURL_READFUNC_ABORT;
864 if (( msg->pos < msg->len) && (msg->len < (size * nmemb)))
866 memcpy(stream, msg->buf, msg->len);
868 bytes_sent = msg->len;
875 * Callback method used with libcurl
876 * Method is called when libcurl needs to write data during sending
877 * @param stream pointer where to write data
878 * @param size size of an individual element
879 * @param nmemb count of elements that can be written to the buffer
880 * @param ptr destination pointer, passed to the libcurl handle
881 * @return bytes read from stream
883 static size_t send_write_callback( void *stream, size_t size, size_t nmemb, void *ptr)
885 char * data = malloc(size*nmemb +1);
887 memcpy( data, stream, size*nmemb);
888 data[size*nmemb] = '\0';
889 /* Just a dummy print for the response recieved for the PUT message */
890 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Recieved %u bytes: `%s' \n", size * nmemb, data);
892 return (size * nmemb);
897 * Function setting up file descriptors and scheduling task to run
898 * @param session session to send data to
899 * @return bytes sent to peer
901 static size_t send_prepare(struct Session* session );
904 * Function setting up curl handle and selecting message to send
905 * @param ses session to send data to
906 * @return bytes sent to peer
908 static ssize_t send_select_init (struct Session* ses )
912 struct HTTP_Message * msg;
914 if ( NULL == ses->curl_handle)
915 ses->curl_handle = curl_easy_init();
916 if( NULL == ses->curl_handle)
918 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Getting cURL handle failed\n");
921 msg = ses->pending_outbound_msg;
926 curl_easy_setopt(ses->curl_handle, CURLOPT_VERBOSE, 1L);
928 curl_easy_setopt(ses->curl_handle, CURLOPT_URL, msg->dest_url);
929 curl_easy_setopt(ses->curl_handle, CURLOPT_PUT, 1L);
930 curl_easy_setopt(ses->curl_handle, CURLOPT_HEADERFUNCTION, &header_function);
931 curl_easy_setopt(ses->curl_handle, CURLOPT_READFUNCTION, send_read_callback);
932 curl_easy_setopt(ses->curl_handle, CURLOPT_READDATA, ses);
933 curl_easy_setopt(ses->curl_handle, CURLOPT_WRITEFUNCTION, send_write_callback);
934 curl_easy_setopt(ses->curl_handle, CURLOPT_READDATA, ses);
935 curl_easy_setopt(ses->curl_handle, CURLOPT_INFILESIZE_LARGE, (curl_off_t) msg->len);
936 curl_easy_setopt(ses->curl_handle, CURLOPT_TIMEOUT, (long) (timeout.value / 1000 ));
937 curl_easy_setopt(ses->curl_handle, CURLOPT_CONNECTTIMEOUT, HTTP_CONNECT_TIMEOUT);
939 mret = curl_multi_add_handle(multi_handle, ses->curl_handle);
940 if (mret != CURLM_OK)
942 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
943 _("%s failed at %s:%d: `%s'\n"),
944 "curl_multi_add_handle", __FILE__, __LINE__,
945 curl_multi_strerror (mret));
948 bytes_sent = send_prepare (ses );
952 static void send_execute (void *cls,
953 const struct GNUNET_SCHEDULER_TaskContext *tc)
959 struct Session * cs = NULL;
961 http_task_send = GNUNET_SCHEDULER_NO_TASK;
962 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
968 mret = curl_multi_perform (multi_handle, &running);
974 msg = curl_multi_info_read (multi_handle, &running);
975 GNUNET_break (msg != NULL);
978 /* get session for affected curl handle */
979 GNUNET_assert ( msg->easy_handle != NULL );
980 cs = find_session_by_curlhandle (msg->easy_handle);
981 GNUNET_assert ( cs != NULL );
982 GNUNET_assert ( cs->pending_outbound_msg != NULL );
987 if ( (msg->data.result != CURLE_OK) &&
988 (msg->data.result != CURLE_GOT_NOTHING) )
991 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
992 _("%s failed for `%s' at %s:%d: `%s'\n"),
993 "curl_multi_perform",
994 GNUNET_i2s(&cs->sender),
997 curl_easy_strerror (msg->data.result));
998 /* sending msg failed*/
999 if (( NULL != cs->pending_outbound_msg) && ( NULL != cs->pending_outbound_msg->transmit_cont))
1000 cs->pending_outbound_msg->transmit_cont (cs->pending_outbound_msg->transmit_cont_cls,&cs->sender,GNUNET_SYSERR);
1004 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1005 "Send to peer `%s' completed.\n", GNUNET_i2s(&cs->sender));
1007 curl_easy_cleanup(cs->curl_handle);
1008 cs->curl_handle=NULL;
1010 /* Calling transmit continuation */
1011 if (( NULL != cs->pending_outbound_msg) && (NULL != cs->pending_outbound_msg->transmit_cont))
1012 cs->pending_outbound_msg->transmit_cont (cs->pending_outbound_msg->transmit_cont_cls,&cs->sender,GNUNET_OK);
1014 if (GNUNET_OK != remove_http_message(cs, cs->pending_outbound_msg))
1015 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Message could not be removed from session `%s'", GNUNET_i2s(&cs->sender));
1017 /* send pending messages */
1018 if (cs->pending_outbound_msg != NULL)
1020 send_select_init (cs);
1029 while ( (running > 0) );
1032 while (mret == CURLM_CALL_MULTI_PERFORM);
1038 * Function setting up file descriptors and scheduling task to run
1039 * @param ses session to send data to
1040 * @return bytes sent to peer
1042 static size_t send_prepare(struct Session* session )
1048 struct GNUNET_NETWORK_FDSet *grs;
1049 struct GNUNET_NETWORK_FDSet *gws;
1057 mret = curl_multi_fdset (multi_handle, &rs, &ws, &es, &max);
1058 if (mret != CURLM_OK)
1060 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1061 _("%s failed at %s:%d: `%s'\n"),
1062 "curl_multi_fdset", __FILE__, __LINE__,
1063 curl_multi_strerror (mret));
1066 mret = curl_multi_timeout (multi_handle, &to);
1067 if (mret != CURLM_OK)
1069 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1070 _("%s failed at %s:%d: `%s'\n"),
1071 "curl_multi_timeout", __FILE__, __LINE__,
1072 curl_multi_strerror (mret));
1076 grs = GNUNET_NETWORK_fdset_create ();
1077 gws = GNUNET_NETWORK_fdset_create ();
1078 GNUNET_NETWORK_fdset_copy_native (grs, &rs, max + 1);
1079 GNUNET_NETWORK_fdset_copy_native (gws, &ws, max + 1);
1080 http_task_send = GNUNET_SCHEDULER_add_select (plugin->env->sched,
1081 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1082 GNUNET_SCHEDULER_NO_TASK,
1083 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 0),
1088 GNUNET_NETWORK_fdset_destroy (gws);
1089 GNUNET_NETWORK_fdset_destroy (grs);
1091 /* FIXME: return bytes REALLY sent */
1096 * Function that can be used by the transport service to transmit
1097 * a message using the plugin.
1099 * @param cls closure
1100 * @param target who should receive this message
1101 * @param priority how important is the message
1102 * @param msgbuf the message to transmit
1103 * @param msgbuf_size number of bytes in 'msgbuf'
1104 * @param to when should we time out
1105 * @param session which session must be used (or NULL for "any")
1106 * @param addr the address to use (can be NULL if the plugin
1107 * is "on its own" (i.e. re-use existing TCP connection))
1108 * @param addrlen length of the address in bytes
1109 * @param force_address GNUNET_YES if the plugin MUST use the given address,
1110 * otherwise the plugin may use other addresses or
1111 * existing connections (if available)
1112 * @param cont continuation to call once the message has
1113 * been transmitted (or if the transport is ready
1114 * for the next transmission call; or if the
1115 * peer disconnected...)
1116 * @param cont_cls closure for cont
1117 * @return number of bytes used (on the physical network, with overheads);
1118 * -1 on hard errors (i.e. address invalid); 0 is a legal value
1119 * and does NOT mean that the message was not transmitted (DV)
1122 http_plugin_send (void *cls,
1123 const struct GNUNET_PeerIdentity *target,
1126 unsigned int priority,
1127 struct GNUNET_TIME_Relative to,
1128 struct Session *session,
1132 GNUNET_TRANSPORT_TransmitContinuation cont,
1136 struct Session* ses;
1137 struct Session* ses_temp;
1138 struct HTTP_Message * msg;
1139 struct HTTP_Message * tmp;
1144 /* find session for peer */
1145 ses = find_session_by_pi (target);
1147 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Existing session for peer `%s' found\n", GNUNET_i2s(target));
1150 /* create new session object */
1152 ses = create_session(NULL, (struct sockaddr_in *) addr, target);
1153 ses->is_active = GNUNET_YES;
1155 /* Insert session into linked list */
1156 if ( plugin->sessions == NULL)
1158 plugin->sessions = ses;
1159 plugin->session_count = 1;
1161 ses_temp = plugin->sessions;
1162 while ( ses_temp->next != NULL )
1164 ses_temp = ses_temp->next;
1166 if (ses_temp != ses )
1168 ses_temp->next = ses;
1169 plugin->session_count++;
1171 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"New Session `%s' inserted, count %u \n", GNUNET_i2s(target), plugin->session_count);
1174 GNUNET_assert (addr!=NULL);
1177 /* setting url to send to */
1178 if (force_address == GNUNET_YES)
1180 if (addrlen == (sizeof (struct IPv4HttpAddress)))
1182 address = GNUNET_malloc(INET_ADDRSTRLEN + 14 + strlen ((const char *) (&ses->hash)));
1183 inet_ntop(AF_INET,&((struct IPv4HttpAddress *) addr)->ipv4_addr,address,INET_ADDRSTRLEN);
1184 port = ntohs(((struct IPv4HttpAddress *) addr)->u_port);
1185 GNUNET_asprintf(&address,"http://%s:%u/%s",address,port, (char *) (&ses->hash));
1187 else if (addrlen == (sizeof (struct IPv6HttpAddress)))
1189 address = GNUNET_malloc(INET6_ADDRSTRLEN + 14 + strlen ((const char *) (&ses->hash)));
1190 inet_ntop(AF_INET6, &((struct IPv6HttpAddress *) addr)->ipv6_addr,address,INET6_ADDRSTRLEN);
1191 port = ntohs(((struct IPv6HttpAddress *) addr)->u6_port);
1192 GNUNET_asprintf(&address,"http://%s:%u/%s",address,port,(char *) (&ses->hash));
1201 GNUNET_assert (address != NULL);
1204 /* setting up message */
1205 msg = GNUNET_malloc (sizeof (struct HTTP_Message));
1207 msg->len = msgbuf_size;
1209 msg->buf = GNUNET_malloc (msgbuf_size);
1210 msg->dest_url = address;
1211 msg->transmit_cont = cont;
1212 msg->transmit_cont_cls = cont_cls;
1213 memcpy (msg->buf,msgbuf, msgbuf_size);
1215 /* insert created message in list of pending messages */
1216 if (ses->pending_outbound_msg == NULL)
1218 ses->pending_outbound_msg = msg;
1220 tmp = ses->pending_outbound_msg;
1221 while ( NULL != tmp->next)
1230 if (msg == ses->pending_outbound_msg)
1232 bytes_sent = send_select_init (ses);
1241 * Function that can be used to force the plugin to disconnect
1242 * from the given peer and cancel all previous transmissions
1243 * (and their continuationc).
1245 * @param cls closure
1246 * @param target peer from which to disconnect
1249 http_plugin_disconnect (void *cls,
1250 const struct GNUNET_PeerIdentity *target)
1252 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"HTTP Plugin: http_plugin_disconnect\n");
1253 // struct Plugin *plugin = cls;
1259 * Convert the transports address to a nice, human-readable
1262 * @param cls closure
1263 * @param type name of the transport that generated the address
1264 * @param addr one of the addresses of the host, NULL for the last address
1265 * the specific address format depends on the transport
1266 * @param addrlen length of the address
1267 * @param numeric should (IP) addresses be displayed in numeric form?
1268 * @param timeout after how long should we give up?
1269 * @param asc function to call on each string
1270 * @param asc_cls closure for asc
1273 http_plugin_address_pretty_printer (void *cls,
1278 struct GNUNET_TIME_Relative timeout,
1279 GNUNET_TRANSPORT_AddressStringCallback
1282 const struct IPv4HttpAddress *t4;
1283 const struct IPv6HttpAddress *t6;
1284 struct sockaddr_in a4;
1285 struct sockaddr_in6 a6;
1290 if (addrlen == sizeof (struct IPv6HttpAddress))
1292 address = GNUNET_malloc (INET6_ADDRSTRLEN);
1294 a6.sin6_addr = t6->ipv6_addr;
1295 inet_ntop(AF_INET6, &(a6.sin6_addr),address,INET6_ADDRSTRLEN);
1296 port = ntohs(t6->u6_port);
1298 else if (addrlen == sizeof (struct IPv4HttpAddress))
1300 address = GNUNET_malloc (INET_ADDRSTRLEN);
1302 a4.sin_addr.s_addr = t4->ipv4_addr;
1303 inet_ntop(AF_INET, &(a4.sin_addr),address,INET_ADDRSTRLEN);
1304 port = ntohs(t4->u_port);
1308 /* invalid address */
1309 GNUNET_break_op (0);
1310 asc (asc_cls, NULL);
1314 ret = GNUNET_malloc(strlen(address) +14);
1315 GNUNET_asprintf(&ret,"http://%s:%u/",address,port);
1316 GNUNET_free (address);
1323 * Another peer has suggested an address for this
1324 * peer and transport plugin. Check that this could be a valid
1325 * address. If so, consider adding it to the list
1328 * @param cls closure
1329 * @param addr pointer to the address
1330 * @param addrlen length of addr
1331 * @return GNUNET_OK if this is a plausible address for this peer
1335 http_plugin_address_suggested (void *cls,
1336 void *addr, size_t addrlen)
1338 struct IPv4HttpAddress *v4;
1339 struct IPv6HttpAddress *v6;
1342 if ((addrlen != sizeof (struct IPv4HttpAddress)) &&
1343 (addrlen != sizeof (struct IPv6HttpAddress)))
1345 return GNUNET_SYSERR;
1347 if (addrlen == sizeof (struct IPv4HttpAddress))
1349 v4 = (struct IPv4HttpAddress *) addr;
1350 if (INADDR_LOOPBACK == ntohl(v4->ipv4_addr))
1352 return GNUNET_SYSERR;
1354 port = ntohs (v4->u_port);
1355 if (port != plugin->port_inbound)
1357 return GNUNET_SYSERR;
1362 v6 = (struct IPv6HttpAddress *) addr;
1363 if (IN6_IS_ADDR_LINKLOCAL (&v6->ipv6_addr))
1365 return GNUNET_SYSERR;
1367 port = ntohs (v6->u6_port);
1368 if (port != plugin->port_inbound)
1370 return GNUNET_SYSERR;
1380 * Function called for a quick conversion of the binary address to
1381 * a numeric address. Note that the caller must not free the
1382 * address and that the next call to this function is allowed
1383 * to override the address again.
1385 * @param cls closure
1386 * @param addr binary address
1387 * @param addrlen length of the address
1388 * @return string representing the same address
1391 http_plugin_address_to_string (void *cls,
1395 const struct IPv4HttpAddress *t4;
1396 const struct IPv6HttpAddress *t6;
1397 struct sockaddr_in a4;
1398 struct sockaddr_in6 a6;
1403 if (addrlen == sizeof (struct IPv6HttpAddress))
1405 address = GNUNET_malloc (INET6_ADDRSTRLEN);
1407 a6.sin6_addr = t6->ipv6_addr;
1408 inet_ntop(AF_INET6, &(a6.sin6_addr),address,INET6_ADDRSTRLEN);
1409 port = ntohs(t6->u6_port);
1411 else if (addrlen == sizeof (struct IPv4HttpAddress))
1413 address = GNUNET_malloc (INET_ADDRSTRLEN);
1415 a4.sin_addr.s_addr = t4->ipv4_addr;
1416 inet_ntop(AF_INET, &(a4.sin_addr),address,INET_ADDRSTRLEN);
1417 port = ntohs(t4->u_port);
1421 /* invalid address */
1425 ret = GNUNET_malloc(strlen(address) +6);
1426 GNUNET_asprintf(&ret,"%s:%u",address,port);
1427 GNUNET_free (address);
1432 * Add the IP of our network interface to the list of
1433 * our external IP addresses.
1435 * @param cls the 'struct Plugin*'
1436 * @param name name of the interface
1437 * @param isDefault do we think this may be our default interface
1438 * @param addr address of the interface
1439 * @param addrlen number of bytes in addr
1440 * @return GNUNET_OK to continue iterating
1443 process_interfaces (void *cls,
1446 const struct sockaddr *addr, socklen_t addrlen)
1448 struct IPv4HttpAddress t4;
1449 struct IPv6HttpAddress t6;
1456 af = addr->sa_family;
1459 if (INADDR_LOOPBACK == ntohl(((struct sockaddr_in *) addr)->sin_addr.s_addr))
1461 /* skip loopback addresses */
1464 t4.ipv4_addr = ((struct sockaddr_in *) addr)->sin_addr.s_addr;
1465 t4.u_port = htons (plugin->port_inbound);
1469 else if (af == AF_INET6)
1471 if (IN6_IS_ADDR_LINKLOCAL (&((struct sockaddr_in6 *) addr)->sin6_addr))
1473 /* skip link local addresses */
1476 if (IN6_IS_ADDR_LOOPBACK (&((struct sockaddr_in6 *) addr)->sin6_addr))
1478 /* skip loopback addresses */
1481 memcpy (&t6.ipv6_addr,
1482 &((struct sockaddr_in6 *) addr)->sin6_addr,
1483 sizeof (struct in6_addr));
1484 t6.u6_port = htons (plugin->port_inbound);
1493 plugin->env->notify_address(plugin->env->cls,"http",arg, args, GNUNET_TIME_UNIT_FOREVER_REL);
1499 * Exit point from the plugin.
1502 libgnunet_plugin_transport_http_done (void *cls)
1504 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
1505 struct Plugin *plugin = api->cls;
1506 struct Session * cs;
1507 struct Session * cs_next;
1510 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Unloading http plugin...\n");
1512 if ( http_task_v4 != GNUNET_SCHEDULER_NO_TASK)
1514 GNUNET_SCHEDULER_cancel(plugin->env->sched, http_task_v4);
1515 http_task_v4 = GNUNET_SCHEDULER_NO_TASK;
1518 if ( http_task_v6 != GNUNET_SCHEDULER_NO_TASK)
1520 GNUNET_SCHEDULER_cancel(plugin->env->sched, http_task_v6);
1521 http_task_v6 = GNUNET_SCHEDULER_NO_TASK;
1524 if ( http_task_send != GNUNET_SCHEDULER_NO_TASK)
1526 GNUNET_SCHEDULER_cancel(plugin->env->sched, http_task_send);
1527 http_task_send = GNUNET_SCHEDULER_NO_TASK;
1530 if (http_daemon_v4 != NULL)
1532 MHD_stop_daemon (http_daemon_v4);
1533 http_daemon_v4 = NULL;
1535 if (http_daemon_v6 != NULL)
1537 MHD_stop_daemon (http_daemon_v6);
1538 http_daemon_v6 = NULL;
1541 mret = curl_multi_cleanup(multi_handle);
1542 if ( CURLM_OK != mret)
1543 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"curl multihandle clean up failed");
1545 /* free all sessions */
1546 cs = plugin->sessions;
1550 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Freeing session for peer `%s'\n",GNUNET_i2s(&cs->sender));
1554 /* freeing messages */
1555 struct HTTP_Message *cur;
1556 struct HTTP_Message *tmp;
1557 cur = cs->pending_outbound_msg;
1562 if (NULL != cur->buf)
1563 GNUNET_free (cur->buf);
1567 GNUNET_free (cs->pending_inbound_msg->buf);
1568 GNUNET_free (cs->pending_inbound_msg);
1569 GNUNET_free_non_null (cs->addr_inbound);
1570 GNUNET_free_non_null (cs->addr_outbound);
1573 plugin->session_count--;
1579 /* GNUNET_SERVICE_stop (plugin->service); */
1580 GNUNET_free (hostname);
1581 GNUNET_free (plugin);
1588 * Entry point for the plugin.
1591 libgnunet_plugin_transport_http_init (void *cls)
1593 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
1594 struct GNUNET_TRANSPORT_PluginFunctions *api;
1595 struct GNUNET_SERVICE_Context *service;
1596 unsigned int timeout;
1597 struct GNUNET_TIME_Relative gn_timeout;
1598 long long unsigned int port;
1600 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Starting http plugin...\n");
1604 service = GNUNET_SERVICE_start ("transport-http", env->sched, env->cfg);
1605 if (service == NULL)
1607 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, "", _
1608 ("Failed to start service for `%s' transport plugin.\n"),
1614 plugin = GNUNET_malloc (sizeof (struct Plugin));
1616 plugin->sessions = NULL;
1617 plugin->service = service;
1618 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
1620 api->send = &http_plugin_send;
1621 api->disconnect = &http_plugin_disconnect;
1622 api->address_pretty_printer = &http_plugin_address_pretty_printer;
1623 api->check_address = &http_plugin_address_suggested;
1624 api->address_to_string = &http_plugin_address_to_string;
1626 hostname = GNUNET_RESOLVER_local_fqdn_get ();
1628 /* Hashing our identity to use it in URLs */
1629 GNUNET_CRYPTO_hash_to_enc ( &(plugin->env->my_identity->hashPubKey), &my_ascii_hash_ident);
1631 /* Reading port number from config file */
1633 GNUNET_CONFIGURATION_get_value_number (env->cfg,
1639 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
1642 ("Require valid port number for transport plugin `%s' in configuration!\n"),
1644 libgnunet_plugin_transport_http_done (api);
1647 plugin->port_inbound = port;
1648 gn_timeout = GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT;
1649 timeout = ( gn_timeout.value / 1000);
1650 if ((http_daemon_v4 == NULL) && (http_daemon_v6 == NULL) && (port != 0))
1652 http_daemon_v6 = MHD_start_daemon (MHD_USE_IPv6,
1654 &acceptPolicyCallback,
1655 NULL , &accessHandlerCallback, NULL,
1656 MHD_OPTION_CONNECTION_LIMIT, (unsigned int) 16,
1657 MHD_OPTION_PER_IP_CONNECTION_LIMIT, (unsigned int) 1,
1658 MHD_OPTION_CONNECTION_TIMEOUT, timeout,
1659 /* FIXME: set correct limit */
1660 MHD_OPTION_CONNECTION_MEMORY_LIMIT, (size_t) (16 * 1024),
1661 MHD_OPTION_NOTIFY_COMPLETED, &requestCompletedCallback, NULL,
1663 http_daemon_v4 = MHD_start_daemon (MHD_NO_FLAG,
1665 &acceptPolicyCallback,
1666 NULL , &accessHandlerCallback, NULL,
1667 MHD_OPTION_CONNECTION_LIMIT, (unsigned int) 16,
1668 MHD_OPTION_PER_IP_CONNECTION_LIMIT, (unsigned int) 1,
1669 MHD_OPTION_CONNECTION_TIMEOUT, timeout,
1670 /* FIXME: set correct limit */
1671 MHD_OPTION_CONNECTION_MEMORY_LIMIT, (size_t) (16 * 1024),
1672 MHD_OPTION_NOTIFY_COMPLETED, &requestCompletedCallback, NULL,
1675 if (http_daemon_v4 != NULL)
1676 http_task_v4 = http_daemon_prepare (http_daemon_v4);
1677 if (http_daemon_v6 != NULL)
1678 http_task_v6 = http_daemon_prepare (http_daemon_v6);
1680 if (http_task_v4 != GNUNET_SCHEDULER_NO_TASK)
1681 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Starting MHD with IPv4 on port %u\n",port);
1682 if (http_task_v6 != GNUNET_SCHEDULER_NO_TASK)
1683 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Starting MHD with IPv4 and IPv6 on port %u\n",port);
1686 /* Initializing cURL */
1687 multi_handle = curl_multi_init();
1688 if ( NULL == multi_handle )
1690 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
1692 _("Could not initialize curl multi handle, failed to start http plugin!\n"),
1694 libgnunet_plugin_transport_http_done (api);
1698 GNUNET_OS_network_interfaces_list (&process_interfaces, plugin);
1703 /* end of plugin_transport_template.c */