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
36 #include "gnunet_util_lib.h"
37 #include "gnunet_env_lib.h"
38 #include "gnunet_multicast_service.h"
39 #include "gnunet_psyc_service.h"
40 #include "gnunet_psyc_util_lib.h"
43 #define LOG(kind,...) GNUNET_log_from (kind, "psyc-api",__VA_ARGS__)
47 * Handle to access PSYC channel operations for both the master and slaves.
49 struct GNUNET_PSYC_Channel
52 * Configuration to use.
54 const struct GNUNET_CONFIGURATION_Handle *cfg;
57 * Client connection to the service.
59 struct GNUNET_CLIENT_MANAGER_Connection *client;
62 * Transmission handle;
64 struct GNUNET_PSYC_TransmitHandle *tmit;
69 struct GNUNET_PSYC_ReceiveHandle *recv;
72 * Message to send on reconnect.
74 struct GNUNET_MessageHeader *connect_msg;
77 * Function called after disconnected from the service.
79 GNUNET_ContinuationCallback disconnect_cb;
82 * Closure for @a disconnect_cb.
87 * Are we polling for incoming messages right now?
92 * Is this a master or slave channel?
97 * Is this channel in the process of disconnecting from the service?
98 * #GNUNET_YES or #GNUNET_NO
100 uint8_t is_disconnecting;
105 * Handle for the master of a PSYC channel.
107 struct GNUNET_PSYC_Master
109 struct GNUNET_PSYC_Channel chn;
111 GNUNET_PSYC_MasterStartCallback start_cb;
114 * Join request callback.
116 GNUNET_PSYC_JoinRequestCallback join_req_cb;
119 * Closure for the callbacks.
126 * Handle for a PSYC channel slave.
128 struct GNUNET_PSYC_Slave
130 struct GNUNET_PSYC_Channel chn;
132 GNUNET_PSYC_SlaveConnectCallback connect_cb;
134 GNUNET_PSYC_JoinDecisionCallback join_dcsn_cb;
137 * Closure for the callbacks.
144 * Handle that identifies a join request.
146 * Used to match calls to #GNUNET_PSYC_JoinRequestCallback to the
147 * corresponding calls to GNUNET_PSYC_join_decision().
149 struct GNUNET_PSYC_JoinHandle
151 struct GNUNET_PSYC_Master *mst;
152 struct GNUNET_CRYPTO_EcdsaPublicKey slave_key;
157 * Handle for a pending PSYC transmission operation.
159 struct GNUNET_PSYC_SlaveTransmitHandle
166 * Handle to a story telling operation.
168 struct GNUNET_PSYC_Story
175 * Handle for a state query operation.
177 struct GNUNET_PSYC_StateQuery
184 channel_send_connect_msg (struct GNUNET_PSYC_Channel *chn)
186 uint16_t cmsg_size = ntohs (chn->connect_msg->size);
187 struct GNUNET_MessageHeader * cmsg = GNUNET_malloc (cmsg_size);
188 memcpy (cmsg, chn->connect_msg, cmsg_size);
189 GNUNET_CLIENT_MANAGER_transmit_now (chn->client, cmsg);
194 channel_recv_disconnect (void *cls,
195 struct GNUNET_CLIENT_MANAGER_Connection *client,
196 const struct GNUNET_MessageHeader *msg)
198 struct GNUNET_PSYC_Channel *
199 chn = GNUNET_CLIENT_MANAGER_get_user_context_ (client, sizeof (*chn));
200 GNUNET_CLIENT_MANAGER_reconnect (client);
201 channel_send_connect_msg (chn);
206 channel_recv_message (void *cls,
207 struct GNUNET_CLIENT_MANAGER_Connection *client,
208 const struct GNUNET_MessageHeader *msg)
210 struct GNUNET_PSYC_Channel *
211 chn = GNUNET_CLIENT_MANAGER_get_user_context_ (client, sizeof (*chn));
212 GNUNET_PSYC_receive_message (chn->recv,
213 (const struct GNUNET_PSYC_MessageHeader *) msg);
218 channel_recv_message_ack (void *cls,
219 struct GNUNET_CLIENT_MANAGER_Connection *client,
220 const struct GNUNET_MessageHeader *msg)
222 struct GNUNET_PSYC_Channel *
223 chn = GNUNET_CLIENT_MANAGER_get_user_context_ (client, sizeof (*chn));
224 GNUNET_PSYC_transmit_got_ack (chn->tmit);
229 master_recv_start_ack (void *cls,
230 struct GNUNET_CLIENT_MANAGER_Connection *client,
231 const struct GNUNET_MessageHeader *msg)
233 struct GNUNET_PSYC_Master *
234 mst = GNUNET_CLIENT_MANAGER_get_user_context_ (client,
235 sizeof (struct GNUNET_PSYC_Channel));
237 struct CountersResult *cres = (struct CountersResult *) msg;
238 if (NULL != mst->start_cb)
239 mst->start_cb (mst->cb_cls, GNUNET_ntohll (cres->max_message_id));
244 master_recv_join_request (void *cls,
245 struct GNUNET_CLIENT_MANAGER_Connection *client,
246 const struct GNUNET_MessageHeader *msg)
248 struct GNUNET_PSYC_Master *
249 mst = GNUNET_CLIENT_MANAGER_get_user_context_ (client,
250 sizeof (struct GNUNET_PSYC_Channel));
251 if (NULL == mst->join_req_cb)
254 const struct GNUNET_PSYC_JoinRequestMessage *
255 req = (const struct GNUNET_PSYC_JoinRequestMessage *) msg;
256 const struct GNUNET_PSYC_Message *join_msg = NULL;
257 if (sizeof (*req) + sizeof (*join_msg) <= ntohs (req->header.size))
259 join_msg = (struct GNUNET_PSYC_Message *) &req[1];
260 LOG (GNUNET_ERROR_TYPE_ERROR,
261 "Received join_msg of type %u and size %u.\n",
262 ntohs (join_msg->header.type), ntohs (join_msg->header.size));
265 struct GNUNET_PSYC_JoinHandle *jh = GNUNET_malloc (sizeof (*jh));
267 jh->slave_key = req->slave_key;
269 if (NULL != mst->join_req_cb)
270 mst->join_req_cb (mst->cb_cls, req, &req->slave_key, join_msg, jh);
275 slave_recv_join_ack (void *cls,
276 struct GNUNET_CLIENT_MANAGER_Connection *client,
277 const struct GNUNET_MessageHeader *msg)
279 struct GNUNET_PSYC_Slave *
280 slv = GNUNET_CLIENT_MANAGER_get_user_context_ (client,
281 sizeof (struct GNUNET_PSYC_Channel));
282 struct CountersResult *cres = (struct CountersResult *) msg;
283 if (NULL != slv->connect_cb)
284 slv->connect_cb (slv->cb_cls, GNUNET_ntohll (cres->max_message_id));
289 slave_recv_join_decision (void *cls,
290 struct GNUNET_CLIENT_MANAGER_Connection *client,
291 const struct GNUNET_MessageHeader *msg)
293 struct GNUNET_PSYC_Slave *
294 slv = GNUNET_CLIENT_MANAGER_get_user_context_ (client,
295 sizeof (struct GNUNET_PSYC_Channel));
296 const struct GNUNET_PSYC_JoinDecisionMessage *
297 dcsn = (const struct GNUNET_PSYC_JoinDecisionMessage *) msg;
299 struct GNUNET_PSYC_Message *pmsg = NULL;
300 if (ntohs (dcsn->header.size) <= sizeof (*dcsn) + sizeof (*pmsg))
301 pmsg = (struct GNUNET_PSYC_Message *) &dcsn[1];
303 if (NULL != slv->join_dcsn_cb)
304 slv->join_dcsn_cb (slv->cb_cls, dcsn, ntohl (dcsn->is_admitted), pmsg);
308 static struct GNUNET_CLIENT_MANAGER_MessageHandler master_handlers[] =
310 { &channel_recv_message, NULL,
311 GNUNET_MESSAGE_TYPE_PSYC_MESSAGE,
312 sizeof (struct GNUNET_PSYC_MessageHeader), GNUNET_YES },
314 { &channel_recv_message_ack, NULL,
315 GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_ACK,
316 sizeof (struct GNUNET_MessageHeader), GNUNET_NO },
318 { &master_recv_start_ack, NULL,
319 GNUNET_MESSAGE_TYPE_PSYC_MASTER_START_ACK,
320 sizeof (struct CountersResult), GNUNET_NO },
322 { &master_recv_join_request, NULL,
323 GNUNET_MESSAGE_TYPE_PSYC_JOIN_REQUEST,
324 sizeof (struct GNUNET_PSYC_JoinRequestMessage), GNUNET_YES },
326 { &channel_recv_disconnect, NULL, 0, 0, GNUNET_NO },
328 { NULL, NULL, 0, 0, GNUNET_NO }
332 static struct GNUNET_CLIENT_MANAGER_MessageHandler slave_handlers[] =
334 { &channel_recv_message, NULL,
335 GNUNET_MESSAGE_TYPE_PSYC_MESSAGE,
336 sizeof (struct GNUNET_PSYC_MessageHeader), GNUNET_YES },
338 { &channel_recv_message_ack, NULL,
339 GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_ACK,
340 sizeof (struct GNUNET_MessageHeader), GNUNET_NO },
342 { &slave_recv_join_ack, NULL,
343 GNUNET_MESSAGE_TYPE_PSYC_SLAVE_JOIN_ACK,
344 sizeof (struct CountersResult), GNUNET_NO },
346 { &slave_recv_join_decision, NULL,
347 GNUNET_MESSAGE_TYPE_PSYC_JOIN_DECISION,
348 sizeof (struct GNUNET_PSYC_JoinDecisionMessage), GNUNET_YES },
350 { &channel_recv_disconnect, NULL, 0, 0, GNUNET_NO },
352 { NULL, NULL, 0, 0, GNUNET_NO }
357 channel_cleanup (struct GNUNET_PSYC_Channel *chn)
359 GNUNET_PSYC_transmit_destroy (chn->tmit);
360 GNUNET_PSYC_receive_destroy (chn->recv);
361 GNUNET_free (chn->connect_msg);
362 if (NULL != chn->disconnect_cb)
363 chn->disconnect_cb (chn->disconnect_cls);
368 master_cleanup (void *cls)
370 struct GNUNET_PSYC_Master *mst = cls;
371 channel_cleanup (&mst->chn);
377 slave_cleanup (void *cls)
379 struct GNUNET_PSYC_Slave *slv = cls;
380 channel_cleanup (&slv->chn);
386 * Start a PSYC master channel.
388 * Will start a multicast group identified by the given ECC key. Messages
389 * received from group members will be given to the respective handler methods.
390 * If a new member wants to join a group, the "join" method handler will be
391 * invoked; the join handler must then generate a "join" message to approve the
392 * joining of the new member. The channel can also change group membership
393 * without explicit requests. Note that PSYC doesn't itself "understand" join
394 * or part messages, the respective methods must call other PSYC functions to
395 * inform PSYC about the meaning of the respective events.
397 * @param cfg Configuration to use (to connect to PSYC service).
398 * @param channel_key ECC key that will be used to sign messages for this
399 * PSYC session. The public key is used to identify the PSYC channel.
400 * Note that end-users will usually not use the private key directly, but
401 * rather look it up in GNS for places managed by other users, or select
402 * a file with the private key(s) when setting up their own channels
403 * FIXME: we'll likely want to use NOT the p521 curve here, but a cheaper
405 * @param policy Channel policy specifying join and history restrictions.
406 * Used to automate join decisions.
407 * @param message_cb Function to invoke on message parts received from slaves.
408 * @param join_request_cb Function to invoke when a slave wants to join.
409 * @param master_start_cb Function to invoke after the channel master started.
410 * @param cls Closure for @a method and @a join_cb.
412 * @return Handle for the channel master, NULL on error.
414 struct GNUNET_PSYC_Master *
415 GNUNET_PSYC_master_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
416 const struct GNUNET_CRYPTO_EddsaPrivateKey *channel_key,
417 enum GNUNET_PSYC_Policy policy,
418 GNUNET_PSYC_MasterStartCallback start_cb,
419 GNUNET_PSYC_JoinRequestCallback join_request_cb,
420 GNUNET_PSYC_MessageCallback message_cb,
421 GNUNET_PSYC_MessagePartCallback message_part_cb,
424 struct GNUNET_PSYC_Master *mst = GNUNET_malloc (sizeof (*mst));
425 struct GNUNET_PSYC_Channel *chn = &mst->chn;
427 struct MasterStartRequest *req = GNUNET_malloc (sizeof (*req));
428 req->header.size = htons (sizeof (*req));
429 req->header.type = htons (GNUNET_MESSAGE_TYPE_PSYC_MASTER_START);
430 req->channel_key = *channel_key;
431 req->policy = policy;
433 chn->connect_msg = (struct GNUNET_MessageHeader *) req;
435 chn->is_master = GNUNET_YES;
437 mst->start_cb = start_cb;
438 mst->join_req_cb = join_request_cb;
441 chn->client = GNUNET_CLIENT_MANAGER_connect (cfg, "psyc", master_handlers);
442 GNUNET_CLIENT_MANAGER_set_user_context_ (chn->client, mst, sizeof (*chn));
444 chn->tmit = GNUNET_PSYC_transmit_create (chn->client);
445 chn->recv = GNUNET_PSYC_receive_create (message_cb, message_part_cb, cls);
447 channel_send_connect_msg (chn);
453 * Stop a PSYC master channel.
455 * @param master PSYC channel master to stop.
456 * @param keep_active FIXME
459 GNUNET_PSYC_master_stop (struct GNUNET_PSYC_Master *mst,
461 GNUNET_ContinuationCallback stop_cb,
464 struct GNUNET_PSYC_Channel *chn = &mst->chn;
466 /* FIXME: send msg to service */
468 chn->is_disconnecting = GNUNET_YES;
469 chn->disconnect_cb = stop_cb;
470 chn->disconnect_cls = stop_cls;
472 GNUNET_CLIENT_MANAGER_disconnect (mst->chn.client, GNUNET_YES,
473 &master_cleanup, mst);
478 * Function to call with the decision made for a join request.
480 * Must be called once and only once in response to an invocation of the
481 * #GNUNET_PSYC_JoinCallback.
483 * @param jh Join request handle.
484 * @param is_admitted #GNUNET_YES if the join is approved,
485 * #GNUNET_NO if it is disapproved,
486 * #GNUNET_SYSERR if we cannot answer the request.
487 * @param relay_count Number of relays given.
488 * @param relays Array of suggested peers that might be useful relays to use
489 * when joining the multicast group (essentially a list of peers that
490 * are already part of the multicast group and might thus be willing
491 * to help with routing). If empty, only this local peer (which must
492 * be the multicast origin) is a good candidate for building the
493 * multicast tree. Note that it is unnecessary to specify our own
494 * peer identity in this array.
495 * @param join_resp Application-dependent join response message.
497 * @return #GNUNET_OK on success,
498 * #GNUNET_SYSERR if the message is too large.
501 GNUNET_PSYC_join_decision (struct GNUNET_PSYC_JoinHandle *jh,
503 uint32_t relay_count,
504 const struct GNUNET_PeerIdentity *relays,
505 const struct GNUNET_PSYC_Message *join_resp)
507 struct GNUNET_PSYC_Channel *chn = &jh->mst->chn;
508 struct GNUNET_PSYC_JoinDecisionMessage *dcsn;
509 uint16_t join_resp_size
510 = (NULL != join_resp) ? ntohs (join_resp->header.size) : 0;
511 uint16_t relay_size = relay_count * sizeof (*relays);
513 if (GNUNET_MULTICAST_FRAGMENT_MAX_PAYLOAD
514 < sizeof (*dcsn) + relay_size + join_resp_size)
515 return GNUNET_SYSERR;
517 dcsn = GNUNET_malloc (sizeof (*dcsn) + relay_size + join_resp_size);
518 dcsn->header.size = htons (sizeof (*dcsn) + relay_size + join_resp_size);
519 dcsn->header.type = htons (GNUNET_MESSAGE_TYPE_PSYC_JOIN_DECISION);
520 dcsn->is_admitted = htonl (is_admitted);
521 dcsn->slave_key = jh->slave_key;
523 if (0 < join_resp_size)
524 memcpy (&dcsn[1], join_resp, join_resp_size);
526 GNUNET_CLIENT_MANAGER_transmit (chn->client, &dcsn->header);
533 * Send a message to call a method to all members in the PSYC channel.
535 * @param master Handle to the PSYC channel.
536 * @param method_name Which method should be invoked.
537 * @param notify_mod Function to call to obtain modifiers.
538 * @param notify_data Function to call to obtain fragments of the data.
539 * @param notify_cls Closure for @a notify_mod and @a notify_data.
540 * @param flags Flags for the message being transmitted.
542 * @return Transmission handle, NULL on error (i.e. more than one request queued).
544 struct GNUNET_PSYC_MasterTransmitHandle *
545 GNUNET_PSYC_master_transmit (struct GNUNET_PSYC_Master *mst,
546 const char *method_name,
547 GNUNET_PSYC_TransmitNotifyModifier notify_mod,
548 GNUNET_PSYC_TransmitNotifyData notify_data,
550 enum GNUNET_PSYC_MasterTransmitFlags flags)
553 == GNUNET_PSYC_transmit_message (mst->chn.tmit, method_name, NULL,
554 notify_mod, notify_data, notify_cls,
556 return (struct GNUNET_PSYC_MasterTransmitHandle *) mst->chn.tmit;
563 * Resume transmission to the channel.
565 * @param tmit Handle of the request that is being resumed.
568 GNUNET_PSYC_master_transmit_resume (struct GNUNET_PSYC_MasterTransmitHandle *tmit)
570 GNUNET_PSYC_transmit_resume ((struct GNUNET_PSYC_TransmitHandle *) tmit);
575 * Abort transmission request to the channel.
577 * @param tmit Handle of the request that is being aborted.
580 GNUNET_PSYC_master_transmit_cancel (struct GNUNET_PSYC_MasterTransmitHandle *tmit)
582 GNUNET_PSYC_transmit_cancel ((struct GNUNET_PSYC_TransmitHandle *) tmit);
587 * Convert a channel @a master to a @e channel handle to access the @e channel
590 * @param master Channel master handle.
592 * @return Channel handle, valid for as long as @a master is valid.
594 struct GNUNET_PSYC_Channel *
595 GNUNET_PSYC_master_get_channel (struct GNUNET_PSYC_Master *master)
602 * Join a PSYC channel.
604 * The entity joining is always the local peer. The user must immediately use
605 * the GNUNET_PSYC_slave_transmit() functions to transmit a @e join_msg to the
606 * channel; if the join request succeeds, the channel state (and @e recent
607 * method calls) will be replayed to the joining member. There is no explicit
608 * notification on failure (as the channel may simply take days to approve,
609 * and disapproval is simply being ignored).
611 * @param cfg Configuration to use.
612 * @param channel_key ECC public key that identifies the channel we wish to join.
613 * @param slave_key ECC private-public key pair that identifies the slave, and
614 * used by multicast to sign the join request and subsequent unicast
615 * requests sent to the master.
616 * @param origin Peer identity of the origin.
617 * @param relay_count Number of peers in the @a relays array.
618 * @param relays Peer identities of members of the multicast group, which serve
619 * as relays and used to join the group at.
620 * @param message_cb Function to invoke on message parts received from the
621 * channel, typically at least contains method handlers for @e join and
623 * @param slave_connect_cb Function invoked once we have connected to the
625 * @param join_decision_cb Function invoked once we have received a join
627 * @param cls Closure for @a message_cb and @a slave_joined_cb.
628 * @param method_name Method name for the join request.
629 * @param env Environment containing transient variables for the request, or NULL.
630 * @param data Payload for the join message.
631 * @param data_size Number of bytes in @a data.
633 * @return Handle for the slave, NULL on error.
635 struct GNUNET_PSYC_Slave *
636 GNUNET_PSYC_slave_join (const struct GNUNET_CONFIGURATION_Handle *cfg,
637 const struct GNUNET_CRYPTO_EddsaPublicKey *channel_key,
638 const struct GNUNET_CRYPTO_EcdsaPrivateKey *slave_key,
639 const struct GNUNET_PeerIdentity *origin,
640 uint32_t relay_count,
641 const struct GNUNET_PeerIdentity *relays,
642 GNUNET_PSYC_MessageCallback message_cb,
643 GNUNET_PSYC_MessagePartCallback message_part_cb,
644 GNUNET_PSYC_SlaveConnectCallback connect_cb,
645 GNUNET_PSYC_JoinDecisionCallback join_decision_cb,
647 const struct GNUNET_PSYC_Message *join_msg)
649 struct GNUNET_PSYC_Slave *slv = GNUNET_malloc (sizeof (*slv));
650 struct GNUNET_PSYC_Channel *chn = &slv->chn;
652 uint16_t relay_size = relay_count * sizeof (*relays);
653 uint16_t join_msg_size = ntohs (join_msg->header.size);
654 struct SlaveJoinRequest *req
655 = GNUNET_malloc (sizeof (*req) + relay_size + join_msg_size);
656 req->header.size = htons (sizeof (*req)
657 + relay_count * sizeof (*relays));
658 req->header.type = htons (GNUNET_MESSAGE_TYPE_PSYC_SLAVE_JOIN);
659 req->channel_key = *channel_key;
660 req->slave_key = *slave_key;
661 req->origin = *origin;
662 req->relay_count = htonl (relay_count);
665 memcpy (&req[1], relays, relay_size);
667 if (0 < join_msg_size)
668 memcpy ((char *) &req[1] + relay_size, join_msg, join_msg_size);
670 chn->connect_msg = (struct GNUNET_MessageHeader *) req;
672 chn->is_master = GNUNET_NO;
674 slv->connect_cb = connect_cb;
675 slv->join_dcsn_cb = join_decision_cb;
678 chn->client = GNUNET_CLIENT_MANAGER_connect (cfg, "psyc", slave_handlers);
679 GNUNET_CLIENT_MANAGER_set_user_context_ (chn->client, slv, sizeof (*chn));
681 chn->recv = GNUNET_PSYC_receive_create (message_cb, message_part_cb, cls);
682 chn->tmit = GNUNET_PSYC_transmit_create (chn->client);
684 channel_send_connect_msg (chn);
690 * Part a PSYC channel.
692 * Will terminate the connection to the PSYC service. Polite clients should
693 * first explicitly send a part request (via GNUNET_PSYC_slave_transmit()).
695 * @param slave Slave handle.
698 GNUNET_PSYC_slave_part (struct GNUNET_PSYC_Slave *slv,
700 GNUNET_ContinuationCallback part_cb,
703 struct GNUNET_PSYC_Channel *chn = &slv->chn;
705 /* FIXME: send msg to service */
707 chn->is_disconnecting = GNUNET_YES;
708 chn->disconnect_cb = part_cb;
709 chn->disconnect_cls = part_cls;
711 GNUNET_CLIENT_MANAGER_disconnect (slv->chn.client, GNUNET_YES,
712 &slave_cleanup, slv);
717 * Request a message to be sent to the channel master.
719 * @param slave Slave handle.
720 * @param method_name Which (PSYC) method should be invoked (on host).
721 * @param notify_mod Function to call to obtain modifiers.
722 * @param notify_data Function to call to obtain fragments of the data.
723 * @param notify_cls Closure for @a notify.
724 * @param flags Flags for the message being transmitted.
726 * @return Transmission handle, NULL on error (i.e. more than one request
729 struct GNUNET_PSYC_SlaveTransmitHandle *
730 GNUNET_PSYC_slave_transmit (struct GNUNET_PSYC_Slave *slv,
731 const char *method_name,
732 GNUNET_PSYC_TransmitNotifyModifier notify_mod,
733 GNUNET_PSYC_TransmitNotifyData notify_data,
735 enum GNUNET_PSYC_SlaveTransmitFlags flags)
739 == GNUNET_PSYC_transmit_message (slv->chn.tmit, method_name, NULL,
740 notify_mod, notify_data, notify_cls,
742 return (struct GNUNET_PSYC_SlaveTransmitHandle *) slv->chn.tmit;
749 * Resume transmission to the master.
751 * @param tmit Handle of the request that is being resumed.
754 GNUNET_PSYC_slave_transmit_resume (struct GNUNET_PSYC_SlaveTransmitHandle *tmit)
756 GNUNET_PSYC_transmit_resume ((struct GNUNET_PSYC_TransmitHandle *) tmit);
761 * Abort transmission request to master.
763 * @param tmit Handle of the request that is being aborted.
766 GNUNET_PSYC_slave_transmit_cancel (struct GNUNET_PSYC_SlaveTransmitHandle *tmit)
768 GNUNET_PSYC_transmit_cancel ((struct GNUNET_PSYC_TransmitHandle *) tmit);
773 * Convert @a slave to a @e channel handle to access the @e channel APIs.
775 * @param slv Slave handle.
777 * @return Channel handle, valid for as long as @a slave is valid.
779 struct GNUNET_PSYC_Channel *
780 GNUNET_PSYC_slave_get_channel (struct GNUNET_PSYC_Slave *slv)
787 * Add a slave to the channel's membership list.
789 * Note that this will NOT generate any PSYC traffic, it will merely update the
790 * local database to modify how we react to <em>membership test</em> queries.
791 * The channel master still needs to explicitly transmit a @e join message to
792 * notify other channel members and they then also must still call this function
793 * in their respective methods handling the @e join message. This way, how @e
794 * join and @e part operations are exactly implemented is still up to the
795 * application; for example, there might be a @e part_all method to kick out
798 * Note that channel slaves are explicitly trusted to execute such methods
799 * correctly; not doing so correctly will result in either denying other slaves
800 * access or offering access to channel data to non-members.
802 * @param channel Channel handle.
803 * @param slave_key Identity of channel slave to add.
804 * @param announced_at ID of the message that announced the membership change.
805 * @param effective_since Addition of slave is in effect since this message ID.
808 GNUNET_PSYC_channel_slave_add (struct GNUNET_PSYC_Channel *chn,
809 const struct GNUNET_CRYPTO_EcdsaPublicKey *slave_key,
810 uint64_t announced_at,
811 uint64_t effective_since)
813 struct ChannelSlaveAddRequest *add = GNUNET_malloc (sizeof (*add));
814 add->header.type = htons (GNUNET_MESSAGE_TYPE_PSYC_CHANNEL_SLAVE_ADD);
815 add->header.size = htons (sizeof (*add));
816 add->announced_at = GNUNET_htonll (announced_at);
817 add->effective_since = GNUNET_htonll (effective_since);
818 GNUNET_CLIENT_MANAGER_transmit (chn->client, &add->header);
823 * Remove a slave from the channel's membership list.
825 * Note that this will NOT generate any PSYC traffic, it will merely update the
826 * local database to modify how we react to <em>membership test</em> queries.
827 * The channel master still needs to explicitly transmit a @e part message to
828 * notify other channel members and they then also must still call this function
829 * in their respective methods handling the @e part message. This way, how
830 * @e join and @e part operations are exactly implemented is still up to the
831 * application; for example, there might be a @e part_all message to kick out
834 * Note that channel members are explicitly trusted to perform these
835 * operations correctly; not doing so correctly will result in either
836 * denying members access or offering access to channel data to
839 * @param channel Channel handle.
840 * @param slave_key Identity of channel slave to remove.
841 * @param announced_at ID of the message that announced the membership change.
844 GNUNET_PSYC_channel_slave_remove (struct GNUNET_PSYC_Channel *chn,
845 const struct GNUNET_CRYPTO_EcdsaPublicKey *slave_key,
846 uint64_t announced_at)
848 struct ChannelSlaveRemoveRequest *rm = GNUNET_malloc (sizeof (*rm));
849 rm->header.type = htons (GNUNET_MESSAGE_TYPE_PSYC_CHANNEL_SLAVE_RM);
850 rm->header.size = htons (sizeof (*rm));
851 rm->announced_at = GNUNET_htonll (announced_at);
852 GNUNET_CLIENT_MANAGER_transmit (chn->client, &rm->header);
857 * Request to be told the message history of the channel.
859 * Historic messages (but NOT the state at the time) will be replayed (given to
860 * the normal method handlers) if available and if access is permitted.
862 * To get the latest message, use 0 for both the start and end message ID.
864 * @param channel Which channel should be replayed?
865 * @param start_message_id Earliest interesting point in history.
866 * @param end_message_id Last (exclusive) interesting point in history.
867 * @param message_cb Function to invoke on message parts received from the story.
868 * @param finish_cb Function to call when the requested story has been fully
869 * told (counting message IDs might not suffice, as some messages
870 * might be secret and thus the listener would not know the story is
871 * finished without being told explicitly) once this function
872 * has been called, the client must not call
873 * GNUNET_PSYC_channel_story_tell_cancel() anymore.
874 * @param cls Closure for the callbacks.
876 * @return Handle to cancel story telling operation.
878 struct GNUNET_PSYC_Story *
879 GNUNET_PSYC_channel_story_tell (struct GNUNET_PSYC_Channel *channel,
880 uint64_t start_message_id,
881 uint64_t end_message_id,
882 GNUNET_PSYC_MessageCallback message_cb,
883 GNUNET_PSYC_MessagePartCallback message_part_cb,
884 GNUNET_PSYC_FinishCallback finish_cb,
892 * Abort story telling.
894 * This function must not be called from within method handlers (as given to
895 * GNUNET_PSYC_slave_join()) of the slave.
897 * @param story Story telling operation to stop.
900 GNUNET_PSYC_channel_story_tell_cancel (struct GNUNET_PSYC_Story *story)
907 * Retrieve the best matching channel state variable.
909 * If the requested variable name is not present in the state, the nearest
910 * less-specific name is matched; for example, requesting "_a_b" will match "_a"
911 * if "_a_b" does not exist.
913 * @param channel Channel handle.
914 * @param full_name Full name of the requested variable, the actual variable
915 * returned might have a shorter name..
916 * @param cb Function called once when a matching state variable is found.
917 * Not called if there's no matching state variable.
918 * @param cb_cls Closure for the callbacks.
920 * @return Handle that can be used to cancel the query operation.
922 struct GNUNET_PSYC_StateQuery *
923 GNUNET_PSYC_channel_state_get (struct GNUNET_PSYC_Channel *channel,
924 const char *full_name,
925 GNUNET_PSYC_StateCallback cb,
933 * Return all channel state variables whose name matches a given prefix.
935 * A name matches if it starts with the given @a name_prefix, thus requesting
936 * the empty prefix ("") will match all values; requesting "_a_b" will also
937 * return values stored under "_a_b_c".
939 * The @a state_cb is invoked on all matching state variables asynchronously, as
940 * the state is stored in and retrieved from the PSYCstore,
942 * @param channel Channel handle.
943 * @param name_prefix Prefix of the state variable name to match.
944 * @param cb Function to call with the matching state variables.
945 * @param cb_cls Closure for the callbacks.
947 * @return Handle that can be used to cancel the query operation.
949 struct GNUNET_PSYC_StateQuery *
950 GNUNET_PSYC_channel_state_get_prefix (struct GNUNET_PSYC_Channel *channel,
951 const char *name_prefix,
952 GNUNET_PSYC_StateCallback cb,
960 * Cancel a state query operation.
962 * @param query Handle for the operation to cancel.
965 GNUNET_PSYC_channel_state_get_cancel (struct GNUNET_PSYC_StateQuery *query)
971 /* end of psyc_api.c */