2 This file is part of GNUnet.
3 Copyright (C) 2011, 2017 GNUnet e.V.
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., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
21 * @file cadet/cadet_api.c
22 * @brief cadet api: client implementation of cadet service
23 * @author Bartlomiej Polot
24 * @author Christian Grothoff
27 #include "gnunet_util_lib.h"
28 #include "gnunet_constants.h"
29 #include "gnunet_cadet_service.h"
31 #include "cadet_protocol.h"
33 #define LOG(kind,...) GNUNET_log_from (kind, "cadet-api",__VA_ARGS__)
44 GNUNET_CADET_ChannelCB channel_cb;
49 GNUNET_CADET_PeersCB peers_cb;
54 GNUNET_CADET_PeerCB peer_cb;
59 GNUNET_CADET_TunnelsCB tunnels_cb;
64 GNUNET_CADET_TunnelCB tunnel_cb;
69 * Opaque handle to the service.
71 struct GNUNET_CADET_Handle
76 struct GNUNET_MQ_Handle *mq;
81 struct GNUNET_CONTAINER_MultiHashMap *ports;
86 struct GNUNET_CONTAINER_MultiHashMap32 *channels;
89 * child of the next channel to create (to avoid reusing IDs often)
91 struct GNUNET_CADET_ClientChannelNumber next_ccn;
94 * Configuration given by the client, in case of reconnection
96 const struct GNUNET_CONFIGURATION_Handle *cfg;
99 * Task for trying to reconnect.
101 struct GNUNET_SCHEDULER_Task *reconnect_task;
104 * Callback for an info task (only one active at a time).
106 union CadetInfoCB info_cb;
109 * Info callback closure for @c info_cb.
114 * Time to the next reconnect in case one reconnect fails
116 struct GNUNET_TIME_Relative reconnect_time;
122 * Opaque handle to a channel.
124 struct GNUNET_CADET_Channel
128 * Other end of the channel.
130 struct GNUNET_PeerIdentity peer;
133 * Handle to the cadet this channel belongs to
135 struct GNUNET_CADET_Handle *cadet;
138 * Channel's port, if incoming.
140 struct GNUNET_CADET_Port *incoming_port;
143 * Any data the caller wants to put in here, used for the
144 * various callbacks (@e disconnects, @e window_changes, handlers).
149 * Message Queue for the channel (which we are implementing).
151 struct GNUNET_MQ_Handle *mq;
154 * Task to allow mq to send more traffic.
156 struct GNUNET_SCHEDULER_Task *mq_cont;
159 * Pending envelope with a message to be transmitted to the
160 * service as soon as we are allowed to. Should only be
161 * non-NULL if @e allow_send is 0.
163 struct GNUNET_MQ_Envelope *pending_env;
166 * Window change handler.
168 GNUNET_CADET_WindowSizeEventHandler window_changes;
171 * Disconnect handler.
173 GNUNET_CADET_DisconnectEventHandler disconnects;
176 * Local ID of the channel, #GNUNET_CADET_LOCAL_CHANNEL_ID_CLI bit is set if outbound.
178 struct GNUNET_CADET_ClientChannelNumber ccn;
181 * Channel options: reliability, etc.
183 enum GNUNET_CADET_ChannelOption options;
186 * How many messages are we allowed to send to the service right now?
188 unsigned int allow_send;
194 * Opaque handle to a port.
196 struct GNUNET_CADET_Port
202 struct GNUNET_HashCode id;
205 * Handle to the CADET session this port belongs to.
207 struct GNUNET_CADET_Handle *cadet;
210 * Closure for @a handler.
215 * Handler for incoming channels on this port
217 GNUNET_CADET_ConnectEventHandler connects;
220 * Closure for @ref connects
225 * Window size change handler.
227 GNUNET_CADET_WindowSizeEventHandler window_changes;
230 * Handler called when an incoming channel is destroyed.
232 GNUNET_CADET_DisconnectEventHandler disconnects;
235 * Payload handlers for incoming channels.
237 struct GNUNET_MQ_MessageHandler *handlers;
242 * Find the Port struct for a hash.
244 * @param h CADET handle.
245 * @param hash HashCode for the port number.
246 * @return The port handle if known, NULL otherwise.
248 static struct GNUNET_CADET_Port *
249 find_port (const struct GNUNET_CADET_Handle *h,
250 const struct GNUNET_HashCode *hash)
252 return GNUNET_CONTAINER_multihashmap_get (h->ports,
258 * Get the channel handler for the channel specified by id from the given handle
260 * @param h Cadet handle
261 * @param ccn ID of the wanted channel
262 * @return handle to the required channel or NULL if not found
264 static struct GNUNET_CADET_Channel *
265 find_channel (struct GNUNET_CADET_Handle *h,
266 struct GNUNET_CADET_ClientChannelNumber ccn)
268 return GNUNET_CONTAINER_multihashmap32_get (h->channels,
269 ntohl (ccn.channel_of_client));
274 * Create a new channel and insert it in the channel list of the cadet handle
276 * @param h Cadet handle
277 * @param ccnp pointer to desired ccn of the channel, NULL to assign one automatically.
278 * @return Handle to the created channel.
280 static struct GNUNET_CADET_Channel *
281 create_channel (struct GNUNET_CADET_Handle *h,
282 const struct GNUNET_CADET_ClientChannelNumber *ccnp)
284 struct GNUNET_CADET_Channel *ch;
285 struct GNUNET_CADET_ClientChannelNumber ccn;
287 ch = GNUNET_new (struct GNUNET_CADET_Channel);
294 h->next_ccn.channel_of_client
295 = htonl (GNUNET_CADET_LOCAL_CHANNEL_ID_CLI | (1 + ntohl (h->next_ccn.channel_of_client)));
303 GNUNET_assert (GNUNET_OK ==
304 GNUNET_CONTAINER_multihashmap32_put (h->channels,
305 ntohl (ch->ccn.channel_of_client),
307 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
313 * Destroy the specified channel.
314 * - Destroys all peers, calling the disconnect callback on each if needed
315 * - Cancels all outgoing traffic for that channel, calling respective notifys
316 * - Calls cleaner if channel was inbound
317 * - Frees all memory used
319 * @param ch Pointer to the channel.
320 * @param call_cleaner Whether to call the cleaner handler.
323 destroy_channel (struct GNUNET_CADET_Channel *ch)
325 struct GNUNET_CADET_Handle *h = ch->cadet;
327 LOG (GNUNET_ERROR_TYPE_DEBUG,
328 "Destroying channel %X of %p\n",
331 GNUNET_assert (GNUNET_YES ==
332 GNUNET_CONTAINER_multihashmap32_remove (h->channels,
333 ntohl (ch->ccn.channel_of_client),
335 if (NULL != ch->mq_cont)
337 GNUNET_SCHEDULER_cancel (ch->mq_cont);
340 /* signal channel destruction */
341 if (NULL != ch->disconnects)
342 ch->disconnects (ch->ctx,
344 if (NULL != ch->pending_env)
345 GNUNET_MQ_discard (ch->pending_env);
346 GNUNET_MQ_destroy (ch->mq);
352 * Reconnect to the service, retransmit all infomation to try to restore the
355 * @param h handle to the cadet
358 reconnect (struct GNUNET_CADET_Handle *h);
362 * Reconnect callback: tries to reconnect again after a failer previous
365 * @param cls closure (cadet handle)
368 reconnect_cbk (void *cls)
370 struct GNUNET_CADET_Handle *h = cls;
372 h->reconnect_task = NULL;
378 * Function called during #reconnect() to destroy
379 * all channels that are still open.
381 * @param cls the `struct GNUNET_CADET_Handle`
382 * @param cid chanenl ID
383 * @param value a `struct GNUNET_CADET_Channel` to destroy
384 * @return #GNUNET_OK (continue to iterate)
387 destroy_channel_on_reconnect_cb (void *cls,
391 /* struct GNUNET_CADET_Handle *handle = cls; */
392 struct GNUNET_CADET_Channel *ch = value;
394 destroy_channel (ch);
400 * Reconnect to the service, retransmit all infomation to try to restore the
403 * @param h handle to the cadet
405 * @return #GNUNET_YES in case of sucess, #GNUNET_NO otherwise (service down...)
408 schedule_reconnect (struct GNUNET_CADET_Handle *h)
410 if (NULL != h->reconnect_task)
412 GNUNET_CONTAINER_multihashmap32_iterate (h->channels,
413 &destroy_channel_on_reconnect_cb,
416 = GNUNET_SCHEDULER_add_delayed (h->reconnect_time,
420 = GNUNET_TIME_STD_BACKOFF (h->reconnect_time);
425 * Notify the application about a change in the window size (if needed).
427 * @param ch Channel to notify about.
430 notify_window_size (struct GNUNET_CADET_Channel *ch)
432 if (NULL != ch->window_changes)
433 ch->window_changes (ch->ctx,
434 ch, /* FIXME: remove 'ch'? */
440 * Transmit the next message from our queue.
442 * @param cls Closure (channel whose mq to activate).
445 cadet_mq_send_now (void *cls)
447 struct GNUNET_CADET_Channel *ch = cls;
448 struct GNUNET_MQ_Envelope *env = ch->pending_env;
451 if (0 == ch->allow_send)
453 /* how did we get here? */
459 /* how did we get here? */
464 ch->pending_env = NULL;
465 GNUNET_MQ_send (ch->cadet->mq,
467 GNUNET_MQ_impl_send_continue (ch->mq);
472 * Implement sending functionality of a message queue for
473 * us sending messages to a peer.
475 * Encapsulates the payload message in a #GNUNET_CADET_LocalData message
476 * in order to label the message with the channel ID and send the
477 * encapsulated message to the service.
479 * @param mq the message queue
480 * @param msg the message to send
481 * @param impl_state state of the implementation
484 cadet_mq_send_impl (struct GNUNET_MQ_Handle *mq,
485 const struct GNUNET_MessageHeader *msg,
488 struct GNUNET_CADET_Channel *ch = impl_state;
489 struct GNUNET_CADET_Handle *h = ch->cadet;
491 struct GNUNET_MQ_Envelope *env;
492 struct GNUNET_CADET_LocalData *cadet_msg;
496 /* We're currently reconnecting, pretend this worked */
497 GNUNET_MQ_impl_send_continue (mq);
501 /* check message size for sanity */
502 msize = ntohs (msg->size);
503 if (msize > GNUNET_CONSTANTS_MAX_CADET_MESSAGE_SIZE)
506 GNUNET_MQ_impl_send_continue (mq);
509 env = GNUNET_MQ_msg_nested_mh (cadet_msg,
510 GNUNET_MESSAGE_TYPE_CADET_LOCAL_DATA,
512 cadet_msg->ccn = ch->ccn;
513 GNUNET_assert (NULL == ch->pending_env);
514 ch->pending_env = env;
515 if (0 < ch->allow_send)
517 = GNUNET_SCHEDULER_add_now (&cadet_mq_send_now,
523 * Handle destruction of a message queue. Implementations must not
524 * free @a mq, but should take care of @a impl_state.
526 * @param mq the message queue to destroy
527 * @param impl_state state of the implementation
530 cadet_mq_destroy_impl (struct GNUNET_MQ_Handle *mq,
533 struct GNUNET_CADET_Channel *ch = impl_state;
535 GNUNET_assert (mq == ch->mq);
541 * We had an error processing a message we forwarded from a peer to
542 * the CADET service. We should just complain about it but otherwise
543 * continue processing.
545 * @param cls closure with our `struct GNUNET_CADET_Channel`
546 * @param error error code
549 cadet_mq_error_handler (void *cls,
550 enum GNUNET_MQ_Error error)
552 struct GNUNET_CADET_Channel *ch = cls;
555 if (GNUNET_MQ_ERROR_NO_MATCH == error)
557 /* Got a message we did not understand, still try to continue! */
558 GNUNET_CADET_receive_done (ch);
562 schedule_reconnect (ch->cadet);
568 * Implementation function that cancels the currently sent message.
569 * Should basically undo whatever #mq_send_impl() did.
571 * @param mq message queue
572 * @param impl_state state specific to the implementation
575 cadet_mq_cancel_impl (struct GNUNET_MQ_Handle *mq,
578 struct GNUNET_CADET_Channel *ch = impl_state;
580 GNUNET_assert (NULL != ch->pending_env);
581 GNUNET_MQ_discard (ch->pending_env);
582 ch->pending_env = NULL;
583 if (NULL != ch->mq_cont)
585 GNUNET_SCHEDULER_cancel (ch->mq_cont);
592 * Process the new channel notification and add it to the channels in the handle
594 * @param h The cadet handle
595 * @param msg A message with the details of the new incoming channel
598 handle_channel_created (void *cls,
599 const struct GNUNET_CADET_LocalChannelCreateMessage *msg)
601 struct GNUNET_CADET_Handle *h = cls;
602 struct GNUNET_CADET_Channel *ch;
603 struct GNUNET_CADET_Port *port;
604 const struct GNUNET_HashCode *port_number;
605 struct GNUNET_CADET_ClientChannelNumber ccn;
608 port_number = &msg->port;
609 if (ntohl (ccn.channel_of_client) >= GNUNET_CADET_LOCAL_CHANNEL_ID_CLI)
618 /* We could have closed the port but the service didn't know about it yet
619 * This is not an error.
621 struct GNUNET_CADET_LocalChannelDestroyMessage *d_msg;
622 struct GNUNET_MQ_Envelope *env;
624 LOG (GNUNET_ERROR_TYPE_DEBUG,
625 "No handler for incoming channel %X (on port %s, recently closed?)\n",
626 ntohl (ccn.channel_of_client),
627 GNUNET_h2s (port_number));
628 env = GNUNET_MQ_msg (d_msg,
629 GNUNET_MESSAGE_TYPE_CADET_LOCAL_CHANNEL_DESTROY);
630 d_msg->ccn = msg->ccn;
631 GNUNET_MQ_send (h->mq,
636 ch = create_channel (h,
638 ch->peer = msg->peer;
640 ch->incoming_port = port;
641 ch->options = ntohl (msg->opt);
642 LOG (GNUNET_ERROR_TYPE_DEBUG,
643 "Creating incoming channel %X [%s] %p\n",
644 ntohl (ccn.channel_of_client),
645 GNUNET_h2s (port_number),
648 GNUNET_assert (NULL != port->connects);
649 ch->window_changes = port->window_changes;
650 ch->disconnects = port->disconnects;
651 ch->mq = GNUNET_MQ_queue_for_callbacks (&cadet_mq_send_impl,
652 &cadet_mq_destroy_impl,
653 &cadet_mq_cancel_impl,
656 &cadet_mq_error_handler,
658 ch->ctx = port->connects (port->cls,
661 GNUNET_MQ_set_handlers_closure (ch->mq,
667 * Process the channel destroy notification and free associated resources
669 * @param h The cadet handle
670 * @param msg A message with the details of the channel being destroyed
673 handle_channel_destroy (void *cls,
674 const struct GNUNET_CADET_LocalChannelDestroyMessage *msg)
676 struct GNUNET_CADET_Handle *h = cls;
677 struct GNUNET_CADET_Channel *ch;
679 ch = find_channel (h,
683 LOG (GNUNET_ERROR_TYPE_DEBUG,
684 "Received channel destroy for unknown channel %X from CADET service (recently close?)\n",
685 ntohl (msg->ccn.channel_of_client));
688 LOG (GNUNET_ERROR_TYPE_DEBUG,
689 "Received channel destroy for channel %X from CADET service\n",
690 ntohl (msg->ccn.channel_of_client));
691 destroy_channel (ch);
696 * Check that message received from CADET service is well-formed.
698 * @param cls the `struct GNUNET_CADET_Handle`
699 * @param message the message we got
700 * @return #GNUNET_OK if the message is well-formed,
701 * #GNUNET_SYSERR otherwise
704 check_local_data (void *cls,
705 const struct GNUNET_CADET_LocalData *message)
709 size = ntohs (message->header.size);
710 if (sizeof (*message) + sizeof (struct GNUNET_MessageHeader) > size)
713 return GNUNET_SYSERR;
720 * Process the incoming data packets, call appropriate handlers.
722 * @param h The cadet handle
723 * @param message A message encapsulating the data
726 handle_local_data (void *cls,
727 const struct GNUNET_CADET_LocalData *message)
729 struct GNUNET_CADET_Handle *h = cls;
730 const struct GNUNET_MessageHeader *payload;
731 struct GNUNET_CADET_Channel *ch;
735 ch = find_channel (h,
739 LOG (GNUNET_ERROR_TYPE_DEBUG,
740 "Unknown channel %X for incoming data (recently closed?)\n",
741 ntohl (message->ccn.channel_of_client));
745 payload = (const struct GNUNET_MessageHeader *) &message[1];
746 type = ntohs (payload->type);
747 fwd = ntohl (ch->ccn.channel_of_client) <= GNUNET_CADET_LOCAL_CHANNEL_ID_CLI;
748 LOG (GNUNET_ERROR_TYPE_DEBUG,
749 "Got a %s data on channel %s [%X] of type %u\n",
751 GNUNET_i2s (&ch->peer),
752 ntohl (message->ccn.channel_of_client),
754 GNUNET_MQ_inject_message (ch->mq,
760 * Process a local ACK message, enabling the client to send
761 * more data to the service.
763 * @param h Cadet handle.
764 * @param message Message itself.
767 handle_local_ack (void *cls,
768 const struct GNUNET_CADET_LocalAck *message)
770 struct GNUNET_CADET_Handle *h = cls;
771 struct GNUNET_CADET_Channel *ch;
773 ch = find_channel (h,
777 LOG (GNUNET_ERROR_TYPE_DEBUG,
778 "ACK on unknown channel %X\n",
779 ntohl (message->ccn.channel_of_client));
783 if (NULL == ch->pending_env)
785 LOG (GNUNET_ERROR_TYPE_DEBUG,
786 "Got an ACK on mq channel %X, allow send now %u!\n",
787 ntohl (ch->ccn.channel_of_client),
789 notify_window_size (ch);
792 if (NULL != ch->mq_cont)
793 return; /* already working on it! */
794 LOG (GNUNET_ERROR_TYPE_DEBUG,
795 "Got an ACK on mq channel %X, sending pending message!\n",
796 ntohl (ch->ccn.channel_of_client));
798 = GNUNET_SCHEDULER_add_now (&cadet_mq_send_now,
804 * Generic error handler, called with the appropriate error code and
805 * the same closure specified at the creation of the message queue.
806 * Not every message queue implementation supports an error handler.
808 * @param cls closure, a `struct GNUNET_CORE_Handle *`
809 * @param error error code
812 handle_mq_error (void *cls,
813 enum GNUNET_MQ_Error error)
815 struct GNUNET_CADET_Handle *h = cls;
817 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
820 GNUNET_MQ_destroy (h->mq);
827 * Process a local reply about info on all tunnels, pass info to the user.
829 * @param cls Closure (Cadet handle).
830 * @param msg Message itself.
833 handle_get_peers (void *cls,
834 const struct GNUNET_CADET_LocalInfoPeer *msg)
836 struct GNUNET_CADET_Handle *h = cls;
838 if (NULL == h->info_cb.peers_cb)
840 h->info_cb.peers_cb (h->info_cls,
842 (int) ntohs (msg->tunnel),
843 (unsigned int) ntohs (msg->paths),
849 * Check that message received from CADET service is well-formed.
851 * @param cls the `struct GNUNET_CADET_Handle`
852 * @param message the message we got
853 * @return #GNUNET_OK if the message is well-formed,
854 * #GNUNET_SYSERR otherwise
857 check_get_peer (void *cls,
858 const struct GNUNET_CADET_LocalInfoPeer *message)
860 size_t msize = sizeof (struct GNUNET_CADET_LocalInfoPeer);
861 const struct GNUNET_PeerIdentity *paths_array;
867 esize = ntohs (message->header.size);
871 return GNUNET_SYSERR;
873 if (0 != ((esize - msize) % sizeof (struct GNUNET_PeerIdentity)))
876 return GNUNET_SYSERR;
878 peers = (esize - msize) / sizeof (struct GNUNET_PeerIdentity);
879 epaths = ntohs (message->paths);
880 paths_array = (const struct GNUNET_PeerIdentity *) &message[1];
882 for (unsigned int i = 0; i < peers; i++)
883 if (0 == memcmp (&paths_array[i],
884 &message->destination,
885 sizeof (struct GNUNET_PeerIdentity)))
890 return GNUNET_SYSERR;
897 * Process a local peer info reply, pass info to the user.
899 * @param cls Closure (Cadet handle).
900 * @param message Message itself.
903 handle_get_peer (void *cls,
904 const struct GNUNET_CADET_LocalInfoPeer *message)
906 struct GNUNET_CADET_Handle *h = cls;
907 const struct GNUNET_PeerIdentity *paths_array;
909 unsigned int path_length;
913 if (NULL == h->info_cb.peer_cb)
915 paths = ntohs (message->paths);
916 paths_array = (const struct GNUNET_PeerIdentity *) &message[1];
917 peers = (ntohs (message->header.size) - sizeof (*message))
918 / sizeof (struct GNUNET_PeerIdentity);
920 neighbor = GNUNET_NO;
922 for (unsigned int i = 0; i < peers; i++)
924 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
926 GNUNET_i2s (&paths_array[i]));
928 if (0 == memcmp (&paths_array[i], &message->destination,
929 sizeof (struct GNUNET_PeerIdentity)))
931 if (1 == path_length)
932 neighbor = GNUNET_YES;
937 /* Call Callback with tunnel info. */
938 paths_array = (const struct GNUNET_PeerIdentity *) &message[1];
939 h->info_cb.peer_cb (h->info_cls,
940 &message->destination,
941 (int) ntohs (message->tunnel),
949 * Process a local reply about info on all tunnels, pass info to the user.
951 * @param cls Closure (Cadet handle).
952 * @param message Message itself.
955 handle_get_tunnels (void *cls,
956 const struct GNUNET_CADET_LocalInfoTunnel *msg)
958 struct GNUNET_CADET_Handle *h = cls;
960 if (NULL == h->info_cb.tunnels_cb)
962 h->info_cb.tunnels_cb (h->info_cls,
964 ntohl (msg->channels),
965 ntohl (msg->connections),
967 ntohs (msg->cstate));
973 * Check that message received from CADET service is well-formed.
975 * @param cls the `struct GNUNET_CADET_Handle`
976 * @param msg the message we got
977 * @return #GNUNET_OK if the message is well-formed,
978 * #GNUNET_SYSERR otherwise
981 check_get_tunnel (void *cls,
982 const struct GNUNET_CADET_LocalInfoTunnel *msg)
989 /* Verify message sanity */
990 msize = ntohs (msg->header.size);
991 esize = sizeof (struct GNUNET_CADET_LocalInfoTunnel);
995 return GNUNET_SYSERR;
997 ch_n = ntohl (msg->channels);
998 c_n = ntohl (msg->connections);
999 esize += ch_n * sizeof (struct GNUNET_CADET_ChannelTunnelNumber);
1000 esize += c_n * sizeof (struct GNUNET_CADET_ConnectionTunnelIdentifier);
1003 GNUNET_break_op (0);
1004 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1005 "m:%u, e: %u (%u ch, %u conn)\n",
1006 (unsigned int) msize,
1007 (unsigned int) esize,
1010 return GNUNET_SYSERR;
1017 * Process a local tunnel info reply, pass info to the user.
1019 * @param cls Closure (Cadet handle).
1020 * @param msg Message itself.
1023 handle_get_tunnel (void *cls,
1024 const struct GNUNET_CADET_LocalInfoTunnel *msg)
1026 struct GNUNET_CADET_Handle *h = cls;
1029 const struct GNUNET_CADET_ConnectionTunnelIdentifier *conns;
1030 const struct GNUNET_CADET_ChannelTunnelNumber *chns;
1032 if (NULL == h->info_cb.tunnel_cb)
1035 ch_n = ntohl (msg->channels);
1036 c_n = ntohl (msg->connections);
1038 /* Call Callback with tunnel info. */
1039 conns = (const struct GNUNET_CADET_ConnectionTunnelIdentifier *) &msg[1];
1040 chns = (const struct GNUNET_CADET_ChannelTunnelNumber *) &conns[c_n];
1041 h->info_cb.tunnel_cb (h->info_cls,
1047 ntohs (msg->estate),
1048 ntohs (msg->cstate));
1053 * Reconnect to the service, retransmit all infomation to try to restore the
1056 * @param h handle to the cadet
1059 reconnect (struct GNUNET_CADET_Handle *h)
1061 struct GNUNET_MQ_MessageHandler handlers[] = {
1062 GNUNET_MQ_hd_fixed_size (channel_created,
1063 GNUNET_MESSAGE_TYPE_CADET_LOCAL_CHANNEL_CREATE,
1064 struct GNUNET_CADET_LocalChannelCreateMessage,
1066 GNUNET_MQ_hd_fixed_size (channel_destroy,
1067 GNUNET_MESSAGE_TYPE_CADET_LOCAL_CHANNEL_DESTROY,
1068 struct GNUNET_CADET_LocalChannelDestroyMessage,
1070 GNUNET_MQ_hd_var_size (local_data,
1071 GNUNET_MESSAGE_TYPE_CADET_LOCAL_DATA,
1072 struct GNUNET_CADET_LocalData,
1074 GNUNET_MQ_hd_fixed_size (local_ack,
1075 GNUNET_MESSAGE_TYPE_CADET_LOCAL_ACK,
1076 struct GNUNET_CADET_LocalAck,
1078 GNUNET_MQ_hd_fixed_size (get_peers,
1079 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEERS,
1080 struct GNUNET_CADET_LocalInfoPeer,
1082 GNUNET_MQ_hd_var_size (get_peer,
1083 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEER,
1084 struct GNUNET_CADET_LocalInfoPeer,
1086 GNUNET_MQ_hd_fixed_size (get_tunnels,
1087 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNELS,
1088 struct GNUNET_CADET_LocalInfoTunnel,
1090 GNUNET_MQ_hd_var_size (get_tunnel,
1091 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNEL,
1092 struct GNUNET_CADET_LocalInfoTunnel,
1094 GNUNET_MQ_handler_end ()
1097 h->mq = GNUNET_CLIENT_connect (h->cfg,
1104 schedule_reconnect (h);
1107 h->reconnect_time = GNUNET_TIME_UNIT_MILLISECONDS;
1112 * Function called during #GNUNET_CADET_disconnect() to destroy
1113 * all channels that are still open.
1115 * @param cls the `struct GNUNET_CADET_Handle`
1116 * @param cid chanenl ID
1117 * @param value a `struct GNUNET_CADET_Channel` to destroy
1118 * @return #GNUNET_OK (continue to iterate)
1121 destroy_channel_cb (void *cls,
1125 /* struct GNUNET_CADET_Handle *handle = cls; */
1126 struct GNUNET_CADET_Channel *ch = value;
1128 if (ntohl (ch->ccn.channel_of_client) >= GNUNET_CADET_LOCAL_CHANNEL_ID_CLI)
1131 LOG (GNUNET_ERROR_TYPE_DEBUG,
1132 "channel %X not destroyed\n",
1133 ntohl (ch->ccn.channel_of_client));
1135 destroy_channel (ch);
1141 * Function called during #GNUNET_CADET_disconnect() to destroy
1142 * all ports that are still open.
1144 * @param cls the `struct GNUNET_CADET_Handle`
1146 * @param value a `struct GNUNET_CADET_Channel` to destroy
1147 * @return #GNUNET_OK (continue to iterate)
1150 destroy_port_cb (void *cls,
1151 const struct GNUNET_HashCode *id,
1154 /* struct GNUNET_CADET_Handle *handle = cls; */
1155 struct GNUNET_CADET_Port *port = value;
1157 /* This is a warning, the app should have cleanly closed all open ports */
1159 GNUNET_CADET_close_port (port);
1165 * Disconnect from the cadet service. All channels will be destroyed. All channel
1166 * disconnect callbacks will be called on any still connected peers, notifying
1167 * about their disconnection. The registered inbound channel cleaner will be
1168 * called should any inbound channels still exist.
1170 * @param handle connection to cadet to disconnect
1173 GNUNET_CADET_disconnect (struct GNUNET_CADET_Handle *handle)
1175 GNUNET_CONTAINER_multihashmap_iterate (handle->ports,
1178 GNUNET_CONTAINER_multihashmap_destroy (handle->ports);
1179 handle->ports = NULL;
1180 GNUNET_CONTAINER_multihashmap32_iterate (handle->channels,
1181 &destroy_channel_cb,
1183 GNUNET_CONTAINER_multihashmap32_destroy (handle->channels);
1184 handle->channels = NULL;
1185 if (NULL != handle->mq)
1187 GNUNET_MQ_destroy (handle->mq);
1190 if (NULL != handle->reconnect_task)
1192 GNUNET_SCHEDULER_cancel (handle->reconnect_task);
1193 handle->reconnect_task = NULL;
1195 GNUNET_free (handle);
1200 * Close a port opened with @a GNUNET_CADET_open_port().
1201 * The @a new_channel callback will no longer be called.
1203 * @param p Port handle.
1206 GNUNET_CADET_close_port (struct GNUNET_CADET_Port *p)
1208 struct GNUNET_CADET_PortMessage *msg;
1209 struct GNUNET_MQ_Envelope *env;
1211 env = GNUNET_MQ_msg (msg,
1212 GNUNET_MESSAGE_TYPE_CADET_LOCAL_PORT_CLOSE);
1214 GNUNET_MQ_send (p->cadet->mq,
1216 GNUNET_assert (GNUNET_YES ==
1217 GNUNET_CONTAINER_multihashmap_remove (p->cadet->ports,
1220 GNUNET_free_non_null (p->handlers);
1226 * Destroy an existing channel.
1228 * The existing end callback for the channel will be called immediately.
1229 * Any pending outgoing messages will be sent but no incoming messages will be
1230 * accepted and no data callbacks will be called.
1232 * @param channel Channel handle, becomes invalid after this call.
1235 GNUNET_CADET_channel_destroy (struct GNUNET_CADET_Channel *channel)
1237 struct GNUNET_CADET_Handle *h = channel->cadet;
1238 struct GNUNET_CADET_LocalChannelDestroyMessage *msg;
1239 struct GNUNET_MQ_Envelope *env;
1243 env = GNUNET_MQ_msg (msg,
1244 GNUNET_MESSAGE_TYPE_CADET_LOCAL_CHANNEL_DESTROY);
1245 msg->ccn = channel->ccn;
1246 GNUNET_MQ_send (h->mq,
1249 destroy_channel (channel);
1254 * Get information about a channel.
1256 * @param channel Channel handle.
1257 * @param option Query (GNUNET_CADET_OPTION_*).
1258 * @param ... dependant on option, currently not used
1260 * @return Union with an answer to the query.
1262 const union GNUNET_CADET_ChannelInfo *
1263 GNUNET_CADET_channel_get_info (struct GNUNET_CADET_Channel *channel,
1264 enum GNUNET_CADET_ChannelOption option,
1267 static int bool_flag;
1271 case GNUNET_CADET_OPTION_NOBUFFER:
1272 case GNUNET_CADET_OPTION_RELIABLE:
1273 case GNUNET_CADET_OPTION_OUT_OF_ORDER:
1274 if (0 != (option & channel->options))
1275 bool_flag = GNUNET_YES;
1277 bool_flag = GNUNET_NO;
1278 return (const union GNUNET_CADET_ChannelInfo *) &bool_flag;
1280 case GNUNET_CADET_OPTION_PEER:
1281 return (const union GNUNET_CADET_ChannelInfo *) &channel->peer;
1291 * Send an ack on the channel to confirm the processing of a message.
1293 * @param ch Channel on which to send the ACK.
1296 GNUNET_CADET_receive_done (struct GNUNET_CADET_Channel *channel)
1298 struct GNUNET_CADET_LocalAck *msg;
1299 struct GNUNET_MQ_Envelope *env;
1301 env = GNUNET_MQ_msg (msg,
1302 GNUNET_MESSAGE_TYPE_CADET_LOCAL_ACK);
1303 LOG (GNUNET_ERROR_TYPE_DEBUG,
1304 "Sending ACK on channel %X\n",
1305 ntohl (channel->ccn.channel_of_client));
1306 msg->ccn = channel->ccn;
1307 GNUNET_MQ_send (channel->cadet->mq,
1313 * Send message of @a type to CADET service of @a h
1315 * @param h handle to CADET service
1316 * @param type message type of trivial information request to send
1319 send_info_request (struct GNUNET_CADET_Handle *h,
1322 struct GNUNET_MessageHeader *msg;
1323 struct GNUNET_MQ_Envelope *env;
1325 env = GNUNET_MQ_msg (msg,
1327 GNUNET_MQ_send (h->mq,
1333 * Request a debug dump on the service's STDERR.
1335 * WARNING: unstable API, likely to change in the future!
1337 * @param h cadet handle
1340 GNUNET_CADET_request_dump (struct GNUNET_CADET_Handle *h)
1342 send_info_request (h,
1343 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_DUMP);
1348 * Request information about peers known to the running cadet service.
1349 * The callback will be called for every peer known to the service.
1350 * Only one info request (of any kind) can be active at once.
1352 * WARNING: unstable API, likely to change in the future!
1354 * @param h Handle to the cadet peer.
1355 * @param callback Function to call with the requested data.
1356 * @param callback_cls Closure for @c callback.
1357 * @return #GNUNET_OK / #GNUNET_SYSERR
1360 GNUNET_CADET_get_peers (struct GNUNET_CADET_Handle *h,
1361 GNUNET_CADET_PeersCB callback,
1364 if (NULL != h->info_cb.peers_cb)
1367 return GNUNET_SYSERR;
1369 send_info_request (h,
1370 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEERS);
1371 h->info_cb.peers_cb = callback;
1372 h->info_cls = callback_cls;
1378 * Cancel a peer info request. The callback will not be called (anymore).
1380 * WARNING: unstable API, likely to change in the future!
1382 * @param h Cadet handle.
1383 * @return Closure given to GNUNET_CADET_get_peers().
1386 GNUNET_CADET_get_peers_cancel (struct GNUNET_CADET_Handle *h)
1388 void *cls = h->info_cls;
1390 h->info_cb.peers_cb = NULL;
1397 * Request information about a peer known to the running cadet peer.
1398 * The callback will be called for the tunnel once.
1399 * Only one info request (of any kind) can be active at once.
1401 * WARNING: unstable API, likely to change in the future!
1403 * @param h Handle to the cadet peer.
1404 * @param id Peer whose tunnel to examine.
1405 * @param callback Function to call with the requested data.
1406 * @param callback_cls Closure for @c callback.
1407 * @return #GNUNET_OK / #GNUNET_SYSERR
1410 GNUNET_CADET_get_peer (struct GNUNET_CADET_Handle *h,
1411 const struct GNUNET_PeerIdentity *id,
1412 GNUNET_CADET_PeerCB callback,
1415 struct GNUNET_CADET_LocalInfo *msg;
1416 struct GNUNET_MQ_Envelope *env;
1418 if (NULL != h->info_cb.peer_cb)
1421 return GNUNET_SYSERR;
1423 env = GNUNET_MQ_msg (msg,
1424 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEER);
1426 GNUNET_MQ_send (h->mq,
1428 h->info_cb.peer_cb = callback;
1429 h->info_cls = callback_cls;
1435 * Request information about tunnels of the running cadet peer.
1436 * The callback will be called for every tunnel of the service.
1437 * Only one info request (of any kind) can be active at once.
1439 * WARNING: unstable API, likely to change in the future!
1441 * @param h Handle to the cadet peer.
1442 * @param callback Function to call with the requested data.
1443 * @param callback_cls Closure for @c callback.
1444 * @return #GNUNET_OK / #GNUNET_SYSERR
1447 GNUNET_CADET_get_tunnels (struct GNUNET_CADET_Handle *h,
1448 GNUNET_CADET_TunnelsCB callback,
1451 if (NULL != h->info_cb.tunnels_cb)
1454 return GNUNET_SYSERR;
1456 send_info_request (h,
1457 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNELS);
1458 h->info_cb.tunnels_cb = callback;
1459 h->info_cls = callback_cls;
1465 * Cancel a monitor request. The monitor callback will not be called.
1467 * @param h Cadet handle.
1468 * @return Closure given to GNUNET_CADET_get_tunnels().
1471 GNUNET_CADET_get_tunnels_cancel (struct GNUNET_CADET_Handle *h)
1473 void *cls = h->info_cls;
1475 h->info_cb.tunnels_cb = NULL;
1482 * Request information about a tunnel of the running cadet peer.
1483 * The callback will be called for the tunnel once.
1484 * Only one info request (of any kind) can be active at once.
1486 * WARNING: unstable API, likely to change in the future!
1488 * @param h Handle to the cadet peer.
1489 * @param id Peer whose tunnel to examine.
1490 * @param callback Function to call with the requested data.
1491 * @param callback_cls Closure for @c callback.
1492 * @return #GNUNET_OK / #GNUNET_SYSERR
1495 GNUNET_CADET_get_tunnel (struct GNUNET_CADET_Handle *h,
1496 const struct GNUNET_PeerIdentity *id,
1497 GNUNET_CADET_TunnelCB callback,
1500 struct GNUNET_CADET_LocalInfo *msg;
1501 struct GNUNET_MQ_Envelope *env;
1503 if (NULL != h->info_cb.tunnel_cb)
1506 return GNUNET_SYSERR;
1508 env = GNUNET_MQ_msg (msg,
1509 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNEL);
1511 GNUNET_MQ_send (h->mq,
1513 h->info_cb.tunnel_cb = callback;
1514 h->info_cls = callback_cls;
1520 * Transitional function to convert an unsigned int port to a hash value.
1521 * WARNING: local static value returned, NOT reentrant!
1522 * WARNING: do not use this function for new code!
1524 * @param port Numerical port (unsigned int format).
1526 * @return A GNUNET_HashCode usable for the new CADET API.
1528 const struct GNUNET_HashCode *
1529 GC_u2h (uint32_t port)
1531 static struct GNUNET_HashCode hash;
1533 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1534 "This is a transitional function, use proper crypto hashes as CADET ports\n");
1535 GNUNET_CRYPTO_hash (&port,
1543 * Connect to the MQ-based cadet service.
1545 * @param cfg Configuration to use.
1547 * @return Handle to the cadet service NULL on error.
1549 struct GNUNET_CADET_Handle *
1550 GNUNET_CADET_connect (const struct GNUNET_CONFIGURATION_Handle *cfg)
1552 struct GNUNET_CADET_Handle *h;
1554 LOG (GNUNET_ERROR_TYPE_DEBUG,
1555 "GNUNET_CADET_connect()\n");
1556 h = GNUNET_new (struct GNUNET_CADET_Handle);
1558 h->ports = GNUNET_CONTAINER_multihashmap_create (4,
1560 h->channels = GNUNET_CONTAINER_multihashmap32_create (4);
1565 GNUNET_CADET_disconnect (h);
1568 h->next_ccn.channel_of_client = htonl (GNUNET_CADET_LOCAL_CHANNEL_ID_CLI);
1569 h->reconnect_time = GNUNET_TIME_UNIT_MILLISECONDS;
1570 h->reconnect_task = NULL;
1577 * Open a port to receive incomming MQ-based channels.
1579 * @param h CADET handle.
1580 * @param port Hash identifying the port.
1581 * @param connects Function called when an incoming channel is connected.
1582 * @param connects_cls Closure for the @a connects handler.
1583 * @param window_changes Function called when the transmit window size changes.
1584 * @param disconnects Function called when a channel is disconnected.
1585 * @param handlers Callbacks for messages we care about, NULL-terminated.
1586 * @return Port handle.
1588 struct GNUNET_CADET_Port *
1589 GNUNET_CADET_open_port (struct GNUNET_CADET_Handle *h,
1590 const struct GNUNET_HashCode *port,
1591 GNUNET_CADET_ConnectEventHandler connects,
1592 void * connects_cls,
1593 GNUNET_CADET_WindowSizeEventHandler window_changes,
1594 GNUNET_CADET_DisconnectEventHandler disconnects,
1595 const struct GNUNET_MQ_MessageHandler *handlers)
1597 struct GNUNET_CADET_PortMessage *msg;
1598 struct GNUNET_MQ_Envelope *env;
1599 struct GNUNET_CADET_Port *p;
1601 GNUNET_assert (NULL != connects);
1602 GNUNET_assert (NULL != disconnects);
1604 p = GNUNET_new (struct GNUNET_CADET_Port);
1607 p->connects = connects;
1608 p->cls = connects_cls;
1609 p->window_changes = window_changes;
1610 p->disconnects = disconnects;
1611 p->handlers = GNUNET_MQ_copy_handlers (handlers);
1613 GNUNET_assert (GNUNET_OK ==
1614 GNUNET_CONTAINER_multihashmap_put (h->ports,
1617 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1619 env = GNUNET_MQ_msg (msg,
1620 GNUNET_MESSAGE_TYPE_CADET_LOCAL_PORT_OPEN);
1622 GNUNET_MQ_send (h->mq,
1629 * Create a new channel towards a remote peer.
1631 * If the destination port is not open by any peer or the destination peer
1632 * does not accept the channel, #GNUNET_CADET_ChannelEndHandler will be called
1635 * @param h CADET handle.
1636 * @param channel_cls Closure for the channel. It's given to:
1637 * - The disconnect handler @a disconnects
1638 * - Each message type callback in @a handlers
1639 * @param destination Peer identity the channel should go to.
1640 * @param port Identification of the destination port.
1641 * @param options CadetOption flag field, with all desired option bits set to 1.
1642 * @param window_changes Function called when the transmit window size changes.
1643 * @param disconnects Function called when the channel is disconnected.
1644 * @param handlers Callbacks for messages we care about, NULL-terminated.
1645 * @return Handle to the channel.
1647 struct GNUNET_CADET_Channel *
1648 GNUNET_CADET_channel_create (struct GNUNET_CADET_Handle *h,
1650 const struct GNUNET_PeerIdentity *destination,
1651 const struct GNUNET_HashCode *port,
1652 enum GNUNET_CADET_ChannelOption options,
1653 GNUNET_CADET_WindowSizeEventHandler window_changes,
1654 GNUNET_CADET_DisconnectEventHandler disconnects,
1655 const struct GNUNET_MQ_MessageHandler *handlers)
1657 struct GNUNET_CADET_Channel *ch;
1658 struct GNUNET_CADET_LocalChannelCreateMessage *msg;
1659 struct GNUNET_MQ_Envelope *env;
1661 GNUNET_assert (NULL != disconnects);
1662 ch = create_channel (h,
1664 ch->ctx = channel_cls;
1665 ch->peer = *destination;
1666 ch->options = options;
1667 ch->window_changes = window_changes;
1668 ch->disconnects = disconnects;
1670 /* Create MQ for channel */
1671 ch->mq = GNUNET_MQ_queue_for_callbacks (&cadet_mq_send_impl,
1672 &cadet_mq_destroy_impl,
1673 &cadet_mq_cancel_impl,
1676 &cadet_mq_error_handler,
1678 GNUNET_MQ_set_handlers_closure (ch->mq, channel_cls);
1680 /* Request channel creation to service */
1681 env = GNUNET_MQ_msg (msg,
1682 GNUNET_MESSAGE_TYPE_CADET_LOCAL_CHANNEL_CREATE);
1685 msg->peer = *destination;
1686 msg->opt = htonl (options);
1687 GNUNET_MQ_send (h->mq,
1694 * Obtain the message queue for a connected peer.
1696 * @param channel The channel handle from which to get the MQ.
1698 * @return NULL if @a channel is not yet connected.
1700 struct GNUNET_MQ_Handle *
1701 GNUNET_CADET_get_mq (const struct GNUNET_CADET_Channel *channel)
1706 /* end of cadet_api.c */