]> granicus.if.org Git - pdns/commitdiff
hook up EDNS options infra to lua, provide getEDNSOptions to get all of them or getDN...
authorbert hubert <bert.hubert@netherlabs.nl>
Tue, 26 Jan 2016 10:09:43 +0000 (11:09 +0100)
committerbert hubert <bert.hubert@netherlabs.nl>
Tue, 26 Jan 2016 10:09:43 +0000 (11:09 +0100)
pdns/lua-recursor4.cc
pdns/lua-recursor4.hh
pdns/pdns_recursor.cc

index a0c999b56de832b501f29c26cb7fb74abcc51d4c..42bf13b4d698ab686ae69add8b73b12e121bf131 100644 (file)
@@ -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<DNSRecord>& ret, const vector<pair<uint8_t,string> >* ednsOpts, int& res, bool* variable)
+bool RecursorLua4::preresolve(const ComboAddress& remote, const ComboAddress& local, const DNSName& query, const QType& qtype, vector<DNSRecord>& ret, const vector<pair<uint16_t,string> >* ednsOpts, int& res, bool* variable)
 {
   return false;
 }
@@ -127,6 +127,25 @@ static int getFakePTRRecords(const DNSName& qname, const std::string& prefix, ve
 
 }
 
+vector<pair<uint16_t, string> > RecursorLua4::DNSQuestion::getEDNSOptions()
+{
+  if(ednsOptions)
+    return *ednsOptions;
+  else
+    return vector<pair<uint16_t,string>>();
+}
+
+boost::optional<string>  RecursorLua4::DNSQuestion::getEDNSOption(uint16_t code)
+{
+  if(ednsOptions)
+    for(const auto& o : *ednsOptions)
+      if(o.first==code)
+        return o.second;
+        
+  return boost::optional<string>();
+}
+
+
 vector<pair<int, DNSRecord> > RecursorLua4::DNSQuestion::getRecords()
 {
   vector<pair<int, DNSRecord> > 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<string(DNSRecord::*)()>("getContent", [](const DNSRecord& dr) { return dr.d_content->getZoneRepresentation(); });
 
@@ -335,9 +356,9 @@ RecursorLua4::RecursorLua4(const std::string& fname)
   d_gettag = d_lw->readVariable<boost::optional<gettag_t>>("gettag").get_value_or(0);
 }
 
-bool RecursorLua4::preresolve(const ComboAddress& remote,const ComboAddress& local, const DNSName& query, const QType& qtype, vector<DNSRecord>& res, const vector<pair<uint8_t,string> >* ednsOpts, int& ret, bool* variable)
+bool RecursorLua4::preresolve(const ComboAddress& remote,const ComboAddress& local, const DNSName& query, const QType& qtype, vector<DNSRecord>& res, const vector<pair<uint16_t,string> >* 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<DNSRecord>& 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<DNSRecord>& res, const vector<pair<uint8_t,string> >* ednsOpts, int& ret, bool* variable)
+bool RecursorLua4::genhook(luacall_t& func, const ComboAddress& remote,const ComboAddress& local, const DNSName& query, const QType& qtype, vector<DNSRecord>& res, const vector<pair<uint16_t,string> >* 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
 
index 67ccafeade78b0c4eeddb998c80d29928962a3e7..94c8d052a398c4f72242d21d7458c6fbb62a02a1 100644 (file)
@@ -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<DNSRecord>& res, const vector<pair<uint8_t,string> >* ednsOpts, int& ret, bool* variable);
+  bool preresolve(const ComboAddress& remote,const ComboAddress& local, const DNSName& query, const QType& qtype, vector<DNSRecord>& res, const vector<pair<uint16_t,string> >* ednsOpts, int& ret, bool* variable);
   bool nxdomain(const ComboAddress& remote, const ComboAddress& local, const DNSName& query, const QType& qtype, vector<DNSRecord>& res, int& ret, bool* variable);
   bool nodata(const ComboAddress& remote, const ComboAddress& local, const DNSName& query, const QType& qtype, vector<DNSRecord>& res, int& ret, bool* variable);
   bool postresolve(const ComboAddress& remote, const ComboAddress& local, const DNSName& query, const QType& qtype, vector<DNSRecord>& res, int& ret, bool* variable);
@@ -37,6 +37,8 @@ private:
     void addAnswer(uint16_t type, const std::string& content, boost::optional<int> ttl, boost::optional<string> name);
     void addRecord(uint16_t type, const std::string& content, DNSResourceRecord::Place place, boost::optional<int> ttl, boost::optional<string> name);
     vector<pair<int,DNSRecord> > getRecords();
+    vector<pair<uint16_t, string> > getEDNSOptions();
+    boost::optional<string> getEDNSOption(uint16_t code);
     void setRecords(const vector<pair<int,DNSRecord> >& records);
     bool variable{false};
     
@@ -49,14 +51,14 @@ private:
     string udpCallback;
     
     std::unordered_map<string,string> data;
-    std::vector<pair<uint16_t, string>>* ednsOptions;
+    const std::vector<pair<uint16_t, string>>* ednsOptions;
     DNSName followupName;
   };
 
   LuaContext* d_lw;
   typedef std::function<bool(std::shared_ptr<DNSQuestion>)> 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<DNSRecord>& res, const vector<pair<uint8_t,string> >* ednsOpts, int& ret, bool* variable);
+  bool genhook(luacall_t& func, const ComboAddress& remote,const ComboAddress& local, const DNSName& query, const QType& qtype, vector<DNSRecord>& res, const vector<pair<uint16_t,string> >* ednsOpts, int& ret, bool* variable);
   typedef std::function<bool(ComboAddress,ComboAddress, struct dnsheader)> ipfilter_t;
   ipfilter_t d_ipfilter;
 };
index c66a899568f407b510a17c32c3821f95eadfce01..7c8508f898cb4f9b22b540b32ee24ced0bdfd7d9 100644 (file)
@@ -190,6 +190,7 @@ struct DNSComboWriter {
   int d_tag;
   string d_query;
   shared_ptr<TCPConnection> d_tcpConnection;
+  vector<pair<uint16_t, string> > 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<DNSRecord> 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);
       }