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_util_lib.h"
29 #include "gnunet_protocols.h"
30 #include "gnunet_statistics_service.h"
31 #include "gnunet_transport_service.h"
32 #include "gnunet_transport_plugin.h"
34 #define LOG(kind,...) GNUNET_log_from (kind, "transport-template",__VA_ARGS__)
37 * After how long do we expire an address that we
38 * learned from another peer if it is not reconfirmed
41 #define LEARNED_ADDRESS_EXPIRATION GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_HOURS, 6)
43 #define PLUGIN_NAME "template"
46 * Encapsulation of all of the state of the plugin.
52 * Session handle for connections.
57 * To whom are we talking to (set to our identity
58 * if we are still waiting for the welcome message)
60 struct GNUNET_PeerIdentity sender;
63 * Stored in a linked list.
68 * Pointer to the global plugin struct.
70 struct Plugin *plugin;
73 * The client (used to identify this connection)
78 * Continuation function to call once the transmission buffer
79 * has again space available. NULL if there is no
80 * continuation to call.
82 GNUNET_TRANSPORT_TransmitContinuation transmit_cont;
85 * Closure for transmit_cont.
87 void *transmit_cont_cls;
90 * At what time did we reset last_received last?
92 struct GNUNET_TIME_Absolute last_quota_update;
95 * How many bytes have we received since the "last_quota_update"
98 uint64_t last_received;
101 * Number of bytes per ms that this peer is allowed
108 GNUNET_NETWORK_STRUCT_BEGIN
110 struct TemplateAddress
113 * Address options in NBO
115 uint32_t options GNUNET_PACKED;
117 /* Add address here */
120 GNUNET_NETWORK_STRUCT_END
123 * Encapsulation of all of the state of the plugin.
130 struct GNUNET_TRANSPORT_PluginEnvironment *env;
133 * List of open sessions.
135 struct Session *sessions;
138 * Options in HBO to be used with addresses
145 * Function that can be used by the transport service to transmit
146 * a message using the plugin. Note that in the case of a
147 * peer disconnecting, the continuation MUST be called
148 * prior to the disconnect notification itself. This function
149 * will be called with this peer's HELLO message to initiate
150 * a fresh connection to another peer.
153 * @param session which session must be used
154 * @param msgbuf the message to transmit
155 * @param msgbuf_size number of bytes in @a msgbuf
156 * @param priority how important is the message (most plugins will
157 * ignore message priority and just FIFO)
158 * @param to how long to wait at most for the transmission (does not
159 * require plugins to discard the message after the timeout,
160 * just advisory for the desired delay; most plugins will ignore
162 * @param cont continuation to call once the message has
163 * been transmitted (or if the transport is ready
164 * for the next transmission call; or if the
165 * peer disconnected...); can be NULL
166 * @param cont_cls closure for @a cont
167 * @return number of bytes used (on the physical network, with overheads);
168 * -1 on hard errors (i.e. address invalid); 0 is a legal value
169 * and does NOT mean that the message was not transmitted (DV)
172 template_plugin_send (void *cls,
173 struct Session *session,
174 const char *msgbuf, size_t msgbuf_size,
175 unsigned int priority,
176 struct GNUNET_TIME_Relative to,
177 GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
179 struct Plugin *plugin = cls;
182 GNUNET_assert (plugin != NULL);
183 GNUNET_assert (session != NULL);
185 /* struct Plugin *plugin = cls; */
191 * Function that can be used to force the plugin to disconnect
192 * from the given peer and cancel all previous transmissions
193 * (and their continuationc).
196 * @param target peer from which to disconnect
199 template_plugin_disconnect_peer (void *cls,
200 const struct GNUNET_PeerIdentity *target)
202 // struct Plugin *plugin = cls;
208 * Function that can be used to force the plugin to disconnect
209 * from the given peer and cancel all previous transmissions
210 * (and their continuationc).
213 * @param session session from which to disconnect
214 * @return #GNUNET_OK on success
217 template_plugin_disconnect_session (void *cls,
218 struct Session *session)
220 // struct Plugin *plugin = cls;
222 return GNUNET_SYSERR;
227 * Function obtain the network type for a session
229 * @param cls closure ('struct Plugin*')
230 * @param session the session
231 * @return the network type in HBO or GNUNET_SYSERR
233 static enum GNUNET_ATS_Network_Type
234 template_plugin_get_network (void *cls,
235 struct Session *session)
237 GNUNET_assert (NULL != session);
238 return GNUNET_ATS_NET_UNSPECIFIED; /* Change to correct network type */
243 * Convert the transports address to a nice, human-readable
247 * @param type name of the transport that generated the address
248 * @param addr one of the addresses of the host, NULL for the last address
249 * the specific address format depends on the transport
250 * @param addrlen length of the address
251 * @param numeric should (IP) addresses be displayed in numeric form?
252 * @param timeout after how long should we give up?
253 * @param asc function to call on each string
254 * @param asc_cls closure for asc
257 template_plugin_address_pretty_printer (void *cls, const char *type,
258 const void *addr, size_t addrlen,
260 struct GNUNET_TIME_Relative timeout,
261 GNUNET_TRANSPORT_AddressStringCallback
266 asc (asc_cls, TRANSPORT_SESSION_INBOUND_STRING);
274 * Another peer has suggested an address for this
275 * peer and transport plugin. Check that this could be a valid
276 * address. If so, consider adding it to the list
280 * @param addr pointer to the address
281 * @param addrlen length of addr
282 * @return #GNUNET_OK if this is a plausible address for this peer
286 template_plugin_address_suggested (void *cls, const void *addr, size_t addrlen)
288 /* struct Plugin *plugin = cls; */
290 /* check if the address is belonging to the plugin*/
296 * Function called for a quick conversion of the binary address to
297 * a numeric address. Note that the caller must not free the
298 * address and that the next call to this function is allowed
299 * to override the address again.
302 * @param addr binary address
303 * @param addrlen length of the address
304 * @return string representing the same address
307 template_plugin_address_to_string (void *cls, const void *addr, size_t addrlen)
310 * Print address in format template.options.address
315 return TRANSPORT_SESSION_INBOUND_STRING;
324 * Function called to convert a string address to
327 * @param cls closure ('struct Plugin*')
328 * @param addr string address
329 * @param addrlen length of the @a addr
330 * @param buf location to store the buffer
331 * @param added location to store the number of bytes in the buffer.
332 * If the function returns #GNUNET_SYSERR, its contents are undefined.
333 * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
336 template_plugin_string_to_address (void *cls,
339 void **buf, size_t *added)
342 * Parse string in format template.options.address
345 return GNUNET_SYSERR;
350 * Create a new session to transmit data to the target
351 * This session will used to send data to this peer and the plugin will
352 * notify us by calling the env->session_end function
355 * @param address pointer to the GNUNET_HELLO_Address
356 * @return the session if the address is valid, NULL otherwise
358 static struct Session *
359 template_plugin_get_session (void *cls,
360 const struct GNUNET_HELLO_Address *address)
368 * Entry point for the plugin.
371 libgnunet_plugin_transport_template_init (void *cls)
373 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
374 struct GNUNET_TRANSPORT_PluginFunctions *api;
375 struct Plugin *plugin;
377 if (NULL == env->receive)
379 /* run in 'stub' mode (i.e. as part of gnunet-peerinfo), don't fully
380 initialze the plugin or the API */
381 api = GNUNET_new (struct GNUNET_TRANSPORT_PluginFunctions);
383 api->address_to_string = &template_plugin_address_to_string;
384 api->string_to_address = &template_plugin_string_to_address;
385 api->address_pretty_printer = &template_plugin_address_pretty_printer;
389 plugin = GNUNET_new (struct Plugin);
391 api = GNUNET_new (struct GNUNET_TRANSPORT_PluginFunctions);
393 api->send = &template_plugin_send;
394 api->disconnect_peer = &template_plugin_disconnect_peer;
395 api->disconnect_session = &template_plugin_disconnect_session;
396 api->address_pretty_printer = &template_plugin_address_pretty_printer;
397 api->check_address = &template_plugin_address_suggested;
398 api->address_to_string = &template_plugin_address_to_string;
399 api->string_to_address = &template_plugin_string_to_address;
400 api->get_session = &template_plugin_get_session;
401 api->get_network = &template_plugin_get_network;
402 LOG (GNUNET_ERROR_TYPE_INFO, "Template plugin successfully loaded\n");
408 * Exit point from the plugin.
411 libgnunet_plugin_transport_template_done (void *cls)
413 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
414 struct Plugin *plugin = api->cls;
416 GNUNET_free (plugin);
421 /* end of plugin_transport_template.c */