eff6af40bd92dc0ecc475b3905f65e242c5e1e51
[oweals/gnunet.git] / src / cadet / gnunet-service-cadet_channel.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2013 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
22 #include "platform.h"
23 #include "gnunet_util_lib.h"
24
25 #include "gnunet_statistics_service.h"
26
27 #include "cadet.h"
28 #include "cadet_protocol.h"
29
30 #include "gnunet-service-cadet_channel.h"
31 #include "gnunet-service-cadet_local.h"
32 #include "gnunet-service-cadet_tunnel.h"
33 #include "gnunet-service-cadet_peer.h"
34
35 #define LOG(level, ...) GNUNET_log_from(level,"cadet-chn",__VA_ARGS__)
36 #define LOG2(level, ...) GNUNET_log_from_nocheck(level,"cadet-chn",__VA_ARGS__)
37
38 #define CADET_RETRANSMIT_TIME    GNUNET_TIME_relative_multiply(\
39                                     GNUNET_TIME_UNIT_MILLISECONDS, 250)
40 #define CADET_RETRANSMIT_MARGIN  4
41
42
43 /**
44  * All the states a connection can be in.
45  */
46 enum CadetChannelState
47 {
48   /**
49    * Uninitialized status, should never appear in operation.
50    */
51   CADET_CHANNEL_NEW,
52
53   /**
54    * Connection create message sent, waiting for ACK.
55    */
56   CADET_CHANNEL_SENT,
57
58   /**
59    * Connection confirmed, ready to carry traffic.
60    */
61   CADET_CHANNEL_READY
62 };
63
64
65 /**
66  * Info holder for channel messages in queues.
67  */
68 struct CadetChannelQueue
69 {
70   /**
71    * Tunnel Queue.
72    */
73   struct CadetTunnelQueue *tq;
74
75   /**
76    * Message type (DATA/DATA_ACK)
77    */
78   uint16_t type;
79
80   /**
81    * Message copy (for DATAs, to start retransmission timer)
82    */
83   struct CadetReliableMessage *copy;
84
85   /**
86    * Reliability (for DATA_ACKs, to access rel->ack_q)
87    */
88   struct CadetChannelReliability *rel;
89 };
90
91
92 /**
93  * Info needed to retry a message in case it gets lost.
94  */
95 struct CadetReliableMessage
96 {
97   /**
98    * Double linked list, FIFO style
99    */
100   struct CadetReliableMessage   *next;
101   struct CadetReliableMessage   *prev;
102
103   /**
104    * Type of message (payload, channel management).
105    */
106   int16_t                       type;
107
108   /**
109    * Tunnel Reliability queue this message is in.
110    */
111   struct CadetChannelReliability *rel;
112
113   /**
114    * ID of the message (ACK needed to free)
115    */
116   uint32_t                      mid;
117
118   /**
119    * Tunnel Queue.
120    */
121   struct CadetChannelQueue      *chq;
122
123   /**
124    * When was this message issued (to calculate ACK delay)
125    */
126   struct GNUNET_TIME_Absolute   timestamp;
127
128   /* struct GNUNET_CADET_ChannelDataMessage with payload */
129 };
130
131
132 /**
133  * Info about the traffic state for a client in a channel.
134  */
135 struct CadetChannelReliability
136 {
137   /**
138    * Channel this is about.
139    */
140   struct CadetChannel *ch;
141
142   /**
143    * DLL of messages sent and not yet ACK'd.
144    */
145   struct CadetReliableMessage        *head_sent;
146   struct CadetReliableMessage        *tail_sent;
147
148   /**
149    * DLL of messages received out of order.
150    */
151   struct CadetReliableMessage        *head_recv;
152   struct CadetReliableMessage        *tail_recv;
153
154   /**
155    * Messages received.
156    */
157   unsigned int                      n_recv;
158
159   /**
160    * Next MID to use for outgoing traffic.
161    */
162   uint32_t                          mid_send;
163
164   /**
165    * Next MID expected for incoming traffic.
166    */
167   uint32_t                          mid_recv;
168
169   /**
170    * Handle for queued unique data CREATE, DATA_ACK.
171    */
172   struct CadetChannelQueue           *uniq;
173
174   /**
175    * Can we send data to the client?
176    */
177   int                               client_ready;
178
179   /**
180    * Can the client send data to us?
181    */
182   int                               client_allowed;
183
184   /**
185    * Task to resend/poll in case no ACK is received.
186    */
187   struct GNUNET_SCHEDULER_Task *   retry_task;
188
189   /**
190    * Counter for exponential backoff.
191    */
192   struct GNUNET_TIME_Relative       retry_timer;
193
194   /**
195    * How long does it usually take to get an ACK.
196    */
197   struct GNUNET_TIME_Relative       expected_delay;
198 };
199
200
201 /**
202  * Struct containing all information regarding a channel to a remote client.
203  */
204 struct CadetChannel
205 {
206   /**
207    * Tunnel this channel is in.
208    */
209   struct CadetTunnel *t;
210
211   /**
212    * Destination port of the channel.
213    */
214   struct GNUNET_HashCode port;
215
216   /**
217    * Global channel number ( < GNUNET_CADET_LOCAL_CHANNEL_ID_CLI)
218    */
219   struct GNUNET_CADET_ChannelNumber gid;
220
221   /**
222    * Local tunnel number for root (owner) client.
223    * ( >= GNUNET_CADET_LOCAL_CHANNEL_ID_CLI or 0 )
224    */
225   struct GNUNET_CADET_ClientChannelNumber lid_root;
226
227   /**
228    * Local tunnel number for local destination clients (incoming number)
229    * ( >= GNUNET_CADET_LOCAL_CHANNEL_ID_SERV or 0).
230    */
231   struct GNUNET_CADET_ClientChannelNumber lid_dest;
232
233   /**
234    * Channel state.
235    */
236   enum CadetChannelState state;
237
238   /**
239    * Is the tunnel bufferless (minimum latency)?
240    */
241   int nobuffer;
242
243   /**
244    * Is the tunnel reliable?
245    */
246   int reliable;
247
248   /**
249    * Last time the channel was used
250    */
251   struct GNUNET_TIME_Absolute timestamp;
252
253   /**
254    * Client owner of the tunnel, if any
255    */
256   struct CadetClient *root;
257
258   /**
259    * Client destination of the tunnel, if any.
260    */
261   struct CadetClient *dest;
262
263   /**
264    * Flag to signal the destruction of the channel.
265    * If this is set to #GNUNET_YES the channel will be destroyed
266    * when the queue is empty.
267    */
268   int destroy;
269
270   /**
271    * Total (reliable) messages pending ACK for this channel.
272    */
273   unsigned int pending_messages;
274
275   /**
276    * Reliability data.
277    * Only present (non-NULL) at the owner of a tunnel.
278    */
279   struct CadetChannelReliability *root_rel;
280
281   /**
282    * Reliability data.
283    * Only present (non-NULL) at the destination of a tunnel.
284    */
285   struct CadetChannelReliability *dest_rel;
286
287 };
288
289
290 /******************************************************************************/
291 /*******************************   GLOBALS  ***********************************/
292 /******************************************************************************/
293
294 /**
295  * Global handle to the statistics service.
296  */
297 extern struct GNUNET_STATISTICS_Handle *stats;
298
299 /**
300  * Local peer own ID (memory efficient handle).
301  */
302 extern GNUNET_PEER_Id myid;
303
304
305 /******************************************************************************/
306 /********************************   STATIC  ***********************************/
307 /******************************************************************************/
308
309
310 /**
311  * Destroy a reliable message after it has been acknowledged, either by
312  * direct mid ACK or bitfield. Updates the appropriate data structures and
313  * timers and frees all memory.
314  *
315  * @param copy Message that is no longer needed: remote peer got it.
316  * @param update_time Is the timing information relevant?
317  *                    If this message is ACK in a batch the timing information
318  *                    is skewed by the retransmission, count only for the
319  *                    retransmitted message.
320  *
321  * @return #GNUNET_YES if channel was destroyed as a result of the call,
322  *         #GNUNET_NO otherwise.
323  */
324 static int
325 rel_message_free (struct CadetReliableMessage *copy, int update_time);
326
327 /**
328  * send a channel create message.
329  *
330  * @param ch Channel for which to send.
331  */
332 static void
333 send_create (struct CadetChannel *ch);
334
335 /**
336  * Confirm we got a channel create, FWD ack.
337  *
338  * @param ch The channel to confirm.
339  * @param fwd Should we send a FWD ACK? (going dest->root)
340  */
341 static void
342 send_ack (struct CadetChannel *ch, int fwd);
343
344
345
346 /**
347  * Test if the channel is loopback: both root and dest are on the local peer.
348  *
349  * @param ch Channel to test.
350  *
351  * @return #GNUNET_YES if channel is loopback, #GNUNET_NO otherwise.
352  */
353 static int
354 is_loopback (const struct CadetChannel *ch)
355 {
356   if (NULL != ch->t)
357     return GCT_is_loopback (ch->t);
358
359   return (NULL != ch->root && NULL != ch->dest);
360 }
361
362
363 /**
364  * Save a copy of the data message for later retransmission.
365  *
366  * @param msg Message to copy.
367  * @param mid Message ID.
368  * @param rel Reliability data for retransmission.
369  */
370 static struct CadetReliableMessage *
371 copy_message (const struct GNUNET_CADET_ChannelDataMessage *msg, uint32_t mid,
372               struct CadetChannelReliability *rel)
373 {
374   struct CadetReliableMessage *copy;
375   uint16_t size;
376
377   size = ntohs (msg->header.size);
378   copy = GNUNET_malloc (sizeof (*copy) + size);
379   copy->mid = mid;
380   copy->rel = rel;
381   copy->type = GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DATA;
382   GNUNET_memcpy (&copy[1], msg, size);
383
384   return copy;
385 }
386
387 /**
388  * We have received a message out of order, or the client is not ready.
389  * Buffer it until we receive an ACK from the client or the missing
390  * message from the channel.
391  *
392  * @param msg Message to buffer (MUST be of type CADET_DATA).
393  * @param rel Reliability data to the corresponding direction.
394  */
395 static void
396 add_buffered_data (const struct GNUNET_CADET_ChannelDataMessage *msg,
397                    struct CadetChannelReliability *rel)
398 {
399   struct CadetReliableMessage *copy;
400   struct CadetReliableMessage *prev;
401   uint32_t mid;
402
403   mid = ntohl (msg->mid);
404
405   LOG (GNUNET_ERROR_TYPE_DEBUG, "add_buffered_data MID %u (%u)\n",
406        mid, rel->n_recv);
407
408   rel->n_recv++;
409
410   // FIXME do something better than O(n), although n < 64...
411   // FIXME start from the end (most messages are the latest ones)
412   for (prev = rel->head_recv; NULL != prev; prev = prev->next)
413   {
414     LOG (GNUNET_ERROR_TYPE_DEBUG, " prev %u\n", prev->mid);
415     if (prev->mid == mid)
416     {
417       LOG (GNUNET_ERROR_TYPE_DEBUG, " already there!\n");
418       rel->n_recv--;
419       return;
420     }
421     else if (GC_is_pid_bigger (prev->mid, mid))
422     {
423       LOG (GNUNET_ERROR_TYPE_DEBUG, " bingo!\n");
424       copy = copy_message (msg, mid, rel);
425       GNUNET_CONTAINER_DLL_insert_before (rel->head_recv, rel->tail_recv,
426                                           prev, copy);
427       return;
428     }
429   }
430   copy = copy_message (msg, mid, rel);
431   LOG (GNUNET_ERROR_TYPE_DEBUG, " insert at tail! (now: %u)\n", rel->n_recv);
432   GNUNET_CONTAINER_DLL_insert_tail (rel->head_recv, rel->tail_recv, copy);
433   LOG (GNUNET_ERROR_TYPE_DEBUG, "add_buffered_data END\n");
434 }
435
436
437 /**
438  * Add a destination client to a channel, initializing all data structures
439  * in the channel and the client.
440  *
441  * @param ch Channel to which add the destination.
442  * @param c Client which to add to the channel.
443  */
444 static void
445 add_destination (struct CadetChannel *ch, struct CadetClient *c)
446 {
447   if (NULL != ch->dest)
448   {
449     GNUNET_break (0);
450     return;
451   }
452
453   /* Assign local id as destination */
454   ch->lid_dest = GML_get_next_chid (c);
455
456   /* Store in client's hashmap */
457   GML_channel_add (c, ch->lid_dest, ch);
458
459   GNUNET_break (NULL == ch->dest_rel);
460   ch->dest_rel = GNUNET_new (struct CadetChannelReliability);
461   ch->dest_rel->ch = ch;
462   ch->dest_rel->expected_delay.rel_value_us = 0;
463   ch->dest_rel->retry_timer = CADET_RETRANSMIT_TIME;
464
465   ch->dest = c;
466 }
467
468
469 /**
470  * Set options in a channel, extracted from a bit flag field.
471  *
472  * @param ch Channel to set options to.
473  * @param options Bit array in host byte order.
474  */
475 static void
476 channel_set_options (struct CadetChannel *ch, uint32_t options)
477 {
478   ch->nobuffer = (options & GNUNET_CADET_OPTION_NOBUFFER) != 0 ?
479   GNUNET_YES : GNUNET_NO;
480   ch->reliable = (options & GNUNET_CADET_OPTION_RELIABLE) != 0 ?
481   GNUNET_YES : GNUNET_NO;
482 }
483
484
485 /**
486  * Get a bit flag field with the options of a channel.
487  *
488  * @param ch Channel to get options from.
489  *
490  * @return Bit array in host byte order.
491  */
492 static uint32_t
493 channel_get_options (struct CadetChannel *ch)
494 {
495   uint32_t options;
496
497   options = 0;
498   if (ch->nobuffer)
499     options |= GNUNET_CADET_OPTION_NOBUFFER;
500   if (ch->reliable)
501     options |= GNUNET_CADET_OPTION_RELIABLE;
502
503   return options;
504 }
505
506
507 /**
508  * Notify a client that the channel is no longer valid.
509  *
510  * @param ch Channel that is destroyed.
511  * @param local_only Should we avoid sending it to other peers?
512  */
513 static void
514 send_destroy (struct CadetChannel *ch, int local_only)
515 {
516   struct GNUNET_CADET_ChannelManageMessage msg;
517
518   msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DESTROY);
519   msg.header.size = htons (sizeof (msg));
520   msg.chid = ch->gid;
521
522   /* If root is not NULL, notify.
523    * If it's NULL, check lid_root. When a local destroy comes in, root
524    * is set to NULL but lid_root is left untouched. In this case, do nothing,
525    * the client is the one who requested the channel to be destroyed.
526    */
527   if (NULL != ch->root)
528     GML_send_channel_destroy (ch->root, ch->lid_root);
529   else if (0 == ch->lid_root.channel_of_client && GNUNET_NO == local_only)
530     GCCH_send_prebuilt_message (&msg.header, ch, GNUNET_NO, NULL);
531
532   if (NULL != ch->dest)
533     GML_send_channel_destroy (ch->dest, ch->lid_dest);
534   else if (0 == ch->lid_dest.channel_of_client && GNUNET_NO == local_only)
535     GCCH_send_prebuilt_message (&msg.header, ch, GNUNET_YES, NULL);
536 }
537
538
539 /**
540  * Notify the destination client that a new incoming channel was created.
541  *
542  * @param ch Channel that was created.
543  */
544 static void
545 send_client_create (struct CadetChannel *ch)
546 {
547   uint32_t opt;
548
549   if (NULL == ch->dest)
550     return;
551
552   opt = 0;
553   opt |= GNUNET_YES == ch->reliable ? GNUNET_CADET_OPTION_RELIABLE : 0;
554   opt |= GNUNET_YES == ch->nobuffer ? GNUNET_CADET_OPTION_NOBUFFER : 0;
555   GML_send_channel_create (ch->dest,
556                            ch->lid_dest,
557                            &ch->port,
558                            opt,
559                            GCT_get_destination (ch->t));
560
561 }
562
563
564 /**
565  * Send data to a client.
566  *
567  * If the client is ready, send directly, otherwise buffer while listening
568  * for a local ACK.
569  *
570  * @param ch Channel
571  * @param msg Message.
572  * @param fwd Is this a fwd (root->dest) message?
573  */
574 static void
575 send_client_data (struct CadetChannel *ch,
576                   const struct GNUNET_CADET_ChannelDataMessage *msg,
577                   int fwd)
578 {
579   if (fwd)
580   {
581     if (ch->dest_rel->client_ready)
582     {
583       GML_send_data (ch->dest, msg, ch->lid_dest);
584       ch->dest_rel->client_ready = GNUNET_NO;
585       ch->dest_rel->mid_recv++;
586     }
587     else
588       add_buffered_data (msg, ch->dest_rel);
589   }
590   else
591   {
592     if (ch->root_rel->client_ready)
593     {
594       GML_send_data (ch->root, msg, ch->lid_root);
595       ch->root_rel->client_ready = GNUNET_NO;
596       ch->root_rel->mid_recv++;
597     }
598     else
599       add_buffered_data (msg, ch->root_rel);
600   }
601 }
602
603
604 /**
605  * Send a buffered message to the client, for in order delivery or
606  * as result of client ACK.
607  *
608  * @param ch Channel on which to empty the message buffer.
609  * @param c Client to send to.
610  * @param fwd Is this to send FWD data?.
611  */
612 static void
613 send_client_buffered_data (struct CadetChannel *ch,
614                            struct CadetClient *c,
615                            int fwd)
616 {
617   struct CadetReliableMessage *copy;
618   struct CadetChannelReliability *rel;
619
620   LOG (GNUNET_ERROR_TYPE_DEBUG, "send_buffered_data\n");
621   rel = fwd ? ch->dest_rel : ch->root_rel;
622   if (GNUNET_NO == rel->client_ready)
623   {
624     LOG (GNUNET_ERROR_TYPE_DEBUG, "client not ready\n");
625     return;
626   }
627
628   copy = rel->head_recv;
629   /* We never buffer channel management messages */
630   if (NULL != copy)
631   {
632     if (copy->mid == rel->mid_recv || GNUNET_NO == ch->reliable)
633     {
634       struct GNUNET_CADET_ChannelDataMessage *msg = (struct GNUNET_CADET_ChannelDataMessage *) &copy[1];
635
636       LOG (GNUNET_ERROR_TYPE_DEBUG, " have %u! now expecting %u\n",
637            copy->mid, rel->mid_recv + 1);
638       send_client_data (ch, msg, fwd);
639       rel->n_recv--;
640       GNUNET_CONTAINER_DLL_remove (rel->head_recv, rel->tail_recv, copy);
641       LOG (GNUNET_ERROR_TYPE_DEBUG, " free copy recv MID %u (%p), %u left\n",
642            copy->mid, copy, rel->n_recv);
643       GNUNET_free (copy);
644       GCCH_send_data_ack (ch, fwd);
645     }
646     else
647     {
648       LOG (GNUNET_ERROR_TYPE_DEBUG, " reliable && don't have %u, next is %u\n",
649            rel->mid_recv, copy->mid);
650       if (GNUNET_YES == ch->destroy)
651       {
652         /* We don't have the next data piece and the remote peer has closed the
653          * channel. We won't receive it anymore, so just destroy the channel.
654          * FIXME: wait some time to allow other connections to
655          *        deliver missing messages
656          */
657         send_destroy (ch, GNUNET_YES);
658         GCCH_destroy (ch);
659       }
660     }
661   }
662   LOG (GNUNET_ERROR_TYPE_DEBUG, "send_buffered_data END\n");
663 }
664
665
666 /**
667  * Allow a client to send more data.
668  *
669  * In case the client was already allowed to send data, do nothing.
670  *
671  * @param ch Channel.
672  * @param fwd Is this a FWD ACK? (FWD ACKs are sent to root)
673  */
674 static void
675 send_client_ack (struct CadetChannel *ch, int fwd)
676 {
677   struct CadetChannelReliability *rel = fwd ? ch->root_rel : ch->dest_rel;
678   struct CadetClient *c = fwd ? ch->root : ch->dest;
679
680   if (NULL == c)
681   {
682     GNUNET_break (GNUNET_NO != ch->destroy);
683     return;
684   }
685   LOG (GNUNET_ERROR_TYPE_DEBUG,
686        "  sending %s ack to client on channel %s\n",
687        GC_f2s (fwd), GCCH_2s (ch));
688
689   if (NULL == rel)
690   {
691     GNUNET_break (0);
692     return;
693   }
694
695   if (GNUNET_YES == rel->client_allowed)
696   {
697     LOG (GNUNET_ERROR_TYPE_DEBUG, "  already allowed\n");
698     return;
699   }
700   rel->client_allowed = GNUNET_YES;
701
702   GML_send_ack (c, fwd ? ch->lid_root : ch->lid_dest);
703 }
704
705
706 /**
707  * Notify the root that the destination rejected the channel.
708  *
709  * @param ch Rejected channel.
710  */
711 static void
712 send_client_nack (struct CadetChannel *ch)
713 {
714   if (NULL == ch->root)
715   {
716     GNUNET_break (0);
717     return;
718   }
719   GML_send_channel_nack (ch->root, ch->lid_root);
720 }
721
722
723 /**
724  * We haven't received an ACK after a certain time: restransmit the message.
725  *
726  * @param cls Closure (CadetChannelReliability with the message to restransmit)
727  */
728 static void
729 channel_retransmit_message (void *cls)
730 {
731   struct CadetChannelReliability *rel = cls;
732   struct CadetReliableMessage *copy;
733   struct CadetChannel *ch;
734   struct GNUNET_CADET_ChannelDataMessage *payload;
735   int fwd;
736
737   rel->retry_task = NULL;
738   ch = rel->ch;
739   copy = rel->head_sent;
740   if (NULL == copy)
741   {
742     GNUNET_break (0); // FIXME tripped in rps testcase
743     return;
744   }
745
746   payload = (struct GNUNET_CADET_ChannelDataMessage *) &copy[1];
747   fwd = (rel == ch->root_rel);
748
749   /* Message not found in the queue that we are going to use. */
750   LOG (GNUNET_ERROR_TYPE_DEBUG, "RETRANSMIT MID %u\n", copy->mid);
751
752   GCCH_send_prebuilt_message (&payload->header, ch, fwd, copy);
753   GNUNET_STATISTICS_update (stats, "# data retransmitted", 1, GNUNET_NO);
754 }
755
756
757 /**
758  * We haven't received an Channel ACK after a certain time: resend the CREATE.
759  *
760  * @param cls Closure (CadetChannelReliability of the channel to recreate)
761  */
762 static void
763 channel_recreate (void *cls)
764 {
765   struct CadetChannelReliability *rel = cls;
766
767   rel->retry_task = NULL;
768   LOG (GNUNET_ERROR_TYPE_DEBUG, "RE-CREATE\n");
769   GNUNET_STATISTICS_update (stats,
770                             "# data retransmitted", 1, GNUNET_NO);
771
772   if (rel == rel->ch->root_rel)
773   {
774     send_create (rel->ch);
775   }
776   else if (rel == rel->ch->dest_rel)
777   {
778     send_ack (rel->ch, GNUNET_YES);
779   }
780   else
781   {
782     GNUNET_break (0);
783   }
784 }
785
786
787 /**
788  * Message has been sent: start retransmission timer.
789  *
790  * @param cls Closure (queue structure).
791  * @param t Tunnel.
792  * @param q Queue handler (no longer valid).
793  * @param type Type of message.
794  * @param size Size of the message.
795  */
796 static void
797 ch_message_sent (void *cls,
798                  struct CadetTunnel *t,
799                  struct CadetTunnelQueue *q,
800                  uint16_t type, size_t size)
801 {
802   struct CadetChannelQueue *chq = cls;
803   struct CadetReliableMessage *copy = chq->copy;
804   struct CadetChannelReliability *rel;
805
806   LOG (GNUNET_ERROR_TYPE_DEBUG, "channel_message_sent callback %s\n",
807        GC_m2s (chq->type));
808
809   switch (chq->type)
810   {
811     case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DATA:
812       LOG (GNUNET_ERROR_TYPE_DEBUG, "data MID %u sent\n", copy->mid);
813       GNUNET_assert (chq == copy->chq);
814       copy->timestamp = GNUNET_TIME_absolute_get ();
815       rel = copy->rel;
816       if (NULL == rel->retry_task)
817       {
818         LOG (GNUNET_ERROR_TYPE_DEBUG, "  scheduling retry in %d * %s\n",
819              CADET_RETRANSMIT_MARGIN,
820              GNUNET_STRINGS_relative_time_to_string (rel->expected_delay,
821                                                      GNUNET_YES));
822         if (0 != rel->expected_delay.rel_value_us)
823         {
824           rel->retry_timer =
825               GNUNET_TIME_relative_saturating_multiply (rel->expected_delay,
826                                                         CADET_RETRANSMIT_MARGIN);
827         }
828         else
829         {
830           rel->retry_timer = CADET_RETRANSMIT_TIME;
831         }
832         LOG (GNUNET_ERROR_TYPE_DEBUG, "  using delay %s\n",
833              GNUNET_STRINGS_relative_time_to_string (rel->retry_timer,
834                                                      GNUNET_NO));
835         rel->retry_task =
836             GNUNET_SCHEDULER_add_delayed (rel->retry_timer,
837                                           &channel_retransmit_message, rel);
838       }
839       else
840       {
841         LOG (GNUNET_ERROR_TYPE_DEBUG, "retry running %p\n", rel->retry_task);
842       }
843       copy->chq = NULL;
844       break;
845
846
847     case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DATA_ACK:
848     case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_CREATE:
849     case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_CREATE_ACK:
850       LOG (GNUNET_ERROR_TYPE_DEBUG, "sent %s\n", GC_m2s (chq->type));
851       rel = chq->rel;
852       GNUNET_assert (rel->uniq == chq);
853       rel->uniq = NULL;
854
855       if (CADET_CHANNEL_READY != rel->ch->state
856           && GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DATA_ACK != type
857           && GNUNET_NO == rel->ch->destroy)
858       {
859         GNUNET_assert (NULL == rel->retry_task);
860         LOG (GNUNET_ERROR_TYPE_DEBUG, "STD BACKOFF %s\n",
861              GNUNET_STRINGS_relative_time_to_string (rel->retry_timer,
862                                                      GNUNET_NO));
863         rel->retry_timer = GNUNET_TIME_STD_BACKOFF (rel->retry_timer);
864         rel->retry_task = GNUNET_SCHEDULER_add_delayed (rel->retry_timer,
865                                                         &channel_recreate, rel);
866       }
867       break;
868
869     default:
870       GNUNET_break (0);
871   }
872
873   GNUNET_free (chq);
874 }
875
876
877 /**
878  * send a channel create message.
879  *
880  * @param ch Channel for which to send.
881  */
882 static void
883 send_create (struct CadetChannel *ch)
884 {
885   struct GNUNET_CADET_ChannelCreateMessage msgcc;
886
887   msgcc.header.size = htons (sizeof (msgcc));
888   msgcc.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CHANNEL_CREATE);
889   msgcc.chid = ch->gid;
890   msgcc.port = ch->port;
891   msgcc.opt = htonl (channel_get_options (ch));
892
893   GCCH_send_prebuilt_message (&msgcc.header, ch, GNUNET_YES, NULL);
894 }
895
896
897 /**
898  * Confirm we got a channel create or FWD ack.
899  *
900  * @param ch The channel to confirm.
901  * @param fwd Should we send a FWD ACK? (going dest->root)
902  */
903 static void
904 send_ack (struct CadetChannel *ch, int fwd)
905 {
906   struct GNUNET_CADET_ChannelManageMessage msg;
907
908   msg.header.size = htons (sizeof (msg));
909   msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CHANNEL_CREATE_ACK);
910   LOG (GNUNET_ERROR_TYPE_DEBUG,
911        "  sending channel %s ack for channel %s\n",
912        GC_f2s (fwd), GCCH_2s (ch));
913
914   msg.chid =ch->gid;
915   GCCH_send_prebuilt_message (&msg.header, ch, !fwd, NULL);
916 }
917
918
919 /**
920  * Send a message and don't keep any info about it: we won't need to cancel it
921  * or resend it.
922  *
923  * @param msg Header of the message to fire away.
924  * @param ch Channel on which the message should go.
925  * @param force Is this a forced (undroppable) message?
926  */
927 static void
928 fire_and_forget (const struct GNUNET_MessageHeader *msg,
929                  struct CadetChannel *ch,
930                  int force)
931 {
932   GNUNET_break (NULL ==
933                 GCT_send_prebuilt_message (msg, ch->t, NULL,
934                                            force, NULL, NULL));
935 }
936
937
938 /**
939  * Notify that a channel create didn't succeed.
940  *
941  * @param ch The channel to reject.
942  */
943 static void
944 send_nack (struct CadetChannel *ch)
945 {
946   struct GNUNET_CADET_ChannelManageMessage msg;
947
948   msg.header.size = htons (sizeof (msg));
949   msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CHANNEL_CREATE_NACK_DEPRECATED);
950   LOG (GNUNET_ERROR_TYPE_DEBUG,
951        "  sending channel NACK for channel %s\n",
952        GCCH_2s (ch));
953
954   msg.chid = ch->gid;
955   GCCH_send_prebuilt_message (&msg.header, ch, GNUNET_NO, NULL);
956 }
957
958
959 /**
960  * Destroy all reliable messages queued for a channel,
961  * during a channel destruction.
962  * Frees the reliability structure itself.
963  *
964  * @param rel Reliability data for a channel.
965  */
966 static void
967 channel_rel_free_all (struct CadetChannelReliability *rel)
968 {
969   struct CadetReliableMessage *copy;
970   struct CadetReliableMessage *next;
971
972   if (NULL == rel)
973     return;
974
975   for (copy = rel->head_recv; NULL != copy; copy = next)
976   {
977     next = copy->next;
978     GNUNET_CONTAINER_DLL_remove (rel->head_recv, rel->tail_recv, copy);
979     LOG (GNUNET_ERROR_TYPE_DEBUG, " COPYFREE ALL RECV %p\n", copy);
980     GNUNET_break (NULL == copy->chq);
981     GNUNET_free (copy);
982   }
983   for (copy = rel->head_sent; NULL != copy; copy = next)
984   {
985     next = copy->next;
986     GNUNET_CONTAINER_DLL_remove (rel->head_sent, rel->tail_sent, copy);
987     LOG (GNUNET_ERROR_TYPE_DEBUG, " COPYFREE ALL SEND %p\n", copy);
988     if (NULL != copy->chq)
989     {
990       if (NULL != copy->chq->tq)
991       {
992         GCT_cancel (copy->chq->tq);
993         /* ch_message_sent will free copy->q */
994       }
995       else
996       {
997         GNUNET_free (copy->chq);
998         GNUNET_break (0);
999       }
1000     }
1001     GNUNET_free (copy);
1002   }
1003   if (NULL != rel->uniq && NULL != rel->uniq->tq)
1004   {
1005     GCT_cancel (rel->uniq->tq);
1006     /* ch_message_sent is called freeing uniq */
1007   }
1008   if (NULL != rel->retry_task)
1009   {
1010     GNUNET_SCHEDULER_cancel (rel->retry_task);
1011     rel->retry_task = NULL;
1012   }
1013   GNUNET_free (rel);
1014 }
1015
1016
1017 /**
1018  * Mark future messages as ACK'd.
1019  *
1020  * @param rel Reliability data.
1021  * @param msg DataACK message with a bitfield of future ACK'd messages.
1022  *
1023  * @return How many messages have been freed.
1024  */
1025 static unsigned int
1026 channel_rel_free_sent (struct CadetChannelReliability *rel,
1027                        const struct GNUNET_CADET_ChannelDataAckMessage *msg)
1028 {
1029   struct CadetReliableMessage *copy;
1030   struct CadetReliableMessage *next;
1031   uint64_t bitfield;
1032   uint64_t mask;
1033   uint32_t mid;
1034   uint32_t target;
1035   unsigned int i;
1036   unsigned int r;
1037
1038   bitfield = msg->futures;
1039   mid = ntohl (msg->mid);
1040   LOG (GNUNET_ERROR_TYPE_DEBUG, "free_sent_reliable %u %lX\n", mid, bitfield);
1041   LOG (GNUNET_ERROR_TYPE_DEBUG, " rel %p, head %p\n", rel, rel->head_sent);
1042   for (i = 0, r = 0, copy = rel->head_sent;
1043        i < 64 && NULL != copy && 0 != bitfield;
1044        i++)
1045   {
1046     LOG (GNUNET_ERROR_TYPE_DEBUG, " trying bit %u (mid %u)\n", i, mid + i + 1);
1047     mask = 0x1LL << i;
1048     if (0 == (bitfield & mask))
1049      continue;
1050
1051     LOG (GNUNET_ERROR_TYPE_DEBUG, " set!\n");
1052     /* Bit was set, clear the bit from the bitfield */
1053     bitfield &= ~mask;
1054
1055     /* The i-th bit was set. Do we have that copy? */
1056     /* Skip copies with mid < target */
1057     target = mid + i + 1;
1058     LOG (GNUNET_ERROR_TYPE_DEBUG, " target %u\n", target);
1059     while (NULL != copy && GC_is_pid_bigger (target, copy->mid))
1060       copy = copy->next;
1061
1062     /* Did we run out of copies? (previously freed, it's ok) */
1063     if (NULL == copy)
1064     {
1065       LOG (GNUNET_ERROR_TYPE_DEBUG, "run out of copies...\n");
1066       return r;
1067     }
1068
1069     /* Did we overshoot the target? (previously freed, it's ok) */
1070     if (GC_is_pid_bigger (copy->mid, target))
1071     {
1072       LOG (GNUNET_ERROR_TYPE_DEBUG, " next copy %u\n", copy->mid);
1073       i += copy->mid - target - 1;   /* MID: 90, t = 85, i += 4 (i++ later) */
1074       mask = (0x1LL << (i + 1)) - 1; /* Mask = i-th bit and all before */
1075       bitfield &= ~mask;             /* Clear all bits up to MID - 1 */
1076       continue;
1077     }
1078
1079     /* Now copy->mid == target, free it */
1080     next = copy->next;
1081     GNUNET_break (GNUNET_YES != rel_message_free (copy, GNUNET_YES));
1082     r++;
1083     copy = next;
1084   }
1085   LOG (GNUNET_ERROR_TYPE_DEBUG, "free_sent_reliable END\n");
1086   return r;
1087 }
1088
1089
1090 /**
1091  * Destroy a reliable message after it has been acknowledged, either by
1092  * direct mid ACK or bitfield. Updates the appropriate data structures and
1093  * timers and frees all memory.
1094  *
1095  * @param copy Message that is no longer needed: remote peer got it.
1096  * @param update_time Is the timing information relevant?
1097  *                    If this message is ACK in a batch the timing information
1098  *                    is skewed by the retransmission, count only for the
1099  *                    retransmitted message.
1100  *
1101  * @return #GNUNET_YES if channel was destroyed as a result of the call,
1102  *         #GNUNET_NO otherwise.
1103  */
1104 static int
1105 rel_message_free (struct CadetReliableMessage *copy, int update_time)
1106 {
1107   struct CadetChannelReliability *rel;
1108   struct GNUNET_TIME_Relative time;
1109
1110   rel = copy->rel;
1111   LOG (GNUNET_ERROR_TYPE_DEBUG, "Freeing %u\n", copy->mid);
1112   if (GNUNET_YES == update_time)
1113   {
1114     time = GNUNET_TIME_absolute_get_duration (copy->timestamp);
1115     if (0 == rel->expected_delay.rel_value_us)
1116       rel->expected_delay = time;
1117     else
1118     {
1119       rel->expected_delay.rel_value_us *= 7;
1120       rel->expected_delay.rel_value_us += time.rel_value_us;
1121       rel->expected_delay.rel_value_us /= 8;
1122     }
1123     LOG (GNUNET_ERROR_TYPE_DEBUG, "  message time %12s\n",
1124          GNUNET_STRINGS_relative_time_to_string (time, GNUNET_NO));
1125     LOG (GNUNET_ERROR_TYPE_DEBUG, "  new delay    %12s\n",
1126          GNUNET_STRINGS_relative_time_to_string (rel->expected_delay,
1127                                                  GNUNET_NO));
1128     rel->retry_timer = rel->expected_delay;
1129   }
1130   else
1131   {
1132     LOG (GNUNET_ERROR_TYPE_DEBUG, "batch free, ignoring timing\n");
1133   }
1134   rel->ch->pending_messages--;
1135   if (NULL != copy->chq)
1136   {
1137     GCT_cancel (copy->chq->tq);
1138     /* copy->q is set to NULL by ch_message_sent */
1139   }
1140   GNUNET_CONTAINER_DLL_remove (rel->head_sent, rel->tail_sent, copy);
1141   LOG (GNUNET_ERROR_TYPE_DEBUG, " free send copy MID %u at %p\n",
1142        copy->mid, copy);
1143   GNUNET_free (copy);
1144
1145   if (GNUNET_NO != rel->ch->destroy && 0 == rel->ch->pending_messages)
1146   {
1147     GCCH_destroy (rel->ch);
1148     return GNUNET_YES;
1149   }
1150   return GNUNET_NO;
1151 }
1152
1153
1154 /**
1155  * Channel was ACK'd by remote peer, mark as ready and cancel retransmission.
1156  *
1157  * @param ch Channel to mark as ready.
1158  * @param fwd Was the ACK message a FWD ACK? (dest->root, SYNACK)
1159  */
1160 static void
1161 channel_confirm (struct CadetChannel *ch, int fwd)
1162 {
1163   struct CadetChannelReliability *rel;
1164   enum CadetChannelState oldstate;
1165
1166   rel = fwd ? ch->root_rel : ch->dest_rel;
1167   if (NULL == rel)
1168   {
1169     GNUNET_break (GNUNET_NO != ch->destroy);
1170     return;
1171   }
1172   LOG (GNUNET_ERROR_TYPE_DEBUG, "  channel confirm %s %s\n",
1173        GC_f2s (fwd), GCCH_2s (ch));
1174   oldstate = ch->state;
1175   ch->state = CADET_CHANNEL_READY;
1176
1177   if (CADET_CHANNEL_READY != oldstate || GNUNET_YES == is_loopback (ch))
1178   {
1179     rel->client_ready = GNUNET_YES;
1180     rel->expected_delay = rel->retry_timer;
1181     LOG (GNUNET_ERROR_TYPE_DEBUG, "  confirm retry timer %s\n",
1182          GNUNET_STRINGS_relative_time_to_string (rel->retry_timer, GNUNET_NO));
1183     if (GCT_get_connections_buffer (ch->t) > 0 || GCT_is_loopback (ch->t))
1184       send_client_ack (ch, fwd);
1185
1186     if (NULL != rel->retry_task)
1187     {
1188       GNUNET_SCHEDULER_cancel (rel->retry_task);
1189       rel->retry_task = NULL;
1190     }
1191     else if (NULL != rel->uniq)
1192     {
1193       GCT_cancel (rel->uniq->tq);
1194       /* ch_message_sent will free and NULL uniq */
1195     }
1196     else if (GNUNET_NO == is_loopback (ch))
1197     {
1198       /* We SHOULD have been trying to retransmit this! */
1199       GNUNET_break (0);
1200     }
1201   }
1202
1203   /* In case of a FWD ACK (SYNACK) send a BCK ACK (ACK). */
1204   if (GNUNET_YES == fwd)
1205     send_ack (ch, GNUNET_NO);
1206 }
1207
1208
1209 /**
1210  * Save a copy to retransmit in case it gets lost.
1211  *
1212  * Initializes all needed callbacks and timers.
1213  *
1214  * @param ch Channel this message goes on.
1215  * @param msg Message to copy.
1216  * @param fwd Is this fwd traffic?
1217  */
1218 static struct CadetReliableMessage *
1219 channel_save_copy (struct CadetChannel *ch,
1220                    const struct GNUNET_MessageHeader *msg,
1221                    int fwd)
1222 {
1223   struct CadetChannelReliability *rel;
1224   struct CadetReliableMessage *copy;
1225   uint32_t mid;
1226   uint16_t type;
1227   uint16_t size;
1228
1229   rel = fwd ? ch->root_rel : ch->dest_rel;
1230   mid = rel->mid_send - 1;
1231   type = ntohs (msg->type);
1232   size = ntohs (msg->size);
1233
1234   LOG (GNUNET_ERROR_TYPE_DEBUG, "save MID %u %s\n", mid, GC_m2s (type));
1235   copy = GNUNET_malloc (sizeof (struct CadetReliableMessage) + size);
1236   LOG (GNUNET_ERROR_TYPE_DEBUG, "  at %p\n", copy);
1237   copy->mid = mid;
1238   copy->rel = rel;
1239   copy->type = type;
1240   GNUNET_memcpy (&copy[1], msg, size);
1241   GNUNET_CONTAINER_DLL_insert_tail (rel->head_sent, rel->tail_sent, copy);
1242   ch->pending_messages++;
1243
1244   return copy;
1245 }
1246
1247
1248 /**
1249  * Create a new channel.
1250  *
1251  * @param t Tunnel this channel is in.
1252  * @param owner Client that owns the channel, NULL for foreign channels.
1253  * @param lid_root Local ID for root client.
1254  *
1255  * @return A new initialized channel. NULL on error.
1256  */
1257 static struct CadetChannel *
1258 channel_new (struct CadetTunnel *t,
1259              struct CadetClient *owner,
1260              struct GNUNET_CADET_ClientChannelNumber lid_root)
1261 {
1262   struct CadetChannel *ch;
1263
1264   ch = GNUNET_new (struct CadetChannel);
1265   ch->root = owner;
1266   ch->lid_root = lid_root;
1267   ch->t = t;
1268
1269   GNUNET_STATISTICS_update (stats, "# channels", 1, GNUNET_NO);
1270
1271   if (NULL != owner)
1272   {
1273     ch->gid = GCT_get_next_chid (t);
1274     GML_channel_add (owner, lid_root, ch);
1275   }
1276   GCT_add_channel (t, ch);
1277
1278   return ch;
1279 }
1280
1281
1282 /**
1283  * Handle a loopback message: call the appropriate handler for the message type.
1284  *
1285  * @param ch Channel this message is on.
1286  * @param msgh Message header.
1287  * @param fwd Is this FWD traffic?
1288  */
1289 void
1290 handle_loopback (struct CadetChannel *ch,
1291                  const struct GNUNET_MessageHeader *msgh,
1292                  int fwd)
1293 {
1294   uint16_t type;
1295
1296   type = ntohs (msgh->type);
1297   LOG (GNUNET_ERROR_TYPE_DEBUG,
1298        "Loopback %s %s message!\n",
1299        GC_f2s (fwd), GC_m2s (type));
1300
1301   switch (type)
1302   {
1303     case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DATA:
1304       /* Don't send hop ACK, wait for client to ACK */
1305       LOG (GNUNET_ERROR_TYPE_DEBUG, "SEND loopback %u (%u)\n",
1306            ntohl (((struct GNUNET_CADET_ChannelDataMessage *) msgh)->mid), ntohs (msgh->size));
1307       GCCH_handle_data (ch, (struct GNUNET_CADET_ChannelDataMessage *) msgh, fwd);
1308       break;
1309
1310     case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DATA_ACK:
1311       GCCH_handle_data_ack (ch, (struct GNUNET_CADET_ChannelDataAckMessage *) msgh, fwd);
1312       break;
1313
1314     case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_CREATE:
1315       GCCH_handle_create (ch->t,
1316                           (struct GNUNET_CADET_ChannelCreateMessage *) msgh);
1317       break;
1318
1319     case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_CREATE_ACK:
1320       GCCH_handle_ack (ch,
1321                        (struct GNUNET_CADET_ChannelManageMessage *) msgh,
1322                        fwd);
1323       break;
1324
1325     case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_CREATE_NACK_DEPRECATED:
1326       GCCH_handle_nack (ch);
1327       break;
1328
1329     case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DESTROY:
1330       GCCH_handle_destroy (ch,
1331                            (struct GNUNET_CADET_ChannelManageMessage *) msgh,
1332                            fwd);
1333       break;
1334
1335     default:
1336       GNUNET_break_op (0);
1337       LOG (GNUNET_ERROR_TYPE_DEBUG,
1338            "end-to-end message not known (%u)\n",
1339            ntohs (msgh->type));
1340   }
1341 }
1342
1343
1344
1345 /******************************************************************************/
1346 /********************************    API    ***********************************/
1347 /******************************************************************************/
1348
1349 /**
1350  * Destroy a channel and free all resources.
1351  *
1352  * @param ch Channel to destroy.
1353  */
1354 void
1355 GCCH_destroy (struct CadetChannel *ch)
1356 {
1357   struct CadetClient *c;
1358   struct CadetTunnel *t;
1359
1360   if (NULL == ch)
1361     return;
1362   if (2 == ch->destroy)
1363     return; /* recursive call */
1364   ch->destroy = 2;
1365
1366   LOG (GNUNET_ERROR_TYPE_DEBUG, "destroying channel %s:%u\n",
1367               GCT_2s (ch->t), ch->gid);
1368   GCCH_debug (ch, GNUNET_ERROR_TYPE_DEBUG);
1369
1370   c = ch->root;
1371   if (NULL != c)
1372   {
1373     GML_channel_remove (c, ch->lid_root, ch);
1374   }
1375
1376   c = ch->dest;
1377   if (NULL != c)
1378   {
1379     GML_channel_remove (c, ch->lid_dest, ch);
1380   }
1381
1382   channel_rel_free_all (ch->root_rel);
1383   channel_rel_free_all (ch->dest_rel);
1384
1385   t = ch->t;
1386   GCT_remove_channel (t, ch);
1387   GNUNET_STATISTICS_update (stats, "# channels", -1, GNUNET_NO);
1388
1389   GNUNET_free (ch);
1390   GCT_destroy_if_empty (t);
1391 }
1392
1393
1394 /**
1395  * Get the channel's public ID.
1396  *
1397  * @param ch Channel.
1398  *
1399  * @return ID used to identify the channel with the remote peer.
1400  */
1401 struct GNUNET_CADET_ChannelNumber
1402 GCCH_get_id (const struct CadetChannel *ch)
1403 {
1404   return ch->gid;
1405 }
1406
1407
1408 /**
1409  * Get the channel tunnel.
1410  *
1411  * @param ch Channel to get the tunnel from.
1412  *
1413  * @return tunnel of the channel.
1414  */
1415 struct CadetTunnel *
1416 GCCH_get_tunnel (const struct CadetChannel *ch)
1417 {
1418   return ch->t;
1419 }
1420
1421
1422 /**
1423  * Get free buffer space towards the client on a specific channel.
1424  *
1425  * @param ch Channel.
1426  * @param fwd Is query about FWD traffic?
1427  *
1428  * @return Free buffer space [0 - 64]
1429  */
1430 unsigned int
1431 GCCH_get_buffer (struct CadetChannel *ch, int fwd)
1432 {
1433   struct CadetChannelReliability *rel;
1434
1435   rel = fwd ? ch->dest_rel : ch->root_rel;
1436   LOG (GNUNET_ERROR_TYPE_DEBUG, "   get buffer, channel %s\n", GCCH_2s (ch));
1437   GCCH_debug (ch, GNUNET_ERROR_TYPE_DEBUG);
1438   /* If rel is NULL it means that the end is not yet created,
1439    * most probably is a loopback channel at the point of sending
1440    * the ChannelCreate to itself.
1441    */
1442   if (NULL == rel)
1443   {
1444     LOG (GNUNET_ERROR_TYPE_DEBUG, "  rel is NULL: max\n");
1445     return 64;
1446   }
1447
1448   LOG (GNUNET_ERROR_TYPE_DEBUG, "   n_recv %d\n", rel->n_recv);
1449   return (64 - rel->n_recv);
1450 }
1451
1452
1453 /**
1454  * Get flow control status of end point: is client allow to send?
1455  *
1456  * @param ch Channel.
1457  * @param fwd Is query about FWD traffic? (Request root status).
1458  *
1459  * @return #GNUNET_YES if client is allowed to send us data.
1460  */
1461 int
1462 GCCH_get_allowed (struct CadetChannel *ch, int fwd)
1463 {
1464   struct CadetChannelReliability *rel;
1465
1466   rel = fwd ? ch->root_rel : ch->dest_rel;
1467
1468   if (NULL == rel)
1469   {
1470     /* Probably shutting down: root/dest NULL'ed to mark disconnection */
1471     GNUNET_break (GNUNET_NO != ch->destroy);
1472     return 0;
1473   }
1474
1475   return rel->client_allowed;
1476 }
1477
1478
1479 /**
1480  * Is the root client for this channel on this peer?
1481  *
1482  * @param ch Channel.
1483  * @param fwd Is this for fwd traffic?
1484  *
1485  * @return #GNUNET_YES in case it is.
1486  */
1487 int
1488 GCCH_is_origin (struct CadetChannel *ch, int fwd)
1489 {
1490   struct CadetClient *c;
1491
1492   c = fwd ? ch->root : ch->dest;
1493   return NULL != c;
1494 }
1495
1496
1497 /**
1498  * Is the destination client for this channel on this peer?
1499  *
1500  * @param ch Channel.
1501  * @param fwd Is this for fwd traffic?
1502  *
1503  * @return #GNUNET_YES in case it is.
1504  */
1505 int
1506 GCCH_is_terminal (struct CadetChannel *ch, int fwd)
1507 {
1508   struct CadetClient *c;
1509
1510   c = fwd ? ch->dest : ch->root;
1511   return NULL != c;
1512 }
1513
1514
1515 /**
1516  * Send an end-to-end ACK message for the most recent in-sequence payload.
1517  *
1518  * If channel is not reliable, do nothing.
1519  *
1520  * @param ch Channel this is about.
1521  * @param fwd Is for FWD traffic? (ACK dest->owner)
1522  */
1523 void
1524 GCCH_send_data_ack (struct CadetChannel *ch, int fwd)
1525 {
1526   struct GNUNET_CADET_ChannelDataAckMessage msg;
1527   struct CadetChannelReliability *rel;
1528   struct CadetReliableMessage *copy;
1529   unsigned int delta;
1530   uint64_t mask;
1531   uint32_t ack;
1532
1533   if (GNUNET_NO == ch->reliable)
1534     return;
1535
1536   rel = fwd ? ch->dest_rel : ch->root_rel;
1537   ack = rel->mid_recv - 1;
1538
1539   msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DATA_ACK);
1540   msg.header.size = htons (sizeof (msg));
1541   msg.chid = ch->gid;
1542   msg.mid = htonl (ack);
1543
1544   msg.futures = 0LL;
1545   for (copy = rel->head_recv; NULL != copy; copy = copy->next)
1546   {
1547     if (copy->type != GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DATA)
1548     {
1549       LOG (GNUNET_ERROR_TYPE_DEBUG, " Type %s, expected DATA\n",
1550            GC_m2s (copy->type));
1551       continue;
1552     }
1553     GNUNET_assert (GC_is_pid_bigger(copy->mid, ack));
1554     delta = copy->mid - (ack + 1);
1555     if (63 < delta)
1556       break;
1557     mask = 0x1LL << delta;
1558     msg.futures |= mask;
1559     LOG (GNUNET_ERROR_TYPE_DEBUG,
1560          " setting bit for %u (delta %u) (%lX) -> %lX\n",
1561          copy->mid, delta, mask, msg.futures);
1562   }
1563
1564   GCCH_send_prebuilt_message (&msg.header, ch, !fwd, NULL);
1565   LOG (GNUNET_ERROR_TYPE_DEBUG, "send_data_ack END\n");
1566 }
1567
1568
1569 /**
1570  * Allow a client to send us more data, in case it was choked.
1571  *
1572  * @param ch Channel.
1573  * @param fwd Is this about FWD traffic? (Root client).
1574  */
1575 void
1576 GCCH_allow_client (struct CadetChannel *ch, int fwd)
1577 {
1578   struct CadetChannelReliability *rel;
1579   unsigned int buffer;
1580
1581   LOG (GNUNET_ERROR_TYPE_DEBUG, "GMCH allow\n");
1582
1583   if (CADET_CHANNEL_READY != ch->state)
1584   {
1585     LOG (GNUNET_ERROR_TYPE_DEBUG, " channel not ready yet!\n");
1586     return;
1587   }
1588
1589   if (GNUNET_YES == ch->reliable)
1590   {
1591     rel = fwd ? ch->root_rel : ch->dest_rel;
1592     if (NULL == rel)
1593     {
1594       GNUNET_break (GNUNET_NO != ch->destroy);
1595       return;
1596     }
1597     if (NULL != rel->head_sent)
1598     {
1599       if (64 <= rel->mid_send - rel->head_sent->mid)
1600       {
1601         LOG (GNUNET_ERROR_TYPE_DEBUG, " too big MID gap! Wait for ACK.\n");
1602         return;
1603       }
1604       else
1605       {
1606         LOG (GNUNET_ERROR_TYPE_DEBUG, " gap ok: %u - %u\n",
1607              rel->head_sent->mid, rel->mid_send);
1608         struct CadetReliableMessage *aux;
1609         for (aux = rel->head_sent; NULL != aux; aux = aux->next)
1610         {
1611           LOG (GNUNET_ERROR_TYPE_DEBUG, "   - sent mid %u\n", aux->mid);
1612         }
1613       }
1614     }
1615     else
1616     {
1617       LOG (GNUNET_ERROR_TYPE_DEBUG, " head sent is NULL\n");
1618     }
1619   }
1620
1621   if (is_loopback (ch))
1622     buffer = GCCH_get_buffer (ch, fwd);
1623   else
1624     buffer = GCT_get_connections_buffer (ch->t);
1625
1626   if (0 == buffer)
1627   {
1628     LOG (GNUNET_ERROR_TYPE_DEBUG, " no buffer space.\n");
1629     return;
1630   }
1631
1632   LOG (GNUNET_ERROR_TYPE_DEBUG, " buffer space %u, allowing\n", buffer);
1633   send_client_ack (ch, fwd);
1634 }
1635
1636
1637 /**
1638  * Log channel info.
1639  *
1640  * @param ch Channel.
1641  * @param level Debug level to use.
1642  */
1643 void
1644 GCCH_debug (struct CadetChannel *ch, enum GNUNET_ErrorType level)
1645 {
1646   int do_log;
1647
1648   do_log = GNUNET_get_log_call_status (level & (~GNUNET_ERROR_TYPE_BULK),
1649                                        "cadet-chn",
1650                                        __FILE__, __FUNCTION__, __LINE__);
1651   if (0 == do_log)
1652     return;
1653
1654   if (NULL == ch)
1655   {
1656     LOG2 (level, "CHN *** DEBUG NULL CHANNEL ***\n");
1657     return;
1658   }
1659   LOG2 (level, "CHN Channel %s:%X (%p)\n", GCT_2s (ch->t), ch->gid, ch);
1660   LOG2 (level, "CHN   root %p/%p\n", ch->root, ch->root_rel);
1661   if (NULL != ch->root)
1662   {
1663     LOG2 (level, "CHN   cli %s\n", GML_2s (ch->root));
1664     LOG2 (level, "CHN   ready %s\n", ch->root_rel->client_ready ? "YES" : "NO");
1665     LOG2 (level, "CHN   id %X\n", ch->lid_root.channel_of_client);
1666     LOG2 (level, "CHN   recv %d\n", ch->root_rel->n_recv);
1667     LOG2 (level, "CHN   MID r: %d, s: %d\n",
1668           ch->root_rel->mid_recv, ch->root_rel->mid_send);
1669   }
1670   LOG2 (level, "CHN   dest %p/%p\n",
1671               ch->dest, ch->dest_rel);
1672   if (NULL != ch->dest)
1673   {
1674     LOG2 (level, "CHN   cli %s\n", GML_2s (ch->dest));
1675     LOG2 (level, "CHN   ready %s\n", ch->dest_rel->client_ready ? "YES" : "NO");
1676     LOG2 (level, "CHN   id %X\n", ch->lid_dest);
1677     LOG2 (level, "CHN   recv %d\n", ch->dest_rel->n_recv);
1678     LOG2 (level, "CHN   MID r: %d, s: %d\n",
1679           ch->dest_rel->mid_recv, ch->dest_rel->mid_send);
1680
1681   }
1682 }
1683
1684
1685 /**
1686  * Handle an ACK given by a client.
1687  *
1688  * Mark client as ready and send him any buffered data we could have for him.
1689  *
1690  * @param ch Channel.
1691  * @param fwd Is this a "FWD ACK"? (FWD ACKs are sent by dest and go BCK)
1692  */
1693 void
1694 GCCH_handle_local_ack (struct CadetChannel *ch, int fwd)
1695 {
1696   struct CadetChannelReliability *rel;
1697   struct CadetClient *c;
1698
1699   rel = fwd ? ch->dest_rel : ch->root_rel;
1700   c   = fwd ? ch->dest     : ch->root;
1701
1702   rel->client_ready = GNUNET_YES;
1703   send_client_buffered_data (ch, c, fwd);
1704
1705   if (GNUNET_YES == ch->destroy && 0 == rel->n_recv)
1706   {
1707     send_destroy (ch, GNUNET_YES);
1708     GCCH_destroy (ch);
1709     return;
1710   }
1711   /* if loopback is marked for destruction, no need to ACK to the other peer,
1712    * it requested the destruction and is already gone, therefore, else if.
1713    */
1714   else if (is_loopback (ch))
1715   {
1716     unsigned int buffer;
1717
1718     buffer = GCCH_get_buffer (ch, fwd);
1719     if (0 < buffer)
1720       GCCH_allow_client (ch, fwd);
1721
1722     return;
1723   }
1724   GCT_send_connection_acks (ch->t);
1725 }
1726
1727
1728 /**
1729  * Handle data given by a client.
1730  *
1731  * Check whether the client is allowed to send in this tunnel, save if channel
1732  * is reliable and send an ACK to the client if there is still buffer space
1733  * in the tunnel.
1734  *
1735  * @param ch Channel.
1736  * @param c Client which sent the data.
1737  * @param fwd Is this a FWD data?
1738  * @param message Data message.
1739  * @param size Size of data.
1740  *
1741  * @return #GNUNET_OK if everything goes well, #GNUNET_SYSERR in case of en error.
1742  */
1743 int
1744 GCCH_handle_local_data (struct CadetChannel *ch,
1745                         struct CadetClient *c,
1746                         int fwd,
1747                         const struct GNUNET_MessageHeader *message,
1748                         size_t size)
1749 {
1750   struct CadetChannelReliability *rel;
1751   struct GNUNET_CADET_ChannelDataMessage *payload;
1752   uint16_t p2p_size = sizeof(struct GNUNET_CADET_ChannelDataMessage) + size;
1753   unsigned char cbuf[p2p_size];
1754   unsigned char buffer;
1755
1756   /* Is the client in the channel? */
1757   if ( !( (fwd &&
1758            ch->root == c)
1759          ||
1760           (!fwd &&
1761            ch->dest == c) ) )
1762   {
1763     GNUNET_break_op (0);
1764     return GNUNET_SYSERR;
1765   }
1766
1767   rel = fwd ? ch->root_rel : ch->dest_rel;
1768
1769   if (GNUNET_NO == rel->client_allowed)
1770   {
1771     GNUNET_break_op (0);
1772     return GNUNET_SYSERR;
1773   }
1774
1775   rel->client_allowed = GNUNET_NO;
1776
1777   /* Ok, everything is correct, send the message. */
1778   payload = (struct GNUNET_CADET_ChannelDataMessage *) cbuf;
1779   payload->mid = htonl (rel->mid_send);
1780   rel->mid_send++;
1781   GNUNET_memcpy (&payload[1], message, size);
1782   payload->header.size = htons (p2p_size);
1783   payload->header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DATA);
1784   payload->chid = ch->gid;
1785   LOG (GNUNET_ERROR_TYPE_DEBUG, "  sending on channel...\n");
1786   GCCH_send_prebuilt_message (&payload->header, ch, fwd, NULL);
1787
1788   if (is_loopback (ch))
1789     buffer = GCCH_get_buffer (ch, fwd);
1790   else
1791     buffer = GCT_get_connections_buffer (ch->t);
1792
1793   if (0 < buffer)
1794     GCCH_allow_client (ch, fwd);
1795
1796   return GNUNET_OK;
1797 }
1798
1799
1800 /**
1801  * Handle a channel destroy requested by a client.
1802  *
1803  * TODO: add "reason" field
1804  *
1805  * Destroy the channel and the tunnel in case this was the last channel.
1806  *
1807  * @param ch Channel.
1808  * @param c Client that requested the destruction (to avoid notifying him).
1809  * @param is_root Is the request coming from root?
1810  */
1811 void
1812 GCCH_handle_local_destroy (struct CadetChannel *ch,
1813                            struct CadetClient *c,
1814                            int is_root)
1815 {
1816   ch->destroy = GNUNET_YES;
1817   /* Cleanup after the tunnel */
1818   if (GNUNET_NO == is_root && c == ch->dest)
1819   {
1820     LOG (GNUNET_ERROR_TYPE_DEBUG, " Client %s is destination.\n", GML_2s (c));
1821     GML_client_delete_channel (c, ch, ch->lid_dest);
1822     ch->dest = NULL;
1823   }
1824   if (GNUNET_YES == is_root && c == ch->root)
1825   {
1826     LOG (GNUNET_ERROR_TYPE_DEBUG, " Client %s is owner.\n", GML_2s (c));
1827     GML_client_delete_channel (c, ch, ch->lid_root);
1828     ch->root = NULL;
1829   }
1830
1831   send_destroy (ch, GNUNET_NO);
1832   if (0 == ch->pending_messages)
1833     GCCH_destroy (ch);
1834 }
1835
1836
1837 /**
1838  * Handle a channel create requested by a client.
1839  *
1840  * Create the channel and the tunnel in case this was the first0 channel.
1841  *
1842  * @param c Client that requested the creation (will be the root).
1843  * @param msg Create Channel message.
1844  *
1845  * @return GNUNET_OK if everything went fine, GNUNET_SYSERR otherwise.
1846  */
1847 int
1848 GCCH_handle_local_create (struct CadetClient *c,
1849                           struct GNUNET_CADET_ChannelCreateMessageMessage *msg)
1850 {
1851   struct CadetChannel *ch;
1852   struct CadetTunnel *t;
1853   struct CadetPeer *peer;
1854   struct GNUNET_CADET_ClientChannelNumber chid;
1855
1856   LOG (GNUNET_ERROR_TYPE_DEBUG, "  towards %s:%u\n",
1857        GNUNET_i2s (&msg->peer), GNUNET_h2s (&msg->port));
1858   chid = msg->channel_id;
1859
1860   /* Sanity check for duplicate channel IDs */
1861   if (NULL != GML_channel_get (c, chid))
1862   {
1863     GNUNET_break (0);
1864     return GNUNET_SYSERR;
1865   }
1866
1867   peer = GCP_get (&msg->peer, GNUNET_YES);
1868   GCP_add_tunnel (peer);
1869   t = GCP_get_tunnel (peer);
1870
1871   if (GCP_get_short_id (peer) == myid)
1872   {
1873     GCT_change_cstate (t, CADET_TUNNEL_READY);
1874   }
1875   else
1876   {
1877     /* FIXME change to a tunnel API, eliminate ch <-> peer connection */
1878     GCP_connect (peer);
1879   }
1880
1881   /* Create channel */
1882   ch = channel_new (t, c, chid);
1883   if (NULL == ch)
1884   {
1885     GNUNET_break (0);
1886     return GNUNET_SYSERR;
1887   }
1888   ch->port = msg->port;
1889   channel_set_options (ch, ntohl (msg->opt));
1890
1891   /* In unreliable channels, we'll use the DLL to buffer BCK data */
1892   ch->root_rel = GNUNET_new (struct CadetChannelReliability);
1893   ch->root_rel->ch = ch;
1894   ch->root_rel->retry_timer = CADET_RETRANSMIT_TIME;
1895   ch->root_rel->expected_delay.rel_value_us = 0;
1896
1897   LOG (GNUNET_ERROR_TYPE_DEBUG, "CREATED CHANNEL %s\n", GCCH_2s (ch));
1898
1899   send_create (ch);
1900
1901   return GNUNET_OK;
1902 }
1903
1904
1905 /**
1906  * Handler for cadet network payload traffic.
1907  *
1908  * @param ch Channel for the message.
1909  * @param msg Unencryted data message.
1910  * @param fwd Is this message fwd? This only is meaningful in loopback channels.
1911  *            #GNUNET_YES if message is FWD on the respective channel (loopback)
1912  *            #GNUNET_NO if message is BCK on the respective channel (loopback)
1913  *            #GNUNET_SYSERR if message on a one-ended channel (remote)
1914  */
1915 void
1916 GCCH_handle_data (struct CadetChannel *ch,
1917                   const struct GNUNET_CADET_ChannelDataMessage *msg,
1918                   int fwd)
1919 {
1920   struct CadetChannelReliability *rel;
1921   struct CadetClient *c;
1922   struct GNUNET_MessageHeader *payload_msg;
1923   uint32_t mid;
1924   uint16_t payload_type;
1925   uint16_t payload_size;
1926
1927   /* If this is a remote (non-loopback) channel, find 'fwd'. */
1928   if (GNUNET_SYSERR == fwd)
1929   {
1930     if (is_loopback (ch))
1931     {
1932       /* It is a loopback channel after all... */
1933       GNUNET_break (0);
1934       return;
1935     }
1936     fwd = (NULL != ch->dest) ? GNUNET_YES : GNUNET_NO;
1937   }
1938
1939   /*  Initialize FWD/BCK data */
1940   c   = fwd ? ch->dest     : ch->root;
1941   rel = fwd ? ch->dest_rel : ch->root_rel;
1942
1943   if (NULL == c)
1944   {
1945     GNUNET_break (GNUNET_NO != ch->destroy);
1946     return;
1947   }
1948
1949   if (CADET_CHANNEL_READY != ch->state)
1950   {
1951     if (GNUNET_NO == fwd)
1952     {
1953       /* If we are the root, this means the other peer has sent traffic before
1954        * receiving our ACK. Even if the SYNACK goes missing, no traffic should
1955        * be sent before the ACK.
1956        */
1957       GNUNET_break_op (0);
1958       return;
1959     }
1960     /* If we are the dest, this means that the SYNACK got to the root but
1961      * the ACK went missing. Treat this as an ACK.
1962      */
1963     channel_confirm (ch, GNUNET_NO);
1964   }
1965
1966   payload_msg = (struct GNUNET_MessageHeader *) &msg[1];
1967   payload_type = ntohs (payload_msg->type);
1968   payload_size = ntohs (payload_msg->size);
1969
1970   GNUNET_STATISTICS_update (stats, "# messages received", 1, GNUNET_NO);
1971   GNUNET_STATISTICS_update (stats, "# bytes received", payload_size, GNUNET_NO);
1972
1973   mid = ntohl (msg->mid);
1974   LOG (GNUNET_ERROR_TYPE_INFO, "<== %s (%s %4u) on chan %s (%p) %s [%5u]\n",
1975        GC_m2s (GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DATA), GC_m2s (payload_type), mid,
1976        GCCH_2s (ch), ch, GC_f2s (fwd), ntohs (msg->header.size));
1977
1978   if ( (GNUNET_NO == ch->reliable) ||
1979        ( (! GC_is_pid_bigger (rel->mid_recv, mid)) &&
1980          GC_is_pid_bigger (rel->mid_recv + 64, mid) ) )
1981   {
1982     if (GNUNET_YES == ch->reliable)
1983     {
1984       /* Is this the exact next expected messasge? */
1985       if (mid == rel->mid_recv)
1986       {
1987         LOG (GNUNET_ERROR_TYPE_DEBUG,
1988              "as expected, sending to client\n");
1989         send_client_data (ch, msg, fwd);
1990       }
1991       else
1992       {
1993         LOG (GNUNET_ERROR_TYPE_DEBUG,
1994              "save for later\n");
1995         add_buffered_data (msg, rel);
1996       }
1997     }
1998     else
1999     {
2000       /* Tunnel is unreliable: send to clients directly */
2001       /* FIXME: accept Out Of Order traffic */
2002       rel->mid_recv = mid + 1;
2003       send_client_data (ch, msg, fwd);
2004     }
2005   }
2006   else
2007   {
2008     GNUNET_STATISTICS_update (stats, "# duplicate MID", 1, GNUNET_NO);
2009     if (GC_is_pid_bigger (rel->mid_recv, mid))
2010     {
2011       GNUNET_break_op (0);
2012       LOG (GNUNET_ERROR_TYPE_WARNING,
2013            "MID %u on channel %s not expected (window: %u - %u). Dropping!\n",
2014            mid, GCCH_2s (ch), rel->mid_recv, rel->mid_recv + 63);
2015     }
2016     else
2017     {
2018       LOG (GNUNET_ERROR_TYPE_INFO,
2019            "Duplicate MID %u, channel %s (expecting MID %u). Re-sending ACK!\n",
2020            mid, GCCH_2s (ch), rel->mid_recv);
2021       if (NULL != rel->uniq)
2022       {
2023         LOG (GNUNET_ERROR_TYPE_WARNING,
2024              "We are trying to send an ACK, but don't seem have the "
2025              "bandwidth. Have you set enough [ats] QUOTA in your config?\n");
2026       }
2027
2028     }
2029   }
2030
2031   GCCH_send_data_ack (ch, fwd);
2032 }
2033
2034
2035 /**
2036  * Handler for cadet network traffic end-to-end ACKs.
2037  *
2038  * @param ch Channel on which we got this message.
2039  * @param msg Data message.
2040  * @param fwd Is this message fwd? This only is meaningful in loopback channels.
2041  *            #GNUNET_YES if message is FWD on the respective channel (loopback)
2042  *            #GNUNET_NO if message is BCK on the respective channel (loopback)
2043  *            #GNUNET_SYSERR if message on a one-ended channel (remote)
2044  */
2045 void
2046 GCCH_handle_data_ack (struct CadetChannel *ch,
2047                       const struct GNUNET_CADET_ChannelDataAckMessage *msg,
2048                       int fwd)
2049 {
2050   struct CadetChannelReliability *rel;
2051   struct CadetReliableMessage *copy;
2052   struct CadetReliableMessage *next;
2053   uint32_t ack;
2054   int work;
2055
2056   /* If this is a remote (non-loopback) channel, find 'fwd'. */
2057   if (GNUNET_SYSERR == fwd)
2058   {
2059     if (is_loopback (ch))
2060     {
2061       /* It is a loopback channel after all... */
2062       GNUNET_break (0);
2063       return;
2064     }
2065     /* Inverted: if message came 'FWD' is a 'BCK ACK'. */
2066     fwd = (NULL != ch->dest) ? GNUNET_NO : GNUNET_YES;
2067   }
2068
2069   ack = ntohl (msg->mid);
2070   LOG (GNUNET_ERROR_TYPE_INFO,
2071        "<== %s (0x%010lX %4u) on chan %s (%p) %s [%5u]\n",
2072        GC_m2s (GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DATA_ACK), msg->futures, ack,
2073        GCCH_2s (ch), ch, GC_f2s (fwd), ntohs (msg->header.size));
2074
2075   if (GNUNET_YES == fwd)
2076     rel = ch->root_rel;
2077   else
2078     rel = ch->dest_rel;
2079
2080   if (NULL == rel)
2081   {
2082     GNUNET_break (GNUNET_NO != ch->destroy);
2083     return;
2084   }
2085
2086   /* Free ACK'd copies: no need to retransmit those anymore FIXME refactor */
2087   for (work = GNUNET_NO, copy = rel->head_sent; copy != NULL; copy = next)
2088   {
2089     if (GC_is_pid_bigger (copy->mid, ack))
2090     {
2091       LOG (GNUNET_ERROR_TYPE_DEBUG, "  head %u, out!\n", copy->mid);
2092       if (0 < channel_rel_free_sent (rel, msg))
2093         work = GNUNET_YES;
2094       break;
2095     }
2096     work = GNUNET_YES;
2097     LOG (GNUNET_ERROR_TYPE_DEBUG, "  id %u\n", copy->mid);
2098     next = copy->next;
2099     if (GNUNET_YES == rel_message_free (copy, GNUNET_YES))
2100     {
2101       LOG (GNUNET_ERROR_TYPE_DEBUG, " channel destoyed\n");
2102       return;
2103     }
2104   }
2105
2106   /* ACK client if needed and possible */
2107   GCCH_allow_client (ch, fwd);
2108
2109   /* If some message was free'd, update the retransmission delay */
2110   if (GNUNET_YES == work)
2111   {
2112     if (NULL != rel->retry_task)
2113     {
2114       GNUNET_SCHEDULER_cancel (rel->retry_task);
2115       rel->retry_task = NULL;
2116       if (NULL != rel->head_sent && NULL == rel->head_sent->chq)
2117       {
2118         struct GNUNET_TIME_Absolute new_target;
2119         struct GNUNET_TIME_Relative delay;
2120
2121         delay = GNUNET_TIME_relative_saturating_multiply (rel->retry_timer,
2122                                                           CADET_RETRANSMIT_MARGIN);
2123         new_target = GNUNET_TIME_absolute_add (rel->head_sent->timestamp,
2124                                                delay);
2125         delay = GNUNET_TIME_absolute_get_remaining (new_target);
2126         rel->retry_task =
2127             GNUNET_SCHEDULER_add_delayed (delay,
2128                                           &channel_retransmit_message,
2129                                           rel);
2130       }
2131     }
2132     else
2133     {
2134       /* Work was done but no task was pending.
2135        * Task was cancelled by a retransmission that is sitting in the queue.
2136        */
2137       // FIXME add test to make sure this is the case, probably add return
2138       // value to GCCH_send_prebuilt_message
2139     }
2140   }
2141 }
2142
2143
2144 /**
2145  * Handler for channel create messages.
2146  *
2147  * Does not have fwd parameter because it's always 'FWD': channel is incoming.
2148  *
2149  * @param t Tunnel this channel will be in.
2150  * @param msg Channel crate message.
2151  */
2152 struct CadetChannel *
2153 GCCH_handle_create (struct CadetTunnel *t,
2154                     const struct GNUNET_CADET_ChannelCreateMessage *msg)
2155 {
2156   struct GNUNET_CADET_ClientChannelNumber chid;
2157   struct GNUNET_CADET_ChannelNumber gid;
2158   struct CadetChannel *ch;
2159   struct CadetClient *c;
2160   int new_channel;
2161   const struct GNUNET_HashCode *port;
2162
2163   gid = msg->chid;
2164   ch = GCT_get_channel (t, gid);
2165   if (NULL == ch)
2166   {
2167     /* Create channel */
2168     chid.channel_of_client = htonl (0);
2169     ch = channel_new (t, NULL, chid);
2170     ch->gid = gid;
2171     channel_set_options (ch, ntohl (msg->opt));
2172     new_channel = GNUNET_YES;
2173   }
2174   else
2175   {
2176     new_channel = GNUNET_NO;
2177   }
2178   port = &msg->port;
2179
2180   LOG (GNUNET_ERROR_TYPE_INFO,
2181        "<== %s (  0x%08X %4u) on chan %s (%p) %s [%5u]\n",
2182        GC_m2s (GNUNET_MESSAGE_TYPE_CADET_CHANNEL_CREATE), chid, port,
2183        GCCH_2s (ch), ch, GC_f2s (GNUNET_YES), ntohs (msg->header.size));
2184
2185   if (GNUNET_YES == new_channel || GCT_is_loopback (t))
2186   {
2187     /* Find a destination client */
2188     ch->port = *port;
2189     LOG (GNUNET_ERROR_TYPE_DEBUG, "   port %s\n", GNUNET_h2s (port));
2190     c = GML_client_get_by_port (port);
2191     if (NULL == c)
2192     {
2193       LOG (GNUNET_ERROR_TYPE_DEBUG, "  no client has port registered\n");
2194       if (is_loopback (ch))
2195       {
2196         LOG (GNUNET_ERROR_TYPE_DEBUG, "  loopback: destroy on handler\n");
2197         send_nack (ch);
2198       }
2199       else
2200       {
2201         LOG (GNUNET_ERROR_TYPE_DEBUG, "  not loopback: destroy now\n");
2202         send_nack (ch);
2203         GCCH_destroy (ch);
2204       }
2205       return NULL;
2206     }
2207     else
2208     {
2209       LOG (GNUNET_ERROR_TYPE_DEBUG, "  client %p has port registered\n", c);
2210     }
2211
2212     add_destination (ch, c);
2213     if (GNUNET_YES == ch->reliable)
2214       LOG (GNUNET_ERROR_TYPE_DEBUG, "Reliable\n");
2215     else
2216       LOG (GNUNET_ERROR_TYPE_DEBUG, "Not Reliable\n");
2217
2218     send_client_create (ch);
2219     ch->state =  CADET_CHANNEL_SENT;
2220   }
2221   else
2222   {
2223     LOG (GNUNET_ERROR_TYPE_DEBUG, "  duplicate create channel\n");
2224     if (NULL != ch->dest_rel->retry_task)
2225     {
2226       LOG (GNUNET_ERROR_TYPE_DEBUG, "  clearing retry task\n");
2227       /* we were waiting to re-send our 'SYNACK', wait no more! */
2228       GNUNET_SCHEDULER_cancel (ch->dest_rel->retry_task);
2229       ch->dest_rel->retry_task = NULL;
2230     }
2231     else if (NULL != ch->dest_rel->uniq)
2232     {
2233       /* we are waiting to for our 'SYNACK' to leave the queue, all done! */
2234       return ch;
2235     }
2236   }
2237   send_ack (ch, GNUNET_YES);
2238
2239   return ch;
2240 }
2241
2242
2243 /**
2244  * Handler for channel NACK messages.
2245  *
2246  * NACK messages always go dest -> root, no need for 'fwd' or 'msg' parameter.
2247  *
2248  * @param ch Channel.
2249  */
2250 void
2251 GCCH_handle_nack (struct CadetChannel *ch)
2252 {
2253   LOG (GNUNET_ERROR_TYPE_INFO,
2254        "<== %s (  0x%08X %4u) on chan %s (%p) %s [%5u]\n",
2255        GC_m2s (GNUNET_MESSAGE_TYPE_CADET_CHANNEL_CREATE_NACK_DEPRECATED), ch->gid, 0,
2256        GCCH_2s (ch), ch, "---", 0);
2257
2258   send_client_nack (ch);
2259   GCCH_destroy (ch);
2260 }
2261
2262
2263 /**
2264  * Handler for channel ack messages.
2265  *
2266  * @param ch Channel.
2267  * @param msg Message.
2268  * @param fwd Is this message fwd? This only is meaningful in loopback channels.
2269  *            #GNUNET_YES if message is FWD on the respective channel (loopback)
2270  *            #GNUNET_NO if message is BCK on the respective channel (loopback)
2271  *            #GNUNET_SYSERR if message on a one-ended channel (remote)
2272  */
2273 void
2274 GCCH_handle_ack (struct CadetChannel *ch,
2275                  const struct GNUNET_CADET_ChannelManageMessage *msg,
2276                  int fwd)
2277 {
2278   LOG (GNUNET_ERROR_TYPE_INFO,
2279        "<== %s (  0x%08X %4u) on chan %s (%p) %s [%5u]\n",
2280        GC_m2s (GNUNET_MESSAGE_TYPE_CADET_CHANNEL_CREATE_ACK), ch->gid, 0,
2281        GCCH_2s (ch), ch, GC_f2s (fwd), ntohs (msg->header.size));
2282
2283   /* If this is a remote (non-loopback) channel, find 'fwd'. */
2284   if (GNUNET_SYSERR == fwd)
2285   {
2286     if (is_loopback (ch))
2287     {
2288       /* It is a loopback channel after all... */
2289       GNUNET_break (0);
2290       return;
2291     }
2292     fwd = (NULL != ch->dest) ? GNUNET_YES : GNUNET_NO;
2293   }
2294
2295   channel_confirm (ch, !fwd);
2296 }
2297
2298
2299 /**
2300  * Handler for channel destroy messages.
2301  *
2302  * @param ch Channel to be destroyed of.
2303  * @param msg Message.
2304  * @param fwd Is this message fwd? This only is meaningful in loopback channels.
2305  *            #GNUNET_YES if message is FWD on the respective channel (loopback)
2306  *            #GNUNET_NO if message is BCK on the respective channel (loopback)
2307  *            #GNUNET_SYSERR if message on a one-ended channel (remote)
2308  */
2309 void
2310 GCCH_handle_destroy (struct CadetChannel *ch,
2311                      const struct GNUNET_CADET_ChannelManageMessage *msg,
2312                      int fwd)
2313 {
2314   struct CadetChannelReliability *rel;
2315
2316   LOG (GNUNET_ERROR_TYPE_INFO,
2317        "<== %s (  0x%08X %4u) on chan %s (%p) %s [%5u]\n",
2318        GC_m2s (GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DESTROY), ch->gid, 0,
2319        GCCH_2s (ch), ch, GC_f2s (fwd), ntohs (msg->header.size));
2320
2321   /* If this is a remote (non-loopback) channel, find 'fwd'. */
2322   if (GNUNET_SYSERR == fwd)
2323   {
2324     if (is_loopback (ch))
2325     {
2326       /* It is a loopback channel after all... */
2327       GNUNET_break (0);
2328       return;
2329     }
2330     fwd = (NULL != ch->dest) ? GNUNET_YES : GNUNET_NO;
2331   }
2332
2333   GCCH_debug (ch, GNUNET_ERROR_TYPE_DEBUG);
2334   if ( (fwd && NULL == ch->dest) || (!fwd && NULL == ch->root) )
2335   {
2336     /* Not for us (don't destroy twice a half-open loopback channel) */
2337     return;
2338   }
2339
2340   rel = fwd ? ch->dest_rel : ch->root_rel;
2341   if (0 == rel->n_recv)
2342   {
2343     send_destroy (ch, GNUNET_YES);
2344     GCCH_destroy (ch);
2345   }
2346   else
2347   {
2348     ch->destroy = GNUNET_YES;
2349   }
2350 }
2351
2352
2353 /**
2354  * Sends an already built message on a channel.
2355  *
2356  * If the channel is on a loopback tunnel, notifies the appropriate destination
2357  * client locally.
2358  *
2359  * On a normal channel passes the message to the tunnel for encryption and
2360  * sending on a connection.
2361  *
2362  * This function DOES NOT save the message for retransmission.
2363  *
2364  * @param message Message to send. Function makes a copy of it.
2365  * @param ch Channel on which this message is transmitted.
2366  * @param fwd Is this a fwd message?
2367  * @param existing_copy This is a retransmission, don't save a copy.
2368  */
2369 void
2370 GCCH_send_prebuilt_message (const struct GNUNET_MessageHeader *message,
2371                             struct CadetChannel *ch, int fwd,
2372                             void *existing_copy)
2373 {
2374   struct CadetChannelQueue *chq;
2375   uint32_t data_id;
2376   uint16_t type;
2377   uint16_t size;
2378   char info[32];
2379
2380   type = ntohs (message->type);
2381   size = ntohs (message->size);
2382
2383   data_id = 0;
2384   switch (type)
2385   {
2386     case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DATA:
2387     {
2388       struct GNUNET_CADET_ChannelDataMessage *data_msg;
2389       struct GNUNET_MessageHeader *payload_msg;
2390       uint16_t payload_type;
2391
2392       data_msg = (struct GNUNET_CADET_ChannelDataMessage *) message;
2393       data_id = ntohl (data_msg->mid);
2394       payload_msg = (struct GNUNET_MessageHeader *) &data_msg[1];
2395       payload_type = ntohs (payload_msg->type);
2396       strncpy (info, GC_m2s (payload_type), 31);
2397       info[31] = '\0';
2398       break;
2399     }
2400     case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DATA_ACK:
2401     {
2402       struct GNUNET_CADET_ChannelDataAckMessage *ack_msg;
2403       ack_msg = (struct GNUNET_CADET_ChannelDataAckMessage *) message;
2404       data_id = ntohl (ack_msg->mid);
2405       SPRINTF (info, "0x%010lX",
2406                (unsigned long int) ack_msg->futures);
2407       break;
2408     }
2409     case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_CREATE:
2410     {
2411       struct GNUNET_CADET_ChannelCreateMessage *cc_msg;
2412       cc_msg = (struct GNUNET_CADET_ChannelCreateMessage *) message;
2413       SPRINTF (info, "  0x%08X", ntohl (cc_msg->chid.cn));
2414       break;
2415     }
2416     case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_CREATE_ACK:
2417     case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_CREATE_NACK_DEPRECATED:
2418     case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DESTROY:
2419     {
2420       struct GNUNET_CADET_ChannelManageMessage *m_msg;
2421       m_msg = (struct GNUNET_CADET_ChannelManageMessage *) message;
2422       SPRINTF (info, "  0x%08X", ntohl (m_msg->chid.cn));
2423       break;
2424     }
2425     default:
2426       info[0] = '\0';
2427   }
2428   LOG (GNUNET_ERROR_TYPE_INFO,
2429        "==> %s (%12s %4u) on chan %s (%p) %s [%5u]\n",
2430        GC_m2s (type), info, data_id,
2431        GCCH_2s (ch), ch, GC_f2s (fwd), size);
2432
2433   if (GCT_is_loopback (ch->t))
2434   {
2435     handle_loopback (ch, message, fwd);
2436     return;
2437   }
2438
2439   switch (type)
2440   {
2441     case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DATA:
2442       if (GNUNET_YES == ch->reliable)
2443       {
2444         chq = GNUNET_new (struct CadetChannelQueue);
2445         chq->type = type;
2446         if (NULL == existing_copy)
2447           chq->copy = channel_save_copy (ch, message, fwd);
2448         else
2449         {
2450           chq->copy = (struct CadetReliableMessage *) existing_copy;
2451           if (NULL != chq->copy->chq)
2452           {
2453             /* Last retransmission was queued but not yet sent!
2454              * This retransmission was scheduled by a ch_message_sent which
2455              * followed a very fast RTT, so the tiny delay made the
2456              * retransmission function to execute before the previous
2457              * retransmitted message even had a chance to leave the peer.
2458              * Cancel this message and wait until the pending
2459              * retransmission leaves the peer and ch_message_sent starts
2460              * the timer for the next one.
2461              */
2462             GNUNET_free (chq);
2463             LOG (GNUNET_ERROR_TYPE_DEBUG,
2464                  "  exisitng copy not yet transmitted!\n");
2465             return;
2466           }
2467           LOG (GNUNET_ERROR_TYPE_DEBUG,
2468                "  using existing copy: %p {r:%p q:%p t:%u}\n",
2469                existing_copy,
2470                chq->copy->rel, chq->copy->chq, chq->copy->type);
2471         }
2472         LOG (GNUNET_ERROR_TYPE_DEBUG, "  new chq: %p\n", chq);
2473         chq->copy->chq = chq;
2474         chq->tq = GCT_send_prebuilt_message (message, ch->t, NULL,
2475                                              GNUNET_YES,
2476                                              &ch_message_sent, chq);
2477         /* q itself is stored in copy */
2478         GNUNET_assert (NULL != chq->tq || GNUNET_NO != ch->destroy);
2479       }
2480       else
2481       {
2482         fire_and_forget (message, ch, GNUNET_NO);
2483       }
2484       break;
2485
2486
2487     case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DATA_ACK:
2488     case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_CREATE:
2489     case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_CREATE_ACK:
2490       chq = GNUNET_new (struct CadetChannelQueue);
2491       chq->type = type;
2492       chq->rel = fwd ? ch->root_rel : ch->dest_rel;
2493       if (NULL != chq->rel->uniq)
2494       {
2495         if (NULL != chq->rel->uniq->tq)
2496         {
2497           GCT_cancel (chq->rel->uniq->tq);
2498           /* ch_message_sent is called, freeing and NULLing uniq */
2499           GNUNET_break (NULL == chq->rel->uniq);
2500         }
2501         else
2502         {
2503           GNUNET_break (0);
2504           GNUNET_free (chq->rel->uniq);
2505         }
2506       }
2507
2508       chq->rel->uniq = chq;
2509       chq->tq = GCT_send_prebuilt_message (message, ch->t, NULL, GNUNET_YES,
2510                                            &ch_message_sent, chq);
2511       if (NULL == chq->tq)
2512       {
2513         GNUNET_break (0);
2514         chq->rel->uniq = NULL;
2515         GCT_debug (ch->t, GNUNET_ERROR_TYPE_ERROR);
2516         GNUNET_free (chq);
2517         chq = NULL;
2518         return;
2519       }
2520       break;
2521
2522
2523     case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DESTROY:
2524     case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_CREATE_NACK_DEPRECATED:
2525       fire_and_forget (message, ch, GNUNET_YES);
2526       break;
2527
2528
2529     default:
2530       GNUNET_break (0);
2531       LOG (GNUNET_ERROR_TYPE_WARNING, "type %s unknown!\n", GC_m2s (type));
2532       fire_and_forget (message, ch, GNUNET_YES);
2533   }
2534 }
2535
2536
2537 /**
2538  * Get the static string for identification of the channel.
2539  *
2540  * @param ch Channel.
2541  *
2542  * @return Static string with the channel IDs.
2543  */
2544 const char *
2545 GCCH_2s (const struct CadetChannel *ch)
2546 {
2547   static char buf[64];
2548
2549   if (NULL == ch)
2550     return "(NULL Channel)";
2551
2552   SPRINTF (buf,
2553            "%s:%s gid:%X (%X / %X)",
2554            GCT_2s (ch->t),
2555            GNUNET_h2s (&ch->port),
2556            ntohl (ch->gid.cn),
2557            ntohl (ch->lid_root.channel_of_client),
2558            ntohl (ch->lid_dest.channel_of_client));
2559
2560   return buf;
2561 }