- fixing channel functions
[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 "gnunet-service-mesh_channel.h"
36 #include "mesh_protocol_enc.h"
37 #include "mesh_path.h"
38
39
40 #define MESH_MAX_POLL_TIME      GNUNET_TIME_relative_multiply (\
41                                   GNUNET_TIME_UNIT_MINUTES,\
42                                   10)
43 #define MESH_RETRANSMIT_TIME    GNUNET_TIME_UNIT_SECONDS
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] == my_short_id)
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       GMC_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     GMT_add_connection (orig_peer->tunnel, c);
1090     if (MESH_TUNNEL_NEW == GMT_get_state (c->t))
1091       GMT_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     GMC_change_state (c, MESH_CONNECTION_READY);
1203     GMT_change_state (c->t, MESH_TUNNEL_READY);
1204     GMT_send_queued_data (c->t, GNUNET_NO);
1205     return GNUNET_OK;
1206   }
1207
1208   LOG (GNUNET_ERROR_TYPE_DEBUG, "  not for us, retransmitting...\n");
1209   GMC_send_prebuilt_message (message, c, NULL, fwd);
1210   return GNUNET_OK;
1211 }
1212
1213
1214 /**
1215  * Core handler for notifications of broken paths
1216  *
1217  * @param cls Closure (unused).
1218  * @param peer Peer identity of sending neighbor.
1219  * @param message Message.
1220  *
1221  * @return GNUNET_OK to keep the connection open,
1222  *         GNUNET_SYSERR to close it (signal serious error)
1223  */
1224 int
1225 GMC_handle_broken (void *cls, const struct GNUNET_PeerIdentity *peer,
1226                    const struct GNUNET_MessageHeader *message)
1227 {
1228   struct GNUNET_MESH_ConnectionBroken *msg;
1229   struct MeshConnection *c;
1230
1231   LOG (GNUNET_ERROR_TYPE_DEBUG,
1232               "Received a CONNECTION BROKEN msg from %s\n", GNUNET_i2s (peer));
1233   msg = (struct GNUNET_MESH_ConnectionBroken *) message;
1234   LOG (GNUNET_ERROR_TYPE_DEBUG, "  regarding %s\n",
1235               GNUNET_i2s (&msg->peer1));
1236   LOG (GNUNET_ERROR_TYPE_DEBUG, "  regarding %s\n",
1237               GNUNET_i2s (&msg->peer2));
1238   c = connection_get (&msg->cid);
1239   if (NULL == c)
1240   {
1241     GNUNET_break_op (0);
1242     return GNUNET_OK;
1243   }
1244   tunnel_notify_connection_broken (c->t, GNUNET_PEER_search (&msg->peer1),
1245                                    GNUNET_PEER_search (&msg->peer2));
1246   return GNUNET_OK;
1247
1248 }
1249
1250
1251 /**
1252  * Core handler for tunnel destruction
1253  *
1254  * @param cls Closure (unused).
1255  * @param peer Peer identity of sending neighbor.
1256  * @param message Message.
1257  *
1258  * @return GNUNET_OK to keep the connection open,
1259  *         GNUNET_SYSERR to close it (signal serious error)
1260  */
1261 int
1262 GMC_handle_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
1263                     const struct GNUNET_MessageHeader *message)
1264 {
1265   struct GNUNET_MESH_ConnectionDestroy *msg;
1266   struct MeshConnection *c;
1267   GNUNET_PEER_Id id;
1268   int fwd;
1269
1270   msg = (struct GNUNET_MESH_ConnectionDestroy *) message;
1271   LOG (GNUNET_ERROR_TYPE_DEBUG,
1272               "Got a CONNECTION DESTROY message from %s\n",
1273               GNUNET_i2s (peer));
1274   LOG (GNUNET_ERROR_TYPE_DEBUG,
1275               "  for connection %s\n",
1276               GNUNET_h2s (&msg->cid));
1277   c = connection_get (&msg->cid);
1278   if (NULL == c)
1279   {
1280     /* Probably already got the message from another path,
1281      * destroyed the tunnel and retransmitted to children.
1282      * Safe to ignore.
1283      */
1284     GNUNET_STATISTICS_update (stats, "# control on unknown tunnel",
1285                               1, GNUNET_NO);
1286     return GNUNET_OK;
1287   }
1288   id = GNUNET_PEER_search (peer);
1289   if (id == GMP_get_short_id (connection_get_prev_hop (c)))
1290     fwd = GNUNET_YES;
1291   else if (id == GMP_get_short_id (connection_get_next_hop (c)))
1292     fwd = GNUNET_NO;
1293   else
1294   {
1295     GNUNET_break_op (0);
1296     return GNUNET_OK;
1297   }
1298   GMC_send_prebuilt_message (message, c, NULL, fwd);
1299   c->destroy = GNUNET_YES;
1300
1301   return GNUNET_OK;
1302 }
1303
1304 /**
1305  * Generic handler for mesh network encrypted traffic.
1306  *
1307  * @param peer Peer identity this notification is about.
1308  * @param message Encrypted message.
1309  * @param fwd Is this FWD traffic? GNUNET_YES : GNUNET_NO;
1310  *
1311  * @return GNUNET_OK to keep the connection open,
1312  *         GNUNET_SYSERR to close it (signal serious error)
1313  */
1314 static int
1315 handle_mesh_encrypted (const struct GNUNET_PeerIdentity *peer,
1316                        const struct GNUNET_MESH_Encrypted *msg,
1317                        int fwd)
1318 {
1319   struct MeshConnection *c;
1320   struct MeshTunnel3 *t;
1321   struct MeshPeer *neighbor;
1322   struct MeshFlowControl *fc;
1323   uint32_t pid;
1324   uint32_t ttl;
1325   uint16_t type;
1326   size_t size;
1327
1328   /* Check size */
1329   size = ntohs (msg->header.size);
1330   if (size <
1331       sizeof (struct GNUNET_MESH_Encrypted) +
1332       sizeof (struct GNUNET_MessageHeader))
1333   {
1334     GNUNET_break_op (0);
1335     return GNUNET_OK;
1336   }
1337   type = ntohs (msg->header.type);
1338   LOG (GNUNET_ERROR_TYPE_DEBUG, "\n\n");
1339   LOG (GNUNET_ERROR_TYPE_DEBUG, "got a %s message from %s\n",
1340               GNUNET_MESH_DEBUG_M2S (type), GNUNET_i2s (peer));
1341
1342   /* Check connection */
1343   c = connection_get (&msg->cid);
1344   if (NULL == c)
1345   {
1346     GNUNET_STATISTICS_update (stats, "# unknown connection", 1, GNUNET_NO);
1347     LOG (GNUNET_ERROR_TYPE_DEBUG, "WARNING connection unknown\n");
1348     return GNUNET_OK;
1349   }
1350   t = c->t;
1351   fc = fwd ? &c->bck_fc : &c->fwd_fc;
1352
1353   /* Check if origin is as expected */
1354   neighbor = connection_get_hop (c, !fwd);
1355   if (GNUNET_PEER_search (peer) != GMP_get_short_id (neighbor))
1356   {
1357     GNUNET_break_op (0);
1358     return GNUNET_OK;
1359   }
1360
1361   /* Check PID */
1362   pid = ntohl (msg->pid);
1363   if (GMC_is_pid_bigger (pid, fc->last_ack_sent))
1364   {
1365     GNUNET_STATISTICS_update (stats, "# unsolicited message", 1, GNUNET_NO);
1366     LOG (GNUNET_ERROR_TYPE_DEBUG,
1367                 "WARNING Received PID %u, (prev %u), ACK %u\n",
1368                 pid, fc->last_pid_recv, fc->last_ack_sent);
1369     return GNUNET_OK;
1370   }
1371   if (GNUNET_NO == GMC_is_pid_bigger (pid, fc->last_pid_recv))
1372   {
1373     GNUNET_STATISTICS_update (stats, "# duplicate PID", 1, GNUNET_NO);
1374     LOG (GNUNET_ERROR_TYPE_DEBUG,
1375                 " Pid %u not expected (%u+), dropping!\n",
1376                 pid, fc->last_pid_recv + 1);
1377     return GNUNET_OK;
1378   }
1379   if (MESH_CONNECTION_SENT == c->state)
1380     connection_change_state (c, MESH_CONNECTION_READY);
1381   connection_reset_timeout (c, fwd);
1382   fc->last_pid_recv = pid;
1383
1384   /* Is this message for us? */
1385   if (GMC_is_terminal (c, fwd))
1386   {
1387     size_t dsize = size - sizeof (struct GNUNET_MESH_Encrypted);
1388     char cbuf[dsize];
1389     struct GNUNET_MessageHeader *msgh;
1390     unsigned int off;
1391
1392     /* TODO signature verification */
1393     LOG (GNUNET_ERROR_TYPE_DEBUG, "  message for us!\n");
1394     GNUNET_STATISTICS_update (stats, "# messages received", 1, GNUNET_NO);
1395
1396     fc->last_pid_recv = pid;
1397     tunnel_decrypt (t, cbuf, &msg[1], dsize, msg->iv, fwd);
1398     off = 0;
1399     while (off < dsize)
1400     {
1401       msgh = (struct GNUNET_MessageHeader *) &cbuf[off];
1402       handle_decrypted (t, msgh, fwd);
1403       off += ntohs (msgh->size);
1404     }
1405     send_ack (c, NULL, fwd);
1406     return GNUNET_OK;
1407   }
1408
1409   /* Message not for us: forward to next hop */
1410   LOG (GNUNET_ERROR_TYPE_DEBUG, "  not for us, retransmitting...\n");
1411   ttl = ntohl (msg->ttl);
1412   LOG (GNUNET_ERROR_TYPE_DEBUG, "   ttl: %u\n", ttl);
1413   if (ttl == 0)
1414   {
1415     GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
1416     LOG (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
1417     send_ack (c, NULL, fwd);
1418     return GNUNET_OK;
1419   }
1420   GNUNET_STATISTICS_update (stats, "# messages forwarded", 1, GNUNET_NO);
1421
1422   send_prebuilt_message_connection (&msg->header, c, NULL, fwd);
1423
1424   return GNUNET_OK;
1425 }
1426
1427
1428 /**
1429  * Core handler for mesh network traffic going orig->dest.
1430  *
1431  * @param cls Closure (unused).
1432  * @param message Message received.
1433  * @param peer Peer who sent the message.
1434  *
1435  * @return GNUNET_OK to keep the connection open,
1436  *         GNUNET_SYSERR to close it (signal serious error)
1437  */
1438 int
1439 GMC_handle_fwd (void *cls, const struct GNUNET_PeerIdentity *peer,
1440                 const struct GNUNET_MessageHeader *message)
1441 {
1442   return handle_mesh_encrypted (peer,
1443                                 (struct GNUNET_MESH_Encrypted *)message,
1444                                 GNUNET_YES);
1445 }
1446
1447 /**
1448  * Core handler for mesh network traffic going dest->orig.
1449  *
1450  * @param cls Closure (unused).
1451  * @param message Message received.
1452  * @param peer Peer who sent the message.
1453  *
1454  * @return GNUNET_OK to keep the connection open,
1455  *         GNUNET_SYSERR to close it (signal serious error)
1456  */
1457 int
1458 GMC_handle_bck (void *cls, const struct GNUNET_PeerIdentity *peer,
1459                 const struct GNUNET_MessageHeader *message)
1460 {
1461   return handle_mesh_encrypted (peer,
1462                                 (struct GNUNET_MESH_Encrypted *)message,
1463                                 GNUNET_NO);
1464 }
1465
1466
1467 /**
1468  * Core handler for mesh network traffic point-to-point acks.
1469  *
1470  * @param cls closure
1471  * @param message message
1472  * @param peer peer identity this notification is about
1473  *
1474  * @return GNUNET_OK to keep the connection open,
1475  *         GNUNET_SYSERR to close it (signal serious error)
1476  */
1477 int
1478 GMC_handle_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
1479                 const struct GNUNET_MessageHeader *message)
1480 {
1481   struct GNUNET_MESH_ACK *msg;
1482   struct MeshConnection *c;
1483   struct MeshFlowControl *fc;
1484   GNUNET_PEER_Id id;
1485   uint32_t ack;
1486   int fwd;
1487
1488   LOG (GNUNET_ERROR_TYPE_DEBUG, "\n\n");
1489   LOG (GNUNET_ERROR_TYPE_DEBUG, "Got an ACK packet from %s!\n",
1490               GNUNET_i2s (peer));
1491   msg = (struct GNUNET_MESH_ACK *) message;
1492
1493   c = connection_get (&msg->cid);
1494
1495   if (NULL == c)
1496   {
1497     GNUNET_STATISTICS_update (stats, "# ack on unknown connection", 1,
1498                               GNUNET_NO);
1499     return GNUNET_OK;
1500   }
1501
1502   /* Is this a forward or backward ACK? */
1503   id = GNUNET_PEER_search (peer);
1504   if (GMP_get_short_id (connection_get_next_hop (c)) == id)
1505   {
1506     LOG (GNUNET_ERROR_TYPE_DEBUG, "  FWD ACK\n");
1507     fc = &c->fwd_fc;
1508     fwd = GNUNET_YES;
1509   }
1510   else if (GMP_get_short_id (connection_get_prev_hop (c)) == id)
1511   {
1512     LOG (GNUNET_ERROR_TYPE_DEBUG, "  BCK ACK\n");
1513     fc = &c->bck_fc;
1514     fwd = GNUNET_NO;
1515   }
1516   else
1517   {
1518     GNUNET_break_op (0);
1519     return GNUNET_OK;
1520   }
1521
1522   ack = ntohl (msg->ack);
1523   LOG (GNUNET_ERROR_TYPE_DEBUG, "  ACK %u (was %u)\n",
1524               ack, fc->last_ack_recv);
1525   if (GMC_is_pid_bigger (ack, fc->last_ack_recv))
1526     fc->last_ack_recv = ack;
1527
1528   /* Cancel polling if the ACK is big enough. */
1529   if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task &&
1530       GMC_is_pid_bigger (fc->last_ack_recv, fc->last_pid_sent))
1531   {
1532     LOG (GNUNET_ERROR_TYPE_DEBUG, "  Cancel poll\n");
1533     GNUNET_SCHEDULER_cancel (fc->poll_task);
1534     fc->poll_task = GNUNET_SCHEDULER_NO_TASK;
1535     fc->poll_time = GNUNET_TIME_UNIT_SECONDS;
1536   }
1537
1538   connection_unlock_queue (c, fwd);
1539
1540   return GNUNET_OK;
1541 }
1542
1543
1544 /**
1545  * Core handler for mesh network traffic point-to-point ack polls.
1546  *
1547  * @param cls closure
1548  * @param message message
1549  * @param peer peer identity this notification is about
1550  *
1551  * @return GNUNET_OK to keep the connection open,
1552  *         GNUNET_SYSERR to close it (signal serious error)
1553  */
1554 int
1555 GMC_handle_poll (void *cls, const struct GNUNET_PeerIdentity *peer,
1556                  const struct GNUNET_MessageHeader *message)
1557 {
1558   struct GNUNET_MESH_Poll *msg;
1559   struct MeshConnection *c;
1560   struct MeshFlowControl *fc;
1561   GNUNET_PEER_Id id;
1562   uint32_t pid;
1563   int fwd;
1564
1565   LOG (GNUNET_ERROR_TYPE_DEBUG, "\n\n");
1566   LOG (GNUNET_ERROR_TYPE_DEBUG, "Got a POLL packet from %s!\n",
1567               GNUNET_i2s (peer));
1568
1569   msg = (struct GNUNET_MESH_Poll *) message;
1570
1571   c = connection_get (&msg->cid);
1572
1573   if (NULL == c)
1574   {
1575     GNUNET_STATISTICS_update (stats, "# poll on unknown connection", 1,
1576                               GNUNET_NO);
1577     GNUNET_break_op (0);
1578     return GNUNET_OK;
1579   }
1580
1581   /* Is this a forward or backward ACK?
1582    * Note: a poll should never be needed in a loopback case,
1583    * since there is no possiblility of packet loss there, so
1584    * this way of discerining FWD/BCK should not be a problem.
1585    */
1586   id = GNUNET_PEER_search (peer);
1587   if (GMP_get_short_id (connection_get_next_hop (c)) == id)
1588   {
1589     LOG (GNUNET_ERROR_TYPE_DEBUG, "  FWD ACK\n");
1590     fc = &c->fwd_fc;
1591   }
1592   else if (GMP_get_short_id (connection_get_prev_hop (c)) == id)
1593   {
1594     LOG (GNUNET_ERROR_TYPE_DEBUG, "  BCK ACK\n");
1595     fc = &c->bck_fc;
1596   }
1597   else
1598   {
1599     GNUNET_break_op (0);
1600     return GNUNET_OK;
1601   }
1602
1603   pid = ntohl (msg->pid);
1604   LOG (GNUNET_ERROR_TYPE_DEBUG, "  PID %u, OLD %u\n",
1605               pid, fc->last_pid_recv);
1606   fc->last_pid_recv = pid;
1607   fwd = fc == &c->fwd_fc;
1608   send_ack (c, NULL, fwd);
1609
1610   return GNUNET_OK;
1611 }
1612
1613
1614 /**
1615  * Core handler for mesh keepalives.
1616  *
1617  * @param cls closure
1618  * @param message message
1619  * @param peer peer identity this notification is about
1620  * @return GNUNET_OK to keep the connection open,
1621  *         GNUNET_SYSERR to close it (signal serious error)
1622  *
1623  * TODO: Check who we got this from, to validate route.
1624  */
1625 int
1626 GMC_handle_keepalive (void *cls, const struct GNUNET_PeerIdentity *peer,
1627                       const struct GNUNET_MessageHeader *message)
1628 {
1629   struct GNUNET_MESH_ConnectionKeepAlive *msg;
1630   struct MeshConnection *c;
1631   struct MeshPeer *neighbor;
1632   int fwd;
1633
1634   msg = (struct GNUNET_MESH_ConnectionKeepAlive *) message;
1635   LOG (GNUNET_ERROR_TYPE_DEBUG, "got a keepalive packet from %s\n",
1636               GNUNET_i2s (peer));
1637
1638   c = connection_get (&msg->cid);
1639   if (NULL == c)
1640   {
1641     GNUNET_STATISTICS_update (stats, "# keepalive on unknown connection", 1,
1642                               GNUNET_NO);
1643     return GNUNET_OK;
1644   }
1645
1646   fwd = GNUNET_MESSAGE_TYPE_MESH_FWD_KEEPALIVE == ntohs (message->type) ?
1647         GNUNET_YES : GNUNET_NO;
1648
1649   /* Check if origin is as expected */
1650   neighbor = connection_get_hop (c, fwd);
1651   if (GNUNET_PEER_search (peer) != GMP_get_short_id (neighbor))
1652   {
1653     GNUNET_break_op (0);
1654     return GNUNET_OK;
1655   }
1656
1657   connection_change_state (c, MESH_CONNECTION_READY);
1658   connection_reset_timeout (c, fwd);
1659
1660   if (GMC_is_terminal (c, fwd))
1661     return GNUNET_OK;
1662
1663   GNUNET_STATISTICS_update (stats, "# keepalives forwarded", 1, GNUNET_NO);
1664   GMC_send_prebuilt_message (message, c, NULL, fwd);
1665
1666   return GNUNET_OK;
1667 }
1668
1669
1670 /**
1671  * Send an ACK on the appropriate connection/channel, depending on
1672  * the direction and the position of the peer.
1673  *
1674  * @param c Which connection to send the hop-by-hop ACK.
1675  * @param ch Channel, if any.
1676  * @param fwd Is this a fwd ACK? (will go dest->root)
1677  */
1678 void
1679 GMC_send_ack (struct MeshConnection *c, struct MeshChannel *ch, int fwd)
1680 {
1681   unsigned int buffer;
1682
1683   LOG (GNUNET_ERROR_TYPE_DEBUG,
1684               "send ack %s on %p %p\n",
1685               fwd ? "FWD" : "BCK", c, ch);
1686   if (NULL == c || GMC_is_terminal (c, fwd))
1687   {
1688     struct MeshTunnel3 *t;
1689     LOG (GNUNET_ERROR_TYPE_DEBUG, "  getting from all connections\n");
1690     t = (NULL == c) ? GMCH_get_tunnel (ch) : GMC_get_tunnel (c);
1691     buffer = GMT_get_buffer (t, fwd);
1692   }
1693   else
1694   {
1695     LOG (GNUNET_ERROR_TYPE_DEBUG, "  getting from one connection\n");
1696     buffer = GMC_get_buffer (c, fwd);
1697   }
1698   LOG (GNUNET_ERROR_TYPE_DEBUG, "  buffer available: %u\n", buffer);
1699
1700   if ( (NULL != ch && GMCH_is_origin (ch, fwd)) ||
1701        (NULL != c && GMC_is_origin (c, fwd)) )
1702   {
1703     LOG (GNUNET_ERROR_TYPE_DEBUG, "  sending on channel...\n");
1704     if (0 < buffer)
1705     {
1706       GNUNET_assert (NULL != ch);
1707       LOG (GNUNET_ERROR_TYPE_DEBUG, "  really sending!\n");
1708       send_local_ack (ch, fwd);
1709     }
1710   }
1711   else if (NULL == c)
1712   {
1713     LOG (GNUNET_ERROR_TYPE_DEBUG, "  sending on all connections\n");
1714     GNUNET_assert (NULL != ch);
1715     channel_send_connections_ack (ch, buffer, fwd);
1716   }
1717   else
1718   {
1719     LOG (GNUNET_ERROR_TYPE_DEBUG, "  sending on connection\n");
1720     connection_send_ack (c, buffer, fwd);
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  * Get the connection tunnel.
1871  *
1872  * @param c Connection to get the tunnel from.
1873  *
1874  * @return tunnel of the connection.
1875  */
1876 struct MeshTunnel3 *
1877 GMC_get_tunnel (const struct MeshConnection *c)
1878 {
1879   return c->t;
1880 }
1881
1882
1883 /**
1884  * Get free buffer space in a connection.
1885  *
1886  * @param c Connection.
1887  * @param fwd Is query about FWD traffic?
1888  *
1889  * @return Free buffer space [0 - max_msgs_queue/max_connections]
1890  */
1891 unsigned int
1892 GMC_get_buffer (struct MeshConnection *c, int fwd)
1893 {
1894   struct MeshFlowControl *fc;
1895
1896   fc = fwd ? &c->fwd_fc : &c->bck_fc;
1897
1898   return (fc->queue_max - fc->queue_n);
1899 }
1900
1901 /**
1902  * Get messages queued in a connection.
1903  *
1904  * @param c Connection.
1905  * @param fwd Is query about FWD traffic?
1906  *
1907  * @return Number of messages queued.
1908  */
1909 unsigned int
1910 GMC_get_qn (struct MeshConnection *c, int fwd)
1911 {
1912   struct MeshFlowControl *fc;
1913
1914   fc = fwd ? &c->fwd_fc : &c->bck_fc;
1915
1916   return fc->queue_n;
1917 }
1918
1919
1920 /**
1921  * Notify other peers on a connection of a broken link. Mark connections
1922  * to destroy after all traffic has been sent.
1923  *
1924  * @param c Connection on which there has been a disconnection.
1925  * @param peer Peer that disconnected.
1926  * @param my_full_id My ID (to send to other peers).
1927  */
1928 void
1929 GMC_notify_broken (struct MeshConnection *c,
1930                    struct MeshPeer *peer,
1931                    struct GNUNET_PeerIdentity *my_full_id)
1932 {
1933   struct GNUNET_MESH_ConnectionBroken msg;
1934   int fwd;
1935
1936   fwd = peer == connection_get_prev_hop (c);
1937
1938   connection_cancel_queues (c, !fwd);
1939   if (GMC_is_terminal (c, fwd))
1940   {
1941     /* Local shutdown, no one to notify about this. */
1942     GMC_destroy (c);
1943     return;
1944   }
1945
1946   msg.header.size = htons (sizeof (struct GNUNET_MESH_ConnectionBroken));
1947   msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CONNECTION_BROKEN);
1948   msg.cid = c->id;
1949   msg.peer1 = *my_full_id;
1950   msg.peer2 = *GMP_get_id (peer);
1951   GMC_send_prebuilt_message (&msg.header, c, NULL, fwd);
1952   c->destroy = GNUNET_YES;
1953
1954   return;
1955 }
1956
1957
1958 /**
1959  * Is this peer the first one on the connection?
1960  *
1961  * @param c Connection.
1962  * @param fwd Is this about fwd traffic?
1963  *
1964  * @return GNUNET_YES if origin, GNUNET_NO if relay/terminal.
1965  */
1966 int
1967 GMC_is_origin (struct MeshConnection *c, int fwd)
1968 {
1969   if (!fwd && c->path->length - 1 == c->own_pos )
1970     return GNUNET_YES;
1971   if (fwd && 0 == c->own_pos)
1972     return GNUNET_YES;
1973   return GNUNET_NO;
1974 }
1975
1976
1977 /**
1978  * Is this peer the last one on the connection?
1979  *
1980  * @param c Connection.
1981  * @param fwd Is this about fwd traffic?
1982  *            Note that the ROOT is the terminal for BCK traffic!
1983  *
1984  * @return GNUNET_YES if terminal, GNUNET_NO if relay/origin.
1985  */
1986 int
1987 GMC_is_terminal (struct MeshConnection *c, int fwd)
1988 {
1989   return GMC_is_origin (c, !fwd);
1990 }
1991
1992
1993 /**
1994  * Sends an already built message on a connection, properly registering
1995  * all used resources.
1996  *
1997  * @param message Message to send. Function makes a copy of it.
1998  *                If message is not hop-by-hop, decrements TTL of copy.
1999  * @param c Connection on which this message is transmitted.
2000  * @param ch Channel on which this message is transmitted, or NULL.
2001  * @param fwd Is this a fwd message?
2002  */
2003 void
2004 GMC_send_prebuilt_message (const struct GNUNET_MessageHeader *message,
2005                            struct MeshConnection *c,
2006                            struct MeshChannel *ch,
2007                            int fwd)
2008 {
2009   void *data;
2010   size_t size;
2011   uint16_t type;
2012
2013   size = ntohs (message->size);
2014   data = GNUNET_malloc (size);
2015   memcpy (data, message, size);
2016   type = ntohs (message->type);
2017   LOG (GNUNET_ERROR_TYPE_DEBUG, "Send %s (%u) on connection %s\n",
2018               GNUNET_MESH_DEBUG_M2S (type), size, GNUNET_h2s (&c->id));
2019
2020   switch (type)
2021   {
2022     struct GNUNET_MESH_Encrypted *emsg;
2023     struct GNUNET_MESH_ACK       *amsg;
2024     struct GNUNET_MESH_Poll      *pmsg;
2025     struct GNUNET_MESH_ConnectionDestroy *dmsg;
2026     struct GNUNET_MESH_ConnectionBroken  *bmsg;
2027     uint32_t ttl;
2028
2029     case GNUNET_MESSAGE_TYPE_MESH_FWD:
2030     case GNUNET_MESSAGE_TYPE_MESH_BCK:
2031       emsg = (struct GNUNET_MESH_Encrypted *) data;
2032       ttl = ntohl (emsg->ttl);
2033       if (0 == ttl)
2034       {
2035         GNUNET_break_op (0);
2036         return;
2037       }
2038       emsg->cid = c->id;
2039       emsg->ttl = htonl (ttl - 1);
2040       emsg->pid = htonl (fwd ? c->fwd_fc.next_pid++ : c->bck_fc.next_pid++);
2041       LOG (GNUNET_ERROR_TYPE_DEBUG, " pid %u\n", ntohl (emsg->pid));
2042       break;
2043
2044     case GNUNET_MESSAGE_TYPE_MESH_ACK:
2045       amsg = (struct GNUNET_MESH_ACK *) data;
2046       amsg->cid = c->id;
2047       LOG (GNUNET_ERROR_TYPE_DEBUG, " ack %u\n", ntohl (amsg->ack));
2048       break;
2049
2050     case GNUNET_MESSAGE_TYPE_MESH_POLL:
2051       pmsg = (struct GNUNET_MESH_Poll *) data;
2052       pmsg->cid = c->id;
2053       pmsg->pid = htonl (fwd ? c->fwd_fc.last_pid_sent : c->bck_fc.last_pid_sent);
2054       LOG (GNUNET_ERROR_TYPE_DEBUG, " poll %u\n", ntohl (pmsg->pid));
2055       break;
2056
2057     case GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY:
2058       dmsg = (struct GNUNET_MESH_ConnectionDestroy *) data;
2059       dmsg->cid = c->id;
2060       dmsg->reserved = 0;
2061       break;
2062
2063     case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_BROKEN:
2064       bmsg = (struct GNUNET_MESH_ConnectionBroken *) data;
2065       bmsg->cid = c->id;
2066       bmsg->reserved = 0;
2067       break;
2068
2069     case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE:
2070     case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK:
2071       break;
2072
2073     default:
2074       GNUNET_break (0);
2075   }
2076
2077   GMP_queue_add (data,
2078                  type,
2079                  size,
2080                  c,
2081                  ch,
2082                  fwd);
2083 }
2084
2085
2086 /**
2087  * Send a message to all peers in this connection that the connection
2088  * is no longer valid.
2089  *
2090  * If some peer should not receive the message, it should be zero'ed out
2091  * before calling this function.
2092  *
2093  * @param c The connection whose peers to notify.
2094  */
2095 void
2096 GMC_send_destroy (struct MeshConnection *c)
2097 {
2098   struct GNUNET_MESH_ConnectionDestroy msg;
2099
2100   if (GNUNET_YES == c->destroy)
2101     return;
2102
2103   msg.header.size = htons (sizeof (msg));
2104   msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY);;
2105   msg.cid = c->id;
2106   LOG (GNUNET_ERROR_TYPE_DEBUG,
2107               "  sending connection destroy for connection %s\n",
2108               GNUNET_h2s (&c->id));
2109
2110   if (GNUNET_NO == GMC_is_terminal (c, GNUNET_YES))
2111     GMC_send_prebuilt_message (&msg.header, c, NULL, GNUNET_YES);
2112   if (GNUNET_NO == GMC_is_terminal (c, GNUNET_NO))
2113     GMC_send_prebuilt_message (&msg.header, c, NULL, GNUNET_NO);
2114   c->destroy = GNUNET_YES;
2115 }