OLD | NEW |
1 // Copyright (c) 2015, the Fletch 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.md file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | |
5 library mdns; | |
6 | 4 |
7 import 'dart:async'; | 5 import 'dart:async'; |
8 import 'dart:collection'; | 6 import 'dart:collection'; |
9 import 'dart:io'; | 7 import 'dart:io'; |
10 import 'dart:typed_data'; | 8 import 'dart:typed_data'; |
11 | 9 |
12 import 'package:mdns/src/native_extension_client.dart'; | 10 import 'package:mdns/src/constants.dart'; |
13 import 'package:mdns/src/native_protocol_client.dart'; | 11 import 'package:mdns/src/lookup_resolver.dart'; |
| 12 import 'package:mdns/src/packet.dart'; |
14 | 13 |
15 /// Client for DNS lookup using the mDNS protocol. | 14 /// Client for DNS lookup using the mDNS protocol. |
16 /// | 15 /// |
17 /// This client only support "One-Shot Multicast DNS Queries" as described in | 16 /// This client only support "One-Shot Multicast DNS Queries" as described in |
18 /// section 5.1 of https://tools.ietf.org/html/rfc6762 | 17 /// section 5.1 of https://tools.ietf.org/html/rfc6762 |
19 abstract class MDnsClient { | 18 class MDnsClient { |
20 // Instantiate Client for DNS lookup using the mDNS protocol. | 19 bool _starting = false; |
21 // | 20 bool _started = false; |
22 // On Mac OS a native extension is used as the mDNSResponder opens the mDNS | 21 RawDatagramSocket _incoming; |
23 // port in exclusive mode. To test the protocol implementation on Mac OS | 22 final List<RawDatagramSocket> _sockets = <RawDatagramSocket>[]; |
24 // one can turn off mDNSResponder: | 23 final LookupResolver _resolver = new LookupResolver(); |
25 // | 24 |
26 // sudo launchctl unload -w \ | 25 /// Start the mDNS client. |
27 // /System/Library/LaunchDaemons/com.apple.mDNSResponder.plist | 26 Future start() async { |
28 // | 27 if (_started && _starting) { |
29 // And turn it on again: | 28 throw new StateError('mDNS client already started'); |
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 } | 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; |
39 } | 52 } |
40 | 53 |
41 /// Start the mDNS client. | 54 /// Stop the mDNS client. |
42 Future start(); | 55 void stop() { |
| 56 if (!_started) return; |
| 57 if (_starting) { |
| 58 throw new StateError('Cannot stop mDNS client wile it is starting'); |
| 59 } |
43 | 60 |
44 /// Stop the mDNS client. | 61 _sockets.forEach((socket) => socket.close()); |
45 void stop(); | 62 _incoming.close(); |
| 63 |
| 64 _started = false; |
| 65 } |
46 | 66 |
47 /// Lookup [hostname] using mDNS. | 67 /// Lookup [hostname] using mDNS. |
48 /// | 68 /// |
49 /// The `hostname` must have the form `single-dns-label.local`, | 69 /// The `hostname` must have the form `single-dns-label.local`, |
50 /// e.g. `printer.local`. | 70 /// e.g. `printer.local`. |
51 /// | 71 /// |
52 /// If no answer has been received within the specified [timeout] | 72 /// If no answer has been received within the specified [timeout] |
53 /// this method will complete with the value `null`. | 73 /// this method will complete with the value `null`. |
54 Future<InternetAddress> lookup( | 74 Future<InternetAddress> lookup( |
55 String hostname, {Duration timeout: const Duration(seconds: 5)}); | 75 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 } |
56 } | 102 } |
57 | 103 |
58 // Simple standalone test. | 104 // Simple standalone test. |
59 Future main(List<String> args) async { | 105 main() async { |
60 var client = new MDnsClient(); | 106 var client = new MDnsClient(); |
61 await client.start(); | 107 await client.start(); |
62 var address = await client.lookup(args[0]); | 108 var address = await client.lookup('raspberrypi.local'); |
63 client.stop(); | 109 client.stop(); |
64 print(address); | 110 print(address); |
65 } | 111 } |
OLD | NEW |