Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide format_as implementations for DNSName, named enums #30

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions dns-storage.cc
Original file line number Diff line number Diff line change
Expand Up @@ -224,3 +224,8 @@ std::string DNSName::toString() const
str << *this;
return str.str();
}

std::string format_as(const DNSName& name)
{
return name.toString();
}
3 changes: 2 additions & 1 deletion dns-storage.hh
Original file line number Diff line number Diff line change
Expand Up @@ -166,8 +166,9 @@ struct DNSName
std::deque<DNSLabel> d_name;
};

// printing, concatenation
// printing, fmt formatting, concatenation
std::ostream & operator<<(std::ostream &os, const DNSName& d);
std::string format_as(const DNSName& name);
DNSName operator+(const DNSName& a, const DNSName& b);
DNSName makeDNSName(const std::string& str);

Expand Down
35 changes: 15 additions & 20 deletions dnsmon.cc
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,7 @@ CheckResult DNSChecker::perform()

double timeo=0.5;
if(!waitForData(sock, &timeo)) { // timeout
return fmt::format("Timeout asking DNS question for {}|{} to {}",
d_qname.toString(), toString(d_qtype), d_nsip.toStringWithPort());
return fmt::format("Timeout asking DNS question for {}|{} to {}", d_qname, d_qtype, d_nsip.toStringWithPort());
}


Expand All @@ -129,7 +128,7 @@ CheckResult DNSChecker::perform()

if((RCode)dmr.dh.rcode != RCode::Noerror) {
return fmt::format("Got DNS response with RCode {} from {} for question {}|{}",
toString((RCode)dmr.dh.rcode), d_qname.toString(), d_nsip.toStringWithPort(), toString(d_qtype));
(RCode)dmr.dh.rcode, d_qname, d_nsip.toStringWithPort(), d_qtype);
}

std::unique_ptr<RRGen> rr;
Expand All @@ -148,12 +147,12 @@ CheckResult DNSChecker::perform()
for(const auto& a : d_acceptable)
acc.insert(makeDNSName(a));
if(!acc.count(dynamic_cast<NSGen*>(rr.get())->d_name)) {
return fmt::format("Unacceptable DNS answer {} for question {} from {}. Acceptable: {}", rr->toString(), d_qname.toString(), d_nsip.toStringWithPort(), d_acceptable);
return fmt::format("Unacceptable DNS answer {} for question {} from {}. Acceptable: {}", rr->toString(), d_qname, d_nsip.toStringWithPort(), d_acceptable);
}
else matches++;
}
else if(!d_acceptable.count(rr->toString())) {
return fmt::format("Unacceptable DNS answer {} for question {} from {}. Acceptable: {}", rr->toString(), d_qname.toString(), d_nsip.toStringWithPort(), d_acceptable);
return fmt::format("Unacceptable DNS answer {} for question {} from {}. Acceptable: {}", rr->toString(), d_qname, d_nsip.toStringWithPort(), d_acceptable);
}
else matches++;
}
Expand All @@ -166,7 +165,7 @@ CheckResult DNSChecker::perform()
return "";
}
else {
return fmt::format("No matching answer to question {}|{} to {} was received", d_qname.toString(), toString(d_qtype), d_nsip.toStringWithPort());
return fmt::format("No matching answer to question {}|{} to {} was received", d_qname, d_qtype, d_nsip.toStringWithPort());
}

}
Expand Down Expand Up @@ -202,8 +201,7 @@ CheckResult DNSSOAChecker::perform()

double timeo=0.5;
if(!waitForData(sock, &timeo)) { // timeout
return fmt::format("Timeout asking DNS question for {}|{} to {}",
d_domain.toString(), toString(DNSType::SOA), s.toStringWithPort());
return fmt::format("Timeout asking DNS question for {}|{} to {}", d_domain, DNSType::SOA, s.toStringWithPort());
}
string resp = SRecvfrom(sock, 65535, server);

Expand All @@ -220,7 +218,7 @@ CheckResult DNSSOAChecker::perform()

if((RCode)dmr.dh.rcode != RCode::Noerror) {
return fmt::format("Got DNS response with RCode {} for question {}|{}",
toString((RCode)dmr.dh.rcode), d_domain.toString(), toString(DNSType::SOA));
(RCode)dmr.dh.rcode, d_domain, DNSType::SOA);
}

std::unique_ptr<RRGen> rr;
Expand All @@ -233,13 +231,11 @@ CheckResult DNSSOAChecker::perform()
}
}
if(!matches) {
return fmt::format("DNS server {} did not return a SOA for {}",
s.toStringWithPort(), d_domain.toString());
return fmt::format("DNS server {} did not return a SOA for {}", s.toStringWithPort(), d_domain);
}
}
if(harvest.size() != 1) {
return fmt::format("Had different SOA records for {}: {}",
d_domain.toString(), harvest);
return fmt::format("Had different SOA records for {}: {}", d_domain, harvest);
}
else {
return "";
Expand Down Expand Up @@ -278,8 +274,7 @@ CheckResult RRSIGChecker::perform()

double timeo=1.0;
if(!waitForData(sock, &timeo)) { // timeout
return fmt::format("Timeout asking DNS question for {}|{} to {}",
d_qname.toString(), toString(d_qtype), d_nsip.toStringWithPort());
return fmt::format("Timeout asking DNS question for {}|{} to {}", d_qname, d_qtype, d_nsip.toStringWithPort());
}


Expand All @@ -295,7 +290,7 @@ CheckResult RRSIGChecker::perform()

if((RCode)dmr.dh.rcode != RCode::Noerror) {
return fmt::format("Got DNS response with RCode {} from {} for question {}|{}",
toString((RCode)dmr.dh.rcode), d_qname.toString(), d_nsip.toStringWithPort(), toString(d_qtype));
(RCode)dmr.dh.rcode, d_qname, d_nsip.toStringWithPort(), d_qtype);
}

std::unique_ptr<RRGen> rr;
Expand All @@ -304,7 +299,7 @@ CheckResult RRSIGChecker::perform()
if(rrsection == DNSSection::Answer && dt == DNSType::RRSIG && dn == d_qname) {
auto rrsig = dynamic_cast<RRSIGGen*>(rr.get());
if(rrsig->d_type != d_qtype) {
fmt::print("Skipping wrong type {}\n", toString(rrsig->d_type));
fmt::print("Skipping wrong type {}\n", rrsig->d_type);
continue;
}
struct tm tmstart={}, tmend={};
Expand All @@ -319,18 +314,18 @@ CheckResult RRSIGChecker::perform()
if(now + d_minDays * 86400 > expire)
return fmt::format("Got RRSIG that expires in {:.0f} days for {}|{} from {}, valid from {:%Y-%m-%d %H:%M} to {:%Y-%m-%d %H:%M} UTC",
(expire - now)/86400.0,
d_qname.toString(), toString(d_qtype), d_nsip.toStringWithPort(), tmstart, tmend);
d_qname, d_qtype, d_nsip.toStringWithPort(), tmstart, tmend);
else if(now < inception) {
fmt::print("Got RRSIG that is not yet active for {}|{} from {}, valid from {:%Y-%m-%d %H:%M} to {:%Y-%m-%d %H:%M} UTC\n",
d_qname.toString(), toString(d_qtype), d_nsip.toStringWithPort(), tmstart, tmend);
d_qname, d_qtype, d_nsip.toStringWithPort(), tmstart, tmend);

}
else
valid=true;
}
}
if(!valid)
return fmt::format("Did not find an active RRSIG for {}|{} over at server {}", d_qname.toString(), toString(d_qtype), d_nsip.toStringWithPort());
return fmt::format("Did not find an active RRSIG for {}|{} over at server {}", d_qname, d_qtype, d_nsip.toStringWithPort());

return "";
}
4 changes: 3 additions & 1 deletion nenum.hh
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ inline x make##x(const char* from) { \
} \
inline std::ostream& operator<<(std::ostream &os, const x& s) { \
os << toString(s); return os; } \

inline std::string format_as(const x& s) { \
return toString(s); } \

#define COMBOENUM4(x, a1,b1,a2,b2,a3,b3,a4,b4) enum class x : uint16_t { \
a1=b1, a2=b2, a3=b3, a4=b4 }; SMARTENUMSTART(x) SENUM4(x, a1, a2, a3,a4) \
SMARTENUMEND(x)
10 changes: 4 additions & 6 deletions simplomon.hh
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ public:
std::string getDescription() override
{
return fmt::format("DNS check, server {}, qname {}, qtype {}, acceptable: {}",
d_nsip.toStringWithPort(), d_qname.toString(), toString(d_qtype), d_acceptable);
d_nsip.toStringWithPort(), d_qname, d_qtype, d_acceptable);
}
private:
ComboAddress d_nsip;
Expand All @@ -138,7 +138,7 @@ public:
std::string getDescription() override
{
return fmt::format("RRSIG check, server {}, qname {}, qtype {}, minDays: {}",
d_nsip.toStringWithPort(), d_qname.toString(), toString(d_qtype), d_minDays);
d_nsip.toStringWithPort(), d_qname, d_qtype, d_minDays);
}

private:
Expand All @@ -159,8 +159,7 @@ public:
{
std::vector<std::string> servers;
for(const auto& s : d_servers) servers.push_back(s.toStringWithPort());
return fmt::format("DNS SOA check, servers {}, domain {}",
servers, d_domain.toString());
return fmt::format("DNS SOA check, servers {}, domain {}", servers, d_domain);
}

private:
Expand Down Expand Up @@ -304,8 +303,7 @@ public:
std::string getCheckerName() override { return "smtp"; }
std::string getDescription() override
{
return fmt::format("SMTP check for {}",
d_server.toStringWithPort());
return fmt::format("SMTP check for {}", d_server.toStringWithPort());
}

private:
Expand Down
8 changes: 2 additions & 6 deletions support.cc
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,7 @@ static DNSMessageReader sendQuery(const vector<ComboAddress>& resolvers, DNSName
}

if(dmr.dh.tc) {
throw std::runtime_error(fmt::format("Needed to do TCP DNS for {}|{} which we can't",
dn.toString(), toString(dt))
);
throw std::runtime_error(fmt::format("Needed to do TCP DNS for {}|{} which we can't", dn, dt));
// shit
}
else
Expand All @@ -54,9 +52,7 @@ static DNSMessageReader sendQuery(const vector<ComboAddress>& resolvers, DNSName
catch(...){}
}
}
throw std::runtime_error(fmt::format("No DNS server could be reached or responded trying to resolve '{}|{}'",
dn.toString(), toString(dt))
);
throw std::runtime_error(fmt::format("No DNS server could be reached or responded trying to resolve '{}|{}'", dn, dt));
}


Expand Down
Loading