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 dv/plugin_transport_dv.c
23 * @brief DV transport service, takes incoming DV requests and deals with
25 * @author Christian Grothoff
31 * As a start, the dv plugin needs to listen for information from the dv
32 * service. The plugin (?) will be notified by core (?) when a tcp/udp/whatever
33 * message comes in that should be for dv. The plugin will then hand off the message
34 * to the dv service which will decrypt/validate the message (?) and then send the
35 * result back to us (the transport) which will then send the message to the transport
38 * Or, core will notify the dv service directly which will validate,
39 * etc. and then just send a message to us.
41 * For starters, this plugin needs to have a client which will listen for messages from
42 * the dv service that need to be sent up to the gnunet-transport-service.
44 * Messages sent from the dv transport get passed to the dv service which deals
45 * with the actual sending (how much state does this transport need? should it know
46 * which peers it is currently connected to and their distances, or just assume that
47 * anything should be passed along to the dv service?).
50 #include "gnunet_protocols.h"
51 #include "gnunet_connection_lib.h"
52 #include "gnunet_server_lib.h"
53 #include "gnunet_service_lib.h"
54 #include "gnunet_statistics_service.h"
55 #include "gnunet_dv_service.h"
56 #include "gnunet_transport_service.h"
57 #include "../transport/plugin_transport.h"
60 #define DEBUG_TEMPLATE GNUNET_NO
63 * Encapsulation of all of the state of the plugin.
69 * Session handle for connections.
75 * Stored in a linked list.
80 * Pointer to the global plugin struct.
82 struct Plugin *plugin;
85 * The client (used to identify this connection)
90 * Continuation function to call once the transmission buffer
91 * has again space available. NULL if there is no
92 * continuation to call.
94 GNUNET_TRANSPORT_TransmitContinuation transmit_cont;
97 * Closure for transmit_cont.
99 void *transmit_cont_cls;
102 * To whom are we talking to (set to our identity
103 * if we are still waiting for the welcome message)
105 struct GNUNET_PeerIdentity sender;
108 * At what time did we reset last_received last?
110 struct GNUNET_TIME_Absolute last_quota_update;
113 * How many bytes have we received since the "last_quota_update"
116 uint64_t last_received;
119 * Number of bytes per ms that this peer is allowed
127 * Encapsulation of all of the state of the plugin.
134 struct GNUNET_TRANSPORT_PluginEnvironment *env;
137 * List of open sessions.
139 struct Session *sessions;
142 * Handle for the statistics service.
144 struct GNUNET_STATISTICS_Handle *statistics;
149 //struct GNUNET_SERVER_Handle *server;
152 * Handle to the running service.
154 //struct GNUNET_SERVICE_Context *service;
157 * Copy of the handler array where the closures are
158 * set to this struct's instance.
160 struct GNUNET_SERVER_MessageHandler *handlers;
163 * Handle to the DV service
165 struct GNUNET_DV_Handle *dv_handle;
169 void handle_dv_message_received (void *cls,
170 struct GNUNET_PeerIdentity *sender,
173 unsigned int distance,
174 char *sender_address,
175 size_t sender_address_len)
177 struct Plugin *plugin = cls;
179 /* TODO: Add in demultiplexing if we think we'll be receiving multiple messages at once */
180 plugin->env->receive(plugin,
182 (struct GNUNET_MessageHeader *)msg,
190 /* Question: how does the transport service learn of a newly connected (gossipped about)
191 * DV peer? Should the plugin (here) create a HELLO for that peer and send it along,
192 * or should the DV service create a HELLO and send it to us via the other part?
196 * Function that can be used by the transport service to transmit
197 * a message using the plugin.
200 * @param target who should receive this message
201 * @param priority how important is the message
202 * @param msgbuf the message to transmit
203 * @param msgbuf_size number of bytes in 'msgbuf'
204 * @param timeout when should we time out
205 * @param addr the address to use (can be NULL if the plugin
206 * is "on its own" (i.e. re-use existing TCP connection))
207 * @param addrlen length of the address in bytes
208 * @param force_address GNUNET_YES if the plugin MUST use the given address,
209 * otherwise the plugin may use other addresses or
210 * existing connections (if available)
211 * @param cont continuation to call once the message has
212 * been transmitted (or if the transport is ready
213 * for the next transmission call; or if the
214 * peer disconnected...)
215 * @param cont_cls closure for cont
216 * @return number of bytes used (on the physical network, with overheads);
217 * -1 on hard errors (i.e. address invalid); 0 is a legal value
218 * and does NOT mean that the message was not transmitted (DV)
221 dv_plugin_send (void *cls,
222 const struct GNUNET_PeerIdentity *target,
225 unsigned int priority,
226 struct GNUNET_TIME_Relative timeout,
230 GNUNET_TRANSPORT_TransmitContinuation
231 cont, void *cont_cls)
234 struct Plugin *plugin = cls;
236 /* FIXME: do we want the dv plugin to remember sent messages to call continuation once message actually goes out?
237 * Or do we just call the continuation once we've notified the plugin?
239 ret = GNUNET_DV_send(plugin->dv_handle,
247 /*, cont, cont_cls);*/
250 cont(cls, target, GNUNET_OK);
252 cont(cls, target, GNUNET_SYSERR);
260 * Function that can be used to force the plugin to disconnect
261 * from the given peer and cancel all previous transmissions
262 * (and their continuations).
265 * @param target peer from which to disconnect
268 dv_plugin_disconnect (void *cls,
269 const struct GNUNET_PeerIdentity *target)
271 // struct Plugin *plugin = cls;
272 // TODO: Add message type to send to dv service to "disconnect" a peer
277 * Convert the transports address to a nice, human-readable
281 * @param type name of the transport that generated the address
282 * @param addr one of the addresses of the host, NULL for the last address
283 * the specific address format depends on the transport
284 * @param addrlen length of the address
285 * @param numeric should (IP) addresses be displayed in numeric form?
286 * @param timeout after how long should we give up?
287 * @param asc function to call on each string
288 * @param asc_cls closure for asc
291 dv_plugin_address_pretty_printer (void *cls,
296 struct GNUNET_TIME_Relative timeout,
297 GNUNET_TRANSPORT_AddressStringCallback
306 * Another peer has suggested an address for this
307 * peer and transport plugin. Check that this could be a valid
308 * address. If so, consider adding it to the list
312 * @param addr pointer to the address
313 * @param addrlen length of addr
314 * @return GNUNET_OK if this is a plausible address for this peer
317 * FIXME: does this mean anything for the DV plugin?
320 dv_plugin_address_suggested (void *cls,
321 void *addr, size_t addrlen)
323 /* struct Plugin *plugin = cls; */
325 /* check if the address is plausible; if so,
326 add it to our list! */
332 * Entry point for the plugin.
335 libgnunet_plugin_transport_dv_init (void *cls)
337 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
338 struct GNUNET_TRANSPORT_PluginFunctions *api;
339 struct Plugin *plugin;
340 /*struct GNUNET_SERVICE_Context *service;*/
343 * Do we not even need a service for this thing? That's peculiar.
346 service = GNUNET_SERVICE_start ("transport-dv", env->sched, env->cfg);
349 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING,
352 ("Failed to start service for `%s' transport plugin.\n"),
358 * I don't think we need a port, the only way we get stuff is being directly
359 * called by service transport or by responses from the dv-service via our
364 GNUNET_CONFIGURATION_get_value_number (env->cfg,
369 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
372 ("Require valid port number for service `%s' in configuration!\n"),
374 GNUNET_SERVICE_stop (service);
379 plugin = GNUNET_malloc (sizeof (struct Plugin));
381 plugin->statistics = NULL;
382 //plugin->service = service;
383 //plugin->server = GNUNET_SERVICE_get_server (service);
385 plugin->dv_handle = GNUNET_DV_connect(env->sched, env->cfg, &handle_dv_message_received, plugin);
387 if (plugin->dv_handle == NULL)
393 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
395 api->send = &dv_plugin_send;
396 api->disconnect = &dv_plugin_disconnect;
397 api->address_pretty_printer = &dv_plugin_address_pretty_printer;
398 api->check_address = &dv_plugin_address_suggested;
404 * Exit point from the plugin.
407 libgnunet_plugin_transport_dv_done (void *cls)
409 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
410 struct Plugin *plugin = api->cls;
412 GNUNET_free (plugin);
417 /* end of plugin_transport_template.c */