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