remove dead legacy decls
[oweals/gnunet.git] / src / include / gnunet_core_service.h
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2009-2014, 2016 GNUnet e.V.
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., 51 Franklin Street, Fifth Floor,
18      Boston, MA 02110-1301, USA.
19 */
20 /**
21  * @author Christian Grothoff
22  *
23  * @file include/gnunet_core_service.h
24  * Core service; the main API for encrypted P2P communications
25  *
26  * @defgroup core  Core service
27  * Encrypted direct communication between peers
28  *
29  * @see [Documentation](https://gnunet.org/gnunet-core-subsystem)
30  *
31  * @{
32  */
33 #ifndef GNUNET_CORE_SERVICE_H
34 #define GNUNET_CORE_SERVICE_H
35
36 #ifdef __cplusplus
37 extern "C"
38 {
39 #if 0                           /* keep Emacsens' auto-indent happy */
40 }
41 #endif
42 #endif
43
44 #include "gnunet_util_lib.h"
45 #include "gnunet_transport_service.h"
46
47 /**
48  * Version number of GNUnet-core API.
49  */
50 #define GNUNET_CORE_VERSION 0x00000001
51
52 /**
53  * Traffic priorities.
54  */
55 enum GNUNET_CORE_Priority
56 {
57
58   /**
59    * Lowest priority, i.e. background traffic (i.e. fs)
60    */
61   GNUNET_CORE_PRIO_BACKGROUND = 0,
62
63   /**
64    * Normal traffic (i.e. cadet/dv relay, DHT)
65    */
66   GNUNET_CORE_PRIO_BEST_EFFORT = 1,
67
68   /**
69    * Urgent traffic (local peer, i.e. conversation).
70    */
71   GNUNET_CORE_PRIO_URGENT = 2,
72
73   /**
74    * Highest priority, control traffic (i.e. NSE, Core/Cadet KX).
75    */
76   GNUNET_CORE_PRIO_CRITICAL_CONTROL = 3
77
78
79 };
80
81
82 /**
83  * Opaque handle to the service.
84  */
85 struct GNUNET_CORE_Handle;
86
87
88 /**
89  * Method called whenever a given peer connects.
90  *
91  * @param cls closure
92  * @param peer peer identity this notification is about
93  */
94 typedef void *
95 (*GNUNET_CORE_ConnectEventHandler) (void *cls,
96                                     const struct GNUNET_PeerIdentity *peer,
97                                     struct GNUNET_MQ_Handle *mq);
98
99
100 /**
101  * Method called whenever a peer disconnects.
102  *
103  * @param cls closure
104  * @param peer peer identity this notification is about
105  */
106 typedef void
107 (*GNUNET_CORE_DisconnectEventHandler) (void *cls,
108                                        const struct GNUNET_PeerIdentity *peer,
109                                        void *peer_cls);
110
111
112 /**
113  * Functions with this signature are called whenever a message is
114  * received or transmitted.
115  *
116  * @param cls closure (set from #GNUNET_CORE_connect)
117  * @param peer the other peer involved (sender or receiver, NULL
118  *        for loopback messages where we are both sender and receiver)
119  * @param message the actual message
120  * @return #GNUNET_OK to keep the connection open,
121  *         #GNUNET_SYSERR to close connection to the peer (signal serious error)
122  */
123 typedef int
124 (*GNUNET_CORE_MessageCallback) (void *cls,
125                                 const struct GNUNET_PeerIdentity *other,
126                                 const struct GNUNET_MessageHeader *message);
127
128
129 /**
130  * Message handler.  Each struct specifies how to handle on particular
131  * type of message received.
132  */
133 struct GNUNET_CORE_MessageHandler
134 {
135   /**
136    * Function to call for messages of @e type.
137    */
138   GNUNET_CORE_MessageCallback callback;
139
140   /**
141    * Type of the message this handler covers.
142    */
143   uint16_t type;
144
145   /**
146    * Expected size of messages of this type.  Use 0 for variable-size.
147    * If non-zero, messages of the given type will be discarded if they
148    * do not have the right size.
149    */
150   uint16_t expected_size;
151
152 };
153
154
155 /**
156  * Function called after #GNUNET_CORE_connect has succeeded (or failed
157  * for good).  Note that the private key of the peer is intentionally
158  * not exposed here; if you need it, your process should try to read
159  * the private key file directly (which should work if you are
160  * authorized...).  Implementations of this function must not call
161  * #GNUNET_CORE_disconnect (other than by scheduling a new task to
162  * do this later).
163  *
164  * @param cls closure
165  * @param my_identity ID of this peer, NULL if we failed
166  */
167 typedef void
168 (*GNUNET_CORE_StartupCallback) (void *cls,
169                                 const struct GNUNET_PeerIdentity *my_identity);
170
171
172 /**
173  * Connect to the core service.  Note that the connection may complete
174  * (or fail) asynchronously.  This function primarily causes the given
175  * callback notification functions to be invoked whenever the
176  * specified event happens.  The maximum number of queued
177  * notifications (queue length) is per client; the queue is shared
178  * across all types of notifications.  So a slow client that registers
179  * for @a outbound_notify also risks missing @a inbound_notify messages.
180  * Certain events (such as connect/disconnect notifications) are not
181  * subject to queue size limitations.
182  *
183  * @param cfg configuration to use
184  * @param cls closure for the various callbacks that follow (including handlers in the handlers array)
185  * @param init callback to call once we have successfully
186  *        connected to the core service
187  * @param connects function to call on peer connect, can be NULL
188  * @param disconnects function to call on peer disconnect / timeout, can be NULL
189  * @param handlers callbacks for messages we care about, NULL-terminated
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  * @return handle to the core service (only useful for disconnect until @a init is called),
194  *           NULL on error (in this case, init is never called)
195  */
196 struct GNUNET_CORE_Handle *
197 GNUNET_CORE_connect (const struct GNUNET_CONFIGURATION_Handle *cfg,
198                      void *cls,
199                      GNUNET_CORE_StartupCallback init,
200                      GNUNET_CORE_ConnectEventHandler connects,
201                      GNUNET_CORE_DisconnectEventHandler disconnects,
202                      const struct GNUNET_MQ_MessageHandler *handlers);
203
204
205 /**
206  * Disconnect from the core service.
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  * Inquire with CORE what options should be set for a message
216  * so that it is transmitted with the given @a priority and
217  * the given @a cork value.
218  *
219  * @param cork desired corking
220  * @param priority desired message priority
221  * @param[out] flags set to `flags` value for #GNUNET_MQ_set_options()
222  * @return `extra` argument to give to #GNUNET_MQ_set_options()
223  */
224 const void *
225 GNUNET_CORE_get_mq_options (int cork,
226                             enum GNUNET_CORE_Priority priority,
227                             uint64_t *flags);
228
229
230 /**
231  * Obtain the message queue for a connected peer.
232  *
233  * @param h the core handle
234  * @param pid the identity of the peer
235  * @return NULL if @a pid is not connected
236  */
237 struct GNUNET_MQ_Handle *
238 GNUNET_CORE_get_mq (const struct GNUNET_CORE_Handle *h,
239                     const struct GNUNET_PeerIdentity *pid);
240
241
242 /**
243  * Handle for a transmission request.
244  */
245 struct GNUNET_CORE_TransmitHandle;
246
247
248 /**
249  * Ask the core to call @a notify once it is ready to transmit the
250  * given number of bytes to the specified @a target.  Must only be
251  * called after a connection to the respective peer has been
252  * established (and the client has been informed about this).  You may
253  * have one request of this type pending for each connected peer at
254  * any time.  If a peer disconnects, the application MUST call
255  * #GNUNET_CORE_notify_transmit_ready_cancel() on the respective
256  * transmission request, if one such request is pending.
257  *
258  * @param handle connection to core service
259  * @param cork is corking allowed for this transmission?
260  * @param priority how important is the message?
261  * @param maxdelay how long can the message wait? Only effective if @a cork is #GNUNET_YES
262  * @param target who should receive the message, never NULL (can be this peer's identity for loopback)
263  * @param notify_size how many bytes of buffer space does @a notify want?
264  * @param notify function to call when buffer space is available;
265  *        will be called with NULL on timeout; clients MUST cancel
266  *        all pending transmission requests DURING the disconnect
267  *        handler
268  * @param notify_cls closure for @a notify
269  * @return non-NULL if the notify callback was queued,
270  *         NULL if we can not even queue the request (request already pending);
271  *         if NULL is returned, @a notify will NOT be called.
272  */
273 struct GNUNET_CORE_TransmitHandle *
274 GNUNET_CORE_notify_transmit_ready (struct GNUNET_CORE_Handle *handle,
275                                    int cork,
276                                    enum GNUNET_CORE_Priority priority,
277                                    struct GNUNET_TIME_Relative maxdelay,
278                                    const struct GNUNET_PeerIdentity *target,
279                                    size_t notify_size,
280                                    GNUNET_CONNECTION_TransmitReadyNotify notify,
281                                    void *notify_cls);
282
283
284 /**
285  * Cancel the specified transmission-ready notification.
286  *
287  * @param th handle that was returned by #GNUNET_CORE_notify_transmit_ready().
288  */
289 void
290 GNUNET_CORE_notify_transmit_ready_cancel (struct GNUNET_CORE_TransmitHandle *th);
291
292
293 /**
294  * Handle to a CORE monitoring operation.
295  */
296 struct GNUNET_CORE_MonitorHandle;
297
298
299 /**
300  * State machine for our P2P encryption handshake.  Everyone starts in
301  * #GNUNET_CORE_KX_STATE_DOWN, if we receive the other peer's key
302  * (other peer initiated) we start in state
303  * #GNUNET_CORE_KX_STATE_KEY_RECEIVED (since we will immediately send
304  * our own); otherwise we start in #GNUNET_CORE_KX_STATE_KEY_SENT.  If
305  * we get back a PONG from within either state, we move up to
306  * #GNUNET_CORE_KX_STATE_UP (the PONG will always be sent back
307  * encrypted with the key we sent to the other peer).  Eventually,
308  * we will try to rekey, for this we will enter
309  * #GNUNET_CORE_KX_STATE_REKEY_SENT until the rekey operation is
310  * confirmed by a PONG from the other peer.
311  */
312 enum GNUNET_CORE_KxState
313 {
314   /**
315    * No handshake yet.
316    */
317   GNUNET_CORE_KX_STATE_DOWN,
318
319   /**
320    * We've sent our session key.
321    */
322   GNUNET_CORE_KX_STATE_KEY_SENT,
323
324   /**
325    * We've received the other peers session key.
326    */
327   GNUNET_CORE_KX_STATE_KEY_RECEIVED,
328
329   /**
330    * The other peer has confirmed our session key + PING with a PONG
331    * message encrypted with his session key (which we got).  Key
332    * exchange is done.
333    */
334   GNUNET_CORE_KX_STATE_UP,
335
336   /**
337    * We're rekeying (or had a timeout), so we have sent the other peer
338    * our new ephemeral key, but we did not get a matching PONG yet.
339    * This is equivalent to being #GNUNET_CORE_KX_STATE_KEY_RECEIVED,
340    * except that the session is marked as 'up' with sessions (as we
341    * don't want to drop and re-establish P2P connections simply due to
342    * rekeying).
343    */
344   GNUNET_CORE_KX_STATE_REKEY_SENT,
345
346   /**
347    * Last state of a KX (when it is being terminated).  Set
348    * just before CORE frees the internal state for this peer.
349    */
350   GNUNET_CORE_KX_PEER_DISCONNECT,
351
352   /**
353    * This is not a state in a peer's state machine, but a special
354    * value used with the #GNUNET_CORE_MonitorCallback to indicate
355    * that we finished the initial iteration over the peers.
356    */
357   GNUNET_CORE_KX_ITERATION_FINISHED,
358
359   /**
360    * This is not a state in a peer's state machine, but a special
361    * value used with the #GNUNET_CORE_MonitorCallback to indicate
362    * that we lost the connection to the CORE service (and will try
363    * to reconnect).  If this happens, most likely the CORE service
364    * crashed and thus all connection state should be assumed lost.
365    */
366   GNUNET_CORE_KX_CORE_DISCONNECT
367
368 };
369
370
371 /**
372  * Function called by the monitor callback whenever
373  * a peer's connection status changes.
374  *
375  * @param cls closure
376  * @param pid identity of the peer this update is about
377  * @param state current key exchange state of the peer
378  * @param timeout when does the current state expire
379  */
380 typedef void
381 (*GNUNET_CORE_MonitorCallback)(void *cls,
382                                const struct GNUNET_PeerIdentity *pid,
383                                enum GNUNET_CORE_KxState state,
384                                struct GNUNET_TIME_Absolute timeout);
385
386
387 /**
388  * Monitor connectivity and KX status of all peers known to CORE.
389  * Calls @a peer_cb with the current status for each connected peer,
390  * and then once with NULL to indicate that all peers that are
391  * currently active have been handled.  After that, the iteration
392  * continues until it is cancelled.  Normal users of the CORE API are
393  * not expected to use this function.  It is different in that it
394  * truly lists all connections (including those where the KX is in
395  * progress), not just those relevant to the application.  This
396  * function is used by special applications for diagnostics.
397  *
398  * @param cfg configuration handle
399  * @param peer_cb function to call with the peer information
400  * @param peer_cb_cls closure for @a peer_cb
401  * @return NULL on error
402  */
403 struct GNUNET_CORE_MonitorHandle *
404 GNUNET_CORE_monitor_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
405                            GNUNET_CORE_MonitorCallback peer_cb,
406                            void *peer_cb_cls);
407
408
409 /**
410  * Stop monitoring CORE activity.
411  *
412  * @param mh monitor to stop
413  */
414 void
415 GNUNET_CORE_monitor_stop (struct GNUNET_CORE_MonitorHandle *mh);
416
417
418 /**
419  * Check if the given peer is currently connected. This function is for special
420  * cirumstances (GNUNET_TESTBED uses it), normal users of the CORE API are
421  * expected to track which peers are connected based on the connect/disconnect
422  * callbacks from #GNUNET_CORE_connect.  This function is NOT part of the
423  * 'versioned', 'official' API.  This function returns
424  * synchronously after looking in the CORE API cache.
425  *
426  * @param h the core handle
427  * @param pid the identity of the peer to check if it has been connected to us
428  * @return #GNUNET_YES if the peer is connected to us; #GNUNET_NO if not
429  */
430 int
431 GNUNET_CORE_is_peer_connected_sync (const struct GNUNET_CORE_Handle *h,
432                                     const struct GNUNET_PeerIdentity *pid);
433
434
435 /**
436  * Create a message queue for sending messages to a peer with CORE.
437  * Messages may only be queued with #GNUNET_MQ_send once the init callback has
438  * been called for the given handle.
439  * There must only be one queue per peer for each core handle.
440  * The message queue can only be used to transmit messages,
441  * not to receive them.
442  *
443  * @param h the core handle
444  * @param target the target peer for this queue, may not be NULL
445  * @return a message queue for sending messages over the core handle
446  *         to the target peer
447  */
448 struct GNUNET_MQ_Handle *
449 GNUNET_CORE_mq_create (struct GNUNET_CORE_Handle *h,
450                        const struct GNUNET_PeerIdentity *target);
451
452
453 #if 0                           /* keep Emacsens' auto-indent happy */
454 {
455 #endif
456 #ifdef __cplusplus
457 }
458 #endif
459
460 /* ifndef GNUNET_CORE_SERVICE_H */
461 #endif
462
463 /** @} */  /* end of group core */
464
465 /* end of gnunet_core_service.h */