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