2 This file is part of GNUnet.
3 (C) 2012, 2013 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file include/gnunet_psyc_service.h
23 * @brief PSYC service; high-level access to the PSYC protocol
24 * note that clients of this API are NOT expected to
25 * understand the PSYC message format, only the semantics!
26 * Parsing (and serializing) the PSYC stream format is done
27 * within the implementation of the libgnunetpsyc library,
28 * and this API deliberately exposes as little as possible
29 * of the actual data stream format to the application!
30 * @author Christian Grothoff
31 * @author Gabor X Toth
34 * - this API does not know about psyc's "root" and "places";
35 * there is no 'root' in GNUnet-Psyc as we're decentralized;
36 * 'places' and 'persons' are combined within the same
37 * abstraction, that of a "channel". Channels are identified
38 * and accessed in this API using a public/private key.
39 * Higher-level applications should use NAMES within GADS
40 * to obtain public keys, and the distinction between
41 * 'places' and 'persons' can then be made with the help
42 * of the naming system (and/or conventions).
43 * Channels are (as in PSYC) organized into a hierarchy; each
44 * channel owner (the one with the private key) is then
45 * the operator of the multicast group (its Origin in
46 * the terminology of the multicast API).
47 * - The API supports passing large amounts of data using
48 * 'streaming' for the argument passed to a method. State
49 * and variables must fit into memory and cannot be streamed
50 * (thus, no passing of 4 GB of data in a variable;
51 * once we implement this, we might want to create a
52 * @c \#define for the maximum size of a variable).
53 * - PSYC defines standard variables, methods, etc. This
54 * library deliberately abstracts over all of these; a
55 * higher-level API should combine the naming system (GADS)
56 * and standard methods (message, join, part, warn,
57 * fail, error) and variables (action, color, time,
58 * tag, etc.). However, this API does take over the
59 * routing variables, specifically 'context' (channel),
60 * and 'source'. We only kind-of support 'target', as
61 * the target is either everyone in the group or the
62 * origin, and never just a single member of the group;
63 * for such individual messages, an application needs to
64 * construct an 'inbox' channel where the owner (only)
65 * receives messages (but never forwards; private responses
66 * would be transmitted by joining the senders 'inbox'
67 * channel -- or a inbox#bob subchannel). The
68 * goal for all of this is to keep the abstractions in this
69 * API minimal: interaction with multicast, try \& slice,
70 * state/variable/channel management. Higher-level
71 * operations belong elsewhere (so maybe this API should
72 * be called 'PSYC-low', whereas a higher-level API
73 * implementing defaults for standard methods and
74 * variables might be called 'PSYC-std' or 'PSYC-high'.
77 * - rename "channel" to "master"
78 * - rename "member" to "slave"
79 * - rename "group" to "channel"
82 #ifndef GNUNET_PSYC_SERVICE_H
83 #define GNUNET_PSYC_SERVICE_H
88 #if 0 /* keep Emacsens' auto-indent happy */
93 #include "gnunet_util_lib.h"
94 #include "gnunet_psyc_lib.h"
95 #include "gnunet_multicast_service.h"
99 * Version number of GNUnet-PSYC API.
101 #define GNUNET_PSYC_VERSION 0x00000000
104 enum GNUNET_PSYC_MessageFlags
107 * First fragment of a message.
109 GNUNET_PSYC_MESSAGE_FIRST_FRAGMENT = GNUNET_MULTICAST_MESSAGE_FIRST_FRAGMENT,
112 * Last fragment of a message.
114 GNUNET_PSYC_MESSAGE_LAST_FRAGMENT = GNUNET_MULTICAST_MESSAGE_LAST_FRAGMENT,
117 * OR'ed flags if message is not fragmented.
119 GNUNET_PSYC_MESSAGE_NOT_FRAGMENTED = GNUNET_MULTICAST_MESSAGE_NOT_FRAGMENTED
124 * Handle that identifies a join request.
126 * Used to match calls to #GNUNET_PSYC_JoinCallback to the
127 * corresponding calls to GNUNET_PSYC_join_decision().
129 struct GNUNET_PSYC_JoinHandle;
132 * Handle that identifies a part request.
134 * Used to match calls to #GNUNET_PSYC_PartCallback to the
135 * corresponding calls to GNUNET_PSYC_part_ack().
137 struct GNUNET_PSYC_PartHandle;
141 * Method called from PSYC upon receiving a message indicating a call
144 * @param cls Closure.
145 * @param sender Who transmitted the message (origin, except for messages
146 * from one of the members to the origin).
147 * @param message_id Unique message counter for this message;
148 * (unique only in combination with the given sender for
150 * @param method_name Original method name from PSYC (may be more
151 * specific than the registered method name due to try-and-slice matching).
152 * FIXME: no try-and-slice for methods defined here.
153 * @param header_length Number of modifiers in header.
154 * @param header Modifiers present in the message. FIXME: use environment instead?
155 * @param data_offset Byte offset of @a data in the overall data of the method.
156 * @param data_size Number of bytes in @a data.
157 * @param data Data stream given to the method (might not be zero-terminated
158 * if data is binary).
159 * @param frag Fragmentation status for the data.
161 typedef int (*GNUNET_PSYC_Method)(void *cls,
162 const struct GNUNET_PeerIdentity *sender,
164 const char *method_name,
165 size_t header_length,
166 GNUNET_PSYC_Modifier *header,
167 uint64_t data_offset,
170 enum GNUNET_PSYC_MessageFlags flags);
174 * Method called from PSYC upon receiving a join request.
176 * @param cls Closure.
177 * @param peer Peer requesting to join.
178 * @param method_name Method name in the join request.
179 * @param header_length Number of modifiers in header.
180 * @param header Modifiers present in the message.
181 * @param data_size Number of bytes in @a data.
182 * @param data Data stream given to the method (might not be zero-terminated
183 * if data is binary).
185 typedef int (*GNUNET_PSYC_JoinCallback)(void *cls,
186 const struct GNUNET_PeerIdentity *peer,
187 const char *method_name,
188 size_t header_length,
189 GNUNET_PSYC_Modifier *header,
192 struct GNUNET_PSYC_JoinHandle *jh);
196 * Method called from PSYC upon receiving a part request.
198 * @param cls Closure.
199 * @param peer Peer requesting to leave.
200 * @param method_name Method name in the part request.
201 * @param header_length Number of modifiers in header.
202 * @param header Modifiers present in the message.
203 * @param data_size Number of bytes in @a data.
204 * @param data Data stream given to the method (might not be zero-terminated
205 * if data is binary).
207 typedef int (*GNUNET_PSYC_PartCallback)(void *cls,
208 const struct GNUNET_PeerIdentity *peer,
209 const char *method_name,
210 size_t header_length,
211 GNUNET_PSYC_Modifier *header,
214 struct GNUNET_PSYC_PartHandle *ph);
218 * Function to call with the decision made for a join request.
220 * Must be called once and only once in response to an invocation of the
221 * #GNUNET_PSYC_JoinCallback.
223 * @param jh Join request handle.
224 * @param is_admitted #GNUNET_YES if joining is approved,
225 * #GNUNET_NO if it is disapproved
226 * @param method_name Method name for the message transmitted with the response.
227 * @param env Environment: transient variables for the message.
228 * @param data_size Size of @a data.
229 * @param data Data of the message.
232 GNUNET_PSYC_join_decision (struct GNUNET_PSYC_JoinHandle *jh,
234 const char *method_name,
235 const struct GNUNET_ENV_Environment *env,
241 * Send a part acknowledgment.
243 * @param ph Part handle.
246 GNUNET_PSYC_part_ack (struct GNUNET_PSYC_PartHandle *ph);
250 * Handle for the channel of a PSYC group.
252 struct GNUNET_PSYC_Channel;
256 * Create a PSYC channel.
258 * Will create a multicast group identified by the given ECC key. Messages
259 * received from group members will be given to the respective handler methods.
260 * If a new member wants to join a group, the "join" method handler will be
261 * invoked; the join handler must then generate a "join" message to approve the
262 * joining of the new member. The channel can also change group membership
263 * without explicit requests. Note that PSYC doesn't itself "understand" join
264 * or part messages, the respective methods must call other PSYC functions to
265 * inform PSYC about the meaning of the respective events.
267 * @param cfg Configuration to use (to connect to PSYC service).
268 * @param priv_key ECC key that will be used to sign messages for this
269 * PSYC session; public key is used to identify the
270 * PSYC group; FIXME: we'll likely want to use
271 * NOT the p521 curve here, but a cheaper one in the future
272 * Note that end-users will usually not use the private key
273 * directly, but rather look it up in GADS for groups
274 * managed by other users, or select a file with the private
275 * key(s) when setting up their own channels
276 * @param join_policy What is the membership policy of the group?
277 * Used to automate group management decisions.
278 * @param method_cb Function to invoke on messages received from members.
279 * @param join_cb Function to invoke when a peer wants to join.
280 * @param part_cb Function to invoke when a peer wants to part.
281 * @param cls Closure for the callbacks.
282 * @return Handle for the channel, NULL on error.
284 struct GNUNET_PSYC_Channel *
285 GNUNET_PSYC_channel_create (const struct GNUNET_CONFIGURATION_Handle *cfg,
286 const struct GNUNET_CRYPTO_EccPrivateKey *priv_key,
287 enum GNUNET_MULTICAST_JoinPolicy join_policy,
288 GNUNET_PSYC_Method method_cb,
289 GNUNET_PSYC_JoinCallback join_cb,
290 GNUNET_PSYC_PartCallback part_cb,
295 * Function called to provide data for a transmission via PSYC.
297 * Note that returning #GNUNET_OK or #GNUNET_SYSERR (but not #GNUNET_NO)
298 * invalidates the respective transmission handle.
300 * @param cls Closure.
301 * @param message_id Set to the unique message ID that was generated for
303 * @param data_size[in,out] Initially set to the number of bytes available in @a data,
304 * should be set to the number of bytes written to data (IN/OUT).
305 * @param[out] data Where to write the body of the message to give to the method;
306 * function must copy at most @a *data_size bytes to @a data.
307 * @return #GNUNET_SYSERR on error (fatal, aborts transmission)
308 * #GNUNET_NO on success, if more data is to be transmitted later
309 * (should be used if @a *data_size was not big enough to take all the data)
310 * #GNUNET_YES if this completes the transmission (all data supplied)
312 typedef int (*GNUNET_PSYC_ChannelReadyNotify)(void *cls,
319 * Handle for a pending PSYC transmission operation.
321 struct GNUNET_PSYC_ChannelTransmitHandle;
325 * Send a message to call a method to all members in the PSYC channel.
327 * @param channel Handle to the PSYC multicast group.
328 * @param increment_group_generation #GNUNET_YES if we need to increment
329 * the group generation counter after transmitting this message.
330 * @param method_name Which method should be invoked.
331 * @param env Environment: state operations and transient variables for the message.
332 * @param notify Function to call to obtain the arguments.
333 * @param notify_cls Closure for @a notify.
334 * @return Transmission handle, NULL on error (i.e. more than one request queued).
336 struct GNUNET_PSYC_ChannelTransmitHandle *
337 GNUNET_PSYC_channel_transmit (struct GNUNET_PSYC_Channel *channel,
338 int increment_group_generation,
339 const char *method_name,
340 const struct GNUNET_ENV_Environment *env,
341 GNUNET_PSYC_ChannelReadyNotify notify,
346 * Abort transmission request to channel.
348 * @param th Handle of the request that is being aborted.
351 GNUNET_PSYC_channel_transmit_cancel (struct GNUNET_PSYC_ChannelTransmitHandle *th);
355 * Destroy a PSYC channel.
357 * @param channel PSYC channel to terminate.
360 GNUNET_PSYC_channel_destroy (struct GNUNET_PSYC_Channel *channel);
364 * Handle to access PSYC group operations for all members.
366 struct GNUNET_PSYC_Group;
370 * Convert @a channel to a @e group handle to access the @e group APIs.
372 * @param channel Channel handle.
373 * @return Group handle, valid for as long as @a channel is valid.
375 struct GNUNET_PSYC_Group *
376 GNUNET_PSYC_channel_get_group (struct GNUNET_PSYC_Channel *channel);
380 * Convert @a member to a @e group handle to access the @e group APIs.
382 * @param member Membership handle.
383 * @return Group handle, valid for as long as @a member is valid.
385 struct GNUNET_PSYC_Group *
386 GNUNET_PSYC_member_get_group (struct GNUNET_PSYC_Member *member);
390 * Add a member to the group.
392 * Note that this will NOT generate any PSYC traffic, it will merely update the
393 * local data base to modify how we react to <em>membership test</em> queries. The
394 * channel still needs to explicitly transmit a @e join message to notify other
395 * group members and they then also must still call this function in their
396 * respective methods handling the @e join message. This way, how @e join and
397 * @e part operations are exactly implemented is still up to the application;
398 * for example, there might be a @e part_all method to kick out everyone.
400 * Note that group members are explicitly trusted to execute such methods
401 * correctly; not doing so correctly will result in either denying members
402 * access or offering access to group data to non-members.
404 * @param group Group handle.
405 * @param member Which peer to add.
406 * @param message_id Message ID for the message that changed the membership.
409 GNUNET_PSYC_group_member_add (struct GNUNET_PSYC_Group *group,
410 const struct GNUNET_PeerIdentity *member,
411 uint64_t message_id);
415 * Remove a member from the group.
417 * Note that this will NOT generate any PSYC traffic, it will merely update the
418 * local data base to modify how we react to <em>membership test</em> queries. The
419 * channel still needs to explicitly transmit a @e part message to notify other
420 * group members and they then also must still call this function in their
421 * respective methods handling the @e part message. This way, how @e join and
422 * @e part operations are exactly implemented is still up to the application;
423 * for example, there might be a @e part_all message to kick out everyone.
425 * Note that group members are explicitly trusted to perform these
426 * operations correctly; not doing so correctly will result in either
427 * denying members access or offering access to group data to
430 * @param group Group handle.
431 * @param member Which peer to remove.
432 * @param message_id Message ID for the message that changed the membership.
435 GNUNET_PSYC_group_member_remove (struct GNUNET_PSYC_Group *group,
436 const struct GNUNET_PeerIdentity *member,
437 uint64_t message_id);
441 * Function called to inform a member about stored state values for a channel.
443 * @param cls Closure.
444 * @param name Name of the state variable.
445 * @param value Value of the state variable.
446 * @param value_size Number of bytes in @a value.
448 typedef void (*GNUNET_PSYC_StateCallback)(void *cls,
457 * The entity joining is always the local peer. The user must immediately use
458 * the GNUNET_PSYC_member_to_origin() (and possibly
459 * GNUNET_PSYC_member_variable_set()) functions to transmit a @e join_msg to
460 * the channel; if the join request succeeds, the channel state (and @e recent
461 * method calls) will be replayed to the joining member. There is no explicit
462 * notification on failure (as the channel may simply take days to approve, a-v/snd
463 * disapproval is simply being ignored).
465 * @param cfg Configuration to use.
466 * @param pub_key ECC key that identifies the channel we wish to join
467 * @param origin Peer identity of the origin.
468 * @param method Function to invoke on messages received from the channel,
469 * typically at least contains functions for @e join and @e part.
470 * @param method_cls Closure for @a method.
471 * @return Handle for the member, NULL on error.
473 struct GNUNET_PSYC_Member *
474 GNUNET_PSYC_member_join (const struct GNUNET_CONFIGURATION_Handle *cfg,
475 const struct GNUNET_CRYPTO_EccPublicKey *pub_key,
476 const struct GNUNET_PeerIdentity *origin,
477 GNUNET_PSYC_Method method,
484 * Will terminate the connection to the PSYC service. Polite clients should
485 * first explicitly send a @e part request (via GNUNET_PSYC_member_to_origin()).
487 * @param member membership handle
490 GNUNET_PSYC_member_part (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[out] data 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_MemberReadyNotify)(void *cls,
515 * Handle for a pending PSYC transmission operation.
517 struct GNUNET_PSYC_MemberTransmitHandle;
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 env Environment: transient variables for the message.
526 * @param notify Function to call when we are allowed to transmit (to get data).
527 * @param notify_cls Closure for @a notify.
528 * @return Transmission handle, NULL on error (i.e. more than one request queued).
530 struct GNUNET_PSYC_MemberTransmitHandle *
531 GNUNET_PSYC_member_to_origin (struct GNUNET_PSYC_Member *member,
532 const char *method_name,
533 const struct GNUNET_ENV_Environment *env,
534 GNUNET_PSYC_MemberReadyNotify notify,
539 * Abort transmission request to origin.
541 * @param th Handle of the request that is being aborted.
544 GNUNET_PSYC_member_to_origin_cancel (struct GNUNET_PSYC_MemberTransmitHandle *th);
548 * Handle to a story telling operation.
550 struct GNUNET_PSYC_Story;
554 * Request to be told the message history of the channel.
556 * Historic messages (but NOT the state at the time) will be replayed (given to
557 * the normal method handlers) if available and if access is permitted.
559 * To get the latest message, use 0 for both the start and end message ID.
561 * @param member Which channel should be replayed?
562 * @param start_message_id Earliest interesting point in history.
563 * @param end_message_id Last (exclusive) interesting point in history.
564 * @param method Function to invoke on messages received from the story.
565 * @param method_cls Closure for @a method.
566 * @param finish_cb Function to call when the requested story has been fully
567 * told (counting message IDs might not suffice, as some messages
568 * might be secret and thus the listener would not know the story is
569 * finished without being told explicitly); once this function
570 * has been called, the client must not call
571 * GNUNET_PSYC_member_story_tell_cancel() anymore.
572 * @param finish_cb_cls Closure to finish_cb.
573 * @return Handle to cancel story telling operation.
575 struct GNUNET_PSYC_Story *
576 GNUNET_PSYC_member_story_tell (struct GNUNET_PSYC_Member *member,
577 uint64_t start_message_id,
578 uint64_t end_message_id,
579 GNUNET_PSYC_Method method,
581 void (*finish_cb)(void *),
582 void *finish_cb_cls);
586 * Abort story telling.
588 * This function must not be called from within method handlers (as given to
589 * GNUNET_PSYC_member_join()) of the member.
591 * @param story Story telling operation to stop.
594 GNUNET_PSYC_member_story_tell_cancel (struct GNUNET_PSYC_Story *story);
598 * Call the given callback on all matching values (including variables) in the
601 * The callback is invoked synchronously on all matching states (as the state is
602 * fully replicated in the library in this process; channel states should be
603 * small, large data is to be passed as streaming data to methods).
605 * A name matches if it includes the @a state_name prefix, thus requesting the
606 * empty state ("") will match all values; requesting "_a_b" will also return
607 * values stored under "_a_b_c".
609 * @param member Membership handle.
610 * @param state_name Name of the state to query (full name
611 * might be longer, this is only the prefix that must match).
612 * @param cb Function to call on the matching state values.
613 * @param cb_cls Closure for @a cb.
614 * @return Message ID for which the state was returned (last seen
618 GNUNET_PSYC_member_state_get_all (struct GNUNET_PSYC_Member *member,
619 const char *state_name,
620 GNUNET_PSYC_StateCallback cb,
625 * Obtain the current value of the best-matching value in the state
626 * (including variables).
628 * Note that variables are only valid during a #GNUNET_PSYC_Method invocation, as
629 * variables are only valid for the duration of a method invocation.
631 * If the requested variable name does not have an exact state in
632 * the state, the nearest less-specific name is matched; for example,
633 * requesting "_a_b" will match "_a" if "_a_b" does not exist.
635 * @param member Membership handle.
636 * @param variable_name Name of the variable to query.
637 * @param[out] return_value_size Set to number of bytes in variable,
638 * needed as variables might contain binary data and
639 * might also not be 0-terminated; set to 0 on errors.
640 * @return NULL on error (no matching state or variable), pointer
641 to the respective value otherwise.
644 GNUNET_PSYC_member_state_get (struct GNUNET_PSYC_Member *member,
645 const char *variable_name,
646 size_t *return_value_size);
649 #if 0 /* keep Emacsens' auto-indent happy */
656 /* ifndef GNUNET_PSYC_SERVICE_H */
658 /* end of gnunet_psyc_service.h */