本文介绍了如何在delphi中使用indy查找dns记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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

我尝试在 Google 上搜索,发现了这样的内容:

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;

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

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

推荐答案

TIdDNSResolver 正是您要找的.您展示的示例仅使用了 TIdDNSResolver 支持的一小部分.您只需要设置 TIdDNSResolver.QueryType 属性来指定要查询的记录类型,然后遍历 TIdDNSResolver.QueryResult 集合访问个人记录.TIdDNSResolver支持SRVTXT记录,例如:

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支持 :

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

本文档定义了一个新的 SPF 类型 DNS RR,代码 99.这种类型的格式与 TXT RR [RFC1035] 相同.对于任一类型,记录的字符内容都编码为 [US-ASCII].

目前的做法(使用 TXT 记录)不是最佳做法,但这是必要的,因为有许多常用的 DNS 服务器和解析器实现无法处理新的 RR 类型.双记录类型方案为使用为此目的保留的 RR 类型的更好解决方案提供了前向路径.

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 废弃:

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

SPF 记录必须仅作为 DNS TXT(类型 16)资源记录 (RR) [RFC1035] 发布.记录的字符内容编码为 [US-ASCII].SPF 的实验阶段支持使用替代 DNS RR 类型,但已停止使用.

在 2003 年首次开发 SPF 时,分配新 DNS RR 类型的要求比现在严格得多.此外,对新 DNS RR 类型的轻松部署的支持并未广泛部署在 DNS 服务器和供应系统中.因此,SPF 的开发人员发现将 TXT RR 类型用于 SPF 记录更容易、更实用.

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.

在对 [RFC4408] 的审查中,SPFbis 工作组得出结论,其双 RR 类型转换模型存在根本性缺陷,因为它不包含实施者需要提供服务和需要检查的通用 RR 类型.考虑了许多替代方案来解决这个问题,但最终工作组得出的结论是,在可预见的未来向 SPF RR 类型的重大迁移是非常不可能的,解决此互操作性问题的最佳解决方案是从SPF 版本 1.有关详细信息,请参阅 [RFC6686] 的附录 A.

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.

围绕 SPF 十年前初始部署的情况是独一无二的.如果将来开发的 SPF 更新不重用现有的 SPF 记录,则可以使用 SPF RR 类型.SPF 将 TXT RR 类型用于结构化数据,绝不应作为未来协议设计者的先例.可以在 [RFC5507] 中找到有关使用新 DNS RR 类型时的设计注意事项的进一步讨论.

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

这篇关于如何在delphi中使用indy查找dns记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-15 13:27