backup errno
[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  * Opaque internal context for a particular peer of the transport
48  * service.  Plugins will be given a pointer to this type and, if
49  * cheaply possible, should pass this pointer back to the transport
50  * service whenever additional messages from the same peer are
51  * received.
52  */
53 struct ReadyList;
54
55 /**
56  * Function called by the transport for each received message.
57  * This function should also be called with "NULL" for the
58  * message to signal that the other peer disconnected.
59  *
60  * @param cls closure
61  * @param plugin_context value to pass to this plugin
62  *        to respond to the given peer (use is optional,
63  *        but may speed up processing)
64  * @param service_context value passed to the transport-service
65  *        to identify the neighbour; will be NULL on the first
66  *        call for a given peer
67  * @param latency estimated latency for communicating with the
68  *        given peer; should be set to GNUNET_TIME_UNIT_FOREVER_REL
69  *        until the transport has seen messages transmitted in
70  *        BOTH directions (and hence been able to do an actual
71  *        round-trip observation); a non-FOREVER latency is also used
72  *        by the transport to know that communication in both directions
73  *        using this one plugin actually works
74  * @param peer (claimed) identity of the other peer
75  * @param message the message, NULL if peer was disconnected
76  * @return the new service_context that the plugin should use
77  *         for future receive calls for messages from this
78  *         particular peer
79  */
80 typedef struct ReadyList *
81   (*GNUNET_TRANSPORT_PluginReceiveCallback) (void *cls,
82                                              void *plugin_context,
83                                              struct ReadyList *
84                                              service_context,
85                                              struct GNUNET_TIME_Relative
86                                              latency,
87                                              const struct GNUNET_PeerIdentity
88                                              * peer,
89                                              const struct GNUNET_MessageHeader
90                                              * message);
91
92
93 /**
94  * Function that will be called if we receive a validation
95  * of an address challenge that we transmitted to another
96  * peer.  Note that the validation should only be considered
97  * acceptable if the challenge matches AND if the sender
98  * address is at least a plausible address for this peer
99  * (otherwise we may be seeing a MiM attack).
100  *
101  * @param cls closure
102  * @param name name of the transport that generated the address
103  * @param peer who responded to our challenge
104  * @param challenge the challenge number we presumably used
105  * @param sender_addr string describing our sender address (as observed
106  *         by the other peer in human-readable format)
107  */
108 typedef void (*GNUNET_TRANSPORT_ValidationNotification) (void *cls,
109                                                          const char *name,
110                                                          const struct GNUNET_PeerIdentity *peer,
111                                                          uint32_t challenge,
112                                                          const char *sender_addr);
113
114
115
116 /**
117  * Function that will be called for each address the transport
118  * is aware that it might be reachable under.
119  *
120  * @param cls closure
121  * @param name name of the transport that generated the address
122  * @param addr one of the addresses of the host, NULL for the last address
123  *        the specific address format depends on the transport
124  * @param addrlen length of the address
125  * @param expires when should this address automatically expire?
126  */
127 typedef void (*GNUNET_TRANSPORT_AddressNotification) (void *cls,
128                                                       const char *name,
129                                                       const void *addr,
130                                                       size_t addrlen,
131                                                       struct
132                                                       GNUNET_TIME_Relative
133                                                       expires);
134
135
136 /**
137  * Function that will be called for each address obtained from the HELLO.
138  *
139  * @param cls closure
140  * @param name name of the transport that generated the address
141  * @param addr one of the addresses of the host, NULL for the last address
142  *        the specific address format depends on the transport
143  * @param addrlen length of the address
144  */
145 typedef void (*GNUNET_TRANSPORT_AddressCallback) (void *cls,
146                                                   const char *name,
147                                                   const void *addr,
148                                                   size_t addrlen);
149
150
151 /**
152  * Function that allows a transport to query the known
153  * network addresses for a given peer.
154  *
155  * @param cls closure
156  * @param timeout after how long should we time out?
157  * @param target which peer are we looking for?
158  * @param iter function to call for each known address
159  * @param iter_cls closure for iter
160  */
161 typedef void (*GNUNET_TRANSPORT_LookupAddress) (void *cls,
162                                                 struct GNUNET_TIME_Relative
163                                                 timeout,
164                                                 const struct
165                                                 GNUNET_PeerIdentity * target,
166                                                 GNUNET_TRANSPORT_AddressCallback
167                                                 iter, void *iter_cls);
168
169
170 /**
171  * The transport service will pass a pointer to a struct
172  * of this type as the first and only argument to the
173  * entry point of each transport plugin.
174  */
175 struct GNUNET_TRANSPORT_PluginEnvironment
176 {
177   /**
178    * Configuration to use.
179    */
180   const struct GNUNET_CONFIGURATION_Handle *cfg;
181
182   /**
183    * Scheduler to use.
184    */
185   struct GNUNET_SCHEDULER_Handle *sched;
186
187   /**
188    * Our public key.
189    */
190   struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *my_public_key;
191
192   /**
193    * Our private key.
194    */
195   struct GNUNET_CRYPTO_RsaPrivateKey *my_private_key;
196
197   /**
198    * Identity of this peer.
199    */
200   const struct GNUNET_PeerIdentity *my_identity;
201
202   /**
203    * Closure for the various callbacks.
204    */
205   void *cls;
206
207   /**
208    * Function that should be called by the transport plugin
209    * whenever a message is received.
210    */
211   GNUNET_TRANSPORT_PluginReceiveCallback receive;
212
213   /**
214    * Address lookup function.
215    */
216   GNUNET_TRANSPORT_LookupAddress lookup;
217
218   /**
219    * Function that must be called by each plugin to notify the
220    * transport service about the addresses under which the transport
221    * provided by the plugin can be reached.
222    */
223   GNUNET_TRANSPORT_AddressNotification notify_address;
224
225   /**
226    * Function that must be called by each plugin to notify the
227    * transport service about a successful validation of an
228    * address of another peer (or at least likely successful
229    * validation).
230    */
231   GNUNET_TRANSPORT_ValidationNotification notify_validation;
232
233   /**
234    * What is the default quota (in terms of incoming bytes per
235    * ms) for new connections?
236    */
237   uint32_t default_quota_in;
238
239   /**
240    * What is the maximum number of connections that this transport
241    * should allow?  Transports that do not have sessions (such as
242    * UDP) can ignore this value.
243    */
244   uint32_t max_connections;
245
246 };
247
248
249 /**
250  * Function that can be used by the transport service to validate
251  * the address of another peer.  Even if
252  * we already have a connection to this peer, this function is
253  * required to establish a new one.
254  *
255  * @param cls closure
256  * @param target who should receive this message
257  * @param challenge challenge code to use
258  * @param timeout how long should we try to transmit these?
259  * @param addrlen length of the address
260  * @param addr the address
261  * @return GNUNET_OK on success, GNUNET_SYSERR if the address
262  *         format is invalid
263  */
264 typedef int
265   (*GNUNET_TRANSPORT_ValidationFunction) (void *cls,
266                                           const struct
267                                           GNUNET_PeerIdentity * target,
268                                           uint32_t challenge,
269                                           struct GNUNET_TIME_Relative
270                                           timeout, const void *addr,
271                                           size_t addrlen);
272
273 /**
274  * Function called by the GNUNET_TRANSPORT_TransmitFunction
275  * upon "completion".
276  *
277  * @param cls closure
278  * @param service_context value passed to the transport-service
279  *        to identify the neighbour
280  * @param target who was the recipient of the message?
281  * @param result GNUNET_OK on success
282  *               GNUNET_SYSERR if the target disconnected;
283  *               disconnect will ALSO be signalled using
284  *               the ReceiveCallback.
285  */
286 typedef void
287   (*GNUNET_TRANSPORT_TransmitContinuation) (void *cls,
288                                             struct ReadyList *
289                                             service_context,
290                                             const struct GNUNET_PeerIdentity *
291                                             target, int result);
292
293 /**
294  * Function that can be used by the transport service to transmit
295  * a message using the plugin.   Note that in the case of a
296  * peer disconnecting, the continuation MUST be called
297  * prior to the disconnect notification itself.  This function
298  * will be called with this peer's HELLO message to initiate
299  * a fresh connection to another peer.
300  *
301  * @param cls closure
302  * @param plugin_context value we were asked to pass to this plugin
303  *        to respond to the given peer (use is optional,
304  *        but may speed up processing), can be NULL
305  * @param service_context value passed to the transport-service
306  *        to identify the neighbour; NULL is used to indicate
307  *        an urgent message.  If the urgent message can not be
308  *        scheduled for immediate transmission, the plugin is to
309  *        call the continuation with failure immediately
310  * @param target who should receive this message
311  * @param priority how important is the message?
312  * @param msg the message to transmit
313  * @param timeout how long to wait at most for the transmission
314  * @param cont continuation to call once the message has
315  *        been transmitted (or if the transport is ready
316  *        for the next transmission call; or if the
317  *        peer disconnected...); can be NULL
318  * @param cont_cls closure for cont
319  * @return plugin_context that should be used next time for
320  *         sending messages to the specified peer
321  */
322 typedef void *
323   (*GNUNET_TRANSPORT_TransmitFunction) (void *cls,
324                                         void *plugin_context,
325                                         struct ReadyList * service_context,
326                                         const struct GNUNET_PeerIdentity *
327                                         target,
328                                         unsigned int priority,
329                                         const struct GNUNET_MessageHeader *
330                                         msg,
331                                         struct GNUNET_TIME_Relative timeout,
332                                         GNUNET_TRANSPORT_TransmitContinuation
333                                         cont, void *cont_cls);
334
335
336 /**
337  * Function that can be called to force a disconnect from the
338  * specified neighbour.  This should also cancel all previously
339  * scheduled transmissions.  Obviously the transmission may have been
340  * partially completed already, which is OK.  The plugin is supposed
341  * to close the connection (if applicable) and no longer call the
342  * transmit continuation(s).
343  *
344  * Finally, plugin MUST NOT call the services's receive function to
345  * notify the service that the connection to the specified target was
346  * closed after a getting this call.
347  *
348  * @param cls closure
349  * @param plugin_context value we were asked to pass to this plugin
350  *        to respond to the given peer (use is optional,
351  *        but may speed up processing), can be NULL (if
352  *        NULL was returned from the transmit function); note
353  *        that use of NULL is dangerous since then this call may
354  *        cancel any session with the target peer (including
355  *        HELLO validation sessions), which is likely not what
356  *        is intended.
357  * @param service_context must correspond to the service context
358  *        of the corresponding Transmit call; the plugin should
359  *        not cancel a send call made with a different service
360  *        context pointer!  Never NULL.
361  * @param target peer for which the last transmission is
362  *        to be cancelled
363  */
364 typedef void
365   (*GNUNET_TRANSPORT_CancelFunction) (void *cls,
366                                       void *plugin_context,
367                                       struct ReadyList * service_context,
368                                       const struct GNUNET_PeerIdentity *
369                                       target);
370
371
372 /**
373  * Function called by the pretty printer for the resolved address for
374  * each human-readable address obtained.
375  *
376  * @param cls closure
377  * @param hostname one of the names for the host, NULL
378  *        on the last call to the callback
379  */
380 typedef void (*GNUNET_TRANSPORT_AddressStringCallback) (void *cls,
381                                                         const char *address);
382
383
384 /**
385  * Convert the transports address to a nice, human-readable
386  * format.
387  *
388  * @param cls closure
389  * @param name name of the transport that generated the address
390  * @param addr one of the addresses of the host, NULL for the last address
391  *        the specific address format depends on the transport
392  * @param addrlen length of the address
393  * @param numeric should (IP) addresses be displayed in numeric form?
394  * @param timeout after how long should we give up?
395  * @param asc function to call on each string
396  * @param asc_cls closure for asc
397  */
398 typedef void
399   (*GNUNET_TRANSPORT_AddressPrettyPrinter) (void *cls,
400                                             const char *type,
401                                             const void *addr,
402                                             size_t addrlen,
403                                             int numeric,
404                                             struct GNUNET_TIME_Relative
405                                             timeout,
406                                             GNUNET_TRANSPORT_AddressStringCallback
407                                             asc, void *asc_cls);
408
409
410 /**
411  * Set a quota for receiving data from the given peer; this is a
412  * per-transport limit.  The transport should limit its read/select
413  * calls to stay below the quota (in terms of incoming data).
414  *
415  * @param cls closure
416  * @param peer the peer for whom the quota is given
417  * @param quota_in quota for receiving/sending data in bytes per ms
418  */
419 typedef void
420   (*GNUNET_TRANSPORT_SetQuota) (void *cls,
421                                 const struct GNUNET_PeerIdentity * target,
422                                 uint32_t quota_in);
423
424
425 /**
426  * Another peer has suggested an address for this
427  * peer and transport plugin.  Check that this could be a valid
428  * address.  If so, consider adding it to the list
429  * of addresses.
430  *
431  * @param addr pointer to the address
432  * @param addrlen length of addr
433  * @return GNUNET_OK if this is a plausible address for this peer
434  *         and transport
435  */
436 typedef int
437   (*GNUNET_TRANSPORT_SuggestAddress) (void *cls,
438                                       const void *addr, size_t addrlen);
439
440 /**
441  * Each plugin is required to return a pointer to a struct of this
442  * type as the return value from its entry point.
443  */
444 struct GNUNET_TRANSPORT_PluginFunctions
445 {
446
447   /**
448    * Closure for all of the callbacks.
449    */
450   void *cls;
451
452   /**
453    * Function used to send a single message to a particular
454    * peer using the specified address.  Used to validate
455    * HELLOs.
456    */
457   GNUNET_TRANSPORT_ValidationFunction validate;
458
459   /**
460    * Function that the transport service will use to transmit data to
461    * another peer.  May be null for plugins that only support
462    * receiving data.  After this call, the plugin call the specified
463    * continuation with success or error before notifying us about the
464    * target having disconnected.
465    */
466   GNUNET_TRANSPORT_TransmitFunction send;
467
468   /**
469    * Function that can be used to force the plugin to disconnect
470    * from the given peer and cancel all previous transmissions
471    * (and their continuationc).
472    */
473   GNUNET_TRANSPORT_CancelFunction cancel;
474
475   /**
476    * Function to pretty-print addresses.  NOTE: this function is not
477    * yet used by transport-service, but will be used in the future
478    * once the transport-API has been completed.
479    */
480   GNUNET_TRANSPORT_AddressPrettyPrinter address_pretty_printer;
481
482   /**
483    * Function that the transport service can use to try to enforce a
484    * quota for the number of bytes received via this transport.
485    * Transports that can not refuse incoming data (such as UDP)
486    * are free to ignore these calls.
487    */
488   GNUNET_TRANSPORT_SetQuota set_receive_quota;
489
490   /**
491    * Function that will be called if another peer suggested that
492    * we should use a particular address (since he is reaching
493    * us at that address) for this transport.
494    */
495   GNUNET_TRANSPORT_SuggestAddress address_suggested;
496
497   /**
498    * Relative cost of this transport compared to others.  This
499    * is supposed to be a static cost estimate which determines
500    * which plugins should not even be attempted if other,
501    * cheaper transports are already working.  The idea is that
502    * the costs have roughly this relationship:
503    * <pre>
504    * TCP < UDP < HTTP == HTTPS < SMTP
505    * </pre>
506    */
507   unsigned int cost_estimate;
508 };
509
510
511 #endif