-docu, style fixes
[oweals/gnunet.git] / src / fragmentation / defragmentation.c
index 35d59854f99b3d76a8ec93260f9c22b1e1e6b1ec..e3cf2298bd52fbb99c226edbef9aec5fff2d45c1 100644 (file)
@@ -83,7 +83,7 @@ struct MessageContext
    * Task scheduled for transmitting the next ACK to the
    * other peer.
    */
-  GNUNET_SCHEDULER_TaskIdentifier ack_task;
+  struct GNUNET_SCHEDULER_Task * ack_task;
 
   /**
    * When did we receive which fragment? Used to calculate
@@ -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
@@ -203,15 +203,14 @@ struct GNUNET_DEFRAGMENT_Context
  */
 struct GNUNET_DEFRAGMENT_Context *
 GNUNET_DEFRAGMENT_context_create (struct GNUNET_STATISTICS_Handle *stats,
-                                  uint16_t mtu,
-                                  unsigned int num_msgs,
+                                  uint16_t mtu, unsigned int num_msgs,
                                   void *cls,
                                   GNUNET_FRAGMENT_MessageProcessor proc,
                                   GNUNET_DEFRAGMENT_AckProcessor ackp)
 {
   struct GNUNET_DEFRAGMENT_Context *dc;
 
-  dc = GNUNET_malloc (sizeof (struct GNUNET_DEFRAGMENT_Context));
+  dc = GNUNET_new (struct GNUNET_DEFRAGMENT_Context);
   dc->stats = stats;
   dc->cls = cls;
   dc->proc = proc;
@@ -237,10 +236,10 @@ GNUNET_DEFRAGMENT_context_destroy (struct GNUNET_DEFRAGMENT_Context *dc)
   {
     GNUNET_CONTAINER_DLL_remove (dc->head, dc->tail, mc);
     dc->list_size--;
-    if (GNUNET_SCHEDULER_NO_TASK != mc->ack_task)
+    if (NULL != mc->ack_task)
     {
       GNUNET_SCHEDULER_cancel (mc->ack_task);
-      mc->ack_task = GNUNET_SCHEDULER_NO_TASK;
+      mc->ack_task = NULL;
     }
     GNUNET_free (mc);
   }
@@ -262,14 +261,14 @@ send_ack (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
   struct GNUNET_DEFRAGMENT_Context *dc = mc->dc;
   struct FragmentAcknowledgement fa;
 
-  mc->ack_task = GNUNET_SCHEDULER_NO_TASK;
+  mc->ack_task = NULL;
   fa.header.size = htons (sizeof (struct FragmentAcknowledgement));
   fa.header.type = htons (GNUNET_MESSAGE_TYPE_FRAGMENT_ACK);
   fa.fragment_id = htonl (mc->fragment_id);
   fa.bits = GNUNET_htonll (mc->bits);
   GNUNET_STATISTICS_update (mc->dc->stats,
-                            _("# acknowledgements sent for fragment"),
-                            1, GNUNET_NO);
+                            _("# acknowledgements sent for fragment"), 1,
+                            GNUNET_NO);
   dc->ackp (dc->cls, mc->fragment_id, &fa.header);
 }
 
@@ -279,9 +278,9 @@ send_ack (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
  * (C) 2000 Brian Gough
  */
 static void
-gsl_fit_mul (const double *x, const size_t xstride,
-             const double *y, const size_t ystride,
-             const size_t n, double *c1, double *cov_11, double *sumsq)
+gsl_fit_mul (const double *x, const size_t xstride, const double *y,
+             const size_t ystride, const size_t n, double *c1, double *cov_11,
+             double *sumsq)
 {
   double m_x = 0, m_y = 0, m_dx2 = 0, m_dxdy = 0;
 
@@ -355,15 +354,15 @@ estimate_latency (struct MessageContext *mc)
   for (i = 0; i < total; i++)
   {
     x[i] = (double) i;
-    y[i] = (double) (first[i].time.abs_value - first[0].time.abs_value);
+    y[i] = (double) (first[i].time.abs_value_us - first[0].time.abs_value_us);
   }
   gsl_fit_mul (x, 1, y, 1, total, &c1, &cov11, &sumsq);
   c1 += sqrt (sumsq);           /* add 1 std dev */
-  ret.rel_value = (uint64_t) c1;
-  if (ret.rel_value == 0)
-    ret = GNUNET_TIME_UNIT_MILLISECONDS;        /* always at least 1 */
+  ret.rel_value_us = (uint64_t) c1;
+  if (0 == ret.rel_value_us)
+    ret = GNUNET_TIME_UNIT_MICROSECONDS;        /* always at least 1 */
   return ret;
-};
+}
 
 
 /**
@@ -382,17 +381,17 @@ discard_oldest_mc (struct GNUNET_DEFRAGMENT_Context *dc)
   while (NULL != pos)
   {
     if ((old == NULL) ||
-        (old->last_update.abs_value > pos->last_update.abs_value))
+        (old->last_update.abs_value_us > pos->last_update.abs_value_us))
       old = pos;
     pos = pos->next;
   }
   GNUNET_assert (NULL != old);
   GNUNET_CONTAINER_DLL_remove (dc->head, dc->tail, old);
   dc->list_size--;
-  if (GNUNET_SCHEDULER_NO_TASK != old->ack_task)
+  if (NULL != old->ack_task)
   {
     GNUNET_SCHEDULER_cancel (old->ack_task);
-    old->ack_task = GNUNET_SCHEDULER_NO_TASK;
+    old->ack_task = NULL;
   }
   GNUNET_free (old);
 }
@@ -421,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))
   {
@@ -453,12 +454,18 @@ 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;
   bit = foff / (dc->mtu - sizeof (struct FragmentHeader));
-  if (bit * (dc->mtu - sizeof (struct FragmentHeader)) + ntohs (msg->size)
-      sizeof (struct FragmentHeader) > msize)
+  if (bit * (dc->mtu - sizeof (struct FragmentHeader)) + ntohs (msg->size) -
+      sizeof (struct FragmentHeader) > msize)
   {
     /* payload extends past total message size */
     GNUNET_break_op (0);
@@ -497,8 +504,8 @@ GNUNET_DEFRAGMENT_process_fragment (struct GNUNET_DEFRAGMENT_Context *dc,
   {
     mc->bits -= 1LL << bit;
     mbuf = (char *) &mc[1];
-    memcpy (&mbuf[bit * (dc->mtu - sizeof (struct FragmentHeader))],
-            &fh[1], ntohs (msg->size) - sizeof (struct FragmentHeader));
+    memcpy (&mbuf[bit * (dc->mtu - sizeof (struct FragmentHeader))], &fh[1],
+            ntohs (msg->size) - sizeof (struct FragmentHeader));
     mc->last_update = now;
     if (bit < mc->last_bit)
       mc->frag_times_start_offset = mc->frag_times_write_offset;
@@ -511,9 +518,8 @@ GNUNET_DEFRAGMENT_process_fragment (struct GNUNET_DEFRAGMENT_Context *dc,
   else
   {
     duplicate = GNUNET_YES;
-    GNUNET_STATISTICS_update (dc->stats,
-                              _("# duplicate fragments received"),
-                              1, GNUNET_NO);
+    GNUNET_STATISTICS_update (dc->stats, _("# duplicate fragments received"), 1,
+                              GNUNET_NO);
   }
 
   /* count number of missing fragments */
@@ -521,21 +527,32 @@ GNUNET_DEFRAGMENT_process_fragment (struct GNUNET_DEFRAGMENT_Context *dc,
   for (b = 0; b < 64; b++)
     if (0 != (mc->bits & (1LL << b)))
       bc++;
+
+  /* notify about complete message */
+  if ((duplicate == GNUNET_NO) && (0 == mc->bits))
+  {
+    GNUNET_STATISTICS_update (dc->stats, _("# messages defragmented"), 1,
+                              GNUNET_NO);
+    /* 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 ((0 == mc->bits) || (GNUNET_YES == duplicate))     /* message complete or duplicate, ACK now! */
+  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)
+  }
+  if (NULL != mc->ack_task)
     GNUNET_SCHEDULER_cancel (mc->ack_task);
   mc->ack_task = GNUNET_SCHEDULER_add_delayed (delay, &send_ack, mc);
-  if ((duplicate == GNUNET_NO) && (0 == mc->bits))
-  {
-    GNUNET_STATISTICS_update (dc->stats,
-                              _("# messages defragmented"), 1, GNUNET_NO);
-    /* message complete, notify! */
-    dc->proc (dc->cls, mc->msg);
-  }
   if (duplicate == GNUNET_YES)
     return GNUNET_NO;
   return GNUNET_YES;