2 * This file is part of GNUnet
3 * Copyright (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__)
46 struct OperationListItem
48 struct OperationListItem *prev;
49 struct OperationListItem *next;
57 * Continuation to invoke with the result of an operation.
59 GNUNET_PSYC_ResultCallback result_cb;
62 * State variable result callback.
64 GNUNET_PSYC_StateVarCallback state_var_cb;
67 * Closure for the callbacks.
74 * Handle to access PSYC channel operations for both the master and slaves.
76 struct GNUNET_PSYC_Channel
79 * Configuration to use.
81 const struct GNUNET_CONFIGURATION_Handle *cfg;
84 * Client connection to the service.
86 struct GNUNET_CLIENT_MANAGER_Connection *client;
89 * Transmission handle;
91 struct GNUNET_PSYC_TransmitHandle *tmit;
96 struct GNUNET_PSYC_ReceiveHandle *recv;
99 * Message to send on reconnect.
101 struct GNUNET_MessageHeader *connect_msg;
104 * Function called after disconnected from the service.
106 GNUNET_ContinuationCallback disconnect_cb;
109 * Closure for @a disconnect_cb.
111 void *disconnect_cls;
114 * First operation in the linked list.
116 struct OperationListItem *op_head;
119 * Last operation in the linked list.
121 struct OperationListItem *op_tail;
124 * Last operation ID used.
129 * Are we polling for incoming messages right now?
134 * Is this a master or slave channel?
139 * Is this channel in the process of disconnecting from the service?
140 * #GNUNET_YES or #GNUNET_NO
142 uint8_t is_disconnecting;
147 * Handle for the master of a PSYC channel.
149 struct GNUNET_PSYC_Master
151 struct GNUNET_PSYC_Channel chn;
153 GNUNET_PSYC_MasterStartCallback start_cb;
156 * Join request callback.
158 GNUNET_PSYC_JoinRequestCallback join_req_cb;
161 * Closure for the callbacks.
168 * Handle for a PSYC channel slave.
170 struct GNUNET_PSYC_Slave
172 struct GNUNET_PSYC_Channel chn;
174 GNUNET_PSYC_SlaveConnectCallback connect_cb;
176 GNUNET_PSYC_JoinDecisionCallback join_dcsn_cb;
179 * Closure for the callbacks.
186 * Handle that identifies a join request.
188 * Used to match calls to #GNUNET_PSYC_JoinRequestCallback to the
189 * corresponding calls to GNUNET_PSYC_join_decision().
191 struct GNUNET_PSYC_JoinHandle
193 struct GNUNET_PSYC_Master *mst;
194 struct GNUNET_CRYPTO_EcdsaPublicKey slave_key;
199 * Handle for a pending PSYC transmission operation.
201 struct GNUNET_PSYC_SlaveTransmitHandle
208 * Get a fresh operation ID to distinguish between PSYCstore requests.
210 * @param h Handle to the PSYCstore service.
211 * @return next operation id to use
214 op_get_next_id (struct GNUNET_PSYC_Channel *chn)
216 return ++chn->last_op_id;
221 * Find operation by ID.
223 * @return Operation, or NULL if none found.
225 static struct OperationListItem *
226 op_find_by_id (struct GNUNET_PSYC_Channel *chn, uint64_t op_id)
228 struct OperationListItem *op = chn->op_head;
231 if (op->op_id == op_id)
240 op_add (struct GNUNET_PSYC_Channel *chn, GNUNET_PSYC_ResultCallback result_cb,
243 if (NULL == result_cb)
246 struct OperationListItem *op = GNUNET_malloc (sizeof (*op));
247 op->op_id = op_get_next_id (chn);
248 op->result_cb = result_cb;
250 GNUNET_CONTAINER_DLL_insert_tail (chn->op_head, chn->op_tail, op);
252 LOG (GNUNET_ERROR_TYPE_DEBUG,
253 "%p Added operation #%" PRIu64 "\n", chn, op->op_id);
259 op_result (struct GNUNET_PSYC_Channel *chn, uint64_t op_id,
260 int64_t result_code, const char *err_msg)
262 LOG (GNUNET_ERROR_TYPE_DEBUG,
263 "%p Received result for operation #%" PRIu64 ": %" PRId64 " (%s)\n",
264 chn, op_id, result_code, err_msg);
268 struct OperationListItem *op = op_find_by_id (chn, op_id);
271 LOG (GNUNET_ERROR_TYPE_WARNING,
272 "Could not find operation #%" PRIu64 "\n", op_id);
276 GNUNET_CONTAINER_DLL_remove (chn->op_head, chn->op_tail, op);
278 if (NULL != op->result_cb)
279 op->result_cb (op->cls, result_code, err_msg);
287 channel_send_connect_msg (struct GNUNET_PSYC_Channel *chn)
289 uint16_t cmsg_size = ntohs (chn->connect_msg->size);
290 struct GNUNET_MessageHeader * cmsg = GNUNET_malloc (cmsg_size);
291 memcpy (cmsg, chn->connect_msg, cmsg_size);
292 GNUNET_CLIENT_MANAGER_transmit_now (chn->client, cmsg);
297 channel_recv_disconnect (void *cls,
298 struct GNUNET_CLIENT_MANAGER_Connection *client,
299 const struct GNUNET_MessageHeader *msg)
301 struct GNUNET_PSYC_Channel *
302 chn = GNUNET_CLIENT_MANAGER_get_user_context_ (client, sizeof (*chn));
303 GNUNET_CLIENT_MANAGER_reconnect (client);
304 channel_send_connect_msg (chn);
309 channel_recv_result (void *cls,
310 struct GNUNET_CLIENT_MANAGER_Connection *client,
311 const struct GNUNET_MessageHeader *msg)
313 struct GNUNET_PSYC_Channel *
314 chn = GNUNET_CLIENT_MANAGER_get_user_context_ (client, sizeof (*chn));
316 uint16_t size = ntohs (msg->size);
317 const struct OperationResult *res = (const struct OperationResult *) msg;
318 const char *err_msg = NULL;
320 if (sizeof (struct OperationResult) < size)
322 err_msg = (const char *) &res[1];
323 if ('\0' != err_msg[size - sizeof (struct OperationResult) - 1])
330 op_result (chn, GNUNET_ntohll (res->op_id),
331 GNUNET_ntohll (res->result_code) + INT64_MIN, err_msg);
336 channel_recv_state_result (void *cls,
337 struct GNUNET_CLIENT_MANAGER_Connection *client,
338 const struct GNUNET_MessageHeader *msg)
340 struct GNUNET_PSYC_Channel *
341 chn = GNUNET_CLIENT_MANAGER_get_user_context_ (client, sizeof (*chn));
343 const struct OperationResult *res = (const struct OperationResult *) msg;
344 struct OperationListItem *op = op_find_by_id (chn, GNUNET_ntohll (res->op_id));
345 if (NULL == op || NULL == op->state_var_cb)
348 const struct GNUNET_MessageHeader *modc = (struct GNUNET_MessageHeader *) &op[1];
349 uint16_t modc_size = ntohs (modc->size);
350 if (ntohs (msg->size) - sizeof (*msg) != modc_size)
355 switch (ntohs (modc->type))
357 case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_MODIFIER:
359 const struct GNUNET_PSYC_MessageModifier *
360 mod = (const struct GNUNET_PSYC_MessageModifier *) modc;
362 const char *name = (const char *) &mod[1];
363 uint16_t name_size = ntohs (mod->name_size);
364 if ('\0' != name[name_size - 1])
369 op->state_var_cb (op->cls, name, name + name_size, ntohs (mod->value_size));
373 case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_MOD_CONT:
374 op->state_var_cb (op->cls, NULL, (const char *) &modc[1],
375 modc_size - sizeof (*modc));
382 channel_recv_message (void *cls,
383 struct GNUNET_CLIENT_MANAGER_Connection *client,
384 const struct GNUNET_MessageHeader *msg)
386 struct GNUNET_PSYC_Channel *
387 chn = GNUNET_CLIENT_MANAGER_get_user_context_ (client, sizeof (*chn));
388 GNUNET_PSYC_receive_message (chn->recv,
389 (const struct GNUNET_PSYC_MessageHeader *) msg);
394 channel_recv_message_ack (void *cls,
395 struct GNUNET_CLIENT_MANAGER_Connection *client,
396 const struct GNUNET_MessageHeader *msg)
398 struct GNUNET_PSYC_Channel *
399 chn = GNUNET_CLIENT_MANAGER_get_user_context_ (client, sizeof (*chn));
400 GNUNET_PSYC_transmit_got_ack (chn->tmit);
405 master_recv_start_ack (void *cls,
406 struct GNUNET_CLIENT_MANAGER_Connection *client,
407 const struct GNUNET_MessageHeader *msg)
409 struct GNUNET_PSYC_Master *
410 mst = GNUNET_CLIENT_MANAGER_get_user_context_ (client,
411 sizeof (struct GNUNET_PSYC_Channel));
413 struct GNUNET_PSYC_CountersResultMessage *
414 cres = (struct GNUNET_PSYC_CountersResultMessage *) msg;
415 int32_t result = ntohl (cres->result_code) + INT32_MIN;
416 if (GNUNET_OK != result && GNUNET_NO != result)
418 LOG (GNUNET_ERROR_TYPE_ERROR, "Could not start master.\n");
421 if (NULL != mst->start_cb)
422 mst->start_cb (mst->cb_cls, result, GNUNET_ntohll (cres->max_message_id));
427 master_recv_join_request (void *cls,
428 struct GNUNET_CLIENT_MANAGER_Connection *client,
429 const struct GNUNET_MessageHeader *msg)
431 struct GNUNET_PSYC_Master *
432 mst = GNUNET_CLIENT_MANAGER_get_user_context_ (client,
433 sizeof (struct GNUNET_PSYC_Channel));
434 if (NULL == mst->join_req_cb)
437 const struct GNUNET_PSYC_JoinRequestMessage *
438 req = (const struct GNUNET_PSYC_JoinRequestMessage *) msg;
439 const struct GNUNET_PSYC_Message *join_msg = NULL;
440 if (sizeof (*req) + sizeof (*join_msg) <= ntohs (req->header.size))
442 join_msg = (struct GNUNET_PSYC_Message *) &req[1];
443 LOG (GNUNET_ERROR_TYPE_ERROR,
444 "Received join_msg of type %u and size %u.\n",
445 ntohs (join_msg->header.type), ntohs (join_msg->header.size));
448 struct GNUNET_PSYC_JoinHandle *jh = GNUNET_malloc (sizeof (*jh));
450 jh->slave_key = req->slave_key;
452 if (NULL != mst->join_req_cb)
453 mst->join_req_cb (mst->cb_cls, req, &req->slave_key, join_msg, jh);
458 slave_recv_join_ack (void *cls,
459 struct GNUNET_CLIENT_MANAGER_Connection *client,
460 const struct GNUNET_MessageHeader *msg)
462 struct GNUNET_PSYC_Slave *
463 slv = GNUNET_CLIENT_MANAGER_get_user_context_ (client,
464 sizeof (struct GNUNET_PSYC_Channel));
465 struct GNUNET_PSYC_CountersResultMessage *
466 cres = (struct GNUNET_PSYC_CountersResultMessage *) msg;
467 int32_t result = ntohl (cres->result_code) + INT32_MIN;
468 if (GNUNET_YES != result && GNUNET_NO != result)
470 LOG (GNUNET_ERROR_TYPE_ERROR, "Could not join slave.\n");
473 if (NULL != slv->connect_cb)
474 slv->connect_cb (slv->cb_cls, result, GNUNET_ntohll (cres->max_message_id));
479 slave_recv_join_decision (void *cls,
480 struct GNUNET_CLIENT_MANAGER_Connection *client,
481 const struct GNUNET_MessageHeader *msg)
483 struct GNUNET_PSYC_Slave *
484 slv = GNUNET_CLIENT_MANAGER_get_user_context_ (client,
485 sizeof (struct GNUNET_PSYC_Channel));
486 const struct GNUNET_PSYC_JoinDecisionMessage *
487 dcsn = (const struct GNUNET_PSYC_JoinDecisionMessage *) msg;
489 struct GNUNET_PSYC_Message *pmsg = NULL;
490 if (ntohs (dcsn->header.size) <= sizeof (*dcsn) + sizeof (*pmsg))
491 pmsg = (struct GNUNET_PSYC_Message *) &dcsn[1];
493 if (NULL != slv->join_dcsn_cb)
494 slv->join_dcsn_cb (slv->cb_cls, dcsn, ntohl (dcsn->is_admitted), pmsg);
498 static struct GNUNET_CLIENT_MANAGER_MessageHandler master_handlers[] =
500 { &channel_recv_message, NULL,
501 GNUNET_MESSAGE_TYPE_PSYC_MESSAGE,
502 sizeof (struct GNUNET_PSYC_MessageHeader), GNUNET_YES },
504 { &channel_recv_message_ack, NULL,
505 GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_ACK,
506 sizeof (struct GNUNET_MessageHeader), GNUNET_NO },
508 { &master_recv_start_ack, NULL,
509 GNUNET_MESSAGE_TYPE_PSYC_MASTER_START_ACK,
510 sizeof (struct GNUNET_PSYC_CountersResultMessage), GNUNET_NO },
512 { &master_recv_join_request, NULL,
513 GNUNET_MESSAGE_TYPE_PSYC_JOIN_REQUEST,
514 sizeof (struct GNUNET_PSYC_JoinRequestMessage), GNUNET_YES },
516 { &channel_recv_state_result, NULL,
517 GNUNET_MESSAGE_TYPE_PSYC_STATE_RESULT,
518 sizeof (struct OperationResult), GNUNET_YES },
520 { &channel_recv_result, NULL,
521 GNUNET_MESSAGE_TYPE_PSYC_RESULT_CODE,
522 sizeof (struct OperationResult), GNUNET_YES },
524 { &channel_recv_disconnect, NULL, 0, 0, GNUNET_NO },
526 { NULL, NULL, 0, 0, GNUNET_NO }
530 static struct GNUNET_CLIENT_MANAGER_MessageHandler slave_handlers[] =
532 { &channel_recv_message, NULL,
533 GNUNET_MESSAGE_TYPE_PSYC_MESSAGE,
534 sizeof (struct GNUNET_PSYC_MessageHeader), GNUNET_YES },
536 { &channel_recv_message_ack, NULL,
537 GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_ACK,
538 sizeof (struct GNUNET_MessageHeader), GNUNET_NO },
540 { &slave_recv_join_ack, NULL,
541 GNUNET_MESSAGE_TYPE_PSYC_SLAVE_JOIN_ACK,
542 sizeof (struct GNUNET_PSYC_CountersResultMessage), GNUNET_NO },
544 { &slave_recv_join_decision, NULL,
545 GNUNET_MESSAGE_TYPE_PSYC_JOIN_DECISION,
546 sizeof (struct GNUNET_PSYC_JoinDecisionMessage), GNUNET_YES },
548 { &channel_recv_state_result, NULL,
549 GNUNET_MESSAGE_TYPE_PSYC_STATE_RESULT,
550 sizeof (struct OperationResult), GNUNET_YES },
552 { &channel_recv_result, NULL,
553 GNUNET_MESSAGE_TYPE_PSYC_RESULT_CODE,
554 sizeof (struct OperationResult), GNUNET_YES },
556 { &channel_recv_disconnect, NULL, 0, 0, GNUNET_NO },
558 { NULL, NULL, 0, 0, GNUNET_NO }
563 channel_cleanup (struct GNUNET_PSYC_Channel *chn)
565 GNUNET_PSYC_transmit_destroy (chn->tmit);
566 GNUNET_PSYC_receive_destroy (chn->recv);
567 GNUNET_free (chn->connect_msg);
568 if (NULL != chn->disconnect_cb)
569 chn->disconnect_cb (chn->disconnect_cls);
574 master_cleanup (void *cls)
576 struct GNUNET_PSYC_Master *mst = cls;
577 channel_cleanup (&mst->chn);
583 slave_cleanup (void *cls)
585 struct GNUNET_PSYC_Slave *slv = cls;
586 channel_cleanup (&slv->chn);
592 * Start a PSYC master channel.
594 * Will start a multicast group identified by the given ECC key. Messages
595 * received from group members will be given to the respective handler methods.
596 * If a new member wants to join a group, the "join" method handler will be
597 * invoked; the join handler must then generate a "join" message to approve the
598 * joining of the new member. The channel can also change group membership
599 * without explicit requests. Note that PSYC doesn't itself "understand" join
600 * or part messages, the respective methods must call other PSYC functions to
601 * inform PSYC about the meaning of the respective events.
603 * @param cfg Configuration to use (to connect to PSYC service).
604 * @param channel_key ECC key that will be used to sign messages for this
605 * PSYC session. The public key is used to identify the PSYC channel.
606 * Note that end-users will usually not use the private key directly, but
607 * rather look it up in GNS for places managed by other users, or select
608 * a file with the private key(s) when setting up their own channels
609 * FIXME: we'll likely want to use NOT the p521 curve here, but a cheaper
611 * @param policy Channel policy specifying join and history restrictions.
612 * Used to automate join decisions.
613 * @param message_cb Function to invoke on message parts received from slaves.
614 * @param join_request_cb Function to invoke when a slave wants to join.
615 * @param master_start_cb Function to invoke after the channel master started.
616 * @param cls Closure for @a method and @a join_cb.
618 * @return Handle for the channel master, NULL on error.
620 struct GNUNET_PSYC_Master *
621 GNUNET_PSYC_master_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
622 const struct GNUNET_CRYPTO_EddsaPrivateKey *channel_key,
623 enum GNUNET_PSYC_Policy policy,
624 GNUNET_PSYC_MasterStartCallback start_cb,
625 GNUNET_PSYC_JoinRequestCallback join_request_cb,
626 GNUNET_PSYC_MessageCallback message_cb,
627 GNUNET_PSYC_MessagePartCallback message_part_cb,
630 struct GNUNET_PSYC_Master *mst = GNUNET_malloc (sizeof (*mst));
631 struct GNUNET_PSYC_Channel *chn = &mst->chn;
633 struct MasterStartRequest *req = GNUNET_malloc (sizeof (*req));
634 req->header.size = htons (sizeof (*req));
635 req->header.type = htons (GNUNET_MESSAGE_TYPE_PSYC_MASTER_START);
636 req->channel_key = *channel_key;
637 req->policy = policy;
639 chn->connect_msg = (struct GNUNET_MessageHeader *) req;
641 chn->is_master = GNUNET_YES;
643 mst->start_cb = start_cb;
644 mst->join_req_cb = join_request_cb;
647 chn->client = GNUNET_CLIENT_MANAGER_connect (cfg, "psyc", master_handlers);
648 GNUNET_CLIENT_MANAGER_set_user_context_ (chn->client, mst, sizeof (*chn));
650 chn->tmit = GNUNET_PSYC_transmit_create (chn->client);
651 chn->recv = GNUNET_PSYC_receive_create (message_cb, message_part_cb, cls);
653 channel_send_connect_msg (chn);
659 * Stop a PSYC master channel.
661 * @param master PSYC channel master to stop.
662 * @param keep_active FIXME
665 GNUNET_PSYC_master_stop (struct GNUNET_PSYC_Master *mst,
667 GNUNET_ContinuationCallback stop_cb,
670 struct GNUNET_PSYC_Channel *chn = &mst->chn;
672 /* FIXME: send msg to service */
674 chn->is_disconnecting = GNUNET_YES;
675 chn->disconnect_cb = stop_cb;
676 chn->disconnect_cls = stop_cls;
678 GNUNET_CLIENT_MANAGER_disconnect (mst->chn.client, GNUNET_YES,
679 &master_cleanup, mst);
684 * Function to call with the decision made for a join request.
686 * Must be called once and only once in response to an invocation of the
687 * #GNUNET_PSYC_JoinCallback.
689 * @param jh Join request handle.
690 * @param is_admitted #GNUNET_YES if the join is approved,
691 * #GNUNET_NO if it is disapproved,
692 * #GNUNET_SYSERR if we cannot answer the request.
693 * @param relay_count Number of relays given.
694 * @param relays Array of suggested peers that might be useful relays to use
695 * when joining the multicast group (essentially a list of peers that
696 * are already part of the multicast group and might thus be willing
697 * to help with routing). If empty, only this local peer (which must
698 * be the multicast origin) is a good candidate for building the
699 * multicast tree. Note that it is unnecessary to specify our own
700 * peer identity in this array.
701 * @param join_resp Application-dependent join response message.
703 * @return #GNUNET_OK on success,
704 * #GNUNET_SYSERR if the message is too large.
707 GNUNET_PSYC_join_decision (struct GNUNET_PSYC_JoinHandle *jh,
709 uint32_t relay_count,
710 const struct GNUNET_PeerIdentity *relays,
711 const struct GNUNET_PSYC_Message *join_resp)
713 struct GNUNET_PSYC_Channel *chn = &jh->mst->chn;
714 struct GNUNET_PSYC_JoinDecisionMessage *dcsn;
715 uint16_t join_resp_size
716 = (NULL != join_resp) ? ntohs (join_resp->header.size) : 0;
717 uint16_t relay_size = relay_count * sizeof (*relays);
719 if (GNUNET_MULTICAST_FRAGMENT_MAX_PAYLOAD
720 < sizeof (*dcsn) + relay_size + join_resp_size)
721 return GNUNET_SYSERR;
723 dcsn = GNUNET_malloc (sizeof (*dcsn) + relay_size + join_resp_size);
724 dcsn->header.size = htons (sizeof (*dcsn) + relay_size + join_resp_size);
725 dcsn->header.type = htons (GNUNET_MESSAGE_TYPE_PSYC_JOIN_DECISION);
726 dcsn->is_admitted = htonl (is_admitted);
727 dcsn->slave_key = jh->slave_key;
729 if (0 < join_resp_size)
730 memcpy (&dcsn[1], join_resp, join_resp_size);
732 GNUNET_CLIENT_MANAGER_transmit (chn->client, &dcsn->header);
739 * Send a message to call a method to all members in the PSYC channel.
741 * @param master Handle to the PSYC channel.
742 * @param method_name Which method should be invoked.
743 * @param notify_mod Function to call to obtain modifiers.
744 * @param notify_data Function to call to obtain fragments of the data.
745 * @param notify_cls Closure for @a notify_mod and @a notify_data.
746 * @param flags Flags for the message being transmitted.
748 * @return Transmission handle, NULL on error (i.e. more than one request queued).
750 struct GNUNET_PSYC_MasterTransmitHandle *
751 GNUNET_PSYC_master_transmit (struct GNUNET_PSYC_Master *mst,
752 const char *method_name,
753 GNUNET_PSYC_TransmitNotifyModifier notify_mod,
754 GNUNET_PSYC_TransmitNotifyData notify_data,
756 enum GNUNET_PSYC_MasterTransmitFlags flags)
759 == GNUNET_PSYC_transmit_message (mst->chn.tmit, method_name, NULL,
760 notify_mod, notify_data, notify_cls,
762 return (struct GNUNET_PSYC_MasterTransmitHandle *) mst->chn.tmit;
769 * Resume transmission to the channel.
771 * @param tmit Handle of the request that is being resumed.
774 GNUNET_PSYC_master_transmit_resume (struct GNUNET_PSYC_MasterTransmitHandle *tmit)
776 GNUNET_PSYC_transmit_resume ((struct GNUNET_PSYC_TransmitHandle *) tmit);
781 * Abort transmission request to the channel.
783 * @param tmit Handle of the request that is being aborted.
786 GNUNET_PSYC_master_transmit_cancel (struct GNUNET_PSYC_MasterTransmitHandle *tmit)
788 GNUNET_PSYC_transmit_cancel ((struct GNUNET_PSYC_TransmitHandle *) tmit);
793 * Convert a channel @a master to a @e channel handle to access the @e channel
796 * @param master Channel master handle.
798 * @return Channel handle, valid for as long as @a master is valid.
800 struct GNUNET_PSYC_Channel *
801 GNUNET_PSYC_master_get_channel (struct GNUNET_PSYC_Master *master)
808 * Join a PSYC channel.
810 * The entity joining is always the local peer. The user must immediately use
811 * the GNUNET_PSYC_slave_transmit() functions to transmit a @e join_msg to the
812 * channel; if the join request succeeds, the channel state (and @e recent
813 * method calls) will be replayed to the joining member. There is no explicit
814 * notification on failure (as the channel may simply take days to approve,
815 * and disapproval is simply being ignored).
817 * @param cfg Configuration to use.
818 * @param channel_key ECC public key that identifies the channel we wish to join.
819 * @param slave_key ECC private-public key pair that identifies the slave, and
820 * used by multicast to sign the join request and subsequent unicast
821 * requests sent to the master.
822 * @param origin Peer identity of the origin.
823 * @param relay_count Number of peers in the @a relays array.
824 * @param relays Peer identities of members of the multicast group, which serve
825 * as relays and used to join the group at.
826 * @param message_cb Function to invoke on message parts received from the
827 * channel, typically at least contains method handlers for @e join and
829 * @param slave_connect_cb Function invoked once we have connected to the
831 * @param join_decision_cb Function invoked once we have received a join
833 * @param cls Closure for @a message_cb and @a slave_joined_cb.
834 * @param method_name Method name for the join request.
835 * @param env Environment containing transient variables for the request, or NULL.
836 * @param data Payload for the join message.
837 * @param data_size Number of bytes in @a data.
839 * @return Handle for the slave, NULL on error.
841 struct GNUNET_PSYC_Slave *
842 GNUNET_PSYC_slave_join (const struct GNUNET_CONFIGURATION_Handle *cfg,
843 const struct GNUNET_CRYPTO_EddsaPublicKey *channel_key,
844 const struct GNUNET_CRYPTO_EcdsaPrivateKey *slave_key,
845 const struct GNUNET_PeerIdentity *origin,
846 uint32_t relay_count,
847 const struct GNUNET_PeerIdentity *relays,
848 GNUNET_PSYC_MessageCallback message_cb,
849 GNUNET_PSYC_MessagePartCallback message_part_cb,
850 GNUNET_PSYC_SlaveConnectCallback connect_cb,
851 GNUNET_PSYC_JoinDecisionCallback join_decision_cb,
853 const struct GNUNET_PSYC_Message *join_msg)
855 struct GNUNET_PSYC_Slave *slv = GNUNET_malloc (sizeof (*slv));
856 struct GNUNET_PSYC_Channel *chn = &slv->chn;
857 uint16_t relay_size = relay_count * sizeof (*relays);
858 uint16_t join_msg_size;
859 struct SlaveJoinRequest *req;
861 if (NULL == join_msg)
864 join_msg_size = ntohs (join_msg->header.size);
865 req = GNUNET_malloc (sizeof (*req) + relay_size + join_msg_size);
866 req->header.size = htons (sizeof (*req)
867 + relay_count * sizeof (*relays));
868 req->header.type = htons (GNUNET_MESSAGE_TYPE_PSYC_SLAVE_JOIN);
869 req->channel_key = *channel_key;
870 req->slave_key = *slave_key;
871 req->origin = *origin;
872 req->relay_count = htonl (relay_count);
875 memcpy (&req[1], relays, relay_size);
877 if (NULL != join_msg)
878 memcpy ((char *) &req[1] + relay_size,
882 chn->connect_msg = (struct GNUNET_MessageHeader *) req;
884 chn->is_master = GNUNET_NO;
886 slv->connect_cb = connect_cb;
887 slv->join_dcsn_cb = join_decision_cb;
890 chn->client = GNUNET_CLIENT_MANAGER_connect (cfg, "psyc", slave_handlers);
891 GNUNET_CLIENT_MANAGER_set_user_context_ (chn->client, slv, sizeof (*chn));
893 chn->recv = GNUNET_PSYC_receive_create (message_cb, message_part_cb, cls);
894 chn->tmit = GNUNET_PSYC_transmit_create (chn->client);
896 channel_send_connect_msg (chn);
902 * Part a PSYC channel.
904 * Will terminate the connection to the PSYC service. Polite clients should
905 * first explicitly send a part request (via GNUNET_PSYC_slave_transmit()).
907 * @param slave Slave handle.
910 GNUNET_PSYC_slave_part (struct GNUNET_PSYC_Slave *slv,
912 GNUNET_ContinuationCallback part_cb,
915 struct GNUNET_PSYC_Channel *chn = &slv->chn;
917 /* FIXME: send msg to service */
919 chn->is_disconnecting = GNUNET_YES;
920 chn->disconnect_cb = part_cb;
921 chn->disconnect_cls = part_cls;
923 GNUNET_CLIENT_MANAGER_disconnect (slv->chn.client, GNUNET_YES,
924 &slave_cleanup, slv);
929 * Request a message to be sent to the channel master.
931 * @param slave Slave handle.
932 * @param method_name Which (PSYC) method should be invoked (on host).
933 * @param notify_mod Function to call to obtain modifiers.
934 * @param notify_data Function to call to obtain fragments of the data.
935 * @param notify_cls Closure for @a notify.
936 * @param flags Flags for the message being transmitted.
938 * @return Transmission handle, NULL on error (i.e. more than one request
941 struct GNUNET_PSYC_SlaveTransmitHandle *
942 GNUNET_PSYC_slave_transmit (struct GNUNET_PSYC_Slave *slv,
943 const char *method_name,
944 GNUNET_PSYC_TransmitNotifyModifier notify_mod,
945 GNUNET_PSYC_TransmitNotifyData notify_data,
947 enum GNUNET_PSYC_SlaveTransmitFlags flags)
951 == GNUNET_PSYC_transmit_message (slv->chn.tmit, method_name, NULL,
952 notify_mod, notify_data, notify_cls,
954 return (struct GNUNET_PSYC_SlaveTransmitHandle *) slv->chn.tmit;
961 * Resume transmission to the master.
963 * @param tmit Handle of the request that is being resumed.
966 GNUNET_PSYC_slave_transmit_resume (struct GNUNET_PSYC_SlaveTransmitHandle *tmit)
968 GNUNET_PSYC_transmit_resume ((struct GNUNET_PSYC_TransmitHandle *) tmit);
973 * Abort transmission request to master.
975 * @param tmit Handle of the request that is being aborted.
978 GNUNET_PSYC_slave_transmit_cancel (struct GNUNET_PSYC_SlaveTransmitHandle *tmit)
980 GNUNET_PSYC_transmit_cancel ((struct GNUNET_PSYC_TransmitHandle *) tmit);
985 * Convert @a slave to a @e channel handle to access the @e channel APIs.
987 * @param slv Slave handle.
989 * @return Channel handle, valid for as long as @a slave is valid.
991 struct GNUNET_PSYC_Channel *
992 GNUNET_PSYC_slave_get_channel (struct GNUNET_PSYC_Slave *slv)
999 * Add a slave to the channel's membership list.
1001 * Note that this will NOT generate any PSYC traffic, it will merely update the
1002 * local database to modify how we react to <em>membership test</em> queries.
1003 * The channel master still needs to explicitly transmit a @e join message to
1004 * notify other channel members and they then also must still call this function
1005 * in their respective methods handling the @e join message. This way, how @e
1006 * join and @e part operations are exactly implemented is still up to the
1007 * application; for example, there might be a @e part_all method to kick out
1010 * Note that channel slaves are explicitly trusted to execute such methods
1011 * correctly; not doing so correctly will result in either denying other slaves
1012 * access or offering access to channel data to non-members.
1014 * @param channel Channel handle.
1015 * @param slave_key Identity of channel slave to add.
1016 * @param announced_at ID of the message that announced the membership change.
1017 * @param effective_since Addition of slave is in effect since this message ID.
1020 GNUNET_PSYC_channel_slave_add (struct GNUNET_PSYC_Channel *chn,
1021 const struct GNUNET_CRYPTO_EcdsaPublicKey *slave_key,
1022 uint64_t announced_at,
1023 uint64_t effective_since,
1024 GNUNET_PSYC_ResultCallback result_cb,
1027 struct ChannelMembershipStoreRequest *req = GNUNET_malloc (sizeof (*req));
1028 req->header.type = htons (GNUNET_MESSAGE_TYPE_PSYC_CHANNEL_MEMBERSHIP_STORE);
1029 req->header.size = htons (sizeof (*req));
1030 req->slave_key = *slave_key;
1031 req->announced_at = GNUNET_htonll (announced_at);
1032 req->effective_since = GNUNET_htonll (effective_since);
1033 req->did_join = GNUNET_YES;
1034 req->op_id = GNUNET_htonll (op_add (chn, result_cb, cls));
1036 GNUNET_CLIENT_MANAGER_transmit (chn->client, &req->header);
1041 * Remove a slave from the channel's membership list.
1043 * Note that this will NOT generate any PSYC traffic, it will merely update the
1044 * local database to modify how we react to <em>membership test</em> queries.
1045 * The channel master still needs to explicitly transmit a @e part message to
1046 * notify other channel members and they then also must still call this function
1047 * in their respective methods handling the @e part message. This way, how
1048 * @e join and @e part operations are exactly implemented is still up to the
1049 * application; for example, there might be a @e part_all message to kick out
1052 * Note that channel members are explicitly trusted to perform these
1053 * operations correctly; not doing so correctly will result in either
1054 * denying members access or offering access to channel data to
1057 * @param channel Channel handle.
1058 * @param slave_key Identity of channel slave to remove.
1059 * @param announced_at ID of the message that announced the membership change.
1062 GNUNET_PSYC_channel_slave_remove (struct GNUNET_PSYC_Channel *chn,
1063 const struct GNUNET_CRYPTO_EcdsaPublicKey *slave_key,
1064 uint64_t announced_at,
1065 GNUNET_PSYC_ResultCallback result_cb,
1068 struct ChannelMembershipStoreRequest *req = GNUNET_malloc (sizeof (*req));
1069 req->header.type = htons (GNUNET_MESSAGE_TYPE_PSYC_CHANNEL_MEMBERSHIP_STORE);
1070 req->header.size = htons (sizeof (*req));
1071 req->slave_key = *slave_key;
1072 req->announced_at = GNUNET_htonll (announced_at);
1073 req->did_join = GNUNET_NO;
1074 req->op_id = GNUNET_htonll (op_add (chn, result_cb, cls));
1076 GNUNET_CLIENT_MANAGER_transmit (chn->client, &req->header);
1081 * Request to replay a part of the message history of the channel.
1083 * Historic messages (but NOT the state at the time) will be replayed (given to
1084 * the normal method handlers) if available and if access is permitted.
1087 * Which channel should be replayed?
1088 * @param start_message_id
1089 * Earliest interesting point in history.
1090 * @param end_message_id
1091 * Last (inclusive) interesting point in history.
1092 * FIXME: @param method_prefix
1093 * Retrieve only messages with a matching method prefix.
1095 * Function to call when the requested history has been fully replayed.
1097 * Closure for the callbacks.
1099 * @return Handle to cancel history replay operation.
1102 GNUNET_PSYC_channel_history_replay (struct GNUNET_PSYC_Channel *chn,
1103 uint64_t start_message_id,
1104 uint64_t end_message_id,
1105 /* FIXME: const char *method_prefix, */
1106 GNUNET_PSYC_ResultCallback result_cb,
1109 struct HistoryRequest *req = GNUNET_malloc (sizeof (*req));
1110 req->header.type = htons (GNUNET_MESSAGE_TYPE_PSYC_HISTORY_REPLAY);
1111 req->header.size = htons (sizeof (*req));
1112 req->start_message_id = GNUNET_htonll (start_message_id);
1113 req->end_message_id = GNUNET_htonll (end_message_id);
1114 req->op_id = GNUNET_htonll (op_add (chn, result_cb, cls));
1116 GNUNET_CLIENT_MANAGER_transmit (chn->client, &req->header);
1121 * Request to replay the latest messages from the message history of the channel.
1123 * Historic messages (but NOT the state at the time) will be replayed (given to
1124 * the normal method handlers) if available and if access is permitted.
1127 * Which channel should be replayed?
1128 * @param message_limit
1129 * Maximum number of messages to replay.
1130 * FIXME: @param method_prefix
1131 * Retrieve only messages with a matching method prefix.
1133 * Function to call when the requested history has been fully replayed.
1135 * Closure for the callbacks.
1137 * @return Handle to cancel history replay operation.
1140 GNUNET_PSYC_channel_history_replay_latest (struct GNUNET_PSYC_Channel *chn,
1141 uint64_t message_limit,
1142 /* FIXME: const char *method_prefix, */
1143 GNUNET_PSYC_ResultCallback result_cb,
1146 struct HistoryRequest *req = GNUNET_malloc (sizeof (*req));
1147 req->header.type = htons (GNUNET_MESSAGE_TYPE_PSYC_HISTORY_REPLAY);
1148 req->header.size = htons (sizeof (*req));
1149 req->message_limit = GNUNET_htonll (message_limit);
1150 req->op_id = GNUNET_htonll (op_add (chn, result_cb, cls));
1152 GNUNET_CLIENT_MANAGER_transmit (chn->client, &req->header);
1157 * Retrieve the best matching channel state variable.
1159 * If the requested variable name is not present in the state, the nearest
1160 * less-specific name is matched; for example, requesting "_a_b" will match "_a"
1161 * if "_a_b" does not exist.
1166 * Full name of the requested variable.
1167 * The actual variable returned might have a shorter name.
1169 * Function called once when a matching state variable is found.
1170 * Not called if there's no matching state variable.
1172 * Function called after the operation finished.
1173 * (i.e. all state variables have been returned via @a state_cb)
1175 * Closure for the callbacks.
1178 GNUNET_PSYC_channel_state_get (struct GNUNET_PSYC_Channel *chn,
1179 const char *full_name,
1180 GNUNET_PSYC_StateVarCallback var_cb,
1181 GNUNET_PSYC_ResultCallback result_cb,
1184 size_t name_size = strlen (full_name) + 1;
1185 struct StateRequest *req = GNUNET_malloc (sizeof (*req) + name_size);
1186 req->header.type = htons (GNUNET_MESSAGE_TYPE_PSYC_STATE_GET);
1187 req->header.size = htons (sizeof (*req) + name_size);
1188 req->op_id = GNUNET_htonll (op_add (chn, result_cb, cls));
1189 memcpy (&req[1], full_name, name_size);
1191 GNUNET_CLIENT_MANAGER_transmit (chn->client, &req->header);
1196 * Return all channel state variables whose name matches a given prefix.
1198 * A name matches if it starts with the given @a name_prefix, thus requesting
1199 * the empty prefix ("") will match all values; requesting "_a_b" will also
1200 * return values stored under "_a_b_c".
1202 * The @a state_cb is invoked on all matching state variables asynchronously, as
1203 * the state is stored in and retrieved from the PSYCstore,
1207 * @param name_prefix
1208 * Prefix of the state variable name to match.
1210 * Function called once when a matching state variable is found.
1211 * Not called if there's no matching state variable.
1213 * Function called after the operation finished.
1214 * (i.e. all state variables have been returned via @a state_cb)
1216 * Closure for the callbacks.
1219 GNUNET_PSYC_channel_state_get_prefix (struct GNUNET_PSYC_Channel *chn,
1220 const char *name_prefix,
1221 GNUNET_PSYC_StateVarCallback var_cb,
1222 GNUNET_PSYC_ResultCallback result_cb,
1225 size_t name_size = strlen (name_prefix) + 1;
1226 struct StateRequest *req = GNUNET_malloc (sizeof (*req) + name_size);
1227 req->header.type = htons (GNUNET_MESSAGE_TYPE_PSYC_STATE_GET);
1228 req->header.size = htons (sizeof (*req) + name_size);
1229 req->op_id = GNUNET_htonll (op_add (chn, result_cb, cls));
1230 memcpy (&req[1], name_prefix, name_size);
1232 GNUNET_CLIENT_MANAGER_transmit (chn->client, &req->header);
1235 /* end of psyc_api.c */