2 * InspIRCd -- Internet Relay Chat Daemon
4 * Copyright (C) 2013 Adam <Adam@anope.org>
5 * Copyright (C) 2003-2013 Anope Team <team@anope.org>
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.
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
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/>.
30 /* A simple A lookup */
34 /* Reverse DNS lookup */
38 /* IPv6 AAAA lookup */
42 /** Flags that can be AND'd into DNSPacket::flags to receive certain values
46 QUERYFLAGS_QR = 0x8000,
47 QUERYFLAGS_OPCODE = 0x7800,
48 QUERYFLAGS_AA = 0x400,
49 QUERYFLAGS_TC = 0x200,
50 QUERYFLAGS_RD = 0x100,
53 QUERYFLAGS_RCODE = 0xF
64 ERROR_NONSTANDARD_QUERY,
67 ERROR_DOMAIN_NOT_FOUND,
68 ERROR_NOT_IMPLEMENTED,
74 typedef uint16_t RequestId;
78 class Exception : public ModuleException
81 Exception(const std::string& message) : ModuleException(message) { }
89 Question() : type(QUERY_NONE) { }
90 Question(const std::string& n, QueryType t) : name(n), type(t) { }
91 bool operator==(const Question& other) const { return ((name == other.name) && (type == other.type)); }
92 bool operator!=(const Question& other) const { return (!(*this == other)); }
96 size_t operator()(const Question& question) const
98 return irc::insensitive()(question.name);
103 struct ResourceRecord : Question
109 ResourceRecord(const std::string& n, QueryType t) : Question(n, t), ttl(0), created(ServerInstance->Time()) { }
110 ResourceRecord(const Question& question) : Question(question), ttl(0), created(ServerInstance->Time()) { }
116 std::vector<ResourceRecord> answers;
120 Query() : error(ERROR_NONE), cached(false) { }
121 Query(const Question& q) : question(q), error(ERROR_NONE), cached(false) { }
123 const ResourceRecord* FindAnswerOfType(QueryType qtype) const
125 for (std::vector<DNS::ResourceRecord>::const_iterator i = answers.begin(); i != answers.end(); ++i)
127 const DNS::ResourceRecord& rr = *i;
128 if (rr.type == qtype)
141 class Manager : public DataProvider
144 Manager(Module* mod) : DataProvider(mod, "DNS") { }
146 virtual void Process(Request* req) = 0;
147 virtual void RemoveRequest(Request* req) = 0;
148 virtual std::string GetErrorStr(Error) = 0;
153 class Request : public Timer
156 Manager* const manager;
159 /* Use result cache if available */
163 /* Creator of this request */
164 Module* const creator;
166 Request(Manager* mgr, Module* mod, const std::string& addr, QueryType qt, bool usecache = true)
167 : Timer(ServerInstance->Config->ConfValue("dns")->getDuration("timeout", 5, 1))
170 , use_cache(usecache)
178 manager->RemoveRequest(this);
181 /** Called when this request succeeds
182 * @param r The query sent back from the nameserver
184 virtual void OnLookupComplete(const Query* req) = 0;
186 /** Called when this request fails or times out.
187 * @param r The query sent back from the nameserver, check the error code.
189 virtual void OnError(const Query* req) { }
191 /** Used to time out the query, calls OnError and asks the TimerManager
192 * to delete this request
194 bool Tick(time_t now) CXX11_OVERRIDE
196 Query rr(this->question);
197 rr.error = ERROR_TIMEDOUT;