QDnsLookup 类

QDnsLookup 类表示 DNS (域名系统) 查找。 更多...

头: #include <QDnsLookup>
CMake: find_package(Qt6 REQUIRED COMPONENTS Network)
target_link_libraries(mytarget PRIVATE Qt6::Network)
qmake: QT += network
继承: QObject

公共类型

enum Error { NoError, ResolverError, OperationCancelledError, InvalidRequestError, InvalidReplyError, …, TimeoutError }
enum Protocol { Standard, DnsOverTls }
enum 类型 { A, AAAA, ANY, CNAME, MX, …, TXT }

特性

公共函数

QDnsLookup (QObject * parent = nullptr)
QDnsLookup (QDnsLookup::Type type , const QString & name , QObject * parent = nullptr)
QDnsLookup (QDnsLookup::Type type , const QString & name , const QHostAddress & nameserver , QObject * parent = nullptr)
(从 6.6 起) QDnsLookup (QDnsLookup::Type type , const QString & name , const QHostAddress & nameserver , quint16 port , QObject * parent = nullptr)
(从 6.8 起) QDnsLookup (QDnsLookup::Type type , const QString & name , QDnsLookup::Protocol protocol , const QHostAddress & nameserver , quint16 port = 0, QObject * parent = nullptr)
virtual ~QDnsLookup ()
QBindable<QString> bindableName ()
QBindable<QHostAddress> bindableNameserver ()
QBindable<quint16> bindableNameserverPort ()
QBindable<QDnsLookup::Protocol> bindableNameserverProtocol ()
QBindable<QDnsLookup::Type> bindableType ()
QList<QDnsDomainNameRecord> canonicalNameRecords () const
QDnsLookup::Error error () const
QString errorString () const
QList<QDnsHostAddressRecord> hostAddressRecords () const
bool isAuthenticData () const
bool isFinished () const
QList<QDnsMailExchangeRecord> mailExchangeRecords () const
QString name () const
QList<QDnsDomainNameRecord> nameServerRecords () const
QHostAddress nameserver () const
quint16 nameserverPort () const
QDnsLookup::Protocol nameserverProtocol () const
QList<QDnsDomainNameRecord> pointerRecords () const
QList<QDnsServiceRecord> serviceRecords () const
void setName (const QString & name )
(从 6.6 起) void setNameserver (const QHostAddress & nameserver , quint16 port )
void setNameserver (const QHostAddress & nameserver )
void setNameserver (QDnsLookup::Protocol protocol , const QHostAddress & nameserver , quint16 port = 0)
void setNameserverPort (quint16 port )
void setNameserverProtocol (QDnsLookup::Protocol protocol )
(从 6.8 起) void setSslConfiguration (const QSslConfiguration & sslConfiguration )
void setType (QDnsLookup::Type)
QSslConfiguration sslConfiguration () const
QList<QDnsTextRecord> textRecords () const
(从 6.8 起) QList<QDnsTlsAssociationRecord> tlsAssociationRecords () const
QDnsLookup::Type type () const

公共槽

void abort ()
void lookup ()

信号

void finished ()
void nameChanged (const QString & name )
void nameserverChanged (const QHostAddress & nameserver )
void nameserverPortChanged (quint16 port )
void nameserverProtocolChanged (QDnsLookup::Protocol protocol )
void typeChanged (QDnsLookup::Type type )

静态公共成员

(从 6.8 起) quint16 defaultPortForProtocol (QDnsLookup::Protocol protocol )
(从 6.8 起) bool isProtocolSupported (QDnsLookup::Protocol protocol )

详细描述

QDnsLookup 使用由操作系统提供的机制来履行 DNS 查找。要履行查找需要指定 name and type 然后援引 lookup () slot. The finished () signal will be emitted upon completion.

例如,可以确定给定域的 XMPP 聊天客户端应该连接到哪些服务器采用:

void MyObject::lookupServers()
{
    // Create a DNS lookup.
    dns = new QDnsLookup(this);
    connect(dns, &QDnsLookup::finished, this, &MyObject::handleServers);
    // Find the XMPP servers for gmail.com
    dns->setType(QDnsLookup::SRV);
    dns->setName("_xmpp-client._tcp.gmail.com");
    dns->lookup();
}
					

一旦请求完成,可以处理结果采用:

void MyObject::handleServers()
{
    // Check the lookup succeeded.
    if (dns->error() != QDnsLookup::NoError) {
        qWarning("DNS lookup failed");
        dns->deleteLater();
        return;
    }
    // Handle the results.
    const auto records = dns->serviceRecords();
    for (const QDnsServiceRecord &record : records) {
        ...
    }
    dns->deleteLater();
}
					

注意: 若仅仅希望查找与主机名关联的 IP 地址 (或与 IP 地址关联的主机名),应该使用 QHostInfo 代替。

DNS-over-TLS and Authentic Data

QDnsLookup supports DNS-over-TLS (DoT, as specified by RFC 7858 ) on some platforms. That currently includes all Unix platforms where regular queries are supported, if QSslSocket support is present in Qt. To query if support is present at runtime, use isProtocolSupported ().

When using DNS-over-TLS, QDnsLookup only implements the "Opportunistic Privacy Profile" method of authentication, as described in RFC 7858 section 4.1. In this mode, QDnsLookup (through QSslSocket ) only validates that the server presents a certificate that is valid for the server being connected to. Clients may use setSslConfiguration () to impose additional restrictions and sslConfiguration () to obtain information after the query is complete.

QDnsLookup will request DNS servers queried over TLS to perform authentication on the data they return. If they confirm the data is valid, the authenticData property will be set to true. QDnsLookup does not verify the integrity of the data by itself, so applications should only trust this property on servers they have confirmed through other means to be trustworthy.

Authentic Data without TLS

QDnsLookup request Authentic Data for any server set with setNameserver (), even if TLS encryption is not required. This is useful when querying a caching nameserver on the same host as the application or on a trusted network. Though similar to the TLS case, the application is responsible for determining if the server it chose to use is trustworthy, and if the unencrypted connection cannot be tampered with.

QDnsLookup obeys the system configuration to request Authentic Data on the default nameserver (that is, if setNameserver () is not called). This is currently only supported on Linux systems using glibc 2.31 or later. On any other systems, QDnsLookup will ignore the AD bit in the query header.

成员类型文档编制

enum QDnsLookup:: Error

指示在 DNS 查找处理过程中发现的所有可能的错误条件。

常量 描述
QDnsLookup::NoError 0 没有错误条件。
QDnsLookup::ResolverError 1 初始化系统的 DNS 解析器时出错。
QDnsLookup::OperationCancelledError 2 查找被中止使用 abort () 方法。
QDnsLookup::InvalidRequestError 3 请求的 DNS 查找无效。
QDnsLookup::InvalidReplyError 4 由服务器返回的回复无效。
QDnsLookup::ServerFailureError 5 服务器遭遇内部故障当处理请求时 (SERVFAIL)。
QDnsLookup::ServerRefusedError 6 服务器出于安全或策略原因拒绝处理请求 (REFUSED)。
QDnsLookup::NotFoundError 7 请求的域名不存在 (NXDOMAIN)。
QDnsLookup::TimeoutError 8 the server was not reached or did not reply in time (since 6.6).

enum QDnsLookup:: Protocol

Indicates the type of DNS server that is being queried.

常量 描述
QDnsLookup::Standard 0 Regular, unencrypted DNS, using UDP and falling back to TCP as necessary (default port: 53)
QDnsLookup::DnsOverTls 1 Encrypted DNS over TLS (DoT, as specified by RFC 7858 ), over TCP (default port: 853)

另请参阅 isProtocolSupported (), nameserverProtocol ,和 setNameserver ().

enum QDnsLookup:: Type

指示所履行的 DNS 查找类型。

常量 描述
QDnsLookup::A 1 IPv4 地址记录。
QDnsLookup::AAAA 28 IPv6 地址记录。
QDnsLookup::ANY 255 任何记录。
QDnsLookup::CNAME 5 典型名称记录。
QDnsLookup::MX 15 邮件交换记录。
QDnsLookup::NS 2 名称服务器记录。
QDnsLookup::PTR 12 指针记录。
QDnsLookup::SRV 33 服务记录。
QDnsLookup::TLSA (since Qt 6.8) 52 TLS association records.
QDnsLookup::TXT 16 文本记录。

特性文档编制

[read-only, since 6.8] authenticData : const bool

This property holds whether the reply was authenticated by the resolver.

QDnsLookup does not perform the authentication itself. Instead, it trusts the name server that was queried to perform the authentication and report it. The application is responsible for determining if any servers it configured with setNameserver () are trustworthy; if no server was set, QDnsLookup obeys system configuration on whether responses should be trusted.

This property may be set even if error () indicates a resolver error occurred.

该特性在 Qt 6.8 引入。

访问函数:

bool isAuthenticData () const

通知程序信号:

void finished ()

另请参阅 setNameserver () 和 nameserverProtocol ().

[read-only] error : const Error

此特性保持出现错误的类型若 DNS 查找失败,或 NoError .

访问函数:

QDnsLookup::Error error () const

通知程序信号:

void finished ()

[read-only] errorString : const QString

此特性保持错误的人类可读描述,若 DNS 查找失败。

访问函数:

QString errorString () const

通知程序信号:

void finished ()

[bindable] name : QString

注意: 此特性支持 QProperty 绑定。

此特性保持要查找的名称。

If the name to look up is empty, QDnsLookup will attempt to resolve the root domain of DNS. That query is usually performed with QDnsLookup::type 设为 NS .

注意: 名称将使用 IDNA 编码,这意味着它不适合查询兼容 DNS-SD 规范的 SRV 记录。

[bindable] nameserver : QHostAddress

注意: 此特性支持 QProperty 绑定。

此特性保持用于 DNS 查找的名称服务器。

[bindable, since 6.6] nameserverPort : quint16

注意: 此特性支持 QProperty 绑定。

This property holds the port number of nameserver to use for DNS lookup.

The value of 0 indicates that QDnsLookup should use the default port for the nameserverProtocol ().

注意: Setting the port number to any value other than the default (53) can cause the name resolution to fail, depending on the operating system limitations and firewalls, if the nameserverProtocol () to be used QDnsLookup::Standard . Notably, the Windows API used by QDnsLookup is unable to handle alternate port numbers.

该特性在 Qt 6.6 引入。

[bindable, since 6.8] nameserverProtocol : Protocol

注意: 此特性支持 QProperty 绑定。

This property holds the protocol to use when sending the DNS query

该特性在 Qt 6.8 引入。

另请参阅 isProtocolSupported ().

[bindable] type : 类型

注意: 此特性支持 QProperty 绑定。

此特性保持 DNS 查找的类型。

成员函数文档编制

[explicit] QDnsLookup:: QDnsLookup ( QObject * parent = nullptr)

构造 QDnsLookup 对象并设置 parent 作为父级对象。

The type 特性默认为 QDnsLookup::A .

QDnsLookup:: QDnsLookup ( QDnsLookup::Type type , const QString & name , QObject * parent = nullptr)

构造 QDnsLookup 对象为给定 type and name 并设置 parent 作为父级对象。

QDnsLookup:: QDnsLookup ( QDnsLookup::Type type , const QString & name , const QHostAddress & nameserver , QObject * parent = nullptr)

Constructs a QDnsLookup object to issue a query for name of record type type , using the DNS server nameserver running on the default DNS port, and sets parent 作为父级对象。

[since 6.6] QDnsLookup:: QDnsLookup ( QDnsLookup::Type type , const QString & name , const QHostAddress & nameserver , quint16 port , QObject * parent = nullptr)

Constructs a QDnsLookup object to issue a query for name of record type type , using the DNS server nameserver running on port port , and sets parent 作为父级对象。

注意: Setting the port number to any value other than the default (53) can cause the name resolution to fail, depending on the operating system limitations and firewalls, if the nameserverProtocol () to be used QDnsLookup::Standard . Notably, the Windows API used by QDnsLookup is unable to handle alternate port numbers.

该函数在 Qt 6.6 引入。

[since 6.8] QDnsLookup:: QDnsLookup ( QDnsLookup::Type type , const QString & name , QDnsLookup::Protocol protocol , const QHostAddress & nameserver , quint16 port = 0, QObject * parent = nullptr)

Constructs a QDnsLookup object to issue a query for name of record type type , using the DNS server nameserver running on port port , and sets parent 作为父级对象。

The query will be sent using protocol , if supported. Use isProtocolSupported () to check if it is supported.

注意: Setting the port number to any value other than the default (53) can cause the name resolution to fail, depending on the operating system limitations and firewalls, if the nameserverProtocol () to be used QDnsLookup::Standard . Notably, the Windows API used by QDnsLookup is unable to handle alternate port numbers.

该函数在 Qt 6.8 引入。

[virtual noexcept] QDnsLookup:: ~QDnsLookup ()

销毁 QDnsLookup 对象。

它是安全的删除 QDnsLookup 对象即使它尚未完成,简而言之,也从不会收到其结果。

[slot] void QDnsLookup:: abort ()

中止 DNS 查找操作。

若查找已完成,什么都不做。

QList < QDnsDomainNameRecord > QDnsLookup:: canonicalNameRecords () const

返回关联此查找的典型名称记录列表。

[static noexcept, since 6.8] quint16 QDnsLookup:: defaultPortForProtocol ( QDnsLookup::Protocol protocol )

Returns the standard (default) port number for the protocol protocol .

该函数在 Qt 6.8 引入。

另请参阅 isProtocolSupported ().

[signal] void QDnsLookup:: finished ()

此信号被发射,当回复已处理完成。

注意: 通知程序信号对于特性 authenticData , error ,和 errorString .

QList < QDnsHostAddressRecord > QDnsLookup:: hostAddressRecords () const

返回关联此查找的主机地址记录列表。

bool QDnsLookup:: isFinished () const

返回回复是已完成还是被中止。

[static, since 6.8] bool QDnsLookup:: isProtocolSupported ( QDnsLookup::Protocol protocol )

Returns true if DNS queries using protocol are supported with QDnsLookup .

该函数在 Qt 6.8 引入。

另请参阅 nameserverProtocol .

[slot] void QDnsLookup:: lookup ()

履行 DNS 查找。

The finished () signal is emitted upon completion.

QList < QDnsMailExchangeRecord > QDnsLookup:: mailExchangeRecords () const

返回关联此查找的邮件交换记录列表。

记录的排序是根据 RFC 5321 ,因此,若使用它们连接到服务器,应按照列表它们的次序尝试。

[signal] void QDnsLookup:: nameChanged (const QString & name )

此信号发射,当查找 name 改变。 name 是新的查找名称。

注意: 通知程序信号对于特性 name .

QList < QDnsDomainNameRecord > QDnsLookup:: nameServerRecords () const

返回关联此查找的名称服务器记录列表。

QList < QDnsDomainNameRecord > QDnsLookup:: pointerRecords () const

返回关联此查找的指针记录列表。

QList < QDnsServiceRecord > QDnsLookup:: serviceRecords () const

返回关联此查找的服务记录列表。

记录的排序是根据 RFC 2782 ,因此,若使用它们连接到服务器,应按照列表它们的次序尝试。

[since 6.6] void QDnsLookup:: setNameserver (const QHostAddress & nameserver , quint16 port )

Sets the nameserver to nameserver and the port to port .

注意: Setting the port number to any value other than the default (53) can cause the name resolution to fail, depending on the operating system limitations and firewalls, if the nameserverProtocol () to be used QDnsLookup::Standard . Notably, the Windows API used by QDnsLookup is unable to handle alternate port numbers.

注意: setter 函数对于特性 nameserver .

该函数在 Qt 6.6 引入。

另请参阅 QDnsLookup::nameserver and QDnsLookup::nameserverPort .

[since 6.8] void QDnsLookup:: setSslConfiguration (const QSslConfiguration & sslConfiguration )

设置 sslConfiguration to use for outgoing DNS-over-TLS connections.

该函数在 Qt 6.8 引入。

另请参阅 sslConfiguration () 和 QSslSocket::setSslConfiguration ().

QSslConfiguration QDnsLookup:: sslConfiguration () const

Returns the current SSL configuration.

另请参阅 setSslConfiguration ().

QList < QDnsTextRecord > QDnsLookup:: textRecords () const

返回关联此查找的文本记录列表。

[since 6.8] QList < QDnsTlsAssociationRecord > QDnsLookup:: tlsAssociationRecords () const

Returns the list of TLS association records associated with this lookup.

According to the standards relating to DNS-based Authentication of Named Entities (DANE), this field should be ignored and must not be used for verifying the authentity of a given server if the authenticity of the DNS reply cannot itself be confirmed. See isAuthenticData () 了解更多信息。

该函数在 Qt 6.8 引入。

[signal] void QDnsLookup:: typeChanged ( QDnsLookup::Type type )

此信号发射,当查找 type 改变。 type 是新的查找类型。

注意: 通知程序信号对于特性 type .

内容

  1. 公共类型

  2. 特性

  3. 公共函数

  4. 公共槽

  5. 信号

  6. 静态公共成员

  7. 详细描述

  8. DNS-over-TLS and Authentic Data