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