InspIRCd  2.0
dns.h
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  * Copyright (C) 2009 Daniel De Graaf <[email protected]>
5  * Copyright (C) 2005-2008 Craig Edwards <[email protected]>
6  * Copyright (C) 2007 Dennis Friis <[email protected]>
7  *
8  * This file is part of InspIRCd. InspIRCd is free software: you can
9  * redistribute it and/or modify it under the terms of the GNU General Public
10  * License as published by the Free Software Foundation, version 2.
11  *
12  * This program is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
15  * details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program. If not, see <http://www.gnu.org/licenses/>.
19  */
20 
21 
22 /*
23 dns.h - dns library very very loosely based on
24 firedns, Copyright (C) 2002 Ian Gulliver
25 
26 This program is free software; you can redistribute it and/or modify
27 it under the terms of version 2 of the GNU General Public License as
28 published by the Free Software Foundation.
29 
30 This program is distributed in the hope that it will be useful,
31 but WITHOUT ANY WARRANTY; without even the implied warranty of
32 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
33 GNU General Public License for more details.
34 
35 You should have received a copy of the GNU General Public License
36 along with this program; if not, write to the Free Software
37 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
38 */
39 
40 #ifndef DNS_H
41 #define DNS_H
42 
43 #include "socket.h"
44 #include "hashcomp.h"
45 
49 enum QueryType
50 {
52  DNS_QUERY_NONE = 0,
54  DNS_QUERY_A = 1,
56  DNS_QUERY_CNAME = 5,
58  DNS_QUERY_PTR = 12,
60  DNS_QUERY_AAAA = 28,
61 
63  DNS_QUERY_PTR4 = 0xFFFD,
65  DNS_QUERY_PTR6 = 0xFFFE
66 };
67 
71 class CoreExport DNSResult
72 {
73  public:
76  int id;
79  std::string result;
82  unsigned long ttl;
85  std::string original;
88  QueryType type;
89 
97  DNSResult(int i, const std::string &res, unsigned long timetolive, const std::string &orig, QueryType qt = DNS_QUERY_NONE) : id(i), result(res), ttl(timetolive), original(orig), type(qt) { }
98 };
99 
103 typedef std::pair<unsigned char*, std::string> DNSInfo;
104 
107 class CoreExport CachedQuery
108 {
109  public:
112  std::string data;
115  QueryType type;
118  time_t expires;
119 
125  CachedQuery(const std::string &res, QueryType qt, unsigned int ttl);
126 
130  int CalcTTLRemaining();
131 };
132 
135 typedef nspace::hash_map<irc::string, CachedQuery, irc::hash> dnscache;
136 
140 enum ResolverError
141 {
142  RESOLVER_NOERROR = 0,
143  RESOLVER_NSDOWN = 1,
144  RESOLVER_NXDOMAIN = 2,
145  RESOLVER_BADIP = 3,
146  RESOLVER_TIMEOUT = 4,
147  RESOLVER_FORCEUNLOAD = 5
148 };
149 
154 enum ForceProtocol
155 {
157  PROTOCOL_IPV4 = 0,
159  PROTOCOL_IPV6 = 1
160 };
161 
172 class CoreExport Resolver
173 {
174  protected:
182  std::string input;
186  QueryType querytype;
191  std::string server;
197  int myid;
198 
203 
208 
209  public:
242  Resolver(const std::string &source, QueryType qt, bool &cached, Module* creator);
243 
247  virtual ~Resolver();
248 
257  virtual void OnLookupComplete(const std::string &result, unsigned int ttl, bool cached) = 0;
258 
265  virtual void OnError(ResolverError e, const std::string &errormessage);
266 
275  int GetId();
276 
280  Module* GetCreator();
281 
287  void TriggerCachedResult();
288 };
289 
295 class CoreExport DNS : public EventHandler
296 {
297  private:
298 
303  static const int MAX_REQUEST_ID = 0xFFFF;
304 
307  static const unsigned int MAX_CACHE_SIZE = 1000;
308 
312  dnscache* cache;
313 
317  class CacheTimer* PruneTimer;
318 
322  int MakePayload(const char* name, const QueryType rr, const unsigned short rr_class, unsigned char* payload);
323 
324  public:
325 
326  irc::sockets::sockaddrs myserver;
327 
331  Resolver* Classes[MAX_REQUEST_ID];
332 
336  DNSRequest* requests[MAX_REQUEST_ID];
337 
342  static const int QUERY_PORT = 53;
343 
347  static void FillResourceRecord(ResourceRecord* rr, const unsigned char* input);
348 
352  static void FillHeader(DNSHeader *header, const unsigned char *input, const int length);
353 
357  static void EmptyHeader(unsigned char *output, const DNSHeader *header, const int length);
358 
362  int GetIP(const char* name);
363 
369  int GetNameForce(const char *ip, ForceProtocol fp);
370 
374  int GetIP6(const char *name);
375 
379  int GetCName(const char* alias);
380 
385  DNSResult GetResult();
386 
391  void HandleEvent(EventType et, int errornum = 0);
392 
396  bool AddResolverClass(Resolver* r);
397 
401  DNSRequest* AddQuery(DNSHeader *header, int &id, const char* original);
402 
407  DNS();
408 
412  void Rehash();
413 
417  ~DNS();
418 
422  static void MakeIP6Int(char* query, const in6_addr *ip);
423 
429  void CleanResolvers(Module* module);
430 
436  CachedQuery* GetCache(const std::string &source);
437 
441  void DelCache(const std::string &source);
442 
445  int ClearCache();
446 
451  int PruneCache();
452 };
453 
454 #endif
455 
Definition: dns.cpp:118
Definition: dns.h:295
std::string server
Definition: dns.h:191
QueryType type
Definition: dns.h:88
std::string input
Definition: dns.h:182
unsigned long ttl
Definition: dns.h:82
Definition: dns.cpp:78
virtual void HandleEvent(EventType et, int errornum=0)=0
DNSResult(int i, const std::string &res, unsigned long timetolive, const std::string &orig, QueryType qt=DNS_QUERY_NONE)
Definition: dns.h:97
Definition: socket.h:58
std::string result
Definition: dns.h:79
Definition: dns.cpp:101
int myid
Definition: dns.h:197
CachedQuery * CQ
Definition: dns.h:202
Definition: dns.cpp:88
Definition: dns.h:71
Definition: modules.h:350
Definition: dns.h:107
QueryType type
Definition: dns.h:115
ModuleRef Creator
Definition: dns.h:178
std::string original
Definition: dns.h:85
Definition: socketengine.h:164
QueryType querytype
Definition: dns.h:186
time_t expires
Definition: dns.h:118
int id
Definition: dns.h:76
std::string data
Definition: dns.h:112
Definition: dns.h:172
int time_left
Definition: dns.h:207