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