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 Type { 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)
QDnsLookup (QDnsLookup::Type type , const QString & name , const QHostAddress & nameserver , quint16 port , QObject * parent = nullptr)
virtual ~QDnsLookup ()
QBindable<QString> bindableName ()
QBindable<QHostAddress> bindableNameserver ()
QBindable<quint16> bindableNameserverPort ()
QBindable<QDnsLookup::Type> bindableType ()
QList<QDnsDomainNameRecord> canonicalNameRecords () const
QDnsLookup::Error error () const
QString errorString () const
QList<QDnsHostAddressRecord> hostAddressRecords () const
bool isFinished () const
QList<QDnsMailExchangeRecord> mailExchangeRecords () const
QString name () const
QList<QDnsDomainNameRecord> nameServerRecords () const
QHostAddress nameserver () const
quint16 nameserverPort () const
QList<QDnsDomainNameRecord> pointerRecords () const
QList<QDnsServiceRecord> serviceRecords () const
void setName (const QString & name )
void setNameserver (const QHostAddress & nameserver )
void setNameserver (const QHostAddress & nameserver , quint16 port )
void setNameserverPort (quint16 port )
void setType (QDnsLookup::Type)
QList<QDnsTextRecord> textRecords () 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 typeChanged (QDnsLookup::Type type )

详细描述

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 代替。

成员类型文档编制

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:: 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::TXT 16 文本记录。

特性文档编制

[read-only] error : const Error

This property holds the type of error that occurred if the DNS lookup failed, or NoError .

访问函数:

QDnsLookup::Error error () const

通知程序信号:

void finished ()

[read-only] errorString : const QString

This property holds a human-readable description of the error if the DNS lookup failed.

访问函数:

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 .

注意: The name will be encoded using IDNA, which means it's unsuitable for querying SRV records compatible with the DNS-SD specification.

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

注意: 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. Notably, the Windows API used by QDnsLookup is unable to handle alternate port numbers.

This property was introduced in Qt 6.6.

[bindable] type : Type

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

This property holds the type of DNS lookup.

成员函数文档编制

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

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

The type property will default to QDnsLookup::A .

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

构造 QDnsLookup 对象为给定 type and name and sets 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. Notably, the Windows API used by QDnsLookup is unable to handle alternate port numbers.

该函数在 Qt 6.6 引入。

[virtual noexcept] QDnsLookup:: ~QDnsLookup ()

销毁 QDnsLookup 对象。

It is safe to delete a QDnsLookup object even if it is not finished, you will simply never receive its results.

[slot] void QDnsLookup:: abort ()

中止 DNS 查找操作。

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

QList < QDnsDomainNameRecord > QDnsLookup:: canonicalNameRecords () const

Returns the list of canonical name records associated with this lookup.

[signal] void QDnsLookup:: finished ()

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

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

QList < QDnsHostAddressRecord > QDnsLookup:: hostAddressRecords () const

Returns the list of host address records associated with this lookup.

bool QDnsLookup:: isFinished () const

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

[slot] void QDnsLookup:: lookup ()

履行 DNS 查找。

The finished () signal is emitted upon completion.

QList < QDnsMailExchangeRecord > QDnsLookup:: mailExchangeRecords () const

Returns the list of mail exchange records associated with this lookup.

The records are sorted according to RFC 5321 , so if you use them to connect to servers, you should try them in the order they are listed.

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

此信号被发射当查找 name 改变。 name is the new lookup name.

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

QList < QDnsDomainNameRecord > QDnsLookup:: nameServerRecords () const

Returns the list of name server records associated with this lookup.

QList < QDnsDomainNameRecord > QDnsLookup:: pointerRecords () const

Returns the list of pointer records associated with this lookup.

QList < QDnsServiceRecord > QDnsLookup:: serviceRecords () const

Returns the list of service records associated with this lookup.

The records are sorted according to RFC 2782 , so if you use them to connect to servers, you should try them in the order they are listed.

[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. Notably, the Windows API used by QDnsLookup is unable to handle alternate port numbers.

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

该函数在 Qt 6.6 引入。

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

QList < QDnsTextRecord > QDnsLookup:: textRecords () const

Returns the list of text records associated with this lookup.

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

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

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