2 This file is part of GNUnet.
3 (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., 59 Temple Place - Suite 330,
15 Boston, MA 02111-1307, 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__)
32 /******************************************************************************/
33 /************************ DATA STRUCTURES ****************************/
34 /******************************************************************************/
37 * Transmission queue to the service
39 struct GNUNET_CADET_TransmitHandle
45 struct GNUNET_CADET_TransmitHandle *next;
50 struct GNUNET_CADET_TransmitHandle *prev;
53 * Channel this message is sent on / for (may be NULL for control messages).
55 struct GNUNET_CADET_Channel *channel;
58 * Callback to obtain the message to transmit, or NULL if we
59 * got the message in 'data'. Notice that messages built
60 * by 'notify' need to be encapsulated with information about
63 GNUNET_CONNECTION_TransmitReadyNotify notify;
66 * Closure for 'notify'
71 * How long is this message valid. Once the timeout has been
72 * reached, the message must no longer be sent. If this
73 * is a message with a 'notify' callback set, the 'notify'
74 * function should be called with 'buf' NULL and size 0.
76 struct GNUNET_TIME_Absolute timeout;
79 * Task triggering a timeout, can be NO_TASK if the timeout is FOREVER.
81 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
84 * Size of 'data' -- or the desired size of 'notify' if 'data' is NULL.
94 GNUNET_CADET_ChannelCB channel_cb;
99 GNUNET_CADET_PeersCB peers_cb;
104 GNUNET_CADET_PeerCB peer_cb;
109 GNUNET_CADET_TunnelsCB tunnels_cb;
114 GNUNET_CADET_TunnelCB tunnel_cb;
119 * Opaque handle to the service.
121 struct GNUNET_CADET_Handle
125 * Handle to the server connection, to send messages later
127 struct GNUNET_CLIENT_Connection *client;
130 * Set of handlers used for processing incoming messages in the channels
132 const struct GNUNET_CADET_MessageHandler *message_handlers;
135 * Number of handlers in the handlers array.
137 unsigned int n_handlers;
142 const uint32_t *ports;
147 unsigned int n_ports;
150 * Double linked list of the channels this client is connected to, head.
152 struct GNUNET_CADET_Channel *channels_head;
155 * Double linked list of the channels this client is connected to, tail.
157 struct GNUNET_CADET_Channel *channels_tail;
160 * Callback for inbound channel creation
162 GNUNET_CADET_InboundChannelNotificationHandler *new_channel;
165 * Callback for inbound channel disconnection
167 GNUNET_CADET_ChannelEndHandler *cleaner;
170 * Handle to cancel pending transmissions in case of disconnection
172 struct GNUNET_CLIENT_TransmitHandle *th;
175 * Closure for all the handlers given by the client
180 * Messages to send to the service, head.
182 struct GNUNET_CADET_TransmitHandle *th_head;
185 * Messages to send to the service, tail.
187 struct GNUNET_CADET_TransmitHandle *th_tail;
190 * chid of the next channel to create (to avoid reusing IDs often)
192 CADET_ChannelNumber next_chid;
195 * Have we started the task to receive messages from the service
196 * yet? We do this after we send the 'CADET_LOCAL_CONNECT' message.
201 * Configuration given by the client, in case of reconnection
203 const struct GNUNET_CONFIGURATION_Handle *cfg;
206 * Time to the next reconnect in case one reconnect fails
208 struct GNUNET_TIME_Relative reconnect_time;
211 * Task for trying to reconnect.
213 GNUNET_SCHEDULER_TaskIdentifier reconnect_task;
216 * Callback for an info task (only one active at a time).
218 union CadetInfoCB info_cb;
221 * Info callback closure for @c info_cb.
228 * Description of a peer
230 struct GNUNET_CADET_Peer
233 * ID of the peer in short form
238 * Channel this peer belongs to
240 struct GNUNET_CADET_Channel *t;
245 * Opaque handle to a channel.
247 struct GNUNET_CADET_Channel
253 struct GNUNET_CADET_Channel *next;
258 struct GNUNET_CADET_Channel *prev;
261 * Handle to the cadet this channel belongs to
263 struct GNUNET_CADET_Handle *cadet;
266 * Local ID of the channel
268 CADET_ChannelNumber chid;
276 * Other end of the channel.
281 * Any data the caller wants to put in here
286 * Size of packet queued in this channel
288 unsigned int packet_size;
291 * Channel options: reliability, etc.
293 enum GNUNET_CADET_ChannelOption options;
296 * Are we allowed to send to the service?
304 * Implementation state for cadet's message queue.
309 * The current transmit handle, or NULL
310 * if no transmit is active.
312 struct GNUNET_CADET_TransmitHandle *th;
315 * Channel to send the data over.
317 struct GNUNET_CADET_Channel *channel;
321 /******************************************************************************/
322 /*********************** DECLARATIONS *************************/
323 /******************************************************************************/
326 * Function called to send a message to the service.
327 * "buf" will be NULL and "size" zero if the socket was closed for writing in
330 * @param cls closure, the cadet handle
331 * @param size number of bytes available in buf
332 * @param buf where the callee should write the connect message
333 * @return number of bytes written to buf
336 send_callback (void *cls, size_t size, void *buf);
339 /******************************************************************************/
340 /*********************** AUXILIARY FUNCTIONS *************************/
341 /******************************************************************************/
344 * Check if transmission is a payload packet.
346 * @param th Transmission handle.
348 * @return GNUNET_YES if it is a payload packet,
349 * GNUNET_NO if it is a cadet management packet.
352 th_is_payload (struct GNUNET_CADET_TransmitHandle *th)
354 return (th->notify != NULL) ? GNUNET_YES : GNUNET_NO;
359 * Check whether there is any message ready in the queue and find the size.
361 * @param h Cadet handle.
363 * @return The size of the first ready message in the queue,
364 * 0 if there is none.
367 message_ready_size (struct GNUNET_CADET_Handle *h)
369 struct GNUNET_CADET_TransmitHandle *th;
370 struct GNUNET_CADET_Channel *ch;
372 for (th = h->th_head; NULL != th; th = th->next)
375 if (GNUNET_NO == th_is_payload (th))
377 LOG (GNUNET_ERROR_TYPE_DEBUG, "# message internal\n");
380 if (GNUNET_YES == ch->allow_send)
382 LOG (GNUNET_ERROR_TYPE_DEBUG, "# message payload ok\n");
391 * Get the channel handler for the channel specified by id from the given handle
392 * @param h Cadet handle
393 * @param chid ID of the wanted channel
394 * @return handle to the required channel or NULL if not found
396 static struct GNUNET_CADET_Channel *
397 retrieve_channel (struct GNUNET_CADET_Handle *h, CADET_ChannelNumber chid)
399 struct GNUNET_CADET_Channel *ch;
401 ch = h->channels_head;
404 if (ch->chid == chid)
413 * Create a new channel and insert it in the channel list of the cadet handle
415 * @param h Cadet handle
416 * @param chid Desired chid of the channel, 0 to assign one automatically.
418 * @return Handle to the created channel.
420 static struct GNUNET_CADET_Channel *
421 create_channel (struct GNUNET_CADET_Handle *h, CADET_ChannelNumber chid)
423 struct GNUNET_CADET_Channel *ch;
425 ch = GNUNET_new (struct GNUNET_CADET_Channel);
426 GNUNET_CONTAINER_DLL_insert (h->channels_head, h->channels_tail, ch);
430 ch->chid = h->next_chid;
431 while (NULL != retrieve_channel (h, h->next_chid))
434 h->next_chid &= ~GNUNET_CADET_LOCAL_CHANNEL_ID_SERV;
435 h->next_chid |= GNUNET_CADET_LOCAL_CHANNEL_ID_CLI;
442 ch->allow_send = GNUNET_NO;
448 * Destroy the specified channel.
449 * - Destroys all peers, calling the disconnect callback on each if needed
450 * - Cancels all outgoing traffic for that channel, calling respective notifys
451 * - Calls cleaner if channel was inbound
452 * - Frees all memory used
454 * @param ch Pointer to the channel.
455 * @param call_cleaner Whether to call the cleaner handler.
457 * @return Handle to the required channel or NULL if not found.
460 destroy_channel (struct GNUNET_CADET_Channel *ch, int call_cleaner)
462 struct GNUNET_CADET_Handle *h;
463 struct GNUNET_CADET_TransmitHandle *th;
464 struct GNUNET_CADET_TransmitHandle *next;
466 LOG (GNUNET_ERROR_TYPE_DEBUG, " destroy_channel %X\n", ch->chid);
475 GNUNET_CONTAINER_DLL_remove (h->channels_head, h->channels_tail, ch);
477 /* signal channel destruction */
478 if ( (NULL != h->cleaner) && (0 != ch->peer) && (GNUNET_YES == call_cleaner) )
480 LOG (GNUNET_ERROR_TYPE_DEBUG, " calling cleaner\n");
481 h->cleaner (h->cls, ch, ch->ctx);
484 /* check that clients did not leave messages behind in the queue */
485 for (th = h->th_head; NULL != th; th = next)
488 if (th->channel != ch)
490 /* Clients should have aborted their requests already.
491 * Management traffic should be ok, as clients can't cancel that.
492 * If the service crashed and we are reconnecting, it's ok.
494 GNUNET_break (GNUNET_NO == th_is_payload (th)
495 || GNUNET_NO == h->in_receive);
496 GNUNET_CONTAINER_DLL_remove (h->th_head, h->th_tail, th);
498 /* clean up request */
499 if (GNUNET_SCHEDULER_NO_TASK != th->timeout_task)
500 GNUNET_SCHEDULER_cancel (th->timeout_task);
504 /* if there are no more pending requests with cadet service, cancel active request */
505 /* Note: this should be unnecessary... */
506 if ((0 == message_ready_size (h)) && (NULL != h->th))
508 GNUNET_CLIENT_notify_transmit_ready_cancel (h->th);
513 GNUNET_PEER_change_rc (ch->peer, -1);
520 * Notify client that the transmission has timed out
523 * @param tc task context
526 timeout_transmission (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
528 struct GNUNET_CADET_TransmitHandle *th = cls;
529 struct GNUNET_CADET_Handle *cadet;
531 cadet = th->channel->cadet;
532 GNUNET_CONTAINER_DLL_remove (cadet->th_head, cadet->th_tail, th);
533 th->channel->packet_size = 0;
534 if (GNUNET_YES == th_is_payload (th))
535 th->notify (th->notify_cls, 0, NULL);
537 if ((0 == message_ready_size (cadet)) && (NULL != cadet->th))
539 /* nothing ready to transmit, no point in asking for transmission */
540 GNUNET_CLIENT_notify_transmit_ready_cancel (cadet->th);
547 * Add a transmit handle to the transmission queue and set the
550 * @param h cadet handle with the queue head and tail
551 * @param th handle to the packet to be transmitted
554 add_to_queue (struct GNUNET_CADET_Handle *h,
555 struct GNUNET_CADET_TransmitHandle *th)
557 GNUNET_CONTAINER_DLL_insert_tail (h->th_head, h->th_tail, th);
558 if (GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us == th->timeout.abs_value_us)
561 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_absolute_get_remaining
562 (th->timeout), &timeout_transmission, th);
567 * Auxiliary function to send an already constructed packet to the service.
568 * Takes care of creating a new queue element, copying the message and
569 * calling the tmt_rdy function if necessary.
571 * @param h cadet handle
572 * @param msg message to transmit
573 * @param channel channel this send is related to (NULL if N/A)
576 send_packet (struct GNUNET_CADET_Handle *h,
577 const struct GNUNET_MessageHeader *msg,
578 struct GNUNET_CADET_Channel *channel);
582 * Send an ack on the channel to confirm the processing of a message.
584 * @param ch Channel on which to send the ACK.
587 send_ack (struct GNUNET_CADET_Channel *ch)
589 struct GNUNET_CADET_LocalAck msg;
591 LOG (GNUNET_ERROR_TYPE_DEBUG, "Sending ACK on channel %X\n", ch->chid);
592 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_LOCAL_ACK);
593 msg.header.size = htons (sizeof (msg));
594 msg.channel_id = htonl (ch->chid);
596 send_packet (ch->cadet, &msg.header, ch);
603 * Reconnect callback: tries to reconnect again after a failer previous
605 * @param cls closure (cadet handle)
606 * @param tc task context
609 reconnect_cbk (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
613 * Send a connect packet to the service with the applications and types
614 * requested by the user.
616 * @param h The cadet handle.
620 send_connect (struct GNUNET_CADET_Handle *h)
624 size = sizeof (struct GNUNET_CADET_ClientConnect);
625 size += h->n_ports * sizeof (uint32_t);
627 char buf[size] GNUNET_ALIGN;
628 struct GNUNET_CADET_ClientConnect *msg;
632 /* build connection packet */
633 msg = (struct GNUNET_CADET_ClientConnect *) buf;
634 msg->header.type = htons (GNUNET_MESSAGE_TYPE_CADET_LOCAL_CONNECT);
635 msg->header.size = htons (size);
636 ports = (uint32_t *) &msg[1];
637 for (i = 0; i < h->n_ports; i++)
639 ports[i] = htonl (h->ports[i]);
640 LOG (GNUNET_ERROR_TYPE_DEBUG, " port %u\n",
643 LOG (GNUNET_ERROR_TYPE_DEBUG,
644 "Sending %lu bytes long message with %u ports\n",
645 ntohs (msg->header.size), h->n_ports);
646 send_packet (h, &msg->header, NULL);
652 * Reconnect to the service, retransmit all infomation to try to restore the
655 * @param h handle to the cadet
657 * @return GNUNET_YES in case of sucess, GNUNET_NO otherwise (service down...)
660 do_reconnect (struct GNUNET_CADET_Handle *h)
662 LOG (GNUNET_ERROR_TYPE_DEBUG, "*****************************\n");
663 LOG (GNUNET_ERROR_TYPE_DEBUG, "******* RECONNECT *******\n");
664 LOG (GNUNET_ERROR_TYPE_DEBUG, "*****************************\n");
665 LOG (GNUNET_ERROR_TYPE_DEBUG, "******** on %p *******\n", h);
666 LOG (GNUNET_ERROR_TYPE_DEBUG, "*****************************\n");
671 GNUNET_CLIENT_notify_transmit_ready_cancel (h->th);
674 if (NULL != h->client)
676 GNUNET_CLIENT_disconnect (h->client);
680 h->client = GNUNET_CLIENT_connect ("cadet", h->cfg);
681 if (h->client == NULL)
683 h->reconnect_task = GNUNET_SCHEDULER_add_delayed (h->reconnect_time,
686 GNUNET_TIME_relative_min (GNUNET_TIME_UNIT_SECONDS,
687 GNUNET_TIME_relative_multiply
688 (h->reconnect_time, 2));
689 LOG (GNUNET_ERROR_TYPE_DEBUG, "Next retry in %s\n",
690 GNUNET_STRINGS_relative_time_to_string (h->reconnect_time,
697 h->reconnect_time = GNUNET_TIME_UNIT_MILLISECONDS;
704 * Reconnect callback: tries to reconnect again after a failer previous
706 * @param cls closure (cadet handle)
707 * @param tc task context
710 reconnect_cbk (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
712 struct GNUNET_CADET_Handle *h = cls;
714 h->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
715 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
722 * Reconnect to the service, retransmit all infomation to try to restore the
725 * @param h handle to the cadet
727 * @return GNUNET_YES in case of sucess, GNUNET_NO otherwise (service down...)
730 reconnect (struct GNUNET_CADET_Handle *h)
732 struct GNUNET_CADET_Channel *ch;
733 struct GNUNET_CADET_Channel *next;
735 LOG (GNUNET_ERROR_TYPE_DEBUG,
736 "Requested RECONNECT, destroying all channels\n");
737 h->in_receive = GNUNET_NO;
738 for (ch = h->channels_head; NULL != ch; ch = next)
741 destroy_channel (ch, GNUNET_YES);
743 if (GNUNET_SCHEDULER_NO_TASK == h->reconnect_task)
744 h->reconnect_task = GNUNET_SCHEDULER_add_delayed (h->reconnect_time,
749 /******************************************************************************/
750 /*********************** RECEIVE HANDLERS ****************************/
751 /******************************************************************************/
754 * Process the new channel notification and add it to the channels in the handle
756 * @param h The cadet handle
757 * @param msg A message with the details of the new incoming channel
760 process_channel_created (struct GNUNET_CADET_Handle *h,
761 const struct GNUNET_CADET_ChannelMessage *msg)
763 struct GNUNET_CADET_Channel *ch;
764 CADET_ChannelNumber chid;
767 chid = ntohl (msg->channel_id);
768 port = ntohl (msg->port);
769 LOG (GNUNET_ERROR_TYPE_DEBUG, "Creating incoming channel %X:%u\n", chid, port);
770 if (chid < GNUNET_CADET_LOCAL_CHANNEL_ID_SERV)
775 if (NULL != h->new_channel)
779 ch = create_channel (h, chid);
780 ch->allow_send = GNUNET_NO;
781 ch->peer = GNUNET_PEER_intern (&msg->peer);
785 ch->options = ntohl (msg->opt);
787 LOG (GNUNET_ERROR_TYPE_DEBUG, " created channel %p\n", ch);
788 ctx = h->new_channel (h->cls, ch, &msg->peer, ch->port, ch->options);
791 LOG (GNUNET_ERROR_TYPE_DEBUG, "User notified\n");
795 struct GNUNET_CADET_ChannelMessage d_msg;
797 LOG (GNUNET_ERROR_TYPE_DEBUG, "No handler for incoming channels\n");
799 d_msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DESTROY);
800 d_msg.header.size = htons (sizeof (struct GNUNET_CADET_ChannelMessage));
801 d_msg.channel_id = msg->channel_id;
802 memset (&d_msg.peer, 0, sizeof (struct GNUNET_PeerIdentity));
806 send_packet (h, &d_msg.header, NULL);
813 * Process the channel destroy notification and free associated resources
815 * @param h The cadet handle
816 * @param msg A message with the details of the channel being destroyed
819 process_channel_destroy (struct GNUNET_CADET_Handle *h,
820 const struct GNUNET_CADET_ChannelMessage *msg)
822 struct GNUNET_CADET_Channel *ch;
823 CADET_ChannelNumber chid;
825 LOG (GNUNET_ERROR_TYPE_DEBUG, "Channel Destroy received from service\n");
826 chid = ntohl (msg->channel_id);
827 ch = retrieve_channel (h, chid);
831 LOG (GNUNET_ERROR_TYPE_DEBUG, "channel %X unknown\n", chid);
834 LOG (GNUNET_ERROR_TYPE_DEBUG, " destroying channel %X\n", ch->chid);
835 destroy_channel (ch, GNUNET_YES);
840 * Process the incoming data packets, call appropriate handlers.
842 * @param h The cadet handle
843 * @param message A message encapsulating the data
846 process_incoming_data (struct GNUNET_CADET_Handle *h,
847 const struct GNUNET_MessageHeader *message)
849 const struct GNUNET_MessageHeader *payload;
850 const struct GNUNET_CADET_MessageHandler *handler;
851 struct GNUNET_CADET_LocalData *dmsg;
852 struct GNUNET_CADET_Channel *ch;
857 LOG (GNUNET_ERROR_TYPE_DEBUG, "Got a data message!\n");
858 dmsg = (struct GNUNET_CADET_LocalData *) message;
859 ch = retrieve_channel (h, ntohl (dmsg->id));
866 payload = (struct GNUNET_MessageHeader *) &dmsg[1];
867 LOG (GNUNET_ERROR_TYPE_DEBUG, " %s data on channel %s [%X]\n",
868 GC_f2s (ch->chid >= GNUNET_CADET_LOCAL_CHANNEL_ID_SERV),
869 GNUNET_i2s (GNUNET_PEER_resolve2 (ch->peer)), ntohl (dmsg->id));
871 size = ntohs (message->size);
872 LOG (GNUNET_ERROR_TYPE_DEBUG, " %u bytes\n", size);
876 /* Channel was ignored/destroyed, probably service didn't get it yet */
877 LOG (GNUNET_ERROR_TYPE_DEBUG, " ignored!\n");
880 type = ntohs (payload->type);
881 size = ntohs (payload->size);
882 LOG (GNUNET_ERROR_TYPE_DEBUG, " payload type %s\n", GC_m2s (type));
883 for (i = 0; i < h->n_handlers; i++)
885 handler = &h->message_handlers[i];
886 LOG (GNUNET_ERROR_TYPE_DEBUG, " checking handler for type %u\n",
888 if (handler->type == type)
891 handler->callback (h->cls, ch, &ch->ctx, payload))
893 LOG (GNUNET_ERROR_TYPE_DEBUG, "callback caused disconnection\n");
894 GNUNET_CADET_channel_destroy (ch);
899 LOG (GNUNET_ERROR_TYPE_DEBUG,
900 "callback completed successfully\n");
909 * Process a local ACK message, enabling the client to send
910 * more data to the service.
912 * @param h Cadet handle.
913 * @param message Message itself.
916 process_ack (struct GNUNET_CADET_Handle *h,
917 const struct GNUNET_MessageHeader *message)
919 struct GNUNET_CADET_LocalAck *msg;
920 struct GNUNET_CADET_Channel *ch;
921 CADET_ChannelNumber chid;
923 LOG (GNUNET_ERROR_TYPE_DEBUG, "Got an ACK!\n");
924 msg = (struct GNUNET_CADET_LocalAck *) message;
925 chid = ntohl (msg->channel_id);
926 ch = retrieve_channel (h, chid);
929 LOG (GNUNET_ERROR_TYPE_DEBUG, "ACK on unknown channel %X\n", chid);
932 LOG (GNUNET_ERROR_TYPE_DEBUG, " on channel %X!\n", ch->chid);
933 ch->allow_send = GNUNET_YES;
934 if (NULL == h->th && 0 < ch->packet_size)
936 LOG (GNUNET_ERROR_TYPE_DEBUG, " tmt rdy was NULL, requesting!\n");
937 h->th = GNUNET_CLIENT_notify_transmit_ready (h->client, ch->packet_size,
938 GNUNET_TIME_UNIT_FOREVER_REL,
939 GNUNET_YES, &send_callback, h);
945 * Process a local reply about info on all channels, pass info to the user.
947 * @param h Cadet handle.
948 * @param message Message itself.
951 // process_get_channels (struct GNUNET_CADET_Handle *h,
952 // const struct GNUNET_MessageHeader *message)
954 // struct GNUNET_CADET_LocalInfo *msg;
956 // GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Get Channels messasge received\n");
958 // if (NULL == h->channels_cb)
960 // GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " ignored\n");
964 // msg = (struct GNUNET_CADET_LocalInfo *) message;
965 // if (ntohs (message->size) !=
966 // (sizeof (struct GNUNET_CADET_LocalInfo) +
967 // sizeof (struct GNUNET_PeerIdentity)))
969 // GNUNET_break_op (0);
970 // GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
971 // "Get channels message: size %hu - expected %u\n",
972 // ntohs (message->size),
973 // sizeof (struct GNUNET_CADET_LocalInfo));
976 // h->channels_cb (h->channels_cls,
977 // ntohl (msg->channel_id),
979 // &msg->destination);
985 * Process a local monitor_channel reply, pass info to the user.
987 * @param h Cadet handle.
988 * @param message Message itself.
991 // process_show_channel (struct GNUNET_CADET_Handle *h,
992 // const struct GNUNET_MessageHeader *message)
994 // struct GNUNET_CADET_LocalInfo *msg;
997 // GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Show Channel messasge received\n");
999 // if (NULL == h->channel_cb)
1001 // GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " ignored\n");
1005 // /* Verify message sanity */
1006 // msg = (struct GNUNET_CADET_LocalInfo *) message;
1007 // esize = sizeof (struct GNUNET_CADET_LocalInfo);
1008 // if (ntohs (message->size) != esize)
1010 // GNUNET_break_op (0);
1011 // GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1012 // "Show channel message: size %hu - expected %u\n",
1013 // ntohs (message->size),
1016 // h->channel_cb (h->channel_cls, NULL, NULL);
1017 // h->channel_cb = NULL;
1018 // h->channel_cls = NULL;
1023 // h->channel_cb (h->channel_cls,
1024 // &msg->destination,
1031 * Process a local reply about info on all tunnels, pass info to the user.
1033 * @param h Cadet handle.
1034 * @param message Message itself.
1037 process_get_peers (struct GNUNET_CADET_Handle *h,
1038 const struct GNUNET_MessageHeader *message)
1040 struct GNUNET_CADET_LocalInfoPeer *msg;
1043 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Get Peer messasge received\n");
1045 if (NULL == h->info_cb.peers_cb)
1047 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ignored\n");
1051 size = ntohs (message->size);
1052 if (sizeof (struct GNUNET_CADET_LocalInfoPeer) > size)
1054 h->info_cb.peers_cb (h->info_cls, NULL, -1, 0, 0);
1055 h->info_cb.peers_cb = NULL;
1060 msg = (struct GNUNET_CADET_LocalInfoPeer *) message;
1061 h->info_cb.peers_cb (h->info_cls, &msg->destination,
1062 (int) ntohs (msg->tunnel),
1063 (unsigned int ) ntohs (msg->paths),
1069 * Process a local peer info reply, pass info to the user.
1071 * @param h Cadet handle.
1072 * @param message Message itself.
1075 process_get_peer (struct GNUNET_CADET_Handle *h,
1076 const struct GNUNET_MessageHeader *message)
1078 struct GNUNET_CADET_LocalInfoTunnel *msg;
1083 struct GNUNET_CADET_Hash *conns;
1084 CADET_ChannelNumber *chns;
1086 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Get Tunnel messasge received\n");
1087 if (NULL == h->info_cb.tunnel_cb)
1089 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ignored\n");
1093 /* Verify message sanity */
1094 msg = (struct GNUNET_CADET_LocalInfoTunnel *) message;
1095 msize = ntohs (message->size);
1096 esize = sizeof (struct GNUNET_CADET_LocalInfoTunnel);
1099 GNUNET_break_op (0);
1100 h->info_cb.tunnel_cb (h->info_cls, NULL, 0, 0, NULL, NULL, 0, 0);
1103 ch_n = ntohl (msg->channels);
1104 c_n = ntohl (msg->connections);
1105 esize += ch_n * sizeof (CADET_ChannelNumber);
1106 esize += c_n * sizeof (struct GNUNET_CADET_Hash);
1109 GNUNET_break_op (0);
1110 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "m:%u, e: %u (%u ch, %u conn)\n",
1111 msize, esize, ch_n, c_n);
1112 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "%u (%u ch, %u conn)\n",
1113 sizeof (struct GNUNET_CADET_LocalInfoTunnel),
1114 sizeof (CADET_ChannelNumber), sizeof (struct GNUNET_HashCode));
1115 h->info_cb.tunnel_cb (h->info_cls, NULL, 0, 0, NULL, NULL, 0, 0);
1119 /* Call Callback with tunnel info. */
1120 conns = (struct GNUNET_CADET_Hash *) &msg[1];
1121 chns = (CADET_ChannelNumber *) &conns[c_n];
1122 h->info_cb.tunnel_cb (h->info_cls, &msg->destination,
1123 ch_n, c_n, chns, conns,
1124 ntohs (msg->estate), ntohs (msg->cstate));
1127 h->info_cb.tunnel_cb = NULL;
1133 * Process a local reply about info on all tunnels, pass info to the user.
1135 * @param h Cadet handle.
1136 * @param message Message itself.
1139 process_get_tunnels (struct GNUNET_CADET_Handle *h,
1140 const struct GNUNET_MessageHeader *message)
1142 struct GNUNET_CADET_LocalInfoTunnel *msg;
1145 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Get Tunnels messasge received\n");
1147 if (NULL == h->info_cb.tunnels_cb)
1149 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ignored\n");
1153 size = ntohs (message->size);
1154 if (sizeof (struct GNUNET_CADET_LocalInfoTunnel) > size)
1156 h->info_cb.tunnels_cb (h->info_cls, NULL, 0, 0, 0, 0);
1157 h->info_cb.tunnels_cb = NULL;
1162 msg = (struct GNUNET_CADET_LocalInfoTunnel *) message;
1163 h->info_cb.tunnels_cb (h->info_cls, &msg->destination,
1164 ntohl (msg->channels), ntohl (msg->connections),
1165 ntohs (msg->estate), ntohs (msg->cstate));
1171 * Process a local tunnel info reply, pass info to the user.
1173 * @param h Cadet handle.
1174 * @param message Message itself.
1177 process_get_tunnel (struct GNUNET_CADET_Handle *h,
1178 const struct GNUNET_MessageHeader *message)
1180 struct GNUNET_CADET_LocalInfoTunnel *msg;
1185 struct GNUNET_CADET_Hash *conns;
1186 CADET_ChannelNumber *chns;
1188 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Get Tunnel messasge received\n");
1189 if (NULL == h->info_cb.tunnel_cb)
1191 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ignored\n");
1195 /* Verify message sanity */
1196 msg = (struct GNUNET_CADET_LocalInfoTunnel *) message;
1197 msize = ntohs (message->size);
1198 esize = sizeof (struct GNUNET_CADET_LocalInfoTunnel);
1201 GNUNET_break_op (0);
1202 h->info_cb.tunnel_cb (h->info_cls, NULL, 0, 0, NULL, NULL, 0, 0);
1205 ch_n = ntohl (msg->channels);
1206 c_n = ntohl (msg->connections);
1207 esize += ch_n * sizeof (CADET_ChannelNumber);
1208 esize += c_n * sizeof (struct GNUNET_CADET_Hash);
1211 GNUNET_break_op (0);
1212 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "m:%u, e: %u (%u ch, %u conn)\n",
1213 msize, esize, ch_n, c_n);
1214 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "%u (%u ch, %u conn)\n",
1215 sizeof (struct GNUNET_CADET_LocalInfoTunnel),
1216 sizeof (CADET_ChannelNumber), sizeof (struct GNUNET_HashCode));
1217 h->info_cb.tunnel_cb (h->info_cls, NULL, 0, 0, NULL, NULL, 0, 0);
1221 /* Call Callback with tunnel info. */
1222 conns = (struct GNUNET_CADET_Hash *) &msg[1];
1223 chns = (CADET_ChannelNumber *) &conns[c_n];
1224 h->info_cb.tunnel_cb (h->info_cls, &msg->destination,
1225 ch_n, c_n, chns, conns,
1226 ntohs (msg->estate), ntohs (msg->cstate));
1229 h->info_cb.tunnel_cb = NULL;
1235 * Function to process all messages received from the service
1237 * @param cls closure
1238 * @param msg message received, NULL on timeout or fatal error
1241 msg_received (void *cls, const struct GNUNET_MessageHeader *msg)
1243 struct GNUNET_CADET_Handle *h = cls;
1248 LOG (GNUNET_ERROR_TYPE_DEBUG,
1249 "Cadet service disconnected, reconnecting\n", h);
1253 type = ntohs (msg->type);
1254 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n");
1255 LOG (GNUNET_ERROR_TYPE_DEBUG, "Received a message: %s\n",
1259 /* Notify of a new incoming channel */
1260 case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_CREATE:
1261 process_channel_created (h, (struct GNUNET_CADET_ChannelMessage *) msg);
1263 /* Notify of a channel disconnection */
1264 case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DESTROY: /* TODO separate(gid problem)*/
1265 case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_NACK:
1266 process_channel_destroy (h, (struct GNUNET_CADET_ChannelMessage *) msg);
1268 case GNUNET_MESSAGE_TYPE_CADET_LOCAL_DATA:
1269 process_incoming_data (h, msg);
1271 case GNUNET_MESSAGE_TYPE_CADET_LOCAL_ACK:
1272 process_ack (h, msg);
1274 // case GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_CHANNELS:
1275 // process_get_channels (h, msg);
1277 // case GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_CHANNEL:
1278 // process_show_channel (h, msg);
1280 case GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEERS:
1281 process_get_peers (h, msg);
1283 case GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEER:
1284 process_get_peer (h, msg);
1286 case GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNELS:
1287 process_get_tunnels (h, msg);
1289 case GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNEL:
1290 process_get_tunnel (h, msg);
1292 // case GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_CHANNEL:
1293 // process_show_channel (h, msg);
1296 /* We shouldn't get any other packages, log and ignore */
1297 LOG (GNUNET_ERROR_TYPE_WARNING,
1298 "unsolicited message form service (type %s)\n",
1299 GC_m2s (ntohs (msg->type)));
1301 LOG (GNUNET_ERROR_TYPE_DEBUG, "message processed\n");
1302 if (GNUNET_YES == h->in_receive)
1304 GNUNET_CLIENT_receive (h->client, &msg_received, h,
1305 GNUNET_TIME_UNIT_FOREVER_REL);
1309 LOG (GNUNET_ERROR_TYPE_DEBUG,
1310 "in receive off, not calling CLIENT_receive\n");
1315 /******************************************************************************/
1316 /************************ SEND FUNCTIONS ****************************/
1317 /******************************************************************************/
1320 * Function called to send a message to the service.
1321 * "buf" will be NULL and "size" zero if the socket was closed for writing in
1324 * @param cls closure, the cadet handle
1325 * @param size number of bytes available in buf
1326 * @param buf where the callee should write the connect message
1327 * @return number of bytes written to buf
1330 send_callback (void *cls, size_t size, void *buf)
1332 struct GNUNET_CADET_Handle *h = cls;
1333 struct GNUNET_CADET_TransmitHandle *th;
1334 struct GNUNET_CADET_TransmitHandle *next;
1335 struct GNUNET_CADET_Channel *ch;
1341 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n");
1342 LOG (GNUNET_ERROR_TYPE_DEBUG, "# Send packet() Buffer %u\n", size);
1343 if ((0 == size) || (NULL == buf))
1345 LOG (GNUNET_ERROR_TYPE_DEBUG, "# Received NULL send callback on %p\n", h);
1352 nsize = message_ready_size (h);
1353 while ((NULL != (th = next)) && (0 < nsize) && (size >= nsize))
1356 if (GNUNET_YES == th_is_payload (th))
1358 struct GNUNET_CADET_LocalData *dmsg;
1359 struct GNUNET_MessageHeader *mh;
1361 LOG (GNUNET_ERROR_TYPE_DEBUG, "# payload\n");
1362 if (GNUNET_NO == ch->allow_send)
1364 /* This channel is not ready to transmit yet, try next message */
1368 ch->packet_size = 0;
1369 GNUNET_assert (size >= th->size);
1370 dmsg = (struct GNUNET_CADET_LocalData *) cbuf;
1371 mh = (struct GNUNET_MessageHeader *) &dmsg[1];
1372 psize = th->notify (th->notify_cls,
1373 size - sizeof (struct GNUNET_CADET_LocalData),
1377 psize += sizeof (struct GNUNET_CADET_LocalData);
1378 GNUNET_assert (size >= psize);
1379 dmsg->header.size = htons (psize);
1380 dmsg->id = htonl (ch->chid);
1381 dmsg->header.type = htons (GNUNET_MESSAGE_TYPE_CADET_LOCAL_DATA);
1382 LOG (GNUNET_ERROR_TYPE_DEBUG, "# payload type %s\n",
1383 GC_m2s (ntohs (mh->type)));
1384 ch->allow_send = GNUNET_NO;
1388 LOG (GNUNET_ERROR_TYPE_DEBUG,
1389 "# callback returned size 0, "
1390 "application canceled transmission\n");
1395 struct GNUNET_MessageHeader *mh = (struct GNUNET_MessageHeader *) &th[1];
1397 LOG (GNUNET_ERROR_TYPE_DEBUG, "# cadet internal traffic, type %s\n",
1398 GC_m2s (ntohs (mh->type)));
1399 memcpy (cbuf, &th[1], th->size);
1402 if (th->timeout_task != GNUNET_SCHEDULER_NO_TASK)
1403 GNUNET_SCHEDULER_cancel (th->timeout_task);
1404 GNUNET_CONTAINER_DLL_remove (h->th_head, h->th_tail, th);
1407 nsize = message_ready_size (h);
1412 LOG (GNUNET_ERROR_TYPE_DEBUG, "# total size: %u\n", tsize);
1414 size = message_ready_size (h);
1417 LOG (GNUNET_ERROR_TYPE_DEBUG, "# next size: %u\n", size);
1419 GNUNET_CLIENT_notify_transmit_ready (h->client, size,
1420 GNUNET_TIME_UNIT_FOREVER_REL,
1421 GNUNET_YES, &send_callback, h);
1425 if (NULL != h->th_head)
1426 LOG (GNUNET_ERROR_TYPE_DEBUG, "# can't transmit any more\n");
1428 LOG (GNUNET_ERROR_TYPE_DEBUG, "# nothing left to transmit\n");
1430 if (GNUNET_NO == h->in_receive)
1432 LOG (GNUNET_ERROR_TYPE_DEBUG, "# start receiving from service\n");
1433 h->in_receive = GNUNET_YES;
1434 GNUNET_CLIENT_receive (h->client, &msg_received, h,
1435 GNUNET_TIME_UNIT_FOREVER_REL);
1437 LOG (GNUNET_ERROR_TYPE_DEBUG, "# Send packet() END\n");
1443 * Auxiliary function to send an already constructed packet to the service.
1444 * Takes care of creating a new queue element, copying the message and
1445 * calling the tmt_rdy function if necessary.
1447 * @param h cadet handle
1448 * @param msg message to transmit
1449 * @param channel channel this send is related to (NULL if N/A)
1452 send_packet (struct GNUNET_CADET_Handle *h,
1453 const struct GNUNET_MessageHeader *msg,
1454 struct GNUNET_CADET_Channel *channel)
1456 struct GNUNET_CADET_TransmitHandle *th;
1459 LOG (GNUNET_ERROR_TYPE_DEBUG, " Sending message to service: %s\n",
1460 GC_m2s(ntohs(msg->type)));
1461 msize = ntohs (msg->size);
1462 th = GNUNET_malloc (sizeof (struct GNUNET_CADET_TransmitHandle) + msize);
1463 th->timeout = GNUNET_TIME_UNIT_FOREVER_ABS;
1465 th->channel = channel;
1466 memcpy (&th[1], msg, msize);
1467 add_to_queue (h, th);
1468 LOG (GNUNET_ERROR_TYPE_DEBUG, " queued\n");
1471 LOG (GNUNET_ERROR_TYPE_DEBUG, " calling ntfy tmt rdy for %u bytes\n", msize);
1473 GNUNET_CLIENT_notify_transmit_ready (h->client, msize,
1474 GNUNET_TIME_UNIT_FOREVER_REL,
1475 GNUNET_YES, &send_callback, h);
1479 /******************************************************************************/
1480 /********************** API CALL DEFINITIONS *************************/
1481 /******************************************************************************/
1483 struct GNUNET_CADET_Handle *
1484 GNUNET_CADET_connect (const struct GNUNET_CONFIGURATION_Handle *cfg, void *cls,
1485 GNUNET_CADET_InboundChannelNotificationHandler new_channel,
1486 GNUNET_CADET_ChannelEndHandler cleaner,
1487 const struct GNUNET_CADET_MessageHandler *handlers,
1488 const uint32_t *ports)
1490 struct GNUNET_CADET_Handle *h;
1492 LOG (GNUNET_ERROR_TYPE_DEBUG, "GNUNET_CADET_connect()\n");
1493 h = GNUNET_new (struct GNUNET_CADET_Handle);
1494 LOG (GNUNET_ERROR_TYPE_DEBUG, " addr %p\n", h);
1496 h->new_channel = new_channel;
1497 h->cleaner = cleaner;
1498 h->client = GNUNET_CLIENT_connect ("cadet", cfg);
1499 if (h->client == NULL)
1506 h->message_handlers = handlers;
1508 h->next_chid = GNUNET_CADET_LOCAL_CHANNEL_ID_CLI;
1509 h->reconnect_time = GNUNET_TIME_UNIT_MILLISECONDS;
1510 h->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
1512 if (NULL != ports && ports[0] != 0 && NULL == new_channel)
1515 LOG (GNUNET_ERROR_TYPE_DEBUG,
1516 "no new channel handler given, ports parameter is useless!!\n");
1518 if ((NULL == ports || ports[0] == 0) && NULL != new_channel)
1521 LOG (GNUNET_ERROR_TYPE_DEBUG,
1522 "no ports given, new channel handler will never be called!!\n");
1524 /* count handlers */
1525 for (h->n_handlers = 0;
1526 handlers && handlers[h->n_handlers].type;
1528 for (h->n_ports = 0;
1529 ports && ports[h->n_ports];
1532 LOG (GNUNET_ERROR_TYPE_DEBUG, "GNUNET_CADET_connect() END\n");
1538 GNUNET_CADET_disconnect (struct GNUNET_CADET_Handle *handle)
1540 struct GNUNET_CADET_Channel *ch;
1541 struct GNUNET_CADET_Channel *aux;
1542 struct GNUNET_CADET_TransmitHandle *th;
1544 LOG (GNUNET_ERROR_TYPE_DEBUG, "CADET DISCONNECT\n");
1546 ch = handle->channels_head;
1550 if (ch->chid < GNUNET_CADET_LOCAL_CHANNEL_ID_SERV)
1553 LOG (GNUNET_ERROR_TYPE_DEBUG, "channel %X not destroyed\n", ch->chid);
1555 destroy_channel (ch, GNUNET_YES);
1558 while ( (th = handle->th_head) != NULL)
1560 struct GNUNET_MessageHeader *msg;
1562 /* Make sure it is an allowed packet (everything else should have been
1563 * already canceled).
1565 GNUNET_break (GNUNET_NO == th_is_payload (th));
1566 msg = (struct GNUNET_MessageHeader *) &th[1];
1567 switch (ntohs(msg->type))
1569 case GNUNET_MESSAGE_TYPE_CADET_LOCAL_CONNECT:
1570 case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_CREATE:
1571 case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DESTROY:
1572 case GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_CHANNELS:
1573 case GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_CHANNEL:
1574 case GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEER:
1575 case GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEERS:
1576 case GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNEL:
1577 case GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNELS:
1581 LOG (GNUNET_ERROR_TYPE_ERROR, "unexpected msg %u\n",
1585 GNUNET_CONTAINER_DLL_remove (handle->th_head, handle->th_tail, th);
1589 if (NULL != handle->th)
1591 GNUNET_CLIENT_notify_transmit_ready_cancel (handle->th);
1594 if (NULL != handle->client)
1596 GNUNET_CLIENT_disconnect (handle->client);
1597 handle->client = NULL;
1599 if (GNUNET_SCHEDULER_NO_TASK != handle->reconnect_task)
1601 GNUNET_SCHEDULER_cancel(handle->reconnect_task);
1602 handle->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
1604 GNUNET_free (handle);
1609 * Create a new channel towards a remote peer.
1611 * If the destination port is not open by any peer or the destination peer
1612 * does not accept the channel, #GNUNET_CADET_ChannelEndHandler will be called
1615 * @param h cadet handle
1616 * @param channel_ctx client's channel context to associate with the channel
1617 * @param peer peer identity the channel should go to
1618 * @param port Port number.
1619 * @param options CadetOption flag field, with all desired option bits set to 1.
1621 * @return handle to the channel
1623 struct GNUNET_CADET_Channel *
1624 GNUNET_CADET_channel_create (struct GNUNET_CADET_Handle *h,
1626 const struct GNUNET_PeerIdentity *peer,
1628 enum GNUNET_CADET_ChannelOption options)
1630 struct GNUNET_CADET_Channel *ch;
1631 struct GNUNET_CADET_ChannelMessage msg;
1633 LOG (GNUNET_ERROR_TYPE_DEBUG,
1634 "Creating new channel to %s:%u\n",
1635 GNUNET_i2s (peer), port);
1636 ch = create_channel (h, 0);
1637 LOG (GNUNET_ERROR_TYPE_DEBUG, " at %p\n", ch);
1638 LOG (GNUNET_ERROR_TYPE_DEBUG, " number %X\n", ch->chid);
1639 ch->ctx = channel_ctx;
1640 ch->peer = GNUNET_PEER_intern (peer);
1641 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CHANNEL_CREATE);
1642 msg.header.size = htons (sizeof (struct GNUNET_CADET_ChannelMessage));
1643 msg.channel_id = htonl (ch->chid);
1644 msg.port = htonl (port);
1646 msg.opt = htonl (options);
1648 send_packet (h, &msg.header, ch);
1654 GNUNET_CADET_channel_destroy (struct GNUNET_CADET_Channel *channel)
1656 struct GNUNET_CADET_Handle *h;
1657 struct GNUNET_CADET_ChannelMessage msg;
1658 struct GNUNET_CADET_TransmitHandle *th;
1660 LOG (GNUNET_ERROR_TYPE_DEBUG, "Destroying channel\n");
1663 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DESTROY);
1664 msg.header.size = htons (sizeof (struct GNUNET_CADET_ChannelMessage));
1665 msg.channel_id = htonl (channel->chid);
1666 memset (&msg.peer, 0, sizeof (struct GNUNET_PeerIdentity));
1672 struct GNUNET_CADET_TransmitHandle *aux;
1673 if (th->channel == channel)
1676 /* FIXME call the handler? */
1677 if (GNUNET_YES == th_is_payload (th))
1678 th->notify (th->notify_cls, 0, NULL);
1679 GNUNET_CONTAINER_DLL_remove (h->th_head, h->th_tail, th);
1687 destroy_channel (channel, GNUNET_YES);
1688 send_packet (h, &msg.header, NULL);
1693 * Get information about a channel.
1695 * @param channel Channel handle.
1696 * @param option Query (GNUNET_CADET_OPTION_*).
1697 * @param ... dependant on option, currently not used
1699 * @return Union with an answer to the query.
1701 const union GNUNET_CADET_ChannelInfo *
1702 GNUNET_CADET_channel_get_info (struct GNUNET_CADET_Channel *channel,
1703 enum GNUNET_CADET_ChannelOption option, ...)
1705 static int bool_flag;
1706 const union GNUNET_CADET_ChannelInfo *ret;
1710 case GNUNET_CADET_OPTION_NOBUFFER:
1711 case GNUNET_CADET_OPTION_RELIABLE:
1712 case GNUNET_CADET_OPTION_OOORDER:
1713 if (0 != (option & channel->options))
1714 bool_flag = GNUNET_YES;
1716 bool_flag = GNUNET_NO;
1717 ret = (const union GNUNET_CADET_ChannelInfo *) &bool_flag;
1719 case GNUNET_CADET_OPTION_PEER:
1720 ret = (const union GNUNET_CADET_ChannelInfo *) GNUNET_PEER_resolve2 (channel->peer);
1730 struct GNUNET_CADET_TransmitHandle *
1731 GNUNET_CADET_notify_transmit_ready (struct GNUNET_CADET_Channel *channel, int cork,
1732 struct GNUNET_TIME_Relative maxdelay,
1734 GNUNET_CONNECTION_TransmitReadyNotify notify,
1737 struct GNUNET_CADET_TransmitHandle *th;
1739 GNUNET_assert (NULL != channel);
1740 LOG (GNUNET_ERROR_TYPE_DEBUG, "CADET NOTIFY TRANSMIT READY\n");
1741 LOG (GNUNET_ERROR_TYPE_DEBUG, " on channel %X\n", channel->chid);
1742 LOG (GNUNET_ERROR_TYPE_DEBUG, " allow_send %d\n", channel->allow_send);
1743 if (channel->chid >= GNUNET_CADET_LOCAL_CHANNEL_ID_SERV)
1744 LOG (GNUNET_ERROR_TYPE_DEBUG, " to origin\n");
1746 LOG (GNUNET_ERROR_TYPE_DEBUG, " to destination\n");
1747 LOG (GNUNET_ERROR_TYPE_DEBUG, " payload size %u\n", notify_size);
1748 GNUNET_assert (NULL != notify);
1749 GNUNET_assert (0 == channel->packet_size); // Only one data packet allowed
1750 th = GNUNET_new (struct GNUNET_CADET_TransmitHandle);
1751 th->channel = channel;
1752 th->timeout = GNUNET_TIME_relative_to_absolute (maxdelay);
1753 th->size = notify_size + sizeof (struct GNUNET_CADET_LocalData);
1754 channel->packet_size = th->size;
1755 LOG (GNUNET_ERROR_TYPE_DEBUG, " total size %u\n", th->size);
1756 th->notify = notify;
1757 th->notify_cls = notify_cls;
1758 add_to_queue (channel->cadet, th);
1759 if (NULL != channel->cadet->th)
1761 if (GNUNET_NO == channel->allow_send)
1763 LOG (GNUNET_ERROR_TYPE_DEBUG, " call client notify tmt rdy\n");
1764 channel->cadet->th =
1765 GNUNET_CLIENT_notify_transmit_ready (channel->cadet->client, th->size,
1766 GNUNET_TIME_UNIT_FOREVER_REL,
1767 GNUNET_YES, &send_callback,
1769 LOG (GNUNET_ERROR_TYPE_DEBUG, "CADET NOTIFY TRANSMIT READY END\n");
1775 GNUNET_CADET_notify_transmit_ready_cancel (struct GNUNET_CADET_TransmitHandle *th)
1777 struct GNUNET_CADET_Handle *cadet;
1779 th->channel->packet_size = 0;
1780 cadet = th->channel->cadet;
1781 if (th->timeout_task != GNUNET_SCHEDULER_NO_TASK)
1782 GNUNET_SCHEDULER_cancel (th->timeout_task);
1783 GNUNET_CONTAINER_DLL_remove (cadet->th_head, cadet->th_tail, th);
1785 if ((0 == message_ready_size (cadet)) && (NULL != cadet->th))
1787 /* queue empty, no point in asking for transmission */
1788 GNUNET_CLIENT_notify_transmit_ready_cancel (cadet->th);
1795 GNUNET_CADET_receive_done (struct GNUNET_CADET_Channel *channel)
1802 send_info_request (struct GNUNET_CADET_Handle *h, uint16_t type)
1804 struct GNUNET_MessageHeader msg;
1806 msg.size = htons (sizeof (msg));
1807 msg.type = htons (type);
1808 send_packet (h, &msg, NULL);
1813 * Request information about peers known to the running cadet service.
1814 * The callback will be called for every peer known to the service.
1815 * Only one info request (of any kind) can be active at once.
1818 * WARNING: unstable API, likely to change in the future!
1820 * @param h Handle to the cadet peer.
1821 * @param callback Function to call with the requested data.
1822 * @param callback_cls Closure for @c callback.
1824 * @return #GNUNET_OK / #GNUNET_SYSERR
1827 GNUNET_CADET_get_peers (struct GNUNET_CADET_Handle *h,
1828 GNUNET_CADET_PeersCB callback,
1831 if (NULL != h->info_cb.peers_cb)
1834 return GNUNET_SYSERR;
1836 send_info_request (h, GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEERS);
1837 h->info_cb.peers_cb = callback;
1838 h->info_cls = callback_cls;
1844 * Cancel a peer info request. The callback will not be called (anymore).
1846 * WARNING: unstable API, likely to change in the future!
1848 * @param h Cadet handle.
1850 * @return Closure given to GNUNET_CADET_get_peers.
1853 GNUNET_CADET_get_peers_cancel (struct GNUNET_CADET_Handle *h)
1858 h->info_cb.peers_cb = NULL;
1865 * Request information about a peer known to the running cadet peer.
1866 * The callback will be called for the tunnel once.
1867 * Only one info request (of any kind) can be active at once.
1869 * WARNING: unstable API, likely to change in the future!
1871 * @param h Handle to the cadet peer.
1872 * @param id Peer whose tunnel to examine.
1873 * @param callback Function to call with the requested data.
1874 * @param callback_cls Closure for @c callback.
1876 * @return #GNUNET_OK / #GNUNET_SYSERR
1879 GNUNET_CADET_get_peer (struct GNUNET_CADET_Handle *h,
1880 const struct GNUNET_PeerIdentity *id,
1881 GNUNET_CADET_PeerCB callback,
1884 struct GNUNET_CADET_LocalInfo msg;
1886 if (NULL != h->info_cb.peer_cb)
1889 return GNUNET_SYSERR;
1892 memset (&msg, 0, sizeof (msg));
1893 msg.header.size = htons (sizeof (msg));
1894 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEER);
1896 send_packet (h, &msg.header, NULL);
1897 h->info_cb.peer_cb = callback;
1898 h->info_cls = callback_cls;
1904 * Request information about tunnels of the running cadet peer.
1905 * The callback will be called for every tunnel of the service.
1906 * Only one info request (of any kind) can be active at once.
1908 * WARNING: unstable API, likely to change in the future!
1910 * @param h Handle to the cadet peer.
1911 * @param callback Function to call with the requested data.
1912 * @param callback_cls Closure for @c callback.
1914 * @return #GNUNET_OK / #GNUNET_SYSERR
1917 GNUNET_CADET_get_tunnels (struct GNUNET_CADET_Handle *h,
1918 GNUNET_CADET_TunnelsCB callback,
1921 if (NULL != h->info_cb.tunnels_cb)
1924 return GNUNET_SYSERR;
1926 send_info_request (h, GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNELS);
1927 h->info_cb.tunnels_cb = callback;
1928 h->info_cls = callback_cls;
1934 * Cancel a monitor request. The monitor callback will not be called.
1936 * @param h Cadet handle.
1938 * @return Closure given to GNUNET_CADET_get_tunnels.
1941 GNUNET_CADET_get_tunnels_cancel (struct GNUNET_CADET_Handle *h)
1945 h->info_cb.tunnels_cb = NULL;
1955 * Request information about a tunnel of the running cadet peer.
1956 * The callback will be called for the tunnel once.
1957 * Only one info request (of any kind) can be active at once.
1959 * WARNING: unstable API, likely to change in the future!
1961 * @param h Handle to the cadet peer.
1962 * @param id Peer whose tunnel to examine.
1963 * @param callback Function to call with the requested data.
1964 * @param callback_cls Closure for @c callback.
1966 * @return #GNUNET_OK / #GNUNET_SYSERR
1969 GNUNET_CADET_get_tunnel (struct GNUNET_CADET_Handle *h,
1970 const struct GNUNET_PeerIdentity *id,
1971 GNUNET_CADET_TunnelCB callback,
1974 struct GNUNET_CADET_LocalInfo msg;
1976 if (NULL != h->info_cb.tunnel_cb)
1979 return GNUNET_SYSERR;
1982 memset (&msg, 0, sizeof (msg));
1983 msg.header.size = htons (sizeof (msg));
1984 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNEL);
1986 send_packet (h, &msg.header, NULL);
1987 h->info_cb.tunnel_cb = callback;
1988 h->info_cls = callback_cls;
1994 * Request information about a specific channel of the running cadet peer.
1996 * WARNING: unstable API, likely to change in the future!
1997 * FIXME Add destination option.
1999 * @param h Handle to the cadet peer.
2000 * @param initiator ID of the owner of the channel.
2001 * @param channel_number Channel number.
2002 * @param callback Function to call with the requested data.
2003 * @param callback_cls Closure for @c callback.
2005 * @return #GNUNET_OK / #GNUNET_SYSERR
2008 GNUNET_CADET_show_channel (struct GNUNET_CADET_Handle *h,
2009 struct GNUNET_PeerIdentity *initiator,
2010 unsigned int channel_number,
2011 GNUNET_CADET_ChannelCB callback,
2014 struct GNUNET_CADET_LocalInfo msg;
2016 if (NULL != h->info_cb.channel_cb)
2019 return GNUNET_SYSERR;
2022 msg.header.size = htons (sizeof (msg));
2023 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_CHANNEL);
2024 msg.peer = *initiator;
2025 msg.channel_id = htonl (channel_number);
2026 // msg.reserved = 0;
2027 send_packet (h, &msg.header, NULL);
2028 h->info_cb.channel_cb = callback;
2029 h->info_cls = callback_cls;
2035 * Function called to notify a client about the connection
2036 * begin ready to queue more data. "buf" will be
2037 * NULL and "size" zero if the connection was closed for
2038 * writing in the meantime.
2040 * @param cls closure
2041 * @param size number of bytes available in buf
2042 * @param buf where the callee should write the message
2043 * @return number of bytes written to buf
2046 cadet_mq_ntr (void *cls, size_t size,
2049 struct GNUNET_MQ_Handle *mq = cls;
2050 struct CadetMQState *state = GNUNET_MQ_impl_state (mq);
2051 const struct GNUNET_MessageHeader *msg = GNUNET_MQ_impl_current (mq);
2057 GNUNET_MQ_inject_error (mq, GNUNET_MQ_ERROR_WRITE);
2060 msize = ntohs (msg->size);
2061 GNUNET_assert (msize <= size);
2062 memcpy (buf, msg, msize);
2063 GNUNET_MQ_impl_send_continue (mq);
2069 * Signature of functions implementing the
2070 * sending functionality of a message queue.
2072 * @param mq the message queue
2073 * @param msg the message to send
2074 * @param impl_state state of the implementation
2077 cadet_mq_send_impl (struct GNUNET_MQ_Handle *mq,
2078 const struct GNUNET_MessageHeader *msg, void *impl_state)
2080 struct CadetMQState *state = impl_state;
2082 GNUNET_assert (NULL == state->th);
2084 GNUNET_CADET_notify_transmit_ready (state->channel,
2085 /* FIXME: add option for corking */
2087 GNUNET_TIME_UNIT_FOREVER_REL,
2095 * Signature of functions implementing the
2096 * destruction of a message queue.
2097 * Implementations must not free 'mq', but should
2098 * take care of 'impl_state'.
2100 * @param mq the message queue to destroy
2101 * @param impl_state state of the implementation
2104 cadet_mq_destroy_impl (struct GNUNET_MQ_Handle *mq, void *impl_state)
2106 struct CadetMQState *state = impl_state;
2108 if (NULL != state->th)
2109 GNUNET_CADET_notify_transmit_ready_cancel (state->th);
2111 GNUNET_free (state);
2116 * Create a message queue for a cadet channel.
2117 * The message queue can only be used to transmit messages,
2118 * not to receive them.
2120 * @param channel the channel to create the message qeue for
2121 * @return a message queue to messages over the channel
2123 struct GNUNET_MQ_Handle *
2124 GNUNET_CADET_mq_create (struct GNUNET_CADET_Channel *channel)
2126 struct GNUNET_MQ_Handle *mq;
2127 struct CadetMQState *state;
2129 state = GNUNET_new (struct CadetMQState);
2130 state->channel = channel;
2132 mq = GNUNET_MQ_queue_for_callbacks (cadet_mq_send_impl,
2133 cadet_mq_destroy_impl,
2134 NULL, /* FIXME: cancel impl. */
2136 NULL, /* no msg handlers */
2137 NULL, /* no err handlers */
2138 NULL); /* no handler cls */