-more work on multicast/psyc API designs
[oweals/gnunet.git] / src / include / gnunet_psyc_service.h
1 /*
2      This file is part of GNUnet.
3      (C) 2012, 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_psyc_service.h
23  * @brief PSYC service; high-level access to the PSYC protocol
24  *        note that clients of this API are NOT expected to
25  *        understand the PSYC message format, only the semantics!
26  *        Parsing (and serializing) the PSYC stream format is done
27  *        within the implementation of the libgnunetpsyc library,
28  *        and this API deliberately exposes as little as possible
29  *        of the actual data stream format to the application!
30  * @author Christian Grothoff
31  *
32  * NOTE:
33  * - this API does not know about psyc's "root" and "places";
34  *   there is no 'root' in GNUnet-Psyc as we're decentralized;
35  *   'places' and 'persons' are combined within the same 
36  *   abstraction, that of a "channel".  Channels are identified
37  *   and accessed in this API using a public/private key.  
38  *   Higher-level applications should use NAMES within GADS
39  *   to obtain public keys, and the distinction between 
40  *   'places' and 'persons' can then be made with the help
41  *   of the naming system (and/or conventions).
42  *   Channels are (as in PSYC) organized into a hierarchy; each
43  *   channel owner (the one with the private key) is then
44  *   the operator of the multicast group (its Origin in 
45  *   the terminology of the multicast API).
46  * - The API supports passing large amounts of data using
47  *   'streaming' for the argument passed to a method.  State
48  *   and variables must fit into memory and cannot be streamed
49  *   (thus, no passing of 4 GB of data in a variable; 
50  *   once we implement this, we might want to create a
51  *   #define for the maximum size of a variable).
52  * - PSYC defines standard variables, methods, etc.  This
53  *   library deliberately abstracts over all of these; a
54  *   higher-level API should combine the naming system (GADS)
55  *   and standard methods (message, join, leave, warn,
56  *   fail, error) and variables (action, color, time,
57  *   tag, etc.).  However, this API does take over the
58  *   routing variables, specifically 'context' (channel),
59  *   and 'source'.  We only kind-of support 'target', as
60  *   the target is either everyone in the group or the
61  *   origin, and never just a single member of the group;
62  *   for such individual messages, an application needs to
63  *   construct an 'inbox' channel where the owner (only)
64  *   receives messages (but never forwards; private responses
65  *   would be transmitted by joining the senders 'inbox'
66  *   channel -- or a inbox#bob subchannel).  The
67  *   goal for all of this is to keep the abstractions in this 
68  *   API minimal: interaction with multicast, try \& slice,
69  *   state/variable/channel management.  Higher-level
70  *   operations belong elsewhere (so maybe this API should
71  *   be called 'PSYC-low', whereas a higher-level API
72  *   implementing defaults for standard methods and
73  *   variables might be called 'PSYC-std' or 'PSYC-high'.
74  */
75
76 #ifndef GNUNET_PSYC_SERVICE_H
77 #define GNUNET_PSYC_SERVICE_H
78
79 #ifdef __cplusplus
80 extern "C"
81 {
82 #if 0                           /* keep Emacsens' auto-indent happy */
83 }
84 #endif
85 #endif
86
87 #include "gnunet_util_lib.h"
88 #include "gnunet_multicast_service.h"
89
90
91 /**
92  * Version number of GNUnet-PSYC API.
93  */
94 #define GNUNET_PSYC_VERSION 0x00000000
95
96
97 /**
98  * Method called from PSYC upon receiving a message indicating a call
99  * to a 'method'.  
100  *
101  * @param cls closure
102  * @param full_method_name original method name from PSYC (may be more
103  *        specific than the registered method name due to try-and-slice matching)
104  * @param sender who transmitted the message (origin, except for messages
105  *        from one of the members to the origin)
106  * @param message_id unique message counter for this message;
107  *                   (unique only in combination with the given sender for
108  *                    this channel)
109  * @param group_generation group generation counter for this message
110  *                   (always zero for messages from members to channel owner)
111  * @param data_size number of bytes in 'data'
112  * @param data data stream given to the method (might not be zero-terminated 
113  *             if data is binary)
114  */
115 typedef int (*GNUNET_PSYC_Method)(void *cls,
116                                   const char *full_method_name,
117                                   const struct GNUNET_PeerIdentity *sender,
118                                   uint64_t message_id,
119                                   uint64_t group_generation,
120                                   size_t data_size,
121                                   const void *data);
122
123
124 /**
125  * Handle for the channel of a PSYC group.
126  */
127 struct GNUNET_PSYC_Channel;
128
129
130 /**
131  * Start a PSYC channel.  Will create a multicast group identified by
132  * the given ECC key.  Messages recevied from group members will be
133  * given to the respective handler methods.  If a new member wants to
134  * join a group, the "join" method handler will be invoked; the join
135  * handler must then generate a "join" message to approve the joining
136  * of the new member.  The channel can also change group membership
137  * without explicit requests.  Note that PSYC doesn't itself "understand"
138  * join or leave messages, the respective methods must call other
139  * PSYC functions to inform PSYC about the meaning of the respective
140  * events.
141  *
142  * @param cfg configuration to use (to connect to PSYC service)
143  * @param parent parent channel, NULL for top-level channels
144  * @param name name of the channel, only important if this is a subchannel
145  * @param method_count number of methods in 'methods' array
146  * @param methods functions to invoke on messages received from members,
147  *                typcially at least contains functions for 'join' and 'leave'.
148  * @param priv_key ECC key that will be used to sign messages for this
149  *                 PSYC session; public key is used to identify the
150  *                 PSYC group; FIXME: we'll likely want to use
151  *                 NOT the p521 curve here, but a cheaper one in the future
152  *                 Note that end-users will usually not use the private key
153  *                 directly, but rather look it up in GADS for groups 
154  *                 managed by other users, or select a file with the private
155  *                 key(s) when setting up their own channels
156  * @param join_policy what is the membership policy of the group?
157  *                 Used to automate group management decisions.
158  * @return handle for the channel, NULL on error 
159  */
160 struct GNUNET_PSYC_Channel *
161 GNUNET_PSYC_channel_start (const struct GNUNET_CONFIGURATION_Handle *cfg, 
162                            struct GNUNET_PSYC_Channel *parent,
163                            const char *name,
164                            unsigned int method_count,
165                            const struct GNUNET_PSYC_Method *methods,
166                            const struct GNUNET_CRYPTO_EccPrivateKey *priv_key,
167                            enum GNUNET_MULTICAST_JoinPolicy join_policy);
168
169
170 /**
171  * Possible operations on PSYC state (persistent) and variables (per message).
172  */
173 enum GNUNET_PSYC_Operator
174 {
175   /**
176    * Replace the full state with the new value ("=").
177    */
178   GNUNET_PSYC_SOT_SET_STATE = 0,
179   
180   /**
181    * Delete the complete entry from the state (given data must be
182    * empty).  Equivalent to 'SET' with emtpy data, but more
183    * explicit ("=");
184    */
185   GNUNET_PSYC_SOT_DELETE = 0,
186   
187   /**
188    * Set the value of a variable to a new value (":").
189    */
190   GNUNET_PSYC_SOT_SET_VARIABLE,
191   
192   /**
193    * Add the given value to the set of values in the state ("+").
194    */
195   GNUNET_PSYC_SOT_ADD_STATE,
196   
197   /**
198    * Remove the given value from the set of values in the state ("-").
199    */
200   GNUNET_PSYC_SOT_REMOVE_STATE
201   
202 };
203
204
205 /**
206  * Update channel state (or set a variable).  The state of a channel
207  * must fit into the memory of each member (and the channel); large
208  * values that require streaming must only be passed as the stream
209  * arguments to methods.  State updates might not be transmitted to
210  * group members until the next call to
211  * 'GNUNET_PSYC_channel_broadcast_call_method'.  Variable updates must
212  * be given just before the call to the respective method that needs
213  * the variables.
214  *
215  * @param channel handle to the PSYC group / channel
216  * @param full_state_name name of the field in the channel state to change
217  * @param type kind of update operation (add, remove, replace, delete)
218  * @param data_size number of bytes in data
219  * @param data new state value
220  * @return GNUNET_OK on success, GNUNET_SYSERR on internal error
221  *        (i.e. state too large)
222  */
223 int
224 GNUNET_PSYC_channel_update (struct GNUNET_PSYC_Channel *channel,
225                             const char *full_state_name,
226                             enum GNUNET_PSYC_Operator type,
227                             size_t data_size,
228                             const void *data);
229
230
231 /**
232  * Function called to provide data for a transmission via PSYC.  Note
233  * that returning GNUNET_OK or GNUNET_SYSERR (but not GNUNET_NO)
234  * invalidates the respective transmission handle.
235  *
236  * @param cls closure
237  * @param message_id set to the unique message ID that was generated for
238  *        this message
239  * @param group_generation set to the group generation used for this
240  *        message
241  * @param data_size initially set to the number of bytes available in 'data',
242  *        should be set to the number of bytes written to data (IN/OUT)
243  * @param data where to write the body of the message to give to the method;
244  *        function must copy at most '*data_size' bytes to 'data'.
245  * @return GNUNET_SYSERR on error (fatal, aborts transmission)
246  *         GNUNET_NO on success, if more data is to be transmitted later 
247  *         (should be used if 'data_size' was not big enough to take all the data)
248  *         GNUNET_OK if this completes the transmission (all data supplied)
249  */
250 typedef int (*GNUNET_PSYC_ChannelReadyNotify)(void *cls,
251                                               uint64_t message_id,
252                                               uint64_t group_generation,
253                                               size_t *data_size,
254                                               void *data);
255
256
257 /**
258  * Handle for a pending PSYC transmission operation.
259  */
260 struct GNUNET_PSYC_ChannelTransmitHandle;
261
262
263 /**
264  * Send a message to call a method to all members in the PSYC channel
265  * (and all parent channels if this is a subchannel).
266  *
267  * @param channel handle to the PSYC multicast group
268  * @param increment_group_generation GNUNET_YES if we need to increment
269  *        the group generation counter after transmitting this message
270  * @param full_method_name which method should be invoked
271  * @param notify function to call to obtain the arguments
272  * @param notify_cls closure for 'notify'
273  * @return transmission handle, NULL on error (i.e. more than one request queued)
274  */
275 struct GNUNET_PSYC_ChannelTransmitHandle *
276 GNUNET_PSYC_channel_notify_transmit_ready (struct GNUNET_PSYC_Channel *channel,
277                                            int increment_group_generation,
278                                            const char *full_method_name,
279                                            GNUNET_PSYC_ChannelReadyNotify notify,
280                                            void *notify_cls);
281
282
283 /**
284  * Abort transmission request to channel.
285  *
286  * @param th handle of the request that is being aborted
287  */
288 void
289 GNUNET_PSYC_channel_notify_transmit_ready_cancel (struct GNUNET_PSYC_ChannelTransmitHandle *th);
290
291
292 /**
293  * End a PSYC channel.  Note that subchannels MUST be ended before
294  * their parents.
295  *
296  * @param channel PSYC channel to terminate
297  */
298 void
299 GNUNET_PSYC_channel_end (struct GNUNET_PSYC_Channel *channel);
300
301
302 /**
303  * Handle to access PSYC group operations for all members.
304  */
305 struct GNUNET_PSYC_Group;
306
307
308 /**
309  * Convert 'channel' to a 'group' handle to access the 'group' APIs.
310  * 
311  * @param channel channel handle
312  * @return group handle, valid for as long as 'channel' is valid
313  */ 
314 struct GNUNET_PSYC_Group *
315 GNUNET_PSYC_channel_get_group (struct GNUNET_PSYC_Channel *channel);
316
317
318 /**
319  * Convert 'member' to a 'group' handle to access the 'group' APIs.
320  * 
321  * @param member membership handle
322  * @return group handle, valid for as long as 'member' is valid
323  */ 
324 struct GNUNET_PSYC_Group *
325 GNUNET_PSYC_member_get_group (struct GNUNET_PSYC_Member *member);
326
327
328 /**
329  * Add a member to the group.    Note that this will NOT generate any
330  * PSYC traffic, it will merely update the local data base to modify
331  * how we react to 'membership test' queries.  The channel still needs to
332  * explicitly transmit a 'join' message to notify other group members
333  * and they then also must still call this function in their respective
334  * methods handling the 'join' message.  This way, how 'join' and 'leave'
335  * operations are exactly implemented is still up to the application;
336  * for example, there might be a 'leave_all' method to kick out everyone.
337  *
338  * Note that group members are explicitly trusted to execute such 
339  * methods correctly; not doing so correctly will result in either
340  * denying members access or offering access to group data to
341  * non-members.
342  *
343  * @param group group handle
344  * @param member which peer to add
345  * @param message_id message ID for the message that changed the membership
346  * @param group_generation the generation ID where the change went into effect
347  */
348 void
349 GNUNET_PSYC_group_member_admit (struct GNUNET_PSYC_Group *group,
350                                 const struct GNUNET_PeerIdentity *member,
351                                 uint64_t message_id,
352                                 uint64_t group_generation);
353
354
355 /**
356  * Remove a member from the group.  Note that this will NOT generate any
357  * PSYC traffic, it will merely update the local data base to modify
358  * how we react to 'membership test' queries.  The channel still needs to
359  * explicitly transmit a 'leave' message to notify other group members
360  * and they then also must still call this function in their respective
361  * methods handling the 'leave' message.  This way, how 'join' and 'leave'
362  * operations are exactly implemented is still up to the application;
363  * for example, there might be a 'leave_all' message to kick out everyone.
364  *
365  * Note that group members are explicitly trusted to perform these
366  * operations correctly; not doing so correctly will result in either
367  * denying members access or offering access to group data to
368  * non-members.
369  *
370  * @param group group handle
371  * @param member which peer to remove
372  * @param message_id message ID for the message that changed the membership
373  * @param group_generation the generation ID where the change went into effect
374  */
375 void
376 GNUNET_PSYC_group_member_kick (struct GNUNET_PSYC_Group *group,
377                                const struct GNUNET_PeerIdentity *member,
378                                uint64_t message_id,
379                                uint64_t group_generation);
380
381
382 /**
383  * Function called to inform a member about state changes for a
384  * channel.  Note that (for sets) only the delta is communicated, not
385  * the full state.
386  *
387  * @param cls closure
388  * @param full_state_name full name of the state
389  * @param type how to interpret the change
390  * @param state_value information about the new state
391  * @param state_value_size number of bytes in 'state_value'
392  */
393 typedef void (*GNUNET_PSYC_StateCallback)(void *cls,
394                                           const char *full_state_name,
395                                           enum GNUNET_PSYC_Operator type,
396                                           const void *state_value,
397                                           size_t state_value_size);
398
399
400 /**
401  * Descriptor for an event handler handling PSYC state updates.
402  */
403 struct GNUNET_PSYC_StateHandler
404 {
405
406   /**
407    * Name of the state this handler calls about, used in try-and-slice matching.
408    */
409   const char *state_name;
410
411   /**
412    * Function to call whenever the respective state changes.
413    */
414   GNUNET_PSYC_StateCallback event_handler;
415
416   /**
417    * Closure for the 'event_handler' function.
418    */
419   void *event_handler_cls;
420
421 };
422
423
424 /**
425  * Join a PSYC group.  The entity joining is always the local peer.
426  * The user must immediately use the 'GNUNET_PSYC_member_send_to_host'
427  * (and possibly 'GNUNET_PSYC_member_host_variable_set') functions to
428  * transmit a 'join_msg' to the channel; if the join request succeeds,
429  * the channel state (and 'recent' method calls) will be replayed to
430  * the joining member.  There is no explicit notification on failure
431  * (as the channel may simply take days to approve, and disapproval is
432  * simply being ignored).
433  *
434  * @param cfg configuration to use
435  * @param pub_key ECC key that identifies the channel we wish to join
436  * @param method_count number of methods in 'methods' array
437  * @param methods functions to invoke on messages received from the channel,
438  *                typcially at least contains functions for 'join' and 'leave'.
439  * @param state_count number of state handlers
440  * @param state_handlers array of state event handlers
441  * @return handle for the member, NULL on error 
442  */
443 struct GNUNET_PSYC_Member *
444 GNUNET_PSYC_member_join (const struct GNUNET_CONFIGURATION_Handle *cfg, 
445                          const struct GNUNET_CRYPTO_EccPublicKey *pub_key,
446                          unsigned int method_count,
447                          const struct GNUNET_PSYC_Method *methods,
448                          unsigned int state_count,
449                          struct GNUNET_PSYC_StateHandler *state_handlers);
450
451
452 /**
453  * Leave a multicast group.  Will terminate the connection to the PSYC
454  * service.  Polite clients should first explicitly send a 'leave'
455  * request (via 'GNUNET_PSYC_member_send_to_host').  
456  *
457  * @param member membership handle
458  */
459 void
460 GNUNET_PSYC_member_leave (struct GNUNET_PSYC_Member *member);
461
462
463 /**
464  * Function called to provide data for a transmission to the channel
465  * owner (aka the 'host' of the channel).  Note that returning
466  * GNUNET_OK or GNUNET_SYSERR (but not GNUNET_NO) invalidates the
467  * respective transmission handle.
468  *
469  * @param cls closure
470  * @param data_size initially set to the number of bytes available in 'data',
471  *        should be set to the number of bytes written to data (IN/OUT)
472  * @param data where to write the body of the message to give to the method;
473  *        function must copy at most '*data_size' bytes to 'data'.
474  * @return GNUNET_SYSERR on error (fatal, aborts transmission)
475  *         GNUNET_NO on success, if more data is to be transmitted later
476  *         GNUNET_OK if this completes the transmission (all data supplied)
477  */
478 typedef int (*GNUNET_PSYC_HostReadyNotify)(void *cls,
479                                            size_t *data_size,
480                                            char *data);
481
482
483 /**
484  * Handle for a pending PSYC transmission operation.
485  */
486 struct GNUNET_PSYC_HostTransmitHandle;
487
488
489 /**
490  * Request a message to be send to the channel host.
491  *
492  * @param member membership handle
493  * @param method_name which (PSYC) method should be invoked (on host)
494  * @param notify function to call when we are allowed to transmit (to get data)
495  * @param notify_cls closure for 'notify'
496  * @return transmission handle, NULL on error (i.e. more than one request queued)
497  */
498 struct GNUNET_PSYC_HostTransmitHandle *
499 GNUNET_PSYC_member_send_to_host (struct GNUNET_PSYC_Member *member,
500                                  const char *method_name,
501                                  GNUNET_PSYC_HostReadyNotify notify,
502                                  void *notify_cls);
503
504
505 /**
506  * Set a (temporary, ":") variable for the next message being transmitted
507  * via 'GNUNET_PSYC_member_send_to_host'. If 'GNUNET_PSYC_member_send_to_host'
508  * is called and then cancelled, all variables that were set using this
509  * function will be unset (lost/forgotten).  To clear a variable state after
510  * setting it, you can also call this function again with NULL/0 for the value.
511  *
512  * @param member membership handle
513  * @param variable_name name of the variable to set
514  * @param value value to set for the given variable
515  * @param value_size number of bytes in 'value'
516  */
517 uint64_t
518 GNUNET_PSYC_member_host_variable_set (struct GNUNET_PSYC_Member *member,
519                                       const char *variable_name,
520                                       const void *value,
521                                       size_t value_size);
522
523
524 /**
525  * Abort transmission request to host.
526  *
527  * @param th handle of the request that is being aborted
528  */
529 void
530 GNUNET_PSYC_member_send_to_host_cancel (struct GNUNET_PSYC_HostTransmitHandle *th);
531
532
533 /**
534  * Handle to a story telling operation.
535  */
536 struct GNUNET_PSYC_Story;
537
538
539 /**
540  * Request to be told the message history of the channel.  Historic
541  * messages (but NOT the state at the time) will be replayed (given to
542  * the normal method handlers) if available and if access is
543  * permitted.
544  *
545  * @param member which channel should be replayed?
546  * @param start earliest interesting point in history
547  * @param end last (exclusive) interesting point in history
548  * @param finish_cb function to call when the requested story has been fully 
549  *        told (counting message IDs might not suffice, as some messages
550  *        might be secret and thus the listener would not know the story is 
551  *        finished without being told explicitly); once this function
552  *        has been called, the client must not call
553  *        'GNUNET_PSYC_member_story_tell_cancel' anymore
554  * @param finish_cb_cls closure to finish_cb
555  * @return handle to cancel story telling operation
556  */
557 struct GNUNET_PSYC_Story *
558 GNUNET_PSYC_member_story_tell (struct GNUNET_PSYC_Member *member,
559                                uint64_t start,
560                                uint64_t end,
561                                void (*finish_cb)(void *),
562                                void *finish_cb_cls);
563
564
565 /**
566  * Abort story telling.  This function must not be called from within
567  * method handlers (as given to 'GNUNET_PSYC_member_join') of the
568  * member.
569  *
570  * @param story story telling operation to stop
571  */
572 void
573 GNUNET_PSYC_member_story_tell_cancel (struct GNUNET_PSYC_Story *story);
574
575
576 /**
577  * Call the given callback on all matching values (including
578  * variables) in the channel state.  The callback is invoked
579  * synchronously on all matching states (as the state is fully
580  * replicated in the library in this process; channel states should be
581  * small, large data is to be passed as streaming data to methods).
582  *
583  * A name matches if it includes the 'state_name' prefix, thus
584  * requesting the empty state ("") will match all values; requesting
585  * "a_b" will also return values stored under "a_b_c".
586  *
587  * @param member membership handle
588  * @param state_name name of the state to query (full name 
589  *        might be longer, this is only the prefix that must match)
590  * @param cb function to call on the matching state values
591  * @param cb_cls closure for 'cb'
592  * @return message ID for which the state was returned (last seen
593  *         message ID)
594  */
595 uint64_t
596 GNUNET_PSYC_member_state_get_all (struct GNUNET_PSYC_Member *member,
597                                   const char *state_name,
598                                   GNUNET_PSYC_StateCallback cb,
599                                   void *cb_cls);
600
601
602 /**
603  * Obtain the current value of the best-matching value in the state
604  * (including variables).  Note that variables are only valid during a
605  * GNUNET_PSYC_Method invocation, as variables are only valid for the
606  * duration of a method invocation.  
607  *
608  * If the requested variable name does not have an exact state in
609  * the state, the nearest less-specific name is matched; for example,
610  * requesting "a_b" will match "a" if "a_b" does not exist.
611  *
612  * @param member membership handle
613  * @param variable_name name of the variable to query 
614  * @param return_value_size set to number of bytes in variable, 
615  *        needed as variables might contain binary data and
616  *        might also not be 0-terminated; set to 0 on errors
617  * @return NULL on error (no matching state or variable), pointer
618           to the respective value otherwise
619  */
620 const void *
621 GNUNET_PSYC_member_state_get (struct GNUNET_PSYC_Member *member,
622                               const char *variable_name,
623                               size_t *return_value_size);
624
625
626
627 #if 0                           /* keep Emacsens' auto-indent happy */
628 {
629 #endif
630 #ifdef __cplusplus
631 }
632 #endif
633
634 /* ifndef GNUNET_PSYC_SERVICE_H */
635 #endif
636 /* end of gnunet_psyc_service.h */