- sync
[oweals/gnunet.git] / src / mesh / gnunet-service-mesh_connection.c
1 /*
2      This file is part of GNUnet.
3      (C) 2001-2013 Christian Grothoff (and other contributing authors)
4
5      GNUnet is free software; you can redistribute it and/or modify
6      it under the terms of the GNU General Public License as published
7      by the Free Software Foundation; either version 3, or (at your
8      option) any later version.
9
10      GNUnet is distributed in the hope that it will be useful, but
11      WITHOUT ANY WARRANTY; without even the implied warranty of
12      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13      General Public License for more details.
14
15      You should have received a copy of the GNU General Public License
16      along with GNUnet; see the file COPYING.  If not, write to the
17      Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18      Boston, MA 02111-1307, USA.
19 */
20
21 /**
22  * @file mesh/gnunet-service-mesh_connection.c
23  * @brief GNUnet MESH service connection handling
24  * @author Bartlomiej Polot
25  */
26
27 #include "platform.h"
28 #include "gnunet_util_lib.h"
29
30 #include "gnunet_statistics_service.h"
31
32 #include "gnunet-service-mesh_connection.h"
33 #include "gnunet-service-mesh_peer.h"
34 #include "gnunet-service-mesh_tunnel.h"
35 #include "mesh_protocol_enc.h"
36 #include "mesh_path.h"
37
38
39 #define MESH_MAX_POLL_TIME      GNUNET_TIME_relative_multiply (\
40                                   GNUNET_TIME_UNIT_MINUTES,\
41                                   10)
42 #define MESH_RETRANSMIT_TIME    GNUNET_TIME_UNIT_SECONDS
43 #define MESH_RETRANSMIT_MARGIN  4
44
45 #define LOG(level, ...) GNUNET_log_from (level,"mesh-con",__VA_ARGS__)
46
47
48 /******************************************************************************/
49 /********************************   STRUCTS  **********************************/
50 /******************************************************************************/
51
52 /**
53  * Struct containing info about a queued transmission to this peer
54  */
55 struct MeshPeerQueue
56 {
57     /**
58       * DLL next
59       */
60   struct MeshPeerQueue *next;
61
62     /**
63       * DLL previous
64       */
65   struct MeshPeerQueue *prev;
66
67     /**
68      * Peer this transmission is directed to.
69      */
70   struct MeshPeer *peer;
71
72     /**
73      * Connection this message belongs to.
74      */
75   struct MeshConnection *c;
76
77     /**
78      * Is FWD in c?
79      */
80   int fwd;
81
82     /**
83      * Channel this message belongs to, if known.
84      */
85   struct MeshChannel *ch;
86
87     /**
88      * Pointer to info stucture used as cls.
89      */
90   void *cls;
91
92     /**
93      * Type of message
94      */
95   uint16_t type;
96
97     /**
98      * Size of the message
99      */
100   size_t size;
101 };
102
103
104 /**
105  * Struct to encapsulate all the Flow Control information to a peer to which
106  * we are directly connected (on a core level).
107  */
108 struct MeshFlowControl
109 {
110   /**
111    * Connection this controls.
112    */
113   struct MeshConnection *c;
114
115   /**
116    * How many messages are in the queue on this connection.
117    */
118   unsigned int queue_n;
119
120   /**
121    * How many messages do we accept in the queue.
122    */
123   unsigned int queue_max;
124
125   /**
126    * Next ID to use.
127    */
128   uint32_t next_pid;
129
130   /**
131    * ID of the last packet sent towards the peer.
132    */
133   uint32_t last_pid_sent;
134
135   /**
136    * ID of the last packet received from the peer.
137    */
138   uint32_t last_pid_recv;
139
140   /**
141    * Last ACK sent to the peer (peer can't send more than this PID).
142    */
143   uint32_t last_ack_sent;
144
145   /**
146    * Last ACK sent towards the origin (for traffic towards leaf node).
147    */
148   uint32_t last_ack_recv;
149
150   /**
151    * Task to poll the peer in case of a lost ACK causes stall.
152    */
153   GNUNET_SCHEDULER_TaskIdentifier poll_task;
154
155   /**
156    * How frequently to poll for ACKs.
157    */
158   struct GNUNET_TIME_Relative poll_time;
159 };
160
161
162 /**
163  * Struct containing all information regarding a connection to a peer.
164  */
165 struct MeshConnection
166 {
167   /**
168    * Tunnel this connection is part of.
169    */
170   struct MeshTunnel3 *t;
171
172   /**
173    * Flow control information for traffic fwd.
174    */
175   struct MeshFlowControl fwd_fc;
176
177   /**
178    * Flow control information for traffic bck.
179    */
180   struct MeshFlowControl bck_fc;
181
182   /**
183    * ID of the connection.
184    */
185   struct GNUNET_HashCode id;
186
187   /**
188    * State of the connection.
189    */
190   enum MeshConnectionState state;
191
192   /**
193    * Path being used for the tunnel.
194    */
195   struct MeshPeerPath *path;
196
197   /**
198    * Position of the local peer in the path.
199    */
200   unsigned int own_pos;
201
202   /**
203    * Task to keep the used paths alive at the owner,
204    * time tunnel out on all the other peers.
205    */
206   GNUNET_SCHEDULER_TaskIdentifier fwd_maintenance_task;
207
208   /**
209    * Task to keep the used paths alive at the destination,
210    * time tunnel out on all the other peers.
211    */
212   GNUNET_SCHEDULER_TaskIdentifier bck_maintenance_task;
213
214   /**
215    * Pending message count.
216    */
217   int pending_messages;
218
219   /**
220    * Destroy flag: if true, destroy on last message.
221    */
222   int destroy;
223 };
224
225 /******************************************************************************/
226 /*******************************   GLOBALS  ***********************************/
227 /******************************************************************************/
228
229 /**
230  * Global handle to the statistics service.
231  */
232 extern struct GNUNET_STATISTICS_Handle *stats;
233
234 /**
235  * Connections known, indexed by cid (MeshConnection).
236  */
237 static struct GNUNET_CONTAINER_MultiHashMap *connections;
238
239 /**
240  * How many connections are we willing to maintain.
241  * Local connections are always allowed, even if there are more connections than max.
242  */
243 static unsigned long long max_connections;
244
245 /**
246  * How many messages *in total* are we willing to queue, divide by number of
247  * connections to get connection queue size.
248  */
249 static unsigned long long max_msgs_queue;
250
251 /**
252  * How often to send path keepalives. Paths timeout after 4 missed.
253  */
254 static struct GNUNET_TIME_Relative refresh_connection_time;
255
256
257 /******************************************************************************/
258 /********************************   STATIC  ***********************************/
259 /******************************************************************************/
260
261 #if 0 // avoid compiler warning for unused static function
262 static void
263 fc_debug (struct MeshFlowControl *fc)
264 {
265   LOG (GNUNET_ERROR_TYPE_DEBUG, "    IN: %u/%u\n",
266               fc->last_pid_recv, fc->last_ack_sent);
267   LOG (GNUNET_ERROR_TYPE_DEBUG, "    OUT: %u/%u\n",
268               fc->last_pid_sent, fc->last_ack_recv);
269   LOG (GNUNET_ERROR_TYPE_DEBUG, "    QUEUE: %u/%u\n",
270               fc->queue_n, fc->queue_max);
271 }
272
273 static void
274 connection_debug (struct MeshConnection *c)
275 {
276   if (NULL == c)
277   {
278     LOG (GNUNET_ERROR_TYPE_DEBUG, "*** DEBUG NULL CONNECTION ***\n");
279     return;
280   }
281   LOG (GNUNET_ERROR_TYPE_DEBUG, "Connection %s:%X\n",
282               peer2s (c->t->peer), GNUNET_h2s (&c->id));
283   LOG (GNUNET_ERROR_TYPE_DEBUG, "  state: %u, pending msgs: %u\n",
284               c->state, c->pending_messages);
285   LOG (GNUNET_ERROR_TYPE_DEBUG, "  FWD FC\n");
286   fc_debug (&c->fwd_fc);
287   LOG (GNUNET_ERROR_TYPE_DEBUG, "  BCK FC\n");
288   fc_debug (&c->bck_fc);
289 }
290 #endif
291
292 /**
293  * Get string description for tunnel state.
294  *
295  * @param s Tunnel state.
296  *
297  * @return String representation.
298  */
299 static const char *
300 GMC_state2s (enum MeshConnectionState s)
301 {
302   switch (s)
303   {
304     case MESH_CONNECTION_NEW:
305       return "MESH_CONNECTION_NEW";
306     case MESH_CONNECTION_SENT:
307       return "MESH_CONNECTION_SENT";
308     case MESH_CONNECTION_ACK:
309       return "MESH_CONNECTION_ACK";
310     case MESH_CONNECTION_READY:
311       return "MESH_CONNECTION_READY";
312     default:
313       return "MESH_CONNECTION_STATE_ERROR";
314   }
315 }
316
317
318 /**
319  * Initialize a Flow Control structure to the initial state.
320  *
321  * @param fc Flow Control structure to initialize.
322  */
323 static void
324 fc_init (struct MeshFlowControl *fc)
325 {
326   fc->next_pid = 0;
327   fc->last_pid_sent = (uint32_t) -1; /* Next (expected) = 0 */
328   fc->last_pid_recv = (uint32_t) -1;
329   fc->last_ack_sent = (uint32_t) 0;
330   fc->last_ack_recv = (uint32_t) 0;
331   fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
332   fc->poll_time = GNUNET_TIME_UNIT_SECONDS;
333   fc->queue_n = 0;
334   fc->queue_max = (max_msgs_queue / max_connections) + 1;
335 }
336
337
338 /**
339  * Find a connection.
340  *
341  * @param cid Connection ID.
342  */
343 static struct MeshConnection *
344 connection_get (const struct GNUNET_HashCode *cid)
345 {
346   return GNUNET_CONTAINER_multihashmap_get (connections, cid);
347 }
348
349
350 /**
351  * Send an ACK informing the predecessor about the available buffer space.
352  *
353  * Note that for fwd ack, the FWD mean forward *traffic* (root->dest),
354  * the ACK itself goes "back" (dest->root).
355  *
356  * @param c Connection on which to send the ACK.
357  * @param buffer How much space free to advertise?
358  * @param fwd Is this FWD ACK? (Going dest->owner)
359  */
360 static void
361 connection_send_ack (struct MeshConnection *c, unsigned int buffer, int fwd)
362 {
363   struct MeshFlowControl *next_fc;
364   struct MeshFlowControl *prev_fc;
365   struct GNUNET_MESH_ACK msg;
366   uint32_t ack;
367   int delta;
368
369   next_fc = fwd ? &c->fwd_fc : &c->bck_fc;
370   prev_fc = fwd ? &c->bck_fc : &c->fwd_fc;
371
372   LOG (GNUNET_ERROR_TYPE_DEBUG,
373               "connection send %s ack on %s\n",
374               fwd ? "FWD" : "BCK", GNUNET_h2s (&c->id));
375
376   /* Check if we need to transmit the ACK */
377   if (prev_fc->last_ack_sent - prev_fc->last_pid_recv > 3)
378   {
379     LOG (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, buffer > 3\n");
380     LOG (GNUNET_ERROR_TYPE_DEBUG,
381                 "  last pid recv: %u, last ack sent: %u\n",
382                 prev_fc->last_pid_recv, prev_fc->last_ack_sent);
383     return;
384   }
385
386   /* Ok, ACK might be necessary, what PID to ACK? */
387   delta = next_fc->queue_max - next_fc->queue_n;
388   ack = prev_fc->last_pid_recv + delta;
389   LOG (GNUNET_ERROR_TYPE_DEBUG, " ACK %u\n", ack);
390   LOG (GNUNET_ERROR_TYPE_DEBUG,
391               " last pid %u, last ack %u, qmax %u, q %u\n",
392               prev_fc->last_pid_recv, prev_fc->last_ack_sent,
393               next_fc->queue_max, next_fc->queue_n);
394   if (ack == prev_fc->last_ack_sent)
395   {
396     LOG (GNUNET_ERROR_TYPE_DEBUG, "Not sending FWD ACK, not needed\n");
397     return;
398   }
399
400   prev_fc->last_ack_sent = ack;
401
402   /* Build ACK message and send on connection */
403   msg.header.size = htons (sizeof (msg));
404   msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_ACK);
405   msg.ack = htonl (ack);
406   msg.cid = c->id;
407
408   send_prebuilt_message_connection (&msg.header, c, NULL, !fwd);
409 }
410
411
412 /**
413  * Sends a CONNECTION ACK message in reponse to a received CONNECTION_CREATE
414  * directed to us.
415  *
416  * @param connection Connection to confirm.
417  * @param fwd Is this a fwd ACK? (First is bck (SYNACK), second is fwd (ACK))
418  */
419 static void
420 send_connection_ack (struct MeshConnection *connection, int fwd)
421 {
422   struct MeshTunnel3 *t;
423
424   t = connection->t;
425   LOG (GNUNET_ERROR_TYPE_DEBUG, "Send connection ack\n");
426   queue_add (NULL,
427              GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK,
428              sizeof (struct GNUNET_MESH_ConnectionACK),
429              connection,
430              NULL,
431              fwd);
432   if (MESH_TUNNEL_NEW == t->state)
433     tunnel_change_state (t, MESH_TUNNEL_WAITING);
434   if (MESH_CONNECTION_READY != connection->state)
435     connection_change_state (connection, MESH_CONNECTION_SENT);
436 }
437
438
439 /**
440  * Sends a CREATE CONNECTION message for a path to a peer.
441  * Changes the connection and tunnel states if necessary.
442  *
443  * @param connection Connection to create.
444  */
445 static void
446 send_connection_create (struct MeshConnection *connection)
447 {
448   struct MeshTunnel3 *t;
449
450   t = connection->t;
451   LOG (GNUNET_ERROR_TYPE_DEBUG, "Send connection create\n");
452   queue_add (NULL,
453              GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE,
454              sizeof (struct GNUNET_MESH_ConnectionCreate) +
455                 (connection->path->length *
456                  sizeof (struct GNUNET_PeerIdentity)),
457              connection,
458              NULL,
459              GNUNET_YES);
460   if (NULL != t &&
461       (MESH_TUNNEL_SEARCHING == t->state || MESH_TUNNEL_NEW == t->state))
462     tunnel_change_state (t, MESH_TUNNEL_WAITING);
463   if (MESH_CONNECTION_NEW == connection->state)
464     connection_change_state (connection, MESH_CONNECTION_SENT);
465 }
466
467
468 static void
469 connection_change_state (struct MeshConnection* c,
470                          enum MeshConnectionState state)
471 {
472   LOG (GNUNET_ERROR_TYPE_DEBUG,
473               "Connection %s state was %s\n",
474               GNUNET_h2s (&c->id), GMC_state2s (c->state));
475   LOG (GNUNET_ERROR_TYPE_DEBUG,
476               "Connection %s state is now %s\n",
477               GNUNET_h2s (&c->id), GMC_state2s (state));
478   c->state = state;
479 }
480
481
482
483 /**
484  * Send keepalive packets for a connection.
485  *
486  * @param c Connection to keep alive..
487  * @param fwd Is this a FWD keepalive? (owner -> dest).
488  */
489 static void
490 connection_keepalive (struct MeshConnection *c, int fwd)
491 {
492   struct GNUNET_MESH_ConnectionKeepAlive *msg;
493   size_t size = sizeof (struct GNUNET_MESH_ConnectionKeepAlive);
494   char cbuf[size];
495   uint16_t type;
496
497   type = fwd ? GNUNET_MESSAGE_TYPE_MESH_FWD_KEEPALIVE :
498                GNUNET_MESSAGE_TYPE_MESH_BCK_KEEPALIVE;
499
500   LOG (GNUNET_ERROR_TYPE_DEBUG,
501               "sending %s keepalive for connection %s[%d]\n",
502               fwd ? "FWD" : "BCK",
503               peer2s (c->t->peer),
504               c->id);
505
506   msg = (struct GNUNET_MESH_ConnectionKeepAlive *) cbuf;
507   msg->header.size = htons (size);
508   msg->header.type = htons (type);
509   msg->cid = c->id;
510
511   send_prebuilt_message_connection (&msg->header, c, NULL, fwd);
512 }
513
514
515 /**
516  * Send CONNECTION_{CREATE/ACK} packets for a connection.
517  *
518  * @param c Connection for which to send the message.
519  * @param fwd If GNUNET_YES, send CREATE, otherwise send ACK.
520  */
521 static void
522 connection_recreate (struct MeshConnection *c, int fwd)
523 {
524   LOG (GNUNET_ERROR_TYPE_DEBUG, "sending connection recreate\n");
525   if (fwd)
526     send_connection_create (c);
527   else
528     send_connection_ack (c, GNUNET_NO);
529 }
530
531
532 /**
533  * Generic connection timer management.
534  * Depending on the role of the peer in the connection will send the
535  * appropriate message (build or keepalive)
536  *
537  * @param c Conncetion to maintain.
538  * @param fwd Is FWD?
539  */
540 static void
541 connection_maintain (struct MeshConnection *c, int fwd)
542 {
543   if (MESH_TUNNEL_SEARCHING == c->t->state)
544   {
545     /* TODO DHT GET with RO_BART */
546     return;
547   }
548   switch (c->state)
549   {
550     case MESH_CONNECTION_NEW:
551       GNUNET_break (0);
552     case MESH_CONNECTION_SENT:
553       connection_recreate (c, fwd);
554       break;
555     case MESH_CONNECTION_READY:
556       connection_keepalive (c, fwd);
557       break;
558     default:
559       break;
560   }
561 }
562
563
564 static void
565 connection_fwd_keepalive (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
566 {
567   struct MeshConnection *c = cls;
568
569   c->fwd_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
570   if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
571     return;
572
573   connection_maintain (c, GNUNET_YES);
574   c->fwd_maintenance_task = GNUNET_SCHEDULER_add_delayed (refresh_connection_time,
575                                                           &connection_fwd_keepalive,
576                                                           c);
577 }
578
579
580 static void
581 connection_bck_keepalive (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
582 {
583   struct MeshConnection *c = cls;
584
585   c->bck_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
586   if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
587     return;
588
589   connection_maintain (c, GNUNET_NO);
590   c->bck_maintenance_task = GNUNET_SCHEDULER_add_delayed (refresh_connection_time,
591                                                           &connection_bck_keepalive,
592                                                           c);
593 }
594
595
596
597 /**
598  * Get the first transmittable message for a connection.
599  *
600  * @param c Connection.
601  * @param fwd Is this FWD?
602  *
603  * @return First transmittable message.
604  */
605 static struct MeshPeerQueue *
606 connection_get_first_message (struct MeshConnection *c, int fwd)
607 {
608   struct MeshPeerQueue *q;
609   struct MeshPeer *p;
610
611   p = connection_get_hop (c, fwd);
612
613   for (q = p->queue_head; NULL != q; q = q->next)
614   {
615     if (q->c != c)
616       continue;
617     if (queue_is_sendable (q))
618       return q;
619   }
620
621   return NULL;
622 }
623
624
625 /**
626  * @brief Re-initiate traffic on this connection if necessary.
627  *
628  * Check if there is traffic queued towards this peer
629  * and the core transmit handle is NULL (traffic was stalled).
630  * If so, call core tmt rdy.
631  *
632  * @param c Connection on which initiate traffic.
633  * @param fwd Is this about fwd traffic?
634  */
635 static void
636 connection_unlock_queue (struct MeshConnection *c, int fwd)
637 {
638   struct MeshPeer *peer;
639   struct MeshPeerQueue *q;
640   size_t size;
641
642   LOG (GNUNET_ERROR_TYPE_DEBUG,
643               "connection_unlock_queue %s on %s\n",
644               fwd ? "FWD" : "BCK", GNUNET_h2s (&c->id));
645
646   if (GMC_is_terminal (c, fwd))
647   {
648     LOG (GNUNET_ERROR_TYPE_DEBUG, " is terminal!\n");
649     return;
650   }
651
652   peer = connection_get_hop (c, fwd);
653
654   if (NULL != peer->core_transmit)
655   {
656     LOG (GNUNET_ERROR_TYPE_DEBUG, "  already unlocked!\n");
657     return; /* Already unlocked */
658   }
659
660   q = connection_get_first_message (c, fwd);
661   if (NULL == q)
662   {
663     LOG (GNUNET_ERROR_TYPE_DEBUG, "  queue empty!\n");
664     return; /* Nothing to transmit */
665   }
666
667   size = q->size;
668   peer->core_transmit =
669       GNUNET_CORE_notify_transmit_ready (core_handle,
670                                          GNUNET_NO,
671                                          0,
672                                          GNUNET_TIME_UNIT_FOREVER_REL,
673                                          GNUNET_PEER_resolve2 (peer->id),
674                                          size,
675                                          &queue_send,
676                                          peer);
677 }
678
679
680 /**
681  * Cancel all transmissions that belong to a certain connection.
682  *
683  * @param c Connection which to cancel.
684  * @param fwd Cancel fwd traffic?
685  */
686 static void
687 connection_cancel_queues (struct MeshConnection *c, int fwd)
688 {
689   struct MeshPeerQueue *q;
690   struct MeshPeerQueue *next;
691   struct MeshFlowControl *fc;
692   struct MeshPeer *peer;
693
694   if (NULL == c)
695   {
696     GNUNET_break (0);
697     return;
698   }
699   fc = fwd ? &c->fwd_fc : &c->bck_fc;
700   peer = connection_get_hop (c, fwd);
701
702   for (q = peer->queue_head; NULL != q; q = next)
703   {
704     next = q->next;
705     if (q->c == c)
706     {
707       LOG (GNUNET_ERROR_TYPE_DEBUG,
708                   "connection_cancel_queue %s\n",
709                   GNUNET_MESH_DEBUG_M2S (q->type));
710       queue_destroy (q, GNUNET_YES);
711     }
712   }
713   if (NULL == peer->queue_head)
714   {
715     if (NULL != peer->core_transmit)
716     {
717       GNUNET_CORE_notify_transmit_ready_cancel (peer->core_transmit);
718       peer->core_transmit = NULL;
719     }
720     if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task)
721     {
722       GNUNET_SCHEDULER_cancel (fc->poll_task);
723       fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
724     }
725   }
726 }
727
728
729 /**
730  * Function called if a connection has been stalled for a while,
731  * possibly due to a missed ACK. Poll the neighbor about its ACK status.
732  *
733  * @param cls Closure (poll ctx).
734  * @param tc TaskContext.
735  */
736 static void
737 connection_poll (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
738 {
739   struct MeshFlowControl *fc = cls;
740   struct GNUNET_MESH_Poll msg;
741   struct MeshConnection *c;
742
743   fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
744   if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
745   {
746     return;
747   }
748
749   c = fc->c;
750   LOG (GNUNET_ERROR_TYPE_DEBUG, " *** Polling!\n");
751   LOG (GNUNET_ERROR_TYPE_DEBUG, " *** connection [%X]\n",
752               GNUNET_h2s (&c->id));
753   LOG (GNUNET_ERROR_TYPE_DEBUG, " ***   %s\n",
754               fc == &c->fwd_fc ? "FWD" : "BCK");
755
756   msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_POLL);
757   msg.header.size = htons (sizeof (msg));
758   LOG (GNUNET_ERROR_TYPE_DEBUG, " *** pid (%u)!\n", fc->last_pid_sent);
759   send_prebuilt_message_connection (&msg.header, c, NULL, fc == &c->fwd_fc);
760   fc->poll_time = GNUNET_TIME_STD_BACKOFF (fc->poll_time);
761   fc->poll_task = GNUNET_SCHEDULER_add_delayed (fc->poll_time,
762                                                 &connection_poll, fc);
763 }
764
765
766
767
768 /**
769  * Get the previous hop in a connection
770  *
771  * @param c Connection.
772  *
773  * @return Previous peer in the connection.
774  */
775 static struct MeshPeer *
776 connection_get_prev_hop (struct MeshConnection *c)
777 {
778   GNUNET_PEER_Id id;
779
780   if (0 == c->own_pos || c->path->length < 2)
781     id = c->path->peers[0];
782   else
783     id = c->path->peers[c->own_pos - 1];
784
785   return peer_get_short (id);
786 }
787
788
789 /**
790  * Get the next hop in a connection
791  *
792  * @param c Connection.
793  *
794  * @return Next peer in the connection.
795  */
796 static struct MeshPeer *
797 connection_get_next_hop (struct MeshConnection *c)
798 {
799   GNUNET_PEER_Id id;
800
801   if ((c->path->length - 1) == c->own_pos || c->path->length < 2)
802     id = c->path->peers[c->path->length - 1];
803   else
804     id = c->path->peers[c->own_pos + 1];
805
806   return peer_get_short (id);
807 }
808
809
810 /**
811  * Get the hop in a connection.
812  *
813  * @param c Connection.
814  * @param fwd Next hop?
815  *
816  * @return Next peer in the connection.
817  */
818 static struct MeshPeer *
819 connection_get_hop (struct MeshConnection *c, int fwd)
820 {
821   if (fwd)
822     return connection_get_next_hop (c);
823   return connection_get_prev_hop (c);
824 }
825
826
827
828
829 /**
830  * Timeout function due to lack of keepalive/traffic from the owner.
831  * Destroys connection if called.
832  *
833  * @param cls Closure (connection to destroy).
834  * @param tc TaskContext.
835  */
836 static void
837 connection_fwd_timeout (void *cls,
838                         const struct GNUNET_SCHEDULER_TaskContext *tc)
839 {
840   struct MeshConnection *c = cls;
841
842   c->fwd_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
843   if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
844     return;
845   LOG (GNUNET_ERROR_TYPE_DEBUG,
846               "Connection %s[%X] FWD timed out. Destroying.\n",
847               peer2s (c->t->peer),
848               c->id);
849
850   if (GMC_is_origin (c, GNUNET_YES)) /* If local, leave. */
851     return;
852
853   GMC_destroy (c);
854 }
855
856
857 /**
858  * Timeout function due to lack of keepalive/traffic from the destination.
859  * Destroys connection if called.
860  *
861  * @param cls Closure (connection to destroy).
862  * @param tc TaskContext
863  */
864 static void
865 connection_bck_timeout (void *cls,
866                         const struct GNUNET_SCHEDULER_TaskContext *tc)
867 {
868   struct MeshConnection *c = cls;
869
870   c->bck_maintenance_task = GNUNET_SCHEDULER_NO_TASK;
871   if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
872     return;
873
874   LOG (GNUNET_ERROR_TYPE_DEBUG,
875               "Connection %s[%X] FWD timed out. Destroying.\n",
876               peer2s (c->t->peer),
877               c->id);
878
879   if (GMC_is_origin (c, GNUNET_NO)) /* If local, leave. */
880     return;
881
882   GMC_destroy (c);
883 }
884
885
886 /**
887  * Resets the connection timeout task, some other message has done the
888  * task's job.
889  * - For the first peer on the direction this means to send
890  *   a keepalive or a path confirmation message (either create or ACK).
891  * - For all other peers, this means to destroy the connection,
892  *   due to lack of activity.
893  * Starts the tiemout if no timeout was running (connection just created).
894  *
895  * @param c Connection whose timeout to reset.
896  * @param fwd Is this forward?
897  *
898  * TODO use heap to improve efficiency of scheduler.
899  */
900 static void
901 connection_reset_timeout (struct MeshConnection *c, int fwd)
902 {
903   GNUNET_SCHEDULER_TaskIdentifier *ti;
904   GNUNET_SCHEDULER_Task f;
905
906   ti = fwd ? &c->fwd_maintenance_task : &c->bck_maintenance_task;
907
908   if (GNUNET_SCHEDULER_NO_TASK != *ti)
909     GNUNET_SCHEDULER_cancel (*ti);
910
911   if (GMC_is_origin (c, fwd)) /* Endpoint */
912   {
913     f  = fwd ? &connection_fwd_keepalive : &connection_bck_keepalive;
914     *ti = GNUNET_SCHEDULER_add_delayed (refresh_connection_time, f, c);
915   }
916   else /* Relay */
917   {
918     struct GNUNET_TIME_Relative delay;
919
920     delay = GNUNET_TIME_relative_multiply (refresh_connection_time, 4);
921     f  = fwd ? &connection_fwd_timeout : &connection_bck_timeout;
922     *ti = GNUNET_SCHEDULER_add_delayed (delay, f, c);
923   }
924 }
925
926
927 /**
928  * 
929  */
930 static void
931 register_neighbors (struct MeshConnection *c)
932 {
933   struct MeshPeer *peer;
934
935   peer = connection_get_next_hop (c);
936   if (GNUNET_NO == GMP_is_neighbor (peer))
937   {
938     GMC_destroy (c);
939     return NULL;
940   }
941   GMP_add_connection (peer, c);
942   peer = connection_get_prev_hop (c);
943   if (GNUNET_NO == GMP_is_neighbor (peer))
944   {
945     GMC_destroy (c);
946     return NULL;
947   }
948   GMP_add_connection (peer, c);
949 }
950
951
952 /**
953  * 
954  */
955 static void
956 unregister_neighbors (struct MeshConnection *c)
957 {
958   struct MeshPeer *peer;
959
960   peer = connection_get_next_hop (c);
961   GMP_remove_connection (peer, c);
962
963   peer = connection_get_prev_hop (c);
964   GMP_remove_connection (peer, c);
965
966 }
967
968
969 /******************************************************************************/
970 /********************************    API    ***********************************/
971 /******************************************************************************/
972
973 /**
974  * Core handler for connection creation.
975  *
976  * @param cls Closure (unused).
977  * @param peer Sender (neighbor).
978  * @param message Message.
979  *
980  * @return GNUNET_OK to keep the connection open,
981  *         GNUNET_SYSERR to close it (signal serious error)
982  */
983 int
984 GMC_handle_create (void *cls, const struct GNUNET_PeerIdentity *peer,
985                    const struct GNUNET_MessageHeader *message)
986 {
987   struct GNUNET_MESH_ConnectionCreate *msg;
988   struct GNUNET_PeerIdentity *id;
989   struct GNUNET_HashCode *cid;
990   struct MeshPeerPath *path;
991   struct MeshPeer *dest_peer;
992   struct MeshPeer *orig_peer;
993   struct MeshConnection *c;
994   unsigned int own_pos;
995   uint16_t size;
996   uint16_t i;
997
998   LOG (GNUNET_ERROR_TYPE_DEBUG, "\n\n");
999   LOG (GNUNET_ERROR_TYPE_DEBUG, "Received a connection create msg\n");
1000
1001   /* Check size */
1002   size = ntohs (message->size);
1003   if (size < sizeof (struct GNUNET_MESH_ConnectionCreate))
1004   {
1005     GNUNET_break_op (0);
1006     return GNUNET_OK;
1007   }
1008
1009   /* Calculate hops */
1010   size -= sizeof (struct GNUNET_MESH_ConnectionCreate);
1011   if (size % sizeof (struct GNUNET_PeerIdentity))
1012   {
1013     GNUNET_break_op (0);
1014     return GNUNET_OK;
1015   }
1016   size /= sizeof (struct GNUNET_PeerIdentity);
1017   if (1 > size)
1018   {
1019     GNUNET_break_op (0);
1020     return GNUNET_OK;
1021   }
1022   LOG (GNUNET_ERROR_TYPE_DEBUG, "    path has %u hops.\n", size);
1023
1024   /* Get parameters */
1025   msg = (struct GNUNET_MESH_ConnectionCreate *) message;
1026   cid = &msg->cid;
1027   id = (struct GNUNET_PeerIdentity *) &msg[1];
1028   LOG (GNUNET_ERROR_TYPE_DEBUG,
1029               "    connection %s (%s).\n",
1030               GNUNET_h2s (cid), GNUNET_i2s (id));
1031
1032   /* Create connection */
1033   c = connection_get (cid);
1034   if (NULL == c)
1035   {
1036     LOG (GNUNET_ERROR_TYPE_DEBUG, "  Creating connection\n");
1037     c = connection_new (cid);
1038     if (NULL == c)
1039       return GNUNET_OK;
1040     connection_reset_timeout (c, GNUNET_YES);
1041
1042     /* Create path */
1043     LOG (GNUNET_ERROR_TYPE_DEBUG, "  Creating path...\n");
1044     path = path_new (size);
1045     own_pos = 0;
1046     for (i = 0; i < size; i++)
1047     {
1048       LOG (GNUNET_ERROR_TYPE_DEBUG, "  ... adding %s\n",
1049                   GNUNET_i2s (&id[i]));
1050       path->peers[i] = GNUNET_PEER_intern (&id[i]);
1051       if (path->peers[i] == myid)
1052         own_pos = i;
1053     }
1054     if (own_pos == 0 && path->peers[own_pos] != myid)
1055     {
1056       /* create path: self not found in path through self */
1057       GNUNET_break_op (0);
1058       path_destroy (path);
1059       connection_destroy (c);
1060       return GNUNET_OK;
1061     }
1062     LOG (GNUNET_ERROR_TYPE_DEBUG, "  Own position: %u\n", own_pos);
1063     path_add_to_peers (path, GNUNET_NO);
1064     c->path = path_duplicate (path);
1065     c->own_pos = own_pos;
1066   }
1067   else
1068   {
1069     path = NULL;
1070   }
1071   if (MESH_CONNECTION_NEW == c->state)
1072     connection_change_state (c, MESH_CONNECTION_SENT);
1073
1074   /* Remember peers */
1075   dest_peer = peer_get (&id[size - 1]);
1076   orig_peer = peer_get (&id[0]);
1077
1078   /* Is it a connection to us? */
1079   if (c->own_pos == size - 1)
1080   {
1081     LOG (GNUNET_ERROR_TYPE_DEBUG, "  It's for us!\n");
1082     peer_add_path_to_origin (orig_peer, path, GNUNET_YES);
1083
1084     if (NULL == orig_peer->tunnel)
1085     {
1086       orig_peer->tunnel = tunnel_new ();
1087       orig_peer->tunnel->peer = orig_peer;
1088     }
1089     tunnel_add_connection (orig_peer->tunnel, c);
1090     if (MESH_TUNNEL_NEW == c->t->state)
1091       tunnel_change_state (c->t,  MESH_TUNNEL_WAITING);
1092
1093     send_connection_ack (c, GNUNET_NO);
1094     if (MESH_CONNECTION_SENT == c->state)
1095       connection_change_state (c, MESH_CONNECTION_ACK);
1096
1097     /* Keep tunnel alive in direction dest->owner*/
1098     connection_reset_timeout (c, GNUNET_NO);
1099   }
1100   else
1101   {
1102     /* It's for somebody else! Retransmit. */
1103     LOG (GNUNET_ERROR_TYPE_DEBUG, "  Retransmitting.\n");
1104     peer_add_path (dest_peer, path_duplicate (path), GNUNET_NO);
1105     peer_add_path_to_origin (orig_peer, path, GNUNET_NO);
1106     send_prebuilt_message_connection (message, c, NULL, GNUNET_YES);
1107   }
1108   return GNUNET_OK;
1109 }
1110
1111
1112 /**
1113  * Core handler for path confirmations.
1114  *
1115  * @param cls closure
1116  * @param message message
1117  * @param peer peer identity this notification is about
1118  *
1119  * @return GNUNET_OK to keep the connection open,
1120  *         GNUNET_SYSERR to close it (signal serious error)
1121  */
1122 int
1123 GMC_handle_confirm (void *cls, const struct GNUNET_PeerIdentity *peer,
1124                     const struct GNUNET_MessageHeader *message)
1125 {
1126   struct GNUNET_MESH_ConnectionACK *msg;
1127   struct MeshConnection *c;
1128   struct MeshPeerPath *p;
1129   struct MeshPeer *pi;
1130   int fwd;
1131
1132   LOG (GNUNET_ERROR_TYPE_DEBUG, "\n\n");
1133   LOG (GNUNET_ERROR_TYPE_DEBUG, "Received a connection ACK msg\n");
1134   msg = (struct GNUNET_MESH_ConnectionACK *) message;
1135   LOG (GNUNET_ERROR_TYPE_DEBUG, "  on connection %s\n",
1136               GNUNET_h2s (&msg->cid));
1137   c = connection_get (&msg->cid);
1138   if (NULL == c)
1139   {
1140     GNUNET_STATISTICS_update (stats, "# control on unknown connection",
1141                               1, GNUNET_NO);
1142     LOG (GNUNET_ERROR_TYPE_DEBUG, "  don't know the connection!\n");
1143     return GNUNET_OK;
1144   }
1145
1146
1147   LOG (GNUNET_ERROR_TYPE_DEBUG, "  via peer %s\n",
1148               GNUNET_i2s (peer));
1149   pi = peer_get (peer);
1150   if (connection_get_next_hop (c) == pi)
1151   {
1152     LOG (GNUNET_ERROR_TYPE_DEBUG, "  SYNACK\n");
1153     fwd = GNUNET_NO;
1154     if (MESH_CONNECTION_SENT == c->state)
1155       connection_change_state (c, MESH_CONNECTION_ACK);
1156   }
1157   else if (connection_get_prev_hop (c) == pi)
1158   {
1159     LOG (GNUNET_ERROR_TYPE_DEBUG, "  ACK\n");
1160     fwd = GNUNET_YES;
1161     connection_change_state (c, MESH_CONNECTION_READY);
1162   }
1163   else
1164   {
1165     GNUNET_break_op (0);
1166     return GNUNET_OK;
1167   }
1168   connection_reset_timeout (c, fwd);
1169
1170   /* Add path to peers? */
1171   p = c->path;
1172   if (NULL != p)
1173   {
1174     path_add_to_peers (p, GNUNET_YES);
1175   }
1176   else
1177   {
1178     GNUNET_break (0);
1179   }
1180
1181   /* Message for us as creator? */
1182   if (connection_is_origin (c, GNUNET_YES))
1183   {
1184     LOG (GNUNET_ERROR_TYPE_DEBUG, "  Connection (SYN)ACK for us!\n");
1185     connection_change_state (c, MESH_CONNECTION_READY);
1186     if (MESH_TUNNEL_READY != c->t->state)
1187       tunnel_change_state (c->t, MESH_TUNNEL_READY);
1188     send_connection_ack (c, GNUNET_YES);
1189     tunnel_send_queued_data (c->t, GNUNET_YES);
1190     if (3 <= tunnel_count_connections (c->t) && NULL != c->t->peer->dhtget)
1191     {
1192       GNUNET_DHT_get_stop (c->t->peer->dhtget);
1193       c->t->peer->dhtget = NULL;
1194     }
1195     return GNUNET_OK;
1196   }
1197
1198   /* Message for us as destination? */
1199   if (GMC_is_terminal (c, GNUNET_YES))
1200   {
1201     LOG (GNUNET_ERROR_TYPE_DEBUG, "  Connection ACK for us!\n");
1202     if (MESH_TUNNEL_READY != c->t->state)
1203       tunnel_change_state (c->t, MESH_TUNNEL_READY);
1204     connection_change_state (c, MESH_CONNECTION_READY);
1205     tunnel_send_queued_data (c->t, GNUNET_NO);
1206     return GNUNET_OK;
1207   }
1208
1209   LOG (GNUNET_ERROR_TYPE_DEBUG, "  not for us, retransmitting...\n");
1210   send_prebuilt_message_connection (message, c, NULL, fwd);
1211   return GNUNET_OK;
1212 }
1213
1214
1215 /**
1216  * Core handler for notifications of broken paths
1217  *
1218  * @param cls Closure (unused).
1219  * @param peer Peer identity of sending neighbor.
1220  * @param message Message.
1221  *
1222  * @return GNUNET_OK to keep the connection open,
1223  *         GNUNET_SYSERR to close it (signal serious error)
1224  */
1225 int
1226 GMC_handle_broken (void *cls, const struct GNUNET_PeerIdentity *peer,
1227                    const struct GNUNET_MessageHeader *message)
1228 {
1229   struct GNUNET_MESH_ConnectionBroken *msg;
1230   struct MeshConnection *c;
1231
1232   LOG (GNUNET_ERROR_TYPE_DEBUG,
1233               "Received a CONNECTION BROKEN msg from %s\n", GNUNET_i2s (peer));
1234   msg = (struct GNUNET_MESH_ConnectionBroken *) message;
1235   LOG (GNUNET_ERROR_TYPE_DEBUG, "  regarding %s\n",
1236               GNUNET_i2s (&msg->peer1));
1237   LOG (GNUNET_ERROR_TYPE_DEBUG, "  regarding %s\n",
1238               GNUNET_i2s (&msg->peer2));
1239   c = connection_get (&msg->cid);
1240   if (NULL == c)
1241   {
1242     GNUNET_break_op (0);
1243     return GNUNET_OK;
1244   }
1245   tunnel_notify_connection_broken (c->t, GNUNET_PEER_search (&msg->peer1),
1246                                    GNUNET_PEER_search (&msg->peer2));
1247   return GNUNET_OK;
1248
1249 }
1250
1251
1252 /**
1253  * Core handler for tunnel destruction
1254  *
1255  * @param cls Closure (unused).
1256  * @param peer Peer identity of sending neighbor.
1257  * @param message Message.
1258  *
1259  * @return GNUNET_OK to keep the connection open,
1260  *         GNUNET_SYSERR to close it (signal serious error)
1261  */
1262 int
1263 GMC_handle_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
1264                     const struct GNUNET_MessageHeader *message)
1265 {
1266   struct GNUNET_MESH_ConnectionDestroy *msg;
1267   struct MeshConnection *c;
1268   GNUNET_PEER_Id id;
1269   int fwd;
1270
1271   msg = (struct GNUNET_MESH_ConnectionDestroy *) message;
1272   LOG (GNUNET_ERROR_TYPE_DEBUG,
1273               "Got a CONNECTION DESTROY message from %s\n",
1274               GNUNET_i2s (peer));
1275   LOG (GNUNET_ERROR_TYPE_DEBUG,
1276               "  for connection %s\n",
1277               GNUNET_h2s (&msg->cid));
1278   c = connection_get (&msg->cid);
1279   if (NULL == c)
1280   {
1281     /* Probably already got the message from another path,
1282      * destroyed the tunnel and retransmitted to children.
1283      * Safe to ignore.
1284      */
1285     GNUNET_STATISTICS_update (stats, "# control on unknown tunnel",
1286                               1, GNUNET_NO);
1287     return GNUNET_OK;
1288   }
1289   id = GNUNET_PEER_search (peer);
1290   if (id == connection_get_prev_hop (c)->id)
1291     fwd = GNUNET_YES;
1292   else if (id == connection_get_next_hop (c)->id)
1293     fwd = GNUNET_NO;
1294   else
1295   {
1296     GNUNET_break_op (0);
1297     return GNUNET_OK;
1298   }
1299   send_prebuilt_message_connection (message, c, NULL, fwd);
1300   c->destroy = GNUNET_YES;
1301
1302   return GNUNET_OK;
1303 }
1304
1305 /**
1306  * Generic handler for mesh network encrypted traffic.
1307  *
1308  * @param peer Peer identity this notification is about.
1309  * @param message Encrypted message.
1310  * @param fwd Is this FWD traffic? GNUNET_YES : GNUNET_NO;
1311  *
1312  * @return GNUNET_OK to keep the connection open,
1313  *         GNUNET_SYSERR to close it (signal serious error)
1314  */
1315 static int
1316 handle_mesh_encrypted (const struct GNUNET_PeerIdentity *peer,
1317                        const struct GNUNET_MESH_Encrypted *msg,
1318                        int fwd)
1319 {
1320   struct MeshConnection *c;
1321   struct MeshTunnel3 *t;
1322   struct MeshPeer *neighbor;
1323   struct MeshFlowControl *fc;
1324   uint32_t pid;
1325   uint32_t ttl;
1326   uint16_t type;
1327   size_t size;
1328
1329   /* Check size */
1330   size = ntohs (msg->header.size);
1331   if (size <
1332       sizeof (struct GNUNET_MESH_Encrypted) +
1333       sizeof (struct GNUNET_MessageHeader))
1334   {
1335     GNUNET_break_op (0);
1336     return GNUNET_OK;
1337   }
1338   type = ntohs (msg->header.type);
1339   LOG (GNUNET_ERROR_TYPE_DEBUG, "\n\n");
1340   LOG (GNUNET_ERROR_TYPE_DEBUG, "got a %s message from %s\n",
1341               GNUNET_MESH_DEBUG_M2S (type), GNUNET_i2s (peer));
1342
1343   /* Check connection */
1344   c = connection_get (&msg->cid);
1345   if (NULL == c)
1346   {
1347     GNUNET_STATISTICS_update (stats, "# unknown connection", 1, GNUNET_NO);
1348     LOG (GNUNET_ERROR_TYPE_DEBUG, "WARNING connection unknown\n");
1349     return GNUNET_OK;
1350   }
1351   t = c->t;
1352   fc = fwd ? &c->bck_fc : &c->fwd_fc;
1353
1354   /* Check if origin is as expected */
1355   neighbor = connection_get_hop (c, !fwd);
1356   if (peer_get (peer)->id != neighbor->id)
1357   {
1358     GNUNET_break_op (0);
1359     return GNUNET_OK;
1360   }
1361
1362   /* Check PID */
1363   pid = ntohl (msg->pid);
1364   if (GMC_is_pid_bigger (pid, fc->last_ack_sent))
1365   {
1366     GNUNET_STATISTICS_update (stats, "# unsolicited message", 1, GNUNET_NO);
1367     LOG (GNUNET_ERROR_TYPE_DEBUG,
1368                 "WARNING Received PID %u, (prev %u), ACK %u\n",
1369                 pid, fc->last_pid_recv, fc->last_ack_sent);
1370     return GNUNET_OK;
1371   }
1372   if (GNUNET_NO == GMC_is_pid_bigger (pid, fc->last_pid_recv))
1373   {
1374     GNUNET_STATISTICS_update (stats, "# duplicate PID", 1, GNUNET_NO);
1375     LOG (GNUNET_ERROR_TYPE_DEBUG,
1376                 " Pid %u not expected (%u+), dropping!\n",
1377                 pid, fc->last_pid_recv + 1);
1378     return GNUNET_OK;
1379   }
1380   if (MESH_CONNECTION_SENT == c->state)
1381     connection_change_state (c, MESH_CONNECTION_READY);
1382   connection_reset_timeout (c, fwd);
1383   fc->last_pid_recv = pid;
1384
1385   /* Is this message for us? */
1386   if (GMC_is_terminal (c, fwd))
1387   {
1388     size_t dsize = size - sizeof (struct GNUNET_MESH_Encrypted);
1389     char cbuf[dsize];
1390     struct GNUNET_MessageHeader *msgh;
1391     unsigned int off;
1392
1393     /* TODO signature verification */
1394     LOG (GNUNET_ERROR_TYPE_DEBUG, "  message for us!\n");
1395     GNUNET_STATISTICS_update (stats, "# messages received", 1, GNUNET_NO);
1396
1397     fc->last_pid_recv = pid;
1398     tunnel_decrypt (t, cbuf, &msg[1], dsize, msg->iv, fwd);
1399     off = 0;
1400     while (off < dsize)
1401     {
1402       msgh = (struct GNUNET_MessageHeader *) &cbuf[off];
1403       handle_decrypted (t, msgh, fwd);
1404       off += ntohs (msgh->size);
1405     }
1406     send_ack (c, NULL, fwd);
1407     return GNUNET_OK;
1408   }
1409
1410   /* Message not for us: forward to next hop */
1411   LOG (GNUNET_ERROR_TYPE_DEBUG, "  not for us, retransmitting...\n");
1412   ttl = ntohl (msg->ttl);
1413   LOG (GNUNET_ERROR_TYPE_DEBUG, "   ttl: %u\n", ttl);
1414   if (ttl == 0)
1415   {
1416     GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
1417     LOG (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
1418     send_ack (c, NULL, fwd);
1419     return GNUNET_OK;
1420   }
1421   GNUNET_STATISTICS_update (stats, "# messages forwarded", 1, GNUNET_NO);
1422
1423   send_prebuilt_message_connection (&msg->header, c, NULL, fwd);
1424
1425   return GNUNET_OK;
1426 }
1427
1428
1429 /**
1430  * Core handler for mesh network traffic going orig->dest.
1431  *
1432  * @param cls Closure (unused).
1433  * @param message Message received.
1434  * @param peer Peer who sent the message.
1435  *
1436  * @return GNUNET_OK to keep the connection open,
1437  *         GNUNET_SYSERR to close it (signal serious error)
1438  */
1439 int
1440 GMC_handle_fwd (void *cls, const struct GNUNET_PeerIdentity *peer,
1441                 const struct GNUNET_MessageHeader *message)
1442 {
1443   return handle_mesh_encrypted (peer,
1444                                 (struct GNUNET_MESH_Encrypted *)message,
1445                                 GNUNET_YES);
1446 }
1447
1448 /**
1449  * Core handler for mesh network traffic going dest->orig.
1450  *
1451  * @param cls Closure (unused).
1452  * @param message Message received.
1453  * @param peer Peer who sent the message.
1454  *
1455  * @return GNUNET_OK to keep the connection open,
1456  *         GNUNET_SYSERR to close it (signal serious error)
1457  */
1458 int
1459 GMC_handle_bck (void *cls, const struct GNUNET_PeerIdentity *peer,
1460                 const struct GNUNET_MessageHeader *message)
1461 {
1462   return handle_mesh_encrypted (peer,
1463                                 (struct GNUNET_MESH_Encrypted *)message,
1464                                 GNUNET_NO);
1465 }
1466
1467
1468 /**
1469  * Core handler for mesh network traffic point-to-point acks.
1470  *
1471  * @param cls closure
1472  * @param message message
1473  * @param peer peer identity this notification is about
1474  *
1475  * @return GNUNET_OK to keep the connection open,
1476  *         GNUNET_SYSERR to close it (signal serious error)
1477  */
1478 int
1479 GMC_handle_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
1480                 const struct GNUNET_MessageHeader *message)
1481 {
1482   struct GNUNET_MESH_ACK *msg;
1483   struct MeshConnection *c;
1484   struct MeshFlowControl *fc;
1485   GNUNET_PEER_Id id;
1486   uint32_t ack;
1487   int fwd;
1488
1489   LOG (GNUNET_ERROR_TYPE_DEBUG, "\n\n");
1490   LOG (GNUNET_ERROR_TYPE_DEBUG, "Got an ACK packet from %s!\n",
1491               GNUNET_i2s (peer));
1492   msg = (struct GNUNET_MESH_ACK *) message;
1493
1494   c = connection_get (&msg->cid);
1495
1496   if (NULL == c)
1497   {
1498     GNUNET_STATISTICS_update (stats, "# ack on unknown connection", 1,
1499                               GNUNET_NO);
1500     return GNUNET_OK;
1501   }
1502
1503   /* Is this a forward or backward ACK? */
1504   id = GNUNET_PEER_search (peer);
1505   if (connection_get_next_hop (c)->id == id)
1506   {
1507     LOG (GNUNET_ERROR_TYPE_DEBUG, "  FWD ACK\n");
1508     fc = &c->fwd_fc;
1509     fwd = GNUNET_YES;
1510   }
1511   else if (connection_get_prev_hop (c)->id == id)
1512   {
1513     LOG (GNUNET_ERROR_TYPE_DEBUG, "  BCK ACK\n");
1514     fc = &c->bck_fc;
1515     fwd = GNUNET_NO;
1516   }
1517   else
1518   {
1519     GNUNET_break_op (0);
1520     return GNUNET_OK;
1521   }
1522
1523   ack = ntohl (msg->ack);
1524   LOG (GNUNET_ERROR_TYPE_DEBUG, "  ACK %u (was %u)\n",
1525               ack, fc->last_ack_recv);
1526   if (GMC_is_pid_bigger (ack, fc->last_ack_recv))
1527     fc->last_ack_recv = ack;
1528
1529   /* Cancel polling if the ACK is big enough. */
1530   if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task &&
1531       GMC_is_pid_bigger (fc->last_ack_recv, fc->last_pid_sent))
1532   {
1533     LOG (GNUNET_ERROR_TYPE_DEBUG, "  Cancel poll\n");
1534     GNUNET_SCHEDULER_cancel (fc->poll_task);
1535     fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
1536     fc->poll_time = GNUNET_TIME_UNIT_SECONDS;
1537   }
1538
1539   connection_unlock_queue (c, fwd);
1540
1541   return GNUNET_OK;
1542 }
1543
1544
1545 /**
1546  * Core handler for mesh network traffic point-to-point ack polls.
1547  *
1548  * @param cls closure
1549  * @param message message
1550  * @param peer peer identity this notification is about
1551  *
1552  * @return GNUNET_OK to keep the connection open,
1553  *         GNUNET_SYSERR to close it (signal serious error)
1554  */
1555 int
1556 GMC_handle_poll (void *cls, const struct GNUNET_PeerIdentity *peer,
1557                  const struct GNUNET_MessageHeader *message)
1558 {
1559   struct GNUNET_MESH_Poll *msg;
1560   struct MeshConnection *c;
1561   struct MeshFlowControl *fc;
1562   GNUNET_PEER_Id id;
1563   uint32_t pid;
1564   int fwd;
1565
1566   LOG (GNUNET_ERROR_TYPE_DEBUG, "\n\n");
1567   LOG (GNUNET_ERROR_TYPE_DEBUG, "Got a POLL packet from %s!\n",
1568               GNUNET_i2s (peer));
1569
1570   msg = (struct GNUNET_MESH_Poll *) message;
1571
1572   c = connection_get (&msg->cid);
1573
1574   if (NULL == c)
1575   {
1576     GNUNET_STATISTICS_update (stats, "# poll on unknown connection", 1,
1577                               GNUNET_NO);
1578     GNUNET_break_op (0);
1579     return GNUNET_OK;
1580   }
1581
1582   /* Is this a forward or backward ACK?
1583    * Note: a poll should never be needed in a loopback case,
1584    * since there is no possiblility of packet loss there, so
1585    * this way of discerining FWD/BCK should not be a problem.
1586    */
1587   id = GNUNET_PEER_search (peer);
1588   if (connection_get_next_hop (c)->id == id)
1589   {
1590     LOG (GNUNET_ERROR_TYPE_DEBUG, "  FWD ACK\n");
1591     fc = &c->fwd_fc;
1592   }
1593   else if (connection_get_prev_hop (c)->id == id)
1594   {
1595     LOG (GNUNET_ERROR_TYPE_DEBUG, "  BCK ACK\n");
1596     fc = &c->bck_fc;
1597   }
1598   else
1599   {
1600     GNUNET_break_op (0);
1601     return GNUNET_OK;
1602   }
1603
1604   pid = ntohl (msg->pid);
1605   LOG (GNUNET_ERROR_TYPE_DEBUG, "  PID %u, OLD %u\n",
1606               pid, fc->last_pid_recv);
1607   fc->last_pid_recv = pid;
1608   fwd = fc == &c->fwd_fc;
1609   send_ack (c, NULL, fwd);
1610
1611   return GNUNET_OK;
1612 }
1613
1614
1615 /**
1616  * Core handler for mesh keepalives.
1617  *
1618  * @param cls closure
1619  * @param message message
1620  * @param peer peer identity this notification is about
1621  * @return GNUNET_OK to keep the connection open,
1622  *         GNUNET_SYSERR to close it (signal serious error)
1623  *
1624  * TODO: Check who we got this from, to validate route.
1625  */
1626 int
1627 GMC_handle_keepalive (void *cls, const struct GNUNET_PeerIdentity *peer,
1628                     const struct GNUNET_MessageHeader *message)
1629 {
1630   struct GNUNET_MESH_ConnectionKeepAlive *msg;
1631   struct MeshConnection *c;
1632   struct MeshPeer *neighbor;
1633   int fwd;
1634
1635   msg = (struct GNUNET_MESH_ConnectionKeepAlive *) message;
1636   LOG (GNUNET_ERROR_TYPE_DEBUG, "got a keepalive packet from %s\n",
1637               GNUNET_i2s (peer));
1638
1639   c = connection_get (&msg->cid);
1640   if (NULL == c)
1641   {
1642     GNUNET_STATISTICS_update (stats, "# keepalive on unknown connection", 1,
1643                               GNUNET_NO);
1644     return GNUNET_OK;
1645   }
1646
1647   fwd = GNUNET_MESSAGE_TYPE_MESH_FWD_KEEPALIVE == ntohs (message->type) ?
1648         GNUNET_YES : GNUNET_NO;
1649
1650   /* Check if origin is as expected */
1651   neighbor = connection_get_hop (c, fwd);
1652   if (peer_get (peer)->id != neighbor->id)
1653   {
1654     GNUNET_break_op (0);
1655     return GNUNET_OK;
1656   }
1657
1658   connection_change_state (c, MESH_CONNECTION_READY);
1659   connection_reset_timeout (c, fwd);
1660
1661   if (GMC_is_terminal (c, fwd))
1662     return GNUNET_OK;
1663
1664   GNUNET_STATISTICS_update (stats, "# keepalives forwarded", 1, GNUNET_NO);
1665   GMC_send_prebuilt_message (message, c, NULL, fwd);
1666
1667   return GNUNET_OK;
1668 }
1669
1670
1671 /**
1672  * Send an ACK on the appropriate connection/channel, depending on
1673  * the direction and the position of the peer.
1674  *
1675  * @param c Which connection to send the hop-by-hop ACK.
1676  * @param ch Channel, if any.
1677  * @param fwd Is this a fwd ACK? (will go dest->root)
1678  */
1679 static void
1680 send_ack (struct MeshConnection *c, struct MeshChannel *ch, int fwd)
1681 {
1682   unsigned int buffer;
1683
1684   LOG (GNUNET_ERROR_TYPE_DEBUG,
1685               "send ack %s on %p %p\n",
1686               fwd ? "FWD" : "BCK", c, ch);
1687   if (NULL == c || GMC_is_terminal (c, fwd))
1688   {
1689     LOG (GNUNET_ERROR_TYPE_DEBUG, "  getting from all connections\n");
1690     buffer = GMT_get_buffer (NULL == c ? ch->t : c->t, fwd);
1691   }
1692   else
1693   {
1694     LOG (GNUNET_ERROR_TYPE_DEBUG, "  getting from one connection\n");
1695     buffer = GMC_get_buffer (c, fwd);
1696   }
1697   LOG (GNUNET_ERROR_TYPE_DEBUG, "  buffer available: %u\n", buffer);
1698
1699   if ( (NULL != ch && GMCH_is_origin (ch, fwd)) ||
1700        (NULL != c && GMC_is_origin (c, fwd)) )
1701   {
1702     LOG (GNUNET_ERROR_TYPE_DEBUG, "  sending on channel...\n");
1703     if (0 < buffer)
1704     {
1705       GNUNET_assert (NULL != ch);
1706       LOG (GNUNET_ERROR_TYPE_DEBUG, "  really sending!\n");
1707       send_local_ack (ch, fwd);
1708     }
1709   }
1710   else if (NULL == c)
1711   {
1712     LOG (GNUNET_ERROR_TYPE_DEBUG, "  sending on all connections\n");
1713     GNUNET_assert (NULL != ch);
1714     channel_send_connections_ack (ch, buffer, fwd);
1715   }
1716   else
1717   {
1718     LOG (GNUNET_ERROR_TYPE_DEBUG, "  sending on connection\n");
1719     connection_send_ack (c, buffer, fwd);
1720   }
1721 }
1722
1723
1724
1725 /**
1726  * Initialize the connections subsystem
1727  *
1728  * @param c Configuration handle.
1729  */
1730 void
1731 GMC_init (const struct GNUNET_CONFIGURATION_Handle *c)
1732 {
1733   if (GNUNET_OK !=
1734       GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_MSGS_QUEUE",
1735                                              &max_msgs_queue))
1736   {
1737     LOG_config_invalid (GNUNET_ERROR_TYPE_ERROR,
1738                                "MESH", "MAX_MSGS_QUEUE", "MISSING");
1739     GNUNET_SCHEDULER_shutdown ();
1740     return;
1741   }
1742
1743   if (GNUNET_OK !=
1744       GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_CONNECTIONS",
1745                                              &max_connections))
1746   {
1747     LOG_config_invalid (GNUNET_ERROR_TYPE_ERROR,
1748                                "MESH", "MAX_CONNECTIONS", "MISSING");
1749     GNUNET_SCHEDULER_shutdown ();
1750     return;
1751   }
1752
1753   if (GNUNET_OK !=
1754       GNUNET_CONFIGURATION_get_value_time (c, "MESH", "REFRESH_CONNECTION_TIME",
1755                                            &refresh_connection_time))
1756   {
1757     LOG_config_invalid (GNUNET_ERROR_TYPE_ERROR,
1758                                "MESH", "REFRESH_CONNECTION_TIME", "MISSING");
1759     GNUNET_SCHEDULER_shutdown ();
1760     return;
1761   }
1762   connections = GNUNET_CONTAINER_multihashmap_create (1024, GNUNET_YES);
1763 }
1764
1765 /**
1766  * Shut down the connections subsystem.
1767  */
1768 void
1769 GMC_shutdown (void)
1770 {
1771 }
1772
1773
1774 struct MeshConnection *
1775 GMC_new (const struct GNUNET_HashCode *cid,
1776          struct MeshTunnel3 *t,
1777          struct MeshPeerPath *p,
1778          unsigned int own_pos)
1779 {
1780   struct MeshConnection *c;
1781
1782   c = GNUNET_new (struct MeshConnection);
1783   c->id = *cid;
1784   GNUNET_CONTAINER_multihashmap_put (connections, &c->id, c,
1785                                      GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
1786   fc_init (&c->fwd_fc);
1787   fc_init (&c->bck_fc);
1788   c->fwd_fc.c = c;
1789   c->bck_fc.c = c;
1790
1791   c->t = t;
1792   if (own_pos > p->length - 1)
1793   {
1794     GNUNET_break (0);
1795     GMC_destroy (c);
1796     return NULL;
1797   }
1798   c->own_pos = own_pos;
1799   c->path = p;
1800
1801   if (0 == own_pos)
1802   {
1803     c->fwd_maintenance_task =
1804             GNUNET_SCHEDULER_add_delayed (refresh_connection_time,
1805                                           &connection_fwd_keepalive, c);
1806   }
1807   register_neighbors (c);
1808   return c;
1809 }
1810
1811
1812 void
1813 GMC_destroy (struct MeshConnection *c)
1814 {
1815   struct MeshPeer *peer;
1816
1817   if (NULL == c)
1818     return;
1819
1820   LOG (GNUNET_ERROR_TYPE_DEBUG, "destroying connection %s\n",
1821        GNUNET_h2s (&c->id));
1822
1823   /* Cancel all traffic */
1824   connection_cancel_queues (c, GNUNET_YES);
1825   connection_cancel_queues (c, GNUNET_NO);
1826
1827   /* Cancel maintainance task (keepalive/timeout) */
1828   if (GNUNET_SCHEDULER_NO_TASK != c->fwd_maintenance_task)
1829     GNUNET_SCHEDULER_cancel (c->fwd_maintenance_task);
1830   if (GNUNET_SCHEDULER_NO_TASK != c->bck_maintenance_task)
1831     GNUNET_SCHEDULER_cancel (c->bck_maintenance_task);
1832
1833   /* Unregister from neighbors */
1834   unregister_neighbors (c);
1835
1836   /* Delete */
1837   GNUNET_STATISTICS_update (stats, "# connections", -1, GNUNET_NO);
1838   GMT_remove_connection (c->t, c);
1839   GNUNET_free (c);
1840 }
1841
1842 /**
1843  * Get the connection ID.
1844  *
1845  * @param c Connection to get the ID from.
1846  *
1847  * @return ID of the connection.
1848  */
1849 const struct GNUNET_HashCode *
1850 GMC_get_id (const struct MeshConnection *c)
1851 {
1852   return &c->id;
1853 }
1854
1855
1856 /**
1857  * Get the connection state.
1858  *
1859  * @param c Connection to get the state from.
1860  *
1861  * @return state of the connection.
1862  */
1863 enum MeshConnectionState
1864 GMC_get_state (const struct MeshConnection *c)
1865 {
1866   return c->state;
1867 }
1868
1869
1870 /**
1871  * Get free buffer space in a connection.
1872  *
1873  * @param c Connection.
1874  * @param fwd Is query about FWD traffic?
1875  *
1876  * @return Free buffer space [0 - max_msgs_queue/max_connections]
1877  */
1878 unsigned int
1879 GMC_get_buffer (struct MeshConnection *c, int fwd)
1880 {
1881   struct MeshFlowControl *fc;
1882
1883   fc = fwd ? &c->fwd_fc : &c->bck_fc;
1884
1885   return (fc->queue_max - fc->queue_n);
1886 }
1887
1888 /**
1889  * Get messages queued in a connection.
1890  *
1891  * @param c Connection.
1892  * @param fwd Is query about FWD traffic?
1893  *
1894  * @return Number of messages queued.
1895  */
1896 unsigned int
1897 GMC_get_qn (struct MeshConnection *c, int fwd)
1898 {
1899   struct MeshFlowControl *fc;
1900
1901   fc = fwd ? &c->fwd_fc : &c->bck_fc;
1902
1903   return fc->queue_n;
1904 }
1905
1906
1907 /**
1908  * Notify other peers on a connection of a broken link. Mark connections
1909  * to destroy after all traffic has been sent.
1910  *
1911  * @param c Connection on which there has been a disconnection.
1912  * @param peer Peer that disconnected.
1913  * @param my_full_id My ID (to send to other peers).
1914  */
1915 void
1916 GMC_notify_broken (struct MeshConnection *c,
1917                    struct MeshPeer *peer,
1918                    struct GNUNET_PeerIdentity *my_full_id)
1919 {
1920   struct GNUNET_MESH_ConnectionBroken msg;
1921   int fwd;
1922
1923   fwd = peer == connection_get_prev_hop (c);
1924
1925   connection_cancel_queues (c, !fwd);
1926   if (GMC_is_terminal (c, fwd))
1927   {
1928     /* Local shutdown, no one to notify about this. */
1929     GMC_destroy (c);
1930     return;
1931   }
1932
1933   msg.header.size = htons (sizeof (struct GNUNET_MESH_ConnectionBroken));
1934   msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CONNECTION_BROKEN);
1935   msg.cid = c->id;
1936   msg.peer1 = *my_full_id;
1937   msg.peer2 = *GMP_get_id (peer);
1938   GMC_send_prebuilt_message (&msg.header, c, NULL, fwd);
1939   c->destroy = GNUNET_YES;
1940
1941   return;
1942 }
1943
1944
1945 /**
1946  * Is this peer the first one on the connection?
1947  *
1948  * @param c Connection.
1949  * @param fwd Is this about fwd traffic?
1950  *
1951  * @return GNUNET_YES if origin, GNUNET_NO if relay/terminal.
1952  */
1953 int
1954 GMC_is_origin (struct MeshConnection *c, int fwd)
1955 {
1956   if (!fwd && c->path->length - 1 == c->own_pos )
1957     return GNUNET_YES;
1958   if (fwd && 0 == c->own_pos)
1959     return GNUNET_YES;
1960   return GNUNET_NO;
1961 }
1962
1963
1964 /**
1965  * Is this peer the last one on the connection?
1966  *
1967  * @param c Connection.
1968  * @param fwd Is this about fwd traffic?
1969  *            Note that the ROOT is the terminal for BCK traffic!
1970  *
1971  * @return GNUNET_YES if terminal, GNUNET_NO if relay/origin.
1972  */
1973 int
1974 GMC_is_terminal (struct MeshConnection *c, int fwd)
1975 {
1976   return GMC_is_origin (c, !fwd);
1977 }
1978
1979
1980 /**
1981  * Sends an already built message on a connection, properly registering
1982  * all used resources.
1983  *
1984  * @param message Message to send. Function makes a copy of it.
1985  *                If message is not hop-by-hop, decrements TTL of copy.
1986  * @param c Connection on which this message is transmitted.
1987  * @param ch Channel on which this message is transmitted, or NULL.
1988  * @param fwd Is this a fwd message?
1989  */
1990 void
1991 GMC_send_prebuilt_message (const struct GNUNET_MessageHeader *message,
1992                            struct MeshConnection *c,
1993                            struct MeshChannel *ch,
1994                            int fwd)
1995 {
1996   void *data;
1997   size_t size;
1998   uint16_t type;
1999
2000   size = ntohs (message->size);
2001   data = GNUNET_malloc (size);
2002   memcpy (data, message, size);
2003   type = ntohs (message->type);
2004   LOG (GNUNET_ERROR_TYPE_DEBUG, "Send %s (%u) on connection %s\n",
2005               GNUNET_MESH_DEBUG_M2S (type), size, GNUNET_h2s (&c->id));
2006
2007   switch (type)
2008   {
2009     struct GNUNET_MESH_Encrypted *emsg;
2010     struct GNUNET_MESH_ACK       *amsg;
2011     struct GNUNET_MESH_Poll      *pmsg;
2012     struct GNUNET_MESH_ConnectionDestroy *dmsg;
2013     struct GNUNET_MESH_ConnectionBroken  *bmsg;
2014     uint32_t ttl;
2015
2016     case GNUNET_MESSAGE_TYPE_MESH_FWD:
2017     case GNUNET_MESSAGE_TYPE_MESH_BCK:
2018       emsg = (struct GNUNET_MESH_Encrypted *) data;
2019       ttl = ntohl (emsg->ttl);
2020       if (0 == ttl)
2021       {
2022         GNUNET_break_op (0);
2023         return;
2024       }
2025       emsg->cid = c->id;
2026       emsg->ttl = htonl (ttl - 1);
2027       emsg->pid = htonl (fwd ? c->fwd_fc.next_pid++ : c->bck_fc.next_pid++);
2028       LOG (GNUNET_ERROR_TYPE_DEBUG, " pid %u\n", ntohl (emsg->pid));
2029       break;
2030
2031     case GNUNET_MESSAGE_TYPE_MESH_ACK:
2032       amsg = (struct GNUNET_MESH_ACK *) data;
2033       amsg->cid = c->id;
2034       LOG (GNUNET_ERROR_TYPE_DEBUG, " ack %u\n", ntohl (amsg->ack));
2035       break;
2036
2037     case GNUNET_MESSAGE_TYPE_MESH_POLL:
2038       pmsg = (struct GNUNET_MESH_Poll *) data;
2039       pmsg->cid = c->id;
2040       pmsg->pid = htonl (fwd ? c->fwd_fc.last_pid_sent : c->bck_fc.last_pid_sent);
2041       LOG (GNUNET_ERROR_TYPE_DEBUG, " poll %u\n", ntohl (pmsg->pid));
2042       break;
2043
2044     case GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY:
2045       dmsg = (struct GNUNET_MESH_ConnectionDestroy *) data;
2046       dmsg->cid = c->id;
2047       dmsg->reserved = 0;
2048       break;
2049
2050     case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_BROKEN:
2051       bmsg = (struct GNUNET_MESH_ConnectionBroken *) data;
2052       bmsg->cid = c->id;
2053       bmsg->reserved = 0;
2054       break;
2055
2056     case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE:
2057     case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK:
2058       break;
2059
2060     default:
2061       GNUNET_break (0);
2062   }
2063
2064   GMP_queue_add (data,
2065                  type,
2066                  size,
2067                  c,
2068                  ch,
2069                  fwd);
2070 }
2071
2072
2073 /**
2074  * Send a message to all peers in this connection that the connection
2075  * is no longer valid.
2076  *
2077  * If some peer should not receive the message, it should be zero'ed out
2078  * before calling this function.
2079  *
2080  * @param c The connection whose peers to notify.
2081  */
2082 void
2083 GMC_send_destroy (struct MeshConnection *c)
2084 {
2085   struct GNUNET_MESH_ConnectionDestroy msg;
2086
2087   if (GNUNET_YES == c->destroy)
2088     return;
2089
2090   msg.header.size = htons (sizeof (msg));
2091   msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY);;
2092   msg.cid = c->id;
2093   LOG (GNUNET_ERROR_TYPE_DEBUG,
2094               "  sending connection destroy for connection %s\n",
2095               GNUNET_h2s (&c->id));
2096
2097   if (GNUNET_NO == GMC_is_terminal (c, GNUNET_YES))
2098     GMC_send_prebuilt_message (&msg.header, c, NULL, GNUNET_YES);
2099   if (GNUNET_NO == GMC_is_terminal (c, GNUNET_NO))
2100     GMC_send_prebuilt_message (&msg.header, c, NULL, GNUNET_NO);
2101   c->destroy = GNUNET_YES;
2102 }