OLD | NEW |
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.src.native_protocol_client; |
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'; | |
9 | 10 |
| 11 import 'package:mdns/mdns.dart'; |
10 import 'package:mdns/src/constants.dart'; | 12 import 'package:mdns/src/constants.dart'; |
11 import 'package:mdns/src/lookup_resolver.dart'; | 13 import 'package:mdns/src/lookup_resolver.dart'; |
12 import 'package:mdns/src/packet.dart'; | 14 import 'package:mdns/src/packet.dart'; |
13 | 15 |
14 /// Client for DNS lookup using the mDNS protocol. | 16 // Implementation of mDNS client using the native protocol. |
15 /// | 17 class NativeProtocolMDnsClient implements MDnsClient { |
16 /// This client only support "One-Shot Multicast DNS Queries" as described in | |
17 /// section 5.1 of https://tools.ietf.org/html/rfc6762 | |
18 class MDnsClient { | |
19 bool _starting = false; | 18 bool _starting = false; |
20 bool _started = false; | 19 bool _started = false; |
21 RawDatagramSocket _incoming; | 20 RawDatagramSocket _incoming; |
22 final List<RawDatagramSocket> _sockets = <RawDatagramSocket>[]; | 21 final List<RawDatagramSocket> _sockets = <RawDatagramSocket>[]; |
23 final LookupResolver _resolver = new LookupResolver(); | 22 final LookupResolver _resolver = new LookupResolver(); |
24 | 23 |
25 /// Start the mDNS client. | 24 /// Start the mDNS client. |
26 Future start() async { | 25 Future start() async { |
27 if (_started && _starting) { | 26 if (_started && _starting) { |
28 throw new StateError('mDNS client already started'); | 27 throw new StateError('mDNS client already started'); |
(...skipping 15 matching lines...) Expand all Loading... |
44 | 43 |
45 // Join multicast on this interface. | 44 // Join multicast on this interface. |
46 _incoming.joinMulticast(mDnsAddress, interface); | 45 _incoming.joinMulticast(mDnsAddress, interface); |
47 } | 46 } |
48 _incoming.listen(_handleIncoming); | 47 _incoming.listen(_handleIncoming); |
49 | 48 |
50 _starting = false; | 49 _starting = false; |
51 _started = true; | 50 _started = true; |
52 } | 51 } |
53 | 52 |
54 /// Stop the mDNS client. | |
55 void stop() { | 53 void stop() { |
56 if (!_started) return; | 54 if (!_started) return; |
57 if (_starting) { | 55 if (_starting) { |
58 throw new StateError('Cannot stop mDNS client wile it is starting'); | 56 throw new StateError('Cannot stop mDNS client wile it is starting'); |
59 } | 57 } |
60 | 58 |
61 _sockets.forEach((socket) => socket.close()); | 59 _sockets.forEach((socket) => socket.close()); |
62 _incoming.close(); | 60 _incoming.close(); |
63 | 61 |
64 _started = false; | 62 _started = false; |
65 } | 63 } |
66 | 64 |
67 /// Lookup [hostname] using mDNS. | |
68 /// | |
69 /// The `hostname` must have the form `single-dns-label.local`, | |
70 /// e.g. `printer.local`. | |
71 /// | |
72 /// If no answer has been received within the specified [timeout] | |
73 /// this method will complete with the value `null`. | |
74 Future<InternetAddress> lookup( | 65 Future<InternetAddress> lookup( |
75 String hostname, {Duration timeout: const Duration(seconds: 5)}) { | 66 String hostname, {Duration timeout: const Duration(seconds: 5)}) { |
76 if (!_started) { | 67 if (!_started) { |
77 throw new StateError('mDNS client is not started'); | 68 throw new StateError('mDNS client is not started'); |
78 } | 69 } |
79 | 70 |
80 // Add the pending request before sending the query. | 71 // Add the pending request before sending the query. |
81 var future = _resolver.addPendingRequest(hostname, timeout); | 72 var future = _resolver.addPendingRequest(hostname, timeout); |
82 | 73 |
83 // Send the request on all interfaces. | 74 // Send the request on all interfaces. |
84 List<int> packet = encodeMDnsQuery(hostname); | 75 List<int> packet = encodeMDnsQuery(hostname); |
85 for (int i = 0; i < _sockets.length; i++) { | 76 for (int i = 0; i < _sockets.length; i++) { |
86 _sockets[i].send(packet, mDnsAddress, mDnsPort); | 77 _sockets[i].send(packet, mDnsAddress, mDnsPort); |
87 } | 78 } |
88 | 79 |
89 return future; | 80 return future; |
90 } | 81 } |
91 | 82 |
92 // Process incoming datagrams. | 83 // Process incoming datagrams. |
93 _handleIncoming(event) { | 84 _handleIncoming(event) { |
94 if (event == RawSocketEvent.READ) { | 85 if (event == RawSocketEvent.READ) { |
95 var data = _incoming.receive(); | 86 var data = _incoming.receive(); |
96 var response = decodeMDnsResponse(data.data); | 87 var response = decodeMDnsResponse(data.data); |
97 if (response != null) { | 88 if (response != null) { |
98 _resolver.handleResponse(response); | 89 _resolver.handleResponse(response); |
99 } | 90 } |
100 } | 91 } |
101 } | 92 } |
102 } | 93 } |
103 | |
104 // Simple standalone test. | |
105 main() async { | |
106 var client = new MDnsClient(); | |
107 await client.start(); | |
108 var address = await client.lookup('raspberrypi.local'); | |
109 client.stop(); | |
110 print(address); | |
111 } | |
OLD | NEW |