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
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__)
35 /******************************************************************************/
36 /************************ DATA STRUCTURES ****************************/
37 /******************************************************************************/
40 * Transmission queue to the service
44 struct GNUNET_CADET_TransmitHandle
49 struct GNUNET_CADET_TransmitHandle *next;
54 struct GNUNET_CADET_TransmitHandle *prev;
57 * Channel this message is sent on / for (may be NULL for control messages).
59 struct GNUNET_CADET_Channel *channel;
64 struct GNUNET_SCHEDULER_Task *request_data_task;
67 * Callback to obtain the message to transmit, or NULL if we
68 * got the message in 'data'. Notice that messages built
69 * by 'notify' need to be encapsulated with information about
72 GNUNET_CONNECTION_TransmitReadyNotify notify;
75 * Closure for 'notify'
80 * Size of the payload.
92 GNUNET_CADET_ChannelCB channel_cb;
97 GNUNET_CADET_PeersCB peers_cb;
102 GNUNET_CADET_PeerCB peer_cb;
107 GNUNET_CADET_TunnelsCB tunnels_cb;
112 GNUNET_CADET_TunnelCB tunnel_cb;
117 * Opaque handle to the service.
119 struct GNUNET_CADET_Handle
122 * Flag to indicate old or MQ API.
127 * Message queue (if available).
129 struct GNUNET_MQ_Handle *mq;
132 * Set of handlers used for processing incoming messages in the channels
136 const struct GNUNET_CADET_MessageHandler *message_handlers;
139 * Number of handlers in the handlers array.
143 unsigned int n_handlers;
148 struct GNUNET_CONTAINER_MultiHashMap *ports;
151 * Double linked list of the channels this client is connected to, head.
153 struct GNUNET_CADET_Channel *channels_head;
156 * Double linked list of the channels this client is connected to, tail.
158 struct GNUNET_CADET_Channel *channels_tail;
161 * Callback for inbound channel disconnection
163 GNUNET_CADET_ChannelEndHandler *cleaner;
166 * Closure for all the handlers given by the client
173 * Messages to send to the service, head.
177 struct GNUNET_CADET_TransmitHandle *th_head;
180 * Messages to send to the service, tail.
184 struct GNUNET_CADET_TransmitHandle *th_tail;
187 * child of the next channel to create (to avoid reusing IDs often)
189 struct GNUNET_CADET_ClientChannelNumber next_ccn;
192 * Configuration given by the client, in case of reconnection
194 const struct GNUNET_CONFIGURATION_Handle *cfg;
197 * Time to the next reconnect in case one reconnect fails
199 struct GNUNET_TIME_Relative reconnect_time;
202 * Task for trying to reconnect.
204 struct GNUNET_SCHEDULER_Task * reconnect_task;
207 * Callback for an info task (only one active at a time).
209 union CadetInfoCB info_cb;
212 * Info callback closure for @c info_cb.
219 * Description of a peer
221 struct GNUNET_CADET_Peer
224 * ID of the peer in short form
229 * Channel this peer belongs to
231 struct GNUNET_CADET_Channel *t;
236 * Opaque handle to a channel.
238 struct GNUNET_CADET_Channel
243 struct GNUNET_CADET_Channel *next;
248 struct GNUNET_CADET_Channel *prev;
251 * Handle to the cadet this channel belongs to
253 struct GNUNET_CADET_Handle *cadet;
256 * Local ID of the channel
258 struct GNUNET_CADET_ClientChannelNumber ccn;
261 * Channel's port, if incoming.
263 struct GNUNET_CADET_Port *incoming_port;
266 * Other end of the channel.
271 * Any data the caller wants to put in here
276 * Channel options: reliability, etc.
278 enum GNUNET_CADET_ChannelOption options;
281 * Are we allowed to send to the service?
285 unsigned int allow_send;
287 /****************************************************************************/
288 /***************************** MQ ************************************/
289 /****************************************************************************/
292 * Message Queue for the channel.
294 struct GNUNET_MQ_Handle *mq;
297 * Window change handler.
299 GNUNET_CADET_WindowSizeEventHandler window_changes;
302 * Disconnect handler.
304 GNUNET_CADET_DisconnectEventHandler disconnects;
310 * Opaque handle to a port.
312 struct GNUNET_CADET_Port
315 * Handle to the CADET session this port belongs to.
317 struct GNUNET_CADET_Handle *cadet;
322 struct GNUNET_HashCode *hash;
325 * Callback handler for incoming channels on this port.
327 GNUNET_CADET_InboundChannelNotificationHandler *handler;
330 * Closure for @a handler.
337 * Implementation state for cadet's message queue.
342 * The current transmit handle, or NULL
343 * if no transmit is active.
345 struct GNUNET_CADET_TransmitHandle *th;
348 * Channel to send the data over.
350 struct GNUNET_CADET_Channel *channel;
354 /******************************************************************************/
355 /*********************** AUXILIARY FUNCTIONS *************************/
356 /******************************************************************************/
359 * Check if transmission is a payload packet.
361 * @param th Transmission handle.
363 * @return #GNUNET_YES if it is a payload packet,
364 * #GNUNET_NO if it is a cadet management packet.
367 th_is_payload (struct GNUNET_CADET_TransmitHandle *th)
369 return (th->notify != NULL) ? GNUNET_YES : GNUNET_NO;
374 * Find the Port struct for a hash.
376 * @param h CADET handle.
377 * @param hash HashCode for the port number.
379 * @return The port handle if known, NULL otherwise.
381 static struct GNUNET_CADET_Port *
382 find_port (const struct GNUNET_CADET_Handle *h,
383 const struct GNUNET_HashCode *hash)
385 struct GNUNET_CADET_Port *p;
387 p = GNUNET_CONTAINER_multihashmap_get (h->ports, hash);
394 * Get the channel handler for the channel specified by id from the given handle
396 * @param h Cadet handle
397 * @param ccn ID of the wanted channel
398 * @return handle to the required channel or NULL if not found
400 static struct GNUNET_CADET_Channel *
401 retrieve_channel (struct GNUNET_CADET_Handle *h,
402 struct GNUNET_CADET_ClientChannelNumber ccn)
404 struct GNUNET_CADET_Channel *ch;
406 for (ch = h->channels_head; NULL != ch; ch = ch->next)
407 if (ch->ccn.channel_of_client == ccn.channel_of_client)
414 * Create a new channel and insert it in the channel list of the cadet handle
416 * @param h Cadet handle
417 * @param ccn Desired ccn of the channel, 0 to assign one automatically.
419 * @return Handle to the created channel.
421 static struct GNUNET_CADET_Channel *
422 create_channel (struct GNUNET_CADET_Handle *h,
423 struct GNUNET_CADET_ClientChannelNumber ccn)
425 struct GNUNET_CADET_Channel *ch;
427 ch = GNUNET_new (struct GNUNET_CADET_Channel);
428 GNUNET_CONTAINER_DLL_insert (h->channels_head,
432 if (0 == ccn.channel_of_client)
434 ch->ccn = h->next_ccn;
435 while (NULL != retrieve_channel (h,
438 h->next_ccn.channel_of_client
439 = htonl (1 + ntohl (h->next_ccn.channel_of_client));
440 if (0 == ntohl (h->next_ccn.channel_of_client))
441 h->next_ccn.channel_of_client
442 = htonl (GNUNET_CADET_LOCAL_CHANNEL_ID_CLI);
454 * Destroy the specified channel.
455 * - Destroys all peers, calling the disconnect callback on each if needed
456 * - Cancels all outgoing traffic for that channel, calling respective notifys
457 * - Calls cleaner if channel was inbound
458 * - Frees all memory used
460 * @param ch Pointer to the channel.
461 * @param call_cleaner Whether to call the cleaner handler.
463 * @return Handle to the required channel or NULL if not found.
465 // FIXME: simplify: call_cleaner is always #GNUNET_YES!!!
467 destroy_channel (struct GNUNET_CADET_Channel *ch,
470 struct GNUNET_CADET_Handle *h;
471 struct GNUNET_CADET_TransmitHandle *th;
472 struct GNUNET_CADET_TransmitHandle *next;
480 LOG (GNUNET_ERROR_TYPE_DEBUG,
481 " destroy_channel %X of %p\n",
485 GNUNET_CONTAINER_DLL_remove (h->channels_head,
489 /* signal channel destruction */
490 if ( (NULL != h->cleaner) &&
492 (GNUNET_YES == call_cleaner) )
494 LOG (GNUNET_ERROR_TYPE_DEBUG,
495 " calling cleaner\n");
496 h->cleaner (h->cls, ch, ch->ctx);
499 /* check that clients did not leave messages behind in the queue */
500 for (th = h->th_head; NULL != th; th = next)
503 if (th->channel != ch)
505 /* Clients should have aborted their requests already.
506 * Management traffic should be ok, as clients can't cancel that.
507 * If the service crashed and we are reconnecting, it's ok.
509 GNUNET_break (GNUNET_NO == th_is_payload (th));
510 GNUNET_CADET_notify_transmit_ready_cancel (th);
514 GNUNET_PEER_change_rc (ch->peer, -1);
520 * Add a transmit handle to the transmission queue and set the
523 * @param h cadet handle with the queue head and tail
524 * @param th handle to the packet to be transmitted
527 add_to_queue (struct GNUNET_CADET_Handle *h,
528 struct GNUNET_CADET_TransmitHandle *th)
530 GNUNET_CONTAINER_DLL_insert_tail (h->th_head,
537 * Remove a transmit handle from the transmission queue, if present.
539 * Safe to call even if not queued.
541 * @param th handle to the packet to be unqueued.
544 remove_from_queue (struct GNUNET_CADET_TransmitHandle *th)
546 struct GNUNET_CADET_Handle *h = th->channel->cadet;
548 /* It might or might not have been queued (rarely not), but check anyway. */
549 if (NULL != th->next || h->th_tail == th)
551 GNUNET_CONTAINER_DLL_remove (h->th_head, h->th_tail, th);
557 /******************************************************************************/
558 /*********************** RECEIVE HANDLERS ****************************/
559 /******************************************************************************/
563 * Call the @a notify callback given to #GNUNET_CADET_notify_transmit_ready to
564 * request the data to send over MQ. Since MQ manages the queue, this function
565 * is scheduled immediatly after a transmit ready notification.
567 * @param cls Closure (transmit handle).
570 request_data (void *cls)
572 struct GNUNET_CADET_TransmitHandle *th = cls;
573 struct GNUNET_CADET_LocalData *msg;
574 struct GNUNET_MQ_Envelope *env;
577 LOG (GNUNET_ERROR_TYPE_DEBUG,
578 "Requesting Data: %u bytes (allow send is %u)\n",
580 th->channel->allow_send);
582 GNUNET_assert (0 < th->channel->allow_send);
583 th->channel->allow_send--;
584 /* NOTE: we may be allowed to send another packet immediately,
585 albeit the current logic waits for the ACK. */
586 th->request_data_task = NULL;
587 remove_from_queue (th);
589 env = GNUNET_MQ_msg_extra (msg,
591 GNUNET_MESSAGE_TYPE_CADET_LOCAL_DATA);
592 msg->ccn = th->channel->ccn;
593 osize = th->notify (th->notify_cls,
596 GNUNET_assert (osize == th->size);
598 GNUNET_MQ_send (th->channel->cadet->mq,
605 * Process the new channel notification and add it to the channels in the handle
607 * @param h The cadet handle
608 * @param msg A message with the details of the new incoming channel
611 handle_channel_created (void *cls,
612 const struct GNUNET_CADET_LocalChannelCreateMessage *msg)
614 struct GNUNET_CADET_Handle *h = cls;
615 struct GNUNET_CADET_Channel *ch;
616 struct GNUNET_CADET_Port *port;
617 const struct GNUNET_HashCode *port_number;
618 struct GNUNET_CADET_ClientChannelNumber ccn;
621 port_number = &msg->port;
622 if (ntohl (ccn.channel_of_client) >= GNUNET_CADET_LOCAL_CHANNEL_ID_CLI)
627 port = find_port (h, port_number);
630 struct GNUNET_CADET_LocalChannelDestroyMessage *d_msg;
631 struct GNUNET_MQ_Envelope *env;
634 LOG (GNUNET_ERROR_TYPE_DEBUG,
635 "No handler for incoming channel %X [%s]\n",
636 ntohl (ccn.channel_of_client),
637 GNUNET_h2s (port_number));
638 /* FIXME: should disconnect instead, this is a serious error! */
639 env = GNUNET_MQ_msg (d_msg,
640 GNUNET_MESSAGE_TYPE_CADET_LOCAL_CHANNEL_DESTROY);
641 d_msg->ccn = msg->ccn;
642 GNUNET_MQ_send (h->mq,
647 ch = create_channel (h,
649 ch->peer = GNUNET_PEER_intern (&msg->peer);
652 ch->incoming_port = port;
653 ch->options = ntohl (msg->opt);
655 LOG (GNUNET_ERROR_TYPE_DEBUG,
656 "Creating incoming channel %X [%s] %p\n",
657 ntohl (ccn.channel_of_client),
658 GNUNET_h2s (port_number),
660 ch->ctx = port->handler (port->cls,
669 * Process the channel destroy notification and free associated resources
671 * @param h The cadet handle
672 * @param msg A message with the details of the channel being destroyed
675 handle_channel_destroy (void *cls,
676 const struct GNUNET_CADET_LocalChannelDestroyMessage *msg)
678 struct GNUNET_CADET_Handle *h = cls;
679 struct GNUNET_CADET_Channel *ch;
680 struct GNUNET_CADET_ClientChannelNumber ccn;
683 LOG (GNUNET_ERROR_TYPE_DEBUG,
684 "Channel %X Destroy from service\n",
685 ntohl (ccn.channel_of_client));
686 ch = retrieve_channel (h,
691 LOG (GNUNET_ERROR_TYPE_DEBUG,
692 "channel %X unknown\n",
693 ntohl (ccn.channel_of_client));
702 * Check that message received from CADET service is well-formed.
704 * @param cls the `struct GNUNET_CADET_Handle`
705 * @param message the message we got
706 * @return #GNUNET_OK if the message is well-formed,
707 * #GNUNET_SYSERR otherwise
710 check_local_data (void *cls,
711 const struct GNUNET_CADET_LocalData *message)
713 struct GNUNET_CADET_Handle *h = cls;
714 struct GNUNET_CADET_Channel *ch;
717 size = ntohs (message->header.size);
718 if (sizeof (*message) + sizeof (struct GNUNET_MessageHeader) > size)
721 return GNUNET_SYSERR;
724 ch = retrieve_channel (h,
729 return GNUNET_SYSERR;
737 * Process the incoming data packets, call appropriate handlers.
739 * @param h The cadet handle
740 * @param message A message encapsulating the data
743 handle_local_data (void *cls,
744 const struct GNUNET_CADET_LocalData *message)
746 struct GNUNET_CADET_Handle *h = cls;
747 const struct GNUNET_MessageHeader *payload;
748 const struct GNUNET_CADET_MessageHandler *handler;
749 struct GNUNET_CADET_Channel *ch;
752 ch = retrieve_channel (h,
754 GNUNET_assert (NULL != ch);
756 payload = (struct GNUNET_MessageHeader *) &message[1];
757 type = ntohs (payload->type);
758 LOG (GNUNET_ERROR_TYPE_DEBUG,
759 "Got a %s data on channel %s [%X] of type %s (%u)\n",
760 GC_f2s (ntohl (ch->ccn.channel_of_client) >=
761 GNUNET_CADET_LOCAL_CHANNEL_ID_CLI),
762 GNUNET_i2s (GNUNET_PEER_resolve2 (ch->peer)),
763 ntohl (message->ccn.channel_of_client),
766 for (unsigned i=0;i<h->n_handlers;i++)
768 handler = &h->message_handlers[i];
769 if (handler->type == type)
772 handler->callback (h->cls,
777 LOG (GNUNET_ERROR_TYPE_DEBUG,
778 "callback caused disconnection\n");
779 GNUNET_CADET_channel_destroy (ch);
785 /* Other peer sent message we do not comprehend. */
787 GNUNET_CADET_receive_done (ch);
792 * Process a local ACK message, enabling the client to send
793 * more data to the service.
795 * @param h Cadet handle.
796 * @param message Message itself.
799 handle_local_ack (void *cls,
800 const struct GNUNET_CADET_LocalAck *message)
802 struct GNUNET_CADET_Handle *h = cls;
803 struct GNUNET_CADET_Channel *ch;
804 struct GNUNET_CADET_ClientChannelNumber ccn;
805 struct GNUNET_CADET_TransmitHandle *th;
808 ch = retrieve_channel (h, ccn);
811 LOG (GNUNET_ERROR_TYPE_DEBUG,
812 "ACK on unknown channel %X\n",
813 ntohl (ccn.channel_of_client));
817 LOG (GNUNET_ERROR_TYPE_DEBUG,
818 "Got an ACK on channel %X, allow send now %u!\n",
819 ntohl (ch->ccn.channel_of_client),
821 for (th = h->th_head; NULL != th; th = th->next)
823 if ( (th->channel == ch) &&
824 (NULL == th->request_data_task) )
826 th->request_data_task
827 = GNUNET_SCHEDULER_add_now (&request_data,
835 * Reconnect to the service, retransmit all infomation to try to restore the
838 * @param h handle to the cadet
840 * @return #GNUNET_YES in case of sucess, #GNUNET_NO otherwise (service down...)
843 reconnect (struct GNUNET_CADET_Handle *h);
847 * Reconnect callback: tries to reconnect again after a failer previous
850 * @param cls closure (cadet handle)
853 reconnect_cbk (void *cls);
857 * Generic error handler, called with the appropriate error code and
858 * the same closure specified at the creation of the message queue.
859 * Not every message queue implementation supports an error handler.
861 * @param cls closure, a `struct GNUNET_CORE_Handle *`
862 * @param error error code
865 handle_mq_error (void *cls,
866 enum GNUNET_MQ_Error error)
868 struct GNUNET_CADET_Handle *h = cls;
870 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MQ ERROR: %u\n", error);
871 GNUNET_MQ_destroy (h->mq);
878 * Process a local reply about info on all channels, pass info to the user.
880 * @param h Cadet handle.
881 * @param message Message itself.
884 // process_get_channels (struct GNUNET_CADET_Handle *h,
885 // const struct GNUNET_MessageHeader *message)
887 // struct GNUNET_CADET_LocalInfo *msg;
889 // GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Get Channels messasge received\n");
891 // if (NULL == h->channels_cb)
893 // GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " ignored\n");
897 // msg = (struct GNUNET_CADET_LocalInfo *) message;
898 // if (ntohs (message->size) !=
899 // (sizeof (struct GNUNET_CADET_LocalInfo) +
900 // sizeof (struct GNUNET_PeerIdentity)))
902 // GNUNET_break_op (0);
903 // GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
904 // "Get channels message: size %hu - expected %u\n",
905 // ntohs (message->size),
906 // sizeof (struct GNUNET_CADET_LocalInfo));
909 // h->channels_cb (h->channels_cls,
910 // ntohl (msg->channel_id),
912 // &msg->destination);
918 * Process a local monitor_channel reply, pass info to the user.
920 * @param h Cadet handle.
921 * @param message Message itself.
924 // process_show_channel (struct GNUNET_CADET_Handle *h,
925 // const struct GNUNET_MessageHeader *message)
927 // struct GNUNET_CADET_LocalInfo *msg;
930 // GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Show Channel messasge received\n");
932 // if (NULL == h->channel_cb)
934 // GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " ignored\n");
938 // /* Verify message sanity */
939 // msg = (struct GNUNET_CADET_LocalInfo *) message;
940 // esize = sizeof (struct GNUNET_CADET_LocalInfo);
941 // if (ntohs (message->size) != esize)
943 // GNUNET_break_op (0);
944 // GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
945 // "Show channel message: size %hu - expected %u\n",
946 // ntohs (message->size),
949 // h->channel_cb (h->channel_cls, NULL, NULL);
950 // h->channel_cb = NULL;
951 // h->channel_cls = NULL;
956 // h->channel_cb (h->channel_cls,
957 // &msg->destination,
964 * Check that message received from CADET service is well-formed.
966 * @param cls the `struct GNUNET_CADET_Handle`
967 * @param message the message we got
968 * @return #GNUNET_OK if the message is well-formed,
969 * #GNUNET_SYSERR otherwise
972 check_get_peers (void *cls,
973 const struct GNUNET_CADET_LocalInfoPeer *message)
975 struct GNUNET_CADET_Handle *h = cls;
978 if (NULL == h->info_cb.peers_cb)
980 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
981 " no handler for peesr monitor message!\n");
982 return GNUNET_SYSERR;
985 size = ntohs (message->header.size);
986 if (sizeof (struct GNUNET_CADET_LocalInfoPeer) > size)
988 h->info_cb.peers_cb (h->info_cls, NULL, -1, 0, 0);
989 h->info_cb.peers_cb = NULL;
991 return GNUNET_SYSERR;
999 * Process a local reply about info on all tunnels, pass info to the user.
1001 * @param cls Closure (Cadet handle).
1002 * @param msg Message itself.
1005 handle_get_peers (void *cls,
1006 const struct GNUNET_CADET_LocalInfoPeer *msg)
1008 struct GNUNET_CADET_Handle *h = cls;
1009 h->info_cb.peers_cb (h->info_cls, &msg->destination,
1010 (int) ntohs (msg->tunnel),
1011 (unsigned int ) ntohs (msg->paths),
1017 * Check that message received from CADET service is well-formed.
1019 * @param cls the `struct GNUNET_CADET_Handle`
1020 * @param message the message we got
1021 * @return #GNUNET_OK if the message is well-formed,
1022 * #GNUNET_SYSERR otherwise
1025 check_get_peer (void *cls,
1026 const struct GNUNET_CADET_LocalInfoPeer *message)
1028 struct GNUNET_CADET_Handle *h = cls;
1029 const size_t msize = sizeof (struct GNUNET_CADET_LocalInfoPeer);
1030 struct GNUNET_PeerIdentity *paths_array;
1032 unsigned int epaths;
1036 if (NULL == h->info_cb.peer_cb)
1038 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1039 " no handler for peer monitor message!\n");
1043 /* Verify message sanity */
1044 esize = ntohs (message->header.size);
1047 GNUNET_break_op (0);
1048 h->info_cb.peer_cb (h->info_cls, NULL, 0, 0, 0, NULL);
1051 if (0 != ((esize - msize) % sizeof (struct GNUNET_PeerIdentity)))
1053 GNUNET_break_op (0);
1054 h->info_cb.peer_cb (h->info_cls, NULL, 0, 0, 0, NULL);
1058 peers = (esize - msize) / sizeof (struct GNUNET_PeerIdentity);
1059 epaths = (unsigned int) ntohs (message->paths);
1060 paths_array = (struct GNUNET_PeerIdentity *) &message[1];
1062 for (int i = 0; i < peers; i++)
1064 if (0 == memcmp (&paths_array[i], &message->destination,
1065 sizeof (struct GNUNET_PeerIdentity)))
1070 if (paths != epaths)
1072 GNUNET_break_op (0);
1073 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "p:%u, e: %u\n", paths, epaths);
1074 h->info_cb.peer_cb (h->info_cls, NULL, 0, 0, 0, NULL);
1081 h->info_cb.peer_cb = NULL;
1083 return GNUNET_SYSERR;
1088 * Process a local peer info reply, pass info to the user.
1090 * @param cls Closure (Cadet handle).
1091 * @param message Message itself.
1094 handle_get_peer (void *cls,
1095 const struct GNUNET_CADET_LocalInfoPeer *message)
1097 struct GNUNET_CADET_Handle *h = cls;
1098 struct GNUNET_PeerIdentity *paths_array;
1100 unsigned int path_length;
1104 paths = (unsigned int) ntohs (message->paths);
1105 paths_array = (struct GNUNET_PeerIdentity *) &message[1];
1106 peers = (ntohs (message->header.size) - sizeof (*message))
1107 / sizeof (struct GNUNET_PeerIdentity);
1109 neighbor = GNUNET_NO;
1111 for (int i = 0; i < peers; i++)
1113 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " %s\n", GNUNET_i2s (&paths_array[i]));
1115 if (0 == memcmp (&paths_array[i], &message->destination,
1116 sizeof (struct GNUNET_PeerIdentity)))
1118 if (1 == path_length)
1119 neighbor = GNUNET_YES;
1124 /* Call Callback with tunnel info. */
1125 paths_array = (struct GNUNET_PeerIdentity *) &message[1];
1126 h->info_cb.peer_cb (h->info_cls,
1127 &message->destination,
1128 (int) ntohs (message->tunnel),
1136 * Check that message received from CADET service is well-formed.
1138 * @param cls the `struct GNUNET_CADET_Handle`
1139 * @param msg the message we got
1140 * @return #GNUNET_OK if the message is well-formed,
1141 * #GNUNET_SYSERR otherwise
1144 check_get_tunnels (void *cls,
1145 const struct GNUNET_CADET_LocalInfoTunnel *msg)
1147 struct GNUNET_CADET_Handle *h = cls;
1150 if (NULL == h->info_cb.tunnels_cb)
1152 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1153 " no handler for tunnels monitor message!\n");
1154 return GNUNET_SYSERR;
1157 size = ntohs (msg->header.size);
1158 if (sizeof (struct GNUNET_CADET_LocalInfoTunnel) > size)
1160 h->info_cb.tunnels_cb (h->info_cls, NULL, 0, 0, 0, 0);
1161 h->info_cb.tunnels_cb = NULL;
1163 return GNUNET_SYSERR;
1170 * Process a local reply about info on all tunnels, pass info to the user.
1172 * @param cls Closure (Cadet handle).
1173 * @param message Message itself.
1176 handle_get_tunnels (void *cls,
1177 const struct GNUNET_CADET_LocalInfoTunnel *msg)
1179 struct GNUNET_CADET_Handle *h = cls;
1181 h->info_cb.tunnels_cb (h->info_cls,
1183 ntohl (msg->channels),
1184 ntohl (msg->connections),
1185 ntohs (msg->estate),
1186 ntohs (msg->cstate));
1192 * Check that message received from CADET service is well-formed.
1194 * @param cls the `struct GNUNET_CADET_Handle`
1195 * @param msg the message we got
1196 * @return #GNUNET_OK if the message is well-formed,
1197 * #GNUNET_SYSERR otherwise
1200 check_get_tunnel (void *cls,
1201 const struct GNUNET_CADET_LocalInfoTunnel *msg)
1203 struct GNUNET_CADET_Handle *h = cls;
1209 if (NULL == h->info_cb.tunnel_cb)
1211 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1212 " no handler for tunnel monitor message!\n");
1216 /* Verify message sanity */
1217 msize = ntohs (msg->header.size);
1218 esize = sizeof (struct GNUNET_CADET_LocalInfoTunnel);
1221 GNUNET_break_op (0);
1222 h->info_cb.tunnel_cb (h->info_cls,
1223 NULL, 0, 0, NULL, NULL, 0, 0);
1226 ch_n = ntohl (msg->channels);
1227 c_n = ntohl (msg->connections);
1228 esize += ch_n * sizeof (struct GNUNET_CADET_ChannelTunnelNumber);
1229 esize += c_n * sizeof (struct GNUNET_CADET_ConnectionTunnelIdentifier);
1232 GNUNET_break_op (0);
1233 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1234 "m:%u, e: %u (%u ch, %u conn)\n",
1235 (unsigned int) msize,
1236 (unsigned int) esize,
1239 h->info_cb.tunnel_cb (h->info_cls,
1240 NULL, 0, 0, NULL, NULL, 0, 0);
1247 h->info_cb.tunnel_cb = NULL;
1249 return GNUNET_SYSERR;
1254 * Process a local tunnel info reply, pass info to the user.
1256 * @param cls Closure (Cadet handle).
1257 * @param msg Message itself.
1260 handle_get_tunnel (void *cls,
1261 const struct GNUNET_CADET_LocalInfoTunnel *msg)
1263 struct GNUNET_CADET_Handle *h = cls;
1266 const struct GNUNET_CADET_ConnectionTunnelIdentifier *conns;
1267 const struct GNUNET_CADET_ChannelTunnelNumber *chns;
1269 ch_n = ntohl (msg->channels);
1270 c_n = ntohl (msg->connections);
1272 /* Call Callback with tunnel info. */
1273 conns = (const struct GNUNET_CADET_ConnectionTunnelIdentifier *) &msg[1];
1274 chns = (const struct GNUNET_CADET_ChannelTunnelNumber *) &conns[c_n];
1275 h->info_cb.tunnel_cb (h->info_cls,
1281 ntohs (msg->estate),
1282 ntohs (msg->cstate));
1288 * Reconnect to the service, retransmit all infomation to try to restore the
1291 * @param h handle to the cadet
1292 * @return #GNUNET_YES in case of success, #GNUNET_NO otherwise (service down...)
1295 do_reconnect (struct GNUNET_CADET_Handle *h)
1297 struct GNUNET_MQ_MessageHandler handlers[] = {
1298 GNUNET_MQ_hd_fixed_size (channel_created,
1299 GNUNET_MESSAGE_TYPE_CADET_LOCAL_CHANNEL_CREATE,
1300 struct GNUNET_CADET_LocalChannelCreateMessage,
1302 GNUNET_MQ_hd_fixed_size (channel_destroy,
1303 GNUNET_MESSAGE_TYPE_CADET_LOCAL_CHANNEL_DESTROY,
1304 struct GNUNET_CADET_LocalChannelDestroyMessage,
1306 GNUNET_MQ_hd_var_size (local_data,
1307 GNUNET_MESSAGE_TYPE_CADET_LOCAL_DATA,
1308 struct GNUNET_CADET_LocalData,
1310 GNUNET_MQ_hd_fixed_size (local_ack,
1311 GNUNET_MESSAGE_TYPE_CADET_LOCAL_ACK,
1312 struct GNUNET_CADET_LocalAck,
1314 GNUNET_MQ_hd_var_size (get_peers,
1315 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEERS,
1316 struct GNUNET_CADET_LocalInfoPeer,
1318 GNUNET_MQ_hd_var_size (get_peer,
1319 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEER,
1320 struct GNUNET_CADET_LocalInfoPeer,
1322 GNUNET_MQ_hd_var_size (get_tunnels,
1323 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNELS,
1324 struct GNUNET_CADET_LocalInfoTunnel,
1326 GNUNET_MQ_hd_var_size (get_tunnel,
1327 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNEL,
1328 struct GNUNET_CADET_LocalInfoTunnel,
1331 // GNUNET_MQ_hd_fixed_Y size (channel_destroyed,
1332 // GNUNET_MESSAGE_TYPE_CADET_CHANNEL_OPEN_NACK_DEPRECATED,
1333 // struct GNUNET_CADET_ChannelDestroyMessage);
1334 GNUNET_MQ_handler_end ()
1337 LOG (GNUNET_ERROR_TYPE_DEBUG, "Connecting to CADET\n");
1339 GNUNET_assert (NULL == h->mq);
1340 h->mq = GNUNET_CLIENT_connect (h->cfg,
1352 h->reconnect_time = GNUNET_TIME_UNIT_MILLISECONDS;
1358 * Reconnect callback: tries to reconnect again after a failer previous
1361 * @param cls closure (cadet handle)
1364 reconnect_cbk (void *cls)
1366 struct GNUNET_CADET_Handle *h = cls;
1368 h->reconnect_task = NULL;
1374 * Reconnect to the service, retransmit all infomation to try to restore the
1377 * @param h handle to the cadet
1379 * @return #GNUNET_YES in case of sucess, #GNUNET_NO otherwise (service down...)
1382 reconnect (struct GNUNET_CADET_Handle *h)
1384 struct GNUNET_CADET_Channel *ch;
1386 LOG (GNUNET_ERROR_TYPE_DEBUG,
1387 "Requested RECONNECT, destroying all channels\n");
1388 while (NULL != (ch = h->channels_head))
1389 destroy_channel (ch, GNUNET_YES);
1390 if (NULL == h->reconnect_task)
1391 h->reconnect_task = GNUNET_SCHEDULER_add_delayed (h->reconnect_time,
1396 /******************************************************************************/
1397 /********************** API CALL DEFINITIONS *************************/
1398 /******************************************************************************/
1400 struct GNUNET_CADET_Handle *
1401 GNUNET_CADET_connect (const struct GNUNET_CONFIGURATION_Handle *cfg,
1403 GNUNET_CADET_ChannelEndHandler cleaner,
1404 const struct GNUNET_CADET_MessageHandler *handlers)
1406 struct GNUNET_CADET_Handle *h;
1408 h = GNUNET_new (struct GNUNET_CADET_Handle);
1409 LOG (GNUNET_ERROR_TYPE_DEBUG,
1410 "GNUNET_CADET_connect() %p\n",
1413 h->cleaner = cleaner;
1414 h->ports = GNUNET_CONTAINER_multihashmap_create (4, GNUNET_YES);
1419 GNUNET_CADET_disconnect (h);
1423 h->message_handlers = handlers;
1424 h->next_ccn.channel_of_client = htonl (GNUNET_CADET_LOCAL_CHANNEL_ID_CLI);
1425 h->reconnect_time = GNUNET_TIME_UNIT_MILLISECONDS;
1426 h->reconnect_task = NULL;
1428 /* count handlers */
1429 for (h->n_handlers = 0;
1430 handlers && handlers[h->n_handlers].type;
1437 * Disconnect from the cadet service. All channels will be destroyed. All channel
1438 * disconnect callbacks will be called on any still connected peers, notifying
1439 * about their disconnection. The registered inbound channel cleaner will be
1440 * called should any inbound channels still exist.
1442 * @param handle connection to cadet to disconnect
1445 GNUNET_CADET_disconnect (struct GNUNET_CADET_Handle *handle)
1447 struct GNUNET_CADET_Channel *ch;
1448 struct GNUNET_CADET_Channel *aux;
1449 struct GNUNET_CADET_TransmitHandle *th;
1451 LOG (GNUNET_ERROR_TYPE_DEBUG,
1452 "CADET DISCONNECT\n");
1453 ch = handle->channels_head;
1457 if (ntohl (ch->ccn.channel_of_client) >= GNUNET_CADET_LOCAL_CHANNEL_ID_CLI)
1460 LOG (GNUNET_ERROR_TYPE_DEBUG,
1461 "channel %X not destroyed\n",
1462 ntohl (ch->ccn.channel_of_client));
1464 destroy_channel (ch,
1468 while (NULL != (th = handle->th_head))
1470 struct GNUNET_MessageHeader *msg;
1472 /* Make sure it is an allowed packet (everything else should have been
1473 * already canceled).
1475 GNUNET_break (GNUNET_NO == th_is_payload (th));
1476 msg = (struct GNUNET_MessageHeader *) &th[1];
1477 switch (ntohs(msg->type))
1479 case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_OPEN:
1480 case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DESTROY:
1481 case GNUNET_MESSAGE_TYPE_CADET_LOCAL_PORT_OPEN:
1482 case GNUNET_MESSAGE_TYPE_CADET_LOCAL_PORT_CLOSE:
1483 case GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_CHANNELS:
1484 case GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_CHANNEL:
1485 case GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEER:
1486 case GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEERS:
1487 case GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNEL:
1488 case GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNELS:
1492 LOG (GNUNET_ERROR_TYPE_ERROR, "unexpected unsent msg %s\n",
1493 GC_m2s (ntohs(msg->type)));
1496 GNUNET_CADET_notify_transmit_ready_cancel (th);
1499 if (NULL != handle->mq)
1501 GNUNET_MQ_destroy (handle->mq);
1504 if (NULL != handle->reconnect_task)
1506 GNUNET_SCHEDULER_cancel(handle->reconnect_task);
1507 handle->reconnect_task = NULL;
1510 GNUNET_CONTAINER_multihashmap_destroy (handle->ports);
1511 handle->ports = NULL;
1512 GNUNET_free (handle);
1517 * Open a port to receive incomming channels.
1519 * @param h CADET handle.
1520 * @param port Hash representing the port number.
1521 * @param new_channel Function called when an channel is received.
1522 * @param new_channel_cls Closure for @a new_channel.
1523 * @return Port handle.
1525 struct GNUNET_CADET_Port *
1526 GNUNET_CADET_open_port (struct GNUNET_CADET_Handle *h,
1527 const struct GNUNET_HashCode *port,
1528 GNUNET_CADET_InboundChannelNotificationHandler
1530 void *new_channel_cls)
1532 struct GNUNET_CADET_PortMessage *msg;
1533 struct GNUNET_MQ_Envelope *env;
1534 struct GNUNET_CADET_Port *p;
1536 GNUNET_assert (NULL != new_channel);
1537 p = GNUNET_new (struct GNUNET_CADET_Port);
1539 p->hash = GNUNET_new (struct GNUNET_HashCode);
1541 p->handler = new_channel;
1542 p->cls = new_channel_cls;
1543 GNUNET_assert (GNUNET_OK ==
1544 GNUNET_CONTAINER_multihashmap_put (h->ports,
1547 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1549 env = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_CADET_LOCAL_PORT_OPEN);
1550 msg->port = *p->hash;
1551 GNUNET_MQ_send (h->mq, env);
1557 * Close a port opened with @a GNUNET_CADET_open_port.
1558 * The @a new_channel callback will no longer be called.
1560 * @param p Port handle.
1563 GNUNET_CADET_close_port (struct GNUNET_CADET_Port *p)
1565 struct GNUNET_CADET_PortMessage *msg;
1566 struct GNUNET_MQ_Envelope *env;
1568 env = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_CADET_LOCAL_PORT_CLOSE);
1570 msg->port = *p->hash;
1571 GNUNET_MQ_send (p->cadet->mq, env);
1572 GNUNET_CONTAINER_multihashmap_remove (p->cadet->ports, p->hash, p);
1573 GNUNET_free (p->hash);
1579 * Create a new channel towards a remote peer.
1581 * If the destination port is not open by any peer or the destination peer
1582 * does not accept the channel, #GNUNET_CADET_ChannelEndHandler will be called
1585 * @param h cadet handle
1586 * @param channel_ctx client's channel context to associate with the channel
1587 * @param peer peer identity the channel should go to
1588 * @param port Port hash (port number).
1589 * @param options CadetOption flag field, with all desired option bits set to 1.
1590 * @return handle to the channel
1592 struct GNUNET_CADET_Channel *
1593 GNUNET_CADET_channel_create (struct GNUNET_CADET_Handle *h,
1595 const struct GNUNET_PeerIdentity *peer,
1596 const struct GNUNET_HashCode *port,
1597 enum GNUNET_CADET_ChannelOption options)
1599 struct GNUNET_CADET_LocalChannelCreateMessage *msg;
1600 struct GNUNET_MQ_Envelope *env;
1601 struct GNUNET_CADET_Channel *ch;
1602 struct GNUNET_CADET_ClientChannelNumber ccn;
1604 ccn.channel_of_client = htonl (0);
1605 ch = create_channel (h, ccn);
1606 ch->ctx = channel_ctx;
1607 ch->peer = GNUNET_PEER_intern (peer);
1609 LOG (GNUNET_ERROR_TYPE_DEBUG,
1610 "Creating new channel to %s:%u at %p number %X\n",
1614 ntohl (ch->ccn.channel_of_client));
1615 env = GNUNET_MQ_msg (msg,
1616 GNUNET_MESSAGE_TYPE_CADET_LOCAL_CHANNEL_CREATE);
1620 msg->opt = htonl (options);
1621 GNUNET_MQ_send (h->mq,
1628 GNUNET_CADET_channel_destroy (struct GNUNET_CADET_Channel *channel)
1630 struct GNUNET_CADET_Handle *h;
1631 struct GNUNET_CADET_LocalChannelDestroyMessage *msg;
1632 struct GNUNET_MQ_Envelope *env;
1633 struct GNUNET_CADET_TransmitHandle *th;
1634 struct GNUNET_CADET_TransmitHandle *next;
1636 LOG (GNUNET_ERROR_TYPE_DEBUG,
1637 "Destroying channel\n");
1639 for (th = h->th_head; th != NULL; th = next)
1642 if (th->channel == channel)
1645 if (GNUNET_YES == th_is_payload (th))
1647 /* applications should cancel before destroying channel */
1648 LOG (GNUNET_ERROR_TYPE_WARNING,
1649 "Channel destroyed without cancelling transmission requests\n");
1650 th->notify (th->notify_cls, 0, NULL);
1654 LOG (GNUNET_ERROR_TYPE_WARNING,
1655 "no meta-traffic should be queued\n");
1657 GNUNET_CONTAINER_DLL_remove (h->th_head,
1660 GNUNET_CADET_notify_transmit_ready_cancel (th);
1664 env = GNUNET_MQ_msg (msg,
1665 GNUNET_MESSAGE_TYPE_CADET_LOCAL_CHANNEL_DESTROY);
1666 msg->ccn = channel->ccn;
1667 GNUNET_MQ_send (h->mq,
1670 destroy_channel (channel,
1676 * Get information about a channel.
1678 * @param channel Channel handle.
1679 * @param option Query (GNUNET_CADET_OPTION_*).
1680 * @param ... dependant on option, currently not used
1682 * @return Union with an answer to the query.
1684 const union GNUNET_CADET_ChannelInfo *
1685 GNUNET_CADET_channel_get_info (struct GNUNET_CADET_Channel *channel,
1686 enum GNUNET_CADET_ChannelOption option, ...)
1688 static int bool_flag;
1689 const union GNUNET_CADET_ChannelInfo *ret;
1693 case GNUNET_CADET_OPTION_NOBUFFER:
1694 case GNUNET_CADET_OPTION_RELIABLE:
1695 case GNUNET_CADET_OPTION_OUT_OF_ORDER:
1696 if (0 != (option & channel->options))
1697 bool_flag = GNUNET_YES;
1699 bool_flag = GNUNET_NO;
1700 ret = (const union GNUNET_CADET_ChannelInfo *) &bool_flag;
1702 case GNUNET_CADET_OPTION_PEER:
1703 ret = (const union GNUNET_CADET_ChannelInfo *) GNUNET_PEER_resolve2 (channel->peer);
1714 struct GNUNET_CADET_TransmitHandle *
1715 GNUNET_CADET_notify_transmit_ready (struct GNUNET_CADET_Channel *channel,
1717 struct GNUNET_TIME_Relative maxdelay,
1719 GNUNET_CONNECTION_TransmitReadyNotify notify,
1722 struct GNUNET_CADET_TransmitHandle *th;
1724 GNUNET_assert (NULL != channel);
1725 GNUNET_assert (NULL != notify);
1726 GNUNET_assert (GNUNET_CONSTANTS_MAX_CADET_MESSAGE_SIZE >= notify_size);
1727 LOG (GNUNET_ERROR_TYPE_DEBUG,
1728 "CADET NOTIFY TRANSMIT READY on channel %X allow_send is %u to %s with %u bytes\n",
1729 ntohl (channel->ccn.channel_of_client),
1730 channel->allow_send,
1731 (ntohl (channel->ccn.channel_of_client) >=
1732 GNUNET_CADET_LOCAL_CHANNEL_ID_CLI)
1735 (unsigned int) notify_size);
1736 if (GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us != maxdelay.rel_value_us)
1738 LOG (GNUNET_ERROR_TYPE_WARNING,
1739 "CADET transmit ready timeout is deprected (has no effect)\n");
1742 th = GNUNET_new (struct GNUNET_CADET_TransmitHandle);
1743 th->channel = channel;
1744 th->size = notify_size;
1745 th->notify = notify;
1746 th->notify_cls = notify_cls;
1747 if (0 != channel->allow_send)
1748 th->request_data_task
1749 = GNUNET_SCHEDULER_add_now (&request_data,
1752 add_to_queue (channel->cadet,
1759 GNUNET_CADET_notify_transmit_ready_cancel (struct GNUNET_CADET_TransmitHandle *th)
1761 if (NULL != th->request_data_task)
1763 GNUNET_SCHEDULER_cancel (th->request_data_task);
1764 th->request_data_task = NULL;
1766 remove_from_queue (th);
1772 * Send an ack on the channel to confirm the processing of a message.
1774 * @param ch Channel on which to send the ACK.
1777 GNUNET_CADET_receive_done (struct GNUNET_CADET_Channel *channel)
1779 struct GNUNET_CADET_LocalAck *msg;
1780 struct GNUNET_MQ_Envelope *env;
1782 env = GNUNET_MQ_msg (msg,
1783 GNUNET_MESSAGE_TYPE_CADET_LOCAL_ACK);
1784 LOG (GNUNET_ERROR_TYPE_DEBUG,
1785 "Sending ACK on channel %X\n",
1786 ntohl (channel->ccn.channel_of_client));
1787 msg->ccn = channel->ccn;
1788 GNUNET_MQ_send (channel->cadet->mq,
1794 send_info_request (struct GNUNET_CADET_Handle *h, uint16_t type)
1796 struct GNUNET_MessageHeader *msg;
1797 struct GNUNET_MQ_Envelope *env;
1799 LOG (GNUNET_ERROR_TYPE_DEBUG,
1800 " Sending %s monitor message to service\n",
1803 env = GNUNET_MQ_msg (msg, type);
1804 GNUNET_MQ_send (h->mq, env);
1809 * Request a debug dump on the service's STDERR.
1811 * WARNING: unstable API, likely to change in the future!
1813 * @param h cadet handle
1816 GNUNET_CADET_request_dump (struct GNUNET_CADET_Handle *h)
1818 LOG (GNUNET_ERROR_TYPE_DEBUG, "requesting dump\n");
1819 send_info_request (h, GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_DUMP);
1824 * Request information about peers known to the running cadet service.
1825 * The callback will be called for every peer known to the service.
1826 * Only one info request (of any kind) can be active at once.
1829 * WARNING: unstable API, likely to change in the future!
1831 * @param h Handle to the cadet peer.
1832 * @param callback Function to call with the requested data.
1833 * @param callback_cls Closure for @c callback.
1835 * @return #GNUNET_OK / #GNUNET_SYSERR
1838 GNUNET_CADET_get_peers (struct GNUNET_CADET_Handle *h,
1839 GNUNET_CADET_PeersCB callback,
1842 if (NULL != h->info_cb.peers_cb)
1845 return GNUNET_SYSERR;
1847 send_info_request (h, GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEERS);
1848 h->info_cb.peers_cb = callback;
1849 h->info_cls = callback_cls;
1855 * Cancel a peer info request. The callback will not be called (anymore).
1857 * WARNING: unstable API, likely to change in the future!
1859 * @param h Cadet handle.
1861 * @return Closure given to GNUNET_CADET_get_peers.
1864 GNUNET_CADET_get_peers_cancel (struct GNUNET_CADET_Handle *h)
1869 h->info_cb.peers_cb = NULL;
1876 * Request information about a peer known to the running cadet peer.
1877 * The callback will be called for the tunnel once.
1878 * Only one info request (of any kind) can be active at once.
1880 * WARNING: unstable API, likely to change in the future!
1882 * @param h Handle to the cadet peer.
1883 * @param id Peer whose tunnel to examine.
1884 * @param callback Function to call with the requested data.
1885 * @param callback_cls Closure for @c callback.
1887 * @return #GNUNET_OK / #GNUNET_SYSERR
1890 GNUNET_CADET_get_peer (struct GNUNET_CADET_Handle *h,
1891 const struct GNUNET_PeerIdentity *id,
1892 GNUNET_CADET_PeerCB callback,
1895 struct GNUNET_CADET_LocalInfo *msg;
1896 struct GNUNET_MQ_Envelope *env;
1898 if (NULL != h->info_cb.peer_cb)
1901 return GNUNET_SYSERR;
1904 env = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEER);
1906 GNUNET_MQ_send (h->mq, env);
1908 h->info_cb.peer_cb = callback;
1909 h->info_cls = callback_cls;
1915 * Request information about tunnels of the running cadet peer.
1916 * The callback will be called for every tunnel of the service.
1917 * Only one info request (of any kind) can be active at once.
1919 * WARNING: unstable API, likely to change in the future!
1921 * @param h Handle to the cadet peer.
1922 * @param callback Function to call with the requested data.
1923 * @param callback_cls Closure for @c callback.
1925 * @return #GNUNET_OK / #GNUNET_SYSERR
1928 GNUNET_CADET_get_tunnels (struct GNUNET_CADET_Handle *h,
1929 GNUNET_CADET_TunnelsCB callback,
1932 if (NULL != h->info_cb.tunnels_cb)
1935 return GNUNET_SYSERR;
1937 send_info_request (h, GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNELS);
1938 h->info_cb.tunnels_cb = callback;
1939 h->info_cls = callback_cls;
1945 * Cancel a monitor request. The monitor callback will not be called.
1947 * @param h Cadet handle.
1949 * @return Closure given to GNUNET_CADET_get_tunnels.
1952 GNUNET_CADET_get_tunnels_cancel (struct GNUNET_CADET_Handle *h)
1956 h->info_cb.tunnels_cb = NULL;
1966 * Request information about a tunnel of the running cadet peer.
1967 * The callback will be called for the tunnel once.
1968 * Only one info request (of any kind) can be active at once.
1970 * WARNING: unstable API, likely to change in the future!
1972 * @param h Handle to the cadet peer.
1973 * @param id Peer whose tunnel to examine.
1974 * @param callback Function to call with the requested data.
1975 * @param callback_cls Closure for @c callback.
1977 * @return #GNUNET_OK / #GNUNET_SYSERR
1980 GNUNET_CADET_get_tunnel (struct GNUNET_CADET_Handle *h,
1981 const struct GNUNET_PeerIdentity *id,
1982 GNUNET_CADET_TunnelCB callback,
1985 struct GNUNET_CADET_LocalInfo *msg;
1986 struct GNUNET_MQ_Envelope *env;
1988 if (NULL != h->info_cb.tunnel_cb)
1991 return GNUNET_SYSERR;
1994 env = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNEL);
1996 GNUNET_MQ_send (h->mq, env);
1998 h->info_cb.tunnel_cb = callback;
1999 h->info_cls = callback_cls;
2005 * Request information about a specific channel of the running cadet peer.
2007 * WARNING: unstable API, likely to change in the future!
2008 * FIXME Add destination option.
2010 * @param h Handle to the cadet peer.
2011 * @param initiator ID of the owner of the channel.
2012 * @param channel_number Channel number.
2013 * @param callback Function to call with the requested data.
2014 * @param callback_cls Closure for @c callback.
2016 * @return #GNUNET_OK / #GNUNET_SYSERR
2019 GNUNET_CADET_show_channel (struct GNUNET_CADET_Handle *h,
2020 struct GNUNET_PeerIdentity *initiator,
2021 unsigned int channel_number,
2022 GNUNET_CADET_ChannelCB callback,
2025 struct GNUNET_CADET_LocalInfo *msg;
2026 struct GNUNET_MQ_Envelope *env;
2028 if (NULL != h->info_cb.channel_cb)
2031 return GNUNET_SYSERR;
2034 env = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_CHANNEL);
2035 msg->peer = *initiator;
2036 msg->ccn.channel_of_client = htonl (channel_number);
2037 GNUNET_MQ_send (h->mq, env);
2039 h->info_cb.channel_cb = callback;
2040 h->info_cls = callback_cls;
2046 * Function called to notify a client about the connection
2047 * begin ready to queue more data. "buf" will be
2048 * NULL and "size" zero if the connection was closed for
2049 * writing in the meantime.
2051 * @param cls closure
2052 * @param size number of bytes available in buf
2053 * @param buf where the callee should write the message
2054 * @return number of bytes written to buf
2057 cadet_mq_ntr (void *cls, size_t size,
2060 struct GNUNET_MQ_Handle *mq = cls;
2061 struct CadetMQState *state = GNUNET_MQ_impl_state (mq);
2062 const struct GNUNET_MessageHeader *msg = GNUNET_MQ_impl_current (mq);
2068 GNUNET_MQ_inject_error (mq, GNUNET_MQ_ERROR_WRITE);
2071 msize = ntohs (msg->size);
2072 GNUNET_assert (msize <= size);
2073 GNUNET_memcpy (buf, msg, msize);
2074 GNUNET_MQ_impl_send_continue (mq);
2080 * Signature of functions implementing the
2081 * sending functionality of a message queue.
2083 * @param mq the message queue
2084 * @param msg the message to send
2085 * @param impl_state state of the implementation
2088 cadet_mq_send_impl_old (struct GNUNET_MQ_Handle *mq,
2089 const struct GNUNET_MessageHeader *msg,
2092 struct CadetMQState *state = impl_state;
2094 GNUNET_assert (NULL == state->th);
2096 GNUNET_CADET_notify_transmit_ready (state->channel,
2097 /* FIXME: add option for corking */
2099 GNUNET_TIME_UNIT_FOREVER_REL,
2107 * Signature of functions implementing the
2108 * destruction of a message queue.
2109 * Implementations must not free 'mq', but should
2110 * take care of 'impl_state'.
2112 * @param mq the message queue to destroy
2113 * @param impl_state state of the implementation
2116 cadet_mq_destroy_impl_old (struct GNUNET_MQ_Handle *mq,
2119 struct CadetMQState *state = impl_state;
2121 if (NULL != state->th)
2122 GNUNET_CADET_notify_transmit_ready_cancel (state->th);
2124 GNUNET_free (state);
2129 * Create a message queue for a cadet channel.
2130 * The message queue can only be used to transmit messages,
2131 * not to receive them.
2133 * @param channel the channel to create the message qeue for
2134 * @return a message queue to messages over the channel
2136 struct GNUNET_MQ_Handle *
2137 GNUNET_CADET_mq_create (struct GNUNET_CADET_Channel *channel)
2139 struct GNUNET_MQ_Handle *mq;
2140 struct CadetMQState *state;
2142 state = GNUNET_new (struct CadetMQState);
2143 state->channel = channel;
2145 mq = GNUNET_MQ_queue_for_callbacks (&cadet_mq_send_impl_old,
2146 &cadet_mq_destroy_impl_old,
2147 NULL, /* FIXME: cancel impl. */
2149 NULL, /* no msg handlers */
2150 NULL, /* no err handlers */
2151 NULL); /* no handler cls */
2157 * Transitional function to convert an unsigned int port to a hash value.
2158 * WARNING: local static value returned, NOT reentrant!
2159 * WARNING: do not use this function for new code!
2161 * @param port Numerical port (unsigned int format).
2163 * @return A GNUNET_HashCode usable for the new CADET API.
2165 const struct GNUNET_HashCode *
2166 GC_u2h (uint32_t port)
2168 static struct GNUNET_HashCode hash;
2170 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2171 "This is a transitional function, "
2172 "use proper crypto hashes as CADET ports\n");
2173 GNUNET_CRYPTO_hash (&port, sizeof (port), &hash);
2180 /******************************************************************************/
2181 /******************************* MQ-BASED API *********************************/
2182 /******************************************************************************/
2185 * Connect to the MQ-based cadet service.
2187 * @param cfg Configuration to use.
2189 * @return Handle to the cadet service NULL on error.
2191 struct GNUNET_CADET_Handle *
2192 GNUNET_CADET_connecT (const struct GNUNET_CONFIGURATION_Handle *cfg)
2194 struct GNUNET_CADET_Handle *h;
2196 LOG (GNUNET_ERROR_TYPE_DEBUG,
2197 "GNUNET_CADET_connecT()\n");
2198 h = GNUNET_new (struct GNUNET_CADET_Handle);
2200 h->mq_api = GNUNET_YES;
2201 h->ports = GNUNET_CONTAINER_multihashmap_create (4, GNUNET_YES);
2206 GNUNET_CADET_disconnect (h);
2209 h->next_ccn.channel_of_client = htonl (GNUNET_CADET_LOCAL_CHANNEL_ID_CLI);
2210 h->reconnect_time = GNUNET_TIME_UNIT_MILLISECONDS;
2211 h->reconnect_task = NULL;
2218 * Open a port to receive incomming MQ-based channels.
2220 * @param h CADET handle.
2221 * @param port Hash identifying the port.
2222 * @param connects Function called when an incoming channel is connected.
2223 * @param connects_cls Closure for the @a connects handler.
2224 * @param window_changes Function called when the transmit window size changes.
2225 * @param disconnects Function called when a channel is disconnected.
2226 * @param handlers Callbacks for messages we care about, NULL-terminated.
2228 * @return Port handle.
2230 struct GNUNET_CADET_Port *
2231 GNUNET_CADET_open_porT (struct GNUNET_CADET_Handle *h,
2232 const struct GNUNET_HashCode *port,
2233 GNUNET_CADET_ConnectEventHandler connects,
2234 void * connects_cls,
2235 GNUNET_CADET_WindowSizeEventHandler window_changes,
2236 GNUNET_CADET_DisconnectEventHandler disconnects,
2237 const struct GNUNET_MQ_MessageHandler *handlers)
2244 * Implement sending functionality of a message queue for
2245 * us sending messages to a peer.
2247 * Encapsulates the payload message in a #GNUNET_CADET_LocalData message
2248 * in order to label the message with the channel ID and send the
2249 * encapsulated message to the service.
2251 * @param mq the message queue
2252 * @param msg the message to send
2253 * @param impl_state state of the implementation
2256 cadet_mq_send_impl (struct GNUNET_MQ_Handle *mq,
2257 const struct GNUNET_MessageHeader *msg,
2260 struct GNUNET_CADET_Channel *ch = impl_state;
2261 struct GNUNET_CADET_Handle *h = ch->cadet;
2263 struct GNUNET_MQ_Envelope *env;
2264 struct GNUNET_CADET_LocalData *cadet_msg;
2269 /* We're currently reconnecting, pretend this worked */
2270 GNUNET_MQ_impl_send_continue (mq);
2274 /* check message size for sanity */
2275 msize = ntohs (msg->size);
2276 if (msize > GNUNET_CONSTANTS_MAX_CADET_MESSAGE_SIZE)
2279 GNUNET_MQ_impl_send_continue (mq);
2283 env = GNUNET_MQ_msg_nested_mh (cadet_msg,
2284 GNUNET_MESSAGE_TYPE_CADET_LOCAL_DATA,
2286 cadet_msg->ccn = ch->ccn;
2287 GNUNET_MQ_send (h->mq, env);
2288 GNUNET_MQ_impl_send_continue (mq);
2293 * Handle destruction of a message queue. Implementations must not
2294 * free @a mq, but should take care of @a impl_state.
2296 * @param mq the message queue to destroy
2297 * @param impl_state state of the implementation
2300 cadet_mq_destroy_impl (struct GNUNET_MQ_Handle *mq,
2303 struct GNUNET_CADET_Channel *ch = impl_state;
2305 GNUNET_assert (mq == ch->mq);
2311 * We had an error processing a message we forwarded from a peer to
2312 * the CADET service. We should just complain about it but otherwise
2313 * continue processing.
2315 * @param cls closure
2316 * @param error error code
2319 cadet_mq_error_handler (void *cls,
2320 enum GNUNET_MQ_Error error)
2322 GNUNET_break_op (0);
2327 * Implementation function that cancels the currently sent message.
2328 * Should basically undo whatever #mq_send_impl() did.
2330 * @param mq message queue
2331 * @param impl_state state specific to the implementation
2334 cadet_mq_cancel_impl (struct GNUNET_MQ_Handle *mq,
2337 struct GNUNET_CADET_Channel *ch = impl_state;
2339 LOG (GNUNET_ERROR_TYPE_WARNING,
2340 "Cannot cancel mq message on channel %X of %p\n",
2341 ch->ccn.channel_of_client, ch->cadet);
2348 * Create a new channel towards a remote peer.
2350 * If the destination port is not open by any peer or the destination peer
2351 * does not accept the channel, #GNUNET_CADET_ChannelEndHandler will be called
2354 * @param h CADET handle.
2355 * @param channel_cls Closure for the channel. It's given to:
2356 * - The disconnect handler @a disconnects
2357 * - Each message type callback in @a handlers
2358 * @param destination Peer identity the channel should go to.
2359 * @param port Identification of the destination port.
2360 * @param options CadetOption flag field, with all desired option bits set to 1.
2361 * @param window_changes Function called when the transmit window size changes.
2362 * @param disconnects Function called when the channel is disconnected.
2363 * @param handlers Callbacks for messages we care about, NULL-terminated.
2365 * @return Handle to the channel.
2367 struct GNUNET_CADET_Channel *
2368 GNUNET_CADET_channel_creatE (struct GNUNET_CADET_Handle *h,
2370 const struct GNUNET_PeerIdentity *destination,
2371 const struct GNUNET_HashCode *port,
2372 enum GNUNET_CADET_ChannelOption options,
2373 GNUNET_CADET_WindowSizeEventHandler window_changes,
2374 GNUNET_CADET_DisconnectEventHandler disconnects,
2375 const struct GNUNET_MQ_MessageHandler *handlers)
2377 struct GNUNET_CADET_Channel *ch;
2378 struct GNUNET_CADET_ClientChannelNumber ccn;
2379 struct GNUNET_CADET_LocalChannelCreateMessage *msg;
2380 struct GNUNET_MQ_Envelope *env;
2382 /* Save parameters */
2383 ccn.channel_of_client = htonl (0);
2384 ch = create_channel (h, ccn);
2385 ch->ctx = channel_cls;
2386 ch->peer = GNUNET_PEER_intern (destination);
2387 ch->options = options;
2388 ch->window_changes = window_changes;
2389 ch->disconnects = disconnects;
2391 /* Create MQ for channel */
2392 ch->mq = GNUNET_MQ_queue_for_callbacks (&cadet_mq_send_impl,
2393 &cadet_mq_destroy_impl,
2394 &cadet_mq_cancel_impl,
2397 &cadet_mq_error_handler,
2399 GNUNET_MQ_set_handlers_closure (ch->mq, channel_cls);
2401 /* Request channel creation to service */
2402 env = GNUNET_MQ_msg (msg,
2403 GNUNET_MESSAGE_TYPE_CADET_LOCAL_CHANNEL_CREATE);
2406 msg->peer = *destination;
2407 msg->opt = htonl (options);
2408 GNUNET_MQ_send (h->mq,
2416 * Obtain the message queue for a connected peer.
2418 * @param channel The channel handle from which to get the MQ.
2420 * @return NULL if @a channel is not yet connected.
2422 struct GNUNET_MQ_Handle *
2423 GNUNET_CADET_get_mq (const struct GNUNET_CADET_Channel *channel)