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;
1186 GNUNET_CADET_close_port (port);
1192 * Disconnect from the cadet service. All channels will be destroyed. All channel
1193 * disconnect callbacks will be called on any still connected peers, notifying
1194 * about their disconnection. The registered inbound channel cleaner will be
1195 * called should any inbound channels still exist.
1197 * @param handle connection to cadet to disconnect
1200 GNUNET_CADET_disconnect (struct GNUNET_CADET_Handle *handle)
1202 GNUNET_CONTAINER_multihashmap_iterate (handle->ports,
1205 GNUNET_CONTAINER_multihashmap_destroy (handle->ports);
1206 handle->ports = NULL;
1207 GNUNET_CONTAINER_multihashmap32_iterate (handle->channels,
1208 &destroy_channel_cb,
1210 GNUNET_CONTAINER_multihashmap32_destroy (handle->channels);
1211 handle->channels = NULL;
1212 if (NULL != handle->mq)
1214 GNUNET_MQ_destroy (handle->mq);
1217 if (NULL != handle->reconnect_task)
1219 GNUNET_SCHEDULER_cancel (handle->reconnect_task);
1220 handle->reconnect_task = NULL;
1222 GNUNET_free (handle);
1227 * Close a port opened with @a GNUNET_CADET_open_port().
1228 * The @a new_channel callback will no longer be called.
1230 * @param p Port handle.
1233 GNUNET_CADET_close_port (struct GNUNET_CADET_Port *p)
1235 struct GNUNET_CADET_PortMessage *msg;
1236 struct GNUNET_MQ_Envelope *env;
1238 env = GNUNET_MQ_msg (msg,
1239 GNUNET_MESSAGE_TYPE_CADET_LOCAL_PORT_CLOSE);
1241 GNUNET_MQ_send (p->cadet->mq,
1243 GNUNET_assert (GNUNET_YES ==
1244 GNUNET_CONTAINER_multihashmap_remove (p->cadet->ports,
1247 GNUNET_free (p->handlers);
1253 * Destroy an existing channel.
1255 * The existing end callback for the channel will be called immediately.
1256 * Any pending outgoing messages will be sent but no incoming messages will be
1257 * accepted and no data callbacks will be called.
1259 * @param channel Channel handle, becomes invalid after this call.
1262 GNUNET_CADET_channel_destroy (struct GNUNET_CADET_Channel *channel)
1264 struct GNUNET_CADET_Handle *h = channel->cadet;
1265 struct GNUNET_CADET_LocalChannelDestroyMessage *msg;
1266 struct GNUNET_MQ_Envelope *env;
1270 env = GNUNET_MQ_msg (msg,
1271 GNUNET_MESSAGE_TYPE_CADET_LOCAL_CHANNEL_DESTROY);
1272 msg->ccn = channel->ccn;
1273 GNUNET_MQ_send (h->mq,
1276 destroy_channel (channel);
1281 * Get information about a channel.
1283 * @param channel Channel handle.
1284 * @param option Query (GNUNET_CADET_OPTION_*).
1285 * @param ... dependant on option, currently not used
1287 * @return Union with an answer to the query.
1289 const union GNUNET_CADET_ChannelInfo *
1290 GNUNET_CADET_channel_get_info (struct GNUNET_CADET_Channel *channel,
1291 enum GNUNET_CADET_ChannelOption option,
1294 static int bool_flag;
1298 case GNUNET_CADET_OPTION_NOBUFFER:
1299 case GNUNET_CADET_OPTION_RELIABLE:
1300 case GNUNET_CADET_OPTION_OUT_OF_ORDER:
1301 if (0 != (option & channel->options))
1302 bool_flag = GNUNET_YES;
1304 bool_flag = GNUNET_NO;
1305 return (const union GNUNET_CADET_ChannelInfo *) &bool_flag;
1307 case GNUNET_CADET_OPTION_PEER:
1308 return (const union GNUNET_CADET_ChannelInfo *) &channel->peer;
1318 * Send an ack on the channel to confirm the processing of a message.
1320 * @param ch Channel on which to send the ACK.
1323 GNUNET_CADET_receive_done (struct GNUNET_CADET_Channel *channel)
1325 struct GNUNET_CADET_LocalAck *msg;
1326 struct GNUNET_MQ_Envelope *env;
1328 env = GNUNET_MQ_msg (msg,
1329 GNUNET_MESSAGE_TYPE_CADET_LOCAL_ACK);
1330 LOG (GNUNET_ERROR_TYPE_DEBUG,
1331 "Sending ACK on channel %X\n",
1332 ntohl (channel->ccn.channel_of_client));
1333 msg->ccn = channel->ccn;
1334 GNUNET_MQ_send (channel->cadet->mq,
1340 * Send message of @a type to CADET service of @a h
1342 * @param h handle to CADET service
1343 * @param type message type of trivial information request to send
1346 send_info_request (struct GNUNET_CADET_Handle *h,
1349 struct GNUNET_MessageHeader *msg;
1350 struct GNUNET_MQ_Envelope *env;
1352 env = GNUNET_MQ_msg (msg,
1354 GNUNET_MQ_send (h->mq,
1360 * Request a debug dump on the service's STDERR.
1362 * WARNING: unstable API, likely to change in the future!
1364 * @param h cadet handle
1367 GNUNET_CADET_request_dump (struct GNUNET_CADET_Handle *h)
1369 send_info_request (h,
1370 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_DUMP);
1375 * Request information about peers known to the running cadet service.
1376 * The callback will be called for every peer known to the service.
1377 * Only one info request (of any kind) can be active at once.
1379 * WARNING: unstable API, likely to change in the future!
1381 * @param h Handle to the cadet peer.
1382 * @param callback Function to call with the requested data.
1383 * @param callback_cls Closure for @c callback.
1384 * @return #GNUNET_OK / #GNUNET_SYSERR
1387 GNUNET_CADET_get_peers (struct GNUNET_CADET_Handle *h,
1388 GNUNET_CADET_PeersCB callback,
1391 if (NULL != h->info_cb.peers_cb)
1394 return GNUNET_SYSERR;
1396 send_info_request (h,
1397 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEERS);
1398 h->info_cb.peers_cb = callback;
1399 h->info_cls = callback_cls;
1405 * Cancel a peer info request. The callback will not be called (anymore).
1407 * WARNING: unstable API, likely to change in the future!
1409 * @param h Cadet handle.
1410 * @return Closure given to GNUNET_CADET_get_peers().
1413 GNUNET_CADET_get_peers_cancel (struct GNUNET_CADET_Handle *h)
1415 void *cls = h->info_cls;
1417 h->info_cb.peers_cb = NULL;
1424 * Request information about a peer known to the running cadet peer.
1425 * The callback will be called for the tunnel once.
1426 * Only one info request (of any kind) can be active at once.
1428 * WARNING: unstable API, likely to change in the future!
1430 * @param h Handle to the cadet peer.
1431 * @param id Peer whose tunnel to examine.
1432 * @param callback Function to call with the requested data.
1433 * @param callback_cls Closure for @c callback.
1434 * @return #GNUNET_OK / #GNUNET_SYSERR
1437 GNUNET_CADET_get_peer (struct GNUNET_CADET_Handle *h,
1438 const struct GNUNET_PeerIdentity *id,
1439 GNUNET_CADET_PeerCB callback,
1442 struct GNUNET_CADET_LocalInfo *msg;
1443 struct GNUNET_MQ_Envelope *env;
1445 if (NULL != h->info_cb.peer_cb)
1448 return GNUNET_SYSERR;
1450 env = GNUNET_MQ_msg (msg,
1451 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEER);
1453 GNUNET_MQ_send (h->mq,
1455 h->info_cb.peer_cb = callback;
1456 h->info_cls = callback_cls;
1462 * Request information about tunnels of the running cadet peer.
1463 * The callback will be called for every tunnel of the service.
1464 * Only one info request (of any kind) can be active at once.
1466 * WARNING: unstable API, likely to change in the future!
1468 * @param h Handle to the cadet peer.
1469 * @param callback Function to call with the requested data.
1470 * @param callback_cls Closure for @c callback.
1471 * @return #GNUNET_OK / #GNUNET_SYSERR
1474 GNUNET_CADET_get_tunnels (struct GNUNET_CADET_Handle *h,
1475 GNUNET_CADET_TunnelsCB callback,
1478 if (NULL != h->info_cb.tunnels_cb)
1481 return GNUNET_SYSERR;
1483 send_info_request (h,
1484 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNELS);
1485 h->info_cb.tunnels_cb = callback;
1486 h->info_cls = callback_cls;
1492 * Cancel a monitor request. The monitor callback will not be called.
1494 * @param h Cadet handle.
1495 * @return Closure given to GNUNET_CADET_get_tunnels().
1498 GNUNET_CADET_get_tunnels_cancel (struct GNUNET_CADET_Handle *h)
1500 void *cls = h->info_cls;
1502 h->info_cb.tunnels_cb = NULL;
1509 * Request information about a tunnel of the running cadet peer.
1510 * The callback will be called for the tunnel once.
1511 * Only one info request (of any kind) can be active at once.
1513 * WARNING: unstable API, likely to change in the future!
1515 * @param h Handle to the cadet peer.
1516 * @param id Peer whose tunnel to examine.
1517 * @param callback Function to call with the requested data.
1518 * @param callback_cls Closure for @c callback.
1519 * @return #GNUNET_OK / #GNUNET_SYSERR
1522 GNUNET_CADET_get_tunnel (struct GNUNET_CADET_Handle *h,
1523 const struct GNUNET_PeerIdentity *id,
1524 GNUNET_CADET_TunnelCB callback,
1527 struct GNUNET_CADET_LocalInfo *msg;
1528 struct GNUNET_MQ_Envelope *env;
1530 if (NULL != h->info_cb.tunnel_cb)
1533 return GNUNET_SYSERR;
1535 env = GNUNET_MQ_msg (msg,
1536 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNEL);
1538 GNUNET_MQ_send (h->mq,
1540 h->info_cb.tunnel_cb = callback;
1541 h->info_cls = callback_cls;
1547 * Transitional function to convert an unsigned int port to a hash value.
1548 * WARNING: local static value returned, NOT reentrant!
1549 * WARNING: do not use this function for new code!
1551 * @param port Numerical port (unsigned int format).
1553 * @return A GNUNET_HashCode usable for the new CADET API.
1555 const struct GNUNET_HashCode *
1556 GC_u2h (uint32_t port)
1558 static struct GNUNET_HashCode hash;
1560 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1561 "This is a transitional function, use proper crypto hashes as CADET ports\n");
1562 GNUNET_CRYPTO_hash (&port,
1570 * Connect to the MQ-based cadet service.
1572 * @param cfg Configuration to use.
1574 * @return Handle to the cadet service NULL on error.
1576 struct GNUNET_CADET_Handle *
1577 GNUNET_CADET_connecT (const struct GNUNET_CONFIGURATION_Handle *cfg)
1579 struct GNUNET_CADET_Handle *h;
1581 LOG (GNUNET_ERROR_TYPE_DEBUG,
1582 "GNUNET_CADET_connecT()\n");
1583 h = GNUNET_new (struct GNUNET_CADET_Handle);
1585 h->ports = GNUNET_CONTAINER_multihashmap_create (4,
1587 h->channels = GNUNET_CONTAINER_multihashmap32_create (4);
1592 GNUNET_CADET_disconnect (h);
1595 h->next_ccn.channel_of_client = htonl (GNUNET_CADET_LOCAL_CHANNEL_ID_CLI);
1596 h->reconnect_time = GNUNET_TIME_UNIT_MILLISECONDS;
1597 h->reconnect_task = NULL;
1604 * Open a port to receive incomming MQ-based channels.
1606 * @param h CADET handle.
1607 * @param port Hash identifying the port.
1608 * @param connects Function called when an incoming channel is connected.
1609 * @param connects_cls Closure for the @a connects handler.
1610 * @param window_changes Function called when the transmit window size changes.
1611 * @param disconnects Function called when a channel is disconnected.
1612 * @param handlers Callbacks for messages we care about, NULL-terminated.
1613 * @return Port handle.
1615 struct GNUNET_CADET_Port *
1616 GNUNET_CADET_open_porT (struct GNUNET_CADET_Handle *h,
1617 const struct GNUNET_HashCode *port,
1618 GNUNET_CADET_ConnectEventHandler connects,
1619 void * connects_cls,
1620 GNUNET_CADET_WindowSizeEventHandler window_changes,
1621 GNUNET_CADET_DisconnectEventHandler disconnects,
1622 const struct GNUNET_MQ_MessageHandler *handlers)
1624 struct GNUNET_CADET_PortMessage *msg;
1625 struct GNUNET_MQ_Envelope *env;
1626 struct GNUNET_CADET_Port *p;
1628 GNUNET_assert (NULL != connects);
1629 GNUNET_assert (NULL != disconnects);
1631 p = GNUNET_new (struct GNUNET_CADET_Port);
1634 p->connects = connects;
1635 p->cls = connects_cls;
1636 p->window_changes = window_changes;
1637 p->disconnects = disconnects;
1638 if (NULL != handlers)
1642 for (i=0;NULL != handlers[i].cb; i++) ;
1643 p->handlers = GNUNET_new_array (i + 1,
1644 struct GNUNET_MQ_MessageHandler);
1645 GNUNET_memcpy ((struct GNUNET_MQ_MessageHandler *) p->handlers,
1647 i * sizeof (struct GNUNET_MQ_MessageHandler));
1650 GNUNET_assert (GNUNET_OK ==
1651 GNUNET_CONTAINER_multihashmap_put (h->ports,
1654 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1656 env = GNUNET_MQ_msg (msg,
1657 GNUNET_MESSAGE_TYPE_CADET_LOCAL_PORT_OPEN);
1659 GNUNET_MQ_send (h->mq,
1666 * Create a new channel towards a remote peer.
1668 * If the destination port is not open by any peer or the destination peer
1669 * does not accept the channel, #GNUNET_CADET_ChannelEndHandler will be called
1672 * @param h CADET handle.
1673 * @param channel_cls Closure for the channel. It's given to:
1674 * - The disconnect handler @a disconnects
1675 * - Each message type callback in @a handlers
1676 * @param destination Peer identity the channel should go to.
1677 * @param port Identification of the destination port.
1678 * @param options CadetOption flag field, with all desired option bits set to 1.
1679 * @param window_changes Function called when the transmit window size changes.
1680 * @param disconnects Function called when the channel is disconnected.
1681 * @param handlers Callbacks for messages we care about, NULL-terminated.
1682 * @return Handle to the channel.
1684 struct GNUNET_CADET_Channel *
1685 GNUNET_CADET_channel_creatE (struct GNUNET_CADET_Handle *h,
1687 const struct GNUNET_PeerIdentity *destination,
1688 const struct GNUNET_HashCode *port,
1689 enum GNUNET_CADET_ChannelOption options,
1690 GNUNET_CADET_WindowSizeEventHandler window_changes,
1691 GNUNET_CADET_DisconnectEventHandler disconnects,
1692 const struct GNUNET_MQ_MessageHandler *handlers)
1694 struct GNUNET_CADET_Channel *ch;
1695 struct GNUNET_CADET_LocalChannelCreateMessage *msg;
1696 struct GNUNET_MQ_Envelope *env;
1698 GNUNET_assert (NULL != disconnects);
1699 ch = create_channel (h,
1701 ch->ctx = channel_cls;
1702 ch->peer = *destination;
1703 ch->options = options;
1704 ch->window_changes = window_changes;
1705 ch->disconnects = disconnects;
1707 /* Create MQ for channel */
1708 ch->mq = GNUNET_MQ_queue_for_callbacks (&cadet_mq_send_impl,
1709 &cadet_mq_destroy_impl,
1710 &cadet_mq_cancel_impl,
1713 &cadet_mq_error_handler,
1715 GNUNET_MQ_set_handlers_closure (ch->mq, channel_cls);
1717 /* Request channel creation to service */
1718 env = GNUNET_MQ_msg (msg,
1719 GNUNET_MESSAGE_TYPE_CADET_LOCAL_CHANNEL_CREATE);
1722 msg->peer = *destination;
1723 msg->opt = htonl (options);
1724 GNUNET_MQ_send (h->mq,
1731 * Obtain the message queue for a connected peer.
1733 * @param channel The channel handle from which to get the MQ.
1735 * @return NULL if @a channel is not yet connected.
1737 struct GNUNET_MQ_Handle *
1738 GNUNET_CADET_get_mq (const struct GNUNET_CADET_Channel *channel)
1743 /* end of cadet_api.c */