3 #include "DNS-iostream.hpp"
13 #include <arpa/nameser.h>
15 #include <glog/logging.h>
19 DEFINE_bool(log_dns_data
, false, "log all DNS TCP protocol data");
20 DEFINE_bool(random_dns_servers
, false, "Pick starting DNS server at random");
23 // The default timeout in glibc is 5 seconds. My setup with unbound
24 // in front of stubby with DNSSEC checking and all that seems to work
25 // better with just a little more time.
27 auto constexpr read_timeout
{std::chrono::seconds(11)};
28 auto constexpr write_timeout
{std::chrono::seconds(1)};
30 enum class sock_type
: bool { stream
, dgram
};
33 char const* host
; // name used to match cert
39 constexpr nameserver nameservers
[]{
74 "2001:4860:4860::8888",
80 "2001:4860:4860::8844",
85 "1dot1dot1dot1.cloudflare-dns.com",
91 "1dot1dot1dot1.cloudflare-dns.com",
92 "2606:4700:4700::1111",
97 "1dot1dot1dot1.cloudflare-dns.com",
98 "2606:4700:4700::1001",
128 } // namespace Config
130 template <typename T
, std::size_t N
>
131 constexpr std::size_t countof(T
const (&)[N
]) noexcept
138 Resolver::Resolver(fs::path config_path
)
140 auto tries
= countof(Config::nameservers
);
142 if (FLAGS_random_dns_servers
) {
143 std::uniform_int_distribution
<int> uniform_dist(
144 0, countof(Config::nameservers
) - 1);
145 ns_
= uniform_dist(rng_
);
148 ns_
= static_cast<int>(countof(Config::nameservers
) - 1);
153 // try the next one, with wrap
154 if (++ns_
== countof(Config::nameservers
))
157 auto const& nameserver
= Config::nameservers
[ns_
];
158 auto typ
= (nameserver
.typ
== Config::sock_type::stream
) ? SOCK_STREAM
162 osutil::get_port(nameserver
.port
, (typ
== SOCK_STREAM
) ? "tcp" : "udp");
165 if (IP4::is_address(nameserver
.addr
)) {
166 ns_fd_
= socket(AF_INET
, typ
, 0);
167 PCHECK(ns_fd_
>= 0) << "socket() failed";
169 auto in4
{sockaddr_in
{}};
170 in4
.sin_family
= AF_INET
;
171 in4
.sin_port
= htons(port
);
172 CHECK_EQ(inet_pton(AF_INET
, nameserver
.addr
,
173 reinterpret_cast<void*>(&in4
.sin_addr
)),
175 if (connect(ns_fd_
, reinterpret_cast<const sockaddr
*>(&in4
),
177 PLOG(INFO
) << "connect failed " << nameserver
.host
<< '['
178 << nameserver
.addr
<< "]:" << nameserver
.port
;
184 else if (IP6::is_address(nameserver
.addr
)) {
185 ns_fd_
= socket(AF_INET6
, typ
, 0);
186 PCHECK(ns_fd_
>= 0) << "socket() failed";
188 auto in6
{sockaddr_in6
{}};
189 in6
.sin6_family
= AF_INET6
;
190 in6
.sin6_port
= htons(port
);
191 CHECK_EQ(inet_pton(AF_INET6
, nameserver
.addr
,
192 reinterpret_cast<void*>(&in6
.sin6_addr
)),
194 if (connect(ns_fd_
, reinterpret_cast<const sockaddr
*>(&in6
),
196 PLOG(INFO
) << "connect failed " << nameserver
.host
<< '['
197 << nameserver
.addr
<< "]:" << nameserver
.port
;
204 POSIX::set_nonblocking(ns_fd_
);
206 if (nameserver
.typ
== Config::sock_type::stream
) {
207 ns_sock_
= std::make_unique
<Sock
>(ns_fd_
, ns_fd_
);
208 if (FLAGS_log_dns_data
) {
209 ns_sock_
->log_data_on();
212 ns_sock_
->log_data_off();
216 DNS::RR_collection tlsa_rrs
; // empty FIXME!
217 ns_sock_
->starttls_client(config_path
, nullptr, nameserver
.host
,
219 if (ns_sock_
->verified()) {
233 LOG(FATAL
) << "no nameservers left to try";
236 message
Resolver::xchg(message
const& q
)
238 if (Config::nameservers
[ns_
].typ
== Config::sock_type::stream
) {
239 CHECK_EQ(ns_fd_
, -1);
241 auto const sp
= static_cast<std::span
<DNS::message::octet
const>>(q
);
243 uint16_t sz
= sp
.size();
246 ns_sock_
->out().write(reinterpret_cast<char const*>(&sz
), sizeof sz
);
247 ns_sock_
->out().write(reinterpret_cast<char const*>(sp
.data()), sp
.size());
248 ns_sock_
->out().flush();
251 ns_sock_
->in().read(reinterpret_cast<char*>(&sz
), sizeof sz
);
254 DNS::message::container_t
bfr(sz
);
255 ns_sock_
->in().read(reinterpret_cast<char*>(bfr
.data()), sz
);
256 CHECK_EQ(ns_sock_
->in().gcount(), std::streamsize(sz
));
258 if (!ns_sock_
->in()) {
259 LOG(WARNING
) << "Resolver::xchg was able to read only "
260 << ns_sock_
->in().gcount() << " octets";
263 return message
{std::move(bfr
)};
266 CHECK(Config::nameservers
[ns_
].typ
== Config::sock_type::dgram
);
271 auto const sp
= static_cast<std::span
<DNS::message::octet
const>>(q
);
274 POSIX::write(ns_fd_
, reinterpret_cast<char const*>(sp
.data()), sp
.size(),
275 Config::write_timeout
, t_o
);
276 if (wrlen
!= std::streamsize(sp
.size())) {
277 LOG(WARNING
) << "DNS write failed";
281 LOG(WARNING
) << "DNS write timed out";
285 DNS::message::container_t
bfr(Config::max_udp_sz
);
287 auto constexpr hook
{[]() {}};
288 auto const a_rdlen
= POSIX::read(ns_fd_
, reinterpret_cast<char*>(bfr
.data()),
289 bfr
.size(), hook
, Config::read_timeout
, t_o
);
291 LOG(WARNING
) << "DNS read failed";
295 LOG(WARNING
) << "DNS read timed out";
299 bfr
.resize(a_rdlen
); // down from max_udp_sz
302 return message
{std::move(bfr
)};
305 RR_collection
Resolver::get_records(RR_type typ
, char const* name
)
307 Query
q(*this, typ
, name
);
308 return q
.get_records();
311 std::vector
<std::string
> Resolver::get_strings(RR_type typ
, char const* name
)
313 Query
q(*this, typ
, name
);
314 return q
.get_strings();
317 bool Query::xchg_(Resolver
& res
, uint16_t id
)
325 auto const a_sp
= static_cast<std::span
<DNS::message::octet
const>>(a_
);
328 bogus_or_indeterminate_
= true;
329 LOG(WARNING
) << "no reply from nameserver";
333 if (a_sp
.size() < message::min_sz()) {
334 bogus_or_indeterminate_
= true;
335 LOG(WARNING
) << "packet too small";
342 LOG(WARNING
) << "packet out of order; ids don't match, got " << a_
.id()
343 << " expecting " << id
;
350 bogus_or_indeterminate_
= true;
351 LOG(WARNING
) << "no tries left, giving up";
356 Query::Query(Resolver
& res
, RR_type type
, char const* name
)
359 uint16_t id
= res
.rnd_id();
360 uint16_t cls
= ns_c_in
;
362 q_
= create_question(name
, type
, cls
, id
);
367 auto const a_sp
= static_cast<std::span
<DNS::message::octet
const>>(a_
);
369 if (a_sp
.size() < message::min_sz()) {
370 bogus_or_indeterminate_
= true;
371 LOG(INFO
) << "bad (or no) reply for " << name
<< '/' << type
;
375 check_answer(nx_domain_
, bogus_or_indeterminate_
, rcode_
, extended_rcode_
,
376 truncation_
, authentic_data_
, has_record_
, q_
, a_
, type
, name
);
379 // if UDP, retry with TCP
380 bogus_or_indeterminate_
= true;
381 LOG(INFO
) << "truncated answer for " << name
<< '/' << type
;
385 RR_collection
Query::get_records()
387 if (bogus_or_indeterminate_
)
388 return RR_collection
{};
390 return DNS::get_records(a_
, bogus_or_indeterminate_
);
393 std::vector
<std::string
> Query::get_strings()
395 std::vector
<std::string
> ret
;
397 auto const rr_set
= get_records();
399 for (auto rr
: rr_set
) {
401 [&ret
, type
= type_
](auto const& r
) {
402 if (type
== r
.rr_type()) {
403 auto const s
= r
.as_str();