-bump name to 97
[oweals/gnunet.git] / src / mesh / gnunet-service-mesh_tunnel.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 #include "platform.h"
22 #include "gnunet_util_lib.h"
23
24 #include "gnunet_statistics_service.h"
25
26 #include "mesh_protocol_enc.h"
27
28 #include "gnunet-service-mesh_tunnel.h"
29 #include "gnunet-service-mesh_connection.h"
30 #include "gnunet-service-mesh_channel.h"
31 #include "gnunet-service-mesh_peer.h"
32 #include "mesh_path.h"
33
34 #define LOG(level, ...) GNUNET_log_from(level,"mesh-tun",__VA_ARGS__)
35
36 /**
37  * All the states a tunnel can be in.
38  */
39 enum MeshTunnelState
40 {
41     /**
42      * Uninitialized status, should never appear in operation.
43      */
44   MESH_TUNNEL_NEW,
45
46     /**
47      * Path to the peer not known yet
48      */
49   MESH_TUNNEL_SEARCHING,
50
51     /**
52      * Request sent, not yet answered.
53      */
54   MESH_TUNNEL_WAITING,
55
56     /**
57      * Peer connected and ready to accept data
58      */
59   MESH_TUNNEL_READY,
60
61     /**
62      * Peer connected previosly but not responding
63      */
64   MESH_TUNNEL_RECONNECTING
65 };
66
67
68
69 /******************************************************************************/
70 /********************************   STRUCTS  **********************************/
71 /******************************************************************************/
72
73 /**
74  * Struct containing all information regarding a tunnel to a peer.
75  */
76 struct MeshTunnel2
77 {
78     /**
79      * Endpoint of the tunnel.
80      */
81   struct MeshPeer *peer;
82
83     /**
84      * State of the tunnel.
85      */
86   enum MeshTunnelState state;
87
88   /**
89    * Local peer ephemeral private key
90    */
91   struct GNUNET_CRYPTO_EccPrivateKey *my_eph_key;
92
93   /**
94    * Local peer ephemeral public key
95    */
96   struct GNUNET_CRYPTO_EccPublicSignKey *my_eph;
97
98   /**
99    * Remote peer's public key.
100    */
101   struct GNUNET_CRYPTO_EccPublicSignKey *peers_eph;
102
103   /**
104    * Encryption ("our") key.
105    */
106   struct GNUNET_CRYPTO_SymmetricSessionKey e_key;
107
108   /**
109    * Decryption ("their") key.
110    */
111   struct GNUNET_CRYPTO_SymmetricSessionKey d_key;
112
113   /**
114    * Paths that are actively used to reach the destination peer.
115    */
116   struct MeshConnection *connection_head;
117   struct MeshConnection *connection_tail;
118
119   /**
120    * Next connection number.
121    */
122   uint32_t next_cid;
123
124   /**
125    * Channels inside this tunnel.
126    */
127   struct MeshChannel *channel_head;
128   struct MeshChannel *channel_tail;
129
130   /**
131    * Channel ID for the next created channel.
132    */
133   MESH_ChannelNumber next_chid;
134
135   /**
136    * Channel ID for the next incoming channel.
137    */
138   MESH_ChannelNumber next_local_chid;
139
140   /**
141    * Pending message count.
142    */
143   int pending_messages;
144
145   /**
146    * Destroy flag: if true, destroy on last message.
147    */
148   int destroy;
149
150   /**
151    * Queued messages, to transmit once tunnel gets connected.
152    */
153   struct MeshTunnelQueue *tq_head;
154   struct MeshTunnelQueue *tq_tail;
155 };
156
157
158 /**
159  * Struct used to queue messages in a tunnel.
160  */
161 struct MeshTunnelQueue
162 {
163   /**
164    * DLL
165    */
166   struct MeshTunnelQueue *next;
167   struct MeshTunnelQueue *prev;
168
169   /**
170    * Channel.
171    */
172   struct MeshChannel *ch;
173
174   /**
175    * Message to send.
176    */
177   /* struct GNUNET_MessageHeader *msg; */
178 };
179
180 /******************************************************************************/
181 /*******************************   GLOBALS  ***********************************/
182 /******************************************************************************/
183
184 /**
185  * Global handle to the statistics service.
186  */
187 extern struct GNUNET_STATISTICS_Handle *stats;
188
189 /**
190  * Default TTL for payload packets.
191  */
192 static unsigned long long default_ttl;
193
194 /**
195  * Local peer own ID (memory efficient handle).
196  */
197 static GNUNET_PEER_Id my_short_id;
198
199 /**
200  * Local peer own ID (full value).
201  */
202 const static struct GNUNET_PeerIdentity *my_full_id;
203
204 /**
205  * Own private key.
206  */
207 const static struct GNUNET_CRYPTO_EccPrivateKey *my_private_key;
208
209
210 /******************************************************************************/
211 /********************************   STATIC  ***********************************/
212 /******************************************************************************/
213
214 /**
215  * Get string description for tunnel state.
216  *
217  * @param s Tunnel state.
218  *
219  * @return String representation.
220  */
221 static const char *
222 GNUNET_MESH_DEBUG_TS2S (enum MeshTunnelState s)
223 {
224   static char buf[128];
225
226   switch (s)
227   {
228     case MESH_TUNNEL_NEW:
229       return "MESH_TUNNEL_NEW";
230     case MESH_TUNNEL_SEARCHING:
231       return "MESH_TUNNEL_SEARCHING";
232     case MESH_TUNNEL_WAITING:
233       return "MESH_TUNNEL_WAITING";
234     case MESH_TUNNEL_READY:
235       return "MESH_TUNNEL_READY";
236     case MESH_TUNNEL_RECONNECTING:
237       return "MESH_TUNNEL_RECONNECTING";
238
239     default:
240       sprintf (buf, "%u (UNKNOWN STATE)", s);
241       return buf;
242   }
243 }
244
245
246 /**
247  * Pick a connection on which send the next data message.
248  *
249  * @param t Tunnel on which to send the message.
250  * @param fwd Is this a fwd message?
251  *
252  * @return The connection on which to send the next message.
253  */
254 static struct MeshConnection *
255 tunnel_get_connection (struct MeshTunnel2 *t, int fwd)
256 {
257   struct MeshConnection *c;
258   struct MeshConnection *best;
259   struct MeshFlowControl *fc;
260   unsigned int lowest_q;
261
262   LOG (GNUNET_ERROR_TYPE_DEBUG, "tunnel_get_connection %s\n",
263               peer2s (t->peer));
264   best = NULL;
265   lowest_q = UINT_MAX;
266   for (c = t->connection_head; NULL != c; c = c->next)
267   {
268     LOG (GNUNET_ERROR_TYPE_DEBUG, "  connection %s: %u\n",
269                 GNUNET_h2s (GMC_get_id (c)), c->state);
270     if (MESH_CONNECTION_READY == c->state)
271     {
272       fc = fwd ? &c->fwd_fc : &c->bck_fc;
273       if (NULL == fc)
274       {
275         GNUNET_break (0);
276         continue;
277       }
278       LOG (GNUNET_ERROR_TYPE_DEBUG, "    q_n %u, \n", fc->queue_n);
279       if (fc->queue_n < lowest_q)
280       {
281         best = c;
282         lowest_q = fc->queue_n;
283       }
284     }
285   }
286   return best;
287 }
288
289
290 /**
291  * Get the total buffer space for a tunnel.
292  *
293  * @param t Tunnel.
294  * @param fwd Is this for FWD traffic?
295  *
296  * @return Buffer space offered by all connections in the tunnel.
297  */
298 static unsigned int
299 tunnel_get_buffer (struct MeshTunnel2 *t, int fwd)
300 {
301   struct MeshConnection *c;
302   struct MeshFlowControl *fc;
303   unsigned int buffer;
304
305   c = t->connection_head;
306   buffer = 0;
307
308   /* If terminal, return biggest channel buffer */
309   if (NULL == c || GMC_is_terminal (c, fwd))
310   {
311     struct MeshChannel *ch;
312     unsigned int ch_buf;
313
314     if (NULL == t->channel_head)
315       return 64;
316
317     for (ch = t->channel_head; NULL != ch; ch = ch->next)
318     {
319       ch_buf = GMCH_get_buffer (ch, fwd);
320       if (ch_buf > buffer)
321         buffer = ch_buf;
322     }
323     return buffer;
324   }
325
326   /* If not terminal, return sum of connection buffers */
327   while (NULL != c)
328   {
329     if (c->state != MESH_CONNECTION_READY)
330     {
331       c = c->next;
332       continue;
333     }
334
335     fc = fwd ? &c->fwd_fc : &c->bck_fc;
336     buffer += fc->queue_max - fc->queue_n;
337     c = c->next;
338   }
339
340   return buffer;
341 }
342
343
344 /**
345  * Send all cached messages that we can, tunnel is online.
346  *
347  * @param t Tunnel that holds the messages.
348  * @param fwd Is this fwd?
349  */
350 static void
351 tunnel_send_queued_data (struct MeshTunnel2 *t, int fwd)
352 {
353   struct MeshTunnelQueue *tq;
354   struct MeshTunnelQueue *next;
355   unsigned int room;
356
357   LOG (GNUNET_ERROR_TYPE_DEBUG,
358               "tunnel_send_queued_data on tunnel %s\n",
359               peer2s (t->peer));
360   room = tunnel_get_buffer (t, fwd);
361   LOG (GNUNET_ERROR_TYPE_DEBUG, "  buffer space: %u\n", room);
362   for (tq = t->tq_head; NULL != tq && room > 0; tq = next)
363   {
364     next = tq->next;
365     room--;
366     GNUNET_CONTAINER_DLL_remove (t->tq_head, t->tq_tail, tq);
367     GMCH_send_prebuilt_message ((struct GNUNET_MessageHeader *) &tq[1],
368                                 tq->ch, fwd);
369
370     GNUNET_free (tq);
371   }
372 }
373
374
375 /******************************************************************************/
376 /********************************    API    ***********************************/
377 /******************************************************************************/
378
379 /**
380  * Cache a message to be sent once tunnel is online.
381  *
382  * @param t Tunnel to hold the message.
383  * @param ch Channel the message is about.
384  * @param msg Message itself (copy will be made).
385  * @param fwd Is this fwd?
386  */
387 void
388 GMT_queue_data (struct MeshTunnel2 *t,
389                 struct MeshChannel *ch,
390                 struct GNUNET_MessageHeader *msg,
391                 int fwd)
392 {
393   struct MeshTunnelQueue *tq;
394   uint16_t size = ntohs (msg->size);
395
396   tq = GNUNET_malloc (sizeof (struct MeshTunnelQueue) + size);
397
398   tq->ch = ch;
399   memcpy (&tq[1], msg, size);
400   GNUNET_CONTAINER_DLL_insert_tail (t->tq_head, t->tq_tail, tq);
401
402   if (MESH_TUNNEL_READY == t->state)
403     tunnel_send_queued_data (t, fwd);
404 }
405
406
407 /**
408  * Initialize the tunnel subsystem.
409  *
410  * @param c Configuration handle.
411  * @param id Peer identity.
412  * @param key ECC private key, to derive all other keys and do crypto.
413  */
414 void
415 GMT_init (const struct GNUNET_CONFIGURATION_Handle *c,
416           const struct GNUNET_PeerIdentity *id,
417           const struct GNUNET_CRYPTO_EccPrivateKey *key)
418 {
419   if (GNUNET_OK !=
420       GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DEFAULT_TTL",
421                                              &default_ttl))
422   {
423     LOG_config_invalid (GNUNET_ERROR_TYPE_WARNING,
424                                "MESH", "DEFAULT_TTL", "USING DEFAULT");
425     default_ttl = 64;
426   }
427   my_full_id = id;
428   my_private_key = key;
429   my_short_id = GNUNET_PEER_intern (my_full_id);
430 }
431
432
433 /**
434  * Shut down the tunnel subsystem.
435  */
436 void
437 GMT_shutdown (void)
438 {
439   GNUNET_PEER_change_rc (my_short_id, -1);
440 }
441
442
443 /**
444  * Create a tunnel.
445  */
446 struct MeshTunnel2 *
447 GMT_new (void)
448 {
449   struct MeshTunnel2 *t;
450
451   t = GNUNET_new (struct MeshTunnel2);
452   t->next_chid = 0;
453   t->next_local_chid = GNUNET_MESH_LOCAL_CHANNEL_ID_SERV;
454 //   if (GNUNET_OK !=
455 //       GNUNET_CONTAINER_multihashmap_put (tunnels, tid, t,
456 //                                          GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
457 //   {
458 //     GNUNET_break (0);
459 //     tunnel_destroy (t);
460 //     return NULL;
461 //   }
462
463 //   char salt[] = "salt";
464 //   GNUNET_CRYPTO_kdf (&t->e_key, sizeof (struct GNUNET_CRYPTO_SymmetricSessionKey),
465 //                      salt, sizeof (salt),
466 //                      &t->e_key, sizeof (struct GNUNET_CRYPTO_SymmetricSessionKey),
467 //                      &my_full_id, sizeof (struct GNUNET_PeerIdentity),
468 //                      GNUNET_PEER_resolve2 (t->peer->id), sizeof (struct GNUNET_PeerIdentity),
469 //                      NULL);
470 //   GNUNET_CRYPTO_kdf (&t->d_key, sizeof (struct GNUNET_CRYPTO_SymmetricSessionKey),
471 //                      salt, sizeof (salt),
472 //                      &t->d_key, sizeof (struct GNUNET_CRYPTO_SymmetricSessionKey),
473 //                      GNUNET_PEER_resolve2 (t->peer->id), sizeof (struct GNUNET_PeerIdentity),
474 //                      &my_full_id, sizeof (struct GNUNET_PeerIdentity),
475 //                      NULL);
476
477   return t;
478 }
479
480
481
482 /**
483  * Change the tunnel state.
484  *
485  * @param t Tunnel whose state to change.
486  * @param state New state.
487  */
488 void
489 GMT_change_state (struct MeshTunnel2* t, enum MeshTunnelState state)
490 {
491   if (NULL == t)
492     return;
493   LOG (GNUNET_ERROR_TYPE_DEBUG,
494               "Tunnel %s state was %s\n",
495               peer2s (t->peer),
496               GNUNET_MESH_DEBUG_TS2S (t->state));
497   LOG (GNUNET_ERROR_TYPE_DEBUG,
498               "Tunnel %s state is now %s\n",
499               peer2s (t->peer),
500               GNUNET_MESH_DEBUG_TS2S (state));
501   t->state = state;
502 }
503
504
505 /**
506  * Add a connection to a tunnel.
507  *
508  * @param t Tunnel.
509  * @param c Connection.
510  */
511 void
512 GMT_add_connection (struct MeshTunnel2 *t, struct MeshConnection *c)
513 {
514   struct MeshConnection *aux;
515   c->t = t;
516   for (aux = t->connection_head; aux != NULL; aux = aux->next)
517     if (aux == c)
518       return;
519     GNUNET_CONTAINER_DLL_insert_tail (t->connection_head, t->connection_tail, c);
520 }
521
522
523
524
525 /**
526  * Tunnel is empty: destroy it.
527  *
528  * Notifies all connections about the destruction.
529  *
530  * @param t Tunnel to destroy.
531  */
532 void
533 GMT_destroy_empty (struct MeshTunnel2 *t)
534 {
535   struct MeshConnection *c;
536
537   for (c = t->connection_head; NULL != c; c = c->next)
538   {
539     if (GNUNET_NO == c->destroy)
540       GMC_send_destroy (c);
541   }
542
543   if (0 == t->pending_messages)
544     GMT_destroy (t);
545   else
546     t->destroy = GNUNET_YES;
547 }
548
549
550 /**
551  * Destroy tunnel if empty (no more channels).
552  *
553  * @param t Tunnel to destroy if empty.
554  */
555 void
556 GMT_destroy_if_empty (struct MeshTunnel2 *t)
557 {
558   if (1 < GMCH_count (t->channel_head))
559     return;
560
561   GMT_destroy_empty (t);
562 }
563
564
565
566 /**
567  * Destroy the tunnel.
568  *
569  * This function does not generate any warning traffic to clients or peers.
570  *
571  * Tasks:
572  * Cancel messages belonging to this tunnel queued to neighbors.
573  * Free any allocated resources linked to the tunnel.
574  *
575  * @param t The tunnel to destroy.
576  */
577 void
578 GMT_destroy (struct MeshTunnel2 *t)
579 {
580   struct MeshConnection *c;
581   struct MeshConnection *next;
582
583   if (NULL == t)
584     return;
585
586   LOG (GNUNET_ERROR_TYPE_DEBUG, "destroying tunnel %s\n",
587               peer2s (t->peer));
588
589 //   if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (tunnels, &t->id, t))
590 //     GNUNET_break (0);
591
592   for (c = t->connection_head; NULL != c; c = next)
593   {
594     next = c->next;
595     GMC_destroy (c);
596   }
597
598   GNUNET_STATISTICS_update (stats, "# tunnels", -1, GNUNET_NO);
599   GMP_set_tunnel (t->peer, NULL);
600
601   GNUNET_free (t);
602 }
603
604 /**
605  * Demultiplex by message type and call appropriate handler for a message
606  * towards a channel of a local tunnel.
607  *
608  * @param t Tunnel this message came on.
609  * @param msgh Message header.
610  * @param fwd Is this message fwd?
611  */
612 void
613 GMT_handle_decrypted (struct MeshTunnel2 *t,
614                       const struct GNUNET_MessageHeader *msgh,
615                       int fwd)
616 {
617   switch (ntohs (msgh->type))
618   {
619     case GNUNET_MESSAGE_TYPE_MESH_DATA:
620       /* Don't send hop ACK, wait for client to ACK */
621       GMCH_handle_data (t, (struct GNUNET_MESH_Data *) msgh, fwd);
622       break;
623
624     case GNUNET_MESSAGE_TYPE_MESH_DATA_ACK:
625       GMCH_handle_data_ack (t, (struct GNUNET_MESH_DataACK *) msgh, fwd);
626       break;
627
628     case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE:
629       GMCH_handle_create (t,
630                           (struct GNUNET_MESH_ChannelCreate *) msgh,
631                           fwd);
632       break;
633
634     case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_ACK:
635       GMCH_handle_ack (t,
636                        (struct GNUNET_MESH_ChannelManage *) msgh,
637                        fwd);
638       break;
639
640     case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_DESTROY:
641       GMCH_handle_destroy (t,
642                            (struct GNUNET_MESH_ChannelManage *) msgh,
643                            fwd);
644       break;
645
646     default:
647       GNUNET_break_op (0);
648       LOG (GNUNET_ERROR_TYPE_DEBUG,
649            "end-to-end message not known (%u)\n",
650            ntohs (msgh->type));
651   }
652 }
653
654
655 /**
656  * Notifies a tunnel that a connection has broken that affects at least
657  * some of its peers. Sends a notification towards the root of the tree.
658  * In case the peer is the owner of the tree, notifies the client that owns
659  * the tunnel and tries to reconnect.
660  *
661  * FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME
662  *
663  * @param t Tunnel affected.
664  * @param p1 Peer that got disconnected from p2.
665  * @param p2 Peer that got disconnected from p1.
666  *
667  * @return Short ID of the peer disconnected (either p1 or p2).
668  *         0 if the tunnel remained unaffected.
669  */
670 GNUNET_PEER_Id
671 GMT_notify_connection_broken (struct MeshTunnel2* t,
672                               GNUNET_PEER_Id p1, GNUNET_PEER_Id p2)
673 {
674 //   if (myid != p1 && myid != p2) FIXME
675 //   {
676 //     return;
677 //   }
678 //
679 //   if (tree_get_predecessor (t->tree) != 0)
680 //   {
681 //     /* We are the peer still connected, notify owner of the disconnection. */
682 //     struct GNUNET_MESH_PathBroken msg;
683 //     struct GNUNET_PeerIdentity neighbor;
684 //
685 //     msg.header.size = htons (sizeof (msg));
686 //     msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN);
687 //     GNUNET_PEER_resolve (t->id.oid, &msg.oid);
688 //     msg.tid = htonl (t->id.tid);
689 //     msg.peer1 = my_full_id;
690 //     GNUNET_PEER_resolve (pid, &msg.peer2);
691 //     GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &neighbor);
692 //     send_prebuilt_message (&msg.header, &neighbor, t);
693 //   }
694   return 0;
695 }
696
697 /**
698  * @brief Use the given path for the tunnel.
699  * Update the next and prev hops (and RCs).
700  * (Re)start the path refresh in case the tunnel is locally owned.
701  *
702  * @param t Tunnel to update.
703  * @param p Path to use.
704  *
705  * @return Connection created.
706  */
707 struct MeshConnection *
708 GMT_use_path (struct MeshTunnel2 *t, struct MeshPeerPath *p)
709 {
710   struct MeshConnection *c;
711   struct GNUNET_HashCode cid;
712   unsigned int own_pos;
713
714   if (NULL == t || NULL == p)
715   {
716     GNUNET_break (0);
717     return NULL;
718   }
719
720   for (own_pos = 0; own_pos < p->length; own_pos++)
721   {
722     if (p->peers[own_pos] == my_short_id)
723       break;
724   }
725   if (own_pos > p->length - 1)
726   {
727     GNUNET_break (0);
728     return NULL;
729   }
730
731   GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_NONCE, &cid);
732   c = GMC_new (&cid, t, p, own_pos);
733   GNUNET_CONTAINER_DLL_insert (t->connection_head, t->connection_tail, c);
734   return c;
735 }
736
737
738 /**
739  * FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME
740  * Encrypt data with the tunnel key.
741  *
742  * @param t Tunnel whose key to use.
743  * @param dst Destination for the encrypted data.
744  * @param src Source of the plaintext.
745  * @param size Size of the plaintext.
746  * @param iv Initialization Vector to use.
747  * @param fwd Is this a fwd message?
748  */
749 void
750 GMT_encrypt (struct MeshTunnel2 *t,
751              void *dst, const void *src,
752              size_t size, uint64_t iv, int fwd)
753 {
754   memcpy (dst, src, size);
755 }
756
757
758 /**
759  * FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME FIXME
760  * Decrypt data with the tunnel key.
761  *
762  * @param t Tunnel whose key to use.
763  * @param dst Destination for the plaintext.
764  * @param src Source of the encrypted data.
765  * @param size Size of the encrypted data.
766  * @param iv Initialization Vector to use.
767  * @param fwd Is this a fwd message?
768  */
769 void
770 GMT_decrypt (struct MeshTunnel2 *t,
771              void *dst, const void *src,
772              size_t size, uint64_t iv, int fwd)
773 {
774   memcpy (dst, src, size);
775 }
776
777
778 /**
779  * Count established (ready) connections of a tunnel.
780  *
781  * @param t Tunnel on which to send the message.
782  *
783  * @return Number of connections.
784  */
785 unsigned int
786 GMT_count_connections (struct MeshTunnel2 *t)
787 {
788   return GMC_count (t->connection_head);
789 }
790
791
792 /**
793  * Sends an already built message on a tunnel, choosing the best connection.
794  *
795  * @param message Message to send. Function modifies it.
796  * @param t Tunnel on which this message is transmitted.
797  * @param ch Channel on which this message is transmitted.
798  * @param fwd Is this a fwd message?
799  */
800 void
801 GMT_send_prebuilt_message (struct GNUNET_MESH_Encrypted *msg,
802                            struct MeshTunnel2 *t,
803                            struct MeshChannel *ch,
804                            int fwd)
805 {
806   struct MeshConnection *c;
807   uint16_t type;
808
809   LOG (GNUNET_ERROR_TYPE_DEBUG, "Send on Tunnel %s\n",
810               peer2s (t->peer));
811   c = tunnel_get_connection (t, fwd);
812   if (NULL == c)
813   {
814     GNUNET_break (GNUNET_YES == t->destroy);
815     return;
816   }
817   type = ntohs (msg->header.type);
818   switch (type)
819   {
820     case GNUNET_MESSAGE_TYPE_MESH_FWD:
821     case GNUNET_MESSAGE_TYPE_MESH_BCK:
822     case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE:
823     case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_DESTROY:
824       msg->cid = *GMC_get_id (c);
825       msg->ttl = htonl (default_ttl);
826       break;
827     default:
828       LOG (GNUNET_ERROR_TYPE_DEBUG, "unkown type %s\n",
829            GNUNET_MESH_DEBUG_M2S (type));
830       GNUNET_break (0);
831   }
832   msg->reserved = 0;
833
834   GMC_send_prebuilt_message (&msg->header, c, ch, fwd);
835 }