@@ -83,13 +83,13 @@ def emit(self, record):
83
83
_DNS_TTL = 60 * 60 # one hour default TTL
84
84
85
85
_MAX_MSG_TYPICAL = 1460 # unused
86
- _MAX_MSG_ABSOLUTE = 8972
86
+ _MAX_MSG_ABSOLUTE = 8966
87
87
88
88
_FLAGS_QR_MASK = 0x8000 # query response mask
89
89
_FLAGS_QR_QUERY = 0x0000 # query
90
90
_FLAGS_QR_RESPONSE = 0x8000 # response
91
91
92
- _FLAGS_AA = 0x0400 # Authorative answer
92
+ _FLAGS_AA = 0x0400 # Authoritative answer
93
93
_FLAGS_TC = 0x0200 # Truncated
94
94
_FLAGS_RD = 0x0100 # Recursion desired
95
95
_FLAGS_RA = 0x8000 # Recursion available
@@ -750,15 +750,14 @@ class DNSOutgoing(object):
750
750
751
751
"""Object representation of an outgoing packet"""
752
752
753
- def __init__ (self , flags , multicast = True , build_on_fly = False ):
753
+ def __init__ (self , flags , multicast = True ):
754
754
self .finished = False
755
755
self .id = 0
756
756
self .multicast = multicast
757
757
self .flags = flags
758
758
self .names = {}
759
759
self .data = []
760
760
self .size = 12
761
- self .build_on_fly = build_on_fly
762
761
self .state = self .State .init
763
762
764
763
self .questions = []
@@ -768,26 +767,11 @@ def __init__(self, flags, multicast=True, build_on_fly=False):
768
767
769
768
class State (enum .Enum ):
770
769
init = 0
771
- adding_questions = 1
772
- adding_answers = 2
773
- adding_authoratives = 3
774
- adding_additionals = 4
775
- finished = 5
776
-
777
- def set_state (self , state ):
778
- if self .state != state :
779
- if self .state .value > state .value :
780
- raise Error ('Out of order DNSOutgoing build %s -> %s' % (
781
- self .state .name , state .name ))
782
- self .state = state
783
- return self .state != self .State .finished
770
+ finished = 1
784
771
785
772
def add_question (self , record ):
786
773
"""Adds a question"""
787
774
self .questions .append (record )
788
- if self .build_on_fly :
789
- if self .set_state (self .State .adding_questions ):
790
- self .write_question (record )
791
775
792
776
def add_answer (self , inp , record ):
793
777
"""Adds an answer"""
@@ -799,23 +783,14 @@ def add_answer_at_time(self, record, now):
799
783
if record is not None :
800
784
if now == 0 or not record .is_expired (now ):
801
785
self .answers .append ((record , now ))
802
- if self .build_on_fly :
803
- if self .set_state (self .State .adding_answers ):
804
- self .write_record (record , now )
805
786
806
787
def add_authorative_answer (self , record ):
807
788
"""Adds an authoritative answer"""
808
789
self .authorities .append (record )
809
- if self .build_on_fly :
810
- if self .set_state (self .State .adding_authoratives ):
811
- self .write_record (record , 0 )
812
790
813
791
def add_additional_answer (self , record ):
814
792
"""Adds an additional answer"""
815
793
self .additionals .append (record )
816
- if self .build_on_fly :
817
- if self .set_state (self .State .adding_additionals ):
818
- self .write_record (record , 0 )
819
794
820
795
def pack (self , format_ , value ):
821
796
self .data .append (struct .pack (format_ , value ))
@@ -916,6 +891,9 @@ def write_question(self, question):
916
891
def write_record (self , record , now ):
917
892
"""Writes a record (answer, authoritative answer, additional) to
918
893
the packet"""
894
+ if self .state == self .State .finished :
895
+ return 1
896
+
919
897
start_data_length , start_size = len (self .data ), self .size
920
898
self .write_name (record .name )
921
899
self .write_short (record .type )
@@ -944,30 +922,31 @@ def write_record(self, record, now):
944
922
self .data .pop ()
945
923
self .size = start_size
946
924
self .state = self .State .finished
925
+ return 1
926
+ return 0
947
927
948
928
def packet (self ):
949
929
"""Returns a string containing the packet's bytes
950
930
951
931
No further parts should be added to the packet once this
952
932
is done."""
933
+
934
+ overrun_answers , overrun_authorities , overrun_additionals = 0 , 0 , 0
935
+
953
936
if self .state != self .State .finished :
954
- if not self .build_on_fly :
955
- for question in self .questions :
956
- self .write_question (question )
957
- for answer , time_ in self .answers :
958
- if self .state != self .State .finished :
959
- self .write_record (answer , time_ )
960
- for authority in self .authorities :
961
- if self .state != self .State .finished :
962
- self .write_record (authority , 0 )
963
- for additional in self .additionals :
964
- if self .state != self .State .finished :
965
- self .write_record (additional , 0 )
937
+ for question in self .questions :
938
+ self .write_question (question )
939
+ for answer , time_ in self .answers :
940
+ overrun_answers += self .write_record (answer , time_ )
941
+ for authority in self .authorities :
942
+ overrun_authorities += self .write_record (authority , 0 )
943
+ for additional in self .additionals :
944
+ overrun_additionals += self .write_record (additional , 0 )
966
945
self .state = self .State .finished
967
946
968
- self .insert_short (0 , len (self .additionals ))
969
- self .insert_short (0 , len (self .authorities ))
970
- self .insert_short (0 , len (self .answers ))
947
+ self .insert_short (0 , len (self .additionals ) - overrun_additionals )
948
+ self .insert_short (0 , len (self .authorities ) - overrun_authorities )
949
+ self .insert_short (0 , len (self .answers ) - overrun_answers )
971
950
self .insert_short (0 , len (self .questions ))
972
951
self .insert_short (0 , self .flags )
973
952
if self .multicast :
@@ -1283,13 +1262,11 @@ def run(self):
1283
1262
return
1284
1263
now = current_time_millis ()
1285
1264
if self .next_time <= now :
1286
- out = DNSOutgoing (_FLAGS_QR_QUERY , build_on_fly = True )
1265
+ out = DNSOutgoing (_FLAGS_QR_QUERY )
1287
1266
out .add_question (DNSQuestion (self .type , _TYPE_PTR , _CLASS_IN ))
1288
1267
for record in self .services .values ():
1289
1268
if not record .is_expired (now ):
1290
1269
out .add_answer_at_time (record , now )
1291
- if out .state == out .State .finished :
1292
- break
1293
1270
1294
1271
self .zc .send (out )
1295
1272
self .next_time = now + self .delay
0 commit comments