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, "Requesting Data: %u bytes\n", th->size);
569 GNUNET_assert (GNUNET_YES == th->channel->allow_send);
570 th->channel->allow_send = GNUNET_NO;
571 th->request_data_task = NULL;
572 th->channel->packet_size = 0;
573 remove_from_queue (th);
575 env = GNUNET_MQ_msg_extra (msg,
577 GNUNET_MESSAGE_TYPE_CADET_LOCAL_DATA);
578 msg->ccn = th->channel->ccn;
579 osize = th->notify (th->notify_cls,
582 GNUNET_assert (osize == th->size);
583 GNUNET_MQ_send (th->channel->cadet->mq,
590 * Process the new channel notification and add it to the channels in the handle
592 * @param h The cadet handle
593 * @param msg A message with the details of the new incoming channel
596 handle_channel_created (void *cls,
597 const struct GNUNET_CADET_LocalChannelCreateMessage *msg)
599 struct GNUNET_CADET_Handle *h = cls;
600 struct GNUNET_CADET_Channel *ch;
601 struct GNUNET_CADET_Port *port;
602 const struct GNUNET_HashCode *port_number;
603 struct GNUNET_CADET_ClientChannelNumber ccn;
606 port_number = &msg->port;
607 LOG (GNUNET_ERROR_TYPE_DEBUG,
608 "Creating incoming channel %X [%s]\n",
609 ntohl (ccn.channel_of_client),
610 GNUNET_h2s (port_number));
611 if (ntohl (ccn.channel_of_client) >= GNUNET_CADET_LOCAL_CHANNEL_ID_CLI)
616 port = find_port (h, port_number);
621 ch = create_channel (h, ccn);
622 ch->allow_send = GNUNET_NO;
623 ch->peer = GNUNET_PEER_intern (&msg->peer);
627 ch->options = ntohl (msg->opt);
629 LOG (GNUNET_ERROR_TYPE_DEBUG, " created channel %p\n", ch);
630 ctx = port->handler (port->cls, ch, &msg->peer, port->hash, ch->options);
633 LOG (GNUNET_ERROR_TYPE_DEBUG, "User notified\n");
637 struct GNUNET_CADET_LocalChannelDestroyMessage *d_msg;
638 struct GNUNET_MQ_Envelope *env;
640 LOG (GNUNET_ERROR_TYPE_DEBUG, "No handler for incoming channels\n");
641 env = GNUNET_MQ_msg (d_msg,
642 GNUNET_MESSAGE_TYPE_CADET_LOCAL_CHANNEL_DESTROY);
643 d_msg->ccn = msg->ccn;
644 GNUNET_MQ_send (h->mq, env);
651 * Process the channel destroy notification and free associated resources
653 * @param h The cadet handle
654 * @param msg A message with the details of the channel being destroyed
657 handle_channel_destroy (void *cls,
658 const struct GNUNET_CADET_LocalChannelDestroyMessage *msg)
660 struct GNUNET_CADET_Handle *h = cls;
661 struct GNUNET_CADET_Channel *ch;
662 struct GNUNET_CADET_ClientChannelNumber ccn;
665 LOG (GNUNET_ERROR_TYPE_DEBUG,
666 "Channel %X Destroy from service\n",
667 ntohl (ccn.channel_of_client));
668 ch = retrieve_channel (h,
673 LOG (GNUNET_ERROR_TYPE_DEBUG,
674 "channel %X unknown\n",
675 ntohl (ccn.channel_of_client));
684 * Check that message received from CADET service is well-formed.
686 * @param cls the `struct GNUNET_CADET_Handle`
687 * @param message the message we got
688 * @return #GNUNET_OK if the message is well-formed,
689 * #GNUNET_SYSERR otherwise
692 check_local_data (void *cls,
693 const struct GNUNET_CADET_LocalData *message)
695 struct GNUNET_CADET_Handle *h = cls;
696 struct GNUNET_CADET_Channel *ch;
699 size = ntohs (message->header.size);
700 if (sizeof (*message) + sizeof (struct GNUNET_MessageHeader) > size)
703 return GNUNET_SYSERR;
706 ch = retrieve_channel (h,
711 return GNUNET_SYSERR;
719 * Process the incoming data packets, call appropriate handlers.
721 * @param h The cadet handle
722 * @param message A message encapsulating the data
725 handle_local_data (void *cls,
726 const struct GNUNET_CADET_LocalData *message)
728 struct GNUNET_CADET_Handle *h = cls;
729 const struct GNUNET_MessageHeader *payload;
730 const struct GNUNET_CADET_MessageHandler *handler;
731 struct GNUNET_CADET_Channel *ch;
735 LOG (GNUNET_ERROR_TYPE_DEBUG,
736 "Got a data message!\n");
737 ch = retrieve_channel (h,
739 GNUNET_assert (NULL != ch);
741 payload = (struct GNUNET_MessageHeader *) &message[1];
742 LOG (GNUNET_ERROR_TYPE_DEBUG, " %s data on channel %s [%X]\n",
743 GC_f2s (ntohl (ch->ccn.channel_of_client) >=
744 GNUNET_CADET_LOCAL_CHANNEL_ID_CLI),
745 GNUNET_i2s (GNUNET_PEER_resolve2 (ch->peer)),
746 ntohl (message->ccn.channel_of_client));
748 type = ntohs (payload->type);
749 LOG (GNUNET_ERROR_TYPE_DEBUG, " payload type %s\n", GC_m2s (type));
750 for (i = 0; i < h->n_handlers; i++)
752 handler = &h->message_handlers[i];
753 LOG (GNUNET_ERROR_TYPE_DEBUG, " checking handler for type %u\n",
755 if (handler->type == type)
758 handler->callback (h->cls, ch, &ch->ctx, payload))
760 LOG (GNUNET_ERROR_TYPE_DEBUG, "callback caused disconnection\n");
761 GNUNET_CADET_channel_destroy (ch);
766 LOG (GNUNET_ERROR_TYPE_DEBUG,
767 "callback completed successfully\n");
776 * Process a local ACK message, enabling the client to send
777 * more data to the service.
779 * @param h Cadet handle.
780 * @param message Message itself.
783 handle_local_ack (void *cls,
784 const struct GNUNET_CADET_LocalAck *message)
786 struct GNUNET_CADET_Handle *h = cls;
787 struct GNUNET_CADET_Channel *ch;
788 struct GNUNET_CADET_ClientChannelNumber ccn;
791 ch = retrieve_channel (h, ccn);
794 LOG (GNUNET_ERROR_TYPE_DEBUG,
795 "ACK on unknown channel %X\n",
796 ntohl (ccn.channel_of_client));
799 LOG (GNUNET_ERROR_TYPE_DEBUG,
800 "Got an ACK on channel %X!\n",
801 ntohl (ch->ccn.channel_of_client));
802 ch->allow_send = GNUNET_YES;
803 if (0 < ch->packet_size)
805 struct GNUNET_CADET_TransmitHandle *th;
806 struct GNUNET_CADET_TransmitHandle *next;
807 LOG (GNUNET_ERROR_TYPE_DEBUG,
808 " pending data, sending %u bytes!\n",
810 for (th = h->th_head; NULL != th; th = next)
813 if (th->channel == ch)
815 GNUNET_assert (NULL == th->request_data_task);
816 th->request_data_task = GNUNET_SCHEDULER_add_now (&request_data, th);
820 /* Complain if we got thru all th without sending anything, ch was wrong */
821 GNUNET_break (NULL != th);
826 * Reconnect to the service, retransmit all infomation to try to restore the
829 * @param h handle to the cadet
831 * @return #GNUNET_YES in case of sucess, #GNUNET_NO otherwise (service down...)
834 reconnect (struct GNUNET_CADET_Handle *h);
838 * Reconnect callback: tries to reconnect again after a failer previous
841 * @param cls closure (cadet handle)
844 reconnect_cbk (void *cls);
848 * Generic error handler, called with the appropriate error code and
849 * the same closure specified at the creation of the message queue.
850 * Not every message queue implementation supports an error handler.
852 * @param cls closure, a `struct GNUNET_CORE_Handle *`
853 * @param error error code
856 handle_mq_error (void *cls,
857 enum GNUNET_MQ_Error error)
859 struct GNUNET_CADET_Handle *h = cls;
861 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MQ ERROR: %u\n", error);
862 GNUNET_MQ_destroy (h->mq);
869 * Process a local reply about info on all channels, pass info to the user.
871 * @param h Cadet handle.
872 * @param message Message itself.
875 // process_get_channels (struct GNUNET_CADET_Handle *h,
876 // const struct GNUNET_MessageHeader *message)
878 // struct GNUNET_CADET_LocalInfo *msg;
880 // GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Get Channels messasge received\n");
882 // if (NULL == h->channels_cb)
884 // GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " ignored\n");
888 // msg = (struct GNUNET_CADET_LocalInfo *) message;
889 // if (ntohs (message->size) !=
890 // (sizeof (struct GNUNET_CADET_LocalInfo) +
891 // sizeof (struct GNUNET_PeerIdentity)))
893 // GNUNET_break_op (0);
894 // GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
895 // "Get channels message: size %hu - expected %u\n",
896 // ntohs (message->size),
897 // sizeof (struct GNUNET_CADET_LocalInfo));
900 // h->channels_cb (h->channels_cls,
901 // ntohl (msg->channel_id),
903 // &msg->destination);
909 * Process a local monitor_channel reply, pass info to the user.
911 * @param h Cadet handle.
912 * @param message Message itself.
915 // process_show_channel (struct GNUNET_CADET_Handle *h,
916 // const struct GNUNET_MessageHeader *message)
918 // struct GNUNET_CADET_LocalInfo *msg;
921 // GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Show Channel messasge received\n");
923 // if (NULL == h->channel_cb)
925 // GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " ignored\n");
929 // /* Verify message sanity */
930 // msg = (struct GNUNET_CADET_LocalInfo *) message;
931 // esize = sizeof (struct GNUNET_CADET_LocalInfo);
932 // if (ntohs (message->size) != esize)
934 // GNUNET_break_op (0);
935 // GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
936 // "Show channel message: size %hu - expected %u\n",
937 // ntohs (message->size),
940 // h->channel_cb (h->channel_cls, NULL, NULL);
941 // h->channel_cb = NULL;
942 // h->channel_cls = NULL;
947 // h->channel_cb (h->channel_cls,
948 // &msg->destination,
955 * Check that message received from CADET service is well-formed.
957 * @param cls the `struct GNUNET_CADET_Handle`
958 * @param message the message we got
959 * @return #GNUNET_OK if the message is well-formed,
960 * #GNUNET_SYSERR otherwise
963 check_get_peers (void *cls,
964 const struct GNUNET_CADET_LocalInfoPeer *message)
966 struct GNUNET_CADET_Handle *h = cls;
969 if (NULL == h->info_cb.peers_cb)
971 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
972 " no handler for peesr monitor message!\n");
973 return GNUNET_SYSERR;
976 size = ntohs (message->header.size);
977 if (sizeof (struct GNUNET_CADET_LocalInfoPeer) > size)
979 h->info_cb.peers_cb (h->info_cls, NULL, -1, 0, 0);
980 h->info_cb.peers_cb = NULL;
982 return GNUNET_SYSERR;
990 * Process a local reply about info on all tunnels, pass info to the user.
992 * @param cls Closure (Cadet handle).
993 * @param msg Message itself.
996 handle_get_peers (void *cls,
997 const struct GNUNET_CADET_LocalInfoPeer *msg)
999 struct GNUNET_CADET_Handle *h = cls;
1000 h->info_cb.peers_cb (h->info_cls, &msg->destination,
1001 (int) ntohs (msg->tunnel),
1002 (unsigned int ) ntohs (msg->paths),
1008 * Check that message received from CADET service is well-formed.
1010 * @param cls the `struct GNUNET_CADET_Handle`
1011 * @param message the message we got
1012 * @return #GNUNET_OK if the message is well-formed,
1013 * #GNUNET_SYSERR otherwise
1016 check_get_peer (void *cls,
1017 const struct GNUNET_CADET_LocalInfoPeer *message)
1019 struct GNUNET_CADET_Handle *h = cls;
1020 const size_t msize = sizeof (struct GNUNET_CADET_LocalInfoPeer);
1021 struct GNUNET_PeerIdentity *paths_array;
1023 unsigned int epaths;
1027 if (NULL == h->info_cb.peer_cb)
1029 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1030 " no handler for peer monitor message!\n");
1034 /* Verify message sanity */
1035 esize = ntohs (message->header.size);
1038 GNUNET_break_op (0);
1039 h->info_cb.peer_cb (h->info_cls, NULL, 0, 0, 0, NULL);
1042 if (0 != ((esize - msize) % sizeof (struct GNUNET_PeerIdentity)))
1044 GNUNET_break_op (0);
1045 h->info_cb.peer_cb (h->info_cls, NULL, 0, 0, 0, NULL);
1049 peers = (esize - msize) / sizeof (struct GNUNET_PeerIdentity);
1050 epaths = (unsigned int) ntohs (message->paths);
1051 paths_array = (struct GNUNET_PeerIdentity *) &message[1];
1053 for (int i = 0; i < peers; i++)
1055 if (0 == memcmp (&paths_array[i], &message->destination,
1056 sizeof (struct GNUNET_PeerIdentity)))
1061 if (paths != epaths)
1063 GNUNET_break_op (0);
1064 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "p:%u, e: %u\n", paths, epaths);
1065 h->info_cb.peer_cb (h->info_cls, NULL, 0, 0, 0, NULL);
1072 h->info_cb.peer_cb = NULL;
1074 return GNUNET_SYSERR;
1079 * Process a local peer info reply, pass info to the user.
1081 * @param cls Closure (Cadet handle).
1082 * @param message Message itself.
1085 handle_get_peer (void *cls,
1086 const struct GNUNET_CADET_LocalInfoPeer *message)
1088 struct GNUNET_CADET_Handle *h = cls;
1089 struct GNUNET_PeerIdentity *paths_array;
1091 unsigned int path_length;
1095 paths = (unsigned int) ntohs (message->paths);
1096 paths_array = (struct GNUNET_PeerIdentity *) &message[1];
1097 peers = (ntohs (message->header.size) - sizeof (*message))
1098 / sizeof (struct GNUNET_PeerIdentity);
1100 neighbor = GNUNET_NO;
1102 for (int i = 0; i < peers; i++)
1104 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " %s\n", GNUNET_i2s (&paths_array[i]));
1106 if (0 == memcmp (&paths_array[i], &message->destination,
1107 sizeof (struct GNUNET_PeerIdentity)))
1109 if (1 == path_length)
1110 neighbor = GNUNET_YES;
1115 /* Call Callback with tunnel info. */
1116 paths_array = (struct GNUNET_PeerIdentity *) &message[1];
1117 h->info_cb.peer_cb (h->info_cls,
1118 &message->destination,
1119 (int) ntohs (message->tunnel),
1127 * Check that message received from CADET service is well-formed.
1129 * @param cls the `struct GNUNET_CADET_Handle`
1130 * @param msg the message we got
1131 * @return #GNUNET_OK if the message is well-formed,
1132 * #GNUNET_SYSERR otherwise
1135 check_get_tunnels (void *cls,
1136 const struct GNUNET_CADET_LocalInfoTunnel *msg)
1138 struct GNUNET_CADET_Handle *h = cls;
1141 if (NULL == h->info_cb.tunnels_cb)
1143 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1144 " no handler for tunnels monitor message!\n");
1145 return GNUNET_SYSERR;
1148 size = ntohs (msg->header.size);
1149 if (sizeof (struct GNUNET_CADET_LocalInfoTunnel) > size)
1151 h->info_cb.tunnels_cb (h->info_cls, NULL, 0, 0, 0, 0);
1152 h->info_cb.tunnels_cb = NULL;
1154 return GNUNET_SYSERR;
1161 * Process a local reply about info on all tunnels, pass info to the user.
1163 * @param cls Closure (Cadet handle).
1164 * @param message Message itself.
1167 handle_get_tunnels (void *cls,
1168 const struct GNUNET_CADET_LocalInfoTunnel *msg)
1170 struct GNUNET_CADET_Handle *h = cls;
1172 h->info_cb.tunnels_cb (h->info_cls,
1174 ntohl (msg->channels),
1175 ntohl (msg->connections),
1176 ntohs (msg->estate),
1177 ntohs (msg->cstate));
1183 * Check that message received from CADET service is well-formed.
1185 * @param cls the `struct GNUNET_CADET_Handle`
1186 * @param msg the message we got
1187 * @return #GNUNET_OK if the message is well-formed,
1188 * #GNUNET_SYSERR otherwise
1191 check_get_tunnel (void *cls,
1192 const struct GNUNET_CADET_LocalInfoTunnel *msg)
1194 struct GNUNET_CADET_Handle *h = cls;
1200 if (NULL == h->info_cb.tunnel_cb)
1202 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1203 " no handler for tunnel monitor message!\n");
1207 /* Verify message sanity */
1208 msize = ntohs (msg->header.size);
1209 esize = sizeof (struct GNUNET_CADET_LocalInfoTunnel);
1212 GNUNET_break_op (0);
1213 h->info_cb.tunnel_cb (h->info_cls,
1214 NULL, 0, 0, NULL, NULL, 0, 0);
1217 ch_n = ntohl (msg->channels);
1218 c_n = ntohl (msg->connections);
1219 esize += ch_n * sizeof (struct GNUNET_CADET_ChannelTunnelNumber);
1220 esize += c_n * sizeof (struct GNUNET_CADET_ConnectionTunnelIdentifier);
1223 GNUNET_break_op (0);
1224 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1225 "m:%u, e: %u (%u ch, %u conn)\n",
1226 (unsigned int) msize,
1227 (unsigned int) esize,
1230 h->info_cb.tunnel_cb (h->info_cls,
1231 NULL, 0, 0, NULL, NULL, 0, 0);
1238 h->info_cb.tunnel_cb = NULL;
1240 return GNUNET_SYSERR;
1245 * Process a local tunnel info reply, pass info to the user.
1247 * @param cls Closure (Cadet handle).
1248 * @param msg Message itself.
1251 handle_get_tunnel (void *cls,
1252 const struct GNUNET_CADET_LocalInfoTunnel *msg)
1254 struct GNUNET_CADET_Handle *h = cls;
1257 const struct GNUNET_CADET_ConnectionTunnelIdentifier *conns;
1258 const struct GNUNET_CADET_ChannelTunnelNumber *chns;
1260 ch_n = ntohl (msg->channels);
1261 c_n = ntohl (msg->connections);
1263 /* Call Callback with tunnel info. */
1264 conns = (const struct GNUNET_CADET_ConnectionTunnelIdentifier *) &msg[1];
1265 chns = (const struct GNUNET_CADET_ChannelTunnelNumber *) &conns[c_n];
1266 h->info_cb.tunnel_cb (h->info_cls,
1272 ntohs (msg->estate),
1273 ntohs (msg->cstate));
1279 * Reconnect to the service, retransmit all infomation to try to restore the
1282 * @param h handle to the cadet
1284 * @return GNUNET_YES in case of sucess, GNUNET_NO otherwise (service down...)
1287 do_reconnect (struct GNUNET_CADET_Handle *h)
1289 struct GNUNET_MQ_MessageHandler handlers[] = {
1290 GNUNET_MQ_hd_fixed_size (channel_created,
1291 GNUNET_MESSAGE_TYPE_CADET_LOCAL_CHANNEL_CREATE,
1292 struct GNUNET_CADET_LocalChannelCreateMessage,
1294 GNUNET_MQ_hd_fixed_size (channel_destroy,
1295 GNUNET_MESSAGE_TYPE_CADET_LOCAL_CHANNEL_DESTROY,
1296 struct GNUNET_CADET_LocalChannelDestroyMessage,
1298 GNUNET_MQ_hd_var_size (local_data,
1299 GNUNET_MESSAGE_TYPE_CADET_LOCAL_DATA,
1300 struct GNUNET_CADET_LocalData,
1302 GNUNET_MQ_hd_fixed_size (local_ack,
1303 GNUNET_MESSAGE_TYPE_CADET_LOCAL_ACK,
1304 struct GNUNET_CADET_LocalAck,
1306 GNUNET_MQ_hd_var_size (get_peers,
1307 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEERS,
1308 struct GNUNET_CADET_LocalInfoPeer,
1310 GNUNET_MQ_hd_var_size (get_peer,
1311 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEER,
1312 struct GNUNET_CADET_LocalInfoPeer,
1314 GNUNET_MQ_hd_var_size (get_tunnels,
1315 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNELS,
1316 struct GNUNET_CADET_LocalInfoTunnel,
1318 GNUNET_MQ_hd_var_size (get_tunnel,
1319 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNEL,
1320 struct GNUNET_CADET_LocalInfoTunnel,
1323 // GNUNET_MQ_hd_fixed_Y size (channel_destroyed,
1324 // GNUNET_MESSAGE_TYPE_CADET_CHANNEL_OPEN_NACK_DEPRECATED,
1325 // struct GNUNET_CADET_ChannelDestroyMessage);
1326 GNUNET_MQ_handler_end ()
1329 LOG (GNUNET_ERROR_TYPE_DEBUG, "Connecting to CADET\n");
1331 GNUNET_assert (NULL == h->mq);
1332 h->mq = GNUNET_CLIENT_connect (h->cfg,
1344 h->reconnect_time = GNUNET_TIME_UNIT_MILLISECONDS;
1350 * Reconnect callback: tries to reconnect again after a failer previous
1353 * @param cls closure (cadet handle)
1356 reconnect_cbk (void *cls)
1358 struct GNUNET_CADET_Handle *h = cls;
1360 h->reconnect_task = NULL;
1366 * Reconnect to the service, retransmit all infomation to try to restore the
1369 * @param h handle to the cadet
1371 * @return #GNUNET_YES in case of sucess, #GNUNET_NO otherwise (service down...)
1374 reconnect (struct GNUNET_CADET_Handle *h)
1376 struct GNUNET_CADET_Channel *ch;
1378 LOG (GNUNET_ERROR_TYPE_DEBUG,
1379 "Requested RECONNECT, destroying all channels\n");
1380 for (ch = h->channels_head; NULL != ch; ch = h->channels_head)
1381 destroy_channel (ch, GNUNET_YES);
1382 if (NULL == h->reconnect_task)
1383 h->reconnect_task = GNUNET_SCHEDULER_add_delayed (h->reconnect_time,
1388 /******************************************************************************/
1389 /********************** API CALL DEFINITIONS *************************/
1390 /******************************************************************************/
1392 struct GNUNET_CADET_Handle *
1393 GNUNET_CADET_connect (const struct GNUNET_CONFIGURATION_Handle *cfg,
1395 GNUNET_CADET_ChannelEndHandler cleaner,
1396 const struct GNUNET_CADET_MessageHandler *handlers)
1398 struct GNUNET_CADET_Handle *h;
1400 LOG (GNUNET_ERROR_TYPE_DEBUG, "GNUNET_CADET_connect()\n");
1401 h = GNUNET_new (struct GNUNET_CADET_Handle);
1402 LOG (GNUNET_ERROR_TYPE_DEBUG, " addr %p\n", h);
1404 h->cleaner = cleaner;
1405 h->ports = GNUNET_CONTAINER_multihashmap_create (4, GNUNET_YES);
1410 GNUNET_CADET_disconnect (h);
1414 h->message_handlers = handlers;
1415 h->next_ccn.channel_of_client = htonl (GNUNET_CADET_LOCAL_CHANNEL_ID_CLI);
1416 h->reconnect_time = GNUNET_TIME_UNIT_MILLISECONDS;
1417 h->reconnect_task = NULL;
1419 /* count handlers */
1420 for (h->n_handlers = 0;
1421 handlers && handlers[h->n_handlers].type;
1423 LOG (GNUNET_ERROR_TYPE_DEBUG, "GNUNET_CADET_connect() END\n");
1429 * Disconnect from the cadet service. All channels will be destroyed. All channel
1430 * disconnect callbacks will be called on any still connected peers, notifying
1431 * about their disconnection. The registered inbound channel cleaner will be
1432 * called should any inbound channels still exist.
1434 * @param handle connection to cadet to disconnect
1437 GNUNET_CADET_disconnect (struct GNUNET_CADET_Handle *handle)
1439 struct GNUNET_CADET_Channel *ch;
1440 struct GNUNET_CADET_Channel *aux;
1441 struct GNUNET_CADET_TransmitHandle *th;
1443 LOG (GNUNET_ERROR_TYPE_DEBUG,
1444 "CADET DISCONNECT\n");
1445 ch = handle->channels_head;
1449 if (ntohl (ch->ccn.channel_of_client) >= GNUNET_CADET_LOCAL_CHANNEL_ID_CLI)
1452 LOG (GNUNET_ERROR_TYPE_DEBUG,
1453 "channel %X not destroyed\n",
1454 ntohl (ch->ccn.channel_of_client));
1456 destroy_channel (ch,
1460 while (NULL != (th = handle->th_head))
1462 struct GNUNET_MessageHeader *msg;
1464 /* Make sure it is an allowed packet (everything else should have been
1465 * already canceled).
1467 GNUNET_break (GNUNET_NO == th_is_payload (th));
1468 msg = (struct GNUNET_MessageHeader *) &th[1];
1469 switch (ntohs(msg->type))
1471 case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_OPEN:
1472 case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DESTROY:
1473 case GNUNET_MESSAGE_TYPE_CADET_LOCAL_PORT_OPEN:
1474 case GNUNET_MESSAGE_TYPE_CADET_LOCAL_PORT_CLOSE:
1475 case GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_CHANNELS:
1476 case GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_CHANNEL:
1477 case GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEER:
1478 case GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEERS:
1479 case GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNEL:
1480 case GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNELS:
1484 LOG (GNUNET_ERROR_TYPE_ERROR, "unexpected unsent msg %s\n",
1485 GC_m2s (ntohs(msg->type)));
1488 GNUNET_CADET_notify_transmit_ready_cancel (th);
1491 if (NULL != handle->mq)
1493 GNUNET_MQ_destroy (handle->mq);
1496 if (NULL != handle->reconnect_task)
1498 GNUNET_SCHEDULER_cancel(handle->reconnect_task);
1499 handle->reconnect_task = NULL;
1502 GNUNET_CONTAINER_multihashmap_destroy (handle->ports);
1503 handle->ports = NULL;
1504 GNUNET_free (handle);
1509 * Open a port to receive incomming channels.
1511 * @param h CADET handle.
1512 * @param port Hash representing the port number.
1513 * @param new_channel Function called when an channel is received.
1514 * @param new_channel_cls Closure for @a new_channel.
1516 * @return Port handle.
1518 struct GNUNET_CADET_Port *
1519 GNUNET_CADET_open_port (struct GNUNET_CADET_Handle *h,
1520 const struct GNUNET_HashCode *port,
1521 GNUNET_CADET_InboundChannelNotificationHandler
1523 void *new_channel_cls)
1525 struct GNUNET_CADET_PortMessage *msg;
1526 struct GNUNET_MQ_Envelope *env;
1527 struct GNUNET_CADET_Port *p;
1529 GNUNET_assert (NULL != new_channel);
1530 p = GNUNET_new (struct GNUNET_CADET_Port);
1532 p->hash = GNUNET_new (struct GNUNET_HashCode);
1534 p->handler = new_channel;
1535 p->cls = new_channel_cls;
1536 GNUNET_assert (GNUNET_OK ==
1537 GNUNET_CONTAINER_multihashmap_put (h->ports,
1540 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1542 env = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_CADET_LOCAL_PORT_OPEN);
1543 msg->port = *p->hash;
1544 GNUNET_MQ_send (h->mq, env);
1550 * Close a port opened with @a GNUNET_CADET_open_port.
1551 * The @a new_channel callback will no longer be called.
1553 * @param p Port handle.
1556 GNUNET_CADET_close_port (struct GNUNET_CADET_Port *p)
1558 struct GNUNET_CADET_PortMessage *msg;
1559 struct GNUNET_MQ_Envelope *env;
1561 env = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_CADET_LOCAL_PORT_CLOSE);
1563 msg->port = *p->hash;
1564 GNUNET_MQ_send (p->cadet->mq, env);
1565 GNUNET_CONTAINER_multihashmap_remove (p->cadet->ports, p->hash, p);
1566 GNUNET_free (p->hash);
1572 * Create a new channel towards a remote peer.
1574 * If the destination port is not open by any peer or the destination peer
1575 * does not accept the channel, #GNUNET_CADET_ChannelEndHandler will be called
1578 * @param h cadet handle
1579 * @param channel_ctx client's channel context to associate with the channel
1580 * @param peer peer identity the channel should go to
1581 * @param port Port hash (port number).
1582 * @param options CadetOption flag field, with all desired option bits set to 1.
1583 * @return handle to the channel
1585 struct GNUNET_CADET_Channel *
1586 GNUNET_CADET_channel_create (struct GNUNET_CADET_Handle *h,
1588 const struct GNUNET_PeerIdentity *peer,
1589 const struct GNUNET_HashCode *port,
1590 enum GNUNET_CADET_ChannelOption options)
1592 struct GNUNET_CADET_LocalChannelCreateMessage *msg;
1593 struct GNUNET_MQ_Envelope *env;
1594 struct GNUNET_CADET_Channel *ch;
1595 struct GNUNET_CADET_ClientChannelNumber ccn;
1597 LOG (GNUNET_ERROR_TYPE_DEBUG,
1598 "Creating new channel to %s:%u\n",
1599 GNUNET_i2s (peer), port);
1600 ccn.channel_of_client = htonl (0);
1601 ch = create_channel (h, ccn);
1602 LOG (GNUNET_ERROR_TYPE_DEBUG, " at %p\n", ch);
1603 LOG (GNUNET_ERROR_TYPE_DEBUG, " number %X\n",
1604 ntohl (ch->ccn.channel_of_client));
1605 ch->ctx = channel_ctx;
1606 ch->peer = GNUNET_PEER_intern (peer);
1608 env = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_CADET_LOCAL_CHANNEL_CREATE);
1612 msg->opt = htonl (options);
1613 ch->allow_send = GNUNET_NO;
1614 GNUNET_MQ_send (h->mq,
1621 GNUNET_CADET_channel_destroy (struct GNUNET_CADET_Channel *channel)
1623 struct GNUNET_CADET_Handle *h;
1624 struct GNUNET_CADET_LocalChannelDestroyMessage *msg;
1625 struct GNUNET_MQ_Envelope *env;
1626 struct GNUNET_CADET_TransmitHandle *th;
1627 struct GNUNET_CADET_TransmitHandle *next;
1629 LOG (GNUNET_ERROR_TYPE_DEBUG, "Destroying channel\n");
1631 for (th = h->th_head; th != NULL; th = next)
1634 if (th->channel == channel)
1637 if (GNUNET_YES == th_is_payload (th))
1639 /* applications should cancel before destroying channel */
1640 LOG (GNUNET_ERROR_TYPE_WARNING,
1641 "Channel destroyed without cancelling transmission requests\n");
1642 th->notify (th->notify_cls, 0, NULL);
1646 LOG (GNUNET_ERROR_TYPE_WARNING, "no meta-traffic should be queued\n");
1648 GNUNET_CONTAINER_DLL_remove (h->th_head,
1651 GNUNET_CADET_notify_transmit_ready_cancel (th);
1655 env = GNUNET_MQ_msg (msg,
1656 GNUNET_MESSAGE_TYPE_CADET_LOCAL_CHANNEL_DESTROY);
1657 msg->ccn = channel->ccn;
1658 GNUNET_MQ_send (h->mq,
1661 destroy_channel (channel,
1667 * Get information about a channel.
1669 * @param channel Channel handle.
1670 * @param option Query (GNUNET_CADET_OPTION_*).
1671 * @param ... dependant on option, currently not used
1673 * @return Union with an answer to the query.
1675 const union GNUNET_CADET_ChannelInfo *
1676 GNUNET_CADET_channel_get_info (struct GNUNET_CADET_Channel *channel,
1677 enum GNUNET_CADET_ChannelOption option, ...)
1679 static int bool_flag;
1680 const union GNUNET_CADET_ChannelInfo *ret;
1684 case GNUNET_CADET_OPTION_NOBUFFER:
1685 case GNUNET_CADET_OPTION_RELIABLE:
1686 case GNUNET_CADET_OPTION_OUT_OF_ORDER:
1687 if (0 != (option & channel->options))
1688 bool_flag = GNUNET_YES;
1690 bool_flag = GNUNET_NO;
1691 ret = (const union GNUNET_CADET_ChannelInfo *) &bool_flag;
1693 case GNUNET_CADET_OPTION_PEER:
1694 ret = (const union GNUNET_CADET_ChannelInfo *) GNUNET_PEER_resolve2 (channel->peer);
1705 struct GNUNET_CADET_TransmitHandle *
1706 GNUNET_CADET_notify_transmit_ready (struct GNUNET_CADET_Channel *channel,
1708 struct GNUNET_TIME_Relative maxdelay,
1710 GNUNET_CONNECTION_TransmitReadyNotify notify,
1713 struct GNUNET_CADET_TransmitHandle *th;
1715 GNUNET_assert (NULL != channel);
1716 GNUNET_assert (GNUNET_CONSTANTS_MAX_CADET_MESSAGE_SIZE >= notify_size);
1717 LOG (GNUNET_ERROR_TYPE_DEBUG, "CADET NOTIFY TRANSMIT READY\n");
1718 LOG (GNUNET_ERROR_TYPE_DEBUG, " on channel %X\n", channel->ccn);
1719 LOG (GNUNET_ERROR_TYPE_DEBUG, " allow_send %d\n", channel->allow_send);
1720 if (ntohl (channel->ccn.channel_of_client) >=
1721 GNUNET_CADET_LOCAL_CHANNEL_ID_CLI)
1722 LOG (GNUNET_ERROR_TYPE_DEBUG, " to origin\n");
1724 LOG (GNUNET_ERROR_TYPE_DEBUG, " to destination\n");
1725 LOG (GNUNET_ERROR_TYPE_DEBUG, " payload size %u\n", notify_size);
1726 GNUNET_assert (NULL != notify);
1727 GNUNET_assert (0 == channel->packet_size); // Only one data packet allowed
1729 if (GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us != maxdelay.rel_value_us)
1731 LOG (GNUNET_ERROR_TYPE_WARNING,
1732 "CADET transmit ready timeout is deprected (has no effect)\n");
1735 th = GNUNET_new (struct GNUNET_CADET_TransmitHandle);
1736 th->channel = channel;
1737 th->size = notify_size;
1738 channel->packet_size = th->size;
1739 LOG (GNUNET_ERROR_TYPE_DEBUG, " total size %u\n", th->size);
1740 th->notify = notify;
1741 th->notify_cls = notify_cls;
1742 if (GNUNET_YES == channel->allow_send)
1743 th->request_data_task = GNUNET_SCHEDULER_add_now (&request_data, th);
1745 add_to_queue (channel->cadet, th);
1747 LOG (GNUNET_ERROR_TYPE_DEBUG, "CADET NOTIFY TRANSMIT READY END\n");
1753 GNUNET_CADET_notify_transmit_ready_cancel (struct GNUNET_CADET_TransmitHandle *th)
1755 if (NULL != th->request_data_task)
1757 GNUNET_SCHEDULER_cancel (th->request_data_task);
1759 th->request_data_task = NULL;
1761 remove_from_queue (th);
1767 GNUNET_CADET_receive_done (struct GNUNET_CADET_Channel *channel)
1774 send_info_request (struct GNUNET_CADET_Handle *h, uint16_t type)
1776 struct GNUNET_MessageHeader *msg;
1777 struct GNUNET_MQ_Envelope *env;
1779 LOG (GNUNET_ERROR_TYPE_DEBUG,
1780 " Sending %s monitor message to service\n",
1783 env = GNUNET_MQ_msg (msg, type);
1784 GNUNET_MQ_send (h->mq, env);
1789 * Request a debug dump on the service's STDERR.
1791 * WARNING: unstable API, likely to change in the future!
1793 * @param h cadet handle
1796 GNUNET_CADET_request_dump (struct GNUNET_CADET_Handle *h)
1798 LOG (GNUNET_ERROR_TYPE_DEBUG, "requesting dump\n");
1799 send_info_request (h, GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_DUMP);
1804 * Request information about peers known to the running cadet service.
1805 * The callback will be called for every peer known to the service.
1806 * Only one info request (of any kind) can be active at once.
1809 * WARNING: unstable API, likely to change in the future!
1811 * @param h Handle to the cadet peer.
1812 * @param callback Function to call with the requested data.
1813 * @param callback_cls Closure for @c callback.
1815 * @return #GNUNET_OK / #GNUNET_SYSERR
1818 GNUNET_CADET_get_peers (struct GNUNET_CADET_Handle *h,
1819 GNUNET_CADET_PeersCB callback,
1822 if (NULL != h->info_cb.peers_cb)
1825 return GNUNET_SYSERR;
1827 send_info_request (h, GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEERS);
1828 h->info_cb.peers_cb = callback;
1829 h->info_cls = callback_cls;
1835 * Cancel a peer info request. The callback will not be called (anymore).
1837 * WARNING: unstable API, likely to change in the future!
1839 * @param h Cadet handle.
1841 * @return Closure given to GNUNET_CADET_get_peers.
1844 GNUNET_CADET_get_peers_cancel (struct GNUNET_CADET_Handle *h)
1849 h->info_cb.peers_cb = NULL;
1856 * Request information about a peer known to the running cadet peer.
1857 * The callback will be called for the tunnel once.
1858 * Only one info request (of any kind) can be active at once.
1860 * WARNING: unstable API, likely to change in the future!
1862 * @param h Handle to the cadet peer.
1863 * @param id Peer whose tunnel to examine.
1864 * @param callback Function to call with the requested data.
1865 * @param callback_cls Closure for @c callback.
1867 * @return #GNUNET_OK / #GNUNET_SYSERR
1870 GNUNET_CADET_get_peer (struct GNUNET_CADET_Handle *h,
1871 const struct GNUNET_PeerIdentity *id,
1872 GNUNET_CADET_PeerCB callback,
1875 struct GNUNET_CADET_LocalInfo *msg;
1876 struct GNUNET_MQ_Envelope *env;
1878 if (NULL != h->info_cb.peer_cb)
1881 return GNUNET_SYSERR;
1884 env = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEER);
1886 GNUNET_MQ_send (h->mq, env);
1888 h->info_cb.peer_cb = callback;
1889 h->info_cls = callback_cls;
1895 * Request information about tunnels of the running cadet peer.
1896 * The callback will be called for every tunnel of the service.
1897 * Only one info request (of any kind) can be active at once.
1899 * WARNING: unstable API, likely to change in the future!
1901 * @param h Handle to the cadet peer.
1902 * @param callback Function to call with the requested data.
1903 * @param callback_cls Closure for @c callback.
1905 * @return #GNUNET_OK / #GNUNET_SYSERR
1908 GNUNET_CADET_get_tunnels (struct GNUNET_CADET_Handle *h,
1909 GNUNET_CADET_TunnelsCB callback,
1912 if (NULL != h->info_cb.tunnels_cb)
1915 return GNUNET_SYSERR;
1917 send_info_request (h, GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNELS);
1918 h->info_cb.tunnels_cb = callback;
1919 h->info_cls = callback_cls;
1925 * Cancel a monitor request. The monitor callback will not be called.
1927 * @param h Cadet handle.
1929 * @return Closure given to GNUNET_CADET_get_tunnels.
1932 GNUNET_CADET_get_tunnels_cancel (struct GNUNET_CADET_Handle *h)
1936 h->info_cb.tunnels_cb = NULL;
1946 * Request information about a tunnel of the running cadet peer.
1947 * The callback will be called for the tunnel once.
1948 * Only one info request (of any kind) can be active at once.
1950 * WARNING: unstable API, likely to change in the future!
1952 * @param h Handle to the cadet peer.
1953 * @param id Peer whose tunnel to examine.
1954 * @param callback Function to call with the requested data.
1955 * @param callback_cls Closure for @c callback.
1957 * @return #GNUNET_OK / #GNUNET_SYSERR
1960 GNUNET_CADET_get_tunnel (struct GNUNET_CADET_Handle *h,
1961 const struct GNUNET_PeerIdentity *id,
1962 GNUNET_CADET_TunnelCB callback,
1965 struct GNUNET_CADET_LocalInfo *msg;
1966 struct GNUNET_MQ_Envelope *env;
1968 if (NULL != h->info_cb.tunnel_cb)
1971 return GNUNET_SYSERR;
1974 env = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNEL);
1976 GNUNET_MQ_send (h->mq, env);
1978 h->info_cb.tunnel_cb = callback;
1979 h->info_cls = callback_cls;
1985 * Request information about a specific channel of the running cadet peer.
1987 * WARNING: unstable API, likely to change in the future!
1988 * FIXME Add destination option.
1990 * @param h Handle to the cadet peer.
1991 * @param initiator ID of the owner of the channel.
1992 * @param channel_number Channel number.
1993 * @param callback Function to call with the requested data.
1994 * @param callback_cls Closure for @c callback.
1996 * @return #GNUNET_OK / #GNUNET_SYSERR
1999 GNUNET_CADET_show_channel (struct GNUNET_CADET_Handle *h,
2000 struct GNUNET_PeerIdentity *initiator,
2001 unsigned int channel_number,
2002 GNUNET_CADET_ChannelCB callback,
2005 struct GNUNET_CADET_LocalInfo *msg;
2006 struct GNUNET_MQ_Envelope *env;
2008 if (NULL != h->info_cb.channel_cb)
2011 return GNUNET_SYSERR;
2014 env = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_CHANNEL);
2015 msg->peer = *initiator;
2016 msg->ccn.channel_of_client = htonl (channel_number);
2017 GNUNET_MQ_send (h->mq, env);
2019 h->info_cb.channel_cb = callback;
2020 h->info_cls = callback_cls;
2026 * Function called to notify a client about the connection
2027 * begin ready to queue more data. "buf" will be
2028 * NULL and "size" zero if the connection was closed for
2029 * writing in the meantime.
2031 * @param cls closure
2032 * @param size number of bytes available in buf
2033 * @param buf where the callee should write the message
2034 * @return number of bytes written to buf
2037 cadet_mq_ntr (void *cls, size_t size,
2040 struct GNUNET_MQ_Handle *mq = cls;
2041 struct CadetMQState *state = GNUNET_MQ_impl_state (mq);
2042 const struct GNUNET_MessageHeader *msg = GNUNET_MQ_impl_current (mq);
2048 GNUNET_MQ_inject_error (mq, GNUNET_MQ_ERROR_WRITE);
2051 msize = ntohs (msg->size);
2052 GNUNET_assert (msize <= size);
2053 GNUNET_memcpy (buf, msg, msize);
2054 GNUNET_MQ_impl_send_continue (mq);
2060 * Signature of functions implementing the
2061 * sending functionality of a message queue.
2063 * @param mq the message queue
2064 * @param msg the message to send
2065 * @param impl_state state of the implementation
2068 cadet_mq_send_impl (struct GNUNET_MQ_Handle *mq,
2069 const struct GNUNET_MessageHeader *msg,
2072 struct CadetMQState *state = impl_state;
2074 GNUNET_assert (NULL == state->th);
2076 GNUNET_CADET_notify_transmit_ready (state->channel,
2077 /* FIXME: add option for corking */
2079 GNUNET_TIME_UNIT_FOREVER_REL,
2087 * Signature of functions implementing the
2088 * destruction of a message queue.
2089 * Implementations must not free 'mq', but should
2090 * take care of 'impl_state'.
2092 * @param mq the message queue to destroy
2093 * @param impl_state state of the implementation
2096 cadet_mq_destroy_impl (struct GNUNET_MQ_Handle *mq,
2099 struct CadetMQState *state = impl_state;
2101 if (NULL != state->th)
2102 GNUNET_CADET_notify_transmit_ready_cancel (state->th);
2104 GNUNET_free (state);
2109 * Create a message queue for a cadet channel.
2110 * The message queue can only be used to transmit messages,
2111 * not to receive them.
2113 * @param channel the channel to create the message qeue for
2114 * @return a message queue to messages over the channel
2116 struct GNUNET_MQ_Handle *
2117 GNUNET_CADET_mq_create (struct GNUNET_CADET_Channel *channel)
2119 struct GNUNET_MQ_Handle *mq;
2120 struct CadetMQState *state;
2122 state = GNUNET_new (struct CadetMQState);
2123 state->channel = channel;
2125 mq = GNUNET_MQ_queue_for_callbacks (&cadet_mq_send_impl,
2126 &cadet_mq_destroy_impl,
2127 NULL, /* FIXME: cancel impl. */
2129 NULL, /* no msg handlers */
2130 NULL, /* no err handlers */
2131 NULL); /* no handler cls */
2137 * Transitional function to convert an unsigned int port to a hash value.
2138 * WARNING: local static value returned, NOT reentrant!
2139 * WARNING: do not use this function for new code!
2141 * @param port Numerical port (unsigned int format).
2143 * @return A GNUNET_HashCode usable for the new CADET API.
2145 const struct GNUNET_HashCode *
2146 GC_u2h (uint32_t port)
2148 static struct GNUNET_HashCode hash;
2150 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2151 "This is a transitional function, "
2152 "use proper crypto hashes as CADET ports\n");
2153 GNUNET_CRYPTO_hash (&port, sizeof (port), &hash);