355efc5aa2d8c798547b752ac5d502de82b2961c
[oweals/gnunet.git] / src / cadet / cadet_api.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2011, 2017 GNUnet e.V.
4
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.
9
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.
14
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.
19 */
20 /**
21  * @file cadet/cadet_api.c
22  * @brief cadet api: client implementation of cadet service
23  * @author Bartlomiej Polot
24  */
25
26 #include "platform.h"
27 #include "gnunet_util_lib.h"
28 #include "gnunet_constants.h"
29 #include "gnunet_cadet_service.h"
30 #include "cadet.h"
31 #include "cadet_protocol.h"
32
33 #define LOG(kind,...) GNUNET_log_from (kind, "cadet-api",__VA_ARGS__)
34
35 /******************************************************************************/
36 /************************      DATA STRUCTURES     ****************************/
37 /******************************************************************************/
38
39 /**
40  * Transmission queue to the service
41  */
42 struct GNUNET_CADET_TransmitHandle
43 {
44   /**
45    * Double Linked list
46    */
47   struct GNUNET_CADET_TransmitHandle *next;
48
49   /**
50    * Double Linked list
51    */
52   struct GNUNET_CADET_TransmitHandle *prev;
53
54   /**
55    * Channel this message is sent on / for (may be NULL for control messages).
56    */
57   struct GNUNET_CADET_Channel *channel;
58
59   /**
60    * Request data task.
61    */
62   struct GNUNET_SCHEDULER_Task *request_data_task;
63
64   /**
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
68    * the 'target'.
69    */
70   GNUNET_CONNECTION_TransmitReadyNotify notify;
71
72   /**
73    * Closure for 'notify'
74    */
75   void *notify_cls;
76
77   /**
78    * Size of the payload.
79    */
80   size_t size;
81 };
82
83
84 union CadetInfoCB
85 {
86
87   /**
88    * Channel callback.
89    */
90   GNUNET_CADET_ChannelCB channel_cb;
91
92   /**
93    * Monitor callback
94    */
95   GNUNET_CADET_PeersCB peers_cb;
96
97   /**
98    * Monitor callback
99    */
100   GNUNET_CADET_PeerCB peer_cb;
101
102   /**
103    * Monitor callback
104    */
105   GNUNET_CADET_TunnelsCB tunnels_cb;
106
107   /**
108    * Tunnel callback.
109    */
110   GNUNET_CADET_TunnelCB tunnel_cb;
111 };
112
113
114 /**
115  * Opaque handle to the service.
116  */
117 struct GNUNET_CADET_Handle
118 {
119   /**
120    * Message queue (if available).
121    */
122   struct GNUNET_MQ_Handle *mq;
123
124   /**
125    * Set of handlers used for processing incoming messages in the channels
126    */
127   const struct GNUNET_CADET_MessageHandler *message_handlers;
128
129   /**
130    * Number of handlers in the handlers array.
131    */
132   unsigned int n_handlers;
133
134   /**
135    * Ports open.
136    */
137   struct GNUNET_CONTAINER_MultiHashMap *ports;
138
139   /**
140    * Double linked list of the channels this client is connected to, head.
141    */
142   struct GNUNET_CADET_Channel *channels_head;
143
144   /**
145    * Double linked list of the channels this client is connected to, tail.
146    */
147   struct GNUNET_CADET_Channel *channels_tail;
148
149   /**
150    * Callback for inbound channel disconnection
151    */
152   GNUNET_CADET_ChannelEndHandler *cleaner;
153
154   /**
155    * Closure for all the handlers given by the client
156    */
157   void *cls;
158
159   /**
160    * Messages to send to the service, head.
161    */
162   struct GNUNET_CADET_TransmitHandle *th_head;
163
164   /**
165    * Messages to send to the service, tail.
166    */
167   struct GNUNET_CADET_TransmitHandle *th_tail;
168
169   /**
170    * child of the next channel to create (to avoid reusing IDs often)
171    */
172   struct GNUNET_CADET_ClientChannelNumber next_ccn;
173
174   /**
175    * Configuration given by the client, in case of reconnection
176    */
177   const struct GNUNET_CONFIGURATION_Handle *cfg;
178
179   /**
180    * Time to the next reconnect in case one reconnect fails
181    */
182   struct GNUNET_TIME_Relative reconnect_time;
183
184   /**
185    * Task for trying to reconnect.
186    */
187   struct GNUNET_SCHEDULER_Task * reconnect_task;
188
189   /**
190    * Callback for an info task (only one active at a time).
191    */
192   union CadetInfoCB info_cb;
193
194   /**
195    * Info callback closure for @c info_cb.
196    */
197   void *info_cls;
198 };
199
200
201 /**
202  * Description of a peer
203  */
204 struct GNUNET_CADET_Peer
205 {
206   /**
207    * ID of the peer in short form
208    */
209   GNUNET_PEER_Id id;
210
211   /**
212    * Channel this peer belongs to
213    */
214   struct GNUNET_CADET_Channel *t;
215 };
216
217
218 /**
219  * Opaque handle to a channel.
220  */
221 struct GNUNET_CADET_Channel
222 {
223   /**
224    * DLL next
225    */
226   struct GNUNET_CADET_Channel *next;
227
228   /**
229    * DLL prev
230    */
231   struct GNUNET_CADET_Channel *prev;
232
233   /**
234    * Handle to the cadet this channel belongs to
235    */
236   struct GNUNET_CADET_Handle *cadet;
237
238   /**
239    * Local ID of the channel
240    */
241   struct GNUNET_CADET_ClientChannelNumber ccn;
242
243   /**
244    * Channel's port, if any.
245    */
246   struct GNUNET_CADET_Port *port;
247
248   /**
249    * Other end of the channel.
250    */
251   GNUNET_PEER_Id peer;
252
253   /**
254    * Any data the caller wants to put in here
255    */
256   void *ctx;
257
258   /**
259    * Channel options: reliability, etc.
260    */
261   enum GNUNET_CADET_ChannelOption options;
262
263   /**
264    * Are we allowed to send to the service?
265    */
266   unsigned int allow_send;
267
268 };
269
270
271 /**
272  * Opaque handle to a port.
273  */
274 struct GNUNET_CADET_Port
275 {
276   /**
277    * Handle to the CADET session this port belongs to.
278    */
279   struct GNUNET_CADET_Handle *cadet;
280
281   /**
282    * Port ID.
283    */
284   struct GNUNET_HashCode *hash;
285
286   /**
287    * Callback handler for incoming channels on this port.
288    */
289   GNUNET_CADET_InboundChannelNotificationHandler *handler;
290
291   /**
292    * Closure for @a handler.
293    */
294   void *cls;
295 };
296
297
298 /**
299  * Implementation state for cadet's message queue.
300  */
301 struct CadetMQState
302 {
303   /**
304    * The current transmit handle, or NULL
305    * if no transmit is active.
306    */
307   struct GNUNET_CADET_TransmitHandle *th;
308
309   /**
310    * Channel to send the data over.
311    */
312   struct GNUNET_CADET_Channel *channel;
313 };
314
315
316 /******************************************************************************/
317 /***********************     AUXILIARY FUNCTIONS      *************************/
318 /******************************************************************************/
319
320 /**
321  * Check if transmission is a payload packet.
322  *
323  * @param th Transmission handle.
324  *
325  * @return #GNUNET_YES if it is a payload packet,
326  *         #GNUNET_NO if it is a cadet management packet.
327  */
328 static int
329 th_is_payload (struct GNUNET_CADET_TransmitHandle *th)
330 {
331   return (th->notify != NULL) ? GNUNET_YES : GNUNET_NO;
332 }
333
334
335 /**
336  * Find the Port struct for a hash.
337  *
338  * @param h CADET handle.
339  * @param hash HashCode for the port number.
340  *
341  * @return The port handle if known, NULL otherwise.
342  */
343 static struct GNUNET_CADET_Port *
344 find_port (const struct GNUNET_CADET_Handle *h,
345            const struct GNUNET_HashCode *hash)
346 {
347   struct GNUNET_CADET_Port *p;
348
349   p = GNUNET_CONTAINER_multihashmap_get (h->ports, hash);
350
351   return p;
352 }
353
354
355 /**
356  * Get the channel handler for the channel specified by id from the given handle
357  *
358  * @param h Cadet handle
359  * @param ccn ID of the wanted channel
360  * @return handle to the required channel or NULL if not found
361  */
362 static struct GNUNET_CADET_Channel *
363 retrieve_channel (struct GNUNET_CADET_Handle *h,
364                   struct GNUNET_CADET_ClientChannelNumber ccn)
365 {
366   struct GNUNET_CADET_Channel *ch;
367
368   for (ch = h->channels_head; NULL != ch; ch = ch->next)
369     if (ch->ccn.channel_of_client == ccn.channel_of_client)
370       return ch;
371   return NULL;
372 }
373
374
375 /**
376  * Create a new channel and insert it in the channel list of the cadet handle
377  *
378  * @param h Cadet handle
379  * @param ccn Desired ccn of the channel, 0 to assign one automatically.
380  *
381  * @return Handle to the created channel.
382  */
383 static struct GNUNET_CADET_Channel *
384 create_channel (struct GNUNET_CADET_Handle *h,
385                 struct GNUNET_CADET_ClientChannelNumber ccn)
386 {
387   struct GNUNET_CADET_Channel *ch;
388
389   ch = GNUNET_new (struct GNUNET_CADET_Channel);
390   GNUNET_CONTAINER_DLL_insert (h->channels_head,
391                                h->channels_tail,
392                                ch);
393   ch->cadet = h;
394   if (0 == ccn.channel_of_client)
395   {
396     ch->ccn = h->next_ccn;
397     while (NULL != retrieve_channel (h,
398                                      h->next_ccn))
399     {
400       h->next_ccn.channel_of_client
401         = htonl (1 + ntohl (h->next_ccn.channel_of_client));
402       if (0 == ntohl (h->next_ccn.channel_of_client))
403         h->next_ccn.channel_of_client
404           = htonl (GNUNET_CADET_LOCAL_CHANNEL_ID_CLI);
405     }
406   }
407   else
408   {
409     ch->ccn = ccn;
410   }
411   return ch;
412 }
413
414
415 /**
416  * Destroy the specified channel.
417  * - Destroys all peers, calling the disconnect callback on each if needed
418  * - Cancels all outgoing traffic for that channel, calling respective notifys
419  * - Calls cleaner if channel was inbound
420  * - Frees all memory used
421  *
422  * @param ch Pointer to the channel.
423  * @param call_cleaner Whether to call the cleaner handler.
424  *
425  * @return Handle to the required channel or NULL if not found.
426  */
427 // FIXME: simplify: call_cleaner is always #GNUNET_YES!!!
428 static void
429 destroy_channel (struct GNUNET_CADET_Channel *ch,
430                  int call_cleaner)
431 {
432   struct GNUNET_CADET_Handle *h;
433   struct GNUNET_CADET_TransmitHandle *th;
434   struct GNUNET_CADET_TransmitHandle *next;
435
436   if (NULL == ch)
437   {
438     GNUNET_break (0);
439     return;
440   }
441   h = ch->cadet;
442   LOG (GNUNET_ERROR_TYPE_DEBUG,
443        " destroy_channel %X of %p\n",
444        ch->ccn,
445        h);
446
447   GNUNET_CONTAINER_DLL_remove (h->channels_head,
448                                h->channels_tail,
449                                ch);
450
451   /* signal channel destruction */
452   if ( (NULL != h->cleaner) &&
453        (0 != ch->peer) &&
454        (GNUNET_YES == call_cleaner) )
455   {
456     LOG (GNUNET_ERROR_TYPE_DEBUG,
457          " calling cleaner\n");
458     h->cleaner (h->cls, ch, ch->ctx);
459   }
460
461   /* check that clients did not leave messages behind in the queue */
462   for (th = h->th_head; NULL != th; th = next)
463   {
464     next = th->next;
465     if (th->channel != ch)
466       continue;
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.
470      */
471     GNUNET_break (GNUNET_NO == th_is_payload (th));
472     GNUNET_CADET_notify_transmit_ready_cancel (th);
473   }
474
475   if (0 != ch->peer)
476     GNUNET_PEER_change_rc (ch->peer, -1);
477   GNUNET_free (ch);
478
479 }
480
481
482 /**
483  * Add a transmit handle to the transmission queue and set the
484  * timeout if needed.
485  *
486  * @param h cadet handle with the queue head and tail
487  * @param th handle to the packet to be transmitted
488  */
489 static void
490 add_to_queue (struct GNUNET_CADET_Handle *h,
491               struct GNUNET_CADET_TransmitHandle *th)
492 {
493   GNUNET_CONTAINER_DLL_insert_tail (h->th_head, h->th_tail, th);
494 }
495
496
497 /**
498  * Remove a transmit handle from the transmission queue, if present.
499  *
500  * Safe to call even if not queued.
501  *
502  * @param th handle to the packet to be unqueued.
503  */
504 static void
505 remove_from_queue (struct GNUNET_CADET_TransmitHandle *th)
506 {
507   struct GNUNET_CADET_Handle *h = th->channel->cadet;
508
509   /* It might or might not have been queued (rarely not), but check anyway. */
510   if (NULL != th->next || h->th_tail == th)
511   {
512     GNUNET_CONTAINER_DLL_remove (h->th_head, h->th_tail, th);
513   }
514 }
515
516
517 /**
518  * Send an ack on the channel to confirm the processing of a message.
519  *
520  * @param ch Channel on which to send the ACK.
521  */
522 static void
523 send_ack (struct GNUNET_CADET_Channel *ch)
524 {
525   struct GNUNET_CADET_LocalAck *msg;
526   struct GNUNET_MQ_Envelope *env;
527
528   env = GNUNET_MQ_msg (msg,
529                        GNUNET_MESSAGE_TYPE_CADET_LOCAL_ACK);
530
531   LOG (GNUNET_ERROR_TYPE_DEBUG,
532        "Sending ACK on channel %X\n",
533        ch->ccn.channel_of_client);
534   msg->ccn = ch->ccn;
535   GNUNET_MQ_send (ch->cadet->mq,
536                   env);
537 }
538
539
540
541 /******************************************************************************/
542 /***********************      RECEIVE HANDLERS     ****************************/
543 /******************************************************************************/
544
545
546 /**
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.
550  *
551  * @param cls Closure (transmit handle).
552  */
553 static void
554 request_data (void *cls)
555 {
556   struct GNUNET_CADET_TransmitHandle *th = cls;
557   struct GNUNET_CADET_LocalData *msg;
558   struct GNUNET_MQ_Envelope *env;
559   size_t osize;
560
561   LOG (GNUNET_ERROR_TYPE_DEBUG,
562        "Requesting Data: %u bytes\n",
563        th->size);
564
565   GNUNET_assert (0 < th->channel->allow_send);
566   th->channel->allow_send--;
567   /* NOTE: we may be allowed to send another packet immediately,
568      albeit the current logic waits for the ACK. */
569   th->request_data_task = NULL;
570   remove_from_queue (th);
571
572   env = GNUNET_MQ_msg_extra (msg,
573                              th->size,
574                              GNUNET_MESSAGE_TYPE_CADET_LOCAL_DATA);
575   msg->ccn = th->channel->ccn;
576   osize = th->notify (th->notify_cls,
577                       th->size,
578                       &msg[1]);
579   GNUNET_assert (osize == th->size);
580   GNUNET_MQ_send (th->channel->cadet->mq,
581                   env);
582   GNUNET_free (th);
583 }
584
585
586 /**
587  * Process the new channel notification and add it to the channels in the handle
588  *
589  * @param h     The cadet handle
590  * @param msg   A message with the details of the new incoming channel
591  */
592 static void
593 handle_channel_created (void *cls,
594                         const struct GNUNET_CADET_LocalChannelCreateMessage *msg)
595 {
596   struct GNUNET_CADET_Handle *h = cls;
597   struct GNUNET_CADET_Channel *ch;
598   struct GNUNET_CADET_Port *port;
599   const struct GNUNET_HashCode *port_number;
600   struct GNUNET_CADET_ClientChannelNumber ccn;
601
602   ccn = msg->ccn;
603   port_number = &msg->port;
604   LOG (GNUNET_ERROR_TYPE_DEBUG,
605        "Creating incoming channel %X [%s]\n",
606        ntohl (ccn.channel_of_client),
607        GNUNET_h2s (port_number));
608   if (ntohl (ccn.channel_of_client) >= GNUNET_CADET_LOCAL_CHANNEL_ID_CLI)
609   {
610     GNUNET_break (0);
611     return;
612   }
613   port = find_port (h, port_number);
614   if (NULL != port)
615   {
616     void *ctx;
617
618     ch = create_channel (h, ccn);
619     ch->peer = GNUNET_PEER_intern (&msg->peer);
620     ch->cadet = h;
621     ch->ccn = ccn;
622     ch->port = port;
623     ch->options = ntohl (msg->opt);
624
625     LOG (GNUNET_ERROR_TYPE_DEBUG, "  created channel %p\n", ch);
626     ctx = port->handler (port->cls, ch, &msg->peer, port->hash, ch->options);
627     if (NULL != ctx)
628       ch->ctx = ctx;
629     LOG (GNUNET_ERROR_TYPE_DEBUG, "User notified\n");
630   }
631   else
632   {
633     struct GNUNET_CADET_LocalChannelDestroyMessage *d_msg;
634     struct GNUNET_MQ_Envelope *env;
635
636     LOG (GNUNET_ERROR_TYPE_DEBUG, "No handler for incoming channels\n");
637     env = GNUNET_MQ_msg (d_msg,
638                          GNUNET_MESSAGE_TYPE_CADET_LOCAL_CHANNEL_DESTROY);
639     d_msg->ccn = msg->ccn;
640     GNUNET_MQ_send (h->mq, env);
641   }
642   return;
643 }
644
645
646 /**
647  * Process the channel destroy notification and free associated resources
648  *
649  * @param h     The cadet handle
650  * @param msg   A message with the details of the channel being destroyed
651  */
652 static void
653 handle_channel_destroy (void *cls,
654                         const struct GNUNET_CADET_LocalChannelDestroyMessage *msg)
655 {
656   struct GNUNET_CADET_Handle *h = cls;
657   struct GNUNET_CADET_Channel *ch;
658   struct GNUNET_CADET_ClientChannelNumber ccn;
659
660   ccn = msg->ccn;
661   LOG (GNUNET_ERROR_TYPE_DEBUG,
662        "Channel %X Destroy from service\n",
663        ntohl (ccn.channel_of_client));
664   ch = retrieve_channel (h,
665                          ccn);
666
667   if (NULL == ch)
668   {
669     LOG (GNUNET_ERROR_TYPE_DEBUG,
670          "channel %X unknown\n",
671          ntohl (ccn.channel_of_client));
672     return;
673   }
674   destroy_channel (ch,
675                    GNUNET_YES);
676 }
677
678
679 /**
680  * Check that message received from CADET service is well-formed.
681  *
682  * @param cls the `struct GNUNET_CADET_Handle`
683  * @param message the message we got
684  * @return #GNUNET_OK if the message is well-formed,
685  *         #GNUNET_SYSERR otherwise
686  */
687 static int
688 check_local_data (void *cls,
689                   const struct GNUNET_CADET_LocalData *message)
690 {
691   struct GNUNET_CADET_Handle *h = cls;
692   struct GNUNET_CADET_Channel *ch;
693   uint16_t size;
694
695   size = ntohs (message->header.size);
696   if (sizeof (*message) + sizeof (struct GNUNET_MessageHeader) > size)
697   {
698     GNUNET_break_op (0);
699     return GNUNET_SYSERR;
700   }
701
702   ch = retrieve_channel (h,
703                          message->ccn);
704   if (NULL == ch)
705   {
706     GNUNET_break_op (0);
707     return GNUNET_SYSERR;
708   }
709
710   return GNUNET_OK;
711 }
712
713
714 /**
715  * Process the incoming data packets, call appropriate handlers.
716  *
717  * @param h       The cadet handle
718  * @param message A message encapsulating the data
719  */
720 static void
721 handle_local_data (void *cls,
722                    const struct GNUNET_CADET_LocalData *message)
723 {
724   struct GNUNET_CADET_Handle *h = cls;
725   const struct GNUNET_MessageHeader *payload;
726   const struct GNUNET_CADET_MessageHandler *handler;
727   struct GNUNET_CADET_Channel *ch;
728   unsigned int i;
729   uint16_t type;
730
731   LOG (GNUNET_ERROR_TYPE_DEBUG,
732        "Got a data message!\n");
733   ch = retrieve_channel (h,
734                          message->ccn);
735   GNUNET_assert (NULL != ch);
736
737   payload = (struct GNUNET_MessageHeader *) &message[1];
738   LOG (GNUNET_ERROR_TYPE_DEBUG, "  %s data on channel %s [%X]\n",
739        GC_f2s (ntohl (ch->ccn.channel_of_client) >=
740                GNUNET_CADET_LOCAL_CHANNEL_ID_CLI),
741        GNUNET_i2s (GNUNET_PEER_resolve2 (ch->peer)),
742        ntohl (message->ccn.channel_of_client));
743
744   type = ntohs (payload->type);
745   LOG (GNUNET_ERROR_TYPE_DEBUG, "  payload type %s\n", GC_m2s (type));
746   for (i = 0; i < h->n_handlers; i++)
747   {
748     handler = &h->message_handlers[i];
749     LOG (GNUNET_ERROR_TYPE_DEBUG, "    checking handler for type %u\n",
750          handler->type);
751     if (handler->type == type)
752     {
753       if (GNUNET_OK !=
754           handler->callback (h->cls, ch, &ch->ctx, payload))
755       {
756         LOG (GNUNET_ERROR_TYPE_DEBUG, "callback caused disconnection\n");
757         GNUNET_CADET_channel_destroy (ch);
758         break;
759       }
760       else
761       {
762         LOG (GNUNET_ERROR_TYPE_DEBUG,
763              "callback completed successfully\n");
764         break;
765       }
766     }
767   }
768 }
769
770
771 /**
772  * Process a local ACK message, enabling the client to send
773  * more data to the service.
774  *
775  * @param h Cadet handle.
776  * @param message Message itself.
777  */
778 static void
779 handle_local_ack (void *cls,
780                   const struct GNUNET_CADET_LocalAck *message)
781 {
782   struct GNUNET_CADET_Handle *h = cls;
783   struct GNUNET_CADET_Channel *ch;
784   struct GNUNET_CADET_ClientChannelNumber ccn;
785   struct GNUNET_CADET_TransmitHandle *th;
786
787   ccn = message->ccn;
788   ch = retrieve_channel (h, ccn);
789   if (NULL == ch)
790   {
791     LOG (GNUNET_ERROR_TYPE_DEBUG,
792          "ACK on unknown channel %X\n",
793          ntohl (ccn.channel_of_client));
794     return;
795   }
796   LOG (GNUNET_ERROR_TYPE_DEBUG,
797        "Got an ACK on channel %X!\n",
798        ntohl (ch->ccn.channel_of_client));
799   ch->allow_send++;
800   LOG (GNUNET_ERROR_TYPE_DEBUG,
801        "Checking for pending data\n");
802   for (th = h->th_head; NULL != th; th = th->next)
803   {
804     if ( (th->channel == ch) &&
805          (NULL == th->request_data_task) )
806     {
807       th->request_data_task
808         = GNUNET_SCHEDULER_add_now (&request_data,
809                                     th);
810       break;
811     }
812   }
813 }
814
815 /**
816  * Reconnect to the service, retransmit all infomation to try to restore the
817  * original state.
818  *
819  * @param h handle to the cadet
820  *
821  * @return #GNUNET_YES in case of sucess, #GNUNET_NO otherwise (service down...)
822  */
823 static void
824 reconnect (struct GNUNET_CADET_Handle *h);
825
826
827 /**
828  * Reconnect callback: tries to reconnect again after a failer previous
829  * reconnection.
830  *
831  * @param cls closure (cadet handle)
832  */
833 static void
834 reconnect_cbk (void *cls);
835
836
837 /**
838  * Generic error handler, called with the appropriate error code and
839  * the same closure specified at the creation of the message queue.
840  * Not every message queue implementation supports an error handler.
841  *
842  * @param cls closure, a `struct GNUNET_CORE_Handle *`
843  * @param error error code
844  */
845 static void
846 handle_mq_error (void *cls,
847                  enum GNUNET_MQ_Error error)
848 {
849   struct GNUNET_CADET_Handle *h = cls;
850
851   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MQ ERROR: %u\n", error);
852   GNUNET_MQ_destroy (h->mq);
853   h->mq = NULL;
854   reconnect (h);
855 }
856
857
858 /*
859  * Process a local reply about info on all channels, pass info to the user.
860  *
861  * @param h Cadet handle.
862  * @param message Message itself.
863  */
864 // static void
865 // process_get_channels (struct GNUNET_CADET_Handle *h,
866 //                      const struct GNUNET_MessageHeader *message)
867 // {
868 //   struct GNUNET_CADET_LocalInfo *msg;
869 //
870 //   GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Get Channels messasge received\n");
871 //
872 //   if (NULL == h->channels_cb)
873 //   {
874 //     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "  ignored\n");
875 //     return;
876 //   }
877 //
878 //   msg = (struct GNUNET_CADET_LocalInfo *) message;
879 //   if (ntohs (message->size) !=
880 //       (sizeof (struct GNUNET_CADET_LocalInfo) +
881 //        sizeof (struct GNUNET_PeerIdentity)))
882 //   {
883 //     GNUNET_break_op (0);
884 //     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
885 //                 "Get channels message: size %hu - expected %u\n",
886 //                 ntohs (message->size),
887 //                 sizeof (struct GNUNET_CADET_LocalInfo));
888 //     return;
889 //   }
890 //   h->channels_cb (h->channels_cls,
891 //                   ntohl (msg->channel_id),
892 //                   &msg->owner,
893 //                   &msg->destination);
894 // }
895
896
897
898 /*
899  * Process a local monitor_channel reply, pass info to the user.
900  *
901  * @param h Cadet handle.
902  * @param message Message itself.
903  */
904 // static void
905 // process_show_channel (struct GNUNET_CADET_Handle *h,
906 //                      const struct GNUNET_MessageHeader *message)
907 // {
908 //   struct GNUNET_CADET_LocalInfo *msg;
909 //   size_t esize;
910 //
911 //   GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Show Channel messasge received\n");
912 //
913 //   if (NULL == h->channel_cb)
914 //   {
915 //     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "  ignored\n");
916 //     return;
917 //   }
918 //
919 //   /* Verify message sanity */
920 //   msg = (struct GNUNET_CADET_LocalInfo *) message;
921 //   esize = sizeof (struct GNUNET_CADET_LocalInfo);
922 //   if (ntohs (message->size) != esize)
923 //   {
924 //     GNUNET_break_op (0);
925 //     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
926 //                 "Show channel message: size %hu - expected %u\n",
927 //                 ntohs (message->size),
928 //                 esize);
929 //
930 //     h->channel_cb (h->channel_cls, NULL, NULL);
931 //     h->channel_cb = NULL;
932 //     h->channel_cls = NULL;
933 //
934 //     return;
935 //   }
936 //
937 //   h->channel_cb (h->channel_cls,
938 //                  &msg->destination,
939 //                  &msg->owner);
940 // }
941
942
943
944 /**
945  * Check that message received from CADET service is well-formed.
946  *
947  * @param cls the `struct GNUNET_CADET_Handle`
948  * @param message the message we got
949  * @return #GNUNET_OK if the message is well-formed,
950  *         #GNUNET_SYSERR otherwise
951  */
952 static int
953 check_get_peers (void *cls,
954                  const struct GNUNET_CADET_LocalInfoPeer *message)
955 {
956   struct GNUNET_CADET_Handle *h = cls;
957   uint16_t size;
958
959   if (NULL == h->info_cb.peers_cb)
960   {
961     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
962                 "  no handler for peesr monitor message!\n");
963     return GNUNET_SYSERR;
964   }
965
966   size = ntohs (message->header.size);
967   if (sizeof (struct GNUNET_CADET_LocalInfoPeer) > size)
968   {
969     h->info_cb.peers_cb (h->info_cls, NULL, -1, 0, 0);
970     h->info_cb.peers_cb = NULL;
971     h->info_cls = NULL;
972     return GNUNET_SYSERR;
973   }
974
975   return GNUNET_OK;
976 }
977
978
979 /**
980  * Process a local reply about info on all tunnels, pass info to the user.
981  *
982  * @param cls Closure (Cadet handle).
983  * @param msg Message itself.
984  */
985 static void
986 handle_get_peers (void *cls,
987                   const struct GNUNET_CADET_LocalInfoPeer *msg)
988 {
989   struct GNUNET_CADET_Handle *h = cls;
990   h->info_cb.peers_cb (h->info_cls, &msg->destination,
991                        (int) ntohs (msg->tunnel),
992                        (unsigned int ) ntohs (msg->paths),
993                        0);
994 }
995
996
997 /**
998  * Check that message received from CADET service is well-formed.
999  *
1000  * @param cls the `struct GNUNET_CADET_Handle`
1001  * @param message the message we got
1002  * @return #GNUNET_OK if the message is well-formed,
1003  *         #GNUNET_SYSERR otherwise
1004  */
1005 static int
1006 check_get_peer (void *cls,
1007                 const struct GNUNET_CADET_LocalInfoPeer *message)
1008 {
1009   struct GNUNET_CADET_Handle *h = cls;
1010   const size_t msize = sizeof (struct GNUNET_CADET_LocalInfoPeer);
1011   struct GNUNET_PeerIdentity *paths_array;
1012   size_t esize;
1013   unsigned int epaths;
1014   unsigned int paths;
1015   unsigned int peers;
1016
1017   if (NULL == h->info_cb.peer_cb)
1018   {
1019     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1020                 "  no handler for peer monitor message!\n");
1021     goto clean_cls;
1022   }
1023
1024   /* Verify message sanity */
1025   esize = ntohs (message->header.size);
1026   if (esize < msize)
1027   {
1028     GNUNET_break_op (0);
1029     h->info_cb.peer_cb (h->info_cls, NULL, 0, 0, 0, NULL);
1030     goto clean_cls;
1031   }
1032   if (0 != ((esize - msize) % sizeof (struct GNUNET_PeerIdentity)))
1033   {
1034     GNUNET_break_op (0);
1035     h->info_cb.peer_cb (h->info_cls, NULL, 0, 0, 0, NULL);
1036     goto clean_cls;
1037
1038   }
1039   peers = (esize - msize) / sizeof (struct GNUNET_PeerIdentity);
1040   epaths = (unsigned int) ntohs (message->paths);
1041   paths_array = (struct GNUNET_PeerIdentity *) &message[1];
1042   paths = 0;
1043   for (int i = 0; i < peers; i++)
1044   {
1045     if (0 == memcmp (&paths_array[i], &message->destination,
1046                      sizeof (struct GNUNET_PeerIdentity)))
1047     {
1048       paths++;
1049     }
1050   }
1051   if (paths != epaths)
1052   {
1053     GNUNET_break_op (0);
1054     GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "p:%u, e: %u\n", paths, epaths);
1055     h->info_cb.peer_cb (h->info_cls, NULL, 0, 0, 0, NULL);
1056     goto clean_cls;
1057   }
1058
1059   return GNUNET_OK;
1060
1061 clean_cls:
1062   h->info_cb.peer_cb = NULL;
1063   h->info_cls = NULL;
1064   return GNUNET_SYSERR;
1065 }
1066
1067
1068 /**
1069  * Process a local peer info reply, pass info to the user.
1070  *
1071  * @param cls Closure (Cadet handle).
1072  * @param message Message itself.
1073  */
1074 static void
1075 handle_get_peer (void *cls,
1076                  const struct GNUNET_CADET_LocalInfoPeer *message)
1077 {
1078   struct GNUNET_CADET_Handle *h = cls;
1079   struct GNUNET_PeerIdentity *paths_array;
1080   unsigned int paths;
1081   unsigned int path_length;
1082   int neighbor;
1083   unsigned int peers;
1084
1085   paths = (unsigned int) ntohs (message->paths);
1086   paths_array = (struct GNUNET_PeerIdentity *) &message[1];
1087   peers = (ntohs (message->header.size) - sizeof (*message))
1088           / sizeof (struct GNUNET_PeerIdentity);
1089   path_length = 0;
1090   neighbor = GNUNET_NO;
1091
1092   for (int i = 0; i < peers; i++)
1093   {
1094     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " %s\n", GNUNET_i2s (&paths_array[i]));
1095     path_length++;
1096     if (0 == memcmp (&paths_array[i], &message->destination,
1097                      sizeof (struct GNUNET_PeerIdentity)))
1098     {
1099       if (1 == path_length)
1100         neighbor = GNUNET_YES;
1101       path_length = 0;
1102     }
1103   }
1104
1105   /* Call Callback with tunnel info. */
1106   paths_array = (struct GNUNET_PeerIdentity *) &message[1];
1107   h->info_cb.peer_cb (h->info_cls,
1108                       &message->destination,
1109                       (int) ntohs (message->tunnel),
1110                       neighbor,
1111                       paths,
1112                       paths_array);
1113 }
1114
1115
1116 /**
1117  * Check that message received from CADET service is well-formed.
1118  *
1119  * @param cls the `struct GNUNET_CADET_Handle`
1120  * @param msg the message we got
1121  * @return #GNUNET_OK if the message is well-formed,
1122  *         #GNUNET_SYSERR otherwise
1123  */
1124 static int
1125 check_get_tunnels (void *cls,
1126                    const struct GNUNET_CADET_LocalInfoTunnel *msg)
1127 {
1128   struct GNUNET_CADET_Handle *h = cls;
1129   uint16_t size;
1130
1131   if (NULL == h->info_cb.tunnels_cb)
1132   {
1133     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1134                 "  no handler for tunnels monitor message!\n");
1135     return GNUNET_SYSERR;
1136   }
1137
1138   size = ntohs (msg->header.size);
1139   if (sizeof (struct GNUNET_CADET_LocalInfoTunnel) > size)
1140   {
1141     h->info_cb.tunnels_cb (h->info_cls, NULL, 0, 0, 0, 0);
1142     h->info_cb.tunnels_cb = NULL;
1143     h->info_cls = NULL;
1144     return GNUNET_SYSERR;
1145   }
1146   return GNUNET_OK;
1147 }
1148
1149
1150 /**
1151  * Process a local reply about info on all tunnels, pass info to the user.
1152  *
1153  * @param cls Closure (Cadet handle).
1154  * @param message Message itself.
1155  */
1156 static void
1157 handle_get_tunnels (void *cls,
1158                     const struct GNUNET_CADET_LocalInfoTunnel *msg)
1159 {
1160   struct GNUNET_CADET_Handle *h = cls;
1161
1162   h->info_cb.tunnels_cb (h->info_cls,
1163                          &msg->destination,
1164                          ntohl (msg->channels),
1165                          ntohl (msg->connections),
1166                          ntohs (msg->estate),
1167                          ntohs (msg->cstate));
1168
1169 }
1170
1171
1172 /**
1173  * Check that message received from CADET service is well-formed.
1174  *
1175  * @param cls the `struct GNUNET_CADET_Handle`
1176  * @param msg the message we got
1177  * @return #GNUNET_OK if the message is well-formed,
1178  *         #GNUNET_SYSERR otherwise
1179  */
1180 static int
1181 check_get_tunnel (void *cls,
1182                   const struct GNUNET_CADET_LocalInfoTunnel *msg)
1183 {
1184   struct GNUNET_CADET_Handle *h = cls;
1185   unsigned int ch_n;
1186   unsigned int c_n;
1187   size_t esize;
1188   size_t msize;
1189
1190   if (NULL == h->info_cb.tunnel_cb)
1191   {
1192     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1193                 "  no handler for tunnel monitor message!\n");
1194     goto clean_cls;
1195   }
1196
1197   /* Verify message sanity */
1198   msize = ntohs (msg->header.size);
1199   esize = sizeof (struct GNUNET_CADET_LocalInfoTunnel);
1200   if (esize > msize)
1201   {
1202     GNUNET_break_op (0);
1203     h->info_cb.tunnel_cb (h->info_cls,
1204                           NULL, 0, 0, NULL, NULL, 0, 0);
1205     goto clean_cls;
1206   }
1207   ch_n = ntohl (msg->channels);
1208   c_n = ntohl (msg->connections);
1209   esize += ch_n * sizeof (struct GNUNET_CADET_ChannelTunnelNumber);
1210   esize += c_n * sizeof (struct GNUNET_CADET_ConnectionTunnelIdentifier);
1211   if (msize != esize)
1212   {
1213     GNUNET_break_op (0);
1214     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1215                 "m:%u, e: %u (%u ch, %u conn)\n",
1216                 (unsigned int) msize,
1217                 (unsigned int) esize,
1218                 ch_n,
1219                 c_n);
1220     h->info_cb.tunnel_cb (h->info_cls,
1221                           NULL, 0, 0, NULL, NULL, 0, 0);
1222     goto clean_cls;
1223   }
1224
1225   return GNUNET_OK;
1226
1227 clean_cls:
1228   h->info_cb.tunnel_cb = NULL;
1229   h->info_cls = NULL;
1230   return GNUNET_SYSERR;
1231 }
1232
1233
1234 /**
1235  * Process a local tunnel info reply, pass info to the user.
1236  *
1237  * @param cls Closure (Cadet handle).
1238  * @param msg Message itself.
1239  */
1240 static void
1241 handle_get_tunnel (void *cls,
1242                    const struct GNUNET_CADET_LocalInfoTunnel *msg)
1243 {
1244   struct GNUNET_CADET_Handle *h = cls;
1245   unsigned int ch_n;
1246   unsigned int c_n;
1247   const struct GNUNET_CADET_ConnectionTunnelIdentifier *conns;
1248   const struct GNUNET_CADET_ChannelTunnelNumber *chns;
1249
1250   ch_n = ntohl (msg->channels);
1251   c_n = ntohl (msg->connections);
1252
1253   /* Call Callback with tunnel info. */
1254   conns = (const struct GNUNET_CADET_ConnectionTunnelIdentifier *) &msg[1];
1255   chns = (const struct GNUNET_CADET_ChannelTunnelNumber *) &conns[c_n];
1256   h->info_cb.tunnel_cb (h->info_cls,
1257                         &msg->destination,
1258                         ch_n,
1259                         c_n,
1260                         chns,
1261                         conns,
1262                         ntohs (msg->estate),
1263                         ntohs (msg->cstate));
1264 }
1265
1266
1267
1268 /**
1269  * Reconnect to the service, retransmit all infomation to try to restore the
1270  * original state.
1271  *
1272  * @param h handle to the cadet
1273  *
1274  * @return GNUNET_YES in case of sucess, GNUNET_NO otherwise (service down...)
1275  */
1276 static int
1277 do_reconnect (struct GNUNET_CADET_Handle *h)
1278 {
1279   struct GNUNET_MQ_MessageHandler handlers[] = {
1280     GNUNET_MQ_hd_fixed_size (channel_created,
1281                              GNUNET_MESSAGE_TYPE_CADET_LOCAL_CHANNEL_CREATE,
1282                              struct GNUNET_CADET_LocalChannelCreateMessage,
1283                              h),
1284     GNUNET_MQ_hd_fixed_size (channel_destroy,
1285                              GNUNET_MESSAGE_TYPE_CADET_LOCAL_CHANNEL_DESTROY,
1286                              struct GNUNET_CADET_LocalChannelDestroyMessage,
1287                              h),
1288     GNUNET_MQ_hd_var_size (local_data,
1289                            GNUNET_MESSAGE_TYPE_CADET_LOCAL_DATA,
1290                            struct GNUNET_CADET_LocalData,
1291                            h),
1292     GNUNET_MQ_hd_fixed_size (local_ack,
1293                              GNUNET_MESSAGE_TYPE_CADET_LOCAL_ACK,
1294                              struct GNUNET_CADET_LocalAck,
1295                              h),
1296     GNUNET_MQ_hd_var_size (get_peers,
1297                            GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEERS,
1298                            struct GNUNET_CADET_LocalInfoPeer,
1299                            h),
1300     GNUNET_MQ_hd_var_size (get_peer,
1301                            GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEER,
1302                            struct GNUNET_CADET_LocalInfoPeer,
1303                            h),
1304     GNUNET_MQ_hd_var_size (get_tunnels,
1305                            GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNELS,
1306                            struct GNUNET_CADET_LocalInfoTunnel,
1307                            h),
1308     GNUNET_MQ_hd_var_size (get_tunnel,
1309                            GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNEL,
1310                            struct GNUNET_CADET_LocalInfoTunnel,
1311                            h),
1312   // FIXME
1313 //   GNUNET_MQ_hd_fixed_Y       size (channel_destroyed,
1314 //                            GNUNET_MESSAGE_TYPE_CADET_CHANNEL_OPEN_NACK_DEPRECATED,
1315 //                            struct GNUNET_CADET_ChannelDestroyMessage);
1316     GNUNET_MQ_handler_end ()
1317   };
1318
1319   LOG (GNUNET_ERROR_TYPE_DEBUG, "Connecting to CADET\n");
1320
1321   GNUNET_assert (NULL == h->mq);
1322   h->mq = GNUNET_CLIENT_connect (h->cfg,
1323                                  "cadet",
1324                                  handlers,
1325                                  &handle_mq_error,
1326                                  h);
1327   if (NULL == h->mq)
1328   {
1329     reconnect (h);
1330     return GNUNET_NO;
1331   }
1332   else
1333   {
1334     h->reconnect_time = GNUNET_TIME_UNIT_MILLISECONDS;
1335   }
1336   return GNUNET_YES;
1337 }
1338
1339 /**
1340  * Reconnect callback: tries to reconnect again after a failer previous
1341  * reconnecttion
1342  *
1343  * @param cls closure (cadet handle)
1344  */
1345 static void
1346 reconnect_cbk (void *cls)
1347 {
1348   struct GNUNET_CADET_Handle *h = cls;
1349
1350   h->reconnect_task = NULL;
1351   do_reconnect (h);
1352 }
1353
1354
1355 /**
1356  * Reconnect to the service, retransmit all infomation to try to restore the
1357  * original state.
1358  *
1359  * @param h handle to the cadet
1360  *
1361  * @return #GNUNET_YES in case of sucess, #GNUNET_NO otherwise (service down...)
1362  */
1363 static void
1364 reconnect (struct GNUNET_CADET_Handle *h)
1365 {
1366   struct GNUNET_CADET_Channel *ch;
1367
1368   LOG (GNUNET_ERROR_TYPE_DEBUG,
1369        "Requested RECONNECT, destroying all channels\n");
1370   for (ch = h->channels_head; NULL != ch; ch = h->channels_head)
1371     destroy_channel (ch, GNUNET_YES);
1372   if (NULL == h->reconnect_task)
1373     h->reconnect_task = GNUNET_SCHEDULER_add_delayed (h->reconnect_time,
1374                                                       &reconnect_cbk, h);
1375 }
1376
1377
1378 /******************************************************************************/
1379 /**********************      API CALL DEFINITIONS     *************************/
1380 /******************************************************************************/
1381
1382 struct GNUNET_CADET_Handle *
1383 GNUNET_CADET_connect (const struct GNUNET_CONFIGURATION_Handle *cfg,
1384                       void *cls,
1385                       GNUNET_CADET_ChannelEndHandler cleaner,
1386                       const struct GNUNET_CADET_MessageHandler *handlers)
1387 {
1388   struct GNUNET_CADET_Handle *h;
1389
1390   LOG (GNUNET_ERROR_TYPE_DEBUG, "GNUNET_CADET_connect()\n");
1391   h = GNUNET_new (struct GNUNET_CADET_Handle);
1392   LOG (GNUNET_ERROR_TYPE_DEBUG, " addr %p\n", h);
1393   h->cfg = cfg;
1394   h->cleaner = cleaner;
1395   h->ports = GNUNET_CONTAINER_multihashmap_create (4, GNUNET_YES);
1396   do_reconnect (h);
1397   if (h->mq == NULL)
1398   {
1399     GNUNET_break (0);
1400     GNUNET_CADET_disconnect (h);
1401     return NULL;
1402   }
1403   h->cls = cls;
1404   h->message_handlers = handlers;
1405   h->next_ccn.channel_of_client = htonl (GNUNET_CADET_LOCAL_CHANNEL_ID_CLI);
1406   h->reconnect_time = GNUNET_TIME_UNIT_MILLISECONDS;
1407   h->reconnect_task = NULL;
1408
1409   /* count handlers */
1410   for (h->n_handlers = 0;
1411        handlers && handlers[h->n_handlers].type;
1412        h->n_handlers++) ;
1413   LOG (GNUNET_ERROR_TYPE_DEBUG, "GNUNET_CADET_connect() END\n");
1414   return h;
1415 }
1416
1417
1418 /**
1419  * Disconnect from the cadet service. All channels will be destroyed. All channel
1420  * disconnect callbacks will be called on any still connected peers, notifying
1421  * about their disconnection. The registered inbound channel cleaner will be
1422  * called should any inbound channels still exist.
1423  *
1424  * @param handle connection to cadet to disconnect
1425  */
1426 void
1427 GNUNET_CADET_disconnect (struct GNUNET_CADET_Handle *handle)
1428 {
1429   struct GNUNET_CADET_Channel *ch;
1430   struct GNUNET_CADET_Channel *aux;
1431   struct GNUNET_CADET_TransmitHandle *th;
1432
1433   LOG (GNUNET_ERROR_TYPE_DEBUG,
1434        "CADET DISCONNECT\n");
1435   ch = handle->channels_head;
1436   while (NULL != ch)
1437   {
1438     aux = ch->next;
1439     if (ntohl (ch->ccn.channel_of_client) >= GNUNET_CADET_LOCAL_CHANNEL_ID_CLI)
1440     {
1441       GNUNET_break (0);
1442       LOG (GNUNET_ERROR_TYPE_DEBUG,
1443            "channel %X not destroyed\n",
1444            ntohl (ch->ccn.channel_of_client));
1445     }
1446     destroy_channel (ch,
1447                      GNUNET_YES);
1448     ch = aux;
1449   }
1450   while (NULL != (th = handle->th_head))
1451   {
1452     struct GNUNET_MessageHeader *msg;
1453
1454     /* Make sure it is an allowed packet (everything else should have been
1455      * already canceled).
1456      */
1457     GNUNET_break (GNUNET_NO == th_is_payload (th));
1458     msg = (struct GNUNET_MessageHeader *) &th[1];
1459     switch (ntohs(msg->type))
1460     {
1461       case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_OPEN:
1462       case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DESTROY:
1463       case GNUNET_MESSAGE_TYPE_CADET_LOCAL_PORT_OPEN:
1464       case GNUNET_MESSAGE_TYPE_CADET_LOCAL_PORT_CLOSE:
1465       case GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_CHANNELS:
1466       case GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_CHANNEL:
1467       case GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEER:
1468       case GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEERS:
1469       case GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNEL:
1470       case GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNELS:
1471         break;
1472       default:
1473         GNUNET_break (0);
1474         LOG (GNUNET_ERROR_TYPE_ERROR, "unexpected unsent msg %s\n",
1475              GC_m2s (ntohs(msg->type)));
1476     }
1477
1478     GNUNET_CADET_notify_transmit_ready_cancel (th);
1479   }
1480
1481   if (NULL != handle->mq)
1482   {
1483     GNUNET_MQ_destroy (handle->mq);
1484     handle->mq = NULL;
1485   }
1486   if (NULL != handle->reconnect_task)
1487   {
1488     GNUNET_SCHEDULER_cancel(handle->reconnect_task);
1489     handle->reconnect_task = NULL;
1490   }
1491
1492   GNUNET_CONTAINER_multihashmap_destroy (handle->ports);
1493   handle->ports = NULL;
1494   GNUNET_free (handle);
1495 }
1496
1497
1498 /**
1499  * Open a port to receive incomming channels.
1500  *
1501  * @param h CADET handle.
1502  * @param port Hash representing the port number.
1503  * @param new_channel Function called when an channel is received.
1504  * @param new_channel_cls Closure for @a new_channel.
1505  *
1506  * @return Port handle.
1507  */
1508 struct GNUNET_CADET_Port *
1509 GNUNET_CADET_open_port (struct GNUNET_CADET_Handle *h,
1510                         const struct GNUNET_HashCode *port,
1511                         GNUNET_CADET_InboundChannelNotificationHandler
1512                             new_channel,
1513                         void *new_channel_cls)
1514 {
1515   struct GNUNET_CADET_PortMessage *msg;
1516   struct GNUNET_MQ_Envelope *env;
1517   struct GNUNET_CADET_Port *p;
1518
1519   GNUNET_assert (NULL != new_channel);
1520   p = GNUNET_new (struct GNUNET_CADET_Port);
1521   p->cadet = h;
1522   p->hash = GNUNET_new (struct GNUNET_HashCode);
1523   *p->hash = *port;
1524   p->handler = new_channel;
1525   p->cls = new_channel_cls;
1526   GNUNET_assert (GNUNET_OK ==
1527                  GNUNET_CONTAINER_multihashmap_put (h->ports,
1528                                                     p->hash,
1529                                                     p,
1530                                                     GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1531
1532   env = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_CADET_LOCAL_PORT_OPEN);
1533   msg->port = *p->hash;
1534   GNUNET_MQ_send (h->mq, env);
1535
1536   return p;
1537 }
1538
1539 /**
1540  * Close a port opened with @a GNUNET_CADET_open_port.
1541  * The @a new_channel callback will no longer be called.
1542  *
1543  * @param p Port handle.
1544  */
1545 void
1546 GNUNET_CADET_close_port (struct GNUNET_CADET_Port *p)
1547 {
1548   struct GNUNET_CADET_PortMessage *msg;
1549   struct GNUNET_MQ_Envelope *env;
1550
1551   env = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_CADET_LOCAL_PORT_CLOSE);
1552
1553   msg->port = *p->hash;
1554   GNUNET_MQ_send (p->cadet->mq, env);
1555   GNUNET_CONTAINER_multihashmap_remove (p->cadet->ports, p->hash, p);
1556   GNUNET_free (p->hash);
1557   GNUNET_free (p);
1558 }
1559
1560
1561 /**
1562  * Create a new channel towards a remote peer.
1563  *
1564  * If the destination port is not open by any peer or the destination peer
1565  * does not accept the channel, #GNUNET_CADET_ChannelEndHandler will be called
1566  * for this channel.
1567  *
1568  * @param h cadet handle
1569  * @param channel_ctx client's channel context to associate with the channel
1570  * @param peer peer identity the channel should go to
1571  * @param port Port hash (port number).
1572  * @param options CadetOption flag field, with all desired option bits set to 1.
1573  * @return handle to the channel
1574  */
1575 struct GNUNET_CADET_Channel *
1576 GNUNET_CADET_channel_create (struct GNUNET_CADET_Handle *h,
1577                             void *channel_ctx,
1578                             const struct GNUNET_PeerIdentity *peer,
1579                             const struct GNUNET_HashCode *port,
1580                             enum GNUNET_CADET_ChannelOption options)
1581 {
1582   struct GNUNET_CADET_LocalChannelCreateMessage *msg;
1583   struct GNUNET_MQ_Envelope *env;
1584   struct GNUNET_CADET_Channel *ch;
1585   struct GNUNET_CADET_ClientChannelNumber ccn;
1586
1587   LOG (GNUNET_ERROR_TYPE_DEBUG,
1588        "Creating new channel to %s:%u\n",
1589        GNUNET_i2s (peer), port);
1590   ccn.channel_of_client = htonl (0);
1591   ch = create_channel (h, ccn);
1592   LOG (GNUNET_ERROR_TYPE_DEBUG, "  at %p\n", ch);
1593   LOG (GNUNET_ERROR_TYPE_DEBUG, "  number %X\n",
1594        ntohl (ch->ccn.channel_of_client));
1595   ch->ctx = channel_ctx;
1596   ch->peer = GNUNET_PEER_intern (peer);
1597
1598   env = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_CADET_LOCAL_CHANNEL_CREATE);
1599   msg->ccn = ch->ccn;
1600   msg->port = *port;
1601   msg->peer = *peer;
1602   msg->opt = htonl (options);
1603   GNUNET_MQ_send (h->mq,
1604                   env);
1605   return ch;
1606 }
1607
1608
1609 void
1610 GNUNET_CADET_channel_destroy (struct GNUNET_CADET_Channel *channel)
1611 {
1612   struct GNUNET_CADET_Handle *h;
1613   struct GNUNET_CADET_LocalChannelDestroyMessage *msg;
1614   struct GNUNET_MQ_Envelope *env;
1615   struct GNUNET_CADET_TransmitHandle *th;
1616   struct GNUNET_CADET_TransmitHandle *next;
1617
1618   LOG (GNUNET_ERROR_TYPE_DEBUG, "Destroying channel\n");
1619   h = channel->cadet;
1620   for  (th = h->th_head; th != NULL; th = next)
1621   {
1622     next = th->next;
1623     if (th->channel == channel)
1624     {
1625       GNUNET_break (0);
1626       if (GNUNET_YES == th_is_payload (th))
1627       {
1628         /* applications should cancel before destroying channel */
1629         LOG (GNUNET_ERROR_TYPE_WARNING,
1630              "Channel destroyed without cancelling transmission requests\n");
1631         th->notify (th->notify_cls, 0, NULL);
1632       }
1633       else
1634       {
1635         LOG (GNUNET_ERROR_TYPE_WARNING, "no meta-traffic should be queued\n");
1636       }
1637       GNUNET_CONTAINER_DLL_remove (h->th_head,
1638                                    h->th_tail,
1639                                    th);
1640       GNUNET_CADET_notify_transmit_ready_cancel (th);
1641     }
1642   }
1643
1644   env = GNUNET_MQ_msg (msg,
1645                        GNUNET_MESSAGE_TYPE_CADET_LOCAL_CHANNEL_DESTROY);
1646   msg->ccn = channel->ccn;
1647   GNUNET_MQ_send (h->mq,
1648                   env);
1649
1650   destroy_channel (channel,
1651                    GNUNET_YES);
1652 }
1653
1654
1655 /**
1656  * Get information about a channel.
1657  *
1658  * @param channel Channel handle.
1659  * @param option Query (GNUNET_CADET_OPTION_*).
1660  * @param ... dependant on option, currently not used
1661  *
1662  * @return Union with an answer to the query.
1663  */
1664 const union GNUNET_CADET_ChannelInfo *
1665 GNUNET_CADET_channel_get_info (struct GNUNET_CADET_Channel *channel,
1666                               enum GNUNET_CADET_ChannelOption option, ...)
1667 {
1668   static int bool_flag;
1669   const union GNUNET_CADET_ChannelInfo *ret;
1670
1671   switch (option)
1672   {
1673     case GNUNET_CADET_OPTION_NOBUFFER:
1674     case GNUNET_CADET_OPTION_RELIABLE:
1675     case GNUNET_CADET_OPTION_OUT_OF_ORDER:
1676       if (0 != (option & channel->options))
1677         bool_flag = GNUNET_YES;
1678       else
1679         bool_flag = GNUNET_NO;
1680       ret = (const union GNUNET_CADET_ChannelInfo *) &bool_flag;
1681       break;
1682     case GNUNET_CADET_OPTION_PEER:
1683       ret = (const union GNUNET_CADET_ChannelInfo *) GNUNET_PEER_resolve2 (channel->peer);
1684       break;
1685     default:
1686       GNUNET_break (0);
1687       return NULL;
1688   }
1689
1690   return ret;
1691 }
1692
1693
1694 struct GNUNET_CADET_TransmitHandle *
1695 GNUNET_CADET_notify_transmit_ready (struct GNUNET_CADET_Channel *channel,
1696                                     int cork,
1697                                     struct GNUNET_TIME_Relative maxdelay,
1698                                     size_t notify_size,
1699                                     GNUNET_CONNECTION_TransmitReadyNotify notify,
1700                                     void *notify_cls)
1701 {
1702   struct GNUNET_CADET_TransmitHandle *th;
1703
1704   GNUNET_assert (NULL != channel);
1705   GNUNET_assert (GNUNET_CONSTANTS_MAX_CADET_MESSAGE_SIZE >= notify_size);
1706   LOG (GNUNET_ERROR_TYPE_DEBUG, "CADET NOTIFY TRANSMIT READY\n");
1707   LOG (GNUNET_ERROR_TYPE_DEBUG, "    on channel %X\n", channel->ccn);
1708   LOG (GNUNET_ERROR_TYPE_DEBUG, "    allow_send %d\n", channel->allow_send);
1709   if (ntohl (channel->ccn.channel_of_client) >=
1710       GNUNET_CADET_LOCAL_CHANNEL_ID_CLI)
1711     LOG (GNUNET_ERROR_TYPE_DEBUG, "    to origin\n");
1712   else
1713     LOG (GNUNET_ERROR_TYPE_DEBUG, "    to destination\n");
1714   LOG (GNUNET_ERROR_TYPE_DEBUG, "    payload size %u\n", notify_size);
1715   GNUNET_assert (NULL != notify);
1716   if (GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us != maxdelay.rel_value_us)
1717   {
1718     LOG (GNUNET_ERROR_TYPE_WARNING,
1719          "CADET transmit ready timeout is deprected (has no effect)\n");
1720   }
1721
1722   th = GNUNET_new (struct GNUNET_CADET_TransmitHandle);
1723   th->channel = channel;
1724   th->size = notify_size;
1725   LOG (GNUNET_ERROR_TYPE_DEBUG, "    total size %u\n", th->size);
1726   th->notify = notify;
1727   th->notify_cls = notify_cls;
1728   if (0 != channel->allow_send)
1729     th->request_data_task = GNUNET_SCHEDULER_add_now (&request_data, th);
1730   else
1731     add_to_queue (channel->cadet, th);
1732
1733   LOG (GNUNET_ERROR_TYPE_DEBUG, "CADET NOTIFY TRANSMIT READY END\n");
1734   return th;
1735 }
1736
1737
1738 void
1739 GNUNET_CADET_notify_transmit_ready_cancel (struct GNUNET_CADET_TransmitHandle *th)
1740 {
1741   if (NULL != th->request_data_task)
1742   {
1743     GNUNET_SCHEDULER_cancel (th->request_data_task);
1744   }
1745   th->request_data_task = NULL;
1746
1747   remove_from_queue (th);
1748   GNUNET_free (th);
1749 }
1750
1751
1752 void
1753 GNUNET_CADET_receive_done (struct GNUNET_CADET_Channel *channel)
1754 {
1755   send_ack (channel);
1756 }
1757
1758
1759 static void
1760 send_info_request (struct GNUNET_CADET_Handle *h, uint16_t type)
1761 {
1762   struct GNUNET_MessageHeader *msg;
1763   struct GNUNET_MQ_Envelope *env;
1764
1765   LOG (GNUNET_ERROR_TYPE_DEBUG,
1766        " Sending %s monitor message to service\n",
1767        GC_m2s(type));
1768
1769   env = GNUNET_MQ_msg (msg, type);
1770   GNUNET_MQ_send (h->mq, env);
1771 }
1772
1773
1774 /**
1775  * Request a debug dump on the service's STDERR.
1776  *
1777  * WARNING: unstable API, likely to change in the future!
1778  *
1779  * @param h cadet handle
1780  */
1781 void
1782 GNUNET_CADET_request_dump (struct GNUNET_CADET_Handle *h)
1783 {
1784   LOG (GNUNET_ERROR_TYPE_DEBUG, "requesting dump\n");
1785   send_info_request (h, GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_DUMP);
1786 }
1787
1788
1789 /**
1790  * Request information about peers known to the running cadet service.
1791  * The callback will be called for every peer known to the service.
1792  * Only one info request (of any kind) can be active at once.
1793  *
1794  *
1795  * WARNING: unstable API, likely to change in the future!
1796  *
1797  * @param h Handle to the cadet peer.
1798  * @param callback Function to call with the requested data.
1799  * @param callback_cls Closure for @c callback.
1800  *
1801  * @return #GNUNET_OK / #GNUNET_SYSERR
1802  */
1803 int
1804 GNUNET_CADET_get_peers (struct GNUNET_CADET_Handle *h,
1805                        GNUNET_CADET_PeersCB callback,
1806                        void *callback_cls)
1807 {
1808   if (NULL != h->info_cb.peers_cb)
1809   {
1810     GNUNET_break (0);
1811     return GNUNET_SYSERR;
1812   }
1813   send_info_request (h, GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEERS);
1814   h->info_cb.peers_cb = callback;
1815   h->info_cls = callback_cls;
1816   return GNUNET_OK;
1817 }
1818
1819
1820 /**
1821  * Cancel a peer info request. The callback will not be called (anymore).
1822  *
1823  * WARNING: unstable API, likely to change in the future!
1824  *
1825  * @param h Cadet handle.
1826  *
1827  * @return Closure given to GNUNET_CADET_get_peers.
1828  */
1829 void *
1830 GNUNET_CADET_get_peers_cancel (struct GNUNET_CADET_Handle *h)
1831 {
1832   void *cls;
1833
1834   cls = h->info_cls;
1835   h->info_cb.peers_cb = NULL;
1836   h->info_cls = NULL;
1837   return cls;
1838 }
1839
1840
1841 /**
1842  * Request information about a peer known to the running cadet peer.
1843  * The callback will be called for the tunnel once.
1844  * Only one info request (of any kind) can be active at once.
1845  *
1846  * WARNING: unstable API, likely to change in the future!
1847  *
1848  * @param h Handle to the cadet peer.
1849  * @param id Peer whose tunnel to examine.
1850  * @param callback Function to call with the requested data.
1851  * @param callback_cls Closure for @c callback.
1852  *
1853  * @return #GNUNET_OK / #GNUNET_SYSERR
1854  */
1855 int
1856 GNUNET_CADET_get_peer (struct GNUNET_CADET_Handle *h,
1857                        const struct GNUNET_PeerIdentity *id,
1858                        GNUNET_CADET_PeerCB callback,
1859                        void *callback_cls)
1860 {
1861   struct GNUNET_CADET_LocalInfo *msg;
1862   struct GNUNET_MQ_Envelope *env;
1863
1864   if (NULL != h->info_cb.peer_cb)
1865   {
1866     GNUNET_break (0);
1867     return GNUNET_SYSERR;
1868   }
1869
1870   env = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEER);
1871   msg->peer = *id;
1872   GNUNET_MQ_send (h->mq, env);
1873
1874   h->info_cb.peer_cb = callback;
1875   h->info_cls = callback_cls;
1876   return GNUNET_OK;
1877 }
1878
1879
1880 /**
1881  * Request information about tunnels of the running cadet peer.
1882  * The callback will be called for every tunnel of the service.
1883  * Only one info request (of any kind) can be active at once.
1884  *
1885  * WARNING: unstable API, likely to change in the future!
1886  *
1887  * @param h Handle to the cadet peer.
1888  * @param callback Function to call with the requested data.
1889  * @param callback_cls Closure for @c callback.
1890  *
1891  * @return #GNUNET_OK / #GNUNET_SYSERR
1892  */
1893 int
1894 GNUNET_CADET_get_tunnels (struct GNUNET_CADET_Handle *h,
1895                          GNUNET_CADET_TunnelsCB callback,
1896                          void *callback_cls)
1897 {
1898   if (NULL != h->info_cb.tunnels_cb)
1899   {
1900     GNUNET_break (0);
1901     return GNUNET_SYSERR;
1902   }
1903   send_info_request (h, GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNELS);
1904   h->info_cb.tunnels_cb = callback;
1905   h->info_cls = callback_cls;
1906   return GNUNET_OK;
1907 }
1908
1909
1910 /**
1911  * Cancel a monitor request. The monitor callback will not be called.
1912  *
1913  * @param h Cadet handle.
1914  *
1915  * @return Closure given to GNUNET_CADET_get_tunnels.
1916  */
1917 void *
1918 GNUNET_CADET_get_tunnels_cancel (struct GNUNET_CADET_Handle *h)
1919 {
1920   void *cls;
1921
1922   h->info_cb.tunnels_cb = NULL;
1923   cls = h->info_cls;
1924   h->info_cls = NULL;
1925
1926   return cls;
1927 }
1928
1929
1930
1931 /**
1932  * Request information about a tunnel of the running cadet peer.
1933  * The callback will be called for the tunnel once.
1934  * Only one info request (of any kind) can be active at once.
1935  *
1936  * WARNING: unstable API, likely to change in the future!
1937  *
1938  * @param h Handle to the cadet peer.
1939  * @param id Peer whose tunnel to examine.
1940  * @param callback Function to call with the requested data.
1941  * @param callback_cls Closure for @c callback.
1942  *
1943  * @return #GNUNET_OK / #GNUNET_SYSERR
1944  */
1945 int
1946 GNUNET_CADET_get_tunnel (struct GNUNET_CADET_Handle *h,
1947                         const struct GNUNET_PeerIdentity *id,
1948                         GNUNET_CADET_TunnelCB callback,
1949                         void *callback_cls)
1950 {
1951   struct GNUNET_CADET_LocalInfo *msg;
1952   struct GNUNET_MQ_Envelope *env;
1953
1954   if (NULL != h->info_cb.tunnel_cb)
1955   {
1956     GNUNET_break (0);
1957     return GNUNET_SYSERR;
1958   }
1959
1960   env = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNEL);
1961   msg->peer = *id;
1962   GNUNET_MQ_send (h->mq, env);
1963
1964   h->info_cb.tunnel_cb = callback;
1965   h->info_cls = callback_cls;
1966   return GNUNET_OK;
1967 }
1968
1969
1970 /**
1971  * Request information about a specific channel of the running cadet peer.
1972  *
1973  * WARNING: unstable API, likely to change in the future!
1974  * FIXME Add destination option.
1975  *
1976  * @param h Handle to the cadet peer.
1977  * @param initiator ID of the owner of the channel.
1978  * @param channel_number Channel number.
1979  * @param callback Function to call with the requested data.
1980  * @param callback_cls Closure for @c callback.
1981  *
1982  * @return #GNUNET_OK / #GNUNET_SYSERR
1983  */
1984 int
1985 GNUNET_CADET_show_channel (struct GNUNET_CADET_Handle *h,
1986                            struct GNUNET_PeerIdentity *initiator,
1987                            unsigned int channel_number,
1988                            GNUNET_CADET_ChannelCB callback,
1989                            void *callback_cls)
1990 {
1991   struct GNUNET_CADET_LocalInfo *msg;
1992   struct GNUNET_MQ_Envelope *env;
1993
1994   if (NULL != h->info_cb.channel_cb)
1995   {
1996     GNUNET_break (0);
1997     return GNUNET_SYSERR;
1998   }
1999
2000   env = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_CHANNEL);
2001   msg->peer = *initiator;
2002   msg->ccn.channel_of_client = htonl (channel_number);
2003   GNUNET_MQ_send (h->mq, env);
2004
2005   h->info_cb.channel_cb = callback;
2006   h->info_cls = callback_cls;
2007   return GNUNET_OK;
2008 }
2009
2010
2011 /**
2012  * Function called to notify a client about the connection
2013  * begin ready to queue more data.  "buf" will be
2014  * NULL and "size" zero if the connection was closed for
2015  * writing in the meantime.
2016  *
2017  * @param cls closure
2018  * @param size number of bytes available in buf
2019  * @param buf where the callee should write the message
2020  * @return number of bytes written to buf
2021  */
2022 static size_t
2023 cadet_mq_ntr (void *cls, size_t size,
2024              void *buf)
2025 {
2026   struct GNUNET_MQ_Handle *mq = cls;
2027   struct CadetMQState *state = GNUNET_MQ_impl_state (mq);
2028   const struct GNUNET_MessageHeader *msg = GNUNET_MQ_impl_current (mq);
2029   uint16_t msize;
2030
2031   state->th = NULL;
2032   if (NULL == buf)
2033   {
2034     GNUNET_MQ_inject_error (mq, GNUNET_MQ_ERROR_WRITE);
2035     return 0;
2036   }
2037   msize = ntohs (msg->size);
2038   GNUNET_assert (msize <= size);
2039   GNUNET_memcpy (buf, msg, msize);
2040   GNUNET_MQ_impl_send_continue (mq);
2041   return msize;
2042 }
2043
2044
2045 /**
2046  * Signature of functions implementing the
2047  * sending functionality of a message queue.
2048  *
2049  * @param mq the message queue
2050  * @param msg the message to send
2051  * @param impl_state state of the implementation
2052  */
2053 static void
2054 cadet_mq_send_impl (struct GNUNET_MQ_Handle *mq,
2055                     const struct GNUNET_MessageHeader *msg,
2056                     void *impl_state)
2057 {
2058   struct CadetMQState *state = impl_state;
2059
2060   GNUNET_assert (NULL == state->th);
2061   state->th =
2062       GNUNET_CADET_notify_transmit_ready (state->channel,
2063                                          /* FIXME: add option for corking */
2064                                          GNUNET_NO,
2065                                          GNUNET_TIME_UNIT_FOREVER_REL,
2066                                          ntohs (msg->size),
2067                                          &cadet_mq_ntr, mq);
2068
2069 }
2070
2071
2072 /**
2073  * Signature of functions implementing the
2074  * destruction of a message queue.
2075  * Implementations must not free 'mq', but should
2076  * take care of 'impl_state'.
2077  *
2078  * @param mq the message queue to destroy
2079  * @param impl_state state of the implementation
2080  */
2081 static void
2082 cadet_mq_destroy_impl (struct GNUNET_MQ_Handle *mq,
2083                        void *impl_state)
2084 {
2085   struct CadetMQState *state = impl_state;
2086
2087   if (NULL != state->th)
2088     GNUNET_CADET_notify_transmit_ready_cancel (state->th);
2089
2090   GNUNET_free (state);
2091 }
2092
2093
2094 /**
2095  * Create a message queue for a cadet channel.
2096  * The message queue can only be used to transmit messages,
2097  * not to receive them.
2098  *
2099  * @param channel the channel to create the message qeue for
2100  * @return a message queue to messages over the channel
2101  */
2102 struct GNUNET_MQ_Handle *
2103 GNUNET_CADET_mq_create (struct GNUNET_CADET_Channel *channel)
2104 {
2105   struct GNUNET_MQ_Handle *mq;
2106   struct CadetMQState *state;
2107
2108   state = GNUNET_new (struct CadetMQState);
2109   state->channel = channel;
2110
2111   mq = GNUNET_MQ_queue_for_callbacks (&cadet_mq_send_impl,
2112                                       &cadet_mq_destroy_impl,
2113                                       NULL, /* FIXME: cancel impl. */
2114                                       state,
2115                                       NULL, /* no msg handlers */
2116                                       NULL, /* no err handlers */
2117                                       NULL); /* no handler cls */
2118   return mq;
2119 }
2120
2121
2122 /**
2123  * Transitional function to convert an unsigned int port to a hash value.
2124  * WARNING: local static value returned, NOT reentrant!
2125  * WARNING: do not use this function for new code!
2126  *
2127  * @param port Numerical port (unsigned int format).
2128  *
2129  * @return A GNUNET_HashCode usable for the new CADET API.
2130  */
2131 const struct GNUNET_HashCode *
2132 GC_u2h (uint32_t port)
2133 {
2134   static struct GNUNET_HashCode hash;
2135
2136   GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2137               "This is a transitional function, "
2138               "use proper crypto hashes as CADET ports\n");
2139   GNUNET_CRYPTO_hash (&port, sizeof (port), &hash);
2140
2141   return &hash;
2142 }