private DNSResponse SearchInCache(Question question)
{
if (!m_UseCache)
{
return null;
}
string strKey = question.QClass + "-" + question.QType + "-" + question.QName;
//logger.Debug("Checking cache for " + strKey + ".");
lock (m_lookupFailures)
{
if (m_lookupFailures.ContainsKey(strKey))
{
//logger.Debug("DNS cache failed result found for " + strKey + ".");
if (DateTime.Now.Subtract(m_lookupFailures[strKey].TimeStamp).TotalSeconds < FAILURE_RETRY)
{
return m_lookupFailures[strKey];
}
else
{
m_lookupFailures.Remove(strKey);
return null;
}
}
}
DNSResponse response = null;
lock (m_ResponseCache)
{
if (!m_ResponseCache.ContainsKey(strKey))
{
//logger.Debug("DNS cache no result found for " + strKey + ".");
return null;
}
response = m_ResponseCache[strKey];
if (response.Answers.Count == 0)
{
// A response should not have been cached with no answer records.
m_ResponseCache.Remove(strKey);
return null;
}
}
//int TimeLived = (int)((DateTime.Now.Ticks - response.TimeStamp.Ticks) / TimeSpan.TicksPerSecond);
int secondsLived = (int)(DateTime.Now.Subtract(response.TimeStamp).TotalSeconds % Int32.MaxValue);
//logger.Debug("Seconds lived=" + secondsLived + ".");
foreach (RR rr in response.RecordsRR)
{
//rr.TimeLived = TimeLived;
// The TTL property calculates its actual time to live
if (secondsLived >= rr.TTL)
{
//logger.Debug("DNS cache out of date result found for " + strKey + ".");
return null; // out of date
}
}
//logger.Debug("DNS cache curent result found for " + strKey + ".");
return response;
}