#include <netnames.h>
Public Types | |
| enum | Mode { Single, LongLived } |
| enum | Error { ErrorGeneric, ErrorNoName, ErrorTimeout, ErrorNoLocal, ErrorNoLongLived } |
Signals | |
| void | resultsReady (const QList< XMPP::NameRecord > &results) |
| void | error (XMPP::NameResolver::Error e) |
Public Member Functions | |
| NameResolver (QObject *parent=0) | |
| ~NameResolver () | |
| void | start (const QByteArray &name, NameRecord::Type type=NameRecord::A, Mode mode=Single) |
| void | stop () |
NameResolver performs an asynchronous DNS lookup for a given domain name and record type. Call start() to begin. The resultsReady() signal is emitted on success, otherwise error() is emitted. To cancel a lookup, call stop().
For example, here is how to obtain the IPv4 addresses of a domain name:
NameResolver *resolver; void do_lookup() { resolver = new NameResolver; connect(resolver, SIGNAL(resultsReady(const QList<XMPP::NameRecord> &)), SLOT(dns_resultsReady(const QList<XMPP::NameRecord> &))); connect(resolver, SIGNAL(error(XMPP::NameResolver::Error)), SLOT(dns_error(XMPP::NameResolver::Error))); // look up affinix.com resolver->start("affinix.com"); } void dns_resultsReady(const QList<XMPP::NameRecord> &results) { // print IP addresses foreach(NameRecord i, results) printf("%s\n", qPrintable(i.address().toString())); } void dns_error(XMPP::NameResolver::Error error) { // handle error ... }
Yes, a domain name can have multiple IP addresses. Many applications ignore this fact, and use only one of the answers. A proper network application should try connecting to each IP address until one succeeds.
To lookup other types, pass the desired type to start(). For example, suppose you want to look up the MX record of a domain name:
// look up the MX record for affinix.com resolver->start("affinix.com", NameRecord::Mx);
It is also possible to perform long-lived queries. This is generally useful for DNS Service Discovery. Long-lived queries are continuous, and resultsReady() may be emitted multiple times. Unlike a normal lookup, which stops once the results are returned, a long-lived query will keep going until stop() is called.
For example, suppose you want to scan the local network for SSH services. According to the DNS-SD protocol, this is done by querying for the name "_ssh._tcp.local." of type PTR.
// monitor for SSH services on the local network resolver->start("_ssh._tcp.local.", NameRecord::Ptr, NameResolver::LongLived);
Don't be alarmed by the trailing dot (".") character in this last example. It is not well known, but all valid DNS domain names end with a dot. However, NameResolver, like most DNS programming interfaces, allows the dot to be left out. What this means is that if a trailing dot is missing in the input to start(), NameResolver will internally append one before performing the query.
|
|
|
Resolve mode.
|
|
|
Constructs a new resolver object with the given parent.
|
|
|
Destroys the resolver object. The lookup is, of course, stopped. |
|
|
Notification of error. This signal is emitted if an error has occurred while performing a lookup. The reason for error can be found in e. Regardless of the mode used, the lookup is stopped when an error occurs. |
|
|
Notification of result records. This signal is emitted when results of the lookup operation have arrived. The results parameter is a list of NameRecords. All records will be of the type queried for with start(), unless the NameRecord::Any type was specified, in which case the records may be of any type When using the NameResolver::Single mode, the lookup is stopped once results are ready. However, with the NameResolver::LongLived mode, the lookup stays active, and in that case this signal may be emitted multiple times. |
|
||||||||||||||||
|
Starts a lookup. A lookup for name of type is started. For normal queries, mode should be NameResolver::Single (this is the default). For long-lived queries, use NameResolver::LongLived.
|
|
|
Stops a lookup. Use this function if you want to stop the current lookup, such that the resolver object may be reused again later. If you don't plan to reuse the object, then destroying the object is enough.
|
1.4.6