LCOV - code coverage report
Current view: top level - lib/src/models - arp_data.dart (source / functions) Coverage Total Hit
Test: lcov.info Lines: 87.9 % 33 29
Test Date: 2026-11-15 16:52:47 Functions: - 0 0

            Line data    Source code
       1              : import 'package:json_annotation/json_annotation.dart';
       2              : import 'package:network_tools/src/database/drift_database.dart';
       3              : import 'package:universal_io/io.dart';
       4              : 
       5              : part 'arp_data.g.dart';
       6              : 
       7              : /// One row of the operating system's neighbor (ARP) table.
       8              : ///
       9              : /// Host scanners attach these to [ActiveHost.arpData] on Linux, macOS and
      10              : /// Windows. The table is read in numeric form, so [hostname] is usually empty;
      11              : /// device names come from reverse DNS and mDNS instead.
      12              : @JsonSerializable()
      13              : class ARPData {
      14            8 :   ARPData({
      15              :     required this.hostname,
      16              :     required this.iPAddress,
      17              :     required this.macAddress,
      18              :     required this.interfaceName,
      19              :     required this.interfaceType,
      20              :     required this.createdAt,
      21              :   });
      22              : 
      23            1 :   factory ARPData.fromJson(Map<String, dynamic> json) =>
      24            1 :       _$ARPDataFromJson(json);
      25              : 
      26            2 :   factory ARPData.fromDriftData(ARPDriftData arpData) => ARPData(
      27            1 :     hostname: arpData.hostname,
      28            1 :     iPAddress: arpData.iPAddress,
      29            1 :     macAddress: arpData.macAddress,
      30            1 :     interfaceName: arpData.interfaceName,
      31            1 :     interfaceType: arpData.interfaceType,
      32            1 :     createdAt: arpData.createdAt,
      33              :   );
      34              : 
      35            7 :   factory ARPData.fromRegExpMatch(RegExpMatch match) {
      36           14 :     final String parsedHostname = match.groupNames.contains('host')
      37           14 :         ? (match.namedGroup("host")?.trim() ?? nullHostName)
      38              :         : nullHostName;
      39            7 :     return ARPData(
      40              :       // `arp -an` prints "?" when it did not resolve a name; treat that as
      41              :       // "unknown" instead of a usable device name.
      42            7 :       hostname: parsedHostname == '?' ? nullHostName : parsedHostname,
      43           14 :       iPAddress: match.namedGroup("ip")?.trim() ?? ARPData.nullIPAddress,
      44           14 :       macAddress: match.namedGroup("mac")?.trim() ?? ARPData.nullMacAddress,
      45           14 :       interfaceName: match.groupNames.contains('intf')
      46           14 :           ? (match.namedGroup("intf")?.trim() ?? nullHostName)
      47              :           : nullHostName,
      48              :       interfaceType:
      49           14 :           match.namedGroup("typ")?.trim() ?? ARPData.nullInterfaceType,
      50            7 :       createdAt: DateTime.now(),
      51              :     );
      52              :   }
      53              : 
      54              :   /// Host name recorded in the table, or an empty string when the platform did
      55              :   /// not resolve one. Populated only by the name-resolving `arp -a` read.
      56              :   final String hostname;
      57              : 
      58              :   /// The host's IPv4 address.
      59              :   final String iPAddress;
      60              :   static const String primaryKeySembast = 'iPAddress';
      61              :   static const String nullIPAddress = '0.0.0.0';
      62              :   static const String nullMacAddress = 'ff:ff:ff:ff:ff:ff';
      63              :   static const String nullInterfaceType = 'ethernet';
      64              :   static const String nullHostName = '';
      65              : 
      66              :   /// MAC address of the host, normalized to two hex digits per octet.
      67              :   final String macAddress;
      68              : 
      69              :   /// Name of the interface the entry was learned on, e.g. `en0` or `eth0`.
      70              :   final String interfaceName;
      71              : 
      72              :   /// Link type reported by the platform, e.g. `ether` or `ethernet`.
      73              :   final String interfaceType;
      74              : 
      75              :   /// When this row was read into the local database.
      76              :   final DateTime createdAt;
      77              : 
      78            2 :   Map<String, dynamic> toJson() => _$ARPDataToJson(this);
      79              : 
      80              :   /// Whether [iPAddress] holds a real address instead of [nullIPAddress].
      81            3 :   bool get notNullIPAddress => iPAddress != nullIPAddress;
      82              : 
      83              :   /// Whether [macAddress] holds a real MAC instead of [nullMacAddress].
      84            3 :   bool get notNullMacAddress => macAddress != nullMacAddress;
      85              : 
      86              :   /// Whether [interfaceType] holds a real link type instead of
      87              :   /// [nullInterfaceType].
      88            3 :   bool get notNullInterfaceType => interfaceType != nullInterfaceType;
      89              : 
      90            7 :   @override
      91              :   String toString() {
      92            7 :     if (Platform.isMacOS) {
      93            0 :       return '$hostname ($iPAddress) at $macAddress on $interfaceName ifscope [$interfaceType]';
      94            7 :     } else if (Platform.isLinux) {
      95           42 :       return '$hostname ($iPAddress) at $macAddress [$interfaceType] on $interfaceName';
      96            0 :     } else if (Platform.isWindows) {
      97            0 :       return 'Internet Address: $iPAddress, Physical Address: $macAddress, Type: $interfaceType';
      98              :     }
      99            0 :     return '$hostname ($iPAddress) at $macAddress on $interfaceName type [$interfaceType]';
     100              :   }
     101              : }
        

Generated by: LCOV version 2.0-1