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