- add underlay api implementation
[oweals/gnunet.git] / src / mesh / gnunet-service-mesh_channel.c
index b6305aabefbc97444b2258189ee42c70e2bea1ab..2e52a2ea5da6898985240fe92b830533c0e543ef 100644 (file)
@@ -316,7 +316,7 @@ extern GNUNET_PEER_Id myid;
  *                    is skewed by the retransmission, count only for the
  *                    retransmitted message.
  */
-static void
+static int
 rel_message_free (struct MeshReliableMessage *copy, int update_time);
 
 /**
@@ -338,6 +338,23 @@ send_ack (struct MeshChannel *ch, int fwd);
 
 
 
+/**
+ * Test if the channel is loopback: both root and dest are on the local peer.
+ *
+ * @param ch Channel to test.
+ *
+ * @return #GNUNET_YES if channel is loopback, #GNUNET_NO otherwise.
+ */
+static int
+is_loopback (const struct MeshChannel *ch)
+{
+  if (NULL != ch->t)
+    return GMT_is_loopback (ch->t);
+
+  return (NULL != ch->root && NULL != ch->dest);
+}
+
+
 /**
  * We have received a message out of order, or the client is not ready.
  * Buffer it until we receive an ACK from the client or the missing
@@ -386,6 +403,7 @@ add_buffered_data (const struct GNUNET_MESH_Data *msg,
     LOG (GNUNET_ERROR_TYPE_DEBUG, "add_buffered_data END\n");
 }
 
+
 /**
  * Add a destination client to a channel, initializing all data structures
  * in the channel and the client.
@@ -717,7 +735,9 @@ ch_message_sent (void *cls,
       rel = copy->rel;
       if (GNUNET_SCHEDULER_NO_TASK == rel->retry_task)
       {
-        LOG (GNUNET_ERROR_TYPE_DEBUG, "!! scheduling retry %u\n");
+        LOG (GNUNET_ERROR_TYPE_DEBUG, "!! scheduling retry in %s\n",
+             GNUNET_STRINGS_relative_time_to_string (rel->expected_delay,
+                                                     GNUNET_YES));
         if (0 != rel->expected_delay.rel_value_us)
         {
           LOG (GNUNET_ERROR_TYPE_DEBUG, "!! delay != 0\n");
@@ -727,7 +747,7 @@ ch_message_sent (void *cls,
         }
         else
         {
-          LOG (GNUNET_ERROR_TYPE_DEBUG, "!! delay 0\n");
+          LOG (GNUNET_ERROR_TYPE_DEBUG, "!! delay reset\n");
           rel->retry_timer = MESH_RETRANSMIT_TIME;
         }
         LOG (GNUNET_ERROR_TYPE_DEBUG, "!! using delay %s\n",
@@ -830,7 +850,7 @@ fire_and_forget (const struct GNUNET_MessageHeader *msg,
                  struct MeshChannel *ch,
                  int force)
 {
-  GNUNET_break (NULL == GMT_send_prebuilt_message (msg, ch->t, ch, force,
+  GNUNET_break (NULL == GMT_send_prebuilt_message (msg, ch->t, force,
                                                    NULL, NULL));
 }
 
@@ -909,6 +929,7 @@ channel_rel_free_all (struct MeshChannelReliability *rel)
     next = copy->next;
     GNUNET_CONTAINER_DLL_remove (rel->head_recv, rel->tail_recv, copy);
     LOG (GNUNET_ERROR_TYPE_DEBUG, " COPYFREE BATCH RECV %p\n", copy);
+    GNUNET_break (NULL == copy->q);
     GNUNET_free (copy);
   }
   for (copy = rel->head_sent; NULL != copy; copy = next)
@@ -916,14 +937,31 @@ channel_rel_free_all (struct MeshChannelReliability *rel)
     next = copy->next;
     GNUNET_CONTAINER_DLL_remove (rel->head_sent, rel->tail_sent, copy);
     LOG (GNUNET_ERROR_TYPE_DEBUG, " COPYFREE BATCH %p\n", copy);
+    if (NULL != copy->q)
+    {
+      if (NULL != copy->q->q)
+      {
+        GMT_cancel (copy->q->q);
+        /* ch_message_sent will free copy->q */
+      }
+      else
+      {
+        GNUNET_free (copy->q);
+        GNUNET_break (0);
+      }
+    }
     GNUNET_free (copy);
   }
+  if (NULL != rel->uniq && NULL != rel->uniq->q)
+  {
+    GMT_cancel (rel->uniq->q);
+    /* ch_message_sent is called freeing uniq */
+  }
   if (GNUNET_SCHEDULER_NO_TASK != rel->retry_task)
   {
     GNUNET_SCHEDULER_cancel (rel->retry_task);
+    rel->retry_task = GNUNET_SCHEDULER_NO_TASK;
   }
-  if (NULL != rel->uniq && NULL != rel->uniq->q)
-    GMT_cancel (rel->uniq->q);
   GNUNET_free (rel);
 }
 
@@ -992,7 +1030,7 @@ channel_rel_free_sent (struct MeshChannelReliability *rel,
 
     /* Now copy->mid == target, free it */
     next = copy->next;
-    rel_message_free (copy, GNUNET_YES);
+    GNUNET_break (GNUNET_YES != rel_message_free (copy, GNUNET_YES));
     copy = next;
   }
   LOG (GNUNET_ERROR_TYPE_DEBUG, "free_sent_reliable END\n");
@@ -1009,8 +1047,11 @@ channel_rel_free_sent (struct MeshChannelReliability *rel,
  *                    If this message is ACK in a batch the timing information
  *                    is skewed by the retransmission, count only for the
  *                    retransmitted message.
+ *
+ * @return #GNUNET_YES if channel was destroyed as a result of the call,
+ *         #GNUNET_NO otherwise.
  */
-static void
+static int
 rel_message_free (struct MeshReliableMessage *copy, int update_time)
 {
   struct MeshChannelReliability *rel;
@@ -1055,7 +1096,9 @@ rel_message_free (struct MeshReliableMessage *copy, int update_time)
     struct MeshTunnel3 *t = rel->ch->t;
     GMCH_destroy (rel->ch);
     GMT_destroy_if_empty (t);
+    return GNUNET_YES;
   }
+  return GNUNET_NO;
 }
 
 
@@ -1072,8 +1115,8 @@ channel_confirm (struct MeshChannel *ch, int fwd)
   enum MeshChannelState oldstate;
 
   LOG (GNUNET_ERROR_TYPE_DEBUG,
-              "  channel confirm %s %s:%X\n",
-              GM_f2s (fwd), GMT_2s (ch->t), ch->gid);
+              "  channel confirm %s %s\n",
+              GM_f2s (fwd), GMCH_2s (ch));
   oldstate = ch->state;
   ch->state = MESH_CHANNEL_READY;
 
@@ -1094,12 +1137,15 @@ channel_confirm (struct MeshChannel *ch, int fwd)
   else if (NULL != rel->uniq)
   {
     GMT_cancel (rel->uniq->q);
-    /* ch_sent_message will free and NULL uniq */
+    /* ch_message_sent will free and NULL uniq */
   }
   else
   {
-    /* We SHOULD have been trying to retransmit this! */
-    GNUNET_break (oldstate == MESH_CHANNEL_READY);
+    if (GNUNET_NO == is_loopback (ch))
+    {
+      /* We SHOULD have been trying to retransmit this! */
+      GNUNET_break (oldstate == MESH_CHANNEL_READY);
+    }
   }
 
   /* In case of a FWD ACK (SYNACK) send a BCK ACK (ACK). */
@@ -1181,23 +1227,6 @@ channel_new (struct MeshTunnel3 *t,
 }
 
 
-/**
- * Test if the channel is loopback: both root and dest are on the local peer.
- *
- * @param ch Channel to test.
- *
- * @return #GNUNET_YES if channel is loopback, #NGUNET_NO otherwise.
- */
-static int
-is_loopback (const struct MeshChannel *ch)
-{
-  if (NULL != ch->t)
-    return GMT_is_loopback (ch->t);
-
-  return (NULL != ch->root && NULL != ch->dest);
-}
-
-
 /**
  * Handle a loopback message: call the appropriate handler for the message type.
  *
@@ -1275,6 +1304,9 @@ GMCH_destroy (struct MeshChannel *ch)
 
   if (NULL == ch)
     return;
+  if (2 == ch->destroy)
+    return; /* recursive call */
+  ch->destroy = 2;
 
   LOG (GNUNET_ERROR_TYPE_DEBUG, "destroying channel %s:%u\n",
               GMT_2s (ch->t), ch->gid);
@@ -1511,10 +1543,20 @@ GMCH_allow_client (struct MeshChannel *ch, int fwd)
       GNUNET_break (GNUNET_NO != ch->destroy);
       return;
     }
-    if (NULL != rel->head_sent && 64 <= rel->mid_send - rel->head_sent->mid)
+    if (NULL != rel->head_sent)
     {
-      LOG (GNUNET_ERROR_TYPE_DEBUG, " too big MID gap! Wait for ACK.\n");
-      return;
+      if (64 <= rel->mid_send - rel->head_sent->mid)
+      {
+        LOG (GNUNET_ERROR_TYPE_DEBUG, " too big MID gap! Wait for ACK.\n");
+        return;
+      }
+      else
+        LOG (GNUNET_ERROR_TYPE_DEBUG, " gap ok: %u - %u\n",
+             rel->head_sent->mid, rel->mid_send);
+    }
+    else
+    {
+      LOG (GNUNET_ERROR_TYPE_DEBUG, " head sent is NULL\n");
     }
   }
 
@@ -1664,14 +1706,14 @@ GMCH_handle_local_data (struct MeshChannel *ch,
   if (is_loopback (ch))
   {
     if (GMCH_get_buffer (ch, fwd) > 0)
-      send_client_ack (ch, fwd);
+      GMCH_allow_client (ch, fwd);
 
     return GNUNET_OK;
   }
 
   if (GMT_get_connections_buffer (ch->t) > 0)
   {
-    send_client_ack (ch, fwd);
+    GMCH_allow_client (ch, fwd);
   }
 
   return GNUNET_OK;
@@ -1953,7 +1995,8 @@ GMCH_handle_data_ack (struct MeshChannel *ch,
     work = GNUNET_YES;
     LOG (GNUNET_ERROR_TYPE_DEBUG, " !!  id %u\n", copy->mid);
     next = copy->next;
-    rel_message_free (copy, GNUNET_YES);
+    if (GNUNET_YES == rel_message_free (copy, GNUNET_YES))
+      return;
   }
 
   /* ACK client if needed */
@@ -1965,6 +2008,7 @@ GMCH_handle_data_ack (struct MeshChannel *ch,
     if (GNUNET_SCHEDULER_NO_TASK != rel->retry_task)
     {
       GNUNET_SCHEDULER_cancel (rel->retry_task);
+      rel->retry_task = GNUNET_SCHEDULER_NO_TASK;
       if (NULL != rel->head_sent && NULL == rel->head_sent->q)
       {
         struct GNUNET_TIME_Absolute new_target;
@@ -1980,10 +2024,6 @@ GMCH_handle_data_ack (struct MeshChannel *ch,
                                           &channel_retransmit_message,
                                           rel);
       }
-      else /* either no more traffic to ack or traffic has just been queued */
-      {
-        rel->retry_task = GNUNET_SCHEDULER_NO_TASK;
-      }
     }
     else /* work was done but no task was pending? shouldn't happen! */
     {
@@ -2231,10 +2271,11 @@ GMCH_send_prebuilt_message (const struct GNUNET_MessageHeader *message,
         }
         LOG (GNUNET_ERROR_TYPE_DEBUG, "  new q: %p\n", q);
         q->copy->q = q;
-        q->q = GMT_send_prebuilt_message (message, ch->t, ch,
+        q->q = GMT_send_prebuilt_message (message, ch->t,
                                           NULL != existing_copy,
                                           &ch_message_sent, q);
         /* q itself is stored in copy */
+        GNUNET_assert (NULL != q->q || GNUNET_NO != ch->destroy);
       }
       else
       {
@@ -2268,7 +2309,7 @@ GMCH_send_prebuilt_message (const struct GNUNET_MessageHeader *message,
           GNUNET_free (q->rel->uniq);
         }
       }
-      q->q = GMT_send_prebuilt_message (message, ch->t, ch, GNUNET_YES,
+      q->q = GMT_send_prebuilt_message (message, ch->t, GNUNET_YES,
                                         &ch_message_sent, q);
       q->rel->uniq = q;
       break;