multicast/psyc: no part cb needed
[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  * @author Gabor X Toth
32  *
33  * NOTE:
34  * - this API does not know about psyc's "root" and "places";
35  *   there is no 'root' in GNUnet-Psyc as we're decentralized;
36  *   'places' and 'persons' are combined within the same
37  *   abstraction, that of a "channel".  Channels are identified
38  *   and accessed in this API using a public/private key.
39  *   Higher-level applications should use NAMES within GADS
40  *   to obtain public keys, and the distinction between
41  *   'places' and 'persons' can then be made with the help
42  *   of the naming system (and/or conventions).
43  *   Channels are (as in PSYC) organized into a hierarchy; each
44  *   channel master (the one with the private key) is then
45  *   the operator of the multicast group (its Origin in
46  *   the terminology of the multicast API).
47  * - The API supports passing large amounts of data using
48  *   'streaming' for the argument passed to a method.  State
49  *   and variables must fit into memory and cannot be streamed
50  *   (thus, no passing of 4 GB of data in a variable;
51  *   once we implement this, we might want to create a
52  *   @c \#define for the maximum size of a variable).
53  * - PSYC defines standard variables, methods, etc.  This
54  *   library deliberately abstracts over all of these; a
55  *   higher-level API should combine the naming system (GADS)
56  *   and standard methods (message, join, part, warn,
57  *   fail, error) and variables (action, color, time,
58  *   tag, etc.).  However, this API does take over the
59  *   routing variables, specifically 'context' (channel),
60  *   and 'source'.  We only kind-of support 'target', as
61  *   the target is either everyone in the group or the
62  *   origin, and never just a single member of the group;
63  *   for such individual messages, an application needs to
64  *   construct an 'inbox' channel where the master (only)
65  *   receives messages (but never forwards; private responses
66  *   would be transmitted by joining the senders 'inbox'
67  *   channel -- or a inbox#bob subchannel).  The
68  *   goal for all of this is to keep the abstractions in this
69  *   API minimal: interaction with multicast, try \& slice,
70  *   state/variable/channel management.  Higher-level
71  *   operations belong elsewhere (so maybe this API should
72  *   be called 'PSYC-low', whereas a higher-level API
73  *   implementing defaults for standard methods and
74  *   variables might be called 'PSYC-std' or 'PSYC-high'.
75  */
76
77 #ifndef GNUNET_PSYC_SERVICE_H
78 #define GNUNET_PSYC_SERVICE_H
79
80 #ifdef __cplusplus
81 extern "C"
82 {
83 #if 0                           /* keep Emacsens' auto-indent happy */
84 }
85 #endif
86 #endif
87
88 #include "gnunet_util_lib.h"
89 #include "gnunet_psyc_lib.h"
90 #include "gnunet_multicast_service.h"
91
92
93 /** 
94  * Version number of GNUnet-PSYC API.
95  */
96 #define GNUNET_PSYC_VERSION 0x00000000
97
98
99 enum GNUNET_PSYC_MessageFlags
100 {
101   /**
102    * First fragment of a message.
103    */
104   GNUNET_PSYC_MESSAGE_FIRST_FRAGMENT = 1 << 0,
105
106   /**
107    * Last fragment of a message.
108    */
109   GNUNET_PSYC_MESSAGE_LAST_FRAGMENT = 1 << 1,
110
111   /** 
112    * OR'ed flags if message is not fragmented.
113    */
114   GNUNET_PSYC_MESSAGE_NOT_FRAGMENTED
115     = GNUNET_PSYC_MESSAGE_FIRST_FRAGMENT
116     | GNUNET_PSYC_MESSAGE_LAST_FRAGMENT,
117
118   /**
119    * Historic message, retrieved from PSYCstore.
120    */
121   GNUNET_PSYC_MESSAGE_HISTORIC = 1 << 30
122 };
123
124
125 /** 
126  * Handle that identifies a join request.
127  *
128  * Used to match calls to #GNUNET_PSYC_JoinCallback to the
129  * corresponding calls to GNUNET_PSYC_join_decision().
130  */
131 struct GNUNET_PSYC_JoinHandle;
132
133
134 /** 
135  * Method called from PSYC upon receiving a message indicating a call
136  * to a @e method.
137  *
138  * @param cls Closure.
139  * @param sender Who transmitted the message (master, except for messages
140  *        from one of the slaves to the master).
141  * @param message_id Unique message counter for this message;
142  *                   (unique only in combination with the given sender for
143  *                    this channel).
144  * @param method_name Original method name from PSYC (may be more
145  *        specific than the registered method name due to try-and-slice matching).
146  *        FIXME: no try-and-slice for methods defined here.
147  * @param header_length Number of modifiers in header.
148  * @param header Modifiers present in the message. FIXME: use environment instead?
149  * @param data_offset Byte offset of @a data in the overall data of the method.
150  * @param data_size Number of bytes in @a data.
151  * @param data Data stream given to the method (might not be zero-terminated
152  *             if data is binary).
153  * @param frag Fragmentation status for the data.
154  */
155 typedef int (*GNUNET_PSYC_Method)(void *cls,
156                                   const struct GNUNET_PeerIdentity *sender,
157                                   uint64_t message_id,
158                                   const char *method_name,
159                                   size_t header_length,
160                                   GNUNET_PSYC_Modifier *header,
161                                   uint64_t data_offset,
162                                   size_t data_size,
163                                   const void *data,
164                                   enum GNUNET_PSYC_MessageFlags flags);
165
166
167 /** 
168  * Method called from PSYC upon receiving a join request.
169  *
170  * @param cls Closure.
171  * @param peer Peer requesting to join.
172  * @param method_name Method name in the join request.
173  * @param header_length Number of modifiers in header.
174  * @param header Modifiers present in the message.
175  * @param data_size Number of bytes in @a data.
176  * @param data Data stream given to the method (might not be zero-terminated
177  *             if data is binary).
178  */
179 typedef int (*GNUNET_PSYC_JoinCallback)(void *cls,
180                                         const struct GNUNET_PeerIdentity *peer,
181                                         const char *method_name,
182                                         size_t header_length,
183                                         GNUNET_PSYC_Modifier *header,
184                                         size_t data_size,
185                                         const void *data,
186                                         struct GNUNET_PSYC_JoinHandle *jh);
187
188
189 /** 
190  * Function to call with the decision made for a join request.
191  *
192  * Must be called once and only once in response to an invocation of the
193  * #GNUNET_PSYC_JoinCallback.
194  *
195  * @param jh Join request handle.
196  * @param is_admitted #GNUNET_YES if joining is approved,
197  *        #GNUNET_NO if it is disapproved
198  * @param method_name Method name for the message transmitted with the response.
199  * @param env Environment containing transient variables for the message, or NULL.
200  * @param data_size Size of @a data.
201  * @param data Data of the message.
202  */
203 void
204 GNUNET_PSYC_join_decision (struct GNUNET_PSYC_JoinHandle *jh,
205                            int is_admitted,
206                            const char *method_name,
207                            const struct GNUNET_ENV_Environment *env,
208                            size_t data_size,
209                            const void *data);
210
211
212 /** 
213  * Handle for the master of a PSYC channel.
214  */
215 struct GNUNET_PSYC_Master;
216
217
218 /** 
219  * Start a PSYC master channel.
220  *
221  * Will start a multicast group identified by the given ECC key.  Messages
222  * received from group members will be given to the respective handler methods.
223  * If a new member wants to join a group, the "join" method handler will be
224  * invoked; the join handler must then generate a "join" message to approve the
225  * joining of the new member.  The channel can also change group membership
226  * without explicit requests.  Note that PSYC doesn't itself "understand" join
227  * or part messages, the respective methods must call other PSYC functions to
228  * inform PSYC about the meaning of the respective events.
229  *
230  * @param cfg Configuration to use (to connect to PSYC service).
231  * @param priv_key ECC key that will be used to sign messages for this
232  *                 PSYC session; public key is used to identify the
233  *                 PSYC channel; FIXME: we'll likely want to use
234  *                 NOT the p512 curve here, but a cheaper one in the future
235  *                 Note that end-users will usually not use the private key
236  *                 directly, but rather look it up in GADS for places
237  *                 managed by other users, or select a file with the private
238  *                 key(s) when setting up their own channels
239  * @param join_policy What is the membership policy of the group?
240  *                 Used to automate group management decisions.
241  * @param method_cb Function to invoke on messages received from members.
242  * @param join_cb Function to invoke when a peer wants to join.
243  * @param cls Closure for the callbacks.
244  * @return Handle for the channel master, NULL on error.
245  */
246 struct GNUNET_PSYC_Master *
247 GNUNET_PSYC_master_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
248                           const struct GNUNET_CRYPTO_EccPrivateKey *priv_key,
249                           enum GNUNET_MULTICAST_JoinPolicy join_policy,
250                           GNUNET_PSYC_Method method_cb,
251                           GNUNET_PSYC_JoinCallback join_cb,
252                           void *cls);
253
254
255 /** 
256  * Function called to provide data for a transmission via PSYC.
257  *
258  * Note that returning #GNUNET_OK or #GNUNET_SYSERR (but not #GNUNET_NO)
259  * invalidates the respective transmission handle.
260  *
261  * @param cls Closure.
262  * @param message_id Set to the unique message ID that was generated for
263  *        this message.
264  * @param[in,out] data_size Initially set to the number of bytes available in @a data,
265  *        should be set to the number of bytes written to data (IN/OUT).
266  * @param[out] data Where to write the body of the message to give to the method;
267  *        function must copy at most @a *data_size bytes to @a data.
268  * @return #GNUNET_SYSERR on error (fatal, aborts transmission)
269  *         #GNUNET_NO on success, if more data is to be transmitted later
270  *         (should be used if @a *data_size was not big enough to take all the data)
271  *         #GNUNET_YES if this completes the transmission (all data supplied)
272  */
273 typedef int (*GNUNET_PSYC_MasterReadyNotify)(void *cls,
274                                              uint64_t message_id,
275                                              size_t *data_size,
276                                              void *data);
277
278
279 /** 
280  * Handle for a pending PSYC transmission operation.
281  */
282 struct GNUNET_PSYC_MasterTransmitHandle;
283
284
285 /** 
286  * Send a message to call a method to all members in the PSYC channel.
287  *
288  * @param master Handle to the PSYC channel.
289  * @param increment_group_generation #GNUNET_YES if we need to increment
290  *        the group generation counter after transmitting this message.
291  * @param method_name Which method should be invoked.
292  * @param env Environment containing state operations and transient variables
293  *            for the message, or NULL.
294  * @param notify Function to call to obtain the arguments.
295  * @param notify_cls Closure for @a notify.
296  * @return Transmission handle, NULL on error (i.e. more than one request queued).
297  */
298 struct GNUNET_PSYC_MasterTransmitHandle *
299 GNUNET_PSYC_master_transmit (struct GNUNET_PSYC_Master *master,
300                              int increment_group_generation,
301                              const char *method_name,
302                              const struct GNUNET_ENV_Environment *env,
303                              GNUNET_PSYC_MasterReadyNotify notify,
304                              void *notify_cls);
305
306
307 /** 
308  * Abort transmission request to channel.
309  *
310  * @param th Handle of the request that is being aborted.
311  */
312 void
313 GNUNET_PSYC_master_transmit_cancel (struct GNUNET_PSYC_MasterTransmitHandle *th);
314
315
316 /** 
317  * Stop a PSYC master channel.
318  *
319  * @param master PSYC channel master to stop.
320  */
321 tvoid
322 GNUNET_PSYC_master_stop (struct GNUNET_PSYC_Master *master);
323
324
325 /** 
326  * Handle for a PSYC channel slave.
327  */
328 struct GNUNET_PSYC_Slave;
329
330
331 /** 
332  * Join a PSYC channel.
333  *
334  * The entity joining is always the local peer.  The user must immediately use
335  * the GNUNET_PSYC_slave_to_master() functions to transmit a @e join_msg to the
336  * channel; if the join request succeeds, the channel state (and @e recent
337  * method calls) will be replayed to the joining member.  There is no explicit
338  * notification on failure (as the channel may simply take days to approve,
339  * and disapproval is simply being ignored).
340  *
341  * @param cfg Configuration to use.
342  * @param pub_key ECC key that identifies the channel we wish to join.
343  * @param origin Peer identity of the origin.
344  * @param method Function to invoke on messages received from the channel,
345  *                typically at least contains functions for @e join and @e part.
346  * @param method_cls Closure for @a method.
347  * @param method_name Method name for the join request.
348  * @param env Environment containing transient variables for the request, or NULL.
349  * @param data_size Number of bytes in @a data.
350  * @param data Payload for the join message.
351  * @return Handle for the slave, NULL on error.
352  */
353 struct GNUNET_PSYC_Slave *
354 GNUNET_PSYC_slave_join (const struct GNUNET_CONFIGURATION_Handle *cfg,
355                         const struct GNUNET_CRYPTO_EccPublicKey *pub_key,
356                         const struct GNUNET_PeerIdentity *origin,
357                         GNUNET_PSYC_Method method,
358                         void *method_cls,
359                         const char *method_name,
360                         const struct GNUNET_ENV_Environment *env,
361                         size_t data_size,
362                         const void *data);
363
364
365 /** 
366  * Part a PSYC channel.
367  *
368  * Will terminate the connection to the PSYC service.  Polite clients should
369  * first explicitly send a @e part request (via GNUNET_PSYC_slave_to_master()).
370  *
371  * @param slave Slave handle.
372  */
373 void
374 GNUNET_PSYC_slave_part (struct GNUNET_PSYC_Slave *slave);
375
376
377 /** 
378  * Function called to provide data for a transmission to the channel
379  * master (aka the @e host of the channel).
380  *
381  * Note that returning #GNUNET_OK or #GNUNET_SYSERR (but not #GNUNET_NO)
382  * invalidates the respective transmission handle.
383  *
384  * @param cls Closure.
385  * @param[in,out] data_size Initially set to the number of bytes available in @a data,
386  *        should be set to the number of bytes written to data (IN/OUT).
387  * @param[out] data Where to write the body of the message to give to the method;
388  *        function must copy at most @a *data_size bytes to @a data.
389  * @return #GNUNET_SYSERR on error (fatal, aborts transmission).
390  *         #GNUNET_NO on success, if more data is to be transmitted later.
391  *         #GNUNET_YES if this completes the transmission (all data supplied).
392  */
393 typedef int (*GNUNET_PSYC_SlaveReadyNotify)(void *cls,
394                                             size_t *data_size,
395                                             char *data);
396
397
398 /** 
399  * Handle for a pending PSYC transmission operation.
400  */
401 struct GNUNET_PSYC_SlaveTransmitHandle;
402
403
404 /** 
405  * Request a message to be sent to the channel master.
406  *
407  * @param slave Slave handle.
408  * @param method_name Which (PSYC) method should be invoked (on host).
409  * @param env Environment containing transient variables for the message, or NULL.
410  * @param notify Function to call when we are allowed to transmit (to get data).
411  * @param notify_cls Closure for @a notify.
412  * @return Transmission handle, NULL on error (i.e. more than one request queued).
413  */
414 struct GNUNET_PSYC_SlaveTransmitHandle *
415 GNUNET_PSYC_slave_transmit (struct GNUNET_PSYC_Slave *slave,
416                             const char *method_name,
417                             const struct GNUNET_ENV_Environment *env,
418                             GNUNET_PSYC_SlaveReadyNotify notify,
419                             void *notify_cls);
420
421
422 /** 
423  * Abort transmission request to master.
424  *
425  * @param th Handle of the request that is being aborted.
426  */
427 void
428 GNUNET_PSYC_slave_transmit_cancel (struct GNUNET_PSYC_SlaveTransmitHandle *th);
429
430
431 /** 
432  * Handle to access PSYC channel operations for both the master and slaves.
433  */
434 struct GNUNET_PSYC_Channel;
435
436
437 /** 
438  * Convert a channel @a master to a @e channel handle to access the @e channel APIs.
439  *
440  * @param master Channel master handle.
441  * @return Channel handle, valid for as long as @a master is valid.
442  */
443 struct GNUNET_PSYC_Channel *
444 GNUNET_PSYC_master_get_channel (struct GNUNET_PSYC_Master *master);
445
446
447 /** 
448  * Convert @a slave to a @e channel handle to access the @e channel APIs.
449  *
450  * @param slave Slave handle.
451  * @return Channel handle, valid for as long as @a slave is valid.
452  */
453 struct GNUNET_PSYC_Channel *
454 GNUNET_PSYC_slave_get_channel (struct GNUNET_PSYC_Slave *slave);
455
456
457 /** 
458  * Add a member to the channel.
459  *
460  * Note that this will NOT generate any PSYC traffic, it will merely update the
461  * local data base to modify how we react to <em>membership test</em> queries.
462  * The channel master still needs to explicitly transmit a @e join message to
463  * notify other channel members and they then also must still call this function
464  * in their respective methods handling the @e join message.  This way, how @e
465  * join and @e part operations are exactly implemented is still up to the
466  * application; for example, there might be a @e part_all method to kick out
467  * everyone.
468  *
469  * Note that channel members are explicitly trusted to execute such methods
470  * correctly; not doing so correctly will result in either denying members
471  * access or offering access to channel data to non-members.
472  *
473  * @param channel Channel handle.
474  * @param member Which peer to add.
475  * @param message_id Message ID for the message that changed the membership.
476  */
477 void
478 GNUNET_PSYC_channel_member_add (struct GNUNET_PSYC_Channel *channel,
479                                 const struct GNUNET_PeerIdentity *member,
480                                 uint64_t message_id);
481
482
483 /** 
484  * Remove a member from the channel.
485  *
486  * Note that this will NOT generate any PSYC traffic, it will merely update the
487  * local data base to modify how we react to <em>membership test</em> queries.
488  * The channel master still needs to explicitly transmit a @e part message to
489  * notify other channel members and they then also must still call this function
490  * in their respective methods handling the @e part message.  This way, how
491  * @e join and @e part operations are exactly implemented is still up to the
492  * application; for example, there might be a @e part_all message to kick out
493  * everyone.
494  *
495  * Note that channel members are explicitly trusted to perform these
496  * operations correctly; not doing so correctly will result in either
497  * denying members access or offering access to channel data to
498  * non-members.
499  *
500  * @param channel Channel handle.
501  * @param member Which peer to remove.
502  * @param message_id Message ID for the message that changed the membership.
503  */
504 void
505 GNUNET_PSYC_channel_member_remove (struct GNUNET_PSYC_Channel *channel,
506                                    const struct GNUNET_PeerIdentity *member,
507                                    uint64_t message_id);
508
509
510 /** 
511  * Function called to inform a member about stored state values for a channel.
512  *
513  * @param cls Closure.
514  * @param name Name of the state variable.
515  * @param value Value of the state variable.
516  * @param value_size Number of bytes in @a value.
517  */
518 typedef void (*GNUNET_PSYC_StateCallback)(void *cls,
519                                           const char *name,
520                                           size_t value_size,
521                                           const void *value);
522
523
524 /** 
525  * Handle to a story telling operation.
526  */
527 struct GNUNET_PSYC_Story;
528
529
530 /** 
531  * Request to be told the message history of the channel.
532  *
533  * Historic messages (but NOT the state at the time) will be replayed (given to
534  * the normal method handlers) if available and if access is permitted.
535  *
536  * To get the latest message, use 0 for both the start and end message ID.
537  *
538  * @param channel Which channel should be replayed?
539  * @param start_message_id Earliest interesting point in history.
540  * @param end_message_id Last (exclusive) interesting point in history.
541  * @param method Function to invoke on messages received from the story.
542  * @param method_cls Closure for @a method.
543  * @param finish_cb Function to call when the requested story has been fully
544  *        told (counting message IDs might not suffice, as some messages
545  *        might be secret and thus the listener would not know the story is
546  *        finished without being told explicitly); once this function
547  *        has been called, the client must not call
548  *        GNUNET_PSYC_channel_story_tell_cancel() anymore.
549  * @param finish_cb_cls Closure to finish_cb.
550  * @return Handle to cancel story telling operation.
551  */
552 struct GNUNET_PSYC_Story *
553 GNUNET_PSYC_channel_story_tell (struct GNUNET_PSYC_Channel *channel,
554                                 uint64_t start_message_id,
555                                 uint64_t end_message_id,
556                                 GNUNET_PSYC_Method method,
557                                 void *method_cls,
558                                 void (*finish_cb)(void *),
559                                 void *finish_cb_cls);
560
561
562 /** 
563  * Abort story telling.
564  *
565  * This function must not be called from within method handlers (as given to
566  * GNUNET_PSYC_slave_join()) of the slave.
567  *
568  * @param story Story telling operation to stop.
569  */
570 void
571 GNUNET_PSYC_channel_story_tell_cancel (struct GNUNET_PSYC_Story *story);
572
573
574 /** 
575  * Call the given callback on all matching values (including variables) in the
576  * channel state.
577  *
578  * The callback is invoked synchronously on all matching states (as the state is
579  * fully replicated in the library in this process; channel states should be
580  * small, large data is to be passed as streaming data to methods).
581  *
582  * A name matches if it includes the @a state_name prefix, thus requesting the
583  * empty state ("") will match all values; requesting "_a_b" will also return
584  * values stored under "_a_b_c".
585  *
586  * @param channel Channel handle.
587  * @param state_name Name of the state to query (full name
588  *        might be longer, this is only the prefix that must match).
589  * @param cb Function to call on the matching state values.
590  * @param cb_cls Closure for @a cb.
591  * @return Message ID for which the state was returned (last seen
592  *         message ID).
593  */
594 uint64_t
595 GNUNET_PSYC_channel_state_get_all (struct GNUNET_PSYC_Channel *channel,
596                                    const char *state_name,
597                                    GNUNET_PSYC_StateCallback cb,
598                                    void *cb_cls);
599
600
601 /** 
602  * Obtain the current value of the best-matching value in the state
603  * (including variables).
604  *
605  * Note that variables are only valid during a #GNUNET_PSYC_Method invocation, as
606  * variables are only valid for the 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 channel Channel handle.
613  * @param variable_name Name of the variable to query.
614  * @param[out] 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_channel_state_get (struct GNUNET_PSYC_Channel *channel,
622                                const char *variable_name,
623                                size_t *return_value_size);
624
625
626 #if 0                           /* keep Emacsens' auto-indent happy */
627 {
628 #endif
629 #ifdef __cplusplus
630 }
631 #endif
632
633 /* ifndef GNUNET_PSYC_SERVICE_H */
634 #endif
635 /* end of gnunet_psyc_service.h */