Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(317)

Side by Side Diff: pkg/mdns/lib/mdns.dart

Issue 1403303012: Re-land "Extend the mDNS package with a native extension used on Mac OS" (Closed) Base URL: git@github.com:dart-lang/fletch.git@master
Patch Set: Added mussing scripts Created 5 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « fletch.gyp ('k') | pkg/mdns/lib/native/libmdns_extension_lib.dylib.sha1 » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2015, the Fletch 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.md file.
4
5 library mdns;
4 6
5 import 'dart:async'; 7 import 'dart:async';
6 import 'dart:collection'; 8 import 'dart:collection';
7 import 'dart:io'; 9 import 'dart:io';
8 import 'dart:typed_data'; 10 import 'dart:typed_data';
9 11
10 import 'package:mdns/src/constants.dart'; 12 import 'package:mdns/src/native_extension_client.dart';
11 import 'package:mdns/src/lookup_resolver.dart'; 13 import 'package:mdns/src/native_protocol_client.dart';
12 import 'package:mdns/src/packet.dart';
13 14
14 /// Client for DNS lookup using the mDNS protocol. 15 /// Client for DNS lookup using the mDNS protocol.
15 /// 16 ///
16 /// This client only support "One-Shot Multicast DNS Queries" as described in 17 /// This client only support "One-Shot Multicast DNS Queries" as described in
17 /// section 5.1 of https://tools.ietf.org/html/rfc6762 18 /// section 5.1 of https://tools.ietf.org/html/rfc6762
18 class MDnsClient { 19 abstract class MDnsClient {
19 bool _starting = false; 20 // Instantiate Client for DNS lookup using the mDNS protocol.
20 bool _started = false; 21 //
21 RawDatagramSocket _incoming; 22 // On Mac OS a native extension is used as the mDNSResponder opens the mDNS
22 final List<RawDatagramSocket> _sockets = <RawDatagramSocket>[]; 23 // port in exclusive mode. To test the protocol implementation on Mac OS
23 final LookupResolver _resolver = new LookupResolver(); 24 // one can turn off mDNSResponder:
25 //
26 // sudo launchctl unload -w \
27 // /System/Library/LaunchDaemons/com.apple.mDNSResponder.plist
28 //
29 // And turn it on again:
30 //
31 // sudo launchctl load -w \
32 // /System/Library/LaunchDaemons/com.apple.mDNSResponder.plist
33 factory MDnsClient() {
34 if (Platform.isMacOS) {
35 return new NativeExtensionMDnsClient();
36 } else {
37 return new NativeProtocolMDnsClient();
38 }
39 }
24 40
25 /// Start the mDNS client. 41 /// Start the mDNS client.
26 Future start() async { 42 Future start();
27 if (_started && _starting) {
28 throw new StateError('mDNS client already started');
29 }
30 _starting = true;
31
32 // Listen on all addresses.
33 _incoming = await RawDatagramSocket.bind(
34 InternetAddress.ANY_IP_V4, mDnsPort, reuseAddress: true);
35
36 // Find all network interfaces with an IPv4 address.
37 var interfaces =
38 await NetworkInterface.list(type: InternetAddressType.IP_V4);
39 for (NetworkInterface interface in interfaces) {
40 // Create a socket for sending on each adapter.
41 var socket = await RawDatagramSocket.bind(
42 interface.addresses[0], mDnsPort, reuseAddress: true);
43 _sockets.add(socket);
44
45 // Join multicast on this interface.
46 _incoming.joinMulticast(mDnsAddress, interface);
47 }
48 _incoming.listen(_handleIncoming);
49
50 _starting = false;
51 _started = true;
52 }
53 43
54 /// Stop the mDNS client. 44 /// Stop the mDNS client.
55 void stop() { 45 void stop();
56 if (!_started) return;
57 if (_starting) {
58 throw new StateError('Cannot stop mDNS client wile it is starting');
59 }
60
61 _sockets.forEach((socket) => socket.close());
62 _incoming.close();
63
64 _started = false;
65 }
66 46
67 /// Lookup [hostname] using mDNS. 47 /// Lookup [hostname] using mDNS.
68 /// 48 ///
69 /// The `hostname` must have the form `single-dns-label.local`, 49 /// The `hostname` must have the form `single-dns-label.local`,
70 /// e.g. `printer.local`. 50 /// e.g. `printer.local`.
71 /// 51 ///
72 /// If no answer has been received within the specified [timeout] 52 /// If no answer has been received within the specified [timeout]
73 /// this method will complete with the value `null`. 53 /// this method will complete with the value `null`.
74 Future<InternetAddress> lookup( 54 Future<InternetAddress> lookup(
75 String hostname, {Duration timeout: const Duration(seconds: 5)}) { 55 String hostname, {Duration timeout: const Duration(seconds: 5)});
76 if (!_started) {
77 throw new StateError('mDNS client is not started');
78 }
79
80 // Add the pending request before sending the query.
81 var future = _resolver.addPendingRequest(hostname, timeout);
82
83 // Send the request on all interfaces.
84 List<int> packet = encodeMDnsQuery(hostname);
85 for (int i = 0; i < _sockets.length; i++) {
86 _sockets[i].send(packet, mDnsAddress, mDnsPort);
87 }
88
89 return future;
90 }
91
92 // Process incoming datagrams.
93 _handleIncoming(event) {
94 if (event == RawSocketEvent.READ) {
95 var data = _incoming.receive();
96 var response = decodeMDnsResponse(data.data);
97 if (response != null) {
98 _resolver.handleResponse(response);
99 }
100 }
101 }
102 } 56 }
103 57
104 // Simple standalone test. 58 // Simple standalone test.
105 main() async { 59 Future main(List<String> args) async {
106 var client = new MDnsClient(); 60 var client = new MDnsClient();
107 await client.start(); 61 await client.start();
108 var address = await client.lookup('raspberrypi.local'); 62 var address = await client.lookup(args[0]);
109 client.stop(); 63 client.stop();
110 print(address); 64 print(address);
111 } 65 }
OLDNEW
« no previous file with comments | « fletch.gyp ('k') | pkg/mdns/lib/native/libmdns_extension_lib.dylib.sha1 » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698