8c57b23b7447140f17cedf158eb25ee072209838
[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  * Request that the core should try to connect to a particular peer.
246  * Once the request has been transmitted to the core, the continuation
247  * function will be called.  Note that this does NOT mean that a
248  * connection was successfully established -- it only means that the
249  * core will now try.  Successful establishment of the connection
250  * will be signalled to the 'connects' callback argument of
251  * 'GNUNET_CORE_connect' only.  If the core service does not respond
252  * to our connection attempt within the given time frame, 'cont' will
253  * be called with the TIMEOUT reason code.
254  *
255  * @param h core handle
256  * @param timeout how long to try to talk to core
257  * @param peer who should we connect to
258  * @param cont function to call once the request has been completed (or timed out)
259  * @param cont_cls closure for cont
260  * @return NULL on error (cont will not be called), otherwise handle for cancellation
261  */
262 struct GNUNET_CORE_PeerRequestHandle *
263 GNUNET_CORE_peer_request_connect (struct GNUNET_CORE_Handle *h,
264                                   struct GNUNET_TIME_Relative timeout,
265                                   const struct GNUNET_PeerIdentity * peer,
266                                   GNUNET_SCHEDULER_Task cont,
267                                   void *cont_cls);
268
269
270 /**
271  * Cancel a pending request to connect to a particular peer.  Must not
272  * be called after the 'cont' function was invoked.
273  *
274  * @param req request handle that was returned for the original request
275  */
276 void
277 GNUNET_CORE_peer_request_connect_cancel (struct GNUNET_CORE_PeerRequestHandle *req);
278
279
280 /**
281  * Function called with perference change information about the given peer.
282  *
283  * @param cls closure
284  * @param peer identifies the peer
285  * @param bandwidth_out available amount of outbound bandwidth
286  * @param amount set to the amount that was actually reserved or unreserved;
287  *               either the full requested amount or zero (no partial reservations)
288  * @param preference current traffic preference for the given peer
289  */
290 typedef void
291   (*GNUNET_CORE_PeerConfigurationInfoCallback) (void *cls,
292                                                 const struct
293                                                 GNUNET_PeerIdentity * peer,
294                                                 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
295                                                 int amount,
296                                                 uint64_t preference);
297
298
299
300 /**
301  * Context that can be used to cancel a peer information request.
302  */
303 struct GNUNET_CORE_InformationRequestContext;
304
305
306 /**
307  * Obtain statistics and/or change preferences for the given peer.
308  * You can only have one such pending request per peer.
309  *
310  * @param h core handle
311  * @param peer identifies the peer
312  * @param timeout after how long should we give up (and call "info" with NULL
313  *                for "peer" to signal an error)?
314  * @param bw_out set to the current bandwidth limit (sending) for this peer,
315  *                caller should set "bpm_out" to "-1" to avoid changing
316  *                the current value; otherwise "bw_out" will be lowered to
317  *                the specified value; passing a pointer to "0" can be used to force
318  *                us to disconnect from the peer; "bw_out" might not increase
319  *                as specified since the upper bound is generally
320  *                determined by the other peer!
321  * @param amount reserve N bytes for receiving, negative
322  *                amounts can be used to undo a (recent) reservation;
323  * @param preference increase incoming traffic share preference by this amount;
324  *                in the absence of "amount" reservations, we use this
325  *                preference value to assign proportional bandwidth shares
326  *                to all connected peers
327  * @param info function to call with the resulting configuration information
328  * @param info_cls closure for info
329  * @return NULL on error
330  */
331 struct GNUNET_CORE_InformationRequestContext *
332 GNUNET_CORE_peer_change_preference (struct GNUNET_CORE_Handle *h,
333                                     const struct GNUNET_PeerIdentity *peer,
334                                     struct GNUNET_TIME_Relative timeout,
335                                     struct GNUNET_BANDWIDTH_Value32NBO bw_out,
336                                     int32_t amount,
337                                     uint64_t preference,
338                                     GNUNET_CORE_PeerConfigurationInfoCallback info,
339                                     void *info_cls);
340
341
342 /**
343  * Cancel request for getting information about a peer.
344  * Note that an eventual change in preference, trust or bandwidth
345  * assignment MAY have already been committed at the time, 
346  * so cancelling a request is NOT sure to undo the original
347  * request.  The original request may or may not still commit.
348  * The only thing cancellation ensures is that the callback
349  * from the original request will no longer be called.
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 /**
358  * Iterate over all connected peers.
359  *
360  * @param cfg configuration handle
361  * @param peer_cb function to call with the peer information
362  * @param cb_cls closure for peer_cb
363  * @return GNUNET_OK on success, GNUNET_SYSERR on errors
364  */
365 int
366 GNUNET_CORE_iterate_peers (const struct GNUNET_CONFIGURATION_Handle *cfg,
367                            GNUNET_CORE_ConnectEventHandler peer_cb,
368                            void *cb_cls);
369
370
371 /**
372  * Handle for a transmission request.
373  */
374 struct GNUNET_CORE_TransmitHandle;
375
376
377 /**
378  * Ask the core to call "notify" once it is ready to transmit the
379  * given number of bytes to the specified "target".  If we are not yet
380  * connected to the specified peer, a call to this function will cause
381  * us to try to establish a connection.
382  *
383  * @param handle connection to core service
384  * @param priority how important is the message?
385  * @param maxdelay how long can the message wait?
386  * @param target who should receive the message,
387  *        use NULL for this peer (loopback)
388  * @param notify_size how many bytes of buffer space does notify want?
389  * @param notify function to call when buffer space is available;
390  *        will be called with NULL on timeout or if the overall queue
391  *        for this peer is larger than queue_size and this is currently
392  *        the message with the lowest priority
393  * @param notify_cls closure for notify
394  * @return non-NULL if the notify callback was queued,
395  *         NULL if we can not even queue the request (insufficient
396  *         memory); if NULL is returned, "notify" will NOT be called.
397  */
398 struct GNUNET_CORE_TransmitHandle *
399 GNUNET_CORE_notify_transmit_ready (struct
400                                    GNUNET_CORE_Handle
401                                    *handle,
402                                    uint32_t priority,
403                                    struct
404                                    GNUNET_TIME_Relative
405                                    maxdelay,
406                                    const
407                                    struct
408                                    GNUNET_PeerIdentity
409                                    *target,
410                                    size_t
411                                    notify_size,
412                                    GNUNET_CONNECTION_TransmitReadyNotify
413                                    notify,
414                                    void
415                                    *notify_cls);
416
417
418 /**
419  * Cancel the specified transmission-ready notification.
420  *
421  * @param th handle that was returned by "notify_transmit_ready".
422  */
423 void
424 GNUNET_CORE_notify_transmit_ready_cancel (struct GNUNET_CORE_TransmitHandle
425                                           *th);
426
427
428 #if 0                           /* keep Emacsens' auto-indent happy */
429 {
430 #endif
431 #ifdef __cplusplus
432 }
433 #endif
434
435 /* ifndef GNUNET_CORE_SERVICE_H */
436 #endif
437 /* end of gnunet_core_service.h */