make the smtp_to_mbx flag actually work, other random changes
[ghsmtp.git] / jnk / dns2.cpp
blob205889c5643ffd6e653a2fbac671bf1ca2ebcacc
1 // brain dead command line tool to query DNS
3 #include "DNS.hpp"
4 #include "IP4.hpp"
6 #include <glog/logging.h>
8 void do_dotted_quad(char const* addr)
10 DNS::Resolver res;
12 std::string reversed{IP4::reverse(addr)};
14 constexpr const char* const rbls[]
15 = {"zen.spamhaus.org", "b.barracudacentral.org"};
17 for (auto rbl : rbls) {
18 std::string lookup = reversed + rbl;
20 DNS::Domain dom(lookup.c_str());
21 DNS::Query<DNS::RR_type::A> q(res, dom);
23 if (q.get_rcode() == DNS::Pkt_rcode::NOERROR) {
24 DNS::Rrlist<DNS::RR_type::A> rrlst(q);
26 if (!rrlst.empty()) {
27 std::cout << "found in " << rbl << '\n';
28 std::vector<std::string> codes = rrlst.get();
29 for (auto code : codes) {
30 std::cout << code << '\n';
34 else {
35 if (q.get_rcode() == DNS::Pkt_rcode::NXDOMAIN) {
36 std::cout << "not found in " << rbl << '\n';
38 else {
39 std::cout << "Error from lookup at " << rbl << " " << q.get_rcode()
40 << '\n';
45 std::string fcrdns;
47 auto ptrs
48 = DNS::get_records<DNS::RR_type::PTR>(res, reversed + "in-addr.arpa");
50 for (auto ptr : ptrs) {
51 // chop off the trailing '.'
52 auto length = ptr.length();
53 if (length && ('.' == ptr.at(length - 1))) {
54 ptr.erase(length - 1, 1);
56 auto addrs = DNS::get_records<DNS::RR_type::A>(res, ptr);
57 for (auto const& a : addrs) {
58 if (a == addr) {
59 fcrdns = ptr;
60 goto found;
65 std::cout << "no fcrdns\n";
66 return;
68 found:
69 std::cout << fcrdns << '\n';
71 auto txts = DNS::get_records<DNS::RR_type::TXT>(res, fcrdns);
72 for (auto const& txt : txts) {
73 std::cout << "\"" << txt << "\"\n";
77 void do_domain(char const* domain)
79 DNS::Resolver res;
81 auto addrs = DNS::get_records<DNS::RR_type::A>(res, domain);
82 for (auto const& a : addrs) {
83 std::cout << a << '\n';
86 auto txts = DNS::get_records<DNS::RR_type::TXT>(res, domain);
87 for (auto const& txt : txts) {
88 std::cout << "\"" << txt << "\"\n";
92 int main(int argc, char const* argv[])
94 google::InitGoogleLogging(argv[0]);
96 for (int i = 1; i < argc; ++i) {
97 if (IP4::is_address(argv[i])) {
98 do_dotted_quad(argv[i]);
100 else {
101 do_domain(argv[i]);