1 #include "DNS-fcrdns.hpp"
8 #include <glog/logging.h>
10 // <https://en.wikipedia.org/wiki/Forward-confirmed_reverse_DNS>
14 std::vector
<std::string
> fcrdns4(Resolver
& res
, std::string_view addr
)
16 auto const reversed
{IP4::reverse(addr
)};
18 // The reverse part, check PTR records.
19 auto const ptrs
= res
.get_records(RR_type::PTR
, reversed
+ "in-addr.arpa");
21 std::vector
<std::string
> fcrdns
;
23 for (auto const& ptr
: ptrs
) {
24 if (std::holds_alternative
<DNS::RR_PTR
>(ptr
)) {
25 // The forward part, check each PTR for matching A record.
27 = res
.get_strings(RR_type::A
, std::get
<DNS::RR_PTR
>(ptr
).str());
28 if (std::find(begin(addrs
), end(addrs
), addr
) != end(addrs
)) {
29 fcrdns
.push_back(std::get
<DNS::RR_PTR
>(ptr
).str());
34 // Sort 1st by name length: short to long.
35 std::sort(begin(fcrdns
), end(fcrdns
),
36 [](std::string_view a
, std::string_view b
) {
37 if (a
.length() != b
.length())
38 return a
.length() < b
.length();
42 std::unique(begin(fcrdns
), end(fcrdns
));
47 std::vector
<std::string
> fcrdns6(Resolver
& res
, std::string_view addr
)
49 auto const reversed
{IP6::reverse(addr
)};
51 // The reverse part, check PTR records.
52 auto const ptrs
= res
.get_records(RR_type::PTR
, reversed
+ "ip6.arpa");
54 std::vector
<std::string
> fcrdns
;
56 for (auto const& ptr
: ptrs
) {
57 if (std::holds_alternative
<DNS::RR_PTR
>(ptr
)) {
58 // The forward part, check each PTR for matching AAAA record.
60 = res
.get_strings(RR_type::AAAA
, std::get
<DNS::RR_PTR
>(ptr
).str());
61 if (std::find(begin(addrs
), end(addrs
), addr
) != end(addrs
)) {
62 fcrdns
.push_back(std::get
<DNS::RR_PTR
>(ptr
).str());
67 // Sort 1st by name length: short to long.
68 std::sort(begin(fcrdns
), end(fcrdns
), [](auto a
, auto b
) {
69 if (size(a
) != size(b
))
70 return size(a
) < size(b
);
77 std::vector
<std::string
> fcrdns(Resolver
& res
, std::string_view addr
)
79 // <https://en.wikipedia.org/wiki/Forward-confirmed_reverse_DNS>
81 if (IP4::is_address(addr
))
82 return fcrdns4(res
, addr
);
83 if (IP6::is_address(addr
))
84 return fcrdns6(res
, addr
);
85 LOG(FATAL
) << "not a valid IP address " << addr
;