2ee9fb71810cf3054aba1590f35a0d4c2561923f
[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 self our own identity (API should check that it matches
124  *             the identity found by transport), or NULL (no check)
125  * @param cls closure for the callbacks
126  * @param rec receive function to call
127  * @param nc function to call on connect events
128  * @param nd function to call on disconnect events
129  * @return NULL on error
130  */
131 struct GNUNET_TRANSPORT_Handle *GNUNET_TRANSPORT_connect (struct
132                                                           GNUNET_SCHEDULER_Handle
133                                                           *sched,
134                                                           const struct
135                                                           GNUNET_CONFIGURATION_Handle
136                                                           *cfg, 
137                                                           const struct GNUNET_PeerIdentity *self,
138                                                           void *cls,
139                                                           GNUNET_TRANSPORT_ReceiveCallback
140                                                           rec,
141                                                           GNUNET_TRANSPORT_NotifyConnect
142                                                           nc,
143                                                           GNUNET_TRANSPORT_NotifyDisconnect
144                                                           nd);
145
146
147 /**
148  * Disconnect from the transport service.
149  *
150  * @param handle handle returned from connect
151  */
152 void GNUNET_TRANSPORT_disconnect (struct GNUNET_TRANSPORT_Handle *handle);
153
154
155 /**
156  * Set the share of incoming/outgoing bandwidth for the given
157  * peer to the specified amount.
158  *
159  * @param handle connection to transport service
160  * @param target who's bandwidth quota is being changed
161  * @param quota_in incoming bandwidth quota
162  * @param quota_out outgoing bandwidth quota
163  * @param timeout how long to wait until signaling failure if
164  *        we can not communicate the quota change
165  * @param cont continuation to call when done, will be called
166  *        either with reason "TIMEOUT" or with reason "PREREQ_DONE"
167  * @param cont_cls closure for continuation
168  */
169 void
170 GNUNET_TRANSPORT_set_quota (struct GNUNET_TRANSPORT_Handle *handle,
171                             const struct GNUNET_PeerIdentity *target,
172                             struct GNUNET_BANDWIDTH_Value32NBO quota_in,
173                             struct GNUNET_BANDWIDTH_Value32NBO quota_out,
174                             struct GNUNET_TIME_Relative timeout,
175                             GNUNET_SCHEDULER_Task cont, void *cont_cls);
176
177
178 /**
179  * Opaque handle for a transmission-ready request.
180  */
181 struct GNUNET_TRANSPORT_TransmitHandle;
182
183
184 /**
185  * Check if we could queue a message of the given size for
186  * transmission.  The transport service will take both its internal
187  * buffers and bandwidth limits imposed by the other peer into
188  * consideration when answering this query.
189  *
190  * @param handle connection to transport service
191  * @param target who should receive the message
192  * @param size how big is the message we want to transmit?
193  * @param priority how important is the message?
194  * @param timeout after how long should we give up (and call
195  *        notify with buf NULL and size 0)?
196  * @param notify function to call when we are ready to
197  *        send such a message
198  * @param notify_cls closure for notify
199  * @return NULL if someone else is already waiting to be notified
200  *         non-NULL if the notify callback was queued (can be used to cancel
201  *         using GNUNET_TRANSPORT_notify_transmit_ready_cancel)
202  */
203 struct GNUNET_TRANSPORT_TransmitHandle
204   *GNUNET_TRANSPORT_notify_transmit_ready (struct GNUNET_TRANSPORT_Handle
205                                            *handle,
206                                            const struct GNUNET_PeerIdentity
207                                            *target, size_t size,
208                                            uint32_t priority,
209                                            struct GNUNET_TIME_Relative
210                                            timeout,
211                                            GNUNET_CONNECTION_TransmitReadyNotify
212                                            notify, void *notify_cls);
213
214
215 /**
216  * Cancel the specified transmission-ready notification.
217  *
218  * @param h handle of the transmission notification request to cancel
219  */
220 void
221 GNUNET_TRANSPORT_notify_transmit_ready_cancel (struct
222                                                GNUNET_TRANSPORT_TransmitHandle
223                                                *h);
224
225
226
227 /**
228  * Function called whenever there is an update to the
229  * HELLO of this peer.
230  *
231  * @param cls closure
232  * @param hello our updated HELLO
233  */
234 typedef void (*GNUNET_TRANSPORT_HelloUpdateCallback)(void *cls,
235                                                      const struct GNUNET_MessageHeader *hello);
236
237
238 /**
239  * Obtain updates on changes to the HELLO message for this peer.
240  *
241  * @param handle connection to transport service
242  * @param rec function to call with the HELLO
243  * @param rec_cls closure for rec
244  */
245 void
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 handle connection to transport service
255  * @param rec function previously registered to be called with the HELLOs
256  * @param rec_cls closure for rec
257  */
258 void
259 GNUNET_TRANSPORT_get_hello_cancel (struct GNUNET_TRANSPORT_Handle *handle,
260                                    GNUNET_TRANSPORT_HelloUpdateCallback rec,
261                                    void *rec_cls);
262
263
264 /**
265  * Offer the transport service the HELLO of another peer.  Note that
266  * the transport service may just ignore this message if the HELLO is
267  * malformed or useless due to our local configuration.  If the HELLO
268  * is working, we should add it to PEERINFO.
269  *
270  * @param handle connection to transport service
271  * @param hello the hello message
272  */
273 void
274 GNUNET_TRANSPORT_offer_hello (struct GNUNET_TRANSPORT_Handle *handle,
275                               const struct GNUNET_MessageHeader *hello);
276
277
278 /**
279  * Convert a binary address into a human readable address.
280  *
281  * @param sched scheduler to use
282  * @param cfg configuration to use
283  * @param address address to convert (binary format)
284  * @param addressLen number of bytes in address
285  * @param numeric should (IP) addresses be displayed in numeric form 
286  *                (otherwise do reverse DNS lookup)
287  * @param nameTrans name of the transport to which the address belongs
288  * @param timeout how long is the lookup allowed to take at most
289  * @param aluc function to call with the results
290  * @param aluc_cls closure for aluc
291  */
292 void
293 GNUNET_TRANSPORT_address_lookup (struct GNUNET_SCHEDULER_Handle *sched,
294                                  const struct GNUNET_CONFIGURATION_Handle *cfg,
295                                  const char * address,
296                                  size_t addressLen,
297                                  int numeric,
298                                  const char * nameTrans,
299                                  struct GNUNET_TIME_Relative timeout,
300                                  GNUNET_TRANSPORT_AddressLookUpCallback aluc,
301                                  void *aluc_cls);
302
303
304
305 /**
306  * Handle for blacklisting peers.
307  */
308 struct GNUNET_TRANSPORT_Blacklist;
309
310
311 /**
312  * Function that decides if a connection is acceptable or not.
313  *
314  * @param cls closure
315  * @param pid peer to approve or disapproave
316  * @return GNUNET_OK if the connection is allowed
317  */
318 typedef int (*GNUNET_TRANSPORT_BlacklistCallback)(void *cls,
319                                                   const struct GNUNET_PeerIdentity *pid);
320
321
322 /**
323  * Install a blacklist callback.  The service will be queried for all
324  * existing connections as well as any fresh connections to check if
325  * they are permitted.  If the blacklisting callback is unregistered,
326  * all hosts that were denied in the past will automatically be
327  * whitelisted again.  Cancelling the blacklist handle is also the
328  * only way to re-enable connections from peers that were previously
329  * blacklisted.
330  *
331  * @param sched scheduler to use
332  * @param cfg configuration to use
333  * @param cb callback to invoke to check if connections are allowed
334  * @param cb_cls closure for cb
335  * @return NULL on error, otherwise handle for cancellation
336  */
337 struct GNUNET_TRANSPORT_Blacklist *
338 GNUNET_TRANSPORT_blacklist (struct GNUNET_SCHEDULER_Handle *sched,
339                             const struct GNUNET_CONFIGURATION_Handle *cfg,
340                             GNUNET_TRANSPORT_BlacklistCallback cb,
341                             void *cb_cls);
342
343
344 /**
345  * Abort the blacklist.  Note that this function is the only way for
346  * removing a peer from the blacklist.
347  *
348  * @param br handle of the request that is to be cancelled
349  */
350 void
351 GNUNET_TRANSPORT_blacklist_cancel (struct GNUNET_TRANSPORT_Blacklist *br);
352
353
354
355 #if 0                           /* keep Emacsens' auto-indent happy */
356 {
357 #endif
358 #ifdef __cplusplus
359 }
360 #endif
361
362 /* ifndef GNUNET_TRANSPORT_SERVICE_H */
363 #endif
364 /* end of gnunet_transport_service.h */