|
22 | 22 |
|
23 | 23 | import enum |
24 | 24 | import struct |
25 | | -from typing import Any, Dict, List, Optional, Sequence, Set, TYPE_CHECKING, Tuple, Union, cast |
| 25 | +from typing import Any, Callable, Dict, List, Optional, Sequence, Set, TYPE_CHECKING, Tuple, Union, cast |
26 | 26 |
|
27 | 27 |
|
28 | 28 | from ._dns import DNSAddress, DNSHinfo, DNSNsec, DNSPointer, DNSQuestion, DNSRecord, DNSService, DNSText |
@@ -106,24 +106,28 @@ def __init__(self, data: bytes, scope_id: Optional[int] = None, now: Optional[fl |
106 | 106 | self._read_others = False |
107 | 107 | self.now = now or current_time_millis() |
108 | 108 | self.scope_id = scope_id |
109 | | - |
| 109 | + self._parse_data(self._initial_parse) |
| 110 | + |
| 111 | + def _initial_parse(self) -> None: |
| 112 | + """Parse the data needed to initalize the packet object.""" |
| 113 | + self.read_header() |
| 114 | + self.read_questions() |
| 115 | + if not self.num_questions: |
| 116 | + self.read_others() |
| 117 | + self.valid = True |
| 118 | + |
| 119 | + def _parse_data(self, parser_call: Callable) -> None: |
| 120 | + """Parse part of the packet and catch exceptions.""" |
110 | 121 | try: |
111 | | - self.read_header() |
112 | | - self.read_questions() |
113 | | - if not self.num_questions: |
114 | | - self.read_others() |
115 | | - self.valid = True |
| 122 | + parser_call() |
116 | 123 | except DECODE_EXCEPTIONS: |
117 | | - self.log_exception_warning('Choked at offset %d while unpacking %r', self.offset, data) |
| 124 | + self.log_exception_warning('Choked at offset %d while unpacking %r', self.offset, self.data) |
118 | 125 |
|
119 | 126 | @property |
120 | 127 | def answers(self) -> List[DNSRecord]: |
121 | 128 | """Answers in the packet.""" |
122 | 129 | if not self._read_others: |
123 | | - try: |
124 | | - self.read_others() |
125 | | - except DECODE_EXCEPTIONS: |
126 | | - self.log_exception_warning('Choked at offset %d while unpacking %r', self.offset, data) |
| 130 | + self._parse_data(self.read_others) |
127 | 131 | return self._answers |
128 | 132 |
|
129 | 133 | def __repr__(self) -> str: |
|
0 commit comments