Line data Source code
1 : import 'package:network_tools/src/models/active_host.dart';
2 : import 'package:network_tools/src/models/callbacks.dart';
3 : import 'package:network_tools/src/models/sendable_active_host.dart';
4 : import 'package:network_tools/src/services/port_scanner_service.dart';
5 :
6 : /// Scans for all hosts in a subnet.
7 : ///
8 : /// On Linux, macOS and Windows discovery is ARP-first: the scanner primes the
9 : /// neighbour cache and reads the numeric ARP table once, which is much faster
10 : /// than pinging every address and also finds hosts that ignore ICMP. Mobile
11 : /// platforms, non-local subnets and loopback use an ICMP sweep instead.
12 : ///
13 : /// ARP does not carry host names. Name lookups run concurrently for the hosts
14 : /// that were found and are attached to [ActiveHost.hostName]; a network
15 : /// without reverse DNS simply yields no names.
16 : abstract class HostScannerService {
17 7 : HostScannerService() {
18 : _instance = this;
19 : }
20 :
21 3 : static late HostScannerService _instance;
22 :
23 : /// The registered scanner implementation.
24 : ///
25 : /// Available after the package is configured, usually by calling
26 : /// `configureNetworkTools` from `main`.
27 6 : static HostScannerService get instance => _instance;
28 :
29 : /// Devices scan will start from this integer Id
30 : static const int defaultFirstHostId = 1;
31 :
32 : /// Devices scan will stop at this integer id
33 : static const int defaultLastHostId = 254;
34 :
35 : /// Streams every reachable device in [subnet] (e.g. `192.168.1`).
36 : ///
37 : /// [hostIds] limits the scan to specific host ids and takes precedence over
38 : /// the [firstHostId]..[lastHostId] range. [timeoutInSeconds] is the ICMP
39 : /// timeout used to confirm hosts found through the neighbor table and for
40 : /// the sweep fallback.
41 : ///
42 : /// Results are emitted in ascending address order unless
43 : /// [resultsInAddressAscendingOrder] is `false`, which delivers each host as
44 : /// soon as it is confirmed. [progressCallback] receives values up to 100;
45 : /// it is not called in unordered mode. Cancel the subscription when the
46 : /// stream is no longer needed.
47 : ///
48 : /// Host details ([ActiveHost.hostName], [ActiveHost.deviceName],
49 : /// [ActiveHost.getMacAddress], [ActiveHost.vendor]) are resolved lazily, so
50 : /// they do not delay discovery. Discovery may already have resolved the
51 : /// host name; otherwise the first access performs the lookup.
52 : Stream<ActiveHost> getAllPingableDevices(
53 : String subnet, {
54 : int firstHostId = defaultFirstHostId,
55 : int lastHostId = defaultLastHostId,
56 : List<int> hostIds = const [],
57 : int timeoutInSeconds = 2,
58 : ProgressCallback? progressCallback,
59 : bool resultsInAddressAscendingOrder = true,
60 : });
61 :
62 : /// Same as [getAllPingableDevices], but emits sendable objects that can cross
63 : /// an isolate boundary. MAC addresses and host names discovered during the
64 : /// scan are carried on [SendableActiveHost].
65 : Stream<SendableActiveHost> getAllSendablePingableDevices(
66 : String subnet, {
67 : int firstHostId = defaultFirstHostId,
68 : int lastHostId = defaultLastHostId,
69 : List<int> hostIds = const [],
70 : int timeoutInSeconds = 2,
71 : ProgressCallback? progressCallback,
72 : bool resultsInAddressAscendingOrder = true,
73 : });
74 :
75 : /// Validates the requested range and returns the last host id to scan.
76 : ///
77 : /// The maximum host id depends on the subnet class. Throws a [String] when
78 : /// the range is invalid or outside the supported host ids.
79 : int validateAndGetLastValidSubnet(
80 : String subnet,
81 : int firstHostId,
82 : int lastHostId,
83 : );
84 :
85 : /// Same as [getAllPingableDevices], but runs the entire scan in a separate
86 : /// isolate so the calling isolate (usually the UI) stays responsive.
87 : ///
88 : /// The first result arrives after the isolate starts and the neighbor table
89 : /// is read; the vendor table is not built there because lookups happen when
90 : /// host details are accessed in the calling isolate.
91 : Stream<ActiveHost> getAllPingableDevicesAsync(
92 : String subnet, {
93 : int firstHostId = defaultFirstHostId,
94 : int lastHostId = defaultLastHostId,
95 : List<int> hostIds = const [],
96 : int timeoutInSeconds = 2,
97 : ProgressCallback? progressCallback,
98 : bool resultsInAddressAscendingOrder = true,
99 : });
100 :
101 : /// Streams the devices in [subnet] that have [port] open.
102 : ///
103 : /// The ICMP host scan runs first, then a bounded socket pool (see
104 : /// [maxConcurrency]) connects to [port] on each device. Results follow the
105 : /// same ordering and progress rules as [getAllPingableDevices].
106 : Stream<ActiveHost> scanDevicesForSinglePort(
107 : String subnet,
108 : int port, {
109 : int firstHostId = defaultFirstHostId,
110 : int lastHostId = defaultLastHostId,
111 : Duration timeout = const Duration(milliseconds: 2000),
112 : ProgressCallback? progressCallback,
113 : bool resultsInAddressAscendingOrder = true,
114 : int maxConcurrency = PortScannerService.defaultMaxConcurrency,
115 : });
116 : }
|