From 514c20aa99cd2f29c7e5da1e263ab7e81e3a9ff3 Mon Sep 17 00:00:00 2001 From: Ian Jackson Date: Fri, 9 Dec 2016 20:46:40 +0000 Subject: [PATCH] timeout robustness: Introduce adns__timeout_{set,clear} This abstracts away the open-coded handling of qu->timeout. Rename the field to catch all call sites. No functional change. Signed-off-by: Ian Jackson --- src/event.c | 9 +++++++-- src/internal.h | 7 ++++++- src/query.c | 2 +- src/transmit.c | 6 ++---- 4 files changed, 16 insertions(+), 8 deletions(-) diff --git a/src/event.c b/src/event.c index 28cc60c..845bc69 100644 --- a/src/event.c +++ b/src/event.c @@ -149,6 +149,11 @@ void adns__tcp_tryconnect(adns_state ads, struct timeval now) { /* Timeout handling functions. */ +void adns__timeout_set(adns_query qu, struct timeval now, int ms) { + qu->timeout_expires= now; + timevaladd(&qu->timeout_expires,ms); +} + void adns__must_gettimeofday(adns_state ads, const struct timeval **now_io, struct timeval *tv_buf) { const struct timeval *now; @@ -212,8 +217,8 @@ static void timeouts_queue(adns_state ads, int act, for (qu= queue->head; qu; qu= nqu) { nqu= qu->next; - if (!timercmp(&now,&qu->timeout,>)) { - inter_maxtoabs(tv_io,tvbuf,now,qu->timeout); + if (!timercmp(&now,&qu->timeout_expires,>)) { + inter_maxtoabs(tv_io,tvbuf,now,qu->timeout_expires); } else { if (!act) { inter_immed(tv_io,tvbuf); return; } LIST_UNLINK(*queue,qu); diff --git a/src/internal.h b/src/internal.h index 0fd64da..23c7aa6 100644 --- a/src/internal.h +++ b/src/internal.h @@ -298,7 +298,7 @@ struct adns__query { int id, flags, retries; int udpnextserver; unsigned long udpsent; /* bitmap indexed by server */ - struct timeval timeout; + struct timeval timeout_expires; time_t expires; /* Earliest expiry time of any record we used. */ qcontext ctx; @@ -885,6 +885,11 @@ void adns__autosys(adns_state ads, struct timeval now); * lest we end up in recursive descent ! */ +void adns__timeout_set(adns_query qu, struct timeval now, int ms); +static inline void adns__timeout_clear(adns_query qu) + { timerclear(&qu->timeout_expires); } + + void adns__must_gettimeofday(adns_state ads, const struct timeval **now_io, struct timeval *tv_buf); /* Call with care - might reentrantly cause queries to be completed! */ diff --git a/src/query.c b/src/query.c index 393478a..4eba888 100644 --- a/src/query.c +++ b/src/query.c @@ -72,7 +72,7 @@ static adns_query query_alloc(adns_state ads, qu->retries= 0; qu->udpnextserver= 0; qu->udpsent= 0; - timerclear(&qu->timeout); + adns__timeout_clear(qu); qu->expires= now.tv_sec + MAXTTLBELIEVE; memset(&qu->ctx,0,sizeof(qu->ctx)); diff --git a/src/transmit.c b/src/transmit.c index 1674dbc..bf2f8c6 100644 --- a/src/transmit.c +++ b/src/transmit.c @@ -234,8 +234,7 @@ void adns__querysend_tcp(adns_query qu, struct timeval now) { static void query_usetcp(adns_query qu, struct timeval now) { qu->state= query_tcpw; - qu->timeout= now; - timevaladd(&qu->timeout,TCPWAITMS); + adns__timeout_set(qu,now,TCPWAITMS); LIST_LINK_TAIL(qu->ads->tcpw,qu); adns__querysend_tcp(qu,now); adns__tcp_tryconnect(qu->ads,now); @@ -281,8 +280,7 @@ void adns__query_send(adns_query qu, struct timeval now) { if (r<0 && errno != EAGAIN) adns__warn(ads,serv,0,"sendto failed: %s",strerror(errno)); - qu->timeout= now; - timevaladd(&qu->timeout,UDPRETRYMS); + adns__timeout_set(qu, now, UDPRETRYMS); qu->udpsent |= (1<udpnextserver= (serv+1)%ads->nservers; qu->retries++; -- 2.30.2