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