|
4 | 4 |
|
5 | 5 | import 'dart:convert';
|
6 | 6 | import 'dart:io';
|
| 7 | +import 'dart:math'; |
7 | 8 | import 'dart:typed_data';
|
8 | 9 |
|
9 | 10 | import 'constants.dart';
|
@@ -134,76 +135,55 @@ _FQDNReadResult _readFQDN(
|
134 | 135 |
|
135 | 136 | final List<String> parts = <String>[];
|
136 | 137 | final int prevOffset = offset;
|
137 |
| - while (true) { |
138 |
| - // At least one byte is required. |
139 |
| - checkLength(offset + 1); |
140 |
| - |
141 |
| - // Check for compressed. |
142 |
| - if (data[offset] & 0xc0 == 0xc0) { |
143 |
| - // At least two bytes are required for a compressed FQDN. |
144 |
| - checkLength(offset + 2); |
145 |
| - |
146 |
| - // A compressed FQDN has a new offset in the lower 14 bits. |
147 |
| - final _FQDNReadResult result = _readFQDN( |
148 |
| - data, byteData, byteData.getUint16(offset) & ~0xc000, length); |
149 |
| - parts.addAll(result.fqdnParts); |
150 |
| - offset += 2; |
151 |
| - break; |
152 |
| - } else { |
153 |
| - // A normal FQDN part has a length and a UTF-8 encoded name |
154 |
| - // part. If the length is 0 this is the end of the FQDN. |
155 |
| - final int partLength = data[offset]; |
156 |
| - offset++; |
157 |
| - if (partLength > 0) { |
158 |
| - checkLength(offset + partLength); |
159 |
| - final Uint8List partBytes = |
160 |
| - Uint8List.view(data.buffer, offset, partLength); |
161 |
| - offset += partLength; |
162 |
| - // According to the RFC, this is supposed to be utf-8 encoded, but |
163 |
| - // we should continue decoding even if it isn't to avoid dropping the |
164 |
| - // rest of the data, which might still be useful. |
165 |
| - parts.add(utf8.decode(partBytes, allowMalformed: true)); |
166 |
| - } else { |
| 138 | + final List<int> offsetsToVisit = <int>[offset]; |
| 139 | + int upperLimitOffset = offset; |
| 140 | + int highestOffsetRead = offset; |
| 141 | + |
| 142 | + while (offsetsToVisit.isNotEmpty) { |
| 143 | + offset = offsetsToVisit.removeLast(); |
| 144 | + |
| 145 | + while (true) { |
| 146 | + // At least one byte is required. |
| 147 | + checkLength(offset + 1); |
| 148 | + // Check for compressed. |
| 149 | + if (data[offset] & 0xc0 == 0xc0) { |
| 150 | + // At least two bytes are required for a compressed FQDN (see RFC1035 section 4.1.4). |
| 151 | + checkLength(offset + 2); |
| 152 | + |
| 153 | + // A compressed FQDN has a new offset in the lower 14 bits. |
| 154 | + final int pointerDest = byteData.getUint16(offset) & ~0xc000; |
| 155 | + // Pointers can only point to prior occurances of some name. |
| 156 | + // This check also guards against pointers that form loops. |
| 157 | + if (pointerDest >= upperLimitOffset) { |
| 158 | + throw MDnsDecodeException(offset); |
| 159 | + } |
| 160 | + upperLimitOffset = pointerDest; |
| 161 | + offsetsToVisit.add(pointerDest); |
| 162 | + highestOffsetRead = max(highestOffsetRead, offset + 2); |
167 | 163 | break;
|
| 164 | + } else { |
| 165 | + // A normal FQDN part has a length and a UTF-8 encoded name |
| 166 | + // part. If the length is 0 this is the end of the FQDN. |
| 167 | + final int partLength = data[offset]; |
| 168 | + offset++; |
| 169 | + if (partLength > 0) { |
| 170 | + checkLength(offset + partLength); |
| 171 | + final Uint8List partBytes = |
| 172 | + Uint8List.view(data.buffer, offset, partLength); |
| 173 | + offset += partLength; |
| 174 | + // According to the RFC, this is supposed to be utf-8 encoded, but |
| 175 | + // we should continue decoding even if it isn't to avoid dropping the |
| 176 | + // rest of the data, which might still be useful. |
| 177 | + parts.add(utf8.decode(partBytes, allowMalformed: true)); |
| 178 | + highestOffsetRead = max(highestOffsetRead, offset); |
| 179 | + } else { |
| 180 | + highestOffsetRead = max(highestOffsetRead, offset); |
| 181 | + break; |
| 182 | + } |
168 | 183 | }
|
169 | 184 | }
|
170 | 185 | }
|
171 |
| - return _FQDNReadResult(parts, offset - prevOffset); |
172 |
| -} |
173 |
| - |
174 |
| -/// Decode an mDNS query packet. |
175 |
| -/// |
176 |
| -/// If decoding fails (e.g. due to an invalid packet), `null` is returned. |
177 |
| -/// |
178 |
| -/// See https://tools.ietf.org/html/rfc1035 for format. |
179 |
| -ResourceRecordQuery? decodeMDnsQuery(List<int> packet) { |
180 |
| - final int length = packet.length; |
181 |
| - if (length < _kHeaderSize) { |
182 |
| - return null; |
183 |
| - } |
184 |
| - |
185 |
| - final Uint8List data = |
186 |
| - packet is Uint8List ? packet : Uint8List.fromList(packet); |
187 |
| - final ByteData packetBytes = ByteData.view(data.buffer); |
188 |
| - |
189 |
| - // Check whether it's a query. |
190 |
| - final int flags = packetBytes.getUint16(_kFlagsOffset); |
191 |
| - if (flags != 0) { |
192 |
| - return null; |
193 |
| - } |
194 |
| - final int questionCount = packetBytes.getUint16(_kQdcountOffset); |
195 |
| - if (questionCount == 0) { |
196 |
| - return null; |
197 |
| - } |
198 |
| - |
199 |
| - final _FQDNReadResult fqdn = |
200 |
| - _readFQDN(data, packetBytes, _kHeaderSize, data.length); |
201 |
| - |
202 |
| - int offset = _kHeaderSize + fqdn.bytesRead; |
203 |
| - final int type = packetBytes.getUint16(offset); |
204 |
| - offset += 2; |
205 |
| - final int queryType = packetBytes.getUint16(offset) & 0x8000; |
206 |
| - return ResourceRecordQuery(type, fqdn.fqdn, queryType); |
| 186 | + return _FQDNReadResult(parts, highestOffsetRead - prevOffset); |
207 | 187 | }
|
208 | 188 |
|
209 | 189 | /// Decode an mDNS response packet.
|
|
0 commit comments