Fixed a memory leak when receiving a second create path for the same tunnel
[oweals/gnunet.git] / src / mesh / gnunet-service-mesh.c
index 97593d3bbd04a32049bedf6270dbd9a08198da65..9d3784567842a9832f81a28a154b59061c3e334b 100644 (file)
@@ -603,7 +603,7 @@ announce_id (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
    * - Adapt X to churn
    */
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: DHT_put for ID %s started.\n",
-              GNUNET_h2s_full (&my_full_id.hashPubKey));
+              GNUNET_i2s (&my_full_id));
   GNUNET_DHT_put (dht_handle,   /* DHT handle */
                   &my_full_id.hashPubKey,       /* Key to use */
                   10U,          /* Replication level */
@@ -1130,7 +1130,7 @@ send_core_data_raw (void *cls, size_t size, void *buf)
  * Sends an already built message to a peer, properly registrating
  * all used resources.
  *
- * @param message Message to send.
+ * @param message Message to send. Fucntion makes a copy of it.
  * @param peer Short ID of the neighbor whom to send the message.
  */
 static void
@@ -1236,10 +1236,7 @@ send_create_path (struct MeshPeerInfo *peer,
 static void
 send_destroy_path (struct MeshTunnel *t, GNUNET_PEER_Id destination)
 {
-  struct GNUNET_MESH_ManipulatePath *msg;
-  struct GNUNET_PeerIdentity *pi;
   struct MeshPeerPath *p;
-  unsigned int i;
   size_t size;
 
   p = tree_get_path_to_peer(t->tree, destination);
@@ -1250,16 +1247,23 @@ send_destroy_path (struct MeshTunnel *t, GNUNET_PEER_Id destination)
   }
   size = sizeof (struct GNUNET_MESH_ManipulatePath);
   size += p->length * sizeof (struct GNUNET_PeerIdentity);
-  msg = GNUNET_malloc (size);
-  msg->header.size = htons (size);
-  msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY);
-  msg->tid = htonl (t->id.tid);
-  pi = (struct GNUNET_PeerIdentity *) &msg[1];
-  for (i = 0; i < p->length; i++)
   {
-    GNUNET_PEER_resolve(p->peers[i], &pi[i]);
+    struct GNUNET_MESH_ManipulatePath *msg;
+    struct GNUNET_PeerIdentity *pi;
+    char cbuf[size];
+    unsigned int i;
+
+    msg = (struct GNUNET_MESH_ManipulatePath *) cbuf;
+    msg->header.size = htons (size);
+    msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY);
+    msg->tid = htonl (t->id.tid);
+    pi = (struct GNUNET_PeerIdentity *) &msg[1];
+    for (i = 0; i < p->length; i++)
+    {
+      GNUNET_PEER_resolve(p->peers[i], &pi[i]);
+    }
+    send_message (&msg->header, path_get_first_hop(t->tree, destination));
   }
-  send_message (&msg->header, path_get_first_hop(t->tree, destination));
   path_destroy (p);
 }
 
@@ -1282,6 +1286,12 @@ peer_info_connect (struct MeshPeerInfo *peer, struct MeshTunnel *t)
   if (NULL != peer->path_head)
   {
     p = tree_get_path_to_peer(t->tree, peer->id);
+    if (NULL == p)
+    {
+      GNUNET_break (0);
+      return;
+    }
+
     if (p->length > 1)
     {
       send_create_path(peer, p, t);
@@ -1321,6 +1331,28 @@ peer_info_connect (struct MeshPeerInfo *peer, struct MeshTunnel *t)
 }
 
 
+/**
+ * Task to delay the connection of a peer
+ *
+ * @param cls Closure (path info with tunnel and peer to connect).
+ *            Will be free'd on exection.
+ * @param tc TaskContext
+ */
+static void
+peer_info_connect_task (void *cls,
+                        const struct GNUNET_SCHEDULER_TaskContext *tc)
+{
+  struct MeshPathInfo *path_info = cls;
+
+  if (GNUNET_SCHEDULER_REASON_SHUTDOWN == tc->reason)
+  {
+    GNUNET_free (cls);
+  }
+  peer_info_connect (path_info->peer, path_info->t);
+  GNUNET_free (cls);
+}
+
+
 /**
  * Destroy the peer_info and free any allocated resources linked to it
  * 
@@ -1470,9 +1502,19 @@ path_add_to_peer (struct MeshPeerInfo *peer_info, struct MeshPeerPath *path)
   unsigned int l;
   unsigned int l2;
 
+#if MESH_DEBUG
+  struct GNUNET_PeerIdentity id;
+
+  GNUNET_PEER_resolve (peer_info->id, &id);
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+              "MESH: adding path [%u] to peer %s\n",
+              path->length,
+              GNUNET_i2s (&id));
+#endif
   if (NULL == peer_info || NULL == path)
   {
     GNUNET_break (0);
+    path_destroy (path);
     return;
   }
 
@@ -1489,7 +1531,10 @@ path_add_to_peer (struct MeshPeerInfo *peer_info, struct MeshPeerPath *path)
     if (l2 > l)
     {
       GNUNET_CONTAINER_DLL_insert_before (peer_info->path_head,
-                                          peer_info->path_tail, aux, path);
+                                          peer_info->path_tail,
+                                          aux,
+                                          path);
+      return;
     }
     else
     {
@@ -1532,9 +1577,9 @@ path_add_to_origin (struct MeshPeerInfo *peer_info, struct MeshPeerPath *path)
  */
 static struct MeshPeerPath *
 path_build_from_dht (const struct GNUNET_PeerIdentity *get_path,
-                    unsigned int get_path_length,
+                     unsigned int get_path_length,
                      const struct GNUNET_PeerIdentity *put_path,
-                    unsigned int put_path_length)
+                     unsigned int put_path_length)
 {
   struct MeshPeerPath *p;
   GNUNET_PEER_Id id;
@@ -1595,11 +1640,11 @@ path_build_from_dht (const struct GNUNET_PeerIdentity *get_path,
   if (get_path_length > 0)
     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                 "MESH:    (first of GET: %s)\n",
-                GNUNET_h2s_full(&get_path[0].hashPubKey));
+                GNUNET_i2s(&get_path[0]));
   if (put_path_length > 0)
     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                 "MESH:    (first of PUT: %s)\n",
-                GNUNET_h2s_full(&put_path[0].hashPubKey));
+                GNUNET_i2s(&put_path[0]));
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
               "MESH:    In total: %d hops\n",
               p->length);
@@ -1611,7 +1656,7 @@ path_build_from_dht (const struct GNUNET_PeerIdentity *get_path,
     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
               "MESH:        %u: %s\n",
               p->peers[i],
-              GNUNET_h2s_full(&peer_id.hashPubKey));
+              GNUNET_i2s(&peer_id));
   }
 #endif
   return p;
@@ -1701,6 +1746,7 @@ void
 notify_peer_disconnected (const struct MeshTunnelTreeNode *n)
 {
   struct MeshPeerInfo *peer;
+  struct MeshPathInfo *path_info;
 
   if (NULL != n->t->client && NULL != nc)
   {
@@ -1713,7 +1759,10 @@ notify_peer_disconnected (const struct MeshTunnelTreeNode *n)
                                                 &msg.header, GNUNET_NO);
   }
   peer = peer_info_get_short(n->peer);
-  peer_info_connect(peer, n->t);
+  path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
+  path_info->peer = peer;
+  path_info->t = n->t;
+  GNUNET_SCHEDULER_add_now(&peer_info_connect_task, path_info);
 }
 
 
@@ -2481,7 +2530,8 @@ handle_mesh_path_create (void *cls, const struct GNUNET_PeerIdentity *peer,
     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                 "MESH:   It's for us!\n");
     path_add_to_origin (orig_peer_info, path);
-    t->peers = GNUNET_CONTAINER_multihashmap_create(4);
+    if (NULL == t->peers)
+      t->peers = GNUNET_CONTAINER_multihashmap_create(4);
     GNUNET_break (GNUNET_OK == GNUNET_CONTAINER_multihashmap_put (
         t->peers,
         &my_full_id.hashPubKey,
@@ -2605,6 +2655,7 @@ handle_mesh_path_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
   if (own_pos < path->length - 1)
     send_message (message, &pi[own_pos + 1]);
   tunnel_delete_peer (t, path->peers[path->length - 1]);
+  path_destroy (path);
   return GNUNET_OK;
 }
 
@@ -3145,7 +3196,7 @@ dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
   GNUNET_PEER_resolve (path_info->peer->id, &pi);
   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
              "MESH:   for %s\n",
-             GNUNET_h2s_full(&pi.hashPubKey));
+             GNUNET_i2s(&pi));
 //   GNUNET_DHT_get_stop(path_info->peer->dhtget);
 //   path_info->peer->dhtget = NULL;
 
@@ -3235,7 +3286,6 @@ handle_local_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH:    (SERVER DOWN)\n");
     return;
   }
-  GNUNET_SERVER_client_drop (client);
   c = clients;
   while (NULL != c)
   {
@@ -3246,6 +3296,7 @@ handle_local_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
       continue;
     }
     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: matching client found\n");
+    GNUNET_SERVER_client_drop (c->handle);
     if (NULL != c->tunnels)
     {
       GNUNET_CONTAINER_multihashmap_iterate (c->tunnels,
@@ -3301,7 +3352,6 @@ handle_local_new_client (void *cls, struct GNUNET_SERVER_Client *client,
   uint16_t i;
 
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: new client connected\n");
-  GNUNET_SERVER_client_keep (client);
   /* Check data sanity */
   size = ntohs (message->size) - sizeof (struct GNUNET_MESH_ClientConnect);
   cc_msg = (struct GNUNET_MESH_ClientConnect *) message;
@@ -3322,6 +3372,7 @@ handle_local_new_client (void *cls, struct GNUNET_SERVER_Client *client,
 #endif
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH:   CLIENT NEW %u at %p\n", c->id, c);
   c->handle = client;
+  GNUNET_SERVER_client_keep (client);
   a = (GNUNET_MESH_ApplicationType *) &cc_msg[1];
   if (napps > 0)
   {
@@ -3588,7 +3639,7 @@ handle_local_connect_add (void *cls, struct GNUNET_SERVER_Client *client,
     return;
   }
   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "MESH:      for %s\n",
-             GNUNET_h2s_full(&peer_msg->peer.hashPubKey));
+             GNUNET_i2s(&peer_msg->peer));
   peer_info = peer_info_get (&peer_msg->peer);
 
   tunnel_add_peer(t, peer_info);
@@ -3757,7 +3808,7 @@ handle_local_connect_by_type (void *cls, struct GNUNET_SERVER_Client *client,
     GNUNET_DHT_get_stop (t->dht_get_type);
   }
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH:  looking in DHT for %s\n",
-              GNUNET_h2s_full (&hash));
+              GNUNET_h2s (&hash));
   t->dht_get_type =
       GNUNET_DHT_get_start (dht_handle,
                             GNUNET_TIME_UNIT_FOREVER_REL,
@@ -4092,7 +4143,7 @@ core_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
 #if MESH_DEBUG_CONNECTION
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH: Peer connected\n");
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH:      %s\n",
-              GNUNET_h2s(&my_full_id.hashPubKey));
+              GNUNET_i2s(&my_full_id));
 #endif
   peer_info = peer_info_get (peer);
   if (myid == peer_info->id)
@@ -4106,7 +4157,7 @@ core_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
   else
   {
     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MESH:      %s\n",
-                GNUNET_h2s(&peer->hashPubKey));
+                GNUNET_i2s(peer));
   }
 #endif
   path = path_new (2);