-bump name to 97
[oweals/gnunet.git] / src / mesh / gnunet-service-mesh_tunnel.c
index 6da299f8ad6c8f620f179960b4235f66dfaf291f..44ac78567a9d453eef55ec5c79eda6933da6b6a2 100644 (file)
 #include "platform.h"
 #include "gnunet_util_lib.h"
 
+#include "gnunet_statistics_service.h"
+
 #include "mesh_protocol_enc.h"
 
 #include "gnunet-service-mesh_tunnel.h"
 #include "gnunet-service-mesh_connection.h"
 #include "gnunet-service-mesh_channel.h"
+#include "gnunet-service-mesh_peer.h"
 #include "mesh_path.h"
 
-#define LOG (level, ...) GNUNET_log_from ("mesh-tun", level, __VA_ARGS__)
+#define LOG(level, ...) GNUNET_log_from(level,"mesh-tun",__VA_ARGS__)
 
 /**
  * All the states a tunnel can be in.
@@ -178,6 +181,11 @@ struct MeshTunnelQueue
 /*******************************   GLOBALS  ***********************************/
 /******************************************************************************/
 
+/**
+ * Global handle to the statistics service.
+ */
+extern struct GNUNET_STATISTICS_Handle *stats;
+
 /**
  * Default TTL for payload packets.
  */
@@ -203,7 +211,6 @@ const static struct GNUNET_CRYPTO_EccPrivateKey *my_private_key;
 /********************************   STATIC  ***********************************/
 /******************************************************************************/
 
-
 /**
  * Get string description for tunnel state.
  *
@@ -259,7 +266,7 @@ tunnel_get_connection (struct MeshTunnel2 *t, int fwd)
   for (c = t->connection_head; NULL != c; c = c->next)
   {
     LOG (GNUNET_ERROR_TYPE_DEBUG, "  connection %s: %u\n",
-                GNUNET_h2s (&c->id), c->state);
+                GNUNET_h2s (GMC_get_id (c)), c->state);
     if (MESH_CONNECTION_READY == c->state)
     {
       fc = fwd ? &c->fwd_fc : &c->bck_fc;
@@ -365,6 +372,10 @@ tunnel_send_queued_data (struct MeshTunnel2 *t, int fwd)
 }
 
 
+/******************************************************************************/
+/********************************    API    ***********************************/
+/******************************************************************************/
+
 /**
  * Cache a message to be sent once tunnel is online.
  *
@@ -393,11 +404,6 @@ GMT_queue_data (struct MeshTunnel2 *t,
 }
 
 
-
-/******************************************************************************/
-/********************************    API    ***********************************/
-/******************************************************************************/
-
 /**
  * Initialize the tunnel subsystem.
  *
@@ -549,7 +555,7 @@ GMT_destroy_empty (struct MeshTunnel2 *t)
 void
 GMT_destroy_if_empty (struct MeshTunnel2 *t)
 {
-  if (1 <= GMCH_count (t->channel_head))
+  if (1 < GMCH_count (t->channel_head))
     return;
 
   GMT_destroy_empty (t);
@@ -590,7 +596,7 @@ GMT_destroy (struct MeshTunnel2 *t)
   }
 
   GNUNET_STATISTICS_update (stats, "# tunnels", -1, GNUNET_NO);
-  t->peer->tunnel = NULL;
+  GMP_set_tunnel (t->peer, NULL);
 
   GNUNET_free (t);
 }
@@ -612,35 +618,36 @@ GMT_handle_decrypted (struct MeshTunnel2 *t,
   {
     case GNUNET_MESSAGE_TYPE_MESH_DATA:
       /* Don't send hop ACK, wait for client to ACK */
-      handle_data (t, (struct GNUNET_MESH_Data *) msgh, fwd);
+      GMCH_handle_data (t, (struct GNUNET_MESH_Data *) msgh, fwd);
       break;
 
     case GNUNET_MESSAGE_TYPE_MESH_DATA_ACK:
-      handle_data_ack (t, (struct GNUNET_MESH_DataACK *) msgh, fwd);
+      GMCH_handle_data_ack (t, (struct GNUNET_MESH_DataACK *) msgh, fwd);
       break;
 
     case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE:
-      handle_channel_create (t,
-                             (struct GNUNET_MESH_ChannelCreate *) msgh,
-                             fwd);
+      GMCH_handle_create (t,
+                          (struct GNUNET_MESH_ChannelCreate *) msgh,
+                          fwd);
       break;
 
     case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_ACK:
-      handle_channel_ack (t,
-                          (struct GNUNET_MESH_ChannelManage *) msgh,
-                          fwd);
+      GMCH_handle_ack (t,
+                       (struct GNUNET_MESH_ChannelManage *) msgh,
+                       fwd);
       break;
 
     case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_DESTROY:
-      handle_channel_destroy (t,
-                              (struct GNUNET_MESH_ChannelManage *) msgh,
-                              fwd);
+      GMCH_handle_destroy (t,
+                           (struct GNUNET_MESH_ChannelManage *) msgh,
+                           fwd);
       break;
 
     default:
+      GNUNET_break_op (0);
       LOG (GNUNET_ERROR_TYPE_DEBUG,
-                  "end-to-end message not known (%u)\n",
-                  ntohs (msgh->type));
+           "end-to-end message not known (%u)\n",
+           ntohs (msgh->type));
   }
 }
 
@@ -702,7 +709,6 @@ GMT_use_path (struct MeshTunnel2 *t, struct MeshPeerPath *p)
 {
   struct MeshConnection *c;
   struct GNUNET_HashCode cid;
-  struct MeshPeer *peer;
   unsigned int own_pos;
 
   if (NULL == t || NULL == p)
@@ -711,48 +717,20 @@ GMT_use_path (struct MeshTunnel2 *t, struct MeshPeerPath *p)
     return NULL;
   }
 
-  GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_NONCE, &cid);
-
-  c = GMC_new (&cid);
-  c->t = t;
-  GNUNET_CONTAINER_DLL_insert (t->connection_head, t->connection_tail, c);
   for (own_pos = 0; own_pos < p->length; own_pos++)
   {
-    if (p->peers[own_pos] == myid)
+    if (p->peers[own_pos] == my_short_id)
       break;
   }
   if (own_pos > p->length - 1)
   {
     GNUNET_break (0);
-    connection_destroy (c);
     return NULL;
   }
-  c->own_pos = own_pos;
-  c->path = p;
-
-  if (0 == own_pos)
-  {
-    c->fwd_maintenance_task =
-        GNUNET_SCHEDULER_add_delayed (refresh_connection_time,
-                                      &connection_fwd_keepalive, c);
-  }
 
-  peer = connection_get_next_hop (c);
-  if (NULL == peer->connections)
-  {
-    connection_destroy (c);
-    return NULL;
-  }
-  GNUNET_CONTAINER_multihashmap_put (peer->connections, &c->id, c,
-                                     GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
-  peer = connection_get_prev_hop (c);
-  if (NULL == peer->connections)
-  {
-    connection_destroy (c);
-    return NULL;
-  }
-  GNUNET_CONTAINER_multihashmap_put (peer->connections, &c->id, c,
-                                     GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
+  GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_NONCE, &cid);
+  c = GMC_new (&cid, t, p, own_pos);
+  GNUNET_CONTAINER_DLL_insert (t->connection_head, t->connection_tail, c);
   return c;
 }
 
@@ -843,12 +821,12 @@ GMT_send_prebuilt_message (struct GNUNET_MESH_Encrypted *msg,
     case GNUNET_MESSAGE_TYPE_MESH_BCK:
     case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE:
     case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_DESTROY:
-      msg->cid = c->id;
+      msg->cid = *GMC_get_id (c);
       msg->ttl = htonl (default_ttl);
       break;
     default:
       LOG (GNUNET_ERROR_TYPE_DEBUG, "unkown type %s\n",
-                  GNUNET_MESH_DEBUG_M2S (type));
+           GNUNET_MESH_DEBUG_M2S (type));
       GNUNET_break (0);
   }
   msg->reserved = 0;