2 * This file is part of GNUnet
3 * (C) 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 psyc/psyc_api.c
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 Gabor X Toth
34 #include "gnunet_util_lib.h"
35 #include "gnunet_env_lib.h"
36 #include "gnunet_psyc_service.h"
39 #define LOG(kind,...) GNUNET_log_from (kind, "psyc-api",__VA_ARGS__)
42 struct OperationHandle
44 struct OperationHandle *prev;
45 struct OperationHandle *next;
46 const struct GNUNET_MessageHeader *msg;
50 * Handle to access PSYC channel operations for both the master and slaves.
52 struct GNUNET_PSYC_Channel
55 * Configuration to use.
57 const struct GNUNET_CONFIGURATION_Handle *cfg;
60 * Socket (if available).
62 struct GNUNET_CLIENT_Connection *client;
65 * Currently pending transmission request, or NULL for none.
67 struct GNUNET_CLIENT_TransmitHandle *th;
70 * Head of operations to transmit.
72 struct OperationHandle *transmit_head;
75 * Tail of operations to transmit.
77 struct OperationHandle *transmit_tail;
80 * Message to send on reconnect.
82 struct GNUNET_MessageHeader *reconnect_msg;
85 * Task doing exponential back-off trying to reconnect.
87 GNUNET_SCHEDULER_TaskIdentifier reconnect_task;
90 * Time for next connect retry.
92 struct GNUNET_TIME_Relative reconnect_delay;
94 GNUNET_PSYC_Method method_cb;
96 GNUNET_PSYC_JoinCallback join_cb;
101 * Are we polling for incoming messages right now?
106 * Are we currently transmitting a message?
113 * Handle for the master of a PSYC channel.
115 struct GNUNET_PSYC_Master
117 struct GNUNET_PSYC_Channel ch;
119 GNUNET_PSYC_MasterStartCallback start_cb;
121 uint64_t max_message_id;
126 * Handle for a PSYC channel slave.
128 struct GNUNET_PSYC_Slave
130 struct GNUNET_PSYC_Channel ch;
135 * Handle that identifies a join request.
137 * Used to match calls to #GNUNET_PSYC_JoinCallback to the
138 * corresponding calls to GNUNET_PSYC_join_decision().
140 struct GNUNET_PSYC_JoinHandle
147 * Handle for a pending PSYC transmission operation.
149 struct GNUNET_PSYC_MasterTransmitHandle
151 struct GNUNET_PSYC_Master *master;
152 const struct GNUNET_ENV_Environment *env;
153 GNUNET_PSYC_MasterTransmitNotify notify;
155 enum GNUNET_PSYC_MasterTransmitFlags flags;
160 * Handle for a pending PSYC transmission operation.
162 struct GNUNET_PSYC_SlaveTransmitHandle
169 * Handle to a story telling operation.
171 struct GNUNET_PSYC_Story
178 * Handle for a state query operation.
180 struct GNUNET_PSYC_StateQuery
187 * Try again to connect to the PSYCstore service.
189 * @param cls handle to the PSYCstore service.
190 * @param tc scheduler context
193 reconnect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
197 * Reschedule a connect attempt to the service.
199 * @param h transport service to reconnect
202 reschedule_connect (struct GNUNET_PSYC_Channel *c)
204 GNUNET_assert (c->reconnect_task == GNUNET_SCHEDULER_NO_TASK);
208 GNUNET_CLIENT_notify_transmit_ready_cancel (c->th);
211 if (NULL != c->client)
213 GNUNET_CLIENT_disconnect (c->client);
216 c->in_receive = GNUNET_NO;
217 LOG (GNUNET_ERROR_TYPE_DEBUG,
218 "Scheduling task to reconnect to PSYCstore service in %s.\n",
219 GNUNET_STRINGS_relative_time_to_string (c->reconnect_delay, GNUNET_YES));
221 GNUNET_SCHEDULER_add_delayed (c->reconnect_delay, &reconnect, c);
222 c->reconnect_delay = GNUNET_TIME_STD_BACKOFF (c->reconnect_delay);
227 * Schedule transmission of the next message from our queue.
229 * @param h PSYCstore handle
232 transmit_next (struct GNUNET_PSYC_Channel *c);
236 * Type of a function to call when we receive a message
240 * @param msg message received, NULL on timeout or fatal error
243 message_handler (void *cls, const struct GNUNET_MessageHeader *msg)
245 struct GNUNET_PSYC_Channel *ch = cls;
246 struct GNUNET_PSYC_Master *mst = cls;
247 struct GNUNET_PSYC_Slave *slv = cls;
251 reschedule_connect (ch);
254 uint16_t size_eq = 0;
255 uint16_t size_min = 0;
256 const uint16_t size = ntohs (msg->size);
257 const uint16_t type = ntohs (msg->type);
259 LOG (GNUNET_ERROR_TYPE_DEBUG,
260 "Received message of type %d from PSYC service\n", type);
264 case GNUNET_MESSAGE_TYPE_PSYC_MASTER_START_ACK:
265 case GNUNET_MESSAGE_TYPE_PSYC_SLAVE_JOIN_ACK:
266 size_eq = sizeof (struct CountersResult);
270 if (! ((0 < size_eq && size == size_eq)
271 || (0 < size_min && size >= size_min)))
274 reschedule_connect (ch);
278 struct CountersResult *cres;
282 case GNUNET_MESSAGE_TYPE_PSYC_MASTER_START_ACK:
283 cres = (struct CountersResult *) msg;
284 mst->max_message_id = GNUNET_ntohll (cres->max_message_id);
285 if (NULL != mst->start_cb)
286 mst->start_cb (ch->cb_cls, mst->max_message_id);
289 case GNUNET_MESSAGE_TYPE_PSYC_SLAVE_JOIN_ACK:
290 cres = (struct CountersResult *) msg;
292 slv->max_message_id = GNUNET_ntohll (cres->max_message_id);
293 if (NULL != slv->join_ack_cb)
294 mst->join_ack_cb (ch->cb_cls, mst->max_message_id);
299 GNUNET_CLIENT_receive (ch->client, &message_handler, ch,
300 GNUNET_TIME_UNIT_FOREVER_REL);
305 * Transmit next message to service.
307 * @param cls The 'struct GNUNET_PSYCSTORE_Handle'.
308 * @param size Number of bytes available in buf.
309 * @param buf Where to copy the message.
310 * @return Number of bytes copied to buf.
313 send_next_message (void *cls, size_t size, void *buf)
315 struct GNUNET_PSYC_Channel *ch = cls;
316 struct OperationHandle *op = ch->transmit_head;
322 ret = ntohs (op->msg->size);
325 reschedule_connect (ch);
328 LOG (GNUNET_ERROR_TYPE_DEBUG,
329 "Sending message of type %d to PSYCstore service\n",
330 ntohs (op->msg->type));
331 memcpy (buf, op->msg, ret);
333 GNUNET_CONTAINER_DLL_remove (ch->transmit_head, ch->transmit_tail, op);
336 if (NULL != ch->transmit_head)
339 if (GNUNET_NO == ch->in_receive)
341 ch->in_receive = GNUNET_YES;
342 GNUNET_CLIENT_receive (ch->client, &message_handler, ch,
343 GNUNET_TIME_UNIT_FOREVER_REL);
350 * Schedule transmission of the next message from our queue.
352 * @param h PSYCstore handle.
355 transmit_next (struct GNUNET_PSYC_Channel *ch)
357 if (NULL != ch->th || NULL == ch->client)
360 struct OperationHandle *op = ch->transmit_head;
364 ch->th = GNUNET_CLIENT_notify_transmit_ready (ch->client,
365 ntohs (op->msg->size),
366 GNUNET_TIME_UNIT_FOREVER_REL,
374 * Try again to connect to the PSYC service.
376 * @param cls Channel handle.
377 * @param tc Scheduler context.
380 reconnect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
382 struct GNUNET_PSYC_Channel *ch = cls;
384 ch->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
385 LOG (GNUNET_ERROR_TYPE_DEBUG,
386 "Connecting to PSYC service.\n");
387 GNUNET_assert (NULL == ch->client);
388 ch->client = GNUNET_CLIENT_connect ("psyc", ch->cfg);
389 GNUNET_assert (NULL != ch->client);
391 if (NULL == ch->transmit_head ||
392 ch->transmit_head->msg->type != ch->reconnect_msg->type)
394 struct OperationHandle *op
395 = GNUNET_malloc (sizeof (struct OperationHandle)
396 + ntohs (ch->reconnect_msg->size));
397 memcpy (&op[1], ch->reconnect_msg, ntohs (ch->reconnect_msg->size));
398 op->msg = (struct GNUNET_MessageHeader *) &op[1];
399 GNUNET_CONTAINER_DLL_insert (ch->transmit_head, ch->transmit_tail, op);
407 * Disconnect from the PSYC service.
409 * @param cls Channel handle.
410 * @param tc Scheduler context.
415 struct GNUNET_PSYC_Channel *ch = c;
416 GNUNET_assert (NULL != ch);
417 GNUNET_assert (ch->transmit_head == ch->transmit_tail);
418 if (ch->reconnect_task != GNUNET_SCHEDULER_NO_TASK)
420 GNUNET_SCHEDULER_cancel (ch->reconnect_task);
421 ch->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
425 GNUNET_CLIENT_notify_transmit_ready_cancel (ch->th);
428 if (NULL != ch->client)
430 GNUNET_CLIENT_disconnect (ch->client);
433 if (NULL != ch->reconnect_msg)
434 ch->reconnect_msg = NULL;
439 * Start a PSYC master channel.
441 * Will start a multicast group identified by the given ECC key. Messages
442 * received from group members will be given to the respective handler methods.
443 * If a new member wants to join a group, the "join" method handler will be
444 * invoked; the join handler must then generate a "join" message to approve the
445 * joining of the new member. The channel can also change group membership
446 * without explicit requests. Note that PSYC doesn't itself "understand" join
447 * or part messages, the respective methods must call other PSYC functions to
448 * inform PSYC about the meaning of the respective events.
450 * @param cfg Configuration to use (to connect to PSYC service).
451 * @param channel_key ECC key that will be used to sign messages for this
452 * PSYC session. The public key is used to identify the PSYC channel.
453 * Note that end-users will usually not use the private key directly, but
454 * rather look it up in GNS for places managed by other users, or select
455 * a file with the private key(s) when setting up their own channels
456 * FIXME: we'll likely want to use NOT the p521 curve here, but a cheaper
458 * @param policy Channel policy specifying join and history restrictions.
459 * Used to automate join decisions.
460 * @param method Function to invoke on messages received from slaves.
461 * @param join_cb Function to invoke when a peer wants to join.
462 * @param cls Closure for @a method and @a join_cb.
463 * @return Handle for the channel master, NULL on error.
465 struct GNUNET_PSYC_Master *
466 GNUNET_PSYC_master_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
467 const struct GNUNET_CRYPTO_EccPrivateKey *channel_key,
468 enum GNUNET_PSYC_Policy policy,
469 GNUNET_PSYC_Method method,
470 GNUNET_PSYC_JoinCallback join_cb,
471 GNUNET_PSYC_MasterStartCallback master_started_cb,
474 struct GNUNET_PSYC_Master *mst = GNUNET_malloc (sizeof (*mst));
475 struct GNUNET_PSYC_Channel *ch = &mst->ch;
476 struct MasterStartRequest *req = GNUNET_malloc (sizeof (*req));
478 req->header.size = htons (sizeof (*req) + sizeof (*channel_key));
479 req->header.type = htons (GNUNET_MESSAGE_TYPE_PSYC_MASTER_START);
480 req->channel_key = *channel_key;
481 req->policy = policy;
484 ch->reconnect_msg = (struct GNUNET_MessageHeader *) req;
485 ch->reconnect_delay = GNUNET_TIME_UNIT_ZERO;
486 ch->reconnect_task = GNUNET_SCHEDULER_add_now (&reconnect, mst);
488 ch->method_cb = method;
489 ch->join_cb = join_cb;
491 mst->start_cb = master_started_cb;
498 * Stop a PSYC master channel.
500 * @param master PSYC channel master to stop.
503 GNUNET_PSYC_master_stop (struct GNUNET_PSYC_Master *mst)
511 * Function to call with the decision made for a join request.
513 * Must be called once and only once in response to an invocation of the
514 * #GNUNET_PSYC_JoinCallback.
516 * @param jh Join request handle.
517 * @param is_admitted #GNUNET_YES if joining is approved,
518 * #GNUNET_NO if it is disapproved.
519 * @param relay_count Number of relays given.
520 * @param relays Array of suggested peers that might be useful relays to use
521 * when joining the multicast group (essentially a list of peers that
522 * are already part of the multicast group and might thus be willing
523 * to help with routing). If empty, only this local peer (which must
524 * be the multicast origin) is a good candidate for building the
525 * multicast tree. Note that it is unnecessary to specify our own
526 * peer identity in this array.
527 * @param method_name Method name for the message transmitted with the response.
528 * @param env Environment containing transient variables for the message, or NULL.
529 * @param data Data of the message.
530 * @param data_size Size of @a data.
533 GNUNET_PSYC_join_decision (struct GNUNET_PSYC_JoinHandle *jh,
535 unsigned int relay_count,
536 const struct GNUNET_PeerIdentity *relays,
537 const char *method_name,
538 const struct GNUNET_ENV_Environment *env,
546 /* FIXME: split up value into <64K chunks and transmit the continuations in
549 send_modifier (void *cls, struct GNUNET_ENV_Modifier *mod)
551 struct GNUNET_PSYC_Channel *ch = cls;
552 size_t name_size = strlen (mod->name) + 1;
553 struct GNUNET_PSYC_MessageModifier *pmod;
554 struct OperationHandle *op = GNUNET_malloc (sizeof (*op) + sizeof (*pmod)
555 + name_size + mod->value_size);
556 pmod = (struct GNUNET_PSYC_MessageModifier *) &op[1];
557 op->msg = (struct GNUNET_MessageHeader *) pmod;
559 pmod->header.type = GNUNET_MESSAGE_TYPE_PSYC_TRANSMIT_MODIFIER;
560 pmod->header.size = htons (sizeof (*pmod) + name_size + mod->value_size);
561 pmod->name_size = htons (name_size);
562 memcpy (&pmod[1], mod->name, name_size);
563 memcpy ((void *) &pmod[1] + name_size, mod->value, mod->value_size);
565 GNUNET_CONTAINER_DLL_insert (ch->transmit_head, ch->transmit_tail, op);
571 * Send a message to call a method to all members in the PSYC channel.
573 * @param mst Handle to the PSYC channel.
574 * @param method_name Which method should be invoked.
575 * @param env Environment containing state operations and transient variables
576 * for the message, or NULL.
577 * @param notify Function to call to obtain the arguments.
578 * @param notify_cls Closure for @a notify.
579 * @param flags Flags for the message being transmitted.
580 * @return Transmission handle, NULL on error (i.e. more than one request
583 struct GNUNET_PSYC_MasterTransmitHandle *
584 GNUNET_PSYC_master_transmit (struct GNUNET_PSYC_Master *mst,
585 const char *method_name,
586 const struct GNUNET_ENV_Environment *env,
587 GNUNET_PSYC_MasterTransmitNotify notify,
589 enum GNUNET_PSYC_MasterTransmitFlags flags)
591 GNUNET_assert (NULL != mst);
592 struct GNUNET_PSYC_Channel *ch = &mst->ch;
593 if (GNUNET_NO != ch->in_transmit)
595 ch->in_transmit = GNUNET_YES;
597 struct GNUNET_PSYC_MessageMethod *pmeth;
598 struct OperationHandle *op = GNUNET_malloc (sizeof (*op) + sizeof (*pmeth));
599 pmeth = (struct GNUNET_PSYC_MessageMethod *) &op[1];
600 op->msg = (struct GNUNET_MessageHeader *) pmeth;
602 pmeth->header.type = GNUNET_MESSAGE_TYPE_PSYC_TRANSMIT_METHOD;
603 size_t size = strlen (method_name) + 1;
604 pmeth->header.size = htons (sizeof (*pmeth) + size);
605 pmeth->flags = htonl (flags);
607 = GNUNET_ntohll (GNUNET_ENV_environment_get_mod_count (env));
608 memcpy (&pmeth[1], method_name, size);
610 GNUNET_CONTAINER_DLL_insert (ch->transmit_head, ch->transmit_tail, op);
612 GNUNET_ENV_environment_iterate (env, send_modifier, mst);
614 struct GNUNET_PSYC_MasterTransmitHandle *th = GNUNET_malloc (sizeof (*th));
618 th->notify_cls = notify_cls;
624 * Abort transmission request to the channel.
626 * @param th Handle of the request that is being aborted.
629 GNUNET_PSYC_master_transmit_cancel (struct GNUNET_PSYC_MasterTransmitHandle *th)
631 struct GNUNET_PSYC_Master *mst = th->master;
632 struct GNUNET_PSYC_Channel *ch = &mst->ch;
633 if (GNUNET_NO != ch->in_transmit)
641 * Join a PSYC channel.
643 * The entity joining is always the local peer. The user must immediately use
644 * the GNUNET_PSYC_slave_transmit() functions to transmit a @e join_msg to the
645 * channel; if the join request succeeds, the channel state (and @e recent
646 * method calls) will be replayed to the joining member. There is no explicit
647 * notification on failure (as the channel may simply take days to approve,
648 * and disapproval is simply being ignored).
650 * @param cfg Configuration to use.
651 * @param channel_key ECC public key that identifies the channel we wish to join.
652 * @param slave_key ECC private-public key pair that identifies the slave, and
653 * used by multicast to sign the join request and subsequent unicast
654 * requests sent to the master.
655 * @param origin Peer identity of the origin.
656 * @param relay_count Number of peers in the @a relays array.
657 * @param relays Peer identities of members of the multicast group, which serve
658 * as relays and used to join the group at.
659 * @param method Function to invoke on messages received from the channel,
660 * typically at least contains functions for @e join and @e part.
661 * @param join_cb Function to invoke when a peer wants to join.
662 * @param cls Closure for @a method_cb and @a join_cb.
663 * @param method_name Method name for the join request.
664 * @param env Environment containing transient variables for the request, or NULL.
665 * @param data Payload for the join message.
666 * @param data_size Number of bytes in @a data.
667 * @return Handle for the slave, NULL on error.
669 struct GNUNET_PSYC_Slave *
670 GNUNET_PSYC_slave_join (const struct GNUNET_CONFIGURATION_Handle *cfg,
671 const struct GNUNET_CRYPTO_EccPublicSignKey *channel_key,
672 const struct GNUNET_CRYPTO_EccPrivateKey *slave_key,
673 const struct GNUNET_PeerIdentity *origin,
675 const struct GNUNET_PeerIdentity *relays,
676 GNUNET_PSYC_Method method,
677 GNUNET_PSYC_JoinCallback join_cb,
678 GNUNET_PSYC_SlaveJoinCallback slave_joined_cb,
680 const char *method_name,
681 const struct GNUNET_ENV_Environment *env,
685 struct GNUNET_PSYC_Slave *slv = GNUNET_malloc (sizeof (*slv));
686 struct GNUNET_PSYC_Channel *ch = &slv->ch;
687 struct SlaveJoinRequest *req = GNUNET_malloc (sizeof (*req));
689 req->header.size = htons (sizeof (*req)
690 + sizeof (*channel_key) + sizeof (*slave_key)
691 + relay_count * sizeof (*relays));
692 req->header.type = htons (GNUNET_MESSAGE_TYPE_PSYC_SLAVE_JOIN);
693 req->channel_key = *channel_key;
694 req->slave_key = *slave_key;
695 req->relay_count = relay_count;
696 memcpy (&req[1], relays, relay_count * sizeof (*relays));
699 ch->reconnect_msg = (struct GNUNET_MessageHeader *) req;
700 ch->reconnect_delay = GNUNET_TIME_UNIT_ZERO;
701 ch->reconnect_task = GNUNET_SCHEDULER_add_now (&reconnect, slv);
708 * Part a PSYC channel.
710 * Will terminate the connection to the PSYC service. Polite clients should
711 * first explicitly send a @e part request (via GNUNET_PSYC_slave_transmit()).
713 * @param slv Slave handle.
716 GNUNET_PSYC_slave_part (struct GNUNET_PSYC_Slave *slv)
724 * Request a message to be sent to the channel master.
726 * @param slave Slave handle.
727 * @param method_name Which (PSYC) method should be invoked (on host).
728 * @param env Environment containing transient variables for the message, or
730 * @param notify Function to call when we are allowed to transmit (to get data).
731 * @param notify_cls Closure for @a notify.
732 * @param flags Flags for the message being transmitted.
733 * @return Transmission handle, NULL on error (i.e. more than one request
736 struct GNUNET_PSYC_SlaveTransmitHandle *
737 GNUNET_PSYC_slave_transmit (struct GNUNET_PSYC_Slave *slave,
738 const char *method_name,
739 const struct GNUNET_ENV_Environment *env,
740 GNUNET_PSYC_SlaveTransmitNotify notify,
742 enum GNUNET_PSYC_SlaveTransmitFlags flags)
749 * Abort transmission request to master.
751 * @param th Handle of the request that is being aborted.
754 GNUNET_PSYC_slave_transmit_cancel (struct GNUNET_PSYC_SlaveTransmitHandle *th)
761 * Convert a channel @a master to a @e channel handle to access the @e channel
764 * @param master Channel master handle.
765 * @return Channel handle, valid for as long as @a master is valid.
767 struct GNUNET_PSYC_Channel *
768 GNUNET_PSYC_master_get_channel (struct GNUNET_PSYC_Master *master)
770 return (struct GNUNET_PSYC_Channel *) master;
775 * Convert @a slave to a @e channel handle to access the @e channel APIs.
777 * @param slave Slave handle.
778 * @return Channel handle, valid for as long as @a slave is valid.
780 struct GNUNET_PSYC_Channel *
781 GNUNET_PSYC_slave_get_channel (struct GNUNET_PSYC_Slave *slave)
783 return (struct GNUNET_PSYC_Channel *) slave;
788 * Add a slave to the channel's membership list.
790 * Note that this will NOT generate any PSYC traffic, it will merely update the
791 * local database to modify how we react to <em>membership test</em> queries.
792 * The channel master still needs to explicitly transmit a @e join message to
793 * notify other channel members and they then also must still call this function
794 * in their respective methods handling the @e join message. This way, how @e
795 * join and @e part operations are exactly implemented is still up to the
796 * application; for example, there might be a @e part_all method to kick out
799 * Note that channel slaves are explicitly trusted to execute such methods
800 * correctly; not doing so correctly will result in either denying other slaves
801 * access or offering access to channel data to non-members.
803 * @param ch Channel handle.
804 * @param slave_key Identity of channel slave to add.
805 * @param announced_at ID of the message that announced the membership change.
806 * @param effective_since Addition of slave is in effect since this message ID.
809 GNUNET_PSYC_channel_slave_add (struct GNUNET_PSYC_Channel *ch,
810 const struct GNUNET_CRYPTO_EccPublicSignKey
812 uint64_t announced_at,
813 uint64_t effective_since)
815 struct ChannelSlaveAdd *slvadd;
816 struct OperationHandle *op = GNUNET_malloc (sizeof (*op) + sizeof (*slvadd));
817 slvadd = (struct ChannelSlaveAdd *) &op[1];
818 op->msg = (struct GNUNET_MessageHeader *) slvadd;
820 slvadd->header.type = GNUNET_MESSAGE_TYPE_PSYC_CHANNEL_SLAVE_ADD;
821 slvadd->header.size = htons (sizeof (*slvadd));
822 slvadd->announced_at = GNUNET_htonll (announced_at);
823 slvadd->effective_since = GNUNET_htonll (effective_since);
825 GNUNET_CONTAINER_DLL_insert (ch->transmit_head, ch->transmit_tail, op);
831 * Remove a slave from the channel's membership list.
833 * Note that this will NOT generate any PSYC traffic, it will merely update the
834 * local database to modify how we react to <em>membership test</em> queries.
835 * The channel master still needs to explicitly transmit a @e part message to
836 * notify other channel members and they then also must still call this function
837 * in their respective methods handling the @e part message. This way, how
838 * @e join and @e part operations are exactly implemented is still up to the
839 * application; for example, there might be a @e part_all message to kick out
842 * Note that channel members are explicitly trusted to perform these
843 * operations correctly; not doing so correctly will result in either
844 * denying members access or offering access to channel data to
847 * @param ch Channel handle.
848 * @param slave_key Identity of channel slave to remove.
849 * @param announced_at ID of the message that announced the membership change.
852 GNUNET_PSYC_channel_slave_remove (struct GNUNET_PSYC_Channel *ch,
853 const struct GNUNET_CRYPTO_EccPublicSignKey
855 uint64_t announced_at)
857 struct ChannelSlaveRemove *slvrm;
858 struct OperationHandle *op = GNUNET_malloc (sizeof (*op) + sizeof (*slvrm));
859 slvrm = (struct ChannelSlaveRemove *) &op[1];
860 op->msg = (struct GNUNET_MessageHeader *) slvrm;
862 slvrm->header.type = GNUNET_MESSAGE_TYPE_PSYC_CHANNEL_SLAVE_RM;
863 slvrm->header.size = htons (sizeof (*slvrm));
864 slvrm->announced_at = GNUNET_htonll (announced_at);
866 GNUNET_CONTAINER_DLL_insert (ch->transmit_head, ch->transmit_tail, op);
872 * Request to be told the message history of the channel.
874 * Historic messages (but NOT the state at the time) will be replayed (given to
875 * the normal method handlers) if available and if access is permitted.
877 * To get the latest message, use 0 for both the start and end message ID.
879 * @param ch Which channel should be replayed?
880 * @param start_message_id Earliest interesting point in history.
881 * @param end_message_id Last (exclusive) interesting point in history.
882 * @param method Function to invoke on messages received from the story.
883 * @param finish_cb Function to call when the requested story has been fully
884 * told (counting message IDs might not suffice, as some messages
885 * might be secret and thus the listener would not know the story is
886 * finished without being told explicitly)
890 * has been called, the client must not call
891 * GNUNET_PSYC_channel_story_tell_cancel() anymore.
892 * @param cls Closure for the callbacks.
893 * @return Handle to cancel story telling operation.
895 struct GNUNET_PSYC_Story *
896 GNUNET_PSYC_channel_story_tell (struct GNUNET_PSYC_Channel *ch,
897 uint64_t start_message_id,
898 uint64_t end_message_id,
899 GNUNET_PSYC_Method method,
900 GNUNET_PSYC_FinishCallback *finish_cb,
908 * Abort story telling.
910 * This function must not be called from within method handlers (as given to
911 * GNUNET_PSYC_slave_join()) of the slave.
913 * @param story Story telling operation to stop.
916 GNUNET_PSYC_channel_story_tell_cancel (struct GNUNET_PSYC_Story *story)
923 * Retrieve the best matching channel state variable.
925 * If the requested variable name is not present in the state, the nearest
926 * less-specific name is matched; for example, requesting "_a_b" will match "_a"
927 * if "_a_b" does not exist.
929 * @param channel Channel handle.
930 * @param full_name Full name of the requested variable, the actual variable
931 * returned might have a shorter name..
932 * @param cb Function called once when a matching state variable is found.
933 * Not called if there's no matching state variable.
934 * @param cb_cls Closure for the callbacks.
935 * @return Handle that can be used to cancel the query operation.
937 struct GNUNET_PSYC_StateQuery *
938 GNUNET_PSYC_channel_state_get (struct GNUNET_PSYC_Channel *channel,
939 const char *full_name,
940 GNUNET_PSYC_StateCallback cb,
948 * Return all channel state variables whose name matches a given prefix.
950 * A name matches if it starts with the given @a name_prefix, thus requesting
951 * the empty prefix ("") will match all values; requesting "_a_b" will also
952 * return values stored under "_a_b_c".
954 * The @a state_cb is invoked on all matching state variables asynchronously, as
955 * the state is stored in and retrieved from the PSYCstore,
957 * @param channel Channel handle.
958 * @param name_prefix Prefix of the state variable name to match.
959 * @param cb Function to call with the matching state variables.
960 * @param cb_cls Closure for the callbacks.
961 * @return Handle that can be used to cancel the query operation.
963 struct GNUNET_PSYC_StateQuery *
964 GNUNET_PSYC_channel_state_get_prefix (struct GNUNET_PSYC_Channel *channel,
965 const char *name_prefix,
966 GNUNET_PSYC_StateCallback cb,
974 * Cancel a state query operation.
976 * @param query Handle for the operation to cancel.
979 GNUNET_PSYC_channel_state_get_cancel (struct GNUNET_PSYC_StateQuery *query)
985 /* end of psyc_api.c */