-
-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
25e92f9
commit 0825b7e
Showing
5 changed files
with
101 additions
and
14 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
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() { | ||
//TODO: add platform specific format | ||
if (Platform.isMacOS) { | ||
return '$host ($iPAddress) at $macAddress on $interfaceName ifscope [$interfaceType]'; | ||
} | ||
return '$host ($iPAddress) at $macAddress on $interfaceName ifscope [$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,46 @@ | ||
import 'dart:convert'; | ||
import 'dart:io'; | ||
|
||
import 'package:network_tools/src/models/arp_data.dart'; | ||
|
||
class ARPTable { | ||
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 { | ||
//todo: implement windows regex | ||
return null; | ||
} | ||
for (final entry in entries) { | ||
final match = pattern.firstMatch(entry); | ||
if (match != null) { | ||
final arpData = ARPData( | ||
host: match.namedGroup("host"), | ||
iPAddress: match.namedGroup("ip"), | ||
macAddress: match.namedGroup("mac"), | ||
interfaceName: match.namedGroup("intf"), | ||
interfaceType: match.namedGroup("typ"), | ||
); | ||
final key = arpData.iPAddress; | ||
if (key != null) { | ||
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