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
61 ERROR_NONSTANDARD_QUERY,
64 ERROR_DOMAIN_NOT_FOUND,
65 ERROR_NOT_IMPLEMENTED,
71 typedef uint16_t RequestId;
76 * The maximum value of a dns request id,
77 * 16 bits wide, 0xFFFF.
79 const int MAX_REQUEST_ID = 0xFFFF;
81 class Exception : public ModuleException
84 Exception(const std::string& message) : ModuleException(message) { }
92 Question() : type(QUERY_NONE) { }
93 Question(const std::string& n, QueryType t) : name(n), type(t) { }
94 bool operator==(const Question& other) const { return ((name == other.name) && (type == other.type)); }
98 size_t operator()(const Question& question) const
100 return irc::insensitive()(question.name);
105 struct ResourceRecord : Question
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()) { }
118 std::vector<ResourceRecord> answers;
122 Query() : error(ERROR_NONE), cached(false) { }
123 Query(const Question& q) : question(q), error(ERROR_NONE), cached(false) { }
131 class Manager : public DataProvider
134 Manager(Module* mod) : DataProvider(mod, "DNS") { }
136 virtual void Process(Request* req) = 0;
137 virtual void RemoveRequest(Request* req) = 0;
138 virtual std::string GetErrorStr(Error) = 0;
143 class Request : public Timer, public Question
146 Manager* const manager;
148 /* Use result cache if available */
152 /* Creator of this request */
153 Module* const creator;
155 Request(Manager* mgr, Module* mod, const std::string& addr, QueryType qt, bool usecache = true)
156 : Timer((ServerInstance->Config->dns_timeout ? ServerInstance->Config->dns_timeout : 5))
159 , use_cache(usecache)
163 ServerInstance->Timers.AddTimer(this);
168 manager->RemoveRequest(this);
171 /** Called when this request succeeds
172 * @param r The query sent back from the nameserver
174 virtual void OnLookupComplete(const Query* req) = 0;
176 /** Called when this request fails or times out.
177 * @param r The query sent back from the nameserver, check the error code.
179 virtual void OnError(const Query* req) { }
181 /** Used to time out the query, calls OnError and asks the TimerManager
182 * to delete this request
184 bool Tick(time_t now)
187 rr.error = ERROR_TIMEDOUT;