Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
55 commits
Select commit Hold shift + click to select a range
cda0d64
Add a mDNS package
sgjesse Oct 28, 2015
5242a77
Fix warning and hint int test
sgjesse Oct 28, 2015
8394100
Correct two typos.
karlklose Oct 29, 2015
3147614
Extend the mDNS package with a native extension used on Mac OS
sgjesse Nov 2, 2015
1aecdd0
Revert "Extend the mDNS package with a native extension used on Mac OS"
sgjesse Nov 2, 2015
b753a49
Re-land "Extend the mDNS package with a native extension used on Mac OS"
sgjesse Nov 3, 2015
bd21b36
Improve resource record implementation in the mdns package.
karlklose Nov 6, 2015
5eae734
Minor fixes to the mDNS package
sgjesse Nov 10, 2015
be10902
Update the mDNS command line tools in the mdns package
sgjesse Nov 10, 2015
67e73fc
Update the mDNS native extension for Mac OS to handle PTR and SRV rec…
sgjesse Nov 10, 2015
51c2879
Add multiple results and timeout handling to the Mac OS mDNS native e…
sgjesse Nov 10, 2015
8ba84d1
Add mDNS discovery to the Fletch compiler
sgjesse Nov 10, 2015
2b2fe61
Avoid a warning due to bug in Dart SDK
sgjesse Nov 10, 2015
afd0ffa
Update project authors statements to Dartino
mit-mit Jan 28, 2016
27e32bb
Rename fletch -> dartino
ricowind Feb 3, 2016
a02b198
Update Dart binaries to 1.18.0-dev.4.4
karlklose Jul 28, 2016
372e481
Move the serial_port native code
sgjesse Aug 3, 2016
13440cc
Initial refactor/adding of mdns
dnfield Oct 18, 2018
a062a28
Initial refactor/adding of mdns
dnfield Oct 18, 2018
0a158b2
move helper methods to tool/
dnfield Oct 18, 2018
cf98f1f
update docs, add example
dnfield Oct 18, 2018
2d8f0ab
merge
dnfield Oct 18, 2018
f962358
remove RRType.any - not ready
dnfield Oct 18, 2018
a223011
README
dnfield Oct 18, 2018
1ae7747
remove lint ignore, update CI
dnfield Oct 18, 2018
8a711b9
update CI for non-flutter packages
dnfield Oct 18, 2018
60f3635
update regex
dnfield Oct 18, 2018
a935aee
revert incremental_build.sh
dnfield Oct 18, 2018
4aba212
Update docs, expose multicast parameter
dnfield Oct 19, 2018
cefc13b
fix tests, run analyze first
dnfield Oct 19, 2018
dcf69e4
Fix readme for mDNS
dnfield Oct 19, 2018
2c09d26
move bin to example, remove args from main pubspec
dnfield Oct 20, 2018
d0071bd
Update example
dnfield Oct 20, 2018
af7a0df
Remove collection dep
dnfield Oct 20, 2018
14826cb
Fix unintentional change to Palette, avoid abbreviations, real hash c…
dnfield Oct 22, 2018
f3a79a8
vertical alignment
dnfield Oct 22, 2018
b9572ba
dartfmt and remove all references to args package
dnfield Oct 23, 2018
9dbb4dd
cleanup
dnfield Oct 24, 2018
5bcabe2
typo, word wrap
dnfield Oct 24, 2018
1961d9f
Add type parameters
dnfield Oct 24, 2018
0eb9bc1
Update examples to use type parameters
dnfield Oct 25, 2018
d379223
minor fixes
dnfield Nov 2, 2018
8d3c4e2
Merge remote-tracking branch 'upstream/master'
dnfield Nov 7, 2018
64902f4
remvoe debugging cde
dnfield Nov 7, 2018
b8251d9
move dumpDatagram to tools
dnfield Nov 7, 2018
2ad7aea
format
dnfield Nov 7, 2018
09b47d2
Set socket options
dnfield Jan 17, 2019
0962e7d
bump
dnfield Jan 19, 2019
3d2f2a4
update version constraint, fix analyzer issues
dnfield Jan 19, 2019
4a0974d
Merge remote-tracking branch 'upstream/master'
dnfield Jan 23, 2019
de88b93
Fix cache, var names, comments
dnfield Jan 24, 2019
3602baa
one missed comment
dnfield Jan 24, 2019
bceebbd
missed renames
dnfield Jan 24, 2019
b5b1d4e
formatting
dnfield Jan 24, 2019
554ac6f
missing comment
dnfield Jan 24, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Update the mDNS native extension for Mac OS to handle PTR and SRV rec…
…ords

Change to use the API DNSServiceQueryRecord to support all record types.

[email protected], [email protected]
BUG=

Review URL: https://codereview.chromium.org/1411063006 .
  • Loading branch information
sgjesse authored and dnfield committed Oct 17, 2018
commit 67e73fc08a71db9c54a53898c99b0891c6c24982
2 changes: 1 addition & 1 deletion pkg/mdns/lib/native/libmdns_extension_lib.dylib.sha1
Original file line number Diff line number Diff line change
@@ -1 +1 @@
22e12795d48159983c756e96b4355ca63b020676
dd179a2f61425350ffe2c99201302d2343f80598
2 changes: 2 additions & 0 deletions pkg/mdns/lib/src/constants.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ const int arcountOffset = 10;

const int headerSize = 12;

const int srvHeaderSize = 6;

const int responseFlags = 0x8400;

class RRType {
Expand Down
58 changes: 41 additions & 17 deletions pkg/mdns/lib/src/native_extension_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -66,17 +66,13 @@ class NativeExtensionMDnsClient implements MDnsClient {
throw new StateError('mDNS client is not started');
}

if (type != RRType.A) {
// TODO(karlklose): add support.
throw 'RR type $type not supported.';
}

// Add the pending request before sending the query.
var result = _resolver.addPendingRequest(type, name, timeout);

// Send the request.
_service.send([_incoming.sendPort,
RequestType.lookupRequest.index,
type,
name]);

return result;
Expand All @@ -87,19 +83,47 @@ class NativeExtensionMDnsClient implements MDnsClient {
// Right now the only response we can get is the response to a
// lookupRequest where the response looks like this:
//
// response[0]: hostname (String)
// response[1]: IPv4 address (Uint8List)
if (response is List && response.length == 2) {
// response[0]: fullname (String)
// response[1]: type (int)
// response[2]: ttl (int)
// response[3]: data (Uint8List)
if (response is List && response.length == 4) {
if (response[0] is String &&
response[1] is List && response[1].length == 4) {
response = new ResourceRecord(
RRType.A,
response[0],
response[1].codeUnits,
// TODO(karlklose): modify extension to return TTL too. For new
// we set it to 2 seconds.
new DateTime.now().millisecondsSinceEpoch + 2000);
_resolver.handleResponse([response]);
response[1] is int &&
response[2] is int &&
response[3] is List) {
String fqdn = response[0];
// The strings from the C system can have \032 for spaces.
fqdn = fqdn.replaceAll(r'\032', ' ');
int type = response[1];
int ttl = response[2];
List<int> data = response[3];

int validUntil = new DateTime.now().millisecondsSinceEpoch + ttl * 1000;
ResourceRecord result;
switch (type) {
case RRType.A:
if (data.length == 4) {
var address =
new InternetAddress(data.map((n) => '$n').join('.'));
result = new ResourceRecord(type, fqdn, address, validUntil);
}
break;
case RRType.SRV:
// Skip the SRV record header for now - only read the name.
if (data.length > srvHeaderSize) {
var rData = readFQDN(data, srvHeaderSize);
result = new ResourceRecord(type, fqdn, rData, validUntil);
}
break;
case RRType.PTR:
var rData = readFQDN(data);
result = new ResourceRecord(type, fqdn, rData, validUntil);
break;
}
if (result != null) {
_resolver.handleResponse([result]);
}
} else {
// TODO(sgjesse): Improve the error handling.
print('mDNS Response not understood');
Expand Down
105 changes: 61 additions & 44 deletions pkg/mdns/lib/src/packet.dart
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,62 @@ class ResourceRecord {

/// Result of reading a FQDN.
class FQDNReadResult {
final List<String> fqdn;
final List<String> fqdnParts;
final int bytesRead;
FQDNReadResult(this.fqdn, this.bytesRead);
String get fqdn => fqdnParts.join('.');
FQDNReadResult(this.fqdnParts, this.bytesRead);
}

String readFQDN(List<int> packet, [int offset = 0]) {
Uint8List data =
packet is Uint8List ? packet : new Uint8List.fromList(packet);
ByteData bd = new ByteData.view(data.buffer);

return _readFQDN(data, bd, offset, data.length).fqdn;
}

// Read a FQDN at the given offset. Returns a pair with the FQDN
// parts and the number of bytes consumed.
//
// If decoding fails (e.g. due to an invalid packet) `null` is returned.
FQDNReadResult _readFQDN(Uint8List data, ByteData bd, int offset, int length) {
void checkLength(int required) {
if (length < required) throw new MDnsDecodeException(required);
}

List<String> parts = [];
int prevOffset = offset;
while (true) {
// At least one byte is required.
checkLength(offset + 1);

// Check for compressed.
if (data[offset] & 0xc0 == 0xc0) {
// At least two bytes are required for a compressed FQDN.
checkLength(offset + 2);

// A compressed FQDN has a new offset in the lower 14 bits.
FQDNReadResult result = _readFQDN(
data, bd, bd.getUint16(offset) & ~0xc000, length);
parts.addAll(result.fqdnParts);
offset += 2;
break;
} else {
// A normal FQDN part has a length and a UTF-8 encoded name
// part. If the length is 0 this is the end of the FQDN.
var partLength = data[offset];
offset++;
if (partLength > 0) {
checkLength(offset + partLength);
var partBytes = new Uint8List.view(data.buffer, offset, partLength);
offset += partLength;
parts.add(UTF8.decode(partBytes));
} else {
break;
}
}
}
return new FQDNReadResult(parts, offset - prevOffset);
}

/// Decode a mDNS package.
Expand Down Expand Up @@ -125,46 +178,10 @@ List<ResourceRecord> decodeMDnsResponse(List<int> packet) {
if (length < required) throw new MDnsDecodeException(required);
}

// Read a FQDN at the given offset. Returns a pair with the FQDN
// parts and the number of bytes consumed.
//
// If decoding fails (e.g. due to an invalid packet) `null` is returned.
FQDNReadResult readFQDN(int offset) {
List<String> parts = [];
int prevOffset = offset;
while (true) {
// At least two bytes required.
checkLength(offset + 2);

// Check for compressed.
if (data[offset] & 0xc0 == 0xc0) {
// A compressed FQDN has a new offset in the lower 14 bits.
FQDNReadResult result = readFQDN(bd.getUint16(offset) & ~0xc000);
parts.addAll(result.fqdn);
offset += 2;
break;
} else {
// A normal FQDN part has a length and a UTF-8 encoded name
// part. If the length is 0 this is the end of the FQDN.
var partLength = data[offset];
offset++;
if (partLength != 0) {
checkLength(offset + partLength);
var partBytes = new Uint8List.view(data.buffer, offset, partLength);
offset += partLength;
parts.add(UTF8.decode(partBytes));
} else {
break;
}
}
}
return new FQDNReadResult(parts, offset - prevOffset);
}

ResourceRecord readResourceRecord() {
// First read the FQDN.
FQDNReadResult result = readFQDN(offset);
var fqdn = result.fqdn.join('.');
FQDNReadResult result = _readFQDN(data, bd, offset, length);
var fqdn = result.fqdn;
offset += result.bytesRead;
checkLength(offset + 2);
int type = bd.getUint16(offset);
Expand Down Expand Up @@ -201,15 +218,15 @@ List<ResourceRecord> decodeMDnsResponse(List<int> packet) {
checkLength(offset + 2);
int port = bd.getUint16(offset);
offset += 2;
FQDNReadResult result = readFQDN(offset);
rData = result.fqdn.join('.');
FQDNReadResult result = _readFQDN(data, bd, offset, length);
rData = result.fqdn;
offset += rDataLength - 6;
break;
case RRType.PTR:
checkLength(offset + rDataLength);
FQDNReadResult result = readFQDN(offset);
FQDNReadResult result = _readFQDN(data, bd, offset, length);
offset += rDataLength;
rData = result.fqdn.join('.');
rData = result.fqdn;
break;
case RRType.TXT:
// TODO(karlklose): convert to a String or Map.
Expand Down
41 changes: 41 additions & 0 deletions pkg/mdns/test/decode_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@
// BSD-style license that can be found in the LICENSE.md file.

import 'package:expect/expect.dart';
import 'package:mdns/src/constants.dart';
import 'package:mdns/src/packet.dart';

main() {
testValidPackages();
testBadPackages();
testHexDumpList();
testPTRRData();
testSRVRData();
}

testValidPackages() {
Expand Down Expand Up @@ -58,6 +61,18 @@ testBadPackages() {
}
}

testPTRRData() {
Expect.equals(
'sgjesse-macbookpro2 [78:31:c1:b8:55:38]._workstation._tcp.local',
readFQDN(ptrRData));
print(readFQDN(ptrRData2));
Expect.equals('fletch-agent._fletch_agent._tcp.local', readFQDN(ptrRData2));
}

testSRVRData() {
Expect.equals('fletch.local', readFQDN(srvRData, srvHeaderSize));
}

testHexDumpList() {
// Call hexDumpList to get rid of hint.
formatHexStream("");
Expand Down Expand Up @@ -144,6 +159,32 @@ var packagePtrResponse = <int>[
0x78, 0x00, 0x04, 0xa9, 0xfe, 0xa7, 0xac
];

var ptrRData = <int>[
0x27, 0x73, 0x67, 0x6a, 0x65, 0x73, 0x73, 0x65,
0x2d, 0x6d, 0x61, 0x63, 0x62, 0x6f, 0x6f, 0x6b,
0x70, 0x72, 0x6f, 0x32, 0x20, 0x5b, 0x37, 0x38,
0x3a, 0x33, 0x31, 0x3a, 0x63, 0x31, 0x3a, 0x62,
0x38, 0x3a, 0x35, 0x35, 0x3a, 0x33, 0x38, 0x5d,
0x0c, 0x5f, 0x77, 0x6f, 0x72, 0x6b, 0x73, 0x74,
0x61, 0x74, 0x69, 0x6f, 0x6e, 0x04, 0x5f, 0x74,
0x63, 0x70, 0x05, 0x6c, 0x6f, 0x63, 0x61, 0x6c,
0x00
];

var ptrRData2 = <int>[
0x0c, 0x66, 0x6c, 0x65, 0x74, 0x63, 0x68, 0x2d,
0x61, 0x67, 0x65, 0x6e, 0x74, 0x0d, 0x5f, 0x66,
0x6c, 0x65, 0x74, 0x63, 0x68, 0x5f, 0x61, 0x67,
0x65, 0x6e, 0x74, 0x04, 0x5f, 0x74, 0x63, 0x70,
0x05, 0x6c, 0x6f, 0x63, 0x61, 0x6c, 0x00
];

var srvRData = <int> [
0x00, 0x00, 0x00, 0x00, 0x2f, 0x59, 0x06, 0x66,
0x6c, 0x65, 0x74, 0x63, 0x68, 0x05, 0x6c, 0x6f,
0x63, 0x61, 0x6c, 0x00
];

// Support code to generate the hex-lists above from
// a hex-stream.
void formatHexStream(String hexStream) {
Expand Down