예제 #1
0
 /// <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;
 }
예제 #2
0
        /// <summary>
        /// override base method to use DnsClientWithCache passing in specific name server
        /// </summary>
        /// <param name="domain">The domain name to resolve.</param>
        /// <param name="nameserver">Nameserver to use during resolution</param>
        public override IEnumerable <CertRecord> ResolveCERTFromNameServer(string domain, IPAddress nameserver)
        {
            if (string.IsNullOrEmpty(domain))
            {
                throw new ArgumentException("value was null or empty", "domain");
            }
            if (nameserver == null)
            {
                throw new ArgumentNullException("nameserver");
            }

            using (DnsClientWithCache client = new DnsClientWithCache(nameserver))
            {
                client.Cache       = m_cache;
                client.UseUDPFirst = false;
                return(client.ResolveCERT(domain));
            }
        }
예제 #3
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;
        }
예제 #4
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; 
        }        
예제 #5
0
        /// <summary>
        /// override base method to use DnsClientWithCache passing in specific name server
        /// </summary>
        /// <param name="domain">The domain name to resolve.</param>
        /// <param name="nameserver">Nameserver to use during resolution</param>
        public override IEnumerable<CertRecord> ResolveCERTFromNameServer(string domain, IPAddress nameserver)
        {
            if (string.IsNullOrEmpty(domain))
            {
                throw new ArgumentException("value was null or empty", "domain");
            }
            if (nameserver == null)
            {
                throw new ArgumentNullException("nameserver");
            }

            using (DnsClientWithCache client = new DnsClientWithCache(nameserver))
            {
                client.Cache = m_cache;
                client.UseUDPFirst = false;
                return client.ResolveCERT(domain);
            }
        }