7a3ccbf7b35690d5758ab110339efb77b8750ee7
[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 2, 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_configuration_lib.h"
39 #include "gnunet_crypto_lib.h"
40 #include "gnunet_connection_lib.h"
41 #include "gnunet_scheduler_lib.h"
42 #include "gnunet_time_lib.h"
43
44 /**
45  * Version number of the transport API.
46  */
47 #define GNUNET_TRANSPORT_VERSION 0x00000000
48
49 /**
50  * Function called by the transport for each received message.
51  *
52  * @param cls closure
53  * @param peer (claimed) identity of the other peer
54  * @param message the message
55  * @param latency estimated latency for communicating with the
56  *             given peer (round-trip)
57  * @param distance in overlay hops, as given by transport plugin
58  */
59 typedef void (*GNUNET_TRANSPORT_ReceiveCallback) (void *cls,
60                                                   const struct
61                                                   GNUNET_PeerIdentity * peer,
62                                                   const struct
63                                                   GNUNET_MessageHeader *
64                                                   message,
65                                                   struct GNUNET_TIME_Relative
66                                                   latency,
67                                                   uint32_t distance);
68
69
70 /**
71  * Opaque handle to the service.
72  */
73 struct GNUNET_TRANSPORT_Handle;
74
75
76 /**
77  * Function called to notify transport users that another
78  * peer connected to us.
79  *
80  * @param cls closure
81  * @param peer the peer that connected
82  * @param latency estimated latency for communicating with the
83  *             given peer (round-trip)
84  * @param distance in overlay hops, as given by transport plugin
85  */
86 typedef void
87   (*GNUNET_TRANSPORT_NotifyConnect) (void *cls,
88                                      const struct GNUNET_PeerIdentity * peer,
89                                      struct GNUNET_TIME_Relative latency,
90                                      uint32_t distance);
91
92 /**
93  * Function called to notify transport users that another
94  * peer disconnected from us.
95  *
96  * @param cls closure
97  * @param peer the peer that disconnected
98  */
99 typedef void
100   (*GNUNET_TRANSPORT_NotifyDisconnect) (void *cls,
101                                         const struct GNUNET_PeerIdentity *
102                                         peer);
103
104
105 /**
106  * Function to call with a human-readable format of an address
107  *
108  * @param cls closure
109  * @param address NULL on error, otherwise 0-terminated printable UTF-8 string
110  */
111 typedef void
112 (*GNUNET_TRANSPORT_AddressLookUpCallback) (void *cls,
113                                            const char *address);
114
115
116 /**
117  * Connect to the transport service.  Note that the connection may
118  * complete (or fail) asynchronously.
119  *
120  * @param sched scheduler to use
121  * @param cfg configuration to use
122  * @param cls closure for the callbacks
123  * @param rec receive function to call
124  * @param nc function to call on connect events
125  * @param nd function to call on disconnect events
126  * @return NULL on error
127  */
128 struct GNUNET_TRANSPORT_Handle *GNUNET_TRANSPORT_connect (struct
129                                                           GNUNET_SCHEDULER_Handle
130                                                           *sched,
131                                                           const struct
132                                                           GNUNET_CONFIGURATION_Handle
133                                                           *cfg, void *cls,
134                                                           GNUNET_TRANSPORT_ReceiveCallback
135                                                           rec,
136                                                           GNUNET_TRANSPORT_NotifyConnect
137                                                           nc,
138                                                           GNUNET_TRANSPORT_NotifyDisconnect
139                                                           nd);
140
141
142 /**
143  * Disconnect from the transport service.
144  *
145  * @param handle handle returned from connect
146  */
147 void GNUNET_TRANSPORT_disconnect (struct GNUNET_TRANSPORT_Handle *handle);
148
149
150 /**
151  * Set the share of incoming/outgoing bandwidth for the given
152  * peer to the specified amount.
153  *
154  * @param handle connection to transport service
155  * @param target who's bandwidth quota is being changed
156  * @param quota_in incoming bandwidth quota in bytes per ms
157  * @param quota_out outgoing bandwidth quota in bytes per ms
158  * @param timeout how long to wait until signaling failure if
159  *        we can not communicate the quota change
160  * @param cont continuation to call when done, will be called
161  *        either with reason "TIMEOUT" or with reason "PREREQ_DONE"
162  * @param cont_cls closure for continuation
163  */
164 void
165 GNUNET_TRANSPORT_set_quota (struct GNUNET_TRANSPORT_Handle *handle,
166                             const struct GNUNET_PeerIdentity *target,
167                             uint32_t quota_in,
168                             uint32_t quota_out,
169                             struct GNUNET_TIME_Relative timeout,
170                             GNUNET_SCHEDULER_Task cont, void *cont_cls);
171
172
173 /**
174  * Opaque handle for a transmission-ready request.
175  */
176 struct GNUNET_TRANSPORT_TransmitHandle;
177
178
179 /**
180  * Check if we could queue a message of the given size for
181  * transmission.  The transport service will take both its
182  * internal buffers and bandwidth limits imposed by the
183  * other peer into consideration when answering this query.
184  *
185  * @param handle connection to transport service
186  * @param target who should receive the message
187  * @param size how big is the message we want to transmit?
188  * @param priority how important is the message?
189  * @param timeout after how long should we give up (and call
190  *        notify with buf NULL and size 0)?
191  * @param notify function to call when we are ready to
192  *        send such a message
193  * @param notify_cls closure for notify
194  * @return NULL if someone else is already waiting to be notified
195  *         non-NULL if the notify callback was queued (can be used to cancel
196  *         using GNUNET_TRANSPORT_notify_transmit_ready_cancel)
197  */
198 struct GNUNET_TRANSPORT_TransmitHandle
199   *GNUNET_TRANSPORT_notify_transmit_ready (struct GNUNET_TRANSPORT_Handle
200                                            *handle,
201                                            const struct GNUNET_PeerIdentity
202                                            *target, size_t size,
203                                            uint32_t priority,
204                                            struct GNUNET_TIME_Relative
205                                            timeout,
206                                            GNUNET_CONNECTION_TransmitReadyNotify
207                                            notify, void *notify_cls);
208
209
210 /**
211  * Cancel the specified transmission-ready notification.
212  *
213  * @param h handle of the transmission notification request to cancel
214  */
215 void
216 GNUNET_TRANSPORT_notify_transmit_ready_cancel (struct
217                                                GNUNET_TRANSPORT_TransmitHandle
218                                                *h);
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 GNUNET_MessageHeader *hello);
231
232
233 /**
234  * Obtain updates on changes to the HELLO message for this peer.
235  *
236  * @param handle connection to transport service
237  * @param rec function to call with the HELLO
238  * @param rec_cls closure for rec
239  */
240 void
241 GNUNET_TRANSPORT_get_hello (struct GNUNET_TRANSPORT_Handle *handle,
242                             GNUNET_TRANSPORT_HelloUpdateCallback rec,
243                             void *rec_cls);
244
245
246 /**
247  * Stop receiving updates about changes to our HELLO message.
248  *
249  * @param handle connection to transport service
250  * @param rec function previously registered to be called with the HELLOs
251  * @param rec_cls closure for rec
252  */
253 void
254 GNUNET_TRANSPORT_get_hello_cancel (struct GNUNET_TRANSPORT_Handle *handle,
255                                    GNUNET_TRANSPORT_HelloUpdateCallback rec,
256                                    void *rec_cls);
257
258
259 /**
260  * Offer the transport service the HELLO of another peer.  Note that
261  * the transport service may just ignore this message if the HELLO is
262  * malformed or useless due to our local configuration.  If the HELLO
263  * is working, we should add it to PEERINFO.
264  *
265  * @param handle connection to transport service
266  * @param hello the hello message
267  */
268 void
269 GNUNET_TRANSPORT_offer_hello (struct GNUNET_TRANSPORT_Handle *handle,
270                               const struct GNUNET_MessageHeader *hello);
271
272
273 /**
274  * Convert a binary address into a human readable address.
275  *
276  * @param sched scheduler to use
277  * @param cfg configuration to use
278  * @param address address to convert (binary format)
279  * @param addressLen number of bytes in address
280  * @param numeric should (IP) addresses be displayed in numeric form 
281  *                (otherwise do reverse DNS lookup)
282  * @param nameTrans name of the transport to which the address belongs
283  * @param timeout how long is the lookup allowed to take at most
284  * @param aluc function to call with the results
285  * @param aluc_cls closure for aluc
286  */
287 void
288 GNUNET_TRANSPORT_address_lookup (struct GNUNET_SCHEDULER_Handle *sched,
289                                  const struct GNUNET_CONFIGURATION_Handle *cfg,
290                                  const char * address,
291                                  size_t addressLen,
292                                  int numeric,
293                                  const char * nameTrans,
294                                  struct GNUNET_TIME_Relative timeout,
295                                  GNUNET_TRANSPORT_AddressLookUpCallback aluc,
296                                  void *aluc_cls);
297
298
299
300 /**
301  * Handle for blacklisting requests.
302  */
303 struct GNUNET_TRANSPORT_BlacklistRequest;
304
305
306 /**
307  * Blacklist a peer for a given period of time.  All connections
308  * (inbound and outbound) to a peer that is blacklisted will be
309  * dropped (as soon as we learn who the connection is for).  A second
310  * call to this function for the same peer overrides previous
311  * blacklisting requests.
312  *
313  * @param sched scheduler to use
314  * @param cfg configuration to use
315  * @param peer identity of peer to blacklist
316  * @param duration how long to blacklist, use GNUNET_TIME_UNIT_ZERO to
317  *        re-enable connections
318  * @param timeout when should this operation (trying to establish the
319  *        blacklisting time out)
320  * @param cont continuation to call once the request has been processed
321  * @param cont_cls closure for cont
322  * @return NULL on error, otherwise handle for cancellation
323  */
324 struct GNUNET_TRANSPORT_BlacklistRequest *
325 GNUNET_TRANSPORT_blacklist (struct GNUNET_SCHEDULER_Handle *sched,
326                             const struct GNUNET_CONFIGURATION_Handle *cfg,
327                             const struct GNUNET_PeerIdentity *peer,
328                             struct GNUNET_TIME_Relative duration,
329                             struct GNUNET_TIME_Relative timeout,
330                             GNUNET_SCHEDULER_Task cont,
331                             void *cont_cls);
332
333
334 /**
335  * Abort transmitting the blacklist request.  Note that this function
336  * is NOT for removing a peer from the blacklist (for that, call 
337  * GNUNET_TRANSPORT_blacklist with a duration of zero).  This function
338  * is only for aborting the transmission of a blacklist request
339  * (i.e. because of shutdown).
340  *
341  * @param br handle of the request that is to be cancelled
342  */
343 void
344 GNUNET_TRANSPORT_blacklist_cancel (struct GNUNET_TRANSPORT_BlacklistRequest * br);
345
346
347 /**
348  * Handle for blacklist notifications.
349  */
350 struct GNUNET_TRANSPORT_BlacklistNotification;
351
352
353 /**
354  * Signature of function called whenever the blacklist status of
355  * a peer changes.  This includes changes to the duration of the
356  * blacklist status as well as the expiration of an existing
357  * blacklist status.
358  *
359  * @param cls closure
360  * @param peer identity of peer with the change
361  * @param until GNUNET_TIME_UNIT_ZERO_ABS if the peer is no
362  *              longer blacklisted, otherwise the time at
363  *              which the current blacklisting will expire
364  */
365 typedef void (*GNUNET_TRANSPORT_BlacklistCallback)(void *cls,
366                                                    const struct GNUNET_PeerIdentity *peer,
367                                                    struct GNUNET_TIME_Absolute until);
368
369
370 /**
371  * Call a function whenever a peer's blacklisting status changes.
372  *
373  * @param sched scheduler to use
374  * @param cfg configuration to use
375  * @param bc function to call on status changes
376  * @param bc_cls closure for bc
377  * @return NULL on error, otherwise handle for cancellation
378  */
379 struct GNUNET_TRANSPORT_BlacklistNotification *
380 GNUNET_TRANSPORT_blacklist_notify (struct GNUNET_SCHEDULER_Handle *sched,
381                                    const struct GNUNET_CONFIGURATION_Handle *cfg,
382                                    GNUNET_TRANSPORT_BlacklistCallback bc,
383                                    void *bc_cls);
384
385
386 /**
387  * Stop calling the notification callback associated with
388  * the given blacklist notification.
389  *
390  * @param bn handle of the request that is to be cancelled
391  */
392 void
393 GNUNET_TRANSPORT_blacklist_notify_cancel (struct GNUNET_TRANSPORT_BlacklistNotification * bn);
394
395
396
397 #if 0                           /* keep Emacsens' auto-indent happy */
398 {
399 #endif
400 #ifdef __cplusplus
401 }
402 #endif
403
404 /* ifndef GNUNET_TRANSPORT_SERVICE_H */
405 #endif
406 /* end of gnunet_transport_service.h */