OLD | NEW |
(Empty) | |
| 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 |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 import 'dart:async'; |
| 6 import 'dart:collection'; |
| 7 import 'dart:io'; |
| 8 import 'dart:typed_data'; |
| 9 |
| 10 import 'package:mdns/src/constants.dart'; |
| 11 import 'package:mdns/src/lookup_resolver.dart'; |
| 12 import 'package:mdns/src/packet.dart'; |
| 13 |
| 14 /// Client for DNS lookup using the mDNS protocol. |
| 15 /// |
| 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; |
| 20 bool _started = false; |
| 21 RawDatagramSocket _incoming; |
| 22 final List<RawDatagramSocket> _sockets = <RawDatagramSocket>[]; |
| 23 final LookupResolver _resolver = new LookupResolver(); |
| 24 |
| 25 /// Start the mDNS client. |
| 26 Future start() async { |
| 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 |
| 54 /// Stop the mDNS client. |
| 55 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 |
| 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( |
| 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 } |
| 102 } |
| 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 |