如何在 delphi 中使用 indy 查找 DNS 记录

how to lookup dns records with indy in delphi

如何在 Delphi 中使用 Indy 查找 DNS 记录?例如SRV条记录,SPF条记录,TEXT条记录等

我知道我们可以直接从 Windows 使用 nslookup,但我想用 Indy 或任何其他 Delphi 组件来做到这一点。

我试着搜索 Google,我发现了这样的东西:

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;

但这只是查找 IP 地址。我想要 SRVTEXT 条记录,也许还有 SPF 条记录。

TIdDNSResolver 就是您要找的。您展示的示例仅使用了 TIdDNSResolver 支持的一小部分。您只需设置 TIdDNSResolver.QueryType 属性 以指定您要查询的记录类型,然后循环访问 TIdDNSResolver.QueryResult 集合以访问单个记录。 TIdDNSResolver支持SRVTXT条记录,例如:

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 支持 2006 年在 RFC 4408 中定义的 SPF 记录类型(代码 99):

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.

该记录类型后来在 2014 年被 RFC 7208 废弃:

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].