- begin work on enhanced multipart receiving
[oweals/gnunet.git] / src / mesh / gnunet-service-mesh_connection.c
index 61fe0f2e27df8cec5520bdc7b4e7790f0fbd8913..04f1e07c4c0849254d1f725ceb80f09be36733b1 100644 (file)
@@ -179,7 +179,8 @@ struct MeshConnection
   enum MeshConnectionState state;
 
   /**
-   * Path being used for the tunnel.
+   * Path being used for the tunnel. At the origin of the connection
+   * it's a pointer to the destination's path pool, otherwise just a copy.
    */
   struct MeshPeerPath *path;
 
@@ -221,6 +222,11 @@ struct MeshConnectionQueue
    */
   struct MeshPeerQueue *q;
 
+  /**
+   * Was this a forced message? (Do not account for it)
+   */
+  int forced;
+
   /**
    * Continuation to call once sent.
    */
@@ -334,6 +340,8 @@ GMC_state2s (enum MeshConnectionState s)
       return "MESH_CONNECTION_ACK";
     case MESH_CONNECTION_READY:
       return "MESH_CONNECTION_READY";
+    case MESH_CONNECTION_DESTROYED:
+      return "MESH_CONNECTION_DESTROYED";
     default:
       return "MESH_CONNECTION_STATE_ERROR";
   }
@@ -379,6 +387,11 @@ connection_change_state (struct MeshConnection* c,
   LOG (GNUNET_ERROR_TYPE_DEBUG,
               "Connection %s state was %s\n",
               GMC_2s (c), GMC_state2s (c->state));
+  if (MESH_CONNECTION_DESTROYED == c->state)
+  {
+    LOG (GNUNET_ERROR_TYPE_DEBUG, "state not changing anymore\n");
+    return;
+  }
   LOG (GNUNET_ERROR_TYPE_DEBUG,
               "Connection %s state is now %s\n",
               GMC_2s (c), GMC_state2s (state));
@@ -442,7 +455,7 @@ send_ack (struct MeshConnection *c, unsigned int buffer, int fwd, int force)
 
   LOG (GNUNET_ERROR_TYPE_DEBUG,
               "connection send %s ack on %s\n",
-              fwd ? "FWD" : "BCK", GMC_2s (c));
+              GM_f2s (fwd), GMC_2s (c));
 
   /* Check if we need to transmit the ACK. */
   delta = prev_fc->last_ack_sent - prev_fc->last_pid_recv;
@@ -471,7 +484,7 @@ send_ack (struct MeshConnection *c, unsigned int buffer, int fwd, int force)
   /* Check if message is already in queue */
   if (NULL != prev_fc->ack_msg)
   {
-    if (GMC_is_pid_bigger (ack, prev_fc->last_ack_sent))
+    if (GM_is_pid_bigger (ack, prev_fc->last_ack_sent))
     {
       LOG (GNUNET_ERROR_TYPE_DEBUG, " canceling old ACK\n");
       GMC_cancel (prev_fc->ack_msg);
@@ -492,7 +505,8 @@ send_ack (struct MeshConnection *c, unsigned int buffer, int fwd, int force)
   msg.ack = htonl (ack);
   msg.cid = c->id;
 
-  prev_fc->ack_msg = GMC_send_prebuilt_message (&msg.header, c, !fwd,
+  prev_fc->ack_msg = GMC_send_prebuilt_message (&msg.header, c,
+                                                !fwd, GNUNET_YES,
                                                 &ack_sent, prev_fc);
 }
 
@@ -519,15 +533,17 @@ message_sent (void *cls,
   struct MeshFlowControl *fc;
   struct MeshConnectionQueue *q = cls;
   double usecsperbyte;
+  int forced;
 
   fc = fwd ? &c->fwd_fc : &c->bck_fc;
   LOG (GNUNET_ERROR_TYPE_DEBUG,
        "!  sent %s %s\n",
-       fwd ? "FWD" : "BCK",
-       GNUNET_MESH_DEBUG_M2S (type));
+       GM_f2s (fwd),
+       GM_m2s (type));
   LOG (GNUNET_ERROR_TYPE_DEBUG, "!  C_P- %p %u\n", c, c->pending_messages);
   if (NULL != q)
   {
+    forced = q->forced;
     if (NULL != q->cont)
     {
       LOG (GNUNET_ERROR_TYPE_DEBUG, "!  calling cont\n");
@@ -535,6 +551,15 @@ message_sent (void *cls,
     }
     GNUNET_free (q);
   }
+  else if (type == GNUNET_MESSAGE_TYPE_MESH_ENCRYPTED)
+  {
+    /* If NULL == q and ENCRYPTED == type, message must have been ch_mngmnt */
+    forced = GNUNET_YES;
+  }
+  else
+  {
+    forced = GNUNET_NO;
+  }
   c->pending_messages--;
   if (GNUNET_YES == c->destroy && 0 == c->pending_messages)
   {
@@ -548,10 +573,19 @@ message_sent (void *cls,
     case GNUNET_MESSAGE_TYPE_MESH_ENCRYPTED:
       fc->last_pid_sent++;
       LOG (GNUNET_ERROR_TYPE_DEBUG, "!  Q_N- %p %u\n", fc, fc->queue_n);
-      fc->queue_n--;
-      LOG (GNUNET_ERROR_TYPE_DEBUG,
-           "!   accounting pid %u\n",
-           fc->last_pid_sent);
+      if (GNUNET_NO == forced)
+      {
+        fc->queue_n--;
+        LOG (GNUNET_ERROR_TYPE_DEBUG,
+            "!   accounting pid %u\n",
+            fc->last_pid_sent);
+      }
+      else
+      {
+        LOG (GNUNET_ERROR_TYPE_DEBUG,
+             "!   forced, Q_N not accounting pid %u\n",
+             fc->last_pid_sent);
+      }
       GMC_send_ack (c, fwd, GNUNET_NO);
       break;
 
@@ -605,7 +639,6 @@ get_prev_hop (const struct MeshConnection *c)
 {
   GNUNET_PEER_Id id;
 
-  LOG (GNUNET_ERROR_TYPE_DEBUG, "Get prev hop, own pos %u\n", c->own_pos);
   if (0 == c->own_pos || c->path->length < 2)
     id = c->path->peers[0];
   else
@@ -697,14 +730,14 @@ send_connection_ack (struct MeshConnection *connection, int fwd)
 
   t = connection->t;
   LOG (GNUNET_ERROR_TYPE_DEBUG, "Send connection %s ACK\n",
-       !fwd ? "FWD" : "BCK");
+       !GM_f2s (fwd));
   GMP_queue_add (get_hop (connection, fwd), NULL,
                  GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK,
                  sizeof (struct GNUNET_MESH_ConnectionACK),
                  connection, fwd, &message_sent, NULL);
   connection->pending_messages++;
-  if (MESH_TUNNEL3_NEW == GMT_get_state (t))
-    GMT_change_state (t, MESH_TUNNEL3_WAITING);
+  if (MESH_TUNNEL3_NEW == GMT_get_cstate (t))
+    GMT_change_cstate (t, MESH_TUNNEL3_WAITING);
   if (MESH_CONNECTION_READY != connection->state)
     connection_change_state (connection, MESH_CONNECTION_SENT);
 }
@@ -731,18 +764,15 @@ send_broken (struct MeshConnection *c,
   msg.cid = c->id;
   msg.peer1 = *id1;
   msg.peer2 = *id2;
-  GMC_send_prebuilt_message (&msg.header, c, fwd, NULL, NULL);
+  GMC_send_prebuilt_message (&msg.header, c, fwd, GNUNET_YES, NULL, NULL);
 }
 
 
-
 /**
  * Send keepalive packets for a connection.
  *
  * @param c Connection to keep alive..
  * @param fwd Is this a FWD keepalive? (owner -> dest).
- *
- * FIXME use only one type, register in GMC_send_prebuilt_message()
  */
 static void
 connection_keepalive (struct MeshConnection *c, int fwd)
@@ -750,21 +780,18 @@ connection_keepalive (struct MeshConnection *c, int fwd)
   struct GNUNET_MESH_ConnectionKeepAlive *msg;
   size_t size = sizeof (struct GNUNET_MESH_ConnectionKeepAlive);
   char cbuf[size];
-  uint16_t type;
-
-  type = fwd ? GNUNET_MESSAGE_TYPE_MESH_FWD_KEEPALIVE :
-               GNUNET_MESSAGE_TYPE_MESH_BCK_KEEPALIVE;
 
   LOG (GNUNET_ERROR_TYPE_DEBUG,
        "sending %s keepalive for connection %s]\n",
-       fwd ? "FWD" : "BCK", GMC_2s (c));
+       GM_f2s (fwd), GMC_2s (c));
 
   msg = (struct GNUNET_MESH_ConnectionKeepAlive *) cbuf;
   msg->header.size = htons (size);
-  msg->header.type = htons (type);
+  msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_KEEPALIVE);
   msg->cid = c->id;
+  msg->reserved = htonl (0);
 
-  GMC_send_prebuilt_message (&msg->header, c, fwd, NULL, NULL);
+  GMC_send_prebuilt_message (&msg->header, c, fwd, GNUNET_YES, NULL, NULL);
 }
 
 
@@ -796,7 +823,7 @@ connection_recreate (struct MeshConnection *c, int fwd)
 static void
 connection_maintain (struct MeshConnection *c, int fwd)
 {
-  if (MESH_TUNNEL3_SEARCHING == GMT_get_state (c->t))
+  if (MESH_TUNNEL3_SEARCHING == GMT_get_cstate (c->t))
   {
     /* TODO DHT GET with RO_BART */
     return;
@@ -873,7 +900,7 @@ connection_unlock_queue (struct MeshConnection *c, int fwd)
 
   LOG (GNUNET_ERROR_TYPE_DEBUG,
               "connection_unlock_queue %s on %s\n",
-              fwd ? "FWD" : "BCK", GMC_2s (c));
+              GM_f2s (fwd), GMC_2s (c));
 
   if (GMC_is_terminal (c, fwd))
   {
@@ -903,7 +930,7 @@ connection_cancel_queues (struct MeshConnection *c, int fwd)
 
   LOG (GNUNET_ERROR_TYPE_DEBUG,
        " *** Cancel %s queues for connection %s\n",
-       fwd ? "FWD" : "BCK", GMC_2s (c));
+       GM_f2s (fwd), GMC_2s (c));
   if (NULL == c)
   {
     GNUNET_break (0);
@@ -996,7 +1023,8 @@ connection_poll (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
   msg.header.size = htons (sizeof (msg));
   msg.pid = htonl (fc->last_pid_sent);
   LOG (GNUNET_ERROR_TYPE_DEBUG, " *** last pid sent: %u!\n", fc->last_pid_sent);
-  fc->poll_msg = GMC_send_prebuilt_message (&msg.header, c, fc == &c->fwd_fc,
+  fc->poll_msg = GMC_send_prebuilt_message (&msg.header, c,
+                                            fc == &c->fwd_fc, GNUNET_YES,
                                             &poll_sent, fc);
 }
 
@@ -1064,7 +1092,7 @@ connection_bck_timeout (void *cls,
  *   a keepalive or a path confirmation message (either create or ACK).
  * - For all other peers, this means to destroy the connection,
  *   due to lack of activity.
- * Starts the tiemout if no timeout was running (connection just created).
+ * Starts the timeout if no timeout was running (connection just created).
  *
  * @param c Connection whose timeout to reset.
  * @param fwd Is this forward?
@@ -1079,6 +1107,8 @@ connection_reset_timeout (struct MeshConnection *c, int fwd)
 
   ti = fwd ? &c->fwd_maintenance_task : &c->bck_maintenance_task;
 
+  LOG (GNUNET_ERROR_TYPE_DEBUG, "Connection %s reset timeout\n", GM_f2s (fwd));
+
   if (GNUNET_SCHEDULER_NO_TASK != *ti)
     GNUNET_SCHEDULER_cancel (*ti);
 
@@ -1117,7 +1147,18 @@ register_neighbors (struct MeshConnection *c)
 
   if (GNUNET_NO == GMP_is_neighbor (next_peer)
       || GNUNET_NO == GMP_is_neighbor (prev_peer))
+  {
+    if (GMC_is_origin (c, GNUNET_YES))
+    GNUNET_STATISTICS_update (stats, "# local bad paths", 1, GNUNET_NO);
+    GNUNET_STATISTICS_update (stats, "# bad paths", 1, GNUNET_NO);
+
+    LOG (GNUNET_ERROR_TYPE_DEBUG, "  register neighbors failed\n");
+    LOG (GNUNET_ERROR_TYPE_DEBUG, "  prev: %s, neighbor: %d\n",
+         GMP_2s (prev_peer), GMP_is_neighbor (prev_peer));
+    LOG (GNUNET_ERROR_TYPE_DEBUG, "  next: %s, neighbor: %d\n",
+         GMP_2s (next_peer), GMP_is_neighbor (next_peer));
     return GNUNET_SYSERR;
+  }
 
   GMP_add_connection (next_peer, c);
   GMP_add_connection (prev_peer, c);
@@ -1137,11 +1178,18 @@ unregister_neighbors (struct MeshConnection *c)
   struct MeshPeer *peer;
 
   peer = get_next_hop (c);
-  GMP_remove_connection (peer, c);
+  if (GNUNET_OK != GMP_remove_connection (peer, c))
+  {
+    GNUNET_break (MESH_CONNECTION_NEW == c->state);
+    LOG (GNUNET_ERROR_TYPE_ERROR, "  cstate: %u\n", c->state);
+  }
 
   peer = get_prev_hop (c);
-  GMP_remove_connection (peer, c);
-
+  if (GNUNET_OK != GMP_remove_connection (peer, c))
+  {
+    GNUNET_break (MESH_CONNECTION_NEW == c->state);
+    LOG (GNUNET_ERROR_TYPE_ERROR, "  cstate: %u\n", c->state);
+  }
 }
 
 
@@ -1250,7 +1298,7 @@ GMC_handle_create (void *cls, const struct GNUNET_PeerIdentity *peer,
     }
     LOG (GNUNET_ERROR_TYPE_DEBUG, "  Own position: %u\n", own_pos);
     GMP_add_path_to_all (path, GNUNET_NO);
-        LOG (GNUNET_ERROR_TYPE_DEBUG, "  Creating connection\n");
+    LOG (GNUNET_ERROR_TYPE_DEBUG, "  Creating connection\n");
     c = GMC_new (cid, NULL, path_duplicate (path), own_pos);
     if (NULL == c)
     {
@@ -1277,17 +1325,20 @@ GMC_handle_create (void *cls, const struct GNUNET_PeerIdentity *peer,
     GMP_add_path_to_origin (orig_peer, path_duplicate (path), GNUNET_YES);
 
     add_to_peer (c, orig_peer);
-    if (MESH_TUNNEL3_NEW == GMT_get_state (c->t))
-      GMT_change_state (c->t,  MESH_TUNNEL3_WAITING);
+    if (MESH_TUNNEL3_NEW == GMT_get_cstate (c->t))
+      GMT_change_cstate (c->t,  MESH_TUNNEL3_WAITING);
 
     send_connection_ack (c, GNUNET_NO);
     if (MESH_CONNECTION_SENT == c->state)
       connection_change_state (c, MESH_CONNECTION_ACK);
 
     /* Keep tunnel alive in direction dest->owner*/
-    c->bck_maintenance_task =
-            GNUNET_SCHEDULER_add_delayed (create_connection_time,
-                                          &connection_bck_keepalive, c);
+    if (GNUNET_SCHEDULER_NO_TASK == c->bck_maintenance_task)
+    {
+      c->bck_maintenance_task =
+        GNUNET_SCHEDULER_add_delayed (create_connection_time,
+                                      &connection_bck_keepalive, c);
+    }
   }
   else
   {
@@ -1295,7 +1346,7 @@ GMC_handle_create (void *cls, const struct GNUNET_PeerIdentity *peer,
     LOG (GNUNET_ERROR_TYPE_DEBUG, "  Retransmitting.\n");
     GMP_add_path (dest_peer, path_duplicate (path), GNUNET_NO);
     GMP_add_path_to_origin (orig_peer, path_duplicate (path), GNUNET_NO);
-    GMC_send_prebuilt_message (message, c, GNUNET_YES, NULL, NULL);
+    GMC_send_prebuilt_message (message, c, GNUNET_YES, GNUNET_YES, NULL, NULL);
   }
   path_destroy (path);
   return GNUNET_OK;
@@ -1386,13 +1437,14 @@ GMC_handle_confirm (void *cls, const struct GNUNET_PeerIdentity *peer,
     if (MESH_CONNECTION_SENT == oldstate)
       connection_reset_timeout (c, GNUNET_YES);
 
-    /* Change connection and tunnel state */
+    /* Change connection state */
     connection_change_state (c, MESH_CONNECTION_READY);
-    if (MESH_TUNNEL3_WAITING == GMT_get_state (c->t))
-      GMT_change_state (c->t, MESH_TUNNEL3_READY);
-
-    /* Send ACK (~TCP ACK)*/
     send_connection_ack (c, GNUNET_YES);
+
+    /* Change tunnel state, trigger KX */
+    if (MESH_TUNNEL3_WAITING == GMT_get_cstate (c->t))
+      GMT_change_cstate (c->t, MESH_TUNNEL3_READY);
+
     return GNUNET_OK;
   }
 
@@ -1411,14 +1463,14 @@ GMC_handle_confirm (void *cls, const struct GNUNET_PeerIdentity *peer,
       connection_reset_timeout (c, GNUNET_NO);
 
     /* Change tunnel state */
-    if (MESH_TUNNEL3_WAITING == GMT_get_state (c->t))
-      GMT_change_state (c->t, MESH_TUNNEL3_READY);
+    if (MESH_TUNNEL3_WAITING == GMT_get_cstate (c->t))
+      GMT_change_cstate (c->t, MESH_TUNNEL3_READY);
 
     return GNUNET_OK;
   }
 
   LOG (GNUNET_ERROR_TYPE_DEBUG, "  not for us, retransmitting...\n");
-  GMC_send_prebuilt_message (message, c, fwd, NULL, NULL);
+  GMC_send_prebuilt_message (message, c, fwd, GNUNET_YES, NULL, NULL);
   return GNUNET_OK;
 }
 
@@ -1457,7 +1509,6 @@ GMC_handle_broken (void* cls,
   }
 
   fwd = is_fwd (c, id);
-  connection_cancel_queues (c, !fwd);
   if (GMC_is_terminal (c, fwd))
   {
     if (0 < c->pending_messages)
@@ -1467,8 +1518,9 @@ GMC_handle_broken (void* cls,
   }
   else
   {
-    GMC_send_prebuilt_message (message, c, fwd, NULL, NULL);
+    GMC_send_prebuilt_message (message, c, fwd, GNUNET_YES, NULL, NULL);
     c->destroy = GNUNET_YES;
+    connection_cancel_queues (c, !fwd);
   }
 
   return GNUNET_OK;
@@ -1508,8 +1560,9 @@ GMC_handle_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
      * destroyed the tunnel and retransmitted to children.
      * Safe to ignore.
      */
-    GNUNET_STATISTICS_update (stats, "# control on unknown tunnel",
+    GNUNET_STATISTICS_update (stats, "# control on unknown connection",
                               1, GNUNET_NO);
+    LOG (GNUNET_ERROR_TYPE_DEBUG, "  connection unknown: already destroyed?\n");
     return GNUNET_OK;
   }
   fwd = is_fwd (c, peer);
@@ -1518,8 +1571,16 @@ GMC_handle_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
     GNUNET_break_op (0);
     return GNUNET_OK;
   }
-  GMC_send_prebuilt_message (message, c, fwd, NULL, NULL);
+  if (GNUNET_NO == GMC_is_terminal (c, fwd))
+    GMC_send_prebuilt_message (message, c, fwd, GNUNET_YES, NULL, NULL);
+  else if (0 == c->pending_messages)
+  {
+    LOG (GNUNET_ERROR_TYPE_DEBUG, "!  directly destroying connection!\n");
+    GMC_destroy (c);
+    return GNUNET_OK;
+  }
   c->destroy = GNUNET_YES;
+  c->state = MESH_CONNECTION_DESTROYED;
 
   return GNUNET_OK;
 }
@@ -1559,7 +1620,7 @@ handle_mesh_encrypted (const struct GNUNET_PeerIdentity *peer,
   type = ntohs (msg->header.type);
   LOG (GNUNET_ERROR_TYPE_DEBUG, "\n\n");
   LOG (GNUNET_ERROR_TYPE_DEBUG, "got a %s message (#%u) from %s\n",
-       GNUNET_MESH_DEBUG_M2S (type), ntohl (msg->pid), GNUNET_i2s (peer));
+       GM_m2s (type), ntohl (msg->pid), GNUNET_i2s (peer));
 
   /* Check connection */
   c = connection_get (&msg->cid);
@@ -1597,7 +1658,7 @@ handle_mesh_encrypted (const struct GNUNET_PeerIdentity *peer,
   /* Check PID */
   fc = fwd ? &c->bck_fc : &c->fwd_fc;
   pid = ntohl (msg->pid);
-  if (GMC_is_pid_bigger (pid, fc->last_ack_sent))
+  if (GM_is_pid_bigger (pid, fc->last_ack_sent))
   {
     GNUNET_STATISTICS_update (stats, "# unsolicited message", 1, GNUNET_NO);
     LOG (GNUNET_ERROR_TYPE_DEBUG,
@@ -1605,7 +1666,7 @@ handle_mesh_encrypted (const struct GNUNET_PeerIdentity *peer,
                 pid, fc->last_pid_recv, fc->last_ack_sent);
     return GNUNET_OK;
   }
-  if (GNUNET_NO == GMC_is_pid_bigger (pid, fc->last_pid_recv))
+  if (GNUNET_NO == GM_is_pid_bigger (pid, fc->last_pid_recv))
   {
     GNUNET_STATISTICS_update (stats, "# duplicate PID", 1, GNUNET_NO);
     LOG (GNUNET_ERROR_TYPE_DEBUG,
@@ -1649,7 +1710,7 @@ handle_mesh_encrypted (const struct GNUNET_PeerIdentity *peer,
   }
 
   GNUNET_STATISTICS_update (stats, "# messages forwarded", 1, GNUNET_NO);
-  GMC_send_prebuilt_message (&msg->header, c, fwd, NULL, NULL);
+  GMC_send_prebuilt_message (&msg->header, c, fwd, GNUNET_NO, NULL, NULL);
 
   return GNUNET_OK;
 }
@@ -1686,7 +1747,7 @@ handle_mesh_kx (const struct GNUNET_PeerIdentity *peer,
   type = ntohs (msg->header.type);
   LOG (GNUNET_ERROR_TYPE_DEBUG, "\n\n");
   LOG (GNUNET_ERROR_TYPE_DEBUG, "got a %s message from %s\n",
-              GNUNET_MESH_DEBUG_M2S (type), GNUNET_i2s (peer));
+       GM_m2s (type), GNUNET_i2s (peer));
 
   /* Check connection */
   c = connection_get (&msg->cid);
@@ -1696,6 +1757,7 @@ handle_mesh_kx (const struct GNUNET_PeerIdentity *peer,
     LOG (GNUNET_ERROR_TYPE_DEBUG, "WARNING connection unknown\n");
     return GNUNET_OK;
   }
+  LOG (GNUNET_ERROR_TYPE_DEBUG, " on connection %s\n", GMC_2s (c));
 
   /* Check if origin is as expected */
   neighbor = get_prev_hop (c);
@@ -1725,8 +1787,8 @@ handle_mesh_kx (const struct GNUNET_PeerIdentity *peer,
   connection_reset_timeout (c, fwd);
   if (NULL != c->t)
   {
-    if (MESH_TUNNEL3_WAITING == GMT_get_state (c->t))
-      GMT_change_state (c->t, MESH_TUNNEL3_READY);
+    if (MESH_TUNNEL3_WAITING == GMT_get_cstate (c->t))
+      GMT_change_cstate (c->t, MESH_TUNNEL3_READY);
   }
 
   /* Is this message for us? */
@@ -1746,7 +1808,7 @@ handle_mesh_kx (const struct GNUNET_PeerIdentity *peer,
   /* Message not for us: forward to next hop */
   LOG (GNUNET_ERROR_TYPE_DEBUG, "  not for us, retransmitting...\n");
   GNUNET_STATISTICS_update (stats, "# messages forwarded", 1, GNUNET_NO);
-  GMC_send_prebuilt_message (&msg->header, c, fwd, NULL, NULL);
+  GMC_send_prebuilt_message (&msg->header, c, fwd, GNUNET_NO, NULL, NULL);
 
   return GNUNET_OK;
 }
@@ -1848,12 +1910,12 @@ GMC_handle_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
   ack = ntohl (msg->ack);
   LOG (GNUNET_ERROR_TYPE_DEBUG, "  ACK %u (was %u)\n",
               ack, fc->last_ack_recv);
-  if (GMC_is_pid_bigger (ack, fc->last_ack_recv))
+  if (GM_is_pid_bigger (ack, fc->last_ack_recv))
     fc->last_ack_recv = ack;
 
   /* Cancel polling if the ACK is big enough. */
   if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task &&
-      GMC_is_pid_bigger (fc->last_ack_recv, fc->last_pid_sent))
+      GM_is_pid_bigger (fc->last_ack_recv, fc->last_pid_sent))
   {
     LOG (GNUNET_ERROR_TYPE_DEBUG, "  Cancel poll\n");
     GNUNET_SCHEDULER_cancel (fc->poll_task);
@@ -1955,6 +2017,7 @@ GMC_handle_keepalive (void *cls, const struct GNUNET_PeerIdentity *peer,
   struct GNUNET_MESH_ConnectionKeepAlive *msg;
   struct MeshConnection *c;
   struct MeshPeer *neighbor;
+  GNUNET_PEER_Id peer_id;
   int fwd;
 
   msg = (struct GNUNET_MESH_ConnectionKeepAlive *) message;
@@ -1969,15 +2032,25 @@ GMC_handle_keepalive (void *cls, const struct GNUNET_PeerIdentity *peer,
     return GNUNET_OK;
   }
 
-  fwd = GNUNET_MESSAGE_TYPE_MESH_FWD_KEEPALIVE == ntohs (message->type) ?
-        GNUNET_YES : GNUNET_NO;
-
-  /* Check if origin is as expected */
-  neighbor = get_hop (c, fwd);
-  if (GNUNET_PEER_search (peer) != GMP_get_short_id (neighbor))
+  /* Check if origin is as expected TODO refactor and reuse */
+  peer_id = GNUNET_PEER_search (peer);
+  neighbor = get_prev_hop (c);
+  if (peer_id == GMP_get_short_id (neighbor))
   {
-    GNUNET_break_op (0);
-    return GNUNET_OK;
+    fwd = GNUNET_YES;
+  }
+  else
+  {
+    neighbor = get_next_hop (c);
+    if (peer_id == GMP_get_short_id (neighbor))
+    {
+      fwd = GNUNET_NO;
+    }
+    else
+    {
+      GNUNET_break_op (0);
+      return GNUNET_OK;
+    }
   }
 
   connection_change_state (c, MESH_CONNECTION_READY);
@@ -1987,7 +2060,7 @@ GMC_handle_keepalive (void *cls, const struct GNUNET_PeerIdentity *peer,
     return GNUNET_OK;
 
   GNUNET_STATISTICS_update (stats, "# keepalives forwarded", 1, GNUNET_NO);
-  GMC_send_prebuilt_message (message, c, fwd, NULL, NULL);
+  GMC_send_prebuilt_message (message, c, fwd, GNUNET_YES, NULL, NULL);
 
   return GNUNET_OK;
 }
@@ -2008,7 +2081,7 @@ GMC_send_ack (struct MeshConnection *c, int fwd, int force)
 
   LOG (GNUNET_ERROR_TYPE_DEBUG,
        "GMC send %s ACK on %s\n",
-       fwd ? "FWD" : "BCK", GMC_2s (c));
+       GM_f2s (fwd), GMC_2s (c));
 
   if (NULL == c)
   {
@@ -2016,6 +2089,12 @@ GMC_send_ack (struct MeshConnection *c, int fwd, int force)
     return;
   }
 
+  if (GNUNET_NO != c->destroy)
+  {
+    LOG (GNUNET_ERROR_TYPE_DEBUG, "  being destroyed, why bother...\n");
+    return;
+  }
+
   /* Get available buffer space */
   if (GMC_is_terminal (c, fwd))
   {
@@ -2132,33 +2211,37 @@ GMC_new (const struct GNUNET_HashCode *cid,
 
   c = GNUNET_new (struct MeshConnection);
   c->id = *cid;
-  GNUNET_CONTAINER_multihashmap_put (connections, &c->id, c,
-                                     GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
+  GNUNET_assert (GNUNET_OK ==
+                 GNUNET_CONTAINER_multihashmap_put (connections,
+                                                    &c->id, c,
+                                                    GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
   fc_init (&c->fwd_fc);
   fc_init (&c->bck_fc);
   c->fwd_fc.c = c;
   c->bck_fc.c = c;
 
   c->t = t;
-  if (own_pos > p->length - 1)
+  GNUNET_assert (own_pos <= p->length - 1);
+  c->own_pos = own_pos;
+  c->path = p;
+
+  if (GNUNET_OK != register_neighbors (c))
   {
-    GNUNET_break (0);
+    if (0 == own_pos)
+    {
+      GMT_remove_path (c->t, p);
+      c->t = NULL;
+      c->path = NULL;
+    }
     GMC_destroy (c);
     return NULL;
   }
-  c->own_pos = own_pos;
-  c->path = p;
 
   if (0 == own_pos)
   {
     c->fwd_maintenance_task =
-            GNUNET_SCHEDULER_add_delayed (create_connection_time,
-                                          &connection_fwd_keepalive, c);
-  }
-  if (GNUNET_OK != register_neighbors (c))
-  {
-    GMC_destroy (c);
-    return NULL;
+      GNUNET_SCHEDULER_add_delayed (create_connection_time,
+                                    &connection_fwd_keepalive, c);
   }
 
   return c;
@@ -2169,7 +2252,10 @@ void
 GMC_destroy (struct MeshConnection *c)
 {
   if (NULL == c)
+  {
+    GNUNET_break (0);
     return;
+  }
 
   if (2 == c->destroy) /* cancel queues -> GMP_queue_cancel -> q_destroy -> */
     return;            /* -> message_sent -> GMC_destroy. Don't loop. */
@@ -2182,27 +2268,17 @@ GMC_destroy (struct MeshConnection *c)
        c->fwd_fc.poll_task, c->bck_fc.poll_task);
 
   /* Cancel all traffic */
-  connection_cancel_queues (c, GNUNET_YES);
-  connection_cancel_queues (c, GNUNET_NO);
+  if (NULL != c->path)
+  {
+    connection_cancel_queues (c, GNUNET_YES);
+    connection_cancel_queues (c, GNUNET_NO);
+    unregister_neighbors (c);
+  }
 
   LOG (GNUNET_ERROR_TYPE_DEBUG, " fc tasks f: %u, b: %u\n",
        c->fwd_fc.poll_task, c->bck_fc.poll_task);
 
   /* Cancel maintainance task (keepalive/timeout) */
-  if (GNUNET_SCHEDULER_NO_TASK != c->fwd_maintenance_task)
-    GNUNET_SCHEDULER_cancel (c->fwd_maintenance_task);
-  if (GNUNET_SCHEDULER_NO_TASK != c->bck_maintenance_task)
-    GNUNET_SCHEDULER_cancel (c->bck_maintenance_task);
-  if (GNUNET_SCHEDULER_NO_TASK != c->fwd_fc.poll_task)
-  {
-    GNUNET_SCHEDULER_cancel (c->fwd_fc.poll_task);
-    LOG (GNUNET_ERROR_TYPE_DEBUG, " *** POLL FWD canceled\n");
-  }
-  if (GNUNET_SCHEDULER_NO_TASK != c->bck_fc.poll_task)
-  {
-    GNUNET_SCHEDULER_cancel (c->bck_fc.poll_task);
-    LOG (GNUNET_ERROR_TYPE_DEBUG, " *** POLL BCK canceled\n");
-  }
   if (NULL != c->fwd_fc.poll_msg)
   {
     GMC_cancel (c->fwd_fc.poll_msg);
@@ -2214,20 +2290,31 @@ GMC_destroy (struct MeshConnection *c)
     LOG (GNUNET_ERROR_TYPE_DEBUG, " *** POLL msg BCK canceled\n");
   }
 
-  /* Unregister from neighbors */
-  unregister_neighbors (c);
-
-  /* Delete */
-  GNUNET_STATISTICS_update (stats, "# connections", -1, GNUNET_NO);
+  /* Delete from tunnel */
   if (NULL != c->t)
     GMT_remove_connection (c->t, c);
 
-  if (GNUNET_NO == GMC_is_origin (c, GNUNET_YES))
+  if (GNUNET_NO == GMC_is_origin (c, GNUNET_YES) && NULL != c->path)
     path_destroy (c->path);
+  if (GNUNET_SCHEDULER_NO_TASK != c->fwd_maintenance_task)
+    GNUNET_SCHEDULER_cancel (c->fwd_maintenance_task);
+  if (GNUNET_SCHEDULER_NO_TASK != c->bck_maintenance_task)
+    GNUNET_SCHEDULER_cancel (c->bck_maintenance_task);
+  if (GNUNET_SCHEDULER_NO_TASK != c->fwd_fc.poll_task)
+  {
+    GNUNET_SCHEDULER_cancel (c->fwd_fc.poll_task);
+    LOG (GNUNET_ERROR_TYPE_DEBUG, " *** POLL FWD canceled\n");
+  }
+  if (GNUNET_SCHEDULER_NO_TASK != c->bck_fc.poll_task)
+  {
+    GNUNET_SCHEDULER_cancel (c->bck_fc.poll_task);
+    LOG (GNUNET_ERROR_TYPE_DEBUG, " *** POLL BCK canceled\n");
+  }
 
   GNUNET_break (GNUNET_YES ==
                 GNUNET_CONTAINER_multihashmap_remove (connections, &c->id, c));
 
+  GNUNET_STATISTICS_update (stats, "# connections", -1, GNUNET_NO);
   GNUNET_free (c);
 }
 
@@ -2255,7 +2342,9 @@ GMC_get_id (const struct MeshConnection *c)
 const struct MeshPeerPath *
 GMC_get_path (const struct MeshConnection *c)
 {
-  return c->path;
+  if (GNUNET_NO == c->destroy)
+    return c->path;
+  return NULL;
 }
 
 
@@ -2318,7 +2407,7 @@ GMC_get_allowed (struct MeshConnection *c, int fwd)
   struct MeshFlowControl *fc;
 
   fc = fwd ? &c->fwd_fc : &c->bck_fc;
-  if (GMC_is_pid_bigger(fc->last_pid_recv, fc->last_ack_sent))
+  if (GM_is_pid_bigger(fc->last_pid_recv, fc->last_ack_sent))
   {
     return 0;
   }
@@ -2393,6 +2482,7 @@ GMC_notify_broken (struct MeshConnection *c,
    * (the one we just scheduled), so no point in checking whether to
    * destroy immediately. */
   c->destroy = GNUNET_YES;
+  c->state = MESH_CONNECTION_DESTROYED;
 
   /**
    * Cancel all queues, if no message is left, connection will be destroyed.
@@ -2452,7 +2542,7 @@ GMC_is_sendable (struct MeshConnection *c, int fwd)
   struct MeshFlowControl *fc;
 
   fc = fwd ? &c->fwd_fc : &c->bck_fc;
-  if (GMC_is_pid_bigger (fc->last_ack_recv, fc->last_pid_sent))
+  if (GM_is_pid_bigger (fc->last_ack_recv, fc->last_pid_sent))
     return GNUNET_YES;
   return GNUNET_NO;
 }
@@ -2465,6 +2555,7 @@ GMC_is_sendable (struct MeshConnection *c, int fwd)
  *                If message is not hop-by-hop, decrements TTL of copy.
  * @param c Connection on which this message is transmitted.
  * @param fwd Is this a fwd message?
+ * @param force Force the connection to accept the message (buffer overfill).
  * @param cont Continuation called once message is sent. Can be NULL.
  * @param cont_cls Closure for @c cont.
  *
@@ -2474,7 +2565,7 @@ GMC_is_sendable (struct MeshConnection *c, int fwd)
  */
 struct MeshConnectionQueue *
 GMC_send_prebuilt_message (const struct GNUNET_MessageHeader *message,
-                           struct MeshConnection *c, int fwd,
+                           struct MeshConnection *c, int fwd, int force,
                            GMC_sent cont, void *cont_cls)
 {
   struct MeshFlowControl *fc;
@@ -2489,10 +2580,10 @@ GMC_send_prebuilt_message (const struct GNUNET_MessageHeader *message,
   memcpy (data, message, size);
   type = ntohs (message->type);
   LOG (GNUNET_ERROR_TYPE_DEBUG, "Send %s (%u bytes) on connection %s\n",
-              GNUNET_MESH_DEBUG_M2S (type), size, GMC_2s (c));
+       GM_m2s (type), size, GMC_2s (c));
 
   fc = fwd ? &c->fwd_fc : &c->bck_fc;
-  droppable = GNUNET_YES;
+  droppable = GNUNET_NO == force;
   switch (type)
   {
     struct GNUNET_MESH_Encrypted *emsg;
@@ -2516,11 +2607,18 @@ GMC_send_prebuilt_message (const struct GNUNET_MessageHeader *message,
       emsg->ttl = htonl (ttl - 1);
       emsg->pid = htonl (fc->next_pid++);
       LOG (GNUNET_ERROR_TYPE_DEBUG, "  Q_N+ %p %u\n", fc, fc->queue_n);
-      fc->queue_n++;
-      LOG (GNUNET_ERROR_TYPE_DEBUG, "pid %u\n", ntohl (emsg->pid));
-      LOG (GNUNET_ERROR_TYPE_DEBUG, "last pid sent %u\n", fc->last_pid_sent);
-      LOG (GNUNET_ERROR_TYPE_DEBUG, "     ack recv %u\n", fc->last_ack_recv);
-      if (GMC_is_pid_bigger (fc->last_pid_sent + 1, fc->last_ack_recv))
+      if (GNUNET_YES == droppable)
+      {
+        fc->queue_n++;
+        LOG (GNUNET_ERROR_TYPE_DEBUG, "pid %u\n", ntohl (emsg->pid));
+        LOG (GNUNET_ERROR_TYPE_DEBUG, "last pid sent %u\n", fc->last_pid_sent);
+        LOG (GNUNET_ERROR_TYPE_DEBUG, "     ack recv %u\n", fc->last_ack_recv);
+      }
+      else
+      {
+        LOG (GNUNET_ERROR_TYPE_DEBUG, "  not droppable, Q_N stays the same\n");
+      }
+      if (GM_is_pid_bigger (fc->last_pid_sent + 1, fc->last_ack_recv))
       {
         GMC_start_poll (c, fwd);
       }
@@ -2559,10 +2657,13 @@ GMC_send_prebuilt_message (const struct GNUNET_MessageHeader *message,
 
     case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE:
     case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK:
+    case GNUNET_MESSAGE_TYPE_MESH_KEEPALIVE:
       break;
 
     default:
       GNUNET_break (0);
+      GNUNET_free (data);
+      return NULL;
   }
 
   if (fc->queue_n > fc->queue_max && droppable)
@@ -2585,14 +2686,8 @@ GMC_send_prebuilt_message (const struct GNUNET_MessageHeader *message,
   LOG (GNUNET_ERROR_TYPE_DEBUG, "  C_P+ %p %u\n", c, c->pending_messages);
   c->pending_messages++;
 
-  if (NULL == cont)
-  {
-    (void) GMP_queue_add (get_hop (c, fwd), data, type, size, c, fwd,
-                          &message_sent, NULL);
-    return NULL;
-  }
-
   q = GNUNET_new (struct MeshConnectionQueue);
+  q->forced = !droppable;
   q->q = GMP_queue_add (get_hop (c, fwd), data, type, size, c, fwd,
                         &message_sent, q);
   if (NULL == q->q)
@@ -2636,21 +2731,24 @@ GMC_cancel (struct MeshConnectionQueue *q)
 void
 GMC_send_create (struct MeshConnection *connection)
 {
-  enum MeshTunnel3State state;
+  enum MeshTunnel3CState state;
   size_t size;
 
   size = sizeof (struct GNUNET_MESH_ConnectionCreate);
   size += connection->path->length * sizeof (struct GNUNET_PeerIdentity);
+
   LOG (GNUNET_ERROR_TYPE_DEBUG, "Send connection create\n");
-  GMP_queue_add (get_next_hop (connection), NULL,
-                 GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE,
-                 size, connection, GNUNET_YES, &message_sent, NULL);
   LOG (GNUNET_ERROR_TYPE_DEBUG, "  C_P+ %p %u (create)\n",
        connection, connection->pending_messages);
   connection->pending_messages++;
-  state = GMT_get_state (connection->t);
+
+  GMP_queue_add (get_next_hop (connection), NULL,
+                 GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE,
+                 size, connection, GNUNET_YES, &message_sent, NULL);
+
+  state = GMT_get_cstate (connection->t);
   if (MESH_TUNNEL3_SEARCHING == state || MESH_TUNNEL3_NEW == state)
-    GMT_change_state (connection->t, MESH_TUNNEL3_WAITING);
+    GMT_change_cstate (connection->t, MESH_TUNNEL3_WAITING);
   if (MESH_CONNECTION_NEW == connection->state)
     connection_change_state (connection, MESH_CONNECTION_SENT);
 }
@@ -2681,10 +2779,13 @@ GMC_send_destroy (struct MeshConnection *c)
               GMC_2s (c));
 
   if (GNUNET_NO == GMC_is_terminal (c, GNUNET_YES))
-    GMC_send_prebuilt_message (&msg.header, c, GNUNET_YES, NULL, NULL);
+    GMC_send_prebuilt_message (&msg.header, c,
+                               GNUNET_YES, GNUNET_YES, NULL, NULL);
   if (GNUNET_NO == GMC_is_terminal (c, GNUNET_NO))
-    GMC_send_prebuilt_message (&msg.header, c, GNUNET_NO, NULL, NULL);
+    GMC_send_prebuilt_message (&msg.header, c,
+                               GNUNET_NO, GNUNET_YES, NULL, NULL);
   c->destroy = GNUNET_YES;
+  c->state = MESH_CONNECTION_DESTROYED;
 }
 
 
@@ -2705,7 +2806,7 @@ GMC_start_poll (struct MeshConnection *c, int fwd)
 
   fc = fwd ? &c->fwd_fc : &c->bck_fc;
   LOG (GNUNET_ERROR_TYPE_DEBUG, " *** POLL %s requested\n",
-       fwd ? "FWD" : "BCK");
+       GM_f2s (fwd));
   if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task || NULL != fc->poll_msg)
   {
     LOG (GNUNET_ERROR_TYPE_DEBUG, " ***   not needed (%u, %p)\n",
@@ -2748,6 +2849,9 @@ GMC_stop_poll (struct MeshConnection *c, int fwd)
 const char *
 GMC_2s (struct MeshConnection *c)
 {
+  if (NULL == c)
+    return "NULL";
+
   if (NULL != c->t)
   {
     static char buf[128];