From e8340d276cf7b172fc5ba3e83246b73925750a04 Mon Sep 17 00:00:00 2001 From: bert hubert Date: Tue, 26 Jan 2016 11:09:43 +0100 Subject: [PATCH] hook up EDNS options infra to lua, provide getEDNSOptions to get all of them or getDNSOption to probe for 1 --- pdns/lua-recursor4.cc | 33 +++++++++++++++++++++++++++------ pdns/lua-recursor4.hh | 8 +++++--- pdns/pdns_recursor.cc | 4 +++- 3 files changed, 35 insertions(+), 10 deletions(-) diff --git a/pdns/lua-recursor4.cc b/pdns/lua-recursor4.cc index a0c999b56..42bf13b4d 100644 --- a/pdns/lua-recursor4.cc +++ b/pdns/lua-recursor4.cc @@ -29,7 +29,7 @@ bool RecursorLua4::postresolve(const ComboAddress& remote,const ComboAddress& lo } -bool RecursorLua4::preresolve(const ComboAddress& remote, const ComboAddress& local, const DNSName& query, const QType& qtype, vector& ret, const vector >* ednsOpts, int& res, bool* variable) +bool RecursorLua4::preresolve(const ComboAddress& remote, const ComboAddress& local, const DNSName& query, const QType& qtype, vector& ret, const vector >* ednsOpts, int& res, bool* variable) { return false; } @@ -127,6 +127,25 @@ static int getFakePTRRecords(const DNSName& qname, const std::string& prefix, ve } +vector > RecursorLua4::DNSQuestion::getEDNSOptions() +{ + if(ednsOptions) + return *ednsOptions; + else + return vector>(); +} + +boost::optional RecursorLua4::DNSQuestion::getEDNSOption(uint16_t code) +{ + if(ednsOptions) + for(const auto& o : *ednsOptions) + if(o.first==code) + return o.second; + + return boost::optional(); +} + + vector > RecursorLua4::DNSQuestion::getRecords() { vector > ret; @@ -252,10 +271,12 @@ RecursorLua4::RecursorLua4(const std::string& fname) d_lw->registerMember("udpAnswer", &DNSQuestion::udpAnswer); d_lw->registerMember("udpQueryDest", &DNSQuestion::udpQueryDest); d_lw->registerMember("udpCallback", &DNSQuestion::udpCallback); + d_lw->registerFunction("getEDNSOptions", &DNSQuestion::getEDNSOptions); + d_lw->registerFunction("getEDNSOption", &DNSQuestion::getEDNSOption); d_lw->registerMember("name", &DNSRecord::d_name); d_lw->registerMember("type", &DNSRecord::d_type); d_lw->registerMember("ttl", &DNSRecord::d_ttl); - d_lw->registerMember("ednsOptions", &DNSQuestion::ednsOptions); + d_lw->registerFunction("getContent", [](const DNSRecord& dr) { return dr.d_content->getZoneRepresentation(); }); @@ -335,9 +356,9 @@ RecursorLua4::RecursorLua4(const std::string& fname) d_gettag = d_lw->readVariable>("gettag").get_value_or(0); } -bool RecursorLua4::preresolve(const ComboAddress& remote,const ComboAddress& local, const DNSName& query, const QType& qtype, vector& res, const vector >* ednsOpts, int& ret, bool* variable) +bool RecursorLua4::preresolve(const ComboAddress& remote,const ComboAddress& local, const DNSName& query, const QType& qtype, vector& res, const vector >* ednsOpts, int& ret, bool* variable) { - return genhook(d_preresolve, remote, local, query, qtype, res, 0, ret, variable); + return genhook(d_preresolve, remote, local, query, qtype, res, ednsOpts, ret, variable); } bool RecursorLua4::nxdomain(const ComboAddress& remote,const ComboAddress& local, const DNSName& query, const QType& qtype, vector& res, int& ret, bool* variable) @@ -374,7 +395,7 @@ int RecursorLua4::gettag(const ComboAddress& remote, const ComboAddress& local, return 0; } -bool RecursorLua4::genhook(luacall_t& func, const ComboAddress& remote,const ComboAddress& local, const DNSName& query, const QType& qtype, vector& res, const vector >* ednsOpts, int& ret, bool* variable) +bool RecursorLua4::genhook(luacall_t& func, const ComboAddress& remote,const ComboAddress& local, const DNSName& query, const QType& qtype, vector& res, const vector >* ednsOpts, int& ret, bool* variable) { if(!func) return false; @@ -385,7 +406,7 @@ bool RecursorLua4::genhook(luacall_t& func, const ComboAddress& remote,const Com dq->local=local; dq->remote=remote; dq->records = res; - + dq->ednsOptions = ednsOpts; bool handled=func(dq); if(variable) *variable |= dq->variable; // could still be set to indicate this *name* is variable diff --git a/pdns/lua-recursor4.hh b/pdns/lua-recursor4.hh index 67ccafead..94c8d052a 100644 --- a/pdns/lua-recursor4.hh +++ b/pdns/lua-recursor4.hh @@ -12,7 +12,7 @@ class RecursorLua4 : public boost::noncopyable public: explicit RecursorLua4(const std::string& fname); - bool preresolve(const ComboAddress& remote,const ComboAddress& local, const DNSName& query, const QType& qtype, vector& res, const vector >* ednsOpts, int& ret, bool* variable); + bool preresolve(const ComboAddress& remote,const ComboAddress& local, const DNSName& query, const QType& qtype, vector& res, const vector >* ednsOpts, int& ret, bool* variable); bool nxdomain(const ComboAddress& remote, const ComboAddress& local, const DNSName& query, const QType& qtype, vector& res, int& ret, bool* variable); bool nodata(const ComboAddress& remote, const ComboAddress& local, const DNSName& query, const QType& qtype, vector& res, int& ret, bool* variable); bool postresolve(const ComboAddress& remote, const ComboAddress& local, const DNSName& query, const QType& qtype, vector& res, int& ret, bool* variable); @@ -37,6 +37,8 @@ private: void addAnswer(uint16_t type, const std::string& content, boost::optional ttl, boost::optional name); void addRecord(uint16_t type, const std::string& content, DNSResourceRecord::Place place, boost::optional ttl, boost::optional name); vector > getRecords(); + vector > getEDNSOptions(); + boost::optional getEDNSOption(uint16_t code); void setRecords(const vector >& records); bool variable{false}; @@ -49,14 +51,14 @@ private: string udpCallback; std::unordered_map data; - std::vector>* ednsOptions; + const std::vector>* ednsOptions; DNSName followupName; }; LuaContext* d_lw; typedef std::function)> luacall_t; luacall_t d_preresolve, d_nxdomain, d_nodata, d_postresolve, d_preoutquery, d_postoutquery; - bool genhook(luacall_t& func, const ComboAddress& remote,const ComboAddress& local, const DNSName& query, const QType& qtype, vector& res, const vector >* ednsOpts, int& ret, bool* variable); + bool genhook(luacall_t& func, const ComboAddress& remote,const ComboAddress& local, const DNSName& query, const QType& qtype, vector& res, const vector >* ednsOpts, int& ret, bool* variable); typedef std::function ipfilter_t; ipfilter_t d_ipfilter; }; diff --git a/pdns/pdns_recursor.cc b/pdns/pdns_recursor.cc index c66a89956..7c8508f89 100644 --- a/pdns/pdns_recursor.cc +++ b/pdns/pdns_recursor.cc @@ -190,6 +190,7 @@ struct DNSComboWriter { int d_tag; string d_query; shared_ptr d_tcpConnection; + vector > d_ednsOpts; }; @@ -607,6 +608,7 @@ void startDoResolve(void *p) if(getEDNSOpts(dc->d_mdp, &edo)) { if(!dc->d_tcp) maxanswersize = min(edo.d_packetsize, g_udpTruncationThreshold); + dc->d_ednsOpts = edo.d_options; haveEDNS=true; } vector ret; @@ -705,7 +707,7 @@ void startDoResolve(void *p) } - if(!t_pdl->get() || !(*t_pdl)->preresolve(dc->d_remote, dc->d_local, dc->d_mdp.d_qname, QType(dc->d_mdp.d_qtype), ret, res, &variableAnswer)) { + if(!t_pdl->get() || !(*t_pdl)->preresolve(dc->d_remote, dc->d_local, dc->d_mdp.d_qname, QType(dc->d_mdp.d_qtype), ret, dc->d_ednsOpts.empty() ? 0 : &dc->d_ednsOpts, res, &variableAnswer)) { try { res = sr.beginResolve(dc->d_mdp.d_qname, QType(dc->d_mdp.d_qtype), dc->d_mdp.d_qclass, ret); } -- 2.40.0