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