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 * List of open sessions.
127 struct Session *sessions;
132 * Daemon for listening for new connections.
134 static struct MHD_Daemon *http_daemon;
137 * Curl multi for managing client operations.
139 static CURLM *curl_multi;
142 * Function that can be used by the transport service to transmit
143 * a message using the plugin.
146 * @param target who should receive this message
147 * @param priority how important is the message
148 * @param msgbuf the message to transmit
149 * @param msgbuf_size number of bytes in 'msgbuf'
150 * @param timeout when should we time out
151 * @param session which session must be used (or NULL for "any")
152 * @param addr the address to use (can be NULL if the plugin
153 * is "on its own" (i.e. re-use existing TCP connection))
154 * @param addrlen length of the address in bytes
155 * @param force_address GNUNET_YES if the plugin MUST use the given address,
156 * otherwise the plugin may use other addresses or
157 * existing connections (if available)
158 * @param cont continuation to call once the message has
159 * been transmitted (or if the transport is ready
160 * for the next transmission call; or if the
161 * peer disconnected...)
162 * @param cont_cls closure for cont
163 * @return number of bytes used (on the physical network, with overheads);
164 * -1 on hard errors (i.e. address invalid); 0 is a legal value
165 * and does NOT mean that the message was not transmitted (DV)
168 http_plugin_send (void *cls,
169 const struct GNUNET_PeerIdentity *
173 unsigned int priority,
174 struct GNUNET_TIME_Relative timeout,
175 struct Session *session,
179 GNUNET_TRANSPORT_TransmitContinuation
180 cont, void *cont_cls)
183 /* struct Plugin *plugin = cls; */
190 * Function that can be used to force the plugin to disconnect
191 * from the given peer and cancel all previous transmissions
192 * (and their continuationc).
195 * @param target peer from which to disconnect
198 http_plugin_disconnect (void *cls,
199 const struct GNUNET_PeerIdentity *target)
201 // struct Plugin *plugin = cls;
208 * Convert the transports address to a nice, human-readable
212 * @param type name of the transport that generated the address
213 * @param addr one of the addresses of the host, NULL for the last address
214 * the specific address format depends on the transport
215 * @param addrlen length of the address
216 * @param numeric should (IP) addresses be displayed in numeric form?
217 * @param timeout after how long should we give up?
218 * @param asc function to call on each string
219 * @param asc_cls closure for asc
222 http_plugin_address_pretty_printer (void *cls,
227 struct GNUNET_TIME_Relative timeout,
228 GNUNET_TRANSPORT_AddressStringCallback
237 * Another peer has suggested an address for this
238 * peer and transport plugin. Check that this could be a valid
239 * address. If so, consider adding it to the list
243 * @param addr pointer to the address
244 * @param addrlen length of addr
245 * @return GNUNET_OK if this is a plausible address for this peer
249 http_plugin_address_suggested (void *cls,
250 void *addr, size_t addrlen)
252 /* struct Plugin *plugin = cls; */
254 /* check if the address is plausible; if so,
255 add it to our list! */
260 * Check if we are allowed to connect to the given IP.
263 acceptPolicyCallback (void *cls,
264 const struct sockaddr *addr, socklen_t addr_len)
267 /* Currently all incoming connections are accepted, so nothing to do here */
272 * Process GET or PUT request received via MHD. For
273 * GET, queue response that will send back our pending
274 * messages. For PUT, process incoming data and send
275 * to GNUnet core. In either case, check if a session
276 * already exists and create a new one if not.
279 accessHandlerCallback (void *cls,
280 struct MHD_Connection *session,
284 const char *upload_data,
285 size_t * upload_data_size, void **httpSessionCache)
287 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"HTTP Daemon has an incoming `%s' request from \n",method);
289 if ( 0 == strcmp (MHD_HTTP_METHOD_PUT, method) )
291 /* PUT method here */
292 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Got PUT Request with size %u \n",upload_data_size);
294 // GNUNET_STATISTICS_update( plugin->env->stats , gettext_noop("# PUT requests"), 1, GNUNET_NO);
296 if ( 0 == strcmp (MHD_HTTP_METHOD_GET, method) )
302 * MHD is done handling a request. Cleanup
303 * the respective transport state.
306 requestCompletedCallback (void *unused,
307 struct MHD_Connection *session,
308 void **httpSessionCache)
314 * Entry point for the plugin.
317 libgnunet_plugin_transport_http_init (void *cls)
319 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
320 struct GNUNET_TRANSPORT_PluginFunctions *api;
321 struct Plugin *plugin;
322 long long unsigned int port;
325 plugin = GNUNET_malloc (sizeof (struct Plugin));
328 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
330 api->send = &http_plugin_send;
331 api->disconnect = &http_plugin_disconnect;
332 api->address_pretty_printer = &http_plugin_address_pretty_printer;
333 api->check_address = &http_plugin_address_suggested;
335 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Starting http plugin...\n");
336 /* Reading port number from config file */
338 GNUNET_CONFIGURATION_get_value_number (env->cfg,
344 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
347 ("Require valid port number for service `%s' in configuration!\n"),
351 use_ipv6 = GNUNET_YES;
352 use_ipv6 = GNUNET_CONFIGURATION_get_value_yesno (env->cfg, "transport-http","USE_IPV6");
353 if ((http_daemon == NULL) && (port != 0))
355 if ( use_ipv6 == GNUNET_YES)
357 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Starting MHD on port %u with IPv6 enabled\n",port);
358 http_daemon = MHD_start_daemon (MHD_USE_IPv6,
360 &acceptPolicyCallback,
361 NULL, &accessHandlerCallback, NULL,
362 MHD_OPTION_CONNECTION_TIMEOUT,
363 (unsigned int) HTTP_TIMEOUT,
364 MHD_OPTION_CONNECTION_MEMORY_LIMIT,
365 (unsigned int) GNUNET_SERVER_MAX_MESSAGE_SIZE,
366 MHD_OPTION_CONNECTION_LIMIT,
368 MHD_OPTION_PER_IP_CONNECTION_LIMIT,
370 MHD_OPTION_NOTIFY_COMPLETED,
371 &requestCompletedCallback, NULL,
376 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Starting MHD on port %u with IPv6 disabled\n",port);
377 http_daemon = MHD_start_daemon (MHD_NO_FLAG,
379 &acceptPolicyCallback,
380 NULL, &accessHandlerCallback, NULL,
381 MHD_OPTION_CONNECTION_TIMEOUT,
382 (unsigned int) HTTP_TIMEOUT,
383 MHD_OPTION_CONNECTION_MEMORY_LIMIT,
384 (unsigned int) GNUNET_SERVER_MAX_MESSAGE_SIZE,
385 MHD_OPTION_CONNECTION_LIMIT,
387 MHD_OPTION_PER_IP_CONNECTION_LIMIT,
389 MHD_OPTION_NOTIFY_COMPLETED,
390 &requestCompletedCallback, NULL,
395 curl_multi = curl_multi_init ();
398 if (NULL == plugin->env->stats)
400 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
401 _("Failed to retrieve statistics handle\n"));
405 GNUNET_STATISTICS_set ( env->stats, "# PUT requests", 0, GNUNET_NO);
407 if ( (NULL == http_daemon) || (NULL == curl_multi))
409 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Initializing http plugin failed\n");
418 * Exit point from the plugin.
421 libgnunet_plugin_transport_http_done (void *cls)
423 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
424 struct Plugin *plugin = api->cls;
426 if (http_daemon != NULL)
428 MHD_stop_daemon (http_daemon);
432 curl_multi_cleanup (curl_multi);
435 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Shutting down http plugin...\n");
437 GNUNET_free (plugin);
442 /* end of plugin_transport_http.c */