diff --git a/lib/src/device_info/vendor_table.dart b/lib/src/device_info/vendor_table.dart index 1131816..dfacec5 100644 --- a/lib/src/device_info/vendor_table.dart +++ b/lib/src/device_info/vendor_table.dart @@ -10,20 +10,14 @@ import 'package:universal_io/io.dart'; class VendorTable { static Map _vendorTableMap = {}; - static Future getVendor(Future arpDataFuture) async { - final arpData = await arpDataFuture; - if (arpData != null) { - if (arpData.notNullMacAddress) { - await createVendorTableMap(); - final pattern = arpData.macAddress.contains(':') ? ':' : '-'; - return _vendorTableMap[arpData.macAddress - .split(pattern) - .sublist(0, 3) - .join() - .toUpperCase()] as Vendor?; - } - } - return null; + static Future macToVendor(String macAddress) async { + await createVendorTableMap(); + final pattern = macAddress.contains(':') ? ':' : '-'; + return _vendorTableMap[macAddress + .split(pattern) + .sublist(0, 3) + .join() + .toUpperCase()] as Vendor?; } static Future createVendorTableMap() async { diff --git a/lib/src/models/active_host.dart b/lib/src/models/active_host.dart index d3dcb3f..623407e 100644 --- a/lib/src/models/active_host.dart +++ b/lib/src/models/active_host.dart @@ -10,9 +10,11 @@ class ActiveHost { ActiveHost({ required this.internetAddress, this.openPorts = const [], + String? macAddress, PingData? pingData, MdnsInfo? mdnsInfoVar, }) { + _macAddress = macAddress; final String tempAddress = internetAddress.address; if (tempAddress.contains('.')) { @@ -53,6 +55,7 @@ class ActiveHost { } factory ActiveHost.buildWithAddress({ required String address, + String? macAddress, List openPorts = const [], PingData? pingData, MdnsInfo? mdnsInfo, @@ -64,6 +67,7 @@ class ActiveHost { } return ActiveHost( internetAddress: internetAddressTemp, + macAddress: macAddress, openPorts: openPorts, pingData: pingData, mdnsInfoVar: mdnsInfo, @@ -72,6 +76,7 @@ class ActiveHost { factory ActiveHost.fromSendableActiveHost({ required SendableActiveHost sendableActiveHost, + String? macAddress, MdnsInfo? mdnsInfo, }) { final InternetAddress? internetAddressTemp = @@ -81,6 +86,7 @@ class ActiveHost { } return ActiveHost( internetAddress: internetAddressTemp, + macAddress: macAddress, openPorts: sendableActiveHost.openPorts, pingData: sendableActiveHost.pingData, mdnsInfoVar: mdnsInfo, @@ -111,6 +117,10 @@ class ActiveHost { /// Only works if arpData is not null and have valid mac address. late Future vendor; + String? _macAddress; + Future getMacAddress() async => + _macAddress ?? (await arpData)?.macAddress; + /// List of all the open port of this device List openPorts; @@ -130,7 +140,12 @@ class ActiveHost { PingData tempPingData = const PingData(); - Ping(host, count: 1, timeout: timeoutInSeconds).stream.listen((pingData) { + Ping( + host, + count: 1, + timeout: timeoutInSeconds, + forceCodepage: Platform.isWindows, + ).stream.listen((pingData) { final PingResponse? response = pingData.response; if (response != null) { final Duration? time = response.time; @@ -184,7 +199,9 @@ class ActiveHost { } Future setVendor() async { - return VendorTable.getVendor(arpData); + final String? macAddress = await getMacAddress(); + + return macAddress == null ? null : VendorTable.macToVendor(macAddress); } /// Try to find the mdns name of this device, if not exist mdns name will diff --git a/lib/src/services/impls/host_scanner_service_impl.dart b/lib/src/services/impls/host_scanner_service_impl.dart index 50d7c87..6a4ce56 100644 --- a/lib/src/services/impls/host_scanner_service_impl.dart +++ b/lib/src/services/impls/host_scanner_service_impl.dart @@ -6,6 +6,7 @@ import 'package:dart_ping/dart_ping.dart'; import 'package:network_tools/network_tools.dart'; import 'package:network_tools/src/network_tools_utils.dart'; import 'package:network_tools/src/services/arp_service.dart'; +import 'package:universal_io/io.dart'; /// Scans for all hosts in a subnet. class HostScannerServiceImpl extends HostScannerService { @@ -106,8 +107,12 @@ class HostScannerServiceImpl extends HostScannerService { }) async { SendableActiveHost? tempSendableActivateHost; - await for (final PingData pingData - in Ping(host, count: 1, timeout: timeoutInSeconds).stream) { + await for (final PingData pingData in Ping( + host, + count: 1, + timeout: timeoutInSeconds, + forceCodepage: Platform.isWindows, + ).stream) { final PingResponse? response = pingData.response; final PingError? pingError = pingData.error; if (response != null) {