- fix handling of duplicate incoming channel create with respect to queued retransmis...
[oweals/gnunet.git] / src / mesh / gnunet-service-mesh_channel.c
index cb5a279733ac12ffed62e4b03927aa89c9ded5fe..0f6592267f08d6f0fc0c6a79ac309f9aa9d45c9e 100644 (file)
@@ -332,9 +332,10 @@ send_create (struct MeshChannel *ch);
  *
  * @param ch The channel to confirm.
  * @param fwd Should we send a FWD ACK? (going dest->root)
+ * @param reaction This ACK is a reaction to a duplicate CREATE, don't save.
  */
 static void
-send_ack (struct MeshChannel *ch, int fwd);
+send_ack (struct MeshChannel *ch, int fwd, int reaction);
 
 
 
@@ -703,7 +704,7 @@ channel_recreate (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
   }
   else if (rel == rel->ch->dest_rel)
   {
-    send_ack (rel->ch, GNUNET_YES);
+    send_ack (rel->ch, GNUNET_YES, GNUNET_NO);
   }
   else
   {
@@ -780,7 +781,8 @@ ch_message_sent (void *cls,
       rel->uniq = NULL;
 
       if (MESH_CHANNEL_READY != rel->ch->state
-          && GNUNET_MESSAGE_TYPE_MESH_DATA_ACK != type)
+          && GNUNET_MESSAGE_TYPE_MESH_DATA_ACK != type
+          && GNUNET_NO == rel->ch->destroy)
       {
         GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == rel->retry_task);
         LOG (GNUNET_ERROR_TYPE_DEBUG, "!!! STD BACKOFF %s\n",
@@ -826,9 +828,10 @@ send_create (struct MeshChannel *ch)
  *
  * @param ch The channel to confirm.
  * @param fwd Should we send a FWD ACK? (going dest->root)
+ * @param reaction This ACK is a reaction to a duplicate CREATE, don't save.
  */
 static void
-send_ack (struct MeshChannel *ch, int fwd)
+send_ack (struct MeshChannel *ch, int fwd, int reaction)
 {
   struct GNUNET_MESH_ChannelManage msg;
 
@@ -839,7 +842,7 @@ send_ack (struct MeshChannel *ch, int fwd)
        GM_f2s (fwd), GMCH_2s (ch));
 
   msg.chid = htonl (ch->gid);
-  GMCH_send_prebuilt_message (&msg.header, ch, !fwd, NULL);
+  GMCH_send_prebuilt_message (&msg.header, ch, !fwd, reaction ? &msg : NULL);
 }
 
 
@@ -856,8 +859,8 @@ fire_and_forget (const struct GNUNET_MessageHeader *msg,
                  struct MeshChannel *ch,
                  int force)
 {
-  GNUNET_break (NULL == GMT_send_prebuilt_message (msg, ch->t, force,
-                                                   NULL, NULL));
+  GNUNET_break (NULL == GMT_send_prebuilt_message (msg, ch->t, NULL,
+                                                   force, NULL, NULL));
 }
 
 
@@ -1076,16 +1079,15 @@ rel_message_free (struct MeshReliableMessage *copy, int update_time)
       rel->expected_delay.rel_value_us += time.rel_value_us;
       rel->expected_delay.rel_value_us /= 8;
     }
-    LOG (GNUNET_ERROR_TYPE_DEBUG, "!!!  took %s\n",
-                GNUNET_STRINGS_relative_time_to_string (time, GNUNET_NO));
-    LOG (GNUNET_ERROR_TYPE_DEBUG, "!!!  new expected delay %s\n",
-                GNUNET_STRINGS_relative_time_to_string (rel->expected_delay,
-                                                        GNUNET_NO));
+    LOG (GNUNET_ERROR_TYPE_INFO, "!!!  took %s, new delay %s\n",
+         GNUNET_STRINGS_relative_time_to_string (time, GNUNET_NO),
+         GNUNET_STRINGS_relative_time_to_string (rel->expected_delay,
+                                                 GNUNET_NO));
     rel->retry_timer = rel->expected_delay;
   }
   else
   {
-    LOG (GNUNET_ERROR_TYPE_DEBUG, "!!! batch free, ignoring timing\n");
+    LOG (GNUNET_ERROR_TYPE_INFO, "!!! batch free, ignoring timing\n");
   }
   rel->ch->pending_messages--;
   if (NULL != copy->chq)
@@ -1164,7 +1166,7 @@ channel_confirm (struct MeshChannel *ch, int fwd)
 
   /* In case of a FWD ACK (SYNACK) send a BCK ACK (ACK). */
   if (GNUNET_YES == fwd)
-    send_ack (ch, GNUNET_NO);
+    send_ack (ch, GNUNET_NO, GNUNET_NO);
 }
 
 
@@ -1490,9 +1492,7 @@ GMCH_send_data_ack (struct MeshChannel *ch, int fwd)
   }
   rel = fwd ? ch->dest_rel : ch->root_rel;
   ack = rel->mid_recv - 1;
-  LOG (GNUNET_ERROR_TYPE_DEBUG,
-              " !! Send DATA_ACK for %u\n",
-              ack);
+  LOG (GNUNET_ERROR_TYPE_INFO, "=> DATA_ACK for %u\n", ack);
 
   msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_DATA_ACK);
   msg.header.size = htons (sizeof (msg));
@@ -1940,9 +1940,9 @@ GMCH_handle_data (struct MeshChannel *ch,
   else
   {
     GNUNET_break_op (GM_is_pid_bigger (rel->mid_recv, mid));
-    LOG (GNUNET_ERROR_TYPE_DEBUG,
-                " !!! MID %u not expected (%u - %u), dropping!\n",
-                mid, rel->mid_recv, rel->mid_recv + 63);
+    LOG (GNUNET_ERROR_TYPE_WARNING,
+         "MID %u not expected (%u - %u), dropping!\n",
+         mid, rel->mid_recv, rel->mid_recv + 63);
   }
 
   GMCH_send_data_ack (ch, fwd);
@@ -1984,8 +1984,7 @@ GMCH_handle_data_ack (struct MeshChannel *ch,
   }
 
   ack = ntohl (msg->mid);
-  LOG (GNUNET_ERROR_TYPE_DEBUG, "!!! %s ACK %u\n",
-       (GNUNET_YES == fwd) ? "FWD" : "BCK", ack);
+  LOG (GNUNET_ERROR_TYPE_INFO, "<= %s ACK %u\n", GM_f2s (fwd), ack);
 
   if (GNUNET_YES == fwd)
   {
@@ -2006,12 +2005,12 @@ GMCH_handle_data_ack (struct MeshChannel *ch,
   {
     if (GM_is_pid_bigger (copy->mid, ack))
     {
-      LOG (GNUNET_ERROR_TYPE_DEBUG, "!!!  head %u, out!\n", copy->mid);
+      LOG (GNUNET_ERROR_TYPE_DEBUG, "  head %u, out!\n", copy->mid);
       channel_rel_free_sent (rel, msg);
       break;
     }
     work = GNUNET_YES;
-    LOG (GNUNET_ERROR_TYPE_DEBUG, " !!  id %u\n", copy->mid);
+    LOG (GNUNET_ERROR_TYPE_DEBUG, "  id %u\n", copy->mid);
     next = copy->next;
     if (GNUNET_YES == rel_message_free (copy, GNUNET_YES))
       return;
@@ -2068,7 +2067,9 @@ GMCH_handle_create (struct MeshTunnel3 *t,
   struct MeshChannel *ch;
   struct MeshClient *c;
   int new_channel;
+  int reaction;
 
+  reaction = GNUNET_NO;
   chid = ntohl (msg->chid);
   ch = GMT_get_channel (t, chid);
   if (NULL == ch)
@@ -2123,8 +2124,16 @@ GMCH_handle_create (struct MeshTunnel3 *t,
   else
   {
     LOG (GNUNET_ERROR_TYPE_DEBUG, "  duplicate create channel\n");
+    reaction = GNUNET_YES;
+    if (GNUNET_SCHEDULER_NO_TASK != ch->dest_rel->retry_task)
+    {
+      LOG (GNUNET_ERROR_TYPE_DEBUG, "  clearing retry task\n");
+      /* we were waiting to re-send our 'SYNACK', wait no more! */
+      GNUNET_SCHEDULER_cancel (ch->dest_rel->retry_task);
+      ch->dest_rel->retry_task = GNUNET_SCHEDULER_NO_TASK;
+    }
   }
-  send_ack (ch, GNUNET_YES);
+  send_ack (ch, GNUNET_YES, reaction);
 
   return ch;
 }
@@ -2244,9 +2253,8 @@ GMCH_send_prebuilt_message (const struct GNUNET_MessageHeader *message,
   uint16_t type;
 
   type = ntohs (message->type);
-  LOG (GNUNET_ERROR_TYPE_DEBUG, "GMCH Send %s %s on channel %s\n",
-       GM_f2s (fwd), GM_m2s (type),
-       GMCH_2s (ch));
+  LOG (GNUNET_ERROR_TYPE_INFO, "=> %s %s on channel %s\n",
+       GM_m2s (type), GM_f2s (fwd), GMCH_2s (ch));
 
   if (GMT_is_loopback (ch->t))
   {
@@ -2256,8 +2264,12 @@ GMCH_send_prebuilt_message (const struct GNUNET_MessageHeader *message,
 
   switch (type)
   {
+    struct GNUNET_MESH_Data *payload;
     case GNUNET_MESSAGE_TYPE_MESH_DATA:
 
+      payload = (struct GNUNET_MESH_Data *) message;
+      LOG (GNUNET_ERROR_TYPE_INFO, "=> %s %u\n",
+           GM_m2s (type), ntohl(payload->mid));
       if (GNUNET_YES == ch->reliable)
       {
         chq = GNUNET_new (struct MeshChannelQueue);
@@ -2290,9 +2302,9 @@ GMCH_send_prebuilt_message (const struct GNUNET_MessageHeader *message,
         }
         LOG (GNUNET_ERROR_TYPE_DEBUG, "  new chq: %p\n", chq);
             chq->copy->chq = chq;
-            chq->tq = GMT_send_prebuilt_message (message, ch->t,
-                                          NULL != existing_copy,
-                                          &ch_message_sent, chq);
+            chq->tq = GMT_send_prebuilt_message (message, ch->t, NULL,
+                                                 NULL != existing_copy,
+                                                 &ch_message_sent, chq);
         /* q itself is stored in copy */
         GNUNET_assert (NULL != chq->tq || GNUNET_NO != ch->destroy);
       }
@@ -2304,7 +2316,7 @@ GMCH_send_prebuilt_message (const struct GNUNET_MessageHeader *message,
 
 
     case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_ACK:
-      if (GNUNET_YES == fwd)
+      if (GNUNET_YES == fwd || NULL != existing_copy)
       {
         /* BCK ACK (going FWD) is just a response for a SYNACK, don't keep*/
         fire_and_forget (message, ch, GNUNET_YES);
@@ -2329,7 +2341,7 @@ GMCH_send_prebuilt_message (const struct GNUNET_MessageHeader *message,
           GNUNET_free (chq->rel->uniq);
         }
       }
-      chq->tq = GMT_send_prebuilt_message (message, ch->t, GNUNET_YES,
+      chq->tq = GMT_send_prebuilt_message (message, ch->t, NULL, GNUNET_YES,
                                            &ch_message_sent, chq);
       if (NULL == chq->tq)
       {