4
4
using System . Management ;
5
5
using System . Net . NetworkInformation ;
6
6
using Microsoft . Win32 ;
7
+ using PcapDotNet . Core . Native ;
7
8
using PcapDotNet . Packets ;
8
9
using PcapDotNet . Packets . Ethernet ;
9
10
@@ -31,6 +32,8 @@ public static string GetGuid(this LivePacketDevice livePacketDevice)
31
32
throw new ArgumentNullException ( "livePacketDevice" ) ;
32
33
33
34
string livePacketDeviceName = livePacketDevice . Name ;
35
+ if ( Environment . OSVersion . Platform == PlatformID . Unix || Environment . OSVersion . Platform == PlatformID . MacOSX )
36
+ return livePacketDeviceName ;
34
37
if ( ! livePacketDeviceName . StartsWith ( NamePrefix , StringComparison . Ordinal ) )
35
38
{
36
39
throw new InvalidOperationException ( string . Format ( CultureInfo . InvariantCulture ,
@@ -50,11 +53,14 @@ public static string GetGuid(this LivePacketDevice livePacketDevice)
50
53
/// <exception cref="InvalidOperationException">When the PNPDeviceID cannot be retrieved from the registry.</exception>
51
54
public static string GetPnpDeviceId ( this LivePacketDevice livePacketDevice )
52
55
{
56
+ if ( Environment . OSVersion . Platform == PlatformID . Unix || Environment . OSVersion . Platform == PlatformID . MacOSX )
57
+ throw new InvalidOperationException ( "Platform not supported" ) ;
58
+
53
59
string guid = livePacketDevice . GetGuid ( ) ;
54
60
55
61
using ( RegistryKey key = Registry . LocalMachine . OpenSubKey ( NetworkConnectionConfigKey + @"\" + guid + @"\Connection" ) )
56
62
{
57
- string pnpDeviceId = key . GetValue ( "PnpInstanceID" ) as string ;
63
+ string pnpDeviceId = key ? . GetValue ( "PnpInstanceID" ) as string ;
58
64
if ( pnpDeviceId == null )
59
65
throw new InvalidOperationException ( "Could not find PNP Device ID in the registry" ) ;
60
66
return pnpDeviceId ;
@@ -75,7 +81,7 @@ public static NetworkInterface GetNetworkInterface(this LivePacketDevice livePac
75
81
throw new ArgumentNullException ( "livePacketDevice" ) ;
76
82
77
83
string guid = GetGuid ( livePacketDevice ) ;
78
- return NetworkInterface . GetAllNetworkInterfaces ( ) . FirstOrDefault ( networkInterface => networkInterface . Id == guid ) ;
84
+ return Interop . Pcap . GetAllNetworkInterfacesByDotNet ( ) . FirstOrDefault ( networkInterface => networkInterface . Id == guid ) ;
79
85
}
80
86
81
87
/// <summary>
@@ -95,6 +101,12 @@ public static MacAddress GetMacAddress(this LivePacketDevice livePacketDevice)
95
101
return new MacAddress ( addressBytes . ReadUInt48 ( 0 , Endianity . Big ) ) ;
96
102
}
97
103
104
+ if ( Environment . OSVersion . Platform != PlatformID . Unix && Environment . OSVersion . Platform != PlatformID . MacOSX
105
+ && livePacketDevice . Name == "rpcap://\\ Device\\ NPF_Loopback" )
106
+ {
107
+ return new MacAddress ( ) ;
108
+ }
109
+
98
110
return livePacketDevice . GetMacAddressWmi ( ) ;
99
111
}
100
112
@@ -107,6 +119,9 @@ public static MacAddress GetMacAddress(this LivePacketDevice livePacketDevice)
107
119
/// <exception cref="InvalidOperationException">When the <see cref="MacAddress"/> cannot be retrieved using WMI.</exception>
108
120
private static MacAddress GetMacAddressWmi ( this LivePacketDevice livePacketDevice )
109
121
{
122
+ if ( Environment . OSVersion . Platform == PlatformID . Unix || Environment . OSVersion . Platform == PlatformID . MacOSX )
123
+ throw new InvalidOperationException ( "No MAC Address on device: " + livePacketDevice . Name ) ;
124
+
110
125
string pnpDeviceId = livePacketDevice . GetPnpDeviceId ( ) ;
111
126
string escapedPnpDeviceId = pnpDeviceId . Replace ( @"\" , @"\\" ) ;
112
127
@@ -128,4 +143,4 @@ private static MacAddress GetMacAddressWmi(this LivePacketDevice livePacketDevic
128
143
throw new InvalidOperationException ( "No MAC Address for WMI instance with PNP Device ID: " + pnpDeviceId ) ;
129
144
}
130
145
}
131
- }
146
+ }
0 commit comments