stun: set delay in retransmission instead of adding it
authorFabrice Bellet <fabrice@bellet.info>
Sun, 5 Apr 2020 19:02:47 +0000 (21:02 +0200)
committerOlivier CrĂȘte <olivier.crete@ocrete.ca>
Thu, 7 May 2020 00:22:48 +0000 (00:22 +0000)
We may have situation when stun_timer_refresh is called with a
significant delay after the current deadline. In the actual situation,
this delay is just included to the computation of the new deadline of the
next stun retransmission. We think this may lead to unfair situations,
where the next deadline may be too short, just to compensate the first
deadline that was too long.

For example, if a stun request is scheduled with a delay of
200ms for the 2nd transmission, and 400ms for the 3rd transmission,
if stun_timer_remainder() is called 300ms after the start of the
timer, the second delay will last only 300ms, instead of 400ms.

stun/usages/timer.c

index 5370cba..3a2f2e6 100644 (file)
@@ -86,8 +86,10 @@ static void stun_gettime (struct timeval *now)
 }
 
 
-static void add_delay (struct timeval *ts, unsigned delay)
+static void set_delay (struct timeval *ts, unsigned delay)
 {
+  stun_gettime (ts);
+
   /* Delay is in ms. */
   ts->tv_sec += delay / 1000;
   ts->tv_usec += (delay % 1000) * 1000;
@@ -103,11 +105,10 @@ static void add_delay (struct timeval *ts, unsigned delay)
 void stun_timer_start (StunTimer *timer, unsigned int initial_timeout,
     unsigned int max_retransmissions)
 {
-  stun_gettime (&timer->deadline);
   timer->retransmissions = 1;
   timer->delay = initial_timeout;
   timer->max_retransmissions = max_retransmissions;
-  add_delay (&timer->deadline, timer->delay);
+  set_delay (&timer->deadline, timer->delay);
 }
 
 
@@ -149,7 +150,7 @@ StunUsageTimerReturn stun_timer_refresh (StunTimer *timer)
       timer->delay = timer->delay / 2;
     else
       timer->delay = timer->delay * 2;
-    add_delay (&timer->deadline, timer->delay);
+    set_delay (&timer->deadline, timer->delay);
     timer->retransmissions++;
     return STUN_USAGE_TIMER_RETURN_RETRANSMIT;
   }