add completion callback for overlay topology configure functions
[oweals/gnunet.git] / src / include / gnunet_transport_service.h
1 /*
2      This file is part of GNUnet.
3      (C) 2009, 2010, 2011 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 3, 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 include/gnunet_transport_service.h
23  * @brief low-level P2P IO
24  * @author Christian Grothoff
25  */
26
27 #ifndef GNUNET_TRANSPORT_SERVICE_H
28 #define GNUNET_TRANSPORT_SERVICE_H
29
30 #ifdef __cplusplus
31 extern "C"
32 {
33 #if 0                           /* keep Emacsens' auto-indent happy */
34 }
35 #endif
36 #endif
37
38 #include "gnunet_util_lib.h"
39 #include "gnunet_ats_service.h"
40
41 /**
42  * Version number of the transport API.
43  */
44 #define GNUNET_TRANSPORT_VERSION 0x00000000
45
46 enum TRAFFIC_METRIC_DIRECTION
47 {
48         TM_SEND = 0,
49         TM_RECEIVE = 1,
50         TM_BOTH = 2
51 };
52
53
54 /**
55  * Function called by the transport for each received message.
56  *
57  * @param cls closure
58  * @param peer (claimed) identity of the other peer
59  * @param message the message
60  * @param ats performance data
61  * @param ats_count number of entries in ats
62  */
63 typedef void (*GNUNET_TRANSPORT_ReceiveCallback) (void *cls,
64                                                   const struct
65                                                   GNUNET_PeerIdentity * peer,
66                                                   const struct
67                                                   GNUNET_MessageHeader *
68                                                   message,
69                                                   const struct
70                                                   GNUNET_ATS_Information * ats,
71                                                   uint32_t ats_count);
72
73
74 /**
75  * Opaque handle to the service.
76  */
77 struct GNUNET_TRANSPORT_Handle;
78
79
80 /**
81  * Function called to notify transport users that another
82  * peer connected to us.
83  *
84  * @param cls closure
85  * @param peer the peer that connected
86  * @param ats performance data
87  * @param ats_count number of entries in ats (excluding 0-termination)
88  */
89 typedef void (*GNUNET_TRANSPORT_NotifyConnect) (void *cls,
90                                                 const struct GNUNET_PeerIdentity
91                                                 * peer,
92                                                 const struct
93                                                 GNUNET_ATS_Information * ats,
94                                                 uint32_t ats_count);
95
96 /**
97  * Function called to notify transport users that another
98  * peer disconnected from us.
99  *
100  * @param cls closure
101  * @param peer the peer that disconnected
102  */
103 typedef void (*GNUNET_TRANSPORT_NotifyDisconnect) (void *cls,
104                                                    const struct
105                                                    GNUNET_PeerIdentity * peer);
106
107
108 /**
109  * Function to call with result of the try connect request.
110  *
111  *
112  * @param cls closure
113  * @param result GNUNET_OK if message was transmitted to transport service
114  *               GNUNET_SYSERR if message was not transmitted to transport service
115  */
116 typedef void (*GNUNET_TRANSPORT_TryConnectCallback) (void *cls,
117                                                      const int result);
118
119
120 /**
121  * Function to call with a textual representation of an address.
122  * This function will be called several times with different possible
123  * textual representations, and a last time with NULL to signal the end
124  * of the iteration.
125  *
126  * @param cls closure
127  * @param address NULL on error or end of iteration,
128  *        otherwise 0-terminated printable UTF-8 string
129  */
130 typedef void (*GNUNET_TRANSPORT_AddressToStringCallback) (void *cls,
131                                                           const char *address);
132
133
134 /**
135  * Function to call with a binary format of an address
136  *
137  * @param cls closure
138  * @param peer peer this update is about (never NULL)
139  * @param address address, NULL for disconnect notification in monitor mode
140  */
141 typedef void (*GNUNET_TRANSPORT_PeerIterateCallback) (void *cls,
142                                                       const struct
143                                                       GNUNET_PeerIdentity *
144                                                       peer,
145                                                       const struct
146                                                       GNUNET_HELLO_Address *
147                                                       address);
148
149
150 /**
151  * Connect to the transport service.  Note that the connection may
152  * complete (or fail) asynchronously.
153  *
154  * @param cfg configuration to use
155  * @param self our own identity (API should check that it matches
156  *             the identity found by transport), or NULL (no check)
157  * @param cls closure for the callbacks
158  * @param rec receive function to call
159  * @param nc function to call on connect events
160  * @param nd function to call on disconnect events
161  * @return NULL on error
162  */
163 struct GNUNET_TRANSPORT_Handle *
164 GNUNET_TRANSPORT_connect (const struct GNUNET_CONFIGURATION_Handle *cfg,
165                           const struct GNUNET_PeerIdentity *self, void *cls,
166                           GNUNET_TRANSPORT_ReceiveCallback rec,
167                           GNUNET_TRANSPORT_NotifyConnect nc,
168                           GNUNET_TRANSPORT_NotifyDisconnect nd);
169
170
171 /**
172  * Disconnect from the transport service.
173  *
174  * @param handle handle returned from connect
175  */
176 void
177 GNUNET_TRANSPORT_disconnect (struct GNUNET_TRANSPORT_Handle *handle);
178
179
180 /**
181  * Opaque handle for a transmission-ready request.
182  */
183 struct GNUNET_TRANSPORT_TryConnectHandle;
184
185
186 /**
187  * Ask the transport service to establish a connection to
188  * the given peer.
189  *
190  * @param handle connection to transport service
191  * @param target who we should try to connect to
192  * @param cb callback to be called when request was transmitted to transport
193  *         service
194  * @param cb_cls closure for the callback
195  * @return a GNUNET_TRANSPORT_TryConnectHandle handle or
196  *         NULL on failure (cb will not be called)
197  */
198 struct GNUNET_TRANSPORT_TryConnectHandle *
199 GNUNET_TRANSPORT_try_connect (struct GNUNET_TRANSPORT_Handle *handle,
200                               const struct GNUNET_PeerIdentity *target,
201                               GNUNET_TRANSPORT_TryConnectCallback cb,
202                               void *cb_cls);
203
204
205 /**
206  * Cancel the request to transport to try a connect
207  * Callback will not be called
208  *
209  * @param tch GNUNET_TRANSPORT_TryConnectHandle handle to cancel
210  */
211 void
212 GNUNET_TRANSPORT_try_connect_cancel (struct GNUNET_TRANSPORT_TryConnectHandle *tch);
213
214 /**
215  * Opaque handle for a transmission-ready request.
216  */
217 struct GNUNET_TRANSPORT_TransmitHandle;
218
219
220 /**
221  * Check if we could queue a message of the given size for
222  * transmission.  The transport service will take both its internal
223  * buffers and bandwidth limits imposed by the other peer into
224  * consideration when answering this query.
225  *
226  * @param handle connection to transport service
227  * @param target who should receive the message
228  * @param size how big is the message we want to transmit?
229  * @param priority how important is the message? @deprecated - remove?
230  * @param timeout after how long should we give up (and call
231  *        notify with buf NULL and size 0)?
232  * @param notify function to call when we are ready to
233  *        send such a message
234  * @param notify_cls closure for notify
235  * @return NULL if someone else is already waiting to be notified
236  *         non-NULL if the notify callback was queued (can be used to cancel
237  *         using GNUNET_TRANSPORT_notify_transmit_ready_cancel)
238  */
239 struct GNUNET_TRANSPORT_TransmitHandle *
240 GNUNET_TRANSPORT_notify_transmit_ready (struct GNUNET_TRANSPORT_Handle *handle,
241                                         const struct GNUNET_PeerIdentity
242                                         *target, size_t size, uint32_t priority,
243                                         struct GNUNET_TIME_Relative timeout,
244                                         GNUNET_CONNECTION_TransmitReadyNotify
245                                         notify, void *notify_cls);
246
247
248 /**
249  * Cancel the specified transmission-ready notification.
250  *
251  * @param th handle of the transmission notification request to cancel
252  */
253 void
254 GNUNET_TRANSPORT_notify_transmit_ready_cancel (struct
255                                                GNUNET_TRANSPORT_TransmitHandle
256                                                *th);
257
258
259
260 /**
261  * Function called whenever there is an update to the
262  * HELLO of this peer.
263  *
264  * @param cls closure
265  * @param hello our updated HELLO
266  */
267 typedef void (*GNUNET_TRANSPORT_HelloUpdateCallback) (void *cls,
268                                                       const struct
269                                                       GNUNET_MessageHeader *
270                                                       hello);
271
272
273 /**
274  * Handle to cancel a 'GNUNET_TRANSPORT_get_hello' operation.
275  */
276 struct GNUNET_TRANSPORT_GetHelloHandle;
277
278
279 /**
280   * Checks if a neighbour is connected
281   *
282   * @param handle connection to transport service
283   * @peer the peer to check
284   * @return GNUNET_YES or GNUNET_NO
285   *
286   */
287 int
288 GNUNET_TRANSPORT_check_neighbour_connected (struct GNUNET_TRANSPORT_Handle *handle,
289                                                                 const struct GNUNET_PeerIdentity *peer);
290
291
292 void
293 GNUNET_TRANSPORT_set_traffic_metric (struct GNUNET_TRANSPORT_Handle *handle,
294                                                                                                                                                 const struct GNUNET_PeerIdentity *peer,
295                                                                                                                                                 int direction,
296                                                                                                                                                 const struct GNUNET_ATS_Information *ats,
297                                                                                                                                                 size_t ats_count);
298
299
300 /**
301  * Obtain updates on changes to the HELLO message for this peer. The callback
302  * given in this function is never called synchronously.
303  *
304  * @param handle connection to transport service
305  * @param rec function to call with the HELLO
306  * @param rec_cls closure for rec
307  * @return handle to cancel the operation
308  */
309 struct GNUNET_TRANSPORT_GetHelloHandle *
310 GNUNET_TRANSPORT_get_hello (struct GNUNET_TRANSPORT_Handle *handle,
311                             GNUNET_TRANSPORT_HelloUpdateCallback rec,
312                             void *rec_cls);
313
314
315 /**
316  * Stop receiving updates about changes to our HELLO message.
317  *
318  * @param ghh handle to cancel
319  */
320 void
321 GNUNET_TRANSPORT_get_hello_cancel (struct GNUNET_TRANSPORT_GetHelloHandle *ghh);
322
323
324 /**
325  * Offer the transport service the HELLO of another peer.  Note that
326  * the transport service may just ignore this message if the HELLO is
327  * malformed or useless due to our local configuration.
328  *
329  * @param handle connection to transport service
330  * @param hello the hello message
331  * @param cont continuation to call when HELLO has been sent,
332  *      tc reason GNUNET_SCHEDULER_REASON_TIMEOUT for fail
333  *      tc reasong GNUNET_SCHEDULER_REASON_READ_READY for success
334  * @param cls closure for continuation
335  * @return a GNUNET_TRANSPORT_OfferHelloHandle handle or NULL on failure,
336  *      in case of failure cont will not be called
337  *
338  */
339 struct GNUNET_TRANSPORT_OfferHelloHandle *
340 GNUNET_TRANSPORT_offer_hello (struct GNUNET_TRANSPORT_Handle *handle,
341                               const struct GNUNET_MessageHeader *hello,
342                               GNUNET_SCHEDULER_Task cont, void *cls);
343
344
345 /**
346  * Cancel the request to transport to offer the HELLO message
347  *
348  * @param ohh the GNUNET_TRANSPORT_OfferHelloHandle to cancel
349  */
350 void
351 GNUNET_TRANSPORT_offer_hello_cancel (struct GNUNET_TRANSPORT_OfferHelloHandle *ohh);
352
353 /**
354  * Handle to cancel a pending address lookup.
355  */
356 struct GNUNET_TRANSPORT_AddressToStringContext;
357
358
359 /**
360  * Convert a binary address into a human readable address.
361  *
362  * @param cfg configuration to use
363  * @param address address to convert (binary format)
364  * @param numeric should (IP) addresses be displayed in numeric form
365  *                (otherwise do reverse DNS lookup)
366  * @param timeout how long is the lookup allowed to take at most
367  * @param aluc function to call with the results
368  * @param aluc_cls closure for aluc
369  * @return handle to cancel the operation, NULL on error
370  */
371 struct GNUNET_TRANSPORT_AddressToStringContext *
372 GNUNET_TRANSPORT_address_to_string (const struct GNUNET_CONFIGURATION_Handle
373                                     *cfg,
374                                     const struct GNUNET_HELLO_Address *address,
375                                     int numeric,
376                                     struct GNUNET_TIME_Relative timeout,
377                                     GNUNET_TRANSPORT_AddressToStringCallback
378                                     aluc, void *aluc_cls);
379
380
381 /**
382  * Cancel request for address conversion.
383  *
384  * @param alc handle for the request to cancel
385  */
386 void
387 GNUNET_TRANSPORT_address_to_string_cancel (struct
388                                            GNUNET_TRANSPORT_AddressToStringContext
389                                            *alc);
390
391
392 /**
393  * Return all the known addresses for a specific peer or all peers.
394  * Returns continuously all address if one_shot is set to GNUNET_NO
395  *
396  * CHANGE: Returns the address(es) that we are currently using for this
397  * peer.  Upon completion, the 'AddressLookUpCallback' is called one more
398  * time with 'NULL' for the address and the peer.  After this, the operation must no
399  * longer be explicitly cancelled.
400  *
401  * @param cfg configuration to use
402  * @param peer peer identity to look up the addresses of, CHANGE: allow NULL for all (connected) peers
403  * @param one_shot GNUNET_YES to return the current state and then end (with NULL+NULL),
404  *                 GNUNET_NO to monitor the set of addresses used (continuously, must be explicitly canceled, NOT implemented yet!)
405  * @param timeout how long is the lookup allowed to take at most
406  * @param peer_address_callback function to call with the results
407  * @param peer_address_callback_cls closure for peer_address_callback
408  */
409 struct GNUNET_TRANSPORT_PeerIterateContext *
410 GNUNET_TRANSPORT_peer_get_active_addresses (const struct
411                                             GNUNET_CONFIGURATION_Handle *cfg,
412                                             const struct GNUNET_PeerIdentity
413                                             *peer, int one_shot,
414                                             struct GNUNET_TIME_Relative timeout,
415                                             GNUNET_TRANSPORT_PeerIterateCallback
416                                             peer_address_callback,
417                                             void *peer_address_callback_cls);
418
419
420 /**
421  * Cancel request for peer lookup.
422  *
423  * @param alc handle for the request to cancel
424  */
425 void
426 GNUNET_TRANSPORT_peer_get_active_addresses_cancel (struct
427                                                    GNUNET_TRANSPORT_PeerIterateContext
428                                                    *alc);
429
430
431 /**
432  * Handle for blacklisting peers.
433  */
434 struct GNUNET_TRANSPORT_Blacklist;
435
436
437 /**
438  * Function that decides if a connection is acceptable or not.
439  *
440  * @param cls closure
441  * @param pid peer to approve or disapproave
442  * @return GNUNET_OK if the connection is allowed, GNUNET_SYSERR if not
443  */
444 typedef int (*GNUNET_TRANSPORT_BlacklistCallback) (void *cls,
445                                                    const struct
446                                                    GNUNET_PeerIdentity * pid);
447
448
449 /**
450  * Install a blacklist callback.  The service will be queried for all
451  * existing connections as well as any fresh connections to check if
452  * they are permitted.  If the blacklisting callback is unregistered,
453  * all hosts that were denied in the past will automatically be
454  * whitelisted again.  Cancelling the blacklist handle is also the
455  * only way to re-enable connections from peers that were previously
456  * blacklisted.
457  *
458  * @param cfg configuration to use
459  * @param cb callback to invoke to check if connections are allowed
460  * @param cb_cls closure for cb
461  * @return NULL on error, otherwise handle for cancellation
462  */
463 struct GNUNET_TRANSPORT_Blacklist *
464 GNUNET_TRANSPORT_blacklist (const struct GNUNET_CONFIGURATION_Handle *cfg,
465                             GNUNET_TRANSPORT_BlacklistCallback cb,
466                             void *cb_cls);
467
468
469 /**
470  * Abort the blacklist.  Note that this function is the only way for
471  * removing a peer from the blacklist.
472  *
473  * @param br handle of the request that is to be cancelled
474  */
475 void
476 GNUNET_TRANSPORT_blacklist_cancel (struct GNUNET_TRANSPORT_Blacklist *br);
477
478
479
480 #if 0                           /* keep Emacsens' auto-indent happy */
481 {
482 #endif
483 #ifdef __cplusplus
484 }
485 #endif
486
487 /* ifndef GNUNET_TRANSPORT_SERVICE_H */
488 #endif
489 /* end of gnunet_transport_service.h */