Async 1.7.0
AsyncDnsLookup.h
Go to the documentation of this file.
1
30
34
35
36
37#ifndef ASYNC_DNS_LOOKUP_INCLUDED
38#define ASYNC_DNS_LOOKUP_INCLUDED
39
40
41/****************************************************************************
42 *
43 * System Includes
44 *
45 ****************************************************************************/
46
47#include <sigc++/sigc++.h>
48
49#include <vector>
50#include <memory>
51
52
53/****************************************************************************
54 *
55 * Project Includes
56 *
57 ****************************************************************************/
58
59#include <AsyncIpAddress.h>
61
62
63/****************************************************************************
64 *
65 * Local Includes
66 *
67 ****************************************************************************/
68
69
70
71/****************************************************************************
72 *
73 * Forward declarations
74 *
75 ****************************************************************************/
76
77
78
79/****************************************************************************
80 *
81 * Namespace
82 *
83 ****************************************************************************/
84
85namespace Async
86{
87
88/****************************************************************************
89 *
90 * Defines & typedefs
91 *
92 ****************************************************************************/
93
94class DnsLookupWorker;
95
96
97/****************************************************************************
98 *
99 * Exported Global Variables
100 *
101 ****************************************************************************/
102
103
104
105/****************************************************************************
106 *
107 * Class definitions
108 *
109 ****************************************************************************/
110
121class DnsLookup : public sigc::trackable
122{
123 public:
125 template <class RR> using RRList = std::vector<std::unique_ptr<RR>>;
126 template <class RR> using SharedRRList = std::vector<std::shared_ptr<RR>>;
127
132
138 DnsLookup(const std::string& label, Type type=Type::A);
139
144
151
163 void setLookupParams(const std::string& label, Type type=Type::A);
164
177 bool lookup(const std::string& label, Type type=Type::A);
178
187 bool lookup(void);
188
192 void abort(void);
193
198 Type type(void) const { return m_type; }
199
204 std::string typeStr(void) const
205 {
207 }
208
213 const std::string &label(void) const { return m_label; }
214
219 bool isPending(void) const;
220
229 bool lookupFailed(void) const;
230
235 bool resultsAreReady(void) const;
236
245 bool recordsValid(void) const;
246
251 bool empty(void) const { return resourceRecordsP().empty(); }
252
256 void clear(void);
257
271
277 {
278 return m_static_rrs;
279 }
280
292 std::vector<IpAddress> addresses(void);
293
303 {
305 cloneResourceRecords(rrs, type);
306 return rrs;
307 }
308
321 template <class RR>
323 {
324 rrs.clear();
325 cloneResourceRecords(rrs, RR::staticType());
326 }
327
328 template <class RR>
330 {
331 rrs.clear();
332 cloneResourceRecords(rrs, RR::staticType());
333 }
334
339 sigc::signal<void, DnsLookup&> resultsReady;
340
341 private:
342 typedef std::vector<DnsResourceRecord*> RRListP;
343
344 std::string m_label;
345 Type m_type = Type::A;
346 DnsLookupWorker* m_worker = 0;
347 RRList<DnsResourceRecord> m_static_rrs;
348
349 void onResultsReady(void);
350 const RRListP& resourceRecordsP(void) const;
351
352 template <class RR>
353 void cloneResourceRecords(RRList<RR>& rrs, Type type) const
354 {
355 for (const auto& rr : resourceRecordsP())
356 {
357 if ((type == RR::Type::ANY) || (rr->type() == type))
358 {
359 rrs.push_back(std::unique_ptr<RR>(static_cast<RR*>(rr->clone())));
360 }
361 }
362 }
363
364 template <class RR>
365 void cloneResourceRecords(SharedRRList<RR>& rrs, Type type) const
366 {
367 for (const auto& rr : resourceRecordsP())
368 {
369 if ((type == RR::Type::ANY) || (rr->type() == type))
370 {
371 rrs.push_back(std::shared_ptr<RR>(static_cast<RR*>(rr->clone())));
372 }
373 }
374 }
375
376}; /* class DnsLookup */
377
378
379} /* namespace */
380
381#endif /* ASYNC_DNS_LOOKUP_INCLUDED */
382
383
384
385/*
386 * This file has not been truncated
387 */
388
A collection of classes representing DNS resource records.
Platform independent representation of an IP address.
std::vector< std::unique_ptr< RR > > RRList
~DnsLookup(void)
Destructor.
void resourceRecords(SharedRRList< RR > &rrs) const
void resourceRecords(RRList< RR > &rrs) const
Return resource records of a specific type.
std::vector< std::shared_ptr< RR > > SharedRRList
bool recordsValid(void) const
Check if the cached records are valid.
DnsResourceRecord::List resourceRecords(Type type=Type::ANY) const
Return all matching resource records.
DnsLookup & operator=(DnsLookup &&other)
Move assignment operator.
const RRList< DnsResourceRecord > & staticResourceRecords(void) const
Get all previously added static resource records.
bool empty(void) const
Check if the query returned any answers.
const std::string & label(void) const
Return the associated label.
std::string typeStr(void) const
Return the type of lookup as a string.
void abort(void)
Abort a pending lookup.
bool lookupFailed(void) const
Check if the lookup failed.
bool lookup(const std::string &label, Type type=Type::A)
Start a DNS lookup.
void addStaticResourceRecord(DnsResourceRecord *rr)
Add a static resource record to the lookup result.
DnsLookup(void)
Default Constructor.
bool resultsAreReady(void) const
Check if the DNS lookup is done or not.
std::vector< IpAddress > addresses(void)
Return the addresses for the host in the query.
DnsResourceRecord::Type Type
sigc::signal< void, DnsLookup & > resultsReady
A signal to indicate that the query has been completed.
bool lookup(void)
Start a DNS lookup using previously configured parameters.
DnsLookup(const std::string &label, Type type=Type::A)
Constructor.
Type type(void) const
Return the type of lookup.
bool isPending(void) const
Check if a DNS lookup is pending.
void clear(void)
Remove any cached DNS resource records.
void setLookupParams(const std::string &label, Type type=Type::A)
Prepare a lookup by setting up query parameters.
The base class for representing a DNS resource record.
std::vector< std::unique_ptr< DnsResourceRecord > > List
The type for a list of resource records.
static const std::string & typeToString(Type type)
The type for this specific class represented as a string.
Namespace for the asynchronous programming classes.