externalizing secushare logic
[oweals/gnunet.git] / src / include / gnunet_core_service.h
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2009-2017 GNUnet e.V.
4
5      GNUnet is free software: you can redistribute it and/or modify it
6      under the terms of the GNU Affero General Public License as published
7      by the Free Software Foundation, either version 3 of the License,
8      or (at your 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      Affero General Public License for more details.
14     
15      You should have received a copy of the GNU Affero General Public License
16      along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
18      SPDX-License-Identifier: AGPL3.0-or-later
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  * @return closure associated with @a peer. given to mq callbacks and
94  *         #GNUNET_CORE_DisconnectEventHandler
95  */
96 typedef void *
97 (*GNUNET_CORE_ConnectEventHandler) (void *cls,
98                                     const struct GNUNET_PeerIdentity *peer,
99                                     struct GNUNET_MQ_Handle *mq);
100
101
102 /**
103  * Method called whenever a peer disconnects.
104  *
105  * @param cls closure
106  * @param peer peer identity this notification is about
107  * @param peer_cls closure associated with peer. given in
108  *        #GNUNET_CORE_ConnectEventHandler
109  */
110 typedef void
111 (*GNUNET_CORE_DisconnectEventHandler) (void *cls,
112                                        const struct GNUNET_PeerIdentity *peer,
113                                        void *peer_cls);
114
115
116 /**
117  * Function called after #GNUNET_CORE_connect has succeeded (or failed
118  * for good).  Note that the private key of the peer is intentionally
119  * not exposed here; if you need it, your process should try to read
120  * the private key file directly (which should work if you are
121  * authorized...).  Implementations of this function must not call
122  * #GNUNET_CORE_disconnect (other than by scheduling a new task to
123  * do this later).
124  *
125  * @param cls closure
126  * @param my_identity ID of this peer, NULL if we failed
127  */
128 typedef void
129 (*GNUNET_CORE_StartupCallback) (void *cls,
130                                 const struct GNUNET_PeerIdentity *my_identity);
131
132
133 /**
134  * Connect to the core service.  Note that the connection may complete
135  * (or fail) asynchronously.  This function primarily causes the given
136  * callback notification functions to be invoked whenever the
137  * specified event happens.  The maximum number of queued
138  * notifications (queue length) is per client; the queue is shared
139  * across all types of notifications.  So a slow client that registers
140  * for @a outbound_notify also risks missing @a inbound_notify messages.
141  * Certain events (such as connect/disconnect notifications) are not
142  * subject to queue size limitations.
143  *
144  * @param cfg configuration to use
145  * @param cls closure for the various callbacks that follow (including handlers in the handlers array)
146  * @param init callback to call once we have successfully
147  *        connected to the core service
148  * @param connects function to call on peer connect, can be NULL
149  * @param disconnects function to call on peer disconnect / timeout, can be NULL
150  * @param handlers callbacks for messages we care about, NULL-terminated
151  *                note that the core is allowed to drop notifications about inbound
152  *                messages if the client does not process them fast enough (for this
153  *                notification type, a bounded queue is used)
154  * @return handle to the core service (only useful for disconnect until @a init is called),
155  *           NULL on error (in this case, init is never called)
156  */
157 struct GNUNET_CORE_Handle *
158 GNUNET_CORE_connect (const struct GNUNET_CONFIGURATION_Handle *cfg,
159                      void *cls,
160                      GNUNET_CORE_StartupCallback init,
161                      GNUNET_CORE_ConnectEventHandler connects,
162                      GNUNET_CORE_DisconnectEventHandler disconnects,
163                      const struct GNUNET_MQ_MessageHandler *handlers);
164
165
166 /**
167  * Disconnect from the core service.
168  *
169  * @param handle connection to core to disconnect
170  */
171 void
172 GNUNET_CORE_disconnect (struct GNUNET_CORE_Handle *handle);
173
174
175 /**
176  * Inquire with CORE what options should be set for a message
177  * so that it is transmitted with the given @a priority and
178  * the given @a cork value.
179  *
180  * @param cork desired corking
181  * @param priority desired message priority
182  * @param[out] flags set to `flags` value for #GNUNET_MQ_set_options()
183  * @return `extra` argument to give to #GNUNET_MQ_set_options()
184  */
185 const void *
186 GNUNET_CORE_get_mq_options (int cork,
187                             enum GNUNET_CORE_Priority priority,
188                             uint64_t *flags);
189
190
191 /**
192  * Obtain the message queue for a connected peer.
193  *
194  * @param h the core handle
195  * @param pid the identity of the peer
196  * @return NULL if @a pid is not connected
197  */
198 struct GNUNET_MQ_Handle *
199 GNUNET_CORE_get_mq (const struct GNUNET_CORE_Handle *h,
200                     const struct GNUNET_PeerIdentity *pid);
201
202
203 /**
204  * Handle to a CORE monitoring operation.
205  */
206 struct GNUNET_CORE_MonitorHandle;
207
208
209 /**
210  * State machine for our P2P encryption handshake.  Everyone starts in
211  * #GNUNET_CORE_KX_STATE_DOWN, if we receive the other peer's key
212  * (other peer initiated) we start in state
213  * #GNUNET_CORE_KX_STATE_KEY_RECEIVED (since we will immediately send
214  * our own); otherwise we start in #GNUNET_CORE_KX_STATE_KEY_SENT.  If
215  * we get back a PONG from within either state, we move up to
216  * #GNUNET_CORE_KX_STATE_UP (the PONG will always be sent back
217  * encrypted with the key we sent to the other peer).  Eventually,
218  * we will try to rekey, for this we will enter
219  * #GNUNET_CORE_KX_STATE_REKEY_SENT until the rekey operation is
220  * confirmed by a PONG from the other peer.
221  */
222 enum GNUNET_CORE_KxState
223 {
224   /**
225    * No handshake yet.
226    */
227   GNUNET_CORE_KX_STATE_DOWN = 0,
228
229   /**
230    * We've sent our session key.
231    */
232   GNUNET_CORE_KX_STATE_KEY_SENT,
233
234   /**
235    * We've received the other peers session key.
236    */
237   GNUNET_CORE_KX_STATE_KEY_RECEIVED,
238
239   /**
240    * The other peer has confirmed our session key + PING with a PONG
241    * message encrypted with their session key (which we got).  Key
242    * exchange is done.
243    */
244   GNUNET_CORE_KX_STATE_UP,
245
246   /**
247    * We're rekeying (or had a timeout), so we have sent the other peer
248    * our new ephemeral key, but we did not get a matching PONG yet.
249    * This is equivalent to being #GNUNET_CORE_KX_STATE_KEY_RECEIVED,
250    * except that the session is marked as 'up' with sessions (as we
251    * don't want to drop and re-establish P2P connections simply due to
252    * rekeying).
253    */
254   GNUNET_CORE_KX_STATE_REKEY_SENT,
255
256   /**
257    * Last state of a KX (when it is being terminated).  Set
258    * just before CORE frees the internal state for this peer.
259    */
260   GNUNET_CORE_KX_PEER_DISCONNECT,
261
262   /**
263    * This is not a state in a peer's state machine, but a special
264    * value used with the #GNUNET_CORE_MonitorCallback to indicate
265    * that we finished the initial iteration over the peers.
266    */
267   GNUNET_CORE_KX_ITERATION_FINISHED,
268
269   /**
270    * This is not a state in a peer's state machine, but a special
271    * value used with the #GNUNET_CORE_MonitorCallback to indicate
272    * that we lost the connection to the CORE service (and will try
273    * to reconnect).  If this happens, most likely the CORE service
274    * crashed and thus all connection state should be assumed lost.
275    */
276   GNUNET_CORE_KX_CORE_DISCONNECT
277
278 };
279
280
281 /**
282  * Function called by the monitor callback whenever
283  * a peer's connection status changes.
284  *
285  * @param cls closure
286  * @param pid identity of the peer this update is about
287  * @param state current key exchange state of the peer
288  * @param timeout when does the current state expire
289  */
290 typedef void
291 (*GNUNET_CORE_MonitorCallback)(void *cls,
292                                const struct GNUNET_PeerIdentity *pid,
293                                enum GNUNET_CORE_KxState state,
294                                struct GNUNET_TIME_Absolute timeout);
295
296
297 /**
298  * Monitor connectivity and KX status of all peers known to CORE.
299  * Calls @a peer_cb with the current status for each connected peer,
300  * and then once with NULL to indicate that all peers that are
301  * currently active have been handled.  After that, the iteration
302  * continues until it is cancelled.  Normal users of the CORE API are
303  * not expected to use this function.  It is different in that it
304  * truly lists all connections (including those where the KX is in
305  * progress), not just those relevant to the application.  This
306  * function is used by special applications for diagnostics.
307  *
308  * @param cfg configuration handle
309  * @param peer_cb function to call with the peer information
310  * @param peer_cb_cls closure for @a peer_cb
311  * @return NULL on error
312  */
313 struct GNUNET_CORE_MonitorHandle *
314 GNUNET_CORE_monitor_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
315                            GNUNET_CORE_MonitorCallback peer_cb,
316                            void *peer_cb_cls);
317
318
319 /**
320  * Stop monitoring CORE activity.
321  *
322  * @param mh monitor to stop
323  */
324 void
325 GNUNET_CORE_monitor_stop (struct GNUNET_CORE_MonitorHandle *mh);
326
327
328 /**
329  * Check if the given peer is currently connected. This function is for special
330  * cirumstances (GNUNET_TESTBED uses it), normal users of the CORE API are
331  * expected to track which peers are connected based on the connect/disconnect
332  * callbacks from #GNUNET_CORE_connect.  This function is NOT part of the
333  * 'versioned', 'official' API.  This function returns
334  * synchronously after looking in the CORE API cache.
335  *
336  * @param h the core handle
337  * @param pid the identity of the peer to check if it has been connected to us
338  * @return #GNUNET_YES if the peer is connected to us; #GNUNET_NO if not
339  */
340 int
341 GNUNET_CORE_is_peer_connected_sync (const struct GNUNET_CORE_Handle *h,
342                                     const struct GNUNET_PeerIdentity *pid);
343
344
345 /**
346  * Create a message queue for sending messages to a peer with CORE.
347  * Messages may only be queued with #GNUNET_MQ_send once the init callback has
348  * been called for the given handle.
349  * There must only be one queue per peer for each core handle.
350  * The message queue can only be used to transmit messages,
351  * not to receive them.
352  *
353  * @param h the core handle
354  * @param target the target peer for this queue, may not be NULL
355  * @return a message queue for sending messages over the core handle
356  *         to the target peer
357  */
358 struct GNUNET_MQ_Handle *
359 GNUNET_CORE_mq_create (struct GNUNET_CORE_Handle *h,
360                        const struct GNUNET_PeerIdentity *target);
361
362
363 #if 0                           /* keep Emacsens' auto-indent happy */
364 {
365 #endif
366 #ifdef __cplusplus
367 }
368 #endif
369
370 /* ifndef GNUNET_CORE_SERVICE_H */
371 #endif
372
373 /** @} */  /* end of group core */
374
375 /* end of gnunet_core_service.h */