Removing all the tests for the bluetooth transport plugin.
[oweals/gnunet.git] / src / transport / plugin_transport_template.c
1 /*
2      This file is part of GNUnet
3      (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009 Christian Grothoff (and other contributing authors)
4
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.
9
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.
14
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.
19 */
20
21 /**
22  * @file transport/plugin_transport_template.c
23  * @brief template for a new transport service
24  * @author Christian Grothoff
25  */
26
27 #include "platform.h"
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"
35
36 #define LOG(kind,...) GNUNET_log_from (kind, "transport-template",__VA_ARGS__)
37
38 /**
39  * After how long do we expire an address that we
40  * learned from another peer if it is not reconfirmed
41  * by anyone?
42  */
43 #define LEARNED_ADDRESS_EXPIRATION GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_HOURS, 6)
44
45 #define PLUGIN_NAME "template"
46
47 /**
48  * Encapsulation of all of the state of the plugin.
49  */
50 struct Plugin;
51
52
53 /**
54  * Session handle for connections.
55  */
56 struct Session
57 {
58   /**
59    * To whom are we talking to (set to our identity
60    * if we are still waiting for the welcome message)
61    */
62   struct GNUNET_PeerIdentity sender;
63
64   /**
65    * Stored in a linked list.
66    */
67   struct Session *next;
68
69   /**
70    * Pointer to the global plugin struct.
71    */
72   struct Plugin *plugin;
73
74   /**
75    * The client (used to identify this connection)
76    */
77   /* void *client; */
78
79   /**
80    * Continuation function to call once the transmission buffer
81    * has again space available.  NULL if there is no
82    * continuation to call.
83    */
84   GNUNET_TRANSPORT_TransmitContinuation transmit_cont;
85
86   /**
87    * Closure for transmit_cont.
88    */
89   void *transmit_cont_cls;
90
91   /**
92    * At what time did we reset last_received last?
93    */
94   struct GNUNET_TIME_Absolute last_quota_update;
95
96   /**
97    * How many bytes have we received since the "last_quota_update"
98    * timestamp?
99    */
100   uint64_t last_received;
101
102   /**
103    * Number of bytes per ms that this peer is allowed
104    * to send to us.
105    */
106   uint32_t quota;
107
108 };
109
110 GNUNET_NETWORK_STRUCT_BEGIN
111
112 struct TemplateAddress
113 {
114         /**
115          * Address options in NBO
116          */
117         uint32_t options GNUNET_PACKED;
118
119         /* Add address here */
120 };
121
122 GNUNET_NETWORK_STRUCT_END
123
124 /**
125  * Encapsulation of all of the state of the plugin.
126  */
127 struct Plugin
128 {
129   /**
130    * Our environment.
131    */
132   struct GNUNET_TRANSPORT_PluginEnvironment *env;
133
134   /**
135    * List of open sessions.
136    */
137   struct Session *sessions;
138
139   /**
140    * Options in HBO to be used with addresses
141    */
142
143 };
144
145
146 /**
147  * Function that can be used by the transport service to transmit
148  * a message using the plugin.   Note that in the case of a
149  * peer disconnecting, the continuation MUST be called
150  * prior to the disconnect notification itself.  This function
151  * will be called with this peer's HELLO message to initiate
152  * a fresh connection to another peer.
153  *
154  * @param cls closure
155  * @param session which session must be used
156  * @param msgbuf the message to transmit
157  * @param msgbuf_size number of bytes in 'msgbuf'
158  * @param priority how important is the message (most plugins will
159  *                 ignore message priority and just FIFO)
160  * @param to how long to wait at most for the transmission (does not
161  *                require plugins to discard the message after the timeout,
162  *                just advisory for the desired delay; most plugins will ignore
163  *                this as well)
164  * @param cont continuation to call once the message has
165  *        been transmitted (or if the transport is ready
166  *        for the next transmission call; or if the
167  *        peer disconnected...); can be NULL
168  * @param cont_cls closure for cont
169  * @return number of bytes used (on the physical network, with overheads);
170  *         -1 on hard errors (i.e. address invalid); 0 is a legal value
171  *         and does NOT mean that the message was not transmitted (DV)
172  */
173 static ssize_t
174 template_plugin_send (void *cls,
175                   struct Session *session,
176                   const char *msgbuf, size_t msgbuf_size,
177                   unsigned int priority,
178                   struct GNUNET_TIME_Relative to,
179                   GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
180 {
181   struct Plugin *plugin = cls;
182   int bytes_sent = 0;
183
184   GNUNET_assert (plugin != NULL);
185   GNUNET_assert (session != NULL);
186
187   /*  struct Plugin *plugin = cls; */
188   return bytes_sent;
189 }
190
191
192
193 /**
194  * Function that can be used to force the plugin to disconnect
195  * from the given peer and cancel all previous transmissions
196  * (and their continuationc).
197  *
198  * @param cls closure
199  * @param target peer from which to disconnect
200  */
201 static void
202 template_plugin_disconnect (void *cls, const struct GNUNET_PeerIdentity *target)
203 {
204   // struct Plugin *plugin = cls;
205   // FIXME
206 }
207
208
209 /**
210  * Function obtain the network type for a session
211  *
212  * @param cls closure ('struct Plugin*')
213  * @param session the session
214  * @return the network type in HBO or GNUNET_SYSERR
215  */
216 static enum GNUNET_ATS_Network_Type
217 template_plugin_get_network (void *cls, 
218                              struct Session *session)
219 {
220   GNUNET_assert (NULL != session);
221   return GNUNET_ATS_NET_UNSPECIFIED; /* Change to correct network type */
222 }
223
224
225 /**
226  * Convert the transports address to a nice, human-readable
227  * format.
228  *
229  * @param cls closure
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
238  */
239 static void
240 template_plugin_address_pretty_printer (void *cls, const char *type,
241                                         const void *addr, size_t addrlen,
242                                         int numeric,
243                                         struct GNUNET_TIME_Relative timeout,
244                                         GNUNET_TRANSPORT_AddressStringCallback
245                                         asc, void *asc_cls)
246 {
247         if (0 == addrlen)
248         {
249                 asc (asc_cls, TRANSPORT_SESSION_INBOUND_STRING);
250         }
251
252   asc (asc_cls, NULL);
253 }
254
255
256
257 /**
258  * Another peer has suggested an address for this
259  * peer and transport plugin.  Check that this could be a valid
260  * address.  If so, consider adding it to the list
261  * of addresses.
262  *
263  * @param cls closure
264  * @param addr pointer to the address
265  * @param addrlen length of addr
266  * @return GNUNET_OK if this is a plausible address for this peer
267  *         and transport
268  */
269 static int
270 template_plugin_address_suggested (void *cls, const void *addr, size_t addrlen)
271 {
272   /* struct Plugin *plugin = cls; */
273
274   /* check if the address is belonging to the plugin*/
275   return GNUNET_OK;
276 }
277
278
279 /**
280  * Function called for a quick conversion of the binary address to
281  * a numeric address.  Note that the caller must not free the
282  * address and that the next call to this function is allowed
283  * to override the address again.
284  *
285  * @param cls closure
286  * @param addr binary address
287  * @param addrlen length of the address
288  * @return string representing the same address
289  */
290 static const char *
291 template_plugin_address_to_string (void *cls, const void *addr, size_t addrlen)
292 {
293         /*
294          * Print address in format template.options.address
295          */
296
297         if (0 == addrlen)
298         {
299                 return TRANSPORT_SESSION_INBOUND_STRING;
300         }
301
302   GNUNET_break (0);
303   return NULL;
304 }
305
306
307 /**
308  * Function called to convert a string address to
309  * a binary address.
310  *
311  * @param cls closure ('struct Plugin*')
312  * @param addr string address
313  * @param addrlen length of the address
314  * @param buf location to store the buffer
315  * @param added location to store the number of bytes in the buffer.
316  *        If the function returns GNUNET_SYSERR, its contents are undefined.
317  * @return GNUNET_OK on success, GNUNET_SYSERR on failure
318  */
319 static int
320 template_plugin_string_to_address (void *cls, const char *addr, uint16_t addrlen,
321     void **buf, size_t *added)
322 {
323
324         /*
325          * Parse string in format template.options.address
326          */
327
328
329   GNUNET_break (0);
330   return GNUNET_SYSERR;
331 }
332
333
334 /**
335  * Create a new session to transmit data to the target
336  * This session will used to send data to this peer and the plugin will
337  * notify us by calling the env->session_end function
338  *
339  * @param cls closure
340  * @param address pointer to the GNUNET_HELLO_Address
341  * @return the session if the address is valid, NULL otherwise
342  */
343 static struct Session *
344 template_plugin_get_session (void *cls,
345                         const struct GNUNET_HELLO_Address *address)
346 {
347   GNUNET_break (0);
348   return NULL;
349 }
350
351 /**
352  * Entry point for the plugin.
353  */
354 void *
355 libgnunet_plugin_transport_template_init (void *cls)
356 {
357   struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
358   struct GNUNET_TRANSPORT_PluginFunctions *api;
359   struct Plugin *plugin;
360
361   if (NULL == env->receive)
362   {
363     /* run in 'stub' mode (i.e. as part of gnunet-peerinfo), don't fully
364        initialze the plugin or the API */
365     api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
366     api->cls = NULL;
367     api->address_to_string = &template_plugin_address_to_string;
368     api->string_to_address = &template_plugin_string_to_address;
369     api->address_pretty_printer = &template_plugin_address_pretty_printer;
370     return api;
371   }
372
373   plugin = GNUNET_malloc (sizeof (struct Plugin));
374   plugin->env = env;
375   api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
376   api->cls = plugin;
377   api->send = &template_plugin_send;
378   api->disconnect = &template_plugin_disconnect;
379   api->address_pretty_printer = &template_plugin_address_pretty_printer;
380   api->check_address = &template_plugin_address_suggested;
381   api->address_to_string = &template_plugin_address_to_string;
382   api->string_to_address = &template_plugin_string_to_address;
383   api->get_session = &template_plugin_get_session;
384   api->get_network = &template_plugin_get_network;
385   LOG (GNUNET_ERROR_TYPE_INFO, "Template plugin successfully loaded\n");
386   return api;
387 }
388
389
390 /**
391  * Exit point from the plugin.
392  */
393 void *
394 libgnunet_plugin_transport_template_done (void *cls)
395 {
396   struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
397   struct Plugin *plugin = api->cls;
398
399   GNUNET_free (plugin);
400   GNUNET_free (api);
401   return NULL;
402 }
403
404 /* end of plugin_transport_template.c */