]> git.ipfire.org Git - thirdparty/pdns.git/commitdiff
add a 'Ragel' based DNS TXT parser & hook it up. With this change we can finally...
authorBert Hubert <bert.hubert@netherlabs.nl>
Thu, 10 Feb 2011 13:01:24 +0000 (13:01 +0000)
committerBert Hubert <bert.hubert@netherlabs.nl>
Thu, 10 Feb 2011 13:01:24 +0000 (13:01 +0000)
Ragel is now a 'build-from-svn' dependency, but not 'build-from-tarball'.

git-svn-id: svn://svn.powerdns.com/pdns/trunk/pdns@2000 d19b8d6e-7fed-0310-83ef-9ca221ded41b

pdns/Makefile.am
pdns/backends/bind/Makefile.am
pdns/dnslabeltext.rl [new file with mode: 0644]
pdns/dnswriter.cc
pdns/dnswriter.hh

index 8d006b2be4efb4ddbbd6a0f546b3c0ef516ce624..f820976ba5a366c62f1cdf3c6158efb8474bae8d 100644 (file)
@@ -41,7 +41,7 @@ aes/aescrypt.c aes/aes.h aes/aeskey.c aes/aes_modes.c aes/aesopt.h \
 aes/aestab.c aes/aestab.h aes/brg_endian.h aes/brg_types.h aes/dns_random.cc \
 randomhelper.cc namespaces.hh nsecrecords.cc base32.cc dbdnsseckeeper.cc dnssecinfra.cc \
 dnsseckeeper.hh dnssecinfra.hh base32.hh dns.cc dnssecsigner.cc polarrsakeyinfra.cc md5.cc \
-md5.hh signingpipe.cc signingpipe.hh
+md5.hh signingpipe.cc signingpipe.hh dnslabeltext.cc
 
 #
 pdns_server_LDFLAGS=@moduleobjects@ @modulelibs@ @DYNLINKFLAGS@ @LIBDL@ @THREADFLAGS@  -Lext/polarssl/library 
@@ -72,7 +72,7 @@ pdnssec_SOURCES=pdnssec.cc dbdnsseckeeper.cc sstuff.hh dnsparser.cc dnsparser.hh
        backends/gsql/gsqlbackend.cc \
        backends/gsql/gsqlbackend.hh backends/gsql/ssql.hh zoneparser-tng.cc \
        dynlistener.cc dns.cc randombackend.cc dnssecsigner.cc polarrsakeyinfra.cc md5.cc \
-       signingpipe.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)
@@ -93,26 +93,26 @@ pdnssec_LDADD += -lcryptopp
 endif
 
 
-sdig_SOURCES=sdig.cc sstuff.hh dnsparser.cc dnsparser.hh dnsrecords.cc dnswriter.cc dnswriter.hh \
+sdig_SOURCES=sdig.cc sstuff.hh dnsparser.cc dnsparser.hh dnsrecords.cc dnswriter.cc dnslabeltext.cc dnswriter.hh \
        misc.cc misc.hh rcpgenerator.cc rcpgenerator.hh base64.cc base64.hh unix_utility.cc \
        logger.cc statbag.cc qtype.cc sillyrecords.cc nsecrecords.cc base32.cc
 
-toysdig_SOURCES=toysdig.cc sstuff.hh dnsparser.cc dnsparser.hh dnsrecords.cc dnswriter.cc dnswriter.hh \
+toysdig_SOURCES=toysdig.cc sstuff.hh dnsparser.cc dnsparser.hh dnsrecords.cc dnswriter.cc dnslabeltext.cc dnswriter.hh \
        misc.cc misc.hh rcpgenerator.cc rcpgenerator.hh base64.cc base64.hh unix_utility.cc \
        logger.cc statbag.cc qtype.cc sillyrecords.cc nsecrecords.cc base32.cc dnslabel.cc dnslabel.hh
 
 
-#tcptorture_SOURCES=tcptorture.cc sstuff.hh dnsparser.cc dnsparser.hh dnsrecords.cc dnswriter.cc dnswriter.hh \
+#tcptorture_SOURCES=tcptorture.cc sstuff.hh dnsparser.cc dnsparser.hh dnsrecords.cc dnswriter.cc dnslabeltext.cc dnswriter.hh \
 #      misc.cc misc.hh rcpgenerator.cc rcpgenerator.hh base64.cc base64.hh unix_utility.cc \
 #      logger.cc statbag.cc qtype.cc sillyrecords.cc nsecrecords.cc base32.cc
 
 
-tsig_tests_SOURCES=tsig-tests.cc sstuff.hh dnsparser.cc dnsparser.hh dnsrecords.cc dnswriter.cc dnswriter.hh \
+tsig_tests_SOURCES=tsig-tests.cc sstuff.hh dnsparser.cc dnsparser.hh dnsrecords.cc dnswriter.cc dnslabeltext.cc dnswriter.hh \
        misc.cc misc.hh rcpgenerator.cc rcpgenerator.hh base64.cc base64.hh unix_utility.cc \
        logger.cc statbag.cc qtype.cc sillyrecords.cc md5.cc
 
 
-speedtest_SOURCES=speedtest.cc dnsparser.cc dnsparser.hh dnsrecords.cc dnswriter.cc dnswriter.hh \
+speedtest_SOURCES=speedtest.cc dnsparser.cc dnsparser.hh dnsrecords.cc dnswriter.cc dnslabeltext.cc dnswriter.hh \
        misc.cc misc.hh rcpgenerator.cc rcpgenerator.hh base64.cc base64.hh unix_utility.cc \
        qtype.cc sillyrecords.cc logger.cc statbag.cc nsecrecords.cc base32.cc
 
@@ -122,7 +122,7 @@ dnswasher_SOURCES=dnswasher.cc misc.cc unix_utility.cc qtype.cc \
 dnswasher_LDFLAGS= @DYNLINKFLAGS@ @THREADFLAGS@
 
 dnsbulktest_SOURCES=dnsbulktest.cc misc.cc unix_utility.cc qtype.cc logger.cc statbag.cc dnsparser.cc \
-       dnsrecords.cc dnswriter.cc rcpgenerator.cc base32.cc base64.cc sillyrecords.cc nsecrecords.cc 
+       dnsrecords.cc dnswriter.cc dnslabeltext.cc rcpgenerator.cc base32.cc base64.cc sillyrecords.cc nsecrecords.cc 
 dnsbulktest_LDFLAGS= @DYNLINKFLAGS@ @THREADFLAGS@
 
 
@@ -130,12 +130,12 @@ dnsbulktest_LDFLAGS= @DYNLINKFLAGS@ @THREADFLAGS@
 
 dnsscan_SOURCES=dnsscan.cc misc.cc  qtype.cc anadns.hh \
        logger.cc statbag.cc  dnspcap.cc dnspcap.hh dnsparser.cc dnsrecords.cc dnsparser.hh  \
-       rcpgenerator.cc rcpgenerator.hh base64.cc base64.hh dnswriter.cc dnswriter.hh \
+       rcpgenerator.cc rcpgenerator.hh base64.cc base64.hh dnswriter.cc dnslabeltext.cc dnswriter.hh \
        unix_utility.cc utility.hh              
 
 dnslog_SOURCES=dnslog.cc misc.cc  qtype.cc anadns.hh \
        logger.cc statbag.cc  dnspcap.cc dnspcap.hh dnsparser.cc dnsrecords.cc dnsparser.hh  \
-       rcpgenerator.cc rcpgenerator.hh base64.cc base64.hh dnswriter.cc dnswriter.hh \
+       rcpgenerator.cc rcpgenerator.hh base64.cc base64.hh dnswriter.cc dnslabeltext.cc dnswriter.hh \
        unix_utility.cc utility.hh              
 
 #socketbench_SOURCES=socketbench.cc qtype.cc misc.cc unix_utility.cc\
@@ -143,21 +143,21 @@ dnslog_SOURCES=dnslog.cc misc.cc  qtype.cc anadns.hh \
 
 dnsreplay_SOURCES=dnsreplay.cc misc.cc  qtype.cc anadns.hh \
        logger.cc statbag.cc  dnspcap.cc dnspcap.hh dnsparser.cc dnsrecords.cc dnsparser.hh  \
-       rcpgenerator.cc rcpgenerator.hh base64.cc base64.hh dnswriter.cc dnswriter.hh \
+       rcpgenerator.cc rcpgenerator.hh base64.cc base64.hh dnswriter.cc dnslabeltext.cc dnswriter.hh \
        unix_utility.cc utility.hh sillyrecords.cc nsecrecords.cc base32.cc
 
 dnsreplay_LDFLAGS= @DYNLINKFLAGS@ @THREADFLAGS@ $(BOOST_PROGRAM_OPTIONS_LDFLAGS)
 dnsreplay_LDADD= $(BOOST_PROGRAM_OPTIONS_LIBS)
 
 nproxy_SOURCES=nproxy.cc dnsparser.cc dnsrecords.cc dnsparser.hh  \
-       rcpgenerator.cc rcpgenerator.hh base64.cc base64.hh dnswriter.cc dnswriter.hh \
+       rcpgenerator.cc rcpgenerator.hh base64.cc base64.hh dnswriter.cc dnslabeltext.cc dnswriter.hh \
        sillyrecords.cc selectmplexer.cc   mplexer.hh
 
 nproxy_LDFLAGS= @DYNLINKFLAGS@ @THREADFLAGS@ $(BOOST_PROGRAM_OPTIONS_LDFLAGS)
 nproxy_LDADD= $(BOOST_PROGRAM_OPTIONS_LIBS)
 
 notify_SOURCES=notify.cc dnsparser.cc dnsrecords.cc dnsparser.hh  \
-       rcpgenerator.cc rcpgenerator.hh base64.cc base64.hh dnswriter.cc dnswriter.hh \
+       rcpgenerator.cc rcpgenerator.hh base64.cc base64.hh dnswriter.cc dnslabeltext.cc dnswriter.hh \
        sillyrecords.cc selectmplexer.cc nsecrecords.cc base32.cc misc.cc unix_utility.cc \
        logger.cc qtype.cc statbag.cc
 
@@ -166,19 +166,19 @@ notify_LDADD= $(BOOST_PROGRAM_OPTIONS_LIBS)
 
 dnsscope_SOURCES=dnsscope.cc misc.cc unix_utility.cc qtype.cc \
        logger.cc statbag.cc  dnspcap.cc dnspcap.hh dnsparser.cc dnsrecords.cc \
-       base64.cc base64.hh dnswriter.cc dnswriter.hh rcpgenerator.cc rcpgenerator.hh \
+       base64.cc base64.hh dnswriter.cc dnslabeltext.cc dnswriter.hh rcpgenerator.cc rcpgenerator.hh \
        utility.hh dnsparser.hh sillyrecords.cc nsecrecords.cc base32.cc
 
 dnsscope_LDFLAGS= @DYNLINKFLAGS@ @THREADFLAGS@
 
 dnsgram_SOURCES=dnsgram.cc misc.cc unix_utility.cc qtype.cc \
        logger.cc statbag.cc  dnspcap.cc dnspcap.hh dnsparser.cc dnsrecords.cc \
-       base64.cc base64.hh dnswriter.cc dnswriter.hh rcpgenerator.cc rcpgenerator.hh \
+       base64.cc base64.hh dnswriter.cc dnslabeltext.cc dnswriter.hh rcpgenerator.cc rcpgenerator.hh \
        utility.hh dnsparser.hh sillyrecords.cc nsecrecords.cc base32.cc
 
 dnsdemog_SOURCES=dnsdemog.cc misc.cc unix_utility.cc qtype.cc \
        logger.cc statbag.cc  dnspcap.cc dnspcap.hh dnsparser.cc dnsrecords.cc \
-       base64.cc base64.hh dnswriter.cc dnswriter.hh rcpgenerator.cc rcpgenerator.hh \
+       base64.cc base64.hh dnswriter.cc dnslabeltext.cc dnswriter.hh rcpgenerator.cc rcpgenerator.hh \
        utility.hh dnsparser.hh sillyrecords.cc
 
 rec_control_SOURCES=rec_channel.cc rec_channel.hh rec_control.cc arguments.cc arguments.hh misc.cc qtype.cc \
@@ -187,7 +187,7 @@ rec_control_SOURCES=rec_channel.cc rec_channel.hh rec_control.cc arguments.cc ar
 pdns_recursor_SOURCES=syncres.cc resolver.hh misc.cc unix_utility.cc qtype.cc \
 logger.cc statbag.cc arguments.cc  lwres.cc pdns_recursor.cc reczones.cc lwres.hh \
 mtasker.hh syncres.hh recursor_cache.cc recursor_cache.hh dnsparser.cc \
-dnswriter.cc dnswriter.hh dnsrecords.cc dnsrecords.hh rcpgenerator.cc rcpgenerator.hh \
+dnswriter.cc dnslabeltext.cc dnswriter.hh dnsrecords.cc dnsrecords.hh rcpgenerator.cc rcpgenerator.hh \
 base64.cc base64.hh zoneparser-tng.cc zoneparser-tng.hh rec_channel.cc rec_channel.hh \
 rec_channel_rec.cc selectmplexer.cc epollmplexer.cc sillyrecords.cc htimer.cc htimer.hh \
 aes/dns_random.cc aes/aescrypt.c aes/aeskey.c aes/aestab.c aes/aes_modes.c \
@@ -205,6 +205,6 @@ misc.cc unix_utility.cc qtype.cc
 pdns_control_INCLUDES=path.hh
 pdns_control_LDFLAGS=@THREADFLAGS@
 
-# binpatch_SOURCES=binpatch.cc 
-
-
+dnslabeltext.cc: dnslabeltext.rl
+       ragel dnslabeltext.rl -o dnslabeltext.cc
+       
\ No newline at end of file
index 6093832976b606fd3f3a09811fc8615a6e8e5e7d..94e1def806bb315066c638455bae38bb55f53fb5 100644 (file)
@@ -18,13 +18,13 @@ zone2sql_SOURCES=bindparser.yy bindlexer.l \
 ../../arguments.cc ../../logger.cc zone2sql.cc ../../statbag.cc ../../misc.cc \
 ../../unix_utility.cc ../../qtype.cc ../../dns.cc \
 ../../zoneparser-tng.cc ../../dnsrecords.cc ../../sillyrecords.cc \
-../../dnswriter.cc ../../rcpgenerator.cc ../../dnsparser.cc ../../base64.cc \
+../../dnswriter.cc ../../dnslabeltext.cc ../../rcpgenerator.cc ../../dnsparser.cc ../../base64.cc \
 ../../nsecrecords.cc ../../dnssecinfra.cc  ../../base32.cc # ../../dbdnsseckeeper.cc
 
 zone2ldap_SOURCES=bindparser.yy bindlexer.l \
 ../../arguments.cc ../../logger.cc zone2ldap.cc ../../statbag.cc ../../misc.cc \
 ../../unix_utility.cc ../../qtype.cc  ../../zoneparser-tng.cc ../../dnsrecords.cc \
-../../dnswriter.cc ../../rcpgenerator.cc ../../dnsparser.cc ../../base64.cc ../../sillyrecords.cc \
+../../dnswriter.cc ../../dnslabeltext.cc  ../../rcpgenerator.cc ../../dnsparser.cc ../../base64.cc ../../sillyrecords.cc \
 ../../nsecrecords.cc ../../dnssecinfra.cc  ../../base32.cc # ../../dbdnsseckeeper.cc
 
 zone2ldap_LDFLAGS=@THREADFLAGS@  -L../../ext/polarssl/library
diff --git a/pdns/dnslabeltext.rl b/pdns/dnslabeltext.rl
new file mode 100644 (file)
index 0000000..65f5bee
--- /dev/null
@@ -0,0 +1,92 @@
+#include <stdlib.h>
+#include <string.h>
+#include <stdio.h>
+#include <unistd.h>
+#include <string>
+#include "namespaces.hh"
+
+%%{
+        machine dnstext;
+        write data;
+}%%
+
+namespace {
+void appendSplit(vector<string>& ret, string& segment, char c)
+{
+  if(segment.size()>254) {
+    ret.push_back(segment);
+    segment.clear();
+  }
+  segment.append(1, c);
+}
+}
+
+vector<string> segmentDNSText(const string& input )
+{
+        const char *p = input.c_str(), *pe = input.c_str() + input.length();
+        const char* eof = pe;
+        int cs;
+        char val = 0;
+
+        string segment;
+        vector<string> ret;
+
+        %%{
+                action segmentEnd { 
+                        ret.push_back(segment);
+                        segment.clear();
+                }
+                action segmentBegin { 
+                        segment.clear();
+                }
+
+                action reportEscaped {
+                  char c = *fpc;
+                  appendSplit(ret, segment, c);
+                }
+                action reportEscapedNumber {
+                  char c = *fpc;
+                  val *= 10;
+                  val += c-'0';
+                  
+                }
+                action doneEscapedNumber {
+                  appendSplit(ret, segment, val);
+                  val=0;
+                }
+                
+                action reportPlain {
+                  appendSplit(ret, segment, *(fpc));
+                }
+
+                escaped = '\\' ((["\\]@reportEscaped) | ([0-9]{3}$reportEscapedNumber%doneEscapedNumber));
+                plain = (print-'\\'-'"') $ reportPlain;
+                labelElement = escaped | plain;
+                
+
+                main := (('"' labelElement* '"' space?) >segmentBegin %segmentEnd)+;
+
+                # Initialize and execute.
+                write init;
+                write exec;
+        }%%
+
+        if ( cs < dnstext_first_final ) {
+                throw runtime_error("Unable to parse DNS TXT '"+input+"'");
+        }
+
+        return ret;
+};
+
+#if 0
+int main()
+{
+       //char blah[]="\"blah\" \"bleh\" \"bloeh\\\"bleh\" \"\\97enzo\"";
+  char blah[]="\"v=spf1 ip4:67.106.74.128/25 ip4:63.138.42.224/28 ip4:65.204.46.224/27 \\013\\010ip4:66.104.217.176/28 \\013\\010ip4:209.48.147.0/27 ~all\"";
+  //char blah[]="\"abc \\097\\098 def\"";
+  printf("Input: '%s'\n", blah);
+       vector<string> res=dnstext(blah);
+  cerr<<res.size()<<" segments"<<endl;
+  cerr<<res[0]<<endl;
+}
+#endif
index ddfd19cc2dec294d4b90f6082872a9f21117bebe..8b9283e4069fe1edc37c6591bdb3f120137a6830 100644 (file)
@@ -1,8 +1,7 @@
 #include "dnswriter.hh"
 #include "misc.hh"
 #include "dnsparser.hh"
-#include <boost/tokenizer.hpp>
-#include <boost/algorithm/string.hpp>
+#include <boost/foreach.hpp>
 #include <limits.h>
 
 DNSPacketWriter::DNSPacketWriter(vector<uint8_t>& content, const string& qname, uint16_t  qtype, uint16_t qclass, uint8_t opcode)
@@ -135,30 +134,26 @@ void DNSPacketWriter::xfr8BitInt(uint8_t val)
   d_record.push_back(val);
 }
 
+
+/* input: 
+  "" -> 0
+  "blah" -> 4blah
+  "blah" "blah" -> output 4blah4blah
+  "verylongstringlongerthan256....characters" \xffverylongstring\x23characters (autosplit)
+  "blah\"blah" -> 9blah"blah
+  "blah\97" -> 5blahb
+  */
 void DNSPacketWriter::xfrText(const string& text, bool)
 {
-  using boost::escaped_list_separator;
-  using boost::tokenizer;
-
-  escaped_list_separator<char> sep('\\', ' ' , '"');
-  tokenizer<escaped_list_separator<char> > tok(text, sep);
-
-  tokenizer<escaped_list_separator<char> >::iterator beg=tok.begin();
-
-  if(beg==tok.end()) {
+  if(text.empty()) {
     d_record.push_back(0);
+    return;
+  }
+  vector<string> segments = segmentDNSText(text);
+  BOOST_FOREACH(const string& str, segments) {
+    d_record.push_back(str.length());
+    d_record.insert(d_record.end(), str.c_str(), str.c_str() + str.length());
   }
-  else 
-    for(; beg!=tok.end(); ++beg){
-      if(beg->empty()) 
-        d_record.push_back(0);
-      else 
-        for (unsigned int i = 0; i < beg->length(); i += 0xff){
-          d_record.push_back(std::min((string::size_type)0xffU, beg->length()-i));
-          const uint8_t* ptr=(uint8_t*)(beg->c_str()) + i;
-          d_record.insert(d_record.end(), ptr, ptr+ std::min((string::size_type)0xffU, beg->length()-i));
-        }
-    }
 }
 
 DNSPacketWriter::lmap_t::iterator find(DNSPacketWriter::lmap_t& lmap, const string& label)
index 3e97bd32a5f24f13f93b260ec6307047cc0a6367..a395edf8304e1fb88aca10c1a065764bc127f6f0 100644 (file)
@@ -124,4 +124,5 @@ private:
 
 typedef vector<pair<string::size_type, string::size_type> > labelparts_t;
 bool labeltokUnescape(labelparts_t& parts, const string& label);
+std::vector<string> segmentDNSText(const string& text); // from dnslabeltext.rl
 #endif