Async 1.8.0
AsyncDnsLookup.h
Go to the documentation of this file.
1
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#include <random>
52
53
54/****************************************************************************
55 *
56 * Project Includes
57 *
58 ****************************************************************************/
59
60#include <AsyncIpAddress.h>
62
63
64/****************************************************************************
65 *
66 * Local Includes
67 *
68 ****************************************************************************/
69
70
71
72/****************************************************************************
73 *
74 * Forward declarations
75 *
76 ****************************************************************************/
77
78
79
80/****************************************************************************
81 *
82 * Namespace
83 *
84 ****************************************************************************/
85
86namespace Async
87{
88
89/****************************************************************************
90 *
91 * Defines & typedefs
92 *
93 ****************************************************************************/
94
95class DnsLookupWorker;
96
97
98/****************************************************************************
99 *
100 * Exported Global Variables
101 *
102 ****************************************************************************/
103
104
105
106/****************************************************************************
107 *
108 * Class definitions
109 *
110 ****************************************************************************/
111
122class DnsLookup : public sigc::trackable
123{
124 public:
126 template <class RR> using RRList = std::vector<std::unique_ptr<RR>>;
127 template <class RR> using SharedRRList = std::vector<std::shared_ptr<RR>>;
128
133
139 DnsLookup(const std::string& label, Type type=Type::A);
140
145
152
164 void setLookupParams(const std::string& label, Type type=Type::A);
165
178 bool lookup(const std::string& label, Type type=Type::A);
179
188 bool lookup(void);
189
193 void abort(void);
194
199 Type type(void) const { return m_type; }
200
205 std::string typeStr(void) const
206 {
208 }
209
214 const std::string &label(void) const { return m_label; }
215
220 bool isPending(void) const;
221
230 bool lookupFailed(void) const;
231
236 bool resultsAreReady(void) const;
237
246 bool recordsValid(void) const;
247
252 bool empty(void) const { return resourceRecordsP().empty(); }
253
257 void clear(void);
258
272
278 {
279 return m_static_rrs;
280 }
281
296 std::vector<IpAddress> addresses(void);
297
307 {
309 cloneResourceRecords(rrs, type);
310 return rrs;
311 }
312
325 template <class RR>
327 {
328 rrs.clear();
329 cloneResourceRecords(rrs, RR::staticType());
330 }
331
332 template <class RR>
334 {
335 rrs.clear();
336 cloneResourceRecords(rrs, RR::staticType());
337 }
338
343 sigc::signal<void, DnsLookup&> resultsReady;
344
345 private:
346 typedef std::vector<DnsResourceRecord*> RRListP;
347
348 std::string m_label;
349 Type m_type = Type::A;
350 DnsLookupWorker* m_worker = 0;
351 RRList<DnsResourceRecord> m_static_rrs;
352 std::default_random_engine m_rng;
353
354 void onResultsReady(void);
355 const RRListP& resourceRecordsP(void) const;
356
357 template <class RR>
358 void cloneResourceRecords(RRList<RR>& rrs, Type type) const
359 {
360 for (const auto& rr : resourceRecordsP())
361 {
362 if ((type == RR::Type::ANY) || (rr->type() == type))
363 {
364 rrs.push_back(std::unique_ptr<RR>(static_cast<RR*>(rr->clone())));
365 }
366 }
367 }
368
369 template <class RR>
370 void cloneResourceRecords(SharedRRList<RR>& rrs, Type type) const
371 {
372 for (const auto& rr : resourceRecordsP())
373 {
374 if ((type == RR::Type::ANY) || (rr->type() == type))
375 {
376 rrs.push_back(std::shared_ptr<RR>(static_cast<RR*>(rr->clone())));
377 }
378 }
379 }
380
381}; /* class DnsLookup */
382
383
384} /* namespace */
385
386#endif /* ASYNC_DNS_LOOKUP_INCLUDED */
387
388
389
390/*
391 * This file has not been truncated
392 */
393
A collection of classes representing DNS resource records.
Platform independent representation of an IP address.
A class for performing asynchronous DNS lookups.
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.