2222
2323import struct
2424import sys
25- from typing import Any , Callable , Dict , List , Optional , Set , Tuple , Union , cast
25+ from typing import Any , Dict , List , Optional , Set , Tuple , Union
2626
2727from .._dns import (
2828 DNSAddress ,
@@ -194,10 +194,6 @@ def __repr__(self) -> str:
194194 ]
195195 )
196196
197- def _unpack (self , unpacker : Callable [[bytes , int ], tuple ], length : int ) -> tuple :
198- self .offset += length
199- return unpacker (self .data , self .offset - length )
200-
201197 def _read_header (self ) -> None :
202198 """Reads header portion of packet"""
203199 (
@@ -207,7 +203,8 @@ def _read_header(self) -> None:
207203 self .num_answers ,
208204 self .num_authorities ,
209205 self .num_additionals ,
210- ) = self ._unpack (UNPACK_6H , 12 )
206+ ) = UNPACK_6H (self .data )
207+ self .offset += 12
211208
212209 def _read_questions (self ) -> None :
213210 """Reads questions section of packet"""
@@ -264,18 +261,24 @@ def _read_record(
264261 ) -> Optional [DNSRecord ]:
265262 """Read known records types and skip unknown ones."""
266263 if type_ == _TYPE_A :
267- return DNSAddress (domain , type_ , class_ , ttl , self ._read_string (4 ), created = self .now )
264+ dns_address = DNSAddress (domain , type_ , class_ , ttl , self ._read_string (4 ))
265+ dns_address .created = self .now
266+ return dns_address
268267 if type_ in (_TYPE_CNAME , _TYPE_PTR ):
269268 return DNSPointer (domain , type_ , class_ , ttl , self ._read_name (), self .now )
270269 if type_ == _TYPE_TXT :
271270 return DNSText (domain , type_ , class_ , ttl , self ._read_string (length ), self .now )
272271 if type_ == _TYPE_SRV :
272+ priority , weight , port = UNPACK_3H (self .data , self .offset )
273+ self .offset += 6
273274 return DNSService (
274275 domain ,
275276 type_ ,
276277 class_ ,
277278 ttl ,
278- * cast (Tuple [int , int , int ], self ._unpack (UNPACK_3H , 6 )),
279+ priority ,
280+ weight ,
281+ port ,
279282 self ._read_name (),
280283 self .now ,
281284 )
@@ -285,14 +288,15 @@ def _read_record(
285288 type_ ,
286289 class_ ,
287290 ttl ,
288- self ._read_character_string ().decode ('utf-8' ),
289- self ._read_character_string ().decode ('utf-8' ),
291+ self ._read_character_string ().decode ('utf-8' , 'replace' ),
292+ self ._read_character_string ().decode ('utf-8' , 'replace' ),
290293 self .now ,
291294 )
292295 if type_ == _TYPE_AAAA :
293- return DNSAddress (
294- domain , type_ , class_ , ttl , self ._read_string (16 ), created = self .now , scope_id = self .scope_id
295- )
296+ dns_address = DNSAddress (domain , type_ , class_ , ttl , self ._read_string (16 ))
297+ dns_address .created = self .now
298+ dns_address .scope_id = self .scope_id
299+ return dns_address
296300 if type_ == _TYPE_NSEC :
297301 name_start = self .offset
298302 return DNSNsec (
@@ -384,4 +388,4 @@ def _decode_labels_at_offset(self, off: int, labels: List[str], seen_pointers: S
384388 )
385389 return off + DNS_COMPRESSION_POINTER_LEN
386390
387- raise IncomingDecodeError ("Corrupt packet received while decoding name from {self.source}" )
391+ raise IncomingDecodeError (f "Corrupt packet received while decoding name from { self .source } " )
0 commit comments