- add separate encryption enabled mesh to avoid breakage during developement
[oweals/gnunet.git] / src / mesh / gnunet-service-mesh_peer.c
index 2bba99319146c41093cb9c6636854934d47204f6..515c4b4f0fdae87e218465702fca5530893d6019 100644 (file)
@@ -232,7 +232,7 @@ notify_broken (void *cls,
   struct MeshPeer *peer = cls;
   struct MeshConnection *c = value;
 
-  GMC_notify_broken (c, peer, &my_full_id);
+  GMC_notify_broken (c, peer);
 
   return GNUNET_YES;
 }
@@ -247,30 +247,30 @@ notify_broken (void *cls,
 static void
 core_connect (void *cls, const struct GNUNET_PeerIdentity *peer)
 {
-  struct MeshPeer *pi;
+  struct MeshPeer *mp;
   struct MeshPeerPath *path;
 
-  LOG ("Peer connected\n");
-  LOG ("     %s\n", GNUNET_i2s (&my_full_id));
-  pi = GMP_get (peer);
-  if (myid == pi->id)
+  LOG (GNUNET_ERROR_TYPE_DEBUG, "Peer connected\n");
+  LOG (GNUNET_ERROR_TYPE_DEBUG, "     %s\n", GNUNET_i2s (&my_full_id));
+  mp = GMP_get (peer);
+  if (myid == mp->id)
   {
-    LOG ("     (self)\n");
+    LOG (GNUNET_ERROR_TYPE_DEBUG, "     (self)\n");
     path = path_new (1);
   }
   else
   {
-    LOG ("     %s\n", GNUNET_i2s (peer));
+    LOG (GNUNET_ERROR_TYPE_DEBUG, "     %s\n", GNUNET_i2s (peer));
     path = path_new (2);
-    path->peers[1] = pi->id;
-    GNUNET_PEER_change_rc (pi->id, 1);
+    path->peers[1] = mp->id;
+    GNUNET_PEER_change_rc (mp->id, 1);
     GNUNET_STATISTICS_update (stats, "# peers", 1, GNUNET_NO);
   }
   path->peers[0] = myid;
   GNUNET_PEER_change_rc (myid, 1);
-  peer_add_path (pi, path, GNUNET_YES);
+  GMP_add_path (mp, path, GNUNET_YES);
 
-  pi->connections = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_YES);
+  mp->connections = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_YES);
   return;
 }
 
@@ -286,7 +286,7 @@ core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
 {
   struct MeshPeer *pi;
 
-  LOG ("Peer disconnected\n");
+  LOG (GNUNET_ERROR_TYPE_DEBUG, "Peer disconnected\n");
   pi = GNUNET_CONTAINER_multipeermap_get (peers, peer);
   if (NULL == pi)
   {
@@ -304,7 +304,7 @@ core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
     }
   if (myid == pi->id)
   {
-    LOG ("     (self)\n");
+    LOG (GNUNET_ERROR_TYPE_DEBUG, "     (self)\n");
   }
   GNUNET_STATISTICS_update (stats, "# peers", -1, GNUNET_NO);
 
@@ -332,8 +332,7 @@ static struct GNUNET_CORE_MessageHandler core_handlers[] = {
     sizeof (struct GNUNET_MESH_ACK)},
   {&GMC_handle_poll, GNUNET_MESSAGE_TYPE_MESH_POLL,
     sizeof (struct GNUNET_MESH_Poll)},
-  {&GMC_handle_fwd, GNUNET_MESSAGE_TYPE_MESH_FWD, 0},
-  {&GMC_handle_bck, GNUNET_MESSAGE_TYPE_MESH_BCK, 0},
+  {&GMC_handle_encrypted, GNUNET_MESSAGE_TYPE_MESH_ENCRYPTED, 0},
   {NULL, 0, 0}
 };
 
@@ -417,7 +416,7 @@ send_core_connection_create (struct MeshConnection *c, size_t size, void *buf)
 {
   struct GNUNET_MESH_ConnectionCreate *msg;
   struct GNUNET_PeerIdentity *peer_ptr;
-  struct MeshPeerPath *p = c->path;
+  const struct MeshPeerPath *p = GMC_get_path (c);
   size_t size_needed;
   int i;
 
@@ -434,7 +433,7 @@ send_core_connection_create (struct MeshConnection *c, size_t size, void *buf)
   msg = (struct GNUNET_MESH_ConnectionCreate *) buf;
   msg->header.size = htons (size_needed);
   msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE);
-  msg->cid = c->id;
+  msg->cid = *GMC_get_id (c);
 
   peer_ptr = (struct GNUNET_PeerIdentity *) &msg[1];
   for (i = 0; i < p->length; i++)
@@ -442,8 +441,9 @@ send_core_connection_create (struct MeshConnection *c, size_t size, void *buf)
     GNUNET_PEER_resolve (p->peers[i], peer_ptr++);
   }
 
-  LOG (GNUNET_ERROR_TYPE_DEBUG,
-              "CONNECTION CREATE (%u bytes long) sent!\n", size_needed);
+  LOG (GNUNET_ERROR_TYPE_DEBUG, 
+       "CONNECTION CREATE (%u bytes long) sent!\n",
+       size_needed);
   return size_needed;
 }
 
@@ -461,10 +461,8 @@ static size_t
 send_core_connection_ack (struct MeshConnection *c, size_t size, void *buf)
 {
   struct GNUNET_MESH_ConnectionACK *msg = buf;
-  struct MeshTunnel3 *t = c->t;
 
   LOG (GNUNET_ERROR_TYPE_DEBUG, "Sending CONNECTION ACK...\n");
-  GNUNET_assert (NULL != t);
   if (sizeof (struct GNUNET_MESH_ConnectionACK) > size)
   {
     GNUNET_break (0);
@@ -472,7 +470,7 @@ send_core_connection_ack (struct MeshConnection *c, size_t size, void *buf)
   }
   msg->header.size = htons (sizeof (struct GNUNET_MESH_ConnectionACK));
   msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK);
-  msg->cid = c->id;
+  msg->cid = *GMC_get_id (c);
   msg->reserved = 0;
 
   /* TODO add signature */
@@ -534,21 +532,21 @@ peer_destroy (struct MeshPeer *peer)
     LOG (GNUNET_ERROR_TYPE_WARNING,
                 "removing peer %s, not in peermap\n", GNUNET_i2s (&id));
   }
-    if (NULL != peer->search_h)
-    {
-      GMD_search_stop (peer->search_h);
-    }
-      p = peer->path_head;
-      while (NULL != p)
-      {
-        nextp = p->next;
-        GNUNET_CONTAINER_DLL_remove (peer->path_head, peer->path_tail, p);
-        path_destroy (p);
-        p = nextp;
-      }
-        tunnel_destroy_empty (peer->tunnel);
-        GNUNET_free (peer);
-        return GNUNET_OK;
+  if (NULL != peer->search_h)
+  {
+    GMD_search_stop (peer->search_h);
+  }
+  p = peer->path_head;
+  while (NULL != p)
+  {
+    nextp = p->next;
+    GNUNET_CONTAINER_DLL_remove (peer->path_head, peer->path_tail, p);
+    path_destroy (p);
+    p = nextp;
+  }
+  GMT_destroy_empty (peer->tunnel);
+  GNUNET_free (peer);
+  return GNUNET_OK;
 }
 
 
@@ -646,47 +644,6 @@ peer_delete_oldest (void)
 }
 
 
-/**
- * Get a cost of a path for a peer considering existing tunnel connections.
- *
- * @param peer Peer towards which the path is considered.
- * @param path Candidate path.
- *
- * @return Cost of the path (path length + number of overlapping nodes)
- */
-static unsigned int
-peer_get_path_cost (const struct MeshPeer *peer,
-                    const struct MeshPeerPath *path)
-{
-  struct MeshConnection *c;
-  unsigned int overlap;
-  unsigned int i;
-  unsigned int j;
-
-  if (NULL == path)
-    return 0;
-
-  overlap = 0;
-  GNUNET_assert (NULL != peer->tunnel);
-
-  for (i = 0; i < path->length; i++)
-  {
-    for (c = peer->tunnel->connection_head; NULL != c; c = c->next)
-    {
-      for (j = 0; j < c->path->length; j++)
-      {
-        if (path->peers[i] == c->path->peers[j])
-        {
-          overlap++;
-          break;
-        }
-      }
-    }
-  }
-  return (path->length + overlap) * (path->score * -1);
-}
-
-
 /**
  * Choose the best path towards a peer considering the tunnel properties.
  *
@@ -709,7 +666,7 @@ peer_get_best_path (const struct MeshPeer *peer)
     if (GNUNET_YES == GMT_is_path_used (peer->tunnel, p))
       continue; /* If path is already in use, skip it. */
 
-    if ((cost = peer_get_path_cost (peer, p)) < best_cost)
+    if ((cost = GMT_get_path_cost (peer->tunnel, p)) < best_cost)
     {
       best_cost = cost;
       best_p = p;
@@ -816,8 +773,7 @@ queue_destroy (struct MeshPeerQueue *queue, int clear_cls)
       case GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY:
         LOG (GNUNET_ERROR_TYPE_INFO, "destroying a DESTROY message\n");
         /* fall through */
-      case GNUNET_MESSAGE_TYPE_MESH_FWD:
-      case GNUNET_MESSAGE_TYPE_MESH_BCK:
+      case GNUNET_MESSAGE_TYPE_MESH_ENCRYPTED:
       case GNUNET_MESSAGE_TYPE_MESH_ACK:
       case GNUNET_MESSAGE_TYPE_MESH_POLL:
       case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK:
@@ -914,8 +870,7 @@ queue_send (void *cls, size_t size, void *buf)
     case GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY:
     case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_DESTROY:
     case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_BROKEN:
-    case GNUNET_MESSAGE_TYPE_MESH_FWD:
-    case GNUNET_MESSAGE_TYPE_MESH_BCK:
+    case GNUNET_MESSAGE_TYPE_MESH_ENCRYPTED:
     case GNUNET_MESSAGE_TYPE_MESH_ACK:
     case GNUNET_MESSAGE_TYPE_MESH_POLL:
       LOG (GNUNET_ERROR_TYPE_DEBUG,
@@ -1206,6 +1161,7 @@ GMP_queue_unlock (struct MeshPeer *peer, struct MeshConnection *c)
 void
 GMP_init (const struct GNUNET_CONFIGURATION_Handle *c)
 {
+  LOG (GNUNET_ERROR_TYPE_DEBUG, "init\n");
   peers = GNUNET_CONTAINER_multipeermap_create (128, GNUNET_NO);
   if (GNUNET_OK !=
       GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_PEERS",
@@ -1431,6 +1387,7 @@ GMP_is_neighbor (const struct MeshPeer *peer)
 
 /**
  * Create and initialize a new tunnel towards a peer, in case it has none.
+ * In case the peer already has a tunnel, nothing is done.
  *
  * Does not generate any traffic, just creates the local data structures.
  *
@@ -1630,6 +1587,45 @@ GMP_remove_connection (struct MeshPeer *peer,
                                                c);
 }
 
+/**
+ * Start the DHT search for new paths towards the peer: we don't have
+ * enough good connections.
+ *
+ * @param peer Destination peer.
+ */
+void
+GMP_start_search (struct MeshPeer *peer)
+{
+  if (NULL != peer->search_h)
+  {
+    GNUNET_break (0);
+    return;
+  }
+
+  peer->search_h = GMD_search (GMP_get_id (peer), &search_handler, peer);
+}
+
+
+/**
+ * Stop the DHT search for new paths towards the peer: we already have
+ * enough good connections.
+ *
+ * @param peer Destination peer.
+ */
+void
+GMP_stop_search (struct MeshPeer *peer)
+{
+  if (NULL == peer->search_h)
+  {
+    GNUNET_break (0);
+    return;
+  }
+
+  GMD_search_stop (peer->search_h);
+  peer->search_h = NULL;
+}
+
+
 /**
  * Get the Full ID of a peer.
  *
@@ -1658,6 +1654,20 @@ GMP_get_short_id (const struct MeshPeer *peer)
 }
 
 
+/**
+ * Get the tunnel towards a peer.
+ *
+ * @param peer Peer to get from.
+ *
+ * @return Tunnel towards peer.
+ */
+struct MeshTunnel3 *
+GMP_get_tunnel (const struct MeshPeer *peer)
+{
+  return peer->tunnel;
+}
+
+
 /**
  * Get the static string for a peer ID.
  *