QDnsLookup Class▲
-
Header: QDnsLookup
-
Since: Qt 5.0
-
CMake:
find_package(Qt6 REQUIRED COMPONENTS Network)
target_link_libraries(mytarget PRIVATE Qt6::Network)
-
qmake: QT += network
-
Inherits: QObject
-
Group: QDnsLookup is part of Network Programming API
Detailed Description▲
QDnsLookup uses the mechanisms provided by the operating system to perform DNS lookups. To perform a lookup you need to specify a name and type then invoke the lookup() slot. The finished() signal will be emitted upon completion.
For example, you can determine which servers an XMPP chat client should connect to for a given domain with:
void
MyObject::
lookupServers()
{
// Create a DNS lookup.
dns =
new
QDnsLookup(this
);
connect(dns, &
amp;QDnsLookup::
finished, this
, &
amp;MyObject::
handleServers);
// Find the XMPP servers for gmail.com
dns-&
gt;setType(QDnsLookup::
SRV);
dns-&
gt;setName("_xmpp-client._tcp.gmail.com"
);
dns-&
gt;lookup();
}
Once the request finishes you can handle the results with:
void
MyObject::
handleServers()
{
// Check the lookup succeeded.
if
(dns-&
gt;error() !=
QDnsLookup::
NoError) {
qWarning("DNS lookup failed"
);
dns-&
gt;deleteLater();
return
;
}
// Handle the results.
const
auto
records =
dns-&
gt;serviceRecords();
for
(const
QDnsServiceRecord &
amp;record : records) {
...
}
dns-&
gt;deleteLater();
}
If you simply want to find the IP address(es) associated with a host name, or the host name associated with an IP address you should use QHostInfo instead.
Member Type Documentation▲
enum QDnsLookup::Error▲
Indicates all possible error conditions found during the processing of the DNS lookup.
Constant |
Value |
Description |
---|---|---|
QDnsLookup::NoError |
0 |
no error condition. |
QDnsLookup::ResolverError |
1 |
there was an error initializing the system's DNS resolver. |
QDnsLookup::OperationCancelledError |
2 |
the lookup was aborted using the abort() method. |
QDnsLookup::InvalidRequestError |
3 |
the requested DNS lookup was invalid. |
QDnsLookup::InvalidReplyError |
4 |
the reply returned by the server was invalid. |
QDnsLookup::ServerFailureError |
5 |
the server encountered an internal failure while processing the request (SERVFAIL). |
QDnsLookup::ServerRefusedError |
6 |
the server refused to process the request for security or policy reasons (REFUSED). |
QDnsLookup::NotFoundError |
7 |
the requested domain name does not exist (NXDOMAIN). |
enum QDnsLookup::Type▲
Indicates the type of DNS lookup that was performed.
Constant |
Value |
Description |
---|---|---|
QDnsLookup::A |
1 |
IPv4 address records. |
QDnsLookup::AAAA |
28 |
IPv6 address records. |
QDnsLookup::ANY |
255 |
any records. |
QDnsLookup::CNAME |
5 |
canonical name records. |
QDnsLookup::MX |
15 |
mail exchange records. |
QDnsLookup::NS |
2 |
name server records. |
QDnsLookup::PTR |
12 |
pointer records. |
QDnsLookup::SRV |
33 |
service records. |
QDnsLookup::TXT |
16 |
text records. |
Property Documentation▲
[read-only] error : const Error▲
This property holds the type of error that occurred if the DNS lookup failed, or NoError.
Access functions:
-
error() const
Notifier signal:
-
void finished()
[read-only] errorString : const QString▲
This property holds a human-readable description of the error if the DNS lookup failed.
Access functions:
-
errorString() const
Notifier signal:
-
void finished()
[bindable] name : QString▲
This property supports QProperty bindings.
This property holds the name to lookup.
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▲
This property supports QProperty bindings.
This property holds the nameserver to use for DNS lookup.
[bindable] type : Type▲
This property supports QProperty bindings.
This property holds the type of DNS lookup.
Member Function Documentation▲
[explicit] QDnsLookup::QDnsLookup(QObject *parent = nullptr)▲
Constructs a QDnsLookup object and sets parent as the parent object.
The type property will default to QDnsLookup::A.
QDnsLookup::QDnsLookup(QDnsLookup::Type type, const QString &name, QObject *parent = nullptr)▲
Constructs a QDnsLookup object for the given type and name and sets parent as the parent object.
[since 5.4] QDnsLookup::QDnsLookup(QDnsLookup::Type type, const QString &name, const QHostAddress &nameserver, QObject *parent = nullptr)▲
Constructs a QDnsLookup object for the given type, name and nameserver and sets parent as the parent object.
This function was introduced in Qt 5.4.
[virtual] QDnsLookup::~QDnsLookup()▲
Destroys the QDnsLookup object.
It is safe to delete a QDnsLookup object even if it is not finished, you will simply never receive its results.
void QDnsLookup::abort()▲
Aborts the DNS lookup operation.
If the lookup is already finished, does nothing.
QList<QDnsDomainNameRecord> QDnsLookup::canonicalNameRecords() const▲
Returns the list of canonical name records associated with this lookup.
void QDnsLookup::finished()▲
This signal is emitted when the reply has finished processing.
Notifier signal for property error.
Notifier signal for property errorString.
QList<QDnsHostAddressRecord> QDnsLookup::hostAddressRecords() const▲
Returns the list of host address records associated with this lookup.
bool QDnsLookup::isFinished() const▲
Returns whether the reply has finished or was aborted.
void QDnsLookup::lookup()▲
Performs the DNS lookup.
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.
void QDnsLookup::nameChanged(const QString &name)▲
This signal is emitted when the lookup name changes. name is the new lookup name.
Notifier signal for property 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.
QList<QDnsTextRecord> QDnsLookup::textRecords() const▲
Returns the list of text records associated with this lookup.