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 Affero 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.
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
19 * @file cadet/cadet_api.c
20 * @brief cadet api: client implementation of cadet service
21 * @author Bartlomiej Polot
22 * @author Christian Grothoff
25 #include "gnunet_util_lib.h"
26 #include "gnunet_constants.h"
27 #include "gnunet_cadet_service.h"
29 #include "cadet_protocol.h"
31 #define LOG(kind,...) GNUNET_log_from (kind, "cadet-api",__VA_ARGS__)
42 GNUNET_CADET_ChannelCB channel_cb;
47 GNUNET_CADET_PeersCB peers_cb;
52 GNUNET_CADET_PeerCB peer_cb;
57 GNUNET_CADET_TunnelsCB tunnels_cb;
62 GNUNET_CADET_TunnelCB tunnel_cb;
67 * Opaque handle to the service.
69 struct GNUNET_CADET_Handle
74 struct GNUNET_MQ_Handle *mq;
79 struct GNUNET_CONTAINER_MultiHashMap *ports;
84 struct GNUNET_CONTAINER_MultiHashMap32 *channels;
87 * child of the next channel to create (to avoid reusing IDs often)
89 struct GNUNET_CADET_ClientChannelNumber next_ccn;
92 * Configuration given by the client, in case of reconnection
94 const struct GNUNET_CONFIGURATION_Handle *cfg;
97 * Task for trying to reconnect.
99 struct GNUNET_SCHEDULER_Task *reconnect_task;
102 * Callback for an info task (only one active at a time).
104 union CadetInfoCB info_cb;
107 * Info callback closure for @c info_cb.
112 * Time to the next reconnect in case one reconnect fails
114 struct GNUNET_TIME_Relative reconnect_time;
120 * Opaque handle to a channel.
122 struct GNUNET_CADET_Channel
126 * Other end of the channel.
128 struct GNUNET_PeerIdentity peer;
131 * Handle to the cadet this channel belongs to
133 struct GNUNET_CADET_Handle *cadet;
136 * Channel's port, if incoming.
138 struct GNUNET_CADET_Port *incoming_port;
141 * Any data the caller wants to put in here, used for the
142 * various callbacks (@e disconnects, @e window_changes, handlers).
147 * Message Queue for the channel (which we are implementing).
149 struct GNUNET_MQ_Handle *mq;
152 * Task to allow mq to send more traffic.
154 struct GNUNET_SCHEDULER_Task *mq_cont;
157 * Pending envelope with a message to be transmitted to the
158 * service as soon as we are allowed to. Should only be
159 * non-NULL if @e allow_send is 0.
161 struct GNUNET_MQ_Envelope *pending_env;
164 * Window change handler.
166 GNUNET_CADET_WindowSizeEventHandler window_changes;
169 * Disconnect handler.
171 GNUNET_CADET_DisconnectEventHandler disconnects;
174 * Local ID of the channel, #GNUNET_CADET_LOCAL_CHANNEL_ID_CLI bit is set if outbound.
176 struct GNUNET_CADET_ClientChannelNumber ccn;
179 * Channel options: reliability, etc.
181 enum GNUNET_CADET_ChannelOption options;
184 * How many messages are we allowed to send to the service right now?
186 unsigned int allow_send;
192 * Opaque handle to a port.
194 struct GNUNET_CADET_Port
200 struct GNUNET_HashCode id;
203 * Handle to the CADET session this port belongs to.
205 struct GNUNET_CADET_Handle *cadet;
208 * Closure for @a handler.
213 * Handler for incoming channels on this port
215 GNUNET_CADET_ConnectEventHandler connects;
218 * Closure for @ref connects
223 * Window size change handler.
225 GNUNET_CADET_WindowSizeEventHandler window_changes;
228 * Handler called when an incoming channel is destroyed.
230 GNUNET_CADET_DisconnectEventHandler disconnects;
233 * Payload handlers for incoming channels.
235 struct GNUNET_MQ_MessageHandler *handlers;
240 * Find the Port struct for a hash.
242 * @param h CADET handle.
243 * @param hash HashCode for the port number.
244 * @return The port handle if known, NULL otherwise.
246 static struct GNUNET_CADET_Port *
247 find_port (const struct GNUNET_CADET_Handle *h,
248 const struct GNUNET_HashCode *hash)
250 return GNUNET_CONTAINER_multihashmap_get (h->ports,
256 * Get the channel handler for the channel specified by id from the given handle
258 * @param h Cadet handle
259 * @param ccn ID of the wanted channel
260 * @return handle to the required channel or NULL if not found
262 static struct GNUNET_CADET_Channel *
263 find_channel (struct GNUNET_CADET_Handle *h,
264 struct GNUNET_CADET_ClientChannelNumber ccn)
266 return GNUNET_CONTAINER_multihashmap32_get (h->channels,
267 ntohl (ccn.channel_of_client));
272 * Create a new channel and insert it in the channel list of the cadet handle
274 * @param h Cadet handle
275 * @param ccnp pointer to desired ccn of the channel, NULL to assign one automatically.
276 * @return Handle to the created channel.
278 static struct GNUNET_CADET_Channel *
279 create_channel (struct GNUNET_CADET_Handle *h,
280 const struct GNUNET_CADET_ClientChannelNumber *ccnp)
282 struct GNUNET_CADET_Channel *ch;
283 struct GNUNET_CADET_ClientChannelNumber ccn;
285 ch = GNUNET_new (struct GNUNET_CADET_Channel);
292 h->next_ccn.channel_of_client
293 = htonl (GNUNET_CADET_LOCAL_CHANNEL_ID_CLI | (1 + ntohl (h->next_ccn.channel_of_client)));
301 GNUNET_assert (GNUNET_OK ==
302 GNUNET_CONTAINER_multihashmap32_put (h->channels,
303 ntohl (ch->ccn.channel_of_client),
305 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
311 * Destroy the specified channel.
312 * - Destroys all peers, calling the disconnect callback on each if needed
313 * - Cancels all outgoing traffic for that channel, calling respective notifys
314 * - Calls cleaner if channel was inbound
315 * - Frees all memory used
317 * @param ch Pointer to the channel.
318 * @param call_cleaner Whether to call the cleaner handler.
321 destroy_channel (struct GNUNET_CADET_Channel *ch)
323 struct GNUNET_CADET_Handle *h = ch->cadet;
325 LOG (GNUNET_ERROR_TYPE_DEBUG,
326 "Destroying channel %X of %p\n",
327 htonl (ch->ccn.channel_of_client),
329 GNUNET_assert (GNUNET_YES ==
330 GNUNET_CONTAINER_multihashmap32_remove (h->channels,
331 ntohl (ch->ccn.channel_of_client),
333 if (NULL != ch->mq_cont)
335 GNUNET_SCHEDULER_cancel (ch->mq_cont);
338 /* signal channel destruction */
339 if (NULL != ch->disconnects)
340 ch->disconnects (ch->ctx,
342 if (NULL != ch->pending_env)
343 GNUNET_MQ_discard (ch->pending_env);
344 GNUNET_MQ_destroy (ch->mq);
350 * Reconnect to the service, retransmit all infomation to try to restore the
353 * @param h handle to the cadet
356 reconnect (struct GNUNET_CADET_Handle *h);
360 * Function called during #reconnect_cbk() to (re)open
361 * all ports that are still open.
363 * @param cls the `struct GNUNET_CADET_Handle`
365 * @param value a `struct GNUNET_CADET_Channel` to open
366 * @return #GNUNET_OK (continue to iterate)
369 open_port_cb (void *cls,
370 const struct GNUNET_HashCode *id,
373 struct GNUNET_CADET_Handle *h = cls;
374 struct GNUNET_CADET_Port *port = value;
375 struct GNUNET_CADET_PortMessage *msg;
376 struct GNUNET_MQ_Envelope *env;
379 env = GNUNET_MQ_msg (msg,
380 GNUNET_MESSAGE_TYPE_CADET_LOCAL_PORT_OPEN);
381 msg->port = port->id;
382 GNUNET_MQ_send (h->mq,
389 * Reconnect callback: tries to reconnect again after a failer previous
392 * @param cls closure (cadet handle)
395 reconnect_cbk (void *cls)
397 struct GNUNET_CADET_Handle *h = cls;
399 h->reconnect_task = NULL;
401 = GNUNET_TIME_STD_BACKOFF (h->reconnect_time);
403 GNUNET_CONTAINER_multihashmap_iterate (h->ports,
410 * Notify the application about a change in the window size (if needed).
412 * @param ch Channel to notify about.
415 notify_window_size (struct GNUNET_CADET_Channel *ch)
417 if (NULL != ch->window_changes)
418 ch->window_changes (ch->ctx,
419 ch, /* FIXME: remove 'ch'? */
425 * Transmit the next message from our queue.
427 * @param cls Closure (channel whose mq to activate).
430 cadet_mq_send_now (void *cls)
432 struct GNUNET_CADET_Channel *ch = cls;
433 struct GNUNET_MQ_Envelope *env = ch->pending_env;
436 if (0 == ch->allow_send)
438 /* how did we get here? */
444 /* how did we get here? */
449 ch->pending_env = NULL;
450 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
451 "Sending message on channel %s to CADET, new window size is %u\n",
452 GNUNET_i2s (&ch->peer),
454 GNUNET_MQ_send (ch->cadet->mq,
456 GNUNET_MQ_impl_send_continue (ch->mq);
461 * Implement sending functionality of a message queue for
462 * us sending messages to a peer.
464 * Encapsulates the payload message in a #GNUNET_CADET_LocalData message
465 * in order to label the message with the channel ID and send the
466 * encapsulated message to the service.
468 * @param mq the message queue
469 * @param msg the message to send
470 * @param impl_state state of the implementation
473 cadet_mq_send_impl (struct GNUNET_MQ_Handle *mq,
474 const struct GNUNET_MessageHeader *msg,
477 struct GNUNET_CADET_Channel *ch = impl_state;
478 struct GNUNET_CADET_Handle *h = ch->cadet;
480 struct GNUNET_MQ_Envelope *env;
481 struct GNUNET_CADET_LocalData *cadet_msg = NULL;
485 /* We're currently reconnecting, pretend this worked */
486 GNUNET_MQ_impl_send_continue (mq);
490 /* check message size for sanity */
491 msize = ntohs (msg->size);
492 if (msize > GNUNET_CONSTANTS_MAX_CADET_MESSAGE_SIZE)
495 GNUNET_MQ_impl_send_continue (mq);
498 env = GNUNET_MQ_msg_nested_mh (cadet_msg,
499 GNUNET_MESSAGE_TYPE_CADET_LOCAL_DATA,
501 cadet_msg->ccn = ch->ccn;
502 GNUNET_assert (NULL == ch->pending_env);
503 ch->pending_env = env;
504 if (0 < ch->allow_send)
506 = GNUNET_SCHEDULER_add_now (&cadet_mq_send_now,
512 * Handle destruction of a message queue. Implementations must not
513 * free @a mq, but should take care of @a impl_state.
515 * @param mq the message queue to destroy
516 * @param impl_state state of the implementation
519 cadet_mq_destroy_impl (struct GNUNET_MQ_Handle *mq,
522 struct GNUNET_CADET_Channel *ch = impl_state;
524 GNUNET_assert (mq == ch->mq);
530 * We had an error processing a message we forwarded from a peer to
531 * the CADET service. We should just complain about it but otherwise
532 * continue processing.
534 * @param cls closure with our `struct GNUNET_CADET_Channel`
535 * @param error error code
538 cadet_mq_error_handler (void *cls,
539 enum GNUNET_MQ_Error error)
541 struct GNUNET_CADET_Channel *ch = cls;
543 if (GNUNET_MQ_ERROR_NO_MATCH == error)
545 /* Got a message we did not understand, still try to continue! */
547 GNUNET_CADET_receive_done (ch);
552 GNUNET_CADET_channel_destroy (ch);
558 * Implementation function that cancels the currently sent message.
559 * Should basically undo whatever #mq_send_impl() did.
561 * @param mq message queue
562 * @param impl_state state specific to the implementation
565 cadet_mq_cancel_impl (struct GNUNET_MQ_Handle *mq,
568 struct GNUNET_CADET_Channel *ch = impl_state;
571 GNUNET_assert (NULL != ch->pending_env);
572 GNUNET_MQ_discard (ch->pending_env);
573 ch->pending_env = NULL;
574 if (NULL != ch->mq_cont)
576 GNUNET_SCHEDULER_cancel (ch->mq_cont);
583 * Process the new channel notification and add it to the channels in the handle
585 * @param h The cadet handle
586 * @param msg A message with the details of the new incoming channel
589 handle_channel_created (void *cls,
590 const struct GNUNET_CADET_LocalChannelCreateMessage *msg)
592 struct GNUNET_CADET_Handle *h = cls;
593 struct GNUNET_CADET_Channel *ch;
594 struct GNUNET_CADET_Port *port;
595 const struct GNUNET_HashCode *port_number;
596 struct GNUNET_CADET_ClientChannelNumber ccn;
599 port_number = &msg->port;
600 if (ntohl (ccn.channel_of_client) >= GNUNET_CADET_LOCAL_CHANNEL_ID_CLI)
609 /* We could have closed the port but the service didn't know about it yet
610 * This is not an error.
612 struct GNUNET_CADET_LocalChannelDestroyMessage *d_msg;
613 struct GNUNET_MQ_Envelope *env;
615 LOG (GNUNET_ERROR_TYPE_DEBUG,
616 "No handler for incoming channel %X (on port %s, recently closed?)\n",
617 ntohl (ccn.channel_of_client),
618 GNUNET_h2s (port_number));
619 env = GNUNET_MQ_msg (d_msg,
620 GNUNET_MESSAGE_TYPE_CADET_LOCAL_CHANNEL_DESTROY);
621 d_msg->ccn = msg->ccn;
622 GNUNET_MQ_send (h->mq,
627 ch = create_channel (h,
629 ch->peer = msg->peer;
630 ch->incoming_port = port;
631 ch->options = ntohl (msg->opt);
632 LOG (GNUNET_ERROR_TYPE_DEBUG,
633 "Creating incoming channel %X [%s] %p\n",
634 ntohl (ccn.channel_of_client),
635 GNUNET_h2s (port_number),
638 GNUNET_assert (NULL != port->connects);
639 ch->window_changes = port->window_changes;
640 ch->disconnects = port->disconnects;
641 ch->mq = GNUNET_MQ_queue_for_callbacks (&cadet_mq_send_impl,
642 &cadet_mq_destroy_impl,
643 &cadet_mq_cancel_impl,
646 &cadet_mq_error_handler,
648 ch->ctx = port->connects (port->cls,
651 GNUNET_MQ_set_handlers_closure (ch->mq,
657 * Process the channel destroy notification and free associated resources
659 * @param h The cadet handle
660 * @param msg A message with the details of the channel being destroyed
663 handle_channel_destroy (void *cls,
664 const struct GNUNET_CADET_LocalChannelDestroyMessage *msg)
666 struct GNUNET_CADET_Handle *h = cls;
667 struct GNUNET_CADET_Channel *ch;
669 ch = find_channel (h,
673 LOG (GNUNET_ERROR_TYPE_DEBUG,
674 "Received channel destroy for unknown channel %X from CADET service (recently close?)\n",
675 ntohl (msg->ccn.channel_of_client));
678 LOG (GNUNET_ERROR_TYPE_DEBUG,
679 "Received channel destroy for channel %X from CADET service\n",
680 ntohl (msg->ccn.channel_of_client));
681 destroy_channel (ch);
686 * Check that message received from CADET service is well-formed.
688 * @param cls the `struct GNUNET_CADET_Handle`
689 * @param message the message we got
690 * @return #GNUNET_OK if the message is well-formed,
691 * #GNUNET_SYSERR otherwise
694 check_local_data (void *cls,
695 const struct GNUNET_CADET_LocalData *message)
700 size = ntohs (message->header.size);
701 if (sizeof (*message) + sizeof (struct GNUNET_MessageHeader) > size)
704 return GNUNET_SYSERR;
711 * Process the incoming data packets, call appropriate handlers.
713 * @param h The cadet handle
714 * @param message A message encapsulating the data
717 handle_local_data (void *cls,
718 const struct GNUNET_CADET_LocalData *message)
720 struct GNUNET_CADET_Handle *h = cls;
721 const struct GNUNET_MessageHeader *payload;
722 struct GNUNET_CADET_Channel *ch;
726 ch = find_channel (h,
730 LOG (GNUNET_ERROR_TYPE_DEBUG,
731 "Unknown channel %X for incoming data (recently closed?)\n",
732 ntohl (message->ccn.channel_of_client));
736 payload = (const struct GNUNET_MessageHeader *) &message[1];
737 type = ntohs (payload->type);
738 fwd = ntohl (ch->ccn.channel_of_client) <= GNUNET_CADET_LOCAL_CHANNEL_ID_CLI;
739 LOG (GNUNET_ERROR_TYPE_DEBUG,
740 "Got a %s data on channel %s [%X] of type %u\n",
742 GNUNET_i2s (&ch->peer),
743 ntohl (message->ccn.channel_of_client),
745 GNUNET_MQ_inject_message (ch->mq,
751 * Process a local ACK message, enabling the client to send
752 * more data to the service.
754 * @param h Cadet handle.
755 * @param message Message itself.
758 handle_local_ack (void *cls,
759 const struct GNUNET_CADET_LocalAck *message)
761 struct GNUNET_CADET_Handle *h = cls;
762 struct GNUNET_CADET_Channel *ch;
764 ch = find_channel (h,
768 LOG (GNUNET_ERROR_TYPE_DEBUG,
769 "ACK on unknown channel %X\n",
770 ntohl (message->ccn.channel_of_client));
774 LOG (GNUNET_ERROR_TYPE_DEBUG,
775 "Got an ACK on mq channel %X (peer %s); new window size is %u!\n",
776 ntohl (ch->ccn.channel_of_client),
777 GNUNET_i2s (&ch->peer),
779 if (NULL == ch->pending_env)
781 LOG (GNUNET_ERROR_TYPE_DEBUG,
782 "Got an ACK on mq channel %X, allow send now %u!\n",
783 ntohl (ch->ccn.channel_of_client),
785 notify_window_size (ch);
788 if (NULL != ch->mq_cont)
789 return; /* already working on it! */
791 = GNUNET_SCHEDULER_add_now (&cadet_mq_send_now,
797 * Function called during #GNUNET_CADET_disconnect() to destroy
798 * all channels that are still open.
800 * @param cls the `struct GNUNET_CADET_Handle`
801 * @param cid chanenl ID
802 * @param value a `struct GNUNET_CADET_Channel` to destroy
803 * @return #GNUNET_OK (continue to iterate)
806 destroy_channel_cb (void *cls,
810 /* struct GNUNET_CADET_Handle *handle = cls; */
811 struct GNUNET_CADET_Channel *ch = value;
815 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
816 "Destroying channel due to GNUNET_CADET_disconnect()\n");
817 destroy_channel (ch);
823 * Generic error handler, called with the appropriate error code and
824 * the same closure specified at the creation of the message queue.
825 * Not every message queue implementation supports an error handler.
827 * @param cls closure, a `struct GNUNET_CORE_Handle *`
828 * @param error error code
831 handle_mq_error (void *cls,
832 enum GNUNET_MQ_Error error)
834 struct GNUNET_CADET_Handle *h = cls;
836 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
839 GNUNET_CONTAINER_multihashmap32_iterate (h->channels,
842 GNUNET_MQ_destroy (h->mq);
844 h->reconnect_task = GNUNET_SCHEDULER_add_delayed (h->reconnect_time,
851 * Check that message received from CADET service is well-formed.
853 * @param cls the `struct GNUNET_CADET_Handle`
854 * @param message the message we got
855 * @return #GNUNET_OK if the message is well-formed,
856 * #GNUNET_SYSERR otherwise
859 check_get_peers (void *cls,
860 const struct GNUNET_MessageHeader *message)
865 esize = ntohs (message->size);
866 if (sizeof (struct GNUNET_CADET_LocalInfoPeer) == esize)
868 if (sizeof (struct GNUNET_MessageHeader) == esize)
870 return GNUNET_SYSERR;
875 * Process a local reply about info on all tunnels, pass info to the user.
877 * @param cls Closure (Cadet handle).
878 * @param msg Message itself.
881 handle_get_peers (void *cls,
882 const struct GNUNET_MessageHeader *msg)
884 struct GNUNET_CADET_Handle *h = cls;
885 const struct GNUNET_CADET_LocalInfoPeer *info =
886 (const struct GNUNET_CADET_LocalInfoPeer *) msg;
888 if (NULL == h->info_cb.peers_cb)
890 if (sizeof (struct GNUNET_CADET_LocalInfoPeer) == ntohs (msg->size))
891 h->info_cb.peers_cb (h->info_cls,
893 (int) ntohs (info->tunnel),
894 (unsigned int) ntohs (info->paths),
897 h->info_cb.peers_cb (h->info_cls,
906 * Check that message received from CADET service is well-formed.
908 * @param cls the `struct GNUNET_CADET_Handle`
909 * @param message the message we got
910 * @return #GNUNET_OK if the message is well-formed,
911 * #GNUNET_SYSERR otherwise
914 check_get_peer (void *cls,
915 const struct GNUNET_CADET_LocalInfoPeer *message)
917 size_t msize = sizeof (struct GNUNET_CADET_LocalInfoPeer);
921 esize = ntohs (message->header.size);
925 return GNUNET_SYSERR;
927 if (0 != ((esize - msize) % sizeof (struct GNUNET_PeerIdentity)))
930 return GNUNET_SYSERR;
937 * Process a local peer info reply, pass info to the user.
939 * @param cls Closure (Cadet handle).
940 * @param message Message itself.
943 handle_get_peer (void *cls,
944 const struct GNUNET_CADET_LocalInfoPeer *message)
946 struct GNUNET_CADET_Handle *h = cls;
947 const struct GNUNET_PeerIdentity *paths_array;
949 unsigned int path_length;
953 if (NULL == h->info_cb.peer_cb)
956 LOG (GNUNET_ERROR_TYPE_DEBUG,
957 "number of paths %u\n",
958 ntohs (message->paths));
960 paths = ntohs (message->paths);
961 paths_array = (const struct GNUNET_PeerIdentity *) &message[1];
962 peers = (ntohs (message->header.size) - sizeof (*message))
963 / sizeof (struct GNUNET_PeerIdentity);
965 neighbor = GNUNET_NO;
967 for (unsigned int i = 0; i < peers; i++)
969 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
971 GNUNET_i2s (&paths_array[i]));
973 if (0 == memcmp (&paths_array[i], &message->destination,
974 sizeof (struct GNUNET_PeerIdentity)))
976 if (1 == path_length)
977 neighbor = GNUNET_YES;
982 /* Call Callback with tunnel info. */
983 paths_array = (const struct GNUNET_PeerIdentity *) &message[1];
984 h->info_cb.peer_cb (h->info_cls,
985 &message->destination,
986 (int) ntohs (message->tunnel),
990 (int) ntohs (message->offset),
991 (int) ntohs (message->finished_with_paths));
996 * Check that message received from CADET service is well-formed.
998 * @param cls the `struct GNUNET_CADET_Handle`
999 * @param message the message we got
1000 * @return #GNUNET_OK if the message is well-formed,
1001 * #GNUNET_SYSERR otherwise
1004 check_get_tunnels (void *cls,
1005 const struct GNUNET_MessageHeader *message)
1010 esize = ntohs (message->size);
1011 if (sizeof (struct GNUNET_CADET_LocalInfoTunnel) == esize)
1013 if (sizeof (struct GNUNET_MessageHeader) == esize)
1015 return GNUNET_SYSERR;
1020 * Process a local reply about info on all tunnels, pass info to the user.
1022 * @param cls Closure (Cadet handle).
1023 * @param message Message itself.
1026 handle_get_tunnels (void *cls,
1027 const struct GNUNET_MessageHeader *msg)
1029 struct GNUNET_CADET_Handle *h = cls;
1030 const struct GNUNET_CADET_LocalInfoTunnel *info =
1031 (const struct GNUNET_CADET_LocalInfoTunnel *) msg;
1033 if (NULL == h->info_cb.tunnels_cb)
1035 if (sizeof (struct GNUNET_CADET_LocalInfoTunnel) == ntohs (msg->size))
1036 h->info_cb.tunnels_cb (h->info_cls,
1038 ntohl (info->channels),
1039 ntohl (info->connections),
1040 ntohs (info->estate),
1041 ntohs (info->cstate));
1043 h->info_cb.tunnels_cb (h->info_cls,
1053 * Check that message received from CADET service is well-formed.
1055 * @param cls the `struct GNUNET_CADET_Handle`
1056 * @param msg the message we got
1057 * @return #GNUNET_OK if the message is well-formed,
1058 * #GNUNET_SYSERR otherwise
1061 check_get_tunnel (void *cls,
1062 const struct GNUNET_CADET_LocalInfoTunnel *msg)
1070 /* Verify message sanity */
1071 msize = ntohs (msg->header.size);
1072 esize = sizeof (struct GNUNET_CADET_LocalInfoTunnel);
1076 return GNUNET_SYSERR;
1078 ch_n = ntohl (msg->channels);
1079 c_n = ntohl (msg->connections);
1080 esize += ch_n * sizeof (struct GNUNET_CADET_ChannelTunnelNumber);
1081 esize += c_n * sizeof (struct GNUNET_CADET_ConnectionTunnelIdentifier);
1084 GNUNET_break_op (0);
1085 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1086 "m:%u, e: %u (%u ch, %u conn)\n",
1087 (unsigned int) msize,
1088 (unsigned int) esize,
1091 return GNUNET_SYSERR;
1098 * Process a local tunnel info reply, pass info to the user.
1100 * @param cls Closure (Cadet handle).
1101 * @param msg Message itself.
1104 handle_get_tunnel (void *cls,
1105 const struct GNUNET_CADET_LocalInfoTunnel *msg)
1107 struct GNUNET_CADET_Handle *h = cls;
1110 const struct GNUNET_CADET_ConnectionTunnelIdentifier *conns;
1111 const struct GNUNET_CADET_ChannelTunnelNumber *chns;
1113 if (NULL == h->info_cb.tunnel_cb)
1115 ch_n = ntohl (msg->channels);
1116 c_n = ntohl (msg->connections);
1118 /* Call Callback with tunnel info. */
1119 conns = (const struct GNUNET_CADET_ConnectionTunnelIdentifier *) &msg[1];
1120 chns = (const struct GNUNET_CADET_ChannelTunnelNumber *) &conns[c_n];
1121 h->info_cb.tunnel_cb (h->info_cls,
1127 ntohs (msg->estate),
1128 ntohs (msg->cstate));
1133 * Reconnect to the service, retransmit all infomation to try to restore the
1136 * @param h handle to the cadet
1139 reconnect (struct GNUNET_CADET_Handle *h)
1141 struct GNUNET_MQ_MessageHandler handlers[] = {
1142 GNUNET_MQ_hd_fixed_size (channel_created,
1143 GNUNET_MESSAGE_TYPE_CADET_LOCAL_CHANNEL_CREATE,
1144 struct GNUNET_CADET_LocalChannelCreateMessage,
1146 GNUNET_MQ_hd_fixed_size (channel_destroy,
1147 GNUNET_MESSAGE_TYPE_CADET_LOCAL_CHANNEL_DESTROY,
1148 struct GNUNET_CADET_LocalChannelDestroyMessage,
1150 GNUNET_MQ_hd_var_size (local_data,
1151 GNUNET_MESSAGE_TYPE_CADET_LOCAL_DATA,
1152 struct GNUNET_CADET_LocalData,
1154 GNUNET_MQ_hd_fixed_size (local_ack,
1155 GNUNET_MESSAGE_TYPE_CADET_LOCAL_ACK,
1156 struct GNUNET_CADET_LocalAck,
1158 GNUNET_MQ_hd_var_size (get_peers,
1159 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEERS,
1160 struct GNUNET_MessageHeader,
1162 GNUNET_MQ_hd_var_size (get_peer,
1163 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEER,
1164 struct GNUNET_CADET_LocalInfoPeer,
1166 GNUNET_MQ_hd_var_size (get_tunnels,
1167 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNELS,
1168 struct GNUNET_MessageHeader,
1170 GNUNET_MQ_hd_var_size (get_tunnel,
1171 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNEL,
1172 struct GNUNET_CADET_LocalInfoTunnel,
1174 GNUNET_MQ_handler_end ()
1177 GNUNET_assert (NULL == h->mq);
1178 h->mq = GNUNET_CLIENT_connect (h->cfg,
1187 * Function called during #GNUNET_CADET_disconnect() to destroy
1188 * all ports that are still open.
1190 * @param cls the `struct GNUNET_CADET_Handle`
1192 * @param value a `struct GNUNET_CADET_Channel` to destroy
1193 * @return #GNUNET_OK (continue to iterate)
1196 destroy_port_cb (void *cls,
1197 const struct GNUNET_HashCode *id,
1200 /* struct GNUNET_CADET_Handle *handle = cls; */
1201 struct GNUNET_CADET_Port *port = value;
1205 /* This is a warning, the app should have cleanly closed all open ports */
1207 GNUNET_CADET_close_port (port);
1213 * Disconnect from the cadet service. All channels will be destroyed. All channel
1214 * disconnect callbacks will be called on any still connected peers, notifying
1215 * about their disconnection. The registered inbound channel cleaner will be
1216 * called should any inbound channels still exist.
1218 * @param handle connection to cadet to disconnect
1221 GNUNET_CADET_disconnect (struct GNUNET_CADET_Handle *handle)
1223 GNUNET_CONTAINER_multihashmap_iterate (handle->ports,
1226 GNUNET_CONTAINER_multihashmap_destroy (handle->ports);
1227 handle->ports = NULL;
1228 GNUNET_CONTAINER_multihashmap32_iterate (handle->channels,
1229 &destroy_channel_cb,
1231 GNUNET_CONTAINER_multihashmap32_destroy (handle->channels);
1232 handle->channels = NULL;
1233 if (NULL != handle->mq)
1235 GNUNET_MQ_destroy (handle->mq);
1238 if (NULL != handle->reconnect_task)
1240 GNUNET_SCHEDULER_cancel (handle->reconnect_task);
1241 handle->reconnect_task = NULL;
1243 GNUNET_free (handle);
1248 * Close a port opened with @a GNUNET_CADET_open_port().
1249 * The @a new_channel callback will no longer be called.
1251 * @param p Port handle.
1254 GNUNET_CADET_close_port (struct GNUNET_CADET_Port *p)
1256 struct GNUNET_CADET_PortMessage *msg;
1257 struct GNUNET_MQ_Envelope *env;
1259 GNUNET_assert (GNUNET_YES ==
1260 GNUNET_CONTAINER_multihashmap_remove (p->cadet->ports,
1263 env = GNUNET_MQ_msg (msg,
1264 GNUNET_MESSAGE_TYPE_CADET_LOCAL_PORT_CLOSE);
1266 GNUNET_MQ_send (p->cadet->mq,
1268 GNUNET_free_non_null (p->handlers);
1274 * Destroy an existing channel.
1276 * The existing end callback for the channel will be called immediately.
1277 * Any pending outgoing messages will be sent but no incoming messages will be
1278 * accepted and no data callbacks will be called.
1280 * @param channel Channel handle, becomes invalid after this call.
1283 GNUNET_CADET_channel_destroy (struct GNUNET_CADET_Channel *channel)
1285 struct GNUNET_CADET_Handle *h = channel->cadet;
1286 struct GNUNET_CADET_LocalChannelDestroyMessage *msg;
1287 struct GNUNET_MQ_Envelope *env;
1291 env = GNUNET_MQ_msg (msg,
1292 GNUNET_MESSAGE_TYPE_CADET_LOCAL_CHANNEL_DESTROY);
1293 msg->ccn = channel->ccn;
1294 GNUNET_MQ_send (h->mq,
1297 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1298 "Destroying channel due to GNUNET_CADET_channel_destroy()\n");
1299 destroy_channel (channel);
1304 * Get information about a channel.
1306 * @param channel Channel handle.
1307 * @param option Query (GNUNET_CADET_OPTION_*).
1308 * @param ... dependant on option, currently not used
1310 * @return Union with an answer to the query.
1312 const union GNUNET_CADET_ChannelInfo *
1313 GNUNET_CADET_channel_get_info (struct GNUNET_CADET_Channel *channel,
1314 enum GNUNET_CADET_ChannelOption option,
1317 static int bool_flag;
1321 case GNUNET_CADET_OPTION_NOBUFFER:
1322 case GNUNET_CADET_OPTION_RELIABLE:
1323 case GNUNET_CADET_OPTION_OUT_OF_ORDER:
1324 if (0 != (option & channel->options))
1325 bool_flag = GNUNET_YES;
1327 bool_flag = GNUNET_NO;
1328 return (const union GNUNET_CADET_ChannelInfo *) &bool_flag;
1330 case GNUNET_CADET_OPTION_PEER:
1331 return (const union GNUNET_CADET_ChannelInfo *) &channel->peer;
1341 * Send an ack on the channel to confirm the processing of a message.
1343 * @param ch Channel on which to send the ACK.
1346 GNUNET_CADET_receive_done (struct GNUNET_CADET_Channel *channel)
1348 struct GNUNET_CADET_LocalAck *msg;
1349 struct GNUNET_MQ_Envelope *env;
1351 env = GNUNET_MQ_msg (msg,
1352 GNUNET_MESSAGE_TYPE_CADET_LOCAL_ACK);
1353 LOG (GNUNET_ERROR_TYPE_DEBUG,
1354 "Sending ACK on channel %X\n",
1355 ntohl (channel->ccn.channel_of_client));
1356 msg->ccn = channel->ccn;
1357 GNUNET_MQ_send (channel->cadet->mq,
1363 * Send message of @a type to CADET service of @a h
1365 * @param h handle to CADET service
1366 * @param type message type of trivial information request to send
1369 send_info_request (struct GNUNET_CADET_Handle *h,
1372 struct GNUNET_MessageHeader *msg;
1373 struct GNUNET_MQ_Envelope *env;
1375 env = GNUNET_MQ_msg (msg,
1377 GNUNET_MQ_send (h->mq,
1383 * Request a debug dump on the service's STDERR.
1385 * WARNING: unstable API, likely to change in the future!
1387 * @param h cadet handle
1390 GNUNET_CADET_request_dump (struct GNUNET_CADET_Handle *h)
1392 send_info_request (h,
1393 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_DUMP);
1398 * Request information about peers known to the running cadet service.
1399 * The callback will be called for every peer known to the service.
1400 * Only one info request (of any kind) can be active at once.
1402 * WARNING: unstable API, likely to change in the future!
1404 * @param h Handle to the cadet peer.
1405 * @param callback Function to call with the requested data.
1406 * @param callback_cls Closure for @c callback.
1407 * @return #GNUNET_OK / #GNUNET_SYSERR
1410 GNUNET_CADET_get_peers (struct GNUNET_CADET_Handle *h,
1411 GNUNET_CADET_PeersCB callback,
1414 if (NULL != h->info_cb.peers_cb)
1417 return GNUNET_SYSERR;
1419 send_info_request (h,
1420 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEERS);
1421 h->info_cb.peers_cb = callback;
1422 h->info_cls = callback_cls;
1428 * Cancel a peer info request. The callback will not be called (anymore).
1430 * WARNING: unstable API, likely to change in the future!
1432 * @param h Cadet handle.
1433 * @return Closure given to GNUNET_CADET_get_peers().
1436 GNUNET_CADET_get_peers_cancel (struct GNUNET_CADET_Handle *h)
1438 void *cls = h->info_cls;
1440 h->info_cb.peers_cb = NULL;
1447 * Request information about a peer known to the running cadet peer.
1448 * The callback will be called for the tunnel once.
1449 * Only one info request (of any kind) can be active at once.
1451 * WARNING: unstable API, likely to change in the future!
1453 * @param h Handle to the cadet peer.
1454 * @param id Peer whose tunnel to examine.
1455 * @param callback Function to call with the requested data.
1456 * @param callback_cls Closure for @c callback.
1457 * @return #GNUNET_OK / #GNUNET_SYSERR
1460 GNUNET_CADET_get_peer (struct GNUNET_CADET_Handle *h,
1461 const struct GNUNET_PeerIdentity *id,
1462 GNUNET_CADET_PeerCB callback,
1465 struct GNUNET_CADET_LocalInfo *msg;
1466 struct GNUNET_MQ_Envelope *env;
1468 if (NULL != h->info_cb.peer_cb)
1471 return GNUNET_SYSERR;
1473 env = GNUNET_MQ_msg (msg,
1474 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEER);
1476 GNUNET_MQ_send (h->mq,
1478 h->info_cb.peer_cb = callback;
1479 h->info_cls = callback_cls;
1485 * Request information about tunnels of the running cadet peer.
1486 * The callback will be called for every tunnel of the service.
1487 * Only one info request (of any kind) can be active at once.
1489 * WARNING: unstable API, likely to change in the future!
1491 * @param h Handle to the cadet peer.
1492 * @param callback Function to call with the requested data.
1493 * @param callback_cls Closure for @c callback.
1494 * @return #GNUNET_OK / #GNUNET_SYSERR
1497 GNUNET_CADET_get_tunnels (struct GNUNET_CADET_Handle *h,
1498 GNUNET_CADET_TunnelsCB callback,
1501 if (NULL != h->info_cb.tunnels_cb)
1504 return GNUNET_SYSERR;
1506 send_info_request (h,
1507 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNELS);
1508 h->info_cb.tunnels_cb = callback;
1509 h->info_cls = callback_cls;
1515 * Cancel a monitor request. The monitor callback will not be called.
1517 * @param h Cadet handle.
1518 * @return Closure given to GNUNET_CADET_get_tunnels().
1521 GNUNET_CADET_get_tunnels_cancel (struct GNUNET_CADET_Handle *h)
1523 void *cls = h->info_cls;
1525 h->info_cb.tunnels_cb = NULL;
1532 * Request information about a tunnel of the running cadet peer.
1533 * The callback will be called for the tunnel once.
1534 * Only one info request (of any kind) can be active at once.
1536 * WARNING: unstable API, likely to change in the future!
1538 * @param h Handle to the cadet peer.
1539 * @param id Peer whose tunnel to examine.
1540 * @param callback Function to call with the requested data.
1541 * @param callback_cls Closure for @c callback.
1542 * @return #GNUNET_OK / #GNUNET_SYSERR
1545 GNUNET_CADET_get_tunnel (struct GNUNET_CADET_Handle *h,
1546 const struct GNUNET_PeerIdentity *id,
1547 GNUNET_CADET_TunnelCB callback,
1550 struct GNUNET_CADET_LocalInfo *msg;
1551 struct GNUNET_MQ_Envelope *env;
1553 if (NULL != h->info_cb.tunnel_cb)
1556 return GNUNET_SYSERR;
1558 env = GNUNET_MQ_msg (msg,
1559 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNEL);
1561 GNUNET_MQ_send (h->mq,
1563 h->info_cb.tunnel_cb = callback;
1564 h->info_cls = callback_cls;
1570 * Transitional function to convert an unsigned int port to a hash value.
1571 * WARNING: local static value returned, NOT reentrant!
1572 * WARNING: do not use this function for new code!
1574 * @param port Numerical port (unsigned int format).
1576 * @return A GNUNET_HashCode usable for the new CADET API.
1578 const struct GNUNET_HashCode *
1579 GC_u2h (uint32_t port)
1581 static struct GNUNET_HashCode hash;
1583 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1584 "This is a transitional function, use proper crypto hashes as CADET ports\n");
1585 GNUNET_CRYPTO_hash (&port,
1593 * Connect to the MQ-based cadet service.
1595 * @param cfg Configuration to use.
1597 * @return Handle to the cadet service NULL on error.
1599 struct GNUNET_CADET_Handle *
1600 GNUNET_CADET_connect (const struct GNUNET_CONFIGURATION_Handle *cfg)
1602 struct GNUNET_CADET_Handle *h;
1604 LOG (GNUNET_ERROR_TYPE_DEBUG,
1605 "GNUNET_CADET_connect()\n");
1606 h = GNUNET_new (struct GNUNET_CADET_Handle);
1608 h->ports = GNUNET_CONTAINER_multihashmap_create (4,
1610 h->channels = GNUNET_CONTAINER_multihashmap32_create (4);
1615 GNUNET_CADET_disconnect (h);
1618 h->next_ccn.channel_of_client = htonl (GNUNET_CADET_LOCAL_CHANNEL_ID_CLI);
1624 * Open a port to receive incomming MQ-based channels.
1626 * @param h CADET handle.
1627 * @param port Hash identifying the port.
1628 * @param connects Function called when an incoming channel is connected.
1629 * @param connects_cls Closure for the @a connects handler.
1630 * @param window_changes Function called when the transmit window size changes.
1631 * @param disconnects Function called when a channel is disconnected.
1632 * @param handlers Callbacks for messages we care about, NULL-terminated.
1633 * @return Port handle, NULL if port is in use
1635 struct GNUNET_CADET_Port *
1636 GNUNET_CADET_open_port (struct GNUNET_CADET_Handle *h,
1637 const struct GNUNET_HashCode *port,
1638 GNUNET_CADET_ConnectEventHandler connects,
1639 void * connects_cls,
1640 GNUNET_CADET_WindowSizeEventHandler window_changes,
1641 GNUNET_CADET_DisconnectEventHandler disconnects,
1642 const struct GNUNET_MQ_MessageHandler *handlers)
1644 struct GNUNET_CADET_Port *p;
1646 GNUNET_assert (NULL != connects);
1647 GNUNET_assert (NULL != disconnects);
1648 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1649 "Listening to CADET port %s\n",
1652 p = GNUNET_new (struct GNUNET_CADET_Port);
1656 GNUNET_CONTAINER_multihashmap_put (h->ports,
1659 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
1664 p->connects = connects;
1665 p->cls = connects_cls;
1666 p->window_changes = window_changes;
1667 p->disconnects = disconnects;
1668 p->handlers = GNUNET_MQ_copy_handlers (handlers);
1670 GNUNET_assert (GNUNET_OK ==
1679 * Create a new channel towards a remote peer.
1681 * If the destination port is not open by any peer or the destination peer
1682 * does not accept the channel, #GNUNET_CADET_ChannelEndHandler will be called
1685 * @param h CADET handle.
1686 * @param channel_cls Closure for the channel. It's given to:
1687 * - The disconnect handler @a disconnects
1688 * - Each message type callback in @a handlers
1689 * @param destination Peer identity the channel should go to.
1690 * @param port Identification of the destination port.
1691 * @param options CadetOption flag field, with all desired option bits set to 1.
1692 * @param window_changes Function called when the transmit window size changes.
1693 * @param disconnects Function called when the channel is disconnected.
1694 * @param handlers Callbacks for messages we care about, NULL-terminated.
1695 * @return Handle to the channel.
1697 struct GNUNET_CADET_Channel *
1698 GNUNET_CADET_channel_create (struct GNUNET_CADET_Handle *h,
1700 const struct GNUNET_PeerIdentity *destination,
1701 const struct GNUNET_HashCode *port,
1702 enum GNUNET_CADET_ChannelOption options,
1703 GNUNET_CADET_WindowSizeEventHandler window_changes,
1704 GNUNET_CADET_DisconnectEventHandler disconnects,
1705 const struct GNUNET_MQ_MessageHandler *handlers)
1707 struct GNUNET_CADET_Channel *ch;
1708 struct GNUNET_CADET_LocalChannelCreateMessage *msg;
1709 struct GNUNET_MQ_Envelope *env;
1711 GNUNET_assert (NULL != disconnects);
1712 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1713 "Creating channel to peer %s at port %s\n",
1714 GNUNET_i2s (destination),
1716 ch = create_channel (h,
1718 ch->ctx = channel_cls;
1719 ch->peer = *destination;
1720 ch->options = options;
1721 ch->window_changes = window_changes;
1722 ch->disconnects = disconnects;
1724 /* Create MQ for channel */
1725 ch->mq = GNUNET_MQ_queue_for_callbacks (&cadet_mq_send_impl,
1726 &cadet_mq_destroy_impl,
1727 &cadet_mq_cancel_impl,
1730 &cadet_mq_error_handler,
1732 GNUNET_MQ_set_handlers_closure (ch->mq,
1735 /* Request channel creation to service */
1736 env = GNUNET_MQ_msg (msg,
1737 GNUNET_MESSAGE_TYPE_CADET_LOCAL_CHANNEL_CREATE);
1740 msg->peer = *destination;
1741 msg->opt = htonl (options);
1742 GNUNET_MQ_send (h->mq,
1749 * Obtain the message queue for a connected peer.
1751 * @param channel The channel handle from which to get the MQ.
1753 * @return NULL if @a channel is not yet connected.
1755 struct GNUNET_MQ_Handle *
1756 GNUNET_CADET_get_mq (const struct GNUNET_CADET_Channel *channel)
1761 /* end of cadet_api.c */