Async 1.7.0
Async::DnsResourceRecordSRV Class Reference

A class for representing a SRV DNS resource record. More...

#include <AsyncDnsResourceRecord.h>

Inheritance diagram for Async::DnsResourceRecordSRV:
Async::DnsResourceRecordCRTP< DnsResourceRecordSRV > Async::DnsResourceRecord

Public Types

using Prio = unsigned int
 
using Weight = unsigned int
 
using Port = unsigned int
 
using Target = std::string
 
- Public Types inherited from Async::DnsResourceRecordCRTP< DnsResourceRecordSRV >
using List
 The type for a list of resource records.
 
- Public Types inherited from Async::DnsResourceRecord
enum class  Type {
  ANY , A , PTR , CNAME ,
  SRV
}
 The record type. More...
 
enum class  Class { IN }
 The record class. More...
 
using Name = std::string
 
using Ttl = uint32_t
 
using List = std::vector<std::unique_ptr<DnsResourceRecord>>
 The type for a list of resource records.
 

Public Member Functions

 DnsResourceRecordSRV (const Name &name, Ttl ttl, Prio prio, Weight weight, Port port, const Target &target)
 Constructor.
 
virtual bool operator== (const DnsResourceRecordSRV &other) const
 Equality comparison operator.
 
virtual std::string toString (void) const
 The string representation of this record.
 
void setPrio (Prio prio)
 Set the prio for this record.
 
Prio prio (void) const
 The prio for this record.
 
void setWeight (Weight weight)
 Set the weight for this record.
 
Weight weight (void) const
 The weight for this record.
 
void setPort (Port port)
 Set the network port for this record.
 
Port port (void) const
 The network port for this record.
 
void setTarget (const Target &target)
 Set the FQDN for this record.
 
const Targettarget (void) const
 The FQDN for this record.
 
- Public Member Functions inherited from Async::DnsResourceRecordCRTP< DnsResourceRecordSRV >
 DnsResourceRecordCRTP (const Name &name, Ttl ttl)
 Constructor.
 
virtual DnsResourceRecordclone (void) const
 Clone this class.
 
virtual bool operator== (const DnsResourceRecord &other) const
 Equality comparison operator.
 
virtual const Type type (void) const override
 The type of record.
 
- Public Member Functions inherited from Async::DnsResourceRecord
 DnsResourceRecord (const Name &name, Ttl ttl)
 Constructor.
 
virtual ~DnsResourceRecord (void)
 Destructor.
 
Class classId (void) const
 The DNS class for the record.
 
const char * classStr (void) const
 The DNS class for the record as a string.
 
const std::string & typeStr (void) const
 The type of record as a string.
 
void setName (const Name &name)
 Set the name for this record.
 
const Namename (void) const
 The name of this record.
 
void setTtl (Ttl ttl)
 Set the TTL for this record.
 
Ttl ttl (void) const
 The TTL for this record.
 

Static Public Member Functions

static const Type staticType (void)
 The type for this specific class.
 
- Static Public Member Functions inherited from Async::DnsResourceRecord
static const Type staticType (void)
 The type for this specific class.
 
static const std::string & typeToString (Type type)
 The type for this specific class represented as a string.
 

Additional Inherited Members

- Static Public Attributes inherited from Async::DnsResourceRecord
static constexpr Ttl MAX_TTL = 0x7fffffff
 The maximum allowed value for a TTL.
 

Detailed Description

A class for representing a SRV DNS resource record.

Author
Tobias Blomberg / SM0SVX
Date
2021-05-22

This class represents an SRV DNS resource record. One or more resource records is the result of performing a DNS query. This specific resource record maps a service name to information for that service.

This is one of the more advanced resource record types. A query for this type of resource record often return multiple entries. Each entry contain information about priority, weight, port and target.

Priority: The priority of the target host, lower value means more preferred. Weight: A relative weight for records with the same priority, higher value means higher chance of getting picked. Port: the TCP or UDP port on which the service is to be found. Target: the canonical hostname of the machine providing the service, ending in a dot.

An example of SRV records in textual form that might be found in a zone file might be the following:

_sip._tcp.example.com. 86400 IN SRV 0 60 5060 sipserver1.example.com. _sip._tcp.example.com. 86400 IN SRV 0 40 5060 sipserver2.example.com. _sip._tcp.example.com. 86400 IN SRV 1 100 5061 sipserver-backup.example.com.

The client should choose sipserver1 or sipserver2 first. Sipserver1 should be chosen 60% of the time and sipserver2 should be chosen 40% of the time. If none of the two first servers are reachable the client should try to connect to sipserver-backup.

#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
DnsResourceRecordA::List a_rrs;
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
DnsResourceRecordPTR::List ptr_rrs;
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
DnsResourceRecordCNAME::List cname_rrs;
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
DnsResourceRecordSRV::List srv_rrs;
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.
Namespace for the asynchronous programming classes.

Definition at line 579 of file AsyncDnsResourceRecord.h.

Member Typedef Documentation

◆ Port

using Async::DnsResourceRecordSRV::Port = unsigned int

Definition at line 585 of file AsyncDnsResourceRecord.h.

◆ Prio

using Async::DnsResourceRecordSRV::Prio = unsigned int

Definition at line 583 of file AsyncDnsResourceRecord.h.

◆ Target

Definition at line 586 of file AsyncDnsResourceRecord.h.

◆ Weight

Definition at line 584 of file AsyncDnsResourceRecord.h.

Constructor & Destructor Documentation

◆ DnsResourceRecordSRV()

Async::DnsResourceRecordSRV::DnsResourceRecordSRV ( const Name & name,
Ttl ttl,
Prio prio,
Weight weight,
Port port,
const Target & target )
inline

Constructor.

Parameters
nameThe name of this record
ttlThe time-to-live, in seconds, for this record
prioThe priority for this record
weightThe weight for this record
portThe network port for this record
targetThe FQDN associaated with this record name

Definition at line 602 of file AsyncDnsResourceRecord.h.

References Async::DnsResourceRecordCRTP< DnsResourceRecordSRV >::DnsResourceRecordCRTP(), DnsResourceRecordSRV(), Async::DnsResourceRecord::name(), port(), prio(), target(), Async::DnsResourceRecord::ttl(), and weight().

Referenced by DnsResourceRecordSRV(), and operator==().

Member Function Documentation

◆ operator==()

virtual bool Async::DnsResourceRecordSRV::operator== ( const DnsResourceRecordSRV & other) const
inlinevirtual

Equality comparison operator.

Parameters
otherThe other resource record to comapare to
Returns
Return true if the two records are equal

NOTE: The TTL is not used in the comparison.

Implements Async::DnsResourceRecordCRTP< DnsResourceRecordSRV >.

Definition at line 614 of file AsyncDnsResourceRecord.h.

References DnsResourceRecordSRV(), Async::DnsResourceRecord::operator==(), port(), prio(), target(), and weight().

◆ port()

Port Async::DnsResourceRecordSRV::port ( void ) const
inline

The network port for this record.

Returns
Return the network port number

Definition at line 669 of file AsyncDnsResourceRecord.h.

Referenced by DnsResourceRecordSRV(), operator==(), setPort(), and toString().

◆ prio()

Prio Async::DnsResourceRecordSRV::prio ( void ) const
inline

The prio for this record.

Returns
The prio number for this record (lower mean higher prio)

Definition at line 645 of file AsyncDnsResourceRecord.h.

Referenced by DnsResourceRecordSRV(), operator==(), setPrio(), and toString().

◆ setPort()

void Async::DnsResourceRecordSRV::setPort ( Port port)
inline

Set the network port for this record.

Parameters
portThe new network port number

Definition at line 663 of file AsyncDnsResourceRecord.h.

References port().

◆ setPrio()

void Async::DnsResourceRecordSRV::setPrio ( Prio prio)
inline

Set the prio for this record.

Parameters
prioThe new priority for this record (lower mean higher prio)

Definition at line 639 of file AsyncDnsResourceRecord.h.

References prio().

◆ setTarget()

void Async::DnsResourceRecordSRV::setTarget ( const Target & target)
inline

Set the FQDN for this record.

Parameters
targetThe new FQDN for this record

Definition at line 675 of file AsyncDnsResourceRecord.h.

References target().

◆ setWeight()

void Async::DnsResourceRecordSRV::setWeight ( Weight weight)
inline

Set the weight for this record.

Parameters
weightThe new weight for this record

Definition at line 651 of file AsyncDnsResourceRecord.h.

References weight().

◆ staticType()

static const Type Async::DnsResourceRecordSRV::staticType ( void )
inlinestatic

The type for this specific class.

Definition at line 591 of file AsyncDnsResourceRecord.h.

References Async::DnsResourceRecord::SRV.

◆ target()

const Target & Async::DnsResourceRecordSRV::target ( void ) const
inline

The FQDN for this record.

Returns
Return the FQDN for this record

Definition at line 681 of file AsyncDnsResourceRecord.h.

Referenced by DnsResourceRecordSRV(), operator==(), setTarget(), and toString().

◆ toString()

virtual std::string Async::DnsResourceRecordSRV::toString ( void ) const
inlinevirtual

The string representation of this record.

Returns
Return the string representation of this record

Reimplemented from Async::DnsResourceRecord.

Definition at line 627 of file AsyncDnsResourceRecord.h.

References port(), prio(), target(), Async::DnsResourceRecord::toString(), and weight().

◆ weight()

Weight Async::DnsResourceRecordSRV::weight ( void ) const
inline

The weight for this record.

Returns
Return the weight for this record

Definition at line 657 of file AsyncDnsResourceRecord.h.

Referenced by DnsResourceRecordSRV(), operator==(), setWeight(), and toString().


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