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
Initial refactor/adding of mdns
  • Loading branch information
dnfield committed Oct 18, 2018
commit 13440cc41d0c4be1e24eec655d2e59ff6d3b35fc
1 change: 1 addition & 0 deletions packages/mdns/.dart_tool/pub/bin/sdk-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
2.1.0-dev.7.1.flutter-b99bcfd309
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@

import 'package:args/args.dart';

import '../lib/mdns.dart';
import 'package:dart_mdns/mdns_client.dart';

main(List<String> args) async {
void main(List<String> args) async {
// Parse the command line arguments.
var parser = new ArgParser();
final ArgParser parser = ArgParser();
parser.addOption('timeout', abbr: 't', defaultsTo: '5');
var arguments = parser.parse(args);
final ArgResults arguments = parser.parse(args);

if (arguments.rest.length != 1) {
print('''
Expand All @@ -24,14 +24,13 @@ For example:
return;
}

var name = arguments.rest[0];
final String name = arguments.rest[0];

MDnsClient client = new MDnsClient();
final MDnsClient client = MDnsClient();
await client.start();
var timeout;
timeout = new Duration(seconds: int.parse(arguments['timeout']));
await for (ResourceRecord record in
client.lookup(RRType.A, name, timeout: timeout)) {
final Duration timeout = Duration(seconds: int.parse(arguments['timeout']));
await for (IPAddressResourceRecord record
in client.lookup(RRType.a, name, timeout: timeout)) {
print('Found address (${record.address}).');
}
client.stop();
Expand Down
27 changes: 16 additions & 11 deletions pkg/mdns/bin/mdns-sd.dart → packages/mdns/bin/mdns-sd.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@

import 'package:args/args.dart';

import '../lib/mdns.dart';
import 'package:dart_mdns/mdns_client.dart';

main(List<String> args) async {
void main(List<String> args) async {
// Parse the command line arguments.
var parser = new ArgParser();
final ArgParser parser = ArgParser();
parser.addOption('timeout', abbr: 't', defaultsTo: '5');
var arguments = parser.parse(args);
final ArgResults arguments = parser.parse(args);

if (arguments.rest.length != 1) {
print('''
Expand All @@ -24,15 +24,20 @@ For example:
return;
}

var name = arguments.rest[0];
final String name = arguments.rest[0];

MDnsClient client = new MDnsClient();
final MDnsClient client = MDnsClient();
await client.start();
await for (ResourceRecord ptr in client.lookup(RRType.PTR, name)) {
String domain = ptr.domainName;
await for (ResourceRecord srv in client.lookup(RRType.SRV, domain)) {
String target = srv.target;
await for (ResourceRecord ip in client.lookup(RRType.A, target)) {
await for (PtrResourceRecord ptr in client.lookup(RRType.ptr, name)) {
final String domain = ptr.domainName;
print(ptr);
await for (SrvResourceRecord srv in client.lookup(RRType.srv, domain)) {
final String target = srv.target;
print(srv);
await client.lookup(RRType.txt, domain).forEach(print);
await for (IPAddressResourceRecord ip
in client.lookup(RRType.a, target)) {
print(ip);
print('Service instance found at $target (${ip.address}).');
}
}
Expand Down
1 change: 1 addition & 0 deletions packages/mdns/lib/dart_mdns.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export 'mdns_client.dart';
138 changes: 138 additions & 0 deletions packages/mdns/lib/mdns_client.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
// ignore_for_file: undefined_named_parameter
// Copyright (c) 2015, the Dartino project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE.md file.

import 'dart:async';
import 'dart:io';

import 'package:dart_mdns/src/constants.dart';
import 'package:dart_mdns/src/lookup_resolver.dart';
import 'package:dart_mdns/src/native_protocol_client.dart';
import 'package:dart_mdns/src/packet.dart';

export 'package:dart_mdns/src/constants.dart' show RRType;
export 'package:dart_mdns/src/packet.dart'
show
ResourceRecord,
IPAddressResourceRecord,
SrvResourceRecord,
TxtResourceRecord,
PtrResourceRecord;

/// Client for DNS lookup using the mDNS protocol.
///
/// This client only support "One-Shot Multicast DNS Queries" as described in
/// section 5.1 of https://tools.ietf.org/html/rfc6762
class MDnsClient {
bool _starting = false;
bool _started = false;
RawDatagramSocket _incoming;
final List<RawDatagramSocket> _sockets = <RawDatagramSocket>[];
final LookupResolver _resolver = LookupResolver();
final ResourceRecordCache _cache = ResourceRecordCache();

/// Start the mDNS client.
Future<void> start() async {
if (_started && _starting) {
throw StateError('mDNS client already started');
}
_starting = true;

// Listen on all addresses.
_incoming = await RawDatagramSocket.bind(
InternetAddress.anyIPv4,
mDnsPort,
reuseAddress: true,
reusePort: true,
ttl: 255,
);
// Find all network interfaces with an IPv4 address.
final List<NetworkInterface> interfaces = await NetworkInterface.list(
includeLinkLocal: true,
type: InternetAddressType.IPv4,
includeLoopback: true,
);

_sockets.add(_incoming);

for (NetworkInterface interface in interfaces) {
// Create a socket for sending on each adapter.
final RawDatagramSocket socket = await RawDatagramSocket.bind(
interface.addresses[0],
mDnsPort,
reuseAddress: true,
reusePort: true,
ttl: 255,
);
_sockets.add(socket);

// Join multicast on this interface.
_incoming.joinMulticast(mDnsAddress, interface);
}
_incoming.listen(_handleIncoming);

_starting = false;
_started = true;
}

/// Stop the client and close any associated sockets.
void stop() {
if (!_started) {
return;
}
if (_starting) {
throw StateError('Cannot stop mDNS client wile it is starting');
}

for (RawDatagramSocket socket in _sockets) {
socket.close();
}

_resolver.clearPendingRequests();

_started = false;
}

/// Lookup a [ResourceRecord], potentially from cache.
Stream<ResourceRecord> lookup(int type, String name,
{Duration timeout = const Duration(seconds: 5)}) {
if (!_started) {
throw StateError('mDNS client is not started');
}
// Look for entries in the cache.
final List<ResourceRecord> cached = <ResourceRecord>[];
_cache.lookup(name, type, cached);
if (cached.isNotEmpty) {
final StreamController<ResourceRecord> controller =
StreamController<ResourceRecord>();
cached.forEach(controller.add);
controller.close();
return controller.stream;
}

// Add the pending request before sending the query.
final Stream<ResourceRecord> results =
_resolver.addPendingRequest(type, name, timeout);

// Send the request on all interfaces.
final List<int> packet = encodeMDnsQuery(name, type);
for (RawDatagramSocket socket in _sockets) {
socket.send(packet, mDnsAddress, mDnsPort);
}
return results;
}

// Process incoming datagrams.
void _handleIncoming(RawSocketEvent event) {
if (event == RawSocketEvent.read) {
final Datagram datagram = _incoming.receive();

final List<ResourceRecord> response = decodeMDnsResponse(datagram.data);
if (response != null) {
_cache.updateRecords(response);
_resolver.handleResponse(response);
}
}
}
}
44 changes: 44 additions & 0 deletions packages/mdns/lib/src/constants.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Copyright (c) 2015, the Dartino project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE.md file.

import 'dart:io';

/// The IPv4 mDNS Address.
InternetAddress mDnsAddress = InternetAddress('224.0.0.251');

/// The mDNS port.
const int mDnsPort = 5353;

/// Enumeration of support resource record types.
class RRType {
/// An IPv4 Address record (1).
static const int a = 1;

/// An IPv6 Address record (28).
static const int aaaa = 28;

/// An IP Address reverse map record (12).
static const int ptr = 12;

/// An available service record (33).
static const int srv = 33;

/// A text record (16).
static const int txt = 16;
}

/// Enumeration of supported resource record class types.
class RRClass {
/// Internet address class ("IN").
static const int internet = 1;
}

/// Enumeration of DNS question types.
class QuestionType {
/// "QU" Question.
static const int unicast = 0x8000;

/// "QM" Question.
static const int multicast = 0x0000;
}
83 changes: 83 additions & 0 deletions packages/mdns/lib/src/lookup_resolver.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
// Copyright (c) 2015, the Dartino project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE.md file.

import 'dart:async';
import 'dart:collection';

import 'package:dart_mdns/src/packet.dart';

/// Class for maintaining state about pending mDNS requests.
class PendingRequest extends LinkedListEntry<PendingRequest> {
/// Creates a new PendingRequest.
PendingRequest(this.type, this.name, this.controller);

/// The [RRType] of the request.
final int type;

/// The domain name.
final String name;

/// A StreamController managing the request.
final StreamController<ResourceRecord> controller;

/// The timer for the request.
Timer timer;
}

/// Class for keeping track of pending lookups and process incoming
/// query responses.
class LookupResolver {
/// The requests the process.
final LinkedList<PendingRequest> pendingRequests =
LinkedList<PendingRequest>();

/// Adds a request and returns a [Stream] of [ResourceRecord] responses.
Stream<ResourceRecord> addPendingRequest(
int type, String name, Duration timeout) {
final StreamController<ResourceRecord> controller =
StreamController<ResourceRecord>();
final PendingRequest request = PendingRequest(type, name, controller);
final Timer timer = Timer(timeout, () {
request.unlink();
controller.close();
});
request.timer = timer;
pendingRequests.add(request);
return controller.stream;
}

/// Processes responses back to the caller.
void handleResponse(List<ResourceRecord> response) {
for (ResourceRecord r in response) {
final int type = r.rrValue;
String name = r.name.toLowerCase();
if (name.endsWith('.')) {
name = name.substring(0, name.length - 1);
}

bool responseMatches(PendingRequest request) {
return request.name.toLowerCase() == name && request.type == type;
}

for (PendingRequest pendingRequest in pendingRequests) {
if (responseMatches(pendingRequest)) {
if (pendingRequest.controller.isClosed) {
return;
}
pendingRequest.controller.add(r);
}
}
}
}

/// Removes any pending requests and ends processing.
void clearPendingRequests() {
while (pendingRequests.isNotEmpty) {
final PendingRequest request = pendingRequests.first;
request.unlink();
request.timer.cancel();
request.controller.close();
}
}
}
Loading