]> granicus.if.org Git - pdns/commitdiff
remove our old DNSResourceRecord serializer, replace it with the boost one, plus...
authorBert Hubert <bert.hubert@netherlabs.nl>
Fri, 4 Mar 2011 20:43:28 +0000 (20:43 +0000)
committerBert Hubert <bert.hubert@netherlabs.nl>
Fri, 4 Mar 2011 20:43:28 +0000 (20:43 +0000)
git-svn-id: svn://svn.powerdns.com/pdns/trunk/pdns@2059 d19b8d6e-7fed-0310-83ef-9ca221ded41b

pdns/Makefile.am
pdns/dns.hh
pdns/qtype.cc
pdns/qtype.hh
pdns/ueberbackend.cc
pdns/ueberbackend.hh

index 7dadaa2c5a38d768f3c952796d87c7fe367ab43f..918e1367cb5632ecb49ba9e62fa847b14974757a 100644 (file)
@@ -44,8 +44,8 @@ dnsseckeeper.hh dnssecinfra.hh base32.hh dns.cc dnssecsigner.cc polarrsakeyinfra
 md5.hh signingpipe.cc signingpipe.hh dnslabeltext.cc
 
 #
-pdns_server_LDFLAGS=@moduleobjects@ @modulelibs@ @DYNLINKFLAGS@ @LIBDL@ @THREADFLAGS@  -Lext/polarssl/library 
-pdns_server_LDADD= -lpolarssl
+pdns_server_LDFLAGS=@moduleobjects@ @modulelibs@ @DYNLINKFLAGS@ @LIBDL@ @THREADFLAGS@  -Lext/polarssl/library $(BOOST_SERIALIZATION_LDFLAGS) 
+pdns_server_LDADD= -lpolarssl $(BOOST_SERIALIZATION_LIBS) 
 
 if BOTAN19
 pdns_server_SOURCES += botan19signers.cc botansigners.cc
@@ -74,8 +74,8 @@ pdnssec_SOURCES=pdnssec.cc dbdnsseckeeper.cc sstuff.hh dnsparser.cc dnsparser.hh
        dynlistener.cc dns.cc randombackend.cc dnssecsigner.cc polarrsakeyinfra.cc md5.cc \
        signingpipe.cc dnslabeltext.cc
 
-pdnssec_LDFLAGS=@moduleobjects@ @modulelibs@ @DYNLINKFLAGS@ @LIBDL@ @THREADFLAGS@ -Lext/polarssl/library/ $(BOOST_PROGRAM_OPTIONS_LDFLAGS) 
-pdnssec_LDADD= -lpolarssl $(BOOST_PROGRAM_OPTIONS_LIBS)
+pdnssec_LDFLAGS=@moduleobjects@ @modulelibs@ @DYNLINKFLAGS@ @LIBDL@ @THREADFLAGS@ -Lext/polarssl/library/ $(BOOST_PROGRAM_OPTIONS_LDFLAGS) $(BOOST_SERIALIZATION_LDFLAGS)
+pdnssec_LDADD= -lpolarssl $(BOOST_PROGRAM_OPTIONS_LIBS) $(BOOST_SERIALIZATION_LIBS)
 
 if BOTAN19
 pdnssec_SOURCES += botan19signers.cc botansigners.cc
index 050d2b7bbc6204a21852fc856b94ac874ad1880d..044a2d108e4ba72be9413efc149a792220c96509 100644 (file)
 #include <boost/tuple/tuple_comparison.hpp>
 #include <boost/multi_index/key_extractors.hpp>
 #include <boost/multi_index/sequenced_index.hpp>
+
+#include <boost/serialization/vector.hpp>
+#include <boost/serialization/string.hpp>
+#include <boost/serialization/version.hpp>
+
+
 #include "utility.hh"
 #include "qtype.hh"
 #include <time.h>
@@ -66,9 +72,6 @@ public:
   DNSResourceRecord() : qclass(1), priority(0), last_modified(0), d_place(ANSWER) {};
   ~DNSResourceRecord(){};
 
-  string serialize() const;
-  int unSerialize(const string &str);
-
   // data
   
   QType qtype; //!< qtype of this record, ie A, CNAME, MX etc
@@ -76,7 +79,7 @@ public:
   string qname; //!< the name of this record, for example: www.powerdns.com
   string wildcardname;
   string content; //!< what this record points to. Example: 10.1.2.3
-  uint16_t priority; //!< For qtype's that support a priority or preference. Currently only MX
+  uint16_t priority; //!< For qtypes that support a priority or preference (MX, SRV)
   uint32_t ttl; //!< Time To Live of this record
   int domain_id; //!< If a backend implements this, the domain_id of the zone this record is in
   time_t last_modified; //!< For autocalculating SOA serial numbers - the backend needs to fill this in
@@ -85,6 +88,22 @@ public:
 
   bool auth;
 
+  template<class Archive>
+  void serialize(Archive & ar, const unsigned int version)
+  {
+    ar & qtype;
+    ar & qclass;
+    ar & qname;
+    ar & wildcardname;
+    ar & content;
+    ar & priority;
+    ar & ttl;
+    ar & domain_id;
+    ar & last_modified;
+    ar & d_place;
+    ar & auth;
+  }
+
   bool operator<(const DNSResourceRecord &b) const
   {
     if(qname < b.qname)
@@ -93,9 +112,6 @@ public:
       return(content < b.content);
     return false;
   }
-
-private:
-  string escape(const string &str) const;
 };
 
 #ifdef _MSC_VER
index 55e5aaea4d648110e78a9c9e02703b3768d7aacc..4aadb4d33e674d7e34e2c2ab3b1518fe37facef3 100644 (file)
@@ -133,97 +133,3 @@ QType::QType(uint16_t n)
   QType();
   code=n;
 }
-
-string DNSResourceRecord::serialize() const
-{
-  ostringstream ostr;
-  ostr<<escape(qname)<<"|"<<qtype.getName()<<"|"<<escape(content)<<"|"<<ttl<<"|"<<priority<<"|"<<domain_id
-     <<"|"<<last_modified;
-  return ostr.str();
-}
-
-string DNSResourceRecord::escape(const string &name) const
-{
-  string a;
-
-  for(string::const_iterator i=name.begin();i!=name.end();++i)
-    if(*i=='|' || *i=='\\'){
-      a+='\\';
-      a+=*i;
-    }
-    else
-      a+=*i;
-
-  return a;
-}
-
-int DNSResourceRecord::unSerialize(const string &source)
-{
-  // qname|qtype|content|ttl|priority|domain_id|last_modified;
-  string chunk;
-  unsigned int m=0;
-  for(int n=0;n<7;++n) {
-    chunk="";
-    for(;m<source.size();++m) {
-      if(source[m]=='\\' && m+1<source.size()) 
-        chunk.append(1,source[++m]);
-      else if(source[m]=='|') {
-        ++m;
-        break;
-      }
-      else 
-        chunk.append(1,source[m]);
-    }
-    switch(n) {
-    case 0:
-      qname=chunk;
-      break;
-    case 1:
-      qtype=chunk;
-      break;
-    case 2:
-      content=chunk;
-      break;
-    case 3:
-      ttl=atoi(chunk.c_str());
-      break;
-    case 4:
-      priority=atoi(chunk.c_str());
-      break;
-    case 5:
-      domain_id=atoi(chunk.c_str());
-      break;
-    case 6:
-      last_modified=atoi(chunk.c_str());
-      break;
-    }
-  }
-  return m;
-}
-
-
-#if 0
-int main(int argc, char **argv)
-{
-  QType t;
-
-  cout << endl;
-  cout << "Assiging a '6'" << endl;
-  t=6;
-  cout << "Code is now " << t.getCode() << endl;
-  cout << "Name is now " << t.getName() << endl;
-
-  cout << endl;
-
-  cout << "Assiging a 'CNAME'" << endl;
-  t="CNAME";
-  cout << "Code is now " << t.getCode() << endl;
-  cout << "Name is now " << t.getName() << endl;
-
-  QType u;
-  u="SOA";
-  cout << u.getCode() << endl;
-
-
-}
-#endif
index 611982532e14db4646658ffc8045b0fe7ef37482..913df96c914c49c2109e9d7117b66a07f3053036 100644 (file)
@@ -23,7 +23,6 @@
 #include <string>
 #include <vector>
 #include <utility>
-
 #include "namespaces.hh"
 
 /** The QType class is meant to deal easily with the different kind of resource types, like 'A', 'NS',
@@ -64,6 +63,12 @@ public:
     return code < rhs.code;
   }
 
+  template<class Archive>
+  void serialize(Archive &ar, const unsigned int version)
+  {
+    ar & code;
+  }
+
   bool operator==(const QType &) const; //!< equality operator
 
   const string getName() const; //!< Get a string representation of this type
index 993b582087a3576499d0708b59106014633d21ab..3b4d296cf55fcca6e530b1bdd382322d11151570 100644 (file)
@@ -15,6 +15,9 @@
     along with this program; if not, write to the Free Software
     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */
+#include <boost/archive/binary_iarchive.hpp>
+#include <boost/archive/binary_oarchive.hpp>
+
 #include "packetcache.hh"
 #include "utility.hh"
 
 #include <string>
 #include <map>
 #include <sys/types.h>
-
+#include <sstream>
 #include <errno.h>
 #include <iostream>
 #include <sstream>
 #include <functional>
 #include <boost/foreach.hpp>
-
 #include "dns.hh"
 #include "arguments.hh"
 #include "dnsbackend.hh"
@@ -219,15 +221,14 @@ bool UeberBackend::getSOA(const string &domain, SOAData &sd, DNSPacket *p)
   d_question.zoneId=-1;
     
   if(sd.db!=(DNSBackend *)-1) {
-    int cstat=cacheHas(d_question,d_answer);
-    if(cstat==0) {
+    int cstat=cacheHas(d_question,d_answers);
+    if(cstat==0) { // negative
       return false;
     }
-    else if(cstat==1) {
-      // ehm 
-      fillSOAData(d_answer.content,sd);
-      sd.domain_id=d_answer.domain_id;
-      sd.ttl=d_answer.ttl;
+    else if(cstat==1 && !d_answers.empty()) {
+      fillSOAData(d_answers[0].content,sd);
+      sd.domain_id=d_answers[0].domain_id;
+      sd.ttl=d_answers[0].ttl;
       sd.db=0;
       return true;
     }
@@ -241,7 +242,9 @@ bool UeberBackend::getSOA(const string &domain, SOAData &sd, DNSPacket *p)
       rr.content=serializeSOAData(sd);
       rr.ttl=sd.ttl;
       rr.domain_id=sd.domain_id;
-      addOneCache(d_question,rr);
+      vector<DNSResourceRecord> rrs;
+      rrs.push_back(rr);
+      addCache(d_question, rrs);
       return true;
     }
 
@@ -301,7 +304,8 @@ void UeberBackend::cleanup()
 // silly Solaris fix
 #undef PC
 
-int UeberBackend::cacheHas(const Question &q, DNSResourceRecord &rr)
+// returns -1 for miss, 0 for negative match, 1 for hit
+int UeberBackend::cacheHas(const Question &q, vector<DNSResourceRecord> &rrs)
 {
   extern PacketCache PC;
   static unsigned int *qcachehit=S.getPointer("query-cache-hit");
@@ -318,8 +322,7 @@ int UeberBackend::cacheHas(const Question &q, DNSResourceRecord &rr)
   string content;
   //  L<<Logger::Warning<<"looking up: '"<<q.qname+"'|N|"+q.qtype.getName()+"|"+itoa(q.zoneId)<<endl;
 
-  bool ret=PC.getEntry(q.qname, q.qtype, PacketCache::QUERYCACHE, content, q.zoneId);   // think about lowercasing here
-
+  bool ret=PC.getEntry(q.qname, q.qtype, PacketCache::QUERYCACHE, content, -1);   // think about lowercasing here
   if(!ret) {
     (*qcachemiss)++;
     return -1;
@@ -327,7 +330,11 @@ int UeberBackend::cacheHas(const Question &q, DNSResourceRecord &rr)
   (*qcachehit)++;
   if(content.empty()) // negatively cached
     return 0;
-  rr.unSerialize(content);
+  
+  std::istringstream istr(content);
+  boost::archive::binary_iarchive boa(istr);
+  
+  boa >> rrs;
   return 1;
 }
 
@@ -337,18 +344,22 @@ void UeberBackend::addNegCache(const Question &q)
   static int negqueryttl=::arg().asNum("negquery-cache-ttl");
   if(!negqueryttl)
     return;
-  //  L<<Logger::Warning<<"negative inserting: "<<q.qname+"|N|"+q.qtype.getName()+"|"+itoa(q.zoneId)<<endl;
-  PC.insert(q.qname, q.qtype, PacketCache::QUERYCACHE, "", negqueryttl, q.zoneId);
+  PC.insert(q.qname, q.qtype, PacketCache::QUERYCACHE, "", negqueryttl, -1);
 }
 
-void UeberBackend::addOneCache(const Question &q, const DNSResourceRecord &rr)
+void UeberBackend::addCache(const Question &q, const vector<DNSResourceRecord> &rrs)
 {
   extern PacketCache PC;
   static int queryttl=::arg().asNum("query-cache-ttl");
   if(!queryttl)
     return;
+  
   //  L<<Logger::Warning<<"inserting: "<<q.qname+"|N|"+q.qtype.getName()+"|"+itoa(q.zoneId)<<endl;
-  PC.insert(q.qname, q.qtype, PacketCache::QUERYCACHE, rr.serialize(), queryttl, q.zoneId);
+  std::ostringstream ostr;
+  boost::archive::binary_oarchive boa(ostr);
+  
+  boa << rrs;
+  PC.insert(q.qname, q.qtype, PacketCache::QUERYCACHE, ostr.str(), queryttl, -1);
 }
 
 void UeberBackend::alsoNotifies(const string &domain, set<string> *ips)
@@ -396,8 +407,8 @@ void UeberBackend::lookup(const QType &qtype,const string &qname, DNSPacket *pkt
   else {
     d_question.qtype=qtype;
     d_question.qname=qname;
-    d_question.zoneId=zoneId;
-    int cstat=cacheHas(d_question,d_answer);
+    d_question.zoneId=-1;
+    int cstat=cacheHas(d_question, d_answers);
     if(cstat<0) { // nothing
       d_negcached=d_cached=false;
       (d_handle.d_hinterBackend=backends[d_handle.i++])->lookup(qtype, qname,pkt_p,zoneId);
@@ -405,15 +416,16 @@ void UeberBackend::lookup(const QType &qtype,const string &qname, DNSPacket *pkt
     else if(cstat==0) {
       d_negcached=true;
       d_cached=false;
+      d_answers.clear();
     }
     else {
       d_negcached=false;
       d_cached=true;
+      d_cachehandleiter = d_answers.begin();
     }
   }
 
   d_handle.parent=this;
-
 }
 
 bool UeberBackend::get(DNSResourceRecord &rr)
@@ -423,24 +435,22 @@ bool UeberBackend::get(DNSResourceRecord &rr)
   }
 
   if(d_cached) {
-    rr=d_answer;
-    d_negcached=true; // ugly, confusing 
-    return true;
+    if(d_cachehandleiter != d_answers.end()) {
+      rr=*d_cachehandleiter++;;
+      return true;
+    }
+    return false;
   }
   if(!d_handle.get(rr)) {
     if(!d_ancount && !d_handle.qname.empty()) // don't cache axfr
       addNegCache(d_question);
 
-    if(d_ancount==1) {
-      addOneCache(d_question, lastrr);
-    }
-
+    addCache(d_question, d_answers);
+    d_answers.clear();
     return false;
   }
-
-  if(!d_ancount++) {
-    lastrr=rr;
-  }
+  d_ancount++;
+  d_answers.push_back(rr);
   return true;
 }
 
index 962e3a60ad783919051e3128c7120b42f842b108..ea8dfbb749d5ac3d1d7d587b0f7f7c9305a044b9 100644 (file)
@@ -149,11 +149,12 @@ private:
     string qname;
     int zoneId;
   }d_question;
-  DNSResourceRecord d_answer;
+  vector<DNSResourceRecord> d_answers;
+  vector<DNSResourceRecord>::const_iterator d_cachehandleiter;
 
-  int cacheHas(const Question &q, DNSResourceRecord &rr);
+  int cacheHas(const Question &q, vector<DNSResourceRecord> &rrs);
   void addNegCache(const Question &q);
-  void addOneCache(const Question &q, const DNSResourceRecord &rr);
+  void addCache(const Question &q, const vector<DNSResourceRecord> &rrs);
   
   static pthread_mutex_t d_mut;
   static pthread_cond_t d_cond;