From: bert hubert Date: Mon, 25 Apr 2016 08:02:01 +0000 (+0200) Subject: initial work on TeeAction, still has some missing bits, like not blocking the whole... X-Git-Tag: rec-4.0.0-alpha3~46^2~5 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=cf6874baf0b716a0273554264fe7f1a927d58010;p=thirdparty%2Fpdns.git initial work on TeeAction, still has some missing bits, like not blocking the whole daemon when you delete a rule --- diff --git a/pdns/dnsdist-lua2.cc b/pdns/dnsdist-lua2.cc index 27dd903d0b..5759ffe5fc 100644 --- a/pdns/dnsdist-lua2.cc +++ b/pdns/dnsdist-lua2.cc @@ -583,4 +583,14 @@ void moreLua(bool client) return std::make_shared(ComboAddress(remote), timeout ? *timeout : 2, maxQueuedEntries ? *maxQueuedEntries : 100, reconnectWaitTime ? *reconnectWaitTime : 1); }); + g_lua.writeFunction("TeeAction", [](const std::string& remote) { + return std::shared_ptr(new TeeAction(ComboAddress(remote, 53))); + }); + + g_lua.registerFunction("printStats", [](const DNSAction& ta) { + auto stats = ta.getStats(); + for(const auto& s : stats) { + g_outputBuffer+=s.first+"\t"+std::to_string(s.second)+"\n"; + } + }); } diff --git a/pdns/dnsdist.hh b/pdns/dnsdist.hh index a7893b38e5..cd2aced74c 100644 --- a/pdns/dnsdist.hh +++ b/pdns/dnsdist.hh @@ -424,6 +424,10 @@ public: enum class Action { Drop, Nxdomain, Spoof, Allow, HeaderModify, Pool, Delay, None}; virtual Action operator()(DNSQuestion*, string* ruleresult) const =0; virtual string toString() const = 0; + virtual std::unordered_map getStats() const + { + return {{}}; + } }; class DNSResponseAction diff --git a/pdns/dnsdistdist/Makefile.am b/pdns/dnsdistdist/Makefile.am index e8a8ab581a..16cdd557cb 100644 --- a/pdns/dnsdistdist/Makefile.am +++ b/pdns/dnsdistdist/Makefile.am @@ -75,7 +75,7 @@ dnsdist_SOURCES = \ dnslabeltext.cc \ dnsname.cc dnsname.hh \ dnsparser.hh dnsparser.cc \ - dnsrulactions.hh \ + dnsrulactions.cc dnsrulactions.hh \ dnswriter.cc dnswriter.hh \ dolog.hh \ ednsoptions.cc ednsoptions.hh \ diff --git a/pdns/dnsdistdist/dnsrulactions.cc b/pdns/dnsdistdist/dnsrulactions.cc new file mode 100644 index 0000000000..263cd73d37 --- /dev/null +++ b/pdns/dnsdistdist/dnsrulactions.cc @@ -0,0 +1,55 @@ +#include "dnsrulactions.hh" +#include + +using namespace std; + +TeeAction::TeeAction(const ComboAddress& ca) : d_remote(ca) +{ + cerr<<"Created!"<dh, dq->len, 0); + return DNSAction::Action::None; +} + +string TeeAction::toString() const +{ + return "tee to "+d_remote.toStringWithPort(); +} + +std::unordered_map TeeAction::getStats() const +{ + return {{"queries", d_queries}, + {"responses", d_responses}, + {"socket-errors", d_errors}}; +} + +void TeeAction::worker() +{ + char packet[1500]; + int res=0; + for(;;) { + res=recv(d_fd, packet, sizeof(packet), 0); + if(res < 0) + d_errors++; + else if(res > 0) + d_responses++; + if(d_pleaseQuit) + break; + } +} diff --git a/pdns/dnsrulactions.hh b/pdns/dnsrulactions.hh index 9435a19db2..ba6c75831a 100644 --- a/pdns/dnsrulactions.hh +++ b/pdns/dnsrulactions.hh @@ -411,6 +411,28 @@ private: }; +class TeeAction : public DNSAction +{ +public: + TeeAction(const ComboAddress& ca); + ~TeeAction(); + DNSAction::Action operator()(DNSQuestion* dq, string* ruleresult) const override; + string toString() const override; + std::unordered_map getStats() const override; +private: + ComboAddress d_remote; + std::thread d_worker; + void worker(); + + int d_fd; + unsigned long d_errors{0}; + mutable unsigned long d_queries{0}; + unsigned long d_responses{0}; + std::atomic d_pleaseQuit{false}; +}; + + + class PoolAction : public DNSAction { public: