Skip to content

Commit

Permalink
Merge pull request #195 from osociety/dev
Browse files Browse the repository at this point in the history
Dev -> Main
  • Loading branch information
git-elliot authored Mar 30, 2024
2 parents 84c9068 + 434f53b commit bc1c630
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 18 deletions.
22 changes: 8 additions & 14 deletions lib/src/device_info/vendor_table.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,14 @@ import 'package:universal_io/io.dart';
class VendorTable {
static Map<dynamic, dynamic> _vendorTableMap = {};

static Future<Vendor?> getVendor(Future<ARPData?> 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<Vendor?> macToVendor(String macAddress) async {
await createVendorTableMap();
final pattern = macAddress.contains(':') ? ':' : '-';
return _vendorTableMap[macAddress
.split(pattern)
.sublist(0, 3)
.join()
.toUpperCase()] as Vendor?;
}

static Future<void> createVendorTableMap() async {
Expand Down
21 changes: 19 additions & 2 deletions lib/src/models/active_host.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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('.')) {
Expand Down Expand Up @@ -53,6 +55,7 @@ class ActiveHost {
}
factory ActiveHost.buildWithAddress({
required String address,
String? macAddress,
List<OpenPort> openPorts = const [],
PingData? pingData,
MdnsInfo? mdnsInfo,
Expand All @@ -64,6 +67,7 @@ class ActiveHost {
}
return ActiveHost(
internetAddress: internetAddressTemp,
macAddress: macAddress,
openPorts: openPorts,
pingData: pingData,
mdnsInfoVar: mdnsInfo,
Expand All @@ -72,6 +76,7 @@ class ActiveHost {

factory ActiveHost.fromSendableActiveHost({
required SendableActiveHost sendableActiveHost,
String? macAddress,
MdnsInfo? mdnsInfo,
}) {
final InternetAddress? internetAddressTemp =
Expand All @@ -81,6 +86,7 @@ class ActiveHost {
}
return ActiveHost(
internetAddress: internetAddressTemp,
macAddress: macAddress,
openPorts: sendableActiveHost.openPorts,
pingData: sendableActiveHost.pingData,
mdnsInfoVar: mdnsInfo,
Expand Down Expand Up @@ -111,6 +117,10 @@ class ActiveHost {
/// Only works if arpData is not null and have valid mac address.
late Future<Vendor?> vendor;

String? _macAddress;
Future<String?> getMacAddress() async =>
_macAddress ?? (await arpData)?.macAddress;

/// List of all the open port of this device
List<OpenPort> openPorts;

Expand All @@ -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;
Expand Down Expand Up @@ -184,7 +199,9 @@ class ActiveHost {
}

Future<Vendor?> 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
Expand Down
9 changes: 7 additions & 2 deletions lib/src/services/impls/host_scanner_service_impl.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit bc1c630

Please sign in to comment.