]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - include/modules/dns.h
Fix various documentation comments.
[user/henk/code/inspircd.git] / include / modules / dns.h
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2017, 2019, 2021 Sadie Powell <sadie@witchery.services>
5  *   Copyright (C) 2014-2015 Attila Molnar <attilamolnar@hush.com>
6  *   Copyright (C) 2013, 2015-2016 Adam <Adam@anope.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 #pragma once
22
23 namespace DNS
24 {
25         /** Valid query types
26          */
27         enum QueryType
28         {
29                 /* Nothing */
30                 QUERY_NONE,
31                 /* A simple A lookup */
32                 QUERY_A = 1,
33                 /* A CNAME lookup */
34                 QUERY_CNAME = 5,
35                 /* Reverse DNS lookup */
36                 QUERY_PTR = 12,
37                 /* TXT */
38                 QUERY_TXT = 16,
39                 /* IPv6 AAAA lookup */
40                 QUERY_AAAA = 28
41         };
42
43         /** Flags that can be AND'd into DNSPacket::flags to receive certain values
44          */
45         enum
46         {
47                 QUERYFLAGS_QR = 0x8000,
48                 QUERYFLAGS_OPCODE = 0x7800,
49                 QUERYFLAGS_AA = 0x400,
50                 QUERYFLAGS_TC = 0x200,
51                 QUERYFLAGS_RD = 0x100,
52                 QUERYFLAGS_RA = 0x80,
53                 QUERYFLAGS_Z = 0x70,
54                 QUERYFLAGS_RCODE = 0xF
55         };
56
57         enum Error
58         {
59                 ERROR_NONE,
60                 ERROR_DISABLED,
61                 ERROR_UNKNOWN,
62                 ERROR_UNLOADED,
63                 ERROR_TIMEDOUT,
64                 ERROR_MALFORMED,
65                 ERROR_NOT_AN_ANSWER,
66                 ERROR_NONSTANDARD_QUERY,
67                 ERROR_FORMAT_ERROR,
68                 ERROR_SERVER_FAILURE,
69                 ERROR_DOMAIN_NOT_FOUND,
70                 ERROR_NOT_IMPLEMENTED,
71                 ERROR_REFUSED,
72                 ERROR_NO_RECORDS,
73                 ERROR_INVALIDTYPE
74         };
75
76         typedef uint16_t RequestId;
77
78         const int PORT = 53;
79
80         class Exception : public ModuleException
81         {
82          public:
83                 Exception(const std::string& message) : ModuleException(message) { }
84         };
85
86         struct Question
87         {
88                 std::string name;
89                 QueryType type;
90
91                 Question() : type(QUERY_NONE) { }
92                 Question(const std::string& n, QueryType t) : name(n), type(t) { }
93                 bool operator==(const Question& other) const { return ((name == other.name) && (type == other.type)); }
94                 bool operator!=(const Question& other) const { return (!(*this == other)); }
95
96                 struct hash
97                 {
98                         size_t operator()(const Question& question) const
99                         {
100                                 return irc::insensitive()(question.name);
101                         }
102                 };
103         };
104
105         struct ResourceRecord : Question
106         {
107                 unsigned int ttl;
108                 std::string rdata;
109                 time_t created;
110
111                 ResourceRecord(const std::string& n, QueryType t) : Question(n, t), ttl(0), created(ServerInstance->Time()) { }
112                 ResourceRecord(const Question& question) : Question(question), ttl(0), created(ServerInstance->Time()) { }
113         };
114
115         struct Query
116         {
117                 Question question;
118                 std::vector<ResourceRecord> answers;
119                 Error error;
120                 bool cached;
121
122                 Query() : error(ERROR_NONE), cached(false) { }
123                 Query(const Question& q) : question(q), error(ERROR_NONE), cached(false) { }
124
125                 const ResourceRecord* FindAnswerOfType(QueryType qtype) const
126                 {
127                         for (std::vector<DNS::ResourceRecord>::const_iterator i = answers.begin(); i != answers.end(); ++i)
128                         {
129                                 const DNS::ResourceRecord& rr = *i;
130                                 if (rr.type == qtype)
131                                         return &rr;
132                         }
133
134                         return NULL;
135                 }
136         };
137
138         class ReplySocket;
139         class Request;
140
141         /** DNS manager
142          */
143         class Manager : public DataProvider
144         {
145          public:
146                 Manager(Module* mod) : DataProvider(mod, "DNS") { }
147
148                 virtual void Process(Request* req) = 0;
149                 virtual void RemoveRequest(Request* req) = 0;
150                 virtual std::string GetErrorStr(Error) = 0;
151                 virtual std::string GetTypeStr(QueryType) = 0;
152         };
153
154         /** A DNS query.
155          */
156         class Request : public Timer
157         {
158          protected:
159                 Manager* const manager;
160          public:
161                 Question question;
162                 /* Use result cache if available */
163                 bool use_cache;
164                 /* Request id */
165                 RequestId id;
166                 /* Creator of this request */
167                 Module* const creator;
168
169                 Request(Manager* mgr, Module* mod, const std::string& addr, QueryType qt, bool usecache = true)
170                         : Timer(ServerInstance->Config->ConfValue("dns")->getDuration("timeout", 5, 1))
171                         , manager(mgr)
172                         , question(addr, qt)
173                         , use_cache(usecache)
174                         , id(0)
175                         , creator(mod)
176                 {
177                 }
178
179                 virtual ~Request()
180                 {
181                         manager->RemoveRequest(this);
182                 }
183
184                 /** Called when this request succeeds
185                  * @param req The query sent back from the nameserver
186                  */
187                 virtual void OnLookupComplete(const Query* req) = 0;
188
189                 /** Called when this request fails or times out.
190                  * @param req The query sent back from the nameserver, check the error code.
191                  */
192                 virtual void OnError(const Query* req) { }
193
194                 /** Used to time out the query, calls OnError and asks the TimerManager
195                  * to delete this request
196                  */
197                 bool Tick(time_t now) CXX11_OVERRIDE
198                 {
199                         Query rr(this->question);
200                         rr.error = ERROR_TIMEDOUT;
201                         this->OnError(&rr);
202                         delete this;
203                         return false;
204                 }
205         };
206
207 } // namespace DNS