2 This file is part of GNUnet.
3 Copyright (C) 2011 Christian Grothoff (and other contributing authors)
4 GNUnet is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published
6 by the Free Software Foundation; either version 3, or (at your
7 option) any later version.
8 GNUnet is distributed in the hope that it will be useful, but
9 WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 General Public License for more details.
12 You should have received a copy of the GNU General Public License
13 along with GNUnet; see the file COPYING. If not, write to the
14 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
15 Boston, MA 02110-1301, USA.
19 * @file cadet/cadet_api.c
20 * @brief cadet api: client implementation of new cadet service
21 * @author Bartlomiej Polot
25 #include "gnunet_util_lib.h"
26 #include "gnunet_cadet_service.h"
28 #include "cadet_protocol.h"
30 #define LOG(kind,...) GNUNET_log_from (kind, "cadet-api",__VA_ARGS__)
31 #define DATA_OVERHEAD sizeof(struct GNUNET_CADET_LocalData)
33 /******************************************************************************/
34 /************************ DATA STRUCTURES ****************************/
35 /******************************************************************************/
38 * Transmission queue to the service
40 struct GNUNET_CADET_TransmitHandle
46 struct GNUNET_CADET_TransmitHandle *next;
51 struct GNUNET_CADET_TransmitHandle *prev;
54 * Channel this message is sent on / for (may be NULL for control messages).
56 struct GNUNET_CADET_Channel *channel;
59 * Callback to obtain the message to transmit, or NULL if we
60 * got the message in 'data'. Notice that messages built
61 * by 'notify' need to be encapsulated with information about
64 GNUNET_CONNECTION_TransmitReadyNotify notify;
67 * Closure for 'notify'
72 * How long is this message valid. Once the timeout has been
73 * reached, the message must no longer be sent. If this
74 * is a message with a 'notify' callback set, the 'notify'
75 * function should be called with 'buf' NULL and size 0.
77 struct GNUNET_TIME_Absolute timeout;
80 * Task triggering a timeout, can be NO_TASK if the timeout is FOREVER.
82 struct GNUNET_SCHEDULER_Task * timeout_task;
85 * Size of 'data' -- or the desired size of 'notify' if 'data' is NULL.
95 GNUNET_CADET_ChannelCB channel_cb;
100 GNUNET_CADET_PeersCB peers_cb;
105 GNUNET_CADET_PeerCB peer_cb;
110 GNUNET_CADET_TunnelsCB tunnels_cb;
115 GNUNET_CADET_TunnelCB tunnel_cb;
120 * Opaque handle to the service.
122 struct GNUNET_CADET_Handle
126 * Handle to the server connection, to send messages later
128 struct GNUNET_CLIENT_Connection *client;
131 * Set of handlers used for processing incoming messages in the channels
133 const struct GNUNET_CADET_MessageHandler *message_handlers;
136 * Number of handlers in the handlers array.
138 unsigned int n_handlers;
143 const uint32_t *ports;
148 unsigned int n_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 creation
163 GNUNET_CADET_InboundChannelNotificationHandler *new_channel;
166 * Callback for inbound channel disconnection
168 GNUNET_CADET_ChannelEndHandler *cleaner;
171 * Handle to cancel pending transmissions in case of disconnection
173 struct GNUNET_CLIENT_TransmitHandle *th;
176 * Closure for all the handlers given by the client
181 * Messages to send to the service, head.
183 struct GNUNET_CADET_TransmitHandle *th_head;
186 * Messages to send to the service, tail.
188 struct GNUNET_CADET_TransmitHandle *th_tail;
191 * chid of the next channel to create (to avoid reusing IDs often)
193 CADET_ChannelNumber next_chid;
196 * Have we started the task to receive messages from the service
197 * yet? We do this after we send the 'CADET_LOCAL_CONNECT' message.
202 * Configuration given by the client, in case of reconnection
204 const struct GNUNET_CONFIGURATION_Handle *cfg;
207 * Time to the next reconnect in case one reconnect fails
209 struct GNUNET_TIME_Relative reconnect_time;
212 * Task for trying to reconnect.
214 struct GNUNET_SCHEDULER_Task * reconnect_task;
217 * Callback for an info task (only one active at a time).
219 union CadetInfoCB info_cb;
222 * Info callback closure for @c info_cb.
229 * Description of a peer
231 struct GNUNET_CADET_Peer
234 * ID of the peer in short form
239 * Channel this peer belongs to
241 struct GNUNET_CADET_Channel *t;
246 * Opaque handle to a channel.
248 struct GNUNET_CADET_Channel
254 struct GNUNET_CADET_Channel *next;
259 struct GNUNET_CADET_Channel *prev;
262 * Handle to the cadet this channel belongs to
264 struct GNUNET_CADET_Handle *cadet;
267 * Local ID of the channel
269 CADET_ChannelNumber chid;
277 * Other end of the channel.
282 * Any data the caller wants to put in here
287 * Size of packet queued in this channel
289 unsigned int packet_size;
292 * Channel options: reliability, etc.
294 enum GNUNET_CADET_ChannelOption options;
297 * Are we allowed to send to the service?
305 * Implementation state for cadet's message queue.
310 * The current transmit handle, or NULL
311 * if no transmit is active.
313 struct GNUNET_CADET_TransmitHandle *th;
316 * Channel to send the data over.
318 struct GNUNET_CADET_Channel *channel;
322 /******************************************************************************/
323 /*********************** DECLARATIONS *************************/
324 /******************************************************************************/
327 * Function called to send a message to the service.
328 * "buf" will be NULL and "size" zero if the socket was closed for writing in
331 * @param cls closure, the cadet handle
332 * @param size number of bytes available in buf
333 * @param buf where the callee should write the connect message
334 * @return number of bytes written to buf
337 send_callback (void *cls, size_t size, void *buf);
340 /******************************************************************************/
341 /*********************** AUXILIARY FUNCTIONS *************************/
342 /******************************************************************************/
345 * Check if transmission is a payload packet.
347 * @param th Transmission handle.
349 * @return #GNUNET_YES if it is a payload packet,
350 * #GNUNET_NO if it is a cadet management packet.
353 th_is_payload (struct GNUNET_CADET_TransmitHandle *th)
355 return (th->notify != NULL) ? GNUNET_YES : GNUNET_NO;
360 * Check whether there is any message ready in the queue and find the size.
362 * @param h Cadet handle.
364 * @return The size of the first ready message in the queue, including overhead.
365 * 0 if there is none.
368 message_ready_size (struct GNUNET_CADET_Handle *h)
370 struct GNUNET_CADET_TransmitHandle *th;
371 struct GNUNET_CADET_Channel *ch;
373 for (th = h->th_head; NULL != th; th = th->next)
376 if (GNUNET_NO == th_is_payload (th) || GNUNET_YES == ch->allow_send)
384 * Get the channel handler for the channel specified by id from the given handle
385 * @param h Cadet handle
386 * @param chid ID of the wanted channel
387 * @return handle to the required channel or NULL if not found
389 static struct GNUNET_CADET_Channel *
390 retrieve_channel (struct GNUNET_CADET_Handle *h, CADET_ChannelNumber chid)
392 struct GNUNET_CADET_Channel *ch;
394 ch = h->channels_head;
397 if (ch->chid == chid)
406 * Create a new channel and insert it in the channel list of the cadet handle
408 * @param h Cadet handle
409 * @param chid Desired chid of the channel, 0 to assign one automatically.
411 * @return Handle to the created channel.
413 static struct GNUNET_CADET_Channel *
414 create_channel (struct GNUNET_CADET_Handle *h, CADET_ChannelNumber chid)
416 struct GNUNET_CADET_Channel *ch;
418 ch = GNUNET_new (struct GNUNET_CADET_Channel);
419 GNUNET_CONTAINER_DLL_insert (h->channels_head, h->channels_tail, ch);
423 ch->chid = h->next_chid;
424 while (NULL != retrieve_channel (h, h->next_chid))
427 h->next_chid &= ~GNUNET_CADET_LOCAL_CHANNEL_ID_SERV;
428 h->next_chid |= GNUNET_CADET_LOCAL_CHANNEL_ID_CLI;
435 ch->allow_send = GNUNET_NO;
441 * Destroy the specified channel.
442 * - Destroys all peers, calling the disconnect callback on each if needed
443 * - Cancels all outgoing traffic for that channel, calling respective notifys
444 * - Calls cleaner if channel was inbound
445 * - Frees all memory used
447 * @param ch Pointer to the channel.
448 * @param call_cleaner Whether to call the cleaner handler.
450 * @return Handle to the required channel or NULL if not found.
453 destroy_channel (struct GNUNET_CADET_Channel *ch, int call_cleaner)
455 struct GNUNET_CADET_Handle *h;
456 struct GNUNET_CADET_TransmitHandle *th;
457 struct GNUNET_CADET_TransmitHandle *next;
459 LOG (GNUNET_ERROR_TYPE_DEBUG, " destroy_channel %X\n", ch->chid);
468 GNUNET_CONTAINER_DLL_remove (h->channels_head, h->channels_tail, ch);
470 /* signal channel destruction */
471 if ( (NULL != h->cleaner) && (0 != ch->peer) && (GNUNET_YES == call_cleaner) )
473 LOG (GNUNET_ERROR_TYPE_DEBUG, " calling cleaner\n");
474 h->cleaner (h->cls, ch, ch->ctx);
477 /* check that clients did not leave messages behind in the queue */
478 for (th = h->th_head; NULL != th; th = next)
481 if (th->channel != ch)
483 /* Clients should have aborted their requests already.
484 * Management traffic should be ok, as clients can't cancel that.
485 * If the service crashed and we are reconnecting, it's ok.
487 GNUNET_break (GNUNET_NO == th_is_payload (th)
488 || GNUNET_NO == h->in_receive);
489 GNUNET_CONTAINER_DLL_remove (h->th_head, h->th_tail, th);
491 /* clean up request */
492 if (NULL != th->timeout_task)
493 GNUNET_SCHEDULER_cancel (th->timeout_task);
497 /* if there are no more pending requests with cadet service, cancel active request */
498 /* Note: this should be unnecessary... */
499 if ((0 == message_ready_size (h)) && (NULL != h->th))
501 GNUNET_CLIENT_notify_transmit_ready_cancel (h->th);
506 GNUNET_PEER_change_rc (ch->peer, -1);
513 * Notify client that the transmission has timed out
516 * @param tc task context
519 timeout_transmission (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
521 struct GNUNET_CADET_TransmitHandle *th = cls;
522 struct GNUNET_CADET_Handle *cadet = th->channel->cadet;
524 th->timeout_task = NULL;
525 th->channel->packet_size = 0;
526 GNUNET_CONTAINER_DLL_remove (cadet->th_head, cadet->th_tail, th);
527 if (GNUNET_YES == th_is_payload (th))
528 GNUNET_break (0 == th->notify (th->notify_cls, 0, NULL));
530 if ((0 == message_ready_size (cadet)) && (NULL != cadet->th))
532 /* nothing ready to transmit, no point in asking for transmission */
533 GNUNET_CLIENT_notify_transmit_ready_cancel (cadet->th);
540 * Add a transmit handle to the transmission queue and set the
543 * @param h cadet handle with the queue head and tail
544 * @param th handle to the packet to be transmitted
547 add_to_queue (struct GNUNET_CADET_Handle *h,
548 struct GNUNET_CADET_TransmitHandle *th)
550 GNUNET_CONTAINER_DLL_insert_tail (h->th_head, h->th_tail, th);
551 if (GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us == th->timeout.abs_value_us)
554 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_absolute_get_remaining
555 (th->timeout), &timeout_transmission, th);
560 * Auxiliary function to send an already constructed packet to the service.
561 * Takes care of creating a new queue element, copying the message and
562 * calling the tmt_rdy function if necessary.
564 * @param h cadet handle
565 * @param msg message to transmit
566 * @param channel channel this send is related to (NULL if N/A)
569 send_packet (struct GNUNET_CADET_Handle *h,
570 const struct GNUNET_MessageHeader *msg,
571 struct GNUNET_CADET_Channel *channel);
575 * Send an ack on the channel to confirm the processing of a message.
577 * @param ch Channel on which to send the ACK.
580 send_ack (struct GNUNET_CADET_Channel *ch)
582 struct GNUNET_CADET_LocalAck msg;
584 LOG (GNUNET_ERROR_TYPE_DEBUG, "Sending ACK on channel %X\n", ch->chid);
585 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_LOCAL_ACK);
586 msg.header.size = htons (sizeof (msg));
587 msg.channel_id = htonl (ch->chid);
589 send_packet (ch->cadet, &msg.header, ch);
596 * Reconnect callback: tries to reconnect again after a failer previous
598 * @param cls closure (cadet handle)
599 * @param tc task context
602 reconnect_cbk (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
606 * Send a connect packet to the service with the applications and types
607 * requested by the user.
609 * @param h The cadet handle.
613 send_connect (struct GNUNET_CADET_Handle *h)
617 size = sizeof (struct GNUNET_CADET_ClientConnect);
618 size += h->n_ports * sizeof (uint32_t);
620 char buf[size] GNUNET_ALIGN;
621 struct GNUNET_CADET_ClientConnect *msg;
625 /* build connection packet */
626 msg = (struct GNUNET_CADET_ClientConnect *) buf;
627 msg->header.type = htons (GNUNET_MESSAGE_TYPE_CADET_LOCAL_CONNECT);
628 msg->header.size = htons (size);
629 ports = (uint32_t *) &msg[1];
630 for (i = 0; i < h->n_ports; i++)
632 ports[i] = htonl (h->ports[i]);
633 LOG (GNUNET_ERROR_TYPE_DEBUG, " port %u\n",
636 LOG (GNUNET_ERROR_TYPE_DEBUG,
637 "Sending %lu bytes long message with %u ports\n",
638 ntohs (msg->header.size), h->n_ports);
639 send_packet (h, &msg->header, NULL);
645 * Reconnect to the service, retransmit all infomation to try to restore the
648 * @param h handle to the cadet
650 * @return GNUNET_YES in case of sucess, GNUNET_NO otherwise (service down...)
653 do_reconnect (struct GNUNET_CADET_Handle *h)
655 LOG (GNUNET_ERROR_TYPE_DEBUG, "*****************************\n");
656 LOG (GNUNET_ERROR_TYPE_DEBUG, "******* RECONNECT *******\n");
657 LOG (GNUNET_ERROR_TYPE_DEBUG, "*****************************\n");
658 LOG (GNUNET_ERROR_TYPE_DEBUG, "******** on %p *******\n", h);
659 LOG (GNUNET_ERROR_TYPE_DEBUG, "*****************************\n");
664 GNUNET_CLIENT_notify_transmit_ready_cancel (h->th);
667 if (NULL != h->client)
669 GNUNET_CLIENT_disconnect (h->client);
673 h->client = GNUNET_CLIENT_connect ("cadet", h->cfg);
674 if (h->client == NULL)
676 h->reconnect_task = GNUNET_SCHEDULER_add_delayed (h->reconnect_time,
679 GNUNET_TIME_relative_min (GNUNET_TIME_UNIT_SECONDS,
680 GNUNET_TIME_relative_multiply
681 (h->reconnect_time, 2));
682 LOG (GNUNET_ERROR_TYPE_DEBUG, "Next retry in %s\n",
683 GNUNET_STRINGS_relative_time_to_string (h->reconnect_time,
690 h->reconnect_time = GNUNET_TIME_UNIT_MILLISECONDS;
697 * Reconnect callback: tries to reconnect again after a failer previous
699 * @param cls closure (cadet handle)
700 * @param tc task context
703 reconnect_cbk (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
705 struct GNUNET_CADET_Handle *h = cls;
707 h->reconnect_task = NULL;
708 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
715 * Reconnect to the service, retransmit all infomation to try to restore the
718 * @param h handle to the cadet
720 * @return GNUNET_YES in case of sucess, GNUNET_NO otherwise (service down...)
723 reconnect (struct GNUNET_CADET_Handle *h)
725 struct GNUNET_CADET_Channel *ch;
727 LOG (GNUNET_ERROR_TYPE_DEBUG,
728 "Requested RECONNECT, destroying all channels\n");
729 h->in_receive = GNUNET_NO;
730 for (ch = h->channels_head; NULL != ch; ch = h->channels_head)
731 destroy_channel (ch, GNUNET_YES);
732 if (NULL == h->reconnect_task)
733 h->reconnect_task = GNUNET_SCHEDULER_add_delayed (h->reconnect_time,
738 /******************************************************************************/
739 /*********************** RECEIVE HANDLERS ****************************/
740 /******************************************************************************/
743 * Process the new channel notification and add it to the channels in the handle
745 * @param h The cadet handle
746 * @param msg A message with the details of the new incoming channel
749 process_channel_created (struct GNUNET_CADET_Handle *h,
750 const struct GNUNET_CADET_ChannelMessage *msg)
752 struct GNUNET_CADET_Channel *ch;
753 CADET_ChannelNumber chid;
756 chid = ntohl (msg->channel_id);
757 port = ntohl (msg->port);
758 LOG (GNUNET_ERROR_TYPE_DEBUG, "Creating incoming channel %X:%u\n", chid, port);
759 if (chid < GNUNET_CADET_LOCAL_CHANNEL_ID_SERV)
764 if (NULL != h->new_channel)
768 ch = create_channel (h, chid);
769 ch->allow_send = GNUNET_NO;
770 ch->peer = GNUNET_PEER_intern (&msg->peer);
774 ch->options = ntohl (msg->opt);
776 LOG (GNUNET_ERROR_TYPE_DEBUG, " created channel %p\n", ch);
777 ctx = h->new_channel (h->cls, ch, &msg->peer, ch->port, ch->options);
780 LOG (GNUNET_ERROR_TYPE_DEBUG, "User notified\n");
784 struct GNUNET_CADET_ChannelMessage d_msg;
786 LOG (GNUNET_ERROR_TYPE_DEBUG, "No handler for incoming channels\n");
788 d_msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DESTROY);
789 d_msg.header.size = htons (sizeof (struct GNUNET_CADET_ChannelMessage));
790 d_msg.channel_id = msg->channel_id;
791 memset (&d_msg.peer, 0, sizeof (struct GNUNET_PeerIdentity));
795 send_packet (h, &d_msg.header, NULL);
802 * Process the channel destroy notification and free associated resources
804 * @param h The cadet handle
805 * @param msg A message with the details of the channel being destroyed
808 process_channel_destroy (struct GNUNET_CADET_Handle *h,
809 const struct GNUNET_CADET_ChannelMessage *msg)
811 struct GNUNET_CADET_Channel *ch;
812 CADET_ChannelNumber chid;
814 LOG (GNUNET_ERROR_TYPE_DEBUG, "Channel Destroy received from service\n");
815 chid = ntohl (msg->channel_id);
816 ch = retrieve_channel (h, chid);
820 LOG (GNUNET_ERROR_TYPE_DEBUG, "channel %X unknown\n", chid);
823 LOG (GNUNET_ERROR_TYPE_DEBUG, " destroying channel %X\n", ch->chid);
824 destroy_channel (ch, GNUNET_YES);
829 * Process the incoming data packets, call appropriate handlers.
831 * @param h The cadet handle
832 * @param message A message encapsulating the data
835 process_incoming_data (struct GNUNET_CADET_Handle *h,
836 const struct GNUNET_MessageHeader *message)
838 const struct GNUNET_MessageHeader *payload;
839 const struct GNUNET_CADET_MessageHandler *handler;
840 struct GNUNET_CADET_LocalData *dmsg;
841 struct GNUNET_CADET_Channel *ch;
846 LOG (GNUNET_ERROR_TYPE_DEBUG, "Got a data message!\n");
847 dmsg = (struct GNUNET_CADET_LocalData *) message;
848 ch = retrieve_channel (h, ntohl (dmsg->id));
855 payload = (struct GNUNET_MessageHeader *) &dmsg[1];
856 LOG (GNUNET_ERROR_TYPE_DEBUG, " %s data on channel %s [%X]\n",
857 GC_f2s (ch->chid >= GNUNET_CADET_LOCAL_CHANNEL_ID_SERV),
858 GNUNET_i2s (GNUNET_PEER_resolve2 (ch->peer)), ntohl (dmsg->id));
860 size = ntohs (message->size);
861 LOG (GNUNET_ERROR_TYPE_DEBUG, " %u bytes\n", size);
863 type = ntohs (payload->type);
864 size = ntohs (payload->size);
865 LOG (GNUNET_ERROR_TYPE_DEBUG, " payload type %s\n", GC_m2s (type));
866 for (i = 0; i < h->n_handlers; i++)
868 handler = &h->message_handlers[i];
869 LOG (GNUNET_ERROR_TYPE_DEBUG, " checking handler for type %u\n",
871 if (handler->type == type)
874 handler->callback (h->cls, ch, &ch->ctx, payload))
876 LOG (GNUNET_ERROR_TYPE_DEBUG, "callback caused disconnection\n");
877 GNUNET_CADET_channel_destroy (ch);
882 LOG (GNUNET_ERROR_TYPE_DEBUG,
883 "callback completed successfully\n");
892 * Process a local ACK message, enabling the client to send
893 * more data to the service.
895 * @param h Cadet handle.
896 * @param message Message itself.
899 process_ack (struct GNUNET_CADET_Handle *h,
900 const struct GNUNET_MessageHeader *message)
902 struct GNUNET_CADET_LocalAck *msg;
903 struct GNUNET_CADET_Channel *ch;
904 CADET_ChannelNumber chid;
906 LOG (GNUNET_ERROR_TYPE_DEBUG, "Got an ACK!\n");
907 msg = (struct GNUNET_CADET_LocalAck *) message;
908 chid = ntohl (msg->channel_id);
909 ch = retrieve_channel (h, chid);
912 LOG (GNUNET_ERROR_TYPE_DEBUG, "ACK on unknown channel %X\n", chid);
915 LOG (GNUNET_ERROR_TYPE_DEBUG, " on channel %X!\n", ch->chid);
916 ch->allow_send = GNUNET_YES;
917 if (NULL == h->th && 0 < ch->packet_size)
919 LOG (GNUNET_ERROR_TYPE_DEBUG, " tmt rdy was NULL, requesting!\n");
920 h->th = GNUNET_CLIENT_notify_transmit_ready (h->client, ch->packet_size,
921 GNUNET_TIME_UNIT_FOREVER_REL,
922 GNUNET_YES, &send_callback, h);
928 * Process a local reply about info on all channels, pass info to the user.
930 * @param h Cadet handle.
931 * @param message Message itself.
934 // process_get_channels (struct GNUNET_CADET_Handle *h,
935 // const struct GNUNET_MessageHeader *message)
937 // struct GNUNET_CADET_LocalInfo *msg;
939 // GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Get Channels messasge received\n");
941 // if (NULL == h->channels_cb)
943 // GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " ignored\n");
947 // msg = (struct GNUNET_CADET_LocalInfo *) message;
948 // if (ntohs (message->size) !=
949 // (sizeof (struct GNUNET_CADET_LocalInfo) +
950 // sizeof (struct GNUNET_PeerIdentity)))
952 // GNUNET_break_op (0);
953 // GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
954 // "Get channels message: size %hu - expected %u\n",
955 // ntohs (message->size),
956 // sizeof (struct GNUNET_CADET_LocalInfo));
959 // h->channels_cb (h->channels_cls,
960 // ntohl (msg->channel_id),
962 // &msg->destination);
968 * Process a local monitor_channel reply, pass info to the user.
970 * @param h Cadet handle.
971 * @param message Message itself.
974 // process_show_channel (struct GNUNET_CADET_Handle *h,
975 // const struct GNUNET_MessageHeader *message)
977 // struct GNUNET_CADET_LocalInfo *msg;
980 // GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Show Channel messasge received\n");
982 // if (NULL == h->channel_cb)
984 // GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " ignored\n");
988 // /* Verify message sanity */
989 // msg = (struct GNUNET_CADET_LocalInfo *) message;
990 // esize = sizeof (struct GNUNET_CADET_LocalInfo);
991 // if (ntohs (message->size) != esize)
993 // GNUNET_break_op (0);
994 // GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
995 // "Show channel message: size %hu - expected %u\n",
996 // ntohs (message->size),
999 // h->channel_cb (h->channel_cls, NULL, NULL);
1000 // h->channel_cb = NULL;
1001 // h->channel_cls = NULL;
1006 // h->channel_cb (h->channel_cls,
1007 // &msg->destination,
1014 * Process a local reply about info on all tunnels, pass info to the user.
1016 * @param h Cadet handle.
1017 * @param message Message itself.
1020 process_get_peers (struct GNUNET_CADET_Handle *h,
1021 const struct GNUNET_MessageHeader *message)
1023 struct GNUNET_CADET_LocalInfoPeer *msg;
1026 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Get Peer messasge received\n");
1028 if (NULL == h->info_cb.peers_cb)
1030 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ignored\n");
1034 size = ntohs (message->size);
1035 if (sizeof (struct GNUNET_CADET_LocalInfoPeer) > size)
1037 h->info_cb.peers_cb (h->info_cls, NULL, -1, 0, 0);
1038 h->info_cb.peers_cb = NULL;
1043 msg = (struct GNUNET_CADET_LocalInfoPeer *) message;
1044 h->info_cb.peers_cb (h->info_cls, &msg->destination,
1045 (int) ntohs (msg->tunnel),
1046 (unsigned int ) ntohs (msg->paths),
1052 * Process a local peer info reply, pass info to the user.
1054 * @param h Cadet handle.
1055 * @param message Message itself.
1058 process_get_peer (struct GNUNET_CADET_Handle *h,
1059 const struct GNUNET_MessageHeader *message)
1061 struct GNUNET_CADET_LocalInfoTunnel *msg;
1065 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Get Tunnel messasge received\n");
1066 if (NULL == h->info_cb.peer_cb)
1068 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ignored\n");
1072 /* Verify message sanity */
1073 msg = (struct GNUNET_CADET_LocalInfoTunnel *) message;
1074 msize = ntohs (message->size);
1075 esize = sizeof (struct GNUNET_CADET_LocalInfoPeer);
1078 GNUNET_break_op (0);
1079 h->info_cb.peer_cb (h->info_cls, NULL, 0, 0, 0, NULL);
1082 // esize += ch_n * sizeof (CADET_ChannelNumber);
1083 // esize += c_n * sizeof (struct GNUNET_CADET_Hash);
1086 GNUNET_break_op (0);
1087 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "m:%u, e: %u\n", msize, esize);
1088 h->info_cb.peer_cb (h->info_cls, NULL, 0, 0, 0, NULL);
1092 /* Call Callback with tunnel info. */
1093 h->info_cb.peer_cb (h->info_cls, &msg->destination, 0, 0, 0, NULL);
1096 h->info_cb.peer_cb = NULL;
1102 * Process a local reply about info on all tunnels, pass info to the user.
1104 * @param h Cadet handle.
1105 * @param message Message itself.
1108 process_get_tunnels (struct GNUNET_CADET_Handle *h,
1109 const struct GNUNET_MessageHeader *message)
1111 struct GNUNET_CADET_LocalInfoTunnel *msg;
1114 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Get Tunnels messasge received\n");
1116 if (NULL == h->info_cb.tunnels_cb)
1118 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ignored\n");
1122 size = ntohs (message->size);
1123 if (sizeof (struct GNUNET_CADET_LocalInfoTunnel) > size)
1125 h->info_cb.tunnels_cb (h->info_cls, NULL, 0, 0, 0, 0);
1126 h->info_cb.tunnels_cb = NULL;
1131 msg = (struct GNUNET_CADET_LocalInfoTunnel *) message;
1132 h->info_cb.tunnels_cb (h->info_cls, &msg->destination,
1133 ntohl (msg->channels), ntohl (msg->connections),
1134 ntohs (msg->estate), ntohs (msg->cstate));
1140 * Process a local tunnel info reply, pass info to the user.
1142 * @param h Cadet handle.
1143 * @param message Message itself.
1146 process_get_tunnel (struct GNUNET_CADET_Handle *h,
1147 const struct GNUNET_MessageHeader *message)
1149 struct GNUNET_CADET_LocalInfoTunnel *msg;
1154 struct GNUNET_CADET_Hash *conns;
1155 CADET_ChannelNumber *chns;
1157 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Get Tunnel messasge received\n");
1158 if (NULL == h->info_cb.tunnel_cb)
1160 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ignored\n");
1164 /* Verify message sanity */
1165 msg = (struct GNUNET_CADET_LocalInfoTunnel *) message;
1166 msize = ntohs (message->size);
1167 esize = sizeof (struct GNUNET_CADET_LocalInfoTunnel);
1170 GNUNET_break_op (0);
1171 h->info_cb.tunnel_cb (h->info_cls, NULL, 0, 0, NULL, NULL, 0, 0);
1174 ch_n = ntohl (msg->channels);
1175 c_n = ntohl (msg->connections);
1176 esize += ch_n * sizeof (CADET_ChannelNumber);
1177 esize += c_n * sizeof (struct GNUNET_CADET_Hash);
1180 GNUNET_break_op (0);
1181 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "m:%u, e: %u (%u ch, %u conn)\n",
1182 msize, esize, ch_n, c_n);
1183 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "%u (%u ch, %u conn)\n",
1184 sizeof (struct GNUNET_CADET_LocalInfoTunnel),
1185 sizeof (CADET_ChannelNumber), sizeof (struct GNUNET_HashCode));
1186 h->info_cb.tunnel_cb (h->info_cls, NULL, 0, 0, NULL, NULL, 0, 0);
1190 /* Call Callback with tunnel info. */
1191 conns = (struct GNUNET_CADET_Hash *) &msg[1];
1192 chns = (CADET_ChannelNumber *) &conns[c_n];
1193 h->info_cb.tunnel_cb (h->info_cls, &msg->destination,
1194 ch_n, c_n, chns, conns,
1195 ntohs (msg->estate), ntohs (msg->cstate));
1198 h->info_cb.tunnel_cb = NULL;
1204 * Function to process all messages received from the service
1206 * @param cls closure
1207 * @param msg message received, NULL on timeout or fatal error
1210 msg_received (void *cls, const struct GNUNET_MessageHeader *msg)
1212 struct GNUNET_CADET_Handle *h = cls;
1217 LOG (GNUNET_ERROR_TYPE_DEBUG,
1218 "Cadet service disconnected, reconnecting\n", h);
1222 type = ntohs (msg->type);
1223 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n");
1224 LOG (GNUNET_ERROR_TYPE_DEBUG, "Received a message: %s\n",
1228 /* Notify of a new incoming channel */
1229 case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_CREATE:
1230 process_channel_created (h, (struct GNUNET_CADET_ChannelMessage *) msg);
1232 /* Notify of a channel disconnection */
1233 case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DESTROY: /* TODO separate(gid problem)*/
1234 case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_NACK:
1235 process_channel_destroy (h, (struct GNUNET_CADET_ChannelMessage *) msg);
1237 case GNUNET_MESSAGE_TYPE_CADET_LOCAL_DATA:
1238 process_incoming_data (h, msg);
1240 case GNUNET_MESSAGE_TYPE_CADET_LOCAL_ACK:
1241 process_ack (h, msg);
1243 // case GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_CHANNELS:
1244 // process_get_channels (h, msg);
1246 // case GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_CHANNEL:
1247 // process_show_channel (h, msg);
1249 case GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEERS:
1250 process_get_peers (h, msg);
1252 case GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEER:
1253 process_get_peer (h, msg);
1255 case GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNELS:
1256 process_get_tunnels (h, msg);
1258 case GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNEL:
1259 process_get_tunnel (h, msg);
1261 // case GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_CHANNEL:
1262 // process_show_channel (h, msg);
1265 /* We shouldn't get any other packages, log and ignore */
1266 LOG (GNUNET_ERROR_TYPE_WARNING,
1267 "unsolicited message form service (type %s)\n",
1268 GC_m2s (ntohs (msg->type)));
1270 LOG (GNUNET_ERROR_TYPE_DEBUG, "message processed\n");
1271 if (GNUNET_YES == h->in_receive)
1273 GNUNET_CLIENT_receive (h->client, &msg_received, h,
1274 GNUNET_TIME_UNIT_FOREVER_REL);
1278 LOG (GNUNET_ERROR_TYPE_DEBUG,
1279 "in receive off, not calling CLIENT_receive\n");
1284 /******************************************************************************/
1285 /************************ SEND FUNCTIONS ****************************/
1286 /******************************************************************************/
1289 * Function called to send a message to the service.
1290 * "buf" will be NULL and "size" zero if the socket was closed for writing in
1293 * @param cls closure, the cadet handle
1294 * @param size number of bytes available in buf
1295 * @param buf where the callee should write the connect message
1296 * @return number of bytes written to buf
1299 send_callback (void *cls, size_t size, void *buf)
1301 struct GNUNET_CADET_Handle *h = cls;
1302 struct GNUNET_CADET_TransmitHandle *th;
1303 struct GNUNET_CADET_TransmitHandle *next;
1304 struct GNUNET_CADET_Channel *ch;
1310 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n");
1311 LOG (GNUNET_ERROR_TYPE_DEBUG, "# Send callback, buffer %u\n", size);
1312 if ((0 == size) || (NULL == buf))
1314 LOG (GNUNET_ERROR_TYPE_DEBUG, "# Received NULL send callback on %p\n", h);
1321 nsize = message_ready_size (h);
1322 while ((NULL != (th = next)) && (0 < nsize) && (size >= nsize))
1325 if (GNUNET_YES == th_is_payload (th))
1327 struct GNUNET_CADET_LocalData *dmsg;
1328 struct GNUNET_MessageHeader *mh;
1330 LOG (GNUNET_ERROR_TYPE_DEBUG, "# payload\n");
1331 if (GNUNET_NO == ch->allow_send)
1333 /* This channel is not ready to transmit yet, Try the next message */
1337 ch->packet_size = 0;
1338 GNUNET_assert (size >= th->size);
1339 dmsg = (struct GNUNET_CADET_LocalData *) cbuf;
1340 mh = (struct GNUNET_MessageHeader *) &dmsg[1];
1341 psize = th->notify (th->notify_cls, size - DATA_OVERHEAD, mh);
1345 GNUNET_assert (sizeof (struct GNUNET_MessageHeader) <= psize);
1346 psize += DATA_OVERHEAD;
1347 GNUNET_assert (size >= psize);
1348 dmsg->header.type = htons (GNUNET_MESSAGE_TYPE_CADET_LOCAL_DATA);
1349 dmsg->header.size = htons (psize);
1350 dmsg->id = htonl (ch->chid);
1351 LOG (GNUNET_ERROR_TYPE_DEBUG, "# payload type %s\n",
1352 GC_m2s (ntohs (mh->type)));
1353 ch->allow_send = GNUNET_NO;
1357 LOG (GNUNET_ERROR_TYPE_DEBUG,
1358 "# callback returned size 0, "
1359 "application canceled transmission\n");
1364 const struct GNUNET_MessageHeader *mh;
1366 mh = (const struct GNUNET_MessageHeader *) &th[1];
1367 LOG (GNUNET_ERROR_TYPE_DEBUG, "# cadet internal traffic, type %s\n",
1368 GC_m2s (ntohs (mh->type)));
1369 memcpy (cbuf, &th[1], th->size);
1372 GNUNET_assert (GNUNET_CONSTANTS_MAX_CADET_MESSAGE_SIZE >= psize);
1373 if (th->timeout_task != NULL)
1374 GNUNET_SCHEDULER_cancel (th->timeout_task);
1376 GNUNET_CONTAINER_DLL_remove (h->th_head, h->th_tail, th);
1378 nsize = message_ready_size (h);
1383 LOG (GNUNET_ERROR_TYPE_DEBUG, "# total size: %u\n", tsize);
1385 size = message_ready_size (h);
1388 LOG (GNUNET_ERROR_TYPE_DEBUG, "# next size: %u\n", size);
1390 GNUNET_CLIENT_notify_transmit_ready (h->client, size,
1391 GNUNET_TIME_UNIT_FOREVER_REL,
1392 GNUNET_YES, &send_callback, h);
1396 if (NULL != h->th_head)
1397 LOG (GNUNET_ERROR_TYPE_DEBUG, "# can't transmit any more\n");
1399 LOG (GNUNET_ERROR_TYPE_DEBUG, "# nothing left to transmit\n");
1401 if (GNUNET_NO == h->in_receive)
1403 LOG (GNUNET_ERROR_TYPE_DEBUG, "# start receiving from service\n");
1404 h->in_receive = GNUNET_YES;
1405 GNUNET_CLIENT_receive (h->client, &msg_received, h,
1406 GNUNET_TIME_UNIT_FOREVER_REL);
1408 LOG (GNUNET_ERROR_TYPE_DEBUG, "# Send packet() END\n");
1414 * Auxiliary function to send an already constructed packet to the service.
1415 * Takes care of creating a new queue element, copying the message and
1416 * calling the tmt_rdy function if necessary.
1418 * @param h cadet handle
1419 * @param msg message to transmit
1420 * @param channel channel this send is related to (NULL if N/A)
1423 send_packet (struct GNUNET_CADET_Handle *h,
1424 const struct GNUNET_MessageHeader *msg,
1425 struct GNUNET_CADET_Channel *channel)
1427 struct GNUNET_CADET_TransmitHandle *th;
1430 LOG (GNUNET_ERROR_TYPE_DEBUG, " Sending message to service: %s\n",
1431 GC_m2s(ntohs(msg->type)));
1432 msize = ntohs (msg->size);
1433 th = GNUNET_malloc (sizeof (struct GNUNET_CADET_TransmitHandle) + msize);
1434 th->timeout = GNUNET_TIME_UNIT_FOREVER_ABS;
1436 th->channel = channel;
1437 memcpy (&th[1], msg, msize);
1438 add_to_queue (h, th);
1441 LOG (GNUNET_ERROR_TYPE_DEBUG, " calling ntfy tmt rdy for %u bytes\n", msize);
1443 GNUNET_CLIENT_notify_transmit_ready (h->client, msize,
1444 GNUNET_TIME_UNIT_FOREVER_REL,
1445 GNUNET_YES, &send_callback, h);
1449 /******************************************************************************/
1450 /********************** API CALL DEFINITIONS *************************/
1451 /******************************************************************************/
1453 struct GNUNET_CADET_Handle *
1454 GNUNET_CADET_connect (const struct GNUNET_CONFIGURATION_Handle *cfg, void *cls,
1455 GNUNET_CADET_InboundChannelNotificationHandler new_channel,
1456 GNUNET_CADET_ChannelEndHandler cleaner,
1457 const struct GNUNET_CADET_MessageHandler *handlers,
1458 const uint32_t *ports)
1460 struct GNUNET_CADET_Handle *h;
1462 LOG (GNUNET_ERROR_TYPE_DEBUG, "GNUNET_CADET_connect()\n");
1463 h = GNUNET_new (struct GNUNET_CADET_Handle);
1464 LOG (GNUNET_ERROR_TYPE_DEBUG, " addr %p\n", h);
1466 h->new_channel = new_channel;
1467 h->cleaner = cleaner;
1468 h->client = GNUNET_CLIENT_connect ("cadet", cfg);
1469 if (h->client == NULL)
1476 h->message_handlers = handlers;
1478 h->next_chid = GNUNET_CADET_LOCAL_CHANNEL_ID_CLI;
1479 h->reconnect_time = GNUNET_TIME_UNIT_MILLISECONDS;
1480 h->reconnect_task = NULL;
1482 if (NULL != ports && ports[0] != 0 && NULL == new_channel)
1485 LOG (GNUNET_ERROR_TYPE_DEBUG,
1486 "no new channel handler given, ports parameter is useless!!\n");
1488 if ((NULL == ports || ports[0] == 0) && NULL != new_channel)
1491 LOG (GNUNET_ERROR_TYPE_DEBUG,
1492 "no ports given, new channel handler will never be called!!\n");
1494 /* count handlers */
1495 for (h->n_handlers = 0;
1496 handlers && handlers[h->n_handlers].type;
1498 for (h->n_ports = 0;
1499 ports && ports[h->n_ports];
1502 LOG (GNUNET_ERROR_TYPE_DEBUG, "GNUNET_CADET_connect() END\n");
1508 GNUNET_CADET_disconnect (struct GNUNET_CADET_Handle *handle)
1510 struct GNUNET_CADET_Channel *ch;
1511 struct GNUNET_CADET_Channel *aux;
1512 struct GNUNET_CADET_TransmitHandle *th;
1514 LOG (GNUNET_ERROR_TYPE_DEBUG, "CADET DISCONNECT\n");
1516 ch = handle->channels_head;
1520 if (ch->chid < GNUNET_CADET_LOCAL_CHANNEL_ID_SERV)
1523 LOG (GNUNET_ERROR_TYPE_DEBUG, "channel %X not destroyed\n", ch->chid);
1525 destroy_channel (ch, GNUNET_YES);
1528 while ( (th = handle->th_head) != NULL)
1530 struct GNUNET_MessageHeader *msg;
1532 /* Make sure it is an allowed packet (everything else should have been
1533 * already canceled).
1535 GNUNET_break (GNUNET_NO == th_is_payload (th));
1536 msg = (struct GNUNET_MessageHeader *) &th[1];
1537 switch (ntohs(msg->type))
1539 case GNUNET_MESSAGE_TYPE_CADET_LOCAL_CONNECT:
1540 case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_CREATE:
1541 case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DESTROY:
1542 case GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_CHANNELS:
1543 case GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_CHANNEL:
1544 case GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEER:
1545 case GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEERS:
1546 case GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNEL:
1547 case GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNELS:
1551 LOG (GNUNET_ERROR_TYPE_ERROR, "unexpected msg %u\n",
1555 GNUNET_CONTAINER_DLL_remove (handle->th_head, handle->th_tail, th);
1559 if (NULL != handle->th)
1561 GNUNET_CLIENT_notify_transmit_ready_cancel (handle->th);
1564 if (NULL != handle->client)
1566 GNUNET_CLIENT_disconnect (handle->client);
1567 handle->client = NULL;
1569 if (NULL != handle->reconnect_task)
1571 GNUNET_SCHEDULER_cancel(handle->reconnect_task);
1572 handle->reconnect_task = NULL;
1574 GNUNET_free (handle);
1579 * Create a new channel towards a remote peer.
1581 * If the destination port is not open by any peer or the destination peer
1582 * does not accept the channel, #GNUNET_CADET_ChannelEndHandler will be called
1585 * @param h cadet handle
1586 * @param channel_ctx client's channel context to associate with the channel
1587 * @param peer peer identity the channel should go to
1588 * @param port Port number.
1589 * @param options CadetOption flag field, with all desired option bits set to 1.
1591 * @return handle to the channel
1593 struct GNUNET_CADET_Channel *
1594 GNUNET_CADET_channel_create (struct GNUNET_CADET_Handle *h,
1596 const struct GNUNET_PeerIdentity *peer,
1598 enum GNUNET_CADET_ChannelOption options)
1600 struct GNUNET_CADET_Channel *ch;
1601 struct GNUNET_CADET_ChannelMessage msg;
1603 LOG (GNUNET_ERROR_TYPE_DEBUG,
1604 "Creating new channel to %s:%u\n",
1605 GNUNET_i2s (peer), port);
1606 ch = create_channel (h, 0);
1607 LOG (GNUNET_ERROR_TYPE_DEBUG, " at %p\n", ch);
1608 LOG (GNUNET_ERROR_TYPE_DEBUG, " number %X\n", ch->chid);
1609 ch->ctx = channel_ctx;
1610 ch->peer = GNUNET_PEER_intern (peer);
1611 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CHANNEL_CREATE);
1612 msg.header.size = htons (sizeof (struct GNUNET_CADET_ChannelMessage));
1613 msg.channel_id = htonl (ch->chid);
1614 msg.port = htonl (port);
1616 msg.opt = htonl (options);
1617 ch->allow_send = GNUNET_NO;
1618 send_packet (h, &msg.header, ch);
1624 GNUNET_CADET_channel_destroy (struct GNUNET_CADET_Channel *channel)
1626 struct GNUNET_CADET_Handle *h;
1627 struct GNUNET_CADET_ChannelMessage msg;
1628 struct GNUNET_CADET_TransmitHandle *th;
1630 LOG (GNUNET_ERROR_TYPE_DEBUG, "Destroying channel\n");
1633 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DESTROY);
1634 msg.header.size = htons (sizeof (struct GNUNET_CADET_ChannelMessage));
1635 msg.channel_id = htonl (channel->chid);
1636 memset (&msg.peer, 0, sizeof (struct GNUNET_PeerIdentity));
1642 struct GNUNET_CADET_TransmitHandle *aux;
1643 if (th->channel == channel)
1646 if (GNUNET_YES == th_is_payload (th))
1648 /* applications should cancel before destroying channel */
1650 th->notify (th->notify_cls, 0, NULL);
1652 GNUNET_CADET_notify_transmit_ready_cancel (th);
1659 destroy_channel (channel, GNUNET_YES);
1660 send_packet (h, &msg.header, NULL);
1665 * Get information about a channel.
1667 * @param channel Channel handle.
1668 * @param option Query (GNUNET_CADET_OPTION_*).
1669 * @param ... dependant on option, currently not used
1671 * @return Union with an answer to the query.
1673 const union GNUNET_CADET_ChannelInfo *
1674 GNUNET_CADET_channel_get_info (struct GNUNET_CADET_Channel *channel,
1675 enum GNUNET_CADET_ChannelOption option, ...)
1677 static int bool_flag;
1678 const union GNUNET_CADET_ChannelInfo *ret;
1682 case GNUNET_CADET_OPTION_NOBUFFER:
1683 case GNUNET_CADET_OPTION_RELIABLE:
1684 case GNUNET_CADET_OPTION_OOORDER:
1685 if (0 != (option & channel->options))
1686 bool_flag = GNUNET_YES;
1688 bool_flag = GNUNET_NO;
1689 ret = (const union GNUNET_CADET_ChannelInfo *) &bool_flag;
1691 case GNUNET_CADET_OPTION_PEER:
1692 ret = (const union GNUNET_CADET_ChannelInfo *) GNUNET_PEER_resolve2 (channel->peer);
1702 struct GNUNET_CADET_TransmitHandle *
1703 GNUNET_CADET_notify_transmit_ready (struct GNUNET_CADET_Channel *channel, int cork,
1704 struct GNUNET_TIME_Relative maxdelay,
1706 GNUNET_CONNECTION_TransmitReadyNotify notify,
1709 struct GNUNET_CADET_TransmitHandle *th;
1711 GNUNET_assert (NULL != channel);
1712 GNUNET_assert (GNUNET_CONSTANTS_MAX_CADET_MESSAGE_SIZE >= notify_size);
1713 LOG (GNUNET_ERROR_TYPE_DEBUG, "CADET NOTIFY TRANSMIT READY\n");
1714 LOG (GNUNET_ERROR_TYPE_DEBUG, " on channel %X\n", channel->chid);
1715 LOG (GNUNET_ERROR_TYPE_DEBUG, " allow_send %d\n", channel->allow_send);
1716 if (channel->chid >= GNUNET_CADET_LOCAL_CHANNEL_ID_SERV)
1717 LOG (GNUNET_ERROR_TYPE_DEBUG, " to origin\n");
1719 LOG (GNUNET_ERROR_TYPE_DEBUG, " to destination\n");
1720 LOG (GNUNET_ERROR_TYPE_DEBUG, " payload size %u\n", notify_size);
1721 GNUNET_assert (NULL != notify);
1722 GNUNET_assert (0 == channel->packet_size); // Only one data packet allowed
1723 th = GNUNET_new (struct GNUNET_CADET_TransmitHandle);
1724 th->channel = channel;
1725 th->timeout = GNUNET_TIME_relative_to_absolute (maxdelay);
1726 th->size = notify_size + DATA_OVERHEAD;
1727 channel->packet_size = th->size;
1728 LOG (GNUNET_ERROR_TYPE_DEBUG, " total size %u\n", th->size);
1729 th->notify = notify;
1730 th->notify_cls = notify_cls;
1731 add_to_queue (channel->cadet, th);
1732 if (NULL != channel->cadet->th)
1734 if (GNUNET_NO == channel->allow_send)
1736 LOG (GNUNET_ERROR_TYPE_DEBUG, " call client notify tmt rdy\n");
1737 channel->cadet->th =
1738 GNUNET_CLIENT_notify_transmit_ready (channel->cadet->client, th->size,
1739 GNUNET_TIME_UNIT_FOREVER_REL,
1740 GNUNET_YES, &send_callback,
1742 LOG (GNUNET_ERROR_TYPE_DEBUG, "CADET NOTIFY TRANSMIT READY END\n");
1748 GNUNET_CADET_notify_transmit_ready_cancel (struct GNUNET_CADET_TransmitHandle *th)
1750 struct GNUNET_CADET_Handle *cadet;
1752 th->channel->packet_size = 0;
1753 cadet = th->channel->cadet;
1754 if (th->timeout_task != NULL)
1755 GNUNET_SCHEDULER_cancel (th->timeout_task);
1756 GNUNET_CONTAINER_DLL_remove (cadet->th_head, cadet->th_tail, th);
1758 if ((0 == message_ready_size (cadet)) && (NULL != cadet->th))
1760 /* queue empty, no point in asking for transmission */
1761 GNUNET_CLIENT_notify_transmit_ready_cancel (cadet->th);
1768 GNUNET_CADET_receive_done (struct GNUNET_CADET_Channel *channel)
1775 send_info_request (struct GNUNET_CADET_Handle *h, uint16_t type)
1777 struct GNUNET_MessageHeader msg;
1779 msg.size = htons (sizeof (msg));
1780 msg.type = htons (type);
1781 send_packet (h, &msg, NULL);
1786 * Request a debug dump on the service's STDERR.
1788 * WARNING: unstable API, likely to change in the future!
1790 * @param h cadet handle
1793 GNUNET_CADET_request_dump (struct GNUNET_CADET_Handle *h)
1795 LOG (GNUNET_ERROR_TYPE_DEBUG, "requesting dump\n");
1796 send_info_request (h, GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_DUMP);
1801 * Request information about peers known to the running cadet service.
1802 * The callback will be called for every peer known to the service.
1803 * Only one info request (of any kind) can be active at once.
1806 * WARNING: unstable API, likely to change in the future!
1808 * @param h Handle to the cadet peer.
1809 * @param callback Function to call with the requested data.
1810 * @param callback_cls Closure for @c callback.
1812 * @return #GNUNET_OK / #GNUNET_SYSERR
1815 GNUNET_CADET_get_peers (struct GNUNET_CADET_Handle *h,
1816 GNUNET_CADET_PeersCB callback,
1819 if (NULL != h->info_cb.peers_cb)
1822 return GNUNET_SYSERR;
1824 send_info_request (h, GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEERS);
1825 h->info_cb.peers_cb = callback;
1826 h->info_cls = callback_cls;
1832 * Cancel a peer info request. The callback will not be called (anymore).
1834 * WARNING: unstable API, likely to change in the future!
1836 * @param h Cadet handle.
1838 * @return Closure given to GNUNET_CADET_get_peers.
1841 GNUNET_CADET_get_peers_cancel (struct GNUNET_CADET_Handle *h)
1846 h->info_cb.peers_cb = NULL;
1853 * Request information about a peer known to the running cadet peer.
1854 * The callback will be called for the tunnel once.
1855 * Only one info request (of any kind) can be active at once.
1857 * WARNING: unstable API, likely to change in the future!
1859 * @param h Handle to the cadet peer.
1860 * @param id Peer whose tunnel to examine.
1861 * @param callback Function to call with the requested data.
1862 * @param callback_cls Closure for @c callback.
1864 * @return #GNUNET_OK / #GNUNET_SYSERR
1867 GNUNET_CADET_get_peer (struct GNUNET_CADET_Handle *h,
1868 const struct GNUNET_PeerIdentity *id,
1869 GNUNET_CADET_PeerCB callback,
1872 struct GNUNET_CADET_LocalInfo msg;
1874 if (NULL != h->info_cb.peer_cb)
1877 return GNUNET_SYSERR;
1880 memset (&msg, 0, sizeof (msg));
1881 msg.header.size = htons (sizeof (msg));
1882 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEER);
1884 send_packet (h, &msg.header, NULL);
1885 h->info_cb.peer_cb = callback;
1886 h->info_cls = callback_cls;
1892 * Request information about tunnels of the running cadet peer.
1893 * The callback will be called for every tunnel of the service.
1894 * Only one info request (of any kind) can be active at once.
1896 * WARNING: unstable API, likely to change in the future!
1898 * @param h Handle to the cadet peer.
1899 * @param callback Function to call with the requested data.
1900 * @param callback_cls Closure for @c callback.
1902 * @return #GNUNET_OK / #GNUNET_SYSERR
1905 GNUNET_CADET_get_tunnels (struct GNUNET_CADET_Handle *h,
1906 GNUNET_CADET_TunnelsCB callback,
1909 if (NULL != h->info_cb.tunnels_cb)
1912 return GNUNET_SYSERR;
1914 send_info_request (h, GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNELS);
1915 h->info_cb.tunnels_cb = callback;
1916 h->info_cls = callback_cls;
1922 * Cancel a monitor request. The monitor callback will not be called.
1924 * @param h Cadet handle.
1926 * @return Closure given to GNUNET_CADET_get_tunnels.
1929 GNUNET_CADET_get_tunnels_cancel (struct GNUNET_CADET_Handle *h)
1933 h->info_cb.tunnels_cb = NULL;
1943 * Request information about a tunnel of the running cadet peer.
1944 * The callback will be called for the tunnel once.
1945 * Only one info request (of any kind) can be active at once.
1947 * WARNING: unstable API, likely to change in the future!
1949 * @param h Handle to the cadet peer.
1950 * @param id Peer whose tunnel to examine.
1951 * @param callback Function to call with the requested data.
1952 * @param callback_cls Closure for @c callback.
1954 * @return #GNUNET_OK / #GNUNET_SYSERR
1957 GNUNET_CADET_get_tunnel (struct GNUNET_CADET_Handle *h,
1958 const struct GNUNET_PeerIdentity *id,
1959 GNUNET_CADET_TunnelCB callback,
1962 struct GNUNET_CADET_LocalInfo msg;
1964 if (NULL != h->info_cb.tunnel_cb)
1967 return GNUNET_SYSERR;
1970 memset (&msg, 0, sizeof (msg));
1971 msg.header.size = htons (sizeof (msg));
1972 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNEL);
1974 send_packet (h, &msg.header, NULL);
1975 h->info_cb.tunnel_cb = callback;
1976 h->info_cls = callback_cls;
1982 * Request information about a specific channel of the running cadet peer.
1984 * WARNING: unstable API, likely to change in the future!
1985 * FIXME Add destination option.
1987 * @param h Handle to the cadet peer.
1988 * @param initiator ID of the owner of the channel.
1989 * @param channel_number Channel number.
1990 * @param callback Function to call with the requested data.
1991 * @param callback_cls Closure for @c callback.
1993 * @return #GNUNET_OK / #GNUNET_SYSERR
1996 GNUNET_CADET_show_channel (struct GNUNET_CADET_Handle *h,
1997 struct GNUNET_PeerIdentity *initiator,
1998 unsigned int channel_number,
1999 GNUNET_CADET_ChannelCB callback,
2002 struct GNUNET_CADET_LocalInfo msg;
2004 if (NULL != h->info_cb.channel_cb)
2007 return GNUNET_SYSERR;
2010 msg.header.size = htons (sizeof (msg));
2011 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_CHANNEL);
2012 msg.peer = *initiator;
2013 msg.channel_id = htonl (channel_number);
2014 // msg.reserved = 0;
2015 send_packet (h, &msg.header, NULL);
2016 h->info_cb.channel_cb = callback;
2017 h->info_cls = callback_cls;
2023 * Function called to notify a client about the connection
2024 * begin ready to queue more data. "buf" will be
2025 * NULL and "size" zero if the connection was closed for
2026 * writing in the meantime.
2028 * @param cls closure
2029 * @param size number of bytes available in buf
2030 * @param buf where the callee should write the message
2031 * @return number of bytes written to buf
2034 cadet_mq_ntr (void *cls, size_t size,
2037 struct GNUNET_MQ_Handle *mq = cls;
2038 struct CadetMQState *state = GNUNET_MQ_impl_state (mq);
2039 const struct GNUNET_MessageHeader *msg = GNUNET_MQ_impl_current (mq);
2045 GNUNET_MQ_inject_error (mq, GNUNET_MQ_ERROR_WRITE);
2048 msize = ntohs (msg->size);
2049 GNUNET_assert (msize <= size);
2050 memcpy (buf, msg, msize);
2051 GNUNET_MQ_impl_send_continue (mq);
2057 * Signature of functions implementing the
2058 * sending functionality of a message queue.
2060 * @param mq the message queue
2061 * @param msg the message to send
2062 * @param impl_state state of the implementation
2065 cadet_mq_send_impl (struct GNUNET_MQ_Handle *mq,
2066 const struct GNUNET_MessageHeader *msg,
2069 struct CadetMQState *state = impl_state;
2071 GNUNET_assert (NULL == state->th);
2073 GNUNET_CADET_notify_transmit_ready (state->channel,
2074 /* FIXME: add option for corking */
2076 GNUNET_TIME_UNIT_FOREVER_REL,
2084 * Signature of functions implementing the
2085 * destruction of a message queue.
2086 * Implementations must not free 'mq', but should
2087 * take care of 'impl_state'.
2089 * @param mq the message queue to destroy
2090 * @param impl_state state of the implementation
2093 cadet_mq_destroy_impl (struct GNUNET_MQ_Handle *mq,
2096 struct CadetMQState *state = impl_state;
2098 if (NULL != state->th)
2099 GNUNET_CADET_notify_transmit_ready_cancel (state->th);
2101 GNUNET_free (state);
2106 * Create a message queue for a cadet channel.
2107 * The message queue can only be used to transmit messages,
2108 * not to receive them.
2110 * @param channel the channel to create the message qeue for
2111 * @return a message queue to messages over the channel
2113 struct GNUNET_MQ_Handle *
2114 GNUNET_CADET_mq_create (struct GNUNET_CADET_Channel *channel)
2116 struct GNUNET_MQ_Handle *mq;
2117 struct CadetMQState *state;
2119 state = GNUNET_new (struct CadetMQState);
2120 state->channel = channel;
2122 mq = GNUNET_MQ_queue_for_callbacks (&cadet_mq_send_impl,
2123 &cadet_mq_destroy_impl,
2124 NULL, /* FIXME: cancel impl. */
2126 NULL, /* no msg handlers */
2127 NULL, /* no err handlers */
2128 NULL); /* no handler cls */