/// <summary>
 /// Initializes a new instance of the <b>DnsClientWithCacheFacts</b> class.
 /// </summary>
 public DnsClientWithCacheFacts()
     : base(DumpIsEnabled)
 {
     m_cache  = new DnsResponseCache(Guid.NewGuid().ToString("D"));
     m_client = new DnsClientWithCache(PublicDns)
     {
         Timeout = TimeSpan.FromSeconds(5), Cache = m_cache
     };
     m_client.MaxRetries = 1;
     m_clientNoCache     = new DnsClient(PublicDns)
     {
         Timeout = TimeSpan.FromSeconds(5)
     };
     m_clientNoCache.MaxRetries = 1;
 }
Esempio n. 2
0
        private IEnumerable <IPAddress> GetNameServers(string domain)
        {
            IEnumerable <IPAddress> nameServers = null;

            foreach (IPEndPoint endpoint in m_primaryDnsServers)
            {
                DnsClient client = null;
                try
                {
                    if (m_doCache)
                    {
                        client = new DnsClientWithCache(endpoint);
                    }
                    else
                    {
                        client = new DnsClient(endpoint);
                    }

                    client.Timeout = Timeout;
                    nameServers    = client.GetNameServers(domain);

                    if (nameServers == null)
                    {
                        continue;
                    }

                    foreach (IPAddress nameServer in nameServers)
                    {
                        if (nameServer != IPAddress.None)
                        {
                            return(nameServers);
                        }
                    }
                }
                finally
                {
                    if (client != null)
                    {
                        client.Dispose();
                    }
                }
            }

            return(null);
        }
Esempio n. 3
0
        DnsResponse ProcessRequest(DnsRequest request)
        {
            IEnumerable <IPAddress> nameServers = GetNameServers(request.Question.Domain);

            if (nameServers == null)
            {
                throw new DnsServerException(DnsStandard.ResponseCode.NameError);
            }

            bool useUdp = request.Question.Type == DnsStandard.RecordType.CERT ? false : true;

            int         count    = 0;
            DnsResponse response = null;

            foreach (IPAddress nameserver in nameServers)
            {
                DnsClient client = null;
                try
                {
                    if (m_doCache)
                    {
                        client = new DnsClientWithCache(new IPEndPoint(nameserver, m_dnsResolutionPort));
                    }
                    else
                    {
                        client = new DnsClient(new IPEndPoint(nameserver, m_dnsResolutionPort));
                    }

                    client.Timeout     = Timeout;
                    client.UseUDPFirst = useUdp;
                    DnsRequest newRequest = new DnsRequest(new DnsQuestion(request.Question));
                    response = client.Resolve(newRequest);

                    if (response != null)
                    {
                        // Clone the response before returning it since the response may be cached
                        // and we don't want the cached response to be modified.
                        response = response.Clone();

                        // updates the TTL of records to reflect the elapsed time since the
                        // record was cached.
                        response.UpdateRecordsTTL();
                        break;
                    }
                }
                catch (DnsException)
                {
                    continue;
                }
                finally
                {
                    if (client != null)
                    {
                        client.Dispose();
                    }
                }

                count++;
                if (count > m_maxNameServersToAttempt)
                {
                    break;
                }
            }

            if (response == null)
            {
                throw new DnsServerException(DnsStandard.ResponseCode.ServerFailure);
            }

            response.RequestID = request.RequestID;

            return(response);
        }