3572c7837204c07136cdb1975fbf98e50ee95080
[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_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 "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  * Function that can be used by the transport service to transmit
134  * a message using the plugin.
135  *
136  * @param cls closure
137  * @param target who should receive this message
138  * @param priority how important is the message
139  * @param msg the message to transmit
140  * @param timeout when should we time out 
141  * @param cont continuation to call once the message has
142  *        been transmitted (or if the transport is ready
143  *        for the next transmission call; or if the
144  *        peer disconnected...)
145  * @param cont_cls closure for cont
146  */
147 static ssize_t
148 template_plugin_send (void *cls,
149                       const struct GNUNET_PeerIdentity *
150                       target,
151                       const struct GNUNET_MessageHeader *msg,
152                       unsigned int priority,
153                       struct GNUNET_TIME_Relative timeout,
154                       const void *addr,
155                       size_t addrlen,
156                       int force_address,
157                       GNUNET_TRANSPORT_TransmitContinuation
158                       cont, void *cont_cls)
159 {
160   int bytes_sent = 0;
161   /*  struct Plugin *plugin = cls; */
162   return bytes_sent;
163 }
164
165
166
167 /**
168  * Function that can be used to force the plugin to disconnect
169  * from the given peer and cancel all previous transmissions
170  * (and their continuationc).
171  *
172  * @param cls closure
173  * @param target peer from which to disconnect
174  */
175 static void
176 template_plugin_disconnect (void *cls,
177                             const struct GNUNET_PeerIdentity *target)
178 {
179   // struct Plugin *plugin = cls;
180   // FIXME
181 }
182
183
184 /**
185  * Convert the transports address to a nice, human-readable
186  * format.
187  *
188  * @param cls closure
189  * @param type name of the transport that generated the address
190  * @param addr one of the addresses of the host, NULL for the last address
191  *        the specific address format depends on the transport
192  * @param addrlen length of the address
193  * @param numeric should (IP) addresses be displayed in numeric form?
194  * @param timeout after how long should we give up?
195  * @param asc function to call on each string
196  * @param asc_cls closure for asc
197  */
198 static void
199 template_plugin_address_pretty_printer (void *cls,
200                                         const char *type,
201                                         const void *addr,
202                                         size_t addrlen,
203                                         int numeric,
204                                         struct GNUNET_TIME_Relative timeout,
205                                         GNUNET_TRANSPORT_AddressStringCallback
206                                         asc, void *asc_cls)
207 {
208   asc (asc_cls, NULL);
209 }
210
211 /**
212  * Set a quota for receiving data from the given peer; this is a
213  * per-transport limit.  The transport should limit its read/select
214  * calls to stay below the quota (in terms of incoming data).
215  *
216  * @param cls closure
217  * @param target the peer for whom the quota is given
218  * @param quota_in quota for receiving/sending data in bytes per ms
219  */
220 static void
221 template_plugin_set_receive_quota (void *cls,
222                                    const struct GNUNET_PeerIdentity *target,
223                                    uint32_t quota_in)
224 {
225   // struct Plugin *plugin = cls;
226   // FIXME!
227 }
228
229
230 /**
231  * Another peer has suggested an address for this
232  * peer and transport plugin.  Check that this could be a valid
233  * address.  If so, consider adding it to the list
234  * of addresses.
235  *
236  * @param cls closure
237  * @param addr pointer to the address
238  * @param addrlen length of addr
239  * @return GNUNET_OK if this is a plausible address for this peer
240  *         and transport
241  */
242 static int
243 template_plugin_address_suggested (void *cls,
244                                   void *addr, size_t addrlen)
245 {
246   /* struct Plugin *plugin = cls; */
247
248   /* check if the address is plausible; if so,
249      add it to our list! */
250   return GNUNET_OK;
251 }
252
253
254 /**
255  * Entry point for the plugin.
256  */
257 void *
258 gnunet_plugin_transport_template_init (void *cls)
259 {
260   struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
261   struct GNUNET_TRANSPORT_PluginFunctions *api;
262   struct Plugin *plugin;
263
264   plugin = GNUNET_malloc (sizeof (struct Plugin));
265   plugin->env = env;
266   plugin->statistics = NULL;
267   api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
268   api->cls = plugin;
269   api->send = &template_plugin_send;
270   api->disconnect = &template_plugin_disconnect;
271   api->address_pretty_printer = &template_plugin_address_pretty_printer;
272   api->set_receive_quota = &template_plugin_set_receive_quota;
273   api->check_address = &template_plugin_address_suggested;
274   return api;
275 }
276
277
278 /**
279  * Exit point from the plugin.
280  */
281 void *
282 gnunet_plugin_transport_template_done (void *cls)
283 {
284   struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
285   struct Plugin *plugin = api->cls;
286
287   GNUNET_free (plugin);
288   GNUNET_free (api);
289   return NULL;
290 }
291
292 /* end of plugin_transport_template.c */