eliminate last calls to GNUNET_CORE_peer_request_connect
[oweals/gnunet.git] / src / include / gnunet_core_service.h
1 /*
2      This file is part of GNUnet.
3      (C) 2009, 2010 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_core_service.h
23  * @brief core service; this is the main API for encrypted P2P
24  *        communications
25  * @author Christian Grothoff
26  */
27
28 #ifndef GNUNET_CORE_SERVICE_H
29 #define GNUNET_CORE_SERVICE_H
30
31 #ifdef __cplusplus
32 extern "C"
33 {
34 #if 0                           /* keep Emacsens' auto-indent happy */
35 }
36 #endif
37 #endif
38
39 #include "gnunet_util_lib.h"
40 #include "gnunet_transport_service.h"
41
42 /**
43  * Version number of GNUnet-core API.
44  */
45 #define GNUNET_CORE_VERSION 0x00000000
46
47
48 /**
49  * Opaque handle to the service.
50  */
51 struct GNUNET_CORE_Handle;
52
53
54 /**
55  * Method called whenever a given peer connects.
56  *
57  * @param cls closure
58  * @param peer peer identity this notification is about
59  * @param atsi performance data for the connection
60  */
61 typedef void (*GNUNET_CORE_ConnectEventHandler) (void *cls,
62                                                  const struct
63                                                  GNUNET_PeerIdentity * peer,
64                                                  const struct
65                                                  GNUNET_TRANSPORT_ATS_Information
66                                                  * atsi);
67
68
69 /**
70  * Method called whenever a given peer has a status change.
71  *
72  * @param cls closure
73  * @param peer peer identity this notification is about
74  * @param timeout absolute time when this peer will time out
75  *        unless we see some further activity from it
76  * @param bandwidth_in available amount of inbound bandwidth
77  * @param bandwidth_out available amount of outbound bandwidth
78  * @param atsi performance data for the connection
79  */
80 typedef void (*GNUNET_CORE_PeerStatusEventHandler) (void *cls,
81                                                     const struct
82                                                     GNUNET_PeerIdentity * peer,
83                                                     struct
84                                                     GNUNET_BANDWIDTH_Value32NBO
85                                                     bandwidth_in,
86                                                     struct
87                                                     GNUNET_BANDWIDTH_Value32NBO
88                                                     bandwidth_out,
89                                                     struct GNUNET_TIME_Absolute
90                                                     timeout,
91                                                     const struct
92                                                     GNUNET_TRANSPORT_ATS_Information
93                                                     * atsi);
94
95
96 /**
97  * Method called whenever a peer disconnects.
98  *
99  * @param cls closure
100  * @param peer peer identity this notification is about
101  */
102 typedef void (*GNUNET_CORE_DisconnectEventHandler) (void *cls,
103                                                     const struct
104                                                     GNUNET_PeerIdentity * peer);
105
106
107 /**
108  * Functions with this signature are called whenever a message is
109  * received or transmitted.
110  *
111  * @param cls closure (set from GNUNET_CORE_connect)
112  * @param peer the other peer involved (sender or receiver, NULL
113  *        for loopback messages where we are both sender and receiver)
114  * @param message the actual message
115  * @param atsi performance data for the connection
116  * @return GNUNET_OK to keep the connection open,
117  *         GNUNET_SYSERR to close it (signal serious error)
118  */
119 typedef int (*GNUNET_CORE_MessageCallback) (void *cls,
120                                             const struct GNUNET_PeerIdentity *
121                                             other,
122                                             const struct GNUNET_MessageHeader *
123                                             message,
124                                             const struct
125                                             GNUNET_TRANSPORT_ATS_Information *
126                                             atsi);
127
128
129 /**
130  * Message handler.  Each struct specifies how to handle on particular
131  * type of message received.
132  */
133 struct GNUNET_CORE_MessageHandler
134 {
135   /**
136    * Function to call for messages of "type".
137    */
138   GNUNET_CORE_MessageCallback callback;
139
140   /**
141    * Type of the message this handler covers.
142    */
143   uint16_t type;
144
145   /**
146    * Expected size of messages of this type.  Use 0 for variable-size.
147    * If non-zero, messages of the given type will be discarded if they
148    * do not have the right size.
149    */
150   uint16_t expected_size;
151
152 };
153
154
155 /**
156  * Function called after GNUNET_CORE_connect has succeeded
157  * (or failed for good).  Note that the private key of the
158  * peer is intentionally not exposed here; if you need it,
159  * your process should try to read the private key file
160  * directly (which should work if you are authorized...).
161  *
162  * @param cls closure
163  * @param server handle to the server, NULL if we failed
164  * @param my_identity ID of this peer, NULL if we failed
165  * @param publicKey public key of this peer, NULL if we failed
166  */
167 typedef void (*GNUNET_CORE_StartupCallback) (void *cls,
168                                              struct GNUNET_CORE_Handle * server,
169                                              const struct GNUNET_PeerIdentity *
170                                              my_identity,
171                                              const struct
172                                              GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded
173                                              * publicKey);
174
175
176 /**
177  * Connect to the core service.  Note that the connection may complete
178  * (or fail) asynchronously.  This function primarily causes the given
179  * callback notification functions to be invoked whenever the
180  * specified event happens.  The maximum number of queued
181  * notifications (queue length) is per client but the queue is shared
182  * across all types of notifications.  So a slow client that registers
183  * for 'outbound_notify' also risks missing 'inbound_notify' messages.
184  * Certain events (such as connect/disconnect notifications) are not
185  * subject to queue size limitations.
186  *
187  * @param cfg configuration to use
188  * @param queue_size size of the per-peer message queue
189  * @param cls closure for the various callbacks that follow (including handlers in the handlers array)
190  * @param init callback to call on timeout or once we have successfully
191  *        connected to the core service; note that timeout is only meaningful if init is not NULL
192  * @param connects function to call on peer connect, can be NULL
193  * @param disconnects function to call on peer disconnect / timeout, can be NULL
194  * @param status_events function to call on peer status changes, can be NULL
195  * @param inbound_notify function to call for all inbound messages, can be NULL
196  *                note that the core is allowed to drop notifications about inbound
197  *                messages if the client does not process them fast enough (for this
198  *                notification type, a bounded queue is used)
199  * @param inbound_hdr_only set to GNUNET_YES if inbound_notify will only read the
200  *                GNUNET_MessageHeader and hence we do not need to give it the full message;
201  *                can be used to improve efficiency, ignored if inbound_notify is NULL
202  *                note that the core is allowed to drop notifications about inbound
203  *                messages if the client does not process them fast enough (for this
204  *                notification type, a bounded queue is used)
205  * @param outbound_notify function to call for all outbound messages, can be NULL;
206  *                note that the core is allowed to drop notifications about outbound
207  *                messages if the client does not process them fast enough (for this
208  *                notification type, a bounded queue is used)
209  * @param outbound_hdr_only set to GNUNET_YES if outbound_notify will only read the
210  *                GNUNET_MessageHeader and hence we do not need to give it the full message
211  *                can be used to improve efficiency, ignored if outbound_notify is NULL
212  *                note that the core is allowed to drop notifications about outbound
213  *                messages if the client does not process them fast enough (for this
214  *                notification type, a bounded queue is used)
215  * @param handlers callbacks for messages we care about, NULL-terminated
216  *                note that the core is allowed to drop notifications about inbound
217  *                messages if the client does not process them fast enough (for this
218  *                notification type, a bounded queue is used)
219  * @return handle to the core service (only useful for disconnect until 'init' is called),
220  *           NULL on error (in this case, init is never called)
221  */
222 struct GNUNET_CORE_Handle *
223 GNUNET_CORE_connect (const struct GNUNET_CONFIGURATION_Handle *cfg,
224                      unsigned int queue_size, void *cls,
225                      GNUNET_CORE_StartupCallback init,
226                      GNUNET_CORE_ConnectEventHandler connects,
227                      GNUNET_CORE_DisconnectEventHandler disconnects,
228                      GNUNET_CORE_PeerStatusEventHandler status_events,
229                      GNUNET_CORE_MessageCallback inbound_notify,
230                      int inbound_hdr_only,
231                      GNUNET_CORE_MessageCallback outbound_notify,
232                      int outbound_hdr_only,
233                      const struct GNUNET_CORE_MessageHandler *handlers);
234
235
236 /**
237  * Disconnect from the core service.    This function can only
238  * be called *after* all pending 'GNUNET_CORE_notify_transmit_ready'
239  * requests have been explicitly cancelled.
240  *
241  * @param handle connection to core to disconnect
242  */
243 void
244 GNUNET_CORE_disconnect (struct GNUNET_CORE_Handle *handle);
245
246
247 /**
248  * Function called with perference change information about the given peer.
249  *
250  * @param cls closure
251  * @param peer identifies the peer
252  * @param bandwidth_out available amount of outbound bandwidth
253  * @param amount set to the amount that was actually reserved or unreserved;
254  *               either the full requested amount or zero (no partial reservations)
255  * @param res_delay if the reservation could not be satisfied (amount was 0), how
256  *        long should the client wait until re-trying?
257  * @param preference current traffic preference for the given peer
258  */
259 typedef void (*GNUNET_CORE_PeerConfigurationInfoCallback) (void *cls,
260                                                            const struct
261                                                            GNUNET_PeerIdentity *
262                                                            peer,
263                                                            struct
264                                                            GNUNET_BANDWIDTH_Value32NBO
265                                                            bandwidth_out,
266                                                            int32_t amount,
267                                                            struct
268                                                            GNUNET_TIME_Relative
269                                                            res_delay,
270                                                            uint64_t preference);
271
272
273
274 /**
275  * Context that can be used to cancel a peer information request.
276  */
277 struct GNUNET_CORE_InformationRequestContext;
278
279
280 /**
281  * Obtain statistics and/or change preferences for the given peer.
282  * You can only have one such pending request per peer.
283  *
284  * @param h core handle
285  * @param peer identifies the peer
286  * @param timeout after how long should we give up (and call "info" with NULL
287  *                for "peer" to signal an error)?
288  * @param bw_out set to the current bandwidth limit (sending) for this peer,
289  *                caller should set "bpm_out" to "GNUNET_BANDWIDTH_VALUE_MAX" to avoid changing
290  *                the current value; otherwise "bw_out" will be lowered to
291  *                the specified value; passing a pointer to "0" can be used to force
292  *                us to disconnect from the peer; "bw_out" might not increase
293  *                as specified since the upper bound is generally
294  *                determined by the other peer!
295  * @param amount reserve N bytes for receiving, negative
296  *                amounts can be used to undo a (recent) reservation;
297  * @param preference increase incoming traffic share preference by this amount;
298  *                in the absence of "amount" reservations, we use this
299  *                preference value to assign proportional bandwidth shares
300  *                to all connected peers
301  * @param info function to call with the resulting configuration information
302  * @param info_cls closure for info
303  * @return NULL on error
304  */
305 struct GNUNET_CORE_InformationRequestContext *
306 GNUNET_CORE_peer_change_preference (struct GNUNET_CORE_Handle *h,
307                                     const struct GNUNET_PeerIdentity *peer,
308                                     struct GNUNET_TIME_Relative timeout,
309                                     struct GNUNET_BANDWIDTH_Value32NBO bw_out,
310                                     int32_t amount, uint64_t preference,
311                                     GNUNET_CORE_PeerConfigurationInfoCallback
312                                     info, void *info_cls);
313
314
315 /**
316  * Cancel request for getting information about a peer.
317  * Note that an eventual change in preference, trust or bandwidth
318  * assignment MAY have already been committed at the time,
319  * so cancelling a request is NOT sure to undo the original
320  * request.  The original request may or may not still commit.
321  * The only thing cancellation ensures is that the callback
322  * from the original request will no longer be called.
323  *
324  * @param irc context returned by the original GNUNET_CORE_peer_get_info call
325  */
326 void
327 GNUNET_CORE_peer_change_preference_cancel (struct
328                                            GNUNET_CORE_InformationRequestContext
329                                            *irc);
330
331
332 /**
333  * Iterate over all connected peers.
334  *
335  * @param cfg configuration handle
336  * @param peer_cb function to call with the peer information
337  * @param cb_cls closure for peer_cb
338  * @return GNUNET_OK on success, GNUNET_SYSERR on errors
339  */
340 int
341 GNUNET_CORE_iterate_peers (const struct GNUNET_CONFIGURATION_Handle *cfg,
342                            GNUNET_CORE_ConnectEventHandler peer_cb,
343                            void *cb_cls);
344
345 /**
346  * Iterate over all currently connected peers.
347  * Calls peer_cb with each connected peer, and then
348  * once with NULL to indicate that all peers have
349  * been handled.
350  *
351  * @param cfg configuration to use
352  * @param peer the specific peer to check for
353  * @param peer_cb function to call with the peer information
354  * @param cb_cls closure for peer_cb
355  *
356  * @return GNUNET_OK if iterating, GNUNET_SYSERR on error
357  */
358 int
359 GNUNET_CORE_is_peer_connected (const struct GNUNET_CONFIGURATION_Handle *cfg,
360                                struct GNUNET_PeerIdentity *peer,
361                                GNUNET_CORE_ConnectEventHandler peer_cb,
362                                void *cb_cls);
363
364
365 /**
366  * Handle for a transmission request.
367  */
368 struct GNUNET_CORE_TransmitHandle;
369
370
371 /**
372  * Ask the core to call "notify" once it is ready to transmit the
373  * given number of bytes to the specified "target".   Must only be
374  * called after a connection to the respective peer has been
375  * established (and the client has been informed about this).
376  *
377  *
378  * @param handle connection to core service
379  * @param cork is corking allowed for this transmission?
380  * @param priority how important is the message?
381  * @param maxdelay how long can the message wait?
382  * @param target who should receive the message,
383  *        use NULL for this peer (loopback)
384  * @param notify_size how many bytes of buffer space does notify want?
385  * @param notify function to call when buffer space is available;
386  *        will be called with NULL on timeout or if the overall queue
387  *        for this peer is larger than queue_size and this is currently
388  *        the message with the lowest priority; will also be called
389  *        with 'NULL' buf if the peer disconnects; since the disconnect
390  *        signal will be emmitted even later, clients MUST cancel
391  *        all pending transmission requests DURING the disconnect
392  *        handler (unless they ensure that 'notify' never calls
393  *        'GNUNET_CORE_notify_transmit_ready').
394  * @param notify_cls closure for notify
395  * @return non-NULL if the notify callback was queued,
396  *         NULL if we can not even queue the request (insufficient
397  *         memory); if NULL is returned, "notify" will NOT be called.
398  */
399 struct GNUNET_CORE_TransmitHandle *
400 GNUNET_CORE_notify_transmit_ready (struct GNUNET_CORE_Handle *handle, int cork,
401                                    uint32_t priority,
402                                    struct GNUNET_TIME_Relative maxdelay,
403                                    const struct GNUNET_PeerIdentity *target,
404                                    size_t notify_size,
405                                    GNUNET_CONNECTION_TransmitReadyNotify notify,
406                                    void *notify_cls);
407
408
409 /**
410  * Cancel the specified transmission-ready notification.
411  *
412  * @param th handle that was returned by "notify_transmit_ready".
413  */
414 void
415 GNUNET_CORE_notify_transmit_ready_cancel (struct GNUNET_CORE_TransmitHandle
416                                           *th);
417
418
419 #if 0                           /* keep Emacsens' auto-indent happy */
420 {
421 #endif
422 #ifdef __cplusplus
423 }
424 #endif
425
426 /* ifndef GNUNET_CORE_SERVICE_H */
427 #endif
428 /* end of gnunet_core_service.h */