(no commit message)
[oweals/gnunet.git] / src / include / gnunet_transport_service.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 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 typedef void (*GNUNET_TRANSPORT_AddressLookUpCallback) (void *cls,
108                                                         const char *address);
109
110
111 /**
112  * Function to call with a binary format of an address
113  *
114  * @param cls closure
115  * @param peer peer identity
116  * @param transport transport plugin
117  * @param addr address
118  * @param addrlen address length
119  */
120 typedef void (*GNUNET_TRANSPORT_AddressLookUpBinaryCallback) (void *cls,
121                                                               const struct
122                                                               GNUNET_PeerIdentity
123                                                               * peer,
124                                                               const char
125                                                               *transport,
126                                                               const void *addr,
127                                                               size_t addrlen);
128
129
130 /**
131  * Connect to the transport service.  Note that the connection may
132  * complete (or fail) asynchronously.
133  *
134  * @param cfg configuration to use
135  * @param self our own identity (API should check that it matches
136  *             the identity found by transport), or NULL (no check)
137  * @param cls closure for the callbacks
138  * @param rec receive function to call
139  * @param nc function to call on connect events
140  * @param nd function to call on disconnect events
141  * @return NULL on error
142  */
143 struct GNUNET_TRANSPORT_Handle *
144 GNUNET_TRANSPORT_connect (const struct GNUNET_CONFIGURATION_Handle *cfg,
145                           const struct GNUNET_PeerIdentity *self, void *cls,
146                           GNUNET_TRANSPORT_ReceiveCallback rec,
147                           GNUNET_TRANSPORT_NotifyConnect nc,
148                           GNUNET_TRANSPORT_NotifyDisconnect nd);
149
150
151 /**
152  * Disconnect from the transport service.
153  *
154  * @param handle handle returned from connect
155  */
156 void
157 GNUNET_TRANSPORT_disconnect (struct GNUNET_TRANSPORT_Handle *handle);
158
159
160 /**
161  * Ask the transport service to establish a connection to
162  * the given peer.
163  *
164  * @param handle connection to transport service
165  * @param target who we should try to connect to
166  */
167 void
168 GNUNET_TRANSPORT_try_connect (struct GNUNET_TRANSPORT_Handle *handle,
169                               const struct GNUNET_PeerIdentity *target);
170
171
172 /**
173  * Opaque handle for a transmission-ready request.
174  */
175 struct GNUNET_TRANSPORT_TransmitHandle;
176
177
178 /**
179  * Check if we could queue a message of the given size for
180  * transmission.  The transport service will take both its internal
181  * buffers and bandwidth limits imposed by the other peer into
182  * consideration when answering this query.
183  *
184  * @param handle connection to transport service
185  * @param target who should receive the message
186  * @param size how big is the message we want to transmit?
187  * @param priority how important is the message? @deprecated - remove?
188  * @param timeout after how long should we give up (and call
189  *        notify with buf NULL and size 0)?
190  * @param notify function to call when we are ready to
191  *        send such a message
192  * @param notify_cls closure for notify
193  * @return NULL if someone else is already waiting to be notified
194  *         non-NULL if the notify callback was queued (can be used to cancel
195  *         using GNUNET_TRANSPORT_notify_transmit_ready_cancel)
196  */
197 struct GNUNET_TRANSPORT_TransmitHandle *
198 GNUNET_TRANSPORT_notify_transmit_ready (struct GNUNET_TRANSPORT_Handle *handle,
199                                         const struct GNUNET_PeerIdentity
200                                         *target, size_t size, uint32_t priority,
201                                         struct GNUNET_TIME_Relative timeout,
202                                         GNUNET_CONNECTION_TransmitReadyNotify
203                                         notify, void *notify_cls);
204
205
206 /**
207  * Cancel the specified transmission-ready notification.
208  *
209  * @param th handle of the transmission notification request to cancel
210  */
211 void
212 GNUNET_TRANSPORT_notify_transmit_ready_cancel (struct
213                                                GNUNET_TRANSPORT_TransmitHandle
214                                                *th);
215
216
217
218 /**
219  * Function called whenever there is an update to the
220  * HELLO of this peer.
221  *
222  * @param cls closure
223  * @param hello our updated HELLO
224  */
225 typedef void (*GNUNET_TRANSPORT_HelloUpdateCallback) (void *cls,
226                                                       const struct
227                                                       GNUNET_MessageHeader *
228                                                       hello);
229
230
231 /**
232  * Handle to cancel a 'GNUNET_TRANSPORT_get_hello' operation.
233  */
234 struct GNUNET_TRANSPORT_GetHelloHandle;
235
236
237 /**
238  * Obtain updates on changes to the HELLO message for this peer.
239  *
240  * @param handle connection to transport service
241  * @param rec function to call with the HELLO
242  * @param rec_cls closure for rec
243  * @return handle to cancel the operation
244  */
245 struct GNUNET_TRANSPORT_GetHelloHandle *
246 GNUNET_TRANSPORT_get_hello (struct GNUNET_TRANSPORT_Handle *handle,
247                             GNUNET_TRANSPORT_HelloUpdateCallback rec,
248                             void *rec_cls);
249
250
251 /**
252  * Stop receiving updates about changes to our HELLO message.
253  *
254  * @param ghh handle returned from 'GNUNET_TRANSPORT_get_hello')
255  */
256 void
257 GNUNET_TRANSPORT_get_hello_cancel (struct GNUNET_TRANSPORT_GetHelloHandle *ghh);
258
259
260 /**
261  * Offer the transport service the HELLO of another peer.  Note that
262  * the transport service may just ignore this message if the HELLO is
263  * malformed or useless due to our local configuration.
264  *
265  * @param handle connection to transport service
266  * @param hello the hello message
267  * @param cont continuation to call when HELLO has been sent
268  * @param cls closure for continuation
269  */
270 void
271 GNUNET_TRANSPORT_offer_hello (struct GNUNET_TRANSPORT_Handle *handle,
272                               const struct GNUNET_MessageHeader *hello,
273                               GNUNET_SCHEDULER_Task cont, void *cls);
274
275
276 /**
277  * Handle to cancel a pending address lookup.
278  */
279 struct GNUNET_TRANSPORT_AddressLookupContext;
280
281
282 /**
283  * Convert a binary address into a human readable address.
284  *
285  * @param cfg configuration to use
286  * @param address address to convert (binary format)
287  * @param addressLen number of bytes in address
288  * @param numeric should (IP) addresses be displayed in numeric form
289  *                (otherwise do reverse DNS lookup)
290  * @param nameTrans name of the transport to which the address belongs
291  * @param timeout how long is the lookup allowed to take at most
292  * @param aluc function to call with the results
293  * @param aluc_cls closure for aluc
294  * @return handle to cancel the operation, NULL on error
295  */
296 // FIXME: use 'GNUNET_HELLO_Address' here!
297 struct GNUNET_TRANSPORT_AddressLookupContext *
298 GNUNET_TRANSPORT_address_lookup (const struct GNUNET_CONFIGURATION_Handle *cfg,
299                                  const char *address, size_t addressLen,
300                                  int numeric, const char *nameTrans,
301                                  struct GNUNET_TIME_Relative timeout,
302                                  GNUNET_TRANSPORT_AddressLookUpCallback aluc,
303                                  void *aluc_cls);
304
305
306 /**
307  * Cancel request for address conversion.
308  *
309  * @param alc handle for the request to cancel
310  */
311 void
312 GNUNET_TRANSPORT_address_lookup_cancel (struct
313                                         GNUNET_TRANSPORT_AddressLookupContext
314                                         *alc);
315
316
317 /**
318  * Handle to cancel a pending address lookup.
319  */
320 struct GNUNET_TRANSPORT_AddressLookupContext;
321
322
323 /**
324  * Return all the known addresses for a peer. FIXME: document better!
325  * FIXME: use better name!
326  *
327  * @param cfg configuration to use
328  * @param peer peer identity to look up the addresses of
329  * @param timeout how long is the lookup allowed to take at most
330  * @param peer_address_callback function to call with the results
331  * @param peer_address_callback_cls closure for peer_address_callback
332  */
333 struct GNUNET_TRANSPORT_PeerAddressLookupContext *
334 GNUNET_TRANSPORT_peer_address_lookup (const struct GNUNET_CONFIGURATION_Handle
335                                       *cfg,
336                                       const struct GNUNET_PeerIdentity *peer,
337                                       struct GNUNET_TIME_Relative timeout,
338                                       GNUNET_TRANSPORT_AddressLookUpCallback
339                                       peer_address_callback,
340                                       void *peer_address_callback_cls);
341
342
343 /**
344  * Cancel request for peer lookup.
345  *
346  * @param alc handle for the request to cancel
347  */
348 void
349 GNUNET_TRANSPORT_peer_address_lookup_cancel (struct
350                                              GNUNET_TRANSPORT_PeerAddressLookupContext
351                                              *alc);
352
353
354 /**
355  * Return all the known addresses. FIXME: document better!
356  * FIXME: use better name! FIXME: extend API to allow cancellation!
357  *
358  * @param cfg configuration to use
359  * @param timeout how long is the lookup allowed to take at most
360  * @param peer_address_callback function to call with the results
361  * @param peer_address_callback_cls closure for peer_address_callback
362  */
363 void
364 GNUNET_TRANSPORT_address_iterate (const struct GNUNET_CONFIGURATION_Handle *cfg,
365                                   struct GNUNET_TIME_Relative timeout,
366                                   GNUNET_TRANSPORT_AddressLookUpBinaryCallback
367                                   peer_address_callback,
368                                   void *peer_address_callback_cls);
369
370
371 /**
372  * Handle for blacklisting peers.
373  */
374 struct GNUNET_TRANSPORT_Blacklist;
375
376
377 /**
378  * Function that decides if a connection is acceptable or not.
379  *
380  * @param cls closure
381  * @param pid peer to approve or disapproave
382  * @return GNUNET_OK if the connection is allowed
383  */
384 typedef int (*GNUNET_TRANSPORT_BlacklistCallback) (void *cls,
385                                                    const struct
386                                                    GNUNET_PeerIdentity * pid);
387
388
389 /**
390  * Install a blacklist callback.  The service will be queried for all
391  * existing connections as well as any fresh connections to check if
392  * they are permitted.  If the blacklisting callback is unregistered,
393  * all hosts that were denied in the past will automatically be
394  * whitelisted again.  Cancelling the blacklist handle is also the
395  * only way to re-enable connections from peers that were previously
396  * blacklisted.
397  *
398  * @param cfg configuration to use
399  * @param cb callback to invoke to check if connections are allowed
400  * @param cb_cls closure for cb
401  * @return NULL on error, otherwise handle for cancellation
402  */
403 struct GNUNET_TRANSPORT_Blacklist *
404 GNUNET_TRANSPORT_blacklist (const struct GNUNET_CONFIGURATION_Handle *cfg,
405                             GNUNET_TRANSPORT_BlacklistCallback cb,
406                             void *cb_cls);
407
408
409 /**
410  * Abort the blacklist.  Note that this function is the only way for
411  * removing a peer from the blacklist.
412  *
413  * @param br handle of the request that is to be cancelled
414  */
415 void
416 GNUNET_TRANSPORT_blacklist_cancel (struct GNUNET_TRANSPORT_Blacklist *br);
417
418
419
420 #if 0                           /* keep Emacsens' auto-indent happy */
421 {
422 #endif
423 #ifdef __cplusplus
424 }
425 #endif
426
427 /* ifndef GNUNET_TRANSPORT_SERVICE_H */
428 #endif
429 /* end of gnunet_transport_service.h */