- fix logging to remove error messages
[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 DEBUG_TEMPLATE GNUNET_EXTRA_LOGGING
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
127
128 /**
129  * Function that can be used by the transport service to transmit
130  * a message using the plugin.   Note that in the case of a
131  * peer disconnecting, the continuation MUST be called
132  * prior to the disconnect notification itself.  This function
133  * will be called with this peer's HELLO message to initiate
134  * a fresh connection to another peer.
135  *
136  * @param cls closure
137  * @param session which session must be used
138  * @param msgbuf the message to transmit
139  * @param msgbuf_size number of bytes in 'msgbuf'
140  * @param priority how important is the message (most plugins will
141  *                 ignore message priority and just FIFO)
142  * @param to how long to wait at most for the transmission (does not
143  *                require plugins to discard the message after the timeout,
144  *                just advisory for the desired delay; most plugins will ignore
145  *                this as well)
146  * @param cont continuation to call once the message has
147  *        been transmitted (or if the transport is ready
148  *        for the next transmission call; or if the
149  *        peer disconnected...); can be NULL
150  * @param cont_cls closure for cont
151  * @return number of bytes used (on the physical network, with overheads);
152  *         -1 on hard errors (i.e. address invalid); 0 is a legal value
153  *         and does NOT mean that the message was not transmitted (DV)
154  */
155 static ssize_t
156 template_plugin_send (void *cls,
157                   struct Session *session,
158                   const char *msgbuf, size_t msgbuf_size,
159                   unsigned int priority,
160                   struct GNUNET_TIME_Relative to,
161                   GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
162 {
163   struct Plugin *plugin = cls;
164   int bytes_sent = 0;
165
166   GNUNET_assert (plugin != NULL);
167   GNUNET_assert (session != NULL);
168
169   /*  struct Plugin *plugin = cls; */
170   return bytes_sent;
171 }
172
173
174
175 /**
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).
179  *
180  * @param cls closure
181  * @param target peer from which to disconnect
182  */
183 static void
184 template_plugin_disconnect (void *cls, const struct GNUNET_PeerIdentity *target)
185 {
186   // struct Plugin *plugin = cls;
187   // FIXME
188 }
189
190
191 /**
192  * Convert the transports address to a nice, human-readable
193  * format.
194  *
195  * @param cls closure
196  * @param type name of the transport that generated the address
197  * @param addr one of the addresses of the host, NULL for the last address
198  *        the specific address format depends on the transport
199  * @param addrlen length of the address
200  * @param numeric should (IP) addresses be displayed in numeric form?
201  * @param timeout after how long should we give up?
202  * @param asc function to call on each string
203  * @param asc_cls closure for asc
204  */
205 static void
206 template_plugin_address_pretty_printer (void *cls, const char *type,
207                                         const void *addr, size_t addrlen,
208                                         int numeric,
209                                         struct GNUNET_TIME_Relative timeout,
210                                         GNUNET_TRANSPORT_AddressStringCallback
211                                         asc, void *asc_cls)
212 {
213   asc (asc_cls, NULL);
214 }
215
216
217
218 /**
219  * Another peer has suggested an address for this
220  * peer and transport plugin.  Check that this could be a valid
221  * address.  If so, consider adding it to the list
222  * of addresses.
223  *
224  * @param cls closure
225  * @param addr pointer to the address
226  * @param addrlen length of addr
227  * @return GNUNET_OK if this is a plausible address for this peer
228  *         and transport
229  */
230 static int
231 template_plugin_address_suggested (void *cls, const void *addr, size_t addrlen)
232 {
233   /* struct Plugin *plugin = cls; */
234
235   /* check if the address is plausible; if so,
236    * add it to our list! */
237   return GNUNET_OK;
238 }
239
240
241 /**
242  * Function called for a quick conversion of the binary address to
243  * a numeric address.  Note that the caller must not free the
244  * address and that the next call to this function is allowed
245  * to override the address again.
246  *
247  * @param cls closure
248  * @param addr binary address
249  * @param addrlen length of the address
250  * @return string representing the same address
251  */
252 static const char *
253 template_plugin_address_to_string (void *cls, const void *addr, size_t addrlen)
254 {
255   GNUNET_break (0);
256   return NULL;
257 }
258
259
260
261
262 /**
263  * Entry point for the plugin.
264  */
265 void *
266 gnunet_plugin_transport_template_init (void *cls)
267 {
268   struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
269   struct GNUNET_TRANSPORT_PluginFunctions *api;
270   struct Plugin *plugin;
271
272   plugin = GNUNET_malloc (sizeof (struct Plugin));
273   plugin->env = env;
274   api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
275   api->cls = plugin;
276   api->send = &template_plugin_send;
277   api->disconnect = &template_plugin_disconnect;
278   api->address_pretty_printer = &template_plugin_address_pretty_printer;
279   api->check_address = &template_plugin_address_suggested;
280   api->address_to_string = &template_plugin_address_to_string;
281   return api;
282 }
283
284
285 /**
286  * Exit point from the plugin.
287  */
288 void *
289 gnunet_plugin_transport_template_done (void *cls)
290 {
291   struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
292   struct Plugin *plugin = api->cls;
293
294   GNUNET_free (plugin);
295   GNUNET_free (api);
296   return NULL;
297 }
298
299 /* end of plugin_transport_template.c */