2 This file is part of GNUnet.
3 Copyright (C) 2011, 2017 GNUnet e.V.
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
21 * @file cadet/cadet_api.c
22 * @brief cadet api: client implementation of cadet service
23 * @author Bartlomiej Polot
27 #include "gnunet_util_lib.h"
28 #include "gnunet_constants.h"
29 #include "gnunet_cadet_service.h"
31 #include "cadet_protocol.h"
33 #define LOG(kind,...) GNUNET_log_from (kind, "cadet-api",__VA_ARGS__)
35 /******************************************************************************/
36 /************************ DATA STRUCTURES ****************************/
37 /******************************************************************************/
40 * Transmission queue to the service
44 struct GNUNET_CADET_TransmitHandle
49 struct GNUNET_CADET_TransmitHandle *next;
54 struct GNUNET_CADET_TransmitHandle *prev;
57 * Channel this message is sent on / for (may be NULL for control messages).
59 struct GNUNET_CADET_Channel *channel;
64 struct GNUNET_SCHEDULER_Task *request_data_task;
67 * Callback to obtain the message to transmit, or NULL if we
68 * got the message in 'data'. Notice that messages built
69 * by 'notify' need to be encapsulated with information about
72 GNUNET_CONNECTION_TransmitReadyNotify notify;
75 * Closure for 'notify'
80 * Size of the payload.
92 GNUNET_CADET_ChannelCB channel_cb;
97 GNUNET_CADET_PeersCB peers_cb;
102 GNUNET_CADET_PeerCB peer_cb;
107 GNUNET_CADET_TunnelsCB tunnels_cb;
112 GNUNET_CADET_TunnelCB tunnel_cb;
117 * Opaque handle to the service.
119 struct GNUNET_CADET_Handle
122 * Flag to indicate old or MQ API.
127 * Message queue (if available).
129 struct GNUNET_MQ_Handle *mq;
132 * Set of handlers used for processing incoming messages in the channels
136 const struct GNUNET_CADET_MessageHandler *message_handlers;
139 * Number of handlers in the handlers array.
143 unsigned int n_handlers;
148 struct GNUNET_CONTAINER_MultiHashMap *ports;
151 * Double linked list of the channels this client is connected to, head.
153 struct GNUNET_CADET_Channel *channels_head;
156 * Double linked list of the channels this client is connected to, tail.
158 struct GNUNET_CADET_Channel *channels_tail;
161 * Callback for inbound channel disconnection
163 GNUNET_CADET_ChannelEndHandler *cleaner;
166 * Closure for all the handlers given by the client
173 * Messages to send to the service, head.
177 struct GNUNET_CADET_TransmitHandle *th_head;
180 * Messages to send to the service, tail.
184 struct GNUNET_CADET_TransmitHandle *th_tail;
187 * child of the next channel to create (to avoid reusing IDs often)
189 struct GNUNET_CADET_ClientChannelNumber next_ccn;
192 * Configuration given by the client, in case of reconnection
194 const struct GNUNET_CONFIGURATION_Handle *cfg;
197 * Time to the next reconnect in case one reconnect fails
199 struct GNUNET_TIME_Relative reconnect_time;
202 * Task for trying to reconnect.
204 struct GNUNET_SCHEDULER_Task * reconnect_task;
207 * Callback for an info task (only one active at a time).
209 union CadetInfoCB info_cb;
212 * Info callback closure for @c info_cb.
219 * Description of a peer
221 struct GNUNET_CADET_Peer
224 * ID of the peer in short form
229 * Channel this peer belongs to
231 struct GNUNET_CADET_Channel *t;
236 * Opaque handle to a channel.
238 struct GNUNET_CADET_Channel
243 struct GNUNET_CADET_Channel *next;
248 struct GNUNET_CADET_Channel *prev;
251 * Handle to the cadet this channel belongs to
253 struct GNUNET_CADET_Handle *cadet;
256 * Local ID of the channel
258 struct GNUNET_CADET_ClientChannelNumber ccn;
261 * Channel's port, if incoming.
263 struct GNUNET_CADET_Port *incoming_port;
266 * Other end of the channel.
271 * Any data the caller wants to put in here
276 * Channel options: reliability, etc.
278 enum GNUNET_CADET_ChannelOption options;
281 * Are we allowed to send to the service?
285 unsigned int allow_send;
287 /***************************** MQ ************************************/
289 * Message Queue for the channel.
291 struct GNUNET_MQ_Handle *mq;
294 * Window change handler.
296 GNUNET_CADET_WindowSizeEventHandler window_changes;
299 * Disconnect handler.
301 GNUNET_CADET_DisconnectEventHandler disconnects;
307 * Opaque handle to a port.
309 struct GNUNET_CADET_Port
312 * Handle to the CADET session this port belongs to.
314 struct GNUNET_CADET_Handle *cadet;
319 struct GNUNET_HashCode *hash;
322 * Callback handler for incoming channels on this port.
324 GNUNET_CADET_InboundChannelNotificationHandler *handler;
327 * Closure for @a handler.
331 /***************************** MQ ************************************/
336 struct GNUNET_HashCode id;
339 * Handler for incoming channels on this port
341 GNUNET_CADET_ConnectEventHandler connects;
344 * Closure for @ref connects
349 * Window size change handler.
351 GNUNET_CADET_WindowSizeEventHandler window_changes;
354 * Handler called when an incoming channel is destroyed..
356 GNUNET_CADET_DisconnectEventHandler disconnects;
359 * Payload handlers for incoming channels.
361 const struct GNUNET_MQ_MessageHandler *handlers;
366 * Implementation state for cadet's message queue.
371 * The current transmit handle, or NULL
372 * if no transmit is active.
374 struct GNUNET_CADET_TransmitHandle *th;
377 * Channel to send the data over.
379 struct GNUNET_CADET_Channel *channel;
384 /******************************************************************************/
385 /********************* FUNCTION DECLARATIONS *************************/
386 /******************************************************************************/
389 * Reconnect to the service, retransmit all infomation to try to restore the
392 * @param h Handle to the CADET service.
395 schedule_reconnect (struct GNUNET_CADET_Handle *h);
399 * Reconnect callback: tries to reconnect again after a failer previous
402 * @param cls Closure (cadet handle).
405 reconnect_cbk (void *cls);
409 * Reconnect to the service, retransmit all infomation to try to restore the
412 * @param h handle to the cadet
415 reconnect (struct GNUNET_CADET_Handle *h);
418 /******************************************************************************/
419 /*********************** AUXILIARY FUNCTIONS *************************/
420 /******************************************************************************/
423 * Check if transmission is a payload packet.
425 * @param th Transmission handle.
427 * @return #GNUNET_YES if it is a payload packet,
428 * #GNUNET_NO if it is a cadet management packet.
431 th_is_payload (struct GNUNET_CADET_TransmitHandle *th)
433 return (th->notify != NULL) ? GNUNET_YES : GNUNET_NO;
438 * Find the Port struct for a hash.
440 * @param h CADET handle.
441 * @param hash HashCode for the port number.
443 * @return The port handle if known, NULL otherwise.
445 static struct GNUNET_CADET_Port *
446 find_port (const struct GNUNET_CADET_Handle *h,
447 const struct GNUNET_HashCode *hash)
449 struct GNUNET_CADET_Port *p;
451 p = GNUNET_CONTAINER_multihashmap_get (h->ports, hash);
458 * Get the channel handler for the channel specified by id from the given handle
460 * @param h Cadet handle
461 * @param ccn ID of the wanted channel
462 * @return handle to the required channel or NULL if not found
464 static struct GNUNET_CADET_Channel *
465 retrieve_channel (struct GNUNET_CADET_Handle *h,
466 struct GNUNET_CADET_ClientChannelNumber ccn)
468 struct GNUNET_CADET_Channel *ch;
470 for (ch = h->channels_head; NULL != ch; ch = ch->next)
471 if (ch->ccn.channel_of_client == ccn.channel_of_client)
478 * Create a new channel and insert it in the channel list of the cadet handle
480 * @param h Cadet handle
481 * @param ccn Desired ccn of the channel, 0 to assign one automatically.
483 * @return Handle to the created channel.
485 static struct GNUNET_CADET_Channel *
486 create_channel (struct GNUNET_CADET_Handle *h,
487 struct GNUNET_CADET_ClientChannelNumber ccn)
489 struct GNUNET_CADET_Channel *ch;
491 ch = GNUNET_new (struct GNUNET_CADET_Channel);
492 GNUNET_CONTAINER_DLL_insert (h->channels_head,
496 if (0 == ccn.channel_of_client)
498 ch->ccn = h->next_ccn;
499 while (NULL != retrieve_channel (h,
502 h->next_ccn.channel_of_client
503 = htonl (1 + ntohl (h->next_ccn.channel_of_client));
504 if (0 == ntohl (h->next_ccn.channel_of_client))
505 h->next_ccn.channel_of_client
506 = htonl (GNUNET_CADET_LOCAL_CHANNEL_ID_CLI);
518 * Destroy the specified channel.
519 * - Destroys all peers, calling the disconnect callback on each if needed
520 * - Cancels all outgoing traffic for that channel, calling respective notifys
521 * - Calls cleaner if channel was inbound
522 * - Frees all memory used
524 * @param ch Pointer to the channel.
525 * @param call_cleaner Whether to call the cleaner handler.
527 * @return Handle to the required channel or NULL if not found.
530 destroy_channel (struct GNUNET_CADET_Channel *ch)
532 struct GNUNET_CADET_Handle *h;
533 struct GNUNET_CADET_TransmitHandle *th;
534 struct GNUNET_CADET_TransmitHandle *next;
542 LOG (GNUNET_ERROR_TYPE_DEBUG,
543 " destroy_channel %X of %p\n",
547 GNUNET_CONTAINER_DLL_remove (h->channels_head,
551 /* signal channel destruction */
554 if (NULL != h->cleaner)
557 LOG (GNUNET_ERROR_TYPE_DEBUG,
558 " calling cleaner\n");
559 h->cleaner (h->cls, ch, ch->ctx);
561 else if (NULL != ch->disconnects)
563 LOG (GNUNET_ERROR_TYPE_DEBUG,
564 " calling disconnect handler\n");
565 ch->disconnects (ch->ctx, ch);
569 /* Application won't be aware of the channel destruction and use
570 * a pointer to free'd memory.
576 /* check that clients did not leave messages behind in the queue */
577 for (th = h->th_head; NULL != th; th = next)
580 if (th->channel != ch)
582 /* Clients should have aborted their requests already.
583 * Management traffic should be ok, as clients can't cancel that.
584 * If the service crashed and we are reconnecting, it's ok.
586 GNUNET_break (GNUNET_NO == th_is_payload (th));
587 GNUNET_CADET_notify_transmit_ready_cancel (th);
591 GNUNET_PEER_change_rc (ch->peer, -1);
597 * Add a transmit handle to the transmission queue and set the
600 * @param h cadet handle with the queue head and tail
601 * @param th handle to the packet to be transmitted
604 add_to_queue (struct GNUNET_CADET_Handle *h,
605 struct GNUNET_CADET_TransmitHandle *th)
607 GNUNET_CONTAINER_DLL_insert_tail (h->th_head,
614 * Remove a transmit handle from the transmission queue, if present.
616 * Safe to call even if not queued.
618 * @param th handle to the packet to be unqueued.
621 remove_from_queue (struct GNUNET_CADET_TransmitHandle *th)
623 struct GNUNET_CADET_Handle *h = th->channel->cadet;
625 /* It might or might not have been queued (rarely not), but check anyway. */
626 if (NULL != th->next || h->th_tail == th)
628 GNUNET_CONTAINER_DLL_remove (h->th_head, h->th_tail, th);
633 /******************************************************************************/
634 /*********************** MQ API CALLBACKS ****************************/
635 /******************************************************************************/
639 * Implement sending functionality of a message queue for
640 * us sending messages to a peer.
642 * Encapsulates the payload message in a #GNUNET_CADET_LocalData message
643 * in order to label the message with the channel ID and send the
644 * encapsulated message to the service.
646 * @param mq the message queue
647 * @param msg the message to send
648 * @param impl_state state of the implementation
651 cadet_mq_send_impl (struct GNUNET_MQ_Handle *mq,
652 const struct GNUNET_MessageHeader *msg,
655 struct GNUNET_CADET_Channel *ch = impl_state;
656 struct GNUNET_CADET_Handle *h = ch->cadet;
658 struct GNUNET_MQ_Envelope *env;
659 struct GNUNET_CADET_LocalData *cadet_msg;
664 /* We're currently reconnecting, pretend this worked */
665 GNUNET_MQ_impl_send_continue (mq);
669 /* check message size for sanity */
670 msize = ntohs (msg->size);
671 if (msize > GNUNET_CONSTANTS_MAX_CADET_MESSAGE_SIZE)
674 GNUNET_MQ_impl_send_continue (mq);
678 env = GNUNET_MQ_msg_nested_mh (cadet_msg,
679 GNUNET_MESSAGE_TYPE_CADET_LOCAL_DATA,
681 cadet_msg->ccn = ch->ccn;
682 GNUNET_MQ_send (h->mq, env);
683 GNUNET_MQ_impl_send_continue (mq);
688 * Handle destruction of a message queue. Implementations must not
689 * free @a mq, but should take care of @a impl_state.
691 * @param mq the message queue to destroy
692 * @param impl_state state of the implementation
695 cadet_mq_destroy_impl (struct GNUNET_MQ_Handle *mq,
698 struct GNUNET_CADET_Channel *ch = impl_state;
700 GNUNET_assert (mq == ch->mq);
706 * We had an error processing a message we forwarded from a peer to
707 * the CADET service. We should just complain about it but otherwise
708 * continue processing.
711 * @param error error code
714 cadet_mq_error_handler (void *cls,
715 enum GNUNET_MQ_Error error)
722 * Implementation function that cancels the currently sent message.
723 * Should basically undo whatever #mq_send_impl() did.
725 * @param mq message queue
726 * @param impl_state state specific to the implementation
729 cadet_mq_cancel_impl (struct GNUNET_MQ_Handle *mq,
732 struct GNUNET_CADET_Channel *ch = impl_state;
734 LOG (GNUNET_ERROR_TYPE_WARNING,
735 "Cannot cancel mq message on channel %X of %p\n",
736 ch->ccn.channel_of_client, ch->cadet);
742 /******************************************************************************/
743 /*********************** RECEIVE HANDLERS ****************************/
744 /******************************************************************************/
748 * Call the @a notify callback given to #GNUNET_CADET_notify_transmit_ready to
749 * request the data to send over MQ. Since MQ manages the queue, this function
750 * is scheduled immediatly after a transmit ready notification.
752 * @param cls Closure (transmit handle).
755 request_data (void *cls)
757 struct GNUNET_CADET_TransmitHandle *th = cls;
758 struct GNUNET_CADET_LocalData *msg;
759 struct GNUNET_MQ_Envelope *env;
762 LOG (GNUNET_ERROR_TYPE_DEBUG,
763 "Requesting Data: %u bytes (allow send is %u)\n",
765 th->channel->allow_send);
767 GNUNET_assert (0 < th->channel->allow_send);
768 th->channel->allow_send--;
769 /* NOTE: we may be allowed to send another packet immediately,
770 albeit the current logic waits for the ACK. */
771 th->request_data_task = NULL;
772 remove_from_queue (th);
774 env = GNUNET_MQ_msg_extra (msg,
776 GNUNET_MESSAGE_TYPE_CADET_LOCAL_DATA);
777 msg->ccn = th->channel->ccn;
778 osize = th->notify (th->notify_cls,
781 GNUNET_assert (osize == th->size);
783 GNUNET_MQ_send (th->channel->cadet->mq,
790 * Process the new channel notification and add it to the channels in the handle
792 * @param h The cadet handle
793 * @param msg A message with the details of the new incoming channel
796 handle_channel_created (void *cls,
797 const struct GNUNET_CADET_LocalChannelCreateMessage *msg)
799 struct GNUNET_CADET_Handle *h = cls;
800 struct GNUNET_CADET_Channel *ch;
801 struct GNUNET_CADET_Port *port;
802 const struct GNUNET_HashCode *port_number;
803 struct GNUNET_CADET_ClientChannelNumber ccn;
806 port_number = &msg->port;
807 if (ntohl (ccn.channel_of_client) >= GNUNET_CADET_LOCAL_CHANNEL_ID_CLI)
812 port = find_port (h, port_number);
815 /* We could have closed the port but the service didn't know about it yet
816 * This is not an error.
818 struct GNUNET_CADET_LocalChannelDestroyMessage *d_msg;
819 struct GNUNET_MQ_Envelope *env;
822 LOG (GNUNET_ERROR_TYPE_DEBUG,
823 "No handler for incoming channel %X (on port %s, recently closed?)\n",
824 ntohl (ccn.channel_of_client),
825 GNUNET_h2s (port_number));
826 env = GNUNET_MQ_msg (d_msg,
827 GNUNET_MESSAGE_TYPE_CADET_LOCAL_CHANNEL_DESTROY);
828 d_msg->ccn = msg->ccn;
829 GNUNET_MQ_send (h->mq,
834 ch = create_channel (h,
836 ch->peer = GNUNET_PEER_intern (&msg->peer);
839 ch->incoming_port = port;
840 ch->options = ntohl (msg->opt);
841 LOG (GNUNET_ERROR_TYPE_DEBUG,
842 "Creating incoming channel %X [%s] %p\n",
843 ntohl (ccn.channel_of_client),
844 GNUNET_h2s (port_number),
847 if (NULL != port->handler)
851 ch->ctx = port->handler (port->cls,
858 GNUNET_assert (NULL != port->connects);
859 ch->window_changes = port->window_changes;
860 ch->disconnects = port->disconnects;
861 ch->mq = GNUNET_MQ_queue_for_callbacks (&cadet_mq_send_impl,
862 &cadet_mq_destroy_impl,
863 &cadet_mq_cancel_impl,
866 &cadet_mq_error_handler,
868 ch->ctx = port->connects (port->cadet->cls,
871 GNUNET_MQ_set_handlers_closure (ch->mq, ch->ctx);
877 * Process the channel destroy notification and free associated resources
879 * @param h The cadet handle
880 * @param msg A message with the details of the channel being destroyed
883 handle_channel_destroy (void *cls,
884 const struct GNUNET_CADET_LocalChannelDestroyMessage *msg)
886 struct GNUNET_CADET_Handle *h = cls;
887 struct GNUNET_CADET_Channel *ch;
888 struct GNUNET_CADET_ClientChannelNumber ccn;
891 LOG (GNUNET_ERROR_TYPE_DEBUG,
892 "Channel %X Destroy from service\n",
893 ntohl (ccn.channel_of_client));
894 ch = retrieve_channel (h,
899 LOG (GNUNET_ERROR_TYPE_DEBUG,
900 "channel %X unknown\n",
901 ntohl (ccn.channel_of_client));
904 destroy_channel (ch);
909 * Check that message received from CADET service is well-formed.
911 * @param cls the `struct GNUNET_CADET_Handle`
912 * @param message the message we got
913 * @return #GNUNET_OK if the message is well-formed,
914 * #GNUNET_SYSERR otherwise
917 check_local_data (void *cls,
918 const struct GNUNET_CADET_LocalData *message)
920 struct GNUNET_CADET_Handle *h = cls;
921 struct GNUNET_CADET_Channel *ch;
924 size = ntohs (message->header.size);
925 if (sizeof (*message) + sizeof (struct GNUNET_MessageHeader) > size)
928 return GNUNET_SYSERR;
931 ch = retrieve_channel (h,
936 return GNUNET_SYSERR;
944 * Process the incoming data packets, call appropriate handlers.
946 * @param h The cadet handle
947 * @param message A message encapsulating the data
950 handle_local_data (void *cls,
951 const struct GNUNET_CADET_LocalData *message)
953 struct GNUNET_CADET_Handle *h = cls;
954 const struct GNUNET_MessageHeader *payload;
955 const struct GNUNET_CADET_MessageHandler *handler;
956 struct GNUNET_CADET_Channel *ch;
959 ch = retrieve_channel (h,
968 payload = (struct GNUNET_MessageHeader *) &message[1];
969 type = ntohs (payload->type);
970 LOG (GNUNET_ERROR_TYPE_DEBUG,
971 "Got a %s data on channel %s [%X] of type %s (%u)\n",
972 GC_f2s (ntohl (ch->ccn.channel_of_client) >=
973 GNUNET_CADET_LOCAL_CHANNEL_ID_CLI),
974 GNUNET_i2s (GNUNET_PEER_resolve2 (ch->peer)),
975 ntohl (message->ccn.channel_of_client),
978 for (unsigned i=0;i<h->n_handlers;i++)
980 handler = &h->message_handlers[i];
981 if (handler->type == type)
984 handler->callback (h->cls,
989 LOG (GNUNET_ERROR_TYPE_DEBUG,
990 "callback caused disconnection\n");
991 GNUNET_CADET_channel_destroy (ch);
997 /* Other peer sent message we do not comprehend. */
999 GNUNET_CADET_receive_done (ch);
1004 * Process a local ACK message, enabling the client to send
1005 * more data to the service.
1007 * @param h Cadet handle.
1008 * @param message Message itself.
1011 handle_local_ack (void *cls,
1012 const struct GNUNET_CADET_LocalAck *message)
1014 struct GNUNET_CADET_Handle *h = cls;
1015 struct GNUNET_CADET_Channel *ch;
1016 struct GNUNET_CADET_ClientChannelNumber ccn;
1017 struct GNUNET_CADET_TransmitHandle *th;
1020 ch = retrieve_channel (h, ccn);
1023 LOG (GNUNET_ERROR_TYPE_DEBUG,
1024 "ACK on unknown channel %X\n",
1025 ntohl (ccn.channel_of_client));
1029 LOG (GNUNET_ERROR_TYPE_DEBUG,
1030 "Got an ACK on channel %X, allow send now %u!\n",
1031 ntohl (ch->ccn.channel_of_client),
1033 for (th = h->th_head; NULL != th; th = th->next)
1035 if ( (th->channel == ch) &&
1036 (NULL == th->request_data_task) )
1038 th->request_data_task
1039 = GNUNET_SCHEDULER_add_now (&request_data,
1048 * Generic error handler, called with the appropriate error code and
1049 * the same closure specified at the creation of the message queue.
1050 * Not every message queue implementation supports an error handler.
1052 * @param cls closure, a `struct GNUNET_CORE_Handle *`
1053 * @param error error code
1056 handle_mq_error (void *cls,
1057 enum GNUNET_MQ_Error error)
1059 struct GNUNET_CADET_Handle *h = cls;
1061 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MQ ERROR: %u\n", error);
1062 GNUNET_MQ_destroy (h->mq);
1069 * Process a local reply about info on all channels, pass info to the user.
1071 * @param h Cadet handle.
1072 * @param message Message itself.
1075 // process_get_channels (struct GNUNET_CADET_Handle *h,
1076 // const struct GNUNET_MessageHeader *message)
1078 // struct GNUNET_CADET_LocalInfo *msg;
1080 // GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Get Channels messasge received\n");
1082 // if (NULL == h->channels_cb)
1084 // GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " ignored\n");
1088 // msg = (struct GNUNET_CADET_LocalInfo *) message;
1089 // if (ntohs (message->size) !=
1090 // (sizeof (struct GNUNET_CADET_LocalInfo) +
1091 // sizeof (struct GNUNET_PeerIdentity)))
1093 // GNUNET_break_op (0);
1094 // GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1095 // "Get channels message: size %hu - expected %u\n",
1096 // ntohs (message->size),
1097 // sizeof (struct GNUNET_CADET_LocalInfo));
1100 // h->channels_cb (h->channels_cls,
1101 // ntohl (msg->channel_id),
1103 // &msg->destination);
1109 * Process a local monitor_channel reply, pass info to the user.
1111 * @param h Cadet handle.
1112 * @param message Message itself.
1115 // process_show_channel (struct GNUNET_CADET_Handle *h,
1116 // const struct GNUNET_MessageHeader *message)
1118 // struct GNUNET_CADET_LocalInfo *msg;
1121 // GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Show Channel messasge received\n");
1123 // if (NULL == h->channel_cb)
1125 // GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " ignored\n");
1129 // /* Verify message sanity */
1130 // msg = (struct GNUNET_CADET_LocalInfo *) message;
1131 // esize = sizeof (struct GNUNET_CADET_LocalInfo);
1132 // if (ntohs (message->size) != esize)
1134 // GNUNET_break_op (0);
1135 // GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1136 // "Show channel message: size %hu - expected %u\n",
1137 // ntohs (message->size),
1140 // h->channel_cb (h->channel_cls, NULL, NULL);
1141 // h->channel_cb = NULL;
1142 // h->channel_cls = NULL;
1147 // h->channel_cb (h->channel_cls,
1148 // &msg->destination,
1155 * Check that message received from CADET service is well-formed.
1157 * @param cls the `struct GNUNET_CADET_Handle`
1158 * @param message the message we got
1159 * @return #GNUNET_OK if the message is well-formed,
1160 * #GNUNET_SYSERR otherwise
1163 check_get_peers (void *cls,
1164 const struct GNUNET_CADET_LocalInfoPeer *message)
1166 struct GNUNET_CADET_Handle *h = cls;
1169 if (NULL == h->info_cb.peers_cb)
1171 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1172 " no handler for peesr monitor message!\n");
1173 return GNUNET_SYSERR;
1176 size = ntohs (message->header.size);
1177 if (sizeof (struct GNUNET_CADET_LocalInfoPeer) > size)
1179 h->info_cb.peers_cb (h->info_cls, NULL, -1, 0, 0);
1180 h->info_cb.peers_cb = NULL;
1182 return GNUNET_SYSERR;
1190 * Process a local reply about info on all tunnels, pass info to the user.
1192 * @param cls Closure (Cadet handle).
1193 * @param msg Message itself.
1196 handle_get_peers (void *cls,
1197 const struct GNUNET_CADET_LocalInfoPeer *msg)
1199 struct GNUNET_CADET_Handle *h = cls;
1200 h->info_cb.peers_cb (h->info_cls, &msg->destination,
1201 (int) ntohs (msg->tunnel),
1202 (unsigned int ) ntohs (msg->paths),
1208 * Check that message received from CADET service is well-formed.
1210 * @param cls the `struct GNUNET_CADET_Handle`
1211 * @param message the message we got
1212 * @return #GNUNET_OK if the message is well-formed,
1213 * #GNUNET_SYSERR otherwise
1216 check_get_peer (void *cls,
1217 const struct GNUNET_CADET_LocalInfoPeer *message)
1219 struct GNUNET_CADET_Handle *h = cls;
1220 const size_t msize = sizeof (struct GNUNET_CADET_LocalInfoPeer);
1221 struct GNUNET_PeerIdentity *paths_array;
1223 unsigned int epaths;
1227 if (NULL == h->info_cb.peer_cb)
1229 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1230 " no handler for peer monitor message!\n");
1234 /* Verify message sanity */
1235 esize = ntohs (message->header.size);
1238 GNUNET_break_op (0);
1239 h->info_cb.peer_cb (h->info_cls, NULL, 0, 0, 0, NULL);
1242 if (0 != ((esize - msize) % sizeof (struct GNUNET_PeerIdentity)))
1244 GNUNET_break_op (0);
1245 h->info_cb.peer_cb (h->info_cls, NULL, 0, 0, 0, NULL);
1249 peers = (esize - msize) / sizeof (struct GNUNET_PeerIdentity);
1250 epaths = (unsigned int) ntohs (message->paths);
1251 paths_array = (struct GNUNET_PeerIdentity *) &message[1];
1253 for (int i = 0; i < peers; i++)
1255 if (0 == memcmp (&paths_array[i], &message->destination,
1256 sizeof (struct GNUNET_PeerIdentity)))
1261 if (paths != epaths)
1263 GNUNET_break_op (0);
1264 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "p:%u, e: %u\n", paths, epaths);
1265 h->info_cb.peer_cb (h->info_cls, NULL, 0, 0, 0, NULL);
1272 h->info_cb.peer_cb = NULL;
1274 return GNUNET_SYSERR;
1279 * Process a local peer info reply, pass info to the user.
1281 * @param cls Closure (Cadet handle).
1282 * @param message Message itself.
1285 handle_get_peer (void *cls,
1286 const struct GNUNET_CADET_LocalInfoPeer *message)
1288 struct GNUNET_CADET_Handle *h = cls;
1289 struct GNUNET_PeerIdentity *paths_array;
1291 unsigned int path_length;
1295 paths = (unsigned int) ntohs (message->paths);
1296 paths_array = (struct GNUNET_PeerIdentity *) &message[1];
1297 peers = (ntohs (message->header.size) - sizeof (*message))
1298 / sizeof (struct GNUNET_PeerIdentity);
1300 neighbor = GNUNET_NO;
1302 for (int i = 0; i < peers; i++)
1304 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " %s\n", GNUNET_i2s (&paths_array[i]));
1306 if (0 == memcmp (&paths_array[i], &message->destination,
1307 sizeof (struct GNUNET_PeerIdentity)))
1309 if (1 == path_length)
1310 neighbor = GNUNET_YES;
1315 /* Call Callback with tunnel info. */
1316 paths_array = (struct GNUNET_PeerIdentity *) &message[1];
1317 h->info_cb.peer_cb (h->info_cls,
1318 &message->destination,
1319 (int) ntohs (message->tunnel),
1327 * Check that message received from CADET service is well-formed.
1329 * @param cls the `struct GNUNET_CADET_Handle`
1330 * @param msg the message we got
1331 * @return #GNUNET_OK if the message is well-formed,
1332 * #GNUNET_SYSERR otherwise
1335 check_get_tunnels (void *cls,
1336 const struct GNUNET_CADET_LocalInfoTunnel *msg)
1338 struct GNUNET_CADET_Handle *h = cls;
1341 if (NULL == h->info_cb.tunnels_cb)
1343 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1344 " no handler for tunnels monitor message!\n");
1345 return GNUNET_SYSERR;
1348 size = ntohs (msg->header.size);
1349 if (sizeof (struct GNUNET_CADET_LocalInfoTunnel) > size)
1351 h->info_cb.tunnels_cb (h->info_cls, NULL, 0, 0, 0, 0);
1352 h->info_cb.tunnels_cb = NULL;
1354 return GNUNET_SYSERR;
1361 * Process a local reply about info on all tunnels, pass info to the user.
1363 * @param cls Closure (Cadet handle).
1364 * @param message Message itself.
1367 handle_get_tunnels (void *cls,
1368 const struct GNUNET_CADET_LocalInfoTunnel *msg)
1370 struct GNUNET_CADET_Handle *h = cls;
1372 h->info_cb.tunnels_cb (h->info_cls,
1374 ntohl (msg->channels),
1375 ntohl (msg->connections),
1376 ntohs (msg->estate),
1377 ntohs (msg->cstate));
1383 * Check that message received from CADET service is well-formed.
1385 * @param cls the `struct GNUNET_CADET_Handle`
1386 * @param msg the message we got
1387 * @return #GNUNET_OK if the message is well-formed,
1388 * #GNUNET_SYSERR otherwise
1391 check_get_tunnel (void *cls,
1392 const struct GNUNET_CADET_LocalInfoTunnel *msg)
1394 struct GNUNET_CADET_Handle *h = cls;
1400 if (NULL == h->info_cb.tunnel_cb)
1402 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1403 " no handler for tunnel monitor message!\n");
1407 /* Verify message sanity */
1408 msize = ntohs (msg->header.size);
1409 esize = sizeof (struct GNUNET_CADET_LocalInfoTunnel);
1412 GNUNET_break_op (0);
1413 h->info_cb.tunnel_cb (h->info_cls,
1414 NULL, 0, 0, NULL, NULL, 0, 0);
1417 ch_n = ntohl (msg->channels);
1418 c_n = ntohl (msg->connections);
1419 esize += ch_n * sizeof (struct GNUNET_CADET_ChannelTunnelNumber);
1420 esize += c_n * sizeof (struct GNUNET_CADET_ConnectionTunnelIdentifier);
1423 GNUNET_break_op (0);
1424 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1425 "m:%u, e: %u (%u ch, %u conn)\n",
1426 (unsigned int) msize,
1427 (unsigned int) esize,
1430 h->info_cb.tunnel_cb (h->info_cls,
1431 NULL, 0, 0, NULL, NULL, 0, 0);
1438 h->info_cb.tunnel_cb = NULL;
1440 return GNUNET_SYSERR;
1445 * Process a local tunnel info reply, pass info to the user.
1447 * @param cls Closure (Cadet handle).
1448 * @param msg Message itself.
1451 handle_get_tunnel (void *cls,
1452 const struct GNUNET_CADET_LocalInfoTunnel *msg)
1454 struct GNUNET_CADET_Handle *h = cls;
1457 const struct GNUNET_CADET_ConnectionTunnelIdentifier *conns;
1458 const struct GNUNET_CADET_ChannelTunnelNumber *chns;
1460 ch_n = ntohl (msg->channels);
1461 c_n = ntohl (msg->connections);
1463 /* Call Callback with tunnel info. */
1464 conns = (const struct GNUNET_CADET_ConnectionTunnelIdentifier *) &msg[1];
1465 chns = (const struct GNUNET_CADET_ChannelTunnelNumber *) &conns[c_n];
1466 h->info_cb.tunnel_cb (h->info_cls,
1472 ntohs (msg->estate),
1473 ntohs (msg->cstate));
1478 * Reconnect to the service, retransmit all infomation to try to restore the
1481 * @param h handle to the cadet
1484 reconnect (struct GNUNET_CADET_Handle *h)
1486 struct GNUNET_MQ_MessageHandler handlers[] = {
1487 GNUNET_MQ_hd_fixed_size (channel_created,
1488 GNUNET_MESSAGE_TYPE_CADET_LOCAL_CHANNEL_CREATE,
1489 struct GNUNET_CADET_LocalChannelCreateMessage,
1491 GNUNET_MQ_hd_fixed_size (channel_destroy,
1492 GNUNET_MESSAGE_TYPE_CADET_LOCAL_CHANNEL_DESTROY,
1493 struct GNUNET_CADET_LocalChannelDestroyMessage,
1495 GNUNET_MQ_hd_var_size (local_data,
1496 GNUNET_MESSAGE_TYPE_CADET_LOCAL_DATA,
1497 struct GNUNET_CADET_LocalData,
1499 GNUNET_MQ_hd_fixed_size (local_ack,
1500 GNUNET_MESSAGE_TYPE_CADET_LOCAL_ACK,
1501 struct GNUNET_CADET_LocalAck,
1503 GNUNET_MQ_hd_var_size (get_peers,
1504 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEERS,
1505 struct GNUNET_CADET_LocalInfoPeer,
1507 GNUNET_MQ_hd_var_size (get_peer,
1508 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEER,
1509 struct GNUNET_CADET_LocalInfoPeer,
1511 GNUNET_MQ_hd_var_size (get_tunnels,
1512 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNELS,
1513 struct GNUNET_CADET_LocalInfoTunnel,
1515 GNUNET_MQ_hd_var_size (get_tunnel,
1516 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNEL,
1517 struct GNUNET_CADET_LocalInfoTunnel,
1520 // GNUNET_MQ_hd_fixed_Y size (channel_destroyed,
1521 // GNUNET_MESSAGE_TYPE_CADET_CHANNEL_OPEN_NACK_DEPRECATED,
1522 // struct GNUNET_CADET_ChannelDestroyMessage);
1523 GNUNET_MQ_handler_end ()
1525 struct GNUNET_CADET_Channel *ch;
1527 while (NULL != (ch = h->channels_head))
1529 LOG (GNUNET_ERROR_TYPE_DEBUG,
1530 "Destroying channel due to a reconnect\n");
1531 destroy_channel (ch);
1534 LOG (GNUNET_ERROR_TYPE_DEBUG, "Connecting to CADET\n");
1538 GNUNET_MQ_destroy (h->mq);
1541 h->mq = GNUNET_CLIENT_connect (h->cfg,
1548 schedule_reconnect (h);
1553 h->reconnect_time = GNUNET_TIME_UNIT_MILLISECONDS;
1558 * Reconnect callback: tries to reconnect again after a failer previous
1561 * @param cls closure (cadet handle)
1564 reconnect_cbk (void *cls)
1566 struct GNUNET_CADET_Handle *h = cls;
1568 h->reconnect_task = NULL;
1574 * Reconnect to the service, retransmit all infomation to try to restore the
1577 * @param h handle to the cadet
1579 * @return #GNUNET_YES in case of sucess, #GNUNET_NO otherwise (service down...)
1582 schedule_reconnect (struct GNUNET_CADET_Handle *h)
1584 if (NULL == h->reconnect_task)
1586 h->reconnect_task = GNUNET_SCHEDULER_add_delayed (h->reconnect_time,
1588 h->reconnect_time = GNUNET_TIME_STD_BACKOFF (h->reconnect_time);
1593 /******************************************************************************/
1594 /********************** API CALL DEFINITIONS *************************/
1595 /******************************************************************************/
1597 struct GNUNET_CADET_Handle *
1598 GNUNET_CADET_connect (const struct GNUNET_CONFIGURATION_Handle *cfg,
1600 GNUNET_CADET_ChannelEndHandler cleaner,
1601 const struct GNUNET_CADET_MessageHandler *handlers)
1603 struct GNUNET_CADET_Handle *h;
1605 h = GNUNET_new (struct GNUNET_CADET_Handle);
1606 LOG (GNUNET_ERROR_TYPE_DEBUG,
1607 "GNUNET_CADET_connect() %p\n",
1610 h->cleaner = cleaner;
1611 h->ports = GNUNET_CONTAINER_multihashmap_create (4, GNUNET_YES);
1616 GNUNET_CADET_disconnect (h);
1620 h->message_handlers = handlers;
1621 h->next_ccn.channel_of_client = htonl (GNUNET_CADET_LOCAL_CHANNEL_ID_CLI);
1622 h->reconnect_time = GNUNET_TIME_UNIT_MILLISECONDS;
1623 h->reconnect_task = NULL;
1625 /* count handlers */
1626 for (h->n_handlers = 0;
1627 handlers && handlers[h->n_handlers].type;
1634 * Disconnect from the cadet service. All channels will be destroyed. All channel
1635 * disconnect callbacks will be called on any still connected peers, notifying
1636 * about their disconnection. The registered inbound channel cleaner will be
1637 * called should any inbound channels still exist.
1639 * @param handle connection to cadet to disconnect
1642 GNUNET_CADET_disconnect (struct GNUNET_CADET_Handle *handle)
1644 struct GNUNET_CADET_Channel *ch;
1645 struct GNUNET_CADET_Channel *aux;
1646 struct GNUNET_CADET_TransmitHandle *th;
1648 LOG (GNUNET_ERROR_TYPE_DEBUG,
1649 "CADET DISCONNECT\n");
1650 ch = handle->channels_head;
1654 if (ntohl (ch->ccn.channel_of_client) >= GNUNET_CADET_LOCAL_CHANNEL_ID_CLI)
1657 LOG (GNUNET_ERROR_TYPE_DEBUG,
1658 "channel %X not destroyed\n",
1659 ntohl (ch->ccn.channel_of_client));
1661 destroy_channel (ch);
1664 while (NULL != (th = handle->th_head))
1666 struct GNUNET_MessageHeader *msg;
1668 /* Make sure it is an allowed packet (everything else should have been
1669 * already canceled).
1671 GNUNET_break (GNUNET_NO == th_is_payload (th));
1672 msg = (struct GNUNET_MessageHeader *) &th[1];
1673 switch (ntohs(msg->type))
1675 case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_OPEN:
1676 case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DESTROY:
1677 case GNUNET_MESSAGE_TYPE_CADET_LOCAL_PORT_OPEN:
1678 case GNUNET_MESSAGE_TYPE_CADET_LOCAL_PORT_CLOSE:
1679 case GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_CHANNELS:
1680 case GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_CHANNEL:
1681 case GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEER:
1682 case GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEERS:
1683 case GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNEL:
1684 case GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNELS:
1688 LOG (GNUNET_ERROR_TYPE_ERROR, "unexpected unsent msg %s\n",
1689 GC_m2s (ntohs(msg->type)));
1692 GNUNET_CADET_notify_transmit_ready_cancel (th);
1695 if (NULL != handle->mq)
1697 GNUNET_MQ_destroy (handle->mq);
1700 if (NULL != handle->reconnect_task)
1702 GNUNET_SCHEDULER_cancel(handle->reconnect_task);
1703 handle->reconnect_task = NULL;
1706 GNUNET_CONTAINER_multihashmap_destroy (handle->ports);
1707 handle->ports = NULL;
1708 GNUNET_free (handle);
1713 * Open a port to receive incomming channels.
1715 * @param h CADET handle.
1716 * @param port Hash representing the port number.
1717 * @param new_channel Function called when an channel is received.
1718 * @param new_channel_cls Closure for @a new_channel.
1719 * @return Port handle.
1721 struct GNUNET_CADET_Port *
1722 GNUNET_CADET_open_port (struct GNUNET_CADET_Handle *h,
1723 const struct GNUNET_HashCode *port,
1724 GNUNET_CADET_InboundChannelNotificationHandler
1726 void *new_channel_cls)
1728 struct GNUNET_CADET_PortMessage *msg;
1729 struct GNUNET_MQ_Envelope *env;
1730 struct GNUNET_CADET_Port *p;
1732 GNUNET_assert (NULL != new_channel);
1733 p = GNUNET_new (struct GNUNET_CADET_Port);
1735 p->hash = GNUNET_new (struct GNUNET_HashCode);
1737 p->handler = new_channel;
1738 p->cls = new_channel_cls;
1739 GNUNET_assert (GNUNET_OK ==
1740 GNUNET_CONTAINER_multihashmap_put (h->ports,
1743 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1745 env = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_CADET_LOCAL_PORT_OPEN);
1746 msg->port = *p->hash;
1747 GNUNET_MQ_send (h->mq, env);
1753 * Close a port opened with @a GNUNET_CADET_open_port.
1754 * The @a new_channel callback will no longer be called.
1756 * @param p Port handle.
1759 GNUNET_CADET_close_port (struct GNUNET_CADET_Port *p)
1761 struct GNUNET_CADET_PortMessage *msg;
1762 struct GNUNET_MQ_Envelope *env;
1763 struct GNUNET_HashCode *id;
1765 env = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_CADET_LOCAL_PORT_CLOSE);
1767 id = NULL != p->hash ? p->hash : &p->id;
1769 GNUNET_MQ_send (p->cadet->mq, env);
1770 GNUNET_CONTAINER_multihashmap_remove (p->cadet->ports, id, p);
1771 GNUNET_free_non_null (p->hash);
1777 * Create a new channel towards a remote peer.
1779 * If the destination port is not open by any peer or the destination peer
1780 * does not accept the channel, #GNUNET_CADET_ChannelEndHandler will be called
1783 * @param h cadet handle
1784 * @param channel_ctx client's channel context to associate with the channel
1785 * @param peer peer identity the channel should go to
1786 * @param port Port hash (port number).
1787 * @param options CadetOption flag field, with all desired option bits set to 1.
1788 * @return handle to the channel
1790 struct GNUNET_CADET_Channel *
1791 GNUNET_CADET_channel_create (struct GNUNET_CADET_Handle *h,
1793 const struct GNUNET_PeerIdentity *peer,
1794 const struct GNUNET_HashCode *port,
1795 enum GNUNET_CADET_ChannelOption options)
1797 struct GNUNET_CADET_LocalChannelCreateMessage *msg;
1798 struct GNUNET_MQ_Envelope *env;
1799 struct GNUNET_CADET_Channel *ch;
1800 struct GNUNET_CADET_ClientChannelNumber ccn;
1802 ccn.channel_of_client = htonl (0);
1803 ch = create_channel (h, ccn);
1804 ch->ctx = channel_ctx;
1805 ch->peer = GNUNET_PEER_intern (peer);
1807 LOG (GNUNET_ERROR_TYPE_DEBUG,
1808 "Creating new channel to %s:%u at %p number %X\n",
1812 ntohl (ch->ccn.channel_of_client));
1813 env = GNUNET_MQ_msg (msg,
1814 GNUNET_MESSAGE_TYPE_CADET_LOCAL_CHANNEL_CREATE);
1818 msg->opt = htonl (options);
1819 GNUNET_MQ_send (h->mq,
1826 GNUNET_CADET_channel_destroy (struct GNUNET_CADET_Channel *channel)
1828 struct GNUNET_CADET_Handle *h;
1829 struct GNUNET_CADET_LocalChannelDestroyMessage *msg;
1830 struct GNUNET_MQ_Envelope *env;
1831 struct GNUNET_CADET_TransmitHandle *th;
1832 struct GNUNET_CADET_TransmitHandle *next;
1834 LOG (GNUNET_ERROR_TYPE_DEBUG,
1835 "Destroying channel\n");
1837 for (th = h->th_head; th != NULL; th = next)
1840 if (th->channel == channel)
1843 if (GNUNET_YES == th_is_payload (th))
1845 /* applications should cancel before destroying channel */
1846 LOG (GNUNET_ERROR_TYPE_WARNING,
1847 "Channel destroyed without cancelling transmission requests\n");
1848 th->notify (th->notify_cls, 0, NULL);
1852 LOG (GNUNET_ERROR_TYPE_WARNING,
1853 "no meta-traffic should be queued\n");
1855 GNUNET_CONTAINER_DLL_remove (h->th_head,
1858 GNUNET_CADET_notify_transmit_ready_cancel (th);
1862 env = GNUNET_MQ_msg (msg,
1863 GNUNET_MESSAGE_TYPE_CADET_LOCAL_CHANNEL_DESTROY);
1864 msg->ccn = channel->ccn;
1865 GNUNET_MQ_send (h->mq,
1868 destroy_channel (channel);
1873 * Get information about a channel.
1875 * @param channel Channel handle.
1876 * @param option Query (GNUNET_CADET_OPTION_*).
1877 * @param ... dependant on option, currently not used
1879 * @return Union with an answer to the query.
1881 const union GNUNET_CADET_ChannelInfo *
1882 GNUNET_CADET_channel_get_info (struct GNUNET_CADET_Channel *channel,
1883 enum GNUNET_CADET_ChannelOption option, ...)
1885 static int bool_flag;
1886 const union GNUNET_CADET_ChannelInfo *ret;
1890 case GNUNET_CADET_OPTION_NOBUFFER:
1891 case GNUNET_CADET_OPTION_RELIABLE:
1892 case GNUNET_CADET_OPTION_OUT_OF_ORDER:
1893 if (0 != (option & channel->options))
1894 bool_flag = GNUNET_YES;
1896 bool_flag = GNUNET_NO;
1897 ret = (const union GNUNET_CADET_ChannelInfo *) &bool_flag;
1899 case GNUNET_CADET_OPTION_PEER:
1900 ret = (const union GNUNET_CADET_ChannelInfo *) GNUNET_PEER_resolve2 (channel->peer);
1911 struct GNUNET_CADET_TransmitHandle *
1912 GNUNET_CADET_notify_transmit_ready (struct GNUNET_CADET_Channel *channel,
1914 struct GNUNET_TIME_Relative maxdelay,
1916 GNUNET_CONNECTION_TransmitReadyNotify notify,
1919 struct GNUNET_CADET_TransmitHandle *th;
1921 GNUNET_assert (NULL != channel);
1922 GNUNET_assert (NULL != notify);
1923 GNUNET_assert (GNUNET_CONSTANTS_MAX_CADET_MESSAGE_SIZE >= notify_size);
1924 LOG (GNUNET_ERROR_TYPE_DEBUG,
1925 "CADET NOTIFY TRANSMIT READY on channel %X allow_send is %u to %s with %u bytes\n",
1926 ntohl (channel->ccn.channel_of_client),
1927 channel->allow_send,
1928 (ntohl (channel->ccn.channel_of_client) >=
1929 GNUNET_CADET_LOCAL_CHANNEL_ID_CLI)
1932 (unsigned int) notify_size);
1933 if (GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us != maxdelay.rel_value_us)
1935 LOG (GNUNET_ERROR_TYPE_WARNING,
1936 "CADET transmit ready timeout is deprected (has no effect)\n");
1939 th = GNUNET_new (struct GNUNET_CADET_TransmitHandle);
1940 th->channel = channel;
1941 th->size = notify_size;
1942 th->notify = notify;
1943 th->notify_cls = notify_cls;
1944 if (0 != channel->allow_send)
1945 th->request_data_task
1946 = GNUNET_SCHEDULER_add_now (&request_data,
1949 add_to_queue (channel->cadet,
1956 GNUNET_CADET_notify_transmit_ready_cancel (struct GNUNET_CADET_TransmitHandle *th)
1958 if (NULL != th->request_data_task)
1960 GNUNET_SCHEDULER_cancel (th->request_data_task);
1961 th->request_data_task = NULL;
1963 remove_from_queue (th);
1969 * Send an ack on the channel to confirm the processing of a message.
1971 * @param ch Channel on which to send the ACK.
1974 GNUNET_CADET_receive_done (struct GNUNET_CADET_Channel *channel)
1976 struct GNUNET_CADET_LocalAck *msg;
1977 struct GNUNET_MQ_Envelope *env;
1979 env = GNUNET_MQ_msg (msg,
1980 GNUNET_MESSAGE_TYPE_CADET_LOCAL_ACK);
1981 LOG (GNUNET_ERROR_TYPE_DEBUG,
1982 "Sending ACK on channel %X\n",
1983 ntohl (channel->ccn.channel_of_client));
1984 msg->ccn = channel->ccn;
1985 GNUNET_MQ_send (channel->cadet->mq,
1991 send_info_request (struct GNUNET_CADET_Handle *h, uint16_t type)
1993 struct GNUNET_MessageHeader *msg;
1994 struct GNUNET_MQ_Envelope *env;
1996 LOG (GNUNET_ERROR_TYPE_DEBUG,
1997 " Sending %s monitor message to service\n",
2000 env = GNUNET_MQ_msg (msg, type);
2001 GNUNET_MQ_send (h->mq, env);
2006 * Request a debug dump on the service's STDERR.
2008 * WARNING: unstable API, likely to change in the future!
2010 * @param h cadet handle
2013 GNUNET_CADET_request_dump (struct GNUNET_CADET_Handle *h)
2015 LOG (GNUNET_ERROR_TYPE_DEBUG, "requesting dump\n");
2016 send_info_request (h, GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_DUMP);
2021 * Request information about peers known to the running cadet service.
2022 * The callback will be called for every peer known to the service.
2023 * Only one info request (of any kind) can be active at once.
2026 * WARNING: unstable API, likely to change in the future!
2028 * @param h Handle to the cadet peer.
2029 * @param callback Function to call with the requested data.
2030 * @param callback_cls Closure for @c callback.
2032 * @return #GNUNET_OK / #GNUNET_SYSERR
2035 GNUNET_CADET_get_peers (struct GNUNET_CADET_Handle *h,
2036 GNUNET_CADET_PeersCB callback,
2039 if (NULL != h->info_cb.peers_cb)
2042 return GNUNET_SYSERR;
2044 send_info_request (h, GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEERS);
2045 h->info_cb.peers_cb = callback;
2046 h->info_cls = callback_cls;
2052 * Cancel a peer info request. The callback will not be called (anymore).
2054 * WARNING: unstable API, likely to change in the future!
2056 * @param h Cadet handle.
2058 * @return Closure given to GNUNET_CADET_get_peers.
2061 GNUNET_CADET_get_peers_cancel (struct GNUNET_CADET_Handle *h)
2066 h->info_cb.peers_cb = NULL;
2073 * Request information about a peer known to the running cadet peer.
2074 * The callback will be called for the tunnel once.
2075 * Only one info request (of any kind) can be active at once.
2077 * WARNING: unstable API, likely to change in the future!
2079 * @param h Handle to the cadet peer.
2080 * @param id Peer whose tunnel to examine.
2081 * @param callback Function to call with the requested data.
2082 * @param callback_cls Closure for @c callback.
2084 * @return #GNUNET_OK / #GNUNET_SYSERR
2087 GNUNET_CADET_get_peer (struct GNUNET_CADET_Handle *h,
2088 const struct GNUNET_PeerIdentity *id,
2089 GNUNET_CADET_PeerCB callback,
2092 struct GNUNET_CADET_LocalInfo *msg;
2093 struct GNUNET_MQ_Envelope *env;
2095 if (NULL != h->info_cb.peer_cb)
2098 return GNUNET_SYSERR;
2101 env = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEER);
2103 GNUNET_MQ_send (h->mq, env);
2105 h->info_cb.peer_cb = callback;
2106 h->info_cls = callback_cls;
2112 * Request information about tunnels of the running cadet peer.
2113 * The callback will be called for every tunnel of the service.
2114 * Only one info request (of any kind) can be active at once.
2116 * WARNING: unstable API, likely to change in the future!
2118 * @param h Handle to the cadet peer.
2119 * @param callback Function to call with the requested data.
2120 * @param callback_cls Closure for @c callback.
2122 * @return #GNUNET_OK / #GNUNET_SYSERR
2125 GNUNET_CADET_get_tunnels (struct GNUNET_CADET_Handle *h,
2126 GNUNET_CADET_TunnelsCB callback,
2129 if (NULL != h->info_cb.tunnels_cb)
2132 return GNUNET_SYSERR;
2134 send_info_request (h, GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNELS);
2135 h->info_cb.tunnels_cb = callback;
2136 h->info_cls = callback_cls;
2142 * Cancel a monitor request. The monitor callback will not be called.
2144 * @param h Cadet handle.
2146 * @return Closure given to GNUNET_CADET_get_tunnels.
2149 GNUNET_CADET_get_tunnels_cancel (struct GNUNET_CADET_Handle *h)
2153 h->info_cb.tunnels_cb = NULL;
2163 * Request information about a tunnel of the running cadet peer.
2164 * The callback will be called for the tunnel once.
2165 * Only one info request (of any kind) can be active at once.
2167 * WARNING: unstable API, likely to change in the future!
2169 * @param h Handle to the cadet peer.
2170 * @param id Peer whose tunnel to examine.
2171 * @param callback Function to call with the requested data.
2172 * @param callback_cls Closure for @c callback.
2174 * @return #GNUNET_OK / #GNUNET_SYSERR
2177 GNUNET_CADET_get_tunnel (struct GNUNET_CADET_Handle *h,
2178 const struct GNUNET_PeerIdentity *id,
2179 GNUNET_CADET_TunnelCB callback,
2182 struct GNUNET_CADET_LocalInfo *msg;
2183 struct GNUNET_MQ_Envelope *env;
2185 if (NULL != h->info_cb.tunnel_cb)
2188 return GNUNET_SYSERR;
2191 env = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNEL);
2193 GNUNET_MQ_send (h->mq, env);
2195 h->info_cb.tunnel_cb = callback;
2196 h->info_cls = callback_cls;
2202 * Request information about a specific channel of the running cadet peer.
2204 * WARNING: unstable API, likely to change in the future!
2205 * FIXME Add destination option.
2207 * @param h Handle to the cadet peer.
2208 * @param initiator ID of the owner of the channel.
2209 * @param channel_number Channel number.
2210 * @param callback Function to call with the requested data.
2211 * @param callback_cls Closure for @c callback.
2213 * @return #GNUNET_OK / #GNUNET_SYSERR
2216 GNUNET_CADET_show_channel (struct GNUNET_CADET_Handle *h,
2217 struct GNUNET_PeerIdentity *initiator,
2218 unsigned int channel_number,
2219 GNUNET_CADET_ChannelCB callback,
2222 struct GNUNET_CADET_LocalInfo *msg;
2223 struct GNUNET_MQ_Envelope *env;
2225 if (NULL != h->info_cb.channel_cb)
2228 return GNUNET_SYSERR;
2231 env = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_CHANNEL);
2232 msg->peer = *initiator;
2233 msg->ccn.channel_of_client = htonl (channel_number);
2234 GNUNET_MQ_send (h->mq, env);
2236 h->info_cb.channel_cb = callback;
2237 h->info_cls = callback_cls;
2243 * Function called to notify a client about the connection
2244 * begin ready to queue more data. "buf" will be
2245 * NULL and "size" zero if the connection was closed for
2246 * writing in the meantime.
2248 * @param cls closure
2249 * @param size number of bytes available in buf
2250 * @param buf where the callee should write the message
2251 * @return number of bytes written to buf
2254 cadet_mq_ntr (void *cls, size_t size,
2257 struct GNUNET_MQ_Handle *mq = cls;
2258 struct CadetMQState *state = GNUNET_MQ_impl_state (mq);
2259 const struct GNUNET_MessageHeader *msg = GNUNET_MQ_impl_current (mq);
2265 GNUNET_MQ_inject_error (mq, GNUNET_MQ_ERROR_WRITE);
2268 msize = ntohs (msg->size);
2269 GNUNET_assert (msize <= size);
2270 GNUNET_memcpy (buf, msg, msize);
2271 GNUNET_MQ_impl_send_continue (mq);
2277 * Signature of functions implementing the
2278 * sending functionality of a message queue.
2280 * @param mq the message queue
2281 * @param msg the message to send
2282 * @param impl_state state of the implementation
2285 cadet_mq_send_impl_old (struct GNUNET_MQ_Handle *mq,
2286 const struct GNUNET_MessageHeader *msg,
2289 struct CadetMQState *state = impl_state;
2291 GNUNET_assert (NULL == state->th);
2293 GNUNET_CADET_notify_transmit_ready (state->channel,
2294 /* FIXME: add option for corking */
2296 GNUNET_TIME_UNIT_FOREVER_REL,
2304 * Signature of functions implementing the
2305 * destruction of a message queue.
2306 * Implementations must not free 'mq', but should
2307 * take care of 'impl_state'.
2309 * @param mq the message queue to destroy
2310 * @param impl_state state of the implementation
2313 cadet_mq_destroy_impl_old (struct GNUNET_MQ_Handle *mq,
2316 struct CadetMQState *state = impl_state;
2318 if (NULL != state->th)
2319 GNUNET_CADET_notify_transmit_ready_cancel (state->th);
2321 GNUNET_free (state);
2326 * Create a message queue for a cadet channel.
2327 * The message queue can only be used to transmit messages,
2328 * not to receive them.
2330 * @param channel the channel to create the message qeue for
2331 * @return a message queue to messages over the channel
2333 struct GNUNET_MQ_Handle *
2334 GNUNET_CADET_mq_create (struct GNUNET_CADET_Channel *channel)
2336 struct GNUNET_MQ_Handle *mq;
2337 struct CadetMQState *state;
2339 state = GNUNET_new (struct CadetMQState);
2340 state->channel = channel;
2342 mq = GNUNET_MQ_queue_for_callbacks (&cadet_mq_send_impl_old,
2343 &cadet_mq_destroy_impl_old,
2344 NULL, /* FIXME: cancel impl. */
2346 NULL, /* no msg handlers */
2347 NULL, /* no err handlers */
2348 NULL); /* no handler cls */
2354 * Transitional function to convert an unsigned int port to a hash value.
2355 * WARNING: local static value returned, NOT reentrant!
2356 * WARNING: do not use this function for new code!
2358 * @param port Numerical port (unsigned int format).
2360 * @return A GNUNET_HashCode usable for the new CADET API.
2362 const struct GNUNET_HashCode *
2363 GC_u2h (uint32_t port)
2365 static struct GNUNET_HashCode hash;
2367 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2368 "This is a transitional function, "
2369 "use proper crypto hashes as CADET ports\n");
2370 GNUNET_CRYPTO_hash (&port, sizeof (port), &hash);
2377 /******************************************************************************/
2378 /******************************* MQ-BASED API *********************************/
2379 /******************************************************************************/
2382 * Connect to the MQ-based cadet service.
2384 * @param cfg Configuration to use.
2386 * @return Handle to the cadet service NULL on error.
2388 struct GNUNET_CADET_Handle *
2389 GNUNET_CADET_connecT (const struct GNUNET_CONFIGURATION_Handle *cfg)
2391 struct GNUNET_CADET_Handle *h;
2393 LOG (GNUNET_ERROR_TYPE_DEBUG,
2394 "GNUNET_CADET_connecT()\n");
2395 h = GNUNET_new (struct GNUNET_CADET_Handle);
2397 h->mq_api = GNUNET_YES;
2398 h->ports = GNUNET_CONTAINER_multihashmap_create (4, GNUNET_YES);
2403 GNUNET_CADET_disconnect (h);
2406 h->next_ccn.channel_of_client = htonl (GNUNET_CADET_LOCAL_CHANNEL_ID_CLI);
2407 h->reconnect_time = GNUNET_TIME_UNIT_MILLISECONDS;
2408 h->reconnect_task = NULL;
2415 * Open a port to receive incomming MQ-based channels.
2417 * @param h CADET handle.
2418 * @param port Hash identifying the port.
2419 * @param connects Function called when an incoming channel is connected.
2420 * @param connects_cls Closure for the @a connects handler.
2421 * @param window_changes Function called when the transmit window size changes.
2422 * @param disconnects Function called when a channel is disconnected.
2423 * @param handlers Callbacks for messages we care about, NULL-terminated.
2425 * @return Port handle.
2427 struct GNUNET_CADET_Port *
2428 GNUNET_CADET_open_porT (struct GNUNET_CADET_Handle *h,
2429 const struct GNUNET_HashCode *port,
2430 GNUNET_CADET_ConnectEventHandler connects,
2431 void * connects_cls,
2432 GNUNET_CADET_WindowSizeEventHandler window_changes,
2433 GNUNET_CADET_DisconnectEventHandler disconnects,
2434 const struct GNUNET_MQ_MessageHandler *handlers)
2436 struct GNUNET_CADET_PortMessage *msg;
2437 struct GNUNET_MQ_Envelope *env;
2438 struct GNUNET_CADET_Port *p;
2440 GNUNET_assert (NULL != connects);
2441 GNUNET_assert (NULL != disconnects);
2443 p = GNUNET_new (struct GNUNET_CADET_Port);
2446 p->connects = connects;
2447 p->cls = connects_cls;
2448 p->window_changes = window_changes;
2449 p->disconnects = disconnects;
2450 p->handlers = handlers;
2452 GNUNET_assert (GNUNET_OK ==
2453 GNUNET_CONTAINER_multihashmap_put (h->ports,
2456 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
2458 env = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_CADET_LOCAL_PORT_OPEN);
2460 GNUNET_MQ_send (h->mq, env);
2467 * Create a new channel towards a remote peer.
2469 * If the destination port is not open by any peer or the destination peer
2470 * does not accept the channel, #GNUNET_CADET_ChannelEndHandler will be called
2473 * @param h CADET handle.
2474 * @param channel_cls Closure for the channel. It's given to:
2475 * - The disconnect handler @a disconnects
2476 * - Each message type callback in @a handlers
2477 * @param destination Peer identity the channel should go to.
2478 * @param port Identification of the destination port.
2479 * @param options CadetOption flag field, with all desired option bits set to 1.
2480 * @param window_changes Function called when the transmit window size changes.
2481 * @param disconnects Function called when the channel is disconnected.
2482 * @param handlers Callbacks for messages we care about, NULL-terminated.
2484 * @return Handle to the channel.
2486 struct GNUNET_CADET_Channel *
2487 GNUNET_CADET_channel_creatE (struct GNUNET_CADET_Handle *h,
2489 const struct GNUNET_PeerIdentity *destination,
2490 const struct GNUNET_HashCode *port,
2491 enum GNUNET_CADET_ChannelOption options,
2492 GNUNET_CADET_WindowSizeEventHandler window_changes,
2493 GNUNET_CADET_DisconnectEventHandler disconnects,
2494 const struct GNUNET_MQ_MessageHandler *handlers)
2496 struct GNUNET_CADET_Channel *ch;
2497 struct GNUNET_CADET_ClientChannelNumber ccn;
2498 struct GNUNET_CADET_LocalChannelCreateMessage *msg;
2499 struct GNUNET_MQ_Envelope *env;
2501 GNUNET_assert (NULL != disconnects);
2503 /* Save parameters */
2504 ccn.channel_of_client = htonl (0);
2505 ch = create_channel (h, ccn);
2506 ch->ctx = channel_cls;
2507 ch->peer = GNUNET_PEER_intern (destination);
2508 ch->options = options;
2509 ch->window_changes = window_changes;
2510 ch->disconnects = disconnects;
2512 /* Create MQ for channel */
2513 ch->mq = GNUNET_MQ_queue_for_callbacks (&cadet_mq_send_impl,
2514 &cadet_mq_destroy_impl,
2515 &cadet_mq_cancel_impl,
2518 &cadet_mq_error_handler,
2520 GNUNET_MQ_set_handlers_closure (ch->mq, channel_cls);
2522 /* Request channel creation to service */
2523 env = GNUNET_MQ_msg (msg,
2524 GNUNET_MESSAGE_TYPE_CADET_LOCAL_CHANNEL_CREATE);
2527 msg->peer = *destination;
2528 msg->opt = htonl (options);
2529 GNUNET_MQ_send (h->mq,
2537 * Obtain the message queue for a connected peer.
2539 * @param channel The channel handle from which to get the MQ.
2541 * @return NULL if @a channel is not yet connected.
2543 struct GNUNET_MQ_Handle *
2544 GNUNET_CADET_get_mq (const struct GNUNET_CADET_Channel *channel)