de10d5809aba1b11698793d372c8fdd67577fe58
[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  * Add a transmit handle to the transmission queue and set the
483  * timeout if needed.
484  *
485  * @param h cadet handle with the queue head and tail
486  * @param th handle to the packet to be transmitted
487  */
488 static void
489 add_to_queue (struct GNUNET_CADET_Handle *h,
490               struct GNUNET_CADET_TransmitHandle *th)
491 {
492   GNUNET_CONTAINER_DLL_insert_tail (h->th_head, h->th_tail, th);
493 }
494
495
496 /**
497  * Remove a transmit handle from the transmission queue, if present.
498  *
499  * Safe to call even if not queued.
500  *
501  * @param th handle to the packet to be unqueued.
502  */
503 static void
504 remove_from_queue (struct GNUNET_CADET_TransmitHandle *th)
505 {
506   struct GNUNET_CADET_Handle *h = th->channel->cadet;
507
508   /* It might or might not have been queued (rarely not), but check anyway. */
509   if (NULL != th->next || h->th_tail == th)
510   {
511     GNUNET_CONTAINER_DLL_remove (h->th_head, h->th_tail, th);
512   }
513 }
514
515
516 /**
517  * Send an ack on the channel to confirm the processing of a message.
518  *
519  * @param ch Channel on which to send the ACK.
520  */
521 static void
522 send_ack (struct GNUNET_CADET_Channel *ch)
523 {
524   struct GNUNET_CADET_LocalAck *msg;
525   struct GNUNET_MQ_Envelope *env;
526
527   env = GNUNET_MQ_msg (msg,
528                        GNUNET_MESSAGE_TYPE_CADET_LOCAL_ACK);
529
530   LOG (GNUNET_ERROR_TYPE_DEBUG,
531        "Sending ACK on channel %X\n",
532        ch->ccn.channel_of_client);
533   msg->ccn = ch->ccn;
534   GNUNET_MQ_send (ch->cadet->mq,
535                   env);
536 }
537
538
539
540 /******************************************************************************/
541 /***********************      RECEIVE HANDLERS     ****************************/
542 /******************************************************************************/
543
544
545 /**
546  * Call the @a notify callback given to #GNUNET_CADET_notify_transmit_ready to
547  * request the data to send over MQ. Since MQ manages the queue, this function
548  * is scheduled immediatly after a transmit ready notification.
549  *
550  * @param cls Closure (transmit handle).
551  */
552 static void
553 request_data (void *cls)
554 {
555   struct GNUNET_CADET_TransmitHandle *th = cls;
556   struct GNUNET_CADET_LocalData *msg;
557   struct GNUNET_MQ_Envelope *env;
558   size_t osize;
559
560   LOG (GNUNET_ERROR_TYPE_DEBUG,
561        "Requesting Data: %u bytes\n",
562        th->size);
563
564   GNUNET_assert (0 < th->channel->allow_send);
565   th->channel->allow_send--;
566   /* NOTE: we may be allowed to send another packet immediately,
567      albeit the current logic waits for the ACK. */
568   th->request_data_task = NULL;
569   remove_from_queue (th);
570
571   env = GNUNET_MQ_msg_extra (msg,
572                              th->size,
573                              GNUNET_MESSAGE_TYPE_CADET_LOCAL_DATA);
574   msg->ccn = th->channel->ccn;
575   osize = th->notify (th->notify_cls,
576                       th->size,
577                       &msg[1]);
578   GNUNET_assert (osize == th->size);
579   GNUNET_MQ_send (th->channel->cadet->mq,
580                   env);
581   GNUNET_free (th);
582 }
583
584
585 /**
586  * Process the new channel notification and add it to the channels in the handle
587  *
588  * @param h     The cadet handle
589  * @param msg   A message with the details of the new incoming channel
590  */
591 static void
592 handle_channel_created (void *cls,
593                         const struct GNUNET_CADET_LocalChannelCreateMessage *msg)
594 {
595   struct GNUNET_CADET_Handle *h = cls;
596   struct GNUNET_CADET_Channel *ch;
597   struct GNUNET_CADET_Port *port;
598   const struct GNUNET_HashCode *port_number;
599   struct GNUNET_CADET_ClientChannelNumber ccn;
600
601   ccn = msg->ccn;
602   port_number = &msg->port;
603   LOG (GNUNET_ERROR_TYPE_DEBUG,
604        "Creating incoming channel %X [%s]\n",
605        ntohl (ccn.channel_of_client),
606        GNUNET_h2s (port_number));
607   if (ntohl (ccn.channel_of_client) >= GNUNET_CADET_LOCAL_CHANNEL_ID_CLI)
608   {
609     GNUNET_break (0);
610     return;
611   }
612   port = find_port (h, port_number);
613   if (NULL != port)
614   {
615     void *ctx;
616
617     ch = create_channel (h, ccn);
618     ch->peer = GNUNET_PEER_intern (&msg->peer);
619     ch->cadet = h;
620     ch->ccn = ccn;
621     ch->port = port;
622     ch->options = ntohl (msg->opt);
623
624     LOG (GNUNET_ERROR_TYPE_DEBUG, "  created channel %p\n", ch);
625     ctx = port->handler (port->cls, ch, &msg->peer, port->hash, ch->options);
626     if (NULL != ctx)
627       ch->ctx = ctx;
628     LOG (GNUNET_ERROR_TYPE_DEBUG, "User notified\n");
629   }
630   else
631   {
632     struct GNUNET_CADET_LocalChannelDestroyMessage *d_msg;
633     struct GNUNET_MQ_Envelope *env;
634
635     LOG (GNUNET_ERROR_TYPE_DEBUG, "No handler for incoming channels\n");
636     env = GNUNET_MQ_msg (d_msg,
637                          GNUNET_MESSAGE_TYPE_CADET_LOCAL_CHANNEL_DESTROY);
638     d_msg->ccn = msg->ccn;
639     GNUNET_MQ_send (h->mq, env);
640   }
641   return;
642 }
643
644
645 /**
646  * Process the channel destroy notification and free associated resources
647  *
648  * @param h     The cadet handle
649  * @param msg   A message with the details of the channel being destroyed
650  */
651 static void
652 handle_channel_destroy (void *cls,
653                         const struct GNUNET_CADET_LocalChannelDestroyMessage *msg)
654 {
655   struct GNUNET_CADET_Handle *h = cls;
656   struct GNUNET_CADET_Channel *ch;
657   struct GNUNET_CADET_ClientChannelNumber ccn;
658
659   ccn = msg->ccn;
660   LOG (GNUNET_ERROR_TYPE_DEBUG,
661        "Channel %X Destroy from service\n",
662        ntohl (ccn.channel_of_client));
663   ch = retrieve_channel (h,
664                          ccn);
665
666   if (NULL == ch)
667   {
668     LOG (GNUNET_ERROR_TYPE_DEBUG,
669          "channel %X unknown\n",
670          ntohl (ccn.channel_of_client));
671     return;
672   }
673   destroy_channel (ch,
674                    GNUNET_YES);
675 }
676
677
678 /**
679  * Check that message received from CADET service is well-formed.
680  *
681  * @param cls the `struct GNUNET_CADET_Handle`
682  * @param message the message we got
683  * @return #GNUNET_OK if the message is well-formed,
684  *         #GNUNET_SYSERR otherwise
685  */
686 static int
687 check_local_data (void *cls,
688                   const struct GNUNET_CADET_LocalData *message)
689 {
690   struct GNUNET_CADET_Handle *h = cls;
691   struct GNUNET_CADET_Channel *ch;
692   uint16_t size;
693
694   size = ntohs (message->header.size);
695   if (sizeof (*message) + sizeof (struct GNUNET_MessageHeader) > size)
696   {
697     GNUNET_break_op (0);
698     return GNUNET_SYSERR;
699   }
700
701   ch = retrieve_channel (h,
702                          message->ccn);
703   if (NULL == ch)
704   {
705     GNUNET_break_op (0);
706     return GNUNET_SYSERR;
707   }
708
709   return GNUNET_OK;
710 }
711
712
713 /**
714  * Process the incoming data packets, call appropriate handlers.
715  *
716  * @param h       The cadet handle
717  * @param message A message encapsulating the data
718  */
719 static void
720 handle_local_data (void *cls,
721                    const struct GNUNET_CADET_LocalData *message)
722 {
723   struct GNUNET_CADET_Handle *h = cls;
724   const struct GNUNET_MessageHeader *payload;
725   const struct GNUNET_CADET_MessageHandler *handler;
726   struct GNUNET_CADET_Channel *ch;
727   unsigned int i;
728   uint16_t type;
729
730   LOG (GNUNET_ERROR_TYPE_DEBUG,
731        "Got a data message!\n");
732   ch = retrieve_channel (h,
733                          message->ccn);
734   GNUNET_assert (NULL != ch);
735
736   payload = (struct GNUNET_MessageHeader *) &message[1];
737   LOG (GNUNET_ERROR_TYPE_DEBUG, "  %s data on channel %s [%X]\n",
738        GC_f2s (ntohl (ch->ccn.channel_of_client) >=
739                GNUNET_CADET_LOCAL_CHANNEL_ID_CLI),
740        GNUNET_i2s (GNUNET_PEER_resolve2 (ch->peer)),
741        ntohl (message->ccn.channel_of_client));
742
743   type = ntohs (payload->type);
744   LOG (GNUNET_ERROR_TYPE_DEBUG, "  payload type %s\n", GC_m2s (type));
745   for (i = 0; i < h->n_handlers; i++)
746   {
747     handler = &h->message_handlers[i];
748     LOG (GNUNET_ERROR_TYPE_DEBUG, "    checking handler for type %u\n",
749          handler->type);
750     if (handler->type == type)
751     {
752       if (GNUNET_OK !=
753           handler->callback (h->cls, ch, &ch->ctx, payload))
754       {
755         LOG (GNUNET_ERROR_TYPE_DEBUG, "callback caused disconnection\n");
756         GNUNET_CADET_channel_destroy (ch);
757         break;
758       }
759       else
760       {
761         LOG (GNUNET_ERROR_TYPE_DEBUG,
762              "callback completed successfully\n");
763         break;
764       }
765     }
766   }
767 }
768
769
770 /**
771  * Process a local ACK message, enabling the client to send
772  * more data to the service.
773  *
774  * @param h Cadet handle.
775  * @param message Message itself.
776  */
777 static void
778 handle_local_ack (void *cls,
779                   const struct GNUNET_CADET_LocalAck *message)
780 {
781   struct GNUNET_CADET_Handle *h = cls;
782   struct GNUNET_CADET_Channel *ch;
783   struct GNUNET_CADET_ClientChannelNumber ccn;
784   struct GNUNET_CADET_TransmitHandle *th;
785
786   ccn = message->ccn;
787   ch = retrieve_channel (h, ccn);
788   if (NULL == ch)
789   {
790     LOG (GNUNET_ERROR_TYPE_DEBUG,
791          "ACK on unknown channel %X\n",
792          ntohl (ccn.channel_of_client));
793     return;
794   }
795   LOG (GNUNET_ERROR_TYPE_DEBUG,
796        "Got an ACK on channel %X!\n",
797        ntohl (ch->ccn.channel_of_client));
798   ch->allow_send++;
799   LOG (GNUNET_ERROR_TYPE_DEBUG,
800        "Checking for pending data\n");
801   for (th = h->th_head; NULL != th; th = th->next)
802   {
803     if ( (th->channel == ch) &&
804          (NULL == th->request_data_task) )
805     {
806       th->request_data_task
807         = GNUNET_SCHEDULER_add_now (&request_data,
808                                     th);
809       break;
810     }
811   }
812 }
813
814 /**
815  * Reconnect to the service, retransmit all infomation to try to restore the
816  * original state.
817  *
818  * @param h handle to the cadet
819  *
820  * @return #GNUNET_YES in case of sucess, #GNUNET_NO otherwise (service down...)
821  */
822 static void
823 reconnect (struct GNUNET_CADET_Handle *h);
824
825
826 /**
827  * Reconnect callback: tries to reconnect again after a failer previous
828  * reconnection.
829  *
830  * @param cls closure (cadet handle)
831  */
832 static void
833 reconnect_cbk (void *cls);
834
835
836 /**
837  * Generic error handler, called with the appropriate error code and
838  * the same closure specified at the creation of the message queue.
839  * Not every message queue implementation supports an error handler.
840  *
841  * @param cls closure, a `struct GNUNET_CORE_Handle *`
842  * @param error error code
843  */
844 static void
845 handle_mq_error (void *cls,
846                  enum GNUNET_MQ_Error error)
847 {
848   struct GNUNET_CADET_Handle *h = cls;
849
850   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MQ ERROR: %u\n", error);
851   GNUNET_MQ_destroy (h->mq);
852   h->mq = NULL;
853   reconnect (h);
854 }
855
856
857 /*
858  * Process a local reply about info on all channels, pass info to the user.
859  *
860  * @param h Cadet handle.
861  * @param message Message itself.
862  */
863 // static void
864 // process_get_channels (struct GNUNET_CADET_Handle *h,
865 //                      const struct GNUNET_MessageHeader *message)
866 // {
867 //   struct GNUNET_CADET_LocalInfo *msg;
868 //
869 //   GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Get Channels messasge received\n");
870 //
871 //   if (NULL == h->channels_cb)
872 //   {
873 //     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "  ignored\n");
874 //     return;
875 //   }
876 //
877 //   msg = (struct GNUNET_CADET_LocalInfo *) message;
878 //   if (ntohs (message->size) !=
879 //       (sizeof (struct GNUNET_CADET_LocalInfo) +
880 //        sizeof (struct GNUNET_PeerIdentity)))
881 //   {
882 //     GNUNET_break_op (0);
883 //     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
884 //                 "Get channels message: size %hu - expected %u\n",
885 //                 ntohs (message->size),
886 //                 sizeof (struct GNUNET_CADET_LocalInfo));
887 //     return;
888 //   }
889 //   h->channels_cb (h->channels_cls,
890 //                   ntohl (msg->channel_id),
891 //                   &msg->owner,
892 //                   &msg->destination);
893 // }
894
895
896
897 /*
898  * Process a local monitor_channel reply, pass info to the user.
899  *
900  * @param h Cadet handle.
901  * @param message Message itself.
902  */
903 // static void
904 // process_show_channel (struct GNUNET_CADET_Handle *h,
905 //                      const struct GNUNET_MessageHeader *message)
906 // {
907 //   struct GNUNET_CADET_LocalInfo *msg;
908 //   size_t esize;
909 //
910 //   GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Show Channel messasge received\n");
911 //
912 //   if (NULL == h->channel_cb)
913 //   {
914 //     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "  ignored\n");
915 //     return;
916 //   }
917 //
918 //   /* Verify message sanity */
919 //   msg = (struct GNUNET_CADET_LocalInfo *) message;
920 //   esize = sizeof (struct GNUNET_CADET_LocalInfo);
921 //   if (ntohs (message->size) != esize)
922 //   {
923 //     GNUNET_break_op (0);
924 //     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
925 //                 "Show channel message: size %hu - expected %u\n",
926 //                 ntohs (message->size),
927 //                 esize);
928 //
929 //     h->channel_cb (h->channel_cls, NULL, NULL);
930 //     h->channel_cb = NULL;
931 //     h->channel_cls = NULL;
932 //
933 //     return;
934 //   }
935 //
936 //   h->channel_cb (h->channel_cls,
937 //                  &msg->destination,
938 //                  &msg->owner);
939 // }
940
941
942
943 /**
944  * Check that message received from CADET service is well-formed.
945  *
946  * @param cls the `struct GNUNET_CADET_Handle`
947  * @param message the message we got
948  * @return #GNUNET_OK if the message is well-formed,
949  *         #GNUNET_SYSERR otherwise
950  */
951 static int
952 check_get_peers (void *cls,
953                  const struct GNUNET_CADET_LocalInfoPeer *message)
954 {
955   struct GNUNET_CADET_Handle *h = cls;
956   uint16_t size;
957
958   if (NULL == h->info_cb.peers_cb)
959   {
960     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
961                 "  no handler for peesr monitor message!\n");
962     return GNUNET_SYSERR;
963   }
964
965   size = ntohs (message->header.size);
966   if (sizeof (struct GNUNET_CADET_LocalInfoPeer) > size)
967   {
968     h->info_cb.peers_cb (h->info_cls, NULL, -1, 0, 0);
969     h->info_cb.peers_cb = NULL;
970     h->info_cls = NULL;
971     return GNUNET_SYSERR;
972   }
973
974   return GNUNET_OK;
975 }
976
977
978 /**
979  * Process a local reply about info on all tunnels, pass info to the user.
980  *
981  * @param cls Closure (Cadet handle).
982  * @param msg Message itself.
983  */
984 static void
985 handle_get_peers (void *cls,
986                   const struct GNUNET_CADET_LocalInfoPeer *msg)
987 {
988   struct GNUNET_CADET_Handle *h = cls;
989   h->info_cb.peers_cb (h->info_cls, &msg->destination,
990                        (int) ntohs (msg->tunnel),
991                        (unsigned int ) ntohs (msg->paths),
992                        0);
993 }
994
995
996 /**
997  * Check that message received from CADET service is well-formed.
998  *
999  * @param cls the `struct GNUNET_CADET_Handle`
1000  * @param message the message we got
1001  * @return #GNUNET_OK if the message is well-formed,
1002  *         #GNUNET_SYSERR otherwise
1003  */
1004 static int
1005 check_get_peer (void *cls,
1006                 const struct GNUNET_CADET_LocalInfoPeer *message)
1007 {
1008   struct GNUNET_CADET_Handle *h = cls;
1009   const size_t msize = sizeof (struct GNUNET_CADET_LocalInfoPeer);
1010   struct GNUNET_PeerIdentity *paths_array;
1011   size_t esize;
1012   unsigned int epaths;
1013   unsigned int paths;
1014   unsigned int peers;
1015
1016   if (NULL == h->info_cb.peer_cb)
1017   {
1018     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1019                 "  no handler for peer monitor message!\n");
1020     goto clean_cls;
1021   }
1022
1023   /* Verify message sanity */
1024   esize = ntohs (message->header.size);
1025   if (esize < msize)
1026   {
1027     GNUNET_break_op (0);
1028     h->info_cb.peer_cb (h->info_cls, NULL, 0, 0, 0, NULL);
1029     goto clean_cls;
1030   }
1031   if (0 != ((esize - msize) % sizeof (struct GNUNET_PeerIdentity)))
1032   {
1033     GNUNET_break_op (0);
1034     h->info_cb.peer_cb (h->info_cls, NULL, 0, 0, 0, NULL);
1035     goto clean_cls;
1036
1037   }
1038   peers = (esize - msize) / sizeof (struct GNUNET_PeerIdentity);
1039   epaths = (unsigned int) ntohs (message->paths);
1040   paths_array = (struct GNUNET_PeerIdentity *) &message[1];
1041   paths = 0;
1042   for (int i = 0; i < peers; i++)
1043   {
1044     if (0 == memcmp (&paths_array[i], &message->destination,
1045                      sizeof (struct GNUNET_PeerIdentity)))
1046     {
1047       paths++;
1048     }
1049   }
1050   if (paths != epaths)
1051   {
1052     GNUNET_break_op (0);
1053     GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "p:%u, e: %u\n", paths, epaths);
1054     h->info_cb.peer_cb (h->info_cls, NULL, 0, 0, 0, NULL);
1055     goto clean_cls;
1056   }
1057
1058   return GNUNET_OK;
1059
1060 clean_cls:
1061   h->info_cb.peer_cb = NULL;
1062   h->info_cls = NULL;
1063   return GNUNET_SYSERR;
1064 }
1065
1066
1067 /**
1068  * Process a local peer info reply, pass info to the user.
1069  *
1070  * @param cls Closure (Cadet handle).
1071  * @param message Message itself.
1072  */
1073 static void
1074 handle_get_peer (void *cls,
1075                  const struct GNUNET_CADET_LocalInfoPeer *message)
1076 {
1077   struct GNUNET_CADET_Handle *h = cls;
1078   struct GNUNET_PeerIdentity *paths_array;
1079   unsigned int paths;
1080   unsigned int path_length;
1081   int neighbor;
1082   unsigned int peers;
1083
1084   paths = (unsigned int) ntohs (message->paths);
1085   paths_array = (struct GNUNET_PeerIdentity *) &message[1];
1086   peers = (ntohs (message->header.size) - sizeof (*message))
1087           / sizeof (struct GNUNET_PeerIdentity);
1088   path_length = 0;
1089   neighbor = GNUNET_NO;
1090
1091   for (int i = 0; i < peers; i++)
1092   {
1093     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " %s\n", GNUNET_i2s (&paths_array[i]));
1094     path_length++;
1095     if (0 == memcmp (&paths_array[i], &message->destination,
1096                      sizeof (struct GNUNET_PeerIdentity)))
1097     {
1098       if (1 == path_length)
1099         neighbor = GNUNET_YES;
1100       path_length = 0;
1101     }
1102   }
1103
1104   /* Call Callback with tunnel info. */
1105   paths_array = (struct GNUNET_PeerIdentity *) &message[1];
1106   h->info_cb.peer_cb (h->info_cls,
1107                       &message->destination,
1108                       (int) ntohs (message->tunnel),
1109                       neighbor,
1110                       paths,
1111                       paths_array);
1112 }
1113
1114
1115 /**
1116  * Check that message received from CADET service is well-formed.
1117  *
1118  * @param cls the `struct GNUNET_CADET_Handle`
1119  * @param msg the message we got
1120  * @return #GNUNET_OK if the message is well-formed,
1121  *         #GNUNET_SYSERR otherwise
1122  */
1123 static int
1124 check_get_tunnels (void *cls,
1125                    const struct GNUNET_CADET_LocalInfoTunnel *msg)
1126 {
1127   struct GNUNET_CADET_Handle *h = cls;
1128   uint16_t size;
1129
1130   if (NULL == h->info_cb.tunnels_cb)
1131   {
1132     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1133                 "  no handler for tunnels monitor message!\n");
1134     return GNUNET_SYSERR;
1135   }
1136
1137   size = ntohs (msg->header.size);
1138   if (sizeof (struct GNUNET_CADET_LocalInfoTunnel) > size)
1139   {
1140     h->info_cb.tunnels_cb (h->info_cls, NULL, 0, 0, 0, 0);
1141     h->info_cb.tunnels_cb = NULL;
1142     h->info_cls = NULL;
1143     return GNUNET_SYSERR;
1144   }
1145   return GNUNET_OK;
1146 }
1147
1148
1149 /**
1150  * Process a local reply about info on all tunnels, pass info to the user.
1151  *
1152  * @param cls Closure (Cadet handle).
1153  * @param message Message itself.
1154  */
1155 static void
1156 handle_get_tunnels (void *cls,
1157                     const struct GNUNET_CADET_LocalInfoTunnel *msg)
1158 {
1159   struct GNUNET_CADET_Handle *h = cls;
1160
1161   h->info_cb.tunnels_cb (h->info_cls,
1162                          &msg->destination,
1163                          ntohl (msg->channels),
1164                          ntohl (msg->connections),
1165                          ntohs (msg->estate),
1166                          ntohs (msg->cstate));
1167
1168 }
1169
1170
1171 /**
1172  * Check that message received from CADET service is well-formed.
1173  *
1174  * @param cls the `struct GNUNET_CADET_Handle`
1175  * @param msg the message we got
1176  * @return #GNUNET_OK if the message is well-formed,
1177  *         #GNUNET_SYSERR otherwise
1178  */
1179 static int
1180 check_get_tunnel (void *cls,
1181                   const struct GNUNET_CADET_LocalInfoTunnel *msg)
1182 {
1183   struct GNUNET_CADET_Handle *h = cls;
1184   unsigned int ch_n;
1185   unsigned int c_n;
1186   size_t esize;
1187   size_t msize;
1188
1189   if (NULL == h->info_cb.tunnel_cb)
1190   {
1191     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1192                 "  no handler for tunnel monitor message!\n");
1193     goto clean_cls;
1194   }
1195
1196   /* Verify message sanity */
1197   msize = ntohs (msg->header.size);
1198   esize = sizeof (struct GNUNET_CADET_LocalInfoTunnel);
1199   if (esize > msize)
1200   {
1201     GNUNET_break_op (0);
1202     h->info_cb.tunnel_cb (h->info_cls,
1203                           NULL, 0, 0, NULL, NULL, 0, 0);
1204     goto clean_cls;
1205   }
1206   ch_n = ntohl (msg->channels);
1207   c_n = ntohl (msg->connections);
1208   esize += ch_n * sizeof (struct GNUNET_CADET_ChannelTunnelNumber);
1209   esize += c_n * sizeof (struct GNUNET_CADET_ConnectionTunnelIdentifier);
1210   if (msize != esize)
1211   {
1212     GNUNET_break_op (0);
1213     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1214                 "m:%u, e: %u (%u ch, %u conn)\n",
1215                 (unsigned int) msize,
1216                 (unsigned int) esize,
1217                 ch_n,
1218                 c_n);
1219     h->info_cb.tunnel_cb (h->info_cls,
1220                           NULL, 0, 0, NULL, NULL, 0, 0);
1221     goto clean_cls;
1222   }
1223
1224   return GNUNET_OK;
1225
1226 clean_cls:
1227   h->info_cb.tunnel_cb = NULL;
1228   h->info_cls = NULL;
1229   return GNUNET_SYSERR;
1230 }
1231
1232
1233 /**
1234  * Process a local tunnel info reply, pass info to the user.
1235  *
1236  * @param cls Closure (Cadet handle).
1237  * @param msg Message itself.
1238  */
1239 static void
1240 handle_get_tunnel (void *cls,
1241                    const struct GNUNET_CADET_LocalInfoTunnel *msg)
1242 {
1243   struct GNUNET_CADET_Handle *h = cls;
1244   unsigned int ch_n;
1245   unsigned int c_n;
1246   const struct GNUNET_CADET_ConnectionTunnelIdentifier *conns;
1247   const struct GNUNET_CADET_ChannelTunnelNumber *chns;
1248
1249   ch_n = ntohl (msg->channels);
1250   c_n = ntohl (msg->connections);
1251
1252   /* Call Callback with tunnel info. */
1253   conns = (const struct GNUNET_CADET_ConnectionTunnelIdentifier *) &msg[1];
1254   chns = (const struct GNUNET_CADET_ChannelTunnelNumber *) &conns[c_n];
1255   h->info_cb.tunnel_cb (h->info_cls,
1256                         &msg->destination,
1257                         ch_n,
1258                         c_n,
1259                         chns,
1260                         conns,
1261                         ntohs (msg->estate),
1262                         ntohs (msg->cstate));
1263 }
1264
1265
1266
1267 /**
1268  * Reconnect to the service, retransmit all infomation to try to restore the
1269  * original state.
1270  *
1271  * @param h handle to the cadet
1272  *
1273  * @return GNUNET_YES in case of sucess, GNUNET_NO otherwise (service down...)
1274  */
1275 static int
1276 do_reconnect (struct GNUNET_CADET_Handle *h)
1277 {
1278   struct GNUNET_MQ_MessageHandler handlers[] = {
1279     GNUNET_MQ_hd_fixed_size (channel_created,
1280                              GNUNET_MESSAGE_TYPE_CADET_LOCAL_CHANNEL_CREATE,
1281                              struct GNUNET_CADET_LocalChannelCreateMessage,
1282                              h),
1283     GNUNET_MQ_hd_fixed_size (channel_destroy,
1284                              GNUNET_MESSAGE_TYPE_CADET_LOCAL_CHANNEL_DESTROY,
1285                              struct GNUNET_CADET_LocalChannelDestroyMessage,
1286                              h),
1287     GNUNET_MQ_hd_var_size (local_data,
1288                            GNUNET_MESSAGE_TYPE_CADET_LOCAL_DATA,
1289                            struct GNUNET_CADET_LocalData,
1290                            h),
1291     GNUNET_MQ_hd_fixed_size (local_ack,
1292                              GNUNET_MESSAGE_TYPE_CADET_LOCAL_ACK,
1293                              struct GNUNET_CADET_LocalAck,
1294                              h),
1295     GNUNET_MQ_hd_var_size (get_peers,
1296                            GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEERS,
1297                            struct GNUNET_CADET_LocalInfoPeer,
1298                            h),
1299     GNUNET_MQ_hd_var_size (get_peer,
1300                            GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEER,
1301                            struct GNUNET_CADET_LocalInfoPeer,
1302                            h),
1303     GNUNET_MQ_hd_var_size (get_tunnels,
1304                            GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNELS,
1305                            struct GNUNET_CADET_LocalInfoTunnel,
1306                            h),
1307     GNUNET_MQ_hd_var_size (get_tunnel,
1308                            GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNEL,
1309                            struct GNUNET_CADET_LocalInfoTunnel,
1310                            h),
1311   // FIXME
1312 //   GNUNET_MQ_hd_fixed_Y       size (channel_destroyed,
1313 //                            GNUNET_MESSAGE_TYPE_CADET_CHANNEL_OPEN_NACK_DEPRECATED,
1314 //                            struct GNUNET_CADET_ChannelDestroyMessage);
1315     GNUNET_MQ_handler_end ()
1316   };
1317
1318   LOG (GNUNET_ERROR_TYPE_DEBUG, "Connecting to CADET\n");
1319
1320   GNUNET_assert (NULL == h->mq);
1321   h->mq = GNUNET_CLIENT_connect (h->cfg,
1322                                  "cadet",
1323                                  handlers,
1324                                  &handle_mq_error,
1325                                  h);
1326   if (NULL == h->mq)
1327   {
1328     reconnect (h);
1329     return GNUNET_NO;
1330   }
1331   else
1332   {
1333     h->reconnect_time = GNUNET_TIME_UNIT_MILLISECONDS;
1334   }
1335   return GNUNET_YES;
1336 }
1337
1338 /**
1339  * Reconnect callback: tries to reconnect again after a failer previous
1340  * reconnecttion
1341  *
1342  * @param cls closure (cadet handle)
1343  */
1344 static void
1345 reconnect_cbk (void *cls)
1346 {
1347   struct GNUNET_CADET_Handle *h = cls;
1348
1349   h->reconnect_task = NULL;
1350   do_reconnect (h);
1351 }
1352
1353
1354 /**
1355  * Reconnect to the service, retransmit all infomation to try to restore the
1356  * original state.
1357  *
1358  * @param h handle to the cadet
1359  *
1360  * @return #GNUNET_YES in case of sucess, #GNUNET_NO otherwise (service down...)
1361  */
1362 static void
1363 reconnect (struct GNUNET_CADET_Handle *h)
1364 {
1365   struct GNUNET_CADET_Channel *ch;
1366
1367   LOG (GNUNET_ERROR_TYPE_DEBUG,
1368        "Requested RECONNECT, destroying all channels\n");
1369   while (NULL != (ch = h->channels_head))
1370     destroy_channel (ch, GNUNET_YES);
1371   if (NULL == h->reconnect_task)
1372     h->reconnect_task = GNUNET_SCHEDULER_add_delayed (h->reconnect_time,
1373                                                       &reconnect_cbk, h);
1374 }
1375
1376
1377 /******************************************************************************/
1378 /**********************      API CALL DEFINITIONS     *************************/
1379 /******************************************************************************/
1380
1381 struct GNUNET_CADET_Handle *
1382 GNUNET_CADET_connect (const struct GNUNET_CONFIGURATION_Handle *cfg,
1383                       void *cls,
1384                       GNUNET_CADET_ChannelEndHandler cleaner,
1385                       const struct GNUNET_CADET_MessageHandler *handlers)
1386 {
1387   struct GNUNET_CADET_Handle *h;
1388
1389   LOG (GNUNET_ERROR_TYPE_DEBUG, "GNUNET_CADET_connect()\n");
1390   h = GNUNET_new (struct GNUNET_CADET_Handle);
1391   LOG (GNUNET_ERROR_TYPE_DEBUG, " addr %p\n", h);
1392   h->cfg = cfg;
1393   h->cleaner = cleaner;
1394   h->ports = GNUNET_CONTAINER_multihashmap_create (4, GNUNET_YES);
1395   do_reconnect (h);
1396   if (h->mq == NULL)
1397   {
1398     GNUNET_break (0);
1399     GNUNET_CADET_disconnect (h);
1400     return NULL;
1401   }
1402   h->cls = cls;
1403   h->message_handlers = handlers;
1404   h->next_ccn.channel_of_client = htonl (GNUNET_CADET_LOCAL_CHANNEL_ID_CLI);
1405   h->reconnect_time = GNUNET_TIME_UNIT_MILLISECONDS;
1406   h->reconnect_task = NULL;
1407
1408   /* count handlers */
1409   for (h->n_handlers = 0;
1410        handlers && handlers[h->n_handlers].type;
1411        h->n_handlers++) ;
1412   LOG (GNUNET_ERROR_TYPE_DEBUG, "GNUNET_CADET_connect() END\n");
1413   return h;
1414 }
1415
1416
1417 /**
1418  * Disconnect from the cadet service. All channels will be destroyed. All channel
1419  * disconnect callbacks will be called on any still connected peers, notifying
1420  * about their disconnection. The registered inbound channel cleaner will be
1421  * called should any inbound channels still exist.
1422  *
1423  * @param handle connection to cadet to disconnect
1424  */
1425 void
1426 GNUNET_CADET_disconnect (struct GNUNET_CADET_Handle *handle)
1427 {
1428   struct GNUNET_CADET_Channel *ch;
1429   struct GNUNET_CADET_Channel *aux;
1430   struct GNUNET_CADET_TransmitHandle *th;
1431
1432   LOG (GNUNET_ERROR_TYPE_DEBUG,
1433        "CADET DISCONNECT\n");
1434   ch = handle->channels_head;
1435   while (NULL != ch)
1436   {
1437     aux = ch->next;
1438     if (ntohl (ch->ccn.channel_of_client) >= GNUNET_CADET_LOCAL_CHANNEL_ID_CLI)
1439     {
1440       GNUNET_break (0);
1441       LOG (GNUNET_ERROR_TYPE_DEBUG,
1442            "channel %X not destroyed\n",
1443            ntohl (ch->ccn.channel_of_client));
1444     }
1445     destroy_channel (ch,
1446                      GNUNET_YES);
1447     ch = aux;
1448   }
1449   while (NULL != (th = handle->th_head))
1450   {
1451     struct GNUNET_MessageHeader *msg;
1452
1453     /* Make sure it is an allowed packet (everything else should have been
1454      * already canceled).
1455      */
1456     GNUNET_break (GNUNET_NO == th_is_payload (th));
1457     msg = (struct GNUNET_MessageHeader *) &th[1];
1458     switch (ntohs(msg->type))
1459     {
1460       case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_OPEN:
1461       case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DESTROY:
1462       case GNUNET_MESSAGE_TYPE_CADET_LOCAL_PORT_OPEN:
1463       case GNUNET_MESSAGE_TYPE_CADET_LOCAL_PORT_CLOSE:
1464       case GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_CHANNELS:
1465       case GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_CHANNEL:
1466       case GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEER:
1467       case GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEERS:
1468       case GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNEL:
1469       case GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNELS:
1470         break;
1471       default:
1472         GNUNET_break (0);
1473         LOG (GNUNET_ERROR_TYPE_ERROR, "unexpected unsent msg %s\n",
1474              GC_m2s (ntohs(msg->type)));
1475     }
1476
1477     GNUNET_CADET_notify_transmit_ready_cancel (th);
1478   }
1479
1480   if (NULL != handle->mq)
1481   {
1482     GNUNET_MQ_destroy (handle->mq);
1483     handle->mq = NULL;
1484   }
1485   if (NULL != handle->reconnect_task)
1486   {
1487     GNUNET_SCHEDULER_cancel(handle->reconnect_task);
1488     handle->reconnect_task = NULL;
1489   }
1490
1491   GNUNET_CONTAINER_multihashmap_destroy (handle->ports);
1492   handle->ports = NULL;
1493   GNUNET_free (handle);
1494 }
1495
1496
1497 /**
1498  * Open a port to receive incomming channels.
1499  *
1500  * @param h CADET handle.
1501  * @param port Hash representing the port number.
1502  * @param new_channel Function called when an channel is received.
1503  * @param new_channel_cls Closure for @a new_channel.
1504  *
1505  * @return Port handle.
1506  */
1507 struct GNUNET_CADET_Port *
1508 GNUNET_CADET_open_port (struct GNUNET_CADET_Handle *h,
1509                         const struct GNUNET_HashCode *port,
1510                         GNUNET_CADET_InboundChannelNotificationHandler
1511                             new_channel,
1512                         void *new_channel_cls)
1513 {
1514   struct GNUNET_CADET_PortMessage *msg;
1515   struct GNUNET_MQ_Envelope *env;
1516   struct GNUNET_CADET_Port *p;
1517
1518   GNUNET_assert (NULL != new_channel);
1519   p = GNUNET_new (struct GNUNET_CADET_Port);
1520   p->cadet = h;
1521   p->hash = GNUNET_new (struct GNUNET_HashCode);
1522   *p->hash = *port;
1523   p->handler = new_channel;
1524   p->cls = new_channel_cls;
1525   GNUNET_assert (GNUNET_OK ==
1526                  GNUNET_CONTAINER_multihashmap_put (h->ports,
1527                                                     p->hash,
1528                                                     p,
1529                                                     GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1530
1531   env = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_CADET_LOCAL_PORT_OPEN);
1532   msg->port = *p->hash;
1533   GNUNET_MQ_send (h->mq, env);
1534
1535   return p;
1536 }
1537
1538 /**
1539  * Close a port opened with @a GNUNET_CADET_open_port.
1540  * The @a new_channel callback will no longer be called.
1541  *
1542  * @param p Port handle.
1543  */
1544 void
1545 GNUNET_CADET_close_port (struct GNUNET_CADET_Port *p)
1546 {
1547   struct GNUNET_CADET_PortMessage *msg;
1548   struct GNUNET_MQ_Envelope *env;
1549
1550   env = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_CADET_LOCAL_PORT_CLOSE);
1551
1552   msg->port = *p->hash;
1553   GNUNET_MQ_send (p->cadet->mq, env);
1554   GNUNET_CONTAINER_multihashmap_remove (p->cadet->ports, p->hash, p);
1555   GNUNET_free (p->hash);
1556   GNUNET_free (p);
1557 }
1558
1559
1560 /**
1561  * Create a new channel towards a remote peer.
1562  *
1563  * If the destination port is not open by any peer or the destination peer
1564  * does not accept the channel, #GNUNET_CADET_ChannelEndHandler will be called
1565  * for this channel.
1566  *
1567  * @param h cadet handle
1568  * @param channel_ctx client's channel context to associate with the channel
1569  * @param peer peer identity the channel should go to
1570  * @param port Port hash (port number).
1571  * @param options CadetOption flag field, with all desired option bits set to 1.
1572  * @return handle to the channel
1573  */
1574 struct GNUNET_CADET_Channel *
1575 GNUNET_CADET_channel_create (struct GNUNET_CADET_Handle *h,
1576                             void *channel_ctx,
1577                             const struct GNUNET_PeerIdentity *peer,
1578                             const struct GNUNET_HashCode *port,
1579                             enum GNUNET_CADET_ChannelOption options)
1580 {
1581   struct GNUNET_CADET_LocalChannelCreateMessage *msg;
1582   struct GNUNET_MQ_Envelope *env;
1583   struct GNUNET_CADET_Channel *ch;
1584   struct GNUNET_CADET_ClientChannelNumber ccn;
1585
1586   LOG (GNUNET_ERROR_TYPE_DEBUG,
1587        "Creating new channel to %s:%u\n",
1588        GNUNET_i2s (peer), port);
1589   ccn.channel_of_client = htonl (0);
1590   ch = create_channel (h, ccn);
1591   LOG (GNUNET_ERROR_TYPE_DEBUG, "  at %p\n", ch);
1592   LOG (GNUNET_ERROR_TYPE_DEBUG, "  number %X\n",
1593        ntohl (ch->ccn.channel_of_client));
1594   ch->ctx = channel_ctx;
1595   ch->peer = GNUNET_PEER_intern (peer);
1596
1597   env = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_CADET_LOCAL_CHANNEL_CREATE);
1598   msg->ccn = ch->ccn;
1599   msg->port = *port;
1600   msg->peer = *peer;
1601   msg->opt = htonl (options);
1602   GNUNET_MQ_send (h->mq,
1603                   env);
1604   return ch;
1605 }
1606
1607
1608 void
1609 GNUNET_CADET_channel_destroy (struct GNUNET_CADET_Channel *channel)
1610 {
1611   struct GNUNET_CADET_Handle *h;
1612   struct GNUNET_CADET_LocalChannelDestroyMessage *msg;
1613   struct GNUNET_MQ_Envelope *env;
1614   struct GNUNET_CADET_TransmitHandle *th;
1615   struct GNUNET_CADET_TransmitHandle *next;
1616
1617   LOG (GNUNET_ERROR_TYPE_DEBUG, "Destroying channel\n");
1618   h = channel->cadet;
1619   for  (th = h->th_head; th != NULL; th = next)
1620   {
1621     next = th->next;
1622     if (th->channel == channel)
1623     {
1624       GNUNET_break (0);
1625       if (GNUNET_YES == th_is_payload (th))
1626       {
1627         /* applications should cancel before destroying channel */
1628         LOG (GNUNET_ERROR_TYPE_WARNING,
1629              "Channel destroyed without cancelling transmission requests\n");
1630         th->notify (th->notify_cls, 0, NULL);
1631       }
1632       else
1633       {
1634         LOG (GNUNET_ERROR_TYPE_WARNING, "no meta-traffic should be queued\n");
1635       }
1636       GNUNET_CONTAINER_DLL_remove (h->th_head,
1637                                    h->th_tail,
1638                                    th);
1639       GNUNET_CADET_notify_transmit_ready_cancel (th);
1640     }
1641   }
1642
1643   env = GNUNET_MQ_msg (msg,
1644                        GNUNET_MESSAGE_TYPE_CADET_LOCAL_CHANNEL_DESTROY);
1645   msg->ccn = channel->ccn;
1646   GNUNET_MQ_send (h->mq,
1647                   env);
1648
1649   destroy_channel (channel,
1650                    GNUNET_YES);
1651 }
1652
1653
1654 /**
1655  * Get information about a channel.
1656  *
1657  * @param channel Channel handle.
1658  * @param option Query (GNUNET_CADET_OPTION_*).
1659  * @param ... dependant on option, currently not used
1660  *
1661  * @return Union with an answer to the query.
1662  */
1663 const union GNUNET_CADET_ChannelInfo *
1664 GNUNET_CADET_channel_get_info (struct GNUNET_CADET_Channel *channel,
1665                               enum GNUNET_CADET_ChannelOption option, ...)
1666 {
1667   static int bool_flag;
1668   const union GNUNET_CADET_ChannelInfo *ret;
1669
1670   switch (option)
1671   {
1672     case GNUNET_CADET_OPTION_NOBUFFER:
1673     case GNUNET_CADET_OPTION_RELIABLE:
1674     case GNUNET_CADET_OPTION_OUT_OF_ORDER:
1675       if (0 != (option & channel->options))
1676         bool_flag = GNUNET_YES;
1677       else
1678         bool_flag = GNUNET_NO;
1679       ret = (const union GNUNET_CADET_ChannelInfo *) &bool_flag;
1680       break;
1681     case GNUNET_CADET_OPTION_PEER:
1682       ret = (const union GNUNET_CADET_ChannelInfo *) GNUNET_PEER_resolve2 (channel->peer);
1683       break;
1684     default:
1685       GNUNET_break (0);
1686       return NULL;
1687   }
1688
1689   return ret;
1690 }
1691
1692
1693 struct GNUNET_CADET_TransmitHandle *
1694 GNUNET_CADET_notify_transmit_ready (struct GNUNET_CADET_Channel *channel,
1695                                     int cork,
1696                                     struct GNUNET_TIME_Relative maxdelay,
1697                                     size_t notify_size,
1698                                     GNUNET_CONNECTION_TransmitReadyNotify notify,
1699                                     void *notify_cls)
1700 {
1701   struct GNUNET_CADET_TransmitHandle *th;
1702
1703   GNUNET_assert (NULL != channel);
1704   GNUNET_assert (GNUNET_CONSTANTS_MAX_CADET_MESSAGE_SIZE >= notify_size);
1705   LOG (GNUNET_ERROR_TYPE_DEBUG, "CADET NOTIFY TRANSMIT READY\n");
1706   LOG (GNUNET_ERROR_TYPE_DEBUG, "    on channel %X\n", channel->ccn);
1707   LOG (GNUNET_ERROR_TYPE_DEBUG, "    allow_send %d\n", channel->allow_send);
1708   if (ntohl (channel->ccn.channel_of_client) >=
1709       GNUNET_CADET_LOCAL_CHANNEL_ID_CLI)
1710     LOG (GNUNET_ERROR_TYPE_DEBUG, "    to origin\n");
1711   else
1712     LOG (GNUNET_ERROR_TYPE_DEBUG, "    to destination\n");
1713   LOG (GNUNET_ERROR_TYPE_DEBUG, "    payload size %u\n", notify_size);
1714   GNUNET_assert (NULL != notify);
1715   if (GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us != maxdelay.rel_value_us)
1716   {
1717     LOG (GNUNET_ERROR_TYPE_WARNING,
1718          "CADET transmit ready timeout is deprected (has no effect)\n");
1719   }
1720
1721   th = GNUNET_new (struct GNUNET_CADET_TransmitHandle);
1722   th->channel = channel;
1723   th->size = notify_size;
1724   LOG (GNUNET_ERROR_TYPE_DEBUG, "    total size %u\n", th->size);
1725   th->notify = notify;
1726   th->notify_cls = notify_cls;
1727   if (0 != channel->allow_send)
1728     th->request_data_task = GNUNET_SCHEDULER_add_now (&request_data, th);
1729   else
1730     add_to_queue (channel->cadet, th);
1731
1732   LOG (GNUNET_ERROR_TYPE_DEBUG, "CADET NOTIFY TRANSMIT READY END\n");
1733   return th;
1734 }
1735
1736
1737 void
1738 GNUNET_CADET_notify_transmit_ready_cancel (struct GNUNET_CADET_TransmitHandle *th)
1739 {
1740   if (NULL != th->request_data_task)
1741   {
1742     GNUNET_SCHEDULER_cancel (th->request_data_task);
1743     th->request_data_task = NULL;
1744   }
1745   remove_from_queue (th);
1746   GNUNET_free (th);
1747 }
1748
1749
1750 void
1751 GNUNET_CADET_receive_done (struct GNUNET_CADET_Channel *channel)
1752 {
1753   send_ack (channel);
1754 }
1755
1756
1757 static void
1758 send_info_request (struct GNUNET_CADET_Handle *h, uint16_t type)
1759 {
1760   struct GNUNET_MessageHeader *msg;
1761   struct GNUNET_MQ_Envelope *env;
1762
1763   LOG (GNUNET_ERROR_TYPE_DEBUG,
1764        " Sending %s monitor message to service\n",
1765        GC_m2s(type));
1766
1767   env = GNUNET_MQ_msg (msg, type);
1768   GNUNET_MQ_send (h->mq, env);
1769 }
1770
1771
1772 /**
1773  * Request a debug dump on the service's STDERR.
1774  *
1775  * WARNING: unstable API, likely to change in the future!
1776  *
1777  * @param h cadet handle
1778  */
1779 void
1780 GNUNET_CADET_request_dump (struct GNUNET_CADET_Handle *h)
1781 {
1782   LOG (GNUNET_ERROR_TYPE_DEBUG, "requesting dump\n");
1783   send_info_request (h, GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_DUMP);
1784 }
1785
1786
1787 /**
1788  * Request information about peers known to the running cadet service.
1789  * The callback will be called for every peer known to the service.
1790  * Only one info request (of any kind) can be active at once.
1791  *
1792  *
1793  * WARNING: unstable API, likely to change in the future!
1794  *
1795  * @param h Handle to the cadet peer.
1796  * @param callback Function to call with the requested data.
1797  * @param callback_cls Closure for @c callback.
1798  *
1799  * @return #GNUNET_OK / #GNUNET_SYSERR
1800  */
1801 int
1802 GNUNET_CADET_get_peers (struct GNUNET_CADET_Handle *h,
1803                        GNUNET_CADET_PeersCB callback,
1804                        void *callback_cls)
1805 {
1806   if (NULL != h->info_cb.peers_cb)
1807   {
1808     GNUNET_break (0);
1809     return GNUNET_SYSERR;
1810   }
1811   send_info_request (h, GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEERS);
1812   h->info_cb.peers_cb = callback;
1813   h->info_cls = callback_cls;
1814   return GNUNET_OK;
1815 }
1816
1817
1818 /**
1819  * Cancel a peer info request. The callback will not be called (anymore).
1820  *
1821  * WARNING: unstable API, likely to change in the future!
1822  *
1823  * @param h Cadet handle.
1824  *
1825  * @return Closure given to GNUNET_CADET_get_peers.
1826  */
1827 void *
1828 GNUNET_CADET_get_peers_cancel (struct GNUNET_CADET_Handle *h)
1829 {
1830   void *cls;
1831
1832   cls = h->info_cls;
1833   h->info_cb.peers_cb = NULL;
1834   h->info_cls = NULL;
1835   return cls;
1836 }
1837
1838
1839 /**
1840  * Request information about a peer known to the running cadet peer.
1841  * The callback will be called for the tunnel once.
1842  * Only one info request (of any kind) can be active at once.
1843  *
1844  * WARNING: unstable API, likely to change in the future!
1845  *
1846  * @param h Handle to the cadet peer.
1847  * @param id Peer whose tunnel to examine.
1848  * @param callback Function to call with the requested data.
1849  * @param callback_cls Closure for @c callback.
1850  *
1851  * @return #GNUNET_OK / #GNUNET_SYSERR
1852  */
1853 int
1854 GNUNET_CADET_get_peer (struct GNUNET_CADET_Handle *h,
1855                        const struct GNUNET_PeerIdentity *id,
1856                        GNUNET_CADET_PeerCB callback,
1857                        void *callback_cls)
1858 {
1859   struct GNUNET_CADET_LocalInfo *msg;
1860   struct GNUNET_MQ_Envelope *env;
1861
1862   if (NULL != h->info_cb.peer_cb)
1863   {
1864     GNUNET_break (0);
1865     return GNUNET_SYSERR;
1866   }
1867
1868   env = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEER);
1869   msg->peer = *id;
1870   GNUNET_MQ_send (h->mq, env);
1871
1872   h->info_cb.peer_cb = callback;
1873   h->info_cls = callback_cls;
1874   return GNUNET_OK;
1875 }
1876
1877
1878 /**
1879  * Request information about tunnels of the running cadet peer.
1880  * The callback will be called for every tunnel of the service.
1881  * Only one info request (of any kind) can be active at once.
1882  *
1883  * WARNING: unstable API, likely to change in the future!
1884  *
1885  * @param h Handle to the cadet peer.
1886  * @param callback Function to call with the requested data.
1887  * @param callback_cls Closure for @c callback.
1888  *
1889  * @return #GNUNET_OK / #GNUNET_SYSERR
1890  */
1891 int
1892 GNUNET_CADET_get_tunnels (struct GNUNET_CADET_Handle *h,
1893                          GNUNET_CADET_TunnelsCB callback,
1894                          void *callback_cls)
1895 {
1896   if (NULL != h->info_cb.tunnels_cb)
1897   {
1898     GNUNET_break (0);
1899     return GNUNET_SYSERR;
1900   }
1901   send_info_request (h, GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNELS);
1902   h->info_cb.tunnels_cb = callback;
1903   h->info_cls = callback_cls;
1904   return GNUNET_OK;
1905 }
1906
1907
1908 /**
1909  * Cancel a monitor request. The monitor callback will not be called.
1910  *
1911  * @param h Cadet handle.
1912  *
1913  * @return Closure given to GNUNET_CADET_get_tunnels.
1914  */
1915 void *
1916 GNUNET_CADET_get_tunnels_cancel (struct GNUNET_CADET_Handle *h)
1917 {
1918   void *cls;
1919
1920   h->info_cb.tunnels_cb = NULL;
1921   cls = h->info_cls;
1922   h->info_cls = NULL;
1923
1924   return cls;
1925 }
1926
1927
1928
1929 /**
1930  * Request information about a tunnel of the running cadet peer.
1931  * The callback will be called for the tunnel once.
1932  * Only one info request (of any kind) can be active at once.
1933  *
1934  * WARNING: unstable API, likely to change in the future!
1935  *
1936  * @param h Handle to the cadet peer.
1937  * @param id Peer whose tunnel to examine.
1938  * @param callback Function to call with the requested data.
1939  * @param callback_cls Closure for @c callback.
1940  *
1941  * @return #GNUNET_OK / #GNUNET_SYSERR
1942  */
1943 int
1944 GNUNET_CADET_get_tunnel (struct GNUNET_CADET_Handle *h,
1945                         const struct GNUNET_PeerIdentity *id,
1946                         GNUNET_CADET_TunnelCB callback,
1947                         void *callback_cls)
1948 {
1949   struct GNUNET_CADET_LocalInfo *msg;
1950   struct GNUNET_MQ_Envelope *env;
1951
1952   if (NULL != h->info_cb.tunnel_cb)
1953   {
1954     GNUNET_break (0);
1955     return GNUNET_SYSERR;
1956   }
1957
1958   env = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNEL);
1959   msg->peer = *id;
1960   GNUNET_MQ_send (h->mq, env);
1961
1962   h->info_cb.tunnel_cb = callback;
1963   h->info_cls = callback_cls;
1964   return GNUNET_OK;
1965 }
1966
1967
1968 /**
1969  * Request information about a specific channel of the running cadet peer.
1970  *
1971  * WARNING: unstable API, likely to change in the future!
1972  * FIXME Add destination option.
1973  *
1974  * @param h Handle to the cadet peer.
1975  * @param initiator ID of the owner of the channel.
1976  * @param channel_number Channel number.
1977  * @param callback Function to call with the requested data.
1978  * @param callback_cls Closure for @c callback.
1979  *
1980  * @return #GNUNET_OK / #GNUNET_SYSERR
1981  */
1982 int
1983 GNUNET_CADET_show_channel (struct GNUNET_CADET_Handle *h,
1984                            struct GNUNET_PeerIdentity *initiator,
1985                            unsigned int channel_number,
1986                            GNUNET_CADET_ChannelCB callback,
1987                            void *callback_cls)
1988 {
1989   struct GNUNET_CADET_LocalInfo *msg;
1990   struct GNUNET_MQ_Envelope *env;
1991
1992   if (NULL != h->info_cb.channel_cb)
1993   {
1994     GNUNET_break (0);
1995     return GNUNET_SYSERR;
1996   }
1997
1998   env = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_CHANNEL);
1999   msg->peer = *initiator;
2000   msg->ccn.channel_of_client = htonl (channel_number);
2001   GNUNET_MQ_send (h->mq, env);
2002
2003   h->info_cb.channel_cb = callback;
2004   h->info_cls = callback_cls;
2005   return GNUNET_OK;
2006 }
2007
2008
2009 /**
2010  * Function called to notify a client about the connection
2011  * begin ready to queue more data.  "buf" will be
2012  * NULL and "size" zero if the connection was closed for
2013  * writing in the meantime.
2014  *
2015  * @param cls closure
2016  * @param size number of bytes available in buf
2017  * @param buf where the callee should write the message
2018  * @return number of bytes written to buf
2019  */
2020 static size_t
2021 cadet_mq_ntr (void *cls, size_t size,
2022              void *buf)
2023 {
2024   struct GNUNET_MQ_Handle *mq = cls;
2025   struct CadetMQState *state = GNUNET_MQ_impl_state (mq);
2026   const struct GNUNET_MessageHeader *msg = GNUNET_MQ_impl_current (mq);
2027   uint16_t msize;
2028
2029   state->th = NULL;
2030   if (NULL == buf)
2031   {
2032     GNUNET_MQ_inject_error (mq, GNUNET_MQ_ERROR_WRITE);
2033     return 0;
2034   }
2035   msize = ntohs (msg->size);
2036   GNUNET_assert (msize <= size);
2037   GNUNET_memcpy (buf, msg, msize);
2038   GNUNET_MQ_impl_send_continue (mq);
2039   return msize;
2040 }
2041
2042
2043 /**
2044  * Signature of functions implementing the
2045  * sending functionality of a message queue.
2046  *
2047  * @param mq the message queue
2048  * @param msg the message to send
2049  * @param impl_state state of the implementation
2050  */
2051 static void
2052 cadet_mq_send_impl (struct GNUNET_MQ_Handle *mq,
2053                     const struct GNUNET_MessageHeader *msg,
2054                     void *impl_state)
2055 {
2056   struct CadetMQState *state = impl_state;
2057
2058   GNUNET_assert (NULL == state->th);
2059   state->th =
2060       GNUNET_CADET_notify_transmit_ready (state->channel,
2061                                          /* FIXME: add option for corking */
2062                                          GNUNET_NO,
2063                                          GNUNET_TIME_UNIT_FOREVER_REL,
2064                                          ntohs (msg->size),
2065                                          &cadet_mq_ntr, mq);
2066
2067 }
2068
2069
2070 /**
2071  * Signature of functions implementing the
2072  * destruction of a message queue.
2073  * Implementations must not free 'mq', but should
2074  * take care of 'impl_state'.
2075  *
2076  * @param mq the message queue to destroy
2077  * @param impl_state state of the implementation
2078  */
2079 static void
2080 cadet_mq_destroy_impl (struct GNUNET_MQ_Handle *mq,
2081                        void *impl_state)
2082 {
2083   struct CadetMQState *state = impl_state;
2084
2085   if (NULL != state->th)
2086     GNUNET_CADET_notify_transmit_ready_cancel (state->th);
2087
2088   GNUNET_free (state);
2089 }
2090
2091
2092 /**
2093  * Create a message queue for a cadet channel.
2094  * The message queue can only be used to transmit messages,
2095  * not to receive them.
2096  *
2097  * @param channel the channel to create the message qeue for
2098  * @return a message queue to messages over the channel
2099  */
2100 struct GNUNET_MQ_Handle *
2101 GNUNET_CADET_mq_create (struct GNUNET_CADET_Channel *channel)
2102 {
2103   struct GNUNET_MQ_Handle *mq;
2104   struct CadetMQState *state;
2105
2106   state = GNUNET_new (struct CadetMQState);
2107   state->channel = channel;
2108
2109   mq = GNUNET_MQ_queue_for_callbacks (&cadet_mq_send_impl,
2110                                       &cadet_mq_destroy_impl,
2111                                       NULL, /* FIXME: cancel impl. */
2112                                       state,
2113                                       NULL, /* no msg handlers */
2114                                       NULL, /* no err handlers */
2115                                       NULL); /* no handler cls */
2116   return mq;
2117 }
2118
2119
2120 /**
2121  * Transitional function to convert an unsigned int port to a hash value.
2122  * WARNING: local static value returned, NOT reentrant!
2123  * WARNING: do not use this function for new code!
2124  *
2125  * @param port Numerical port (unsigned int format).
2126  *
2127  * @return A GNUNET_HashCode usable for the new CADET API.
2128  */
2129 const struct GNUNET_HashCode *
2130 GC_u2h (uint32_t port)
2131 {
2132   static struct GNUNET_HashCode hash;
2133
2134   GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2135               "This is a transitional function, "
2136               "use proper crypto hashes as CADET ports\n");
2137   GNUNET_CRYPTO_hash (&port, sizeof (port), &hash);
2138
2139   return &hash;
2140 }