40
40
)
41
41
from .incoming import DNSIncoming
42
42
43
+ str_ = str
44
+ float_ = float
45
+ DNSQuestion_ = DNSQuestion
46
+ DNSRecord_ = DNSRecord
47
+
43
48
44
49
class State (enum .Enum ):
45
50
init = 0
@@ -238,7 +243,7 @@ def write_character_string(self, value: bytes) -> None:
238
243
self ._write_byte (length )
239
244
self .write_string (value )
240
245
241
- def write_name (self , name : str ) -> None :
246
+ def write_name (self , name : str_ ) -> None :
242
247
"""
243
248
Write names to packet
244
249
@@ -276,26 +281,26 @@ def write_name(self, name: str) -> None:
276
281
# this is the end of a name
277
282
self ._write_byte (0 )
278
283
279
- def _write_question (self , question : DNSQuestion ) -> bool :
284
+ def _write_question (self , question : DNSQuestion_ ) -> bool :
280
285
"""Writes a question to the packet"""
281
286
start_data_length , start_size = len (self .data ), self .size
282
287
self .write_name (question .name )
283
288
self .write_short (question .type )
284
289
self ._write_record_class (question )
285
290
return self ._check_data_limit_or_rollback (start_data_length , start_size )
286
291
287
- def _write_record_class (self , record : Union [DNSQuestion , DNSRecord ]) -> None :
292
+ def _write_record_class (self , record : Union [DNSQuestion_ , DNSRecord_ ]) -> None :
288
293
"""Write out the record class including the unique/unicast (QU) bit."""
289
294
if record .unique and self .multicast :
290
295
self .write_short (record .class_ | _CLASS_UNIQUE )
291
296
else :
292
297
self .write_short (record .class_ )
293
298
294
- def _write_ttl (self , record : DNSRecord , now : float ) -> None :
299
+ def _write_ttl (self , record : DNSRecord_ , now : float_ ) -> None :
295
300
"""Write out the record ttl."""
296
301
self ._write_int (record .ttl if now == 0 else record .get_remaining_ttl (now ))
297
302
298
- def _write_record (self , record : DNSRecord , now : float ) -> bool :
303
+ def _write_record (self , record : DNSRecord_ , now : float_ ) -> bool :
299
304
"""Writes a record (answer, authoritative answer, additional) to
300
305
the packet. Returns True on success, or False if we did not
301
306
because the packet because the record does not fit."""
@@ -308,7 +313,9 @@ def _write_record(self, record: DNSRecord, now: float) -> bool:
308
313
self .write_short (0 ) # Will get replaced with the actual size
309
314
record .write (self )
310
315
# Adjust size for the short we will write before this record
311
- length = sum (len (d ) for d in self .data [index + 1 :])
316
+ length = 0
317
+ for d in self .data [index + 1 :]:
318
+ length += len (d )
312
319
# Here we replace the 0 length short we wrote
313
320
# before with the actual length
314
321
self ._replace_short (index , length )
0 commit comments