- add underlay api implementation
[oweals/gnunet.git] / src / mesh / gnunet-service-mesh_channel.c
index 1640dd7c6caca68effbb941d40f0e6349c88178a..2e52a2ea5da6898985240fe92b830533c0e543ef 100644 (file)
@@ -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,10 +937,26 @@ 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);
@@ -1078,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;
 
@@ -1100,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). */
@@ -1187,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, #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);
-}
-
-
 /**
  * Handle a loopback message: call the appropriate handler for the message type.
  *
@@ -1281,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);
@@ -1517,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");
     }
   }
 
@@ -1670,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;
@@ -2235,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
       {
@@ -2272,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;