1
0
mirror of https://github.com/moparisthebest/curl synced 2024-12-21 15:48:49 -05:00

timediff: return timediff_t from the time diff functions

... to cater for systems with unsigned time_t variables.

- Renamed the functions to curlx_timediff and Curl_timediff_us.

- Added overflow protection for both of them in either direction for
  both 32 bit and 64 bit time_ts

- Reprefixed the curlx_time functions to use Curl_*

Reported-by: Peter Piekarski
Fixes #2004
Closes #2005
This commit is contained in:
Daniel Stenberg 2017-10-23 12:05:49 +02:00
parent 016c6a6abb
commit b9d25f9a6b
22 changed files with 214 additions and 110 deletions

View File

@ -401,7 +401,7 @@ CURLcode Curl_resolver_wait_resolv(struct connectdata *conn,
result = CURLE_ABORTED_BY_CALLBACK; result = CURLE_ABORTED_BY_CALLBACK;
else { else {
struct curltime now2 = Curl_tvnow(); struct curltime now2 = Curl_tvnow();
time_t timediff = Curl_tvdiff(now2, now); /* spent time */ timediff_t timediff = Curl_timediff(now2, now); /* spent time */
if(timediff <= 0) if(timediff <= 0)
timeout -= 1; /* always deduct at least 1 */ timeout -= 1; /* always deduct at least 1 */
else if(timediff > timeout) else if(timediff > timeout)

View File

@ -535,7 +535,8 @@ CURLcode Curl_resolver_is_resolved(struct connectdata *conn,
} }
else { else {
/* poll for name lookup done with exponential backoff up to 250ms */ /* poll for name lookup done with exponential backoff up to 250ms */
time_t elapsed = Curl_tvdiff(Curl_tvnow(), data->progress.t_startsingle); timediff_t elapsed = Curl_timediff(Curl_tvnow(),
data->progress.t_startsingle);
if(elapsed < 0) if(elapsed < 0)
elapsed = 0; elapsed = 0;

View File

@ -225,10 +225,10 @@ time_t Curl_timeleft(struct Curl_easy *data,
/* subtract elapsed time */ /* subtract elapsed time */
if(duringconnect) if(duringconnect)
/* since this most recent connect started */ /* since this most recent connect started */
timeout_ms -= Curl_tvdiff(*nowp, data->progress.t_startsingle); timeout_ms -= Curl_timediff(*nowp, data->progress.t_startsingle);
else else
/* since the entire operation started */ /* since the entire operation started */
timeout_ms -= Curl_tvdiff(*nowp, data->progress.t_startop); timeout_ms -= Curl_timediff(*nowp, data->progress.t_startop);
if(!timeout_ms) if(!timeout_ms)
/* avoid returning 0 as that means no timeout! */ /* avoid returning 0 as that means no timeout! */
return -1; return -1;
@ -765,7 +765,7 @@ CURLcode Curl_is_connected(struct connectdata *conn,
if(rc == 0) { /* no connection yet */ if(rc == 0) { /* no connection yet */
error = 0; error = 0;
if(curlx_tvdiff(now, conn->connecttime) >= conn->timeoutms_per_addr) { if(Curl_timediff(now, conn->connecttime) >= conn->timeoutms_per_addr) {
infof(data, "After %ldms connect time, move on!\n", infof(data, "After %ldms connect time, move on!\n",
conn->timeoutms_per_addr); conn->timeoutms_per_addr);
error = ETIMEDOUT; error = ETIMEDOUT;
@ -773,7 +773,7 @@ CURLcode Curl_is_connected(struct connectdata *conn,
/* should we try another protocol family? */ /* should we try another protocol family? */
if(i == 0 && conn->tempaddr[1] == NULL && if(i == 0 && conn->tempaddr[1] == NULL &&
curlx_tvdiff(now, conn->connecttime) >= HAPPY_EYEBALLS_TIMEOUT) { Curl_timediff(now, conn->connecttime) >= HAPPY_EYEBALLS_TIMEOUT) {
trynextip(conn, sockindex, 1); trynextip(conn, sockindex, 1);
} }
} }

View File

@ -586,12 +586,12 @@ static CURLcode wait_or_timeout(struct Curl_multi *multi, struct events *ev)
} }
/* get the time stamp to use to figure out how long poll takes */ /* get the time stamp to use to figure out how long poll takes */
before = curlx_tvnow(); before = Curl_tvnow();
/* wait for activity or timeout */ /* wait for activity or timeout */
pollrc = Curl_poll(fds, numfds, (int)ev->ms); pollrc = Curl_poll(fds, numfds, (int)ev->ms);
after = curlx_tvnow(); after = Curl_tvnow();
ev->msbump = FALSE; /* reset here */ ev->msbump = FALSE; /* reset here */
@ -619,7 +619,7 @@ static CURLcode wait_or_timeout(struct Curl_multi *multi, struct events *ev)
/* If nothing updated the timeout, we decrease it by the spent time. /* If nothing updated the timeout, we decrease it by the spent time.
* If it was updated, it has the new timeout time stored already. * If it was updated, it has the new timeout time stored already.
*/ */
time_t timediff = curlx_tvdiff(after, before); timediff_t timediff = Curl_timediff(after, before);
if(timediff > 0) { if(timediff > 0) {
if(timediff > ev->ms) if(timediff > ev->ms)
ev->ms = 0; ev->ms = 0;
@ -680,17 +680,17 @@ static CURLcode easy_transfer(struct Curl_multi *multi)
int still_running = 0; int still_running = 0;
int rc; int rc;
before = curlx_tvnow(); before = Curl_tvnow();
mcode = curl_multi_wait(multi, NULL, 0, 1000, &rc); mcode = curl_multi_wait(multi, NULL, 0, 1000, &rc);
if(!mcode) { if(!mcode) {
if(!rc) { if(!rc) {
struct curltime after = curlx_tvnow(); struct curltime after = Curl_tvnow();
/* If it returns without any filedescriptor instantly, we need to /* If it returns without any filedescriptor instantly, we need to
avoid busy-looping during periods where it has nothing particular avoid busy-looping during periods where it has nothing particular
to wait for */ to wait for */
if(curlx_tvdiff(after, before) <= 10) { if(Curl_timediff(after, before) <= 10) {
without_fds++; without_fds++;
if(without_fds > 2) { if(without_fds > 2) {
int sleep_ms = without_fds < 10 ? (1 << (without_fds - 1)) : 1000; int sleep_ms = without_fds < 10 ? (1 << (without_fds - 1)) : 1000;

View File

@ -351,7 +351,7 @@ static time_t ftp_timeleft_accept(struct Curl_easy *data)
timeout_ms = other; timeout_ms = other;
else { else {
/* subtract elapsed time */ /* subtract elapsed time */
timeout_ms -= Curl_tvdiff(now, data->progress.t_acceptdata); timeout_ms -= Curl_timediff(now, data->progress.t_acceptdata);
if(!timeout_ms) if(!timeout_ms)
/* avoid returning 0 as that means no timeout! */ /* avoid returning 0 as that means no timeout! */
return -1; return -1;

View File

@ -688,8 +688,8 @@ clean_up:
the time we spent until now! */ the time we spent until now! */
if(prev_alarm) { if(prev_alarm) {
/* there was an alarm() set before us, now put it back */ /* there was an alarm() set before us, now put it back */
unsigned long elapsed_secs = (unsigned long) (Curl_tvdiff(Curl_tvnow(), timediff_t elapsed_secs = Curl_timediff(Curl_tvnow(),
conn->created) / 1000); conn->created) / 1000;
/* the alarm period is counted in even number of seconds */ /* the alarm period is counted in even number of seconds */
unsigned long alarm_set = prev_alarm - elapsed_secs; unsigned long alarm_set = prev_alarm - elapsed_secs;

View File

@ -1380,23 +1380,23 @@ static CURLMcode multi_runsingle(struct Curl_multi *multi,
/* Handle timed out */ /* Handle timed out */
if(data->mstate == CURLM_STATE_WAITRESOLVE) if(data->mstate == CURLM_STATE_WAITRESOLVE)
failf(data, "Resolving timed out after %ld milliseconds", failf(data, "Resolving timed out after %ld milliseconds",
Curl_tvdiff(now, data->progress.t_startsingle)); Curl_timediff(now, data->progress.t_startsingle));
else if(data->mstate == CURLM_STATE_WAITCONNECT) else if(data->mstate == CURLM_STATE_WAITCONNECT)
failf(data, "Connection timed out after %ld milliseconds", failf(data, "Connection timed out after %ld milliseconds",
Curl_tvdiff(now, data->progress.t_startsingle)); Curl_timediff(now, data->progress.t_startsingle));
else { else {
k = &data->req; k = &data->req;
if(k->size != -1) { if(k->size != -1) {
failf(data, "Operation timed out after %ld milliseconds with %" failf(data, "Operation timed out after %ld milliseconds with %"
CURL_FORMAT_CURL_OFF_T " out of %" CURL_FORMAT_CURL_OFF_T " out of %"
CURL_FORMAT_CURL_OFF_T " bytes received", CURL_FORMAT_CURL_OFF_T " bytes received",
Curl_tvdiff(now, data->progress.t_startsingle), Curl_timediff(now, data->progress.t_startsingle),
k->bytecount, k->size); k->bytecount, k->size);
} }
else { else {
failf(data, "Operation timed out after %ld milliseconds with %" failf(data, "Operation timed out after %ld milliseconds with %"
CURL_FORMAT_CURL_OFF_T " bytes received", CURL_FORMAT_CURL_OFF_T " bytes received",
Curl_tvdiff(now, data->progress.t_startsingle), Curl_timediff(now, data->progress.t_startsingle),
k->bytecount); k->bytecount);
} }
} }
@ -2514,9 +2514,9 @@ static CURLMcode add_next_timeout(struct curltime now,
timeout in *tv */ timeout in *tv */
for(e = list->head; e;) { for(e = list->head; e;) {
struct curl_llist_element *n = e->next; struct curl_llist_element *n = e->next;
time_t diff; timediff_t diff;
node = (struct time_node *)e->ptr; node = (struct time_node *)e->ptr;
diff = curlx_tvdiff(node->time, now); diff = Curl_timediff(node->time, now);
if(diff <= 0) if(diff <= 0)
/* remove outdated entry */ /* remove outdated entry */
Curl_llist_remove(list, e, NULL); Curl_llist_remove(list, e, NULL);
@ -2790,8 +2790,8 @@ static CURLMcode multi_timeout(struct Curl_multi *multi,
if(Curl_splaycomparekeys(multi->timetree->key, now) > 0) { if(Curl_splaycomparekeys(multi->timetree->key, now) > 0) {
/* some time left before expiration */ /* some time left before expiration */
*timeout_ms = (long)curlx_tvdiff(multi->timetree->key, now); timediff_t diff = Curl_timediff(multi->timetree->key, now);
if(!*timeout_ms) if(diff <= 0)
/* /*
* Since we only provide millisecond resolution on the returned value * Since we only provide millisecond resolution on the returned value
* and the diff might be less than one millisecond here, we don't * and the diff might be less than one millisecond here, we don't
@ -2800,6 +2800,10 @@ static CURLMcode multi_timeout(struct Curl_multi *multi,
* millisecond! instead we return 1 until the time is ripe. * millisecond! instead we return 1 until the time is ripe.
*/ */
*timeout_ms = 1; *timeout_ms = 1;
else
/* this should be safe even on 64 bit archs, as we don't use that
overly long timeouts */
*timeout_ms = (long)diff;
} }
else else
/* 0 means immediately */ /* 0 means immediately */
@ -2906,7 +2910,7 @@ multi_addtimeout(struct Curl_easy *data,
/* find the correct spot in the list */ /* find the correct spot in the list */
for(e = timeoutlist->head; e; e = e->next) { for(e = timeoutlist->head; e; e = e->next) {
struct time_node *check = (struct time_node *)e->ptr; struct time_node *check = (struct time_node *)e->ptr;
time_t diff = curlx_tvdiff(check->time, node->time); timediff_t diff = Curl_timediff(check->time, node->time);
if(diff > 0) if(diff > 0)
break; break;
prev = e; prev = e;
@ -2965,7 +2969,7 @@ void Curl_expire(struct Curl_easy *data, time_t milli, expire_id id)
/* This means that the struct is added as a node in the splay tree. /* This means that the struct is added as a node in the splay tree.
Compare if the new time is earlier, and only remove-old/add-new if it Compare if the new time is earlier, and only remove-old/add-new if it
is. */ is. */
time_t diff = curlx_tvdiff(set, *nowp); timediff_t diff = Curl_timediff(set, *nowp);
if(diff > 0) { if(diff > 0) {
/* The current splay tree entry is sooner than this new expiry time. /* The current splay tree entry is sooner than this new expiry time.

View File

@ -61,12 +61,12 @@ time_t Curl_pp_state_timeout(struct pingpong *pp)
/* Without a requested timeout, we only wait 'response_time' seconds for the /* Without a requested timeout, we only wait 'response_time' seconds for the
full response to arrive before we bail out */ full response to arrive before we bail out */
timeout_ms = response_time - timeout_ms = response_time -
Curl_tvdiff(Curl_tvnow(), pp->response); /* spent time */ Curl_timediff(Curl_tvnow(), pp->response); /* spent time */
if(data->set.timeout) { if(data->set.timeout) {
/* if timeout is requested, find out how much remaining time we have */ /* if timeout is requested, find out how much remaining time we have */
timeout2_ms = data->set.timeout - /* timeout time */ timeout2_ms = data->set.timeout - /* timeout time */
Curl_tvdiff(Curl_tvnow(), conn->now); /* spent time */ Curl_timediff(Curl_tvnow(), conn->now); /* spent time */
/* pick the lowest number */ /* pick the lowest number */
timeout_ms = CURLMIN(timeout_ms, timeout2_ms); timeout_ms = CURLMIN(timeout_ms, timeout2_ms);

View File

@ -212,13 +212,13 @@ void Curl_pgrsTime(struct Curl_easy *data, timerid timer)
/* this is the normal end-of-transfer thing */ /* this is the normal end-of-transfer thing */
break; break;
case TIMER_REDIRECT: case TIMER_REDIRECT:
data->progress.t_redirect = Curl_tvdiff_us(now, data->progress.start); data->progress.t_redirect = Curl_timediff_us(now, data->progress.start);
break; break;
} }
if(delta) { if(delta) {
time_t us = Curl_tvdiff_us(now, data->progress.t_startsingle); timediff_t us = Curl_timediff_us(now, data->progress.t_startsingle);
if(!us) if(us < 1)
us++; /* make sure at least one microsecond passed */ us = 1; /* make sure at least one microsecond passed */
*delta += us; *delta += us;
} }
} }
@ -274,7 +274,7 @@ long Curl_pgrsLimitWaitTime(curl_off_t cursize,
return -1; return -1;
minimum = (time_t) (CURL_OFF_T_C(1000) * size / limit); minimum = (time_t) (CURL_OFF_T_C(1000) * size / limit);
actual = Curl_tvdiff(now, start); actual = Curl_timediff(now, start);
if(actual < minimum) if(actual < minimum)
/* this is a conversion on some systems (64bit time_t => 32bit long) */ /* this is a conversion on some systems (64bit time_t => 32bit long) */
@ -373,7 +373,7 @@ int Curl_pgrsUpdate(struct connectdata *conn)
now = Curl_tvnow(); /* what time is it */ now = Curl_tvnow(); /* what time is it */
/* The time spent so far (from the start) */ /* The time spent so far (from the start) */
data->progress.timespent = Curl_tvdiff_us(now, data->progress.start); data->progress.timespent = Curl_timediff_us(now, data->progress.start);
timespent = (curl_off_t)data->progress.timespent/1000000; /* seconds */ timespent = (curl_off_t)data->progress.timespent/1000000; /* seconds */
/* The average download speed this far */ /* The average download speed this far */
@ -413,7 +413,7 @@ int Curl_pgrsUpdate(struct connectdata *conn)
/* first of all, we don't do this if there's no counted seconds yet */ /* first of all, we don't do this if there's no counted seconds yet */
if(countindex) { if(countindex) {
time_t span_ms; timediff_t span_ms;
/* Get the index position to compare with the 'nowindex' position. /* Get the index position to compare with the 'nowindex' position.
Get the oldest entry possible. While we have less than CURR_TIME Get the oldest entry possible. While we have less than CURR_TIME
@ -422,7 +422,7 @@ int Curl_pgrsUpdate(struct connectdata *conn)
data->progress.speeder_c%CURR_TIME:0; data->progress.speeder_c%CURR_TIME:0;
/* Figure out the exact time for the time span */ /* Figure out the exact time for the time span */
span_ms = Curl_tvdiff(now, span_ms = Curl_timediff(now,
data->progress.speeder_time[checkindex]); data->progress.speeder_time[checkindex]);
if(0 == span_ms) if(0 == span_ms)
span_ms = 1; /* at least one millisecond MUST have passed */ span_ms = 1; /* at least one millisecond MUST have passed */

View File

@ -86,7 +86,7 @@ static CURLcode randit(struct Curl_easy *data, unsigned int *rnd)
#endif #endif
if(!seeded) { if(!seeded) {
struct curltime now = curlx_tvnow(); struct curltime now = Curl_tvnow();
infof(data, "WARNING: Using weak random seed\n"); infof(data, "WARNING: Using weak random seed\n");
randseed += (unsigned int)now.tv_usec + (unsigned int)now.tv_sec; randseed += (unsigned int)now.tv_usec + (unsigned int)now.tv_sec;
randseed = randseed * 1103515245 + 12345; randseed = randseed * 1103515245 + 12345;

View File

@ -51,7 +51,7 @@
#include "warnless.h" #include "warnless.h"
/* Convenience local macros */ /* Convenience local macros */
#define ELAPSED_MS() (int)curlx_tvdiff(curlx_tvnow(), initial_tv) #define ELAPSED_MS() (int)Curl_timediff(Curl_tvnow(), initial_tv)
int Curl_ack_eintr = 0; int Curl_ack_eintr = 0;
#define ERROR_NOT_EINTR(error) (Curl_ack_eintr || error != EINTR) #define ERROR_NOT_EINTR(error) (Curl_ack_eintr || error != EINTR)
@ -96,7 +96,7 @@ int Curl_wait_ms(int timeout_ms)
Sleep(timeout_ms); Sleep(timeout_ms);
#else #else
pending_ms = timeout_ms; pending_ms = timeout_ms;
initial_tv = curlx_tvnow(); initial_tv = Curl_tvnow();
do { do {
#if defined(HAVE_POLL_FINE) #if defined(HAVE_POLL_FINE)
r = poll(NULL, 0, pending_ms); r = poll(NULL, 0, pending_ms);
@ -184,7 +184,7 @@ int Curl_socket_check(curl_socket_t readfd0, /* two sockets to read from */
if(timeout_ms > 0) { if(timeout_ms > 0) {
pending_ms = (int)timeout_ms; pending_ms = (int)timeout_ms;
initial_tv = curlx_tvnow(); initial_tv = Curl_tvnow();
} }
#ifdef HAVE_POLL_FINE #ifdef HAVE_POLL_FINE
@ -425,7 +425,7 @@ int Curl_poll(struct pollfd ufds[], unsigned int nfds, int timeout_ms)
if(timeout_ms > 0) { if(timeout_ms > 0) {
pending_ms = timeout_ms; pending_ms = timeout_ms;
initial_tv = curlx_tvnow(); initial_tv = Curl_tvnow();
} }
#ifdef HAVE_POLL_FINE #ifdef HAVE_POLL_FINE

View File

@ -46,7 +46,7 @@ CURLcode Curl_speedcheck(struct Curl_easy *data,
data->state.keeps_speed = now; data->state.keeps_speed = now;
else { else {
/* how long has it been under the limit */ /* how long has it been under the limit */
time_t howlong = Curl_tvdiff(now, data->state.keeps_speed); timediff_t howlong = Curl_timediff(now, data->state.keeps_speed);
if(howlong >= data->set.low_speed_time * 1000) { if(howlong >= data->set.low_speed_time * 1000) {
/* too long */ /* too long */

View File

@ -1561,7 +1561,7 @@ static CURLcode telnet_do(struct connectdata *conn, bool *done)
if(data->set.timeout) { if(data->set.timeout) {
now = Curl_tvnow(); now = Curl_tvnow();
if(Curl_tvdiff(now, conn->created) >= data->set.timeout) { if(Curl_timediff(now, conn->created) >= data->set.timeout) {
failf(data, "Time-out"); failf(data, "Time-out");
result = CURLE_OPERATION_TIMEDOUT; result = CURLE_OPERATION_TIMEDOUT;
keepon = FALSE; keepon = FALSE;
@ -1679,7 +1679,7 @@ static CURLcode telnet_do(struct connectdata *conn, bool *done)
if(data->set.timeout) { if(data->set.timeout) {
now = Curl_tvnow(); now = Curl_tvnow();
if(Curl_tvdiff(now, conn->created) >= data->set.timeout) { if(Curl_timediff(now, conn->created) >= data->set.timeout) {
failf(data, "Time-out"); failf(data, "Time-out");
result = CURLE_OPERATION_TIMEDOUT; result = CURLE_OPERATION_TIMEDOUT;
keepon = FALSE; keepon = FALSE;

View File

@ -24,7 +24,7 @@
#if defined(WIN32) && !defined(MSDOS) #if defined(WIN32) && !defined(MSDOS)
struct curltime curlx_tvnow(void) struct curltime Curl_tvnow(void)
{ {
/* /*
** GetTickCount() is available on _all_ Windows versions from W95 up ** GetTickCount() is available on _all_ Windows versions from W95 up
@ -48,7 +48,7 @@ struct curltime curlx_tvnow(void)
#elif defined(HAVE_CLOCK_GETTIME_MONOTONIC) #elif defined(HAVE_CLOCK_GETTIME_MONOTONIC)
struct curltime curlx_tvnow(void) struct curltime Curl_tvnow(void)
{ {
/* /*
** clock_gettime() is granted to be increased monotonically when the ** clock_gettime() is granted to be increased monotonically when the
@ -86,7 +86,7 @@ struct curltime curlx_tvnow(void)
#elif defined(HAVE_GETTIMEOFDAY) #elif defined(HAVE_GETTIMEOFDAY)
struct curltime curlx_tvnow(void) struct curltime Curl_tvnow(void)
{ {
/* /*
** gettimeofday() is not granted to be increased monotonically, due to ** gettimeofday() is not granted to be increased monotonically, due to
@ -103,7 +103,7 @@ struct curltime curlx_tvnow(void)
#else #else
struct curltime curlx_tvnow(void) struct curltime Curl_tvnow(void)
{ {
/* /*
** time() returns the value of time in seconds since the Epoch. ** time() returns the value of time in seconds since the Epoch.
@ -116,6 +116,14 @@ struct curltime curlx_tvnow(void)
#endif #endif
#if SIZEOF_TIME_T < 8
#define TIME_MAX INT_MAX
#define TIME_MIN INT_MIN
#else
#define TIME_MAX 9223372036854775807LL
#define TIME_MIN -9223372036854775807LL
#endif
/* /*
* Make sure that the first argument is the more recent time, as otherwise * Make sure that the first argument is the more recent time, as otherwise
* we'll get a weird negative time-diff back... * we'll get a weird negative time-diff back...
@ -125,17 +133,15 @@ struct curltime curlx_tvnow(void)
* *
* @unittest: 1323 * @unittest: 1323
*/ */
time_t curlx_tvdiff(struct curltime newer, struct curltime older) timediff_t Curl_timediff(struct curltime newer, struct curltime older)
{ {
#if SIZEOF_TIME_T < 8 timediff_t diff = newer.tv_sec-older.tv_sec;
/* for 32bit time_t systems, add a precaution to avoid overflow for really if(diff >= (TIME_MAX/1000))
big time differences */ return TIME_MAX;
time_t diff = newer.tv_sec-older.tv_sec; else if(diff <= (TIME_MIN/1000))
if(diff >= (0x7fffffff/1000)) return TIME_MIN;
return 0x7fffffff; return (timediff_t)(newer.tv_sec-older.tv_sec)*1000+
#endif (timediff_t)(newer.tv_usec-older.tv_usec)/1000;
return (newer.tv_sec-older.tv_sec)*1000+
(int)(newer.tv_usec-older.tv_usec)/1000;
} }
/* /*
@ -145,18 +151,13 @@ time_t curlx_tvdiff(struct curltime newer, struct curltime older)
* Returns: the time difference in number of microseconds. For too large diffs * Returns: the time difference in number of microseconds. For too large diffs
* it returns max value. * it returns max value.
*/ */
time_t Curl_tvdiff_us(struct curltime newer, struct curltime older) timediff_t Curl_timediff_us(struct curltime newer, struct curltime older)
{ {
time_t diff = newer.tv_sec-older.tv_sec; timediff_t diff = newer.tv_sec-older.tv_sec;
#if SIZEOF_TIME_T < 8 if(diff >= (TIME_MAX/1000000))
/* for 32bit time_t systems */ return TIME_MAX;
if(diff >= (0x7fffffff/1000000)) else if(diff <= (TIME_MIN/1000000))
return 0x7fffffff; return TIME_MIN;
#else return (timediff_t)(newer.tv_sec-older.tv_sec)*1000000+
/* for 64bit time_t systems */ (timediff_t)(newer.tv_usec-older.tv_usec);
if(diff >= (0x7fffffffffffffffLL/1000000))
return 0x7fffffffffffffffLL;
#endif
return (newer.tv_sec-older.tv_sec)*1000000+
(int)(newer.tv_usec-older.tv_usec);
} }

View File

@ -22,19 +22,21 @@
* *
***************************************************************************/ ***************************************************************************/
/*
* CAUTION: this header is designed to work when included by the app-side
* as well as the library. Do not mix with library internals!
*/
#include "curl_setup.h" #include "curl_setup.h"
#if SIZEOF_TIME_T < 8
typedef int timediff_t;
#else
typedef ssize_t timediff_t;
#endif
struct curltime { struct curltime {
time_t tv_sec; /* seconds */ time_t tv_sec; /* seconds */
unsigned int tv_usec; /* microseconds */ int tv_usec; /* microseconds */
}; };
struct curltime curlx_tvnow(void); struct curltime Curl_tvnow(void);
/* /*
* Make sure that the first argument (t1) is the more recent time and t2 is * Make sure that the first argument (t1) is the more recent time and t2 is
@ -42,7 +44,7 @@ struct curltime curlx_tvnow(void);
* *
* Returns: the time difference in number of milliseconds. * Returns: the time difference in number of milliseconds.
*/ */
time_t curlx_tvdiff(struct curltime t1, struct curltime t2); timediff_t Curl_timediff(struct curltime t1, struct curltime t2);
/* /*
* Make sure that the first argument (t1) is the more recent time and t2 is * Make sure that the first argument (t1) is the more recent time and t2 is
@ -50,12 +52,7 @@ time_t curlx_tvdiff(struct curltime t1, struct curltime t2);
* *
* Returns: the time difference in number of microseconds. * Returns: the time difference in number of microseconds.
*/ */
time_t Curl_tvdiff_us(struct curltime newer, struct curltime older); timediff_t Curl_timediff_us(struct curltime newer, struct curltime older);
/* These two defines below exist to provide the older API for library
internals only. */
#define Curl_tvnow() curlx_tvnow()
#define Curl_tvdiff(x,y) curlx_tvdiff(x,y)
#endif /* HEADER_CURL_TIMEVAL_H */ #endif /* HEADER_CURL_TIMEVAL_H */

View File

@ -1174,7 +1174,7 @@ CURLcode Curl_readwrite(struct connectdata *conn,
*/ */
time_t ms = Curl_tvdiff(k->now, k->start100); timediff_t ms = Curl_timediff(k->now, k->start100);
if(ms >= data->set.expect_100_timeout) { if(ms >= data->set.expect_100_timeout) {
/* we've waited long enough, continue anyway */ /* we've waited long enough, continue anyway */
k->exp100 = EXP100_SEND_DATA; k->exp100 = EXP100_SEND_DATA;
@ -1198,13 +1198,14 @@ CURLcode Curl_readwrite(struct connectdata *conn,
failf(data, "Operation timed out after %ld milliseconds with %" failf(data, "Operation timed out after %ld milliseconds with %"
CURL_FORMAT_CURL_OFF_T " out of %" CURL_FORMAT_CURL_OFF_T " out of %"
CURL_FORMAT_CURL_OFF_T " bytes received", CURL_FORMAT_CURL_OFF_T " bytes received",
Curl_tvdiff(k->now, data->progress.t_startsingle), k->bytecount, Curl_timediff(k->now, data->progress.t_startsingle),
k->size); k->bytecount, k->size);
} }
else { else {
failf(data, "Operation timed out after %ld milliseconds with %" failf(data, "Operation timed out after %ld milliseconds with %"
CURL_FORMAT_CURL_OFF_T " bytes received", CURL_FORMAT_CURL_OFF_T " bytes received",
Curl_tvdiff(k->now, data->progress.t_startsingle), k->bytecount); Curl_timediff(k->now, data->progress.t_startsingle),
k->bytecount);
} }
return CURLE_OPERATION_TIMEDOUT; return CURLE_OPERATION_TIMEDOUT;
} }

View File

@ -3459,8 +3459,8 @@ Curl_oldest_idle_connection(struct Curl_easy *data)
struct curl_hash_iterator iter; struct curl_hash_iterator iter;
struct curl_llist_element *curr; struct curl_llist_element *curr;
struct curl_hash_element *he; struct curl_hash_element *he;
time_t highscore =- 1; timediff_t highscore =- 1;
time_t score; timediff_t score;
struct curltime now; struct curltime now;
struct connectdata *conn_candidate = NULL; struct connectdata *conn_candidate = NULL;
struct connectbundle *bundle; struct connectbundle *bundle;
@ -3481,7 +3481,7 @@ Curl_oldest_idle_connection(struct Curl_easy *data)
if(!conn->inuse) { if(!conn->inuse) {
/* Set higher score for the age passed since the connection was used */ /* Set higher score for the age passed since the connection was used */
score = Curl_tvdiff(now, conn->now); score = Curl_timediff(now, conn->now);
if(score > highscore) { if(score > highscore) {
highscore = score; highscore = score;
@ -3522,8 +3522,8 @@ find_oldest_idle_connection_in_bundle(struct Curl_easy *data,
struct connectbundle *bundle) struct connectbundle *bundle)
{ {
struct curl_llist_element *curr; struct curl_llist_element *curr;
time_t highscore = -1; timediff_t highscore = -1;
time_t score; timediff_t score;
struct curltime now; struct curltime now;
struct connectdata *conn_candidate = NULL; struct connectdata *conn_candidate = NULL;
struct connectdata *conn; struct connectdata *conn;
@ -3538,7 +3538,7 @@ find_oldest_idle_connection_in_bundle(struct Curl_easy *data,
if(!conn->inuse) { if(!conn->inuse) {
/* Set higher score for the age passed since the connection was used */ /* Set higher score for the age passed since the connection was used */
score = Curl_tvdiff(now, conn->now); score = Curl_timediff(now, conn->now);
if(score > highscore) { if(score > highscore) {
highscore = score; highscore = score;
@ -3613,7 +3613,7 @@ static int call_disconnect_if_dead(struct connectdata *conn,
static void prune_dead_connections(struct Curl_easy *data) static void prune_dead_connections(struct Curl_easy *data)
{ {
struct curltime now = Curl_tvnow(); struct curltime now = Curl_tvnow();
time_t elapsed = Curl_tvdiff(now, data->state.conn_cache->last_cleanup); time_t elapsed = Curl_timediff(now, data->state.conn_cache->last_cleanup);
if(elapsed >= 1000L) { if(elapsed >= 1000L) {
Curl_conncache_foreach(data->state.conn_cache, data, Curl_conncache_foreach(data->state.conn_cache, data,

View File

@ -446,14 +446,14 @@ static CURLcode Curl_ossl_seed(struct Curl_easy *data)
size_t len = sizeof(randb); size_t len = sizeof(randb);
size_t i, i_max; size_t i, i_max;
for(i = 0, i_max = len / sizeof(struct curltime); i < i_max; ++i) { for(i = 0, i_max = len / sizeof(struct curltime); i < i_max; ++i) {
struct curltime tv = curlx_tvnow(); struct curltime tv = Curl_tvnow();
Curl_wait_ms(1); Curl_wait_ms(1);
tv.tv_sec *= i + 1; tv.tv_sec *= i + 1;
tv.tv_usec *= (unsigned int)i + 2; tv.tv_usec *= (unsigned int)i + 2;
tv.tv_sec ^= ((curlx_tvnow().tv_sec + curlx_tvnow().tv_usec) * tv.tv_sec ^= ((Curl_tvnow().tv_sec + Curl_tvnow().tv_usec) *
(i + 3)) << 8; (i + 3)) << 8;
tv.tv_usec ^= (unsigned int) ((curlx_tvnow().tv_sec + tv.tv_usec ^= (unsigned int) ((Curl_tvnow().tv_sec +
curlx_tvnow().tv_usec) * Curl_tvnow().tv_usec) *
(i + 4)) << 16; (i + 4)) << 16;
memcpy(&randb[i * sizeof(struct curltime)], &tv, memcpy(&randb[i * sizeof(struct curltime)], &tv,
sizeof(struct curltime)); sizeof(struct curltime));

View File

@ -4,14 +4,12 @@ CURLX_SRCS = \
../../lib/mprintf.c \ ../../lib/mprintf.c \
../../lib/nonblock.c \ ../../lib/nonblock.c \
../../lib/strtoofft.c \ ../../lib/strtoofft.c \
../../lib/timeval.c \
../../lib/warnless.c ../../lib/warnless.c
CURLX_HDRS = \ CURLX_HDRS = \
../../lib/curlx.h \ ../../lib/curlx.h \
../../lib/nonblock.h \ ../../lib/nonblock.h \
../../lib/strtoofft.h \ ../../lib/strtoofft.h \
../../lib/timeval.h \
../../lib/warnless.h ../../lib/warnless.h
USEFUL = \ USEFUL = \

View File

@ -67,6 +67,8 @@ const struct in6_addr in6addr_any = {{ IN6ADDR_ANY_INIT }};
#endif /* w32api < 3.6 */ #endif /* w32api < 3.6 */
#endif /* ENABLE_IPV6 && __MINGW32__*/ #endif /* ENABLE_IPV6 && __MINGW32__*/
static struct timeval tvnow(void);
/* This function returns a pointer to STATIC memory. It converts the given /* This function returns a pointer to STATIC memory. It converts the given
* binary lump to a hex formatted string usable for output in logs or * binary lump to a hex formatted string usable for output in logs or
* whatever. * whatever.
@ -100,7 +102,7 @@ void logmsg(const char *msg, ...)
char buffer[2048 + 1]; char buffer[2048 + 1];
FILE *logfp; FILE *logfp;
int error; int error;
struct curltime tv; struct timeval tv;
time_t sec; time_t sec;
struct tm *now; struct tm *now;
char timebuf[20]; char timebuf[20];
@ -112,7 +114,7 @@ void logmsg(const char *msg, ...)
return; return;
} }
tv = curlx_tvnow(); tv = tvnow();
if(!known_offset) { if(!known_offset) {
epoch_offset = time(NULL) - tv.tv_sec; epoch_offset = time(NULL) - tv.tv_sec;
known_offset = 1; known_offset = 1;
@ -213,7 +215,7 @@ int wait_ms(int timeout_ms)
#ifndef HAVE_POLL_FINE #ifndef HAVE_POLL_FINE
struct timeval pending_tv; struct timeval pending_tv;
#endif #endif
struct curltime initial_tv; struct timeval initial_tv;
int pending_ms; int pending_ms;
int error; int error;
#endif #endif
@ -231,7 +233,7 @@ int wait_ms(int timeout_ms)
Sleep(timeout_ms); Sleep(timeout_ms);
#else #else
pending_ms = timeout_ms; pending_ms = timeout_ms;
initial_tv = curlx_tvnow(); initial_tv = tvnow();
do { do {
#if defined(HAVE_POLL_FINE) #if defined(HAVE_POLL_FINE)
r = poll(NULL, 0, pending_ms); r = poll(NULL, 0, pending_ms);
@ -245,7 +247,7 @@ int wait_ms(int timeout_ms)
error = errno; error = errno;
if(error && (error != EINTR)) if(error && (error != EINTR))
break; break;
pending_ms = timeout_ms - (int)curlx_tvdiff(curlx_tvnow(), initial_tv); pending_ms = timeout_ms - (int)timediff(tvnow(), initial_tv);
if(pending_ms <= 0) if(pending_ms <= 0)
break; break;
} while(r == -1); } while(r == -1);
@ -397,3 +399,102 @@ int strncasecompare(const char *first, const char *second, size_t max)
return raw_toupper(*first) == raw_toupper(*second); return raw_toupper(*first) == raw_toupper(*second);
} }
#if defined(WIN32) && !defined(MSDOS)
static struct timeval tvnow(void)
{
/*
** GetTickCount() is available on _all_ Windows versions from W95 up
** to nowadays. Returns milliseconds elapsed since last system boot,
** increases monotonically and wraps once 49.7 days have elapsed.
**
** GetTickCount64() is available on Windows version from Windows Vista
** and Windows Server 2008 up to nowadays. The resolution of the
** function is limited to the resolution of the system timer, which
** is typically in the range of 10 milliseconds to 16 milliseconds.
*/
struct timeval now;
#if defined(_WIN32_WINNT) && (_WIN32_WINNT >= 0x0600)
ULONGLONG milliseconds = GetTickCount64();
#else
DWORD milliseconds = GetTickCount();
#endif
now.tv_sec = (long)(milliseconds / 1000);
now.tv_usec = (milliseconds % 1000) * 1000;
return now;
}
#elif defined(HAVE_CLOCK_GETTIME_MONOTONIC)
static struct timeval tvnow(void)
{
/*
** clock_gettime() is granted to be increased monotonically when the
** monotonic clock is queried. Time starting point is unspecified, it
** could be the system start-up time, the Epoch, or something else,
** in any case the time starting point does not change once that the
** system has started up.
*/
struct timeval now;
struct timespec tsnow;
if(0 == clock_gettime(CLOCK_MONOTONIC, &tsnow)) {
now.tv_sec = tsnow.tv_sec;
now.tv_usec = tsnow.tv_nsec / 1000;
}
/*
** Even when the configure process has truly detected monotonic clock
** availability, it might happen that it is not actually available at
** run-time. When this occurs simply fallback to other time source.
*/
#ifdef HAVE_GETTIMEOFDAY
else
(void)gettimeofday(&now, NULL);
#else
else {
now.tv_sec = (long)time(NULL);
now.tv_usec = 0;
}
#endif
return now;
}
#elif defined(HAVE_GETTIMEOFDAY)
static struct timeval tvnow(void)
{
/*
** gettimeofday() is not granted to be increased monotonically, due to
** clock drifting and external source time synchronization it can jump
** forward or backward in time.
*/
struct timeval now;
(void)gettimeofday(&now, NULL);
return now;
}
#else
static struct timeval tvnow(void)
{
/*
** time() returns the value of time in seconds since the Epoch.
*/
struct timeval now;
now.tv_sec = (long)time(NULL);
now.tv_usec = 0;
return now;
}
#endif
long timediff(struct timeval newer, struct timeval older)
{
timediff_t diff = newer.tv_sec-older.tv_sec;
if(diff >= (LONG_MAX/1000))
return LONG_MAX;
else if(diff <= (LONG_MIN/1000))
return LONG_MIN;
return (long)(newer.tv_sec-older.tv_sec)*1000+
(long)(newer.tv_usec-older.tv_usec)/1000;
}

View File

@ -7,7 +7,7 @@
* | (__| |_| | _ <| |___ * | (__| |_| | _ <| |___
* \___|\___/|_| \_\_____| * \___|\___/|_| \_\_____|
* *
* Copyright (C) 1998 - 2016, Daniel Stenberg, <daniel@haxx.se>, et al. * Copyright (C) 1998 - 2017, Daniel Stenberg, <daniel@haxx.se>, et al.
* *
* This software is licensed as described in the file COPYING, which * This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms * you should have received as part of this distribution. The terms
@ -25,6 +25,7 @@
char *data_to_hex(char *data, size_t len); char *data_to_hex(char *data, size_t len);
void logmsg(const char *msg, ...); void logmsg(const char *msg, ...);
long timediff(struct timeval newer, struct timeval older);
#define TEST_DATA_PATH "%s/data/test%ld" #define TEST_DATA_PATH "%s/data/test%ld"

View File

@ -50,7 +50,7 @@ UNITTEST_START
size_t i; size_t i;
for(i = 0; i < sizeof(tests)/sizeof(tests[0]); i++) { for(i = 0; i < sizeof(tests)/sizeof(tests[0]); i++) {
time_t result = curlx_tvdiff(tests[i].first, tests[i].second); timediff_t result = Curl_timediff(tests[i].first, tests[i].second);
if(result != tests[i].result) { if(result != tests[i].result) {
printf("%d.%06u to %d.%06u got %d, but expected %d\n", printf("%d.%06u to %d.%06u got %d, but expected %d\n",
tests[i].first.tv_sec, tests[i].first.tv_sec,