]> git.ipfire.org Git - thirdparty/pdns.git/commitdiff
initial work on TeeAction, still has some missing bits, like not blocking the whole...
authorbert hubert <bert.hubert@netherlabs.nl>
Mon, 25 Apr 2016 08:02:01 +0000 (10:02 +0200)
committerbert hubert <bert.hubert@netherlabs.nl>
Mon, 25 Apr 2016 08:02:01 +0000 (10:02 +0200)
pdns/dnsdist-lua2.cc
pdns/dnsdist.hh
pdns/dnsdistdist/Makefile.am
pdns/dnsdistdist/dnsrulactions.cc [new file with mode: 0644]
pdns/dnsrulactions.hh

index 27dd903d0b0cb8df1f46a38689482c76710fc47a..5759ffe5fce81b4db7429365ef2cb6ae4860bf4d 100644 (file)
@@ -583,4 +583,14 @@ void moreLua(bool client)
         return std::make_shared<RemoteLogger>(ComboAddress(remote), timeout ? *timeout : 2, maxQueuedEntries ? *maxQueuedEntries : 100, reconnectWaitTime ? *reconnectWaitTime : 1);
       });
 
+    g_lua.writeFunction("TeeAction", [](const std::string& remote) {
+        return std::shared_ptr<DNSAction>(new TeeAction(ComboAddress(remote, 53)));
+      });
+
+    g_lua.registerFunction<void(DNSAction::*)()>("printStats", [](const DNSAction& ta) {
+        auto stats = ta.getStats();
+        for(const auto& s : stats) {
+          g_outputBuffer+=s.first+"\t"+std::to_string(s.second)+"\n";
+        }
+      });
 }
index a7893b38e57325c65a29c9aa2c8bddde3597d62f..cd2aced74c037fd714e79936a2bcab7a904f5d4b 100644 (file)
@@ -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<string, double> getStats() const 
+  {
+    return {{}};
+  }
 };
 
 class DNSResponseAction
index e8a8ab581ab724fb9d04a4b2225443d53545f39a..16cdd557cb3f60c3a11212d310d0807a57cf9492 100644 (file)
@@ -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 (file)
index 0000000..263cd73
--- /dev/null
@@ -0,0 +1,55 @@
+#include "dnsrulactions.hh"
+#include <iostream>
+
+using namespace std;
+
+TeeAction::TeeAction(const ComboAddress& ca) : d_remote(ca)
+{
+  cerr<<"Created!"<<endl;
+  d_fd=SSocket(d_remote.sin4.sin_family, SOCK_DGRAM, 0);
+  SConnect(d_fd, d_remote);
+  d_worker=std::thread(std::bind(&TeeAction::worker, this));
+  
+}
+
+TeeAction::~TeeAction()
+{
+  cerr<<"Closding down!"<<endl;
+  d_pleaseQuit=true;
+  close(d_fd);
+  d_worker.join();
+}
+
+DNSAction::Action TeeAction::operator()(DNSQuestion* dq, string* ruleresult) const 
+{
+  d_queries++;
+  send(d_fd, (char*)dq->dh, dq->len, 0);
+  return DNSAction::Action::None;
+}
+
+string TeeAction::toString() const
+{
+  return "tee to "+d_remote.toStringWithPort();
+}
+
+std::unordered_map<string,double> 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;
+  }
+}
index 9435a19db20514571aadfa13891e1d6771d5293c..ba6c75831a725ed711dcda6329dcc7f1e49a9af5 100644 (file)
@@ -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<string, double> 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<bool> d_pleaseQuit{false};
+};
+
+
+
 class PoolAction : public DNSAction
 {
 public: