ermya
ermya

Reputation: 76

how to lookup dns records with indy in delphi

How can I lookup DNS records with Indy in Delphi? For example, SRV records, SPF records, TEXT records, etc.

I know we can use nslookup directly from Windows, but I want to do this with Indy, or any other Delphi component.

I tried searching Google, and I found something like this:

function ReverseDNSLookup(IPAddress: String; DNSServer: String =
SDefaultDNS; Timeout: Integer = 30; Retries: Integer = 3) : string;
var
  AIdDNSResolver: TIdDNSResolver;
  RetryCount: Integer;
begin
  Result := '';
  IPAddress := ReverseIP(IPAddress);

  AIdDNSResolver := TIdDNSResolver.Create(nil);
  try
    AIdDNSResolver.QueryResult.Clear;
    AIdDNSResolver.WaitingTime := Timeout;
    AIdDNSResolver.QueryType := [qtPTR];
    AIdDNSResolver.Host := DNSServer;

    RetryCount := Retries;
    repeat
      try
        dec(RetryCount);

        AIdDNSResolver.Resolve(IPAddress);

        Break;
      except
        on e: Exception do
        begin
          if RetryCount <= 0 then
          begin
    //            if SameText(e.Message, RSCodeQueryName) then
    //              Result := FALSE
    //            else
                  raise Exception.Create(e.Message);
            Break;
          end;
        end;
      end;
    until false;

    if AIdDNSResolver.QueryResult.Count > 0 then
      Result := AIdDNSResolver.QueryResult.DomainName;
  finally
    FreeAndNil(AIdDNSResolver);
  end;
end;

But all it is for is looking up IP addresses. I want SRV and TEXT records, and maybe SPF records.

Upvotes: 1

Views: 3904

Answers (1)

Remy Lebeau
Remy Lebeau

Reputation: 598001

TIdDNSResolver is what you are looking for. The example you show is only using a small subset of what TIdDNSResolver supports. You simply need to set the TIdDNSResolver.QueryType property to specify the type(s) of record(s) you want to query, and then loop through the TIdDNSResolver.QueryResult collection to access the individual records. TIdDNSResolver supports SRV and TXT records, for example:

var
  DNS: TIdDNSResolver;
  I: Integer;
  Record: TResultRecord;
  Txt: TTextRecord;
  Srv: TSRVRecord;
begin
  DNS := TIdDNSResolver.Create(nil);
  try
    DNS.WaitingTime := Timeout;
    DNS.QueryType := [qtTXT, qtService];
    DNS.Host := 'some.dns.server';

    DNS.Resolve('some.hostname');

    for I := 0 to DNS.QueryResult.Count -1 do
    begin
      Record := DNS.QueryResult[I];
      case Record.RecType of
      begin
        qtTXT: begin
          Txt := TTextRecord(Record);
          // use Txt.Text as needed...
        end;
        qtService: begin
          Srv := TSRVRecord(Record);
          // use Srv.OriginalName, Srv.Service, Srv.Protocol, etc as needed...
        end;
      else
        // something else...
      end;
    end;
  finally
    DNS.Free;
  end;
end;

TIdDNSResolver does not support the SPF record type (code 99) that was defined in RFC 4408 in 2006:

This document defines a new DNS RR of type SPF, code 99. The format of this type is identical to the TXT RR [RFC1035]. For either type, the character content of the record is encoded as [US-ASCII].

It is recognized that the current practice (using a TXT record) is not optimal, but it is necessary because there are a number of DNS server and resolver implementations in common use that cannot handle the new RR type. The two-record-type scheme provides a forward path to the better solution of using an RR type reserved for this purpose.

That record type was later obsoleted by RFC 7208 in 2014:

SPF records MUST be published as a DNS TXT (type 16) Resource Record (RR) [RFC1035] only. The character content of the record is encoded as [US-ASCII]. Use of alternative DNS RR types was supported in SPF's experimental phase but has been discontinued.

In 2003, when SPF was first being developed, the requirements for assignment of a new DNS RR type were considerably more stringent than they are now. Additionally, support for easy deployment of new DNS RR types was not widely deployed in DNS servers and provisioning systems. As a result, developers of SPF found it easier and more practical to use the TXT RR type for SPF records.

In its review of [RFC4408], the SPFbis working group concluded that its dual RR type transition model was fundamentally flawed since it contained no common RR type that implementers were required to serve and required to check. Many alternatives were considered to resolve this issue, but ultimately the working group concluded that significant migration to the SPF RR type in the foreseeable future was very unlikely and that the best solution for resolving this interoperability issue was to drop support for the SPF RR type from SPF version 1. See Appendix A of [RFC6686] for further information.

The circumstances surrounding SPF's initial deployment a decade ago are unique. If a future update to SPF were developed that did not reuse existing SPF records, it could use the SPF RR type. SPF's use of the TXT RR type for structured data should in no way be taken as precedent for future protocol designers. Further discussion of design considerations when using new DNS RR types can be found in [RFC5507].

Upvotes: 4

Related Questions