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
104 enum GNUNET_PSYC_MessageFlags
107 * First fragment of a message.
109 GNUNET_PSYC_MESSAGE_FIRST_FRAGMENT = GNUNET_MULTICAST_MESSAGE_FIRST_FRAGMENT,
112 * Last fragment of a message.
114 GNUNET_PSYC_MESSAGE_LAST_FRAGMENT = GNUNET_MULTICAST_MESSAGE_LAST_FRAGMENT,
117 * OR'ed flags if message is not fragmented.
119 GNUNET_PSYC_MESSAGE_NOT_FRAGMENTED = GNUNET_MULTICAST_MESSAGE_NOT_FRAGMENTED
124 * Handle that identifies a join request.
126 * Used to match calls to #GNUNET_PSYC_JoinCallback to the
127 * corresponding calls to GNUNET_PSYC_join_decision().
129 struct GNUNET_PSYC_JoinHandle;
132 * Handle that identifies a part request.
134 * Used to match calls to #GNUNET_PSYC_PartCallback to the
135 * corresponding calls to GNUNET_PSYC_part_ack().
137 struct GNUNET_PSYC_PartHandle;
141 * Method called from PSYC upon receiving a message indicating a call
144 * @param cls Closure.
145 * @param sender Who transmitted the message (origin, except for messages
146 * from one of the members to the origin).
147 * @param message_id Unique message counter for this message;
148 * (unique only in combination with the given sender for
150 * @param group_generation Group generation counter for this message
151 * (always zero for messages from members to channel owner); FIXME: needed?
152 * @param method_name Original method name from PSYC (may be more
153 * specific than the registered method name due to try-and-slice matching).
154 * FIXME: no try-and-slice for methods defined here.
155 * @param header_length Number of modifiers in header.
156 * @param header Modifiers present in the message.
157 * @param data_offset Byte offset of @a data in the overall data of the method.
158 * @param data_size Number of bytes in @a data.
159 * @param data Data stream given to the method (might not be zero-terminated
160 * if data is binary).
161 * @param frag Fragmentation status for the data.
163 typedef int (*GNUNET_PSYC_Method)(void *cls,
164 const struct GNUNET_PeerIdentity *sender,
166 uint64_t group_generation,
167 const char *method_name,
168 size_t header_length,
169 GNUNET_PSYC_Modifier *header,
170 uint64_t data_offset,
173 enum GNUNET_PSYC_MessageFlags flags);
177 * Method called from PSYC upon receiving a join request.
179 * @param cls Closure.
180 * @param peer Peer requesting to join.
181 * @param method_name Method name in the join request.
182 * @param header_length Number of modifiers in header.
183 * @param header Modifiers present in the message.
184 * @param data_size Number of bytes in @a data.
185 * @param data Data stream given to the method (might not be zero-terminated
186 * if data is binary).
188 typedef int (*GNUNET_PSYC_JoinCallback)(void *cls,
189 const struct GNUNET_PeerIdentity *peer,
190 const char *method_name,
191 size_t header_length,
192 GNUNET_PSYC_Modifier *header,
195 struct GNUNET_PSYC_JoinHandle *jh);
199 * Method called from PSYC upon receiving a part request.
201 * @param cls Closure.
202 * @param peer Peer requesting to leave.
203 * @param method_name Method name in the part request.
204 * @param header_length Number of modifiers in header.
205 * @param header Modifiers present in the message.
206 * @param data_size Number of bytes in @a data.
207 * @param data Data stream given to the method (might not be zero-terminated
208 * if data is binary).
210 typedef int (*GNUNET_PSYC_PartCallback)(void *cls,
211 const struct GNUNET_PeerIdentity *peer,
212 const char *method_name,
213 size_t header_length,
214 GNUNET_PSYC_Modifier *header,
217 struct GNUNET_PSYC_PartHandle *ph);
221 * Function to call with the decision made for a join request.
223 * Must be called once and only once in response to an invocation of the
224 * #GNUNET_PSYC_JoinCallback.
226 * @param jh Join request handle.
227 * @param is_admitted #GNUNET_YES if joining is approved,
228 * #GNUNET_NO if it is disapproved
229 * @param method_name Method name for the message transmitted with the response.
230 * @param data_size Size of @a data.
231 * @param data Data of the message.
234 GNUNET_PSYC_join_decision (struct GNUNET_PSYC_JoinHandle *jh,
236 const char *method_name,
242 * Send a part acknowledgment.
244 * @param ph Part handle.
247 GNUNET_PSYC_part_ack (struct GNUNET_PSYC_PartHandle *ph);
251 * Handle for the channel of a PSYC group.
253 struct GNUNET_PSYC_Channel;
257 * Create a PSYC channel.
259 * Will create a multicast group identified by the given ECC key. Messages
260 * received from group members will be given to the respective handler methods.
261 * If a new member wants to join a group, the "join" method handler will be
262 * invoked; the join handler must then generate a "join" message to approve the
263 * joining of the new member. The channel can also change group membership
264 * without explicit requests. Note that PSYC doesn't itself "understand" join
265 * or part messages, the respective methods must call other PSYC functions to
266 * inform PSYC about the meaning of the respective events.
268 * @param cfg Configuration to use (to connect to PSYC service).
269 * @param priv_key ECC key that will be used to sign messages for this
270 * PSYC session; public key is used to identify the
271 * PSYC group; FIXME: we'll likely want to use
272 * NOT the p521 curve here, but a cheaper one in the future
273 * Note that end-users will usually not use the private key
274 * directly, but rather look it up in GADS for groups
275 * managed by other users, or select a file with the private
276 * key(s) when setting up their own channels
277 * @param join_policy What is the membership policy of the group?
278 * Used to automate group management decisions.
279 * @param method_cb Function to invoke on messages received from members.
280 * @param join_cb Function to invoke when a peer wants to join.
281 * @param part_cb Function to invoke when a peer wants to part.
282 * @param cls Closure for the callbacks.
283 * @return Handle for the channel, NULL on error.
285 struct GNUNET_PSYC_Channel *
286 GNUNET_PSYC_channel_create (const struct GNUNET_CONFIGURATION_Handle *cfg,
287 const struct GNUNET_CRYPTO_EccPrivateKey *priv_key,
288 enum GNUNET_MULTICAST_JoinPolicy join_policy,
289 GNUNET_PSYC_Method method_cb,
290 GNUNET_PSYC_JoinCallback join_cb,
291 GNUNET_PSYC_PartCallback part_cb,
296 * Modify channel state (or set a transient variable).
298 * The state of a channel must fit into the memory of each member (and the
299 * channel); large values that require streaming must only be passed as the
300 * stream arguments to methods. State modifications might not be transmitted to
301 * group members until the next call to GNUNET_PSYC_channel_transmit().
302 * Variable updates must be given just before the call to the respective method
303 * that needs the variables.
305 * @param channel Handle to the PSYC group / channel.
306 * @param oper Kind of update operation (add, remove, replace, delete).
307 * @param name Name of the state or transient variable to modify.
308 * @param value_size Number of bytes in @a value.
309 * @param value Value of state variable.
310 * @return #GNUNET_OK on success, #GNUNET_SYSERR on internal error
311 * (i.e. state too large).
314 GNUNET_PSYC_channel_state_modify (struct GNUNET_PSYC_Channel *channel,
315 enum GNUNET_PSYC_Operator oper,
322 * Function called to provide data for a transmission via PSYC.
324 * Note that returning #GNUNET_OK or #GNUNET_SYSERR (but not #GNUNET_NO)
325 * invalidates the respective transmission handle.
327 * @param cls Closure.
328 * @param message_id Set to the unique message ID that was generated for
330 * @param group_generation Set to the group generation used for this
332 * @param data_size[in,out] Initially set to the number of bytes available in @a data,
333 * should be set to the number of bytes written to data (IN/OUT).
334 * @param[out] data Where to write the body of the message to give to the method;
335 * function must copy at most @a *data_size bytes to @a data.
336 * @return #GNUNET_SYSERR on error (fatal, aborts transmission)
337 * #GNUNET_NO on success, if more data is to be transmitted later
338 * (should be used if @a *data_size was not big enough to take all the data)
339 * #GNUNET_YES if this completes the transmission (all data supplied)
341 typedef int (*GNUNET_PSYC_ChannelReadyNotify)(void *cls,
343 uint64_t group_generation,
349 * Handle for a pending PSYC transmission operation.
351 struct GNUNET_PSYC_ChannelTransmitHandle;
355 * Send a message to call a method to all members in the PSYC channel.
357 * @param channel Handle to the PSYC multicast group.
358 * @param increment_group_generation #GNUNET_YES if we need to increment
359 * the group generation counter after transmitting this message.
360 * @param method_name Which method should be invoked.
361 * @param notify Function to call to obtain the arguments.
362 * @param notify_cls Closure for @a notify.
363 * @return Transmission handle, NULL on error (i.e. more than one request queued).
365 struct GNUNET_PSYC_ChannelTransmitHandle *
366 GNUNET_PSYC_channel_transmit (struct GNUNET_PSYC_Channel *channel,
367 int increment_group_generation,
368 const char *method_name,
369 GNUNET_PSYC_ChannelReadyNotify notify,
374 * Abort transmission request to channel.
376 * @param th Handle of the request that is being aborted.
379 GNUNET_PSYC_channel_transmit_cancel (struct GNUNET_PSYC_ChannelTransmitHandle *th);
383 * Destroy a PSYC channel.
385 * @param channel PSYC channel to terminate.
388 GNUNET_PSYC_channel_destroy (struct GNUNET_PSYC_Channel *channel);
392 * Handle to access PSYC group operations for all members.
394 struct GNUNET_PSYC_Group;
398 * Convert @a channel to a @e group handle to access the @e group APIs.
400 * @param channel Channel handle.
401 * @return Group handle, valid for as long as @a channel is valid.
403 struct GNUNET_PSYC_Group *
404 GNUNET_PSYC_channel_get_group (struct GNUNET_PSYC_Channel *channel);
408 * Convert @a member to a @e group handle to access the @e group APIs.
410 * @param member Membership handle.
411 * @return Group handle, valid for as long as @a member is valid.
413 struct GNUNET_PSYC_Group *
414 GNUNET_PSYC_member_get_group (struct GNUNET_PSYC_Member *member);
418 * Add a member to the group.
420 * Note that this will NOT generate any PSYC traffic, it will merely update the
421 * local data base to modify how we react to <em>membership test</em> queries. The
422 * channel still needs to explicitly transmit a @e join message to notify other
423 * group members and they then also must still call this function in their
424 * respective methods handling the @e join message. This way, how @e join and
425 * @e part operations are exactly implemented is still up to the application;
426 * for example, there might be a @e part_all method to kick out everyone.
428 * Note that group members are explicitly trusted to execute such methods
429 * correctly; not doing so correctly will result in either denying members
430 * access or offering access to group data to non-members.
432 * @param group Group handle.
433 * @param member Which peer to add.
434 * @param message_id Message ID for the message that changed the membership.
435 * @param group_generation The generation ID where the change went into effect.
438 GNUNET_PSYC_group_member_add (struct GNUNET_PSYC_Group *group,
439 const struct GNUNET_PeerIdentity *member,
441 uint64_t group_generation);
445 * Remove a member from the group.
447 * Note that this will NOT generate any PSYC traffic, it will merely update the
448 * local data base to modify how we react to <em>membership test</em> queries. The
449 * channel still needs to explicitly transmit a @e part message to notify other
450 * group members and they then also must still call this function in their
451 * respective methods handling the @e part message. This way, how @e join and
452 * @e part operations are exactly implemented is still up to the application;
453 * for example, there might be a @e part_all message to kick out everyone.
455 * Note that group members are explicitly trusted to perform these
456 * operations correctly; not doing so correctly will result in either
457 * denying members access or offering access to group data to
460 * @param group Group handle.
461 * @param member Which peer to remove.
462 * @param message_id Message ID for the message that changed the membership.
463 * @param group_generation The generation ID where the change went into effect.
466 GNUNET_PSYC_group_member_remove (struct GNUNET_PSYC_Group *group,
467 const struct GNUNET_PeerIdentity *member,
469 uint64_t group_generation);
473 * Function called to inform a member about stored state values for a channel.
475 * @param cls Closure.
476 * @param name Name of the state variable.
477 * @param value Value of the state variable.
478 * @param value_size Number of bytes in @a value.
480 typedef void (*GNUNET_PSYC_StateCallback)(void *cls,
489 * The entity joining is always the local peer. The user must immediately use
490 * the GNUNET_PSYC_member_to_origin() (and possibly
491 * GNUNET_PSYC_member_variable_set()) functions to transmit a @e join_msg to
492 * the channel; if the join request succeeds, the channel state (and @e recent
493 * method calls) will be replayed to the joining member. There is no explicit
494 * notification on failure (as the channel may simply take days to approve, a-v/snd
495 * disapproval is simply being ignored).
497 * @param cfg Configuration to use.
498 * @param pub_key ECC key that identifies the channel we wish to join
499 * @param origin Peer identity of the origin.
500 * @param method Function to invoke on messages received from the channel,
501 * typically at least contains functions for @e join and @e part.
502 * @param method_cls Closure for @a method.
503 * @return Handle for the member, NULL on error.
505 struct GNUNET_PSYC_Member *
506 GNUNET_PSYC_member_join (const struct GNUNET_CONFIGURATION_Handle *cfg,
507 const struct GNUNET_CRYPTO_EccPublicKey *pub_key,
508 const struct GNUNET_PeerIdentity *origin,
509 GNUNET_PSYC_Method method,
516 * Will terminate the connection to the PSYC service. Polite clients should
517 * first explicitly send a @e part request (via GNUNET_PSYC_member_to_origin()).
519 * @param member membership handle
522 GNUNET_PSYC_member_part (struct GNUNET_PSYC_Member *member);
526 * Function called to provide data for a transmission to the channel
527 * owner (aka the @e host of the channel).
529 * Note that returning #GNUNET_OK or #GNUNET_SYSERR (but not #GNUNET_NO)
530 * invalidates the respective transmission handle.
532 * @param cls Closure.
533 * @param data_size[in,out] Initially set to the number of bytes available in @a data,
534 * should be set to the number of bytes written to data (IN/OUT).
535 * @param[out] data Where to write the body of the message to give to the method;
536 * function must copy at most @a *data_size bytes to @a data.
537 * @return #GNUNET_SYSERR on error (fatal, aborts transmission).
538 * #GNUNET_NO on success, if more data is to be transmitted later.
539 * #GNUNET_YES if this completes the transmission (all data supplied).
541 typedef int (*GNUNET_PSYC_MemberReadyNotify)(void *cls,
547 * Handle for a pending PSYC transmission operation.
549 struct GNUNET_PSYC_MemberTransmitHandle;
553 * Request a message to be sent to the channel origin.
555 * @param member Membership handle.
556 * @param method_name Which (PSYC) method should be invoked (on host).
557 * @param notify Function to call when we are allowed to transmit (to get data).
558 * @param notify_cls Closure for @a notify.
559 * @return Transmission handle, NULL on error (i.e. more than one request queued).
561 struct GNUNET_PSYC_MemberTransmitHandle *
562 GNUNET_PSYC_member_to_origin (struct GNUNET_PSYC_Member *member,
563 const char *method_name,
564 GNUNET_PSYC_MemberReadyNotify notify,
569 * Set a (temporary, ":") variable for the next message being transmitted
570 * via GNUNET_PSYC_member_to_origin().
572 * If GNUNET_PSYC_member_to_origin() is called and then cancelled, all
573 * variables that were set using this function will be unset (lost/forgotten).
574 * To clear a variable state after setting it, you can also call this function
575 * again with NULL/0 for the @a value.
577 * @param member Membership handle.
578 * @param name Name of the variable to set.
579 * @param value_size Number of bytes in @a value.
580 * @param value Value to set for the given variable.
583 GNUNET_PSYC_member_variable_set (struct GNUNET_PSYC_Member *member,
590 * Abort transmission request to origin.
592 * @param th Handle of the request that is being aborted.
595 GNUNET_PSYC_member_to_origin_cancel (struct GNUNET_PSYC_MemberTransmitHandle *th);
599 * Handle to a story telling operation.
601 struct GNUNET_PSYC_Story;
605 * Request to be told the message history of the channel.
607 * Historic messages (but NOT the state at the time) will be replayed (given to
608 * the normal method handlers) if available and if access is permitted.
610 * To get the latest message, use 0 for both the start and end message ID.
612 * @param member Which channel should be replayed?
613 * @param start_message_id Earliest interesting point in history.
614 * @param end_message_id Last (exclusive) interesting point in history.
615 * @param method Function to invoke on messages received from the story.
616 * @param method_cls Closure for @a method.
617 * @param finish_cb Function to call when the requested story has been fully
618 * told (counting message IDs might not suffice, as some messages
619 * might be secret and thus the listener would not know the story is
620 * finished without being told explicitly); once this function
621 * has been called, the client must not call
622 * GNUNET_PSYC_member_story_tell_cancel() anymore.
623 * @param finish_cb_cls Closure to finish_cb.
624 * @return Handle to cancel story telling operation.
626 struct GNUNET_PSYC_Story *
627 GNUNET_PSYC_member_story_tell (struct GNUNET_PSYC_Member *member,
628 uint64_t start_message_id,
629 uint64_t end_message_id,
630 GNUNET_PSYC_Method method,
632 void (*finish_cb)(void *),
633 void *finish_cb_cls);
637 * Abort story telling.
639 * This function must not be called from within method handlers (as given to
640 * GNUNET_PSYC_member_join()) of the member.
642 * @param story Story telling operation to stop.
645 GNUNET_PSYC_member_story_tell_cancel (struct GNUNET_PSYC_Story *story);
649 * Call the given callback on all matching values (including variables) in the
652 * The callback is invoked synchronously on all matching states (as the state is
653 * fully replicated in the library in this process; channel states should be
654 * small, large data is to be passed as streaming data to methods).
656 * A name matches if it includes the @a state_name prefix, thus requesting the
657 * empty state ("") will match all values; requesting "_a_b" will also return
658 * values stored under "_a_b_c".
660 * @param member Membership handle.
661 * @param state_name Name of the state to query (full name
662 * might be longer, this is only the prefix that must match).
663 * @param cb Function to call on the matching state values.
664 * @param cb_cls Closure for @a cb.
665 * @return Message ID for which the state was returned (last seen
669 GNUNET_PSYC_member_state_get_all (struct GNUNET_PSYC_Member *member,
670 const char *state_name,
671 GNUNET_PSYC_StateCallback cb,
676 * Obtain the current value of the best-matching value in the state
677 * (including variables).
679 * Note that variables are only valid during a #GNUNET_PSYC_Method invocation, as
680 * variables are only valid for the duration of a method invocation.
682 * If the requested variable name does not have an exact state in
683 * the state, the nearest less-specific name is matched; for example,
684 * requesting "_a_b" will match "_a" if "_a_b" does not exist.
686 * @param member Membership handle.
687 * @param variable_name Name of the variable to query.
688 * @param[out] return_value_size Set to number of bytes in variable,
689 * needed as variables might contain binary data and
690 * might also not be 0-terminated; set to 0 on errors.
691 * @return NULL on error (no matching state or variable), pointer
692 to the respective value otherwise.
695 GNUNET_PSYC_member_state_get (struct GNUNET_PSYC_Member *member,
696 const char *variable_name,
697 size_t *return_value_size);
700 #if 0 /* keep Emacsens' auto-indent happy */
707 /* ifndef GNUNET_PSYC_SERVICE_H */
709 /* end of gnunet_psyc_service.h */