Anope IRC Services  Version 2.0
dns.h
Go to the documentation of this file.
1 /*
2  *
3  * (C) 2003-2014 Anope Team
4  * Contact us at team@anope.org
5  *
6  * Please read COPYING and README for further details.
7  *
8  * Based on the original code of Epona by Lara.
9  * Based on the original code of Services by Andy Church.
10  *
11  */
12 
13 #ifndef DNS_H
14 #define DNS_H
15 
16 namespace DNS
17 {
20  enum QueryType
21  {
22  /* Nothing */
24  /* A simple A lookup */
25  QUERY_A = 1,
26  /* An authoritative name server */
27  QUERY_NS = 2,
28  /* A CNAME lookup */
30  /* Start of a zone of authority */
31  QUERY_SOA = 6,
32  /* Reverse DNS lookup */
33  QUERY_PTR = 12,
34  /* IPv6 AAAA lookup */
35  QUERY_AAAA = 28,
36  /* Zone transfer */
37  QUERY_AXFR = 252,
38  /* A lookup for any record */
39  QUERY_ANY = 255
40  };
41 
44  enum
45  {
46  QUERYFLAGS_QR = 0x8000,
49  QUERYFLAGS_AA = 0x400,
50  QUERYFLAGS_TC = 0x200,
51  QUERYFLAGS_RD = 0x100,
52  QUERYFLAGS_RA = 0x80,
53  QUERYFLAGS_Z = 0x70,
55  };
56 
57  enum Error
58  {
72  };
73 
74  struct Question
75  {
78  unsigned short qclass;
79 
80  Question() : type(QUERY_NONE), qclass(0) { }
81  Question(const Anope::string &n, QueryType t, unsigned short c = 1) : name(n), type(t), qclass(c) { }
82  inline bool operator==(const Question & other) const { return name == other.name && type == other.type && qclass == other.qclass; }
83 
84  struct hash
85  {
86  size_t operator()(const Question &q) const
87  {
88  return Anope::hash_ci()(q.name);
89  }
90  };
91  };
92 
94  {
95  unsigned int ttl;
97  time_t created;
98 
99  ResourceRecord(const Anope::string &n, QueryType t, unsigned short c = 1) : Question(n, t, c), ttl(0), created(Anope::CurTime) { }
100  ResourceRecord(const Question &q) : Question(q), ttl(0), created(Anope::CurTime) { }
101  };
102 
103  struct Query
104  {
105  std::vector<Question> questions;
106  std::vector<ResourceRecord> answers, authorities, additional;
108 
109  Query() : error(ERROR_NONE) { }
110  Query(const Question &q) : error(ERROR_NONE) { questions.push_back(q); }
111  };
112 
113  class ReplySocket;
114  class Request;
115 
118  class Manager : public Service
119  {
120  public:
121  Manager(Module *creator) : Service(creator, "DNS::Manager", "dns/manager") { }
122  virtual ~Manager() { }
123 
124  virtual void Process(Request *req) = 0;
125  virtual void RemoveRequest(Request *req) = 0;
126 
127  virtual bool HandlePacket(ReplySocket *s, const unsigned char *const data, int len, sockaddrs *from) = 0;
128 
129  virtual void UpdateSerial() = 0;
130  virtual void Notify(const Anope::string &zone) = 0;
131  virtual uint32_t GetSerial() const = 0;
132  };
133 
136  class Request : public Timer, public Question
137  {
139  public:
140  /* Use result cache if available */
141  bool use_cache;
142  /* Request id */
143  unsigned short id;
144  /* Creator of this request */
146 
147  Request(Manager *mgr, Module *c, const Anope::string &addr, QueryType qt, bool cache = false) : Timer(0), Question(addr, qt), manager(mgr),
148  use_cache(cache), id(0), creator(c) { }
149 
150  virtual ~Request()
151  {
152  manager->RemoveRequest(this);
153  }
154 
158  virtual void OnLookupComplete(const Query *r) = 0;
159 
163  virtual void OnError(const Query *r) { }
164 
168  void Tick(time_t) anope_override
169  {
170  Log(LOG_DEBUG_2) << "Resolver: timeout for query " << this->name;
171  Query rr(*this);
172  rr.error = ERROR_TIMEDOUT;
173  this->OnError(&rr);
174  }
175  };
176 
177 } // namespace DNS
178 
179 #endif // DNS_H
180 
181 
Error error
Definition: dns.h:107
void Tick(time_t) anope_override
Definition: dns.h:168
bool use_cache
Definition: dns.h:141
Definition: timers.h:18
virtual void UpdateSerial()=0
Manager * manager
Definition: dns.h:138
Request(Manager *mgr, Module *c, const Anope::string &addr, QueryType qt, bool cache=false)
Definition: dns.h:147
bool operator==(const Question &other) const
Definition: dns.h:82
virtual uint32_t GetSerial() const =0
unsigned short id
Definition: dns.h:143
time_t created
Definition: dns.h:97
virtual bool HandlePacket(ReplySocket *s, const unsigned char *const data, int len, sockaddrs *from)=0
Anope::string rdata
Definition: dns.h:96
Query()
Definition: dns.h:109
std::vector< ResourceRecord > authorities
Definition: dns.h:106
unsigned int ttl
Definition: dns.h:95
std::vector< Question > questions
Definition: dns.h:105
CoreExport time_t CurTime
Definition: main.cpp:41
Error
Definition: dns.h:57
Question()
Definition: dns.h:80
virtual void Process(Request *req)=0
ResourceRecord(const Question &q)
Definition: dns.h:100
virtual ~Request()
Definition: dns.h:150
Query(const Question &q)
Definition: dns.h:110
std::vector< ResourceRecord > additional
Definition: dns.h:106
virtual void OnError(const Query *r)
Definition: dns.h:163
unsigned short qclass
Definition: dns.h:78
size_t operator()(const Question &q) const
Definition: dns.h:86
virtual void Notify(const Anope::string &zone)=0
#define anope_override
Definition: services.h:56
virtual void RemoveRequest(Request *req)=0
Definition: defs.h:28
QueryType
Definition: dns.h:20
QueryType type
Definition: dns.h:77
Manager(Module *creator)
Definition: dns.h:121
Module * creator
Definition: dns.h:145
virtual void OnLookupComplete(const Query *r)=0
Anope::string name
Definition: dns.h:76
Definition: logger.h:53
Question(const Anope::string &n, QueryType t, unsigned short c=1)
Definition: dns.h:81
std::vector< ResourceRecord > answers
Definition: dns.h:106
Definition: anope.h:20
ResourceRecord(const Anope::string &n, QueryType t, unsigned short c=1)
Definition: dns.h:99
virtual ~Manager()
Definition: dns.h:122