+ int ret;
+ do
+ {
+ ret = replay_req_remove_cadet (chn);
+ }
+ while (GNUNET_YES == ret);
+
+ GNUNET_free (chn);
+}
+
+
+static int
+check_cadet_join_request (void *cls,
+ const struct MulticastJoinRequestMessage *req)
+{
+ struct Channel *chn = cls;
+
+ if (NULL == chn
+ || JOIN_NOT_ASKED != chn->join_status)
+ {
+ return GNUNET_SYSERR;
+ }
+
+ uint16_t size = ntohs (req->header.size);
+ if (size < sizeof (*req))
+ {
+ GNUNET_break_op (0);
+ return GNUNET_SYSERR;
+ }
+ if (ntohl (req->purpose.size) != (size
+ - sizeof (req->header)
+ - sizeof (req->reserved)
+ - sizeof (req->signature)))
+ {
+ GNUNET_break_op (0);
+ return GNUNET_SYSERR;
+ }
+ if (GNUNET_OK !=
+ GNUNET_CRYPTO_ecdsa_verify (GNUNET_SIGNATURE_PURPOSE_MULTICAST_REQUEST,
+ &req->purpose, &req->signature,
+ &req->member_pub_key))
+ {
+ GNUNET_break_op (0);
+ return GNUNET_SYSERR;
+ }
+
+ return GNUNET_OK;
+}
+
+
+/**
+ * Incoming join request message from CADET.
+ */
+static void
+handle_cadet_join_request (void *cls,
+ const struct MulticastJoinRequestMessage *req)
+{
+ struct Channel *chn = cls;
+ GNUNET_CADET_receive_done (chn->channel);
+
+ struct GNUNET_HashCode group_pub_hash;
+ GNUNET_CRYPTO_hash (&req->group_pub_key, sizeof (req->group_pub_key), &group_pub_hash);
+ chn->group_pub_key = req->group_pub_key;
+ chn->group_pub_hash = group_pub_hash;
+ chn->member_pub_key = req->member_pub_key;
+ chn->peer = req->peer;
+ chn->join_status = JOIN_WAITING;
+
+ client_send_all (&group_pub_hash, &req->header);
+}
+
+
+static int
+check_cadet_join_decision (void *cls,
+ const struct MulticastJoinDecisionMessageHeader *hdcsn)
+{
+ uint16_t size = ntohs (hdcsn->header.size);
+ if (size < sizeof (struct MulticastJoinDecisionMessageHeader) +
+ sizeof (struct MulticastJoinDecisionMessage))
+ {
+ GNUNET_break_op (0);
+ return GNUNET_SYSERR;
+ }
+
+ struct Channel *chn = cls;
+ if (NULL == chn)
+ {
+ GNUNET_break (0);
+ return GNUNET_SYSERR;
+ }
+ if (NULL == chn->group || GNUNET_NO != chn->group->is_origin)
+ {
+ GNUNET_break (0);
+ return GNUNET_SYSERR;
+ }
+ switch (chn->join_status)
+ {
+ case JOIN_REFUSED:
+ return GNUNET_SYSERR;
+
+ case JOIN_ADMITTED:
+ return GNUNET_OK;
+
+ case JOIN_NOT_ASKED:
+ case JOIN_WAITING:
+ break;
+ }
+
+ return GNUNET_OK;
+}
+
+
+/**
+ * Incoming join decision message from CADET.
+ */
+static void
+handle_cadet_join_decision (void *cls,
+ const struct MulticastJoinDecisionMessageHeader *hdcsn)
+{
+ const struct MulticastJoinDecisionMessage *
+ dcsn = (const struct MulticastJoinDecisionMessage *) &hdcsn[1];
+
+ struct Channel *chn = cls;
+ GNUNET_CADET_receive_done (chn->channel);
+
+ // FIXME: do we need to copy chn->peer or compare it with hdcsn->peer?
+ struct Member *mem = (struct Member *) chn->group;
+ client_send_join_decision (mem, hdcsn);
+ if (GNUNET_YES == ntohl (dcsn->is_admitted))
+ {
+ chn->join_status = JOIN_ADMITTED;
+ }
+ else
+ {
+ chn->join_status = JOIN_REFUSED;
+ cadet_channel_destroy (chn);
+ }
+}
+
+
+static int
+check_cadet_message (void *cls,
+ const struct GNUNET_MULTICAST_MessageHeader *msg)
+{
+ uint16_t size = ntohs (msg->header.size);
+ if (size < sizeof (*msg))
+ {
+ GNUNET_break_op (0);
+ return GNUNET_SYSERR;
+ }
+
+ struct Channel *chn = cls;
+ if (NULL == chn)
+ {
+ GNUNET_break (0);
+ return GNUNET_SYSERR;
+ }
+ if (ntohl (msg->purpose.size) != (size
+ - sizeof (msg->header)
+ - sizeof (msg->hop_counter)
+ - sizeof (msg->signature)))
+ {
+ GNUNET_break_op (0);
+ return GNUNET_SYSERR;
+ }
+ if (GNUNET_OK !=
+ GNUNET_CRYPTO_eddsa_verify (GNUNET_SIGNATURE_PURPOSE_MULTICAST_MESSAGE,
+ &msg->purpose, &msg->signature,
+ &chn->group_pub_key))
+ {
+ GNUNET_break_op (0);
+ return GNUNET_SYSERR;
+ }
+
+ return GNUNET_OK;
+}
+
+
+/**
+ * Incoming multicast message from CADET.
+ */
+static void
+handle_cadet_message (void *cls,
+ const struct GNUNET_MULTICAST_MessageHeader *msg)
+{
+ struct Channel *chn = cls;
+ GNUNET_CADET_receive_done (chn->channel);
+ client_send_all (&chn->group_pub_hash, &msg->header);
+}
+
+
+static int
+check_cadet_request (void *cls,
+ const struct GNUNET_MULTICAST_RequestHeader *req)
+{
+ uint16_t size = ntohs (req->header.size);
+ if (size < sizeof (*req))
+ {
+ GNUNET_break_op (0);
+ return GNUNET_SYSERR;
+ }
+
+ struct Channel *chn = cls;
+ if (NULL == chn)
+ {
+ GNUNET_break (0);
+ return GNUNET_SYSERR;
+ }
+ if (ntohl (req->purpose.size) != (size
+ - sizeof (req->header)
+ - sizeof (req->member_pub_key)
+ - sizeof (req->signature)))
+ {
+ GNUNET_break_op (0);
+ return GNUNET_SYSERR;
+ }
+ if (GNUNET_OK !=
+ GNUNET_CRYPTO_ecdsa_verify (GNUNET_SIGNATURE_PURPOSE_MULTICAST_REQUEST,
+ &req->purpose, &req->signature,
+ &req->member_pub_key))
+ {
+ GNUNET_break_op (0);
+ return GNUNET_SYSERR;
+ }
+
+ return GNUNET_OK;
+}
+
+
+/**
+ * Incoming multicast request message from CADET.
+ */
+static void
+handle_cadet_request (void *cls,
+ const struct GNUNET_MULTICAST_RequestHeader *req)
+{
+ struct Channel *chn = cls;
+ GNUNET_CADET_receive_done (chn->channel);
+ client_send_origin (&chn->group_pub_hash, &req->header);
+}
+
+
+static int
+check_cadet_replay_request (void *cls,
+ const struct MulticastReplayRequestMessage *req)
+{
+ uint16_t size = ntohs (req->header.size);
+ if (size < sizeof (*req))
+ {
+ GNUNET_break_op (0);
+ return GNUNET_SYSERR;
+ }
+
+ struct Channel *chn = cls;
+ if (NULL == chn)
+ {
+ GNUNET_break_op (0);
+ return GNUNET_SYSERR;
+ }
+
+ return GNUNET_OK;
+}
+
+
+/**
+ * Incoming multicast replay request from CADET.
+ */
+static void
+handle_cadet_replay_request (void *cls,
+ const struct MulticastReplayRequestMessage *req)
+{
+ struct Channel *chn = cls;
+ GNUNET_CADET_receive_done (chn->channel);
+
+ struct MulticastReplayRequestMessage rep = *req;
+ GNUNET_memcpy (&rep.member_pub_key, &chn->member_pub_key, sizeof (chn->member_pub_key));
+
+ struct GNUNET_CONTAINER_MultiHashMap *
+ grp_replay_req = GNUNET_CONTAINER_multihashmap_get (replay_req_cadet,
+ &chn->group->pub_key_hash);
+ if (NULL == grp_replay_req)
+ {
+ grp_replay_req = GNUNET_CONTAINER_multihashmap_create (1, GNUNET_NO);
+ GNUNET_CONTAINER_multihashmap_put (replay_req_cadet,
+ &chn->group->pub_key_hash, grp_replay_req,
+ GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
+ }
+ struct GNUNET_HashCode key_hash;
+ replay_key_hash (rep.fragment_id, rep.message_id, rep.fragment_offset,
+ rep.flags, &key_hash);
+ GNUNET_CONTAINER_multihashmap_put (grp_replay_req, &key_hash, chn,
+ GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
+
+ client_send_random (&chn->group_pub_hash, &rep.header);
+}
+
+
+static int
+check_cadet_replay_response (void *cls,
+ const struct MulticastReplayResponseMessage *res)
+{
+ struct Channel *chn = cls;
+ if (NULL == chn)
+ {
+ GNUNET_break (0);
+ return GNUNET_SYSERR;
+ }
+ return GNUNET_OK;
+}
+
+
+/**
+ * Incoming multicast replay response from CADET.
+ */
+static void
+handle_cadet_replay_response (void *cls,
+ const struct MulticastReplayResponseMessage *res)
+{
+ struct Channel *chn = cls;
+ GNUNET_CADET_receive_done (chn->channel);
+
+ /* @todo FIXME: got replay error response, send request to other members */
+}
+
+
+static void
+group_set_cadet_port_hash (struct Group *grp)
+{
+ struct CadetPort {
+ struct GNUNET_CRYPTO_EddsaPublicKey pub_key;
+ uint32_t app_type;
+ } port = {
+ grp->pub_key,
+ GNUNET_APPLICATION_TYPE_MULTICAST,
+ };
+ GNUNET_CRYPTO_hash (&port, sizeof (port), &grp->cadet_port_hash);
+}
+
+
+
+/**
+ * Create new outgoing CADET channel.
+ *
+ * @param peer
+ * Peer to connect to.
+ * @param group_pub_key
+ * Public key of group the channel belongs to.
+ * @param group_pub_hash
+ * Hash of @a group_pub_key.
+ *
+ * @return Channel.
+ */
+static struct Channel *
+cadet_channel_create (struct Group *grp, struct GNUNET_PeerIdentity *peer)
+{
+ struct Channel *chn = GNUNET_malloc (sizeof (*chn));
+ chn->group = grp;
+ chn->group_pub_key = grp->pub_key;
+ chn->group_pub_hash = grp->pub_key_hash;
+ chn->peer = *peer;
+ chn->direction = DIR_OUTGOING;
+ chn->is_connected = GNUNET_NO;
+ chn->join_status = JOIN_WAITING;
+
+ struct GNUNET_MQ_MessageHandler cadet_handlers[] = {
+ GNUNET_MQ_hd_var_size (cadet_message,
+ GNUNET_MESSAGE_TYPE_MULTICAST_MESSAGE,
+ struct GNUNET_MULTICAST_MessageHeader,
+ chn),