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