2 This file is part of GNUnet.
3 Copyright (C) 2011, 2017 GNUnet e.V.
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
21 * @file cadet/cadet_api.c
22 * @brief cadet api: client implementation of cadet service
23 * @author Bartlomiej Polot
27 #include "gnunet_util_lib.h"
28 #include "gnunet_constants.h"
29 #include "gnunet_cadet_service.h"
31 #include "cadet_protocol.h"
33 #define LOG(kind,...) GNUNET_log_from (kind, "cadet-api",__VA_ARGS__)
35 /******************************************************************************/
36 /************************ DATA STRUCTURES ****************************/
37 /******************************************************************************/
40 * Transmission queue to the service
42 struct GNUNET_CADET_TransmitHandle
47 struct GNUNET_CADET_TransmitHandle *next;
52 struct GNUNET_CADET_TransmitHandle *prev;
55 * Channel this message is sent on / for (may be NULL for control messages).
57 struct GNUNET_CADET_Channel *channel;
62 struct GNUNET_SCHEDULER_Task *request_data_task;
65 * Callback to obtain the message to transmit, or NULL if we
66 * got the message in 'data'. Notice that messages built
67 * by 'notify' need to be encapsulated with information about
70 GNUNET_CONNECTION_TransmitReadyNotify notify;
73 * Closure for 'notify'
78 * Size of the payload.
90 GNUNET_CADET_ChannelCB channel_cb;
95 GNUNET_CADET_PeersCB peers_cb;
100 GNUNET_CADET_PeerCB peer_cb;
105 GNUNET_CADET_TunnelsCB tunnels_cb;
110 GNUNET_CADET_TunnelCB tunnel_cb;
115 * Opaque handle to the service.
117 struct GNUNET_CADET_Handle
120 * Message queue (if available).
122 struct GNUNET_MQ_Handle *mq;
125 * Set of handlers used for processing incoming messages in the channels
127 const struct GNUNET_CADET_MessageHandler *message_handlers;
130 * Number of handlers in the handlers array.
132 unsigned int n_handlers;
137 struct GNUNET_CONTAINER_MultiHashMap *ports;
140 * Double linked list of the channels this client is connected to, head.
142 struct GNUNET_CADET_Channel *channels_head;
145 * Double linked list of the channels this client is connected to, tail.
147 struct GNUNET_CADET_Channel *channels_tail;
150 * Callback for inbound channel disconnection
152 GNUNET_CADET_ChannelEndHandler *cleaner;
155 * Closure for all the handlers given by the client
160 * Messages to send to the service, head.
162 struct GNUNET_CADET_TransmitHandle *th_head;
165 * Messages to send to the service, tail.
167 struct GNUNET_CADET_TransmitHandle *th_tail;
170 * child of the next channel to create (to avoid reusing IDs often)
172 struct GNUNET_CADET_ClientChannelNumber next_chid;
175 * Configuration given by the client, in case of reconnection
177 const struct GNUNET_CONFIGURATION_Handle *cfg;
180 * Time to the next reconnect in case one reconnect fails
182 struct GNUNET_TIME_Relative reconnect_time;
185 * Task for trying to reconnect.
187 struct GNUNET_SCHEDULER_Task * reconnect_task;
190 * Callback for an info task (only one active at a time).
192 union CadetInfoCB info_cb;
195 * Info callback closure for @c info_cb.
202 * Description of a peer
204 struct GNUNET_CADET_Peer
207 * ID of the peer in short form
212 * Channel this peer belongs to
214 struct GNUNET_CADET_Channel *t;
219 * Opaque handle to a channel.
221 struct GNUNET_CADET_Channel
226 struct GNUNET_CADET_Channel *next;
231 struct GNUNET_CADET_Channel *prev;
234 * Handle to the cadet this channel belongs to
236 struct GNUNET_CADET_Handle *cadet;
239 * Local ID of the channel
241 struct GNUNET_CADET_ClientChannelNumber chid;
244 * Channel's port, if any.
246 struct GNUNET_CADET_Port *port;
249 * Other end of the channel.
254 * Any data the caller wants to put in here
259 * Size of packet queued in this channel
261 unsigned int packet_size;
264 * Channel options: reliability, etc.
266 enum GNUNET_CADET_ChannelOption options;
269 * Are we allowed to send to the service?
277 * Opaque handle to a port.
279 struct GNUNET_CADET_Port
282 * Handle to the CADET session this port belongs to.
284 struct GNUNET_CADET_Handle *cadet;
289 struct GNUNET_HashCode *hash;
292 * Callback handler for incoming channels on this port.
294 GNUNET_CADET_InboundChannelNotificationHandler *handler;
297 * Closure for @a handler.
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 /*********************** AUXILIARY FUNCTIONS *************************/
323 /******************************************************************************/
326 * Check if transmission is a payload packet.
328 * @param th Transmission handle.
330 * @return #GNUNET_YES if it is a payload packet,
331 * #GNUNET_NO if it is a cadet management packet.
334 th_is_payload (struct GNUNET_CADET_TransmitHandle *th)
336 return (th->notify != NULL) ? GNUNET_YES : GNUNET_NO;
341 * Find the Port struct for a hash.
343 * @param h CADET handle.
344 * @param hash HashCode for the port number.
346 * @return The port handle if known, NULL otherwise.
348 static struct GNUNET_CADET_Port *
349 find_port (const struct GNUNET_CADET_Handle *h,
350 const struct GNUNET_HashCode *hash)
352 struct GNUNET_CADET_Port *p;
354 p = GNUNET_CONTAINER_multihashmap_get (h->ports, hash);
361 * Get the channel handler for the channel specified by id from the given handle
363 * @param h Cadet handle
364 * @param chid ID of the wanted channel
365 * @return handle to the required channel or NULL if not found
367 static struct GNUNET_CADET_Channel *
368 retrieve_channel (struct GNUNET_CADET_Handle *h,
369 struct GNUNET_CADET_ClientChannelNumber chid)
371 struct GNUNET_CADET_Channel *ch;
373 for (ch = h->channels_head; NULL != ch; ch = ch->next)
374 if (ch->chid.channel_of_client == chid.channel_of_client)
381 * Create a new channel and insert it in the channel list of the cadet handle
383 * @param h Cadet handle
384 * @param chid Desired chid of the channel, 0 to assign one automatically.
386 * @return Handle to the created channel.
388 static struct GNUNET_CADET_Channel *
389 create_channel (struct GNUNET_CADET_Handle *h,
390 struct GNUNET_CADET_ClientChannelNumber chid)
392 struct GNUNET_CADET_Channel *ch;
394 ch = GNUNET_new (struct GNUNET_CADET_Channel);
395 GNUNET_CONTAINER_DLL_insert (h->channels_head,
399 if (0 == chid.channel_of_client)
401 ch->chid = h->next_chid;
402 while (NULL != retrieve_channel (h,
405 h->next_chid.channel_of_client
406 = htonl (1 + ntohl (h->next_chid.channel_of_client));
407 if (0 == ntohl (h->next_chid.channel_of_client))
408 h->next_chid.channel_of_client
409 = htonl (GNUNET_CADET_LOCAL_CHANNEL_ID_CLI);
416 ch->allow_send = GNUNET_NO;
422 * Destroy the specified channel.
423 * - Destroys all peers, calling the disconnect callback on each if needed
424 * - Cancels all outgoing traffic for that channel, calling respective notifys
425 * - Calls cleaner if channel was inbound
426 * - Frees all memory used
428 * @param ch Pointer to the channel.
429 * @param call_cleaner Whether to call the cleaner handler.
431 * @return Handle to the required channel or NULL if not found.
433 // FIXME: simplify: call_cleaner is always #GNUNET_YES!!!
435 destroy_channel (struct GNUNET_CADET_Channel *ch, int call_cleaner)
437 struct GNUNET_CADET_Handle *h;
438 struct GNUNET_CADET_TransmitHandle *th;
439 struct GNUNET_CADET_TransmitHandle *next;
441 LOG (GNUNET_ERROR_TYPE_DEBUG, " destroy_channel %X\n", ch->chid);
450 GNUNET_CONTAINER_DLL_remove (h->channels_head,
454 /* signal channel destruction */
455 if ( (NULL != h->cleaner) && (0 != ch->peer) && (GNUNET_YES == call_cleaner) )
457 LOG (GNUNET_ERROR_TYPE_DEBUG, " calling cleaner\n");
458 h->cleaner (h->cls, ch, ch->ctx);
461 /* check that clients did not leave messages behind in the queue */
462 for (th = h->th_head; NULL != th; th = next)
465 if (th->channel != ch)
467 /* Clients should have aborted their requests already.
468 * Management traffic should be ok, as clients can't cancel that.
469 * If the service crashed and we are reconnecting, it's ok.
471 GNUNET_break (GNUNET_NO == th_is_payload (th));
472 GNUNET_CADET_notify_transmit_ready_cancel (th);
476 GNUNET_PEER_change_rc (ch->peer, -1);
483 * Add a transmit handle to the transmission queue and set the
486 * @param h cadet handle with the queue head and tail
487 * @param th handle to the packet to be transmitted
490 add_to_queue (struct GNUNET_CADET_Handle *h,
491 struct GNUNET_CADET_TransmitHandle *th)
493 GNUNET_CONTAINER_DLL_insert_tail (h->th_head, h->th_tail, th);
498 * Remove a transmit handle from the transmission queue, if present.
500 * Safe to call even if not queued.
502 * @param th handle to the packet to be unqueued.
505 remove_from_queue (struct GNUNET_CADET_TransmitHandle *th)
507 struct GNUNET_CADET_Handle *h = th->channel->cadet;
509 /* It might or might not have been queued (rarely not), but check anyway. */
510 if (NULL != th->next || h->th_tail == th)
512 GNUNET_CONTAINER_DLL_remove (h->th_head, h->th_tail, th);
518 * Send an ack on the channel to confirm the processing of a message.
520 * @param ch Channel on which to send the ACK.
523 send_ack (struct GNUNET_CADET_Channel *ch)
525 struct GNUNET_CADET_LocalAck *msg;
526 struct GNUNET_MQ_Envelope *env;
528 env = GNUNET_MQ_msg (msg,
529 GNUNET_MESSAGE_TYPE_CADET_LOCAL_ACK);
531 LOG (GNUNET_ERROR_TYPE_DEBUG,
532 "Sending ACK on channel %X\n",
533 ch->chid.channel_of_client);
534 msg->channel_id = ch->chid;
535 GNUNET_MQ_send (ch->cadet->mq,
541 /******************************************************************************/
542 /*********************** RECEIVE HANDLERS ****************************/
543 /******************************************************************************/
547 * Call the @a notify callback given to #GNUNET_CADET_notify_transmit_ready to
548 * request the data to send over MQ. Since MQ manages the queue, this function
549 * is scheduled immediatly after a transmit ready notification.
551 * @param cls Closure (transmit handle).
554 request_data (void *cls)
556 struct GNUNET_CADET_TransmitHandle *th = cls;
557 struct GNUNET_CADET_LocalData *msg;
558 struct GNUNET_MQ_Envelope *env;
561 LOG (GNUNET_ERROR_TYPE_DEBUG, "Requesting Data: %u bytes\n", th->size);
563 GNUNET_assert (GNUNET_YES == th->channel->allow_send);
564 th->channel->allow_send = GNUNET_NO;
565 th->request_data_task = NULL;
566 th->channel->packet_size = 0;
567 remove_from_queue (th);
569 env = GNUNET_MQ_msg_extra (msg,
571 GNUNET_MESSAGE_TYPE_CADET_LOCAL_DATA);
572 msg->channel_id = th->channel->chid;
573 osize = th->notify (th->notify_cls,
576 GNUNET_assert (osize == th->size);
577 GNUNET_MQ_send (th->channel->cadet->mq,
584 * Process the new channel notification and add it to the channels in the handle
586 * @param h The cadet handle
587 * @param msg A message with the details of the new incoming channel
590 handle_channel_created (void *cls,
591 const struct GNUNET_CADET_TunnelCreateMessage *msg)
593 struct GNUNET_CADET_Handle *h = cls;
594 struct GNUNET_CADET_Channel *ch;
595 struct GNUNET_CADET_Port *port;
596 const struct GNUNET_HashCode *port_number;
597 struct GNUNET_CADET_ClientChannelNumber chid;
599 chid = msg->channel_id;
600 port_number = &msg->port;
601 LOG (GNUNET_ERROR_TYPE_DEBUG,
602 "Creating incoming channel %X [%s]\n",
603 ntohl (chid.channel_of_client),
604 GNUNET_h2s (port_number));
605 if (ntohl (chid.channel_of_client) >= GNUNET_CADET_LOCAL_CHANNEL_ID_CLI)
610 port = find_port (h, port_number);
615 ch = create_channel (h, chid);
616 ch->allow_send = GNUNET_NO;
617 ch->peer = GNUNET_PEER_intern (&msg->peer);
621 ch->options = ntohl (msg->opt);
623 LOG (GNUNET_ERROR_TYPE_DEBUG, " created channel %p\n", ch);
624 ctx = port->handler (port->cls, ch, &msg->peer, port->hash, ch->options);
627 LOG (GNUNET_ERROR_TYPE_DEBUG, "User notified\n");
631 struct GNUNET_CADET_TunnelDestroyMessage *d_msg;
632 struct GNUNET_MQ_Envelope *env;
634 LOG (GNUNET_ERROR_TYPE_DEBUG, "No handler for incoming channels\n");
635 env = GNUNET_MQ_msg (d_msg,
636 GNUNET_MESSAGE_TYPE_CADET_LOCAL_TUNNEL_DESTROY);
637 d_msg->channel_id = msg->channel_id;
638 GNUNET_MQ_send (h->mq, env);
645 * Process the channel destroy notification and free associated resources
647 * @param h The cadet handle
648 * @param msg A message with the details of the channel being destroyed
651 handle_channel_destroy (void *cls,
652 const struct GNUNET_CADET_TunnelDestroyMessage *msg)
654 struct GNUNET_CADET_Handle *h = cls;
655 struct GNUNET_CADET_Channel *ch;
656 struct GNUNET_CADET_ClientChannelNumber chid;
658 chid = msg->channel_id;
659 LOG (GNUNET_ERROR_TYPE_DEBUG,
660 "Channel %X Destroy from service\n",
661 ntohl (chid.channel_of_client));
662 ch = retrieve_channel (h,
667 LOG (GNUNET_ERROR_TYPE_DEBUG,
668 "channel %X unknown\n",
669 ntohl (chid.channel_of_client));
678 * Check that message received from CADET service is well-formed.
680 * @param cls the `struct GNUNET_CADET_Handle`
681 * @param message the message we got
682 * @return #GNUNET_OK if the message is well-formed,
683 * #GNUNET_SYSERR otherwise
686 check_local_data (void *cls,
687 const struct GNUNET_CADET_LocalData *message)
689 struct GNUNET_CADET_Handle *h = cls;
690 struct GNUNET_CADET_Channel *ch;
693 size = ntohs (message->header.size);
694 if (sizeof (*message) + sizeof (struct GNUNET_MessageHeader) > size)
697 return GNUNET_SYSERR;
700 ch = retrieve_channel (h,
701 message->channel_id);
705 return GNUNET_SYSERR;
713 * Process the incoming data packets, call appropriate handlers.
715 * @param h The cadet handle
716 * @param message A message encapsulating the data
719 handle_local_data (void *cls,
720 const struct GNUNET_CADET_LocalData *message)
722 struct GNUNET_CADET_Handle *h = cls;
723 const struct GNUNET_MessageHeader *payload;
724 const struct GNUNET_CADET_MessageHandler *handler;
725 struct GNUNET_CADET_Channel *ch;
729 LOG (GNUNET_ERROR_TYPE_DEBUG,
730 "Got a data message!\n");
731 ch = retrieve_channel (h, message->channel_id);
732 GNUNET_assert (NULL != ch);
734 payload = (struct GNUNET_MessageHeader *) &message[1];
735 LOG (GNUNET_ERROR_TYPE_DEBUG, " %s data on channel %s [%X]\n",
736 GC_f2s (ntohl (ch->chid.channel_of_client) >=
737 GNUNET_CADET_LOCAL_CHANNEL_ID_CLI),
738 GNUNET_i2s (GNUNET_PEER_resolve2 (ch->peer)),
739 ntohl (message->channel_id.channel_of_client));
741 type = ntohs (payload->type);
742 LOG (GNUNET_ERROR_TYPE_DEBUG, " payload type %s\n", GC_m2s (type));
743 for (i = 0; i < h->n_handlers; i++)
745 handler = &h->message_handlers[i];
746 LOG (GNUNET_ERROR_TYPE_DEBUG, " checking handler for type %u\n",
748 if (handler->type == type)
751 handler->callback (h->cls, ch, &ch->ctx, payload))
753 LOG (GNUNET_ERROR_TYPE_DEBUG, "callback caused disconnection\n");
754 GNUNET_CADET_channel_destroy (ch);
759 LOG (GNUNET_ERROR_TYPE_DEBUG,
760 "callback completed successfully\n");
769 * Process a local ACK message, enabling the client to send
770 * more data to the service.
772 * @param h Cadet handle.
773 * @param message Message itself.
776 handle_local_ack (void *cls,
777 const struct GNUNET_CADET_LocalAck *message)
779 struct GNUNET_CADET_Handle *h = cls;
780 struct GNUNET_CADET_Channel *ch;
781 struct GNUNET_CADET_ClientChannelNumber chid;
783 LOG (GNUNET_ERROR_TYPE_DEBUG, "Got an ACK!\n");
784 chid = message->channel_id;
785 ch = retrieve_channel (h, chid);
788 LOG (GNUNET_ERROR_TYPE_DEBUG,
789 "ACK on unknown channel %X\n",
790 ntohl (chid.channel_of_client));
793 LOG (GNUNET_ERROR_TYPE_DEBUG,
795 ntohl (ch->chid.channel_of_client));
796 ch->allow_send = GNUNET_YES;
797 if (0 < ch->packet_size)
799 struct GNUNET_CADET_TransmitHandle *th;
800 struct GNUNET_CADET_TransmitHandle *next;
801 LOG (GNUNET_ERROR_TYPE_DEBUG,
802 " pending data, sending %u bytes!\n",
804 for (th = h->th_head; NULL != th; th = next)
807 if (th->channel == ch)
809 GNUNET_assert (NULL == th->request_data_task);
810 th->request_data_task = GNUNET_SCHEDULER_add_now (&request_data, th);
814 /* Complain if we got thru all th without sending anything, ch was wrong */
815 GNUNET_break (NULL != th);
820 * Reconnect to the service, retransmit all infomation to try to restore the
823 * @param h handle to the cadet
825 * @return #GNUNET_YES in case of sucess, #GNUNET_NO otherwise (service down...)
828 reconnect (struct GNUNET_CADET_Handle *h);
832 * Reconnect callback: tries to reconnect again after a failer previous
835 * @param cls closure (cadet handle)
838 reconnect_cbk (void *cls);
842 * Generic error handler, called with the appropriate error code and
843 * the same closure specified at the creation of the message queue.
844 * Not every message queue implementation supports an error handler.
846 * @param cls closure, a `struct GNUNET_CORE_Handle *`
847 * @param error error code
850 handle_mq_error (void *cls,
851 enum GNUNET_MQ_Error error)
853 struct GNUNET_CADET_Handle *h = cls;
855 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MQ ERROR: %u\n", error);
856 GNUNET_MQ_destroy (h->mq);
863 * Process a local reply about info on all channels, pass info to the user.
865 * @param h Cadet handle.
866 * @param message Message itself.
869 // process_get_channels (struct GNUNET_CADET_Handle *h,
870 // const struct GNUNET_MessageHeader *message)
872 // struct GNUNET_CADET_LocalInfo *msg;
874 // GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Get Channels messasge received\n");
876 // if (NULL == h->channels_cb)
878 // GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " ignored\n");
882 // msg = (struct GNUNET_CADET_LocalInfo *) message;
883 // if (ntohs (message->size) !=
884 // (sizeof (struct GNUNET_CADET_LocalInfo) +
885 // sizeof (struct GNUNET_PeerIdentity)))
887 // GNUNET_break_op (0);
888 // GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
889 // "Get channels message: size %hu - expected %u\n",
890 // ntohs (message->size),
891 // sizeof (struct GNUNET_CADET_LocalInfo));
894 // h->channels_cb (h->channels_cls,
895 // ntohl (msg->channel_id),
897 // &msg->destination);
903 * Process a local monitor_channel reply, pass info to the user.
905 * @param h Cadet handle.
906 * @param message Message itself.
909 // process_show_channel (struct GNUNET_CADET_Handle *h,
910 // const struct GNUNET_MessageHeader *message)
912 // struct GNUNET_CADET_LocalInfo *msg;
915 // GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Show Channel messasge received\n");
917 // if (NULL == h->channel_cb)
919 // GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " ignored\n");
923 // /* Verify message sanity */
924 // msg = (struct GNUNET_CADET_LocalInfo *) message;
925 // esize = sizeof (struct GNUNET_CADET_LocalInfo);
926 // if (ntohs (message->size) != esize)
928 // GNUNET_break_op (0);
929 // GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
930 // "Show channel message: size %hu - expected %u\n",
931 // ntohs (message->size),
934 // h->channel_cb (h->channel_cls, NULL, NULL);
935 // h->channel_cb = NULL;
936 // h->channel_cls = NULL;
941 // h->channel_cb (h->channel_cls,
942 // &msg->destination,
949 * Check that message received from CADET service is well-formed.
951 * @param cls the `struct GNUNET_CADET_Handle`
952 * @param message the message we got
953 * @return #GNUNET_OK if the message is well-formed,
954 * #GNUNET_SYSERR otherwise
957 check_get_peers (void *cls,
958 const struct GNUNET_CADET_LocalInfoPeer *message)
960 struct GNUNET_CADET_Handle *h = cls;
963 if (NULL == h->info_cb.peers_cb)
965 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
966 " no handler for peesr monitor message!\n");
967 return GNUNET_SYSERR;
970 size = ntohs (message->header.size);
971 if (sizeof (struct GNUNET_CADET_LocalInfoPeer) > size)
973 h->info_cb.peers_cb (h->info_cls, NULL, -1, 0, 0);
974 h->info_cb.peers_cb = NULL;
976 return GNUNET_SYSERR;
984 * Process a local reply about info on all tunnels, pass info to the user.
986 * @param cls Closure (Cadet handle).
987 * @param msg Message itself.
990 handle_get_peers (void *cls,
991 const struct GNUNET_CADET_LocalInfoPeer *msg)
993 struct GNUNET_CADET_Handle *h = cls;
994 h->info_cb.peers_cb (h->info_cls, &msg->destination,
995 (int) ntohs (msg->tunnel),
996 (unsigned int ) ntohs (msg->paths),
1002 * Check that message received from CADET service is well-formed.
1004 * @param cls the `struct GNUNET_CADET_Handle`
1005 * @param message the message we got
1006 * @return #GNUNET_OK if the message is well-formed,
1007 * #GNUNET_SYSERR otherwise
1010 check_get_peer (void *cls,
1011 const struct GNUNET_CADET_LocalInfoPeer *message)
1013 struct GNUNET_CADET_Handle *h = cls;
1014 const size_t msize = sizeof (struct GNUNET_CADET_LocalInfoPeer);
1015 struct GNUNET_PeerIdentity *paths_array;
1017 unsigned int epaths;
1021 if (NULL == h->info_cb.peer_cb)
1023 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1024 " no handler for peer monitor message!\n");
1028 /* Verify message sanity */
1029 esize = ntohs (message->header.size);
1032 GNUNET_break_op (0);
1033 h->info_cb.peer_cb (h->info_cls, NULL, 0, 0, 0, NULL);
1036 if (0 != ((esize - msize) % sizeof (struct GNUNET_PeerIdentity)))
1038 GNUNET_break_op (0);
1039 h->info_cb.peer_cb (h->info_cls, NULL, 0, 0, 0, NULL);
1043 peers = (esize - msize) / sizeof (struct GNUNET_PeerIdentity);
1044 epaths = (unsigned int) ntohs (message->paths);
1045 paths_array = (struct GNUNET_PeerIdentity *) &message[1];
1047 for (int i = 0; i < peers; i++)
1049 if (0 == memcmp (&paths_array[i], &message->destination,
1050 sizeof (struct GNUNET_PeerIdentity)))
1055 if (paths != epaths)
1057 GNUNET_break_op (0);
1058 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "p:%u, e: %u\n", paths, epaths);
1059 h->info_cb.peer_cb (h->info_cls, NULL, 0, 0, 0, NULL);
1066 h->info_cb.peer_cb = NULL;
1068 return GNUNET_SYSERR;
1073 * Process a local peer info reply, pass info to the user.
1075 * @param cls Closure (Cadet handle).
1076 * @param message Message itself.
1079 handle_get_peer (void *cls,
1080 const struct GNUNET_CADET_LocalInfoPeer *message)
1082 struct GNUNET_CADET_Handle *h = cls;
1083 struct GNUNET_PeerIdentity *paths_array;
1085 unsigned int path_length;
1089 paths = (unsigned int) ntohs (message->paths);
1090 paths_array = (struct GNUNET_PeerIdentity *) &message[1];
1091 peers = (ntohs (message->header.size) - sizeof (*message))
1092 / sizeof (struct GNUNET_PeerIdentity);
1094 neighbor = GNUNET_NO;
1096 for (int i = 0; i < peers; i++)
1098 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " %s\n", GNUNET_i2s (&paths_array[i]));
1100 if (0 == memcmp (&paths_array[i], &message->destination,
1101 sizeof (struct GNUNET_PeerIdentity)))
1103 if (1 == path_length)
1104 neighbor = GNUNET_YES;
1109 /* Call Callback with tunnel info. */
1110 paths_array = (struct GNUNET_PeerIdentity *) &message[1];
1111 h->info_cb.peer_cb (h->info_cls,
1112 &message->destination,
1113 (int) ntohs (message->tunnel),
1121 * Check that message received from CADET service is well-formed.
1123 * @param cls the `struct GNUNET_CADET_Handle`
1124 * @param msg the message we got
1125 * @return #GNUNET_OK if the message is well-formed,
1126 * #GNUNET_SYSERR otherwise
1129 check_get_tunnels (void *cls,
1130 const struct GNUNET_CADET_LocalInfoTunnel *msg)
1132 struct GNUNET_CADET_Handle *h = cls;
1135 if (NULL == h->info_cb.tunnels_cb)
1137 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1138 " no handler for tunnels monitor message!\n");
1139 return GNUNET_SYSERR;
1142 size = ntohs (msg->header.size);
1143 if (sizeof (struct GNUNET_CADET_LocalInfoTunnel) > size)
1145 h->info_cb.tunnels_cb (h->info_cls, NULL, 0, 0, 0, 0);
1146 h->info_cb.tunnels_cb = NULL;
1148 return GNUNET_SYSERR;
1155 * Process a local reply about info on all tunnels, pass info to the user.
1157 * @param cls Closure (Cadet handle).
1158 * @param message Message itself.
1161 handle_get_tunnels (void *cls,
1162 const struct GNUNET_CADET_LocalInfoTunnel *msg)
1164 struct GNUNET_CADET_Handle *h = cls;
1166 h->info_cb.tunnels_cb (h->info_cls,
1168 ntohl (msg->channels),
1169 ntohl (msg->connections),
1170 ntohs (msg->estate),
1171 ntohs (msg->cstate));
1177 * Check that message received from CADET service is well-formed.
1179 * @param cls the `struct GNUNET_CADET_Handle`
1180 * @param msg the message we got
1181 * @return #GNUNET_OK if the message is well-formed,
1182 * #GNUNET_SYSERR otherwise
1185 check_get_tunnel (void *cls,
1186 const struct GNUNET_CADET_LocalInfoTunnel *msg)
1188 struct GNUNET_CADET_Handle *h = cls;
1194 if (NULL == h->info_cb.tunnel_cb)
1196 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1197 " no handler for tunnel monitor message!\n");
1201 /* Verify message sanity */
1202 msize = ntohs (msg->header.size);
1203 esize = sizeof (struct GNUNET_CADET_LocalInfoTunnel);
1206 GNUNET_break_op (0);
1207 h->info_cb.tunnel_cb (h->info_cls,
1208 NULL, 0, 0, NULL, NULL, 0, 0);
1211 ch_n = ntohl (msg->channels);
1212 c_n = ntohl (msg->connections);
1213 esize += ch_n * sizeof (struct GNUNET_CADET_ChannelTunnelNumber);
1214 esize += c_n * sizeof (struct GNUNET_CADET_ConnectionTunnelIdentifier);
1217 GNUNET_break_op (0);
1218 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1219 "m:%u, e: %u (%u ch, %u conn)\n",
1220 (unsigned int) msize,
1221 (unsigned int) esize,
1224 h->info_cb.tunnel_cb (h->info_cls,
1225 NULL, 0, 0, NULL, NULL, 0, 0);
1232 h->info_cb.tunnel_cb = NULL;
1234 return GNUNET_SYSERR;
1239 * Process a local tunnel info reply, pass info to the user.
1241 * @param cls Closure (Cadet handle).
1242 * @param msg Message itself.
1245 handle_get_tunnel (void *cls,
1246 const struct GNUNET_CADET_LocalInfoTunnel *msg)
1248 struct GNUNET_CADET_Handle *h = cls;
1251 const struct GNUNET_CADET_ConnectionTunnelIdentifier *conns;
1252 const struct GNUNET_CADET_ChannelTunnelNumber *chns;
1254 ch_n = ntohl (msg->channels);
1255 c_n = ntohl (msg->connections);
1257 /* Call Callback with tunnel info. */
1258 conns = (const struct GNUNET_CADET_ConnectionTunnelIdentifier *) &msg[1];
1259 chns = (const struct GNUNET_CADET_ChannelTunnelNumber *) &conns[c_n];
1260 h->info_cb.tunnel_cb (h->info_cls,
1266 ntohs (msg->estate),
1267 ntohs (msg->cstate));
1273 * Reconnect to the service, retransmit all infomation to try to restore the
1276 * @param h handle to the cadet
1278 * @return GNUNET_YES in case of sucess, GNUNET_NO otherwise (service down...)
1281 do_reconnect (struct GNUNET_CADET_Handle *h)
1283 struct GNUNET_MQ_MessageHandler handlers[] = {
1284 GNUNET_MQ_hd_fixed_size (channel_created,
1285 GNUNET_MESSAGE_TYPE_CADET_LOCAL_TUNNEL_CREATE,
1286 struct GNUNET_CADET_TunnelCreateMessage,
1288 GNUNET_MQ_hd_fixed_size (channel_destroy,
1289 GNUNET_MESSAGE_TYPE_CADET_LOCAL_TUNNEL_DESTROY,
1290 struct GNUNET_CADET_TunnelDestroyMessage,
1292 GNUNET_MQ_hd_var_size (local_data,
1293 GNUNET_MESSAGE_TYPE_CADET_LOCAL_DATA,
1294 struct GNUNET_CADET_LocalData,
1296 GNUNET_MQ_hd_fixed_size (local_ack,
1297 GNUNET_MESSAGE_TYPE_CADET_LOCAL_ACK,
1298 struct GNUNET_CADET_LocalAck,
1300 GNUNET_MQ_hd_var_size (get_peers,
1301 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEERS,
1302 struct GNUNET_CADET_LocalInfoPeer,
1304 GNUNET_MQ_hd_var_size (get_peer,
1305 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEER,
1306 struct GNUNET_CADET_LocalInfoPeer,
1308 GNUNET_MQ_hd_var_size (get_tunnels,
1309 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNELS,
1310 struct GNUNET_CADET_LocalInfoTunnel,
1312 GNUNET_MQ_hd_var_size (get_tunnel,
1313 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNEL,
1314 struct GNUNET_CADET_LocalInfoTunnel,
1317 // GNUNET_MQ_hd_fixed_Y size (channel_destroyed,
1318 // GNUNET_MESSAGE_TYPE_CADET_CHANNEL_OPEN_NACK_DEPRECATED,
1319 // struct GNUNET_CADET_ChannelDestroyMessage);
1320 GNUNET_MQ_handler_end ()
1323 LOG (GNUNET_ERROR_TYPE_DEBUG, "Connecting to CADET\n");
1325 GNUNET_assert (NULL == h->mq);
1326 h->mq = GNUNET_CLIENT_connect (h->cfg,
1338 h->reconnect_time = GNUNET_TIME_UNIT_MILLISECONDS;
1344 * Reconnect callback: tries to reconnect again after a failer previous
1347 * @param cls closure (cadet handle)
1350 reconnect_cbk (void *cls)
1352 struct GNUNET_CADET_Handle *h = cls;
1354 h->reconnect_task = NULL;
1360 * Reconnect to the service, retransmit all infomation to try to restore the
1363 * @param h handle to the cadet
1365 * @return #GNUNET_YES in case of sucess, #GNUNET_NO otherwise (service down...)
1368 reconnect (struct GNUNET_CADET_Handle *h)
1370 struct GNUNET_CADET_Channel *ch;
1372 LOG (GNUNET_ERROR_TYPE_DEBUG,
1373 "Requested RECONNECT, destroying all channels\n");
1374 for (ch = h->channels_head; NULL != ch; ch = h->channels_head)
1375 destroy_channel (ch, GNUNET_YES);
1376 if (NULL == h->reconnect_task)
1377 h->reconnect_task = GNUNET_SCHEDULER_add_delayed (h->reconnect_time,
1382 /******************************************************************************/
1383 /********************** API CALL DEFINITIONS *************************/
1384 /******************************************************************************/
1386 struct GNUNET_CADET_Handle *
1387 GNUNET_CADET_connect (const struct GNUNET_CONFIGURATION_Handle *cfg,
1389 GNUNET_CADET_ChannelEndHandler cleaner,
1390 const struct GNUNET_CADET_MessageHandler *handlers)
1392 struct GNUNET_CADET_Handle *h;
1394 LOG (GNUNET_ERROR_TYPE_DEBUG, "GNUNET_CADET_connect()\n");
1395 h = GNUNET_new (struct GNUNET_CADET_Handle);
1396 LOG (GNUNET_ERROR_TYPE_DEBUG, " addr %p\n", h);
1398 h->cleaner = cleaner;
1399 h->ports = GNUNET_CONTAINER_multihashmap_create (4, GNUNET_YES);
1404 GNUNET_CADET_disconnect (h);
1408 h->message_handlers = handlers;
1409 h->next_chid.channel_of_client = htonl (GNUNET_CADET_LOCAL_CHANNEL_ID_CLI);
1410 h->reconnect_time = GNUNET_TIME_UNIT_MILLISECONDS;
1411 h->reconnect_task = NULL;
1413 /* count handlers */
1414 for (h->n_handlers = 0;
1415 handlers && handlers[h->n_handlers].type;
1417 LOG (GNUNET_ERROR_TYPE_DEBUG, "GNUNET_CADET_connect() END\n");
1423 GNUNET_CADET_disconnect (struct GNUNET_CADET_Handle *handle)
1425 struct GNUNET_CADET_Channel *ch;
1426 struct GNUNET_CADET_Channel *aux;
1427 struct GNUNET_CADET_TransmitHandle *th;
1429 LOG (GNUNET_ERROR_TYPE_DEBUG,
1430 "CADET DISCONNECT\n");
1431 ch = handle->channels_head;
1435 if (ntohl (ch->chid.channel_of_client) >= GNUNET_CADET_LOCAL_CHANNEL_ID_CLI)
1438 LOG (GNUNET_ERROR_TYPE_DEBUG,
1439 "channel %X not destroyed\n",
1440 ntohl (ch->chid.channel_of_client));
1442 destroy_channel (ch,
1446 while (NULL != (th = handle->th_head))
1448 struct GNUNET_MessageHeader *msg;
1450 /* Make sure it is an allowed packet (everything else should have been
1451 * already canceled).
1453 GNUNET_break (GNUNET_NO == th_is_payload (th));
1454 msg = (struct GNUNET_MessageHeader *) &th[1];
1455 switch (ntohs(msg->type))
1457 case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_OPEN:
1458 case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DESTROY:
1459 case GNUNET_MESSAGE_TYPE_CADET_LOCAL_PORT_OPEN:
1460 case GNUNET_MESSAGE_TYPE_CADET_LOCAL_PORT_CLOSE:
1461 case GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_CHANNELS:
1462 case GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_CHANNEL:
1463 case GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEER:
1464 case GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEERS:
1465 case GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNEL:
1466 case GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNELS:
1470 LOG (GNUNET_ERROR_TYPE_ERROR, "unexpected unsent msg %s\n",
1471 GC_m2s (ntohs(msg->type)));
1474 GNUNET_CADET_notify_transmit_ready_cancel (th);
1477 if (NULL != handle->mq)
1479 GNUNET_MQ_destroy (handle->mq);
1482 if (NULL != handle->reconnect_task)
1484 GNUNET_SCHEDULER_cancel(handle->reconnect_task);
1485 handle->reconnect_task = NULL;
1488 GNUNET_CONTAINER_multihashmap_destroy (handle->ports);
1489 handle->ports = NULL;
1490 GNUNET_free (handle);
1495 * Open a port to receive incomming channels.
1497 * @param h CADET handle.
1498 * @param port Hash representing the port number.
1499 * @param new_channel Function called when an channel is received.
1500 * @param new_channel_cls Closure for @a new_channel.
1502 * @return Port handle.
1504 struct GNUNET_CADET_Port *
1505 GNUNET_CADET_open_port (struct GNUNET_CADET_Handle *h,
1506 const struct GNUNET_HashCode *port,
1507 GNUNET_CADET_InboundChannelNotificationHandler
1509 void *new_channel_cls)
1511 struct GNUNET_CADET_PortMessage *msg;
1512 struct GNUNET_MQ_Envelope *env;
1513 struct GNUNET_CADET_Port *p;
1515 GNUNET_assert (NULL != new_channel);
1516 p = GNUNET_new (struct GNUNET_CADET_Port);
1518 p->hash = GNUNET_new (struct GNUNET_HashCode);
1520 p->handler = new_channel;
1521 p->cls = new_channel_cls;
1522 GNUNET_assert (GNUNET_OK ==
1523 GNUNET_CONTAINER_multihashmap_put (h->ports,
1526 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1528 env = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_CADET_LOCAL_PORT_OPEN);
1529 msg->port = *p->hash;
1530 GNUNET_MQ_send (h->mq, env);
1536 * Close a port opened with @a GNUNET_CADET_open_port.
1537 * The @a new_channel callback will no longer be called.
1539 * @param p Port handle.
1542 GNUNET_CADET_close_port (struct GNUNET_CADET_Port *p)
1544 struct GNUNET_CADET_PortMessage *msg;
1545 struct GNUNET_MQ_Envelope *env;
1547 env = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_CADET_LOCAL_PORT_CLOSE);
1549 msg->port = *p->hash;
1550 GNUNET_MQ_send (p->cadet->mq, env);
1551 GNUNET_CONTAINER_multihashmap_remove (p->cadet->ports, p->hash, p);
1552 GNUNET_free (p->hash);
1558 * Create a new channel towards a remote peer.
1560 * If the destination port is not open by any peer or the destination peer
1561 * does not accept the channel, #GNUNET_CADET_ChannelEndHandler will be called
1564 * @param h cadet handle
1565 * @param channel_ctx client's channel context to associate with the channel
1566 * @param peer peer identity the channel should go to
1567 * @param port Port hash (port number).
1568 * @param options CadetOption flag field, with all desired option bits set to 1.
1570 * @return handle to the channel
1572 struct GNUNET_CADET_Channel *
1573 GNUNET_CADET_channel_create (struct GNUNET_CADET_Handle *h,
1575 const struct GNUNET_PeerIdentity *peer,
1576 const struct GNUNET_HashCode *port,
1577 enum GNUNET_CADET_ChannelOption options)
1579 struct GNUNET_CADET_TunnelCreateMessage *msg;
1580 struct GNUNET_MQ_Envelope *env;
1581 struct GNUNET_CADET_Channel *ch;
1582 struct GNUNET_CADET_ClientChannelNumber chid;
1584 LOG (GNUNET_ERROR_TYPE_DEBUG,
1585 "Creating new channel to %s:%u\n",
1586 GNUNET_i2s (peer), port);
1587 chid.channel_of_client = htonl (0);
1588 ch = create_channel (h, chid);
1589 LOG (GNUNET_ERROR_TYPE_DEBUG, " at %p\n", ch);
1590 LOG (GNUNET_ERROR_TYPE_DEBUG, " number %X\n",
1591 ntohl (ch->chid.channel_of_client));
1592 ch->ctx = channel_ctx;
1593 ch->peer = GNUNET_PEER_intern (peer);
1595 env = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_CADET_LOCAL_TUNNEL_CREATE);
1596 msg->channel_id = ch->chid;
1599 msg->opt = htonl (options);
1600 ch->allow_send = GNUNET_NO;
1601 GNUNET_MQ_send (h->mq,
1608 GNUNET_CADET_channel_destroy (struct GNUNET_CADET_Channel *channel)
1610 struct GNUNET_CADET_Handle *h;
1611 struct GNUNET_CADET_TunnelDestroyMessage *msg;
1612 struct GNUNET_MQ_Envelope *env;
1613 struct GNUNET_CADET_TransmitHandle *th;
1614 struct GNUNET_CADET_TransmitHandle *next;
1616 LOG (GNUNET_ERROR_TYPE_DEBUG, "Destroying channel\n");
1618 for (th = h->th_head; th != NULL; th = next)
1621 if (th->channel == channel)
1624 if (GNUNET_YES == th_is_payload (th))
1626 /* applications should cancel before destroying channel */
1627 LOG (GNUNET_ERROR_TYPE_WARNING,
1628 "Channel destroyed without cancelling transmission requests\n");
1629 th->notify (th->notify_cls, 0, NULL);
1633 LOG (GNUNET_ERROR_TYPE_WARNING, "no meta-traffic should be queued\n");
1635 GNUNET_CONTAINER_DLL_remove (h->th_head, h->th_tail, th);
1636 GNUNET_CADET_notify_transmit_ready_cancel (th);
1640 env = GNUNET_MQ_msg (msg,
1641 GNUNET_MESSAGE_TYPE_CADET_LOCAL_TUNNEL_DESTROY);
1642 msg->channel_id = channel->chid;
1643 GNUNET_MQ_send (h->mq, env);
1645 destroy_channel (channel, GNUNET_YES);
1650 * Get information about a channel.
1652 * @param channel Channel handle.
1653 * @param option Query (GNUNET_CADET_OPTION_*).
1654 * @param ... dependant on option, currently not used
1656 * @return Union with an answer to the query.
1658 const union GNUNET_CADET_ChannelInfo *
1659 GNUNET_CADET_channel_get_info (struct GNUNET_CADET_Channel *channel,
1660 enum GNUNET_CADET_ChannelOption option, ...)
1662 static int bool_flag;
1663 const union GNUNET_CADET_ChannelInfo *ret;
1667 case GNUNET_CADET_OPTION_NOBUFFER:
1668 case GNUNET_CADET_OPTION_RELIABLE:
1669 case GNUNET_CADET_OPTION_OUT_OF_ORDER:
1670 if (0 != (option & channel->options))
1671 bool_flag = GNUNET_YES;
1673 bool_flag = GNUNET_NO;
1674 ret = (const union GNUNET_CADET_ChannelInfo *) &bool_flag;
1676 case GNUNET_CADET_OPTION_PEER:
1677 ret = (const union GNUNET_CADET_ChannelInfo *) GNUNET_PEER_resolve2 (channel->peer);
1688 struct GNUNET_CADET_TransmitHandle *
1689 GNUNET_CADET_notify_transmit_ready (struct GNUNET_CADET_Channel *channel,
1691 struct GNUNET_TIME_Relative maxdelay,
1693 GNUNET_CONNECTION_TransmitReadyNotify notify,
1696 struct GNUNET_CADET_TransmitHandle *th;
1698 GNUNET_assert (NULL != channel);
1699 GNUNET_assert (GNUNET_CONSTANTS_MAX_CADET_MESSAGE_SIZE >= notify_size);
1700 LOG (GNUNET_ERROR_TYPE_DEBUG, "CADET NOTIFY TRANSMIT READY\n");
1701 LOG (GNUNET_ERROR_TYPE_DEBUG, " on channel %X\n", channel->chid);
1702 LOG (GNUNET_ERROR_TYPE_DEBUG, " allow_send %d\n", channel->allow_send);
1703 if (ntohl (channel->chid.channel_of_client) >=
1704 GNUNET_CADET_LOCAL_CHANNEL_ID_CLI)
1705 LOG (GNUNET_ERROR_TYPE_DEBUG, " to origin\n");
1707 LOG (GNUNET_ERROR_TYPE_DEBUG, " to destination\n");
1708 LOG (GNUNET_ERROR_TYPE_DEBUG, " payload size %u\n", notify_size);
1709 GNUNET_assert (NULL != notify);
1710 GNUNET_assert (0 == channel->packet_size); // Only one data packet allowed
1712 if (GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us != maxdelay.rel_value_us)
1714 LOG (GNUNET_ERROR_TYPE_WARNING,
1715 "CADET transmit ready timeout is deprected (has no effect)\n");
1718 th = GNUNET_new (struct GNUNET_CADET_TransmitHandle);
1719 th->channel = channel;
1720 th->size = notify_size;
1721 channel->packet_size = th->size;
1722 LOG (GNUNET_ERROR_TYPE_DEBUG, " total size %u\n", th->size);
1723 th->notify = notify;
1724 th->notify_cls = notify_cls;
1725 if (GNUNET_YES == channel->allow_send)
1726 th->request_data_task = GNUNET_SCHEDULER_add_now (&request_data, th);
1728 add_to_queue (channel->cadet, th);
1730 LOG (GNUNET_ERROR_TYPE_DEBUG, "CADET NOTIFY TRANSMIT READY END\n");
1736 GNUNET_CADET_notify_transmit_ready_cancel (struct GNUNET_CADET_TransmitHandle *th)
1738 if (NULL != th->request_data_task)
1740 GNUNET_SCHEDULER_cancel (th->request_data_task);
1742 th->request_data_task = NULL;
1744 remove_from_queue (th);
1750 GNUNET_CADET_receive_done (struct GNUNET_CADET_Channel *channel)
1757 send_info_request (struct GNUNET_CADET_Handle *h, uint16_t type)
1759 struct GNUNET_MessageHeader *msg;
1760 struct GNUNET_MQ_Envelope *env;
1762 LOG (GNUNET_ERROR_TYPE_DEBUG,
1763 " Sending %s monitor message to service\n",
1766 env = GNUNET_MQ_msg (msg, type);
1767 GNUNET_MQ_send (h->mq, env);
1772 * Request a debug dump on the service's STDERR.
1774 * WARNING: unstable API, likely to change in the future!
1776 * @param h cadet handle
1779 GNUNET_CADET_request_dump (struct GNUNET_CADET_Handle *h)
1781 LOG (GNUNET_ERROR_TYPE_DEBUG, "requesting dump\n");
1782 send_info_request (h, GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_DUMP);
1787 * Request information about peers known to the running cadet service.
1788 * The callback will be called for every peer known to the service.
1789 * Only one info request (of any kind) can be active at once.
1792 * WARNING: unstable API, likely to change in the future!
1794 * @param h Handle to the cadet peer.
1795 * @param callback Function to call with the requested data.
1796 * @param callback_cls Closure for @c callback.
1798 * @return #GNUNET_OK / #GNUNET_SYSERR
1801 GNUNET_CADET_get_peers (struct GNUNET_CADET_Handle *h,
1802 GNUNET_CADET_PeersCB callback,
1805 if (NULL != h->info_cb.peers_cb)
1808 return GNUNET_SYSERR;
1810 send_info_request (h, GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEERS);
1811 h->info_cb.peers_cb = callback;
1812 h->info_cls = callback_cls;
1818 * Cancel a peer info request. The callback will not be called (anymore).
1820 * WARNING: unstable API, likely to change in the future!
1822 * @param h Cadet handle.
1824 * @return Closure given to GNUNET_CADET_get_peers.
1827 GNUNET_CADET_get_peers_cancel (struct GNUNET_CADET_Handle *h)
1832 h->info_cb.peers_cb = NULL;
1839 * Request information about a peer known to the running cadet peer.
1840 * The callback will be called for the tunnel once.
1841 * Only one info request (of any kind) can be active at once.
1843 * WARNING: unstable API, likely to change in the future!
1845 * @param h Handle to the cadet peer.
1846 * @param id Peer whose tunnel to examine.
1847 * @param callback Function to call with the requested data.
1848 * @param callback_cls Closure for @c callback.
1850 * @return #GNUNET_OK / #GNUNET_SYSERR
1853 GNUNET_CADET_get_peer (struct GNUNET_CADET_Handle *h,
1854 const struct GNUNET_PeerIdentity *id,
1855 GNUNET_CADET_PeerCB callback,
1858 struct GNUNET_CADET_LocalInfo *msg;
1859 struct GNUNET_MQ_Envelope *env;
1861 if (NULL != h->info_cb.peer_cb)
1864 return GNUNET_SYSERR;
1867 env = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEER);
1869 GNUNET_MQ_send (h->mq, env);
1871 h->info_cb.peer_cb = callback;
1872 h->info_cls = callback_cls;
1878 * Request information about tunnels of the running cadet peer.
1879 * The callback will be called for every tunnel of the service.
1880 * Only one info request (of any kind) can be active at once.
1882 * WARNING: unstable API, likely to change in the future!
1884 * @param h Handle to the cadet peer.
1885 * @param callback Function to call with the requested data.
1886 * @param callback_cls Closure for @c callback.
1888 * @return #GNUNET_OK / #GNUNET_SYSERR
1891 GNUNET_CADET_get_tunnels (struct GNUNET_CADET_Handle *h,
1892 GNUNET_CADET_TunnelsCB callback,
1895 if (NULL != h->info_cb.tunnels_cb)
1898 return GNUNET_SYSERR;
1900 send_info_request (h, GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNELS);
1901 h->info_cb.tunnels_cb = callback;
1902 h->info_cls = callback_cls;
1908 * Cancel a monitor request. The monitor callback will not be called.
1910 * @param h Cadet handle.
1912 * @return Closure given to GNUNET_CADET_get_tunnels.
1915 GNUNET_CADET_get_tunnels_cancel (struct GNUNET_CADET_Handle *h)
1919 h->info_cb.tunnels_cb = NULL;
1929 * Request information about a tunnel of the running cadet peer.
1930 * The callback will be called for the tunnel once.
1931 * Only one info request (of any kind) can be active at once.
1933 * WARNING: unstable API, likely to change in the future!
1935 * @param h Handle to the cadet peer.
1936 * @param id Peer whose tunnel to examine.
1937 * @param callback Function to call with the requested data.
1938 * @param callback_cls Closure for @c callback.
1940 * @return #GNUNET_OK / #GNUNET_SYSERR
1943 GNUNET_CADET_get_tunnel (struct GNUNET_CADET_Handle *h,
1944 const struct GNUNET_PeerIdentity *id,
1945 GNUNET_CADET_TunnelCB callback,
1948 struct GNUNET_CADET_LocalInfo *msg;
1949 struct GNUNET_MQ_Envelope *env;
1951 if (NULL != h->info_cb.tunnel_cb)
1954 return GNUNET_SYSERR;
1957 env = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNEL);
1959 GNUNET_MQ_send (h->mq, env);
1961 h->info_cb.tunnel_cb = callback;
1962 h->info_cls = callback_cls;
1968 * Request information about a specific channel of the running cadet peer.
1970 * WARNING: unstable API, likely to change in the future!
1971 * FIXME Add destination option.
1973 * @param h Handle to the cadet peer.
1974 * @param initiator ID of the owner of the channel.
1975 * @param channel_number Channel number.
1976 * @param callback Function to call with the requested data.
1977 * @param callback_cls Closure for @c callback.
1979 * @return #GNUNET_OK / #GNUNET_SYSERR
1982 GNUNET_CADET_show_channel (struct GNUNET_CADET_Handle *h,
1983 struct GNUNET_PeerIdentity *initiator,
1984 unsigned int channel_number,
1985 GNUNET_CADET_ChannelCB callback,
1988 struct GNUNET_CADET_LocalInfo *msg;
1989 struct GNUNET_MQ_Envelope *env;
1991 if (NULL != h->info_cb.channel_cb)
1994 return GNUNET_SYSERR;
1997 env = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_CHANNEL);
1998 msg->peer = *initiator;
1999 msg->channel_id.channel_of_client = htonl (channel_number);
2000 GNUNET_MQ_send (h->mq, env);
2002 h->info_cb.channel_cb = callback;
2003 h->info_cls = callback_cls;
2009 * Function called to notify a client about the connection
2010 * begin ready to queue more data. "buf" will be
2011 * NULL and "size" zero if the connection was closed for
2012 * writing in the meantime.
2014 * @param cls closure
2015 * @param size number of bytes available in buf
2016 * @param buf where the callee should write the message
2017 * @return number of bytes written to buf
2020 cadet_mq_ntr (void *cls, size_t size,
2023 struct GNUNET_MQ_Handle *mq = cls;
2024 struct CadetMQState *state = GNUNET_MQ_impl_state (mq);
2025 const struct GNUNET_MessageHeader *msg = GNUNET_MQ_impl_current (mq);
2031 GNUNET_MQ_inject_error (mq, GNUNET_MQ_ERROR_WRITE);
2034 msize = ntohs (msg->size);
2035 GNUNET_assert (msize <= size);
2036 GNUNET_memcpy (buf, msg, msize);
2037 GNUNET_MQ_impl_send_continue (mq);
2043 * Signature of functions implementing the
2044 * sending functionality of a message queue.
2046 * @param mq the message queue
2047 * @param msg the message to send
2048 * @param impl_state state of the implementation
2051 cadet_mq_send_impl (struct GNUNET_MQ_Handle *mq,
2052 const struct GNUNET_MessageHeader *msg,
2055 struct CadetMQState *state = impl_state;
2057 GNUNET_assert (NULL == state->th);
2059 GNUNET_CADET_notify_transmit_ready (state->channel,
2060 /* FIXME: add option for corking */
2062 GNUNET_TIME_UNIT_FOREVER_REL,
2070 * Signature of functions implementing the
2071 * destruction of a message queue.
2072 * Implementations must not free 'mq', but should
2073 * take care of 'impl_state'.
2075 * @param mq the message queue to destroy
2076 * @param impl_state state of the implementation
2079 cadet_mq_destroy_impl (struct GNUNET_MQ_Handle *mq,
2082 struct CadetMQState *state = impl_state;
2084 if (NULL != state->th)
2085 GNUNET_CADET_notify_transmit_ready_cancel (state->th);
2087 GNUNET_free (state);
2092 * Create a message queue for a cadet channel.
2093 * The message queue can only be used to transmit messages,
2094 * not to receive them.
2096 * @param channel the channel to create the message qeue for
2097 * @return a message queue to messages over the channel
2099 struct GNUNET_MQ_Handle *
2100 GNUNET_CADET_mq_create (struct GNUNET_CADET_Channel *channel)
2102 struct GNUNET_MQ_Handle *mq;
2103 struct CadetMQState *state;
2105 state = GNUNET_new (struct CadetMQState);
2106 state->channel = channel;
2108 mq = GNUNET_MQ_queue_for_callbacks (&cadet_mq_send_impl,
2109 &cadet_mq_destroy_impl,
2110 NULL, /* FIXME: cancel impl. */
2112 NULL, /* no msg handlers */
2113 NULL, /* no err handlers */
2114 NULL); /* no handler cls */
2120 * Transitional function to convert an unsigned int port to a hash value.
2121 * WARNING: local static value returned, NOT reentrant!
2122 * WARNING: do not use this function for new code!
2124 * @param port Numerical port (unsigned int format).
2126 * @return A GNUNET_HashCode usable for the new CADET API.
2128 const struct GNUNET_HashCode *
2129 GC_u2h (uint32_t port)
2131 static struct GNUNET_HashCode hash;
2133 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2134 "This is a transitional function, "
2135 "use proper crypto hashes as CADET ports\n");
2136 GNUNET_CRYPTO_hash (&port, sizeof (port), &hash);