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;
130 * Handle for the statistics service.
132 struct GNUNET_STATISTICS_Handle *statistics;
137 * Daemon for listening for new connections.
139 static struct MHD_Daemon *http_daemon;
142 * Curl multi for managing client operations.
144 static CURLM *curl_multi;
147 * Function that can be used by the transport service to transmit
148 * a message using the plugin.
151 * @param target who should receive this message
152 * @param priority how important is the message
153 * @param msgbuf the message to transmit
154 * @param msgbuf_size number of bytes in 'msgbuf'
155 * @param timeout when should we time out
156 * @param session which session must be used (or NULL for "any")
157 * @param addr the address to use (can be NULL if the plugin
158 * is "on its own" (i.e. re-use existing TCP connection))
159 * @param addrlen length of the address in bytes
160 * @param force_address GNUNET_YES if the plugin MUST use the given address,
161 * otherwise the plugin may use other addresses or
162 * existing connections (if available)
163 * @param cont continuation to call once the message has
164 * been transmitted (or if the transport is ready
165 * for the next transmission call; or if the
166 * peer disconnected...)
167 * @param cont_cls closure for cont
168 * @return number of bytes used (on the physical network, with overheads);
169 * -1 on hard errors (i.e. address invalid); 0 is a legal value
170 * and does NOT mean that the message was not transmitted (DV)
173 http_plugin_send (void *cls,
174 const struct GNUNET_PeerIdentity *
178 unsigned int priority,
179 struct GNUNET_TIME_Relative timeout,
180 struct Session *session,
184 GNUNET_TRANSPORT_TransmitContinuation
185 cont, void *cont_cls)
188 /* struct Plugin *plugin = cls; */
195 * Function that can be used to force the plugin to disconnect
196 * from the given peer and cancel all previous transmissions
197 * (and their continuationc).
200 * @param target peer from which to disconnect
203 http_plugin_disconnect (void *cls,
204 const struct GNUNET_PeerIdentity *target)
206 // struct Plugin *plugin = cls;
213 * Convert the transports address to a nice, human-readable
217 * @param type name of the transport that generated the address
218 * @param addr one of the addresses of the host, NULL for the last address
219 * the specific address format depends on the transport
220 * @param addrlen length of the address
221 * @param numeric should (IP) addresses be displayed in numeric form?
222 * @param timeout after how long should we give up?
223 * @param asc function to call on each string
224 * @param asc_cls closure for asc
227 http_plugin_address_pretty_printer (void *cls,
232 struct GNUNET_TIME_Relative timeout,
233 GNUNET_TRANSPORT_AddressStringCallback
242 * Another peer has suggested an address for this
243 * peer and transport plugin. Check that this could be a valid
244 * address. If so, consider adding it to the list
248 * @param addr pointer to the address
249 * @param addrlen length of addr
250 * @return GNUNET_OK if this is a plausible address for this peer
254 http_plugin_address_suggested (void *cls,
255 void *addr, size_t addrlen)
257 /* struct Plugin *plugin = cls; */
259 /* check if the address is plausible; if so,
260 add it to our list! */
265 * Check if we are allowed to connect to the given IP.
268 acceptPolicyCallback (void *cls,
269 const struct sockaddr *addr, socklen_t addr_len)
275 * Process GET or PUT request received via MHD. For
276 * GET, queue response that will send back our pending
277 * messages. For PUT, process incoming data and send
278 * to GNUnet core. In either case, check if a session
279 * already exists and create a new one if not.
282 accessHandlerCallback (void *cls,
283 struct MHD_Connection *session,
287 const char *upload_data,
288 size_t * upload_data_size, void **httpSessionCache)
294 * MHD is done handling a request. Cleanup
295 * the respective transport state.
298 requestCompletedCallback (void *unused,
299 struct MHD_Connection *session,
300 void **httpSessionCache)
306 * Entry point for the plugin.
309 libgnunet_plugin_transport_http_init (void *cls)
311 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
312 struct GNUNET_TRANSPORT_PluginFunctions *api;
313 struct Plugin *plugin;
314 long long unsigned int port;
317 plugin = GNUNET_malloc (sizeof (struct Plugin));
319 plugin->statistics = NULL;
320 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
322 api->send = &http_plugin_send;
323 api->disconnect = &http_plugin_disconnect;
324 api->address_pretty_printer = &http_plugin_address_pretty_printer;
325 api->check_address = &http_plugin_address_suggested;
327 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Starting http plugin...\n");
328 /* Reading port number from config file */
330 GNUNET_CONFIGURATION_get_value_number (env->cfg,
336 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
339 ("Require valid port number for service `%s' in configuration!\n"),
343 use_ipv6 = GNUNET_YES;
344 use_ipv6 = GNUNET_CONFIGURATION_get_value_yesno (env->cfg, "transport-http","USE_IPV6");
345 if ((http_daemon == NULL) && (port != 0))
347 if ( use_ipv6 == GNUNET_YES)
349 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Starting MHD on port %u with IPv6 enabled\n",port);
350 http_daemon = MHD_start_daemon (MHD_USE_IPv6,
352 &acceptPolicyCallback,
353 NULL, &accessHandlerCallback, NULL,
354 MHD_OPTION_CONNECTION_TIMEOUT,
355 (unsigned int) HTTP_TIMEOUT,
356 MHD_OPTION_CONNECTION_MEMORY_LIMIT,
357 (unsigned int) GNUNET_SERVER_MAX_MESSAGE_SIZE,
358 MHD_OPTION_CONNECTION_LIMIT,
360 MHD_OPTION_PER_IP_CONNECTION_LIMIT,
362 MHD_OPTION_NOTIFY_COMPLETED,
363 &requestCompletedCallback, NULL,
368 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Starting MHD on port %u with IPv6 disabled\n",port);
369 http_daemon = MHD_start_daemon (MHD_NO_FLAG,
371 &acceptPolicyCallback,
372 NULL, &accessHandlerCallback, NULL,
373 MHD_OPTION_CONNECTION_TIMEOUT,
374 (unsigned int) HTTP_TIMEOUT,
375 MHD_OPTION_CONNECTION_MEMORY_LIMIT,
376 (unsigned int) GNUNET_SERVER_MAX_MESSAGE_SIZE,
377 MHD_OPTION_CONNECTION_LIMIT,
379 MHD_OPTION_PER_IP_CONNECTION_LIMIT,
381 MHD_OPTION_NOTIFY_COMPLETED,
382 &requestCompletedCallback, NULL,
387 curl_multi = curl_multi_init ();
389 if ( (NULL == http_daemon) || (NULL == curl_multi))
391 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Initializing http plugin failed\n");
400 * Exit point from the plugin.
403 libgnunet_plugin_transport_http_done (void *cls)
405 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
406 struct Plugin *plugin = api->cls;
408 if (http_daemon != NULL)
410 MHD_stop_daemon (http_daemon);
414 curl_multi_cleanup (curl_multi);
417 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Shutting down http plugin...\n");
419 GNUNET_free (plugin);
424 /* end of plugin_transport_http.c */