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