1bf82030d8d6fa882b9a01f50bf4c7a4cf9162b2
[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
120     (*GNUNET_CORE_MessageCallback) (void *cls,
121                                     const struct GNUNET_PeerIdentity * other,
122                                     const struct GNUNET_MessageHeader * message,
123                                     const struct
124                                     GNUNET_TRANSPORT_ATS_Information * atsi);
125
126
127 /**
128  * Message handler.  Each struct specifies how to handle on particular
129  * type of message received.
130  */
131 struct GNUNET_CORE_MessageHandler
132 {
133   /**
134    * Function to call for messages of "type".
135    */
136   GNUNET_CORE_MessageCallback callback;
137
138   /**
139    * Type of the message this handler covers.
140    */
141   uint16_t type;
142
143   /**
144    * Expected size of messages of this type.  Use 0 for variable-size.
145    * If non-zero, messages of the given type will be discarded if they
146    * do not have the right size.
147    */
148   uint16_t expected_size;
149
150 };
151
152
153 /**
154  * Function called after GNUNET_CORE_connect has succeeded
155  * (or failed for good).  Note that the private key of the
156  * peer is intentionally not exposed here; if you need it,
157  * your process should try to read the private key file
158  * directly (which should work if you are authorized...).
159  *
160  * @param cls closure
161  * @param server handle to the server, NULL if we failed
162  * @param my_identity ID of this peer, NULL if we failed
163  * @param publicKey public key of this peer, NULL if we failed
164  */
165 typedef void
166     (*GNUNET_CORE_StartupCallback) (void *cls,
167                                     struct GNUNET_CORE_Handle * server,
168                                     const struct GNUNET_PeerIdentity *
169                                     my_identity,
170                                     const struct
171                                     GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *
172                                     publicKey);
173
174
175 /**
176  * Connect to the core service.  Note that the connection may complete
177  * (or fail) asynchronously.  This function primarily causes the given
178  * callback notification functions to be invoked whenever the
179  * specified event happens.  The maximum number of queued
180  * notifications (queue length) is per client but the queue is shared
181  * across all types of notifications.  So a slow client that registers
182  * for 'outbound_notify' also risks missing 'inbound_notify' messages.
183  * Certain events (such as connect/disconnect notifications) are not
184  * subject to queue size limitations.
185  *
186  * @param cfg configuration to use
187  * @param queue_size size of the per-peer message queue
188  * @param cls closure for the various callbacks that follow (including handlers in the handlers array)
189  * @param init callback to call on timeout or once we have successfully
190  *        connected to the core service; note that timeout is only meaningful if init is not NULL
191  * @param connects function to call on peer connect, can be NULL
192  * @param disconnects function to call on peer disconnect / timeout, can be NULL
193  * @param status_events function to call on peer status changes, can be NULL
194  * @param inbound_notify function to call for all inbound messages, can be NULL
195  *                note that the core is allowed to drop notifications about inbound
196  *                messages if the client does not process them fast enough (for this
197  *                notification type, a bounded queue is used)
198  * @param inbound_hdr_only set to GNUNET_YES if inbound_notify will only read the
199  *                GNUNET_MessageHeader and hence we do not need to give it the full message;
200  *                can be used to improve efficiency, ignored if inbound_notify is NULL
201  *                note that the core is allowed to drop notifications about inbound
202  *                messages if the client does not process them fast enough (for this
203  *                notification type, a bounded queue is used) 
204  * @param outbound_notify function to call for all outbound messages, can be NULL;
205  *                note that the core is allowed to drop notifications about outbound
206  *                messages if the client does not process them fast enough (for this
207  *                notification type, a bounded queue is used)
208  * @param outbound_hdr_only set to GNUNET_YES if outbound_notify will only read the
209  *                GNUNET_MessageHeader and hence we do not need to give it the full message
210  *                can be used to improve efficiency, ignored if outbound_notify is NULL
211  *                note that the core is allowed to drop notifications about outbound
212  *                messages if the client does not process them fast enough (for this
213  *                notification type, a bounded queue is used)
214  * @param handlers callbacks for messages we care about, NULL-terminated
215  *                note that the core is allowed to drop notifications about inbound
216  *                messages if the client does not process them fast enough (for this
217  *                notification type, a bounded queue is used)
218  * @return handle to the core service (only useful for disconnect until 'init' is called),
219  *           NULL on error (in this case, init is never called)
220  */
221 struct GNUNET_CORE_Handle *GNUNET_CORE_connect (const struct
222                                                 GNUNET_CONFIGURATION_Handle
223                                                 *cfg, unsigned int queue_size,
224                                                 void *cls,
225                                                 GNUNET_CORE_StartupCallback
226                                                 init,
227                                                 GNUNET_CORE_ConnectEventHandler
228                                                 connects,
229                                                 GNUNET_CORE_DisconnectEventHandler
230                                                 disconnects,
231                                                 GNUNET_CORE_PeerStatusEventHandler
232                                                 status_events,
233                                                 GNUNET_CORE_MessageCallback
234                                                 inbound_notify,
235                                                 int inbound_hdr_only,
236                                                 GNUNET_CORE_MessageCallback
237                                                 outbound_notify,
238                                                 int outbound_hdr_only,
239                                                 const struct
240                                                 GNUNET_CORE_MessageHandler
241                                                 *handlers);
242
243
244 /**
245  * Disconnect from the core service.    This function can only 
246  * be called *after* all pending 'GNUNET_CORE_notify_transmit_ready'
247  * requests have been explicitly cancelled.
248  *
249  * @param handle connection to core to disconnect
250  */
251 void GNUNET_CORE_disconnect (struct GNUNET_CORE_Handle *handle);
252
253
254 /**
255  * Handle for a request to the core to connect or disconnect
256  * from a particular peer.  Can be used to cancel the request
257  * (before the 'cont'inuation is called).
258  */
259 struct GNUNET_CORE_PeerRequestHandle;
260
261
262 /**
263  * Type of function called upon completion.
264  *
265  * @param cls closure
266  * @param success GNUNET_OK on success (which for request_connect
267  *        ONLY means that we transmitted the connect request to CORE,
268  *        it does not mean that we are actually now connected!);
269  *        GNUNET_NO on timeout,
270  *        GNUNET_SYSERR if core was shut down
271  */
272 typedef void (*GNUNET_CORE_ControlContinuation) (void *cls, int success);
273
274
275 /**
276  * Request that the core should try to connect to a particular peer.
277  * Once the request has been transmitted to the core, the continuation
278  * function will be called.  Note that this does NOT mean that a
279  * connection was successfully established -- it only means that the
280  * core will now try.  Successful establishment of the connection
281  * will be signalled to the 'connects' callback argument of
282  * 'GNUNET_CORE_connect' only.  If the core service does not respond
283  * to our connection attempt within the given time frame, 'cont' will
284  * be called with the TIMEOUT reason code.
285  *
286  * @param h core handle
287  * @param peer who should we connect to
288  * @param cont function to call once the request has been completed (or timed out)
289  * @param cont_cls closure for cont
290  * @return NULL on error (cont will not be called), otherwise handle for cancellation
291  */
292 struct GNUNET_CORE_PeerRequestHandle *GNUNET_CORE_peer_request_connect (struct
293                                                                         GNUNET_CORE_Handle
294                                                                         *h,
295                                                                         const
296                                                                         struct
297                                                                         GNUNET_PeerIdentity
298                                                                         *peer,
299                                                                         GNUNET_CORE_ControlContinuation
300                                                                         cont,
301                                                                         void
302                                                                         *cont_cls);
303
304
305 /**
306  * Cancel a pending request to connect to a particular peer.  Must not
307  * be called after the 'cont' function was invoked.
308  *
309  * @param req request handle that was returned for the original request
310  */
311 void
312 GNUNET_CORE_peer_request_connect_cancel (struct GNUNET_CORE_PeerRequestHandle
313                                          *req);
314
315
316 /**
317  * Function called with perference change information about the given peer.
318  *
319  * @param cls closure
320  * @param peer identifies the peer
321  * @param bandwidth_out available amount of outbound bandwidth
322  * @param amount set to the amount that was actually reserved or unreserved;
323  *               either the full requested amount or zero (no partial reservations)
324  * @param res_delay if the reservation could not be satisfied (amount was 0), how
325  *        long should the client wait until re-trying?
326  * @param preference current traffic preference for the given peer
327  */
328 typedef void
329     (*GNUNET_CORE_PeerConfigurationInfoCallback) (void *cls,
330                                                   const struct
331                                                   GNUNET_PeerIdentity * peer,
332                                                   struct
333                                                   GNUNET_BANDWIDTH_Value32NBO
334                                                   bandwidth_out, int32_t amount,
335                                                   struct GNUNET_TIME_Relative
336                                                   res_delay,
337                                                   uint64_t preference);
338
339
340
341 /**
342  * Context that can be used to cancel a peer information request.
343  */
344 struct GNUNET_CORE_InformationRequestContext;
345
346
347 /**
348  * Obtain statistics and/or change preferences for the given peer.
349  * You can only have one such pending request per peer.
350  *
351  * @param h core handle
352  * @param peer identifies the peer
353  * @param timeout after how long should we give up (and call "info" with NULL
354  *                for "peer" to signal an error)?
355  * @param bw_out set to the current bandwidth limit (sending) for this peer,
356  *                caller should set "bpm_out" to "GNUNET_BANDWIDTH_VALUE_MAX" to avoid changing
357  *                the current value; otherwise "bw_out" will be lowered to
358  *                the specified value; passing a pointer to "0" can be used to force
359  *                us to disconnect from the peer; "bw_out" might not increase
360  *                as specified since the upper bound is generally
361  *                determined by the other peer!
362  * @param amount reserve N bytes for receiving, negative
363  *                amounts can be used to undo a (recent) reservation;
364  * @param preference increase incoming traffic share preference by this amount;
365  *                in the absence of "amount" reservations, we use this
366  *                preference value to assign proportional bandwidth shares
367  *                to all connected peers
368  * @param info function to call with the resulting configuration information
369  * @param info_cls closure for info
370  * @return NULL on error
371  */
372 struct GNUNET_CORE_InformationRequestContext
373     *GNUNET_CORE_peer_change_preference (struct GNUNET_CORE_Handle *h,
374                                          const struct GNUNET_PeerIdentity *peer,
375                                          struct GNUNET_TIME_Relative timeout,
376                                          struct GNUNET_BANDWIDTH_Value32NBO
377                                          bw_out, int32_t amount,
378                                          uint64_t preference,
379                                          GNUNET_CORE_PeerConfigurationInfoCallback
380                                          info, void *info_cls);
381
382
383 /**
384  * Cancel request for getting information about a peer.
385  * Note that an eventual change in preference, trust or bandwidth
386  * assignment MAY have already been committed at the time, 
387  * so cancelling a request is NOT sure to undo the original
388  * request.  The original request may or may not still commit.
389  * The only thing cancellation ensures is that the callback
390  * from the original request will no longer be called.
391  *
392  * @param irc context returned by the original GNUNET_CORE_peer_get_info call
393  */
394 void
395 GNUNET_CORE_peer_change_preference_cancel (struct
396                                            GNUNET_CORE_InformationRequestContext
397                                            *irc);
398
399
400 /**
401  * Iterate over all connected peers.
402  *
403  * @param cfg configuration handle
404  * @param peer_cb function to call with the peer information
405  * @param cb_cls closure for peer_cb
406  * @return GNUNET_OK on success, GNUNET_SYSERR on errors
407  */
408 int
409 GNUNET_CORE_iterate_peers (const struct GNUNET_CONFIGURATION_Handle *cfg,
410                            GNUNET_CORE_ConnectEventHandler peer_cb,
411                            void *cb_cls);
412
413 /**
414  * Iterate over all currently connected peers.
415  * Calls peer_cb with each connected peer, and then
416  * once with NULL to indicate that all peers have
417  * been handled.
418  *
419  * @param cfg configuration to use
420  * @param peer the specific peer to check for
421  * @param peer_cb function to call with the peer information
422  * @param cb_cls closure for peer_cb
423  *
424  * @return GNUNET_OK if iterating, GNUNET_SYSERR on error
425  */
426 int
427 GNUNET_CORE_is_peer_connected (const struct GNUNET_CONFIGURATION_Handle *cfg,
428                                struct GNUNET_PeerIdentity *peer,
429                                GNUNET_CORE_ConnectEventHandler peer_cb,
430                                void *cb_cls);
431
432
433 /**
434  * Handle for a transmission request.
435  */
436 struct GNUNET_CORE_TransmitHandle;
437
438
439 /**
440  * Ask the core to call "notify" once it is ready to transmit the
441  * given number of bytes to the specified "target".   Must only be
442  * called after a connection to the respective peer has been
443  * established (and the client has been informed about this).
444  * 
445  *
446  * @param handle connection to core service
447  * @param cork is corking allowed for this transmission?
448  * @param priority how important is the message?
449  * @param maxdelay how long can the message wait?
450  * @param target who should receive the message,
451  *        use NULL for this peer (loopback)
452  * @param notify_size how many bytes of buffer space does notify want?
453  * @param notify function to call when buffer space is available;
454  *        will be called with NULL on timeout or if the overall queue
455  *        for this peer is larger than queue_size and this is currently
456  *        the message with the lowest priority; will also be called
457  *        with 'NULL' buf if the peer disconnects; since the disconnect
458  *        signal will be emmitted even later, clients MUST cancel
459  *        all pending transmission requests DURING the disconnect
460  *        handler (unless they ensure that 'notify' never calls
461  *        'GNUNET_CORE_notify_transmit_ready').
462  * @param notify_cls closure for notify
463  * @return non-NULL if the notify callback was queued,
464  *         NULL if we can not even queue the request (insufficient
465  *         memory); if NULL is returned, "notify" will NOT be called.
466  */
467 struct GNUNET_CORE_TransmitHandle *GNUNET_CORE_notify_transmit_ready (struct
468                                                                       GNUNET_CORE_Handle
469                                                                       *handle,
470                                                                       int cork,
471                                                                       uint32_t
472                                                                       priority,
473                                                                       struct
474                                                                       GNUNET_TIME_Relative
475                                                                       maxdelay,
476                                                                       const
477                                                                       struct
478                                                                       GNUNET_PeerIdentity
479                                                                       *target,
480                                                                       size_t
481                                                                       notify_size,
482                                                                       GNUNET_CONNECTION_TransmitReadyNotify
483                                                                       notify,
484                                                                       void
485                                                                       *notify_cls);
486
487
488 /**
489  * Cancel the specified transmission-ready notification.
490  *
491  * @param th handle that was returned by "notify_transmit_ready".
492  */
493 void
494 GNUNET_CORE_notify_transmit_ready_cancel (struct GNUNET_CORE_TransmitHandle
495                                           *th);
496
497
498 #if 0                           /* keep Emacsens' auto-indent happy */
499 {
500 #endif
501 #ifdef __cplusplus
502 }
503 #endif
504
505 /* ifndef GNUNET_CORE_SERVICE_H */
506 #endif
507 /* end of gnunet_core_service.h */