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
33 * - this API does not know about psyc's "root" and "places";
34 * there is no 'root' in GNUnet-Psyc as we're decentralized;
35 * 'places' and 'persons' are combined within the same
36 * abstraction, that of a "channel". Channels are identified
37 * and accessed in this API using a public/private key.
38 * Higher-level applications should use NAMES within GADS
39 * to obtain public keys, and the distinction between
40 * 'places' and 'persons' can then be made with the help
41 * of the naming system (and/or conventions).
42 * Channels are (as in PSYC) organized into a hierarchy; each
43 * channel owner (the one with the private key) is then
44 * the operator of the multicast group (its Origin in
45 * the terminology of the multicast API).
46 * - The API supports passing large amounts of data using
47 * 'streaming' for the argument passed to a method. State
48 * and variables must fit into memory and cannot be streamed
49 * (thus, no passing of 4 GB of data in a variable;
50 * once we implement this, we might want to create a
51 * @c #define for the maximum size of a variable).
52 * - PSYC defines standard variables, methods, etc. This
53 * library deliberately abstracts over all of these; a
54 * higher-level API should combine the naming system (GADS)
55 * and standard methods (message, join, leave, warn,
56 * fail, error) and variables (action, color, time,
57 * tag, etc.). However, this API does take over the
58 * routing variables, specifically 'context' (channel),
59 * and 'source'. We only kind-of support 'target', as
60 * the target is either everyone in the group or the
61 * origin, and never just a single member of the group;
62 * for such individual messages, an application needs to
63 * construct an 'inbox' channel where the owner (only)
64 * receives messages (but never forwards; private responses
65 * would be transmitted by joining the senders 'inbox'
66 * channel -- or a inbox#bob subchannel). The
67 * goal for all of this is to keep the abstractions in this
68 * API minimal: interaction with multicast, try \& slice,
69 * state/variable/channel management. Higher-level
70 * operations belong elsewhere (so maybe this API should
71 * be called 'PSYC-low', whereas a higher-level API
72 * implementing defaults for standard methods and
73 * variables might be called 'PSYC-std' or 'PSYC-high'.
76 #ifndef GNUNET_PSYC_SERVICE_H
77 #define GNUNET_PSYC_SERVICE_H
82 #if 0 /* keep Emacsens' auto-indent happy */
87 #include "gnunet_util_lib.h"
88 #include "gnunet_multicast_service.h"
92 * Version number of GNUnet-PSYC API.
94 #define GNUNET_PSYC_VERSION 0x00000000
98 * Information flags for data fragments set via PSYC.
100 enum GNUNET_PSYC_FragmentStatus
103 * This is the first part of data for the given method call.
105 GNUNET_PSYC_FS_FIRST = 1,
108 * This is the last part of data for the given method call.
110 GNUNET_PSYC_FS_LAST = 2,
113 * OR'ed flags if payload is not fragmented.
115 GNUNET_PSYC_FS_NOT_FRAGMENTED = (GNUNET_PSYC_FS_FIRST | GNUNET_PSYC_FS_LAST)
120 * Method called from PSYC upon receiving a message indicating a call
123 * @param cls Closure.
124 * @param full_method_name Original method name from PSYC (may be more
125 * specific than the registered method name due to try-and-slice matching).
126 * @param sender Who transmitted the message (origin, except for messages
127 * from one of the members to the origin).
128 * @param message_id Unique message counter for this message;
129 * (unique only in combination with the given sender for
131 * @param group_generation Group generation counter for this message
132 * (always zero for messages from members to channel owner); FIXME: needed?
133 * @param data_off Byte offset of @a data in the overall data of the method.
134 * @param data_size Number of bytes in @a data.
135 * @param data Data stream given to the method (might not be zero-terminated
136 * if data is binary).
137 * @param frag Fragmentation status for the data.
139 typedef int (*GNUNET_PSYC_Method)(void *cls,
140 const char *full_method_name,
141 const struct GNUNET_PeerIdentity *sender,
143 uint64_t group_generation,
147 enum GNUNET_PSYC_FragmentStatus frag);
151 * Handle for the channel of a PSYC group.
153 struct GNUNET_PSYC_Channel;
157 * Create a PSYC channel.
159 * Will create a multicast group identified by the given ECC key. Messages
160 * received from group members will be given to the respective handler methods.
161 * If a new member wants to join a group, the "join" method handler will be
162 * invoked; the join handler must then generate a "join" message to approve the
163 * joining of the new member. The channel can also change group membership
164 * without explicit requests. Note that PSYC doesn't itself "understand" join
165 * or leave messages, the respective methods must call other PSYC functions to
166 * inform PSYC about the meaning of the respective events.
168 * @param cfg Configuration to use (to connect to PSYC service).
169 * @param method Function to invoke on messages received from members,
170 * typcially at least contains functions for @e join and @e leave.
171 * @param method_cls Closure for @a method.
172 * @param priv_key ECC key that will be used to sign messages for this
173 * PSYC session; public key is used to identify the
174 * PSYC group; FIXME: we'll likely want to use
175 * NOT the p521 curve here, but a cheaper one in the future
176 * Note that end-users will usually not use the private key
177 * directly, but rather look it up in GADS for groups
178 * managed by other users, or select a file with the private
179 * key(s) when setting up their own channels
180 * @param join_policy What is the membership policy of the group?
181 * Used to automate group management decisions.
182 * @return Handle for the channel, NULL on error.
184 struct GNUNET_PSYC_Channel *
185 GNUNET_PSYC_channel_create (const struct GNUNET_CONFIGURATION_Handle *cfg,
186 GNUNET_PSYC_Method method,
188 const struct GNUNET_CRYPTO_EccPrivateKey *priv_key,
189 enum GNUNET_MULTICAST_JoinPolicy join_policy);
193 * Possible operations on PSYC state (persistent) and variables (per message).
195 enum GNUNET_PSYC_Operator
198 * Replace the full state with the new value ("=").
200 GNUNET_PSYC_SOT_SET_STATE = 0,
203 * Delete the complete entry from the state (given data must be
204 * empty). Equivalent to @a SET with empty data, but more
207 GNUNET_PSYC_SOT_DELETE = 0,
210 * Set the value of a variable to a new value (":").
212 GNUNET_PSYC_SOT_SET_VARIABLE,
215 * Add the given value to the set of values in the state ("+").
217 GNUNET_PSYC_SOT_ADD_STATE,
220 * Remove the given value from the set of values in the state ("-").
222 GNUNET_PSYC_SOT_REMOVE_STATE
228 * Update channel state (or set a variable).
230 * The state of a channel must fit into the memory of each member (and the
231 * channel); large values that require streaming must only be passed as the
232 * stream arguments to methods. State updates might not be transmitted to group
233 * members until the next call to GNUNET_PSYC_channel_notify_transmit_ready().
234 * Variable updates must be given just before the call to the respective method
235 * that needs the variables.
237 * @param channel Handle to the PSYC group / channel.
238 * @param full_state_name Name of the field in the channel state to change.
239 * @param type Kind of update operation (add, remove, replace, delete).
240 * @param data_size Number of bytes in data.
241 * @param data New state value.
242 * @return #GNUNET_OK on success, #GNUNET_SYSERR on internal error
243 * (i.e. state too large).
246 GNUNET_PSYC_channel_state_update (struct GNUNET_PSYC_Channel *channel,
247 const char *full_state_name,
248 enum GNUNET_PSYC_Operator type,
254 * Function called to provide data for a transmission via PSYC.
256 * Note that returning #GNUNET_OK or #GNUNET_SYSERR (but not #GNUNET_NO)
257 * invalidates the respective transmission handle.
259 * @param cls Closure.
260 * @param message_id Set to the unique message ID that was generated for
262 * @param group_generation Set to the group generation used for this
264 * @param data_size[in,out] 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 data[out] 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)
273 typedef int (*GNUNET_PSYC_ChannelReadyNotify)(void *cls,
275 uint64_t group_generation,
281 * Handle for a pending PSYC transmission operation.
283 struct GNUNET_PSYC_ChannelTransmitHandle;
287 * Send a message to call a method to all members in the PSYC channel.
289 * @param channel Handle to the PSYC multicast group.
290 * @param increment_group_generation #GNUNET_YES if we need to increment
291 * the group generation counter after transmitting this message.
292 * @param full_method_name Which method should be invoked.
293 * @param notify Function to call to obtain the arguments.
294 * @param notify_cls Closure for @a notify.
295 * @return Transmission handle, NULL on error (i.e. more than one request queued).
297 struct GNUNET_PSYC_ChannelTransmitHandle *
298 GNUNET_PSYC_channel_notify_transmit_ready (struct GNUNET_PSYC_Channel *channel,
299 int increment_group_generation,
300 const char *full_method_name,
301 GNUNET_PSYC_ChannelReadyNotify notify,
306 * Abort transmission request to channel.
308 * @param th Handle of the request that is being aborted.
311 GNUNET_PSYC_channel_notify_transmit_ready_cancel (struct GNUNET_PSYC_ChannelTransmitHandle *th);
315 * Destroy a PSYC channel.
317 * @param channel PSYC channel to terminate.
320 GNUNET_PSYC_channel_destroy (struct GNUNET_PSYC_Channel *channel);
324 * Handle to access PSYC group operations for all members.
326 struct GNUNET_PSYC_Group;
330 * Convert @a channel to a @e group handle to access the @e group APIs.
332 * @param channel Channel handle.
333 * @return Group handle, valid for as long as @a channel is valid.
335 struct GNUNET_PSYC_Group *
336 GNUNET_PSYC_channel_get_group (struct GNUNET_PSYC_Channel *channel);
340 * Convert @a member to a @e group handle to access the @e group APIs.
342 * @param member Membership handle.
343 * @return Group handle, valid for as long as @a member is valid.
345 struct GNUNET_PSYC_Group *
346 GNUNET_PSYC_member_get_group (struct GNUNET_PSYC_Member *member);
350 * Add a member to the group.
352 * Note that this will NOT generate any PSYC traffic, it will merely update the
353 * local data base to modify how we react to <em>membership test</em> queries. The
354 * channel still needs to explicitly transmit a @e join message to notify other
355 * group members and they then also must still call this function in their
356 * respective methods handling the @e join message. This way, how @e join and
357 * @e leave operations are exactly implemented is still up to the application;
358 * for example, there might be a @e leave_all method to kick out everyone.
360 * Note that group members are explicitly trusted to execute such
361 * methods correctly; not doing so correctly will result in either
362 * denying members access or offering access to group data to
365 * @param group Group handle.
366 * @param member Which peer to add.
367 * @param message_id Message ID for the message that changed the membership.
368 * @param group_generation The generation ID where the change went into effect.
371 GNUNET_PSYC_group_member_add (struct GNUNET_PSYC_Group *group,
372 const struct GNUNET_PeerIdentity *member,
374 uint64_t group_generation);
378 * Remove a member from the group.
380 * Note that this will NOT generate any PSYC traffic, it will merely update the
381 * local data base to modify how we react to <em>membership test</em> queries. The
382 * channel still needs to explicitly transmit a @e leave message to notify other
383 * group members and they then also must still call this function in their
384 * respective methods handling the @e leave message. This way, how @e join and
385 * @e leave operations are exactly implemented is still up to the application;
386 * for example, there might be a @e leave_all message to kick out everyone.
388 * Note that group members are explicitly trusted to perform these
389 * operations correctly; not doing so correctly will result in either
390 * denying members access or offering access to group data to
393 * @param group Group handle.
394 * @param member Which peer to remove.
395 * @param message_id Message ID for the message that changed the membership.
396 * @param group_generation The generation ID where the change went into effect.
399 GNUNET_PSYC_group_member_remove (struct GNUNET_PSYC_Group *group,
400 const struct GNUNET_PeerIdentity *member,
402 uint64_t group_generation);
406 * Function called to inform a member about state changes for a channel.
408 * Note that (for sets) only the delta is communicated, not the full state.
410 * @param cls Closure.
411 * @param full_state_name Full name of the state.
412 * @param type How to interpret the change.
413 * @param state_value Information about the new state.
414 * @param state_value_size Number of bytes in @a state_value.
416 typedef void (*GNUNET_PSYC_StateCallback)(void *cls,
417 const char *full_state_name,
418 enum GNUNET_PSYC_Operator type,
419 const void *state_value,
420 size_t state_value_size);
424 * Descriptor for an event handler handling PSYC state updates.
426 struct GNUNET_PSYC_StateHandler
430 * Name of the state variable this handler calls about, used in try-and-slice matching.
432 const char *state_name;
435 * Function to call whenever the respective state changes.
437 GNUNET_PSYC_StateCallback event_handler;
440 * Closure for the @a event_handler function.
442 void *event_handler_cls;
450 * The entity joining is always the local peer. The user must immediately use
451 * the GNUNET_PSYC_member_send_to_host() (and possibly
452 * GNUNET_PSYC_member_host_variable_set()) functions to transmit a @e join_msg to
453 * the channel; if the join request succeeds, the channel state (and @e recent
454 * method calls) will be replayed to the joining member. There is no explicit
455 * notification on failure (as the channel may simply take days to approve, and
456 * disapproval is simply being ignored).
458 * @param cfg Configuration to use.
459 * @param pub_key ECC key that identifies the channel we wish to join
460 * @param method Function to invoke on messages received from the channel,
461 * typically at least contains functions for @e join and @e leave.
462 * @param method_cls Closure for @a method.
463 * @param state_count Number of @a state_handlers.
464 * @param state_handlers Array of state event handlers.
465 * @return Handle for the member, NULL on error.
467 struct GNUNET_PSYC_Member *
468 GNUNET_PSYC_member_join (const struct GNUNET_CONFIGURATION_Handle *cfg,
469 const struct GNUNET_CRYPTO_EccPublicKey *pub_key,
470 GNUNET_PSYC_Method method,
472 unsigned int state_count,
473 struct GNUNET_PSYC_StateHandler *state_handlers);
477 * Leave a multicast group.
479 * Will terminate the connection to the PSYC service. Polite clients should
480 * first explicitly send a @e leave request (via
481 * GNUNET_PSYC_member_send_to_host()).
483 * @param member membership handle
486 GNUNET_PSYC_member_leave (struct GNUNET_PSYC_Member *member);
490 * Function called to provide data for a transmission to the channel
491 * owner (aka the @e host of the channel).
493 * Note that returning #GNUNET_OK or #GNUNET_SYSERR (but not #GNUNET_NO)
494 * invalidates the respective transmission handle.
496 * @param cls Closure.
497 * @param data_size[in,out] Initially set to the number of bytes available in @a data,
498 * should be set to the number of bytes written to data (IN/OUT).
499 * @param data[out] Where to write the body of the message to give to the method;
500 * function must copy at most @a *data_size bytes to @a data.
501 * @return #GNUNET_SYSERR on error (fatal, aborts transmission).
502 * #GNUNET_NO on success, if more data is to be transmitted later.
503 * #GNUNET_YES if this completes the transmission (all data supplied).
505 typedef int (*GNUNET_PSYC_OriginReadyNotify)(void *cls,
511 * Handle for a pending PSYC transmission operation.
513 struct GNUNET_PSYC_OriginTransmitHandle;
517 * Request a message to be sent to the channel origin.
519 * @param member Membership handle.
520 * @param method_name Which (PSYC) method should be invoked (on host).
521 * @param notify Function to call when we are allowed to transmit (to get data).
522 * @param notify_cls Closure for @a notify.
523 * @return Transmission handle, NULL on error (i.e. more than one request queued).
525 struct GNUNET_PSYC_OriginTransmitHandle *
526 GNUNET_PSYC_member_send_to_origin (struct GNUNET_PSYC_Member *member,
527 const char *method_name,
528 GNUNET_PSYC_OriginReadyNotify notify,
533 * Set a (temporary, ":") variable for the next message being transmitted
534 * via GNUNET_PSYC_member_send_to_host().
536 * If GNUNET_PSYC_member_send_to_host() is called and then cancelled, all
537 * variables that were set using this function will be unset (lost/forgotten).
538 * To clear a variable state after setting it, you can also call this function
539 * again with NULL/0 for the @a value.
541 * @param member Membership handle.
542 * @param variable_name Name of the variable to set.
543 * @param value Value to set for the given variable.
544 * @param value_size Number of bytes in @a value.
547 GNUNET_PSYC_member_origin_variable_set (struct GNUNET_PSYC_Member *member,
548 const char *variable_name,
554 * Abort transmission request to origin.
556 * @param th Handle of the request that is being aborted.
559 GNUNET_PSYC_member_send_to_origin_cancel (struct GNUNET_PSYC_OriginTransmitHandle *th);
563 * Handle to a story telling operation.
565 struct GNUNET_PSYC_Story;
569 * Request to be told the message history of the channel.
571 * Historic messages (but NOT the state at the time) will be replayed (given to
572 * the normal method handlers) if available and if access is permitted.
574 * @param member Which channel should be replayed?
575 * @param start Earliest interesting point in history.
576 * @param end Last (exclusive) interesting point in history.
577 * @param method Function to invoke on messages received from the story.
578 * @param method_cls Closure for @a method.
579 * @param finish_cb Function to call when the requested story has been fully
580 * told (counting message IDs might not suffice, as some messages
581 * might be secret and thus the listener would not know the story is
582 * finished without being told explicitly); once this function
583 * has been called, the client must not call
584 * GNUNET_PSYC_member_story_tell_cancel() anymore.
585 * @param finish_cb_cls Closure to finish_cb.
586 * @return Handle to cancel story telling operation.
588 struct GNUNET_PSYC_Story *
589 GNUNET_PSYC_member_story_tell (struct GNUNET_PSYC_Member *member,
592 GNUNET_PSYC_Method method,
594 void (*finish_cb)(void *),
595 void *finish_cb_cls);
599 * Abort story telling.
601 * This function must not be called from within method handlers (as given to
602 * GNUNET_PSYC_member_join()) of the member.
604 * @param story Story telling operation to stop.
607 GNUNET_PSYC_member_story_tell_cancel (struct GNUNET_PSYC_Story *story);
611 * Call the given callback on all matching values (including variables) in the
614 * The callback is invoked synchronously on all matching states (as the state is
615 * fully replicated in the library in this process; channel states should be
616 * small, large data is to be passed as streaming data to methods).
618 * A name matches if it includes the @a state_name prefix, thus requesting the
619 * empty state ("") will match all values; requesting "a_b" will also return
620 * values stored under "a_b_c".
622 * @param member Membership handle.
623 * @param state_name Name of the state to query (full name
624 * might be longer, this is only the prefix that must match).
625 * @param cb Function to call on the matching state values.
626 * @param cb_cls Closure for @a cb.
627 * @return Message ID for which the state was returned (last seen
631 GNUNET_PSYC_member_state_get_all (struct GNUNET_PSYC_Member *member,
632 const char *state_name,
633 GNUNET_PSYC_StateCallback cb,
638 * Obtain the current value of the best-matching value in the state
639 * (including variables).
641 * Note that variables are only valid during a #GNUNET_PSYC_Method invocation, as
642 * variables are only valid for the duration of a method invocation.
644 * If the requested variable name does not have an exact state in
645 * the state, the nearest less-specific name is matched; for example,
646 * requesting "a_b" will match "a" if "a_b" does not exist.
648 * @param member Membership handle.
649 * @param variable_name Name of the variable to query.
650 * @param return_value_size Set to number of bytes in variable,
651 * needed as variables might contain binary data and
652 * might also not be 0-terminated; set to 0 on errors.
653 * @return NULL on error (no matching state or variable), pointer
654 to the respective value otherwise.
657 GNUNET_PSYC_member_state_get (struct GNUNET_PSYC_Member *member,
658 const char *variable_name,
659 size_t *return_value_size);
663 #if 0 /* keep Emacsens' auto-indent happy */
670 /* ifndef GNUNET_PSYC_SERVICE_H */
672 /* end of gnunet_psyc_service.h */