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");
24 // The default timeout in glibc is 5 seconds. My setup with unbound
25 // in front of stubby with DNSSEC checking and all that seems to work
26 // better with just a little more time.
28 auto constexpr read_timeout
{std::chrono::seconds(7)};
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 ns_
= std::experimental::randint(
143 0, static_cast<int>(countof(Config::nameservers
) - 1));
147 // try the next one, with wrap
148 if (++ns_
== countof(Config::nameservers
))
151 auto const& nameserver
= Config::nameservers
[ns_
];
153 auto typ
= (nameserver
.typ
== Config::sock_type::stream
) ? SOCK_STREAM
156 osutil::get_port(nameserver
.port
, (typ
== SOCK_STREAM
) ? "tcp" : "udp");
159 if (IP4::is_address(nameserver
.addr
)) {
160 ns_fd_
= socket(AF_INET
, typ
, 0);
161 PCHECK(ns_fd_
>= 0) << "socket() failed";
163 auto in4
{sockaddr_in
{}};
164 in4
.sin_family
= AF_INET
;
165 in4
.sin_port
= htons(port
);
166 CHECK_EQ(inet_pton(AF_INET
, nameserver
.addr
,
167 reinterpret_cast<void*>(&in4
.sin_addr
)),
169 if (connect(ns_fd_
, reinterpret_cast<const sockaddr
*>(&in4
),
171 PLOG(INFO
) << "connect failed " << nameserver
.host
<< '['
172 << nameserver
.addr
<< "]:" << nameserver
.port
;
178 else if (IP6::is_address(nameserver
.addr
)) {
179 ns_fd_
= socket(AF_INET6
, typ
, 0);
180 PCHECK(ns_fd_
>= 0) << "socket() failed";
182 auto in6
{sockaddr_in6
{}};
183 in6
.sin6_family
= AF_INET6
;
184 in6
.sin6_port
= htons(port
);
185 CHECK_EQ(inet_pton(AF_INET6
, nameserver
.addr
,
186 reinterpret_cast<void*>(&in6
.sin6_addr
)),
188 if (connect(ns_fd_
, reinterpret_cast<const sockaddr
*>(&in6
),
190 PLOG(INFO
) << "connect failed " << nameserver
.host
<< '['
191 << nameserver
.addr
<< "]:" << nameserver
.port
;
198 POSIX::set_nonblocking(ns_fd_
);
200 if (nameserver
.typ
== Config::sock_type::stream
) {
201 ns_sock_
= std::make_unique
<Sock
>(ns_fd_
, ns_fd_
);
202 if (FLAGS_log_dns_data
) {
203 ns_sock_
->log_data_on();
206 ns_sock_
->log_data_off();
210 DNS::RR_collection tlsa_rrs
; // empty FIXME!
211 ns_sock_
->starttls_client(config_path
, nullptr, nameserver
.host
,
213 if (ns_sock_
->verified()) {
227 LOG(FATAL
) << "no nameservers left to try";
230 message
Resolver::xchg(message
const& q
)
232 if (Config::nameservers
[ns_
].typ
== Config::sock_type::stream
) {
233 CHECK_EQ(ns_fd_
, -1);
235 uint16_t sz
= htons(std::size(q
));
237 ns_sock_
->out().write(reinterpret_cast<char const*>(&sz
), sizeof sz
);
238 ns_sock_
->out().write(reinterpret_cast<char const*>(begin(q
)), size(q
));
239 ns_sock_
->out().flush();
242 ns_sock_
->in().read(reinterpret_cast<char*>(&sz
), sizeof sz
);
245 DNS::message::container_t
bfr(sz
);
246 ns_sock_
->in().read(reinterpret_cast<char*>(bfr
.data()), sz
);
247 CHECK_EQ(ns_sock_
->in().gcount(), std::streamsize(sz
));
249 if (!ns_sock_
->in()) {
250 LOG(WARNING
) << "Resolver::xchg was able to read only "
251 << ns_sock_
->in().gcount() << " octets";
254 return message
{std::move(bfr
)};
257 CHECK(Config::nameservers
[ns_
].typ
== Config::sock_type::dgram
);
260 CHECK_EQ(send(ns_fd_
, std::begin(q
), std::size(q
), 0), std::size(q
));
262 DNS::message::container_t
bfr(Config::max_udp_sz
);
264 auto constexpr hook
{[]() {}};
266 auto const a_buf
= reinterpret_cast<char*>(bfr
.data());
267 auto const a_buflen
= POSIX::read(ns_fd_
, a_buf
, int(Config::max_udp_sz
),
268 hook
, Config::read_timeout
, t_o
);
271 LOG(WARNING
) << "DNS read failed";
276 LOG(WARNING
) << "DNS read timed out";
280 bfr
.resize(a_buflen
);
283 return message
{std::move(bfr
)};
286 RR_collection
Resolver::get_records(RR_type typ
, char const* name
)
288 Query
q(*this, typ
, name
);
289 return q
.get_records();
292 std::vector
<std::string
> Resolver::get_strings(RR_type typ
, char const* name
)
294 Query
q(*this, typ
, name
);
295 return q
.get_strings();
298 bool Query::xchg_(Resolver
& res
, uint16_t id
)
307 bogus_or_indeterminate_
= true;
308 LOG(WARNING
) << "no reply from nameserver";
312 if (size(a_
) < min_message_sz()) {
313 bogus_or_indeterminate_
= true;
314 LOG(WARNING
) << "packet too small";
321 LOG(WARNING
) << "packet out of order; ids don't match, got " << a_
.id()
322 << " expecting " << id
;
329 bogus_or_indeterminate_
= true;
330 LOG(WARNING
) << "no tries left, giving up";
335 Query::Query(Resolver
& res
, RR_type type
, char const* name
)
338 static_assert(std::numeric_limits
<uint16_t>::min() == 0);
339 static_assert(std::numeric_limits
<uint16_t>::max() == 65535);
342 std::experimental::randint(std::numeric_limits
<uint16_t>::min(),
343 std::numeric_limits
<uint16_t>::max());
345 uint16_t cls
= ns_c_in
;
347 q_
= create_question(name
, type
, cls
, id
);
352 if (size(a_
) < min_message_sz()) {
353 bogus_or_indeterminate_
= true;
354 LOG(INFO
) << "bad (or no) reply for " << name
<< '/' << type
;
358 check_answer(nx_domain_
, bogus_or_indeterminate_
, rcode_
, extended_rcode_
,
359 truncation_
, authentic_data_
, has_record_
, q_
, a_
, type
, name
);
362 // if UDP, retry with TCP
363 bogus_or_indeterminate_
= true;
364 LOG(INFO
) << "truncated answer for " << name
<< '/' << type
;
368 RR_collection
Query::get_records()
370 if (bogus_or_indeterminate_
)
371 return RR_collection
{};
373 return DNS::get_records(a_
, bogus_or_indeterminate_
);
376 std::vector
<std::string
> Query::get_strings()
378 std::vector
<std::string
> ret
;
380 auto const rr_set
= get_records();
382 for (auto rr
: rr_set
) {
384 [&ret
, type
= type_
](auto const& r
) {
385 if (type
== r
.rr_type()) {
386 auto const s
= r
.as_str();