ng
[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 2, 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_network_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"
35
36 #define DEBUG_TEMPLATE GNUNET_NO
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
46 /**
47  * Encapsulation of all of the state of the plugin.
48  */
49 struct Plugin;
50
51
52 /**
53  * Session handle for connections.
54  */
55 struct Session
56 {
57
58   /**
59    * Stored in a linked list.
60    */
61   struct Session *next;
62
63   /**
64    * Pointer to the global plugin struct.
65    */
66   struct Plugin *plugin;
67
68   /**
69    * The client (used to identify this connection)
70    */
71   /* void *client; */
72
73   /**
74    * Continuation function to call once the transmission buffer
75    * has again space available.  NULL if there is no
76    * continuation to call.
77    */
78   GNUNET_TRANSPORT_TransmitContinuation transmit_cont;
79
80   /**
81    * Closure for transmit_cont.
82    */
83   void *transmit_cont_cls;
84
85   /**
86    * To whom are we talking to (set to our identity
87    * if we are still waiting for the welcome message)
88    */
89   struct GNUNET_PeerIdentity sender;
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 /**
111  * Encapsulation of all of the state of the plugin.
112  */
113 struct Plugin
114 {
115   /**
116    * Our environment.
117    */
118   struct GNUNET_TRANSPORT_PluginEnvironment *env;
119
120   /**
121    * List of open sessions.
122    */
123   struct Session *sessions;
124
125   /**
126    * Handle for the statistics service.
127    */
128   struct GNUNET_STATISTICS_Handle *statistics;
129
130 };
131
132
133
134 /**
135  * Function that can be used by the transport service to transmit
136  * a message using the plugin using a fresh connection (even if
137  * we already have a connection to this peer, this function is
138  * required to establish a new one).
139  *
140  * @param cls closure
141  * @param target who should receive this message
142  * @param msg1 first message to transmit
143  * @param msg2 second message to transmit (can be NULL)
144  * @param timeout how long until we give up?
145  * @param addr the address
146  * @param addrlen length of the address
147  * @return non-null session if the transmission has been scheduled
148  *         NULL if the address format is invalid
149  */
150 static void *
151 template_plugin_send_to (void *cls,
152                          const struct GNUNET_PeerIdentity *target,
153                          const struct GNUNET_MessageHeader *msg1,
154                          const struct GNUNET_MessageHeader *msg2,
155                          struct GNUNET_TIME_Relative timeout,
156                          const void *addr, size_t addrlen)
157 {
158   // FIXME
159   return NULL;
160 }
161
162
163 /**
164  * Function that can be used by the transport service to transmit
165  * a message using the plugin.
166  *
167  * @param cls closure
168  * @param plugin_context value we were asked to pass to this plugin
169  *        to respond to the given peer (use is optional,
170  *        but may speed up processing), can be NULL
171  * @param service_context value passed to the transport-service
172  *        to identify the neighbour
173  * @param target who should receive this message
174  * @param msg the message to transmit
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
182  */
183 static void *
184 template_plugin_send (void *cls,
185                       void *plugin_context,
186                       struct ReadyList *service_context,
187                       const struct GNUNET_PeerIdentity *target,
188                       const struct GNUNET_MessageHeader *msg,
189                       struct GNUNET_TIME_Relative timeout,
190                       GNUNET_TRANSPORT_TransmitContinuation cont,
191                       void *cont_cls)
192 {
193   //  struct Plugin *plugin = cls;
194   return NULL;
195 }
196
197
198
199 /**
200  *
201  * @param cls closure
202  * @param plugin_context value we were asked to pass to this plugin
203  *        to respond to the given peer (use is optional,
204  *        but may speed up processing), can be NULL (if
205  *        NULL was returned from the transmit function)
206  * @param service_context must correspond to the service context
207  *        of the corresponding Transmit call; the plugin should
208  *        not cancel a send call made with a different service
209  *        context pointer!  Never NULL.
210  * @param target peer for which the last transmission is
211  *        to be cancelled
212  */
213 static void
214 template_plugin_cancel (void *cls,
215                         void *plugin_context,
216                         struct ReadyList *service_context,
217                         const struct GNUNET_PeerIdentity *target)
218 {
219   // struct Plugin *plugin = cls;
220   // FIXME
221 }
222
223
224 /**
225  * Convert the transports address to a nice, human-readable
226  * format.
227  *
228  * @param cls closure
229  * @param name name of the transport that generated the address
230  * @param addr one of the addresses of the host, NULL for the last address
231  *        the specific address format depends on the transport
232  * @param addrlen length of the address
233  * @param numeric should (IP) addresses be displayed in numeric form?
234  * @param timeout after how long should we give up?
235  * @param asc function to call on each string
236  * @param asc_cls closure for asc
237  */
238 static void
239 template_plugin_address_pretty_printer (void *cls,
240                                         const char *type,
241                                         const void *addr,
242                                         size_t addrlen,
243                                         int numeric,
244                                         struct GNUNET_TIME_Relative timeout,
245                                         GNUNET_TRANSPORT_AddressStringCallback
246                                         asc, void *asc_cls)
247 {
248   asc (asc_cls, NULL);
249 }
250
251 /**
252  * Set a quota for receiving data from the given peer; this is a
253  * per-transport limit.  The transport should limit its read/select
254  * calls to stay below the quota (in terms of incoming data).
255  *
256  * @param cls closure
257  * @param peer the peer for whom the quota is given
258  * @param quota_in quota for receiving/sending data in bytes per ms
259  */
260 static void
261 template_plugin_set_receive_quota (void *cls,
262                                    const struct GNUNET_PeerIdentity *target,
263                                    uint32_t quota_in)
264 {
265   // struct Plugin *plugin = cls;
266   // FIXME!
267 }
268
269
270 /**
271  * Another peer has suggested an address for this
272  * peer and transport plugin.  Check that this could be a valid
273  * address.  If so, consider adding it to the list
274  * of addresses.
275  *
276  * @param cls closure
277  * @param addr pointer to the address
278  * @param addrlen length of addr
279  * @return GNUNET_OK if this is a plausible address for this peer
280  *         and transport
281  */
282 static int
283 template_plugin_address_suggested (void *cls,
284                                    const void *addr, size_t addrlen)
285 {
286   // struct Plugin *plugin = cls;
287
288   /* check if the address is plausible; if so,
289      add it to our list! */
290   // FIXME!
291   return GNUNET_OK;
292 }
293
294
295 /**
296  * Entry point for the plugin.
297  */
298 void *
299 gnunet_plugin_transport_template_init (void *cls)
300 {
301   struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
302   struct GNUNET_TRANSPORT_PluginFunctions *api;
303   struct Plugin *plugin;
304
305   plugin = GNUNET_malloc (sizeof (struct Plugin));
306   plugin->env = env;
307   plugin->statistics = NULL;
308   api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
309   api->cls = plugin;
310   api->send_to = &template_plugin_send_to;
311   api->send = &template_plugin_send;
312   api->cancel = &template_plugin_cancel;
313   api->address_pretty_printer = &template_plugin_address_pretty_printer;
314   api->set_receive_quota = &template_plugin_set_receive_quota;
315   api->address_suggested = &template_plugin_address_suggested;
316   api->cost_estimate = 42;      // FIXME
317   return api;
318 }
319
320
321 /**
322  * Exit point from the plugin.
323  */
324 void *
325 gnunet_plugin_transport_template_done (void *cls)
326 {
327   struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
328   struct Plugin *plugin = api->cls;
329
330   GNUNET_free (plugin);
331   GNUNET_free (api);
332   return NULL;
333 }
334
335 /* end of plugin_transport_template.c */