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