-
-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #139 from osociety/arp-data
Arp data for Linux and Macos
- Loading branch information
Showing
7 changed files
with
115 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import 'dart:io'; | ||
|
||
class ARPData { | ||
ARPData({ | ||
required this.host, | ||
required this.iPAddress, | ||
required this.macAddress, | ||
required this.interfaceName, | ||
required this.interfaceType, | ||
}); | ||
|
||
final String? host; | ||
final String? iPAddress; | ||
final String? macAddress; | ||
final String? interfaceName; | ||
final String? interfaceType; | ||
|
||
@override | ||
String toString() { | ||
if (Platform.isMacOS) { | ||
return '$host ($iPAddress) at $macAddress on $interfaceName ifscope [$interfaceType]'; | ||
} else if (Platform.isLinux) { | ||
return '$host ($iPAddress) at $macAddress [$interfaceType] on $interfaceName'; | ||
} else if (Platform.isWindows) { | ||
return 'Internet Address: $iPAddress, Physical Address: $macAddress, Type: $interfaceType'; | ||
} | ||
return '$host ($iPAddress) at $macAddress on $interfaceName type [$interfaceType]'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import 'dart:convert'; | ||
import 'dart:io'; | ||
|
||
import 'package:logging/logging.dart'; | ||
import 'package:network_tools/src/models/arp_data.dart'; | ||
|
||
class ARPTable { | ||
static final arpLogger = Logger("arp-table-logger"); | ||
static final arpTable = <String, ARPData>{}; | ||
static bool resolved = false; | ||
|
||
static Future<ARPData?> entryFor(String address) async { | ||
if (resolved) return arpTable[address]; | ||
final result = await Process.run('arp', ['-a']); | ||
final entries = const LineSplitter().convert(result.stdout.toString()); | ||
RegExp? pattern; | ||
if (Platform.isMacOS) { | ||
pattern = RegExp( | ||
r'(?<host>[\w.?]*)\s\((?<ip>.*)\)\sat\s(?<mac>.*)\son\s(?<intf>\w+)\sifscope\s*(\w*)\s*\[(?<typ>.*)\]', | ||
); | ||
} else if (Platform.isLinux) { | ||
pattern = RegExp( | ||
r'(?<host>[\w.?]*)\s\((?<ip>.*)\)\sat\s(?<mac>.*)\s\[(?<typ>.*)\]\son\s(?<intf>\w+)', | ||
); | ||
} else { | ||
pattern = RegExp(r'(?<ip>.*)\s(?<mac>.*)\s(?<typ>.*)'); | ||
} | ||
|
||
for (final entry in entries) { | ||
final match = pattern.firstMatch(entry); | ||
if (match != null) { | ||
final arpData = ARPData( | ||
host: match.groupNames.contains('host') | ||
? match.namedGroup("host") | ||
: null, | ||
iPAddress: match.namedGroup("ip"), | ||
macAddress: match.namedGroup("mac"), | ||
interfaceName: match.groupNames.contains('intf') | ||
? match.namedGroup("intf") | ||
: null, | ||
interfaceType: match.namedGroup("typ"), | ||
); | ||
final key = arpData.iPAddress; | ||
if (key != null) { | ||
arpLogger.fine("Adding entry to table -> $arpData"); | ||
arpTable[key] = arpData; | ||
} | ||
} | ||
} | ||
resolved = true; | ||
return arpTable[address]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters