Ejemplo n.º 1
0
        public static QueryResult DNSQuery(IPAddress dnsServer, string domain)
        {
            if (dnsServer == null)
                dnsServer = IPAddress.Parse("8.8.8.8");

            QueryResult queryResult = new QueryResult()
            {
                Domain = domain,
                DNS = dnsServer.ToString(),
                NSCount = -1
            };

            Response response = null;
            try
            {
                // create a DNS request
                Request request = new Request();
                request.AddQuestion(new Question(domain, DnsType.NS, DnsClass.IN));

                response = Resolver.Lookup(request, dnsServer);
            }
            catch (Exception ex)
            {
                queryResult.Error = ex.Message;
            }

            if (response == null)
            {
                queryResult.Info = "No answer";
                return queryResult;
            }

            queryResult.Info = response.AuthoritativeAnswer ? "authoritative answer" : "Non-authoritative answer";

            // queryResult.NSCount = response.Answers.Length + response.AdditionalRecords.Length + response.NameServers.Length;

            foreach (Answer answer in response.Answers)
            {
                if (answer.Record != null)
                    queryResult.NSList.Add(answer.Record.ToString());
            }

            foreach (AdditionalRecord additionalRecord in response.AdditionalRecords)
            {
                if (additionalRecord.Record != null)
                    queryResult.NSList.Add(additionalRecord.Record.ToString());
            }

            foreach (NameServer nameServer in response.NameServers)
            {
                if (nameServer.Record != null)
                    queryResult.NSList.Add(nameServer.Record.ToString());
            }

            queryResult.NSCount = queryResult.NSList.Count;

            return queryResult;
        }