00001 /* Copyright 1998 by the Massachusetts Institute of Technology. 00002 * 00003 * Permission to use, copy, modify, and distribute this 00004 * software and its documentation for any purpose and without 00005 * fee is hereby granted, provided that the above copyright 00006 * notice appear in all copies and that both that copyright 00007 * notice and this permission notice appear in supporting 00008 * documentation, and that the name of M.I.T. not be used in 00009 * advertising or publicity pertaining to distribution of the 00010 * software without specific, written prior permission. 00011 * M.I.T. makes no representations about the suitability of 00012 * this software for any purpose. It is provided "as is" 00013 * without express or implied warranty. 00014 * 00015 * CHANGELOG: this file has been modified by Sergio Perez Alcañiz <serpeal@disca.upv.es> 00016 * Departamento de Informática de Sistemas y Computadores 00017 * Universidad Politécnica de Valencia 00018 * Valencia (Spain) 00019 * Date: April 2003 00020 * 00021 */ 00022 00023 #include "ares_private.h" 00024 00025 struct timeval *ares_timeout(ares_channel channel, struct timeval *maxtv, 00026 struct timeval *tvbuf) 00027 { 00028 struct query *query; 00029 time_t now; 00030 int offset, min_offset; 00031 00032 /* No queries, no timeout (and no fetch of the current time). */ 00033 if (!channel->queries) 00034 return maxtv; 00035 00036 /* Find the minimum timeout for the current set of queries. */ 00037 time(&now); 00038 min_offset = -1; 00039 for (query = channel->queries; query; query = query->next) 00040 { 00041 if (query->timeout == 0) 00042 continue; 00043 offset = query->timeout - now; 00044 if (offset < 0) 00045 offset = 0; 00046 if (min_offset == -1 || offset < min_offset) 00047 min_offset = offset; 00048 } 00049 00050 /* If we found a minimum timeout and it's sooner than the one 00051 * specified in maxtv (if any), return it. Otherwise go with 00052 * maxtv. 00053 */ 00054 if (min_offset != -1 && (!maxtv || min_offset <= maxtv->tv_sec)) 00055 { 00056 tvbuf->tv_sec = min_offset; 00057 tvbuf->tv_usec = 0; 00058 return tvbuf; 00059 } 00060 else 00061 return maxtv; 00062 }