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 */
36 /* IPv6 AAAA lookup */
40 /** Flags that can be AND'd into DNSPacket::flags to receive certain values
44 QUERYFLAGS_QR = 0x8000,
45 QUERYFLAGS_OPCODE = 0x7800,
46 QUERYFLAGS_AA = 0x400,
47 QUERYFLAGS_TC = 0x200,
48 QUERYFLAGS_RD = 0x100,
51 QUERYFLAGS_RCODE = 0xF
62 ERROR_NONSTANDARD_QUERY,
65 ERROR_DOMAIN_NOT_FOUND,
66 ERROR_NOT_IMPLEMENTED,
72 typedef uint16_t RequestId;
76 class Exception : public ModuleException
79 Exception(const std::string& message) : ModuleException(message) { }
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)); }
94 size_t operator()(const Question& question) const
96 return irc::insensitive()(question.name);
101 struct ResourceRecord : Question
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()) { }
114 std::vector<ResourceRecord> answers;
118 Query() : error(ERROR_NONE), cached(false) { }
119 Query(const Question& q) : question(q), error(ERROR_NONE), cached(false) { }
121 const ResourceRecord* FindAnswerOfType(QueryType qtype) const
123 for (std::vector<DNS::ResourceRecord>::const_iterator i = answers.begin(); i != answers.end(); ++i)
125 const DNS::ResourceRecord& rr = *i;
126 if (rr.type == qtype)
139 class Manager : public DataProvider
142 Manager(Module* mod) : DataProvider(mod, "DNS") { }
144 virtual void Process(Request* req) = 0;
145 virtual void RemoveRequest(Request* req) = 0;
146 virtual std::string GetErrorStr(Error) = 0;
151 class Request : public Timer
154 Manager* const manager;
157 /* Use result cache if available */
161 /* Creator of this request */
162 Module* const creator;
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))
168 , use_cache(usecache)
176 manager->RemoveRequest(this);
179 /** Called when this request succeeds
180 * @param r The query sent back from the nameserver
182 virtual void OnLookupComplete(const Query* req) = 0;
184 /** Called when this request fails or times out.
185 * @param r The query sent back from the nameserver, check the error code.
187 virtual void OnError(const Query* req) { }
189 /** Used to time out the query, calls OnError and asks the TimerManager
190 * to delete this request
192 bool Tick(time_t now)
194 Query rr(this->question);
195 rr.error = ERROR_TIMEDOUT;