Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
QDnsLookup Class Reference

The QDnsLookup class represents a DNS lookup. More...

#include <qdnslookup.h>

+ Inheritance diagram for QDnsLookup:
+ Collaboration diagram for QDnsLookup:

Public Types

enum  Error {
  NoError = 0 , ResolverError , OperationCancelledError , InvalidRequestError ,
  InvalidReplyError , ServerFailureError , ServerRefusedError , NotFoundError ,
  TimeoutError
}
 Indicates all possible error conditions found during the processing of the DNS lookup. More...
 
enum  Type {
  A = 1 , AAAA = 28 , ANY = 255 , CNAME = 5 ,
  MX = 15 , NS = 2 , PTR = 12 , SRV = 33 ,
  TXT = 16
}
 Indicates the type of DNS lookup that was performed. More...
 

Public Slots

void abort ()
 Aborts the DNS lookup operation.
 
void lookup ()
 Performs the DNS lookup.
 
- Public Slots inherited from QObject
void deleteLater ()
 \threadsafe
 

Signals

void finished ()
 This signal is emitted when the reply has finished processing.
 
void nameChanged (const QString &name)
 This signal is emitted when the lookup \l name changes.
 
void typeChanged (Type type)
 This signal is emitted when the lookup \l type changes.
 
void nameserverChanged (const QHostAddress &nameserver)
 
void nameserverPortChanged (quint16 port)
 
- Signals inherited from QObject
void destroyed (QObject *=nullptr)
 This signal is emitted immediately before the object obj is destroyed, after any instances of QPointer have been notified, and cannot be blocked.
 
void objectNameChanged (const QString &objectName, QPrivateSignal)
 This signal is emitted after the object's name has been changed.
 

Public Member Functions

 QDnsLookup (QObject *parent=nullptr)
 Constructs a QDnsLookup object and sets parent as the parent object.
 
 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.
 
 QDnsLookup (Type type, const QString &name, const QHostAddress &nameserver, QObject *parent=nullptr)
 
 QDnsLookup (Type type, const QString &name, const QHostAddress &nameserver, quint16 port, QObject *parent=nullptr)
 
 ~QDnsLookup ()
 Destroys the QDnsLookup object.
 
Error error () const
 
QString errorString () const
 
bool isFinished () const
 Returns whether the reply has finished or was aborted.
 
QString name () const
 
void setName (const QString &name)
 
QBindable< QStringbindableName ()
 
Type type () const
 
void setType (QDnsLookup::Type)
 
QBindable< TypebindableType ()
 
QHostAddress nameserver () const
 
void setNameserver (const QHostAddress &nameserver)
 
QBindable< QHostAddressbindableNameserver ()
 
quint16 nameserverPort () const
 
void setNameserverPort (quint16 port)
 
QBindable< quint16bindableNameserverPort ()
 
void setNameserver (const QHostAddress &nameserver, quint16 port)
 
QList< QDnsDomainNameRecordcanonicalNameRecords () const
 Returns the list of canonical name records associated with this lookup.
 
QList< QDnsHostAddressRecordhostAddressRecords () const
 Returns the list of host address records associated with this lookup.
 
QList< QDnsMailExchangeRecordmailExchangeRecords () const
 Returns the list of mail exchange records associated with this lookup.
 
QList< QDnsDomainNameRecordnameServerRecords () const
 Returns the list of name server records associated with this lookup.
 
QList< QDnsDomainNameRecordpointerRecords () const
 Returns the list of pointer records associated with this lookup.
 
QList< QDnsServiceRecordserviceRecords () const
 Returns the list of service records associated with this lookup.
 
QList< QDnsTextRecordtextRecords () const
 Returns the list of text records associated with this lookup.
 
- Public Member Functions inherited from QObject
Q_INVOKABLE QObject (QObject *parent=nullptr)
 Constructs an object with parent object parent.
 
virtual ~QObject ()
 Destroys the object, deleting all its child objects.
 
virtual bool event (QEvent *event)
 This virtual function receives events to an object and should return true if the event e was recognized and processed.
 
virtual bool eventFilter (QObject *watched, QEvent *event)
 Filters events if this object has been installed as an event filter for the watched object.
 
QString objectName () const
 
Q_WEAK_OVERLOAD void setObjectName (const QString &name)
 Sets the object's name to name.
 
void setObjectName (QAnyStringView name)
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
 
QBindable< QStringbindableObjectName ()
 
bool isWidgetType () const
 Returns true if the object is a widget; otherwise returns false.
 
bool isWindowType () const
 Returns true if the object is a window; otherwise returns false.
 
bool isQuickItemType () const
 Returns true if the object is a QQuickItem; otherwise returns false.
 
bool signalsBlocked () const noexcept
 Returns true if signals are blocked; otherwise returns false.
 
bool blockSignals (bool b) noexcept
 If block is true, signals emitted by this object are blocked (i.e., emitting a signal will not invoke anything connected to it).
 
QThreadthread () const
 Returns the thread in which the object lives.
 
bool moveToThread (QThread *thread QT6_DECL_NEW_OVERLOAD_TAIL)
 Changes the thread affinity for this object and its children and returns true on success.
 
int startTimer (int interval, Qt::TimerType timerType=Qt::CoarseTimer)
 This is an overloaded function that will start a timer of type timerType and a timeout of interval milliseconds.
 
int startTimer (std::chrono::nanoseconds time, Qt::TimerType timerType=Qt::CoarseTimer)
 
void killTimer (int id)
 Kills the timer with timer identifier, id.
 
void killTimer (Qt::TimerId id)
 
template<typename T >
findChild (QAnyStringView aName, Qt::FindChildOptions options=Qt::FindChildrenRecursively) const
 Returns the child of this object that can be cast into type T and that is called name, or \nullptr if there is no such object.
 
template<typename T >
QList< T > findChildren (QAnyStringView aName, Qt::FindChildOptions options=Qt::FindChildrenRecursively) const
 Returns all children of this object with the given name that can be cast to type T, or an empty list if there are no such objects.
 
template<typename T >
findChild (Qt::FindChildOptions options=Qt::FindChildrenRecursively) const
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
 
template<typename T >
QList< T > findChildren (Qt::FindChildOptions options=Qt::FindChildrenRecursively) const
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
 
const QObjectListchildren () const
 Returns a list of child objects.
 
void setParent (QObject *parent)
 Makes the object a child of parent.
 
void installEventFilter (QObject *filterObj)
 Installs an event filter filterObj on this object.
 
void removeEventFilter (QObject *obj)
 Removes an event filter object obj from this object.
 
QMetaObject::Connection connect (const QObject *sender, const char *signal, const char *member, Qt::ConnectionType type=Qt::AutoConnection) const
 
bool disconnect (const char *signal=nullptr, const QObject *receiver=nullptr, const char *member=nullptr) const
 
bool disconnect (const QObject *receiver, const char *member=nullptr) const
 
void dumpObjectTree () const
 Dumps a tree of children to the debug output.
 
void dumpObjectInfo () const
 Dumps information about signal connections, etc.
 
bool setProperty (const char *name, const QVariant &value)
 Sets the value of the object's name property to value.
 
bool setProperty (const char *name, QVariant &&value)
 
QVariant property (const char *name) const
 Returns the value of the object's name property.
 
QList< QByteArraydynamicPropertyNames () const
 
QBindingStoragebindingStorage ()
 
const QBindingStoragebindingStorage () const
 
QObjectparent () const
 Returns a pointer to the parent object.
 
bool inherits (const char *classname) const
 Returns true if this object is an instance of a class that inherits className or a QObject subclass that inherits className; otherwise returns false.
 

Properties

Error error
 the type of error that occurred if the DNS lookup failed, or NoError.
 
QString errorString
 a human-readable description of the error if the DNS lookup failed.
 
QString name
 the name to lookup.
 
Type type
 the type of DNS lookup.
 
QHostAddress nameserver
 the nameserver to use for DNS lookup.
 
quint16 nameserverPort
 the port number of nameserver to use for DNS lookup.
 
- Properties inherited from QObject
QString objectName
 the name of this object
 

Additional Inherited Members

- Static Public Member Functions inherited from QObject
static QMetaObject::Connection connect (const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
 \threadsafe
 
static QMetaObject::Connection connect (const QObject *sender, const QMetaMethod &signal, const QObject *receiver, const QMetaMethod &method, Qt::ConnectionType type=Qt::AutoConnection)
 
template<typename Func1 , typename Func2 >
static QMetaObject::Connection connect (const typename QtPrivate::FunctionPointer< Func1 >::Object *sender, Func1 signal, const typename QtPrivate::ContextTypeForFunctor< Func2 >::ContextType *context, Func2 &&slot, Qt::ConnectionType type=Qt::AutoConnection)
 
template<typename Func1 , typename Func2 >
static QMetaObject::Connection connect (const typename QtPrivate::FunctionPointer< Func1 >::Object *sender, Func1 signal, Func2 &&slot)
 
static bool disconnect (const QObject *sender, const char *signal, const QObject *receiver, const char *member)
 \threadsafe
 
static bool disconnect (const QObject *sender, const QMetaMethod &signal, const QObject *receiver, const QMetaMethod &member)
 
static bool disconnect (const QMetaObject::Connection &)
 Disconnect a connection.
 
template<typename Func1 , typename Func2 >
static bool disconnect (const typename QtPrivate::FunctionPointer< Func1 >::Object *sender, Func1 signal, const typename QtPrivate::FunctionPointer< Func2 >::Object *receiver, Func2 slot)
 
template<typename Func1 >
static bool disconnect (const typename QtPrivate::FunctionPointer< Func1 >::Object *sender, Func1 signal, const QObject *receiver, void **zero)
 
- Protected Member Functions inherited from QObject
QObjectsender () const
 Returns a pointer to the object that sent the signal, if called in a slot activated by a signal; otherwise it returns \nullptr.
 
int senderSignalIndex () const
 
int receivers (const char *signal) const
 Returns the number of receivers connected to the signal.
 
bool isSignalConnected (const QMetaMethod &signal) const
 
virtual void timerEvent (QTimerEvent *event)
 This event handler can be reimplemented in a subclass to receive timer events for the object.
 
virtual void childEvent (QChildEvent *event)
 This event handler can be reimplemented in a subclass to receive child events.
 
virtual void customEvent (QEvent *event)
 This event handler can be reimplemented in a subclass to receive custom events.
 
virtual void connectNotify (const QMetaMethod &signal)
 
virtual void disconnectNotify (const QMetaMethod &signal)
 
 QObject (QObjectPrivate &dd, QObject *parent=nullptr)
 
- Protected Attributes inherited from QObject
QScopedPointer< QObjectDatad_ptr
 

Detailed Description

The QDnsLookup class represents a DNS lookup.

Since
5.0

\inmodule QtNetwork

QDnsLookup uses the mechanisms provided by the operating system to perform DNS lookups. To perform a lookup you need to specify a \l name and \l type then invoke the \l{QDnsLookup::lookup()}{lookup()} slot. The \l{QDnsLookup::finished()}{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, &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();
}

Once the request finishes you can handle the results with:

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();
}
The QDnsLookup class represents a DNS lookup.
Definition qdnslookup.h:141
void finished()
This signal is emitted when the reply has finished processing.
The QDnsServiceRecord class stores information about a DNS SRV record.
Definition qdnslookup.h:94
Note
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.

Definition at line 140 of file qdnslookup.h.

Member Enumeration Documentation

◆ Error

Indicates all possible error conditions found during the processing of the DNS lookup.

\value NoError no error condition.

\value ResolverError there was an error initializing the system's DNS resolver.

\value OperationCancelledError the lookup was aborted using the abort() method.

\value InvalidRequestError the requested DNS lookup was invalid.

\value InvalidReplyError the reply returned by the server was invalid.

\value ServerFailureError the server encountered an internal failure while processing the request (SERVFAIL).

\value ServerRefusedError the server refused to process the request for security or policy reasons (REFUSED).

\value NotFoundError the requested domain name does not exist (NXDOMAIN).

\value TimeoutError the server was not reached or did not reply in time (since 6.6).

Enumerator
NoError 
ResolverError 
OperationCancelledError 
InvalidRequestError 
InvalidReplyError 
ServerFailureError 
ServerRefusedError 
NotFoundError 
TimeoutError 

Definition at line 153 of file qdnslookup.h.

◆ Type

Indicates the type of DNS lookup that was performed.

\value A IPv4 address records.

\value AAAA IPv6 address records.

\value ANY any records.

\value CNAME canonical name records.

\value MX mail exchange records.

\value NS name server records.

\value PTR pointer records.

\value SRV service records.

\value TXT text records.

Enumerator
AAAA 
ANY 
CNAME 
MX 
NS 
PTR 
SRV 
TXT 

Definition at line 167 of file qdnslookup.h.

Constructor & Destructor Documentation

◆ QDnsLookup() [1/4]

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

Constructs a QDnsLookup object and sets parent as the parent object.

The \l type property will default to QDnsLookup::A.

Definition at line 245 of file qdnslookup.cpp.

◆ QDnsLookup() [2/4]

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.

Definition at line 255 of file qdnslookup.cpp.

References d, name, and type.

◆ QDnsLookup() [3/4]

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

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 as the parent object.

Definition at line 272 of file qdnslookup.cpp.

◆ QDnsLookup() [4/4]

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

Constructs a QDnsLookup object to issue a query for \a name of record type
\a type, using the DNS server \a nameserver running on port \a port, and
sets \a parent as the parent object.

! [nameserver-port]

Note
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. ! [nameserver-port]

Definition at line 292 of file qdnslookup.cpp.

References d, name, nameserver, and type.

◆ ~QDnsLookup()

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.

Definition at line 309 of file qdnslookup.cpp.

Member Function Documentation

◆ abort

void QDnsLookup::abort ( )
slot

Aborts the DNS lookup operation.

If the lookup is already finished, does nothing.

Definition at line 532 of file qdnslookup.cpp.

References d, emit, finished(), OperationCancelledError, and tr.

+ Here is the call graph for this function:

◆ bindableName()

QBindable< QString > QDnsLookup::bindableName ( )

Definition at line 365 of file qdnslookup.cpp.

References d.

◆ bindableNameserver()

QBindable< QHostAddress > QDnsLookup::bindableNameserver ( )

Definition at line 409 of file qdnslookup.cpp.

References d.

◆ bindableNameserverPort()

QBindable< quint16 > QDnsLookup::bindableNameserverPort ( )

Definition at line 433 of file qdnslookup.cpp.

References d.

◆ bindableType()

QBindable< QDnsLookup::Type > QDnsLookup::bindableType ( )

Definition at line 387 of file qdnslookup.cpp.

References d.

◆ canonicalNameRecords()

QList< QDnsDomainNameRecord > QDnsLookup::canonicalNameRecords ( ) const

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

Definition at line 459 of file qdnslookup.cpp.

◆ error()

QDnsLookup::Error QDnsLookup::error ( ) const

Definition at line 318 of file qdnslookup.cpp.

◆ errorString()

QString QDnsLookup::errorString ( ) const

Definition at line 328 of file qdnslookup.cpp.

◆ finished

void QDnsLookup::finished ( )
signal

This signal is emitted when the reply has finished processing.

Referenced by abort(), and lookup().

+ Here is the caller graph for this function:

◆ hostAddressRecords()

QList< QDnsHostAddressRecord > QDnsLookup::hostAddressRecords ( ) const

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

Definition at line 468 of file qdnslookup.cpp.

◆ isFinished()

bool QDnsLookup::isFinished ( ) const

Returns whether the reply has finished or was aborted.

Definition at line 337 of file qdnslookup.cpp.

◆ lookup

void QDnsLookup::lookup ( )
slot

Performs the DNS lookup.

The \l{QDnsLookup::finished()}{finished()} signal is emitted upon completion.

Definition at line 551 of file qdnslookup.cpp.

References Qt::BlockingQueuedConnection, QObject::connect(), d, emit, QNetworkReply::error(), QIODevice::errorString(), finished(), QDnsLookupRunnable::finished(), QCoreApplication::instance(), QNetworkReply::isFinished(), qDebug, qPrintable, qWarning, reply, and QObject::sender().

+ Here is the call graph for this function:

◆ mailExchangeRecords()

QList< QDnsMailExchangeRecord > QDnsLookup::mailExchangeRecords ( ) const

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

The records are sorted according to \l{http://www.rfc-editor.org/rfc/rfc5321.txt}{RFC 5321}, so if you use them to connect to servers, you should try them in the order they are listed.

Definition at line 481 of file qdnslookup.cpp.

◆ name()

QString QDnsLookup::name ( ) const

Definition at line 354 of file qdnslookup.cpp.

◆ nameChanged

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

This signal is emitted when the lookup \l name changes.

name is the new lookup name.

◆ nameserver()

QHostAddress QDnsLookup::nameserver ( ) const

Definition at line 398 of file qdnslookup.cpp.

◆ nameserverChanged

void QDnsLookup::nameserverChanged ( const QHostAddress & nameserver)
signal

◆ nameserverPort()

quint16 QDnsLookup::nameserverPort ( ) const

Definition at line 422 of file qdnslookup.cpp.

◆ nameserverPortChanged

void QDnsLookup::nameserverPortChanged ( quint16 port)
signal

◆ nameServerRecords()

QList< QDnsDomainNameRecord > QDnsLookup::nameServerRecords ( ) const

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

Definition at line 490 of file qdnslookup.cpp.

◆ pointerRecords()

QList< QDnsDomainNameRecord > QDnsLookup::pointerRecords ( ) const

Returns the list of pointer records associated with this lookup.

Definition at line 499 of file qdnslookup.cpp.

◆ serviceRecords()

QList< QDnsServiceRecord > QDnsLookup::serviceRecords ( ) const

Returns the list of service records associated with this lookup.

The records are sorted according to \l{http://www.rfc-editor.org/rfc/rfc2782.txt}{RFC 2782}, so if you use them to connect to servers, you should try them in the order they are listed.

Definition at line 512 of file qdnslookup.cpp.

◆ setName()

void QDnsLookup::setName ( const QString & name)

Definition at line 359 of file qdnslookup.cpp.

References d, and name.

◆ setNameserver() [1/2]

void QDnsLookup::setNameserver ( const QHostAddress & nameserver)

Definition at line 403 of file qdnslookup.cpp.

References d, and nameserver.

Referenced by setNameserver().

+ Here is the caller graph for this function:

◆ setNameserver() [2/2]

void QDnsLookup::setNameserver ( const QHostAddress & nameserver,
quint16 port )
Since
6.6 Sets the nameserver to nameserver and the port to port.

nameserver-port

See also
QDnsLookup::nameserver, QDnsLookup::nameserverPort

Definition at line 447 of file qdnslookup.cpp.

References Qt::beginPropertyUpdateGroup(), Qt::endPropertyUpdateGroup(), nameserver, setNameserver(), and setNameserverPort().

+ Here is the call graph for this function:

◆ setNameserverPort()

void QDnsLookup::setNameserverPort ( quint16 port)

Definition at line 427 of file qdnslookup.cpp.

References d, and nameserverPort.

Referenced by setNameserver().

+ Here is the caller graph for this function:

◆ setType()

void QDnsLookup::setType ( QDnsLookup::Type )

Definition at line 381 of file qdnslookup.cpp.

References d, and type.

◆ textRecords()

QList< QDnsTextRecord > QDnsLookup::textRecords ( ) const

Returns the list of text records associated with this lookup.

Definition at line 521 of file qdnslookup.cpp.

◆ type()

QDnsLookup::Type QDnsLookup::type ( ) const

Definition at line 376 of file qdnslookup.cpp.

◆ typeChanged

void QDnsLookup::typeChanged ( Type type)
signal

This signal is emitted when the lookup \l type changes.

type is the new lookup type.

Property Documentation

◆ error

QDnsLookup::error
read

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

Definition at line 143 of file qdnslookup.h.

◆ errorString

QDnsLookup::errorString
read

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

Definition at line 144 of file qdnslookup.h.

◆ name

QDnsLookup::name
readwrite

the name to lookup.

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 set to \l{QDnsLookup::Type}{NS}.

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

Definition at line 145 of file qdnslookup.h.

Referenced by QDnsLookup(), QDnsLookup(), and setName().

◆ nameserver

QDnsLookup::nameserver
readwrite

the nameserver to use for DNS lookup.

Definition at line 147 of file qdnslookup.h.

Referenced by QDnsLookup(), setNameserver(), and setNameserver().

◆ nameserverPort

QDnsLookup::nameserverPort
readwrite

the port number of nameserver to use for DNS lookup.

Since
6.6
nameserver-port

Definition at line 149 of file qdnslookup.h.

Referenced by setNameserverPort().

◆ type

QDnsLookup::type
readwrite

the type of DNS lookup.

Definition at line 146 of file qdnslookup.h.

Referenced by QDnsLookup(), QDnsLookup(), and setType().


The documentation for this class was generated from the following files: