Commit 56b474c3 authored by Youness Alaoui's avatar Youness Alaoui

Use timeval instead of the unix-only timespec

parent bae32229
...@@ -67,29 +67,30 @@ ...@@ -67,29 +67,30 @@
* Clock used throughout the STUN code. * Clock used throughout the STUN code.
* STUN requires a monotonic 1kHz clock to operate properly. * STUN requires a monotonic 1kHz clock to operate properly.
*/ */
static void stun_gettime (struct timespec *restrict now) static void stun_gettime (struct timeval *restrict now)
{ {
#if defined (_POSIX_MONOTONIC_CLOCK) && (_POSIX_MONOTONIC_CLOCK >= 0) #if defined (_POSIX_MONOTONIC_CLOCK) && (_POSIX_MONOTONIC_CLOCK >= 0)
if (clock_gettime (CLOCK_MONOTONIC, now)) struct timespec spec;
if (!clock_gettime (CLOCK_MONOTONIC, &spec)) {
now->tv_sec = spec.tv_sec;
now->tv_usec = spec.tv_nsec / 1000;
} else
#endif #endif
{ // fallback to wall clock { // fallback to wall clock
struct timeval tv; gettimeofday (now, NULL);
gettimeofday (&tv, NULL);
now->tv_sec = tv.tv_sec;
now->tv_nsec = tv.tv_usec * 1000;
} }
} }
static void add_delay (struct timespec *ts, unsigned delay) static void add_delay (struct timeval *ts, unsigned delay)
{ {
div_t d = div (delay, 1000); div_t d = div (delay, 1000);
ts->tv_sec += d.quot; ts->tv_sec += d.quot;
ts->tv_nsec += d.rem * 1000000; ts->tv_usec += d.rem * 1000;
while (ts->tv_nsec > 1000000000) while (ts->tv_usec > 1000000)
{ {
ts->tv_nsec -= 1000000000; ts->tv_usec -= 1000000;
ts->tv_sec++; ts->tv_sec++;
} }
} }
...@@ -113,18 +114,18 @@ void stun_timer_start_reliable (stun_timer_t *timer) ...@@ -113,18 +114,18 @@ void stun_timer_start_reliable (stun_timer_t *timer)
unsigned stun_timer_remainder (const stun_timer_t *timer) unsigned stun_timer_remainder (const stun_timer_t *timer)
{ {
unsigned delay; unsigned delay;
struct timespec now; struct timeval now;
stun_gettime (&now); stun_gettime (&now);
if (now.tv_sec > timer->deadline.tv_sec) if (now.tv_sec > timer->deadline.tv_sec)
return 0; return 0;
delay = timer->deadline.tv_sec - now.tv_sec; delay = timer->deadline.tv_sec - now.tv_sec;
if ((delay == 0) && (now.tv_nsec >= timer->deadline.tv_nsec)) if ((delay == 0) && (now.tv_usec >= timer->deadline.tv_usec))
return 0; return 0;
delay *= 1000; delay *= 1000;
delay += ((signed)(timer->deadline.tv_nsec - now.tv_nsec)) / 1000000; delay += ((signed)(timer->deadline.tv_usec - now.tv_usec)) / 1000;
return delay; return delay;
} }
......
...@@ -41,12 +41,16 @@ ...@@ -41,12 +41,16 @@
* @brief STUN retransmission timer * @brief STUN retransmission timer
*/ */
#ifdef _WIN32
#include <winsock2.h>
#else
# include <sys/types.h> # include <sys/types.h>
# include <time.h> # include <time.h>
#endif
typedef struct stun_timer_s typedef struct stun_timer_s
{ {
struct timespec deadline; struct timeval deadline;
unsigned delay; unsigned delay;
} stun_timer_t; } stun_timer_t;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment