InspIRCd  2.0
 All Classes Namespaces Functions Variables Typedefs Enumerations
dns.h
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  * Copyright (C) 2009 Daniel De Graaf <danieldg@inspircd.org>
5  * Copyright (C) 2005-2008 Craig Edwards <craigedwards@brainbox.cc>
6  * Copyright (C) 2007 Dennis Friis <peavey@inspircd.org>
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 class CoreExport DNSResult
50 {
51  public:
54  int id;
57  std::string result;
60  unsigned long ttl;
63  std::string original;
64 
71  DNSResult(int i, const std::string &res, unsigned long timetolive, const std::string &orig) : id(i), result(res), ttl(timetolive), original(orig) { }
72 };
73 
77 typedef std::pair<unsigned char*, std::string> DNSInfo;
78 
81 class CoreExport CachedQuery
82 {
83  public:
86  std::string data;
89  time_t expires;
90 
95  CachedQuery(const std::string &res, unsigned int ttl);
96 
100  int CalcTTLRemaining();
101 };
102 
105 typedef nspace::hash_map<irc::string, CachedQuery, irc::hash> dnscache;
106 
110 enum ResolverError
111 {
112  RESOLVER_NOERROR = 0,
113  RESOLVER_NSDOWN = 1,
114  RESOLVER_NXDOMAIN = 2,
115  RESOLVER_BADIP = 3,
116  RESOLVER_TIMEOUT = 4,
117  RESOLVER_FORCEUNLOAD = 5
118 };
119 
123 enum QueryType
124 {
126  DNS_QUERY_NONE = 0,
128  DNS_QUERY_A = 1,
130  DNS_QUERY_CNAME = 5,
132  DNS_QUERY_PTR = 12,
134  DNS_QUERY_AAAA = 28,
135 
137  DNS_QUERY_PTR4 = 0xFFFD,
139  DNS_QUERY_PTR6 = 0xFFFE
140 };
141 
146 enum ForceProtocol
147 {
149  PROTOCOL_IPV4 = 0,
151  PROTOCOL_IPV6 = 1
152 };
153 
164 class CoreExport Resolver
165 {
166  protected:
174  std::string input;
178  QueryType querytype;
183  std::string server;
189  int myid;
190 
195 
200 
201  public:
234  Resolver(const std::string &source, QueryType qt, bool &cached, Module* creator);
235 
239  virtual ~Resolver();
240 
249  virtual void OnLookupComplete(const std::string &result, unsigned int ttl, bool cached) = 0;
250 
257  virtual void OnError(ResolverError e, const std::string &errormessage);
258 
267  int GetId();
268 
272  Module* GetCreator();
273 
279  void TriggerCachedResult();
280 };
281 
287 class CoreExport DNS : public EventHandler
288 {
289  private:
290 
295  static const int MAX_REQUEST_ID = 0xFFFF;
296 
300  dnscache* cache;
301 
305  class CacheTimer* PruneTimer;
306 
310  int MakePayload(const char* name, const QueryType rr, const unsigned short rr_class, unsigned char* payload);
311 
312  public:
313 
314  irc::sockets::sockaddrs myserver;
315 
319  Resolver* Classes[MAX_REQUEST_ID];
320 
324  DNSRequest* requests[MAX_REQUEST_ID];
325 
330  static const int QUERY_PORT = 53;
331 
335  static void FillResourceRecord(ResourceRecord* rr, const unsigned char* input);
336 
340  static void FillHeader(DNSHeader *header, const unsigned char *input, const int length);
341 
345  static void EmptyHeader(unsigned char *output, const DNSHeader *header, const int length);
346 
350  int GetIP(const char* name);
351 
357  int GetNameForce(const char *ip, ForceProtocol fp);
358 
362  int GetIP6(const char *name);
363 
367  int GetCName(const char* alias);
368 
373  DNSResult GetResult();
374 
379  void HandleEvent(EventType et, int errornum = 0);
380 
384  bool AddResolverClass(Resolver* r);
385 
389  DNSRequest* AddQuery(DNSHeader *header, int &id, const char* original);
390 
395  DNS();
396 
400  void Rehash();
401 
405  ~DNS();
406 
410  static void MakeIP6Int(char* query, const in6_addr *ip);
411 
417  void CleanResolvers(Module* module);
418 
424  CachedQuery* GetCache(const std::string &source);
425 
429  void DelCache(const std::string &source);
430 
433  int ClearCache();
434 
439  int PruneCache();
440 };
441 
442 #endif
443 
Definition: dns.cpp:118
Definition: dns.h:287
std::string server
Definition: dns.h:183
std::string input
Definition: dns.h:174
unsigned long ttl
Definition: dns.h:60
Definition: dns.cpp:78
virtual void HandleEvent(EventType et, int errornum=0)=0
Definition: socket.h:58
std::string result
Definition: dns.h:57
Definition: dns.cpp:101
int myid
Definition: dns.h:189
CachedQuery * CQ
Definition: dns.h:194
Definition: dns.cpp:88
Definition: dns.h:49
Definition: modules.h:350
Definition: dns.h:81
ModuleRef Creator
Definition: dns.h:170
std::string original
Definition: dns.h:63
Definition: socketengine.h:164
QueryType querytype
Definition: dns.h:178
DNSResult(int i, const std::string &res, unsigned long timetolive, const std::string &orig)
Definition: dns.h:71
time_t expires
Definition: dns.h:89
int id
Definition: dns.h:54
std::string data
Definition: dns.h:86
Definition: dns.h:164
int time_left
Definition: dns.h:199