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