如何在delphi中用indy查找dns记录 [英] how to lookup dns records with indy in delphi
问题描述
如何在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地址.我想要SRV
和TEXT
记录,也许还有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
支持SRV
和TXT
记录,例如:
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
不不支持SPF记录类型(代码99). ="nofollow"> RFC 4408 在2006年:
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].
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].
已经认识到,当前的做法(使用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类型,但已停止使用.
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.
2003年,当SPF首次开发时,分配新DNS RR类型的要求比现在要严格得多.此外,在DNS服务器和预配系统中尚未广泛部署对轻松部署新DNS RR类型的支持.结果,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.
SPFbis工作组在对[RFC4408]的审查中得出的结论是,其双重RR类型转换模型从根本上是有缺陷的,因为它不包含实施者需要提供服务和检查的通用RR类型.考虑了许多替代方案来解决此问题,但最终工作组得出结论,在可预见的将来不太可能向SPF 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类型的方法绝不应作为未来协议设计者的先例.在使用新的DNS RR类型时,有关设计注意事项的进一步讨论可以在[RFC5507]中找到.
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记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!