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