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",
329 htonl (ch->ccn.channel_of_client),
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 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
395 "Destroying channel due to reconnect\n");
396 destroy_channel (ch);
402 * Reconnect to the service, retransmit all infomation to try to restore the
405 * @param h handle to the cadet
407 * @return #GNUNET_YES in case of sucess, #GNUNET_NO otherwise (service down...)
410 schedule_reconnect (struct GNUNET_CADET_Handle *h)
412 if (NULL != h->reconnect_task)
414 GNUNET_CONTAINER_multihashmap32_iterate (h->channels,
415 &destroy_channel_on_reconnect_cb,
418 = GNUNET_SCHEDULER_add_delayed (h->reconnect_time,
422 = GNUNET_TIME_STD_BACKOFF (h->reconnect_time);
427 * Notify the application about a change in the window size (if needed).
429 * @param ch Channel to notify about.
432 notify_window_size (struct GNUNET_CADET_Channel *ch)
434 if (NULL != ch->window_changes)
435 ch->window_changes (ch->ctx,
436 ch, /* FIXME: remove 'ch'? */
442 * Transmit the next message from our queue.
444 * @param cls Closure (channel whose mq to activate).
447 cadet_mq_send_now (void *cls)
449 struct GNUNET_CADET_Channel *ch = cls;
450 struct GNUNET_MQ_Envelope *env = ch->pending_env;
453 if (0 == ch->allow_send)
455 /* how did we get here? */
461 /* how did we get here? */
466 ch->pending_env = NULL;
467 GNUNET_MQ_send (ch->cadet->mq,
469 GNUNET_MQ_impl_send_continue (ch->mq);
474 * Implement sending functionality of a message queue for
475 * us sending messages to a peer.
477 * Encapsulates the payload message in a #GNUNET_CADET_LocalData message
478 * in order to label the message with the channel ID and send the
479 * encapsulated message to the service.
481 * @param mq the message queue
482 * @param msg the message to send
483 * @param impl_state state of the implementation
486 cadet_mq_send_impl (struct GNUNET_MQ_Handle *mq,
487 const struct GNUNET_MessageHeader *msg,
490 struct GNUNET_CADET_Channel *ch = impl_state;
491 struct GNUNET_CADET_Handle *h = ch->cadet;
493 struct GNUNET_MQ_Envelope *env;
494 struct GNUNET_CADET_LocalData *cadet_msg = NULL;
498 /* We're currently reconnecting, pretend this worked */
499 GNUNET_MQ_impl_send_continue (mq);
503 /* check message size for sanity */
504 msize = ntohs (msg->size);
505 if (msize > GNUNET_CONSTANTS_MAX_CADET_MESSAGE_SIZE)
508 GNUNET_MQ_impl_send_continue (mq);
511 env = GNUNET_MQ_msg_nested_mh (cadet_msg,
512 GNUNET_MESSAGE_TYPE_CADET_LOCAL_DATA,
514 cadet_msg->ccn = ch->ccn;
515 GNUNET_assert (NULL == ch->pending_env);
516 ch->pending_env = env;
517 if (0 < ch->allow_send)
519 = GNUNET_SCHEDULER_add_now (&cadet_mq_send_now,
525 * Handle destruction of a message queue. Implementations must not
526 * free @a mq, but should take care of @a impl_state.
528 * @param mq the message queue to destroy
529 * @param impl_state state of the implementation
532 cadet_mq_destroy_impl (struct GNUNET_MQ_Handle *mq,
535 struct GNUNET_CADET_Channel *ch = impl_state;
537 GNUNET_assert (mq == ch->mq);
543 * We had an error processing a message we forwarded from a peer to
544 * the CADET service. We should just complain about it but otherwise
545 * continue processing.
547 * @param cls closure with our `struct GNUNET_CADET_Channel`
548 * @param error error code
551 cadet_mq_error_handler (void *cls,
552 enum GNUNET_MQ_Error error)
554 struct GNUNET_CADET_Channel *ch = cls;
557 if (GNUNET_MQ_ERROR_NO_MATCH == error)
559 /* Got a message we did not understand, still try to continue! */
560 GNUNET_CADET_receive_done (ch);
564 schedule_reconnect (ch->cadet);
570 * Implementation function that cancels the currently sent message.
571 * Should basically undo whatever #mq_send_impl() did.
573 * @param mq message queue
574 * @param impl_state state specific to the implementation
577 cadet_mq_cancel_impl (struct GNUNET_MQ_Handle *mq,
580 struct GNUNET_CADET_Channel *ch = impl_state;
582 GNUNET_assert (NULL != ch->pending_env);
583 GNUNET_MQ_discard (ch->pending_env);
584 ch->pending_env = NULL;
585 if (NULL != ch->mq_cont)
587 GNUNET_SCHEDULER_cancel (ch->mq_cont);
594 * Process the new channel notification and add it to the channels in the handle
596 * @param h The cadet handle
597 * @param msg A message with the details of the new incoming channel
600 handle_channel_created (void *cls,
601 const struct GNUNET_CADET_LocalChannelCreateMessage *msg)
603 struct GNUNET_CADET_Handle *h = cls;
604 struct GNUNET_CADET_Channel *ch;
605 struct GNUNET_CADET_Port *port;
606 const struct GNUNET_HashCode *port_number;
607 struct GNUNET_CADET_ClientChannelNumber ccn;
610 port_number = &msg->port;
611 if (ntohl (ccn.channel_of_client) >= GNUNET_CADET_LOCAL_CHANNEL_ID_CLI)
620 /* We could have closed the port but the service didn't know about it yet
621 * This is not an error.
623 struct GNUNET_CADET_LocalChannelDestroyMessage *d_msg;
624 struct GNUNET_MQ_Envelope *env;
626 LOG (GNUNET_ERROR_TYPE_DEBUG,
627 "No handler for incoming channel %X (on port %s, recently closed?)\n",
628 ntohl (ccn.channel_of_client),
629 GNUNET_h2s (port_number));
630 env = GNUNET_MQ_msg (d_msg,
631 GNUNET_MESSAGE_TYPE_CADET_LOCAL_CHANNEL_DESTROY);
632 d_msg->ccn = msg->ccn;
633 GNUNET_MQ_send (h->mq,
638 ch = create_channel (h,
640 ch->peer = msg->peer;
641 ch->incoming_port = port;
642 ch->options = ntohl (msg->opt);
643 LOG (GNUNET_ERROR_TYPE_DEBUG,
644 "Creating incoming channel %X [%s] %p\n",
645 ntohl (ccn.channel_of_client),
646 GNUNET_h2s (port_number),
649 GNUNET_assert (NULL != port->connects);
650 ch->window_changes = port->window_changes;
651 ch->disconnects = port->disconnects;
652 ch->mq = GNUNET_MQ_queue_for_callbacks (&cadet_mq_send_impl,
653 &cadet_mq_destroy_impl,
654 &cadet_mq_cancel_impl,
657 &cadet_mq_error_handler,
659 ch->ctx = port->connects (port->cls,
662 GNUNET_MQ_set_handlers_closure (ch->mq,
668 * Process the channel destroy notification and free associated resources
670 * @param h The cadet handle
671 * @param msg A message with the details of the channel being destroyed
674 handle_channel_destroy (void *cls,
675 const struct GNUNET_CADET_LocalChannelDestroyMessage *msg)
677 struct GNUNET_CADET_Handle *h = cls;
678 struct GNUNET_CADET_Channel *ch;
680 ch = find_channel (h,
684 LOG (GNUNET_ERROR_TYPE_DEBUG,
685 "Received channel destroy for unknown channel %X from CADET service (recently close?)\n",
686 ntohl (msg->ccn.channel_of_client));
689 LOG (GNUNET_ERROR_TYPE_DEBUG,
690 "Received channel destroy for channel %X from CADET service\n",
691 ntohl (msg->ccn.channel_of_client));
692 destroy_channel (ch);
697 * Check that message received from CADET service is well-formed.
699 * @param cls the `struct GNUNET_CADET_Handle`
700 * @param message the message we got
701 * @return #GNUNET_OK if the message is well-formed,
702 * #GNUNET_SYSERR otherwise
705 check_local_data (void *cls,
706 const struct GNUNET_CADET_LocalData *message)
710 size = ntohs (message->header.size);
711 if (sizeof (*message) + sizeof (struct GNUNET_MessageHeader) > size)
714 return GNUNET_SYSERR;
721 * Process the incoming data packets, call appropriate handlers.
723 * @param h The cadet handle
724 * @param message A message encapsulating the data
727 handle_local_data (void *cls,
728 const struct GNUNET_CADET_LocalData *message)
730 struct GNUNET_CADET_Handle *h = cls;
731 const struct GNUNET_MessageHeader *payload;
732 struct GNUNET_CADET_Channel *ch;
736 ch = find_channel (h,
740 LOG (GNUNET_ERROR_TYPE_DEBUG,
741 "Unknown channel %X for incoming data (recently closed?)\n",
742 ntohl (message->ccn.channel_of_client));
746 payload = (const struct GNUNET_MessageHeader *) &message[1];
747 type = ntohs (payload->type);
748 fwd = ntohl (ch->ccn.channel_of_client) <= GNUNET_CADET_LOCAL_CHANNEL_ID_CLI;
749 LOG (GNUNET_ERROR_TYPE_DEBUG,
750 "Got a %s data on channel %s [%X] of type %u\n",
752 GNUNET_i2s (&ch->peer),
753 ntohl (message->ccn.channel_of_client),
755 GNUNET_MQ_inject_message (ch->mq,
761 * Process a local ACK message, enabling the client to send
762 * more data to the service.
764 * @param h Cadet handle.
765 * @param message Message itself.
768 handle_local_ack (void *cls,
769 const struct GNUNET_CADET_LocalAck *message)
771 struct GNUNET_CADET_Handle *h = cls;
772 struct GNUNET_CADET_Channel *ch;
774 ch = find_channel (h,
778 LOG (GNUNET_ERROR_TYPE_DEBUG,
779 "ACK on unknown channel %X\n",
780 ntohl (message->ccn.channel_of_client));
784 if (NULL == ch->pending_env)
786 LOG (GNUNET_ERROR_TYPE_DEBUG,
787 "Got an ACK on mq channel %X, allow send now %u!\n",
788 ntohl (ch->ccn.channel_of_client),
790 notify_window_size (ch);
793 if (NULL != ch->mq_cont)
794 return; /* already working on it! */
795 LOG (GNUNET_ERROR_TYPE_DEBUG,
796 "Got an ACK on mq channel %X, sending pending message!\n",
797 ntohl (ch->ccn.channel_of_client));
799 = GNUNET_SCHEDULER_add_now (&cadet_mq_send_now,
805 * Generic error handler, called with the appropriate error code and
806 * the same closure specified at the creation of the message queue.
807 * Not every message queue implementation supports an error handler.
809 * @param cls closure, a `struct GNUNET_CORE_Handle *`
810 * @param error error code
813 handle_mq_error (void *cls,
814 enum GNUNET_MQ_Error error)
816 struct GNUNET_CADET_Handle *h = cls;
818 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
821 GNUNET_MQ_destroy (h->mq);
828 * Check that message received from CADET service is well-formed.
830 * @param cls the `struct GNUNET_CADET_Handle`
831 * @param message the message we got
832 * @return #GNUNET_OK if the message is well-formed,
833 * #GNUNET_SYSERR otherwise
836 check_get_peers (void *cls,
837 const struct GNUNET_MessageHeader *message)
841 esize = ntohs (message->size);
842 if (sizeof (struct GNUNET_CADET_LocalInfoPeer) == esize)
844 if (sizeof (struct GNUNET_MessageHeader) == esize)
846 return GNUNET_SYSERR;
851 * Process a local reply about info on all tunnels, pass info to the user.
853 * @param cls Closure (Cadet handle).
854 * @param msg Message itself.
857 handle_get_peers (void *cls,
858 const struct GNUNET_MessageHeader *msg)
860 struct GNUNET_CADET_Handle *h = cls;
861 const struct GNUNET_CADET_LocalInfoPeer *info =
862 (const struct GNUNET_CADET_LocalInfoPeer *) msg;
864 if (NULL == h->info_cb.peers_cb)
866 if (sizeof (struct GNUNET_CADET_LocalInfoPeer) == ntohs (msg->size))
867 h->info_cb.peers_cb (h->info_cls,
869 (int) ntohs (info->tunnel),
870 (unsigned int) ntohs (info->paths),
873 h->info_cb.peers_cb (h->info_cls,
882 * Check that message received from CADET service is well-formed.
884 * @param cls the `struct GNUNET_CADET_Handle`
885 * @param message the message we got
886 * @return #GNUNET_OK if the message is well-formed,
887 * #GNUNET_SYSERR otherwise
890 check_get_peer (void *cls,
891 const struct GNUNET_CADET_LocalInfoPeer *message)
893 size_t msize = sizeof (struct GNUNET_CADET_LocalInfoPeer);
894 const struct GNUNET_PeerIdentity *paths_array;
900 esize = ntohs (message->header.size);
904 return GNUNET_SYSERR;
906 if (0 != ((esize - msize) % sizeof (struct GNUNET_PeerIdentity)))
909 return GNUNET_SYSERR;
911 peers = (esize - msize) / sizeof (struct GNUNET_PeerIdentity);
912 epaths = ntohs (message->paths);
913 paths_array = (const struct GNUNET_PeerIdentity *) &message[1];
915 for (unsigned int i = 0; i < peers; i++)
916 if (0 == memcmp (&paths_array[i],
917 &message->destination,
918 sizeof (struct GNUNET_PeerIdentity)))
923 return GNUNET_SYSERR;
930 * Process a local peer info reply, pass info to the user.
932 * @param cls Closure (Cadet handle).
933 * @param message Message itself.
936 handle_get_peer (void *cls,
937 const struct GNUNET_CADET_LocalInfoPeer *message)
939 struct GNUNET_CADET_Handle *h = cls;
940 const struct GNUNET_PeerIdentity *paths_array;
942 unsigned int path_length;
946 if (NULL == h->info_cb.peer_cb)
948 paths = ntohs (message->paths);
949 paths_array = (const struct GNUNET_PeerIdentity *) &message[1];
950 peers = (ntohs (message->header.size) - sizeof (*message))
951 / sizeof (struct GNUNET_PeerIdentity);
953 neighbor = GNUNET_NO;
955 for (unsigned int i = 0; i < peers; i++)
957 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
959 GNUNET_i2s (&paths_array[i]));
961 if (0 == memcmp (&paths_array[i], &message->destination,
962 sizeof (struct GNUNET_PeerIdentity)))
964 if (1 == path_length)
965 neighbor = GNUNET_YES;
970 /* Call Callback with tunnel info. */
971 paths_array = (const struct GNUNET_PeerIdentity *) &message[1];
972 h->info_cb.peer_cb (h->info_cls,
973 &message->destination,
974 (int) ntohs (message->tunnel),
982 * Check that message received from CADET service is well-formed.
984 * @param cls the `struct GNUNET_CADET_Handle`
985 * @param message the message we got
986 * @return #GNUNET_OK if the message is well-formed,
987 * #GNUNET_SYSERR otherwise
990 check_get_tunnels (void *cls,
991 const struct GNUNET_MessageHeader *message)
995 esize = ntohs (message->size);
996 if (sizeof (struct GNUNET_CADET_LocalInfoTunnel) == esize)
998 if (sizeof (struct GNUNET_MessageHeader) == esize)
1000 return GNUNET_SYSERR;
1005 * Process a local reply about info on all tunnels, pass info to the user.
1007 * @param cls Closure (Cadet handle).
1008 * @param message Message itself.
1011 handle_get_tunnels (void *cls,
1012 const struct GNUNET_MessageHeader *msg)
1014 struct GNUNET_CADET_Handle *h = cls;
1015 const struct GNUNET_CADET_LocalInfoTunnel *info =
1016 (const struct GNUNET_CADET_LocalInfoTunnel *) msg;
1018 if (NULL == h->info_cb.tunnels_cb)
1020 if (sizeof (struct GNUNET_CADET_LocalInfoTunnel) == ntohs (msg->size))
1021 h->info_cb.tunnels_cb (h->info_cls,
1023 ntohl (info->channels),
1024 ntohl (info->connections),
1025 ntohs (info->estate),
1026 ntohs (info->cstate));
1028 h->info_cb.tunnels_cb (h->info_cls,
1038 * Check that message received from CADET service is well-formed.
1040 * @param cls the `struct GNUNET_CADET_Handle`
1041 * @param msg the message we got
1042 * @return #GNUNET_OK if the message is well-formed,
1043 * #GNUNET_SYSERR otherwise
1046 check_get_tunnel (void *cls,
1047 const struct GNUNET_CADET_LocalInfoTunnel *msg)
1054 /* Verify message sanity */
1055 msize = ntohs (msg->header.size);
1056 esize = sizeof (struct GNUNET_CADET_LocalInfoTunnel);
1060 return GNUNET_SYSERR;
1062 ch_n = ntohl (msg->channels);
1063 c_n = ntohl (msg->connections);
1064 esize += ch_n * sizeof (struct GNUNET_CADET_ChannelTunnelNumber);
1065 esize += c_n * sizeof (struct GNUNET_CADET_ConnectionTunnelIdentifier);
1068 GNUNET_break_op (0);
1069 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1070 "m:%u, e: %u (%u ch, %u conn)\n",
1071 (unsigned int) msize,
1072 (unsigned int) esize,
1075 return GNUNET_SYSERR;
1082 * Process a local tunnel info reply, pass info to the user.
1084 * @param cls Closure (Cadet handle).
1085 * @param msg Message itself.
1088 handle_get_tunnel (void *cls,
1089 const struct GNUNET_CADET_LocalInfoTunnel *msg)
1091 struct GNUNET_CADET_Handle *h = cls;
1094 const struct GNUNET_CADET_ConnectionTunnelIdentifier *conns;
1095 const struct GNUNET_CADET_ChannelTunnelNumber *chns;
1097 if (NULL == h->info_cb.tunnel_cb)
1100 ch_n = ntohl (msg->channels);
1101 c_n = ntohl (msg->connections);
1103 /* Call Callback with tunnel info. */
1104 conns = (const struct GNUNET_CADET_ConnectionTunnelIdentifier *) &msg[1];
1105 chns = (const struct GNUNET_CADET_ChannelTunnelNumber *) &conns[c_n];
1106 h->info_cb.tunnel_cb (h->info_cls,
1112 ntohs (msg->estate),
1113 ntohs (msg->cstate));
1118 * Reconnect to the service, retransmit all infomation to try to restore the
1121 * @param h handle to the cadet
1124 reconnect (struct GNUNET_CADET_Handle *h)
1126 struct GNUNET_MQ_MessageHandler handlers[] = {
1127 GNUNET_MQ_hd_fixed_size (channel_created,
1128 GNUNET_MESSAGE_TYPE_CADET_LOCAL_CHANNEL_CREATE,
1129 struct GNUNET_CADET_LocalChannelCreateMessage,
1131 GNUNET_MQ_hd_fixed_size (channel_destroy,
1132 GNUNET_MESSAGE_TYPE_CADET_LOCAL_CHANNEL_DESTROY,
1133 struct GNUNET_CADET_LocalChannelDestroyMessage,
1135 GNUNET_MQ_hd_var_size (local_data,
1136 GNUNET_MESSAGE_TYPE_CADET_LOCAL_DATA,
1137 struct GNUNET_CADET_LocalData,
1139 GNUNET_MQ_hd_fixed_size (local_ack,
1140 GNUNET_MESSAGE_TYPE_CADET_LOCAL_ACK,
1141 struct GNUNET_CADET_LocalAck,
1143 GNUNET_MQ_hd_var_size (get_peers,
1144 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEERS,
1145 struct GNUNET_MessageHeader,
1147 GNUNET_MQ_hd_var_size (get_peer,
1148 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEER,
1149 struct GNUNET_CADET_LocalInfoPeer,
1151 GNUNET_MQ_hd_var_size (get_tunnels,
1152 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNELS,
1153 struct GNUNET_MessageHeader,
1155 GNUNET_MQ_hd_var_size (get_tunnel,
1156 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNEL,
1157 struct GNUNET_CADET_LocalInfoTunnel,
1159 GNUNET_MQ_handler_end ()
1162 GNUNET_assert (NULL == h->mq);
1163 h->mq = GNUNET_CLIENT_connect (h->cfg,
1170 schedule_reconnect (h);
1173 h->reconnect_time = GNUNET_TIME_UNIT_MILLISECONDS;
1178 * Function called during #GNUNET_CADET_disconnect() to destroy
1179 * all channels that are still open.
1181 * @param cls the `struct GNUNET_CADET_Handle`
1182 * @param cid chanenl ID
1183 * @param value a `struct GNUNET_CADET_Channel` to destroy
1184 * @return #GNUNET_OK (continue to iterate)
1187 destroy_channel_cb (void *cls,
1191 /* struct GNUNET_CADET_Handle *handle = cls; */
1192 struct GNUNET_CADET_Channel *ch = value;
1194 if (ntohl (ch->ccn.channel_of_client) >= GNUNET_CADET_LOCAL_CHANNEL_ID_CLI)
1197 LOG (GNUNET_ERROR_TYPE_DEBUG,
1198 "channel %X not destroyed\n",
1199 ntohl (ch->ccn.channel_of_client));
1201 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1202 "Destroying channel due to GNUNET_CADET_disconnect()\n");
1203 destroy_channel (ch);
1209 * Function called during #GNUNET_CADET_disconnect() to destroy
1210 * all ports that are still open.
1212 * @param cls the `struct GNUNET_CADET_Handle`
1214 * @param value a `struct GNUNET_CADET_Channel` to destroy
1215 * @return #GNUNET_OK (continue to iterate)
1218 destroy_port_cb (void *cls,
1219 const struct GNUNET_HashCode *id,
1222 /* struct GNUNET_CADET_Handle *handle = cls; */
1223 struct GNUNET_CADET_Port *port = value;
1225 /* This is a warning, the app should have cleanly closed all open ports */
1227 GNUNET_CADET_close_port (port);
1233 * Disconnect from the cadet service. All channels will be destroyed. All channel
1234 * disconnect callbacks will be called on any still connected peers, notifying
1235 * about their disconnection. The registered inbound channel cleaner will be
1236 * called should any inbound channels still exist.
1238 * @param handle connection to cadet to disconnect
1241 GNUNET_CADET_disconnect (struct GNUNET_CADET_Handle *handle)
1243 GNUNET_CONTAINER_multihashmap_iterate (handle->ports,
1246 GNUNET_CONTAINER_multihashmap_destroy (handle->ports);
1247 handle->ports = NULL;
1248 GNUNET_CONTAINER_multihashmap32_iterate (handle->channels,
1249 &destroy_channel_cb,
1251 GNUNET_CONTAINER_multihashmap32_destroy (handle->channels);
1252 handle->channels = NULL;
1253 if (NULL != handle->mq)
1255 GNUNET_MQ_destroy (handle->mq);
1258 if (NULL != handle->reconnect_task)
1260 GNUNET_SCHEDULER_cancel (handle->reconnect_task);
1261 handle->reconnect_task = NULL;
1263 GNUNET_free (handle);
1268 * Close a port opened with @a GNUNET_CADET_open_port().
1269 * The @a new_channel callback will no longer be called.
1271 * @param p Port handle.
1274 GNUNET_CADET_close_port (struct GNUNET_CADET_Port *p)
1276 struct GNUNET_CADET_PortMessage *msg;
1277 struct GNUNET_MQ_Envelope *env;
1279 GNUNET_assert (GNUNET_YES ==
1280 GNUNET_CONTAINER_multihashmap_remove (p->cadet->ports,
1283 env = GNUNET_MQ_msg (msg,
1284 GNUNET_MESSAGE_TYPE_CADET_LOCAL_PORT_CLOSE);
1286 GNUNET_MQ_send (p->cadet->mq,
1288 GNUNET_free_non_null (p->handlers);
1294 * Destroy an existing channel.
1296 * The existing end callback for the channel will be called immediately.
1297 * Any pending outgoing messages will be sent but no incoming messages will be
1298 * accepted and no data callbacks will be called.
1300 * @param channel Channel handle, becomes invalid after this call.
1303 GNUNET_CADET_channel_destroy (struct GNUNET_CADET_Channel *channel)
1305 struct GNUNET_CADET_Handle *h = channel->cadet;
1306 struct GNUNET_CADET_LocalChannelDestroyMessage *msg;
1307 struct GNUNET_MQ_Envelope *env;
1311 env = GNUNET_MQ_msg (msg,
1312 GNUNET_MESSAGE_TYPE_CADET_LOCAL_CHANNEL_DESTROY);
1313 msg->ccn = channel->ccn;
1314 GNUNET_MQ_send (h->mq,
1317 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1318 "Destroying channel due to GNUNET_CADET_channel_destroy()\n");
1319 destroy_channel (channel);
1324 * Get information about a channel.
1326 * @param channel Channel handle.
1327 * @param option Query (GNUNET_CADET_OPTION_*).
1328 * @param ... dependant on option, currently not used
1330 * @return Union with an answer to the query.
1332 const union GNUNET_CADET_ChannelInfo *
1333 GNUNET_CADET_channel_get_info (struct GNUNET_CADET_Channel *channel,
1334 enum GNUNET_CADET_ChannelOption option,
1337 static int bool_flag;
1341 case GNUNET_CADET_OPTION_NOBUFFER:
1342 case GNUNET_CADET_OPTION_RELIABLE:
1343 case GNUNET_CADET_OPTION_OUT_OF_ORDER:
1344 if (0 != (option & channel->options))
1345 bool_flag = GNUNET_YES;
1347 bool_flag = GNUNET_NO;
1348 return (const union GNUNET_CADET_ChannelInfo *) &bool_flag;
1350 case GNUNET_CADET_OPTION_PEER:
1351 return (const union GNUNET_CADET_ChannelInfo *) &channel->peer;
1361 * Send an ack on the channel to confirm the processing of a message.
1363 * @param ch Channel on which to send the ACK.
1366 GNUNET_CADET_receive_done (struct GNUNET_CADET_Channel *channel)
1368 struct GNUNET_CADET_LocalAck *msg;
1369 struct GNUNET_MQ_Envelope *env;
1371 env = GNUNET_MQ_msg (msg,
1372 GNUNET_MESSAGE_TYPE_CADET_LOCAL_ACK);
1373 LOG (GNUNET_ERROR_TYPE_DEBUG,
1374 "Sending ACK on channel %X\n",
1375 ntohl (channel->ccn.channel_of_client));
1376 msg->ccn = channel->ccn;
1377 GNUNET_MQ_send (channel->cadet->mq,
1383 * Send message of @a type to CADET service of @a h
1385 * @param h handle to CADET service
1386 * @param type message type of trivial information request to send
1389 send_info_request (struct GNUNET_CADET_Handle *h,
1392 struct GNUNET_MessageHeader *msg;
1393 struct GNUNET_MQ_Envelope *env;
1395 env = GNUNET_MQ_msg (msg,
1397 GNUNET_MQ_send (h->mq,
1403 * Request a debug dump on the service's STDERR.
1405 * WARNING: unstable API, likely to change in the future!
1407 * @param h cadet handle
1410 GNUNET_CADET_request_dump (struct GNUNET_CADET_Handle *h)
1412 send_info_request (h,
1413 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_DUMP);
1418 * Request information about peers known to the running cadet service.
1419 * The callback will be called for every peer known to the service.
1420 * Only one info request (of any kind) can be active at once.
1422 * WARNING: unstable API, likely to change in the future!
1424 * @param h Handle to the cadet peer.
1425 * @param callback Function to call with the requested data.
1426 * @param callback_cls Closure for @c callback.
1427 * @return #GNUNET_OK / #GNUNET_SYSERR
1430 GNUNET_CADET_get_peers (struct GNUNET_CADET_Handle *h,
1431 GNUNET_CADET_PeersCB callback,
1434 if (NULL != h->info_cb.peers_cb)
1437 return GNUNET_SYSERR;
1439 send_info_request (h,
1440 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEERS);
1441 h->info_cb.peers_cb = callback;
1442 h->info_cls = callback_cls;
1448 * Cancel a peer info request. The callback will not be called (anymore).
1450 * WARNING: unstable API, likely to change in the future!
1452 * @param h Cadet handle.
1453 * @return Closure given to GNUNET_CADET_get_peers().
1456 GNUNET_CADET_get_peers_cancel (struct GNUNET_CADET_Handle *h)
1458 void *cls = h->info_cls;
1460 h->info_cb.peers_cb = NULL;
1467 * Request information about a peer known to the running cadet peer.
1468 * The callback will be called for the tunnel once.
1469 * Only one info request (of any kind) can be active at once.
1471 * WARNING: unstable API, likely to change in the future!
1473 * @param h Handle to the cadet peer.
1474 * @param id Peer whose tunnel to examine.
1475 * @param callback Function to call with the requested data.
1476 * @param callback_cls Closure for @c callback.
1477 * @return #GNUNET_OK / #GNUNET_SYSERR
1480 GNUNET_CADET_get_peer (struct GNUNET_CADET_Handle *h,
1481 const struct GNUNET_PeerIdentity *id,
1482 GNUNET_CADET_PeerCB callback,
1485 struct GNUNET_CADET_LocalInfo *msg;
1486 struct GNUNET_MQ_Envelope *env;
1488 if (NULL != h->info_cb.peer_cb)
1491 return GNUNET_SYSERR;
1493 env = GNUNET_MQ_msg (msg,
1494 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEER);
1496 GNUNET_MQ_send (h->mq,
1498 h->info_cb.peer_cb = callback;
1499 h->info_cls = callback_cls;
1505 * Request information about tunnels of the running cadet peer.
1506 * The callback will be called for every tunnel of the service.
1507 * Only one info request (of any kind) can be active at once.
1509 * WARNING: unstable API, likely to change in the future!
1511 * @param h Handle to the cadet peer.
1512 * @param callback Function to call with the requested data.
1513 * @param callback_cls Closure for @c callback.
1514 * @return #GNUNET_OK / #GNUNET_SYSERR
1517 GNUNET_CADET_get_tunnels (struct GNUNET_CADET_Handle *h,
1518 GNUNET_CADET_TunnelsCB callback,
1521 if (NULL != h->info_cb.tunnels_cb)
1524 return GNUNET_SYSERR;
1526 send_info_request (h,
1527 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNELS);
1528 h->info_cb.tunnels_cb = callback;
1529 h->info_cls = callback_cls;
1535 * Cancel a monitor request. The monitor callback will not be called.
1537 * @param h Cadet handle.
1538 * @return Closure given to GNUNET_CADET_get_tunnels().
1541 GNUNET_CADET_get_tunnels_cancel (struct GNUNET_CADET_Handle *h)
1543 void *cls = h->info_cls;
1545 h->info_cb.tunnels_cb = NULL;
1552 * Request information about a tunnel of the running cadet peer.
1553 * The callback will be called for the tunnel once.
1554 * Only one info request (of any kind) can be active at once.
1556 * WARNING: unstable API, likely to change in the future!
1558 * @param h Handle to the cadet peer.
1559 * @param id Peer whose tunnel to examine.
1560 * @param callback Function to call with the requested data.
1561 * @param callback_cls Closure for @c callback.
1562 * @return #GNUNET_OK / #GNUNET_SYSERR
1565 GNUNET_CADET_get_tunnel (struct GNUNET_CADET_Handle *h,
1566 const struct GNUNET_PeerIdentity *id,
1567 GNUNET_CADET_TunnelCB callback,
1570 struct GNUNET_CADET_LocalInfo *msg;
1571 struct GNUNET_MQ_Envelope *env;
1573 if (NULL != h->info_cb.tunnel_cb)
1576 return GNUNET_SYSERR;
1578 env = GNUNET_MQ_msg (msg,
1579 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNEL);
1581 GNUNET_MQ_send (h->mq,
1583 h->info_cb.tunnel_cb = callback;
1584 h->info_cls = callback_cls;
1590 * Transitional function to convert an unsigned int port to a hash value.
1591 * WARNING: local static value returned, NOT reentrant!
1592 * WARNING: do not use this function for new code!
1594 * @param port Numerical port (unsigned int format).
1596 * @return A GNUNET_HashCode usable for the new CADET API.
1598 const struct GNUNET_HashCode *
1599 GC_u2h (uint32_t port)
1601 static struct GNUNET_HashCode hash;
1603 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1604 "This is a transitional function, use proper crypto hashes as CADET ports\n");
1605 GNUNET_CRYPTO_hash (&port,
1613 * Connect to the MQ-based cadet service.
1615 * @param cfg Configuration to use.
1617 * @return Handle to the cadet service NULL on error.
1619 struct GNUNET_CADET_Handle *
1620 GNUNET_CADET_connect (const struct GNUNET_CONFIGURATION_Handle *cfg)
1622 struct GNUNET_CADET_Handle *h;
1624 LOG (GNUNET_ERROR_TYPE_DEBUG,
1625 "GNUNET_CADET_connect()\n");
1626 h = GNUNET_new (struct GNUNET_CADET_Handle);
1628 h->ports = GNUNET_CONTAINER_multihashmap_create (4,
1630 h->channels = GNUNET_CONTAINER_multihashmap32_create (4);
1635 GNUNET_CADET_disconnect (h);
1638 h->next_ccn.channel_of_client = htonl (GNUNET_CADET_LOCAL_CHANNEL_ID_CLI);
1639 h->reconnect_time = GNUNET_TIME_UNIT_MILLISECONDS;
1640 h->reconnect_task = NULL;
1647 * Open a port to receive incomming MQ-based channels.
1649 * @param h CADET handle.
1650 * @param port Hash identifying the port.
1651 * @param connects Function called when an incoming channel is connected.
1652 * @param connects_cls Closure for the @a connects handler.
1653 * @param window_changes Function called when the transmit window size changes.
1654 * @param disconnects Function called when a channel is disconnected.
1655 * @param handlers Callbacks for messages we care about, NULL-terminated.
1656 * @return Port handle, NULL if port is in use
1658 struct GNUNET_CADET_Port *
1659 GNUNET_CADET_open_port (struct GNUNET_CADET_Handle *h,
1660 const struct GNUNET_HashCode *port,
1661 GNUNET_CADET_ConnectEventHandler connects,
1662 void * connects_cls,
1663 GNUNET_CADET_WindowSizeEventHandler window_changes,
1664 GNUNET_CADET_DisconnectEventHandler disconnects,
1665 const struct GNUNET_MQ_MessageHandler *handlers)
1667 struct GNUNET_CADET_PortMessage *msg;
1668 struct GNUNET_MQ_Envelope *env;
1669 struct GNUNET_CADET_Port *p;
1671 GNUNET_assert (NULL != connects);
1672 GNUNET_assert (NULL != disconnects);
1673 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1674 "Listening to CADET port %s\n",
1677 p = GNUNET_new (struct GNUNET_CADET_Port);
1681 GNUNET_CONTAINER_multihashmap_put (h->ports,
1684 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
1689 p->connects = connects;
1690 p->cls = connects_cls;
1691 p->window_changes = window_changes;
1692 p->disconnects = disconnects;
1693 p->handlers = GNUNET_MQ_copy_handlers (handlers);
1696 env = GNUNET_MQ_msg (msg,
1697 GNUNET_MESSAGE_TYPE_CADET_LOCAL_PORT_OPEN);
1699 GNUNET_MQ_send (h->mq,
1706 * Create a new channel towards a remote peer.
1708 * If the destination port is not open by any peer or the destination peer
1709 * does not accept the channel, #GNUNET_CADET_ChannelEndHandler will be called
1712 * @param h CADET handle.
1713 * @param channel_cls Closure for the channel. It's given to:
1714 * - The disconnect handler @a disconnects
1715 * - Each message type callback in @a handlers
1716 * @param destination Peer identity the channel should go to.
1717 * @param port Identification of the destination port.
1718 * @param options CadetOption flag field, with all desired option bits set to 1.
1719 * @param window_changes Function called when the transmit window size changes.
1720 * @param disconnects Function called when the channel is disconnected.
1721 * @param handlers Callbacks for messages we care about, NULL-terminated.
1722 * @return Handle to the channel.
1724 struct GNUNET_CADET_Channel *
1725 GNUNET_CADET_channel_create (struct GNUNET_CADET_Handle *h,
1727 const struct GNUNET_PeerIdentity *destination,
1728 const struct GNUNET_HashCode *port,
1729 enum GNUNET_CADET_ChannelOption options,
1730 GNUNET_CADET_WindowSizeEventHandler window_changes,
1731 GNUNET_CADET_DisconnectEventHandler disconnects,
1732 const struct GNUNET_MQ_MessageHandler *handlers)
1734 struct GNUNET_CADET_Channel *ch;
1735 struct GNUNET_CADET_LocalChannelCreateMessage *msg;
1736 struct GNUNET_MQ_Envelope *env;
1738 GNUNET_assert (NULL != disconnects);
1739 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1740 "Creating channel to peer %s at port %s\n",
1741 GNUNET_i2s (destination),
1743 ch = create_channel (h,
1745 ch->ctx = channel_cls;
1746 ch->peer = *destination;
1747 ch->options = options;
1748 ch->window_changes = window_changes;
1749 ch->disconnects = disconnects;
1751 /* Create MQ for channel */
1752 ch->mq = GNUNET_MQ_queue_for_callbacks (&cadet_mq_send_impl,
1753 &cadet_mq_destroy_impl,
1754 &cadet_mq_cancel_impl,
1757 &cadet_mq_error_handler,
1759 GNUNET_MQ_set_handlers_closure (ch->mq, channel_cls);
1761 /* Request channel creation to service */
1762 env = GNUNET_MQ_msg (msg,
1763 GNUNET_MESSAGE_TYPE_CADET_LOCAL_CHANNEL_CREATE);
1766 msg->peer = *destination;
1767 msg->opt = htonl (options);
1768 GNUNET_MQ_send (h->mq,
1775 * Obtain the message queue for a connected peer.
1777 * @param channel The channel handle from which to get the MQ.
1779 * @return NULL if @a channel is not yet connected.
1781 struct GNUNET_MQ_Handle *
1782 GNUNET_CADET_get_mq (const struct GNUNET_CADET_Channel *channel)
1787 /* end of cadet_api.c */