Commit 2b6be2f2 authored by Brickner_cp's avatar Brickner_cp

Add a conversion from NetworkInterface to LivePacketDevice -...

Add a conversion from NetworkInterface to LivePacketDevice - NetworkInterface.GetLivePacketDevice().
parent 5a676ce0
......@@ -14,8 +14,8 @@ namespace PcapDotNet.Core.Extensions
/// </summary>
public static class LivePacketDeviceExtensions
{
const string NamePrefix = @"rpcap://\Device\NPF_";
const string NetworkConnectionConfigKey = @"SYSTEM\CurrentControlSet\Control\Network\{4D36E972-E325-11CE-BFC1-08002BE10318}";
internal const string NamePrefix = @"rpcap://\Device\NPF_";
private const string NetworkConnectionConfigKey = @"SYSTEM\CurrentControlSet\Control\Network\{4D36E972-E325-11CE-BFC1-08002BE10318}";
/// <summary>
/// Returns the GUID (NetCfgInstanceId) for a <see cref="LivePacketDevice"/> instance.
......@@ -27,7 +27,7 @@ namespace PcapDotNet.Core.Extensions
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1011:ConsiderPassingBaseTypesAsParameters")]
public static string GetGuid(this LivePacketDevice livePacketDevice)
{
if (livePacketDevice == null)
if (livePacketDevice == null)
throw new ArgumentNullException("livePacketDevice");
string livePacketDeviceName = livePacketDevice.Name;
......@@ -71,7 +71,7 @@ namespace PcapDotNet.Core.Extensions
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1011:ConsiderPassingBaseTypesAsParameters")]
public static NetworkInterface GetNetworkInterface(this LivePacketDevice livePacketDevice)
{
if (livePacketDevice == null)
if (livePacketDevice == null)
throw new ArgumentNullException("livePacketDevice");
string guid = GetGuid(livePacketDevice);
......@@ -113,7 +113,9 @@ namespace PcapDotNet.Core.Extensions
ManagementScope scope = new ManagementScope(@"\\.\root\cimv2");
scope.Connect();
var searcher = new ManagementObjectSearcher(scope, new SelectQuery("SELECT MACAddress FROM Win32_NetworkAdapter WHERE PNPDeviceID='" + escapedPnpDeviceId + "'"));
var searcher = new ManagementObjectSearcher(scope,
new SelectQuery("SELECT MACAddress FROM Win32_NetworkAdapter WHERE PNPDeviceID='" + escapedPnpDeviceId +
"'"));
foreach (ManagementObject managementObject in searcher.Get())
{
string macAddress = managementObject["MACAddress"] as string;
......
using System;
using System.Linq;
using System.Net.NetworkInformation;
namespace PcapDotNet.Core.Extensions
{
/// <summary>
/// Extension methods for NetworkInterface class.
/// </summary>
public static class NetworkInterfaceExtensions
{
/// <summary>
/// Returns the LivePacketDevice of the given NetworkInterface.
/// The LivePacketDevice is found using the NetworkInterface's id and the LivePacketDevice's name.
/// If no interface is found, null is returned.
/// </summary>
/// <param name="networkInterface">The NetworkInterface to look for a matching LivePacketDevice for.</param>
/// <returns>The LivePacketDevice found according to the given NetworkInterface or null if none is found.</returns>
public static LivePacketDevice GetLivePacketDevice(this NetworkInterface networkInterface)
{
if (networkInterface == null)
throw new ArgumentNullException("networkInterface");
return LivePacketDevice.AllLocalMachine.FirstOrDefault(device => device.Name == LivePacketDeviceExtensions.NamePrefix + networkInterface.Id);
}
}
}
\ No newline at end of file
......@@ -79,6 +79,7 @@
</ItemGroup>
<ItemGroup>
<Compile Include="LivePacketDeviceExtensions.cs" />
<Compile Include="NetworkInterfaceExtensions.cs" />
<Compile Include="PacketCommunicatorExtensions.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
......
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.NetworkInformation;
using System.Threading;
using PcapDotNet.Base;
using PcapDotNet.Core.Extensions;
......@@ -841,9 +842,10 @@ namespace PcapDotNet.Core.Test
public static PacketCommunicator OpenLiveDevice()
{
IList<LivePacketDevice> devices = LivePacketDevice.AllLocalMachine;
MoreAssert.IsBiggerOrEqual(1, devices.Count);
LivePacketDevice device = devices[0];
NetworkInterface networkInterface =
NetworkInterface.GetAllNetworkInterfaces().FirstOrDefault(
ni => !ni.IsReceiveOnly && ni.NetworkInterfaceType == NetworkInterfaceType.Ethernet && ni.OperationalStatus == OperationalStatus.Up);
LivePacketDevice device = networkInterface.GetLivePacketDevice();
MoreAssert.IsMatch(@"Network adapter '.*' on local host", device.Description);
Assert.AreEqual(DeviceAttributes.None, device.Attributes);
Assert.AreNotEqual(MacAddress.Zero, device.GetMacAddress());
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment