From af1ea9f60bac6595db7337b888fe27e94bf880b2 Mon Sep 17 00:00:00 2001 From: Thomas Bernard Date: Wed, 23 Sep 2020 00:48:23 +0200 Subject: [PATCH] miniupnpc: use clock_gettime() instead of gettimeofday() if possible --- miniupnpc/minissdpc.c | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/miniupnpc/minissdpc.c b/miniupnpc/minissdpc.c index 8d0e4a5..881aa70 100644 --- a/miniupnpc/minissdpc.c +++ b/miniupnpc/minissdpc.c @@ -9,8 +9,8 @@ #include #include #include +#include #include -#include #if defined (__NetBSD__) #include #endif @@ -445,6 +445,27 @@ parseMSEARCHReply(const char * reply, int size, } } +#if defined(CLOCK_MONOTONIC_FAST) +#define UPNP_CLOCKID CLOCK_MONOTONIC_FAST +#elif defined(CLOCK_MONOTONIC) +#define UPNP_CLOCKID CLOCK_MONOTONIC +#endif + +static int upnp_gettimeofday(struct timeval * tv) +{ +#if defined(CLOCK_MONOTONIC_FAST) || defined(CLOCK_MONOTONIC) + struct timespec ts; + int ret_code = clock_gettime(UPNP_CLOCKID, &ts); + if (ret_code == 0) + { + tv->tv_sec = ts.tv_sec; + tv->tv_usec = ts.tv_nsec / 1000; + } + return ret_code; +#else + return gettimeofday(tv, NULL); +#endif +} /* port upnp discover : SSDP protocol */ #define SSDP_PORT 1900 #define XSTR(s) STR(s) @@ -844,7 +865,7 @@ ssdpDiscoverDevices(const char * const deviceTypes[], * when the last deviceType is reached */ if((sentok && !searchalltypes) || !deviceTypes[deviceIndex + 1]) { struct timeval start, current; - if (gettimeofday(&start, NULL) < 0) { + if (upnp_gettimeofday(&start) < 0) { start.tv_sec = 0; start.tv_usec = 0; } @@ -913,7 +934,7 @@ ssdpDiscoverDevices(const char * const deviceTypes[], tmp->scope_id = scope_id; devlist = tmp; } - if (gettimeofday(¤t, NULL) >= 0) { + if (upnp_gettimeofday(¤t) >= 0) { /* exit the loop if delay is reached */ long interval = (current.tv_sec - start.tv_sec) * 1000; interval += (current.tv_usec - start.tv_usec) / 1000;