From: Remi Gacogne Date: Tue, 28 Mar 2023 07:12:25 +0000 (+0200) Subject: dnsdist: Apply suggestions from code review, apply formatting X-Git-Tag: dnsdist-1.9.0-alpha0~1^2 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=refs%2Fpull%2F12689%2Fhead;p=thirdparty%2Fpdns.git dnsdist: Apply suggestions from code review, apply formatting --- diff --git a/pdns/dnsdist-lua-inspection.cc b/pdns/dnsdist-lua-inspection.cc index b1f7ac0e09..3e5dcd096a 100644 --- a/pdns/dnsdist-lua-inspection.cc +++ b/pdns/dnsdist-lua-inspection.cc @@ -414,12 +414,12 @@ void setupLuaInspection(LuaContext& luaCtx) setLuaNoSideEffect(); boost::optional nm; boost::optional dn; - int msec=-1; + int msec = -1; std::unique_ptr outputFile{nullptr, fclose}; if (options) { - if (options->count("outputFile")) { - const std::string& outputFileName = options->at("outputFile"); + std::string outputFileName; + if (getOptionalValue(options, "outputFile", outputFileName) > 0) { int fd = open(outputFileName.c_str(), O_CREAT | O_EXCL | O_WRONLY, 0600); if (fd < 0) { g_outputBuffer = "Error opening dump file for writing: " + stringerror() + "\n"; @@ -428,37 +428,41 @@ void setupLuaInspection(LuaContext& luaCtx) outputFile = std::unique_ptr(fdopen(fd, "w"), fclose); if (outputFile == nullptr) { g_outputBuffer = "Error opening dump file for writing: " + stringerror() + "\n"; + close(fd); return; } } + checkAllParametersConsumed("grepq", options); } vector vec; - auto str=boost::get(&inp); - if(str) + auto str = boost::get(&inp); + if (str) { vec.push_back(*str); + } else { auto v = boost::get>(inp); - for(const auto& a: v) + for (const auto& a: v) { vec.push_back(a.second); + } } - for(const auto& s : vec) { - try - { + for (const auto& s : vec) { + try { nm = Netmask(s); - } - catch(...) { - if(boost::ends_with(s,"ms") && sscanf(s.c_str(), "%ums", &msec)) { + } + catch (...) { + if (boost::ends_with(s,"ms") && sscanf(s.c_str(), "%ums", &msec)) { ; } else { - try { dn=DNSName(s); } - catch(...) - { - g_outputBuffer = "Could not parse '"+s+"' as domain name or netmask"; - return; - } + try { + dn = DNSName(s); + } + catch (...) { + g_outputBuffer = "Could not parse '"+s+"' as domain name or netmask"; + return; + } } } } @@ -497,16 +501,18 @@ void setupLuaInspection(LuaContext& luaCtx) std::multimap out; boost::format fmt("%-7.1f %-47s %-12s %-12s %-5d %-25s %-5s %-6.1f %-2s %-2s %-2s %-s\n"); + const auto headLine = (fmt % "Time" % "Client" % "Protocol" % "Server" % "ID" % "Name" % "Type" % "Lat." % "TC" % "RD" % "AA" % "Rcode").str(); if (!outputFile) { - g_outputBuffer += (fmt % "Time" % "Client" % "Protocol" % "Server" % "ID" % "Name" % "Type" % "Lat." % "TC" % "RD" % "AA" % "Rcode").str(); + g_outputBuffer += headLine; } else { - fprintf(outputFile.get(), "%s", (fmt % "Time" % "Client" % "Protocol" % "Server" % "ID" % "Name" % "Type" % "Lat." % "TC" % "RD" % "AA" % "Rcode").str().c_str()); + fprintf(outputFile.get(), "%s", headLine.c_str()); } - if(msec==-1) { - for(const auto& c : qr) { - bool nmmatch=true, dnmatch=true; + if (msec == -1) { + for (const auto& c : qr) { + bool nmmatch = true; + bool dnmatch = true; if (nm) { nmmatch = nm->match(c.requestor); } @@ -526,17 +532,19 @@ void setupLuaInspection(LuaContext& luaCtx) } out.emplace(c.when, (fmt % DiffTime(now, c.when) % c.requestor.toStringWithPort() % dnsdist::Protocol(c.protocol).toString() % "" % htons(c.dh.id) % c.name.toString() % qt.toString() % "" % (c.dh.tc ? "TC" : "") % (c.dh.rd ? "RD" : "") % (c.dh.aa ? "AA" : "") % ("Question" + extra)).str()); - if(limit && *limit==++num) + if (limit && *limit == ++num) { break; + } } } } - num=0; - + num = 0; string extra; - for(const auto& c : rr) { - bool nmmatch=true, dnmatch=true, msecmatch=true; + for (const auto& c : rr) { + bool nmmatch = true; + bool dnmatch = true; + bool msecmatch = true; if (nm) { nmmatch = nm->match(c.requestor); } @@ -549,13 +557,13 @@ void setupLuaInspection(LuaContext& luaCtx) } } if (msec != -1) { - msecmatch=(c.usec/1000 > (unsigned int)msec); + msecmatch = (c.usec/1000 > (unsigned int)msec); } if (nmmatch && dnmatch && msecmatch) { QType qt(c.qtype); if (!c.dh.rcode) { - extra=". " +std::to_string(htons(c.dh.ancount))+ " answers"; + extra = ". " +std::to_string(htons(c.dh.ancount)) + " answers"; } else { extra.clear();