PSYC(store), Multicast: use ECDSA slave/member keys; PSYC: add GNUNET_PSYC_message_cr...
[oweals/gnunet.git] / src / include / gnunet_psycstore_service.h
1 /*
2      This file is part of GNUnet.
3      (C) 2013 Christian Grothoff (and other contributing authors)
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., 59 Temple Place - Suite 330,
18      Boston, MA 02111-1307, USA.
19 */
20
21 /**
22  * @file include/gnunet_psycstore_service.h
23  * @brief PSYCstore service; implements persistent storage for the PSYC service
24  * @author Gabor X Toth
25  * @author Christian Grothoff
26  */
27 #ifndef GNUNET_PSYCSTORE_SERVICE_H
28 #define GNUNET_PSYCSTORE_SERVICE_H
29
30 #ifdef __cplusplus
31 extern "C"
32 {
33 #if 0                           /* keep Emacsens' auto-indent happy */
34 }
35 #endif
36 #endif
37
38 #include "gnunet_util_lib.h"
39 #include "gnunet_env_lib.h"
40 #include "gnunet_multicast_service.h"
41 #include "gnunet_psyc_service.h"
42
43 /**
44  * Version number of GNUnet PSYCstore API.
45  */
46 #define GNUNET_PSYCSTORE_VERSION 0x00000000
47
48 /**
49  * Flags for stored messages.
50  */
51 enum GNUNET_PSYCSTORE_MessageFlags
52 {
53   /**
54    * The message contains state modifiers.
55    */
56   GNUNET_PSYCSTORE_MESSAGE_STATE = 1 << 0,
57
58   /**
59    * The state modifiers have been applied to the state store.
60    */
61   GNUNET_PSYCSTORE_MESSAGE_STATE_APPLIED = 1 << 1,
62
63   /**
64    * The message contains a state hash.
65    */
66   GNUNET_PSYCSTORE_MESSAGE_STATE_HASH = 1 << 2
67 };
68
69
70 /**
71  * Handle for a PSYCstore
72  */
73 struct GNUNET_PSYCSTORE_Handle;
74
75
76 /**
77  * Connect to the PSYCstore service.
78  *
79  * @param cfg Configuration to use.
80  *
81  * @return Handle for the connecton.
82  */
83 struct GNUNET_PSYCSTORE_Handle *
84 GNUNET_PSYCSTORE_connect (const struct GNUNET_CONFIGURATION_Handle *cfg);
85
86
87 /**
88  * Disconnect from the PSYCstore service.
89  *
90  * @param h Handle for the connection.
91  */
92 void
93 GNUNET_PSYCSTORE_disconnect (struct GNUNET_PSYCSTORE_Handle *h);
94
95
96 /**
97  * Handle for an operation on the PSYCSTORE (useful to cancel the operation).
98  */
99 struct GNUNET_PSYCSTORE_OperationHandle;
100
101
102 /**
103  * Function called with the result of an asynchronous operation.
104  *
105  * @param result #GNUNET_SYSERR on error,
106  *        #GNUNET_YES on success or if the peer was a member,
107  *        #GNUNET_NO if the peer was not a member
108  */
109 typedef void
110 (*GNUNET_PSYCSTORE_ResultCallback) (void *cls,
111                                     int64_t result,
112                                     const char *err_msg);
113
114
115 /**
116  * Store join/leave events for a PSYC channel in order to be able to answer
117  * membership test queries later.
118  *
119  * @param h Handle for the PSYCstore.
120  * @param channel_key The channel where the event happened.
121  * @param slave_key Public key of joining/leaving slave.
122  * @param did_join #GNUNET_YES on join, #GNUNET_NO on part.
123  * @param announced_at ID of the message that announced the membership change.
124  * @param effective_since Message ID this membership change is in effect since.
125  *        For joins it is <= announced_at, for parts it is always 0.
126  * @param group_generation In case of a part, the last group generation the
127  *        slave has access to.  It has relevance when a larger message have
128  *        fragments with different group generations.
129  * @param rcb Callback to call with the result of the storage operation.
130  * @param rcb_cls Closure for the callback.
131  *
132  * @return Operation handle that can be used to cancel the operation.
133  */
134 struct GNUNET_PSYCSTORE_OperationHandle *
135 GNUNET_PSYCSTORE_membership_store (struct GNUNET_PSYCSTORE_Handle *h,
136                                    const struct GNUNET_CRYPTO_EddsaPublicKey *channel_key,
137                                    const struct GNUNET_CRYPTO_EcdsaPublicKey *slave_key,
138                                    int did_join,
139                                    uint64_t announced_at,
140                                    uint64_t effective_since,
141                                    uint64_t group_generation,
142                                    GNUNET_PSYCSTORE_ResultCallback rcb,
143                                    void *rcb_cls);
144
145
146 /**
147  * Test if a member was admitted to the channel at the given message ID.
148  *
149  * This is useful when relaying and replaying messages to check if a particular
150  * slave has access to the message fragment with a given group generation.  It
151  * is also used when handling join requests to determine whether the slave is
152  * currently admitted to the channel.
153  *
154  * @param h Handle for the PSYCstore.
155  * @param channel_key The channel we are interested in.
156  * @param slave_key Public key of slave whose membership to check.
157  * @param message_id Message ID for which to do the membership test.
158  * @param group_generation Group generation of the fragment of the message to
159  *        test.  It has relevance if the message consists of multiple fragments
160  *        with different group generations.
161  *        FIXME: not needed if there are no overlapping messages.
162  * @param rcb Callback to call with the test result.
163  * @param rcb_cls Closure for the callback.
164  *
165  * @return Operation handle that can be used to cancel the operation.
166  */
167 struct GNUNET_PSYCSTORE_OperationHandle *
168 GNUNET_PSYCSTORE_membership_test (struct GNUNET_PSYCSTORE_Handle *h,
169                                   const struct GNUNET_CRYPTO_EddsaPublicKey *channel_key,
170                                   const struct GNUNET_CRYPTO_EcdsaPublicKey *slave_key,
171                                   uint64_t message_id,
172                                   uint64_t group_generation,
173                                   GNUNET_PSYCSTORE_ResultCallback rcb,
174                                   void *rcb_cls);
175
176
177 /**
178  * Store a message fragment sent to a channel.
179  *
180  * @param h Handle for the PSYCstore.
181  * @param channel_key The channel the message belongs to.
182  * @param msg Message to store.
183  * @param psycstore_flags Flags indicating whether the PSYC message contains
184  *        state modifiers.
185  * @param rcb Callback to call with the result of the operation.
186  * @param rcb_cls Closure for the callback.
187  *
188  * @return Handle that can be used to cancel the operation.
189  */
190 struct GNUNET_PSYCSTORE_OperationHandle *
191 GNUNET_PSYCSTORE_fragment_store (struct GNUNET_PSYCSTORE_Handle *h,
192                                  const struct GNUNET_CRYPTO_EddsaPublicKey *channel_key,
193                                  const struct GNUNET_MULTICAST_MessageHeader *msg,
194                                  enum GNUNET_PSYCSTORE_MessageFlags psycstore_flags,
195                                  GNUNET_PSYCSTORE_ResultCallback rcb,
196                                  void *rcb_cls);
197
198
199 /**
200  * Function called with one message fragment, as the result of a
201  * GNUNET_PSYCSTORE_fragment_get() or GNUNET_PSYCSTORE_message_get() call.
202  *
203  * @param cls Closure.
204  * @param message The retrieved message fragment.  A NULL value indicates that
205  *        there are no more results to be returned.
206  * @param psycstore_flags Flags stored with the message.
207  *
208  * @return #GNUNET_NO to stop calling this callback with further fragments,
209  *         #GNUNET_YES to continue.
210  */
211 typedef int
212 (*GNUNET_PSYCSTORE_FragmentCallback) (void *cls,
213                                       struct GNUNET_MULTICAST_MessageHeader *message,
214                                       enum GNUNET_PSYCSTORE_MessageFlags psycstore_flags);
215
216
217 /**
218  * Retrieve a message fragment by fragment ID.
219  *
220  * @param h Handle for the PSYCstore.
221  * @param channel_key The channel we are interested in.
222  * @param fragment_id Fragment ID to check.  Use 0 to get the latest message fragment.
223  * @param fcb Callback to call with the retrieved fragment.
224  * @param rcb Callback to call with the result of the operation.
225  * @param cls Closure for the callbacks.
226  *
227  * @return Handle that can be used to cancel the operation.
228  */
229 struct GNUNET_PSYCSTORE_OperationHandle *
230 GNUNET_PSYCSTORE_fragment_get (struct GNUNET_PSYCSTORE_Handle *h,
231                                const struct GNUNET_CRYPTO_EddsaPublicKey *channel_key,
232                                uint64_t fragment_id,
233                                GNUNET_PSYCSTORE_FragmentCallback fcb,
234                                GNUNET_PSYCSTORE_ResultCallback rcb,
235                                void *cls);
236
237
238 /**
239  * Retrieve all fragments of a message.
240  *
241  * @param h Handle for the PSYCstore.
242  * @param channel_key The channel we are interested in.
243  * @param message_id Message ID to check.  Use 0 to get the latest message.
244  * @param fcb Callback to call with the retrieved fragments.
245  * @param rcb Callback to call with the result of the operation.
246  * @param cls Closure for the callbacks.
247  *
248  * @return Handle that can be used to cancel the operation.
249  */
250 struct GNUNET_PSYCSTORE_OperationHandle *
251 GNUNET_PSYCSTORE_message_get (struct GNUNET_PSYCSTORE_Handle *h,
252                               const struct GNUNET_CRYPTO_EddsaPublicKey *channel_key,
253                               uint64_t message_id,
254                               GNUNET_PSYCSTORE_FragmentCallback fcb,
255                               GNUNET_PSYCSTORE_ResultCallback rcb,
256                               void *cls);
257
258
259 /**
260  * Retrieve a fragment of message specified by its message ID and fragment
261  * offset.
262  *
263  * @param h Handle for the PSYCstore.
264  * @param channel_key The channel we are interested in.
265  * @param message_id Message ID to check.  Use 0 to get the latest message.
266  * @param fragment_offset Offset of the fragment to retrieve.
267  * @param fcb Callback to call with the retrieved fragments.
268  * @param rcb Callback to call with the result of the operation.
269  * @param cls Closure for the callbacks.
270  *
271  * @return Handle that can be used to cancel the operation.
272  */
273 struct GNUNET_PSYCSTORE_OperationHandle *
274 GNUNET_PSYCSTORE_message_get_fragment (struct GNUNET_PSYCSTORE_Handle *h,
275                                        const struct GNUNET_CRYPTO_EddsaPublicKey *channel_key,
276                                        uint64_t message_id,
277                                        uint64_t fragment_offset,
278                                        GNUNET_PSYCSTORE_FragmentCallback fcb,
279                                        GNUNET_PSYCSTORE_ResultCallback rcb,
280                                        void *cls);
281
282
283 /**
284  * Callback used to return the latest value of counters for the channel master.
285  *
286  * @see GNUNET_PSYCSTORE_counters_get()
287  *
288  * @param cls Closure.
289  * @param result_code Status code for the operation:
290  *        #GNUNET_OK: success, counter values are returned.
291  *        #GNUNET_NO: no message has been sent to the channel yet.
292  *        #GNUNET_SYSERR: an error occurred.
293  * @param max_fragment_id Latest message fragment ID, used by multicast.
294  * @param max_message_id Latest message ID, used by PSYC.
295  * @param max_group_generation Latest group generation, used by PSYC.
296  * @param max_state_message_id Latest message ID containing state modifiers that
297  *        was applied to the state store.  Used for the state sync process.
298  */
299 typedef void
300 (*GNUNET_PSYCSTORE_CountersCallback) (void *cls,
301                                       int result_code,
302                                       uint64_t max_fragment_id,
303                                       uint64_t max_message_id,
304                                       uint64_t max_group_generation,
305                                       uint64_t max_state_message_id);
306
307
308 /**
309  * Retrieve latest values of counters for a channel.
310  *
311  * The current value of counters are needed
312  * - when a channel master is restarted, so that it can continue incrementing
313  *   the counters from their last value.
314  * - when a channel slave rejoins and starts the state synchronization process.
315  *
316  * @param h Handle for the PSYCstore.
317  * @param channel_key Public key that identifies the channel.
318  * @param ccb Callback to call with the result.
319  * @param ccb_cls Closure for the @a ccb callback.
320  *
321  * @return Handle that can be used to cancel the operation.
322  */
323 struct GNUNET_PSYCSTORE_OperationHandle *
324 GNUNET_PSYCSTORE_counters_get (struct GNUNET_PSYCSTORE_Handle *h,
325                                struct GNUNET_CRYPTO_EddsaPublicKey *channel_key,
326                                GNUNET_PSYCSTORE_CountersCallback ccb,
327                                void *ccb_cls);
328
329
330 /**
331  * Apply modifiers of a message to the current channel state.
332  *
333  * An error is returned if there are missing messages containing state
334  * operations before the current one.
335  *
336  * @param h Handle for the PSYCstore.
337  * @param channel_key The channel we are interested in.
338  * @param message_id ID of the message that contains the @a modifiers.
339  * @param state_delta Value of the @e state_delta PSYC header variable of the message.
340  * @param modifier_count Number of elements in the @a modifiers array.
341  * @param modifiers List of modifiers to apply.
342  * @param rcb Callback to call with the result of the operation.
343  * @param rcb_cls Closure for the @a rcb callback.
344  *
345  * @return Handle that can be used to cancel the operation.
346  */
347 struct GNUNET_PSYCSTORE_OperationHandle *
348 GNUNET_PSYCSTORE_state_modify (struct GNUNET_PSYCSTORE_Handle *h,
349                                const struct GNUNET_CRYPTO_EddsaPublicKey *channel_key,
350                                uint64_t message_id,
351                                uint64_t state_delta,
352                                size_t modifier_count,
353                                const struct GNUNET_ENV_Modifier *modifiers,
354                                GNUNET_PSYCSTORE_ResultCallback rcb,
355                                void *rcb_cls);
356
357
358 /**
359  * Store synchronized state.
360  *
361  * @param h Handle for the PSYCstore.
362  * @param channel_key The channel we are interested in.
363  * @param message_id ID of the message that contains the state_hash PSYC header variable.
364  * @param modifier_count Number of elements in the @a modifiers array.
365  * @param modifiers Full state to store.
366  * @param rcb Callback to call with the result of the operation.
367  * @param rcb_cls Closure for the callback.
368  *
369  * @return Handle that can be used to cancel the operation.
370  */
371 struct GNUNET_PSYCSTORE_OperationHandle *
372 GNUNET_PSYCSTORE_state_sync (struct GNUNET_PSYCSTORE_Handle *h,
373                              const struct GNUNET_CRYPTO_EddsaPublicKey *channel_key,
374                              uint64_t message_id,
375                              size_t modifier_count,
376                              const struct GNUNET_ENV_Modifier *modifiers,
377                              GNUNET_PSYCSTORE_ResultCallback rcb,
378                              void *rcb_cls);
379
380
381
382 /**
383  * Reset the state of a channel.
384  *
385  * Delete all state variables stored for the given channel.
386  *
387  * @param h Handle for the PSYCstore.
388  * @param channel_key The channel we are interested in.
389  * @param rcb Callback to call with the result of the operation.
390  * @param rcb_cls Closure for the callback.
391  *
392  * @return Handle that can be used to cancel the operation.
393  */
394 struct GNUNET_PSYCSTORE_OperationHandle *
395 GNUNET_PSYCSTORE_state_reset (struct GNUNET_PSYCSTORE_Handle *h,
396                               const struct GNUNET_CRYPTO_EddsaPublicKey
397                               *channel_key,
398                               GNUNET_PSYCSTORE_ResultCallback rcb,
399                               void *rcb_cls);
400
401
402 /**
403  * Update signed values of state variables in the state store.
404  *
405  * @param h Handle for the PSYCstore.
406  * @param channel_key The channel we are interested in.
407  * @param message_id Message ID that contained the state @a hash.
408  * @param hash Hash of the serialized full state.
409  * @param rcb Callback to call with the result of the operation.
410  * @param rcb_cls Closure for the callback.
411  *
412  */
413 struct GNUNET_PSYCSTORE_OperationHandle *
414 GNUNET_PSYCSTORE_state_hash_update (struct GNUNET_PSYCSTORE_Handle *h,
415                                     const struct GNUNET_CRYPTO_EddsaPublicKey *channel_key,
416                                     uint64_t message_id,
417                                     const struct GNUNET_HashCode *hash,
418                                     GNUNET_PSYCSTORE_ResultCallback rcb,
419                                     void *rcb_cls);
420
421
422 /**
423  * Function called with the value of a state variable.
424  *
425  * @param cls Closure.
426  * @param name Name of the state variable.  A NULL value indicates that there are no more
427  *        state variables to be returned.
428  * @param value Value of the state variable.
429  * @param value_size Number of bytes in @a value.
430  *
431  * @return #GNUNET_NO to stop calling this callback with further variables,
432  *         #GNUNET_YES to continue.
433  */;
434 typedef int
435 (*GNUNET_PSYCSTORE_StateCallback) (void *cls, const char *name,
436                                    const void *value, size_t value_size);
437
438
439 /**
440  * Retrieve the best matching state variable.
441  *
442  * @param h Handle for the PSYCstore.
443  * @param channel_key The channel we are interested in.
444  * @param name Name of variable to match, the returned variable might be less specific.
445  * @param scb Callback to return the matching state variable.
446  * @param rcb Callback to call with the result of the operation.
447  * @param cls Closure for the callbacks.
448  *
449  * @return Handle that can be used to cancel the operation.
450  */
451 struct GNUNET_PSYCSTORE_OperationHandle *
452 GNUNET_PSYCSTORE_state_get (struct GNUNET_PSYCSTORE_Handle *h,
453                             const struct GNUNET_CRYPTO_EddsaPublicKey *channel_key,
454                             const char *name,
455                             GNUNET_PSYCSTORE_StateCallback scb,
456                             GNUNET_PSYCSTORE_ResultCallback rcb,
457                             void *cls);
458
459
460 /**
461  * Retrieve all state variables for a channel with the given prefix.
462  *
463  * @param h Handle for the PSYCstore.
464  * @param channel_key The channel we are interested in.
465  * @param name_prefix Prefix of state variable names to match.
466  * @param scb Callback to return matching state variables.
467  * @param rcb Callback to call with the result of the operation.
468  * @param cls Closure for the callbacks.
469  *
470  * @return Handle that can be used to cancel the operation.
471  */
472 struct GNUNET_PSYCSTORE_OperationHandle *
473 GNUNET_PSYCSTORE_state_get_prefix (struct GNUNET_PSYCSTORE_Handle *h,
474                                    const struct GNUNET_CRYPTO_EddsaPublicKey *channel_key,
475                                    const char *name_prefix,
476                                    GNUNET_PSYCSTORE_StateCallback scb,
477                                    GNUNET_PSYCSTORE_ResultCallback rcb,
478                                    void *cls);
479
480
481 /**
482  * Cancel an operation.
483  *
484  * @param op Handle for the operation to cancel.
485  */
486 void
487 GNUNET_PSYCSTORE_operation_cancel (struct GNUNET_PSYCSTORE_OperationHandle *op);
488
489
490
491
492 #if 0                           /* keep Emacsens' auto-indent happy */
493 {
494 #endif
495 #ifdef __cplusplus
496 }
497 #endif
498
499 /* ifndef GNUNET_PSYCSTORE_SERVICE_H */
500 #endif
501 /* end of gnunet_psycstore_service.h */