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 master (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 master (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 #ifndef GNUNET_PSYC_SERVICE_H
78 #define GNUNET_PSYC_SERVICE_H
83 #if 0 /* keep Emacsens' auto-indent happy */
88 #include "gnunet_util_lib.h"
89 #include "gnunet_psyc_lib.h"
90 #include "gnunet_multicast_service.h"
94 * Version number of GNUnet-PSYC API.
96 #define GNUNET_PSYC_VERSION 0x00000000
99 enum GNUNET_PSYC_MessageFlags
102 * First fragment of a message.
104 GNUNET_PSYC_MESSAGE_FIRST_FRAGMENT = 1 << 0,
107 * Last fragment of a message.
109 GNUNET_PSYC_MESSAGE_LAST_FRAGMENT = 1 << 1,
112 * OR'ed flags if message is not fragmented.
114 GNUNET_PSYC_MESSAGE_NOT_FRAGMENTED
115 = GNUNET_PSYC_MESSAGE_FIRST_FRAGMENT
116 | GNUNET_PSYC_MESSAGE_LAST_FRAGMENT,
119 * Historic message, retrieved from PSYCstore.
121 GNUNET_PSYC_MESSAGE_HISTORIC = 1 << 30
126 * Handle that identifies a join request.
128 * Used to match calls to #GNUNET_PSYC_JoinCallback to the
129 * corresponding calls to GNUNET_PSYC_join_decision().
131 struct GNUNET_PSYC_JoinHandle;
135 * Method called from PSYC upon receiving a message indicating a call
138 * @param cls Closure.
139 * @param sender Who transmitted the message (master, except for messages
140 * from one of the slaves to the master).
141 * @param message_id Unique message counter for this message;
142 * (unique only in combination with the given sender for
144 * @param method_name Original method name from PSYC (may be more
145 * specific than the registered method name due to try-and-slice matching).
146 * FIXME: no try-and-slice for methods defined here.
147 * @param header_length Number of modifiers in header.
148 * @param header Modifiers present in the message. FIXME: use environment instead?
149 * @param data_offset Byte offset of @a data in the overall data of the method.
150 * @param data_size Number of bytes in @a data.
151 * @param data Data stream given to the method (might not be zero-terminated
152 * if data is binary).
153 * @param frag Fragmentation status for the data.
155 typedef int (*GNUNET_PSYC_Method)(void *cls,
156 const struct GNUNET_PeerIdentity *sender,
158 const char *method_name,
159 size_t header_length,
160 GNUNET_PSYC_Modifier *header,
161 uint64_t data_offset,
164 enum GNUNET_PSYC_MessageFlags flags);
168 * Method called from PSYC upon receiving a join request.
170 * @param cls Closure.
171 * @param peer Peer requesting to join.
172 * @param method_name Method name in the join request.
173 * @param header_length Number of modifiers in header.
174 * @param header Modifiers present in the message.
175 * @param data_size Number of bytes in @a data.
176 * @param data Data stream given to the method (might not be zero-terminated
177 * if data is binary).
179 typedef int (*GNUNET_PSYC_JoinCallback)(void *cls,
180 const struct GNUNET_PeerIdentity *peer,
181 const char *method_name,
182 size_t header_length,
183 GNUNET_PSYC_Modifier *header,
186 struct GNUNET_PSYC_JoinHandle *jh);
190 * Function to call with the decision made for a join request.
192 * Must be called once and only once in response to an invocation of the
193 * #GNUNET_PSYC_JoinCallback.
195 * @param jh Join request handle.
196 * @param is_admitted #GNUNET_YES if joining is approved,
197 * #GNUNET_NO if it is disapproved
198 * @param relay_count Number of relays given.
199 * @param relays Array of suggested peers that might be useful relays to use
200 * when joining the multicast group (essentially a list of peers that
201 * are already part of the multicast group and might thus be willing
202 * to help with routing). If empty, only this local peer (which must
203 * be the multicast origin) is a good candidate for building the
204 * multicast tree. Note that it is unnecessary to specify our own
205 * peer identity in this array.
206 * @param method_name Method name for the message transmitted with the response.
207 * @param env Environment containing transient variables for the message, or NULL.
208 * @param data_size Size of @a data.
209 * @param data Data of the message.
212 GNUNET_PSYC_join_decision (struct GNUNET_PSYC_JoinHandle *jh,
214 unsigned int relay_count,
215 const struct GNUNET_PeerIdentity *relays,
216 const char *method_name,
217 const struct GNUNET_ENV_Environment *env,
223 * Handle for the master of a PSYC channel.
225 struct GNUNET_PSYC_Master;
229 * Start a PSYC master channel.
231 * Will start a multicast group identified by the given ECC key. Messages
232 * received from group members will be given to the respective handler methods.
233 * If a new member wants to join a group, the "join" method handler will be
234 * invoked; the join handler must then generate a "join" message to approve the
235 * joining of the new member. The channel can also change group membership
236 * without explicit requests. Note that PSYC doesn't itself "understand" join
237 * or part messages, the respective methods must call other PSYC functions to
238 * inform PSYC about the meaning of the respective events.
240 * @param cfg Configuration to use (to connect to PSYC service).
241 * @param priv_key ECC key that will be used to sign messages for this
242 * PSYC session. The public key is used to identify the PSYC channel.
243 * Note that end-users will usually not use the private key directly, but
244 * rather look it up in GADS for places managed by other users, or select
245 * a file with the private key(s) when setting up their own channels
246 * FIXME: we'll likely want to use NOT the p521 curve here, but a cheaper
248 * @param policy Group policy specifying join and history restrictions.
249 * Used to automate group management decisions.
250 * @param method_cb Function to invoke on messages received from members.
251 * @param join_cb Function to invoke when a peer wants to join.
252 * @param cls Closure for the callbacks.
253 * @return Handle for the channel master, NULL on error.
255 struct GNUNET_PSYC_Master *
256 GNUNET_PSYC_master_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
257 const struct GNUNET_CRYPTO_EccPrivateKey *priv_key,
258 enum GNUNET_MULTICAST_GroupPolicy policy,
259 GNUNET_PSYC_Method method_cb,
260 GNUNET_PSYC_JoinCallback join_cb,
265 * Function called to provide data for a transmission via PSYC.
267 * Note that returning #GNUNET_OK or #GNUNET_SYSERR (but not #GNUNET_NO)
268 * invalidates the respective transmission handle.
270 * @param cls Closure.
271 * @param message_id Set to the unique message ID that was generated for
273 * @param[in,out] data_size Initially set to the number of bytes available in @a data,
274 * should be set to the number of bytes written to data (IN/OUT).
275 * @param[out] data Where to write the body of the message to give to the method;
276 * function must copy at most @a *data_size bytes to @a data.
277 * @return #GNUNET_SYSERR on error (fatal, aborts transmission)
278 * #GNUNET_NO on success, if more data is to be transmitted later
279 * (should be used if @a *data_size was not big enough to take all the data)
280 * #GNUNET_YES if this completes the transmission (all data supplied)
282 typedef int (*GNUNET_PSYC_MasterReadyNotify)(void *cls,
289 * Handle for a pending PSYC transmission operation.
291 struct GNUNET_PSYC_MasterTransmitHandle;
295 * Send a message to call a method to all members in the PSYC channel.
297 * @param master Handle to the PSYC channel.
298 * @param increment_group_generation #GNUNET_YES if we need to increment
299 * the group generation counter after transmitting this message.
300 * @param method_name Which method should be invoked.
301 * @param env Environment containing state operations and transient variables
302 * for the message, or NULL.
303 * @param notify Function to call to obtain the arguments.
304 * @param notify_cls Closure for @a notify.
305 * @return Transmission handle, NULL on error (i.e. more than one request queued).
307 struct GNUNET_PSYC_MasterTransmitHandle *
308 GNUNET_PSYC_master_transmit (struct GNUNET_PSYC_Master *master,
309 int increment_group_generation,
310 const char *method_name,
311 const struct GNUNET_ENV_Environment *env,
312 GNUNET_PSYC_MasterReadyNotify notify,
317 * Abort transmission request to channel.
319 * @param th Handle of the request that is being aborted.
322 GNUNET_PSYC_master_transmit_cancel (struct GNUNET_PSYC_MasterTransmitHandle *th);
326 * Stop a PSYC master channel.
328 * @param master PSYC channel master to stop.
331 GNUNET_PSYC_master_stop (struct GNUNET_PSYC_Master *master);
335 * Handle for a PSYC channel slave.
337 struct GNUNET_PSYC_Slave;
341 * Join a PSYC channel.
343 * The entity joining is always the local peer. The user must immediately use
344 * the GNUNET_PSYC_slave_to_master() functions to transmit a @e join_msg to the
345 * channel; if the join request succeeds, the channel state (and @e recent
346 * method calls) will be replayed to the joining member. There is no explicit
347 * notification on failure (as the channel may simply take days to approve,
348 * and disapproval is simply being ignored).
350 * @param cfg Configuration to use.
351 * @param pub_key ECC key that identifies the channel we wish to join.
352 * @param origin Peer identity of the origin.
353 * @param method Function to invoke on messages received from the channel,
354 * typically at least contains functions for @e join and @e part.
355 * @param method_cls Closure for @a method.
356 * @param method_name Method name for the join request.
357 * @param env Environment containing transient variables for the request, or NULL.
358 * @param data_size Number of bytes in @a data.
359 * @param data Payload for the join message.
360 * @return Handle for the slave, NULL on error.
362 struct GNUNET_PSYC_Slave *
363 GNUNET_PSYC_slave_join (const struct GNUNET_CONFIGURATION_Handle *cfg,
364 const struct GNUNET_CRYPTO_EccPublicKey *pub_key,
365 const struct GNUNET_PeerIdentity *origin,
366 GNUNET_PSYC_Method method,
368 const char *method_name,
369 const struct GNUNET_ENV_Environment *env,
375 * Part a PSYC channel.
377 * Will terminate the connection to the PSYC service. Polite clients should
378 * first explicitly send a @e part request (via GNUNET_PSYC_slave_to_master()).
380 * @param slave Slave handle.
383 GNUNET_PSYC_slave_part (struct GNUNET_PSYC_Slave *slave);
387 * Function called to provide data for a transmission to the channel
388 * master (aka the @e host of the channel).
390 * Note that returning #GNUNET_OK or #GNUNET_SYSERR (but not #GNUNET_NO)
391 * invalidates the respective transmission handle.
393 * @param cls Closure.
394 * @param[in,out] data_size Initially set to the number of bytes available in @a data,
395 * should be set to the number of bytes written to data (IN/OUT).
396 * @param[out] data Where to write the body of the message to give to the method;
397 * function must copy at most @a *data_size bytes to @a data.
398 * @return #GNUNET_SYSERR on error (fatal, aborts transmission).
399 * #GNUNET_NO on success, if more data is to be transmitted later.
400 * #GNUNET_YES if this completes the transmission (all data supplied).
402 typedef int (*GNUNET_PSYC_SlaveReadyNotify)(void *cls,
408 * Handle for a pending PSYC transmission operation.
410 struct GNUNET_PSYC_SlaveTransmitHandle;
414 * Request a message to be sent to the channel master.
416 * @param slave Slave handle.
417 * @param method_name Which (PSYC) method should be invoked (on host).
418 * @param env Environment containing transient variables for the message, or NULL.
419 * @param notify Function to call when we are allowed to transmit (to get data).
420 * @param notify_cls Closure for @a notify.
421 * @return Transmission handle, NULL on error (i.e. more than one request queued).
423 struct GNUNET_PSYC_SlaveTransmitHandle *
424 GNUNET_PSYC_slave_transmit (struct GNUNET_PSYC_Slave *slave,
425 const char *method_name,
426 const struct GNUNET_ENV_Environment *env,
427 GNUNET_PSYC_SlaveReadyNotify notify,
432 * Abort transmission request to master.
434 * @param th Handle of the request that is being aborted.
437 GNUNET_PSYC_slave_transmit_cancel (struct GNUNET_PSYC_SlaveTransmitHandle *th);
441 * Handle to access PSYC channel operations for both the master and slaves.
443 struct GNUNET_PSYC_Channel;
447 * Convert a channel @a master to a @e channel handle to access the @e channel APIs.
449 * @param master Channel master handle.
450 * @return Channel handle, valid for as long as @a master is valid.
452 struct GNUNET_PSYC_Channel *
453 GNUNET_PSYC_master_get_channel (struct GNUNET_PSYC_Master *master);
457 * Convert @a slave to a @e channel handle to access the @e channel APIs.
459 * @param slave Slave handle.
460 * @return Channel handle, valid for as long as @a slave is valid.
462 struct GNUNET_PSYC_Channel *
463 GNUNET_PSYC_slave_get_channel (struct GNUNET_PSYC_Slave *slave);
467 * Add a member to the channel.
469 * Note that this will NOT generate any PSYC traffic, it will merely update the
470 * local data base to modify how we react to <em>membership test</em> queries.
471 * The channel master still needs to explicitly transmit a @e join message to
472 * notify other channel members and they then also must still call this function
473 * in their respective methods handling the @e join message. This way, how @e
474 * join and @e part operations are exactly implemented is still up to the
475 * application; for example, there might be a @e part_all method to kick out
478 * Note that channel members are explicitly trusted to execute such methods
479 * correctly; not doing so correctly will result in either denying members
480 * access or offering access to channel data to non-members.
482 * @param channel Channel handle.
483 * @param member Which peer to add.
484 * @param message_id Message ID for the message that changed the membership.
487 GNUNET_PSYC_channel_member_add (struct GNUNET_PSYC_Channel *channel,
488 const struct GNUNET_PeerIdentity *member,
489 uint64_t message_id);
493 * Remove a member from the channel.
495 * Note that this will NOT generate any PSYC traffic, it will merely update the
496 * local data base to modify how we react to <em>membership test</em> queries.
497 * The channel master still needs to explicitly transmit a @e part message to
498 * notify other channel members and they then also must still call this function
499 * in their respective methods handling the @e part message. This way, how
500 * @e join and @e part operations are exactly implemented is still up to the
501 * application; for example, there might be a @e part_all message to kick out
504 * Note that channel members are explicitly trusted to perform these
505 * operations correctly; not doing so correctly will result in either
506 * denying members access or offering access to channel data to
509 * @param channel Channel handle.
510 * @param member Which peer to remove.
511 * @param message_id Message ID for the message that changed the membership.
514 GNUNET_PSYC_channel_member_remove (struct GNUNET_PSYC_Channel *channel,
515 const struct GNUNET_PeerIdentity *member,
516 uint64_t message_id);
520 * Function called to inform a member about stored state values for a channel.
522 * @param cls Closure.
523 * @param name Name of the state variable.
524 * @param value Value of the state variable.
525 * @param value_size Number of bytes in @a value.
527 typedef void (*GNUNET_PSYC_StateCallback)(void *cls,
534 * Handle to a story telling operation.
536 struct GNUNET_PSYC_Story;
540 * Request to be told the message history of the channel.
542 * Historic messages (but NOT the state at the time) will be replayed (given to
543 * the normal method handlers) if available and if access is permitted.
545 * To get the latest message, use 0 for both the start and end message ID.
547 * @param channel Which channel should be replayed?
548 * @param start_message_id Earliest interesting point in history.
549 * @param end_message_id Last (exclusive) interesting point in history.
550 * @param method Function to invoke on messages received from the story.
551 * @param method_cls Closure for @a method.
552 * @param finish_cb Function to call when the requested story has been fully
553 * told (counting message IDs might not suffice, as some messages
554 * might be secret and thus the listener would not know the story is
555 * finished without being told explicitly); once this function
556 * has been called, the client must not call
557 * GNUNET_PSYC_channel_story_tell_cancel() anymore.
558 * @param finish_cb_cls Closure to finish_cb.
559 * @return Handle to cancel story telling operation.
561 struct GNUNET_PSYC_Story *
562 GNUNET_PSYC_channel_story_tell (struct GNUNET_PSYC_Channel *channel,
563 uint64_t start_message_id,
564 uint64_t end_message_id,
565 GNUNET_PSYC_Method method,
567 void (*finish_cb)(void *),
568 void *finish_cb_cls);
572 * Abort story telling.
574 * This function must not be called from within method handlers (as given to
575 * GNUNET_PSYC_slave_join()) of the slave.
577 * @param story Story telling operation to stop.
580 GNUNET_PSYC_channel_story_tell_cancel (struct GNUNET_PSYC_Story *story);
584 * Call the given callback on all matching values (including variables) in the
587 * The callback is invoked synchronously on all matching states (as the state is
588 * fully replicated in the library in this process; channel states should be
589 * small, large data is to be passed as streaming data to methods).
591 * A name matches if it includes the @a state_name prefix, thus requesting the
592 * empty state ("") will match all values; requesting "_a_b" will also return
593 * values stored under "_a_b_c".
595 * @param channel Channel handle.
596 * @param state_name Name of the state to query (full name
597 * might be longer, this is only the prefix that must match).
598 * @param cb Function to call on the matching state values.
599 * @param cb_cls Closure for @a cb.
600 * @return Message ID for which the state was returned (last seen
604 GNUNET_PSYC_channel_state_get_all (struct GNUNET_PSYC_Channel *channel,
605 const char *state_name,
606 GNUNET_PSYC_StateCallback cb,
611 * Obtain the current value of the best-matching value in the state
612 * (including variables).
614 * Note that variables are only valid during a #GNUNET_PSYC_Method invocation, as
615 * variables are only valid for the duration of a method invocation.
617 * If the requested variable name does not have an exact state in
618 * the state, the nearest less-specific name is matched; for example,
619 * requesting "_a_b" will match "_a" if "_a_b" does not exist.
621 * @param channel Channel handle.
622 * @param variable_name Name of the variable to query.
623 * @param[out] return_value_size Set to number of bytes in variable,
624 * needed as variables might contain binary data and
625 * might also not be 0-terminated; set to 0 on errors.
626 * @return NULL on error (no matching state or variable), pointer
627 to the respective value otherwise.
630 GNUNET_PSYC_channel_state_get (struct GNUNET_PSYC_Channel *channel,
631 const char *variable_name,
632 size_t *return_value_size);
635 #if 0 /* keep Emacsens' auto-indent happy */
642 /* ifndef GNUNET_PSYC_SERVICE_H */
644 /* end of gnunet_psyc_service.h */