[svn] commit: r1202 - in /branches/xfrin/src: bin/xfrin/xfrin.py.in lib/dns/message_python.cc

BIND 10 source code commits bind10-changes at lists.isc.org
Mon Mar 8 10:57:38 UTC 2010


Author: zhanglikun
Date: Mon Mar  8 10:57:37 2010
New Revision: 1202

Log:
Fix the the following two errors:
1. Insert the zone records at the end of AXFR, instead of inserting one record after parsing it from the package.(This is decided by the Evan's interface.)
2. Xfrin fails when AXFR response is returned in one TCP package, because the two SOA records are archived to one rrset. 
Woooooooo!, Xfrin can work now, but needs more unittest code.

Modified:
    branches/xfrin/src/bin/xfrin/xfrin.py.in
    branches/xfrin/src/lib/dns/message_python.cc

Modified: branches/xfrin/src/bin/xfrin/xfrin.py.in
==============================================================================
--- branches/xfrin/src/bin/xfrin/xfrin.py.in (original)
+++ branches/xfrin/src/bin/xfrin/xfrin.py.in Mon Mar  8 10:57:37 2010
@@ -69,12 +69,17 @@
         conn.finish_xfrin()              # finsih the xfrin
     '''
         
-    def __init__(self, zone_name, master_addr, port = 53, check_soa = True, idle_timeout = 60):
+    def __init__(self, zone_name, db_file, master_addr, 
+                 port = 53, 
+                 check_soa = True, 
+                 idle_timeout = 60):
         ''' idle_timeout: max idle time for read data from socket.
         '''
         asyncore.dispatcher.__init__(self)
         self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
         self._zone_name = zone_name
+        self._db_file = db_file
+        self._records = []
         self._idle_timeout = idle_timeout
         self.setblocking(1)
         self.connect((master_addr, port))
@@ -182,11 +187,15 @@
                 if ret == XFRIN_OK:
                     ret = self._handle_xfrin_response()
 
-            print('[xfrin] AXFR is started!')
+            self._insert_record_to_sqlite3(self._records)
+            print('[xfrin] AXFR finished!')
+ 
         except XfrinException as e:
             print(e)
+            print('[xfrin] Error happened during xfrin!')
         finally:
-            self.close()
+           self.close()
+
         return ret
     
     def _check_response_status(self, msg):
@@ -198,6 +207,32 @@
 
         if msg.get_rr_count(section.ANSWER()) == 0:
             raise XfrinException('answer section is empty')
+
+
+    def _handle_answer_section(self, rrset_iter):
+        soa_count = 0
+        while not rrset_iter.is_last():
+            rrset = rrset_iter.get_rrset()
+            rrset_iter.next()
+            rrset_name = rrset.get_name().to_text()
+            rrset_ttl = int(rrset.get_ttl().to_text())
+            rrset_class = rrset.get_class().to_text()
+            rrset_type = rrset.get_type().to_text()
+
+            rdata_iter = rrset.get_rdata_iterator()
+            rdata_iter.first()
+            while not rdata_iter.is_last():
+                # Count the soa record count
+                if rrset.get_type() == rr_type.SOA():
+                    soa_count += 1
+
+                rdata_text = rdata_iter.get_current().to_text()
+                rr_data = (rrset_name, rrset_ttl, rrset_class, rrset_type, rdata_text)
+                self._records.append(rr_data)
+                #self._insert_record_to_sqlite3(rr_data) 
+                rdata_iter.next()
+        
+        return soa_count 
 
 
     def _handle_xfrin_response(self):
@@ -211,33 +246,16 @@
             self._check_response_status(msg)
             
             rrset_iter = section_iter(msg, section.ANSWER())
-            while not rrset_iter.is_last():
-                rrset = rrset_iter.get_rrset()
-                rrset_iter.next()
-
-                rrset_name = rrset.get_name().to_text()
-                rrset_ttl = int(rrset.get_ttl().to_text())
-                rrset_class = rrset.get_class().to_text()
-                rrset_type = rrset.get_type().to_text()
-                if rrset_type == rr_type.SOA():
-                    soa_count += 1
-
-                if soa_count == 2:
-                    return XFRIN_OK
-
-                rdata_iter = rrset.get_rdata_iterator()
-                rdata_iter.first()
-                while not rdata_iter.is_last():
-                    rdata_text = rdata_iter.get_current().to_text()
-                    rr_data = [(rrset_name, rrset_ttl, rrset_class, rrset_type, rdata_text)]
-                    self._insert_record_to_sqlite3(rr_data) 
-                    rdata_iter.next()
+            soa_count += self._handle_answer_section(rrset_iter)
+            if soa_count == 2:
+                return XFRIN_OK
 
         return XFRIN_OK
 
-    def _insert_record_to_sqlite3(self, rr):
-        print(rr)
-        isc.auth.sqlite3_ds.load('sqlite3.db', self._zone_name, rr)
+    def _insert_record_to_sqlite3(self, rrs):
+        ''' The interface provided by sqlite3_ds only support insert all the records
+        at the end of AXFR'''
+        isc.auth.sqlite3_ds.load(self._db_file, self._zone_name, rrs)
 
     def handle_timeout(self):
         pass
@@ -250,10 +268,10 @@
         # Overwrite the log function, log nothing
         pass
 
-def process_xfrin(xfrin_recorder, zone_name, master_addr, port, check_soa):
+def process_xfrin(xfrin_recorder, zone_name, db_file, master_addr, port, check_soa):
     xfrin_recorder.increment(name)
     print(zone_name, master_addr, port, check_soa)
-    conn = XfrinConnection(zone_name, master_addr, int(port), check_soa)
+    conn = XfrinConnection(zone_name, db_file, master_addr, int(port), check_soa)
     conn.do_xfrin(False)
     xfrin_recorder.decrement(zone_name)
 
@@ -300,6 +318,10 @@
         full_config = self._cc.get_full_config()
         for item in full_config:
             print(item + ":" + str(full_config[item]))
+
+
+    def shutdown(self):
+        pass
 
 
     def command_handler(self, command, args):
@@ -362,16 +384,16 @@
             return (1, 'zone xfrin is in progress')
 
         xfrin_thread = threading.Thread(target = process_xfrin, 
-                         args = (self.recorder, zone_name, master_addr, port, check_soa))
+                         args = (self.recorder, zone_name, db_file, master_addr, port, check_soa))
         xfrin_thread.start()
         return (0, 'zone xfrin is started')
 
 
-httpd = None
+xfrind = None
 
 def signal_handler(signal, frame):
-    if httpd:
-        httpd.shutdown()
+    if xfrind:
+        xfrind.shutdown()
     sys.exit(0)
 
 def set_signal_handler():
@@ -396,15 +418,15 @@
 if __name__ == '__main__':
     try:
         set_signal_handler()
-        obj = Xfrin()
-        obj.startup()
+        xfrind = Xfrin()
+        xfrind.startup()
     except KeyboardInterrupt:
         print("exit http server")
     except Exception as e:
         print(e)
 
-    if httpd:
-        httpd.shutdown()
-
-
-
+    if xfrind:
+        xfrind.shutdown()
+
+
+

Modified: branches/xfrin/src/lib/dns/message_python.cc
==============================================================================
--- branches/xfrin/src/lib/dns/message_python.cc (original)
+++ branches/xfrin/src/lib/dns/message_python.cc Mon Mar  8 10:57:37 2010
@@ -12,7 +12,7 @@
 // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
 // PERFORMANCE OF THIS SOFTWARE.
 
-// $Id: exceptions.cc 476 2010-01-19 00:29:28Z jinmei $
+// $Id: message_python.cc 2010-03-08 18:44:00 feng $
 
 #include <string>
 #include <boost/python.hpp>
@@ -24,7 +24,7 @@
 #include <boost/python/copy_const_reference.hpp>
 #include <boost/shared_ptr.hpp>
 
-#include <exceptions/exceptions.h>
+#include "exceptions.h"
 #include "buffer.h"
 #include "name.h"
 #include "messagerenderer.h"
@@ -152,7 +152,8 @@
             RRsetList_iterator_wrapper(const RRsetList& list) : cur_(list.begin()), end_(list.end()){}
             RRsetPtr Next()
             {
-                if(cur_ == end_) {
+                if(cur_ == end_) 
+                {
                     PyErr_SetObject(PyExc_StopIteration, Py_None);
                     throw_error_already_set();
                 }
@@ -171,13 +172,13 @@
             RRsetList::const_iterator cur_;
             RRsetList::const_iterator end_;
     };
-    
+
     class Question_iterator_wrapper
     {
         public:
             Question_iterator_wrapper(const Message &message) : cur_(message.beginQuestion()), end_(message.endQuestion()){}
             QuestionPtr getQuestion() const { return *cur_;}
-            bool hasNext() {return cur_ != end_;}
+            bool isLast() {return cur_ == end_;}
             void next(){ ++cur_;}
         private:
             QuestionIterator cur_;
@@ -189,7 +190,7 @@
         public:
             Section_iterator_wrapper(const Message &message, const Section &section) : cur_(message.beginSection(section)), end_(message.endSection(section)){}
             RRsetPtr getRRset() const { return *cur_;}
-            bool hasNext() {return cur_ != end_;}
+            bool isLast() {return cur_ == end_;}
             void next(){ ++cur_;}
 
         private:
@@ -481,17 +482,17 @@
         .def("get_bit", &MessageFlag::getBit)
         .def("QR", &MessageFlag::QR, return_value_policy<copy_const_reference>())
         .staticmethod("QR")
-        .def("AA", &MessageFlag::QR, return_value_policy<copy_const_reference>())
+        .def("AA", &MessageFlag::AA, return_value_policy<copy_const_reference>())
         .staticmethod("AA")
-        .def("TC", &MessageFlag::QR, return_value_policy<copy_const_reference>())
+        .def("TC", &MessageFlag::TC, return_value_policy<copy_const_reference>())
         .staticmethod("TC")
-        .def("RD", &MessageFlag::QR, return_value_policy<copy_const_reference>())
+        .def("RD", &MessageFlag::RD, return_value_policy<copy_const_reference>())
         .staticmethod("RD")
-        .def("RA", &MessageFlag::QR, return_value_policy<copy_const_reference>())
+        .def("RA", &MessageFlag::RA, return_value_policy<copy_const_reference>())
         .staticmethod("RA")
-        .def("AD", &MessageFlag::QR, return_value_policy<copy_const_reference>())
+        .def("AD", &MessageFlag::AD, return_value_policy<copy_const_reference>())
         .staticmethod("AD")
-        .def("CD", &MessageFlag::QR, return_value_policy<copy_const_reference>())
+        .def("CD", &MessageFlag::CD, return_value_policy<copy_const_reference>())
         .staticmethod("CD");
       
        class_<Opcode>("op_code", no_init)
@@ -612,15 +613,15 @@
         .def("make_response", &Message::makeResponse)
         .def("to_wire", &Message::toWire)
         .def("from_wire", &Message::fromWire);
-    
+
     class_<Question_iterator_wrapper>("question_iter", init<const Message &>())
         .def("get_question", &Question_iterator_wrapper::getQuestion)
-        .def("has_next", &Question_iterator_wrapper::hasNext)
+        .def("is_last", &Question_iterator_wrapper::isLast)
         .def("next", &Question_iterator_wrapper::next);
 
     class_<Section_iterator_wrapper>("section_iter", init<const Message &, const Section &>())
         .def("get_rrset", &Section_iterator_wrapper::getRRset)
-        .def("has_next", &Section_iterator_wrapper::hasNext)
+        .def("is_last", &Section_iterator_wrapper::isLast)
         .def("next", &Section_iterator_wrapper::next);
 }
  




More information about the bind10-changes mailing list