diff --git a/packages/multicast_dns/lib/src/packet.dart b/packages/multicast_dns/lib/src/packet.dart index 8614f4597a0d..eac3f6793fe1 100644 --- a/packages/multicast_dns/lib/src/packet.dart +++ b/packages/multicast_dns/lib/src/packet.dart @@ -222,11 +222,15 @@ List decodeMDnsResponse(List packet) { final ByteData packetBytes = ByteData.view(data.buffer); final int answerCount = packetBytes.getUint16(_kAncountOffset); - if (answerCount == 0) { + final int authorityCount = packetBytes.getUint16(_kNscountOffset); + final int additionalCount = packetBytes.getUint16(_kArcountOffset); + final int remainingCount = answerCount + authorityCount + additionalCount; + + if (remainingCount == 0) { return null; } - final int answerRecordCount = packetBytes.getUint16(_kArcountOffset); + final int questionCount = packetBytes.getUint16(_kQdcountOffset); int offset = _kHeaderSize; void checkLength(int required) { @@ -346,7 +350,14 @@ List decodeMDnsResponse(List packet) { final List result = []; try { - for (int i = 0; i < answerCount + answerRecordCount; i++) { + for (int i = 0; i < questionCount; i++) { + final _FQDNReadResult result = + _readFQDN(data, packetBytes, offset, length); + offset += result.bytesRead; + checkLength(offset + 4); + offset += 4; + } + for (int i = 0; i < remainingCount; i++) { final ResourceRecord record = readResourceRecord(); if (record != null) { result.add(record); diff --git a/packages/multicast_dns/test/decode_test.dart b/packages/multicast_dns/test/decode_test.dart index 5790278c9d3a..56aa02026a50 100644 --- a/packages/multicast_dns/test/decode_test.dart +++ b/packages/multicast_dns/test/decode_test.dart @@ -122,6 +122,47 @@ void testValidPackages() { ), ]); }); + + // Fixes https://github.com/flutter/flutter/issues/31854 + test('Can decode packages with question, answer and additional', () { + final List result = + decodeMDnsResponse(packetWithQuestionAnArCount); + expect(result, isNotNull); + expect(result.length, 2); + expect(result, [ + PtrResourceRecord( + '_______________.____._____', + result[0].validUntil, + domainName: '_______________________._______________.____._____', + ), + PtrResourceRecord( + '_______________.____._____', + result[1].validUntil, + domainName: '____________________________._______________.____._____', + ), + ]); + }); + + // Fixes https://github.com/flutter/flutter/issues/31854 + test('Can decode packages without question and with answer and additional', + () { + final List result = + decodeMDnsResponse(packetWithoutQuestionWithAnArCount); + expect(result, isNotNull); + expect(result.length, 2); + expect(result, [ + PtrResourceRecord( + '_______________.____._____', + result[0].validUntil, + domainName: '______________________._______________.____._____', + ), + TxtResourceRecord( + '_______________.____._____', + result[1].validUntil, + text: '\u{14}model=MacBookPro14,3\nosxvers=18\u{12}ecolor=225,225,223', + ), + ]); + }); } void testBadPackages() { @@ -832,3 +873,308 @@ const List srvRData = [ 0x6c, 0x00 ]; + +const List packetWithQuestionAnArCount = [ + 0, + 0, + 2, + 0, + 0, + 1, + 0, + 1, + 0, + 0, + 0, + 1, + 15, + 95, + 95, + 95, + 95, + 95, + 95, + 95, + 95, + 95, + 95, + 95, + 95, + 95, + 95, + 95, + 4, + 95, + 95, + 95, + 95, + 5, + 95, + 95, + 95, + 95, + 95, + 0, + 0, + 12, + 0, + 1, + 192, + 12, + 0, + 12, + 0, + 1, + 0, + 0, + 14, + 13, + 0, + 26, + 23, + 95, + 95, + 95, + 95, + 95, + 95, + 95, + 95, + 95, + 95, + 95, + 95, + 95, + 95, + 95, + 95, + 95, + 95, + 95, + 95, + 95, + 95, + 95, + 192, + 12, + 192, + 12, + 0, + 12, + 0, + 1, + 0, + 0, + 14, + 13, + 0, + 31, + 28, + 95, + 95, + 95, + 95, + 95, + 95, + 95, + 95, + 95, + 95, + 95, + 95, + 95, + 95, + 95, + 95, + 95, + 95, + 95, + 95, + 95, + 95, + 95, + 95, + 95, + 95, + 95, + 95, + 192, + 12, +]; + +const List packetWithoutQuestionWithAnArCount = [ + 0, + 0, + 132, + 0, + 0, + 0, + 0, + 1, + 0, + 0, + 0, + 1, + 15, + 95, + 95, + 95, + 95, + 95, + 95, + 95, + 95, + 95, + 95, + 95, + 95, + 95, + 95, + 95, + 4, + 95, + 95, + 95, + 95, + 5, + 95, + 95, + 95, + 95, + 95, + 0, + 0, + 12, + 0, + 1, + 0, + 0, + 17, + 148, + 0, + 25, + 22, + 95, + 95, + 95, + 95, + 95, + 95, + 95, + 95, + 95, + 95, + 95, + 95, + 95, + 95, + 95, + 95, + 95, + 95, + 95, + 95, + 95, + 95, + 192, + 12, + 22, + 95, + 95, + 95, + 95, + 95, + 95, + 95, + 95, + 95, + 95, + 95, + 95, + 95, + 95, + 95, + 95, + 95, + 95, + 95, + 95, + 95, + 95, + 12, + 95, + 95, + 95, + 95, + 95, + 95, + 95, + 95, + 95, + 95, + 95, + 95, + 192, + 28, + 0, + 16, + 0, + 1, + 0, + 0, + 17, + 148, + 0, + 51, + 20, + 109, + 111, + 100, + 101, + 108, + 61, + 77, + 97, + 99, + 66, + 111, + 111, + 107, + 80, + 114, + 111, + 49, + 52, + 44, + 51, + 10, + 111, + 115, + 120, + 118, + 101, + 114, + 115, + 61, + 49, + 56, + 18, + 101, + 99, + 111, + 108, + 111, + 114, + 61, + 50, + 50, + 53, + 44, + 50, + 50, + 53, + 44, + 50, + 50, + 51, +];