PcapDevice.MacAddress throws NullReferenceException
Brought to you by:
chrismorgan,
tamirgal
On some machines, the property MacAddress from PcapDevice throws a NullReferenceException.
This is the code I use:
public void SetUp()
{
var devices = CaptureDeviceList.Instance;
// Sniff all devices
foreach (var device in devices)
{
device.Open(DeviceMode.Promiscuous);
device.Filter = "tcp portrange 8383-9000";
Logger.WriteLine("[DEBUG] Set filter for {0}", device.Description);
device.OnPacketArrival += device_OnPacketArrival;
device.StartCapture();
_devices.Add(device);
if (device.LinkType != LinkLayers.Ethernet)
{
Logger.WriteLine("Skipped sniffing {0} ({1})", device.Description, device.MacAddress);
StopCapture(device);
}
else
{
Logger.WriteLine("Started sniffing {0} ({1})", device.Description, device.MacAddress);
}
}
}
I have no idea what triggers this, but I think I'll add a throw ... catch block for now.
Anonymous
One reason I found for this happening is if you have a virtual Ethernet adapter enabled such as the one that comes with Hyper-V. I disabled my virtual adapter and the problem ceased.