3 #include "DNS-iostream.hpp"
13 #include <arpa/nameser.h>
15 #include <experimental/random>
17 #include <glog/logging.h>
21 DEFINE_bool(log_dns_data
, false, "log all DNS TCP protocol data");
22 DEFINE_bool(random_dns_servers
, false, "Pick starting DNS server at random");
25 // The default timeout in glibc is 5 seconds. My setup with unbound
26 // in front of stubby with DNSSEC checking and all that seems to work
27 // better with just a little more time.
29 auto constexpr read_timeout
{std::chrono::seconds(7)};
31 enum class sock_type
: bool { stream
, dgram
};
34 char const* host
; // name used to match cert
40 constexpr nameserver nameservers
[]{
81 "2001:4860:4860::8888",
87 "2001:4860:4860::8844",
92 "1dot1dot1dot1.cloudflare-dns.com",
98 "1dot1dot1dot1.cloudflare-dns.com",
99 "2606:4700:4700::1111",
104 "1dot1dot1dot1.cloudflare-dns.com",
105 "2606:4700:4700::1001",
135 } // namespace Config
137 template <typename T
, std::size_t N
>
138 constexpr std::size_t countof(T
const (&)[N
]) noexcept
145 Resolver::Resolver(fs::path config_path
)
147 auto tries
= countof(Config::nameservers
);
149 if (FLAGS_random_dns_servers
) {
150 ns_
= std::experimental::randint(
151 0, static_cast<int>(countof(Config::nameservers
) - 1));
154 ns_
= static_cast<int>(countof(Config::nameservers
) - 1);
159 // try the next one, with wrap
160 if (++ns_
== countof(Config::nameservers
))
163 auto const& nameserver
= Config::nameservers
[ns_
];
164 auto typ
= (nameserver
.typ
== Config::sock_type::stream
) ? SOCK_STREAM
168 osutil::get_port(nameserver
.port
, (typ
== SOCK_STREAM
) ? "tcp" : "udp");
171 if (IP4::is_address(nameserver
.addr
)) {
172 ns_fd_
= socket(AF_INET
, typ
, 0);
173 PCHECK(ns_fd_
>= 0) << "socket() failed";
175 auto in4
{sockaddr_in
{}};
176 in4
.sin_family
= AF_INET
;
177 in4
.sin_port
= htons(port
);
178 CHECK_EQ(inet_pton(AF_INET
, nameserver
.addr
,
179 reinterpret_cast<void*>(&in4
.sin_addr
)),
181 if (connect(ns_fd_
, reinterpret_cast<const sockaddr
*>(&in4
),
183 PLOG(INFO
) << "connect failed " << nameserver
.host
<< '['
184 << nameserver
.addr
<< "]:" << nameserver
.port
;
190 else if (IP6::is_address(nameserver
.addr
)) {
191 ns_fd_
= socket(AF_INET6
, typ
, 0);
192 PCHECK(ns_fd_
>= 0) << "socket() failed";
194 auto in6
{sockaddr_in6
{}};
195 in6
.sin6_family
= AF_INET6
;
196 in6
.sin6_port
= htons(port
);
197 CHECK_EQ(inet_pton(AF_INET6
, nameserver
.addr
,
198 reinterpret_cast<void*>(&in6
.sin6_addr
)),
200 if (connect(ns_fd_
, reinterpret_cast<const sockaddr
*>(&in6
),
202 PLOG(INFO
) << "connect failed " << nameserver
.host
<< '['
203 << nameserver
.addr
<< "]:" << nameserver
.port
;
210 POSIX::set_nonblocking(ns_fd_
);
212 if (nameserver
.typ
== Config::sock_type::stream
) {
213 ns_sock_
= std::make_unique
<Sock
>(ns_fd_
, ns_fd_
);
214 if (FLAGS_log_dns_data
) {
215 ns_sock_
->log_data_on();
218 ns_sock_
->log_data_off();
222 DNS::RR_collection tlsa_rrs
; // empty FIXME!
223 ns_sock_
->starttls_client(config_path
, nullptr, nameserver
.host
,
225 if (ns_sock_
->verified()) {
239 LOG(FATAL
) << "no nameservers left to try";
242 message
Resolver::xchg(message
const& q
)
244 if (Config::nameservers
[ns_
].typ
== Config::sock_type::stream
) {
245 CHECK_EQ(ns_fd_
, -1);
247 uint16_t sz
= htons(std::size(q
));
249 ns_sock_
->out().write(reinterpret_cast<char const*>(&sz
), sizeof sz
);
250 ns_sock_
->out().write(reinterpret_cast<char const*>(begin(q
)), size(q
));
251 ns_sock_
->out().flush();
254 ns_sock_
->in().read(reinterpret_cast<char*>(&sz
), sizeof sz
);
257 DNS::message::container_t
bfr(sz
);
258 ns_sock_
->in().read(reinterpret_cast<char*>(bfr
.data()), sz
);
259 CHECK_EQ(ns_sock_
->in().gcount(), std::streamsize(sz
));
261 if (!ns_sock_
->in()) {
262 LOG(WARNING
) << "Resolver::xchg was able to read only "
263 << ns_sock_
->in().gcount() << " octets";
266 return message
{std::move(bfr
)};
269 CHECK(Config::nameservers
[ns_
].typ
== Config::sock_type::dgram
);
272 CHECK_EQ(send(ns_fd_
, std::begin(q
), std::size(q
), 0), std::size(q
));
274 DNS::message::container_t
bfr(Config::max_udp_sz
);
276 auto constexpr hook
{[]() {}};
278 auto const a_buf
= reinterpret_cast<char*>(bfr
.data());
279 auto const a_buflen
= POSIX::read(ns_fd_
, a_buf
, int(Config::max_udp_sz
),
280 hook
, Config::read_timeout
, t_o
);
283 LOG(WARNING
) << "DNS read failed";
288 LOG(WARNING
) << "DNS read timed out";
292 bfr
.resize(a_buflen
);
295 return message
{std::move(bfr
)};
298 RR_collection
Resolver::get_records(RR_type typ
, char const* name
)
300 Query
q(*this, typ
, name
);
301 return q
.get_records();
304 std::vector
<std::string
> Resolver::get_strings(RR_type typ
, char const* name
)
306 Query
q(*this, typ
, name
);
307 return q
.get_strings();
310 bool Query::xchg_(Resolver
& res
, uint16_t id
)
319 bogus_or_indeterminate_
= true;
320 LOG(WARNING
) << "no reply from nameserver";
324 if (size(a_
) < min_message_sz()) {
325 bogus_or_indeterminate_
= true;
326 LOG(WARNING
) << "packet too small";
333 LOG(WARNING
) << "packet out of order; ids don't match, got " << a_
.id()
334 << " expecting " << id
;
341 bogus_or_indeterminate_
= true;
342 LOG(WARNING
) << "no tries left, giving up";
347 Query::Query(Resolver
& res
, RR_type type
, char const* name
)
350 static_assert(std::numeric_limits
<uint16_t>::min() == 0);
351 static_assert(std::numeric_limits
<uint16_t>::max() == 65535);
354 std::experimental::randint(std::numeric_limits
<uint16_t>::min(),
355 std::numeric_limits
<uint16_t>::max());
357 uint16_t cls
= ns_c_in
;
359 q_
= create_question(name
, type
, cls
, id
);
364 if (size(a_
) < min_message_sz()) {
365 bogus_or_indeterminate_
= true;
366 LOG(INFO
) << "bad (or no) reply for " << name
<< '/' << type
;
370 check_answer(nx_domain_
, bogus_or_indeterminate_
, rcode_
, extended_rcode_
,
371 truncation_
, authentic_data_
, has_record_
, q_
, a_
, type
, name
);
374 // if UDP, retry with TCP
375 bogus_or_indeterminate_
= true;
376 LOG(INFO
) << "truncated answer for " << name
<< '/' << type
;
380 RR_collection
Query::get_records()
382 if (bogus_or_indeterminate_
)
383 return RR_collection
{};
385 return DNS::get_records(a_
, bogus_or_indeterminate_
);
388 std::vector
<std::string
> Query::get_strings()
390 std::vector
<std::string
> ret
;
392 auto const rr_set
= get_records();
394 for (auto rr
: rr_set
) {
396 [&ret
, type
= type_
](auto const& r
) {
397 if (type
== r
.rr_type()) {
398 auto const s
= r
.as_str();