further simplify transport API
[oweals/gnunet.git] / src / transport / plugin_transport.h
1 /*
2      This file is part of GNUnet
3      (C) 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.h
23  * @brief API for the transport services.  This header
24  *        specifies the struct that is given to the plugin's entry
25  *        method and the other struct that must be returned.
26  *        Note that the destructors of transport plugins will
27  *        be given the value returned by the constructor
28  *        and is expected to return a NULL pointer.
29  *
30  * TODO:
31  * - consider moving DATA message (latency measurement)
32  *   to service; avoids encapsulation overheads and
33  *   would enable latency measurements for non-bidi
34  *   transports.
35  * -
36  *
37  * @author Christian Grothoff
38  */
39 #ifndef PLUGIN_TRANSPORT_H
40 #define PLUGIN_TRANSPORT_H
41
42 #include "gnunet_configuration_lib.h"
43 #include "gnunet_scheduler_lib.h"
44 #include "gnunet_transport_service.h"
45
46 /**
47  * Function called by the transport for each received message.
48  * This function should also be called with "NULL" for the
49  * message to signal that the other peer disconnected.
50  *
51  * @param cls closure
52  * @param latency estimated latency for communicating with the
53  *        given peer; should be set to GNUNET_TIME_UNIT_FOREVER_REL
54  *        until the transport has seen messages transmitted in
55  *        BOTH directions (and hence been able to do an actual
56  *        round-trip observation); a non-FOREVER latency is also used
57  *        by the transport to know that communication in both directions
58  *        using this one plugin actually works
59  * @param peer (claimed) identity of the other peer
60  * @param message the message, NULL if peer was disconnected
61  */
62 typedef void (*GNUNET_TRANSPORT_PluginReceiveCallback) (void *cls,
63                                                         struct GNUNET_TIME_Relative
64                                                         latency,
65                                                         const struct GNUNET_PeerIdentity
66                                                         * peer,
67                                                         const struct GNUNET_MessageHeader
68                                                         * message);
69
70
71 /**
72  * Function that will be called if we receive a validation
73  * of an address challenge that we transmitted to another
74  * peer.  Note that the validation should only be considered
75  * acceptable if the challenge matches AND if the sender
76  * address is at least a plausible address for this peer
77  * (otherwise we may be seeing a MiM attack).
78  *
79  * @param cls closure
80  * @param name name of the transport that generated the address
81  * @param peer who responded to our challenge
82  * @param challenge the challenge number we presumably used
83  * @param sender_addr string describing our sender address (as observed
84  *         by the other peer in human-readable format)
85  */
86 typedef void (*GNUNET_TRANSPORT_ValidationNotification) (void *cls,
87                                                          const char *name,
88                                                          const struct GNUNET_PeerIdentity *peer,
89                                                          uint32_t challenge,
90                                                          const char *sender_addr);
91
92
93
94 /**
95  * Function that will be called for each address the transport
96  * is aware that it might be reachable under.
97  *
98  * @param cls closure
99  * @param name name of the transport that generated the address
100  * @param addr one of the addresses of the host, NULL for the last address
101  *        the specific address format depends on the transport
102  * @param addrlen length of the address
103  * @param expires when should this address automatically expire?
104  */
105 typedef void (*GNUNET_TRANSPORT_AddressNotification) (void *cls,
106                                                       const char *name,
107                                                       const void *addr,
108                                                       size_t addrlen,
109                                                       struct
110                                                       GNUNET_TIME_Relative
111                                                       expires);
112
113
114 /**
115  * Function that will be called for each address obtained from the HELLO.
116  *
117  * @param cls closure
118  * @param name name of the transport that generated the address
119  * @param addr one of the addresses of the host, NULL for the last address
120  *        the specific address format depends on the transport
121  * @param addrlen length of the address
122  */
123 typedef void (*GNUNET_TRANSPORT_AddressCallback) (void *cls,
124                                                   const char *name,
125                                                   const void *addr,
126                                                   size_t addrlen);
127
128
129 /**
130  * The transport service will pass a pointer to a struct
131  * of this type as the first and only argument to the
132  * entry point of each transport plugin.
133  */
134 struct GNUNET_TRANSPORT_PluginEnvironment
135 {
136   /**
137    * Configuration to use.
138    */
139   const struct GNUNET_CONFIGURATION_Handle *cfg;
140
141   /**
142    * Scheduler to use.
143    */
144   struct GNUNET_SCHEDULER_Handle *sched;
145
146   /**
147    * Our public key.
148    */
149   struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *my_public_key;
150
151   /**
152    * Our private key.
153    */
154   struct GNUNET_CRYPTO_RsaPrivateKey *my_private_key;
155
156   /**
157    * Identity of this peer.
158    */
159   const struct GNUNET_PeerIdentity *my_identity;
160
161   /**
162    * Closure for the various callbacks.
163    */
164   void *cls;
165
166   /**
167    * Function that should be called by the transport plugin
168    * whenever a message is received.
169    */
170   GNUNET_TRANSPORT_PluginReceiveCallback receive;
171
172   /**
173    * Function that must be called by each plugin to notify the
174    * transport service about the addresses under which the transport
175    * provided by the plugin can be reached.
176    */
177   GNUNET_TRANSPORT_AddressNotification notify_address;
178
179   /**
180    * Function that must be called by each plugin to notify the
181    * transport service about a successful validation of an
182    * address of another peer (or at least likely successful
183    * validation).
184    */
185   GNUNET_TRANSPORT_ValidationNotification notify_validation;
186
187   /**
188    * What is the default quota (in terms of incoming bytes per
189    * ms) for new connections?
190    */
191   uint32_t default_quota_in;
192
193   /**
194    * What is the maximum number of connections that this transport
195    * should allow?  Transports that do not have sessions (such as
196    * UDP) can ignore this value.
197    */
198   uint32_t max_connections;
199
200 };
201
202
203 /**
204  * Function that can be used by the transport service to validate
205  * the address of another peer.  Even if
206  * we already have a connection to this peer, this function is
207  * required to establish a new one.
208  *
209  * @param cls closure
210  * @param target who should receive this message
211  * @param challenge challenge code to use
212  * @param timeout how long should we try to transmit these?
213  * @param addrlen length of the address
214  * @param addr the address
215  * @return GNUNET_OK on success, GNUNET_SYSERR if the address
216  *         format is invalid
217  */
218 typedef int
219   (*GNUNET_TRANSPORT_ValidationFunction) (void *cls,
220                                           const struct
221                                           GNUNET_PeerIdentity * target,
222                                           uint32_t challenge,
223                                           struct GNUNET_TIME_Relative
224                                           timeout, const void *addr,
225                                           size_t addrlen);
226
227 /**
228  * Function called by the GNUNET_TRANSPORT_TransmitFunction
229  * upon "completion".
230  *
231  * @param cls closure
232  * @param target who was the recipient of the message?
233  * @param result GNUNET_OK on success
234  *               GNUNET_SYSERR if the target disconnected;
235  *               disconnect will ALSO be signalled using
236  *               the ReceiveCallback.
237  */
238 typedef void
239   (*GNUNET_TRANSPORT_TransmitContinuation) (void *cls,
240                                             const struct GNUNET_PeerIdentity *
241                                             target, int result);
242
243
244 /**
245  * Function that can be used by the transport service to transmit
246  * a message using the plugin.   Note that in the case of a
247  * peer disconnecting, the continuation MUST be called
248  * prior to the disconnect notification itself.  This function
249  * will be called with this peer's HELLO message to initiate
250  * a fresh connection to another peer.
251  *
252  * @param cls closure
253  * @param target who should receive this message
254  * @param priority how important is the message?
255  * @param msg the message to transmit
256  * @param timeout how long to wait at most for the transmission
257  * @param cont continuation to call once the message has
258  *        been transmitted (or if the transport is ready
259  *        for the next transmission call; or if the
260  *        peer disconnected...); can be NULL
261  * @param cont_cls closure for cont
262  */
263 typedef void 
264   (*GNUNET_TRANSPORT_TransmitFunction) (void *cls,
265                                         const struct GNUNET_PeerIdentity *
266                                         target,
267                                         unsigned int priority,
268                                         const struct GNUNET_MessageHeader *
269                                         msg,
270                                         struct GNUNET_TIME_Relative timeout,
271                                         GNUNET_TRANSPORT_TransmitContinuation
272                                         cont, void *cont_cls);
273
274
275 /**
276  * Function that can be called to force a disconnect from the
277  * specified neighbour.  This should also cancel all previously
278  * scheduled transmissions.  Obviously the transmission may have been
279  * partially completed already, which is OK.  The plugin is supposed
280  * to close the connection (if applicable) and no longer call the
281  * transmit continuation(s).
282  *
283  * Finally, plugin MUST NOT call the services's receive function to
284  * notify the service that the connection to the specified target was
285  * closed after a getting this call.
286  *
287  * @param cls closure
288  * @param target peer for which the last transmission is
289  *        to be cancelled
290  */
291 typedef void
292   (*GNUNET_TRANSPORT_DisconnectFunction) (void *cls,
293                                           const struct GNUNET_PeerIdentity *
294                                           target);
295
296
297 /**
298  * Function called by the pretty printer for the resolved address for
299  * each human-readable address obtained.
300  *
301  * @param cls closure
302  * @param hostname one of the names for the host, NULL
303  *        on the last call to the callback
304  */
305 typedef void (*GNUNET_TRANSPORT_AddressStringCallback) (void *cls,
306                                                         const char *address);
307
308
309 /**
310  * Convert the transports address to a nice, human-readable
311  * format.
312  *
313  * @param cls closure
314  * @param name name of the transport that generated the address
315  * @param addr one of the addresses of the host, NULL for the last address
316  *        the specific address format depends on the transport
317  * @param addrlen length of the address
318  * @param numeric should (IP) addresses be displayed in numeric form?
319  * @param timeout after how long should we give up?
320  * @param asc function to call on each string
321  * @param asc_cls closure for asc
322  */
323 typedef void
324   (*GNUNET_TRANSPORT_AddressPrettyPrinter) (void *cls,
325                                             const char *type,
326                                             const void *addr,
327                                             size_t addrlen,
328                                             int numeric,
329                                             struct GNUNET_TIME_Relative
330                                             timeout,
331                                             GNUNET_TRANSPORT_AddressStringCallback
332                                             asc, void *asc_cls);
333
334
335 /**
336  * Set a quota for receiving data from the given peer; this is a
337  * per-transport limit.  The transport should limit its read/select
338  * calls to stay below the quota (in terms of incoming data).
339  *
340  * @param cls closure
341  * @param peer the peer for whom the quota is given
342  * @param quota_in quota for receiving/sending data in bytes per ms
343  */
344 typedef void
345   (*GNUNET_TRANSPORT_SetQuota) (void *cls,
346                                 const struct GNUNET_PeerIdentity * target,
347                                 uint32_t quota_in);
348
349
350 /**
351  * Another peer has suggested an address for this
352  * peer and transport plugin.  Check that this could be a valid
353  * address.  If so, consider adding it to the list
354  * of addresses.
355  *
356  * @param addr pointer to the address
357  * @param addrlen length of addr
358  * @return GNUNET_OK if this is a plausible address for this peer
359  *         and transport
360  */
361 typedef int
362   (*GNUNET_TRANSPORT_SuggestAddress) (void *cls,
363                                       const void *addr, size_t addrlen);
364
365 /**
366  * Each plugin is required to return a pointer to a struct of this
367  * type as the return value from its entry point.
368  */
369 struct GNUNET_TRANSPORT_PluginFunctions
370 {
371
372   /**
373    * Closure for all of the callbacks.
374    */
375   void *cls;
376
377   /**
378    * Function used to send a single message to a particular
379    * peer using the specified address.  Used to validate
380    * HELLOs.
381    */
382   GNUNET_TRANSPORT_ValidationFunction validate;
383
384   /**
385    * Function that the transport service will use to transmit data to
386    * another peer.  May be null for plugins that only support
387    * receiving data.  After this call, the plugin call the specified
388    * continuation with success or error before notifying us about the
389    * target having disconnected.
390    */
391   GNUNET_TRANSPORT_TransmitFunction send;
392
393   /**
394    * Function that can be used to force the plugin to disconnect from
395    * the given peer and cancel all previous transmissions (and their
396    * continuations).  Note that if the transport does not have
397    * sessions / persistent connections (for example, UDP), this
398    * function may very well do nothing.
399    */
400   GNUNET_TRANSPORT_DisconnectFunction disconnect;
401
402   /**
403    * Function to pretty-print addresses.  NOTE: this function is not
404    * yet used by transport-service, but will be used in the future
405    * once the transport-API has been completed.
406    */
407   GNUNET_TRANSPORT_AddressPrettyPrinter address_pretty_printer;
408
409   /**
410    * Function that the transport service can use to try to enforce a
411    * quota for the number of bytes received via this transport.
412    * Transports that can not refuse incoming data (such as UDP)
413    * are free to ignore these calls.
414    */
415   GNUNET_TRANSPORT_SetQuota set_receive_quota;
416
417   /**
418    * Function that will be called if another peer suggested that
419    * we should use a particular address (since he is reaching
420    * us at that address) for this transport.
421    */
422   GNUNET_TRANSPORT_SuggestAddress address_suggested;
423
424   /**
425    * Relative cost of this transport compared to others.  This
426    * is supposed to be a static cost estimate which determines
427    * which plugins should not even be attempted if other,
428    * cheaper transports are already working.  The idea is that
429    * the costs have roughly this relationship:
430    * <pre>
431    * TCP < UDP < HTTP == HTTPS < SMTP
432    * </pre>
433    */
434   unsigned int cost_estimate;
435 };
436
437
438 #endif