|
Packit |
514978 |
|
|
Packit |
514978 |
/* Copyright 1998 by the Massachusetts Institute of Technology.
|
|
Packit |
514978 |
*
|
|
Packit |
514978 |
* Permission to use, copy, modify, and distribute this
|
|
Packit |
514978 |
* software and its documentation for any purpose and without
|
|
Packit |
514978 |
* fee is hereby granted, provided that the above copyright
|
|
Packit |
514978 |
* notice appear in all copies and that both that copyright
|
|
Packit |
514978 |
* notice and this permission notice appear in supporting
|
|
Packit |
514978 |
* documentation, and that the name of M.I.T. not be used in
|
|
Packit |
514978 |
* advertising or publicity pertaining to distribution of the
|
|
Packit |
514978 |
* software without specific, written prior permission.
|
|
Packit |
514978 |
* M.I.T. makes no representations about the suitability of
|
|
Packit |
514978 |
* this software for any purpose. It is provided "as is"
|
|
Packit |
514978 |
* without express or implied warranty.
|
|
Packit |
514978 |
*/
|
|
Packit |
514978 |
|
|
Packit |
514978 |
#include "ares_setup.h"
|
|
Packit |
514978 |
|
|
Packit |
514978 |
#ifdef HAVE_LIMITS_H
|
|
Packit |
514978 |
#include <limits.h>
|
|
Packit |
514978 |
#endif
|
|
Packit |
514978 |
|
|
Packit |
514978 |
#include "ares.h"
|
|
Packit |
514978 |
#include "ares_private.h"
|
|
Packit |
514978 |
|
|
Packit |
514978 |
/* return time offset between now and (future) check, in milliseconds */
|
|
Packit |
514978 |
static long timeoffset(struct timeval *now, struct timeval *check)
|
|
Packit |
514978 |
{
|
|
Packit |
514978 |
return (check->tv_sec - now->tv_sec)*1000 +
|
|
Packit |
514978 |
(check->tv_usec - now->tv_usec)/1000;
|
|
Packit |
514978 |
}
|
|
Packit |
514978 |
|
|
Packit |
514978 |
/* WARNING: Beware that this is linear in the number of outstanding
|
|
Packit |
514978 |
* requests! You are probably far better off just calling ares_process()
|
|
Packit |
514978 |
* once per second, rather than calling ares_timeout() to figure out
|
|
Packit |
514978 |
* when to next call ares_process().
|
|
Packit |
514978 |
*/
|
|
Packit |
514978 |
struct timeval *ares_timeout(ares_channel channel, struct timeval *maxtv,
|
|
Packit |
514978 |
struct timeval *tvbuf)
|
|
Packit |
514978 |
{
|
|
Packit |
514978 |
struct query *query;
|
|
Packit |
514978 |
struct list_node* list_head;
|
|
Packit |
514978 |
struct list_node* list_node;
|
|
Packit |
514978 |
struct timeval now;
|
|
Packit |
514978 |
struct timeval nextstop;
|
|
Packit |
514978 |
long offset, min_offset;
|
|
Packit |
514978 |
|
|
Packit |
514978 |
/* No queries, no timeout (and no fetch of the current time). */
|
|
Packit |
514978 |
if (ares__is_list_empty(&(channel->all_queries)))
|
|
Packit |
514978 |
return maxtv;
|
|
Packit |
514978 |
|
|
Packit |
514978 |
/* Find the minimum timeout for the current set of queries. */
|
|
Packit |
514978 |
now = ares__tvnow();
|
|
Packit |
514978 |
min_offset = -1;
|
|
Packit |
514978 |
|
|
Packit |
514978 |
list_head = &(channel->all_queries);
|
|
Packit |
514978 |
for (list_node = list_head->next; list_node != list_head;
|
|
Packit |
514978 |
list_node = list_node->next)
|
|
Packit |
514978 |
{
|
|
Packit |
514978 |
query = list_node->data;
|
|
Packit |
514978 |
if (query->timeout.tv_sec == 0)
|
|
Packit |
514978 |
continue;
|
|
Packit |
514978 |
offset = timeoffset(&now, &query->timeout);
|
|
Packit |
514978 |
if (offset < 0)
|
|
Packit |
514978 |
offset = 0;
|
|
Packit |
514978 |
if (min_offset == -1 || offset < min_offset)
|
|
Packit |
514978 |
min_offset = offset;
|
|
Packit |
514978 |
}
|
|
Packit |
514978 |
|
|
Packit |
514978 |
/* If we found a minimum timeout and it's sooner than the one specified in
|
|
Packit |
514978 |
* maxtv (if any), return it. Otherwise go with maxtv.
|
|
Packit |
514978 |
*/
|
|
Packit |
514978 |
if (min_offset != -1)
|
|
Packit |
514978 |
{
|
|
Packit |
514978 |
int ioffset = (min_offset > (long)INT_MAX) ? INT_MAX : (int)min_offset;
|
|
Packit |
514978 |
|
|
Packit |
514978 |
nextstop.tv_sec = ioffset/1000;
|
|
Packit |
514978 |
nextstop.tv_usec = (ioffset%1000)*1000;
|
|
Packit |
514978 |
|
|
Packit |
514978 |
if (!maxtv || ares__timedout(maxtv, &nextstop))
|
|
Packit |
514978 |
{
|
|
Packit |
514978 |
*tvbuf = nextstop;
|
|
Packit |
514978 |
return tvbuf;
|
|
Packit |
514978 |
}
|
|
Packit |
514978 |
}
|
|
Packit |
514978 |
|
|
Packit |
514978 |
return maxtv;
|
|
Packit |
514978 |
}
|