Skip to content

Commit 1e7c074

Browse files
authored
Remove DNSOutgoing.packet backwards compatibility (#569)
- DNSOutgoing.packet only returned a partial message when the DNSOutgoing contents exceeded _MAX_MSG_ABSOLUTE or _MAX_MSG_TYPICAL This was a legacy function that was replaced with .packets() which always returns a complete payload in #248 As packet() should not be used since it will end up missing data, it has been removed
1 parent 0e0bc2a commit 1e7c074

5 files changed

Lines changed: 35 additions & 50 deletions

File tree

tests/test_core.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ def mock_incoming_msg(service_state_change: r.ServiceStateChange) -> r.DNSIncomi
116116
),
117117
0,
118118
)
119-
return r.DNSIncoming(generated.packet())
119+
return r.DNSIncoming(generated.packets()[0])
120120

121121
if service_state_change == r.ServiceStateChange.Removed:
122122
ttl = 0
@@ -154,7 +154,7 @@ def mock_incoming_msg(service_state_change: r.ServiceStateChange) -> r.DNSIncomi
154154
0,
155155
)
156156

157-
return r.DNSIncoming(generated.packet())
157+
return r.DNSIncoming(generated.packets()[0])
158158

159159
def mock_split_incoming_msg(service_state_change: r.ServiceStateChange) -> r.DNSIncoming:
160160
"""Mock an incoming message for the case where the packet is split."""
@@ -183,7 +183,7 @@ def mock_split_incoming_msg(service_state_change: r.ServiceStateChange) -> r.DNS
183183
),
184184
0,
185185
)
186-
return r.DNSIncoming(generated.packet())
186+
return r.DNSIncoming(generated.packets()[0])
187187

188188
service_name = 'name._type._tcp.local.'
189189
service_type = '_type._tcp.local.'

tests/test_dns.py

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -141,20 +141,20 @@ def test_dns_outgoing_repr(self):
141141
class PacketGeneration(unittest.TestCase):
142142
def test_parse_own_packet_simple(self):
143143
generated = r.DNSOutgoing(0)
144-
r.DNSIncoming(generated.packet())
144+
r.DNSIncoming(generated.packets()[0])
145145

146146
def test_parse_own_packet_simple_unicast(self):
147147
generated = r.DNSOutgoing(0, False)
148-
r.DNSIncoming(generated.packet())
148+
r.DNSIncoming(generated.packets()[0])
149149

150150
def test_parse_own_packet_flags(self):
151151
generated = r.DNSOutgoing(const._FLAGS_QR_QUERY)
152-
r.DNSIncoming(generated.packet())
152+
r.DNSIncoming(generated.packets()[0])
153153

154154
def test_parse_own_packet_question(self):
155155
generated = r.DNSOutgoing(const._FLAGS_QR_QUERY)
156156
generated.add_question(r.DNSQuestion("testname.local.", const._TYPE_SRV, const._CLASS_IN))
157-
r.DNSIncoming(generated.packet())
157+
r.DNSIncoming(generated.packets()[0])
158158

159159
def test_parse_own_packet_response(self):
160160
generated = r.DNSOutgoing(const._FLAGS_QR_RESPONSE)
@@ -171,15 +171,15 @@ def test_parse_own_packet_response(self):
171171
),
172172
0,
173173
)
174-
parsed = r.DNSIncoming(generated.packet())
174+
parsed = r.DNSIncoming(generated.packets()[0])
175175
assert len(generated.answers) == 1
176176
assert len(generated.answers) == len(parsed.answers)
177177

178178
def test_match_question(self):
179179
generated = r.DNSOutgoing(const._FLAGS_QR_QUERY)
180180
question = r.DNSQuestion("testname.local.", const._TYPE_SRV, const._CLASS_IN)
181181
generated.add_question(question)
182-
parsed = r.DNSIncoming(generated.packet())
182+
parsed = r.DNSIncoming(generated.packets()[0])
183183
assert len(generated.questions) == 1
184184
assert len(generated.questions) == len(parsed.questions)
185185
assert question == parsed.questions[0]
@@ -220,7 +220,7 @@ def test_suppress_answer(self):
220220
)
221221
query_generated.add_answer_at_time(answer1, 0)
222222
query_generated.add_answer_at_time(staleanswer2, 0)
223-
query = r.DNSIncoming(query_generated.packet())
223+
query = r.DNSIncoming(query_generated.packets()[0])
224224

225225
# Should be suppressed
226226
response = r.DNSOutgoing(const._FLAGS_QR_RESPONSE)
@@ -255,14 +255,14 @@ def test_suppress_answer(self):
255255
def test_dns_hinfo(self):
256256
generated = r.DNSOutgoing(0)
257257
generated.add_additional_answer(DNSHinfo('irrelevant', const._TYPE_HINFO, 0, 0, 'cpu', 'os'))
258-
parsed = r.DNSIncoming(generated.packet())
258+
parsed = r.DNSIncoming(generated.packets()[0])
259259
answer = cast(r.DNSHinfo, parsed.answers[0])
260260
assert answer.cpu == u'cpu'
261261
assert answer.os == u'os'
262262

263263
generated = r.DNSOutgoing(0)
264264
generated.add_additional_answer(DNSHinfo('irrelevant', const._TYPE_HINFO, 0, 0, 'cpu', 'x' * 257))
265-
self.assertRaises(r.NamePartTooLongException, generated.packet)
265+
self.assertRaises(r.NamePartTooLongException, generated.packets)
266266

267267
def test_many_questions(self):
268268
"""Test many questions get seperated into multiple packets."""
@@ -290,7 +290,7 @@ def test_only_one_answer_can_by_large(self):
290290
https://datatracker.ietf.org/doc/html/rfc6762#section-17
291291
"""
292292
generated = r.DNSOutgoing(const._FLAGS_QR_RESPONSE)
293-
query = r.DNSIncoming(r.DNSOutgoing(const._FLAGS_QR_QUERY).packet())
293+
query = r.DNSIncoming(r.DNSOutgoing(const._FLAGS_QR_QUERY).packets()[0])
294294
for i in range(3):
295295
generated.add_answer(
296296
query,
@@ -381,25 +381,25 @@ class PacketForm(unittest.TestCase):
381381
def test_transaction_id(self):
382382
"""ID must be zero in a DNS-SD packet"""
383383
generated = r.DNSOutgoing(const._FLAGS_QR_QUERY)
384-
bytes = generated.packet()
384+
bytes = generated.packets()[0]
385385
id = bytes[0] << 8 | bytes[1]
386386
assert id == 0
387387

388388
def test_query_header_bits(self):
389389
generated = r.DNSOutgoing(const._FLAGS_QR_QUERY)
390-
bytes = generated.packet()
390+
bytes = generated.packets()[0]
391391
flags = bytes[2] << 8 | bytes[3]
392392
assert flags == 0x0
393393

394394
def test_response_header_bits(self):
395395
generated = r.DNSOutgoing(const._FLAGS_QR_RESPONSE)
396-
bytes = generated.packet()
396+
bytes = generated.packets()[0]
397397
flags = bytes[2] << 8 | bytes[3]
398398
assert flags == 0x8000
399399

400400
def test_numbers(self):
401401
generated = r.DNSOutgoing(const._FLAGS_QR_RESPONSE)
402-
bytes = generated.packet()
402+
bytes = generated.packets()[0]
403403
(num_questions, num_answers, num_authorities, num_additionals) = struct.unpack('!4H', bytes[4:12])
404404
assert num_questions == 0
405405
assert num_answers == 0
@@ -411,7 +411,7 @@ def test_numbers_questions(self):
411411
question = r.DNSQuestion("testname.local.", const._TYPE_SRV, const._CLASS_IN)
412412
for i in range(10):
413413
generated.add_question(question)
414-
bytes = generated.packet()
414+
bytes = generated.packets()[0]
415415
(num_questions, num_answers, num_authorities, num_additionals) = struct.unpack('!4H', bytes[4:12])
416416
assert num_questions == 10
417417
assert num_answers == 0
@@ -422,7 +422,7 @@ def test_numbers_questions(self):
422422
class TestDnsIncoming(unittest.TestCase):
423423
def test_incoming_exception_handling(self):
424424
generated = r.DNSOutgoing(0)
425-
packet = generated.packet()
425+
packet = generated.packets()[0]
426426
packet = packet[:8] + b'deadbeef' + packet[8:]
427427
parsed = r.DNSIncoming(packet)
428428
parsed = r.DNSIncoming(packet)
@@ -432,7 +432,7 @@ def test_incoming_unknown_type(self):
432432
generated = r.DNSOutgoing(0)
433433
answer = r.DNSAddress('a', const._TYPE_SOA, const._CLASS_IN, 1, b'a')
434434
generated.add_additional_answer(answer)
435-
packet = generated.packet()
435+
packet = generated.packets()[0]
436436
parsed = r.DNSIncoming(packet)
437437
assert len(parsed.answers) == 0
438438
assert parsed.is_query() != parsed.is_response()
@@ -443,7 +443,7 @@ def test_incoming_ipv6(self):
443443
generated = r.DNSOutgoing(0)
444444
answer = r.DNSAddress('domain', const._TYPE_AAAA, const._CLASS_IN | const._CLASS_UNIQUE, 1, packed)
445445
generated.add_additional_answer(answer)
446-
packet = generated.packet()
446+
packet = generated.packets()[0]
447447
parsed = r.DNSIncoming(packet)
448448
record = parsed.answers[0]
449449
assert isinstance(record, r.DNSAddress)

tests/test_init.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -43,36 +43,36 @@ def test_long_name(self):
4343
"this.is.a.very.long.name.with.lots.of.parts.in.it.local.", const._TYPE_SRV, const._CLASS_IN
4444
)
4545
generated.add_question(question)
46-
r.DNSIncoming(generated.packet())
46+
r.DNSIncoming(generated.packets()[0])
4747

4848
def test_exceedingly_long_name(self):
4949
generated = r.DNSOutgoing(const._FLAGS_QR_RESPONSE)
5050
name = "%slocal." % ("part." * 1000)
5151
question = r.DNSQuestion(name, const._TYPE_SRV, const._CLASS_IN)
5252
generated.add_question(question)
53-
r.DNSIncoming(generated.packet())
53+
r.DNSIncoming(generated.packets()[0])
5454

5555
def test_extra_exceedingly_long_name(self):
5656
generated = r.DNSOutgoing(const._FLAGS_QR_RESPONSE)
5757
name = "%slocal." % ("part." * 4000)
5858
question = r.DNSQuestion(name, const._TYPE_SRV, const._CLASS_IN)
5959
generated.add_question(question)
60-
r.DNSIncoming(generated.packet())
60+
r.DNSIncoming(generated.packets()[0])
6161

6262
def test_exceedingly_long_name_part(self):
6363
name = "%s.local." % ("a" * 1000)
6464
generated = r.DNSOutgoing(const._FLAGS_QR_RESPONSE)
6565
question = r.DNSQuestion(name, const._TYPE_SRV, const._CLASS_IN)
6666
generated.add_question(question)
67-
self.assertRaises(r.NamePartTooLongException, generated.packet)
67+
self.assertRaises(r.NamePartTooLongException, generated.packets)
6868

6969
def test_same_name(self):
7070
name = "paired.local."
7171
generated = r.DNSOutgoing(const._FLAGS_QR_RESPONSE)
7272
question = r.DNSQuestion(name, const._TYPE_SRV, const._CLASS_IN)
7373
generated.add_question(question)
7474
generated.add_question(question)
75-
r.DNSIncoming(generated.packet())
75+
r.DNSIncoming(generated.packets()[0])
7676

7777
def test_lots_of_names(self):
7878

@@ -156,7 +156,7 @@ def on_service_state_change(zeroconf, service_type, state_change, name):
156156
assert mocked_log_warn.call_count == call_counts[0]
157157

158158
# force a receive of a packet
159-
packet = out.packet()
159+
packet = out.packets()[0]
160160
s = zc._respond_sockets[0]
161161

162162
# mock the zeroconf logger and check for the correct logging backoff
@@ -317,7 +317,7 @@ def _process_outgoing_packet(out):
317317
query.add_question(r.DNSQuestion(info.name, const._TYPE_SRV, const._CLASS_IN))
318318
query.add_question(r.DNSQuestion(info.name, const._TYPE_TXT, const._CLASS_IN))
319319
query.add_question(r.DNSQuestion(info.server, const._TYPE_A, const._CLASS_IN))
320-
_process_outgoing_packet(zc.query_handler.response(r.DNSIncoming(query.packet()), False))
320+
_process_outgoing_packet(zc.query_handler.response(r.DNSIncoming(query.packets()[0]), False))
321321
assert nbr_answers == 4 and nbr_additionals == 4 and nbr_authorities == 0
322322
nbr_answers = nbr_additionals = nbr_authorities = 0
323323

@@ -348,7 +348,7 @@ def _process_outgoing_packet(out):
348348
query.add_question(r.DNSQuestion(info.name, const._TYPE_SRV, const._CLASS_IN))
349349
query.add_question(r.DNSQuestion(info.name, const._TYPE_TXT, const._CLASS_IN))
350350
query.add_question(r.DNSQuestion(info.server, const._TYPE_A, const._CLASS_IN))
351-
_process_outgoing_packet(zc.query_handler.response(r.DNSIncoming(query.packet()), False))
351+
_process_outgoing_packet(zc.query_handler.response(r.DNSIncoming(query.packets()[0]), False))
352352
assert nbr_answers == 4 and nbr_additionals == 4 and nbr_authorities == 0
353353
nbr_answers = nbr_additionals = nbr_authorities = 0
354354

@@ -860,7 +860,7 @@ def mock_incoming_msg(service_state_change: r.ServiceStateChange) -> r.DNSIncomi
860860
r.DNSPointer(service_type, const._TYPE_PTR, const._CLASS_IN, ttl, service_name), 0
861861
)
862862

863-
return r.DNSIncoming(generated.packet())
863+
return r.DNSIncoming(generated.packets()[0])
864864

865865
zeroconf = r.Zeroconf(interfaces=['127.0.0.1'])
866866
service_browser = r.ServiceBrowser(zeroconf, service_type, listener=MyServiceListener())
@@ -1021,7 +1021,7 @@ def test_ptr_optimization():
10211021
# query
10221022
query = r.DNSOutgoing(const._FLAGS_QR_QUERY | const._FLAGS_AA)
10231023
query.add_question(r.DNSQuestion(info.type, const._TYPE_PTR, const._CLASS_IN))
1024-
out = zc.query_handler.response(r.DNSIncoming(query.packet()), False)
1024+
out = zc.query_handler.response(r.DNSIncoming(query.packets()[0]), False)
10251025
assert out is not None
10261026
nbr_answers += len(out.answers)
10271027
nbr_authorities += len(out.authorities)

tests/test_services.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ def mock_incoming_msg(records) -> r.DNSIncoming:
229229
for record in records:
230230
generated.add_answer_at_time(record, 0)
231231

232-
return r.DNSIncoming(generated.packet())
232+
return r.DNSIncoming(generated.packets()[0])
233233

234234
def get_service_info_helper(zc, type, name):
235235
nonlocal service_info
@@ -366,7 +366,7 @@ def mock_incoming_msg(records) -> r.DNSIncoming:
366366
for record in records:
367367
generated.add_answer_at_time(record, 0)
368368

369-
return r.DNSIncoming(generated.packet())
369+
return r.DNSIncoming(generated.packets()[0])
370370

371371
def get_service_info_helper(zc, type, name):
372372
nonlocal service_info
@@ -466,7 +466,7 @@ def mock_incoming_msg(
466466
generated.add_answer_at_time(
467467
r.DNSPointer(service_type, const._TYPE_PTR, const._CLASS_IN, ttl, service_name), 0
468468
)
469-
return r.DNSIncoming(generated.packet())
469+
return r.DNSIncoming(generated.packets()[0])
470470

471471
zeroconf = r.Zeroconf(interfaces=['127.0.0.1'])
472472
service_browser = r.ServiceBrowser(zeroconf, service_types, listener=MyServiceListener())
@@ -638,7 +638,7 @@ def current_time_millis():
638638

639639
def send(out, addr=const._MDNS_ADDR, port=const._MDNS_PORT):
640640
"""Sends an outgoing packet."""
641-
pout = r.DNSIncoming(out.packet())
641+
pout = r.DNSIncoming(out.packets()[0])
642642
nonlocal nbr_answers
643643
for answer in pout.answers:
644644
nbr_answers += 1

zeroconf/dns.py

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -799,21 +799,6 @@ def _check_data_limit_or_rollback(self, start_data_length: int, start_size: int)
799799
del self.names[name]
800800
return False
801801

802-
def packet(self) -> bytes:
803-
"""Returns a bytestring containing the first packet's bytes.
804-
805-
Generally, you want to use packets() in case the response
806-
does not fit in a single packet, but this exists for
807-
backward compatibility."""
808-
packets = self.packets()
809-
if len(packets) == 0:
810-
return b''
811-
if len(packets[0]) > _MAX_MSG_ABSOLUTE:
812-
QuietLogger.log_warning_once(
813-
"Created over-sized packet (%d bytes) %r", len(packets[0]), packets[0]
814-
)
815-
return packets[0]
816-
817802
def _write_questions_from_offset(self, questions_offset: int) -> int:
818803
questions_written = 0
819804
for question in self.questions[questions_offset:]:

0 commit comments

Comments
 (0)