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 * Encapsulation of all of the state of the plugin.
57 * Session handle for connections.
63 * Stored in a linked list.
68 * Pointer to the global plugin struct.
70 struct Plugin *plugin;
73 * The client (used to identify this connection)
78 * Continuation function to call once the transmission buffer
79 * has again space available. NULL if there is no
80 * continuation to call.
82 GNUNET_TRANSPORT_TransmitContinuation transmit_cont;
85 * Closure for transmit_cont.
87 void *transmit_cont_cls;
90 * To whom are we talking to (set to our identity
91 * if we are still waiting for the welcome message)
93 struct GNUNET_PeerIdentity sender;
96 * At what time did we reset last_received last?
98 struct GNUNET_TIME_Absolute last_quota_update;
101 * How many bytes have we received since the "last_quota_update"
104 uint64_t last_received;
107 * Number of bytes per ms that this peer is allowed
115 * Encapsulation of all of the state of the plugin.
122 struct GNUNET_TRANSPORT_PluginEnvironment *env;
125 * Handle to the network service.
127 struct GNUNET_SERVICE_Context *service;
130 * List of open sessions.
132 struct Session *sessions;
135 static struct Plugin *plugin;
138 * Daemon for listening for new connections.
140 static struct MHD_Daemon *http_daemon;
143 * Our primary task for http
145 static GNUNET_SCHEDULER_TaskIdentifier http_task;
148 * Curl multi for managing client operations.
150 static CURLM *curl_multi;
153 * Function that can be used by the transport service to transmit
154 * a message using the plugin.
157 * @param target who should receive this message
158 * @param priority how important is the message
159 * @param msgbuf the message to transmit
160 * @param msgbuf_size number of bytes in 'msgbuf'
161 * @param timeout when should we time out
162 * @param session which session must be used (or NULL for "any")
163 * @param addr the address to use (can be NULL if the plugin
164 * is "on its own" (i.e. re-use existing TCP connection))
165 * @param addrlen length of the address in bytes
166 * @param force_address GNUNET_YES if the plugin MUST use the given address,
167 * otherwise the plugin may use other addresses or
168 * existing connections (if available)
169 * @param cont continuation to call once the message has
170 * been transmitted (or if the transport is ready
171 * for the next transmission call; or if the
172 * peer disconnected...)
173 * @param cont_cls closure for cont
174 * @return number of bytes used (on the physical network, with overheads);
175 * -1 on hard errors (i.e. address invalid); 0 is a legal value
176 * and does NOT mean that the message was not transmitted (DV)
179 http_plugin_send (void *cls,
180 const struct GNUNET_PeerIdentity *
184 unsigned int priority,
185 struct GNUNET_TIME_Relative timeout,
186 struct Session *session,
190 GNUNET_TRANSPORT_TransmitContinuation
191 cont, void *cont_cls)
194 /* struct Plugin *plugin = cls; */
201 * Function that can be used to force the plugin to disconnect
202 * from the given peer and cancel all previous transmissions
203 * (and their continuationc).
206 * @param target peer from which to disconnect
209 http_plugin_disconnect (void *cls,
210 const struct GNUNET_PeerIdentity *target)
212 // struct Plugin *plugin = cls;
219 * Convert the transports address to a nice, human-readable
223 * @param type name of the transport that generated the address
224 * @param addr one of the addresses of the host, NULL for the last address
225 * the specific address format depends on the transport
226 * @param addrlen length of the address
227 * @param numeric should (IP) addresses be displayed in numeric form?
228 * @param timeout after how long should we give up?
229 * @param asc function to call on each string
230 * @param asc_cls closure for asc
233 http_plugin_address_pretty_printer (void *cls,
238 struct GNUNET_TIME_Relative timeout,
239 GNUNET_TRANSPORT_AddressStringCallback
248 * Another peer has suggested an address for this
249 * peer and transport plugin. Check that this could be a valid
250 * address. If so, consider adding it to the list
254 * @param addr pointer to the address
255 * @param addrlen length of addr
256 * @return GNUNET_OK if this is a plausible address for this peer
260 http_plugin_address_suggested (void *cls,
261 void *addr, size_t addrlen)
263 /* struct Plugin *plugin = cls; */
265 /* check if the address is plausible; if so,
266 add it to our list! */
271 * Check if we are allowed to connect to the given IP.
274 acceptPolicyCallback (void *cls,
275 const struct sockaddr *addr, socklen_t addr_len)
277 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG," Incoming connection \n");
278 /* Currently all incoming connections are accepted, so nothing to do here */
283 * Process GET or PUT request received via MHD. For
284 * GET, queue response that will send back our pending
285 * messages. For PUT, process incoming data and send
286 * to GNUnet core. In either case, check if a session
287 * already exists and create a new one if not.
290 accessHandlerCallback (void *cls,
291 struct MHD_Connection *session,
295 const char *upload_data,
296 size_t * upload_data_size, void **httpSessionCache)
298 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"HTTP Daemon has an incoming `%s' request from \n",method);
300 /* Find out if session exists, otherwise create one */
302 /* Is it a PUT or a GET request */
303 if ( 0 == strcmp (MHD_HTTP_METHOD_PUT, method) )
305 /* PUT method here */
306 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Got PUT Request with size %u \n",upload_data_size);
308 GNUNET_STATISTICS_update( plugin->env->stats , gettext_noop("# PUT requests"), 1, GNUNET_NO);
310 if ( 0 == strcmp (MHD_HTTP_METHOD_GET, method) )
312 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Got GET Request with size %u \n",upload_data_size);
313 GNUNET_STATISTICS_update( plugin->env->stats , gettext_noop("# GET requests"), 1, GNUNET_NO);
320 * MHD is done handling a request. Cleanup
321 * the respective transport state.
324 requestCompletedCallback (void *unused,
325 struct MHD_Connection *session,
326 void **httpSessionCache)
333 * Function that queries MHD's select sets and
334 * starts the task waiting for them.
336 static GNUNET_SCHEDULER_TaskIdentifier prepare_daemon (struct MHD_Daemon *daemon_handle);
338 * Call MHD to process pending requests and then go back
339 * and schedule the next run.
342 run_daemon (void *cls,
343 const struct GNUNET_SCHEDULER_TaskContext *tc)
345 struct MHD_Daemon *daemon_handle = cls;
347 if (daemon_handle == http_daemon)
348 http_task = GNUNET_SCHEDULER_NO_TASK;
350 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
352 GNUNET_assert (MHD_YES == MHD_run (daemon_handle));
353 if (daemon_handle == http_daemon)
354 http_task = prepare_daemon (daemon_handle);
359 * Function that queries MHD's select sets and
360 * starts the task waiting for them.
362 static GNUNET_SCHEDULER_TaskIdentifier
363 prepare_daemon (struct MHD_Daemon *daemon_handle)
365 GNUNET_SCHEDULER_TaskIdentifier ret;
369 struct GNUNET_NETWORK_FDSet *wrs;
370 struct GNUNET_NETWORK_FDSet *wws;
371 struct GNUNET_NETWORK_FDSet *wes;
373 unsigned long long timeout;
375 struct GNUNET_TIME_Relative tv;
380 wrs = GNUNET_NETWORK_fdset_create ();
381 wes = GNUNET_NETWORK_fdset_create ();
382 wws = GNUNET_NETWORK_fdset_create ();
384 GNUNET_assert (MHD_YES ==
385 MHD_get_fdset (daemon_handle,
390 haveto = MHD_get_timeout (daemon_handle, &timeout);
391 if (haveto == MHD_YES)
392 tv.value = (uint64_t) timeout;
394 tv = GNUNET_TIME_UNIT_FOREVER_REL;
395 GNUNET_NETWORK_fdset_copy_native (wrs, &rs, max);
396 GNUNET_NETWORK_fdset_copy_native (wws, &ws, max);
397 GNUNET_NETWORK_fdset_copy_native (wes, &es, max);
398 ret = GNUNET_SCHEDULER_add_select (plugin->env->sched,
399 GNUNET_SCHEDULER_PRIORITY_HIGH,
400 GNUNET_SCHEDULER_NO_TASK,
406 GNUNET_NETWORK_fdset_destroy (wrs);
407 GNUNET_NETWORK_fdset_destroy (wws);
408 GNUNET_NETWORK_fdset_destroy (wes);
414 * Entry point for the plugin.
417 libgnunet_plugin_transport_http_init (void *cls)
419 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
420 struct GNUNET_TRANSPORT_PluginFunctions *api;
421 long long unsigned int port;
422 struct GNUNET_SERVICE_Context *service;
425 plugin = GNUNET_malloc (sizeof (struct Plugin));
428 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
430 api->send = &http_plugin_send;
431 api->disconnect = &http_plugin_disconnect;
432 api->address_pretty_printer = &http_plugin_address_pretty_printer;
433 api->check_address = &http_plugin_address_suggested;
436 service = GNUNET_SERVICE_start ("transport-http", env->sched, env->cfg);
439 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
441 _("Failed to start service for `%s' transport plugin.\n"),
445 plugin->service = service;
447 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Starting http plugin...\n");
448 /* Reading port number from config file */
450 GNUNET_CONFIGURATION_get_value_number (env->cfg,
456 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
459 ("Require valid port number for service `%s' in configuration!\n"),
464 use_ipv6 = GNUNET_YES;
465 use_ipv6 = GNUNET_CONFIGURATION_get_value_yesno (env->cfg, "transport-http","USE_IPV6");
466 if ((http_daemon == NULL) && (port != 0))
468 if ( use_ipv6 == GNUNET_YES)
470 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Starting MHD on port %u with IPv6 enabled\n",port);
471 http_daemon = MHD_start_daemon (MHD_USE_IPv6,
473 &acceptPolicyCallback,
474 NULL, &accessHandlerCallback, NULL,
475 MHD_OPTION_CONNECTION_TIMEOUT,
476 (unsigned int) HTTP_TIMEOUT,
477 MHD_OPTION_CONNECTION_MEMORY_LIMIT,
478 (unsigned int) GNUNET_SERVER_MAX_MESSAGE_SIZE,
479 MHD_OPTION_CONNECTION_LIMIT,
481 MHD_OPTION_PER_IP_CONNECTION_LIMIT,
483 MHD_OPTION_NOTIFY_COMPLETED,
484 &requestCompletedCallback, NULL,
489 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Starting MHD on port %u with IPv6 disabled\n",port);
490 http_daemon = MHD_start_daemon (MHD_NO_FLAG,
492 &acceptPolicyCallback,
493 NULL, &accessHandlerCallback, NULL,
494 MHD_OPTION_CONNECTION_TIMEOUT,
495 (unsigned int) HTTP_TIMEOUT,
496 MHD_OPTION_CONNECTION_MEMORY_LIMIT,
497 (unsigned int) GNUNET_SERVER_MAX_MESSAGE_SIZE,
498 MHD_OPTION_CONNECTION_LIMIT,
500 MHD_OPTION_PER_IP_CONNECTION_LIMIT,
502 MHD_OPTION_NOTIFY_COMPLETED,
503 &requestCompletedCallback, NULL,
508 curl_multi = curl_multi_init ();
510 if (http_daemon != NULL)
511 http_task = prepare_daemon (http_daemon);
513 if (NULL == plugin->env->stats)
515 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
516 _("Failed to retrieve statistics handle\n"));
521 GNUNET_STATISTICS_set ( env->stats, "# PUT requests", 0, GNUNET_NO);
522 GNUNET_STATISTICS_set ( env->stats, "# GET requests", 0, GNUNET_NO);
524 if ( (NULL == http_daemon) || (NULL == curl_multi))
526 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Initializing http plugin failed\n");
536 * Exit point from the plugin.
539 libgnunet_plugin_transport_http_done (void *cls)
541 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
542 struct Plugin *plugin = api->cls;
544 if (http_daemon != NULL)
546 MHD_stop_daemon (http_daemon);
550 curl_multi_cleanup (curl_multi);
553 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Shutting down http plugin...\n");
554 /* GNUNET_SERVICE_stop (plugin->service); */
555 GNUNET_free (plugin);
560 /* end of plugin_transport_http.c */