Async 1.8.0
Async::DnsLookup Class Reference

A class for performing asynchronous DNS lookups. More...

#include <AsyncDnsLookup.h>

Inheritance diagram for Async::DnsLookup:

Public Types

using Type = DnsResourceRecord::Type
 
template<class RR >
using RRList = std::vector<std::unique_ptr<RR>>
 
template<class RR >
using SharedRRList = std::vector<std::shared_ptr<RR>>
 

Public Member Functions

 DnsLookup (void)
 Default Constructor.
 
 DnsLookup (const std::string &label, Type type=Type::A)
 Constructor.
 
 ~DnsLookup (void)
 Destructor.
 
DnsLookupoperator= (DnsLookup &&other)
 Move assignment operator.
 
void setLookupParams (const std::string &label, Type type=Type::A)
 Prepare a lookup by setting up query parameters.
 
bool lookup (const std::string &label, Type type=Type::A)
 Start a DNS lookup.
 
bool lookup (void)
 Start a DNS lookup using previously configured parameters.
 
void abort (void)
 Abort a pending lookup.
 
Type type (void) const
 Return the type of lookup.
 
std::string typeStr (void) const
 Return the type of lookup as a string.
 
const std::string & label (void) const
 Return the associated label.
 
bool isPending (void) const
 Check if a DNS lookup is pending.
 
bool lookupFailed (void) const
 Check if the lookup failed.
 
bool resultsAreReady (void) const
 Check if the DNS lookup is done or not.
 
bool recordsValid (void) const
 Check if the cached records are valid.
 
bool empty (void) const
 Check if the query returned any answers.
 
void clear (void)
 Remove any cached DNS resource records.
 
void addStaticResourceRecord (DnsResourceRecord *rr)
 Add a static resource record to the lookup result.
 
const RRList< DnsResourceRecord > & staticResourceRecords (void) const
 Get all previously added static resource records.
 
std::vector< IpAddressaddresses (void)
 Return the addresses for the host in the query.
 
DnsResourceRecord::List resourceRecords (Type type=Type::ANY) const
 Return all matching resource records.
 
template<class RR >
void resourceRecords (RRList< RR > &rrs) const
 Return resource records of a specific type.
 
template<class RR >
void resourceRecords (SharedRRList< RR > &rrs) const
 

Public Attributes

sigc::signal< void, DnsLookup & > resultsReady
 A signal to indicate that the query has been completed.
 

Detailed Description

A class for performing asynchronous DNS lookups.

Author
Tobias Blomberg
Date
2003-04-12

Use this class to make DNS lookups. Right now it supports looking up A, PTR, CNAME and SRV records. An example usage can be seen below.

#include <iostream>
#include <AsyncDnsLookup.h>
using namespace Async;
class MyClass : public sigc::trackable
{
public:
MyClass(void)
{
dns.resultsReady.connect(mem_fun(*this, &MyClass::onResultsReady));
dns.lookup("www.svxlink.org");
//dns.lookup("www.svxlink.org", DnsLookup::Type::CNAME);
//dns.lookup("185.199.110.153");
//dns.lookup("185.199.110.153", DnsLookup::Type::PTR);
//dns.lookup("153.110.199.185.in-addr.arpa.", DnsLookup::Type::PTR);
//std::string srv = "_svxreflector._tcp.test.svxlink.org";
//dns.addStaticResourceRecord(
// new DnsResourceRecordSRV(srv, 3600, 15, 10, 5304, "localhost."));
//dns.lookup(srv, DnsLookup::Type::SRV);
std::cout << "Starting " << dns.typeStr() << " record query for \""
<< dns.label() << "\"..." << std::endl;
}
void onResultsReady(DnsLookup& dns)
{
if (!dns.lookupFailed())
{
// Simple IP address lookup API
std::cout << "IP addresses received:\n";
for (auto& addr : dns.addresses())
{
std::cout << addr << std::endl;
}
std::cout << std::endl;
// Access to all resource records
std::cout << "All resource records received:\n";
for (auto& rr : dns.resourceRecords())
{
std::cout << rr->toString() << std::endl;
}
std::cout << std::endl;
// Access A records with full detail
dns.resourceRecords(a_rrs);
if (!a_rrs.empty())
{
std::cout << "A records received:\n";
for (auto& rr : a_rrs)
{
std::cout << rr->name() << "\t" << rr->ttl() << "\t" << rr->ip()
<< std::endl;
}
std::cout << std::endl;
}
// Access PTR records with full detail
dns.resourceRecords(ptr_rrs);
if (!ptr_rrs.empty())
{
std::cout << "PTR records received:\n";
for (auto& rr : ptr_rrs)
{
std::cout << rr->name() << "\t" << rr->ttl() << "\t" << rr->dname()
<< std::endl;
}
std::cout << std::endl;
}
// Access CNAME records with full detail
dns.resourceRecords(cname_rrs);
if (!cname_rrs.empty())
{
std::cout << "CNAME records received:\n";
for (auto& rr : cname_rrs)
{
std::cout << rr->name() << "\t" << rr->ttl() << "\t" << rr->cname()
<< std::endl;
}
std::cout << std::endl;
}
// Access SRV records with full detail
dns.resourceRecords(srv_rrs);
if (!srv_rrs.empty())
{
std::cout << "SRV records received:\n";
for (auto& rr : srv_rrs)
{
std::cout << rr->name() << "\t" << rr->ttl() << "\t" << rr->prio()
<< " " << rr->weight() << " " << rr->port() << " "
<< rr->target() << std::endl;
}
}
}
else
{
std::cout << "*** ERROR: The " << dns.typeStr()
<< " record DNS lookup for " << dns.label()
<< " failed" << std::endl;
}
Application::app().quit();
}
private:
DnsLookup dns;
};
int main(int argc, char **argv)
{
MyClass dns;
app.exec();
}
The core class for writing asyncronous cpp applications.
Contains a class for executing DNS queries.
An application class for writing non GUI applications.
void exec(void)
Execute the application main loop.
A class for performing asynchronous DNS lookups.
DnsResourceRecord::List resourceRecords(Type type=Type::ANY) const
Return all matching resource records.
const std::string & label(void) const
Return the associated label.
std::string typeStr(void) const
Return the type of lookup as a string.
bool lookupFailed(void) const
Check if the lookup failed.
std::vector< IpAddress > addresses(void)
Return the addresses for the host in the query.
std::vector< std::unique_ptr< DnsResourceRecord > > List
The type for a list of resource records.
Namespace for the asynchronous programming classes.
Examples
AsyncDnsLookup_demo.cpp.

Definition at line 122 of file AsyncDnsLookup.h.

Member Typedef Documentation

◆ RRList

template<class RR >
using Async::DnsLookup::RRList = std::vector<std::unique_ptr<RR>>

Definition at line 126 of file AsyncDnsLookup.h.

◆ SharedRRList

template<class RR >
using Async::DnsLookup::SharedRRList = std::vector<std::shared_ptr<RR>>

Definition at line 127 of file AsyncDnsLookup.h.

◆ Type

Constructor & Destructor Documentation

◆ DnsLookup() [1/2]

Async::DnsLookup::DnsLookup ( void )

Default Constructor.

◆ DnsLookup() [2/2]

Async::DnsLookup::DnsLookup ( const std::string & label,
Type type = Type::A )

Constructor.

Parameters
labelThe label (e.g. hostname) to lookup
typeWhat kind of resource record to look up

◆ ~DnsLookup()

Async::DnsLookup::~DnsLookup ( void )

Destructor.

Member Function Documentation

◆ abort()

void Async::DnsLookup::abort ( void )

Abort a pending lookup.

◆ addresses()

std::vector< IpAddress > Async::DnsLookup::addresses ( void )

Return the addresses for the host in the query.

Returns
Return a stl vector which contains all the addresses associated with the hostname in the query.
Precondition
The result is not available before the resultsReay signal has been emitted

Use this function to retrieve all the IP-addresses associated with the hostname in the query. Use the lookupFailed() function to find out if the query was successful or not.

The order of the hosts in the returned vector will be randomized on each call to this function.

Examples
AsyncDnsLookup_demo.cpp.

◆ addStaticResourceRecord()

void Async::DnsLookup::addStaticResourceRecord ( DnsResourceRecord * rr)

Add a static resource record to the lookup result.

Parameters
rrThe resource record to add

Use this function to add a static resource record to the DNS lookup result. Static resource records will be added to the looked up resource records before signalling that the results are ready, just as if they have been received from the DNS system. If the lookup label is empty when a lookup is initiated, no real lookup will be performed. Only static resource records will be returned in that case.

◆ clear()

void Async::DnsLookup::clear ( void )

Remove any cached DNS resource records.

◆ empty()

bool Async::DnsLookup::empty ( void ) const
inline

Check if the query returned any answers.

Returns
Return true if there are any answer records

Definition at line 252 of file AsyncDnsLookup.h.

◆ isPending()

bool Async::DnsLookup::isPending ( void ) const

Check if a DNS lookup is pending.

Returns
Return true if a DNS lookup is pending

Referenced by Async::TcpClientBase::isIdle().

◆ label()

const std::string & Async::DnsLookup::label ( void ) const
inline

Return the associated label.

Returns
Returns the label associated with this DNS lookup
Examples
AsyncDnsLookup_demo.cpp.

Definition at line 214 of file AsyncDnsLookup.h.

Referenced by Async::TcpClientBase::remoteHostName().

◆ lookup() [1/2]

bool Async::DnsLookup::lookup ( const std::string & label,
Type type = Type::A )

Start a DNS lookup.

Parameters
labelThe label (e.g. hostname) to lookup
typeWhat kind of resource record to look up
Returns
Return true if the lookup process has been started

Use this function to start a DNS lookup. If a lookup is already running for the given label/type the lookup request will be ignored. If the label or type does not match a currently pending query, the pending query will be aborted. NOTE: This function may emit the resultsReady signal before returning.

◆ lookup() [2/2]

bool Async::DnsLookup::lookup ( void )

Start a DNS lookup using previously configured parameters.

Returns
Return true if the lookup process has been started

Use this function to start a DNS lookup using parameters previously configured. If a lookup is already running the request will be ignored. NOTE: This function may emit the resultsReady signal before returning.

◆ lookupFailed()

bool Async::DnsLookup::lookupFailed ( void ) const

Check if the lookup failed.

Returns
Returns true if the lookup failed in any way

Use this function to check if the DNS lookup completed without any errors. Even if the lookup is marked as failed it may still contain valid records.

Examples
AsyncDnsLookup_demo.cpp.

◆ operator=()

DnsLookup & Async::DnsLookup::operator= ( DnsLookup && other)

Move assignment operator.

Parameters
otherThe other object to move data from
Returns
Returns this object

◆ recordsValid()

bool Async::DnsLookup::recordsValid ( void ) const

Check if the cached records are valid.

Returns
Return true if all records in the cache is valid

This function is used to check if the cache contain resource records that are still valid. A cached record become invalid when the time-to-live, TTL, expires.

◆ resourceRecords() [1/3]

template<class RR >
void Async::DnsLookup::resourceRecords ( RRList< RR > & rrs) const
inline

Return resource records of a specific type.

Parameters
rrsStore RRs in this vector that match the RR type

Use this template function to get all resource records of a specific type. E.g.

std::vector<Async::DnsResourceRecordA> rrs; dns_lookup.resourceRecords(rrs);

That code will return only the A records.

Definition at line 326 of file AsyncDnsLookup.h.

◆ resourceRecords() [2/3]

template<class RR >
void Async::DnsLookup::resourceRecords ( SharedRRList< RR > & rrs) const
inline

Definition at line 333 of file AsyncDnsLookup.h.

◆ resourceRecords() [3/3]

DnsResourceRecord::List Async::DnsLookup::resourceRecords ( Type type = Type::ANY) const
inline

Return all matching resource records.

Returns
Returns all matching resource records in an stl vector

Use this function to return all resource records, that is of the given type, that the query resulted in. If no type is given the function will return all resource records.

Examples
AsyncDnsLookup_demo.cpp.

Definition at line 306 of file AsyncDnsLookup.h.

References type().

◆ resultsAreReady()

bool Async::DnsLookup::resultsAreReady ( void ) const

Check if the DNS lookup is done or not.

Returns
Returns true if results are ready or false if not

◆ setLookupParams()

void Async::DnsLookup::setLookupParams ( const std::string & label,
Type type = Type::A )

Prepare a lookup by setting up query parameters.

Parameters
labelThe label (e.g. hostname) to lookup
typeWhat kind of resource record to look up
Returns
Return true if the lookup process has been started

Use this function to prepare a DNS lookup by setting up the query parameters. If the label or type does not match a currently pending query, the pending query will be aborted. To start a query, the lookup() function need to be called.

◆ staticResourceRecords()

const RRList< DnsResourceRecord > & Async::DnsLookup::staticResourceRecords ( void ) const
inline

Get all previously added static resource records.

Returns
Returns a vector of resource records

Definition at line 277 of file AsyncDnsLookup.h.

◆ type()

Type Async::DnsLookup::type ( void ) const
inline

Return the type of lookup.

Returns
Returns the lookup type

Definition at line 199 of file AsyncDnsLookup.h.

Referenced by resourceRecords(), and typeStr().

◆ typeStr()

std::string Async::DnsLookup::typeStr ( void ) const
inline

Return the type of lookup as a string.

Returns
Returns a string representation of the lookup type
Examples
AsyncDnsLookup_demo.cpp.

Definition at line 205 of file AsyncDnsLookup.h.

References type(), and Async::DnsResourceRecord::typeToString().

Member Data Documentation

◆ resultsReady

sigc::signal<void, DnsLookup&> Async::DnsLookup::resultsReady

A signal to indicate that the query has been completed.

Parameters
dnsA reference to the DNS object associated with the query

Definition at line 343 of file AsyncDnsLookup.h.


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