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"
36 #define DEBUG_TEMPLATE GNUNET_NO
39 * After how long do we expire an address that we
40 * learned from another peer if it is not reconfirmed
43 #define LEARNED_ADDRESS_EXPIRATION GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_HOURS, 6)
47 * Encapsulation of all of the state of the plugin.
53 * Session handle for connections.
59 * Stored in a linked list.
64 * Pointer to the global plugin struct.
66 struct Plugin *plugin;
69 * The client (used to identify this connection)
74 * Continuation function to call once the transmission buffer
75 * has again space available. NULL if there is no
76 * continuation to call.
78 GNUNET_TRANSPORT_TransmitContinuation transmit_cont;
81 * Closure for transmit_cont.
83 void *transmit_cont_cls;
86 * To whom are we talking to (set to our identity
87 * if we are still waiting for the welcome message)
89 struct GNUNET_PeerIdentity sender;
92 * At what time did we reset last_received last?
94 struct GNUNET_TIME_Absolute last_quota_update;
97 * How many bytes have we received since the "last_quota_update"
100 uint64_t last_received;
103 * Number of bytes per ms that this peer is allowed
111 * Encapsulation of all of the state of the plugin.
118 struct GNUNET_TRANSPORT_PluginEnvironment *env;
121 * List of open sessions.
123 struct Session *sessions;
126 * Handle for the statistics service.
128 struct GNUNET_STATISTICS_Handle *statistics;
135 * Function that can be used by the transport service to validate that
136 * another peer is reachable at a particular address (even if we
137 * already have a connection to this peer, this function is required
138 * to establish a new one).
141 * @param target who should receive this message
142 * @param challenge challenge code to use
143 * @param addrlen length of the address
144 * @param addr the address
145 * @param timeout how long should we try to transmit these?
146 * @return GNUNET_OK if the transmission has been scheduled
149 template_plugin_validate (void *cls,
150 const struct GNUNET_PeerIdentity *target,
152 struct GNUNET_TIME_Relative timeout,
157 return GNUNET_SYSERR;
162 * Function that can be used by the transport service to transmit
163 * a message using the plugin.
166 * @param plugin_context value we were asked to pass to this plugin
167 * to respond to the given peer (use is optional,
168 * but may speed up processing), can be NULL
169 * @param service_context value passed to the transport-service
170 * to identify the neighbour
171 * @param target who should receive this message
172 * @param priority how important is the message
173 * @param msg the message to transmit
174 * @param timeout when should we time out
175 * @param cont continuation to call once the message has
176 * been transmitted (or if the transport is ready
177 * for the next transmission call; or if the
178 * peer disconnected...)
179 * @param cont_cls closure for cont
180 * @return plugin_context that should be used next time for
181 * sending messages to the specified peer
184 template_plugin_send (void *cls,
185 void *plugin_context,
186 struct ReadyList *service_context,
187 const struct GNUNET_PeerIdentity *target,
188 unsigned int priority,
189 const struct GNUNET_MessageHeader *msg,
190 struct GNUNET_TIME_Relative timeout,
191 GNUNET_TRANSPORT_TransmitContinuation cont,
194 // struct Plugin *plugin = cls;
203 * @param plugin_context value we were asked to pass to this plugin
204 * to respond to the given peer (use is optional,
205 * but may speed up processing), can be NULL (if
206 * NULL was returned from the transmit function)
207 * @param service_context must correspond to the service context
208 * of the corresponding Transmit call; the plugin should
209 * not cancel a send call made with a different service
210 * context pointer! Never NULL.
211 * @param target peer for which the last transmission is
215 template_plugin_cancel (void *cls,
216 void *plugin_context,
217 struct ReadyList *service_context,
218 const struct GNUNET_PeerIdentity *target)
220 // struct Plugin *plugin = cls;
226 * Convert the transports address to a nice, human-readable
230 * @param type name of the transport that generated the address
231 * @param addr one of the addresses of the host, NULL for the last address
232 * the specific address format depends on the transport
233 * @param addrlen length of the address
234 * @param numeric should (IP) addresses be displayed in numeric form?
235 * @param timeout after how long should we give up?
236 * @param asc function to call on each string
237 * @param asc_cls closure for asc
240 template_plugin_address_pretty_printer (void *cls,
245 struct GNUNET_TIME_Relative timeout,
246 GNUNET_TRANSPORT_AddressStringCallback
253 * Set a quota for receiving data from the given peer; this is a
254 * per-transport limit. The transport should limit its read/select
255 * calls to stay below the quota (in terms of incoming data).
258 * @param target the peer for whom the quota is given
259 * @param quota_in quota for receiving/sending data in bytes per ms
262 template_plugin_set_receive_quota (void *cls,
263 const struct GNUNET_PeerIdentity *target,
266 // struct Plugin *plugin = cls;
272 * Another peer has suggested an address for this
273 * peer and transport plugin. Check that this could be a valid
274 * address. If so, consider adding it to the list
278 * @param addr pointer to the address
279 * @param addrlen length of addr
280 * @return GNUNET_OK if this is a plausible address for this peer
284 template_plugin_address_suggested (void *cls,
285 const void *addr, size_t addrlen)
287 // struct Plugin *plugin = cls;
289 /* check if the address is plausible; if so,
290 add it to our list! */
297 * Entry point for the plugin.
300 gnunet_plugin_transport_template_init (void *cls)
302 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
303 struct GNUNET_TRANSPORT_PluginFunctions *api;
304 struct Plugin *plugin;
306 plugin = GNUNET_malloc (sizeof (struct Plugin));
308 plugin->statistics = NULL;
309 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
311 api->validate = &template_plugin_validate;
312 api->send = &template_plugin_send;
313 api->cancel = &template_plugin_cancel;
314 api->address_pretty_printer = &template_plugin_address_pretty_printer;
315 api->set_receive_quota = &template_plugin_set_receive_quota;
316 api->address_suggested = &template_plugin_address_suggested;
317 api->cost_estimate = 42; // FIXME
323 * Exit point from the plugin.
326 gnunet_plugin_transport_template_done (void *cls)
328 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
329 struct Plugin *plugin = api->cls;
331 GNUNET_free (plugin);
336 /* end of plugin_transport_template.c */