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 connections.
147 static struct MHD_Daemon *http_daemon;
150 * Our primary task for http
152 static GNUNET_SCHEDULER_TaskIdentifier http_task;
155 * Curl multi for managing client operations.
157 static CURLM *curl_multi;
160 * Function that can be used by the transport service to transmit
161 * a message using the plugin.
164 * @param target who should receive this message
165 * @param priority how important is the message
166 * @param msgbuf the message to transmit
167 * @param msgbuf_size number of bytes in 'msgbuf'
168 * @param timeout when should we time out
169 * @param session which session must be used (or NULL for "any")
170 * @param addr the address to use (can be NULL if the plugin
171 * is "on its own" (i.e. re-use existing TCP connection))
172 * @param addrlen length of the address in bytes
173 * @param force_address GNUNET_YES if the plugin MUST use the given address,
174 * otherwise the plugin may use other addresses or
175 * existing connections (if available)
176 * @param cont continuation to call once the message has
177 * been transmitted (or if the transport is ready
178 * for the next transmission call; or if the
179 * peer disconnected...)
180 * @param cont_cls closure for cont
181 * @return number of bytes used (on the physical network, with overheads);
182 * -1 on hard errors (i.e. address invalid); 0 is a legal value
183 * and does NOT mean that the message was not transmitted (DV)
186 http_plugin_send (void *cls,
187 const struct GNUNET_PeerIdentity *
191 unsigned int priority,
192 struct GNUNET_TIME_Relative timeout,
193 struct Session *session,
197 GNUNET_TRANSPORT_TransmitContinuation
198 cont, void *cont_cls)
201 /* struct Plugin *plugin = cls; */
208 * Function that can be used to force the plugin to disconnect
209 * from the given peer and cancel all previous transmissions
210 * (and their continuationc).
213 * @param target peer from which to disconnect
216 http_plugin_disconnect (void *cls,
217 const struct GNUNET_PeerIdentity *target)
219 // struct Plugin *plugin = cls;
226 * Convert the transports address to a nice, human-readable
230 * @param type name of the transport that generated the address
231 * @param addr one of the addresses of the host, NULL for the last address
232 * the specific address format depends on the transport
233 * @param addrlen length of the address
234 * @param numeric should (IP) addresses be displayed in numeric form?
235 * @param timeout after how long should we give up?
236 * @param asc function to call on each string
237 * @param asc_cls closure for asc
240 http_plugin_address_pretty_printer (void *cls,
245 struct GNUNET_TIME_Relative timeout,
246 GNUNET_TRANSPORT_AddressStringCallback
255 * Another peer has suggested an address for this
256 * peer and transport plugin. Check that this could be a valid
257 * address. If so, consider adding it to the list
261 * @param addr pointer to the address
262 * @param addrlen length of addr
263 * @return GNUNET_OK if this is a plausible address for this peer
267 http_plugin_address_suggested (void *cls,
268 void *addr, size_t addrlen)
270 /* struct Plugin *plugin = cls; */
272 /* check if the address is plausible; if so,
273 add it to our list! */
278 * Check if we are allowed to connect to the given IP.
281 acceptPolicyCallback (void *cls,
282 const struct sockaddr *addr, socklen_t addr_len)
284 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Incoming connection \n");
285 /* Currently all incoming connections are accepted, so nothing to do here */
290 * Process GET or PUT request received via MHD. For
291 * GET, queue response that will send back our pending
292 * messages. For PUT, process incoming data and send
293 * to GNUnet core. In either case, check if a session
294 * already exists and create a new one if not.
297 accessHandlerCallback (void *cls,
298 struct MHD_Connection *session,
302 const char *upload_data,
303 size_t * upload_data_size, void **httpSessionCache)
305 struct MHD_Response *response;
307 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"HTTP Daemon has an incoming `%s' request from \n",method);
309 /* Find out if session exists, otherwise create one */
311 /* Is it a PUT or a GET request */
312 if ( 0 == strcmp (MHD_HTTP_METHOD_PUT, method) )
314 /* PUT method here */
315 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Got PUT Request with size %u \n",upload_data_size);
317 GNUNET_STATISTICS_update( plugin->env->stats , gettext_noop("# PUT requests"), 1, GNUNET_NO);
319 if ( 0 == strcmp (MHD_HTTP_METHOD_GET, method) )
321 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Got GET Request with size %u \n",upload_data_size);
322 GNUNET_STATISTICS_update( plugin->env->stats , gettext_noop("# GET requests"), 1, GNUNET_NO);
325 response = MHD_create_response_from_data (strlen (HTTP_PUT_RESPONSE),
326 HTTP_PUT_RESPONSE, MHD_NO, MHD_NO);
327 MHD_queue_response (session, MHD_HTTP_OK, response);
328 MHD_destroy_response (response);
334 * MHD is done handling a request. Cleanup
335 * the respective transport state.
338 requestCompletedCallback (void *unused,
339 struct MHD_Connection *session,
340 void **httpSessionCache)
347 * Function that queries MHD's select sets and
348 * starts the task waiting for them.
350 static GNUNET_SCHEDULER_TaskIdentifier prepare_daemon (struct MHD_Daemon *daemon_handle);
352 * Call MHD to process pending requests and then go back
353 * and schedule the next run.
356 run_daemon (void *cls,
357 const struct GNUNET_SCHEDULER_TaskContext *tc)
359 struct MHD_Daemon *daemon_handle = cls;
361 if (daemon_handle == http_daemon)
362 http_task = GNUNET_SCHEDULER_NO_TASK;
364 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
366 GNUNET_assert (MHD_YES == MHD_run (daemon_handle));
367 if (daemon_handle == http_daemon)
368 http_task = prepare_daemon (daemon_handle);
373 * Function that queries MHD's select sets and
374 * starts the task waiting for them.
376 static GNUNET_SCHEDULER_TaskIdentifier
377 prepare_daemon (struct MHD_Daemon *daemon_handle)
379 GNUNET_SCHEDULER_TaskIdentifier ret;
383 struct GNUNET_NETWORK_FDSet *wrs;
384 struct GNUNET_NETWORK_FDSet *wws;
385 struct GNUNET_NETWORK_FDSet *wes;
387 unsigned long long timeout;
389 struct GNUNET_TIME_Relative tv;
394 wrs = GNUNET_NETWORK_fdset_create ();
395 wes = GNUNET_NETWORK_fdset_create ();
396 wws = GNUNET_NETWORK_fdset_create ();
398 GNUNET_assert (MHD_YES ==
399 MHD_get_fdset (daemon_handle,
404 haveto = MHD_get_timeout (daemon_handle, &timeout);
405 if (haveto == MHD_YES)
406 tv.value = (uint64_t) timeout;
408 tv = GNUNET_TIME_UNIT_FOREVER_REL;
409 GNUNET_NETWORK_fdset_copy_native (wrs, &rs, max);
410 GNUNET_NETWORK_fdset_copy_native (wws, &ws, max);
411 GNUNET_NETWORK_fdset_copy_native (wes, &es, max);
412 ret = GNUNET_SCHEDULER_add_select (plugin->env->sched,
413 GNUNET_SCHEDULER_PRIORITY_HIGH,
414 GNUNET_SCHEDULER_NO_TASK,
420 GNUNET_NETWORK_fdset_destroy (wrs);
421 GNUNET_NETWORK_fdset_destroy (wws);
422 GNUNET_NETWORK_fdset_destroy (wes);
428 * Entry point for the plugin.
431 libgnunet_plugin_transport_http_init (void *cls)
433 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
434 struct GNUNET_TRANSPORT_PluginFunctions *api;
435 long long unsigned int port;
436 /* struct GNUNET_SERVICE_Context *service; */
439 plugin = GNUNET_malloc (sizeof (struct Plugin));
442 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
444 api->send = &http_plugin_send;
445 api->disconnect = &http_plugin_disconnect;
446 api->address_pretty_printer = &http_plugin_address_pretty_printer;
447 api->check_address = &http_plugin_address_suggested;
450 service = GNUNET_SERVICE_start ("transport-http", env->sched, env->cfg);
453 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
455 _("Failed to start service for `%s' transport plugin.\n"),
459 plugin->service = service;
461 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Starting http plugin...\n");
462 /* Reading port number from config file */
464 GNUNET_CONFIGURATION_get_value_number (env->cfg,
470 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
473 ("Require valid port number for service `%s' in configuration!\n"),
478 use_ipv6 = GNUNET_YES;
479 use_ipv6 = GNUNET_CONFIGURATION_get_value_yesno (env->cfg, "transport-http","USE_IPV6");
480 if ((http_daemon == NULL) && (port != 0))
482 if ( use_ipv6 == GNUNET_YES)
484 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Starting MHD on port %u with IPv6 enabled\n",port);
485 http_daemon = MHD_start_daemon (MHD_USE_IPv6,
487 &acceptPolicyCallback,
488 NULL, &accessHandlerCallback, NULL,
489 MHD_OPTION_CONNECTION_LIMIT, (unsigned int) 16,
490 MHD_OPTION_PER_IP_CONNECTION_LIMIT, (unsigned int) 1,
491 MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) 16,
492 MHD_OPTION_CONNECTION_MEMORY_LIMIT, (size_t) (16 * 1024),
497 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Starting MHD on port %u with IPv6 disabled\n",port);
498 http_daemon = MHD_start_daemon (MHD_NO_FLAG,
500 &acceptPolicyCallback,
501 NULL, &accessHandlerCallback, NULL,
502 MHD_OPTION_CONNECTION_LIMIT, (unsigned int) 16,
503 MHD_OPTION_PER_IP_CONNECTION_LIMIT, (unsigned int) 1,
504 MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) 16,
505 MHD_OPTION_CONNECTION_MEMORY_LIMIT, (size_t) (16 * 1024),
510 curl_multi = curl_multi_init ();
512 if (http_daemon != NULL)
513 http_task = prepare_daemon (http_daemon);
515 if (NULL == plugin->env->stats)
517 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
518 _("Failed to retrieve statistics handle\n"));
523 GNUNET_STATISTICS_set ( env->stats, "# PUT requests", 0, GNUNET_NO);
524 GNUNET_STATISTICS_set ( env->stats, "# GET requests", 0, GNUNET_NO);
526 if ( (NULL == http_daemon) || (NULL == curl_multi))
528 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Initializing http plugin failed\n");
538 * Exit point from the plugin.
541 libgnunet_plugin_transport_http_done (void *cls)
543 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
544 struct Plugin *plugin = api->cls;
546 if ( http_task != GNUNET_SCHEDULER_NO_TASK)
548 GNUNET_SCHEDULER_cancel(plugin->env->sched, http_task);
551 if (http_daemon != NULL)
553 MHD_stop_daemon (http_daemon);
557 curl_multi_cleanup (curl_multi);
560 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Shutting down http plugin...\n");
561 /* GNUNET_SERVICE_stop (plugin->service); */
562 GNUNET_free (plugin);
567 /* end of plugin_transport_http.c */