- fix segfault
[oweals/gnunet.git] / src / mesh / gnunet-service-mesh_connection.c
index 94813adf24efe74a88d0261ae375cee503cf54f5..e966e7192f50417bc92583c84c84e3246c198bb8 100644 (file)
@@ -354,7 +354,7 @@ connection_change_state (struct MeshConnection* c,
 /**
  * Callback called when a queued message is sent.
  *
- * Calculates the average time 
+ * Calculates the average time and connection packet tracking.
  *
  * @param cls Closure.
  * @param c Connection this message was on.
@@ -373,8 +373,29 @@ message_sent (void *cls,
   struct MeshFlowControl *fc;
   double usecsperbyte;
 
+  fc = fwd ? &c->fwd_fc : &c->bck_fc;
+  LOG (GNUNET_ERROR_TYPE_DEBUG, "!  Q_N- %p %u\n", fc, fc->queue_n);
+  fc->queue_n--;
+  c->pending_messages--;
+  if (GNUNET_YES == c->destroy && 0 == c->pending_messages)
+  {
+    LOG (GNUNET_ERROR_TYPE_DEBUG, "!  destroying connection!\n");
+    GMC_destroy (c);
+  }
+  /* Send ACK if needed, after accounting for sent ID in fc->queue_n */
+  switch (type)
+  {
+    case GNUNET_MESSAGE_TYPE_MESH_ENCRYPTED:
+      fc->last_pid_sent++;
+      LOG (GNUNET_ERROR_TYPE_DEBUG, "!   accounting pid %u\n", fc->last_pid_sent);
+//       send_ack (c, ch, fwd);
+      break;
+    default:
+      break;
+  }
+
   if (NULL == c->perf)
-    return; /* Only endpoints are interested in this. */
+    return; /* Only endpoints are interested in timing. */
 
   LOG (GNUNET_ERROR_TYPE_DEBUG, "!  message sent!\n");
   p = c->perf;
@@ -397,27 +418,6 @@ message_sent (void *cls,
   }
   p->idx = (p->idx + 1) % AVG_MSGS;
 
-  fc = fwd ? &c->fwd_fc : &c->bck_fc;
-  LOG (GNUNET_ERROR_TYPE_DEBUG, "!  Q_N- %p %u\n", fc, fc->queue_n);
-  fc->queue_n--;
-  c->pending_messages--;
-  if (GNUNET_YES == c->destroy && 0 == c->pending_messages)
-  {
-    LOG (GNUNET_ERROR_TYPE_DEBUG, "!  destroying connection!\n");
-    GMC_destroy (c);
-  }
-  /* Send ACK if needed, after accounting for sent ID in fc->queue_n */
-  switch (type)
-  {
-    case GNUNET_MESSAGE_TYPE_MESH_FWD:
-    case GNUNET_MESSAGE_TYPE_MESH_BCK:
-      fc->last_pid_sent++;
-      LOG (GNUNET_ERROR_TYPE_DEBUG, "!   accounting pid %u\n", fc->last_pid_sent);
-//       send_ack (c, ch, fwd);
-      break;
-    default:
-      break;
-  }
 //   if (NULL != c->t)
 //   {
 //     c->t->pending_messages--;
@@ -516,7 +516,8 @@ connection_send_ack (struct MeshConnection *c, unsigned int buffer, int fwd)
               fwd ? "FWD" : "BCK", GNUNET_h2s (&c->id));
 
   /* Check if we need to transmit the ACK */
-  if (prev_fc->last_ack_sent - prev_fc->last_pid_recv > 3)
+  delta = prev_fc->last_ack_sent - prev_fc->last_pid_recv;
+  if (3 < delta && buffer < delta)
   {
     LOG (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, buffer > 3\n");
     LOG (GNUNET_ERROR_TYPE_DEBUG,
@@ -526,8 +527,7 @@ connection_send_ack (struct MeshConnection *c, unsigned int buffer, int fwd)
   }
 
   /* Ok, ACK might be necessary, what PID to ACK? */
-  delta = next_fc->queue_max - next_fc->queue_n;
-  ack = prev_fc->last_pid_recv + delta;
+  ack = prev_fc->last_pid_recv + buffer;
   LOG (GNUNET_ERROR_TYPE_DEBUG, " ACK %u\n", ack);
   LOG (GNUNET_ERROR_TYPE_DEBUG,
        " last pid %u, last ack %u, qmax %u, q %u\n",
@@ -929,6 +929,23 @@ unregister_neighbors (struct MeshConnection *c)
 }
 
 
+/**
+ * Bind the connection to the peer and the tunnel to that peer.
+ *
+ * If the peer has no tunnel, create one. Update tunnel and connection
+ * data structres to reflect new status.
+ *
+ * @param c Connection.
+ * @param peer Peer.
+ */
+static void
+add_to_peer (struct MeshConnection *c, struct MeshPeer *peer)
+{
+  GMP_add_tunnel (peer);
+  c->t = GMP_get_tunnel (peer);
+  GMT_add_connection (c->t, c);
+}
+
 /******************************************************************************/
 /********************************    API    ***********************************/
 /******************************************************************************/
@@ -1013,7 +1030,6 @@ GMC_handle_create (void *cls, const struct GNUNET_PeerIdentity *peer,
       /* create path: self not found in path through self */
       GNUNET_break_op (0);
       path_destroy (path);
-      GMC_destroy (c);
       return GNUNET_OK;
     }
     LOG (GNUNET_ERROR_TYPE_DEBUG, "  Own position: %u\n", own_pos);
@@ -1041,8 +1057,7 @@ GMC_handle_create (void *cls, const struct GNUNET_PeerIdentity *peer,
     LOG (GNUNET_ERROR_TYPE_DEBUG, "  It's for us!\n");
     GMP_add_path_to_origin (orig_peer, path, GNUNET_YES);
 
-    GMP_add_tunnel (orig_peer);
-    GMP_add_connection (orig_peer, c);
+    add_to_peer (c, orig_peer);
     if (MESH_TUNNEL3_NEW == GMT_get_state (c->t))
       GMT_change_state (c->t,  MESH_TUNNEL3_WAITING);
 
@@ -1294,23 +1309,23 @@ GMC_handle_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
  *
  * @param peer Peer identity this notification is about.
  * @param message Encrypted message.
- * @param fwd Is this FWD traffic? GNUNET_YES : GNUNET_NO;
  *
  * @return GNUNET_OK to keep the connection open,
  *         GNUNET_SYSERR to close it (signal serious error)
  */
 static int
 handle_mesh_encrypted (const struct GNUNET_PeerIdentity *peer,
-                       const struct GNUNET_MESH_Encrypted *msg,
-                       int fwd)
+                       const struct GNUNET_MESH_Encrypted *msg)
 {
   struct MeshConnection *c;
   struct MeshPeer *neighbor;
   struct MeshFlowControl *fc;
+  GNUNET_PEER_Id peer_id;
   uint32_t pid;
   uint32_t ttl;
   uint16_t type;
   size_t size;
+  int fwd;
 
   /* Check size */
   size = ntohs (msg->header.size);
@@ -1335,15 +1350,27 @@ handle_mesh_encrypted (const struct GNUNET_PeerIdentity *peer,
     return GNUNET_OK;
   }
 
-  fc = fwd ? &c->bck_fc : &c->fwd_fc;
-
   /* Check if origin is as expected */
-  neighbor = get_hop (c, !fwd);
-  if (GNUNET_PEER_search (peer) != GMP_get_short_id (neighbor))
+  neighbor = get_prev_hop (c);
+  peer_id = GNUNET_PEER_search (peer);
+  if (peer_id == GMP_get_short_id (neighbor))
   {
-    GNUNET_break_op (0);
-    return GNUNET_OK;
+    fwd = GNUNET_YES;
+  }
+  else
+  {
+    neighbor = get_next_hop (c);
+    if (peer_id == GMP_get_short_id (neighbor))
+    {
+      fwd = GNUNET_NO;
+    }
+    else
+    {
+      GNUNET_break_op (0);
+      return GNUNET_OK;
+    }
   }
+  fc = fwd ? &c->bck_fc : &c->fwd_fc;
 
   /* Check PID */
   pid = ntohl (msg->pid);
@@ -1406,7 +1433,7 @@ handle_mesh_encrypted (const struct GNUNET_PeerIdentity *peer,
 
 
 /**
- * Core handler for mesh network traffic going orig->dest.
+ * Core handler for encrypted mesh network traffic (channel mgmt, data).
  *
  * @param cls Closure (unused).
  * @param message Message received.
@@ -1416,31 +1443,11 @@ handle_mesh_encrypted (const struct GNUNET_PeerIdentity *peer,
  *         GNUNET_SYSERR to close it (signal serious error)
  */
 int
-GMC_handle_fwd (void *cls, const struct GNUNET_PeerIdentity *peer,
-                const struct GNUNET_MessageHeader *message)
-{
-  return handle_mesh_encrypted (peer,
-                                (struct GNUNET_MESH_Encrypted *)message,
-                                GNUNET_YES);
-}
-
-/**
- * Core handler for mesh network traffic going dest->orig.
- *
- * @param cls Closure (unused).
- * @param message Message received.
- * @param peer Peer who sent the message.
- *
- * @return GNUNET_OK to keep the connection open,
- *         GNUNET_SYSERR to close it (signal serious error)
- */
-int
-GMC_handle_bck (void *cls, const struct GNUNET_PeerIdentity *peer,
-                const struct GNUNET_MessageHeader *message)
+GMC_handle_encrypted (void *cls, const struct GNUNET_PeerIdentity *peer,
+                      const struct GNUNET_MessageHeader *message)
 {
   return handle_mesh_encrypted (peer,
-                                (struct GNUNET_MESH_Encrypted *)message,
-                                GNUNET_NO);
+                                (struct GNUNET_MESH_Encrypted *)message);
 }
 
 
@@ -1695,7 +1702,7 @@ GMC_send_ack (struct MeshConnection *c, struct MeshChannel *ch, int fwd)
   {
     LOG (GNUNET_ERROR_TYPE_DEBUG, "  sending on all connections\n");
     GNUNET_assert (NULL != ch);
-    channel_send_connections_ack (ch, buffer, fwd);
+    GMT_send_acks (GMCH_get_tunnel (ch), buffer, fwd);
   }
   else
   {
@@ -1713,6 +1720,7 @@ GMC_send_ack (struct MeshConnection *c, struct MeshChannel *ch, int fwd)
 void
 GMC_init (const struct GNUNET_CONFIGURATION_Handle *c)
 {
+  LOG (GNUNET_ERROR_TYPE_DEBUG, "init\n");
   if (GNUNET_OK !=
       GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_MSGS_QUEUE",
                                              &max_msgs_queue))
@@ -1751,6 +1759,7 @@ GMC_init (const struct GNUNET_CONFIGURATION_Handle *c)
 void
 GMC_shutdown (void)
 {
+  GNUNET_CONTAINER_multihashmap_destroy (connections);
 }
 
 
@@ -1816,7 +1825,8 @@ GMC_destroy (struct MeshConnection *c)
 
   /* Delete */
   GNUNET_STATISTICS_update (stats, "# connections", -1, GNUNET_NO);
-  GMT_remove_connection (c->t, c);
+  if (NULL != c->t)
+    GMT_remove_connection (c->t, c);
   GNUNET_free (c);
 }
 
@@ -1933,6 +1943,23 @@ GMC_get_qn (struct MeshConnection *c, int fwd)
 }
 
 
+/**
+ * Allow the connection to advertise a buffer of the given size.
+ *
+ * The connection will send an @c fwd ACK message (so: in direction !fwd)
+ * allowing up to last_pid_recv + buffer.
+ *
+ * @param c Connection.
+ * @param buffer How many more messages the connection can accept.
+ * @param fwd Is this about FWD traffic? (The ack will go dest->root).
+ */
+void
+GMC_allow (struct MeshConnection *c, unsigned int buffer, int fwd)
+{
+  connection_send_ack (c, buffer, fwd);
+}
+
+
 /**
  * Send a notification that a connection is broken.
  *
@@ -2084,8 +2111,7 @@ GMC_send_prebuilt_message (const struct GNUNET_MessageHeader *message,
     struct GNUNET_MESH_ConnectionBroken  *bmsg;
     uint32_t ttl;
 
-    case GNUNET_MESSAGE_TYPE_MESH_FWD:
-    case GNUNET_MESSAGE_TYPE_MESH_BCK:
+    case GNUNET_MESSAGE_TYPE_MESH_ENCRYPTED:
       emsg = (struct GNUNET_MESH_Encrypted *) data;
       ttl = ntohl (emsg->ttl);
       if (0 == ttl)
@@ -2170,7 +2196,7 @@ GMC_send_prebuilt_message (const struct GNUNET_MessageHeader *message,
 void
 GMC_send_create (struct MeshConnection *connection)
 {
-enum MeshTunnel3State state;
+  enum MeshTunnel3State state;
   size_t size;
 
   size = sizeof (struct GNUNET_MESH_ConnectionCreate);
@@ -2185,6 +2211,7 @@ enum MeshTunnel3State state;
     GMT_change_state (connection->t, MESH_TUNNEL3_WAITING);
   if (MESH_CONNECTION_NEW == connection->state)
     connection_change_state (connection, MESH_CONNECTION_SENT);
+  connection->fwd_fc.queue_n++;
 }