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 * Callback handler for incoming channels on this port.
212 GNUNET_CADET_InboundChannelNotificationHandler *handler;
215 * Closure for @a handler.
220 * Handler for incoming channels on this port
222 GNUNET_CADET_ConnectEventHandler connects;
225 * Closure for @ref connects
230 * Window size change handler.
232 GNUNET_CADET_WindowSizeEventHandler window_changes;
235 * Handler called when an incoming channel is destroyed.
237 GNUNET_CADET_DisconnectEventHandler disconnects;
240 * Payload handlers for incoming channels.
242 struct GNUNET_MQ_MessageHandler *handlers;
247 * Find the Port struct for a hash.
249 * @param h CADET handle.
250 * @param hash HashCode for the port number.
251 * @return The port handle if known, NULL otherwise.
253 static struct GNUNET_CADET_Port *
254 find_port (const struct GNUNET_CADET_Handle *h,
255 const struct GNUNET_HashCode *hash)
257 return GNUNET_CONTAINER_multihashmap_get (h->ports,
263 * Get the channel handler for the channel specified by id from the given handle
265 * @param h Cadet handle
266 * @param ccn ID of the wanted channel
267 * @return handle to the required channel or NULL if not found
269 static struct GNUNET_CADET_Channel *
270 find_channel (struct GNUNET_CADET_Handle *h,
271 struct GNUNET_CADET_ClientChannelNumber ccn)
273 return GNUNET_CONTAINER_multihashmap32_get (h->channels,
274 ntohl (ccn.channel_of_client));
279 * Create a new channel and insert it in the channel list of the cadet handle
281 * @param h Cadet handle
282 * @param ccnp pointer to desired ccn of the channel, NULL to assign one automatically.
283 * @return Handle to the created channel.
285 static struct GNUNET_CADET_Channel *
286 create_channel (struct GNUNET_CADET_Handle *h,
287 const struct GNUNET_CADET_ClientChannelNumber *ccnp)
289 struct GNUNET_CADET_Channel *ch;
290 struct GNUNET_CADET_ClientChannelNumber ccn;
292 ch = GNUNET_new (struct GNUNET_CADET_Channel);
299 h->next_ccn.channel_of_client
300 = htonl (GNUNET_CADET_LOCAL_CHANNEL_ID_CLI | (1 + ntohl (h->next_ccn.channel_of_client)));
308 GNUNET_assert (GNUNET_OK ==
309 GNUNET_CONTAINER_multihashmap32_put (h->channels,
310 ntohl (ch->ccn.channel_of_client),
312 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
318 * Destroy the specified channel.
319 * - Destroys all peers, calling the disconnect callback on each if needed
320 * - Cancels all outgoing traffic for that channel, calling respective notifys
321 * - Calls cleaner if channel was inbound
322 * - Frees all memory used
324 * @param ch Pointer to the channel.
325 * @param call_cleaner Whether to call the cleaner handler.
328 destroy_channel (struct GNUNET_CADET_Channel *ch)
330 struct GNUNET_CADET_Handle *h = ch->cadet;
332 LOG (GNUNET_ERROR_TYPE_DEBUG,
333 "Destroying channel %X of %p\n",
336 GNUNET_assert (GNUNET_YES ==
337 GNUNET_CONTAINER_multihashmap32_remove (h->channels,
338 ntohl (ch->ccn.channel_of_client),
340 if (NULL != ch->mq_cont)
342 GNUNET_SCHEDULER_cancel (ch->mq_cont);
345 /* signal channel destruction */
346 if (NULL != ch->disconnects)
347 ch->disconnects (ch->ctx,
349 if (NULL != ch->pending_env)
350 GNUNET_MQ_discard (ch->pending_env);
351 GNUNET_MQ_destroy (ch->mq);
357 * Reconnect to the service, retransmit all infomation to try to restore the
360 * @param h handle to the cadet
363 reconnect (struct GNUNET_CADET_Handle *h);
367 * Reconnect callback: tries to reconnect again after a failer previous
370 * @param cls closure (cadet handle)
373 reconnect_cbk (void *cls)
375 struct GNUNET_CADET_Handle *h = cls;
377 h->reconnect_task = NULL;
383 * Function called during #reconnect() to destroy
384 * all channels that are still open.
386 * @param cls the `struct GNUNET_CADET_Handle`
387 * @param cid chanenl ID
388 * @param value a `struct GNUNET_CADET_Channel` to destroy
389 * @return #GNUNET_OK (continue to iterate)
392 destroy_channel_on_reconnect_cb (void *cls,
396 /* struct GNUNET_CADET_Handle *handle = cls; */
397 struct GNUNET_CADET_Channel *ch = value;
399 destroy_channel (ch);
405 * Reconnect to the service, retransmit all infomation to try to restore the
408 * @param h handle to the cadet
410 * @return #GNUNET_YES in case of sucess, #GNUNET_NO otherwise (service down...)
413 schedule_reconnect (struct GNUNET_CADET_Handle *h)
415 if (NULL != h->reconnect_task)
417 GNUNET_CONTAINER_multihashmap32_iterate (h->channels,
418 &destroy_channel_on_reconnect_cb,
421 = GNUNET_SCHEDULER_add_delayed (h->reconnect_time,
425 = GNUNET_TIME_STD_BACKOFF (h->reconnect_time);
430 * Notify the application about a change in the window size (if needed).
432 * @param ch Channel to notify about.
435 notify_window_size (struct GNUNET_CADET_Channel *ch)
437 if (NULL != ch->window_changes)
438 ch->window_changes (ch->ctx,
439 ch, /* FIXME: remove 'ch'? */
445 * Allow the MQ implementation to send the next message.
447 * @param cls Closure (channel whose mq to activate).
450 cadet_mq_send_continue (void *cls)
452 struct GNUNET_CADET_Channel *ch = cls;
455 GNUNET_assert (NULL == ch->pending_env);
456 GNUNET_MQ_impl_send_continue (ch->mq);
461 * Transmit the next message from our queue.
463 * @param cls Closure (channel whose mq to activate).
466 cadet_mq_send_now (void *cls)
468 struct GNUNET_CADET_Channel *ch = cls;
469 struct GNUNET_MQ_Envelope *env = ch->pending_env;
472 if (0 == ch->allow_send)
474 /* how did we get here? */
480 /* how did we get here? */
485 GNUNET_MQ_impl_send_in_flight (ch->mq);
486 ch->pending_env = NULL;
487 GNUNET_MQ_notify_sent (env,
488 &cadet_mq_send_continue,
490 GNUNET_MQ_send (ch->cadet->mq,
496 * Implement sending functionality of a message queue for
497 * us sending messages to a peer.
499 * Encapsulates the payload message in a #GNUNET_CADET_LocalData message
500 * in order to label the message with the channel ID and send the
501 * encapsulated message to the service.
503 * @param mq the message queue
504 * @param msg the message to send
505 * @param impl_state state of the implementation
508 cadet_mq_send_impl (struct GNUNET_MQ_Handle *mq,
509 const struct GNUNET_MessageHeader *msg,
512 struct GNUNET_CADET_Channel *ch = impl_state;
513 struct GNUNET_CADET_Handle *h = ch->cadet;
515 struct GNUNET_MQ_Envelope *env;
516 struct GNUNET_CADET_LocalData *cadet_msg;
520 /* We're currently reconnecting, pretend this worked */
521 GNUNET_MQ_impl_send_continue (mq);
525 /* check message size for sanity */
526 msize = ntohs (msg->size);
527 if (msize > GNUNET_CONSTANTS_MAX_CADET_MESSAGE_SIZE)
530 GNUNET_MQ_impl_send_continue (mq);
533 env = GNUNET_MQ_msg_nested_mh (cadet_msg,
534 GNUNET_MESSAGE_TYPE_CADET_LOCAL_DATA,
536 cadet_msg->ccn = ch->ccn;
537 GNUNET_assert (NULL == ch->pending_env);
538 ch->pending_env = env;
539 if (0 < ch->allow_send)
541 = GNUNET_SCHEDULER_add_now (&cadet_mq_send_now,
547 * Handle destruction of a message queue. Implementations must not
548 * free @a mq, but should take care of @a impl_state.
550 * @param mq the message queue to destroy
551 * @param impl_state state of the implementation
554 cadet_mq_destroy_impl (struct GNUNET_MQ_Handle *mq,
557 struct GNUNET_CADET_Channel *ch = impl_state;
559 GNUNET_assert (mq == ch->mq);
565 * We had an error processing a message we forwarded from a peer to
566 * the CADET service. We should just complain about it but otherwise
567 * continue processing.
569 * @param cls closure with our `struct GNUNET_CADET_Channel`
570 * @param error error code
573 cadet_mq_error_handler (void *cls,
574 enum GNUNET_MQ_Error error)
576 struct GNUNET_CADET_Channel *ch = cls;
579 if (GNUNET_MQ_ERROR_NO_MATCH == error)
581 /* Got a message we did not understand, still try to continue! */
582 GNUNET_CADET_receive_done (ch);
586 schedule_reconnect (ch->cadet);
592 * Implementation function that cancels the currently sent message.
593 * Should basically undo whatever #mq_send_impl() did.
595 * @param mq message queue
596 * @param impl_state state specific to the implementation
599 cadet_mq_cancel_impl (struct GNUNET_MQ_Handle *mq,
602 struct GNUNET_CADET_Channel *ch = impl_state;
604 GNUNET_assert (NULL != ch->pending_env);
605 GNUNET_MQ_discard (ch->pending_env);
606 ch->pending_env = NULL;
611 * Process the new channel notification and add it to the channels in the handle
613 * @param h The cadet handle
614 * @param msg A message with the details of the new incoming channel
617 handle_channel_created (void *cls,
618 const struct GNUNET_CADET_LocalChannelCreateMessage *msg)
620 struct GNUNET_CADET_Handle *h = cls;
621 struct GNUNET_CADET_Channel *ch;
622 struct GNUNET_CADET_Port *port;
623 const struct GNUNET_HashCode *port_number;
624 struct GNUNET_CADET_ClientChannelNumber ccn;
627 port_number = &msg->port;
628 if (ntohl (ccn.channel_of_client) >= GNUNET_CADET_LOCAL_CHANNEL_ID_CLI)
637 /* We could have closed the port but the service didn't know about it yet
638 * This is not an error.
640 struct GNUNET_CADET_LocalChannelDestroyMessage *d_msg;
641 struct GNUNET_MQ_Envelope *env;
644 LOG (GNUNET_ERROR_TYPE_DEBUG,
645 "No handler for incoming channel %X (on port %s, recently closed?)\n",
646 ntohl (ccn.channel_of_client),
647 GNUNET_h2s (port_number));
648 env = GNUNET_MQ_msg (d_msg,
649 GNUNET_MESSAGE_TYPE_CADET_LOCAL_CHANNEL_DESTROY);
650 d_msg->ccn = msg->ccn;
651 GNUNET_MQ_send (h->mq,
656 ch = create_channel (h,
658 ch->peer = msg->peer;
660 ch->incoming_port = port;
661 ch->options = ntohl (msg->opt);
662 LOG (GNUNET_ERROR_TYPE_DEBUG,
663 "Creating incoming channel %X [%s] %p\n",
664 ntohl (ccn.channel_of_client),
665 GNUNET_h2s (port_number),
668 GNUNET_assert (NULL != port->connects);
669 ch->window_changes = port->window_changes;
670 ch->disconnects = port->disconnects;
671 ch->mq = GNUNET_MQ_queue_for_callbacks (&cadet_mq_send_impl,
672 &cadet_mq_destroy_impl,
673 &cadet_mq_cancel_impl,
676 &cadet_mq_error_handler,
678 ch->ctx = port->connects (port->cls,
681 GNUNET_MQ_set_handlers_closure (ch->mq,
687 * Process the channel destroy notification and free associated resources
689 * @param h The cadet handle
690 * @param msg A message with the details of the channel being destroyed
693 handle_channel_destroy (void *cls,
694 const struct GNUNET_CADET_LocalChannelDestroyMessage *msg)
696 struct GNUNET_CADET_Handle *h = cls;
697 struct GNUNET_CADET_Channel *ch;
699 LOG (GNUNET_ERROR_TYPE_DEBUG,
700 "Received channel destroy for channel %X from CADET service\n",
701 ntohl (msg->ccn.channel_of_client));
702 ch = find_channel (h,
709 destroy_channel (ch);
714 * Check that message received from CADET service is well-formed.
716 * @param cls the `struct GNUNET_CADET_Handle`
717 * @param message the message we got
718 * @return #GNUNET_OK if the message is well-formed,
719 * #GNUNET_SYSERR otherwise
722 check_local_data (void *cls,
723 const struct GNUNET_CADET_LocalData *message)
725 struct GNUNET_CADET_Handle *h = cls;
726 struct GNUNET_CADET_Channel *ch;
729 size = ntohs (message->header.size);
730 if (sizeof (*message) + sizeof (struct GNUNET_MessageHeader) > size)
733 return GNUNET_SYSERR;
736 ch = find_channel (h,
741 return GNUNET_SYSERR;
749 * Process the incoming data packets, call appropriate handlers.
751 * @param h The cadet handle
752 * @param message A message encapsulating the data
755 handle_local_data (void *cls,
756 const struct GNUNET_CADET_LocalData *message)
758 struct GNUNET_CADET_Handle *h = cls;
759 const struct GNUNET_MessageHeader *payload;
760 struct GNUNET_CADET_Channel *ch;
764 ch = find_channel (h,
773 payload = (const struct GNUNET_MessageHeader *) &message[1];
774 type = ntohs (payload->type);
775 fwd = ntohl (ch->ccn.channel_of_client) <= GNUNET_CADET_LOCAL_CHANNEL_ID_CLI;
776 LOG (GNUNET_ERROR_TYPE_DEBUG,
777 "Got a %s data on channel %s [%X] of type %u\n",
779 GNUNET_i2s (&ch->peer),
780 ntohl (message->ccn.channel_of_client),
782 GNUNET_MQ_inject_message (ch->mq,
788 * Process a local ACK message, enabling the client to send
789 * more data to the service.
791 * @param h Cadet handle.
792 * @param message Message itself.
795 handle_local_ack (void *cls,
796 const struct GNUNET_CADET_LocalAck *message)
798 struct GNUNET_CADET_Handle *h = cls;
799 struct GNUNET_CADET_Channel *ch;
801 ch = find_channel (h,
805 LOG (GNUNET_ERROR_TYPE_DEBUG,
806 "ACK on unknown channel %X\n",
807 ntohl (message->ccn.channel_of_client));
811 if (NULL == ch->pending_env)
813 LOG (GNUNET_ERROR_TYPE_DEBUG,
814 "Got an ACK on mq channel %X, allow send now %u!\n",
815 ntohl (ch->ccn.channel_of_client),
817 notify_window_size (ch);
820 if (NULL != ch->mq_cont)
821 return; /* already working on it! */
822 LOG (GNUNET_ERROR_TYPE_DEBUG,
823 "Got an ACK on mq channel %X, sending pending message!\n",
824 ntohl (ch->ccn.channel_of_client));
826 = GNUNET_SCHEDULER_add_now (&cadet_mq_send_now,
832 * Generic error handler, called with the appropriate error code and
833 * the same closure specified at the creation of the message queue.
834 * Not every message queue implementation supports an error handler.
836 * @param cls closure, a `struct GNUNET_CORE_Handle *`
837 * @param error error code
840 handle_mq_error (void *cls,
841 enum GNUNET_MQ_Error error)
843 struct GNUNET_CADET_Handle *h = cls;
845 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
848 GNUNET_MQ_destroy (h->mq);
855 * Process a local reply about info on all tunnels, pass info to the user.
857 * @param cls Closure (Cadet handle).
858 * @param msg Message itself.
861 handle_get_peers (void *cls,
862 const struct GNUNET_CADET_LocalInfoPeer *msg)
864 struct GNUNET_CADET_Handle *h = cls;
866 if (NULL == h->info_cb.peers_cb)
868 h->info_cb.peers_cb (h->info_cls,
870 (int) ntohs (msg->tunnel),
871 (unsigned int) ntohs (msg->paths),
877 * Check that message received from CADET service is well-formed.
879 * @param cls the `struct GNUNET_CADET_Handle`
880 * @param message the message we got
881 * @return #GNUNET_OK if the message is well-formed,
882 * #GNUNET_SYSERR otherwise
885 check_get_peer (void *cls,
886 const struct GNUNET_CADET_LocalInfoPeer *message)
888 size_t msize = sizeof (struct GNUNET_CADET_LocalInfoPeer);
889 const struct GNUNET_PeerIdentity *paths_array;
895 esize = ntohs (message->header.size);
899 return GNUNET_SYSERR;
901 if (0 != ((esize - msize) % sizeof (struct GNUNET_PeerIdentity)))
904 return GNUNET_SYSERR;
906 peers = (esize - msize) / sizeof (struct GNUNET_PeerIdentity);
907 epaths = ntohs (message->paths);
908 paths_array = (const struct GNUNET_PeerIdentity *) &message[1];
910 for (unsigned int i = 0; i < peers; i++)
911 if (0 == memcmp (&paths_array[i],
912 &message->destination,
913 sizeof (struct GNUNET_PeerIdentity)))
918 return GNUNET_SYSERR;
925 * Process a local peer info reply, pass info to the user.
927 * @param cls Closure (Cadet handle).
928 * @param message Message itself.
931 handle_get_peer (void *cls,
932 const struct GNUNET_CADET_LocalInfoPeer *message)
934 struct GNUNET_CADET_Handle *h = cls;
935 const struct GNUNET_PeerIdentity *paths_array;
937 unsigned int path_length;
941 if (NULL == h->info_cb.peer_cb)
943 paths = ntohs (message->paths);
944 paths_array = (const struct GNUNET_PeerIdentity *) &message[1];
945 peers = (ntohs (message->header.size) - sizeof (*message))
946 / sizeof (struct GNUNET_PeerIdentity);
948 neighbor = GNUNET_NO;
950 for (unsigned int i = 0; i < peers; i++)
952 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
954 GNUNET_i2s (&paths_array[i]));
956 if (0 == memcmp (&paths_array[i], &message->destination,
957 sizeof (struct GNUNET_PeerIdentity)))
959 if (1 == path_length)
960 neighbor = GNUNET_YES;
965 /* Call Callback with tunnel info. */
966 paths_array = (const struct GNUNET_PeerIdentity *) &message[1];
967 h->info_cb.peer_cb (h->info_cls,
968 &message->destination,
969 (int) ntohs (message->tunnel),
977 * Process a local reply about info on all tunnels, pass info to the user.
979 * @param cls Closure (Cadet handle).
980 * @param message Message itself.
983 handle_get_tunnels (void *cls,
984 const struct GNUNET_CADET_LocalInfoTunnel *msg)
986 struct GNUNET_CADET_Handle *h = cls;
988 if (NULL == h->info_cb.tunnels_cb)
990 h->info_cb.tunnels_cb (h->info_cls,
992 ntohl (msg->channels),
993 ntohl (msg->connections),
995 ntohs (msg->cstate));
1001 * Check that message received from CADET service is well-formed.
1003 * @param cls the `struct GNUNET_CADET_Handle`
1004 * @param msg the message we got
1005 * @return #GNUNET_OK if the message is well-formed,
1006 * #GNUNET_SYSERR otherwise
1009 check_get_tunnel (void *cls,
1010 const struct GNUNET_CADET_LocalInfoTunnel *msg)
1017 /* Verify message sanity */
1018 msize = ntohs (msg->header.size);
1019 esize = sizeof (struct GNUNET_CADET_LocalInfoTunnel);
1023 return GNUNET_SYSERR;
1025 ch_n = ntohl (msg->channels);
1026 c_n = ntohl (msg->connections);
1027 esize += ch_n * sizeof (struct GNUNET_CADET_ChannelTunnelNumber);
1028 esize += c_n * sizeof (struct GNUNET_CADET_ConnectionTunnelIdentifier);
1031 GNUNET_break_op (0);
1032 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1033 "m:%u, e: %u (%u ch, %u conn)\n",
1034 (unsigned int) msize,
1035 (unsigned int) esize,
1038 return GNUNET_SYSERR;
1045 * Process a local tunnel info reply, pass info to the user.
1047 * @param cls Closure (Cadet handle).
1048 * @param msg Message itself.
1051 handle_get_tunnel (void *cls,
1052 const struct GNUNET_CADET_LocalInfoTunnel *msg)
1054 struct GNUNET_CADET_Handle *h = cls;
1057 const struct GNUNET_CADET_ConnectionTunnelIdentifier *conns;
1058 const struct GNUNET_CADET_ChannelTunnelNumber *chns;
1060 if (NULL == h->info_cb.tunnel_cb)
1063 ch_n = ntohl (msg->channels);
1064 c_n = ntohl (msg->connections);
1066 /* Call Callback with tunnel info. */
1067 conns = (const struct GNUNET_CADET_ConnectionTunnelIdentifier *) &msg[1];
1068 chns = (const struct GNUNET_CADET_ChannelTunnelNumber *) &conns[c_n];
1069 h->info_cb.tunnel_cb (h->info_cls,
1075 ntohs (msg->estate),
1076 ntohs (msg->cstate));
1081 * Reconnect to the service, retransmit all infomation to try to restore the
1084 * @param h handle to the cadet
1087 reconnect (struct GNUNET_CADET_Handle *h)
1089 struct GNUNET_MQ_MessageHandler handlers[] = {
1090 GNUNET_MQ_hd_fixed_size (channel_created,
1091 GNUNET_MESSAGE_TYPE_CADET_LOCAL_CHANNEL_CREATE,
1092 struct GNUNET_CADET_LocalChannelCreateMessage,
1094 GNUNET_MQ_hd_fixed_size (channel_destroy,
1095 GNUNET_MESSAGE_TYPE_CADET_LOCAL_CHANNEL_DESTROY,
1096 struct GNUNET_CADET_LocalChannelDestroyMessage,
1098 GNUNET_MQ_hd_var_size (local_data,
1099 GNUNET_MESSAGE_TYPE_CADET_LOCAL_DATA,
1100 struct GNUNET_CADET_LocalData,
1102 GNUNET_MQ_hd_fixed_size (local_ack,
1103 GNUNET_MESSAGE_TYPE_CADET_LOCAL_ACK,
1104 struct GNUNET_CADET_LocalAck,
1106 GNUNET_MQ_hd_fixed_size (get_peers,
1107 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEERS,
1108 struct GNUNET_CADET_LocalInfoPeer,
1110 GNUNET_MQ_hd_var_size (get_peer,
1111 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEER,
1112 struct GNUNET_CADET_LocalInfoPeer,
1114 GNUNET_MQ_hd_fixed_size (get_tunnels,
1115 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNELS,
1116 struct GNUNET_CADET_LocalInfoTunnel,
1118 GNUNET_MQ_hd_var_size (get_tunnel,
1119 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNEL,
1120 struct GNUNET_CADET_LocalInfoTunnel,
1122 GNUNET_MQ_handler_end ()
1125 h->mq = GNUNET_CLIENT_connect (h->cfg,
1132 schedule_reconnect (h);
1135 h->reconnect_time = GNUNET_TIME_UNIT_MILLISECONDS;
1140 * Function called during #GNUNET_CADET_disconnect() to destroy
1141 * all channels that are still open.
1143 * @param cls the `struct GNUNET_CADET_Handle`
1144 * @param cid chanenl ID
1145 * @param value a `struct GNUNET_CADET_Channel` to destroy
1146 * @return #GNUNET_OK (continue to iterate)
1149 destroy_channel_cb (void *cls,
1153 /* struct GNUNET_CADET_Handle *handle = cls; */
1154 struct GNUNET_CADET_Channel *ch = value;
1156 if (ntohl (ch->ccn.channel_of_client) >= GNUNET_CADET_LOCAL_CHANNEL_ID_CLI)
1159 LOG (GNUNET_ERROR_TYPE_DEBUG,
1160 "channel %X not destroyed\n",
1161 ntohl (ch->ccn.channel_of_client));
1163 destroy_channel (ch);
1169 * Function called during #GNUNET_CADET_disconnect() to destroy
1170 * all ports that are still open.
1172 * @param cls the `struct GNUNET_CADET_Handle`
1174 * @param value a `struct GNUNET_CADET_Channel` to destroy
1175 * @return #GNUNET_OK (continue to iterate)
1178 destroy_port_cb (void *cls,
1179 const struct GNUNET_HashCode *id,
1182 /* struct GNUNET_CADET_Handle *handle = cls; */
1183 struct GNUNET_CADET_Port *port = value;
1185 /* This is a warning, the app should have cleanly closed all open ports */
1187 GNUNET_CADET_close_port (port);
1193 * Disconnect from the cadet service. All channels will be destroyed. All channel
1194 * disconnect callbacks will be called on any still connected peers, notifying
1195 * about their disconnection. The registered inbound channel cleaner will be
1196 * called should any inbound channels still exist.
1198 * @param handle connection to cadet to disconnect
1201 GNUNET_CADET_disconnect (struct GNUNET_CADET_Handle *handle)
1203 GNUNET_CONTAINER_multihashmap_iterate (handle->ports,
1206 GNUNET_CONTAINER_multihashmap_destroy (handle->ports);
1207 handle->ports = NULL;
1208 GNUNET_CONTAINER_multihashmap32_iterate (handle->channels,
1209 &destroy_channel_cb,
1211 GNUNET_CONTAINER_multihashmap32_destroy (handle->channels);
1212 handle->channels = NULL;
1213 if (NULL != handle->mq)
1215 GNUNET_MQ_destroy (handle->mq);
1218 if (NULL != handle->reconnect_task)
1220 GNUNET_SCHEDULER_cancel (handle->reconnect_task);
1221 handle->reconnect_task = NULL;
1223 GNUNET_free (handle);
1228 * Close a port opened with @a GNUNET_CADET_open_port().
1229 * The @a new_channel callback will no longer be called.
1231 * @param p Port handle.
1234 GNUNET_CADET_close_port (struct GNUNET_CADET_Port *p)
1236 struct GNUNET_CADET_PortMessage *msg;
1237 struct GNUNET_MQ_Envelope *env;
1239 env = GNUNET_MQ_msg (msg,
1240 GNUNET_MESSAGE_TYPE_CADET_LOCAL_PORT_CLOSE);
1242 GNUNET_MQ_send (p->cadet->mq,
1244 GNUNET_assert (GNUNET_YES ==
1245 GNUNET_CONTAINER_multihashmap_remove (p->cadet->ports,
1248 GNUNET_free_non_null (p->handlers);
1254 * Destroy an existing channel.
1256 * The existing end callback for the channel will be called immediately.
1257 * Any pending outgoing messages will be sent but no incoming messages will be
1258 * accepted and no data callbacks will be called.
1260 * @param channel Channel handle, becomes invalid after this call.
1263 GNUNET_CADET_channel_destroy (struct GNUNET_CADET_Channel *channel)
1265 struct GNUNET_CADET_Handle *h = channel->cadet;
1266 struct GNUNET_CADET_LocalChannelDestroyMessage *msg;
1267 struct GNUNET_MQ_Envelope *env;
1271 env = GNUNET_MQ_msg (msg,
1272 GNUNET_MESSAGE_TYPE_CADET_LOCAL_CHANNEL_DESTROY);
1273 msg->ccn = channel->ccn;
1274 GNUNET_MQ_send (h->mq,
1277 destroy_channel (channel);
1282 * Get information about a channel.
1284 * @param channel Channel handle.
1285 * @param option Query (GNUNET_CADET_OPTION_*).
1286 * @param ... dependant on option, currently not used
1288 * @return Union with an answer to the query.
1290 const union GNUNET_CADET_ChannelInfo *
1291 GNUNET_CADET_channel_get_info (struct GNUNET_CADET_Channel *channel,
1292 enum GNUNET_CADET_ChannelOption option,
1295 static int bool_flag;
1299 case GNUNET_CADET_OPTION_NOBUFFER:
1300 case GNUNET_CADET_OPTION_RELIABLE:
1301 case GNUNET_CADET_OPTION_OUT_OF_ORDER:
1302 if (0 != (option & channel->options))
1303 bool_flag = GNUNET_YES;
1305 bool_flag = GNUNET_NO;
1306 return (const union GNUNET_CADET_ChannelInfo *) &bool_flag;
1308 case GNUNET_CADET_OPTION_PEER:
1309 return (const union GNUNET_CADET_ChannelInfo *) &channel->peer;
1319 * Send an ack on the channel to confirm the processing of a message.
1321 * @param ch Channel on which to send the ACK.
1324 GNUNET_CADET_receive_done (struct GNUNET_CADET_Channel *channel)
1326 struct GNUNET_CADET_LocalAck *msg;
1327 struct GNUNET_MQ_Envelope *env;
1329 env = GNUNET_MQ_msg (msg,
1330 GNUNET_MESSAGE_TYPE_CADET_LOCAL_ACK);
1331 LOG (GNUNET_ERROR_TYPE_DEBUG,
1332 "Sending ACK on channel %X\n",
1333 ntohl (channel->ccn.channel_of_client));
1334 msg->ccn = channel->ccn;
1335 GNUNET_MQ_send (channel->cadet->mq,
1341 * Send message of @a type to CADET service of @a h
1343 * @param h handle to CADET service
1344 * @param type message type of trivial information request to send
1347 send_info_request (struct GNUNET_CADET_Handle *h,
1350 struct GNUNET_MessageHeader *msg;
1351 struct GNUNET_MQ_Envelope *env;
1353 env = GNUNET_MQ_msg (msg,
1355 GNUNET_MQ_send (h->mq,
1361 * Request a debug dump on the service's STDERR.
1363 * WARNING: unstable API, likely to change in the future!
1365 * @param h cadet handle
1368 GNUNET_CADET_request_dump (struct GNUNET_CADET_Handle *h)
1370 send_info_request (h,
1371 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_DUMP);
1376 * Request information about peers known to the running cadet service.
1377 * The callback will be called for every peer known to the service.
1378 * Only one info request (of any kind) can be active at once.
1380 * WARNING: unstable API, likely to change in the future!
1382 * @param h Handle to the cadet peer.
1383 * @param callback Function to call with the requested data.
1384 * @param callback_cls Closure for @c callback.
1385 * @return #GNUNET_OK / #GNUNET_SYSERR
1388 GNUNET_CADET_get_peers (struct GNUNET_CADET_Handle *h,
1389 GNUNET_CADET_PeersCB callback,
1392 if (NULL != h->info_cb.peers_cb)
1395 return GNUNET_SYSERR;
1397 send_info_request (h,
1398 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEERS);
1399 h->info_cb.peers_cb = callback;
1400 h->info_cls = callback_cls;
1406 * Cancel a peer info request. The callback will not be called (anymore).
1408 * WARNING: unstable API, likely to change in the future!
1410 * @param h Cadet handle.
1411 * @return Closure given to GNUNET_CADET_get_peers().
1414 GNUNET_CADET_get_peers_cancel (struct GNUNET_CADET_Handle *h)
1416 void *cls = h->info_cls;
1418 h->info_cb.peers_cb = NULL;
1425 * Request information about a peer known to the running cadet peer.
1426 * The callback will be called for the tunnel once.
1427 * Only one info request (of any kind) can be active at once.
1429 * WARNING: unstable API, likely to change in the future!
1431 * @param h Handle to the cadet peer.
1432 * @param id Peer whose tunnel to examine.
1433 * @param callback Function to call with the requested data.
1434 * @param callback_cls Closure for @c callback.
1435 * @return #GNUNET_OK / #GNUNET_SYSERR
1438 GNUNET_CADET_get_peer (struct GNUNET_CADET_Handle *h,
1439 const struct GNUNET_PeerIdentity *id,
1440 GNUNET_CADET_PeerCB callback,
1443 struct GNUNET_CADET_LocalInfo *msg;
1444 struct GNUNET_MQ_Envelope *env;
1446 if (NULL != h->info_cb.peer_cb)
1449 return GNUNET_SYSERR;
1451 env = GNUNET_MQ_msg (msg,
1452 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEER);
1454 GNUNET_MQ_send (h->mq,
1456 h->info_cb.peer_cb = callback;
1457 h->info_cls = callback_cls;
1463 * Request information about tunnels of the running cadet peer.
1464 * The callback will be called for every tunnel of the service.
1465 * Only one info request (of any kind) can be active at once.
1467 * WARNING: unstable API, likely to change in the future!
1469 * @param h Handle to the cadet peer.
1470 * @param callback Function to call with the requested data.
1471 * @param callback_cls Closure for @c callback.
1472 * @return #GNUNET_OK / #GNUNET_SYSERR
1475 GNUNET_CADET_get_tunnels (struct GNUNET_CADET_Handle *h,
1476 GNUNET_CADET_TunnelsCB callback,
1479 if (NULL != h->info_cb.tunnels_cb)
1482 return GNUNET_SYSERR;
1484 send_info_request (h,
1485 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNELS);
1486 h->info_cb.tunnels_cb = callback;
1487 h->info_cls = callback_cls;
1493 * Cancel a monitor request. The monitor callback will not be called.
1495 * @param h Cadet handle.
1496 * @return Closure given to GNUNET_CADET_get_tunnels().
1499 GNUNET_CADET_get_tunnels_cancel (struct GNUNET_CADET_Handle *h)
1501 void *cls = h->info_cls;
1503 h->info_cb.tunnels_cb = NULL;
1510 * Request information about a tunnel of the running cadet peer.
1511 * The callback will be called for the tunnel once.
1512 * Only one info request (of any kind) can be active at once.
1514 * WARNING: unstable API, likely to change in the future!
1516 * @param h Handle to the cadet peer.
1517 * @param id Peer whose tunnel to examine.
1518 * @param callback Function to call with the requested data.
1519 * @param callback_cls Closure for @c callback.
1520 * @return #GNUNET_OK / #GNUNET_SYSERR
1523 GNUNET_CADET_get_tunnel (struct GNUNET_CADET_Handle *h,
1524 const struct GNUNET_PeerIdentity *id,
1525 GNUNET_CADET_TunnelCB callback,
1528 struct GNUNET_CADET_LocalInfo *msg;
1529 struct GNUNET_MQ_Envelope *env;
1531 if (NULL != h->info_cb.tunnel_cb)
1534 return GNUNET_SYSERR;
1536 env = GNUNET_MQ_msg (msg,
1537 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNEL);
1539 GNUNET_MQ_send (h->mq,
1541 h->info_cb.tunnel_cb = callback;
1542 h->info_cls = callback_cls;
1548 * Transitional function to convert an unsigned int port to a hash value.
1549 * WARNING: local static value returned, NOT reentrant!
1550 * WARNING: do not use this function for new code!
1552 * @param port Numerical port (unsigned int format).
1554 * @return A GNUNET_HashCode usable for the new CADET API.
1556 const struct GNUNET_HashCode *
1557 GC_u2h (uint32_t port)
1559 static struct GNUNET_HashCode hash;
1561 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1562 "This is a transitional function, use proper crypto hashes as CADET ports\n");
1563 GNUNET_CRYPTO_hash (&port,
1571 * Connect to the MQ-based cadet service.
1573 * @param cfg Configuration to use.
1575 * @return Handle to the cadet service NULL on error.
1577 struct GNUNET_CADET_Handle *
1578 GNUNET_CADET_connecT (const struct GNUNET_CONFIGURATION_Handle *cfg)
1580 struct GNUNET_CADET_Handle *h;
1582 LOG (GNUNET_ERROR_TYPE_DEBUG,
1583 "GNUNET_CADET_connecT()\n");
1584 h = GNUNET_new (struct GNUNET_CADET_Handle);
1586 h->ports = GNUNET_CONTAINER_multihashmap_create (4,
1588 h->channels = GNUNET_CONTAINER_multihashmap32_create (4);
1593 GNUNET_CADET_disconnect (h);
1596 h->next_ccn.channel_of_client = htonl (GNUNET_CADET_LOCAL_CHANNEL_ID_CLI);
1597 h->reconnect_time = GNUNET_TIME_UNIT_MILLISECONDS;
1598 h->reconnect_task = NULL;
1605 * Open a port to receive incomming MQ-based channels.
1607 * @param h CADET handle.
1608 * @param port Hash identifying the port.
1609 * @param connects Function called when an incoming channel is connected.
1610 * @param connects_cls Closure for the @a connects handler.
1611 * @param window_changes Function called when the transmit window size changes.
1612 * @param disconnects Function called when a channel is disconnected.
1613 * @param handlers Callbacks for messages we care about, NULL-terminated.
1614 * @return Port handle.
1616 struct GNUNET_CADET_Port *
1617 GNUNET_CADET_open_porT (struct GNUNET_CADET_Handle *h,
1618 const struct GNUNET_HashCode *port,
1619 GNUNET_CADET_ConnectEventHandler connects,
1620 void * connects_cls,
1621 GNUNET_CADET_WindowSizeEventHandler window_changes,
1622 GNUNET_CADET_DisconnectEventHandler disconnects,
1623 const struct GNUNET_MQ_MessageHandler *handlers)
1625 struct GNUNET_CADET_PortMessage *msg;
1626 struct GNUNET_MQ_Envelope *env;
1627 struct GNUNET_CADET_Port *p;
1629 GNUNET_assert (NULL != connects);
1630 GNUNET_assert (NULL != disconnects);
1632 p = GNUNET_new (struct GNUNET_CADET_Port);
1635 p->connects = connects;
1636 p->cls = connects_cls;
1637 p->window_changes = window_changes;
1638 p->disconnects = disconnects;
1639 p->handlers = GNUNET_MQ_copy_handlers (handlers);
1641 GNUNET_assert (GNUNET_OK ==
1642 GNUNET_CONTAINER_multihashmap_put (h->ports,
1645 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1647 env = GNUNET_MQ_msg (msg,
1648 GNUNET_MESSAGE_TYPE_CADET_LOCAL_PORT_OPEN);
1650 GNUNET_MQ_send (h->mq,
1657 * Create a new channel towards a remote peer.
1659 * If the destination port is not open by any peer or the destination peer
1660 * does not accept the channel, #GNUNET_CADET_ChannelEndHandler will be called
1663 * @param h CADET handle.
1664 * @param channel_cls Closure for the channel. It's given to:
1665 * - The disconnect handler @a disconnects
1666 * - Each message type callback in @a handlers
1667 * @param destination Peer identity the channel should go to.
1668 * @param port Identification of the destination port.
1669 * @param options CadetOption flag field, with all desired option bits set to 1.
1670 * @param window_changes Function called when the transmit window size changes.
1671 * @param disconnects Function called when the channel is disconnected.
1672 * @param handlers Callbacks for messages we care about, NULL-terminated.
1673 * @return Handle to the channel.
1675 struct GNUNET_CADET_Channel *
1676 GNUNET_CADET_channel_creatE (struct GNUNET_CADET_Handle *h,
1678 const struct GNUNET_PeerIdentity *destination,
1679 const struct GNUNET_HashCode *port,
1680 enum GNUNET_CADET_ChannelOption options,
1681 GNUNET_CADET_WindowSizeEventHandler window_changes,
1682 GNUNET_CADET_DisconnectEventHandler disconnects,
1683 const struct GNUNET_MQ_MessageHandler *handlers)
1685 struct GNUNET_CADET_Channel *ch;
1686 struct GNUNET_CADET_LocalChannelCreateMessage *msg;
1687 struct GNUNET_MQ_Envelope *env;
1689 GNUNET_assert (NULL != disconnects);
1690 ch = create_channel (h,
1692 ch->ctx = channel_cls;
1693 ch->peer = *destination;
1694 ch->options = options;
1695 ch->window_changes = window_changes;
1696 ch->disconnects = disconnects;
1698 /* Create MQ for channel */
1699 ch->mq = GNUNET_MQ_queue_for_callbacks (&cadet_mq_send_impl,
1700 &cadet_mq_destroy_impl,
1701 &cadet_mq_cancel_impl,
1704 &cadet_mq_error_handler,
1706 GNUNET_MQ_set_handlers_closure (ch->mq, channel_cls);
1708 /* Request channel creation to service */
1709 env = GNUNET_MQ_msg (msg,
1710 GNUNET_MESSAGE_TYPE_CADET_LOCAL_CHANNEL_CREATE);
1713 msg->peer = *destination;
1714 msg->opt = htonl (options);
1715 GNUNET_MQ_send (h->mq,
1722 * Obtain the message queue for a connected peer.
1724 * @param channel The channel handle from which to get the MQ.
1726 * @return NULL if @a channel is not yet connected.
1728 struct GNUNET_MQ_Handle *
1729 GNUNET_CADET_get_mq (const struct GNUNET_CADET_Channel *channel)
1734 /* end of cadet_api.c */