]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - include/modules/dns.h
Fix some minor problems with the filter docs.
[user/henk/code/inspircd.git] / include / modules / dns.h
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2013 Adam <Adam@anope.org>
5  *   Copyright (C) 2003-2013 Anope Team <team@anope.org>
6  *
7  * This file is part of InspIRCd.  InspIRCd is free software: you can
8  * redistribute it and/or modify it under the terms of the GNU General Public
9  * License as published by the Free Software Foundation, version 2.
10  *
11  * This program is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
14  * details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 #pragma once
21
22 namespace DNS
23 {
24         /** Valid query types
25          */
26         enum QueryType
27         {
28                 /* Nothing */
29                 QUERY_NONE,
30                 /* A simple A lookup */
31                 QUERY_A = 1,
32                 /* A CNAME lookup */
33                 QUERY_CNAME = 5,
34                 /* Reverse DNS lookup */
35                 QUERY_PTR = 12,
36                 /* IPv6 AAAA lookup */
37                 QUERY_AAAA = 28
38         };
39
40         /** Flags that can be AND'd into DNSPacket::flags to receive certain values
41          */
42         enum
43         {
44                 QUERYFLAGS_QR = 0x8000,
45                 QUERYFLAGS_OPCODE = 0x7800,
46                 QUERYFLAGS_AA = 0x400,
47                 QUERYFLAGS_TC = 0x200,
48                 QUERYFLAGS_RD = 0x100,
49                 QUERYFLAGS_RA = 0x80,
50                 QUERYFLAGS_Z = 0x70,
51                 QUERYFLAGS_RCODE = 0xF
52         };
53
54         enum Error
55         {
56                 ERROR_NONE,
57                 ERROR_UNKNOWN,
58                 ERROR_UNLOADED,
59                 ERROR_TIMEDOUT,
60                 ERROR_MALFORMED,
61                 ERROR_NOT_AN_ANSWER,
62                 ERROR_NONSTANDARD_QUERY,
63                 ERROR_FORMAT_ERROR,
64                 ERROR_SERVER_FAILURE,
65                 ERROR_DOMAIN_NOT_FOUND,
66                 ERROR_NOT_IMPLEMENTED,
67                 ERROR_REFUSED,
68                 ERROR_NO_RECORDS,
69                 ERROR_INVALIDTYPE
70         };
71
72         typedef uint16_t RequestId;
73
74         const int PORT = 53;
75
76         class Exception : public ModuleException
77         {
78          public:
79                 Exception(const std::string& message) : ModuleException(message) { }
80         };
81
82         struct Question
83         {
84                 std::string name;
85                 QueryType type;
86
87                 Question() : type(QUERY_NONE) { }
88                 Question(const std::string& n, QueryType t) : name(n), type(t) { }
89                 bool operator==(const Question& other) const { return ((name == other.name) && (type == other.type)); }
90                 bool operator!=(const Question& other) const { return (!(*this == other)); }
91
92                 struct hash
93                 {
94                         size_t operator()(const Question& question) const
95                         {
96                                 return irc::insensitive()(question.name);
97                         }
98                 };
99         };
100
101         struct ResourceRecord : Question
102         {
103                 unsigned int ttl;
104                 std::string rdata;
105                 time_t created;
106
107                 ResourceRecord(const std::string& n, QueryType t) : Question(n, t), ttl(0), created(ServerInstance->Time()) { }
108                 ResourceRecord(const Question& question) : Question(question), ttl(0), created(ServerInstance->Time()) { }
109         };
110
111         struct Query
112         {
113                 Question question;
114                 std::vector<ResourceRecord> answers;
115                 Error error;
116                 bool cached;
117
118                 Query() : error(ERROR_NONE), cached(false) { }
119                 Query(const Question& q) : question(q), error(ERROR_NONE), cached(false) { }
120
121                 const ResourceRecord* FindAnswerOfType(QueryType qtype) const
122                 {
123                         for (std::vector<DNS::ResourceRecord>::const_iterator i = answers.begin(); i != answers.end(); ++i)
124                         {
125                                 const DNS::ResourceRecord& rr = *i;
126                                 if (rr.type == qtype)
127                                         return &rr;
128                         }
129
130                         return NULL;
131                 }
132         };
133
134         class ReplySocket;
135         class Request;
136
137         /** DNS manager
138          */
139         class Manager : public DataProvider
140         {
141          public:
142                 Manager(Module* mod) : DataProvider(mod, "DNS") { }
143
144                 virtual void Process(Request* req) = 0;
145                 virtual void RemoveRequest(Request* req) = 0;
146                 virtual std::string GetErrorStr(Error) = 0;
147         };
148
149         /** A DNS query.
150          */
151         class Request : public Timer
152         {
153          protected:
154                 Manager* const manager;
155          public:
156                 Question question;
157                 /* Use result cache if available */
158                 bool use_cache;
159                 /* Request id */
160                 RequestId id;
161                 /* Creator of this request */
162                 Module* const creator;
163
164                 Request(Manager* mgr, Module* mod, const std::string& addr, QueryType qt, bool usecache = true)
165                         : Timer((ServerInstance->Config->dns_timeout ? ServerInstance->Config->dns_timeout : 5))
166                         , manager(mgr)
167                         , question(addr, qt)
168                         , use_cache(usecache)
169                         , id(0)
170                         , creator(mod)
171                 {
172                 }
173
174                 virtual ~Request()
175                 {
176                         manager->RemoveRequest(this);
177                 }
178
179                 /** Called when this request succeeds
180                  * @param r The query sent back from the nameserver
181                  */
182                 virtual void OnLookupComplete(const Query* req) = 0;
183
184                 /** Called when this request fails or times out.
185                  * @param r The query sent back from the nameserver, check the error code.
186                  */
187                 virtual void OnError(const Query* req) { }
188
189                 /** Used to time out the query, calls OnError and asks the TimerManager
190                  * to delete this request
191                  */
192                 bool Tick(time_t now)
193                 {
194                         Query rr(this->question);
195                         rr.error = ERROR_TIMEDOUT;
196                         this->OnError(&rr);
197                         delete this;
198                         return false;
199                 }
200         };
201
202 } // namespace DNS