2 This file is part of GNUnet
3 (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 2, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file transport/plugin_transport_http.c
23 * @brief Implementation of the HTTP transport service
24 * @author Matthias Wachs
28 #include "gnunet_protocols.h"
29 #include "gnunet_connection_lib.h"
30 #include "gnunet_server_lib.h"
31 #include "gnunet_service_lib.h"
32 #include "gnunet_statistics_service.h"
33 #include "gnunet_transport_service.h"
34 #include "plugin_transport.h"
35 #include "microhttpd.h"
36 #include <curl/curl.h>
38 #define VERBOSE GNUNET_YES
39 #define DEBUG GNUNET_YES
42 * After how long do we expire an address that we
43 * learned from another peer if it is not reconfirmed
46 #define LEARNED_ADDRESS_EXPIRATION GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_HOURS, 6)
48 #define HTTP_TIMEOUT 600
50 #define CURL_EASY_SETOPT(c, a, b) do { ret = curl_easy_setopt(c, a, b); if (ret != CURLE_OK) GNUNET_log(GNUNET_ERROR_TYPE_WARNING, _("%s failed at %s:%d: `%s'\n"), "curl_easy_setopt", __FILE__, __LINE__, curl_easy_strerror(ret)); } while (0);
53 * Text of the response sent back after the last bytes of a PUT
54 * request have been received (just to formally obey the HTTP
57 #define HTTP_PUT_RESPONSE "Thank you!"
60 * Encapsulation of all of the state of the plugin.
66 * Session handle for connections.
72 * Stored in a linked list.
77 * Pointer to the global plugin struct.
79 struct Plugin *plugin;
82 * The client (used to identify this connection)
87 * Continuation function to call once the transmission buffer
88 * has again space available. NULL if there is no
89 * continuation to call.
91 GNUNET_TRANSPORT_TransmitContinuation transmit_cont;
94 * Closure for transmit_cont.
96 void *transmit_cont_cls;
99 * To whom are we talking to (set to our identity
100 * if we are still waiting for the welcome message)
102 struct GNUNET_PeerIdentity sender;
105 * At what time did we reset last_received last?
107 struct GNUNET_TIME_Absolute last_quota_update;
110 * How many bytes have we received since the "last_quota_update"
113 uint64_t last_received;
116 * Number of bytes per ms that this peer is allowed
124 * Encapsulation of all of the state of the plugin.
131 struct GNUNET_TRANSPORT_PluginEnvironment *env;
134 * Handle to the network service.
136 struct GNUNET_SERVICE_Context *service;
139 * List of open sessions.
141 struct Session *sessions;
144 static struct Plugin *plugin;
147 * Daemon for listening for new IPv4 connections.
149 static struct MHD_Daemon *http_daemon_v4;
152 * Daemon for listening for new IPv6connections.
154 static struct MHD_Daemon *http_daemon_v6;
157 * Our primary task for http daemon handling IPv4 connections
159 static GNUNET_SCHEDULER_TaskIdentifier http_task_v4;
162 * Our primary task for http daemon handling IPv6 connections
164 static GNUNET_SCHEDULER_TaskIdentifier http_task_v6;
166 static char * hd_src ;
170 * Curl multi for managing client operations.
172 static CURLM *curl_multi;
174 static char * get_url( const struct GNUNET_PeerIdentity * target)
176 return strdup("http://localhost:12389");
179 static size_t curl_read_function( void *ptr, size_t size, size_t nmemb, void *stream)
181 // strcpy ("Testmessa")
186 * Function that can be used by the transport service to transmit
187 * a message using the plugin.
190 * @param target who should receive this message
191 * @param priority how important is the message
192 * @param msgbuf the message to transmit
193 * @param msgbuf_size number of bytes in 'msgbuf'
194 * @param timeout when should we time out
195 * @param session which session must be used (or NULL for "any")
196 * @param addr the address to use (can be NULL if the plugin
197 * is "on its own" (i.e. re-use existing TCP connection))
198 * @param addrlen length of the address in bytes
199 * @param force_address GNUNET_YES if the plugin MUST use the given address,
200 * otherwise the plugin may use other addresses or
201 * existing connections (if available)
202 * @param cont continuation to call once the message has
203 * been transmitted (or if the transport is ready
204 * for the next transmission call; or if the
205 * peer disconnected...)
206 * @param cont_cls closure for cont
207 * @return number of bytes used (on the physical network, with overheads);
208 * -1 on hard errors (i.e. address invalid); 0 is a legal value
209 * and does NOT mean that the message was not transmitted (DV)
212 http_plugin_send (void *cls,
213 const struct GNUNET_PeerIdentity * target,
216 unsigned int priority,
217 struct GNUNET_TIME_Relative timeout,
218 struct Session *session,
222 GNUNET_TRANSPORT_TransmitContinuation cont,
225 char * peer_url = get_url( target );
230 /* struct Plugin *plugin = cls; */
232 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Sending %u bytes (`%s') to `%s'\n",msgbuf_size, msgbuf,GNUNET_i2s(target));
233 /* Insert code to send using cURL */
234 curl = curl_easy_init ();
236 CURL_EASY_SETOPT (curl, CURLOPT_FOLLOWLOCATION, 1);
237 CURL_EASY_SETOPT (curl, CURLOPT_MAXREDIRS, 4);
239 /* setting put options */
240 CURL_EASY_SETOPT (curl, CURLOPT_UPLOAD, 1L);
241 CURL_EASY_SETOPT (curl, CURLOPT_PUT, 1L);
242 CURL_EASY_SETOPT (curl, CURLOPT_READDATA, msgbuf);
245 /* no need to abort if the above failed */
246 CURL_EASY_SETOPT (curl,
254 CURL_EASY_SETOPT (curl,
258 CURL_EASY_SETOPT (curl,
262 CURL_EASY_SETOPT (curl,
264 GNUNET_SERVER_MAX_MESSAGE_SIZE);
265 if (0 == strncmp (peer_url, "http", 4))
266 CURL_EASY_SETOPT (curl, CURLOPT_USERAGENT, "GNUnet");
267 CURL_EASY_SETOPT (curl,
268 CURLOPT_CONNECTTIMEOUT,
270 CURL_EASY_SETOPT (curl,
274 GNUNET_free(peer_url);
281 * Function that can be used to force the plugin to disconnect
282 * from the given peer and cancel all previous transmissions
283 * (and their continuationc).
286 * @param target peer from which to disconnect
289 http_plugin_disconnect (void *cls,
290 const struct GNUNET_PeerIdentity *target)
292 // struct Plugin *plugin = cls;
299 * Convert the transports address to a nice, human-readable
303 * @param type name of the transport that generated the address
304 * @param addr one of the addresses of the host, NULL for the last address
305 * the specific address format depends on the transport
306 * @param addrlen length of the address
307 * @param numeric should (IP) addresses be displayed in numeric form?
308 * @param timeout after how long should we give up?
309 * @param asc function to call on each string
310 * @param asc_cls closure for asc
313 http_plugin_address_pretty_printer (void *cls,
318 struct GNUNET_TIME_Relative timeout,
319 GNUNET_TRANSPORT_AddressStringCallback
328 * Another peer has suggested an address for this
329 * peer and transport plugin. Check that this could be a valid
330 * address. If so, consider adding it to the list
334 * @param addr pointer to the address
335 * @param addrlen length of addr
336 * @return GNUNET_OK if this is a plausible address for this peer
340 http_plugin_address_suggested (void *cls,
341 void *addr, size_t addrlen)
343 /* struct Plugin *plugin = cls; */
345 /* check if the address is plausible; if so,
346 add it to our list! */
351 * Check if we are allowed to connect to the given IP.
354 acceptPolicyCallback (void *cls,
355 const struct sockaddr *addr, socklen_t addr_len)
357 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Incoming connection \n");
358 /* Currently all incoming connections are accepted, so nothing to do here */
363 * Process GET or PUT request received via MHD. For
364 * GET, queue response that will send back our pending
365 * messages. For PUT, process incoming data and send
366 * to GNUnet core. In either case, check if a session
367 * already exists and create a new one if not.
370 accessHandlerCallback (void *cls,
371 struct MHD_Connection *session,
375 const char *upload_data,
376 size_t * upload_data_size, void **httpSessionCache)
378 struct MHD_Response *response;
380 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"HTTP Daemon has an incoming `%s' request from \n",method);
382 /* Find out if session exists, otherwise create one */
384 /* Is it a PUT or a GET request */
385 if ( 0 == strcmp (MHD_HTTP_METHOD_PUT, method) )
387 /* PUT method here */
388 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Got PUT Request with size %u \n",upload_data_size);
389 GNUNET_STATISTICS_update( plugin->env->stats , gettext_noop("# PUT requests"), 1, GNUNET_NO);
391 if ( 0 == strcmp (MHD_HTTP_METHOD_GET, method) )
393 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Got GET Request with size\n");
394 GNUNET_STATISTICS_update( plugin->env->stats , gettext_noop("# GET requests"), 1, GNUNET_NO);
397 response = MHD_create_response_from_data (strlen (HTTP_PUT_RESPONSE),
398 HTTP_PUT_RESPONSE, MHD_NO, MHD_NO);
399 MHD_queue_response (session, MHD_HTTP_OK, response);
400 MHD_destroy_response (response);
406 * Function that queries MHD's select sets and
407 * starts the task waiting for them.
409 static GNUNET_SCHEDULER_TaskIdentifier prepare_daemon (struct MHD_Daemon *daemon_handle);
411 * Call MHD to process pending requests and then go back
412 * and schedule the next run.
415 run_daemon (void *cls,
416 const struct GNUNET_SCHEDULER_TaskContext *tc)
418 struct MHD_Daemon *daemon_handle = cls;
420 if (daemon_handle == http_daemon_v4)
421 http_task_v4 = GNUNET_SCHEDULER_NO_TASK;
423 if (daemon_handle == http_daemon_v6)
424 http_task_v6 = GNUNET_SCHEDULER_NO_TASK;
427 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
429 GNUNET_assert (MHD_YES == MHD_run (daemon_handle));
430 if (daemon_handle == http_daemon_v4)
431 http_task_v4 = prepare_daemon (daemon_handle);
432 if (daemon_handle == http_daemon_v6)
433 http_task_v6 = prepare_daemon (daemon_handle);
437 * Function that queries MHD's select sets and
438 * starts the task waiting for them.
440 static GNUNET_SCHEDULER_TaskIdentifier
441 prepare_daemon (struct MHD_Daemon *daemon_handle)
443 GNUNET_SCHEDULER_TaskIdentifier ret;
447 struct GNUNET_NETWORK_FDSet *wrs;
448 struct GNUNET_NETWORK_FDSet *wws;
449 struct GNUNET_NETWORK_FDSet *wes;
451 unsigned long long timeout;
453 struct GNUNET_TIME_Relative tv;
458 wrs = GNUNET_NETWORK_fdset_create ();
459 wes = GNUNET_NETWORK_fdset_create ();
460 wws = GNUNET_NETWORK_fdset_create ();
462 GNUNET_assert (MHD_YES ==
463 MHD_get_fdset (daemon_handle,
468 haveto = MHD_get_timeout (daemon_handle, &timeout);
469 if (haveto == MHD_YES)
470 tv.value = (uint64_t) timeout;
472 tv = GNUNET_TIME_UNIT_FOREVER_REL;
473 GNUNET_NETWORK_fdset_copy_native (wrs, &rs, max);
474 GNUNET_NETWORK_fdset_copy_native (wws, &ws, max);
475 GNUNET_NETWORK_fdset_copy_native (wes, &es, max);
476 ret = GNUNET_SCHEDULER_add_select (plugin->env->sched,
477 GNUNET_SCHEDULER_PRIORITY_HIGH,
478 GNUNET_SCHEDULER_NO_TASK,
484 GNUNET_NETWORK_fdset_destroy (wrs);
485 GNUNET_NETWORK_fdset_destroy (wws);
486 GNUNET_NETWORK_fdset_destroy (wes);
491 * Exit point from the plugin.
494 libgnunet_plugin_transport_http_done (void *cls)
496 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
497 struct Plugin *plugin = api->cls;
499 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Shutting down http plugin...\n");
501 if ( http_task_v4 != GNUNET_SCHEDULER_NO_TASK)
503 GNUNET_SCHEDULER_cancel(plugin->env->sched, http_task_v4);
504 http_task_v4 = GNUNET_SCHEDULER_NO_TASK;
507 if ( http_task_v6 != GNUNET_SCHEDULER_NO_TASK)
509 GNUNET_SCHEDULER_cancel(plugin->env->sched, http_task_v6);
510 http_task_v6 = GNUNET_SCHEDULER_NO_TASK;
513 if (http_daemon_v4 != NULL)
515 MHD_stop_daemon (http_daemon_v4);
516 http_daemon_v4 = NULL;
518 if (http_daemon_v6 != NULL)
520 MHD_stop_daemon (http_daemon_v6);
521 http_daemon_v6 = NULL;
525 if ( NULL != curl_multi)
527 curl_multi_cleanup (curl_multi);
530 GNUNET_free (plugin);
536 * Entry point for the plugin.
539 libgnunet_plugin_transport_http_init (void *cls)
541 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
542 struct GNUNET_TRANSPORT_PluginFunctions *api;
543 long long unsigned int port;
545 plugin = GNUNET_malloc (sizeof (struct Plugin));
548 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
550 api->send = &http_plugin_send;
551 api->disconnect = &http_plugin_disconnect;
552 api->address_pretty_printer = &http_plugin_address_pretty_printer;
553 api->check_address = &http_plugin_address_suggested;
556 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Starting http plugin...\n");
557 /* Reading port number from config file */
559 GNUNET_CONFIGURATION_get_value_number (env->cfg,
565 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
568 ("Require valid port number for service `%s' in configuration!\n"),
570 libgnunet_plugin_transport_http_done (api);
573 if ((http_daemon_v4 == NULL) && (http_daemon_v6 == NULL) && (port != 0))
575 http_daemon_v6 = MHD_start_daemon (MHD_USE_IPv6,
577 &acceptPolicyCallback,
578 NULL, &accessHandlerCallback, NULL,
579 MHD_OPTION_CONNECTION_LIMIT, (unsigned int) 16,
580 MHD_OPTION_PER_IP_CONNECTION_LIMIT, (unsigned int) 1,
581 MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) 16,
582 MHD_OPTION_CONNECTION_MEMORY_LIMIT, (size_t) (16 * 1024),
584 http_daemon_v4 = MHD_start_daemon (MHD_NO_FLAG,
586 &acceptPolicyCallback,
587 NULL, &accessHandlerCallback, NULL,
588 MHD_OPTION_CONNECTION_LIMIT, (unsigned int) 16,
589 MHD_OPTION_PER_IP_CONNECTION_LIMIT, (unsigned int) 1,
590 MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) 16,
591 MHD_OPTION_CONNECTION_MEMORY_LIMIT, (size_t) (16 * 1024),
595 curl_multi = curl_multi_init ();
597 if (http_daemon_v4 != NULL)
598 http_task_v4 = prepare_daemon (http_daemon_v4);
599 if (http_daemon_v6 != NULL)
600 http_task_v6 = prepare_daemon (http_daemon_v6);
602 if ((http_daemon_v4 == NULL) || (http_daemon_v6 != NULL))
603 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Starting MHD on port %u\n",port);
606 if (NULL == plugin->env->stats)
608 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
609 _("Failed to retrieve statistics handle\n"));
610 libgnunet_plugin_transport_http_done (api);
614 GNUNET_STATISTICS_set ( env->stats, "# PUT requests", 0, GNUNET_NO);
615 GNUNET_STATISTICS_set ( env->stats, "# GET requests", 0, GNUNET_NO);
617 if ( ((NULL == http_daemon_v4) && (NULL == http_daemon_v6)) || (NULL == curl_multi))
619 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Initializing http plugin failed\n");
620 libgnunet_plugin_transport_http_done (api);
627 /* end of plugin_transport_http.c */