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;
156 unsigned int http_result_code;
161 * Session handle for connections.
167 * Stored in a linked list.
169 struct Session *next;
172 * Pointer to the global plugin struct.
174 struct Plugin *plugin;
177 * To whom are we talking to (set to our identity
178 * if we are still waiting for the welcome message)
180 struct GNUNET_PeerIdentity sender;
183 * Sender's ip address to distinguish between incoming connections
185 struct sockaddr_in * addr_inbound;
188 * Sender's ip address recieved by transport
190 struct sockaddr_in * addr_outbound;
193 * Did we initiate the connection (GNUNET_YES) or the other peer (GNUNET_NO)?
195 unsigned int is_client;
198 * Is the connection active (GNUNET_YES) or terminated (GNUNET_NO)?
200 unsigned int is_active;
203 * At what time did we reset last_received last?
205 struct GNUNET_TIME_Absolute last_quota_update;
208 * How many bytes have we received since the "last_quota_update"
211 uint64_t last_received;
214 * Number of bytes per ms that this peer is allowed
220 * Is there a HTTP/PUT in progress?
222 unsigned int is_put_in_progress;
225 * Is there a HTTP/PUT in progress?
227 unsigned int is_bad_request;
232 struct GNUNET_CRYPTO_HashAsciiEncoded hash;
234 struct HTTP_Message * pending_outbound_msg;;
236 struct HTTP_Message * pending_inbound_msg;
242 * Encapsulation of all of the state of the plugin.
249 struct GNUNET_TRANSPORT_PluginEnvironment *env;
251 unsigned int port_inbound;
254 * List of open sessions.
256 struct Session *sessions;
259 * Number of active sessions
262 unsigned int session_count;
266 * Daemon for listening for new IPv4 connections.
268 static struct MHD_Daemon *http_daemon_v4;
271 * Daemon for listening for new IPv6connections.
273 static struct MHD_Daemon *http_daemon_v6;
276 * Our primary task for http daemon handling IPv4 connections
278 static GNUNET_SCHEDULER_TaskIdentifier http_task_v4;
281 * Our primary task for http daemon handling IPv6 connections
283 static GNUNET_SCHEDULER_TaskIdentifier http_task_v6;
287 * The task sending data
289 static GNUNET_SCHEDULER_TaskIdentifier http_task_send;
293 * Information about this plugin
295 static struct Plugin *plugin;
300 static CURLM *multi_handle;
303 * Our ASCII encoded, hashed peer identity
304 * This string is used to distinguish between connections and is added to the urls
306 static struct GNUNET_CRYPTO_HashAsciiEncoded my_ascii_hash_ident;
308 // MW: please document (which timeout is this!?)
309 static struct GNUNET_TIME_Relative timeout;
312 * Finds a http session in our linked list using peer identity as a key
313 * @param peer peeridentity
314 * @return http session corresponding to peer identity
316 static struct Session * find_session_by_pi( const struct GNUNET_PeerIdentity *peer )
318 struct Session * cur;
319 GNUNET_HashCode hc_peer;
320 GNUNET_HashCode hc_current;
322 cur = plugin->sessions;
323 hc_peer = peer->hashPubKey;
326 hc_current = cur->sender.hashPubKey;
327 if ( 0 == GNUNET_CRYPTO_hash_cmp( &hc_peer, &hc_current))
329 cur = plugin->sessions->next;
335 * Finds a http session in our linked list using libcurl handle as a key
336 * Needed when sending data with libcurl to differentiate between sessions
337 * @param handle peeridentity
338 * @return http session corresponding to peer identity
340 static struct Session * find_session_by_curlhandle( CURL* handle )
342 struct Session * cur;
344 cur = plugin->sessions;
347 if ( handle == cur->curl_handle )
349 cur = plugin->sessions->next;
355 * Create a new session
357 * @param addr_in address the peer is using inbound
358 * @param addr_out address the peer is using outbound
359 * @param peer identity
360 * @return created session object
362 static struct Session * create_session (struct sockaddr_in *addr_in, struct sockaddr_in *addr_out, const struct GNUNET_PeerIdentity *peer)
364 struct Session * ses = GNUNET_malloc ( sizeof( struct Session) );
366 ses->addr_inbound = GNUNET_malloc ( sizeof (struct sockaddr_in) );
367 ses->addr_outbound = GNUNET_malloc ( sizeof (struct sockaddr_in) );
368 ses->plugin = plugin;
369 if ((NULL != addr_in) && (( AF_INET == addr_in->sin_family) || ( AF_INET6 == addr_in->sin_family)))
371 memcpy(ses->addr_inbound, addr_in, sizeof (struct sockaddr_in));
373 if ((NULL != addr_out) && (( AF_INET == addr_out->sin_family) || ( AF_INET6 == addr_out->sin_family)))
375 memcpy(ses->addr_outbound, addr_out, sizeof (struct sockaddr_in));
377 memcpy(&ses->sender, peer, sizeof (struct GNUNET_PeerIdentity));
378 GNUNET_CRYPTO_hash_to_enc(&ses->sender.hashPubKey,&(ses->hash));
379 ses->is_active = GNUNET_NO;
380 ses->pending_inbound_msg = GNUNET_malloc( sizeof (struct HTTP_Message));
381 ses->pending_inbound_msg->buf = GNUNET_malloc(GNUNET_SERVER_MAX_MESSAGE_SIZE);
382 ses->pending_inbound_msg->len = GNUNET_SERVER_MAX_MESSAGE_SIZE;
383 ses->pending_inbound_msg->pos = 0;
388 * Callback called by MHD when a connection is terminated
390 static void requestCompletedCallback (void *cls, struct MHD_Connection * connection, void **httpSessionCache)
394 cs = *httpSessionCache;
397 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Connection from peer `%s' was terminated\n",GNUNET_i2s(&cs->sender));
398 /* session set to inactive */
399 cs->is_active = GNUNET_NO;
400 cs->is_put_in_progress = GNUNET_NO;
406 * Check if we are allowed to connect to the given IP.
409 acceptPolicyCallback (void *cls,
410 const struct sockaddr *addr, socklen_t addr_len)
412 /* Every connection is accepted, nothing more to do here */
417 * Process GET or PUT request received via MHD. For
418 * GET, queue response that will send back our pending
419 * messages. For PUT, process incoming data and send
420 * to GNUnet core. In either case, check if a session
421 * already exists and create a new one if not.
424 accessHandlerCallback (void *cls,
425 struct MHD_Connection *session,
429 const char *upload_data,
430 size_t * upload_data_size, void **httpSessionCache)
432 struct MHD_Response *response;
434 struct Session * cs_temp;
435 const union MHD_ConnectionInfo * conn_info;
436 struct sockaddr_in *addrin;
437 struct sockaddr_in6 *addrin6;
438 char address[INET6_ADDRSTRLEN+14];
439 struct GNUNET_PeerIdentity pi_in;
441 struct GNUNET_MessageHeader *gn_msg;
442 int send_error_to_client;
445 send_error_to_client = GNUNET_NO;
447 if ( NULL == *httpSessionCache)
449 /* check url for peer identity */
450 res = GNUNET_CRYPTO_hash_from_string ( &url[1], &(pi_in.hashPubKey));
451 if ( GNUNET_SYSERR == res )
453 response = MHD_create_response_from_data (strlen (HTTP_ERROR_RESPONSE),HTTP_ERROR_RESPONSE, MHD_NO, MHD_NO);
454 res = MHD_queue_response (session, MHD_HTTP_NOT_FOUND, response);
455 MHD_destroy_response (response);
457 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Peer has no valid ident, sent HTTP 1.1/404\n");
459 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Peer has no valid ident, could not send error\n");
463 conn_info = MHD_get_connection_info(session, MHD_CONNECTION_INFO_CLIENT_ADDRESS );
464 /* Incoming IPv4 connection */
465 if ( AF_INET == conn_info->client_addr->sin_family)
467 addrin = conn_info->client_addr;
468 inet_ntop(addrin->sin_family, &(addrin->sin_addr),address,INET_ADDRSTRLEN);
470 /* Incoming IPv6 connection */
471 if ( AF_INET6 == conn_info->client_addr->sin_family)
473 addrin6 = (struct sockaddr_in6 *) conn_info->client_addr;
474 inet_ntop(addrin6->sin6_family, &(addrin6->sin6_addr),address,INET6_ADDRSTRLEN);
476 /* find existing session for address */
478 if (plugin->session_count > 0)
480 cs = plugin->sessions;
484 /* Comparison based on ip address */
485 // res = (0 == memcmp(&(conn_info->client_addr->sin_addr),&(cs->addr->sin_addr), sizeof (struct in_addr))) ? GNUNET_YES : GNUNET_NO;
487 /* Comparison based on ip address, port number and address family */
488 // res = (0 == memcmp((conn_info->client_addr),(cs->addr), sizeof (struct sockaddr_in))) ? GNUNET_YES : GNUNET_NO;
490 /* Comparison based on PeerIdentity */
491 res = (0 == memcmp(&pi_in,&(cs->sender), sizeof (struct GNUNET_PeerIdentity))) ? GNUNET_YES : GNUNET_NO;
493 if ( GNUNET_YES == res)
495 /* existing session for this address found */
496 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Session for peer `%s' found\n",GNUNET_i2s(&cs->sender));
502 /* no existing session, create a new one*/
505 /* create new session object */
506 cs = create_session(conn_info->client_addr, NULL, &pi_in);
508 /* Insert session into linked list */
509 if ( plugin->sessions == NULL)
511 plugin->sessions = cs;
512 plugin->session_count = 1;
514 cs_temp = plugin->sessions;
515 while ( cs_temp->next != NULL )
517 cs_temp = cs_temp->next;
522 plugin->session_count++;
524 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"New Session `%s' inserted, count %u \n", GNUNET_i2s(&cs->sender), plugin->session_count);
528 if (*httpSessionCache == NULL)
530 *httpSessionCache = cs;
531 /* Updating session */
532 memcpy(cs->addr_inbound,conn_info->client_addr, sizeof(struct sockaddr_in));
534 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));
538 cs = *httpSessionCache;
540 /* Is it a PUT or a GET request */
541 if ( 0 == strcmp (MHD_HTTP_METHOD_PUT, method) )
544 if ((*upload_data_size == 0) && (cs->is_put_in_progress == GNUNET_NO))
546 if (cs->pending_inbound_msg->pos !=0 )
548 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
549 _("Incoming message from peer `%s', while existing message with %u bytes was not forwarded to transport'\n"),
550 GNUNET_i2s(&cs->sender), cs->pending_inbound_msg->pos);
551 cs->pending_inbound_msg->pos = 0;
554 cs->is_put_in_progress = GNUNET_YES;
555 cs->is_bad_request = GNUNET_NO;
556 cs->is_active = GNUNET_YES;
560 if ((*upload_data_size > 0) && (cs->is_bad_request != GNUNET_YES))
562 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))
564 /* copy uploaded data to buffer */
565 memcpy(&cs->pending_inbound_msg->buf[cs->pending_inbound_msg->pos],upload_data,*upload_data_size);
566 cs->pending_inbound_msg->pos += *upload_data_size;
567 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"%u bytes added to message of %u bytes\n",*upload_data_size, cs->pending_inbound_msg->pos);
568 *upload_data_size = 0;
573 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);
574 cs->is_bad_request = GNUNET_YES;
575 /* (*upload_data_size) bytes not processed */
580 if ((cs->is_put_in_progress == GNUNET_YES) && (cs->is_bad_request == GNUNET_YES))
582 *upload_data_size = 0;
583 response = MHD_create_response_from_data (strlen (HTTP_PUT_RESPONSE),HTTP_PUT_RESPONSE, MHD_NO, MHD_NO);
584 res = MHD_queue_response (session, MHD_HTTP_REQUEST_ENTITY_TOO_LARGE, response);
587 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Sent HTTP/1.1: 413 Request Entity Too Large as PUT Response\n");
588 cs->is_bad_request = GNUNET_NO;
589 cs->is_put_in_progress =GNUNET_NO;
590 cs->pending_inbound_msg->pos = 0;
592 MHD_destroy_response (response);
596 if ((*upload_data_size == 0) && (cs->is_put_in_progress == GNUNET_YES) && (cs->is_bad_request == GNUNET_NO))
598 send_error_to_client = GNUNET_YES;
599 struct GNUNET_MessageHeader * gn_msg = NULL;
600 /*check message and forward here */
603 if (cs->pending_inbound_msg->pos >= sizeof (struct GNUNET_MessageHeader))
606 gn_msg = GNUNET_malloc (cs->pending_inbound_msg->pos);
607 memcpy (gn_msg,cs->pending_inbound_msg->buf,cs->pending_inbound_msg->pos);
608 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"msg->size: %u \n",ntohs (gn_msg->size));
609 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"msg->type: %u \n",ntohs (gn_msg->type));
611 //MY VERSION: if ((ntohs(gn_msg->size) == cs->pending_inbound_msg->pos))
612 if ((ntohs(gn_msg->size) <= cs->pending_inbound_msg->pos))
614 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));
615 /* forwarding message to transport */
618 if ( AF_INET == cs->addr_inbound->sin_family)
620 tmp = GNUNET_malloc (INET_ADDRSTRLEN + 14);
621 inet_ntop(AF_INET, &(cs->addr_inbound)->sin_addr,address,INET_ADDRSTRLEN);
622 GNUNET_asprintf(&tmp,"%s:%u",address,ntohs(cs->addr_inbound->sin_port));
624 /* Incoming IPv6 connection */
625 if ( AF_INET6 == cs->addr_inbound->sin_family)
627 tmp = GNUNET_malloc (INET6_ADDRSTRLEN + 14);
628 inet_ntop(AF_INET6, &((struct sockaddr_in6 *) cs->addr_inbound)->sin6_addr,address,INET6_ADDRSTRLEN);
629 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));
635 GNUNET_free_non_null(tmp);
637 send_error_to_client = GNUNET_NO;
641 if (send_error_to_client == GNUNET_NO)
643 response = MHD_create_response_from_data (strlen (HTTP_PUT_RESPONSE),HTTP_PUT_RESPONSE, MHD_NO, MHD_NO);
644 res = MHD_queue_response (session, MHD_HTTP_OK, response);
645 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Sent HTTP/1.1: 200 OK as PUT Response\n",HTTP_PUT_RESPONSE, strlen (HTTP_PUT_RESPONSE), res );
646 MHD_destroy_response (response);
650 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Recieved malformed message with %u bytes\n", cs->pending_inbound_msg->pos);
651 response = MHD_create_response_from_data (strlen (HTTP_PUT_RESPONSE),HTTP_PUT_RESPONSE, MHD_NO, MHD_NO);
652 res = MHD_queue_response (session, MHD_HTTP_BAD_REQUEST, response);
653 MHD_destroy_response (response);
654 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Sent HTTP/1.1: 400 BAD REQUEST as PUT Response\n");
657 GNUNET_free_non_null (gn_msg);
658 cs->is_put_in_progress = GNUNET_NO;
659 cs->is_bad_request = GNUNET_NO;
660 cs->pending_inbound_msg->pos = 0;
664 if ( 0 == strcmp (MHD_HTTP_METHOD_GET, method) )
666 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Got GET Request\n");
667 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"URL: `%s'\n",url);
668 response = MHD_create_response_from_data (strlen (HTTP_PUT_RESPONSE),HTTP_PUT_RESPONSE, MHD_NO, MHD_NO);
669 res = MHD_queue_response (session, MHD_HTTP_OK, response);
670 MHD_destroy_response (response);
678 * Call MHD to process pending requests and then go back
679 * and schedule the next run.
681 static void http_daemon_run (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
684 * Function that queries MHD's select sets and
685 * starts the task waiting for them.
687 static GNUNET_SCHEDULER_TaskIdentifier
688 http_daemon_prepare (struct MHD_Daemon *daemon_handle)
690 GNUNET_SCHEDULER_TaskIdentifier ret;
694 struct GNUNET_NETWORK_FDSet *wrs;
695 struct GNUNET_NETWORK_FDSet *wws;
696 struct GNUNET_NETWORK_FDSet *wes;
698 unsigned long long timeout;
700 struct GNUNET_TIME_Relative tv;
705 wrs = GNUNET_NETWORK_fdset_create ();
706 wes = GNUNET_NETWORK_fdset_create ();
707 wws = GNUNET_NETWORK_fdset_create ();
709 GNUNET_assert (MHD_YES ==
710 MHD_get_fdset (daemon_handle,
715 haveto = MHD_get_timeout (daemon_handle, &timeout);
716 if (haveto == MHD_YES)
717 tv.value = (uint64_t) timeout;
719 tv = GNUNET_TIME_UNIT_FOREVER_REL;
720 GNUNET_NETWORK_fdset_copy_native (wrs, &rs, max);
721 GNUNET_NETWORK_fdset_copy_native (wws, &ws, max);
722 GNUNET_NETWORK_fdset_copy_native (wes, &es, max);
723 ret = GNUNET_SCHEDULER_add_select (plugin->env->sched,
724 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
725 GNUNET_SCHEDULER_NO_TASK,
731 GNUNET_NETWORK_fdset_destroy (wrs);
732 GNUNET_NETWORK_fdset_destroy (wws);
733 GNUNET_NETWORK_fdset_destroy (wes);
738 * Call MHD to process pending requests and then go back
739 * and schedule the next run.
741 static void http_daemon_run (void *cls,
742 const struct GNUNET_SCHEDULER_TaskContext *tc)
744 struct MHD_Daemon *daemon_handle = cls;
746 if (daemon_handle == http_daemon_v4)
747 http_task_v4 = GNUNET_SCHEDULER_NO_TASK;
749 if (daemon_handle == http_daemon_v6)
750 http_task_v6 = GNUNET_SCHEDULER_NO_TASK;
752 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
755 GNUNET_assert (MHD_YES == MHD_run (daemon_handle));
756 if (daemon_handle == http_daemon_v4)
757 http_task_v4 = http_daemon_prepare (daemon_handle);
758 if (daemon_handle == http_daemon_v6)
759 http_task_v6 = http_daemon_prepare (daemon_handle);
764 * Removes a message from the linked list of messages
765 * @param ses session to remove message from
766 * @param msg message to remove
767 * @return GNUNET_SYSERR if msg not found, GNUNET_OK on success
770 static int remove_http_message(struct Session * ses, struct HTTP_Message * msg)
772 struct HTTP_Message * cur;
773 struct HTTP_Message * next;
775 cur = ses->pending_outbound_msg;
779 return GNUNET_SYSERR;
783 ses->pending_outbound_msg = cur->next;
784 GNUNET_free (cur->buf);
785 GNUNET_free (cur->dest_url);
791 while (cur->next!=msg)
793 if (cur->next != NULL)
796 return GNUNET_SYSERR;
799 cur->next = cur->next->next;
800 GNUNET_free (cur->next->buf);
801 GNUNET_free (cur->next->dest_url);
802 GNUNET_free (cur->next);
808 static size_t header_function( void *ptr, size_t size, size_t nmemb, void *stream)
811 size_t len = size * nmemb;
812 struct Session * ses = stream;
815 if ((size * nmemb) < SIZE_MAX)
816 tmp = GNUNET_malloc (len+1);
818 if ((tmp != NULL) && (len > 0))
823 if (tmp[len-2] == 13)
827 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Header: `%s'\n",tmp);
829 if (0==strcmp (tmp,"HTTP/1.1 100 Continue"))
831 ses->pending_outbound_msg->http_result_code=100;
833 if (0==strcmp (tmp,"HTTP/1.1 200 OK"))
835 ses->pending_outbound_msg->http_result_code=200;
837 if (0==strcmp (tmp,"HTTP/1.1 400 Bad Request"))
839 ses->pending_outbound_msg->http_result_code=400;
841 if (0==strcmp (tmp,"HTTP/1.1 404 Not Found"))
843 ses->pending_outbound_msg->http_result_code=404;
845 if (0==strcmp (tmp,"HTTP/1.1 413 Request Entity Too Large"))
847 ses->pending_outbound_msg->http_result_code=413;
857 * Callback method used with libcurl
858 * Method is called when libcurl needs to read data during sending
859 * @param stream pointer where to write data
860 * @param size size of an individual element
861 * @param nmemb count of elements that can be written to the buffer
862 * @param ptr source pointer, passed to the libcurl handle
863 * @return bytes written to stream
865 static size_t send_read_callback(void *stream, size_t size, size_t nmemb, void *ptr)
867 struct Session * ses = ptr;
868 struct HTTP_Message * msg = ses->pending_outbound_msg;
873 if (( msg->pos < msg->len))
875 /* data fit in buffer */
876 if ((msg->len - msg->pos) <= (size * nmemb))
878 len = (msg->len - msg->pos);
879 memcpy(stream, &msg->buf[msg->pos], len);
886 memcpy(stream, &msg->buf[msg->pos], len);
891 /* no data to send */
900 * Callback method used with libcurl
901 * Method is called when libcurl needs to write data during sending
902 * @param stream pointer where to write data
903 * @param size size of an individual element
904 * @param nmemb count of elements that can be written to the buffer
905 * @param ptr destination pointer, passed to the libcurl handle
906 * @return bytes read from stream
908 static size_t send_write_callback( void *stream, size_t size, size_t nmemb, void *ptr)
912 if ((size * nmemb) < SIZE_MAX)
913 data = GNUNET_malloc(size*nmemb +1);
916 memcpy( data, stream, size*nmemb);
917 data[size*nmemb] = '\0';
920 return (size * nmemb);
925 * Function setting up file descriptors and scheduling task to run
926 * @param ses session to send data to
927 * @return bytes sent to peer
929 static size_t send_prepare(struct Session* ses );
932 * Function setting up curl handle and selecting message to send
933 * @param ses session to send data to
934 * @return bytes sent to peer
936 static ssize_t send_select_init (struct Session* ses )
940 struct HTTP_Message * msg;
942 if ( NULL == ses->curl_handle)
943 ses->curl_handle = curl_easy_init();
944 if( NULL == ses->curl_handle)
946 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Getting cURL handle failed\n");
949 msg = ses->pending_outbound_msg;
954 curl_easy_setopt(ses->curl_handle, CURLOPT_VERBOSE, 1L);
956 curl_easy_setopt(ses->curl_handle, CURLOPT_URL, msg->dest_url);
957 curl_easy_setopt(ses->curl_handle, CURLOPT_PUT, 1L);
958 curl_easy_setopt(ses->curl_handle, CURLOPT_HEADERFUNCTION, &header_function);
959 curl_easy_setopt(ses->curl_handle, CURLOPT_WRITEHEADER, ses);
960 curl_easy_setopt(ses->curl_handle, CURLOPT_READFUNCTION, send_read_callback);
961 curl_easy_setopt(ses->curl_handle, CURLOPT_READDATA, ses);
962 curl_easy_setopt(ses->curl_handle, CURLOPT_WRITEFUNCTION, send_write_callback);
963 curl_easy_setopt(ses->curl_handle, CURLOPT_READDATA, ses);
964 curl_easy_setopt(ses->curl_handle, CURLOPT_INFILESIZE_LARGE, (curl_off_t) msg->len);
965 curl_easy_setopt(ses->curl_handle, CURLOPT_TIMEOUT, (long) (timeout.value / 1000 ));
966 curl_easy_setopt(ses->curl_handle, CURLOPT_CONNECTTIMEOUT, HTTP_CONNECT_TIMEOUT);
967 curl_easy_setopt(ses->curl_handle, CURLOPT_BUFFERSIZE, GNUNET_SERVER_MAX_MESSAGE_SIZE);
969 mret = curl_multi_add_handle(multi_handle, ses->curl_handle);
970 if (mret != CURLM_OK)
972 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
973 _("%s failed at %s:%d: `%s'\n"),
974 "curl_multi_add_handle", __FILE__, __LINE__,
975 curl_multi_strerror (mret));
978 bytes_sent = send_prepare (ses );
982 static void send_execute (void *cls,
983 const struct GNUNET_SCHEDULER_TaskContext *tc)
985 static unsigned int handles_last_run;
989 struct Session * cs = NULL;
991 http_task_send = GNUNET_SCHEDULER_NO_TASK;
992 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
998 mret = curl_multi_perform (multi_handle, &running);
999 if (running < handles_last_run)
1004 msg = curl_multi_info_read (multi_handle, &running);
1005 GNUNET_break (msg != NULL);
1008 /* get session for affected curl handle */
1009 GNUNET_assert ( msg->easy_handle != NULL );
1010 cs = find_session_by_curlhandle (msg->easy_handle);
1011 GNUNET_assert ( cs != NULL );
1012 GNUNET_assert ( cs->pending_outbound_msg != NULL );
1017 if ( (msg->data.result != CURLE_OK) &&
1018 (msg->data.result != CURLE_GOT_NOTHING) )
1021 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1022 _("%s failed for `%s' at %s:%d: `%s'\n"),
1023 "curl_multi_perform",
1024 GNUNET_i2s(&cs->sender),
1027 curl_easy_strerror (msg->data.result));
1028 /* sending msg failed*/
1029 if (( NULL != cs->pending_outbound_msg) && ( NULL != cs->pending_outbound_msg->transmit_cont))
1030 cs->pending_outbound_msg->transmit_cont (cs->pending_outbound_msg->transmit_cont_cls,&cs->sender,GNUNET_SYSERR);
1035 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1036 "Send to peer `%s' completed with code %u\n", GNUNET_i2s(&cs->sender),cs->pending_outbound_msg->http_result_code);
1038 curl_easy_cleanup(cs->curl_handle);
1039 cs->curl_handle=NULL;
1041 /* Calling transmit continuation */
1042 if (( NULL != cs->pending_outbound_msg) && (NULL != cs->pending_outbound_msg->transmit_cont))
1044 /* HTTP 1xx : Last message before here was informational */
1045 if ((cs->pending_outbound_msg->http_result_code >=100) && (cs->pending_outbound_msg->http_result_code < 200))
1046 cs->pending_outbound_msg->transmit_cont (cs->pending_outbound_msg->transmit_cont_cls,&cs->sender,GNUNET_OK);
1047 /* HTTP 2xx: successful operations */
1048 if ((cs->pending_outbound_msg->http_result_code >=200) && (cs->pending_outbound_msg->http_result_code < 300))
1049 cs->pending_outbound_msg->transmit_cont (cs->pending_outbound_msg->transmit_cont_cls,&cs->sender,GNUNET_OK);
1050 /* HTTP 3xx..5xx: error */
1051 if ((cs->pending_outbound_msg->http_result_code >=300) && (cs->pending_outbound_msg->http_result_code < 600))
1052 cs->pending_outbound_msg->transmit_cont (cs->pending_outbound_msg->transmit_cont_cls,&cs->sender,GNUNET_SYSERR);
1054 if (GNUNET_OK != remove_http_message(cs, cs->pending_outbound_msg))
1055 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Message could not be removed from session `%s'", GNUNET_i2s(&cs->sender));
1057 /* send pending messages */
1058 if (cs->pending_outbound_msg != NULL)
1060 send_select_init (cs);
1069 while ( (running > 0) );
1071 handles_last_run = running;
1073 while (mret == CURLM_CALL_MULTI_PERFORM);
1079 * Function setting up file descriptors and scheduling task to run
1080 * @param ses session to send data to
1081 * @return bytes sent to peer
1083 static size_t send_prepare(struct Session* ses )
1089 struct GNUNET_NETWORK_FDSet *grs;
1090 struct GNUNET_NETWORK_FDSet *gws;
1098 mret = curl_multi_fdset (multi_handle, &rs, &ws, &es, &max);
1099 if (mret != CURLM_OK)
1101 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1102 _("%s failed at %s:%d: `%s'\n"),
1103 "curl_multi_fdset", __FILE__, __LINE__,
1104 curl_multi_strerror (mret));
1107 mret = curl_multi_timeout (multi_handle, &to);
1108 if (mret != CURLM_OK)
1110 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1111 _("%s failed at %s:%d: `%s'\n"),
1112 "curl_multi_timeout", __FILE__, __LINE__,
1113 curl_multi_strerror (mret));
1117 grs = GNUNET_NETWORK_fdset_create ();
1118 gws = GNUNET_NETWORK_fdset_create ();
1119 GNUNET_NETWORK_fdset_copy_native (grs, &rs, max + 1);
1120 GNUNET_NETWORK_fdset_copy_native (gws, &ws, max + 1);
1121 http_task_send = GNUNET_SCHEDULER_add_select (plugin->env->sched,
1122 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1123 GNUNET_SCHEDULER_NO_TASK,
1124 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 0),
1129 GNUNET_NETWORK_fdset_destroy (gws);
1130 GNUNET_NETWORK_fdset_destroy (grs);
1132 /* FIXME: return bytes REALLY sent */
1137 * Function that can be used by the transport service to transmit
1138 * a message using the plugin.
1140 * @param cls closure
1141 * @param target who should receive this message
1142 * @param priority how important is the message
1143 * @param msgbuf the message to transmit
1144 * @param msgbuf_size number of bytes in 'msgbuf'
1145 * @param to when should we time out
1146 * @param session which session must be used (or NULL for "any")
1147 * @param addr the address to use (can be NULL if the plugin
1148 * is "on its own" (i.e. re-use existing TCP connection))
1149 * @param addrlen length of the address in bytes
1150 * @param force_address GNUNET_YES if the plugin MUST use the given address,
1151 * otherwise the plugin may use other addresses or
1152 * existing connections (if available)
1153 * @param cont continuation to call once the message has
1154 * been transmitted (or if the transport is ready
1155 * for the next transmission call; or if the
1156 * peer disconnected...)
1157 * @param cont_cls closure for cont
1158 * @return number of bytes used (on the physical network, with overheads);
1159 * -1 on hard errors (i.e. address invalid); 0 is a legal value
1160 * and does NOT mean that the message was not transmitted (DV)
1163 http_plugin_send (void *cls,
1164 const struct GNUNET_PeerIdentity *target,
1167 unsigned int priority,
1168 struct GNUNET_TIME_Relative to,
1169 struct Session *session,
1173 GNUNET_TRANSPORT_TransmitContinuation cont,
1178 struct Session* ses;
1179 struct Session* ses_temp;
1180 struct HTTP_Message * msg;
1181 struct HTTP_Message * tmp;
1186 /* find session for peer */
1187 ses = find_session_by_pi (target);
1189 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1190 "Existing session for peer `%s' found\n", GNUNET_i2s(target));
1193 /* create new session object */
1195 ses = create_session(NULL, (struct sockaddr_in *) addr, target);
1196 ses->is_active = GNUNET_YES;
1198 /* Insert session into linked list */
1199 if ( plugin->sessions == NULL)
1201 plugin->sessions = ses;
1202 plugin->session_count = 1;
1204 ses_temp = plugin->sessions;
1205 while ( ses_temp->next != NULL )
1207 ses_temp = ses_temp->next;
1209 if (ses_temp != ses )
1211 ses_temp->next = ses;
1212 plugin->session_count++;
1214 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1215 "New Session `%s' inserted, count %u\n", GNUNET_i2s(target), plugin->session_count);
1218 GNUNET_assert (addr!=NULL);
1221 /* setting url to send to */
1222 if (force_address == GNUNET_YES)
1224 if (addrlen == (sizeof (struct IPv4HttpAddress)))
1226 address = GNUNET_malloc(INET_ADDRSTRLEN + 1);
1227 inet_ntop(AF_INET, &((struct IPv4HttpAddress *) addr)->ipv4_addr,address,INET_ADDRSTRLEN);
1228 port = ntohs(((struct IPv4HttpAddress *) addr)->u_port);
1229 GNUNET_asprintf (&url,
1233 (char *) (&my_ascii_hash_ident));
1234 GNUNET_free(address);
1236 else if (addrlen == (sizeof (struct IPv6HttpAddress)))
1238 address = GNUNET_malloc(INET6_ADDRSTRLEN + 1);
1239 inet_ntop(AF_INET6, &((struct IPv6HttpAddress *) addr)->ipv6_addr,address,INET6_ADDRSTRLEN);
1240 port = ntohs(((struct IPv6HttpAddress *) addr)->u6_port);
1241 GNUNET_asprintf(&url,
1243 address,port,(char *) (&my_ascii_hash_ident));
1244 GNUNET_free(address);
1248 /* setting up message */
1249 msg = GNUNET_malloc (sizeof (struct HTTP_Message));
1251 msg->len = msgbuf_size;
1253 msg->buf = GNUNET_malloc (msgbuf_size);
1254 msg->dest_url = url;
1255 msg->transmit_cont = cont;
1256 msg->transmit_cont_cls = cont_cls;
1257 memcpy (msg->buf,msgbuf, msgbuf_size);
1259 /* insert created message in list of pending messages */
1260 if (ses->pending_outbound_msg == NULL)
1262 ses->pending_outbound_msg = msg;
1264 tmp = ses->pending_outbound_msg;
1265 while ( NULL != tmp->next)
1274 if (msg == ses->pending_outbound_msg)
1276 bytes_sent = send_select_init (ses);
1285 * Function that can be used to force the plugin to disconnect
1286 * from the given peer and cancel all previous transmissions
1287 * (and their continuationc).
1289 * @param cls closure
1290 * @param target peer from which to disconnect
1293 http_plugin_disconnect (void *cls,
1294 const struct GNUNET_PeerIdentity *target)
1296 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"HTTP Plugin: http_plugin_disconnect\n");
1297 // struct Plugin *plugin = cls;
1303 * Convert the transports address to a nice, human-readable
1306 * @param cls closure
1307 * @param type name of the transport that generated the address
1308 * @param addr one of the addresses of the host, NULL for the last address
1309 * the specific address format depends on the transport
1310 * @param addrlen length of the address
1311 * @param numeric should (IP) addresses be displayed in numeric form?
1312 * @param timeout after how long should we give up?
1313 * @param asc function to call on each string
1314 * @param asc_cls closure for asc
1317 http_plugin_address_pretty_printer (void *cls,
1322 struct GNUNET_TIME_Relative timeout,
1323 GNUNET_TRANSPORT_AddressStringCallback
1326 const struct IPv4HttpAddress *t4;
1327 const struct IPv6HttpAddress *t6;
1328 struct sockaddr_in a4;
1329 struct sockaddr_in6 a6;
1335 if (addrlen == sizeof (struct IPv6HttpAddress))
1337 address = GNUNET_malloc (INET6_ADDRSTRLEN);
1339 a6.sin6_addr = t6->ipv6_addr;
1340 inet_ntop(AF_INET6, &(a6.sin6_addr),address,INET6_ADDRSTRLEN);
1341 port = ntohs(t6->u6_port);
1343 else if (addrlen == sizeof (struct IPv4HttpAddress))
1345 address = GNUNET_malloc (INET_ADDRSTRLEN);
1347 a4.sin_addr.s_addr = t4->ipv4_addr;
1348 inet_ntop(AF_INET, &(a4.sin_addr),address,INET_ADDRSTRLEN);
1349 port = ntohs(t4->u_port);
1353 /* invalid address */
1354 GNUNET_break_op (0);
1355 asc (asc_cls, NULL);
1358 res = GNUNET_asprintf(&ret,"http://%s:%u/",address,port);
1359 GNUNET_free (address);
1360 GNUNET_assert(res != 0);
1368 * Another peer has suggested an address for this
1369 * peer and transport plugin. Check that this could be a valid
1370 * address. If so, consider adding it to the list
1373 * @param cls closure
1374 * @param addr pointer to the address
1375 * @param addrlen length of addr
1376 * @return GNUNET_OK if this is a plausible address for this peer
1380 http_plugin_address_suggested (void *cls,
1381 void *addr, size_t addrlen)
1383 struct IPv4HttpAddress *v4;
1384 struct IPv6HttpAddress *v6;
1387 if ((addrlen != sizeof (struct IPv4HttpAddress)) &&
1388 (addrlen != sizeof (struct IPv6HttpAddress)))
1390 return GNUNET_SYSERR;
1392 if (addrlen == sizeof (struct IPv4HttpAddress))
1394 v4 = (struct IPv4HttpAddress *) addr;
1395 if (INADDR_LOOPBACK == ntohl(v4->ipv4_addr))
1397 return GNUNET_SYSERR;
1399 port = ntohs (v4->u_port);
1400 if (port != plugin->port_inbound)
1402 return GNUNET_SYSERR;
1407 v6 = (struct IPv6HttpAddress *) addr;
1408 if (IN6_IS_ADDR_LINKLOCAL (&v6->ipv6_addr))
1410 return GNUNET_SYSERR;
1412 port = ntohs (v6->u6_port);
1413 if (port != plugin->port_inbound)
1415 return GNUNET_SYSERR;
1425 * Function called for a quick conversion of the binary address to
1426 * a numeric address. Note that the caller must not free the
1427 * address and that the next call to this function is allowed
1428 * to override the address again.
1430 * @param cls closure
1431 * @param addr binary address
1432 * @param addrlen length of the address
1433 * @return string representing the same address
1436 http_plugin_address_to_string (void *cls,
1440 const struct IPv4HttpAddress *t4;
1441 const struct IPv6HttpAddress *t6;
1442 struct sockaddr_in a4;
1443 struct sockaddr_in6 a6;
1449 if (addrlen == sizeof (struct IPv6HttpAddress))
1451 address = GNUNET_malloc (INET6_ADDRSTRLEN);
1453 a6.sin6_addr = t6->ipv6_addr;
1454 inet_ntop(AF_INET6, &(a6.sin6_addr),address,INET6_ADDRSTRLEN);
1455 port = ntohs(t6->u6_port);
1457 else if (addrlen == sizeof (struct IPv4HttpAddress))
1459 address = GNUNET_malloc (INET_ADDRSTRLEN);
1461 a4.sin_addr.s_addr = t4->ipv4_addr;
1462 inet_ntop(AF_INET, &(a4.sin_addr),address,INET_ADDRSTRLEN);
1463 port = ntohs(t4->u_port);
1467 /* invalid address */
1470 res = GNUNET_asprintf(&ret,"%s:%u",address,port);
1471 GNUNET_free (address);
1472 GNUNET_assert(res != 0);
1477 * Add the IP of our network interface to the list of
1478 * our external IP addresses.
1480 * @param cls the 'struct Plugin*'
1481 * @param name name of the interface
1482 * @param isDefault do we think this may be our default interface
1483 * @param addr address of the interface
1484 * @param addrlen number of bytes in addr
1485 * @return GNUNET_OK to continue iterating
1488 process_interfaces (void *cls,
1491 const struct sockaddr *addr, socklen_t addrlen)
1493 struct IPv4HttpAddress t4;
1494 struct IPv6HttpAddress t6;
1499 af = addr->sa_family;
1502 if (INADDR_LOOPBACK == ntohl(((struct sockaddr_in *) addr)->sin_addr.s_addr))
1504 /* skip loopback addresses */
1507 t4.ipv4_addr = ((struct sockaddr_in *) addr)->sin_addr.s_addr;
1508 t4.u_port = htons (plugin->port_inbound);
1512 else if (af == AF_INET6)
1514 if (IN6_IS_ADDR_LINKLOCAL (&((struct sockaddr_in6 *) addr)->sin6_addr))
1516 /* skip link local addresses */
1519 if (IN6_IS_ADDR_LOOPBACK (&((struct sockaddr_in6 *) addr)->sin6_addr))
1521 /* skip loopback addresses */
1524 memcpy (&t6.ipv6_addr,
1525 &((struct sockaddr_in6 *) addr)->sin6_addr,
1526 sizeof (struct in6_addr));
1527 t6.u6_port = htons (plugin->port_inbound);
1536 plugin->env->notify_address(plugin->env->cls,"http",arg, args, GNUNET_TIME_UNIT_FOREVER_REL);
1541 * Exit point from the plugin.
1544 libgnunet_plugin_transport_http_done (void *cls)
1546 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
1547 struct Plugin *plugin = api->cls;
1548 struct Session * cs;
1549 struct Session * cs_next;
1552 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Unloading http plugin...\n");
1554 if ( http_task_v4 != GNUNET_SCHEDULER_NO_TASK)
1556 GNUNET_SCHEDULER_cancel(plugin->env->sched, http_task_v4);
1557 http_task_v4 = GNUNET_SCHEDULER_NO_TASK;
1560 if ( http_task_v6 != GNUNET_SCHEDULER_NO_TASK)
1562 GNUNET_SCHEDULER_cancel(plugin->env->sched, http_task_v6);
1563 http_task_v6 = GNUNET_SCHEDULER_NO_TASK;
1566 if ( http_task_send != GNUNET_SCHEDULER_NO_TASK)
1568 GNUNET_SCHEDULER_cancel(plugin->env->sched, http_task_send);
1569 http_task_send = GNUNET_SCHEDULER_NO_TASK;
1572 if (http_daemon_v4 != NULL)
1574 MHD_stop_daemon (http_daemon_v4);
1575 http_daemon_v4 = NULL;
1577 if (http_daemon_v6 != NULL)
1579 MHD_stop_daemon (http_daemon_v6);
1580 http_daemon_v6 = NULL;
1583 /* free all sessions */
1584 cs = plugin->sessions;
1588 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Freeing session for peer `%s'\n",GNUNET_i2s(&cs->sender));
1592 /* freeing messages */
1593 struct HTTP_Message *cur;
1594 struct HTTP_Message *tmp;
1595 cur = cs->pending_outbound_msg;
1600 if (NULL != cur->buf)
1601 GNUNET_free (cur->buf);
1605 GNUNET_free (cs->pending_inbound_msg->buf);
1606 GNUNET_free (cs->pending_inbound_msg);
1607 GNUNET_free_non_null (cs->addr_inbound);
1608 GNUNET_free_non_null (cs->addr_outbound);
1611 plugin->session_count--;
1615 mret = curl_multi_cleanup(multi_handle);
1616 if ( CURLM_OK != mret)
1617 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"curl multihandle clean up failed");
1619 GNUNET_free (plugin);
1626 * Entry point for the plugin.
1629 libgnunet_plugin_transport_http_init (void *cls)
1631 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
1632 struct GNUNET_TRANSPORT_PluginFunctions *api;
1633 unsigned int timeout;
1634 struct GNUNET_TIME_Relative gn_timeout;
1635 long long unsigned int port;
1637 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Starting http plugin...\n");
1638 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"size_t %u\n",sizeof(size_t));
1639 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"uint64_t %u\n",sizeof(uint64_t));
1641 plugin = GNUNET_malloc (sizeof (struct Plugin));
1643 plugin->sessions = NULL;
1644 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
1646 api->send = &http_plugin_send;
1647 api->disconnect = &http_plugin_disconnect;
1648 api->address_pretty_printer = &http_plugin_address_pretty_printer;
1649 api->check_address = &http_plugin_address_suggested;
1650 api->address_to_string = &http_plugin_address_to_string;
1652 /* Hashing our identity to use it in URLs */
1653 GNUNET_CRYPTO_hash_to_enc ( &(plugin->env->my_identity->hashPubKey), &my_ascii_hash_ident);
1655 /* Reading port number from config file */
1657 GNUNET_CONFIGURATION_get_value_number (env->cfg,
1663 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
1666 ("Require valid port number for transport plugin `%s' in configuration!\n"),
1668 libgnunet_plugin_transport_http_done (api);
1671 GNUNET_assert ((port > 0) && (port <= 65535));
1672 plugin->port_inbound = port;
1673 gn_timeout = GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT;
1674 timeout = ( gn_timeout.value / 1000);
1675 if ((http_daemon_v4 == NULL) && (http_daemon_v6 == NULL) && (port != 0))
1677 http_daemon_v6 = MHD_start_daemon (MHD_USE_IPv6,
1679 &acceptPolicyCallback,
1680 NULL , &accessHandlerCallback, NULL,
1681 MHD_OPTION_CONNECTION_LIMIT, (unsigned int) 16,
1682 MHD_OPTION_PER_IP_CONNECTION_LIMIT, (unsigned int) 1,
1683 MHD_OPTION_CONNECTION_TIMEOUT, timeout,
1684 /* FIXME: set correct limit */
1685 MHD_OPTION_CONNECTION_MEMORY_LIMIT, (size_t) (16 * 1024),
1686 MHD_OPTION_NOTIFY_COMPLETED, &requestCompletedCallback, NULL,
1688 http_daemon_v4 = MHD_start_daemon (MHD_NO_FLAG,
1690 &acceptPolicyCallback,
1691 NULL , &accessHandlerCallback, NULL,
1692 MHD_OPTION_CONNECTION_LIMIT, (unsigned int) 16,
1693 MHD_OPTION_PER_IP_CONNECTION_LIMIT, (unsigned int) 1,
1694 MHD_OPTION_CONNECTION_TIMEOUT, timeout,
1695 /* FIXME: set correct limit */
1696 MHD_OPTION_CONNECTION_MEMORY_LIMIT, (size_t) (16 * 1024),
1697 MHD_OPTION_NOTIFY_COMPLETED, &requestCompletedCallback, NULL,
1700 if (http_daemon_v4 != NULL)
1701 http_task_v4 = http_daemon_prepare (http_daemon_v4);
1702 if (http_daemon_v6 != NULL)
1703 http_task_v6 = http_daemon_prepare (http_daemon_v6);
1705 if (http_task_v4 != GNUNET_SCHEDULER_NO_TASK)
1706 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Starting MHD with IPv4 on port %u\n",port);
1707 else if (http_task_v6 != GNUNET_SCHEDULER_NO_TASK)
1708 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Starting MHD with IPv4 and IPv6 on port %u\n",port);
1711 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"No MHD was started, transport plugin not functional!\n");
1712 libgnunet_plugin_transport_http_done (api);
1716 /* Initializing cURL */
1717 multi_handle = curl_multi_init();
1718 if ( NULL == multi_handle )
1720 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
1722 _("Could not initialize curl multi handle, failed to start http plugin!\n"),
1724 libgnunet_plugin_transport_http_done (api);
1728 GNUNET_OS_network_interfaces_list (&process_interfaces, plugin);
1733 /* end of plugin_transport_template.c */