-bump year'
[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
98
99 /**
100  * Method called whenever a peer disconnects.
101  *
102  * @param cls closure
103  * @param peer peer identity this notification is about
104  */
105 typedef void
106 (*GNUNET_CORE_DisconnectEventHandler) (void *cls,
107                                        const struct GNUNET_PeerIdentity *peer);
108
109
110 /**
111  * Method called whenever a given peer connects.
112  *
113  * @param cls closure
114  * @param peer peer identity this notification is about
115  */
116 typedef void *
117 (*GNUNET_CORE_ConnecTEventHandler) (void *cls,
118                                     const struct GNUNET_PeerIdentity *peer,
119                                     struct GNUNET_MQ_Handle *mq);
120
121
122 /**
123  * Method called whenever a peer disconnects.
124  *
125  * @param cls closure
126  * @param peer peer identity this notification is about
127  */
128 typedef void
129 (*GNUNET_CORE_DisconnecTEventHandler) (void *cls,
130                                        const struct GNUNET_PeerIdentity *peer,
131                                        void *peer_cls);
132
133
134 /**
135  * Functions with this signature are called whenever a message is
136  * received or transmitted.
137  *
138  * @param cls closure (set from #GNUNET_CORE_connect)
139  * @param peer the other peer involved (sender or receiver, NULL
140  *        for loopback messages where we are both sender and receiver)
141  * @param message the actual message
142  * @return #GNUNET_OK to keep the connection open,
143  *         #GNUNET_SYSERR to close connection to the peer (signal serious error)
144  */
145 typedef int
146 (*GNUNET_CORE_MessageCallback) (void *cls,
147                                 const struct GNUNET_PeerIdentity *other,
148                                 const struct GNUNET_MessageHeader *message);
149
150
151 /**
152  * Message handler.  Each struct specifies how to handle on particular
153  * type of message received.
154  */
155 struct GNUNET_CORE_MessageHandler
156 {
157   /**
158    * Function to call for messages of @e type.
159    */
160   GNUNET_CORE_MessageCallback callback;
161
162   /**
163    * Type of the message this handler covers.
164    */
165   uint16_t type;
166
167   /**
168    * Expected size of messages of this type.  Use 0 for variable-size.
169    * If non-zero, messages of the given type will be discarded if they
170    * do not have the right size.
171    */
172   uint16_t expected_size;
173
174 };
175
176
177 /**
178  * Function called after #GNUNET_CORE_connect has succeeded (or failed
179  * for good).  Note that the private key of the peer is intentionally
180  * not exposed here; if you need it, your process should try to read
181  * the private key file directly (which should work if you are
182  * authorized...).  Implementations of this function must not call
183  * #GNUNET_CORE_disconnect (other than by scheduling a new task to
184  * do this later).
185  *
186  * @param cls closure
187  * @param my_identity ID of this peer, NULL if we failed
188  */
189 typedef void
190 (*GNUNET_CORE_StartupCallback) (void *cls,
191                                 const struct GNUNET_PeerIdentity *my_identity);
192
193
194 /**
195  * Connect to the core service.  Note that the connection may complete
196  * (or fail) asynchronously.  This function primarily causes the given
197  * callback notification functions to be invoked whenever the
198  * specified event happens.  The maximum number of queued
199  * notifications (queue length) is per client; the queue is shared
200  * across all types of notifications.  So a slow client that registers
201  * for @a outbound_notify also risks missing @a inbound_notify messages.
202  * Certain events (such as connect/disconnect notifications) are not
203  * subject to queue size limitations.
204  *
205  * @param cfg configuration to use
206  * @param cls closure for the various callbacks that follow (including handlers in the handlers array)
207  * @param init callback to call once we have successfully
208  *        connected to the core service
209  * @param connects function to call on peer connect, can be NULL
210  * @param disconnects function to call on peer disconnect / timeout, can be NULL
211  * @param inbound_notify function to call for all inbound messages, can be NULL
212  *                note that the core is allowed to drop notifications about inbound
213  *                messages if the client does not process them fast enough (for this
214  *                notification type, a bounded queue is used)
215  * @param inbound_hdr_only set to #GNUNET_YES if @a inbound_notify will only read the
216  *                `struct GNUNET_MessageHeader` and hence we do not need to give it the full message;
217  *                can be used to improve efficiency, ignored if inbound_notify is NULL
218  *                note that the core is allowed to drop notifications about inbound
219  *                messages if the client does not process them fast enough (for this
220  *                notification type, a bounded queue is used)
221  * @param outbound_notify function to call for all outbound messages, can be NULL;
222  *                note that the core is allowed to drop notifications about outbound
223  *                messages if the client does not process them fast enough (for this
224  *                notification type, a bounded queue is used)
225  * @param outbound_hdr_only set to #GNUNET_YES if @a outbound_notify will only read the
226  *                `struct GNUNET_MessageHeader` and hence we do not need to give it the full message
227  *                can be used to improve efficiency, ignored if outbound_notify is NULL
228  *                note that the core is allowed to drop notifications about outbound
229  *                messages if the client does not process them fast enough (for this
230  *                notification type, a bounded queue is used)
231  * @param handlers callbacks for messages we care about, NULL-terminated
232  *                note that the core is allowed to drop notifications about inbound
233  *                messages if the client does not process them fast enough (for this
234  *                notification type, a bounded queue is used)
235  * @return handle to the core service (only useful for disconnect until @a init is called),
236  *           NULL on error (in this case, init is never called)
237  */
238 struct GNUNET_CORE_Handle *
239 GNUNET_CORE_connect (const struct GNUNET_CONFIGURATION_Handle *cfg,
240                      void *cls,
241                      GNUNET_CORE_StartupCallback init,
242                      GNUNET_CORE_ConnectEventHandler connects,
243                      GNUNET_CORE_DisconnectEventHandler disconnects,
244                      GNUNET_CORE_MessageCallback inbound_notify,
245                      int inbound_hdr_only,
246                      GNUNET_CORE_MessageCallback outbound_notify,
247                      int outbound_hdr_only,
248                      const struct GNUNET_CORE_MessageHandler *handlers);
249
250
251 /**
252  * Connect to the core service.  Note that the connection may complete
253  * (or fail) asynchronously.  This function primarily causes the given
254  * callback notification functions to be invoked whenever the
255  * specified event happens.  The maximum number of queued
256  * notifications (queue length) is per client; the queue is shared
257  * across all types of notifications.  So a slow client that registers
258  * for @a outbound_notify also risks missing @a inbound_notify messages.
259  * Certain events (such as connect/disconnect notifications) are not
260  * subject to queue size limitations.
261  *
262  * @param cfg configuration to use
263  * @param cls closure for the various callbacks that follow (including handlers in the handlers array)
264  * @param init callback to call once we have successfully
265  *        connected to the core service
266  * @param connects function to call on peer connect, can be NULL
267  * @param disconnects function to call on peer disconnect / timeout, can be NULL
268  * @param handlers callbacks for messages we care about, NULL-terminated
269  *                note that the core is allowed to drop notifications about inbound
270  *                messages if the client does not process them fast enough (for this
271  *                notification type, a bounded queue is used)
272  * @return handle to the core service (only useful for disconnect until @a init is called),
273  *           NULL on error (in this case, init is never called)
274  */
275 struct GNUNET_CORE_Handle *
276 GNUNET_CORE_connecT (const struct GNUNET_CONFIGURATION_Handle *cfg,
277                      void *cls,
278                      GNUNET_CORE_StartupCallback init,
279                      GNUNET_CORE_ConnecTEventHandler connects,
280                      GNUNET_CORE_DisconnecTEventHandler disconnects,
281                      const struct GNUNET_MQ_MessageHandler *handlers);
282
283
284 /**
285  * Disconnect from the core service.    This function can only
286  * be called *after* all pending #GNUNET_CORE_notify_transmit_ready
287  * requests have been explicitly cancelled.
288  *
289  * @param handle connection to core to disconnect
290  */
291 void
292 GNUNET_CORE_disconnect (struct GNUNET_CORE_Handle *handle);
293
294
295 /**
296  * Handle for a transmission request.
297  */
298 struct GNUNET_CORE_TransmitHandle;
299
300
301 /**
302  * Ask the core to call @a notify once it is ready to transmit the
303  * given number of bytes to the specified @a target.  Must only be
304  * called after a connection to the respective peer has been
305  * established (and the client has been informed about this).  You may
306  * have one request of this type pending for each connected peer at
307  * any time.  If a peer disconnects, the application MUST call
308  * #GNUNET_CORE_notify_transmit_ready_cancel() on the respective
309  * transmission request, if one such request is pending.
310  *
311  * @param handle connection to core service
312  * @param cork is corking allowed for this transmission?
313  * @param priority how important is the message?
314  * @param maxdelay how long can the message wait? Only effective if @a cork is #GNUNET_YES
315  * @param target who should receive the message, never NULL (can be this peer's identity for loopback)
316  * @param notify_size how many bytes of buffer space does @a notify want?
317  * @param notify function to call when buffer space is available;
318  *        will be called with NULL on timeout; clients MUST cancel
319  *        all pending transmission requests DURING the disconnect
320  *        handler
321  * @param notify_cls closure for @a notify
322  * @return non-NULL if the notify callback was queued,
323  *         NULL if we can not even queue the request (request already pending);
324  *         if NULL is returned, @a notify will NOT be called.
325  */
326 struct GNUNET_CORE_TransmitHandle *
327 GNUNET_CORE_notify_transmit_ready (struct GNUNET_CORE_Handle *handle,
328                                    int cork,
329                                    enum GNUNET_CORE_Priority priority,
330                                    struct GNUNET_TIME_Relative maxdelay,
331                                    const struct GNUNET_PeerIdentity *target,
332                                    size_t notify_size,
333                                    GNUNET_CONNECTION_TransmitReadyNotify notify,
334                                    void *notify_cls);
335
336
337 /**
338  * Cancel the specified transmission-ready notification.
339  *
340  * @param th handle that was returned by #GNUNET_CORE_notify_transmit_ready().
341  */
342 void
343 GNUNET_CORE_notify_transmit_ready_cancel (struct GNUNET_CORE_TransmitHandle *th);
344
345
346 /**
347  * Handle to a CORE monitoring operation.
348  */
349 struct GNUNET_CORE_MonitorHandle;
350
351
352 /**
353  * State machine for our P2P encryption handshake.  Everyone starts in
354  * #GNUNET_CORE_KX_STATE_DOWN, if we receive the other peer's key
355  * (other peer initiated) we start in state
356  * #GNUNET_CORE_KX_STATE_KEY_RECEIVED (since we will immediately send
357  * our own); otherwise we start in #GNUNET_CORE_KX_STATE_KEY_SENT.  If
358  * we get back a PONG from within either state, we move up to
359  * #GNUNET_CORE_KX_STATE_UP (the PONG will always be sent back
360  * encrypted with the key we sent to the other peer).  Eventually,
361  * we will try to rekey, for this we will enter
362  * #GNUNET_CORE_KX_STATE_REKEY_SENT until the rekey operation is
363  * confirmed by a PONG from the other peer.
364  */
365 enum GNUNET_CORE_KxState
366 {
367   /**
368    * No handshake yet.
369    */
370   GNUNET_CORE_KX_STATE_DOWN,
371
372   /**
373    * We've sent our session key.
374    */
375   GNUNET_CORE_KX_STATE_KEY_SENT,
376
377   /**
378    * We've received the other peers session key.
379    */
380   GNUNET_CORE_KX_STATE_KEY_RECEIVED,
381
382   /**
383    * The other peer has confirmed our session key + PING with a PONG
384    * message encrypted with his session key (which we got).  Key
385    * exchange is done.
386    */
387   GNUNET_CORE_KX_STATE_UP,
388
389   /**
390    * We're rekeying (or had a timeout), so we have sent the other peer
391    * our new ephemeral key, but we did not get a matching PONG yet.
392    * This is equivalent to being #GNUNET_CORE_KX_STATE_KEY_RECEIVED,
393    * except that the session is marked as 'up' with sessions (as we
394    * don't want to drop and re-establish P2P connections simply due to
395    * rekeying).
396    */
397   GNUNET_CORE_KX_STATE_REKEY_SENT,
398
399   /**
400    * Last state of a KX (when it is being terminated).  Set
401    * just before CORE frees the internal state for this peer.
402    */
403   GNUNET_CORE_KX_PEER_DISCONNECT,
404
405   /**
406    * This is not a state in a peer's state machine, but a special
407    * value used with the #GNUNET_CORE_MonitorCallback to indicate
408    * that we finished the initial iteration over the peers.
409    */
410   GNUNET_CORE_KX_ITERATION_FINISHED,
411
412   /**
413    * This is not a state in a peer's state machine, but a special
414    * value used with the #GNUNET_CORE_MonitorCallback to indicate
415    * that we lost the connection to the CORE service (and will try
416    * to reconnect).  If this happens, most likely the CORE service
417    * crashed and thus all connection state should be assumed lost.
418    */
419   GNUNET_CORE_KX_CORE_DISCONNECT
420
421 };
422
423
424 /**
425  * Function called by the monitor callback whenever
426  * a peer's connection status changes.
427  *
428  * @param cls closure
429  * @param pid identity of the peer this update is about
430  * @param state current key exchange state of the peer
431  * @param timeout when does the current state expire
432  */
433 typedef void
434 (*GNUNET_CORE_MonitorCallback)(void *cls,
435                                const struct GNUNET_PeerIdentity *pid,
436                                enum GNUNET_CORE_KxState state,
437                                struct GNUNET_TIME_Absolute timeout);
438
439
440 /**
441  * Monitor connectivity and KX status of all peers known to CORE.
442  * Calls @a peer_cb with the current status for each connected peer,
443  * and then once with NULL to indicate that all peers that are
444  * currently active have been handled.  After that, the iteration
445  * continues until it is cancelled.  Normal users of the CORE API are
446  * not expected to use this function.  It is different in that it
447  * truly lists all connections (including those where the KX is in
448  * progress), not just those relevant to the application.  This
449  * function is used by special applications for diagnostics.
450  *
451  * @param cfg configuration handle
452  * @param peer_cb function to call with the peer information
453  * @param peer_cb_cls closure for @a peer_cb
454  * @return NULL on error
455  */
456 struct GNUNET_CORE_MonitorHandle *
457 GNUNET_CORE_monitor_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
458                            GNUNET_CORE_MonitorCallback peer_cb,
459                            void *peer_cb_cls);
460
461
462 /**
463  * Stop monitoring CORE activity.
464  *
465  * @param mh monitor to stop
466  */
467 void
468 GNUNET_CORE_monitor_stop (struct GNUNET_CORE_MonitorHandle *mh);
469
470
471 /**
472  * Check if the given peer is currently connected. This function is for special
473  * cirumstances (GNUNET_TESTBED uses it), normal users of the CORE API are
474  * expected to track which peers are connected based on the connect/disconnect
475  * callbacks from #GNUNET_CORE_connect.  This function is NOT part of the
476  * 'versioned', 'official' API.  This function returns
477  * synchronously after looking in the CORE API cache.
478  *
479  * @param h the core handle
480  * @param pid the identity of the peer to check if it has been connected to us
481  * @return #GNUNET_YES if the peer is connected to us; #GNUNET_NO if not
482  */
483 int
484 GNUNET_CORE_is_peer_connected_sync (const struct GNUNET_CORE_Handle *h,
485                                     const struct GNUNET_PeerIdentity *pid);
486
487
488 /**
489  * Create a message queue for sending messages to a peer with CORE.
490  * Messages may only be queued with #GNUNET_MQ_send once the init callback has
491  * been called for the given handle.
492  * There must only be one queue per peer for each core handle.
493  * The message queue can only be used to transmit messages,
494  * not to receive them.
495  *
496  * @param h the core handle
497  * @param target the target peer for this queue, may not be NULL
498  * @return a message queue for sending messages over the core handle
499  *         to the target peer
500  */
501 struct GNUNET_MQ_Handle *
502 GNUNET_CORE_mq_create (struct GNUNET_CORE_Handle *h,
503                        const struct GNUNET_PeerIdentity *target);
504
505
506 #if 0                           /* keep Emacsens' auto-indent happy */
507 {
508 #endif
509 #ifdef __cplusplus
510 }
511 #endif
512
513 /* ifndef GNUNET_CORE_SERVICE_H */
514 #endif
515
516 /** @} */  /* end of group core */
517
518 /* end of gnunet_core_service.h */