1 | /* Copyright 1998 by the Massachusetts Institute of Technology. |
---|
2 | * |
---|
3 | * Permission to use, copy, modify, and distribute this |
---|
4 | * software and its documentation for any purpose and without |
---|
5 | * fee is hereby granted, provided that the above copyright |
---|
6 | * notice appear in all copies and that both that copyright |
---|
7 | * notice and this permission notice appear in supporting |
---|
8 | * documentation, and that the name of M.I.T. not be used in |
---|
9 | * advertising or publicity pertaining to distribution of the |
---|
10 | * software without specific, written prior permission. |
---|
11 | * M.I.T. makes no representations about the suitability of |
---|
12 | * this software for any purpose. It is provided "as is" |
---|
13 | * without express or implied warranty. |
---|
14 | */ |
---|
15 | |
---|
16 | static const char rcsid[] = "$Id: ares_timeout.c,v 1.1 1998-08-13 18:06:35 ghudson Exp $"; |
---|
17 | |
---|
18 | #include <sys/types.h> |
---|
19 | #include <sys/time.h> |
---|
20 | #include <time.h> |
---|
21 | #include "ares.h" |
---|
22 | #include "ares_private.h" |
---|
23 | |
---|
24 | struct timeval *ares_timeout(ares_channel channel, struct timeval *maxtv, |
---|
25 | struct timeval *tvbuf) |
---|
26 | { |
---|
27 | struct query *query; |
---|
28 | time_t now; |
---|
29 | int offset, min_offset; |
---|
30 | |
---|
31 | /* No queries, no timeout (and no fetch of the current time). */ |
---|
32 | if (!channel->queries) |
---|
33 | return maxtv; |
---|
34 | |
---|
35 | /* Find the minimum timeout for the current set of queries. */ |
---|
36 | time(&now); |
---|
37 | min_offset = -1; |
---|
38 | for (query = channel->queries; query; query = query->next) |
---|
39 | { |
---|
40 | if (query->timeout == 0) |
---|
41 | continue; |
---|
42 | offset = query->timeout - now; |
---|
43 | if (offset < 0) |
---|
44 | offset = 0; |
---|
45 | if (min_offset == -1 || offset < min_offset) |
---|
46 | min_offset = offset; |
---|
47 | } |
---|
48 | |
---|
49 | /* If we found a minimum timeout and it's sooner than the one |
---|
50 | * specified in maxtv (if any), return it. Otherwise go with |
---|
51 | * maxtv. |
---|
52 | */ |
---|
53 | if (min_offset != -1 && (!maxtv || min_offset <= maxtv->tv_sec)) |
---|
54 | { |
---|
55 | tvbuf->tv_sec = min_offset; |
---|
56 | tvbuf->tv_usec = 0; |
---|
57 | return tvbuf; |
---|
58 | } |
---|
59 | else |
---|
60 | return maxtv; |
---|
61 | } |
---|