REST/NAMESTORE: rework API
[oweals/gnunet.git] / src / fragmentation / fragmentation.c
index a702fd35d31066dbaf6990b2400b6e88b8dd4133..9fca6eef024a693bcb425504f71e85bd3f8a8c6c 100644 (file)
@@ -2,20 +2,20 @@
      This file is part of GNUnet
      Copyright (C) 2009-2013 GNUnet e.V.
 
-     GNUnet is free software; you can redistribute it and/or modify
-     it under the terms of the GNU General Public License as published
-     by the Free Software Foundation; either version 3, or (at your
-     option) any later version.
+     GNUnet is free software: you can redistribute it and/or modify it
+     under the terms of the GNU Affero General Public License as published
+     by the Free Software Foundation, either version 3 of the License,
+     or (at your option) any later version.
 
      GNUnet is distributed in the hope that it will be useful, but
      WITHOUT ANY WARRANTY; without even the implied warranty of
      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
-     General Public License for more details.
+     Affero General Public License for more details.
+    
+     You should have received a copy of the GNU Affero General Public License
+     along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
-     You should have received a copy of the GNU General Public License
-     along with GNUnet; see the file COPYING.  If not, write to the
-     Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
-     Boston, MA 02110-1301, USA.
+     SPDX-License-Identifier: AGPL3.0-or-later
 */
 /**
  * @file src/fragmentation/fragmentation.c
@@ -167,11 +167,9 @@ GNUNET_FRAGMENT_print_ack (const struct GNUNET_MessageHeader *ack)
  * Transmit the next fragment to the other peer.
  *
  * @param cls the `struct GNUNET_FRAGMENT_Context`
- * @param tc scheduler context
  */
 static void
-transmit_next (void *cls,
-               const struct GNUNET_SCHEDULER_TaskContext *tc)
+transmit_next (void *cls)
 {
   struct GNUNET_FRAGMENT_Context *fc = cls;
   char msg[fc->mtu];
@@ -189,7 +187,7 @@ transmit_next (void *cls,
     return;                     /* all done */
   /* calculate delay */
   wrap = 0;
-  while (0 == (fc->acks & (1LL << fc->next_transmission)))
+  while (0 == (fc->acks & (1LLU << fc->next_transmission)))
   {
     fc->next_transmission = (fc->next_transmission + 1) % 64;
     wrap |= (0 == fc->next_transmission);
@@ -220,7 +218,7 @@ transmit_next (void *cls,
   }
   fc->next_transmission = (fc->next_transmission + 1) % 64;
   wrap |= (0 == fc->next_transmission);
-  while (0 == (fc->acks & (1LL << fc->next_transmission)))
+  while (0 == (fc->acks & (1LLU << fc->next_transmission)))
   {
     fc->next_transmission = (fc->next_transmission + 1) % 64;
     wrap |= (0 == fc->next_transmission);
@@ -234,7 +232,7 @@ transmit_next (void *cls,
   fh->fragment_id = htonl (fc->fragment_id);
   fh->total_size = fc->msg->size;       /* already in big-endian */
   fh->offset = htons ((fc->mtu - sizeof (struct FragmentHeader)) * bit);
-  memcpy (&fh[1], &mbuf[bit * (fc->mtu - sizeof (struct FragmentHeader))],
+  GNUNET_memcpy (&fh[1], &mbuf[bit * (fc->mtu - sizeof (struct FragmentHeader))],
           fsize - sizeof (struct FragmentHeader));
   if (NULL != fc->tracker)
     GNUNET_BANDWIDTH_tracker_consume (fc->tracker, fsize);
@@ -260,14 +258,18 @@ transmit_next (void *cls,
                                                 fsize);
   else
     delay = GNUNET_TIME_UNIT_ZERO;
-  delay = GNUNET_TIME_relative_max (delay,
-                                   GNUNET_TIME_relative_multiply (fc->msg_delay,
-                                                                  (1ULL << fc->num_rounds)));
+  if (fc->num_rounds < 64)
+    delay = GNUNET_TIME_relative_max (delay,
+                                      GNUNET_TIME_relative_saturating_multiply
+                                      (fc->msg_delay,
+                                       (1ULL << fc->num_rounds)));
+  else
+    delay = GNUNET_TIME_UNIT_FOREVER_REL;
   if (wrap)
   {
     /* full round transmitted wait 2x delay for ACK before going again */
     fc->num_rounds++;
-    delay = GNUNET_TIME_relative_multiply (fc->ack_delay, 2);
+    delay = GNUNET_TIME_relative_saturating_multiply (fc->ack_delay, 2);
     /* never use zero, need some time for ACK always */
     delay = GNUNET_TIME_relative_max (MIN_ACK_DELAY, delay);
     fc->wack = GNUNET_YES;
@@ -341,7 +343,7 @@ GNUNET_FRAGMENT_context_create (struct GNUNET_STATISTICS_Handle *stats,
   fc->fragment_id =
       GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
                                 UINT32_MAX);
-  memcpy (&fc[1], msg, size);
+  GNUNET_memcpy (&fc[1], msg, size);
   bits =
       (size + mtu - sizeof (struct FragmentHeader) - 1) / (mtu -
                                                            sizeof (struct
@@ -350,7 +352,7 @@ GNUNET_FRAGMENT_context_create (struct GNUNET_STATISTICS_Handle *stats,
   if (bits == 64)
     fc->acks_mask = UINT64_MAX; /* set all 64 bit */
   else
-    fc->acks_mask = (1LL << bits) - 1;  /* set lowest 'bits' bit */
+    fc->acks_mask = (1LLU << bits) - 1;  /* set lowest 'bits' bit */
   fc->acks = fc->acks_mask;
   fc->task = GNUNET_SCHEDULER_add_now (&transmit_next, fc);
   return fc;
@@ -371,8 +373,9 @@ GNUNET_FRAGMENT_context_transmission_done (struct GNUNET_FRAGMENT_Context *fc)
   fc->proc_busy = GNUNET_NO;
   GNUNET_assert (fc->task == NULL);
   fc->task =
-      GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_absolute_get_remaining
-                                    (fc->delay_until), &transmit_next, fc);
+    GNUNET_SCHEDULER_add_at (fc->delay_until,
+                             &transmit_next,
+                             fc);
 }
 
 
@@ -430,8 +433,8 @@ GNUNET_FRAGMENT_process_ack (struct GNUNET_FRAGMENT_Context *fc,
     if (0 == ack_cnt)
     {
       /* complete loss */
-      fc->msg_delay = GNUNET_TIME_relative_multiply (fc->msg_delay,
-                                                    snd_cnt);
+      fc->msg_delay = GNUNET_TIME_relative_saturating_multiply (fc->msg_delay,
+                                                                snd_cnt);
     }
     else if (snd_cnt > ack_cnt)
     {
@@ -513,8 +516,8 @@ GNUNET_FRAGMENT_context_destroy (struct GNUNET_FRAGMENT_Context *fc,
   if (NULL != ack_delay)
     *ack_delay = fc->ack_delay;
   if (NULL != msg_delay)
-    *msg_delay = GNUNET_TIME_relative_multiply (fc->msg_delay,
-                                               fc->num_rounds);
+    *msg_delay = GNUNET_TIME_relative_saturating_multiply (fc->msg_delay,
+                                                           fc->num_rounds);
   GNUNET_free (fc);
 }