| OLD | NEW |
| 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 library mdns.src.packet; | 5 library mdns.src.packet; |
| 6 | 6 |
| 7 import 'dart:convert'; | 7 import 'dart:convert'; |
| 8 import 'dart:io'; | 8 import 'dart:io'; |
| 9 import 'dart:typed_data'; | 9 import 'dart:typed_data'; |
| 10 | 10 |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 62 class FQDNReadResult { | 62 class FQDNReadResult { |
| 63 final List<String> fqdn; | 63 final List<String> fqdn; |
| 64 final int bytesRead; | 64 final int bytesRead; |
| 65 FQDNReadResult(this.fqdn, this.bytesRead); | 65 FQDNReadResult(this.fqdn, this.bytesRead); |
| 66 } | 66 } |
| 67 | 67 |
| 68 /// Decode a mDNS package. | 68 /// Decode a mDNS package. |
| 69 /// | 69 /// |
| 70 /// If decoding fails (e.g. due to an invalid packet) `null` is returned. | 70 /// If decoding fails (e.g. due to an invalid packet) `null` is returned. |
| 71 /// | 71 /// |
| 72 /// See See https://tools.ietf.org/html/rfc1035 for the format. | 72 /// See https://tools.ietf.org/html/rfc1035 for the format. |
| 73 List<DecodeResult> decodeMDnsResponse(List<int> packet) { | 73 List<DecodeResult> decodeMDnsResponse(List<int> packet) { |
| 74 int length = packet.length; | 74 int length = packet.length; |
| 75 if (length < headerSize) return null; | 75 if (length < headerSize) return null; |
| 76 | 76 |
| 77 Uint8List data = | 77 Uint8List data = |
| 78 packet is Uint8List ? packet : new Uint8List.fromList(packet); | 78 packet is Uint8List ? packet : new Uint8List.fromList(packet); |
| 79 ByteData bd = new ByteData.view(data.buffer); | 79 ByteData bd = new ByteData.view(data.buffer); |
| 80 // Query identifier. | 80 // Query identifier. |
| 81 int id = bd.getUint16(idOffset); | 81 int id = bd.getUint16(idOffset); |
| 82 // Flags. | 82 // Flags. |
| (...skipping 10 matching lines...) Expand all Loading... |
| 93 int arcount = bd.getUint16(arcountOffset); | 93 int arcount = bd.getUint16(arcountOffset); |
| 94 int offset = headerSize; | 94 int offset = headerSize; |
| 95 | 95 |
| 96 void checkLength(int required) { | 96 void checkLength(int required) { |
| 97 if (length < required) throw new MDnsDecodeException(required); | 97 if (length < required) throw new MDnsDecodeException(required); |
| 98 } | 98 } |
| 99 | 99 |
| 100 // Read a FQDN at the given offset. Returns a pair with the FQDN | 100 // Read a FQDN at the given offset. Returns a pair with the FQDN |
| 101 // parts and the number of bytes consumed. | 101 // parts and the number of bytes consumed. |
| 102 // | 102 // |
| 103 // If decoding fails (e.g. die to an invalid packet) `null` is returned. | 103 // If decoding fails (e.g. due to an invalid packet) `null` is returned. |
| 104 FQDNReadResult readFQDN(int offset) { | 104 FQDNReadResult readFQDN(int offset) { |
| 105 List<String> parts = []; | 105 List<String> parts = []; |
| 106 int prevOffset = offset; | 106 int prevOffset = offset; |
| 107 while (true) { | 107 while (true) { |
| 108 // At least two bytes required. | 108 // At least two bytes required. |
| 109 checkLength(offset + 2); | 109 checkLength(offset + 2); |
| 110 | 110 |
| 111 // Check for compressed. | 111 // Check for compressed. |
| 112 if (data[offset] & 0xc0 == 0xc0) { | 112 if (data[offset] & 0xc0 == 0xc0) { |
| 113 // A compressed FQDN has a new offset in the lower 14 bits. | 113 // A compressed FQDN has a new offset in the lower 14 bits. |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 178 return result; | 178 return result; |
| 179 } | 179 } |
| 180 | 180 |
| 181 /// Exceptions thrown by decoder when the packet is invalid. | 181 /// Exceptions thrown by decoder when the packet is invalid. |
| 182 class MDnsDecodeException implements Exception { | 182 class MDnsDecodeException implements Exception { |
| 183 /// Exception message. | 183 /// Exception message. |
| 184 final int offset; | 184 final int offset; |
| 185 const MDnsDecodeException(this.offset); | 185 const MDnsDecodeException(this.offset); |
| 186 String toString() => 'Decoding error at $offset'; | 186 String toString() => 'Decoding error at $offset'; |
| 187 } | 187 } |
| OLD | NEW |