-fixing #2546
[oweals/gnunet.git] / src / fragmentation / defragmentation.c
index bd4dfba5a5a13d6ea64ea9b1e56f680179c9ae88..2d3f4a57b6a55122d7ecb82b98cfeed83b3853e6 100644 (file)
@@ -192,7 +192,7 @@ struct GNUNET_DEFRAGMENT_Context
  * Create a defragmentation context.
  *
  * @param stats statistics context
- * @param mtu the maximum message size for each fragment 
+ * @param mtu the maximum message size for each fragment
  * @param num_msgs how many fragmented messages
  *                 to we defragment at most at the same time?
  * @param cls closure for proc and ackp
@@ -362,7 +362,7 @@ estimate_latency (struct MessageContext *mc)
   if (ret.rel_value == 0)
     ret = GNUNET_TIME_UNIT_MILLISECONDS;        /* always at least 1 */
   return ret;
-};
+}
 
 
 /**
@@ -420,7 +420,9 @@ GNUNET_DEFRAGMENT_process_fragment (struct GNUNET_DEFRAGMENT_Context *dc,
   unsigned int bc;
   unsigned int b;
   unsigned int n;
+  unsigned int num_fragments;
   int duplicate;
+  int last;
 
   if (ntohs (msg->size) < sizeof (struct FragmentHeader))
   {
@@ -452,6 +454,12 @@ GNUNET_DEFRAGMENT_process_fragment (struct GNUNET_DEFRAGMENT_Context *dc,
     return GNUNET_SYSERR;
   }
   GNUNET_STATISTICS_update (dc->stats, _("# fragments received"), 1, GNUNET_NO);
+  num_fragments = (ntohs (msg->size) + dc->mtu - sizeof (struct FragmentHeader)-1) / (dc->mtu - sizeof (struct FragmentHeader));
+  last = 0;
+  for (mc = dc->head; NULL != mc; mc = mc->next)
+    if (mc->fragment_id > fid)
+      last++;
+  
   mc = dc->head;
   while ((NULL != mc) && (fid != mc->fragment_id))
     mc = mc->next;
@@ -519,14 +527,8 @@ GNUNET_DEFRAGMENT_process_fragment (struct GNUNET_DEFRAGMENT_Context *dc,
   for (b = 0; b < 64; b++)
     if (0 != (mc->bits & (1LL << b)))
       bc++;
-  if (mc->frag_times_write_offset - mc->frag_times_start_offset > 1)
-    dc->latency = estimate_latency (mc);
-  delay = GNUNET_TIME_relative_multiply (dc->latency, bc + 1);
-  if ((0 == mc->bits) || (GNUNET_YES == duplicate))     /* message complete or duplicate, ACK now! */
-    delay = GNUNET_TIME_UNIT_ZERO;
-  if (GNUNET_SCHEDULER_NO_TASK != mc->ack_task)
-    GNUNET_SCHEDULER_cancel (mc->ack_task);
-  mc->ack_task = GNUNET_SCHEDULER_add_delayed (delay, &send_ack, mc);
+
+  /* notify about complete message */
   if ((duplicate == GNUNET_NO) && (0 == mc->bits))
   {
     GNUNET_STATISTICS_update (dc->stats, _("# messages defragmented"), 1,
@@ -534,6 +536,23 @@ GNUNET_DEFRAGMENT_process_fragment (struct GNUNET_DEFRAGMENT_Context *dc,
     /* message complete, notify! */
     dc->proc (dc->cls, mc->msg);
   }
+  /* send ACK */
+  if (mc->frag_times_write_offset - mc->frag_times_start_offset > 1)
+  { 
+    dc->latency = estimate_latency (mc);
+  }
+  delay = GNUNET_TIME_relative_multiply (dc->latency, bc + 1);
+  if ( (last + fid == num_fragments) ||
+       (0 == mc->bits) || 
+       (GNUNET_YES == duplicate))     
+  {
+    /* message complete or duplicate or last missing fragment in
+       linear sequence; ACK now! */
+    delay = GNUNET_TIME_UNIT_ZERO;
+  }
+  if (GNUNET_SCHEDULER_NO_TASK != mc->ack_task)
+    GNUNET_SCHEDULER_cancel (mc->ack_task);
+  mc->ack_task = GNUNET_SCHEDULER_add_delayed (delay, &send_ack, mc);
   if (duplicate == GNUNET_YES)
     return GNUNET_NO;
   return GNUNET_YES;