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(7)};
29 enum class sock_type
: bool { stream
, dgram
};
32 char const* host
; // name used to match cert
38 constexpr nameserver nameservers
[]{
79 "2001:4860:4860::8888",
85 "2001:4860:4860::8844",
90 "1dot1dot1dot1.cloudflare-dns.com",
96 "1dot1dot1dot1.cloudflare-dns.com",
97 "2606:4700:4700::1111",
102 "1dot1dot1dot1.cloudflare-dns.com",
103 "2606:4700:4700::1001",
133 } // namespace Config
135 template <typename T
, std::size_t N
>
136 constexpr std::size_t countof(T
const (&)[N
]) noexcept
143 Resolver::Resolver(fs::path config_path
)
145 auto tries
= countof(Config::nameservers
);
147 if (FLAGS_random_dns_servers
) {
148 std::uniform_int_distribution
<int> uniform_dist(
149 0, countof(Config::nameservers
) - 1);
150 ns_
= uniform_dist(rng_
);
153 ns_
= static_cast<int>(countof(Config::nameservers
) - 1);
158 // try the next one, with wrap
159 if (++ns_
== countof(Config::nameservers
))
162 auto const& nameserver
= Config::nameservers
[ns_
];
163 auto typ
= (nameserver
.typ
== Config::sock_type::stream
) ? SOCK_STREAM
167 osutil::get_port(nameserver
.port
, (typ
== SOCK_STREAM
) ? "tcp" : "udp");
170 if (IP4::is_address(nameserver
.addr
)) {
171 ns_fd_
= socket(AF_INET
, typ
, 0);
172 PCHECK(ns_fd_
>= 0) << "socket() failed";
174 auto in4
{sockaddr_in
{}};
175 in4
.sin_family
= AF_INET
;
176 in4
.sin_port
= htons(port
);
177 CHECK_EQ(inet_pton(AF_INET
, nameserver
.addr
,
178 reinterpret_cast<void*>(&in4
.sin_addr
)),
180 if (connect(ns_fd_
, reinterpret_cast<const sockaddr
*>(&in4
),
182 PLOG(INFO
) << "connect failed " << nameserver
.host
<< '['
183 << nameserver
.addr
<< "]:" << nameserver
.port
;
189 else if (IP6::is_address(nameserver
.addr
)) {
190 ns_fd_
= socket(AF_INET6
, typ
, 0);
191 PCHECK(ns_fd_
>= 0) << "socket() failed";
193 auto in6
{sockaddr_in6
{}};
194 in6
.sin6_family
= AF_INET6
;
195 in6
.sin6_port
= htons(port
);
196 CHECK_EQ(inet_pton(AF_INET6
, nameserver
.addr
,
197 reinterpret_cast<void*>(&in6
.sin6_addr
)),
199 if (connect(ns_fd_
, reinterpret_cast<const sockaddr
*>(&in6
),
201 PLOG(INFO
) << "connect failed " << nameserver
.host
<< '['
202 << nameserver
.addr
<< "]:" << nameserver
.port
;
209 POSIX::set_nonblocking(ns_fd_
);
211 if (nameserver
.typ
== Config::sock_type::stream
) {
212 ns_sock_
= std::make_unique
<Sock
>(ns_fd_
, ns_fd_
);
213 if (FLAGS_log_dns_data
) {
214 ns_sock_
->log_data_on();
217 ns_sock_
->log_data_off();
221 DNS::RR_collection tlsa_rrs
; // empty FIXME!
222 ns_sock_
->starttls_client(config_path
, nullptr, nameserver
.host
,
224 if (ns_sock_
->verified()) {
238 LOG(FATAL
) << "no nameservers left to try";
241 message
Resolver::xchg(message
const& q
)
243 if (Config::nameservers
[ns_
].typ
== Config::sock_type::stream
) {
244 CHECK_EQ(ns_fd_
, -1);
246 uint16_t sz
= htons(std::size(q
));
248 ns_sock_
->out().write(reinterpret_cast<char const*>(&sz
), sizeof sz
);
249 ns_sock_
->out().write(reinterpret_cast<char const*>(begin(q
)), size(q
));
250 ns_sock_
->out().flush();
253 ns_sock_
->in().read(reinterpret_cast<char*>(&sz
), sizeof sz
);
256 DNS::message::container_t
bfr(sz
);
257 ns_sock_
->in().read(reinterpret_cast<char*>(bfr
.data()), sz
);
258 CHECK_EQ(ns_sock_
->in().gcount(), std::streamsize(sz
));
260 if (!ns_sock_
->in()) {
261 LOG(WARNING
) << "Resolver::xchg was able to read only "
262 << ns_sock_
->in().gcount() << " octets";
265 return message
{std::move(bfr
)};
268 CHECK(Config::nameservers
[ns_
].typ
== Config::sock_type::dgram
);
271 CHECK_EQ(send(ns_fd_
, std::begin(q
), std::size(q
), 0), std::size(q
));
273 DNS::message::container_t
bfr(Config::max_udp_sz
);
275 auto constexpr hook
{[]() {}};
277 auto const a_buf
= reinterpret_cast<char*>(bfr
.data());
278 auto const a_buflen
= POSIX::read(ns_fd_
, a_buf
, int(Config::max_udp_sz
),
279 hook
, Config::read_timeout
, t_o
);
282 LOG(WARNING
) << "DNS read failed";
287 LOG(WARNING
) << "DNS read timed out";
291 bfr
.resize(a_buflen
);
294 return message
{std::move(bfr
)};
297 RR_collection
Resolver::get_records(RR_type typ
, char const* name
)
299 Query
q(*this, typ
, name
);
300 return q
.get_records();
303 std::vector
<std::string
> Resolver::get_strings(RR_type typ
, char const* name
)
305 Query
q(*this, typ
, name
);
306 return q
.get_strings();
309 bool Query::xchg_(Resolver
& res
, uint16_t id
)
318 bogus_or_indeterminate_
= true;
319 LOG(WARNING
) << "no reply from nameserver";
323 if (size(a_
) < min_message_sz()) {
324 bogus_or_indeterminate_
= true;
325 LOG(WARNING
) << "packet too small";
332 LOG(WARNING
) << "packet out of order; ids don't match, got " << a_
.id()
333 << " expecting " << id
;
340 bogus_or_indeterminate_
= true;
341 LOG(WARNING
) << "no tries left, giving up";
346 Query::Query(Resolver
& res
, RR_type type
, char const* name
)
349 uint16_t id
= res
.rnd_id();
350 uint16_t cls
= ns_c_in
;
352 q_
= create_question(name
, type
, cls
, id
);
357 if (size(a_
) < min_message_sz()) {
358 bogus_or_indeterminate_
= true;
359 LOG(INFO
) << "bad (or no) reply for " << name
<< '/' << type
;
363 check_answer(nx_domain_
, bogus_or_indeterminate_
, rcode_
, extended_rcode_
,
364 truncation_
, authentic_data_
, has_record_
, q_
, a_
, type
, name
);
367 // if UDP, retry with TCP
368 bogus_or_indeterminate_
= true;
369 LOG(INFO
) << "truncated answer for " << name
<< '/' << type
;
373 RR_collection
Query::get_records()
375 if (bogus_or_indeterminate_
)
376 return RR_collection
{};
378 return DNS::get_records(a_
, bogus_or_indeterminate_
);
381 std::vector
<std::string
> Query::get_strings()
383 std::vector
<std::string
> ret
;
385 auto const rr_set
= get_records();
387 for (auto rr
: rr_set
) {
389 [&ret
, type
= type_
](auto const& r
) {
390 if (type
== r
.rr_type()) {
391 auto const s
= r
.as_str();