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 * Task to allow mq to send more traffic.
296 struct GNUNET_SCHEDULER_Task *mq_cont;
299 * Pending envelope in case we don't have an ACK from the service.
301 struct GNUNET_MQ_Envelope *pending_env;
304 * Window change handler.
306 GNUNET_CADET_WindowSizeEventHandler window_changes;
309 * Disconnect handler.
311 GNUNET_CADET_DisconnectEventHandler disconnects;
317 * Opaque handle to a port.
319 struct GNUNET_CADET_Port
322 * Handle to the CADET session this port belongs to.
324 struct GNUNET_CADET_Handle *cadet;
331 struct GNUNET_HashCode *hash;
334 * Callback handler for incoming channels on this port.
336 GNUNET_CADET_InboundChannelNotificationHandler *handler;
339 * Closure for @a handler.
343 /***************************** MQ ************************************/
348 struct GNUNET_HashCode id;
351 * Handler for incoming channels on this port
353 GNUNET_CADET_ConnectEventHandler connects;
356 * Closure for @ref connects
361 * Window size change handler.
363 GNUNET_CADET_WindowSizeEventHandler window_changes;
366 * Handler called when an incoming channel is destroyed..
368 GNUNET_CADET_DisconnectEventHandler disconnects;
371 * Payload handlers for incoming channels.
373 const struct GNUNET_MQ_MessageHandler *handlers;
378 * Implementation state for cadet's message queue.
383 * The current transmit handle, or NULL
384 * if no transmit is active.
386 struct GNUNET_CADET_TransmitHandle *th;
389 * Channel to send the data over.
391 struct GNUNET_CADET_Channel *channel;
396 /******************************************************************************/
397 /********************* FUNCTION DECLARATIONS *************************/
398 /******************************************************************************/
401 * Reconnect to the service, retransmit all infomation to try to restore the
404 * @param h Handle to the CADET service.
407 schedule_reconnect (struct GNUNET_CADET_Handle *h);
411 * Reconnect callback: tries to reconnect again after a failer previous
414 * @param cls Closure (cadet handle).
417 reconnect_cbk (void *cls);
421 * Reconnect to the service, retransmit all infomation to try to restore the
424 * @param h handle to the cadet
427 reconnect (struct GNUNET_CADET_Handle *h);
430 /******************************************************************************/
431 /*********************** AUXILIARY FUNCTIONS *************************/
432 /******************************************************************************/
435 * Check if transmission is a payload packet.
437 * @param th Transmission handle.
439 * @return #GNUNET_YES if it is a payload packet,
440 * #GNUNET_NO if it is a cadet management packet.
443 th_is_payload (struct GNUNET_CADET_TransmitHandle *th)
445 return (th->notify != NULL) ? GNUNET_YES : GNUNET_NO;
450 * Find the Port struct for a hash.
452 * @param h CADET handle.
453 * @param hash HashCode for the port number.
455 * @return The port handle if known, NULL otherwise.
457 static struct GNUNET_CADET_Port *
458 find_port (const struct GNUNET_CADET_Handle *h,
459 const struct GNUNET_HashCode *hash)
461 struct GNUNET_CADET_Port *p;
463 p = GNUNET_CONTAINER_multihashmap_get (h->ports, hash);
470 * Get the channel handler for the channel specified by id from the given handle
472 * @param h Cadet handle
473 * @param ccn ID of the wanted channel
474 * @return handle to the required channel or NULL if not found
476 static struct GNUNET_CADET_Channel *
477 retrieve_channel (struct GNUNET_CADET_Handle *h,
478 struct GNUNET_CADET_ClientChannelNumber ccn)
480 struct GNUNET_CADET_Channel *ch;
482 for (ch = h->channels_head; NULL != ch; ch = ch->next)
483 if (ch->ccn.channel_of_client == ccn.channel_of_client)
490 * Create a new channel and insert it in the channel list of the cadet handle
492 * @param h Cadet handle
493 * @param ccn Desired ccn of the channel, 0 to assign one automatically.
495 * @return Handle to the created channel.
497 static struct GNUNET_CADET_Channel *
498 create_channel (struct GNUNET_CADET_Handle *h,
499 struct GNUNET_CADET_ClientChannelNumber ccn)
501 struct GNUNET_CADET_Channel *ch;
503 ch = GNUNET_new (struct GNUNET_CADET_Channel);
504 GNUNET_CONTAINER_DLL_insert (h->channels_head,
508 if (0 == ccn.channel_of_client)
510 ch->ccn = h->next_ccn;
511 while (NULL != retrieve_channel (h,
514 h->next_ccn.channel_of_client
515 = htonl (1 + ntohl (h->next_ccn.channel_of_client));
516 if (0 == ntohl (h->next_ccn.channel_of_client))
517 h->next_ccn.channel_of_client
518 = htonl (GNUNET_CADET_LOCAL_CHANNEL_ID_CLI);
530 * Destroy the specified channel.
531 * - Destroys all peers, calling the disconnect callback on each if needed
532 * - Cancels all outgoing traffic for that channel, calling respective notifys
533 * - Calls cleaner if channel was inbound
534 * - Frees all memory used
536 * @param ch Pointer to the channel.
537 * @param call_cleaner Whether to call the cleaner handler.
539 * @return Handle to the required channel or NULL if not found.
542 destroy_channel (struct GNUNET_CADET_Channel *ch)
544 struct GNUNET_CADET_Handle *h;
545 struct GNUNET_CADET_TransmitHandle *th;
546 struct GNUNET_CADET_TransmitHandle *next;
554 LOG (GNUNET_ERROR_TYPE_DEBUG,
555 " destroy_channel %X of %p\n",
559 GNUNET_CONTAINER_DLL_remove (h->channels_head,
562 if (NULL != ch->mq_cont)
564 GNUNET_SCHEDULER_cancel (ch->mq_cont);
567 /* signal channel destruction */
570 if (NULL != h->cleaner)
573 LOG (GNUNET_ERROR_TYPE_DEBUG,
574 " calling cleaner\n");
575 h->cleaner (h->cls, ch, ch->ctx);
577 else if (NULL != ch->disconnects)
579 LOG (GNUNET_ERROR_TYPE_DEBUG,
580 " calling disconnect handler\n");
581 ch->disconnects (ch->ctx, ch);
585 /* Application won't be aware of the channel destruction and use
586 * a pointer to free'd memory.
592 /* check that clients did not leave messages behind in the queue */
593 for (th = h->th_head; NULL != th; th = next)
596 if (th->channel != ch)
598 /* Clients should have aborted their requests already.
599 * Management traffic should be ok, as clients can't cancel that.
600 * If the service crashed and we are reconnecting, it's ok.
602 GNUNET_break (GNUNET_NO == th_is_payload (th));
603 GNUNET_CADET_notify_transmit_ready_cancel (th);
607 GNUNET_PEER_change_rc (ch->peer, -1);
613 * Add a transmit handle to the transmission queue and set the
616 * @param h cadet handle with the queue head and tail
617 * @param th handle to the packet to be transmitted
620 add_to_queue (struct GNUNET_CADET_Handle *h,
621 struct GNUNET_CADET_TransmitHandle *th)
623 GNUNET_CONTAINER_DLL_insert_tail (h->th_head,
630 * Remove a transmit handle from the transmission queue, if present.
632 * Safe to call even if not queued.
634 * @param th handle to the packet to be unqueued.
637 remove_from_queue (struct GNUNET_CADET_TransmitHandle *th)
639 struct GNUNET_CADET_Handle *h = th->channel->cadet;
641 /* It might or might not have been queued (rarely not), but check anyway. */
642 if (NULL != th->next || h->th_tail == th)
644 GNUNET_CONTAINER_DLL_remove (h->th_head, h->th_tail, th);
650 * Notify the application about a change in the window size (if needed).
652 * @param ch Channel to notify about.
655 notify_window_size (struct GNUNET_CADET_Channel *ch)
657 if (NULL != ch->window_changes)
659 ch->window_changes (ch->ctx, ch, ch->allow_send);
663 /******************************************************************************/
664 /*********************** MQ API CALLBACKS ****************************/
665 /******************************************************************************/
668 * Allow the MQ implementation to send the next message.
670 * @param cls Closure (channel whose mq to activate).
673 cadet_mq_send_continue (void *cls)
675 struct GNUNET_CADET_Channel *ch = cls;
678 GNUNET_MQ_impl_send_continue (ch->mq);
682 * Implement sending functionality of a message queue for
683 * us sending messages to a peer.
685 * Encapsulates the payload message in a #GNUNET_CADET_LocalData message
686 * in order to label the message with the channel ID and send the
687 * encapsulated message to the service.
689 * @param mq the message queue
690 * @param msg the message to send
691 * @param impl_state state of the implementation
694 cadet_mq_send_impl (struct GNUNET_MQ_Handle *mq,
695 const struct GNUNET_MessageHeader *msg,
698 struct GNUNET_CADET_Channel *ch = impl_state;
699 struct GNUNET_CADET_Handle *h = ch->cadet;
701 struct GNUNET_MQ_Envelope *env;
702 struct GNUNET_CADET_LocalData *cadet_msg;
707 /* We're currently reconnecting, pretend this worked */
708 GNUNET_MQ_impl_send_continue (mq);
712 /* check message size for sanity */
713 msize = ntohs (msg->size);
714 if (msize > GNUNET_CONSTANTS_MAX_CADET_MESSAGE_SIZE)
717 GNUNET_MQ_impl_send_continue (mq);
721 env = GNUNET_MQ_msg_nested_mh (cadet_msg,
722 GNUNET_MESSAGE_TYPE_CADET_LOCAL_DATA,
724 cadet_msg->ccn = ch->ccn;
726 if (0 < ch->allow_send)
728 /* Service has allowed this message, just send it and continue accepting */
729 GNUNET_MQ_send (h->mq, env);
731 ch->mq_cont = GNUNET_SCHEDULER_add_now (&cadet_mq_send_continue, ch);
732 // notify_window_size (ch); /* FIXME add "verbose" setting? */
736 /* Service has NOT allowed this message, queue it and wait for an ACK */
737 GNUNET_assert (NULL == ch->pending_env);
738 ch->pending_env = env;
744 * Handle destruction of a message queue. Implementations must not
745 * free @a mq, but should take care of @a impl_state.
747 * @param mq the message queue to destroy
748 * @param impl_state state of the implementation
751 cadet_mq_destroy_impl (struct GNUNET_MQ_Handle *mq,
754 struct GNUNET_CADET_Channel *ch = impl_state;
756 GNUNET_assert (mq == ch->mq);
762 * We had an error processing a message we forwarded from a peer to
763 * the CADET service. We should just complain about it but otherwise
764 * continue processing.
767 * @param error error code
770 cadet_mq_error_handler (void *cls,
771 enum GNUNET_MQ_Error error)
778 * Implementation function that cancels the currently sent message.
779 * Should basically undo whatever #mq_send_impl() did.
781 * @param mq message queue
782 * @param impl_state state specific to the implementation
786 cadet_mq_cancel_impl (struct GNUNET_MQ_Handle *mq,
789 struct GNUNET_CADET_Channel *ch = impl_state;
791 LOG (GNUNET_ERROR_TYPE_WARNING,
792 "Cannot cancel mq message on channel %X of %p\n",
793 ch->ccn.channel_of_client, ch->cadet);
799 /******************************************************************************/
800 /*********************** RECEIVE HANDLERS ****************************/
801 /******************************************************************************/
805 * Call the @a notify callback given to #GNUNET_CADET_notify_transmit_ready to
806 * request the data to send over MQ. Since MQ manages the queue, this function
807 * is scheduled immediatly after a transmit ready notification.
809 * @param cls Closure (transmit handle).
812 request_data (void *cls)
814 struct GNUNET_CADET_TransmitHandle *th = cls;
815 struct GNUNET_CADET_LocalData *msg;
816 struct GNUNET_MQ_Envelope *env;
819 LOG (GNUNET_ERROR_TYPE_DEBUG,
820 "Requesting Data: %u bytes (allow send is %u)\n",
822 th->channel->allow_send);
824 GNUNET_assert (0 < th->channel->allow_send);
825 th->channel->allow_send--;
826 /* NOTE: we may be allowed to send another packet immediately,
827 albeit the current logic waits for the ACK. */
828 th->request_data_task = NULL;
829 remove_from_queue (th);
831 env = GNUNET_MQ_msg_extra (msg,
833 GNUNET_MESSAGE_TYPE_CADET_LOCAL_DATA);
834 msg->ccn = th->channel->ccn;
835 osize = th->notify (th->notify_cls,
838 GNUNET_assert (osize == th->size);
840 GNUNET_MQ_send (th->channel->cadet->mq,
847 * Process the new channel notification and add it to the channels in the handle
849 * @param h The cadet handle
850 * @param msg A message with the details of the new incoming channel
853 handle_channel_created (void *cls,
854 const struct GNUNET_CADET_LocalChannelCreateMessage *msg)
856 struct GNUNET_CADET_Handle *h = cls;
857 struct GNUNET_CADET_Channel *ch;
858 struct GNUNET_CADET_Port *port;
859 const struct GNUNET_HashCode *port_number;
860 struct GNUNET_CADET_ClientChannelNumber ccn;
863 port_number = &msg->port;
864 if (ntohl (ccn.channel_of_client) >= GNUNET_CADET_LOCAL_CHANNEL_ID_CLI)
869 port = find_port (h, port_number);
872 /* We could have closed the port but the service didn't know about it yet
873 * This is not an error.
875 struct GNUNET_CADET_LocalChannelDestroyMessage *d_msg;
876 struct GNUNET_MQ_Envelope *env;
879 LOG (GNUNET_ERROR_TYPE_DEBUG,
880 "No handler for incoming channel %X (on port %s, recently closed?)\n",
881 ntohl (ccn.channel_of_client),
882 GNUNET_h2s (port_number));
883 env = GNUNET_MQ_msg (d_msg,
884 GNUNET_MESSAGE_TYPE_CADET_LOCAL_CHANNEL_DESTROY);
885 d_msg->ccn = msg->ccn;
886 GNUNET_MQ_send (h->mq,
891 ch = create_channel (h,
893 ch->peer = GNUNET_PEER_intern (&msg->peer);
896 ch->incoming_port = port;
897 ch->options = ntohl (msg->opt);
898 LOG (GNUNET_ERROR_TYPE_DEBUG,
899 "Creating incoming channel %X [%s] %p\n",
900 ntohl (ccn.channel_of_client),
901 GNUNET_h2s (port_number),
904 if (NULL != port->handler)
908 ch->ctx = port->handler (port->cls,
917 GNUNET_assert (NULL != port->connects);
918 ch->window_changes = port->window_changes;
919 ch->disconnects = port->disconnects;
920 ch->mq = GNUNET_MQ_queue_for_callbacks (&cadet_mq_send_impl,
921 &cadet_mq_destroy_impl,
922 &cadet_mq_cancel_impl,
925 &cadet_mq_error_handler,
927 ch->ctx = port->connects (port->cls,
930 GNUNET_MQ_set_handlers_closure (ch->mq, ch->ctx);
936 * Process the channel destroy notification and free associated resources
938 * @param h The cadet handle
939 * @param msg A message with the details of the channel being destroyed
942 handle_channel_destroy (void *cls,
943 const struct GNUNET_CADET_LocalChannelDestroyMessage *msg)
945 struct GNUNET_CADET_Handle *h = cls;
946 struct GNUNET_CADET_Channel *ch;
947 struct GNUNET_CADET_ClientChannelNumber ccn;
950 LOG (GNUNET_ERROR_TYPE_DEBUG,
951 "Channel %X Destroy from service\n",
952 ntohl (ccn.channel_of_client));
953 ch = retrieve_channel (h,
958 LOG (GNUNET_ERROR_TYPE_DEBUG,
959 "channel %X unknown\n",
960 ntohl (ccn.channel_of_client));
963 destroy_channel (ch);
968 * Check that message received from CADET service is well-formed.
970 * @param cls the `struct GNUNET_CADET_Handle`
971 * @param message the message we got
972 * @return #GNUNET_OK if the message is well-formed,
973 * #GNUNET_SYSERR otherwise
976 check_local_data (void *cls,
977 const struct GNUNET_CADET_LocalData *message)
979 struct GNUNET_CADET_Handle *h = cls;
980 struct GNUNET_CADET_Channel *ch;
983 size = ntohs (message->header.size);
984 if (sizeof (*message) + sizeof (struct GNUNET_MessageHeader) > size)
987 return GNUNET_SYSERR;
990 ch = retrieve_channel (h,
995 return GNUNET_SYSERR;
1003 * Process the incoming data packets, call appropriate handlers.
1005 * @param h The cadet handle
1006 * @param message A message encapsulating the data
1009 handle_local_data (void *cls,
1010 const struct GNUNET_CADET_LocalData *message)
1012 struct GNUNET_CADET_Handle *h = cls;
1013 const struct GNUNET_MessageHeader *payload;
1014 const struct GNUNET_CADET_MessageHandler *handler;
1015 struct GNUNET_CADET_Channel *ch;
1019 ch = retrieve_channel (h,
1023 GNUNET_break_op (0);
1028 payload = (struct GNUNET_MessageHeader *) &message[1];
1029 type = ntohs (payload->type);
1030 fwd = ntohl (ch->ccn.channel_of_client) <= GNUNET_CADET_LOCAL_CHANNEL_ID_CLI;
1031 LOG (GNUNET_ERROR_TYPE_DEBUG,
1032 "Got a %s data on channel %s [%X] of type %s (%u)\n",
1034 GNUNET_i2s (GNUNET_PEER_resolve2 (ch->peer)),
1035 ntohl (message->ccn.channel_of_client),
1040 LOG (GNUNET_ERROR_TYPE_DEBUG,
1041 "injecting msg %s into mq %p\n",
1042 GC_m2s (ntohs (payload->type)),
1044 GNUNET_MQ_inject_message (ch->mq, payload);
1047 /** @a deprecated */
1048 for (unsigned i=0;i<h->n_handlers;i++)
1050 handler = &h->message_handlers[i];
1051 if (handler->type == type)
1054 handler->callback (h->cls,
1059 LOG (GNUNET_ERROR_TYPE_DEBUG,
1060 "callback caused disconnection\n");
1061 GNUNET_CADET_channel_destroy (ch);
1067 /* Other peer sent message we do not comprehend. */
1068 GNUNET_break_op (0);
1069 GNUNET_CADET_receive_done (ch);
1074 * Process a local ACK message, enabling the client to send
1075 * more data to the service.
1077 * @param h Cadet handle.
1078 * @param message Message itself.
1081 handle_local_ack (void *cls,
1082 const struct GNUNET_CADET_LocalAck *message)
1084 struct GNUNET_CADET_Handle *h = cls;
1085 struct GNUNET_CADET_Channel *ch;
1086 struct GNUNET_CADET_ClientChannelNumber ccn;
1087 struct GNUNET_CADET_TransmitHandle *th;
1090 ch = retrieve_channel (h, ccn);
1093 LOG (GNUNET_ERROR_TYPE_DEBUG,
1094 "ACK on unknown channel %X\n",
1095 ntohl (ccn.channel_of_client));
1101 if (NULL == ch->pending_env)
1103 LOG (GNUNET_ERROR_TYPE_DEBUG,
1104 "Got an ACK on mq channel %X, allow send now %u!\n",
1105 ntohl (ch->ccn.channel_of_client),
1107 notify_window_size (ch);
1111 LOG (GNUNET_ERROR_TYPE_DEBUG,
1112 "Got an ACK on mq channel %X, sending pending message!\n",
1113 ntohl (ch->ccn.channel_of_client));
1114 GNUNET_MQ_send (h->mq, ch->pending_env);
1116 ch->pending_env = NULL;
1117 ch->mq_cont = GNUNET_SCHEDULER_add_now (&cadet_mq_send_continue, ch);
1124 for (th = h->th_head; NULL != th; th = th->next)
1126 if ( (th->channel == ch) &&
1127 (NULL == th->request_data_task) )
1129 th->request_data_task
1130 = GNUNET_SCHEDULER_add_now (&request_data,
1139 * Generic error handler, called with the appropriate error code and
1140 * the same closure specified at the creation of the message queue.
1141 * Not every message queue implementation supports an error handler.
1143 * @param cls closure, a `struct GNUNET_CORE_Handle *`
1144 * @param error error code
1147 handle_mq_error (void *cls,
1148 enum GNUNET_MQ_Error error)
1150 struct GNUNET_CADET_Handle *h = cls;
1152 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MQ ERROR: %u\n", error);
1153 GNUNET_MQ_destroy (h->mq);
1160 * Process a local reply about info on all channels, pass info to the user.
1162 * @param h Cadet handle.
1163 * @param message Message itself.
1166 // process_get_channels (struct GNUNET_CADET_Handle *h,
1167 // const struct GNUNET_MessageHeader *message)
1169 // struct GNUNET_CADET_LocalInfo *msg;
1171 // GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Get Channels messasge received\n");
1173 // if (NULL == h->channels_cb)
1175 // GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " ignored\n");
1179 // msg = (struct GNUNET_CADET_LocalInfo *) message;
1180 // if (ntohs (message->size) !=
1181 // (sizeof (struct GNUNET_CADET_LocalInfo) +
1182 // sizeof (struct GNUNET_PeerIdentity)))
1184 // GNUNET_break_op (0);
1185 // GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1186 // "Get channels message: size %hu - expected %u\n",
1187 // ntohs (message->size),
1188 // sizeof (struct GNUNET_CADET_LocalInfo));
1191 // h->channels_cb (h->channels_cls,
1192 // ntohl (msg->channel_id),
1194 // &msg->destination);
1200 * Process a local monitor_channel reply, pass info to the user.
1202 * @param h Cadet handle.
1203 * @param message Message itself.
1206 // process_show_channel (struct GNUNET_CADET_Handle *h,
1207 // const struct GNUNET_MessageHeader *message)
1209 // struct GNUNET_CADET_LocalInfo *msg;
1212 // GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Show Channel messasge received\n");
1214 // if (NULL == h->channel_cb)
1216 // GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " ignored\n");
1220 // /* Verify message sanity */
1221 // msg = (struct GNUNET_CADET_LocalInfo *) message;
1222 // esize = sizeof (struct GNUNET_CADET_LocalInfo);
1223 // if (ntohs (message->size) != esize)
1225 // GNUNET_break_op (0);
1226 // GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1227 // "Show channel message: size %hu - expected %u\n",
1228 // ntohs (message->size),
1231 // h->channel_cb (h->channel_cls, NULL, NULL);
1232 // h->channel_cb = NULL;
1233 // h->channel_cls = NULL;
1238 // h->channel_cb (h->channel_cls,
1239 // &msg->destination,
1246 * Check that message received from CADET service is well-formed.
1248 * @param cls the `struct GNUNET_CADET_Handle`
1249 * @param message the message we got
1250 * @return #GNUNET_OK if the message is well-formed,
1251 * #GNUNET_SYSERR otherwise
1254 check_get_peers (void *cls,
1255 const struct GNUNET_CADET_LocalInfoPeer *message)
1257 struct GNUNET_CADET_Handle *h = cls;
1260 if (NULL == h->info_cb.peers_cb)
1262 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1263 " no handler for peesr monitor message!\n");
1264 return GNUNET_SYSERR;
1267 size = ntohs (message->header.size);
1268 if (sizeof (struct GNUNET_CADET_LocalInfoPeer) > size)
1270 h->info_cb.peers_cb (h->info_cls, NULL, -1, 0, 0);
1271 h->info_cb.peers_cb = NULL;
1273 return GNUNET_SYSERR;
1281 * Process a local reply about info on all tunnels, pass info to the user.
1283 * @param cls Closure (Cadet handle).
1284 * @param msg Message itself.
1287 handle_get_peers (void *cls,
1288 const struct GNUNET_CADET_LocalInfoPeer *msg)
1290 struct GNUNET_CADET_Handle *h = cls;
1291 h->info_cb.peers_cb (h->info_cls, &msg->destination,
1292 (int) ntohs (msg->tunnel),
1293 (unsigned int ) ntohs (msg->paths),
1299 * Check that message received from CADET service is well-formed.
1301 * @param cls the `struct GNUNET_CADET_Handle`
1302 * @param message the message we got
1303 * @return #GNUNET_OK if the message is well-formed,
1304 * #GNUNET_SYSERR otherwise
1307 check_get_peer (void *cls,
1308 const struct GNUNET_CADET_LocalInfoPeer *message)
1310 struct GNUNET_CADET_Handle *h = cls;
1311 const size_t msize = sizeof (struct GNUNET_CADET_LocalInfoPeer);
1312 struct GNUNET_PeerIdentity *paths_array;
1314 unsigned int epaths;
1318 if (NULL == h->info_cb.peer_cb)
1320 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1321 " no handler for peer monitor message!\n");
1325 /* Verify message sanity */
1326 esize = ntohs (message->header.size);
1329 GNUNET_break_op (0);
1330 h->info_cb.peer_cb (h->info_cls, NULL, 0, 0, 0, NULL);
1333 if (0 != ((esize - msize) % sizeof (struct GNUNET_PeerIdentity)))
1335 GNUNET_break_op (0);
1336 h->info_cb.peer_cb (h->info_cls, NULL, 0, 0, 0, NULL);
1340 peers = (esize - msize) / sizeof (struct GNUNET_PeerIdentity);
1341 epaths = (unsigned int) ntohs (message->paths);
1342 paths_array = (struct GNUNET_PeerIdentity *) &message[1];
1344 for (int i = 0; i < peers; i++)
1346 if (0 == memcmp (&paths_array[i], &message->destination,
1347 sizeof (struct GNUNET_PeerIdentity)))
1352 if (paths != epaths)
1354 GNUNET_break_op (0);
1355 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "p:%u, e: %u\n", paths, epaths);
1356 h->info_cb.peer_cb (h->info_cls, NULL, 0, 0, 0, NULL);
1363 h->info_cb.peer_cb = NULL;
1365 return GNUNET_SYSERR;
1370 * Process a local peer info reply, pass info to the user.
1372 * @param cls Closure (Cadet handle).
1373 * @param message Message itself.
1376 handle_get_peer (void *cls,
1377 const struct GNUNET_CADET_LocalInfoPeer *message)
1379 struct GNUNET_CADET_Handle *h = cls;
1380 struct GNUNET_PeerIdentity *paths_array;
1382 unsigned int path_length;
1386 paths = (unsigned int) ntohs (message->paths);
1387 paths_array = (struct GNUNET_PeerIdentity *) &message[1];
1388 peers = (ntohs (message->header.size) - sizeof (*message))
1389 / sizeof (struct GNUNET_PeerIdentity);
1391 neighbor = GNUNET_NO;
1393 for (int i = 0; i < peers; i++)
1395 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " %s\n", GNUNET_i2s (&paths_array[i]));
1397 if (0 == memcmp (&paths_array[i], &message->destination,
1398 sizeof (struct GNUNET_PeerIdentity)))
1400 if (1 == path_length)
1401 neighbor = GNUNET_YES;
1406 /* Call Callback with tunnel info. */
1407 paths_array = (struct GNUNET_PeerIdentity *) &message[1];
1408 h->info_cb.peer_cb (h->info_cls,
1409 &message->destination,
1410 (int) ntohs (message->tunnel),
1418 * Check that message received from CADET service is well-formed.
1420 * @param cls the `struct GNUNET_CADET_Handle`
1421 * @param msg the message we got
1422 * @return #GNUNET_OK if the message is well-formed,
1423 * #GNUNET_SYSERR otherwise
1426 check_get_tunnels (void *cls,
1427 const struct GNUNET_CADET_LocalInfoTunnel *msg)
1429 struct GNUNET_CADET_Handle *h = cls;
1432 if (NULL == h->info_cb.tunnels_cb)
1434 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1435 " no handler for tunnels monitor message!\n");
1436 return GNUNET_SYSERR;
1439 size = ntohs (msg->header.size);
1440 if (sizeof (struct GNUNET_CADET_LocalInfoTunnel) > size)
1442 h->info_cb.tunnels_cb (h->info_cls, NULL, 0, 0, 0, 0);
1443 h->info_cb.tunnels_cb = NULL;
1445 return GNUNET_SYSERR;
1452 * Process a local reply about info on all tunnels, pass info to the user.
1454 * @param cls Closure (Cadet handle).
1455 * @param message Message itself.
1458 handle_get_tunnels (void *cls,
1459 const struct GNUNET_CADET_LocalInfoTunnel *msg)
1461 struct GNUNET_CADET_Handle *h = cls;
1463 h->info_cb.tunnels_cb (h->info_cls,
1465 ntohl (msg->channels),
1466 ntohl (msg->connections),
1467 ntohs (msg->estate),
1468 ntohs (msg->cstate));
1474 * Check that message received from CADET service is well-formed.
1476 * @param cls the `struct GNUNET_CADET_Handle`
1477 * @param msg the message we got
1478 * @return #GNUNET_OK if the message is well-formed,
1479 * #GNUNET_SYSERR otherwise
1482 check_get_tunnel (void *cls,
1483 const struct GNUNET_CADET_LocalInfoTunnel *msg)
1485 struct GNUNET_CADET_Handle *h = cls;
1491 if (NULL == h->info_cb.tunnel_cb)
1493 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1494 " no handler for tunnel monitor message!\n");
1498 /* Verify message sanity */
1499 msize = ntohs (msg->header.size);
1500 esize = sizeof (struct GNUNET_CADET_LocalInfoTunnel);
1503 GNUNET_break_op (0);
1504 h->info_cb.tunnel_cb (h->info_cls,
1505 NULL, 0, 0, NULL, NULL, 0, 0);
1508 ch_n = ntohl (msg->channels);
1509 c_n = ntohl (msg->connections);
1510 esize += ch_n * sizeof (struct GNUNET_CADET_ChannelTunnelNumber);
1511 esize += c_n * sizeof (struct GNUNET_CADET_ConnectionTunnelIdentifier);
1514 GNUNET_break_op (0);
1515 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1516 "m:%u, e: %u (%u ch, %u conn)\n",
1517 (unsigned int) msize,
1518 (unsigned int) esize,
1521 h->info_cb.tunnel_cb (h->info_cls,
1522 NULL, 0, 0, NULL, NULL, 0, 0);
1529 h->info_cb.tunnel_cb = NULL;
1531 return GNUNET_SYSERR;
1536 * Process a local tunnel info reply, pass info to the user.
1538 * @param cls Closure (Cadet handle).
1539 * @param msg Message itself.
1542 handle_get_tunnel (void *cls,
1543 const struct GNUNET_CADET_LocalInfoTunnel *msg)
1545 struct GNUNET_CADET_Handle *h = cls;
1548 const struct GNUNET_CADET_ConnectionTunnelIdentifier *conns;
1549 const struct GNUNET_CADET_ChannelTunnelNumber *chns;
1551 ch_n = ntohl (msg->channels);
1552 c_n = ntohl (msg->connections);
1554 /* Call Callback with tunnel info. */
1555 conns = (const struct GNUNET_CADET_ConnectionTunnelIdentifier *) &msg[1];
1556 chns = (const struct GNUNET_CADET_ChannelTunnelNumber *) &conns[c_n];
1557 h->info_cb.tunnel_cb (h->info_cls,
1563 ntohs (msg->estate),
1564 ntohs (msg->cstate));
1569 * Reconnect to the service, retransmit all infomation to try to restore the
1572 * @param h handle to the cadet
1575 reconnect (struct GNUNET_CADET_Handle *h)
1577 struct GNUNET_MQ_MessageHandler handlers[] = {
1578 GNUNET_MQ_hd_fixed_size (channel_created,
1579 GNUNET_MESSAGE_TYPE_CADET_LOCAL_CHANNEL_CREATE,
1580 struct GNUNET_CADET_LocalChannelCreateMessage,
1582 GNUNET_MQ_hd_fixed_size (channel_destroy,
1583 GNUNET_MESSAGE_TYPE_CADET_LOCAL_CHANNEL_DESTROY,
1584 struct GNUNET_CADET_LocalChannelDestroyMessage,
1586 GNUNET_MQ_hd_var_size (local_data,
1587 GNUNET_MESSAGE_TYPE_CADET_LOCAL_DATA,
1588 struct GNUNET_CADET_LocalData,
1590 GNUNET_MQ_hd_fixed_size (local_ack,
1591 GNUNET_MESSAGE_TYPE_CADET_LOCAL_ACK,
1592 struct GNUNET_CADET_LocalAck,
1594 GNUNET_MQ_hd_var_size (get_peers,
1595 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEERS,
1596 struct GNUNET_CADET_LocalInfoPeer,
1598 GNUNET_MQ_hd_var_size (get_peer,
1599 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEER,
1600 struct GNUNET_CADET_LocalInfoPeer,
1602 GNUNET_MQ_hd_var_size (get_tunnels,
1603 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNELS,
1604 struct GNUNET_CADET_LocalInfoTunnel,
1606 GNUNET_MQ_hd_var_size (get_tunnel,
1607 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNEL,
1608 struct GNUNET_CADET_LocalInfoTunnel,
1611 // GNUNET_MQ_hd_fixed_Y size (channel_destroyed,
1612 // GNUNET_MESSAGE_TYPE_CADET_CHANNEL_OPEN_NACK_DEPRECATED,
1613 // struct GNUNET_CADET_ChannelDestroyMessage);
1614 GNUNET_MQ_handler_end ()
1616 struct GNUNET_CADET_Channel *ch;
1618 while (NULL != (ch = h->channels_head))
1620 LOG (GNUNET_ERROR_TYPE_DEBUG,
1621 "Destroying channel due to a reconnect\n");
1622 destroy_channel (ch);
1625 LOG (GNUNET_ERROR_TYPE_DEBUG, "Connecting to CADET\n");
1629 GNUNET_MQ_destroy (h->mq);
1632 h->mq = GNUNET_CLIENT_connect (h->cfg,
1639 schedule_reconnect (h);
1644 h->reconnect_time = GNUNET_TIME_UNIT_MILLISECONDS;
1649 * Reconnect callback: tries to reconnect again after a failer previous
1652 * @param cls closure (cadet handle)
1655 reconnect_cbk (void *cls)
1657 struct GNUNET_CADET_Handle *h = cls;
1659 h->reconnect_task = NULL;
1665 * Reconnect to the service, retransmit all infomation to try to restore the
1668 * @param h handle to the cadet
1670 * @return #GNUNET_YES in case of sucess, #GNUNET_NO otherwise (service down...)
1673 schedule_reconnect (struct GNUNET_CADET_Handle *h)
1675 if (NULL == h->reconnect_task)
1677 h->reconnect_task = GNUNET_SCHEDULER_add_delayed (h->reconnect_time,
1679 h->reconnect_time = GNUNET_TIME_STD_BACKOFF (h->reconnect_time);
1684 /******************************************************************************/
1685 /********************** API CALL DEFINITIONS *************************/
1686 /******************************************************************************/
1688 struct GNUNET_CADET_Handle *
1689 GNUNET_CADET_connect (const struct GNUNET_CONFIGURATION_Handle *cfg,
1691 GNUNET_CADET_ChannelEndHandler cleaner,
1692 const struct GNUNET_CADET_MessageHandler *handlers)
1694 struct GNUNET_CADET_Handle *h;
1696 h = GNUNET_new (struct GNUNET_CADET_Handle);
1697 LOG (GNUNET_ERROR_TYPE_DEBUG,
1698 "GNUNET_CADET_connect() %p\n",
1701 h->cleaner = cleaner;
1702 h->ports = GNUNET_CONTAINER_multihashmap_create (4, GNUNET_YES);
1707 GNUNET_CADET_disconnect (h);
1711 h->message_handlers = handlers;
1712 h->next_ccn.channel_of_client = htonl (GNUNET_CADET_LOCAL_CHANNEL_ID_CLI);
1713 h->reconnect_time = GNUNET_TIME_UNIT_MILLISECONDS;
1714 h->reconnect_task = NULL;
1716 /* count handlers */
1717 for (h->n_handlers = 0;
1718 handlers && handlers[h->n_handlers].type;
1725 * Disconnect from the cadet service. All channels will be destroyed. All channel
1726 * disconnect callbacks will be called on any still connected peers, notifying
1727 * about their disconnection. The registered inbound channel cleaner will be
1728 * called should any inbound channels still exist.
1730 * @param handle connection to cadet to disconnect
1733 GNUNET_CADET_disconnect (struct GNUNET_CADET_Handle *handle)
1735 struct GNUNET_CADET_Channel *ch;
1736 struct GNUNET_CADET_Channel *aux;
1737 struct GNUNET_CADET_TransmitHandle *th;
1739 LOG (GNUNET_ERROR_TYPE_DEBUG,
1740 "CADET DISCONNECT\n");
1741 ch = handle->channels_head;
1745 if (ntohl (ch->ccn.channel_of_client) >= GNUNET_CADET_LOCAL_CHANNEL_ID_CLI)
1748 LOG (GNUNET_ERROR_TYPE_DEBUG,
1749 "channel %X not destroyed\n",
1750 ntohl (ch->ccn.channel_of_client));
1752 destroy_channel (ch);
1755 while (NULL != (th = handle->th_head))
1757 struct GNUNET_MessageHeader *msg;
1759 /* Make sure it is an allowed packet (everything else should have been
1760 * already canceled).
1762 GNUNET_break (GNUNET_NO == th_is_payload (th));
1763 msg = (struct GNUNET_MessageHeader *) &th[1];
1764 switch (ntohs(msg->type))
1766 case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_OPEN:
1767 case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DESTROY:
1768 case GNUNET_MESSAGE_TYPE_CADET_LOCAL_PORT_OPEN:
1769 case GNUNET_MESSAGE_TYPE_CADET_LOCAL_PORT_CLOSE:
1770 case GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_CHANNELS:
1771 case GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_CHANNEL:
1772 case GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEER:
1773 case GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEERS:
1774 case GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNEL:
1775 case GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNELS:
1779 LOG (GNUNET_ERROR_TYPE_ERROR, "unexpected unsent msg %s\n",
1780 GC_m2s (ntohs(msg->type)));
1783 GNUNET_CADET_notify_transmit_ready_cancel (th);
1786 if (NULL != handle->mq)
1788 GNUNET_MQ_destroy (handle->mq);
1791 if (NULL != handle->reconnect_task)
1793 GNUNET_SCHEDULER_cancel(handle->reconnect_task);
1794 handle->reconnect_task = NULL;
1797 GNUNET_CONTAINER_multihashmap_destroy (handle->ports);
1798 handle->ports = NULL;
1799 GNUNET_free (handle);
1804 * Open a port to receive incomming channels.
1806 * @param h CADET handle.
1807 * @param port Hash representing the port number.
1808 * @param new_channel Function called when an channel is received.
1809 * @param new_channel_cls Closure for @a new_channel.
1810 * @return Port handle.
1812 struct GNUNET_CADET_Port *
1813 GNUNET_CADET_open_port (struct GNUNET_CADET_Handle *h,
1814 const struct GNUNET_HashCode *port,
1815 GNUNET_CADET_InboundChannelNotificationHandler
1817 void *new_channel_cls)
1819 struct GNUNET_CADET_PortMessage *msg;
1820 struct GNUNET_MQ_Envelope *env;
1821 struct GNUNET_CADET_Port *p;
1823 GNUNET_assert (NULL != new_channel);
1824 p = GNUNET_new (struct GNUNET_CADET_Port);
1826 p->hash = GNUNET_new (struct GNUNET_HashCode);
1828 p->handler = new_channel;
1829 p->cls = new_channel_cls;
1830 GNUNET_assert (GNUNET_OK ==
1831 GNUNET_CONTAINER_multihashmap_put (h->ports,
1834 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1836 env = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_CADET_LOCAL_PORT_OPEN);
1837 msg->port = *p->hash;
1838 GNUNET_MQ_send (h->mq, env);
1844 * Close a port opened with @a GNUNET_CADET_open_port.
1845 * The @a new_channel callback will no longer be called.
1847 * @param p Port handle.
1850 GNUNET_CADET_close_port (struct GNUNET_CADET_Port *p)
1852 struct GNUNET_CADET_PortMessage *msg;
1853 struct GNUNET_MQ_Envelope *env;
1854 struct GNUNET_HashCode *id;
1856 env = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_CADET_LOCAL_PORT_CLOSE);
1858 id = NULL != p->hash ? p->hash : &p->id;
1860 GNUNET_MQ_send (p->cadet->mq, env);
1861 GNUNET_CONTAINER_multihashmap_remove (p->cadet->ports, id, p);
1862 GNUNET_free_non_null (p->hash);
1868 * Create a new channel towards a remote peer.
1870 * If the destination port is not open by any peer or the destination peer
1871 * does not accept the channel, #GNUNET_CADET_ChannelEndHandler will be called
1874 * @param h cadet handle
1875 * @param channel_ctx client's channel context to associate with the channel
1876 * @param peer peer identity the channel should go to
1877 * @param port Port hash (port number).
1878 * @param options CadetOption flag field, with all desired option bits set to 1.
1879 * @return handle to the channel
1881 struct GNUNET_CADET_Channel *
1882 GNUNET_CADET_channel_create (struct GNUNET_CADET_Handle *h,
1884 const struct GNUNET_PeerIdentity *peer,
1885 const struct GNUNET_HashCode *port,
1886 enum GNUNET_CADET_ChannelOption options)
1888 struct GNUNET_CADET_LocalChannelCreateMessage *msg;
1889 struct GNUNET_MQ_Envelope *env;
1890 struct GNUNET_CADET_Channel *ch;
1891 struct GNUNET_CADET_ClientChannelNumber ccn;
1893 ccn.channel_of_client = htonl (0);
1894 ch = create_channel (h, ccn);
1895 ch->ctx = channel_ctx;
1896 ch->peer = GNUNET_PEER_intern (peer);
1898 LOG (GNUNET_ERROR_TYPE_DEBUG,
1899 "Creating new channel to %s:%u at %p number %X\n",
1903 ntohl (ch->ccn.channel_of_client));
1904 env = GNUNET_MQ_msg (msg,
1905 GNUNET_MESSAGE_TYPE_CADET_LOCAL_CHANNEL_CREATE);
1909 msg->opt = htonl (options);
1910 GNUNET_MQ_send (h->mq,
1917 GNUNET_CADET_channel_destroy (struct GNUNET_CADET_Channel *channel)
1919 struct GNUNET_CADET_Handle *h;
1920 struct GNUNET_CADET_LocalChannelDestroyMessage *msg;
1921 struct GNUNET_MQ_Envelope *env;
1922 struct GNUNET_CADET_TransmitHandle *th;
1923 struct GNUNET_CADET_TransmitHandle *next;
1925 LOG (GNUNET_ERROR_TYPE_DEBUG,
1926 "Destroying channel\n");
1928 for (th = h->th_head; th != NULL; th = next)
1931 if (th->channel == channel)
1934 if (GNUNET_YES == th_is_payload (th))
1936 /* applications should cancel before destroying channel */
1937 LOG (GNUNET_ERROR_TYPE_WARNING,
1938 "Channel destroyed without cancelling transmission requests\n");
1939 th->notify (th->notify_cls, 0, NULL);
1943 LOG (GNUNET_ERROR_TYPE_WARNING,
1944 "no meta-traffic should be queued\n");
1946 GNUNET_CONTAINER_DLL_remove (h->th_head,
1949 GNUNET_CADET_notify_transmit_ready_cancel (th);
1953 env = GNUNET_MQ_msg (msg,
1954 GNUNET_MESSAGE_TYPE_CADET_LOCAL_CHANNEL_DESTROY);
1955 msg->ccn = channel->ccn;
1956 GNUNET_MQ_send (h->mq,
1959 destroy_channel (channel);
1964 * Get information about a channel.
1966 * @param channel Channel handle.
1967 * @param option Query (GNUNET_CADET_OPTION_*).
1968 * @param ... dependant on option, currently not used
1970 * @return Union with an answer to the query.
1972 const union GNUNET_CADET_ChannelInfo *
1973 GNUNET_CADET_channel_get_info (struct GNUNET_CADET_Channel *channel,
1974 enum GNUNET_CADET_ChannelOption option, ...)
1976 static int bool_flag;
1977 const union GNUNET_CADET_ChannelInfo *ret;
1981 case GNUNET_CADET_OPTION_NOBUFFER:
1982 case GNUNET_CADET_OPTION_RELIABLE:
1983 case GNUNET_CADET_OPTION_OUT_OF_ORDER:
1984 if (0 != (option & channel->options))
1985 bool_flag = GNUNET_YES;
1987 bool_flag = GNUNET_NO;
1988 ret = (const union GNUNET_CADET_ChannelInfo *) &bool_flag;
1990 case GNUNET_CADET_OPTION_PEER:
1991 ret = (const union GNUNET_CADET_ChannelInfo *) GNUNET_PEER_resolve2 (channel->peer);
2002 struct GNUNET_CADET_TransmitHandle *
2003 GNUNET_CADET_notify_transmit_ready (struct GNUNET_CADET_Channel *channel,
2005 struct GNUNET_TIME_Relative maxdelay,
2007 GNUNET_CONNECTION_TransmitReadyNotify notify,
2010 struct GNUNET_CADET_TransmitHandle *th;
2012 GNUNET_assert (NULL != channel);
2013 GNUNET_assert (NULL != notify);
2014 GNUNET_assert (GNUNET_CONSTANTS_MAX_CADET_MESSAGE_SIZE >= notify_size);
2015 LOG (GNUNET_ERROR_TYPE_DEBUG,
2016 "CADET NOTIFY TRANSMIT READY on channel %X allow_send is %u to %s with %u bytes\n",
2017 ntohl (channel->ccn.channel_of_client),
2018 channel->allow_send,
2019 (ntohl (channel->ccn.channel_of_client) >=
2020 GNUNET_CADET_LOCAL_CHANNEL_ID_CLI)
2023 (unsigned int) notify_size);
2024 if (GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us != maxdelay.rel_value_us)
2026 LOG (GNUNET_ERROR_TYPE_WARNING,
2027 "CADET transmit ready timeout is deprected (has no effect)\n");
2030 th = GNUNET_new (struct GNUNET_CADET_TransmitHandle);
2031 th->channel = channel;
2032 th->size = notify_size;
2033 th->notify = notify;
2034 th->notify_cls = notify_cls;
2035 if (0 != channel->allow_send)
2036 th->request_data_task
2037 = GNUNET_SCHEDULER_add_now (&request_data,
2040 add_to_queue (channel->cadet,
2047 GNUNET_CADET_notify_transmit_ready_cancel (struct GNUNET_CADET_TransmitHandle *th)
2049 if (NULL != th->request_data_task)
2051 GNUNET_SCHEDULER_cancel (th->request_data_task);
2052 th->request_data_task = NULL;
2054 remove_from_queue (th);
2060 * Send an ack on the channel to confirm the processing of a message.
2062 * @param ch Channel on which to send the ACK.
2065 GNUNET_CADET_receive_done (struct GNUNET_CADET_Channel *channel)
2067 struct GNUNET_CADET_LocalAck *msg;
2068 struct GNUNET_MQ_Envelope *env;
2070 env = GNUNET_MQ_msg (msg,
2071 GNUNET_MESSAGE_TYPE_CADET_LOCAL_ACK);
2072 LOG (GNUNET_ERROR_TYPE_DEBUG,
2073 "Sending ACK on channel %X\n",
2074 ntohl (channel->ccn.channel_of_client));
2075 msg->ccn = channel->ccn;
2076 GNUNET_MQ_send (channel->cadet->mq,
2082 send_info_request (struct GNUNET_CADET_Handle *h, uint16_t type)
2084 struct GNUNET_MessageHeader *msg;
2085 struct GNUNET_MQ_Envelope *env;
2087 LOG (GNUNET_ERROR_TYPE_DEBUG,
2088 " Sending %s monitor message to service\n",
2091 env = GNUNET_MQ_msg (msg, type);
2092 GNUNET_MQ_send (h->mq, env);
2097 * Request a debug dump on the service's STDERR.
2099 * WARNING: unstable API, likely to change in the future!
2101 * @param h cadet handle
2104 GNUNET_CADET_request_dump (struct GNUNET_CADET_Handle *h)
2106 LOG (GNUNET_ERROR_TYPE_DEBUG, "requesting dump\n");
2107 send_info_request (h, GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_DUMP);
2112 * Request information about peers known to the running cadet service.
2113 * The callback will be called for every peer known to the service.
2114 * Only one info request (of any kind) can be active at once.
2117 * WARNING: unstable API, likely to change in the future!
2119 * @param h Handle to the cadet peer.
2120 * @param callback Function to call with the requested data.
2121 * @param callback_cls Closure for @c callback.
2123 * @return #GNUNET_OK / #GNUNET_SYSERR
2126 GNUNET_CADET_get_peers (struct GNUNET_CADET_Handle *h,
2127 GNUNET_CADET_PeersCB callback,
2130 if (NULL != h->info_cb.peers_cb)
2133 return GNUNET_SYSERR;
2135 send_info_request (h, GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEERS);
2136 h->info_cb.peers_cb = callback;
2137 h->info_cls = callback_cls;
2143 * Cancel a peer info request. The callback will not be called (anymore).
2145 * WARNING: unstable API, likely to change in the future!
2147 * @param h Cadet handle.
2149 * @return Closure given to GNUNET_CADET_get_peers.
2152 GNUNET_CADET_get_peers_cancel (struct GNUNET_CADET_Handle *h)
2157 h->info_cb.peers_cb = NULL;
2164 * Request information about a peer known to the running cadet peer.
2165 * The callback will be called for the tunnel once.
2166 * Only one info request (of any kind) can be active at once.
2168 * WARNING: unstable API, likely to change in the future!
2170 * @param h Handle to the cadet peer.
2171 * @param id Peer whose tunnel to examine.
2172 * @param callback Function to call with the requested data.
2173 * @param callback_cls Closure for @c callback.
2175 * @return #GNUNET_OK / #GNUNET_SYSERR
2178 GNUNET_CADET_get_peer (struct GNUNET_CADET_Handle *h,
2179 const struct GNUNET_PeerIdentity *id,
2180 GNUNET_CADET_PeerCB callback,
2183 struct GNUNET_CADET_LocalInfo *msg;
2184 struct GNUNET_MQ_Envelope *env;
2186 if (NULL != h->info_cb.peer_cb)
2189 return GNUNET_SYSERR;
2192 env = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEER);
2194 GNUNET_MQ_send (h->mq, env);
2196 h->info_cb.peer_cb = callback;
2197 h->info_cls = callback_cls;
2203 * Request information about tunnels of the running cadet peer.
2204 * The callback will be called for every tunnel of the service.
2205 * Only one info request (of any kind) can be active at once.
2207 * WARNING: unstable API, likely to change in the future!
2209 * @param h Handle to the cadet peer.
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_get_tunnels (struct GNUNET_CADET_Handle *h,
2217 GNUNET_CADET_TunnelsCB callback,
2220 if (NULL != h->info_cb.tunnels_cb)
2223 return GNUNET_SYSERR;
2225 send_info_request (h, GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNELS);
2226 h->info_cb.tunnels_cb = callback;
2227 h->info_cls = callback_cls;
2233 * Cancel a monitor request. The monitor callback will not be called.
2235 * @param h Cadet handle.
2237 * @return Closure given to GNUNET_CADET_get_tunnels.
2240 GNUNET_CADET_get_tunnels_cancel (struct GNUNET_CADET_Handle *h)
2244 h->info_cb.tunnels_cb = NULL;
2254 * Request information about a tunnel of the running cadet peer.
2255 * The callback will be called for the tunnel once.
2256 * Only one info request (of any kind) can be active at once.
2258 * WARNING: unstable API, likely to change in the future!
2260 * @param h Handle to the cadet peer.
2261 * @param id Peer whose tunnel to examine.
2262 * @param callback Function to call with the requested data.
2263 * @param callback_cls Closure for @c callback.
2265 * @return #GNUNET_OK / #GNUNET_SYSERR
2268 GNUNET_CADET_get_tunnel (struct GNUNET_CADET_Handle *h,
2269 const struct GNUNET_PeerIdentity *id,
2270 GNUNET_CADET_TunnelCB callback,
2273 struct GNUNET_CADET_LocalInfo *msg;
2274 struct GNUNET_MQ_Envelope *env;
2276 if (NULL != h->info_cb.tunnel_cb)
2279 return GNUNET_SYSERR;
2282 env = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNEL);
2284 GNUNET_MQ_send (h->mq, env);
2286 h->info_cb.tunnel_cb = callback;
2287 h->info_cls = callback_cls;
2293 * Request information about a specific channel of the running cadet peer.
2295 * WARNING: unstable API, likely to change in the future!
2296 * FIXME Add destination option.
2298 * @param h Handle to the cadet peer.
2299 * @param initiator ID of the owner of the channel.
2300 * @param channel_number Channel number.
2301 * @param callback Function to call with the requested data.
2302 * @param callback_cls Closure for @c callback.
2304 * @return #GNUNET_OK / #GNUNET_SYSERR
2307 GNUNET_CADET_show_channel (struct GNUNET_CADET_Handle *h,
2308 struct GNUNET_PeerIdentity *initiator,
2309 unsigned int channel_number,
2310 GNUNET_CADET_ChannelCB callback,
2313 struct GNUNET_CADET_LocalInfo *msg;
2314 struct GNUNET_MQ_Envelope *env;
2316 if (NULL != h->info_cb.channel_cb)
2319 return GNUNET_SYSERR;
2322 env = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_CHANNEL);
2323 msg->peer = *initiator;
2324 msg->ccn.channel_of_client = htonl (channel_number);
2325 GNUNET_MQ_send (h->mq, env);
2327 h->info_cb.channel_cb = callback;
2328 h->info_cls = callback_cls;
2334 * Function called to notify a client about the connection
2335 * begin ready to queue more data. "buf" will be
2336 * NULL and "size" zero if the connection was closed for
2337 * writing in the meantime.
2339 * @param cls closure
2340 * @param size number of bytes available in buf
2341 * @param buf where the callee should write the message
2342 * @return number of bytes written to buf
2345 cadet_mq_ntr (void *cls, size_t size,
2348 struct GNUNET_MQ_Handle *mq = cls;
2349 struct CadetMQState *state = GNUNET_MQ_impl_state (mq);
2350 const struct GNUNET_MessageHeader *msg = GNUNET_MQ_impl_current (mq);
2356 GNUNET_MQ_inject_error (mq, GNUNET_MQ_ERROR_WRITE);
2359 msize = ntohs (msg->size);
2360 GNUNET_assert (msize <= size);
2361 GNUNET_memcpy (buf, msg, msize);
2362 GNUNET_MQ_impl_send_continue (mq);
2368 * Signature of functions implementing the
2369 * sending functionality of a message queue.
2371 * @param mq the message queue
2372 * @param msg the message to send
2373 * @param impl_state state of the implementation
2376 cadet_mq_send_impl_old (struct GNUNET_MQ_Handle *mq,
2377 const struct GNUNET_MessageHeader *msg,
2380 struct CadetMQState *state = impl_state;
2382 GNUNET_assert (NULL == state->th);
2384 GNUNET_CADET_notify_transmit_ready (state->channel,
2385 /* FIXME: add option for corking */
2387 GNUNET_TIME_UNIT_FOREVER_REL,
2395 * Signature of functions implementing the
2396 * destruction of a message queue.
2397 * Implementations must not free 'mq', but should
2398 * take care of 'impl_state'.
2400 * @param mq the message queue to destroy
2401 * @param impl_state state of the implementation
2404 cadet_mq_destroy_impl_old (struct GNUNET_MQ_Handle *mq,
2407 struct CadetMQState *state = impl_state;
2409 if (NULL != state->th)
2410 GNUNET_CADET_notify_transmit_ready_cancel (state->th);
2412 GNUNET_free (state);
2417 * Create a message queue for a cadet channel.
2418 * The message queue can only be used to transmit messages,
2419 * not to receive them.
2421 * @param channel the channel to create the message qeue for
2422 * @return a message queue to messages over the channel
2424 struct GNUNET_MQ_Handle *
2425 GNUNET_CADET_mq_create (struct GNUNET_CADET_Channel *channel)
2427 struct GNUNET_MQ_Handle *mq;
2428 struct CadetMQState *state;
2430 state = GNUNET_new (struct CadetMQState);
2431 state->channel = channel;
2433 mq = GNUNET_MQ_queue_for_callbacks (&cadet_mq_send_impl_old,
2434 &cadet_mq_destroy_impl_old,
2435 NULL, /* FIXME: cancel impl. */
2437 NULL, /* no msg handlers */
2438 NULL, /* no err handlers */
2439 NULL); /* no handler cls */
2445 * Transitional function to convert an unsigned int port to a hash value.
2446 * WARNING: local static value returned, NOT reentrant!
2447 * WARNING: do not use this function for new code!
2449 * @param port Numerical port (unsigned int format).
2451 * @return A GNUNET_HashCode usable for the new CADET API.
2453 const struct GNUNET_HashCode *
2454 GC_u2h (uint32_t port)
2456 static struct GNUNET_HashCode hash;
2458 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2459 "This is a transitional function, "
2460 "use proper crypto hashes as CADET ports\n");
2461 GNUNET_CRYPTO_hash (&port, sizeof (port), &hash);
2468 /******************************************************************************/
2469 /******************************* MQ-BASED API *********************************/
2470 /******************************************************************************/
2473 * Connect to the MQ-based cadet service.
2475 * @param cfg Configuration to use.
2477 * @return Handle to the cadet service NULL on error.
2479 struct GNUNET_CADET_Handle *
2480 GNUNET_CADET_connecT (const struct GNUNET_CONFIGURATION_Handle *cfg)
2482 struct GNUNET_CADET_Handle *h;
2484 LOG (GNUNET_ERROR_TYPE_DEBUG,
2485 "GNUNET_CADET_connecT()\n");
2486 h = GNUNET_new (struct GNUNET_CADET_Handle);
2488 h->mq_api = GNUNET_YES;
2489 h->ports = GNUNET_CONTAINER_multihashmap_create (4, GNUNET_YES);
2494 GNUNET_CADET_disconnect (h);
2497 h->next_ccn.channel_of_client = htonl (GNUNET_CADET_LOCAL_CHANNEL_ID_CLI);
2498 h->reconnect_time = GNUNET_TIME_UNIT_MILLISECONDS;
2499 h->reconnect_task = NULL;
2506 * Open a port to receive incomming MQ-based channels.
2508 * @param h CADET handle.
2509 * @param port Hash identifying the port.
2510 * @param connects Function called when an incoming channel is connected.
2511 * @param connects_cls Closure for the @a connects handler.
2512 * @param window_changes Function called when the transmit window size changes.
2513 * @param disconnects Function called when a channel is disconnected.
2514 * @param handlers Callbacks for messages we care about, NULL-terminated.
2516 * @return Port handle.
2518 struct GNUNET_CADET_Port *
2519 GNUNET_CADET_open_porT (struct GNUNET_CADET_Handle *h,
2520 const struct GNUNET_HashCode *port,
2521 GNUNET_CADET_ConnectEventHandler connects,
2522 void * connects_cls,
2523 GNUNET_CADET_WindowSizeEventHandler window_changes,
2524 GNUNET_CADET_DisconnectEventHandler disconnects,
2525 const struct GNUNET_MQ_MessageHandler *handlers)
2527 struct GNUNET_CADET_PortMessage *msg;
2528 struct GNUNET_MQ_Envelope *env;
2529 struct GNUNET_CADET_Port *p;
2531 GNUNET_assert (NULL != connects);
2532 GNUNET_assert (NULL != disconnects);
2534 p = GNUNET_new (struct GNUNET_CADET_Port);
2537 p->connects = connects;
2538 p->cls = connects_cls;
2539 p->window_changes = window_changes;
2540 p->disconnects = disconnects;
2541 if (NULL != handlers)
2544 for (i=0;NULL != handlers[i].cb; i++) ;
2545 p->handlers = GNUNET_new_array (i + 1,
2546 struct GNUNET_MQ_MessageHandler);
2547 GNUNET_memcpy ((struct GNUNET_MQ_MessageHandler *) p->handlers,
2549 i * sizeof (struct GNUNET_MQ_MessageHandler));
2552 GNUNET_assert (GNUNET_OK ==
2553 GNUNET_CONTAINER_multihashmap_put (h->ports,
2556 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
2558 env = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_CADET_LOCAL_PORT_OPEN);
2560 GNUNET_MQ_send (h->mq, env);
2567 * Create a new channel towards a remote peer.
2569 * If the destination port is not open by any peer or the destination peer
2570 * does not accept the channel, #GNUNET_CADET_ChannelEndHandler will be called
2573 * @param h CADET handle.
2574 * @param channel_cls Closure for the channel. It's given to:
2575 * - The disconnect handler @a disconnects
2576 * - Each message type callback in @a handlers
2577 * @param destination Peer identity the channel should go to.
2578 * @param port Identification of the destination port.
2579 * @param options CadetOption flag field, with all desired option bits set to 1.
2580 * @param window_changes Function called when the transmit window size changes.
2581 * @param disconnects Function called when the channel is disconnected.
2582 * @param handlers Callbacks for messages we care about, NULL-terminated.
2584 * @return Handle to the channel.
2586 struct GNUNET_CADET_Channel *
2587 GNUNET_CADET_channel_creatE (struct GNUNET_CADET_Handle *h,
2589 const struct GNUNET_PeerIdentity *destination,
2590 const struct GNUNET_HashCode *port,
2591 enum GNUNET_CADET_ChannelOption options,
2592 GNUNET_CADET_WindowSizeEventHandler window_changes,
2593 GNUNET_CADET_DisconnectEventHandler disconnects,
2594 const struct GNUNET_MQ_MessageHandler *handlers)
2596 struct GNUNET_CADET_Channel *ch;
2597 struct GNUNET_CADET_ClientChannelNumber ccn;
2598 struct GNUNET_CADET_LocalChannelCreateMessage *msg;
2599 struct GNUNET_MQ_Envelope *env;
2601 GNUNET_assert (NULL != disconnects);
2603 /* Save parameters */
2604 ccn.channel_of_client = htonl (0);
2605 ch = create_channel (h, ccn);
2606 ch->ctx = channel_cls;
2607 ch->peer = GNUNET_PEER_intern (destination);
2608 ch->options = options;
2609 ch->window_changes = window_changes;
2610 ch->disconnects = disconnects;
2612 /* Create MQ for channel */
2613 ch->mq = GNUNET_MQ_queue_for_callbacks (&cadet_mq_send_impl,
2614 &cadet_mq_destroy_impl,
2615 &cadet_mq_cancel_impl,
2618 &cadet_mq_error_handler,
2620 GNUNET_MQ_set_handlers_closure (ch->mq, channel_cls);
2622 /* Request channel creation to service */
2623 env = GNUNET_MQ_msg (msg,
2624 GNUNET_MESSAGE_TYPE_CADET_LOCAL_CHANNEL_CREATE);
2627 msg->peer = *destination;
2628 msg->opt = htonl (options);
2629 GNUNET_MQ_send (h->mq,
2637 * Obtain the message queue for a connected peer.
2639 * @param channel The channel handle from which to get the MQ.
2641 * @return NULL if @a channel is not yet connected.
2643 struct GNUNET_MQ_Handle *
2644 GNUNET_CADET_get_mq (const struct GNUNET_CADET_Channel *channel)