EpcTools
An event based multi-threaded C++ development framework.
dnsquery.h
Go to the documentation of this file.
1 /*
2 * Copyright (c) 2017 Sprint
3 * Copyright (c) 2019 Sprint
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17 
18 #ifndef __DNSQUERY_H
19 #define __DNSQUERY_H
20 
23 
24 #include <iostream>
25 #include <memory>
26 #include <string>
27 #include <map>
28 #include <list>
29 
30 #include "estring.h"
31 #include "esynch.h"
32 #include "dnsrecord.h"
33 
34 namespace DNS
35 {
36  class Cache;
37  class Query;
38  class QueryProcessor;
39  class QueryProcessorThread;
40  class QueryCacheKey;
41 
44 
46  typedef std::shared_ptr<Query> QueryPtr;
48  typedef std::map<QueryCacheKey, QueryPtr> QueryCache;
49  extern "C" typedef Void(*CachedDNSQueryCallback)(QueryPtr q, Bool cacheHit, const Void *data);
51 
54 
56  class QueryCacheKey
57  {
58  public:
59  QueryCacheKey( ns_type rtype, const std::string &domain )
60  : m_type( rtype ),
61  m_domain( domain )
62  {
63  }
64 
65  QueryCacheKey( const QueryCacheKey &other )
66  {
67  m_type = other.m_type;
68  m_domain = other.m_domain;
69  }
70 
71  const QueryCacheKey& operator=( const QueryCacheKey &r )
72  {
73  m_type = r.m_type;
74  m_domain = r.m_domain;
75  return *this;
76  }
77 
78  Bool operator<( const QueryCacheKey &r ) const
79  {
80  return
81  this->m_type < r.m_type ? true :
82  this->m_type > r.m_type ? false :
83  this->m_domain < r.m_domain ? true : false;
84  }
85 
86  const ns_type getType() { return m_type; }
87  const EString &getDomain() { return m_domain; }
88 
89  private:
90  ns_type m_type;
91  EString m_domain;
92  };
94 
97 
99  class Query
100  {
101  friend Cache;
102  friend QueryProcessor;
103  friend QueryProcessorThread;
104 
105  public:
109  Query( ns_type rtype, const std::string &domain )
110  : m_qp( NULL ),
111  m_cb( NULL ),
112  m_event( NULL ),
113  m_data( NULL ),
114  m_type( rtype ),
115  m_domain( domain ),
116  m_ttl( UINT32_MAX ),
117  m_expires( LONG_MAX ),
118  m_ignorecache( false )
119  {
120  }
123  {
124  }
125 
129  {
130  if ( q )
131  m_question.push_back( q );
132  }
136  {
137  if ( a )
138  {
139  if ( m_expires == 0 || a->getTTL() != 0 )
140  {
141  if (a->getTTL() != 0)
142  {
143  if (a->getExpires() < m_expires)
144  m_expires = a->getExpires();
145  if (a->getTTL() < m_ttl)
146  m_ttl = a->getTTL();
147  }
148  }
149  m_answer.push_back( a );
150  }
151  }
155  {
156  if ( a )
157  {
158  if ( m_expires == 0 || a->getTTL() != 0 )
159  {
160  if (a->getTTL() != 0)
161  {
162  if (a->getExpires() < m_expires)
163  m_expires = a->getExpires();
164  if (a->getTTL() < m_ttl)
165  m_ttl = a->getTTL();
166  }
167  }
168  m_authority.push_back( a );
169  }
170  }
174  {
175  if ( a )
176  {
177  if ( m_expires == 0 || a->getTTL() != 0 )
178  {
179  if (a->getTTL() != 0)
180  {
181  if (a->getExpires() < m_expires)
182  m_expires = a->getExpires();
183  if (a->getTTL() < m_ttl)
184  m_ttl = a->getTTL();
185  }
186  }
187  m_additional.push_back( a );
188  }
189  }
190 
193  ns_type getType() { return m_type; }
196  const EString &getDomain() { return m_domain; }
197 
200  uint32_t getTTL() { return m_ttl; }
203  time_t getExpires() { return m_expires; }
206  Bool isExpired() { return time(NULL) >= m_expires; }
209  Bool ignoreCache() { return m_ignorecache; }
210 
213  const std::list<Question*> &getQuestions() { return m_question; }
216  const ResourceRecordList &getAnswers() { return m_answer; }
219  const ResourceRecordList &getAuthorities() { return m_authority; }
222  const ResourceRecordList &getAdditional() { return m_additional; }
223 
225  Void dump()
226  {
227  std::cout << "QUERY type=" << getType() << " domain=" << getDomain() << std::endl;
228  std::cout << "QUESTION:" << std::endl;
229  for (QuestionList::const_iterator it = getQuestions().begin();
230  it != getQuestions().end();
231  ++it )
232  {
233  (*it)->dump();
234  }
235 
236  std::cout << "ANSWER:" << std::endl;
237  for (ResourceRecordList::const_iterator it = getAnswers().begin();
238  it != getAnswers().end();
239  ++it )
240  {
241  (*it)->dump();
242  }
243 
244  std::cout << "AUTHORITY:" << std::endl;
245  for (ResourceRecordList::const_iterator it = getAuthorities().begin();
246  it != getAuthorities().end();
247  ++it )
248  {
249  (*it)->dump();
250  }
251 
252  std::cout << "ADDITIONAL:" << std::endl;
253  for (ResourceRecordList::const_iterator it = getAdditional().begin();
254  it != getAdditional().end();
255  ++it )
256  {
257  (*it)->dump();
258  }
259  }
260 
263  EEvent *getCompletionEvent() { return m_event; }
266  EEvent *setCompletionEvent(EEvent *event) { return m_event = event; }
267 
270  CachedDNSQueryCallback getCallback() { return m_cb; }
273  CachedDNSQueryCallback setCallback(CachedDNSQueryCallback cb) { return m_cb = cb; }
274 
277  Bool getError() { return m_err; }
281  Bool setError(Bool err) { return m_err = err; }
282 
285  EString &getErrorMsg() { return m_errmsg; }
289  EString &setErrorMsg(const char *errmsg) { return m_errmsg = errmsg; }
291  EString &setErrorMsg(const std::string &errmsg) { return m_errmsg = errmsg; }
292 
295  Bool getIgnoreCache() { return m_ignorecache; }
299  Bool setIgnoreCache(Bool ignorecache) { return m_ignorecache = ignorecache; }
300 
301  protected:
303  QueryProcessor *getQueryProcessor() { return m_qp; }
304  QueryProcessor *setQueryProcessor(QueryProcessor *qp) { return m_qp = qp; }
305 
306  const Void *getData() { return m_data; }
307  const Void *setData(const Void *data) { return m_data = data; }
309 
310  private:
311  QueryProcessor *m_qp;
312  CachedDNSQueryCallback m_cb;
313  EEvent *m_event;
314  const Void *m_data;
315 
316  ns_type m_type;
317  EString m_domain;
318  QuestionList m_question;
319  ResourceRecordList m_answer;
320  ResourceRecordList m_authority;
321  ResourceRecordList m_additional;
322  uint32_t m_ttl;
323  time_t m_expires;
324  Bool m_ignorecache;
325 
326  Bool m_err;
327  EString m_errmsg;
328  };
329 }
330 
331 #endif // #ifndef __DNSQUERY_H
DNS::Query::getErrorMsg
EString & getErrorMsg()
Retrieves the error message associated with this DNS query.
Definition: dnsquery.h:285
DNS::Query::getType
ns_type getType()
Retrieves the named service type associated with this query.
Definition: dnsquery.h:193
DNS::Query::getAnswers
const ResourceRecordList & getAnswers()
Retrieves the collection of query result answer records.
Definition: dnsquery.h:216
EEvent
An object that can be waited on to be set in another thread.
Definition: esynch.h:616
DNS::Query::getCallback
CachedDNSQueryCallback getCallback()
Retrieves the query completion callback function.
Definition: dnsquery.h:270
DNS::ResourceRecord::getExpires
time_t getExpires()
Retrieves the expiration time of this resource record.
Definition: dnsrecord.h:145
DNS::Query::getExpires
time_t getExpires()
retrieves the expiration time associated with the query results.
Definition: dnsquery.h:203
DNS::Query::getAuthorities
const ResourceRecordList & getAuthorities()
Retrieves the collection of query result authority records.
Definition: dnsquery.h:219
DNS::Query::getTTL
uint32_t getTTL()
Retrieves the time to live (TTL) value associated with the query results.
Definition: dnsquery.h:200
DNS::ResourceRecord::getTTL
uint32_t getTTL()
Retrieves the time interval (in seconds) that the resource record may be cached before it should be d...
Definition: dnsrecord.h:142
DNS::Query::addAdditional
Void addAdditional(ResourceRecord *a)
Adds an additional record to the query results.
Definition: dnsquery.h:173
DNS::Query::getIgnoreCache
Bool getIgnoreCache()
Retrieves the current ignore cache value.
Definition: dnsquery.h:295
DNS::Query::setCompletionEvent
EEvent * setCompletionEvent(EEvent *event)
Assigns the query completion event.
Definition: dnsquery.h:266
DNS::Query::setError
Bool setError(Bool err)
Assigns the error indication.
Definition: dnsquery.h:281
DNS::Query::setCallback
CachedDNSQueryCallback setCallback(CachedDNSQueryCallback cb)
Assigns the query completion callback function for this DNS query.
Definition: dnsquery.h:273
esynch.h
Contains definitions for synchronization objects.
DNS::ResourceRecord
Represents a DNS Resource Record.
Definition: dnsrecord.h:106
DNS::Query::getQuestions
const std::list< Question * > & getQuestions()
Retrieves the collection of query result question records.
Definition: dnsquery.h:213
DNS::Query::getDomain
const EString & getDomain()
Retrieves the domain associated with this query.
Definition: dnsquery.h:196
DNS::Query::Query
Query(ns_type rtype, const std::string &domain)
Class constructor.
Definition: dnsquery.h:109
DNS::Cache
Defines the functionality associated with a DNS cache.
Definition: dnscache.h:184
dnsrecord.h
Defines the classes related to DNS records.
DNS::Query
Defines a DNS query.
Definition: dnsquery.h:99
DNS::Query::addAnswer
Void addAnswer(ResourceRecord *a)
Adds an answer record to the query results.
Definition: dnsquery.h:135
DNS::Query::~Query
~Query()
Class destructor.
Definition: dnsquery.h:122
DNS::Query::getAdditional
const ResourceRecordList & getAdditional()
Retrieves the collection of query result additional records.
Definition: dnsquery.h:222
DNS::Query::setIgnoreCache
Bool setIgnoreCache(Bool ignorecache)
Assigns the ignore cache value.
Definition: dnsquery.h:299
DNS::Query::setErrorMsg
EString & setErrorMsg(const std::string &errmsg)
Assigns the error message associated with this DNS query.
Definition: dnsquery.h:291
DNS::Query::dump
Void dump()
Prints the information associated with this DNS query object.
Definition: dnsquery.h:225
DNS
Definition: dnscache.h:33
EString
String class.
Definition: estring.h:30
DNS::Query::addAuthority
Void addAuthority(ResourceRecord *a)
Adds an authority record to the query results.
Definition: dnsquery.h:154
DNS::Question
Represents the question for the name server.
Definition: dnsrecord.h:40
DNS::Query::ignoreCache
Bool ignoreCache()
Retrieves an indication if the DNS cache for this query should be ignored.
Definition: dnsquery.h:209
DNS::Query::setErrorMsg
EString & setErrorMsg(const char *errmsg)
Assigns the error message associated with this DNS query.
Definition: dnsquery.h:289
DNS::QueryPtr
std::shared_ptr< Query > QueryPtr
A typedef to std::shared_ptr<Query>.
Definition: dnsquery.h:40
DNS::Query::getError
Bool getError()
Retrieves the error indication.
Definition: dnsquery.h:277
DNS::Query::isExpired
Bool isExpired()
Retrieves an indication if the query results have expired.
Definition: dnsquery.h:206
estring.h
Encapsulates and extends a std::string object.
DNS::Query::addQuestion
Void addQuestion(Question *q)
Adds a question record to a query results.
Definition: dnsquery.h:128
DNS::Query::getCompletionEvent
EEvent * getCompletionEvent()
Retrieves the completion event associated with this query.
Definition: dnsquery.h:263