From: bert hubert Date: Mon, 29 Feb 2016 10:32:00 +0000 (+0100) Subject: implement exposing edns subnet in preresolve() - other hooks would require some more... X-Git-Tag: rec-4.0.0-alpha2~31^2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=5ecf1d7ec62b4c1b7b41af7510907dca693555c2;p=thirdparty%2Fpdns.git implement exposing edns subnet in preresolve() - other hooks would require some more work (they don't get EDNS anyhow) --- diff --git a/pdns/ednssubnet.hh b/pdns/ednssubnet.hh index a724b4336c..0848320101 100644 --- a/pdns/ednssubnet.hh +++ b/pdns/ednssubnet.hh @@ -28,8 +28,6 @@ extern NetmaskGroup g_ednssubnets; extern SuffixMatchNode g_ednsdomains; - - struct EDNSSubnetOpts { Netmask source; diff --git a/pdns/lua-recursor4.cc b/pdns/lua-recursor4.cc index 96440970a8..1543a7bdae 100644 --- a/pdns/lua-recursor4.cc +++ b/pdns/lua-recursor4.cc @@ -5,6 +5,7 @@ #include "syncres.hh" #include "namespaces.hh" #include "rec_channel.hh" +#include "ednssubnet.hh" #include #if !defined(HAVE_LUA) @@ -145,6 +146,23 @@ boost::optional RecursorLua4::DNSQuestion::getEDNSOption(uint16_t code) return boost::optional(); } +boost::optional RecursorLua4::DNSQuestion::getEDNSSubnet() +{ + + if(ednsOptions) { + for(const auto& o : *ednsOptions) { + if(o.first==8) { + EDNSSubnetOpts eso; + if(getEDNSSubnetOptsFromString(o.second, &eso)) + return eso.source; + else + break; + } + } + } + return boost::optional(); +} + vector > RecursorLua4::DNSQuestion::getRecords() { @@ -256,6 +274,10 @@ RecursorLua4::RecursorLua4(const std::string& fname) return ComboAddress::addressOnlyEqual()(lhs, rhs); }); + + d_lw->registerFunction("getNetwork", [](const Netmask& nm) { return nm.getNetwork(); } ); // const reference makes this necessary + d_lw->registerFunction("toString", &Netmask::toString); + d_lw->writeFunction("newNMG", []() { return NetmaskGroup(); }); d_lw->registerFunction("addMask", [](NetmaskGroup&nmg, const std::string& mask) { @@ -282,6 +304,7 @@ RecursorLua4::RecursorLua4(const std::string& fname) d_lw->registerMember("udpCallback", &DNSQuestion::udpCallback); d_lw->registerFunction("getEDNSOptions", &DNSQuestion::getEDNSOptions); d_lw->registerFunction("getEDNSOption", &DNSQuestion::getEDNSOption); + d_lw->registerFunction("getEDNSSubnet", &DNSQuestion::getEDNSSubnet); d_lw->registerMember("name", &DNSRecord::d_name); d_lw->registerMember("type", &DNSRecord::d_type); d_lw->registerMember("ttl", &DNSRecord::d_ttl); diff --git a/pdns/lua-recursor4.hh b/pdns/lua-recursor4.hh index 94c8d052a3..12e622e84a 100644 --- a/pdns/lua-recursor4.hh +++ b/pdns/lua-recursor4.hh @@ -39,6 +39,7 @@ private: vector > getRecords(); vector > getEDNSOptions(); boost::optional getEDNSOption(uint16_t code); + boost::optional getEDNSSubnet(); void setRecords(const vector >& records); bool variable{false}; diff --git a/pdns/powerdns-example-script.lua b/pdns/powerdns-example-script.lua index 3374e4e468..a22c9ed0a0 100644 --- a/pdns/powerdns-example-script.lua +++ b/pdns/powerdns-example-script.lua @@ -20,6 +20,12 @@ magicMetric = getMetric("magic") function preresolve(dq) print("Got question for "..dq.qname:toString().." from "..dq.remoteaddr:toString().." to "..dq.localaddr:toString()) + local ednssubnet=dq:getEDNSSubnet() + if(ednssubnet) then + print("Packet EDNS subnet source: "..ednssubnet:toString()..", "..ednssubnet:getNetwork():toString()) + end + + local a=dq:getEDNSOption(3) if(a) then print("There is an EDNS option 3 present: "..a)