4a662487c6e07e22e10c03104a0de43fd4b1c0cc
[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_chid;
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 chid;
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    * Size of packet queued in this channel
260    */
261   unsigned int packet_size;
262
263   /**
264    * Channel options: reliability, etc.
265    */
266   enum GNUNET_CADET_ChannelOption options;
267
268   /**
269    * Are we allowed to send to the service?
270    */
271   int allow_send;
272
273 };
274
275
276 /**
277  * Opaque handle to a port.
278  */
279 struct GNUNET_CADET_Port
280 {
281   /**
282    * Handle to the CADET session this port belongs to.
283    */
284   struct GNUNET_CADET_Handle *cadet;
285
286   /**
287    * Port ID.
288    */
289   struct GNUNET_HashCode *hash;
290
291   /**
292    * Callback handler for incoming channels on this port.
293    */
294   GNUNET_CADET_InboundChannelNotificationHandler *handler;
295
296   /**
297    * Closure for @a handler.
298    */
299   void *cls;
300 };
301
302
303 /**
304  * Implementation state for cadet's message queue.
305  */
306 struct CadetMQState
307 {
308   /**
309    * The current transmit handle, or NULL
310    * if no transmit is active.
311    */
312   struct GNUNET_CADET_TransmitHandle *th;
313
314   /**
315    * Channel to send the data over.
316    */
317   struct GNUNET_CADET_Channel *channel;
318 };
319
320
321 /******************************************************************************/
322 /***********************     AUXILIARY FUNCTIONS      *************************/
323 /******************************************************************************/
324
325 /**
326  * Check if transmission is a payload packet.
327  *
328  * @param th Transmission handle.
329  *
330  * @return #GNUNET_YES if it is a payload packet,
331  *         #GNUNET_NO if it is a cadet management packet.
332  */
333 static int
334 th_is_payload (struct GNUNET_CADET_TransmitHandle *th)
335 {
336   return (th->notify != NULL) ? GNUNET_YES : GNUNET_NO;
337 }
338
339
340 /**
341  * Find the Port struct for a hash.
342  *
343  * @param h CADET handle.
344  * @param hash HashCode for the port number.
345  *
346  * @return The port handle if known, NULL otherwise.
347  */
348 static struct GNUNET_CADET_Port *
349 find_port (const struct GNUNET_CADET_Handle *h,
350            const struct GNUNET_HashCode *hash)
351 {
352   struct GNUNET_CADET_Port *p;
353
354   p = GNUNET_CONTAINER_multihashmap_get (h->ports, hash);
355
356   return p;
357 }
358
359
360 /**
361  * Get the channel handler for the channel specified by id from the given handle
362  *
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
366  */
367 static struct GNUNET_CADET_Channel *
368 retrieve_channel (struct GNUNET_CADET_Handle *h,
369                   struct GNUNET_CADET_ClientChannelNumber chid)
370 {
371   struct GNUNET_CADET_Channel *ch;
372
373   for (ch = h->channels_head; NULL != ch; ch = ch->next)
374     if (ch->chid.channel_of_client == chid.channel_of_client)
375       return ch;
376   return NULL;
377 }
378
379
380 /**
381  * Create a new channel and insert it in the channel list of the cadet handle
382  *
383  * @param h Cadet handle
384  * @param chid Desired chid of the channel, 0 to assign one automatically.
385  *
386  * @return Handle to the created channel.
387  */
388 static struct GNUNET_CADET_Channel *
389 create_channel (struct GNUNET_CADET_Handle *h,
390                 struct GNUNET_CADET_ClientChannelNumber chid)
391 {
392   struct GNUNET_CADET_Channel *ch;
393
394   ch = GNUNET_new (struct GNUNET_CADET_Channel);
395   GNUNET_CONTAINER_DLL_insert (h->channels_head,
396                                h->channels_tail,
397                                ch);
398   ch->cadet = h;
399   if (0 == chid.channel_of_client)
400   {
401     ch->chid = h->next_chid;
402     while (NULL != retrieve_channel (h,
403                                      h->next_chid))
404     {
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);
410     }
411   }
412   else
413   {
414     ch->chid = chid;
415   }
416   ch->allow_send = GNUNET_NO;
417   return ch;
418 }
419
420
421 /**
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
427  *
428  * @param ch Pointer to the channel.
429  * @param call_cleaner Whether to call the cleaner handler.
430  *
431  * @return Handle to the required channel or NULL if not found.
432  */
433 // FIXME: simplify: call_cleaner is always #GNUNET_YES!!!
434 static void
435 destroy_channel (struct GNUNET_CADET_Channel *ch, int call_cleaner)
436 {
437   struct GNUNET_CADET_Handle *h;
438   struct GNUNET_CADET_TransmitHandle *th;
439   struct GNUNET_CADET_TransmitHandle *next;
440
441   LOG (GNUNET_ERROR_TYPE_DEBUG, " destroy_channel %X\n", ch->chid);
442
443   if (NULL == ch)
444   {
445     GNUNET_break (0);
446     return;
447   }
448   h = ch->cadet;
449
450   GNUNET_CONTAINER_DLL_remove (h->channels_head,
451                                h->channels_tail,
452                                ch);
453
454   /* signal channel destruction */
455   if ( (NULL != h->cleaner) && (0 != ch->peer) && (GNUNET_YES == call_cleaner) )
456   {
457     LOG (GNUNET_ERROR_TYPE_DEBUG, " 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->chid.channel_of_client);
534   msg->channel_id = ch->chid;
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, "Requesting Data: %u bytes\n", th->size);
562
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);
568
569   env = GNUNET_MQ_msg_extra (msg,
570                              th->size,
571                              GNUNET_MESSAGE_TYPE_CADET_LOCAL_DATA);
572   msg->id = th->channel->chid;
573   osize = th->notify (th->notify_cls,
574                       th->size,
575                       &msg[1]);
576   GNUNET_assert (osize == th->size);
577   GNUNET_MQ_send (th->channel->cadet->mq,
578                   env);
579   GNUNET_free (th);
580 }
581
582
583 /**
584  * Process the new channel notification and add it to the channels in the handle
585  *
586  * @param h     The cadet handle
587  * @param msg   A message with the details of the new incoming channel
588  */
589 static void
590 handle_channel_created (void *cls,
591                         const struct GNUNET_CADET_ChannelOpenMessageMessage *msg)
592 {
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;
598
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)
606   {
607     GNUNET_break (0);
608     return;
609   }
610   port = find_port (h, port_number);
611   if (NULL != port)
612   {
613     void *ctx;
614
615     ch = create_channel (h, chid);
616     ch->allow_send = GNUNET_NO;
617     ch->peer = GNUNET_PEER_intern (&msg->peer);
618     ch->cadet = h;
619     ch->chid = chid;
620     ch->port = port;
621     ch->options = ntohl (msg->opt);
622
623     LOG (GNUNET_ERROR_TYPE_DEBUG, "  created channel %p\n", ch);
624     ctx = port->handler (port->cls, ch, &msg->peer, port->hash, ch->options);
625     if (NULL != ctx)
626       ch->ctx = ctx;
627     LOG (GNUNET_ERROR_TYPE_DEBUG, "User notified\n");
628   }
629   else
630   {
631     struct GNUNET_CADET_ChannelDestroyMessage *d_msg;
632     struct GNUNET_MQ_Envelope *env;
633
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);
638   }
639   return;
640 }
641
642
643 /**
644  * Process the channel destroy notification and free associated resources
645  *
646  * @param h     The cadet handle
647  * @param msg   A message with the details of the channel being destroyed
648  */
649 static void
650 handle_channel_destroy (void *cls,
651                         const struct GNUNET_CADET_ChannelDestroyMessage *msg)
652 {
653   struct GNUNET_CADET_Handle *h = cls;
654   struct GNUNET_CADET_Channel *ch;
655   struct GNUNET_CADET_ClientChannelNumber chid;
656
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,
662                          chid);
663
664   if (NULL == ch)
665   {
666     LOG (GNUNET_ERROR_TYPE_DEBUG,
667          "channel %X unknown\n",
668          ntohl (chid.channel_of_client));
669     return;
670   }
671   destroy_channel (ch,
672                    GNUNET_YES);
673 }
674
675
676 /**
677  * Check that message received from CADET service is well-formed.
678  *
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
683  */
684 static int
685 check_local_data (void *cls,
686                   const struct GNUNET_CADET_LocalData *message)
687 {
688   struct GNUNET_CADET_Handle *h = cls;
689   struct GNUNET_CADET_Channel *ch;
690   uint16_t size;
691
692   size = ntohs (message->header.size);
693   if (sizeof (*message) + sizeof (struct GNUNET_MessageHeader) > size)
694   {
695     GNUNET_break_op (0);
696     return GNUNET_SYSERR;
697   }
698
699   ch = retrieve_channel (h,
700                          message->id);
701   if (NULL == ch)
702   {
703     GNUNET_break_op (0);
704     return GNUNET_SYSERR;
705   }
706
707   return GNUNET_OK;
708 }
709
710
711 /**
712  * Process the incoming data packets, call appropriate handlers.
713  *
714  * @param h       The cadet handle
715  * @param message A message encapsulating the data
716  */
717 static void
718 handle_local_data (void *cls,
719                    const struct GNUNET_CADET_LocalData *message)
720 {
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;
725   unsigned int i;
726   uint16_t type;
727
728   LOG (GNUNET_ERROR_TYPE_DEBUG,
729        "Got a data message!\n");
730   ch = retrieve_channel (h, message->id);
731   GNUNET_assert (NULL != ch);
732
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->id.channel_of_client));
739
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++)
743   {
744     handler = &h->message_handlers[i];
745     LOG (GNUNET_ERROR_TYPE_DEBUG, "    checking handler for type %u\n",
746          handler->type);
747     if (handler->type == type)
748     {
749       if (GNUNET_OK !=
750           handler->callback (h->cls, ch, &ch->ctx, payload))
751       {
752         LOG (GNUNET_ERROR_TYPE_DEBUG, "callback caused disconnection\n");
753         GNUNET_CADET_channel_destroy (ch);
754         break;
755       }
756       else
757       {
758         LOG (GNUNET_ERROR_TYPE_DEBUG,
759              "callback completed successfully\n");
760         break;
761       }
762     }
763   }
764 }
765
766
767 /**
768  * Process a local ACK message, enabling the client to send
769  * more data to the service.
770  *
771  * @param h Cadet handle.
772  * @param message Message itself.
773  */
774 static void
775 handle_local_ack (void *cls,
776                   const struct GNUNET_CADET_LocalAck *message)
777 {
778   struct GNUNET_CADET_Handle *h = cls;
779   struct GNUNET_CADET_Channel *ch;
780   struct GNUNET_CADET_ClientChannelNumber chid;
781
782   LOG (GNUNET_ERROR_TYPE_DEBUG, "Got an ACK!\n");
783   chid = message->channel_id;
784   ch = retrieve_channel (h, chid);
785   if (NULL == ch)
786   {
787     LOG (GNUNET_ERROR_TYPE_DEBUG,
788          "ACK on unknown channel %X\n",
789          ntohl (chid.channel_of_client));
790     return;
791   }
792   LOG (GNUNET_ERROR_TYPE_DEBUG,
793        "  on channel %X!\n",
794        ntohl (ch->chid.channel_of_client));
795   ch->allow_send = GNUNET_YES;
796   if (0 < ch->packet_size)
797   {
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",
802          ch->packet_size);
803     for (th = h->th_head; NULL != th; th = next)
804     {
805       next = th->next;
806       if (th->channel == ch)
807       {
808         GNUNET_assert (NULL == th->request_data_task);
809         th->request_data_task = GNUNET_SCHEDULER_add_now (&request_data, th);
810         break;
811       }
812     }
813     /* Complain if we got thru all th without sending anything, ch was wrong */
814     GNUNET_break (NULL != th);
815   }
816 }
817
818 /**
819  * Reconnect to the service, retransmit all infomation to try to restore the
820  * original state.
821  *
822  * @param h handle to the cadet
823  *
824  * @return #GNUNET_YES in case of sucess, #GNUNET_NO otherwise (service down...)
825  */
826 static void
827 reconnect (struct GNUNET_CADET_Handle *h);
828
829
830 /**
831  * Reconnect callback: tries to reconnect again after a failer previous
832  * reconnection.
833  *
834  * @param cls closure (cadet handle)
835  */
836 static void
837 reconnect_cbk (void *cls);
838
839
840 /**
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.
844  *
845  * @param cls closure, a `struct GNUNET_CORE_Handle *`
846  * @param error error code
847  */
848 static void
849 handle_mq_error (void *cls,
850                  enum GNUNET_MQ_Error error)
851 {
852   struct GNUNET_CADET_Handle *h = cls;
853
854   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MQ ERROR: %u\n", error);
855   GNUNET_MQ_destroy (h->mq);
856   h->mq = NULL;
857   reconnect (h);
858 }
859
860
861 /*
862  * Process a local reply about info on all channels, pass info to the user.
863  *
864  * @param h Cadet handle.
865  * @param message Message itself.
866  */
867 // static void
868 // process_get_channels (struct GNUNET_CADET_Handle *h,
869 //                      const struct GNUNET_MessageHeader *message)
870 // {
871 //   struct GNUNET_CADET_LocalInfo *msg;
872 //
873 //   GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Get Channels messasge received\n");
874 //
875 //   if (NULL == h->channels_cb)
876 //   {
877 //     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "  ignored\n");
878 //     return;
879 //   }
880 //
881 //   msg = (struct GNUNET_CADET_LocalInfo *) message;
882 //   if (ntohs (message->size) !=
883 //       (sizeof (struct GNUNET_CADET_LocalInfo) +
884 //        sizeof (struct GNUNET_PeerIdentity)))
885 //   {
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));
891 //     return;
892 //   }
893 //   h->channels_cb (h->channels_cls,
894 //                   ntohl (msg->channel_id),
895 //                   &msg->owner,
896 //                   &msg->destination);
897 // }
898
899
900
901 /*
902  * Process a local monitor_channel reply, pass info to the user.
903  *
904  * @param h Cadet handle.
905  * @param message Message itself.
906  */
907 // static void
908 // process_show_channel (struct GNUNET_CADET_Handle *h,
909 //                      const struct GNUNET_MessageHeader *message)
910 // {
911 //   struct GNUNET_CADET_LocalInfo *msg;
912 //   size_t esize;
913 //
914 //   GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Show Channel messasge received\n");
915 //
916 //   if (NULL == h->channel_cb)
917 //   {
918 //     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "  ignored\n");
919 //     return;
920 //   }
921 //
922 //   /* Verify message sanity */
923 //   msg = (struct GNUNET_CADET_LocalInfo *) message;
924 //   esize = sizeof (struct GNUNET_CADET_LocalInfo);
925 //   if (ntohs (message->size) != esize)
926 //   {
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),
931 //                 esize);
932 //
933 //     h->channel_cb (h->channel_cls, NULL, NULL);
934 //     h->channel_cb = NULL;
935 //     h->channel_cls = NULL;
936 //
937 //     return;
938 //   }
939 //
940 //   h->channel_cb (h->channel_cls,
941 //                  &msg->destination,
942 //                  &msg->owner);
943 // }
944
945
946
947 /**
948  * Check that message received from CADET service is well-formed.
949  *
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
954  */
955 static int
956 check_get_peers (void *cls,
957                  const struct GNUNET_CADET_LocalInfoPeer *message)
958 {
959   struct GNUNET_CADET_Handle *h = cls;
960   uint16_t size;
961
962   if (NULL == h->info_cb.peers_cb)
963   {
964     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
965                 "  no handler for peesr monitor message!\n");
966     return GNUNET_SYSERR;
967   }
968
969   size = ntohs (message->header.size);
970   if (sizeof (struct GNUNET_CADET_LocalInfoPeer) > size)
971   {
972     h->info_cb.peers_cb (h->info_cls, NULL, -1, 0, 0);
973     h->info_cb.peers_cb = NULL;
974     h->info_cls = NULL;
975     return GNUNET_SYSERR;
976   }
977
978   return GNUNET_OK;
979 }
980
981
982 /**
983  * Process a local reply about info on all tunnels, pass info to the user.
984  *
985  * @param cls Closure (Cadet handle).
986  * @param msg Message itself.
987  */
988 static void
989 handle_get_peers (void *cls,
990                   const struct GNUNET_CADET_LocalInfoPeer *msg)
991 {
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),
996                        0);
997 }
998
999
1000 /**
1001  * Check that message received from CADET service is well-formed.
1002  *
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
1007  */
1008 static int
1009 check_get_peer (void *cls,
1010                 const struct GNUNET_CADET_LocalInfoPeer *message)
1011 {
1012   struct GNUNET_CADET_Handle *h = cls;
1013   const size_t msize = sizeof (struct GNUNET_CADET_LocalInfoPeer);
1014   struct GNUNET_PeerIdentity *paths_array;
1015   size_t esize;
1016   unsigned int epaths;
1017   unsigned int paths;
1018   unsigned int peers;
1019
1020   if (NULL == h->info_cb.peer_cb)
1021   {
1022     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1023                 "  no handler for peer monitor message!\n");
1024     goto clean_cls;
1025   }
1026
1027   /* Verify message sanity */
1028   esize = ntohs (message->header.size);
1029   if (esize < msize)
1030   {
1031     GNUNET_break_op (0);
1032     h->info_cb.peer_cb (h->info_cls, NULL, 0, 0, 0, NULL);
1033     goto clean_cls;
1034   }
1035   if (0 != ((esize - msize) % sizeof (struct GNUNET_PeerIdentity)))
1036   {
1037     GNUNET_break_op (0);
1038     h->info_cb.peer_cb (h->info_cls, NULL, 0, 0, 0, NULL);
1039     goto clean_cls;
1040
1041   }
1042   peers = (esize - msize) / sizeof (struct GNUNET_PeerIdentity);
1043   epaths = (unsigned int) ntohs (message->paths);
1044   paths_array = (struct GNUNET_PeerIdentity *) &message[1];
1045   paths = 0;
1046   for (int i = 0; i < peers; i++)
1047   {
1048     if (0 == memcmp (&paths_array[i], &message->destination,
1049                      sizeof (struct GNUNET_PeerIdentity)))
1050     {
1051       paths++;
1052     }
1053   }
1054   if (paths != epaths)
1055   {
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);
1059     goto clean_cls;
1060   }
1061
1062   return GNUNET_OK;
1063
1064 clean_cls:
1065   h->info_cb.peer_cb = NULL;
1066   h->info_cls = NULL;
1067   return GNUNET_SYSERR;
1068 }
1069
1070
1071 /**
1072  * Process a local peer info reply, pass info to the user.
1073  *
1074  * @param cls Closure (Cadet handle).
1075  * @param message Message itself.
1076  */
1077 static void
1078 handle_get_peer (void *cls,
1079                  const struct GNUNET_CADET_LocalInfoPeer *message)
1080 {
1081   struct GNUNET_CADET_Handle *h = cls;
1082   struct GNUNET_PeerIdentity *paths_array;
1083   unsigned int paths;
1084   unsigned int path_length;
1085   int neighbor;
1086   unsigned int peers;
1087
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);
1092   path_length = 0;
1093   neighbor = GNUNET_NO;
1094
1095   for (int i = 0; i < peers; i++)
1096   {
1097     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " %s\n", GNUNET_i2s (&paths_array[i]));
1098     path_length++;
1099     if (0 == memcmp (&paths_array[i], &message->destination,
1100                      sizeof (struct GNUNET_PeerIdentity)))
1101     {
1102       if (1 == path_length)
1103         neighbor = GNUNET_YES;
1104       path_length = 0;
1105     }
1106   }
1107
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),
1113                       neighbor,
1114                       paths,
1115                       paths_array);
1116 }
1117
1118
1119 /**
1120  * Check that message received from CADET service is well-formed.
1121  *
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
1126  */
1127 static int
1128 check_get_tunnels (void *cls,
1129                    const struct GNUNET_CADET_LocalInfoTunnel *msg)
1130 {
1131   struct GNUNET_CADET_Handle *h = cls;
1132   uint16_t size;
1133
1134   if (NULL == h->info_cb.tunnels_cb)
1135   {
1136     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1137                 "  no handler for tunnels monitor message!\n");
1138     return GNUNET_SYSERR;
1139   }
1140
1141   size = ntohs (msg->header.size);
1142   if (sizeof (struct GNUNET_CADET_LocalInfoTunnel) > size)
1143   {
1144     h->info_cb.tunnels_cb (h->info_cls, NULL, 0, 0, 0, 0);
1145     h->info_cb.tunnels_cb = NULL;
1146     h->info_cls = NULL;
1147     return GNUNET_SYSERR;
1148   }
1149   return GNUNET_OK;
1150 }
1151
1152
1153 /**
1154  * Process a local reply about info on all tunnels, pass info to the user.
1155  *
1156  * @param cls Closure (Cadet handle).
1157  * @param message Message itself.
1158  */
1159 static void
1160 handle_get_tunnels (void *cls,
1161                     const struct GNUNET_CADET_LocalInfoTunnel *msg)
1162 {
1163   struct GNUNET_CADET_Handle *h = cls;
1164
1165   h->info_cb.tunnels_cb (h->info_cls,
1166                          &msg->destination,
1167                          ntohl (msg->channels),
1168                          ntohl (msg->connections),
1169                          ntohs (msg->estate),
1170                          ntohs (msg->cstate));
1171
1172 }
1173
1174
1175 /**
1176  * Check that message received from CADET service is well-formed.
1177  *
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
1182  */
1183 static int
1184 check_get_tunnel (void *cls,
1185                   const struct GNUNET_CADET_LocalInfoTunnel *msg)
1186 {
1187   struct GNUNET_CADET_Handle *h = cls;
1188   unsigned int ch_n;
1189   unsigned int c_n;
1190   size_t esize;
1191   size_t msize;
1192
1193   if (NULL == h->info_cb.tunnel_cb)
1194   {
1195     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1196                 "  no handler for tunnel monitor message!\n");
1197     goto clean_cls;
1198   }
1199
1200   /* Verify message sanity */
1201   msize = ntohs (msg->header.size);
1202   esize = sizeof (struct GNUNET_CADET_LocalInfoTunnel);
1203   if (esize > msize)
1204   {
1205     GNUNET_break_op (0);
1206     h->info_cb.tunnel_cb (h->info_cls,
1207                           NULL, 0, 0, NULL, NULL, 0, 0);
1208     goto clean_cls;
1209   }
1210   ch_n = ntohl (msg->channels);
1211   c_n = ntohl (msg->connections);
1212   esize += ch_n * sizeof (struct GNUNET_CADET_ChannelNumber);
1213   esize += c_n * sizeof (struct GNUNET_CADET_ConnectionTunnelIdentifier);
1214   if (msize != esize)
1215   {
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,
1221                 ch_n,
1222                 c_n);
1223     h->info_cb.tunnel_cb (h->info_cls,
1224                           NULL, 0, 0, NULL, NULL, 0, 0);
1225     goto clean_cls;
1226   }
1227
1228   return GNUNET_OK;
1229
1230 clean_cls:
1231   h->info_cb.tunnel_cb = NULL;
1232   h->info_cls = NULL;
1233   return GNUNET_SYSERR;
1234 }
1235
1236
1237 /**
1238  * Process a local tunnel info reply, pass info to the user.
1239  *
1240  * @param cls Closure (Cadet handle).
1241  * @param msg Message itself.
1242  */
1243 static void
1244 handle_get_tunnel (void *cls,
1245                    const struct GNUNET_CADET_LocalInfoTunnel *msg)
1246 {
1247   struct GNUNET_CADET_Handle *h = cls;
1248   unsigned int ch_n;
1249   unsigned int c_n;
1250   const struct GNUNET_CADET_ConnectionTunnelIdentifier *conns;
1251   const struct GNUNET_CADET_ChannelNumber *chns;
1252
1253   ch_n = ntohl (msg->channels);
1254   c_n = ntohl (msg->connections);
1255
1256   /* Call Callback with tunnel info. */
1257   conns = (const struct GNUNET_CADET_ConnectionTunnelIdentifier *) &msg[1];
1258   chns = (const struct GNUNET_CADET_ChannelNumber *) &conns[c_n];
1259   h->info_cb.tunnel_cb (h->info_cls,
1260                         &msg->destination,
1261                         ch_n,
1262                         c_n,
1263                         chns,
1264                         conns,
1265                         ntohs (msg->estate),
1266                         ntohs (msg->cstate));
1267 }
1268
1269
1270
1271 /**
1272  * Reconnect to the service, retransmit all infomation to try to restore the
1273  * original state.
1274  *
1275  * @param h handle to the cadet
1276  *
1277  * @return GNUNET_YES in case of sucess, GNUNET_NO otherwise (service down...)
1278  */
1279 static int
1280 do_reconnect (struct GNUNET_CADET_Handle *h)
1281 {
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,
1286                              h),
1287     GNUNET_MQ_hd_fixed_size (channel_destroy,
1288                              GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DESTROY,
1289                              struct GNUNET_CADET_ChannelDestroyMessage,
1290                              h),
1291     GNUNET_MQ_hd_var_size (local_data,
1292                            GNUNET_MESSAGE_TYPE_CADET_LOCAL_DATA,
1293                            struct GNUNET_CADET_LocalData,
1294                            h),
1295     GNUNET_MQ_hd_fixed_size (local_ack,
1296                              GNUNET_MESSAGE_TYPE_CADET_LOCAL_ACK,
1297                              struct GNUNET_CADET_LocalAck,
1298                              h),
1299     GNUNET_MQ_hd_var_size (get_peers,
1300                            GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEERS,
1301                            struct GNUNET_CADET_LocalInfoPeer,
1302                            h),
1303     GNUNET_MQ_hd_var_size (get_peer,
1304                            GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEER,
1305                            struct GNUNET_CADET_LocalInfoPeer,
1306                            h),
1307     GNUNET_MQ_hd_var_size (get_tunnels,
1308                            GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNELS,
1309                            struct GNUNET_CADET_LocalInfoTunnel,
1310                            h),
1311     GNUNET_MQ_hd_var_size (get_tunnel,
1312                            GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNEL,
1313                            struct GNUNET_CADET_LocalInfoTunnel,
1314                            h),
1315   // FIXME
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 ()
1320   };
1321
1322   LOG (GNUNET_ERROR_TYPE_DEBUG, "Connecting to CADET\n");
1323
1324   GNUNET_assert (NULL == h->mq);
1325   h->mq = GNUNET_CLIENT_connect (h->cfg,
1326                                  "cadet",
1327                                  handlers,
1328                                  &handle_mq_error,
1329                                  h);
1330   if (NULL == h->mq)
1331   {
1332     reconnect (h);
1333     return GNUNET_NO;
1334   }
1335   else
1336   {
1337     h->reconnect_time = GNUNET_TIME_UNIT_MILLISECONDS;
1338   }
1339   return GNUNET_YES;
1340 }
1341
1342 /**
1343  * Reconnect callback: tries to reconnect again after a failer previous
1344  * reconnecttion
1345  *
1346  * @param cls closure (cadet handle)
1347  */
1348 static void
1349 reconnect_cbk (void *cls)
1350 {
1351   struct GNUNET_CADET_Handle *h = cls;
1352
1353   h->reconnect_task = NULL;
1354   do_reconnect (h);
1355 }
1356
1357
1358 /**
1359  * Reconnect to the service, retransmit all infomation to try to restore the
1360  * original state.
1361  *
1362  * @param h handle to the cadet
1363  *
1364  * @return #GNUNET_YES in case of sucess, #GNUNET_NO otherwise (service down...)
1365  */
1366 static void
1367 reconnect (struct GNUNET_CADET_Handle *h)
1368 {
1369   struct GNUNET_CADET_Channel *ch;
1370
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,
1377                                                       &reconnect_cbk, h);
1378 }
1379
1380
1381 /******************************************************************************/
1382 /**********************      API CALL DEFINITIONS     *************************/
1383 /******************************************************************************/
1384
1385 struct GNUNET_CADET_Handle *
1386 GNUNET_CADET_connect (const struct GNUNET_CONFIGURATION_Handle *cfg,
1387                       void *cls,
1388                       GNUNET_CADET_ChannelEndHandler cleaner,
1389                       const struct GNUNET_CADET_MessageHandler *handlers)
1390 {
1391   struct GNUNET_CADET_Handle *h;
1392
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);
1396   h->cfg = cfg;
1397   h->cleaner = cleaner;
1398   h->ports = GNUNET_CONTAINER_multihashmap_create (4, GNUNET_YES);
1399   do_reconnect (h);
1400   if (h->mq == NULL)
1401   {
1402     GNUNET_break (0);
1403     GNUNET_CADET_disconnect (h);
1404     return NULL;
1405   }
1406   h->cls = cls;
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;
1411
1412   /* count handlers */
1413   for (h->n_handlers = 0;
1414        handlers && handlers[h->n_handlers].type;
1415        h->n_handlers++) ;
1416   LOG (GNUNET_ERROR_TYPE_DEBUG, "GNUNET_CADET_connect() END\n");
1417   return h;
1418 }
1419
1420
1421 void
1422 GNUNET_CADET_disconnect (struct GNUNET_CADET_Handle *handle)
1423 {
1424   struct GNUNET_CADET_Channel *ch;
1425   struct GNUNET_CADET_Channel *aux;
1426   struct GNUNET_CADET_TransmitHandle *th;
1427
1428   LOG (GNUNET_ERROR_TYPE_DEBUG,
1429        "CADET DISCONNECT\n");
1430   ch = handle->channels_head;
1431   while (NULL != ch)
1432   {
1433     aux = ch->next;
1434     if (ntohl (ch->chid.channel_of_client) >= GNUNET_CADET_LOCAL_CHANNEL_ID_CLI)
1435     {
1436       GNUNET_break (0);
1437       LOG (GNUNET_ERROR_TYPE_DEBUG,
1438            "channel %X not destroyed\n",
1439            ntohl (ch->chid.channel_of_client));
1440     }
1441     destroy_channel (ch,
1442                      GNUNET_YES);
1443     ch = aux;
1444   }
1445   while (NULL != (th = handle->th_head))
1446   {
1447     struct GNUNET_MessageHeader *msg;
1448
1449     /* Make sure it is an allowed packet (everything else should have been
1450      * already canceled).
1451      */
1452     GNUNET_break (GNUNET_NO == th_is_payload (th));
1453     msg = (struct GNUNET_MessageHeader *) &th[1];
1454     switch (ntohs(msg->type))
1455     {
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:
1466         break;
1467       default:
1468         GNUNET_break (0);
1469         LOG (GNUNET_ERROR_TYPE_ERROR, "unexpected unsent msg %s\n",
1470              GC_m2s (ntohs(msg->type)));
1471     }
1472
1473     GNUNET_CADET_notify_transmit_ready_cancel (th);
1474   }
1475
1476   if (NULL != handle->mq)
1477   {
1478     GNUNET_MQ_destroy (handle->mq);
1479     handle->mq = NULL;
1480   }
1481   if (NULL != handle->reconnect_task)
1482   {
1483     GNUNET_SCHEDULER_cancel(handle->reconnect_task);
1484     handle->reconnect_task = NULL;
1485   }
1486
1487   GNUNET_CONTAINER_multihashmap_destroy (handle->ports);
1488   handle->ports = NULL;
1489   GNUNET_free (handle);
1490 }
1491
1492
1493 /**
1494  * Open a port to receive incomming channels.
1495  *
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.
1500  *
1501  * @return Port handle.
1502  */
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
1507                             new_channel,
1508                         void *new_channel_cls)
1509 {
1510   struct GNUNET_CADET_PortMessage *msg;
1511   struct GNUNET_MQ_Envelope *env;
1512   struct GNUNET_CADET_Port *p;
1513
1514   GNUNET_assert (NULL != new_channel);
1515   p = GNUNET_new (struct GNUNET_CADET_Port);
1516   p->cadet = h;
1517   p->hash = GNUNET_new (struct GNUNET_HashCode);
1518   *p->hash = *port;
1519   p->handler = new_channel;
1520   p->cls = new_channel_cls;
1521   GNUNET_assert (GNUNET_OK ==
1522                  GNUNET_CONTAINER_multihashmap_put (h->ports,
1523                                                     p->hash,
1524                                                     p,
1525                                                     GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1526
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);
1530
1531   return p;
1532 }
1533
1534 /**
1535  * Close a port opened with @a GNUNET_CADET_open_port.
1536  * The @a new_channel callback will no longer be called.
1537  *
1538  * @param p Port handle.
1539  */
1540 void
1541 GNUNET_CADET_close_port (struct GNUNET_CADET_Port *p)
1542 {
1543   struct GNUNET_CADET_PortMessage *msg;
1544   struct GNUNET_MQ_Envelope *env;
1545
1546   env = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_CADET_LOCAL_PORT_CLOSE);
1547
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);
1552   GNUNET_free (p);
1553 }
1554
1555
1556 /**
1557  * Create a new channel towards a remote peer.
1558  *
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
1561  * for this channel.
1562  *
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.
1568  *
1569  * @return handle to the channel
1570  */
1571 struct GNUNET_CADET_Channel *
1572 GNUNET_CADET_channel_create (struct GNUNET_CADET_Handle *h,
1573                             void *channel_ctx,
1574                             const struct GNUNET_PeerIdentity *peer,
1575                             const struct GNUNET_HashCode *port,
1576                             enum GNUNET_CADET_ChannelOption options)
1577 {
1578   struct GNUNET_CADET_ChannelOpenMessageMessage *msg;
1579   struct GNUNET_MQ_Envelope *env;
1580   struct GNUNET_CADET_Channel *ch;
1581   struct GNUNET_CADET_ClientChannelNumber chid;
1582
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);
1593
1594   env = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_CADET_CHANNEL_OPEN);
1595   msg->channel_id = ch->chid;
1596   msg->port = *port;
1597   msg->peer = *peer;
1598   msg->opt = htonl (options);
1599   ch->allow_send = GNUNET_NO;
1600   GNUNET_MQ_send (h->mq,
1601                   env);
1602   return ch;
1603 }
1604
1605
1606 void
1607 GNUNET_CADET_channel_destroy (struct GNUNET_CADET_Channel *channel)
1608 {
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;
1614
1615   LOG (GNUNET_ERROR_TYPE_DEBUG, "Destroying channel\n");
1616   h = channel->cadet;
1617   for  (th = h->th_head; th != NULL; th = next)
1618   {
1619     next = th->next;
1620     if (th->channel == channel)
1621     {
1622       GNUNET_break (0);
1623       if (GNUNET_YES == th_is_payload (th))
1624       {
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);
1629       }
1630       else
1631       {
1632         LOG (GNUNET_ERROR_TYPE_WARNING, "no meta-traffic should be queued\n");
1633       }
1634       GNUNET_CONTAINER_DLL_remove (h->th_head, h->th_tail, th);
1635       GNUNET_CADET_notify_transmit_ready_cancel (th);
1636     }
1637   }
1638
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);
1642
1643   destroy_channel (channel, GNUNET_YES);
1644 }
1645
1646
1647 /**
1648  * Get information about a channel.
1649  *
1650  * @param channel Channel handle.
1651  * @param option Query (GNUNET_CADET_OPTION_*).
1652  * @param ... dependant on option, currently not used
1653  *
1654  * @return Union with an answer to the query.
1655  */
1656 const union GNUNET_CADET_ChannelInfo *
1657 GNUNET_CADET_channel_get_info (struct GNUNET_CADET_Channel *channel,
1658                               enum GNUNET_CADET_ChannelOption option, ...)
1659 {
1660   static int bool_flag;
1661   const union GNUNET_CADET_ChannelInfo *ret;
1662
1663   switch (option)
1664   {
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;
1670       else
1671         bool_flag = GNUNET_NO;
1672       ret = (const union GNUNET_CADET_ChannelInfo *) &bool_flag;
1673       break;
1674     case GNUNET_CADET_OPTION_PEER:
1675       ret = (const union GNUNET_CADET_ChannelInfo *) GNUNET_PEER_resolve2 (channel->peer);
1676       break;
1677     default:
1678       GNUNET_break (0);
1679       return NULL;
1680   }
1681
1682   return ret;
1683 }
1684
1685
1686 struct GNUNET_CADET_TransmitHandle *
1687 GNUNET_CADET_notify_transmit_ready (struct GNUNET_CADET_Channel *channel,
1688                                     int cork,
1689                                     struct GNUNET_TIME_Relative maxdelay,
1690                                     size_t notify_size,
1691                                     GNUNET_CONNECTION_TransmitReadyNotify notify,
1692                                     void *notify_cls)
1693 {
1694   struct GNUNET_CADET_TransmitHandle *th;
1695
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");
1704   else
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
1709
1710   if (GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us != maxdelay.rel_value_us)
1711   {
1712     LOG (GNUNET_ERROR_TYPE_WARNING,
1713          "CADET transmit ready timeout is deprected (has no effect)\n");
1714   }
1715
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);
1725   else
1726     add_to_queue (channel->cadet, th);
1727
1728   LOG (GNUNET_ERROR_TYPE_DEBUG, "CADET NOTIFY TRANSMIT READY END\n");
1729   return th;
1730 }
1731
1732
1733 void
1734 GNUNET_CADET_notify_transmit_ready_cancel (struct GNUNET_CADET_TransmitHandle *th)
1735 {
1736   if (NULL != th->request_data_task)
1737   {
1738     GNUNET_SCHEDULER_cancel (th->request_data_task);
1739   }
1740   th->request_data_task = NULL;
1741
1742   remove_from_queue (th);
1743   GNUNET_free (th);
1744 }
1745
1746
1747 void
1748 GNUNET_CADET_receive_done (struct GNUNET_CADET_Channel *channel)
1749 {
1750   send_ack (channel);
1751 }
1752
1753
1754 static void
1755 send_info_request (struct GNUNET_CADET_Handle *h, uint16_t type)
1756 {
1757   struct GNUNET_MessageHeader *msg;
1758   struct GNUNET_MQ_Envelope *env;
1759
1760   LOG (GNUNET_ERROR_TYPE_DEBUG,
1761        " Sending %s monitor message to service\n",
1762        GC_m2s(type));
1763
1764   env = GNUNET_MQ_msg (msg, type);
1765   GNUNET_MQ_send (h->mq, env);
1766 }
1767
1768
1769 /**
1770  * Request a debug dump on the service's STDERR.
1771  *
1772  * WARNING: unstable API, likely to change in the future!
1773  *
1774  * @param h cadet handle
1775  */
1776 void
1777 GNUNET_CADET_request_dump (struct GNUNET_CADET_Handle *h)
1778 {
1779   LOG (GNUNET_ERROR_TYPE_DEBUG, "requesting dump\n");
1780   send_info_request (h, GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_DUMP);
1781 }
1782
1783
1784 /**
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.
1788  *
1789  *
1790  * WARNING: unstable API, likely to change in the future!
1791  *
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.
1795  *
1796  * @return #GNUNET_OK / #GNUNET_SYSERR
1797  */
1798 int
1799 GNUNET_CADET_get_peers (struct GNUNET_CADET_Handle *h,
1800                        GNUNET_CADET_PeersCB callback,
1801                        void *callback_cls)
1802 {
1803   if (NULL != h->info_cb.peers_cb)
1804   {
1805     GNUNET_break (0);
1806     return GNUNET_SYSERR;
1807   }
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;
1811   return GNUNET_OK;
1812 }
1813
1814
1815 /**
1816  * Cancel a peer info request. The callback will not be called (anymore).
1817  *
1818  * WARNING: unstable API, likely to change in the future!
1819  *
1820  * @param h Cadet handle.
1821  *
1822  * @return Closure given to GNUNET_CADET_get_peers.
1823  */
1824 void *
1825 GNUNET_CADET_get_peers_cancel (struct GNUNET_CADET_Handle *h)
1826 {
1827   void *cls;
1828
1829   cls = h->info_cls;
1830   h->info_cb.peers_cb = NULL;
1831   h->info_cls = NULL;
1832   return cls;
1833 }
1834
1835
1836 /**
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.
1840  *
1841  * WARNING: unstable API, likely to change in the future!
1842  *
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.
1847  *
1848  * @return #GNUNET_OK / #GNUNET_SYSERR
1849  */
1850 int
1851 GNUNET_CADET_get_peer (struct GNUNET_CADET_Handle *h,
1852                        const struct GNUNET_PeerIdentity *id,
1853                        GNUNET_CADET_PeerCB callback,
1854                        void *callback_cls)
1855 {
1856   struct GNUNET_CADET_LocalInfo *msg;
1857   struct GNUNET_MQ_Envelope *env;
1858
1859   if (NULL != h->info_cb.peer_cb)
1860   {
1861     GNUNET_break (0);
1862     return GNUNET_SYSERR;
1863   }
1864
1865   env = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEER);
1866   msg->peer = *id;
1867   GNUNET_MQ_send (h->mq, env);
1868
1869   h->info_cb.peer_cb = callback;
1870   h->info_cls = callback_cls;
1871   return GNUNET_OK;
1872 }
1873
1874
1875 /**
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.
1879  *
1880  * WARNING: unstable API, likely to change in the future!
1881  *
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.
1885  *
1886  * @return #GNUNET_OK / #GNUNET_SYSERR
1887  */
1888 int
1889 GNUNET_CADET_get_tunnels (struct GNUNET_CADET_Handle *h,
1890                          GNUNET_CADET_TunnelsCB callback,
1891                          void *callback_cls)
1892 {
1893   if (NULL != h->info_cb.tunnels_cb)
1894   {
1895     GNUNET_break (0);
1896     return GNUNET_SYSERR;
1897   }
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;
1901   return GNUNET_OK;
1902 }
1903
1904
1905 /**
1906  * Cancel a monitor request. The monitor callback will not be called.
1907  *
1908  * @param h Cadet handle.
1909  *
1910  * @return Closure given to GNUNET_CADET_get_tunnels.
1911  */
1912 void *
1913 GNUNET_CADET_get_tunnels_cancel (struct GNUNET_CADET_Handle *h)
1914 {
1915   void *cls;
1916
1917   h->info_cb.tunnels_cb = NULL;
1918   cls = h->info_cls;
1919   h->info_cls = NULL;
1920
1921   return cls;
1922 }
1923
1924
1925
1926 /**
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.
1930  *
1931  * WARNING: unstable API, likely to change in the future!
1932  *
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.
1937  *
1938  * @return #GNUNET_OK / #GNUNET_SYSERR
1939  */
1940 int
1941 GNUNET_CADET_get_tunnel (struct GNUNET_CADET_Handle *h,
1942                         const struct GNUNET_PeerIdentity *id,
1943                         GNUNET_CADET_TunnelCB callback,
1944                         void *callback_cls)
1945 {
1946   struct GNUNET_CADET_LocalInfo *msg;
1947   struct GNUNET_MQ_Envelope *env;
1948
1949   if (NULL != h->info_cb.tunnel_cb)
1950   {
1951     GNUNET_break (0);
1952     return GNUNET_SYSERR;
1953   }
1954
1955   env = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNEL);
1956   msg->peer = *id;
1957   GNUNET_MQ_send (h->mq, env);
1958
1959   h->info_cb.tunnel_cb = callback;
1960   h->info_cls = callback_cls;
1961   return GNUNET_OK;
1962 }
1963
1964
1965 /**
1966  * Request information about a specific channel of the running cadet peer.
1967  *
1968  * WARNING: unstable API, likely to change in the future!
1969  * FIXME Add destination option.
1970  *
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.
1976  *
1977  * @return #GNUNET_OK / #GNUNET_SYSERR
1978  */
1979 int
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,
1984                            void *callback_cls)
1985 {
1986   struct GNUNET_CADET_LocalInfo *msg;
1987   struct GNUNET_MQ_Envelope *env;
1988
1989   if (NULL != h->info_cb.channel_cb)
1990   {
1991     GNUNET_break (0);
1992     return GNUNET_SYSERR;
1993   }
1994
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);
1999
2000   h->info_cb.channel_cb = callback;
2001   h->info_cls = callback_cls;
2002   return GNUNET_OK;
2003 }
2004
2005
2006 /**
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.
2011  *
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
2016  */
2017 static size_t
2018 cadet_mq_ntr (void *cls, size_t size,
2019              void *buf)
2020 {
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);
2024   uint16_t msize;
2025
2026   state->th = NULL;
2027   if (NULL == buf)
2028   {
2029     GNUNET_MQ_inject_error (mq, GNUNET_MQ_ERROR_WRITE);
2030     return 0;
2031   }
2032   msize = ntohs (msg->size);
2033   GNUNET_assert (msize <= size);
2034   GNUNET_memcpy (buf, msg, msize);
2035   GNUNET_MQ_impl_send_continue (mq);
2036   return msize;
2037 }
2038
2039
2040 /**
2041  * Signature of functions implementing the
2042  * sending functionality of a message queue.
2043  *
2044  * @param mq the message queue
2045  * @param msg the message to send
2046  * @param impl_state state of the implementation
2047  */
2048 static void
2049 cadet_mq_send_impl (struct GNUNET_MQ_Handle *mq,
2050                     const struct GNUNET_MessageHeader *msg,
2051                     void *impl_state)
2052 {
2053   struct CadetMQState *state = impl_state;
2054
2055   GNUNET_assert (NULL == state->th);
2056   state->th =
2057       GNUNET_CADET_notify_transmit_ready (state->channel,
2058                                          /* FIXME: add option for corking */
2059                                          GNUNET_NO,
2060                                          GNUNET_TIME_UNIT_FOREVER_REL,
2061                                          ntohs (msg->size),
2062                                          &cadet_mq_ntr, mq);
2063
2064 }
2065
2066
2067 /**
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'.
2072  *
2073  * @param mq the message queue to destroy
2074  * @param impl_state state of the implementation
2075  */
2076 static void
2077 cadet_mq_destroy_impl (struct GNUNET_MQ_Handle *mq,
2078                        void *impl_state)
2079 {
2080   struct CadetMQState *state = impl_state;
2081
2082   if (NULL != state->th)
2083     GNUNET_CADET_notify_transmit_ready_cancel (state->th);
2084
2085   GNUNET_free (state);
2086 }
2087
2088
2089 /**
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.
2093  *
2094  * @param channel the channel to create the message qeue for
2095  * @return a message queue to messages over the channel
2096  */
2097 struct GNUNET_MQ_Handle *
2098 GNUNET_CADET_mq_create (struct GNUNET_CADET_Channel *channel)
2099 {
2100   struct GNUNET_MQ_Handle *mq;
2101   struct CadetMQState *state;
2102
2103   state = GNUNET_new (struct CadetMQState);
2104   state->channel = channel;
2105
2106   mq = GNUNET_MQ_queue_for_callbacks (&cadet_mq_send_impl,
2107                                       &cadet_mq_destroy_impl,
2108                                       NULL, /* FIXME: cancel impl. */
2109                                       state,
2110                                       NULL, /* no msg handlers */
2111                                       NULL, /* no err handlers */
2112                                       NULL); /* no handler cls */
2113   return mq;
2114 }
2115
2116
2117 /**
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!
2121  *
2122  * @param port Numerical port (unsigned int format).
2123  *
2124  * @return A GNUNET_HashCode usable for the new CADET API.
2125  */
2126 const struct GNUNET_HashCode *
2127 GC_u2h (uint32_t port)
2128 {
2129   static struct GNUNET_HashCode hash;
2130
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);
2135
2136   return &hash;
2137 }