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 "gnunet_server_lib.h"
37 #include "plugin_transport.h"
38 #include "gnunet_os_lib.h"
39 #include "microhttpd.h"
40 #include <curl/curl.h>
43 #define DEBUG_CURL GNUNET_YES
44 #define DEBUG_HTTP GNUNET_NO
47 * Text of the response sent back after the last bytes of a PUT
48 * request have been received (just to formally obey the HTTP
51 #define HTTP_PUT_RESPONSE "Thank you!"
54 * After how long do we expire an address that we
55 * learned from another peer if it is not reconfirmed
58 #define LEARNED_ADDRESS_EXPIRATION GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_HOURS, 6)
61 * Page returned if request invalid
63 #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>"
66 * Timeout for a http connect
68 #define HTTP_CONNECT_TIMEOUT 30
71 * Timeout for a http connect
73 #define HTTP_MESSAGE_INITIAL_BUFFERSIZE GNUNET_SERVER_MAX_MESSAGE_SIZE
77 * Network format for IPv4 addresses.
79 struct IPv4HttpAddress
82 * IPv4 address, in network byte order.
87 * Port number, in network byte order.
95 * Network format for IPv6 addresses.
97 struct IPv6HttpAddress
102 struct in6_addr ipv6_addr;
105 * Port number, in network byte order.
114 * Message to send using http
119 * Next field for linked list
121 struct HTTP_Message * next;
124 * buffer containing data to send
129 * amount of data already sent
141 * Continuation function to call once the transmission buffer
142 * has again space available. NULL if there is no
143 * continuation to call.
145 GNUNET_TRANSPORT_TransmitContinuation transmit_cont;
148 * Closure for transmit_cont.
150 void *transmit_cont_cls;
152 unsigned int http_result_code;
157 * Session handle for connections.
163 * Stored in a linked list.
165 struct Session *next;
168 * Pointer to the global plugin struct.
170 struct Plugin *plugin;
173 * To whom are we talking to (set to our identity
174 * if we are still waiting for the welcome message)
176 struct GNUNET_PeerIdentity partner;
179 * Sender's ip address to distinguish between incoming connections
181 //struct sockaddr_in * addr_inbound;
184 * Sender's ip address to distinguish between incoming connections
195 * Sender's ip address to distinguish between incoming connections
197 //char * addr_inbound_str;
201 * Sender's ip address specified by transport
203 //struct sockaddr_in * addr_outbound;
206 * Did we initiate the connection (GNUNET_YES) or the other peer (GNUNET_NO)?
211 * Is the connection active (GNUNET_YES) or terminated (GNUNET_NO)?
216 * At what time did we reset last_received last?
218 struct GNUNET_TIME_Absolute last_quota_update;
221 * How many bytes have we received since the "last_quota_update"
224 uint64_t last_received;
227 * Number of bytes per ms that this peer is allowed
233 * Is there a HTTP/PUT in progress?
235 int is_put_in_progress;
238 * Is the http request invalid?
245 struct GNUNET_CRYPTO_HashAsciiEncoded hash;
247 struct HTTP_Message * pending_outbound_msg;
249 struct HTTP_Message * pending_inbound_msg;
253 struct GNUNET_SERVER_MessageStreamTokenizer * msgtok;
257 * Encapsulation of all of the state of the plugin.
264 struct GNUNET_TRANSPORT_PluginEnvironment *env;
266 unsigned int port_inbound;
269 * List of open sessions.
271 struct Session *sessions;
274 * Number of active sessions
277 unsigned int session_count;
281 * Daemon for listening for new IPv4 connections.
283 static struct MHD_Daemon *http_daemon_v4;
286 * Daemon for listening for new IPv6connections.
288 static struct MHD_Daemon *http_daemon_v6;
291 * Our primary task for http daemon handling IPv4 connections
293 static GNUNET_SCHEDULER_TaskIdentifier http_task_v4;
296 * Our primary task for http daemon handling IPv6 connections
298 static GNUNET_SCHEDULER_TaskIdentifier http_task_v6;
302 * The task sending data
304 static GNUNET_SCHEDULER_TaskIdentifier http_task_send;
308 * Information about this plugin
310 static struct Plugin *plugin;
315 static CURLM *multi_handle;
318 * Our ASCII encoded, hashed peer identity
319 * This string is used to distinguish between connections and is added to the urls
321 static struct GNUNET_CRYPTO_HashAsciiEncoded my_ascii_hash_ident;
323 // MW: please document (which timeout is this!?)
324 static struct GNUNET_TIME_Relative timeout;
327 * Finds a http session in our linked list using peer identity as a key
328 * @param peer peeridentity
329 * @return http session corresponding to peer identity
331 static struct Session * find_session_by_pi( const struct GNUNET_PeerIdentity *peer )
333 struct Session * cur;
334 GNUNET_HashCode hc_peer;
335 GNUNET_HashCode hc_current;
337 cur = plugin->sessions;
338 hc_peer = peer->hashPubKey;
341 hc_current = cur->partner.hashPubKey;
342 if ( 0 == GNUNET_CRYPTO_hash_cmp( &hc_peer, &hc_current))
344 cur = plugin->sessions->next;
350 * Finds a http session in our linked list using libcurl handle as a key
351 * Needed when sending data with libcurl to differentiate between sessions
352 * @param handle peeridentity
353 * @return http session corresponding to peer identity
355 static struct Session * find_session_by_curlhandle( CURL* handle )
357 struct Session * cur;
359 cur = plugin->sessions;
362 if ( handle == cur->curl_handle )
364 cur = plugin->sessions->next;
370 * Create a new session
372 * @param addr_in address the peer is using inbound
373 * @param addr_out address the peer is using outbound
374 * @param peer identity
375 * @return created session object
377 static struct Session * create_session (char * addr_in, size_t addrlen_in, char * addr_out, size_t addrlen_out, const struct GNUNET_PeerIdentity *peer)
379 struct Session * ses = GNUNET_malloc ( sizeof( struct Session) );
383 ses->addr_in = GNUNET_malloc (addrlen_in);
384 ses->addr_in_len = addrlen_in;
385 memcpy(ses->addr_in,addr_in,addrlen_in);
388 if (addrlen_out != 0)
390 ses->addr_out = GNUNET_malloc (addrlen_out);
391 ses->addr_out_len = addrlen_out;
392 memcpy(ses->addr_out,addr_out,addrlen_out);
394 ses->plugin = plugin;
395 memcpy(&ses->partner, peer, sizeof (struct GNUNET_PeerIdentity));
396 GNUNET_CRYPTO_hash_to_enc(&ses->partner.hashPubKey,&(ses->hash));
397 ses->is_active = GNUNET_NO;
398 ses->pending_inbound_msg = GNUNET_malloc( sizeof (struct HTTP_Message));
399 ses->pending_inbound_msg->buf = GNUNET_malloc(GNUNET_SERVER_MAX_MESSAGE_SIZE);
400 ses->pending_inbound_msg->len = GNUNET_SERVER_MAX_MESSAGE_SIZE;
401 ses->pending_inbound_msg->pos = 0;
407 * Callback called by MHD when a connection is terminated
409 static void requestCompletedCallback (void *cls, struct MHD_Connection * connection, void **httpSessionCache)
413 cs = *httpSessionCache;
416 /*GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection from peer `%s' was terminated\n",GNUNET_i2s(&cs->partner));*/
417 /* session set to inactive */
418 cs->is_active = GNUNET_NO;
419 cs->is_put_in_progress = GNUNET_NO;
423 static void messageTokenizerCallback (void *cls,
426 GNUNET_MessageHeader *
429 struct Session * cs = cls;
430 GNUNET_assert(cs != NULL);
432 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
433 "Received message with type %u and size %u from `%s'\n",
434 ntohs(message->type),
435 ntohs(message->size),
436 GNUNET_i2s(&(cs->partner)));
437 plugin->env->receive(plugin->env->cls,
442 /*(char *)cs->addr_in,
447 * Check if ip is allowed to connect.
450 acceptPolicyCallback (void *cls,
451 const struct sockaddr *addr, socklen_t addr_len)
453 /* Every connection is accepted, nothing more to do here */
458 * Process GET or PUT request received via MHD. For
459 * GET, queue response that will send back our pending
460 * messages. For PUT, process incoming data and send
461 * to GNUnet core. In either case, check if a session
462 * already exists and create a new one if not.
465 accessHandlerCallback (void *cls,
466 struct MHD_Connection *session,
470 const char *upload_data,
471 size_t * upload_data_size, void **httpSessionCache)
473 struct MHD_Response *response;
475 struct Session * cs_temp;
476 const union MHD_ConnectionInfo * conn_info;
477 struct sockaddr_in *addrin;
478 struct sockaddr_in6 *addrin6;
479 char address[INET6_ADDRSTRLEN+14];
480 struct GNUNET_PeerIdentity pi_in;
482 struct GNUNET_MessageHeader *cur_msg;
483 int send_error_to_client;
484 struct IPv4HttpAddress ipv4addr;
485 struct IPv6HttpAddress ipv6addr;
488 send_error_to_client = GNUNET_NO;
490 if ( NULL == *httpSessionCache)
492 /* check url for peer identity */
493 res = GNUNET_CRYPTO_hash_from_string ( &url[1], &(pi_in.hashPubKey));
494 if ( GNUNET_SYSERR == res )
496 response = MHD_create_response_from_data (strlen (HTTP_ERROR_RESPONSE),HTTP_ERROR_RESPONSE, MHD_NO, MHD_NO);
497 res = MHD_queue_response (session, MHD_HTTP_NOT_FOUND, response);
498 MHD_destroy_response (response);
500 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Peer has no valid ident, sent HTTP 1.1/404\n");
502 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Peer has no valid ident, could not send error\n");
506 conn_info = MHD_get_connection_info(session, MHD_CONNECTION_INFO_CLIENT_ADDRESS );
507 /* Incoming IPv4 connection */
508 if ( AF_INET == conn_info->client_addr->sin_family)
510 addrin = conn_info->client_addr;
511 inet_ntop(addrin->sin_family, &(addrin->sin_addr),address,INET_ADDRSTRLEN);
512 memcpy(&ipv4addr.ipv4_addr,&(addrin->sin_addr),sizeof(struct in_addr));
513 ipv4addr.u_port = addrin->sin_port;
515 /* Incoming IPv6 connection */
516 if ( AF_INET6 == conn_info->client_addr->sin_family)
518 addrin6 = (struct sockaddr_in6 *) conn_info->client_addr;
519 inet_ntop(addrin6->sin6_family, &(addrin6->sin6_addr),address,INET6_ADDRSTRLEN);
520 memcpy(&ipv6addr.ipv6_addr,&(addrin6->sin6_addr),sizeof(struct in_addr));
521 ipv6addr.u6_port = addrin6->sin6_port;
523 /* find existing session for address */
525 if (plugin->session_count > 0)
527 cs = plugin->sessions;
530 res = (0 == memcmp(&pi_in,&(cs->partner), sizeof (struct GNUNET_PeerIdentity))) ? GNUNET_YES : GNUNET_NO;
531 if ( GNUNET_YES == res)
536 /* no existing session, create a new one*/
539 /* create new session object */
540 if ( AF_INET6 == conn_info->client_addr->sin_family)
541 cs = create_session((char *) &ipv6addr, sizeof(struct IPv6HttpAddress),NULL, 0, &pi_in);
542 if ( AF_INET == conn_info->client_addr->sin_family)
543 cs = create_session((char *) &ipv4addr, sizeof(struct IPv4HttpAddress),NULL, 0, &pi_in);
545 /* Insert session into linked list */
546 if ( plugin->sessions == NULL)
548 plugin->sessions = cs;
549 plugin->session_count = 1;
551 cs_temp = plugin->sessions;
552 while ( cs_temp->next != NULL )
554 cs_temp = cs_temp->next;
559 plugin->session_count++;
561 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"New Session for peer `%s' inserted, count %u \n", GNUNET_i2s(&cs->partner), plugin->session_count);
564 /* Set closure and update current session*/
565 if (*httpSessionCache == NULL)
567 *httpSessionCache = cs;
568 /* Updating session */
570 memcpy(cs->addr_inbound,conn_info->client_addr, sizeof(struct sockaddr_in));
571 if ( AF_INET == cs->addr_inbound->sin_family)
573 GNUNET_asprintf(&cs->addr_inbound_str,"%s:%u",address,ntohs(cs->addr_inbound->sin_port));
576 if ( AF_INET6 == cs->addr_inbound->sin_family)
578 GNUNET_asprintf(&cs->addr_inbound_str,"[%s]:%u",address,ntohs(cs->addr_inbound->sin_port));
582 if (cs->msgtok==NULL)
583 cs->msgtok = GNUNET_SERVER_mst_create (GNUNET_SERVER_MAX_MESSAGE_SIZE, cs, &messageTokenizerCallback, cs);
585 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"HTTP Daemon has new an incoming `%s' request from peer `%s'\n",method, GNUNET_i2s(&cs->partner));
589 cs = *httpSessionCache;
591 /* Is it a PUT or a GET request */
592 if ( 0 == strcmp (MHD_HTTP_METHOD_PUT, method) )
595 if ((*upload_data_size == 0) && (cs->is_put_in_progress == GNUNET_NO))
597 if (cs->pending_inbound_msg->pos !=0 )
599 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
600 _("Incoming message from peer `%s', while existing message with %u bytes was not forwarded to transport'\n"),
601 GNUNET_i2s(&cs->partner), cs->pending_inbound_msg->pos);
602 cs->pending_inbound_msg->pos = 0;
605 cs->is_put_in_progress = GNUNET_YES;
606 cs->is_bad_request = GNUNET_NO;
607 cs->is_active = GNUNET_YES;
611 if ((*upload_data_size > 0) && (cs->is_bad_request != GNUNET_YES))
613 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))
615 /* copy uploaded data to buffer */
616 memcpy(&cs->pending_inbound_msg->buf[cs->pending_inbound_msg->pos],upload_data,*upload_data_size);
617 cs->pending_inbound_msg->pos += *upload_data_size;
618 *upload_data_size = 0;
623 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);
624 cs->is_bad_request = GNUNET_YES;
625 /* (*upload_data_size) bytes not processed */
630 if ((cs->is_put_in_progress == GNUNET_YES) && (cs->is_bad_request == GNUNET_YES))
632 *upload_data_size = 0;
633 response = MHD_create_response_from_data (strlen (HTTP_PUT_RESPONSE),HTTP_PUT_RESPONSE, MHD_NO, MHD_NO);
634 res = MHD_queue_response (session, MHD_HTTP_REQUEST_ENTITY_TOO_LARGE, response);
637 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Sent HTTP/1.1: 413 Request Entity Too Large as PUT Response\n");
638 cs->is_bad_request = GNUNET_NO;
639 cs->is_put_in_progress =GNUNET_NO;
640 cs->pending_inbound_msg->pos = 0;
642 MHD_destroy_response (response);
646 if ((*upload_data_size == 0) && (cs->is_put_in_progress == GNUNET_YES) && (cs->is_bad_request == GNUNET_NO))
648 send_error_to_client = GNUNET_YES;
650 if (cs->pending_inbound_msg->pos >= sizeof (struct GNUNET_MessageHeader))
652 cur_msg = (struct GNUNET_MessageHeader *) cs->pending_inbound_msg->buf;
653 res = GNUNET_SERVER_mst_receive(cs->msgtok,cs->pending_inbound_msg->buf,cs->pending_inbound_msg->pos, GNUNET_YES, GNUNET_NO);
654 if ((res != GNUNET_SYSERR) && (res != GNUNET_NO))
655 send_error_to_client = GNUNET_NO;
657 if (send_error_to_client == GNUNET_NO)
659 response = MHD_create_response_from_data (strlen (HTTP_PUT_RESPONSE),HTTP_PUT_RESPONSE, MHD_NO, MHD_NO);
660 res = MHD_queue_response (session, MHD_HTTP_OK, response);
661 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Sent HTTP/1.1: 200 OK as PUT Response\n",HTTP_PUT_RESPONSE, strlen (HTTP_PUT_RESPONSE), res );
662 MHD_destroy_response (response);
666 response = MHD_create_response_from_data (strlen (HTTP_PUT_RESPONSE),HTTP_PUT_RESPONSE, MHD_NO, MHD_NO);
667 res = MHD_queue_response (session, MHD_HTTP_BAD_REQUEST, response);
668 MHD_destroy_response (response);
669 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Sent HTTP/1.1: 400 BAD REQUEST as PUT Response\n");
671 cs->is_put_in_progress = GNUNET_NO;
672 cs->is_bad_request = GNUNET_NO;
673 cs->pending_inbound_msg->pos = 0;
677 if ( 0 == strcmp (MHD_HTTP_METHOD_GET, method) )
679 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Got GET Request\n");
680 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"URL: `%s'\n",url);
681 response = MHD_create_response_from_data (strlen (HTTP_PUT_RESPONSE),HTTP_PUT_RESPONSE, MHD_NO, MHD_NO);
682 res = MHD_queue_response (session, MHD_HTTP_OK, response);
683 MHD_destroy_response (response);
691 * Call MHD to process pending requests and then go back
692 * and schedule the next run.
694 static void http_daemon_run (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
697 * Function that queries MHD's select sets and
698 * starts the task waiting for them.
700 static GNUNET_SCHEDULER_TaskIdentifier
701 http_daemon_prepare (struct MHD_Daemon *daemon_handle)
703 GNUNET_SCHEDULER_TaskIdentifier ret;
707 struct GNUNET_NETWORK_FDSet *wrs;
708 struct GNUNET_NETWORK_FDSet *wws;
709 struct GNUNET_NETWORK_FDSet *wes;
711 unsigned long long timeout;
713 struct GNUNET_TIME_Relative tv;
718 wrs = GNUNET_NETWORK_fdset_create ();
719 wes = GNUNET_NETWORK_fdset_create ();
720 wws = GNUNET_NETWORK_fdset_create ();
722 GNUNET_assert (MHD_YES ==
723 MHD_get_fdset (daemon_handle,
728 haveto = MHD_get_timeout (daemon_handle, &timeout);
729 if (haveto == MHD_YES)
730 tv.value = (uint64_t) timeout;
732 tv = GNUNET_TIME_UNIT_FOREVER_REL;
733 GNUNET_NETWORK_fdset_copy_native (wrs, &rs, max);
734 GNUNET_NETWORK_fdset_copy_native (wws, &ws, max);
735 GNUNET_NETWORK_fdset_copy_native (wes, &es, max);
736 ret = GNUNET_SCHEDULER_add_select (plugin->env->sched,
737 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
738 GNUNET_SCHEDULER_NO_TASK,
744 GNUNET_NETWORK_fdset_destroy (wrs);
745 GNUNET_NETWORK_fdset_destroy (wws);
746 GNUNET_NETWORK_fdset_destroy (wes);
751 * Call MHD to process pending requests and then go back
752 * and schedule the next run.
754 static void http_daemon_run (void *cls,
755 const struct GNUNET_SCHEDULER_TaskContext *tc)
757 struct MHD_Daemon *daemon_handle = cls;
759 if (daemon_handle == http_daemon_v4)
760 http_task_v4 = GNUNET_SCHEDULER_NO_TASK;
762 if (daemon_handle == http_daemon_v6)
763 http_task_v6 = GNUNET_SCHEDULER_NO_TASK;
765 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
768 GNUNET_assert (MHD_YES == MHD_run (daemon_handle));
769 if (daemon_handle == http_daemon_v4)
770 http_task_v4 = http_daemon_prepare (daemon_handle);
771 if (daemon_handle == http_daemon_v6)
772 http_task_v6 = http_daemon_prepare (daemon_handle);
777 * Removes a message from the linked list of messages
778 * @param ses session to remove message from
779 * @param msg message to remove
780 * @return GNUNET_SYSERR if msg not found, GNUNET_OK on success
783 static int remove_http_message(struct Session * ses, struct HTTP_Message * msg)
785 struct HTTP_Message * cur;
786 struct HTTP_Message * next;
788 cur = ses->pending_outbound_msg;
792 return GNUNET_SYSERR;
796 ses->pending_outbound_msg = cur->next;
797 GNUNET_free (cur->buf);
798 GNUNET_free (cur->dest_url);
804 while (cur->next!=msg)
806 if (cur->next != NULL)
809 return GNUNET_SYSERR;
812 cur->next = cur->next->next;
813 GNUNET_free (cur->next->buf);
814 GNUNET_free (cur->next->dest_url);
815 GNUNET_free (cur->next);
821 static size_t header_function( void *ptr, size_t size, size_t nmemb, void *stream)
824 size_t len = size * nmemb;
825 struct Session * ses = stream;
828 if ((size * nmemb) < SIZE_MAX)
829 tmp = GNUNET_malloc (len+1);
831 if ((tmp != NULL) && (len > 0))
836 if (tmp[len-2] == 13)
840 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Header: `%s'\n",tmp);
842 if (0==strcmp (tmp,"HTTP/1.1 100 Continue"))
844 ses->pending_outbound_msg->http_result_code=100;
846 if (0==strcmp (tmp,"HTTP/1.1 200 OK"))
848 ses->pending_outbound_msg->http_result_code=200;
850 if (0==strcmp (tmp,"HTTP/1.1 400 Bad Request"))
852 ses->pending_outbound_msg->http_result_code=400;
854 if (0==strcmp (tmp,"HTTP/1.1 404 Not Found"))
856 ses->pending_outbound_msg->http_result_code=404;
858 if (0==strcmp (tmp,"HTTP/1.1 413 Request Entity Too Large"))
860 ses->pending_outbound_msg->http_result_code=413;
870 * Callback method used with libcurl
871 * Method is called when libcurl needs to read data during sending
872 * @param stream pointer where to write data
873 * @param size size of an individual element
874 * @param nmemb count of elements that can be written to the buffer
875 * @param ptr source pointer, passed to the libcurl handle
876 * @return bytes written to stream
878 static size_t send_read_callback(void *stream, size_t size, size_t nmemb, void *ptr)
880 struct Session * ses = ptr;
881 struct HTTP_Message * msg = ses->pending_outbound_msg;
886 if (( msg->pos < msg->len))
888 /* data fit in buffer */
889 if ((msg->len - msg->pos) <= (size * nmemb))
891 len = (msg->len - msg->pos);
892 memcpy(stream, &msg->buf[msg->pos], len);
899 memcpy(stream, &msg->buf[msg->pos], len);
904 /* no data to send */
913 * Callback method used with libcurl
914 * Method is called when libcurl needs to write data during sending
915 * @param stream pointer where to write data
916 * @param size size of an individual element
917 * @param nmemb count of elements that can be written to the buffer
918 * @param ptr destination pointer, passed to the libcurl handle
919 * @return bytes read from stream
921 static size_t send_write_callback( void *stream, size_t size, size_t nmemb, void *ptr)
925 if ((size * nmemb) < SIZE_MAX)
926 data = GNUNET_malloc(size*nmemb +1);
929 memcpy( data, stream, size*nmemb);
930 data[size*nmemb] = '\0';
933 return (size * nmemb);
938 * Function setting up file descriptors and scheduling task to run
939 * @param ses session to send data to
940 * @return bytes sent to peer
942 static size_t send_prepare(struct Session* ses );
945 * Function setting up curl handle and selecting message to send
946 * @param ses session to send data to
947 * @return bytes sent to peer
949 static ssize_t send_select_init (struct Session* ses )
953 struct HTTP_Message * msg;
955 if ( NULL == ses->curl_handle)
956 ses->curl_handle = curl_easy_init();
957 if( NULL == ses->curl_handle)
959 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Getting cURL handle failed\n");
962 msg = ses->pending_outbound_msg;
967 curl_easy_setopt(ses->curl_handle, CURLOPT_VERBOSE, 1L);
969 curl_easy_setopt(ses->curl_handle, CURLOPT_URL, msg->dest_url);
970 curl_easy_setopt(ses->curl_handle, CURLOPT_PUT, 1L);
971 curl_easy_setopt(ses->curl_handle, CURLOPT_HEADERFUNCTION, &header_function);
972 curl_easy_setopt(ses->curl_handle, CURLOPT_WRITEHEADER, ses);
973 curl_easy_setopt(ses->curl_handle, CURLOPT_READFUNCTION, send_read_callback);
974 curl_easy_setopt(ses->curl_handle, CURLOPT_READDATA, ses);
975 curl_easy_setopt(ses->curl_handle, CURLOPT_WRITEFUNCTION, send_write_callback);
976 curl_easy_setopt(ses->curl_handle, CURLOPT_READDATA, ses);
977 curl_easy_setopt(ses->curl_handle, CURLOPT_INFILESIZE_LARGE, (curl_off_t) msg->len);
978 curl_easy_setopt(ses->curl_handle, CURLOPT_TIMEOUT, (long) (timeout.value / 1000 ));
979 curl_easy_setopt(ses->curl_handle, CURLOPT_CONNECTTIMEOUT, HTTP_CONNECT_TIMEOUT);
980 curl_easy_setopt(ses->curl_handle, CURLOPT_BUFFERSIZE, GNUNET_SERVER_MAX_MESSAGE_SIZE);
982 mret = curl_multi_add_handle(multi_handle, ses->curl_handle);
983 if (mret != CURLM_OK)
985 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
986 _("%s failed at %s:%d: `%s'\n"),
987 "curl_multi_add_handle", __FILE__, __LINE__,
988 curl_multi_strerror (mret));
991 bytes_sent = send_prepare (ses );
995 static void send_execute (void *cls,
996 const struct GNUNET_SCHEDULER_TaskContext *tc)
998 static unsigned int handles_last_run;
1000 struct CURLMsg *msg;
1002 struct Session * cs = NULL;
1004 http_task_send = GNUNET_SCHEDULER_NO_TASK;
1005 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1011 mret = curl_multi_perform (multi_handle, &running);
1012 if (running < handles_last_run)
1017 msg = curl_multi_info_read (multi_handle, &running);
1018 GNUNET_break (msg != NULL);
1021 /* get session for affected curl handle */
1022 GNUNET_assert ( msg->easy_handle != NULL );
1023 cs = find_session_by_curlhandle (msg->easy_handle);
1024 GNUNET_assert ( cs != NULL );
1025 GNUNET_assert ( cs->pending_outbound_msg != NULL );
1030 if ( (msg->data.result != CURLE_OK) &&
1031 (msg->data.result != CURLE_GOT_NOTHING) )
1033 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1034 _("%s failed for `%s' at %s:%d: `%s'\n"),
1035 "curl_multi_perform",
1036 GNUNET_i2s(&cs->partner),
1039 curl_easy_strerror (msg->data.result));
1040 /* sending msg failed*/
1041 if (( NULL != cs->pending_outbound_msg) && ( NULL != cs->pending_outbound_msg->transmit_cont))
1042 cs->pending_outbound_msg->transmit_cont (cs->pending_outbound_msg->transmit_cont_cls,&cs->partner,GNUNET_SYSERR);
1047 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1048 "Send to peer `%s' completed with code %u\n", GNUNET_i2s(&cs->partner),cs->pending_outbound_msg->http_result_code);
1050 curl_easy_cleanup(cs->curl_handle);
1051 cs->curl_handle=NULL;
1053 /* Calling transmit continuation */
1054 if (( NULL != cs->pending_outbound_msg) && (NULL != cs->pending_outbound_msg->transmit_cont))
1056 /* HTTP 1xx : Last message before here was informational */
1057 if ((cs->pending_outbound_msg->http_result_code >=100) && (cs->pending_outbound_msg->http_result_code < 200))
1058 cs->pending_outbound_msg->transmit_cont (cs->pending_outbound_msg->transmit_cont_cls,&cs->partner,GNUNET_OK);
1059 /* HTTP 2xx: successful operations */
1060 if ((cs->pending_outbound_msg->http_result_code >=200) && (cs->pending_outbound_msg->http_result_code < 300))
1061 cs->pending_outbound_msg->transmit_cont (cs->pending_outbound_msg->transmit_cont_cls,&cs->partner,GNUNET_OK);
1062 /* HTTP 3xx..5xx: error */
1063 if ((cs->pending_outbound_msg->http_result_code >=300) && (cs->pending_outbound_msg->http_result_code < 600))
1064 cs->pending_outbound_msg->transmit_cont (cs->pending_outbound_msg->transmit_cont_cls,&cs->partner,GNUNET_SYSERR);
1067 if (GNUNET_OK != remove_http_message(cs, cs->pending_outbound_msg))
1068 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Message could not be removed from session `%s'", GNUNET_i2s(&cs->partner));
1069 /* send pending messages */
1070 if (cs->pending_outbound_msg != NULL)
1072 send_select_init (cs);
1080 while ( (running > 0) );
1082 handles_last_run = running;
1084 while (mret == CURLM_CALL_MULTI_PERFORM);
1090 * Function setting up file descriptors and scheduling task to run
1091 * @param ses session to send data to
1092 * @return bytes sent to peer
1094 static size_t send_prepare(struct Session* ses )
1100 struct GNUNET_NETWORK_FDSet *grs;
1101 struct GNUNET_NETWORK_FDSet *gws;
1109 mret = curl_multi_fdset (multi_handle, &rs, &ws, &es, &max);
1110 if (mret != CURLM_OK)
1112 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1113 _("%s failed at %s:%d: `%s'\n"),
1114 "curl_multi_fdset", __FILE__, __LINE__,
1115 curl_multi_strerror (mret));
1118 mret = curl_multi_timeout (multi_handle, &to);
1119 if (mret != CURLM_OK)
1121 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1122 _("%s failed at %s:%d: `%s'\n"),
1123 "curl_multi_timeout", __FILE__, __LINE__,
1124 curl_multi_strerror (mret));
1128 grs = GNUNET_NETWORK_fdset_create ();
1129 gws = GNUNET_NETWORK_fdset_create ();
1130 GNUNET_NETWORK_fdset_copy_native (grs, &rs, max + 1);
1131 GNUNET_NETWORK_fdset_copy_native (gws, &ws, max + 1);
1132 http_task_send = GNUNET_SCHEDULER_add_select (plugin->env->sched,
1133 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1134 GNUNET_SCHEDULER_NO_TASK,
1135 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 0),
1140 GNUNET_NETWORK_fdset_destroy (gws);
1141 GNUNET_NETWORK_fdset_destroy (grs);
1143 /* FIXME: return bytes REALLY sent */
1148 * Function that can be used by the transport service to transmit
1149 * a message using the plugin.
1151 * @param cls closure
1152 * @param target who should receive this message
1153 * @param priority how important is the message
1154 * @param msgbuf the message to transmit
1155 * @param msgbuf_size number of bytes in 'msgbuf'
1156 * @param to when should we time out
1157 * @param session which session must be used (or NULL for "any")
1158 * @param addr the address to use (can be NULL if the plugin
1159 * is "on its own" (i.e. re-use existing TCP connection))
1160 * @param addrlen length of the address in bytes
1161 * @param force_address GNUNET_YES if the plugin MUST use the given address,
1162 * otherwise the plugin may use other addresses or
1163 * existing connections (if available)
1164 * @param cont continuation to call once the message has
1165 * been transmitted (or if the transport is ready
1166 * for the next transmission call; or if the
1167 * peer disconnected...)
1168 * @param cont_cls closure for cont
1169 * @return number of bytes used (on the physical network, with overheads);
1170 * -1 on hard errors (i.e. address invalid); 0 is a legal value
1171 * and does NOT mean that the message was not transmitted (DV)
1174 http_plugin_send (void *cls,
1175 const struct GNUNET_PeerIdentity *target,
1178 unsigned int priority,
1179 struct GNUNET_TIME_Relative to,
1180 struct Session *session,
1184 GNUNET_TRANSPORT_TransmitContinuation cont,
1189 struct Session* ses;
1190 struct Session* ses_temp;
1191 struct HTTP_Message * msg;
1192 struct HTTP_Message * tmp;
1197 /* find session for peer */
1198 ses = find_session_by_pi (target);
1199 /* if (NULL != ses ) GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Existing session for peer `%s' found\n", GNUNET_i2s(target));*/
1202 /* create new session object */
1204 ses = create_session((char *) addr, addrlen, NULL, 0, target);
1205 ses->is_active = GNUNET_YES;
1207 /* Insert session into linked list */
1208 if ( plugin->sessions == NULL)
1210 plugin->sessions = ses;
1211 plugin->session_count = 1;
1213 ses_temp = plugin->sessions;
1214 while ( ses_temp->next != NULL )
1216 ses_temp = ses_temp->next;
1218 if (ses_temp != ses )
1220 ses_temp->next = ses;
1221 plugin->session_count++;
1223 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1224 "New Session `%s' inserted, count %u\n", GNUNET_i2s(target), plugin->session_count);
1227 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1228 "Session `%s' found, count %u\n", GNUNET_i2s(target), plugin->session_count);
1230 GNUNET_assert (addr!=NULL);
1232 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"HTTP Plugin: len `%u'\n",addrlen);
1233 if (ses->addr_out != NULL) GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"have addr out!!!!'\n");
1234 if (addrlen == (sizeof (struct IPv4HttpAddress)))
1236 address = GNUNET_malloc(INET_ADDRSTRLEN + 1);
1237 inet_ntop(AF_INET, &((struct IPv4HttpAddress *) addr)->ipv4_addr,address,INET_ADDRSTRLEN);
1238 port = ntohs(((struct IPv4HttpAddress *) addr)->u_port);
1239 GNUNET_asprintf (&url,
1243 (char *) (&my_ascii_hash_ident));
1244 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"HTTP Plugin: address `%s'\n",address);
1245 GNUNET_free(address);
1247 else if (addrlen == (sizeof (struct IPv6HttpAddress)))
1249 address = GNUNET_malloc(INET6_ADDRSTRLEN + 1);
1250 inet_ntop(AF_INET6, &((struct IPv6HttpAddress *) addr)->ipv6_addr,address,INET6_ADDRSTRLEN);
1251 port = ntohs(((struct IPv6HttpAddress *) addr)->u6_port);
1252 GNUNET_asprintf(&url,
1254 address,port,(char *) (&my_ascii_hash_ident));
1255 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"HTTP Plugin: address `%s'\n",address);
1256 GNUNET_free(address);
1259 /* setting up message */
1260 msg = GNUNET_malloc (sizeof (struct HTTP_Message));
1262 msg->len = msgbuf_size;
1264 msg->buf = GNUNET_malloc (msgbuf_size);
1265 msg->dest_url = url;
1266 msg->transmit_cont = cont;
1267 msg->transmit_cont_cls = cont_cls;
1268 memcpy (msg->buf,msgbuf, msgbuf_size);
1269 /* insert created message in list of pending messages */
1270 if (ses->pending_outbound_msg == NULL)
1272 ses->pending_outbound_msg = msg;
1274 tmp = ses->pending_outbound_msg;
1275 while ( NULL != tmp->next)
1283 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"HTTP Plugin: sending %u bytes of data from peer `%4.4s' to peer `%s'\n",msgbuf_size,(char *) &my_ascii_hash_ident,GNUNET_i2s(target));
1284 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"HTTP Plugin: url `%s'\n",url);
1285 if (msg == ses->pending_outbound_msg)
1287 bytes_sent = send_select_init (ses);
1296 * Function that can be used to force the plugin to disconnect
1297 * from the given peer and cancel all previous transmissions
1298 * (and their continuationc).
1300 * @param cls closure
1301 * @param target peer from which to disconnect
1304 http_plugin_disconnect (void *cls,
1305 const struct GNUNET_PeerIdentity *target)
1307 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"HTTP Plugin: http_plugin_disconnect\n");
1308 // struct Plugin *plugin = cls;
1314 * Convert the transports address to a nice, human-readable
1317 * @param cls closure
1318 * @param type name of the transport that generated the address
1319 * @param addr one of the addresses of the host, NULL for the last address
1320 * the specific address format depends on the transport
1321 * @param addrlen length of the address
1322 * @param numeric should (IP) addresses be displayed in numeric form?
1323 * @param timeout after how long should we give up?
1324 * @param asc function to call on each string
1325 * @param asc_cls closure for asc
1328 http_plugin_address_pretty_printer (void *cls,
1333 struct GNUNET_TIME_Relative timeout,
1334 GNUNET_TRANSPORT_AddressStringCallback
1337 const struct IPv4HttpAddress *t4;
1338 const struct IPv6HttpAddress *t6;
1339 struct sockaddr_in a4;
1340 struct sockaddr_in6 a6;
1346 if (addrlen == sizeof (struct IPv6HttpAddress))
1348 address = GNUNET_malloc (INET6_ADDRSTRLEN);
1350 a6.sin6_addr = t6->ipv6_addr;
1351 inet_ntop(AF_INET6, &(a6.sin6_addr),address,INET6_ADDRSTRLEN);
1352 port = ntohs(t6->u6_port);
1354 else if (addrlen == sizeof (struct IPv4HttpAddress))
1356 address = GNUNET_malloc (INET_ADDRSTRLEN);
1358 a4.sin_addr.s_addr = t4->ipv4_addr;
1359 inet_ntop(AF_INET, &(a4.sin_addr),address,INET_ADDRSTRLEN);
1360 port = ntohs(t4->u_port);
1364 /* invalid address */
1365 GNUNET_break_op (0);
1366 asc (asc_cls, NULL);
1369 res = GNUNET_asprintf(&ret,"http://%s:%u/",address,port);
1370 GNUNET_free (address);
1371 GNUNET_assert(res != 0);
1379 * Another peer has suggested an address for this
1380 * peer and transport plugin. Check that this could be a valid
1381 * address. If so, consider adding it to the list
1384 * @param cls closure
1385 * @param addr pointer to the address
1386 * @param addrlen length of addr
1387 * @return GNUNET_OK if this is a plausible address for this peer
1391 http_plugin_address_suggested (void *cls,
1392 void *addr, size_t addrlen)
1394 struct IPv4HttpAddress *v4;
1395 struct IPv6HttpAddress *v6;
1398 if ((addrlen != sizeof (struct IPv4HttpAddress)) &&
1399 (addrlen != sizeof (struct IPv6HttpAddress)))
1401 return GNUNET_SYSERR;
1403 if (addrlen == sizeof (struct IPv4HttpAddress))
1405 v4 = (struct IPv4HttpAddress *) addr;
1406 if (INADDR_LOOPBACK == ntohl(v4->ipv4_addr))
1408 return GNUNET_SYSERR;
1410 port = ntohs (v4->u_port);
1411 if (port != plugin->port_inbound)
1413 return GNUNET_SYSERR;
1418 v6 = (struct IPv6HttpAddress *) addr;
1419 if (IN6_IS_ADDR_LINKLOCAL (&v6->ipv6_addr))
1421 return GNUNET_SYSERR;
1423 port = ntohs (v6->u6_port);
1424 if (port != plugin->port_inbound)
1426 return GNUNET_SYSERR;
1436 * Function called for a quick conversion of the binary address to
1437 * a numeric address. Note that the caller must not free the
1438 * address and that the next call to this function is allowed
1439 * to override the address again.
1441 * @param cls closure
1442 * @param addr binary address
1443 * @param addrlen length of the address
1444 * @return string representing the same address
1447 http_plugin_address_to_string (void *cls,
1451 const struct IPv4HttpAddress *t4;
1452 const struct IPv6HttpAddress *t6;
1453 struct sockaddr_in a4;
1454 struct sockaddr_in6 a6;
1460 if (addrlen == sizeof (struct IPv6HttpAddress))
1462 address = GNUNET_malloc (INET6_ADDRSTRLEN);
1464 a6.sin6_addr = t6->ipv6_addr;
1465 inet_ntop(AF_INET6, &(a6.sin6_addr),address,INET6_ADDRSTRLEN);
1466 port = ntohs(t6->u6_port);
1468 else if (addrlen == sizeof (struct IPv4HttpAddress))
1470 address = GNUNET_malloc (INET_ADDRSTRLEN);
1472 a4.sin_addr.s_addr = t4->ipv4_addr;
1473 inet_ntop(AF_INET, &(a4.sin_addr),address,INET_ADDRSTRLEN);
1474 port = ntohs(t4->u_port);
1478 /* invalid address */
1481 res = GNUNET_asprintf(&ret,"%s:%u",address,port);
1482 GNUNET_free (address);
1483 GNUNET_assert(res != 0);
1488 * Add the IP of our network interface to the list of
1489 * our external IP addresses.
1491 * @param cls the 'struct Plugin*'
1492 * @param name name of the interface
1493 * @param isDefault do we think this may be our default interface
1494 * @param addr address of the interface
1495 * @param addrlen number of bytes in addr
1496 * @return GNUNET_OK to continue iterating
1499 process_interfaces (void *cls,
1502 const struct sockaddr *addr, socklen_t addrlen)
1504 struct IPv4HttpAddress t4;
1505 struct IPv6HttpAddress t6;
1510 af = addr->sa_family;
1513 if (INADDR_LOOPBACK == ntohl(((struct sockaddr_in *) addr)->sin_addr.s_addr))
1515 /* skip loopback addresses */
1518 t4.ipv4_addr = ((struct sockaddr_in *) addr)->sin_addr.s_addr;
1519 t4.u_port = htons (plugin->port_inbound);
1523 else if (af == AF_INET6)
1525 if (IN6_IS_ADDR_LINKLOCAL (&((struct sockaddr_in6 *) addr)->sin6_addr))
1527 /* skip link local addresses */
1530 if (IN6_IS_ADDR_LOOPBACK (&((struct sockaddr_in6 *) addr)->sin6_addr))
1532 /* skip loopback addresses */
1535 memcpy (&t6.ipv6_addr,
1536 &((struct sockaddr_in6 *) addr)->sin6_addr,
1537 sizeof (struct in6_addr));
1538 t6.u6_port = htons (plugin->port_inbound);
1547 plugin->env->notify_address(plugin->env->cls,"http",arg, args, GNUNET_TIME_UNIT_FOREVER_REL);
1552 * Exit point from the plugin.
1555 libgnunet_plugin_transport_http_done (void *cls)
1557 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
1558 struct Plugin *plugin = api->cls;
1559 struct Session * cs;
1560 struct Session * cs_next;
1563 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Unloading http plugin...\n");
1565 if ( http_task_v4 != GNUNET_SCHEDULER_NO_TASK)
1567 GNUNET_SCHEDULER_cancel(plugin->env->sched, http_task_v4);
1568 http_task_v4 = GNUNET_SCHEDULER_NO_TASK;
1571 if ( http_task_v6 != GNUNET_SCHEDULER_NO_TASK)
1573 GNUNET_SCHEDULER_cancel(plugin->env->sched, http_task_v6);
1574 http_task_v6 = GNUNET_SCHEDULER_NO_TASK;
1577 if ( http_task_send != GNUNET_SCHEDULER_NO_TASK)
1579 GNUNET_SCHEDULER_cancel(plugin->env->sched, http_task_send);
1580 http_task_send = GNUNET_SCHEDULER_NO_TASK;
1583 if (http_daemon_v4 != NULL)
1585 MHD_stop_daemon (http_daemon_v4);
1586 http_daemon_v4 = NULL;
1588 if (http_daemon_v6 != NULL)
1590 MHD_stop_daemon (http_daemon_v6);
1591 http_daemon_v6 = NULL;
1594 /* free all sessions */
1595 cs = plugin->sessions;
1599 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Freeing session for peer `%s'\n",GNUNET_i2s(&cs->partner));
1603 /* freeing messages */
1604 struct HTTP_Message *cur;
1605 struct HTTP_Message *tmp;
1606 cur = cs->pending_outbound_msg;
1611 GNUNET_free_non_null(cur->dest_url);
1612 GNUNET_free_non_null (cur->buf);
1616 GNUNET_SERVER_mst_destroy (cs->msgtok);
1617 GNUNET_free (cs->pending_inbound_msg->buf);
1618 GNUNET_free (cs->pending_inbound_msg);
1619 GNUNET_free_non_null (cs->addr_in);
1620 GNUNET_free_non_null (cs->addr_out);
1623 plugin->session_count--;
1627 mret = curl_multi_cleanup(multi_handle);
1628 if ( CURLM_OK != mret)
1629 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"curl multihandle clean up failed");
1631 GNUNET_free (plugin);
1638 * Entry point for the plugin.
1641 libgnunet_plugin_transport_http_init (void *cls)
1643 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
1644 struct GNUNET_TRANSPORT_PluginFunctions *api;
1645 unsigned int timeout;
1646 struct GNUNET_TIME_Relative gn_timeout;
1647 long long unsigned int port;
1649 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Starting http plugin...\n");
1651 plugin = GNUNET_malloc (sizeof (struct Plugin));
1653 plugin->sessions = NULL;
1654 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
1656 api->send = &http_plugin_send;
1657 api->disconnect = &http_plugin_disconnect;
1658 api->address_pretty_printer = &http_plugin_address_pretty_printer;
1659 api->check_address = &http_plugin_address_suggested;
1660 api->address_to_string = &http_plugin_address_to_string;
1662 /* Hashing our identity to use it in URLs */
1663 GNUNET_CRYPTO_hash_to_enc ( &(plugin->env->my_identity->hashPubKey), &my_ascii_hash_ident);
1665 /* Reading port number from config file */
1667 GNUNET_CONFIGURATION_get_value_number (env->cfg,
1673 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
1676 ("Require valid port number for transport plugin `%s' in configuration!\n"),
1678 libgnunet_plugin_transport_http_done (api);
1681 GNUNET_assert ((port > 0) && (port <= 65535));
1682 plugin->port_inbound = port;
1683 gn_timeout = GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT;
1684 timeout = ( gn_timeout.value / 1000);
1685 if ((http_daemon_v4 == NULL) && (http_daemon_v6 == NULL) && (port != 0))
1687 http_daemon_v6 = MHD_start_daemon (MHD_USE_IPv6,
1689 &acceptPolicyCallback,
1690 NULL , &accessHandlerCallback, NULL,
1691 MHD_OPTION_CONNECTION_LIMIT, (unsigned int) 16,
1692 MHD_OPTION_PER_IP_CONNECTION_LIMIT, (unsigned int) 1,
1693 MHD_OPTION_CONNECTION_TIMEOUT, timeout,
1694 /* FIXME: set correct limit */
1695 MHD_OPTION_CONNECTION_MEMORY_LIMIT, (size_t) (16 * 1024),
1696 MHD_OPTION_NOTIFY_COMPLETED, &requestCompletedCallback, NULL,
1698 http_daemon_v4 = MHD_start_daemon (MHD_NO_FLAG,
1700 &acceptPolicyCallback,
1701 NULL , &accessHandlerCallback, NULL,
1702 MHD_OPTION_CONNECTION_LIMIT, (unsigned int) 16,
1703 MHD_OPTION_PER_IP_CONNECTION_LIMIT, (unsigned int) 1,
1704 MHD_OPTION_CONNECTION_TIMEOUT, timeout,
1705 /* FIXME: set correct limit */
1706 MHD_OPTION_CONNECTION_MEMORY_LIMIT, (size_t) (16 * 1024),
1707 MHD_OPTION_NOTIFY_COMPLETED, &requestCompletedCallback, NULL,
1710 if (http_daemon_v4 != NULL)
1711 http_task_v4 = http_daemon_prepare (http_daemon_v4);
1712 if (http_daemon_v6 != NULL)
1713 http_task_v6 = http_daemon_prepare (http_daemon_v6);
1715 if (http_task_v4 != GNUNET_SCHEDULER_NO_TASK)
1716 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Starting MHD with IPv4 on port %u\n",port);
1717 else if (http_task_v6 != GNUNET_SCHEDULER_NO_TASK)
1718 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Starting MHD with IPv4 and IPv6 on port %u\n",port);
1721 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"No MHD was started, transport plugin not functional!\n");
1722 libgnunet_plugin_transport_http_done (api);
1726 /* Initializing cURL */
1727 multi_handle = curl_multi_init();
1728 if ( NULL == multi_handle )
1730 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
1732 _("Could not initialize curl multi handle, failed to start http plugin!\n"),
1734 libgnunet_plugin_transport_http_done (api);
1738 GNUNET_OS_network_interfaces_list (&process_interfaces, plugin);
1743 /* end of plugin_transport_template.c */