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 it
6 under the terms of the GNU General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your 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 Affero General Public License for more details.
16 * @file cadet/cadet_api.c
17 * @brief cadet api: client implementation of cadet service
18 * @author Bartlomiej Polot
19 * @author Christian Grothoff
22 #include "gnunet_util_lib.h"
23 #include "gnunet_constants.h"
24 #include "gnunet_cadet_service.h"
26 #include "cadet_protocol.h"
28 #define LOG(kind,...) GNUNET_log_from (kind, "cadet-api",__VA_ARGS__)
39 GNUNET_CADET_ChannelCB channel_cb;
44 GNUNET_CADET_PeersCB peers_cb;
49 GNUNET_CADET_PeerCB peer_cb;
54 GNUNET_CADET_TunnelsCB tunnels_cb;
59 GNUNET_CADET_TunnelCB tunnel_cb;
64 * Opaque handle to the service.
66 struct GNUNET_CADET_Handle
71 struct GNUNET_MQ_Handle *mq;
76 struct GNUNET_CONTAINER_MultiHashMap *ports;
81 struct GNUNET_CONTAINER_MultiHashMap32 *channels;
84 * child of the next channel to create (to avoid reusing IDs often)
86 struct GNUNET_CADET_ClientChannelNumber next_ccn;
89 * Configuration given by the client, in case of reconnection
91 const struct GNUNET_CONFIGURATION_Handle *cfg;
94 * Task for trying to reconnect.
96 struct GNUNET_SCHEDULER_Task *reconnect_task;
99 * Callback for an info task (only one active at a time).
101 union CadetInfoCB info_cb;
104 * Info callback closure for @c info_cb.
109 * Time to the next reconnect in case one reconnect fails
111 struct GNUNET_TIME_Relative reconnect_time;
117 * Opaque handle to a channel.
119 struct GNUNET_CADET_Channel
123 * Other end of the channel.
125 struct GNUNET_PeerIdentity peer;
128 * Handle to the cadet this channel belongs to
130 struct GNUNET_CADET_Handle *cadet;
133 * Channel's port, if incoming.
135 struct GNUNET_CADET_Port *incoming_port;
138 * Any data the caller wants to put in here, used for the
139 * various callbacks (@e disconnects, @e window_changes, handlers).
144 * Message Queue for the channel (which we are implementing).
146 struct GNUNET_MQ_Handle *mq;
149 * Task to allow mq to send more traffic.
151 struct GNUNET_SCHEDULER_Task *mq_cont;
154 * Pending envelope with a message to be transmitted to the
155 * service as soon as we are allowed to. Should only be
156 * non-NULL if @e allow_send is 0.
158 struct GNUNET_MQ_Envelope *pending_env;
161 * Window change handler.
163 GNUNET_CADET_WindowSizeEventHandler window_changes;
166 * Disconnect handler.
168 GNUNET_CADET_DisconnectEventHandler disconnects;
171 * Local ID of the channel, #GNUNET_CADET_LOCAL_CHANNEL_ID_CLI bit is set if outbound.
173 struct GNUNET_CADET_ClientChannelNumber ccn;
176 * Channel options: reliability, etc.
178 enum GNUNET_CADET_ChannelOption options;
181 * How many messages are we allowed to send to the service right now?
183 unsigned int allow_send;
189 * Opaque handle to a port.
191 struct GNUNET_CADET_Port
197 struct GNUNET_HashCode id;
200 * Handle to the CADET session this port belongs to.
202 struct GNUNET_CADET_Handle *cadet;
205 * Closure for @a handler.
210 * Handler for incoming channels on this port
212 GNUNET_CADET_ConnectEventHandler connects;
215 * Closure for @ref connects
220 * Window size change handler.
222 GNUNET_CADET_WindowSizeEventHandler window_changes;
225 * Handler called when an incoming channel is destroyed.
227 GNUNET_CADET_DisconnectEventHandler disconnects;
230 * Payload handlers for incoming channels.
232 struct GNUNET_MQ_MessageHandler *handlers;
237 * Find the Port struct for a hash.
239 * @param h CADET handle.
240 * @param hash HashCode for the port number.
241 * @return The port handle if known, NULL otherwise.
243 static struct GNUNET_CADET_Port *
244 find_port (const struct GNUNET_CADET_Handle *h,
245 const struct GNUNET_HashCode *hash)
247 return GNUNET_CONTAINER_multihashmap_get (h->ports,
253 * Get the channel handler for the channel specified by id from the given handle
255 * @param h Cadet handle
256 * @param ccn ID of the wanted channel
257 * @return handle to the required channel or NULL if not found
259 static struct GNUNET_CADET_Channel *
260 find_channel (struct GNUNET_CADET_Handle *h,
261 struct GNUNET_CADET_ClientChannelNumber ccn)
263 return GNUNET_CONTAINER_multihashmap32_get (h->channels,
264 ntohl (ccn.channel_of_client));
269 * Create a new channel and insert it in the channel list of the cadet handle
271 * @param h Cadet handle
272 * @param ccnp pointer to desired ccn of the channel, NULL to assign one automatically.
273 * @return Handle to the created channel.
275 static struct GNUNET_CADET_Channel *
276 create_channel (struct GNUNET_CADET_Handle *h,
277 const struct GNUNET_CADET_ClientChannelNumber *ccnp)
279 struct GNUNET_CADET_Channel *ch;
280 struct GNUNET_CADET_ClientChannelNumber ccn;
282 ch = GNUNET_new (struct GNUNET_CADET_Channel);
289 h->next_ccn.channel_of_client
290 = htonl (GNUNET_CADET_LOCAL_CHANNEL_ID_CLI | (1 + ntohl (h->next_ccn.channel_of_client)));
298 GNUNET_assert (GNUNET_OK ==
299 GNUNET_CONTAINER_multihashmap32_put (h->channels,
300 ntohl (ch->ccn.channel_of_client),
302 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
308 * Destroy the specified channel.
309 * - Destroys all peers, calling the disconnect callback on each if needed
310 * - Cancels all outgoing traffic for that channel, calling respective notifys
311 * - Calls cleaner if channel was inbound
312 * - Frees all memory used
314 * @param ch Pointer to the channel.
315 * @param call_cleaner Whether to call the cleaner handler.
318 destroy_channel (struct GNUNET_CADET_Channel *ch)
320 struct GNUNET_CADET_Handle *h = ch->cadet;
322 LOG (GNUNET_ERROR_TYPE_DEBUG,
323 "Destroying channel %X of %p\n",
324 htonl (ch->ccn.channel_of_client),
326 GNUNET_assert (GNUNET_YES ==
327 GNUNET_CONTAINER_multihashmap32_remove (h->channels,
328 ntohl (ch->ccn.channel_of_client),
330 if (NULL != ch->mq_cont)
332 GNUNET_SCHEDULER_cancel (ch->mq_cont);
335 /* signal channel destruction */
336 if (NULL != ch->disconnects)
337 ch->disconnects (ch->ctx,
339 if (NULL != ch->pending_env)
340 GNUNET_MQ_discard (ch->pending_env);
341 GNUNET_MQ_destroy (ch->mq);
347 * Reconnect to the service, retransmit all infomation to try to restore the
350 * @param h handle to the cadet
353 reconnect (struct GNUNET_CADET_Handle *h);
357 * Reconnect callback: tries to reconnect again after a failer previous
360 * @param cls closure (cadet handle)
363 reconnect_cbk (void *cls)
365 struct GNUNET_CADET_Handle *h = cls;
367 h->reconnect_task = NULL;
373 * Function called during #reconnect() to destroy
374 * all channels that are still open.
376 * @param cls the `struct GNUNET_CADET_Handle`
377 * @param cid chanenl ID
378 * @param value a `struct GNUNET_CADET_Channel` to destroy
379 * @return #GNUNET_OK (continue to iterate)
382 destroy_channel_on_reconnect_cb (void *cls,
386 /* struct GNUNET_CADET_Handle *handle = cls; */
387 struct GNUNET_CADET_Channel *ch = value;
389 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
390 "Destroying channel due to reconnect\n");
391 destroy_channel (ch);
397 * Reconnect to the service, retransmit all infomation to try to restore the
400 * @param h handle to the cadet
402 * @return #GNUNET_YES in case of sucess, #GNUNET_NO otherwise (service down...)
405 schedule_reconnect (struct GNUNET_CADET_Handle *h)
407 if (NULL != h->reconnect_task)
409 GNUNET_CONTAINER_multihashmap32_iterate (h->channels,
410 &destroy_channel_on_reconnect_cb,
413 = GNUNET_SCHEDULER_add_delayed (h->reconnect_time,
417 = GNUNET_TIME_STD_BACKOFF (h->reconnect_time);
422 * Notify the application about a change in the window size (if needed).
424 * @param ch Channel to notify about.
427 notify_window_size (struct GNUNET_CADET_Channel *ch)
429 if (NULL != ch->window_changes)
430 ch->window_changes (ch->ctx,
431 ch, /* FIXME: remove 'ch'? */
437 * Transmit the next message from our queue.
439 * @param cls Closure (channel whose mq to activate).
442 cadet_mq_send_now (void *cls)
444 struct GNUNET_CADET_Channel *ch = cls;
445 struct GNUNET_MQ_Envelope *env = ch->pending_env;
448 if (0 == ch->allow_send)
450 /* how did we get here? */
456 /* how did we get here? */
461 ch->pending_env = NULL;
462 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
463 "Sending message on channel %s to CADET, new window size is %u\n",
464 GNUNET_i2s (&ch->peer),
466 GNUNET_MQ_send (ch->cadet->mq,
468 GNUNET_MQ_impl_send_continue (ch->mq);
473 * Implement sending functionality of a message queue for
474 * us sending messages to a peer.
476 * Encapsulates the payload message in a #GNUNET_CADET_LocalData message
477 * in order to label the message with the channel ID and send the
478 * encapsulated message to the service.
480 * @param mq the message queue
481 * @param msg the message to send
482 * @param impl_state state of the implementation
485 cadet_mq_send_impl (struct GNUNET_MQ_Handle *mq,
486 const struct GNUNET_MessageHeader *msg,
489 struct GNUNET_CADET_Channel *ch = impl_state;
490 struct GNUNET_CADET_Handle *h = ch->cadet;
492 struct GNUNET_MQ_Envelope *env;
493 struct GNUNET_CADET_LocalData *cadet_msg = NULL;
497 /* We're currently reconnecting, pretend this worked */
498 GNUNET_MQ_impl_send_continue (mq);
502 /* check message size for sanity */
503 msize = ntohs (msg->size);
504 if (msize > GNUNET_CONSTANTS_MAX_CADET_MESSAGE_SIZE)
507 GNUNET_MQ_impl_send_continue (mq);
510 env = GNUNET_MQ_msg_nested_mh (cadet_msg,
511 GNUNET_MESSAGE_TYPE_CADET_LOCAL_DATA,
513 cadet_msg->ccn = ch->ccn;
514 GNUNET_assert (NULL == ch->pending_env);
515 ch->pending_env = env;
516 if (0 < ch->allow_send)
518 = GNUNET_SCHEDULER_add_now (&cadet_mq_send_now,
524 * Handle destruction of a message queue. Implementations must not
525 * free @a mq, but should take care of @a impl_state.
527 * @param mq the message queue to destroy
528 * @param impl_state state of the implementation
531 cadet_mq_destroy_impl (struct GNUNET_MQ_Handle *mq,
534 struct GNUNET_CADET_Channel *ch = impl_state;
536 GNUNET_assert (mq == ch->mq);
542 * We had an error processing a message we forwarded from a peer to
543 * the CADET service. We should just complain about it but otherwise
544 * continue processing.
546 * @param cls closure with our `struct GNUNET_CADET_Channel`
547 * @param error error code
550 cadet_mq_error_handler (void *cls,
551 enum GNUNET_MQ_Error error)
553 struct GNUNET_CADET_Channel *ch = cls;
556 if (GNUNET_MQ_ERROR_NO_MATCH == error)
558 /* Got a message we did not understand, still try to continue! */
559 GNUNET_CADET_receive_done (ch);
563 schedule_reconnect (ch->cadet);
569 * Implementation function that cancels the currently sent message.
570 * Should basically undo whatever #mq_send_impl() did.
572 * @param mq message queue
573 * @param impl_state state specific to the implementation
576 cadet_mq_cancel_impl (struct GNUNET_MQ_Handle *mq,
579 struct GNUNET_CADET_Channel *ch = impl_state;
581 GNUNET_assert (NULL != ch->pending_env);
582 GNUNET_MQ_discard (ch->pending_env);
583 ch->pending_env = NULL;
584 if (NULL != ch->mq_cont)
586 GNUNET_SCHEDULER_cancel (ch->mq_cont);
593 * Process the new channel notification and add it to the channels in the handle
595 * @param h The cadet handle
596 * @param msg A message with the details of the new incoming channel
599 handle_channel_created (void *cls,
600 const struct GNUNET_CADET_LocalChannelCreateMessage *msg)
602 struct GNUNET_CADET_Handle *h = cls;
603 struct GNUNET_CADET_Channel *ch;
604 struct GNUNET_CADET_Port *port;
605 const struct GNUNET_HashCode *port_number;
606 struct GNUNET_CADET_ClientChannelNumber ccn;
609 port_number = &msg->port;
610 if (ntohl (ccn.channel_of_client) >= GNUNET_CADET_LOCAL_CHANNEL_ID_CLI)
619 /* We could have closed the port but the service didn't know about it yet
620 * This is not an error.
622 struct GNUNET_CADET_LocalChannelDestroyMessage *d_msg;
623 struct GNUNET_MQ_Envelope *env;
625 LOG (GNUNET_ERROR_TYPE_DEBUG,
626 "No handler for incoming channel %X (on port %s, recently closed?)\n",
627 ntohl (ccn.channel_of_client),
628 GNUNET_h2s (port_number));
629 env = GNUNET_MQ_msg (d_msg,
630 GNUNET_MESSAGE_TYPE_CADET_LOCAL_CHANNEL_DESTROY);
631 d_msg->ccn = msg->ccn;
632 GNUNET_MQ_send (h->mq,
637 ch = create_channel (h,
639 ch->peer = msg->peer;
640 ch->incoming_port = port;
641 ch->options = ntohl (msg->opt);
642 LOG (GNUNET_ERROR_TYPE_DEBUG,
643 "Creating incoming channel %X [%s] %p\n",
644 ntohl (ccn.channel_of_client),
645 GNUNET_h2s (port_number),
648 GNUNET_assert (NULL != port->connects);
649 ch->window_changes = port->window_changes;
650 ch->disconnects = port->disconnects;
651 ch->mq = GNUNET_MQ_queue_for_callbacks (&cadet_mq_send_impl,
652 &cadet_mq_destroy_impl,
653 &cadet_mq_cancel_impl,
656 &cadet_mq_error_handler,
658 ch->ctx = port->connects (port->cls,
661 GNUNET_MQ_set_handlers_closure (ch->mq,
667 * Process the channel destroy notification and free associated resources
669 * @param h The cadet handle
670 * @param msg A message with the details of the channel being destroyed
673 handle_channel_destroy (void *cls,
674 const struct GNUNET_CADET_LocalChannelDestroyMessage *msg)
676 struct GNUNET_CADET_Handle *h = cls;
677 struct GNUNET_CADET_Channel *ch;
679 ch = find_channel (h,
683 LOG (GNUNET_ERROR_TYPE_DEBUG,
684 "Received channel destroy for unknown channel %X from CADET service (recently close?)\n",
685 ntohl (msg->ccn.channel_of_client));
688 LOG (GNUNET_ERROR_TYPE_DEBUG,
689 "Received channel destroy for channel %X from CADET service\n",
690 ntohl (msg->ccn.channel_of_client));
691 destroy_channel (ch);
696 * Check that message received from CADET service is well-formed.
698 * @param cls the `struct GNUNET_CADET_Handle`
699 * @param message the message we got
700 * @return #GNUNET_OK if the message is well-formed,
701 * #GNUNET_SYSERR otherwise
704 check_local_data (void *cls,
705 const struct GNUNET_CADET_LocalData *message)
709 size = ntohs (message->header.size);
710 if (sizeof (*message) + sizeof (struct GNUNET_MessageHeader) > size)
713 return GNUNET_SYSERR;
720 * Process the incoming data packets, call appropriate handlers.
722 * @param h The cadet handle
723 * @param message A message encapsulating the data
726 handle_local_data (void *cls,
727 const struct GNUNET_CADET_LocalData *message)
729 struct GNUNET_CADET_Handle *h = cls;
730 const struct GNUNET_MessageHeader *payload;
731 struct GNUNET_CADET_Channel *ch;
735 ch = find_channel (h,
739 LOG (GNUNET_ERROR_TYPE_DEBUG,
740 "Unknown channel %X for incoming data (recently closed?)\n",
741 ntohl (message->ccn.channel_of_client));
745 payload = (const struct GNUNET_MessageHeader *) &message[1];
746 type = ntohs (payload->type);
747 fwd = ntohl (ch->ccn.channel_of_client) <= GNUNET_CADET_LOCAL_CHANNEL_ID_CLI;
748 LOG (GNUNET_ERROR_TYPE_DEBUG,
749 "Got a %s data on channel %s [%X] of type %u\n",
751 GNUNET_i2s (&ch->peer),
752 ntohl (message->ccn.channel_of_client),
754 GNUNET_MQ_inject_message (ch->mq,
760 * Process a local ACK message, enabling the client to send
761 * more data to the service.
763 * @param h Cadet handle.
764 * @param message Message itself.
767 handle_local_ack (void *cls,
768 const struct GNUNET_CADET_LocalAck *message)
770 struct GNUNET_CADET_Handle *h = cls;
771 struct GNUNET_CADET_Channel *ch;
773 ch = find_channel (h,
777 LOG (GNUNET_ERROR_TYPE_DEBUG,
778 "ACK on unknown channel %X\n",
779 ntohl (message->ccn.channel_of_client));
783 LOG (GNUNET_ERROR_TYPE_DEBUG,
784 "Got an ACK on mq channel %X (peer %s); new window size is %u!\n",
785 ntohl (ch->ccn.channel_of_client),
786 GNUNET_i2s (&ch->peer),
788 if (NULL == ch->pending_env)
790 LOG (GNUNET_ERROR_TYPE_DEBUG,
791 "Got an ACK on mq channel %X, allow send now %u!\n",
792 ntohl (ch->ccn.channel_of_client),
794 notify_window_size (ch);
797 if (NULL != ch->mq_cont)
798 return; /* already working on it! */
800 = GNUNET_SCHEDULER_add_now (&cadet_mq_send_now,
806 * Generic error handler, called with the appropriate error code and
807 * the same closure specified at the creation of the message queue.
808 * Not every message queue implementation supports an error handler.
810 * @param cls closure, a `struct GNUNET_CORE_Handle *`
811 * @param error error code
814 handle_mq_error (void *cls,
815 enum GNUNET_MQ_Error error)
817 struct GNUNET_CADET_Handle *h = cls;
819 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
822 GNUNET_MQ_destroy (h->mq);
829 * Check that message received from CADET service is well-formed.
831 * @param cls the `struct GNUNET_CADET_Handle`
832 * @param message the message we got
833 * @return #GNUNET_OK if the message is well-formed,
834 * #GNUNET_SYSERR otherwise
837 check_get_peers (void *cls,
838 const struct GNUNET_MessageHeader *message)
842 esize = ntohs (message->size);
843 if (sizeof (struct GNUNET_CADET_LocalInfoPeer) == esize)
845 if (sizeof (struct GNUNET_MessageHeader) == esize)
847 return GNUNET_SYSERR;
852 * Process a local reply about info on all tunnels, pass info to the user.
854 * @param cls Closure (Cadet handle).
855 * @param msg Message itself.
858 handle_get_peers (void *cls,
859 const struct GNUNET_MessageHeader *msg)
861 struct GNUNET_CADET_Handle *h = cls;
862 const struct GNUNET_CADET_LocalInfoPeer *info =
863 (const struct GNUNET_CADET_LocalInfoPeer *) msg;
865 if (NULL == h->info_cb.peers_cb)
867 if (sizeof (struct GNUNET_CADET_LocalInfoPeer) == ntohs (msg->size))
868 h->info_cb.peers_cb (h->info_cls,
870 (int) ntohs (info->tunnel),
871 (unsigned int) ntohs (info->paths),
874 h->info_cb.peers_cb (h->info_cls,
883 * Check that message received from CADET service is well-formed.
885 * @param cls the `struct GNUNET_CADET_Handle`
886 * @param message the message we got
887 * @return #GNUNET_OK if the message is well-formed,
888 * #GNUNET_SYSERR otherwise
891 check_get_peer (void *cls,
892 const struct GNUNET_CADET_LocalInfoPeer *message)
894 size_t msize = sizeof (struct GNUNET_CADET_LocalInfoPeer);
895 const struct GNUNET_PeerIdentity *paths_array;
901 esize = ntohs (message->header.size);
905 return GNUNET_SYSERR;
907 if (0 != ((esize - msize) % sizeof (struct GNUNET_PeerIdentity)))
910 return GNUNET_SYSERR;
912 peers = (esize - msize) / sizeof (struct GNUNET_PeerIdentity);
913 epaths = ntohs (message->paths);
914 paths_array = (const struct GNUNET_PeerIdentity *) &message[1];
916 for (unsigned int i = 0; i < peers; i++)
917 if (0 == memcmp (&paths_array[i],
918 &message->destination,
919 sizeof (struct GNUNET_PeerIdentity)))
924 return GNUNET_SYSERR;
931 * Process a local peer info reply, pass info to the user.
933 * @param cls Closure (Cadet handle).
934 * @param message Message itself.
937 handle_get_peer (void *cls,
938 const struct GNUNET_CADET_LocalInfoPeer *message)
940 struct GNUNET_CADET_Handle *h = cls;
941 const struct GNUNET_PeerIdentity *paths_array;
943 unsigned int path_length;
947 if (NULL == h->info_cb.peer_cb)
949 paths = ntohs (message->paths);
950 paths_array = (const struct GNUNET_PeerIdentity *) &message[1];
951 peers = (ntohs (message->header.size) - sizeof (*message))
952 / sizeof (struct GNUNET_PeerIdentity);
954 neighbor = GNUNET_NO;
956 for (unsigned int i = 0; i < peers; i++)
958 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
960 GNUNET_i2s (&paths_array[i]));
962 if (0 == memcmp (&paths_array[i], &message->destination,
963 sizeof (struct GNUNET_PeerIdentity)))
965 if (1 == path_length)
966 neighbor = GNUNET_YES;
971 /* Call Callback with tunnel info. */
972 paths_array = (const struct GNUNET_PeerIdentity *) &message[1];
973 h->info_cb.peer_cb (h->info_cls,
974 &message->destination,
975 (int) ntohs (message->tunnel),
983 * Check that message received from CADET service is well-formed.
985 * @param cls the `struct GNUNET_CADET_Handle`
986 * @param message the message we got
987 * @return #GNUNET_OK if the message is well-formed,
988 * #GNUNET_SYSERR otherwise
991 check_get_tunnels (void *cls,
992 const struct GNUNET_MessageHeader *message)
997 esize = ntohs (message->size);
998 if (sizeof (struct GNUNET_CADET_LocalInfoTunnel) == esize)
1000 if (sizeof (struct GNUNET_MessageHeader) == esize)
1002 return GNUNET_SYSERR;
1007 * Process a local reply about info on all tunnels, pass info to the user.
1009 * @param cls Closure (Cadet handle).
1010 * @param message Message itself.
1013 handle_get_tunnels (void *cls,
1014 const struct GNUNET_MessageHeader *msg)
1016 struct GNUNET_CADET_Handle *h = cls;
1017 const struct GNUNET_CADET_LocalInfoTunnel *info =
1018 (const struct GNUNET_CADET_LocalInfoTunnel *) msg;
1020 if (NULL == h->info_cb.tunnels_cb)
1022 if (sizeof (struct GNUNET_CADET_LocalInfoTunnel) == ntohs (msg->size))
1023 h->info_cb.tunnels_cb (h->info_cls,
1025 ntohl (info->channels),
1026 ntohl (info->connections),
1027 ntohs (info->estate),
1028 ntohs (info->cstate));
1030 h->info_cb.tunnels_cb (h->info_cls,
1040 * Check that message received from CADET service is well-formed.
1042 * @param cls the `struct GNUNET_CADET_Handle`
1043 * @param msg the message we got
1044 * @return #GNUNET_OK if the message is well-formed,
1045 * #GNUNET_SYSERR otherwise
1048 check_get_tunnel (void *cls,
1049 const struct GNUNET_CADET_LocalInfoTunnel *msg)
1057 /* Verify message sanity */
1058 msize = ntohs (msg->header.size);
1059 esize = sizeof (struct GNUNET_CADET_LocalInfoTunnel);
1063 return GNUNET_SYSERR;
1065 ch_n = ntohl (msg->channels);
1066 c_n = ntohl (msg->connections);
1067 esize += ch_n * sizeof (struct GNUNET_CADET_ChannelTunnelNumber);
1068 esize += c_n * sizeof (struct GNUNET_CADET_ConnectionTunnelIdentifier);
1071 GNUNET_break_op (0);
1072 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1073 "m:%u, e: %u (%u ch, %u conn)\n",
1074 (unsigned int) msize,
1075 (unsigned int) esize,
1078 return GNUNET_SYSERR;
1085 * Process a local tunnel info reply, pass info to the user.
1087 * @param cls Closure (Cadet handle).
1088 * @param msg Message itself.
1091 handle_get_tunnel (void *cls,
1092 const struct GNUNET_CADET_LocalInfoTunnel *msg)
1094 struct GNUNET_CADET_Handle *h = cls;
1097 const struct GNUNET_CADET_ConnectionTunnelIdentifier *conns;
1098 const struct GNUNET_CADET_ChannelTunnelNumber *chns;
1100 if (NULL == h->info_cb.tunnel_cb)
1102 ch_n = ntohl (msg->channels);
1103 c_n = ntohl (msg->connections);
1105 /* Call Callback with tunnel info. */
1106 conns = (const struct GNUNET_CADET_ConnectionTunnelIdentifier *) &msg[1];
1107 chns = (const struct GNUNET_CADET_ChannelTunnelNumber *) &conns[c_n];
1108 h->info_cb.tunnel_cb (h->info_cls,
1114 ntohs (msg->estate),
1115 ntohs (msg->cstate));
1120 * Reconnect to the service, retransmit all infomation to try to restore the
1123 * @param h handle to the cadet
1126 reconnect (struct GNUNET_CADET_Handle *h)
1128 struct GNUNET_MQ_MessageHandler handlers[] = {
1129 GNUNET_MQ_hd_fixed_size (channel_created,
1130 GNUNET_MESSAGE_TYPE_CADET_LOCAL_CHANNEL_CREATE,
1131 struct GNUNET_CADET_LocalChannelCreateMessage,
1133 GNUNET_MQ_hd_fixed_size (channel_destroy,
1134 GNUNET_MESSAGE_TYPE_CADET_LOCAL_CHANNEL_DESTROY,
1135 struct GNUNET_CADET_LocalChannelDestroyMessage,
1137 GNUNET_MQ_hd_var_size (local_data,
1138 GNUNET_MESSAGE_TYPE_CADET_LOCAL_DATA,
1139 struct GNUNET_CADET_LocalData,
1141 GNUNET_MQ_hd_fixed_size (local_ack,
1142 GNUNET_MESSAGE_TYPE_CADET_LOCAL_ACK,
1143 struct GNUNET_CADET_LocalAck,
1145 GNUNET_MQ_hd_var_size (get_peers,
1146 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEERS,
1147 struct GNUNET_MessageHeader,
1149 GNUNET_MQ_hd_var_size (get_peer,
1150 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEER,
1151 struct GNUNET_CADET_LocalInfoPeer,
1153 GNUNET_MQ_hd_var_size (get_tunnels,
1154 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNELS,
1155 struct GNUNET_MessageHeader,
1157 GNUNET_MQ_hd_var_size (get_tunnel,
1158 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNEL,
1159 struct GNUNET_CADET_LocalInfoTunnel,
1161 GNUNET_MQ_handler_end ()
1164 GNUNET_assert (NULL == h->mq);
1165 h->mq = GNUNET_CLIENT_connect (h->cfg,
1172 schedule_reconnect (h);
1175 h->reconnect_time = GNUNET_TIME_UNIT_MILLISECONDS;
1180 * Function called during #GNUNET_CADET_disconnect() to destroy
1181 * all channels that are still open.
1183 * @param cls the `struct GNUNET_CADET_Handle`
1184 * @param cid chanenl ID
1185 * @param value a `struct GNUNET_CADET_Channel` to destroy
1186 * @return #GNUNET_OK (continue to iterate)
1189 destroy_channel_cb (void *cls,
1193 /* struct GNUNET_CADET_Handle *handle = cls; */
1194 struct GNUNET_CADET_Channel *ch = value;
1198 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1199 "Destroying channel due to GNUNET_CADET_disconnect()\n");
1200 destroy_channel (ch);
1206 * Function called during #GNUNET_CADET_disconnect() to destroy
1207 * all ports that are still open.
1209 * @param cls the `struct GNUNET_CADET_Handle`
1211 * @param value a `struct GNUNET_CADET_Channel` to destroy
1212 * @return #GNUNET_OK (continue to iterate)
1215 destroy_port_cb (void *cls,
1216 const struct GNUNET_HashCode *id,
1219 /* struct GNUNET_CADET_Handle *handle = cls; */
1220 struct GNUNET_CADET_Port *port = value;
1223 /* This is a warning, the app should have cleanly closed all open ports */
1225 GNUNET_CADET_close_port (port);
1231 * Disconnect from the cadet service. All channels will be destroyed. All channel
1232 * disconnect callbacks will be called on any still connected peers, notifying
1233 * about their disconnection. The registered inbound channel cleaner will be
1234 * called should any inbound channels still exist.
1236 * @param handle connection to cadet to disconnect
1239 GNUNET_CADET_disconnect (struct GNUNET_CADET_Handle *handle)
1241 GNUNET_CONTAINER_multihashmap_iterate (handle->ports,
1244 GNUNET_CONTAINER_multihashmap_destroy (handle->ports);
1245 handle->ports = NULL;
1246 GNUNET_CONTAINER_multihashmap32_iterate (handle->channels,
1247 &destroy_channel_cb,
1249 GNUNET_CONTAINER_multihashmap32_destroy (handle->channels);
1250 handle->channels = NULL;
1251 if (NULL != handle->mq)
1253 GNUNET_MQ_destroy (handle->mq);
1256 if (NULL != handle->reconnect_task)
1258 GNUNET_SCHEDULER_cancel (handle->reconnect_task);
1259 handle->reconnect_task = NULL;
1261 GNUNET_free (handle);
1266 * Close a port opened with @a GNUNET_CADET_open_port().
1267 * The @a new_channel callback will no longer be called.
1269 * @param p Port handle.
1272 GNUNET_CADET_close_port (struct GNUNET_CADET_Port *p)
1274 struct GNUNET_CADET_PortMessage *msg;
1275 struct GNUNET_MQ_Envelope *env;
1277 GNUNET_assert (GNUNET_YES ==
1278 GNUNET_CONTAINER_multihashmap_remove (p->cadet->ports,
1281 env = GNUNET_MQ_msg (msg,
1282 GNUNET_MESSAGE_TYPE_CADET_LOCAL_PORT_CLOSE);
1284 GNUNET_MQ_send (p->cadet->mq,
1286 GNUNET_free_non_null (p->handlers);
1292 * Destroy an existing channel.
1294 * The existing end callback for the channel will be called immediately.
1295 * Any pending outgoing messages will be sent but no incoming messages will be
1296 * accepted and no data callbacks will be called.
1298 * @param channel Channel handle, becomes invalid after this call.
1301 GNUNET_CADET_channel_destroy (struct GNUNET_CADET_Channel *channel)
1303 struct GNUNET_CADET_Handle *h = channel->cadet;
1304 struct GNUNET_CADET_LocalChannelDestroyMessage *msg;
1305 struct GNUNET_MQ_Envelope *env;
1309 env = GNUNET_MQ_msg (msg,
1310 GNUNET_MESSAGE_TYPE_CADET_LOCAL_CHANNEL_DESTROY);
1311 msg->ccn = channel->ccn;
1312 GNUNET_MQ_send (h->mq,
1315 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1316 "Destroying channel due to GNUNET_CADET_channel_destroy()\n");
1317 destroy_channel (channel);
1322 * Get information about a channel.
1324 * @param channel Channel handle.
1325 * @param option Query (GNUNET_CADET_OPTION_*).
1326 * @param ... dependant on option, currently not used
1328 * @return Union with an answer to the query.
1330 const union GNUNET_CADET_ChannelInfo *
1331 GNUNET_CADET_channel_get_info (struct GNUNET_CADET_Channel *channel,
1332 enum GNUNET_CADET_ChannelOption option,
1335 static int bool_flag;
1339 case GNUNET_CADET_OPTION_NOBUFFER:
1340 case GNUNET_CADET_OPTION_RELIABLE:
1341 case GNUNET_CADET_OPTION_OUT_OF_ORDER:
1342 if (0 != (option & channel->options))
1343 bool_flag = GNUNET_YES;
1345 bool_flag = GNUNET_NO;
1346 return (const union GNUNET_CADET_ChannelInfo *) &bool_flag;
1348 case GNUNET_CADET_OPTION_PEER:
1349 return (const union GNUNET_CADET_ChannelInfo *) &channel->peer;
1359 * Send an ack on the channel to confirm the processing of a message.
1361 * @param ch Channel on which to send the ACK.
1364 GNUNET_CADET_receive_done (struct GNUNET_CADET_Channel *channel)
1366 struct GNUNET_CADET_LocalAck *msg;
1367 struct GNUNET_MQ_Envelope *env;
1369 env = GNUNET_MQ_msg (msg,
1370 GNUNET_MESSAGE_TYPE_CADET_LOCAL_ACK);
1371 LOG (GNUNET_ERROR_TYPE_DEBUG,
1372 "Sending ACK on channel %X\n",
1373 ntohl (channel->ccn.channel_of_client));
1374 msg->ccn = channel->ccn;
1375 GNUNET_MQ_send (channel->cadet->mq,
1381 * Send message of @a type to CADET service of @a h
1383 * @param h handle to CADET service
1384 * @param type message type of trivial information request to send
1387 send_info_request (struct GNUNET_CADET_Handle *h,
1390 struct GNUNET_MessageHeader *msg;
1391 struct GNUNET_MQ_Envelope *env;
1393 env = GNUNET_MQ_msg (msg,
1395 GNUNET_MQ_send (h->mq,
1401 * Request a debug dump on the service's STDERR.
1403 * WARNING: unstable API, likely to change in the future!
1405 * @param h cadet handle
1408 GNUNET_CADET_request_dump (struct GNUNET_CADET_Handle *h)
1410 send_info_request (h,
1411 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_DUMP);
1416 * Request information about peers known to the running cadet service.
1417 * The callback will be called for every peer known to the service.
1418 * Only one info request (of any kind) can be active at once.
1420 * WARNING: unstable API, likely to change in the future!
1422 * @param h Handle to the cadet peer.
1423 * @param callback Function to call with the requested data.
1424 * @param callback_cls Closure for @c callback.
1425 * @return #GNUNET_OK / #GNUNET_SYSERR
1428 GNUNET_CADET_get_peers (struct GNUNET_CADET_Handle *h,
1429 GNUNET_CADET_PeersCB callback,
1432 if (NULL != h->info_cb.peers_cb)
1435 return GNUNET_SYSERR;
1437 send_info_request (h,
1438 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEERS);
1439 h->info_cb.peers_cb = callback;
1440 h->info_cls = callback_cls;
1446 * Cancel a peer info request. The callback will not be called (anymore).
1448 * WARNING: unstable API, likely to change in the future!
1450 * @param h Cadet handle.
1451 * @return Closure given to GNUNET_CADET_get_peers().
1454 GNUNET_CADET_get_peers_cancel (struct GNUNET_CADET_Handle *h)
1456 void *cls = h->info_cls;
1458 h->info_cb.peers_cb = NULL;
1465 * Request information about a peer known to the running cadet peer.
1466 * The callback will be called for the tunnel once.
1467 * Only one info request (of any kind) can be active at once.
1469 * WARNING: unstable API, likely to change in the future!
1471 * @param h Handle to the cadet peer.
1472 * @param id Peer whose tunnel to examine.
1473 * @param callback Function to call with the requested data.
1474 * @param callback_cls Closure for @c callback.
1475 * @return #GNUNET_OK / #GNUNET_SYSERR
1478 GNUNET_CADET_get_peer (struct GNUNET_CADET_Handle *h,
1479 const struct GNUNET_PeerIdentity *id,
1480 GNUNET_CADET_PeerCB callback,
1483 struct GNUNET_CADET_LocalInfo *msg;
1484 struct GNUNET_MQ_Envelope *env;
1486 if (NULL != h->info_cb.peer_cb)
1489 return GNUNET_SYSERR;
1491 env = GNUNET_MQ_msg (msg,
1492 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEER);
1494 GNUNET_MQ_send (h->mq,
1496 h->info_cb.peer_cb = callback;
1497 h->info_cls = callback_cls;
1503 * Request information about tunnels of the running cadet peer.
1504 * The callback will be called for every tunnel of the service.
1505 * Only one info request (of any kind) can be active at once.
1507 * WARNING: unstable API, likely to change in the future!
1509 * @param h Handle to the cadet peer.
1510 * @param callback Function to call with the requested data.
1511 * @param callback_cls Closure for @c callback.
1512 * @return #GNUNET_OK / #GNUNET_SYSERR
1515 GNUNET_CADET_get_tunnels (struct GNUNET_CADET_Handle *h,
1516 GNUNET_CADET_TunnelsCB callback,
1519 if (NULL != h->info_cb.tunnels_cb)
1522 return GNUNET_SYSERR;
1524 send_info_request (h,
1525 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNELS);
1526 h->info_cb.tunnels_cb = callback;
1527 h->info_cls = callback_cls;
1533 * Cancel a monitor request. The monitor callback will not be called.
1535 * @param h Cadet handle.
1536 * @return Closure given to GNUNET_CADET_get_tunnels().
1539 GNUNET_CADET_get_tunnels_cancel (struct GNUNET_CADET_Handle *h)
1541 void *cls = h->info_cls;
1543 h->info_cb.tunnels_cb = NULL;
1550 * Request information about a tunnel of the running cadet peer.
1551 * The callback will be called for the tunnel once.
1552 * Only one info request (of any kind) can be active at once.
1554 * WARNING: unstable API, likely to change in the future!
1556 * @param h Handle to the cadet peer.
1557 * @param id Peer whose tunnel to examine.
1558 * @param callback Function to call with the requested data.
1559 * @param callback_cls Closure for @c callback.
1560 * @return #GNUNET_OK / #GNUNET_SYSERR
1563 GNUNET_CADET_get_tunnel (struct GNUNET_CADET_Handle *h,
1564 const struct GNUNET_PeerIdentity *id,
1565 GNUNET_CADET_TunnelCB callback,
1568 struct GNUNET_CADET_LocalInfo *msg;
1569 struct GNUNET_MQ_Envelope *env;
1571 if (NULL != h->info_cb.tunnel_cb)
1574 return GNUNET_SYSERR;
1576 env = GNUNET_MQ_msg (msg,
1577 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNEL);
1579 GNUNET_MQ_send (h->mq,
1581 h->info_cb.tunnel_cb = callback;
1582 h->info_cls = callback_cls;
1588 * Transitional function to convert an unsigned int port to a hash value.
1589 * WARNING: local static value returned, NOT reentrant!
1590 * WARNING: do not use this function for new code!
1592 * @param port Numerical port (unsigned int format).
1594 * @return A GNUNET_HashCode usable for the new CADET API.
1596 const struct GNUNET_HashCode *
1597 GC_u2h (uint32_t port)
1599 static struct GNUNET_HashCode hash;
1601 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1602 "This is a transitional function, use proper crypto hashes as CADET ports\n");
1603 GNUNET_CRYPTO_hash (&port,
1611 * Connect to the MQ-based cadet service.
1613 * @param cfg Configuration to use.
1615 * @return Handle to the cadet service NULL on error.
1617 struct GNUNET_CADET_Handle *
1618 GNUNET_CADET_connect (const struct GNUNET_CONFIGURATION_Handle *cfg)
1620 struct GNUNET_CADET_Handle *h;
1622 LOG (GNUNET_ERROR_TYPE_DEBUG,
1623 "GNUNET_CADET_connect()\n");
1624 h = GNUNET_new (struct GNUNET_CADET_Handle);
1626 h->ports = GNUNET_CONTAINER_multihashmap_create (4,
1628 h->channels = GNUNET_CONTAINER_multihashmap32_create (4);
1633 GNUNET_CADET_disconnect (h);
1636 h->next_ccn.channel_of_client = htonl (GNUNET_CADET_LOCAL_CHANNEL_ID_CLI);
1637 h->reconnect_time = GNUNET_TIME_UNIT_MILLISECONDS;
1638 h->reconnect_task = NULL;
1645 * Open a port to receive incomming MQ-based channels.
1647 * @param h CADET handle.
1648 * @param port Hash identifying the port.
1649 * @param connects Function called when an incoming channel is connected.
1650 * @param connects_cls Closure for the @a connects handler.
1651 * @param window_changes Function called when the transmit window size changes.
1652 * @param disconnects Function called when a channel is disconnected.
1653 * @param handlers Callbacks for messages we care about, NULL-terminated.
1654 * @return Port handle, NULL if port is in use
1656 struct GNUNET_CADET_Port *
1657 GNUNET_CADET_open_port (struct GNUNET_CADET_Handle *h,
1658 const struct GNUNET_HashCode *port,
1659 GNUNET_CADET_ConnectEventHandler connects,
1660 void * connects_cls,
1661 GNUNET_CADET_WindowSizeEventHandler window_changes,
1662 GNUNET_CADET_DisconnectEventHandler disconnects,
1663 const struct GNUNET_MQ_MessageHandler *handlers)
1665 struct GNUNET_CADET_PortMessage *msg;
1666 struct GNUNET_MQ_Envelope *env;
1667 struct GNUNET_CADET_Port *p;
1669 GNUNET_assert (NULL != connects);
1670 GNUNET_assert (NULL != disconnects);
1671 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1672 "Listening to CADET port %s\n",
1675 p = GNUNET_new (struct GNUNET_CADET_Port);
1679 GNUNET_CONTAINER_multihashmap_put (h->ports,
1682 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
1687 p->connects = connects;
1688 p->cls = connects_cls;
1689 p->window_changes = window_changes;
1690 p->disconnects = disconnects;
1691 p->handlers = GNUNET_MQ_copy_handlers (handlers);
1694 env = GNUNET_MQ_msg (msg,
1695 GNUNET_MESSAGE_TYPE_CADET_LOCAL_PORT_OPEN);
1697 GNUNET_MQ_send (h->mq,
1704 * Create a new channel towards a remote peer.
1706 * If the destination port is not open by any peer or the destination peer
1707 * does not accept the channel, #GNUNET_CADET_ChannelEndHandler will be called
1710 * @param h CADET handle.
1711 * @param channel_cls Closure for the channel. It's given to:
1712 * - The disconnect handler @a disconnects
1713 * - Each message type callback in @a handlers
1714 * @param destination Peer identity the channel should go to.
1715 * @param port Identification of the destination port.
1716 * @param options CadetOption flag field, with all desired option bits set to 1.
1717 * @param window_changes Function called when the transmit window size changes.
1718 * @param disconnects Function called when the channel is disconnected.
1719 * @param handlers Callbacks for messages we care about, NULL-terminated.
1720 * @return Handle to the channel.
1722 struct GNUNET_CADET_Channel *
1723 GNUNET_CADET_channel_create (struct GNUNET_CADET_Handle *h,
1725 const struct GNUNET_PeerIdentity *destination,
1726 const struct GNUNET_HashCode *port,
1727 enum GNUNET_CADET_ChannelOption options,
1728 GNUNET_CADET_WindowSizeEventHandler window_changes,
1729 GNUNET_CADET_DisconnectEventHandler disconnects,
1730 const struct GNUNET_MQ_MessageHandler *handlers)
1732 struct GNUNET_CADET_Channel *ch;
1733 struct GNUNET_CADET_LocalChannelCreateMessage *msg;
1734 struct GNUNET_MQ_Envelope *env;
1736 GNUNET_assert (NULL != disconnects);
1737 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1738 "Creating channel to peer %s at port %s\n",
1739 GNUNET_i2s (destination),
1741 ch = create_channel (h,
1743 ch->ctx = channel_cls;
1744 ch->peer = *destination;
1745 ch->options = options;
1746 ch->window_changes = window_changes;
1747 ch->disconnects = disconnects;
1749 /* Create MQ for channel */
1750 ch->mq = GNUNET_MQ_queue_for_callbacks (&cadet_mq_send_impl,
1751 &cadet_mq_destroy_impl,
1752 &cadet_mq_cancel_impl,
1755 &cadet_mq_error_handler,
1757 GNUNET_MQ_set_handlers_closure (ch->mq, channel_cls);
1759 /* Request channel creation to service */
1760 env = GNUNET_MQ_msg (msg,
1761 GNUNET_MESSAGE_TYPE_CADET_LOCAL_CHANNEL_CREATE);
1764 msg->peer = *destination;
1765 msg->opt = htonl (options);
1766 GNUNET_MQ_send (h->mq,
1773 * Obtain the message queue for a connected peer.
1775 * @param channel The channel handle from which to get the MQ.
1777 * @return NULL if @a channel is not yet connected.
1779 struct GNUNET_MQ_Handle *
1780 GNUNET_CADET_get_mq (const struct GNUNET_CADET_Channel *channel)
1785 /* end of cadet_api.c */