-peer review
[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 0x00000001
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  * @param atsi_count number of records in 'atsi'
61  */
62 typedef void (*GNUNET_CORE_ConnectEventHandler) (void *cls,
63                                                  const struct
64                                                  GNUNET_PeerIdentity * peer,
65                                                  const struct
66                                                  GNUNET_ATS_Information * atsi,
67                                                  unsigned int atsi_count);
68
69
70 /**
71  * Method called whenever a peer disconnects.
72  *
73  * @param cls closure
74  * @param peer peer identity this notification is about
75  */
76 typedef void (*GNUNET_CORE_DisconnectEventHandler) (void *cls,
77                                                     const struct
78                                                     GNUNET_PeerIdentity * peer);
79
80
81 /**
82  * Functions with this signature are called whenever a message is
83  * received or transmitted.
84  *
85  * @param cls closure (set from GNUNET_CORE_connect)
86  * @param peer the other peer involved (sender or receiver, NULL
87  *        for loopback messages where we are both sender and receiver)
88  * @param message the actual message
89  * @param atsi performance data for the connection
90  * @param atsi_count number of records in 'atsi'
91  * @return GNUNET_OK to keep the connection open,
92  *         GNUNET_SYSERR to close it (signal serious error)
93  */
94 typedef int (*GNUNET_CORE_MessageCallback) (void *cls,
95                                             const struct GNUNET_PeerIdentity *
96                                             other,
97                                             const struct GNUNET_MessageHeader *
98                                             message,
99                                             const struct GNUNET_ATS_Information
100                                             * atsi, unsigned int atsi_count);
101
102
103 /**
104  * Message handler.  Each struct specifies how to handle on particular
105  * type of message received.
106  */
107 struct GNUNET_CORE_MessageHandler
108 {
109   /**
110    * Function to call for messages of "type".
111    */
112   GNUNET_CORE_MessageCallback callback;
113
114   /**
115    * Type of the message this handler covers.
116    */
117   uint16_t type;
118
119   /**
120    * Expected size of messages of this type.  Use 0 for variable-size.
121    * If non-zero, messages of the given type will be discarded if they
122    * do not have the right size.
123    */
124   uint16_t expected_size;
125
126 };
127
128
129 /**
130  * Function called after GNUNET_CORE_connect has succeeded (or failed
131  * for good).  Note that the private key of the peer is intentionally
132  * not exposed here; if you need it, your process should try to read
133  * the private key file directly (which should work if you are
134  * authorized...).
135  *
136  * @param cls closure
137  * @param server handle to the server, NULL if we failed
138  * @param my_identity ID of this peer, NULL if we failed
139  */
140 typedef void (*GNUNET_CORE_StartupCallback) (void *cls,
141                                              struct GNUNET_CORE_Handle * server,
142                                              const struct GNUNET_PeerIdentity *
143                                              my_identity);
144
145
146 /**
147  * Connect to the core service.  Note that the connection may complete
148  * (or fail) asynchronously.  This function primarily causes the given
149  * callback notification functions to be invoked whenever the
150  * specified event happens.  The maximum number of queued
151  * notifications (queue length) is per client; the queue is shared
152  * across all types of notifications.  So a slow client that registers
153  * for 'outbound_notify' also risks missing 'inbound_notify' messages.
154  * Certain events (such as connect/disconnect notifications) are not
155  * subject to queue size limitations.
156  *
157  * @param cfg configuration to use
158  * @param cls closure for the various callbacks that follow (including handlers in the handlers array)
159  * @param init callback to call once we have successfully
160  *        connected to the core service
161  * @param connects function to call on peer connect, can be NULL
162  * @param disconnects function to call on peer disconnect / timeout, can be NULL
163  * @param inbound_notify function to call for all inbound messages, can be NULL
164  *                note that the core is allowed to drop notifications about inbound
165  *                messages if the client does not process them fast enough (for this
166  *                notification type, a bounded queue is used)
167  * @param inbound_hdr_only set to GNUNET_YES if inbound_notify will only read the
168  *                GNUNET_MessageHeader and hence we do not need to give it the full message;
169  *                can be used to improve efficiency, ignored if inbound_notify is NULL
170  *                note that the core is allowed to drop notifications about inbound
171  *                messages if the client does not process them fast enough (for this
172  *                notification type, a bounded queue is used)
173  * @param outbound_notify function to call for all outbound messages, can be NULL;
174  *                note that the core is allowed to drop notifications about outbound
175  *                messages if the client does not process them fast enough (for this
176  *                notification type, a bounded queue is used)
177  * @param outbound_hdr_only set to GNUNET_YES if outbound_notify will only read the
178  *                GNUNET_MessageHeader and hence we do not need to give it the full message
179  *                can be used to improve efficiency, ignored if outbound_notify is NULL
180  *                note that the core is allowed to drop notifications about outbound
181  *                messages if the client does not process them fast enough (for this
182  *                notification type, a bounded queue is used)
183  * @param handlers callbacks for messages we care about, NULL-terminated
184  *                note that the core is allowed to drop notifications about inbound
185  *                messages if the client does not process them fast enough (for this
186  *                notification type, a bounded queue is used)
187  * @return handle to the core service (only useful for disconnect until 'init' is called),
188  *           NULL on error (in this case, init is never called)
189  */
190 struct GNUNET_CORE_Handle *
191 GNUNET_CORE_connect (const struct GNUNET_CONFIGURATION_Handle *cfg,
192                      void *cls,
193                      GNUNET_CORE_StartupCallback init,
194                      GNUNET_CORE_ConnectEventHandler connects,
195                      GNUNET_CORE_DisconnectEventHandler disconnects,
196                      GNUNET_CORE_MessageCallback inbound_notify,
197                      int inbound_hdr_only,
198                      GNUNET_CORE_MessageCallback outbound_notify,
199                      int outbound_hdr_only,
200                      const struct GNUNET_CORE_MessageHandler *handlers);
201
202
203 /**
204  * Disconnect from the core service.    This function can only
205  * be called *after* all pending 'GNUNET_CORE_notify_transmit_ready'
206  * requests have been explicitly cancelled.
207  *
208  * @param handle connection to core to disconnect
209  */
210 void
211 GNUNET_CORE_disconnect (struct GNUNET_CORE_Handle *handle);
212
213
214 /**
215  * Handle for a transmission request.
216  */
217 struct GNUNET_CORE_TransmitHandle;
218
219
220 /**
221  * Ask the core to call "notify" once it is ready to transmit the
222  * given number of bytes to the specified "target".  Must only be
223  * called after a connection to the respective peer has been
224  * established (and the client has been informed about this).  You may
225  * have one request of this type pending for each connected peer at
226  * any time.  If a peer disconnects, the application MUST call
227  * "GNUNET_CORE_notify_transmit_ready_cancel" on the respective
228  * transmission request, if one such request is pending.
229  *
230  * @param handle connection to core service
231  * @param cork is corking allowed for this transmission?
232  * @param priority how important is the message?
233  * @param maxdelay how long can the message wait?
234  * @param target who should receive the message, never NULL (can be this peer's identity for loopback)
235  * @param notify_size how many bytes of buffer space does notify want?
236  * @param notify function to call when buffer space is available;
237  *        will be called with NULL on timeout; clients MUST cancel
238  *        all pending transmission requests DURING the disconnect
239  *        handler
240  * @param notify_cls closure for notify
241  * @return non-NULL if the notify callback was queued,
242  *         NULL if we can not even queue the request (request already pending);
243  *         if NULL is returned, "notify" will NOT be called.
244  */
245 struct GNUNET_CORE_TransmitHandle *
246 GNUNET_CORE_notify_transmit_ready (struct GNUNET_CORE_Handle *handle, int cork,
247                                    uint32_t priority,
248                                    struct GNUNET_TIME_Relative maxdelay,
249                                    const struct GNUNET_PeerIdentity *target,
250                                    size_t notify_size,
251                                    GNUNET_CONNECTION_TransmitReadyNotify notify,
252                                    void *notify_cls);
253
254
255 /**
256  * Cancel the specified transmission-ready notification.
257  *
258  * @param th handle that was returned by "notify_transmit_ready".
259  */
260 void
261 GNUNET_CORE_notify_transmit_ready_cancel (struct GNUNET_CORE_TransmitHandle
262                                           *th);
263
264
265
266
267
268 /**
269  * Iterate over all connected peers.  Calls peer_cb with each
270  * connected peer, and then once with NULL to indicate that all peers
271  * have been handled.  Normal users of the CORE API are not expected
272  * to use this function.  It is different in that it truly lists
273  * all connections, not just those relevant to the application.  This
274  * function is used by special applications for diagnostics.  This
275  * function is NOT part of the 'versioned', 'official' API.
276  *
277  * FIXME: we should probably make it possible to 'cancel' the
278  * operation...
279  *
280  * @param cfg configuration handle
281  * @param peer_cb function to call with the peer information
282  * @param cb_cls closure for peer_cb
283  * @return GNUNET_OK on success, GNUNET_SYSERR on errors
284  */
285 int
286 GNUNET_CORE_iterate_peers (const struct GNUNET_CONFIGURATION_Handle *cfg,
287                            GNUNET_CORE_ConnectEventHandler peer_cb,
288                            void *cb_cls);
289
290
291 /**
292  * Handle to cancel 'is_peer_connected' test.
293  */
294 struct GNUNET_CORE_ConnectTestHandle;
295
296
297 /**
298  * Check if the given peer is currently connected and return information
299  * about the session if so.  This function is for special cirumstances
300  * (GNUNET_TESTING uses it), normal users of the CORE API are
301  * expected to track which peers are connected based on the
302  * connect/disconnect callbacks from GNUNET_CORE_connect.  This
303  * function is NOT part of the 'versioned', 'official' API.
304  *
305  * @param cfg configuration to use
306  * @param peer the specific peer to check for
307  * @param peer_cb function to call with the peer information
308  * @param cb_cls closure for peer_cb
309  * @return handle to cancel the operation
310  */
311 struct GNUNET_CORE_ConnectTestHandle *
312 GNUNET_CORE_is_peer_connected (const struct GNUNET_CONFIGURATION_Handle *cfg,
313                                const struct GNUNET_PeerIdentity *peer,
314                                GNUNET_CORE_ConnectEventHandler peer_cb,
315                                void *cb_cls);
316
317
318 /**
319  * Abort 'is_connected' test operation.
320  *
321  * @param cth handle for operation to cancel
322  */
323 void
324 GNUNET_CORE_is_peer_connected_cancel (struct GNUNET_CORE_ConnectTestHandle *cth);
325
326
327 #if 0                           /* keep Emacsens' auto-indent happy */
328 {
329 #endif
330 #ifdef __cplusplus
331 }
332 #endif
333
334 /* ifndef GNUNET_CORE_SERVICE_H */
335 #endif
336 /* end of gnunet_core_service.h */