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