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