]> git.ipfire.org Git - thirdparty/pdns.git/commitdiff
dnsdist: Apply suggestions from code review, apply formatting 12689/head
authorRemi Gacogne <remi.gacogne@powerdns.com>
Tue, 28 Mar 2023 07:12:25 +0000 (09:12 +0200)
committerRemi Gacogne <remi.gacogne@powerdns.com>
Tue, 28 Mar 2023 07:12:25 +0000 (09:12 +0200)
pdns/dnsdist-lua-inspection.cc

index b1f7ac0e095a6334977c86a821e56de68e5f8c4f..3e5dcd096a77a41e5c5f7665f4f368ac96827aee 100644 (file)
@@ -414,12 +414,12 @@ void setupLuaInspection(LuaContext& luaCtx)
       setLuaNoSideEffect();
       boost::optional<Netmask>  nm;
       boost::optional<DNSName> dn;
-      int msec=-1;
+      int msec = -1;
       std::unique_ptr<FILE, decltype(&fclose)> outputFile{nullptr, fclose};
 
       if (options) {
-        if (options->count("outputFile")) {
-          const std::string& outputFileName = options->at("outputFile");
+        std::string outputFileName;
+        if (getOptionalValue<std::string>(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<FILE, decltype(&fclose)>(fdopen(fd, "w"), fclose);
           if (outputFile == nullptr) {
             g_outputBuffer = "Error opening dump file for writing: " + stringerror() + "\n";
+            close(fd);
             return;
           }
         }
+        checkAllParametersConsumed("grepq", options);
       }
 
       vector<string> vec;
-      auto str=boost::get<string>(&inp);
-      if(str)
+      auto str = boost::get<string>(&inp);
+      if (str) {
         vec.push_back(*str);
+      }
       else {
         auto v = boost::get<LuaArray<std::string>>(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<struct timespec, string> 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();