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 auto const last
= std::unique(fcrdns
.begin(), fcrdns
.end());
43 fcrdns
.erase(last
, fcrdns
.end());
48 std::vector
<std::string
> fcrdns6(Resolver
& res
, std::string_view addr
)
50 auto const reversed
{IP6::reverse(addr
)};
52 // The reverse part, check PTR records.
53 auto const ptrs
= res
.get_records(RR_type::PTR
, reversed
+ "ip6.arpa");
55 std::vector
<std::string
> fcrdns
;
57 for (auto const& ptr
: ptrs
) {
58 if (std::holds_alternative
<DNS::RR_PTR
>(ptr
)) {
59 // The forward part, check each PTR for matching AAAA record.
61 res
.get_strings(RR_type::AAAA
, std::get
<DNS::RR_PTR
>(ptr
).str());
62 if (std::find(begin(addrs
), end(addrs
), addr
) != end(addrs
)) {
63 fcrdns
.push_back(std::get
<DNS::RR_PTR
>(ptr
).str());
68 // Sort 1st by name length: short to long.
69 std::sort(begin(fcrdns
), end(fcrdns
), [](auto a
, auto b
) {
70 if (size(a
) != size(b
))
71 return size(a
) < size(b
);
78 std::vector
<std::string
> fcrdns(Resolver
& res
, std::string_view addr
)
80 // <https://en.wikipedia.org/wiki/Forward-confirmed_reverse_DNS>
82 if (IP4::is_address(addr
))
83 return fcrdns4(res
, addr
);
84 if (IP6::is_address(addr
))
85 return fcrdns6(res
, addr
);
86 LOG(FATAL
) << "not a valid IP address " << addr
;