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_template.c
23 * @brief template for a new transport service
24 * @author Christian Grothoff
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 DEBUG_HTTP GNUNET_NO
40 * Text of the response sent back after the last bytes of a PUT
41 * request have been received (just to formally obey the HTTP
44 #define HTTP_PUT_RESPONSE "Thank you!"
48 * After how long do we expire an address that we
49 * learned from another peer if it is not reconfirmed
52 #define LEARNED_ADDRESS_EXPIRATION GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_HOURS, 6)
56 * Encapsulation of all of the state of the plugin.
62 * Session handle for connections.
68 * Stored in a linked list.
73 * Pointer to the global plugin struct.
75 struct Plugin *plugin;
78 * The client (used to identify this connection)
83 * Continuation function to call once the transmission buffer
84 * has again space available. NULL if there is no
85 * continuation to call.
87 GNUNET_TRANSPORT_TransmitContinuation transmit_cont;
90 * Closure for transmit_cont.
92 void *transmit_cont_cls;
95 * To whom are we talking to (set to our identity
96 * if we are still waiting for the welcome message)
98 struct GNUNET_PeerIdentity sender;
101 * At what time did we reset last_received last?
103 struct GNUNET_TIME_Absolute last_quota_update;
106 * How many bytes have we received since the "last_quota_update"
109 uint64_t last_received;
112 * Number of bytes per ms that this peer is allowed
120 * Encapsulation of all of the state of the plugin.
127 struct GNUNET_TRANSPORT_PluginEnvironment *env;
130 * List of open sessions.
132 struct Session *sessions;
137 * Daemon for listening for new IPv4 connections.
139 static struct MHD_Daemon *http_daemon_v4;
142 * Daemon for listening for new IPv6connections.
144 static struct MHD_Daemon *http_daemon_v6;
147 * Our primary task for http daemon handling IPv4 connections
149 static GNUNET_SCHEDULER_TaskIdentifier http_task_v4;
152 * Our primary task for http daemon handling IPv6 connections
154 static GNUNET_SCHEDULER_TaskIdentifier http_task_v6;
156 struct Plugin *plugin;
159 * Check if we are allowed to connect to the given IP.
162 acceptPolicyCallback (void *cls,
163 const struct sockaddr *addr, socklen_t addr_len)
165 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Incoming connection \n");
166 /* Currently all incoming connections are accepted, so nothing to do here */
171 * Process GET or PUT request received via MHD. For
172 * GET, queue response that will send back our pending
173 * messages. For PUT, process incoming data and send
174 * to GNUnet core. In either case, check if a session
175 * already exists and create a new one if not.
178 accessHandlerCallback (void *cls,
179 struct MHD_Connection *session,
183 const char *upload_data,
184 size_t * upload_data_size, void **httpSessionCache)
186 struct MHD_Response *response;
188 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"HTTP Daemon has an incoming `%s' request from \n",method);
190 /* Find out if session exists, otherwise create one */
192 /* Is it a PUT or a GET request */
193 if ( 0 == strcmp (MHD_HTTP_METHOD_PUT, method) )
195 /* PUT method here */
196 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Got PUT Request with size %u \n",upload_data_size);
197 //GNUNET_STATISTICS_update( plugin->env->stats , gettext_noop("# PUT requests"), 1, GNUNET_NO);
199 if ( 0 == strcmp (MHD_HTTP_METHOD_GET, method) )
201 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Got GET Request with size\n");
202 //GNUNET_STATISTICS_update( plugin->env->stats , gettext_noop("# GET requests"), 1, GNUNET_NO);
205 response = MHD_create_response_from_data (strlen (HTTP_PUT_RESPONSE),
206 HTTP_PUT_RESPONSE, MHD_NO, MHD_NO);
207 MHD_queue_response (session, MHD_HTTP_OK, response);
208 MHD_destroy_response (response);
214 * Call MHD to process pending requests and then go back
215 * and schedule the next run.
217 static void run_daemon (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
220 * Function that queries MHD's select sets and
221 * starts the task waiting for them.
223 static GNUNET_SCHEDULER_TaskIdentifier
224 prepare_daemon (struct MHD_Daemon *daemon_handle)
226 GNUNET_SCHEDULER_TaskIdentifier ret;
230 struct GNUNET_NETWORK_FDSet *wrs;
231 struct GNUNET_NETWORK_FDSet *wws;
232 struct GNUNET_NETWORK_FDSet *wes;
234 unsigned long long timeout;
236 struct GNUNET_TIME_Relative tv;
241 wrs = GNUNET_NETWORK_fdset_create ();
242 wes = GNUNET_NETWORK_fdset_create ();
243 wws = GNUNET_NETWORK_fdset_create ();
245 GNUNET_assert (MHD_YES ==
246 MHD_get_fdset (daemon_handle,
251 haveto = MHD_get_timeout (daemon_handle, &timeout);
252 if (haveto == MHD_YES)
253 tv.value = (uint64_t) timeout;
255 tv = GNUNET_TIME_UNIT_FOREVER_REL;
256 GNUNET_NETWORK_fdset_copy_native (wrs, &rs, max);
257 GNUNET_NETWORK_fdset_copy_native (wws, &ws, max);
258 GNUNET_NETWORK_fdset_copy_native (wes, &es, max);
259 ret = GNUNET_SCHEDULER_add_select (plugin->env->sched,
260 GNUNET_SCHEDULER_PRIORITY_HIGH,
261 GNUNET_SCHEDULER_NO_TASK,
267 GNUNET_NETWORK_fdset_destroy (wrs);
268 GNUNET_NETWORK_fdset_destroy (wws);
269 GNUNET_NETWORK_fdset_destroy (wes);
274 * Call MHD to process pending requests and then go back
275 * and schedule the next run.
278 run_daemon (void *cls,
279 const struct GNUNET_SCHEDULER_TaskContext *tc)
281 struct MHD_Daemon *daemon_handle = cls;
283 if (daemon_handle == http_daemon_v4)
284 http_task_v4 = GNUNET_SCHEDULER_NO_TASK;
286 if (daemon_handle == http_daemon_v6)
287 http_task_v6 = GNUNET_SCHEDULER_NO_TASK;
289 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
292 GNUNET_assert (MHD_YES == MHD_run (daemon_handle));
293 if (daemon_handle == http_daemon_v4)
294 http_task_v4 = prepare_daemon (daemon_handle);
295 if (daemon_handle == http_daemon_v6)
296 http_task_v6 = prepare_daemon (daemon_handle);
301 * Function that can be used by the transport service to transmit
302 * a message using the plugin.
305 * @param target who should receive this message
306 * @param priority how important is the message
307 * @param msgbuf the message to transmit
308 * @param msgbuf_size number of bytes in 'msgbuf'
309 * @param timeout when should we time out
310 * @param session which session must be used (or NULL for "any")
311 * @param addr the address to use (can be NULL if the plugin
312 * is "on its own" (i.e. re-use existing TCP connection))
313 * @param addrlen length of the address in bytes
314 * @param force_address GNUNET_YES if the plugin MUST use the given address,
315 * otherwise the plugin may use other addresses or
316 * existing connections (if available)
317 * @param cont continuation to call once the message has
318 * been transmitted (or if the transport is ready
319 * for the next transmission call; or if the
320 * peer disconnected...)
321 * @param cont_cls closure for cont
322 * @return number of bytes used (on the physical network, with overheads);
323 * -1 on hard errors (i.e. address invalid); 0 is a legal value
324 * and does NOT mean that the message was not transmitted (DV)
327 template_plugin_send (void *cls,
328 const struct GNUNET_PeerIdentity *
332 unsigned int priority,
333 struct GNUNET_TIME_Relative timeout,
334 struct Session *session,
338 GNUNET_TRANSPORT_TransmitContinuation
339 cont, void *cont_cls)
342 /* struct Plugin *plugin = cls; */
349 * Function that can be used to force the plugin to disconnect
350 * from the given peer and cancel all previous transmissions
351 * (and their continuationc).
354 * @param target peer from which to disconnect
357 template_plugin_disconnect (void *cls,
358 const struct GNUNET_PeerIdentity *target)
360 // struct Plugin *plugin = cls;
366 * Convert the transports address to a nice, human-readable
370 * @param type name of the transport that generated the address
371 * @param addr one of the addresses of the host, NULL for the last address
372 * the specific address format depends on the transport
373 * @param addrlen length of the address
374 * @param numeric should (IP) addresses be displayed in numeric form?
375 * @param timeout after how long should we give up?
376 * @param asc function to call on each string
377 * @param asc_cls closure for asc
380 template_plugin_address_pretty_printer (void *cls,
385 struct GNUNET_TIME_Relative timeout,
386 GNUNET_TRANSPORT_AddressStringCallback
395 * Another peer has suggested an address for this
396 * peer and transport plugin. Check that this could be a valid
397 * address. If so, consider adding it to the list
401 * @param addr pointer to the address
402 * @param addrlen length of addr
403 * @return GNUNET_OK if this is a plausible address for this peer
407 template_plugin_address_suggested (void *cls,
408 void *addr, size_t addrlen)
410 /* struct Plugin *plugin = cls; */
412 /* check if the address is plausible; if so,
413 add it to our list! */
419 * Function called for a quick conversion of the binary address to
420 * a numeric address. Note that the caller must not free the
421 * address and that the next call to this function is allowed
422 * to override the address again.
425 * @param addr binary address
426 * @param addrlen length of the address
427 * @return string representing the same address
430 template_plugin_address_to_string (void *cls,
439 * Exit point from the plugin.
442 libgnunet_plugin_transport_http_done (void *cls)
444 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
445 struct Plugin *plugin = api->cls;
447 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Unloading http plugin...\n");
449 if ( http_task_v4 != GNUNET_SCHEDULER_NO_TASK)
451 GNUNET_SCHEDULER_cancel(plugin->env->sched, http_task_v4);
452 http_task_v4 = GNUNET_SCHEDULER_NO_TASK;
455 if ( http_task_v6 != GNUNET_SCHEDULER_NO_TASK)
457 GNUNET_SCHEDULER_cancel(plugin->env->sched, http_task_v6);
458 http_task_v6 = GNUNET_SCHEDULER_NO_TASK;
461 if (http_daemon_v4 != NULL)
463 MHD_stop_daemon (http_daemon_v4);
464 http_daemon_v4 = NULL;
466 if (http_daemon_v6 != NULL)
468 MHD_stop_daemon (http_daemon_v6);
469 http_daemon_v6 = NULL;
472 GNUNET_free (plugin);
479 * Entry point for the plugin.
482 libgnunet_plugin_transport_http_init (void *cls)
484 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
485 struct GNUNET_TRANSPORT_PluginFunctions *api;
487 long long unsigned int port;
489 plugin = GNUNET_malloc (sizeof (struct Plugin));
491 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
493 api->send = &template_plugin_send;
494 api->disconnect = &template_plugin_disconnect;
495 api->address_pretty_printer = &template_plugin_address_pretty_printer;
496 api->check_address = &template_plugin_address_suggested;
497 api->address_to_string = &template_plugin_address_to_string;
499 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Starting http plugin...\n");
500 /* Reading port number from config file */
502 GNUNET_CONFIGURATION_get_value_number (env->cfg,
508 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
511 ("Require valid port number for transport plugin `%s' in configuration!\n"),
513 libgnunet_plugin_transport_http_done (api);
517 if ((http_daemon_v4 == NULL) && (http_daemon_v6 == NULL) && (port != 0))
519 http_daemon_v6 = MHD_start_daemon (MHD_USE_IPv6,
521 &acceptPolicyCallback,
522 NULL, &accessHandlerCallback, NULL,
523 MHD_OPTION_CONNECTION_LIMIT, (unsigned int) 16,
524 MHD_OPTION_PER_IP_CONNECTION_LIMIT, (unsigned int) 1,
525 MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) 16,
526 MHD_OPTION_CONNECTION_MEMORY_LIMIT, (size_t) (16 * 1024),
528 http_daemon_v4 = MHD_start_daemon (MHD_NO_FLAG,
530 &acceptPolicyCallback,
531 NULL, &accessHandlerCallback, NULL,
532 MHD_OPTION_CONNECTION_LIMIT, (unsigned int) 16,
533 MHD_OPTION_PER_IP_CONNECTION_LIMIT, (unsigned int) 1,
534 MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) 16,
535 MHD_OPTION_CONNECTION_MEMORY_LIMIT, (size_t) (16 * 1024),
539 if (http_daemon_v4 != NULL)
540 http_task_v4 = prepare_daemon (http_daemon_v4);
541 if (http_daemon_v6 != NULL)
542 http_task_v6 = prepare_daemon (http_daemon_v6);
544 if ((http_daemon_v4 == NULL) || (http_daemon_v6 != NULL))
545 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Starting MHD on port %u\n",port);
550 /* end of plugin_transport_template.c */