]> granicus.if.org Git - pdns/commitdiff
implement grepq() for dnsdist
authorbert hubert <bert.hubert@netherlabs.nl>
Mon, 7 Dec 2015 22:05:26 +0000 (23:05 +0100)
committerbert hubert <bert.hubert@netherlabs.nl>
Mon, 7 Dec 2015 22:05:26 +0000 (23:05 +0100)
pdns/Makefile.am
pdns/dnsdist-lua2.cc
pdns/dnsdistdist/Makefile.am
pdns/dnsdistdist/dns.cc [new symlink]

index 479f401b52465b4dfb4b9ed44984a5367b8579bf..6876dc30cd943e5d0d9bdc8f0912b297d21942a3 100644 (file)
@@ -599,6 +599,7 @@ dnstcpbench_LDADD = \
 dnsdist_SOURCES = \
        base32.cc \
        base64.hh \
+       dns.cc \
        dnsparser.hh dnsparser.cc \
        ednssubnet.cc ednssubnet.hh \
        dnsdist.cc \
index 47bdd7de26bdfddfcafd03bdf880782ca855a81e..90c9daafd48d6ec108e5b7471f961ab66de5cf18 100644 (file)
@@ -195,4 +195,62 @@ void moreLua()
        g_outputBuffer += (fmt % l.first % l.second.toString()).str();
       }
     });
+
+  g_lua.writeFunction("grepq", [](const std::string& s, boost::optional<unsigned int> limit) {
+      boost::optional<Netmask>  nm;
+      boost::optional<DNSName> dn;
+      try 
+      {
+        nm = Netmask(s);
+      }
+      catch(...) {
+        try { dn=DNSName(s); }
+        catch(...) 
+          {
+            g_outputBuffer = "Could not parse '"+s+"' as domain name or netmask";
+            return;
+          }
+      }
+
+      decltype(g_rings.queryRing) qr;
+      decltype(g_rings.respRing) rr;
+      {
+        ReadLock rl(&g_rings.queryLock);
+        qr=g_rings.queryRing;
+      }
+      {
+       std::lock_guard<std::mutex> lock(g_rings.respMutex);
+        rr=g_rings.respRing;
+      }
+      
+      unsigned int num=0;
+      struct timespec now;
+      clock_gettime(CLOCK_MONOTONIC, &now);
+      std::multimap<struct timespec, string> out;
+      for(const auto& c : qr) {
+        if((nm && nm->match(c.requestor)) || (dn && c.name.isPartOf(*dn)))  {
+          QType qt(c.qtype);
+          out.insert(make_pair(c.when,std::to_string(DiffTime(now, c.when))+'\t'+c.requestor.toStringWithPort() +'\t'+c.name.toString() + '\t' + qt.getName()));
+
+          if(limit && *limit==++num)
+            break;
+        }
+      }
+      num=0;
+
+      for(const auto& c : rr) {
+        if((nm && nm->match(c.requestor)) || (dn && c.name.isPartOf(*dn)))  {
+          QType qt(c.qtype);
+          out.insert(make_pair(c.when,std::to_string(DiffTime(now, c.when))+'\t'+c.requestor.toStringWithPort() +'\t'+c.name.toString() + '\t' + qt.getName()+'\t' + std::to_string(c.usec/1000.0) + '\t'+ RCode::to_s(c.rcode)));
+
+          if(limit && *limit==++num)
+            break;
+        }
+      }
+
+      for(const auto& p : out) {
+        g_outputBuffer+=p.second;
+        g_outputBuffer.append(1,'\n');
+      }
+    });
 }
index 3915cce44774ad8d4478a699a21ae5cbf93432d7..6433a7adb8a585c2363d318e55adc023376fa650 100644 (file)
@@ -31,7 +31,7 @@ endif
 
 dnsdist_SOURCES = \
        base64.hh \
-       dns.hh \
+       dns.cc dns.hh \
        dnsdist.cc dnsdist.hh \
        dnsdist-carbon.cc \
        dnsdist-ecs.cc dnsdist-ecs.hh \
diff --git a/pdns/dnsdistdist/dns.cc b/pdns/dnsdistdist/dns.cc
new file mode 120000 (symlink)
index 0000000..370b206
--- /dev/null
@@ -0,0 +1 @@
+../dns.cc
\ No newline at end of file