2 /* Copyright 1998 by the Massachusetts Institute of Technology.
4 * Permission to use, copy, modify, and distribute this
5 * software and its documentation for any purpose and without
6 * fee is hereby granted, provided that the above copyright
7 * notice appear in all copies and that both that copyright
8 * notice and this permission notice appear in supporting
9 * documentation, and that the name of M.I.T. not be used in
10 * advertising or publicity pertaining to distribution of the
11 * software without specific, written prior permission.
12 * M.I.T. makes no representations about the suitability of
13 * this software for any purpose. It is provided "as is"
14 * without express or implied warranty.
17 #include "ares_setup.h"
24 #include "ares_private.h"
26 /* return time offset between now and (future) check, in milliseconds */
27 static long timeoffset(struct timeval *now, struct timeval *check)
29 return (check->tv_sec - now->tv_sec)*1000 +
30 (check->tv_usec - now->tv_usec)/1000;
33 /* WARNING: Beware that this is linear in the number of outstanding
34 * requests! You are probably far better off just calling ares_process()
35 * once per second, rather than calling ares_timeout() to figure out
36 * when to next call ares_process().
38 struct timeval *ares_timeout(ares_channel channel, struct timeval *maxtv,
39 struct timeval *tvbuf)
42 struct list_node* list_head;
43 struct list_node* list_node;
45 struct timeval nextstop;
46 long offset, min_offset;
48 /* No queries, no timeout (and no fetch of the current time). */
49 if (ares__is_list_empty(&(channel->all_queries)))
52 /* Find the minimum timeout for the current set of queries. */
56 list_head = &(channel->all_queries);
57 for (list_node = list_head->next; list_node != list_head;
58 list_node = list_node->next)
60 query = list_node->data;
61 if (query->timeout.tv_sec == 0)
63 offset = timeoffset(&now, &query->timeout);
66 if (min_offset == -1 || offset < min_offset)
70 /* If we found a minimum timeout and it's sooner than the one specified in
71 * maxtv (if any), return it. Otherwise go with maxtv.
75 int ioffset = (min_offset > (long)INT_MAX) ? INT_MAX : (int)min_offset;
77 nextstop.tv_sec = ioffset/1000;
78 nextstop.tv_usec = (ioffset%1000)*1000;
80 if (!maxtv || ares__timedout(maxtv, &nextstop))