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