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 header_length Number of modifiers in header.
159 * @param header Modifiers present in the message.
160 * @param data_off Byte offset of @a data in the overall data of the method.
161 * @param data_size Number of bytes in @a data.
162 * @param data Data stream given to the method (might not be zero-terminated
163 * if data is binary).
164 * @param frag Fragmentation status for the data.
166 typedef int (*GNUNET_PSYC_Method)(void *cls,
167 const struct GNUNET_PeerIdentity *sender,
169 uint64_t group_generation,
170 const char *method_name,
174 enum GNUNET_PSYC_FragmentStatus frag);
178 * Method called from PSYC upon receiving a join request.
180 * @param cls Closure.
181 * @param peer Peer requesting to join.
182 * @param method_name Method name in the join request.
183 * @param header_length Number of modifiers in header.
184 * @param header Modifiers present in the message.
185 * @param data_size Number of bytes in @a data.
186 * @param data Data stream given to the method (might not be zero-terminated
187 * if data is binary).
189 typedef int (*GNUNET_PSYC_JoinCallback)(void *cls,
190 const struct GNUNET_PeerIdentity *peer,
191 const char *method_name,
192 size_t header_length,
193 GNUNET_PSYC_Modifier *header,
196 struct GNUNET_PSYC_JoinHandle *jh);
200 * Method called from PSYC upon receiving a part request.
202 * @param cls Closure.
203 * @param peer Peer requesting to leave.
204 * @param method_name Method name in the part request.
205 * @param header_length Number of modifiers in header.
206 * @param header Modifiers present in the message.
207 * @param data_size Number of bytes in @a data.
208 * @param data Data stream given to the method (might not be zero-terminated
209 * if data is binary).
211 typedef int (*GNUNET_PSYC_PartCallback)(void *cls,
212 const struct GNUNET_PeerIdentity *peer,
213 const char *method_name,
214 size_t header_length,
215 GNUNET_PSYC_Modifier *header,
218 struct GNUNET_PSYC_PartHandle *ph);
222 * Function to call with the decision made for a join request.
224 * Must be called once and only once in response to an invocation of the
225 * #GNUNET_PSYC_JoinCallback.
227 * @param jh Join request handle.
228 * @param is_admitted #GNUNET_YES if joining is approved,
229 * #GNUNET_NO if it is disapproved
230 * @param method_name Method name for the message transmitted with the response.
231 * @param data_size Size of @a data.
232 * @param data Data of the message.
235 GNUNET_PSYC_join_decision (struct GNUNET_PSYC_JoinHandle *jh,
237 const char *method_name,
243 * Send a part acknowledgment.
245 * @param ph Part handle.
248 GNUNET_PSYC_part_ack (struct GNUNET_PSYC_PartHandle *ph);
252 * Handle for the channel of a PSYC group.
254 struct GNUNET_PSYC_Channel;
258 * Create a PSYC channel.
260 * Will create a multicast group identified by the given ECC key. Messages
261 * received from group members will be given to the respective handler methods.
262 * If a new member wants to join a group, the "join" method handler will be
263 * invoked; the join handler must then generate a "join" message to approve the
264 * joining of the new member. The channel can also change group membership
265 * without explicit requests. Note that PSYC doesn't itself "understand" join
266 * or part messages, the respective methods must call other PSYC functions to
267 * inform PSYC about the meaning of the respective events.
269 * @param cfg Configuration to use (to connect to PSYC service).
270 * @param priv_key ECC key that will be used to sign messages for this
271 * PSYC session; public key is used to identify the
272 * PSYC group; FIXME: we'll likely want to use
273 * NOT the p521 curve here, but a cheaper one in the future
274 * Note that end-users will usually not use the private key
275 * directly, but rather look it up in GADS for groups
276 * managed by other users, or select a file with the private
277 * key(s) when setting up their own channels
278 * @param join_policy What is the membership policy of the group?
279 * Used to automate group management decisions.
280 * @param method_cb Function to invoke on messages received from members.
281 * @param join_cb Function to invoke when a peer wants to join.
282 * @param part_cb Function to invoke when a peer wants to part.
283 * @param cls Closure for the callbacks.
284 * @return Handle for the channel, NULL on error.
286 struct GNUNET_PSYC_Channel *
287 GNUNET_PSYC_channel_create (const struct GNUNET_CONFIGURATION_Handle *cfg,
288 const struct GNUNET_CRYPTO_EccPrivateKey *priv_key,
289 enum GNUNET_MULTICAST_JoinPolicy join_policy,
290 GNUNET_PSYC_Method method_cb,
291 GNUNET_PSYC_JoinCallback join_cb,
292 GNUNET_PSYC_PartCallback part_cb,
297 * Modify channel state (or set a transient variable).
299 * The state of a channel must fit into the memory of each member (and the
300 * channel); large values that require streaming must only be passed as the
301 * stream arguments to methods. State modifications might not be transmitted to
302 * group members until the next call to GNUNET_PSYC_channel_transmit().
303 * Variable updates must be given just before the call to the respective method
304 * that needs the variables.
306 * @param channel Handle to the PSYC group / channel.
307 * @param oper Kind of update operation (add, remove, replace, delete).
308 * @param name Name of the state or transient variable to modify.
309 * @param value_size Number of bytes in @a value.
310 * @param value Value of state variable.
311 * @return #GNUNET_OK on success, #GNUNET_SYSERR on internal error
312 * (i.e. state too large).
315 GNUNET_PSYC_channel_state_modify (struct GNUNET_PSYC_Channel *channel,
316 enum GNUNET_PSYC_Operator oper,
323 * Function called to provide data for a transmission via PSYC.
325 * Note that returning #GNUNET_OK or #GNUNET_SYSERR (but not #GNUNET_NO)
326 * invalidates the respective transmission handle.
328 * @param cls Closure.
329 * @param message_id Set to the unique message ID that was generated for
331 * @param group_generation Set to the group generation used for this
333 * @param data_size[in,out] Initially set to the number of bytes available in @a data,
334 * should be set to the number of bytes written to data (IN/OUT).
335 * @param[out] data Where to write the body of the message to give to the method;
336 * function must copy at most @a *data_size bytes to @a data.
337 * @return #GNUNET_SYSERR on error (fatal, aborts transmission)
338 * #GNUNET_NO on success, if more data is to be transmitted later
339 * (should be used if @a *data_size was not big enough to take all the data)
340 * #GNUNET_YES if this completes the transmission (all data supplied)
342 typedef int (*GNUNET_PSYC_ChannelReadyNotify)(void *cls,
344 uint64_t group_generation,
350 * Handle for a pending PSYC transmission operation.
352 struct GNUNET_PSYC_ChannelTransmitHandle;
356 * Send a message to call a method to all members in the PSYC channel.
358 * @param channel Handle to the PSYC multicast group.
359 * @param increment_group_generation #GNUNET_YES if we need to increment
360 * the group generation counter after transmitting this message.
361 * @param method_name Which method should be invoked.
362 * @param notify Function to call to obtain the arguments.
363 * @param notify_cls Closure for @a notify.
364 * @return Transmission handle, NULL on error (i.e. more than one request queued).
366 struct GNUNET_PSYC_ChannelTransmitHandle *
367 GNUNET_PSYC_channel_transmit (struct GNUNET_PSYC_Channel *channel,
368 int increment_group_generation,
369 const char *method_name,
370 GNUNET_PSYC_ChannelReadyNotify notify,
375 * Abort transmission request to channel.
377 * @param th Handle of the request that is being aborted.
380 GNUNET_PSYC_channel_transmit_cancel (struct GNUNET_PSYC_ChannelTransmitHandle *th);
384 * Destroy a PSYC channel.
386 * @param channel PSYC channel to terminate.
389 GNUNET_PSYC_channel_destroy (struct GNUNET_PSYC_Channel *channel);
393 * Handle to access PSYC group operations for all members.
395 struct GNUNET_PSYC_Group;
399 * Convert @a channel to a @e group handle to access the @e group APIs.
401 * @param channel Channel handle.
402 * @return Group handle, valid for as long as @a channel is valid.
404 struct GNUNET_PSYC_Group *
405 GNUNET_PSYC_channel_get_group (struct GNUNET_PSYC_Channel *channel);
409 * Convert @a member to a @e group handle to access the @e group APIs.
411 * @param member Membership handle.
412 * @return Group handle, valid for as long as @a member is valid.
414 struct GNUNET_PSYC_Group *
415 GNUNET_PSYC_member_get_group (struct GNUNET_PSYC_Member *member);
419 * Add a member to the group.
421 * Note that this will NOT generate any PSYC traffic, it will merely update the
422 * local data base to modify how we react to <em>membership test</em> queries. The
423 * channel still needs to explicitly transmit a @e join message to notify other
424 * group members and they then also must still call this function in their
425 * respective methods handling the @e join message. This way, how @e join and
426 * @e part operations are exactly implemented is still up to the application;
427 * for example, there might be a @e part_all method to kick out everyone.
429 * Note that group members are explicitly trusted to execute such methods
430 * correctly; not doing so correctly will result in either denying members
431 * access or offering access to group data to non-members.
433 * @param group Group handle.
434 * @param member Which peer to add.
435 * @param message_id Message ID for the message that changed the membership.
436 * @param group_generation The generation ID where the change went into effect.
439 GNUNET_PSYC_group_member_add (struct GNUNET_PSYC_Group *group,
440 const struct GNUNET_PeerIdentity *member,
442 uint64_t group_generation);
446 * Remove a member from the group.
448 * Note that this will NOT generate any PSYC traffic, it will merely update the
449 * local data base to modify how we react to <em>membership test</em> queries. The
450 * channel still needs to explicitly transmit a @e part message to notify other
451 * group members and they then also must still call this function in their
452 * respective methods handling the @e part message. This way, how @e join and
453 * @e part operations are exactly implemented is still up to the application;
454 * for example, there might be a @e part_all message to kick out everyone.
456 * Note that group members are explicitly trusted to perform these
457 * operations correctly; not doing so correctly will result in either
458 * denying members access or offering access to group data to
461 * @param group Group handle.
462 * @param member Which peer to remove.
463 * @param message_id Message ID for the message that changed the membership.
464 * @param group_generation The generation ID where the change went into effect.
467 GNUNET_PSYC_group_member_remove (struct GNUNET_PSYC_Group *group,
468 const struct GNUNET_PeerIdentity *member,
470 uint64_t group_generation);
474 * Function called to inform a member about stored state values for a channel.
476 * @param cls Closure.
477 * @param name Name of the state variable.
478 * @param value Value of the state variable.
479 * @param value_size Number of bytes in @a value.
481 typedef void (*GNUNET_PSYC_StateCallback)(void *cls,
490 * The entity joining is always the local peer. The user must immediately use
491 * the GNUNET_PSYC_member_to_origin() (and possibly
492 * GNUNET_PSYC_member_variable_set()) functions to transmit a @e join_msg to
493 * the channel; if the join request succeeds, the channel state (and @e recent
494 * method calls) will be replayed to the joining member. There is no explicit
495 * notification on failure (as the channel may simply take days to approve, a-v/snd
496 * disapproval is simply being ignored).
498 * @param cfg Configuration to use.
499 * @param pub_key ECC key that identifies the channel we wish to join
500 * @param origin Peer identity of the origin.
501 * @param method Function to invoke on messages received from the channel,
502 * typically at least contains functions for @e join and @e part.
503 * @param method_cls Closure for @a method.
504 * @return Handle for the member, NULL on error.
506 struct GNUNET_PSYC_Member *
507 GNUNET_PSYC_member_join (const struct GNUNET_CONFIGURATION_Handle *cfg,
508 const struct GNUNET_CRYPTO_EccPublicKey *pub_key,
509 const struct GNUNET_PeerIdentity *origin,
510 GNUNET_PSYC_Method method,
517 * Will terminate the connection to the PSYC service. Polite clients should
518 * first explicitly send a @e part request (via GNUNET_PSYC_member_to_origin()).
520 * @param member membership handle
523 GNUNET_PSYC_member_part (struct GNUNET_PSYC_Member *member);
527 * Function called to provide data for a transmission to the channel
528 * owner (aka the @e host of the channel).
530 * Note that returning #GNUNET_OK or #GNUNET_SYSERR (but not #GNUNET_NO)
531 * invalidates the respective transmission handle.
533 * @param cls Closure.
534 * @param data_size[in,out] Initially set to the number of bytes available in @a data,
535 * should be set to the number of bytes written to data (IN/OUT).
536 * @param[out] data Where to write the body of the message to give to the method;
537 * function must copy at most @a *data_size bytes to @a data.
538 * @return #GNUNET_SYSERR on error (fatal, aborts transmission).
539 * #GNUNET_NO on success, if more data is to be transmitted later.
540 * #GNUNET_YES if this completes the transmission (all data supplied).
542 typedef int (*GNUNET_PSYC_MemberReadyNotify)(void *cls,
548 * Handle for a pending PSYC transmission operation.
550 struct GNUNET_PSYC_MemberTransmitHandle;
554 * Request a message to be sent to the channel origin.
556 * @param member Membership handle.
557 * @param method_name Which (PSYC) method should be invoked (on host).
558 * @param notify Function to call when we are allowed to transmit (to get data).
559 * @param notify_cls Closure for @a notify.
560 * @return Transmission handle, NULL on error (i.e. more than one request queued).
562 struct GNUNET_PSYC_MemberTransmitHandle *
563 GNUNET_PSYC_member_to_origin (struct GNUNET_PSYC_Member *member,
564 const char *method_name,
565 GNUNET_PSYC_MemberReadyNotify notify,
570 * Set a (temporary, ":") variable for the next message being transmitted
571 * via GNUNET_PSYC_member_to_origin().
573 * If GNUNET_PSYC_member_to_origin() is called and then cancelled, all
574 * variables that were set using this function will be unset (lost/forgotten).
575 * To clear a variable state after setting it, you can also call this function
576 * again with NULL/0 for the @a value.
578 * @param member Membership handle.
579 * @param name Name of the variable to set.
580 * @param value_size Number of bytes in @a value.
581 * @param value Value to set for the given variable.
584 GNUNET_PSYC_member_variable_set (struct GNUNET_PSYC_Member *member,
591 * Abort transmission request to origin.
593 * @param th Handle of the request that is being aborted.
596 GNUNET_PSYC_member_to_origin_cancel (struct GNUNET_PSYC_MemberTransmitHandle *th);
600 * Handle to a story telling operation.
602 struct GNUNET_PSYC_Story;
606 * Request to be told the message history of the channel.
608 * Historic messages (but NOT the state at the time) will be replayed (given to
609 * the normal method handlers) if available and if access is permitted.
611 * To get the latest message, use 0 for both the start and end message ID.
613 * @param member Which channel should be replayed?
614 * @param start_message_id Earliest interesting point in history.
615 * @param end_message_id Last (exclusive) interesting point in history.
616 * @param method Function to invoke on messages received from the story.
617 * @param method_cls Closure for @a method.
618 * @param finish_cb Function to call when the requested story has been fully
619 * told (counting message IDs might not suffice, as some messages
620 * might be secret and thus the listener would not know the story is
621 * finished without being told explicitly); once this function
622 * has been called, the client must not call
623 * GNUNET_PSYC_member_story_tell_cancel() anymore.
624 * @param finish_cb_cls Closure to finish_cb.
625 * @return Handle to cancel story telling operation.
627 struct GNUNET_PSYC_Story *
628 GNUNET_PSYC_member_story_tell (struct GNUNET_PSYC_Member *member,
629 uint64_t start_message_id,
630 uint64_t end_message_id,
631 GNUNET_PSYC_Method method,
633 void (*finish_cb)(void *),
634 void *finish_cb_cls);
638 * Abort story telling.
640 * This function must not be called from within method handlers (as given to
641 * GNUNET_PSYC_member_join()) of the member.
643 * @param story Story telling operation to stop.
646 GNUNET_PSYC_member_story_tell_cancel (struct GNUNET_PSYC_Story *story);
650 * Call the given callback on all matching values (including variables) in the
653 * The callback is invoked synchronously on all matching states (as the state is
654 * fully replicated in the library in this process; channel states should be
655 * small, large data is to be passed as streaming data to methods).
657 * A name matches if it includes the @a state_name prefix, thus requesting the
658 * empty state ("") will match all values; requesting "_a_b" will also return
659 * values stored under "_a_b_c".
661 * @param member Membership handle.
662 * @param state_name Name of the state to query (full name
663 * might be longer, this is only the prefix that must match).
664 * @param cb Function to call on the matching state values.
665 * @param cb_cls Closure for @a cb.
666 * @return Message ID for which the state was returned (last seen
670 GNUNET_PSYC_member_state_get_all (struct GNUNET_PSYC_Member *member,
671 const char *state_name,
672 GNUNET_PSYC_StateCallback cb,
677 * Obtain the current value of the best-matching value in the state
678 * (including variables).
680 * Note that variables are only valid during a #GNUNET_PSYC_Method invocation, as
681 * variables are only valid for the duration of a method invocation.
683 * If the requested variable name does not have an exact state in
684 * the state, the nearest less-specific name is matched; for example,
685 * requesting "_a_b" will match "_a" if "_a_b" does not exist.
687 * @param member Membership handle.
688 * @param variable_name Name of the variable to query.
689 * @param[out] return_value_size Set to number of bytes in variable,
690 * needed as variables might contain binary data and
691 * might also not be 0-terminated; set to 0 on errors.
692 * @return NULL on error (no matching state or variable), pointer
693 to the respective value otherwise.
696 GNUNET_PSYC_member_state_get (struct GNUNET_PSYC_Member *member,
697 const char *variable_name,
698 size_t *return_value_size);
701 #if 0 /* keep Emacsens' auto-indent happy */
708 /* ifndef GNUNET_PSYC_SERVICE_H */
710 /* end of gnunet_psyc_service.h */