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