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_ccn;
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 ccn;
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?
271 unsigned int allow_send;
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 ccn 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 ccn)
371 struct GNUNET_CADET_Channel *ch;
373 for (ch = h->channels_head; NULL != ch; ch = ch->next)
374 if (ch->ccn.channel_of_client == ccn.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 ccn Desired ccn 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 ccn)
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 == ccn.channel_of_client)
401 ch->ccn = h->next_ccn;
402 while (NULL != retrieve_channel (h,
405 h->next_ccn.channel_of_client
406 = htonl (1 + ntohl (h->next_ccn.channel_of_client));
407 if (0 == ntohl (h->next_ccn.channel_of_client))
408 h->next_ccn.channel_of_client
409 = htonl (GNUNET_CADET_LOCAL_CHANNEL_ID_CLI);
421 * Destroy the specified channel.
422 * - Destroys all peers, calling the disconnect callback on each if needed
423 * - Cancels all outgoing traffic for that channel, calling respective notifys
424 * - Calls cleaner if channel was inbound
425 * - Frees all memory used
427 * @param ch Pointer to the channel.
428 * @param call_cleaner Whether to call the cleaner handler.
430 * @return Handle to the required channel or NULL if not found.
432 // FIXME: simplify: call_cleaner is always #GNUNET_YES!!!
434 destroy_channel (struct GNUNET_CADET_Channel *ch,
437 struct GNUNET_CADET_Handle *h;
438 struct GNUNET_CADET_TransmitHandle *th;
439 struct GNUNET_CADET_TransmitHandle *next;
447 LOG (GNUNET_ERROR_TYPE_DEBUG,
448 " destroy_channel %X of %p\n",
452 GNUNET_CONTAINER_DLL_remove (h->channels_head,
456 /* signal channel destruction */
457 if ( (NULL != h->cleaner) &&
459 (GNUNET_YES == call_cleaner) )
461 LOG (GNUNET_ERROR_TYPE_DEBUG,
462 " calling cleaner\n");
463 h->cleaner (h->cls, ch, ch->ctx);
466 /* check that clients did not leave messages behind in the queue */
467 for (th = h->th_head; NULL != th; th = next)
470 if (th->channel != ch)
472 /* Clients should have aborted their requests already.
473 * Management traffic should be ok, as clients can't cancel that.
474 * If the service crashed and we are reconnecting, it's ok.
476 GNUNET_break (GNUNET_NO == th_is_payload (th));
477 GNUNET_CADET_notify_transmit_ready_cancel (th);
481 GNUNET_PEER_change_rc (ch->peer, -1);
488 * Add a transmit handle to the transmission queue and set the
491 * @param h cadet handle with the queue head and tail
492 * @param th handle to the packet to be transmitted
495 add_to_queue (struct GNUNET_CADET_Handle *h,
496 struct GNUNET_CADET_TransmitHandle *th)
498 GNUNET_CONTAINER_DLL_insert_tail (h->th_head, h->th_tail, th);
503 * Remove a transmit handle from the transmission queue, if present.
505 * Safe to call even if not queued.
507 * @param th handle to the packet to be unqueued.
510 remove_from_queue (struct GNUNET_CADET_TransmitHandle *th)
512 struct GNUNET_CADET_Handle *h = th->channel->cadet;
514 /* It might or might not have been queued (rarely not), but check anyway. */
515 if (NULL != th->next || h->th_tail == th)
517 GNUNET_CONTAINER_DLL_remove (h->th_head, h->th_tail, th);
523 * Send an ack on the channel to confirm the processing of a message.
525 * @param ch Channel on which to send the ACK.
528 send_ack (struct GNUNET_CADET_Channel *ch)
530 struct GNUNET_CADET_LocalAck *msg;
531 struct GNUNET_MQ_Envelope *env;
533 env = GNUNET_MQ_msg (msg,
534 GNUNET_MESSAGE_TYPE_CADET_LOCAL_ACK);
536 LOG (GNUNET_ERROR_TYPE_DEBUG,
537 "Sending ACK on channel %X\n",
538 ch->ccn.channel_of_client);
540 GNUNET_MQ_send (ch->cadet->mq,
546 /******************************************************************************/
547 /*********************** RECEIVE HANDLERS ****************************/
548 /******************************************************************************/
552 * Call the @a notify callback given to #GNUNET_CADET_notify_transmit_ready to
553 * request the data to send over MQ. Since MQ manages the queue, this function
554 * is scheduled immediatly after a transmit ready notification.
556 * @param cls Closure (transmit handle).
559 request_data (void *cls)
561 struct GNUNET_CADET_TransmitHandle *th = cls;
562 struct GNUNET_CADET_LocalData *msg;
563 struct GNUNET_MQ_Envelope *env;
566 LOG (GNUNET_ERROR_TYPE_DEBUG,
567 "Requesting Data: %u bytes\n",
570 GNUNET_assert (0 < th->channel->allow_send);
571 th->channel->allow_send--;
572 /* NOTE: we may be allowed to send another packet immediately,
573 albeit the current logic waits for the ACK. */
574 th->request_data_task = NULL;
575 th->channel->packet_size = 0;
576 remove_from_queue (th);
578 env = GNUNET_MQ_msg_extra (msg,
580 GNUNET_MESSAGE_TYPE_CADET_LOCAL_DATA);
581 msg->ccn = th->channel->ccn;
582 osize = th->notify (th->notify_cls,
585 GNUNET_assert (osize == th->size);
586 GNUNET_MQ_send (th->channel->cadet->mq,
593 * Process the new channel notification and add it to the channels in the handle
595 * @param h The cadet handle
596 * @param msg A message with the details of the new incoming channel
599 handle_channel_created (void *cls,
600 const struct GNUNET_CADET_LocalChannelCreateMessage *msg)
602 struct GNUNET_CADET_Handle *h = cls;
603 struct GNUNET_CADET_Channel *ch;
604 struct GNUNET_CADET_Port *port;
605 const struct GNUNET_HashCode *port_number;
606 struct GNUNET_CADET_ClientChannelNumber ccn;
609 port_number = &msg->port;
610 LOG (GNUNET_ERROR_TYPE_DEBUG,
611 "Creating incoming channel %X [%s]\n",
612 ntohl (ccn.channel_of_client),
613 GNUNET_h2s (port_number));
614 if (ntohl (ccn.channel_of_client) >= GNUNET_CADET_LOCAL_CHANNEL_ID_CLI)
619 port = find_port (h, port_number);
624 ch = create_channel (h, ccn);
625 ch->peer = GNUNET_PEER_intern (&msg->peer);
629 ch->options = ntohl (msg->opt);
631 LOG (GNUNET_ERROR_TYPE_DEBUG, " created channel %p\n", ch);
632 ctx = port->handler (port->cls, ch, &msg->peer, port->hash, ch->options);
635 LOG (GNUNET_ERROR_TYPE_DEBUG, "User notified\n");
639 struct GNUNET_CADET_LocalChannelDestroyMessage *d_msg;
640 struct GNUNET_MQ_Envelope *env;
642 LOG (GNUNET_ERROR_TYPE_DEBUG, "No handler for incoming channels\n");
643 env = GNUNET_MQ_msg (d_msg,
644 GNUNET_MESSAGE_TYPE_CADET_LOCAL_CHANNEL_DESTROY);
645 d_msg->ccn = msg->ccn;
646 GNUNET_MQ_send (h->mq, env);
653 * Process the channel destroy notification and free associated resources
655 * @param h The cadet handle
656 * @param msg A message with the details of the channel being destroyed
659 handle_channel_destroy (void *cls,
660 const struct GNUNET_CADET_LocalChannelDestroyMessage *msg)
662 struct GNUNET_CADET_Handle *h = cls;
663 struct GNUNET_CADET_Channel *ch;
664 struct GNUNET_CADET_ClientChannelNumber ccn;
667 LOG (GNUNET_ERROR_TYPE_DEBUG,
668 "Channel %X Destroy from service\n",
669 ntohl (ccn.channel_of_client));
670 ch = retrieve_channel (h,
675 LOG (GNUNET_ERROR_TYPE_DEBUG,
676 "channel %X unknown\n",
677 ntohl (ccn.channel_of_client));
686 * Check that message received from CADET service is well-formed.
688 * @param cls the `struct GNUNET_CADET_Handle`
689 * @param message the message we got
690 * @return #GNUNET_OK if the message is well-formed,
691 * #GNUNET_SYSERR otherwise
694 check_local_data (void *cls,
695 const struct GNUNET_CADET_LocalData *message)
697 struct GNUNET_CADET_Handle *h = cls;
698 struct GNUNET_CADET_Channel *ch;
701 size = ntohs (message->header.size);
702 if (sizeof (*message) + sizeof (struct GNUNET_MessageHeader) > size)
705 return GNUNET_SYSERR;
708 ch = retrieve_channel (h,
713 return GNUNET_SYSERR;
721 * Process the incoming data packets, call appropriate handlers.
723 * @param h The cadet handle
724 * @param message A message encapsulating the data
727 handle_local_data (void *cls,
728 const struct GNUNET_CADET_LocalData *message)
730 struct GNUNET_CADET_Handle *h = cls;
731 const struct GNUNET_MessageHeader *payload;
732 const struct GNUNET_CADET_MessageHandler *handler;
733 struct GNUNET_CADET_Channel *ch;
737 LOG (GNUNET_ERROR_TYPE_DEBUG,
738 "Got a data message!\n");
739 ch = retrieve_channel (h,
741 GNUNET_assert (NULL != ch);
743 payload = (struct GNUNET_MessageHeader *) &message[1];
744 LOG (GNUNET_ERROR_TYPE_DEBUG, " %s data on channel %s [%X]\n",
745 GC_f2s (ntohl (ch->ccn.channel_of_client) >=
746 GNUNET_CADET_LOCAL_CHANNEL_ID_CLI),
747 GNUNET_i2s (GNUNET_PEER_resolve2 (ch->peer)),
748 ntohl (message->ccn.channel_of_client));
750 type = ntohs (payload->type);
751 LOG (GNUNET_ERROR_TYPE_DEBUG, " payload type %s\n", GC_m2s (type));
752 for (i = 0; i < h->n_handlers; i++)
754 handler = &h->message_handlers[i];
755 LOG (GNUNET_ERROR_TYPE_DEBUG, " checking handler for type %u\n",
757 if (handler->type == type)
760 handler->callback (h->cls, ch, &ch->ctx, payload))
762 LOG (GNUNET_ERROR_TYPE_DEBUG, "callback caused disconnection\n");
763 GNUNET_CADET_channel_destroy (ch);
768 LOG (GNUNET_ERROR_TYPE_DEBUG,
769 "callback completed successfully\n");
778 * Process a local ACK message, enabling the client to send
779 * more data to the service.
781 * @param h Cadet handle.
782 * @param message Message itself.
785 handle_local_ack (void *cls,
786 const struct GNUNET_CADET_LocalAck *message)
788 struct GNUNET_CADET_Handle *h = cls;
789 struct GNUNET_CADET_Channel *ch;
790 struct GNUNET_CADET_ClientChannelNumber ccn;
793 ch = retrieve_channel (h, ccn);
796 LOG (GNUNET_ERROR_TYPE_DEBUG,
797 "ACK on unknown channel %X\n",
798 ntohl (ccn.channel_of_client));
801 LOG (GNUNET_ERROR_TYPE_DEBUG,
802 "Got an ACK on channel %X!\n",
803 ntohl (ch->ccn.channel_of_client));
805 if (0 < ch->packet_size)
807 struct GNUNET_CADET_TransmitHandle *th;
808 struct GNUNET_CADET_TransmitHandle *next;
810 LOG (GNUNET_ERROR_TYPE_DEBUG,
811 " pending data, sending %u bytes!\n",
813 for (th = h->th_head; NULL != th; th = next)
816 if ( (th->channel == ch) &&
817 (NULL == th->request_data_task) )
819 th->request_data_task
820 = GNUNET_SCHEDULER_add_now (&request_data,
825 /* Complain if we got thru all th without sending anything, ch was wrong */
826 GNUNET_break (NULL != th);
831 * Reconnect to the service, retransmit all infomation to try to restore the
834 * @param h handle to the cadet
836 * @return #GNUNET_YES in case of sucess, #GNUNET_NO otherwise (service down...)
839 reconnect (struct GNUNET_CADET_Handle *h);
843 * Reconnect callback: tries to reconnect again after a failer previous
846 * @param cls closure (cadet handle)
849 reconnect_cbk (void *cls);
853 * Generic error handler, called with the appropriate error code and
854 * the same closure specified at the creation of the message queue.
855 * Not every message queue implementation supports an error handler.
857 * @param cls closure, a `struct GNUNET_CORE_Handle *`
858 * @param error error code
861 handle_mq_error (void *cls,
862 enum GNUNET_MQ_Error error)
864 struct GNUNET_CADET_Handle *h = cls;
866 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MQ ERROR: %u\n", error);
867 GNUNET_MQ_destroy (h->mq);
874 * Process a local reply about info on all channels, pass info to the user.
876 * @param h Cadet handle.
877 * @param message Message itself.
880 // process_get_channels (struct GNUNET_CADET_Handle *h,
881 // const struct GNUNET_MessageHeader *message)
883 // struct GNUNET_CADET_LocalInfo *msg;
885 // GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Get Channels messasge received\n");
887 // if (NULL == h->channels_cb)
889 // GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " ignored\n");
893 // msg = (struct GNUNET_CADET_LocalInfo *) message;
894 // if (ntohs (message->size) !=
895 // (sizeof (struct GNUNET_CADET_LocalInfo) +
896 // sizeof (struct GNUNET_PeerIdentity)))
898 // GNUNET_break_op (0);
899 // GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
900 // "Get channels message: size %hu - expected %u\n",
901 // ntohs (message->size),
902 // sizeof (struct GNUNET_CADET_LocalInfo));
905 // h->channels_cb (h->channels_cls,
906 // ntohl (msg->channel_id),
908 // &msg->destination);
914 * Process a local monitor_channel reply, pass info to the user.
916 * @param h Cadet handle.
917 * @param message Message itself.
920 // process_show_channel (struct GNUNET_CADET_Handle *h,
921 // const struct GNUNET_MessageHeader *message)
923 // struct GNUNET_CADET_LocalInfo *msg;
926 // GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Show Channel messasge received\n");
928 // if (NULL == h->channel_cb)
930 // GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " ignored\n");
934 // /* Verify message sanity */
935 // msg = (struct GNUNET_CADET_LocalInfo *) message;
936 // esize = sizeof (struct GNUNET_CADET_LocalInfo);
937 // if (ntohs (message->size) != esize)
939 // GNUNET_break_op (0);
940 // GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
941 // "Show channel message: size %hu - expected %u\n",
942 // ntohs (message->size),
945 // h->channel_cb (h->channel_cls, NULL, NULL);
946 // h->channel_cb = NULL;
947 // h->channel_cls = NULL;
952 // h->channel_cb (h->channel_cls,
953 // &msg->destination,
960 * Check that message received from CADET service is well-formed.
962 * @param cls the `struct GNUNET_CADET_Handle`
963 * @param message the message we got
964 * @return #GNUNET_OK if the message is well-formed,
965 * #GNUNET_SYSERR otherwise
968 check_get_peers (void *cls,
969 const struct GNUNET_CADET_LocalInfoPeer *message)
971 struct GNUNET_CADET_Handle *h = cls;
974 if (NULL == h->info_cb.peers_cb)
976 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
977 " no handler for peesr monitor message!\n");
978 return GNUNET_SYSERR;
981 size = ntohs (message->header.size);
982 if (sizeof (struct GNUNET_CADET_LocalInfoPeer) > size)
984 h->info_cb.peers_cb (h->info_cls, NULL, -1, 0, 0);
985 h->info_cb.peers_cb = NULL;
987 return GNUNET_SYSERR;
995 * Process a local reply about info on all tunnels, pass info to the user.
997 * @param cls Closure (Cadet handle).
998 * @param msg Message itself.
1001 handle_get_peers (void *cls,
1002 const struct GNUNET_CADET_LocalInfoPeer *msg)
1004 struct GNUNET_CADET_Handle *h = cls;
1005 h->info_cb.peers_cb (h->info_cls, &msg->destination,
1006 (int) ntohs (msg->tunnel),
1007 (unsigned int ) ntohs (msg->paths),
1013 * Check that message received from CADET service is well-formed.
1015 * @param cls the `struct GNUNET_CADET_Handle`
1016 * @param message the message we got
1017 * @return #GNUNET_OK if the message is well-formed,
1018 * #GNUNET_SYSERR otherwise
1021 check_get_peer (void *cls,
1022 const struct GNUNET_CADET_LocalInfoPeer *message)
1024 struct GNUNET_CADET_Handle *h = cls;
1025 const size_t msize = sizeof (struct GNUNET_CADET_LocalInfoPeer);
1026 struct GNUNET_PeerIdentity *paths_array;
1028 unsigned int epaths;
1032 if (NULL == h->info_cb.peer_cb)
1034 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1035 " no handler for peer monitor message!\n");
1039 /* Verify message sanity */
1040 esize = ntohs (message->header.size);
1043 GNUNET_break_op (0);
1044 h->info_cb.peer_cb (h->info_cls, NULL, 0, 0, 0, NULL);
1047 if (0 != ((esize - msize) % sizeof (struct GNUNET_PeerIdentity)))
1049 GNUNET_break_op (0);
1050 h->info_cb.peer_cb (h->info_cls, NULL, 0, 0, 0, NULL);
1054 peers = (esize - msize) / sizeof (struct GNUNET_PeerIdentity);
1055 epaths = (unsigned int) ntohs (message->paths);
1056 paths_array = (struct GNUNET_PeerIdentity *) &message[1];
1058 for (int i = 0; i < peers; i++)
1060 if (0 == memcmp (&paths_array[i], &message->destination,
1061 sizeof (struct GNUNET_PeerIdentity)))
1066 if (paths != epaths)
1068 GNUNET_break_op (0);
1069 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "p:%u, e: %u\n", paths, epaths);
1070 h->info_cb.peer_cb (h->info_cls, NULL, 0, 0, 0, NULL);
1077 h->info_cb.peer_cb = NULL;
1079 return GNUNET_SYSERR;
1084 * Process a local peer info reply, pass info to the user.
1086 * @param cls Closure (Cadet handle).
1087 * @param message Message itself.
1090 handle_get_peer (void *cls,
1091 const struct GNUNET_CADET_LocalInfoPeer *message)
1093 struct GNUNET_CADET_Handle *h = cls;
1094 struct GNUNET_PeerIdentity *paths_array;
1096 unsigned int path_length;
1100 paths = (unsigned int) ntohs (message->paths);
1101 paths_array = (struct GNUNET_PeerIdentity *) &message[1];
1102 peers = (ntohs (message->header.size) - sizeof (*message))
1103 / sizeof (struct GNUNET_PeerIdentity);
1105 neighbor = GNUNET_NO;
1107 for (int i = 0; i < peers; i++)
1109 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " %s\n", GNUNET_i2s (&paths_array[i]));
1111 if (0 == memcmp (&paths_array[i], &message->destination,
1112 sizeof (struct GNUNET_PeerIdentity)))
1114 if (1 == path_length)
1115 neighbor = GNUNET_YES;
1120 /* Call Callback with tunnel info. */
1121 paths_array = (struct GNUNET_PeerIdentity *) &message[1];
1122 h->info_cb.peer_cb (h->info_cls,
1123 &message->destination,
1124 (int) ntohs (message->tunnel),
1132 * Check that message received from CADET service is well-formed.
1134 * @param cls the `struct GNUNET_CADET_Handle`
1135 * @param msg the message we got
1136 * @return #GNUNET_OK if the message is well-formed,
1137 * #GNUNET_SYSERR otherwise
1140 check_get_tunnels (void *cls,
1141 const struct GNUNET_CADET_LocalInfoTunnel *msg)
1143 struct GNUNET_CADET_Handle *h = cls;
1146 if (NULL == h->info_cb.tunnels_cb)
1148 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1149 " no handler for tunnels monitor message!\n");
1150 return GNUNET_SYSERR;
1153 size = ntohs (msg->header.size);
1154 if (sizeof (struct GNUNET_CADET_LocalInfoTunnel) > size)
1156 h->info_cb.tunnels_cb (h->info_cls, NULL, 0, 0, 0, 0);
1157 h->info_cb.tunnels_cb = NULL;
1159 return GNUNET_SYSERR;
1166 * Process a local reply about info on all tunnels, pass info to the user.
1168 * @param cls Closure (Cadet handle).
1169 * @param message Message itself.
1172 handle_get_tunnels (void *cls,
1173 const struct GNUNET_CADET_LocalInfoTunnel *msg)
1175 struct GNUNET_CADET_Handle *h = cls;
1177 h->info_cb.tunnels_cb (h->info_cls,
1179 ntohl (msg->channels),
1180 ntohl (msg->connections),
1181 ntohs (msg->estate),
1182 ntohs (msg->cstate));
1188 * Check that message received from CADET service is well-formed.
1190 * @param cls the `struct GNUNET_CADET_Handle`
1191 * @param msg the message we got
1192 * @return #GNUNET_OK if the message is well-formed,
1193 * #GNUNET_SYSERR otherwise
1196 check_get_tunnel (void *cls,
1197 const struct GNUNET_CADET_LocalInfoTunnel *msg)
1199 struct GNUNET_CADET_Handle *h = cls;
1205 if (NULL == h->info_cb.tunnel_cb)
1207 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1208 " no handler for tunnel monitor message!\n");
1212 /* Verify message sanity */
1213 msize = ntohs (msg->header.size);
1214 esize = sizeof (struct GNUNET_CADET_LocalInfoTunnel);
1217 GNUNET_break_op (0);
1218 h->info_cb.tunnel_cb (h->info_cls,
1219 NULL, 0, 0, NULL, NULL, 0, 0);
1222 ch_n = ntohl (msg->channels);
1223 c_n = ntohl (msg->connections);
1224 esize += ch_n * sizeof (struct GNUNET_CADET_ChannelTunnelNumber);
1225 esize += c_n * sizeof (struct GNUNET_CADET_ConnectionTunnelIdentifier);
1228 GNUNET_break_op (0);
1229 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1230 "m:%u, e: %u (%u ch, %u conn)\n",
1231 (unsigned int) msize,
1232 (unsigned int) esize,
1235 h->info_cb.tunnel_cb (h->info_cls,
1236 NULL, 0, 0, NULL, NULL, 0, 0);
1243 h->info_cb.tunnel_cb = NULL;
1245 return GNUNET_SYSERR;
1250 * Process a local tunnel info reply, pass info to the user.
1252 * @param cls Closure (Cadet handle).
1253 * @param msg Message itself.
1256 handle_get_tunnel (void *cls,
1257 const struct GNUNET_CADET_LocalInfoTunnel *msg)
1259 struct GNUNET_CADET_Handle *h = cls;
1262 const struct GNUNET_CADET_ConnectionTunnelIdentifier *conns;
1263 const struct GNUNET_CADET_ChannelTunnelNumber *chns;
1265 ch_n = ntohl (msg->channels);
1266 c_n = ntohl (msg->connections);
1268 /* Call Callback with tunnel info. */
1269 conns = (const struct GNUNET_CADET_ConnectionTunnelIdentifier *) &msg[1];
1270 chns = (const struct GNUNET_CADET_ChannelTunnelNumber *) &conns[c_n];
1271 h->info_cb.tunnel_cb (h->info_cls,
1277 ntohs (msg->estate),
1278 ntohs (msg->cstate));
1284 * Reconnect to the service, retransmit all infomation to try to restore the
1287 * @param h handle to the cadet
1289 * @return GNUNET_YES in case of sucess, GNUNET_NO otherwise (service down...)
1292 do_reconnect (struct GNUNET_CADET_Handle *h)
1294 struct GNUNET_MQ_MessageHandler handlers[] = {
1295 GNUNET_MQ_hd_fixed_size (channel_created,
1296 GNUNET_MESSAGE_TYPE_CADET_LOCAL_CHANNEL_CREATE,
1297 struct GNUNET_CADET_LocalChannelCreateMessage,
1299 GNUNET_MQ_hd_fixed_size (channel_destroy,
1300 GNUNET_MESSAGE_TYPE_CADET_LOCAL_CHANNEL_DESTROY,
1301 struct GNUNET_CADET_LocalChannelDestroyMessage,
1303 GNUNET_MQ_hd_var_size (local_data,
1304 GNUNET_MESSAGE_TYPE_CADET_LOCAL_DATA,
1305 struct GNUNET_CADET_LocalData,
1307 GNUNET_MQ_hd_fixed_size (local_ack,
1308 GNUNET_MESSAGE_TYPE_CADET_LOCAL_ACK,
1309 struct GNUNET_CADET_LocalAck,
1311 GNUNET_MQ_hd_var_size (get_peers,
1312 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEERS,
1313 struct GNUNET_CADET_LocalInfoPeer,
1315 GNUNET_MQ_hd_var_size (get_peer,
1316 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEER,
1317 struct GNUNET_CADET_LocalInfoPeer,
1319 GNUNET_MQ_hd_var_size (get_tunnels,
1320 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNELS,
1321 struct GNUNET_CADET_LocalInfoTunnel,
1323 GNUNET_MQ_hd_var_size (get_tunnel,
1324 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNEL,
1325 struct GNUNET_CADET_LocalInfoTunnel,
1328 // GNUNET_MQ_hd_fixed_Y size (channel_destroyed,
1329 // GNUNET_MESSAGE_TYPE_CADET_CHANNEL_OPEN_NACK_DEPRECATED,
1330 // struct GNUNET_CADET_ChannelDestroyMessage);
1331 GNUNET_MQ_handler_end ()
1334 LOG (GNUNET_ERROR_TYPE_DEBUG, "Connecting to CADET\n");
1336 GNUNET_assert (NULL == h->mq);
1337 h->mq = GNUNET_CLIENT_connect (h->cfg,
1349 h->reconnect_time = GNUNET_TIME_UNIT_MILLISECONDS;
1355 * Reconnect callback: tries to reconnect again after a failer previous
1358 * @param cls closure (cadet handle)
1361 reconnect_cbk (void *cls)
1363 struct GNUNET_CADET_Handle *h = cls;
1365 h->reconnect_task = NULL;
1371 * Reconnect to the service, retransmit all infomation to try to restore the
1374 * @param h handle to the cadet
1376 * @return #GNUNET_YES in case of sucess, #GNUNET_NO otherwise (service down...)
1379 reconnect (struct GNUNET_CADET_Handle *h)
1381 struct GNUNET_CADET_Channel *ch;
1383 LOG (GNUNET_ERROR_TYPE_DEBUG,
1384 "Requested RECONNECT, destroying all channels\n");
1385 for (ch = h->channels_head; NULL != ch; ch = h->channels_head)
1386 destroy_channel (ch, GNUNET_YES);
1387 if (NULL == h->reconnect_task)
1388 h->reconnect_task = GNUNET_SCHEDULER_add_delayed (h->reconnect_time,
1393 /******************************************************************************/
1394 /********************** API CALL DEFINITIONS *************************/
1395 /******************************************************************************/
1397 struct GNUNET_CADET_Handle *
1398 GNUNET_CADET_connect (const struct GNUNET_CONFIGURATION_Handle *cfg,
1400 GNUNET_CADET_ChannelEndHandler cleaner,
1401 const struct GNUNET_CADET_MessageHandler *handlers)
1403 struct GNUNET_CADET_Handle *h;
1405 LOG (GNUNET_ERROR_TYPE_DEBUG, "GNUNET_CADET_connect()\n");
1406 h = GNUNET_new (struct GNUNET_CADET_Handle);
1407 LOG (GNUNET_ERROR_TYPE_DEBUG, " addr %p\n", h);
1409 h->cleaner = cleaner;
1410 h->ports = GNUNET_CONTAINER_multihashmap_create (4, GNUNET_YES);
1415 GNUNET_CADET_disconnect (h);
1419 h->message_handlers = handlers;
1420 h->next_ccn.channel_of_client = htonl (GNUNET_CADET_LOCAL_CHANNEL_ID_CLI);
1421 h->reconnect_time = GNUNET_TIME_UNIT_MILLISECONDS;
1422 h->reconnect_task = NULL;
1424 /* count handlers */
1425 for (h->n_handlers = 0;
1426 handlers && handlers[h->n_handlers].type;
1428 LOG (GNUNET_ERROR_TYPE_DEBUG, "GNUNET_CADET_connect() END\n");
1434 * Disconnect from the cadet service. All channels will be destroyed. All channel
1435 * disconnect callbacks will be called on any still connected peers, notifying
1436 * about their disconnection. The registered inbound channel cleaner will be
1437 * called should any inbound channels still exist.
1439 * @param handle connection to cadet to disconnect
1442 GNUNET_CADET_disconnect (struct GNUNET_CADET_Handle *handle)
1444 struct GNUNET_CADET_Channel *ch;
1445 struct GNUNET_CADET_Channel *aux;
1446 struct GNUNET_CADET_TransmitHandle *th;
1448 LOG (GNUNET_ERROR_TYPE_DEBUG,
1449 "CADET DISCONNECT\n");
1450 ch = handle->channels_head;
1454 if (ntohl (ch->ccn.channel_of_client) >= GNUNET_CADET_LOCAL_CHANNEL_ID_CLI)
1457 LOG (GNUNET_ERROR_TYPE_DEBUG,
1458 "channel %X not destroyed\n",
1459 ntohl (ch->ccn.channel_of_client));
1461 destroy_channel (ch,
1465 while (NULL != (th = handle->th_head))
1467 struct GNUNET_MessageHeader *msg;
1469 /* Make sure it is an allowed packet (everything else should have been
1470 * already canceled).
1472 GNUNET_break (GNUNET_NO == th_is_payload (th));
1473 msg = (struct GNUNET_MessageHeader *) &th[1];
1474 switch (ntohs(msg->type))
1476 case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_OPEN:
1477 case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DESTROY:
1478 case GNUNET_MESSAGE_TYPE_CADET_LOCAL_PORT_OPEN:
1479 case GNUNET_MESSAGE_TYPE_CADET_LOCAL_PORT_CLOSE:
1480 case GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_CHANNELS:
1481 case GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_CHANNEL:
1482 case GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEER:
1483 case GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEERS:
1484 case GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNEL:
1485 case GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNELS:
1489 LOG (GNUNET_ERROR_TYPE_ERROR, "unexpected unsent msg %s\n",
1490 GC_m2s (ntohs(msg->type)));
1493 GNUNET_CADET_notify_transmit_ready_cancel (th);
1496 if (NULL != handle->mq)
1498 GNUNET_MQ_destroy (handle->mq);
1501 if (NULL != handle->reconnect_task)
1503 GNUNET_SCHEDULER_cancel(handle->reconnect_task);
1504 handle->reconnect_task = NULL;
1507 GNUNET_CONTAINER_multihashmap_destroy (handle->ports);
1508 handle->ports = NULL;
1509 GNUNET_free (handle);
1514 * Open a port to receive incomming channels.
1516 * @param h CADET handle.
1517 * @param port Hash representing the port number.
1518 * @param new_channel Function called when an channel is received.
1519 * @param new_channel_cls Closure for @a new_channel.
1521 * @return Port handle.
1523 struct GNUNET_CADET_Port *
1524 GNUNET_CADET_open_port (struct GNUNET_CADET_Handle *h,
1525 const struct GNUNET_HashCode *port,
1526 GNUNET_CADET_InboundChannelNotificationHandler
1528 void *new_channel_cls)
1530 struct GNUNET_CADET_PortMessage *msg;
1531 struct GNUNET_MQ_Envelope *env;
1532 struct GNUNET_CADET_Port *p;
1534 GNUNET_assert (NULL != new_channel);
1535 p = GNUNET_new (struct GNUNET_CADET_Port);
1537 p->hash = GNUNET_new (struct GNUNET_HashCode);
1539 p->handler = new_channel;
1540 p->cls = new_channel_cls;
1541 GNUNET_assert (GNUNET_OK ==
1542 GNUNET_CONTAINER_multihashmap_put (h->ports,
1545 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1547 env = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_CADET_LOCAL_PORT_OPEN);
1548 msg->port = *p->hash;
1549 GNUNET_MQ_send (h->mq, env);
1555 * Close a port opened with @a GNUNET_CADET_open_port.
1556 * The @a new_channel callback will no longer be called.
1558 * @param p Port handle.
1561 GNUNET_CADET_close_port (struct GNUNET_CADET_Port *p)
1563 struct GNUNET_CADET_PortMessage *msg;
1564 struct GNUNET_MQ_Envelope *env;
1566 env = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_CADET_LOCAL_PORT_CLOSE);
1568 msg->port = *p->hash;
1569 GNUNET_MQ_send (p->cadet->mq, env);
1570 GNUNET_CONTAINER_multihashmap_remove (p->cadet->ports, p->hash, p);
1571 GNUNET_free (p->hash);
1577 * Create a new channel towards a remote peer.
1579 * If the destination port is not open by any peer or the destination peer
1580 * does not accept the channel, #GNUNET_CADET_ChannelEndHandler will be called
1583 * @param h cadet handle
1584 * @param channel_ctx client's channel context to associate with the channel
1585 * @param peer peer identity the channel should go to
1586 * @param port Port hash (port number).
1587 * @param options CadetOption flag field, with all desired option bits set to 1.
1588 * @return handle to the channel
1590 struct GNUNET_CADET_Channel *
1591 GNUNET_CADET_channel_create (struct GNUNET_CADET_Handle *h,
1593 const struct GNUNET_PeerIdentity *peer,
1594 const struct GNUNET_HashCode *port,
1595 enum GNUNET_CADET_ChannelOption options)
1597 struct GNUNET_CADET_LocalChannelCreateMessage *msg;
1598 struct GNUNET_MQ_Envelope *env;
1599 struct GNUNET_CADET_Channel *ch;
1600 struct GNUNET_CADET_ClientChannelNumber ccn;
1602 LOG (GNUNET_ERROR_TYPE_DEBUG,
1603 "Creating new channel to %s:%u\n",
1604 GNUNET_i2s (peer), port);
1605 ccn.channel_of_client = htonl (0);
1606 ch = create_channel (h, ccn);
1607 LOG (GNUNET_ERROR_TYPE_DEBUG, " at %p\n", ch);
1608 LOG (GNUNET_ERROR_TYPE_DEBUG, " number %X\n",
1609 ntohl (ch->ccn.channel_of_client));
1610 ch->ctx = channel_ctx;
1611 ch->peer = GNUNET_PEER_intern (peer);
1613 env = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_CADET_LOCAL_CHANNEL_CREATE);
1617 msg->opt = htonl (options);
1618 GNUNET_MQ_send (h->mq,
1625 GNUNET_CADET_channel_destroy (struct GNUNET_CADET_Channel *channel)
1627 struct GNUNET_CADET_Handle *h;
1628 struct GNUNET_CADET_LocalChannelDestroyMessage *msg;
1629 struct GNUNET_MQ_Envelope *env;
1630 struct GNUNET_CADET_TransmitHandle *th;
1631 struct GNUNET_CADET_TransmitHandle *next;
1633 LOG (GNUNET_ERROR_TYPE_DEBUG, "Destroying channel\n");
1635 for (th = h->th_head; th != NULL; th = next)
1638 if (th->channel == channel)
1641 if (GNUNET_YES == th_is_payload (th))
1643 /* applications should cancel before destroying channel */
1644 LOG (GNUNET_ERROR_TYPE_WARNING,
1645 "Channel destroyed without cancelling transmission requests\n");
1646 th->notify (th->notify_cls, 0, NULL);
1650 LOG (GNUNET_ERROR_TYPE_WARNING, "no meta-traffic should be queued\n");
1652 GNUNET_CONTAINER_DLL_remove (h->th_head,
1655 GNUNET_CADET_notify_transmit_ready_cancel (th);
1659 env = GNUNET_MQ_msg (msg,
1660 GNUNET_MESSAGE_TYPE_CADET_LOCAL_CHANNEL_DESTROY);
1661 msg->ccn = channel->ccn;
1662 GNUNET_MQ_send (h->mq,
1665 destroy_channel (channel,
1671 * Get information about a channel.
1673 * @param channel Channel handle.
1674 * @param option Query (GNUNET_CADET_OPTION_*).
1675 * @param ... dependant on option, currently not used
1677 * @return Union with an answer to the query.
1679 const union GNUNET_CADET_ChannelInfo *
1680 GNUNET_CADET_channel_get_info (struct GNUNET_CADET_Channel *channel,
1681 enum GNUNET_CADET_ChannelOption option, ...)
1683 static int bool_flag;
1684 const union GNUNET_CADET_ChannelInfo *ret;
1688 case GNUNET_CADET_OPTION_NOBUFFER:
1689 case GNUNET_CADET_OPTION_RELIABLE:
1690 case GNUNET_CADET_OPTION_OUT_OF_ORDER:
1691 if (0 != (option & channel->options))
1692 bool_flag = GNUNET_YES;
1694 bool_flag = GNUNET_NO;
1695 ret = (const union GNUNET_CADET_ChannelInfo *) &bool_flag;
1697 case GNUNET_CADET_OPTION_PEER:
1698 ret = (const union GNUNET_CADET_ChannelInfo *) GNUNET_PEER_resolve2 (channel->peer);
1709 struct GNUNET_CADET_TransmitHandle *
1710 GNUNET_CADET_notify_transmit_ready (struct GNUNET_CADET_Channel *channel,
1712 struct GNUNET_TIME_Relative maxdelay,
1714 GNUNET_CONNECTION_TransmitReadyNotify notify,
1717 struct GNUNET_CADET_TransmitHandle *th;
1719 GNUNET_assert (NULL != channel);
1720 GNUNET_assert (GNUNET_CONSTANTS_MAX_CADET_MESSAGE_SIZE >= notify_size);
1721 LOG (GNUNET_ERROR_TYPE_DEBUG, "CADET NOTIFY TRANSMIT READY\n");
1722 LOG (GNUNET_ERROR_TYPE_DEBUG, " on channel %X\n", channel->ccn);
1723 LOG (GNUNET_ERROR_TYPE_DEBUG, " allow_send %d\n", channel->allow_send);
1724 if (ntohl (channel->ccn.channel_of_client) >=
1725 GNUNET_CADET_LOCAL_CHANNEL_ID_CLI)
1726 LOG (GNUNET_ERROR_TYPE_DEBUG, " to origin\n");
1728 LOG (GNUNET_ERROR_TYPE_DEBUG, " to destination\n");
1729 LOG (GNUNET_ERROR_TYPE_DEBUG, " payload size %u\n", notify_size);
1730 GNUNET_assert (NULL != notify);
1731 GNUNET_assert (0 == channel->packet_size); // Only one data packet allowed
1733 if (GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us != maxdelay.rel_value_us)
1735 LOG (GNUNET_ERROR_TYPE_WARNING,
1736 "CADET transmit ready timeout is deprected (has no effect)\n");
1739 th = GNUNET_new (struct GNUNET_CADET_TransmitHandle);
1740 th->channel = channel;
1741 th->size = notify_size;
1742 channel->packet_size = th->size;
1743 LOG (GNUNET_ERROR_TYPE_DEBUG, " total size %u\n", th->size);
1744 th->notify = notify;
1745 th->notify_cls = notify_cls;
1746 if (0 != channel->allow_send)
1747 th->request_data_task = GNUNET_SCHEDULER_add_now (&request_data, th);
1749 add_to_queue (channel->cadet, th);
1751 LOG (GNUNET_ERROR_TYPE_DEBUG, "CADET NOTIFY TRANSMIT READY END\n");
1757 GNUNET_CADET_notify_transmit_ready_cancel (struct GNUNET_CADET_TransmitHandle *th)
1759 if (NULL != th->request_data_task)
1761 GNUNET_SCHEDULER_cancel (th->request_data_task);
1763 th->request_data_task = NULL;
1765 remove_from_queue (th);
1771 GNUNET_CADET_receive_done (struct GNUNET_CADET_Channel *channel)
1778 send_info_request (struct GNUNET_CADET_Handle *h, uint16_t type)
1780 struct GNUNET_MessageHeader *msg;
1781 struct GNUNET_MQ_Envelope *env;
1783 LOG (GNUNET_ERROR_TYPE_DEBUG,
1784 " Sending %s monitor message to service\n",
1787 env = GNUNET_MQ_msg (msg, type);
1788 GNUNET_MQ_send (h->mq, env);
1793 * Request a debug dump on the service's STDERR.
1795 * WARNING: unstable API, likely to change in the future!
1797 * @param h cadet handle
1800 GNUNET_CADET_request_dump (struct GNUNET_CADET_Handle *h)
1802 LOG (GNUNET_ERROR_TYPE_DEBUG, "requesting dump\n");
1803 send_info_request (h, GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_DUMP);
1808 * Request information about peers known to the running cadet service.
1809 * The callback will be called for every peer known to the service.
1810 * Only one info request (of any kind) can be active at once.
1813 * WARNING: unstable API, likely to change in the future!
1815 * @param h Handle to the cadet peer.
1816 * @param callback Function to call with the requested data.
1817 * @param callback_cls Closure for @c callback.
1819 * @return #GNUNET_OK / #GNUNET_SYSERR
1822 GNUNET_CADET_get_peers (struct GNUNET_CADET_Handle *h,
1823 GNUNET_CADET_PeersCB callback,
1826 if (NULL != h->info_cb.peers_cb)
1829 return GNUNET_SYSERR;
1831 send_info_request (h, GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEERS);
1832 h->info_cb.peers_cb = callback;
1833 h->info_cls = callback_cls;
1839 * Cancel a peer info request. The callback will not be called (anymore).
1841 * WARNING: unstable API, likely to change in the future!
1843 * @param h Cadet handle.
1845 * @return Closure given to GNUNET_CADET_get_peers.
1848 GNUNET_CADET_get_peers_cancel (struct GNUNET_CADET_Handle *h)
1853 h->info_cb.peers_cb = NULL;
1860 * Request information about a peer known to the running cadet peer.
1861 * The callback will be called for the tunnel once.
1862 * Only one info request (of any kind) can be active at once.
1864 * WARNING: unstable API, likely to change in the future!
1866 * @param h Handle to the cadet peer.
1867 * @param id Peer whose tunnel to examine.
1868 * @param callback Function to call with the requested data.
1869 * @param callback_cls Closure for @c callback.
1871 * @return #GNUNET_OK / #GNUNET_SYSERR
1874 GNUNET_CADET_get_peer (struct GNUNET_CADET_Handle *h,
1875 const struct GNUNET_PeerIdentity *id,
1876 GNUNET_CADET_PeerCB callback,
1879 struct GNUNET_CADET_LocalInfo *msg;
1880 struct GNUNET_MQ_Envelope *env;
1882 if (NULL != h->info_cb.peer_cb)
1885 return GNUNET_SYSERR;
1888 env = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEER);
1890 GNUNET_MQ_send (h->mq, env);
1892 h->info_cb.peer_cb = callback;
1893 h->info_cls = callback_cls;
1899 * Request information about tunnels of the running cadet peer.
1900 * The callback will be called for every tunnel of the service.
1901 * Only one info request (of any kind) can be active at once.
1903 * WARNING: unstable API, likely to change in the future!
1905 * @param h Handle to the cadet peer.
1906 * @param callback Function to call with the requested data.
1907 * @param callback_cls Closure for @c callback.
1909 * @return #GNUNET_OK / #GNUNET_SYSERR
1912 GNUNET_CADET_get_tunnels (struct GNUNET_CADET_Handle *h,
1913 GNUNET_CADET_TunnelsCB callback,
1916 if (NULL != h->info_cb.tunnels_cb)
1919 return GNUNET_SYSERR;
1921 send_info_request (h, GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNELS);
1922 h->info_cb.tunnels_cb = callback;
1923 h->info_cls = callback_cls;
1929 * Cancel a monitor request. The monitor callback will not be called.
1931 * @param h Cadet handle.
1933 * @return Closure given to GNUNET_CADET_get_tunnels.
1936 GNUNET_CADET_get_tunnels_cancel (struct GNUNET_CADET_Handle *h)
1940 h->info_cb.tunnels_cb = NULL;
1950 * Request information about a tunnel of the running cadet peer.
1951 * The callback will be called for the tunnel once.
1952 * Only one info request (of any kind) can be active at once.
1954 * WARNING: unstable API, likely to change in the future!
1956 * @param h Handle to the cadet peer.
1957 * @param id Peer whose tunnel to examine.
1958 * @param callback Function to call with the requested data.
1959 * @param callback_cls Closure for @c callback.
1961 * @return #GNUNET_OK / #GNUNET_SYSERR
1964 GNUNET_CADET_get_tunnel (struct GNUNET_CADET_Handle *h,
1965 const struct GNUNET_PeerIdentity *id,
1966 GNUNET_CADET_TunnelCB callback,
1969 struct GNUNET_CADET_LocalInfo *msg;
1970 struct GNUNET_MQ_Envelope *env;
1972 if (NULL != h->info_cb.tunnel_cb)
1975 return GNUNET_SYSERR;
1978 env = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNEL);
1980 GNUNET_MQ_send (h->mq, env);
1982 h->info_cb.tunnel_cb = callback;
1983 h->info_cls = callback_cls;
1989 * Request information about a specific channel of the running cadet peer.
1991 * WARNING: unstable API, likely to change in the future!
1992 * FIXME Add destination option.
1994 * @param h Handle to the cadet peer.
1995 * @param initiator ID of the owner of the channel.
1996 * @param channel_number Channel number.
1997 * @param callback Function to call with the requested data.
1998 * @param callback_cls Closure for @c callback.
2000 * @return #GNUNET_OK / #GNUNET_SYSERR
2003 GNUNET_CADET_show_channel (struct GNUNET_CADET_Handle *h,
2004 struct GNUNET_PeerIdentity *initiator,
2005 unsigned int channel_number,
2006 GNUNET_CADET_ChannelCB callback,
2009 struct GNUNET_CADET_LocalInfo *msg;
2010 struct GNUNET_MQ_Envelope *env;
2012 if (NULL != h->info_cb.channel_cb)
2015 return GNUNET_SYSERR;
2018 env = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_CHANNEL);
2019 msg->peer = *initiator;
2020 msg->ccn.channel_of_client = htonl (channel_number);
2021 GNUNET_MQ_send (h->mq, env);
2023 h->info_cb.channel_cb = callback;
2024 h->info_cls = callback_cls;
2030 * Function called to notify a client about the connection
2031 * begin ready to queue more data. "buf" will be
2032 * NULL and "size" zero if the connection was closed for
2033 * writing in the meantime.
2035 * @param cls closure
2036 * @param size number of bytes available in buf
2037 * @param buf where the callee should write the message
2038 * @return number of bytes written to buf
2041 cadet_mq_ntr (void *cls, size_t size,
2044 struct GNUNET_MQ_Handle *mq = cls;
2045 struct CadetMQState *state = GNUNET_MQ_impl_state (mq);
2046 const struct GNUNET_MessageHeader *msg = GNUNET_MQ_impl_current (mq);
2052 GNUNET_MQ_inject_error (mq, GNUNET_MQ_ERROR_WRITE);
2055 msize = ntohs (msg->size);
2056 GNUNET_assert (msize <= size);
2057 GNUNET_memcpy (buf, msg, msize);
2058 GNUNET_MQ_impl_send_continue (mq);
2064 * Signature of functions implementing the
2065 * sending functionality of a message queue.
2067 * @param mq the message queue
2068 * @param msg the message to send
2069 * @param impl_state state of the implementation
2072 cadet_mq_send_impl (struct GNUNET_MQ_Handle *mq,
2073 const struct GNUNET_MessageHeader *msg,
2076 struct CadetMQState *state = impl_state;
2078 GNUNET_assert (NULL == state->th);
2080 GNUNET_CADET_notify_transmit_ready (state->channel,
2081 /* FIXME: add option for corking */
2083 GNUNET_TIME_UNIT_FOREVER_REL,
2091 * Signature of functions implementing the
2092 * destruction of a message queue.
2093 * Implementations must not free 'mq', but should
2094 * take care of 'impl_state'.
2096 * @param mq the message queue to destroy
2097 * @param impl_state state of the implementation
2100 cadet_mq_destroy_impl (struct GNUNET_MQ_Handle *mq,
2103 struct CadetMQState *state = impl_state;
2105 if (NULL != state->th)
2106 GNUNET_CADET_notify_transmit_ready_cancel (state->th);
2108 GNUNET_free (state);
2113 * Create a message queue for a cadet channel.
2114 * The message queue can only be used to transmit messages,
2115 * not to receive them.
2117 * @param channel the channel to create the message qeue for
2118 * @return a message queue to messages over the channel
2120 struct GNUNET_MQ_Handle *
2121 GNUNET_CADET_mq_create (struct GNUNET_CADET_Channel *channel)
2123 struct GNUNET_MQ_Handle *mq;
2124 struct CadetMQState *state;
2126 state = GNUNET_new (struct CadetMQState);
2127 state->channel = channel;
2129 mq = GNUNET_MQ_queue_for_callbacks (&cadet_mq_send_impl,
2130 &cadet_mq_destroy_impl,
2131 NULL, /* FIXME: cancel impl. */
2133 NULL, /* no msg handlers */
2134 NULL, /* no err handlers */
2135 NULL); /* no handler cls */
2141 * Transitional function to convert an unsigned int port to a hash value.
2142 * WARNING: local static value returned, NOT reentrant!
2143 * WARNING: do not use this function for new code!
2145 * @param port Numerical port (unsigned int format).
2147 * @return A GNUNET_HashCode usable for the new CADET API.
2149 const struct GNUNET_HashCode *
2150 GC_u2h (uint32_t port)
2152 static struct GNUNET_HashCode hash;
2154 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2155 "This is a transitional function, "
2156 "use proper crypto hashes as CADET ports\n");
2157 GNUNET_CRYPTO_hash (&port, sizeof (port), &hash);