From 77f8ae1a4b18d1f902da3a06137bb58f7bdf68bf Mon Sep 17 00:00:00 2001 From: bert hubert Date: Mon, 7 Dec 2015 23:05:26 +0100 Subject: [PATCH] implement grepq() for dnsdist --- pdns/Makefile.am | 1 + pdns/dnsdist-lua2.cc | 58 ++++++++++++++++++++++++++++++++++++ pdns/dnsdistdist/Makefile.am | 2 +- pdns/dnsdistdist/dns.cc | 1 + 4 files changed, 61 insertions(+), 1 deletion(-) create mode 120000 pdns/dnsdistdist/dns.cc diff --git a/pdns/Makefile.am b/pdns/Makefile.am index 479f401b5..6876dc30c 100644 --- a/pdns/Makefile.am +++ b/pdns/Makefile.am @@ -599,6 +599,7 @@ dnstcpbench_LDADD = \ dnsdist_SOURCES = \ base32.cc \ base64.hh \ + dns.cc \ dnsparser.hh dnsparser.cc \ ednssubnet.cc ednssubnet.hh \ dnsdist.cc \ diff --git a/pdns/dnsdist-lua2.cc b/pdns/dnsdist-lua2.cc index 47bdd7de2..90c9daafd 100644 --- a/pdns/dnsdist-lua2.cc +++ b/pdns/dnsdist-lua2.cc @@ -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 limit) { + boost::optional nm; + boost::optional 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 lock(g_rings.respMutex); + rr=g_rings.respRing; + } + + unsigned int num=0; + struct timespec now; + clock_gettime(CLOCK_MONOTONIC, &now); + std::multimap 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'); + } + }); } diff --git a/pdns/dnsdistdist/Makefile.am b/pdns/dnsdistdist/Makefile.am index 3915cce44..6433a7adb 100644 --- a/pdns/dnsdistdist/Makefile.am +++ b/pdns/dnsdistdist/Makefile.am @@ -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 index 000000000..370b206b6 --- /dev/null +++ b/pdns/dnsdistdist/dns.cc @@ -0,0 +1 @@ +../dns.cc \ No newline at end of file -- 2.40.0