22
22
23
23
import struct
24
24
import 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
26
26
27
27
from .._dns import (
28
28
DNSAddress ,
@@ -194,10 +194,6 @@ def __repr__(self) -> str:
194
194
]
195
195
)
196
196
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
-
201
197
def _read_header (self ) -> None :
202
198
"""Reads header portion of packet"""
203
199
(
@@ -207,7 +203,8 @@ def _read_header(self) -> None:
207
203
self .num_answers ,
208
204
self .num_authorities ,
209
205
self .num_additionals ,
210
- ) = self ._unpack (UNPACK_6H , 12 )
206
+ ) = UNPACK_6H (self .data )
207
+ self .offset += 12
211
208
212
209
def _read_questions (self ) -> None :
213
210
"""Reads questions section of packet"""
@@ -264,18 +261,24 @@ def _read_record(
264
261
) -> Optional [DNSRecord ]:
265
262
"""Read known records types and skip unknown ones."""
266
263
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
268
267
if type_ in (_TYPE_CNAME , _TYPE_PTR ):
269
268
return DNSPointer (domain , type_ , class_ , ttl , self ._read_name (), self .now )
270
269
if type_ == _TYPE_TXT :
271
270
return DNSText (domain , type_ , class_ , ttl , self ._read_string (length ), self .now )
272
271
if type_ == _TYPE_SRV :
272
+ priority , weight , port = UNPACK_3H (self .data , self .offset )
273
+ self .offset += 6
273
274
return DNSService (
274
275
domain ,
275
276
type_ ,
276
277
class_ ,
277
278
ttl ,
278
- * cast (Tuple [int , int , int ], self ._unpack (UNPACK_3H , 6 )),
279
+ priority ,
280
+ weight ,
281
+ port ,
279
282
self ._read_name (),
280
283
self .now ,
281
284
)
@@ -285,14 +288,15 @@ def _read_record(
285
288
type_ ,
286
289
class_ ,
287
290
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' ),
290
293
self .now ,
291
294
)
292
295
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
296
300
if type_ == _TYPE_NSEC :
297
301
name_start = self .offset
298
302
return DNSNsec (
@@ -384,4 +388,4 @@ def _decode_labels_at_offset(self, off: int, labels: List[str], seen_pointers: S
384
388
)
385
389
return off + DNS_COMPRESSION_POINTER_LEN
386
390
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