- start test only after warmup
[oweals/gnunet.git] / src / mesh / gnunet-service-mesh_connection.c
index f1dc2dfc90743d88290a9c1311b26470014bf721..5a40dbe43c7594c60832d8d9886e610c608092c0 100644 (file)
@@ -360,7 +360,7 @@ connection_reset_timeout (struct MeshConnection *c, int fwd);
 
 
 /**
- * Get string description for tunnel state.
+ * Get string description for tunnel state. Reentrant.
  *
  * @param s Tunnel state.
  *
@@ -415,14 +415,7 @@ fc_init (struct MeshFlowControl *fc)
 static struct MeshConnection *
 connection_get (const struct GNUNET_MeshHash *cid)
 {
-  struct GNUNET_HashCode hash;
-  struct GNUNET_MeshHash *aux;
-
-  memcpy (&hash, cid, sizeof (cid));
-  aux = (struct GNUNET_MeshHash *) &hash;
-  memset (&aux[1], 0, sizeof (hash) - sizeof (*cid));
-
-  return GNUNET_CONTAINER_multihashmap_get (connections, &hash);
+  return GNUNET_CONTAINER_multihashmap_get (connections, GM_h2hc (cid));
 }
 
 
@@ -431,16 +424,13 @@ connection_change_state (struct MeshConnection* c,
                          enum MeshConnectionState state)
 {
   LOG (GNUNET_ERROR_TYPE_DEBUG,
-              "Connection %s state was %s\n",
-              GMC_2s (c), GMC_state2s (c->state));
+       "Connection %s state %s -> %s\n",
+       GMC_2s (c), GMC_state2s (c->state), GMC_state2s (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));
   c->state = state;
   if (MESH_CONNECTION_READY == state)
     c->create_retry = 1;
@@ -846,7 +836,7 @@ send_broken (struct MeshConnection *c,
  * @param peer Peer to notify (neighbor who sent the connection).
  */
 static void
-send_broken2 (struct GNUNET_HashCode *connection_id,
+send_broken2 (struct GNUNET_MeshHash *connection_id,
              const struct GNUNET_PeerIdentity *id1,
              const struct GNUNET_PeerIdentity *id2,
              GNUNET_PEER_Id peer_id)
@@ -854,8 +844,8 @@ send_broken2 (struct GNUNET_HashCode *connection_id,
   struct GNUNET_MESH_ConnectionBroken *msg;
   struct MeshPeer *neighbor;
 
-  LOG (GNUNET_ERROR_TYPE_INFO,
-       "Send BROKEN on unknown connection %s\n", GNUNET_h2s (connection_id));
+  LOG (GNUNET_ERROR_TYPE_INFO, "=> BROKEN on unknown connection %s\n",
+       GNUNET_h2s (GM_h2hc (connection_id)));
 
   msg = GNUNET_new (struct GNUNET_MESH_ConnectionBroken);
   msg->header.size = htons (sizeof (struct GNUNET_MESH_ConnectionBroken));
@@ -1453,8 +1443,8 @@ build_path_from_peer_ids (struct GNUNET_PeerIdentity *peers,
       if (path->peers[j] == shortid)
       {
         LOG (GNUNET_ERROR_TYPE_DEBUG, "    already exists at pos %u\n", j);
-        offset += i - j;
-        LOG (GNUNET_ERROR_TYPE_DEBUG, "    offset now\n", offset);
+        offset = i - j;
+        LOG (GNUNET_ERROR_TYPE_DEBUG, "    offset now %u\n", offset);
         GNUNET_PEER_change_rc (shortid, -1);
       }
     }
@@ -1487,10 +1477,11 @@ build_path_from_peer_ids (struct GNUNET_PeerIdentity *peers,
 static void
 log_message (const struct GNUNET_MessageHeader *message,
              const struct GNUNET_PeerIdentity *peer,
-             const struct GNUNET_HashCode *hash)
+             const struct GNUNET_MeshHash *hash)
 {
   LOG (GNUNET_ERROR_TYPE_INFO, "<- %s on connection %s from %s\n",
-       GM_m2s (ntohs (message->type)), GNUNET_h2s (hash), GNUNET_i2s (peer));
+       GM_m2s (ntohs (message->type)), GNUNET_h2s (GM_h2hc (hash)),
+       GNUNET_i2s (peer));
 }
 
 /******************************************************************************/
@@ -1513,7 +1504,7 @@ GMC_handle_create (void *cls, const struct GNUNET_PeerIdentity *peer,
 {
   struct GNUNET_MESH_ConnectionCreate *msg;
   struct GNUNET_PeerIdentity *id;
-  struct GNUNET_HashCode *cid;
+  struct GNUNET_MeshHash *cid;
   struct MeshPeerPath *path;
   struct MeshPeer *dest_peer;
   struct MeshPeer *orig_peer;
@@ -1750,7 +1741,7 @@ GMC_handle_confirm (void *cls, const struct GNUNET_PeerIdentity *peer,
 
 
 /**
- * Core handler for notifications of broken paths
+ * Core handler for notifications of broken connections.
  *
  * @param cls Closure (unused).
  * @param id Peer identity of sending neighbor.
@@ -1777,18 +1768,27 @@ GMC_handle_broken (void* cls,
   c = connection_get (&msg->cid);
   if (NULL == c)
   {
-    GNUNET_break_op (0);
+    LOG (GNUNET_ERROR_TYPE_DEBUG, "  duplicate CONNECTION_BROKEN\n");
     return GNUNET_OK;
   }
 
   fwd = is_fwd (c, id);
   if (GMC_is_terminal (c, fwd))
   {
+    struct GNUNET_MessageHeader *msg;
+    struct MeshPeer *peer;
+
+    peer = get_hop (c, !fwd);
     path_invalidate (c->path);
-    if (0 < c->pending_messages)
-      c->destroy = GNUNET_YES;
-    else
-      GMC_destroy (c);
+    c->state = MESH_CONNECTION_DESTROYED;
+    while (NULL != (msg = GMP_connection_pop (peer, c)))
+    {
+      GNUNET_assert (NULL ==
+                     GMT_send_prebuilt_message (msg, c->t, NULL, GNUNET_YES,
+                                                NULL, NULL));
+    }
+
+    GMC_destroy (c);
   }
   else
   {
@@ -1898,8 +1898,8 @@ handle_mesh_encrypted (const struct GNUNET_PeerIdentity *peer,
   if (NULL == c)
   {
     GNUNET_STATISTICS_update (stats, "# unknown connection", 1, GNUNET_NO);
-    LOG (GNUNET_ERROR_TYPE_DEBUG, "WARNING enc on unknown connection %s\n",
-         GNUNET_h2s (&msg->cid));
+    LOG (GNUNET_ERROR_TYPE_DEBUG, "enc on unknown connection %s\n",
+         GNUNET_h2s (GM_h2hc (&msg->cid)));
     return GNUNET_OK;
   }
 
@@ -2023,7 +2023,7 @@ handle_mesh_kx (const struct GNUNET_PeerIdentity *peer,
   {
     GNUNET_STATISTICS_update (stats, "# unknown connection", 1, GNUNET_NO);
     LOG (GNUNET_ERROR_TYPE_DEBUG, "kx on unknown connection %s\n",
-         GNUNET_h2s (&msg->cid));
+         GNUNET_h2s (GM_h2hc (&msg->cid)));
     return GNUNET_OK;
   }
   LOG (GNUNET_ERROR_TYPE_DEBUG, " on connection %s\n", GMC_2s (c));
@@ -2226,7 +2226,7 @@ GMC_handle_poll (void *cls, const struct GNUNET_PeerIdentity *peer,
                               GNUNET_NO);
     LOG (GNUNET_ERROR_TYPE_DEBUG,
          "WARNING POLL message on unknown connection %s!\n",
-         GNUNET_h2s (&msg->cid));
+         GNUNET_h2s (GM_h2hc (&msg->cid)));
     return GNUNET_OK;
   }
 
@@ -2360,7 +2360,7 @@ GMC_init (const struct GNUNET_CONFIGURATION_Handle *c)
     return;
   }
   create_connection_time = GNUNET_TIME_UNIT_SECONDS;
-  connections = GNUNET_CONTAINER_multihashmap_create (1024, GNUNET_YES);
+  connections = GNUNET_CONTAINER_multihashmap_create (1024, GNUNET_NO);
 }
 
 
@@ -2398,7 +2398,7 @@ GMC_shutdown (void)
 
 
 struct MeshConnection *
-GMC_new (const struct GNUNET_HashCode *cid,
+GMC_new (const struct GNUNET_MeshHash *cid,
          struct MeshTunnel3 *t,
          struct MeshPeerPath *p,
          unsigned int own_pos)
@@ -2409,7 +2409,7 @@ GMC_new (const struct GNUNET_HashCode *cid,
   c->id = *cid;
   GNUNET_assert (GNUNET_OK ==
                  GNUNET_CONTAINER_multihashmap_put (connections,
-                                                    &c->id, c,
+                                                    GMC_get_h (c), c,
                                                     GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
   fc_init (&c->fwd_fc);
   fc_init (&c->bck_fc);
@@ -2501,7 +2501,8 @@ GMC_destroy (struct MeshConnection *c)
   }
 
   GNUNET_break (GNUNET_YES ==
-                GNUNET_CONTAINER_multihashmap_remove (connections, &c->id, c));
+                GNUNET_CONTAINER_multihashmap_remove (connections,
+                                                      GMC_get_h (c), c));
 
   GNUNET_STATISTICS_update (stats, "# connections", -1, GNUNET_NO);
   GNUNET_free (c);
@@ -2521,6 +2522,20 @@ GMC_get_id (const struct MeshConnection *c)
 }
 
 
+/**
+ * Get the connection ID.
+ *
+ * @param c Connection to get the ID from.
+ *
+ * @return ID of the connection.
+ */
+const struct GNUNET_HashCode *
+GMC_get_h (const struct MeshConnection *c)
+{
+  return GM_h2hc (&c->id);
+}
+
+
 /**
  * Get the connection path.
  *
@@ -3055,8 +3070,8 @@ GMC_2s (const struct MeshConnection *c)
   {
     static char buf[128];
 
-    sprintf (buf, "%s (->%s)", GNUNET_h2s (&c->id), GMT_2s (c->t));
+    sprintf (buf, "%s (->%s)", GNUNET_h2s (GM_h2hc (GMC_get_id(c))), GMT_2s (c->t));
     return buf;
   }
-  return GNUNET_h2s (&c->id);
+  return GNUNET_h2s (GM_h2hc (&c->id));
 }