fast timeout on disconnect
[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_bandwidth_lib.h"
39 #include "gnunet_configuration_lib.h"
40 #include "gnunet_crypto_lib.h"
41 #include "gnunet_connection_lib.h"
42 #include "gnunet_scheduler_lib.h"
43 #include "gnunet_time_lib.h"
44
45 /**
46  * Version number of the transport API.
47  */
48 #define GNUNET_TRANSPORT_VERSION 0x00000000
49
50 /**
51  * Function called by the transport for each received message.
52  *
53  * @param cls closure
54  * @param peer (claimed) identity of the other peer
55  * @param message the message
56  * @param latency estimated latency for communicating with the
57  *             given peer (round-trip)
58  * @param distance in overlay hops, as given by transport plugin
59  */
60 typedef void (*GNUNET_TRANSPORT_ReceiveCallback) (void *cls,
61                                                   const struct
62                                                   GNUNET_PeerIdentity * peer,
63                                                   const struct
64                                                   GNUNET_MessageHeader *
65                                                   message,
66                                                   struct GNUNET_TIME_Relative
67                                                   latency,
68                                                   uint32_t distance);
69
70
71 /**
72  * Opaque handle to the service.
73  */
74 struct GNUNET_TRANSPORT_Handle;
75
76
77 /**
78  * Function called to notify transport users that another
79  * peer connected to us.
80  *
81  * @param cls closure
82  * @param peer the peer that connected
83  * @param latency estimated latency for communicating with the
84  *             given peer (round-trip)
85  * @param distance in overlay hops, as given by transport plugin
86  */
87 typedef void
88   (*GNUNET_TRANSPORT_NotifyConnect) (void *cls,
89                                      const struct GNUNET_PeerIdentity * peer,
90                                      struct GNUNET_TIME_Relative latency,
91                                      uint32_t distance);
92
93 /**
94  * Function called to notify transport users that another
95  * peer disconnected from us.
96  *
97  * @param cls closure
98  * @param peer the peer that disconnected
99  */
100 typedef void
101   (*GNUNET_TRANSPORT_NotifyDisconnect) (void *cls,
102                                         const struct GNUNET_PeerIdentity *
103                                         peer);
104
105
106 /**
107  * Function to call with a human-readable format of an address
108  *
109  * @param cls closure
110  * @param address NULL on error, otherwise 0-terminated printable UTF-8 string
111  */
112 typedef void
113 (*GNUNET_TRANSPORT_AddressLookUpCallback) (void *cls,
114                                            const char *address);
115
116
117 /**
118  * Connect to the transport service.  Note that the connection may
119  * complete (or fail) asynchronously.
120  *
121  * @param sched scheduler to use
122  * @param cfg configuration to use
123  * @param cls closure for the callbacks
124  * @param rec receive function to call
125  * @param nc function to call on connect events
126  * @param nd function to call on disconnect events
127  * @return NULL on error
128  */
129 struct GNUNET_TRANSPORT_Handle *GNUNET_TRANSPORT_connect (struct
130                                                           GNUNET_SCHEDULER_Handle
131                                                           *sched,
132                                                           const struct
133                                                           GNUNET_CONFIGURATION_Handle
134                                                           *cfg, void *cls,
135                                                           GNUNET_TRANSPORT_ReceiveCallback
136                                                           rec,
137                                                           GNUNET_TRANSPORT_NotifyConnect
138                                                           nc,
139                                                           GNUNET_TRANSPORT_NotifyDisconnect
140                                                           nd);
141
142
143 /**
144  * Disconnect from the transport service.
145  *
146  * @param handle handle returned from connect
147  */
148 void GNUNET_TRANSPORT_disconnect (struct GNUNET_TRANSPORT_Handle *handle);
149
150
151 /**
152  * Set the share of incoming/outgoing bandwidth for the given
153  * peer to the specified amount.
154  *
155  * @param handle connection to transport service
156  * @param target who's bandwidth quota is being changed
157  * @param quota_in incoming bandwidth quota
158  * @param quota_out outgoing bandwidth quota
159  * @param timeout how long to wait until signaling failure if
160  *        we can not communicate the quota change
161  * @param cont continuation to call when done, will be called
162  *        either with reason "TIMEOUT" or with reason "PREREQ_DONE"
163  * @param cont_cls closure for continuation
164  */
165 void
166 GNUNET_TRANSPORT_set_quota (struct GNUNET_TRANSPORT_Handle *handle,
167                             const struct GNUNET_PeerIdentity *target,
168                             struct GNUNET_BANDWIDTH_Value32NBO quota_in,
169                             struct GNUNET_BANDWIDTH_Value32NBO quota_out,
170                             struct GNUNET_TIME_Relative timeout,
171                             GNUNET_SCHEDULER_Task cont, void *cont_cls);
172
173
174 /**
175  * Opaque handle for a transmission-ready request.
176  */
177 struct GNUNET_TRANSPORT_TransmitHandle;
178
179
180 /**
181  * Check if we could queue a message of the given size for
182  * transmission.  The transport service will take both its internal
183  * buffers and bandwidth limits imposed by the other peer into
184  * consideration when answering this query.
185  *
186  * @param handle connection to transport service
187  * @param target who should receive the message
188  * @param size how big is the message we want to transmit?
189  * @param priority how important is the message?
190  * @param timeout after how long should we give up (and call
191  *        notify with buf NULL and size 0)?
192  * @param notify function to call when we are ready to
193  *        send such a message
194  * @param notify_cls closure for notify
195  * @return NULL if someone else is already waiting to be notified
196  *         non-NULL if the notify callback was queued (can be used to cancel
197  *         using GNUNET_TRANSPORT_notify_transmit_ready_cancel)
198  */
199 struct GNUNET_TRANSPORT_TransmitHandle
200   *GNUNET_TRANSPORT_notify_transmit_ready (struct GNUNET_TRANSPORT_Handle
201                                            *handle,
202                                            const struct GNUNET_PeerIdentity
203                                            *target, size_t size,
204                                            uint32_t priority,
205                                            struct GNUNET_TIME_Relative
206                                            timeout,
207                                            GNUNET_CONNECTION_TransmitReadyNotify
208                                            notify, void *notify_cls);
209
210
211 /**
212  * Cancel the specified transmission-ready notification.
213  *
214  * @param h handle of the transmission notification request to cancel
215  */
216 void
217 GNUNET_TRANSPORT_notify_transmit_ready_cancel (struct
218                                                GNUNET_TRANSPORT_TransmitHandle
219                                                *h);
220
221
222
223 /**
224  * Function called whenever there is an update to the
225  * HELLO of this peer.
226  *
227  * @param cls closure
228  * @param hello our updated HELLO
229  */
230 typedef void (*GNUNET_TRANSPORT_HelloUpdateCallback)(void *cls,
231                                                      const struct GNUNET_MessageHeader *hello);
232
233
234 /**
235  * Obtain updates on changes to the HELLO message for this peer.
236  *
237  * @param handle connection to transport service
238  * @param rec function to call with the HELLO
239  * @param rec_cls closure for rec
240  */
241 void
242 GNUNET_TRANSPORT_get_hello (struct GNUNET_TRANSPORT_Handle *handle,
243                             GNUNET_TRANSPORT_HelloUpdateCallback rec,
244                             void *rec_cls);
245
246
247 /**
248  * Stop receiving updates about changes to our HELLO message.
249  *
250  * @param handle connection to transport service
251  * @param rec function previously registered to be called with the HELLOs
252  * @param rec_cls closure for rec
253  */
254 void
255 GNUNET_TRANSPORT_get_hello_cancel (struct GNUNET_TRANSPORT_Handle *handle,
256                                    GNUNET_TRANSPORT_HelloUpdateCallback rec,
257                                    void *rec_cls);
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.  If the HELLO
264  * is working, we should add it to PEERINFO.
265  *
266  * @param handle connection to transport service
267  * @param hello the hello message
268  */
269 void
270 GNUNET_TRANSPORT_offer_hello (struct GNUNET_TRANSPORT_Handle *handle,
271                               const struct GNUNET_MessageHeader *hello);
272
273
274 /**
275  * Convert a binary address into a human readable address.
276  *
277  * @param sched scheduler to use
278  * @param cfg configuration to use
279  * @param address address to convert (binary format)
280  * @param addressLen number of bytes in address
281  * @param numeric should (IP) addresses be displayed in numeric form 
282  *                (otherwise do reverse DNS lookup)
283  * @param nameTrans name of the transport to which the address belongs
284  * @param timeout how long is the lookup allowed to take at most
285  * @param aluc function to call with the results
286  * @param aluc_cls closure for aluc
287  */
288 void
289 GNUNET_TRANSPORT_address_lookup (struct GNUNET_SCHEDULER_Handle *sched,
290                                  const struct GNUNET_CONFIGURATION_Handle *cfg,
291                                  const char * address,
292                                  size_t addressLen,
293                                  int numeric,
294                                  const char * nameTrans,
295                                  struct GNUNET_TIME_Relative timeout,
296                                  GNUNET_TRANSPORT_AddressLookUpCallback aluc,
297                                  void *aluc_cls);
298
299
300
301 /**
302  * Handle for blacklisting peers.
303  */
304 struct GNUNET_TRANSPORT_Blacklist;
305
306
307 /**
308  * Function that decides if a connection is acceptable or not.
309  *
310  * @param cls closure
311  * @param pid peer to approve or disapproave
312  * @return GNUNET_OK if the connection is allowed
313  */
314 typedef int (*GNUNET_TRANSPORT_BlacklistCallback)(void *cls,
315                                                   const struct GNUNET_PeerIdentity *pid);
316
317
318 /**
319  * Install a blacklist callback.  The service will be queried for all
320  * existing connections as well as any fresh connections to check if
321  * they are permitted.  If the blacklisting callback is unregistered,
322  * all hosts that were denied in the past will automatically be
323  * whitelisted again.  Cancelling the blacklist handle is also the
324  * only way to re-enable connections from peers that were previously
325  * blacklisted.
326  *
327  * @param sched scheduler to use
328  * @param cfg configuration to use
329  * @param cb callback to invoke to check if connections are allowed
330  * @param cb_cls closure for cb
331  * @return NULL on error, otherwise handle for cancellation
332  */
333 struct GNUNET_TRANSPORT_Blacklist *
334 GNUNET_TRANSPORT_blacklist (struct GNUNET_SCHEDULER_Handle *sched,
335                             const struct GNUNET_CONFIGURATION_Handle *cfg,
336                             GNUNET_TRANSPORT_BlacklistCallback cb,
337                             void *cb_cls);
338
339
340 /**
341  * Abort the blacklist.  Note that this function is the only way for
342  * removing a peer from the blacklist.
343  *
344  * @param br handle of the request that is to be cancelled
345  */
346 void
347 GNUNET_TRANSPORT_blacklist_cancel (struct GNUNET_TRANSPORT_Blacklist *br);
348
349
350
351 #if 0                           /* keep Emacsens' auto-indent happy */
352 {
353 #endif
354 #ifdef __cplusplus
355 }
356 #endif
357
358 /* ifndef GNUNET_TRANSPORT_SERVICE_H */
359 #endif
360 /* end of gnunet_transport_service.h */