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_YES
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 response = MHD_create_response_from_data (strlen (HTTP_ERROR_RESPONSE),HTTP_ERROR_RESPONSE, MHD_NO, MHD_NO);
467 res = MHD_queue_response (session, MHD_HTTP_NOT_FOUND, response);
468 MHD_destroy_response (response);
470 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Peer has no valid ident, sent HTTP 1.1/404\n");
472 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Peer has no valid ident, could not send error\n");
476 conn_info = MHD_get_connection_info(session, MHD_CONNECTION_INFO_CLIENT_ADDRESS );
477 /* Incoming IPv4 connection */
478 if ( AF_INET == conn_info->client_addr->sin_family)
480 addrin = conn_info->client_addr;
481 inet_ntop(addrin->sin_family, &(addrin->sin_addr),address,INET_ADDRSTRLEN);
483 /* Incoming IPv6 connection */
484 if ( AF_INET6 == conn_info->client_addr->sin_family)
486 addrin6 = (struct sockaddr_in6 *) conn_info->client_addr;
487 inet_ntop(addrin6->sin6_family, &(addrin6->sin6_addr),address,INET6_ADDRSTRLEN);
489 /* find existing session for address */
491 if (plugin->session_count > 0)
493 cs = plugin->sessions;
497 /* Comparison based on ip address */
498 // res = (0 == memcmp(&(conn_info->client_addr->sin_addr),&(cs->addr->sin_addr), sizeof (struct in_addr))) ? GNUNET_YES : GNUNET_NO;
500 /* Comparison based on ip address, port number and address family */
501 // res = (0 == memcmp((conn_info->client_addr),(cs->addr), sizeof (struct sockaddr_in))) ? GNUNET_YES : GNUNET_NO;
503 /* Comparison based on PeerIdentity */
504 res = (0 == memcmp(&pi_in,&(cs->sender), sizeof (struct GNUNET_PeerIdentity))) ? GNUNET_YES : GNUNET_NO;
506 if ( GNUNET_YES == res)
508 /* existing session for this address found */
509 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Session for peer `%s' found\n",GNUNET_i2s(&cs->sender));
515 /* no existing session, create a new one*/
518 /* create new session object */
519 cs = create_session(conn_info->client_addr, NULL, &pi_in);
521 /* Insert session into linked list */
522 if ( plugin->sessions == NULL)
524 plugin->sessions = cs;
525 plugin->session_count = 1;
527 cs_temp = plugin->sessions;
528 while ( cs_temp->next != NULL )
530 cs_temp = cs_temp->next;
535 plugin->session_count++;
537 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"New Session `%s' inserted, count %u \n", GNUNET_i2s(&cs->sender), plugin->session_count);
541 if (*httpSessionCache == NULL)
543 *httpSessionCache = cs;
544 /* Updating session */
545 memcpy(cs->addr_inbound,conn_info->client_addr, sizeof(struct sockaddr_in));
547 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));
551 cs = *httpSessionCache;
553 /* Is it a PUT or a GET request */
554 if ( 0 == strcmp (MHD_HTTP_METHOD_PUT, method) )
557 if ((*upload_data_size == 0) && (cs->is_put_in_progress == GNUNET_NO))
559 if (cs->pending_inbound_msg->pos !=0 )
561 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
562 _("Incoming message from peer `%s', while existing message with %u bytes was not forwarded to transport'\n"),
563 GNUNET_i2s(&cs->sender), cs->pending_inbound_msg->pos);
564 cs->pending_inbound_msg->pos = 0;
567 cs->is_put_in_progress = GNUNET_YES;
568 cs->is_bad_request = GNUNET_NO;
569 cs->is_active = GNUNET_YES;
573 if ((*upload_data_size > 0) && (cs->is_bad_request != GNUNET_YES))
575 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))
577 /* copy uploaded data to buffer */
578 memcpy(&cs->pending_inbound_msg->buf[cs->pending_inbound_msg->pos],upload_data,*upload_data_size);
579 cs->pending_inbound_msg->pos += *upload_data_size;
580 *upload_data_size = 0;
585 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);
586 cs->is_bad_request = GNUNET_YES;
587 /* (*upload_data_size) bytes not processed */
592 if ((cs->is_put_in_progress == GNUNET_YES) && (cs->is_bad_request == GNUNET_YES))
594 *upload_data_size = 0;
595 response = MHD_create_response_from_data (strlen (HTTP_PUT_RESPONSE),HTTP_PUT_RESPONSE, MHD_NO, MHD_NO);
596 res = MHD_queue_response (session, MHD_HTTP_REQUEST_ENTITY_TOO_LARGE, response);
599 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Sent HTTP/1.1: 413 ENTITY TOO LARGE as PUT Response\n");
600 cs->is_bad_request = GNUNET_NO;
601 cs->is_put_in_progress =GNUNET_NO;
603 MHD_destroy_response (response);
607 if ((*upload_data_size == 0) && (cs->is_put_in_progress == GNUNET_YES) && (cs->is_bad_request == GNUNET_NO))
609 send_error_to_client = GNUNET_YES;
610 struct GNUNET_MessageHeader * gn_msg = NULL;
611 /*check message and forward here */
613 if (cs->pending_inbound_msg->pos >= sizeof (struct GNUNET_MessageHeader))
615 gn_msg = GNUNET_malloc (cs->pending_inbound_msg->pos);
616 memcpy (gn_msg,cs->pending_inbound_msg->buf,cs->pending_inbound_msg->pos);
618 if ((ntohs(gn_msg->size) == cs->pending_inbound_msg->pos))
620 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));
621 /* forwarding message to transport */
624 if ( AF_INET == cs->addr_inbound->sin_family)
626 tmp = GNUNET_malloc (INET_ADDRSTRLEN + 14);
627 inet_ntop(AF_INET, &(cs->addr_inbound)->sin_addr,address,INET_ADDRSTRLEN);
628 GNUNET_asprintf(&tmp,"%s:%u",address,ntohs(cs->addr_inbound->sin_port));
630 /* Incoming IPv6 connection */
631 if ( AF_INET6 == cs->addr_inbound->sin_family)
633 tmp = GNUNET_malloc (INET6_ADDRSTRLEN + 14);
634 inet_ntop(AF_INET6, &((struct sockaddr_in6 *) cs->addr_inbound)->sin6_addr,address,INET6_ADDRSTRLEN);
635 GNUNET_asprintf(&tmp,"[%s]:%u",address,ntohs(cs->addr_inbound->sin_port));
637 plugin->env->receive(plugin->env, &(cs->sender), gn_msg, 1, cs , tmp, strlen(tmp));
639 send_error_to_client = GNUNET_NO;
643 if (send_error_to_client == GNUNET_NO)
645 response = MHD_create_response_from_data (strlen (HTTP_PUT_RESPONSE),HTTP_PUT_RESPONSE, MHD_NO, MHD_NO);
646 res = MHD_queue_response (session, MHD_HTTP_OK, response);
647 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Sent HTTP/1.1: 200 OK as PUT Response\n",HTTP_PUT_RESPONSE, strlen (HTTP_PUT_RESPONSE), res );
648 MHD_destroy_response (response);
652 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Recieved malformed message with %u bytes\n", cs->pending_inbound_msg->pos);
653 response = MHD_create_response_from_data (strlen (HTTP_PUT_RESPONSE),HTTP_PUT_RESPONSE, MHD_NO, MHD_NO);
654 res = MHD_queue_response (session, MHD_HTTP_BAD_REQUEST, response);
655 MHD_destroy_response (response);
656 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Sent HTTP/1.1: 400 BAD REQUEST as PUT Response\n");
659 GNUNET_free_non_null (gn_msg);
660 cs->is_put_in_progress = GNUNET_NO;
661 cs->is_bad_request = GNUNET_NO;
662 cs->pending_inbound_msg->pos = 0;
666 if ( 0 == strcmp (MHD_HTTP_METHOD_GET, method) )
668 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Got GET Request\n");
669 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"URL: `%s'\n",url);
670 response = MHD_create_response_from_data (strlen (HTTP_PUT_RESPONSE),HTTP_PUT_RESPONSE, MHD_NO, MHD_NO);
671 res = MHD_queue_response (session, MHD_HTTP_OK, response);
672 MHD_destroy_response (response);
680 * Call MHD to process pending requests and then go back
681 * and schedule the next run.
683 static void http_daemon_run (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
686 * Function that queries MHD's select sets and
687 * starts the task waiting for them.
689 static GNUNET_SCHEDULER_TaskIdentifier
690 http_daemon_prepare (struct MHD_Daemon *daemon_handle)
692 GNUNET_SCHEDULER_TaskIdentifier ret;
696 struct GNUNET_NETWORK_FDSet *wrs;
697 struct GNUNET_NETWORK_FDSet *wws;
698 struct GNUNET_NETWORK_FDSet *wes;
700 unsigned long long timeout;
702 struct GNUNET_TIME_Relative tv;
707 wrs = GNUNET_NETWORK_fdset_create ();
708 wes = GNUNET_NETWORK_fdset_create ();
709 wws = GNUNET_NETWORK_fdset_create ();
711 GNUNET_assert (MHD_YES ==
712 MHD_get_fdset (daemon_handle,
717 haveto = MHD_get_timeout (daemon_handle, &timeout);
718 if (haveto == MHD_YES)
719 tv.value = (uint64_t) timeout;
721 tv = GNUNET_TIME_UNIT_FOREVER_REL;
722 GNUNET_NETWORK_fdset_copy_native (wrs, &rs, max);
723 GNUNET_NETWORK_fdset_copy_native (wws, &ws, max);
724 GNUNET_NETWORK_fdset_copy_native (wes, &es, max);
725 ret = GNUNET_SCHEDULER_add_select (plugin->env->sched,
726 GNUNET_SCHEDULER_PRIORITY_HIGH,
727 GNUNET_SCHEDULER_NO_TASK,
733 GNUNET_NETWORK_fdset_destroy (wrs);
734 GNUNET_NETWORK_fdset_destroy (wws);
735 GNUNET_NETWORK_fdset_destroy (wes);
740 * Call MHD to process pending requests and then go back
741 * and schedule the next run.
743 static void http_daemon_run (void *cls,
744 const struct GNUNET_SCHEDULER_TaskContext *tc)
746 struct MHD_Daemon *daemon_handle = cls;
748 if (daemon_handle == http_daemon_v4)
749 http_task_v4 = GNUNET_SCHEDULER_NO_TASK;
751 if (daemon_handle == http_daemon_v6)
752 http_task_v6 = GNUNET_SCHEDULER_NO_TASK;
754 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
757 GNUNET_assert (MHD_YES == MHD_run (daemon_handle));
758 if (daemon_handle == http_daemon_v4)
759 http_task_v4 = http_daemon_prepare (daemon_handle);
760 if (daemon_handle == http_daemon_v6)
761 http_task_v6 = http_daemon_prepare (daemon_handle);
766 * Removes a message from the linked list of messages
767 * @param ses session to remove message from
768 * @param msg message to remove
769 * @return GNUNET_SYSERR if msg not found, GNUNET_OK on success
772 static int remove_http_message(struct Session * ses, struct HTTP_Message * msg)
774 struct HTTP_Message * cur;
775 struct HTTP_Message * next;
777 cur = ses->pending_outbound_msg;
781 return GNUNET_SYSERR;
785 ses->pending_outbound_msg = cur->next;
786 GNUNET_free (cur->buf);
787 GNUNET_free (cur->dest_url);
793 while (cur->next!=msg)
795 if (cur->next != NULL)
798 return GNUNET_SYSERR;
801 cur->next = cur->next->next;
802 GNUNET_free (cur->next->buf);
803 GNUNET_free (cur->next->dest_url);
804 GNUNET_free (cur->next);
810 static size_t header_function( void *ptr, size_t size, size_t nmemb, void *stream)
813 unsigned int len = size * nmemb;
815 tmp = GNUNET_malloc ( len+1 );
817 if (tmp[len-2] == 13)
820 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Header: `%s'\n",tmp);
823 if (0==strcmp (tmp,"HTTP/1.1 100 Continue"))
825 res->http_result_code=100;
827 if (0==strcmp (tmp,"HTTP/1.1 200 OK"))
829 res->http_result_code=200;
831 if (0==strcmp (tmp,"HTTP/1.1 400 Bad Request"))
833 res->http_result_code=400;
835 if (0==strcmp (tmp,"HTTP/1.1 404 Not Found"))
837 res->http_result_code=404;
839 if (0==strcmp (tmp,"HTTP/1.1 413 Request entity too large"))
841 res->http_result_code=413;
849 * Callback method used with libcurl
850 * Method is called when libcurl needs to read data during sending
851 * @param stream pointer where to write data
852 * @param size size of an individual element
853 * @param nmemb count of elements that can be written to the buffer
854 * @param ptr source pointer, passed to the libcurl handle
855 * @return bytes written to stream
857 static size_t send_read_callback(void *stream, size_t size, size_t nmemb, void *ptr)
859 struct Session * ses = ptr;
860 struct HTTP_Message * msg = ses->pending_outbound_msg;
861 unsigned int bytes_sent;
864 if (msg->len > (size * nmemb))
865 return CURL_READFUNC_ABORT;
867 if (( msg->pos < msg->len) && (msg->len < (size * nmemb)))
869 memcpy(stream, msg->buf, msg->len);
871 bytes_sent = msg->len;
878 * Callback method used with libcurl
879 * Method is called when libcurl needs to write data during sending
880 * @param stream pointer where to write data
881 * @param size size of an individual element
882 * @param nmemb count of elements that can be written to the buffer
883 * @param ptr destination pointer, passed to the libcurl handle
884 * @return bytes read from stream
886 static size_t send_write_callback( void *stream, size_t size, size_t nmemb, void *ptr)
888 char * data = malloc(size*nmemb +1);
890 memcpy( data, stream, size*nmemb);
891 data[size*nmemb] = '\0';
892 /* Just a dummy print for the response recieved for the PUT message */
893 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Recieved %u bytes: `%s' \n", size * nmemb, data);
895 return (size * nmemb);
900 * Function setting up file descriptors and scheduling task to run
901 * @param session session to send data to
902 * @return bytes sent to peer
904 static size_t send_prepare(struct Session* session );
907 * Function setting up curl handle and selecting message to send
908 * @param ses session to send data to
909 * @return bytes sent to peer
911 static ssize_t send_select_init (struct Session* ses )
915 struct HTTP_Message * msg;
917 if ( NULL == ses->curl_handle)
918 ses->curl_handle = curl_easy_init();
919 if( NULL == ses->curl_handle)
921 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Getting cURL handle failed\n");
924 msg = ses->pending_outbound_msg;
929 curl_easy_setopt(ses->curl_handle, CURLOPT_VERBOSE, 1L);
931 curl_easy_setopt(ses->curl_handle, CURLOPT_URL, msg->dest_url);
932 curl_easy_setopt(ses->curl_handle, CURLOPT_PUT, 1L);
933 curl_easy_setopt(ses->curl_handle, CURLOPT_HEADERFUNCTION, &header_function);
934 curl_easy_setopt(ses->curl_handle, CURLOPT_READFUNCTION, send_read_callback);
935 curl_easy_setopt(ses->curl_handle, CURLOPT_READDATA, ses);
936 curl_easy_setopt(ses->curl_handle, CURLOPT_WRITEFUNCTION, send_write_callback);
937 curl_easy_setopt(ses->curl_handle, CURLOPT_READDATA, ses);
938 curl_easy_setopt(ses->curl_handle, CURLOPT_INFILESIZE_LARGE, (curl_off_t) msg->len);
939 curl_easy_setopt(ses->curl_handle, CURLOPT_TIMEOUT, (long) (timeout.value / 1000 ));
940 curl_easy_setopt(ses->curl_handle, CURLOPT_CONNECTTIMEOUT, HTTP_CONNECT_TIMEOUT);
942 mret = curl_multi_add_handle(multi_handle, ses->curl_handle);
943 if (mret != CURLM_OK)
945 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
946 _("%s failed at %s:%d: `%s'\n"),
947 "curl_multi_add_handle", __FILE__, __LINE__,
948 curl_multi_strerror (mret));
951 bytes_sent = send_prepare (ses );
955 static void send_execute (void *cls,
956 const struct GNUNET_SCHEDULER_TaskContext *tc)
962 struct Session * cs = NULL;
964 http_task_send = GNUNET_SCHEDULER_NO_TASK;
965 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
971 mret = curl_multi_perform (multi_handle, &running);
977 msg = curl_multi_info_read (multi_handle, &running);
978 GNUNET_break (msg != NULL);
981 /* get session for affected curl handle */
982 GNUNET_assert ( msg->easy_handle != NULL );
983 cs = find_session_by_curlhandle (msg->easy_handle);
984 GNUNET_assert ( cs != NULL );
985 GNUNET_assert ( cs->pending_outbound_msg != NULL );
990 if ( (msg->data.result != CURLE_OK) &&
991 (msg->data.result != CURLE_GOT_NOTHING) )
994 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
995 _("%s failed for `%s' at %s:%d: `%s'\n"),
996 "curl_multi_perform",
997 GNUNET_i2s(&cs->sender),
1000 curl_easy_strerror (msg->data.result));
1001 /* sending msg failed*/
1002 if (( NULL != cs->pending_outbound_msg) && ( NULL != cs->pending_outbound_msg->transmit_cont))
1003 cs->pending_outbound_msg->transmit_cont (cs->pending_outbound_msg->transmit_cont_cls,&cs->sender,GNUNET_SYSERR);
1007 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1008 "Send to peer `%s' completed.\n", GNUNET_i2s(&cs->sender));
1010 curl_easy_cleanup(cs->curl_handle);
1011 cs->curl_handle=NULL;
1013 /* Calling transmit continuation */
1014 if (( NULL != cs->pending_outbound_msg) && (NULL != cs->pending_outbound_msg->transmit_cont))
1015 cs->pending_outbound_msg->transmit_cont (cs->pending_outbound_msg->transmit_cont_cls,&cs->sender,GNUNET_OK);
1017 if (GNUNET_OK != remove_http_message(cs, cs->pending_outbound_msg))
1018 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Message could not be removed from session `%s'", GNUNET_i2s(&cs->sender));
1020 /* send pending messages */
1021 if (cs->pending_outbound_msg != NULL)
1023 send_select_init (cs);
1032 while ( (running > 0) );
1035 while (mret == CURLM_CALL_MULTI_PERFORM);
1041 * Function setting up file descriptors and scheduling task to run
1042 * @param ses session to send data to
1043 * @return bytes sent to peer
1045 static size_t send_prepare(struct Session* session )
1051 struct GNUNET_NETWORK_FDSet *grs;
1052 struct GNUNET_NETWORK_FDSet *gws;
1060 mret = curl_multi_fdset (multi_handle, &rs, &ws, &es, &max);
1061 if (mret != CURLM_OK)
1063 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1064 _("%s failed at %s:%d: `%s'\n"),
1065 "curl_multi_fdset", __FILE__, __LINE__,
1066 curl_multi_strerror (mret));
1069 mret = curl_multi_timeout (multi_handle, &to);
1070 if (mret != CURLM_OK)
1072 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1073 _("%s failed at %s:%d: `%s'\n"),
1074 "curl_multi_timeout", __FILE__, __LINE__,
1075 curl_multi_strerror (mret));
1079 grs = GNUNET_NETWORK_fdset_create ();
1080 gws = GNUNET_NETWORK_fdset_create ();
1081 GNUNET_NETWORK_fdset_copy_native (grs, &rs, max + 1);
1082 GNUNET_NETWORK_fdset_copy_native (gws, &ws, max + 1);
1083 http_task_send = GNUNET_SCHEDULER_add_select (plugin->env->sched,
1084 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1085 GNUNET_SCHEDULER_NO_TASK,
1086 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 0),
1091 GNUNET_NETWORK_fdset_destroy (gws);
1092 GNUNET_NETWORK_fdset_destroy (grs);
1094 /* FIXME: return bytes REALLY sent */
1099 * Function that can be used by the transport service to transmit
1100 * a message using the plugin.
1102 * @param cls closure
1103 * @param target who should receive this message
1104 * @param priority how important is the message
1105 * @param msgbuf the message to transmit
1106 * @param msgbuf_size number of bytes in 'msgbuf'
1107 * @param to when should we time out
1108 * @param session which session must be used (or NULL for "any")
1109 * @param addr the address to use (can be NULL if the plugin
1110 * is "on its own" (i.e. re-use existing TCP connection))
1111 * @param addrlen length of the address in bytes
1112 * @param force_address GNUNET_YES if the plugin MUST use the given address,
1113 * otherwise the plugin may use other addresses or
1114 * existing connections (if available)
1115 * @param cont continuation to call once the message has
1116 * been transmitted (or if the transport is ready
1117 * for the next transmission call; or if the
1118 * peer disconnected...)
1119 * @param cont_cls closure for cont
1120 * @return number of bytes used (on the physical network, with overheads);
1121 * -1 on hard errors (i.e. address invalid); 0 is a legal value
1122 * and does NOT mean that the message was not transmitted (DV)
1125 http_plugin_send (void *cls,
1126 const struct GNUNET_PeerIdentity *target,
1129 unsigned int priority,
1130 struct GNUNET_TIME_Relative to,
1131 struct Session *session,
1135 GNUNET_TRANSPORT_TransmitContinuation cont,
1139 struct Session* ses;
1140 struct Session* ses_temp;
1141 struct HTTP_Message * msg;
1142 struct HTTP_Message * tmp;
1147 /* find session for peer */
1148 ses = find_session_by_pi (target);
1150 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Existing session for peer `%s' found\n", GNUNET_i2s(target));
1153 /* create new session object */
1155 ses = create_session(NULL, (struct sockaddr_in *) addr, target);
1156 ses->is_active = GNUNET_YES;
1158 /* Insert session into linked list */
1159 if ( plugin->sessions == NULL)
1161 plugin->sessions = ses;
1162 plugin->session_count = 1;
1164 ses_temp = plugin->sessions;
1165 while ( ses_temp->next != NULL )
1167 ses_temp = ses_temp->next;
1169 if (ses_temp != ses )
1171 ses_temp->next = ses;
1172 plugin->session_count++;
1174 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"New Session `%s' inserted, count %u \n", GNUNET_i2s(target), plugin->session_count);
1177 GNUNET_assert (addr!=NULL);
1180 /* setting url to send to */
1181 if (force_address == GNUNET_YES)
1183 if (addrlen == (sizeof (struct IPv4HttpAddress)))
1185 address = GNUNET_malloc(INET_ADDRSTRLEN + 14 + strlen ((const char *) (&ses->hash)));
1186 inet_ntop(AF_INET,&((struct IPv4HttpAddress *) addr)->ipv4_addr,address,INET_ADDRSTRLEN);
1187 port = ntohs(((struct IPv4HttpAddress *) addr)->u_port);
1188 GNUNET_asprintf(&address,"http://%s:%u/%s",address,port, (char *) (&ses->hash));
1190 else if (addrlen == (sizeof (struct IPv6HttpAddress)))
1192 address = GNUNET_malloc(INET6_ADDRSTRLEN + 14 + strlen ((const char *) (&ses->hash)));
1193 inet_ntop(AF_INET6, &((struct IPv6HttpAddress *) addr)->ipv6_addr,address,INET6_ADDRSTRLEN);
1194 port = ntohs(((struct IPv6HttpAddress *) addr)->u6_port);
1195 GNUNET_asprintf(&address,"http://%s:%u/%s",address,port,(char *) (&ses->hash));
1204 GNUNET_assert (address != NULL);
1207 /* setting up message */
1208 msg = GNUNET_malloc (sizeof (struct HTTP_Message));
1210 msg->len = msgbuf_size;
1212 msg->buf = GNUNET_malloc (msgbuf_size);
1213 msg->dest_url = address;
1214 msg->transmit_cont = cont;
1215 msg->transmit_cont_cls = cont_cls;
1216 memcpy (msg->buf,msgbuf, msgbuf_size);
1218 /* insert created message in list of pending messages */
1219 if (ses->pending_outbound_msg == NULL)
1221 ses->pending_outbound_msg = msg;
1223 tmp = ses->pending_outbound_msg;
1224 while ( NULL != tmp->next)
1233 if (msg == ses->pending_outbound_msg)
1235 bytes_sent = send_select_init (ses);
1244 * Function that can be used to force the plugin to disconnect
1245 * from the given peer and cancel all previous transmissions
1246 * (and their continuationc).
1248 * @param cls closure
1249 * @param target peer from which to disconnect
1252 http_plugin_disconnect (void *cls,
1253 const struct GNUNET_PeerIdentity *target)
1255 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"HTTP Plugin: http_plugin_disconnect\n");
1256 // struct Plugin *plugin = cls;
1262 * Convert the transports address to a nice, human-readable
1265 * @param cls closure
1266 * @param type name of the transport that generated the address
1267 * @param addr one of the addresses of the host, NULL for the last address
1268 * the specific address format depends on the transport
1269 * @param addrlen length of the address
1270 * @param numeric should (IP) addresses be displayed in numeric form?
1271 * @param timeout after how long should we give up?
1272 * @param asc function to call on each string
1273 * @param asc_cls closure for asc
1276 http_plugin_address_pretty_printer (void *cls,
1281 struct GNUNET_TIME_Relative timeout,
1282 GNUNET_TRANSPORT_AddressStringCallback
1285 const struct IPv4HttpAddress *t4;
1286 const struct IPv6HttpAddress *t6;
1287 struct sockaddr_in a4;
1288 struct sockaddr_in6 a6;
1293 if (addrlen == sizeof (struct IPv6HttpAddress))
1295 address = GNUNET_malloc (INET6_ADDRSTRLEN);
1297 a6.sin6_addr = t6->ipv6_addr;
1298 inet_ntop(AF_INET6, &(a6.sin6_addr),address,INET6_ADDRSTRLEN);
1299 port = ntohs(t6->u6_port);
1301 else if (addrlen == sizeof (struct IPv4HttpAddress))
1303 address = GNUNET_malloc (INET_ADDRSTRLEN);
1305 a4.sin_addr.s_addr = t4->ipv4_addr;
1306 inet_ntop(AF_INET, &(a4.sin_addr),address,INET_ADDRSTRLEN);
1307 port = ntohs(t4->u_port);
1311 /* invalid address */
1312 GNUNET_break_op (0);
1313 asc (asc_cls, NULL);
1317 ret = GNUNET_malloc(strlen(address) +14);
1318 GNUNET_asprintf(&ret,"http://%s:%u/",address,port);
1319 GNUNET_free (address);
1326 * Another peer has suggested an address for this
1327 * peer and transport plugin. Check that this could be a valid
1328 * address. If so, consider adding it to the list
1331 * @param cls closure
1332 * @param addr pointer to the address
1333 * @param addrlen length of addr
1334 * @return GNUNET_OK if this is a plausible address for this peer
1338 http_plugin_address_suggested (void *cls,
1339 void *addr, size_t addrlen)
1341 struct IPv4HttpAddress *v4;
1342 struct IPv6HttpAddress *v6;
1345 if ((addrlen != sizeof (struct IPv4HttpAddress)) &&
1346 (addrlen != sizeof (struct IPv6HttpAddress)))
1348 return GNUNET_SYSERR;
1350 if (addrlen == sizeof (struct IPv4HttpAddress))
1352 v4 = (struct IPv4HttpAddress *) addr;
1353 if (INADDR_LOOPBACK == ntohl(v4->ipv4_addr))
1355 return GNUNET_SYSERR;
1357 port = ntohs (v4->u_port);
1358 if (port != plugin->port_inbound)
1360 return GNUNET_SYSERR;
1365 v6 = (struct IPv6HttpAddress *) addr;
1366 if (IN6_IS_ADDR_LINKLOCAL (&v6->ipv6_addr))
1368 return GNUNET_SYSERR;
1370 port = ntohs (v6->u6_port);
1371 if (port != plugin->port_inbound)
1373 return GNUNET_SYSERR;
1383 * Function called for a quick conversion of the binary address to
1384 * a numeric address. Note that the caller must not free the
1385 * address and that the next call to this function is allowed
1386 * to override the address again.
1388 * @param cls closure
1389 * @param addr binary address
1390 * @param addrlen length of the address
1391 * @return string representing the same address
1394 http_plugin_address_to_string (void *cls,
1398 const struct IPv4HttpAddress *t4;
1399 const struct IPv6HttpAddress *t6;
1400 struct sockaddr_in a4;
1401 struct sockaddr_in6 a6;
1406 if (addrlen == sizeof (struct IPv6HttpAddress))
1408 address = GNUNET_malloc (INET6_ADDRSTRLEN);
1410 a6.sin6_addr = t6->ipv6_addr;
1411 inet_ntop(AF_INET6, &(a6.sin6_addr),address,INET6_ADDRSTRLEN);
1412 port = ntohs(t6->u6_port);
1414 else if (addrlen == sizeof (struct IPv4HttpAddress))
1416 address = GNUNET_malloc (INET_ADDRSTRLEN);
1418 a4.sin_addr.s_addr = t4->ipv4_addr;
1419 inet_ntop(AF_INET, &(a4.sin_addr),address,INET_ADDRSTRLEN);
1420 port = ntohs(t4->u_port);
1424 /* invalid address */
1428 ret = GNUNET_malloc(strlen(address) +6);
1429 GNUNET_asprintf(&ret,"%s:%u",address,port);
1430 GNUNET_free (address);
1435 * Add the IP of our network interface to the list of
1436 * our external IP addresses.
1438 * @param cls the 'struct Plugin*'
1439 * @param name name of the interface
1440 * @param isDefault do we think this may be our default interface
1441 * @param addr address of the interface
1442 * @param addrlen number of bytes in addr
1443 * @return GNUNET_OK to continue iterating
1446 process_interfaces (void *cls,
1449 const struct sockaddr *addr, socklen_t addrlen)
1451 struct IPv4HttpAddress t4;
1452 struct IPv6HttpAddress t6;
1459 af = addr->sa_family;
1462 if (INADDR_LOOPBACK == ntohl(((struct sockaddr_in *) addr)->sin_addr.s_addr))
1464 /* skip loopback addresses */
1467 t4.ipv4_addr = ((struct sockaddr_in *) addr)->sin_addr.s_addr;
1468 t4.u_port = htons (plugin->port_inbound);
1472 else if (af == AF_INET6)
1474 if (IN6_IS_ADDR_LINKLOCAL (&((struct sockaddr_in6 *) addr)->sin6_addr))
1476 /* skip link local addresses */
1479 if (IN6_IS_ADDR_LOOPBACK (&((struct sockaddr_in6 *) addr)->sin6_addr))
1481 /* skip loopback addresses */
1484 memcpy (&t6.ipv6_addr,
1485 &((struct sockaddr_in6 *) addr)->sin6_addr,
1486 sizeof (struct in6_addr));
1487 t6.u6_port = htons (plugin->port_inbound);
1496 plugin->env->notify_address(plugin->env->cls,"http",arg, args, GNUNET_TIME_UNIT_FOREVER_REL);
1502 * Exit point from the plugin.
1505 libgnunet_plugin_transport_http_done (void *cls)
1507 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
1508 struct Plugin *plugin = api->cls;
1509 struct Session * cs;
1510 struct Session * cs_next;
1513 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Unloading http plugin...\n");
1515 if ( http_task_v4 != GNUNET_SCHEDULER_NO_TASK)
1517 GNUNET_SCHEDULER_cancel(plugin->env->sched, http_task_v4);
1518 http_task_v4 = GNUNET_SCHEDULER_NO_TASK;
1521 if ( http_task_v6 != GNUNET_SCHEDULER_NO_TASK)
1523 GNUNET_SCHEDULER_cancel(plugin->env->sched, http_task_v6);
1524 http_task_v6 = GNUNET_SCHEDULER_NO_TASK;
1527 if ( http_task_send != GNUNET_SCHEDULER_NO_TASK)
1529 GNUNET_SCHEDULER_cancel(plugin->env->sched, http_task_send);
1530 http_task_send = GNUNET_SCHEDULER_NO_TASK;
1533 if (http_daemon_v4 != NULL)
1535 MHD_stop_daemon (http_daemon_v4);
1536 http_daemon_v4 = NULL;
1538 if (http_daemon_v6 != NULL)
1540 MHD_stop_daemon (http_daemon_v6);
1541 http_daemon_v6 = NULL;
1544 mret = curl_multi_cleanup(multi_handle);
1545 if ( CURLM_OK != mret)
1546 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"curl multihandle clean up failed");
1548 /* free all sessions */
1549 cs = plugin->sessions;
1553 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Freeing session for peer `%s'\n",GNUNET_i2s(&cs->sender));
1557 /* freeing messages */
1558 struct HTTP_Message *cur;
1559 struct HTTP_Message *tmp;
1560 cur = cs->pending_outbound_msg;
1565 if (NULL != cur->buf)
1566 GNUNET_free (cur->buf);
1570 GNUNET_free (cs->pending_inbound_msg->buf);
1571 GNUNET_free (cs->pending_inbound_msg);
1572 GNUNET_free_non_null (cs->addr_inbound);
1573 GNUNET_free_non_null (cs->addr_outbound);
1576 plugin->session_count--;
1582 /* GNUNET_SERVICE_stop (plugin->service); */
1583 GNUNET_free (hostname);
1584 GNUNET_free (plugin);
1591 * Entry point for the plugin.
1594 libgnunet_plugin_transport_http_init (void *cls)
1596 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
1597 struct GNUNET_TRANSPORT_PluginFunctions *api;
1598 struct GNUNET_SERVICE_Context *service;
1599 unsigned int timeout;
1600 struct GNUNET_TIME_Relative gn_timeout;
1601 long long unsigned int port;
1603 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Starting http plugin...\n");
1607 service = GNUNET_SERVICE_start ("transport-http", env->sched, env->cfg);
1608 if (service == NULL)
1610 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, "", _
1611 ("Failed to start service for `%s' transport plugin.\n"),
1617 plugin = GNUNET_malloc (sizeof (struct Plugin));
1619 plugin->sessions = NULL;
1620 plugin->service = service;
1621 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
1623 api->send = &http_plugin_send;
1624 api->disconnect = &http_plugin_disconnect;
1625 api->address_pretty_printer = &http_plugin_address_pretty_printer;
1626 api->check_address = &http_plugin_address_suggested;
1627 api->address_to_string = &http_plugin_address_to_string;
1629 hostname = GNUNET_RESOLVER_local_fqdn_get ();
1631 /* Hashing our identity to use it in URLs */
1632 GNUNET_CRYPTO_hash_to_enc ( &(plugin->env->my_identity->hashPubKey), &my_ascii_hash_ident);
1634 /* Reading port number from config file */
1636 GNUNET_CONFIGURATION_get_value_number (env->cfg,
1642 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
1645 ("Require valid port number for transport plugin `%s' in configuration!\n"),
1647 libgnunet_plugin_transport_http_done (api);
1650 plugin->port_inbound = port;
1651 gn_timeout = GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT;
1652 timeout = ( gn_timeout.value / 1000);
1653 if ((http_daemon_v4 == NULL) && (http_daemon_v6 == NULL) && (port != 0))
1655 http_daemon_v6 = MHD_start_daemon (MHD_USE_IPv6,
1657 &acceptPolicyCallback,
1658 NULL , &accessHandlerCallback, NULL,
1659 MHD_OPTION_CONNECTION_LIMIT, (unsigned int) 16,
1660 MHD_OPTION_PER_IP_CONNECTION_LIMIT, (unsigned int) 1,
1661 MHD_OPTION_CONNECTION_TIMEOUT, timeout,
1662 /* FIXME: set correct limit */
1663 MHD_OPTION_CONNECTION_MEMORY_LIMIT, (size_t) (16 * 1024),
1664 MHD_OPTION_NOTIFY_COMPLETED, &requestCompletedCallback, NULL,
1666 http_daemon_v4 = MHD_start_daemon (MHD_NO_FLAG,
1668 &acceptPolicyCallback,
1669 NULL , &accessHandlerCallback, NULL,
1670 MHD_OPTION_CONNECTION_LIMIT, (unsigned int) 16,
1671 MHD_OPTION_PER_IP_CONNECTION_LIMIT, (unsigned int) 1,
1672 MHD_OPTION_CONNECTION_TIMEOUT, timeout,
1673 /* FIXME: set correct limit */
1674 MHD_OPTION_CONNECTION_MEMORY_LIMIT, (size_t) (16 * 1024),
1675 MHD_OPTION_NOTIFY_COMPLETED, &requestCompletedCallback, NULL,
1678 if (http_daemon_v4 != NULL)
1679 http_task_v4 = http_daemon_prepare (http_daemon_v4);
1680 if (http_daemon_v6 != NULL)
1681 http_task_v6 = http_daemon_prepare (http_daemon_v6);
1683 if (http_task_v4 != GNUNET_SCHEDULER_NO_TASK)
1684 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Starting MHD with IPv4 on port %u\n",port);
1685 if (http_task_v6 != GNUNET_SCHEDULER_NO_TASK)
1686 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Starting MHD with IPv4 and IPv6 on port %u\n",port);
1689 /* Initializing cURL */
1690 multi_handle = curl_multi_init();
1691 if ( NULL == multi_handle )
1693 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
1695 _("Could not initialize curl multi handle, failed to start http plugin!\n"),
1697 libgnunet_plugin_transport_http_done (api);
1701 GNUNET_OS_network_interfaces_list (&process_interfaces, plugin);
1706 /* end of plugin_transport_template.c */