2 This file is part of GNUnet.
3 Copyright (C) 2009-2017 GNUnet e.V.
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.
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.
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.
21 * @author Christian Grothoff
23 * @file include/gnunet_core_service.h
24 * Core service; the main API for encrypted P2P communications
26 * @defgroup core Core service
27 * Encrypted direct communication between peers
29 * @see [Documentation](https://gnunet.org/gnunet-core-subsystem)
33 #ifndef GNUNET_CORE_SERVICE_H
34 #define GNUNET_CORE_SERVICE_H
39 #if 0 /* keep Emacsens' auto-indent happy */
44 #include "gnunet_util_lib.h"
45 #include "gnunet_transport_service.h"
48 * Version number of GNUnet-core API.
50 #define GNUNET_CORE_VERSION 0x00000001
55 enum GNUNET_CORE_Priority
59 * Lowest priority, i.e. background traffic (i.e. fs)
61 GNUNET_CORE_PRIO_BACKGROUND = 0,
64 * Normal traffic (i.e. cadet/dv relay, DHT)
66 GNUNET_CORE_PRIO_BEST_EFFORT = 1,
69 * Urgent traffic (local peer, i.e. conversation).
71 GNUNET_CORE_PRIO_URGENT = 2,
74 * Highest priority, control traffic (i.e. NSE, Core/Cadet KX).
76 GNUNET_CORE_PRIO_CRITICAL_CONTROL = 3
83 * Opaque handle to the service.
85 struct GNUNET_CORE_Handle;
89 * Method called whenever a given peer connects.
92 * @param peer peer identity this notification is about
95 (*GNUNET_CORE_ConnectEventHandler) (void *cls,
96 const struct GNUNET_PeerIdentity *peer,
97 struct GNUNET_MQ_Handle *mq);
101 * Method called whenever a peer disconnects.
104 * @param peer peer identity this notification is about
107 (*GNUNET_CORE_DisconnectEventHandler) (void *cls,
108 const struct GNUNET_PeerIdentity *peer,
113 * Function called after #GNUNET_CORE_connect has succeeded (or failed
114 * for good). Note that the private key of the peer is intentionally
115 * not exposed here; if you need it, your process should try to read
116 * the private key file directly (which should work if you are
117 * authorized...). Implementations of this function must not call
118 * #GNUNET_CORE_disconnect (other than by scheduling a new task to
122 * @param my_identity ID of this peer, NULL if we failed
125 (*GNUNET_CORE_StartupCallback) (void *cls,
126 const struct GNUNET_PeerIdentity *my_identity);
130 * Connect to the core service. Note that the connection may complete
131 * (or fail) asynchronously. This function primarily causes the given
132 * callback notification functions to be invoked whenever the
133 * specified event happens. The maximum number of queued
134 * notifications (queue length) is per client; the queue is shared
135 * across all types of notifications. So a slow client that registers
136 * for @a outbound_notify also risks missing @a inbound_notify messages.
137 * Certain events (such as connect/disconnect notifications) are not
138 * subject to queue size limitations.
140 * @param cfg configuration to use
141 * @param cls closure for the various callbacks that follow (including handlers in the handlers array)
142 * @param init callback to call once we have successfully
143 * connected to the core service
144 * @param connects function to call on peer connect, can be NULL
145 * @param disconnects function to call on peer disconnect / timeout, can be NULL
146 * @param handlers callbacks for messages we care about, NULL-terminated
147 * note that the core is allowed to drop notifications about inbound
148 * messages if the client does not process them fast enough (for this
149 * notification type, a bounded queue is used)
150 * @return handle to the core service (only useful for disconnect until @a init is called),
151 * NULL on error (in this case, init is never called)
153 struct GNUNET_CORE_Handle *
154 GNUNET_CORE_connect (const struct GNUNET_CONFIGURATION_Handle *cfg,
156 GNUNET_CORE_StartupCallback init,
157 GNUNET_CORE_ConnectEventHandler connects,
158 GNUNET_CORE_DisconnectEventHandler disconnects,
159 const struct GNUNET_MQ_MessageHandler *handlers);
163 * Disconnect from the core service.
165 * @param handle connection to core to disconnect
168 GNUNET_CORE_disconnect (struct GNUNET_CORE_Handle *handle);
172 * Inquire with CORE what options should be set for a message
173 * so that it is transmitted with the given @a priority and
174 * the given @a cork value.
176 * @param cork desired corking
177 * @param priority desired message priority
178 * @param[out] flags set to `flags` value for #GNUNET_MQ_set_options()
179 * @return `extra` argument to give to #GNUNET_MQ_set_options()
182 GNUNET_CORE_get_mq_options (int cork,
183 enum GNUNET_CORE_Priority priority,
188 * Obtain the message queue for a connected peer.
190 * @param h the core handle
191 * @param pid the identity of the peer
192 * @return NULL if @a pid is not connected
194 struct GNUNET_MQ_Handle *
195 GNUNET_CORE_get_mq (const struct GNUNET_CORE_Handle *h,
196 const struct GNUNET_PeerIdentity *pid);
200 * Handle to a CORE monitoring operation.
202 struct GNUNET_CORE_MonitorHandle;
206 * State machine for our P2P encryption handshake. Everyone starts in
207 * #GNUNET_CORE_KX_STATE_DOWN, if we receive the other peer's key
208 * (other peer initiated) we start in state
209 * #GNUNET_CORE_KX_STATE_KEY_RECEIVED (since we will immediately send
210 * our own); otherwise we start in #GNUNET_CORE_KX_STATE_KEY_SENT. If
211 * we get back a PONG from within either state, we move up to
212 * #GNUNET_CORE_KX_STATE_UP (the PONG will always be sent back
213 * encrypted with the key we sent to the other peer). Eventually,
214 * we will try to rekey, for this we will enter
215 * #GNUNET_CORE_KX_STATE_REKEY_SENT until the rekey operation is
216 * confirmed by a PONG from the other peer.
218 enum GNUNET_CORE_KxState
223 GNUNET_CORE_KX_STATE_DOWN,
226 * We've sent our session key.
228 GNUNET_CORE_KX_STATE_KEY_SENT,
231 * We've received the other peers session key.
233 GNUNET_CORE_KX_STATE_KEY_RECEIVED,
236 * The other peer has confirmed our session key + PING with a PONG
237 * message encrypted with his session key (which we got). Key
240 GNUNET_CORE_KX_STATE_UP,
243 * We're rekeying (or had a timeout), so we have sent the other peer
244 * our new ephemeral key, but we did not get a matching PONG yet.
245 * This is equivalent to being #GNUNET_CORE_KX_STATE_KEY_RECEIVED,
246 * except that the session is marked as 'up' with sessions (as we
247 * don't want to drop and re-establish P2P connections simply due to
250 GNUNET_CORE_KX_STATE_REKEY_SENT,
253 * Last state of a KX (when it is being terminated). Set
254 * just before CORE frees the internal state for this peer.
256 GNUNET_CORE_KX_PEER_DISCONNECT,
259 * This is not a state in a peer's state machine, but a special
260 * value used with the #GNUNET_CORE_MonitorCallback to indicate
261 * that we finished the initial iteration over the peers.
263 GNUNET_CORE_KX_ITERATION_FINISHED,
266 * This is not a state in a peer's state machine, but a special
267 * value used with the #GNUNET_CORE_MonitorCallback to indicate
268 * that we lost the connection to the CORE service (and will try
269 * to reconnect). If this happens, most likely the CORE service
270 * crashed and thus all connection state should be assumed lost.
272 GNUNET_CORE_KX_CORE_DISCONNECT
278 * Function called by the monitor callback whenever
279 * a peer's connection status changes.
282 * @param pid identity of the peer this update is about
283 * @param state current key exchange state of the peer
284 * @param timeout when does the current state expire
287 (*GNUNET_CORE_MonitorCallback)(void *cls,
288 const struct GNUNET_PeerIdentity *pid,
289 enum GNUNET_CORE_KxState state,
290 struct GNUNET_TIME_Absolute timeout);
294 * Monitor connectivity and KX status of all peers known to CORE.
295 * Calls @a peer_cb with the current status for each connected peer,
296 * and then once with NULL to indicate that all peers that are
297 * currently active have been handled. After that, the iteration
298 * continues until it is cancelled. Normal users of the CORE API are
299 * not expected to use this function. It is different in that it
300 * truly lists all connections (including those where the KX is in
301 * progress), not just those relevant to the application. This
302 * function is used by special applications for diagnostics.
304 * @param cfg configuration handle
305 * @param peer_cb function to call with the peer information
306 * @param peer_cb_cls closure for @a peer_cb
307 * @return NULL on error
309 struct GNUNET_CORE_MonitorHandle *
310 GNUNET_CORE_monitor_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
311 GNUNET_CORE_MonitorCallback peer_cb,
316 * Stop monitoring CORE activity.
318 * @param mh monitor to stop
321 GNUNET_CORE_monitor_stop (struct GNUNET_CORE_MonitorHandle *mh);
325 * Check if the given peer is currently connected. This function is for special
326 * cirumstances (GNUNET_TESTBED uses it), normal users of the CORE API are
327 * expected to track which peers are connected based on the connect/disconnect
328 * callbacks from #GNUNET_CORE_connect. This function is NOT part of the
329 * 'versioned', 'official' API. This function returns
330 * synchronously after looking in the CORE API cache.
332 * @param h the core handle
333 * @param pid the identity of the peer to check if it has been connected to us
334 * @return #GNUNET_YES if the peer is connected to us; #GNUNET_NO if not
337 GNUNET_CORE_is_peer_connected_sync (const struct GNUNET_CORE_Handle *h,
338 const struct GNUNET_PeerIdentity *pid);
342 * Create a message queue for sending messages to a peer with CORE.
343 * Messages may only be queued with #GNUNET_MQ_send once the init callback has
344 * been called for the given handle.
345 * There must only be one queue per peer for each core handle.
346 * The message queue can only be used to transmit messages,
347 * not to receive them.
349 * @param h the core handle
350 * @param target the target peer for this queue, may not be NULL
351 * @return a message queue for sending messages over the core handle
354 struct GNUNET_MQ_Handle *
355 GNUNET_CORE_mq_create (struct GNUNET_CORE_Handle *h,
356 const struct GNUNET_PeerIdentity *target);
359 #if 0 /* keep Emacsens' auto-indent happy */
366 /* ifndef GNUNET_CORE_SERVICE_H */
369 /** @} */ /* end of group core */
371 /* end of gnunet_core_service.h */