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