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 * - rename "channel" to "master"
77 * - rename "member" to "slave"
80 #ifndef GNUNET_PSYC_SERVICE_H
81 #define GNUNET_PSYC_SERVICE_H
86 #if 0 /* keep Emacsens' auto-indent happy */
91 #include "gnunet_util_lib.h"
92 #include "gnunet_multicast_service.h"
96 * Version number of GNUnet-PSYC API.
98 #define GNUNET_PSYC_VERSION 0x00000000
102 * Information flags for data fragments set via PSYC.
104 enum GNUNET_PSYC_FragmentStatus
107 * This is the first part of data for the given method call.
109 GNUNET_PSYC_FS_FIRST = 1,
112 * This is the last part of data for the given method call.
114 GNUNET_PSYC_FS_LAST = 2,
117 * OR'ed flags if payload is not fragmented.
119 GNUNET_PSYC_FS_NOT_FRAGMENTED = (GNUNET_PSYC_FS_FIRST | GNUNET_PSYC_FS_LAST)
124 * Method called from PSYC upon receiving a message indicating a call
127 * @param cls Closure.
128 * @param full_method_name Original method name from PSYC (may be more
129 * specific than the registered method name due to try-and-slice matching).
130 * @param sender Who transmitted the message (origin, except for messages
131 * from one of the members to the origin).
132 * @param message_id Unique message counter for this message;
133 * (unique only in combination with the given sender for
135 * @param group_generation Group generation counter for this message
136 * (always zero for messages from members to channel owner); FIXME: needed?
137 * @param data_off Byte offset of @a data in the overall data of the method.
138 * @param data_size Number of bytes in @a data.
139 * @param data Data stream given to the method (might not be zero-terminated
140 * if data is binary).
141 * @param frag Fragmentation status for the data.
143 typedef int (*GNUNET_PSYC_Method)(void *cls,
144 const char *full_method_name,
145 const struct GNUNET_PeerIdentity *sender,
147 uint64_t group_generation,
151 enum GNUNET_PSYC_FragmentStatus frag);
155 * Handle for the channel of a PSYC group.
157 struct GNUNET_PSYC_Channel;
161 * Create a PSYC channel.
163 * Will create a multicast group identified by the given ECC key. Messages
164 * received from group members will be given to the respective handler methods.
165 * If a new member wants to join a group, the "join" method handler will be
166 * invoked; the join handler must then generate a "join" message to approve the
167 * joining of the new member. The channel can also change group membership
168 * without explicit requests. Note that PSYC doesn't itself "understand" join
169 * or leave messages, the respective methods must call other PSYC functions to
170 * inform PSYC about the meaning of the respective events.
172 * @param cfg Configuration to use (to connect to PSYC service).
173 * @param method Function to invoke on messages received from members,
174 * typcially at least contains functions for @e join and @e leave.
175 * @param method_cls Closure for @a method.
176 * @param priv_key ECC key that will be used to sign messages for this
177 * PSYC session; public key is used to identify the
178 * PSYC group; FIXME: we'll likely want to use
179 * NOT the p521 curve here, but a cheaper one in the future
180 * Note that end-users will usually not use the private key
181 * directly, but rather look it up in GADS for groups
182 * managed by other users, or select a file with the private
183 * key(s) when setting up their own channels
184 * @param join_policy What is the membership policy of the group?
185 * Used to automate group management decisions.
186 * @return Handle for the channel, NULL on error.
188 struct GNUNET_PSYC_Channel *
189 GNUNET_PSYC_channel_create (const struct GNUNET_CONFIGURATION_Handle *cfg,
190 GNUNET_PSYC_Method method,
192 const struct GNUNET_CRYPTO_EccPrivateKey *priv_key,
193 enum GNUNET_MULTICAST_JoinPolicy join_policy);
197 * Possible operations on PSYC state (persistent) and variables (per message).
199 enum GNUNET_PSYC_Operator
202 * Replace the full state with the new value ("=").
204 GNUNET_PSYC_SOT_SET_STATE = 0,
207 * Delete the complete entry from the state (given data must be
208 * empty). Equivalent to @a SET with empty data, but more
211 GNUNET_PSYC_SOT_DELETE = 0,
214 * Set the value of a variable to a new value (":").
216 GNUNET_PSYC_SOT_SET_VARIABLE,
219 * Add the given value to the set of values in the state ("+").
221 GNUNET_PSYC_SOT_ADD_STATE,
224 * Remove the given value from the set of values in the state ("-").
226 GNUNET_PSYC_SOT_REMOVE_STATE
232 * Update channel state (or set a variable).
234 * The state of a channel must fit into the memory of each member (and the
235 * channel); large values that require streaming must only be passed as the
236 * stream arguments to methods. State updates might not be transmitted to group
237 * members until the next call to GNUNET_PSYC_channel_notify_transmit_ready().
238 * Variable updates must be given just before the call to the respective method
239 * that needs the variables.
241 * @param channel Handle to the PSYC group / channel.
242 * @param full_state_name Name of the field in the channel state to change.
243 * @param type Kind of update operation (add, remove, replace, delete).
244 * @param data_size Number of bytes in data.
245 * @param data New state value.
246 * @return #GNUNET_OK on success, #GNUNET_SYSERR on internal error
247 * (i.e. state too large).
250 GNUNET_PSYC_channel_state_update (struct GNUNET_PSYC_Channel *channel,
251 const char *full_state_name,
252 enum GNUNET_PSYC_Operator type,
258 * Function called to provide data for a transmission via PSYC.
260 * Note that returning #GNUNET_OK or #GNUNET_SYSERR (but not #GNUNET_NO)
261 * invalidates the respective transmission handle.
263 * @param cls Closure.
264 * @param message_id Set to the unique message ID that was generated for
266 * @param group_generation Set to the group generation used for this
268 * @param data_size[in,out] Initially set to the number of bytes available in @a data,
269 * should be set to the number of bytes written to data (IN/OUT).
270 * @param data[out] Where to write the body of the message to give to the method;
271 * function must copy at most @a *data_size bytes to @a data.
272 * @return #GNUNET_SYSERR on error (fatal, aborts transmission)
273 * #GNUNET_NO on success, if more data is to be transmitted later
274 * (should be used if @a *data_size was not big enough to take all the data)
275 * #GNUNET_YES if this completes the transmission (all data supplied)
277 typedef int (*GNUNET_PSYC_ChannelReadyNotify)(void *cls,
279 uint64_t group_generation,
285 * Handle for a pending PSYC transmission operation.
287 struct GNUNET_PSYC_ChannelTransmitHandle;
291 * Send a message to call a method to all members in the PSYC channel.
293 * @param channel Handle to the PSYC multicast group.
294 * @param increment_group_generation #GNUNET_YES if we need to increment
295 * the group generation counter after transmitting this message.
296 * @param full_method_name Which method should be invoked.
297 * @param notify Function to call to obtain the arguments.
298 * @param notify_cls Closure for @a notify.
299 * @return Transmission handle, NULL on error (i.e. more than one request queued).
301 struct GNUNET_PSYC_ChannelTransmitHandle *
302 GNUNET_PSYC_channel_notify_transmit_ready (struct GNUNET_PSYC_Channel *channel,
303 int increment_group_generation,
304 const char *full_method_name,
305 GNUNET_PSYC_ChannelReadyNotify notify,
310 * Abort transmission request to channel.
312 * @param th Handle of the request that is being aborted.
315 GNUNET_PSYC_channel_notify_transmit_ready_cancel (struct GNUNET_PSYC_ChannelTransmitHandle *th);
319 * Destroy a PSYC channel.
321 * @param channel PSYC channel to terminate.
324 GNUNET_PSYC_channel_destroy (struct GNUNET_PSYC_Channel *channel);
328 * Handle to access PSYC group operations for all members.
330 struct GNUNET_PSYC_Group;
334 * Convert @a channel to a @e group handle to access the @e group APIs.
336 * @param channel Channel handle.
337 * @return Group handle, valid for as long as @a channel is valid.
339 struct GNUNET_PSYC_Group *
340 GNUNET_PSYC_channel_get_group (struct GNUNET_PSYC_Channel *channel);
344 * Convert @a member to a @e group handle to access the @e group APIs.
346 * @param member Membership handle.
347 * @return Group handle, valid for as long as @a member is valid.
349 struct GNUNET_PSYC_Group *
350 GNUNET_PSYC_member_get_group (struct GNUNET_PSYC_Member *member);
354 * Add a member to the group.
356 * Note that this will NOT generate any PSYC traffic, it will merely update the
357 * local data base to modify how we react to <em>membership test</em> queries. The
358 * channel still needs to explicitly transmit a @e join message to notify other
359 * group members and they then also must still call this function in their
360 * respective methods handling the @e join message. This way, how @e join and
361 * @e leave operations are exactly implemented is still up to the application;
362 * for example, there might be a @e leave_all method to kick out everyone.
364 * Note that group members are explicitly trusted to execute such
365 * methods correctly; not doing so correctly will result in either
366 * denying members access or offering access to group data to
369 * @param group Group handle.
370 * @param member Which peer to add.
371 * @param message_id Message ID for the message that changed the membership.
372 * @param group_generation The generation ID where the change went into effect.
375 GNUNET_PSYC_group_member_add (struct GNUNET_PSYC_Group *group,
376 const struct GNUNET_PeerIdentity *member,
378 uint64_t group_generation);
382 * Remove a member from the group.
384 * Note that this will NOT generate any PSYC traffic, it will merely update the
385 * local data base to modify how we react to <em>membership test</em> queries. The
386 * channel still needs to explicitly transmit a @e leave message to notify other
387 * group members and they then also must still call this function in their
388 * respective methods handling the @e leave message. This way, how @e join and
389 * @e leave operations are exactly implemented is still up to the application;
390 * for example, there might be a @e leave_all message to kick out everyone.
392 * Note that group members are explicitly trusted to perform these
393 * operations correctly; not doing so correctly will result in either
394 * denying members access or offering access to group data to
397 * @param group Group handle.
398 * @param member Which peer to remove.
399 * @param message_id Message ID for the message that changed the membership.
400 * @param group_generation The generation ID where the change went into effect.
403 GNUNET_PSYC_group_member_remove (struct GNUNET_PSYC_Group *group,
404 const struct GNUNET_PeerIdentity *member,
406 uint64_t group_generation);
410 * Function called to inform a member about state changes for a channel.
412 * Note that (for sets) only the delta is communicated, not the full state.
414 * @param cls Closure.
415 * @param full_state_name Full name of the state.
416 * @param type How to interpret the change.
417 * @param state_value Information about the new state.
418 * @param state_value_size Number of bytes in @a state_value.
420 typedef void (*GNUNET_PSYC_StateCallback)(void *cls,
421 const char *full_state_name,
422 enum GNUNET_PSYC_Operator type,
423 const void *state_value,
424 size_t state_value_size);
428 * Descriptor for an event handler handling PSYC state updates.
430 struct GNUNET_PSYC_StateHandler
434 * Name of the state variable this handler calls about, used in try-and-slice matching.
436 const char *state_name;
439 * Function to call whenever the respective state changes.
441 GNUNET_PSYC_StateCallback event_handler;
444 * Closure for the @a event_handler function.
446 void *event_handler_cls;
454 * The entity joining is always the local peer. The user must immediately use
455 * the GNUNET_PSYC_member_send_to_host() (and possibly
456 * GNUNET_PSYC_member_host_variable_set()) functions to transmit a @e join_msg to
457 * the channel; if the join request succeeds, the channel state (and @e recent
458 * method calls) will be replayed to the joining member. There is no explicit
459 * notification on failure (as the channel may simply take days to approve, and
460 * disapproval is simply being ignored).
462 * @param cfg Configuration to use.
463 * @param pub_key ECC key that identifies the channel we wish to join
464 * @param method Function to invoke on messages received from the channel,
465 * typically at least contains functions for @e join and @e leave.
466 * @param method_cls Closure for @a method.
467 * @param state_count Number of @a state_handlers.
468 * @param state_handlers Array of state event handlers.
469 * @return Handle for the member, NULL on error.
471 struct GNUNET_PSYC_Member *
472 GNUNET_PSYC_member_join (const struct GNUNET_CONFIGURATION_Handle *cfg,
473 const struct GNUNET_CRYPTO_EccPublicKey *pub_key,
474 GNUNET_PSYC_Method method,
476 unsigned int state_count,
477 struct GNUNET_PSYC_StateHandler *state_handlers);
481 * Leave a multicast group.
483 * Will terminate the connection to the PSYC service. Polite clients should
484 * first explicitly send a @e leave request (via
485 * GNUNET_PSYC_member_send_to_host()).
487 * @param member membership handle
490 GNUNET_PSYC_member_leave (struct GNUNET_PSYC_Member *member);
494 * Function called to provide data for a transmission to the channel
495 * owner (aka the @e host of the channel).
497 * Note that returning #GNUNET_OK or #GNUNET_SYSERR (but not #GNUNET_NO)
498 * invalidates the respective transmission handle.
500 * @param cls Closure.
501 * @param data_size[in,out] Initially set to the number of bytes available in @a data,
502 * should be set to the number of bytes written to data (IN/OUT).
503 * @param data[out] Where to write the body of the message to give to the method;
504 * function must copy at most @a *data_size bytes to @a data.
505 * @return #GNUNET_SYSERR on error (fatal, aborts transmission).
506 * #GNUNET_NO on success, if more data is to be transmitted later.
507 * #GNUNET_YES if this completes the transmission (all data supplied).
509 typedef int (*GNUNET_PSYC_OriginReadyNotify)(void *cls,
515 * Handle for a pending PSYC transmission operation.
517 struct GNUNET_PSYC_OriginTransmitHandle;
521 * Request a message to be sent to the channel origin.
523 * @param member Membership handle.
524 * @param method_name Which (PSYC) method should be invoked (on host).
525 * @param notify Function to call when we are allowed to transmit (to get data).
526 * @param notify_cls Closure for @a notify.
527 * @return Transmission handle, NULL on error (i.e. more than one request queued).
529 struct GNUNET_PSYC_OriginTransmitHandle *
530 GNUNET_PSYC_member_send_to_origin (struct GNUNET_PSYC_Member *member,
531 const char *method_name,
532 GNUNET_PSYC_OriginReadyNotify notify,
537 * Set a (temporary, ":") variable for the next message being transmitted
538 * via GNUNET_PSYC_member_send_to_host().
540 * If GNUNET_PSYC_member_send_to_host() is called and then cancelled, all
541 * variables that were set using this function will be unset (lost/forgotten).
542 * To clear a variable state after setting it, you can also call this function
543 * again with NULL/0 for the @a value.
545 * @param member Membership handle.
546 * @param variable_name Name of the variable to set.
547 * @param value Value to set for the given variable.
548 * @param value_size Number of bytes in @a value.
551 GNUNET_PSYC_member_origin_variable_set (struct GNUNET_PSYC_Member *member,
552 const char *variable_name,
558 * Abort transmission request to origin.
560 * @param th Handle of the request that is being aborted.
563 GNUNET_PSYC_member_send_to_origin_cancel (struct GNUNET_PSYC_OriginTransmitHandle *th);
567 * Handle to a story telling operation.
569 struct GNUNET_PSYC_Story;
573 * Request to be told the message history of the channel.
575 * Historic messages (but NOT the state at the time) will be replayed (given to
576 * the normal method handlers) if available and if access is permitted.
578 * @param member Which channel should be replayed?
579 * @param start Earliest interesting point in history.
580 * @param end Last (exclusive) interesting point in history.
581 * @param method Function to invoke on messages received from the story.
582 * @param method_cls Closure for @a method.
583 * @param finish_cb Function to call when the requested story has been fully
584 * told (counting message IDs might not suffice, as some messages
585 * might be secret and thus the listener would not know the story is
586 * finished without being told explicitly); once this function
587 * has been called, the client must not call
588 * GNUNET_PSYC_member_story_tell_cancel() anymore.
589 * @param finish_cb_cls Closure to finish_cb.
590 * @return Handle to cancel story telling operation.
592 struct GNUNET_PSYC_Story *
593 GNUNET_PSYC_member_story_tell (struct GNUNET_PSYC_Member *member,
596 GNUNET_PSYC_Method method,
598 void (*finish_cb)(void *),
599 void *finish_cb_cls);
603 * Abort story telling.
605 * This function must not be called from within method handlers (as given to
606 * GNUNET_PSYC_member_join()) of the member.
608 * @param story Story telling operation to stop.
611 GNUNET_PSYC_member_story_tell_cancel (struct GNUNET_PSYC_Story *story);
615 * Call the given callback on all matching values (including variables) in the
618 * The callback is invoked synchronously on all matching states (as the state is
619 * fully replicated in the library in this process; channel states should be
620 * small, large data is to be passed as streaming data to methods).
622 * A name matches if it includes the @a state_name prefix, thus requesting the
623 * empty state ("") will match all values; requesting "_a_b" will also return
624 * values stored under "_a_b_c".
626 * @param member Membership handle.
627 * @param state_name Name of the state to query (full name
628 * might be longer, this is only the prefix that must match).
629 * @param cb Function to call on the matching state values.
630 * @param cb_cls Closure for @a cb.
631 * @return Message ID for which the state was returned (last seen
635 GNUNET_PSYC_member_state_get_all (struct GNUNET_PSYC_Member *member,
636 const char *state_name,
637 GNUNET_PSYC_StateCallback cb,
642 * Obtain the current value of the best-matching value in the state
643 * (including variables).
645 * Note that variables are only valid during a #GNUNET_PSYC_Method invocation, as
646 * variables are only valid for the duration of a method invocation.
648 * If the requested variable name does not have an exact state in
649 * the state, the nearest less-specific name is matched; for example,
650 * requesting "_a_b" will match "_a" if "_a_b" does not exist.
652 * @param member Membership handle.
653 * @param variable_name Name of the variable to query.
654 * @param return_value_size Set to number of bytes in variable,
655 * needed as variables might contain binary data and
656 * might also not be 0-terminated; set to 0 on errors.
657 * @return NULL on error (no matching state or variable), pointer
658 to the respective value otherwise.
661 GNUNET_PSYC_member_state_get (struct GNUNET_PSYC_Member *member,
662 const char *variable_name,
663 size_t *return_value_size);
667 #if 0 /* keep Emacsens' auto-indent happy */
674 /* ifndef GNUNET_PSYC_SERVICE_H */
676 /* end of gnunet_psyc_service.h */