4b5baa528d777313eaa62a089fe81d60dee85194
[oweals/gnunet.git] / src / mesh / gnunet-service-mesh_channel.c
1 /*
2      This file is part of GNUnet.
3      (C) 2013 Christian Grothoff (and other contributing authors)
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., 59 Temple Place - Suite 330,
18      Boston, MA 02111-1307, USA.
19 */
20
21
22 #include "platform.h"
23 #include "gnunet_util_lib.h"
24
25 #include "gnunet_statistics_service.h"
26
27 #include "mesh_enc.h"
28 #include "mesh_protocol_enc.h"
29
30 #include "gnunet-service-mesh_channel.h"
31 #include "gnunet-service-mesh_local.h"
32 #include "gnunet-service-mesh_tunnel.h"
33 #include "gnunet-service-mesh_peer.h"
34
35 #define LOG(level, ...) GNUNET_log_from(level,"mesh-chn",__VA_ARGS__)
36
37 #define MESH_RETRANSMIT_TIME    GNUNET_TIME_UNIT_SECONDS
38 #define MESH_RETRANSMIT_MARGIN  4
39
40
41 /**
42  * All the states a connection can be in.
43  */
44 enum MeshChannelState
45 {
46   /**
47    * Uninitialized status, should never appear in operation.
48    */
49   MESH_CHANNEL_NEW,
50
51   /**
52    * Connection create message sent, waiting for ACK.
53    */
54   MESH_CHANNEL_SENT,
55
56   /**
57    * Connection confirmed, ready to carry traffic.
58    */
59   MESH_CHANNEL_READY,
60 };
61
62
63
64 /**
65  * Info needed to retry a message in case it gets lost.
66  */
67 struct MeshReliableMessage
68 {
69     /**
70      * Double linked list, FIFO style
71      */
72   struct MeshReliableMessage    *next;
73   struct MeshReliableMessage    *prev;
74
75     /**
76      * Type of message (payload, channel management).
77      */
78   int16_t type;
79
80     /**
81      * Tunnel Reliability queue this message is in.
82      */
83   struct MeshChannelReliability  *rel;
84
85     /**
86      * ID of the message (ACK needed to free)
87      */
88   uint32_t                      mid;
89
90     /**
91      * When was this message issued (to calculate ACK delay)
92      */
93   struct GNUNET_TIME_Absolute   timestamp;
94
95   /* struct GNUNET_MESH_Data with payload */
96 };
97
98
99 /**
100  * Info about the traffic state for a client in a channel.
101  */
102 struct MeshChannelReliability
103 {
104     /**
105      * Channel this is about.
106      */
107   struct MeshChannel *ch;
108
109     /**
110      * DLL of messages sent and not yet ACK'd.
111      */
112   struct MeshReliableMessage        *head_sent;
113   struct MeshReliableMessage        *tail_sent;
114
115     /**
116      * Messages pending to send.
117      */
118   unsigned int                      n_sent;
119
120     /**
121      * DLL of messages received out of order.
122      */
123   struct MeshReliableMessage        *head_recv;
124   struct MeshReliableMessage        *tail_recv;
125
126     /**
127      * Messages received.
128      */
129   unsigned int                      n_recv;
130
131     /**
132      * Next MID to use for outgoing traffic.
133      */
134   uint32_t                          mid_send;
135
136     /**
137      * Next MID expected for incoming traffic.
138      */
139   uint32_t                          mid_recv;
140
141     /**
142      * Can we send data to the client?
143      */
144   int                               client_ready;
145
146   /**
147    * Can the client send data to us?
148    */
149   int                               client_allowed;
150
151     /**
152      * Task to resend/poll in case no ACK is received.
153      */
154   GNUNET_SCHEDULER_TaskIdentifier   retry_task;
155
156     /**
157      * Counter for exponential backoff.
158      */
159   struct GNUNET_TIME_Relative       retry_timer;
160
161     /**
162      * How long does it usually take to get an ACK.
163      */
164   struct GNUNET_TIME_Relative       expected_delay;
165 };
166
167
168 /**
169  * Struct containing all information regarding a channel to a remote client.
170  */
171 struct MeshChannel
172 {
173     /**
174      * Tunnel this channel is in.
175      */
176   struct MeshTunnel3 *t;
177
178     /**
179      * Destination port of the channel.
180      */
181   uint32_t port;
182
183     /**
184      * Global channel number ( < GNUNET_MESH_LOCAL_CHANNEL_ID_CLI)
185      */
186   MESH_ChannelNumber gid;
187
188     /**
189      * Local tunnel number for root (owner) client.
190      * ( >= GNUNET_MESH_LOCAL_CHANNEL_ID_CLI or 0 )
191      */
192   MESH_ChannelNumber lid_root;
193
194     /**
195      * Local tunnel number for local destination clients (incoming number)
196      * ( >= GNUNET_MESH_LOCAL_CHANNEL_ID_SERV or 0).
197      */
198   MESH_ChannelNumber lid_dest;
199
200     /**
201      * Channel state.
202      */
203   enum MeshChannelState state;
204
205     /**
206      * Is the tunnel bufferless (minimum latency)?
207      */
208   int nobuffer;
209
210     /**
211      * Is the tunnel reliable?
212      */
213   int reliable;
214
215     /**
216      * Last time the channel was used
217      */
218   struct GNUNET_TIME_Absolute timestamp;
219
220     /**
221      * Client owner of the tunnel, if any
222      */
223   struct MeshClient *root;
224
225     /**
226      * Client destination of the tunnel, if any.
227      */
228   struct MeshClient *dest;
229
230     /**
231      * Flag to signal the destruction of the channel.
232      * If this is set GNUNET_YES the channel will be destroyed
233      * when the queue is empty.
234      */
235   int destroy;
236
237     /**
238      * Total messages pending for this channel, payload or not.
239      */
240   unsigned int pending_messages;
241
242     /**
243      * Reliability data.
244      * Only present (non-NULL) at the owner of a tunnel.
245      */
246   struct MeshChannelReliability *root_rel;
247
248     /**
249      * Reliability data.
250      * Only present (non-NULL) at the destination of a tunnel.
251      */
252   struct MeshChannelReliability *dest_rel;
253
254 };
255
256
257 /******************************************************************************/
258 /*******************************   GLOBALS  ***********************************/
259 /******************************************************************************/
260
261 /**
262  * Global handle to the statistics service.
263  */
264 extern struct GNUNET_STATISTICS_Handle *stats;
265
266 /**
267  * Local peer own ID (memory efficient handle).
268  */
269 extern GNUNET_PEER_Id myid;
270
271
272 /******************************************************************************/
273 /********************************   STATIC  ***********************************/
274 /******************************************************************************/
275
276 /**
277  * Destroy a reliable message after it has been acknowledged, either by
278  * direct mid ACK or bitfield. Updates the appropriate data structures and
279  * timers and frees all memory.
280  *
281  * @param copy Message that is no longer needed: remote peer got it.
282  */
283 static void
284 rel_message_free (struct MeshReliableMessage *copy);
285
286 /**
287  * We have received a message out of order, or the client is not ready.
288  * Buffer it until we receive an ACK from the client or the missing
289  * message from the channel.
290  *
291  * @param msg Message to buffer (MUST be of type MESH_DATA).
292  * @param rel Reliability data to the corresponding direction.
293  */
294 static void
295 add_buffered_data (const struct GNUNET_MESH_Data *msg,
296                    struct MeshChannelReliability *rel)
297 {
298   struct MeshReliableMessage *copy;
299   struct MeshReliableMessage *prev;
300   uint32_t mid;
301   uint16_t size;
302
303   size = ntohs (msg->header.size);
304   mid = ntohl (msg->mid);
305
306   LOG (GNUNET_ERROR_TYPE_DEBUG, "add_buffered_data %u\n", mid);
307
308   copy = GNUNET_malloc (sizeof (*copy) + size);
309   copy->mid = mid;
310   copy->rel = rel;
311   memcpy (&copy[1], msg, size);
312
313   rel->n_recv++;
314
315   // FIXME do something better than O(n), although n < 64...
316   // FIXME start from the end (most messages are the latest ones)
317   for (prev = rel->head_recv; NULL != prev; prev = prev->next)
318   {
319     LOG (GNUNET_ERROR_TYPE_DEBUG, " prev %u\n", prev->mid);
320     if (GMC_is_pid_bigger (prev->mid, mid))
321     {
322       LOG (GNUNET_ERROR_TYPE_DEBUG, " bingo!\n");
323       GNUNET_CONTAINER_DLL_insert_before (rel->head_recv, rel->tail_recv,
324                                           prev, copy);
325       return;
326     }
327   }
328     LOG (GNUNET_ERROR_TYPE_DEBUG, " insert at tail!\n");
329     GNUNET_CONTAINER_DLL_insert_tail (rel->head_recv, rel->tail_recv, copy);
330     LOG (GNUNET_ERROR_TYPE_DEBUG, "add_buffered_data END\n");
331 }
332
333 /**
334  * Add a destination client to a channel, initializing all data structures
335  * in the channel and the client.
336  *
337  * @param ch Channel to which add the destination.
338  * @param c Client which to add to the channel.
339  */
340 static void
341 add_destination (struct MeshChannel *ch, struct MeshClient *c)
342 {
343   if (NULL != ch->dest)
344   {
345     GNUNET_break (0);
346     return;
347   }
348
349   /* Assign local id as destination */
350   ch->lid_dest = GML_get_next_chid (c);
351
352   /* Store in client's hashmap */
353   GML_channel_add (c, ch->lid_dest, ch);
354
355   GNUNET_break (NULL == ch->dest_rel);
356   ch->dest_rel = GNUNET_new (struct MeshChannelReliability);
357   ch->dest_rel->ch = ch;
358   ch->dest_rel->expected_delay = MESH_RETRANSMIT_TIME;
359
360   ch->dest = c;
361 }
362
363
364 /**
365  * Send data to a client.
366  *
367  * If the client is ready, send directly, otherwise buffer while listening
368  * for a local ACK.
369  *
370  * @param ch Channel
371  * @param msg Message.
372  * @param fwd Is this a fwd (root->dest) message?
373  */
374 static void
375 send_client_data (struct MeshChannel *ch,
376                   const struct GNUNET_MESH_Data *msg,
377                   int fwd)
378 {
379   if (fwd)
380   {
381     if (ch->dest_rel->client_ready)
382       GML_send_data (ch->dest, msg, ch->lid_dest);
383     else
384       add_buffered_data (msg, ch->dest_rel);
385   }
386   else
387   {
388     if (ch->root_rel->client_ready)
389       GML_send_data (ch->root, msg, ch->lid_root);
390     else
391       add_buffered_data (msg, ch->root_rel);
392   }
393 }
394
395
396 /**
397  * Send a buffered message to the client, for in order delivery or
398  * as result of client ACK.
399  *
400  * @param ch Channel on which to empty the message buffer.
401  * @param c Client to send to.
402  * @param fwd Is this to send FWD data?.
403  */
404 static void
405 send_client_buffered_data (struct MeshChannel *ch,
406                            struct MeshClient *c,
407                            int fwd)
408 {
409   struct MeshReliableMessage *copy;
410   struct MeshChannelReliability *rel;
411
412   LOG (GNUNET_ERROR_TYPE_DEBUG, "send_buffered_data\n");
413   rel = fwd ? ch->dest_rel : ch->root_rel;
414   if (GNUNET_NO == rel->client_ready)
415   {
416     LOG (GNUNET_ERROR_TYPE_DEBUG, "client not ready\n");
417     return;
418   }
419
420   copy = rel->head_recv;
421   /* We never buffer channel management messages */
422   if (NULL != copy)
423   {
424     if (copy->mid == rel->mid_recv || GNUNET_NO == ch->reliable)
425     {
426       struct GNUNET_MESH_Data *msg = (struct GNUNET_MESH_Data *) &copy[1];
427
428       LOG (GNUNET_ERROR_TYPE_DEBUG,
429                   " have %u! now expecting %u\n",
430                   copy->mid, rel->mid_recv + 1);
431       send_client_data (ch, msg, fwd);
432       rel->n_recv--;
433       rel->mid_recv++;
434       GNUNET_CONTAINER_DLL_remove (rel->head_recv, rel->tail_recv, copy);
435       GNUNET_free (copy);
436     }
437     else
438     {
439       LOG (GNUNET_ERROR_TYPE_DEBUG,
440                   " reliable && don't have %u, next is %u\n",
441                   rel->mid_recv,
442                   copy->mid);
443       return;
444     }
445   }
446   LOG (GNUNET_ERROR_TYPE_DEBUG, "send_buffered_data END\n");
447 }
448
449
450 /**
451  * Allow a client to send more data.
452  *
453  * In case the client was already allowed to send data, do nothing.
454  *
455  * @param ch Channel.
456  * @param fwd Is this a FWD ACK? (FWD ACKs are sent to root)
457  */
458 static void
459 send_client_ack (struct MeshChannel *ch, int fwd)
460 {
461   struct MeshChannelReliability *rel = fwd ? ch->root_rel : ch->dest_rel;
462
463   LOG (GNUNET_ERROR_TYPE_DEBUG,
464        "  sending %s ack to client on channel %s\n",
465        fwd ? "FWD" : "BCK", GMCH_2s (ch));
466
467   if (NULL == rel)
468   {
469     GNUNET_break (0);
470     return;
471   }
472
473   if (GNUNET_YES == rel->client_allowed)
474   {
475     LOG (GNUNET_ERROR_TYPE_DEBUG, "  already allowed\n");
476     return;
477   }
478   rel->client_allowed = GNUNET_YES;
479
480   GML_send_ack (fwd ? ch->root : ch->dest, fwd ? ch->lid_root : ch->lid_dest);
481 }
482
483
484 /**
485  * Destroy all reliable messages queued for a channel,
486  * during a channel destruction.
487  * Frees the reliability structure itself.
488  *
489  * @param rel Reliability data for a channel.
490  */
491 static void
492 channel_rel_free_all (struct MeshChannelReliability *rel)
493 {
494   struct MeshReliableMessage *copy;
495   struct MeshReliableMessage *next;
496
497   if (NULL == rel)
498     return;
499
500   for (copy = rel->head_recv; NULL != copy; copy = next)
501   {
502     next = copy->next;
503     GNUNET_CONTAINER_DLL_remove (rel->head_recv, rel->tail_recv, copy);
504     GNUNET_free (copy);
505   }
506   for (copy = rel->head_sent; NULL != copy; copy = next)
507   {
508     next = copy->next;
509     GNUNET_CONTAINER_DLL_remove (rel->head_sent, rel->tail_sent, copy);
510     GNUNET_free (copy);
511   }
512   if (GNUNET_SCHEDULER_NO_TASK != rel->retry_task)
513     GNUNET_SCHEDULER_cancel (rel->retry_task);
514   GNUNET_free (rel);
515 }
516
517
518 /**
519  * Mark future messages as ACK'd.
520  *
521  * @param rel Reliability data.
522  * @param msg DataACK message with a bitfield of future ACK'd messages.
523  */
524 static void
525 channel_rel_free_sent (struct MeshChannelReliability *rel,
526                        const struct GNUNET_MESH_DataACK *msg)
527 {
528   struct MeshReliableMessage *copy;
529   struct MeshReliableMessage *next;
530   uint64_t bitfield;
531   uint64_t mask;
532   uint32_t mid;
533   uint32_t target;
534   unsigned int i;
535
536   bitfield = msg->futures;
537   mid = ntohl (msg->mid);
538   LOG (GNUNET_ERROR_TYPE_DEBUG,
539               "free_sent_reliable %u %llX\n",
540               mid, bitfield);
541   LOG (GNUNET_ERROR_TYPE_DEBUG,
542               " rel %p, head %p\n",
543               rel, rel->head_sent);
544   for (i = 0, copy = rel->head_sent;
545        i < 64 && NULL != copy && 0 != bitfield;
546        i++)
547   {
548     LOG (GNUNET_ERROR_TYPE_DEBUG,
549                 " trying bit %u (mid %u)\n",
550                 i, mid + i + 1);
551     mask = 0x1LL << i;
552     if (0 == (bitfield & mask))
553      continue;
554
555     LOG (GNUNET_ERROR_TYPE_DEBUG, " set!\n");
556     /* Bit was set, clear the bit from the bitfield */
557     bitfield &= ~mask;
558
559     /* The i-th bit was set. Do we have that copy? */
560     /* Skip copies with mid < target */
561     target = mid + i + 1;
562     LOG (GNUNET_ERROR_TYPE_DEBUG, " target %u\n", target);
563     while (NULL != copy && GMC_is_pid_bigger (target, copy->mid))
564      copy = copy->next;
565
566     /* Did we run out of copies? (previously freed, it's ok) */
567     if (NULL == copy)
568     {
569      LOG (GNUNET_ERROR_TYPE_DEBUG, "run out of copies...\n");
570      return;
571     }
572
573     /* Did we overshoot the target? (previously freed, it's ok) */
574     if (GMC_is_pid_bigger (copy->mid, target))
575     {
576      LOG (GNUNET_ERROR_TYPE_DEBUG, " next copy %u\n", copy->mid);
577      continue;
578     }
579
580     /* Now copy->mid == target, free it */
581     next = copy->next;
582     rel_message_free (copy);
583     copy = next;
584   }
585   LOG (GNUNET_ERROR_TYPE_DEBUG, "free_sent_reliable END\n");
586 }
587
588
589 /**
590  * We haven't received an ACK after a certain time: restransmit the message.
591  *
592  * @param cls Closure (MeshReliableMessage with the message to restransmit)
593  * @param tc TaskContext.
594  */
595 static void
596 channel_retransmit_message (void *cls,
597                             const struct GNUNET_SCHEDULER_TaskContext *tc)
598 {
599   struct MeshChannelReliability *rel = cls;
600   struct MeshReliableMessage *copy;
601   struct MeshChannel *ch;
602   struct GNUNET_MESH_Data *payload;
603   int fwd;
604
605   rel->retry_task = GNUNET_SCHEDULER_NO_TASK;
606   if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
607     return;
608
609   ch = rel->ch;
610   copy = rel->head_sent;
611   if (NULL == copy)
612   {
613     GNUNET_break (0);
614     return;
615   }
616
617   /* Search the message to be retransmitted in the outgoing queue.
618    * Check only the queue for the connection that is going to be used,
619    * if the message is stuck in some other connection's queue we shouldn't
620    * act upon it:
621    * - cancelling it and sending the new one doesn't guarantee it's delivery,
622    *   the old connection could be temporary stalled or the queue happened to
623    *   be long at time of insertion.
624    * - not sending the new one could cause terrible delays the old connection
625    *   is stalled.
626    */
627 //   FIXME access to queue elements is limited
628   payload = (struct GNUNET_MESH_Data *) &copy[1];
629   fwd = (rel == ch->root_rel);
630 //   c = GMT_get_connection (ch->t, fwd);
631 //   hop = connection_get_hop (c, fwd);
632 //   for (q = hop->queue_head; NULL != q; q = q->next)
633 //   {
634 //     if (ntohs (payload->header.type) == q->type && ch == q->ch)
635 //     {
636 //       struct GNUNET_MESH_Data *queued_data = q->cls;
637 // 
638 //       if (queued_data->mid == payload->mid)
639 //         break;
640 //     }
641 //   }
642
643   /* Message not found in the queue that we are going to use. */
644 //   if (NULL == q)
645 //   {
646     LOG (GNUNET_ERROR_TYPE_DEBUG, "!!! RETRANSMIT %u\n", copy->mid);
647
648     GMCH_send_prebuilt_message (&payload->header, ch, fwd);
649     GNUNET_STATISTICS_update (stats, "# data retransmitted", 1, GNUNET_NO);
650 //   }
651 //   else
652 //   {
653 //     LOG (GNUNET_ERROR_TYPE_DEBUG, "!!! ALREADY IN QUEUE %u\n", copy->mid);
654 //   }
655
656   rel->retry_timer = GNUNET_TIME_STD_BACKOFF (rel->retry_timer);
657   rel->retry_task = GNUNET_SCHEDULER_add_delayed (rel->retry_timer,
658                                                   &channel_retransmit_message,
659                                                   cls);
660 }
661
662
663 /**
664  * Destroy a reliable message after it has been acknowledged, either by
665  * direct mid ACK or bitfield. Updates the appropriate data structures and
666  * timers and frees all memory.
667  *
668  * @param copy Message that is no longer needed: remote peer got it.
669  */
670 static void
671 rel_message_free (struct MeshReliableMessage *copy)
672 {
673   struct MeshChannelReliability *rel;
674   struct GNUNET_TIME_Relative time;
675
676   rel = copy->rel;
677   time = GNUNET_TIME_absolute_get_duration (copy->timestamp);
678   rel->expected_delay.rel_value_us *= 7;
679   rel->expected_delay.rel_value_us += time.rel_value_us;
680   rel->expected_delay.rel_value_us /= 8;
681   rel->n_sent--;
682   LOG (GNUNET_ERROR_TYPE_DEBUG, "!!! Freeing %u\n", copy->mid);
683   LOG (GNUNET_ERROR_TYPE_DEBUG, "    n_sent %u\n", rel->n_sent);
684   LOG (GNUNET_ERROR_TYPE_DEBUG, "!!!  took %s\n",
685               GNUNET_STRINGS_relative_time_to_string (time, GNUNET_NO));
686   LOG (GNUNET_ERROR_TYPE_DEBUG, "!!!  new expected delay %s\n",
687               GNUNET_STRINGS_relative_time_to_string (rel->expected_delay,
688                                                       GNUNET_NO));
689   rel->retry_timer = rel->expected_delay;
690   GNUNET_CONTAINER_DLL_remove (rel->head_sent, rel->tail_sent, copy);
691   GNUNET_free (copy);
692 }
693
694
695 /**
696  * Confirm we got a channel create.
697  *
698  * @param ch The channel to confirm.
699  * @param fwd Should we send a FWD ACK? (going dest->root)
700  */
701 static void
702 channel_send_ack (struct MeshChannel *ch, int fwd)
703 {
704   struct GNUNET_MESH_ChannelManage msg;
705
706   msg.header.size = htons (sizeof (msg));
707   msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CHANNEL_ACK);
708   LOG (GNUNET_ERROR_TYPE_DEBUG,
709               "  sending channel %s ack for channel %s\n",
710               fwd ? "FWD" : "BCK", GMCH_2s (ch));
711
712   msg.chid = htonl (ch->gid);
713   GMCH_send_prebuilt_message (&msg.header, ch, !fwd);
714 }
715
716
717 /**
718  * Channel was ACK'd by remote peer, mark as ready and cancel retransmission.
719  *
720  * @param ch Channel to mark as ready.
721  * @param fwd Was the ACK message a FWD ACK? (dest->root, SYNACK)
722  */
723 static void
724 channel_confirm (struct MeshChannel *ch, int fwd)
725 {
726   struct MeshChannelReliability *rel;
727   struct MeshReliableMessage *copy;
728   struct MeshReliableMessage *next;
729
730   LOG (GNUNET_ERROR_TYPE_DEBUG,
731               "  channel confirm %s %s:%X\n",
732               fwd ? "FWD" : "BCK", GMT_2s (ch->t), ch->gid);
733   ch->state = MESH_CHANNEL_READY;
734
735   rel = fwd ? ch->root_rel : ch->dest_rel;
736   rel->client_ready = GNUNET_YES;
737   for (copy = rel->head_sent; NULL != copy; copy = next)
738   {
739     struct GNUNET_MessageHeader *msg;
740
741     next = copy->next;
742     msg = (struct GNUNET_MessageHeader *) &copy[1];
743     if (ntohs (msg->type) == GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE)
744     {
745       rel_message_free (copy);
746       /* TODO return? */
747     }
748   }
749   send_client_ack (ch, fwd);
750
751   /* In case of a FWD ACK (SYNACK) send a BCK ACK (ACK). */
752   if (fwd)
753     channel_send_ack (ch, !fwd);
754 }
755
756
757 /**
758  * Save a copy to retransmit in case it gets lost.
759  *
760  * Initializes all needed callbacks and timers.
761  *
762  * @param ch Channel this message goes on.
763  * @param msg Message to copy.
764  * @param fwd Is this fwd traffic?
765  */
766 static void
767 channel_save_copy (struct MeshChannel *ch,
768                    const struct GNUNET_MessageHeader *msg,
769                    int fwd)
770 {
771   struct MeshChannelReliability *rel;
772   struct MeshReliableMessage *copy;
773   uint32_t mid;
774   uint16_t type;
775   uint16_t size;
776
777   rel = fwd ? ch->root_rel : ch->dest_rel;
778   mid = rel->mid_send;
779   type = ntohs (msg->type);
780   size = ntohs (msg->size);
781
782   LOG (GNUNET_ERROR_TYPE_DEBUG, "!!! SAVE %u\n", mid);
783   copy = GNUNET_malloc (sizeof (struct MeshReliableMessage) + size);
784   copy->mid = mid;
785   copy->timestamp = GNUNET_TIME_absolute_get ();
786   copy->rel = rel;
787   copy->type = type;
788   memcpy (&copy[1], msg, size);
789   rel->n_sent++;
790   LOG (GNUNET_ERROR_TYPE_DEBUG, " n_sent %u\n", rel->n_sent);
791   GNUNET_CONTAINER_DLL_insert_tail (rel->head_sent, rel->tail_sent, copy);
792   if (GNUNET_SCHEDULER_NO_TASK == rel->retry_task)
793   {
794     rel->retry_timer =
795         GNUNET_TIME_relative_multiply (rel->expected_delay,
796                                         MESH_RETRANSMIT_MARGIN);
797     rel->retry_task =
798         GNUNET_SCHEDULER_add_delayed (rel->retry_timer,
799                                       &channel_retransmit_message,
800                                       rel);
801   }
802 }
803
804
805 /**
806  * Destroy a channel and free all resources.
807  *
808  * @param ch Channel to destroy.
809  */
810 static void
811 channel_destroy (struct MeshChannel *ch)
812 {
813   struct MeshClient *c;
814
815   if (NULL == ch)
816     return;
817
818   LOG (GNUNET_ERROR_TYPE_DEBUG, "destroying channel %s:%u\n",
819               GMT_2s (ch->t), ch->gid);
820   GMCH_debug (ch);
821
822   c = ch->root;
823   if (NULL != c)
824   {
825     GML_channel_remove (c, ch->lid_root, ch);
826   }
827
828   c = ch->dest;
829   if (NULL != c)
830   {
831     GML_channel_remove (c, ch->lid_dest, ch);
832   }
833
834   channel_rel_free_all (ch->root_rel);
835   channel_rel_free_all (ch->dest_rel);
836
837   GMT_remove_channel (ch->t, ch);
838   GNUNET_STATISTICS_update (stats, "# channels", -1, GNUNET_NO);
839
840   GNUNET_free (ch);
841 }
842
843
844 /**
845  * Create a new channel.
846  *
847  * @param t Tunnel this channel is in.
848  * @param owner Client that owns the channel, NULL for foreign channels.
849  * @param lid_root Local ID for root client.
850  *
851  * @return A new initialized channel. NULL on error.
852  */
853 static struct MeshChannel *
854 channel_new (struct MeshTunnel3 *t,
855              struct MeshClient *owner,
856              MESH_ChannelNumber lid_root)
857 {
858   struct MeshChannel *ch;
859
860   ch = GNUNET_new (struct MeshChannel);
861   ch->root = owner;
862   ch->lid_root = lid_root;
863   ch->t = t;
864
865   GNUNET_STATISTICS_update (stats, "# channels", 1, GNUNET_NO);
866
867   if (NULL != owner)
868   {
869     ch->gid = GMT_get_next_chid (t);
870     GML_channel_add (owner, lid_root, ch);
871   }
872   GMT_add_channel (t, ch);
873
874   return ch;
875 }
876
877
878 /**
879  * Set options in a channel, extracted from a bit flag field
880  *
881  * @param ch Channel to set options to.
882  * @param options Bit array in host byte order.
883  */
884 static void
885 channel_set_options (struct MeshChannel *ch, uint32_t options)
886 {
887   ch->nobuffer = (options & GNUNET_MESH_OPTION_NOBUFFER) != 0 ?
888                  GNUNET_YES : GNUNET_NO;
889   ch->reliable = (options & GNUNET_MESH_OPTION_RELIABLE) != 0 ?
890                  GNUNET_YES : GNUNET_NO;
891 }
892
893 static int
894 is_loopback (const struct MeshChannel *ch)
895 {
896   if (NULL != ch->t)
897     return GMT_is_loopback (ch->t);
898
899   return (NULL != ch->root && NULL != ch->dest);
900 }
901
902
903 /**
904  * Handle a loopback message: call the appropriate handler for the message type.
905  *
906  * @param ch Channel this message is on.
907  * @param msgh Message header.
908  * @param fwd Is this FWD traffic?
909  */
910 void
911 handle_loopback (struct MeshChannel *ch,
912                  const struct GNUNET_MessageHeader *msgh,
913                  int fwd)
914 {
915   uint16_t type;
916
917   type = ntohs (msgh->type);
918   LOG (GNUNET_ERROR_TYPE_DEBUG,
919        "Loopback %s %s message!\n",
920        fwd ? "FWD" : "BCK", GNUNET_MESH_DEBUG_M2S (type));
921
922   switch (type)
923   {
924     case GNUNET_MESSAGE_TYPE_MESH_DATA:
925       /* Don't send hop ACK, wait for client to ACK */
926       GMCH_handle_data (ch, (struct GNUNET_MESH_Data *) msgh, fwd);
927       break;
928
929     case GNUNET_MESSAGE_TYPE_MESH_DATA_ACK:
930       GMCH_handle_data_ack (ch, (struct GNUNET_MESH_DataACK *) msgh, fwd);
931       break;
932
933     case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE:
934       GMCH_handle_create (ch->t,
935                           (struct GNUNET_MESH_ChannelCreate *) msgh);
936       break;
937
938     case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_ACK:
939       GMCH_handle_ack (ch,
940                        (struct GNUNET_MESH_ChannelManage *) msgh,
941                        fwd);
942       break;
943
944     case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_DESTROY:
945       GMCH_handle_destroy (ch,
946                            (struct GNUNET_MESH_ChannelManage *) msgh,
947                            fwd);
948       break;
949
950     default:
951       GNUNET_break_op (0);
952       LOG (GNUNET_ERROR_TYPE_DEBUG,
953            "end-to-end message not known (%u)\n",
954            ntohs (msgh->type));
955   }
956 }
957
958
959
960 /******************************************************************************/
961 /********************************    API    ***********************************/
962 /******************************************************************************/
963
964
965 /**
966  * Get channel ID.
967  *
968  * @param ch Channel.
969  *
970  * @return ID
971  */
972 MESH_ChannelNumber
973 GMCH_get_id (const struct MeshChannel *ch)
974 {
975   return ch->gid;
976 }
977
978
979 /**
980  * Get the channel tunnel.
981  *
982  * @param ch Channel to get the tunnel from.
983  *
984  * @return tunnel of the channel.
985  */
986 struct MeshTunnel3 *
987 GMCH_get_tunnel (const struct MeshChannel *ch)
988 {
989   return ch->t;
990 }
991
992
993 /**
994  * Get free buffer space towards the client on a specific channel.
995  *
996  * @param ch Channel.
997  * @param fwd Is query about FWD traffic?
998  *
999  * @return Free buffer space [0 - 64]
1000  */
1001 unsigned int
1002 GMCH_get_buffer (struct MeshChannel *ch, int fwd)
1003 {
1004   struct MeshChannelReliability *rel;
1005
1006   rel = fwd ? ch->dest_rel : ch->root_rel;
1007
1008   /* If rel is NULL it means that the end is not yet created,
1009    * most probably is a loopback channel at the point of sending
1010    * the ChannelCreate to itself.
1011    */
1012   if (NULL == rel)
1013     return 64;
1014
1015   return (64 - rel->n_recv);
1016 }
1017
1018
1019 /**
1020  * Get flow control status of end point: is client allow to send?
1021  *
1022  * @param ch Channel.
1023  * @param fwd Is query about FWD traffic? (Request root status).
1024  *
1025  * @return #GNUNET_YES if client is allowed to send us data.
1026  */
1027 int
1028 GMCH_get_allowed (struct MeshChannel *ch, int fwd)
1029 {
1030   struct MeshChannelReliability *rel;
1031
1032   rel = fwd ? ch->root_rel : ch->dest_rel;
1033
1034   return rel->client_allowed;
1035 }
1036
1037
1038 /**
1039  * Is the root client for this channel on this peer?
1040  *
1041  * @param ch Channel.
1042  * @param fwd Is this for fwd traffic?
1043  *
1044  * @return #GNUNET_YES in case it is.
1045  */
1046 int
1047 GMCH_is_origin (struct MeshChannel *ch, int fwd)
1048 {
1049   struct MeshClient *c;
1050
1051   c = fwd ? ch->root : ch->dest;
1052   return NULL != c;
1053 }
1054
1055
1056 /**
1057  * Is the destination client for this channel on this peer?
1058  *
1059  * @param ch Channel.
1060  * @param fwd Is this for fwd traffic?
1061  *
1062  * @return #GNUNET_YES in case it is.
1063  */
1064 int
1065 GMCH_is_terminal (struct MeshChannel *ch, int fwd)
1066 {
1067   struct MeshClient *c;
1068
1069   c = fwd ? ch->dest : ch->root;
1070   return NULL != c;
1071 }
1072
1073
1074 /**
1075  * Notify the destination client that a new incoming channel was created.
1076  *
1077  * @param ch Channel that was created.
1078  */
1079 void
1080 GMCH_send_create (struct MeshChannel *ch)
1081 {
1082   uint32_t opt;
1083
1084   if (NULL == ch->dest)
1085     return;
1086
1087   opt = 0;
1088   opt |= GNUNET_YES == ch->reliable ? GNUNET_MESH_OPTION_RELIABLE : 0;
1089   opt |= GNUNET_YES == ch->nobuffer ? GNUNET_MESH_OPTION_NOBUFFER : 0;
1090   GML_send_channel_create (ch->dest, ch->lid_dest, ch->port, opt,
1091                            GMT_get_destination (ch->t));
1092
1093 }
1094
1095 /**
1096  * Notify a client that the channel is no longer valid.
1097  *
1098  * @param ch Channel that is destroyed.
1099  */
1100 void
1101 GMCH_send_destroy (struct MeshChannel *ch)
1102 {
1103   struct GNUNET_MESH_ChannelManage msg;
1104
1105   msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CHANNEL_DESTROY);
1106   msg.header.size = htons (sizeof (msg));
1107   msg.chid = htonl (ch->gid);
1108
1109   /* If root is not NULL, notify.
1110    * If it's NULL, check lid_root. When a local destroy comes in, root 
1111    * is set to NULL but lid_root is left untouched. In this case, do nothing,
1112    * the client is the one who reuqested the channel to be destroyed.
1113    */
1114   if (NULL != ch->root)
1115     GML_send_channel_destroy (ch->root, ch->lid_root);
1116   else if (0 == ch->lid_root)
1117     GMCH_send_prebuilt_message (&msg.header, ch, GNUNET_NO);
1118
1119   if (NULL != ch->dest)
1120     GML_send_channel_destroy (ch->dest, ch->lid_dest);
1121   else if (0 == ch->lid_dest)
1122     GMCH_send_prebuilt_message (&msg.header, ch, GNUNET_YES);
1123 }
1124
1125
1126 /**
1127  * Send data on a channel.
1128  *
1129  * If the destination is local, send it to client, otherwise encrypt and
1130  * send to next hop.
1131  *
1132  * @param ch Channel
1133  * @param msg Message.
1134  * @param fwd Is this a fwd (root->dest) message?
1135  */
1136 void
1137 GMCH_send_data (struct MeshChannel *ch,
1138                 const struct GNUNET_MESH_Data *msg,
1139                 int fwd)
1140 {
1141   if (GMCH_is_terminal (ch, fwd))
1142   {
1143     GML_send_data (fwd ? ch->dest : ch->root,
1144                    msg,
1145                    fwd ? ch->lid_dest : ch->lid_root);
1146   }
1147   else
1148   {
1149     GMT_send_prebuilt_message (&msg->header, ch->t, ch, fwd);
1150   }
1151 }
1152
1153
1154 /**
1155  * Send an end-to-end ACK message for the most recent in-sequence payload.
1156  *
1157  * If channel is not reliable, do nothing.
1158  *
1159  * @param ch Channel this is about.
1160  * @param fwd Is for FWD traffic? (ACK dest->owner)
1161  */
1162 void
1163 GMCH_send_data_ack (struct MeshChannel *ch, int fwd)
1164 {
1165   struct GNUNET_MESH_DataACK msg;
1166   struct MeshChannelReliability *rel;
1167   struct MeshReliableMessage *copy;
1168   unsigned int delta;
1169   uint64_t mask;
1170   uint16_t type;
1171
1172   if (GNUNET_NO == ch->reliable)
1173   {
1174     return;
1175   }
1176   rel = fwd ? ch->dest_rel : ch->root_rel;
1177   LOG (GNUNET_ERROR_TYPE_DEBUG,
1178               "send_data_ack for %u\n",
1179               rel->mid_recv - 1);
1180
1181   type = GNUNET_MESSAGE_TYPE_MESH_DATA_ACK;
1182   msg.header.type = htons (type);
1183   msg.header.size = htons (sizeof (msg));
1184   msg.chid = htonl (ch->gid);
1185   msg.mid = htonl (rel->mid_recv - 1);
1186   msg.futures = 0;
1187   for (copy = rel->head_recv; NULL != copy; copy = copy->next)
1188   {
1189     if (copy->type != type)
1190       continue;
1191     delta = copy->mid - rel->mid_recv;
1192     if (63 < delta)
1193       break;
1194     mask = 0x1LL << delta;
1195     msg.futures |= mask;
1196     LOG (GNUNET_ERROR_TYPE_DEBUG,
1197                 " setting bit for %u (delta %u) (%llX) -> %llX\n",
1198                 copy->mid, delta, mask, msg.futures);
1199   }
1200   LOG (GNUNET_ERROR_TYPE_DEBUG, " final futures %llX\n", msg.futures);
1201
1202   GMCH_send_prebuilt_message (&msg.header, ch, fwd);
1203   LOG (GNUNET_ERROR_TYPE_DEBUG, "send_data_ack END\n");
1204 }
1205
1206
1207 /**
1208  * Allow a client to send us more data, in case it was choked.
1209  *
1210  * @param ch Channel.
1211  * @param fwd Is this about FWD traffic? (Root client).
1212  */
1213 void
1214 GMCH_allow_client (struct MeshChannel *ch, int fwd)
1215 {
1216   if (MESH_CHANNEL_READY != ch->state)
1217     return;
1218
1219   send_client_ack (ch, fwd);
1220 }
1221
1222
1223 /**
1224  * Log channel info.
1225  *
1226  * @param ch Channel.
1227  */
1228 void
1229 GMCH_debug (struct MeshChannel *ch)
1230 {
1231   if (NULL == ch)
1232   {
1233     LOG (GNUNET_ERROR_TYPE_DEBUG, "*** DEBUG NULL CHANNEL ***\n");
1234     return;
1235   }
1236   LOG (GNUNET_ERROR_TYPE_DEBUG, "Channel %s:%X (%p)\n",
1237               GMT_2s (ch->t), ch->gid, ch);
1238   LOG (GNUNET_ERROR_TYPE_DEBUG, "  root %p/%p\n",
1239               ch->root, ch->root_rel);
1240   if (NULL != ch->root)
1241   {
1242     LOG (GNUNET_ERROR_TYPE_DEBUG, "  cli %s\n", GML_2s (ch->root));
1243     LOG (GNUNET_ERROR_TYPE_DEBUG, "  ready %s\n",
1244                 ch->root_rel->client_ready ? "YES" : "NO");
1245     LOG (GNUNET_ERROR_TYPE_DEBUG, "  id %X\n", ch->lid_root);
1246   }
1247   LOG (GNUNET_ERROR_TYPE_DEBUG, "  dest %p/%p\n",
1248               ch->dest, ch->dest_rel);
1249   if (NULL != ch->dest)
1250   {
1251     LOG (GNUNET_ERROR_TYPE_DEBUG, "  cli %s\n", GML_2s (ch->dest));
1252     LOG (GNUNET_ERROR_TYPE_DEBUG, "  ready %s\n",
1253                 ch->dest_rel->client_ready ? "YES" : "NO");
1254     LOG (GNUNET_ERROR_TYPE_DEBUG, "  id %X\n", ch->lid_dest);
1255   }
1256 }
1257
1258
1259 /**
1260  * Handle an ACK given by a client.
1261  *
1262  * Mark client as ready and send him any buffered data we could have for him.
1263  *
1264  * @param ch Channel.
1265  * @param fwd Is this a "FWD ACK"? (FWD ACKs are sent by dest and go BCK)
1266  */
1267 void
1268 GMCH_handle_local_ack (struct MeshChannel *ch, int fwd)
1269 {
1270   struct MeshChannelReliability *rel;
1271   struct MeshClient *c;
1272
1273   rel = fwd ? ch->dest_rel : ch->root_rel;
1274   c   = fwd ? ch->dest     : ch->root;
1275
1276   rel->client_ready = GNUNET_YES;
1277   send_client_buffered_data (ch, c, fwd);
1278   if (is_loopback (ch))
1279   {
1280     unsigned int buffer;
1281
1282     buffer = GMCH_get_buffer (ch, fwd);
1283     if (0 < buffer)
1284       GMCH_allow_client (ch, fwd);
1285
1286     return;
1287   }
1288   GMT_send_connection_acks (ch->t);
1289 }
1290
1291
1292 /**
1293  * Handle data given by a client.
1294  *
1295  * Check whether the client is allowed to send in this tunnel, save if channel
1296  * is reliable and send an ACK to the client if there is still buffer space
1297  * in the tunnel.
1298  *
1299  * @param ch Channel.
1300  * @param c Client which sent the data.
1301  * @param message Message.
1302  * @param fwd Is this a FWD data?
1303  *
1304  * @return GNUNET_OK if everything goes well, GNUNET_SYSERR in case of en error.
1305  */
1306 int
1307 GMCH_handle_local_data (struct MeshChannel *ch,
1308                         struct MeshClient *c,
1309                         struct GNUNET_MessageHeader *message,
1310                         int fwd)
1311 {
1312   struct MeshChannelReliability *rel;
1313   struct GNUNET_MESH_Data *payload;
1314   size_t size = ntohs (message->size);
1315   uint16_t p2p_size = sizeof(struct GNUNET_MESH_Data) + size;
1316   unsigned char cbuf[p2p_size];
1317
1318   /* Is the client in the channel? */
1319   if ( !( (fwd &&
1320            ch->root == c)
1321          ||
1322           (!fwd &&
1323            ch->dest == c) ) )
1324   {
1325     GNUNET_break (0);
1326     return GNUNET_SYSERR;
1327   }
1328
1329   rel = fwd ? ch->root_rel : ch->dest_rel;
1330
1331   rel->client_allowed = GNUNET_NO;
1332
1333   /* Ok, everything is correct, send the message. */
1334   payload = (struct GNUNET_MESH_Data *) cbuf;
1335   payload->mid = htonl (rel->mid_send);
1336   rel->mid_send++;
1337   memcpy (&payload[1], message, size);
1338   payload->header.size = htons (p2p_size);
1339   payload->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_DATA);
1340   payload->chid = htonl (ch->gid);
1341   LOG (GNUNET_ERROR_TYPE_DEBUG, "  sending on channel...\n");
1342   if (GNUNET_YES == ch->reliable)
1343     channel_save_copy (ch, &payload->header, fwd);
1344   GMCH_send_prebuilt_message (&payload->header, ch, fwd);
1345
1346   if (is_loopback (ch))
1347   {
1348     if (GMCH_get_buffer (ch, fwd) > 0);
1349       send_client_ack (ch, fwd);
1350
1351     return GNUNET_OK;
1352   }
1353
1354   if (GMT_get_connections_buffer (ch->t) > 0)
1355   {
1356     send_client_ack (ch, fwd);
1357   }
1358
1359   return GNUNET_OK;
1360 }
1361
1362
1363 /**
1364  * Handle a channel destroy requested by a client.
1365  *
1366  * Destroy the channel and the tunnel in case this was the last channel.
1367  *
1368  * @param ch Channel.
1369  * @param c Client that requested the destruction (to avoid notifying him).
1370  */
1371 void
1372 GMCH_handle_local_destroy (struct MeshChannel *ch,
1373                            struct MeshClient *c)
1374 {
1375   struct MeshTunnel3 *t;
1376
1377   /* Cleanup after the tunnel */
1378   if (c == ch->dest)
1379   {
1380     LOG (GNUNET_ERROR_TYPE_DEBUG, " Client %s is destination.\n", GML_2s (c));
1381     GML_client_delete_channel (c, ch, ch->lid_dest);
1382     ch->dest = NULL;
1383   }
1384   if (c == ch->root)
1385   {
1386     LOG (GNUNET_ERROR_TYPE_DEBUG, " Client %s is owner.\n", GML_2s (c));
1387     GML_client_delete_channel (c, ch, ch->lid_root);
1388     ch->root = NULL;
1389   }
1390
1391   t = ch->t;
1392   GMCH_send_destroy (ch);
1393   channel_destroy (ch);
1394   GMT_destroy_if_empty (t);
1395 }
1396
1397
1398 /**
1399  * Handle a channel create requested by a client.
1400  *
1401  * Create the channel and the tunnel in case this was the first0 channel.
1402  *
1403  * @param c Client that requested the creation (will be the root).
1404  * @param msg Create Channel message.
1405  *
1406  * @return GNUNET_OK if everything went fine, GNUNET_SYSERR otherwise.
1407  */
1408 int
1409 GMCH_handle_local_create (struct MeshClient *c,
1410                           struct GNUNET_MESH_ChannelMessage *msg)
1411 {
1412   struct MeshChannel *ch;
1413   struct MeshTunnel3 *t;
1414   struct MeshPeer *peer;
1415   MESH_ChannelNumber chid;
1416
1417   LOG (GNUNET_ERROR_TYPE_DEBUG, "  towards %s:%u\n",
1418               GNUNET_i2s (&msg->peer), ntohl (msg->port));
1419   chid = ntohl (msg->channel_id);
1420
1421   /* Sanity check for duplicate channel IDs */
1422   if (NULL != GML_channel_get (c, chid))
1423   {
1424     GNUNET_break (0);
1425     return GNUNET_SYSERR;
1426   }
1427
1428   peer = GMP_get (&msg->peer);
1429   GMP_add_tunnel (peer);
1430   t = GMP_get_tunnel (peer);
1431
1432   if (GMP_get_short_id (peer) == myid)
1433   {
1434     GMT_change_state (t, MESH_TUNNEL3_READY);
1435   }
1436   else
1437   {
1438     GMP_connect (peer);
1439   }
1440
1441   /* Create channel */
1442   ch = channel_new (t, c, chid);
1443   if (NULL == ch)
1444   {
1445     GNUNET_break (0);
1446     return GNUNET_SYSERR;
1447   }
1448   ch->port = ntohl (msg->port);
1449   channel_set_options (ch, ntohl (msg->opt));
1450
1451   /* In unreliable channels, we'll use the DLL to buffer BCK data */
1452   ch->root_rel = GNUNET_new (struct MeshChannelReliability);
1453   ch->root_rel->ch = ch;
1454   ch->root_rel->expected_delay = MESH_RETRANSMIT_TIME;
1455
1456   LOG (GNUNET_ERROR_TYPE_DEBUG, "CREATED CHANNEL %s\n", GMCH_2s (ch));
1457
1458   /* Send create channel */
1459   {
1460     struct GNUNET_MESH_ChannelCreate msgcc;
1461
1462     msgcc.header.size = htons (sizeof (msgcc));
1463     msgcc.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE);
1464     msgcc.chid = htonl (ch->gid);
1465     msgcc.port = msg->port;
1466     msgcc.opt = msg->opt;
1467
1468     GMT_send_prebuilt_message (&msgcc.header, t, ch, GNUNET_YES);
1469   }
1470   return GNUNET_OK;
1471 }
1472
1473
1474 /**
1475  * Handler for mesh network payload traffic.
1476  *
1477  * @param ch Channel for the message.
1478  * @param msg Unencryted data message.
1479  * @param fwd Is this message fwd? This only is meaningful in loopback channels.
1480  *            #GNUNET_YES if message is FWD on the respective channel (loopback)
1481  *            #GNUNET_NO if message is BCK on the respective channel (loopback)
1482  *            #GNUNET_SYSERR if message on a one-ended channel (remote)
1483  */
1484 void
1485 GMCH_handle_data (struct MeshChannel *ch,
1486                   const struct GNUNET_MESH_Data *msg,
1487                   int fwd)
1488 {
1489   struct MeshChannelReliability *rel;
1490   struct MeshClient *c;
1491   uint32_t mid;
1492
1493   /* If this is a remote (non-loopback) channel, find 'fwd'. */
1494   if (GNUNET_SYSERR == fwd)
1495   {
1496     if (is_loopback (ch))
1497     {
1498       /* It is a loopback channel after all... */
1499       GNUNET_break (0);
1500       return;
1501     }
1502     fwd = (NULL != ch->dest) ? GNUNET_YES : GNUNET_NO;
1503   }
1504
1505   /*  Initialize FWD/BCK data */
1506   c   = fwd ? ch->dest     : ch->root;
1507   rel = fwd ? ch->dest_rel : ch->root_rel;
1508
1509   if (NULL == c)
1510   {
1511     GNUNET_break (0);
1512     return;
1513   }
1514
1515   GNUNET_STATISTICS_update (stats, "# data received", 1, GNUNET_NO);
1516
1517   mid = ntohl (msg->mid);
1518   LOG (GNUNET_ERROR_TYPE_DEBUG, " mid %u\n", mid);
1519
1520   if (GNUNET_NO == ch->reliable ||
1521       ( !GMC_is_pid_bigger (rel->mid_recv, mid) &&
1522         GMC_is_pid_bigger (rel->mid_recv + 64, mid) ) )
1523   {
1524     LOG (GNUNET_ERROR_TYPE_DEBUG, "!!! RECV %u\n", mid);
1525     if (GNUNET_YES == ch->reliable)
1526     {
1527       /* Is this the exact next expected messasge? */
1528       if (mid == rel->mid_recv)
1529       {
1530         LOG (GNUNET_ERROR_TYPE_DEBUG, "as expected\n");
1531         rel->mid_recv++;
1532         send_client_data (ch, msg, fwd);
1533       }
1534       else
1535       {
1536         LOG (GNUNET_ERROR_TYPE_DEBUG, "save for later\n");
1537         add_buffered_data (msg, rel);
1538       }
1539     }
1540     else
1541     {
1542       /* Tunnel is unreliable: send to clients directly */
1543       /* FIXME: accept Out Of Order traffic */
1544       rel->mid_recv = mid + 1;
1545       send_client_data (ch, msg, fwd);
1546     }
1547   }
1548   else
1549   {
1550     GNUNET_break_op (0);
1551     LOG (GNUNET_ERROR_TYPE_DEBUG,
1552                 " MID %u not expected (%u - %u), dropping!\n",
1553                 mid, rel->mid_recv, rel->mid_recv + 64);
1554   }
1555
1556   GMCH_send_data_ack (ch, fwd);
1557 }
1558
1559
1560 /**
1561  * Handler for mesh network traffic end-to-end ACKs.
1562  *
1563  * @param ch Channel on which we got this message.
1564  * @param msg Data message.
1565  * @param fwd Is this message fwd? This only is meaningful in loopback channels.
1566  *            #GNUNET_YES if message is FWD on the respective channel (loopback)
1567  *            #GNUNET_NO if message is BCK on the respective channel (loopback)
1568  *            #GNUNET_SYSERR if message on a one-ended channel (remote)
1569  */
1570 void
1571 GMCH_handle_data_ack (struct MeshChannel *ch,
1572                       const struct GNUNET_MESH_DataACK *msg,
1573                       int fwd)
1574 {
1575   struct MeshChannelReliability *rel;
1576   struct MeshReliableMessage *copy;
1577   struct MeshReliableMessage *next;
1578   uint32_t ack;
1579   int work;
1580
1581   /* If this is a remote (non-loopback) channel, find 'fwd'. */
1582   if (GNUNET_SYSERR == fwd)
1583   {
1584     if (is_loopback (ch))
1585     {
1586       /* It is a loopback channel after all... */
1587       GNUNET_break (0);
1588       return;
1589     }
1590     fwd = (NULL != ch->dest) ? GNUNET_YES : GNUNET_NO;
1591   }
1592
1593   ack = ntohl (msg->mid);
1594   LOG (GNUNET_ERROR_TYPE_DEBUG, "!!! %s ACK %u\n",
1595        (GNUNET_YES == fwd) ? "FWD" : "BCK", ack);
1596
1597   if (GNUNET_YES == fwd)
1598   {
1599     rel = ch->root_rel;
1600   }
1601   else
1602   {
1603     rel = ch->dest_rel;
1604   }
1605   if (NULL == rel)
1606   {
1607     GNUNET_break (0);
1608     return;
1609   }
1610
1611   for (work = GNUNET_NO, copy = rel->head_sent; copy != NULL; copy = next)
1612   {
1613     if (GMC_is_pid_bigger (copy->mid, ack))
1614     {
1615       LOG (GNUNET_ERROR_TYPE_DEBUG, "!!!  head %u, out!\n", copy->mid);
1616       channel_rel_free_sent (rel, msg);
1617       break;
1618     }
1619     work = GNUNET_YES;
1620     LOG (GNUNET_ERROR_TYPE_DEBUG, "!!!  id %u\n", copy->mid);
1621     next = copy->next;
1622     rel_message_free (copy);
1623   }
1624   /* ACK client if needed */
1625 //   channel_send_ack (t, type, GNUNET_MESSAGE_TYPE_MESH_UNICAST_ACK == type);
1626
1627   /* If some message was free'd, update the retransmission delay*/
1628   if (GNUNET_YES == work)
1629   {
1630     if (GNUNET_SCHEDULER_NO_TASK != rel->retry_task)
1631     {
1632       GNUNET_SCHEDULER_cancel (rel->retry_task);
1633       if (NULL == rel->head_sent)
1634       {
1635         rel->retry_task = GNUNET_SCHEDULER_NO_TASK;
1636       }
1637       else
1638       {
1639         struct GNUNET_TIME_Absolute new_target;
1640         struct GNUNET_TIME_Relative delay;
1641
1642         delay = GNUNET_TIME_relative_multiply (rel->retry_timer,
1643                                                MESH_RETRANSMIT_MARGIN);
1644         new_target = GNUNET_TIME_absolute_add (rel->head_sent->timestamp,
1645                                                delay);
1646         delay = GNUNET_TIME_absolute_get_remaining (new_target);
1647         rel->retry_task =
1648             GNUNET_SCHEDULER_add_delayed (delay,
1649                                           &channel_retransmit_message,
1650                                           rel);
1651       }
1652     }
1653     else
1654       GNUNET_break (0);
1655   }
1656 }
1657
1658
1659 /**
1660  * Handler for channel create messages.
1661  *
1662  * Does not have fwd parameter because it's always 'FWD': channel is incoming.
1663  *
1664  * @param t Tunnel this channel will be in.
1665  * @param msg Channel crate message.
1666  */
1667 struct MeshChannel *
1668 GMCH_handle_create (struct MeshTunnel3 *t,
1669                     const struct GNUNET_MESH_ChannelCreate *msg)
1670 {
1671   MESH_ChannelNumber chid;
1672   struct MeshChannel *ch;
1673   struct MeshClient *c;
1674   uint32_t port;
1675
1676   chid = ntohl (msg->chid);
1677
1678   ch = GMT_get_channel (t, chid);
1679   if (NULL == ch)
1680   {
1681     /* Create channel */
1682     ch = channel_new (t, NULL, 0);
1683     ch->gid = chid;
1684   }
1685   channel_set_options (ch, ntohl (msg->opt));
1686
1687   /* Find a destination client */
1688   port = ntohl (msg->port);
1689   LOG (GNUNET_ERROR_TYPE_DEBUG, "   port %u\n", port);
1690   c = GML_client_get_by_port (port);
1691   if (NULL == c)
1692   {
1693     /* TODO send reject */
1694     LOG (GNUNET_ERROR_TYPE_DEBUG, "  no client has port registered\n");
1695     channel_destroy (ch);
1696     return NULL;
1697   }
1698   else
1699   {
1700     LOG (GNUNET_ERROR_TYPE_DEBUG, "  client %p has port registered\n", c);
1701   }
1702
1703   add_destination (ch, c);
1704   if (GNUNET_YES == ch->reliable)
1705     LOG (GNUNET_ERROR_TYPE_DEBUG, "!!! Reliable\n");
1706   else
1707     LOG (GNUNET_ERROR_TYPE_DEBUG, "!!! Not Reliable\n");
1708
1709   GMCH_send_create (ch);
1710   channel_send_ack (ch, GNUNET_YES);
1711
1712   return ch;
1713 }
1714
1715
1716 /**
1717  * Handler for channel ack messages.
1718  *
1719  * @param ch Channel.
1720  * @param msg Message.
1721  * @param fwd Is this message fwd? This only is meaningful in loopback channels.
1722  *            #GNUNET_YES if message is FWD on the respective channel (loopback)
1723  *            #GNUNET_NO if message is BCK on the respective channel (loopback)
1724  *            #GNUNET_SYSERR if message on a one-ended channel (remote)
1725  */
1726 void
1727 GMCH_handle_ack (struct MeshChannel *ch,
1728                  const struct GNUNET_MESH_ChannelManage *msg,
1729                  int fwd)
1730 {
1731   /* If this is a remote (non-loopback) channel, find 'fwd'. */
1732   if (GNUNET_SYSERR == fwd)
1733   {
1734     if (is_loopback (ch))
1735     {
1736       /* It is a loopback channel after all... */
1737       GNUNET_break (0);
1738       return;
1739     }
1740     fwd = (NULL != ch->dest) ? GNUNET_YES : GNUNET_NO;
1741   }
1742
1743   channel_confirm (ch, !fwd);
1744 }
1745
1746
1747 /**
1748  * Handler for channel destroy messages.
1749  *
1750  * @param ch Channel to be destroyed of.
1751  * @param msg Message.
1752  * @param fwd Is this message fwd? This only is meaningful in loopback channels.
1753  *            #GNUNET_YES if message is FWD on the respective channel (loopback)
1754  *            #GNUNET_NO if message is BCK on the respective channel (loopback)
1755  *            #GNUNET_SYSERR if message on a one-ended channel (remote)
1756  */
1757 void
1758 GMCH_handle_destroy (struct MeshChannel *ch,
1759                      const struct GNUNET_MESH_ChannelManage *msg,
1760                      int fwd)
1761 {
1762   struct MeshTunnel3 *t;
1763
1764   /* If this is a remote (non-loopback) channel, find 'fwd'. */
1765   if (GNUNET_SYSERR == fwd)
1766   {
1767     if (is_loopback (ch))
1768     {
1769       /* It is a loopback channel after all... */
1770       GNUNET_break (0);
1771       return;
1772     }
1773     fwd = (NULL != ch->dest) ? GNUNET_YES : GNUNET_NO;
1774   }
1775
1776   GMCH_debug (ch);
1777   if ( (fwd && NULL == ch->dest) || (!fwd && NULL == ch->root) )
1778   {
1779     /* Not for us (don't destroy twice a half-open loopback channel) */
1780     return;
1781   }
1782
1783   t = ch->t;
1784   GMCH_send_destroy (ch);
1785   channel_destroy (ch);
1786   GMT_destroy_if_empty (t);
1787 }
1788
1789
1790 /**
1791  * Sends an already built message on a channel.
1792  *
1793  * If the channel is on a loopback tunnel, notifies the appropriate destination
1794  * client locally.
1795  *
1796  * On a normal channel passes the message to the tunnel for encryption and
1797  * sending on a connection.
1798  *
1799  * This function DOES NOT save the message for retransmission.
1800  *
1801  * @param message Message to send. Function makes a copy of it.
1802  * @param ch Channel on which this message is transmitted.
1803  * @param fwd Is this a fwd message?
1804  */
1805 void
1806 GMCH_send_prebuilt_message (const struct GNUNET_MessageHeader *message,
1807                             struct MeshChannel *ch, int fwd)
1808 {
1809   LOG (GNUNET_ERROR_TYPE_DEBUG, "GMCH Send %s %s on channel %s\n",
1810        fwd ? "FWD" : "BCK", GNUNET_MESH_DEBUG_M2S (ntohs (message->type)), 
1811        GMCH_2s (ch));
1812
1813   if (GMT_is_loopback (ch->t))
1814   {
1815     handle_loopback (ch, message, fwd);
1816     return;
1817   }
1818
1819   GMT_send_prebuilt_message (message, ch->t, ch, fwd);
1820 }
1821
1822
1823 /**
1824  * Get the static string for identification of the channel.
1825  *
1826  * @param ch Channel.
1827  *
1828  * @return Static string with the channel IDs.
1829  */
1830 const char *
1831 GMCH_2s (const struct MeshChannel *ch)
1832 {
1833   static char buf[64];
1834
1835   if (NULL == ch)
1836     return "(NULL Channel)";
1837
1838   sprintf (buf, "%s:%X (%X / %X)",
1839            GMT_2s (ch->t), ch->gid, ch->lid_root, ch->lid_dest);
1840
1841   return buf;
1842 }