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_ChannelOpenMessageMessage *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_ChannelDestroyMessage *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, GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DESTROY);
636 d_msg->channel_id = msg->channel_id;
637 GNUNET_MQ_send (h->mq, env);
644 * Process the channel destroy notification and free associated resources
646 * @param h The cadet handle
647 * @param msg A message with the details of the channel being destroyed
650 handle_channel_destroy (void *cls,
651 const struct GNUNET_CADET_ChannelDestroyMessage *msg)
653 struct GNUNET_CADET_Handle *h = cls;
654 struct GNUNET_CADET_Channel *ch;
655 struct GNUNET_CADET_ClientChannelNumber chid;
657 chid = msg->channel_id;
658 LOG (GNUNET_ERROR_TYPE_DEBUG,
659 "Channel %X Destroy from service\n",
660 ntohl (chid.channel_of_client));
661 ch = retrieve_channel (h,
666 LOG (GNUNET_ERROR_TYPE_DEBUG,
667 "channel %X unknown\n",
668 ntohl (chid.channel_of_client));
677 * Check that message received from CADET service is well-formed.
679 * @param cls the `struct GNUNET_CADET_Handle`
680 * @param message the message we got
681 * @return #GNUNET_OK if the message is well-formed,
682 * #GNUNET_SYSERR otherwise
685 check_local_data (void *cls,
686 const struct GNUNET_CADET_LocalData *message)
688 struct GNUNET_CADET_Handle *h = cls;
689 struct GNUNET_CADET_Channel *ch;
692 size = ntohs (message->header.size);
693 if (sizeof (*message) + sizeof (struct GNUNET_MessageHeader) > size)
696 return GNUNET_SYSERR;
699 ch = retrieve_channel (h,
700 message->channel_id);
704 return GNUNET_SYSERR;
712 * Process the incoming data packets, call appropriate handlers.
714 * @param h The cadet handle
715 * @param message A message encapsulating the data
718 handle_local_data (void *cls,
719 const struct GNUNET_CADET_LocalData *message)
721 struct GNUNET_CADET_Handle *h = cls;
722 const struct GNUNET_MessageHeader *payload;
723 const struct GNUNET_CADET_MessageHandler *handler;
724 struct GNUNET_CADET_Channel *ch;
728 LOG (GNUNET_ERROR_TYPE_DEBUG,
729 "Got a data message!\n");
730 ch = retrieve_channel (h, message->channel_id);
731 GNUNET_assert (NULL != ch);
733 payload = (struct GNUNET_MessageHeader *) &message[1];
734 LOG (GNUNET_ERROR_TYPE_DEBUG, " %s data on channel %s [%X]\n",
735 GC_f2s (ntohl (ch->chid.channel_of_client) >=
736 GNUNET_CADET_LOCAL_CHANNEL_ID_CLI),
737 GNUNET_i2s (GNUNET_PEER_resolve2 (ch->peer)),
738 ntohl (message->channel_id.channel_of_client));
740 type = ntohs (payload->type);
741 LOG (GNUNET_ERROR_TYPE_DEBUG, " payload type %s\n", GC_m2s (type));
742 for (i = 0; i < h->n_handlers; i++)
744 handler = &h->message_handlers[i];
745 LOG (GNUNET_ERROR_TYPE_DEBUG, " checking handler for type %u\n",
747 if (handler->type == type)
750 handler->callback (h->cls, ch, &ch->ctx, payload))
752 LOG (GNUNET_ERROR_TYPE_DEBUG, "callback caused disconnection\n");
753 GNUNET_CADET_channel_destroy (ch);
758 LOG (GNUNET_ERROR_TYPE_DEBUG,
759 "callback completed successfully\n");
768 * Process a local ACK message, enabling the client to send
769 * more data to the service.
771 * @param h Cadet handle.
772 * @param message Message itself.
775 handle_local_ack (void *cls,
776 const struct GNUNET_CADET_LocalAck *message)
778 struct GNUNET_CADET_Handle *h = cls;
779 struct GNUNET_CADET_Channel *ch;
780 struct GNUNET_CADET_ClientChannelNumber chid;
782 LOG (GNUNET_ERROR_TYPE_DEBUG, "Got an ACK!\n");
783 chid = message->channel_id;
784 ch = retrieve_channel (h, chid);
787 LOG (GNUNET_ERROR_TYPE_DEBUG,
788 "ACK on unknown channel %X\n",
789 ntohl (chid.channel_of_client));
792 LOG (GNUNET_ERROR_TYPE_DEBUG,
794 ntohl (ch->chid.channel_of_client));
795 ch->allow_send = GNUNET_YES;
796 if (0 < ch->packet_size)
798 struct GNUNET_CADET_TransmitHandle *th;
799 struct GNUNET_CADET_TransmitHandle *next;
800 LOG (GNUNET_ERROR_TYPE_DEBUG,
801 " pending data, sending %u bytes!\n",
803 for (th = h->th_head; NULL != th; th = next)
806 if (th->channel == ch)
808 GNUNET_assert (NULL == th->request_data_task);
809 th->request_data_task = GNUNET_SCHEDULER_add_now (&request_data, th);
813 /* Complain if we got thru all th without sending anything, ch was wrong */
814 GNUNET_break (NULL != th);
819 * Reconnect to the service, retransmit all infomation to try to restore the
822 * @param h handle to the cadet
824 * @return #GNUNET_YES in case of sucess, #GNUNET_NO otherwise (service down...)
827 reconnect (struct GNUNET_CADET_Handle *h);
831 * Reconnect callback: tries to reconnect again after a failer previous
834 * @param cls closure (cadet handle)
837 reconnect_cbk (void *cls);
841 * Generic error handler, called with the appropriate error code and
842 * the same closure specified at the creation of the message queue.
843 * Not every message queue implementation supports an error handler.
845 * @param cls closure, a `struct GNUNET_CORE_Handle *`
846 * @param error error code
849 handle_mq_error (void *cls,
850 enum GNUNET_MQ_Error error)
852 struct GNUNET_CADET_Handle *h = cls;
854 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MQ ERROR: %u\n", error);
855 GNUNET_MQ_destroy (h->mq);
862 * Process a local reply about info on all channels, pass info to the user.
864 * @param h Cadet handle.
865 * @param message Message itself.
868 // process_get_channels (struct GNUNET_CADET_Handle *h,
869 // const struct GNUNET_MessageHeader *message)
871 // struct GNUNET_CADET_LocalInfo *msg;
873 // GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Get Channels messasge received\n");
875 // if (NULL == h->channels_cb)
877 // GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " ignored\n");
881 // msg = (struct GNUNET_CADET_LocalInfo *) message;
882 // if (ntohs (message->size) !=
883 // (sizeof (struct GNUNET_CADET_LocalInfo) +
884 // sizeof (struct GNUNET_PeerIdentity)))
886 // GNUNET_break_op (0);
887 // GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
888 // "Get channels message: size %hu - expected %u\n",
889 // ntohs (message->size),
890 // sizeof (struct GNUNET_CADET_LocalInfo));
893 // h->channels_cb (h->channels_cls,
894 // ntohl (msg->channel_id),
896 // &msg->destination);
902 * Process a local monitor_channel reply, pass info to the user.
904 * @param h Cadet handle.
905 * @param message Message itself.
908 // process_show_channel (struct GNUNET_CADET_Handle *h,
909 // const struct GNUNET_MessageHeader *message)
911 // struct GNUNET_CADET_LocalInfo *msg;
914 // GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Show Channel messasge received\n");
916 // if (NULL == h->channel_cb)
918 // GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " ignored\n");
922 // /* Verify message sanity */
923 // msg = (struct GNUNET_CADET_LocalInfo *) message;
924 // esize = sizeof (struct GNUNET_CADET_LocalInfo);
925 // if (ntohs (message->size) != esize)
927 // GNUNET_break_op (0);
928 // GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
929 // "Show channel message: size %hu - expected %u\n",
930 // ntohs (message->size),
933 // h->channel_cb (h->channel_cls, NULL, NULL);
934 // h->channel_cb = NULL;
935 // h->channel_cls = NULL;
940 // h->channel_cb (h->channel_cls,
941 // &msg->destination,
948 * Check that message received from CADET service is well-formed.
950 * @param cls the `struct GNUNET_CADET_Handle`
951 * @param message the message we got
952 * @return #GNUNET_OK if the message is well-formed,
953 * #GNUNET_SYSERR otherwise
956 check_get_peers (void *cls,
957 const struct GNUNET_CADET_LocalInfoPeer *message)
959 struct GNUNET_CADET_Handle *h = cls;
962 if (NULL == h->info_cb.peers_cb)
964 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
965 " no handler for peesr monitor message!\n");
966 return GNUNET_SYSERR;
969 size = ntohs (message->header.size);
970 if (sizeof (struct GNUNET_CADET_LocalInfoPeer) > size)
972 h->info_cb.peers_cb (h->info_cls, NULL, -1, 0, 0);
973 h->info_cb.peers_cb = NULL;
975 return GNUNET_SYSERR;
983 * Process a local reply about info on all tunnels, pass info to the user.
985 * @param cls Closure (Cadet handle).
986 * @param msg Message itself.
989 handle_get_peers (void *cls,
990 const struct GNUNET_CADET_LocalInfoPeer *msg)
992 struct GNUNET_CADET_Handle *h = cls;
993 h->info_cb.peers_cb (h->info_cls, &msg->destination,
994 (int) ntohs (msg->tunnel),
995 (unsigned int ) ntohs (msg->paths),
1001 * Check that message received from CADET service is well-formed.
1003 * @param cls the `struct GNUNET_CADET_Handle`
1004 * @param message the message we got
1005 * @return #GNUNET_OK if the message is well-formed,
1006 * #GNUNET_SYSERR otherwise
1009 check_get_peer (void *cls,
1010 const struct GNUNET_CADET_LocalInfoPeer *message)
1012 struct GNUNET_CADET_Handle *h = cls;
1013 const size_t msize = sizeof (struct GNUNET_CADET_LocalInfoPeer);
1014 struct GNUNET_PeerIdentity *paths_array;
1016 unsigned int epaths;
1020 if (NULL == h->info_cb.peer_cb)
1022 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1023 " no handler for peer monitor message!\n");
1027 /* Verify message sanity */
1028 esize = ntohs (message->header.size);
1031 GNUNET_break_op (0);
1032 h->info_cb.peer_cb (h->info_cls, NULL, 0, 0, 0, NULL);
1035 if (0 != ((esize - msize) % sizeof (struct GNUNET_PeerIdentity)))
1037 GNUNET_break_op (0);
1038 h->info_cb.peer_cb (h->info_cls, NULL, 0, 0, 0, NULL);
1042 peers = (esize - msize) / sizeof (struct GNUNET_PeerIdentity);
1043 epaths = (unsigned int) ntohs (message->paths);
1044 paths_array = (struct GNUNET_PeerIdentity *) &message[1];
1046 for (int i = 0; i < peers; i++)
1048 if (0 == memcmp (&paths_array[i], &message->destination,
1049 sizeof (struct GNUNET_PeerIdentity)))
1054 if (paths != epaths)
1056 GNUNET_break_op (0);
1057 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "p:%u, e: %u\n", paths, epaths);
1058 h->info_cb.peer_cb (h->info_cls, NULL, 0, 0, 0, NULL);
1065 h->info_cb.peer_cb = NULL;
1067 return GNUNET_SYSERR;
1072 * Process a local peer info reply, pass info to the user.
1074 * @param cls Closure (Cadet handle).
1075 * @param message Message itself.
1078 handle_get_peer (void *cls,
1079 const struct GNUNET_CADET_LocalInfoPeer *message)
1081 struct GNUNET_CADET_Handle *h = cls;
1082 struct GNUNET_PeerIdentity *paths_array;
1084 unsigned int path_length;
1088 paths = (unsigned int) ntohs (message->paths);
1089 paths_array = (struct GNUNET_PeerIdentity *) &message[1];
1090 peers = (ntohs (message->header.size) - sizeof (*message))
1091 / sizeof (struct GNUNET_PeerIdentity);
1093 neighbor = GNUNET_NO;
1095 for (int i = 0; i < peers; i++)
1097 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " %s\n", GNUNET_i2s (&paths_array[i]));
1099 if (0 == memcmp (&paths_array[i], &message->destination,
1100 sizeof (struct GNUNET_PeerIdentity)))
1102 if (1 == path_length)
1103 neighbor = GNUNET_YES;
1108 /* Call Callback with tunnel info. */
1109 paths_array = (struct GNUNET_PeerIdentity *) &message[1];
1110 h->info_cb.peer_cb (h->info_cls,
1111 &message->destination,
1112 (int) ntohs (message->tunnel),
1120 * Check that message received from CADET service is well-formed.
1122 * @param cls the `struct GNUNET_CADET_Handle`
1123 * @param msg the message we got
1124 * @return #GNUNET_OK if the message is well-formed,
1125 * #GNUNET_SYSERR otherwise
1128 check_get_tunnels (void *cls,
1129 const struct GNUNET_CADET_LocalInfoTunnel *msg)
1131 struct GNUNET_CADET_Handle *h = cls;
1134 if (NULL == h->info_cb.tunnels_cb)
1136 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1137 " no handler for tunnels monitor message!\n");
1138 return GNUNET_SYSERR;
1141 size = ntohs (msg->header.size);
1142 if (sizeof (struct GNUNET_CADET_LocalInfoTunnel) > size)
1144 h->info_cb.tunnels_cb (h->info_cls, NULL, 0, 0, 0, 0);
1145 h->info_cb.tunnels_cb = NULL;
1147 return GNUNET_SYSERR;
1154 * Process a local reply about info on all tunnels, pass info to the user.
1156 * @param cls Closure (Cadet handle).
1157 * @param message Message itself.
1160 handle_get_tunnels (void *cls,
1161 const struct GNUNET_CADET_LocalInfoTunnel *msg)
1163 struct GNUNET_CADET_Handle *h = cls;
1165 h->info_cb.tunnels_cb (h->info_cls,
1167 ntohl (msg->channels),
1168 ntohl (msg->connections),
1169 ntohs (msg->estate),
1170 ntohs (msg->cstate));
1176 * Check that message received from CADET service is well-formed.
1178 * @param cls the `struct GNUNET_CADET_Handle`
1179 * @param msg the message we got
1180 * @return #GNUNET_OK if the message is well-formed,
1181 * #GNUNET_SYSERR otherwise
1184 check_get_tunnel (void *cls,
1185 const struct GNUNET_CADET_LocalInfoTunnel *msg)
1187 struct GNUNET_CADET_Handle *h = cls;
1193 if (NULL == h->info_cb.tunnel_cb)
1195 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1196 " no handler for tunnel monitor message!\n");
1200 /* Verify message sanity */
1201 msize = ntohs (msg->header.size);
1202 esize = sizeof (struct GNUNET_CADET_LocalInfoTunnel);
1205 GNUNET_break_op (0);
1206 h->info_cb.tunnel_cb (h->info_cls,
1207 NULL, 0, 0, NULL, NULL, 0, 0);
1210 ch_n = ntohl (msg->channels);
1211 c_n = ntohl (msg->connections);
1212 esize += ch_n * sizeof (struct GNUNET_CADET_ChannelTunnelNumber);
1213 esize += c_n * sizeof (struct GNUNET_CADET_ConnectionTunnelIdentifier);
1216 GNUNET_break_op (0);
1217 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1218 "m:%u, e: %u (%u ch, %u conn)\n",
1219 (unsigned int) msize,
1220 (unsigned int) esize,
1223 h->info_cb.tunnel_cb (h->info_cls,
1224 NULL, 0, 0, NULL, NULL, 0, 0);
1231 h->info_cb.tunnel_cb = NULL;
1233 return GNUNET_SYSERR;
1238 * Process a local tunnel info reply, pass info to the user.
1240 * @param cls Closure (Cadet handle).
1241 * @param msg Message itself.
1244 handle_get_tunnel (void *cls,
1245 const struct GNUNET_CADET_LocalInfoTunnel *msg)
1247 struct GNUNET_CADET_Handle *h = cls;
1250 const struct GNUNET_CADET_ConnectionTunnelIdentifier *conns;
1251 const struct GNUNET_CADET_ChannelTunnelNumber *chns;
1253 ch_n = ntohl (msg->channels);
1254 c_n = ntohl (msg->connections);
1256 /* Call Callback with tunnel info. */
1257 conns = (const struct GNUNET_CADET_ConnectionTunnelIdentifier *) &msg[1];
1258 chns = (const struct GNUNET_CADET_ChannelTunnelNumber *) &conns[c_n];
1259 h->info_cb.tunnel_cb (h->info_cls,
1265 ntohs (msg->estate),
1266 ntohs (msg->cstate));
1272 * Reconnect to the service, retransmit all infomation to try to restore the
1275 * @param h handle to the cadet
1277 * @return GNUNET_YES in case of sucess, GNUNET_NO otherwise (service down...)
1280 do_reconnect (struct GNUNET_CADET_Handle *h)
1282 struct GNUNET_MQ_MessageHandler handlers[] = {
1283 GNUNET_MQ_hd_fixed_size (channel_created,
1284 GNUNET_MESSAGE_TYPE_CADET_CHANNEL_OPEN,
1285 struct GNUNET_CADET_ChannelOpenMessageMessage,
1287 GNUNET_MQ_hd_fixed_size (channel_destroy,
1288 GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DESTROY,
1289 struct GNUNET_CADET_ChannelDestroyMessage,
1291 GNUNET_MQ_hd_var_size (local_data,
1292 GNUNET_MESSAGE_TYPE_CADET_LOCAL_DATA,
1293 struct GNUNET_CADET_LocalData,
1295 GNUNET_MQ_hd_fixed_size (local_ack,
1296 GNUNET_MESSAGE_TYPE_CADET_LOCAL_ACK,
1297 struct GNUNET_CADET_LocalAck,
1299 GNUNET_MQ_hd_var_size (get_peers,
1300 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEERS,
1301 struct GNUNET_CADET_LocalInfoPeer,
1303 GNUNET_MQ_hd_var_size (get_peer,
1304 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEER,
1305 struct GNUNET_CADET_LocalInfoPeer,
1307 GNUNET_MQ_hd_var_size (get_tunnels,
1308 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNELS,
1309 struct GNUNET_CADET_LocalInfoTunnel,
1311 GNUNET_MQ_hd_var_size (get_tunnel,
1312 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNEL,
1313 struct GNUNET_CADET_LocalInfoTunnel,
1316 // GNUNET_MQ_hd_fixed_Y size (channel_destroyed,
1317 // GNUNET_MESSAGE_TYPE_CADET_CHANNEL_OPEN_NACK_DEPRECATED,
1318 // struct GNUNET_CADET_ChannelDestroyMessage);
1319 GNUNET_MQ_handler_end ()
1322 LOG (GNUNET_ERROR_TYPE_DEBUG, "Connecting to CADET\n");
1324 GNUNET_assert (NULL == h->mq);
1325 h->mq = GNUNET_CLIENT_connect (h->cfg,
1337 h->reconnect_time = GNUNET_TIME_UNIT_MILLISECONDS;
1343 * Reconnect callback: tries to reconnect again after a failer previous
1346 * @param cls closure (cadet handle)
1349 reconnect_cbk (void *cls)
1351 struct GNUNET_CADET_Handle *h = cls;
1353 h->reconnect_task = NULL;
1359 * Reconnect to the service, retransmit all infomation to try to restore the
1362 * @param h handle to the cadet
1364 * @return #GNUNET_YES in case of sucess, #GNUNET_NO otherwise (service down...)
1367 reconnect (struct GNUNET_CADET_Handle *h)
1369 struct GNUNET_CADET_Channel *ch;
1371 LOG (GNUNET_ERROR_TYPE_DEBUG,
1372 "Requested RECONNECT, destroying all channels\n");
1373 for (ch = h->channels_head; NULL != ch; ch = h->channels_head)
1374 destroy_channel (ch, GNUNET_YES);
1375 if (NULL == h->reconnect_task)
1376 h->reconnect_task = GNUNET_SCHEDULER_add_delayed (h->reconnect_time,
1381 /******************************************************************************/
1382 /********************** API CALL DEFINITIONS *************************/
1383 /******************************************************************************/
1385 struct GNUNET_CADET_Handle *
1386 GNUNET_CADET_connect (const struct GNUNET_CONFIGURATION_Handle *cfg,
1388 GNUNET_CADET_ChannelEndHandler cleaner,
1389 const struct GNUNET_CADET_MessageHandler *handlers)
1391 struct GNUNET_CADET_Handle *h;
1393 LOG (GNUNET_ERROR_TYPE_DEBUG, "GNUNET_CADET_connect()\n");
1394 h = GNUNET_new (struct GNUNET_CADET_Handle);
1395 LOG (GNUNET_ERROR_TYPE_DEBUG, " addr %p\n", h);
1397 h->cleaner = cleaner;
1398 h->ports = GNUNET_CONTAINER_multihashmap_create (4, GNUNET_YES);
1403 GNUNET_CADET_disconnect (h);
1407 h->message_handlers = handlers;
1408 h->next_chid.channel_of_client = htonl (GNUNET_CADET_LOCAL_CHANNEL_ID_CLI);
1409 h->reconnect_time = GNUNET_TIME_UNIT_MILLISECONDS;
1410 h->reconnect_task = NULL;
1412 /* count handlers */
1413 for (h->n_handlers = 0;
1414 handlers && handlers[h->n_handlers].type;
1416 LOG (GNUNET_ERROR_TYPE_DEBUG, "GNUNET_CADET_connect() END\n");
1422 GNUNET_CADET_disconnect (struct GNUNET_CADET_Handle *handle)
1424 struct GNUNET_CADET_Channel *ch;
1425 struct GNUNET_CADET_Channel *aux;
1426 struct GNUNET_CADET_TransmitHandle *th;
1428 LOG (GNUNET_ERROR_TYPE_DEBUG,
1429 "CADET DISCONNECT\n");
1430 ch = handle->channels_head;
1434 if (ntohl (ch->chid.channel_of_client) >= GNUNET_CADET_LOCAL_CHANNEL_ID_CLI)
1437 LOG (GNUNET_ERROR_TYPE_DEBUG,
1438 "channel %X not destroyed\n",
1439 ntohl (ch->chid.channel_of_client));
1441 destroy_channel (ch,
1445 while (NULL != (th = handle->th_head))
1447 struct GNUNET_MessageHeader *msg;
1449 /* Make sure it is an allowed packet (everything else should have been
1450 * already canceled).
1452 GNUNET_break (GNUNET_NO == th_is_payload (th));
1453 msg = (struct GNUNET_MessageHeader *) &th[1];
1454 switch (ntohs(msg->type))
1456 case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_OPEN:
1457 case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DESTROY:
1458 case GNUNET_MESSAGE_TYPE_CADET_LOCAL_PORT_OPEN:
1459 case GNUNET_MESSAGE_TYPE_CADET_LOCAL_PORT_CLOSE:
1460 case GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_CHANNELS:
1461 case GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_CHANNEL:
1462 case GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEER:
1463 case GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEERS:
1464 case GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNEL:
1465 case GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNELS:
1469 LOG (GNUNET_ERROR_TYPE_ERROR, "unexpected unsent msg %s\n",
1470 GC_m2s (ntohs(msg->type)));
1473 GNUNET_CADET_notify_transmit_ready_cancel (th);
1476 if (NULL != handle->mq)
1478 GNUNET_MQ_destroy (handle->mq);
1481 if (NULL != handle->reconnect_task)
1483 GNUNET_SCHEDULER_cancel(handle->reconnect_task);
1484 handle->reconnect_task = NULL;
1487 GNUNET_CONTAINER_multihashmap_destroy (handle->ports);
1488 handle->ports = NULL;
1489 GNUNET_free (handle);
1494 * Open a port to receive incomming channels.
1496 * @param h CADET handle.
1497 * @param port Hash representing the port number.
1498 * @param new_channel Function called when an channel is received.
1499 * @param new_channel_cls Closure for @a new_channel.
1501 * @return Port handle.
1503 struct GNUNET_CADET_Port *
1504 GNUNET_CADET_open_port (struct GNUNET_CADET_Handle *h,
1505 const struct GNUNET_HashCode *port,
1506 GNUNET_CADET_InboundChannelNotificationHandler
1508 void *new_channel_cls)
1510 struct GNUNET_CADET_PortMessage *msg;
1511 struct GNUNET_MQ_Envelope *env;
1512 struct GNUNET_CADET_Port *p;
1514 GNUNET_assert (NULL != new_channel);
1515 p = GNUNET_new (struct GNUNET_CADET_Port);
1517 p->hash = GNUNET_new (struct GNUNET_HashCode);
1519 p->handler = new_channel;
1520 p->cls = new_channel_cls;
1521 GNUNET_assert (GNUNET_OK ==
1522 GNUNET_CONTAINER_multihashmap_put (h->ports,
1525 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1527 env = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_CADET_LOCAL_PORT_OPEN);
1528 msg->port = *p->hash;
1529 GNUNET_MQ_send (h->mq, env);
1535 * Close a port opened with @a GNUNET_CADET_open_port.
1536 * The @a new_channel callback will no longer be called.
1538 * @param p Port handle.
1541 GNUNET_CADET_close_port (struct GNUNET_CADET_Port *p)
1543 struct GNUNET_CADET_PortMessage *msg;
1544 struct GNUNET_MQ_Envelope *env;
1546 env = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_CADET_LOCAL_PORT_CLOSE);
1548 msg->port = *p->hash;
1549 GNUNET_MQ_send (p->cadet->mq, env);
1550 GNUNET_CONTAINER_multihashmap_remove (p->cadet->ports, p->hash, p);
1551 GNUNET_free (p->hash);
1557 * Create a new channel towards a remote peer.
1559 * If the destination port is not open by any peer or the destination peer
1560 * does not accept the channel, #GNUNET_CADET_ChannelEndHandler will be called
1563 * @param h cadet handle
1564 * @param channel_ctx client's channel context to associate with the channel
1565 * @param peer peer identity the channel should go to
1566 * @param port Port hash (port number).
1567 * @param options CadetOption flag field, with all desired option bits set to 1.
1569 * @return handle to the channel
1571 struct GNUNET_CADET_Channel *
1572 GNUNET_CADET_channel_create (struct GNUNET_CADET_Handle *h,
1574 const struct GNUNET_PeerIdentity *peer,
1575 const struct GNUNET_HashCode *port,
1576 enum GNUNET_CADET_ChannelOption options)
1578 struct GNUNET_CADET_ChannelOpenMessageMessage *msg;
1579 struct GNUNET_MQ_Envelope *env;
1580 struct GNUNET_CADET_Channel *ch;
1581 struct GNUNET_CADET_ClientChannelNumber chid;
1583 LOG (GNUNET_ERROR_TYPE_DEBUG,
1584 "Creating new channel to %s:%u\n",
1585 GNUNET_i2s (peer), port);
1586 chid.channel_of_client = htonl (0);
1587 ch = create_channel (h, chid);
1588 LOG (GNUNET_ERROR_TYPE_DEBUG, " at %p\n", ch);
1589 LOG (GNUNET_ERROR_TYPE_DEBUG, " number %X\n",
1590 ntohl (ch->chid.channel_of_client));
1591 ch->ctx = channel_ctx;
1592 ch->peer = GNUNET_PEER_intern (peer);
1594 env = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_CADET_CHANNEL_OPEN);
1595 msg->channel_id = ch->chid;
1598 msg->opt = htonl (options);
1599 ch->allow_send = GNUNET_NO;
1600 GNUNET_MQ_send (h->mq,
1607 GNUNET_CADET_channel_destroy (struct GNUNET_CADET_Channel *channel)
1609 struct GNUNET_CADET_Handle *h;
1610 struct GNUNET_CADET_ChannelDestroyMessage *msg;
1611 struct GNUNET_MQ_Envelope *env;
1612 struct GNUNET_CADET_TransmitHandle *th;
1613 struct GNUNET_CADET_TransmitHandle *next;
1615 LOG (GNUNET_ERROR_TYPE_DEBUG, "Destroying channel\n");
1617 for (th = h->th_head; th != NULL; th = next)
1620 if (th->channel == channel)
1623 if (GNUNET_YES == th_is_payload (th))
1625 /* applications should cancel before destroying channel */
1626 LOG (GNUNET_ERROR_TYPE_WARNING,
1627 "Channel destroyed without cancelling transmission requests\n");
1628 th->notify (th->notify_cls, 0, NULL);
1632 LOG (GNUNET_ERROR_TYPE_WARNING, "no meta-traffic should be queued\n");
1634 GNUNET_CONTAINER_DLL_remove (h->th_head, h->th_tail, th);
1635 GNUNET_CADET_notify_transmit_ready_cancel (th);
1639 env = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DESTROY);
1640 msg->channel_id = channel->chid;
1641 GNUNET_MQ_send (h->mq, env);
1643 destroy_channel (channel, GNUNET_YES);
1648 * Get information about a channel.
1650 * @param channel Channel handle.
1651 * @param option Query (GNUNET_CADET_OPTION_*).
1652 * @param ... dependant on option, currently not used
1654 * @return Union with an answer to the query.
1656 const union GNUNET_CADET_ChannelInfo *
1657 GNUNET_CADET_channel_get_info (struct GNUNET_CADET_Channel *channel,
1658 enum GNUNET_CADET_ChannelOption option, ...)
1660 static int bool_flag;
1661 const union GNUNET_CADET_ChannelInfo *ret;
1665 case GNUNET_CADET_OPTION_NOBUFFER:
1666 case GNUNET_CADET_OPTION_RELIABLE:
1667 case GNUNET_CADET_OPTION_OUT_OF_ORDER:
1668 if (0 != (option & channel->options))
1669 bool_flag = GNUNET_YES;
1671 bool_flag = GNUNET_NO;
1672 ret = (const union GNUNET_CADET_ChannelInfo *) &bool_flag;
1674 case GNUNET_CADET_OPTION_PEER:
1675 ret = (const union GNUNET_CADET_ChannelInfo *) GNUNET_PEER_resolve2 (channel->peer);
1686 struct GNUNET_CADET_TransmitHandle *
1687 GNUNET_CADET_notify_transmit_ready (struct GNUNET_CADET_Channel *channel,
1689 struct GNUNET_TIME_Relative maxdelay,
1691 GNUNET_CONNECTION_TransmitReadyNotify notify,
1694 struct GNUNET_CADET_TransmitHandle *th;
1696 GNUNET_assert (NULL != channel);
1697 GNUNET_assert (GNUNET_CONSTANTS_MAX_CADET_MESSAGE_SIZE >= notify_size);
1698 LOG (GNUNET_ERROR_TYPE_DEBUG, "CADET NOTIFY TRANSMIT READY\n");
1699 LOG (GNUNET_ERROR_TYPE_DEBUG, " on channel %X\n", channel->chid);
1700 LOG (GNUNET_ERROR_TYPE_DEBUG, " allow_send %d\n", channel->allow_send);
1701 if (ntohl (channel->chid.channel_of_client) >=
1702 GNUNET_CADET_LOCAL_CHANNEL_ID_CLI)
1703 LOG (GNUNET_ERROR_TYPE_DEBUG, " to origin\n");
1705 LOG (GNUNET_ERROR_TYPE_DEBUG, " to destination\n");
1706 LOG (GNUNET_ERROR_TYPE_DEBUG, " payload size %u\n", notify_size);
1707 GNUNET_assert (NULL != notify);
1708 GNUNET_assert (0 == channel->packet_size); // Only one data packet allowed
1710 if (GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us != maxdelay.rel_value_us)
1712 LOG (GNUNET_ERROR_TYPE_WARNING,
1713 "CADET transmit ready timeout is deprected (has no effect)\n");
1716 th = GNUNET_new (struct GNUNET_CADET_TransmitHandle);
1717 th->channel = channel;
1718 th->size = notify_size;
1719 channel->packet_size = th->size;
1720 LOG (GNUNET_ERROR_TYPE_DEBUG, " total size %u\n", th->size);
1721 th->notify = notify;
1722 th->notify_cls = notify_cls;
1723 if (GNUNET_YES == channel->allow_send)
1724 th->request_data_task = GNUNET_SCHEDULER_add_now (&request_data, th);
1726 add_to_queue (channel->cadet, th);
1728 LOG (GNUNET_ERROR_TYPE_DEBUG, "CADET NOTIFY TRANSMIT READY END\n");
1734 GNUNET_CADET_notify_transmit_ready_cancel (struct GNUNET_CADET_TransmitHandle *th)
1736 if (NULL != th->request_data_task)
1738 GNUNET_SCHEDULER_cancel (th->request_data_task);
1740 th->request_data_task = NULL;
1742 remove_from_queue (th);
1748 GNUNET_CADET_receive_done (struct GNUNET_CADET_Channel *channel)
1755 send_info_request (struct GNUNET_CADET_Handle *h, uint16_t type)
1757 struct GNUNET_MessageHeader *msg;
1758 struct GNUNET_MQ_Envelope *env;
1760 LOG (GNUNET_ERROR_TYPE_DEBUG,
1761 " Sending %s monitor message to service\n",
1764 env = GNUNET_MQ_msg (msg, type);
1765 GNUNET_MQ_send (h->mq, env);
1770 * Request a debug dump on the service's STDERR.
1772 * WARNING: unstable API, likely to change in the future!
1774 * @param h cadet handle
1777 GNUNET_CADET_request_dump (struct GNUNET_CADET_Handle *h)
1779 LOG (GNUNET_ERROR_TYPE_DEBUG, "requesting dump\n");
1780 send_info_request (h, GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_DUMP);
1785 * Request information about peers known to the running cadet service.
1786 * The callback will be called for every peer known to the service.
1787 * Only one info request (of any kind) can be active at once.
1790 * WARNING: unstable API, likely to change in the future!
1792 * @param h Handle to the cadet peer.
1793 * @param callback Function to call with the requested data.
1794 * @param callback_cls Closure for @c callback.
1796 * @return #GNUNET_OK / #GNUNET_SYSERR
1799 GNUNET_CADET_get_peers (struct GNUNET_CADET_Handle *h,
1800 GNUNET_CADET_PeersCB callback,
1803 if (NULL != h->info_cb.peers_cb)
1806 return GNUNET_SYSERR;
1808 send_info_request (h, GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEERS);
1809 h->info_cb.peers_cb = callback;
1810 h->info_cls = callback_cls;
1816 * Cancel a peer info request. The callback will not be called (anymore).
1818 * WARNING: unstable API, likely to change in the future!
1820 * @param h Cadet handle.
1822 * @return Closure given to GNUNET_CADET_get_peers.
1825 GNUNET_CADET_get_peers_cancel (struct GNUNET_CADET_Handle *h)
1830 h->info_cb.peers_cb = NULL;
1837 * Request information about a peer known to the running cadet peer.
1838 * The callback will be called for the tunnel once.
1839 * Only one info request (of any kind) can be active at once.
1841 * WARNING: unstable API, likely to change in the future!
1843 * @param h Handle to the cadet peer.
1844 * @param id Peer whose tunnel to examine.
1845 * @param callback Function to call with the requested data.
1846 * @param callback_cls Closure for @c callback.
1848 * @return #GNUNET_OK / #GNUNET_SYSERR
1851 GNUNET_CADET_get_peer (struct GNUNET_CADET_Handle *h,
1852 const struct GNUNET_PeerIdentity *id,
1853 GNUNET_CADET_PeerCB callback,
1856 struct GNUNET_CADET_LocalInfo *msg;
1857 struct GNUNET_MQ_Envelope *env;
1859 if (NULL != h->info_cb.peer_cb)
1862 return GNUNET_SYSERR;
1865 env = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEER);
1867 GNUNET_MQ_send (h->mq, env);
1869 h->info_cb.peer_cb = callback;
1870 h->info_cls = callback_cls;
1876 * Request information about tunnels of the running cadet peer.
1877 * The callback will be called for every tunnel of the service.
1878 * Only one info request (of any kind) can be active at once.
1880 * WARNING: unstable API, likely to change in the future!
1882 * @param h Handle to the cadet peer.
1883 * @param callback Function to call with the requested data.
1884 * @param callback_cls Closure for @c callback.
1886 * @return #GNUNET_OK / #GNUNET_SYSERR
1889 GNUNET_CADET_get_tunnels (struct GNUNET_CADET_Handle *h,
1890 GNUNET_CADET_TunnelsCB callback,
1893 if (NULL != h->info_cb.tunnels_cb)
1896 return GNUNET_SYSERR;
1898 send_info_request (h, GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNELS);
1899 h->info_cb.tunnels_cb = callback;
1900 h->info_cls = callback_cls;
1906 * Cancel a monitor request. The monitor callback will not be called.
1908 * @param h Cadet handle.
1910 * @return Closure given to GNUNET_CADET_get_tunnels.
1913 GNUNET_CADET_get_tunnels_cancel (struct GNUNET_CADET_Handle *h)
1917 h->info_cb.tunnels_cb = NULL;
1927 * Request information about a tunnel of the running cadet peer.
1928 * The callback will be called for the tunnel once.
1929 * Only one info request (of any kind) can be active at once.
1931 * WARNING: unstable API, likely to change in the future!
1933 * @param h Handle to the cadet peer.
1934 * @param id Peer whose tunnel to examine.
1935 * @param callback Function to call with the requested data.
1936 * @param callback_cls Closure for @c callback.
1938 * @return #GNUNET_OK / #GNUNET_SYSERR
1941 GNUNET_CADET_get_tunnel (struct GNUNET_CADET_Handle *h,
1942 const struct GNUNET_PeerIdentity *id,
1943 GNUNET_CADET_TunnelCB callback,
1946 struct GNUNET_CADET_LocalInfo *msg;
1947 struct GNUNET_MQ_Envelope *env;
1949 if (NULL != h->info_cb.tunnel_cb)
1952 return GNUNET_SYSERR;
1955 env = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNEL);
1957 GNUNET_MQ_send (h->mq, env);
1959 h->info_cb.tunnel_cb = callback;
1960 h->info_cls = callback_cls;
1966 * Request information about a specific channel of the running cadet peer.
1968 * WARNING: unstable API, likely to change in the future!
1969 * FIXME Add destination option.
1971 * @param h Handle to the cadet peer.
1972 * @param initiator ID of the owner of the channel.
1973 * @param channel_number Channel number.
1974 * @param callback Function to call with the requested data.
1975 * @param callback_cls Closure for @c callback.
1977 * @return #GNUNET_OK / #GNUNET_SYSERR
1980 GNUNET_CADET_show_channel (struct GNUNET_CADET_Handle *h,
1981 struct GNUNET_PeerIdentity *initiator,
1982 unsigned int channel_number,
1983 GNUNET_CADET_ChannelCB callback,
1986 struct GNUNET_CADET_LocalInfo *msg;
1987 struct GNUNET_MQ_Envelope *env;
1989 if (NULL != h->info_cb.channel_cb)
1992 return GNUNET_SYSERR;
1995 env = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_CHANNEL);
1996 msg->peer = *initiator;
1997 msg->channel_id.channel_of_client = htonl (channel_number);
1998 GNUNET_MQ_send (h->mq, env);
2000 h->info_cb.channel_cb = callback;
2001 h->info_cls = callback_cls;
2007 * Function called to notify a client about the connection
2008 * begin ready to queue more data. "buf" will be
2009 * NULL and "size" zero if the connection was closed for
2010 * writing in the meantime.
2012 * @param cls closure
2013 * @param size number of bytes available in buf
2014 * @param buf where the callee should write the message
2015 * @return number of bytes written to buf
2018 cadet_mq_ntr (void *cls, size_t size,
2021 struct GNUNET_MQ_Handle *mq = cls;
2022 struct CadetMQState *state = GNUNET_MQ_impl_state (mq);
2023 const struct GNUNET_MessageHeader *msg = GNUNET_MQ_impl_current (mq);
2029 GNUNET_MQ_inject_error (mq, GNUNET_MQ_ERROR_WRITE);
2032 msize = ntohs (msg->size);
2033 GNUNET_assert (msize <= size);
2034 GNUNET_memcpy (buf, msg, msize);
2035 GNUNET_MQ_impl_send_continue (mq);
2041 * Signature of functions implementing the
2042 * sending functionality of a message queue.
2044 * @param mq the message queue
2045 * @param msg the message to send
2046 * @param impl_state state of the implementation
2049 cadet_mq_send_impl (struct GNUNET_MQ_Handle *mq,
2050 const struct GNUNET_MessageHeader *msg,
2053 struct CadetMQState *state = impl_state;
2055 GNUNET_assert (NULL == state->th);
2057 GNUNET_CADET_notify_transmit_ready (state->channel,
2058 /* FIXME: add option for corking */
2060 GNUNET_TIME_UNIT_FOREVER_REL,
2068 * Signature of functions implementing the
2069 * destruction of a message queue.
2070 * Implementations must not free 'mq', but should
2071 * take care of 'impl_state'.
2073 * @param mq the message queue to destroy
2074 * @param impl_state state of the implementation
2077 cadet_mq_destroy_impl (struct GNUNET_MQ_Handle *mq,
2080 struct CadetMQState *state = impl_state;
2082 if (NULL != state->th)
2083 GNUNET_CADET_notify_transmit_ready_cancel (state->th);
2085 GNUNET_free (state);
2090 * Create a message queue for a cadet channel.
2091 * The message queue can only be used to transmit messages,
2092 * not to receive them.
2094 * @param channel the channel to create the message qeue for
2095 * @return a message queue to messages over the channel
2097 struct GNUNET_MQ_Handle *
2098 GNUNET_CADET_mq_create (struct GNUNET_CADET_Channel *channel)
2100 struct GNUNET_MQ_Handle *mq;
2101 struct CadetMQState *state;
2103 state = GNUNET_new (struct CadetMQState);
2104 state->channel = channel;
2106 mq = GNUNET_MQ_queue_for_callbacks (&cadet_mq_send_impl,
2107 &cadet_mq_destroy_impl,
2108 NULL, /* FIXME: cancel impl. */
2110 NULL, /* no msg handlers */
2111 NULL, /* no err handlers */
2112 NULL); /* no handler cls */
2118 * Transitional function to convert an unsigned int port to a hash value.
2119 * WARNING: local static value returned, NOT reentrant!
2120 * WARNING: do not use this function for new code!
2122 * @param port Numerical port (unsigned int format).
2124 * @return A GNUNET_HashCode usable for the new CADET API.
2126 const struct GNUNET_HashCode *
2127 GC_u2h (uint32_t port)
2129 static struct GNUNET_HashCode hash;
2131 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2132 "This is a transitional function, "
2133 "use proper crypto hashes as CADET ports\n");
2134 GNUNET_CRYPTO_hash (&port, sizeof (port), &hash);