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