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 3, 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 "gnunet_transport_plugin.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;
128 * Function that can be used by the transport service to transmit
129 * a message using the plugin.
132 * @param target who should receive this message
133 * @param priority how important is the message
134 * @param msgbuf the message to transmit
135 * @param msgbuf_size number of bytes in 'msgbuf'
136 * @param timeout when should we time out
137 * @param session which session must be used (or NULL for "any")
138 * @param addr the address to use (can be NULL if the plugin
139 * is "on its own" (i.e. re-use existing TCP connection))
140 * @param addrlen length of the address in bytes
141 * @param force_address GNUNET_YES if the plugin MUST use the given address,
142 * otherwise the plugin may use other addresses or
143 * existing connections (if available)
144 * @param cont continuation to call once the message has
145 * been transmitted (or if the transport is ready
146 * for the next transmission call; or if the
147 * peer disconnected...)
148 * @param cont_cls closure for cont
149 * @return number of bytes used (on the physical network, with overheads);
150 * -1 on hard errors (i.e. address invalid); 0 is a legal value
151 * and does NOT mean that the message was not transmitted (DV)
154 template_plugin_send (void *cls,
155 const struct GNUNET_PeerIdentity *
159 unsigned int priority,
160 struct GNUNET_TIME_Relative timeout,
161 struct Session *session,
165 GNUNET_TRANSPORT_TransmitContinuation
166 cont, void *cont_cls)
169 /* struct Plugin *plugin = cls; */
176 * Function that can be used to force the plugin to disconnect
177 * from the given peer and cancel all previous transmissions
178 * (and their continuationc).
181 * @param target peer from which to disconnect
184 template_plugin_disconnect (void *cls,
185 const struct GNUNET_PeerIdentity *target)
187 // struct Plugin *plugin = cls;
193 * Convert the transports address to a nice, human-readable
197 * @param type name of the transport that generated the address
198 * @param addr one of the addresses of the host, NULL for the last address
199 * the specific address format depends on the transport
200 * @param addrlen length of the address
201 * @param numeric should (IP) addresses be displayed in numeric form?
202 * @param timeout after how long should we give up?
203 * @param asc function to call on each string
204 * @param asc_cls closure for asc
207 template_plugin_address_pretty_printer (void *cls,
212 struct GNUNET_TIME_Relative timeout,
213 GNUNET_TRANSPORT_AddressStringCallback
222 * Another peer has suggested an address for this
223 * peer and transport plugin. Check that this could be a valid
224 * address. If so, consider adding it to the list
228 * @param addr pointer to the address
229 * @param addrlen length of addr
230 * @return GNUNET_OK if this is a plausible address for this peer
234 template_plugin_address_suggested (void *cls,
238 /* struct Plugin *plugin = cls; */
240 /* check if the address is plausible; if so,
241 add it to our list! */
247 * Function called for a quick conversion of the binary address to
248 * a numeric address. Note that the caller must not free the
249 * address and that the next call to this function is allowed
250 * to override the address again.
253 * @param addr binary address
254 * @param addrlen length of the address
255 * @return string representing the same address
258 template_plugin_address_to_string (void *cls,
270 * Entry point for the plugin.
273 gnunet_plugin_transport_template_init (void *cls)
275 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
276 struct GNUNET_TRANSPORT_PluginFunctions *api;
277 struct Plugin *plugin;
279 plugin = GNUNET_malloc (sizeof (struct Plugin));
281 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
283 api->send = &template_plugin_send;
284 api->disconnect = &template_plugin_disconnect;
285 api->address_pretty_printer = &template_plugin_address_pretty_printer;
286 api->check_address = &template_plugin_address_suggested;
287 api->address_to_string = &template_plugin_address_to_string;
293 * Exit point from the plugin.
296 gnunet_plugin_transport_template_done (void *cls)
298 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
299 struct Plugin *plugin = api->cls;
301 GNUNET_free (plugin);
306 /* end of plugin_transport_template.c */