使用 Indy 执行 IPv6 反向 DNS 查找
我正在使用 Indy 使用以下代码执行反向 IPv4 查找。
function ReverseDNSLookup(const IPAddress: String; const DNSServer: String; Timeout, Retries: Integer; var HostName: String): Boolean;
var
AIdDNSResolver: TIdDNSResolver;
RetryCount: Integer;
begin
Result := FALSE;
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;
Result := AIdDNSResolver.QueryResult.Count > 0;
if Result then
begin
Result := TRUE;
HostName := ParseReverseDNSResult(AIdDNSResolver.QueryResult[0].RData);
end;
finally
FreeAndNil(AIdDNSResolver);
end;
end;
为了使其与 IPv6 兼容,我需要更改哪些内容?
我知道我可以使用 getaddrinfo,但在这种特殊情况下我需要直接联系 DNS 服务器。
I am using Indy to perform a reverse IPv4 lookup using the following code.
function ReverseDNSLookup(const IPAddress: String; const DNSServer: String; Timeout, Retries: Integer; var HostName: String): Boolean;
var
AIdDNSResolver: TIdDNSResolver;
RetryCount: Integer;
begin
Result := FALSE;
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;
Result := AIdDNSResolver.QueryResult.Count > 0;
if Result then
begin
Result := TRUE;
HostName := ParseReverseDNSResult(AIdDNSResolver.QueryResult[0].RData);
end;
finally
FreeAndNil(AIdDNSResolver);
end;
end;
What do I need to change in order to make it compatible with IPv6?
I know I can use getaddrinfo, but in this particular case I need to contact the DNS server directly.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你不需要做任何事。对于
qtPTR
查询,Resolve()
会自动检测地址是 IPv4 还是 IPv6,并相应地设置查询格式。You don't have to do anything. For
qtPTR
queries,Resolve()
automatically detects whether the address is IPv4 or IPv6 and formats the query accordingly.