This adds a ProbaRule, ProbaRule(1.0) means 'match always', 0.1 '10%'. Useful for TeeAction.
return std::shared_ptr<DNSRule>(new AllRule());
});
+ g_lua.writeFunction("ProbaRule", [](double proba) {
+ return std::shared_ptr<DNSRule>(new ProbaRule(proba));
+ });
+
+
g_lua.writeFunction("QNameRule", [](const std::string& qname) {
return std::shared_ptr<DNSRule>(new QNameRule(DNSName(qname)));
});
*/
#include "dnsrulactions.hh"
#include <iostream>
+#include <boost/format.hpp>
using namespace std;
+bool ProbaRule::matches(const DNSQuestion* dq) const
+{
+ if(d_proba == 1.0)
+ return true;
+ double rnd = 1.0*random() / RAND_MAX;
+ return rnd > (1.0 - d_proba);
+}
+
+string ProbaRule::toString() const
+{
+ return "match with prob. " + (boost::format("%0.2f") % d_proba).str();
+}
+
+
TeeAction::TeeAction(const ComboAddress& ca, bool addECS) : d_remote(ca), d_addECS(addECS)
{
d_fd=SSocket(d_remote.sin4.sin_family, SOCK_DGRAM, 0);
d_worker.join();
}
+
DNSAction::Action TeeAction::operator()(DNSQuestion* dq, string* ruleresult) const
{
- if(dq->tcp)
+ if(dq->tcp) {
d_tcpdrops++;
+ }
else {
ssize_t res;
d_queries++;
:param int code: The opcode to match
+.. function:: ProbaRule(probability)
+
+ .. versionadded:: 1.3.0
+
+ Matches queries with a given probability. 1.0 means "always"
+
+ :param double probability: Probability of a match
+
.. function:: QClassRule(qclass)
Matches queries with the specified ``qclass``.
};
+class ProbaRule : public DNSRule
+{
+public:
+ ProbaRule(double proba) : d_proba(proba)
+ {
+ }
+ bool matches(const DNSQuestion* dq) const override;
+ string toString() const override;
+ double d_proba;
+};
+
+
class DropAction : public DNSAction
{
public:
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;
bool d_addECS{false};
};
-
-
class PoolAction : public DNSAction
{
public: