-bump name to 97
[oweals/gnunet.git] / src / mesh / gnunet-service-mesh_tunnel.c
index a1569b5d4d5bc96fbb7c295d89567d84b45db309..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(level,"mesh-tun",__VA_ARGS__)
 
 /**
  * All the states a tunnel can be in.
@@ -177,6 +181,11 @@ struct MeshTunnelQueue
 /*******************************   GLOBALS  ***********************************/
 /******************************************************************************/
 
+/**
+ * Global handle to the statistics service.
+ */
+extern struct GNUNET_STATISTICS_Handle *stats;
+
 /**
  * Default TTL for payload packets.
  */
@@ -202,7 +211,6 @@ const static struct GNUNET_CRYPTO_EccPrivateKey *my_private_key;
 /********************************   STATIC  ***********************************/
 /******************************************************************************/
 
-
 /**
  * Get string description for tunnel state.
  *
@@ -251,14 +259,14 @@ tunnel_get_connection (struct MeshTunnel2 *t, int fwd)
   struct MeshFlowControl *fc;
   unsigned int lowest_q;
 
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_get_connection %s\n",
+  LOG (GNUNET_ERROR_TYPE_DEBUG, "tunnel_get_connection %s\n",
               peer2s (t->peer));
   best = NULL;
   lowest_q = UINT_MAX;
   for (c = t->connection_head; NULL != c; c = c->next)
   {
-    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "  connection %s: %u\n",
-                GNUNET_h2s (&c->id), c->state);
+    LOG (GNUNET_ERROR_TYPE_DEBUG, "  connection %s: %u\n",
+                GNUNET_h2s (GMC_get_id (c)), c->state);
     if (MESH_CONNECTION_READY == c->state)
     {
       fc = fwd ? &c->fwd_fc : &c->bck_fc;
@@ -267,7 +275,7 @@ tunnel_get_connection (struct MeshTunnel2 *t, int fwd)
         GNUNET_break (0);
         continue;
       }
-      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "    q_n %u, \n", fc->queue_n);
+      LOG (GNUNET_ERROR_TYPE_DEBUG, "    q_n %u, \n", fc->queue_n);
       if (fc->queue_n < lowest_q)
       {
         best = c;
@@ -346,11 +354,11 @@ tunnel_send_queued_data (struct MeshTunnel2 *t, int fwd)
   struct MeshTunnelQueue *next;
   unsigned int room;
 
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+  LOG (GNUNET_ERROR_TYPE_DEBUG,
               "tunnel_send_queued_data on tunnel %s\n",
               peer2s (t->peer));
   room = tunnel_get_buffer (t, fwd);
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "  buffer space: %u\n", room);
+  LOG (GNUNET_ERROR_TYPE_DEBUG, "  buffer space: %u\n", room);
   for (tq = t->tq_head; NULL != tq && room > 0; tq = next)
   {
     next = tq->next;
@@ -364,6 +372,10 @@ tunnel_send_queued_data (struct MeshTunnel2 *t, int fwd)
 }
 
 
+/******************************************************************************/
+/********************************    API    ***********************************/
+/******************************************************************************/
+
 /**
  * Cache a message to be sent once tunnel is online.
  *
@@ -392,11 +404,6 @@ GMT_queue_data (struct MeshTunnel2 *t,
 }
 
 
-
-/******************************************************************************/
-/********************************    API    ***********************************/
-/******************************************************************************/
-
 /**
  * Initialize the tunnel subsystem.
  *
@@ -413,7 +420,7 @@ GMT_init (const struct GNUNET_CONFIGURATION_Handle *c,
       GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DEFAULT_TTL",
                                              &default_ttl))
   {
-    GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_WARNING,
+    LOG_config_invalid (GNUNET_ERROR_TYPE_WARNING,
                                "MESH", "DEFAULT_TTL", "USING DEFAULT");
     default_ttl = 64;
   }
@@ -483,11 +490,11 @@ GMT_change_state (struct MeshTunnel2* t, enum MeshTunnelState state)
 {
   if (NULL == t)
     return;
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+  LOG (GNUNET_ERROR_TYPE_DEBUG,
               "Tunnel %s state was %s\n",
               peer2s (t->peer),
               GNUNET_MESH_DEBUG_TS2S (t->state));
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+  LOG (GNUNET_ERROR_TYPE_DEBUG,
               "Tunnel %s state is now %s\n",
               peer2s (t->peer),
               GNUNET_MESH_DEBUG_TS2S (state));
@@ -548,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);
@@ -576,7 +583,7 @@ GMT_destroy (struct MeshTunnel2 *t)
   if (NULL == t)
     return;
 
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "destroying tunnel %s\n",
+  LOG (GNUNET_ERROR_TYPE_DEBUG, "destroying tunnel %s\n",
               peer2s (t->peer));
 
 //   if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (tunnels, &t->id, t))
@@ -589,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);
 }
@@ -611,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_log (GNUNET_ERROR_TYPE_DEBUG,
-                  "end-to-end message not known (%u)\n",
-                  ntohs (msgh->type));
+      GNUNET_break_op (0);
+      LOG (GNUNET_ERROR_TYPE_DEBUG,
+           "end-to-end message not known (%u)\n",
+           ntohs (msgh->type));
   }
 }
 
@@ -701,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)
@@ -710,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;
 }
 
@@ -827,7 +806,7 @@ GMT_send_prebuilt_message (struct GNUNET_MESH_Encrypted *msg,
   struct MeshConnection *c;
   uint16_t type;
 
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Send on Tunnel %s\n",
+  LOG (GNUNET_ERROR_TYPE_DEBUG, "Send on Tunnel %s\n",
               peer2s (t->peer));
   c = tunnel_get_connection (t, fwd);
   if (NULL == c)
@@ -842,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:
-      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "unkown type %s\n",
-                  GNUNET_MESH_DEBUG_M2S (type));
+      LOG (GNUNET_ERROR_TYPE_DEBUG, "unkown type %s\n",
+           GNUNET_MESH_DEBUG_M2S (type));
       GNUNET_break (0);
   }
   msg->reserved = 0;