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"
37 #define VERBOSE GNUNET_YES
38 #define DEBUG GNUNET_YES
41 * After how long do we expire an address that we
42 * learned from another peer if it is not reconfirmed
45 #define LEARNED_ADDRESS_EXPIRATION GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_HOURS, 6)
47 #define HTTP_TIMEOUT 600
50 * Encapsulation of all of the state of the plugin.
56 * Session handle for connections.
62 * Stored in a linked list.
67 * Pointer to the global plugin struct.
69 struct Plugin *plugin;
72 * The client (used to identify this connection)
77 * Continuation function to call once the transmission buffer
78 * has again space available. NULL if there is no
79 * continuation to call.
81 GNUNET_TRANSPORT_TransmitContinuation transmit_cont;
84 * Closure for transmit_cont.
86 void *transmit_cont_cls;
89 * To whom are we talking to (set to our identity
90 * if we are still waiting for the welcome message)
92 struct GNUNET_PeerIdentity sender;
95 * At what time did we reset last_received last?
97 struct GNUNET_TIME_Absolute last_quota_update;
100 * How many bytes have we received since the "last_quota_update"
103 uint64_t last_received;
106 * Number of bytes per ms that this peer is allowed
114 * Encapsulation of all of the state of the plugin.
121 struct GNUNET_TRANSPORT_PluginEnvironment *env;
124 * List of open sessions.
126 struct Session *sessions;
129 * Handle for the statistics service.
131 struct GNUNET_STATISTICS_Handle *statistics;
136 * Daemon for listening for new connections.
138 static struct MHD_Daemon *http_daemon;
141 * Function that can be used by the transport service to transmit
142 * a message using the plugin.
145 * @param target who should receive this message
146 * @param priority how important is the message
147 * @param msgbuf the message to transmit
148 * @param msgbuf_size number of bytes in 'msgbuf'
149 * @param timeout when should we time out
150 * @param session which session must be used (or NULL for "any")
151 * @param addr the address to use (can be NULL if the plugin
152 * is "on its own" (i.e. re-use existing TCP connection))
153 * @param addrlen length of the address in bytes
154 * @param force_address GNUNET_YES if the plugin MUST use the given address,
155 * otherwise the plugin may use other addresses or
156 * existing connections (if available)
157 * @param cont continuation to call once the message has
158 * been transmitted (or if the transport is ready
159 * for the next transmission call; or if the
160 * peer disconnected...)
161 * @param cont_cls closure for cont
162 * @return number of bytes used (on the physical network, with overheads);
163 * -1 on hard errors (i.e. address invalid); 0 is a legal value
164 * and does NOT mean that the message was not transmitted (DV)
167 http_plugin_send (void *cls,
168 const struct GNUNET_PeerIdentity *
172 unsigned int priority,
173 struct GNUNET_TIME_Relative timeout,
174 struct Session *session,
178 GNUNET_TRANSPORT_TransmitContinuation
179 cont, void *cont_cls)
182 /* struct Plugin *plugin = cls; */
189 * Function that can be used to force the plugin to disconnect
190 * from the given peer and cancel all previous transmissions
191 * (and their continuationc).
194 * @param target peer from which to disconnect
197 http_plugin_disconnect (void *cls,
198 const struct GNUNET_PeerIdentity *target)
200 // struct Plugin *plugin = cls;
207 * Convert the transports address to a nice, human-readable
211 * @param type name of the transport that generated the address
212 * @param addr one of the addresses of the host, NULL for the last address
213 * the specific address format depends on the transport
214 * @param addrlen length of the address
215 * @param numeric should (IP) addresses be displayed in numeric form?
216 * @param timeout after how long should we give up?
217 * @param asc function to call on each string
218 * @param asc_cls closure for asc
221 http_plugin_address_pretty_printer (void *cls,
226 struct GNUNET_TIME_Relative timeout,
227 GNUNET_TRANSPORT_AddressStringCallback
236 * Another peer has suggested an address for this
237 * peer and transport plugin. Check that this could be a valid
238 * address. If so, consider adding it to the list
242 * @param addr pointer to the address
243 * @param addrlen length of addr
244 * @return GNUNET_OK if this is a plausible address for this peer
248 http_plugin_address_suggested (void *cls,
249 void *addr, size_t addrlen)
251 /* struct Plugin *plugin = cls; */
253 /* check if the address is plausible; if so,
254 add it to our list! */
259 * Check if we are allowed to connect to the given IP.
262 acceptPolicyCallback (void *cls,
263 const struct sockaddr *addr, socklen_t addr_len)
269 * Process GET or PUT request received via MHD. For
270 * GET, queue response that will send back our pending
271 * messages. For PUT, process incoming data and send
272 * to GNUnet core. In either case, check if a session
273 * already exists and create a new one if not.
276 accessHandlerCallback (void *cls,
277 struct MHD_Connection *session,
281 const char *upload_data,
282 size_t * upload_data_size, void **httpSessionCache)
288 * MHD is done handling a request. Cleanup
289 * the respective transport state.
292 requestCompletedCallback (void *unused,
293 struct MHD_Connection *session,
294 void **httpSessionCache)
300 * Entry point for the plugin.
303 libgnunet_plugin_transport_http_init (void *cls)
305 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
306 struct GNUNET_TRANSPORT_PluginFunctions *api;
307 struct Plugin *plugin;
308 long long unsigned int port;
311 plugin = GNUNET_malloc (sizeof (struct Plugin));
313 plugin->statistics = NULL;
314 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
316 api->send = &http_plugin_send;
317 api->disconnect = &http_plugin_disconnect;
318 api->address_pretty_printer = &http_plugin_address_pretty_printer;
319 api->check_address = &http_plugin_address_suggested;
321 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Starting http plugin...\n");
322 /* Reading port number from config file */
324 GNUNET_CONFIGURATION_get_value_number (env->cfg,
330 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
333 ("Require valid port number for service `%s' in configuration!\n"),
337 use_ipv6 = GNUNET_YES;
338 use_ipv6 = GNUNET_CONFIGURATION_get_value_yesno (env->cfg, "transport-http","USE_IPV6");
339 if ((http_daemon == NULL) && (port != 0))
341 if ( use_ipv6 == GNUNET_YES)
343 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"HTTP Daemon could not started, http plugin not working\n");
344 http_daemon = MHD_start_daemon (MHD_USE_IPv6,
346 &acceptPolicyCallback,
347 NULL, &accessHandlerCallback, NULL,
348 MHD_OPTION_CONNECTION_TIMEOUT,
349 (unsigned int) HTTP_TIMEOUT,
350 MHD_OPTION_CONNECTION_MEMORY_LIMIT,
351 (unsigned int) GNUNET_SERVER_MAX_MESSAGE_SIZE,
352 MHD_OPTION_CONNECTION_LIMIT,
354 MHD_OPTION_PER_IP_CONNECTION_LIMIT,
356 MHD_OPTION_NOTIFY_COMPLETED,
357 &requestCompletedCallback, NULL,
362 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Starting MHD on port %u with IPv6 disabled\n",port);
363 http_daemon = MHD_start_daemon (MHD_NO_FLAG,
365 &acceptPolicyCallback,
366 NULL, &accessHandlerCallback, NULL,
367 MHD_OPTION_CONNECTION_TIMEOUT,
368 (unsigned int) HTTP_TIMEOUT,
369 MHD_OPTION_CONNECTION_MEMORY_LIMIT,
370 (unsigned int) GNUNET_SERVER_MAX_MESSAGE_SIZE,
371 MHD_OPTION_CONNECTION_LIMIT,
373 MHD_OPTION_PER_IP_CONNECTION_LIMIT,
375 MHD_OPTION_NOTIFY_COMPLETED,
376 &requestCompletedCallback, NULL,
380 if ( NULL != http_daemon )
384 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,"Starting MHD on port %u with IPv6 disabled\n",port);
391 * Exit point from the plugin.
394 libgnunet_plugin_transport_http_done (void *cls)
396 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
397 struct Plugin *plugin = api->cls;
399 if (http_daemon != NULL)
401 MHD_stop_daemon (http_daemon);
404 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Shutting down http plugin...\n");
406 GNUNET_free (plugin);
411 /* end of plugin_transport_http.c */