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
51 * Text of the response sent back after the last bytes of a PUT
52 * request have been received (just to formally obey the HTTP
55 #define HTTP_PUT_RESPONSE "Thank you!"
58 * Encapsulation of all of the state of the plugin.
64 * Session handle for connections.
70 * Stored in a linked list.
75 * Pointer to the global plugin struct.
77 struct Plugin *plugin;
80 * The client (used to identify this connection)
85 * Continuation function to call once the transmission buffer
86 * has again space available. NULL if there is no
87 * continuation to call.
89 GNUNET_TRANSPORT_TransmitContinuation transmit_cont;
92 * Closure for transmit_cont.
94 void *transmit_cont_cls;
97 * To whom are we talking to (set to our identity
98 * if we are still waiting for the welcome message)
100 struct GNUNET_PeerIdentity sender;
103 * At what time did we reset last_received last?
105 struct GNUNET_TIME_Absolute last_quota_update;
108 * How many bytes have we received since the "last_quota_update"
111 uint64_t last_received;
114 * Number of bytes per ms that this peer is allowed
122 * Encapsulation of all of the state of the plugin.
129 struct GNUNET_TRANSPORT_PluginEnvironment *env;
132 * Handle to the network service.
134 struct GNUNET_SERVICE_Context *service;
137 * List of open sessions.
139 struct Session *sessions;
142 static struct Plugin *plugin;
145 * Daemon for listening for new IPv4 connections.
147 static struct MHD_Daemon *http_daemon_v4;
150 * Daemon for listening for new IPv6connections.
152 static struct MHD_Daemon *http_daemon_v6;
155 * Our primary task for http daemon handling IPv4 connections
157 static GNUNET_SCHEDULER_TaskIdentifier http_task_v4;
160 * Our primary task for http daemon handling IPv6 connections
162 static GNUNET_SCHEDULER_TaskIdentifier http_task_v6;
166 * Curl multi for managing client operations.
168 static CURLM *curl_multi;
171 * Function that can be used by the transport service to transmit
172 * a message using the plugin.
175 * @param target who should receive this message
176 * @param priority how important is the message
177 * @param msgbuf the message to transmit
178 * @param msgbuf_size number of bytes in 'msgbuf'
179 * @param timeout when should we time out
180 * @param session which session must be used (or NULL for "any")
181 * @param addr the address to use (can be NULL if the plugin
182 * is "on its own" (i.e. re-use existing TCP connection))
183 * @param addrlen length of the address in bytes
184 * @param force_address GNUNET_YES if the plugin MUST use the given address,
185 * otherwise the plugin may use other addresses or
186 * existing connections (if available)
187 * @param cont continuation to call once the message has
188 * been transmitted (or if the transport is ready
189 * for the next transmission call; or if the
190 * peer disconnected...)
191 * @param cont_cls closure for cont
192 * @return number of bytes used (on the physical network, with overheads);
193 * -1 on hard errors (i.e. address invalid); 0 is a legal value
194 * and does NOT mean that the message was not transmitted (DV)
197 http_plugin_send (void *cls,
198 const struct GNUNET_PeerIdentity *
202 unsigned int priority,
203 struct GNUNET_TIME_Relative timeout,
204 struct Session *session,
208 GNUNET_TRANSPORT_TransmitContinuation
209 cont, void *cont_cls)
212 /* struct Plugin *plugin = cls; */
219 * Function that can be used to force the plugin to disconnect
220 * from the given peer and cancel all previous transmissions
221 * (and their continuationc).
224 * @param target peer from which to disconnect
227 http_plugin_disconnect (void *cls,
228 const struct GNUNET_PeerIdentity *target)
230 // struct Plugin *plugin = cls;
237 * Convert the transports address to a nice, human-readable
241 * @param type name of the transport that generated the address
242 * @param addr one of the addresses of the host, NULL for the last address
243 * the specific address format depends on the transport
244 * @param addrlen length of the address
245 * @param numeric should (IP) addresses be displayed in numeric form?
246 * @param timeout after how long should we give up?
247 * @param asc function to call on each string
248 * @param asc_cls closure for asc
251 http_plugin_address_pretty_printer (void *cls,
256 struct GNUNET_TIME_Relative timeout,
257 GNUNET_TRANSPORT_AddressStringCallback
266 * Another peer has suggested an address for this
267 * peer and transport plugin. Check that this could be a valid
268 * address. If so, consider adding it to the list
272 * @param addr pointer to the address
273 * @param addrlen length of addr
274 * @return GNUNET_OK if this is a plausible address for this peer
278 http_plugin_address_suggested (void *cls,
279 void *addr, size_t addrlen)
281 /* struct Plugin *plugin = cls; */
283 /* check if the address is plausible; if so,
284 add it to our list! */
289 * Check if we are allowed to connect to the given IP.
292 acceptPolicyCallback (void *cls,
293 const struct sockaddr *addr, socklen_t addr_len)
295 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Incoming connection \n");
296 /* Currently all incoming connections are accepted, so nothing to do here */
301 * Process GET or PUT request received via MHD. For
302 * GET, queue response that will send back our pending
303 * messages. For PUT, process incoming data and send
304 * to GNUnet core. In either case, check if a session
305 * already exists and create a new one if not.
308 accessHandlerCallback (void *cls,
309 struct MHD_Connection *session,
313 const char *upload_data,
314 size_t * upload_data_size, void **httpSessionCache)
316 struct MHD_Response *response;
318 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"HTTP Daemon has an incoming `%s' request from \n",method);
320 /* Find out if session exists, otherwise create one */
322 /* Is it a PUT or a GET request */
323 if ( 0 == strcmp (MHD_HTTP_METHOD_PUT, method) )
325 /* PUT method here */
326 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Got PUT Request with size %u \n",upload_data_size);
327 GNUNET_STATISTICS_update( plugin->env->stats , gettext_noop("# PUT requests"), 1, GNUNET_NO);
329 if ( 0 == strcmp (MHD_HTTP_METHOD_GET, method) )
331 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Got GET Request with size\n");
332 GNUNET_STATISTICS_update( plugin->env->stats , gettext_noop("# GET requests"), 1, GNUNET_NO);
335 response = MHD_create_response_from_data (strlen (HTTP_PUT_RESPONSE),
336 HTTP_PUT_RESPONSE, MHD_NO, MHD_NO);
337 MHD_queue_response (session, MHD_HTTP_OK, response);
338 MHD_destroy_response (response);
344 * Function that queries MHD's select sets and
345 * starts the task waiting for them.
347 static GNUNET_SCHEDULER_TaskIdentifier prepare_daemon (struct MHD_Daemon *daemon_handle);
349 * Call MHD to process pending requests and then go back
350 * and schedule the next run.
353 run_daemon (void *cls,
354 const struct GNUNET_SCHEDULER_TaskContext *tc)
356 struct MHD_Daemon *daemon_handle = cls;
358 if (daemon_handle == http_daemon_v4)
359 http_task_v4 = GNUNET_SCHEDULER_NO_TASK;
361 if (daemon_handle == http_daemon_v6)
362 http_task_v6 = GNUNET_SCHEDULER_NO_TASK;
365 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
367 GNUNET_assert (MHD_YES == MHD_run (daemon_handle));
368 if (daemon_handle == http_daemon_v4)
369 http_task_v4 = prepare_daemon (daemon_handle);
370 if (daemon_handle == http_daemon_v6)
371 http_task_v6 = prepare_daemon (daemon_handle);
375 * Function that queries MHD's select sets and
376 * starts the task waiting for them.
378 static GNUNET_SCHEDULER_TaskIdentifier
379 prepare_daemon (struct MHD_Daemon *daemon_handle)
381 GNUNET_SCHEDULER_TaskIdentifier ret;
385 struct GNUNET_NETWORK_FDSet *wrs;
386 struct GNUNET_NETWORK_FDSet *wws;
387 struct GNUNET_NETWORK_FDSet *wes;
389 unsigned long long timeout;
391 struct GNUNET_TIME_Relative tv;
396 wrs = GNUNET_NETWORK_fdset_create ();
397 wes = GNUNET_NETWORK_fdset_create ();
398 wws = GNUNET_NETWORK_fdset_create ();
400 GNUNET_assert (MHD_YES ==
401 MHD_get_fdset (daemon_handle,
406 haveto = MHD_get_timeout (daemon_handle, &timeout);
407 if (haveto == MHD_YES)
408 tv.value = (uint64_t) timeout;
410 tv = GNUNET_TIME_UNIT_FOREVER_REL;
411 GNUNET_NETWORK_fdset_copy_native (wrs, &rs, max);
412 GNUNET_NETWORK_fdset_copy_native (wws, &ws, max);
413 GNUNET_NETWORK_fdset_copy_native (wes, &es, max);
414 ret = GNUNET_SCHEDULER_add_select (plugin->env->sched,
415 GNUNET_SCHEDULER_PRIORITY_HIGH,
416 GNUNET_SCHEDULER_NO_TASK,
422 GNUNET_NETWORK_fdset_destroy (wrs);
423 GNUNET_NETWORK_fdset_destroy (wws);
424 GNUNET_NETWORK_fdset_destroy (wes);
429 * Exit point from the plugin.
432 libgnunet_plugin_transport_http_done (void *cls)
434 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
435 struct Plugin *plugin = api->cls;
437 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Shutting down http plugin...\n");
439 if ( http_task_v4 != GNUNET_SCHEDULER_NO_TASK)
441 GNUNET_SCHEDULER_cancel(plugin->env->sched, http_task_v4);
444 if ( http_task_v6 != GNUNET_SCHEDULER_NO_TASK)
446 GNUNET_SCHEDULER_cancel(plugin->env->sched, http_task_v6);
449 if (http_daemon_v4 != NULL)
451 MHD_stop_daemon (http_daemon_v4);
452 http_daemon_v4 = NULL;
454 if (http_daemon_v6 != NULL)
456 MHD_stop_daemon (http_daemon_v6);
457 http_daemon_v6 = NULL;
461 if ( NULL != curl_multi)
463 curl_multi_cleanup (curl_multi);
466 GNUNET_free (plugin);
472 * Entry point for the plugin.
475 libgnunet_plugin_transport_http_init (void *cls)
477 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
478 struct GNUNET_TRANSPORT_PluginFunctions *api;
479 long long unsigned int port;
481 plugin = GNUNET_malloc (sizeof (struct Plugin));
484 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
486 api->send = &http_plugin_send;
487 api->disconnect = &http_plugin_disconnect;
488 api->address_pretty_printer = &http_plugin_address_pretty_printer;
489 api->check_address = &http_plugin_address_suggested;
492 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Starting http plugin...\n");
493 /* Reading port number from config file */
495 GNUNET_CONFIGURATION_get_value_number (env->cfg,
501 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
504 ("Require valid port number for service `%s' in configuration!\n"),
506 libgnunet_plugin_transport_http_done (api);
509 if ((http_daemon_v4 == NULL) && (http_daemon_v6 == NULL) && (port != 0))
511 http_daemon_v4 = MHD_start_daemon (MHD_NO_FLAG,
513 &acceptPolicyCallback,
514 NULL, &accessHandlerCallback, NULL,
515 MHD_OPTION_CONNECTION_LIMIT, (unsigned int) 16,
516 MHD_OPTION_PER_IP_CONNECTION_LIMIT, (unsigned int) 1,
517 MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) 16,
518 MHD_OPTION_CONNECTION_MEMORY_LIMIT, (size_t) (16 * 1024),
520 http_daemon_v6 = MHD_start_daemon (MHD_USE_IPv6,
522 &acceptPolicyCallback,
523 NULL, &accessHandlerCallback, NULL,
524 MHD_OPTION_CONNECTION_LIMIT, (unsigned int) 16,
525 MHD_OPTION_PER_IP_CONNECTION_LIMIT, (unsigned int) 1,
526 MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) 16,
527 MHD_OPTION_CONNECTION_MEMORY_LIMIT, (size_t) (16 * 1024),
532 if ((http_daemon_v4 != NULL) && (http_daemon_v6 != NULL))
533 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Starting MHD on port %u with IPv4 and IPv6 enabled\n",port);
534 if ((http_daemon_v4 != NULL) && (http_daemon_v6 == NULL))
535 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Starting MHD on port %u with IPv4 enabled\n",port);
536 if ((http_daemon_v4 == NULL) && (http_daemon_v6 != NULL))
537 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Starting MHD on port %u with IPv6 enabled\n",port);
539 curl_multi = curl_multi_init ();
541 if (http_daemon_v4 != NULL)
542 http_task_v4 = prepare_daemon (http_daemon_v4);
543 if (http_daemon_v6 != NULL)
544 http_task_v6 = prepare_daemon (http_daemon_v6);
546 if (NULL == plugin->env->stats)
548 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
549 _("Failed to retrieve statistics handle\n"));
550 libgnunet_plugin_transport_http_done (api);
554 GNUNET_STATISTICS_set ( env->stats, "# PUT requests", 0, GNUNET_NO);
555 GNUNET_STATISTICS_set ( env->stats, "# GET requests", 0, GNUNET_NO);
557 if ( ((NULL == http_daemon_v4) && (NULL == http_daemon_v6)) || (NULL == curl_multi))
559 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Initializing http plugin failed\n");
560 libgnunet_plugin_transport_http_done (api);
567 /* end of plugin_transport_http.c */