2 This file is part of GNUnet.
3 Copyright (C) 2011 GNUnet e.V.
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
21 * @file cadet/cadet_api.c
22 * @brief cadet api: client implementation of new cadet service
23 * @author Bartlomiej Polot
27 #include "gnunet_util_lib.h"
28 #include "gnunet_cadet_service.h"
30 #include "cadet_protocol.h"
32 #define LOG(kind,...) GNUNET_log_from (kind, "cadet-api",__VA_ARGS__)
33 #define DATA_OVERHEAD sizeof(struct GNUNET_CADET_LocalData)
35 /******************************************************************************/
36 /************************ DATA STRUCTURES ****************************/
37 /******************************************************************************/
40 * Transmission queue to the service
42 struct GNUNET_CADET_TransmitHandle
48 struct GNUNET_CADET_TransmitHandle *next;
53 struct GNUNET_CADET_TransmitHandle *prev;
56 * Channel this message is sent on / for (may be NULL for control messages).
58 struct GNUNET_CADET_Channel *channel;
61 * Callback to obtain the message to transmit, or NULL if we
62 * got the message in 'data'. Notice that messages built
63 * by 'notify' need to be encapsulated with information about
66 GNUNET_CONNECTION_TransmitReadyNotify notify;
69 * Closure for 'notify'
74 * How long is this message valid. Once the timeout has been
75 * reached, the message must no longer be sent. If this
76 * is a message with a 'notify' callback set, the 'notify'
77 * function should be called with 'buf' NULL and size 0.
79 struct GNUNET_TIME_Absolute timeout;
82 * Task triggering a timeout, can be NO_TASK if the timeout is FOREVER.
84 struct GNUNET_SCHEDULER_Task * timeout_task;
87 * Size of 'data' -- or the desired size of 'notify' if 'data' is NULL.
97 GNUNET_CADET_ChannelCB channel_cb;
102 GNUNET_CADET_PeersCB peers_cb;
107 GNUNET_CADET_PeerCB peer_cb;
112 GNUNET_CADET_TunnelsCB tunnels_cb;
117 GNUNET_CADET_TunnelCB tunnel_cb;
122 * Opaque handle to the service.
124 struct GNUNET_CADET_Handle
128 * Handle to the server connection, to send messages later
130 struct GNUNET_CLIENT_Connection *client;
133 * Set of handlers used for processing incoming messages in the channels
135 const struct GNUNET_CADET_MessageHandler *message_handlers;
138 * Number of handlers in the handlers array.
140 unsigned int n_handlers;
145 const uint32_t *ports;
150 unsigned int n_ports;
153 * Double linked list of the channels this client is connected to, head.
155 struct GNUNET_CADET_Channel *channels_head;
158 * Double linked list of the channels this client is connected to, tail.
160 struct GNUNET_CADET_Channel *channels_tail;
163 * Callback for inbound channel creation
165 GNUNET_CADET_InboundChannelNotificationHandler *new_channel;
168 * Callback for inbound channel disconnection
170 GNUNET_CADET_ChannelEndHandler *cleaner;
173 * Handle to cancel pending transmissions in case of disconnection
175 struct GNUNET_CLIENT_TransmitHandle *th;
178 * Closure for all the handlers given by the client
183 * Messages to send to the service, head.
185 struct GNUNET_CADET_TransmitHandle *th_head;
188 * Messages to send to the service, tail.
190 struct GNUNET_CADET_TransmitHandle *th_tail;
193 * chid of the next channel to create (to avoid reusing IDs often)
195 CADET_ChannelNumber next_chid;
198 * Have we started the task to receive messages from the service
199 * yet? We do this after we send the 'CADET_LOCAL_CONNECT' message.
204 * Configuration given by the client, in case of reconnection
206 const struct GNUNET_CONFIGURATION_Handle *cfg;
209 * Time to the next reconnect in case one reconnect fails
211 struct GNUNET_TIME_Relative reconnect_time;
214 * Task for trying to reconnect.
216 struct GNUNET_SCHEDULER_Task * reconnect_task;
219 * Callback for an info task (only one active at a time).
221 union CadetInfoCB info_cb;
224 * Info callback closure for @c info_cb.
231 * Description of a peer
233 struct GNUNET_CADET_Peer
236 * ID of the peer in short form
241 * Channel this peer belongs to
243 struct GNUNET_CADET_Channel *t;
248 * Opaque handle to a channel.
250 struct GNUNET_CADET_Channel
256 struct GNUNET_CADET_Channel *next;
261 struct GNUNET_CADET_Channel *prev;
264 * Handle to the cadet this channel belongs to
266 struct GNUNET_CADET_Handle *cadet;
269 * Local ID of the channel
271 CADET_ChannelNumber chid;
279 * Other end of the channel.
284 * Any data the caller wants to put in here
289 * Size of packet queued in this channel
291 unsigned int packet_size;
294 * Channel options: reliability, etc.
296 enum GNUNET_CADET_ChannelOption options;
299 * Are we allowed to send to the service?
307 * Implementation state for cadet's message queue.
312 * The current transmit handle, or NULL
313 * if no transmit is active.
315 struct GNUNET_CADET_TransmitHandle *th;
318 * Channel to send the data over.
320 struct GNUNET_CADET_Channel *channel;
324 /******************************************************************************/
325 /*********************** DECLARATIONS *************************/
326 /******************************************************************************/
329 * Function called to send a message to the service.
330 * "buf" will be NULL and "size" zero if the socket was closed for writing in
333 * @param cls closure, the cadet handle
334 * @param size number of bytes available in buf
335 * @param buf where the callee should write the connect message
336 * @return number of bytes written to buf
339 send_callback (void *cls, size_t size, void *buf);
342 /******************************************************************************/
343 /*********************** AUXILIARY FUNCTIONS *************************/
344 /******************************************************************************/
347 * Check if transmission is a payload packet.
349 * @param th Transmission handle.
351 * @return #GNUNET_YES if it is a payload packet,
352 * #GNUNET_NO if it is a cadet management packet.
355 th_is_payload (struct GNUNET_CADET_TransmitHandle *th)
357 return (th->notify != NULL) ? GNUNET_YES : GNUNET_NO;
362 * Check whether there is any message ready in the queue and find the size.
364 * @param h Cadet handle.
366 * @return The size of the first ready message in the queue, including overhead.
367 * 0 if there is none.
370 message_ready_size (struct GNUNET_CADET_Handle *h)
372 struct GNUNET_CADET_TransmitHandle *th;
373 struct GNUNET_CADET_Channel *ch;
375 for (th = h->th_head; NULL != th; th = th->next)
378 if (GNUNET_NO == th_is_payload (th) || GNUNET_YES == ch->allow_send)
386 * Get the channel handler for the channel specified by id from the given handle
387 * @param h Cadet handle
388 * @param chid ID of the wanted channel
389 * @return handle to the required channel or NULL if not found
391 static struct GNUNET_CADET_Channel *
392 retrieve_channel (struct GNUNET_CADET_Handle *h, CADET_ChannelNumber chid)
394 struct GNUNET_CADET_Channel *ch;
396 ch = h->channels_head;
399 if (ch->chid == chid)
408 * Create a new channel and insert it in the channel list of the cadet handle
410 * @param h Cadet handle
411 * @param chid Desired chid of the channel, 0 to assign one automatically.
413 * @return Handle to the created channel.
415 static struct GNUNET_CADET_Channel *
416 create_channel (struct GNUNET_CADET_Handle *h, CADET_ChannelNumber chid)
418 struct GNUNET_CADET_Channel *ch;
420 ch = GNUNET_new (struct GNUNET_CADET_Channel);
421 GNUNET_CONTAINER_DLL_insert (h->channels_head, h->channels_tail, ch);
425 ch->chid = h->next_chid;
426 while (NULL != retrieve_channel (h, h->next_chid))
429 h->next_chid &= ~GNUNET_CADET_LOCAL_CHANNEL_ID_SERV;
430 h->next_chid |= GNUNET_CADET_LOCAL_CHANNEL_ID_CLI;
437 ch->allow_send = GNUNET_NO;
443 * Destroy the specified channel.
444 * - Destroys all peers, calling the disconnect callback on each if needed
445 * - Cancels all outgoing traffic for that channel, calling respective notifys
446 * - Calls cleaner if channel was inbound
447 * - Frees all memory used
449 * @param ch Pointer to the channel.
450 * @param call_cleaner Whether to call the cleaner handler.
452 * @return Handle to the required channel or NULL if not found.
455 destroy_channel (struct GNUNET_CADET_Channel *ch, int call_cleaner)
457 struct GNUNET_CADET_Handle *h;
458 struct GNUNET_CADET_TransmitHandle *th;
459 struct GNUNET_CADET_TransmitHandle *next;
461 LOG (GNUNET_ERROR_TYPE_DEBUG, " destroy_channel %X\n", ch->chid);
470 GNUNET_CONTAINER_DLL_remove (h->channels_head, h->channels_tail, ch);
472 /* signal channel destruction */
473 if ( (NULL != h->cleaner) && (0 != ch->peer) && (GNUNET_YES == call_cleaner) )
475 LOG (GNUNET_ERROR_TYPE_DEBUG, " calling cleaner\n");
476 h->cleaner (h->cls, ch, ch->ctx);
479 /* check that clients did not leave messages behind in the queue */
480 for (th = h->th_head; NULL != th; th = next)
483 if (th->channel != ch)
485 /* Clients should have aborted their requests already.
486 * Management traffic should be ok, as clients can't cancel that.
487 * If the service crashed and we are reconnecting, it's ok.
489 GNUNET_break (GNUNET_NO == th_is_payload (th)
490 || GNUNET_NO == h->in_receive);
491 GNUNET_CONTAINER_DLL_remove (h->th_head, h->th_tail, th);
493 /* clean up request */
494 if (NULL != th->timeout_task)
495 GNUNET_SCHEDULER_cancel (th->timeout_task);
499 /* if there are no more pending requests with cadet service, cancel active request */
500 /* Note: this should be unnecessary... */
501 if ((0 == message_ready_size (h)) && (NULL != h->th))
503 GNUNET_CLIENT_notify_transmit_ready_cancel (h->th);
508 GNUNET_PEER_change_rc (ch->peer, -1);
515 * Notify client that the transmission has timed out
520 timeout_transmission (void *cls)
522 struct GNUNET_CADET_TransmitHandle *th = cls;
523 struct GNUNET_CADET_Handle *cadet = th->channel->cadet;
525 th->timeout_task = NULL;
526 th->channel->packet_size = 0;
527 GNUNET_CONTAINER_DLL_remove (cadet->th_head, cadet->th_tail, th);
528 if (GNUNET_YES == th_is_payload (th))
529 GNUNET_break (0 == th->notify (th->notify_cls, 0, NULL));
531 if ((0 == message_ready_size (cadet)) && (NULL != cadet->th))
533 /* nothing ready to transmit, no point in asking for transmission */
534 GNUNET_CLIENT_notify_transmit_ready_cancel (cadet->th);
541 * Add a transmit handle to the transmission queue and set the
544 * @param h cadet handle with the queue head and tail
545 * @param th handle to the packet to be transmitted
548 add_to_queue (struct GNUNET_CADET_Handle *h,
549 struct GNUNET_CADET_TransmitHandle *th)
551 GNUNET_CONTAINER_DLL_insert_tail (h->th_head, h->th_tail, th);
552 if (GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us == th->timeout.abs_value_us)
555 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_absolute_get_remaining
556 (th->timeout), &timeout_transmission, th);
561 * Auxiliary function to send an already constructed packet to the service.
562 * Takes care of creating a new queue element, copying the message and
563 * calling the tmt_rdy function if necessary.
565 * @param h cadet handle
566 * @param msg message to transmit
567 * @param channel channel this send is related to (NULL if N/A)
570 send_packet (struct GNUNET_CADET_Handle *h,
571 const struct GNUNET_MessageHeader *msg,
572 struct GNUNET_CADET_Channel *channel);
576 * Send an ack on the channel to confirm the processing of a message.
578 * @param ch Channel on which to send the ACK.
581 send_ack (struct GNUNET_CADET_Channel *ch)
583 struct GNUNET_CADET_LocalAck msg;
585 LOG (GNUNET_ERROR_TYPE_DEBUG, "Sending ACK on channel %X\n", ch->chid);
586 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_LOCAL_ACK);
587 msg.header.size = htons (sizeof (msg));
588 msg.channel_id = htonl (ch->chid);
590 send_packet (ch->cadet, &msg.header, ch);
597 * Reconnect callback: tries to reconnect again after a failer previous
600 * @param cls closure (cadet handle)
603 reconnect_cbk (void *cls);
607 * Send a connect packet to the service with the applications and types
608 * requested by the user.
610 * @param h The cadet handle.
614 send_connect (struct GNUNET_CADET_Handle *h)
618 size = sizeof (struct GNUNET_CADET_ClientConnect);
619 size += h->n_ports * sizeof (uint32_t);
621 char buf[size] GNUNET_ALIGN;
622 struct GNUNET_CADET_ClientConnect *msg;
626 /* build connection packet */
627 msg = (struct GNUNET_CADET_ClientConnect *) buf;
628 msg->header.type = htons (GNUNET_MESSAGE_TYPE_CADET_LOCAL_CONNECT);
629 msg->header.size = htons (size);
630 ports = (uint32_t *) &msg[1];
631 for (i = 0; i < h->n_ports; i++)
633 ports[i] = htonl (h->ports[i]);
634 LOG (GNUNET_ERROR_TYPE_DEBUG, " port %u\n",
637 LOG (GNUNET_ERROR_TYPE_DEBUG,
638 "Sending %lu bytes long message with %u ports\n",
639 ntohs (msg->header.size), h->n_ports);
640 send_packet (h, &msg->header, NULL);
646 * Reconnect to the service, retransmit all infomation to try to restore the
649 * @param h handle to the cadet
651 * @return GNUNET_YES in case of sucess, GNUNET_NO otherwise (service down...)
654 do_reconnect (struct GNUNET_CADET_Handle *h)
656 LOG (GNUNET_ERROR_TYPE_DEBUG, "*****************************\n");
657 LOG (GNUNET_ERROR_TYPE_DEBUG, "******* RECONNECT *******\n");
658 LOG (GNUNET_ERROR_TYPE_DEBUG, "*****************************\n");
659 LOG (GNUNET_ERROR_TYPE_DEBUG, "******** on %p *******\n", h);
660 LOG (GNUNET_ERROR_TYPE_DEBUG, "*****************************\n");
665 GNUNET_CLIENT_notify_transmit_ready_cancel (h->th);
668 if (NULL != h->client)
670 GNUNET_CLIENT_disconnect (h->client);
674 h->client = GNUNET_CLIENT_connect ("cadet", h->cfg);
675 if (h->client == NULL)
677 h->reconnect_task = GNUNET_SCHEDULER_add_delayed (h->reconnect_time,
680 GNUNET_TIME_relative_min (GNUNET_TIME_UNIT_SECONDS,
681 GNUNET_TIME_relative_multiply
682 (h->reconnect_time, 2));
683 LOG (GNUNET_ERROR_TYPE_DEBUG, "Next retry in %s\n",
684 GNUNET_STRINGS_relative_time_to_string (h->reconnect_time,
691 h->reconnect_time = GNUNET_TIME_UNIT_MILLISECONDS;
698 * Reconnect callback: tries to reconnect again after a failer previous
701 * @param cls closure (cadet handle)
704 reconnect_cbk (void *cls)
706 struct GNUNET_CADET_Handle *h = cls;
708 h->reconnect_task = NULL;
714 * Reconnect to the service, retransmit all infomation to try to restore the
717 * @param h handle to the cadet
719 * @return #GNUNET_YES in case of sucess, #GNUNET_NO otherwise (service down...)
722 reconnect (struct GNUNET_CADET_Handle *h)
724 struct GNUNET_CADET_Channel *ch;
726 LOG (GNUNET_ERROR_TYPE_DEBUG,
727 "Requested RECONNECT, destroying all channels\n");
728 h->in_receive = GNUNET_NO;
729 for (ch = h->channels_head; NULL != ch; ch = h->channels_head)
730 destroy_channel (ch, GNUNET_YES);
731 if (NULL == h->reconnect_task)
732 h->reconnect_task = GNUNET_SCHEDULER_add_delayed (h->reconnect_time,
737 /******************************************************************************/
738 /*********************** RECEIVE HANDLERS ****************************/
739 /******************************************************************************/
742 * Process the new channel notification and add it to the channels in the handle
744 * @param h The cadet handle
745 * @param msg A message with the details of the new incoming channel
748 process_channel_created (struct GNUNET_CADET_Handle *h,
749 const struct GNUNET_CADET_ChannelMessage *msg)
751 struct GNUNET_CADET_Channel *ch;
752 CADET_ChannelNumber chid;
755 chid = ntohl (msg->channel_id);
756 port = ntohl (msg->port);
757 LOG (GNUNET_ERROR_TYPE_DEBUG, "Creating incoming channel %X:%u\n", chid, port);
758 if (chid < GNUNET_CADET_LOCAL_CHANNEL_ID_SERV)
763 if (NULL != h->new_channel)
767 ch = create_channel (h, chid);
768 ch->allow_send = GNUNET_NO;
769 ch->peer = GNUNET_PEER_intern (&msg->peer);
773 ch->options = ntohl (msg->opt);
775 LOG (GNUNET_ERROR_TYPE_DEBUG, " created channel %p\n", ch);
776 ctx = h->new_channel (h->cls, ch, &msg->peer, ch->port, ch->options);
779 LOG (GNUNET_ERROR_TYPE_DEBUG, "User notified\n");
783 struct GNUNET_CADET_ChannelMessage d_msg;
785 LOG (GNUNET_ERROR_TYPE_DEBUG, "No handler for incoming channels\n");
787 d_msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DESTROY);
788 d_msg.header.size = htons (sizeof (struct GNUNET_CADET_ChannelMessage));
789 d_msg.channel_id = msg->channel_id;
790 memset (&d_msg.peer, 0, sizeof (struct GNUNET_PeerIdentity));
794 send_packet (h, &d_msg.header, NULL);
801 * Process the channel destroy notification and free associated resources
803 * @param h The cadet handle
804 * @param msg A message with the details of the channel being destroyed
807 process_channel_destroy (struct GNUNET_CADET_Handle *h,
808 const struct GNUNET_CADET_ChannelMessage *msg)
810 struct GNUNET_CADET_Channel *ch;
811 CADET_ChannelNumber chid;
813 chid = ntohl (msg->channel_id);
814 LOG (GNUNET_ERROR_TYPE_DEBUG, "Channel %X Destroy from service\n", chid);
815 ch = retrieve_channel (h, chid);
819 LOG (GNUNET_ERROR_TYPE_DEBUG, "channel %X unknown\n", chid);
822 destroy_channel (ch, GNUNET_YES);
827 * Process the incoming data packets, call appropriate handlers.
829 * @param h The cadet handle
830 * @param message A message encapsulating the data
833 process_incoming_data (struct GNUNET_CADET_Handle *h,
834 const struct GNUNET_MessageHeader *message)
836 const struct GNUNET_MessageHeader *payload;
837 const struct GNUNET_CADET_MessageHandler *handler;
838 struct GNUNET_CADET_LocalData *dmsg;
839 struct GNUNET_CADET_Channel *ch;
844 LOG (GNUNET_ERROR_TYPE_DEBUG, "Got a data message!\n");
845 dmsg = (struct GNUNET_CADET_LocalData *) message;
846 ch = retrieve_channel (h, ntohl (dmsg->id));
853 payload = (struct GNUNET_MessageHeader *) &dmsg[1];
854 LOG (GNUNET_ERROR_TYPE_DEBUG, " %s data on channel %s [%X]\n",
855 GC_f2s (ch->chid >= GNUNET_CADET_LOCAL_CHANNEL_ID_SERV),
856 GNUNET_i2s (GNUNET_PEER_resolve2 (ch->peer)), ntohl (dmsg->id));
858 size = ntohs (message->size);
859 LOG (GNUNET_ERROR_TYPE_DEBUG, " %u bytes\n", size);
861 type = ntohs (payload->type);
862 size = ntohs (payload->size);
863 LOG (GNUNET_ERROR_TYPE_DEBUG, " payload type %s\n", GC_m2s (type));
864 for (i = 0; i < h->n_handlers; i++)
866 handler = &h->message_handlers[i];
867 LOG (GNUNET_ERROR_TYPE_DEBUG, " checking handler for type %u\n",
869 if (handler->type == type)
872 handler->callback (h->cls, ch, &ch->ctx, payload))
874 LOG (GNUNET_ERROR_TYPE_DEBUG, "callback caused disconnection\n");
875 GNUNET_CADET_channel_destroy (ch);
880 LOG (GNUNET_ERROR_TYPE_DEBUG,
881 "callback completed successfully\n");
890 * Process a local ACK message, enabling the client to send
891 * more data to the service.
893 * @param h Cadet handle.
894 * @param message Message itself.
897 process_ack (struct GNUNET_CADET_Handle *h,
898 const struct GNUNET_MessageHeader *message)
900 struct GNUNET_CADET_LocalAck *msg;
901 struct GNUNET_CADET_Channel *ch;
902 CADET_ChannelNumber chid;
904 LOG (GNUNET_ERROR_TYPE_DEBUG, "Got an ACK!\n");
905 msg = (struct GNUNET_CADET_LocalAck *) message;
906 chid = ntohl (msg->channel_id);
907 ch = retrieve_channel (h, chid);
910 LOG (GNUNET_ERROR_TYPE_DEBUG, "ACK on unknown channel %X\n", chid);
913 LOG (GNUNET_ERROR_TYPE_DEBUG, " on channel %X!\n", ch->chid);
914 ch->allow_send = GNUNET_YES;
915 if (NULL == h->th && 0 < ch->packet_size)
917 LOG (GNUNET_ERROR_TYPE_DEBUG, " tmt rdy was NULL, requesting!\n");
918 h->th = GNUNET_CLIENT_notify_transmit_ready (h->client, ch->packet_size,
919 GNUNET_TIME_UNIT_FOREVER_REL,
920 GNUNET_YES, &send_callback, h);
926 * Process a local reply about info on all channels, pass info to the user.
928 * @param h Cadet handle.
929 * @param message Message itself.
932 // process_get_channels (struct GNUNET_CADET_Handle *h,
933 // const struct GNUNET_MessageHeader *message)
935 // struct GNUNET_CADET_LocalInfo *msg;
937 // GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Get Channels messasge received\n");
939 // if (NULL == h->channels_cb)
941 // GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " ignored\n");
945 // msg = (struct GNUNET_CADET_LocalInfo *) message;
946 // if (ntohs (message->size) !=
947 // (sizeof (struct GNUNET_CADET_LocalInfo) +
948 // sizeof (struct GNUNET_PeerIdentity)))
950 // GNUNET_break_op (0);
951 // GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
952 // "Get channels message: size %hu - expected %u\n",
953 // ntohs (message->size),
954 // sizeof (struct GNUNET_CADET_LocalInfo));
957 // h->channels_cb (h->channels_cls,
958 // ntohl (msg->channel_id),
960 // &msg->destination);
966 * Process a local monitor_channel reply, pass info to the user.
968 * @param h Cadet handle.
969 * @param message Message itself.
972 // process_show_channel (struct GNUNET_CADET_Handle *h,
973 // const struct GNUNET_MessageHeader *message)
975 // struct GNUNET_CADET_LocalInfo *msg;
978 // GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Show Channel messasge received\n");
980 // if (NULL == h->channel_cb)
982 // GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " ignored\n");
986 // /* Verify message sanity */
987 // msg = (struct GNUNET_CADET_LocalInfo *) message;
988 // esize = sizeof (struct GNUNET_CADET_LocalInfo);
989 // if (ntohs (message->size) != esize)
991 // GNUNET_break_op (0);
992 // GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
993 // "Show channel message: size %hu - expected %u\n",
994 // ntohs (message->size),
997 // h->channel_cb (h->channel_cls, NULL, NULL);
998 // h->channel_cb = NULL;
999 // h->channel_cls = NULL;
1004 // h->channel_cb (h->channel_cls,
1005 // &msg->destination,
1012 * Process a local reply about info on all tunnels, pass info to the user.
1014 * @param h Cadet handle.
1015 * @param message Message itself.
1018 process_get_peers (struct GNUNET_CADET_Handle *h,
1019 const struct GNUNET_MessageHeader *message)
1021 struct GNUNET_CADET_LocalInfoPeer *msg;
1024 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Get Peer messasge received\n");
1026 if (NULL == h->info_cb.peers_cb)
1028 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ignored\n");
1032 size = ntohs (message->size);
1033 if (sizeof (struct GNUNET_CADET_LocalInfoPeer) > size)
1035 h->info_cb.peers_cb (h->info_cls, NULL, -1, 0, 0);
1036 h->info_cb.peers_cb = NULL;
1041 msg = (struct GNUNET_CADET_LocalInfoPeer *) message;
1042 h->info_cb.peers_cb (h->info_cls, &msg->destination,
1043 (int) ntohs (msg->tunnel),
1044 (unsigned int ) ntohs (msg->paths),
1050 * Process a local peer info reply, pass info to the user.
1052 * @param h Cadet handle.
1053 * @param message Message itself.
1056 process_get_peer (struct GNUNET_CADET_Handle *h,
1057 const struct GNUNET_MessageHeader *message)
1059 struct GNUNET_CADET_LocalInfoPeer *msg;
1060 struct GNUNET_PeerIdentity *id;
1061 unsigned int epaths;
1063 unsigned int path_length;
1069 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Info Peer messasge received\n");
1070 if (NULL == h->info_cb.peer_cb)
1072 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ignored\n");
1076 /* Verify message sanity */
1077 msg = (struct GNUNET_CADET_LocalInfoPeer *) message;
1078 esize = ntohs (message->size);
1079 msize = sizeof (struct GNUNET_CADET_LocalInfoPeer);
1082 GNUNET_break_op (0);
1083 h->info_cb.peer_cb (h->info_cls, NULL, 0, 0, 0, NULL);
1086 epaths = (unsigned int) ntohs (msg->paths);
1089 neighbor = GNUNET_NO;
1090 id = (struct GNUNET_PeerIdentity *) &msg[1];
1091 for (i = 0; msize < esize; i++)
1093 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " %s\n", GNUNET_i2s (&id[i]));
1094 msize += sizeof (struct GNUNET_PeerIdentity);
1096 if (0 == memcmp (&id[i], &msg->destination,
1097 sizeof (struct GNUNET_PeerIdentity)))
1099 if (1 == path_length)
1100 neighbor = GNUNET_YES;
1107 GNUNET_break_op (0);
1108 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "m:%u, e: %u\n", msize, esize);
1109 h->info_cb.peer_cb (h->info_cls, NULL, 0, 0, 0, NULL);
1112 if (paths != epaths)
1114 GNUNET_break_op (0);
1115 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "p:%u, e: %u\n", paths, epaths);
1116 h->info_cb.peer_cb (h->info_cls, NULL, 0, 0, 0, NULL);
1120 /* Call Callback with tunnel info. */
1121 id = (struct GNUNET_PeerIdentity *) &msg[1];
1122 h->info_cb.peer_cb (h->info_cls,
1124 (int) ntohs (msg->tunnel),
1130 h->info_cb.peer_cb = NULL;
1136 * Process a local reply about info on all tunnels, pass info to the user.
1138 * @param h Cadet handle.
1139 * @param message Message itself.
1142 process_get_tunnels (struct GNUNET_CADET_Handle *h,
1143 const struct GNUNET_MessageHeader *message)
1145 struct GNUNET_CADET_LocalInfoTunnel *msg;
1148 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Get Tunnels messasge received\n");
1150 if (NULL == h->info_cb.tunnels_cb)
1152 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ignored\n");
1156 size = ntohs (message->size);
1157 if (sizeof (struct GNUNET_CADET_LocalInfoTunnel) > size)
1159 h->info_cb.tunnels_cb (h->info_cls, NULL, 0, 0, 0, 0);
1160 h->info_cb.tunnels_cb = NULL;
1165 msg = (struct GNUNET_CADET_LocalInfoTunnel *) message;
1166 h->info_cb.tunnels_cb (h->info_cls, &msg->destination,
1167 ntohl (msg->channels), ntohl (msg->connections),
1168 ntohs (msg->estate), ntohs (msg->cstate));
1174 * Process a local tunnel info reply, pass info to the user.
1176 * @param h Cadet handle.
1177 * @param message Message itself.
1180 process_get_tunnel (struct GNUNET_CADET_Handle *h,
1181 const struct GNUNET_MessageHeader *message)
1183 struct GNUNET_CADET_LocalInfoTunnel *msg;
1188 struct GNUNET_CADET_Hash *conns;
1189 CADET_ChannelNumber *chns;
1191 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Get Tunnel messasge received\n");
1192 if (NULL == h->info_cb.tunnel_cb)
1194 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ignored\n");
1198 /* Verify message sanity */
1199 msg = (struct GNUNET_CADET_LocalInfoTunnel *) message;
1200 msize = ntohs (message->size);
1201 esize = sizeof (struct GNUNET_CADET_LocalInfoTunnel);
1204 GNUNET_break_op (0);
1205 h->info_cb.tunnel_cb (h->info_cls, NULL, 0, 0, NULL, NULL, 0, 0);
1208 ch_n = ntohl (msg->channels);
1209 c_n = ntohl (msg->connections);
1210 esize += ch_n * sizeof (CADET_ChannelNumber);
1211 esize += c_n * sizeof (struct GNUNET_CADET_Hash);
1214 GNUNET_break_op (0);
1215 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "m:%u, e: %u (%u ch, %u conn)\n",
1216 msize, esize, ch_n, c_n);
1217 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "%u (%u ch, %u conn)\n",
1218 sizeof (struct GNUNET_CADET_LocalInfoTunnel),
1219 sizeof (CADET_ChannelNumber), sizeof (struct GNUNET_HashCode));
1220 h->info_cb.tunnel_cb (h->info_cls, NULL, 0, 0, NULL, NULL, 0, 0);
1224 /* Call Callback with tunnel info. */
1225 conns = (struct GNUNET_CADET_Hash *) &msg[1];
1226 chns = (CADET_ChannelNumber *) &conns[c_n];
1227 h->info_cb.tunnel_cb (h->info_cls, &msg->destination,
1228 ch_n, c_n, chns, conns,
1229 ntohs (msg->estate), ntohs (msg->cstate));
1232 h->info_cb.tunnel_cb = NULL;
1238 * Function to process all messages received from the service
1240 * @param cls closure
1241 * @param msg message received, NULL on timeout or fatal error
1244 msg_received (void *cls, const struct GNUNET_MessageHeader *msg)
1246 struct GNUNET_CADET_Handle *h = cls;
1251 LOG (GNUNET_ERROR_TYPE_DEBUG,
1252 "Cadet service disconnected, reconnecting\n", h);
1256 type = ntohs (msg->type);
1257 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n");
1258 LOG (GNUNET_ERROR_TYPE_DEBUG, "Received a message: %s\n",
1262 /* Notify of a new incoming channel */
1263 case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_CREATE:
1264 process_channel_created (h, (struct GNUNET_CADET_ChannelMessage *) msg);
1266 /* Notify of a channel disconnection */
1267 case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DESTROY: /* TODO separate(gid problem)*/
1268 case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_NACK:
1269 process_channel_destroy (h, (struct GNUNET_CADET_ChannelMessage *) msg);
1271 case GNUNET_MESSAGE_TYPE_CADET_LOCAL_DATA:
1272 process_incoming_data (h, msg);
1274 case GNUNET_MESSAGE_TYPE_CADET_LOCAL_ACK:
1275 process_ack (h, msg);
1277 // case GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_CHANNELS:
1278 // process_get_channels (h, msg);
1280 // case GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_CHANNEL:
1281 // process_show_channel (h, msg);
1283 case GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEERS:
1284 process_get_peers (h, msg);
1286 case GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEER:
1287 process_get_peer (h, msg);
1289 case GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNELS:
1290 process_get_tunnels (h, msg);
1292 case GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNEL:
1293 process_get_tunnel (h, msg);
1295 // case GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_CHANNEL:
1296 // process_show_channel (h, msg);
1299 /* We shouldn't get any other packages, log and ignore */
1300 LOG (GNUNET_ERROR_TYPE_WARNING,
1301 "unsolicited message form service (type %s)\n",
1302 GC_m2s (ntohs (msg->type)));
1304 LOG (GNUNET_ERROR_TYPE_DEBUG, "message processed\n");
1305 if (GNUNET_YES == h->in_receive)
1307 GNUNET_CLIENT_receive (h->client, &msg_received, h,
1308 GNUNET_TIME_UNIT_FOREVER_REL);
1312 LOG (GNUNET_ERROR_TYPE_DEBUG,
1313 "in receive off, not calling CLIENT_receive\n");
1318 /******************************************************************************/
1319 /************************ SEND FUNCTIONS ****************************/
1320 /******************************************************************************/
1323 * Function called to send a message to the service.
1324 * "buf" will be NULL and "size" zero if the socket was closed for writing in
1327 * @param cls closure, the cadet handle
1328 * @param size number of bytes available in buf
1329 * @param buf where the callee should write the connect message
1330 * @return number of bytes written to buf
1333 send_callback (void *cls, size_t size, void *buf)
1335 struct GNUNET_CADET_Handle *h = cls;
1336 struct GNUNET_CADET_TransmitHandle *th;
1337 struct GNUNET_CADET_TransmitHandle *next;
1338 struct GNUNET_CADET_Channel *ch;
1344 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n");
1345 LOG (GNUNET_ERROR_TYPE_DEBUG, "# Send callback, buffer %u\n", size);
1346 if ((0 == size) || (NULL == buf))
1348 LOG (GNUNET_ERROR_TYPE_DEBUG, "# Received NULL send callback on %p\n", h);
1355 nsize = message_ready_size (h);
1356 while ((NULL != (th = next)) && (0 < nsize) && (size >= nsize))
1359 if (GNUNET_YES == th_is_payload (th))
1361 struct GNUNET_CADET_LocalData *dmsg;
1362 struct GNUNET_MessageHeader *mh;
1364 LOG (GNUNET_ERROR_TYPE_DEBUG, "# payload, %u bytes on %X (%p)\n",
1365 th->size, ch->chid, ch);
1366 if (GNUNET_NO == ch->allow_send)
1368 /* This channel is not ready to transmit yet, Try the next message */
1372 ch->packet_size = 0;
1373 GNUNET_assert (size >= th->size);
1374 dmsg = (struct GNUNET_CADET_LocalData *) cbuf;
1375 mh = (struct GNUNET_MessageHeader *) &dmsg[1];
1376 psize = th->notify (th->notify_cls, size - DATA_OVERHEAD, mh);
1380 GNUNET_assert (sizeof (struct GNUNET_MessageHeader) <= psize);
1381 psize += DATA_OVERHEAD;
1382 GNUNET_assert (size >= psize);
1383 dmsg->header.type = htons (GNUNET_MESSAGE_TYPE_CADET_LOCAL_DATA);
1384 dmsg->header.size = htons (psize);
1385 dmsg->id = htonl (ch->chid);
1386 LOG (GNUNET_ERROR_TYPE_DEBUG, "# sending, type %s\n",
1387 GC_m2s (ntohs (mh->type)));
1388 ch->allow_send = GNUNET_NO;
1392 LOG (GNUNET_ERROR_TYPE_DEBUG,
1393 "# callback returned size 0, "
1394 "application canceled transmission\n");
1399 const struct GNUNET_MessageHeader *mh;
1401 mh = (const struct GNUNET_MessageHeader *) &th[1];
1402 LOG (GNUNET_ERROR_TYPE_DEBUG, "# cadet internal traffic, type %s\n",
1403 GC_m2s (ntohs (mh->type)));
1404 memcpy (cbuf, &th[1], th->size);
1407 GNUNET_assert (GNUNET_CONSTANTS_MAX_CADET_MESSAGE_SIZE >= psize);
1408 if (th->timeout_task != NULL)
1409 GNUNET_SCHEDULER_cancel (th->timeout_task);
1411 GNUNET_CONTAINER_DLL_remove (h->th_head, h->th_tail, th);
1413 nsize = message_ready_size (h);
1418 LOG (GNUNET_ERROR_TYPE_DEBUG, "# total size: %u\n", tsize);
1420 size = message_ready_size (h);
1423 LOG (GNUNET_ERROR_TYPE_DEBUG, "# next size: %u\n", size);
1425 GNUNET_CLIENT_notify_transmit_ready (h->client, size,
1426 GNUNET_TIME_UNIT_FOREVER_REL,
1427 GNUNET_YES, &send_callback, h);
1431 if (NULL != h->th_head)
1432 LOG (GNUNET_ERROR_TYPE_DEBUG, "# nothing ready to transmit\n");
1434 LOG (GNUNET_ERROR_TYPE_DEBUG, "# nothing left to transmit\n");
1436 if (GNUNET_NO == h->in_receive)
1438 LOG (GNUNET_ERROR_TYPE_DEBUG, "# start receiving from service\n");
1439 h->in_receive = GNUNET_YES;
1440 GNUNET_CLIENT_receive (h->client, &msg_received, h,
1441 GNUNET_TIME_UNIT_FOREVER_REL);
1443 LOG (GNUNET_ERROR_TYPE_DEBUG, "# Send callback() END\n");
1449 * Auxiliary function to send an already constructed packet to the service.
1450 * Takes care of creating a new queue element, copying the message and
1451 * calling the tmt_rdy function if necessary.
1453 * @param h cadet handle
1454 * @param msg message to transmit
1455 * @param channel channel this send is related to (NULL if N/A)
1458 send_packet (struct GNUNET_CADET_Handle *h,
1459 const struct GNUNET_MessageHeader *msg,
1460 struct GNUNET_CADET_Channel *channel)
1462 struct GNUNET_CADET_TransmitHandle *th;
1465 LOG (GNUNET_ERROR_TYPE_DEBUG, " Sending message to service: %s\n",
1466 GC_m2s(ntohs(msg->type)));
1467 msize = ntohs (msg->size);
1468 th = GNUNET_malloc (sizeof (struct GNUNET_CADET_TransmitHandle) + msize);
1469 th->timeout = GNUNET_TIME_UNIT_FOREVER_ABS;
1471 th->channel = channel;
1472 memcpy (&th[1], msg, msize);
1473 add_to_queue (h, th);
1476 LOG (GNUNET_ERROR_TYPE_DEBUG, " calling ntfy tmt rdy for %u bytes\n", msize);
1478 GNUNET_CLIENT_notify_transmit_ready (h->client, msize,
1479 GNUNET_TIME_UNIT_FOREVER_REL,
1480 GNUNET_YES, &send_callback, h);
1484 /******************************************************************************/
1485 /********************** API CALL DEFINITIONS *************************/
1486 /******************************************************************************/
1488 struct GNUNET_CADET_Handle *
1489 GNUNET_CADET_connect (const struct GNUNET_CONFIGURATION_Handle *cfg, void *cls,
1490 GNUNET_CADET_InboundChannelNotificationHandler new_channel,
1491 GNUNET_CADET_ChannelEndHandler cleaner,
1492 const struct GNUNET_CADET_MessageHandler *handlers,
1493 const uint32_t *ports)
1495 struct GNUNET_CADET_Handle *h;
1497 LOG (GNUNET_ERROR_TYPE_DEBUG, "GNUNET_CADET_connect()\n");
1498 h = GNUNET_new (struct GNUNET_CADET_Handle);
1499 LOG (GNUNET_ERROR_TYPE_DEBUG, " addr %p\n", h);
1501 h->new_channel = new_channel;
1502 h->cleaner = cleaner;
1503 h->client = GNUNET_CLIENT_connect ("cadet", cfg);
1504 if (h->client == NULL)
1511 h->message_handlers = handlers;
1513 h->next_chid = GNUNET_CADET_LOCAL_CHANNEL_ID_CLI;
1514 h->reconnect_time = GNUNET_TIME_UNIT_MILLISECONDS;
1515 h->reconnect_task = NULL;
1517 if (NULL != ports && ports[0] != 0 && NULL == new_channel)
1520 LOG (GNUNET_ERROR_TYPE_DEBUG,
1521 "no new channel handler given, ports parameter is useless!!\n");
1523 if ((NULL == ports || ports[0] == 0) && NULL != new_channel)
1526 LOG (GNUNET_ERROR_TYPE_DEBUG,
1527 "no ports given, new channel handler will never be called!!\n");
1529 /* count handlers */
1530 for (h->n_handlers = 0;
1531 handlers && handlers[h->n_handlers].type;
1533 for (h->n_ports = 0;
1534 ports && ports[h->n_ports];
1537 LOG (GNUNET_ERROR_TYPE_DEBUG, "GNUNET_CADET_connect() END\n");
1543 GNUNET_CADET_disconnect (struct GNUNET_CADET_Handle *handle)
1545 struct GNUNET_CADET_Channel *ch;
1546 struct GNUNET_CADET_Channel *aux;
1547 struct GNUNET_CADET_TransmitHandle *th;
1549 LOG (GNUNET_ERROR_TYPE_DEBUG, "CADET DISCONNECT\n");
1551 ch = handle->channels_head;
1555 if (ch->chid < GNUNET_CADET_LOCAL_CHANNEL_ID_SERV)
1558 LOG (GNUNET_ERROR_TYPE_DEBUG, "channel %X not destroyed\n", ch->chid);
1560 destroy_channel (ch, GNUNET_YES);
1563 while ( (th = handle->th_head) != NULL)
1565 struct GNUNET_MessageHeader *msg;
1567 /* Make sure it is an allowed packet (everything else should have been
1568 * already canceled).
1570 GNUNET_break (GNUNET_NO == th_is_payload (th));
1571 msg = (struct GNUNET_MessageHeader *) &th[1];
1572 switch (ntohs(msg->type))
1574 case GNUNET_MESSAGE_TYPE_CADET_LOCAL_CONNECT:
1575 case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_CREATE:
1576 case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DESTROY:
1577 case GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_CHANNELS:
1578 case GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_CHANNEL:
1579 case GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEER:
1580 case GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEERS:
1581 case GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNEL:
1582 case GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNELS:
1586 LOG (GNUNET_ERROR_TYPE_ERROR, "unexpected unsent msg %s\n",
1587 GC_m2s (ntohs(msg->type)));
1590 GNUNET_CONTAINER_DLL_remove (handle->th_head, handle->th_tail, th);
1594 if (NULL != handle->th)
1596 GNUNET_CLIENT_notify_transmit_ready_cancel (handle->th);
1599 if (NULL != handle->client)
1601 GNUNET_CLIENT_disconnect (handle->client);
1602 handle->client = NULL;
1604 if (NULL != handle->reconnect_task)
1606 GNUNET_SCHEDULER_cancel(handle->reconnect_task);
1607 handle->reconnect_task = NULL;
1609 GNUNET_free (handle);
1614 * Create a new channel towards a remote peer.
1616 * If the destination port is not open by any peer or the destination peer
1617 * does not accept the channel, #GNUNET_CADET_ChannelEndHandler will be called
1620 * @param h cadet handle
1621 * @param channel_ctx client's channel context to associate with the channel
1622 * @param peer peer identity the channel should go to
1623 * @param port Port number.
1624 * @param options CadetOption flag field, with all desired option bits set to 1.
1626 * @return handle to the channel
1628 struct GNUNET_CADET_Channel *
1629 GNUNET_CADET_channel_create (struct GNUNET_CADET_Handle *h,
1631 const struct GNUNET_PeerIdentity *peer,
1633 enum GNUNET_CADET_ChannelOption options)
1635 struct GNUNET_CADET_Channel *ch;
1636 struct GNUNET_CADET_ChannelMessage msg;
1638 LOG (GNUNET_ERROR_TYPE_DEBUG,
1639 "Creating new channel to %s:%u\n",
1640 GNUNET_i2s (peer), port);
1641 ch = create_channel (h, 0);
1642 LOG (GNUNET_ERROR_TYPE_DEBUG, " at %p\n", ch);
1643 LOG (GNUNET_ERROR_TYPE_DEBUG, " number %X\n", ch->chid);
1644 ch->ctx = channel_ctx;
1645 ch->peer = GNUNET_PEER_intern (peer);
1646 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CHANNEL_CREATE);
1647 msg.header.size = htons (sizeof (struct GNUNET_CADET_ChannelMessage));
1648 msg.channel_id = htonl (ch->chid);
1649 msg.port = htonl (port);
1651 msg.opt = htonl (options);
1652 ch->allow_send = GNUNET_NO;
1653 send_packet (h, &msg.header, ch);
1659 GNUNET_CADET_channel_destroy (struct GNUNET_CADET_Channel *channel)
1661 struct GNUNET_CADET_Handle *h;
1662 struct GNUNET_CADET_ChannelMessage msg;
1663 struct GNUNET_CADET_TransmitHandle *th;
1665 LOG (GNUNET_ERROR_TYPE_DEBUG, "Destroying channel\n");
1668 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DESTROY);
1669 msg.header.size = htons (sizeof (struct GNUNET_CADET_ChannelMessage));
1670 msg.channel_id = htonl (channel->chid);
1671 memset (&msg.peer, 0, sizeof (struct GNUNET_PeerIdentity));
1677 struct GNUNET_CADET_TransmitHandle *aux;
1678 if (th->channel == channel)
1681 if (GNUNET_YES == th_is_payload (th))
1683 /* applications should cancel before destroying channel */
1685 LOG (GNUNET_ERROR_TYPE_WARNING,
1686 "Channel destroyed without cancelling transmission requests\n");
1687 th->notify (th->notify_cls, 0, NULL);
1689 GNUNET_CADET_notify_transmit_ready_cancel (th);
1696 destroy_channel (channel, GNUNET_YES);
1697 send_packet (h, &msg.header, NULL);
1702 * Get information about a channel.
1704 * @param channel Channel handle.
1705 * @param option Query (GNUNET_CADET_OPTION_*).
1706 * @param ... dependant on option, currently not used
1708 * @return Union with an answer to the query.
1710 const union GNUNET_CADET_ChannelInfo *
1711 GNUNET_CADET_channel_get_info (struct GNUNET_CADET_Channel *channel,
1712 enum GNUNET_CADET_ChannelOption option, ...)
1714 static int bool_flag;
1715 const union GNUNET_CADET_ChannelInfo *ret;
1719 case GNUNET_CADET_OPTION_NOBUFFER:
1720 case GNUNET_CADET_OPTION_RELIABLE:
1721 case GNUNET_CADET_OPTION_OOORDER:
1722 if (0 != (option & channel->options))
1723 bool_flag = GNUNET_YES;
1725 bool_flag = GNUNET_NO;
1726 ret = (const union GNUNET_CADET_ChannelInfo *) &bool_flag;
1728 case GNUNET_CADET_OPTION_PEER:
1729 ret = (const union GNUNET_CADET_ChannelInfo *) GNUNET_PEER_resolve2 (channel->peer);
1739 struct GNUNET_CADET_TransmitHandle *
1740 GNUNET_CADET_notify_transmit_ready (struct GNUNET_CADET_Channel *channel, int cork,
1741 struct GNUNET_TIME_Relative maxdelay,
1743 GNUNET_CONNECTION_TransmitReadyNotify notify,
1746 struct GNUNET_CADET_TransmitHandle *th;
1748 GNUNET_assert (NULL != channel);
1749 GNUNET_assert (GNUNET_CONSTANTS_MAX_CADET_MESSAGE_SIZE >= notify_size);
1750 LOG (GNUNET_ERROR_TYPE_DEBUG, "CADET NOTIFY TRANSMIT READY\n");
1751 LOG (GNUNET_ERROR_TYPE_DEBUG, " on channel %X\n", channel->chid);
1752 LOG (GNUNET_ERROR_TYPE_DEBUG, " allow_send %d\n", channel->allow_send);
1753 if (channel->chid >= GNUNET_CADET_LOCAL_CHANNEL_ID_SERV)
1754 LOG (GNUNET_ERROR_TYPE_DEBUG, " to origin\n");
1756 LOG (GNUNET_ERROR_TYPE_DEBUG, " to destination\n");
1757 LOG (GNUNET_ERROR_TYPE_DEBUG, " payload size %u\n", notify_size);
1758 GNUNET_assert (NULL != notify);
1759 GNUNET_assert (0 == channel->packet_size); // Only one data packet allowed
1760 th = GNUNET_new (struct GNUNET_CADET_TransmitHandle);
1761 th->channel = channel;
1762 th->timeout = GNUNET_TIME_relative_to_absolute (maxdelay);
1763 th->size = notify_size + DATA_OVERHEAD;
1764 channel->packet_size = th->size;
1765 LOG (GNUNET_ERROR_TYPE_DEBUG, " total size %u\n", th->size);
1766 th->notify = notify;
1767 th->notify_cls = notify_cls;
1768 add_to_queue (channel->cadet, th);
1769 if (NULL != channel->cadet->th)
1771 if (GNUNET_NO == channel->allow_send)
1773 LOG (GNUNET_ERROR_TYPE_DEBUG, " call client notify tmt rdy\n");
1774 channel->cadet->th =
1775 GNUNET_CLIENT_notify_transmit_ready (channel->cadet->client, th->size,
1776 GNUNET_TIME_UNIT_FOREVER_REL,
1777 GNUNET_YES, &send_callback,
1779 LOG (GNUNET_ERROR_TYPE_DEBUG, "CADET NOTIFY TRANSMIT READY END\n");
1785 GNUNET_CADET_notify_transmit_ready_cancel (struct GNUNET_CADET_TransmitHandle *th)
1787 struct GNUNET_CADET_Handle *cadet;
1789 LOG (GNUNET_ERROR_TYPE_DEBUG, "CADET NOTIFY TRANSMIT READY CANCEL\n");
1790 LOG (GNUNET_ERROR_TYPE_DEBUG, " on channel %X (%p)\n",
1791 th->channel->chid, th->channel);
1792 LOG (GNUNET_ERROR_TYPE_DEBUG, " size %u bytes\n", th->size);
1793 th->channel->packet_size = 0;
1794 cadet = th->channel->cadet;
1795 if (th->timeout_task != NULL)
1796 GNUNET_SCHEDULER_cancel (th->timeout_task);
1797 GNUNET_CONTAINER_DLL_remove (cadet->th_head, cadet->th_tail, th);
1799 if ((0 == message_ready_size (cadet)) && (NULL != cadet->th))
1801 /* queue empty, no point in asking for transmission */
1802 GNUNET_CLIENT_notify_transmit_ready_cancel (cadet->th);
1805 LOG (GNUNET_ERROR_TYPE_DEBUG, "CADET NOTIFY TRANSMIT READY CANCEL END\n");
1810 GNUNET_CADET_receive_done (struct GNUNET_CADET_Channel *channel)
1817 send_info_request (struct GNUNET_CADET_Handle *h, uint16_t type)
1819 struct GNUNET_MessageHeader msg;
1821 msg.size = htons (sizeof (msg));
1822 msg.type = htons (type);
1823 send_packet (h, &msg, NULL);
1828 * Request a debug dump on the service's STDERR.
1830 * WARNING: unstable API, likely to change in the future!
1832 * @param h cadet handle
1835 GNUNET_CADET_request_dump (struct GNUNET_CADET_Handle *h)
1837 LOG (GNUNET_ERROR_TYPE_DEBUG, "requesting dump\n");
1838 send_info_request (h, GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_DUMP);
1843 * Request information about peers known to the running cadet service.
1844 * The callback will be called for every peer known to the service.
1845 * Only one info request (of any kind) can be active at once.
1848 * WARNING: unstable API, likely to change in the future!
1850 * @param h Handle to the cadet peer.
1851 * @param callback Function to call with the requested data.
1852 * @param callback_cls Closure for @c callback.
1854 * @return #GNUNET_OK / #GNUNET_SYSERR
1857 GNUNET_CADET_get_peers (struct GNUNET_CADET_Handle *h,
1858 GNUNET_CADET_PeersCB callback,
1861 if (NULL != h->info_cb.peers_cb)
1864 return GNUNET_SYSERR;
1866 send_info_request (h, GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEERS);
1867 h->info_cb.peers_cb = callback;
1868 h->info_cls = callback_cls;
1874 * Cancel a peer info request. The callback will not be called (anymore).
1876 * WARNING: unstable API, likely to change in the future!
1878 * @param h Cadet handle.
1880 * @return Closure given to GNUNET_CADET_get_peers.
1883 GNUNET_CADET_get_peers_cancel (struct GNUNET_CADET_Handle *h)
1888 h->info_cb.peers_cb = NULL;
1895 * Request information about a peer known to the running cadet peer.
1896 * The callback will be called for the tunnel once.
1897 * Only one info request (of any kind) can be active at once.
1899 * WARNING: unstable API, likely to change in the future!
1901 * @param h Handle to the cadet peer.
1902 * @param id Peer whose tunnel to examine.
1903 * @param callback Function to call with the requested data.
1904 * @param callback_cls Closure for @c callback.
1906 * @return #GNUNET_OK / #GNUNET_SYSERR
1909 GNUNET_CADET_get_peer (struct GNUNET_CADET_Handle *h,
1910 const struct GNUNET_PeerIdentity *id,
1911 GNUNET_CADET_PeerCB callback,
1914 struct GNUNET_CADET_LocalInfo msg;
1916 if (NULL != h->info_cb.peer_cb)
1919 return GNUNET_SYSERR;
1922 memset (&msg, 0, sizeof (msg));
1923 msg.header.size = htons (sizeof (msg));
1924 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEER);
1926 send_packet (h, &msg.header, NULL);
1927 h->info_cb.peer_cb = callback;
1928 h->info_cls = callback_cls;
1934 * Request information about tunnels of the running cadet peer.
1935 * The callback will be called for every tunnel of the service.
1936 * Only one info request (of any kind) can be active at once.
1938 * WARNING: unstable API, likely to change in the future!
1940 * @param h Handle to the cadet peer.
1941 * @param callback Function to call with the requested data.
1942 * @param callback_cls Closure for @c callback.
1944 * @return #GNUNET_OK / #GNUNET_SYSERR
1947 GNUNET_CADET_get_tunnels (struct GNUNET_CADET_Handle *h,
1948 GNUNET_CADET_TunnelsCB callback,
1951 if (NULL != h->info_cb.tunnels_cb)
1954 return GNUNET_SYSERR;
1956 send_info_request (h, GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNELS);
1957 h->info_cb.tunnels_cb = callback;
1958 h->info_cls = callback_cls;
1964 * Cancel a monitor request. The monitor callback will not be called.
1966 * @param h Cadet handle.
1968 * @return Closure given to GNUNET_CADET_get_tunnels.
1971 GNUNET_CADET_get_tunnels_cancel (struct GNUNET_CADET_Handle *h)
1975 h->info_cb.tunnels_cb = NULL;
1985 * Request information about a tunnel of the running cadet peer.
1986 * The callback will be called for the tunnel once.
1987 * Only one info request (of any kind) can be active at once.
1989 * WARNING: unstable API, likely to change in the future!
1991 * @param h Handle to the cadet peer.
1992 * @param id Peer whose tunnel to examine.
1993 * @param callback Function to call with the requested data.
1994 * @param callback_cls Closure for @c callback.
1996 * @return #GNUNET_OK / #GNUNET_SYSERR
1999 GNUNET_CADET_get_tunnel (struct GNUNET_CADET_Handle *h,
2000 const struct GNUNET_PeerIdentity *id,
2001 GNUNET_CADET_TunnelCB callback,
2004 struct GNUNET_CADET_LocalInfo msg;
2006 if (NULL != h->info_cb.tunnel_cb)
2009 return GNUNET_SYSERR;
2012 memset (&msg, 0, sizeof (msg));
2013 msg.header.size = htons (sizeof (msg));
2014 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNEL);
2016 send_packet (h, &msg.header, NULL);
2017 h->info_cb.tunnel_cb = callback;
2018 h->info_cls = callback_cls;
2024 * Request information about a specific channel of the running cadet peer.
2026 * WARNING: unstable API, likely to change in the future!
2027 * FIXME Add destination option.
2029 * @param h Handle to the cadet peer.
2030 * @param initiator ID of the owner of the channel.
2031 * @param channel_number Channel number.
2032 * @param callback Function to call with the requested data.
2033 * @param callback_cls Closure for @c callback.
2035 * @return #GNUNET_OK / #GNUNET_SYSERR
2038 GNUNET_CADET_show_channel (struct GNUNET_CADET_Handle *h,
2039 struct GNUNET_PeerIdentity *initiator,
2040 unsigned int channel_number,
2041 GNUNET_CADET_ChannelCB callback,
2044 struct GNUNET_CADET_LocalInfo msg;
2046 if (NULL != h->info_cb.channel_cb)
2049 return GNUNET_SYSERR;
2052 msg.header.size = htons (sizeof (msg));
2053 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_CHANNEL);
2054 msg.peer = *initiator;
2055 msg.channel_id = htonl (channel_number);
2056 // msg.reserved = 0;
2057 send_packet (h, &msg.header, NULL);
2058 h->info_cb.channel_cb = callback;
2059 h->info_cls = callback_cls;
2065 * Function called to notify a client about the connection
2066 * begin ready to queue more data. "buf" will be
2067 * NULL and "size" zero if the connection was closed for
2068 * writing in the meantime.
2070 * @param cls closure
2071 * @param size number of bytes available in buf
2072 * @param buf where the callee should write the message
2073 * @return number of bytes written to buf
2076 cadet_mq_ntr (void *cls, size_t size,
2079 struct GNUNET_MQ_Handle *mq = cls;
2080 struct CadetMQState *state = GNUNET_MQ_impl_state (mq);
2081 const struct GNUNET_MessageHeader *msg = GNUNET_MQ_impl_current (mq);
2087 GNUNET_MQ_inject_error (mq, GNUNET_MQ_ERROR_WRITE);
2090 msize = ntohs (msg->size);
2091 GNUNET_assert (msize <= size);
2092 memcpy (buf, msg, msize);
2093 GNUNET_MQ_impl_send_continue (mq);
2099 * Signature of functions implementing the
2100 * sending functionality of a message queue.
2102 * @param mq the message queue
2103 * @param msg the message to send
2104 * @param impl_state state of the implementation
2107 cadet_mq_send_impl (struct GNUNET_MQ_Handle *mq,
2108 const struct GNUNET_MessageHeader *msg,
2111 struct CadetMQState *state = impl_state;
2113 GNUNET_assert (NULL == state->th);
2115 GNUNET_CADET_notify_transmit_ready (state->channel,
2116 /* FIXME: add option for corking */
2118 GNUNET_TIME_UNIT_FOREVER_REL,
2126 * Signature of functions implementing the
2127 * destruction of a message queue.
2128 * Implementations must not free 'mq', but should
2129 * take care of 'impl_state'.
2131 * @param mq the message queue to destroy
2132 * @param impl_state state of the implementation
2135 cadet_mq_destroy_impl (struct GNUNET_MQ_Handle *mq,
2138 struct CadetMQState *state = impl_state;
2140 if (NULL != state->th)
2141 GNUNET_CADET_notify_transmit_ready_cancel (state->th);
2143 GNUNET_free (state);
2148 * Create a message queue for a cadet channel.
2149 * The message queue can only be used to transmit messages,
2150 * not to receive them.
2152 * @param channel the channel to create the message qeue for
2153 * @return a message queue to messages over the channel
2155 struct GNUNET_MQ_Handle *
2156 GNUNET_CADET_mq_create (struct GNUNET_CADET_Channel *channel)
2158 struct GNUNET_MQ_Handle *mq;
2159 struct CadetMQState *state;
2161 state = GNUNET_new (struct CadetMQState);
2162 state->channel = channel;
2164 mq = GNUNET_MQ_queue_for_callbacks (&cadet_mq_send_impl,
2165 &cadet_mq_destroy_impl,
2166 NULL, /* FIXME: cancel impl. */
2168 NULL, /* no msg handlers */
2169 NULL, /* no err handlers */
2170 NULL); /* no handler cls */