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
42 struct GNUNET_CADET_TransmitHandle
47 struct GNUNET_CADET_TransmitHandle *next;
52 struct GNUNET_CADET_TransmitHandle *prev;
55 * Channel this message is sent on / for (may be NULL for control messages).
57 struct GNUNET_CADET_Channel *channel;
62 struct GNUNET_SCHEDULER_Task *request_data_task;
65 * Callback to obtain the message to transmit, or NULL if we
66 * got the message in 'data'. Notice that messages built
67 * by 'notify' need to be encapsulated with information about
70 GNUNET_CONNECTION_TransmitReadyNotify notify;
73 * Closure for 'notify'
78 * Size of the payload.
90 GNUNET_CADET_ChannelCB channel_cb;
95 GNUNET_CADET_PeersCB peers_cb;
100 GNUNET_CADET_PeerCB peer_cb;
105 GNUNET_CADET_TunnelsCB tunnels_cb;
110 GNUNET_CADET_TunnelCB tunnel_cb;
115 * Opaque handle to the service.
117 struct GNUNET_CADET_Handle
120 * Message queue (if available).
122 struct GNUNET_MQ_Handle *mq;
125 * Set of handlers used for processing incoming messages in the channels
127 const struct GNUNET_CADET_MessageHandler *message_handlers;
130 * Number of handlers in the handlers array.
132 unsigned int n_handlers;
137 struct GNUNET_CONTAINER_MultiHashMap *ports;
140 * Double linked list of the channels this client is connected to, head.
142 struct GNUNET_CADET_Channel *channels_head;
145 * Double linked list of the channels this client is connected to, tail.
147 struct GNUNET_CADET_Channel *channels_tail;
150 * Callback for inbound channel disconnection
152 GNUNET_CADET_ChannelEndHandler *cleaner;
155 * Closure for all the handlers given by the client
160 * Messages to send to the service, head.
162 struct GNUNET_CADET_TransmitHandle *th_head;
165 * Messages to send to the service, tail.
167 struct GNUNET_CADET_TransmitHandle *th_tail;
170 * child of the next channel to create (to avoid reusing IDs often)
172 struct GNUNET_CADET_ClientChannelNumber next_ccn;
175 * Configuration given by the client, in case of reconnection
177 const struct GNUNET_CONFIGURATION_Handle *cfg;
180 * Time to the next reconnect in case one reconnect fails
182 struct GNUNET_TIME_Relative reconnect_time;
185 * Task for trying to reconnect.
187 struct GNUNET_SCHEDULER_Task * reconnect_task;
190 * Callback for an info task (only one active at a time).
192 union CadetInfoCB info_cb;
195 * Info callback closure for @c info_cb.
202 * Description of a peer
204 struct GNUNET_CADET_Peer
207 * ID of the peer in short form
212 * Channel this peer belongs to
214 struct GNUNET_CADET_Channel *t;
219 * Opaque handle to a channel.
221 struct GNUNET_CADET_Channel
226 struct GNUNET_CADET_Channel *next;
231 struct GNUNET_CADET_Channel *prev;
234 * Handle to the cadet this channel belongs to
236 struct GNUNET_CADET_Handle *cadet;
239 * Local ID of the channel
241 struct GNUNET_CADET_ClientChannelNumber ccn;
244 * Channel's port, if any.
246 struct GNUNET_CADET_Port *port;
249 * Other end of the channel.
254 * Any data the caller wants to put in here
259 * Size of packet queued in this channel
261 unsigned int packet_size;
264 * Channel options: reliability, etc.
266 enum GNUNET_CADET_ChannelOption options;
269 * Are we allowed to send to the service?
277 * Opaque handle to a port.
279 struct GNUNET_CADET_Port
282 * Handle to the CADET session this port belongs to.
284 struct GNUNET_CADET_Handle *cadet;
289 struct GNUNET_HashCode *hash;
292 * Callback handler for incoming channels on this port.
294 GNUNET_CADET_InboundChannelNotificationHandler *handler;
297 * Closure for @a handler.
304 * Implementation state for cadet's message queue.
309 * The current transmit handle, or NULL
310 * if no transmit is active.
312 struct GNUNET_CADET_TransmitHandle *th;
315 * Channel to send the data over.
317 struct GNUNET_CADET_Channel *channel;
321 /******************************************************************************/
322 /*********************** AUXILIARY FUNCTIONS *************************/
323 /******************************************************************************/
326 * Check if transmission is a payload packet.
328 * @param th Transmission handle.
330 * @return #GNUNET_YES if it is a payload packet,
331 * #GNUNET_NO if it is a cadet management packet.
334 th_is_payload (struct GNUNET_CADET_TransmitHandle *th)
336 return (th->notify != NULL) ? GNUNET_YES : GNUNET_NO;
341 * Find the Port struct for a hash.
343 * @param h CADET handle.
344 * @param hash HashCode for the port number.
346 * @return The port handle if known, NULL otherwise.
348 static struct GNUNET_CADET_Port *
349 find_port (const struct GNUNET_CADET_Handle *h,
350 const struct GNUNET_HashCode *hash)
352 struct GNUNET_CADET_Port *p;
354 p = GNUNET_CONTAINER_multihashmap_get (h->ports, hash);
361 * Get the channel handler for the channel specified by id from the given handle
363 * @param h Cadet handle
364 * @param ccn ID of the wanted channel
365 * @return handle to the required channel or NULL if not found
367 static struct GNUNET_CADET_Channel *
368 retrieve_channel (struct GNUNET_CADET_Handle *h,
369 struct GNUNET_CADET_ClientChannelNumber ccn)
371 struct GNUNET_CADET_Channel *ch;
373 for (ch = h->channels_head; NULL != ch; ch = ch->next)
374 if (ch->ccn.channel_of_client == ccn.channel_of_client)
381 * Create a new channel and insert it in the channel list of the cadet handle
383 * @param h Cadet handle
384 * @param ccn Desired ccn of the channel, 0 to assign one automatically.
386 * @return Handle to the created channel.
388 static struct GNUNET_CADET_Channel *
389 create_channel (struct GNUNET_CADET_Handle *h,
390 struct GNUNET_CADET_ClientChannelNumber ccn)
392 struct GNUNET_CADET_Channel *ch;
394 ch = GNUNET_new (struct GNUNET_CADET_Channel);
395 GNUNET_CONTAINER_DLL_insert (h->channels_head,
399 if (0 == ccn.channel_of_client)
401 ch->ccn = h->next_ccn;
402 while (NULL != retrieve_channel (h,
405 h->next_ccn.channel_of_client
406 = htonl (1 + ntohl (h->next_ccn.channel_of_client));
407 if (0 == ntohl (h->next_ccn.channel_of_client))
408 h->next_ccn.channel_of_client
409 = htonl (GNUNET_CADET_LOCAL_CHANNEL_ID_CLI);
416 ch->allow_send = GNUNET_NO;
422 * Destroy the specified channel.
423 * - Destroys all peers, calling the disconnect callback on each if needed
424 * - Cancels all outgoing traffic for that channel, calling respective notifys
425 * - Calls cleaner if channel was inbound
426 * - Frees all memory used
428 * @param ch Pointer to the channel.
429 * @param call_cleaner Whether to call the cleaner handler.
431 * @return Handle to the required channel or NULL if not found.
433 // FIXME: simplify: call_cleaner is always #GNUNET_YES!!!
435 destroy_channel (struct GNUNET_CADET_Channel *ch,
438 struct GNUNET_CADET_Handle *h;
439 struct GNUNET_CADET_TransmitHandle *th;
440 struct GNUNET_CADET_TransmitHandle *next;
448 LOG (GNUNET_ERROR_TYPE_DEBUG,
449 " destroy_channel %X of %p\n",
453 GNUNET_CONTAINER_DLL_remove (h->channels_head,
457 /* signal channel destruction */
458 if ( (NULL != h->cleaner) &&
460 (GNUNET_YES == call_cleaner) )
462 LOG (GNUNET_ERROR_TYPE_DEBUG,
463 " calling cleaner\n");
464 h->cleaner (h->cls, ch, ch->ctx);
467 /* check that clients did not leave messages behind in the queue */
468 for (th = h->th_head; NULL != th; th = next)
471 if (th->channel != ch)
473 /* Clients should have aborted their requests already.
474 * Management traffic should be ok, as clients can't cancel that.
475 * If the service crashed and we are reconnecting, it's ok.
477 GNUNET_break (GNUNET_NO == th_is_payload (th));
478 GNUNET_CADET_notify_transmit_ready_cancel (th);
482 GNUNET_PEER_change_rc (ch->peer, -1);
489 * Add a transmit handle to the transmission queue and set the
492 * @param h cadet handle with the queue head and tail
493 * @param th handle to the packet to be transmitted
496 add_to_queue (struct GNUNET_CADET_Handle *h,
497 struct GNUNET_CADET_TransmitHandle *th)
499 GNUNET_CONTAINER_DLL_insert_tail (h->th_head, h->th_tail, th);
504 * Remove a transmit handle from the transmission queue, if present.
506 * Safe to call even if not queued.
508 * @param th handle to the packet to be unqueued.
511 remove_from_queue (struct GNUNET_CADET_TransmitHandle *th)
513 struct GNUNET_CADET_Handle *h = th->channel->cadet;
515 /* It might or might not have been queued (rarely not), but check anyway. */
516 if (NULL != th->next || h->th_tail == th)
518 GNUNET_CONTAINER_DLL_remove (h->th_head, h->th_tail, th);
524 * Send an ack on the channel to confirm the processing of a message.
526 * @param ch Channel on which to send the ACK.
529 send_ack (struct GNUNET_CADET_Channel *ch)
531 struct GNUNET_CADET_LocalAck *msg;
532 struct GNUNET_MQ_Envelope *env;
534 env = GNUNET_MQ_msg (msg,
535 GNUNET_MESSAGE_TYPE_CADET_LOCAL_ACK);
537 LOG (GNUNET_ERROR_TYPE_DEBUG,
538 "Sending ACK on channel %X\n",
539 ch->ccn.channel_of_client);
541 GNUNET_MQ_send (ch->cadet->mq,
547 /******************************************************************************/
548 /*********************** RECEIVE HANDLERS ****************************/
549 /******************************************************************************/
553 * Call the @a notify callback given to #GNUNET_CADET_notify_transmit_ready to
554 * request the data to send over MQ. Since MQ manages the queue, this function
555 * is scheduled immediatly after a transmit ready notification.
557 * @param cls Closure (transmit handle).
560 request_data (void *cls)
562 struct GNUNET_CADET_TransmitHandle *th = cls;
563 struct GNUNET_CADET_LocalData *msg;
564 struct GNUNET_MQ_Envelope *env;
567 LOG (GNUNET_ERROR_TYPE_DEBUG,
568 "Requesting Data: %u bytes\n",
571 GNUNET_assert (GNUNET_YES == th->channel->allow_send);
572 th->channel->allow_send = GNUNET_NO;
573 th->request_data_task = NULL;
574 th->channel->packet_size = 0;
575 remove_from_queue (th);
577 env = GNUNET_MQ_msg_extra (msg,
579 GNUNET_MESSAGE_TYPE_CADET_LOCAL_DATA);
580 msg->ccn = th->channel->ccn;
581 osize = th->notify (th->notify_cls,
584 GNUNET_assert (osize == th->size);
585 GNUNET_MQ_send (th->channel->cadet->mq,
592 * Process the new channel notification and add it to the channels in the handle
594 * @param h The cadet handle
595 * @param msg A message with the details of the new incoming channel
598 handle_channel_created (void *cls,
599 const struct GNUNET_CADET_LocalChannelCreateMessage *msg)
601 struct GNUNET_CADET_Handle *h = cls;
602 struct GNUNET_CADET_Channel *ch;
603 struct GNUNET_CADET_Port *port;
604 const struct GNUNET_HashCode *port_number;
605 struct GNUNET_CADET_ClientChannelNumber ccn;
608 port_number = &msg->port;
609 LOG (GNUNET_ERROR_TYPE_DEBUG,
610 "Creating incoming channel %X [%s]\n",
611 ntohl (ccn.channel_of_client),
612 GNUNET_h2s (port_number));
613 if (ntohl (ccn.channel_of_client) >= GNUNET_CADET_LOCAL_CHANNEL_ID_CLI)
618 port = find_port (h, port_number);
623 ch = create_channel (h, ccn);
624 ch->allow_send = GNUNET_NO;
625 ch->peer = GNUNET_PEER_intern (&msg->peer);
629 ch->options = ntohl (msg->opt);
631 LOG (GNUNET_ERROR_TYPE_DEBUG, " created channel %p\n", ch);
632 ctx = port->handler (port->cls, ch, &msg->peer, port->hash, ch->options);
635 LOG (GNUNET_ERROR_TYPE_DEBUG, "User notified\n");
639 struct GNUNET_CADET_LocalChannelDestroyMessage *d_msg;
640 struct GNUNET_MQ_Envelope *env;
642 LOG (GNUNET_ERROR_TYPE_DEBUG, "No handler for incoming channels\n");
643 env = GNUNET_MQ_msg (d_msg,
644 GNUNET_MESSAGE_TYPE_CADET_LOCAL_CHANNEL_DESTROY);
645 d_msg->ccn = msg->ccn;
646 GNUNET_MQ_send (h->mq, env);
653 * Process the channel destroy notification and free associated resources
655 * @param h The cadet handle
656 * @param msg A message with the details of the channel being destroyed
659 handle_channel_destroy (void *cls,
660 const struct GNUNET_CADET_LocalChannelDestroyMessage *msg)
662 struct GNUNET_CADET_Handle *h = cls;
663 struct GNUNET_CADET_Channel *ch;
664 struct GNUNET_CADET_ClientChannelNumber ccn;
667 LOG (GNUNET_ERROR_TYPE_DEBUG,
668 "Channel %X Destroy from service\n",
669 ntohl (ccn.channel_of_client));
670 ch = retrieve_channel (h,
675 LOG (GNUNET_ERROR_TYPE_DEBUG,
676 "channel %X unknown\n",
677 ntohl (ccn.channel_of_client));
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)
697 struct GNUNET_CADET_Handle *h = cls;
698 struct GNUNET_CADET_Channel *ch;
701 size = ntohs (message->header.size);
702 if (sizeof (*message) + sizeof (struct GNUNET_MessageHeader) > size)
705 return GNUNET_SYSERR;
708 ch = retrieve_channel (h,
713 return GNUNET_SYSERR;
721 * Process the incoming data packets, call appropriate handlers.
723 * @param h The cadet handle
724 * @param message A message encapsulating the data
727 handle_local_data (void *cls,
728 const struct GNUNET_CADET_LocalData *message)
730 struct GNUNET_CADET_Handle *h = cls;
731 const struct GNUNET_MessageHeader *payload;
732 const struct GNUNET_CADET_MessageHandler *handler;
733 struct GNUNET_CADET_Channel *ch;
737 LOG (GNUNET_ERROR_TYPE_DEBUG,
738 "Got a data message!\n");
739 ch = retrieve_channel (h,
741 GNUNET_assert (NULL != ch);
743 payload = (struct GNUNET_MessageHeader *) &message[1];
744 LOG (GNUNET_ERROR_TYPE_DEBUG, " %s data on channel %s [%X]\n",
745 GC_f2s (ntohl (ch->ccn.channel_of_client) >=
746 GNUNET_CADET_LOCAL_CHANNEL_ID_CLI),
747 GNUNET_i2s (GNUNET_PEER_resolve2 (ch->peer)),
748 ntohl (message->ccn.channel_of_client));
750 type = ntohs (payload->type);
751 LOG (GNUNET_ERROR_TYPE_DEBUG, " payload type %s\n", GC_m2s (type));
752 for (i = 0; i < h->n_handlers; i++)
754 handler = &h->message_handlers[i];
755 LOG (GNUNET_ERROR_TYPE_DEBUG, " checking handler for type %u\n",
757 if (handler->type == type)
760 handler->callback (h->cls, ch, &ch->ctx, payload))
762 LOG (GNUNET_ERROR_TYPE_DEBUG, "callback caused disconnection\n");
763 GNUNET_CADET_channel_destroy (ch);
768 LOG (GNUNET_ERROR_TYPE_DEBUG,
769 "callback completed successfully\n");
778 * Process a local ACK message, enabling the client to send
779 * more data to the service.
781 * @param h Cadet handle.
782 * @param message Message itself.
785 handle_local_ack (void *cls,
786 const struct GNUNET_CADET_LocalAck *message)
788 struct GNUNET_CADET_Handle *h = cls;
789 struct GNUNET_CADET_Channel *ch;
790 struct GNUNET_CADET_ClientChannelNumber ccn;
793 ch = retrieve_channel (h, ccn);
796 LOG (GNUNET_ERROR_TYPE_DEBUG,
797 "ACK on unknown channel %X\n",
798 ntohl (ccn.channel_of_client));
801 LOG (GNUNET_ERROR_TYPE_DEBUG,
802 "Got an ACK on channel %X!\n",
803 ntohl (ch->ccn.channel_of_client));
804 ch->allow_send = GNUNET_YES;
805 if (0 < ch->packet_size)
807 struct GNUNET_CADET_TransmitHandle *th;
808 struct GNUNET_CADET_TransmitHandle *next;
809 LOG (GNUNET_ERROR_TYPE_DEBUG,
810 " pending data, sending %u bytes!\n",
812 for (th = h->th_head; NULL != th; th = next)
815 if (th->channel == ch)
817 GNUNET_assert (NULL == th->request_data_task);
818 th->request_data_task = GNUNET_SCHEDULER_add_now (&request_data, th);
822 /* Complain if we got thru all th without sending anything, ch was wrong */
823 GNUNET_break (NULL != th);
828 * Reconnect to the service, retransmit all infomation to try to restore the
831 * @param h handle to the cadet
833 * @return #GNUNET_YES in case of sucess, #GNUNET_NO otherwise (service down...)
836 reconnect (struct GNUNET_CADET_Handle *h);
840 * Reconnect callback: tries to reconnect again after a failer previous
843 * @param cls closure (cadet handle)
846 reconnect_cbk (void *cls);
850 * Generic error handler, called with the appropriate error code and
851 * the same closure specified at the creation of the message queue.
852 * Not every message queue implementation supports an error handler.
854 * @param cls closure, a `struct GNUNET_CORE_Handle *`
855 * @param error error code
858 handle_mq_error (void *cls,
859 enum GNUNET_MQ_Error error)
861 struct GNUNET_CADET_Handle *h = cls;
863 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MQ ERROR: %u\n", error);
864 GNUNET_MQ_destroy (h->mq);
871 * Process a local reply about info on all channels, pass info to the user.
873 * @param h Cadet handle.
874 * @param message Message itself.
877 // process_get_channels (struct GNUNET_CADET_Handle *h,
878 // const struct GNUNET_MessageHeader *message)
880 // struct GNUNET_CADET_LocalInfo *msg;
882 // GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Get Channels messasge received\n");
884 // if (NULL == h->channels_cb)
886 // GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " ignored\n");
890 // msg = (struct GNUNET_CADET_LocalInfo *) message;
891 // if (ntohs (message->size) !=
892 // (sizeof (struct GNUNET_CADET_LocalInfo) +
893 // sizeof (struct GNUNET_PeerIdentity)))
895 // GNUNET_break_op (0);
896 // GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
897 // "Get channels message: size %hu - expected %u\n",
898 // ntohs (message->size),
899 // sizeof (struct GNUNET_CADET_LocalInfo));
902 // h->channels_cb (h->channels_cls,
903 // ntohl (msg->channel_id),
905 // &msg->destination);
911 * Process a local monitor_channel reply, pass info to the user.
913 * @param h Cadet handle.
914 * @param message Message itself.
917 // process_show_channel (struct GNUNET_CADET_Handle *h,
918 // const struct GNUNET_MessageHeader *message)
920 // struct GNUNET_CADET_LocalInfo *msg;
923 // GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Show Channel messasge received\n");
925 // if (NULL == h->channel_cb)
927 // GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " ignored\n");
931 // /* Verify message sanity */
932 // msg = (struct GNUNET_CADET_LocalInfo *) message;
933 // esize = sizeof (struct GNUNET_CADET_LocalInfo);
934 // if (ntohs (message->size) != esize)
936 // GNUNET_break_op (0);
937 // GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
938 // "Show channel message: size %hu - expected %u\n",
939 // ntohs (message->size),
942 // h->channel_cb (h->channel_cls, NULL, NULL);
943 // h->channel_cb = NULL;
944 // h->channel_cls = NULL;
949 // h->channel_cb (h->channel_cls,
950 // &msg->destination,
957 * Check that message received from CADET service is well-formed.
959 * @param cls the `struct GNUNET_CADET_Handle`
960 * @param message the message we got
961 * @return #GNUNET_OK if the message is well-formed,
962 * #GNUNET_SYSERR otherwise
965 check_get_peers (void *cls,
966 const struct GNUNET_CADET_LocalInfoPeer *message)
968 struct GNUNET_CADET_Handle *h = cls;
971 if (NULL == h->info_cb.peers_cb)
973 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
974 " no handler for peesr monitor message!\n");
975 return GNUNET_SYSERR;
978 size = ntohs (message->header.size);
979 if (sizeof (struct GNUNET_CADET_LocalInfoPeer) > size)
981 h->info_cb.peers_cb (h->info_cls, NULL, -1, 0, 0);
982 h->info_cb.peers_cb = NULL;
984 return GNUNET_SYSERR;
992 * Process a local reply about info on all tunnels, pass info to the user.
994 * @param cls Closure (Cadet handle).
995 * @param msg Message itself.
998 handle_get_peers (void *cls,
999 const struct GNUNET_CADET_LocalInfoPeer *msg)
1001 struct GNUNET_CADET_Handle *h = cls;
1002 h->info_cb.peers_cb (h->info_cls, &msg->destination,
1003 (int) ntohs (msg->tunnel),
1004 (unsigned int ) ntohs (msg->paths),
1010 * Check that message received from CADET service is well-formed.
1012 * @param cls the `struct GNUNET_CADET_Handle`
1013 * @param message the message we got
1014 * @return #GNUNET_OK if the message is well-formed,
1015 * #GNUNET_SYSERR otherwise
1018 check_get_peer (void *cls,
1019 const struct GNUNET_CADET_LocalInfoPeer *message)
1021 struct GNUNET_CADET_Handle *h = cls;
1022 const size_t msize = sizeof (struct GNUNET_CADET_LocalInfoPeer);
1023 struct GNUNET_PeerIdentity *paths_array;
1025 unsigned int epaths;
1029 if (NULL == h->info_cb.peer_cb)
1031 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1032 " no handler for peer monitor message!\n");
1036 /* Verify message sanity */
1037 esize = ntohs (message->header.size);
1040 GNUNET_break_op (0);
1041 h->info_cb.peer_cb (h->info_cls, NULL, 0, 0, 0, NULL);
1044 if (0 != ((esize - msize) % sizeof (struct GNUNET_PeerIdentity)))
1046 GNUNET_break_op (0);
1047 h->info_cb.peer_cb (h->info_cls, NULL, 0, 0, 0, NULL);
1051 peers = (esize - msize) / sizeof (struct GNUNET_PeerIdentity);
1052 epaths = (unsigned int) ntohs (message->paths);
1053 paths_array = (struct GNUNET_PeerIdentity *) &message[1];
1055 for (int i = 0; i < peers; i++)
1057 if (0 == memcmp (&paths_array[i], &message->destination,
1058 sizeof (struct GNUNET_PeerIdentity)))
1063 if (paths != epaths)
1065 GNUNET_break_op (0);
1066 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "p:%u, e: %u\n", paths, epaths);
1067 h->info_cb.peer_cb (h->info_cls, NULL, 0, 0, 0, NULL);
1074 h->info_cb.peer_cb = NULL;
1076 return GNUNET_SYSERR;
1081 * Process a local peer info reply, pass info to the user.
1083 * @param cls Closure (Cadet handle).
1084 * @param message Message itself.
1087 handle_get_peer (void *cls,
1088 const struct GNUNET_CADET_LocalInfoPeer *message)
1090 struct GNUNET_CADET_Handle *h = cls;
1091 struct GNUNET_PeerIdentity *paths_array;
1093 unsigned int path_length;
1097 paths = (unsigned int) ntohs (message->paths);
1098 paths_array = (struct GNUNET_PeerIdentity *) &message[1];
1099 peers = (ntohs (message->header.size) - sizeof (*message))
1100 / sizeof (struct GNUNET_PeerIdentity);
1102 neighbor = GNUNET_NO;
1104 for (int i = 0; i < peers; i++)
1106 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " %s\n", GNUNET_i2s (&paths_array[i]));
1108 if (0 == memcmp (&paths_array[i], &message->destination,
1109 sizeof (struct GNUNET_PeerIdentity)))
1111 if (1 == path_length)
1112 neighbor = GNUNET_YES;
1117 /* Call Callback with tunnel info. */
1118 paths_array = (struct GNUNET_PeerIdentity *) &message[1];
1119 h->info_cb.peer_cb (h->info_cls,
1120 &message->destination,
1121 (int) ntohs (message->tunnel),
1129 * Check that message received from CADET service is well-formed.
1131 * @param cls the `struct GNUNET_CADET_Handle`
1132 * @param msg the message we got
1133 * @return #GNUNET_OK if the message is well-formed,
1134 * #GNUNET_SYSERR otherwise
1137 check_get_tunnels (void *cls,
1138 const struct GNUNET_CADET_LocalInfoTunnel *msg)
1140 struct GNUNET_CADET_Handle *h = cls;
1143 if (NULL == h->info_cb.tunnels_cb)
1145 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1146 " no handler for tunnels monitor message!\n");
1147 return GNUNET_SYSERR;
1150 size = ntohs (msg->header.size);
1151 if (sizeof (struct GNUNET_CADET_LocalInfoTunnel) > size)
1153 h->info_cb.tunnels_cb (h->info_cls, NULL, 0, 0, 0, 0);
1154 h->info_cb.tunnels_cb = NULL;
1156 return GNUNET_SYSERR;
1163 * Process a local reply about info on all tunnels, pass info to the user.
1165 * @param cls Closure (Cadet handle).
1166 * @param message Message itself.
1169 handle_get_tunnels (void *cls,
1170 const struct GNUNET_CADET_LocalInfoTunnel *msg)
1172 struct GNUNET_CADET_Handle *h = cls;
1174 h->info_cb.tunnels_cb (h->info_cls,
1176 ntohl (msg->channels),
1177 ntohl (msg->connections),
1178 ntohs (msg->estate),
1179 ntohs (msg->cstate));
1185 * Check that message received from CADET service is well-formed.
1187 * @param cls the `struct GNUNET_CADET_Handle`
1188 * @param msg the message we got
1189 * @return #GNUNET_OK if the message is well-formed,
1190 * #GNUNET_SYSERR otherwise
1193 check_get_tunnel (void *cls,
1194 const struct GNUNET_CADET_LocalInfoTunnel *msg)
1196 struct GNUNET_CADET_Handle *h = cls;
1202 if (NULL == h->info_cb.tunnel_cb)
1204 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1205 " no handler for tunnel monitor message!\n");
1209 /* Verify message sanity */
1210 msize = ntohs (msg->header.size);
1211 esize = sizeof (struct GNUNET_CADET_LocalInfoTunnel);
1214 GNUNET_break_op (0);
1215 h->info_cb.tunnel_cb (h->info_cls,
1216 NULL, 0, 0, NULL, NULL, 0, 0);
1219 ch_n = ntohl (msg->channels);
1220 c_n = ntohl (msg->connections);
1221 esize += ch_n * sizeof (struct GNUNET_CADET_ChannelTunnelNumber);
1222 esize += c_n * sizeof (struct GNUNET_CADET_ConnectionTunnelIdentifier);
1225 GNUNET_break_op (0);
1226 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1227 "m:%u, e: %u (%u ch, %u conn)\n",
1228 (unsigned int) msize,
1229 (unsigned int) esize,
1232 h->info_cb.tunnel_cb (h->info_cls,
1233 NULL, 0, 0, NULL, NULL, 0, 0);
1240 h->info_cb.tunnel_cb = NULL;
1242 return GNUNET_SYSERR;
1247 * Process a local tunnel info reply, pass info to the user.
1249 * @param cls Closure (Cadet handle).
1250 * @param msg Message itself.
1253 handle_get_tunnel (void *cls,
1254 const struct GNUNET_CADET_LocalInfoTunnel *msg)
1256 struct GNUNET_CADET_Handle *h = cls;
1259 const struct GNUNET_CADET_ConnectionTunnelIdentifier *conns;
1260 const struct GNUNET_CADET_ChannelTunnelNumber *chns;
1262 ch_n = ntohl (msg->channels);
1263 c_n = ntohl (msg->connections);
1265 /* Call Callback with tunnel info. */
1266 conns = (const struct GNUNET_CADET_ConnectionTunnelIdentifier *) &msg[1];
1267 chns = (const struct GNUNET_CADET_ChannelTunnelNumber *) &conns[c_n];
1268 h->info_cb.tunnel_cb (h->info_cls,
1274 ntohs (msg->estate),
1275 ntohs (msg->cstate));
1281 * Reconnect to the service, retransmit all infomation to try to restore the
1284 * @param h handle to the cadet
1286 * @return GNUNET_YES in case of sucess, GNUNET_NO otherwise (service down...)
1289 do_reconnect (struct GNUNET_CADET_Handle *h)
1291 struct GNUNET_MQ_MessageHandler handlers[] = {
1292 GNUNET_MQ_hd_fixed_size (channel_created,
1293 GNUNET_MESSAGE_TYPE_CADET_LOCAL_CHANNEL_CREATE,
1294 struct GNUNET_CADET_LocalChannelCreateMessage,
1296 GNUNET_MQ_hd_fixed_size (channel_destroy,
1297 GNUNET_MESSAGE_TYPE_CADET_LOCAL_CHANNEL_DESTROY,
1298 struct GNUNET_CADET_LocalChannelDestroyMessage,
1300 GNUNET_MQ_hd_var_size (local_data,
1301 GNUNET_MESSAGE_TYPE_CADET_LOCAL_DATA,
1302 struct GNUNET_CADET_LocalData,
1304 GNUNET_MQ_hd_fixed_size (local_ack,
1305 GNUNET_MESSAGE_TYPE_CADET_LOCAL_ACK,
1306 struct GNUNET_CADET_LocalAck,
1308 GNUNET_MQ_hd_var_size (get_peers,
1309 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEERS,
1310 struct GNUNET_CADET_LocalInfoPeer,
1312 GNUNET_MQ_hd_var_size (get_peer,
1313 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEER,
1314 struct GNUNET_CADET_LocalInfoPeer,
1316 GNUNET_MQ_hd_var_size (get_tunnels,
1317 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNELS,
1318 struct GNUNET_CADET_LocalInfoTunnel,
1320 GNUNET_MQ_hd_var_size (get_tunnel,
1321 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNEL,
1322 struct GNUNET_CADET_LocalInfoTunnel,
1325 // GNUNET_MQ_hd_fixed_Y size (channel_destroyed,
1326 // GNUNET_MESSAGE_TYPE_CADET_CHANNEL_OPEN_NACK_DEPRECATED,
1327 // struct GNUNET_CADET_ChannelDestroyMessage);
1328 GNUNET_MQ_handler_end ()
1331 LOG (GNUNET_ERROR_TYPE_DEBUG, "Connecting to CADET\n");
1333 GNUNET_assert (NULL == h->mq);
1334 h->mq = GNUNET_CLIENT_connect (h->cfg,
1346 h->reconnect_time = GNUNET_TIME_UNIT_MILLISECONDS;
1352 * Reconnect callback: tries to reconnect again after a failer previous
1355 * @param cls closure (cadet handle)
1358 reconnect_cbk (void *cls)
1360 struct GNUNET_CADET_Handle *h = cls;
1362 h->reconnect_task = NULL;
1368 * Reconnect to the service, retransmit all infomation to try to restore the
1371 * @param h handle to the cadet
1373 * @return #GNUNET_YES in case of sucess, #GNUNET_NO otherwise (service down...)
1376 reconnect (struct GNUNET_CADET_Handle *h)
1378 struct GNUNET_CADET_Channel *ch;
1380 LOG (GNUNET_ERROR_TYPE_DEBUG,
1381 "Requested RECONNECT, destroying all channels\n");
1382 for (ch = h->channels_head; NULL != ch; ch = h->channels_head)
1383 destroy_channel (ch, GNUNET_YES);
1384 if (NULL == h->reconnect_task)
1385 h->reconnect_task = GNUNET_SCHEDULER_add_delayed (h->reconnect_time,
1390 /******************************************************************************/
1391 /********************** API CALL DEFINITIONS *************************/
1392 /******************************************************************************/
1394 struct GNUNET_CADET_Handle *
1395 GNUNET_CADET_connect (const struct GNUNET_CONFIGURATION_Handle *cfg,
1397 GNUNET_CADET_ChannelEndHandler cleaner,
1398 const struct GNUNET_CADET_MessageHandler *handlers)
1400 struct GNUNET_CADET_Handle *h;
1402 LOG (GNUNET_ERROR_TYPE_DEBUG, "GNUNET_CADET_connect()\n");
1403 h = GNUNET_new (struct GNUNET_CADET_Handle);
1404 LOG (GNUNET_ERROR_TYPE_DEBUG, " addr %p\n", h);
1406 h->cleaner = cleaner;
1407 h->ports = GNUNET_CONTAINER_multihashmap_create (4, GNUNET_YES);
1412 GNUNET_CADET_disconnect (h);
1416 h->message_handlers = handlers;
1417 h->next_ccn.channel_of_client = htonl (GNUNET_CADET_LOCAL_CHANNEL_ID_CLI);
1418 h->reconnect_time = GNUNET_TIME_UNIT_MILLISECONDS;
1419 h->reconnect_task = NULL;
1421 /* count handlers */
1422 for (h->n_handlers = 0;
1423 handlers && handlers[h->n_handlers].type;
1425 LOG (GNUNET_ERROR_TYPE_DEBUG, "GNUNET_CADET_connect() END\n");
1431 * Disconnect from the cadet service. All channels will be destroyed. All channel
1432 * disconnect callbacks will be called on any still connected peers, notifying
1433 * about their disconnection. The registered inbound channel cleaner will be
1434 * called should any inbound channels still exist.
1436 * @param handle connection to cadet to disconnect
1439 GNUNET_CADET_disconnect (struct GNUNET_CADET_Handle *handle)
1441 struct GNUNET_CADET_Channel *ch;
1442 struct GNUNET_CADET_Channel *aux;
1443 struct GNUNET_CADET_TransmitHandle *th;
1445 LOG (GNUNET_ERROR_TYPE_DEBUG,
1446 "CADET DISCONNECT\n");
1447 ch = handle->channels_head;
1451 if (ntohl (ch->ccn.channel_of_client) >= GNUNET_CADET_LOCAL_CHANNEL_ID_CLI)
1454 LOG (GNUNET_ERROR_TYPE_DEBUG,
1455 "channel %X not destroyed\n",
1456 ntohl (ch->ccn.channel_of_client));
1458 destroy_channel (ch,
1462 while (NULL != (th = handle->th_head))
1464 struct GNUNET_MessageHeader *msg;
1466 /* Make sure it is an allowed packet (everything else should have been
1467 * already canceled).
1469 GNUNET_break (GNUNET_NO == th_is_payload (th));
1470 msg = (struct GNUNET_MessageHeader *) &th[1];
1471 switch (ntohs(msg->type))
1473 case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_OPEN:
1474 case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DESTROY:
1475 case GNUNET_MESSAGE_TYPE_CADET_LOCAL_PORT_OPEN:
1476 case GNUNET_MESSAGE_TYPE_CADET_LOCAL_PORT_CLOSE:
1477 case GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_CHANNELS:
1478 case GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_CHANNEL:
1479 case GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEER:
1480 case GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEERS:
1481 case GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNEL:
1482 case GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNELS:
1486 LOG (GNUNET_ERROR_TYPE_ERROR, "unexpected unsent msg %s\n",
1487 GC_m2s (ntohs(msg->type)));
1490 GNUNET_CADET_notify_transmit_ready_cancel (th);
1493 if (NULL != handle->mq)
1495 GNUNET_MQ_destroy (handle->mq);
1498 if (NULL != handle->reconnect_task)
1500 GNUNET_SCHEDULER_cancel(handle->reconnect_task);
1501 handle->reconnect_task = NULL;
1504 GNUNET_CONTAINER_multihashmap_destroy (handle->ports);
1505 handle->ports = NULL;
1506 GNUNET_free (handle);
1511 * Open a port to receive incomming channels.
1513 * @param h CADET handle.
1514 * @param port Hash representing the port number.
1515 * @param new_channel Function called when an channel is received.
1516 * @param new_channel_cls Closure for @a new_channel.
1518 * @return Port handle.
1520 struct GNUNET_CADET_Port *
1521 GNUNET_CADET_open_port (struct GNUNET_CADET_Handle *h,
1522 const struct GNUNET_HashCode *port,
1523 GNUNET_CADET_InboundChannelNotificationHandler
1525 void *new_channel_cls)
1527 struct GNUNET_CADET_PortMessage *msg;
1528 struct GNUNET_MQ_Envelope *env;
1529 struct GNUNET_CADET_Port *p;
1531 GNUNET_assert (NULL != new_channel);
1532 p = GNUNET_new (struct GNUNET_CADET_Port);
1534 p->hash = GNUNET_new (struct GNUNET_HashCode);
1536 p->handler = new_channel;
1537 p->cls = new_channel_cls;
1538 GNUNET_assert (GNUNET_OK ==
1539 GNUNET_CONTAINER_multihashmap_put (h->ports,
1542 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1544 env = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_CADET_LOCAL_PORT_OPEN);
1545 msg->port = *p->hash;
1546 GNUNET_MQ_send (h->mq, env);
1552 * Close a port opened with @a GNUNET_CADET_open_port.
1553 * The @a new_channel callback will no longer be called.
1555 * @param p Port handle.
1558 GNUNET_CADET_close_port (struct GNUNET_CADET_Port *p)
1560 struct GNUNET_CADET_PortMessage *msg;
1561 struct GNUNET_MQ_Envelope *env;
1563 env = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_CADET_LOCAL_PORT_CLOSE);
1565 msg->port = *p->hash;
1566 GNUNET_MQ_send (p->cadet->mq, env);
1567 GNUNET_CONTAINER_multihashmap_remove (p->cadet->ports, p->hash, p);
1568 GNUNET_free (p->hash);
1574 * Create a new channel towards a remote peer.
1576 * If the destination port is not open by any peer or the destination peer
1577 * does not accept the channel, #GNUNET_CADET_ChannelEndHandler will be called
1580 * @param h cadet handle
1581 * @param channel_ctx client's channel context to associate with the channel
1582 * @param peer peer identity the channel should go to
1583 * @param port Port hash (port number).
1584 * @param options CadetOption flag field, with all desired option bits set to 1.
1585 * @return handle to the channel
1587 struct GNUNET_CADET_Channel *
1588 GNUNET_CADET_channel_create (struct GNUNET_CADET_Handle *h,
1590 const struct GNUNET_PeerIdentity *peer,
1591 const struct GNUNET_HashCode *port,
1592 enum GNUNET_CADET_ChannelOption options)
1594 struct GNUNET_CADET_LocalChannelCreateMessage *msg;
1595 struct GNUNET_MQ_Envelope *env;
1596 struct GNUNET_CADET_Channel *ch;
1597 struct GNUNET_CADET_ClientChannelNumber ccn;
1599 LOG (GNUNET_ERROR_TYPE_DEBUG,
1600 "Creating new channel to %s:%u\n",
1601 GNUNET_i2s (peer), port);
1602 ccn.channel_of_client = htonl (0);
1603 ch = create_channel (h, ccn);
1604 LOG (GNUNET_ERROR_TYPE_DEBUG, " at %p\n", ch);
1605 LOG (GNUNET_ERROR_TYPE_DEBUG, " number %X\n",
1606 ntohl (ch->ccn.channel_of_client));
1607 ch->ctx = channel_ctx;
1608 ch->peer = GNUNET_PEER_intern (peer);
1610 env = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_CADET_LOCAL_CHANNEL_CREATE);
1614 msg->opt = htonl (options);
1615 ch->allow_send = GNUNET_NO;
1616 GNUNET_MQ_send (h->mq,
1623 GNUNET_CADET_channel_destroy (struct GNUNET_CADET_Channel *channel)
1625 struct GNUNET_CADET_Handle *h;
1626 struct GNUNET_CADET_LocalChannelDestroyMessage *msg;
1627 struct GNUNET_MQ_Envelope *env;
1628 struct GNUNET_CADET_TransmitHandle *th;
1629 struct GNUNET_CADET_TransmitHandle *next;
1631 LOG (GNUNET_ERROR_TYPE_DEBUG, "Destroying channel\n");
1633 for (th = h->th_head; th != NULL; th = next)
1636 if (th->channel == channel)
1639 if (GNUNET_YES == th_is_payload (th))
1641 /* applications should cancel before destroying channel */
1642 LOG (GNUNET_ERROR_TYPE_WARNING,
1643 "Channel destroyed without cancelling transmission requests\n");
1644 th->notify (th->notify_cls, 0, NULL);
1648 LOG (GNUNET_ERROR_TYPE_WARNING, "no meta-traffic should be queued\n");
1650 GNUNET_CONTAINER_DLL_remove (h->th_head,
1653 GNUNET_CADET_notify_transmit_ready_cancel (th);
1657 env = GNUNET_MQ_msg (msg,
1658 GNUNET_MESSAGE_TYPE_CADET_LOCAL_CHANNEL_DESTROY);
1659 msg->ccn = channel->ccn;
1660 GNUNET_MQ_send (h->mq,
1663 destroy_channel (channel,
1669 * Get information about a channel.
1671 * @param channel Channel handle.
1672 * @param option Query (GNUNET_CADET_OPTION_*).
1673 * @param ... dependant on option, currently not used
1675 * @return Union with an answer to the query.
1677 const union GNUNET_CADET_ChannelInfo *
1678 GNUNET_CADET_channel_get_info (struct GNUNET_CADET_Channel *channel,
1679 enum GNUNET_CADET_ChannelOption option, ...)
1681 static int bool_flag;
1682 const union GNUNET_CADET_ChannelInfo *ret;
1686 case GNUNET_CADET_OPTION_NOBUFFER:
1687 case GNUNET_CADET_OPTION_RELIABLE:
1688 case GNUNET_CADET_OPTION_OUT_OF_ORDER:
1689 if (0 != (option & channel->options))
1690 bool_flag = GNUNET_YES;
1692 bool_flag = GNUNET_NO;
1693 ret = (const union GNUNET_CADET_ChannelInfo *) &bool_flag;
1695 case GNUNET_CADET_OPTION_PEER:
1696 ret = (const union GNUNET_CADET_ChannelInfo *) GNUNET_PEER_resolve2 (channel->peer);
1707 struct GNUNET_CADET_TransmitHandle *
1708 GNUNET_CADET_notify_transmit_ready (struct GNUNET_CADET_Channel *channel,
1710 struct GNUNET_TIME_Relative maxdelay,
1712 GNUNET_CONNECTION_TransmitReadyNotify notify,
1715 struct GNUNET_CADET_TransmitHandle *th;
1717 GNUNET_assert (NULL != channel);
1718 GNUNET_assert (GNUNET_CONSTANTS_MAX_CADET_MESSAGE_SIZE >= notify_size);
1719 LOG (GNUNET_ERROR_TYPE_DEBUG, "CADET NOTIFY TRANSMIT READY\n");
1720 LOG (GNUNET_ERROR_TYPE_DEBUG, " on channel %X\n", channel->ccn);
1721 LOG (GNUNET_ERROR_TYPE_DEBUG, " allow_send %d\n", channel->allow_send);
1722 if (ntohl (channel->ccn.channel_of_client) >=
1723 GNUNET_CADET_LOCAL_CHANNEL_ID_CLI)
1724 LOG (GNUNET_ERROR_TYPE_DEBUG, " to origin\n");
1726 LOG (GNUNET_ERROR_TYPE_DEBUG, " to destination\n");
1727 LOG (GNUNET_ERROR_TYPE_DEBUG, " payload size %u\n", notify_size);
1728 GNUNET_assert (NULL != notify);
1729 GNUNET_assert (0 == channel->packet_size); // Only one data packet allowed
1731 if (GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us != maxdelay.rel_value_us)
1733 LOG (GNUNET_ERROR_TYPE_WARNING,
1734 "CADET transmit ready timeout is deprected (has no effect)\n");
1737 th = GNUNET_new (struct GNUNET_CADET_TransmitHandle);
1738 th->channel = channel;
1739 th->size = notify_size;
1740 channel->packet_size = th->size;
1741 LOG (GNUNET_ERROR_TYPE_DEBUG, " total size %u\n", th->size);
1742 th->notify = notify;
1743 th->notify_cls = notify_cls;
1744 if (GNUNET_YES == channel->allow_send)
1745 th->request_data_task = GNUNET_SCHEDULER_add_now (&request_data, th);
1747 add_to_queue (channel->cadet, th);
1749 LOG (GNUNET_ERROR_TYPE_DEBUG, "CADET NOTIFY TRANSMIT READY END\n");
1755 GNUNET_CADET_notify_transmit_ready_cancel (struct GNUNET_CADET_TransmitHandle *th)
1757 if (NULL != th->request_data_task)
1759 GNUNET_SCHEDULER_cancel (th->request_data_task);
1761 th->request_data_task = NULL;
1763 remove_from_queue (th);
1769 GNUNET_CADET_receive_done (struct GNUNET_CADET_Channel *channel)
1776 send_info_request (struct GNUNET_CADET_Handle *h, uint16_t type)
1778 struct GNUNET_MessageHeader *msg;
1779 struct GNUNET_MQ_Envelope *env;
1781 LOG (GNUNET_ERROR_TYPE_DEBUG,
1782 " Sending %s monitor message to service\n",
1785 env = GNUNET_MQ_msg (msg, type);
1786 GNUNET_MQ_send (h->mq, env);
1791 * Request a debug dump on the service's STDERR.
1793 * WARNING: unstable API, likely to change in the future!
1795 * @param h cadet handle
1798 GNUNET_CADET_request_dump (struct GNUNET_CADET_Handle *h)
1800 LOG (GNUNET_ERROR_TYPE_DEBUG, "requesting dump\n");
1801 send_info_request (h, GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_DUMP);
1806 * Request information about peers known to the running cadet service.
1807 * The callback will be called for every peer known to the service.
1808 * Only one info request (of any kind) can be active at once.
1811 * WARNING: unstable API, likely to change in the future!
1813 * @param h Handle to the cadet peer.
1814 * @param callback Function to call with the requested data.
1815 * @param callback_cls Closure for @c callback.
1817 * @return #GNUNET_OK / #GNUNET_SYSERR
1820 GNUNET_CADET_get_peers (struct GNUNET_CADET_Handle *h,
1821 GNUNET_CADET_PeersCB callback,
1824 if (NULL != h->info_cb.peers_cb)
1827 return GNUNET_SYSERR;
1829 send_info_request (h, GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEERS);
1830 h->info_cb.peers_cb = callback;
1831 h->info_cls = callback_cls;
1837 * Cancel a peer info request. The callback will not be called (anymore).
1839 * WARNING: unstable API, likely to change in the future!
1841 * @param h Cadet handle.
1843 * @return Closure given to GNUNET_CADET_get_peers.
1846 GNUNET_CADET_get_peers_cancel (struct GNUNET_CADET_Handle *h)
1851 h->info_cb.peers_cb = NULL;
1858 * Request information about a peer known to the running cadet peer.
1859 * The callback will be called for the tunnel once.
1860 * Only one info request (of any kind) can be active at once.
1862 * WARNING: unstable API, likely to change in the future!
1864 * @param h Handle to the cadet peer.
1865 * @param id Peer whose tunnel to examine.
1866 * @param callback Function to call with the requested data.
1867 * @param callback_cls Closure for @c callback.
1869 * @return #GNUNET_OK / #GNUNET_SYSERR
1872 GNUNET_CADET_get_peer (struct GNUNET_CADET_Handle *h,
1873 const struct GNUNET_PeerIdentity *id,
1874 GNUNET_CADET_PeerCB callback,
1877 struct GNUNET_CADET_LocalInfo *msg;
1878 struct GNUNET_MQ_Envelope *env;
1880 if (NULL != h->info_cb.peer_cb)
1883 return GNUNET_SYSERR;
1886 env = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEER);
1888 GNUNET_MQ_send (h->mq, env);
1890 h->info_cb.peer_cb = callback;
1891 h->info_cls = callback_cls;
1897 * Request information about tunnels of the running cadet peer.
1898 * The callback will be called for every tunnel of the service.
1899 * Only one info request (of any kind) can be active at once.
1901 * WARNING: unstable API, likely to change in the future!
1903 * @param h Handle to the cadet peer.
1904 * @param callback Function to call with the requested data.
1905 * @param callback_cls Closure for @c callback.
1907 * @return #GNUNET_OK / #GNUNET_SYSERR
1910 GNUNET_CADET_get_tunnels (struct GNUNET_CADET_Handle *h,
1911 GNUNET_CADET_TunnelsCB callback,
1914 if (NULL != h->info_cb.tunnels_cb)
1917 return GNUNET_SYSERR;
1919 send_info_request (h, GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNELS);
1920 h->info_cb.tunnels_cb = callback;
1921 h->info_cls = callback_cls;
1927 * Cancel a monitor request. The monitor callback will not be called.
1929 * @param h Cadet handle.
1931 * @return Closure given to GNUNET_CADET_get_tunnels.
1934 GNUNET_CADET_get_tunnels_cancel (struct GNUNET_CADET_Handle *h)
1938 h->info_cb.tunnels_cb = NULL;
1948 * Request information about a tunnel of the running cadet peer.
1949 * The callback will be called for the tunnel once.
1950 * Only one info request (of any kind) can be active at once.
1952 * WARNING: unstable API, likely to change in the future!
1954 * @param h Handle to the cadet peer.
1955 * @param id Peer whose tunnel to examine.
1956 * @param callback Function to call with the requested data.
1957 * @param callback_cls Closure for @c callback.
1959 * @return #GNUNET_OK / #GNUNET_SYSERR
1962 GNUNET_CADET_get_tunnel (struct GNUNET_CADET_Handle *h,
1963 const struct GNUNET_PeerIdentity *id,
1964 GNUNET_CADET_TunnelCB callback,
1967 struct GNUNET_CADET_LocalInfo *msg;
1968 struct GNUNET_MQ_Envelope *env;
1970 if (NULL != h->info_cb.tunnel_cb)
1973 return GNUNET_SYSERR;
1976 env = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNEL);
1978 GNUNET_MQ_send (h->mq, env);
1980 h->info_cb.tunnel_cb = callback;
1981 h->info_cls = callback_cls;
1987 * Request information about a specific channel of the running cadet peer.
1989 * WARNING: unstable API, likely to change in the future!
1990 * FIXME Add destination option.
1992 * @param h Handle to the cadet peer.
1993 * @param initiator ID of the owner of the channel.
1994 * @param channel_number Channel number.
1995 * @param callback Function to call with the requested data.
1996 * @param callback_cls Closure for @c callback.
1998 * @return #GNUNET_OK / #GNUNET_SYSERR
2001 GNUNET_CADET_show_channel (struct GNUNET_CADET_Handle *h,
2002 struct GNUNET_PeerIdentity *initiator,
2003 unsigned int channel_number,
2004 GNUNET_CADET_ChannelCB callback,
2007 struct GNUNET_CADET_LocalInfo *msg;
2008 struct GNUNET_MQ_Envelope *env;
2010 if (NULL != h->info_cb.channel_cb)
2013 return GNUNET_SYSERR;
2016 env = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_CHANNEL);
2017 msg->peer = *initiator;
2018 msg->ccn.channel_of_client = htonl (channel_number);
2019 GNUNET_MQ_send (h->mq, env);
2021 h->info_cb.channel_cb = callback;
2022 h->info_cls = callback_cls;
2028 * Function called to notify a client about the connection
2029 * begin ready to queue more data. "buf" will be
2030 * NULL and "size" zero if the connection was closed for
2031 * writing in the meantime.
2033 * @param cls closure
2034 * @param size number of bytes available in buf
2035 * @param buf where the callee should write the message
2036 * @return number of bytes written to buf
2039 cadet_mq_ntr (void *cls, size_t size,
2042 struct GNUNET_MQ_Handle *mq = cls;
2043 struct CadetMQState *state = GNUNET_MQ_impl_state (mq);
2044 const struct GNUNET_MessageHeader *msg = GNUNET_MQ_impl_current (mq);
2050 GNUNET_MQ_inject_error (mq, GNUNET_MQ_ERROR_WRITE);
2053 msize = ntohs (msg->size);
2054 GNUNET_assert (msize <= size);
2055 GNUNET_memcpy (buf, msg, msize);
2056 GNUNET_MQ_impl_send_continue (mq);
2062 * Signature of functions implementing the
2063 * sending functionality of a message queue.
2065 * @param mq the message queue
2066 * @param msg the message to send
2067 * @param impl_state state of the implementation
2070 cadet_mq_send_impl (struct GNUNET_MQ_Handle *mq,
2071 const struct GNUNET_MessageHeader *msg,
2074 struct CadetMQState *state = impl_state;
2076 GNUNET_assert (NULL == state->th);
2078 GNUNET_CADET_notify_transmit_ready (state->channel,
2079 /* FIXME: add option for corking */
2081 GNUNET_TIME_UNIT_FOREVER_REL,
2089 * Signature of functions implementing the
2090 * destruction of a message queue.
2091 * Implementations must not free 'mq', but should
2092 * take care of 'impl_state'.
2094 * @param mq the message queue to destroy
2095 * @param impl_state state of the implementation
2098 cadet_mq_destroy_impl (struct GNUNET_MQ_Handle *mq,
2101 struct CadetMQState *state = impl_state;
2103 if (NULL != state->th)
2104 GNUNET_CADET_notify_transmit_ready_cancel (state->th);
2106 GNUNET_free (state);
2111 * Create a message queue for a cadet channel.
2112 * The message queue can only be used to transmit messages,
2113 * not to receive them.
2115 * @param channel the channel to create the message qeue for
2116 * @return a message queue to messages over the channel
2118 struct GNUNET_MQ_Handle *
2119 GNUNET_CADET_mq_create (struct GNUNET_CADET_Channel *channel)
2121 struct GNUNET_MQ_Handle *mq;
2122 struct CadetMQState *state;
2124 state = GNUNET_new (struct CadetMQState);
2125 state->channel = channel;
2127 mq = GNUNET_MQ_queue_for_callbacks (&cadet_mq_send_impl,
2128 &cadet_mq_destroy_impl,
2129 NULL, /* FIXME: cancel impl. */
2131 NULL, /* no msg handlers */
2132 NULL, /* no err handlers */
2133 NULL); /* no handler cls */
2139 * Transitional function to convert an unsigned int port to a hash value.
2140 * WARNING: local static value returned, NOT reentrant!
2141 * WARNING: do not use this function for new code!
2143 * @param port Numerical port (unsigned int format).
2145 * @return A GNUNET_HashCode usable for the new CADET API.
2147 const struct GNUNET_HashCode *
2148 GC_u2h (uint32_t port)
2150 static struct GNUNET_HashCode hash;
2152 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2153 "This is a transitional function, "
2154 "use proper crypto hashes as CADET ports\n");
2155 GNUNET_CRYPTO_hash (&port, sizeof (port), &hash);