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