-document desired direction of transport API change
[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
47 /**
48  * Function called by the transport for each received message.
49  *
50  * @param cls closure
51  * @param peer (claimed) identity of the other peer
52  * @param message the message
53  * @param ats performance data
54  * @param ats_count number of entries in ats
55  */
56 typedef void (*GNUNET_TRANSPORT_ReceiveCallback) (void *cls,
57                                                   const struct
58                                                   GNUNET_PeerIdentity * peer,
59                                                   const struct
60                                                   GNUNET_MessageHeader *
61                                                   message,
62                                                   const struct
63                                                   GNUNET_ATS_Information * ats,
64                                                   uint32_t ats_count);
65
66
67 /**
68  * Opaque handle to the service.
69  */
70 struct GNUNET_TRANSPORT_Handle;
71
72
73 /**
74  * Function called to notify transport users that another
75  * peer connected to us.
76  *
77  * @param cls closure
78  * @param peer the peer that connected
79  * @param ats performance data
80  * @param ats_count number of entries in ats (excluding 0-termination)
81  */
82 typedef void (*GNUNET_TRANSPORT_NotifyConnect) (void *cls,
83                                                 const struct GNUNET_PeerIdentity
84                                                 * peer,
85                                                 const struct
86                                                 GNUNET_ATS_Information * ats,
87                                                 uint32_t ats_count);
88
89 /**
90  * Function called to notify transport users that another
91  * peer disconnected from us.
92  *
93  * @param cls closure
94  * @param peer the peer that disconnected
95  */
96 typedef void (*GNUNET_TRANSPORT_NotifyDisconnect) (void *cls,
97                                                    const struct
98                                                    GNUNET_PeerIdentity * peer);
99
100
101 /**
102  * Function to call with a binary format of an address
103  *
104  * @param cls closure
105  * @param address NULL on error, otherwise 0-terminated printable UTF-8 string
106  */
107 // RENAME: AddressToStringCallback
108 typedef void (*GNUNET_TRANSPORT_AddressLookUpCallback) (void *cls,
109                                                         const char *address);
110
111
112 /**
113  * Function to call with a binary format of an address
114  *
115  * @param cls closure
116  * @param peer peer identity
117  * @param transport transport plugin
118  * @param addr address
119  * @param addrlen address length
120  */
121 // FIXME: use GNUNET_HELLO_Address (as 2nd arg, replacing others)
122 // FIXME: rename: remove "Binary"
123 // FIXME: use NULL for address on disconnect IF in monitor mode (one_shot = NO)
124 typedef void (*GNUNET_TRANSPORT_AddressLookUpBinaryCallback) (void *cls,
125                                                               const struct
126                                                               GNUNET_PeerIdentity
127                                                               * peer,
128                                                               const char
129                                                               *transport,
130                                                               const void *addr,
131                                                               size_t addrlen);
132
133
134 /**
135  * Connect to the transport service.  Note that the connection may
136  * complete (or fail) asynchronously.
137  *
138  * @param cfg configuration to use
139  * @param self our own identity (API should check that it matches
140  *             the identity found by transport), or NULL (no check)
141  * @param cls closure for the callbacks
142  * @param rec receive function to call
143  * @param nc function to call on connect events
144  * @param nd function to call on disconnect events
145  * @return NULL on error
146  */
147 struct GNUNET_TRANSPORT_Handle *
148 GNUNET_TRANSPORT_connect (const struct GNUNET_CONFIGURATION_Handle *cfg,
149                           const struct GNUNET_PeerIdentity *self, void *cls,
150                           GNUNET_TRANSPORT_ReceiveCallback rec,
151                           GNUNET_TRANSPORT_NotifyConnect nc,
152                           GNUNET_TRANSPORT_NotifyDisconnect nd);
153
154
155 /**
156  * Disconnect from the transport service.
157  *
158  * @param handle handle returned from connect
159  */
160 void
161 GNUNET_TRANSPORT_disconnect (struct GNUNET_TRANSPORT_Handle *handle);
162
163
164 /**
165  * Ask the transport service to establish a connection to
166  * the given peer.
167  *
168  * @param handle connection to transport service
169  * @param target who we should try to connect to
170  */
171 void
172 GNUNET_TRANSPORT_try_connect (struct GNUNET_TRANSPORT_Handle *handle,
173                               const struct GNUNET_PeerIdentity *target);
174
175
176 /**
177  * Opaque handle for a transmission-ready request.
178  */
179 struct GNUNET_TRANSPORT_TransmitHandle;
180
181
182 /**
183  * Check if we could queue a message of the given size for
184  * transmission.  The transport service will take both its internal
185  * buffers and bandwidth limits imposed by the other peer into
186  * consideration when answering this query.
187  *
188  * @param handle connection to transport service
189  * @param target who should receive the message
190  * @param size how big is the message we want to transmit?
191  * @param priority how important is the message? @deprecated - remove?
192  * @param timeout after how long should we give up (and call
193  *        notify with buf NULL and size 0)?
194  * @param notify function to call when we are ready to
195  *        send such a message
196  * @param notify_cls closure for notify
197  * @return NULL if someone else is already waiting to be notified
198  *         non-NULL if the notify callback was queued (can be used to cancel
199  *         using GNUNET_TRANSPORT_notify_transmit_ready_cancel)
200  */
201 struct GNUNET_TRANSPORT_TransmitHandle *
202 GNUNET_TRANSPORT_notify_transmit_ready (struct GNUNET_TRANSPORT_Handle *handle,
203                                         const struct GNUNET_PeerIdentity
204                                         *target, size_t size, uint32_t priority,
205                                         struct GNUNET_TIME_Relative timeout,
206                                         GNUNET_CONNECTION_TransmitReadyNotify
207                                         notify, void *notify_cls);
208
209
210 /**
211  * Cancel the specified transmission-ready notification.
212  *
213  * @param th handle of the transmission notification request to cancel
214  */
215 void
216 GNUNET_TRANSPORT_notify_transmit_ready_cancel (struct
217                                                GNUNET_TRANSPORT_TransmitHandle
218                                                *th);
219
220
221
222 /**
223  * Function called whenever there is an update to the
224  * HELLO of this peer.
225  *
226  * @param cls closure
227  * @param hello our updated HELLO
228  */
229 typedef void (*GNUNET_TRANSPORT_HelloUpdateCallback) (void *cls,
230                                                       const struct
231                                                       GNUNET_MessageHeader *
232                                                       hello);
233
234
235 /**
236  * Handle to cancel a 'GNUNET_TRANSPORT_get_hello' operation.
237  */
238 struct GNUNET_TRANSPORT_GetHelloHandle;
239
240
241 /**
242  * Obtain updates on changes to the HELLO message for this peer.
243  *
244  * @param handle connection to transport service
245  * @param rec function to call with the HELLO
246  * @param rec_cls closure for rec
247  * @return handle to cancel the operation
248  */
249 struct GNUNET_TRANSPORT_GetHelloHandle *
250 GNUNET_TRANSPORT_get_hello (struct GNUNET_TRANSPORT_Handle *handle,
251                             GNUNET_TRANSPORT_HelloUpdateCallback rec,
252                             void *rec_cls);
253
254
255 /**
256  * Stop receiving updates about changes to our HELLO message.
257  *
258  * @param ghh handle returned from 'GNUNET_TRANSPORT_get_hello')
259  */
260 void
261 GNUNET_TRANSPORT_get_hello_cancel (struct GNUNET_TRANSPORT_GetHelloHandle *ghh);
262
263
264 /**
265  * Offer the transport service the HELLO of another peer.  Note that
266  * the transport service may just ignore this message if the HELLO is
267  * malformed or useless due to our local configuration.
268  *
269  * @param handle connection to transport service
270  * @param hello the hello message
271  * @param cont continuation to call when HELLO has been sent
272  * @param cls closure for continuation
273  */
274 void
275 GNUNET_TRANSPORT_offer_hello (struct GNUNET_TRANSPORT_Handle *handle,
276                               const struct GNUNET_MessageHeader *hello,
277                               GNUNET_SCHEDULER_Task cont, void *cls);
278
279
280 /**
281  * Handle to cancel a pending address lookup.
282  */
283 struct GNUNET_TRANSPORT_AddressLookupContext;
284
285
286 /**
287  * Convert a binary address into a human readable address.
288  *
289  * @param cfg configuration to use
290  * @param address address to convert (binary format)
291  * @param addressLen number of bytes in address
292  * @param numeric should (IP) addresses be displayed in numeric form
293  *                (otherwise do reverse DNS lookup)
294  * @param nameTrans name of the transport to which the address belongs
295  * @param timeout how long is the lookup allowed to take at most
296  * @param aluc function to call with the results
297  * @param aluc_cls closure for aluc
298  * @return handle to cancel the operation, NULL on error
299  */
300 // FIXME: use 'GNUNET_HELLO_Address' here!
301 // => rename: address_to_string
302 struct GNUNET_TRANSPORT_AddressLookupContext *
303 GNUNET_TRANSPORT_address_lookup (const struct GNUNET_CONFIGURATION_Handle *cfg,
304                                  const char *address, size_t addressLen,
305                                  int numeric, const char *nameTrans,
306                                  struct GNUNET_TIME_Relative timeout,
307                                  GNUNET_TRANSPORT_AddressLookUpCallback aluc,
308                                  void *aluc_cls);
309
310
311 /**
312  * Cancel request for address conversion.
313  *
314  * @param alc handle for the request to cancel
315  */
316 void
317 GNUNET_TRANSPORT_address_lookup_cancel (struct
318                                         GNUNET_TRANSPORT_AddressLookupContext
319                                         *alc);
320
321
322 /**
323  * Handle to cancel a pending address lookup.
324  */
325 struct GNUNET_TRANSPORT_AddressLookupContext;
326
327
328 /**
329  * Return all the known addresses for a peer. FIXME: document better!
330  * FIXME: use better name!
331  * CHANGE: Returns the address(es) that we are currently using for this
332  * peer.  Upon completion, the 'AddressLookUpCallback' is called one more
333  * time with 'NULL' for the address and the peer.  After this, the operation must no
334  * longer be explicitly cancelled.
335  * TODO: change code that uses this API to see if this is fine...
336  *
337  * @param cfg configuration to use
338  * @param peer peer identity to look up the addresses of, CHANGE: allow NULL for all (connected) peers
339  * FIXME: @param one_shot GNUNET_YES to return the current state and then end (with NULL+NULL), 
340  *                        GNUNET_NO to monitor the set of addresses used (continuously, must be explicitly cancelled)
341  * @param timeout how long is the lookup allowed to take at most
342  * @param peer_address_callback function to call with the results
343  * @param peer_address_callback_cls closure for peer_address_callback
344  */
345 // RENAME: peer_get_active_addresses
346 struct GNUNET_TRANSPORT_PeerAddressLookupContext *
347 GNUNET_TRANSPORT_peer_address_lookup (const struct GNUNET_CONFIGURATION_Handle
348                                       *cfg,
349                                       const struct GNUNET_PeerIdentity *peer,
350                                       // FIXME: add argument: one_shot
351                                       struct GNUNET_TIME_Relative timeout,
352                                       // FIXME: change to the 'Binary' lookup callback (which will
353                                       // be renamed, so the argument type actually won't change)
354                                       GNUNET_TRANSPORT_AddressLookUpCallback
355                                       peer_address_callback,
356                                       void *peer_address_callback_cls);
357
358
359 /**
360  * Cancel request for peer lookup.
361  *
362  * @param alc handle for the request to cancel
363  */
364 void
365 GNUNET_TRANSPORT_peer_address_lookup_cancel (struct
366                                              GNUNET_TRANSPORT_PeerAddressLookupContext
367                                              *alc);
368
369
370 /**
371  * Return all the known addresses. FIXME: document better!
372  * 
373  * FIXME: remove, replace with new 'peer_address_lookup' API
374  * 
375  *
376  * @param cfg configuration to use
377  * @param timeout how long is the lookup allowed to take at most
378  * @param peer_address_callback function to call with the results
379  * @param peer_address_callback_cls closure for peer_address_callback
380  */
381 void
382 GNUNET_TRANSPORT_address_iterate (const struct GNUNET_CONFIGURATION_Handle *cfg,
383                                   struct GNUNET_TIME_Relative timeout,
384                                   GNUNET_TRANSPORT_AddressLookUpBinaryCallback
385                                   peer_address_callback,
386                                   void *peer_address_callback_cls);
387
388
389 /**
390  * Handle for blacklisting peers.
391  */
392 struct GNUNET_TRANSPORT_Blacklist;
393
394
395 /**
396  * Function that decides if a connection is acceptable or not.
397  *
398  * @param cls closure
399  * @param pid peer to approve or disapproave
400  * @return GNUNET_OK if the connection is allowed
401  */
402 typedef int (*GNUNET_TRANSPORT_BlacklistCallback) (void *cls,
403                                                    const struct
404                                                    GNUNET_PeerIdentity * pid);
405
406
407 /**
408  * Install a blacklist callback.  The service will be queried for all
409  * existing connections as well as any fresh connections to check if
410  * they are permitted.  If the blacklisting callback is unregistered,
411  * all hosts that were denied in the past will automatically be
412  * whitelisted again.  Cancelling the blacklist handle is also the
413  * only way to re-enable connections from peers that were previously
414  * blacklisted.
415  *
416  * @param cfg configuration to use
417  * @param cb callback to invoke to check if connections are allowed
418  * @param cb_cls closure for cb
419  * @return NULL on error, otherwise handle for cancellation
420  */
421 struct GNUNET_TRANSPORT_Blacklist *
422 GNUNET_TRANSPORT_blacklist (const struct GNUNET_CONFIGURATION_Handle *cfg,
423                             GNUNET_TRANSPORT_BlacklistCallback cb,
424                             void *cb_cls);
425
426
427 /**
428  * Abort the blacklist.  Note that this function is the only way for
429  * removing a peer from the blacklist.
430  *
431  * @param br handle of the request that is to be cancelled
432  */
433 void
434 GNUNET_TRANSPORT_blacklist_cancel (struct GNUNET_TRANSPORT_Blacklist *br);
435
436
437
438 #if 0                           /* keep Emacsens' auto-indent happy */
439 {
440 #endif
441 #ifdef __cplusplus
442 }
443 #endif
444
445 /* ifndef GNUNET_TRANSPORT_SERVICE_H */
446 #endif
447 /* end of gnunet_transport_service.h */