2 This file is part of GNUnet.
3 (C) 2012, 2013 Christian Grothoff (and other contributing authors)
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.
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.
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.
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
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 owner (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 owner (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'.
77 * - rename "channel" to "master"
78 * - rename "member" to "slave"
79 * - rename "group" to "channel"
82 #ifndef GNUNET_PSYC_SERVICE_H
83 #define GNUNET_PSYC_SERVICE_H
88 #if 0 /* keep Emacsens' auto-indent happy */
93 #include "gnunet_util_lib.h"
94 #include "gnunet_psyc_lib.h"
95 #include "gnunet_multicast_service.h"
99 * Version number of GNUnet-PSYC API.
101 #define GNUNET_PSYC_VERSION 0x00000000
105 * Information flags for data fragments set via PSYC.
107 enum GNUNET_PSYC_FragmentStatus
110 * This is the first part of data for the given method call.
112 GNUNET_PSYC_FS_FIRST = 1,
115 * This is the last part of data for the given method call.
117 GNUNET_PSYC_FS_LAST = 2,
120 * OR'ed flags if payload is not fragmented.
122 GNUNET_PSYC_FS_NOT_FRAGMENTED = (GNUNET_PSYC_FS_FIRST | GNUNET_PSYC_FS_LAST)
127 * Handle that identifies a join request.
129 * Used to match calls to #GNUNET_PSYC_JoinCallback to the
130 * corresponding calls to GNUNET_PSYC_join_decision().
132 struct GNUNET_PSYC_JoinHandle;
135 * Handle that identifies a part request.
137 * Used to match calls to #GNUNET_PSYC_PartCallback to the
138 * corresponding calls to GNUNET_PSYC_part_ack().
140 struct GNUNET_PSYC_PartHandle;
144 * Method called from PSYC upon receiving a message indicating a call
147 * @param cls Closure.
148 * @param sender Who transmitted the message (origin, except for messages
149 * from one of the members to the origin).
150 * @param message_id Unique message counter for this message;
151 * (unique only in combination with the given sender for
153 * @param group_generation Group generation counter for this message
154 * (always zero for messages from members to channel owner); FIXME: needed?
155 * @param method_name Original method name from PSYC (may be more
156 * specific than the registered method name due to try-and-slice matching).
157 * FIXME: no try-and-slice for methods defined here.
158 * @param data_off Byte offset of @a data in the overall data of the method.
159 * @param data_size Number of bytes in @a data.
160 * @param data Data stream given to the method (might not be zero-terminated
161 * if data is binary).
162 * @param frag Fragmentation status for the data.
164 typedef int (*GNUNET_PSYC_Method)(void *cls,
165 const struct GNUNET_PeerIdentity *sender,
167 uint64_t group_generation,
168 const char *method_name,
172 enum GNUNET_PSYC_FragmentStatus frag);
176 * Method called from PSYC upon receiving a join request.
178 * @param cls Closure.
179 * @param sender Who transmitted the message.
180 * @param method_name Method name in the join request.
181 * @param data_size Number of bytes in @a data.
182 * @param data Data stream given to the method (might not be zero-terminated
183 * if data is binary).
185 typedef int (*GNUNET_PSYC_JoinCallback)(void *cls,
186 const struct GNUNET_PeerIdentity *sender,
187 const char *method_name,
190 struct GNUNET_PSYC_JoinHandle *jh);
194 * Method called from PSYC upon receiving a part request.
196 * @param cls Closure.
197 * @param sender Who transmitted the message.
198 * @param method_name Method name in the part request.
199 * @param data_size Number of bytes in @a data.
200 * @param data Data stream given to the method (might not be zero-terminated
201 * if data is binary).
203 typedef int (*GNUNET_PSYC_PartCallback)(void *cls,
204 const struct GNUNET_PeerIdentity *sender,
205 const char *method_name,
208 struct GNUNET_PSYC_PartHandle *ph);
212 * Function to call with the decision made for a join request.
214 * Must be called once and only once in response to an invocation of the
215 * #GNUNET_PSYC_JoinCallback.
217 * @param jh Join request handle.
218 * @param is_admitted #GNUNET_YES if joining is approved,
219 * #GNUNET_NO if it is disapproved
220 * @param method_name Method name for the message transmitted with the response.
221 * @param data_size Size of @a data.
222 * @param data Data of the message.
225 GNUNET_PSYC_join_decision (struct GNUNET_PSYC_JoinHandle *jh,
227 const char *method_name,
233 * Send a part acknowledgment.
235 * @param ph Part handle.
238 GNUNET_PSYC_part_ack (struct GNUNET_PSYC_PartHandle *ph);
242 * Handle for the channel of a PSYC group.
244 struct GNUNET_PSYC_Channel;
248 * Create a PSYC channel.
250 * Will create a multicast group identified by the given ECC key. Messages
251 * received from group members will be given to the respective handler methods.
252 * If a new member wants to join a group, the "join" method handler will be
253 * invoked; the join handler must then generate a "join" message to approve the
254 * joining of the new member. The channel can also change group membership
255 * without explicit requests. Note that PSYC doesn't itself "understand" join
256 * or part messages, the respective methods must call other PSYC functions to
257 * inform PSYC about the meaning of the respective events.
259 * @param cfg Configuration to use (to connect to PSYC service).
260 * @param priv_key ECC key that will be used to sign messages for this
261 * PSYC session; public key is used to identify the
262 * PSYC group; FIXME: we'll likely want to use
263 * NOT the p521 curve here, but a cheaper one in the future
264 * Note that end-users will usually not use the private key
265 * directly, but rather look it up in GADS for groups
266 * managed by other users, or select a file with the private
267 * key(s) when setting up their own channels
268 * @param join_policy What is the membership policy of the group?
269 * Used to automate group management decisions.
270 * @param method_cb Function to invoke on messages received from members.
271 * @param join_cb Function to invoke when a peer wants to join.
272 * @param part_cb Function to invoke when a peer wants to part.
273 * @param cls Closure for the callbacks.
274 * @return Handle for the channel, NULL on error.
276 struct GNUNET_PSYC_Channel *
277 GNUNET_PSYC_channel_create (const struct GNUNET_CONFIGURATION_Handle *cfg,
278 const struct GNUNET_CRYPTO_EccPrivateKey *priv_key,
279 enum GNUNET_MULTICAST_JoinPolicy join_policy,
280 GNUNET_PSYC_Method method_cb,
281 GNUNET_PSYC_JoinCallback join_cb,
282 GNUNET_PSYC_PartCallback part_cb,
287 * Modify channel state (or set a transient variable).
289 * The state of a channel must fit into the memory of each member (and the
290 * channel); large values that require streaming must only be passed as the
291 * stream arguments to methods. State modifications might not be transmitted to
292 * group members until the next call to GNUNET_PSYC_channel_transmit().
293 * Variable updates must be given just before the call to the respective method
294 * that needs the variables.
296 * @param channel Handle to the PSYC group / channel.
297 * @param oper Kind of update operation (add, remove, replace, delete).
298 * @param name Name of the state or transient variable to modify.
299 * @param value_size Number of bytes in @a value.
300 * @param value Value of state variable.
301 * @return #GNUNET_OK on success, #GNUNET_SYSERR on internal error
302 * (i.e. state too large).
305 GNUNET_PSYC_channel_state_modify (struct GNUNET_PSYC_Channel *channel,
306 enum GNUNET_PSYC_Operator oper,
313 * Function called to provide data for a transmission via PSYC.
315 * Note that returning #GNUNET_OK or #GNUNET_SYSERR (but not #GNUNET_NO)
316 * invalidates the respective transmission handle.
318 * @param cls Closure.
319 * @param message_id Set to the unique message ID that was generated for
321 * @param group_generation Set to the group generation used for this
323 * @param data_size[in,out] Initially set to the number of bytes available in @a data,
324 * should be set to the number of bytes written to data (IN/OUT).
325 * @param[out] data Where to write the body of the message to give to the method;
326 * function must copy at most @a *data_size bytes to @a data.
327 * @return #GNUNET_SYSERR on error (fatal, aborts transmission)
328 * #GNUNET_NO on success, if more data is to be transmitted later
329 * (should be used if @a *data_size was not big enough to take all the data)
330 * #GNUNET_YES if this completes the transmission (all data supplied)
332 typedef int (*GNUNET_PSYC_ChannelReadyNotify)(void *cls,
334 uint64_t group_generation,
340 * Handle for a pending PSYC transmission operation.
342 struct GNUNET_PSYC_ChannelTransmitHandle;
346 * Send a message to call a method to all members in the PSYC channel.
348 * @param channel Handle to the PSYC multicast group.
349 * @param increment_group_generation #GNUNET_YES if we need to increment
350 * the group generation counter after transmitting this message.
351 * @param method_name Which method should be invoked.
352 * @param notify Function to call to obtain the arguments.
353 * @param notify_cls Closure for @a notify.
354 * @return Transmission handle, NULL on error (i.e. more than one request queued).
356 struct GNUNET_PSYC_ChannelTransmitHandle *
357 GNUNET_PSYC_channel_transmit (struct GNUNET_PSYC_Channel *channel,
358 int increment_group_generation,
359 const char *method_name,
360 GNUNET_PSYC_ChannelReadyNotify notify,
365 * Abort transmission request to channel.
367 * @param th Handle of the request that is being aborted.
370 GNUNET_PSYC_channel_transmit_cancel (struct GNUNET_PSYC_ChannelTransmitHandle *th);
374 * Destroy a PSYC channel.
376 * @param channel PSYC channel to terminate.
379 GNUNET_PSYC_channel_destroy (struct GNUNET_PSYC_Channel *channel);
383 * Handle to access PSYC group operations for all members.
385 struct GNUNET_PSYC_Group;
389 * Convert @a channel to a @e group handle to access the @e group APIs.
391 * @param channel Channel handle.
392 * @return Group handle, valid for as long as @a channel is valid.
394 struct GNUNET_PSYC_Group *
395 GNUNET_PSYC_channel_get_group (struct GNUNET_PSYC_Channel *channel);
399 * Convert @a member to a @e group handle to access the @e group APIs.
401 * @param member Membership handle.
402 * @return Group handle, valid for as long as @a member is valid.
404 struct GNUNET_PSYC_Group *
405 GNUNET_PSYC_member_get_group (struct GNUNET_PSYC_Member *member);
409 * Add a member to the group.
411 * Note that this will NOT generate any PSYC traffic, it will merely update the
412 * local data base to modify how we react to <em>membership test</em> queries. The
413 * channel still needs to explicitly transmit a @e join message to notify other
414 * group members and they then also must still call this function in their
415 * respective methods handling the @e join message. This way, how @e join and
416 * @e part operations are exactly implemented is still up to the application;
417 * for example, there might be a @e part_all method to kick out everyone.
419 * Note that group members are explicitly trusted to execute such methods
420 * correctly; not doing so correctly will result in either denying members
421 * access or offering access to group data to non-members.
423 * @param group Group handle.
424 * @param member Which peer to add.
425 * @param message_id Message ID for the message that changed the membership.
426 * @param group_generation The generation ID where the change went into effect.
429 GNUNET_PSYC_group_member_add (struct GNUNET_PSYC_Group *group,
430 const struct GNUNET_PeerIdentity *member,
432 uint64_t group_generation);
436 * Remove a member from the group.
438 * Note that this will NOT generate any PSYC traffic, it will merely update the
439 * local data base to modify how we react to <em>membership test</em> queries. The
440 * channel still needs to explicitly transmit a @e part message to notify other
441 * group members and they then also must still call this function in their
442 * respective methods handling the @e part message. This way, how @e join and
443 * @e part operations are exactly implemented is still up to the application;
444 * for example, there might be a @e part_all message to kick out everyone.
446 * Note that group members are explicitly trusted to perform these
447 * operations correctly; not doing so correctly will result in either
448 * denying members access or offering access to group data to
451 * @param group Group handle.
452 * @param member Which peer to remove.
453 * @param message_id Message ID for the message that changed the membership.
454 * @param group_generation The generation ID where the change went into effect.
457 GNUNET_PSYC_group_member_remove (struct GNUNET_PSYC_Group *group,
458 const struct GNUNET_PeerIdentity *member,
460 uint64_t group_generation);
464 * Function called to inform a member about state changes for a channel.
466 * Note that (for sets) only the delta is communicated, not the full state.
468 * @param cls Closure.
469 * @param full_state_name Full name of the state.
470 * @param type How to interpret the change.
471 * @param state_value Information about the new state.
472 * @param state_value_size Number of bytes in @a state_value.
474 typedef void (*GNUNET_PSYC_StateCallback)(void *cls,
475 const char *full_state_name,
476 enum GNUNET_PSYC_Operator type,
477 const void *state_value,
478 size_t state_value_size);
482 * Descriptor for an event handler handling PSYC state updates.
484 struct GNUNET_PSYC_StateHandler
488 * Name of the state variable this handler calls about, used in try-and-slice matching.
490 const char *state_name;
493 * Function to call whenever the respective state changes.
495 GNUNET_PSYC_StateCallback event_handler;
498 * Closure for the @a event_handler function.
500 void *event_handler_cls;
507 * The entity joining is always the local peer. The user must immediately use
508 * the GNUNET_PSYC_member_to_origin() (and possibly
509 * GNUNET_PSYC_member_origin_variable_set()) functions to transmit a @e join_msg to
510 * the channel; if the join request succeeds, the channel state (and @e recent
511 * method calls) will be replayed to the joining member. There is no explicit
512 * notification on failure (as the channel may simply take days to approve, a-v/snd
513 * disapproval is simply being ignored).
515 * @param cfg Configuration to use.
516 * @param pub_key ECC key that identifies the channel we wish to join
517 * @param origin Peer identity of the origin.
518 * @param method Function to invoke on messages received from the channel,
519 * typically at least contains functions for @e join and @e part.
520 * @param method_cls Closure for @a method.
521 * @param state_count Number of @a state_handlers.
522 * @param state_handlers Array of state event handlers.
523 * @return Handle for the member, NULL on error.
525 struct GNUNET_PSYC_Member *
526 GNUNET_PSYC_member_join (const struct GNUNET_CONFIGURATION_Handle *cfg,
527 const struct GNUNET_CRYPTO_EccPublicKey *pub_key,
528 const struct GNUNET_PeerIdentity *origin,
529 GNUNET_PSYC_Method method,
531 unsigned int state_count,
532 struct GNUNET_PSYC_StateHandler *state_handlers);
538 * Will terminate the connection to the PSYC service. Polite clients should
539 * first explicitly send a @e part request (via GNUNET_PSYC_member_to_origin()).
541 * @param member membership handle
544 GNUNET_PSYC_member_part (struct GNUNET_PSYC_Member *member);
548 * Function called to provide data for a transmission to the channel
549 * owner (aka the @e host of the channel).
551 * Note that returning #GNUNET_OK or #GNUNET_SYSERR (but not #GNUNET_NO)
552 * invalidates the respective transmission handle.
554 * @param cls Closure.
555 * @param data_size[in,out] Initially set to the number of bytes available in @a data,
556 * should be set to the number of bytes written to data (IN/OUT).
557 * @param[out] data Where to write the body of the message to give to the method;
558 * function must copy at most @a *data_size bytes to @a data.
559 * @return #GNUNET_SYSERR on error (fatal, aborts transmission).
560 * #GNUNET_NO on success, if more data is to be transmitted later.
561 * #GNUNET_YES if this completes the transmission (all data supplied).
563 typedef int (*GNUNET_PSYC_MemberReadyNotify)(void *cls,
569 * Handle for a pending PSYC transmission operation.
571 struct GNUNET_PSYC_MemberTransmitHandle;
575 * Request a message to be sent to the channel origin.
577 * @param member Membership handle.
578 * @param method_name Which (PSYC) method should be invoked (on host).
579 * @param notify Function to call when we are allowed to transmit (to get data).
580 * @param notify_cls Closure for @a notify.
581 * @return Transmission handle, NULL on error (i.e. more than one request queued).
583 struct GNUNET_PSYC_MemberTransmitHandle *
584 GNUNET_PSYC_member_to_origin (struct GNUNET_PSYC_Member *member,
585 const char *method_name,
586 GNUNET_PSYC_MemberReadyNotify notify,
591 * Set a (temporary, ":") variable for the next message being transmitted
592 * via GNUNET_PSYC_member_to_origin().
594 * If GNUNET_PSYC_member_to_origin() is called and then cancelled, all
595 * variables that were set using this function will be unset (lost/forgotten).
596 * To clear a variable state after setting it, you can also call this function
597 * again with NULL/0 for the @a value.
599 * @param member Membership handle.
600 * @param variable_name Name of the variable to set.
601 * @param value Value to set for the given variable.
602 * @param value_size Number of bytes in @a value.
605 GNUNET_PSYC_member_origin_variable_set (struct GNUNET_PSYC_Member *member,
606 const char *variable_name,
612 * Abort transmission request to origin.
614 * @param th Handle of the request that is being aborted.
617 GNUNET_PSYC_member_to_origin_cancel (struct GNUNET_PSYC_MemberTransmitHandle *th);
621 * Handle to a story telling operation.
623 struct GNUNET_PSYC_Story;
627 * Request to be told the message history of the channel.
629 * Historic messages (but NOT the state at the time) will be replayed (given to
630 * the normal method handlers) if available and if access is permitted.
632 * To get the latest message, use 0 for both the start and end message ID.
634 * @param member Which channel should be replayed?
635 * @param start_message_id Earliest interesting point in history.
636 * @param end_message_id Last (exclusive) interesting point in history.
637 * @param method Function to invoke on messages received from the story.
638 * @param method_cls Closure for @a method.
639 * @param finish_cb Function to call when the requested story has been fully
640 * told (counting message IDs might not suffice, as some messages
641 * might be secret and thus the listener would not know the story is
642 * finished without being told explicitly); once this function
643 * has been called, the client must not call
644 * GNUNET_PSYC_member_story_tell_cancel() anymore.
645 * @param finish_cb_cls Closure to finish_cb.
646 * @return Handle to cancel story telling operation.
648 struct GNUNET_PSYC_Story *
649 GNUNET_PSYC_member_story_tell (struct GNUNET_PSYC_Member *member,
650 uint64_t start_message_id,
651 uint64_t end_message_id,
652 GNUNET_PSYC_Method method,
654 void (*finish_cb)(void *),
655 void *finish_cb_cls);
659 * Abort story telling.
661 * This function must not be called from within method handlers (as given to
662 * GNUNET_PSYC_member_join()) of the member.
664 * @param story Story telling operation to stop.
667 GNUNET_PSYC_member_story_tell_cancel (struct GNUNET_PSYC_Story *story);
671 * Call the given callback on all matching values (including variables) in the
674 * The callback is invoked synchronously on all matching states (as the state is
675 * fully replicated in the library in this process; channel states should be
676 * small, large data is to be passed as streaming data to methods).
678 * A name matches if it includes the @a state_name prefix, thus requesting the
679 * empty state ("") will match all values; requesting "_a_b" will also return
680 * values stored under "_a_b_c".
682 * @param member Membership handle.
683 * @param state_name Name of the state to query (full name
684 * might be longer, this is only the prefix that must match).
685 * @param cb Function to call on the matching state values.
686 * @param cb_cls Closure for @a cb.
687 * @return Message ID for which the state was returned (last seen
691 GNUNET_PSYC_member_state_get_all (struct GNUNET_PSYC_Member *member,
692 const char *state_name,
693 GNUNET_PSYC_StateCallback cb,
698 * Obtain the current value of the best-matching value in the state
699 * (including variables).
701 * Note that variables are only valid during a #GNUNET_PSYC_Method invocation, as
702 * variables are only valid for the duration of a method invocation.
704 * If the requested variable name does not have an exact state in
705 * the state, the nearest less-specific name is matched; for example,
706 * requesting "_a_b" will match "_a" if "_a_b" does not exist.
708 * @param member Membership handle.
709 * @param variable_name Name of the variable to query.
710 * @param[out] return_value_size Set to number of bytes in variable,
711 * needed as variables might contain binary data and
712 * might also not be 0-terminated; set to 0 on errors.
713 * @return NULL on error (no matching state or variable), pointer
714 to the respective value otherwise.
717 GNUNET_PSYC_member_state_get (struct GNUNET_PSYC_Member *member,
718 const char *variable_name,
719 size_t *return_value_size);
722 #if 0 /* keep Emacsens' auto-indent happy */
729 /* ifndef GNUNET_PSYC_SERVICE_H */
731 /* end of gnunet_psyc_service.h */