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