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