- fixes
[oweals/gnunet.git] / src / mesh / gnunet-service-mesh-new.c
index 63a8f25d083f5addcfe8b32c96c23277c20b1446..1eb76232aa22e70527966d9cd01bd1a82064249b 100644 (file)
@@ -2132,7 +2132,7 @@ peer_cancel_queues (GNUNET_PEER_Id neighbor, struct MeshTunnel *t)
       if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == pq->type ||
           GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == pq->type)
       {
-        // Should have been removed on destroy children
+        /* Should have been removed on destroy children */
         GNUNET_break (0);
       }
       queue_destroy (pq, GNUNET_YES);
@@ -2643,6 +2643,7 @@ queue_send (void *cls, size_t size, void *buf)
   struct GNUNET_PeerIdentity dst_id;
   struct MeshFlowControl *fc;
   size_t data_size;
+  uint16_t type;
 
   peer->core_transmit = NULL;
 
@@ -2684,6 +2685,7 @@ queue_send (void *cls, size_t size, void *buf)
   t = queue->tunnel;
   GNUNET_assert (0 < t->pending_messages);
   t->pending_messages--;
+  type = 0;
 
   /* Fill buf */
   switch (queue->type)
@@ -2703,17 +2705,7 @@ queue_send (void *cls, size_t size, void *buf)
     case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
       data_size = send_core_data_raw (queue->cls, size, buf);
       msg = (struct GNUNET_MessageHeader *) buf;
-      switch (ntohs (msg->type)) // Type of preconstructed message
-      {
-        case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
-          tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_UNICAST);
-          break;
-        case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
-          tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN);
-          break;
-        default:
-            break;
-      }
+      type = ntohs (msg->type);
       break;
     case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "*********   path create\n");
@@ -2730,38 +2722,23 @@ queue_send (void *cls, size_t size, void *buf)
                   queue->type);
       data_size = 0;
   }
-  switch (queue->type)
+
+  /* Free queue, but cls was freed by send_core_* */
+  queue_destroy (queue, GNUNET_NO);
+
+  /* Send ACK if needed, after accounting for sent ID in fc->queue_n */
+  switch (type)
   {
     case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
+      tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_UNICAST);
+      break;
     case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
-//         if (cinfo->send_buffer[cinfo->send_buffer_start] != queue)
-//         { FIXME
-//           GNUNET_break (0);
-//           GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
-//                       "at pos %u (%p) != %p\n",
-//                       cinfo->send_buffer_start,
-//                       cinfo->send_buffer[cinfo->send_buffer_start],
-//                       queue);
-//         }
-//         if (cinfo->send_buffer_n > 0)
-//         {
-//           cinfo->send_buffer[cinfo->send_buffer_start] = NULL;
-//           cinfo->send_buffer_n--;
-//           cinfo->send_buffer_start++;
-//           cinfo->send_buffer_start %= t->fwd_queue_max;
-//         }
-//         else
-//         {
-//           GNUNET_break (0);
-//         }
+      tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN);
       break;
     default:
       break;
   }
 
-  /* Free queue, but cls was freed by send_core_* */
-  queue_destroy (queue, GNUNET_NO);
-
   if (GNUNET_YES == t->destroy && 0 == t->pending_messages)
   {
     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "*********  destroying tunnel!\n");
@@ -2786,26 +2763,28 @@ queue_send (void *cls, size_t size, void *buf)
                                             &queue_send,
                                             peer);
   }
-  else
+  else if (NULL != peer->queue_head)
   {
-    if (NULL != peer->queue_head)
+    GNUNET_log (GNUNET_ERROR_TYPE_INFO,
+                "*********   %s stalled\n",
+                GNUNET_i2s (&my_full_id));
+    if (peer->id == t->next_hop)
+      fc = &t->next_fc;
+    else if (peer->id == t->prev_hop)
+      fc = &t->prev_fc;
+    else
     {
-      GNUNET_log (GNUNET_ERROR_TYPE_INFO,
-                  "*********   %s stalled\n",
-                  GNUNET_i2s(&my_full_id));
-      if (peer->id == t->next_hop)
-        fc = &t->next_fc;
-      else
-        fc = &t->prev_fc;
-      if (GNUNET_SCHEDULER_NO_TASK == fc->poll_task)
-      {
-        fc->t = t;
-        fc->poll_task = GNUNET_SCHEDULER_add_delayed (fc->poll_time,
-                                                      &tunnel_poll, fc);
-      }
+      GNUNET_break (0);
+      fc = NULL;
+    }
+    if (NULL != fc && GNUNET_SCHEDULER_NO_TASK == fc->poll_task)
+    {
+      fc->t = t;
+      fc->poll_task = GNUNET_SCHEDULER_add_delayed (fc->poll_time,
+                                                    &tunnel_poll, fc);
     }
   }
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "*********   return %d\n", data_size);
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "*********  Return %d\n", data_size);
   return data_size;
 }
 
@@ -2852,7 +2831,7 @@ queue_add (void *cls, uint16_t type, size_t size,
       GNUNET_STATISTICS_update(stats,
                                "# messages dropped (buffer full)",
                                1, GNUNET_NO);
-      return; // Drop message
+      return; /* Drop message */
     }
     (*n)++;
   }
@@ -2941,7 +2920,7 @@ handle_mesh_path_create (void *cls, const struct GNUNET_PeerIdentity *peer,
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
               "    path is for tunnel %s [%X].\n", GNUNET_i2s (pi), tid);
   t = tunnel_get (pi, tid);
-  if (NULL == t) // FIXME only for INCOMING tunnels?
+  if (NULL == t)
   {
     uint32_t opt;
 
@@ -2949,7 +2928,7 @@ handle_mesh_path_create (void *cls, const struct GNUNET_PeerIdentity *peer,
     t = tunnel_new (GNUNET_PEER_intern (pi), tid, NULL, 0);
     if (NULL == t)
     {
-      // FIXME notify failure
+      GNUNET_break (0);
       return GNUNET_OK;
     }
     opt = ntohl (msg->opt);
@@ -2965,13 +2944,6 @@ handle_mesh_path_create (void *cls, const struct GNUNET_PeerIdentity *peer,
       t->queue_max = 1;
     }
 
-    // FIXME only assign a local tid if a local client is interested (on demand)
-    while (NULL != tunnel_get_incoming (next_local_tid))
-      next_local_tid = (next_local_tid + 1) | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
-    t->local_tid_dest = next_local_tid++;
-    next_local_tid = next_local_tid | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
-    // FIXME end
-
     tunnel_reset_timeout (t);
     GMC_hash32 (t->local_tid_dest, &hash);
     if (GNUNET_OK !=
@@ -3032,9 +3004,16 @@ handle_mesh_path_create (void *cls, const struct GNUNET_PeerIdentity *peer,
   if (own_pos == size - 1)
   {
     /* It is for us! Send ack. */
+    // TODO: check port number / client registration
     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "  It's for us!\n");
     peer_info_add_path_to_origin (orig_peer_info, path, GNUNET_NO);
     t->dest = myid;
+
+    while (NULL != tunnel_get_incoming (next_local_tid))
+      next_local_tid = (next_local_tid + 1) | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
+    t->local_tid_dest = next_local_tid++;
+    next_local_tid = next_local_tid | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
+
     send_path_ack (t);
   }
   else
@@ -3072,7 +3051,6 @@ handle_mesh_path_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
 {
   struct GNUNET_MESH_ManipulatePath *msg;
   struct GNUNET_PeerIdentity *pi;
-  struct MeshPeerPath *path;
   struct MeshTunnel *t;
   unsigned int own_pos;
   unsigned int i;
@@ -3113,25 +3091,19 @@ handle_mesh_path_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
     GNUNET_break_op (0);
     return GNUNET_OK;
   }
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "  Creating path...\n");
-  path = path_new (size);
   own_pos = 0;
   for (i = 0; i < size; i++)
   {
-    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "  ... adding %s\n",
-                GNUNET_i2s (&pi[i]));
-    path->peers[i] = GNUNET_PEER_intern (&pi[i]);
-    if (path->peers[i] == myid)
+    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "  %u: %s\n", i, GNUNET_i2s (&pi[i]));
+    if (GNUNET_PEER_search (&pi[i]) == myid)
       own_pos = i;
   }
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "  Own position: %u\n", own_pos);
-  if (own_pos < path->length - 1)
-    send_prebuilt_message (message, path->peers[own_pos + 1], t);
+  if (own_pos < size - 1)
+    send_prebuilt_message (message, t->next_hop, t);
   else
     send_client_tunnel_destroy (t);
 
-//   tunnel_delete_peer (t, path->peers[path->length - 1]); FIXME
-  path_destroy (path);
   return GNUNET_OK;
 }
 
@@ -3536,6 +3508,7 @@ handle_mesh_poll (void *cls, const struct GNUNET_PeerIdentity *peer,
 {
   struct GNUNET_MESH_Poll *msg;
   struct MeshTunnel *t;
+  GNUNET_PEER_Id id;
 
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got an POLL packet from %s!\n",
               GNUNET_i2s (peer));
@@ -3553,17 +3526,19 @@ handle_mesh_poll (void *cls, const struct GNUNET_PeerIdentity *peer,
   }
 
   /* Is this a forward or backward ACK? */
-  if (t->prev_hop != GNUNET_PEER_search(peer))
+  id = GNUNET_PEER_search(peer);
+  if (t->next_hop == id)
   {
     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "  from FWD\n");
-    /* FIXME cinfo->bck_ack = cinfo->fwd_pid; // mark as ready to send */
     tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_POLL);
   }
-  else
+  else if (t->prev_hop == id)
   {
     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "  from BCK\n");
     tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_POLL);
   }
+  else
+    GNUNET_break (0);
 
   return GNUNET_OK;
 }