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 * #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 * Method called from PSYC upon receiving a message indicating a call
102 * @param full_method_name original method name from PSYC (may be more
103 * specific than the registered method name due to try-and-slice matching)
104 * @param sender who transmitted the message (origin, except for messages
105 * from one of the members to the origin)
106 * @param message_id unique message counter for this message;
107 * (unique only in combination with the given sender for
109 * @param group_generation group generation counter for this message
110 * (always zero for messages from members to channel owner)
111 * @param data_size number of bytes in 'data'
112 * @param data data stream given to the method (might not be zero-terminated
115 typedef int (*GNUNET_PSYC_Method)(void *cls,
116 const char *full_method_name,
117 const struct GNUNET_PeerIdentity *sender,
119 uint64_t group_generation,
125 * Handle for the channel of a PSYC group.
127 struct GNUNET_PSYC_Channel;
131 * Start a PSYC channel. Will create a multicast group identified by
132 * the given public key. Messages recevied from group members will be
133 * given to the respective handler methods. If a new member wants to
134 * join a group, the "join" method handler will be invoked; the join
135 * handler must then generate a "join" message to approve the joining
136 * of the new member. The channel can also change group membership
137 * without explicit requests. Note that PSYC doesn't itself "understand"
138 * join or leave messages, the respective methods must call other
139 * PSYC functions to inform PSYC about the meaning of the respective
142 * @param cfg configuration to use (to connect to PSYC service)
143 * @param parent parent channel, NULL for top-level channels
144 * @param name name of the channel, only important if this is a subchannel
145 * @param method_count number of methods in 'methods' array
146 * @param methods functions to invoke on messages received from members,
147 * typcially at least contains functions for 'join' and 'leave'.
148 * @param priv_key ECC key that will be used to sign messages for this
149 * PSYC session; public key is used to identify the
150 * PSYC group; FIXME: we'll likely want to use
151 * NOT the p521 curve here, but a cheaper one in the future
152 * Note that end-users will usually not use the private key
153 * directly, but rather look it up in GADS for groups
154 * managed by other users, or select a file with the private
155 * key(s) when setting up their own channels
156 * @param join_policy what is the membership policy of the group?
157 * Used to automate group management decisions.
158 * @return handle for the channel, NULL on error
160 struct GNUNET_PSYC_Channel *
161 GNUNET_PSYC_channel_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
162 struct GNUNET_PSYC_Channel *parent,
164 unsigned int method_count,
165 const struct GNUNET_PSYC_Method *methods,
166 const struct GNUNET_CRYPTO_EccPrivateKey *priv_key,
167 enum GNUNET_MULTICAST_JoinPolicy join_policy);
171 * Possible operations on PSYC state (persistent) and variables (per message).
173 enum GNUNET_PSYC_Operator
176 * Replace the full state with the new value ("=").
178 GNUNET_PSYC_SOT_SET_STATE = 0,
181 * Delete the complete entry from the state (given data must be
182 * empty). Equivalent to 'SET' with emtpy data, but more
185 GNUNET_PSYC_SOT_DELETE = 0,
188 * Set the value of a variable to a new value (":").
190 GNUNET_PSYC_SOT_SET_VARIABLE,
193 * Add the given value to the set of values in the state ("+").
195 GNUNET_PSYC_SOT_ADD_STATE,
198 * Remove the given value from the set of values in the state ("-").
200 GNUNET_PSYC_SOT_REMOVE_STATE
206 * Update channel state or variables. The state of a channel must fit
207 * into the memory of each member (and the channel); large values that
208 * require streaming must only be passed as the stream arguments to
209 * methods. State updates might not be transmitted to group members
210 * until the next call to 'GNUNET_PSYC_channel_broadcast_call_method'.
211 * Variable updates must be given just before the call to the
212 * respective method that needs the variables.
214 * @param channel handle to the PSYC group / channel
215 * @param full_state_name name of the field in the channel state to change
216 * @param type kind of update operation (add, remove, replace, delete)
217 * @param data_size number of bytes in data
218 * @param data new state value
219 * @return GNUNET_OK on success, GNUNET_SYSERR on internal error
220 * (i.e. state too large)
223 GNUNET_PSYC_channel_update (struct GNUNET_PSYC_Channel *channel,
224 const char *full_state_name,
225 enum GNUNET_PSYC_Operator type,
231 * Function called to provide data for a transmission via PSYC. Note
232 * that returning GNUNET_OK or GNUNET_SYSERR (but not GNUNET_NO)
233 * invalidates the respective transmission handle.
236 * @param message_id set to the unique message ID that was generated for
238 * @param group_generation set to the group generation used for this
240 * @param data_size initially set to the number of bytes available in 'data',
241 * should be set to the number of bytes written to data (IN/OUT)
242 * @param data where to write the body of the message to give to the method;
243 * function must copy at most '*data_size' bytes to 'data'.
244 * @return GNUNET_SYSERR on error (fatal, aborts transmission)
245 * GNUNET_NO on success, if more data is to be transmitted later
246 * (should be used if 'data_size' was not big enough to take all the data)
247 * GNUNET_OK if this completes the transmission (all data supplied)
249 typedef int (*GNUNET_PSYC_ChannelReadyNotify)(void *cls,
251 uint64_t group_generation,
257 * Handle for a pending PSYC transmission operation.
259 struct GNUNET_PSYC_ChannelTransmitHandle;
263 * Send a message to call a method to all members in the PSYC channel
264 * (and all parent channels if this is a subchannel).
266 * @param channel handle to the PSYC multicast group
267 * @param increment_group_generation GNUNET_YES if we need to increment
268 * the group generation counter after transmitting this message
269 * @param full_method_name which method should be invoked
270 * @param notify function to call to obtain the arguments
271 * @param notify_cls closure for 'notify'
272 * @return transmission handle, NULL on error (i.e. more than one request queued)
274 struct GNUNET_PSYC_ChannelTransmitHandle *
275 GNUNET_PSYC_channel_notify_transmit_ready (struct GNUNET_PSYC_Channel *channel,
276 int increment_group_generation,
277 const char *full_method_name,
278 GNUNET_PSYC_ChannelReadyNotify notify,
283 * Abort transmission request to channel.
285 * @param th handle of the request that is being aborted
288 GNUNET_PSYC_channel_notify_transmit_ready_cancel (struct GNUNET_PSYC_ChannelTransmitHandle *th);
292 * End a PSYC channel. Note that subchannels MUST be ended before
295 * @param channel PSYC channel to terminate
298 GNUNET_PSYC_channel_end (struct GNUNET_PSYC_Channel *channel);
302 * Handle to access PSYC group operations for all members.
304 struct GNUNET_PSYC_Group;
308 * Convert 'channel' to a 'group' handle to access the 'group' APIs.
310 * @param channel channel handle
311 * @return group handle, valid for as long as 'channel' is valid
313 struct GNUNET_PSYC_Group *
314 GNUNET_PSYC_channel_get_group (struct GNUNET_PSYC_Channel *channel);
318 * Add a member to the group. Note that this will NOT generate any
319 * PSYC traffic, it will merely update the local data base to modify
320 * how we react to 'membership test' queries. The channel still needs to
321 * explicitly transmit a 'join' message to notify other group members
322 * and they then also must still call this function in their respective
323 * methods handling the 'join' message. This way, how 'join' and 'leave'
324 * operations are exactly implemented is still up to the application;
325 * for example, there might be a 'leave_all' method to kick out everyone.
327 * Note that group members are explicitly trusted to execute such
328 * methods correctly; not doing so correctly will result in either
329 * denying members access or offering access to group data to
332 * @param group group handle
333 * @param member which peer to add
334 * @param message_id message ID for the message that changed the membership
335 * @param group_generation the generation ID where the change went into effect
338 GNUNET_PSYC_group_member_admit (struct GNUNET_PSYC_Group *group,
339 const struct GNUNET_PeerIdentity *member,
341 uint64_t group_generation);
345 * Remove a member from the group. Note that this will NOT generate any
346 * PSYC traffic, it will merely update the local data base to modify
347 * how we react to 'membership test' queries. The channel still needs to
348 * explicitly transmit a 'leave' message to notify other group members
349 * and they then also must still call this function in their respective
350 * methods handling the 'leave' message. This way, how 'join' and 'leave'
351 * operations are exactly implemented is still up to the application;
352 * for example, there might be a 'leave_all' message to kick out everyone.
354 * Note that group members are explicitly trusted to perform these
355 * operations correctly; not doing so correctly will result in either
356 * denying members access or offering access to group data to
359 * @param group group handle
360 * @param member which peer to remove
361 * @param message_id message ID for the message that changed the membership
362 * @param group_generation the generation ID where the change went into effect
365 GNUNET_PSYC_group_member_kick (struct GNUNET_PSYC_Group *group,
366 const struct GNUNET_PeerIdentity *member,
368 uint64_t group_generation);
372 * Function called to inform a member about state changes for a
373 * channel. Note that (for sets) only the delta is communicated, not
377 * @param full_state_name full name of the state
378 * @param type how to interpret the change
379 * @param state_value information about the new state
381 typedef void (*GNUNET_PSYC_StateCallback)(void *cls,
382 const char *full_state_name,
383 enum GNUNET_PSYC_Operator type,
384 const struct GNUNET_PSYC_Argument *state_value);
388 * Descriptor for an event handler handling PSYC state updates.
390 struct GNUNET_PSYC_StateHandler
394 * Name of the state this handler calls about, used in try-and-slice matching.
396 const char *state_name;
399 * Function to call whenever the respective state changes.
401 GNUNET_PSYC_StateCallback event_handler;
404 * Closure for the 'event_handler' function.
406 void *event_handler_cls;
409 * Description of the kind of state that the handler expects to see.
410 * Non-matching state updates will be ignored (but logged). Note
411 * that the state_types of all states with the same state name
412 * prefix should be identical. For state types, the
413 * 'GNUNET_PSYC_AF_STREAMABLE' and 'GNUNET_PSYC_AF_SET_STREAMABLE'
414 * flags must never be set (as the channel state should be small
415 * enough to (easily) fit into the memory of all PSYC members).
417 struct GNUNET_PSYC_ArgumentDescriptor state_type;
423 * Join a PSYC group. The entity joining is always the local peer.
424 * This will send a 'join_msg' to the channel; if it succeeds, the
425 * channel state (and 'recent' method calls) will be replayed to the
426 * joining member and the 'join' method will be invoked to show that
427 * we joined successfully. There is no explicit notification on
428 * failure (as the channel may simply take days to approve, and
429 * disapproval is simply being ignored).
431 * @param cfg configuration to use
432 * @param pub_key ECC key that identifies the channel we wish to join
433 * @param method_count number of methods in 'methods' array
434 * @param methods functions to invoke on messages received from the channel,
435 * typcially at least contains functions for 'join' and 'leave'.
436 * @param state_count number of state handlers
437 * @param state_handlers array of state event handlers
438 * @param join_msg which method should we invoke on the channel controller
439 * to try to join the channel (i.e. "join")
440 * @param join_cb method to invoke on channel to obtain arguments
441 * for a join method invocation;
442 * use NULL to send nothing (useful for anonymous groups that permit anyone);
443 * arguments to give to join method, must not include streaming args
444 * @param join_cb_cls closure for 'join_cb'
445 * @return handle for the member, NULL on error
447 struct GNUNET_PSYC_Member *
448 GNUNET_PSYC_member_join (const struct GNUNET_CONFIGURATION_Handle *cfg,
449 const struct GNUNET_CRYPTO_EccPublicKey *pub_key,
450 unsigned int method_count,
451 const struct GNUNET_PSYC_Method *methods,
452 unsigned int state_count,
453 struct GNUNET_PSYC_StateHandler *state_handlers,
454 const char *join_method,
455 const struct GNUNET_PSYC_ChannelReadyNotify join_cb,
460 * Function called to provide data for a transmission to the channel
461 * owner (aka the 'host' of the channel). Note that returning
462 * GNUNET_OK or GNUNET_SYSERR (but not GNUNET_NO) invalidates the
463 * respective transmission handle.
466 * @param data_size initially set to the number of bytes available in 'data',
467 * should be set to the number of bytes written to data (IN/OUT)
468 * @param data where to write the body of the message to give to the method;
469 * function must copy at most '*data_size' bytes to 'data'.
470 * @return GNUNET_SYSERR on error (fatal, aborts transmission)
471 * GNUNET_NO on success, if more data is to be transmitted later
472 * GNUNET_OK if this completes the transmission (all data supplied)
474 typedef int (*GNUNET_PSYC_HostReadyNotify)(void *cls,
480 * Handle for a pending PSYC transmission operation.
482 struct GNUNET_PSYC_HostTransmitHandle;
486 * Request a message to be send to the channel.
488 * @param member membership handle
489 * @param request_data which method should be invoked on channel (and how)
490 * @param method_name which method should be invoked
491 * @param argc number of arguments the method takes (size of 'ads' array)
492 * @param ads description of the arguments the method takes
493 * @param notify function to call to obtain the arguments
494 * @param notify_cls closure for 'notify'
495 * @return transmission handle, NULL on error (i.e. more than one request queued)
497 struct GNUNET_PSYC_HostTransmitHandle *
498 GNUNET_PSYC_member_send_to_host (struct GNUNET_PSYC_Member *member,
499 const char *method_name,
500 GNUNET_PSYC_HostReadyNotify notify,
505 * Abort transmission request to host.
507 * @param th handle of the request that is being aborted
510 GNUNET_PSYC_member_send_to_host_cancel (struct GNUNET_PSYC_HostTransmitHandle *th);
514 * Handle to a story telling operation.
516 struct GNUNET_PSYC_Story;
520 * Request to be told the message history of the channel. Historic
521 * messages (but NOT the state at the time) will be replayed (given to
522 * the normal method handlers) if available and if access is
525 * @param member which channel should be replayed?
526 * @param start earliest interesting point in history
527 * @param end last (exclusive) interesting point in history
528 * @param finish_cb function to call when the requested story has been fully
529 * told (counting message IDs might not suffice, as some messages
530 * might be secret and thus the listener would not know the story is
531 * finished without being told explicitly); once this function
532 * has been called, the client must not call
533 * 'GNUNET_PSYC_member_story_tell_cancel' anymore
534 * @param finish_cb_cls closure to finish_cb
535 * @return handle to cancel story telling operation
537 struct GNUNET_PSYC_Story *
538 GNUNET_PSYC_member_story_tell (struct GNUNET_PSYC_Member *member,
541 void (*finish_cb)(void *),
542 void *finish_cb_cls);
546 * Abort story telling. This function must not be called from within
547 * method handlers (as given to 'GNUNET_PSYC_member_join') of the
550 * @param story story telling operation to stop
553 GNUNET_PSYC_member_story_tell_cancel (struct GNUNET_PSYC_Story *story);
557 * Call the given state callback on all matching states in the channel
558 * state. The callback is invoked synchronously on all matching
559 * states (as the state is fully replicated in the library in this
560 * process; channel states should be small, large data is to be passed
561 * as streaming data to methods).
563 * @param member membership handle
564 * @param state_name name of the state to query (full name
565 * might be longer, this is only the prefix that must match)
566 * @param cb function to call on the matching state values
567 * @param cb_cls closure for 'cb'
568 * @return message ID for which the state was returned (last seen
572 GNUNET_PSYC_member_state_get (struct GNUNET_PSYC_Member *member,
573 const char *state_name,
574 GNUNET_PSYC_StateCallback cb,
579 * Obtain the current value of a variable. This function should only
580 * be called during a GNUNET_PSYC_Method invocation (and even then
581 * only if the origin is the state owner), as variables are only valid
582 * for the duration of a method invocation. If this function is
583 * called outside of the scope of such a method invocation, it will
586 * FIXME: do variables have a hierarchy as well? If so,
587 * we should document the lookup semantics.
589 * @param member membership handle
590 * @param variable_name name of the variable to query
591 * @param return_value_size set to number of bytes in variable,
592 * needed as variables might contain binary data and
593 * might also not be 0-terminated; set to 0 on errors
594 * @return NULL on error, pointer to variable state otherwise
597 GNUNET_PSYC_member_variable_get (struct GNUNET_PSYC_Member *member,
598 const char *variable_name,
599 size_t *return_value_size);
603 * Leave a multicast group. Will terminate the connection to the PSYC
604 * service. Polite clients should first explicitly send a 'leave'
605 * request (via 'GNUNET_PSYC_member_send_to_host'). This function
606 * must not be called on a 'member' that was obtained from
607 * GNUNET_PSYC_channel_get_group.
609 * @param member membership handle
612 GNUNET_PSYC_member_leave (struct GNUNET_PSYC_Member *member);
616 #if 0 /* keep Emacsens' auto-indent happy */
623 /* ifndef GNUNET_PSYC_SERVICE_H */
625 /* end of gnunet_psyc_service.h */